lra.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485
  1. /* LRA (local register allocator) driver and LRA utilities.
  2. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. Contributed by Vladimir Makarov <vmakarov@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* The Local Register Allocator (LRA) is a replacement of former
  17. reload pass. It is focused to simplify code solving the reload
  18. pass tasks, to make the code maintenance easier, and to implement new
  19. perspective optimizations.
  20. The major LRA design solutions are:
  21. o division small manageable, separated sub-tasks
  22. o reflection of all transformations and decisions in RTL as more
  23. as possible
  24. o insn constraints as a primary source of the info (minimizing
  25. number of target-depended macros/hooks)
  26. In brief LRA works by iterative insn process with the final goal is
  27. to satisfy all insn and address constraints:
  28. o New reload insns (in brief reloads) and reload pseudos might be
  29. generated;
  30. o Some pseudos might be spilled to assign hard registers to
  31. new reload pseudos;
  32. o Recalculating spilled pseudo values (rematerialization);
  33. o Changing spilled pseudos to stack memory or their equivalences;
  34. o Allocation stack memory changes the address displacement and
  35. new iteration is needed.
  36. Here is block diagram of LRA passes:
  37. ------------------------
  38. --------------- | Undo inheritance for | ---------------
  39. | Memory-memory | | spilled pseudos, | | New (and old) |
  40. | move coalesce |<---| splits for pseudos got |<-- | pseudos |
  41. --------------- | the same hard regs, | | assignment |
  42. Start | | and optional reloads | ---------------
  43. | | ------------------------ ^
  44. V | ---------------- |
  45. ----------- V | Update virtual | |
  46. | Remove |----> ------------>| register | |
  47. | scratches | ^ | displacements | |
  48. ----------- | ---------------- |
  49. | | |
  50. | V New |
  51. | ------------ pseudos -------------------
  52. | |Constraints:| or insns | Inheritance/split |
  53. | | RTL |--------->| transformations |
  54. | | transfor- | | in EBB scope |
  55. | substi- | mations | -------------------
  56. | tutions ------------
  57. | | No change
  58. ---------------- V
  59. | Spilled pseudo | -------------------
  60. | to memory |<----| Rematerialization |
  61. | substitution | -------------------
  62. ----------------
  63. | No susbtitions
  64. V
  65. -------------------------
  66. | Hard regs substitution, |
  67. | devirtalization, and |------> Finish
  68. | restoring scratches got |
  69. | memory |
  70. -------------------------
  71. To speed up the process:
  72. o We process only insns affected by changes on previous
  73. iterations;
  74. o We don't use DFA-infrastructure because it results in much slower
  75. compiler speed than a special IR described below does;
  76. o We use a special insn representation for quick access to insn
  77. info which is always *synchronized* with the current RTL;
  78. o Insn IR is minimized by memory. It is divided on three parts:
  79. o one specific for each insn in RTL (only operand locations);
  80. o one common for all insns in RTL with the same insn code
  81. (different operand attributes from machine descriptions);
  82. o one oriented for maintenance of live info (list of pseudos).
  83. o Pseudo data:
  84. o all insns where the pseudo is referenced;
  85. o live info (conflicting hard regs, live ranges, # of
  86. references etc);
  87. o data used for assigning (preferred hard regs, costs etc).
  88. This file contains LRA driver, LRA utility functions and data, and
  89. code for dealing with scratches. */
  90. #include "config.h"
  91. #include "system.h"
  92. #include "coretypes.h"
  93. #include "tm.h"
  94. #include "hard-reg-set.h"
  95. #include "rtl.h"
  96. #include "tm_p.h"
  97. #include "regs.h"
  98. #include "insn-config.h"
  99. #include "insn-codes.h"
  100. #include "recog.h"
  101. #include "output.h"
  102. #include "addresses.h"
  103. #include "flags.h"
  104. #include "hashtab.h"
  105. #include "hash-set.h"
  106. #include "vec.h"
  107. #include "machmode.h"
  108. #include "input.h"
  109. #include "function.h"
  110. #include "symtab.h"
  111. #include "wide-int.h"
  112. #include "inchash.h"
  113. #include "tree.h"
  114. #include "optabs.h"
  115. #include "statistics.h"
  116. #include "double-int.h"
  117. #include "real.h"
  118. #include "fixed-value.h"
  119. #include "alias.h"
  120. #include "expmed.h"
  121. #include "dojump.h"
  122. #include "explow.h"
  123. #include "calls.h"
  124. #include "emit-rtl.h"
  125. #include "varasm.h"
  126. #include "stmt.h"
  127. #include "expr.h"
  128. #include "predict.h"
  129. #include "dominance.h"
  130. #include "cfg.h"
  131. #include "cfgrtl.h"
  132. #include "cfgbuild.h"
  133. #include "basic-block.h"
  134. #include "except.h"
  135. #include "tree-pass.h"
  136. #include "timevar.h"
  137. #include "target.h"
  138. #include "ira.h"
  139. #include "lra-int.h"
  140. #include "df.h"
  141. /* Dump bitmap SET with TITLE and BB INDEX. */
  142. void
  143. lra_dump_bitmap_with_title (const char *title, bitmap set, int index)
  144. {
  145. unsigned int i;
  146. int count;
  147. bitmap_iterator bi;
  148. static const int max_nums_on_line = 10;
  149. if (bitmap_empty_p (set))
  150. return;
  151. fprintf (lra_dump_file, " %s %d:", title, index);
  152. fprintf (lra_dump_file, "\n");
  153. count = max_nums_on_line + 1;
  154. EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
  155. {
  156. if (count > max_nums_on_line)
  157. {
  158. fprintf (lra_dump_file, "\n ");
  159. count = 0;
  160. }
  161. fprintf (lra_dump_file, " %4u", i);
  162. count++;
  163. }
  164. fprintf (lra_dump_file, "\n");
  165. }
  166. /* Hard registers currently not available for allocation. It can
  167. changed after some hard registers become not eliminable. */
  168. HARD_REG_SET lra_no_alloc_regs;
  169. static int get_new_reg_value (void);
  170. static void expand_reg_info (void);
  171. static void invalidate_insn_recog_data (int);
  172. static int get_insn_freq (rtx_insn *);
  173. static void invalidate_insn_data_regno_info (lra_insn_recog_data_t,
  174. rtx_insn *, int);
  175. /* Expand all regno related info needed for LRA. */
  176. static void
  177. expand_reg_data (int old)
  178. {
  179. resize_reg_info ();
  180. expand_reg_info ();
  181. ira_expand_reg_equiv ();
  182. for (int i = (int) max_reg_num () - 1; i >= old; i--)
  183. lra_change_class (i, ALL_REGS, " Set", true);
  184. }
  185. /* Create and return a new reg of ORIGINAL mode. If ORIGINAL is NULL
  186. or of VOIDmode, use MD_MODE for the new reg. Initialize its
  187. register class to RCLASS. Print message about assigning class
  188. RCLASS containing new register name TITLE unless it is NULL. Use
  189. attributes of ORIGINAL if it is a register. The created register
  190. will have unique held value. */
  191. rtx
  192. lra_create_new_reg_with_unique_value (machine_mode md_mode, rtx original,
  193. enum reg_class rclass, const char *title)
  194. {
  195. machine_mode mode;
  196. rtx new_reg;
  197. if (original == NULL_RTX || (mode = GET_MODE (original)) == VOIDmode)
  198. mode = md_mode;
  199. lra_assert (mode != VOIDmode);
  200. new_reg = gen_reg_rtx (mode);
  201. if (original == NULL_RTX || ! REG_P (original))
  202. {
  203. if (lra_dump_file != NULL)
  204. fprintf (lra_dump_file, " Creating newreg=%i", REGNO (new_reg));
  205. }
  206. else
  207. {
  208. if (ORIGINAL_REGNO (original) >= FIRST_PSEUDO_REGISTER)
  209. ORIGINAL_REGNO (new_reg) = ORIGINAL_REGNO (original);
  210. REG_USERVAR_P (new_reg) = REG_USERVAR_P (original);
  211. REG_POINTER (new_reg) = REG_POINTER (original);
  212. REG_ATTRS (new_reg) = REG_ATTRS (original);
  213. if (lra_dump_file != NULL)
  214. fprintf (lra_dump_file, " Creating newreg=%i from oldreg=%i",
  215. REGNO (new_reg), REGNO (original));
  216. }
  217. if (lra_dump_file != NULL)
  218. {
  219. if (title != NULL)
  220. fprintf (lra_dump_file, ", assigning class %s to%s%s r%d",
  221. reg_class_names[rclass], *title == '\0' ? "" : " ",
  222. title, REGNO (new_reg));
  223. fprintf (lra_dump_file, "\n");
  224. }
  225. expand_reg_data (max_reg_num ());
  226. setup_reg_classes (REGNO (new_reg), rclass, NO_REGS, rclass);
  227. return new_reg;
  228. }
  229. /* Analogous to the previous function but also inherits value of
  230. ORIGINAL. */
  231. rtx
  232. lra_create_new_reg (machine_mode md_mode, rtx original,
  233. enum reg_class rclass, const char *title)
  234. {
  235. rtx new_reg;
  236. new_reg
  237. = lra_create_new_reg_with_unique_value (md_mode, original, rclass, title);
  238. if (original != NULL_RTX && REG_P (original))
  239. lra_assign_reg_val (REGNO (original), REGNO (new_reg));
  240. return new_reg;
  241. }
  242. /* Set up for REGNO unique hold value. */
  243. void
  244. lra_set_regno_unique_value (int regno)
  245. {
  246. lra_reg_info[regno].val = get_new_reg_value ();
  247. }
  248. /* Invalidate INSN related info used by LRA. The info should never be
  249. used after that. */
  250. void
  251. lra_invalidate_insn_data (rtx_insn *insn)
  252. {
  253. lra_invalidate_insn_regno_info (insn);
  254. invalidate_insn_recog_data (INSN_UID (insn));
  255. }
  256. /* Mark INSN deleted and invalidate the insn related info used by
  257. LRA. */
  258. void
  259. lra_set_insn_deleted (rtx_insn *insn)
  260. {
  261. lra_invalidate_insn_data (insn);
  262. SET_INSN_DELETED (insn);
  263. }
  264. /* Delete an unneeded INSN and any previous insns who sole purpose is
  265. loading data that is dead in INSN. */
  266. void
  267. lra_delete_dead_insn (rtx_insn *insn)
  268. {
  269. rtx_insn *prev = prev_real_insn (insn);
  270. rtx prev_dest;
  271. /* If the previous insn sets a register that dies in our insn,
  272. delete it too. */
  273. if (prev && GET_CODE (PATTERN (prev)) == SET
  274. && (prev_dest = SET_DEST (PATTERN (prev)), REG_P (prev_dest))
  275. && reg_mentioned_p (prev_dest, PATTERN (insn))
  276. && find_regno_note (insn, REG_DEAD, REGNO (prev_dest))
  277. && ! side_effects_p (SET_SRC (PATTERN (prev))))
  278. lra_delete_dead_insn (prev);
  279. lra_set_insn_deleted (insn);
  280. }
  281. /* Emit insn x = y + z. Return NULL if we failed to do it.
  282. Otherwise, return the insn. We don't use gen_add3_insn as it might
  283. clobber CC. */
  284. static rtx
  285. emit_add3_insn (rtx x, rtx y, rtx z)
  286. {
  287. rtx_insn *last;
  288. last = get_last_insn ();
  289. if (have_addptr3_insn (x, y, z))
  290. {
  291. rtx insn = gen_addptr3_insn (x, y, z);
  292. /* If the target provides an "addptr" pattern it hopefully does
  293. for a reason. So falling back to the normal add would be
  294. a bug. */
  295. lra_assert (insn != NULL_RTX);
  296. emit_insn (insn);
  297. return insn;
  298. }
  299. rtx_insn *insn = emit_insn (gen_rtx_SET (VOIDmode, x,
  300. gen_rtx_PLUS (GET_MODE (y), y, z)));
  301. if (recog_memoized (insn) < 0)
  302. {
  303. delete_insns_since (last);
  304. insn = NULL;
  305. }
  306. return insn;
  307. }
  308. /* Emit insn x = x + y. Return the insn. We use gen_add2_insn as the
  309. last resort. */
  310. static rtx
  311. emit_add2_insn (rtx x, rtx y)
  312. {
  313. rtx insn;
  314. insn = emit_add3_insn (x, x, y);
  315. if (insn == NULL_RTX)
  316. {
  317. insn = gen_add2_insn (x, y);
  318. if (insn != NULL_RTX)
  319. emit_insn (insn);
  320. }
  321. return insn;
  322. }
  323. /* Target checks operands through operand predicates to recognize an
  324. insn. We should have a special precaution to generate add insns
  325. which are frequent results of elimination.
  326. Emit insns for x = y + z. X can be used to store intermediate
  327. values and should be not in Y and Z when we use X to store an
  328. intermediate value. Y + Z should form [base] [+ index[ * scale]] [
  329. + disp] where base and index are registers, disp and scale are
  330. constants. Y should contain base if it is present, Z should
  331. contain disp if any. index[*scale] can be part of Y or Z. */
  332. void
  333. lra_emit_add (rtx x, rtx y, rtx z)
  334. {
  335. int old;
  336. rtx_insn *last;
  337. rtx a1, a2, base, index, disp, scale, index_scale;
  338. bool ok_p;
  339. rtx add3_insn = emit_add3_insn (x, y, z);
  340. old = max_reg_num ();
  341. if (add3_insn != NULL)
  342. ;
  343. else
  344. {
  345. disp = a2 = NULL_RTX;
  346. if (GET_CODE (y) == PLUS)
  347. {
  348. a1 = XEXP (y, 0);
  349. a2 = XEXP (y, 1);
  350. disp = z;
  351. }
  352. else
  353. {
  354. a1 = y;
  355. if (CONSTANT_P (z))
  356. disp = z;
  357. else
  358. a2 = z;
  359. }
  360. index_scale = scale = NULL_RTX;
  361. if (GET_CODE (a1) == MULT)
  362. {
  363. index_scale = a1;
  364. index = XEXP (a1, 0);
  365. scale = XEXP (a1, 1);
  366. base = a2;
  367. }
  368. else if (a2 != NULL_RTX && GET_CODE (a2) == MULT)
  369. {
  370. index_scale = a2;
  371. index = XEXP (a2, 0);
  372. scale = XEXP (a2, 1);
  373. base = a1;
  374. }
  375. else
  376. {
  377. base = a1;
  378. index = a2;
  379. }
  380. if (! (REG_P (base) || GET_CODE (base) == SUBREG)
  381. || (index != NULL_RTX
  382. && ! (REG_P (index) || GET_CODE (index) == SUBREG))
  383. || (disp != NULL_RTX && ! CONSTANT_P (disp))
  384. || (scale != NULL_RTX && ! CONSTANT_P (scale)))
  385. {
  386. /* Probably we have no 3 op add. Last chance is to use 2-op
  387. add insn. To succeed, don't move Z to X as an address
  388. segment always comes in Y. Otherwise, we might fail when
  389. adding the address segment to register. */
  390. lra_assert (x != y && x != z);
  391. emit_move_insn (x, y);
  392. rtx insn = emit_add2_insn (x, z);
  393. lra_assert (insn != NULL_RTX);
  394. }
  395. else
  396. {
  397. if (index_scale == NULL_RTX)
  398. index_scale = index;
  399. if (disp == NULL_RTX)
  400. {
  401. /* Generate x = index_scale; x = x + base. */
  402. lra_assert (index_scale != NULL_RTX && base != NULL_RTX);
  403. emit_move_insn (x, index_scale);
  404. rtx insn = emit_add2_insn (x, base);
  405. lra_assert (insn != NULL_RTX);
  406. }
  407. else if (scale == NULL_RTX)
  408. {
  409. /* Try x = base + disp. */
  410. lra_assert (base != NULL_RTX);
  411. last = get_last_insn ();
  412. rtx_insn *move_insn =
  413. emit_move_insn (x, gen_rtx_PLUS (GET_MODE (base), base, disp));
  414. if (recog_memoized (move_insn) < 0)
  415. {
  416. delete_insns_since (last);
  417. /* Generate x = disp; x = x + base. */
  418. emit_move_insn (x, disp);
  419. rtx add2_insn = emit_add2_insn (x, base);
  420. lra_assert (add2_insn != NULL_RTX);
  421. }
  422. /* Generate x = x + index. */
  423. if (index != NULL_RTX)
  424. {
  425. rtx insn = emit_add2_insn (x, index);
  426. lra_assert (insn != NULL_RTX);
  427. }
  428. }
  429. else
  430. {
  431. /* Try x = index_scale; x = x + disp; x = x + base. */
  432. last = get_last_insn ();
  433. rtx_insn *move_insn = emit_move_insn (x, index_scale);
  434. ok_p = false;
  435. if (recog_memoized (move_insn) >= 0)
  436. {
  437. rtx insn = emit_add2_insn (x, disp);
  438. if (insn != NULL_RTX)
  439. {
  440. insn = emit_add2_insn (x, base);
  441. if (insn != NULL_RTX)
  442. ok_p = true;
  443. }
  444. }
  445. if (! ok_p)
  446. {
  447. delete_insns_since (last);
  448. /* Generate x = disp; x = x + base; x = x + index_scale. */
  449. emit_move_insn (x, disp);
  450. rtx insn = emit_add2_insn (x, base);
  451. lra_assert (insn != NULL_RTX);
  452. insn = emit_add2_insn (x, index_scale);
  453. lra_assert (insn != NULL_RTX);
  454. }
  455. }
  456. }
  457. }
  458. /* Functions emit_... can create pseudos -- so expand the pseudo
  459. data. */
  460. if (old != max_reg_num ())
  461. expand_reg_data (old);
  462. }
  463. /* The number of emitted reload insns so far. */
  464. int lra_curr_reload_num;
  465. /* Emit x := y, processing special case when y = u + v or y = u + v *
  466. scale + w through emit_add (Y can be an address which is base +
  467. index reg * scale + displacement in general case). X may be used
  468. as intermediate result therefore it should be not in Y. */
  469. void
  470. lra_emit_move (rtx x, rtx y)
  471. {
  472. int old;
  473. if (GET_CODE (y) != PLUS)
  474. {
  475. if (rtx_equal_p (x, y))
  476. return;
  477. old = max_reg_num ();
  478. emit_move_insn (x, y);
  479. if (REG_P (x))
  480. lra_reg_info[ORIGINAL_REGNO (x)].last_reload = ++lra_curr_reload_num;
  481. /* Function emit_move can create pseudos -- so expand the pseudo
  482. data. */
  483. if (old != max_reg_num ())
  484. expand_reg_data (old);
  485. return;
  486. }
  487. lra_emit_add (x, XEXP (y, 0), XEXP (y, 1));
  488. }
  489. /* Update insn operands which are duplication of operands whose
  490. numbers are in array of NOPS (with end marker -1). The insn is
  491. represented by its LRA internal representation ID. */
  492. void
  493. lra_update_dups (lra_insn_recog_data_t id, signed char *nops)
  494. {
  495. int i, j, nop;
  496. struct lra_static_insn_data *static_id = id->insn_static_data;
  497. for (i = 0; i < static_id->n_dups; i++)
  498. for (j = 0; (nop = nops[j]) >= 0; j++)
  499. if (static_id->dup_num[i] == nop)
  500. *id->dup_loc[i] = *id->operand_loc[nop];
  501. }
  502. /* This page contains code dealing with info about registers in the
  503. insns. */
  504. /* Pools for insn reg info. */
  505. static alloc_pool insn_reg_pool;
  506. /* Initiate pool for insn reg info. */
  507. static void
  508. init_insn_regs (void)
  509. {
  510. insn_reg_pool
  511. = create_alloc_pool ("insn regs", sizeof (struct lra_insn_reg), 100);
  512. }
  513. /* Create LRA insn related info about a reference to REGNO in INSN with
  514. TYPE (in/out/inout), biggest reference mode MODE, flag that it is
  515. reference through subreg (SUBREG_P), flag that is early clobbered
  516. in the insn (EARLY_CLOBBER), and reference to the next insn reg
  517. info (NEXT). */
  518. static struct lra_insn_reg *
  519. new_insn_reg (rtx_insn *insn, int regno, enum op_type type,
  520. machine_mode mode,
  521. bool subreg_p, bool early_clobber, struct lra_insn_reg *next)
  522. {
  523. struct lra_insn_reg *ir;
  524. ir = (struct lra_insn_reg *) pool_alloc (insn_reg_pool);
  525. ir->type = type;
  526. ir->biggest_mode = mode;
  527. if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (lra_reg_info[regno].biggest_mode)
  528. && NONDEBUG_INSN_P (insn))
  529. lra_reg_info[regno].biggest_mode = mode;
  530. ir->subreg_p = subreg_p;
  531. ir->early_clobber = early_clobber;
  532. ir->regno = regno;
  533. ir->next = next;
  534. return ir;
  535. }
  536. /* Free insn reg info IR. */
  537. static void
  538. free_insn_reg (struct lra_insn_reg *ir)
  539. {
  540. pool_free (insn_reg_pool, ir);
  541. }
  542. /* Free insn reg info list IR. */
  543. static void
  544. free_insn_regs (struct lra_insn_reg *ir)
  545. {
  546. struct lra_insn_reg *next_ir;
  547. for (; ir != NULL; ir = next_ir)
  548. {
  549. next_ir = ir->next;
  550. free_insn_reg (ir);
  551. }
  552. }
  553. /* Finish pool for insn reg info. */
  554. static void
  555. finish_insn_regs (void)
  556. {
  557. free_alloc_pool (insn_reg_pool);
  558. }
  559. /* This page contains code dealing LRA insn info (or in other words
  560. LRA internal insn representation). */
  561. /* Map INSN_CODE -> the static insn data. This info is valid during
  562. all translation unit. */
  563. struct lra_static_insn_data *insn_code_data[LAST_INSN_CODE];
  564. /* Debug insns are represented as a special insn with one input
  565. operand which is RTL expression in var_location. */
  566. /* The following data are used as static insn operand data for all
  567. debug insns. If structure lra_operand_data is changed, the
  568. initializer should be changed too. */
  569. static struct lra_operand_data debug_operand_data =
  570. {
  571. NULL, /* alternative */
  572. VOIDmode, /* We are not interesting in the operand mode. */
  573. OP_IN,
  574. 0, 0, 0, 0
  575. };
  576. /* The following data are used as static insn data for all debug
  577. insns. If structure lra_static_insn_data is changed, the
  578. initializer should be changed too. */
  579. static struct lra_static_insn_data debug_insn_static_data =
  580. {
  581. &debug_operand_data,
  582. 0, /* Duplication operands #. */
  583. -1, /* Commutative operand #. */
  584. 1, /* Operands #. There is only one operand which is debug RTL
  585. expression. */
  586. 0, /* Duplications #. */
  587. 0, /* Alternatives #. We are not interesting in alternatives
  588. because we does not proceed debug_insns for reloads. */
  589. NULL, /* Hard registers referenced in machine description. */
  590. NULL /* Descriptions of operands in alternatives. */
  591. };
  592. /* Called once per compiler work to initialize some LRA data related
  593. to insns. */
  594. static void
  595. init_insn_code_data_once (void)
  596. {
  597. memset (insn_code_data, 0, sizeof (insn_code_data));
  598. }
  599. /* Called once per compiler work to finalize some LRA data related to
  600. insns. */
  601. static void
  602. finish_insn_code_data_once (void)
  603. {
  604. int i;
  605. for (i = 0; i < LAST_INSN_CODE; i++)
  606. {
  607. if (insn_code_data[i] != NULL)
  608. free (insn_code_data[i]);
  609. }
  610. }
  611. /* Return static insn data, allocate and setup if necessary. Although
  612. dup_num is static data (it depends only on icode), to set it up we
  613. need to extract insn first. So recog_data should be valid for
  614. normal insn (ICODE >= 0) before the call. */
  615. static struct lra_static_insn_data *
  616. get_static_insn_data (int icode, int nop, int ndup, int nalt)
  617. {
  618. struct lra_static_insn_data *data;
  619. size_t n_bytes;
  620. lra_assert (icode < LAST_INSN_CODE);
  621. if (icode >= 0 && (data = insn_code_data[icode]) != NULL)
  622. return data;
  623. lra_assert (nop >= 0 && ndup >= 0 && nalt >= 0);
  624. n_bytes = sizeof (struct lra_static_insn_data)
  625. + sizeof (struct lra_operand_data) * nop
  626. + sizeof (int) * ndup;
  627. data = XNEWVAR (struct lra_static_insn_data, n_bytes);
  628. data->operand_alternative = NULL;
  629. data->n_operands = nop;
  630. data->n_dups = ndup;
  631. data->n_alternatives = nalt;
  632. data->operand = ((struct lra_operand_data *)
  633. ((char *) data + sizeof (struct lra_static_insn_data)));
  634. data->dup_num = ((int *) ((char *) data->operand
  635. + sizeof (struct lra_operand_data) * nop));
  636. if (icode >= 0)
  637. {
  638. int i;
  639. insn_code_data[icode] = data;
  640. for (i = 0; i < nop; i++)
  641. {
  642. data->operand[i].constraint
  643. = insn_data[icode].operand[i].constraint;
  644. data->operand[i].mode = insn_data[icode].operand[i].mode;
  645. data->operand[i].strict_low = insn_data[icode].operand[i].strict_low;
  646. data->operand[i].is_operator
  647. = insn_data[icode].operand[i].is_operator;
  648. data->operand[i].type
  649. = (data->operand[i].constraint[0] == '=' ? OP_OUT
  650. : data->operand[i].constraint[0] == '+' ? OP_INOUT
  651. : OP_IN);
  652. data->operand[i].is_address = false;
  653. }
  654. for (i = 0; i < ndup; i++)
  655. data->dup_num[i] = recog_data.dup_num[i];
  656. }
  657. return data;
  658. }
  659. /* The current length of the following array. */
  660. int lra_insn_recog_data_len;
  661. /* Map INSN_UID -> the insn recog data (NULL if unknown). */
  662. lra_insn_recog_data_t *lra_insn_recog_data;
  663. /* Initialize LRA data about insns. */
  664. static void
  665. init_insn_recog_data (void)
  666. {
  667. lra_insn_recog_data_len = 0;
  668. lra_insn_recog_data = NULL;
  669. init_insn_regs ();
  670. }
  671. /* Expand, if necessary, LRA data about insns. */
  672. static void
  673. check_and_expand_insn_recog_data (int index)
  674. {
  675. int i, old;
  676. if (lra_insn_recog_data_len > index)
  677. return;
  678. old = lra_insn_recog_data_len;
  679. lra_insn_recog_data_len = index * 3 / 2 + 1;
  680. lra_insn_recog_data = XRESIZEVEC (lra_insn_recog_data_t,
  681. lra_insn_recog_data,
  682. lra_insn_recog_data_len);
  683. for (i = old; i < lra_insn_recog_data_len; i++)
  684. lra_insn_recog_data[i] = NULL;
  685. }
  686. /* Finish LRA DATA about insn. */
  687. static void
  688. free_insn_recog_data (lra_insn_recog_data_t data)
  689. {
  690. if (data->operand_loc != NULL)
  691. free (data->operand_loc);
  692. if (data->dup_loc != NULL)
  693. free (data->dup_loc);
  694. if (data->arg_hard_regs != NULL)
  695. free (data->arg_hard_regs);
  696. if (data->icode < 0 && NONDEBUG_INSN_P (data->insn))
  697. {
  698. if (data->insn_static_data->operand_alternative != NULL)
  699. free (const_cast <operand_alternative *>
  700. (data->insn_static_data->operand_alternative));
  701. free_insn_regs (data->insn_static_data->hard_regs);
  702. free (data->insn_static_data);
  703. }
  704. free_insn_regs (data->regs);
  705. data->regs = NULL;
  706. free (data);
  707. }
  708. /* Finish LRA data about all insns. */
  709. static void
  710. finish_insn_recog_data (void)
  711. {
  712. int i;
  713. lra_insn_recog_data_t data;
  714. for (i = 0; i < lra_insn_recog_data_len; i++)
  715. if ((data = lra_insn_recog_data[i]) != NULL)
  716. free_insn_recog_data (data);
  717. finish_insn_regs ();
  718. free (lra_insn_recog_data);
  719. }
  720. /* Setup info about operands in alternatives of LRA DATA of insn. */
  721. static void
  722. setup_operand_alternative (lra_insn_recog_data_t data,
  723. const operand_alternative *op_alt)
  724. {
  725. int i, j, nop, nalt;
  726. int icode = data->icode;
  727. struct lra_static_insn_data *static_data = data->insn_static_data;
  728. static_data->commutative = -1;
  729. nop = static_data->n_operands;
  730. nalt = static_data->n_alternatives;
  731. static_data->operand_alternative = op_alt;
  732. for (i = 0; i < nop; i++)
  733. {
  734. static_data->operand[i].early_clobber = false;
  735. static_data->operand[i].is_address = false;
  736. if (static_data->operand[i].constraint[0] == '%')
  737. {
  738. /* We currently only support one commutative pair of operands. */
  739. if (static_data->commutative < 0)
  740. static_data->commutative = i;
  741. else
  742. lra_assert (icode < 0); /* Asm */
  743. /* The last operand should not be marked commutative. */
  744. lra_assert (i != nop - 1);
  745. }
  746. }
  747. for (j = 0; j < nalt; j++)
  748. for (i = 0; i < nop; i++, op_alt++)
  749. {
  750. static_data->operand[i].early_clobber |= op_alt->earlyclobber;
  751. static_data->operand[i].is_address |= op_alt->is_address;
  752. }
  753. }
  754. /* Recursively process X and collect info about registers, which are
  755. not the insn operands, in X with TYPE (in/out/inout) and flag that
  756. it is early clobbered in the insn (EARLY_CLOBBER) and add the info
  757. to LIST. X is a part of insn given by DATA. Return the result
  758. list. */
  759. static struct lra_insn_reg *
  760. collect_non_operand_hard_regs (rtx *x, lra_insn_recog_data_t data,
  761. struct lra_insn_reg *list,
  762. enum op_type type, bool early_clobber)
  763. {
  764. int i, j, regno, last;
  765. bool subreg_p;
  766. machine_mode mode;
  767. struct lra_insn_reg *curr;
  768. rtx op = *x;
  769. enum rtx_code code = GET_CODE (op);
  770. const char *fmt = GET_RTX_FORMAT (code);
  771. for (i = 0; i < data->insn_static_data->n_operands; i++)
  772. if (x == data->operand_loc[i])
  773. /* It is an operand loc. Stop here. */
  774. return list;
  775. for (i = 0; i < data->insn_static_data->n_dups; i++)
  776. if (x == data->dup_loc[i])
  777. /* It is a dup loc. Stop here. */
  778. return list;
  779. mode = GET_MODE (op);
  780. subreg_p = false;
  781. if (code == SUBREG)
  782. {
  783. op = SUBREG_REG (op);
  784. code = GET_CODE (op);
  785. if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (op)))
  786. {
  787. mode = GET_MODE (op);
  788. if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
  789. subreg_p = true;
  790. }
  791. }
  792. if (REG_P (op))
  793. {
  794. if ((regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER)
  795. return list;
  796. /* Process all regs even unallocatable ones as we need info
  797. about all regs for rematerialization pass. */
  798. for (last = regno + hard_regno_nregs[regno][mode];
  799. regno < last;
  800. regno++)
  801. {
  802. for (curr = list; curr != NULL; curr = curr->next)
  803. if (curr->regno == regno && curr->subreg_p == subreg_p
  804. && curr->biggest_mode == mode)
  805. {
  806. if (curr->type != type)
  807. curr->type = OP_INOUT;
  808. if (curr->early_clobber != early_clobber)
  809. curr->early_clobber = true;
  810. break;
  811. }
  812. if (curr == NULL)
  813. {
  814. /* This is a new hard regno or the info can not be
  815. integrated into the found structure. */
  816. #ifdef STACK_REGS
  817. early_clobber
  818. = (early_clobber
  819. /* This clobber is to inform popping floating
  820. point stack only. */
  821. && ! (FIRST_STACK_REG <= regno
  822. && regno <= LAST_STACK_REG));
  823. #endif
  824. list = new_insn_reg (data->insn, regno, type, mode, subreg_p,
  825. early_clobber, list);
  826. }
  827. }
  828. return list;
  829. }
  830. switch (code)
  831. {
  832. case SET:
  833. list = collect_non_operand_hard_regs (&SET_DEST (op), data,
  834. list, OP_OUT, false);
  835. list = collect_non_operand_hard_regs (&SET_SRC (op), data,
  836. list, OP_IN, false);
  837. break;
  838. case CLOBBER:
  839. /* We treat clobber of non-operand hard registers as early
  840. clobber (the behavior is expected from asm). */
  841. list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
  842. list, OP_OUT, true);
  843. break;
  844. case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
  845. list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
  846. list, OP_INOUT, false);
  847. break;
  848. case PRE_MODIFY: case POST_MODIFY:
  849. list = collect_non_operand_hard_regs (&XEXP (op, 0), data,
  850. list, OP_INOUT, false);
  851. list = collect_non_operand_hard_regs (&XEXP (op, 1), data,
  852. list, OP_IN, false);
  853. break;
  854. default:
  855. fmt = GET_RTX_FORMAT (code);
  856. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  857. {
  858. if (fmt[i] == 'e')
  859. list = collect_non_operand_hard_regs (&XEXP (op, i), data,
  860. list, OP_IN, false);
  861. else if (fmt[i] == 'E')
  862. for (j = XVECLEN (op, i) - 1; j >= 0; j--)
  863. list = collect_non_operand_hard_regs (&XVECEXP (op, i, j), data,
  864. list, OP_IN, false);
  865. }
  866. }
  867. return list;
  868. }
  869. /* Set up and return info about INSN. Set up the info if it is not set up
  870. yet. */
  871. lra_insn_recog_data_t
  872. lra_set_insn_recog_data (rtx_insn *insn)
  873. {
  874. lra_insn_recog_data_t data;
  875. int i, n, icode;
  876. rtx **locs;
  877. unsigned int uid = INSN_UID (insn);
  878. struct lra_static_insn_data *insn_static_data;
  879. check_and_expand_insn_recog_data (uid);
  880. if (DEBUG_INSN_P (insn))
  881. icode = -1;
  882. else
  883. {
  884. icode = INSN_CODE (insn);
  885. if (icode < 0)
  886. /* It might be a new simple insn which is not recognized yet. */
  887. INSN_CODE (insn) = icode = recog_memoized (insn);
  888. }
  889. data = XNEW (struct lra_insn_recog_data);
  890. lra_insn_recog_data[uid] = data;
  891. data->insn = insn;
  892. data->used_insn_alternative = -1;
  893. data->icode = icode;
  894. data->regs = NULL;
  895. if (DEBUG_INSN_P (insn))
  896. {
  897. data->insn_static_data = &debug_insn_static_data;
  898. data->dup_loc = NULL;
  899. data->arg_hard_regs = NULL;
  900. data->preferred_alternatives = ALL_ALTERNATIVES;
  901. data->operand_loc = XNEWVEC (rtx *, 1);
  902. data->operand_loc[0] = &INSN_VAR_LOCATION_LOC (insn);
  903. return data;
  904. }
  905. if (icode < 0)
  906. {
  907. int nop, nalt;
  908. machine_mode operand_mode[MAX_RECOG_OPERANDS];
  909. const char *constraints[MAX_RECOG_OPERANDS];
  910. nop = asm_noperands (PATTERN (insn));
  911. data->operand_loc = data->dup_loc = NULL;
  912. nalt = 1;
  913. if (nop < 0)
  914. {
  915. /* It is a special insn like USE or CLOBBER. We should
  916. recognize any regular insn otherwise LRA can do nothing
  917. with this insn. */
  918. gcc_assert (GET_CODE (PATTERN (insn)) == USE
  919. || GET_CODE (PATTERN (insn)) == CLOBBER
  920. || GET_CODE (PATTERN (insn)) == ASM_INPUT);
  921. data->insn_static_data = insn_static_data
  922. = get_static_insn_data (-1, 0, 0, nalt);
  923. }
  924. else
  925. {
  926. /* expand_asm_operands makes sure there aren't too many
  927. operands. */
  928. lra_assert (nop <= MAX_RECOG_OPERANDS);
  929. if (nop != 0)
  930. data->operand_loc = XNEWVEC (rtx *, nop);
  931. /* Now get the operand values and constraints out of the
  932. insn. */
  933. decode_asm_operands (PATTERN (insn), NULL,
  934. data->operand_loc,
  935. constraints, operand_mode, NULL);
  936. if (nop > 0)
  937. {
  938. const char *p = recog_data.constraints[0];
  939. for (p = constraints[0]; *p; p++)
  940. nalt += *p == ',';
  941. }
  942. data->insn_static_data = insn_static_data
  943. = get_static_insn_data (-1, nop, 0, nalt);
  944. for (i = 0; i < nop; i++)
  945. {
  946. insn_static_data->operand[i].mode = operand_mode[i];
  947. insn_static_data->operand[i].constraint = constraints[i];
  948. insn_static_data->operand[i].strict_low = false;
  949. insn_static_data->operand[i].is_operator = false;
  950. insn_static_data->operand[i].is_address = false;
  951. }
  952. }
  953. for (i = 0; i < insn_static_data->n_operands; i++)
  954. insn_static_data->operand[i].type
  955. = (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
  956. : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
  957. : OP_IN);
  958. data->preferred_alternatives = ALL_ALTERNATIVES;
  959. if (nop > 0)
  960. {
  961. operand_alternative *op_alt = XCNEWVEC (operand_alternative,
  962. nalt * nop);
  963. preprocess_constraints (nop, nalt, constraints, op_alt);
  964. setup_operand_alternative (data, op_alt);
  965. }
  966. }
  967. else
  968. {
  969. insn_extract (insn);
  970. data->insn_static_data = insn_static_data
  971. = get_static_insn_data (icode, insn_data[icode].n_operands,
  972. insn_data[icode].n_dups,
  973. insn_data[icode].n_alternatives);
  974. n = insn_static_data->n_operands;
  975. if (n == 0)
  976. locs = NULL;
  977. else
  978. {
  979. locs = XNEWVEC (rtx *, n);
  980. memcpy (locs, recog_data.operand_loc, n * sizeof (rtx *));
  981. }
  982. data->operand_loc = locs;
  983. n = insn_static_data->n_dups;
  984. if (n == 0)
  985. locs = NULL;
  986. else
  987. {
  988. locs = XNEWVEC (rtx *, n);
  989. memcpy (locs, recog_data.dup_loc, n * sizeof (rtx *));
  990. }
  991. data->dup_loc = locs;
  992. data->preferred_alternatives = get_preferred_alternatives (insn);
  993. const operand_alternative *op_alt = preprocess_insn_constraints (icode);
  994. if (!insn_static_data->operand_alternative)
  995. setup_operand_alternative (data, op_alt);
  996. else if (op_alt != insn_static_data->operand_alternative)
  997. insn_static_data->operand_alternative = op_alt;
  998. }
  999. if (GET_CODE (PATTERN (insn)) == CLOBBER || GET_CODE (PATTERN (insn)) == USE)
  1000. insn_static_data->hard_regs = NULL;
  1001. else
  1002. insn_static_data->hard_regs
  1003. = collect_non_operand_hard_regs (&PATTERN (insn), data,
  1004. NULL, OP_IN, false);
  1005. data->arg_hard_regs = NULL;
  1006. if (CALL_P (insn))
  1007. {
  1008. bool use_p;
  1009. rtx link;
  1010. int n_hard_regs, regno, arg_hard_regs[FIRST_PSEUDO_REGISTER];
  1011. n_hard_regs = 0;
  1012. /* Finding implicit hard register usage. We believe it will be
  1013. not changed whatever transformations are used. Call insns
  1014. are such example. */
  1015. for (link = CALL_INSN_FUNCTION_USAGE (insn);
  1016. link != NULL_RTX;
  1017. link = XEXP (link, 1))
  1018. if (((use_p = GET_CODE (XEXP (link, 0)) == USE)
  1019. || GET_CODE (XEXP (link, 0)) == CLOBBER)
  1020. && REG_P (XEXP (XEXP (link, 0), 0)))
  1021. {
  1022. regno = REGNO (XEXP (XEXP (link, 0), 0));
  1023. lra_assert (regno < FIRST_PSEUDO_REGISTER);
  1024. /* It is an argument register. */
  1025. for (i = (hard_regno_nregs
  1026. [regno][GET_MODE (XEXP (XEXP (link, 0), 0))]) - 1;
  1027. i >= 0;
  1028. i--)
  1029. arg_hard_regs[n_hard_regs++]
  1030. = regno + i + (use_p ? 0 : FIRST_PSEUDO_REGISTER);
  1031. }
  1032. if (n_hard_regs != 0)
  1033. {
  1034. arg_hard_regs[n_hard_regs++] = -1;
  1035. data->arg_hard_regs = XNEWVEC (int, n_hard_regs);
  1036. memcpy (data->arg_hard_regs, arg_hard_regs,
  1037. sizeof (int) * n_hard_regs);
  1038. }
  1039. }
  1040. /* Some output operand can be recognized only from the context not
  1041. from the constraints which are empty in this case. Call insn may
  1042. contain a hard register in set destination with empty constraint
  1043. and extract_insn treats them as an input. */
  1044. for (i = 0; i < insn_static_data->n_operands; i++)
  1045. {
  1046. int j;
  1047. rtx pat, set;
  1048. struct lra_operand_data *operand = &insn_static_data->operand[i];
  1049. /* ??? Should we treat 'X' the same way. It looks to me that
  1050. 'X' means anything and empty constraint means we do not
  1051. care. */
  1052. if (operand->type != OP_IN || *operand->constraint != '\0'
  1053. || operand->is_operator)
  1054. continue;
  1055. pat = PATTERN (insn);
  1056. if (GET_CODE (pat) == SET)
  1057. {
  1058. if (data->operand_loc[i] != &SET_DEST (pat))
  1059. continue;
  1060. }
  1061. else if (GET_CODE (pat) == PARALLEL)
  1062. {
  1063. for (j = XVECLEN (pat, 0) - 1; j >= 0; j--)
  1064. {
  1065. set = XVECEXP (PATTERN (insn), 0, j);
  1066. if (GET_CODE (set) == SET
  1067. && &SET_DEST (set) == data->operand_loc[i])
  1068. break;
  1069. }
  1070. if (j < 0)
  1071. continue;
  1072. }
  1073. else
  1074. continue;
  1075. operand->type = OP_OUT;
  1076. }
  1077. return data;
  1078. }
  1079. /* Return info about insn give by UID. The info should be already set
  1080. up. */
  1081. static lra_insn_recog_data_t
  1082. get_insn_recog_data_by_uid (int uid)
  1083. {
  1084. lra_insn_recog_data_t data;
  1085. data = lra_insn_recog_data[uid];
  1086. lra_assert (data != NULL);
  1087. return data;
  1088. }
  1089. /* Invalidate all info about insn given by its UID. */
  1090. static void
  1091. invalidate_insn_recog_data (int uid)
  1092. {
  1093. lra_insn_recog_data_t data;
  1094. data = lra_insn_recog_data[uid];
  1095. lra_assert (data != NULL);
  1096. free_insn_recog_data (data);
  1097. lra_insn_recog_data[uid] = NULL;
  1098. }
  1099. /* Update all the insn info about INSN. It is usually called when
  1100. something in the insn was changed. Return the updated info. */
  1101. lra_insn_recog_data_t
  1102. lra_update_insn_recog_data (rtx_insn *insn)
  1103. {
  1104. lra_insn_recog_data_t data;
  1105. int n;
  1106. unsigned int uid = INSN_UID (insn);
  1107. struct lra_static_insn_data *insn_static_data;
  1108. HOST_WIDE_INT sp_offset = 0;
  1109. check_and_expand_insn_recog_data (uid);
  1110. if ((data = lra_insn_recog_data[uid]) != NULL
  1111. && data->icode != INSN_CODE (insn))
  1112. {
  1113. sp_offset = data->sp_offset;
  1114. invalidate_insn_data_regno_info (data, insn, get_insn_freq (insn));
  1115. invalidate_insn_recog_data (uid);
  1116. data = NULL;
  1117. }
  1118. if (data == NULL)
  1119. {
  1120. data = lra_get_insn_recog_data (insn);
  1121. /* Initiate or restore SP offset. */
  1122. data->sp_offset = sp_offset;
  1123. return data;
  1124. }
  1125. insn_static_data = data->insn_static_data;
  1126. data->used_insn_alternative = -1;
  1127. if (DEBUG_INSN_P (insn))
  1128. return data;
  1129. if (data->icode < 0)
  1130. {
  1131. int nop;
  1132. machine_mode operand_mode[MAX_RECOG_OPERANDS];
  1133. const char *constraints[MAX_RECOG_OPERANDS];
  1134. nop = asm_noperands (PATTERN (insn));
  1135. if (nop >= 0)
  1136. {
  1137. lra_assert (nop == data->insn_static_data->n_operands);
  1138. /* Now get the operand values and constraints out of the
  1139. insn. */
  1140. decode_asm_operands (PATTERN (insn), NULL,
  1141. data->operand_loc,
  1142. constraints, operand_mode, NULL);
  1143. #ifdef ENABLE_CHECKING
  1144. {
  1145. int i;
  1146. for (i = 0; i < nop; i++)
  1147. lra_assert
  1148. (insn_static_data->operand[i].mode == operand_mode[i]
  1149. && insn_static_data->operand[i].constraint == constraints[i]
  1150. && ! insn_static_data->operand[i].is_operator);
  1151. }
  1152. #endif
  1153. }
  1154. #ifdef ENABLE_CHECKING
  1155. {
  1156. int i;
  1157. for (i = 0; i < insn_static_data->n_operands; i++)
  1158. lra_assert
  1159. (insn_static_data->operand[i].type
  1160. == (insn_static_data->operand[i].constraint[0] == '=' ? OP_OUT
  1161. : insn_static_data->operand[i].constraint[0] == '+' ? OP_INOUT
  1162. : OP_IN));
  1163. }
  1164. #endif
  1165. }
  1166. else
  1167. {
  1168. insn_extract (insn);
  1169. n = insn_static_data->n_operands;
  1170. if (n != 0)
  1171. memcpy (data->operand_loc, recog_data.operand_loc, n * sizeof (rtx *));
  1172. n = insn_static_data->n_dups;
  1173. if (n != 0)
  1174. memcpy (data->dup_loc, recog_data.dup_loc, n * sizeof (rtx *));
  1175. lra_assert (check_bool_attrs (insn));
  1176. }
  1177. return data;
  1178. }
  1179. /* Set up that INSN is using alternative ALT now. */
  1180. void
  1181. lra_set_used_insn_alternative (rtx_insn *insn, int alt)
  1182. {
  1183. lra_insn_recog_data_t data;
  1184. data = lra_get_insn_recog_data (insn);
  1185. data->used_insn_alternative = alt;
  1186. }
  1187. /* Set up that insn with UID is using alternative ALT now. The insn
  1188. info should be already set up. */
  1189. void
  1190. lra_set_used_insn_alternative_by_uid (int uid, int alt)
  1191. {
  1192. lra_insn_recog_data_t data;
  1193. check_and_expand_insn_recog_data (uid);
  1194. data = lra_insn_recog_data[uid];
  1195. lra_assert (data != NULL);
  1196. data->used_insn_alternative = alt;
  1197. }
  1198. /* This page contains code dealing with common register info and
  1199. pseudo copies. */
  1200. /* The size of the following array. */
  1201. static int reg_info_size;
  1202. /* Common info about each register. */
  1203. struct lra_reg *lra_reg_info;
  1204. /* Last register value. */
  1205. static int last_reg_value;
  1206. /* Return new register value. */
  1207. static int
  1208. get_new_reg_value (void)
  1209. {
  1210. return ++last_reg_value;
  1211. }
  1212. /* Pools for copies. */
  1213. static alloc_pool copy_pool;
  1214. /* Vec referring to pseudo copies. */
  1215. static vec<lra_copy_t> copy_vec;
  1216. /* Initialize I-th element of lra_reg_info. */
  1217. static inline void
  1218. initialize_lra_reg_info_element (int i)
  1219. {
  1220. bitmap_initialize (&lra_reg_info[i].insn_bitmap, &reg_obstack);
  1221. #ifdef STACK_REGS
  1222. lra_reg_info[i].no_stack_p = false;
  1223. #endif
  1224. CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
  1225. CLEAR_HARD_REG_SET (lra_reg_info[i].actual_call_used_reg_set);
  1226. lra_reg_info[i].preferred_hard_regno1 = -1;
  1227. lra_reg_info[i].preferred_hard_regno2 = -1;
  1228. lra_reg_info[i].preferred_hard_regno_profit1 = 0;
  1229. lra_reg_info[i].preferred_hard_regno_profit2 = 0;
  1230. lra_reg_info[i].biggest_mode = VOIDmode;
  1231. lra_reg_info[i].live_ranges = NULL;
  1232. lra_reg_info[i].nrefs = lra_reg_info[i].freq = 0;
  1233. lra_reg_info[i].last_reload = 0;
  1234. lra_reg_info[i].restore_regno = -1;
  1235. lra_reg_info[i].val = get_new_reg_value ();
  1236. lra_reg_info[i].offset = 0;
  1237. lra_reg_info[i].copies = NULL;
  1238. }
  1239. /* Initialize common reg info and copies. */
  1240. static void
  1241. init_reg_info (void)
  1242. {
  1243. int i;
  1244. last_reg_value = 0;
  1245. reg_info_size = max_reg_num () * 3 / 2 + 1;
  1246. lra_reg_info = XNEWVEC (struct lra_reg, reg_info_size);
  1247. for (i = 0; i < reg_info_size; i++)
  1248. initialize_lra_reg_info_element (i);
  1249. copy_pool
  1250. = create_alloc_pool ("lra copies", sizeof (struct lra_copy), 100);
  1251. copy_vec.create (100);
  1252. }
  1253. /* Finish common reg info and copies. */
  1254. static void
  1255. finish_reg_info (void)
  1256. {
  1257. int i;
  1258. for (i = 0; i < reg_info_size; i++)
  1259. bitmap_clear (&lra_reg_info[i].insn_bitmap);
  1260. free (lra_reg_info);
  1261. reg_info_size = 0;
  1262. free_alloc_pool (copy_pool);
  1263. copy_vec.release ();
  1264. }
  1265. /* Expand common reg info if it is necessary. */
  1266. static void
  1267. expand_reg_info (void)
  1268. {
  1269. int i, old = reg_info_size;
  1270. if (reg_info_size > max_reg_num ())
  1271. return;
  1272. reg_info_size = max_reg_num () * 3 / 2 + 1;
  1273. lra_reg_info = XRESIZEVEC (struct lra_reg, lra_reg_info, reg_info_size);
  1274. for (i = old; i < reg_info_size; i++)
  1275. initialize_lra_reg_info_element (i);
  1276. }
  1277. /* Free all copies. */
  1278. void
  1279. lra_free_copies (void)
  1280. {
  1281. lra_copy_t cp;
  1282. while (copy_vec.length () != 0)
  1283. {
  1284. cp = copy_vec.pop ();
  1285. lra_reg_info[cp->regno1].copies = lra_reg_info[cp->regno2].copies = NULL;
  1286. pool_free (copy_pool, cp);
  1287. }
  1288. }
  1289. /* Create copy of two pseudos REGNO1 and REGNO2. The copy execution
  1290. frequency is FREQ. */
  1291. void
  1292. lra_create_copy (int regno1, int regno2, int freq)
  1293. {
  1294. bool regno1_dest_p;
  1295. lra_copy_t cp;
  1296. lra_assert (regno1 != regno2);
  1297. regno1_dest_p = true;
  1298. if (regno1 > regno2)
  1299. {
  1300. int temp = regno2;
  1301. regno1_dest_p = false;
  1302. regno2 = regno1;
  1303. regno1 = temp;
  1304. }
  1305. cp = (lra_copy_t) pool_alloc (copy_pool);
  1306. copy_vec.safe_push (cp);
  1307. cp->regno1_dest_p = regno1_dest_p;
  1308. cp->freq = freq;
  1309. cp->regno1 = regno1;
  1310. cp->regno2 = regno2;
  1311. cp->regno1_next = lra_reg_info[regno1].copies;
  1312. lra_reg_info[regno1].copies = cp;
  1313. cp->regno2_next = lra_reg_info[regno2].copies;
  1314. lra_reg_info[regno2].copies = cp;
  1315. if (lra_dump_file != NULL)
  1316. fprintf (lra_dump_file, " Creating copy r%d%sr%d@%d\n",
  1317. regno1, regno1_dest_p ? "<-" : "->", regno2, freq);
  1318. }
  1319. /* Return N-th (0, 1, ...) copy. If there is no copy, return
  1320. NULL. */
  1321. lra_copy_t
  1322. lra_get_copy (int n)
  1323. {
  1324. if (n >= (int) copy_vec.length ())
  1325. return NULL;
  1326. return copy_vec[n];
  1327. }
  1328. /* This page contains code dealing with info about registers in
  1329. insns. */
  1330. /* Process X of insn UID recursively and add info (operand type is
  1331. given by TYPE, flag of that it is early clobber is EARLY_CLOBBER)
  1332. about registers in X to the insn DATA. */
  1333. static void
  1334. add_regs_to_insn_regno_info (lra_insn_recog_data_t data, rtx x, int uid,
  1335. enum op_type type, bool early_clobber)
  1336. {
  1337. int i, j, regno;
  1338. bool subreg_p;
  1339. machine_mode mode;
  1340. const char *fmt;
  1341. enum rtx_code code;
  1342. struct lra_insn_reg *curr;
  1343. code = GET_CODE (x);
  1344. mode = GET_MODE (x);
  1345. subreg_p = false;
  1346. if (GET_CODE (x) == SUBREG)
  1347. {
  1348. x = SUBREG_REG (x);
  1349. code = GET_CODE (x);
  1350. if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
  1351. {
  1352. mode = GET_MODE (x);
  1353. if (GET_MODE_SIZE (mode) > REGMODE_NATURAL_SIZE (mode))
  1354. subreg_p = true;
  1355. }
  1356. }
  1357. if (REG_P (x))
  1358. {
  1359. regno = REGNO (x);
  1360. /* Process all regs even unallocatable ones as we need info about
  1361. all regs for rematerialization pass. */
  1362. expand_reg_info ();
  1363. if (bitmap_set_bit (&lra_reg_info[regno].insn_bitmap, uid))
  1364. {
  1365. data->regs = new_insn_reg (data->insn, regno, type, mode, subreg_p,
  1366. early_clobber, data->regs);
  1367. return;
  1368. }
  1369. else
  1370. {
  1371. for (curr = data->regs; curr != NULL; curr = curr->next)
  1372. if (curr->regno == regno)
  1373. {
  1374. if (curr->subreg_p != subreg_p || curr->biggest_mode != mode)
  1375. /* The info can not be integrated into the found
  1376. structure. */
  1377. data->regs = new_insn_reg (data->insn, regno, type, mode,
  1378. subreg_p, early_clobber,
  1379. data->regs);
  1380. else
  1381. {
  1382. if (curr->type != type)
  1383. curr->type = OP_INOUT;
  1384. if (curr->early_clobber != early_clobber)
  1385. curr->early_clobber = true;
  1386. }
  1387. return;
  1388. }
  1389. gcc_unreachable ();
  1390. }
  1391. }
  1392. switch (code)
  1393. {
  1394. case SET:
  1395. add_regs_to_insn_regno_info (data, SET_DEST (x), uid, OP_OUT, false);
  1396. add_regs_to_insn_regno_info (data, SET_SRC (x), uid, OP_IN, false);
  1397. break;
  1398. case CLOBBER:
  1399. /* We treat clobber of non-operand hard registers as early
  1400. clobber (the behavior is expected from asm). */
  1401. add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_OUT, true);
  1402. break;
  1403. case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
  1404. add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
  1405. break;
  1406. case PRE_MODIFY: case POST_MODIFY:
  1407. add_regs_to_insn_regno_info (data, XEXP (x, 0), uid, OP_INOUT, false);
  1408. add_regs_to_insn_regno_info (data, XEXP (x, 1), uid, OP_IN, false);
  1409. break;
  1410. default:
  1411. if ((code != PARALLEL && code != EXPR_LIST) || type != OP_OUT)
  1412. /* Some targets place small structures in registers for return
  1413. values of functions, and those registers are wrapped in
  1414. PARALLEL that we may see as the destination of a SET. Here
  1415. is an example:
  1416. (call_insn 13 12 14 2 (set (parallel:BLK [
  1417. (expr_list:REG_DEP_TRUE (reg:DI 0 ax)
  1418. (const_int 0 [0]))
  1419. (expr_list:REG_DEP_TRUE (reg:DI 1 dx)
  1420. (const_int 8 [0x8]))
  1421. ])
  1422. (call (mem:QI (symbol_ref:DI (... */
  1423. type = OP_IN;
  1424. fmt = GET_RTX_FORMAT (code);
  1425. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1426. {
  1427. if (fmt[i] == 'e')
  1428. add_regs_to_insn_regno_info (data, XEXP (x, i), uid, type, false);
  1429. else if (fmt[i] == 'E')
  1430. {
  1431. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1432. add_regs_to_insn_regno_info (data, XVECEXP (x, i, j), uid,
  1433. type, false);
  1434. }
  1435. }
  1436. }
  1437. }
  1438. /* Return execution frequency of INSN. */
  1439. static int
  1440. get_insn_freq (rtx_insn *insn)
  1441. {
  1442. basic_block bb = BLOCK_FOR_INSN (insn);
  1443. gcc_checking_assert (bb != NULL);
  1444. return REG_FREQ_FROM_BB (bb);
  1445. }
  1446. /* Invalidate all reg info of INSN with DATA and execution frequency
  1447. FREQ. Update common info about the invalidated registers. */
  1448. static void
  1449. invalidate_insn_data_regno_info (lra_insn_recog_data_t data, rtx_insn *insn,
  1450. int freq)
  1451. {
  1452. int uid;
  1453. bool debug_p;
  1454. unsigned int i;
  1455. struct lra_insn_reg *ir, *next_ir;
  1456. uid = INSN_UID (insn);
  1457. debug_p = DEBUG_INSN_P (insn);
  1458. for (ir = data->regs; ir != NULL; ir = next_ir)
  1459. {
  1460. i = ir->regno;
  1461. next_ir = ir->next;
  1462. free_insn_reg (ir);
  1463. bitmap_clear_bit (&lra_reg_info[i].insn_bitmap, uid);
  1464. if (i >= FIRST_PSEUDO_REGISTER && ! debug_p)
  1465. {
  1466. lra_reg_info[i].nrefs--;
  1467. lra_reg_info[i].freq -= freq;
  1468. lra_assert (lra_reg_info[i].nrefs >= 0 && lra_reg_info[i].freq >= 0);
  1469. }
  1470. }
  1471. data->regs = NULL;
  1472. }
  1473. /* Invalidate all reg info of INSN. Update common info about the
  1474. invalidated registers. */
  1475. void
  1476. lra_invalidate_insn_regno_info (rtx_insn *insn)
  1477. {
  1478. invalidate_insn_data_regno_info (lra_get_insn_recog_data (insn), insn,
  1479. get_insn_freq (insn));
  1480. }
  1481. /* Update common reg info from reg info of insn given by its DATA and
  1482. execution frequency FREQ. */
  1483. static void
  1484. setup_insn_reg_info (lra_insn_recog_data_t data, int freq)
  1485. {
  1486. unsigned int i;
  1487. struct lra_insn_reg *ir;
  1488. for (ir = data->regs; ir != NULL; ir = ir->next)
  1489. if ((i = ir->regno) >= FIRST_PSEUDO_REGISTER)
  1490. {
  1491. lra_reg_info[i].nrefs++;
  1492. lra_reg_info[i].freq += freq;
  1493. }
  1494. }
  1495. /* Set up insn reg info of INSN. Update common reg info from reg info
  1496. of INSN. */
  1497. void
  1498. lra_update_insn_regno_info (rtx_insn *insn)
  1499. {
  1500. int i, uid, freq;
  1501. lra_insn_recog_data_t data;
  1502. struct lra_static_insn_data *static_data;
  1503. enum rtx_code code;
  1504. rtx link;
  1505. if (! INSN_P (insn))
  1506. return;
  1507. data = lra_get_insn_recog_data (insn);
  1508. static_data = data->insn_static_data;
  1509. freq = get_insn_freq (insn);
  1510. invalidate_insn_data_regno_info (data, insn, freq);
  1511. uid = INSN_UID (insn);
  1512. for (i = static_data->n_operands - 1; i >= 0; i--)
  1513. add_regs_to_insn_regno_info (data, *data->operand_loc[i], uid,
  1514. static_data->operand[i].type,
  1515. static_data->operand[i].early_clobber);
  1516. if ((code = GET_CODE (PATTERN (insn))) == CLOBBER || code == USE)
  1517. add_regs_to_insn_regno_info (data, XEXP (PATTERN (insn), 0), uid,
  1518. code == USE ? OP_IN : OP_OUT, false);
  1519. if (CALL_P (insn))
  1520. /* On some targets call insns can refer to pseudos in memory in
  1521. CALL_INSN_FUNCTION_USAGE list. Process them in order to
  1522. consider their occurrences in calls for different
  1523. transformations (e.g. inheritance) with given pseudos. */
  1524. for (link = CALL_INSN_FUNCTION_USAGE (insn);
  1525. link != NULL_RTX;
  1526. link = XEXP (link, 1))
  1527. if (((code = GET_CODE (XEXP (link, 0))) == USE || code == CLOBBER)
  1528. && MEM_P (XEXP (XEXP (link, 0), 0)))
  1529. add_regs_to_insn_regno_info (data, XEXP (XEXP (link, 0), 0), uid,
  1530. code == USE ? OP_IN : OP_OUT, false);
  1531. if (NONDEBUG_INSN_P (insn))
  1532. setup_insn_reg_info (data, freq);
  1533. }
  1534. /* Return reg info of insn given by it UID. */
  1535. struct lra_insn_reg *
  1536. lra_get_insn_regs (int uid)
  1537. {
  1538. lra_insn_recog_data_t data;
  1539. data = get_insn_recog_data_by_uid (uid);
  1540. return data->regs;
  1541. }
  1542. /* This page contains code dealing with stack of the insns which
  1543. should be processed by the next constraint pass. */
  1544. /* Bitmap used to put an insn on the stack only in one exemplar. */
  1545. static sbitmap lra_constraint_insn_stack_bitmap;
  1546. /* The stack itself. */
  1547. vec<rtx_insn *> lra_constraint_insn_stack;
  1548. /* Put INSN on the stack. If ALWAYS_UPDATE is true, always update the reg
  1549. info for INSN, otherwise only update it if INSN is not already on the
  1550. stack. */
  1551. static inline void
  1552. lra_push_insn_1 (rtx_insn *insn, bool always_update)
  1553. {
  1554. unsigned int uid = INSN_UID (insn);
  1555. if (always_update)
  1556. lra_update_insn_regno_info (insn);
  1557. if (uid >= SBITMAP_SIZE (lra_constraint_insn_stack_bitmap))
  1558. lra_constraint_insn_stack_bitmap =
  1559. sbitmap_resize (lra_constraint_insn_stack_bitmap, 3 * uid / 2, 0);
  1560. if (bitmap_bit_p (lra_constraint_insn_stack_bitmap, uid))
  1561. return;
  1562. bitmap_set_bit (lra_constraint_insn_stack_bitmap, uid);
  1563. if (! always_update)
  1564. lra_update_insn_regno_info (insn);
  1565. lra_constraint_insn_stack.safe_push (insn);
  1566. }
  1567. /* Put INSN on the stack. */
  1568. void
  1569. lra_push_insn (rtx_insn *insn)
  1570. {
  1571. lra_push_insn_1 (insn, false);
  1572. }
  1573. /* Put INSN on the stack and update its reg info. */
  1574. void
  1575. lra_push_insn_and_update_insn_regno_info (rtx_insn *insn)
  1576. {
  1577. lra_push_insn_1 (insn, true);
  1578. }
  1579. /* Put insn with UID on the stack. */
  1580. void
  1581. lra_push_insn_by_uid (unsigned int uid)
  1582. {
  1583. lra_push_insn (lra_insn_recog_data[uid]->insn);
  1584. }
  1585. /* Take the last-inserted insns off the stack and return it. */
  1586. rtx_insn *
  1587. lra_pop_insn (void)
  1588. {
  1589. rtx_insn *insn = lra_constraint_insn_stack.pop ();
  1590. bitmap_clear_bit (lra_constraint_insn_stack_bitmap, INSN_UID (insn));
  1591. return insn;
  1592. }
  1593. /* Return the current size of the insn stack. */
  1594. unsigned int
  1595. lra_insn_stack_length (void)
  1596. {
  1597. return lra_constraint_insn_stack.length ();
  1598. }
  1599. /* Push insns FROM to TO (excluding it) going in reverse order. */
  1600. static void
  1601. push_insns (rtx_insn *from, rtx_insn *to)
  1602. {
  1603. rtx_insn *insn;
  1604. if (from == NULL_RTX)
  1605. return;
  1606. for (insn = from; insn != to; insn = PREV_INSN (insn))
  1607. if (INSN_P (insn))
  1608. lra_push_insn (insn);
  1609. }
  1610. /* Set up sp offset for insn in range [FROM, LAST]. The offset is
  1611. taken from the next BB insn after LAST or zero if there in such
  1612. insn. */
  1613. static void
  1614. setup_sp_offset (rtx_insn *from, rtx_insn *last)
  1615. {
  1616. rtx_insn *before = next_nonnote_insn_bb (last);
  1617. HOST_WIDE_INT offset = (before == NULL_RTX || ! INSN_P (before)
  1618. ? 0 : lra_get_insn_recog_data (before)->sp_offset);
  1619. for (rtx_insn *insn = from; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
  1620. lra_get_insn_recog_data (insn)->sp_offset = offset;
  1621. }
  1622. /* Emit insns BEFORE before INSN and insns AFTER after INSN. Put the
  1623. insns onto the stack. Print about emitting the insns with
  1624. TITLE. */
  1625. void
  1626. lra_process_new_insns (rtx_insn *insn, rtx_insn *before, rtx_insn *after,
  1627. const char *title)
  1628. {
  1629. rtx_insn *last;
  1630. if (before == NULL_RTX && after == NULL_RTX)
  1631. return;
  1632. if (lra_dump_file != NULL)
  1633. {
  1634. dump_insn_slim (lra_dump_file, insn);
  1635. if (before != NULL_RTX)
  1636. {
  1637. fprintf (lra_dump_file," %s before:\n", title);
  1638. dump_rtl_slim (lra_dump_file, before, NULL, -1, 0);
  1639. }
  1640. if (after != NULL_RTX)
  1641. {
  1642. fprintf (lra_dump_file, " %s after:\n", title);
  1643. dump_rtl_slim (lra_dump_file, after, NULL, -1, 0);
  1644. }
  1645. fprintf (lra_dump_file, "\n");
  1646. }
  1647. if (before != NULL_RTX)
  1648. {
  1649. emit_insn_before (before, insn);
  1650. push_insns (PREV_INSN (insn), PREV_INSN (before));
  1651. setup_sp_offset (before, PREV_INSN (insn));
  1652. }
  1653. if (after != NULL_RTX)
  1654. {
  1655. for (last = after; NEXT_INSN (last) != NULL_RTX; last = NEXT_INSN (last))
  1656. ;
  1657. emit_insn_after (after, insn);
  1658. push_insns (last, insn);
  1659. setup_sp_offset (after, last);
  1660. }
  1661. }
  1662. /* Replace all references to register OLD_REGNO in *LOC with pseudo
  1663. register NEW_REG. Return true if any change was made. */
  1664. bool
  1665. lra_substitute_pseudo (rtx *loc, int old_regno, rtx new_reg)
  1666. {
  1667. rtx x = *loc;
  1668. bool result = false;
  1669. enum rtx_code code;
  1670. const char *fmt;
  1671. int i, j;
  1672. if (x == NULL_RTX)
  1673. return false;
  1674. code = GET_CODE (x);
  1675. if (code == REG && (int) REGNO (x) == old_regno)
  1676. {
  1677. machine_mode mode = GET_MODE (*loc);
  1678. machine_mode inner_mode = GET_MODE (new_reg);
  1679. if (mode != inner_mode
  1680. && ! (CONST_INT_P (new_reg) && SCALAR_INT_MODE_P (mode)))
  1681. {
  1682. if (GET_MODE_SIZE (mode) >= GET_MODE_SIZE (inner_mode)
  1683. || ! SCALAR_INT_MODE_P (inner_mode))
  1684. new_reg = gen_rtx_SUBREG (mode, new_reg, 0);
  1685. else
  1686. new_reg = gen_lowpart_SUBREG (mode, new_reg);
  1687. }
  1688. *loc = new_reg;
  1689. return true;
  1690. }
  1691. /* Scan all the operand sub-expressions. */
  1692. fmt = GET_RTX_FORMAT (code);
  1693. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1694. {
  1695. if (fmt[i] == 'e')
  1696. {
  1697. if (lra_substitute_pseudo (&XEXP (x, i), old_regno, new_reg))
  1698. result = true;
  1699. }
  1700. else if (fmt[i] == 'E')
  1701. {
  1702. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1703. if (lra_substitute_pseudo (&XVECEXP (x, i, j), old_regno, new_reg))
  1704. result = true;
  1705. }
  1706. }
  1707. return result;
  1708. }
  1709. /* Call lra_substitute_pseudo within an insn. This won't update the insn ptr,
  1710. just the contents of the insn. */
  1711. bool
  1712. lra_substitute_pseudo_within_insn (rtx_insn *insn, int old_regno, rtx new_reg)
  1713. {
  1714. rtx loc = insn;
  1715. return lra_substitute_pseudo (&loc, old_regno, new_reg);
  1716. }
  1717. /* This page contains code dealing with scratches (changing them onto
  1718. pseudos and restoring them from the pseudos).
  1719. We change scratches into pseudos at the beginning of LRA to
  1720. simplify dealing with them (conflicts, hard register assignments).
  1721. If the pseudo denoting scratch was spilled it means that we do need
  1722. a hard register for it. Such pseudos are transformed back to
  1723. scratches at the end of LRA. */
  1724. /* Description of location of a former scratch operand. */
  1725. struct sloc
  1726. {
  1727. rtx_insn *insn; /* Insn where the scratch was. */
  1728. int nop; /* Number of the operand which was a scratch. */
  1729. };
  1730. typedef struct sloc *sloc_t;
  1731. /* Locations of the former scratches. */
  1732. static vec<sloc_t> scratches;
  1733. /* Bitmap of scratch regnos. */
  1734. static bitmap_head scratch_bitmap;
  1735. /* Bitmap of scratch operands. */
  1736. static bitmap_head scratch_operand_bitmap;
  1737. /* Return true if pseudo REGNO is made of SCRATCH. */
  1738. bool
  1739. lra_former_scratch_p (int regno)
  1740. {
  1741. return bitmap_bit_p (&scratch_bitmap, regno);
  1742. }
  1743. /* Return true if the operand NOP of INSN is a former scratch. */
  1744. bool
  1745. lra_former_scratch_operand_p (rtx_insn *insn, int nop)
  1746. {
  1747. return bitmap_bit_p (&scratch_operand_bitmap,
  1748. INSN_UID (insn) * MAX_RECOG_OPERANDS + nop) != 0;
  1749. }
  1750. /* Register operand NOP in INSN as a former scratch. It will be
  1751. changed to scratch back, if it is necessary, at the LRA end. */
  1752. void
  1753. lra_register_new_scratch_op (rtx_insn *insn, int nop)
  1754. {
  1755. lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
  1756. rtx op = *id->operand_loc[nop];
  1757. sloc_t loc = XNEW (struct sloc);
  1758. lra_assert (REG_P (op));
  1759. loc->insn = insn;
  1760. loc->nop = nop;
  1761. scratches.safe_push (loc);
  1762. bitmap_set_bit (&scratch_bitmap, REGNO (op));
  1763. bitmap_set_bit (&scratch_operand_bitmap,
  1764. INSN_UID (insn) * MAX_RECOG_OPERANDS + nop);
  1765. add_reg_note (insn, REG_UNUSED, op);
  1766. }
  1767. /* Change scratches onto pseudos and save their location. */
  1768. static void
  1769. remove_scratches (void)
  1770. {
  1771. int i;
  1772. bool insn_changed_p;
  1773. basic_block bb;
  1774. rtx_insn *insn;
  1775. rtx reg;
  1776. lra_insn_recog_data_t id;
  1777. struct lra_static_insn_data *static_id;
  1778. scratches.create (get_max_uid ());
  1779. bitmap_initialize (&scratch_bitmap, &reg_obstack);
  1780. bitmap_initialize (&scratch_operand_bitmap, &reg_obstack);
  1781. FOR_EACH_BB_FN (bb, cfun)
  1782. FOR_BB_INSNS (bb, insn)
  1783. if (INSN_P (insn))
  1784. {
  1785. id = lra_get_insn_recog_data (insn);
  1786. static_id = id->insn_static_data;
  1787. insn_changed_p = false;
  1788. for (i = 0; i < static_id->n_operands; i++)
  1789. if (GET_CODE (*id->operand_loc[i]) == SCRATCH
  1790. && GET_MODE (*id->operand_loc[i]) != VOIDmode)
  1791. {
  1792. insn_changed_p = true;
  1793. *id->operand_loc[i] = reg
  1794. = lra_create_new_reg (static_id->operand[i].mode,
  1795. *id->operand_loc[i], ALL_REGS, NULL);
  1796. lra_register_new_scratch_op (insn, i);
  1797. if (lra_dump_file != NULL)
  1798. fprintf (lra_dump_file,
  1799. "Removing SCRATCH in insn #%u (nop %d)\n",
  1800. INSN_UID (insn), i);
  1801. }
  1802. if (insn_changed_p)
  1803. /* Because we might use DF right after caller-saves sub-pass
  1804. we need to keep DF info up to date. */
  1805. df_insn_rescan (insn);
  1806. }
  1807. }
  1808. /* Changes pseudos created by function remove_scratches onto scratches. */
  1809. static void
  1810. restore_scratches (void)
  1811. {
  1812. int regno;
  1813. unsigned i;
  1814. sloc_t loc;
  1815. rtx_insn *last = NULL;
  1816. lra_insn_recog_data_t id = NULL;
  1817. for (i = 0; scratches.iterate (i, &loc); i++)
  1818. {
  1819. if (last != loc->insn)
  1820. {
  1821. last = loc->insn;
  1822. id = lra_get_insn_recog_data (last);
  1823. }
  1824. if (REG_P (*id->operand_loc[loc->nop])
  1825. && ((regno = REGNO (*id->operand_loc[loc->nop]))
  1826. >= FIRST_PSEUDO_REGISTER)
  1827. && lra_get_regno_hard_regno (regno) < 0)
  1828. {
  1829. /* It should be only case when scratch register with chosen
  1830. constraint 'X' did not get memory or hard register. */
  1831. lra_assert (lra_former_scratch_p (regno));
  1832. *id->operand_loc[loc->nop]
  1833. = gen_rtx_SCRATCH (GET_MODE (*id->operand_loc[loc->nop]));
  1834. lra_update_dup (id, loc->nop);
  1835. if (lra_dump_file != NULL)
  1836. fprintf (lra_dump_file, "Restoring SCRATCH in insn #%u(nop %d)\n",
  1837. INSN_UID (loc->insn), loc->nop);
  1838. }
  1839. }
  1840. for (i = 0; scratches.iterate (i, &loc); i++)
  1841. free (loc);
  1842. scratches.release ();
  1843. bitmap_clear (&scratch_bitmap);
  1844. bitmap_clear (&scratch_operand_bitmap);
  1845. }
  1846. #ifdef ENABLE_CHECKING
  1847. /* Function checks RTL for correctness. If FINAL_P is true, it is
  1848. done at the end of LRA and the check is more rigorous. */
  1849. static void
  1850. check_rtl (bool final_p)
  1851. {
  1852. basic_block bb;
  1853. rtx_insn *insn;
  1854. lra_assert (! final_p || reload_completed);
  1855. FOR_EACH_BB_FN (bb, cfun)
  1856. FOR_BB_INSNS (bb, insn)
  1857. if (NONDEBUG_INSN_P (insn)
  1858. && GET_CODE (PATTERN (insn)) != USE
  1859. && GET_CODE (PATTERN (insn)) != CLOBBER
  1860. && GET_CODE (PATTERN (insn)) != ASM_INPUT)
  1861. {
  1862. if (final_p)
  1863. {
  1864. #ifdef ENABLED_CHECKING
  1865. extract_constrain_insn (insn);
  1866. #endif
  1867. continue;
  1868. }
  1869. /* LRA code is based on assumption that all addresses can be
  1870. correctly decomposed. LRA can generate reloads for
  1871. decomposable addresses. The decomposition code checks the
  1872. correctness of the addresses. So we don't need to check
  1873. the addresses here. Don't call insn_invalid_p here, it can
  1874. change the code at this stage. */
  1875. if (recog_memoized (insn) < 0 && asm_noperands (PATTERN (insn)) < 0)
  1876. fatal_insn_not_found (insn);
  1877. }
  1878. }
  1879. #endif /* #ifdef ENABLE_CHECKING */
  1880. /* Determine if the current function has an exception receiver block
  1881. that reaches the exit block via non-exceptional edges */
  1882. static bool
  1883. has_nonexceptional_receiver (void)
  1884. {
  1885. edge e;
  1886. edge_iterator ei;
  1887. basic_block *tos, *worklist, bb;
  1888. /* If we're not optimizing, then just err on the safe side. */
  1889. if (!optimize)
  1890. return true;
  1891. /* First determine which blocks can reach exit via normal paths. */
  1892. tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
  1893. FOR_EACH_BB_FN (bb, cfun)
  1894. bb->flags &= ~BB_REACHABLE;
  1895. /* Place the exit block on our worklist. */
  1896. EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_REACHABLE;
  1897. *tos++ = EXIT_BLOCK_PTR_FOR_FN (cfun);
  1898. /* Iterate: find everything reachable from what we've already seen. */
  1899. while (tos != worklist)
  1900. {
  1901. bb = *--tos;
  1902. FOR_EACH_EDGE (e, ei, bb->preds)
  1903. if (e->flags & EDGE_ABNORMAL)
  1904. {
  1905. free (worklist);
  1906. return true;
  1907. }
  1908. else
  1909. {
  1910. basic_block src = e->src;
  1911. if (!(src->flags & BB_REACHABLE))
  1912. {
  1913. src->flags |= BB_REACHABLE;
  1914. *tos++ = src;
  1915. }
  1916. }
  1917. }
  1918. free (worklist);
  1919. /* No exceptional block reached exit unexceptionally. */
  1920. return false;
  1921. }
  1922. #ifdef AUTO_INC_DEC
  1923. /* Process recursively X of INSN and add REG_INC notes if necessary. */
  1924. static void
  1925. add_auto_inc_notes (rtx_insn *insn, rtx x)
  1926. {
  1927. enum rtx_code code = GET_CODE (x);
  1928. const char *fmt;
  1929. int i, j;
  1930. if (code == MEM && auto_inc_p (XEXP (x, 0)))
  1931. {
  1932. add_reg_note (insn, REG_INC, XEXP (XEXP (x, 0), 0));
  1933. return;
  1934. }
  1935. /* Scan all X sub-expressions. */
  1936. fmt = GET_RTX_FORMAT (code);
  1937. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1938. {
  1939. if (fmt[i] == 'e')
  1940. add_auto_inc_notes (insn, XEXP (x, i));
  1941. else if (fmt[i] == 'E')
  1942. for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  1943. add_auto_inc_notes (insn, XVECEXP (x, i, j));
  1944. }
  1945. }
  1946. #endif
  1947. /* Remove all REG_DEAD and REG_UNUSED notes and regenerate REG_INC.
  1948. We change pseudos by hard registers without notification of DF and
  1949. that can make the notes obsolete. DF-infrastructure does not deal
  1950. with REG_INC notes -- so we should regenerate them here. */
  1951. static void
  1952. update_inc_notes (void)
  1953. {
  1954. rtx *pnote;
  1955. basic_block bb;
  1956. rtx_insn *insn;
  1957. FOR_EACH_BB_FN (bb, cfun)
  1958. FOR_BB_INSNS (bb, insn)
  1959. if (NONDEBUG_INSN_P (insn))
  1960. {
  1961. pnote = &REG_NOTES (insn);
  1962. while (*pnote != 0)
  1963. {
  1964. if (REG_NOTE_KIND (*pnote) == REG_DEAD
  1965. || REG_NOTE_KIND (*pnote) == REG_UNUSED
  1966. || REG_NOTE_KIND (*pnote) == REG_INC)
  1967. *pnote = XEXP (*pnote, 1);
  1968. else
  1969. pnote = &XEXP (*pnote, 1);
  1970. }
  1971. #ifdef AUTO_INC_DEC
  1972. add_auto_inc_notes (insn, PATTERN (insn));
  1973. #endif
  1974. }
  1975. }
  1976. /* Set to 1 while in lra. */
  1977. int lra_in_progress;
  1978. /* Start of pseudo regnos before the LRA. */
  1979. int lra_new_regno_start;
  1980. /* Start of reload pseudo regnos before the new spill pass. */
  1981. int lra_constraint_new_regno_start;
  1982. /* Avoid spilling pseudos with regno more than the following value if
  1983. it is possible. */
  1984. int lra_bad_spill_regno_start;
  1985. /* Inheritance pseudo regnos before the new spill pass. */
  1986. bitmap_head lra_inheritance_pseudos;
  1987. /* Split regnos before the new spill pass. */
  1988. bitmap_head lra_split_regs;
  1989. /* Reload pseudo regnos before the new assignmnet pass which still can
  1990. be spilled after the assinment pass as memory is also accepted in
  1991. insns for the reload pseudos. */
  1992. bitmap_head lra_optional_reload_pseudos;
  1993. /* Pseudo regnos used for subreg reloads before the new assignment
  1994. pass. Such pseudos still can be spilled after the assinment
  1995. pass. */
  1996. bitmap_head lra_subreg_reload_pseudos;
  1997. /* File used for output of LRA debug information. */
  1998. FILE *lra_dump_file;
  1999. /* True if we should try spill into registers of different classes
  2000. instead of memory. */
  2001. bool lra_reg_spill_p;
  2002. /* Set up value LRA_REG_SPILL_P. */
  2003. static void
  2004. setup_reg_spill_flag (void)
  2005. {
  2006. int cl, mode;
  2007. if (targetm.spill_class != NULL)
  2008. for (cl = 0; cl < (int) LIM_REG_CLASSES; cl++)
  2009. for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
  2010. if (targetm.spill_class ((enum reg_class) cl,
  2011. (machine_mode) mode) != NO_REGS)
  2012. {
  2013. lra_reg_spill_p = true;
  2014. return;
  2015. }
  2016. lra_reg_spill_p = false;
  2017. }
  2018. /* True if the current function is too big to use regular algorithms
  2019. in LRA. In other words, we should use simpler and faster algorithms
  2020. in LRA. It also means we should not worry about generation code
  2021. for caller saves. The value is set up in IRA. */
  2022. bool lra_simple_p;
  2023. /* Major LRA entry function. F is a file should be used to dump LRA
  2024. debug info. */
  2025. void
  2026. lra (FILE *f)
  2027. {
  2028. int i;
  2029. bool live_p, scratch_p, inserted_p;
  2030. lra_dump_file = f;
  2031. timevar_push (TV_LRA);
  2032. /* Make sure that the last insn is a note. Some subsequent passes
  2033. need it. */
  2034. emit_note (NOTE_INSN_DELETED);
  2035. COPY_HARD_REG_SET (lra_no_alloc_regs, ira_no_alloc_regs);
  2036. init_reg_info ();
  2037. expand_reg_info ();
  2038. init_insn_recog_data ();
  2039. #ifdef ENABLE_CHECKING
  2040. /* Some quick check on RTL generated by previous passes. */
  2041. check_rtl (false);
  2042. #endif
  2043. lra_in_progress = 1;
  2044. lra_live_range_iter = lra_coalesce_iter = lra_constraint_iter = 0;
  2045. lra_assignment_iter = lra_assignment_iter_after_spill = 0;
  2046. lra_inheritance_iter = lra_undo_inheritance_iter = 0;
  2047. lra_rematerialization_iter = 0;
  2048. setup_reg_spill_flag ();
  2049. /* Function remove_scratches can creates new pseudos for clobbers --
  2050. so set up lra_constraint_new_regno_start before its call to
  2051. permit changing reg classes for pseudos created by this
  2052. simplification. */
  2053. lra_constraint_new_regno_start = lra_new_regno_start = max_reg_num ();
  2054. lra_bad_spill_regno_start = INT_MAX;
  2055. remove_scratches ();
  2056. scratch_p = lra_constraint_new_regno_start != max_reg_num ();
  2057. /* A function that has a non-local label that can reach the exit
  2058. block via non-exceptional paths must save all call-saved
  2059. registers. */
  2060. if (cfun->has_nonlocal_label && has_nonexceptional_receiver ())
  2061. crtl->saves_all_registers = 1;
  2062. if (crtl->saves_all_registers)
  2063. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  2064. if (! call_used_regs[i] && ! fixed_regs[i] && ! LOCAL_REGNO (i))
  2065. df_set_regs_ever_live (i, true);
  2066. /* We don't DF from now and avoid its using because it is to
  2067. expensive when a lot of RTL changes are made. */
  2068. df_set_flags (DF_NO_INSN_RESCAN);
  2069. lra_constraint_insn_stack.create (get_max_uid ());
  2070. lra_constraint_insn_stack_bitmap = sbitmap_alloc (get_max_uid ());
  2071. bitmap_clear (lra_constraint_insn_stack_bitmap);
  2072. lra_live_ranges_init ();
  2073. lra_constraints_init ();
  2074. lra_curr_reload_num = 0;
  2075. push_insns (get_last_insn (), NULL);
  2076. /* It is needed for the 1st coalescing. */
  2077. bitmap_initialize (&lra_inheritance_pseudos, &reg_obstack);
  2078. bitmap_initialize (&lra_split_regs, &reg_obstack);
  2079. bitmap_initialize (&lra_optional_reload_pseudos, &reg_obstack);
  2080. bitmap_initialize (&lra_subreg_reload_pseudos, &reg_obstack);
  2081. live_p = false;
  2082. if (get_frame_size () != 0 && crtl->stack_alignment_needed)
  2083. /* If we have a stack frame, we must align it now. The stack size
  2084. may be a part of the offset computation for register
  2085. elimination. */
  2086. assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
  2087. lra_init_equiv ();
  2088. for (;;)
  2089. {
  2090. for (;;)
  2091. {
  2092. /* We should try to assign hard registers to scratches even
  2093. if there were no RTL transformations in
  2094. lra_constraints. */
  2095. if (! lra_constraints (lra_constraint_iter == 0)
  2096. && (lra_constraint_iter > 1
  2097. || (! scratch_p && ! caller_save_needed)))
  2098. break;
  2099. /* Constraint transformations may result in that eliminable
  2100. hard regs become uneliminable and pseudos which use them
  2101. should be spilled. It is better to do it before pseudo
  2102. assignments.
  2103. For example, rs6000 can make
  2104. RS6000_PIC_OFFSET_TABLE_REGNUM uneliminable if we started
  2105. to use a constant pool. */
  2106. lra_eliminate (false, false);
  2107. /* Do inheritance only for regular algorithms. */
  2108. if (! lra_simple_p)
  2109. {
  2110. if (flag_ipa_ra)
  2111. {
  2112. if (live_p)
  2113. lra_clear_live_ranges ();
  2114. /* As a side-effect of lra_create_live_ranges, we calculate
  2115. actual_call_used_reg_set, which is needed during
  2116. lra_inheritance. */
  2117. lra_create_live_ranges (true, true);
  2118. live_p = true;
  2119. }
  2120. lra_inheritance ();
  2121. }
  2122. if (live_p)
  2123. lra_clear_live_ranges ();
  2124. /* We need live ranges for lra_assign -- so build them. But
  2125. don't remove dead insns or change global live info as we
  2126. can undo inheritance transformations after inheritance
  2127. pseudo assigning. */
  2128. lra_create_live_ranges (true, false);
  2129. live_p = true;
  2130. /* If we don't spill non-reload and non-inheritance pseudos,
  2131. there is no sense to run memory-memory move coalescing.
  2132. If inheritance pseudos were spilled, the memory-memory
  2133. moves involving them will be removed by pass undoing
  2134. inheritance. */
  2135. if (lra_simple_p)
  2136. lra_assign ();
  2137. else
  2138. {
  2139. bool spill_p = !lra_assign ();
  2140. if (lra_undo_inheritance ())
  2141. live_p = false;
  2142. if (spill_p)
  2143. {
  2144. if (! live_p)
  2145. {
  2146. lra_create_live_ranges (true, true);
  2147. live_p = true;
  2148. }
  2149. if (lra_coalesce ())
  2150. live_p = false;
  2151. }
  2152. if (! live_p)
  2153. lra_clear_live_ranges ();
  2154. }
  2155. }
  2156. /* Don't clear optional reloads bitmap until all constraints are
  2157. satisfied as we need to differ them from regular reloads. */
  2158. bitmap_clear (&lra_optional_reload_pseudos);
  2159. bitmap_clear (&lra_subreg_reload_pseudos);
  2160. bitmap_clear (&lra_inheritance_pseudos);
  2161. bitmap_clear (&lra_split_regs);
  2162. if (! live_p)
  2163. {
  2164. /* We need full live info for spilling pseudos into
  2165. registers instead of memory. */
  2166. lra_create_live_ranges (lra_reg_spill_p, true);
  2167. live_p = true;
  2168. }
  2169. /* We should check necessity for spilling here as the above live
  2170. range pass can remove spilled pseudos. */
  2171. if (! lra_need_for_spills_p ())
  2172. break;
  2173. /* Now we know what pseudos should be spilled. Try to
  2174. rematerialize them first. */
  2175. if (lra_remat ())
  2176. {
  2177. /* We need full live info -- see the comment above. */
  2178. lra_create_live_ranges (lra_reg_spill_p, true);
  2179. live_p = true;
  2180. if (! lra_need_for_spills_p ())
  2181. break;
  2182. }
  2183. lra_spill ();
  2184. /* Assignment of stack slots changes elimination offsets for
  2185. some eliminations. So update the offsets here. */
  2186. lra_eliminate (false, false);
  2187. lra_constraint_new_regno_start = max_reg_num ();
  2188. if (lra_bad_spill_regno_start == INT_MAX
  2189. && lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES
  2190. && lra_rematerialization_iter > LRA_MAX_REMATERIALIZATION_PASSES)
  2191. /* After switching off inheritance and rematerialization
  2192. passes, avoid spilling reload pseudos will be created to
  2193. prevent LRA cycling in some complicated cases. */
  2194. lra_bad_spill_regno_start = lra_constraint_new_regno_start;
  2195. lra_assignment_iter_after_spill = 0;
  2196. }
  2197. restore_scratches ();
  2198. lra_eliminate (true, false);
  2199. lra_final_code_change ();
  2200. lra_in_progress = 0;
  2201. if (live_p)
  2202. lra_clear_live_ranges ();
  2203. lra_live_ranges_finish ();
  2204. lra_constraints_finish ();
  2205. finish_reg_info ();
  2206. sbitmap_free (lra_constraint_insn_stack_bitmap);
  2207. lra_constraint_insn_stack.release ();
  2208. finish_insn_recog_data ();
  2209. regstat_free_n_sets_and_refs ();
  2210. regstat_free_ri ();
  2211. reload_completed = 1;
  2212. update_inc_notes ();
  2213. inserted_p = fixup_abnormal_edges ();
  2214. /* We've possibly turned single trapping insn into multiple ones. */
  2215. if (cfun->can_throw_non_call_exceptions)
  2216. {
  2217. sbitmap blocks;
  2218. blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
  2219. bitmap_ones (blocks);
  2220. find_many_sub_basic_blocks (blocks);
  2221. sbitmap_free (blocks);
  2222. }
  2223. if (inserted_p)
  2224. commit_edge_insertions ();
  2225. /* Replacing pseudos with their memory equivalents might have
  2226. created shared rtx. Subsequent passes would get confused
  2227. by this, so unshare everything here. */
  2228. unshare_all_rtl_again (get_insns ());
  2229. #ifdef ENABLE_CHECKING
  2230. check_rtl (true);
  2231. #endif
  2232. timevar_pop (TV_LRA);
  2233. }
  2234. /* Called once per compiler to initialize LRA data once. */
  2235. void
  2236. lra_init_once (void)
  2237. {
  2238. init_insn_code_data_once ();
  2239. }
  2240. /* Called once per compiler to finish LRA data which are initialize
  2241. once. */
  2242. void
  2243. lra_finish_once (void)
  2244. {
  2245. finish_insn_code_data_once ();
  2246. }