ira-lives.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613
  1. /* IRA processing allocno lives to build allocno live ranges.
  2. Copyright (C) 2006-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. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "regs.h"
  21. #include "rtl.h"
  22. #include "tm_p.h"
  23. #include "target.h"
  24. #include "flags.h"
  25. #include "except.h"
  26. #include "hard-reg-set.h"
  27. #include "predict.h"
  28. #include "vec.h"
  29. #include "hashtab.h"
  30. #include "hash-set.h"
  31. #include "machmode.h"
  32. #include "input.h"
  33. #include "function.h"
  34. #include "basic-block.h"
  35. #include "insn-config.h"
  36. #include "recog.h"
  37. #include "diagnostic-core.h"
  38. #include "params.h"
  39. #include "df.h"
  40. #include "sbitmap.h"
  41. #include "sparseset.h"
  42. #include "ira-int.h"
  43. /* The code in this file is similar to one in global but the code
  44. works on the allocno basis and creates live ranges instead of
  45. pseudo-register conflicts. */
  46. /* Program points are enumerated by numbers from range
  47. 0..IRA_MAX_POINT-1. There are approximately two times more program
  48. points than insns. Program points are places in the program where
  49. liveness info can be changed. In most general case (there are more
  50. complicated cases too) some program points correspond to places
  51. where input operand dies and other ones correspond to places where
  52. output operands are born. */
  53. int ira_max_point;
  54. /* Arrays of size IRA_MAX_POINT mapping a program point to the allocno
  55. live ranges with given start/finish point. */
  56. live_range_t *ira_start_point_ranges, *ira_finish_point_ranges;
  57. /* Number of the current program point. */
  58. static int curr_point;
  59. /* Point where register pressure excess started or -1 if there is no
  60. register pressure excess. Excess pressure for a register class at
  61. some point means that there are more allocnos of given register
  62. class living at the point than number of hard-registers of the
  63. class available for the allocation. It is defined only for
  64. pressure classes. */
  65. static int high_pressure_start_point[N_REG_CLASSES];
  66. /* Objects live at current point in the scan. */
  67. static sparseset objects_live;
  68. /* A temporary bitmap used in functions that wish to avoid visiting an allocno
  69. multiple times. */
  70. static sparseset allocnos_processed;
  71. /* Set of hard regs (except eliminable ones) currently live. */
  72. static HARD_REG_SET hard_regs_live;
  73. /* The loop tree node corresponding to the current basic block. */
  74. static ira_loop_tree_node_t curr_bb_node;
  75. /* The number of the last processed call. */
  76. static int last_call_num;
  77. /* The number of last call at which given allocno was saved. */
  78. static int *allocno_saved_at_call;
  79. /* The value of get_preferred_alternatives for the current instruction,
  80. supplemental to recog_data. */
  81. static alternative_mask preferred_alternatives;
  82. /* Record the birth of hard register REGNO, updating hard_regs_live and
  83. hard reg conflict information for living allocnos. */
  84. static void
  85. make_hard_regno_born (int regno)
  86. {
  87. unsigned int i;
  88. SET_HARD_REG_BIT (hard_regs_live, regno);
  89. EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
  90. {
  91. ira_object_t obj = ira_object_id_map[i];
  92. SET_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS (obj), regno);
  93. SET_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno);
  94. }
  95. }
  96. /* Process the death of hard register REGNO. This updates
  97. hard_regs_live. */
  98. static void
  99. make_hard_regno_dead (int regno)
  100. {
  101. CLEAR_HARD_REG_BIT (hard_regs_live, regno);
  102. }
  103. /* Record the birth of object OBJ. Set a bit for it in objects_live,
  104. start a new live range for it if necessary and update hard register
  105. conflicts. */
  106. static void
  107. make_object_born (ira_object_t obj)
  108. {
  109. live_range_t lr = OBJECT_LIVE_RANGES (obj);
  110. sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
  111. IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), hard_regs_live);
  112. IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), hard_regs_live);
  113. if (lr == NULL
  114. || (lr->finish != curr_point && lr->finish + 1 != curr_point))
  115. ira_add_live_range_to_object (obj, curr_point, -1);
  116. }
  117. /* Update ALLOCNO_EXCESS_PRESSURE_POINTS_NUM for the allocno
  118. associated with object OBJ. */
  119. static void
  120. update_allocno_pressure_excess_length (ira_object_t obj)
  121. {
  122. ira_allocno_t a = OBJECT_ALLOCNO (obj);
  123. int start, i;
  124. enum reg_class aclass, pclass, cl;
  125. live_range_t p;
  126. aclass = ALLOCNO_CLASS (a);
  127. pclass = ira_pressure_class_translate[aclass];
  128. for (i = 0;
  129. (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
  130. i++)
  131. {
  132. if (! ira_reg_pressure_class_p[cl])
  133. continue;
  134. if (high_pressure_start_point[cl] < 0)
  135. continue;
  136. p = OBJECT_LIVE_RANGES (obj);
  137. ira_assert (p != NULL);
  138. start = (high_pressure_start_point[cl] > p->start
  139. ? high_pressure_start_point[cl] : p->start);
  140. ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) += curr_point - start + 1;
  141. }
  142. }
  143. /* Process the death of object OBJ, which is associated with allocno
  144. A. This finishes the current live range for it. */
  145. static void
  146. make_object_dead (ira_object_t obj)
  147. {
  148. live_range_t lr;
  149. sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (obj));
  150. lr = OBJECT_LIVE_RANGES (obj);
  151. ira_assert (lr != NULL);
  152. lr->finish = curr_point;
  153. update_allocno_pressure_excess_length (obj);
  154. }
  155. /* The current register pressures for each pressure class for the current
  156. basic block. */
  157. static int curr_reg_pressure[N_REG_CLASSES];
  158. /* Record that register pressure for PCLASS increased by N registers.
  159. Update the current register pressure, maximal register pressure for
  160. the current BB and the start point of the register pressure
  161. excess. */
  162. static void
  163. inc_register_pressure (enum reg_class pclass, int n)
  164. {
  165. int i;
  166. enum reg_class cl;
  167. for (i = 0;
  168. (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
  169. i++)
  170. {
  171. if (! ira_reg_pressure_class_p[cl])
  172. continue;
  173. curr_reg_pressure[cl] += n;
  174. if (high_pressure_start_point[cl] < 0
  175. && (curr_reg_pressure[cl] > ira_class_hard_regs_num[cl]))
  176. high_pressure_start_point[cl] = curr_point;
  177. if (curr_bb_node->reg_pressure[cl] < curr_reg_pressure[cl])
  178. curr_bb_node->reg_pressure[cl] = curr_reg_pressure[cl];
  179. }
  180. }
  181. /* Record that register pressure for PCLASS has decreased by NREGS
  182. registers; update current register pressure, start point of the
  183. register pressure excess, and register pressure excess length for
  184. living allocnos. */
  185. static void
  186. dec_register_pressure (enum reg_class pclass, int nregs)
  187. {
  188. int i;
  189. unsigned int j;
  190. enum reg_class cl;
  191. bool set_p = false;
  192. for (i = 0;
  193. (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
  194. i++)
  195. {
  196. if (! ira_reg_pressure_class_p[cl])
  197. continue;
  198. curr_reg_pressure[cl] -= nregs;
  199. ira_assert (curr_reg_pressure[cl] >= 0);
  200. if (high_pressure_start_point[cl] >= 0
  201. && curr_reg_pressure[cl] <= ira_class_hard_regs_num[cl])
  202. set_p = true;
  203. }
  204. if (set_p)
  205. {
  206. EXECUTE_IF_SET_IN_SPARSESET (objects_live, j)
  207. update_allocno_pressure_excess_length (ira_object_id_map[j]);
  208. for (i = 0;
  209. (cl = ira_reg_class_super_classes[pclass][i]) != LIM_REG_CLASSES;
  210. i++)
  211. {
  212. if (! ira_reg_pressure_class_p[cl])
  213. continue;
  214. if (high_pressure_start_point[cl] >= 0
  215. && curr_reg_pressure[cl] <= ira_class_hard_regs_num[cl])
  216. high_pressure_start_point[cl] = -1;
  217. }
  218. }
  219. }
  220. /* Determine from the objects_live bitmap whether REGNO is currently live,
  221. and occupies only one object. Return false if we have no information. */
  222. static bool
  223. pseudo_regno_single_word_and_live_p (int regno)
  224. {
  225. ira_allocno_t a = ira_curr_regno_allocno_map[regno];
  226. ira_object_t obj;
  227. if (a == NULL)
  228. return false;
  229. if (ALLOCNO_NUM_OBJECTS (a) > 1)
  230. return false;
  231. obj = ALLOCNO_OBJECT (a, 0);
  232. return sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj));
  233. }
  234. /* Mark the pseudo register REGNO as live. Update all information about
  235. live ranges and register pressure. */
  236. static void
  237. mark_pseudo_regno_live (int regno)
  238. {
  239. ira_allocno_t a = ira_curr_regno_allocno_map[regno];
  240. enum reg_class pclass;
  241. int i, n, nregs;
  242. if (a == NULL)
  243. return;
  244. /* Invalidate because it is referenced. */
  245. allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
  246. n = ALLOCNO_NUM_OBJECTS (a);
  247. pclass = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
  248. nregs = ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
  249. if (n > 1)
  250. {
  251. /* We track every subobject separately. */
  252. gcc_assert (nregs == n);
  253. nregs = 1;
  254. }
  255. for (i = 0; i < n; i++)
  256. {
  257. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  258. if (sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
  259. continue;
  260. inc_register_pressure (pclass, nregs);
  261. make_object_born (obj);
  262. }
  263. }
  264. /* Like mark_pseudo_regno_live, but try to only mark one subword of
  265. the pseudo as live. SUBWORD indicates which; a value of 0
  266. indicates the low part. */
  267. static void
  268. mark_pseudo_regno_subword_live (int regno, int subword)
  269. {
  270. ira_allocno_t a = ira_curr_regno_allocno_map[regno];
  271. int n;
  272. enum reg_class pclass;
  273. ira_object_t obj;
  274. if (a == NULL)
  275. return;
  276. /* Invalidate because it is referenced. */
  277. allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
  278. n = ALLOCNO_NUM_OBJECTS (a);
  279. if (n == 1)
  280. {
  281. mark_pseudo_regno_live (regno);
  282. return;
  283. }
  284. pclass = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
  285. gcc_assert
  286. (n == ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
  287. obj = ALLOCNO_OBJECT (a, subword);
  288. if (sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
  289. return;
  290. inc_register_pressure (pclass, 1);
  291. make_object_born (obj);
  292. }
  293. /* Mark the register REG as live. Store a 1 in hard_regs_live for
  294. this register, record how many consecutive hardware registers it
  295. actually needs. */
  296. static void
  297. mark_hard_reg_live (rtx reg)
  298. {
  299. int regno = REGNO (reg);
  300. if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
  301. {
  302. int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
  303. enum reg_class aclass, pclass;
  304. while (regno < last)
  305. {
  306. if (! TEST_HARD_REG_BIT (hard_regs_live, regno)
  307. && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
  308. {
  309. aclass = ira_hard_regno_allocno_class[regno];
  310. pclass = ira_pressure_class_translate[aclass];
  311. inc_register_pressure (pclass, 1);
  312. make_hard_regno_born (regno);
  313. }
  314. regno++;
  315. }
  316. }
  317. }
  318. /* Mark a pseudo, or one of its subwords, as live. REGNO is the pseudo's
  319. register number; ORIG_REG is the access in the insn, which may be a
  320. subreg. */
  321. static void
  322. mark_pseudo_reg_live (rtx orig_reg, unsigned regno)
  323. {
  324. if (df_read_modify_subreg_p (orig_reg))
  325. {
  326. mark_pseudo_regno_subword_live (regno,
  327. subreg_lowpart_p (orig_reg) ? 0 : 1);
  328. }
  329. else
  330. mark_pseudo_regno_live (regno);
  331. }
  332. /* Mark the register referenced by use or def REF as live. */
  333. static void
  334. mark_ref_live (df_ref ref)
  335. {
  336. rtx reg = DF_REF_REG (ref);
  337. rtx orig_reg = reg;
  338. if (GET_CODE (reg) == SUBREG)
  339. reg = SUBREG_REG (reg);
  340. if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
  341. mark_pseudo_reg_live (orig_reg, REGNO (reg));
  342. else
  343. mark_hard_reg_live (reg);
  344. }
  345. /* Mark the pseudo register REGNO as dead. Update all information about
  346. live ranges and register pressure. */
  347. static void
  348. mark_pseudo_regno_dead (int regno)
  349. {
  350. ira_allocno_t a = ira_curr_regno_allocno_map[regno];
  351. int n, i, nregs;
  352. enum reg_class cl;
  353. if (a == NULL)
  354. return;
  355. /* Invalidate because it is referenced. */
  356. allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
  357. n = ALLOCNO_NUM_OBJECTS (a);
  358. cl = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
  359. nregs = ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
  360. if (n > 1)
  361. {
  362. /* We track every subobject separately. */
  363. gcc_assert (nregs == n);
  364. nregs = 1;
  365. }
  366. for (i = 0; i < n; i++)
  367. {
  368. ira_object_t obj = ALLOCNO_OBJECT (a, i);
  369. if (!sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
  370. continue;
  371. dec_register_pressure (cl, nregs);
  372. make_object_dead (obj);
  373. }
  374. }
  375. /* Like mark_pseudo_regno_dead, but called when we know that only part of the
  376. register dies. SUBWORD indicates which; a value of 0 indicates the low part. */
  377. static void
  378. mark_pseudo_regno_subword_dead (int regno, int subword)
  379. {
  380. ira_allocno_t a = ira_curr_regno_allocno_map[regno];
  381. int n;
  382. enum reg_class cl;
  383. ira_object_t obj;
  384. if (a == NULL)
  385. return;
  386. /* Invalidate because it is referenced. */
  387. allocno_saved_at_call[ALLOCNO_NUM (a)] = 0;
  388. n = ALLOCNO_NUM_OBJECTS (a);
  389. if (n == 1)
  390. /* The allocno as a whole doesn't die in this case. */
  391. return;
  392. cl = ira_pressure_class_translate[ALLOCNO_CLASS (a)];
  393. gcc_assert
  394. (n == ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
  395. obj = ALLOCNO_OBJECT (a, subword);
  396. if (!sparseset_bit_p (objects_live, OBJECT_CONFLICT_ID (obj)))
  397. return;
  398. dec_register_pressure (cl, 1);
  399. make_object_dead (obj);
  400. }
  401. /* Mark the hard register REG as dead. Store a 0 in hard_regs_live for the
  402. register. */
  403. static void
  404. mark_hard_reg_dead (rtx reg)
  405. {
  406. int regno = REGNO (reg);
  407. if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno))
  408. {
  409. int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
  410. enum reg_class aclass, pclass;
  411. while (regno < last)
  412. {
  413. if (TEST_HARD_REG_BIT (hard_regs_live, regno))
  414. {
  415. aclass = ira_hard_regno_allocno_class[regno];
  416. pclass = ira_pressure_class_translate[aclass];
  417. dec_register_pressure (pclass, 1);
  418. make_hard_regno_dead (regno);
  419. }
  420. regno++;
  421. }
  422. }
  423. }
  424. /* Mark a pseudo, or one of its subwords, as dead. REGNO is the pseudo's
  425. register number; ORIG_REG is the access in the insn, which may be a
  426. subreg. */
  427. static void
  428. mark_pseudo_reg_dead (rtx orig_reg, unsigned regno)
  429. {
  430. if (df_read_modify_subreg_p (orig_reg))
  431. {
  432. mark_pseudo_regno_subword_dead (regno,
  433. subreg_lowpart_p (orig_reg) ? 0 : 1);
  434. }
  435. else
  436. mark_pseudo_regno_dead (regno);
  437. }
  438. /* Mark the register referenced by definition DEF as dead, if the
  439. definition is a total one. */
  440. static void
  441. mark_ref_dead (df_ref def)
  442. {
  443. rtx reg = DF_REF_REG (def);
  444. rtx orig_reg = reg;
  445. if (DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL))
  446. return;
  447. if (GET_CODE (reg) == SUBREG)
  448. reg = SUBREG_REG (reg);
  449. if (DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL)
  450. && (GET_CODE (orig_reg) != SUBREG
  451. || REGNO (reg) < FIRST_PSEUDO_REGISTER
  452. || !df_read_modify_subreg_p (orig_reg)))
  453. return;
  454. if (REGNO (reg) >= FIRST_PSEUDO_REGISTER)
  455. mark_pseudo_reg_dead (orig_reg, REGNO (reg));
  456. else
  457. mark_hard_reg_dead (reg);
  458. }
  459. /* If REG is a pseudo or a subreg of it, and the class of its allocno
  460. intersects CL, make a conflict with pseudo DREG. ORIG_DREG is the
  461. rtx actually accessed, it may be identical to DREG or a subreg of it.
  462. Advance the current program point before making the conflict if
  463. ADVANCE_P. Return TRUE if we will need to advance the current
  464. program point. */
  465. static bool
  466. make_pseudo_conflict (rtx reg, enum reg_class cl, rtx dreg, rtx orig_dreg,
  467. bool advance_p)
  468. {
  469. rtx orig_reg = reg;
  470. ira_allocno_t a;
  471. if (GET_CODE (reg) == SUBREG)
  472. reg = SUBREG_REG (reg);
  473. if (! REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
  474. return advance_p;
  475. a = ira_curr_regno_allocno_map[REGNO (reg)];
  476. if (! reg_classes_intersect_p (cl, ALLOCNO_CLASS (a)))
  477. return advance_p;
  478. if (advance_p)
  479. curr_point++;
  480. mark_pseudo_reg_live (orig_reg, REGNO (reg));
  481. mark_pseudo_reg_live (orig_dreg, REGNO (dreg));
  482. mark_pseudo_reg_dead (orig_reg, REGNO (reg));
  483. mark_pseudo_reg_dead (orig_dreg, REGNO (dreg));
  484. return false;
  485. }
  486. /* Check and make if necessary conflicts for pseudo DREG of class
  487. DEF_CL of the current insn with input operand USE of class USE_CL.
  488. ORIG_DREG is the rtx actually accessed, it may be identical to
  489. DREG or a subreg of it. Advance the current program point before
  490. making the conflict if ADVANCE_P. Return TRUE if we will need to
  491. advance the current program point. */
  492. static bool
  493. check_and_make_def_use_conflict (rtx dreg, rtx orig_dreg,
  494. enum reg_class def_cl, int use,
  495. enum reg_class use_cl, bool advance_p)
  496. {
  497. if (! reg_classes_intersect_p (def_cl, use_cl))
  498. return advance_p;
  499. advance_p = make_pseudo_conflict (recog_data.operand[use],
  500. use_cl, dreg, orig_dreg, advance_p);
  501. /* Reload may end up swapping commutative operands, so you
  502. have to take both orderings into account. The
  503. constraints for the two operands can be completely
  504. different. (Indeed, if the constraints for the two
  505. operands are the same for all alternatives, there's no
  506. point marking them as commutative.) */
  507. if (use < recog_data.n_operands - 1
  508. && recog_data.constraints[use][0] == '%')
  509. advance_p
  510. = make_pseudo_conflict (recog_data.operand[use + 1],
  511. use_cl, dreg, orig_dreg, advance_p);
  512. if (use >= 1
  513. && recog_data.constraints[use - 1][0] == '%')
  514. advance_p
  515. = make_pseudo_conflict (recog_data.operand[use - 1],
  516. use_cl, dreg, orig_dreg, advance_p);
  517. return advance_p;
  518. }
  519. /* Check and make if necessary conflicts for definition DEF of class
  520. DEF_CL of the current insn with input operands. Process only
  521. constraints of alternative ALT. */
  522. static void
  523. check_and_make_def_conflict (int alt, int def, enum reg_class def_cl)
  524. {
  525. int use, use_match;
  526. ira_allocno_t a;
  527. enum reg_class use_cl, acl;
  528. bool advance_p;
  529. rtx dreg = recog_data.operand[def];
  530. rtx orig_dreg = dreg;
  531. if (def_cl == NO_REGS)
  532. return;
  533. if (GET_CODE (dreg) == SUBREG)
  534. dreg = SUBREG_REG (dreg);
  535. if (! REG_P (dreg) || REGNO (dreg) < FIRST_PSEUDO_REGISTER)
  536. return;
  537. a = ira_curr_regno_allocno_map[REGNO (dreg)];
  538. acl = ALLOCNO_CLASS (a);
  539. if (! reg_classes_intersect_p (acl, def_cl))
  540. return;
  541. advance_p = true;
  542. int n_operands = recog_data.n_operands;
  543. const operand_alternative *op_alt = &recog_op_alt[alt * n_operands];
  544. for (use = 0; use < n_operands; use++)
  545. {
  546. int alt1;
  547. if (use == def || recog_data.operand_type[use] == OP_OUT)
  548. continue;
  549. if (op_alt[use].anything_ok)
  550. use_cl = ALL_REGS;
  551. else
  552. use_cl = op_alt[use].cl;
  553. /* If there's any alternative that allows USE to match DEF, do not
  554. record a conflict. If that causes us to create an invalid
  555. instruction due to the earlyclobber, reload must fix it up. */
  556. for (alt1 = 0; alt1 < recog_data.n_alternatives; alt1++)
  557. {
  558. if (!TEST_BIT (preferred_alternatives, alt1))
  559. continue;
  560. const operand_alternative *op_alt1
  561. = &recog_op_alt[alt1 * n_operands];
  562. if (op_alt1[use].matches == def
  563. || (use < n_operands - 1
  564. && recog_data.constraints[use][0] == '%'
  565. && op_alt1[use + 1].matches == def)
  566. || (use >= 1
  567. && recog_data.constraints[use - 1][0] == '%'
  568. && op_alt1[use - 1].matches == def))
  569. break;
  570. }
  571. if (alt1 < recog_data.n_alternatives)
  572. continue;
  573. advance_p = check_and_make_def_use_conflict (dreg, orig_dreg, def_cl,
  574. use, use_cl, advance_p);
  575. if ((use_match = op_alt[use].matches) >= 0)
  576. {
  577. if (use_match == def)
  578. continue;
  579. if (op_alt[use_match].anything_ok)
  580. use_cl = ALL_REGS;
  581. else
  582. use_cl = op_alt[use_match].cl;
  583. advance_p = check_and_make_def_use_conflict (dreg, orig_dreg, def_cl,
  584. use, use_cl, advance_p);
  585. }
  586. }
  587. }
  588. /* Make conflicts of early clobber pseudo registers of the current
  589. insn with its inputs. Avoid introducing unnecessary conflicts by
  590. checking classes of the constraints and pseudos because otherwise
  591. significant code degradation is possible for some targets. */
  592. static void
  593. make_early_clobber_and_input_conflicts (void)
  594. {
  595. int alt;
  596. int def, def_match;
  597. enum reg_class def_cl;
  598. int n_alternatives = recog_data.n_alternatives;
  599. int n_operands = recog_data.n_operands;
  600. const operand_alternative *op_alt = recog_op_alt;
  601. for (alt = 0; alt < n_alternatives; alt++, op_alt += n_operands)
  602. if (TEST_BIT (preferred_alternatives, alt))
  603. for (def = 0; def < n_operands; def++)
  604. {
  605. def_cl = NO_REGS;
  606. if (op_alt[def].earlyclobber)
  607. {
  608. if (op_alt[def].anything_ok)
  609. def_cl = ALL_REGS;
  610. else
  611. def_cl = op_alt[def].cl;
  612. check_and_make_def_conflict (alt, def, def_cl);
  613. }
  614. if ((def_match = op_alt[def].matches) >= 0
  615. && (op_alt[def_match].earlyclobber
  616. || op_alt[def].earlyclobber))
  617. {
  618. if (op_alt[def_match].anything_ok)
  619. def_cl = ALL_REGS;
  620. else
  621. def_cl = op_alt[def_match].cl;
  622. check_and_make_def_conflict (alt, def, def_cl);
  623. }
  624. }
  625. }
  626. /* Mark early clobber hard registers of the current INSN as live (if
  627. LIVE_P) or dead. Return true if there are such registers. */
  628. static bool
  629. mark_hard_reg_early_clobbers (rtx_insn *insn, bool live_p)
  630. {
  631. df_ref def;
  632. bool set_p = false;
  633. FOR_EACH_INSN_DEF (def, insn)
  634. if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
  635. {
  636. rtx dreg = DF_REF_REG (def);
  637. if (GET_CODE (dreg) == SUBREG)
  638. dreg = SUBREG_REG (dreg);
  639. if (! REG_P (dreg) || REGNO (dreg) >= FIRST_PSEUDO_REGISTER)
  640. continue;
  641. /* Hard register clobbers are believed to be early clobber
  642. because there is no way to say that non-operand hard
  643. register clobbers are not early ones. */
  644. if (live_p)
  645. mark_ref_live (def);
  646. else
  647. mark_ref_dead (def);
  648. set_p = true;
  649. }
  650. return set_p;
  651. }
  652. /* Checks that CONSTRAINTS permits to use only one hard register. If
  653. it is so, the function returns the class of the hard register.
  654. Otherwise it returns NO_REGS. */
  655. static enum reg_class
  656. single_reg_class (const char *constraints, rtx op, rtx equiv_const)
  657. {
  658. int c;
  659. enum reg_class cl, next_cl;
  660. enum constraint_num cn;
  661. cl = NO_REGS;
  662. alternative_mask preferred = preferred_alternatives;
  663. for (; (c = *constraints); constraints += CONSTRAINT_LEN (c, constraints))
  664. if (c == '#')
  665. preferred &= ~ALTERNATIVE_BIT (0);
  666. else if (c == ',')
  667. preferred >>= 1;
  668. else if (preferred & 1)
  669. switch (c)
  670. {
  671. case 'g':
  672. return NO_REGS;
  673. default:
  674. /* ??? Is this the best way to handle memory constraints? */
  675. cn = lookup_constraint (constraints);
  676. if (insn_extra_memory_constraint (cn)
  677. || insn_extra_address_constraint (cn))
  678. return NO_REGS;
  679. if (constraint_satisfied_p (op, cn)
  680. || (equiv_const != NULL_RTX
  681. && CONSTANT_P (equiv_const)
  682. && constraint_satisfied_p (equiv_const, cn)))
  683. return NO_REGS;
  684. next_cl = reg_class_for_constraint (cn);
  685. if (next_cl == NO_REGS)
  686. break;
  687. if (cl == NO_REGS
  688. ? ira_class_singleton[next_cl][GET_MODE (op)] < 0
  689. : (ira_class_singleton[cl][GET_MODE (op)]
  690. != ira_class_singleton[next_cl][GET_MODE (op)]))
  691. return NO_REGS;
  692. cl = next_cl;
  693. break;
  694. case '0': case '1': case '2': case '3': case '4':
  695. case '5': case '6': case '7': case '8': case '9':
  696. next_cl
  697. = single_reg_class (recog_data.constraints[c - '0'],
  698. recog_data.operand[c - '0'], NULL_RTX);
  699. if (cl == NO_REGS
  700. ? ira_class_singleton[next_cl][GET_MODE (op)] < 0
  701. : (ira_class_singleton[cl][GET_MODE (op)]
  702. != ira_class_singleton[next_cl][GET_MODE (op)]))
  703. return NO_REGS;
  704. cl = next_cl;
  705. break;
  706. }
  707. return cl;
  708. }
  709. /* The function checks that operand OP_NUM of the current insn can use
  710. only one hard register. If it is so, the function returns the
  711. class of the hard register. Otherwise it returns NO_REGS. */
  712. static enum reg_class
  713. single_reg_operand_class (int op_num)
  714. {
  715. if (op_num < 0 || recog_data.n_alternatives == 0)
  716. return NO_REGS;
  717. return single_reg_class (recog_data.constraints[op_num],
  718. recog_data.operand[op_num], NULL_RTX);
  719. }
  720. /* The function sets up hard register set *SET to hard registers which
  721. might be used by insn reloads because the constraints are too
  722. strict. */
  723. void
  724. ira_implicitly_set_insn_hard_regs (HARD_REG_SET *set)
  725. {
  726. int i, c, regno = 0;
  727. enum reg_class cl;
  728. rtx op;
  729. machine_mode mode;
  730. CLEAR_HARD_REG_SET (*set);
  731. for (i = 0; i < recog_data.n_operands; i++)
  732. {
  733. op = recog_data.operand[i];
  734. if (GET_CODE (op) == SUBREG)
  735. op = SUBREG_REG (op);
  736. if (GET_CODE (op) == SCRATCH
  737. || (REG_P (op) && (regno = REGNO (op)) >= FIRST_PSEUDO_REGISTER))
  738. {
  739. const char *p = recog_data.constraints[i];
  740. mode = (GET_CODE (op) == SCRATCH
  741. ? GET_MODE (op) : PSEUDO_REGNO_MODE (regno));
  742. cl = NO_REGS;
  743. alternative_mask preferred = preferred_alternatives;
  744. for (; (c = *p); p += CONSTRAINT_LEN (c, p))
  745. if (c == '#')
  746. preferred &= ~ALTERNATIVE_BIT (0);
  747. else if (c == ',')
  748. preferred >>= 1;
  749. else if (preferred & 1)
  750. {
  751. cl = reg_class_for_constraint (lookup_constraint (p));
  752. if (cl != NO_REGS)
  753. {
  754. /* There is no register pressure problem if all of the
  755. regs in this class are fixed. */
  756. int regno = ira_class_singleton[cl][mode];
  757. if (regno >= 0)
  758. add_to_hard_reg_set (set, mode, regno);
  759. }
  760. }
  761. }
  762. }
  763. }
  764. /* Processes input operands, if IN_P, or output operands otherwise of
  765. the current insn with FREQ to find allocno which can use only one
  766. hard register and makes other currently living allocnos conflicting
  767. with the hard register. */
  768. static void
  769. process_single_reg_class_operands (bool in_p, int freq)
  770. {
  771. int i, regno;
  772. unsigned int px;
  773. enum reg_class cl;
  774. rtx operand;
  775. ira_allocno_t operand_a, a;
  776. for (i = 0; i < recog_data.n_operands; i++)
  777. {
  778. operand = recog_data.operand[i];
  779. if (in_p && recog_data.operand_type[i] != OP_IN
  780. && recog_data.operand_type[i] != OP_INOUT)
  781. continue;
  782. if (! in_p && recog_data.operand_type[i] != OP_OUT
  783. && recog_data.operand_type[i] != OP_INOUT)
  784. continue;
  785. cl = single_reg_operand_class (i);
  786. if (cl == NO_REGS)
  787. continue;
  788. operand_a = NULL;
  789. if (GET_CODE (operand) == SUBREG)
  790. operand = SUBREG_REG (operand);
  791. if (REG_P (operand)
  792. && (regno = REGNO (operand)) >= FIRST_PSEUDO_REGISTER)
  793. {
  794. enum reg_class aclass;
  795. operand_a = ira_curr_regno_allocno_map[regno];
  796. aclass = ALLOCNO_CLASS (operand_a);
  797. if (ira_class_subset_p[cl][aclass])
  798. {
  799. /* View the desired allocation of OPERAND as:
  800. (REG:YMODE YREGNO),
  801. a simplification of:
  802. (subreg:YMODE (reg:XMODE XREGNO) OFFSET). */
  803. machine_mode ymode, xmode;
  804. int xregno, yregno;
  805. HOST_WIDE_INT offset;
  806. xmode = recog_data.operand_mode[i];
  807. xregno = ira_class_singleton[cl][xmode];
  808. gcc_assert (xregno >= 0);
  809. ymode = ALLOCNO_MODE (operand_a);
  810. offset = subreg_lowpart_offset (ymode, xmode);
  811. yregno = simplify_subreg_regno (xregno, xmode, offset, ymode);
  812. if (yregno >= 0
  813. && ira_class_hard_reg_index[aclass][yregno] >= 0)
  814. {
  815. int cost;
  816. ira_allocate_and_set_costs
  817. (&ALLOCNO_CONFLICT_HARD_REG_COSTS (operand_a),
  818. aclass, 0);
  819. ira_init_register_move_cost_if_necessary (xmode);
  820. cost = freq * (in_p
  821. ? ira_register_move_cost[xmode][aclass][cl]
  822. : ira_register_move_cost[xmode][cl][aclass]);
  823. ALLOCNO_CONFLICT_HARD_REG_COSTS (operand_a)
  824. [ira_class_hard_reg_index[aclass][yregno]] -= cost;
  825. }
  826. }
  827. }
  828. EXECUTE_IF_SET_IN_SPARSESET (objects_live, px)
  829. {
  830. ira_object_t obj = ira_object_id_map[px];
  831. a = OBJECT_ALLOCNO (obj);
  832. if (a != operand_a)
  833. {
  834. /* We could increase costs of A instead of making it
  835. conflicting with the hard register. But it works worse
  836. because it will be spilled in reload in anyway. */
  837. IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
  838. reg_class_contents[cl]);
  839. IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  840. reg_class_contents[cl]);
  841. }
  842. }
  843. }
  844. }
  845. /* Return true when one of the predecessor edges of BB is marked with
  846. EDGE_ABNORMAL_CALL or EDGE_EH. */
  847. static bool
  848. bb_has_abnormal_call_pred (basic_block bb)
  849. {
  850. edge e;
  851. edge_iterator ei;
  852. FOR_EACH_EDGE (e, ei, bb->preds)
  853. {
  854. if (e->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
  855. return true;
  856. }
  857. return false;
  858. }
  859. /* Look through the CALL_INSN_FUNCTION_USAGE of a call insn INSN, and see if
  860. we find a SET rtx that we can use to deduce that a register can be cheaply
  861. caller-saved. Return such a register, or NULL_RTX if none is found. */
  862. static rtx
  863. find_call_crossed_cheap_reg (rtx_insn *insn)
  864. {
  865. rtx cheap_reg = NULL_RTX;
  866. rtx exp = CALL_INSN_FUNCTION_USAGE (insn);
  867. while (exp != NULL)
  868. {
  869. rtx x = XEXP (exp, 0);
  870. if (GET_CODE (x) == SET)
  871. {
  872. exp = x;
  873. break;
  874. }
  875. exp = XEXP (exp, 1);
  876. }
  877. if (exp != NULL)
  878. {
  879. basic_block bb = BLOCK_FOR_INSN (insn);
  880. rtx reg = SET_SRC (exp);
  881. rtx_insn *prev = PREV_INSN (insn);
  882. while (prev && !(INSN_P (prev)
  883. && BLOCK_FOR_INSN (prev) != bb))
  884. {
  885. if (NONDEBUG_INSN_P (prev))
  886. {
  887. rtx set = single_set (prev);
  888. if (set && rtx_equal_p (SET_DEST (set), reg))
  889. {
  890. rtx src = SET_SRC (set);
  891. if (!REG_P (src) || HARD_REGISTER_P (src)
  892. || !pseudo_regno_single_word_and_live_p (REGNO (src)))
  893. break;
  894. if (!modified_between_p (src, prev, insn))
  895. cheap_reg = src;
  896. break;
  897. }
  898. if (set && rtx_equal_p (SET_SRC (set), reg))
  899. {
  900. rtx dest = SET_DEST (set);
  901. if (!REG_P (dest) || HARD_REGISTER_P (dest)
  902. || !pseudo_regno_single_word_and_live_p (REGNO (dest)))
  903. break;
  904. if (!modified_between_p (dest, prev, insn))
  905. cheap_reg = dest;
  906. break;
  907. }
  908. if (reg_overlap_mentioned_p (reg, PATTERN (prev)))
  909. break;
  910. }
  911. prev = PREV_INSN (prev);
  912. }
  913. }
  914. return cheap_reg;
  915. }
  916. /* Process insns of the basic block given by its LOOP_TREE_NODE to
  917. update allocno live ranges, allocno hard register conflicts,
  918. intersected calls, and register pressure info for allocnos for the
  919. basic block for and regions containing the basic block. */
  920. static void
  921. process_bb_node_lives (ira_loop_tree_node_t loop_tree_node)
  922. {
  923. int i, freq;
  924. unsigned int j;
  925. basic_block bb;
  926. rtx_insn *insn;
  927. bitmap_iterator bi;
  928. bitmap reg_live_out;
  929. unsigned int px;
  930. bool set_p;
  931. bb = loop_tree_node->bb;
  932. if (bb != NULL)
  933. {
  934. for (i = 0; i < ira_pressure_classes_num; i++)
  935. {
  936. curr_reg_pressure[ira_pressure_classes[i]] = 0;
  937. high_pressure_start_point[ira_pressure_classes[i]] = -1;
  938. }
  939. curr_bb_node = loop_tree_node;
  940. reg_live_out = df_get_live_out (bb);
  941. sparseset_clear (objects_live);
  942. REG_SET_TO_HARD_REG_SET (hard_regs_live, reg_live_out);
  943. AND_COMPL_HARD_REG_SET (hard_regs_live, eliminable_regset);
  944. AND_COMPL_HARD_REG_SET (hard_regs_live, ira_no_alloc_regs);
  945. for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  946. if (TEST_HARD_REG_BIT (hard_regs_live, i))
  947. {
  948. enum reg_class aclass, pclass, cl;
  949. aclass = ira_allocno_class_translate[REGNO_REG_CLASS (i)];
  950. pclass = ira_pressure_class_translate[aclass];
  951. for (j = 0;
  952. (cl = ira_reg_class_super_classes[pclass][j])
  953. != LIM_REG_CLASSES;
  954. j++)
  955. {
  956. if (! ira_reg_pressure_class_p[cl])
  957. continue;
  958. curr_reg_pressure[cl]++;
  959. if (curr_bb_node->reg_pressure[cl] < curr_reg_pressure[cl])
  960. curr_bb_node->reg_pressure[cl] = curr_reg_pressure[cl];
  961. ira_assert (curr_reg_pressure[cl]
  962. <= ira_class_hard_regs_num[cl]);
  963. }
  964. }
  965. EXECUTE_IF_SET_IN_BITMAP (reg_live_out, FIRST_PSEUDO_REGISTER, j, bi)
  966. mark_pseudo_regno_live (j);
  967. freq = REG_FREQ_FROM_BB (bb);
  968. if (freq == 0)
  969. freq = 1;
  970. /* Invalidate all allocno_saved_at_call entries. */
  971. last_call_num++;
  972. /* Scan the code of this basic block, noting which allocnos and
  973. hard regs are born or die.
  974. Note that this loop treats uninitialized values as live until
  975. the beginning of the block. For example, if an instruction
  976. uses (reg:DI foo), and only (subreg:SI (reg:DI foo) 0) is ever
  977. set, FOO will remain live until the beginning of the block.
  978. Likewise if FOO is not set at all. This is unnecessarily
  979. pessimistic, but it probably doesn't matter much in practice. */
  980. FOR_BB_INSNS_REVERSE (bb, insn)
  981. {
  982. ira_allocno_t a;
  983. df_ref def, use;
  984. bool call_p;
  985. if (!NONDEBUG_INSN_P (insn))
  986. continue;
  987. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  988. fprintf (ira_dump_file, " Insn %u(l%d): point = %d\n",
  989. INSN_UID (insn), loop_tree_node->parent->loop_num,
  990. curr_point);
  991. call_p = CALL_P (insn);
  992. #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
  993. int regno;
  994. bool clear_pic_use_conflict_p = false;
  995. /* Processing insn usage in call insn can create conflict
  996. with pic pseudo and pic hard reg and that is wrong.
  997. Check this situation and fix it at the end of the insn
  998. processing. */
  999. if (call_p && pic_offset_table_rtx != NULL_RTX
  1000. && (regno = REGNO (pic_offset_table_rtx)) >= FIRST_PSEUDO_REGISTER
  1001. && (a = ira_curr_regno_allocno_map[regno]) != NULL)
  1002. clear_pic_use_conflict_p
  1003. = (find_regno_fusage (insn, USE, REAL_PIC_OFFSET_TABLE_REGNUM)
  1004. && ! TEST_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS
  1005. (ALLOCNO_OBJECT (a, 0)),
  1006. REAL_PIC_OFFSET_TABLE_REGNUM));
  1007. #endif
  1008. /* Mark each defined value as live. We need to do this for
  1009. unused values because they still conflict with quantities
  1010. that are live at the time of the definition.
  1011. Ignore DF_REF_MAY_CLOBBERs on a call instruction. Such
  1012. references represent the effect of the called function
  1013. on a call-clobbered register. Marking the register as
  1014. live would stop us from allocating it to a call-crossing
  1015. allocno. */
  1016. FOR_EACH_INSN_DEF (def, insn)
  1017. if (!call_p || !DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
  1018. mark_ref_live (def);
  1019. /* If INSN has multiple outputs, then any value used in one
  1020. of the outputs conflicts with the other outputs. Model this
  1021. by making the used value live during the output phase.
  1022. It is unsafe to use !single_set here since it will ignore
  1023. an unused output. Just because an output is unused does
  1024. not mean the compiler can assume the side effect will not
  1025. occur. Consider if ALLOCNO appears in the address of an
  1026. output and we reload the output. If we allocate ALLOCNO
  1027. to the same hard register as an unused output we could
  1028. set the hard register before the output reload insn. */
  1029. if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
  1030. FOR_EACH_INSN_USE (use, insn)
  1031. {
  1032. int i;
  1033. rtx reg;
  1034. reg = DF_REF_REG (use);
  1035. for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  1036. {
  1037. rtx set;
  1038. set = XVECEXP (PATTERN (insn), 0, i);
  1039. if (GET_CODE (set) == SET
  1040. && reg_overlap_mentioned_p (reg, SET_DEST (set)))
  1041. {
  1042. /* After the previous loop, this is a no-op if
  1043. REG is contained within SET_DEST (SET). */
  1044. mark_ref_live (use);
  1045. break;
  1046. }
  1047. }
  1048. }
  1049. extract_insn (insn);
  1050. preferred_alternatives = get_preferred_alternatives (insn);
  1051. preprocess_constraints (insn);
  1052. process_single_reg_class_operands (false, freq);
  1053. /* See which defined values die here. */
  1054. FOR_EACH_INSN_DEF (def, insn)
  1055. if (!call_p || !DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
  1056. mark_ref_dead (def);
  1057. if (call_p)
  1058. {
  1059. /* Try to find a SET in the CALL_INSN_FUNCTION_USAGE, and from
  1060. there, try to find a pseudo that is live across the call but
  1061. can be cheaply reconstructed from the return value. */
  1062. rtx cheap_reg = find_call_crossed_cheap_reg (insn);
  1063. if (cheap_reg != NULL_RTX)
  1064. add_reg_note (insn, REG_RETURNED, cheap_reg);
  1065. last_call_num++;
  1066. sparseset_clear (allocnos_processed);
  1067. /* The current set of live allocnos are live across the call. */
  1068. EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
  1069. {
  1070. ira_object_t obj = ira_object_id_map[i];
  1071. a = OBJECT_ALLOCNO (obj);
  1072. int num = ALLOCNO_NUM (a);
  1073. HARD_REG_SET this_call_used_reg_set;
  1074. get_call_reg_set_usage (insn, &this_call_used_reg_set,
  1075. call_used_reg_set);
  1076. /* Don't allocate allocnos that cross setjmps or any
  1077. call, if this function receives a nonlocal
  1078. goto. */
  1079. if (cfun->has_nonlocal_label
  1080. || find_reg_note (insn, REG_SETJMP,
  1081. NULL_RTX) != NULL_RTX)
  1082. {
  1083. SET_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj));
  1084. SET_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
  1085. }
  1086. if (can_throw_internal (insn))
  1087. {
  1088. IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
  1089. this_call_used_reg_set);
  1090. IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
  1091. this_call_used_reg_set);
  1092. }
  1093. if (sparseset_bit_p (allocnos_processed, num))
  1094. continue;
  1095. sparseset_set_bit (allocnos_processed, num);
  1096. if (allocno_saved_at_call[num] != last_call_num)
  1097. /* Here we are mimicking caller-save.c behavior
  1098. which does not save hard register at a call if
  1099. it was saved on previous call in the same basic
  1100. block and the hard register was not mentioned
  1101. between the two calls. */
  1102. ALLOCNO_CALL_FREQ (a) += freq;
  1103. /* Mark it as saved at the next call. */
  1104. allocno_saved_at_call[num] = last_call_num + 1;
  1105. ALLOCNO_CALLS_CROSSED_NUM (a)++;
  1106. IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a),
  1107. this_call_used_reg_set);
  1108. if (cheap_reg != NULL_RTX
  1109. && ALLOCNO_REGNO (a) == (int) REGNO (cheap_reg))
  1110. ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)++;
  1111. }
  1112. }
  1113. make_early_clobber_and_input_conflicts ();
  1114. curr_point++;
  1115. /* Mark each used value as live. */
  1116. FOR_EACH_INSN_USE (use, insn)
  1117. mark_ref_live (use);
  1118. process_single_reg_class_operands (true, freq);
  1119. set_p = mark_hard_reg_early_clobbers (insn, true);
  1120. if (set_p)
  1121. {
  1122. mark_hard_reg_early_clobbers (insn, false);
  1123. /* Mark each hard reg as live again. For example, a
  1124. hard register can be in clobber and in an insn
  1125. input. */
  1126. FOR_EACH_INSN_USE (use, insn)
  1127. {
  1128. rtx ureg = DF_REF_REG (use);
  1129. if (GET_CODE (ureg) == SUBREG)
  1130. ureg = SUBREG_REG (ureg);
  1131. if (! REG_P (ureg) || REGNO (ureg) >= FIRST_PSEUDO_REGISTER)
  1132. continue;
  1133. mark_ref_live (use);
  1134. }
  1135. }
  1136. #ifdef REAL_PIC_OFFSET_TABLE_REGNUM
  1137. if (clear_pic_use_conflict_p)
  1138. {
  1139. regno = REGNO (pic_offset_table_rtx);
  1140. a = ira_curr_regno_allocno_map[regno];
  1141. CLEAR_HARD_REG_BIT (OBJECT_CONFLICT_HARD_REGS (ALLOCNO_OBJECT (a, 0)),
  1142. REAL_PIC_OFFSET_TABLE_REGNUM);
  1143. CLEAR_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS
  1144. (ALLOCNO_OBJECT (a, 0)),
  1145. REAL_PIC_OFFSET_TABLE_REGNUM);
  1146. }
  1147. #endif
  1148. curr_point++;
  1149. }
  1150. #ifdef EH_RETURN_DATA_REGNO
  1151. if (bb_has_eh_pred (bb))
  1152. for (j = 0; ; ++j)
  1153. {
  1154. unsigned int regno = EH_RETURN_DATA_REGNO (j);
  1155. if (regno == INVALID_REGNUM)
  1156. break;
  1157. make_hard_regno_born (regno);
  1158. }
  1159. #endif
  1160. /* Allocnos can't go in stack regs at the start of a basic block
  1161. that is reached by an abnormal edge. Likewise for call
  1162. clobbered regs, because caller-save, fixup_abnormal_edges and
  1163. possibly the table driven EH machinery are not quite ready to
  1164. handle such allocnos live across such edges. */
  1165. if (bb_has_abnormal_pred (bb))
  1166. {
  1167. #ifdef STACK_REGS
  1168. EXECUTE_IF_SET_IN_SPARSESET (objects_live, px)
  1169. {
  1170. ira_allocno_t a = OBJECT_ALLOCNO (ira_object_id_map[px]);
  1171. ALLOCNO_NO_STACK_REG_P (a) = true;
  1172. ALLOCNO_TOTAL_NO_STACK_REG_P (a) = true;
  1173. }
  1174. for (px = FIRST_STACK_REG; px <= LAST_STACK_REG; px++)
  1175. make_hard_regno_born (px);
  1176. #endif
  1177. /* No need to record conflicts for call clobbered regs if we
  1178. have nonlocal labels around, as we don't ever try to
  1179. allocate such regs in this case. */
  1180. if (!cfun->has_nonlocal_label && bb_has_abnormal_call_pred (bb))
  1181. for (px = 0; px < FIRST_PSEUDO_REGISTER; px++)
  1182. if (call_used_regs[px])
  1183. make_hard_regno_born (px);
  1184. }
  1185. EXECUTE_IF_SET_IN_SPARSESET (objects_live, i)
  1186. make_object_dead (ira_object_id_map[i]);
  1187. curr_point++;
  1188. }
  1189. /* Propagate register pressure to upper loop tree nodes. */
  1190. if (loop_tree_node != ira_loop_tree_root)
  1191. for (i = 0; i < ira_pressure_classes_num; i++)
  1192. {
  1193. enum reg_class pclass;
  1194. pclass = ira_pressure_classes[i];
  1195. if (loop_tree_node->reg_pressure[pclass]
  1196. > loop_tree_node->parent->reg_pressure[pclass])
  1197. loop_tree_node->parent->reg_pressure[pclass]
  1198. = loop_tree_node->reg_pressure[pclass];
  1199. }
  1200. }
  1201. /* Create and set up IRA_START_POINT_RANGES and
  1202. IRA_FINISH_POINT_RANGES. */
  1203. static void
  1204. create_start_finish_chains (void)
  1205. {
  1206. ira_object_t obj;
  1207. ira_object_iterator oi;
  1208. live_range_t r;
  1209. ira_start_point_ranges
  1210. = (live_range_t *) ira_allocate (ira_max_point * sizeof (live_range_t));
  1211. memset (ira_start_point_ranges, 0, ira_max_point * sizeof (live_range_t));
  1212. ira_finish_point_ranges
  1213. = (live_range_t *) ira_allocate (ira_max_point * sizeof (live_range_t));
  1214. memset (ira_finish_point_ranges, 0, ira_max_point * sizeof (live_range_t));
  1215. FOR_EACH_OBJECT (obj, oi)
  1216. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  1217. {
  1218. r->start_next = ira_start_point_ranges[r->start];
  1219. ira_start_point_ranges[r->start] = r;
  1220. r->finish_next = ira_finish_point_ranges[r->finish];
  1221. ira_finish_point_ranges[r->finish] = r;
  1222. }
  1223. }
  1224. /* Rebuild IRA_START_POINT_RANGES and IRA_FINISH_POINT_RANGES after
  1225. new live ranges and program points were added as a result if new
  1226. insn generation. */
  1227. void
  1228. ira_rebuild_start_finish_chains (void)
  1229. {
  1230. ira_free (ira_finish_point_ranges);
  1231. ira_free (ira_start_point_ranges);
  1232. create_start_finish_chains ();
  1233. }
  1234. /* Compress allocno live ranges by removing program points where
  1235. nothing happens. */
  1236. static void
  1237. remove_some_program_points_and_update_live_ranges (void)
  1238. {
  1239. unsigned i;
  1240. int n;
  1241. int *map;
  1242. ira_object_t obj;
  1243. ira_object_iterator oi;
  1244. live_range_t r, prev_r, next_r;
  1245. sbitmap born_or_dead, born, dead;
  1246. sbitmap_iterator sbi;
  1247. bool born_p, dead_p, prev_born_p, prev_dead_p;
  1248. born = sbitmap_alloc (ira_max_point);
  1249. dead = sbitmap_alloc (ira_max_point);
  1250. bitmap_clear (born);
  1251. bitmap_clear (dead);
  1252. FOR_EACH_OBJECT (obj, oi)
  1253. for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
  1254. {
  1255. ira_assert (r->start <= r->finish);
  1256. bitmap_set_bit (born, r->start);
  1257. bitmap_set_bit (dead, r->finish);
  1258. }
  1259. born_or_dead = sbitmap_alloc (ira_max_point);
  1260. bitmap_ior (born_or_dead, born, dead);
  1261. map = (int *) ira_allocate (sizeof (int) * ira_max_point);
  1262. n = -1;
  1263. prev_born_p = prev_dead_p = false;
  1264. EXECUTE_IF_SET_IN_BITMAP (born_or_dead, 0, i, sbi)
  1265. {
  1266. born_p = bitmap_bit_p (born, i);
  1267. dead_p = bitmap_bit_p (dead, i);
  1268. if ((prev_born_p && ! prev_dead_p && born_p && ! dead_p)
  1269. || (prev_dead_p && ! prev_born_p && dead_p && ! born_p))
  1270. map[i] = n;
  1271. else
  1272. map[i] = ++n;
  1273. prev_born_p = born_p;
  1274. prev_dead_p = dead_p;
  1275. }
  1276. sbitmap_free (born_or_dead);
  1277. sbitmap_free (born);
  1278. sbitmap_free (dead);
  1279. n++;
  1280. if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
  1281. fprintf (ira_dump_file, "Compressing live ranges: from %d to %d - %d%%\n",
  1282. ira_max_point, n, 100 * n / ira_max_point);
  1283. ira_max_point = n;
  1284. FOR_EACH_OBJECT (obj, oi)
  1285. for (r = OBJECT_LIVE_RANGES (obj), prev_r = NULL; r != NULL; r = next_r)
  1286. {
  1287. next_r = r->next;
  1288. r->start = map[r->start];
  1289. r->finish = map[r->finish];
  1290. if (prev_r == NULL || prev_r->start > r->finish + 1)
  1291. {
  1292. prev_r = r;
  1293. continue;
  1294. }
  1295. prev_r->start = r->start;
  1296. prev_r->next = next_r;
  1297. ira_finish_live_range (r);
  1298. }
  1299. ira_free (map);
  1300. }
  1301. /* Print live ranges R to file F. */
  1302. void
  1303. ira_print_live_range_list (FILE *f, live_range_t r)
  1304. {
  1305. for (; r != NULL; r = r->next)
  1306. fprintf (f, " [%d..%d]", r->start, r->finish);
  1307. fprintf (f, "\n");
  1308. }
  1309. DEBUG_FUNCTION void
  1310. debug (live_range &ref)
  1311. {
  1312. ira_print_live_range_list (stderr, &ref);
  1313. }
  1314. DEBUG_FUNCTION void
  1315. debug (live_range *ptr)
  1316. {
  1317. if (ptr)
  1318. debug (*ptr);
  1319. else
  1320. fprintf (stderr, "<nil>\n");
  1321. }
  1322. /* Print live ranges R to stderr. */
  1323. void
  1324. ira_debug_live_range_list (live_range_t r)
  1325. {
  1326. ira_print_live_range_list (stderr, r);
  1327. }
  1328. /* Print live ranges of object OBJ to file F. */
  1329. static void
  1330. print_object_live_ranges (FILE *f, ira_object_t obj)
  1331. {
  1332. ira_print_live_range_list (f, OBJECT_LIVE_RANGES (obj));
  1333. }
  1334. /* Print live ranges of allocno A to file F. */
  1335. static void
  1336. print_allocno_live_ranges (FILE *f, ira_allocno_t a)
  1337. {
  1338. int n = ALLOCNO_NUM_OBJECTS (a);
  1339. int i;
  1340. for (i = 0; i < n; i++)
  1341. {
  1342. fprintf (f, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
  1343. if (n > 1)
  1344. fprintf (f, " [%d]", i);
  1345. fprintf (f, "):");
  1346. print_object_live_ranges (f, ALLOCNO_OBJECT (a, i));
  1347. }
  1348. }
  1349. /* Print live ranges of allocno A to stderr. */
  1350. void
  1351. ira_debug_allocno_live_ranges (ira_allocno_t a)
  1352. {
  1353. print_allocno_live_ranges (stderr, a);
  1354. }
  1355. /* Print live ranges of all allocnos to file F. */
  1356. static void
  1357. print_live_ranges (FILE *f)
  1358. {
  1359. ira_allocno_t a;
  1360. ira_allocno_iterator ai;
  1361. FOR_EACH_ALLOCNO (a, ai)
  1362. print_allocno_live_ranges (f, a);
  1363. }
  1364. /* Print live ranges of all allocnos to stderr. */
  1365. void
  1366. ira_debug_live_ranges (void)
  1367. {
  1368. print_live_ranges (stderr);
  1369. }
  1370. /* The main entry function creates live ranges, set up
  1371. CONFLICT_HARD_REGS and TOTAL_CONFLICT_HARD_REGS for objects, and
  1372. calculate register pressure info. */
  1373. void
  1374. ira_create_allocno_live_ranges (void)
  1375. {
  1376. objects_live = sparseset_alloc (ira_objects_num);
  1377. allocnos_processed = sparseset_alloc (ira_allocnos_num);
  1378. curr_point = 0;
  1379. last_call_num = 0;
  1380. allocno_saved_at_call
  1381. = (int *) ira_allocate (ira_allocnos_num * sizeof (int));
  1382. memset (allocno_saved_at_call, 0, ira_allocnos_num * sizeof (int));
  1383. ira_traverse_loop_tree (true, ira_loop_tree_root, NULL,
  1384. process_bb_node_lives);
  1385. ira_max_point = curr_point;
  1386. create_start_finish_chains ();
  1387. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  1388. print_live_ranges (ira_dump_file);
  1389. /* Clean up. */
  1390. ira_free (allocno_saved_at_call);
  1391. sparseset_free (objects_live);
  1392. sparseset_free (allocnos_processed);
  1393. }
  1394. /* Compress allocno live ranges. */
  1395. void
  1396. ira_compress_allocno_live_ranges (void)
  1397. {
  1398. remove_some_program_points_and_update_live_ranges ();
  1399. ira_rebuild_start_finish_chains ();
  1400. if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
  1401. {
  1402. fprintf (ira_dump_file, "Ranges after the compression:\n");
  1403. print_live_ranges (ira_dump_file);
  1404. }
  1405. }
  1406. /* Free arrays IRA_START_POINT_RANGES and IRA_FINISH_POINT_RANGES. */
  1407. void
  1408. ira_finish_allocno_live_ranges (void)
  1409. {
  1410. ira_free (ira_finish_point_ranges);
  1411. ira_free (ira_start_point_ranges);
  1412. }