lra-lives.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /* Build live ranges for 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 contains code to build pseudo live-ranges (analogous
  17. structures used in IRA, so read comments about the live-ranges
  18. there) and other info necessary for other passes to assign
  19. hard-registers to pseudos, coalesce the spilled pseudos, and assign
  20. stack memory slots to spilled pseudos. */
  21. #include "config.h"
  22. #include "system.h"
  23. #include "coretypes.h"
  24. #include "tm.h"
  25. #include "hard-reg-set.h"
  26. #include "rtl.h"
  27. #include "tm_p.h"
  28. #include "insn-config.h"
  29. #include "recog.h"
  30. #include "output.h"
  31. #include "regs.h"
  32. #include "hashtab.h"
  33. #include "hash-set.h"
  34. #include "vec.h"
  35. #include "machmode.h"
  36. #include "input.h"
  37. #include "function.h"
  38. #include "symtab.h"
  39. #include "flags.h"
  40. #include "statistics.h"
  41. #include "double-int.h"
  42. #include "real.h"
  43. #include "fixed-value.h"
  44. #include "alias.h"
  45. #include "wide-int.h"
  46. #include "inchash.h"
  47. #include "tree.h"
  48. #include "expmed.h"
  49. #include "dojump.h"
  50. #include "explow.h"
  51. #include "calls.h"
  52. #include "emit-rtl.h"
  53. #include "varasm.h"
  54. #include "stmt.h"
  55. #include "expr.h"
  56. #include "predict.h"
  57. #include "dominance.h"
  58. #include "cfg.h"
  59. #include "cfganal.h"
  60. #include "basic-block.h"
  61. #include "except.h"
  62. #include "df.h"
  63. #include "ira.h"
  64. #include "sparseset.h"
  65. #include "lra-int.h"
  66. /* Program points are enumerated by numbers from range
  67. 0..LRA_LIVE_MAX_POINT-1. There are approximately two times more
  68. program points than insns. Program points are places in the
  69. program where liveness info can be changed. In most general case
  70. (there are more complicated cases too) some program points
  71. correspond to places where input operand dies and other ones
  72. correspond to places where output operands are born. */
  73. int lra_live_max_point;
  74. /* Accumulated execution frequency of all references for each hard
  75. register. */
  76. int lra_hard_reg_usage[FIRST_PSEUDO_REGISTER];
  77. /* A global flag whose true value says to build live ranges for all
  78. pseudos, otherwise the live ranges only for pseudos got memory is
  79. build. True value means also building copies and setting up hard
  80. register preferences. The complete info is necessary only for the
  81. assignment pass. The complete info is not needed for the
  82. coalescing and spill passes. */
  83. static bool complete_info_p;
  84. /* Pseudos live at current point in the RTL scan. */
  85. static sparseset pseudos_live;
  86. /* Pseudos probably living through calls and setjumps. As setjump is
  87. a call too, if a bit in PSEUDOS_LIVE_THROUGH_SETJUMPS is set up
  88. then the corresponding bit in PSEUDOS_LIVE_THROUGH_CALLS is set up
  89. too. These data are necessary for cases when only one subreg of a
  90. multi-reg pseudo is set up after a call. So we decide it is
  91. probably live when traversing bb backward. We are sure about
  92. living when we see its usage or definition of the pseudo. */
  93. static sparseset pseudos_live_through_calls;
  94. static sparseset pseudos_live_through_setjumps;
  95. /* Set of hard regs (except eliminable ones) currently live. */
  96. static HARD_REG_SET hard_regs_live;
  97. /* Set of pseudos and hard registers start living/dying in the current
  98. insn. These sets are used to update REG_DEAD and REG_UNUSED notes
  99. in the insn. */
  100. static sparseset start_living, start_dying;
  101. /* Set of pseudos and hard regs dead and unused in the current
  102. insn. */
  103. static sparseset unused_set, dead_set;
  104. /* Bitmap used for holding intermediate bitmap operation results. */
  105. static bitmap_head temp_bitmap;
  106. /* Pool for pseudo live ranges. */
  107. static alloc_pool live_range_pool;
  108. /* Free live range LR. */
  109. static void
  110. free_live_range (lra_live_range_t lr)
  111. {
  112. pool_free (live_range_pool, lr);
  113. }
  114. /* Free live range list LR. */
  115. static void
  116. free_live_range_list (lra_live_range_t lr)
  117. {
  118. lra_live_range_t next;
  119. while (lr != NULL)
  120. {
  121. next = lr->next;
  122. free_live_range (lr);
  123. lr = next;
  124. }
  125. }
  126. /* Create and return pseudo live range with given attributes. */
  127. static lra_live_range_t
  128. create_live_range (int regno, int start, int finish, lra_live_range_t next)
  129. {
  130. lra_live_range_t p;
  131. p = (lra_live_range_t) pool_alloc (live_range_pool);
  132. p->regno = regno;
  133. p->start = start;
  134. p->finish = finish;
  135. p->next = next;
  136. return p;
  137. }
  138. /* Copy live range R and return the result. */
  139. static lra_live_range_t
  140. copy_live_range (lra_live_range_t r)
  141. {
  142. lra_live_range_t p;
  143. p = (lra_live_range_t) pool_alloc (live_range_pool);
  144. *p = *r;
  145. return p;
  146. }
  147. /* Copy live range list given by its head R and return the result. */
  148. lra_live_range_t
  149. lra_copy_live_range_list (lra_live_range_t r)
  150. {
  151. lra_live_range_t p, first, *chain;
  152. first = NULL;
  153. for (chain = &first; r != NULL; r = r->next)
  154. {
  155. p = copy_live_range (r);
  156. *chain = p;
  157. chain = &p->next;
  158. }
  159. return first;
  160. }
  161. /* Merge *non-intersected* ranges R1 and R2 and returns the result.
  162. The function maintains the order of ranges and tries to minimize
  163. size of the result range list. Ranges R1 and R2 may not be used
  164. after the call. */
  165. lra_live_range_t
  166. lra_merge_live_ranges (lra_live_range_t r1, lra_live_range_t r2)
  167. {
  168. lra_live_range_t first, last, temp;
  169. if (r1 == NULL)
  170. return r2;
  171. if (r2 == NULL)
  172. return r1;
  173. for (first = last = NULL; r1 != NULL && r2 != NULL;)
  174. {
  175. if (r1->start < r2->start)
  176. {
  177. temp = r1;
  178. r1 = r2;
  179. r2 = temp;
  180. }
  181. if (r1->start == r2->finish + 1)
  182. {
  183. /* Joint ranges: merge r1 and r2 into r1. */
  184. r1->start = r2->start;
  185. temp = r2;
  186. r2 = r2->next;
  187. pool_free (live_range_pool, temp);
  188. }
  189. else
  190. {
  191. gcc_assert (r2->finish + 1 < r1->start);
  192. /* Add r1 to the result. */
  193. if (first == NULL)
  194. first = last = r1;
  195. else
  196. {
  197. last->next = r1;
  198. last = r1;
  199. }
  200. r1 = r1->next;
  201. }
  202. }
  203. if (r1 != NULL)
  204. {
  205. if (first == NULL)
  206. first = r1;
  207. else
  208. last->next = r1;
  209. }
  210. else
  211. {
  212. lra_assert (r2 != NULL);
  213. if (first == NULL)
  214. first = r2;
  215. else
  216. last->next = r2;
  217. }
  218. return first;
  219. }
  220. /* Return TRUE if live ranges R1 and R2 intersect. */
  221. bool
  222. lra_intersected_live_ranges_p (lra_live_range_t r1, lra_live_range_t r2)
  223. {
  224. /* Remember the live ranges are always kept ordered. */
  225. while (r1 != NULL && r2 != NULL)
  226. {
  227. if (r1->start > r2->finish)
  228. r1 = r1->next;
  229. else if (r2->start > r1->finish)
  230. r2 = r2->next;
  231. else
  232. return true;
  233. }
  234. return false;
  235. }
  236. /* The function processing birth of hard register REGNO. It updates
  237. living hard regs, START_LIVING, and conflict hard regs for living
  238. pseudos. Conflict hard regs for the pic pseudo is not updated if
  239. REGNO is REAL_PIC_OFFSET_TABLE_REGNUM and CHECK_PIC_PSEUDO_P is
  240. true. */
  241. static void
  242. make_hard_regno_born (int regno, bool check_pic_pseudo_p ATTRIBUTE_UNUSED)
  243. {
  244. unsigned int i;
  245. lra_assert (regno < FIRST_PSEUDO_REGISTER);
  246. if (TEST_HARD_REG_BIT (hard_regs_live, regno))
  247. return;
  248. SET_HARD_REG_BIT (hard_regs_live, regno);
  249. sparseset_set_bit (start_living, regno);
  250. EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
  251. #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
  252. if (! check_pic_pseudo_p
  253. || regno != REAL_PIC_OFFSET_TABLE_REGNUM
  254. || pic_offset_table_rtx == NULL
  255. || i != REGNO (pic_offset_table_rtx))
  256. #endif
  257. SET_HARD_REG_BIT (lra_reg_info[i].conflict_hard_regs, regno);
  258. }
  259. /* Process the death of hard register REGNO. This updates
  260. hard_regs_live and START_DYING. */
  261. static void
  262. make_hard_regno_dead (int regno)
  263. {
  264. lra_assert (regno < FIRST_PSEUDO_REGISTER);
  265. if (! TEST_HARD_REG_BIT (hard_regs_live, regno))
  266. return;
  267. sparseset_set_bit (start_dying, regno);
  268. CLEAR_HARD_REG_BIT (hard_regs_live, regno);
  269. }
  270. /* Mark pseudo REGNO as living at program point POINT, update conflicting
  271. hard registers of the pseudo and START_LIVING, and start a new live
  272. range for the pseudo corresponding to REGNO if it is necessary. */
  273. static void
  274. mark_pseudo_live (int regno, int point)
  275. {
  276. lra_live_range_t p;
  277. lra_assert (regno >= FIRST_PSEUDO_REGISTER);
  278. lra_assert (! sparseset_bit_p (pseudos_live, regno));
  279. sparseset_set_bit (pseudos_live, regno);
  280. IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs, hard_regs_live);
  281. if ((complete_info_p || lra_get_regno_hard_regno (regno) < 0)
  282. && ((p = lra_reg_info[regno].live_ranges) == NULL
  283. || (p->finish != point && p->finish + 1 != point)))
  284. lra_reg_info[regno].live_ranges
  285. = create_live_range (regno, point, -1, p);
  286. sparseset_set_bit (start_living, regno);
  287. }
  288. /* Mark pseudo REGNO as not living at program point POINT and update
  289. START_DYING.
  290. This finishes the current live range for the pseudo corresponding
  291. to REGNO. */
  292. static void
  293. mark_pseudo_dead (int regno, int point)
  294. {
  295. lra_live_range_t p;
  296. lra_assert (regno >= FIRST_PSEUDO_REGISTER);
  297. lra_assert (sparseset_bit_p (pseudos_live, regno));
  298. sparseset_clear_bit (pseudos_live, regno);
  299. sparseset_set_bit (start_dying, regno);
  300. if (complete_info_p || lra_get_regno_hard_regno (regno) < 0)
  301. {
  302. p = lra_reg_info[regno].live_ranges;
  303. lra_assert (p != NULL);
  304. p->finish = point;
  305. }
  306. }
  307. /* The corresponding bitmaps of BB currently being processed. */
  308. static bitmap bb_killed_pseudos, bb_gen_pseudos;
  309. /* Mark register REGNO (pseudo or hard register) in MODE as live at
  310. program point POINT. Update BB_GEN_PSEUDOS.
  311. Return TRUE if the liveness tracking sets were modified, or FALSE
  312. if nothing changed. */
  313. static bool
  314. mark_regno_live (int regno, machine_mode mode, int point)
  315. {
  316. int last;
  317. bool changed = false;
  318. if (regno < FIRST_PSEUDO_REGISTER)
  319. {
  320. for (last = regno + hard_regno_nregs[regno][mode];
  321. regno < last;
  322. regno++)
  323. make_hard_regno_born (regno, false);
  324. }
  325. else
  326. {
  327. if (! sparseset_bit_p (pseudos_live, regno))
  328. {
  329. mark_pseudo_live (regno, point);
  330. changed = true;
  331. }
  332. bitmap_set_bit (bb_gen_pseudos, regno);
  333. }
  334. return changed;
  335. }
  336. /* Mark register REGNO in MODE as dead at program point POINT. Update
  337. BB_GEN_PSEUDOS and BB_KILLED_PSEUDOS. Return TRUE if the liveness
  338. tracking sets were modified, or FALSE if nothing changed. */
  339. static bool
  340. mark_regno_dead (int regno, machine_mode mode, int point)
  341. {
  342. int last;
  343. bool changed = false;
  344. if (regno < FIRST_PSEUDO_REGISTER)
  345. {
  346. for (last = regno + hard_regno_nregs[regno][mode];
  347. regno < last;
  348. regno++)
  349. make_hard_regno_dead (regno);
  350. }
  351. else
  352. {
  353. if (sparseset_bit_p (pseudos_live, regno))
  354. {
  355. mark_pseudo_dead (regno, point);
  356. changed = true;
  357. }
  358. bitmap_clear_bit (bb_gen_pseudos, regno);
  359. bitmap_set_bit (bb_killed_pseudos, regno);
  360. }
  361. return changed;
  362. }
  363. /* This page contains code for making global live analysis of pseudos.
  364. The code works only when pseudo live info is changed on a BB
  365. border. That might be a consequence of some global transformations
  366. in LRA, e.g. PIC pseudo reuse or rematerialization. */
  367. /* Structure describing local BB data used for pseudo
  368. live-analysis. */
  369. struct bb_data_pseudos
  370. {
  371. /* Basic block about which the below data are. */
  372. basic_block bb;
  373. bitmap_head killed_pseudos; /* pseudos killed in the BB. */
  374. bitmap_head gen_pseudos; /* pseudos generated in the BB. */
  375. };
  376. /* Array for all BB data. Indexed by the corresponding BB index. */
  377. typedef struct bb_data_pseudos *bb_data_t;
  378. /* All basic block data are referred through the following array. */
  379. static bb_data_t bb_data;
  380. /* Two small functions for access to the bb data. */
  381. static inline bb_data_t
  382. get_bb_data (basic_block bb)
  383. {
  384. return &bb_data[(bb)->index];
  385. }
  386. static inline bb_data_t
  387. get_bb_data_by_index (int index)
  388. {
  389. return &bb_data[index];
  390. }
  391. /* Bitmap with all hard regs. */
  392. static bitmap_head all_hard_regs_bitmap;
  393. /* The transfer function used by the DF equation solver to propagate
  394. live info through block with BB_INDEX according to the following
  395. equation:
  396. bb.livein = (bb.liveout - bb.kill) OR bb.gen
  397. */
  398. static bool
  399. live_trans_fun (int bb_index)
  400. {
  401. basic_block bb = get_bb_data_by_index (bb_index)->bb;
  402. bitmap bb_liveout = df_get_live_out (bb);
  403. bitmap bb_livein = df_get_live_in (bb);
  404. bb_data_t bb_info = get_bb_data (bb);
  405. bitmap_and_compl (&temp_bitmap, bb_liveout, &all_hard_regs_bitmap);
  406. return bitmap_ior_and_compl (bb_livein, &bb_info->gen_pseudos,
  407. &temp_bitmap, &bb_info->killed_pseudos);
  408. }
  409. /* The confluence function used by the DF equation solver to set up
  410. live info for a block BB without predecessor. */
  411. static void
  412. live_con_fun_0 (basic_block bb)
  413. {
  414. bitmap_and_into (df_get_live_out (bb), &all_hard_regs_bitmap);
  415. }
  416. /* The confluence function used by the DF equation solver to propagate
  417. live info from successor to predecessor on edge E according to the
  418. following equation:
  419. bb.liveout = 0 for entry block | OR (livein of successors)
  420. */
  421. static bool
  422. live_con_fun_n (edge e)
  423. {
  424. basic_block bb = e->src;
  425. basic_block dest = e->dest;
  426. bitmap bb_liveout = df_get_live_out (bb);
  427. bitmap dest_livein = df_get_live_in (dest);
  428. return bitmap_ior_and_compl_into (bb_liveout,
  429. dest_livein, &all_hard_regs_bitmap);
  430. }
  431. /* Indexes of all function blocks. */
  432. static bitmap_head all_blocks;
  433. /* Allocate and initialize data needed for global pseudo live
  434. analysis. */
  435. static void
  436. initiate_live_solver (void)
  437. {
  438. bitmap_initialize (&all_hard_regs_bitmap, &reg_obstack);
  439. bitmap_set_range (&all_hard_regs_bitmap, 0, FIRST_PSEUDO_REGISTER);
  440. bb_data = XNEWVEC (struct bb_data_pseudos, last_basic_block_for_fn (cfun));
  441. bitmap_initialize (&all_blocks, &reg_obstack);
  442. basic_block bb;
  443. FOR_ALL_BB_FN (bb, cfun)
  444. {
  445. bb_data_t bb_info = get_bb_data (bb);
  446. bb_info->bb = bb;
  447. bitmap_initialize (&bb_info->killed_pseudos, &reg_obstack);
  448. bitmap_initialize (&bb_info->gen_pseudos, &reg_obstack);
  449. bitmap_set_bit (&all_blocks, bb->index);
  450. }
  451. }
  452. /* Free all data needed for global pseudo live analysis. */
  453. static void
  454. finish_live_solver (void)
  455. {
  456. basic_block bb;
  457. bitmap_clear (&all_blocks);
  458. FOR_ALL_BB_FN (bb, cfun)
  459. {
  460. bb_data_t bb_info = get_bb_data (bb);
  461. bitmap_clear (&bb_info->killed_pseudos);
  462. bitmap_clear (&bb_info->gen_pseudos);
  463. }
  464. free (bb_data);
  465. bitmap_clear (&all_hard_regs_bitmap);
  466. }
  467. /* Insn currently scanned. */
  468. static rtx_insn *curr_insn;
  469. /* The insn data. */
  470. static lra_insn_recog_data_t curr_id;
  471. /* The insn static data. */
  472. static struct lra_static_insn_data *curr_static_id;
  473. /* Return true when one of the predecessor edges of BB is marked with
  474. EDGE_ABNORMAL_CALL or EDGE_EH. */
  475. static bool
  476. bb_has_abnormal_call_pred (basic_block bb)
  477. {
  478. edge e;
  479. edge_iterator ei;
  480. FOR_EACH_EDGE (e, ei, bb->preds)
  481. {
  482. if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
  483. return true;
  484. }
  485. return false;
  486. }
  487. /* Vec containing execution frequencies of program points. */
  488. static vec<int> point_freq_vec;
  489. /* The start of the above vector elements. */
  490. int *lra_point_freq;
  491. /* Increment the current program point POINT to the next point which has
  492. execution frequency FREQ. */
  493. static void
  494. next_program_point (int &point, int freq)
  495. {
  496. point_freq_vec.safe_push (freq);
  497. lra_point_freq = point_freq_vec.address ();
  498. point++;
  499. }
  500. /* Update the preference of HARD_REGNO for pseudo REGNO by PROFIT. */
  501. void
  502. lra_setup_reload_pseudo_preferenced_hard_reg (int regno,
  503. int hard_regno, int profit)
  504. {
  505. lra_assert (regno >= lra_constraint_new_regno_start);
  506. if (lra_reg_info[regno].preferred_hard_regno1 == hard_regno)
  507. lra_reg_info[regno].preferred_hard_regno_profit1 += profit;
  508. else if (lra_reg_info[regno].preferred_hard_regno2 == hard_regno)
  509. lra_reg_info[regno].preferred_hard_regno_profit2 += profit;
  510. else if (lra_reg_info[regno].preferred_hard_regno1 < 0)
  511. {
  512. lra_reg_info[regno].preferred_hard_regno1 = hard_regno;
  513. lra_reg_info[regno].preferred_hard_regno_profit1 = profit;
  514. }
  515. else if (lra_reg_info[regno].preferred_hard_regno2 < 0
  516. || profit > lra_reg_info[regno].preferred_hard_regno_profit2)
  517. {
  518. lra_reg_info[regno].preferred_hard_regno2 = hard_regno;
  519. lra_reg_info[regno].preferred_hard_regno_profit2 = profit;
  520. }
  521. else
  522. return;
  523. /* Keep the 1st hard regno as more profitable. */
  524. if (lra_reg_info[regno].preferred_hard_regno1 >= 0
  525. && lra_reg_info[regno].preferred_hard_regno2 >= 0
  526. && (lra_reg_info[regno].preferred_hard_regno_profit2
  527. > lra_reg_info[regno].preferred_hard_regno_profit1))
  528. {
  529. int temp;
  530. temp = lra_reg_info[regno].preferred_hard_regno1;
  531. lra_reg_info[regno].preferred_hard_regno1
  532. = lra_reg_info[regno].preferred_hard_regno2;
  533. lra_reg_info[regno].preferred_hard_regno2 = temp;
  534. temp = lra_reg_info[regno].preferred_hard_regno_profit1;
  535. lra_reg_info[regno].preferred_hard_regno_profit1
  536. = lra_reg_info[regno].preferred_hard_regno_profit2;
  537. lra_reg_info[regno].preferred_hard_regno_profit2 = temp;
  538. }
  539. if (lra_dump_file != NULL)
  540. {
  541. if ((hard_regno = lra_reg_info[regno].preferred_hard_regno1) >= 0)
  542. fprintf (lra_dump_file,
  543. " Hard reg %d is preferable by r%d with profit %d\n",
  544. hard_regno, regno,
  545. lra_reg_info[regno].preferred_hard_regno_profit1);
  546. if ((hard_regno = lra_reg_info[regno].preferred_hard_regno2) >= 0)
  547. fprintf (lra_dump_file,
  548. " Hard reg %d is preferable by r%d with profit %d\n",
  549. hard_regno, regno,
  550. lra_reg_info[regno].preferred_hard_regno_profit2);
  551. }
  552. }
  553. /* Check that REGNO living through calls and setjumps, set up conflict
  554. regs, and clear corresponding bits in PSEUDOS_LIVE_THROUGH_CALLS and
  555. PSEUDOS_LIVE_THROUGH_SETJUMPS. */
  556. static inline void
  557. check_pseudos_live_through_calls (int regno)
  558. {
  559. int hr;
  560. if (! sparseset_bit_p (pseudos_live_through_calls, regno))
  561. return;
  562. sparseset_clear_bit (pseudos_live_through_calls, regno);
  563. IOR_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs,
  564. call_used_reg_set);
  565. for (hr = 0; hr < FIRST_PSEUDO_REGISTER; hr++)
  566. if (HARD_REGNO_CALL_PART_CLOBBERED (hr, PSEUDO_REGNO_MODE (regno)))
  567. SET_HARD_REG_BIT (lra_reg_info[regno].conflict_hard_regs, hr);
  568. #ifdef ENABLE_CHECKING
  569. lra_reg_info[regno].call_p = true;
  570. #endif
  571. if (! sparseset_bit_p (pseudos_live_through_setjumps, regno))
  572. return;
  573. sparseset_clear_bit (pseudos_live_through_setjumps, regno);
  574. /* Don't allocate pseudos that cross setjmps or any call, if this
  575. function receives a nonlocal goto. */
  576. SET_HARD_REG_SET (lra_reg_info[regno].conflict_hard_regs);
  577. }
  578. /* Process insns of the basic block BB to update pseudo live ranges,
  579. pseudo hard register conflicts, and insn notes. We do it on
  580. backward scan of BB insns. CURR_POINT is the program point where
  581. BB ends. The function updates this counter and returns in
  582. CURR_POINT the program point where BB starts. The function also
  583. does local live info updates and can delete the dead insns if
  584. DEAD_INSN_P. It returns true if pseudo live info was
  585. changed at the BB start. */
  586. static bool
  587. process_bb_lives (basic_block bb, int &curr_point, bool dead_insn_p)
  588. {
  589. int i, regno, freq;
  590. unsigned int j;
  591. bitmap_iterator bi;
  592. bitmap reg_live_out;
  593. unsigned int px;
  594. rtx_insn *next;
  595. rtx link, *link_loc;
  596. bool need_curr_point_incr;
  597. reg_live_out = df_get_live_out (bb);
  598. sparseset_clear (pseudos_live);
  599. sparseset_clear (pseudos_live_through_calls);
  600. sparseset_clear (pseudos_live_through_setjumps);
  601. REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out);
  602. AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
  603. EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
  604. mark_pseudo_live (j, curr_point);
  605. bb_gen_pseudos = &get_bb_data (bb)->gen_pseudos;
  606. bb_killed_pseudos = &get_bb_data (bb)->killed_pseudos;
  607. bitmap_clear (bb_gen_pseudos);
  608. bitmap_clear (bb_killed_pseudos);
  609. freq = REG_FREQ_FROM_BB (bb);
  610. if (lra_dump_file != NULL)
  611. fprintf (lra_dump_file, " BB %d\n", bb->index);
  612. /* Scan the code of this basic block, noting which pseudos and hard
  613. regs are born or die.
  614. Note that this loop treats uninitialized values as live until the
  615. beginning of the block. For example, if an instruction uses
  616. (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever set,
  617. FOO will remain live until the beginning of the block. Likewise
  618. if FOO is not set at all. This is unnecessarily pessimistic, but
  619. it probably doesn't matter much in practice. */
  620. FOR_BB_INSNS_REVERSE_SAFE (bb, curr_insn, next)
  621. {
  622. bool call_p;
  623. int dst_regno, src_regno;
  624. rtx set;
  625. struct lra_insn_reg *reg;
  626. if (!NONDEBUG_INSN_P (curr_insn))
  627. continue;
  628. curr_id = lra_get_insn_recog_data (curr_insn);
  629. curr_static_id = curr_id->insn_static_data;
  630. if (lra_dump_file != NULL)
  631. fprintf (lra_dump_file, " Insn %u: point = %d\n",
  632. INSN_UID (curr_insn), curr_point);
  633. set = single_set (curr_insn);
  634. if (dead_insn_p && set != NULL_RTX
  635. && REG_P (SET_DEST (set)) && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
  636. && find_reg_note (curr_insn, REG_EH_REGION, NULL_RTX) == NULL_RTX
  637. && ! may_trap_p (PATTERN (curr_insn))
  638. /* Don't do premature remove of pic offset pseudo as we can
  639. start to use it after some reload generation. */
  640. && (pic_offset_table_rtx == NULL_RTX
  641. || pic_offset_table_rtx != SET_DEST (set)))
  642. {
  643. bool remove_p = true;
  644. for (reg = curr_id->regs; reg != NULL; reg = reg->next)
  645. if (reg->type != OP_IN && sparseset_bit_p (pseudos_live, reg->regno))
  646. {
  647. remove_p = false;
  648. break;
  649. }
  650. for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
  651. if (reg->type != OP_IN)
  652. {
  653. remove_p = false;
  654. break;
  655. }
  656. if (remove_p && ! volatile_refs_p (PATTERN (curr_insn)))
  657. {
  658. dst_regno = REGNO (SET_DEST (set));
  659. if (lra_dump_file != NULL)
  660. fprintf (lra_dump_file, " Deleting dead insn %u\n",
  661. INSN_UID (curr_insn));
  662. lra_set_insn_deleted (curr_insn);
  663. if (lra_reg_info[dst_regno].nrefs == 0)
  664. {
  665. /* There might be some debug insns with the pseudo. */
  666. unsigned int uid;
  667. rtx_insn *insn;
  668. bitmap_copy (&temp_bitmap, &lra_reg_info[dst_regno].insn_bitmap);
  669. EXECUTE_IF_SET_IN_BITMAP (&temp_bitmap, 0, uid, bi)
  670. {
  671. insn = lra_insn_recog_data[uid]->insn;
  672. lra_substitute_pseudo_within_insn (insn, dst_regno,
  673. SET_SRC (set));
  674. lra_update_insn_regno_info (insn);
  675. }
  676. }
  677. continue;
  678. }
  679. }
  680. /* Update max ref width and hard reg usage. */
  681. for (reg = curr_id->regs; reg != NULL; reg = reg->next)
  682. if (reg->regno >= FIRST_PSEUDO_REGISTER
  683. && (GET_MODE_SIZE (reg->biggest_mode)
  684. > GET_MODE_SIZE (lra_reg_info[reg->regno].biggest_mode)))
  685. lra_reg_info[reg->regno].biggest_mode = reg->biggest_mode;
  686. else if (reg->regno < FIRST_PSEUDO_REGISTER)
  687. lra_hard_reg_usage[reg->regno] += freq;
  688. call_p = CALL_P (curr_insn);
  689. if (complete_info_p
  690. && set != NULL_RTX
  691. && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set))
  692. /* Check that source regno does not conflict with
  693. destination regno to exclude most impossible
  694. preferences. */
  695. && ((((src_regno = REGNO (SET_SRC (set))) >= FIRST_PSEUDO_REGISTER
  696. && ! sparseset_bit_p (pseudos_live, src_regno))
  697. || (src_regno < FIRST_PSEUDO_REGISTER
  698. && ! TEST_HARD_REG_BIT (hard_regs_live, src_regno)))
  699. /* It might be 'inheritance pseudo <- reload pseudo'. */
  700. || (src_regno >= lra_constraint_new_regno_start
  701. && ((int) REGNO (SET_DEST (set))
  702. >= lra_constraint_new_regno_start)
  703. /* Remember to skip special cases where src/dest regnos are
  704. the same, e.g. insn SET pattern has matching constraints
  705. like =r,0. */
  706. && src_regno != (int) REGNO (SET_DEST (set)))))
  707. {
  708. int hard_regno = -1, regno = -1;
  709. dst_regno = REGNO (SET_DEST (set));
  710. if (dst_regno >= lra_constraint_new_regno_start
  711. && src_regno >= lra_constraint_new_regno_start)
  712. {
  713. /* It might be still an original (non-reload) insn with
  714. one unused output and a constraint requiring to use
  715. the same reg for input/output operands. In this case
  716. dst_regno and src_regno have the same value, we don't
  717. need a misleading copy for this case. */
  718. if (dst_regno != src_regno)
  719. lra_create_copy (dst_regno, src_regno, freq);
  720. }
  721. else if (dst_regno >= lra_constraint_new_regno_start)
  722. {
  723. if ((hard_regno = src_regno) >= FIRST_PSEUDO_REGISTER)
  724. hard_regno = reg_renumber[src_regno];
  725. regno = dst_regno;
  726. }
  727. else if (src_regno >= lra_constraint_new_regno_start)
  728. {
  729. if ((hard_regno = dst_regno) >= FIRST_PSEUDO_REGISTER)
  730. hard_regno = reg_renumber[dst_regno];
  731. regno = src_regno;
  732. }
  733. if (regno >= 0 && hard_regno >= 0)
  734. lra_setup_reload_pseudo_preferenced_hard_reg
  735. (regno, hard_regno, freq);
  736. }
  737. sparseset_clear (start_living);
  738. /* Try to avoid unnecessary program point increments, this saves
  739. a lot of time in remove_some_program_points_and_update_live_ranges.
  740. We only need an increment if something becomes live or dies at this
  741. program point. */
  742. need_curr_point_incr = false;
  743. /* Mark each defined value as live. We need to do this for
  744. unused values because they still conflict with quantities
  745. that are live at the time of the definition. */
  746. for (reg = curr_id->regs; reg != NULL; reg = reg->next)
  747. if (reg->type != OP_IN)
  748. {
  749. need_curr_point_incr
  750. |= mark_regno_live (reg->regno, reg->biggest_mode,
  751. curr_point);
  752. check_pseudos_live_through_calls (reg->regno);
  753. }
  754. for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
  755. if (reg->type != OP_IN)
  756. make_hard_regno_born (reg->regno, false);
  757. if (curr_id->arg_hard_regs != NULL)
  758. for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
  759. if (regno >= FIRST_PSEUDO_REGISTER)
  760. /* It is a clobber. */
  761. make_hard_regno_born (regno - FIRST_PSEUDO_REGISTER, false);
  762. sparseset_copy (unused_set, start_living);
  763. sparseset_clear (start_dying);
  764. /* See which defined values die here. */
  765. for (reg = curr_id->regs; reg != NULL; reg = reg->next)
  766. if (reg->type == OP_OUT && ! reg->early_clobber && ! reg->subreg_p)
  767. need_curr_point_incr
  768. |= mark_regno_dead (reg->regno, reg->biggest_mode,
  769. curr_point);
  770. for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
  771. if (reg->type == OP_OUT && ! reg->early_clobber && ! reg->subreg_p)
  772. make_hard_regno_dead (reg->regno);
  773. if (curr_id->arg_hard_regs != NULL)
  774. for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
  775. if (regno >= FIRST_PSEUDO_REGISTER)
  776. /* It is a clobber. */
  777. make_hard_regno_dead (regno - FIRST_PSEUDO_REGISTER);
  778. if (call_p)
  779. {
  780. if (flag_ipa_ra)
  781. {
  782. HARD_REG_SET this_call_used_reg_set;
  783. get_call_reg_set_usage (curr_insn, &this_call_used_reg_set,
  784. call_used_reg_set);
  785. EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j)
  786. IOR_HARD_REG_SET (lra_reg_info[j].actual_call_used_reg_set,
  787. this_call_used_reg_set);
  788. }
  789. sparseset_ior (pseudos_live_through_calls,
  790. pseudos_live_through_calls, pseudos_live);
  791. if (cfun->has_nonlocal_label
  792. || find_reg_note (curr_insn, REG_SETJMP,
  793. NULL_RTX) != NULL_RTX)
  794. sparseset_ior (pseudos_live_through_setjumps,
  795. pseudos_live_through_setjumps, pseudos_live);
  796. }
  797. /* Increment the current program point if we must. */
  798. if (need_curr_point_incr)
  799. next_program_point (curr_point, freq);
  800. sparseset_clear (start_living);
  801. need_curr_point_incr = false;
  802. /* Mark each used value as live. */
  803. for (reg = curr_id->regs; reg != NULL; reg = reg->next)
  804. if (reg->type == OP_IN)
  805. {
  806. need_curr_point_incr
  807. |= mark_regno_live (reg->regno, reg->biggest_mode,
  808. curr_point);
  809. check_pseudos_live_through_calls (reg->regno);
  810. }
  811. for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
  812. if (reg->type == OP_IN)
  813. make_hard_regno_born (reg->regno, false);
  814. if (curr_id->arg_hard_regs != NULL)
  815. /* Make argument hard registers live. Don't create conflict
  816. of used REAL_PIC_OFFSET_TABLE_REGNUM and the pic pseudo. */
  817. for (i = 0; (regno = curr_id->arg_hard_regs[i]) >= 0; i++)
  818. if (regno < FIRST_PSEUDO_REGISTER)
  819. make_hard_regno_born (regno, true);
  820. sparseset_and_compl (dead_set, start_living, start_dying);
  821. /* Mark early clobber outputs dead. */
  822. for (reg = curr_id->regs; reg != NULL; reg = reg->next)
  823. if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p)
  824. need_curr_point_incr
  825. |= mark_regno_dead (reg->regno, reg->biggest_mode,
  826. curr_point);
  827. for (reg = curr_static_id->hard_regs; reg != NULL; reg = reg->next)
  828. if (reg->type == OP_OUT && reg->early_clobber && ! reg->subreg_p)
  829. make_hard_regno_dead (reg->regno);
  830. if (need_curr_point_incr)
  831. next_program_point (curr_point, freq);
  832. /* Update notes. */
  833. for (link_loc = &REG_NOTES (curr_insn); (link = *link_loc) != NULL_RTX;)
  834. {
  835. if (REG_NOTE_KIND (link) != REG_DEAD
  836. && REG_NOTE_KIND (link) != REG_UNUSED)
  837. ;
  838. else if (REG_P (XEXP (link, 0)))
  839. {
  840. regno = REGNO (XEXP (link, 0));
  841. if ((REG_NOTE_KIND (link) == REG_DEAD
  842. && ! sparseset_bit_p (dead_set, regno))
  843. || (REG_NOTE_KIND (link) == REG_UNUSED
  844. && ! sparseset_bit_p (unused_set, regno)))
  845. {
  846. *link_loc = XEXP (link, 1);
  847. continue;
  848. }
  849. if (REG_NOTE_KIND (link) == REG_DEAD)
  850. sparseset_clear_bit (dead_set, regno);
  851. else if (REG_NOTE_KIND (link) == REG_UNUSED)
  852. sparseset_clear_bit (unused_set, regno);
  853. }
  854. link_loc = &XEXP (link, 1);
  855. }
  856. EXECUTE_IF_SET_IN_SPARSESET (dead_set, j)
  857. add_reg_note (curr_insn, REG_DEAD, regno_reg_rtx[j]);
  858. EXECUTE_IF_SET_IN_SPARSESET (unused_set, j)
  859. add_reg_note (curr_insn, REG_UNUSED, regno_reg_rtx[j]);
  860. }
  861. #ifdef EH_RETURN_DATA_REGNO
  862. if (bb_has_eh_pred (bb))
  863. for (j = 0; ; ++j)
  864. {
  865. unsigned int regno = EH_RETURN_DATA_REGNO (j);
  866. if (regno == INVALID_REGNUM)
  867. break;
  868. make_hard_regno_born (regno, false);
  869. }
  870. #endif
  871. /* Pseudos can't go in stack regs at the start of a basic block that
  872. is reached by an abnormal edge. Likewise for call clobbered regs,
  873. because caller-save, fixup_abnormal_edges and possibly the table
  874. driven EH machinery are not quite ready to handle such pseudos
  875. live across such edges. */
  876. if (bb_has_abnormal_pred (bb))
  877. {
  878. #ifdef STACK_REGS
  879. EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, px)
  880. lra_reg_info[px].no_stack_p = true;
  881. for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++)
  882. make_hard_regno_born (px, false);
  883. #endif
  884. /* No need to record conflicts for call clobbered regs if we
  885. have nonlocal labels around, as we don't ever try to
  886. allocate such regs in this case. */
  887. if (!cfun->has_nonlocal_label && bb_has_abnormal_call_pred (bb))
  888. for (px = 0; px < FIRST_PSEUDO_REGISTER; px++)
  889. if (call_used_regs[px])
  890. make_hard_regno_born (px, false);
  891. }
  892. bool live_change_p = false;
  893. /* Check if bb border live info was changed. */
  894. unsigned int live_pseudos_num = 0;
  895. EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb),
  896. FIRST_PSEUDO_REGISTER, j, bi)
  897. {
  898. live_pseudos_num++;
  899. if (! sparseset_bit_p (pseudos_live, j))
  900. {
  901. live_change_p = true;
  902. if (lra_dump_file != NULL)
  903. fprintf (lra_dump_file,
  904. " r%d is removed as live at bb%d start\n", j, bb->index);
  905. break;
  906. }
  907. }
  908. if (! live_change_p
  909. && sparseset_cardinality (pseudos_live) != live_pseudos_num)
  910. {
  911. live_change_p = true;
  912. if (lra_dump_file != NULL)
  913. EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, j)
  914. if (! bitmap_bit_p (df_get_live_in (bb), j))
  915. fprintf (lra_dump_file,
  916. " r%d is added to live at bb%d start\n", j, bb->index);
  917. }
  918. /* See if we'll need an increment at the end of this basic block.
  919. An increment is needed if the PSEUDOS_LIVE set is not empty,
  920. to make sure the finish points are set up correctly. */
  921. need_curr_point_incr = (sparseset_cardinality (pseudos_live) > 0);
  922. EXECUTE_IF_SET_IN_SPARSESET (pseudos_live, i)
  923. mark_pseudo_dead (i, curr_point);
  924. EXECUTE_IF_SET_IN_BITMAP (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, j, bi)
  925. {
  926. if (sparseset_cardinality (pseudos_live_through_calls) == 0)
  927. break;
  928. if (sparseset_bit_p (pseudos_live_through_calls, j))
  929. check_pseudos_live_through_calls (j);
  930. }
  931. if (need_curr_point_incr)
  932. next_program_point (curr_point, freq);
  933. return live_change_p;
  934. }
  935. /* Compress pseudo live ranges by removing program points where
  936. nothing happens. Complexity of many algorithms in LRA is linear
  937. function of program points number. To speed up the code we try to
  938. minimize the number of the program points here. */
  939. static void
  940. remove_some_program_points_and_update_live_ranges (void)
  941. {
  942. unsigned i;
  943. int n, max_regno;
  944. int *map;
  945. lra_live_range_t r, prev_r, next_r;
  946. sbitmap born_or_dead, born, dead;
  947. sbitmap_iterator sbi;
  948. bool born_p, dead_p, prev_born_p, prev_dead_p;
  949. born = sbitmap_alloc (lra_live_max_point);
  950. dead = sbitmap_alloc (lra_live_max_point);
  951. bitmap_clear (born);
  952. bitmap_clear (dead);
  953. max_regno = max_reg_num ();
  954. for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
  955. {
  956. for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
  957. {
  958. lra_assert (r->start <= r->finish);
  959. bitmap_set_bit (born, r->start);
  960. bitmap_set_bit (dead, r->finish);
  961. }
  962. }
  963. born_or_dead = sbitmap_alloc (lra_live_max_point);
  964. bitmap_ior (born_or_dead, born, dead);
  965. map = XCNEWVEC (int, lra_live_max_point);
  966. n = -1;
  967. prev_born_p = prev_dead_p = false;
  968. EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
  969. {
  970. born_p = bitmap_bit_p (born, i);
  971. dead_p = bitmap_bit_p (dead, i);
  972. if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
  973. || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
  974. {
  975. map[i] = n;
  976. lra_point_freq[n] = MAX (lra_point_freq[n], lra_point_freq[i]);
  977. }
  978. else
  979. {
  980. map[i] = ++n;
  981. lra_point_freq[n] = lra_point_freq[i];
  982. }
  983. prev_born_p = born_p;
  984. prev_dead_p = dead_p;
  985. }
  986. sbitmap_free (born_or_dead);
  987. sbitmap_free (born);
  988. sbitmap_free (dead);
  989. n++;
  990. if (lra_dump_file != NULL)
  991. fprintf (lra_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
  992. lra_live_max_point, n, 100 * n / lra_live_max_point);
  993. if (n < lra_live_max_point)
  994. {
  995. lra_live_max_point = n;
  996. for (i = FIRST_PSEUDO_REGISTER; i < (unsigned) max_regno; i++)
  997. {
  998. for (prev_r = NULL, r = lra_reg_info[i].live_ranges;
  999. r != NULL;
  1000. r = next_r)
  1001. {
  1002. next_r = r->next;
  1003. r->start = map[r->start];
  1004. r->finish = map[r->finish];
  1005. if (prev_r == NULL || prev_r->start > r->finish + 1)
  1006. {
  1007. prev_r = r;
  1008. continue;
  1009. }
  1010. prev_r->start = r->start;
  1011. prev_r->next = next_r;
  1012. free_live_range (r);
  1013. }
  1014. }
  1015. }
  1016. free (map);
  1017. }
  1018. /* Print live ranges R to file F. */
  1019. void
  1020. lra_print_live_range_list (FILE *f, lra_live_range_t r)
  1021. {
  1022. for (; r != NULL; r = r->next)
  1023. fprintf (f, " [%d..%d]", r->start, r->finish);
  1024. fprintf (f, "\n");
  1025. }
  1026. DEBUG_FUNCTION void
  1027. debug (lra_live_range &ref)
  1028. {
  1029. lra_print_live_range_list (stderr, &ref);
  1030. }
  1031. DEBUG_FUNCTION void
  1032. debug (lra_live_range *ptr)
  1033. {
  1034. if (ptr)
  1035. debug (*ptr);
  1036. else
  1037. fprintf (stderr, "<nil>\n");
  1038. }
  1039. /* Print live ranges R to stderr. */
  1040. void
  1041. lra_debug_live_range_list (lra_live_range_t r)
  1042. {
  1043. lra_print_live_range_list (stderr, r);
  1044. }
  1045. /* Print live ranges of pseudo REGNO to file F. */
  1046. static void
  1047. print_pseudo_live_ranges (FILE *f, int regno)
  1048. {
  1049. if (lra_reg_info[regno].live_ranges == NULL)
  1050. return;
  1051. fprintf (f, " r%d:", regno);
  1052. lra_print_live_range_list (f, lra_reg_info[regno].live_ranges);
  1053. }
  1054. /* Print live ranges of pseudo REGNO to stderr. */
  1055. void
  1056. lra_debug_pseudo_live_ranges (int regno)
  1057. {
  1058. print_pseudo_live_ranges (stderr, regno);
  1059. }
  1060. /* Print live ranges of all pseudos to file F. */
  1061. static void
  1062. print_live_ranges (FILE *f)
  1063. {
  1064. int i, max_regno;
  1065. max_regno = max_reg_num ();
  1066. for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  1067. print_pseudo_live_ranges (f, i);
  1068. }
  1069. /* Print live ranges of all pseudos to stderr. */
  1070. void
  1071. lra_debug_live_ranges (void)
  1072. {
  1073. print_live_ranges (stderr);
  1074. }
  1075. /* Compress pseudo live ranges. */
  1076. static void
  1077. compress_live_ranges (void)
  1078. {
  1079. remove_some_program_points_and_update_live_ranges ();
  1080. if (lra_dump_file != NULL)
  1081. {
  1082. fprintf (lra_dump_file, "Ranges after the compression:\n");
  1083. print_live_ranges (lra_dump_file);
  1084. }
  1085. }
  1086. /* The number of the current live range pass. */
  1087. int lra_live_range_iter;
  1088. /* The function creates live ranges only for memory pseudos (or for
  1089. all ones if ALL_P), set up CONFLICT_HARD_REGS for the pseudos. It
  1090. also does dead insn elimination if DEAD_INSN_P and global live
  1091. analysis only for pseudos and only if the pseudo live info was
  1092. changed on a BB border. Return TRUE if the live info was
  1093. changed. */
  1094. static bool
  1095. lra_create_live_ranges_1 (bool all_p, bool dead_insn_p)
  1096. {
  1097. basic_block bb;
  1098. int i, hard_regno, max_regno = max_reg_num ();
  1099. int curr_point;
  1100. bool bb_live_change_p, have_referenced_pseudos = false;
  1101. timevar_push (TV_LRA_CREATE_LIVE_RANGES);
  1102. complete_info_p = all_p;
  1103. if (lra_dump_file != NULL)
  1104. fprintf (lra_dump_file,
  1105. "\n********** Pseudo live ranges #%d: **********\n\n",
  1106. ++lra_live_range_iter);
  1107. memset (lra_hard_reg_usage, 0, sizeof (lra_hard_reg_usage));
  1108. for (i = 0; i < max_regno; i++)
  1109. {
  1110. lra_reg_info[i].live_ranges = NULL;
  1111. CLEAR_HARD_REG_SET (lra_reg_info[i].conflict_hard_regs);
  1112. lra_reg_info[i].preferred_hard_regno1 = -1;
  1113. lra_reg_info[i].preferred_hard_regno2 = -1;
  1114. lra_reg_info[i].preferred_hard_regno_profit1 = 0;
  1115. lra_reg_info[i].preferred_hard_regno_profit2 = 0;
  1116. #ifdef STACK_REGS
  1117. lra_reg_info[i].no_stack_p = false;
  1118. #endif
  1119. /* The biggest mode is already set but its value might be to
  1120. conservative because of recent transformation. Here in this
  1121. file we recalculate it again as it costs practically
  1122. nothing. */
  1123. if (regno_reg_rtx[i] != NULL_RTX)
  1124. lra_reg_info[i].biggest_mode = GET_MODE (regno_reg_rtx[i]);
  1125. else
  1126. lra_reg_info[i].biggest_mode = VOIDmode;
  1127. #ifdef ENABLE_CHECKING
  1128. lra_reg_info[i].call_p = false;
  1129. #endif
  1130. if (i >= FIRST_PSEUDO_REGISTER
  1131. && lra_reg_info[i].nrefs != 0)
  1132. {
  1133. if ((hard_regno = reg_renumber[i]) >= 0)
  1134. lra_hard_reg_usage[hard_regno] += lra_reg_info[i].freq;
  1135. have_referenced_pseudos = true;
  1136. }
  1137. }
  1138. lra_free_copies ();
  1139. /* Under some circumstances, we can have functions without pseudo
  1140. registers. For such functions, lra_live_max_point will be 0,
  1141. see e.g. PR55604, and there's nothing more to do for us here. */
  1142. if (! have_referenced_pseudos)
  1143. {
  1144. timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
  1145. return false;
  1146. }
  1147. pseudos_live = sparseset_alloc (max_regno);
  1148. pseudos_live_through_calls = sparseset_alloc (max_regno);
  1149. pseudos_live_through_setjumps = sparseset_alloc (max_regno);
  1150. start_living = sparseset_alloc (max_regno);
  1151. start_dying = sparseset_alloc (max_regno);
  1152. dead_set = sparseset_alloc (max_regno);
  1153. unused_set = sparseset_alloc (max_regno);
  1154. curr_point = 0;
  1155. point_freq_vec.create (get_max_uid () * 2);
  1156. lra_point_freq = point_freq_vec.address ();
  1157. int *post_order_rev_cfg = XNEWVEC (int, last_basic_block_for_fn (cfun));
  1158. int n_blocks_inverted = inverted_post_order_compute (post_order_rev_cfg);
  1159. lra_assert (n_blocks_inverted == n_basic_blocks_for_fn (cfun));
  1160. bb_live_change_p = false;
  1161. for (i = n_blocks_inverted - 1; i >= 0; --i)
  1162. {
  1163. bb = BASIC_BLOCK_FOR_FN (cfun, post_order_rev_cfg[i]);
  1164. if (bb == EXIT_BLOCK_PTR_FOR_FN (cfun) || bb
  1165. == ENTRY_BLOCK_PTR_FOR_FN (cfun))
  1166. continue;
  1167. if (process_bb_lives (bb, curr_point, dead_insn_p))
  1168. bb_live_change_p = true;
  1169. }
  1170. if (bb_live_change_p)
  1171. {
  1172. /* We need to clear pseudo live info as some pseudos can
  1173. disappear, e.g. pseudos with used equivalences. */
  1174. FOR_EACH_BB_FN (bb, cfun)
  1175. {
  1176. bitmap_clear_range (df_get_live_in (bb), FIRST_PSEUDO_REGISTER,
  1177. max_regno - FIRST_PSEUDO_REGISTER);
  1178. bitmap_clear_range (df_get_live_out (bb), FIRST_PSEUDO_REGISTER,
  1179. max_regno - FIRST_PSEUDO_REGISTER);
  1180. }
  1181. /* As we did not change CFG since LRA start we can use
  1182. DF-infrastructure solver to solve live data flow problem. */
  1183. df_simple_dataflow
  1184. (DF_BACKWARD, NULL, live_con_fun_0, live_con_fun_n,
  1185. live_trans_fun, &all_blocks,
  1186. df_get_postorder (DF_BACKWARD), df_get_n_blocks (DF_BACKWARD));
  1187. if (lra_dump_file != NULL)
  1188. {
  1189. fprintf (lra_dump_file,
  1190. "Global pseudo live data have been updated:\n");
  1191. basic_block bb;
  1192. FOR_EACH_BB_FN (bb, cfun)
  1193. {
  1194. bb_data_t bb_info = get_bb_data (bb);
  1195. bitmap bb_livein = df_get_live_in (bb);
  1196. bitmap bb_liveout = df_get_live_out (bb);
  1197. fprintf (lra_dump_file, "\nBB %d:\n", bb->index);
  1198. lra_dump_bitmap_with_title (" gen:",
  1199. &bb_info->gen_pseudos, bb->index);
  1200. lra_dump_bitmap_with_title (" killed:",
  1201. &bb_info->killed_pseudos, bb->index);
  1202. lra_dump_bitmap_with_title (" livein:", bb_livein, bb->index);
  1203. lra_dump_bitmap_with_title (" liveout:", bb_liveout, bb->index);
  1204. }
  1205. }
  1206. }
  1207. free (post_order_rev_cfg);
  1208. lra_live_max_point = curr_point;
  1209. if (lra_dump_file != NULL)
  1210. print_live_ranges (lra_dump_file);
  1211. /* Clean up. */
  1212. sparseset_free (unused_set);
  1213. sparseset_free (dead_set);
  1214. sparseset_free (start_dying);
  1215. sparseset_free (start_living);
  1216. sparseset_free (pseudos_live_through_calls);
  1217. sparseset_free (pseudos_live_through_setjumps);
  1218. sparseset_free (pseudos_live);
  1219. compress_live_ranges ();
  1220. timevar_pop (TV_LRA_CREATE_LIVE_RANGES);
  1221. return bb_live_change_p;
  1222. }
  1223. /* The main entry function creates live-ranges and other live info
  1224. necessary for the assignment sub-pass. It uses
  1225. lra_creates_live_ranges_1 -- so read comments for the
  1226. function. */
  1227. void
  1228. lra_create_live_ranges (bool all_p, bool dead_insn_p)
  1229. {
  1230. if (! lra_create_live_ranges_1 (all_p, dead_insn_p))
  1231. return;
  1232. if (lra_dump_file != NULL)
  1233. fprintf (lra_dump_file, "Live info was changed -- recalculate it\n");
  1234. /* Live info was changed on a bb border. It means that some info,
  1235. e.g. about conflict regs, calls crossed, and live ranges may be
  1236. wrong. We need this info for allocation. So recalculate it
  1237. again but without removing dead insns which can change live info
  1238. again. Repetitive live range calculations are expensive therefore
  1239. we stop here as we already have correct info although some
  1240. improvement in rare cases could be possible on this sub-pass if
  1241. we do dead insn elimination again (still the improvement may
  1242. happen later). */
  1243. lra_clear_live_ranges ();
  1244. bool res = lra_create_live_ranges_1 (all_p, false);
  1245. lra_assert (! res);
  1246. }
  1247. /* Finish all live ranges. */
  1248. void
  1249. lra_clear_live_ranges (void)
  1250. {
  1251. int i;
  1252. for (i = 0; i < max_reg_num (); i++)
  1253. free_live_range_list (lra_reg_info[i].live_ranges);
  1254. point_freq_vec.release ();
  1255. }
  1256. /* Initialize live ranges data once per function. */
  1257. void
  1258. lra_live_ranges_init (void)
  1259. {
  1260. live_range_pool = create_alloc_pool ("live ranges",
  1261. sizeof (struct lra_live_range), 100);
  1262. bitmap_initialize (&temp_bitmap, &reg_obstack);
  1263. initiate_live_solver ();
  1264. }
  1265. /* Finish live ranges data once per function. */
  1266. void
  1267. lra_live_ranges_finish (void)
  1268. {
  1269. finish_live_solver ();
  1270. bitmap_clear (&temp_bitmap);
  1271. free_alloc_pool (live_range_pool);
  1272. }