reg-stack.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381
  1. /* Register to Stack convert for GNU compiler.
  2. Copyright (C) 1992-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This pass converts stack-like registers from the "flat register
  16. file" model that gcc uses, to a stack convention that the 387 uses.
  17. * The form of the input:
  18. On input, the function consists of insn that have had their
  19. registers fully allocated to a set of "virtual" registers. Note that
  20. the word "virtual" is used differently here than elsewhere in gcc: for
  21. each virtual stack reg, there is a hard reg, but the mapping between
  22. them is not known until this pass is run. On output, hard register
  23. numbers have been substituted, and various pop and exchange insns have
  24. been emitted. The hard register numbers and the virtual register
  25. numbers completely overlap - before this pass, all stack register
  26. numbers are virtual, and afterward they are all hard.
  27. The virtual registers can be manipulated normally by gcc, and their
  28. semantics are the same as for normal registers. After the hard
  29. register numbers are substituted, the semantics of an insn containing
  30. stack-like regs are not the same as for an insn with normal regs: for
  31. instance, it is not safe to delete an insn that appears to be a no-op
  32. move. In general, no insn containing hard regs should be changed
  33. after this pass is done.
  34. * The form of the output:
  35. After this pass, hard register numbers represent the distance from
  36. the current top of stack to the desired register. A reference to
  37. FIRST_STACK_REG references the top of stack, FIRST_STACK_REG + 1,
  38. represents the register just below that, and so forth. Also, REG_DEAD
  39. notes indicate whether or not a stack register should be popped.
  40. A "swap" insn looks like a parallel of two patterns, where each
  41. pattern is a SET: one sets A to B, the other B to A.
  42. A "push" or "load" insn is a SET whose SET_DEST is FIRST_STACK_REG
  43. and whose SET_DEST is REG or MEM. Any other SET_DEST, such as PLUS,
  44. will replace the existing stack top, not push a new value.
  45. A store insn is a SET whose SET_DEST is FIRST_STACK_REG, and whose
  46. SET_SRC is REG or MEM.
  47. The case where the SET_SRC and SET_DEST are both FIRST_STACK_REG
  48. appears ambiguous. As a special case, the presence of a REG_DEAD note
  49. for FIRST_STACK_REG differentiates between a load insn and a pop.
  50. If a REG_DEAD is present, the insn represents a "pop" that discards
  51. the top of the register stack. If there is no REG_DEAD note, then the
  52. insn represents a "dup" or a push of the current top of stack onto the
  53. stack.
  54. * Methodology:
  55. Existing REG_DEAD and REG_UNUSED notes for stack registers are
  56. deleted and recreated from scratch. REG_DEAD is never created for a
  57. SET_DEST, only REG_UNUSED.
  58. * asm_operands:
  59. There are several rules on the usage of stack-like regs in
  60. asm_operands insns. These rules apply only to the operands that are
  61. stack-like regs:
  62. 1. Given a set of input regs that die in an asm_operands, it is
  63. necessary to know which are implicitly popped by the asm, and
  64. which must be explicitly popped by gcc.
  65. An input reg that is implicitly popped by the asm must be
  66. explicitly clobbered, unless it is constrained to match an
  67. output operand.
  68. 2. For any input reg that is implicitly popped by an asm, it is
  69. necessary to know how to adjust the stack to compensate for the pop.
  70. If any non-popped input is closer to the top of the reg-stack than
  71. the implicitly popped reg, it would not be possible to know what the
  72. stack looked like - it's not clear how the rest of the stack "slides
  73. up".
  74. All implicitly popped input regs must be closer to the top of
  75. the reg-stack than any input that is not implicitly popped.
  76. 3. It is possible that if an input dies in an insn, reload might
  77. use the input reg for an output reload. Consider this example:
  78. asm ("foo" : "=t" (a) : "f" (b));
  79. This asm says that input B is not popped by the asm, and that
  80. the asm pushes a result onto the reg-stack, i.e., the stack is one
  81. deeper after the asm than it was before. But, it is possible that
  82. reload will think that it can use the same reg for both the input and
  83. the output, if input B dies in this insn.
  84. If any input operand uses the "f" constraint, all output reg
  85. constraints must use the "&" earlyclobber.
  86. The asm above would be written as
  87. asm ("foo" : "=&t" (a) : "f" (b));
  88. 4. Some operands need to be in particular places on the stack. All
  89. output operands fall in this category - there is no other way to
  90. know which regs the outputs appear in unless the user indicates
  91. this in the constraints.
  92. Output operands must specifically indicate which reg an output
  93. appears in after an asm. "=f" is not allowed: the operand
  94. constraints must select a class with a single reg.
  95. 5. Output operands may not be "inserted" between existing stack regs.
  96. Since no 387 opcode uses a read/write operand, all output operands
  97. are dead before the asm_operands, and are pushed by the asm_operands.
  98. It makes no sense to push anywhere but the top of the reg-stack.
  99. Output operands must start at the top of the reg-stack: output
  100. operands may not "skip" a reg.
  101. 6. Some asm statements may need extra stack space for internal
  102. calculations. This can be guaranteed by clobbering stack registers
  103. unrelated to the inputs and outputs.
  104. Here are a couple of reasonable asms to want to write. This asm
  105. takes one input, which is internally popped, and produces two outputs.
  106. asm ("fsincos" : "=t" (cos), "=u" (sin) : "0" (inp));
  107. This asm takes two inputs, which are popped by the fyl2xp1 opcode,
  108. and replaces them with one output. The user must code the "st(1)"
  109. clobber for reg-stack.c to know that fyl2xp1 pops both inputs.
  110. asm ("fyl2xp1" : "=t" (result) : "0" (x), "u" (y) : "st(1)");
  111. */
  112. #include "config.h"
  113. #include "system.h"
  114. #include "coretypes.h"
  115. #include "tm.h"
  116. #include "hash-set.h"
  117. #include "machmode.h"
  118. #include "vec.h"
  119. #include "double-int.h"
  120. #include "input.h"
  121. #include "alias.h"
  122. #include "symtab.h"
  123. #include "wide-int.h"
  124. #include "inchash.h"
  125. #include "tree.h"
  126. #include "varasm.h"
  127. #include "rtl-error.h"
  128. #include "tm_p.h"
  129. #include "hard-reg-set.h"
  130. #include "input.h"
  131. #include "function.h"
  132. #include "insn-config.h"
  133. #include "regs.h"
  134. #include "flags.h"
  135. #include "recog.h"
  136. #include "predict.h"
  137. #include "dominance.h"
  138. #include "cfg.h"
  139. #include "cfgrtl.h"
  140. #include "cfganal.h"
  141. #include "cfgbuild.h"
  142. #include "cfgcleanup.h"
  143. #include "basic-block.h"
  144. #include "reload.h"
  145. #include "ggc.h"
  146. #include "tree-pass.h"
  147. #include "target.h"
  148. #include "df.h"
  149. #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
  150. #include "rtl-iter.h"
  151. #ifdef STACK_REGS
  152. /* We use this array to cache info about insns, because otherwise we
  153. spend too much time in stack_regs_mentioned_p.
  154. Indexed by insn UIDs. A value of zero is uninitialized, one indicates
  155. the insn uses stack registers, two indicates the insn does not use
  156. stack registers. */
  157. static vec<char> stack_regs_mentioned_data;
  158. #define REG_STACK_SIZE (LAST_STACK_REG - FIRST_STACK_REG + 1)
  159. int regstack_completed = 0;
  160. /* This is the basic stack record. TOP is an index into REG[] such
  161. that REG[TOP] is the top of stack. If TOP is -1 the stack is empty.
  162. If TOP is -2, REG[] is not yet initialized. Stack initialization
  163. consists of placing each live reg in array `reg' and setting `top'
  164. appropriately.
  165. REG_SET indicates which registers are live. */
  166. typedef struct stack_def
  167. {
  168. int top; /* index to top stack element */
  169. HARD_REG_SET reg_set; /* set of live registers */
  170. unsigned char reg[REG_STACK_SIZE];/* register - stack mapping */
  171. } *stack_ptr;
  172. /* This is used to carry information about basic blocks. It is
  173. attached to the AUX field of the standard CFG block. */
  174. typedef struct block_info_def
  175. {
  176. struct stack_def stack_in; /* Input stack configuration. */
  177. struct stack_def stack_out; /* Output stack configuration. */
  178. HARD_REG_SET out_reg_set; /* Stack regs live on output. */
  179. int done; /* True if block already converted. */
  180. int predecessors; /* Number of predecessors that need
  181. to be visited. */
  182. } *block_info;
  183. #define BLOCK_INFO(B) ((block_info) (B)->aux)
  184. /* Passed to change_stack to indicate where to emit insns. */
  185. enum emit_where
  186. {
  187. EMIT_AFTER,
  188. EMIT_BEFORE
  189. };
  190. /* The block we're currently working on. */
  191. static basic_block current_block;
  192. /* In the current_block, whether we're processing the first register
  193. stack or call instruction, i.e. the regstack is currently the
  194. same as BLOCK_INFO(current_block)->stack_in. */
  195. static bool starting_stack_p;
  196. /* This is the register file for all register after conversion. */
  197. static rtx
  198. FP_mode_reg[LAST_STACK_REG+1-FIRST_STACK_REG][(int) MAX_MACHINE_MODE];
  199. #define FP_MODE_REG(regno,mode) \
  200. (FP_mode_reg[(regno)-FIRST_STACK_REG][(int) (mode)])
  201. /* Used to initialize uninitialized registers. */
  202. static rtx not_a_num;
  203. /* Forward declarations */
  204. static int stack_regs_mentioned_p (const_rtx pat);
  205. static void pop_stack (stack_ptr, int);
  206. static rtx *get_true_reg (rtx *);
  207. static int check_asm_stack_operands (rtx_insn *);
  208. static void get_asm_operands_in_out (rtx, int *, int *);
  209. static rtx stack_result (tree);
  210. static void replace_reg (rtx *, int);
  211. static void remove_regno_note (rtx_insn *, enum reg_note, unsigned int);
  212. static int get_hard_regnum (stack_ptr, rtx);
  213. static rtx_insn *emit_pop_insn (rtx_insn *, stack_ptr, rtx, enum emit_where);
  214. static void swap_to_top (rtx_insn *, stack_ptr, rtx, rtx);
  215. static bool move_for_stack_reg (rtx_insn *, stack_ptr, rtx);
  216. static bool move_nan_for_stack_reg (rtx_insn *, stack_ptr, rtx);
  217. static int swap_rtx_condition_1 (rtx);
  218. static int swap_rtx_condition (rtx_insn *);
  219. static void compare_for_stack_reg (rtx_insn *, stack_ptr, rtx);
  220. static bool subst_stack_regs_pat (rtx_insn *, stack_ptr, rtx);
  221. static void subst_asm_stack_regs (rtx_insn *, stack_ptr);
  222. static bool subst_stack_regs (rtx_insn *, stack_ptr);
  223. static void change_stack (rtx_insn *, stack_ptr, stack_ptr, enum emit_where);
  224. static void print_stack (FILE *, stack_ptr);
  225. static rtx_insn *next_flags_user (rtx_insn *);
  226. /* Return nonzero if any stack register is mentioned somewhere within PAT. */
  227. static int
  228. stack_regs_mentioned_p (const_rtx pat)
  229. {
  230. const char *fmt;
  231. int i;
  232. if (STACK_REG_P (pat))
  233. return 1;
  234. fmt = GET_RTX_FORMAT (GET_CODE (pat));
  235. for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
  236. {
  237. if (fmt[i] == 'E')
  238. {
  239. int j;
  240. for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
  241. if (stack_regs_mentioned_p (XVECEXP (pat, i, j)))
  242. return 1;
  243. }
  244. else if (fmt[i] == 'e' && stack_regs_mentioned_p (XEXP (pat, i)))
  245. return 1;
  246. }
  247. return 0;
  248. }
  249. /* Return nonzero if INSN mentions stacked registers, else return zero. */
  250. int
  251. stack_regs_mentioned (const_rtx insn)
  252. {
  253. unsigned int uid, max;
  254. int test;
  255. if (! INSN_P (insn) || !stack_regs_mentioned_data.exists ())
  256. return 0;
  257. uid = INSN_UID (insn);
  258. max = stack_regs_mentioned_data.length ();
  259. if (uid >= max)
  260. {
  261. /* Allocate some extra size to avoid too many reallocs, but
  262. do not grow too quickly. */
  263. max = uid + uid / 20 + 1;
  264. stack_regs_mentioned_data.safe_grow_cleared (max);
  265. }
  266. test = stack_regs_mentioned_data[uid];
  267. if (test == 0)
  268. {
  269. /* This insn has yet to be examined. Do so now. */
  270. test = stack_regs_mentioned_p (PATTERN (insn)) ? 1 : 2;
  271. stack_regs_mentioned_data[uid] = test;
  272. }
  273. return test == 1;
  274. }
  275. static rtx ix86_flags_rtx;
  276. static rtx_insn *
  277. next_flags_user (rtx_insn *insn)
  278. {
  279. /* Search forward looking for the first use of this value.
  280. Stop at block boundaries. */
  281. while (insn != BB_END (current_block))
  282. {
  283. insn = NEXT_INSN (insn);
  284. if (INSN_P (insn) && reg_mentioned_p (ix86_flags_rtx, PATTERN (insn)))
  285. return insn;
  286. if (CALL_P (insn))
  287. return NULL;
  288. }
  289. return NULL;
  290. }
  291. /* Reorganize the stack into ascending numbers, before this insn. */
  292. static void
  293. straighten_stack (rtx_insn *insn, stack_ptr regstack)
  294. {
  295. struct stack_def temp_stack;
  296. int top;
  297. /* If there is only a single register on the stack, then the stack is
  298. already in increasing order and no reorganization is needed.
  299. Similarly if the stack is empty. */
  300. if (regstack->top <= 0)
  301. return;
  302. COPY_HARD_REG_SET (temp_stack.reg_set, regstack->reg_set);
  303. for (top = temp_stack.top = regstack->top; top >= 0; top--)
  304. temp_stack.reg[top] = FIRST_STACK_REG + temp_stack.top - top;
  305. change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
  306. }
  307. /* Pop a register from the stack. */
  308. static void
  309. pop_stack (stack_ptr regstack, int regno)
  310. {
  311. int top = regstack->top;
  312. CLEAR_HARD_REG_BIT (regstack->reg_set, regno);
  313. regstack->top--;
  314. /* If regno was not at the top of stack then adjust stack. */
  315. if (regstack->reg [top] != regno)
  316. {
  317. int i;
  318. for (i = regstack->top; i >= 0; i--)
  319. if (regstack->reg [i] == regno)
  320. {
  321. int j;
  322. for (j = i; j < top; j++)
  323. regstack->reg [j] = regstack->reg [j + 1];
  324. break;
  325. }
  326. }
  327. }
  328. /* Return a pointer to the REG expression within PAT. If PAT is not a
  329. REG, possible enclosed by a conversion rtx, return the inner part of
  330. PAT that stopped the search. */
  331. static rtx *
  332. get_true_reg (rtx *pat)
  333. {
  334. for (;;)
  335. switch (GET_CODE (*pat))
  336. {
  337. case SUBREG:
  338. /* Eliminate FP subregister accesses in favor of the
  339. actual FP register in use. */
  340. {
  341. rtx subreg;
  342. if (STACK_REG_P (subreg = SUBREG_REG (*pat)))
  343. {
  344. int regno_off = subreg_regno_offset (REGNO (subreg),
  345. GET_MODE (subreg),
  346. SUBREG_BYTE (*pat),
  347. GET_MODE (*pat));
  348. *pat = FP_MODE_REG (REGNO (subreg) + regno_off,
  349. GET_MODE (subreg));
  350. return pat;
  351. }
  352. }
  353. case FLOAT:
  354. case FIX:
  355. case FLOAT_EXTEND:
  356. pat = & XEXP (*pat, 0);
  357. break;
  358. case UNSPEC:
  359. if (XINT (*pat, 1) == UNSPEC_TRUNC_NOOP
  360. || XINT (*pat, 1) == UNSPEC_FILD_ATOMIC)
  361. pat = & XVECEXP (*pat, 0, 0);
  362. return pat;
  363. case FLOAT_TRUNCATE:
  364. if (!flag_unsafe_math_optimizations)
  365. return pat;
  366. pat = & XEXP (*pat, 0);
  367. break;
  368. default:
  369. return pat;
  370. }
  371. }
  372. /* Set if we find any malformed asms in a block. */
  373. static bool any_malformed_asm;
  374. /* There are many rules that an asm statement for stack-like regs must
  375. follow. Those rules are explained at the top of this file: the rule
  376. numbers below refer to that explanation. */
  377. static int
  378. check_asm_stack_operands (rtx_insn *insn)
  379. {
  380. int i;
  381. int n_clobbers;
  382. int malformed_asm = 0;
  383. rtx body = PATTERN (insn);
  384. char reg_used_as_output[FIRST_PSEUDO_REGISTER];
  385. char implicitly_dies[FIRST_PSEUDO_REGISTER];
  386. rtx *clobber_reg = 0;
  387. int n_inputs, n_outputs;
  388. /* Find out what the constraints require. If no constraint
  389. alternative matches, this asm is malformed. */
  390. extract_constrain_insn (insn);
  391. preprocess_constraints (insn);
  392. get_asm_operands_in_out (body, &n_outputs, &n_inputs);
  393. if (which_alternative < 0)
  394. {
  395. malformed_asm = 1;
  396. /* Avoid further trouble with this insn. */
  397. PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
  398. return 0;
  399. }
  400. const operand_alternative *op_alt = which_op_alt ();
  401. /* Strip SUBREGs here to make the following code simpler. */
  402. for (i = 0; i < recog_data.n_operands; i++)
  403. if (GET_CODE (recog_data.operand[i]) == SUBREG
  404. && REG_P (SUBREG_REG (recog_data.operand[i])))
  405. recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
  406. /* Set up CLOBBER_REG. */
  407. n_clobbers = 0;
  408. if (GET_CODE (body) == PARALLEL)
  409. {
  410. clobber_reg = XALLOCAVEC (rtx, XVECLEN (body, 0));
  411. for (i = 0; i < XVECLEN (body, 0); i++)
  412. if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
  413. {
  414. rtx clobber = XVECEXP (body, 0, i);
  415. rtx reg = XEXP (clobber, 0);
  416. if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
  417. reg = SUBREG_REG (reg);
  418. if (STACK_REG_P (reg))
  419. {
  420. clobber_reg[n_clobbers] = reg;
  421. n_clobbers++;
  422. }
  423. }
  424. }
  425. /* Enforce rule #4: Output operands must specifically indicate which
  426. reg an output appears in after an asm. "=f" is not allowed: the
  427. operand constraints must select a class with a single reg.
  428. Also enforce rule #5: Output operands must start at the top of
  429. the reg-stack: output operands may not "skip" a reg. */
  430. memset (reg_used_as_output, 0, sizeof (reg_used_as_output));
  431. for (i = 0; i < n_outputs; i++)
  432. if (STACK_REG_P (recog_data.operand[i]))
  433. {
  434. if (reg_class_size[(int) op_alt[i].cl] != 1)
  435. {
  436. error_for_asm (insn, "output constraint %d must specify a single register", i);
  437. malformed_asm = 1;
  438. }
  439. else
  440. {
  441. int j;
  442. for (j = 0; j < n_clobbers; j++)
  443. if (REGNO (recog_data.operand[i]) == REGNO (clobber_reg[j]))
  444. {
  445. error_for_asm (insn, "output constraint %d cannot be specified together with \"%s\" clobber",
  446. i, reg_names [REGNO (clobber_reg[j])]);
  447. malformed_asm = 1;
  448. break;
  449. }
  450. if (j == n_clobbers)
  451. reg_used_as_output[REGNO (recog_data.operand[i])] = 1;
  452. }
  453. }
  454. /* Search for first non-popped reg. */
  455. for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
  456. if (! reg_used_as_output[i])
  457. break;
  458. /* If there are any other popped regs, that's an error. */
  459. for (; i < LAST_STACK_REG + 1; i++)
  460. if (reg_used_as_output[i])
  461. break;
  462. if (i != LAST_STACK_REG + 1)
  463. {
  464. error_for_asm (insn, "output regs must be grouped at top of stack");
  465. malformed_asm = 1;
  466. }
  467. /* Enforce rule #2: All implicitly popped input regs must be closer
  468. to the top of the reg-stack than any input that is not implicitly
  469. popped. */
  470. memset (implicitly_dies, 0, sizeof (implicitly_dies));
  471. for (i = n_outputs; i < n_outputs + n_inputs; i++)
  472. if (STACK_REG_P (recog_data.operand[i]))
  473. {
  474. /* An input reg is implicitly popped if it is tied to an
  475. output, or if there is a CLOBBER for it. */
  476. int j;
  477. for (j = 0; j < n_clobbers; j++)
  478. if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
  479. break;
  480. if (j < n_clobbers || op_alt[i].matches >= 0)
  481. implicitly_dies[REGNO (recog_data.operand[i])] = 1;
  482. }
  483. /* Search for first non-popped reg. */
  484. for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
  485. if (! implicitly_dies[i])
  486. break;
  487. /* If there are any other popped regs, that's an error. */
  488. for (; i < LAST_STACK_REG + 1; i++)
  489. if (implicitly_dies[i])
  490. break;
  491. if (i != LAST_STACK_REG + 1)
  492. {
  493. error_for_asm (insn,
  494. "implicitly popped regs must be grouped at top of stack");
  495. malformed_asm = 1;
  496. }
  497. /* Enforce rule #3: If any input operand uses the "f" constraint, all
  498. output constraints must use the "&" earlyclobber.
  499. ??? Detect this more deterministically by having constrain_asm_operands
  500. record any earlyclobber. */
  501. for (i = n_outputs; i < n_outputs + n_inputs; i++)
  502. if (op_alt[i].matches == -1)
  503. {
  504. int j;
  505. for (j = 0; j < n_outputs; j++)
  506. if (operands_match_p (recog_data.operand[j], recog_data.operand[i]))
  507. {
  508. error_for_asm (insn,
  509. "output operand %d must use %<&%> constraint", j);
  510. malformed_asm = 1;
  511. }
  512. }
  513. if (malformed_asm)
  514. {
  515. /* Avoid further trouble with this insn. */
  516. PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
  517. any_malformed_asm = true;
  518. return 0;
  519. }
  520. return 1;
  521. }
  522. /* Calculate the number of inputs and outputs in BODY, an
  523. asm_operands. N_OPERANDS is the total number of operands, and
  524. N_INPUTS and N_OUTPUTS are pointers to ints into which the results are
  525. placed. */
  526. static void
  527. get_asm_operands_in_out (rtx body, int *pout, int *pin)
  528. {
  529. rtx asmop = extract_asm_operands (body);
  530. *pin = ASM_OPERANDS_INPUT_LENGTH (asmop);
  531. *pout = (recog_data.n_operands
  532. - ASM_OPERANDS_INPUT_LENGTH (asmop)
  533. - ASM_OPERANDS_LABEL_LENGTH (asmop));
  534. }
  535. /* If current function returns its result in an fp stack register,
  536. return the REG. Otherwise, return 0. */
  537. static rtx
  538. stack_result (tree decl)
  539. {
  540. rtx result;
  541. /* If the value is supposed to be returned in memory, then clearly
  542. it is not returned in a stack register. */
  543. if (aggregate_value_p (DECL_RESULT (decl), decl))
  544. return 0;
  545. result = DECL_RTL_IF_SET (DECL_RESULT (decl));
  546. if (result != 0)
  547. result = targetm.calls.function_value (TREE_TYPE (DECL_RESULT (decl)),
  548. decl, true);
  549. return result != 0 && STACK_REG_P (result) ? result : 0;
  550. }
  551. /*
  552. * This section deals with stack register substitution, and forms the second
  553. * pass over the RTL.
  554. */
  555. /* Replace REG, which is a pointer to a stack reg RTX, with an RTX for
  556. the desired hard REGNO. */
  557. static void
  558. replace_reg (rtx *reg, int regno)
  559. {
  560. gcc_assert (IN_RANGE (regno, FIRST_STACK_REG, LAST_STACK_REG));
  561. gcc_assert (STACK_REG_P (*reg));
  562. gcc_assert (SCALAR_FLOAT_MODE_P (GET_MODE (*reg))
  563. || GET_MODE_CLASS (GET_MODE (*reg)) == MODE_COMPLEX_FLOAT);
  564. *reg = FP_MODE_REG (regno, GET_MODE (*reg));
  565. }
  566. /* Remove a note of type NOTE, which must be found, for register
  567. number REGNO from INSN. Remove only one such note. */
  568. static void
  569. remove_regno_note (rtx_insn *insn, enum reg_note note, unsigned int regno)
  570. {
  571. rtx *note_link, this_rtx;
  572. note_link = &REG_NOTES (insn);
  573. for (this_rtx = *note_link; this_rtx; this_rtx = XEXP (this_rtx, 1))
  574. if (REG_NOTE_KIND (this_rtx) == note
  575. && REG_P (XEXP (this_rtx, 0)) && REGNO (XEXP (this_rtx, 0)) == regno)
  576. {
  577. *note_link = XEXP (this_rtx, 1);
  578. return;
  579. }
  580. else
  581. note_link = &XEXP (this_rtx, 1);
  582. gcc_unreachable ();
  583. }
  584. /* Find the hard register number of virtual register REG in REGSTACK.
  585. The hard register number is relative to the top of the stack. -1 is
  586. returned if the register is not found. */
  587. static int
  588. get_hard_regnum (stack_ptr regstack, rtx reg)
  589. {
  590. int i;
  591. gcc_assert (STACK_REG_P (reg));
  592. for (i = regstack->top; i >= 0; i--)
  593. if (regstack->reg[i] == REGNO (reg))
  594. break;
  595. return i >= 0 ? (FIRST_STACK_REG + regstack->top - i) : -1;
  596. }
  597. /* Emit an insn to pop virtual register REG before or after INSN.
  598. REGSTACK is the stack state after INSN and is updated to reflect this
  599. pop. WHEN is either emit_insn_before or emit_insn_after. A pop insn
  600. is represented as a SET whose destination is the register to be popped
  601. and source is the top of stack. A death note for the top of stack
  602. cases the movdf pattern to pop. */
  603. static rtx_insn *
  604. emit_pop_insn (rtx_insn *insn, stack_ptr regstack, rtx reg, enum emit_where where)
  605. {
  606. rtx_insn *pop_insn;
  607. rtx pop_rtx;
  608. int hard_regno;
  609. /* For complex types take care to pop both halves. These may survive in
  610. CLOBBER and USE expressions. */
  611. if (COMPLEX_MODE_P (GET_MODE (reg)))
  612. {
  613. rtx reg1 = FP_MODE_REG (REGNO (reg), DFmode);
  614. rtx reg2 = FP_MODE_REG (REGNO (reg) + 1, DFmode);
  615. pop_insn = NULL;
  616. if (get_hard_regnum (regstack, reg1) >= 0)
  617. pop_insn = emit_pop_insn (insn, regstack, reg1, where);
  618. if (get_hard_regnum (regstack, reg2) >= 0)
  619. pop_insn = emit_pop_insn (insn, regstack, reg2, where);
  620. gcc_assert (pop_insn);
  621. return pop_insn;
  622. }
  623. hard_regno = get_hard_regnum (regstack, reg);
  624. gcc_assert (hard_regno >= FIRST_STACK_REG);
  625. pop_rtx = gen_rtx_SET (VOIDmode, FP_MODE_REG (hard_regno, DFmode),
  626. FP_MODE_REG (FIRST_STACK_REG, DFmode));
  627. if (where == EMIT_AFTER)
  628. pop_insn = emit_insn_after (pop_rtx, insn);
  629. else
  630. pop_insn = emit_insn_before (pop_rtx, insn);
  631. add_reg_note (pop_insn, REG_DEAD, FP_MODE_REG (FIRST_STACK_REG, DFmode));
  632. regstack->reg[regstack->top - (hard_regno - FIRST_STACK_REG)]
  633. = regstack->reg[regstack->top];
  634. regstack->top -= 1;
  635. CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (reg));
  636. return pop_insn;
  637. }
  638. /* Emit an insn before or after INSN to swap virtual register REG with
  639. the top of stack. REGSTACK is the stack state before the swap, and
  640. is updated to reflect the swap. A swap insn is represented as a
  641. PARALLEL of two patterns: each pattern moves one reg to the other.
  642. If REG is already at the top of the stack, no insn is emitted. */
  643. static void
  644. emit_swap_insn (rtx_insn *insn, stack_ptr regstack, rtx reg)
  645. {
  646. int hard_regno;
  647. rtx swap_rtx;
  648. int tmp, other_reg; /* swap regno temps */
  649. rtx_insn *i1; /* the stack-reg insn prior to INSN */
  650. rtx i1set = NULL_RTX; /* the SET rtx within I1 */
  651. hard_regno = get_hard_regnum (regstack, reg);
  652. if (hard_regno == FIRST_STACK_REG)
  653. return;
  654. if (hard_regno == -1)
  655. {
  656. /* Something failed if the register wasn't on the stack. If we had
  657. malformed asms, we zapped the instruction itself, but that didn't
  658. produce the same pattern of register sets as before. To prevent
  659. further failure, adjust REGSTACK to include REG at TOP. */
  660. gcc_assert (any_malformed_asm);
  661. regstack->reg[++regstack->top] = REGNO (reg);
  662. return;
  663. }
  664. gcc_assert (hard_regno >= FIRST_STACK_REG);
  665. other_reg = regstack->top - (hard_regno - FIRST_STACK_REG);
  666. tmp = regstack->reg[other_reg];
  667. regstack->reg[other_reg] = regstack->reg[regstack->top];
  668. regstack->reg[regstack->top] = tmp;
  669. /* Find the previous insn involving stack regs, but don't pass a
  670. block boundary. */
  671. i1 = NULL;
  672. if (current_block && insn != BB_HEAD (current_block))
  673. {
  674. rtx_insn *tmp = PREV_INSN (insn);
  675. rtx_insn *limit = PREV_INSN (BB_HEAD (current_block));
  676. while (tmp != limit)
  677. {
  678. if (LABEL_P (tmp)
  679. || CALL_P (tmp)
  680. || NOTE_INSN_BASIC_BLOCK_P (tmp)
  681. || (NONJUMP_INSN_P (tmp)
  682. && stack_regs_mentioned (tmp)))
  683. {
  684. i1 = tmp;
  685. break;
  686. }
  687. tmp = PREV_INSN (tmp);
  688. }
  689. }
  690. if (i1 != NULL_RTX
  691. && (i1set = single_set (i1)) != NULL_RTX)
  692. {
  693. rtx i1src = *get_true_reg (&SET_SRC (i1set));
  694. rtx i1dest = *get_true_reg (&SET_DEST (i1set));
  695. /* If the previous register stack push was from the reg we are to
  696. swap with, omit the swap. */
  697. if (REG_P (i1dest) && REGNO (i1dest) == FIRST_STACK_REG
  698. && REG_P (i1src)
  699. && REGNO (i1src) == (unsigned) hard_regno - 1
  700. && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
  701. return;
  702. /* If the previous insn wrote to the reg we are to swap with,
  703. omit the swap. */
  704. if (REG_P (i1dest) && REGNO (i1dest) == (unsigned) hard_regno
  705. && REG_P (i1src) && REGNO (i1src) == FIRST_STACK_REG
  706. && find_regno_note (i1, REG_DEAD, FIRST_STACK_REG) == NULL_RTX)
  707. return;
  708. }
  709. /* Avoid emitting the swap if this is the first register stack insn
  710. of the current_block. Instead update the current_block's stack_in
  711. and let compensate edges take care of this for us. */
  712. if (current_block && starting_stack_p)
  713. {
  714. BLOCK_INFO (current_block)->stack_in = *regstack;
  715. starting_stack_p = false;
  716. return;
  717. }
  718. swap_rtx = gen_swapxf (FP_MODE_REG (hard_regno, XFmode),
  719. FP_MODE_REG (FIRST_STACK_REG, XFmode));
  720. if (i1)
  721. emit_insn_after (swap_rtx, i1);
  722. else if (current_block)
  723. emit_insn_before (swap_rtx, BB_HEAD (current_block));
  724. else
  725. emit_insn_before (swap_rtx, insn);
  726. }
  727. /* Emit an insns before INSN to swap virtual register SRC1 with
  728. the top of stack and virtual register SRC2 with second stack
  729. slot. REGSTACK is the stack state before the swaps, and
  730. is updated to reflect the swaps. A swap insn is represented as a
  731. PARALLEL of two patterns: each pattern moves one reg to the other.
  732. If SRC1 and/or SRC2 are already at the right place, no swap insn
  733. is emitted. */
  734. static void
  735. swap_to_top (rtx_insn *insn, stack_ptr regstack, rtx src1, rtx src2)
  736. {
  737. struct stack_def temp_stack;
  738. int regno, j, k, temp;
  739. temp_stack = *regstack;
  740. /* Place operand 1 at the top of stack. */
  741. regno = get_hard_regnum (&temp_stack, src1);
  742. gcc_assert (regno >= 0);
  743. if (regno != FIRST_STACK_REG)
  744. {
  745. k = temp_stack.top - (regno - FIRST_STACK_REG);
  746. j = temp_stack.top;
  747. temp = temp_stack.reg[k];
  748. temp_stack.reg[k] = temp_stack.reg[j];
  749. temp_stack.reg[j] = temp;
  750. }
  751. /* Place operand 2 next on the stack. */
  752. regno = get_hard_regnum (&temp_stack, src2);
  753. gcc_assert (regno >= 0);
  754. if (regno != FIRST_STACK_REG + 1)
  755. {
  756. k = temp_stack.top - (regno - FIRST_STACK_REG);
  757. j = temp_stack.top - 1;
  758. temp = temp_stack.reg[k];
  759. temp_stack.reg[k] = temp_stack.reg[j];
  760. temp_stack.reg[j] = temp;
  761. }
  762. change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
  763. }
  764. /* Handle a move to or from a stack register in PAT, which is in INSN.
  765. REGSTACK is the current stack. Return whether a control flow insn
  766. was deleted in the process. */
  767. static bool
  768. move_for_stack_reg (rtx_insn *insn, stack_ptr regstack, rtx pat)
  769. {
  770. rtx *psrc = get_true_reg (&SET_SRC (pat));
  771. rtx *pdest = get_true_reg (&SET_DEST (pat));
  772. rtx src, dest;
  773. rtx note;
  774. bool control_flow_insn_deleted = false;
  775. src = *psrc; dest = *pdest;
  776. if (STACK_REG_P (src) && STACK_REG_P (dest))
  777. {
  778. /* Write from one stack reg to another. If SRC dies here, then
  779. just change the register mapping and delete the insn. */
  780. note = find_regno_note (insn, REG_DEAD, REGNO (src));
  781. if (note)
  782. {
  783. int i;
  784. /* If this is a no-op move, there must not be a REG_DEAD note. */
  785. gcc_assert (REGNO (src) != REGNO (dest));
  786. for (i = regstack->top; i >= 0; i--)
  787. if (regstack->reg[i] == REGNO (src))
  788. break;
  789. /* The destination must be dead, or life analysis is borked. */
  790. gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
  791. /* If the source is not live, this is yet another case of
  792. uninitialized variables. Load up a NaN instead. */
  793. if (i < 0)
  794. return move_nan_for_stack_reg (insn, regstack, dest);
  795. /* It is possible that the dest is unused after this insn.
  796. If so, just pop the src. */
  797. if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
  798. emit_pop_insn (insn, regstack, src, EMIT_AFTER);
  799. else
  800. {
  801. regstack->reg[i] = REGNO (dest);
  802. SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
  803. CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
  804. }
  805. control_flow_insn_deleted |= control_flow_insn_p (insn);
  806. delete_insn (insn);
  807. return control_flow_insn_deleted;
  808. }
  809. /* The source reg does not die. */
  810. /* If this appears to be a no-op move, delete it, or else it
  811. will confuse the machine description output patterns. But if
  812. it is REG_UNUSED, we must pop the reg now, as per-insn processing
  813. for REG_UNUSED will not work for deleted insns. */
  814. if (REGNO (src) == REGNO (dest))
  815. {
  816. if (find_regno_note (insn, REG_UNUSED, REGNO (dest)))
  817. emit_pop_insn (insn, regstack, dest, EMIT_AFTER);
  818. control_flow_insn_deleted |= control_flow_insn_p (insn);
  819. delete_insn (insn);
  820. return control_flow_insn_deleted;
  821. }
  822. /* The destination ought to be dead. */
  823. gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
  824. replace_reg (psrc, get_hard_regnum (regstack, src));
  825. regstack->reg[++regstack->top] = REGNO (dest);
  826. SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
  827. replace_reg (pdest, FIRST_STACK_REG);
  828. }
  829. else if (STACK_REG_P (src))
  830. {
  831. /* Save from a stack reg to MEM, or possibly integer reg. Since
  832. only top of stack may be saved, emit an exchange first if
  833. needs be. */
  834. emit_swap_insn (insn, regstack, src);
  835. note = find_regno_note (insn, REG_DEAD, REGNO (src));
  836. if (note)
  837. {
  838. replace_reg (&XEXP (note, 0), FIRST_STACK_REG);
  839. regstack->top--;
  840. CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (src));
  841. }
  842. else if ((GET_MODE (src) == XFmode)
  843. && regstack->top < REG_STACK_SIZE - 1)
  844. {
  845. /* A 387 cannot write an XFmode value to a MEM without
  846. clobbering the source reg. The output code can handle
  847. this by reading back the value from the MEM.
  848. But it is more efficient to use a temp register if one is
  849. available. Push the source value here if the register
  850. stack is not full, and then write the value to memory via
  851. a pop. */
  852. rtx push_rtx;
  853. rtx top_stack_reg = FP_MODE_REG (FIRST_STACK_REG, GET_MODE (src));
  854. push_rtx = gen_movxf (top_stack_reg, top_stack_reg);
  855. emit_insn_before (push_rtx, insn);
  856. add_reg_note (insn, REG_DEAD, top_stack_reg);
  857. }
  858. replace_reg (psrc, FIRST_STACK_REG);
  859. }
  860. else
  861. {
  862. rtx pat = PATTERN (insn);
  863. gcc_assert (STACK_REG_P (dest));
  864. /* Load from MEM, or possibly integer REG or constant, into the
  865. stack regs. The actual target is always the top of the
  866. stack. The stack mapping is changed to reflect that DEST is
  867. now at top of stack. */
  868. /* The destination ought to be dead. However, there is a
  869. special case with i387 UNSPEC_TAN, where destination is live
  870. (an argument to fptan) but inherent load of 1.0 is modelled
  871. as a load from a constant. */
  872. if (GET_CODE (pat) == PARALLEL
  873. && XVECLEN (pat, 0) == 2
  874. && GET_CODE (XVECEXP (pat, 0, 1)) == SET
  875. && GET_CODE (SET_SRC (XVECEXP (pat, 0, 1))) == UNSPEC
  876. && XINT (SET_SRC (XVECEXP (pat, 0, 1)), 1) == UNSPEC_TAN)
  877. emit_swap_insn (insn, regstack, dest);
  878. else
  879. gcc_assert (get_hard_regnum (regstack, dest) < FIRST_STACK_REG);
  880. gcc_assert (regstack->top < REG_STACK_SIZE);
  881. regstack->reg[++regstack->top] = REGNO (dest);
  882. SET_HARD_REG_BIT (regstack->reg_set, REGNO (dest));
  883. replace_reg (pdest, FIRST_STACK_REG);
  884. }
  885. return control_flow_insn_deleted;
  886. }
  887. /* A helper function which replaces INSN with a pattern that loads up
  888. a NaN into DEST, then invokes move_for_stack_reg. */
  889. static bool
  890. move_nan_for_stack_reg (rtx_insn *insn, stack_ptr regstack, rtx dest)
  891. {
  892. rtx pat;
  893. dest = FP_MODE_REG (REGNO (dest), SFmode);
  894. pat = gen_rtx_SET (VOIDmode, dest, not_a_num);
  895. PATTERN (insn) = pat;
  896. INSN_CODE (insn) = -1;
  897. return move_for_stack_reg (insn, regstack, pat);
  898. }
  899. /* Swap the condition on a branch, if there is one. Return true if we
  900. found a condition to swap. False if the condition was not used as
  901. such. */
  902. static int
  903. swap_rtx_condition_1 (rtx pat)
  904. {
  905. const char *fmt;
  906. int i, r = 0;
  907. if (COMPARISON_P (pat))
  908. {
  909. PUT_CODE (pat, swap_condition (GET_CODE (pat)));
  910. r = 1;
  911. }
  912. else
  913. {
  914. fmt = GET_RTX_FORMAT (GET_CODE (pat));
  915. for (i = GET_RTX_LENGTH (GET_CODE (pat)) - 1; i >= 0; i--)
  916. {
  917. if (fmt[i] == 'E')
  918. {
  919. int j;
  920. for (j = XVECLEN (pat, i) - 1; j >= 0; j--)
  921. r |= swap_rtx_condition_1 (XVECEXP (pat, i, j));
  922. }
  923. else if (fmt[i] == 'e')
  924. r |= swap_rtx_condition_1 (XEXP (pat, i));
  925. }
  926. }
  927. return r;
  928. }
  929. static int
  930. swap_rtx_condition (rtx_insn *insn)
  931. {
  932. rtx pat = PATTERN (insn);
  933. /* We're looking for a single set to cc0 or an HImode temporary. */
  934. if (GET_CODE (pat) == SET
  935. && REG_P (SET_DEST (pat))
  936. && REGNO (SET_DEST (pat)) == FLAGS_REG)
  937. {
  938. insn = next_flags_user (insn);
  939. if (insn == NULL_RTX)
  940. return 0;
  941. pat = PATTERN (insn);
  942. }
  943. /* See if this is, or ends in, a fnstsw. If so, we're not doing anything
  944. with the cc value right now. We may be able to search for one
  945. though. */
  946. if (GET_CODE (pat) == SET
  947. && GET_CODE (SET_SRC (pat)) == UNSPEC
  948. && XINT (SET_SRC (pat), 1) == UNSPEC_FNSTSW)
  949. {
  950. rtx dest = SET_DEST (pat);
  951. /* Search forward looking for the first use of this value.
  952. Stop at block boundaries. */
  953. while (insn != BB_END (current_block))
  954. {
  955. insn = NEXT_INSN (insn);
  956. if (INSN_P (insn) && reg_mentioned_p (dest, insn))
  957. break;
  958. if (CALL_P (insn))
  959. return 0;
  960. }
  961. /* We haven't found it. */
  962. if (insn == BB_END (current_block))
  963. return 0;
  964. /* So we've found the insn using this value. If it is anything
  965. other than sahf or the value does not die (meaning we'd have
  966. to search further), then we must give up. */
  967. pat = PATTERN (insn);
  968. if (GET_CODE (pat) != SET
  969. || GET_CODE (SET_SRC (pat)) != UNSPEC
  970. || XINT (SET_SRC (pat), 1) != UNSPEC_SAHF
  971. || ! dead_or_set_p (insn, dest))
  972. return 0;
  973. /* Now we are prepared to handle this as a normal cc0 setter. */
  974. insn = next_flags_user (insn);
  975. if (insn == NULL_RTX)
  976. return 0;
  977. pat = PATTERN (insn);
  978. }
  979. if (swap_rtx_condition_1 (pat))
  980. {
  981. int fail = 0;
  982. INSN_CODE (insn) = -1;
  983. if (recog_memoized (insn) == -1)
  984. fail = 1;
  985. /* In case the flags don't die here, recurse to try fix
  986. following user too. */
  987. else if (! dead_or_set_p (insn, ix86_flags_rtx))
  988. {
  989. insn = next_flags_user (insn);
  990. if (!insn || !swap_rtx_condition (insn))
  991. fail = 1;
  992. }
  993. if (fail)
  994. {
  995. swap_rtx_condition_1 (pat);
  996. return 0;
  997. }
  998. return 1;
  999. }
  1000. return 0;
  1001. }
  1002. /* Handle a comparison. Special care needs to be taken to avoid
  1003. causing comparisons that a 387 cannot do correctly, such as EQ.
  1004. Also, a pop insn may need to be emitted. The 387 does have an
  1005. `fcompp' insn that can pop two regs, but it is sometimes too expensive
  1006. to do this - a `fcomp' followed by a `fstpl %st(0)' may be easier to
  1007. set up. */
  1008. static void
  1009. compare_for_stack_reg (rtx_insn *insn, stack_ptr regstack, rtx pat_src)
  1010. {
  1011. rtx *src1, *src2;
  1012. rtx src1_note, src2_note;
  1013. src1 = get_true_reg (&XEXP (pat_src, 0));
  1014. src2 = get_true_reg (&XEXP (pat_src, 1));
  1015. /* ??? If fxch turns out to be cheaper than fstp, give priority to
  1016. registers that die in this insn - move those to stack top first. */
  1017. if ((! STACK_REG_P (*src1)
  1018. || (STACK_REG_P (*src2)
  1019. && get_hard_regnum (regstack, *src2) == FIRST_STACK_REG))
  1020. && swap_rtx_condition (insn))
  1021. {
  1022. rtx temp;
  1023. temp = XEXP (pat_src, 0);
  1024. XEXP (pat_src, 0) = XEXP (pat_src, 1);
  1025. XEXP (pat_src, 1) = temp;
  1026. src1 = get_true_reg (&XEXP (pat_src, 0));
  1027. src2 = get_true_reg (&XEXP (pat_src, 1));
  1028. INSN_CODE (insn) = -1;
  1029. }
  1030. /* We will fix any death note later. */
  1031. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1032. if (STACK_REG_P (*src2))
  1033. src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
  1034. else
  1035. src2_note = NULL_RTX;
  1036. emit_swap_insn (insn, regstack, *src1);
  1037. replace_reg (src1, FIRST_STACK_REG);
  1038. if (STACK_REG_P (*src2))
  1039. replace_reg (src2, get_hard_regnum (regstack, *src2));
  1040. if (src1_note)
  1041. {
  1042. pop_stack (regstack, REGNO (XEXP (src1_note, 0)));
  1043. replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
  1044. }
  1045. /* If the second operand dies, handle that. But if the operands are
  1046. the same stack register, don't bother, because only one death is
  1047. needed, and it was just handled. */
  1048. if (src2_note
  1049. && ! (STACK_REG_P (*src1) && STACK_REG_P (*src2)
  1050. && REGNO (*src1) == REGNO (*src2)))
  1051. {
  1052. /* As a special case, two regs may die in this insn if src2 is
  1053. next to top of stack and the top of stack also dies. Since
  1054. we have already popped src1, "next to top of stack" is really
  1055. at top (FIRST_STACK_REG) now. */
  1056. if (get_hard_regnum (regstack, XEXP (src2_note, 0)) == FIRST_STACK_REG
  1057. && src1_note)
  1058. {
  1059. pop_stack (regstack, REGNO (XEXP (src2_note, 0)));
  1060. replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
  1061. }
  1062. else
  1063. {
  1064. /* The 386 can only represent death of the first operand in
  1065. the case handled above. In all other cases, emit a separate
  1066. pop and remove the death note from here. */
  1067. remove_regno_note (insn, REG_DEAD, REGNO (XEXP (src2_note, 0)));
  1068. emit_pop_insn (insn, regstack, XEXP (src2_note, 0),
  1069. EMIT_AFTER);
  1070. }
  1071. }
  1072. }
  1073. /* Substitute hardware stack regs in debug insn INSN, using stack
  1074. layout REGSTACK. If we can't find a hardware stack reg for any of
  1075. the REGs in it, reset the debug insn. */
  1076. static void
  1077. subst_all_stack_regs_in_debug_insn (rtx_insn *insn, struct stack_def *regstack)
  1078. {
  1079. subrtx_ptr_iterator::array_type array;
  1080. FOR_EACH_SUBRTX_PTR (iter, array, &INSN_VAR_LOCATION_LOC (insn), NONCONST)
  1081. {
  1082. rtx *loc = *iter;
  1083. rtx x = *loc;
  1084. if (STACK_REG_P (x))
  1085. {
  1086. int hard_regno = get_hard_regnum (regstack, x);
  1087. /* If we can't find an active register, reset this debug insn. */
  1088. if (hard_regno == -1)
  1089. {
  1090. INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
  1091. return;
  1092. }
  1093. gcc_assert (hard_regno >= FIRST_STACK_REG);
  1094. replace_reg (loc, hard_regno);
  1095. iter.skip_subrtxes ();
  1096. }
  1097. }
  1098. }
  1099. /* Substitute new registers in PAT, which is part of INSN. REGSTACK
  1100. is the current register layout. Return whether a control flow insn
  1101. was deleted in the process. */
  1102. static bool
  1103. subst_stack_regs_pat (rtx_insn *insn, stack_ptr regstack, rtx pat)
  1104. {
  1105. rtx *dest, *src;
  1106. bool control_flow_insn_deleted = false;
  1107. switch (GET_CODE (pat))
  1108. {
  1109. case USE:
  1110. /* Deaths in USE insns can happen in non optimizing compilation.
  1111. Handle them by popping the dying register. */
  1112. src = get_true_reg (&XEXP (pat, 0));
  1113. if (STACK_REG_P (*src)
  1114. && find_regno_note (insn, REG_DEAD, REGNO (*src)))
  1115. {
  1116. /* USEs are ignored for liveness information so USEs of dead
  1117. register might happen. */
  1118. if (TEST_HARD_REG_BIT (regstack->reg_set, REGNO (*src)))
  1119. emit_pop_insn (insn, regstack, *src, EMIT_AFTER);
  1120. return control_flow_insn_deleted;
  1121. }
  1122. /* Uninitialized USE might happen for functions returning uninitialized
  1123. value. We will properly initialize the USE on the edge to EXIT_BLOCK,
  1124. so it is safe to ignore the use here. This is consistent with behavior
  1125. of dataflow analyzer that ignores USE too. (This also imply that
  1126. forcibly initializing the register to NaN here would lead to ICE later,
  1127. since the REG_DEAD notes are not issued.) */
  1128. break;
  1129. case VAR_LOCATION:
  1130. gcc_unreachable ();
  1131. case CLOBBER:
  1132. {
  1133. rtx note;
  1134. dest = get_true_reg (&XEXP (pat, 0));
  1135. if (STACK_REG_P (*dest))
  1136. {
  1137. note = find_reg_note (insn, REG_DEAD, *dest);
  1138. if (pat != PATTERN (insn))
  1139. {
  1140. /* The fix_truncdi_1 pattern wants to be able to
  1141. allocate its own scratch register. It does this by
  1142. clobbering an fp reg so that it is assured of an
  1143. empty reg-stack register. If the register is live,
  1144. kill it now. Remove the DEAD/UNUSED note so we
  1145. don't try to kill it later too.
  1146. In reality the UNUSED note can be absent in some
  1147. complicated cases when the register is reused for
  1148. partially set variable. */
  1149. if (note)
  1150. emit_pop_insn (insn, regstack, *dest, EMIT_BEFORE);
  1151. else
  1152. note = find_reg_note (insn, REG_UNUSED, *dest);
  1153. if (note)
  1154. remove_note (insn, note);
  1155. replace_reg (dest, FIRST_STACK_REG + 1);
  1156. }
  1157. else
  1158. {
  1159. /* A top-level clobber with no REG_DEAD, and no hard-regnum
  1160. indicates an uninitialized value. Because reload removed
  1161. all other clobbers, this must be due to a function
  1162. returning without a value. Load up a NaN. */
  1163. if (!note)
  1164. {
  1165. rtx t = *dest;
  1166. if (COMPLEX_MODE_P (GET_MODE (t)))
  1167. {
  1168. rtx u = FP_MODE_REG (REGNO (t) + 1, SFmode);
  1169. if (get_hard_regnum (regstack, u) == -1)
  1170. {
  1171. rtx pat2 = gen_rtx_CLOBBER (VOIDmode, u);
  1172. rtx_insn *insn2 = emit_insn_before (pat2, insn);
  1173. control_flow_insn_deleted
  1174. |= move_nan_for_stack_reg (insn2, regstack, u);
  1175. }
  1176. }
  1177. if (get_hard_regnum (regstack, t) == -1)
  1178. control_flow_insn_deleted
  1179. |= move_nan_for_stack_reg (insn, regstack, t);
  1180. }
  1181. }
  1182. }
  1183. break;
  1184. }
  1185. case SET:
  1186. {
  1187. rtx *src1 = (rtx *) 0, *src2;
  1188. rtx src1_note, src2_note;
  1189. rtx pat_src;
  1190. dest = get_true_reg (&SET_DEST (pat));
  1191. src = get_true_reg (&SET_SRC (pat));
  1192. pat_src = SET_SRC (pat);
  1193. /* See if this is a `movM' pattern, and handle elsewhere if so. */
  1194. if (STACK_REG_P (*src)
  1195. || (STACK_REG_P (*dest)
  1196. && (REG_P (*src) || MEM_P (*src)
  1197. || CONST_DOUBLE_P (*src))))
  1198. {
  1199. control_flow_insn_deleted |= move_for_stack_reg (insn, regstack, pat);
  1200. break;
  1201. }
  1202. switch (GET_CODE (pat_src))
  1203. {
  1204. case COMPARE:
  1205. compare_for_stack_reg (insn, regstack, pat_src);
  1206. break;
  1207. case CALL:
  1208. {
  1209. int count;
  1210. for (count = hard_regno_nregs[REGNO (*dest)][GET_MODE (*dest)];
  1211. --count >= 0;)
  1212. {
  1213. regstack->reg[++regstack->top] = REGNO (*dest) + count;
  1214. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest) + count);
  1215. }
  1216. }
  1217. replace_reg (dest, FIRST_STACK_REG);
  1218. break;
  1219. case REG:
  1220. /* This is a `tstM2' case. */
  1221. gcc_assert (*dest == cc0_rtx);
  1222. src1 = src;
  1223. /* Fall through. */
  1224. case FLOAT_TRUNCATE:
  1225. case SQRT:
  1226. case ABS:
  1227. case NEG:
  1228. /* These insns only operate on the top of the stack. DEST might
  1229. be cc0_rtx if we're processing a tstM pattern. Also, it's
  1230. possible that the tstM case results in a REG_DEAD note on the
  1231. source. */
  1232. if (src1 == 0)
  1233. src1 = get_true_reg (&XEXP (pat_src, 0));
  1234. emit_swap_insn (insn, regstack, *src1);
  1235. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1236. if (STACK_REG_P (*dest))
  1237. replace_reg (dest, FIRST_STACK_REG);
  1238. if (src1_note)
  1239. {
  1240. replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
  1241. regstack->top--;
  1242. CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
  1243. }
  1244. replace_reg (src1, FIRST_STACK_REG);
  1245. break;
  1246. case MINUS:
  1247. case DIV:
  1248. /* On i386, reversed forms of subM3 and divM3 exist for
  1249. MODE_FLOAT, so the same code that works for addM3 and mulM3
  1250. can be used. */
  1251. case MULT:
  1252. case PLUS:
  1253. /* These insns can accept the top of stack as a destination
  1254. from a stack reg or mem, or can use the top of stack as a
  1255. source and some other stack register (possibly top of stack)
  1256. as a destination. */
  1257. src1 = get_true_reg (&XEXP (pat_src, 0));
  1258. src2 = get_true_reg (&XEXP (pat_src, 1));
  1259. /* We will fix any death note later. */
  1260. if (STACK_REG_P (*src1))
  1261. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1262. else
  1263. src1_note = NULL_RTX;
  1264. if (STACK_REG_P (*src2))
  1265. src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
  1266. else
  1267. src2_note = NULL_RTX;
  1268. /* If either operand is not a stack register, then the dest
  1269. must be top of stack. */
  1270. if (! STACK_REG_P (*src1) || ! STACK_REG_P (*src2))
  1271. emit_swap_insn (insn, regstack, *dest);
  1272. else
  1273. {
  1274. /* Both operands are REG. If neither operand is already
  1275. at the top of stack, choose to make the one that is the
  1276. dest the new top of stack. */
  1277. int src1_hard_regnum, src2_hard_regnum;
  1278. src1_hard_regnum = get_hard_regnum (regstack, *src1);
  1279. src2_hard_regnum = get_hard_regnum (regstack, *src2);
  1280. /* If the source is not live, this is yet another case of
  1281. uninitialized variables. Load up a NaN instead. */
  1282. if (src1_hard_regnum == -1)
  1283. {
  1284. rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src1);
  1285. rtx_insn *insn2 = emit_insn_before (pat2, insn);
  1286. control_flow_insn_deleted
  1287. |= move_nan_for_stack_reg (insn2, regstack, *src1);
  1288. }
  1289. if (src2_hard_regnum == -1)
  1290. {
  1291. rtx pat2 = gen_rtx_CLOBBER (VOIDmode, *src2);
  1292. rtx_insn *insn2 = emit_insn_before (pat2, insn);
  1293. control_flow_insn_deleted
  1294. |= move_nan_for_stack_reg (insn2, regstack, *src2);
  1295. }
  1296. if (src1_hard_regnum != FIRST_STACK_REG
  1297. && src2_hard_regnum != FIRST_STACK_REG)
  1298. emit_swap_insn (insn, regstack, *dest);
  1299. }
  1300. if (STACK_REG_P (*src1))
  1301. replace_reg (src1, get_hard_regnum (regstack, *src1));
  1302. if (STACK_REG_P (*src2))
  1303. replace_reg (src2, get_hard_regnum (regstack, *src2));
  1304. if (src1_note)
  1305. {
  1306. rtx src1_reg = XEXP (src1_note, 0);
  1307. /* If the register that dies is at the top of stack, then
  1308. the destination is somewhere else - merely substitute it.
  1309. But if the reg that dies is not at top of stack, then
  1310. move the top of stack to the dead reg, as though we had
  1311. done the insn and then a store-with-pop. */
  1312. if (REGNO (src1_reg) == regstack->reg[regstack->top])
  1313. {
  1314. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1315. replace_reg (dest, get_hard_regnum (regstack, *dest));
  1316. }
  1317. else
  1318. {
  1319. int regno = get_hard_regnum (regstack, src1_reg);
  1320. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1321. replace_reg (dest, regno);
  1322. regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
  1323. = regstack->reg[regstack->top];
  1324. }
  1325. CLEAR_HARD_REG_BIT (regstack->reg_set,
  1326. REGNO (XEXP (src1_note, 0)));
  1327. replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
  1328. regstack->top--;
  1329. }
  1330. else if (src2_note)
  1331. {
  1332. rtx src2_reg = XEXP (src2_note, 0);
  1333. if (REGNO (src2_reg) == regstack->reg[regstack->top])
  1334. {
  1335. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1336. replace_reg (dest, get_hard_regnum (regstack, *dest));
  1337. }
  1338. else
  1339. {
  1340. int regno = get_hard_regnum (regstack, src2_reg);
  1341. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1342. replace_reg (dest, regno);
  1343. regstack->reg[regstack->top - (regno - FIRST_STACK_REG)]
  1344. = regstack->reg[regstack->top];
  1345. }
  1346. CLEAR_HARD_REG_BIT (regstack->reg_set,
  1347. REGNO (XEXP (src2_note, 0)));
  1348. replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG);
  1349. regstack->top--;
  1350. }
  1351. else
  1352. {
  1353. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1354. replace_reg (dest, get_hard_regnum (regstack, *dest));
  1355. }
  1356. /* Keep operand 1 matching with destination. */
  1357. if (COMMUTATIVE_ARITH_P (pat_src)
  1358. && REG_P (*src1) && REG_P (*src2)
  1359. && REGNO (*src1) != REGNO (*dest))
  1360. {
  1361. int tmp = REGNO (*src1);
  1362. replace_reg (src1, REGNO (*src2));
  1363. replace_reg (src2, tmp);
  1364. }
  1365. break;
  1366. case UNSPEC:
  1367. switch (XINT (pat_src, 1))
  1368. {
  1369. case UNSPEC_FIST:
  1370. case UNSPEC_FIST_ATOMIC:
  1371. case UNSPEC_FIST_FLOOR:
  1372. case UNSPEC_FIST_CEIL:
  1373. /* These insns only operate on the top of the stack. */
  1374. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1375. emit_swap_insn (insn, regstack, *src1);
  1376. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1377. if (STACK_REG_P (*dest))
  1378. replace_reg (dest, FIRST_STACK_REG);
  1379. if (src1_note)
  1380. {
  1381. replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
  1382. regstack->top--;
  1383. CLEAR_HARD_REG_BIT (regstack->reg_set, REGNO (*src1));
  1384. }
  1385. replace_reg (src1, FIRST_STACK_REG);
  1386. break;
  1387. case UNSPEC_FXAM:
  1388. /* This insn only operate on the top of the stack. */
  1389. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1390. emit_swap_insn (insn, regstack, *src1);
  1391. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1392. replace_reg (src1, FIRST_STACK_REG);
  1393. if (src1_note)
  1394. {
  1395. remove_regno_note (insn, REG_DEAD,
  1396. REGNO (XEXP (src1_note, 0)));
  1397. emit_pop_insn (insn, regstack, XEXP (src1_note, 0),
  1398. EMIT_AFTER);
  1399. }
  1400. break;
  1401. case UNSPEC_SIN:
  1402. case UNSPEC_COS:
  1403. case UNSPEC_FRNDINT:
  1404. case UNSPEC_F2XM1:
  1405. case UNSPEC_FRNDINT_FLOOR:
  1406. case UNSPEC_FRNDINT_CEIL:
  1407. case UNSPEC_FRNDINT_TRUNC:
  1408. case UNSPEC_FRNDINT_MASK_PM:
  1409. /* Above insns operate on the top of the stack. */
  1410. case UNSPEC_SINCOS_COS:
  1411. case UNSPEC_XTRACT_FRACT:
  1412. /* Above insns operate on the top two stack slots,
  1413. first part of one input, double output insn. */
  1414. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1415. emit_swap_insn (insn, regstack, *src1);
  1416. /* Input should never die, it is replaced with output. */
  1417. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1418. gcc_assert (!src1_note);
  1419. if (STACK_REG_P (*dest))
  1420. replace_reg (dest, FIRST_STACK_REG);
  1421. replace_reg (src1, FIRST_STACK_REG);
  1422. break;
  1423. case UNSPEC_SINCOS_SIN:
  1424. case UNSPEC_XTRACT_EXP:
  1425. /* These insns operate on the top two stack slots,
  1426. second part of one input, double output insn. */
  1427. regstack->top++;
  1428. /* FALLTHRU */
  1429. case UNSPEC_TAN:
  1430. /* For UNSPEC_TAN, regstack->top is already increased
  1431. by inherent load of constant 1.0. */
  1432. /* Output value is generated in the second stack slot.
  1433. Move current value from second slot to the top. */
  1434. regstack->reg[regstack->top]
  1435. = regstack->reg[regstack->top - 1];
  1436. gcc_assert (STACK_REG_P (*dest));
  1437. regstack->reg[regstack->top - 1] = REGNO (*dest);
  1438. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1439. replace_reg (dest, FIRST_STACK_REG + 1);
  1440. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1441. replace_reg (src1, FIRST_STACK_REG);
  1442. break;
  1443. case UNSPEC_FPATAN:
  1444. case UNSPEC_FYL2X:
  1445. case UNSPEC_FYL2XP1:
  1446. /* These insns operate on the top two stack slots. */
  1447. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1448. src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
  1449. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1450. src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
  1451. swap_to_top (insn, regstack, *src1, *src2);
  1452. replace_reg (src1, FIRST_STACK_REG);
  1453. replace_reg (src2, FIRST_STACK_REG + 1);
  1454. if (src1_note)
  1455. replace_reg (&XEXP (src1_note, 0), FIRST_STACK_REG);
  1456. if (src2_note)
  1457. replace_reg (&XEXP (src2_note, 0), FIRST_STACK_REG + 1);
  1458. /* Pop both input operands from the stack. */
  1459. CLEAR_HARD_REG_BIT (regstack->reg_set,
  1460. regstack->reg[regstack->top]);
  1461. CLEAR_HARD_REG_BIT (regstack->reg_set,
  1462. regstack->reg[regstack->top - 1]);
  1463. regstack->top -= 2;
  1464. /* Push the result back onto the stack. */
  1465. regstack->reg[++regstack->top] = REGNO (*dest);
  1466. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1467. replace_reg (dest, FIRST_STACK_REG);
  1468. break;
  1469. case UNSPEC_FSCALE_FRACT:
  1470. case UNSPEC_FPREM_F:
  1471. case UNSPEC_FPREM1_F:
  1472. /* These insns operate on the top two stack slots,
  1473. first part of double input, double output insn. */
  1474. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1475. src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
  1476. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1477. src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
  1478. /* Inputs should never die, they are
  1479. replaced with outputs. */
  1480. gcc_assert (!src1_note);
  1481. gcc_assert (!src2_note);
  1482. swap_to_top (insn, regstack, *src1, *src2);
  1483. /* Push the result back onto stack. Empty stack slot
  1484. will be filled in second part of insn. */
  1485. if (STACK_REG_P (*dest))
  1486. {
  1487. regstack->reg[regstack->top] = REGNO (*dest);
  1488. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1489. replace_reg (dest, FIRST_STACK_REG);
  1490. }
  1491. replace_reg (src1, FIRST_STACK_REG);
  1492. replace_reg (src2, FIRST_STACK_REG + 1);
  1493. break;
  1494. case UNSPEC_FSCALE_EXP:
  1495. case UNSPEC_FPREM_U:
  1496. case UNSPEC_FPREM1_U:
  1497. /* These insns operate on the top two stack slots,
  1498. second part of double input, double output insn. */
  1499. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1500. src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
  1501. /* Push the result back onto stack. Fill empty slot from
  1502. first part of insn and fix top of stack pointer. */
  1503. if (STACK_REG_P (*dest))
  1504. {
  1505. regstack->reg[regstack->top - 1] = REGNO (*dest);
  1506. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1507. replace_reg (dest, FIRST_STACK_REG + 1);
  1508. }
  1509. replace_reg (src1, FIRST_STACK_REG);
  1510. replace_reg (src2, FIRST_STACK_REG + 1);
  1511. break;
  1512. case UNSPEC_C2_FLAG:
  1513. /* This insn operates on the top two stack slots,
  1514. third part of C2 setting double input insn. */
  1515. src1 = get_true_reg (&XVECEXP (pat_src, 0, 0));
  1516. src2 = get_true_reg (&XVECEXP (pat_src, 0, 1));
  1517. replace_reg (src1, FIRST_STACK_REG);
  1518. replace_reg (src2, FIRST_STACK_REG + 1);
  1519. break;
  1520. case UNSPEC_SAHF:
  1521. /* (unspec [(unspec [(compare)] UNSPEC_FNSTSW)] UNSPEC_SAHF)
  1522. The combination matches the PPRO fcomi instruction. */
  1523. pat_src = XVECEXP (pat_src, 0, 0);
  1524. gcc_assert (GET_CODE (pat_src) == UNSPEC);
  1525. gcc_assert (XINT (pat_src, 1) == UNSPEC_FNSTSW);
  1526. /* Fall through. */
  1527. case UNSPEC_FNSTSW:
  1528. /* Combined fcomp+fnstsw generated for doing well with
  1529. CSE. When optimizing this would have been broken
  1530. up before now. */
  1531. pat_src = XVECEXP (pat_src, 0, 0);
  1532. gcc_assert (GET_CODE (pat_src) == COMPARE);
  1533. compare_for_stack_reg (insn, regstack, pat_src);
  1534. break;
  1535. default:
  1536. gcc_unreachable ();
  1537. }
  1538. break;
  1539. case IF_THEN_ELSE:
  1540. /* This insn requires the top of stack to be the destination. */
  1541. src1 = get_true_reg (&XEXP (pat_src, 1));
  1542. src2 = get_true_reg (&XEXP (pat_src, 2));
  1543. src1_note = find_regno_note (insn, REG_DEAD, REGNO (*src1));
  1544. src2_note = find_regno_note (insn, REG_DEAD, REGNO (*src2));
  1545. /* If the comparison operator is an FP comparison operator,
  1546. it is handled correctly by compare_for_stack_reg () who
  1547. will move the destination to the top of stack. But if the
  1548. comparison operator is not an FP comparison operator, we
  1549. have to handle it here. */
  1550. if (get_hard_regnum (regstack, *dest) >= FIRST_STACK_REG
  1551. && REGNO (*dest) != regstack->reg[regstack->top])
  1552. {
  1553. /* In case one of operands is the top of stack and the operands
  1554. dies, it is safe to make it the destination operand by
  1555. reversing the direction of cmove and avoid fxch. */
  1556. if ((REGNO (*src1) == regstack->reg[regstack->top]
  1557. && src1_note)
  1558. || (REGNO (*src2) == regstack->reg[regstack->top]
  1559. && src2_note))
  1560. {
  1561. int idx1 = (get_hard_regnum (regstack, *src1)
  1562. - FIRST_STACK_REG);
  1563. int idx2 = (get_hard_regnum (regstack, *src2)
  1564. - FIRST_STACK_REG);
  1565. /* Make reg-stack believe that the operands are already
  1566. swapped on the stack */
  1567. regstack->reg[regstack->top - idx1] = REGNO (*src2);
  1568. regstack->reg[regstack->top - idx2] = REGNO (*src1);
  1569. /* Reverse condition to compensate the operand swap.
  1570. i386 do have comparison always reversible. */
  1571. PUT_CODE (XEXP (pat_src, 0),
  1572. reversed_comparison_code (XEXP (pat_src, 0), insn));
  1573. }
  1574. else
  1575. emit_swap_insn (insn, regstack, *dest);
  1576. }
  1577. {
  1578. rtx src_note [3];
  1579. int i;
  1580. src_note[0] = 0;
  1581. src_note[1] = src1_note;
  1582. src_note[2] = src2_note;
  1583. if (STACK_REG_P (*src1))
  1584. replace_reg (src1, get_hard_regnum (regstack, *src1));
  1585. if (STACK_REG_P (*src2))
  1586. replace_reg (src2, get_hard_regnum (regstack, *src2));
  1587. for (i = 1; i <= 2; i++)
  1588. if (src_note [i])
  1589. {
  1590. int regno = REGNO (XEXP (src_note[i], 0));
  1591. /* If the register that dies is not at the top of
  1592. stack, then move the top of stack to the dead reg.
  1593. Top of stack should never die, as it is the
  1594. destination. */
  1595. gcc_assert (regno != regstack->reg[regstack->top]);
  1596. remove_regno_note (insn, REG_DEAD, regno);
  1597. emit_pop_insn (insn, regstack, XEXP (src_note[i], 0),
  1598. EMIT_AFTER);
  1599. }
  1600. }
  1601. /* Make dest the top of stack. Add dest to regstack if
  1602. not present. */
  1603. if (get_hard_regnum (regstack, *dest) < FIRST_STACK_REG)
  1604. regstack->reg[++regstack->top] = REGNO (*dest);
  1605. SET_HARD_REG_BIT (regstack->reg_set, REGNO (*dest));
  1606. replace_reg (dest, FIRST_STACK_REG);
  1607. break;
  1608. default:
  1609. gcc_unreachable ();
  1610. }
  1611. break;
  1612. }
  1613. default:
  1614. break;
  1615. }
  1616. return control_flow_insn_deleted;
  1617. }
  1618. /* Substitute hard regnums for any stack regs in INSN, which has
  1619. N_INPUTS inputs and N_OUTPUTS outputs. REGSTACK is the stack info
  1620. before the insn, and is updated with changes made here.
  1621. There are several requirements and assumptions about the use of
  1622. stack-like regs in asm statements. These rules are enforced by
  1623. record_asm_stack_regs; see comments there for details. Any
  1624. asm_operands left in the RTL at this point may be assume to meet the
  1625. requirements, since record_asm_stack_regs removes any problem asm. */
  1626. static void
  1627. subst_asm_stack_regs (rtx_insn *insn, stack_ptr regstack)
  1628. {
  1629. rtx body = PATTERN (insn);
  1630. rtx *note_reg; /* Array of note contents */
  1631. rtx **note_loc; /* Address of REG field of each note */
  1632. enum reg_note *note_kind; /* The type of each note */
  1633. rtx *clobber_reg = 0;
  1634. rtx **clobber_loc = 0;
  1635. struct stack_def temp_stack;
  1636. int n_notes;
  1637. int n_clobbers;
  1638. rtx note;
  1639. int i;
  1640. int n_inputs, n_outputs;
  1641. if (! check_asm_stack_operands (insn))
  1642. return;
  1643. /* Find out what the constraints required. If no constraint
  1644. alternative matches, that is a compiler bug: we should have caught
  1645. such an insn in check_asm_stack_operands. */
  1646. extract_constrain_insn (insn);
  1647. preprocess_constraints (insn);
  1648. const operand_alternative *op_alt = which_op_alt ();
  1649. get_asm_operands_in_out (body, &n_outputs, &n_inputs);
  1650. /* Strip SUBREGs here to make the following code simpler. */
  1651. for (i = 0; i < recog_data.n_operands; i++)
  1652. if (GET_CODE (recog_data.operand[i]) == SUBREG
  1653. && REG_P (SUBREG_REG (recog_data.operand[i])))
  1654. {
  1655. recog_data.operand_loc[i] = & SUBREG_REG (recog_data.operand[i]);
  1656. recog_data.operand[i] = SUBREG_REG (recog_data.operand[i]);
  1657. }
  1658. /* Set up NOTE_REG, NOTE_LOC and NOTE_KIND. */
  1659. for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1))
  1660. i++;
  1661. note_reg = XALLOCAVEC (rtx, i);
  1662. note_loc = XALLOCAVEC (rtx *, i);
  1663. note_kind = XALLOCAVEC (enum reg_note, i);
  1664. n_notes = 0;
  1665. for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  1666. {
  1667. if (GET_CODE (note) != EXPR_LIST)
  1668. continue;
  1669. rtx reg = XEXP (note, 0);
  1670. rtx *loc = & XEXP (note, 0);
  1671. if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
  1672. {
  1673. loc = & SUBREG_REG (reg);
  1674. reg = SUBREG_REG (reg);
  1675. }
  1676. if (STACK_REG_P (reg)
  1677. && (REG_NOTE_KIND (note) == REG_DEAD
  1678. || REG_NOTE_KIND (note) == REG_UNUSED))
  1679. {
  1680. note_reg[n_notes] = reg;
  1681. note_loc[n_notes] = loc;
  1682. note_kind[n_notes] = REG_NOTE_KIND (note);
  1683. n_notes++;
  1684. }
  1685. }
  1686. /* Set up CLOBBER_REG and CLOBBER_LOC. */
  1687. n_clobbers = 0;
  1688. if (GET_CODE (body) == PARALLEL)
  1689. {
  1690. clobber_reg = XALLOCAVEC (rtx, XVECLEN (body, 0));
  1691. clobber_loc = XALLOCAVEC (rtx *, XVECLEN (body, 0));
  1692. for (i = 0; i < XVECLEN (body, 0); i++)
  1693. if (GET_CODE (XVECEXP (body, 0, i)) == CLOBBER)
  1694. {
  1695. rtx clobber = XVECEXP (body, 0, i);
  1696. rtx reg = XEXP (clobber, 0);
  1697. rtx *loc = & XEXP (clobber, 0);
  1698. if (GET_CODE (reg) == SUBREG && REG_P (SUBREG_REG (reg)))
  1699. {
  1700. loc = & SUBREG_REG (reg);
  1701. reg = SUBREG_REG (reg);
  1702. }
  1703. if (STACK_REG_P (reg))
  1704. {
  1705. clobber_reg[n_clobbers] = reg;
  1706. clobber_loc[n_clobbers] = loc;
  1707. n_clobbers++;
  1708. }
  1709. }
  1710. }
  1711. temp_stack = *regstack;
  1712. /* Put the input regs into the desired place in TEMP_STACK. */
  1713. for (i = n_outputs; i < n_outputs + n_inputs; i++)
  1714. if (STACK_REG_P (recog_data.operand[i])
  1715. && reg_class_subset_p (op_alt[i].cl, FLOAT_REGS)
  1716. && op_alt[i].cl != FLOAT_REGS)
  1717. {
  1718. /* If an operand needs to be in a particular reg in
  1719. FLOAT_REGS, the constraint was either 't' or 'u'. Since
  1720. these constraints are for single register classes, and
  1721. reload guaranteed that operand[i] is already in that class,
  1722. we can just use REGNO (recog_data.operand[i]) to know which
  1723. actual reg this operand needs to be in. */
  1724. int regno = get_hard_regnum (&temp_stack, recog_data.operand[i]);
  1725. gcc_assert (regno >= 0);
  1726. if ((unsigned int) regno != REGNO (recog_data.operand[i]))
  1727. {
  1728. /* recog_data.operand[i] is not in the right place. Find
  1729. it and swap it with whatever is already in I's place.
  1730. K is where recog_data.operand[i] is now. J is where it
  1731. should be. */
  1732. int j, k, temp;
  1733. k = temp_stack.top - (regno - FIRST_STACK_REG);
  1734. j = (temp_stack.top
  1735. - (REGNO (recog_data.operand[i]) - FIRST_STACK_REG));
  1736. temp = temp_stack.reg[k];
  1737. temp_stack.reg[k] = temp_stack.reg[j];
  1738. temp_stack.reg[j] = temp;
  1739. }
  1740. }
  1741. /* Emit insns before INSN to make sure the reg-stack is in the right
  1742. order. */
  1743. change_stack (insn, regstack, &temp_stack, EMIT_BEFORE);
  1744. /* Make the needed input register substitutions. Do death notes and
  1745. clobbers too, because these are for inputs, not outputs. */
  1746. for (i = n_outputs; i < n_outputs + n_inputs; i++)
  1747. if (STACK_REG_P (recog_data.operand[i]))
  1748. {
  1749. int regnum = get_hard_regnum (regstack, recog_data.operand[i]);
  1750. gcc_assert (regnum >= 0);
  1751. replace_reg (recog_data.operand_loc[i], regnum);
  1752. }
  1753. for (i = 0; i < n_notes; i++)
  1754. if (note_kind[i] == REG_DEAD)
  1755. {
  1756. int regnum = get_hard_regnum (regstack, note_reg[i]);
  1757. gcc_assert (regnum >= 0);
  1758. replace_reg (note_loc[i], regnum);
  1759. }
  1760. for (i = 0; i < n_clobbers; i++)
  1761. {
  1762. /* It's OK for a CLOBBER to reference a reg that is not live.
  1763. Don't try to replace it in that case. */
  1764. int regnum = get_hard_regnum (regstack, clobber_reg[i]);
  1765. if (regnum >= 0)
  1766. {
  1767. /* Sigh - clobbers always have QImode. But replace_reg knows
  1768. that these regs can't be MODE_INT and will assert. Just put
  1769. the right reg there without calling replace_reg. */
  1770. *clobber_loc[i] = FP_MODE_REG (regnum, DFmode);
  1771. }
  1772. }
  1773. /* Now remove from REGSTACK any inputs that the asm implicitly popped. */
  1774. for (i = n_outputs; i < n_outputs + n_inputs; i++)
  1775. if (STACK_REG_P (recog_data.operand[i]))
  1776. {
  1777. /* An input reg is implicitly popped if it is tied to an
  1778. output, or if there is a CLOBBER for it. */
  1779. int j;
  1780. for (j = 0; j < n_clobbers; j++)
  1781. if (operands_match_p (clobber_reg[j], recog_data.operand[i]))
  1782. break;
  1783. if (j < n_clobbers || op_alt[i].matches >= 0)
  1784. {
  1785. /* recog_data.operand[i] might not be at the top of stack.
  1786. But that's OK, because all we need to do is pop the
  1787. right number of regs off of the top of the reg-stack.
  1788. record_asm_stack_regs guaranteed that all implicitly
  1789. popped regs were grouped at the top of the reg-stack. */
  1790. CLEAR_HARD_REG_BIT (regstack->reg_set,
  1791. regstack->reg[regstack->top]);
  1792. regstack->top--;
  1793. }
  1794. }
  1795. /* Now add to REGSTACK any outputs that the asm implicitly pushed.
  1796. Note that there isn't any need to substitute register numbers.
  1797. ??? Explain why this is true. */
  1798. for (i = LAST_STACK_REG; i >= FIRST_STACK_REG; i--)
  1799. {
  1800. /* See if there is an output for this hard reg. */
  1801. int j;
  1802. for (j = 0; j < n_outputs; j++)
  1803. if (STACK_REG_P (recog_data.operand[j])
  1804. && REGNO (recog_data.operand[j]) == (unsigned) i)
  1805. {
  1806. regstack->reg[++regstack->top] = i;
  1807. SET_HARD_REG_BIT (regstack->reg_set, i);
  1808. break;
  1809. }
  1810. }
  1811. /* Now emit a pop insn for any REG_UNUSED output, or any REG_DEAD
  1812. input that the asm didn't implicitly pop. If the asm didn't
  1813. implicitly pop an input reg, that reg will still be live.
  1814. Note that we can't use find_regno_note here: the register numbers
  1815. in the death notes have already been substituted. */
  1816. for (i = 0; i < n_outputs; i++)
  1817. if (STACK_REG_P (recog_data.operand[i]))
  1818. {
  1819. int j;
  1820. for (j = 0; j < n_notes; j++)
  1821. if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
  1822. && note_kind[j] == REG_UNUSED)
  1823. {
  1824. insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
  1825. EMIT_AFTER);
  1826. break;
  1827. }
  1828. }
  1829. for (i = n_outputs; i < n_outputs + n_inputs; i++)
  1830. if (STACK_REG_P (recog_data.operand[i]))
  1831. {
  1832. int j;
  1833. for (j = 0; j < n_notes; j++)
  1834. if (REGNO (recog_data.operand[i]) == REGNO (note_reg[j])
  1835. && note_kind[j] == REG_DEAD
  1836. && TEST_HARD_REG_BIT (regstack->reg_set,
  1837. REGNO (recog_data.operand[i])))
  1838. {
  1839. insn = emit_pop_insn (insn, regstack, recog_data.operand[i],
  1840. EMIT_AFTER);
  1841. break;
  1842. }
  1843. }
  1844. }
  1845. /* Substitute stack hard reg numbers for stack virtual registers in
  1846. INSN. Non-stack register numbers are not changed. REGSTACK is the
  1847. current stack content. Insns may be emitted as needed to arrange the
  1848. stack for the 387 based on the contents of the insn. Return whether
  1849. a control flow insn was deleted in the process. */
  1850. static bool
  1851. subst_stack_regs (rtx_insn *insn, stack_ptr regstack)
  1852. {
  1853. rtx *note_link, note;
  1854. bool control_flow_insn_deleted = false;
  1855. int i;
  1856. if (CALL_P (insn))
  1857. {
  1858. int top = regstack->top;
  1859. /* If there are any floating point parameters to be passed in
  1860. registers for this call, make sure they are in the right
  1861. order. */
  1862. if (top >= 0)
  1863. {
  1864. straighten_stack (insn, regstack);
  1865. /* Now mark the arguments as dead after the call. */
  1866. while (regstack->top >= 0)
  1867. {
  1868. CLEAR_HARD_REG_BIT (regstack->reg_set, FIRST_STACK_REG + regstack->top);
  1869. regstack->top--;
  1870. }
  1871. }
  1872. }
  1873. /* Do the actual substitution if any stack regs are mentioned.
  1874. Since we only record whether entire insn mentions stack regs, and
  1875. subst_stack_regs_pat only works for patterns that contain stack regs,
  1876. we must check each pattern in a parallel here. A call_value_pop could
  1877. fail otherwise. */
  1878. if (stack_regs_mentioned (insn))
  1879. {
  1880. int n_operands = asm_noperands (PATTERN (insn));
  1881. if (n_operands >= 0)
  1882. {
  1883. /* This insn is an `asm' with operands. Decode the operands,
  1884. decide how many are inputs, and do register substitution.
  1885. Any REG_UNUSED notes will be handled by subst_asm_stack_regs. */
  1886. subst_asm_stack_regs (insn, regstack);
  1887. return control_flow_insn_deleted;
  1888. }
  1889. if (GET_CODE (PATTERN (insn)) == PARALLEL)
  1890. for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
  1891. {
  1892. if (stack_regs_mentioned_p (XVECEXP (PATTERN (insn), 0, i)))
  1893. {
  1894. if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == CLOBBER)
  1895. XVECEXP (PATTERN (insn), 0, i)
  1896. = shallow_copy_rtx (XVECEXP (PATTERN (insn), 0, i));
  1897. control_flow_insn_deleted
  1898. |= subst_stack_regs_pat (insn, regstack,
  1899. XVECEXP (PATTERN (insn), 0, i));
  1900. }
  1901. }
  1902. else
  1903. control_flow_insn_deleted
  1904. |= subst_stack_regs_pat (insn, regstack, PATTERN (insn));
  1905. }
  1906. /* subst_stack_regs_pat may have deleted a no-op insn. If so, any
  1907. REG_UNUSED will already have been dealt with, so just return. */
  1908. if (NOTE_P (insn) || insn->deleted ())
  1909. return control_flow_insn_deleted;
  1910. /* If this a noreturn call, we can't insert pop insns after it.
  1911. Instead, reset the stack state to empty. */
  1912. if (CALL_P (insn)
  1913. && find_reg_note (insn, REG_NORETURN, NULL))
  1914. {
  1915. regstack->top = -1;
  1916. CLEAR_HARD_REG_SET (regstack->reg_set);
  1917. return control_flow_insn_deleted;
  1918. }
  1919. /* If there is a REG_UNUSED note on a stack register on this insn,
  1920. the indicated reg must be popped. The REG_UNUSED note is removed,
  1921. since the form of the newly emitted pop insn references the reg,
  1922. making it no longer `unset'. */
  1923. note_link = &REG_NOTES (insn);
  1924. for (note = *note_link; note; note = XEXP (note, 1))
  1925. if (REG_NOTE_KIND (note) == REG_UNUSED && STACK_REG_P (XEXP (note, 0)))
  1926. {
  1927. *note_link = XEXP (note, 1);
  1928. insn = emit_pop_insn (insn, regstack, XEXP (note, 0), EMIT_AFTER);
  1929. }
  1930. else
  1931. note_link = &XEXP (note, 1);
  1932. return control_flow_insn_deleted;
  1933. }
  1934. /* Change the organization of the stack so that it fits a new basic
  1935. block. Some registers might have to be popped, but there can never be
  1936. a register live in the new block that is not now live.
  1937. Insert any needed insns before or after INSN, as indicated by
  1938. WHERE. OLD is the original stack layout, and NEW is the desired
  1939. form. OLD is updated to reflect the code emitted, i.e., it will be
  1940. the same as NEW upon return.
  1941. This function will not preserve block_end[]. But that information
  1942. is no longer needed once this has executed. */
  1943. static void
  1944. change_stack (rtx_insn *insn, stack_ptr old, stack_ptr new_stack,
  1945. enum emit_where where)
  1946. {
  1947. int reg;
  1948. int update_end = 0;
  1949. int i;
  1950. /* Stack adjustments for the first insn in a block update the
  1951. current_block's stack_in instead of inserting insns directly.
  1952. compensate_edges will add the necessary code later. */
  1953. if (current_block
  1954. && starting_stack_p
  1955. && where == EMIT_BEFORE)
  1956. {
  1957. BLOCK_INFO (current_block)->stack_in = *new_stack;
  1958. starting_stack_p = false;
  1959. *old = *new_stack;
  1960. return;
  1961. }
  1962. /* We will be inserting new insns "backwards". If we are to insert
  1963. after INSN, find the next insn, and insert before it. */
  1964. if (where == EMIT_AFTER)
  1965. {
  1966. if (current_block && BB_END (current_block) == insn)
  1967. update_end = 1;
  1968. insn = NEXT_INSN (insn);
  1969. }
  1970. /* Initialize partially dead variables. */
  1971. for (i = FIRST_STACK_REG; i < LAST_STACK_REG + 1; i++)
  1972. if (TEST_HARD_REG_BIT (new_stack->reg_set, i)
  1973. && !TEST_HARD_REG_BIT (old->reg_set, i))
  1974. {
  1975. old->reg[++old->top] = i;
  1976. SET_HARD_REG_BIT (old->reg_set, i);
  1977. emit_insn_before (gen_rtx_SET (VOIDmode,
  1978. FP_MODE_REG (i, SFmode), not_a_num), insn);
  1979. }
  1980. /* Pop any registers that are not needed in the new block. */
  1981. /* If the destination block's stack already has a specified layout
  1982. and contains two or more registers, use a more intelligent algorithm
  1983. to pop registers that minimizes the number number of fxchs below. */
  1984. if (new_stack->top > 0)
  1985. {
  1986. bool slots[REG_STACK_SIZE];
  1987. int pops[REG_STACK_SIZE];
  1988. int next, dest, topsrc;
  1989. /* First pass to determine the free slots. */
  1990. for (reg = 0; reg <= new_stack->top; reg++)
  1991. slots[reg] = TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]);
  1992. /* Second pass to allocate preferred slots. */
  1993. topsrc = -1;
  1994. for (reg = old->top; reg > new_stack->top; reg--)
  1995. if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]))
  1996. {
  1997. dest = -1;
  1998. for (next = 0; next <= new_stack->top; next++)
  1999. if (!slots[next] && new_stack->reg[next] == old->reg[reg])
  2000. {
  2001. /* If this is a preference for the new top of stack, record
  2002. the fact by remembering it's old->reg in topsrc. */
  2003. if (next == new_stack->top)
  2004. topsrc = reg;
  2005. slots[next] = true;
  2006. dest = next;
  2007. break;
  2008. }
  2009. pops[reg] = dest;
  2010. }
  2011. else
  2012. pops[reg] = reg;
  2013. /* Intentionally, avoid placing the top of stack in it's correct
  2014. location, if we still need to permute the stack below and we
  2015. can usefully place it somewhere else. This is the case if any
  2016. slot is still unallocated, in which case we should place the
  2017. top of stack there. */
  2018. if (topsrc != -1)
  2019. for (reg = 0; reg < new_stack->top; reg++)
  2020. if (!slots[reg])
  2021. {
  2022. pops[topsrc] = reg;
  2023. slots[new_stack->top] = false;
  2024. slots[reg] = true;
  2025. break;
  2026. }
  2027. /* Third pass allocates remaining slots and emits pop insns. */
  2028. next = new_stack->top;
  2029. for (reg = old->top; reg > new_stack->top; reg--)
  2030. {
  2031. dest = pops[reg];
  2032. if (dest == -1)
  2033. {
  2034. /* Find next free slot. */
  2035. while (slots[next])
  2036. next--;
  2037. dest = next--;
  2038. }
  2039. emit_pop_insn (insn, old, FP_MODE_REG (old->reg[dest], DFmode),
  2040. EMIT_BEFORE);
  2041. }
  2042. }
  2043. else
  2044. {
  2045. /* The following loop attempts to maximize the number of times we
  2046. pop the top of the stack, as this permits the use of the faster
  2047. ffreep instruction on platforms that support it. */
  2048. int live, next;
  2049. live = 0;
  2050. for (reg = 0; reg <= old->top; reg++)
  2051. if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[reg]))
  2052. live++;
  2053. next = live;
  2054. while (old->top >= live)
  2055. if (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[old->top]))
  2056. {
  2057. while (TEST_HARD_REG_BIT (new_stack->reg_set, old->reg[next]))
  2058. next--;
  2059. emit_pop_insn (insn, old, FP_MODE_REG (old->reg[next], DFmode),
  2060. EMIT_BEFORE);
  2061. }
  2062. else
  2063. emit_pop_insn (insn, old, FP_MODE_REG (old->reg[old->top], DFmode),
  2064. EMIT_BEFORE);
  2065. }
  2066. if (new_stack->top == -2)
  2067. {
  2068. /* If the new block has never been processed, then it can inherit
  2069. the old stack order. */
  2070. new_stack->top = old->top;
  2071. memcpy (new_stack->reg, old->reg, sizeof (new_stack->reg));
  2072. }
  2073. else
  2074. {
  2075. /* This block has been entered before, and we must match the
  2076. previously selected stack order. */
  2077. /* By now, the only difference should be the order of the stack,
  2078. not their depth or liveliness. */
  2079. gcc_assert (hard_reg_set_equal_p (old->reg_set, new_stack->reg_set));
  2080. gcc_assert (old->top == new_stack->top);
  2081. /* If the stack is not empty (new_stack->top != -1), loop here emitting
  2082. swaps until the stack is correct.
  2083. The worst case number of swaps emitted is N + 2, where N is the
  2084. depth of the stack. In some cases, the reg at the top of
  2085. stack may be correct, but swapped anyway in order to fix
  2086. other regs. But since we never swap any other reg away from
  2087. its correct slot, this algorithm will converge. */
  2088. if (new_stack->top != -1)
  2089. do
  2090. {
  2091. /* Swap the reg at top of stack into the position it is
  2092. supposed to be in, until the correct top of stack appears. */
  2093. while (old->reg[old->top] != new_stack->reg[new_stack->top])
  2094. {
  2095. for (reg = new_stack->top; reg >= 0; reg--)
  2096. if (new_stack->reg[reg] == old->reg[old->top])
  2097. break;
  2098. gcc_assert (reg != -1);
  2099. emit_swap_insn (insn, old,
  2100. FP_MODE_REG (old->reg[reg], DFmode));
  2101. }
  2102. /* See if any regs remain incorrect. If so, bring an
  2103. incorrect reg to the top of stack, and let the while loop
  2104. above fix it. */
  2105. for (reg = new_stack->top; reg >= 0; reg--)
  2106. if (new_stack->reg[reg] != old->reg[reg])
  2107. {
  2108. emit_swap_insn (insn, old,
  2109. FP_MODE_REG (old->reg[reg], DFmode));
  2110. break;
  2111. }
  2112. } while (reg >= 0);
  2113. /* At this point there must be no differences. */
  2114. for (reg = old->top; reg >= 0; reg--)
  2115. gcc_assert (old->reg[reg] == new_stack->reg[reg]);
  2116. }
  2117. if (update_end)
  2118. BB_END (current_block) = PREV_INSN (insn);
  2119. }
  2120. /* Print stack configuration. */
  2121. static void
  2122. print_stack (FILE *file, stack_ptr s)
  2123. {
  2124. if (! file)
  2125. return;
  2126. if (s->top == -2)
  2127. fprintf (file, "uninitialized\n");
  2128. else if (s->top == -1)
  2129. fprintf (file, "empty\n");
  2130. else
  2131. {
  2132. int i;
  2133. fputs ("[ ", file);
  2134. for (i = 0; i <= s->top; ++i)
  2135. fprintf (file, "%d ", s->reg[i]);
  2136. fputs ("]\n", file);
  2137. }
  2138. }
  2139. /* This function was doing life analysis. We now let the regular live
  2140. code do it's job, so we only need to check some extra invariants
  2141. that reg-stack expects. Primary among these being that all registers
  2142. are initialized before use.
  2143. The function returns true when code was emitted to CFG edges and
  2144. commit_edge_insertions needs to be called. */
  2145. static int
  2146. convert_regs_entry (void)
  2147. {
  2148. int inserted = 0;
  2149. edge e;
  2150. edge_iterator ei;
  2151. /* Load something into each stack register live at function entry.
  2152. Such live registers can be caused by uninitialized variables or
  2153. functions not returning values on all paths. In order to keep
  2154. the push/pop code happy, and to not scrog the register stack, we
  2155. must put something in these registers. Use a QNaN.
  2156. Note that we are inserting converted code here. This code is
  2157. never seen by the convert_regs pass. */
  2158. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
  2159. {
  2160. basic_block block = e->dest;
  2161. block_info bi = BLOCK_INFO (block);
  2162. int reg, top = -1;
  2163. for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
  2164. if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
  2165. {
  2166. rtx init;
  2167. bi->stack_in.reg[++top] = reg;
  2168. init = gen_rtx_SET (VOIDmode,
  2169. FP_MODE_REG (FIRST_STACK_REG, SFmode),
  2170. not_a_num);
  2171. insert_insn_on_edge (init, e);
  2172. inserted = 1;
  2173. }
  2174. bi->stack_in.top = top;
  2175. }
  2176. return inserted;
  2177. }
  2178. /* Construct the desired stack for function exit. This will either
  2179. be `empty', or the function return value at top-of-stack. */
  2180. static void
  2181. convert_regs_exit (void)
  2182. {
  2183. int value_reg_low, value_reg_high;
  2184. stack_ptr output_stack;
  2185. rtx retvalue;
  2186. retvalue = stack_result (current_function_decl);
  2187. value_reg_low = value_reg_high = -1;
  2188. if (retvalue)
  2189. {
  2190. value_reg_low = REGNO (retvalue);
  2191. value_reg_high = END_HARD_REGNO (retvalue) - 1;
  2192. }
  2193. output_stack = &BLOCK_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->stack_in;
  2194. if (value_reg_low == -1)
  2195. output_stack->top = -1;
  2196. else
  2197. {
  2198. int reg;
  2199. output_stack->top = value_reg_high - value_reg_low;
  2200. for (reg = value_reg_low; reg <= value_reg_high; ++reg)
  2201. {
  2202. output_stack->reg[value_reg_high - reg] = reg;
  2203. SET_HARD_REG_BIT (output_stack->reg_set, reg);
  2204. }
  2205. }
  2206. }
  2207. /* Copy the stack info from the end of edge E's source block to the
  2208. start of E's destination block. */
  2209. static void
  2210. propagate_stack (edge e)
  2211. {
  2212. stack_ptr src_stack = &BLOCK_INFO (e->src)->stack_out;
  2213. stack_ptr dest_stack = &BLOCK_INFO (e->dest)->stack_in;
  2214. int reg;
  2215. /* Preserve the order of the original stack, but check whether
  2216. any pops are needed. */
  2217. dest_stack->top = -1;
  2218. for (reg = 0; reg <= src_stack->top; ++reg)
  2219. if (TEST_HARD_REG_BIT (dest_stack->reg_set, src_stack->reg[reg]))
  2220. dest_stack->reg[++dest_stack->top] = src_stack->reg[reg];
  2221. /* Push in any partially dead values. */
  2222. for (reg = FIRST_STACK_REG; reg < LAST_STACK_REG + 1; reg++)
  2223. if (TEST_HARD_REG_BIT (dest_stack->reg_set, reg)
  2224. && !TEST_HARD_REG_BIT (src_stack->reg_set, reg))
  2225. dest_stack->reg[++dest_stack->top] = reg;
  2226. }
  2227. /* Adjust the stack of edge E's source block on exit to match the stack
  2228. of it's target block upon input. The stack layouts of both blocks
  2229. should have been defined by now. */
  2230. static bool
  2231. compensate_edge (edge e)
  2232. {
  2233. basic_block source = e->src, target = e->dest;
  2234. stack_ptr target_stack = &BLOCK_INFO (target)->stack_in;
  2235. stack_ptr source_stack = &BLOCK_INFO (source)->stack_out;
  2236. struct stack_def regstack;
  2237. int reg;
  2238. if (dump_file)
  2239. fprintf (dump_file, "Edge %d->%d: ", source->index, target->index);
  2240. gcc_assert (target_stack->top != -2);
  2241. /* Check whether stacks are identical. */
  2242. if (target_stack->top == source_stack->top)
  2243. {
  2244. for (reg = target_stack->top; reg >= 0; --reg)
  2245. if (target_stack->reg[reg] != source_stack->reg[reg])
  2246. break;
  2247. if (reg == -1)
  2248. {
  2249. if (dump_file)
  2250. fprintf (dump_file, "no changes needed\n");
  2251. return false;
  2252. }
  2253. }
  2254. if (dump_file)
  2255. {
  2256. fprintf (dump_file, "correcting stack to ");
  2257. print_stack (dump_file, target_stack);
  2258. }
  2259. /* Abnormal calls may appear to have values live in st(0), but the
  2260. abnormal return path will not have actually loaded the values. */
  2261. if (e->flags & EDGE_ABNORMAL_CALL)
  2262. {
  2263. /* Assert that the lifetimes are as we expect -- one value
  2264. live at st(0) on the end of the source block, and no
  2265. values live at the beginning of the destination block.
  2266. For complex return values, we may have st(1) live as well. */
  2267. gcc_assert (source_stack->top == 0 || source_stack->top == 1);
  2268. gcc_assert (target_stack->top == -1);
  2269. return false;
  2270. }
  2271. /* Handle non-call EH edges specially. The normal return path have
  2272. values in registers. These will be popped en masse by the unwind
  2273. library. */
  2274. if (e->flags & EDGE_EH)
  2275. {
  2276. gcc_assert (target_stack->top == -1);
  2277. return false;
  2278. }
  2279. /* We don't support abnormal edges. Global takes care to
  2280. avoid any live register across them, so we should never
  2281. have to insert instructions on such edges. */
  2282. gcc_assert (! (e->flags & EDGE_ABNORMAL));
  2283. /* Make a copy of source_stack as change_stack is destructive. */
  2284. regstack = *source_stack;
  2285. /* It is better to output directly to the end of the block
  2286. instead of to the edge, because emit_swap can do minimal
  2287. insn scheduling. We can do this when there is only one
  2288. edge out, and it is not abnormal. */
  2289. if (EDGE_COUNT (source->succs) == 1)
  2290. {
  2291. current_block = source;
  2292. change_stack (BB_END (source), &regstack, target_stack,
  2293. (JUMP_P (BB_END (source)) ? EMIT_BEFORE : EMIT_AFTER));
  2294. }
  2295. else
  2296. {
  2297. rtx_insn *seq;
  2298. rtx_note *after;
  2299. current_block = NULL;
  2300. start_sequence ();
  2301. /* ??? change_stack needs some point to emit insns after. */
  2302. after = emit_note (NOTE_INSN_DELETED);
  2303. change_stack (after, &regstack, target_stack, EMIT_BEFORE);
  2304. seq = get_insns ();
  2305. end_sequence ();
  2306. insert_insn_on_edge (seq, e);
  2307. return true;
  2308. }
  2309. return false;
  2310. }
  2311. /* Traverse all non-entry edges in the CFG, and emit the necessary
  2312. edge compensation code to change the stack from stack_out of the
  2313. source block to the stack_in of the destination block. */
  2314. static bool
  2315. compensate_edges (void)
  2316. {
  2317. bool inserted = false;
  2318. basic_block bb;
  2319. starting_stack_p = false;
  2320. FOR_EACH_BB_FN (bb, cfun)
  2321. if (bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
  2322. {
  2323. edge e;
  2324. edge_iterator ei;
  2325. FOR_EACH_EDGE (e, ei, bb->succs)
  2326. inserted |= compensate_edge (e);
  2327. }
  2328. return inserted;
  2329. }
  2330. /* Select the better of two edges E1 and E2 to use to determine the
  2331. stack layout for their shared destination basic block. This is
  2332. typically the more frequently executed. The edge E1 may be NULL
  2333. (in which case E2 is returned), but E2 is always non-NULL. */
  2334. static edge
  2335. better_edge (edge e1, edge e2)
  2336. {
  2337. if (!e1)
  2338. return e2;
  2339. if (EDGE_FREQUENCY (e1) > EDGE_FREQUENCY (e2))
  2340. return e1;
  2341. if (EDGE_FREQUENCY (e1) < EDGE_FREQUENCY (e2))
  2342. return e2;
  2343. if (e1->count > e2->count)
  2344. return e1;
  2345. if (e1->count < e2->count)
  2346. return e2;
  2347. /* Prefer critical edges to minimize inserting compensation code on
  2348. critical edges. */
  2349. if (EDGE_CRITICAL_P (e1) != EDGE_CRITICAL_P (e2))
  2350. return EDGE_CRITICAL_P (e1) ? e1 : e2;
  2351. /* Avoid non-deterministic behavior. */
  2352. return (e1->src->index < e2->src->index) ? e1 : e2;
  2353. }
  2354. /* Convert stack register references in one block. Return true if the CFG
  2355. has been modified in the process. */
  2356. static bool
  2357. convert_regs_1 (basic_block block)
  2358. {
  2359. struct stack_def regstack;
  2360. block_info bi = BLOCK_INFO (block);
  2361. int reg;
  2362. rtx_insn *insn, *next;
  2363. bool control_flow_insn_deleted = false;
  2364. bool cfg_altered = false;
  2365. int debug_insns_with_starting_stack = 0;
  2366. any_malformed_asm = false;
  2367. /* Choose an initial stack layout, if one hasn't already been chosen. */
  2368. if (bi->stack_in.top == -2)
  2369. {
  2370. edge e, beste = NULL;
  2371. edge_iterator ei;
  2372. /* Select the best incoming edge (typically the most frequent) to
  2373. use as a template for this basic block. */
  2374. FOR_EACH_EDGE (e, ei, block->preds)
  2375. if (BLOCK_INFO (e->src)->done)
  2376. beste = better_edge (beste, e);
  2377. if (beste)
  2378. propagate_stack (beste);
  2379. else
  2380. {
  2381. /* No predecessors. Create an arbitrary input stack. */
  2382. bi->stack_in.top = -1;
  2383. for (reg = LAST_STACK_REG; reg >= FIRST_STACK_REG; --reg)
  2384. if (TEST_HARD_REG_BIT (bi->stack_in.reg_set, reg))
  2385. bi->stack_in.reg[++bi->stack_in.top] = reg;
  2386. }
  2387. }
  2388. if (dump_file)
  2389. {
  2390. fprintf (dump_file, "\nBasic block %d\nInput stack: ", block->index);
  2391. print_stack (dump_file, &bi->stack_in);
  2392. }
  2393. /* Process all insns in this block. Keep track of NEXT so that we
  2394. don't process insns emitted while substituting in INSN. */
  2395. current_block = block;
  2396. next = BB_HEAD (block);
  2397. regstack = bi->stack_in;
  2398. starting_stack_p = true;
  2399. do
  2400. {
  2401. insn = next;
  2402. next = NEXT_INSN (insn);
  2403. /* Ensure we have not missed a block boundary. */
  2404. gcc_assert (next);
  2405. if (insn == BB_END (block))
  2406. next = NULL;
  2407. /* Don't bother processing unless there is a stack reg
  2408. mentioned or if it's a CALL_INSN. */
  2409. if (DEBUG_INSN_P (insn))
  2410. {
  2411. if (starting_stack_p)
  2412. debug_insns_with_starting_stack++;
  2413. else
  2414. {
  2415. subst_all_stack_regs_in_debug_insn (insn, &regstack);
  2416. /* Nothing must ever die at a debug insn. If something
  2417. is referenced in it that becomes dead, it should have
  2418. died before and the reference in the debug insn
  2419. should have been removed so as to avoid changing code
  2420. generation. */
  2421. gcc_assert (!find_reg_note (insn, REG_DEAD, NULL));
  2422. }
  2423. }
  2424. else if (stack_regs_mentioned (insn)
  2425. || CALL_P (insn))
  2426. {
  2427. if (dump_file)
  2428. {
  2429. fprintf (dump_file, " insn %d input stack: ",
  2430. INSN_UID (insn));
  2431. print_stack (dump_file, &regstack);
  2432. }
  2433. control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
  2434. starting_stack_p = false;
  2435. }
  2436. }
  2437. while (next);
  2438. if (debug_insns_with_starting_stack)
  2439. {
  2440. /* Since it's the first non-debug instruction that determines
  2441. the stack requirements of the current basic block, we refrain
  2442. from updating debug insns before it in the loop above, and
  2443. fix them up here. */
  2444. for (insn = BB_HEAD (block); debug_insns_with_starting_stack;
  2445. insn = NEXT_INSN (insn))
  2446. {
  2447. if (!DEBUG_INSN_P (insn))
  2448. continue;
  2449. debug_insns_with_starting_stack--;
  2450. subst_all_stack_regs_in_debug_insn (insn, &bi->stack_in);
  2451. }
  2452. }
  2453. if (dump_file)
  2454. {
  2455. fprintf (dump_file, "Expected live registers [");
  2456. for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
  2457. if (TEST_HARD_REG_BIT (bi->out_reg_set, reg))
  2458. fprintf (dump_file, " %d", reg);
  2459. fprintf (dump_file, " ]\nOutput stack: ");
  2460. print_stack (dump_file, &regstack);
  2461. }
  2462. insn = BB_END (block);
  2463. if (JUMP_P (insn))
  2464. insn = PREV_INSN (insn);
  2465. /* If the function is declared to return a value, but it returns one
  2466. in only some cases, some registers might come live here. Emit
  2467. necessary moves for them. */
  2468. for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; ++reg)
  2469. {
  2470. if (TEST_HARD_REG_BIT (bi->out_reg_set, reg)
  2471. && ! TEST_HARD_REG_BIT (regstack.reg_set, reg))
  2472. {
  2473. rtx set;
  2474. if (dump_file)
  2475. fprintf (dump_file, "Emitting insn initializing reg %d\n", reg);
  2476. set = gen_rtx_SET (VOIDmode, FP_MODE_REG (reg, SFmode), not_a_num);
  2477. insn = emit_insn_after (set, insn);
  2478. control_flow_insn_deleted |= subst_stack_regs (insn, &regstack);
  2479. }
  2480. }
  2481. /* Amongst the insns possibly deleted during the substitution process above,
  2482. might have been the only trapping insn in the block. We purge the now
  2483. possibly dead EH edges here to avoid an ICE from fixup_abnormal_edges,
  2484. called at the end of convert_regs. The order in which we process the
  2485. blocks ensures that we never delete an already processed edge.
  2486. Note that, at this point, the CFG may have been damaged by the emission
  2487. of instructions after an abnormal call, which moves the basic block end
  2488. (and is the reason why we call fixup_abnormal_edges later). So we must
  2489. be sure that the trapping insn has been deleted before trying to purge
  2490. dead edges, otherwise we risk purging valid edges.
  2491. ??? We are normally supposed not to delete trapping insns, so we pretend
  2492. that the insns deleted above don't actually trap. It would have been
  2493. better to detect this earlier and avoid creating the EH edge in the first
  2494. place, still, but we don't have enough information at that time. */
  2495. if (control_flow_insn_deleted)
  2496. cfg_altered |= purge_dead_edges (block);
  2497. /* Something failed if the stack lives don't match. If we had malformed
  2498. asms, we zapped the instruction itself, but that didn't produce the
  2499. same pattern of register kills as before. */
  2500. gcc_assert (hard_reg_set_equal_p (regstack.reg_set, bi->out_reg_set)
  2501. || any_malformed_asm);
  2502. bi->stack_out = regstack;
  2503. bi->done = true;
  2504. return cfg_altered;
  2505. }
  2506. /* Convert registers in all blocks reachable from BLOCK. Return true if the
  2507. CFG has been modified in the process. */
  2508. static bool
  2509. convert_regs_2 (basic_block block)
  2510. {
  2511. basic_block *stack, *sp;
  2512. bool cfg_altered = false;
  2513. /* We process the blocks in a top-down manner, in a way such that one block
  2514. is only processed after all its predecessors. The number of predecessors
  2515. of every block has already been computed. */
  2516. stack = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
  2517. sp = stack;
  2518. *sp++ = block;
  2519. do
  2520. {
  2521. edge e;
  2522. edge_iterator ei;
  2523. block = *--sp;
  2524. /* Processing BLOCK is achieved by convert_regs_1, which may purge
  2525. some dead EH outgoing edge after the deletion of the trapping
  2526. insn inside the block. Since the number of predecessors of
  2527. BLOCK's successors was computed based on the initial edge set,
  2528. we check the necessity to process some of these successors
  2529. before such an edge deletion may happen. However, there is
  2530. a pitfall: if BLOCK is the only predecessor of a successor and
  2531. the edge between them happens to be deleted, the successor
  2532. becomes unreachable and should not be processed. The problem
  2533. is that there is no way to preventively detect this case so we
  2534. stack the successor in all cases and hand over the task of
  2535. fixing up the discrepancy to convert_regs_1. */
  2536. FOR_EACH_EDGE (e, ei, block->succs)
  2537. if (! (e->flags & EDGE_DFS_BACK))
  2538. {
  2539. BLOCK_INFO (e->dest)->predecessors--;
  2540. if (!BLOCK_INFO (e->dest)->predecessors)
  2541. *sp++ = e->dest;
  2542. }
  2543. cfg_altered |= convert_regs_1 (block);
  2544. }
  2545. while (sp != stack);
  2546. free (stack);
  2547. return cfg_altered;
  2548. }
  2549. /* Traverse all basic blocks in a function, converting the register
  2550. references in each insn from the "flat" register file that gcc uses,
  2551. to the stack-like registers the 387 uses. */
  2552. static void
  2553. convert_regs (void)
  2554. {
  2555. bool cfg_altered = false;
  2556. int inserted;
  2557. basic_block b;
  2558. edge e;
  2559. edge_iterator ei;
  2560. /* Initialize uninitialized registers on function entry. */
  2561. inserted = convert_regs_entry ();
  2562. /* Construct the desired stack for function exit. */
  2563. convert_regs_exit ();
  2564. BLOCK_INFO (EXIT_BLOCK_PTR_FOR_FN (cfun))->done = 1;
  2565. /* ??? Future: process inner loops first, and give them arbitrary
  2566. initial stacks which emit_swap_insn can modify. This ought to
  2567. prevent double fxch that often appears at the head of a loop. */
  2568. /* Process all blocks reachable from all entry points. */
  2569. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs)
  2570. cfg_altered |= convert_regs_2 (e->dest);
  2571. /* ??? Process all unreachable blocks. Though there's no excuse
  2572. for keeping these even when not optimizing. */
  2573. FOR_EACH_BB_FN (b, cfun)
  2574. {
  2575. block_info bi = BLOCK_INFO (b);
  2576. if (! bi->done)
  2577. cfg_altered |= convert_regs_2 (b);
  2578. }
  2579. /* We must fix up abnormal edges before inserting compensation code
  2580. because both mechanisms insert insns on edges. */
  2581. inserted |= fixup_abnormal_edges ();
  2582. inserted |= compensate_edges ();
  2583. clear_aux_for_blocks ();
  2584. if (inserted)
  2585. commit_edge_insertions ();
  2586. if (cfg_altered)
  2587. cleanup_cfg (0);
  2588. if (dump_file)
  2589. fputc ('\n', dump_file);
  2590. }
  2591. /* Convert register usage from "flat" register file usage to a "stack
  2592. register file. FILE is the dump file, if used.
  2593. Construct a CFG and run life analysis. Then convert each insn one
  2594. by one. Run a last cleanup_cfg pass, if optimizing, to eliminate
  2595. code duplication created when the converter inserts pop insns on
  2596. the edges. */
  2597. static bool
  2598. reg_to_stack (void)
  2599. {
  2600. basic_block bb;
  2601. int i;
  2602. int max_uid;
  2603. /* Clean up previous run. */
  2604. stack_regs_mentioned_data.release ();
  2605. /* See if there is something to do. Flow analysis is quite
  2606. expensive so we might save some compilation time. */
  2607. for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
  2608. if (df_regs_ever_live_p (i))
  2609. break;
  2610. if (i > LAST_STACK_REG)
  2611. return false;
  2612. df_note_add_problem ();
  2613. df_analyze ();
  2614. mark_dfs_back_edges ();
  2615. /* Set up block info for each basic block. */
  2616. alloc_aux_for_blocks (sizeof (struct block_info_def));
  2617. FOR_EACH_BB_FN (bb, cfun)
  2618. {
  2619. block_info bi = BLOCK_INFO (bb);
  2620. edge_iterator ei;
  2621. edge e;
  2622. int reg;
  2623. FOR_EACH_EDGE (e, ei, bb->preds)
  2624. if (!(e->flags & EDGE_DFS_BACK)
  2625. && e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun))
  2626. bi->predecessors++;
  2627. /* Set current register status at last instruction `uninitialized'. */
  2628. bi->stack_in.top = -2;
  2629. /* Copy live_at_end and live_at_start into temporaries. */
  2630. for (reg = FIRST_STACK_REG; reg <= LAST_STACK_REG; reg++)
  2631. {
  2632. if (REGNO_REG_SET_P (DF_LR_OUT (bb), reg))
  2633. SET_HARD_REG_BIT (bi->out_reg_set, reg);
  2634. if (REGNO_REG_SET_P (DF_LR_IN (bb), reg))
  2635. SET_HARD_REG_BIT (bi->stack_in.reg_set, reg);
  2636. }
  2637. }
  2638. /* Create the replacement registers up front. */
  2639. for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
  2640. {
  2641. machine_mode mode;
  2642. for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT);
  2643. mode != VOIDmode;
  2644. mode = GET_MODE_WIDER_MODE (mode))
  2645. FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
  2646. for (mode = GET_CLASS_NARROWEST_MODE (MODE_COMPLEX_FLOAT);
  2647. mode != VOIDmode;
  2648. mode = GET_MODE_WIDER_MODE (mode))
  2649. FP_MODE_REG (i, mode) = gen_rtx_REG (mode, i);
  2650. }
  2651. ix86_flags_rtx = gen_rtx_REG (CCmode, FLAGS_REG);
  2652. /* A QNaN for initializing uninitialized variables.
  2653. ??? We can't load from constant memory in PIC mode, because
  2654. we're inserting these instructions before the prologue and
  2655. the PIC register hasn't been set up. In that case, fall back
  2656. on zero, which we can get from `fldz'. */
  2657. if ((flag_pic && !TARGET_64BIT)
  2658. || ix86_cmodel == CM_LARGE || ix86_cmodel == CM_LARGE_PIC)
  2659. not_a_num = CONST0_RTX (SFmode);
  2660. else
  2661. {
  2662. REAL_VALUE_TYPE r;
  2663. real_nan (&r, "", 1, SFmode);
  2664. not_a_num = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
  2665. not_a_num = force_const_mem (SFmode, not_a_num);
  2666. }
  2667. /* Allocate a cache for stack_regs_mentioned. */
  2668. max_uid = get_max_uid ();
  2669. stack_regs_mentioned_data.create (max_uid + 1);
  2670. memset (stack_regs_mentioned_data.address (),
  2671. 0, sizeof (char) * (max_uid + 1));
  2672. convert_regs ();
  2673. free_aux_for_blocks ();
  2674. return true;
  2675. }
  2676. #endif /* STACK_REGS */
  2677. namespace {
  2678. const pass_data pass_data_stack_regs =
  2679. {
  2680. RTL_PASS, /* type */
  2681. "*stack_regs", /* name */
  2682. OPTGROUP_NONE, /* optinfo_flags */
  2683. TV_REG_STACK, /* tv_id */
  2684. 0, /* properties_required */
  2685. 0, /* properties_provided */
  2686. 0, /* properties_destroyed */
  2687. 0, /* todo_flags_start */
  2688. 0, /* todo_flags_finish */
  2689. };
  2690. class pass_stack_regs : public rtl_opt_pass
  2691. {
  2692. public:
  2693. pass_stack_regs (gcc::context *ctxt)
  2694. : rtl_opt_pass (pass_data_stack_regs, ctxt)
  2695. {}
  2696. /* opt_pass methods: */
  2697. virtual bool gate (function *)
  2698. {
  2699. #ifdef STACK_REGS
  2700. return true;
  2701. #else
  2702. return false;
  2703. #endif
  2704. }
  2705. }; // class pass_stack_regs
  2706. } // anon namespace
  2707. rtl_opt_pass *
  2708. make_pass_stack_regs (gcc::context *ctxt)
  2709. {
  2710. return new pass_stack_regs (ctxt);
  2711. }
  2712. /* Convert register usage from flat register file usage to a stack
  2713. register file. */
  2714. static unsigned int
  2715. rest_of_handle_stack_regs (void)
  2716. {
  2717. #ifdef STACK_REGS
  2718. reg_to_stack ();
  2719. regstack_completed = 1;
  2720. #endif
  2721. return 0;
  2722. }
  2723. namespace {
  2724. const pass_data pass_data_stack_regs_run =
  2725. {
  2726. RTL_PASS, /* type */
  2727. "stack", /* name */
  2728. OPTGROUP_NONE, /* optinfo_flags */
  2729. TV_REG_STACK, /* tv_id */
  2730. 0, /* properties_required */
  2731. 0, /* properties_provided */
  2732. 0, /* properties_destroyed */
  2733. 0, /* todo_flags_start */
  2734. TODO_df_finish, /* todo_flags_finish */
  2735. };
  2736. class pass_stack_regs_run : public rtl_opt_pass
  2737. {
  2738. public:
  2739. pass_stack_regs_run (gcc::context *ctxt)
  2740. : rtl_opt_pass (pass_data_stack_regs_run, ctxt)
  2741. {}
  2742. /* opt_pass methods: */
  2743. virtual unsigned int execute (function *)
  2744. {
  2745. return rest_of_handle_stack_regs ();
  2746. }
  2747. }; // class pass_stack_regs_run
  2748. } // anon namespace
  2749. rtl_opt_pass *
  2750. make_pass_stack_regs_run (gcc::context *ctxt)
  2751. {
  2752. return new pass_stack_regs_run (ctxt);
  2753. }