lra-assigns.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /* Assign reload pseudos.
  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. /* This file's main objective is to assign hard registers to reload
  17. pseudos. It also tries to allocate hard registers to other
  18. pseudos, but at a lower priority than the reload pseudos. The pass
  19. does not transform the RTL.
  20. We must allocate a hard register to every reload pseudo. We try to
  21. increase the chances of finding a viable allocation by assigning
  22. the pseudos in order of fewest available hard registers first. If
  23. we still fail to find a hard register, we spill other (non-reload)
  24. pseudos in order to make room.
  25. find_hard_regno_for finds hard registers for allocation without
  26. spilling. spill_for does the same with spilling. Both functions
  27. use a cost model to determine the most profitable choice of hard
  28. and spill registers.
  29. Once we have finished allocating reload pseudos, we also try to
  30. assign registers to other (non-reload) pseudos. This is useful if
  31. hard registers were freed up by the spilling just described.
  32. We try to assign hard registers by collecting pseudos into threads.
  33. These threads contain reload and inheritance pseudos that are
  34. connected by copies (move insns). Doing this improves the chances
  35. of pseudos in the thread getting the same hard register and, as a
  36. result, of allowing some move insns to be deleted.
  37. When we assign a hard register to a pseudo, we decrease the cost of
  38. using the same hard register for pseudos that are connected by
  39. copies.
  40. If two hard registers have the same frequency-derived cost, we
  41. prefer hard registers with higher priorities. The mapping of
  42. registers to priorities is controlled by the register_priority
  43. target hook. For example, x86-64 has a few register priorities:
  44. hard registers with and without REX prefixes have different
  45. priorities. This permits us to generate smaller code as insns
  46. without REX prefixes are shorter.
  47. If a few hard registers are still equally good for the assignment,
  48. we choose the least used hard register. It is called leveling and
  49. may be profitable for some targets.
  50. Only insns with changed allocation pseudos are processed on the
  51. next constraint pass.
  52. The pseudo live-ranges are used to find conflicting pseudos.
  53. For understanding the code, it is important to keep in mind that
  54. inheritance, split, and reload pseudos created since last
  55. constraint pass have regno >= lra_constraint_new_regno_start.
  56. Inheritance and split pseudos created on any pass are in the
  57. corresponding bitmaps. Inheritance and split pseudos since the
  58. last constraint pass have also the corresponding non-negative
  59. restore_regno. */
  60. #include "config.h"
  61. #include "system.h"
  62. #include "coretypes.h"
  63. #include "tm.h"
  64. #include "hard-reg-set.h"
  65. #include "rtl.h"
  66. #include "rtl-error.h"
  67. #include "tm_p.h"
  68. #include "target.h"
  69. #include "insn-config.h"
  70. #include "recog.h"
  71. #include "output.h"
  72. #include "regs.h"
  73. #include "hashtab.h"
  74. #include "hash-set.h"
  75. #include "vec.h"
  76. #include "machmode.h"
  77. #include "input.h"
  78. #include "function.h"
  79. #include "symtab.h"
  80. #include "flags.h"
  81. #include "statistics.h"
  82. #include "double-int.h"
  83. #include "real.h"
  84. #include "fixed-value.h"
  85. #include "alias.h"
  86. #include "wide-int.h"
  87. #include "inchash.h"
  88. #include "tree.h"
  89. #include "expmed.h"
  90. #include "dojump.h"
  91. #include "explow.h"
  92. #include "calls.h"
  93. #include "emit-rtl.h"
  94. #include "varasm.h"
  95. #include "stmt.h"
  96. #include "expr.h"
  97. #include "predict.h"
  98. #include "dominance.h"
  99. #include "cfg.h"
  100. #include "basic-block.h"
  101. #include "except.h"
  102. #include "df.h"
  103. #include "ira.h"
  104. #include "sparseset.h"
  105. #include "params.h"
  106. #include "lra-int.h"
  107. /* Current iteration number of the pass and current iteration number
  108. of the pass after the latest spill pass when any former reload
  109. pseudo was spilled. */
  110. int lra_assignment_iter;
  111. int lra_assignment_iter_after_spill;
  112. /* Flag of spilling former reload pseudos on this pass. */
  113. static bool former_reload_pseudo_spill_p;
  114. /* Array containing corresponding values of function
  115. lra_get_allocno_class. It is used to speed up the code. */
  116. static enum reg_class *regno_allocno_class_array;
  117. /* Information about the thread to which a pseudo belongs. Threads are
  118. a set of connected reload and inheritance pseudos with the same set of
  119. available hard registers. Lone registers belong to their own threads. */
  120. struct regno_assign_info
  121. {
  122. /* First/next pseudo of the same thread. */
  123. int first, next;
  124. /* Frequency of the thread (execution frequency of only reload
  125. pseudos in the thread when the thread contains a reload pseudo).
  126. Defined only for the first thread pseudo. */
  127. int freq;
  128. };
  129. /* Map regno to the corresponding regno assignment info. */
  130. static struct regno_assign_info *regno_assign_info;
  131. /* All inherited, subreg or optional pseudos created before last spill
  132. sub-pass. Such pseudos are permitted to get memory instead of hard
  133. regs. */
  134. static bitmap_head non_reload_pseudos;
  135. /* Process a pseudo copy with execution frequency COPY_FREQ connecting
  136. REGNO1 and REGNO2 to form threads. */
  137. static void
  138. process_copy_to_form_thread (int regno1, int regno2, int copy_freq)
  139. {
  140. int last, regno1_first, regno2_first;
  141. lra_assert (regno1 >= lra_constraint_new_regno_start
  142. && regno2 >= lra_constraint_new_regno_start);
  143. regno1_first = regno_assign_info[regno1].first;
  144. regno2_first = regno_assign_info[regno2].first;
  145. if (regno1_first != regno2_first)
  146. {
  147. for (last = regno2_first;
  148. regno_assign_info[last].next >= 0;
  149. last = regno_assign_info[last].next)
  150. regno_assign_info[last].first = regno1_first;
  151. regno_assign_info[last].first = regno1_first;
  152. regno_assign_info[last].next = regno_assign_info[regno1_first].next;
  153. regno_assign_info[regno1_first].next = regno2_first;
  154. regno_assign_info[regno1_first].freq
  155. += regno_assign_info[regno2_first].freq;
  156. }
  157. regno_assign_info[regno1_first].freq -= 2 * copy_freq;
  158. lra_assert (regno_assign_info[regno1_first].freq >= 0);
  159. }
  160. /* Initialize REGNO_ASSIGN_INFO and form threads. */
  161. static void
  162. init_regno_assign_info (void)
  163. {
  164. int i, regno1, regno2, max_regno = max_reg_num ();
  165. lra_copy_t cp;
  166. regno_assign_info = XNEWVEC (struct regno_assign_info, max_regno);
  167. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  168. {
  169. regno_assign_info[i].first = i;
  170. regno_assign_info[i].next = -1;
  171. regno_assign_info[i].freq = lra_reg_info[i].freq;
  172. }
  173. /* Form the threads. */
  174. for (i = 0; (cp = lra_get_copy (i)) != NULL; i++)
  175. if ((regno1 = cp->regno1) >= lra_constraint_new_regno_start
  176. && (regno2 = cp->regno2) >= lra_constraint_new_regno_start
  177. && reg_renumber[regno1] < 0 && lra_reg_info[regno1].nrefs != 0
  178. && reg_renumber[regno2] < 0 && lra_reg_info[regno2].nrefs != 0
  179. && (ira_class_hard_regs_num[regno_allocno_class_array[regno1]]
  180. == ira_class_hard_regs_num[regno_allocno_class_array[regno2]]))
  181. process_copy_to_form_thread (regno1, regno2, cp->freq);
  182. }
  183. /* Free REGNO_ASSIGN_INFO. */
  184. static void
  185. finish_regno_assign_info (void)
  186. {
  187. free (regno_assign_info);
  188. }
  189. /* The function is used to sort *reload* and *inheritance* pseudos to
  190. try to assign them hard registers. We put pseudos from the same
  191. thread always nearby. */
  192. static int
  193. reload_pseudo_compare_func (const void *v1p, const void *v2p)
  194. {
  195. int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
  196. enum reg_class cl1 = regno_allocno_class_array[r1];
  197. enum reg_class cl2 = regno_allocno_class_array[r2];
  198. int diff;
  199. lra_assert (r1 >= lra_constraint_new_regno_start
  200. && r2 >= lra_constraint_new_regno_start);
  201. /* Prefer to assign reload registers with smaller classes first to
  202. guarantee assignment to all reload registers. */
  203. if ((diff = (ira_class_hard_regs_num[cl1]
  204. - ira_class_hard_regs_num[cl2])) != 0)
  205. return diff;
  206. if ((diff
  207. = (ira_reg_class_max_nregs[cl2][lra_reg_info[r2].biggest_mode]
  208. - ira_reg_class_max_nregs[cl1][lra_reg_info[r1].biggest_mode])) != 0
  209. /* The code below executes rarely as nregs == 1 in most cases.
  210. So we should not worry about using faster data structures to
  211. check reload pseudos. */
  212. && ! bitmap_bit_p (&non_reload_pseudos, r1)
  213. && ! bitmap_bit_p (&non_reload_pseudos, r2))
  214. return diff;
  215. if ((diff = (regno_assign_info[regno_assign_info[r2].first].freq
  216. - regno_assign_info[regno_assign_info[r1].first].freq)) != 0)
  217. return diff;
  218. /* Allocate bigger pseudos first to avoid register file
  219. fragmentation. */
  220. if ((diff
  221. = (ira_reg_class_max_nregs[cl2][lra_reg_info[r2].biggest_mode]
  222. - ira_reg_class_max_nregs[cl1][lra_reg_info[r1].biggest_mode])) != 0)
  223. return diff;
  224. /* Put pseudos from the thread nearby. */
  225. if ((diff = regno_assign_info[r1].first - regno_assign_info[r2].first) != 0)
  226. return diff;
  227. /* If regs are equally good, sort by their numbers, so that the
  228. results of qsort leave nothing to chance. */
  229. return r1 - r2;
  230. }
  231. /* The function is used to sort *non-reload* pseudos to try to assign
  232. them hard registers. The order calculation is simpler than in the
  233. previous function and based on the pseudo frequency usage. */
  234. static int
  235. pseudo_compare_func (const void *v1p, const void *v2p)
  236. {
  237. int r1 = *(const int *) v1p, r2 = *(const int *) v2p;
  238. int diff;
  239. /* Prefer to assign more frequently used registers first. */
  240. if ((diff = lra_reg_info[r2].freq - lra_reg_info[r1].freq) != 0)
  241. return diff;
  242. /* If regs are equally good, sort by their numbers, so that the
  243. results of qsort leave nothing to chance. */
  244. return r1 - r2;
  245. }
  246. /* Arrays of size LRA_LIVE_MAX_POINT mapping a program point to the
  247. pseudo live ranges with given start point. We insert only live
  248. ranges of pseudos interesting for assignment purposes. They are
  249. reload pseudos and pseudos assigned to hard registers. */
  250. static lra_live_range_t *start_point_ranges;
  251. /* Used as a flag that a live range is not inserted in the start point
  252. chain. */
  253. static struct lra_live_range not_in_chain_mark;
  254. /* Create and set up START_POINT_RANGES. */
  255. static void
  256. create_live_range_start_chains (void)
  257. {
  258. int i, max_regno;
  259. lra_live_range_t r;
  260. start_point_ranges = XCNEWVEC (lra_live_range_t, lra_live_max_point);
  261. max_regno = max_reg_num ();
  262. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  263. if (i >= lra_constraint_new_regno_start || reg_renumber[i] >= 0)
  264. {
  265. for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
  266. {
  267. r->start_next = start_point_ranges[r->start];
  268. start_point_ranges[r->start] = r;
  269. }
  270. }
  271. else
  272. {
  273. for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
  274. r->start_next = &not_in_chain_mark;
  275. }
  276. }
  277. /* Insert live ranges of pseudo REGNO into start chains if they are
  278. not there yet. */
  279. static void
  280. insert_in_live_range_start_chain (int regno)
  281. {
  282. lra_live_range_t r = lra_reg_info[regno].live_ranges;
  283. if (r->start_next != &not_in_chain_mark)
  284. return;
  285. for (; r != NULL; r = r->next)
  286. {
  287. r->start_next = start_point_ranges[r->start];
  288. start_point_ranges[r->start] = r;
  289. }
  290. }
  291. /* Free START_POINT_RANGES. */
  292. static void
  293. finish_live_range_start_chains (void)
  294. {
  295. gcc_assert (start_point_ranges != NULL);
  296. free (start_point_ranges);
  297. start_point_ranges = NULL;
  298. }
  299. /* Map: program point -> bitmap of all pseudos living at the point and
  300. assigned to hard registers. */
  301. static bitmap_head *live_hard_reg_pseudos;
  302. static bitmap_obstack live_hard_reg_pseudos_bitmap_obstack;
  303. /* reg_renumber corresponding to pseudos marked in
  304. live_hard_reg_pseudos. reg_renumber might be not matched to
  305. live_hard_reg_pseudos but live_pseudos_reg_renumber always reflects
  306. live_hard_reg_pseudos. */
  307. static int *live_pseudos_reg_renumber;
  308. /* Sparseset used to calculate living hard reg pseudos for some program
  309. point range. */
  310. static sparseset live_range_hard_reg_pseudos;
  311. /* Sparseset used to calculate living reload/inheritance pseudos for
  312. some program point range. */
  313. static sparseset live_range_reload_inheritance_pseudos;
  314. /* Allocate and initialize the data about living pseudos at program
  315. points. */
  316. static void
  317. init_lives (void)
  318. {
  319. int i, max_regno = max_reg_num ();
  320. live_range_hard_reg_pseudos = sparseset_alloc (max_regno);
  321. live_range_reload_inheritance_pseudos = sparseset_alloc (max_regno);
  322. live_hard_reg_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
  323. bitmap_obstack_initialize (&live_hard_reg_pseudos_bitmap_obstack);
  324. for (i = 0; i < lra_live_max_point; i++)
  325. bitmap_initialize (&live_hard_reg_pseudos[i],
  326. &live_hard_reg_pseudos_bitmap_obstack);
  327. live_pseudos_reg_renumber = XNEWVEC (int, max_regno);
  328. for (i = 0; i < max_regno; i++)
  329. live_pseudos_reg_renumber[i] = -1;
  330. }
  331. /* Free the data about living pseudos at program points. */
  332. static void
  333. finish_lives (void)
  334. {
  335. sparseset_free (live_range_hard_reg_pseudos);
  336. sparseset_free (live_range_reload_inheritance_pseudos);
  337. free (live_hard_reg_pseudos);
  338. bitmap_obstack_release (&live_hard_reg_pseudos_bitmap_obstack);
  339. free (live_pseudos_reg_renumber);
  340. }
  341. /* Update the LIVE_HARD_REG_PSEUDOS and LIVE_PSEUDOS_REG_RENUMBER
  342. entries for pseudo REGNO. Assume that the register has been
  343. spilled if FREE_P, otherwise assume that it has been assigned
  344. reg_renumber[REGNO] (if >= 0). We also insert the pseudo live
  345. ranges in the start chains when it is assumed to be assigned to a
  346. hard register because we use the chains of pseudos assigned to hard
  347. registers during allocation. */
  348. static void
  349. update_lives (int regno, bool free_p)
  350. {
  351. int p;
  352. lra_live_range_t r;
  353. if (reg_renumber[regno] < 0)
  354. return;
  355. live_pseudos_reg_renumber[regno] = free_p ? -1 : reg_renumber[regno];
  356. for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
  357. {
  358. for (p = r->start; p <= r->finish; p++)
  359. if (free_p)
  360. bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
  361. else
  362. {
  363. bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
  364. insert_in_live_range_start_chain (regno);
  365. }
  366. }
  367. }
  368. /* Sparseset used to calculate reload pseudos conflicting with a given
  369. pseudo when we are trying to find a hard register for the given
  370. pseudo. */
  371. static sparseset conflict_reload_and_inheritance_pseudos;
  372. /* Map: program point -> bitmap of all reload and inheritance pseudos
  373. living at the point. */
  374. static bitmap_head *live_reload_and_inheritance_pseudos;
  375. static bitmap_obstack live_reload_and_inheritance_pseudos_bitmap_obstack;
  376. /* Allocate and initialize data about living reload pseudos at any
  377. given program point. */
  378. static void
  379. init_live_reload_and_inheritance_pseudos (void)
  380. {
  381. int i, p, max_regno = max_reg_num ();
  382. lra_live_range_t r;
  383. conflict_reload_and_inheritance_pseudos = sparseset_alloc (max_regno);
  384. live_reload_and_inheritance_pseudos = XNEWVEC (bitmap_head, lra_live_max_point);
  385. bitmap_obstack_initialize (&live_reload_and_inheritance_pseudos_bitmap_obstack);
  386. for (p = 0; p < lra_live_max_point; p++)
  387. bitmap_initialize (&live_reload_and_inheritance_pseudos[p],
  388. &live_reload_and_inheritance_pseudos_bitmap_obstack);
  389. for (i = lra_constraint_new_regno_start; i < max_regno; i++)
  390. {
  391. for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
  392. for (p = r->start; p <= r->finish; p++)
  393. bitmap_set_bit (&live_reload_and_inheritance_pseudos[p], i);
  394. }
  395. }
  396. /* Finalize data about living reload pseudos at any given program
  397. point. */
  398. static void
  399. finish_live_reload_and_inheritance_pseudos (void)
  400. {
  401. sparseset_free (conflict_reload_and_inheritance_pseudos);
  402. free (live_reload_and_inheritance_pseudos);
  403. bitmap_obstack_release (&live_reload_and_inheritance_pseudos_bitmap_obstack);
  404. }
  405. /* The value used to check that cost of given hard reg is really
  406. defined currently. */
  407. static int curr_hard_regno_costs_check = 0;
  408. /* Array used to check that cost of the corresponding hard reg (the
  409. array element index) is really defined currently. */
  410. static int hard_regno_costs_check[FIRST_PSEUDO_REGISTER];
  411. /* The current costs of allocation of hard regs. Defined only if the
  412. value of the corresponding element of the previous array is equal to
  413. CURR_HARD_REGNO_COSTS_CHECK. */
  414. static int hard_regno_costs[FIRST_PSEUDO_REGISTER];
  415. /* Adjust cost of HARD_REGNO by INCR. Reset the cost first if it is
  416. not defined yet. */
  417. static inline void
  418. adjust_hard_regno_cost (int hard_regno, int incr)
  419. {
  420. if (hard_regno_costs_check[hard_regno] != curr_hard_regno_costs_check)
  421. hard_regno_costs[hard_regno] = 0;
  422. hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
  423. hard_regno_costs[hard_regno] += incr;
  424. }
  425. /* Try to find a free hard register for pseudo REGNO. Return the
  426. hard register on success and set *COST to the cost of using
  427. that register. (If several registers have equal cost, the one with
  428. the highest priority wins.) Return -1 on failure.
  429. If FIRST_P, return the first available hard reg ignoring other
  430. criteria, e.g. allocation cost. This approach results in less hard
  431. reg pool fragmentation and permit to allocate hard regs to reload
  432. pseudos in complicated situations where pseudo sizes are different.
  433. If TRY_ONLY_HARD_REGNO >= 0, consider only that hard register,
  434. otherwise consider all hard registers in REGNO's class.
  435. If REGNO_SET is not empty, only hard registers from the set are
  436. considered. */
  437. static int
  438. find_hard_regno_for_1 (int regno, int *cost, int try_only_hard_regno,
  439. bool first_p, HARD_REG_SET regno_set)
  440. {
  441. HARD_REG_SET conflict_set;
  442. int best_cost = INT_MAX, best_priority = INT_MIN, best_usage = INT_MAX;
  443. lra_live_range_t r;
  444. int p, i, j, rclass_size, best_hard_regno, priority, hard_regno;
  445. int hr, conflict_hr, nregs;
  446. machine_mode biggest_mode;
  447. unsigned int k, conflict_regno;
  448. int offset, val, biggest_nregs, nregs_diff;
  449. enum reg_class rclass;
  450. bitmap_iterator bi;
  451. bool *rclass_intersect_p;
  452. HARD_REG_SET impossible_start_hard_regs, available_regs;
  453. if (hard_reg_set_empty_p (regno_set))
  454. COPY_HARD_REG_SET (conflict_set, lra_no_alloc_regs);
  455. else
  456. {
  457. COMPL_HARD_REG_SET (conflict_set, regno_set);
  458. IOR_HARD_REG_SET (conflict_set, lra_no_alloc_regs);
  459. }
  460. rclass = regno_allocno_class_array[regno];
  461. rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
  462. curr_hard_regno_costs_check++;
  463. sparseset_clear (conflict_reload_and_inheritance_pseudos);
  464. sparseset_clear (live_range_hard_reg_pseudos);
  465. IOR_HARD_REG_SET (conflict_set, lra_reg_info[regno].conflict_hard_regs);
  466. biggest_mode = lra_reg_info[regno].biggest_mode;
  467. for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
  468. {
  469. EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
  470. if (rclass_intersect_p[regno_allocno_class_array[k]])
  471. sparseset_set_bit (live_range_hard_reg_pseudos, k);
  472. EXECUTE_IF_SET_IN_BITMAP (&live_reload_and_inheritance_pseudos[r->start],
  473. 0, k, bi)
  474. if (lra_reg_info[k].preferred_hard_regno1 >= 0
  475. && live_pseudos_reg_renumber[k] < 0
  476. && rclass_intersect_p[regno_allocno_class_array[k]])
  477. sparseset_set_bit (conflict_reload_and_inheritance_pseudos, k);
  478. for (p = r->start + 1; p <= r->finish; p++)
  479. {
  480. lra_live_range_t r2;
  481. for (r2 = start_point_ranges[p];
  482. r2 != NULL;
  483. r2 = r2->start_next)
  484. {
  485. if (r2->regno >= lra_constraint_new_regno_start
  486. && lra_reg_info[r2->regno].preferred_hard_regno1 >= 0
  487. && live_pseudos_reg_renumber[r2->regno] < 0
  488. && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
  489. sparseset_set_bit (conflict_reload_and_inheritance_pseudos,
  490. r2->regno);
  491. if (live_pseudos_reg_renumber[r2->regno] >= 0
  492. && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
  493. sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
  494. }
  495. }
  496. }
  497. if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
  498. {
  499. adjust_hard_regno_cost
  500. (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit1);
  501. if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
  502. adjust_hard_regno_cost
  503. (hard_regno, -lra_reg_info[regno].preferred_hard_regno_profit2);
  504. }
  505. #ifdef STACK_REGS
  506. if (lra_reg_info[regno].no_stack_p)
  507. for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
  508. SET_HARD_REG_BIT (conflict_set, i);
  509. #endif
  510. sparseset_clear_bit (conflict_reload_and_inheritance_pseudos, regno);
  511. val = lra_reg_info[regno].val;
  512. offset = lra_reg_info[regno].offset;
  513. CLEAR_HARD_REG_SET (impossible_start_hard_regs);
  514. EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
  515. if (lra_reg_val_equal_p (conflict_regno, val, offset))
  516. {
  517. conflict_hr = live_pseudos_reg_renumber[conflict_regno];
  518. nregs = (hard_regno_nregs[conflict_hr]
  519. [lra_reg_info[conflict_regno].biggest_mode]);
  520. /* Remember about multi-register pseudos. For example, 2 hard
  521. register pseudos can start on the same hard register but can
  522. not start on HR and HR+1/HR-1. */
  523. for (hr = conflict_hr + 1;
  524. hr < FIRST_PSEUDO_REGISTER && hr < conflict_hr + nregs;
  525. hr++)
  526. SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
  527. for (hr = conflict_hr - 1;
  528. hr >= 0 && hr + hard_regno_nregs[hr][biggest_mode] > conflict_hr;
  529. hr--)
  530. SET_HARD_REG_BIT (impossible_start_hard_regs, hr);
  531. }
  532. else
  533. {
  534. add_to_hard_reg_set (&conflict_set,
  535. lra_reg_info[conflict_regno].biggest_mode,
  536. live_pseudos_reg_renumber[conflict_regno]);
  537. if (hard_reg_set_subset_p (reg_class_contents[rclass],
  538. conflict_set))
  539. return -1;
  540. }
  541. EXECUTE_IF_SET_IN_SPARSESET (conflict_reload_and_inheritance_pseudos,
  542. conflict_regno)
  543. if (!lra_reg_val_equal_p (conflict_regno, val, offset))
  544. {
  545. lra_assert (live_pseudos_reg_renumber[conflict_regno] < 0);
  546. if ((hard_regno
  547. = lra_reg_info[conflict_regno].preferred_hard_regno1) >= 0)
  548. {
  549. adjust_hard_regno_cost
  550. (hard_regno,
  551. lra_reg_info[conflict_regno].preferred_hard_regno_profit1);
  552. if ((hard_regno
  553. = lra_reg_info[conflict_regno].preferred_hard_regno2) >= 0)
  554. adjust_hard_regno_cost
  555. (hard_regno,
  556. lra_reg_info[conflict_regno].preferred_hard_regno_profit2);
  557. }
  558. }
  559. /* Make sure that all registers in a multi-word pseudo belong to the
  560. required class. */
  561. IOR_COMPL_HARD_REG_SET (conflict_set, reg_class_contents[rclass]);
  562. lra_assert (rclass != NO_REGS);
  563. rclass_size = ira_class_hard_regs_num[rclass];
  564. best_hard_regno = -1;
  565. hard_regno = ira_class_hard_regs[rclass][0];
  566. biggest_nregs = hard_regno_nregs[hard_regno][biggest_mode];
  567. nregs_diff = (biggest_nregs
  568. - hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (regno)]);
  569. COPY_HARD_REG_SET (available_regs, reg_class_contents[rclass]);
  570. AND_COMPL_HARD_REG_SET (available_regs, lra_no_alloc_regs);
  571. for (i = 0; i < rclass_size; i++)
  572. {
  573. if (try_only_hard_regno >= 0)
  574. hard_regno = try_only_hard_regno;
  575. else
  576. hard_regno = ira_class_hard_regs[rclass][i];
  577. if (! overlaps_hard_reg_set_p (conflict_set,
  578. PSEUDO_REGNO_MODE (regno), hard_regno)
  579. /* We can not use prohibited_class_mode_regs because it is
  580. not defined for all classes. */
  581. && HARD_REGNO_MODE_OK (hard_regno, PSEUDO_REGNO_MODE (regno))
  582. && ! TEST_HARD_REG_BIT (impossible_start_hard_regs, hard_regno)
  583. && (nregs_diff == 0
  584. || (WORDS_BIG_ENDIAN
  585. ? (hard_regno - nregs_diff >= 0
  586. && TEST_HARD_REG_BIT (available_regs,
  587. hard_regno - nregs_diff))
  588. : TEST_HARD_REG_BIT (available_regs,
  589. hard_regno + nregs_diff))))
  590. {
  591. if (hard_regno_costs_check[hard_regno]
  592. != curr_hard_regno_costs_check)
  593. {
  594. hard_regno_costs_check[hard_regno] = curr_hard_regno_costs_check;
  595. hard_regno_costs[hard_regno] = 0;
  596. }
  597. for (j = 0;
  598. j < hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (regno)];
  599. j++)
  600. if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j)
  601. && ! df_regs_ever_live_p (hard_regno + j))
  602. /* It needs save restore. */
  603. hard_regno_costs[hard_regno]
  604. += (2
  605. * REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
  606. + 1);
  607. priority = targetm.register_priority (hard_regno);
  608. if (best_hard_regno < 0 || hard_regno_costs[hard_regno] < best_cost
  609. || (hard_regno_costs[hard_regno] == best_cost
  610. && (priority > best_priority
  611. || (targetm.register_usage_leveling_p ()
  612. && priority == best_priority
  613. && best_usage > lra_hard_reg_usage[hard_regno]))))
  614. {
  615. best_hard_regno = hard_regno;
  616. best_cost = hard_regno_costs[hard_regno];
  617. best_priority = priority;
  618. best_usage = lra_hard_reg_usage[hard_regno];
  619. }
  620. }
  621. if (try_only_hard_regno >= 0 || (first_p && best_hard_regno >= 0))
  622. break;
  623. }
  624. if (best_hard_regno >= 0)
  625. *cost = best_cost - lra_reg_info[regno].freq;
  626. return best_hard_regno;
  627. }
  628. /* A wrapper for find_hard_regno_for_1 (see comments for that function
  629. description). This function tries to find a hard register for
  630. preferred class first if it is worth. */
  631. static int
  632. find_hard_regno_for (int regno, int *cost, int try_only_hard_regno, bool first_p)
  633. {
  634. int hard_regno;
  635. HARD_REG_SET regno_set;
  636. /* Only original pseudos can have a different preferred class. */
  637. if (try_only_hard_regno < 0 && regno < lra_new_regno_start)
  638. {
  639. enum reg_class pref_class = reg_preferred_class (regno);
  640. if (regno_allocno_class_array[regno] != pref_class)
  641. {
  642. hard_regno = find_hard_regno_for_1 (regno, cost, -1, first_p,
  643. reg_class_contents[pref_class]);
  644. if (hard_regno >= 0)
  645. return hard_regno;
  646. }
  647. }
  648. CLEAR_HARD_REG_SET (regno_set);
  649. return find_hard_regno_for_1 (regno, cost, try_only_hard_regno, first_p,
  650. regno_set);
  651. }
  652. /* Current value used for checking elements in
  653. update_hard_regno_preference_check. */
  654. static int curr_update_hard_regno_preference_check;
  655. /* If an element value is equal to the above variable value, then the
  656. corresponding regno has been processed for preference
  657. propagation. */
  658. static int *update_hard_regno_preference_check;
  659. /* Update the preference for using HARD_REGNO for pseudos that are
  660. connected directly or indirectly with REGNO. Apply divisor DIV
  661. to any preference adjustments.
  662. The more indirectly a pseudo is connected, the smaller its effect
  663. should be. We therefore increase DIV on each "hop". */
  664. static void
  665. update_hard_regno_preference (int regno, int hard_regno, int div)
  666. {
  667. int another_regno, cost;
  668. lra_copy_t cp, next_cp;
  669. /* Search depth 5 seems to be enough. */
  670. if (div > (1 << 5))
  671. return;
  672. for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
  673. {
  674. if (cp->regno1 == regno)
  675. {
  676. next_cp = cp->regno1_next;
  677. another_regno = cp->regno2;
  678. }
  679. else if (cp->regno2 == regno)
  680. {
  681. next_cp = cp->regno2_next;
  682. another_regno = cp->regno1;
  683. }
  684. else
  685. gcc_unreachable ();
  686. if (reg_renumber[another_regno] < 0
  687. && (update_hard_regno_preference_check[another_regno]
  688. != curr_update_hard_regno_preference_check))
  689. {
  690. update_hard_regno_preference_check[another_regno]
  691. = curr_update_hard_regno_preference_check;
  692. cost = cp->freq < div ? 1 : cp->freq / div;
  693. lra_setup_reload_pseudo_preferenced_hard_reg
  694. (another_regno, hard_regno, cost);
  695. update_hard_regno_preference (another_regno, hard_regno, div * 2);
  696. }
  697. }
  698. }
  699. /* Return prefix title for pseudo REGNO. */
  700. static const char *
  701. pseudo_prefix_title (int regno)
  702. {
  703. return
  704. (regno < lra_constraint_new_regno_start ? ""
  705. : bitmap_bit_p (&lra_inheritance_pseudos, regno) ? "inheritance "
  706. : bitmap_bit_p (&lra_split_regs, regno) ? "split "
  707. : bitmap_bit_p (&lra_optional_reload_pseudos, regno) ? "optional reload "
  708. : bitmap_bit_p (&lra_subreg_reload_pseudos, regno) ? "subreg reload "
  709. : "reload ");
  710. }
  711. /* Update REG_RENUMBER and other pseudo preferences by assignment of
  712. HARD_REGNO to pseudo REGNO and print about it if PRINT_P. */
  713. void
  714. lra_setup_reg_renumber (int regno, int hard_regno, bool print_p)
  715. {
  716. int i, hr;
  717. /* We can not just reassign hard register. */
  718. lra_assert (hard_regno < 0 || reg_renumber[regno] < 0);
  719. if ((hr = hard_regno) < 0)
  720. hr = reg_renumber[regno];
  721. reg_renumber[regno] = hard_regno;
  722. lra_assert (hr >= 0);
  723. for (i = 0; i < hard_regno_nregs[hr][PSEUDO_REGNO_MODE (regno)]; i++)
  724. if (hard_regno < 0)
  725. lra_hard_reg_usage[hr + i] -= lra_reg_info[regno].freq;
  726. else
  727. lra_hard_reg_usage[hr + i] += lra_reg_info[regno].freq;
  728. if (print_p && lra_dump_file != NULL)
  729. fprintf (lra_dump_file, " Assign %d to %sr%d (freq=%d)\n",
  730. reg_renumber[regno], pseudo_prefix_title (regno),
  731. regno, lra_reg_info[regno].freq);
  732. if (hard_regno >= 0)
  733. {
  734. curr_update_hard_regno_preference_check++;
  735. update_hard_regno_preference (regno, hard_regno, 1);
  736. }
  737. }
  738. /* Pseudos which occur in insns containing a particular pseudo. */
  739. static bitmap_head insn_conflict_pseudos;
  740. /* Bitmaps used to contain spill pseudos for given pseudo hard regno
  741. and best spill pseudos for given pseudo (and best hard regno). */
  742. static bitmap_head spill_pseudos_bitmap, best_spill_pseudos_bitmap;
  743. /* Current pseudo check for validity of elements in
  744. TRY_HARD_REG_PSEUDOS. */
  745. static int curr_pseudo_check;
  746. /* Array used for validity of elements in TRY_HARD_REG_PSEUDOS. */
  747. static int try_hard_reg_pseudos_check[FIRST_PSEUDO_REGISTER];
  748. /* Pseudos who hold given hard register at the considered points. */
  749. static bitmap_head try_hard_reg_pseudos[FIRST_PSEUDO_REGISTER];
  750. /* Set up try_hard_reg_pseudos for given program point P and class
  751. RCLASS. Those are pseudos living at P and assigned to a hard
  752. register of RCLASS. In other words, those are pseudos which can be
  753. spilled to assign a hard register of RCLASS to a pseudo living at
  754. P. */
  755. static void
  756. setup_try_hard_regno_pseudos (int p, enum reg_class rclass)
  757. {
  758. int i, hard_regno;
  759. machine_mode mode;
  760. unsigned int spill_regno;
  761. bitmap_iterator bi;
  762. /* Find what pseudos could be spilled. */
  763. EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[p], 0, spill_regno, bi)
  764. {
  765. mode = PSEUDO_REGNO_MODE (spill_regno);
  766. hard_regno = live_pseudos_reg_renumber[spill_regno];
  767. if (overlaps_hard_reg_set_p (reg_class_contents[rclass],
  768. mode, hard_regno))
  769. {
  770. for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
  771. {
  772. if (try_hard_reg_pseudos_check[hard_regno + i]
  773. != curr_pseudo_check)
  774. {
  775. try_hard_reg_pseudos_check[hard_regno + i]
  776. = curr_pseudo_check;
  777. bitmap_clear (&try_hard_reg_pseudos[hard_regno + i]);
  778. }
  779. bitmap_set_bit (&try_hard_reg_pseudos[hard_regno + i],
  780. spill_regno);
  781. }
  782. }
  783. }
  784. }
  785. /* Assign temporarily HARD_REGNO to pseudo REGNO. Temporary
  786. assignment means that we might undo the data change. */
  787. static void
  788. assign_temporarily (int regno, int hard_regno)
  789. {
  790. int p;
  791. lra_live_range_t r;
  792. for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
  793. {
  794. for (p = r->start; p <= r->finish; p++)
  795. if (hard_regno < 0)
  796. bitmap_clear_bit (&live_hard_reg_pseudos[p], regno);
  797. else
  798. {
  799. bitmap_set_bit (&live_hard_reg_pseudos[p], regno);
  800. insert_in_live_range_start_chain (regno);
  801. }
  802. }
  803. live_pseudos_reg_renumber[regno] = hard_regno;
  804. }
  805. /* Array used for sorting reload pseudos for subsequent allocation
  806. after spilling some pseudo. */
  807. static int *sorted_reload_pseudos;
  808. /* Spill some pseudos for a reload pseudo REGNO and return hard
  809. register which should be used for pseudo after spilling. The
  810. function adds spilled pseudos to SPILLED_PSEUDO_BITMAP. When we
  811. choose hard register (and pseudos occupying the hard registers and
  812. to be spilled), we take into account not only how REGNO will
  813. benefit from the spills but also how other reload pseudos not yet
  814. assigned to hard registers benefit from the spills too. In very
  815. rare cases, the function can fail and return -1.
  816. If FIRST_P, return the first available hard reg ignoring other
  817. criteria, e.g. allocation cost and cost of spilling non-reload
  818. pseudos. This approach results in less hard reg pool fragmentation
  819. and permit to allocate hard regs to reload pseudos in complicated
  820. situations where pseudo sizes are different. */
  821. static int
  822. spill_for (int regno, bitmap spilled_pseudo_bitmap, bool first_p)
  823. {
  824. int i, j, n, p, hard_regno, best_hard_regno, cost, best_cost, rclass_size;
  825. int reload_hard_regno, reload_cost;
  826. machine_mode mode;
  827. enum reg_class rclass;
  828. unsigned int spill_regno, reload_regno, uid;
  829. int insn_pseudos_num, best_insn_pseudos_num;
  830. int bad_spills_num, smallest_bad_spills_num;
  831. lra_live_range_t r;
  832. bitmap_iterator bi;
  833. rclass = regno_allocno_class_array[regno];
  834. lra_assert (reg_renumber[regno] < 0 && rclass != NO_REGS);
  835. bitmap_clear (&insn_conflict_pseudos);
  836. bitmap_clear (&best_spill_pseudos_bitmap);
  837. EXECUTE_IF_SET_IN_BITMAP (&lra_reg_info[regno].insn_bitmap, 0, uid, bi)
  838. {
  839. struct lra_insn_reg *ir;
  840. for (ir = lra_get_insn_regs (uid); ir != NULL; ir = ir->next)
  841. if (ir->regno >= FIRST_PSEUDO_REGISTER)
  842. bitmap_set_bit (&insn_conflict_pseudos, ir->regno);
  843. }
  844. best_hard_regno = -1;
  845. best_cost = INT_MAX;
  846. best_insn_pseudos_num = INT_MAX;
  847. smallest_bad_spills_num = INT_MAX;
  848. rclass_size = ira_class_hard_regs_num[rclass];
  849. mode = PSEUDO_REGNO_MODE (regno);
  850. /* Invalidate try_hard_reg_pseudos elements. */
  851. curr_pseudo_check++;
  852. for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
  853. for (p = r->start; p <= r->finish; p++)
  854. setup_try_hard_regno_pseudos (p, rclass);
  855. for (i = 0; i < rclass_size; i++)
  856. {
  857. hard_regno = ira_class_hard_regs[rclass][i];
  858. bitmap_clear (&spill_pseudos_bitmap);
  859. for (j = hard_regno_nregs[hard_regno][mode] - 1; j >= 0; j--)
  860. {
  861. if (try_hard_reg_pseudos_check[hard_regno + j] != curr_pseudo_check)
  862. continue;
  863. lra_assert (!bitmap_empty_p (&try_hard_reg_pseudos[hard_regno + j]));
  864. bitmap_ior_into (&spill_pseudos_bitmap,
  865. &try_hard_reg_pseudos[hard_regno + j]);
  866. }
  867. /* Spill pseudos. */
  868. EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
  869. if ((pic_offset_table_rtx != NULL
  870. && spill_regno == REGNO (pic_offset_table_rtx))
  871. || ((int) spill_regno >= lra_constraint_new_regno_start
  872. && ! bitmap_bit_p (&lra_inheritance_pseudos, spill_regno)
  873. && ! bitmap_bit_p (&lra_split_regs, spill_regno)
  874. && ! bitmap_bit_p (&lra_subreg_reload_pseudos, spill_regno)
  875. && ! bitmap_bit_p (&lra_optional_reload_pseudos, spill_regno)))
  876. goto fail;
  877. insn_pseudos_num = 0;
  878. bad_spills_num = 0;
  879. if (lra_dump_file != NULL)
  880. fprintf (lra_dump_file, " Trying %d:", hard_regno);
  881. sparseset_clear (live_range_reload_inheritance_pseudos);
  882. EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
  883. {
  884. if (bitmap_bit_p (&insn_conflict_pseudos, spill_regno))
  885. insn_pseudos_num++;
  886. if (spill_regno >= (unsigned int) lra_bad_spill_regno_start)
  887. bad_spills_num++;
  888. for (r = lra_reg_info[spill_regno].live_ranges;
  889. r != NULL;
  890. r = r->next)
  891. {
  892. for (p = r->start; p <= r->finish; p++)
  893. {
  894. lra_live_range_t r2;
  895. for (r2 = start_point_ranges[p];
  896. r2 != NULL;
  897. r2 = r2->start_next)
  898. if (r2->regno >= lra_constraint_new_regno_start)
  899. sparseset_set_bit (live_range_reload_inheritance_pseudos,
  900. r2->regno);
  901. }
  902. }
  903. }
  904. n = 0;
  905. if (sparseset_cardinality (live_range_reload_inheritance_pseudos)
  906. <= (unsigned)LRA_MAX_CONSIDERED_RELOAD_PSEUDOS)
  907. EXECUTE_IF_SET_IN_SPARSESET (live_range_reload_inheritance_pseudos,
  908. reload_regno)
  909. if ((int) reload_regno != regno
  910. && (ira_reg_classes_intersect_p
  911. [rclass][regno_allocno_class_array[reload_regno]])
  912. && live_pseudos_reg_renumber[reload_regno] < 0
  913. && find_hard_regno_for (reload_regno, &cost, -1, first_p) < 0)
  914. sorted_reload_pseudos[n++] = reload_regno;
  915. EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
  916. {
  917. update_lives (spill_regno, true);
  918. if (lra_dump_file != NULL)
  919. fprintf (lra_dump_file, " spill %d(freq=%d)",
  920. spill_regno, lra_reg_info[spill_regno].freq);
  921. }
  922. hard_regno = find_hard_regno_for (regno, &cost, -1, first_p);
  923. if (hard_regno >= 0)
  924. {
  925. assign_temporarily (regno, hard_regno);
  926. qsort (sorted_reload_pseudos, n, sizeof (int),
  927. reload_pseudo_compare_func);
  928. for (j = 0; j < n; j++)
  929. {
  930. reload_regno = sorted_reload_pseudos[j];
  931. lra_assert (live_pseudos_reg_renumber[reload_regno] < 0);
  932. if ((reload_hard_regno
  933. = find_hard_regno_for (reload_regno,
  934. &reload_cost, -1, first_p)) >= 0)
  935. {
  936. if (lra_dump_file != NULL)
  937. fprintf (lra_dump_file, " assign %d(cost=%d)",
  938. reload_regno, reload_cost);
  939. assign_temporarily (reload_regno, reload_hard_regno);
  940. cost += reload_cost;
  941. }
  942. }
  943. EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
  944. {
  945. rtx_insn_list *x;
  946. cost += lra_reg_info[spill_regno].freq;
  947. if (ira_reg_equiv[spill_regno].memory != NULL
  948. || ira_reg_equiv[spill_regno].constant != NULL)
  949. for (x = ira_reg_equiv[spill_regno].init_insns;
  950. x != NULL;
  951. x = x->next ())
  952. cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (x->insn ()));
  953. }
  954. if (best_insn_pseudos_num > insn_pseudos_num
  955. || (best_insn_pseudos_num == insn_pseudos_num
  956. && (bad_spills_num < smallest_bad_spills_num
  957. || (bad_spills_num == smallest_bad_spills_num
  958. && best_cost > cost))))
  959. {
  960. best_insn_pseudos_num = insn_pseudos_num;
  961. smallest_bad_spills_num = bad_spills_num;
  962. best_cost = cost;
  963. best_hard_regno = hard_regno;
  964. bitmap_copy (&best_spill_pseudos_bitmap, &spill_pseudos_bitmap);
  965. if (lra_dump_file != NULL)
  966. fprintf (lra_dump_file,
  967. " Now best %d(cost=%d, bad_spills=%d, insn_pseudos=%d)\n",
  968. hard_regno, cost, bad_spills_num, insn_pseudos_num);
  969. }
  970. assign_temporarily (regno, -1);
  971. for (j = 0; j < n; j++)
  972. {
  973. reload_regno = sorted_reload_pseudos[j];
  974. if (live_pseudos_reg_renumber[reload_regno] >= 0)
  975. assign_temporarily (reload_regno, -1);
  976. }
  977. }
  978. if (lra_dump_file != NULL)
  979. fprintf (lra_dump_file, "\n");
  980. /* Restore the live hard reg pseudo info for spilled pseudos. */
  981. EXECUTE_IF_SET_IN_BITMAP (&spill_pseudos_bitmap, 0, spill_regno, bi)
  982. update_lives (spill_regno, false);
  983. fail:
  984. ;
  985. }
  986. /* Spill: */
  987. EXECUTE_IF_SET_IN_BITMAP (&best_spill_pseudos_bitmap, 0, spill_regno, bi)
  988. {
  989. if ((int) spill_regno >= lra_constraint_new_regno_start)
  990. former_reload_pseudo_spill_p = true;
  991. if (lra_dump_file != NULL)
  992. fprintf (lra_dump_file, " Spill %sr%d(hr=%d, freq=%d) for r%d\n",
  993. pseudo_prefix_title (spill_regno),
  994. spill_regno, reg_renumber[spill_regno],
  995. lra_reg_info[spill_regno].freq, regno);
  996. update_lives (spill_regno, true);
  997. lra_setup_reg_renumber (spill_regno, -1, false);
  998. }
  999. bitmap_ior_into (spilled_pseudo_bitmap, &best_spill_pseudos_bitmap);
  1000. return best_hard_regno;
  1001. }
  1002. /* Assign HARD_REGNO to REGNO. */
  1003. static void
  1004. assign_hard_regno (int hard_regno, int regno)
  1005. {
  1006. int i;
  1007. lra_assert (hard_regno >= 0);
  1008. lra_setup_reg_renumber (regno, hard_regno, true);
  1009. update_lives (regno, false);
  1010. for (i = 0;
  1011. i < hard_regno_nregs[hard_regno][lra_reg_info[regno].biggest_mode];
  1012. i++)
  1013. df_set_regs_ever_live (hard_regno + i, true);
  1014. }
  1015. /* Array used for sorting different pseudos. */
  1016. static int *sorted_pseudos;
  1017. /* The constraints pass is allowed to create equivalences between
  1018. pseudos that make the current allocation "incorrect" (in the sense
  1019. that pseudos are assigned to hard registers from their own conflict
  1020. sets). The global variable lra_risky_transformations_p says
  1021. whether this might have happened.
  1022. Process pseudos assigned to hard registers (less frequently used
  1023. first), spill if a conflict is found, and mark the spilled pseudos
  1024. in SPILLED_PSEUDO_BITMAP. Set up LIVE_HARD_REG_PSEUDOS from
  1025. pseudos, assigned to hard registers. */
  1026. static void
  1027. setup_live_pseudos_and_spill_after_risky_transforms (bitmap
  1028. spilled_pseudo_bitmap)
  1029. {
  1030. int p, i, j, n, regno, hard_regno;
  1031. unsigned int k, conflict_regno;
  1032. int val, offset;
  1033. HARD_REG_SET conflict_set;
  1034. machine_mode mode;
  1035. lra_live_range_t r;
  1036. bitmap_iterator bi;
  1037. int max_regno = max_reg_num ();
  1038. if (! lra_risky_transformations_p)
  1039. {
  1040. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1041. if (reg_renumber[i] >= 0 && lra_reg_info[i].nrefs > 0)
  1042. update_lives (i, false);
  1043. return;
  1044. }
  1045. for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1046. if ((pic_offset_table_rtx == NULL_RTX
  1047. || i != (int) REGNO (pic_offset_table_rtx))
  1048. && reg_renumber[i] >= 0 && lra_reg_info[i].nrefs > 0)
  1049. sorted_pseudos[n++] = i;
  1050. qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
  1051. if (pic_offset_table_rtx != NULL_RTX
  1052. && (regno = REGNO (pic_offset_table_rtx)) >= FIRST_PSEUDO_REGISTER
  1053. && reg_renumber[regno] >= 0 && lra_reg_info[regno].nrefs > 0)
  1054. sorted_pseudos[n++] = regno;
  1055. for (i = n - 1; i >= 0; i--)
  1056. {
  1057. regno = sorted_pseudos[i];
  1058. hard_regno = reg_renumber[regno];
  1059. lra_assert (hard_regno >= 0);
  1060. mode = lra_reg_info[regno].biggest_mode;
  1061. sparseset_clear (live_range_hard_reg_pseudos);
  1062. for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
  1063. {
  1064. EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
  1065. sparseset_set_bit (live_range_hard_reg_pseudos, k);
  1066. for (p = r->start + 1; p <= r->finish; p++)
  1067. {
  1068. lra_live_range_t r2;
  1069. for (r2 = start_point_ranges[p];
  1070. r2 != NULL;
  1071. r2 = r2->start_next)
  1072. if (live_pseudos_reg_renumber[r2->regno] >= 0)
  1073. sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
  1074. }
  1075. }
  1076. COPY_HARD_REG_SET (conflict_set, lra_no_alloc_regs);
  1077. IOR_HARD_REG_SET (conflict_set, lra_reg_info[regno].conflict_hard_regs);
  1078. val = lra_reg_info[regno].val;
  1079. offset = lra_reg_info[regno].offset;
  1080. EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
  1081. if (!lra_reg_val_equal_p (conflict_regno, val, offset)
  1082. /* If it is multi-register pseudos they should start on
  1083. the same hard register. */
  1084. || hard_regno != reg_renumber[conflict_regno])
  1085. add_to_hard_reg_set (&conflict_set,
  1086. lra_reg_info[conflict_regno].biggest_mode,
  1087. reg_renumber[conflict_regno]);
  1088. if (! overlaps_hard_reg_set_p (conflict_set, mode, hard_regno))
  1089. {
  1090. update_lives (regno, false);
  1091. continue;
  1092. }
  1093. bitmap_set_bit (spilled_pseudo_bitmap, regno);
  1094. for (j = 0;
  1095. j < hard_regno_nregs[hard_regno][PSEUDO_REGNO_MODE (regno)];
  1096. j++)
  1097. lra_hard_reg_usage[hard_regno + j] -= lra_reg_info[regno].freq;
  1098. reg_renumber[regno] = -1;
  1099. if (regno >= lra_constraint_new_regno_start)
  1100. former_reload_pseudo_spill_p = true;
  1101. if (lra_dump_file != NULL)
  1102. fprintf (lra_dump_file, " Spill r%d after risky transformations\n",
  1103. regno);
  1104. }
  1105. }
  1106. /* Improve allocation by assigning the same hard regno of inheritance
  1107. pseudos to the connected pseudos. We need this because inheritance
  1108. pseudos are allocated after reload pseudos in the thread and when
  1109. we assign a hard register to a reload pseudo we don't know yet that
  1110. the connected inheritance pseudos can get the same hard register.
  1111. Add pseudos with changed allocation to bitmap CHANGED_PSEUDOS. */
  1112. static void
  1113. improve_inheritance (bitmap changed_pseudos)
  1114. {
  1115. unsigned int k;
  1116. int regno, another_regno, hard_regno, another_hard_regno, cost, i, n;
  1117. lra_copy_t cp, next_cp;
  1118. bitmap_iterator bi;
  1119. if (lra_inheritance_iter > LRA_MAX_INHERITANCE_PASSES)
  1120. return;
  1121. n = 0;
  1122. EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, k, bi)
  1123. if (reg_renumber[k] >= 0 && lra_reg_info[k].nrefs != 0)
  1124. sorted_pseudos[n++] = k;
  1125. qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
  1126. for (i = 0; i < n; i++)
  1127. {
  1128. regno = sorted_pseudos[i];
  1129. hard_regno = reg_renumber[regno];
  1130. lra_assert (hard_regno >= 0);
  1131. for (cp = lra_reg_info[regno].copies; cp != NULL; cp = next_cp)
  1132. {
  1133. if (cp->regno1 == regno)
  1134. {
  1135. next_cp = cp->regno1_next;
  1136. another_regno = cp->regno2;
  1137. }
  1138. else if (cp->regno2 == regno)
  1139. {
  1140. next_cp = cp->regno2_next;
  1141. another_regno = cp->regno1;
  1142. }
  1143. else
  1144. gcc_unreachable ();
  1145. /* Don't change reload pseudo allocation. It might have
  1146. this allocation for a purpose and changing it can result
  1147. in LRA cycling. */
  1148. if ((another_regno < lra_constraint_new_regno_start
  1149. || bitmap_bit_p (&lra_inheritance_pseudos, another_regno))
  1150. && (another_hard_regno = reg_renumber[another_regno]) >= 0
  1151. && another_hard_regno != hard_regno)
  1152. {
  1153. if (lra_dump_file != NULL)
  1154. fprintf
  1155. (lra_dump_file,
  1156. " Improving inheritance for %d(%d) and %d(%d)...\n",
  1157. regno, hard_regno, another_regno, another_hard_regno);
  1158. update_lives (another_regno, true);
  1159. lra_setup_reg_renumber (another_regno, -1, false);
  1160. if (hard_regno == find_hard_regno_for (another_regno, &cost,
  1161. hard_regno, false))
  1162. assign_hard_regno (hard_regno, another_regno);
  1163. else
  1164. assign_hard_regno (another_hard_regno, another_regno);
  1165. bitmap_set_bit (changed_pseudos, another_regno);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. /* Bitmap finally containing all pseudos spilled on this assignment
  1171. pass. */
  1172. static bitmap_head all_spilled_pseudos;
  1173. /* All pseudos whose allocation was changed. */
  1174. static bitmap_head changed_pseudo_bitmap;
  1175. /* Add to LIVE_RANGE_HARD_REG_PSEUDOS all pseudos conflicting with
  1176. REGNO and whose hard regs can be assigned to REGNO. */
  1177. static void
  1178. find_all_spills_for (int regno)
  1179. {
  1180. int p;
  1181. lra_live_range_t r;
  1182. unsigned int k;
  1183. bitmap_iterator bi;
  1184. enum reg_class rclass;
  1185. bool *rclass_intersect_p;
  1186. rclass = regno_allocno_class_array[regno];
  1187. rclass_intersect_p = ira_reg_classes_intersect_p[rclass];
  1188. for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
  1189. {
  1190. EXECUTE_IF_SET_IN_BITMAP (&live_hard_reg_pseudos[r->start], 0, k, bi)
  1191. if (rclass_intersect_p[regno_allocno_class_array[k]])
  1192. sparseset_set_bit (live_range_hard_reg_pseudos, k);
  1193. for (p = r->start + 1; p <= r->finish; p++)
  1194. {
  1195. lra_live_range_t r2;
  1196. for (r2 = start_point_ranges[p];
  1197. r2 != NULL;
  1198. r2 = r2->start_next)
  1199. {
  1200. if (live_pseudos_reg_renumber[r2->regno] >= 0
  1201. && rclass_intersect_p[regno_allocno_class_array[r2->regno]])
  1202. sparseset_set_bit (live_range_hard_reg_pseudos, r2->regno);
  1203. }
  1204. }
  1205. }
  1206. }
  1207. /* Assign hard registers to reload pseudos and other pseudos. */
  1208. static void
  1209. assign_by_spills (void)
  1210. {
  1211. int i, n, nfails, iter, regno, hard_regno, cost, restore_regno;
  1212. rtx_insn *insn;
  1213. bitmap_head changed_insns, do_not_assign_nonreload_pseudos;
  1214. unsigned int u, conflict_regno;
  1215. bitmap_iterator bi;
  1216. bool reload_p;
  1217. int max_regno = max_reg_num ();
  1218. for (n = 0, i = lra_constraint_new_regno_start; i < max_regno; i++)
  1219. if (reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
  1220. && regno_allocno_class_array[i] != NO_REGS)
  1221. sorted_pseudos[n++] = i;
  1222. bitmap_initialize (&insn_conflict_pseudos, &reg_obstack);
  1223. bitmap_initialize (&spill_pseudos_bitmap, &reg_obstack);
  1224. bitmap_initialize (&best_spill_pseudos_bitmap, &reg_obstack);
  1225. update_hard_regno_preference_check = XCNEWVEC (int, max_regno);
  1226. curr_update_hard_regno_preference_check = 0;
  1227. memset (try_hard_reg_pseudos_check, 0, sizeof (try_hard_reg_pseudos_check));
  1228. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  1229. bitmap_initialize (&try_hard_reg_pseudos[i], &reg_obstack);
  1230. curr_pseudo_check = 0;
  1231. bitmap_initialize (&changed_insns, &reg_obstack);
  1232. bitmap_initialize (&non_reload_pseudos, &reg_obstack);
  1233. bitmap_ior (&non_reload_pseudos, &lra_inheritance_pseudos, &lra_split_regs);
  1234. bitmap_ior_into (&non_reload_pseudos, &lra_subreg_reload_pseudos);
  1235. bitmap_ior_into (&non_reload_pseudos, &lra_optional_reload_pseudos);
  1236. for (iter = 0; iter <= 1; iter++)
  1237. {
  1238. qsort (sorted_pseudos, n, sizeof (int), reload_pseudo_compare_func);
  1239. nfails = 0;
  1240. for (i = 0; i < n; i++)
  1241. {
  1242. regno = sorted_pseudos[i];
  1243. if (lra_dump_file != NULL)
  1244. fprintf (lra_dump_file, " Assigning to %d "
  1245. "(cl=%s, orig=%d, freq=%d, tfirst=%d, tfreq=%d)...\n",
  1246. regno, reg_class_names[regno_allocno_class_array[regno]],
  1247. ORIGINAL_REGNO (regno_reg_rtx[regno]),
  1248. lra_reg_info[regno].freq, regno_assign_info[regno].first,
  1249. regno_assign_info[regno_assign_info[regno].first].freq);
  1250. hard_regno = find_hard_regno_for (regno, &cost, -1, iter == 1);
  1251. reload_p = ! bitmap_bit_p (&non_reload_pseudos, regno);
  1252. if (hard_regno < 0 && reload_p)
  1253. hard_regno = spill_for (regno, &all_spilled_pseudos, iter == 1);
  1254. if (hard_regno < 0)
  1255. {
  1256. if (reload_p)
  1257. sorted_pseudos[nfails++] = regno;
  1258. }
  1259. else
  1260. {
  1261. /* This register might have been spilled by the previous
  1262. pass. Indicate that it is no longer spilled. */
  1263. bitmap_clear_bit (&all_spilled_pseudos, regno);
  1264. assign_hard_regno (hard_regno, regno);
  1265. if (! reload_p)
  1266. /* As non-reload pseudo assignment is changed we
  1267. should reconsider insns referring for the
  1268. pseudo. */
  1269. bitmap_set_bit (&changed_pseudo_bitmap, regno);
  1270. }
  1271. }
  1272. if (nfails == 0)
  1273. break;
  1274. if (iter > 0)
  1275. {
  1276. /* We did not assign hard regs to reload pseudos after two iterations.
  1277. Either it's an asm and something is wrong with the constraints, or
  1278. we have run out of spill registers; error out in either case. */
  1279. bool asm_p = false;
  1280. bitmap_head failed_reload_insns;
  1281. bitmap_initialize (&failed_reload_insns, &reg_obstack);
  1282. for (i = 0; i < nfails; i++)
  1283. {
  1284. regno = sorted_pseudos[i];
  1285. bitmap_ior_into (&failed_reload_insns,
  1286. &lra_reg_info[regno].insn_bitmap);
  1287. /* Assign an arbitrary hard register of regno class to
  1288. avoid further trouble with this insn. */
  1289. bitmap_clear_bit (&all_spilled_pseudos, regno);
  1290. assign_hard_regno
  1291. (ira_class_hard_regs[regno_allocno_class_array[regno]][0],
  1292. regno);
  1293. }
  1294. EXECUTE_IF_SET_IN_BITMAP (&failed_reload_insns, 0, u, bi)
  1295. {
  1296. insn = lra_insn_recog_data[u]->insn;
  1297. if (asm_noperands (PATTERN (insn)) >= 0)
  1298. {
  1299. asm_p = true;
  1300. error_for_asm (insn,
  1301. "%<asm%> operand has impossible constraints");
  1302. /* Avoid further trouble with this insn.
  1303. For asm goto, instead of fixing up all the edges
  1304. just clear the template and clear input operands
  1305. (asm goto doesn't have any output operands). */
  1306. if (JUMP_P (insn))
  1307. {
  1308. rtx asm_op = extract_asm_operands (PATTERN (insn));
  1309. ASM_OPERANDS_TEMPLATE (asm_op) = ggc_strdup ("");
  1310. ASM_OPERANDS_INPUT_VEC (asm_op) = rtvec_alloc (0);
  1311. ASM_OPERANDS_INPUT_CONSTRAINT_VEC (asm_op) = rtvec_alloc (0);
  1312. lra_update_insn_regno_info (insn);
  1313. }
  1314. else
  1315. {
  1316. PATTERN (insn) = gen_rtx_USE (VOIDmode, const0_rtx);
  1317. lra_set_insn_deleted (insn);
  1318. }
  1319. }
  1320. else if (!asm_p)
  1321. {
  1322. error ("unable to find a register to spill");
  1323. fatal_insn ("this is the insn:", insn);
  1324. }
  1325. }
  1326. break;
  1327. }
  1328. /* This is a very rare event. We can not assign a hard register
  1329. to reload pseudo because the hard register was assigned to
  1330. another reload pseudo on a previous assignment pass. For x86
  1331. example, on the 1st pass we assigned CX (although another
  1332. hard register could be used for this) to reload pseudo in an
  1333. insn, on the 2nd pass we need CX (and only this) hard
  1334. register for a new reload pseudo in the same insn. Another
  1335. possible situation may occur in assigning to multi-regs
  1336. reload pseudos when hard regs pool is too fragmented even
  1337. after spilling non-reload pseudos.
  1338. We should do something radical here to succeed. Here we
  1339. spill *all* conflicting pseudos and reassign them. */
  1340. if (lra_dump_file != NULL)
  1341. fprintf (lra_dump_file, " 2nd iter for reload pseudo assignments:\n");
  1342. sparseset_clear (live_range_hard_reg_pseudos);
  1343. for (i = 0; i < nfails; i++)
  1344. {
  1345. if (lra_dump_file != NULL)
  1346. fprintf (lra_dump_file, " Reload r%d assignment failure\n",
  1347. sorted_pseudos[i]);
  1348. find_all_spills_for (sorted_pseudos[i]);
  1349. }
  1350. EXECUTE_IF_SET_IN_SPARSESET (live_range_hard_reg_pseudos, conflict_regno)
  1351. {
  1352. if ((int) conflict_regno >= lra_constraint_new_regno_start)
  1353. {
  1354. sorted_pseudos[nfails++] = conflict_regno;
  1355. former_reload_pseudo_spill_p = true;
  1356. }
  1357. if (lra_dump_file != NULL)
  1358. fprintf (lra_dump_file, " Spill %s r%d(hr=%d, freq=%d)\n",
  1359. pseudo_prefix_title (conflict_regno), conflict_regno,
  1360. reg_renumber[conflict_regno],
  1361. lra_reg_info[conflict_regno].freq);
  1362. update_lives (conflict_regno, true);
  1363. lra_setup_reg_renumber (conflict_regno, -1, false);
  1364. }
  1365. n = nfails;
  1366. }
  1367. improve_inheritance (&changed_pseudo_bitmap);
  1368. bitmap_clear (&non_reload_pseudos);
  1369. bitmap_clear (&changed_insns);
  1370. if (! lra_simple_p)
  1371. {
  1372. /* We should not assign to original pseudos of inheritance
  1373. pseudos or split pseudos if any its inheritance pseudo did
  1374. not get hard register or any its split pseudo was not split
  1375. because undo inheritance/split pass will extend live range of
  1376. such inheritance or split pseudos. */
  1377. bitmap_initialize (&do_not_assign_nonreload_pseudos, &reg_obstack);
  1378. EXECUTE_IF_SET_IN_BITMAP (&lra_inheritance_pseudos, 0, u, bi)
  1379. if ((restore_regno = lra_reg_info[u].restore_regno) >= 0
  1380. && reg_renumber[u] < 0
  1381. && bitmap_bit_p (&lra_inheritance_pseudos, u))
  1382. bitmap_set_bit (&do_not_assign_nonreload_pseudos, restore_regno);
  1383. EXECUTE_IF_SET_IN_BITMAP (&lra_split_regs, 0, u, bi)
  1384. if ((restore_regno = lra_reg_info[u].restore_regno) >= 0
  1385. && reg_renumber[u] >= 0)
  1386. bitmap_set_bit (&do_not_assign_nonreload_pseudos, restore_regno);
  1387. for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1388. if (((i < lra_constraint_new_regno_start
  1389. && ! bitmap_bit_p (&do_not_assign_nonreload_pseudos, i))
  1390. || (bitmap_bit_p (&lra_inheritance_pseudos, i)
  1391. && lra_reg_info[i].restore_regno >= 0)
  1392. || (bitmap_bit_p (&lra_split_regs, i)
  1393. && lra_reg_info[i].restore_regno >= 0)
  1394. || bitmap_bit_p (&lra_subreg_reload_pseudos, i)
  1395. || bitmap_bit_p (&lra_optional_reload_pseudos, i))
  1396. && reg_renumber[i] < 0 && lra_reg_info[i].nrefs != 0
  1397. && regno_allocno_class_array[i] != NO_REGS)
  1398. sorted_pseudos[n++] = i;
  1399. bitmap_clear (&do_not_assign_nonreload_pseudos);
  1400. if (n != 0 && lra_dump_file != NULL)
  1401. fprintf (lra_dump_file, " Reassigning non-reload pseudos\n");
  1402. qsort (sorted_pseudos, n, sizeof (int), pseudo_compare_func);
  1403. for (i = 0; i < n; i++)
  1404. {
  1405. regno = sorted_pseudos[i];
  1406. hard_regno = find_hard_regno_for (regno, &cost, -1, false);
  1407. if (hard_regno >= 0)
  1408. {
  1409. assign_hard_regno (hard_regno, regno);
  1410. /* We change allocation for non-reload pseudo on this
  1411. iteration -- mark the pseudo for invalidation of used
  1412. alternatives of insns containing the pseudo. */
  1413. bitmap_set_bit (&changed_pseudo_bitmap, regno);
  1414. }
  1415. else
  1416. {
  1417. enum reg_class rclass = lra_get_allocno_class (regno);
  1418. enum reg_class spill_class;
  1419. if (targetm.spill_class == NULL
  1420. || lra_reg_info[regno].restore_regno < 0
  1421. || ! bitmap_bit_p (&lra_inheritance_pseudos, regno)
  1422. || (spill_class
  1423. = ((enum reg_class)
  1424. targetm.spill_class
  1425. ((reg_class_t) rclass,
  1426. PSEUDO_REGNO_MODE (regno)))) == NO_REGS)
  1427. continue;
  1428. regno_allocno_class_array[regno] = spill_class;
  1429. hard_regno = find_hard_regno_for (regno, &cost, -1, false);
  1430. if (hard_regno < 0)
  1431. regno_allocno_class_array[regno] = rclass;
  1432. else
  1433. {
  1434. setup_reg_classes
  1435. (regno, spill_class, spill_class, spill_class);
  1436. assign_hard_regno (hard_regno, regno);
  1437. bitmap_set_bit (&changed_pseudo_bitmap, regno);
  1438. }
  1439. }
  1440. }
  1441. }
  1442. free (update_hard_regno_preference_check);
  1443. bitmap_clear (&best_spill_pseudos_bitmap);
  1444. bitmap_clear (&spill_pseudos_bitmap);
  1445. bitmap_clear (&insn_conflict_pseudos);
  1446. }
  1447. /* Entry function to assign hard registers to new reload pseudos
  1448. starting with LRA_CONSTRAINT_NEW_REGNO_START (by possible spilling
  1449. of old pseudos) and possibly to the old pseudos. The function adds
  1450. what insns to process for the next constraint pass. Those are all
  1451. insns who contains non-reload and non-inheritance pseudos with
  1452. changed allocation.
  1453. Return true if we did not spill any non-reload and non-inheritance
  1454. pseudos. */
  1455. bool
  1456. lra_assign (void)
  1457. {
  1458. int i;
  1459. unsigned int u;
  1460. bitmap_iterator bi;
  1461. bitmap_head insns_to_process;
  1462. bool no_spills_p;
  1463. int max_regno = max_reg_num ();
  1464. timevar_push (TV_LRA_ASSIGN);
  1465. lra_assignment_iter++;
  1466. if (lra_dump_file != NULL)
  1467. fprintf (lra_dump_file, "\n********** Assignment #%d: **********\n\n",
  1468. lra_assignment_iter);
  1469. init_lives ();
  1470. sorted_pseudos = XNEWVEC (int, max_regno);
  1471. sorted_reload_pseudos = XNEWVEC (int, max_regno);
  1472. regno_allocno_class_array = XNEWVEC (enum reg_class, max_regno);
  1473. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1474. regno_allocno_class_array[i] = lra_get_allocno_class (i);
  1475. former_reload_pseudo_spill_p = false;
  1476. init_regno_assign_info ();
  1477. bitmap_initialize (&all_spilled_pseudos, &reg_obstack);
  1478. create_live_range_start_chains ();
  1479. setup_live_pseudos_and_spill_after_risky_transforms (&all_spilled_pseudos);
  1480. #ifdef ENABLE_CHECKING
  1481. if (!flag_ipa_ra)
  1482. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1483. if (lra_reg_info[i].nrefs != 0 && reg_renumber[i] >= 0
  1484. && lra_reg_info[i].call_p
  1485. && overlaps_hard_reg_set_p (call_used_reg_set,
  1486. PSEUDO_REGNO_MODE (i), reg_renumber[i]))
  1487. gcc_unreachable ();
  1488. #endif
  1489. /* Setup insns to process on the next constraint pass. */
  1490. bitmap_initialize (&changed_pseudo_bitmap, &reg_obstack);
  1491. init_live_reload_and_inheritance_pseudos ();
  1492. assign_by_spills ();
  1493. finish_live_reload_and_inheritance_pseudos ();
  1494. bitmap_ior_into (&changed_pseudo_bitmap, &all_spilled_pseudos);
  1495. no_spills_p = true;
  1496. EXECUTE_IF_SET_IN_BITMAP (&all_spilled_pseudos, 0, u, bi)
  1497. /* We ignore spilled pseudos created on last inheritance pass
  1498. because they will be removed. */
  1499. if (lra_reg_info[u].restore_regno < 0)
  1500. {
  1501. no_spills_p = false;
  1502. break;
  1503. }
  1504. finish_live_range_start_chains ();
  1505. bitmap_clear (&all_spilled_pseudos);
  1506. bitmap_initialize (&insns_to_process, &reg_obstack);
  1507. EXECUTE_IF_SET_IN_BITMAP (&changed_pseudo_bitmap, 0, u, bi)
  1508. bitmap_ior_into (&insns_to_process, &lra_reg_info[u].insn_bitmap);
  1509. bitmap_clear (&changed_pseudo_bitmap);
  1510. EXECUTE_IF_SET_IN_BITMAP (&insns_to_process, 0, u, bi)
  1511. {
  1512. lra_push_insn_by_uid (u);
  1513. /* Invalidate alternatives for insn should be processed. */
  1514. lra_set_used_insn_alternative_by_uid (u, -1);
  1515. }
  1516. bitmap_clear (&insns_to_process);
  1517. finish_regno_assign_info ();
  1518. free (regno_allocno_class_array);
  1519. free (sorted_pseudos);
  1520. free (sorted_reload_pseudos);
  1521. finish_lives ();
  1522. timevar_pop (TV_LRA_ASSIGN);
  1523. if (former_reload_pseudo_spill_p)
  1524. lra_assignment_iter_after_spill++;
  1525. if (lra_assignment_iter_after_spill > LRA_MAX_ASSIGNMENT_ITERATION_NUMBER)
  1526. internal_error
  1527. ("Maximum number of LRA assignment passes is achieved (%d)\n",
  1528. LRA_MAX_ASSIGNMENT_ITERATION_NUMBER);
  1529. return no_spills_p;
  1530. }