genattrtab.c 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352
  1. /* Generate code from machine description to compute values of attributes.
  2. Copyright (C) 1991-2015 Free Software Foundation, Inc.
  3. Contributed by Richard Kenner (kenner@vlsi1.ultra.nyu.edu)
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* This program handles insn attributes and the DEFINE_DELAY and
  17. DEFINE_INSN_RESERVATION definitions.
  18. It produces a series of functions named `get_attr_...', one for each insn
  19. attribute. Each of these is given the rtx for an insn and returns a member
  20. of the enum for the attribute.
  21. These subroutines have the form of a `switch' on the INSN_CODE (via
  22. `recog_memoized'). Each case either returns a constant attribute value
  23. or a value that depends on tests on other attributes, the form of
  24. operands, or some random C expression (encoded with a SYMBOL_REF
  25. expression).
  26. If the attribute `alternative', or a random C expression is present,
  27. `constrain_operands' is called. If either of these cases of a reference to
  28. an operand is found, `extract_insn' is called.
  29. The special attribute `length' is also recognized. For this operand,
  30. expressions involving the address of an operand or the current insn,
  31. (address (pc)), are valid. In this case, an initial pass is made to
  32. set all lengths that do not depend on address. Those that do are set to
  33. the maximum length. Then each insn that depends on an address is checked
  34. and possibly has its length changed. The process repeats until no further
  35. changed are made. The resulting lengths are saved for use by
  36. `get_attr_length'.
  37. A special form of DEFINE_ATTR, where the expression for default value is a
  38. CONST expression, indicates an attribute that is constant for a given run
  39. of the compiler. The subroutine generated for these attributes has no
  40. parameters as it does not depend on any particular insn. Constant
  41. attributes are typically used to specify which variety of processor is
  42. used.
  43. Internal attributes are defined to handle DEFINE_DELAY and
  44. DEFINE_INSN_RESERVATION. Special routines are output for these cases.
  45. This program works by keeping a list of possible values for each attribute.
  46. These include the basic attribute choices, default values for attribute, and
  47. all derived quantities.
  48. As the description file is read, the definition for each insn is saved in a
  49. `struct insn_def'. When the file reading is complete, a `struct insn_ent'
  50. is created for each insn and chained to the corresponding attribute value,
  51. either that specified, or the default.
  52. An optimization phase is then run. This simplifies expressions for each
  53. insn. EQ_ATTR tests are resolved, whenever possible, to a test that
  54. indicates when the attribute has the specified value for the insn. This
  55. avoids recursive calls during compilation.
  56. The strategy used when processing DEFINE_DELAY definitions is to create
  57. arbitrarily complex expressions and have the optimization simplify them.
  58. Once optimization is complete, any required routines and definitions
  59. will be written.
  60. An optimization that is not yet implemented is to hoist the constant
  61. expressions entirely out of the routines and definitions that are written.
  62. A way to do this is to iterate over all possible combinations of values
  63. for constant attributes and generate a set of functions for that given
  64. combination. An initialization function would be written that evaluates
  65. the attributes and installs the corresponding set of routines and
  66. definitions (each would be accessed through a pointer).
  67. We use the flags in an RTX as follows:
  68. `unchanging' (ATTR_IND_SIMPLIFIED_P): This rtx is fully simplified
  69. independent of the insn code.
  70. `in_struct' (ATTR_CURR_SIMPLIFIED_P): This rtx is fully simplified
  71. for the insn code currently being processed (see optimize_attrs).
  72. `return_val' (ATTR_PERMANENT_P): This rtx is permanent and unique
  73. (see attr_rtx). */
  74. #define ATTR_IND_SIMPLIFIED_P(RTX) (RTX_FLAG ((RTX), unchanging))
  75. #define ATTR_CURR_SIMPLIFIED_P(RTX) (RTX_FLAG ((RTX), in_struct))
  76. #define ATTR_PERMANENT_P(RTX) (RTX_FLAG ((RTX), return_val))
  77. #if 0
  78. #define strcmp_check(S1, S2) ((S1) == (S2) \
  79. ? 0 \
  80. : (gcc_assert (strcmp ((S1), (S2))), 1))
  81. #else
  82. #define strcmp_check(S1, S2) ((S1) != (S2))
  83. #endif
  84. #include "bconfig.h"
  85. #include "system.h"
  86. #include "coretypes.h"
  87. #include "tm.h"
  88. #include "rtl.h"
  89. #include "obstack.h"
  90. #include "errors.h"
  91. #include "read-md.h"
  92. #include "gensupport.h"
  93. #include "fnmatch.h"
  94. #define DEBUG 0
  95. /* Flags for make_internal_attr's `special' parameter. */
  96. #define ATTR_NONE 0
  97. #define ATTR_SPECIAL (1 << 0)
  98. static struct obstack obstack1, obstack2;
  99. static struct obstack *hash_obstack = &obstack1;
  100. static struct obstack *temp_obstack = &obstack2;
  101. /* enough space to reserve for printing out ints */
  102. #define MAX_DIGITS (HOST_BITS_PER_INT * 3 / 10 + 3)
  103. /* Define structures used to record attributes and values. */
  104. /* As each DEFINE_INSN, DEFINE_PEEPHOLE, or DEFINE_ASM_ATTRIBUTES is
  105. encountered, we store all the relevant information into a
  106. `struct insn_def'. This is done to allow attribute definitions to occur
  107. anywhere in the file. */
  108. struct insn_def
  109. {
  110. struct insn_def *next; /* Next insn in chain. */
  111. rtx def; /* The DEFINE_... */
  112. int insn_code; /* Instruction number. */
  113. int insn_index; /* Expression number in file, for errors. */
  114. const char *filename; /* Filename. */
  115. int lineno; /* Line number. */
  116. int num_alternatives; /* Number of alternatives. */
  117. int vec_idx; /* Index of attribute vector in `def'. */
  118. };
  119. /* Once everything has been read in, we store in each attribute value a list
  120. of insn codes that have that value. Here is the structure used for the
  121. list. */
  122. struct insn_ent
  123. {
  124. struct insn_ent *next; /* Next in chain. */
  125. struct insn_def *def; /* Instruction definition. */
  126. };
  127. /* Each value of an attribute (either constant or computed) is assigned a
  128. structure which is used as the listhead of the insns that have that
  129. value. */
  130. struct attr_value
  131. {
  132. rtx value; /* Value of attribute. */
  133. struct attr_value *next; /* Next attribute value in chain. */
  134. struct insn_ent *first_insn; /* First insn with this value. */
  135. int num_insns; /* Number of insns with this value. */
  136. int has_asm_insn; /* True if this value used for `asm' insns */
  137. };
  138. /* Structure for each attribute. */
  139. struct attr_desc
  140. {
  141. char *name; /* Name of attribute. */
  142. const char *enum_name; /* Enum name for DEFINE_ENUM_NAME. */
  143. struct attr_desc *next; /* Next attribute. */
  144. struct attr_value *first_value; /* First value of this attribute. */
  145. struct attr_value *default_val; /* Default value for this attribute. */
  146. int lineno : 24; /* Line number. */
  147. unsigned is_numeric : 1; /* Values of this attribute are numeric. */
  148. unsigned is_const : 1; /* Attribute value constant for each run. */
  149. unsigned is_special : 1; /* Don't call `write_attr_set'. */
  150. };
  151. /* Structure for each DEFINE_DELAY. */
  152. struct delay_desc
  153. {
  154. rtx def; /* DEFINE_DELAY expression. */
  155. struct delay_desc *next; /* Next DEFINE_DELAY. */
  156. int num; /* Number of DEFINE_DELAY, starting at 1. */
  157. int lineno; /* Line number. */
  158. };
  159. struct attr_value_list
  160. {
  161. struct attr_value *av;
  162. struct insn_ent *ie;
  163. struct attr_desc *attr;
  164. struct attr_value_list *next;
  165. };
  166. /* Listheads of above structures. */
  167. /* This one is indexed by the first character of the attribute name. */
  168. #define MAX_ATTRS_INDEX 256
  169. static struct attr_desc *attrs[MAX_ATTRS_INDEX];
  170. static struct insn_def *defs;
  171. static struct delay_desc *delays;
  172. struct attr_value_list **insn_code_values;
  173. /* Other variables. */
  174. static int insn_code_number;
  175. static int insn_index_number;
  176. static int got_define_asm_attributes;
  177. static int must_extract;
  178. static int must_constrain;
  179. static int address_used;
  180. static int length_used;
  181. static int num_delays;
  182. static int have_annul_true, have_annul_false;
  183. static int num_insn_ents;
  184. /* Stores, for each insn code, the number of constraint alternatives. */
  185. static int *insn_n_alternatives;
  186. /* Stores, for each insn code, a bitmap that has bits on for each possible
  187. alternative. */
  188. static uint64_t *insn_alternatives;
  189. /* Used to simplify expressions. */
  190. static rtx true_rtx, false_rtx;
  191. /* Used to reduce calls to `strcmp' */
  192. static const char *alternative_name;
  193. static const char *length_str;
  194. static const char *delay_type_str;
  195. static const char *delay_1_0_str;
  196. static const char *num_delay_slots_str;
  197. /* Simplify an expression. Only call the routine if there is something to
  198. simplify. */
  199. #define SIMPLIFY_TEST_EXP(EXP,INSN_CODE,INSN_INDEX) \
  200. (ATTR_IND_SIMPLIFIED_P (EXP) || ATTR_CURR_SIMPLIFIED_P (EXP) ? (EXP) \
  201. : simplify_test_exp (EXP, INSN_CODE, INSN_INDEX))
  202. #define DEF_ATTR_STRING(S) (attr_string ((S), strlen (S)))
  203. /* Forward declarations of functions used before their definitions, only. */
  204. static char *attr_string (const char *, int);
  205. static char *attr_printf (unsigned int, const char *, ...)
  206. ATTRIBUTE_PRINTF_2;
  207. static rtx make_numeric_value (int);
  208. static struct attr_desc *find_attr (const char **, int);
  209. static rtx mk_attr_alt (uint64_t);
  210. static char *next_comma_elt (const char **);
  211. static rtx insert_right_side (enum rtx_code, rtx, rtx, int, int);
  212. static rtx copy_boolean (rtx);
  213. static int compares_alternatives_p (rtx);
  214. static void make_internal_attr (const char *, rtx, int);
  215. static void insert_insn_ent (struct attr_value *, struct insn_ent *);
  216. static void walk_attr_value (rtx);
  217. static int max_attr_value (rtx, int*);
  218. static int min_attr_value (rtx, int*);
  219. static int or_attr_value (rtx, int*);
  220. static rtx simplify_test_exp (rtx, int, int);
  221. static rtx simplify_test_exp_in_temp (rtx, int, int);
  222. static rtx copy_rtx_unchanging (rtx);
  223. static bool attr_alt_subset_p (rtx, rtx);
  224. static bool attr_alt_subset_of_compl_p (rtx, rtx);
  225. static void clear_struct_flag (rtx);
  226. static void write_attr_valueq (FILE *, struct attr_desc *, const char *);
  227. static struct attr_value *find_most_used (struct attr_desc *);
  228. static void write_attr_set (FILE *, struct attr_desc *, int, rtx,
  229. const char *, const char *, rtx,
  230. int, int, unsigned int);
  231. static void write_attr_case (FILE *, struct attr_desc *,
  232. struct attr_value *,
  233. int, const char *, const char *, int, rtx);
  234. static void write_attr_value (FILE *, struct attr_desc *, rtx);
  235. static void write_upcase (FILE *, const char *);
  236. static void write_indent (FILE *, int);
  237. static rtx identity_fn (rtx);
  238. static rtx zero_fn (rtx);
  239. static rtx one_fn (rtx);
  240. static rtx max_fn (rtx);
  241. static rtx min_fn (rtx);
  242. #define oballoc(T) XOBNEW (hash_obstack, T)
  243. #define oballocvec(T, N) XOBNEWVEC (hash_obstack, T, (N))
  244. /* This gen* file is unique, in that it writes out multiple files.
  245. Before GCC 4.8, insn-attrtab.c was written out containing many large
  246. functions and tables. This made insn-attrtab.c _the_ bottle-neck in
  247. a parallel build, and even made it impossible to build GCC on machines
  248. with relatively small RAM space (PR other/29442). Therefore, the
  249. atrribute functions/tables are now written out to three separate
  250. files: all "*insn_default_latency" functions go to LATENCY_FILE_NAME,
  251. all "*internal_dfa_insn_code" functions go to DFA_FILE_NAME, and the
  252. rest goes to ATTR_FILE_NAME. */
  253. static const char *attr_file_name = NULL;
  254. static const char *dfa_file_name = NULL;
  255. static const char *latency_file_name = NULL;
  256. static FILE *attr_file, *dfa_file, *latency_file;
  257. /* Hash table for sharing RTL and strings. */
  258. /* Each hash table slot is a bucket containing a chain of these structures.
  259. Strings are given negative hash codes; RTL expressions are given positive
  260. hash codes. */
  261. struct attr_hash
  262. {
  263. struct attr_hash *next; /* Next structure in the bucket. */
  264. unsigned int hashcode; /* Hash code of this rtx or string. */
  265. union
  266. {
  267. char *str; /* The string (negative hash codes) */
  268. rtx rtl; /* or the RTL recorded here. */
  269. } u;
  270. };
  271. /* Now here is the hash table. When recording an RTL, it is added to
  272. the slot whose index is the hash code mod the table size. Note
  273. that the hash table is used for several kinds of RTL (see attr_rtx)
  274. and for strings. While all these live in the same table, they are
  275. completely independent, and the hash code is computed differently
  276. for each. */
  277. #define RTL_HASH_SIZE 4093
  278. static struct attr_hash *attr_hash_table[RTL_HASH_SIZE];
  279. /* Here is how primitive or already-shared RTL's hash
  280. codes are made. */
  281. #define RTL_HASH(RTL) ((intptr_t) (RTL) & 0777777)
  282. /* Add an entry to the hash table for RTL with hash code HASHCODE. */
  283. static void
  284. attr_hash_add_rtx (unsigned int hashcode, rtx rtl)
  285. {
  286. struct attr_hash *h;
  287. h = XOBNEW (hash_obstack, struct attr_hash);
  288. h->hashcode = hashcode;
  289. h->u.rtl = rtl;
  290. h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
  291. attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
  292. }
  293. /* Add an entry to the hash table for STRING with hash code HASHCODE. */
  294. static void
  295. attr_hash_add_string (unsigned int hashcode, char *str)
  296. {
  297. struct attr_hash *h;
  298. h = XOBNEW (hash_obstack, struct attr_hash);
  299. h->hashcode = -hashcode;
  300. h->u.str = str;
  301. h->next = attr_hash_table[hashcode % RTL_HASH_SIZE];
  302. attr_hash_table[hashcode % RTL_HASH_SIZE] = h;
  303. }
  304. /* Generate an RTL expression, but avoid duplicates.
  305. Set the ATTR_PERMANENT_P flag for these permanent objects.
  306. In some cases we cannot uniquify; then we return an ordinary
  307. impermanent rtx with ATTR_PERMANENT_P clear.
  308. Args are as follows:
  309. rtx attr_rtx (code, [element1, ..., elementn]) */
  310. static rtx
  311. attr_rtx_1 (enum rtx_code code, va_list p)
  312. {
  313. rtx rt_val = NULL_RTX;/* RTX to return to caller... */
  314. unsigned int hashcode;
  315. struct attr_hash *h;
  316. struct obstack *old_obstack = rtl_obstack;
  317. /* For each of several cases, search the hash table for an existing entry.
  318. Use that entry if one is found; otherwise create a new RTL and add it
  319. to the table. */
  320. if (GET_RTX_CLASS (code) == RTX_UNARY)
  321. {
  322. rtx arg0 = va_arg (p, rtx);
  323. /* A permanent object cannot point to impermanent ones. */
  324. if (! ATTR_PERMANENT_P (arg0))
  325. {
  326. rt_val = rtx_alloc (code);
  327. XEXP (rt_val, 0) = arg0;
  328. return rt_val;
  329. }
  330. hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
  331. for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
  332. if (h->hashcode == hashcode
  333. && GET_CODE (h->u.rtl) == code
  334. && XEXP (h->u.rtl, 0) == arg0)
  335. return h->u.rtl;
  336. if (h == 0)
  337. {
  338. rtl_obstack = hash_obstack;
  339. rt_val = rtx_alloc (code);
  340. XEXP (rt_val, 0) = arg0;
  341. }
  342. }
  343. else if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
  344. || GET_RTX_CLASS (code) == RTX_COMM_ARITH
  345. || GET_RTX_CLASS (code) == RTX_COMPARE
  346. || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
  347. {
  348. rtx arg0 = va_arg (p, rtx);
  349. rtx arg1 = va_arg (p, rtx);
  350. /* A permanent object cannot point to impermanent ones. */
  351. if (! ATTR_PERMANENT_P (arg0) || ! ATTR_PERMANENT_P (arg1))
  352. {
  353. rt_val = rtx_alloc (code);
  354. XEXP (rt_val, 0) = arg0;
  355. XEXP (rt_val, 1) = arg1;
  356. return rt_val;
  357. }
  358. hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
  359. for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
  360. if (h->hashcode == hashcode
  361. && GET_CODE (h->u.rtl) == code
  362. && XEXP (h->u.rtl, 0) == arg0
  363. && XEXP (h->u.rtl, 1) == arg1)
  364. return h->u.rtl;
  365. if (h == 0)
  366. {
  367. rtl_obstack = hash_obstack;
  368. rt_val = rtx_alloc (code);
  369. XEXP (rt_val, 0) = arg0;
  370. XEXP (rt_val, 1) = arg1;
  371. }
  372. }
  373. else if (code == SYMBOL_REF
  374. || (GET_RTX_LENGTH (code) == 1
  375. && GET_RTX_FORMAT (code)[0] == 's'))
  376. {
  377. char *arg0 = va_arg (p, char *);
  378. arg0 = DEF_ATTR_STRING (arg0);
  379. hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0));
  380. for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
  381. if (h->hashcode == hashcode
  382. && GET_CODE (h->u.rtl) == code
  383. && XSTR (h->u.rtl, 0) == arg0)
  384. return h->u.rtl;
  385. if (h == 0)
  386. {
  387. rtl_obstack = hash_obstack;
  388. rt_val = rtx_alloc (code);
  389. XSTR (rt_val, 0) = arg0;
  390. if (code == SYMBOL_REF)
  391. X0EXP (rt_val, 1) = NULL_RTX;
  392. }
  393. }
  394. else if (GET_RTX_LENGTH (code) == 2
  395. && GET_RTX_FORMAT (code)[0] == 's'
  396. && GET_RTX_FORMAT (code)[1] == 's')
  397. {
  398. char *arg0 = va_arg (p, char *);
  399. char *arg1 = va_arg (p, char *);
  400. hashcode = ((HOST_WIDE_INT) code + RTL_HASH (arg0) + RTL_HASH (arg1));
  401. for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
  402. if (h->hashcode == hashcode
  403. && GET_CODE (h->u.rtl) == code
  404. && XSTR (h->u.rtl, 0) == arg0
  405. && XSTR (h->u.rtl, 1) == arg1)
  406. return h->u.rtl;
  407. if (h == 0)
  408. {
  409. rtl_obstack = hash_obstack;
  410. rt_val = rtx_alloc (code);
  411. XSTR (rt_val, 0) = arg0;
  412. XSTR (rt_val, 1) = arg1;
  413. }
  414. }
  415. else if (code == CONST_INT)
  416. {
  417. HOST_WIDE_INT arg0 = va_arg (p, HOST_WIDE_INT);
  418. if (arg0 == 0)
  419. return false_rtx;
  420. else if (arg0 == 1)
  421. return true_rtx;
  422. else
  423. goto nohash;
  424. }
  425. else
  426. {
  427. int i; /* Array indices... */
  428. const char *fmt; /* Current rtx's format... */
  429. nohash:
  430. rt_val = rtx_alloc (code); /* Allocate the storage space. */
  431. fmt = GET_RTX_FORMAT (code); /* Find the right format... */
  432. for (i = 0; i < GET_RTX_LENGTH (code); i++)
  433. {
  434. switch (*fmt++)
  435. {
  436. case '0': /* Unused field. */
  437. break;
  438. case 'i': /* An integer? */
  439. XINT (rt_val, i) = va_arg (p, int);
  440. break;
  441. case 'w': /* A wide integer? */
  442. XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
  443. break;
  444. case 's': /* A string? */
  445. XSTR (rt_val, i) = va_arg (p, char *);
  446. break;
  447. case 'e': /* An expression? */
  448. case 'u': /* An insn? Same except when printing. */
  449. XEXP (rt_val, i) = va_arg (p, rtx);
  450. break;
  451. case 'E': /* An RTX vector? */
  452. XVEC (rt_val, i) = va_arg (p, rtvec);
  453. break;
  454. default:
  455. gcc_unreachable ();
  456. }
  457. }
  458. return rt_val;
  459. }
  460. rtl_obstack = old_obstack;
  461. attr_hash_add_rtx (hashcode, rt_val);
  462. ATTR_PERMANENT_P (rt_val) = 1;
  463. return rt_val;
  464. }
  465. static rtx
  466. attr_rtx (enum rtx_code code, ...)
  467. {
  468. rtx result;
  469. va_list p;
  470. va_start (p, code);
  471. result = attr_rtx_1 (code, p);
  472. va_end (p);
  473. return result;
  474. }
  475. /* Create a new string printed with the printf line arguments into a space
  476. of at most LEN bytes:
  477. rtx attr_printf (len, format, [arg1, ..., argn]) */
  478. static char *
  479. attr_printf (unsigned int len, const char *fmt, ...)
  480. {
  481. char str[256];
  482. va_list p;
  483. va_start (p, fmt);
  484. gcc_assert (len < sizeof str); /* Leave room for \0. */
  485. vsprintf (str, fmt, p);
  486. va_end (p);
  487. return DEF_ATTR_STRING (str);
  488. }
  489. static rtx
  490. attr_eq (const char *name, const char *value)
  491. {
  492. return attr_rtx (EQ_ATTR, DEF_ATTR_STRING (name), DEF_ATTR_STRING (value));
  493. }
  494. static const char *
  495. attr_numeral (int n)
  496. {
  497. return XSTR (make_numeric_value (n), 0);
  498. }
  499. /* Return a permanent (possibly shared) copy of a string STR (not assumed
  500. to be null terminated) with LEN bytes. */
  501. static char *
  502. attr_string (const char *str, int len)
  503. {
  504. struct attr_hash *h;
  505. unsigned int hashcode;
  506. int i;
  507. char *new_str;
  508. /* Compute the hash code. */
  509. hashcode = (len + 1) * 613U + (unsigned) str[0];
  510. for (i = 1; i < len; i += 2)
  511. hashcode = ((hashcode * 613) + (unsigned) str[i]);
  512. if ((int) hashcode < 0)
  513. hashcode = -hashcode;
  514. /* Search the table for the string. */
  515. for (h = attr_hash_table[hashcode % RTL_HASH_SIZE]; h; h = h->next)
  516. if (h->hashcode == -hashcode && h->u.str[0] == str[0]
  517. && !strncmp (h->u.str, str, len))
  518. return h->u.str; /* <-- return if found. */
  519. /* Not found; create a permanent copy and add it to the hash table. */
  520. new_str = XOBNEWVAR (hash_obstack, char, len + 1);
  521. memcpy (new_str, str, len);
  522. new_str[len] = '\0';
  523. attr_hash_add_string (hashcode, new_str);
  524. copy_md_ptr_loc (new_str, str);
  525. return new_str; /* Return the new string. */
  526. }
  527. /* Check two rtx's for equality of contents,
  528. taking advantage of the fact that if both are hashed
  529. then they can't be equal unless they are the same object. */
  530. static int
  531. attr_equal_p (rtx x, rtx y)
  532. {
  533. return (x == y || (! (ATTR_PERMANENT_P (x) && ATTR_PERMANENT_P (y))
  534. && rtx_equal_p (x, y)));
  535. }
  536. /* Copy an attribute value expression,
  537. descending to all depths, but not copying any
  538. permanent hashed subexpressions. */
  539. static rtx
  540. attr_copy_rtx (rtx orig)
  541. {
  542. rtx copy;
  543. int i, j;
  544. RTX_CODE code;
  545. const char *format_ptr;
  546. /* No need to copy a permanent object. */
  547. if (ATTR_PERMANENT_P (orig))
  548. return orig;
  549. code = GET_CODE (orig);
  550. switch (code)
  551. {
  552. case REG:
  553. CASE_CONST_ANY:
  554. case SYMBOL_REF:
  555. case MATCH_TEST:
  556. case CODE_LABEL:
  557. case PC:
  558. case CC0:
  559. return orig;
  560. default:
  561. break;
  562. }
  563. copy = rtx_alloc (code);
  564. PUT_MODE (copy, GET_MODE (orig));
  565. ATTR_IND_SIMPLIFIED_P (copy) = ATTR_IND_SIMPLIFIED_P (orig);
  566. ATTR_CURR_SIMPLIFIED_P (copy) = ATTR_CURR_SIMPLIFIED_P (orig);
  567. ATTR_PERMANENT_P (copy) = ATTR_PERMANENT_P (orig);
  568. format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
  569. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
  570. {
  571. switch (*format_ptr++)
  572. {
  573. case 'e':
  574. XEXP (copy, i) = XEXP (orig, i);
  575. if (XEXP (orig, i) != NULL)
  576. XEXP (copy, i) = attr_copy_rtx (XEXP (orig, i));
  577. break;
  578. case 'E':
  579. case 'V':
  580. XVEC (copy, i) = XVEC (orig, i);
  581. if (XVEC (orig, i) != NULL)
  582. {
  583. XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
  584. for (j = 0; j < XVECLEN (copy, i); j++)
  585. XVECEXP (copy, i, j) = attr_copy_rtx (XVECEXP (orig, i, j));
  586. }
  587. break;
  588. case 'n':
  589. case 'i':
  590. XINT (copy, i) = XINT (orig, i);
  591. break;
  592. case 'w':
  593. XWINT (copy, i) = XWINT (orig, i);
  594. break;
  595. case 's':
  596. case 'S':
  597. XSTR (copy, i) = XSTR (orig, i);
  598. break;
  599. default:
  600. gcc_unreachable ();
  601. }
  602. }
  603. return copy;
  604. }
  605. /* Given a test expression for an attribute, ensure it is validly formed.
  606. IS_CONST indicates whether the expression is constant for each compiler
  607. run (a constant expression may not test any particular insn).
  608. Convert (eq_attr "att" "a1,a2") to (ior (eq_attr ... ) (eq_attrq ..))
  609. and (eq_attr "att" "!a1") to (not (eq_attr "att" "a1")). Do the latter
  610. test first so that (eq_attr "att" "!a1,a2,a3") works as expected.
  611. Update the string address in EQ_ATTR expression to be the same used
  612. in the attribute (or `alternative_name') to speed up subsequent
  613. `find_attr' calls and eliminate most `strcmp' calls.
  614. Return the new expression, if any. */
  615. static rtx
  616. check_attr_test (rtx exp, int is_const, int lineno)
  617. {
  618. struct attr_desc *attr;
  619. struct attr_value *av;
  620. const char *name_ptr, *p;
  621. rtx orexp, newexp;
  622. switch (GET_CODE (exp))
  623. {
  624. case EQ_ATTR:
  625. /* Handle negation test. */
  626. if (XSTR (exp, 1)[0] == '!')
  627. return check_attr_test (attr_rtx (NOT,
  628. attr_eq (XSTR (exp, 0),
  629. &XSTR (exp, 1)[1])),
  630. is_const, lineno);
  631. else if (n_comma_elts (XSTR (exp, 1)) == 1)
  632. {
  633. attr = find_attr (&XSTR (exp, 0), 0);
  634. if (attr == NULL)
  635. {
  636. if (! strcmp (XSTR (exp, 0), "alternative"))
  637. return mk_attr_alt (((uint64_t) 1) << atoi (XSTR (exp, 1)));
  638. else
  639. fatal ("unknown attribute `%s' in EQ_ATTR", XSTR (exp, 0));
  640. }
  641. if (is_const && ! attr->is_const)
  642. fatal ("constant expression uses insn attribute `%s' in EQ_ATTR",
  643. XSTR (exp, 0));
  644. /* Copy this just to make it permanent,
  645. so expressions using it can be permanent too. */
  646. exp = attr_eq (XSTR (exp, 0), XSTR (exp, 1));
  647. /* It shouldn't be possible to simplify the value given to a
  648. constant attribute, so don't expand this until it's time to
  649. write the test expression. */
  650. if (attr->is_const)
  651. ATTR_IND_SIMPLIFIED_P (exp) = 1;
  652. if (attr->is_numeric)
  653. {
  654. for (p = XSTR (exp, 1); *p; p++)
  655. if (! ISDIGIT (*p))
  656. fatal ("attribute `%s' takes only numeric values",
  657. XSTR (exp, 0));
  658. }
  659. else
  660. {
  661. for (av = attr->first_value; av; av = av->next)
  662. if (GET_CODE (av->value) == CONST_STRING
  663. && ! strcmp (XSTR (exp, 1), XSTR (av->value, 0)))
  664. break;
  665. if (av == NULL)
  666. fatal ("unknown value `%s' for `%s' attribute",
  667. XSTR (exp, 1), XSTR (exp, 0));
  668. }
  669. }
  670. else
  671. {
  672. if (! strcmp (XSTR (exp, 0), "alternative"))
  673. {
  674. int set = 0;
  675. name_ptr = XSTR (exp, 1);
  676. while ((p = next_comma_elt (&name_ptr)) != NULL)
  677. set |= ((uint64_t) 1) << atoi (p);
  678. return mk_attr_alt (set);
  679. }
  680. else
  681. {
  682. /* Make an IOR tree of the possible values. */
  683. orexp = false_rtx;
  684. name_ptr = XSTR (exp, 1);
  685. while ((p = next_comma_elt (&name_ptr)) != NULL)
  686. {
  687. newexp = attr_eq (XSTR (exp, 0), p);
  688. orexp = insert_right_side (IOR, orexp, newexp, -2, -2);
  689. }
  690. return check_attr_test (orexp, is_const, lineno);
  691. }
  692. }
  693. break;
  694. case ATTR_FLAG:
  695. break;
  696. case CONST_INT:
  697. /* Either TRUE or FALSE. */
  698. if (XWINT (exp, 0))
  699. return true_rtx;
  700. else
  701. return false_rtx;
  702. case IOR:
  703. case AND:
  704. XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
  705. XEXP (exp, 1) = check_attr_test (XEXP (exp, 1), is_const, lineno);
  706. break;
  707. case NOT:
  708. XEXP (exp, 0) = check_attr_test (XEXP (exp, 0), is_const, lineno);
  709. break;
  710. case MATCH_TEST:
  711. exp = attr_rtx (MATCH_TEST, XSTR (exp, 0));
  712. ATTR_IND_SIMPLIFIED_P (exp) = 1;
  713. break;
  714. case MATCH_OPERAND:
  715. if (is_const)
  716. fatal ("RTL operator \"%s\" not valid in constant attribute test",
  717. GET_RTX_NAME (GET_CODE (exp)));
  718. /* These cases can't be simplified. */
  719. ATTR_IND_SIMPLIFIED_P (exp) = 1;
  720. break;
  721. case LE: case LT: case GT: case GE:
  722. case LEU: case LTU: case GTU: case GEU:
  723. case NE: case EQ:
  724. if (GET_CODE (XEXP (exp, 0)) == SYMBOL_REF
  725. && GET_CODE (XEXP (exp, 1)) == SYMBOL_REF)
  726. exp = attr_rtx (GET_CODE (exp),
  727. attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 0), 0)),
  728. attr_rtx (SYMBOL_REF, XSTR (XEXP (exp, 1), 0)));
  729. /* These cases can't be simplified. */
  730. ATTR_IND_SIMPLIFIED_P (exp) = 1;
  731. break;
  732. case SYMBOL_REF:
  733. if (is_const)
  734. {
  735. /* These cases are valid for constant attributes, but can't be
  736. simplified. */
  737. exp = attr_rtx (SYMBOL_REF, XSTR (exp, 0));
  738. ATTR_IND_SIMPLIFIED_P (exp) = 1;
  739. break;
  740. }
  741. default:
  742. fatal ("RTL operator \"%s\" not valid in attribute test",
  743. GET_RTX_NAME (GET_CODE (exp)));
  744. }
  745. return exp;
  746. }
  747. /* Given an expression, ensure that it is validly formed and that all named
  748. attribute values are valid for the given attribute. Issue a fatal error
  749. if not. If no attribute is specified, assume a numeric attribute.
  750. Return a perhaps modified replacement expression for the value. */
  751. static rtx
  752. check_attr_value (rtx exp, struct attr_desc *attr)
  753. {
  754. struct attr_value *av;
  755. const char *p;
  756. int i;
  757. switch (GET_CODE (exp))
  758. {
  759. case CONST_INT:
  760. if (attr && ! attr->is_numeric)
  761. {
  762. error_with_line (attr->lineno,
  763. "CONST_INT not valid for non-numeric attribute %s",
  764. attr->name);
  765. break;
  766. }
  767. if (INTVAL (exp) < 0)
  768. {
  769. error_with_line (attr->lineno,
  770. "negative numeric value specified for attribute %s",
  771. attr->name);
  772. break;
  773. }
  774. break;
  775. case CONST_STRING:
  776. if (! strcmp (XSTR (exp, 0), "*"))
  777. break;
  778. if (attr == 0 || attr->is_numeric)
  779. {
  780. p = XSTR (exp, 0);
  781. for (; *p; p++)
  782. if (! ISDIGIT (*p))
  783. {
  784. error_with_line (attr ? attr->lineno : 0,
  785. "non-numeric value for numeric attribute %s",
  786. attr ? attr->name : "internal");
  787. break;
  788. }
  789. break;
  790. }
  791. for (av = attr->first_value; av; av = av->next)
  792. if (GET_CODE (av->value) == CONST_STRING
  793. && ! strcmp (XSTR (av->value, 0), XSTR (exp, 0)))
  794. break;
  795. if (av == NULL)
  796. error_with_line (attr->lineno,
  797. "unknown value `%s' for `%s' attribute",
  798. XSTR (exp, 0), attr ? attr->name : "internal");
  799. break;
  800. case IF_THEN_ELSE:
  801. XEXP (exp, 0) = check_attr_test (XEXP (exp, 0),
  802. attr ? attr->is_const : 0,
  803. attr ? attr->lineno : 0);
  804. XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
  805. XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
  806. break;
  807. case PLUS:
  808. case MINUS:
  809. case MULT:
  810. case DIV:
  811. case MOD:
  812. if (attr && !attr->is_numeric)
  813. {
  814. error_with_line (attr->lineno,
  815. "invalid operation `%s' for non-numeric"
  816. " attribute value", GET_RTX_NAME (GET_CODE (exp)));
  817. break;
  818. }
  819. /* Fall through. */
  820. case IOR:
  821. case AND:
  822. XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
  823. XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
  824. break;
  825. case FFS:
  826. case CLZ:
  827. case CTZ:
  828. case POPCOUNT:
  829. case PARITY:
  830. case BSWAP:
  831. XEXP (exp, 0) = check_attr_value (XEXP (exp, 0), attr);
  832. break;
  833. case COND:
  834. if (XVECLEN (exp, 0) % 2 != 0)
  835. {
  836. error_with_line (attr->lineno,
  837. "first operand of COND must have even length");
  838. break;
  839. }
  840. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  841. {
  842. XVECEXP (exp, 0, i) = check_attr_test (XVECEXP (exp, 0, i),
  843. attr ? attr->is_const : 0,
  844. attr ? attr->lineno : 0);
  845. XVECEXP (exp, 0, i + 1)
  846. = check_attr_value (XVECEXP (exp, 0, i + 1), attr);
  847. }
  848. XEXP (exp, 1) = check_attr_value (XEXP (exp, 1), attr);
  849. break;
  850. case ATTR:
  851. {
  852. struct attr_desc *attr2 = find_attr (&XSTR (exp, 0), 0);
  853. if (attr2 == NULL)
  854. error_with_line (attr ? attr->lineno : 0,
  855. "unknown attribute `%s' in ATTR",
  856. XSTR (exp, 0));
  857. else if (attr && attr->is_const && ! attr2->is_const)
  858. error_with_line (attr->lineno,
  859. "non-constant attribute `%s' referenced from `%s'",
  860. XSTR (exp, 0), attr->name);
  861. else if (attr
  862. && attr->is_numeric != attr2->is_numeric)
  863. error_with_line (attr->lineno,
  864. "numeric attribute mismatch calling `%s' from `%s'",
  865. XSTR (exp, 0), attr->name);
  866. }
  867. break;
  868. case SYMBOL_REF:
  869. /* A constant SYMBOL_REF is valid as a constant attribute test and
  870. is expanded later by make_canonical into a COND. In a non-constant
  871. attribute test, it is left be. */
  872. return attr_rtx (SYMBOL_REF, XSTR (exp, 0));
  873. default:
  874. error_with_line (attr ? attr->lineno : 0,
  875. "invalid operation `%s' for attribute value",
  876. GET_RTX_NAME (GET_CODE (exp)));
  877. break;
  878. }
  879. return exp;
  880. }
  881. /* Given an SET_ATTR_ALTERNATIVE expression, convert to the canonical SET.
  882. It becomes a COND with each test being (eq_attr "alternative" "n") */
  883. static rtx
  884. convert_set_attr_alternative (rtx exp, struct insn_def *id)
  885. {
  886. int num_alt = id->num_alternatives;
  887. rtx condexp;
  888. int i;
  889. if (XVECLEN (exp, 1) != num_alt)
  890. {
  891. error_with_line (id->lineno,
  892. "bad number of entries in SET_ATTR_ALTERNATIVE, was %d expected %d",
  893. XVECLEN (exp, 1), num_alt);
  894. return NULL_RTX;
  895. }
  896. /* Make a COND with all tests but the last. Select the last value via the
  897. default. */
  898. condexp = rtx_alloc (COND);
  899. XVEC (condexp, 0) = rtvec_alloc ((num_alt - 1) * 2);
  900. for (i = 0; i < num_alt - 1; i++)
  901. {
  902. const char *p;
  903. p = attr_numeral (i);
  904. XVECEXP (condexp, 0, 2 * i) = attr_eq (alternative_name, p);
  905. XVECEXP (condexp, 0, 2 * i + 1) = XVECEXP (exp, 1, i);
  906. }
  907. XEXP (condexp, 1) = XVECEXP (exp, 1, i);
  908. return attr_rtx (SET, attr_rtx (ATTR, XSTR (exp, 0)), condexp);
  909. }
  910. /* Given a SET_ATTR, convert to the appropriate SET. If a comma-separated
  911. list of values is given, convert to SET_ATTR_ALTERNATIVE first. */
  912. static rtx
  913. convert_set_attr (rtx exp, struct insn_def *id)
  914. {
  915. rtx newexp;
  916. const char *name_ptr;
  917. char *p;
  918. int n;
  919. /* See how many alternative specified. */
  920. n = n_comma_elts (XSTR (exp, 1));
  921. if (n == 1)
  922. return attr_rtx (SET,
  923. attr_rtx (ATTR, XSTR (exp, 0)),
  924. attr_rtx (CONST_STRING, XSTR (exp, 1)));
  925. newexp = rtx_alloc (SET_ATTR_ALTERNATIVE);
  926. XSTR (newexp, 0) = XSTR (exp, 0);
  927. XVEC (newexp, 1) = rtvec_alloc (n);
  928. /* Process each comma-separated name. */
  929. name_ptr = XSTR (exp, 1);
  930. n = 0;
  931. while ((p = next_comma_elt (&name_ptr)) != NULL)
  932. XVECEXP (newexp, 1, n++) = attr_rtx (CONST_STRING, p);
  933. return convert_set_attr_alternative (newexp, id);
  934. }
  935. /* Scan all definitions, checking for validity. Also, convert any SET_ATTR
  936. and SET_ATTR_ALTERNATIVE expressions to the corresponding SET
  937. expressions. */
  938. static void
  939. check_defs (void)
  940. {
  941. struct insn_def *id;
  942. struct attr_desc *attr;
  943. int i;
  944. rtx value;
  945. for (id = defs; id; id = id->next)
  946. {
  947. if (XVEC (id->def, id->vec_idx) == NULL)
  948. continue;
  949. read_md_filename = id->filename;
  950. for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
  951. {
  952. value = XVECEXP (id->def, id->vec_idx, i);
  953. switch (GET_CODE (value))
  954. {
  955. case SET:
  956. if (GET_CODE (XEXP (value, 0)) != ATTR)
  957. {
  958. error_with_line (id->lineno, "bad attribute set");
  959. value = NULL_RTX;
  960. }
  961. break;
  962. case SET_ATTR_ALTERNATIVE:
  963. value = convert_set_attr_alternative (value, id);
  964. break;
  965. case SET_ATTR:
  966. value = convert_set_attr (value, id);
  967. break;
  968. default:
  969. error_with_line (id->lineno, "invalid attribute code %s",
  970. GET_RTX_NAME (GET_CODE (value)));
  971. value = NULL_RTX;
  972. }
  973. if (value == NULL_RTX)
  974. continue;
  975. if ((attr = find_attr (&XSTR (XEXP (value, 0), 0), 0)) == NULL)
  976. {
  977. error_with_line (id->lineno, "unknown attribute %s",
  978. XSTR (XEXP (value, 0), 0));
  979. continue;
  980. }
  981. XVECEXP (id->def, id->vec_idx, i) = value;
  982. XEXP (value, 1) = check_attr_value (XEXP (value, 1), attr);
  983. }
  984. }
  985. }
  986. /* Given a valid expression for an attribute value, remove any IF_THEN_ELSE
  987. expressions by converting them into a COND. This removes cases from this
  988. program. Also, replace an attribute value of "*" with the default attribute
  989. value. */
  990. static rtx
  991. make_canonical (struct attr_desc *attr, rtx exp)
  992. {
  993. int i;
  994. rtx newexp;
  995. switch (GET_CODE (exp))
  996. {
  997. case CONST_INT:
  998. exp = make_numeric_value (INTVAL (exp));
  999. break;
  1000. case CONST_STRING:
  1001. if (! strcmp (XSTR (exp, 0), "*"))
  1002. {
  1003. if (attr == 0 || attr->default_val == 0)
  1004. fatal ("(attr_value \"*\") used in invalid context");
  1005. exp = attr->default_val->value;
  1006. }
  1007. else
  1008. XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
  1009. break;
  1010. case SYMBOL_REF:
  1011. if (!attr->is_const || ATTR_IND_SIMPLIFIED_P (exp))
  1012. break;
  1013. /* The SYMBOL_REF is constant for a given run, so mark it as unchanging.
  1014. This makes the COND something that won't be considered an arbitrary
  1015. expression by walk_attr_value. */
  1016. ATTR_IND_SIMPLIFIED_P (exp) = 1;
  1017. exp = check_attr_value (exp, attr);
  1018. break;
  1019. case IF_THEN_ELSE:
  1020. newexp = rtx_alloc (COND);
  1021. XVEC (newexp, 0) = rtvec_alloc (2);
  1022. XVECEXP (newexp, 0, 0) = XEXP (exp, 0);
  1023. XVECEXP (newexp, 0, 1) = XEXP (exp, 1);
  1024. XEXP (newexp, 1) = XEXP (exp, 2);
  1025. exp = newexp;
  1026. /* Fall through to COND case since this is now a COND. */
  1027. case COND:
  1028. {
  1029. int allsame = 1;
  1030. rtx defval;
  1031. /* First, check for degenerate COND. */
  1032. if (XVECLEN (exp, 0) == 0)
  1033. return make_canonical (attr, XEXP (exp, 1));
  1034. defval = XEXP (exp, 1) = make_canonical (attr, XEXP (exp, 1));
  1035. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  1036. {
  1037. XVECEXP (exp, 0, i) = copy_boolean (XVECEXP (exp, 0, i));
  1038. XVECEXP (exp, 0, i + 1)
  1039. = make_canonical (attr, XVECEXP (exp, 0, i + 1));
  1040. if (! rtx_equal_p (XVECEXP (exp, 0, i + 1), defval))
  1041. allsame = 0;
  1042. }
  1043. if (allsame)
  1044. return defval;
  1045. }
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. return exp;
  1051. }
  1052. static rtx
  1053. copy_boolean (rtx exp)
  1054. {
  1055. if (GET_CODE (exp) == AND || GET_CODE (exp) == IOR)
  1056. return attr_rtx (GET_CODE (exp), copy_boolean (XEXP (exp, 0)),
  1057. copy_boolean (XEXP (exp, 1)));
  1058. if (GET_CODE (exp) == MATCH_OPERAND)
  1059. {
  1060. XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
  1061. XSTR (exp, 2) = DEF_ATTR_STRING (XSTR (exp, 2));
  1062. }
  1063. else if (GET_CODE (exp) == EQ_ATTR)
  1064. {
  1065. XSTR (exp, 0) = DEF_ATTR_STRING (XSTR (exp, 0));
  1066. XSTR (exp, 1) = DEF_ATTR_STRING (XSTR (exp, 1));
  1067. }
  1068. return exp;
  1069. }
  1070. /* Given a value and an attribute description, return a `struct attr_value *'
  1071. that represents that value. This is either an existing structure, if the
  1072. value has been previously encountered, or a newly-created structure.
  1073. `insn_code' is the code of an insn whose attribute has the specified
  1074. value (-2 if not processing an insn). We ensure that all insns for
  1075. a given value have the same number of alternatives if the value checks
  1076. alternatives. */
  1077. static struct attr_value *
  1078. get_attr_value (rtx value, struct attr_desc *attr, int insn_code)
  1079. {
  1080. struct attr_value *av;
  1081. uint64_t num_alt = 0;
  1082. value = make_canonical (attr, value);
  1083. if (compares_alternatives_p (value))
  1084. {
  1085. if (insn_code < 0 || insn_alternatives == NULL)
  1086. fatal ("(eq_attr \"alternatives\" ...) used in non-insn context");
  1087. else
  1088. num_alt = insn_alternatives[insn_code];
  1089. }
  1090. for (av = attr->first_value; av; av = av->next)
  1091. if (rtx_equal_p (value, av->value)
  1092. && (num_alt == 0 || av->first_insn == NULL
  1093. || insn_alternatives[av->first_insn->def->insn_code]))
  1094. return av;
  1095. av = oballoc (struct attr_value);
  1096. av->value = value;
  1097. av->next = attr->first_value;
  1098. attr->first_value = av;
  1099. av->first_insn = NULL;
  1100. av->num_insns = 0;
  1101. av->has_asm_insn = 0;
  1102. return av;
  1103. }
  1104. /* After all DEFINE_DELAYs have been read in, create internal attributes
  1105. to generate the required routines.
  1106. First, we compute the number of delay slots for each insn (as a COND of
  1107. each of the test expressions in DEFINE_DELAYs). Then, if more than one
  1108. delay type is specified, we compute a similar function giving the
  1109. DEFINE_DELAY ordinal for each insn.
  1110. Finally, for each [DEFINE_DELAY, slot #] pair, we compute an attribute that
  1111. tells whether a given insn can be in that delay slot.
  1112. Normal attribute filling and optimization expands these to contain the
  1113. information needed to handle delay slots. */
  1114. static void
  1115. expand_delays (void)
  1116. {
  1117. struct delay_desc *delay;
  1118. rtx condexp;
  1119. rtx newexp;
  1120. int i;
  1121. char *p;
  1122. /* First, generate data for `num_delay_slots' function. */
  1123. condexp = rtx_alloc (COND);
  1124. XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
  1125. XEXP (condexp, 1) = make_numeric_value (0);
  1126. for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
  1127. {
  1128. XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
  1129. XVECEXP (condexp, 0, i + 1)
  1130. = make_numeric_value (XVECLEN (delay->def, 1) / 3);
  1131. }
  1132. make_internal_attr (num_delay_slots_str, condexp, ATTR_NONE);
  1133. /* If more than one delay type, do the same for computing the delay type. */
  1134. if (num_delays > 1)
  1135. {
  1136. condexp = rtx_alloc (COND);
  1137. XVEC (condexp, 0) = rtvec_alloc (num_delays * 2);
  1138. XEXP (condexp, 1) = make_numeric_value (0);
  1139. for (i = 0, delay = delays; delay; i += 2, delay = delay->next)
  1140. {
  1141. XVECEXP (condexp, 0, i) = XEXP (delay->def, 0);
  1142. XVECEXP (condexp, 0, i + 1) = make_numeric_value (delay->num);
  1143. }
  1144. make_internal_attr (delay_type_str, condexp, ATTR_SPECIAL);
  1145. }
  1146. /* For each delay possibility and delay slot, compute an eligibility
  1147. attribute for non-annulled insns and for each type of annulled (annul
  1148. if true and annul if false). */
  1149. for (delay = delays; delay; delay = delay->next)
  1150. {
  1151. for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
  1152. {
  1153. condexp = XVECEXP (delay->def, 1, i);
  1154. if (condexp == 0)
  1155. condexp = false_rtx;
  1156. newexp = attr_rtx (IF_THEN_ELSE, condexp,
  1157. make_numeric_value (1), make_numeric_value (0));
  1158. p = attr_printf (sizeof "*delay__" + MAX_DIGITS * 2,
  1159. "*delay_%d_%d", delay->num, i / 3);
  1160. make_internal_attr (p, newexp, ATTR_SPECIAL);
  1161. if (have_annul_true)
  1162. {
  1163. condexp = XVECEXP (delay->def, 1, i + 1);
  1164. if (condexp == 0) condexp = false_rtx;
  1165. newexp = attr_rtx (IF_THEN_ELSE, condexp,
  1166. make_numeric_value (1),
  1167. make_numeric_value (0));
  1168. p = attr_printf (sizeof "*annul_true__" + MAX_DIGITS * 2,
  1169. "*annul_true_%d_%d", delay->num, i / 3);
  1170. make_internal_attr (p, newexp, ATTR_SPECIAL);
  1171. }
  1172. if (have_annul_false)
  1173. {
  1174. condexp = XVECEXP (delay->def, 1, i + 2);
  1175. if (condexp == 0) condexp = false_rtx;
  1176. newexp = attr_rtx (IF_THEN_ELSE, condexp,
  1177. make_numeric_value (1),
  1178. make_numeric_value (0));
  1179. p = attr_printf (sizeof "*annul_false__" + MAX_DIGITS * 2,
  1180. "*annul_false_%d_%d", delay->num, i / 3);
  1181. make_internal_attr (p, newexp, ATTR_SPECIAL);
  1182. }
  1183. }
  1184. }
  1185. }
  1186. /* Once all attributes and insns have been read and checked, we construct for
  1187. each attribute value a list of all the insns that have that value for
  1188. the attribute. */
  1189. static void
  1190. fill_attr (struct attr_desc *attr)
  1191. {
  1192. struct attr_value *av;
  1193. struct insn_ent *ie;
  1194. struct insn_def *id;
  1195. int i;
  1196. rtx value;
  1197. /* Don't fill constant attributes. The value is independent of
  1198. any particular insn. */
  1199. if (attr->is_const)
  1200. return;
  1201. for (id = defs; id; id = id->next)
  1202. {
  1203. /* If no value is specified for this insn for this attribute, use the
  1204. default. */
  1205. value = NULL;
  1206. if (XVEC (id->def, id->vec_idx))
  1207. for (i = 0; i < XVECLEN (id->def, id->vec_idx); i++)
  1208. if (! strcmp_check (XSTR (XEXP (XVECEXP (id->def, id->vec_idx, i), 0), 0),
  1209. attr->name))
  1210. value = XEXP (XVECEXP (id->def, id->vec_idx, i), 1);
  1211. if (value == NULL)
  1212. av = attr->default_val;
  1213. else
  1214. av = get_attr_value (value, attr, id->insn_code);
  1215. ie = oballoc (struct insn_ent);
  1216. ie->def = id;
  1217. insert_insn_ent (av, ie);
  1218. }
  1219. }
  1220. /* Given an expression EXP, see if it is a COND or IF_THEN_ELSE that has a
  1221. test that checks relative positions of insns (uses MATCH_DUP or PC).
  1222. If so, replace it with what is obtained by passing the expression to
  1223. ADDRESS_FN. If not but it is a COND or IF_THEN_ELSE, call this routine
  1224. recursively on each value (including the default value). Otherwise,
  1225. return the value returned by NO_ADDRESS_FN applied to EXP. */
  1226. static rtx
  1227. substitute_address (rtx exp, rtx (*no_address_fn) (rtx),
  1228. rtx (*address_fn) (rtx))
  1229. {
  1230. int i;
  1231. rtx newexp;
  1232. if (GET_CODE (exp) == COND)
  1233. {
  1234. /* See if any tests use addresses. */
  1235. address_used = 0;
  1236. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  1237. walk_attr_value (XVECEXP (exp, 0, i));
  1238. if (address_used)
  1239. return (*address_fn) (exp);
  1240. /* Make a new copy of this COND, replacing each element. */
  1241. newexp = rtx_alloc (COND);
  1242. XVEC (newexp, 0) = rtvec_alloc (XVECLEN (exp, 0));
  1243. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  1244. {
  1245. XVECEXP (newexp, 0, i) = XVECEXP (exp, 0, i);
  1246. XVECEXP (newexp, 0, i + 1)
  1247. = substitute_address (XVECEXP (exp, 0, i + 1),
  1248. no_address_fn, address_fn);
  1249. }
  1250. XEXP (newexp, 1) = substitute_address (XEXP (exp, 1),
  1251. no_address_fn, address_fn);
  1252. return newexp;
  1253. }
  1254. else if (GET_CODE (exp) == IF_THEN_ELSE)
  1255. {
  1256. address_used = 0;
  1257. walk_attr_value (XEXP (exp, 0));
  1258. if (address_used)
  1259. return (*address_fn) (exp);
  1260. return attr_rtx (IF_THEN_ELSE,
  1261. substitute_address (XEXP (exp, 0),
  1262. no_address_fn, address_fn),
  1263. substitute_address (XEXP (exp, 1),
  1264. no_address_fn, address_fn),
  1265. substitute_address (XEXP (exp, 2),
  1266. no_address_fn, address_fn));
  1267. }
  1268. return (*no_address_fn) (exp);
  1269. }
  1270. /* Make new attributes from the `length' attribute. The following are made,
  1271. each corresponding to a function called from `shorten_branches' or
  1272. `get_attr_length':
  1273. *insn_default_length This is the length of the insn to be returned
  1274. by `get_attr_length' before `shorten_branches'
  1275. has been called. In each case where the length
  1276. depends on relative addresses, the largest
  1277. possible is used. This routine is also used
  1278. to compute the initial size of the insn.
  1279. *insn_variable_length_p This returns 1 if the insn's length depends
  1280. on relative addresses, zero otherwise.
  1281. *insn_current_length This is only called when it is known that the
  1282. insn has a variable length and returns the
  1283. current length, based on relative addresses.
  1284. */
  1285. static void
  1286. make_length_attrs (void)
  1287. {
  1288. static const char *new_names[] =
  1289. {
  1290. "*insn_default_length",
  1291. "*insn_min_length",
  1292. "*insn_variable_length_p",
  1293. "*insn_current_length"
  1294. };
  1295. static rtx (*const no_address_fn[]) (rtx)
  1296. = {identity_fn,identity_fn, zero_fn, zero_fn};
  1297. static rtx (*const address_fn[]) (rtx)
  1298. = {max_fn, min_fn, one_fn, identity_fn};
  1299. size_t i;
  1300. struct attr_desc *length_attr, *new_attr;
  1301. struct attr_value *av, *new_av;
  1302. struct insn_ent *ie, *new_ie;
  1303. /* See if length attribute is defined. If so, it must be numeric. Make
  1304. it special so we don't output anything for it. */
  1305. length_attr = find_attr (&length_str, 0);
  1306. if (length_attr == 0)
  1307. return;
  1308. if (! length_attr->is_numeric)
  1309. fatal ("length attribute must be numeric");
  1310. length_attr->is_const = 0;
  1311. length_attr->is_special = 1;
  1312. /* Make each new attribute, in turn. */
  1313. for (i = 0; i < ARRAY_SIZE (new_names); i++)
  1314. {
  1315. make_internal_attr (new_names[i],
  1316. substitute_address (length_attr->default_val->value,
  1317. no_address_fn[i], address_fn[i]),
  1318. ATTR_NONE);
  1319. new_attr = find_attr (&new_names[i], 0);
  1320. for (av = length_attr->first_value; av; av = av->next)
  1321. for (ie = av->first_insn; ie; ie = ie->next)
  1322. {
  1323. new_av = get_attr_value (substitute_address (av->value,
  1324. no_address_fn[i],
  1325. address_fn[i]),
  1326. new_attr, ie->def->insn_code);
  1327. new_ie = oballoc (struct insn_ent);
  1328. new_ie->def = ie->def;
  1329. insert_insn_ent (new_av, new_ie);
  1330. }
  1331. }
  1332. }
  1333. /* Utility functions called from above routine. */
  1334. static rtx
  1335. identity_fn (rtx exp)
  1336. {
  1337. return exp;
  1338. }
  1339. static rtx
  1340. zero_fn (rtx exp ATTRIBUTE_UNUSED)
  1341. {
  1342. return make_numeric_value (0);
  1343. }
  1344. static rtx
  1345. one_fn (rtx exp ATTRIBUTE_UNUSED)
  1346. {
  1347. return make_numeric_value (1);
  1348. }
  1349. static rtx
  1350. max_fn (rtx exp)
  1351. {
  1352. int unknown;
  1353. return make_numeric_value (max_attr_value (exp, &unknown));
  1354. }
  1355. static rtx
  1356. min_fn (rtx exp)
  1357. {
  1358. int unknown;
  1359. return make_numeric_value (min_attr_value (exp, &unknown));
  1360. }
  1361. static void
  1362. write_length_unit_log (FILE *outf)
  1363. {
  1364. struct attr_desc *length_attr = find_attr (&length_str, 0);
  1365. struct attr_value *av;
  1366. struct insn_ent *ie;
  1367. unsigned int length_unit_log, length_or;
  1368. int unknown = 0;
  1369. if (length_attr)
  1370. {
  1371. length_or = or_attr_value (length_attr->default_val->value, &unknown);
  1372. for (av = length_attr->first_value; av; av = av->next)
  1373. for (ie = av->first_insn; ie; ie = ie->next)
  1374. length_or |= or_attr_value (av->value, &unknown);
  1375. }
  1376. if (length_attr == NULL || unknown)
  1377. length_unit_log = 0;
  1378. else
  1379. {
  1380. length_or = ~length_or;
  1381. for (length_unit_log = 0; length_or & 1; length_or >>= 1)
  1382. length_unit_log++;
  1383. }
  1384. fprintf (outf, "EXPORTED_CONST int length_unit_log = %u;\n", length_unit_log);
  1385. }
  1386. /* Compute approximate cost of the expression. Used to decide whether
  1387. expression is cheap enough for inline. */
  1388. static int
  1389. attr_rtx_cost (rtx x)
  1390. {
  1391. int cost = 1;
  1392. enum rtx_code code;
  1393. if (!x)
  1394. return 0;
  1395. code = GET_CODE (x);
  1396. switch (code)
  1397. {
  1398. case MATCH_OPERAND:
  1399. if (XSTR (x, 1)[0])
  1400. return 10;
  1401. else
  1402. return 1;
  1403. case EQ_ATTR_ALT:
  1404. return 1;
  1405. case EQ_ATTR:
  1406. /* Alternatives don't result into function call. */
  1407. if (!strcmp_check (XSTR (x, 0), alternative_name))
  1408. return 1;
  1409. else
  1410. return 5;
  1411. default:
  1412. {
  1413. int i, j;
  1414. const char *fmt = GET_RTX_FORMAT (code);
  1415. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1416. {
  1417. switch (fmt[i])
  1418. {
  1419. case 'V':
  1420. case 'E':
  1421. for (j = 0; j < XVECLEN (x, i); j++)
  1422. cost += attr_rtx_cost (XVECEXP (x, i, j));
  1423. break;
  1424. case 'e':
  1425. cost += attr_rtx_cost (XEXP (x, i));
  1426. break;
  1427. }
  1428. }
  1429. }
  1430. break;
  1431. }
  1432. return cost;
  1433. }
  1434. /* Take a COND expression and see if any of the conditions in it can be
  1435. simplified. If any are known true or known false for the particular insn
  1436. code, the COND can be further simplified.
  1437. Also call ourselves on any COND operations that are values of this COND.
  1438. We do not modify EXP; rather, we make and return a new rtx. */
  1439. static rtx
  1440. simplify_cond (rtx exp, int insn_code, int insn_index)
  1441. {
  1442. int i, j;
  1443. /* We store the desired contents here,
  1444. then build a new expression if they don't match EXP. */
  1445. rtx defval = XEXP (exp, 1);
  1446. rtx new_defval = XEXP (exp, 1);
  1447. int len = XVECLEN (exp, 0);
  1448. rtx *tests = XNEWVEC (rtx, len);
  1449. int allsame = 1;
  1450. rtx ret;
  1451. /* This lets us free all storage allocated below, if appropriate. */
  1452. obstack_finish (rtl_obstack);
  1453. memcpy (tests, XVEC (exp, 0)->elem, len * sizeof (rtx));
  1454. /* See if default value needs simplification. */
  1455. if (GET_CODE (defval) == COND)
  1456. new_defval = simplify_cond (defval, insn_code, insn_index);
  1457. /* Simplify the subexpressions, and see what tests we can get rid of. */
  1458. for (i = 0; i < len; i += 2)
  1459. {
  1460. rtx newtest, newval;
  1461. /* Simplify this test. */
  1462. newtest = simplify_test_exp_in_temp (tests[i], insn_code, insn_index);
  1463. tests[i] = newtest;
  1464. newval = tests[i + 1];
  1465. /* See if this value may need simplification. */
  1466. if (GET_CODE (newval) == COND)
  1467. newval = simplify_cond (newval, insn_code, insn_index);
  1468. /* Look for ways to delete or combine this test. */
  1469. if (newtest == true_rtx)
  1470. {
  1471. /* If test is true, make this value the default
  1472. and discard this + any following tests. */
  1473. len = i;
  1474. defval = tests[i + 1];
  1475. new_defval = newval;
  1476. }
  1477. else if (newtest == false_rtx)
  1478. {
  1479. /* If test is false, discard it and its value. */
  1480. for (j = i; j < len - 2; j++)
  1481. tests[j] = tests[j + 2];
  1482. i -= 2;
  1483. len -= 2;
  1484. }
  1485. else if (i > 0 && attr_equal_p (newval, tests[i - 1]))
  1486. {
  1487. /* If this value and the value for the prev test are the same,
  1488. merge the tests. */
  1489. tests[i - 2]
  1490. = insert_right_side (IOR, tests[i - 2], newtest,
  1491. insn_code, insn_index);
  1492. /* Delete this test/value. */
  1493. for (j = i; j < len - 2; j++)
  1494. tests[j] = tests[j + 2];
  1495. len -= 2;
  1496. i -= 2;
  1497. }
  1498. else
  1499. tests[i + 1] = newval;
  1500. }
  1501. /* If the last test in a COND has the same value
  1502. as the default value, that test isn't needed. */
  1503. while (len > 0 && attr_equal_p (tests[len - 1], new_defval))
  1504. len -= 2;
  1505. /* See if we changed anything. */
  1506. if (len != XVECLEN (exp, 0) || new_defval != XEXP (exp, 1))
  1507. allsame = 0;
  1508. else
  1509. for (i = 0; i < len; i++)
  1510. if (! attr_equal_p (tests[i], XVECEXP (exp, 0, i)))
  1511. {
  1512. allsame = 0;
  1513. break;
  1514. }
  1515. if (len == 0)
  1516. {
  1517. if (GET_CODE (defval) == COND)
  1518. ret = simplify_cond (defval, insn_code, insn_index);
  1519. else
  1520. ret = defval;
  1521. }
  1522. else if (allsame)
  1523. ret = exp;
  1524. else
  1525. {
  1526. rtx newexp = rtx_alloc (COND);
  1527. XVEC (newexp, 0) = rtvec_alloc (len);
  1528. memcpy (XVEC (newexp, 0)->elem, tests, len * sizeof (rtx));
  1529. XEXP (newexp, 1) = new_defval;
  1530. ret = newexp;
  1531. }
  1532. free (tests);
  1533. return ret;
  1534. }
  1535. /* Remove an insn entry from an attribute value. */
  1536. static void
  1537. remove_insn_ent (struct attr_value *av, struct insn_ent *ie)
  1538. {
  1539. struct insn_ent *previe;
  1540. if (av->first_insn == ie)
  1541. av->first_insn = ie->next;
  1542. else
  1543. {
  1544. for (previe = av->first_insn; previe->next != ie; previe = previe->next)
  1545. ;
  1546. previe->next = ie->next;
  1547. }
  1548. av->num_insns--;
  1549. if (ie->def->insn_code == -1)
  1550. av->has_asm_insn = 0;
  1551. num_insn_ents--;
  1552. }
  1553. /* Insert an insn entry in an attribute value list. */
  1554. static void
  1555. insert_insn_ent (struct attr_value *av, struct insn_ent *ie)
  1556. {
  1557. ie->next = av->first_insn;
  1558. av->first_insn = ie;
  1559. av->num_insns++;
  1560. if (ie->def->insn_code == -1)
  1561. av->has_asm_insn = 1;
  1562. num_insn_ents++;
  1563. }
  1564. /* This is a utility routine to take an expression that is a tree of either
  1565. AND or IOR expressions and insert a new term. The new term will be
  1566. inserted at the right side of the first node whose code does not match
  1567. the root. A new node will be created with the root's code. Its left
  1568. side will be the old right side and its right side will be the new
  1569. term.
  1570. If the `term' is itself a tree, all its leaves will be inserted. */
  1571. static rtx
  1572. insert_right_side (enum rtx_code code, rtx exp, rtx term, int insn_code, int insn_index)
  1573. {
  1574. rtx newexp;
  1575. /* Avoid consing in some special cases. */
  1576. if (code == AND && term == true_rtx)
  1577. return exp;
  1578. if (code == AND && term == false_rtx)
  1579. return false_rtx;
  1580. if (code == AND && exp == true_rtx)
  1581. return term;
  1582. if (code == AND && exp == false_rtx)
  1583. return false_rtx;
  1584. if (code == IOR && term == true_rtx)
  1585. return true_rtx;
  1586. if (code == IOR && term == false_rtx)
  1587. return exp;
  1588. if (code == IOR && exp == true_rtx)
  1589. return true_rtx;
  1590. if (code == IOR && exp == false_rtx)
  1591. return term;
  1592. if (attr_equal_p (exp, term))
  1593. return exp;
  1594. if (GET_CODE (term) == code)
  1595. {
  1596. exp = insert_right_side (code, exp, XEXP (term, 0),
  1597. insn_code, insn_index);
  1598. exp = insert_right_side (code, exp, XEXP (term, 1),
  1599. insn_code, insn_index);
  1600. return exp;
  1601. }
  1602. if (GET_CODE (exp) == code)
  1603. {
  1604. rtx new_rtx = insert_right_side (code, XEXP (exp, 1),
  1605. term, insn_code, insn_index);
  1606. if (new_rtx != XEXP (exp, 1))
  1607. /* Make a copy of this expression and call recursively. */
  1608. newexp = attr_rtx (code, XEXP (exp, 0), new_rtx);
  1609. else
  1610. newexp = exp;
  1611. }
  1612. else
  1613. {
  1614. /* Insert the new term. */
  1615. newexp = attr_rtx (code, exp, term);
  1616. }
  1617. return simplify_test_exp_in_temp (newexp, insn_code, insn_index);
  1618. }
  1619. /* If we have an expression which AND's a bunch of
  1620. (not (eq_attrq "alternative" "n"))
  1621. terms, we may have covered all or all but one of the possible alternatives.
  1622. If so, we can optimize. Similarly for IOR's of EQ_ATTR.
  1623. This routine is passed an expression and either AND or IOR. It returns a
  1624. bitmask indicating which alternatives are mentioned within EXP. */
  1625. static uint64_t
  1626. compute_alternative_mask (rtx exp, enum rtx_code code)
  1627. {
  1628. const char *string;
  1629. if (GET_CODE (exp) == code)
  1630. return compute_alternative_mask (XEXP (exp, 0), code)
  1631. | compute_alternative_mask (XEXP (exp, 1), code);
  1632. else if (code == AND && GET_CODE (exp) == NOT
  1633. && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
  1634. && XSTR (XEXP (exp, 0), 0) == alternative_name)
  1635. string = XSTR (XEXP (exp, 0), 1);
  1636. else if (code == IOR && GET_CODE (exp) == EQ_ATTR
  1637. && XSTR (exp, 0) == alternative_name)
  1638. string = XSTR (exp, 1);
  1639. else if (GET_CODE (exp) == EQ_ATTR_ALT)
  1640. {
  1641. if (code == AND && XINT (exp, 1))
  1642. return XINT (exp, 0);
  1643. if (code == IOR && !XINT (exp, 1))
  1644. return XINT (exp, 0);
  1645. return 0;
  1646. }
  1647. else
  1648. return 0;
  1649. if (string[1] == 0)
  1650. return ((uint64_t) 1) << (string[0] - '0');
  1651. return ((uint64_t) 1) << atoi (string);
  1652. }
  1653. /* Given I, a single-bit mask, return RTX to compare the `alternative'
  1654. attribute with the value represented by that bit. */
  1655. static rtx
  1656. make_alternative_compare (uint64_t mask)
  1657. {
  1658. return mk_attr_alt (mask);
  1659. }
  1660. /* If we are processing an (eq_attr "attr" "value") test, we find the value
  1661. of "attr" for this insn code. From that value, we can compute a test
  1662. showing when the EQ_ATTR will be true. This routine performs that
  1663. computation. If a test condition involves an address, we leave the EQ_ATTR
  1664. intact because addresses are only valid for the `length' attribute.
  1665. EXP is the EQ_ATTR expression and ATTR is the attribute to which
  1666. it refers. VALUE is the value of that attribute for the insn
  1667. corresponding to INSN_CODE and INSN_INDEX. */
  1668. static rtx
  1669. evaluate_eq_attr (rtx exp, struct attr_desc *attr, rtx value,
  1670. int insn_code, int insn_index)
  1671. {
  1672. rtx orexp, andexp;
  1673. rtx right;
  1674. rtx newexp;
  1675. int i;
  1676. while (GET_CODE (value) == ATTR)
  1677. {
  1678. struct attr_value *av = NULL;
  1679. attr = find_attr (&XSTR (value, 0), 0);
  1680. if (insn_code_values)
  1681. {
  1682. struct attr_value_list *iv;
  1683. for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
  1684. if (iv->attr == attr)
  1685. {
  1686. av = iv->av;
  1687. break;
  1688. }
  1689. }
  1690. else
  1691. {
  1692. struct insn_ent *ie;
  1693. for (av = attr->first_value; av; av = av->next)
  1694. for (ie = av->first_insn; ie; ie = ie->next)
  1695. if (ie->def->insn_code == insn_code)
  1696. goto got_av;
  1697. }
  1698. if (av)
  1699. {
  1700. got_av:
  1701. value = av->value;
  1702. }
  1703. }
  1704. switch (GET_CODE (value))
  1705. {
  1706. case CONST_STRING:
  1707. if (! strcmp_check (XSTR (value, 0), XSTR (exp, 1)))
  1708. newexp = true_rtx;
  1709. else
  1710. newexp = false_rtx;
  1711. break;
  1712. case SYMBOL_REF:
  1713. {
  1714. const char *prefix;
  1715. char *string, *p;
  1716. gcc_assert (GET_CODE (exp) == EQ_ATTR);
  1717. prefix = attr->enum_name ? attr->enum_name : attr->name;
  1718. string = ACONCAT ((prefix, "_", XSTR (exp, 1), NULL));
  1719. for (p = string; *p; p++)
  1720. *p = TOUPPER (*p);
  1721. newexp = attr_rtx (EQ, value,
  1722. attr_rtx (SYMBOL_REF,
  1723. DEF_ATTR_STRING (string)));
  1724. break;
  1725. }
  1726. case COND:
  1727. /* We construct an IOR of all the cases for which the
  1728. requested attribute value is present. Since we start with
  1729. FALSE, if it is not present, FALSE will be returned.
  1730. Each case is the AND of the NOT's of the previous conditions with the
  1731. current condition; in the default case the current condition is TRUE.
  1732. For each possible COND value, call ourselves recursively.
  1733. The extra TRUE and FALSE expressions will be eliminated by another
  1734. call to the simplification routine. */
  1735. orexp = false_rtx;
  1736. andexp = true_rtx;
  1737. for (i = 0; i < XVECLEN (value, 0); i += 2)
  1738. {
  1739. rtx this_cond = simplify_test_exp_in_temp (XVECEXP (value, 0, i),
  1740. insn_code, insn_index);
  1741. right = insert_right_side (AND, andexp, this_cond,
  1742. insn_code, insn_index);
  1743. right = insert_right_side (AND, right,
  1744. evaluate_eq_attr (exp, attr,
  1745. XVECEXP (value, 0,
  1746. i + 1),
  1747. insn_code, insn_index),
  1748. insn_code, insn_index);
  1749. orexp = insert_right_side (IOR, orexp, right,
  1750. insn_code, insn_index);
  1751. /* Add this condition into the AND expression. */
  1752. newexp = attr_rtx (NOT, this_cond);
  1753. andexp = insert_right_side (AND, andexp, newexp,
  1754. insn_code, insn_index);
  1755. }
  1756. /* Handle the default case. */
  1757. right = insert_right_side (AND, andexp,
  1758. evaluate_eq_attr (exp, attr, XEXP (value, 1),
  1759. insn_code, insn_index),
  1760. insn_code, insn_index);
  1761. newexp = insert_right_side (IOR, orexp, right, insn_code, insn_index);
  1762. break;
  1763. default:
  1764. gcc_unreachable ();
  1765. }
  1766. /* If uses an address, must return original expression. But set the
  1767. ATTR_IND_SIMPLIFIED_P bit so we don't try to simplify it again. */
  1768. address_used = 0;
  1769. walk_attr_value (newexp);
  1770. if (address_used)
  1771. {
  1772. if (! ATTR_IND_SIMPLIFIED_P (exp))
  1773. return copy_rtx_unchanging (exp);
  1774. return exp;
  1775. }
  1776. else
  1777. return newexp;
  1778. }
  1779. /* This routine is called when an AND of a term with a tree of AND's is
  1780. encountered. If the term or its complement is present in the tree, it
  1781. can be replaced with TRUE or FALSE, respectively.
  1782. Note that (eq_attr "att" "v1") and (eq_attr "att" "v2") cannot both
  1783. be true and hence are complementary.
  1784. There is one special case: If we see
  1785. (and (not (eq_attr "att" "v1"))
  1786. (eq_attr "att" "v2"))
  1787. this can be replaced by (eq_attr "att" "v2"). To do this we need to
  1788. replace the term, not anything in the AND tree. So we pass a pointer to
  1789. the term. */
  1790. static rtx
  1791. simplify_and_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
  1792. {
  1793. rtx left, right;
  1794. rtx newexp;
  1795. rtx temp;
  1796. int left_eliminates_term, right_eliminates_term;
  1797. if (GET_CODE (exp) == AND)
  1798. {
  1799. left = simplify_and_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
  1800. right = simplify_and_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
  1801. if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1802. {
  1803. newexp = attr_rtx (AND, left, right);
  1804. exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
  1805. }
  1806. }
  1807. else if (GET_CODE (exp) == IOR)
  1808. {
  1809. /* For the IOR case, we do the same as above, except that we can
  1810. only eliminate `term' if both sides of the IOR would do so. */
  1811. temp = *pterm;
  1812. left = simplify_and_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
  1813. left_eliminates_term = (temp == true_rtx);
  1814. temp = *pterm;
  1815. right = simplify_and_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
  1816. right_eliminates_term = (temp == true_rtx);
  1817. if (left_eliminates_term && right_eliminates_term)
  1818. *pterm = true_rtx;
  1819. if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1820. {
  1821. newexp = attr_rtx (IOR, left, right);
  1822. exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
  1823. }
  1824. }
  1825. /* Check for simplifications. Do some extra checking here since this
  1826. routine is called so many times. */
  1827. if (exp == *pterm)
  1828. return true_rtx;
  1829. else if (GET_CODE (exp) == NOT && XEXP (exp, 0) == *pterm)
  1830. return false_rtx;
  1831. else if (GET_CODE (*pterm) == NOT && exp == XEXP (*pterm, 0))
  1832. return false_rtx;
  1833. else if (GET_CODE (exp) == EQ_ATTR_ALT && GET_CODE (*pterm) == EQ_ATTR_ALT)
  1834. {
  1835. if (attr_alt_subset_p (*pterm, exp))
  1836. return true_rtx;
  1837. if (attr_alt_subset_of_compl_p (*pterm, exp))
  1838. return false_rtx;
  1839. if (attr_alt_subset_p (exp, *pterm))
  1840. *pterm = true_rtx;
  1841. return exp;
  1842. }
  1843. else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == EQ_ATTR)
  1844. {
  1845. if (XSTR (exp, 0) != XSTR (*pterm, 0))
  1846. return exp;
  1847. if (! strcmp_check (XSTR (exp, 1), XSTR (*pterm, 1)))
  1848. return true_rtx;
  1849. else
  1850. return false_rtx;
  1851. }
  1852. else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
  1853. && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
  1854. {
  1855. if (XSTR (*pterm, 0) != XSTR (XEXP (exp, 0), 0))
  1856. return exp;
  1857. if (! strcmp_check (XSTR (*pterm, 1), XSTR (XEXP (exp, 0), 1)))
  1858. return false_rtx;
  1859. else
  1860. return true_rtx;
  1861. }
  1862. else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
  1863. && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR)
  1864. {
  1865. if (XSTR (exp, 0) != XSTR (XEXP (*pterm, 0), 0))
  1866. return exp;
  1867. if (! strcmp_check (XSTR (exp, 1), XSTR (XEXP (*pterm, 0), 1)))
  1868. return false_rtx;
  1869. else
  1870. *pterm = true_rtx;
  1871. }
  1872. else if (GET_CODE (exp) == NOT && GET_CODE (*pterm) == NOT)
  1873. {
  1874. if (attr_equal_p (XEXP (exp, 0), XEXP (*pterm, 0)))
  1875. return true_rtx;
  1876. }
  1877. else if (GET_CODE (exp) == NOT)
  1878. {
  1879. if (attr_equal_p (XEXP (exp, 0), *pterm))
  1880. return false_rtx;
  1881. }
  1882. else if (GET_CODE (*pterm) == NOT)
  1883. {
  1884. if (attr_equal_p (XEXP (*pterm, 0), exp))
  1885. return false_rtx;
  1886. }
  1887. else if (attr_equal_p (exp, *pterm))
  1888. return true_rtx;
  1889. return exp;
  1890. }
  1891. /* Similar to `simplify_and_tree', but for IOR trees. */
  1892. static rtx
  1893. simplify_or_tree (rtx exp, rtx *pterm, int insn_code, int insn_index)
  1894. {
  1895. rtx left, right;
  1896. rtx newexp;
  1897. rtx temp;
  1898. int left_eliminates_term, right_eliminates_term;
  1899. if (GET_CODE (exp) == IOR)
  1900. {
  1901. left = simplify_or_tree (XEXP (exp, 0), pterm, insn_code, insn_index);
  1902. right = simplify_or_tree (XEXP (exp, 1), pterm, insn_code, insn_index);
  1903. if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1904. {
  1905. newexp = attr_rtx (GET_CODE (exp), left, right);
  1906. exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
  1907. }
  1908. }
  1909. else if (GET_CODE (exp) == AND)
  1910. {
  1911. /* For the AND case, we do the same as above, except that we can
  1912. only eliminate `term' if both sides of the AND would do so. */
  1913. temp = *pterm;
  1914. left = simplify_or_tree (XEXP (exp, 0), &temp, insn_code, insn_index);
  1915. left_eliminates_term = (temp == false_rtx);
  1916. temp = *pterm;
  1917. right = simplify_or_tree (XEXP (exp, 1), &temp, insn_code, insn_index);
  1918. right_eliminates_term = (temp == false_rtx);
  1919. if (left_eliminates_term && right_eliminates_term)
  1920. *pterm = false_rtx;
  1921. if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  1922. {
  1923. newexp = attr_rtx (GET_CODE (exp), left, right);
  1924. exp = simplify_test_exp_in_temp (newexp, insn_code, insn_index);
  1925. }
  1926. }
  1927. if (attr_equal_p (exp, *pterm))
  1928. return false_rtx;
  1929. else if (GET_CODE (exp) == NOT && attr_equal_p (XEXP (exp, 0), *pterm))
  1930. return true_rtx;
  1931. else if (GET_CODE (*pterm) == NOT && attr_equal_p (XEXP (*pterm, 0), exp))
  1932. return true_rtx;
  1933. else if (GET_CODE (*pterm) == EQ_ATTR && GET_CODE (exp) == NOT
  1934. && GET_CODE (XEXP (exp, 0)) == EQ_ATTR
  1935. && XSTR (*pterm, 0) == XSTR (XEXP (exp, 0), 0))
  1936. *pterm = false_rtx;
  1937. else if (GET_CODE (exp) == EQ_ATTR && GET_CODE (*pterm) == NOT
  1938. && GET_CODE (XEXP (*pterm, 0)) == EQ_ATTR
  1939. && XSTR (exp, 0) == XSTR (XEXP (*pterm, 0), 0))
  1940. return false_rtx;
  1941. return exp;
  1942. }
  1943. /* Simplify test expression and use temporary obstack in order to avoid
  1944. memory bloat. Use ATTR_IND_SIMPLIFIED to avoid unnecessary simplifications
  1945. and avoid unnecessary copying if possible. */
  1946. static rtx
  1947. simplify_test_exp_in_temp (rtx exp, int insn_code, int insn_index)
  1948. {
  1949. rtx x;
  1950. struct obstack *old;
  1951. if (ATTR_IND_SIMPLIFIED_P (exp))
  1952. return exp;
  1953. old = rtl_obstack;
  1954. rtl_obstack = temp_obstack;
  1955. x = simplify_test_exp (exp, insn_code, insn_index);
  1956. rtl_obstack = old;
  1957. if (x == exp || rtl_obstack == temp_obstack)
  1958. return x;
  1959. return attr_copy_rtx (x);
  1960. }
  1961. /* Returns true if S1 is a subset of S2. */
  1962. static bool
  1963. attr_alt_subset_p (rtx s1, rtx s2)
  1964. {
  1965. switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
  1966. {
  1967. case (0 << 1) | 0:
  1968. return !(XINT (s1, 0) &~ XINT (s2, 0));
  1969. case (0 << 1) | 1:
  1970. return !(XINT (s1, 0) & XINT (s2, 0));
  1971. case (1 << 1) | 0:
  1972. return false;
  1973. case (1 << 1) | 1:
  1974. return !(XINT (s2, 0) &~ XINT (s1, 0));
  1975. default:
  1976. gcc_unreachable ();
  1977. }
  1978. }
  1979. /* Returns true if S1 is a subset of complement of S2. */
  1980. static bool
  1981. attr_alt_subset_of_compl_p (rtx s1, rtx s2)
  1982. {
  1983. switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
  1984. {
  1985. case (0 << 1) | 0:
  1986. return !(XINT (s1, 0) & XINT (s2, 0));
  1987. case (0 << 1) | 1:
  1988. return !(XINT (s1, 0) & ~XINT (s2, 0));
  1989. case (1 << 1) | 0:
  1990. return !(XINT (s2, 0) &~ XINT (s1, 0));
  1991. case (1 << 1) | 1:
  1992. return false;
  1993. default:
  1994. gcc_unreachable ();
  1995. }
  1996. }
  1997. /* Return EQ_ATTR_ALT expression representing intersection of S1 and S2. */
  1998. static rtx
  1999. attr_alt_intersection (rtx s1, rtx s2)
  2000. {
  2001. rtx result = rtx_alloc (EQ_ATTR_ALT);
  2002. switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
  2003. {
  2004. case (0 << 1) | 0:
  2005. XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
  2006. break;
  2007. case (0 << 1) | 1:
  2008. XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
  2009. break;
  2010. case (1 << 1) | 0:
  2011. XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
  2012. break;
  2013. case (1 << 1) | 1:
  2014. XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
  2015. break;
  2016. default:
  2017. gcc_unreachable ();
  2018. }
  2019. XINT (result, 1) = XINT (s1, 1) & XINT (s2, 1);
  2020. return result;
  2021. }
  2022. /* Return EQ_ATTR_ALT expression representing union of S1 and S2. */
  2023. static rtx
  2024. attr_alt_union (rtx s1, rtx s2)
  2025. {
  2026. rtx result = rtx_alloc (EQ_ATTR_ALT);
  2027. switch ((XINT (s1, 1) << 1) | XINT (s2, 1))
  2028. {
  2029. case (0 << 1) | 0:
  2030. XINT (result, 0) = XINT (s1, 0) | XINT (s2, 0);
  2031. break;
  2032. case (0 << 1) | 1:
  2033. XINT (result, 0) = XINT (s2, 0) & ~XINT (s1, 0);
  2034. break;
  2035. case (1 << 1) | 0:
  2036. XINT (result, 0) = XINT (s1, 0) & ~XINT (s2, 0);
  2037. break;
  2038. case (1 << 1) | 1:
  2039. XINT (result, 0) = XINT (s1, 0) & XINT (s2, 0);
  2040. break;
  2041. default:
  2042. gcc_unreachable ();
  2043. }
  2044. XINT (result, 1) = XINT (s1, 1) | XINT (s2, 1);
  2045. return result;
  2046. }
  2047. /* Return EQ_ATTR_ALT expression representing complement of S. */
  2048. static rtx
  2049. attr_alt_complement (rtx s)
  2050. {
  2051. rtx result = rtx_alloc (EQ_ATTR_ALT);
  2052. XINT (result, 0) = XINT (s, 0);
  2053. XINT (result, 1) = 1 - XINT (s, 1);
  2054. return result;
  2055. }
  2056. /* Return EQ_ATTR_ALT expression representing set containing elements set
  2057. in E. */
  2058. static rtx
  2059. mk_attr_alt (uint64_t e)
  2060. {
  2061. rtx result = rtx_alloc (EQ_ATTR_ALT);
  2062. XINT (result, 0) = e;
  2063. XINT (result, 1) = 0;
  2064. return result;
  2065. }
  2066. /* Given an expression, see if it can be simplified for a particular insn
  2067. code based on the values of other attributes being tested. This can
  2068. eliminate nested get_attr_... calls.
  2069. Note that if an endless recursion is specified in the patterns, the
  2070. optimization will loop. However, it will do so in precisely the cases where
  2071. an infinite recursion loop could occur during compilation. It's better that
  2072. it occurs here! */
  2073. static rtx
  2074. simplify_test_exp (rtx exp, int insn_code, int insn_index)
  2075. {
  2076. rtx left, right;
  2077. struct attr_desc *attr;
  2078. struct attr_value *av;
  2079. struct insn_ent *ie;
  2080. struct attr_value_list *iv;
  2081. uint64_t i;
  2082. rtx newexp = exp;
  2083. bool left_alt, right_alt;
  2084. /* Don't re-simplify something we already simplified. */
  2085. if (ATTR_IND_SIMPLIFIED_P (exp) || ATTR_CURR_SIMPLIFIED_P (exp))
  2086. return exp;
  2087. switch (GET_CODE (exp))
  2088. {
  2089. case AND:
  2090. left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
  2091. if (left == false_rtx)
  2092. return false_rtx;
  2093. right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
  2094. if (right == false_rtx)
  2095. return false_rtx;
  2096. if (GET_CODE (left) == EQ_ATTR_ALT
  2097. && GET_CODE (right) == EQ_ATTR_ALT)
  2098. {
  2099. exp = attr_alt_intersection (left, right);
  2100. return simplify_test_exp (exp, insn_code, insn_index);
  2101. }
  2102. /* If either side is an IOR and we have (eq_attr "alternative" ..")
  2103. present on both sides, apply the distributive law since this will
  2104. yield simplifications. */
  2105. if ((GET_CODE (left) == IOR || GET_CODE (right) == IOR)
  2106. && compute_alternative_mask (left, IOR)
  2107. && compute_alternative_mask (right, IOR))
  2108. {
  2109. if (GET_CODE (left) == IOR)
  2110. {
  2111. rtx tem = left;
  2112. left = right;
  2113. right = tem;
  2114. }
  2115. newexp = attr_rtx (IOR,
  2116. attr_rtx (AND, left, XEXP (right, 0)),
  2117. attr_rtx (AND, left, XEXP (right, 1)));
  2118. return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2119. }
  2120. /* Try with the term on both sides. */
  2121. right = simplify_and_tree (right, &left, insn_code, insn_index);
  2122. if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
  2123. left = simplify_and_tree (left, &right, insn_code, insn_index);
  2124. if (left == false_rtx || right == false_rtx)
  2125. return false_rtx;
  2126. else if (left == true_rtx)
  2127. {
  2128. return right;
  2129. }
  2130. else if (right == true_rtx)
  2131. {
  2132. return left;
  2133. }
  2134. /* See if all or all but one of the insn's alternatives are specified
  2135. in this tree. Optimize if so. */
  2136. if (GET_CODE (left) == NOT)
  2137. left_alt = (GET_CODE (XEXP (left, 0)) == EQ_ATTR
  2138. && XSTR (XEXP (left, 0), 0) == alternative_name);
  2139. else
  2140. left_alt = (GET_CODE (left) == EQ_ATTR_ALT
  2141. && XINT (left, 1));
  2142. if (GET_CODE (right) == NOT)
  2143. right_alt = (GET_CODE (XEXP (right, 0)) == EQ_ATTR
  2144. && XSTR (XEXP (right, 0), 0) == alternative_name);
  2145. else
  2146. right_alt = (GET_CODE (right) == EQ_ATTR_ALT
  2147. && XINT (right, 1));
  2148. if (insn_code >= 0
  2149. && (GET_CODE (left) == AND
  2150. || left_alt
  2151. || GET_CODE (right) == AND
  2152. || right_alt))
  2153. {
  2154. i = compute_alternative_mask (exp, AND);
  2155. if (i & ~insn_alternatives[insn_code])
  2156. fatal ("invalid alternative specified for pattern number %d",
  2157. insn_index);
  2158. /* If all alternatives are excluded, this is false. */
  2159. i ^= insn_alternatives[insn_code];
  2160. if (i == 0)
  2161. return false_rtx;
  2162. else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
  2163. {
  2164. /* If just one excluded, AND a comparison with that one to the
  2165. front of the tree. The others will be eliminated by
  2166. optimization. We do not want to do this if the insn has one
  2167. alternative and we have tested none of them! */
  2168. left = make_alternative_compare (i);
  2169. right = simplify_and_tree (exp, &left, insn_code, insn_index);
  2170. newexp = attr_rtx (AND, left, right);
  2171. return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2172. }
  2173. }
  2174. if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  2175. {
  2176. newexp = attr_rtx (AND, left, right);
  2177. return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2178. }
  2179. break;
  2180. case IOR:
  2181. left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
  2182. if (left == true_rtx)
  2183. return true_rtx;
  2184. right = SIMPLIFY_TEST_EXP (XEXP (exp, 1), insn_code, insn_index);
  2185. if (right == true_rtx)
  2186. return true_rtx;
  2187. if (GET_CODE (left) == EQ_ATTR_ALT
  2188. && GET_CODE (right) == EQ_ATTR_ALT)
  2189. {
  2190. exp = attr_alt_union (left, right);
  2191. return simplify_test_exp (exp, insn_code, insn_index);
  2192. }
  2193. right = simplify_or_tree (right, &left, insn_code, insn_index);
  2194. if (left == XEXP (exp, 0) && right == XEXP (exp, 1))
  2195. left = simplify_or_tree (left, &right, insn_code, insn_index);
  2196. if (right == true_rtx || left == true_rtx)
  2197. return true_rtx;
  2198. else if (left == false_rtx)
  2199. {
  2200. return right;
  2201. }
  2202. else if (right == false_rtx)
  2203. {
  2204. return left;
  2205. }
  2206. /* Test for simple cases where the distributive law is useful. I.e.,
  2207. convert (ior (and (x) (y))
  2208. (and (x) (z)))
  2209. to (and (x)
  2210. (ior (y) (z)))
  2211. */
  2212. else if (GET_CODE (left) == AND && GET_CODE (right) == AND
  2213. && attr_equal_p (XEXP (left, 0), XEXP (right, 0)))
  2214. {
  2215. newexp = attr_rtx (IOR, XEXP (left, 1), XEXP (right, 1));
  2216. left = XEXP (left, 0);
  2217. right = newexp;
  2218. newexp = attr_rtx (AND, left, right);
  2219. return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2220. }
  2221. /* Similarly,
  2222. convert (ior (and (y) (x))
  2223. (and (z) (x)))
  2224. to (and (ior (y) (z))
  2225. (x))
  2226. Note that we want the common term to stay at the end.
  2227. */
  2228. else if (GET_CODE (left) == AND && GET_CODE (right) == AND
  2229. && attr_equal_p (XEXP (left, 1), XEXP (right, 1)))
  2230. {
  2231. newexp = attr_rtx (IOR, XEXP (left, 0), XEXP (right, 0));
  2232. left = newexp;
  2233. right = XEXP (right, 1);
  2234. newexp = attr_rtx (AND, left, right);
  2235. return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2236. }
  2237. /* See if all or all but one of the insn's alternatives are specified
  2238. in this tree. Optimize if so. */
  2239. else if (insn_code >= 0
  2240. && (GET_CODE (left) == IOR
  2241. || (GET_CODE (left) == EQ_ATTR_ALT
  2242. && !XINT (left, 1))
  2243. || (GET_CODE (left) == EQ_ATTR
  2244. && XSTR (left, 0) == alternative_name)
  2245. || GET_CODE (right) == IOR
  2246. || (GET_CODE (right) == EQ_ATTR_ALT
  2247. && !XINT (right, 1))
  2248. || (GET_CODE (right) == EQ_ATTR
  2249. && XSTR (right, 0) == alternative_name)))
  2250. {
  2251. i = compute_alternative_mask (exp, IOR);
  2252. if (i & ~insn_alternatives[insn_code])
  2253. fatal ("invalid alternative specified for pattern number %d",
  2254. insn_index);
  2255. /* If all alternatives are included, this is true. */
  2256. i ^= insn_alternatives[insn_code];
  2257. if (i == 0)
  2258. return true_rtx;
  2259. else if ((i & (i - 1)) == 0 && insn_alternatives[insn_code] > 1)
  2260. {
  2261. /* If just one excluded, IOR a comparison with that one to the
  2262. front of the tree. The others will be eliminated by
  2263. optimization. We do not want to do this if the insn has one
  2264. alternative and we have tested none of them! */
  2265. left = make_alternative_compare (i);
  2266. right = simplify_and_tree (exp, &left, insn_code, insn_index);
  2267. newexp = attr_rtx (IOR, attr_rtx (NOT, left), right);
  2268. return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2269. }
  2270. }
  2271. if (left != XEXP (exp, 0) || right != XEXP (exp, 1))
  2272. {
  2273. newexp = attr_rtx (IOR, left, right);
  2274. return SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2275. }
  2276. break;
  2277. case NOT:
  2278. if (GET_CODE (XEXP (exp, 0)) == NOT)
  2279. {
  2280. left = SIMPLIFY_TEST_EXP (XEXP (XEXP (exp, 0), 0),
  2281. insn_code, insn_index);
  2282. return left;
  2283. }
  2284. left = SIMPLIFY_TEST_EXP (XEXP (exp, 0), insn_code, insn_index);
  2285. if (GET_CODE (left) == NOT)
  2286. return XEXP (left, 0);
  2287. if (left == false_rtx)
  2288. return true_rtx;
  2289. if (left == true_rtx)
  2290. return false_rtx;
  2291. if (GET_CODE (left) == EQ_ATTR_ALT)
  2292. {
  2293. exp = attr_alt_complement (left);
  2294. return simplify_test_exp (exp, insn_code, insn_index);
  2295. }
  2296. /* Try to apply De`Morgan's laws. */
  2297. if (GET_CODE (left) == IOR)
  2298. {
  2299. newexp = attr_rtx (AND,
  2300. attr_rtx (NOT, XEXP (left, 0)),
  2301. attr_rtx (NOT, XEXP (left, 1)));
  2302. newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2303. }
  2304. else if (GET_CODE (left) == AND)
  2305. {
  2306. newexp = attr_rtx (IOR,
  2307. attr_rtx (NOT, XEXP (left, 0)),
  2308. attr_rtx (NOT, XEXP (left, 1)));
  2309. newexp = SIMPLIFY_TEST_EXP (newexp, insn_code, insn_index);
  2310. }
  2311. else if (left != XEXP (exp, 0))
  2312. {
  2313. newexp = attr_rtx (NOT, left);
  2314. }
  2315. break;
  2316. case EQ_ATTR_ALT:
  2317. if (!XINT (exp, 0))
  2318. return XINT (exp, 1) ? true_rtx : false_rtx;
  2319. break;
  2320. case EQ_ATTR:
  2321. if (XSTR (exp, 0) == alternative_name)
  2322. {
  2323. newexp = mk_attr_alt (((uint64_t) 1) << atoi (XSTR (exp, 1)));
  2324. break;
  2325. }
  2326. /* Look at the value for this insn code in the specified attribute.
  2327. We normally can replace this comparison with the condition that
  2328. would give this insn the values being tested for. */
  2329. if (insn_code >= 0
  2330. && (attr = find_attr (&XSTR (exp, 0), 0)) != NULL)
  2331. {
  2332. rtx x;
  2333. av = NULL;
  2334. if (insn_code_values)
  2335. {
  2336. for (iv = insn_code_values[insn_code]; iv; iv = iv->next)
  2337. if (iv->attr == attr)
  2338. {
  2339. av = iv->av;
  2340. break;
  2341. }
  2342. }
  2343. else
  2344. {
  2345. for (av = attr->first_value; av; av = av->next)
  2346. for (ie = av->first_insn; ie; ie = ie->next)
  2347. if (ie->def->insn_code == insn_code)
  2348. goto got_av;
  2349. }
  2350. if (av)
  2351. {
  2352. got_av:
  2353. x = evaluate_eq_attr (exp, attr, av->value,
  2354. insn_code, insn_index);
  2355. x = SIMPLIFY_TEST_EXP (x, insn_code, insn_index);
  2356. if (attr_rtx_cost (x) < 7)
  2357. return x;
  2358. }
  2359. }
  2360. break;
  2361. default:
  2362. break;
  2363. }
  2364. /* We have already simplified this expression. Simplifying it again
  2365. won't buy anything unless we weren't given a valid insn code
  2366. to process (i.e., we are canonicalizing something.). */
  2367. if (insn_code != -2
  2368. && ! ATTR_IND_SIMPLIFIED_P (newexp))
  2369. return copy_rtx_unchanging (newexp);
  2370. return newexp;
  2371. }
  2372. /* Return 1 if any EQ_ATTR subexpression of P refers to ATTR,
  2373. otherwise return 0. */
  2374. static int
  2375. tests_attr_p (rtx p, struct attr_desc *attr)
  2376. {
  2377. const char *fmt;
  2378. int i, ie, j, je;
  2379. if (GET_CODE (p) == EQ_ATTR)
  2380. {
  2381. if (XSTR (p, 0) != attr->name)
  2382. return 0;
  2383. return 1;
  2384. }
  2385. fmt = GET_RTX_FORMAT (GET_CODE (p));
  2386. ie = GET_RTX_LENGTH (GET_CODE (p));
  2387. for (i = 0; i < ie; i++)
  2388. {
  2389. switch (*fmt++)
  2390. {
  2391. case 'e':
  2392. if (tests_attr_p (XEXP (p, i), attr))
  2393. return 1;
  2394. break;
  2395. case 'E':
  2396. je = XVECLEN (p, i);
  2397. for (j = 0; j < je; ++j)
  2398. if (tests_attr_p (XVECEXP (p, i, j), attr))
  2399. return 1;
  2400. break;
  2401. }
  2402. }
  2403. return 0;
  2404. }
  2405. /* Calculate a topological sorting of all attributes so that
  2406. all attributes only depend on attributes in front of it.
  2407. Place the result in *RET (which is a pointer to an array of
  2408. attr_desc pointers), and return the size of that array. */
  2409. static int
  2410. get_attr_order (struct attr_desc ***ret)
  2411. {
  2412. int i, j;
  2413. int num = 0;
  2414. struct attr_desc *attr;
  2415. struct attr_desc **all, **sorted;
  2416. char *handled;
  2417. for (i = 0; i < MAX_ATTRS_INDEX; i++)
  2418. for (attr = attrs[i]; attr; attr = attr->next)
  2419. num++;
  2420. all = XNEWVEC (struct attr_desc *, num);
  2421. sorted = XNEWVEC (struct attr_desc *, num);
  2422. handled = XCNEWVEC (char, num);
  2423. num = 0;
  2424. for (i = 0; i < MAX_ATTRS_INDEX; i++)
  2425. for (attr = attrs[i]; attr; attr = attr->next)
  2426. all[num++] = attr;
  2427. j = 0;
  2428. for (i = 0; i < num; i++)
  2429. if (all[i]->is_const)
  2430. handled[i] = 1, sorted[j++] = all[i];
  2431. /* We have only few attributes hence we can live with the inner
  2432. loop being O(n^2), unlike the normal fast variants of topological
  2433. sorting. */
  2434. while (j < num)
  2435. {
  2436. for (i = 0; i < num; i++)
  2437. if (!handled[i])
  2438. {
  2439. /* Let's see if I depends on anything interesting. */
  2440. int k;
  2441. for (k = 0; k < num; k++)
  2442. if (!handled[k])
  2443. {
  2444. struct attr_value *av;
  2445. for (av = all[i]->first_value; av; av = av->next)
  2446. if (av->num_insns != 0)
  2447. if (tests_attr_p (av->value, all[k]))
  2448. break;
  2449. if (av)
  2450. /* Something in I depends on K. */
  2451. break;
  2452. }
  2453. if (k == num)
  2454. {
  2455. /* Nothing in I depended on anything intersting, so
  2456. it's done. */
  2457. handled[i] = 1;
  2458. sorted[j++] = all[i];
  2459. }
  2460. }
  2461. }
  2462. if (DEBUG)
  2463. for (j = 0; j < num; j++)
  2464. {
  2465. struct attr_desc *attr2;
  2466. struct attr_value *av;
  2467. attr = sorted[j];
  2468. fprintf (stderr, "%s depends on: ", attr->name);
  2469. for (i = 0; i < MAX_ATTRS_INDEX; ++i)
  2470. for (attr2 = attrs[i]; attr2; attr2 = attr2->next)
  2471. if (!attr2->is_const)
  2472. for (av = attr->first_value; av; av = av->next)
  2473. if (av->num_insns != 0)
  2474. if (tests_attr_p (av->value, attr2))
  2475. {
  2476. fprintf (stderr, "%s, ", attr2->name);
  2477. break;
  2478. }
  2479. fprintf (stderr, "\n");
  2480. }
  2481. free (all);
  2482. *ret = sorted;
  2483. return num;
  2484. }
  2485. /* Optimize the attribute lists by seeing if we can determine conditional
  2486. values from the known values of other attributes. This will save subroutine
  2487. calls during the compilation. */
  2488. static void
  2489. optimize_attrs (void)
  2490. {
  2491. struct attr_desc *attr;
  2492. struct attr_value *av;
  2493. struct insn_ent *ie;
  2494. rtx newexp;
  2495. int i;
  2496. struct attr_value_list *ivbuf;
  2497. struct attr_value_list *iv;
  2498. struct attr_desc **topsort;
  2499. int topnum;
  2500. /* For each insn code, make a list of all the insn_ent's for it,
  2501. for all values for all attributes. */
  2502. if (num_insn_ents == 0)
  2503. return;
  2504. /* Make 2 extra elements, for "code" values -2 and -1. */
  2505. insn_code_values = XCNEWVEC (struct attr_value_list *, insn_code_number + 2);
  2506. /* Offset the table address so we can index by -2 or -1. */
  2507. insn_code_values += 2;
  2508. iv = ivbuf = XNEWVEC (struct attr_value_list, num_insn_ents);
  2509. /* Create the chain of insn*attr values such that we see dependend
  2510. attributes after their dependencies. As we use a stack via the
  2511. next pointers start from the end of the topological order. */
  2512. topnum = get_attr_order (&topsort);
  2513. for (i = topnum - 1; i >= 0; i--)
  2514. for (av = topsort[i]->first_value; av; av = av->next)
  2515. for (ie = av->first_insn; ie; ie = ie->next)
  2516. {
  2517. iv->attr = topsort[i];
  2518. iv->av = av;
  2519. iv->ie = ie;
  2520. iv->next = insn_code_values[ie->def->insn_code];
  2521. insn_code_values[ie->def->insn_code] = iv;
  2522. iv++;
  2523. }
  2524. free (topsort);
  2525. /* Sanity check on num_insn_ents. */
  2526. gcc_assert (iv == ivbuf + num_insn_ents);
  2527. /* Process one insn code at a time. */
  2528. for (i = -2; i < insn_code_number; i++)
  2529. {
  2530. /* Clear the ATTR_CURR_SIMPLIFIED_P flag everywhere relevant.
  2531. We use it to mean "already simplified for this insn". */
  2532. for (iv = insn_code_values[i]; iv; iv = iv->next)
  2533. clear_struct_flag (iv->av->value);
  2534. for (iv = insn_code_values[i]; iv; iv = iv->next)
  2535. {
  2536. struct obstack *old = rtl_obstack;
  2537. attr = iv->attr;
  2538. av = iv->av;
  2539. ie = iv->ie;
  2540. if (GET_CODE (av->value) != COND)
  2541. continue;
  2542. rtl_obstack = temp_obstack;
  2543. newexp = av->value;
  2544. while (GET_CODE (newexp) == COND)
  2545. {
  2546. rtx newexp2 = simplify_cond (newexp, ie->def->insn_code,
  2547. ie->def->insn_index);
  2548. if (newexp2 == newexp)
  2549. break;
  2550. newexp = newexp2;
  2551. }
  2552. rtl_obstack = old;
  2553. /* If we created a new value for this instruction, and it's
  2554. cheaper than the old value, and overall cheap, use that
  2555. one as specific value for the current instruction.
  2556. The last test is to avoid exploding the get_attr_ function
  2557. sizes for no much gain. */
  2558. if (newexp != av->value
  2559. && attr_rtx_cost (newexp) < attr_rtx_cost (av->value)
  2560. && attr_rtx_cost (newexp) < 26
  2561. )
  2562. {
  2563. newexp = attr_copy_rtx (newexp);
  2564. remove_insn_ent (av, ie);
  2565. av = get_attr_value (newexp, attr, ie->def->insn_code);
  2566. iv->av = av;
  2567. insert_insn_ent (av, ie);
  2568. }
  2569. }
  2570. }
  2571. free (ivbuf);
  2572. free (insn_code_values - 2);
  2573. insn_code_values = NULL;
  2574. }
  2575. /* Clear the ATTR_CURR_SIMPLIFIED_P flag in EXP and its subexpressions. */
  2576. static void
  2577. clear_struct_flag (rtx x)
  2578. {
  2579. int i;
  2580. int j;
  2581. enum rtx_code code;
  2582. const char *fmt;
  2583. ATTR_CURR_SIMPLIFIED_P (x) = 0;
  2584. if (ATTR_IND_SIMPLIFIED_P (x))
  2585. return;
  2586. code = GET_CODE (x);
  2587. switch (code)
  2588. {
  2589. case REG:
  2590. CASE_CONST_ANY:
  2591. case MATCH_TEST:
  2592. case SYMBOL_REF:
  2593. case CODE_LABEL:
  2594. case PC:
  2595. case CC0:
  2596. case EQ_ATTR:
  2597. case ATTR_FLAG:
  2598. return;
  2599. default:
  2600. break;
  2601. }
  2602. /* Compare the elements. If any pair of corresponding elements
  2603. fail to match, return 0 for the whole things. */
  2604. fmt = GET_RTX_FORMAT (code);
  2605. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2606. {
  2607. switch (fmt[i])
  2608. {
  2609. case 'V':
  2610. case 'E':
  2611. for (j = 0; j < XVECLEN (x, i); j++)
  2612. clear_struct_flag (XVECEXP (x, i, j));
  2613. break;
  2614. case 'e':
  2615. clear_struct_flag (XEXP (x, i));
  2616. break;
  2617. }
  2618. }
  2619. }
  2620. /* Add attribute value NAME to the beginning of ATTR's list. */
  2621. static void
  2622. add_attr_value (struct attr_desc *attr, const char *name)
  2623. {
  2624. struct attr_value *av;
  2625. av = oballoc (struct attr_value);
  2626. av->value = attr_rtx (CONST_STRING, name);
  2627. av->next = attr->first_value;
  2628. attr->first_value = av;
  2629. av->first_insn = NULL;
  2630. av->num_insns = 0;
  2631. av->has_asm_insn = 0;
  2632. }
  2633. /* Create table entries for DEFINE_ATTR or DEFINE_ENUM_ATTR. */
  2634. static void
  2635. gen_attr (rtx exp, int lineno)
  2636. {
  2637. struct enum_type *et;
  2638. struct enum_value *ev;
  2639. struct attr_desc *attr;
  2640. const char *name_ptr;
  2641. char *p;
  2642. /* Make a new attribute structure. Check for duplicate by looking at
  2643. attr->default_val, since it is initialized by this routine. */
  2644. attr = find_attr (&XSTR (exp, 0), 1);
  2645. if (attr->default_val)
  2646. {
  2647. error_with_line (lineno, "duplicate definition for attribute %s",
  2648. attr->name);
  2649. message_with_line (attr->lineno, "previous definition");
  2650. return;
  2651. }
  2652. attr->lineno = lineno;
  2653. if (GET_CODE (exp) == DEFINE_ENUM_ATTR)
  2654. {
  2655. attr->enum_name = XSTR (exp, 1);
  2656. et = lookup_enum_type (XSTR (exp, 1));
  2657. if (!et || !et->md_p)
  2658. error_with_line (lineno, "No define_enum called `%s' defined",
  2659. attr->name);
  2660. if (et)
  2661. for (ev = et->values; ev; ev = ev->next)
  2662. add_attr_value (attr, ev->name);
  2663. }
  2664. else if (*XSTR (exp, 1) == '\0')
  2665. attr->is_numeric = 1;
  2666. else
  2667. {
  2668. name_ptr = XSTR (exp, 1);
  2669. while ((p = next_comma_elt (&name_ptr)) != NULL)
  2670. add_attr_value (attr, p);
  2671. }
  2672. if (GET_CODE (XEXP (exp, 2)) == CONST)
  2673. {
  2674. attr->is_const = 1;
  2675. if (attr->is_numeric)
  2676. error_with_line (lineno,
  2677. "constant attributes may not take numeric values");
  2678. /* Get rid of the CONST node. It is allowed only at top-level. */
  2679. XEXP (exp, 2) = XEXP (XEXP (exp, 2), 0);
  2680. }
  2681. if (! strcmp_check (attr->name, length_str) && ! attr->is_numeric)
  2682. error_with_line (lineno, "`length' attribute must take numeric values");
  2683. /* Set up the default value. */
  2684. XEXP (exp, 2) = check_attr_value (XEXP (exp, 2), attr);
  2685. attr->default_val = get_attr_value (XEXP (exp, 2), attr, -2);
  2686. }
  2687. /* Given a pattern for DEFINE_PEEPHOLE or DEFINE_INSN, return the number of
  2688. alternatives in the constraints. Assume all MATCH_OPERANDs have the same
  2689. number of alternatives as this should be checked elsewhere. */
  2690. static int
  2691. count_alternatives (rtx exp)
  2692. {
  2693. int i, j, n;
  2694. const char *fmt;
  2695. if (GET_CODE (exp) == MATCH_OPERAND)
  2696. return n_comma_elts (XSTR (exp, 2));
  2697. for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
  2698. i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
  2699. switch (*fmt++)
  2700. {
  2701. case 'e':
  2702. case 'u':
  2703. n = count_alternatives (XEXP (exp, i));
  2704. if (n)
  2705. return n;
  2706. break;
  2707. case 'E':
  2708. case 'V':
  2709. if (XVEC (exp, i) != NULL)
  2710. for (j = 0; j < XVECLEN (exp, i); j++)
  2711. {
  2712. n = count_alternatives (XVECEXP (exp, i, j));
  2713. if (n)
  2714. return n;
  2715. }
  2716. }
  2717. return 0;
  2718. }
  2719. /* Returns nonzero if the given expression contains an EQ_ATTR with the
  2720. `alternative' attribute. */
  2721. static int
  2722. compares_alternatives_p (rtx exp)
  2723. {
  2724. int i, j;
  2725. const char *fmt;
  2726. if (GET_CODE (exp) == EQ_ATTR && XSTR (exp, 0) == alternative_name)
  2727. return 1;
  2728. for (i = 0, fmt = GET_RTX_FORMAT (GET_CODE (exp));
  2729. i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
  2730. switch (*fmt++)
  2731. {
  2732. case 'e':
  2733. case 'u':
  2734. if (compares_alternatives_p (XEXP (exp, i)))
  2735. return 1;
  2736. break;
  2737. case 'E':
  2738. for (j = 0; j < XVECLEN (exp, i); j++)
  2739. if (compares_alternatives_p (XVECEXP (exp, i, j)))
  2740. return 1;
  2741. break;
  2742. }
  2743. return 0;
  2744. }
  2745. /* Process DEFINE_PEEPHOLE, DEFINE_INSN, and DEFINE_ASM_ATTRIBUTES. */
  2746. static void
  2747. gen_insn (rtx exp, int lineno)
  2748. {
  2749. struct insn_def *id;
  2750. id = oballoc (struct insn_def);
  2751. id->next = defs;
  2752. defs = id;
  2753. id->def = exp;
  2754. id->filename = read_md_filename;
  2755. id->lineno = lineno;
  2756. switch (GET_CODE (exp))
  2757. {
  2758. case DEFINE_INSN:
  2759. id->insn_code = insn_code_number;
  2760. id->insn_index = insn_index_number;
  2761. id->num_alternatives = count_alternatives (exp);
  2762. if (id->num_alternatives == 0)
  2763. id->num_alternatives = 1;
  2764. id->vec_idx = 4;
  2765. break;
  2766. case DEFINE_PEEPHOLE:
  2767. id->insn_code = insn_code_number;
  2768. id->insn_index = insn_index_number;
  2769. id->num_alternatives = count_alternatives (exp);
  2770. if (id->num_alternatives == 0)
  2771. id->num_alternatives = 1;
  2772. id->vec_idx = 3;
  2773. break;
  2774. case DEFINE_ASM_ATTRIBUTES:
  2775. id->insn_code = -1;
  2776. id->insn_index = -1;
  2777. id->num_alternatives = 1;
  2778. id->vec_idx = 0;
  2779. got_define_asm_attributes = 1;
  2780. break;
  2781. default:
  2782. gcc_unreachable ();
  2783. }
  2784. }
  2785. /* Process a DEFINE_DELAY. Validate the vector length, check if annul
  2786. true or annul false is specified, and make a `struct delay_desc'. */
  2787. static void
  2788. gen_delay (rtx def, int lineno)
  2789. {
  2790. struct delay_desc *delay;
  2791. int i;
  2792. if (XVECLEN (def, 1) % 3 != 0)
  2793. {
  2794. error_with_line (lineno,
  2795. "number of elements in DEFINE_DELAY must"
  2796. " be multiple of three");
  2797. return;
  2798. }
  2799. for (i = 0; i < XVECLEN (def, 1); i += 3)
  2800. {
  2801. if (XVECEXP (def, 1, i + 1))
  2802. have_annul_true = 1;
  2803. if (XVECEXP (def, 1, i + 2))
  2804. have_annul_false = 1;
  2805. }
  2806. delay = oballoc (struct delay_desc);
  2807. delay->def = def;
  2808. delay->num = ++num_delays;
  2809. delay->next = delays;
  2810. delay->lineno = lineno;
  2811. delays = delay;
  2812. }
  2813. /* Names of attributes that could be possibly cached. */
  2814. static const char *cached_attrs[32];
  2815. /* Number of such attributes. */
  2816. static int cached_attr_count;
  2817. /* Bitmasks of possibly cached attributes. */
  2818. static unsigned int attrs_seen_once, attrs_seen_more_than_once;
  2819. static unsigned int attrs_to_cache;
  2820. static unsigned int attrs_cached_inside, attrs_cached_after;
  2821. /* Finds non-const attributes that could be possibly cached.
  2822. When create is TRUE, fills in cached_attrs array.
  2823. Computes ATTRS_SEEN_ONCE and ATTRS_SEEN_MORE_THAN_ONCE
  2824. bitmasks. */
  2825. static void
  2826. find_attrs_to_cache (rtx exp, bool create)
  2827. {
  2828. int i;
  2829. const char *name;
  2830. struct attr_desc *attr;
  2831. if (exp == NULL)
  2832. return;
  2833. switch (GET_CODE (exp))
  2834. {
  2835. case NOT:
  2836. if (GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
  2837. find_attrs_to_cache (XEXP (exp, 0), create);
  2838. return;
  2839. case EQ_ATTR:
  2840. name = XSTR (exp, 0);
  2841. if (name == alternative_name)
  2842. return;
  2843. for (i = 0; i < cached_attr_count; i++)
  2844. if (name == cached_attrs[i])
  2845. {
  2846. if ((attrs_seen_once & (1U << i)) != 0)
  2847. attrs_seen_more_than_once |= (1U << i);
  2848. else
  2849. attrs_seen_once |= (1U << i);
  2850. return;
  2851. }
  2852. if (!create)
  2853. return;
  2854. attr = find_attr (&name, 0);
  2855. gcc_assert (attr);
  2856. if (attr->is_const)
  2857. return;
  2858. if (cached_attr_count == 32)
  2859. return;
  2860. cached_attrs[cached_attr_count] = XSTR (exp, 0);
  2861. attrs_seen_once |= (1U << cached_attr_count);
  2862. cached_attr_count++;
  2863. return;
  2864. case AND:
  2865. case IOR:
  2866. find_attrs_to_cache (XEXP (exp, 0), create);
  2867. find_attrs_to_cache (XEXP (exp, 1), create);
  2868. return;
  2869. case COND:
  2870. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  2871. find_attrs_to_cache (XVECEXP (exp, 0, i), create);
  2872. return;
  2873. default:
  2874. return;
  2875. }
  2876. }
  2877. /* Given a piece of RTX, print a C expression to test its truth value to OUTF.
  2878. We use AND and IOR both for logical and bit-wise operations, so
  2879. interpret them as logical unless they are inside a comparison expression. */
  2880. /* Interpret AND/IOR as bit-wise operations instead of logical. */
  2881. #define FLG_BITWISE 1
  2882. /* Set if cached attribute will be known initialized in else block after
  2883. this condition. This is true for LHS of toplevel && and || and
  2884. even for RHS of ||, but not for RHS of &&. */
  2885. #define FLG_AFTER 2
  2886. /* Set if cached attribute will be known initialized in then block after
  2887. this condition. This is true for LHS of toplevel && and || and
  2888. even for RHS of &&, but not for RHS of ||. */
  2889. #define FLG_INSIDE 4
  2890. /* Cleared when an operand of &&. */
  2891. #define FLG_OUTSIDE_AND 8
  2892. static unsigned int
  2893. write_test_expr (FILE *outf, rtx exp, unsigned int attrs_cached, int flags)
  2894. {
  2895. int comparison_operator = 0;
  2896. RTX_CODE code;
  2897. struct attr_desc *attr;
  2898. /* In order not to worry about operator precedence, surround our part of
  2899. the expression with parentheses. */
  2900. fprintf (outf, "(");
  2901. code = GET_CODE (exp);
  2902. switch (code)
  2903. {
  2904. /* Binary operators. */
  2905. case GEU: case GTU:
  2906. case LEU: case LTU:
  2907. fprintf (outf, "(unsigned) ");
  2908. /* Fall through. */
  2909. case EQ: case NE:
  2910. case GE: case GT:
  2911. case LE: case LT:
  2912. comparison_operator = FLG_BITWISE;
  2913. case PLUS: case MINUS: case MULT: case DIV: case MOD:
  2914. case AND: case IOR: case XOR:
  2915. case ASHIFT: case LSHIFTRT: case ASHIFTRT:
  2916. if ((code != AND && code != IOR) || (flags & FLG_BITWISE))
  2917. {
  2918. flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
  2919. write_test_expr (outf, XEXP (exp, 0), attrs_cached,
  2920. flags | comparison_operator);
  2921. }
  2922. else
  2923. {
  2924. if (code == AND)
  2925. flags &= ~FLG_OUTSIDE_AND;
  2926. if (GET_CODE (XEXP (exp, 0)) == code
  2927. || GET_CODE (XEXP (exp, 0)) == EQ_ATTR
  2928. || (GET_CODE (XEXP (exp, 0)) == NOT
  2929. && GET_CODE (XEXP (XEXP (exp, 0), 0)) == EQ_ATTR))
  2930. attrs_cached
  2931. = write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
  2932. else
  2933. write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
  2934. }
  2935. switch (code)
  2936. {
  2937. case EQ:
  2938. fprintf (outf, " == ");
  2939. break;
  2940. case NE:
  2941. fprintf (outf, " != ");
  2942. break;
  2943. case GE:
  2944. fprintf (outf, " >= ");
  2945. break;
  2946. case GT:
  2947. fprintf (outf, " > ");
  2948. break;
  2949. case GEU:
  2950. fprintf (outf, " >= (unsigned) ");
  2951. break;
  2952. case GTU:
  2953. fprintf (outf, " > (unsigned) ");
  2954. break;
  2955. case LE:
  2956. fprintf (outf, " <= ");
  2957. break;
  2958. case LT:
  2959. fprintf (outf, " < ");
  2960. break;
  2961. case LEU:
  2962. fprintf (outf, " <= (unsigned) ");
  2963. break;
  2964. case LTU:
  2965. fprintf (outf, " < (unsigned) ");
  2966. break;
  2967. case PLUS:
  2968. fprintf (outf, " + ");
  2969. break;
  2970. case MINUS:
  2971. fprintf (outf, " - ");
  2972. break;
  2973. case MULT:
  2974. fprintf (outf, " * ");
  2975. break;
  2976. case DIV:
  2977. fprintf (outf, " / ");
  2978. break;
  2979. case MOD:
  2980. fprintf (outf, " %% ");
  2981. break;
  2982. case AND:
  2983. if (flags & FLG_BITWISE)
  2984. fprintf (outf, " & ");
  2985. else
  2986. fprintf (outf, " && ");
  2987. break;
  2988. case IOR:
  2989. if (flags & FLG_BITWISE)
  2990. fprintf (outf, " | ");
  2991. else
  2992. fprintf (outf, " || ");
  2993. break;
  2994. case XOR:
  2995. fprintf (outf, " ^ ");
  2996. break;
  2997. case ASHIFT:
  2998. fprintf (outf, " << ");
  2999. break;
  3000. case LSHIFTRT:
  3001. case ASHIFTRT:
  3002. fprintf (outf, " >> ");
  3003. break;
  3004. default:
  3005. gcc_unreachable ();
  3006. }
  3007. if (code == AND)
  3008. {
  3009. /* For if (something && (cached_x = get_attr_x (insn)) == X)
  3010. cached_x is only known to be initialized in then block. */
  3011. flags &= ~FLG_AFTER;
  3012. }
  3013. else if (code == IOR)
  3014. {
  3015. if (flags & FLG_OUTSIDE_AND)
  3016. /* For if (something || (cached_x = get_attr_x (insn)) == X)
  3017. cached_x is only known to be initialized in else block
  3018. and else if conditions. */
  3019. flags &= ~FLG_INSIDE;
  3020. else
  3021. /* For if ((something || (cached_x = get_attr_x (insn)) == X)
  3022. && something_else)
  3023. cached_x is not know to be initialized anywhere. */
  3024. flags &= ~(FLG_AFTER | FLG_INSIDE);
  3025. }
  3026. if ((code == AND || code == IOR)
  3027. && (GET_CODE (XEXP (exp, 1)) == code
  3028. || GET_CODE (XEXP (exp, 1)) == EQ_ATTR
  3029. || (GET_CODE (XEXP (exp, 1)) == NOT
  3030. && GET_CODE (XEXP (XEXP (exp, 1), 0)) == EQ_ATTR)))
  3031. attrs_cached
  3032. = write_test_expr (outf, XEXP (exp, 1), attrs_cached, flags);
  3033. else
  3034. write_test_expr (outf, XEXP (exp, 1), attrs_cached,
  3035. flags | comparison_operator);
  3036. break;
  3037. case NOT:
  3038. /* Special-case (not (eq_attrq "alternative" "x")) */
  3039. if (! (flags & FLG_BITWISE) && GET_CODE (XEXP (exp, 0)) == EQ_ATTR)
  3040. {
  3041. if (XSTR (XEXP (exp, 0), 0) == alternative_name)
  3042. {
  3043. fprintf (outf, "which_alternative != %s",
  3044. XSTR (XEXP (exp, 0), 1));
  3045. break;
  3046. }
  3047. fprintf (outf, "! ");
  3048. attrs_cached =
  3049. write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
  3050. break;
  3051. }
  3052. /* Otherwise, fall through to normal unary operator. */
  3053. /* Unary operators. */
  3054. case ABS: case NEG:
  3055. switch (code)
  3056. {
  3057. case NOT:
  3058. if (flags & FLG_BITWISE)
  3059. fprintf (outf, "~ ");
  3060. else
  3061. fprintf (outf, "! ");
  3062. break;
  3063. case ABS:
  3064. fprintf (outf, "abs ");
  3065. break;
  3066. case NEG:
  3067. fprintf (outf, "-");
  3068. break;
  3069. default:
  3070. gcc_unreachable ();
  3071. }
  3072. flags &= ~(FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND);
  3073. write_test_expr (outf, XEXP (exp, 0), attrs_cached, flags);
  3074. break;
  3075. case EQ_ATTR_ALT:
  3076. {
  3077. int set = XINT (exp, 0), bit = 0;
  3078. if (flags & FLG_BITWISE)
  3079. fatal ("EQ_ATTR_ALT not valid inside comparison");
  3080. if (!set)
  3081. fatal ("Empty EQ_ATTR_ALT should be optimized out");
  3082. if (!(set & (set - 1)))
  3083. {
  3084. if (!(set & 0xffff))
  3085. {
  3086. bit += 16;
  3087. set >>= 16;
  3088. }
  3089. if (!(set & 0xff))
  3090. {
  3091. bit += 8;
  3092. set >>= 8;
  3093. }
  3094. if (!(set & 0xf))
  3095. {
  3096. bit += 4;
  3097. set >>= 4;
  3098. }
  3099. if (!(set & 0x3))
  3100. {
  3101. bit += 2;
  3102. set >>= 2;
  3103. }
  3104. if (!(set & 1))
  3105. bit++;
  3106. fprintf (outf, "which_alternative %s= %d",
  3107. XINT (exp, 1) ? "!" : "=", bit);
  3108. }
  3109. else
  3110. {
  3111. fprintf (outf, "%s((1 << which_alternative) & %#x)",
  3112. XINT (exp, 1) ? "!" : "", set);
  3113. }
  3114. }
  3115. break;
  3116. /* Comparison test of an attribute with a value. Most of these will
  3117. have been removed by optimization. Handle "alternative"
  3118. specially and give error if EQ_ATTR present inside a comparison. */
  3119. case EQ_ATTR:
  3120. if (flags & FLG_BITWISE)
  3121. fatal ("EQ_ATTR not valid inside comparison");
  3122. if (XSTR (exp, 0) == alternative_name)
  3123. {
  3124. fprintf (outf, "which_alternative == %s", XSTR (exp, 1));
  3125. break;
  3126. }
  3127. attr = find_attr (&XSTR (exp, 0), 0);
  3128. gcc_assert (attr);
  3129. /* Now is the time to expand the value of a constant attribute. */
  3130. if (attr->is_const)
  3131. {
  3132. write_test_expr (outf,
  3133. evaluate_eq_attr (exp, attr,
  3134. attr->default_val->value,
  3135. -2, -2),
  3136. attrs_cached, 0);
  3137. }
  3138. else
  3139. {
  3140. int i;
  3141. for (i = 0; i < cached_attr_count; i++)
  3142. if (attr->name == cached_attrs[i])
  3143. break;
  3144. if (i < cached_attr_count && (attrs_cached & (1U << i)) != 0)
  3145. fprintf (outf, "cached_%s", attr->name);
  3146. else if (i < cached_attr_count && (attrs_to_cache & (1U << i)) != 0)
  3147. {
  3148. fprintf (outf, "(cached_%s = get_attr_%s (insn))",
  3149. attr->name, attr->name);
  3150. if (flags & FLG_AFTER)
  3151. attrs_cached_after |= (1U << i);
  3152. if (flags & FLG_INSIDE)
  3153. attrs_cached_inside |= (1U << i);
  3154. attrs_cached |= (1U << i);
  3155. }
  3156. else
  3157. fprintf (outf, "get_attr_%s (insn)", attr->name);
  3158. fprintf (outf, " == ");
  3159. write_attr_valueq (outf, attr, XSTR (exp, 1));
  3160. }
  3161. break;
  3162. /* Comparison test of flags for define_delays. */
  3163. case ATTR_FLAG:
  3164. if (flags & FLG_BITWISE)
  3165. fatal ("ATTR_FLAG not valid inside comparison");
  3166. fprintf (outf, "(flags & ATTR_FLAG_%s) != 0", XSTR (exp, 0));
  3167. break;
  3168. /* See if an operand matches a predicate. */
  3169. case MATCH_OPERAND:
  3170. /* If only a mode is given, just ensure the mode matches the operand.
  3171. If neither a mode nor predicate is given, error. */
  3172. if (XSTR (exp, 1) == NULL || *XSTR (exp, 1) == '\0')
  3173. {
  3174. if (GET_MODE (exp) == VOIDmode)
  3175. fatal ("null MATCH_OPERAND specified as test");
  3176. else
  3177. fprintf (outf, "GET_MODE (operands[%d]) == %smode",
  3178. XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
  3179. }
  3180. else
  3181. fprintf (outf, "%s (operands[%d], %smode)",
  3182. XSTR (exp, 1), XINT (exp, 0), GET_MODE_NAME (GET_MODE (exp)));
  3183. break;
  3184. /* Constant integer. */
  3185. case CONST_INT:
  3186. fprintf (outf, HOST_WIDE_INT_PRINT_DEC, XWINT (exp, 0));
  3187. break;
  3188. case MATCH_TEST:
  3189. fprint_c_condition (outf, XSTR (exp, 0));
  3190. if (flags & FLG_BITWISE)
  3191. fprintf (outf, " != 0");
  3192. break;
  3193. /* A random C expression. */
  3194. case SYMBOL_REF:
  3195. fprint_c_condition (outf, XSTR (exp, 0));
  3196. break;
  3197. /* The address of the branch target. */
  3198. case MATCH_DUP:
  3199. fprintf (outf,
  3200. "INSN_ADDRESSES_SET_P () ? INSN_ADDRESSES (INSN_UID (GET_CODE (operands[%d]) == LABEL_REF ? XEXP (operands[%d], 0) : operands[%d])) : 0",
  3201. XINT (exp, 0), XINT (exp, 0), XINT (exp, 0));
  3202. break;
  3203. case PC:
  3204. /* The address of the current insn. We implement this actually as the
  3205. address of the current insn for backward branches, but the last
  3206. address of the next insn for forward branches, and both with
  3207. adjustments that account for the worst-case possible stretching of
  3208. intervening alignments between this insn and its destination. */
  3209. fprintf (outf, "insn_current_reference_address (insn)");
  3210. break;
  3211. case CONST_STRING:
  3212. fprintf (outf, "%s", XSTR (exp, 0));
  3213. break;
  3214. case IF_THEN_ELSE:
  3215. write_test_expr (outf, XEXP (exp, 0), attrs_cached, 0);
  3216. fprintf (outf, " ? ");
  3217. write_test_expr (outf, XEXP (exp, 1), attrs_cached, FLG_BITWISE);
  3218. fprintf (outf, " : ");
  3219. write_test_expr (outf, XEXP (exp, 2), attrs_cached, FLG_BITWISE);
  3220. break;
  3221. default:
  3222. fatal ("bad RTX code `%s' in attribute calculation\n",
  3223. GET_RTX_NAME (code));
  3224. }
  3225. fprintf (outf, ")");
  3226. return attrs_cached;
  3227. }
  3228. /* Given an attribute value, return the maximum CONST_STRING argument
  3229. encountered. Set *UNKNOWNP and return INT_MAX if the value is unknown. */
  3230. static int
  3231. max_attr_value (rtx exp, int *unknownp)
  3232. {
  3233. int current_max;
  3234. int i, n;
  3235. switch (GET_CODE (exp))
  3236. {
  3237. case CONST_STRING:
  3238. current_max = atoi (XSTR (exp, 0));
  3239. break;
  3240. case COND:
  3241. current_max = max_attr_value (XEXP (exp, 1), unknownp);
  3242. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  3243. {
  3244. n = max_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
  3245. if (n > current_max)
  3246. current_max = n;
  3247. }
  3248. break;
  3249. case IF_THEN_ELSE:
  3250. current_max = max_attr_value (XEXP (exp, 1), unknownp);
  3251. n = max_attr_value (XEXP (exp, 2), unknownp);
  3252. if (n > current_max)
  3253. current_max = n;
  3254. break;
  3255. default:
  3256. *unknownp = 1;
  3257. current_max = INT_MAX;
  3258. break;
  3259. }
  3260. return current_max;
  3261. }
  3262. /* Given an attribute value, return the minimum CONST_STRING argument
  3263. encountered. Set *UNKNOWNP and return 0 if the value is unknown. */
  3264. static int
  3265. min_attr_value (rtx exp, int *unknownp)
  3266. {
  3267. int current_min;
  3268. int i, n;
  3269. switch (GET_CODE (exp))
  3270. {
  3271. case CONST_STRING:
  3272. current_min = atoi (XSTR (exp, 0));
  3273. break;
  3274. case COND:
  3275. current_min = min_attr_value (XEXP (exp, 1), unknownp);
  3276. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  3277. {
  3278. n = min_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
  3279. if (n < current_min)
  3280. current_min = n;
  3281. }
  3282. break;
  3283. case IF_THEN_ELSE:
  3284. current_min = min_attr_value (XEXP (exp, 1), unknownp);
  3285. n = min_attr_value (XEXP (exp, 2), unknownp);
  3286. if (n < current_min)
  3287. current_min = n;
  3288. break;
  3289. default:
  3290. *unknownp = 1;
  3291. current_min = INT_MAX;
  3292. break;
  3293. }
  3294. return current_min;
  3295. }
  3296. /* Given an attribute value, return the result of ORing together all
  3297. CONST_STRING arguments encountered. Set *UNKNOWNP and return -1
  3298. if the numeric value is not known. */
  3299. static int
  3300. or_attr_value (rtx exp, int *unknownp)
  3301. {
  3302. int current_or;
  3303. int i;
  3304. switch (GET_CODE (exp))
  3305. {
  3306. case CONST_STRING:
  3307. current_or = atoi (XSTR (exp, 0));
  3308. break;
  3309. case COND:
  3310. current_or = or_attr_value (XEXP (exp, 1), unknownp);
  3311. for (i = 0; i < XVECLEN (exp, 0); i += 2)
  3312. current_or |= or_attr_value (XVECEXP (exp, 0, i + 1), unknownp);
  3313. break;
  3314. case IF_THEN_ELSE:
  3315. current_or = or_attr_value (XEXP (exp, 1), unknownp);
  3316. current_or |= or_attr_value (XEXP (exp, 2), unknownp);
  3317. break;
  3318. default:
  3319. *unknownp = 1;
  3320. current_or = -1;
  3321. break;
  3322. }
  3323. return current_or;
  3324. }
  3325. /* Scan an attribute value, possibly a conditional, and record what actions
  3326. will be required to do any conditional tests in it.
  3327. Specifically, set
  3328. `must_extract' if we need to extract the insn operands
  3329. `must_constrain' if we must compute `which_alternative'
  3330. `address_used' if an address expression was used
  3331. `length_used' if an (eq_attr "length" ...) was used
  3332. */
  3333. static void
  3334. walk_attr_value (rtx exp)
  3335. {
  3336. int i, j;
  3337. const char *fmt;
  3338. RTX_CODE code;
  3339. if (exp == NULL)
  3340. return;
  3341. code = GET_CODE (exp);
  3342. switch (code)
  3343. {
  3344. case SYMBOL_REF:
  3345. if (! ATTR_IND_SIMPLIFIED_P (exp))
  3346. /* Since this is an arbitrary expression, it can look at anything.
  3347. However, constant expressions do not depend on any particular
  3348. insn. */
  3349. must_extract = must_constrain = 1;
  3350. return;
  3351. case MATCH_OPERAND:
  3352. must_extract = 1;
  3353. return;
  3354. case MATCH_TEST:
  3355. case EQ_ATTR_ALT:
  3356. must_extract = must_constrain = 1;
  3357. break;
  3358. case EQ_ATTR:
  3359. if (XSTR (exp, 0) == alternative_name)
  3360. must_extract = must_constrain = 1;
  3361. else if (strcmp_check (XSTR (exp, 0), length_str) == 0)
  3362. length_used = 1;
  3363. return;
  3364. case MATCH_DUP:
  3365. must_extract = 1;
  3366. address_used = 1;
  3367. return;
  3368. case PC:
  3369. address_used = 1;
  3370. return;
  3371. case ATTR_FLAG:
  3372. return;
  3373. default:
  3374. break;
  3375. }
  3376. for (i = 0, fmt = GET_RTX_FORMAT (code); i < GET_RTX_LENGTH (code); i++)
  3377. switch (*fmt++)
  3378. {
  3379. case 'e':
  3380. case 'u':
  3381. walk_attr_value (XEXP (exp, i));
  3382. break;
  3383. case 'E':
  3384. if (XVEC (exp, i) != NULL)
  3385. for (j = 0; j < XVECLEN (exp, i); j++)
  3386. walk_attr_value (XVECEXP (exp, i, j));
  3387. break;
  3388. }
  3389. }
  3390. /* Write out a function to obtain the attribute for a given INSN. */
  3391. static void
  3392. write_attr_get (FILE *outf, struct attr_desc *attr)
  3393. {
  3394. struct attr_value *av, *common_av;
  3395. int i, j;
  3396. /* Find the most used attribute value. Handle that as the `default' of the
  3397. switch we will generate. */
  3398. common_av = find_most_used (attr);
  3399. /* Write out start of function, then all values with explicit `case' lines,
  3400. then a `default', then the value with the most uses. */
  3401. if (attr->enum_name)
  3402. fprintf (outf, "enum %s\n", attr->enum_name);
  3403. else if (!attr->is_numeric)
  3404. fprintf (outf, "enum attr_%s\n", attr->name);
  3405. else
  3406. fprintf (outf, "int\n");
  3407. /* If the attribute name starts with a star, the remainder is the name of
  3408. the subroutine to use, instead of `get_attr_...'. */
  3409. if (attr->name[0] == '*')
  3410. fprintf (outf, "%s (rtx_insn *insn ATTRIBUTE_UNUSED)\n", &attr->name[1]);
  3411. else if (attr->is_const == 0)
  3412. fprintf (outf, "get_attr_%s (rtx_insn *insn ATTRIBUTE_UNUSED)\n", attr->name);
  3413. else
  3414. {
  3415. fprintf (outf, "get_attr_%s (void)\n", attr->name);
  3416. fprintf (outf, "{\n");
  3417. for (av = attr->first_value; av; av = av->next)
  3418. if (av->num_insns == 1)
  3419. write_attr_set (outf, attr, 2, av->value, "return", ";",
  3420. true_rtx, av->first_insn->def->insn_code,
  3421. av->first_insn->def->insn_index, 0);
  3422. else if (av->num_insns != 0)
  3423. write_attr_set (outf, attr, 2, av->value, "return", ";",
  3424. true_rtx, -2, 0, 0);
  3425. fprintf (outf, "}\n\n");
  3426. return;
  3427. }
  3428. fprintf (outf, "{\n");
  3429. /* Find attributes that are worth caching in the conditions. */
  3430. cached_attr_count = 0;
  3431. attrs_seen_more_than_once = 0;
  3432. for (av = attr->first_value; av; av = av->next)
  3433. {
  3434. attrs_seen_once = 0;
  3435. find_attrs_to_cache (av->value, true);
  3436. }
  3437. /* Remove those that aren't worth caching from the array. */
  3438. for (i = 0, j = 0; i < cached_attr_count; i++)
  3439. if ((attrs_seen_more_than_once & (1U << i)) != 0)
  3440. {
  3441. const char *name = cached_attrs[i];
  3442. struct attr_desc *cached_attr;
  3443. if (i != j)
  3444. cached_attrs[j] = name;
  3445. cached_attr = find_attr (&name, 0);
  3446. gcc_assert (cached_attr && cached_attr->is_const == 0);
  3447. if (cached_attr->enum_name)
  3448. fprintf (outf, " enum %s", cached_attr->enum_name);
  3449. else if (!cached_attr->is_numeric)
  3450. fprintf (outf, " enum attr_%s", cached_attr->name);
  3451. else
  3452. fprintf (outf, " int");
  3453. fprintf (outf, " cached_%s ATTRIBUTE_UNUSED;\n", name);
  3454. j++;
  3455. }
  3456. cached_attr_count = j;
  3457. if (cached_attr_count)
  3458. fprintf (outf, "\n");
  3459. fprintf (outf, " switch (recog_memoized (insn))\n");
  3460. fprintf (outf, " {\n");
  3461. for (av = attr->first_value; av; av = av->next)
  3462. if (av != common_av)
  3463. write_attr_case (outf, attr, av, 1, "return", ";", 4, true_rtx);
  3464. write_attr_case (outf, attr, common_av, 0, "return", ";", 4, true_rtx);
  3465. fprintf (outf, " }\n}\n\n");
  3466. cached_attr_count = 0;
  3467. }
  3468. /* Given an AND tree of known true terms (because we are inside an `if' with
  3469. that as the condition or are in an `else' clause) and an expression,
  3470. replace any known true terms with TRUE. Use `simplify_and_tree' to do
  3471. the bulk of the work. */
  3472. static rtx
  3473. eliminate_known_true (rtx known_true, rtx exp, int insn_code, int insn_index)
  3474. {
  3475. rtx term;
  3476. known_true = SIMPLIFY_TEST_EXP (known_true, insn_code, insn_index);
  3477. if (GET_CODE (known_true) == AND)
  3478. {
  3479. exp = eliminate_known_true (XEXP (known_true, 0), exp,
  3480. insn_code, insn_index);
  3481. exp = eliminate_known_true (XEXP (known_true, 1), exp,
  3482. insn_code, insn_index);
  3483. }
  3484. else
  3485. {
  3486. term = known_true;
  3487. exp = simplify_and_tree (exp, &term, insn_code, insn_index);
  3488. }
  3489. return exp;
  3490. }
  3491. /* Write out a series of tests and assignment statements to perform tests and
  3492. sets of an attribute value. We are passed an indentation amount and prefix
  3493. and suffix strings to write around each attribute value (e.g., "return"
  3494. and ";"). */
  3495. static void
  3496. write_attr_set (FILE *outf, struct attr_desc *attr, int indent, rtx value,
  3497. const char *prefix, const char *suffix, rtx known_true,
  3498. int insn_code, int insn_index, unsigned int attrs_cached)
  3499. {
  3500. if (GET_CODE (value) == COND)
  3501. {
  3502. /* Assume the default value will be the default of the COND unless we
  3503. find an always true expression. */
  3504. rtx default_val = XEXP (value, 1);
  3505. rtx our_known_true = known_true;
  3506. rtx newexp;
  3507. int first_if = 1;
  3508. int i;
  3509. if (cached_attr_count)
  3510. {
  3511. attrs_seen_once = 0;
  3512. attrs_seen_more_than_once = 0;
  3513. for (i = 0; i < XVECLEN (value, 0); i += 2)
  3514. find_attrs_to_cache (XVECEXP (value, 0, i), false);
  3515. attrs_to_cache |= attrs_seen_more_than_once;
  3516. }
  3517. for (i = 0; i < XVECLEN (value, 0); i += 2)
  3518. {
  3519. rtx testexp;
  3520. rtx inner_true;
  3521. /* Reset our_known_true after some time to not accumulate
  3522. too much cruft (slowing down genattrtab). */
  3523. if ((i & 31) == 0)
  3524. our_known_true = known_true;
  3525. testexp = eliminate_known_true (our_known_true,
  3526. XVECEXP (value, 0, i),
  3527. insn_code, insn_index);
  3528. newexp = attr_rtx (NOT, testexp);
  3529. newexp = insert_right_side (AND, our_known_true, newexp,
  3530. insn_code, insn_index);
  3531. /* If the test expression is always true or if the next `known_true'
  3532. expression is always false, this is the last case, so break
  3533. out and let this value be the `else' case. */
  3534. if (testexp == true_rtx || newexp == false_rtx)
  3535. {
  3536. default_val = XVECEXP (value, 0, i + 1);
  3537. break;
  3538. }
  3539. /* Compute the expression to pass to our recursive call as being
  3540. known true. */
  3541. inner_true = insert_right_side (AND, our_known_true,
  3542. testexp, insn_code, insn_index);
  3543. /* If this is always false, skip it. */
  3544. if (inner_true == false_rtx)
  3545. continue;
  3546. attrs_cached_inside = attrs_cached;
  3547. attrs_cached_after = attrs_cached;
  3548. write_indent (outf, indent);
  3549. fprintf (outf, "%sif ", first_if ? "" : "else ");
  3550. first_if = 0;
  3551. write_test_expr (outf, testexp, attrs_cached,
  3552. (FLG_AFTER | FLG_INSIDE | FLG_OUTSIDE_AND));
  3553. attrs_cached = attrs_cached_after;
  3554. fprintf (outf, "\n");
  3555. write_indent (outf, indent + 2);
  3556. fprintf (outf, "{\n");
  3557. write_attr_set (outf, attr, indent + 4,
  3558. XVECEXP (value, 0, i + 1), prefix, suffix,
  3559. inner_true, insn_code, insn_index,
  3560. attrs_cached_inside);
  3561. write_indent (outf, indent + 2);
  3562. fprintf (outf, "}\n");
  3563. our_known_true = newexp;
  3564. }
  3565. if (! first_if)
  3566. {
  3567. write_indent (outf, indent);
  3568. fprintf (outf, "else\n");
  3569. write_indent (outf, indent + 2);
  3570. fprintf (outf, "{\n");
  3571. }
  3572. write_attr_set (outf, attr, first_if ? indent : indent + 4, default_val,
  3573. prefix, suffix, our_known_true, insn_code, insn_index,
  3574. attrs_cached);
  3575. if (! first_if)
  3576. {
  3577. write_indent (outf, indent + 2);
  3578. fprintf (outf, "}\n");
  3579. }
  3580. }
  3581. else
  3582. {
  3583. write_indent (outf, indent);
  3584. fprintf (outf, "%s ", prefix);
  3585. write_attr_value (outf, attr, value);
  3586. fprintf (outf, "%s\n", suffix);
  3587. }
  3588. }
  3589. /* Write a series of case statements for every instruction in list IE.
  3590. INDENT is the amount of indentation to write before each case. */
  3591. static void
  3592. write_insn_cases (FILE *outf, struct insn_ent *ie, int indent)
  3593. {
  3594. for (; ie != 0; ie = ie->next)
  3595. if (ie->def->insn_code != -1)
  3596. {
  3597. write_indent (outf, indent);
  3598. if (GET_CODE (ie->def->def) == DEFINE_PEEPHOLE)
  3599. fprintf (outf, "case %d: /* define_peephole, line %d */\n",
  3600. ie->def->insn_code, ie->def->lineno);
  3601. else
  3602. fprintf (outf, "case %d: /* %s */\n",
  3603. ie->def->insn_code, XSTR (ie->def->def, 0));
  3604. }
  3605. }
  3606. /* Write out the computation for one attribute value. */
  3607. static void
  3608. write_attr_case (FILE *outf, struct attr_desc *attr, struct attr_value *av,
  3609. int write_case_lines, const char *prefix, const char *suffix,
  3610. int indent, rtx known_true)
  3611. {
  3612. if (av->num_insns == 0)
  3613. return;
  3614. if (av->has_asm_insn)
  3615. {
  3616. write_indent (outf, indent);
  3617. fprintf (outf, "case -1:\n");
  3618. write_indent (outf, indent + 2);
  3619. fprintf (outf, "if (GET_CODE (PATTERN (insn)) != ASM_INPUT\n");
  3620. write_indent (outf, indent + 2);
  3621. fprintf (outf, " && asm_noperands (PATTERN (insn)) < 0)\n");
  3622. write_indent (outf, indent + 2);
  3623. fprintf (outf, " fatal_insn_not_found (insn);\n");
  3624. }
  3625. if (write_case_lines)
  3626. write_insn_cases (outf, av->first_insn, indent);
  3627. else
  3628. {
  3629. write_indent (outf, indent);
  3630. fprintf (outf, "default:\n");
  3631. }
  3632. /* See what we have to do to output this value. */
  3633. must_extract = must_constrain = address_used = 0;
  3634. walk_attr_value (av->value);
  3635. if (must_constrain)
  3636. {
  3637. write_indent (outf, indent + 2);
  3638. fprintf (outf, "extract_constrain_insn_cached (insn);\n");
  3639. }
  3640. else if (must_extract)
  3641. {
  3642. write_indent (outf, indent + 2);
  3643. fprintf (outf, "extract_insn_cached (insn);\n");
  3644. }
  3645. attrs_to_cache = 0;
  3646. if (av->num_insns == 1)
  3647. write_attr_set (outf, attr, indent + 2, av->value, prefix, suffix,
  3648. known_true, av->first_insn->def->insn_code,
  3649. av->first_insn->def->insn_index, 0);
  3650. else
  3651. write_attr_set (outf, attr, indent + 2, av->value, prefix, suffix,
  3652. known_true, -2, 0, 0);
  3653. if (strncmp (prefix, "return", 6))
  3654. {
  3655. write_indent (outf, indent + 2);
  3656. fprintf (outf, "break;\n");
  3657. }
  3658. fprintf (outf, "\n");
  3659. }
  3660. /* Utilities to write in various forms. */
  3661. static void
  3662. write_attr_valueq (FILE *outf, struct attr_desc *attr, const char *s)
  3663. {
  3664. if (attr->is_numeric)
  3665. {
  3666. int num = atoi (s);
  3667. fprintf (outf, "%d", num);
  3668. if (num > 9 || num < 0)
  3669. fprintf (outf, " /* %#x */", num);
  3670. }
  3671. else
  3672. {
  3673. write_upcase (outf, attr->enum_name ? attr->enum_name : attr->name);
  3674. fprintf (outf, "_");
  3675. write_upcase (outf, s);
  3676. }
  3677. }
  3678. static void
  3679. write_attr_value (FILE *outf, struct attr_desc *attr, rtx value)
  3680. {
  3681. int op;
  3682. switch (GET_CODE (value))
  3683. {
  3684. case CONST_STRING:
  3685. write_attr_valueq (outf, attr, XSTR (value, 0));
  3686. break;
  3687. case CONST_INT:
  3688. fprintf (outf, HOST_WIDE_INT_PRINT_DEC, INTVAL (value));
  3689. break;
  3690. case SYMBOL_REF:
  3691. fprint_c_condition (outf, XSTR (value, 0));
  3692. break;
  3693. case ATTR:
  3694. {
  3695. struct attr_desc *attr2 = find_attr (&XSTR (value, 0), 0);
  3696. if (attr->enum_name)
  3697. fprintf (outf, "(enum %s)", attr->enum_name);
  3698. else if (!attr->is_numeric)
  3699. fprintf (outf, "(enum attr_%s)", attr->name);
  3700. else if (!attr2->is_numeric)
  3701. fprintf (outf, "(int)");
  3702. fprintf (outf, "get_attr_%s (%s)", attr2->name,
  3703. (attr2->is_const ? "" : "insn"));
  3704. }
  3705. break;
  3706. case PLUS:
  3707. op = '+';
  3708. goto do_operator;
  3709. case MINUS:
  3710. op = '-';
  3711. goto do_operator;
  3712. case MULT:
  3713. op = '*';
  3714. goto do_operator;
  3715. case DIV:
  3716. op = '/';
  3717. goto do_operator;
  3718. case MOD:
  3719. op = '%';
  3720. goto do_operator;
  3721. do_operator:
  3722. write_attr_value (outf, attr, XEXP (value, 0));
  3723. fputc (' ', outf);
  3724. fputc (op, outf);
  3725. fputc (' ', outf);
  3726. write_attr_value (outf, attr, XEXP (value, 1));
  3727. break;
  3728. default:
  3729. gcc_unreachable ();
  3730. }
  3731. }
  3732. static void
  3733. write_upcase (FILE *outf, const char *str)
  3734. {
  3735. while (*str)
  3736. {
  3737. /* The argument of TOUPPER should not have side effects. */
  3738. fputc (TOUPPER (*str), outf);
  3739. str++;
  3740. }
  3741. }
  3742. static void
  3743. write_indent (FILE *outf, int indent)
  3744. {
  3745. for (; indent > 8; indent -= 8)
  3746. fprintf (outf, "\t");
  3747. for (; indent; indent--)
  3748. fprintf (outf, " ");
  3749. }
  3750. /* Write a subroutine that is given an insn that requires a delay slot, a
  3751. delay slot ordinal, and a candidate insn. It returns nonzero if the
  3752. candidate can be placed in the specified delay slot of the insn.
  3753. We can write as many as three subroutines. `eligible_for_delay'
  3754. handles normal delay slots, `eligible_for_annul_true' indicates that
  3755. the specified insn can be annulled if the branch is true, and likewise
  3756. for `eligible_for_annul_false'.
  3757. KIND is a string distinguishing these three cases ("delay", "annul_true",
  3758. or "annul_false"). */
  3759. static void
  3760. write_eligible_delay (FILE *outf, const char *kind)
  3761. {
  3762. struct delay_desc *delay;
  3763. int max_slots;
  3764. char str[50];
  3765. const char *pstr;
  3766. struct attr_desc *attr;
  3767. struct attr_value *av, *common_av;
  3768. int i;
  3769. /* Compute the maximum number of delay slots required. We use the delay
  3770. ordinal times this number plus one, plus the slot number as an index into
  3771. the appropriate predicate to test. */
  3772. for (delay = delays, max_slots = 0; delay; delay = delay->next)
  3773. if (XVECLEN (delay->def, 1) / 3 > max_slots)
  3774. max_slots = XVECLEN (delay->def, 1) / 3;
  3775. /* Write function prelude. */
  3776. fprintf (outf, "int\n");
  3777. fprintf (outf, "eligible_for_%s (rtx_insn *delay_insn ATTRIBUTE_UNUSED, int slot, \n"
  3778. " rtx_insn *candidate_insn, int flags ATTRIBUTE_UNUSED)\n",
  3779. kind);
  3780. fprintf (outf, "{\n");
  3781. fprintf (outf, " rtx_insn *insn;\n");
  3782. fprintf (outf, "\n");
  3783. fprintf (outf, " gcc_assert (slot < %d);\n", max_slots);
  3784. fprintf (outf, "\n");
  3785. /* Allow dbr_schedule to pass labels, etc. This can happen if try_split
  3786. converts a compound instruction into a loop. */
  3787. fprintf (outf, " if (!INSN_P (candidate_insn))\n");
  3788. fprintf (outf, " return 0;\n");
  3789. fprintf (outf, "\n");
  3790. /* If more than one delay type, find out which type the delay insn is. */
  3791. if (num_delays > 1)
  3792. {
  3793. attr = find_attr (&delay_type_str, 0);
  3794. gcc_assert (attr);
  3795. common_av = find_most_used (attr);
  3796. fprintf (outf, " insn = delay_insn;\n");
  3797. fprintf (outf, " switch (recog_memoized (insn))\n");
  3798. fprintf (outf, " {\n");
  3799. sprintf (str, " * %d;\n break;", max_slots);
  3800. for (av = attr->first_value; av; av = av->next)
  3801. if (av != common_av)
  3802. write_attr_case (outf, attr, av, 1, "slot +=", str, 4, true_rtx);
  3803. write_attr_case (outf, attr, common_av, 0, "slot +=", str, 4, true_rtx);
  3804. fprintf (outf, " }\n\n");
  3805. /* Ensure matched. Otherwise, shouldn't have been called. */
  3806. fprintf (outf, " gcc_assert (slot >= %d);\n\n", max_slots);
  3807. }
  3808. /* If just one type of delay slot, write simple switch. */
  3809. if (num_delays == 1 && max_slots == 1)
  3810. {
  3811. fprintf (outf, " insn = candidate_insn;\n");
  3812. fprintf (outf, " switch (recog_memoized (insn))\n");
  3813. fprintf (outf, " {\n");
  3814. attr = find_attr (&delay_1_0_str, 0);
  3815. gcc_assert (attr);
  3816. common_av = find_most_used (attr);
  3817. for (av = attr->first_value; av; av = av->next)
  3818. if (av != common_av)
  3819. write_attr_case (outf, attr, av, 1, "return", ";", 4, true_rtx);
  3820. write_attr_case (outf, attr, common_av, 0, "return", ";", 4, true_rtx);
  3821. fprintf (outf, " }\n");
  3822. }
  3823. else
  3824. {
  3825. /* Write a nested CASE. The first indicates which condition we need to
  3826. test, and the inner CASE tests the condition. */
  3827. fprintf (outf, " insn = candidate_insn;\n");
  3828. fprintf (outf, " switch (slot)\n");
  3829. fprintf (outf, " {\n");
  3830. for (delay = delays; delay; delay = delay->next)
  3831. for (i = 0; i < XVECLEN (delay->def, 1); i += 3)
  3832. {
  3833. fprintf (outf, " case %d:\n",
  3834. (i / 3) + (num_delays == 1 ? 0 : delay->num * max_slots));
  3835. fprintf (outf, " switch (recog_memoized (insn))\n");
  3836. fprintf (outf, "\t{\n");
  3837. sprintf (str, "*%s_%d_%d", kind, delay->num, i / 3);
  3838. pstr = str;
  3839. attr = find_attr (&pstr, 0);
  3840. gcc_assert (attr);
  3841. common_av = find_most_used (attr);
  3842. for (av = attr->first_value; av; av = av->next)
  3843. if (av != common_av)
  3844. write_attr_case (outf, attr, av, 1, "return", ";", 8, true_rtx);
  3845. write_attr_case (outf, attr, common_av, 0, "return", ";", 8, true_rtx);
  3846. fprintf (outf, " }\n");
  3847. }
  3848. fprintf (outf, " default:\n");
  3849. fprintf (outf, " gcc_unreachable ();\n");
  3850. fprintf (outf, " }\n");
  3851. }
  3852. fprintf (outf, "}\n\n");
  3853. }
  3854. /* This page contains miscellaneous utility routines. */
  3855. /* Given a pointer to a (char *), return a malloc'ed string containing the
  3856. next comma-separated element. Advance the pointer to after the string
  3857. scanned, or the end-of-string. Return NULL if at end of string. */
  3858. static char *
  3859. next_comma_elt (const char **pstr)
  3860. {
  3861. const char *start;
  3862. start = scan_comma_elt (pstr);
  3863. if (start == NULL)
  3864. return NULL;
  3865. return attr_string (start, *pstr - start);
  3866. }
  3867. /* Return a `struct attr_desc' pointer for a given named attribute. If CREATE
  3868. is nonzero, build a new attribute, if one does not exist. *NAME_P is
  3869. replaced by a pointer to a canonical copy of the string. */
  3870. static struct attr_desc *
  3871. find_attr (const char **name_p, int create)
  3872. {
  3873. struct attr_desc *attr;
  3874. int index;
  3875. const char *name = *name_p;
  3876. /* Before we resort to using `strcmp', see if the string address matches
  3877. anywhere. In most cases, it should have been canonicalized to do so. */
  3878. if (name == alternative_name)
  3879. return NULL;
  3880. index = name[0] & (MAX_ATTRS_INDEX - 1);
  3881. for (attr = attrs[index]; attr; attr = attr->next)
  3882. if (name == attr->name)
  3883. return attr;
  3884. /* Otherwise, do it the slow way. */
  3885. for (attr = attrs[index]; attr; attr = attr->next)
  3886. if (name[0] == attr->name[0] && ! strcmp (name, attr->name))
  3887. {
  3888. *name_p = attr->name;
  3889. return attr;
  3890. }
  3891. if (! create)
  3892. return NULL;
  3893. attr = oballoc (struct attr_desc);
  3894. attr->name = DEF_ATTR_STRING (name);
  3895. attr->enum_name = 0;
  3896. attr->first_value = attr->default_val = NULL;
  3897. attr->is_numeric = attr->is_const = attr->is_special = 0;
  3898. attr->next = attrs[index];
  3899. attrs[index] = attr;
  3900. *name_p = attr->name;
  3901. return attr;
  3902. }
  3903. /* Create internal attribute with the given default value. */
  3904. static void
  3905. make_internal_attr (const char *name, rtx value, int special)
  3906. {
  3907. struct attr_desc *attr;
  3908. attr = find_attr (&name, 1);
  3909. gcc_assert (!attr->default_val);
  3910. attr->is_numeric = 1;
  3911. attr->is_const = 0;
  3912. attr->is_special = (special & ATTR_SPECIAL) != 0;
  3913. attr->default_val = get_attr_value (value, attr, -2);
  3914. }
  3915. /* Find the most used value of an attribute. */
  3916. static struct attr_value *
  3917. find_most_used (struct attr_desc *attr)
  3918. {
  3919. struct attr_value *av;
  3920. struct attr_value *most_used;
  3921. int nuses;
  3922. most_used = NULL;
  3923. nuses = -1;
  3924. for (av = attr->first_value; av; av = av->next)
  3925. if (av->num_insns > nuses)
  3926. nuses = av->num_insns, most_used = av;
  3927. return most_used;
  3928. }
  3929. /* Return (attr_value "n") */
  3930. static rtx
  3931. make_numeric_value (int n)
  3932. {
  3933. static rtx int_values[20];
  3934. rtx exp;
  3935. char *p;
  3936. gcc_assert (n >= 0);
  3937. if (n < 20 && int_values[n])
  3938. return int_values[n];
  3939. p = attr_printf (MAX_DIGITS, "%d", n);
  3940. exp = attr_rtx (CONST_STRING, p);
  3941. if (n < 20)
  3942. int_values[n] = exp;
  3943. return exp;
  3944. }
  3945. static rtx
  3946. copy_rtx_unchanging (rtx orig)
  3947. {
  3948. if (ATTR_IND_SIMPLIFIED_P (orig) || ATTR_CURR_SIMPLIFIED_P (orig))
  3949. return orig;
  3950. ATTR_CURR_SIMPLIFIED_P (orig) = 1;
  3951. return orig;
  3952. }
  3953. /* Determine if an insn has a constant number of delay slots, i.e., the
  3954. number of delay slots is not a function of the length of the insn. */
  3955. static void
  3956. write_const_num_delay_slots (FILE *outf)
  3957. {
  3958. struct attr_desc *attr = find_attr (&num_delay_slots_str, 0);
  3959. struct attr_value *av;
  3960. if (attr)
  3961. {
  3962. fprintf (outf, "int\nconst_num_delay_slots (rtx_insn *insn)\n");
  3963. fprintf (outf, "{\n");
  3964. fprintf (outf, " switch (recog_memoized (insn))\n");
  3965. fprintf (outf, " {\n");
  3966. for (av = attr->first_value; av; av = av->next)
  3967. {
  3968. length_used = 0;
  3969. walk_attr_value (av->value);
  3970. if (length_used)
  3971. write_insn_cases (outf, av->first_insn, 4);
  3972. }
  3973. fprintf (outf, " default:\n");
  3974. fprintf (outf, " return 1;\n");
  3975. fprintf (outf, " }\n}\n\n");
  3976. }
  3977. }
  3978. /* Synthetic attributes used by insn-automata.c and the scheduler.
  3979. These are primarily concerned with (define_insn_reservation)
  3980. patterns. */
  3981. struct insn_reserv
  3982. {
  3983. struct insn_reserv *next;
  3984. const char *name;
  3985. int default_latency;
  3986. rtx condexp;
  3987. /* Sequence number of this insn. */
  3988. int insn_num;
  3989. /* Whether a (define_bypass) construct names this insn in its
  3990. output list. */
  3991. bool bypassed;
  3992. };
  3993. static struct insn_reserv *all_insn_reservs = 0;
  3994. static struct insn_reserv **last_insn_reserv_p = &all_insn_reservs;
  3995. static size_t n_insn_reservs;
  3996. /* Store information from a DEFINE_INSN_RESERVATION for future
  3997. attribute generation. */
  3998. static void
  3999. gen_insn_reserv (rtx def)
  4000. {
  4001. struct insn_reserv *decl = oballoc (struct insn_reserv);
  4002. decl->name = DEF_ATTR_STRING (XSTR (def, 0));
  4003. decl->default_latency = XINT (def, 1);
  4004. decl->condexp = check_attr_test (XEXP (def, 2), 0, 0);
  4005. decl->insn_num = n_insn_reservs;
  4006. decl->bypassed = false;
  4007. decl->next = 0;
  4008. *last_insn_reserv_p = decl;
  4009. last_insn_reserv_p = &decl->next;
  4010. n_insn_reservs++;
  4011. }
  4012. /* Store information from a DEFINE_BYPASS for future attribute
  4013. generation. The only thing we care about is the list of output
  4014. insns, which will later be used to tag reservation structures with
  4015. a 'bypassed' bit. */
  4016. struct bypass_list
  4017. {
  4018. struct bypass_list *next;
  4019. const char *pattern;
  4020. };
  4021. static struct bypass_list *all_bypasses;
  4022. static size_t n_bypasses;
  4023. static size_t n_bypassed;
  4024. static void
  4025. gen_bypass_1 (const char *s, size_t len)
  4026. {
  4027. struct bypass_list *b;
  4028. if (len == 0)
  4029. return;
  4030. s = attr_string (s, len);
  4031. for (b = all_bypasses; b; b = b->next)
  4032. if (s == b->pattern)
  4033. return; /* already got that one */
  4034. b = oballoc (struct bypass_list);
  4035. b->pattern = s;
  4036. b->next = all_bypasses;
  4037. all_bypasses = b;
  4038. n_bypasses++;
  4039. }
  4040. static void
  4041. gen_bypass (rtx def)
  4042. {
  4043. const char *p, *base;
  4044. for (p = base = XSTR (def, 1); *p; p++)
  4045. if (*p == ',')
  4046. {
  4047. gen_bypass_1 (base, p - base);
  4048. do
  4049. p++;
  4050. while (ISSPACE (*p));
  4051. base = p;
  4052. }
  4053. gen_bypass_1 (base, p - base);
  4054. }
  4055. /* Find and mark all of the bypassed insns. */
  4056. static void
  4057. process_bypasses (void)
  4058. {
  4059. struct bypass_list *b;
  4060. struct insn_reserv *r;
  4061. n_bypassed = 0;
  4062. /* The reservation list is likely to be much longer than the bypass
  4063. list. */
  4064. for (r = all_insn_reservs; r; r = r->next)
  4065. for (b = all_bypasses; b; b = b->next)
  4066. if (fnmatch (b->pattern, r->name, 0) == 0)
  4067. {
  4068. n_bypassed++;
  4069. r->bypassed = true;
  4070. break;
  4071. }
  4072. }
  4073. /* Check that attribute NAME is used in define_insn_reservation condition
  4074. EXP. Return true if it is. */
  4075. static bool
  4076. check_tune_attr (const char *name, rtx exp)
  4077. {
  4078. switch (GET_CODE (exp))
  4079. {
  4080. case AND:
  4081. if (check_tune_attr (name, XEXP (exp, 0)))
  4082. return true;
  4083. return check_tune_attr (name, XEXP (exp, 1));
  4084. case IOR:
  4085. return (check_tune_attr (name, XEXP (exp, 0))
  4086. && check_tune_attr (name, XEXP (exp, 1)));
  4087. case EQ_ATTR:
  4088. return XSTR (exp, 0) == name;
  4089. default:
  4090. return false;
  4091. }
  4092. }
  4093. /* Try to find a const attribute (usually cpu or tune) that is used
  4094. in all define_insn_reservation conditions. */
  4095. static struct attr_desc *
  4096. find_tune_attr (rtx exp)
  4097. {
  4098. struct attr_desc *attr;
  4099. switch (GET_CODE (exp))
  4100. {
  4101. case AND:
  4102. case IOR:
  4103. attr = find_tune_attr (XEXP (exp, 0));
  4104. if (attr)
  4105. return attr;
  4106. return find_tune_attr (XEXP (exp, 1));
  4107. case EQ_ATTR:
  4108. if (XSTR (exp, 0) == alternative_name)
  4109. return NULL;
  4110. attr = find_attr (&XSTR (exp, 0), 0);
  4111. gcc_assert (attr);
  4112. if (attr->is_const && !attr->is_special)
  4113. {
  4114. struct insn_reserv *decl;
  4115. for (decl = all_insn_reservs; decl; decl = decl->next)
  4116. if (! check_tune_attr (attr->name, decl->condexp))
  4117. return NULL;
  4118. return attr;
  4119. }
  4120. return NULL;
  4121. default:
  4122. return NULL;
  4123. }
  4124. }
  4125. /* Create all of the attributes that describe automaton properties.
  4126. Write the DFA and latency function prototypes to the files that
  4127. need to have them, and write the init_sched_attrs(). */
  4128. static void
  4129. make_automaton_attrs (void)
  4130. {
  4131. int i;
  4132. struct insn_reserv *decl;
  4133. rtx code_exp, lats_exp, byps_exp;
  4134. struct attr_desc *tune_attr;
  4135. if (n_insn_reservs == 0)
  4136. return;
  4137. tune_attr = find_tune_attr (all_insn_reservs->condexp);
  4138. if (tune_attr != NULL)
  4139. {
  4140. rtx *condexps = XNEWVEC (rtx, n_insn_reservs * 3);
  4141. struct attr_value *val;
  4142. bool first = true;
  4143. gcc_assert (tune_attr->is_const
  4144. && !tune_attr->is_special
  4145. && !tune_attr->is_numeric);
  4146. /* Write the prototypes for all DFA functions. */
  4147. for (val = tune_attr->first_value; val; val = val->next)
  4148. {
  4149. if (val == tune_attr->default_val)
  4150. continue;
  4151. gcc_assert (GET_CODE (val->value) == CONST_STRING);
  4152. fprintf (dfa_file,
  4153. "extern int internal_dfa_insn_code_%s (rtx_insn *);\n",
  4154. XSTR (val->value, 0));
  4155. }
  4156. fprintf (dfa_file, "\n");
  4157. /* Write the prototypes for all latency functions. */
  4158. for (val = tune_attr->first_value; val; val = val->next)
  4159. {
  4160. if (val == tune_attr->default_val)
  4161. continue;
  4162. gcc_assert (GET_CODE (val->value) == CONST_STRING);
  4163. fprintf (latency_file,
  4164. "extern int insn_default_latency_%s (rtx_insn *);\n",
  4165. XSTR (val->value, 0));
  4166. }
  4167. fprintf (latency_file, "\n");
  4168. /* Write the prototypes for all automaton functions. */
  4169. for (val = tune_attr->first_value; val; val = val->next)
  4170. {
  4171. if (val == tune_attr->default_val)
  4172. continue;
  4173. gcc_assert (GET_CODE (val->value) == CONST_STRING);
  4174. fprintf (attr_file,
  4175. "extern int internal_dfa_insn_code_%s (rtx_insn *);\n"
  4176. "extern int insn_default_latency_%s (rtx_insn *);\n",
  4177. XSTR (val->value, 0), XSTR (val->value, 0));
  4178. }
  4179. fprintf (attr_file, "\n");
  4180. fprintf (attr_file, "int (*internal_dfa_insn_code) (rtx_insn *);\n");
  4181. fprintf (attr_file, "int (*insn_default_latency) (rtx_insn *);\n");
  4182. fprintf (attr_file, "\n");
  4183. fprintf (attr_file, "void\n");
  4184. fprintf (attr_file, "init_sched_attrs (void)\n");
  4185. fprintf (attr_file, "{\n");
  4186. for (val = tune_attr->first_value; val; val = val->next)
  4187. {
  4188. int j;
  4189. char *name;
  4190. rtx test = attr_rtx (EQ_ATTR, tune_attr->name, XSTR (val->value, 0));
  4191. if (val == tune_attr->default_val)
  4192. continue;
  4193. for (decl = all_insn_reservs, i = 0;
  4194. decl;
  4195. decl = decl->next)
  4196. {
  4197. rtx ctest = test;
  4198. rtx condexp
  4199. = simplify_and_tree (decl->condexp, &ctest, -2, 0);
  4200. if (condexp == false_rtx)
  4201. continue;
  4202. if (condexp == true_rtx)
  4203. break;
  4204. condexps[i] = condexp;
  4205. condexps[i + 1] = make_numeric_value (decl->insn_num);
  4206. condexps[i + 2] = make_numeric_value (decl->default_latency);
  4207. i += 3;
  4208. }
  4209. code_exp = rtx_alloc (COND);
  4210. lats_exp = rtx_alloc (COND);
  4211. j = i / 3 * 2;
  4212. XVEC (code_exp, 0) = rtvec_alloc (j);
  4213. XVEC (lats_exp, 0) = rtvec_alloc (j);
  4214. if (decl)
  4215. {
  4216. XEXP (code_exp, 1) = make_numeric_value (decl->insn_num);
  4217. XEXP (lats_exp, 1) = make_numeric_value (decl->default_latency);
  4218. }
  4219. else
  4220. {
  4221. XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
  4222. XEXP (lats_exp, 1) = make_numeric_value (0);
  4223. }
  4224. while (i > 0)
  4225. {
  4226. i -= 3;
  4227. j -= 2;
  4228. XVECEXP (code_exp, 0, j) = condexps[i];
  4229. XVECEXP (lats_exp, 0, j) = condexps[i];
  4230. XVECEXP (code_exp, 0, j + 1) = condexps[i + 1];
  4231. XVECEXP (lats_exp, 0, j + 1) = condexps[i + 2];
  4232. }
  4233. name = XNEWVEC (char,
  4234. sizeof ("*internal_dfa_insn_code_")
  4235. + strlen (XSTR (val->value, 0)));
  4236. strcpy (name, "*internal_dfa_insn_code_");
  4237. strcat (name, XSTR (val->value, 0));
  4238. make_internal_attr (name, code_exp, ATTR_NONE);
  4239. strcpy (name, "*insn_default_latency_");
  4240. strcat (name, XSTR (val->value, 0));
  4241. make_internal_attr (name, lats_exp, ATTR_NONE);
  4242. XDELETEVEC (name);
  4243. if (first)
  4244. {
  4245. fprintf (attr_file, " if (");
  4246. first = false;
  4247. }
  4248. else
  4249. fprintf (attr_file, " else if (");
  4250. write_test_expr (attr_file, test, 0, 0);
  4251. fprintf (attr_file, ")\n");
  4252. fprintf (attr_file, " {\n");
  4253. fprintf (attr_file, " internal_dfa_insn_code\n");
  4254. fprintf (attr_file, " = internal_dfa_insn_code_%s;\n",
  4255. XSTR (val->value, 0));
  4256. fprintf (attr_file, " insn_default_latency\n");
  4257. fprintf (attr_file, " = insn_default_latency_%s;\n",
  4258. XSTR (val->value, 0));
  4259. fprintf (attr_file, " }\n");
  4260. }
  4261. fprintf (attr_file, " else\n");
  4262. fprintf (attr_file, " gcc_unreachable ();\n");
  4263. fprintf (attr_file, "}\n");
  4264. fprintf (attr_file, "\n");
  4265. XDELETEVEC (condexps);
  4266. }
  4267. else
  4268. {
  4269. code_exp = rtx_alloc (COND);
  4270. lats_exp = rtx_alloc (COND);
  4271. XVEC (code_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
  4272. XVEC (lats_exp, 0) = rtvec_alloc (n_insn_reservs * 2);
  4273. XEXP (code_exp, 1) = make_numeric_value (n_insn_reservs + 1);
  4274. XEXP (lats_exp, 1) = make_numeric_value (0);
  4275. for (decl = all_insn_reservs, i = 0;
  4276. decl;
  4277. decl = decl->next, i += 2)
  4278. {
  4279. XVECEXP (code_exp, 0, i) = decl->condexp;
  4280. XVECEXP (lats_exp, 0, i) = decl->condexp;
  4281. XVECEXP (code_exp, 0, i+1) = make_numeric_value (decl->insn_num);
  4282. XVECEXP (lats_exp, 0, i+1)
  4283. = make_numeric_value (decl->default_latency);
  4284. }
  4285. make_internal_attr ("*internal_dfa_insn_code", code_exp, ATTR_NONE);
  4286. make_internal_attr ("*insn_default_latency", lats_exp, ATTR_NONE);
  4287. }
  4288. if (n_bypasses == 0)
  4289. byps_exp = make_numeric_value (0);
  4290. else
  4291. {
  4292. process_bypasses ();
  4293. byps_exp = rtx_alloc (COND);
  4294. XVEC (byps_exp, 0) = rtvec_alloc (n_bypassed * 2);
  4295. XEXP (byps_exp, 1) = make_numeric_value (0);
  4296. for (decl = all_insn_reservs, i = 0;
  4297. decl;
  4298. decl = decl->next)
  4299. if (decl->bypassed)
  4300. {
  4301. XVECEXP (byps_exp, 0, i) = decl->condexp;
  4302. XVECEXP (byps_exp, 0, i+1) = make_numeric_value (1);
  4303. i += 2;
  4304. }
  4305. }
  4306. make_internal_attr ("*bypass_p", byps_exp, ATTR_NONE);
  4307. }
  4308. static void
  4309. write_header (FILE *outf)
  4310. {
  4311. fprintf (outf, "/* Generated automatically by the program `genattrtab'\n"
  4312. " from the machine description file `md'. */\n\n");
  4313. fprintf (outf, "#include \"config.h\"\n");
  4314. fprintf (outf, "#include \"system.h\"\n");
  4315. fprintf (outf, "#include \"coretypes.h\"\n");
  4316. fprintf (outf, "#include \"tm.h\"\n");
  4317. fprintf (outf, "#include \"hash-set.h\"\n");
  4318. fprintf (outf, "#include \"machmode.h\"\n");
  4319. fprintf (outf, "#include \"vec.h\"\n");
  4320. fprintf (outf, "#include \"double-int.h\"\n");
  4321. fprintf (outf, "#include \"input.h\"\n");
  4322. fprintf (outf, "#include \"alias.h\"\n");
  4323. fprintf (outf, "#include \"symtab.h\"\n");
  4324. fprintf (outf, "#include \"options.h\"\n");
  4325. fprintf (outf, "#include \"wide-int.h\"\n");
  4326. fprintf (outf, "#include \"inchash.h\"\n");
  4327. fprintf (outf, "#include \"tree.h\"\n");
  4328. fprintf (outf, "#include \"varasm.h\"\n");
  4329. fprintf (outf, "#include \"stor-layout.h\"\n");
  4330. fprintf (outf, "#include \"calls.h\"\n");
  4331. fprintf (outf, "#include \"rtl.h\"\n");
  4332. fprintf (outf, "#include \"insn-attr.h\"\n");
  4333. fprintf (outf, "#include \"tm_p.h\"\n");
  4334. fprintf (outf, "#include \"insn-config.h\"\n");
  4335. fprintf (outf, "#include \"recog.h\"\n");
  4336. fprintf (outf, "#include \"regs.h\"\n");
  4337. fprintf (outf, "#include \"real.h\"\n");
  4338. fprintf (outf, "#include \"output.h\"\n");
  4339. fprintf (outf, "#include \"toplev.h\"\n");
  4340. fprintf (outf, "#include \"flags.h\"\n");
  4341. fprintf (outf, "#include \"function.h\"\n");
  4342. fprintf (outf, "#include \"predict.h\"\n");
  4343. fprintf (outf, "\n");
  4344. fprintf (outf, "#define operands recog_data.operand\n\n");
  4345. }
  4346. static FILE *
  4347. open_outfile (const char *file_name)
  4348. {
  4349. FILE *outf;
  4350. outf = fopen (file_name, "w");
  4351. if (! outf)
  4352. fatal ("cannot open file %s: %s", file_name, xstrerror (errno));
  4353. write_header (outf);
  4354. return outf;
  4355. }
  4356. static bool
  4357. handle_arg (const char *arg)
  4358. {
  4359. switch (arg[1])
  4360. {
  4361. case 'A':
  4362. attr_file_name = &arg[2];
  4363. return true;
  4364. case 'D':
  4365. dfa_file_name = &arg[2];
  4366. return true;
  4367. case 'L':
  4368. latency_file_name = &arg[2];
  4369. return true;
  4370. default:
  4371. return false;
  4372. }
  4373. }
  4374. int
  4375. main (int argc, char **argv)
  4376. {
  4377. rtx desc;
  4378. struct attr_desc *attr;
  4379. struct insn_def *id;
  4380. rtx tem;
  4381. int i;
  4382. progname = "genattrtab";
  4383. if (!init_rtx_reader_args_cb (argc, argv, handle_arg))
  4384. return FATAL_EXIT_CODE;
  4385. attr_file = open_outfile (attr_file_name);
  4386. dfa_file = open_outfile (dfa_file_name);
  4387. latency_file = open_outfile (latency_file_name);
  4388. obstack_init (hash_obstack);
  4389. obstack_init (temp_obstack);
  4390. /* Set up true and false rtx's */
  4391. true_rtx = rtx_alloc (CONST_INT);
  4392. XWINT (true_rtx, 0) = 1;
  4393. false_rtx = rtx_alloc (CONST_INT);
  4394. XWINT (false_rtx, 0) = 0;
  4395. ATTR_IND_SIMPLIFIED_P (true_rtx) = ATTR_IND_SIMPLIFIED_P (false_rtx) = 1;
  4396. ATTR_PERMANENT_P (true_rtx) = ATTR_PERMANENT_P (false_rtx) = 1;
  4397. alternative_name = DEF_ATTR_STRING ("alternative");
  4398. length_str = DEF_ATTR_STRING ("length");
  4399. delay_type_str = DEF_ATTR_STRING ("*delay_type");
  4400. delay_1_0_str = DEF_ATTR_STRING ("*delay_1_0");
  4401. num_delay_slots_str = DEF_ATTR_STRING ("*num_delay_slots");
  4402. /* Read the machine description. */
  4403. while (1)
  4404. {
  4405. int lineno;
  4406. desc = read_md_rtx (&lineno, &insn_code_number);
  4407. if (desc == NULL)
  4408. break;
  4409. switch (GET_CODE (desc))
  4410. {
  4411. case DEFINE_INSN:
  4412. case DEFINE_PEEPHOLE:
  4413. case DEFINE_ASM_ATTRIBUTES:
  4414. gen_insn (desc, lineno);
  4415. break;
  4416. case DEFINE_ATTR:
  4417. case DEFINE_ENUM_ATTR:
  4418. gen_attr (desc, lineno);
  4419. break;
  4420. case DEFINE_DELAY:
  4421. gen_delay (desc, lineno);
  4422. break;
  4423. case DEFINE_INSN_RESERVATION:
  4424. gen_insn_reserv (desc);
  4425. break;
  4426. case DEFINE_BYPASS:
  4427. gen_bypass (desc);
  4428. break;
  4429. default:
  4430. break;
  4431. }
  4432. if (GET_CODE (desc) != DEFINE_ASM_ATTRIBUTES)
  4433. insn_index_number++;
  4434. }
  4435. if (have_error)
  4436. return FATAL_EXIT_CODE;
  4437. insn_code_number++;
  4438. /* If we didn't have a DEFINE_ASM_ATTRIBUTES, make a null one. */
  4439. if (! got_define_asm_attributes)
  4440. {
  4441. tem = rtx_alloc (DEFINE_ASM_ATTRIBUTES);
  4442. XVEC (tem, 0) = rtvec_alloc (0);
  4443. gen_insn (tem, 0);
  4444. }
  4445. /* Expand DEFINE_DELAY information into new attribute. */
  4446. if (num_delays)
  4447. expand_delays ();
  4448. /* Make `insn_alternatives'. */
  4449. insn_alternatives = oballocvec (uint64_t, insn_code_number);
  4450. for (id = defs; id; id = id->next)
  4451. if (id->insn_code >= 0)
  4452. insn_alternatives[id->insn_code]
  4453. = (((uint64_t) 1) << id->num_alternatives) - 1;
  4454. /* Make `insn_n_alternatives'. */
  4455. insn_n_alternatives = oballocvec (int, insn_code_number);
  4456. for (id = defs; id; id = id->next)
  4457. if (id->insn_code >= 0)
  4458. insn_n_alternatives[id->insn_code] = id->num_alternatives;
  4459. /* Construct extra attributes for automata. */
  4460. make_automaton_attrs ();
  4461. /* Prepare to write out attribute subroutines by checking everything stored
  4462. away and building the attribute cases. */
  4463. check_defs ();
  4464. for (i = 0; i < MAX_ATTRS_INDEX; i++)
  4465. for (attr = attrs[i]; attr; attr = attr->next)
  4466. attr->default_val->value
  4467. = check_attr_value (attr->default_val->value, attr);
  4468. if (have_error)
  4469. return FATAL_EXIT_CODE;
  4470. for (i = 0; i < MAX_ATTRS_INDEX; i++)
  4471. for (attr = attrs[i]; attr; attr = attr->next)
  4472. fill_attr (attr);
  4473. /* Construct extra attributes for `length'. */
  4474. make_length_attrs ();
  4475. /* Perform any possible optimizations to speed up compilation. */
  4476. optimize_attrs ();
  4477. /* Now write out all the `gen_attr_...' routines. Do these before the
  4478. special routines so that they get defined before they are used. */
  4479. for (i = 0; i < MAX_ATTRS_INDEX; i++)
  4480. for (attr = attrs[i]; attr; attr = attr->next)
  4481. {
  4482. FILE *outf;
  4483. #define IS_ATTR_GROUP(X) (!strncmp (attr->name, X, strlen (X)))
  4484. if (IS_ATTR_GROUP ("*internal_dfa_insn_code"))
  4485. outf = dfa_file;
  4486. else if (IS_ATTR_GROUP ("*insn_default_latency"))
  4487. outf = latency_file;
  4488. else
  4489. outf = attr_file;
  4490. #undef IS_ATTR_GROUP
  4491. if (! attr->is_special && ! attr->is_const)
  4492. write_attr_get (outf, attr);
  4493. }
  4494. /* Write out delay eligibility information, if DEFINE_DELAY present.
  4495. (The function to compute the number of delay slots will be written
  4496. below.) */
  4497. if (num_delays)
  4498. {
  4499. write_eligible_delay (attr_file, "delay");
  4500. if (have_annul_true)
  4501. write_eligible_delay (attr_file, "annul_true");
  4502. if (have_annul_false)
  4503. write_eligible_delay (attr_file, "annul_false");
  4504. }
  4505. /* Write out constant delay slot info. */
  4506. write_const_num_delay_slots (attr_file);
  4507. write_length_unit_log (attr_file);
  4508. if (fclose (attr_file) != 0)
  4509. fatal ("cannot close file %s: %s", attr_file_name, xstrerror (errno));
  4510. if (fclose (dfa_file) != 0)
  4511. fatal ("cannot close file %s: %s", dfa_file_name, xstrerror (errno));
  4512. if (fclose (latency_file) != 0)
  4513. fatal ("cannot close file %s: %s", latency_file_name, xstrerror (errno));
  4514. return SUCCESS_EXIT_CODE;
  4515. }