cse.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249
  1. /* Common subexpression elimination for GNU compiler.
  2. Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include "config.h"
  18. #include "rtl.h"
  19. #include "insn-config.h"
  20. #include "regs.h"
  21. #include "hard-reg-set.h"
  22. /* The basic idea of common subexpression elimination is to go
  23. through the code, keeping a record of expressions that would
  24. have the same value at the current scan point, and replacing
  25. expressions encountered with the cheapest equivalent expression.
  26. It is too complicated to keep track of the different possibilities
  27. when control paths merge; so, at each label, we forget all that is
  28. known and start fresh. This can be described as processing each
  29. basic block separately. Note, however, that these are not quite
  30. the same as the basic blocks found by a later pass and used for
  31. data flow analysis and register packing. We do not need to start fresh
  32. after a conditional jump instruction if there is no label there.
  33. We use two data structures to record the equivalent expressions:
  34. a hash table for most expressions, and several vectors together
  35. with "quantity numbers" to record equivalent (pseudo) registers.
  36. The use of the special data structure for registers is desirable
  37. because it is faster. It is possible because registers references
  38. contain a fairly small number, the register number, taken from
  39. a contiguously allocated series, and two register references are
  40. identical if they have the same number. General expressions
  41. do not have any such thing, so the only way to retrieve the
  42. information recorded on an expression other than a register
  43. is to keep it in a hash table.
  44. Registers and "quantity numbers":
  45. At the start of each basic block, all of the (hardware and pseudo)
  46. registers used in the function are given distinct quantity
  47. numbers to indicate their contents. During scan, when the code
  48. copies one register into another, we copy the quantity number.
  49. When a register is loaded in any other way, we allocate a new
  50. quantity number to describe the value generated by this operation.
  51. `reg_qty' records what quantity a register is currently thought
  52. of as containing.
  53. We also maintain a bidirectional chain of registers for each
  54. quantity number. `qty_first_reg', `qty_last_reg',
  55. `reg_next_eqv' and `reg_prev_eqv' hold these chains.
  56. The first register in a chain is the one whose lifespan is least local.
  57. Among equals, it is the one that was seen first.
  58. We replace any equivalent register with that one.
  59. Constants and quantity numbers
  60. When a quantity has a known constant value, that value is stored
  61. in the appropriate element of qty_const. This is in addition to
  62. putting the constant in the hash table as is usual for non-regs.
  63. Regs are preferred to constants as they are to everything else,
  64. but expressions containing constants can be simplified, by fold_rtx.
  65. When a quantity has a known nearly constant value (such as an address
  66. of a stack slot), that value is stored in the appropriate element
  67. of qty_const.
  68. Integer constants don't have a machine mode. However, cse
  69. determines the intended machine mode from the destination
  70. of the instruction that moves the constant. The machine mode
  71. is recorded in the hash table along with the actual RTL
  72. constant expression so that different modes are kept separate.
  73. Other expressions:
  74. To record known equivalences among expressions in general
  75. we use a hash table called `table'. It has a fixed number of buckets
  76. that contain chains of `struct table_elt' elements for expressions.
  77. These chains connect the elements whose expressions have the same
  78. hash codes.
  79. Other chains through the same elements connect the elements which
  80. currently have equivalent values.
  81. Register references in an expression are canonicalized before hashing
  82. the expression. This is done using `reg_qty' and `qty_first_reg'.
  83. The hash code of a register reference is computed using the quantity
  84. number, not the register number.
  85. When the value of an expression changes, it is necessary to remove from the
  86. hash table not just that expression but all expressions whose values
  87. could be different as a result.
  88. 1. If the value changing is in memory, except in special cases
  89. ANYTHING referring to memory could be changed. That is because
  90. nobody knows where a pointer does not point.
  91. The function `invalidate_memory' removes what is necessary.
  92. The special cases are when the address is constant or is
  93. a constant plus a fixed register such as the frame pointer
  94. or a static chain pointer. When such addresses are stored in,
  95. we can tell exactly which other such addresses must be invalidated
  96. due to overlap. `invalidate' does this.
  97. All expressions that refer to non-constant
  98. memory addresses are also invalidated. `invalidate_memory' does this.
  99. 2. If the value changing is a register, all expressions
  100. containing references to that register, and only those,
  101. must be removed.
  102. Because searching the entire hash table for expressions that contain
  103. a register is very slow, we try to figure out when it isn't necessary.
  104. Precisely, this is necessary only when expressions have been
  105. entered in the hash table using this register, and then the value has
  106. changed, and then another expression wants to be added to refer to
  107. teh register's new value. This sequence of circumstances is rare
  108. within any one basic block.
  109. The vectors `reg_tick' and `reg_in_table' are used to detect this case.
  110. reg_tick[i] is incremented whenever a value is stored in register i.
  111. reg_in_table[i] holds -1 if no references to register i have been
  112. entered in the table; otherwise, it contains the value reg_tick[i] had
  113. when the references were entered. If we want to enter a reference
  114. and reg_in_table[i] != reg_tick[i], we must scan and remove old references.
  115. Until we want to enter a new entry, the mere fact that the twovectors
  116. don't match makes the entries be ignored if anyone tries to match them.
  117. Registers themselves are entered in the hash table as well as in
  118. the equivalent-register chains. However, the vectors `reg_tick'
  119. and `reg_in_table' do not apply to expressions which are simple
  120. register references. These expressions are removed from the table
  121. immediately when they become invalid, and this can be done even if
  122. we do not immediately search for all the expressions that refer to
  123. the register.
  124. A CLOBBER rtx in an instruction invalidates its operand for further
  125. reuse. A CLOBBER or SET rtx whose operand is a MEM:BLK
  126. invalidates everything that resides in memory.
  127. Related expressions:
  128. Constant expressions that differ only by an additive integer
  129. are called related. When a constant expression is put in
  130. the table, the related expression with no constant term
  131. is also entered. These are made to point at each other
  132. so that it is possible to find out if there exists any
  133. register equivalent to an expression related to a given expression. */
  134. /* One plus largest register number used in this function. */
  135. static int max_reg;
  136. /* Length of vectors indexed by quantity number.
  137. We know in advance we will not need a quantity number this big. */
  138. static int max_qty;
  139. /* Next quantity number to be allocated.
  140. This is 1 + the largest number needed so far. */
  141. static int next_qty;
  142. /* Indexed by quantity number, gives the first (or last) (pseudo) register
  143. in the chain of registers that currently contain this quantity. */
  144. static int *qty_first_reg;
  145. static int *qty_last_reg;
  146. /* Indexed by quantity number, gives the rtx of the constant value of the
  147. quantity, or zero if it does not have a known value.
  148. A sum of the frame pointer (or arg pointer) plus a constant
  149. can also be entered here. */
  150. static rtx *qty_const;
  151. /* Indexed by qty number, gives the insn that stored the constant value
  152. recorded in `qty_const'. */
  153. static rtx *qty_const_insn;
  154. /* Value stored in CC0 by previous insn:
  155. 0 if previous insn didn't store in CC0.
  156. else 0100 + (M&7)<<3 + (N&7)
  157. where M is 1, 0 or -1 if result was >, == or < as signed number
  158. and N is 1, 0 or -1 if result was >, == or < as unsigned number. */
  159. static int prev_insn_cc0;
  160. /* Previous actual insn. 0 if at first insn of basic block. */
  161. static rtx prev_insn;
  162. /* Insn being scanned. */
  163. static rtx this_insn;
  164. /* Index by (pseudo) register number, gives the quantity number
  165. of the register's current contents. */
  166. static int *reg_qty;
  167. /* Index by (pseudo) register number, gives the number of the next
  168. (pseudo) register in the chain of registers sharing the same value.
  169. Or -1 if this register is at the end of the chain. */
  170. static int *reg_next_eqv;
  171. /* Index by (pseudo) register number, gives the number of the previous
  172. (pseudo) register in the chain of registers sharing the same value.
  173. Or -1 if this register is at the beginning of the chain. */
  174. static int *reg_prev_eqv;
  175. /* Index by (pseudo) register number, gives the latest rtx
  176. to use to insert a ref to that register. */
  177. static rtx *reg_rtx;
  178. /* Index by (pseudo) register number, gives the number of times
  179. that register has been altered in the current basic block. */
  180. static int *reg_tick;
  181. /* Index by (pseudo) register number, gives the reg_tick value at which
  182. rtx's containing this register are valid in the hash table.
  183. If this does not equal the current reg_tick value, such expressions
  184. existing in the hash table are invalid.
  185. If this is -1, no expressions containing this register have been
  186. entered in the table. */
  187. static int *reg_in_table;
  188. /* Two vectors of max_reg ints:
  189. one containing all -1's; in the other, element i contains i.
  190. These are used to initialize various other vectors fast. */
  191. static int *all_minus_one;
  192. static int *consec_ints;
  193. /* UID of insn that starts the basic block currently being cse-processed. */
  194. static int cse_basic_block_start;
  195. /* UID of insn that ends the basic block currently being cse-processed. */
  196. static int cse_basic_block_end;
  197. /* Nonzero if cse has altered conditional jump insns
  198. in such a way that jump optimization should be redone. */
  199. static int cse_jumps_altered;
  200. /* canon_hash stores 1 in do_not_record
  201. if it notices a reference to CC0, CC1 or PC. */
  202. static int do_not_record;
  203. /* canon_hash stores 1 in hash_arg_in_memory
  204. if it notices a reference to memory within the expression being hashed. */
  205. static int hash_arg_in_memory;
  206. /* canon_hash stores 1 in hash_arg_in_struct
  207. if it notices a reference to memory that's part of a structure. */
  208. static int hash_arg_in_struct;
  209. /* The hash table contains buckets which are chains of `struct table_elt's,
  210. each recording one expression's information.
  211. That expression is in the `exp' field.
  212. Those elements with the same hash code are chained in both directions
  213. through the `next_same_hash' and `prev_same_hash' fields.
  214. Each set of expressions with equivalent values
  215. are on a two-way chain through the `next_same_value'
  216. and `prev_same_value' fields, and all point with
  217. the `first_same_value' field at the first element in
  218. that chain. The chain is in order of increasing cost.
  219. Each element's cost value is in its `cost' field.
  220. The `in_memory' field is nonzero for elements that
  221. involve any reference to memory. These elements are removed
  222. whenever a write is done to an unidentified location in memory.
  223. To be safe, we assume that a memory address is unidentified unless
  224. the address is either a symbol constant or a constant plus
  225. the frame pointer or argument pointer.
  226. The `in_struct' field is nonzero for elements that
  227. involve any reference to memory inside a structure or array.
  228. The `equivalence_only' field means that this expression came from a
  229. REG_EQUIV or REG_EQUAL note; it is not valid for substitution into an insn.
  230. The `related_value' field is used to connect related expressions
  231. (that differ by adding an integer).
  232. The related expressions are chained in a circular fashion.
  233. `related_value' is zero for expressions for which this
  234. chain is not useful.
  235. The `mode' field is usually the same as GET_MODE (`exp'), but
  236. if `exp' is a CONST_INT and has no machine mode then the `mode'
  237. field is the mode it was being used as. Each constant is
  238. recorded separately for each mode it is used with. */
  239. struct table_elt
  240. {
  241. rtx exp;
  242. struct table_elt *next_same_hash;
  243. struct table_elt *prev_same_hash;
  244. struct table_elt *next_same_value;
  245. struct table_elt *prev_same_value;
  246. struct table_elt *first_same_value;
  247. struct table_elt *related_value;
  248. int cost;
  249. enum machine_mode mode;
  250. char in_memory;
  251. char in_struct;
  252. char equivalence_only;
  253. };
  254. #define HASH(x, m) (canon_hash (x, m) % NBUCKETS)
  255. /* We don't want a lot of buckets, because we rarely have very many
  256. things stored in the hash table, and a lot of buckets slows
  257. down a lot of loops that happen frequently. */
  258. #define NBUCKETS 31
  259. static struct table_elt *table[NBUCKETS];
  260. /* Chain of `struct table_elt's made so far for this function
  261. but currently removed from the table. */
  262. static struct table_elt *free_element_chain;
  263. /* Number of `struct table_elt' structures made so far for this function. */
  264. static int n_elements_made;
  265. /* Maximum value `n_elements_made' has had so far in this compilation
  266. for functions previously processed. */
  267. static int max_elements_made;
  268. /* Bits describing what kind of values in memory must be invalidated
  269. for a particular instruction. If all three bits are zero,
  270. no memory refs need to be invalidated. Each bit is more powerful
  271. than the preceding ones, and if a bit is set then the preceding
  272. bits are also set.
  273. Here is how the bits are set.
  274. Writing at a fixed address invalidates only variable addresses,
  275. writing in a structure element at variable address
  276. invalidates all but scalar variables,
  277. and writing in anything else at variable address invalidates everything. */
  278. struct write_data
  279. {
  280. int var : 1; /* Invalidate variable addresses. */
  281. int nonscalar : 1; /* Invalidate all but scalar variables. */
  282. int all : 1; /* Invalidate all memory refs. */
  283. };
  284. /* Nonzero if X has the form (PLUS frame-pointer integer). */
  285. #define FIXED_BASE_PLUS_P(X) \
  286. (GET_CODE (X) == PLUS && GET_CODE (XEXP (X, 1)) == CONST_INT \
  287. && (XEXP (X, 0) == frame_pointer_rtx || XEXP (X, 0) == arg_pointer_rtx))
  288. static struct table_elt *lookup ();
  289. static void free_element ();
  290. static void remove_invalid_refs ();
  291. static int exp_equiv_p ();
  292. int refers_to_p ();
  293. int refers_to_mem_p ();
  294. static void invalidate_from_clobbers ();
  295. static int safe_hash ();
  296. static int get_integer_term ();
  297. static rtx get_related_value ();
  298. static void note_mem_written ();
  299. static int cse_rtx_addr_varies_p ();
  300. /* Return an estimate of the cost of computing rtx X.
  301. The only use of this is to compare the costs of two expressions
  302. to decide whether to replace one with the other. */
  303. static int
  304. rtx_cost (x)
  305. rtx x;
  306. {
  307. register int i, j;
  308. register RTX_CODE code = GET_CODE (x);
  309. register char *fmt;
  310. register int total;
  311. switch (code)
  312. {
  313. case REG:
  314. return 1;
  315. case SUBREG:
  316. return 2;
  317. CONST_COSTS (x, code);
  318. }
  319. total = 2;
  320. /* Sum the costs of the sub-rtx's, plus 2 just put in. */
  321. fmt = GET_RTX_FORMAT (code);
  322. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  323. if (fmt[i] == 'e')
  324. total += rtx_cost (XEXP (x, i));
  325. else if (fmt[i] == 'E')
  326. for (j = 0; j < XVECLEN (x, i); j++)
  327. total += rtx_cost (XVECEXP (x, i, j));
  328. return total;
  329. }
  330. /* Clear the hash table and initialize each register with its own quantity,
  331. for a new basic block. */
  332. static void
  333. new_basic_block ()
  334. {
  335. register int i;
  336. register int vecsize = max_reg * sizeof (rtx);
  337. next_qty = max_reg;
  338. bzero (reg_rtx, vecsize);
  339. bzero (reg_tick, vecsize);
  340. bcopy (all_minus_one, reg_in_table, vecsize);
  341. bcopy (all_minus_one, reg_next_eqv, vecsize);
  342. bcopy (all_minus_one, reg_prev_eqv, vecsize);
  343. bcopy (consec_ints, reg_qty, vecsize);
  344. for (i = 0; i < max_qty; i++)
  345. {
  346. qty_first_reg[i] = i;
  347. qty_last_reg[i] = i;
  348. qty_const[i] = 0;
  349. qty_const_insn[i] = 0;
  350. }
  351. for (i = 0; i < NBUCKETS; i++)
  352. {
  353. register struct table_elt *this, *next;
  354. for (this = table[i]; this; this = next)
  355. {
  356. next = this->next_same_hash;
  357. free_element (this);
  358. }
  359. }
  360. bzero (table, sizeof table);
  361. prev_insn_cc0 = 0;
  362. prev_insn = 0;
  363. }
  364. /* Say that register REG contains a quantity not in any register before. */
  365. static void
  366. make_new_qty (reg)
  367. register int reg;
  368. {
  369. register int q;
  370. q = reg_qty[reg] = next_qty++;
  371. qty_first_reg[q] = reg;
  372. qty_last_reg[q] = reg;
  373. }
  374. /* Make reg NEW equivalent to reg OLD.
  375. OLD is not changing; NEW is. */
  376. static void
  377. make_regs_eqv (new, old)
  378. register int new, old;
  379. {
  380. register int lastr, firstr;
  381. register int q = reg_qty[old];
  382. /* Nothing should become eqv until it has a "non-invalid" qty number. */
  383. if (q == old)
  384. abort ();
  385. reg_qty[new] = q;
  386. firstr = qty_first_reg[q];
  387. lastr = qty_last_reg[q];
  388. /* Prefer pseudo regs to hard regs with the same value.
  389. Among pseudos, if NEW will live longer than any other reg of the same qty,
  390. and that is beyond the current basic block,
  391. make it the new canonical replacement for this qty. */
  392. if (new >= FIRST_PSEUDO_REGISTER
  393. && (firstr < FIRST_PSEUDO_REGISTER
  394. || ((regno_last_uid[new] > cse_basic_block_end
  395. || regno_first_uid[new] < cse_basic_block_start)
  396. && regno_last_uid[new] > regno_last_uid[firstr])))
  397. {
  398. reg_prev_eqv[firstr] = new;
  399. reg_next_eqv[new] = firstr;
  400. reg_prev_eqv[new] = -1;
  401. qty_first_reg[q] = new;
  402. }
  403. else
  404. {
  405. /* If NEW is a hard reg, insert at end.
  406. Otherwise, insert before any hard regs that are at the end. */
  407. while (lastr < FIRST_PSEUDO_REGISTER && new >= FIRST_PSEUDO_REGISTER)
  408. lastr = reg_prev_eqv[lastr];
  409. reg_next_eqv[new] = reg_next_eqv[lastr];
  410. if (reg_next_eqv[lastr] >= 0)
  411. reg_prev_eqv[reg_next_eqv[lastr]] = new;
  412. else
  413. qty_last_reg[q] = new;
  414. reg_next_eqv[lastr] = new;
  415. reg_prev_eqv[new] = lastr;
  416. }
  417. }
  418. /* Discard the records of what is in register REG. */
  419. static void
  420. reg_invalidate (reg)
  421. register int reg;
  422. {
  423. register int n = reg_next_eqv[reg];
  424. register int p = reg_prev_eqv[reg];
  425. register int q = reg_qty[reg];
  426. reg_tick[reg]++;
  427. if (q == reg)
  428. {
  429. /* Save time if already invalid */
  430. /* It shouldn't be linked to anything if it's invalid. */
  431. if (reg_prev_eqv[q] != -1)
  432. abort ();
  433. if (reg_next_eqv[q] != -1)
  434. abort ();
  435. return;
  436. }
  437. if (n != -1)
  438. reg_prev_eqv[n] = p;
  439. else
  440. qty_last_reg[q] = p;
  441. if (p != -1)
  442. reg_next_eqv[p] = n;
  443. else
  444. qty_first_reg[q] = n;
  445. reg_qty[reg] = reg;
  446. qty_first_reg[reg] = reg;
  447. qty_last_reg[reg] = reg;
  448. reg_next_eqv[reg] = -1;
  449. reg_prev_eqv[reg] = -1;
  450. }
  451. /* Remove any invalid expressions from the hash table
  452. that refer to any of the registers contained in expression X.
  453. Make sure that newly inserted references to those registers
  454. as subexpressions will be considered valid.
  455. mention_regs is not called when a register itself
  456. is being stored in the table. */
  457. static void
  458. mention_regs (x)
  459. rtx x;
  460. {
  461. register RTX_CODE code = GET_CODE (x);
  462. register int i, j;
  463. register char *fmt;
  464. if (code == REG)
  465. {
  466. register int regno = REGNO (x);
  467. reg_rtx[regno] = x;
  468. if (reg_in_table[regno] >= 0 && reg_in_table[regno] != reg_tick[regno])
  469. remove_invalid_refs (regno);
  470. reg_in_table[regno] = reg_tick[regno];
  471. return;
  472. }
  473. fmt = GET_RTX_FORMAT (code);
  474. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  475. if (fmt[i] == 'e')
  476. mention_regs (XEXP (x, i));
  477. else if (fmt[i] == 'E')
  478. for (j = 0; j < XVECLEN (x, i); j++)
  479. mention_regs (XVECEXP (x, i, j));
  480. }
  481. /* Update the register quantities for inserting X into the hash table
  482. with a value equivalent to CLASSP.
  483. (If CLASSP is not a REG or a SUBREG, it is irrelevant.)
  484. If MODIFIED is nonzero, X is a destination; it is being modified.
  485. Note that reg_invalidate should be called on a register
  486. before insert_regs is done on that register with MODIFIED != 0.
  487. Nonzero value means that elements of reg_qty have changed
  488. so X's hash code may be different. */
  489. static int
  490. insert_regs (x, classp, modified)
  491. rtx x;
  492. struct table_elt *classp;
  493. int modified;
  494. {
  495. if (GET_CODE (x) == REG)
  496. {
  497. register int regno = REGNO (x);
  498. reg_rtx[regno] = x;
  499. if (modified || reg_qty[regno] == regno)
  500. {
  501. if (classp && GET_CODE (classp->exp) == REG)
  502. {
  503. make_regs_eqv (regno, REGNO (classp->exp));
  504. /* Make sure reg_rtx is set up even for regs
  505. not explicitly set (such as function value). */
  506. reg_rtx[REGNO (classp->exp)] = classp->exp;
  507. }
  508. else
  509. make_new_qty (regno);
  510. return 1;
  511. }
  512. }
  513. /* Copying a subreg into a subreg makes the regs equivalent,
  514. but only if the entire regs' mode is within one word.
  515. Copying one reg of a DImode into one reg of another DImode
  516. does not make them equivalent. */
  517. else if (GET_CODE (x) == SUBREG
  518. && GET_CODE (SUBREG_REG (x)) == REG
  519. && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
  520. && (modified
  521. || reg_qty[REGNO (SUBREG_REG (x))] == REGNO (SUBREG_REG (x))))
  522. {
  523. if (classp && GET_CODE (classp->exp) == SUBREG
  524. && GET_CODE (SUBREG_REG (classp->exp)) == REG
  525. && GET_MODE (SUBREG_REG (classp->exp)) == GET_MODE (SUBREG_REG (x)))
  526. {
  527. int oregno = REGNO (SUBREG_REG (classp->exp));
  528. make_regs_eqv (REGNO (SUBREG_REG (x)), oregno);
  529. /* Make sure reg_rtx is set up even for regs
  530. not explicitly set (such as function value). */
  531. reg_rtx[oregno] = SUBREG_REG (classp->exp);
  532. }
  533. else
  534. make_new_qty (REGNO (SUBREG_REG (x)));
  535. return 1;
  536. }
  537. else
  538. mention_regs (x);
  539. return 0;
  540. }
  541. /* Look in or update the hash table. */
  542. /* Put the element ELT on the list of free elements. */
  543. static void
  544. free_element (elt)
  545. struct table_elt *elt;
  546. {
  547. elt->next_same_hash = free_element_chain;
  548. free_element_chain = elt;
  549. }
  550. /* Return an element that is free for use. */
  551. static struct table_elt *
  552. get_element ()
  553. {
  554. struct table_elt *elt = free_element_chain;
  555. if (elt)
  556. {
  557. free_element_chain = elt->next_same_hash;
  558. return elt;
  559. }
  560. n_elements_made++;
  561. return (struct table_elt *) oballoc (sizeof (struct table_elt));
  562. }
  563. /* Remove table element ELT from use in the table.
  564. HASH is its hash code, made using the HASH macro.
  565. It's an argument because often that is known in advance
  566. and we save much time not recomputing it. */
  567. static void
  568. remove (elt, hash)
  569. register struct table_elt *elt;
  570. int hash;
  571. {
  572. if (elt == 0)
  573. return;
  574. /* Mark this element as removed. See cse_insn. */
  575. elt->first_same_value = 0;
  576. /* Remove the table element from its equivalence class. */
  577. {
  578. register struct table_elt *prev = elt->prev_same_value;
  579. register struct table_elt *next = elt->next_same_value;
  580. if (next) next->prev_same_value = prev;
  581. if (prev)
  582. prev->next_same_value = next;
  583. else
  584. {
  585. register struct table_elt *newfirst = next;
  586. while (next)
  587. {
  588. next->first_same_value = newfirst;
  589. next = next->next_same_value;
  590. }
  591. }
  592. }
  593. /* Remove the table element from its hash bucket. */
  594. {
  595. register struct table_elt *prev = elt->prev_same_hash;
  596. register struct table_elt *next = elt->next_same_hash;
  597. if (next) next->prev_same_hash = prev;
  598. if (prev)
  599. prev->next_same_hash = next;
  600. else
  601. table[hash] = next;
  602. }
  603. /* Remove the table element from its related-value circular chain. */
  604. if (elt->related_value != 0 && elt->related_value != elt)
  605. {
  606. register struct table_elt *p = elt->related_value;
  607. while (p->related_value != elt)
  608. p = p->related_value;
  609. p->related_value = elt->related_value;
  610. if (p->related_value == p)
  611. p->related_value = 0;
  612. }
  613. free_element (elt);
  614. }
  615. /* Look up X in the hash table and return its table element,
  616. or 0 if X is not in the table.
  617. MODE is the machine-mode of X, or if X is an integer constant
  618. with VOIDmode then MODE is the mode with which X will be used.
  619. Here we are satisfied to find an expression whose tree structure
  620. looks like X. */
  621. static struct table_elt *
  622. lookup (x, hash, mode)
  623. rtx x;
  624. int hash;
  625. enum machine_mode mode;
  626. {
  627. register struct table_elt *p;
  628. for (p = table[hash]; p; p = p->next_same_hash)
  629. if (mode == p->mode && (x == p->exp || exp_equiv_p (x, p->exp, 1)))
  630. return p;
  631. return 0;
  632. }
  633. /* Like `lookup' but don't care whether the table element uses invalid regs.
  634. Also ignore discrepancies in the machine mode of a register. */
  635. static struct table_elt *
  636. lookup_for_remove (x, hash, mode)
  637. rtx x;
  638. int hash;
  639. enum machine_mode mode;
  640. {
  641. register struct table_elt *p;
  642. if (GET_CODE (x) == REG)
  643. {
  644. int regno = REGNO (x);
  645. /* Don't check the machine mode when comparing registers;
  646. invalidating (REG:SI 0) also invalidates (REG:DF 0). */
  647. for (p = table[hash]; p; p = p->next_same_hash)
  648. if (GET_CODE (p->exp) == REG
  649. && REGNO (p->exp) == regno)
  650. return p;
  651. }
  652. else
  653. {
  654. for (p = table[hash]; p; p = p->next_same_hash)
  655. if (mode == p->mode && (x == p->exp || exp_equiv_p (x, p->exp, 0)))
  656. return p;
  657. }
  658. return 0;
  659. }
  660. /* Look for an expression equivalent to X and of the form (CODE Y).
  661. If one is found, return Y. */
  662. static rtx
  663. lookup_as_function (x, code)
  664. rtx x;
  665. enum rtx_code code;
  666. {
  667. register struct table_elt *p = lookup (x, safe_hash (x, 0) % NBUCKETS,
  668. GET_MODE (x));
  669. if (p == 0)
  670. return 0;
  671. for (p = p->first_same_value; p; p = p->next_same_value)
  672. {
  673. if (GET_CODE (p->exp) == code
  674. /* Make sure this is a valid entry in the table. */
  675. && (exp_equiv_p (XEXP (p->exp, 0), XEXP (p->exp, 0))))
  676. return XEXP (p->exp, 0);
  677. }
  678. return 0;
  679. }
  680. /* Insert X in the hash table, assuming HASH is its hash code
  681. and CLASSP is the current first element of the class it should go in
  682. (or 0 if a new class should be made).
  683. It is inserted at the proper position to keep the class in
  684. the order cheapest first.
  685. MODE is the machine-mode of X, or if X is an integer constant
  686. with VOIDmode then MODE is the mode with which X will be used.
  687. For elements of equal cheapness, the most recent one
  688. goes in front, except that the first element in the list
  689. remains first unless a cheaper element is added.
  690. The in_memory field in the hash table element is set to 0.
  691. The caller must set it nonzero if appropriate.
  692. You should call insert_regs (X, CLASSP, MODIFY) before calling here,
  693. and if insert_regs returns a nonzero value
  694. you must then recompute its hash code before calling here.
  695. If necessary, update table showing constant values of quantities. */
  696. #define CHEAPER(X,Y) \
  697. (((X)->cost < (Y)->cost) || \
  698. (GET_CODE ((X)->exp) == REG && GET_CODE ((Y)->exp) == REG \
  699. && (regno_last_uid[REGNO ((X)->exp)] > cse_basic_block_end \
  700. || regno_first_uid[REGNO ((X)->exp)] < cse_basic_block_start) \
  701. && (regno_last_uid[REGNO ((X)->exp)] \
  702. > regno_last_uid[REGNO ((Y)->exp)])))
  703. static struct table_elt *
  704. insert (x, classp, hash, mode)
  705. register rtx x;
  706. register struct table_elt *classp;
  707. int hash;
  708. enum machine_mode mode;
  709. {
  710. register struct table_elt *elt;
  711. /* Put an element for X into the right hash bucket. */
  712. elt = get_element ();
  713. elt->exp = x;
  714. elt->cost = rtx_cost (x) * 2;
  715. /* Make pseudo regs a little cheaper than hard regs. */
  716. if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER)
  717. elt->cost -= 1;
  718. elt->next_same_value = 0;
  719. elt->prev_same_value = 0;
  720. elt->next_same_hash = table[hash];
  721. elt->prev_same_hash = 0;
  722. elt->related_value = 0;
  723. elt->in_memory = 0;
  724. elt->equivalence_only = 0;
  725. elt->mode = mode;
  726. if (table[hash])
  727. table[hash]->prev_same_hash = elt;
  728. table[hash] = elt;
  729. /* Put it into the proper value-class. */
  730. if (classp)
  731. {
  732. if (CHEAPER (elt, classp))
  733. /** Insert at the head of the class */
  734. {
  735. register struct table_elt *p;
  736. elt->next_same_value = classp;
  737. classp->prev_same_value = elt;
  738. elt->first_same_value = elt;
  739. for (p = classp; p; p = p->next_same_value)
  740. p->first_same_value = elt;
  741. }
  742. else
  743. {
  744. /* Insert not at head of the class. */
  745. /* Put it after the last element cheaper than X. */
  746. register struct table_elt *p, *next;
  747. for (p = classp; (next = p->next_same_value) && CHEAPER (p, elt);
  748. p = next);
  749. elt->next_same_value = next;
  750. if (next)
  751. next->prev_same_value = elt;
  752. elt->prev_same_value = p;
  753. p->next_same_value = elt;
  754. elt->first_same_value = classp;
  755. }
  756. }
  757. else
  758. elt->first_same_value = elt;
  759. if ((CONSTANT_P (x) || FIXED_BASE_PLUS_P (x))
  760. && GET_CODE (elt->first_same_value->exp) == REG)
  761. {
  762. qty_const[reg_qty[REGNO (elt->first_same_value->exp)]] = x;
  763. qty_const_insn[reg_qty[REGNO (elt->first_same_value->exp)]] = this_insn;
  764. }
  765. if (GET_CODE (x) == REG)
  766. {
  767. if (elt->next_same_value != 0
  768. && (CONSTANT_P (elt->next_same_value->exp)
  769. || FIXED_BASE_PLUS_P (elt->next_same_value->exp)))
  770. {
  771. qty_const[reg_qty[REGNO (x)]] = elt->next_same_value->exp;
  772. qty_const_insn[reg_qty[REGNO (x)]] = this_insn;
  773. }
  774. if (CONSTANT_P (elt->first_same_value->exp)
  775. || FIXED_BASE_PLUS_P (elt->first_same_value->exp))
  776. {
  777. qty_const[reg_qty[REGNO (x)]] = elt->first_same_value->exp;
  778. qty_const_insn[reg_qty[REGNO (x)]] = this_insn;
  779. }
  780. }
  781. /* If this is a constant with symbolic value,
  782. and it has a term with an explicit integer value,
  783. link it up with related expressions. */
  784. if (GET_CODE (x) == CONST)
  785. {
  786. rtx subexp = get_related_value (x);
  787. int subhash;
  788. struct table_elt *subelt, *subelt_prev;
  789. if (subexp != 0)
  790. {
  791. /* Get the integer-free subexpression in the hash table. */
  792. subhash = safe_hash (subexp, mode) % NBUCKETS;
  793. subelt = lookup (subexp, subhash, mode);
  794. if (subelt == 0)
  795. subelt = insert (subexp, 0, subhash, mode);
  796. /* Initialize SUBELT's circular chain if it has none. */
  797. if (subelt->related_value == 0)
  798. subelt->related_value = subelt;
  799. /* Find the element in the circular chain that precedes SUBELT. */
  800. subelt_prev = subelt;
  801. while (subelt_prev->related_value != subelt)
  802. subelt_prev = subelt_prev->related_value;
  803. /* Put new ELT into SUBELT's circular chain just before SUBELT.
  804. This way the element that follows SUBELT is the oldest one. */
  805. elt->related_value = subelt_prev->related_value;
  806. subelt_prev->related_value = elt;
  807. }
  808. }
  809. return elt;
  810. }
  811. /* Remove from the hash table, or mark as invalid,
  812. all expressions whose values could be altered by storing in X.
  813. X is a register, a subreg, or a memory reference with nonvarying address
  814. (because, when a memory reference with a varying address is stored in,
  815. all memory references are removed by invalidate_memory
  816. so specific invalidation is superfluous).
  817. A nonvarying address may be just a register or just
  818. a symbol reference, or it may be either of those plus
  819. a numeric offset. */
  820. static void
  821. invalidate (x)
  822. rtx x;
  823. {
  824. register int i;
  825. register struct table_elt *p;
  826. register rtx base;
  827. register int start, end;
  828. /* If X is a register, dependencies on its contents
  829. are recorded through the qty number mechanism.
  830. Just change the qty number of the register,
  831. mark it as invalid for expressions that refer to it,
  832. and remove it itself. */
  833. if (GET_CODE (x) == REG)
  834. {
  835. register int hash = HASH (x, 0);
  836. reg_invalidate (REGNO (x));
  837. remove (lookup_for_remove (x, hash, GET_MODE (x)), hash);
  838. return;
  839. }
  840. if (GET_CODE (x) == SUBREG)
  841. {
  842. if (GET_CODE (SUBREG_REG (x)) != REG)
  843. abort ();
  844. invalidate (SUBREG_REG (x));
  845. return;
  846. }
  847. /* X is not a register; it must be a memory reference with
  848. a nonvarying address. Remove all hash table elements
  849. that refer to overlapping pieces of memory. */
  850. if (GET_CODE (x) != MEM)
  851. abort ();
  852. base = XEXP (x, 0);
  853. start = 0;
  854. /* Registers with nonvarying addresses usually have constant equivalents;
  855. but the frame pointer register is also possible. */
  856. if (GET_CODE (base) == REG
  857. && qty_const[reg_qty[REGNO (base)]] != 0)
  858. base = qty_const[reg_qty[REGNO (base)]];
  859. if (GET_CODE (base) == CONST)
  860. base = XEXP (base, 0);
  861. if (GET_CODE (base) == PLUS
  862. && GET_CODE (XEXP (base, 1)) == CONST_INT)
  863. {
  864. start = INTVAL (XEXP (base, 1));
  865. base = XEXP (base, 0);
  866. }
  867. end = start + GET_MODE_SIZE (GET_MODE (x));
  868. for (i = 0; i < NBUCKETS; i++)
  869. {
  870. register struct table_elt *next;
  871. for (p = table[i]; p; p = next)
  872. {
  873. next = p->next_same_hash;
  874. if (refers_to_mem_p (p->exp, base, start, end))
  875. remove (p, i);
  876. }
  877. }
  878. }
  879. /* Remove all expressions that refer to register REGNO,
  880. since they are already invalid, and we are about to
  881. mark that register valid again and don't want the old
  882. expressions to reappear as valid. */
  883. static void
  884. remove_invalid_refs (regno)
  885. int regno;
  886. {
  887. register int i;
  888. register struct table_elt *p, *next;
  889. register rtx x = reg_rtx[regno];
  890. for (i = 0; i < NBUCKETS; i++)
  891. for (p = table[i]; p; p = next)
  892. {
  893. next = p->next_same_hash;
  894. if (GET_CODE (p->exp) != REG && refers_to_p (p->exp, x))
  895. remove (p, i);
  896. }
  897. }
  898. /* Remove from the hash table all expressions that reference memory,
  899. or some of them as specified by *WRITES. */
  900. static void
  901. invalidate_memory (writes)
  902. struct write_data *writes;
  903. {
  904. register int i;
  905. register struct table_elt *p, *next;
  906. int all = writes->all;
  907. int nonscalar = writes->nonscalar;
  908. for (i = 0; i < NBUCKETS; i++)
  909. for (p = table[i]; p; p = next)
  910. {
  911. next = p->next_same_hash;
  912. if (p->in_memory
  913. && (all
  914. || (nonscalar && p->in_struct)
  915. || cse_rtx_addr_varies_p (p->exp)))
  916. remove (p, i);
  917. }
  918. }
  919. /* Return the value of the integer term in X, if one is apparent;
  920. otherwise return 0.
  921. We do not check extremely carefully for the presence of integer terms
  922. but rather consider only the cases that `insert' notices
  923. for the `related_value' field. */
  924. static int
  925. get_integer_term (x)
  926. rtx x;
  927. {
  928. if (GET_CODE (x) == CONST)
  929. x = XEXP (x, 0);
  930. if (GET_CODE (x) == MINUS
  931. && GET_CODE (XEXP (x, 1)) == CONST_INT)
  932. return - INTVAL (XEXP (x, 1));
  933. if (GET_CODE (x) != PLUS)
  934. return 0;
  935. if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  936. return INTVAL (XEXP (x, 0));
  937. if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  938. return INTVAL (XEXP (x, 1));
  939. return 0;
  940. }
  941. static rtx
  942. get_related_value (x)
  943. rtx x;
  944. {
  945. if (GET_CODE (x) != CONST)
  946. return 0;
  947. x = XEXP (x, 0);
  948. if (GET_CODE (x) == PLUS)
  949. {
  950. if (GET_CODE (XEXP (x, 0)) == CONST_INT)
  951. return XEXP (x, 1);
  952. if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  953. return XEXP (x, 0);
  954. }
  955. else if (GET_CODE (x) == MINUS
  956. && GET_CODE (XEXP (x, 1)) == CONST_INT)
  957. return XEXP (x, 0);
  958. return 0;
  959. }
  960. /* Given an expression X of type CONST,
  961. and ELT which is its table entry (or 0 if it
  962. is not in the hash table),
  963. return an alternate expression for X as a register plus integer.
  964. If none can be found or it would not be a valid address, return 0. */
  965. static rtx
  966. use_related_value (x, elt)
  967. rtx x;
  968. struct table_elt *elt;
  969. {
  970. register struct table_elt *relt = 0;
  971. register struct table_elt *p;
  972. int offset;
  973. rtx addr;
  974. /* First, is there anything related known?
  975. If we have a table element, we can tell from that.
  976. Otherwise, must look it up. */
  977. if (elt != 0 && elt->related_value != 0)
  978. relt = elt;
  979. else if (elt == 0 && GET_CODE (x) == CONST)
  980. {
  981. rtx subexp = get_related_value (x);
  982. if (subexp != 0)
  983. relt = lookup (subexp,
  984. safe_hash (subexp, GET_MODE (subexp)) % NBUCKETS,
  985. GET_MODE (subexp));
  986. }
  987. if (relt == 0)
  988. return 0;
  989. /* Search all related table entries for one that has an
  990. equivalent register. */
  991. p = relt;
  992. while (1)
  993. {
  994. if (p->first_same_value != 0
  995. && GET_CODE (p->first_same_value->exp) == REG)
  996. break;
  997. p = p->related_value;
  998. /* We went all the way around, so there is nothing to be found.
  999. Return failure. */
  1000. if (p == relt)
  1001. return 0;
  1002. /* Perhaps RELT was in the table for some other reason and
  1003. it has no related values recorded. */
  1004. if (p == 0)
  1005. return 0;
  1006. }
  1007. offset = (get_integer_term (x) - get_integer_term (p->exp));
  1008. if (offset == 0)
  1009. abort ();
  1010. addr = plus_constant (p->first_same_value->exp, offset);
  1011. if (memory_address_p (QImode, addr))
  1012. return addr;
  1013. return 0;
  1014. }
  1015. /* Hash an rtx. We are careful to make sure the value is never negative.
  1016. Equivalent registers hash identically.
  1017. Store 1 in do_not_record if any subexpression is volatile.
  1018. Store 1 in hash_arg_in_memory if X contains a MEM rtx
  1019. which does not have the ->unchanging bit set.
  1020. In this case, also store 1 in hash_arg_in_struct
  1021. if there is a MEM rtx which has the ->in_struct bit set.
  1022. Note that cse_insn knows that the hash code of a MEM expression
  1023. is just (int) MEM plus the hash code of the address.
  1024. It also knows it can use HASHREG to get the hash code of (REG n). */
  1025. #define HASHBITS 16
  1026. #define HASHREG(RTX) \
  1027. ((((int) REG << 7) + reg_qty[REGNO (RTX)]) % NBUCKETS)
  1028. static int
  1029. canon_hash (x, mode)
  1030. rtx x;
  1031. enum machine_mode mode;
  1032. {
  1033. register int i, j;
  1034. register int hash = 0;
  1035. register RTX_CODE code;
  1036. register char *fmt;
  1037. /* repeat is used to turn tail-recursion into iteration. */
  1038. repeat:
  1039. code = GET_CODE (x);
  1040. switch (code)
  1041. {
  1042. case REG:
  1043. {
  1044. /* We do not invalidate anything on pushing or popping
  1045. because they cannot change anything but the stack pointer;
  1046. but that means we must consider the stack pointer volatile
  1047. since it can be changed "mysteriously". */
  1048. register int regno = REGNO (x);
  1049. if (regno == STACK_POINTER_REGNUM)
  1050. {
  1051. do_not_record = 1;
  1052. return 0;
  1053. }
  1054. return hash + ((int) REG << 7) + reg_qty[regno];
  1055. }
  1056. case CONST_INT:
  1057. hash = INTVAL (x);
  1058. hash = (int) mode + ((int) CONST_INT << 7) + hash + hash >> HASHBITS;
  1059. return ((1 << HASHBITS) - 1) & hash;
  1060. /* Assume there is only one rtx object for any given label. */
  1061. case LABEL_REF:
  1062. return hash + ((int) LABEL_REF << 7) + (int) XEXP (x, 0);
  1063. case SYMBOL_REF:
  1064. return hash + ((int) SYMBOL_REF << 7) + (int) XEXP (x, 0);
  1065. case MEM:
  1066. if (x->volatil)
  1067. {
  1068. do_not_record = 1;
  1069. return 0;
  1070. }
  1071. if (! x->unchanging)
  1072. {
  1073. hash_arg_in_memory = 1;
  1074. if (x->in_struct) hash_arg_in_struct = 1;
  1075. }
  1076. /* Now that we have already found this special case,
  1077. might as well speed it up as much as possible. */
  1078. hash += (int) MEM;
  1079. x = XEXP (x, 0);
  1080. goto repeat;
  1081. case PRE_DEC:
  1082. case PRE_INC:
  1083. case POST_DEC:
  1084. case POST_INC:
  1085. case PC:
  1086. case CC0:
  1087. case CALL:
  1088. do_not_record = 1;
  1089. return 0;
  1090. case ASM_OPERANDS:
  1091. if (x->volatil)
  1092. {
  1093. do_not_record = 1;
  1094. return 0;
  1095. }
  1096. }
  1097. i = GET_RTX_LENGTH (code) - 1;
  1098. hash += (int) code + (int) GET_MODE (x);
  1099. fmt = GET_RTX_FORMAT (code);
  1100. for (; i >= 0; i--)
  1101. {
  1102. if (fmt[i] == 'e')
  1103. {
  1104. /* If we are about to do the last recursive call
  1105. needed at this level, change it into iteration.
  1106. This function is called enough to be worth it. */
  1107. if (i == 0)
  1108. {
  1109. x = XEXP (x, 0);
  1110. goto repeat;
  1111. }
  1112. hash += canon_hash (XEXP (x, i), 0);
  1113. }
  1114. else if (fmt[i] == 'E')
  1115. for (j = 0; j < XVECLEN (x, i); j++)
  1116. hash += canon_hash (XVECEXP (x, i, j), 0);
  1117. else if (fmt[i] == 's')
  1118. {
  1119. register char *p = XSTR (x, i);
  1120. while (*p)
  1121. {
  1122. register int tem = *p++;
  1123. hash += ((1 << HASHBITS) - 1) & (tem + tem >> HASHBITS);
  1124. }
  1125. }
  1126. else
  1127. {
  1128. register int tem = XINT (x, i);
  1129. hash += ((1 << HASHBITS) - 1) & (tem + tem >> HASHBITS);
  1130. }
  1131. }
  1132. return hash;
  1133. }
  1134. /* Like canon_hash but with no side effects. */
  1135. static int
  1136. safe_hash (x, mode)
  1137. rtx x;
  1138. enum machine_mode mode;
  1139. {
  1140. int save_do_not_record = do_not_record;
  1141. int save_hash_arg_in_memory = hash_arg_in_memory;
  1142. int save_hash_arg_in_struct = hash_arg_in_struct;
  1143. int hash = canon_hash (x, mode);
  1144. hash_arg_in_memory = save_hash_arg_in_memory;
  1145. hash_arg_in_struct = save_hash_arg_in_struct;
  1146. do_not_record = save_do_not_record;
  1147. return hash;
  1148. }
  1149. /* Return 1 iff X and Y would canonicalize into the same thing,
  1150. without actually constructing the canonicalization of either one.
  1151. If VALIDATE is nonzero,
  1152. we assume X is an expression being processed from the rtl
  1153. and Y was found in the hash table. We check register refs
  1154. in Y for being marked as valid. */
  1155. static int
  1156. exp_equiv_p (x, y, validate)
  1157. rtx x, y;
  1158. int validate;
  1159. {
  1160. register int i;
  1161. register RTX_CODE code = GET_CODE (x);
  1162. register char *fmt;
  1163. /* An expression is usually equivalent to itself,
  1164. but not if it's a register that's invalid. */
  1165. if (x == y)
  1166. return (code != REG
  1167. || !validate
  1168. || reg_in_table[REGNO (y)] == reg_tick[REGNO (y)]);
  1169. if (code != GET_CODE (y))
  1170. return 0;
  1171. if (code == REG)
  1172. return (reg_qty[REGNO (x)] == reg_qty[REGNO (y)]
  1173. && (!validate
  1174. || reg_in_table[REGNO (y)] == reg_tick[REGNO (y)]));
  1175. /* Assume there is only one rtx object to refer
  1176. to any given label.
  1177. We already know that X and Y are not the same object
  1178. so they must differ. */
  1179. if (code == LABEL_REF || code == SYMBOL_REF)
  1180. return XEXP (x, 0) == XEXP (y, 0);
  1181. /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
  1182. if (GET_MODE (x) != GET_MODE (y))
  1183. return 0;
  1184. /* Compare the elements. If any pair of corresponding elements
  1185. fail to match, return 0 for the whole things. */
  1186. fmt = GET_RTX_FORMAT (code);
  1187. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1188. {
  1189. if (fmt[i] == 'e')
  1190. {
  1191. if (! exp_equiv_p (XEXP (x, i), XEXP (y, i), validate))
  1192. return 0;
  1193. }
  1194. else if (fmt[i] == 'E')
  1195. {
  1196. int j;
  1197. for (j = 0; j < XVECLEN (x, i); j++)
  1198. if (! exp_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j), validate))
  1199. return 0;
  1200. }
  1201. else if (fmt[i] == 's')
  1202. {
  1203. if (strcmp (XSTR (x, i), XSTR (y, i)))
  1204. return 0;
  1205. }
  1206. else
  1207. {
  1208. if (XINT (x, i) != XINT (y, i))
  1209. return 0;
  1210. }
  1211. }
  1212. return 1;
  1213. }
  1214. /* Return 1 iff any subexpression of X matches Y.
  1215. Here we do not require that X or Y be valid (for registers referred to)
  1216. for being in the hash table. */
  1217. int
  1218. refers_to_p (x, y)
  1219. rtx x, y;
  1220. {
  1221. register int i;
  1222. register RTX_CODE code;
  1223. register char *fmt;
  1224. repeat:
  1225. if (x == y)
  1226. return 1;
  1227. code = GET_CODE (x);
  1228. /* If X as a whole has the same code as Y, they may match.
  1229. If so, return 1. */
  1230. if (code == GET_CODE (y))
  1231. {
  1232. if (exp_equiv_p (x, y, 0))
  1233. return 1;
  1234. }
  1235. /* X does not match, so try its subexpressions. */
  1236. fmt = GET_RTX_FORMAT (code);
  1237. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1238. if (fmt[i] == 'e')
  1239. {
  1240. if (i == 0)
  1241. {
  1242. x = XEXP (x, 0);
  1243. goto repeat;
  1244. }
  1245. else
  1246. if (refers_to_p (XEXP (x, i), y))
  1247. return 1;
  1248. }
  1249. else if (fmt[i] == 'E')
  1250. {
  1251. int j;
  1252. for (j = 0; j < XVECLEN (x, i); j++)
  1253. if (refers_to_p (XVECEXP (x, i, j), y))
  1254. return 1;
  1255. }
  1256. return 0;
  1257. }
  1258. /* Return 1 iff any subexpression of X refers to memory
  1259. at an address of REG plus some offset
  1260. such that any of the bytes' offsets fall between START (inclusive)
  1261. and END (exclusive).
  1262. The value is undefined if X is a varying address.
  1263. This function is not used in such cases.
  1264. When used in the cse pass, `qty_const' is nonzero, and it is used
  1265. to treat an address that is a register with a known constant value
  1266. as if it were that constant value.
  1267. In the loop pass, `qty_const' is zero, so this is not done. */
  1268. int
  1269. refers_to_mem_p (x, reg, start, end)
  1270. rtx x, reg;
  1271. int start, end;
  1272. {
  1273. register int i;
  1274. register RTX_CODE code;
  1275. register char *fmt;
  1276. repeat:
  1277. code = GET_CODE (x);
  1278. if (code == MEM)
  1279. {
  1280. register rtx addr = XEXP (x, 0); /* Get the address. */
  1281. int myend;
  1282. if (GET_CODE (addr) == REG
  1283. /* qty_const is 0 when outside the cse pass;
  1284. at such times, this info is not available. */
  1285. && qty_const != 0
  1286. && qty_const[reg_qty[REGNO (addr)]] != 0)
  1287. addr = qty_const[reg_qty[REGNO (addr)]];
  1288. if (GET_CODE (addr) == CONST)
  1289. addr = XEXP (addr, 0);
  1290. /* If ADDR is BASE, or BASE plus an integer, put
  1291. the integer in I. */
  1292. if (addr == reg)
  1293. i = 0;
  1294. else if (GET_CODE (addr) == PLUS
  1295. && XEXP (addr, 0) == reg
  1296. && GET_CODE (XEXP (addr, 1)) == CONST_INT)
  1297. i = INTVAL (XEXP (addr, 1));
  1298. else
  1299. return 0;
  1300. myend = i + GET_MODE_SIZE (GET_MODE (x));
  1301. return myend > start && i < end;
  1302. }
  1303. /* X does not match, so try its subexpressions. */
  1304. fmt = GET_RTX_FORMAT (code);
  1305. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1306. if (fmt[i] == 'e')
  1307. {
  1308. if (i == 0)
  1309. {
  1310. x = XEXP (x, 0);
  1311. goto repeat;
  1312. }
  1313. else
  1314. if (refers_to_mem_p (XEXP (x, i), reg, start, end))
  1315. return 1;
  1316. }
  1317. else if (fmt[i] == 'E')
  1318. {
  1319. int j;
  1320. for (j = 0; j < XVECLEN (x, i); j++)
  1321. if (refers_to_mem_p (XVECEXP (x, i, j), reg, start, end))
  1322. return 1;
  1323. }
  1324. return 0;
  1325. }
  1326. /* Nonzero if X refers to memory at a varying address;
  1327. except that a register which has at the moment a known constant value
  1328. isn't considered variable. */
  1329. static int
  1330. cse_rtx_addr_varies_p (x)
  1331. rtx x;
  1332. {
  1333. if (GET_CODE (x) == MEM
  1334. && GET_CODE (XEXP (x, 0)) == REG
  1335. && qty_const[reg_qty[REGNO (XEXP (x, 0))]] != 0)
  1336. return 0;
  1337. return rtx_addr_varies_p (x);
  1338. }
  1339. /* Canonicalize an expression:
  1340. replace each register reference inside it
  1341. with the "oldest" equivalent register. */
  1342. static rtx
  1343. canon_reg (x)
  1344. rtx x;
  1345. {
  1346. register int i;
  1347. register RTX_CODE code = GET_CODE (x);
  1348. register char *fmt;
  1349. if (code == REG)
  1350. {
  1351. register int qty = reg_qty[REGNO (x)];
  1352. register rtx new = reg_rtx[qty_first_reg[qty]];
  1353. /* Never replace a hard reg, because hard regs can appear
  1354. in more than one machine mode, and we must preserve the mode
  1355. of each occurrence. Also, some hard regs appear in
  1356. MEMs that are shared and mustn't be altered. */
  1357. if (REGNO (x) < FIRST_PSEUDO_REGISTER)
  1358. return x;
  1359. return new ? new : x;
  1360. }
  1361. fmt = GET_RTX_FORMAT (code);
  1362. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1363. {
  1364. register int j;
  1365. if (fmt[i] == 'e')
  1366. XEXP (x, i) = canon_reg (XEXP (x, i));
  1367. else if (fmt[i] == 'E')
  1368. for (j = 0; j < XVECLEN (x, i); j++)
  1369. XVECEXP (x, i, j) = canon_reg (XVECEXP (x, i, j));
  1370. }
  1371. return x;
  1372. }
  1373. /* If X is a nontrivial arithmetic operation on an argument
  1374. for which a constant value can be determined, return
  1375. the result of operating on that value, as a constant.
  1376. Otherwise, return X, possibly with one or more operands
  1377. modified by recursive calls to this function.
  1378. If X is a register whose contents are known, we do NOT
  1379. return those contents. This is because an instruction that
  1380. uses a register is usually faster than one that uses a constant.
  1381. COPYFLAG is nonzero for memory addresses and subexpressions thereof.
  1382. If COPYFLAG is nonzero, we avoid altering X itself
  1383. by creating new structure when necessary. In this case we
  1384. can risk creating invalid structure because it will be tested.
  1385. If COPYFLAG is zero, be careful not to substitute constants
  1386. into expressions that cannot be simplified. */
  1387. static rtx
  1388. fold_rtx (x, copyflag)
  1389. rtx x;
  1390. int copyflag;
  1391. {
  1392. register RTX_CODE code = GET_CODE (x);
  1393. register char *fmt;
  1394. register int i, val;
  1395. rtx new = 0;
  1396. int copied = ! copyflag;
  1397. int width = GET_MODE_BITSIZE (GET_MODE (x));
  1398. /* Constant equivalents of first three operands of X;
  1399. 0 when no such equivalent is known. */
  1400. rtx const_arg0;
  1401. rtx const_arg1;
  1402. rtx const_arg2;
  1403. switch (code)
  1404. {
  1405. case CONST:
  1406. case CONST_INT:
  1407. case CONST_DOUBLE:
  1408. case SYMBOL_REF:
  1409. case LABEL_REF:
  1410. case PC:
  1411. case CC0:
  1412. case REG:
  1413. return x;
  1414. /* We must be careful when folding a memory address
  1415. to avoid making it invalid. So fold nondestrictively
  1416. and use the result only if it's valid. */
  1417. case MEM:
  1418. {
  1419. rtx newaddr = fold_rtx (XEXP (x, 0), 1);
  1420. if (! memory_address_p (GET_MODE (x), newaddr)
  1421. && memory_address_p (GET_MODE (x), XEXP (x, 0)))
  1422. return x;
  1423. if (copyflag)
  1424. return gen_rtx (MEM, GET_MODE (x), newaddr);
  1425. XEXP (x, 0) = newaddr;
  1426. return x;
  1427. }
  1428. }
  1429. const_arg0 = 0;
  1430. const_arg1 = 0;
  1431. const_arg2 = 0;
  1432. /* Try folding our operands.
  1433. Then see which ones have constant values known. */
  1434. fmt = GET_RTX_FORMAT (code);
  1435. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1436. if (fmt[i] == 'e')
  1437. {
  1438. register rtx tem = fold_rtx (XEXP (x, i), copyflag);
  1439. /* If an operand has changed under folding, and we are not supposed to
  1440. alter the original structure, copy X if we haven't yet done so. */
  1441. if (! copied && tem != XEXP (x, i))
  1442. {
  1443. int j;
  1444. rtx new = rtx_alloc (code);
  1445. PUT_MODE (new, GET_MODE (x));
  1446. for (j = 0; j < GET_RTX_LENGTH (code); j++)
  1447. XINT (new, j) = XINT (x, j);
  1448. x = new;
  1449. copied = 1;
  1450. }
  1451. /* Install the possibly altered folded operand. */
  1452. XEXP (x, i) = tem;
  1453. /* For the first three operands, see if the operand
  1454. is constant or equivalent to a constant. */
  1455. if (i < 3)
  1456. {
  1457. rtx tem1;
  1458. rtx const_arg = 0;
  1459. if (CONSTANT_P (tem))
  1460. const_arg = tem;
  1461. else if (GET_CODE (tem) == REG
  1462. && qty_const[reg_qty[REGNO (tem)]] != 0
  1463. /* Make sure it is really a constant */
  1464. && ((tem1 = qty_const[reg_qty[REGNO (tem)]]),
  1465. GET_CODE (tem1) != REG && GET_CODE (tem1) != PLUS))
  1466. const_arg = qty_const[reg_qty[REGNO (tem)]];
  1467. switch (i)
  1468. {
  1469. case 0:
  1470. const_arg0 = const_arg;
  1471. break;
  1472. case 1:
  1473. const_arg1 = const_arg;
  1474. break;
  1475. case 2:
  1476. const_arg2 = const_arg;
  1477. break;
  1478. }
  1479. }
  1480. }
  1481. else if (fmt[i] == 'E')
  1482. /* Don't try to fold inside of a vector of expressions.
  1483. Doing nothing is is harmless. */
  1484. ;
  1485. /* Now decode the kind of rtx X is
  1486. and either return X (if nothing can be done)
  1487. or store a value in VAL and drop through
  1488. (to return a CONST_INT for the integer VAL). */
  1489. if (GET_RTX_LENGTH (code) == 1)
  1490. {
  1491. register int arg0;
  1492. if (const_arg0 == 0 || GET_CODE (const_arg0) != CONST_INT)
  1493. return x;
  1494. arg0 = INTVAL (const_arg0);
  1495. switch (GET_CODE (x))
  1496. {
  1497. case NOT:
  1498. val = ~ arg0;
  1499. break;
  1500. case NEG:
  1501. val = - arg0;
  1502. break;
  1503. case TRUNCATE:
  1504. val = arg0;
  1505. break;
  1506. case ZERO_EXTEND:
  1507. {
  1508. enum machine_mode mode = GET_MODE (XEXP (x, 0));
  1509. if (mode == VOIDmode)
  1510. return x;
  1511. val = arg0 & ~((-1) << GET_MODE_BITSIZE (mode));
  1512. break;
  1513. }
  1514. case SIGN_EXTEND:
  1515. {
  1516. enum machine_mode mode = GET_MODE (XEXP (x, 0));
  1517. if (mode == VOIDmode)
  1518. return x;
  1519. val = arg0 & ~((-1) << GET_MODE_BITSIZE (mode));
  1520. if (val & (1 << (GET_MODE_BITSIZE (mode) - 1)))
  1521. val -= 1 << GET_MODE_BITSIZE (mode);
  1522. break;
  1523. }
  1524. default:
  1525. return x;
  1526. }
  1527. }
  1528. else if (GET_RTX_LENGTH (code) == 2)
  1529. {
  1530. register int arg0, arg1, arg0s, arg1s;
  1531. /* If 1st arg is the condition codes, 2nd must be zero
  1532. and this must be a comparison.
  1533. Decode the info on how the previous insn set the cc0
  1534. and use that to deduce result of comparison. */
  1535. if (XEXP (x, 0) == cc0_rtx)
  1536. {
  1537. if (prev_insn_cc0 == 0
  1538. || const_arg1 != const0_rtx)
  1539. return x;
  1540. if (code == LEU || code == LTU || code == GEU || code == GTU)
  1541. arg0 = prev_insn_cc0 & 7;
  1542. else
  1543. arg0 = (prev_insn_cc0 >> 3) & 7;
  1544. if (arg0 == 7) arg0 = -1;
  1545. switch (code)
  1546. {
  1547. case LE:
  1548. case LEU:
  1549. return (arg0 <= 0) ? const1_rtx : const0_rtx;
  1550. case LT:
  1551. case LTU:
  1552. return (arg0 < 0) ? const1_rtx : const0_rtx;
  1553. case GE:
  1554. case GEU:
  1555. return (arg0 >= 0) ? const1_rtx : const0_rtx;
  1556. case GT:
  1557. case GTU:
  1558. return (arg0 > 0) ? const1_rtx : const0_rtx;
  1559. case NE:
  1560. return (arg0 != 0) ? const1_rtx : const0_rtx;
  1561. case EQ:
  1562. return (arg0 == 0) ? const1_rtx : const0_rtx;
  1563. default:
  1564. abort ();
  1565. }
  1566. }
  1567. if (const_arg0 == 0 || const_arg1 == 0
  1568. || GET_CODE (const_arg0) != CONST_INT
  1569. || GET_CODE (const_arg1) != CONST_INT)
  1570. {
  1571. /* Even if we can't compute a constant result,
  1572. there are some cases worth simplifying. */
  1573. if (code == PLUS)
  1574. {
  1575. if (const_arg0 == const0_rtx)
  1576. return XEXP (x, 1);
  1577. if (const_arg1 == const0_rtx)
  1578. return XEXP (x, 0);
  1579. /* Handle both-operands-constant cases. */
  1580. if (const_arg0 != 0 && const_arg1 != 0)
  1581. {
  1582. if (GET_CODE (const_arg0) == CONST_INT)
  1583. new = plus_constant (const_arg1, INTVAL (const_arg0));
  1584. else if (GET_CODE (const_arg1) == CONST_INT)
  1585. new = plus_constant (const_arg0, INTVAL (const_arg1));
  1586. else
  1587. {
  1588. new = gen_rtx (PLUS, GET_MODE (x), const0_rtx, const0_rtx);
  1589. XEXP (new, 0) = const_arg0;
  1590. if (GET_CODE (const_arg0) == CONST)
  1591. XEXP (new, 0) = XEXP (const_arg0, 0);
  1592. XEXP (new, 1) = const_arg1;
  1593. if (GET_CODE (const_arg1) == CONST)
  1594. XEXP (new, 1) = XEXP (const_arg1, 0);
  1595. new = gen_rtx (CONST, GET_MODE (new), new);
  1596. }
  1597. }
  1598. else if (const_arg0 != 0
  1599. && GET_CODE (const_arg0) == CONST_INT
  1600. && GET_CODE (XEXP (x, 1)) == PLUS
  1601. && (CONSTANT_P (XEXP (XEXP (x, 1), 0))
  1602. || CONSTANT_P (XEXP (XEXP (x, 1), 1))))
  1603. /* constant + (variable + constant)
  1604. can result if an index register is made constant.
  1605. We simplify this by adding the constants.
  1606. If we did not, it would become an invalid address. */
  1607. new = plus_constant (XEXP (x, 1),
  1608. INTVAL (const_arg0));
  1609. else if (const_arg1 != 0
  1610. && GET_CODE (const_arg1) == CONST_INT
  1611. && GET_CODE (XEXP (x, 0)) == PLUS
  1612. && (CONSTANT_P (XEXP (XEXP (x, 0), 0))
  1613. || CONSTANT_P (XEXP (XEXP (x, 0), 1))))
  1614. new = plus_constant (XEXP (x, 0),
  1615. INTVAL (const_arg1));
  1616. }
  1617. else if (code == MINUS)
  1618. {
  1619. if (const_arg1 == const0_rtx)
  1620. return XEXP (x, 0);
  1621. if (XEXP (x, 0) == XEXP (x, 1)
  1622. || (const_arg0 != 0 && const_arg0 == const_arg1))
  1623. {
  1624. /* We can't assume x-x is 0 with IEEE floating point. */
  1625. if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
  1626. return const0_rtx;
  1627. }
  1628. if (const_arg0 != 0 && const_arg1 != 0
  1629. /* Don't let a relocatable value get a negative coeff. */
  1630. && GET_CODE (const_arg1) == CONST_INT)
  1631. new = plus_constant (const_arg0, - INTVAL (const_arg1));
  1632. }
  1633. /* PLUS and MULT can appear inside of a MEM.
  1634. In such situations, a constant term must come second. */
  1635. else if (code == MULT || code == PLUS)
  1636. if (copyflag && const_arg0 != 0)
  1637. {
  1638. if (! copied)
  1639. x = gen_rtx (code, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
  1640. XEXP (x, 0) = XEXP (x, 1);
  1641. XEXP (x, 1) = const_arg0;
  1642. }
  1643. /* If integer truncation is being done with SUBREG,
  1644. we can compute the result. */
  1645. else if (code == SUBREG)
  1646. if (SUBREG_WORD (x) == 0
  1647. && const_arg0 != 0
  1648. && GET_CODE (const_arg0) == CONST_INT
  1649. && GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
  1650. && (GET_MODE (x) == QImode || GET_MODE (x) == HImode
  1651. || GET_MODE (x) == SImode))
  1652. {
  1653. arg0 = INTVAL (const_arg0);
  1654. arg0 &= (1 << GET_MODE_BITSIZE (GET_MODE (x))) - 1;
  1655. if (arg0 == INTVAL (const_arg0))
  1656. new = const_arg0;
  1657. else
  1658. new = gen_rtx (CONST_INT, VOIDmode, arg0);
  1659. }
  1660. if (new != 0 && LEGITIMATE_CONSTANT_P (new))
  1661. return new;
  1662. return x;
  1663. }
  1664. /* Get the integer argument values in two forms:
  1665. zero-extended in ARG0, ARG1 and sign-extended in ARG0S, ARG1S. */
  1666. arg0 = INTVAL (const_arg0);
  1667. arg1 = INTVAL (const_arg1);
  1668. if (width < HOST_BITS_PER_INT)
  1669. {
  1670. arg0 &= (1 << width) - 1;
  1671. arg1 &= (1 << width) - 1;
  1672. arg0s = arg0;
  1673. if (arg0s & (1 << (width - 1)))
  1674. arg0s |= ((-1) << width);
  1675. arg1s = arg1;
  1676. if (arg1s & (1 << (width - 1)))
  1677. arg1s |= ((-1) << width);
  1678. }
  1679. else
  1680. {
  1681. arg0s = arg0;
  1682. arg1s = arg1;
  1683. }
  1684. /* Compute the value of the arithmetic. */
  1685. switch (code)
  1686. {
  1687. case PLUS:
  1688. val = arg0 + arg1;
  1689. break;
  1690. case MINUS:
  1691. if (GET_MODE (x) == VOIDmode)
  1692. /* Overflowless comparison:
  1693. cannot represent an exact answer, so don't fold.
  1694. This is used only to set the CC0,
  1695. and fold_cc0 will take care of it. */
  1696. return x;
  1697. val = arg0 - arg1;
  1698. break;
  1699. case MULT:
  1700. val = arg0s * arg1s;
  1701. break;
  1702. case DIV:
  1703. if (arg1s == 0)
  1704. return x;
  1705. val = arg0s / arg1s;
  1706. break;
  1707. case MOD:
  1708. if (arg1s == 0)
  1709. return x;
  1710. val = arg0s % arg1s;
  1711. break;
  1712. case UMULT:
  1713. val = (unsigned) arg0 * arg1;
  1714. break;
  1715. case UDIV:
  1716. if (arg1 == 0)
  1717. return x;
  1718. val = (unsigned) arg0 / arg1;
  1719. break;
  1720. case UMOD:
  1721. if (arg1 == 0)
  1722. return x;
  1723. val = (unsigned) arg0 % arg1;
  1724. break;
  1725. case AND:
  1726. val = arg0 & arg1;
  1727. break;
  1728. case IOR:
  1729. val = arg0 | arg1;
  1730. break;
  1731. case XOR:
  1732. val = arg0 ^ arg1;
  1733. break;
  1734. case NE:
  1735. val = arg0 != arg1;
  1736. break;
  1737. case EQ:
  1738. val = arg0 == arg1;
  1739. break;
  1740. case LE:
  1741. val = arg0s <= arg1s;
  1742. break;
  1743. case LT:
  1744. val = arg0s < arg1s;
  1745. break;
  1746. case GE:
  1747. val = arg0s >= arg1s;
  1748. break;
  1749. case GT:
  1750. val = arg0s > arg1s;
  1751. break;
  1752. case LEU:
  1753. val = ((unsigned) arg0) <= ((unsigned) arg1);
  1754. break;
  1755. case LTU:
  1756. val = ((unsigned) arg0) < ((unsigned) arg1);
  1757. break;
  1758. case GEU:
  1759. val = ((unsigned) arg0) >= ((unsigned) arg1);
  1760. break;
  1761. case GTU:
  1762. val = ((unsigned) arg0) > ((unsigned) arg1);
  1763. break;
  1764. case LSHIFT:
  1765. val = ((unsigned) arg0) << arg1;
  1766. break;
  1767. case ASHIFT:
  1768. val = arg0s << arg1;
  1769. break;
  1770. case ROTATERT:
  1771. arg1 = - arg1;
  1772. case ROTATE:
  1773. {
  1774. int size = GET_MODE_SIZE (GET_MODE (x)) * BITS_PER_UNIT;
  1775. if (arg1 > 0)
  1776. {
  1777. arg1 %= size;
  1778. val = ((((unsigned) arg0) << arg1)
  1779. | (((unsigned) arg0) >> (size - arg1)));
  1780. }
  1781. else if (arg1 < 0)
  1782. {
  1783. arg1 = (- arg1) % size;
  1784. val = ((((unsigned) arg0) >> arg1)
  1785. | (((unsigned) arg0) << (size - arg1)));
  1786. }
  1787. else
  1788. val = arg0;
  1789. }
  1790. break;
  1791. case LSHIFTRT:
  1792. val = ((unsigned) arg0) >> arg1;
  1793. break;
  1794. case ASHIFTRT:
  1795. val = arg0s >> arg1;
  1796. break;
  1797. default:
  1798. return x;
  1799. }
  1800. }
  1801. else if (code == IF_THEN_ELSE && const_arg0 != 0
  1802. && GET_CODE (const_arg0) == CONST_INT)
  1803. return XEXP (x, ((INTVAL (const_arg0) != 0) ? 1 : 2));
  1804. else if (code == SIGN_EXTRACT || code == ZERO_EXTRACT)
  1805. {
  1806. if (const_arg0 != 0 && const_arg1 != 0 && const_arg2 != 0
  1807. && GET_CODE (const_arg0) == CONST_INT
  1808. && GET_CODE (const_arg1) == CONST_INT
  1809. && GET_CODE (const_arg2) == CONST_INT)
  1810. {
  1811. /* Extracting a bit-field from a constant */
  1812. val = INTVAL (const_arg0);
  1813. #ifdef BITS_BIG_ENDIAN
  1814. val >>= (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
  1815. - INTVAL (const_arg2) - INTVAL (const_arg1));
  1816. #else
  1817. val >>= INTVAL (const_arg2);
  1818. #endif
  1819. if (HOST_BITS_PER_INT != INTVAL (const_arg1))
  1820. {
  1821. /* First zero-extend. */
  1822. val &= (1 << INTVAL (const_arg1)) - 1;
  1823. /* If desired, propagate sign bit. */
  1824. if (code == SIGN_EXTRACT
  1825. && (val & (1 << (INTVAL (const_arg1) - 1))))
  1826. val |= ~ (1 << INTVAL (const_arg1));
  1827. }
  1828. }
  1829. else
  1830. return x;
  1831. }
  1832. else
  1833. return x;
  1834. /* Clear the bits that don't belong in our mode,
  1835. unless they and our sign bit are all one.
  1836. So we get either a reasonable negative value or a reasonable
  1837. unsigned value for this mode. */
  1838. if (width < HOST_BITS_PER_INT)
  1839. {
  1840. if ((val & ((-1) << (width - 1)))
  1841. != ((-1) << (width - 1)))
  1842. val &= (1 << width) - 1;
  1843. }
  1844. /* Now make the new constant. */
  1845. {
  1846. rtx new = gen_rtx (CONST_INT, VOIDmode, val);
  1847. return LEGITIMATE_CONSTANT_P (new) ? new : x;
  1848. }
  1849. }
  1850. /* Given an expression X which is used to set CC0,
  1851. return an integer recording (in the encoding used for prev_insn_cc0)
  1852. how the condition codes would be set by that expression.
  1853. Return 0 if the value is not constant
  1854. or if there is any doubt what condition codes result from it. */
  1855. static int
  1856. fold_cc0 (x)
  1857. rtx x;
  1858. {
  1859. if (GET_CODE (x) == MINUS && GET_MODE (x) == VOIDmode)
  1860. {
  1861. rtx y0 = fold_rtx (XEXP (x, 0), 0);
  1862. rtx y1 = fold_rtx (XEXP (x, 1), 0);
  1863. int u0, u1, s0, s1;
  1864. enum machine_mode m;
  1865. m = GET_MODE (y0);
  1866. if (m == VOIDmode)
  1867. m = GET_MODE (y1);
  1868. if (m == VOIDmode)
  1869. return 0;
  1870. if (GET_CODE (y0) == REG)
  1871. y0 = qty_const[reg_qty[REGNO (y0)]];
  1872. if (y0 == 0 || GET_CODE (y0) != CONST_INT)
  1873. return 0;
  1874. if (GET_CODE (y1) == REG)
  1875. y1 = qty_const[reg_qty[REGNO (y1)]];
  1876. if (y1 == 0 || GET_CODE (y1) != CONST_INT)
  1877. return 0;
  1878. s0 = u0 = INTVAL (y0);
  1879. s1 = u1 = INTVAL (y1);
  1880. {
  1881. int width = GET_MODE_BITSIZE (m);
  1882. if (width < HOST_BITS_PER_INT)
  1883. {
  1884. s0 = u0 &= ~ ((-1) << width);
  1885. s1 = u1 &= ~ ((-1) << width);
  1886. if (u0 & (1 << (width - 1)))
  1887. s0 |= ((-1) << width);
  1888. if (u1 & (1 << (width - 1)))
  1889. s1 |= ((-1) << width);
  1890. }
  1891. }
  1892. return 0100 + ((s0 < s1 ? 7 : s0 > s1) << 3)
  1893. + (((unsigned) u0 < (unsigned) u1) ? 7
  1894. : ((unsigned) u0 > (unsigned) u1));
  1895. }
  1896. {
  1897. rtx y0;
  1898. int u0, s0;
  1899. enum machine_mode m;
  1900. y0 = fold_rtx (x, 0);
  1901. m = GET_MODE (y0);
  1902. if (GET_CODE (y0) == REG)
  1903. y0 = qty_const[reg_qty[REGNO (y0)]];
  1904. if (y0 == 0 || GET_CODE (y0) != CONST_INT)
  1905. return 0;
  1906. s0 = u0 = INTVAL (y0);
  1907. if (m != VOIDmode)
  1908. {
  1909. int width = GET_MODE_BITSIZE (m);
  1910. if (width < HOST_BITS_PER_INT)
  1911. {
  1912. s0 = u0 &= ~ ((-1) << GET_MODE_BITSIZE (m));
  1913. if (u0 & (1 << (GET_MODE_BITSIZE (m) - 1)))
  1914. s0 |= ((-1) << GET_MODE_BITSIZE (m));
  1915. }
  1916. }
  1917. return 0100 + ((s0 < 0 ? 7 : s0 > 0) << 3) + (u0 != 0);
  1918. }
  1919. }
  1920. /* Attempt to prove that a loop will be executed >= 1 times,
  1921. or prove it will be executed 0 times.
  1922. If either can be proved, delete some of the code. */
  1923. static void
  1924. predecide_loop_entry (insn)
  1925. register rtx insn;
  1926. {
  1927. register rtx jump = NEXT_INSN (insn);
  1928. register rtx p = JUMP_LABEL (jump);
  1929. register rtx loop_top_label = NEXT_INSN (NEXT_INSN (jump));
  1930. enum { UNK, DELETE_LOOP, DELETE_JUMP } disposition = UNK;
  1931. int count = 0;
  1932. /* Trace the flow of control through the end test,
  1933. propagating constants, to see if result is determined. */
  1934. prev_insn_cc0 = 0;
  1935. /* Avoid infinite loop if we find a cycle of jumps. */
  1936. while (count < 10)
  1937. {
  1938. /* At end of function? Means rtl is inconsistent,
  1939. but this can happen when stmt.c gets confused
  1940. by a syntax error. */
  1941. if (p == 0)
  1942. break;
  1943. /* Arriving at end of loop means endtest will drop out. */
  1944. if (GET_CODE (p) == NOTE
  1945. && NOTE_LINE_NUMBER (p) == NOTE_INSN_LOOP_END)
  1946. {
  1947. disposition = DELETE_LOOP;
  1948. break;
  1949. }
  1950. else if (GET_CODE (p) == CODE_LABEL || GET_CODE (p) == NOTE)
  1951. ;
  1952. /* We only know how to handle two kinds of insns:
  1953. conditional jumps, and those that set the condition codes. */
  1954. else if (GET_CODE (p) == INSN && GET_CODE (PATTERN (p)) == SET
  1955. && SET_DEST (PATTERN (p)) == cc0_rtx)
  1956. {
  1957. prev_insn_cc0 = fold_cc0 (copy_rtx (SET_SRC (PATTERN (p))));
  1958. }
  1959. else if (GET_CODE (p) == JUMP_INSN
  1960. && GET_CODE (PATTERN (p)) == SET
  1961. && SET_DEST (PATTERN (p)) == pc_rtx)
  1962. {
  1963. register rtx target
  1964. = fold_rtx (SET_SRC (PATTERN (p)), 1);
  1965. if (GET_CODE (target) == LABEL_REF)
  1966. p = XEXP (target, 0);
  1967. else if (target != pc_rtx)
  1968. /* If destination of jump is not fixed, give up. */
  1969. break;
  1970. count++;
  1971. }
  1972. /* Any other kind of insn means we don't know
  1973. what result the test will have. */
  1974. else
  1975. break;
  1976. /* Arriving at top of loop means we can drop straight in.
  1977. Check here because we can arrive only via a jump insn
  1978. which would have changed P above. */
  1979. if (p == loop_top_label)
  1980. {
  1981. disposition = DELETE_JUMP;
  1982. break;
  1983. }
  1984. /* We went past one insn; consider the next. */
  1985. p = NEXT_INSN (p);
  1986. }
  1987. if (disposition == DELETE_JUMP)
  1988. {
  1989. /* We know the loop test will succeed the first time,
  1990. so delete the jump to the test; drop right into loop.
  1991. Note that one call to delete_insn gets the BARRIER as well. */
  1992. delete_insn (jump);
  1993. }
  1994. if (disposition == DELETE_LOOP)
  1995. {
  1996. /* We know the endtest will fail and drop right out of the loop,
  1997. but it isn't safe to delete the loop here.
  1998. There could be jumps into it from outside.
  1999. So make the entry-jump jump around the loop.
  2000. This will cause find_basic_blocks to delete it if appropriate. */
  2001. register rtx label = gen_label_rtx ();
  2002. emit_label_after (label, p);
  2003. redirect_jump (jump, label);
  2004. }
  2005. }
  2006. /* CSE processing for one instruction.
  2007. First simplify sources and addresses of all assignments
  2008. in the instruction, using previously-computed equivalents values.
  2009. Then install the new sources and destinations in the table
  2010. of available values. */
  2011. static rtx set[MAX_SETS_PER_INSN];
  2012. static struct table_elt *src_elt[MAX_SETS_PER_INSN];
  2013. static int src_hash_code[MAX_SETS_PER_INSN];
  2014. static int dest_hash_code[MAX_SETS_PER_INSN];
  2015. static char src_in_memory[MAX_SETS_PER_INSN];
  2016. static char src_in_struct[MAX_SETS_PER_INSN];
  2017. static rtx inner_dest[MAX_SETS_PER_INSN];
  2018. static char src_volatile[MAX_SETS_PER_INSN];
  2019. static void
  2020. cse_insn (insn)
  2021. rtx insn;
  2022. {
  2023. register rtx x = PATTERN (insn);
  2024. register int i;
  2025. register int n_sets = 0;
  2026. /* Records what this insn does to set CC0,
  2027. using same encoding used for prev_insn_cc0. */
  2028. int this_insn_cc0 = 0;
  2029. struct write_data writes_memory;
  2030. static struct write_data init = {0, 0, 0};
  2031. rtx src_eqv = 0;
  2032. struct table_elt *src_eqv_elt = 0;
  2033. int src_eqv_in_memory;
  2034. int src_eqv_in_struct;
  2035. int src_eqv_volatile = 0;
  2036. int src_eqv_hash_code;
  2037. this_insn = insn;
  2038. writes_memory = init;
  2039. /* Find all the SETs and CLOBBERs in this instruction.
  2040. Record all the SETs in the array `set' and count them.
  2041. Also determine whether there is a CLOBBER that invalidates
  2042. all memory references, or all references at varying addresses. */
  2043. if (GET_CODE (x) == SET)
  2044. {
  2045. rtx tem;
  2046. n_sets = 1;
  2047. set[0] = x;
  2048. tem = find_reg_note (insn, REG_EQUIV, 0);
  2049. if (tem == 0)
  2050. tem = find_reg_note (insn, REG_EQUAL, 0);
  2051. if (tem) src_eqv = XEXP (tem, 0);
  2052. /* Return now for unconditional jumps.
  2053. They never need cse processing, so this does not hurt.
  2054. The reason is not efficiency but rather
  2055. so that we can test at the end for instructions
  2056. that have been simplified to unconditional jumps
  2057. and not be misled by unchanged instructions
  2058. that were unconditional jumps to begin with. */
  2059. if (SET_DEST (x) == pc_rtx
  2060. && GET_CODE (SET_SRC (x)) == LABEL_REF)
  2061. return;
  2062. }
  2063. else if (GET_CODE (x) == PARALLEL)
  2064. {
  2065. register int lim = XVECLEN (x, 0);
  2066. for (i = 0; i < lim; i++)
  2067. {
  2068. register rtx y = XVECEXP (x, 0, i);
  2069. if (GET_CODE (y) == SET)
  2070. set[n_sets++] = y;
  2071. else if (GET_CODE (y) == CLOBBER)
  2072. note_mem_written (XEXP (y, 0), &writes_memory);
  2073. else if (GET_CODE (y) == CALL)
  2074. canon_reg (y);
  2075. }
  2076. }
  2077. else if (GET_CODE (x) == CLOBBER)
  2078. note_mem_written (XEXP (x, 0), &writes_memory);
  2079. else if (GET_CODE (x) == CALL)
  2080. canon_reg (x);
  2081. if (n_sets == 0)
  2082. {
  2083. invalidate_from_clobbers (&writes_memory, x);
  2084. return;
  2085. }
  2086. /* Canonicalize sources and addresses of destinations.
  2087. set src_elt[i] to the class each source belongs to.
  2088. Detect assignments from or to volatile things
  2089. and set set[i] to zero so they will be ignored
  2090. in the rest of this function.
  2091. Nothing in this loop changes the hash table or the register chains. */
  2092. for (i = 0; i < n_sets; i++)
  2093. {
  2094. register rtx src, dest;
  2095. register struct table_elt *elt;
  2096. enum machine_mode mode;
  2097. dest = SET_DEST (set[i]);
  2098. src = SET_SRC (set[i]);
  2099. /* If SRC is a constant that has no machine mode,
  2100. hash it with the destination's machine mode.
  2101. This way we can keep different modes separate. */
  2102. mode = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
  2103. /* Replace each registers in SRC with oldest equivalent register,
  2104. but if DEST is a register do not replace it if it appears in SRC. */
  2105. if (GET_CODE (dest) == REG)
  2106. {
  2107. int tem = reg_qty[REGNO (dest)];
  2108. reg_qty[REGNO (dest)] = REGNO (dest);
  2109. src = canon_reg (src);
  2110. if (src_eqv)
  2111. src_eqv = canon_reg (src_eqv);
  2112. reg_qty[REGNO (dest)] = tem;
  2113. }
  2114. else
  2115. {
  2116. src = canon_reg (src);
  2117. if (src_eqv)
  2118. src_eqv = canon_reg (src_eqv);
  2119. }
  2120. if (src_eqv)
  2121. {
  2122. enum machine_mode eqvmode = mode;
  2123. if (GET_CODE (dest) == STRICT_LOW_PART)
  2124. eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
  2125. do_not_record = 0;
  2126. hash_arg_in_memory = 0;
  2127. hash_arg_in_struct = 0;
  2128. src_eqv = fold_rtx (src_eqv, 0);
  2129. src_eqv_hash_code = HASH (src_eqv, eqvmode);
  2130. /* Replace the src_eqv with its cheapest equivalent. */
  2131. if (!do_not_record)
  2132. {
  2133. elt = lookup (src_eqv, src_eqv_hash_code, eqvmode);
  2134. if (elt && elt != elt->first_same_value)
  2135. {
  2136. elt = elt->first_same_value;
  2137. /* Find the cheapest one that is still valid. */
  2138. while ((GET_CODE (elt->exp) != REG
  2139. && !exp_equiv_p (elt->exp, elt->exp, 1))
  2140. || elt->equivalence_only)
  2141. elt = elt->next_same_value;
  2142. src_eqv = copy_rtx (elt->exp);
  2143. hash_arg_in_memory = 0;
  2144. hash_arg_in_struct = 0;
  2145. src_eqv_hash_code = HASH (src_eqv, elt->mode);
  2146. }
  2147. src_eqv_elt = elt;
  2148. }
  2149. else
  2150. src_eqv = 0;
  2151. src_eqv_in_memory = hash_arg_in_memory;
  2152. src_eqv_in_struct = hash_arg_in_struct;
  2153. }
  2154. /* Compute SRC's hash code, and also notice if it
  2155. should not be recorded at all. In that case,
  2156. prevent any further processing of this assignment. */
  2157. do_not_record = 0;
  2158. hash_arg_in_memory = 0;
  2159. hash_arg_in_struct = 0;
  2160. src = fold_rtx (src, 0);
  2161. /* If we have (NOT Y), see if Y is known to be (NOT Z).
  2162. If so, (NOT Y) simplifies to Z. */
  2163. if (GET_CODE (src) == NOT || GET_CODE (src) == NEG)
  2164. {
  2165. rtx y = lookup_as_function (XEXP (src, 0), GET_CODE (src));
  2166. if (y != 0)
  2167. src = y;
  2168. }
  2169. /* If storing a constant value in a register that
  2170. previously held the constant value 0,
  2171. record this fact with a REG_WAS_0 note on this insn. */
  2172. if (GET_CODE (src) == CONST_INT
  2173. && GET_CODE (dest) == REG
  2174. && n_sets == 0
  2175. && qty_const[reg_qty[REGNO (dest)]] == const0_rtx)
  2176. REG_NOTES (insn) = gen_rtx (INSN_LIST, REG_WAS_0,
  2177. qty_const_insn[reg_qty[REGNO (dest)]],
  2178. REG_NOTES (insn));
  2179. src_hash_code[i] = HASH (src, mode);
  2180. src_volatile[i] = do_not_record;
  2181. #if 0
  2182. /* This code caused multiple hash-table entries
  2183. to be created for registers. Invalidation
  2184. would only get one, leaving others that didn't belong.
  2185. I don't know what good this ever did. */
  2186. if (GET_CODE (src) == REG)
  2187. {
  2188. src_in_memory[i] = 0;
  2189. src_elt[i] = 0;
  2190. }
  2191. else ...;
  2192. #endif
  2193. /* If source is a perverse subreg (such as QI treated as an SI),
  2194. treat it as volatile. It may do the work of an SI in one context
  2195. where the extra bits are not being used, but cannot replace an SI
  2196. in general. */
  2197. if (GET_CODE (src) == SUBREG
  2198. && (GET_MODE_SIZE (GET_MODE (src))
  2199. > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
  2200. src_volatile[i] = 1;
  2201. else if (!src_volatile[i])
  2202. {
  2203. /* Replace the source with its cheapest equivalent. */
  2204. elt = lookup (src, src_hash_code[i], mode);
  2205. if (elt && elt != elt->first_same_value)
  2206. {
  2207. elt = elt->first_same_value;
  2208. /* Find the cheapest one that is still valid. */
  2209. while ((GET_CODE (elt->exp) != REG
  2210. && !exp_equiv_p (elt->exp, elt->exp, 1))
  2211. || elt->equivalence_only)
  2212. elt = elt->next_same_value;
  2213. src = copy_rtx (elt->exp);
  2214. hash_arg_in_memory = 0;
  2215. hash_arg_in_struct = 0;
  2216. src_hash_code[i] = HASH (src, elt->mode);
  2217. }
  2218. /* If ELT is a constant, is there a register
  2219. linearly related to it? If so, replace it
  2220. with the sum of that register plus an offset. */
  2221. if (GET_CODE (src) == CONST && n_sets == 1
  2222. && SET_DEST (set[i]) != cc0_rtx)
  2223. {
  2224. rtx newsrc = use_related_value (src, elt);
  2225. if (newsrc == 0 && src_eqv != 0)
  2226. newsrc = use_related_value (src_eqv, src_eqv_elt);
  2227. if (newsrc)
  2228. {
  2229. rtx oldsrc = src;
  2230. src = newsrc;
  2231. hash_arg_in_memory = 0;
  2232. hash_arg_in_struct = 0;
  2233. src_hash_code[i] = HASH (src, GET_MODE (src));
  2234. /* The new expression for the SRC has the same value
  2235. as the previous one; so if the previous one is in
  2236. the hash table, put the new one in as equivalent. */
  2237. if (elt != 0)
  2238. elt = insert (src, elt->first_same_value, src_hash_code[i],
  2239. elt->mode);
  2240. else
  2241. {
  2242. /* Maybe the new expression is in the table already. */
  2243. elt = lookup (src, src_hash_code[i], mode);
  2244. /* And maybe a register contains the same value. */
  2245. if (elt && elt != elt->first_same_value)
  2246. {
  2247. elt = elt->first_same_value;
  2248. /* Find the cheapest one that is still valid. */
  2249. while ((GET_CODE (elt->exp) != REG
  2250. && !exp_equiv_p (elt->exp, elt->exp, 1))
  2251. || elt->equivalence_only)
  2252. elt = elt->next_same_value;
  2253. src = copy_rtx (elt->exp);
  2254. hash_arg_in_memory = 0;
  2255. hash_arg_in_struct = 0;
  2256. src_hash_code[i] = HASH (src, elt->mode);
  2257. }
  2258. }
  2259. /* This would normally be inhibited by the REG_EQUIV
  2260. note we are about to make. */
  2261. SET_SRC (set[i]) = src;
  2262. /* Record the actual constant value in a REG_EQUIV note. */
  2263. if (GET_CODE (SET_DEST (set[i])) == REG)
  2264. REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUIV,
  2265. oldsrc, 0);
  2266. }
  2267. }
  2268. src_elt[i] = elt;
  2269. src_in_memory[i] = hash_arg_in_memory;
  2270. src_in_struct[i] = hash_arg_in_struct;
  2271. }
  2272. /* Either canon_reg or the copy_rtx may have changed this. */
  2273. /* Note it is not safe to replace the sources if there
  2274. is more than one set. We could get an insn
  2275. [(set (reg) (reg)) (set (reg) (reg))], which is probably
  2276. not in the machine description.
  2277. This case we could handle by breaking into several insns.
  2278. Cases of partial substitution cannot win at all. */
  2279. /* Also, if this insn is setting a "constant" register,
  2280. we may not replace the value that is given to it. */
  2281. if (n_sets == 1)
  2282. if (REG_NOTES (insn) == 0
  2283. || REG_NOTE_KIND (REG_NOTES (insn)) != REG_EQUIV)
  2284. SET_SRC (set[i]) = src;
  2285. do_not_record = 0;
  2286. /* Look within any SIGN_EXTRACT or ZERO_EXTRACT
  2287. to the MEM or REG within it. */
  2288. while (1)
  2289. {
  2290. if (GET_CODE (dest) == SIGN_EXTRACT
  2291. || GET_CODE (dest) == ZERO_EXTRACT)
  2292. {
  2293. XEXP (dest, 1) = canon_reg (XEXP (dest, 1));
  2294. XEXP (dest, 2) = canon_reg (XEXP (dest, 2));
  2295. dest = XEXP (dest, 0);
  2296. }
  2297. else if (GET_CODE (dest) == SUBREG
  2298. || GET_CODE (dest) == STRICT_LOW_PART)
  2299. dest = XEXP (dest, 0);
  2300. else
  2301. break;
  2302. }
  2303. inner_dest[i] = dest;
  2304. /* If storing into memory, do cse on the memory address.
  2305. Also compute the hash code of the destination now,
  2306. before the effects of this instruction are recorded,
  2307. since the register values used in the address computation
  2308. are those before this instruction. */
  2309. if (GET_CODE (dest) == MEM)
  2310. {
  2311. register rtx addr;
  2312. register int hash;
  2313. canon_reg (dest);
  2314. dest = fold_rtx (dest, 0);
  2315. addr = XEXP (dest, 0);
  2316. /* Pushing or popping does not invalidate anything. */
  2317. if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
  2318. || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
  2319. && GET_CODE (XEXP (addr, 0)) == REG
  2320. && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
  2321. ;
  2322. else
  2323. /* Otherwise, decide whether we invalidate
  2324. everything in memory, or just things at non-fixed places.
  2325. Writing a large aggregate must invalidate everything
  2326. because we don't know how long it is. */
  2327. note_mem_written (dest, &writes_memory);
  2328. /* Do not consider addresses of local and argument slots.
  2329. The MEM expressions for args and non-register local variables
  2330. are made only once and inserted in many instructions,
  2331. as well as being used to control symbol table output.
  2332. It is not safe to clobber them. */
  2333. if ((GET_CODE (addr) == PLUS
  2334. && GET_CODE (XEXP (addr, 0)) == REG
  2335. && GET_CODE (XEXP (addr, 1)) == CONST_INT
  2336. && (hash = REGNO (XEXP (addr, 0)),
  2337. hash == FRAME_POINTER_REGNUM || hash == ARG_POINTER_REGNUM))
  2338. || (GET_CODE (addr) == REG
  2339. && (hash = REGNO (addr),
  2340. hash == FRAME_POINTER_REGNUM || hash == ARG_POINTER_REGNUM)))
  2341. dest_hash_code[i] = safe_hash (dest) % NBUCKETS;
  2342. else
  2343. {
  2344. /* Look for a simpler equivalent for the destination address. */
  2345. hash = HASH (addr, Pmode);
  2346. if (! do_not_record)
  2347. {
  2348. elt = lookup (addr, hash, Pmode);
  2349. dest_hash_code[i] = ((int) MEM + hash) % NBUCKETS;
  2350. if (elt && elt != elt->first_same_value)
  2351. {
  2352. elt = elt->first_same_value;
  2353. /* Find the cheapest one that is still valid. */
  2354. while ((GET_CODE (elt->exp) != REG
  2355. && !exp_equiv_p (elt->exp, elt->exp, 1))
  2356. || elt->equivalence_only)
  2357. elt = elt->next_same_value;
  2358. addr = copy_rtx (elt->exp);
  2359. /* Create a new MEM rtx, in case the old one
  2360. is shared somewhere else. */
  2361. dest = gen_rtx (MEM, GET_MODE (dest), addr);
  2362. dest->volatil = inner_dest[i]->volatil;
  2363. SET_DEST (set[i]) = dest;
  2364. inner_dest[i] = dest;
  2365. }
  2366. }
  2367. }
  2368. }
  2369. /* Don't enter a bit-field in the hash table
  2370. because the value in it after the store
  2371. may not equal what was stored, due to truncation. */
  2372. if (GET_CODE (SET_DEST (set[i])) == ZERO_EXTRACT
  2373. || GET_CODE (SET_DEST (set[i])) == SIGN_EXTRACT)
  2374. src_volatile[i] = 1, src_eqv = 0;
  2375. /* No further processing for this assignment
  2376. if destination is volatile. */
  2377. else if (do_not_record
  2378. || (GET_CODE (dest) == REG
  2379. ? REGNO (dest) == STACK_POINTER_REGNUM
  2380. : GET_CODE (dest) != MEM))
  2381. set[i] = 0;
  2382. if (set[i] != 0 && dest != SET_DEST (set[i]))
  2383. dest_hash_code[i] = HASH (SET_DEST (set[i]), mode);
  2384. if (dest == cc0_rtx
  2385. && (GET_CODE (src) == MINUS
  2386. || CONSTANT_P (src)
  2387. || GET_CODE (src) == REG))
  2388. this_insn_cc0 = fold_cc0 (src);
  2389. }
  2390. /* Now enter all non-volatile source expressions in the hash table
  2391. if they are not already present.
  2392. Record in src_elt the heads of their equivalence classes.
  2393. This way we can insert the corresponding destinations into
  2394. the same classes even if the actual sources are no longer in them
  2395. (having been invalidated). */
  2396. if (src_eqv && src_eqv_elt == 0 && set[0] != 0)
  2397. {
  2398. register struct table_elt *elt;
  2399. rtx dest = SET_DEST (set[0]);
  2400. enum machine_mode eqvmode = GET_MODE (dest);
  2401. if (GET_CODE (dest) == STRICT_LOW_PART)
  2402. eqvmode = GET_MODE (SUBREG_REG (XEXP (dest, 0)));
  2403. if (insert_regs (src_eqv, 0, 0))
  2404. src_eqv_hash_code = HASH (src_eqv, eqvmode);
  2405. elt = insert (src_eqv, 0, src_eqv_hash_code, eqvmode);
  2406. elt->in_memory = src_eqv_in_memory;
  2407. elt->in_struct = src_eqv_in_struct;
  2408. elt->equivalence_only = 1;
  2409. src_eqv_elt = elt->first_same_value;
  2410. }
  2411. for (i = 0; i < n_sets; i++)
  2412. if (set[i] && ! src_volatile[i])
  2413. {
  2414. if (GET_CODE (SET_DEST (set[i])) == STRICT_LOW_PART)
  2415. {
  2416. src_elt[i] = src_eqv_elt;
  2417. src_hash_code[i] = src_eqv_hash_code;
  2418. }
  2419. else if (src_elt[i] == 0)
  2420. {
  2421. register rtx src = SET_SRC (set[i]);
  2422. register rtx dest = SET_DEST (set[i]);
  2423. register struct table_elt *elt;
  2424. enum machine_mode mode
  2425. = GET_MODE (src) == VOIDmode ? GET_MODE (dest) : GET_MODE (src);
  2426. /* Note that these insert_regs calls cannot remove
  2427. any of the src_elt's, because they would have failed to match
  2428. if not still valid. */
  2429. if (insert_regs (src, 0, 0))
  2430. src_hash_code[i] = HASH (src, mode);
  2431. elt = insert (src, src_eqv_elt, src_hash_code[i], mode);
  2432. elt->in_memory = src_in_memory[i];
  2433. elt->in_struct = src_in_struct[i];
  2434. src_elt[i] = elt->first_same_value;
  2435. }
  2436. }
  2437. invalidate_from_clobbers (&writes_memory, x);
  2438. /* Now invalidate everything set by this instruction.
  2439. If a SUBREG or other funny destination is being set,
  2440. set[i] is still nonzero, so here we invalidate the reg
  2441. a part of which is being set. */
  2442. for (i = 0; i < n_sets; i++)
  2443. if (set[i])
  2444. {
  2445. register rtx dest = inner_dest[i];
  2446. /* Needed for registers to remove the register from its
  2447. previous quantity's chain.
  2448. Needed for memory if this is a nonvarying address, unless
  2449. we have just done an invalidate_memory that covers even those. */
  2450. if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG
  2451. || (! writes_memory.all && ! cse_rtx_addr_varies_p (dest)))
  2452. invalidate (dest);
  2453. }
  2454. /* Make sure registers mentioned in destinations
  2455. are safe for use in an expression to be inserted.
  2456. This removes from the hash table
  2457. any invalid entry that refers to one of these registers. */
  2458. for (i = 0; i < n_sets; i++)
  2459. if (set[i] && GET_CODE (SET_DEST (set[i])) != REG)
  2460. mention_regs (SET_DEST (set[i]));
  2461. /* We may have just removed some of the src_elt's from the hash table.
  2462. So replace each one with the current head of the same class. */
  2463. for (i = 0; i < n_sets; i++)
  2464. if (set[i])
  2465. {
  2466. /* If the source is volatile, its destination goes in
  2467. a class of its own. */
  2468. if (src_volatile[i])
  2469. src_elt[i] = 0;
  2470. if (src_elt[i] && src_elt[i]->first_same_value == 0)
  2471. /* If elt was removed, find current head of same class,
  2472. or 0 if nothing remains of that class. */
  2473. {
  2474. register struct table_elt *elt = src_elt[i];
  2475. while (elt && elt->first_same_value == 0)
  2476. elt = elt->next_same_value;
  2477. src_elt[i] = elt ? elt->first_same_value : 0;
  2478. }
  2479. }
  2480. /* Now insert the destinations into their equivalence classes. */
  2481. for (i = 0; i < n_sets; i++)
  2482. if (set[i])
  2483. {
  2484. register rtx dest = SET_DEST (set[i]);
  2485. register struct table_elt *elt;
  2486. /* STRICT_LOW_PART isn't part of the value BEING set,
  2487. and neither is the SUBREG inside it.
  2488. Note that in this case SRC_ELT[I] is really SRC_EQV_ELT. */
  2489. if (GET_CODE (dest) == STRICT_LOW_PART)
  2490. dest = SUBREG_REG (XEXP (dest, 0));
  2491. if (GET_CODE (dest) == REG || GET_CODE (dest) == SUBREG)
  2492. /* Registers must also be inserted into chains for quantities. */
  2493. if (insert_regs (dest, src_elt[i], 1))
  2494. /* If `insert_regs' changes something, the hash code must be
  2495. recalculated. */
  2496. dest_hash_code[i] = safe_hash (dest) % NBUCKETS;
  2497. elt = insert (dest, src_elt[i], dest_hash_code[i], GET_MODE (dest));
  2498. elt->in_memory = GET_CODE (inner_dest[i]) == MEM;
  2499. if (elt->in_memory)
  2500. {
  2501. elt->in_struct = (inner_dest[i]->in_struct
  2502. || inner_dest[i] != SET_DEST (set[i]));
  2503. }
  2504. }
  2505. /* Special handling for (set REG0 REG1)
  2506. where REG0 is the "cheapest", cheaper than REG1.
  2507. After cse, REG1 will probably not be used in the sequel,
  2508. so (if easily done) change this insn to (set REG1 REG0) and
  2509. replace REG1 with REG0 in the previous insn that computed their value.
  2510. Then REG1 will become a dead store and won't cloud the situation
  2511. for later optimizations. */
  2512. if (n_sets == 1 && set[0] && GET_CODE (SET_DEST (set[0])) == REG
  2513. && GET_CODE (SET_SRC (set[0])) == REG
  2514. && rtx_equal_p (canon_reg (SET_SRC (set[0])), SET_DEST (set[0])))
  2515. {
  2516. rtx prev = PREV_INSN (insn);
  2517. while (prev && GET_CODE (prev) == NOTE)
  2518. prev = PREV_INSN (prev);
  2519. if (prev && GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SET
  2520. && SET_DEST (PATTERN (prev)) == SET_SRC (set[0]))
  2521. {
  2522. rtx dest = SET_DEST (set[0]);
  2523. SET_DEST (PATTERN (prev)) = dest;
  2524. SET_DEST (set[0]) = SET_SRC (set[0]);
  2525. SET_SRC (set[0]) = dest;
  2526. }
  2527. }
  2528. /* Did this insn become an unconditional branch or become a no-op? */
  2529. if (GET_CODE (insn) == JUMP_INSN
  2530. && GET_CODE (x) == SET
  2531. && SET_DEST (x) == pc_rtx)
  2532. {
  2533. if (SET_SRC (x) == pc_rtx)
  2534. {
  2535. PUT_CODE (insn, NOTE);
  2536. NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  2537. NOTE_SOURCE_FILE (insn) = 0;
  2538. cse_jumps_altered = 1;
  2539. /* If previous insn just set CC0 for us, delete it too. */
  2540. if (prev_insn_cc0 != 0)
  2541. {
  2542. PUT_CODE (prev_insn, NOTE);
  2543. NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
  2544. NOTE_SOURCE_FILE (prev_insn) = 0;
  2545. }
  2546. }
  2547. else if (GET_CODE (SET_SRC (x)) == LABEL_REF)
  2548. {
  2549. emit_barrier_after (insn);
  2550. cse_jumps_altered = 1;
  2551. /* If previous insn just set CC0 for us, delete it too. */
  2552. if (prev_insn_cc0 != 0)
  2553. {
  2554. PUT_CODE (prev_insn, NOTE);
  2555. NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
  2556. NOTE_SOURCE_FILE (prev_insn) = 0;
  2557. }
  2558. }
  2559. }
  2560. /* If this insn used to store a value based on CC0 but now value is constant,
  2561. and the previous insn just set CC0 for us, delete previous insn.
  2562. Here we use the fact that nothing expects CC0 to be valid over an insn,
  2563. which is true until the final pass. */
  2564. if (GET_CODE (x) == SET && prev_insn_cc0
  2565. && CONSTANT_P (SET_SRC (x)))
  2566. {
  2567. PUT_CODE (prev_insn, NOTE);
  2568. NOTE_LINE_NUMBER (prev_insn) = NOTE_INSN_DELETED;
  2569. NOTE_SOURCE_FILE (prev_insn) = 0;
  2570. }
  2571. prev_insn_cc0 = this_insn_cc0;
  2572. prev_insn = insn;
  2573. }
  2574. /* Store 1 in *WRITES_PTR for those categories of memory ref
  2575. that must be invalidated when the expression WRITTEN is stored in.
  2576. If WRITTEN is null, say everything must be invalidated. */
  2577. static void
  2578. note_mem_written (written, writes_ptr)
  2579. rtx written;
  2580. struct write_data *writes_ptr;
  2581. {
  2582. static struct write_data everything = {1, 1, 1};
  2583. if (written == 0)
  2584. *writes_ptr = everything;
  2585. else if (GET_CODE (written) == MEM)
  2586. {
  2587. /* Pushing or popping the stack invalidates nothing. */
  2588. rtx addr = XEXP (written, 0);
  2589. if ((GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == PRE_INC
  2590. || GET_CODE (addr) == POST_DEC || GET_CODE (addr) == POST_INC)
  2591. && GET_CODE (XEXP (addr, 0)) == REG
  2592. && REGNO (XEXP (addr, 0)) == STACK_POINTER_REGNUM)
  2593. return;
  2594. if (GET_MODE (written) == BLKmode)
  2595. *writes_ptr = everything;
  2596. else if (cse_rtx_addr_varies_p (written))
  2597. {
  2598. /* A varying address that is a sum indicates an array element,
  2599. and that's just as good as a structure element
  2600. in implying that we need not invalidate scalar variables. */
  2601. if (!(written->in_struct
  2602. || GET_CODE (XEXP (written, 0)) == PLUS))
  2603. writes_ptr->all = 1;
  2604. writes_ptr->nonscalar = 1;
  2605. }
  2606. writes_ptr->var = 1;
  2607. }
  2608. }
  2609. /* Perform invalidation on the basis of everything about an insn
  2610. except for invalidating the actual places that are SET in it.
  2611. This includes the places CLOBBERed, and anything that might
  2612. alias with something that is SET or CLOBBERed.
  2613. W points to the writes_memory for this insn, a struct write_data
  2614. saying which kinds of memory references must be invalidated.
  2615. X is the pattern of the insn. */
  2616. static void
  2617. invalidate_from_clobbers (w, x)
  2618. struct write_data *w;
  2619. rtx x;
  2620. {
  2621. /* If W->var is not set, W specifies no action.
  2622. If W->all is set, this step gets all memory refs
  2623. so they can be ignored in the rest of this function. */
  2624. if (w->var)
  2625. invalidate_memory (w);
  2626. if (GET_CODE (x) == CLOBBER)
  2627. {
  2628. rtx ref = XEXP (x, 0);
  2629. if (ref
  2630. && (GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
  2631. || (GET_CODE (ref) == MEM && ! w->all)))
  2632. invalidate (ref);
  2633. }
  2634. else if (GET_CODE (x) == PARALLEL)
  2635. {
  2636. register int i;
  2637. for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  2638. {
  2639. register rtx y = XVECEXP (x, 0, i);
  2640. if (GET_CODE (y) == CLOBBER)
  2641. {
  2642. rtx ref = XEXP (y, 0);
  2643. if (ref
  2644. &&(GET_CODE (ref) == REG || GET_CODE (ref) == SUBREG
  2645. || (GET_CODE (ref) == MEM && !w->all)))
  2646. invalidate (ref);
  2647. }
  2648. }
  2649. }
  2650. }
  2651. static void cse_basic_block ();
  2652. /* Perform cse on the instructions of a function.
  2653. F is the first instruction.
  2654. NREGS is one plus the highest pseudo-reg number used in the instruction.
  2655. Returns 1 if jump_optimize should be redone due to simplifications
  2656. in conditional jump instructions. */
  2657. int
  2658. cse_main (f, nregs)
  2659. /* f is the first instruction of a chain of insns for one function */
  2660. rtx f;
  2661. /* nregs is the total number of registers used in it */
  2662. int nregs;
  2663. {
  2664. register rtx insn = f;
  2665. register int i;
  2666. cse_jumps_altered = 0;
  2667. init_recog ();
  2668. max_reg = nregs;
  2669. all_minus_one = (int *) alloca (nregs * sizeof (int));
  2670. consec_ints = (int *) alloca (nregs * sizeof (int));
  2671. for (i = 0; i < nregs; i++)
  2672. {
  2673. all_minus_one[i] = -1;
  2674. consec_ints[i] = i;
  2675. }
  2676. reg_next_eqv = (int *) alloca (nregs * sizeof (int));
  2677. reg_prev_eqv = (int *) alloca (nregs * sizeof (int));
  2678. reg_qty = (int *) alloca (nregs * sizeof (int));
  2679. reg_rtx = (rtx *) alloca (nregs * sizeof (rtx));
  2680. reg_in_table = (int *) alloca (nregs * sizeof (int));
  2681. reg_tick = (int *) alloca (nregs * sizeof (int));
  2682. /* Discard all the free elements of the previous function
  2683. since they are allocated in the temporarily obstack. */
  2684. bzero (table, sizeof table);
  2685. free_element_chain = 0;
  2686. n_elements_made = 0;
  2687. /* Loop over basic blocks */
  2688. while (insn)
  2689. {
  2690. register rtx p = insn;
  2691. register int i = 0;
  2692. register int last_uid;
  2693. /* Find end of next basic block */
  2694. while (p && GET_CODE (p) != CODE_LABEL)
  2695. {
  2696. /* Don't cse out the end of a loop. This makes a difference
  2697. only for the unusual loops that always execute at least once;
  2698. all other loops have labels there so we will stop in any case.
  2699. Cse'ing out the end of the loop is dangerous because it
  2700. might cause an invariant expression inside the loop
  2701. to be reused after the end of the loop. This would make it
  2702. hard to move the expression out of the loop in loop.c,
  2703. especially if it is one of several equivalent expressions
  2704. and loop.c would like to eliminate it.
  2705. The occasional optimizations lost by this will all come back
  2706. if loop and cse are made to work alternatingly. */
  2707. if (GET_CODE (p) == NOTE
  2708. && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_END)
  2709. break;
  2710. last_uid = INSN_UID (p);
  2711. p = NEXT_INSN (p);
  2712. i++;
  2713. }
  2714. cse_basic_block_end = last_uid;
  2715. cse_basic_block_start = INSN_UID (insn);
  2716. max_qty = max_reg + i * MAX_SETS_PER_INSN;
  2717. cse_basic_block (insn, p);
  2718. insn = p ? NEXT_INSN (p) : 0;
  2719. }
  2720. /* Tell refers_to_mem_p that qty_const info is not available. */
  2721. qty_const = 0;
  2722. if (max_elements_made < n_elements_made)
  2723. max_elements_made = n_elements_made;
  2724. return cse_jumps_altered;
  2725. }
  2726. static void
  2727. cse_basic_block (from, to)
  2728. register rtx from, to;
  2729. {
  2730. register rtx insn;
  2731. int *qv1 = (int *) alloca (max_qty * sizeof (int));
  2732. int *qv2 = (int *) alloca (max_qty * sizeof (int));
  2733. rtx *qv3 = (rtx *) alloca (max_qty * sizeof (rtx));
  2734. qty_first_reg = qv1;
  2735. qty_last_reg = qv2;
  2736. qty_const = qv3;
  2737. qty_const_insn = (rtx *) alloca (max_qty * sizeof (rtx));
  2738. new_basic_block ();
  2739. for (insn = from; insn != to; insn = NEXT_INSN (insn))
  2740. {
  2741. register RTX_CODE code = GET_CODE (insn);
  2742. if (code == INSN || code == JUMP_INSN || code == CALL_INSN)
  2743. cse_insn (insn);
  2744. /* Memory, and some registers, are invalidate by subroutine calls. */
  2745. if (code == CALL_INSN)
  2746. {
  2747. register int i;
  2748. static struct write_data everything = {1, 1, 1};
  2749. invalidate_memory (&everything);
  2750. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  2751. if (call_used_regs[i] && reg_rtx[i]
  2752. && i != FRAME_POINTER_REGNUM
  2753. && i != ARG_POINTER_REGNUM)
  2754. invalidate (reg_rtx[i]);
  2755. }
  2756. /* Loop beginnings are often followed by jumps
  2757. (that enter the loop above the endtest).
  2758. See if we can prove the loop will be executed at least once;
  2759. if so, delete the jump. Also perhaps we can prove loop
  2760. will never be executed and delete the entire thing. */
  2761. if (code == NOTE
  2762. && NOTE_LINE_NUMBER (insn) == NOTE_INSN_LOOP_BEG
  2763. && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN)
  2764. {
  2765. predecide_loop_entry (insn);
  2766. /* Whether that jump was deleted or not,
  2767. it certainly is the end of the basic block.
  2768. Since the jump is unconditional,
  2769. it requires no further processing here. */
  2770. break;
  2771. }
  2772. }
  2773. if (next_qty > max_qty)
  2774. abort ();
  2775. }