loop-invariant.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. /* RTL-level loop invariant motion.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This implements the loop invariant motion pass. It is very simple
  16. (no calls, no loads/stores, etc.). This should be sufficient to cleanup
  17. things like address arithmetics -- other more complicated invariants should
  18. be eliminated on GIMPLE either in tree-ssa-loop-im.c or in tree-ssa-pre.c.
  19. We proceed loop by loop -- it is simpler than trying to handle things
  20. globally and should not lose much. First we inspect all sets inside loop
  21. and create a dependency graph on insns (saying "to move this insn, you must
  22. also move the following insns").
  23. We then need to determine what to move. We estimate the number of registers
  24. used and move as many invariants as possible while we still have enough free
  25. registers. We prefer the expensive invariants.
  26. Then we move the selected invariants out of the loop, creating a new
  27. temporaries for them if necessary. */
  28. #include "config.h"
  29. #include "system.h"
  30. #include "coretypes.h"
  31. #include "tm.h"
  32. #include "hard-reg-set.h"
  33. #include "rtl.h"
  34. #include "tm_p.h"
  35. #include "obstack.h"
  36. #include "predict.h"
  37. #include "vec.h"
  38. #include "hashtab.h"
  39. #include "hash-set.h"
  40. #include "machmode.h"
  41. #include "input.h"
  42. #include "function.h"
  43. #include "dominance.h"
  44. #include "cfg.h"
  45. #include "cfgrtl.h"
  46. #include "basic-block.h"
  47. #include "cfgloop.h"
  48. #include "symtab.h"
  49. #include "flags.h"
  50. #include "statistics.h"
  51. #include "double-int.h"
  52. #include "real.h"
  53. #include "fixed-value.h"
  54. #include "alias.h"
  55. #include "wide-int.h"
  56. #include "inchash.h"
  57. #include "tree.h"
  58. #include "insn-config.h"
  59. #include "expmed.h"
  60. #include "dojump.h"
  61. #include "explow.h"
  62. #include "calls.h"
  63. #include "emit-rtl.h"
  64. #include "varasm.h"
  65. #include "stmt.h"
  66. #include "expr.h"
  67. #include "recog.h"
  68. #include "target.h"
  69. #include "df.h"
  70. #include "hash-table.h"
  71. #include "except.h"
  72. #include "params.h"
  73. #include "regs.h"
  74. #include "ira.h"
  75. #include "dumpfile.h"
  76. /* The data stored for the loop. */
  77. struct loop_data
  78. {
  79. struct loop *outermost_exit; /* The outermost exit of the loop. */
  80. bool has_call; /* True if the loop contains a call. */
  81. /* Maximal register pressure inside loop for given register class
  82. (defined only for the pressure classes). */
  83. int max_reg_pressure[N_REG_CLASSES];
  84. /* Loop regs referenced and live pseudo-registers. */
  85. bitmap_head regs_ref;
  86. bitmap_head regs_live;
  87. };
  88. #define LOOP_DATA(LOOP) ((struct loop_data *) (LOOP)->aux)
  89. /* The description of an use. */
  90. struct use
  91. {
  92. rtx *pos; /* Position of the use. */
  93. rtx_insn *insn; /* The insn in that the use occurs. */
  94. unsigned addr_use_p; /* Whether the use occurs in an address. */
  95. struct use *next; /* Next use in the list. */
  96. };
  97. /* The description of a def. */
  98. struct def
  99. {
  100. struct use *uses; /* The list of uses that are uniquely reached
  101. by it. */
  102. unsigned n_uses; /* Number of such uses. */
  103. unsigned n_addr_uses; /* Number of uses in addresses. */
  104. unsigned invno; /* The corresponding invariant. */
  105. };
  106. /* The data stored for each invariant. */
  107. struct invariant
  108. {
  109. /* The number of the invariant. */
  110. unsigned invno;
  111. /* The number of the invariant with the same value. */
  112. unsigned eqto;
  113. /* The number of invariants which eqto this. */
  114. unsigned eqno;
  115. /* If we moved the invariant out of the loop, the register that contains its
  116. value. */
  117. rtx reg;
  118. /* If we moved the invariant out of the loop, the original regno
  119. that contained its value. */
  120. int orig_regno;
  121. /* The definition of the invariant. */
  122. struct def *def;
  123. /* The insn in that it is defined. */
  124. rtx_insn *insn;
  125. /* Whether it is always executed. */
  126. bool always_executed;
  127. /* Whether to move the invariant. */
  128. bool move;
  129. /* Whether the invariant is cheap when used as an address. */
  130. bool cheap_address;
  131. /* Cost of the invariant. */
  132. unsigned cost;
  133. /* The invariants it depends on. */
  134. bitmap depends_on;
  135. /* Used for detecting already visited invariants during determining
  136. costs of movements. */
  137. unsigned stamp;
  138. };
  139. /* Currently processed loop. */
  140. static struct loop *curr_loop;
  141. /* Table of invariants indexed by the df_ref uid field. */
  142. static unsigned int invariant_table_size = 0;
  143. static struct invariant ** invariant_table;
  144. /* Entry for hash table of invariant expressions. */
  145. struct invariant_expr_entry
  146. {
  147. /* The invariant. */
  148. struct invariant *inv;
  149. /* Its value. */
  150. rtx expr;
  151. /* Its mode. */
  152. machine_mode mode;
  153. /* Its hash. */
  154. hashval_t hash;
  155. };
  156. /* The actual stamp for marking already visited invariants during determining
  157. costs of movements. */
  158. static unsigned actual_stamp;
  159. typedef struct invariant *invariant_p;
  160. /* The invariants. */
  161. static vec<invariant_p> invariants;
  162. /* Check the size of the invariant table and realloc if necessary. */
  163. static void
  164. check_invariant_table_size (void)
  165. {
  166. if (invariant_table_size < DF_DEFS_TABLE_SIZE ())
  167. {
  168. unsigned int new_size = DF_DEFS_TABLE_SIZE () + (DF_DEFS_TABLE_SIZE () / 4);
  169. invariant_table = XRESIZEVEC (struct invariant *, invariant_table, new_size);
  170. memset (&invariant_table[invariant_table_size], 0,
  171. (new_size - invariant_table_size) * sizeof (struct invariant *));
  172. invariant_table_size = new_size;
  173. }
  174. }
  175. /* Test for possibility of invariantness of X. */
  176. static bool
  177. check_maybe_invariant (rtx x)
  178. {
  179. enum rtx_code code = GET_CODE (x);
  180. int i, j;
  181. const char *fmt;
  182. switch (code)
  183. {
  184. CASE_CONST_ANY:
  185. case SYMBOL_REF:
  186. case CONST:
  187. case LABEL_REF:
  188. return true;
  189. case PC:
  190. case CC0:
  191. case UNSPEC_VOLATILE:
  192. case CALL:
  193. return false;
  194. case REG:
  195. return true;
  196. case MEM:
  197. /* Load/store motion is done elsewhere. ??? Perhaps also add it here?
  198. It should not be hard, and might be faster than "elsewhere". */
  199. /* Just handle the most trivial case where we load from an unchanging
  200. location (most importantly, pic tables). */
  201. if (MEM_READONLY_P (x) && !MEM_VOLATILE_P (x))
  202. break;
  203. return false;
  204. case ASM_OPERANDS:
  205. /* Don't mess with insns declared volatile. */
  206. if (MEM_VOLATILE_P (x))
  207. return false;
  208. break;
  209. default:
  210. break;
  211. }
  212. fmt = GET_RTX_FORMAT (code);
  213. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  214. {
  215. if (fmt[i] == 'e')
  216. {
  217. if (!check_maybe_invariant (XEXP (x, i)))
  218. return false;
  219. }
  220. else if (fmt[i] == 'E')
  221. {
  222. for (j = 0; j < XVECLEN (x, i); j++)
  223. if (!check_maybe_invariant (XVECEXP (x, i, j)))
  224. return false;
  225. }
  226. }
  227. return true;
  228. }
  229. /* Returns the invariant definition for USE, or NULL if USE is not
  230. invariant. */
  231. static struct invariant *
  232. invariant_for_use (df_ref use)
  233. {
  234. struct df_link *defs;
  235. df_ref def;
  236. basic_block bb = DF_REF_BB (use), def_bb;
  237. if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
  238. return NULL;
  239. defs = DF_REF_CHAIN (use);
  240. if (!defs || defs->next)
  241. return NULL;
  242. def = defs->ref;
  243. check_invariant_table_size ();
  244. if (!invariant_table[DF_REF_ID (def)])
  245. return NULL;
  246. def_bb = DF_REF_BB (def);
  247. if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
  248. return NULL;
  249. return invariant_table[DF_REF_ID (def)];
  250. }
  251. /* Computes hash value for invariant expression X in INSN. */
  252. static hashval_t
  253. hash_invariant_expr_1 (rtx_insn *insn, rtx x)
  254. {
  255. enum rtx_code code = GET_CODE (x);
  256. int i, j;
  257. const char *fmt;
  258. hashval_t val = code;
  259. int do_not_record_p;
  260. df_ref use;
  261. struct invariant *inv;
  262. switch (code)
  263. {
  264. CASE_CONST_ANY:
  265. case SYMBOL_REF:
  266. case CONST:
  267. case LABEL_REF:
  268. return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
  269. case REG:
  270. use = df_find_use (insn, x);
  271. if (!use)
  272. return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
  273. inv = invariant_for_use (use);
  274. if (!inv)
  275. return hash_rtx (x, GET_MODE (x), &do_not_record_p, NULL, false);
  276. gcc_assert (inv->eqto != ~0u);
  277. return inv->eqto;
  278. default:
  279. break;
  280. }
  281. fmt = GET_RTX_FORMAT (code);
  282. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  283. {
  284. if (fmt[i] == 'e')
  285. val ^= hash_invariant_expr_1 (insn, XEXP (x, i));
  286. else if (fmt[i] == 'E')
  287. {
  288. for (j = 0; j < XVECLEN (x, i); j++)
  289. val ^= hash_invariant_expr_1 (insn, XVECEXP (x, i, j));
  290. }
  291. else if (fmt[i] == 'i' || fmt[i] == 'n')
  292. val ^= XINT (x, i);
  293. }
  294. return val;
  295. }
  296. /* Returns true if the invariant expressions E1 and E2 used in insns INSN1
  297. and INSN2 have always the same value. */
  298. static bool
  299. invariant_expr_equal_p (rtx_insn *insn1, rtx e1, rtx_insn *insn2, rtx e2)
  300. {
  301. enum rtx_code code = GET_CODE (e1);
  302. int i, j;
  303. const char *fmt;
  304. df_ref use1, use2;
  305. struct invariant *inv1 = NULL, *inv2 = NULL;
  306. rtx sub1, sub2;
  307. /* If mode of only one of the operands is VOIDmode, it is not equivalent to
  308. the other one. If both are VOIDmode, we rely on the caller of this
  309. function to verify that their modes are the same. */
  310. if (code != GET_CODE (e2) || GET_MODE (e1) != GET_MODE (e2))
  311. return false;
  312. switch (code)
  313. {
  314. CASE_CONST_ANY:
  315. case SYMBOL_REF:
  316. case CONST:
  317. case LABEL_REF:
  318. return rtx_equal_p (e1, e2);
  319. case REG:
  320. use1 = df_find_use (insn1, e1);
  321. use2 = df_find_use (insn2, e2);
  322. if (use1)
  323. inv1 = invariant_for_use (use1);
  324. if (use2)
  325. inv2 = invariant_for_use (use2);
  326. if (!inv1 && !inv2)
  327. return rtx_equal_p (e1, e2);
  328. if (!inv1 || !inv2)
  329. return false;
  330. gcc_assert (inv1->eqto != ~0u);
  331. gcc_assert (inv2->eqto != ~0u);
  332. return inv1->eqto == inv2->eqto;
  333. default:
  334. break;
  335. }
  336. fmt = GET_RTX_FORMAT (code);
  337. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  338. {
  339. if (fmt[i] == 'e')
  340. {
  341. sub1 = XEXP (e1, i);
  342. sub2 = XEXP (e2, i);
  343. if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
  344. return false;
  345. }
  346. else if (fmt[i] == 'E')
  347. {
  348. if (XVECLEN (e1, i) != XVECLEN (e2, i))
  349. return false;
  350. for (j = 0; j < XVECLEN (e1, i); j++)
  351. {
  352. sub1 = XVECEXP (e1, i, j);
  353. sub2 = XVECEXP (e2, i, j);
  354. if (!invariant_expr_equal_p (insn1, sub1, insn2, sub2))
  355. return false;
  356. }
  357. }
  358. else if (fmt[i] == 'i' || fmt[i] == 'n')
  359. {
  360. if (XINT (e1, i) != XINT (e2, i))
  361. return false;
  362. }
  363. /* Unhandled type of subexpression, we fail conservatively. */
  364. else
  365. return false;
  366. }
  367. return true;
  368. }
  369. struct invariant_expr_hasher : typed_free_remove <invariant_expr_entry>
  370. {
  371. typedef invariant_expr_entry value_type;
  372. typedef invariant_expr_entry compare_type;
  373. static inline hashval_t hash (const value_type *);
  374. static inline bool equal (const value_type *, const compare_type *);
  375. };
  376. /* Returns hash value for invariant expression entry ENTRY. */
  377. inline hashval_t
  378. invariant_expr_hasher::hash (const value_type *entry)
  379. {
  380. return entry->hash;
  381. }
  382. /* Compares invariant expression entries ENTRY1 and ENTRY2. */
  383. inline bool
  384. invariant_expr_hasher::equal (const value_type *entry1,
  385. const compare_type *entry2)
  386. {
  387. if (entry1->mode != entry2->mode)
  388. return 0;
  389. return invariant_expr_equal_p (entry1->inv->insn, entry1->expr,
  390. entry2->inv->insn, entry2->expr);
  391. }
  392. typedef hash_table<invariant_expr_hasher> invariant_htab_type;
  393. /* Checks whether invariant with value EXPR in machine mode MODE is
  394. recorded in EQ. If this is the case, return the invariant. Otherwise
  395. insert INV to the table for this expression and return INV. */
  396. static struct invariant *
  397. find_or_insert_inv (invariant_htab_type *eq, rtx expr, machine_mode mode,
  398. struct invariant *inv)
  399. {
  400. hashval_t hash = hash_invariant_expr_1 (inv->insn, expr);
  401. struct invariant_expr_entry *entry;
  402. struct invariant_expr_entry pentry;
  403. invariant_expr_entry **slot;
  404. pentry.expr = expr;
  405. pentry.inv = inv;
  406. pentry.mode = mode;
  407. slot = eq->find_slot_with_hash (&pentry, hash, INSERT);
  408. entry = *slot;
  409. if (entry)
  410. return entry->inv;
  411. entry = XNEW (struct invariant_expr_entry);
  412. entry->inv = inv;
  413. entry->expr = expr;
  414. entry->mode = mode;
  415. entry->hash = hash;
  416. *slot = entry;
  417. return inv;
  418. }
  419. /* Finds invariants identical to INV and records the equivalence. EQ is the
  420. hash table of the invariants. */
  421. static void
  422. find_identical_invariants (invariant_htab_type *eq, struct invariant *inv)
  423. {
  424. unsigned depno;
  425. bitmap_iterator bi;
  426. struct invariant *dep;
  427. rtx expr, set;
  428. machine_mode mode;
  429. struct invariant *tmp;
  430. if (inv->eqto != ~0u)
  431. return;
  432. EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
  433. {
  434. dep = invariants[depno];
  435. find_identical_invariants (eq, dep);
  436. }
  437. set = single_set (inv->insn);
  438. expr = SET_SRC (set);
  439. mode = GET_MODE (expr);
  440. if (mode == VOIDmode)
  441. mode = GET_MODE (SET_DEST (set));
  442. tmp = find_or_insert_inv (eq, expr, mode, inv);
  443. inv->eqto = tmp->invno;
  444. if (tmp->invno != inv->invno && inv->always_executed)
  445. tmp->eqno++;
  446. if (dump_file && inv->eqto != inv->invno)
  447. fprintf (dump_file,
  448. "Invariant %d is equivalent to invariant %d.\n",
  449. inv->invno, inv->eqto);
  450. }
  451. /* Find invariants with the same value and record the equivalences. */
  452. static void
  453. merge_identical_invariants (void)
  454. {
  455. unsigned i;
  456. struct invariant *inv;
  457. invariant_htab_type eq (invariants.length ());
  458. FOR_EACH_VEC_ELT (invariants, i, inv)
  459. find_identical_invariants (&eq, inv);
  460. }
  461. /* Determines the basic blocks inside LOOP that are always executed and
  462. stores their bitmap to ALWAYS_REACHED. MAY_EXIT is a bitmap of
  463. basic blocks that may either exit the loop, or contain the call that
  464. does not have to return. BODY is body of the loop obtained by
  465. get_loop_body_in_dom_order. */
  466. static void
  467. compute_always_reached (struct loop *loop, basic_block *body,
  468. bitmap may_exit, bitmap always_reached)
  469. {
  470. unsigned i;
  471. for (i = 0; i < loop->num_nodes; i++)
  472. {
  473. if (dominated_by_p (CDI_DOMINATORS, loop->latch, body[i]))
  474. bitmap_set_bit (always_reached, i);
  475. if (bitmap_bit_p (may_exit, i))
  476. return;
  477. }
  478. }
  479. /* Finds exits out of the LOOP with body BODY. Marks blocks in that we may
  480. exit the loop by cfg edge to HAS_EXIT and MAY_EXIT. In MAY_EXIT
  481. additionally mark blocks that may exit due to a call. */
  482. static void
  483. find_exits (struct loop *loop, basic_block *body,
  484. bitmap may_exit, bitmap has_exit)
  485. {
  486. unsigned i;
  487. edge_iterator ei;
  488. edge e;
  489. struct loop *outermost_exit = loop, *aexit;
  490. bool has_call = false;
  491. rtx_insn *insn;
  492. for (i = 0; i < loop->num_nodes; i++)
  493. {
  494. if (body[i]->loop_father == loop)
  495. {
  496. FOR_BB_INSNS (body[i], insn)
  497. {
  498. if (CALL_P (insn)
  499. && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
  500. || !RTL_CONST_OR_PURE_CALL_P (insn)))
  501. {
  502. has_call = true;
  503. bitmap_set_bit (may_exit, i);
  504. break;
  505. }
  506. }
  507. FOR_EACH_EDGE (e, ei, body[i]->succs)
  508. {
  509. if (flow_bb_inside_loop_p (loop, e->dest))
  510. continue;
  511. bitmap_set_bit (may_exit, i);
  512. bitmap_set_bit (has_exit, i);
  513. outermost_exit = find_common_loop (outermost_exit,
  514. e->dest->loop_father);
  515. }
  516. continue;
  517. }
  518. /* Use the data stored for the subloop to decide whether we may exit
  519. through it. It is sufficient to do this for header of the loop,
  520. as other basic blocks inside it must be dominated by it. */
  521. if (body[i]->loop_father->header != body[i])
  522. continue;
  523. if (LOOP_DATA (body[i]->loop_father)->has_call)
  524. {
  525. has_call = true;
  526. bitmap_set_bit (may_exit, i);
  527. }
  528. aexit = LOOP_DATA (body[i]->loop_father)->outermost_exit;
  529. if (aexit != loop)
  530. {
  531. bitmap_set_bit (may_exit, i);
  532. bitmap_set_bit (has_exit, i);
  533. if (flow_loop_nested_p (aexit, outermost_exit))
  534. outermost_exit = aexit;
  535. }
  536. }
  537. if (loop->aux == NULL)
  538. {
  539. loop->aux = xcalloc (1, sizeof (struct loop_data));
  540. bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
  541. bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
  542. }
  543. LOOP_DATA (loop)->outermost_exit = outermost_exit;
  544. LOOP_DATA (loop)->has_call = has_call;
  545. }
  546. /* Check whether we may assign a value to X from a register. */
  547. static bool
  548. may_assign_reg_p (rtx x)
  549. {
  550. return (GET_MODE (x) != VOIDmode
  551. && GET_MODE (x) != BLKmode
  552. && can_copy_p (GET_MODE (x))
  553. && (!REG_P (x)
  554. || !HARD_REGISTER_P (x)
  555. || REGNO_REG_CLASS (REGNO (x)) != NO_REGS));
  556. }
  557. /* Finds definitions that may correspond to invariants in LOOP with body
  558. BODY. */
  559. static void
  560. find_defs (struct loop *loop)
  561. {
  562. if (dump_file)
  563. {
  564. fprintf (dump_file,
  565. "*****starting processing of loop %d ******\n",
  566. loop->num);
  567. }
  568. df_remove_problem (df_chain);
  569. df_process_deferred_rescans ();
  570. df_chain_add_problem (DF_UD_CHAIN);
  571. df_set_flags (DF_RD_PRUNE_DEAD_DEFS);
  572. df_analyze_loop (loop);
  573. check_invariant_table_size ();
  574. if (dump_file)
  575. {
  576. df_dump_region (dump_file);
  577. fprintf (dump_file,
  578. "*****ending processing of loop %d ******\n",
  579. loop->num);
  580. }
  581. }
  582. /* Creates a new invariant for definition DEF in INSN, depending on invariants
  583. in DEPENDS_ON. ALWAYS_EXECUTED is true if the insn is always executed,
  584. unless the program ends due to a function call. The newly created invariant
  585. is returned. */
  586. static struct invariant *
  587. create_new_invariant (struct def *def, rtx_insn *insn, bitmap depends_on,
  588. bool always_executed)
  589. {
  590. struct invariant *inv = XNEW (struct invariant);
  591. rtx set = single_set (insn);
  592. bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
  593. inv->def = def;
  594. inv->always_executed = always_executed;
  595. inv->depends_on = depends_on;
  596. /* If the set is simple, usually by moving it we move the whole store out of
  597. the loop. Otherwise we save only cost of the computation. */
  598. if (def)
  599. {
  600. inv->cost = set_rtx_cost (set, speed);
  601. /* ??? Try to determine cheapness of address computation. Unfortunately
  602. the address cost is only a relative measure, we can't really compare
  603. it with any absolute number, but only with other address costs.
  604. But here we don't have any other addresses, so compare with a magic
  605. number anyway. It has to be large enough to not regress PR33928
  606. (by avoiding to move reg+8,reg+16,reg+24 invariants), but small
  607. enough to not regress 410.bwaves either (by still moving reg+reg
  608. invariants).
  609. See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html . */
  610. inv->cheap_address = address_cost (SET_SRC (set), word_mode,
  611. ADDR_SPACE_GENERIC, speed) < 3;
  612. }
  613. else
  614. {
  615. inv->cost = set_src_cost (SET_SRC (set), speed);
  616. inv->cheap_address = false;
  617. }
  618. inv->move = false;
  619. inv->reg = NULL_RTX;
  620. inv->orig_regno = -1;
  621. inv->stamp = 0;
  622. inv->insn = insn;
  623. inv->invno = invariants.length ();
  624. inv->eqto = ~0u;
  625. /* Itself. */
  626. inv->eqno = 1;
  627. if (def)
  628. def->invno = inv->invno;
  629. invariants.safe_push (inv);
  630. if (dump_file)
  631. {
  632. fprintf (dump_file,
  633. "Set in insn %d is invariant (%d), cost %d, depends on ",
  634. INSN_UID (insn), inv->invno, inv->cost);
  635. dump_bitmap (dump_file, inv->depends_on);
  636. }
  637. return inv;
  638. }
  639. /* Record USE at DEF. */
  640. static void
  641. record_use (struct def *def, df_ref use)
  642. {
  643. struct use *u = XNEW (struct use);
  644. u->pos = DF_REF_REAL_LOC (use);
  645. u->insn = DF_REF_INSN (use);
  646. u->addr_use_p = (DF_REF_TYPE (use) == DF_REF_REG_MEM_LOAD
  647. || DF_REF_TYPE (use) == DF_REF_REG_MEM_STORE);
  648. u->next = def->uses;
  649. def->uses = u;
  650. def->n_uses++;
  651. if (u->addr_use_p)
  652. def->n_addr_uses++;
  653. }
  654. /* Finds the invariants USE depends on and store them to the DEPENDS_ON
  655. bitmap. Returns true if all dependencies of USE are known to be
  656. loop invariants, false otherwise. */
  657. static bool
  658. check_dependency (basic_block bb, df_ref use, bitmap depends_on)
  659. {
  660. df_ref def;
  661. basic_block def_bb;
  662. struct df_link *defs;
  663. struct def *def_data;
  664. struct invariant *inv;
  665. if (DF_REF_FLAGS (use) & DF_REF_READ_WRITE)
  666. return false;
  667. defs = DF_REF_CHAIN (use);
  668. if (!defs)
  669. {
  670. unsigned int regno = DF_REF_REGNO (use);
  671. /* If this is the use of an uninitialized argument register that is
  672. likely to be spilled, do not move it lest this might extend its
  673. lifetime and cause reload to die. This can occur for a call to
  674. a function taking complex number arguments and moving the insns
  675. preparing the arguments without moving the call itself wouldn't
  676. gain much in practice. */
  677. if ((DF_REF_FLAGS (use) & DF_HARD_REG_LIVE)
  678. && FUNCTION_ARG_REGNO_P (regno)
  679. && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno)))
  680. return false;
  681. return true;
  682. }
  683. if (defs->next)
  684. return false;
  685. def = defs->ref;
  686. check_invariant_table_size ();
  687. inv = invariant_table[DF_REF_ID (def)];
  688. if (!inv)
  689. return false;
  690. def_data = inv->def;
  691. gcc_assert (def_data != NULL);
  692. def_bb = DF_REF_BB (def);
  693. /* Note that in case bb == def_bb, we know that the definition
  694. dominates insn, because def has invariant_table[DF_REF_ID(def)]
  695. defined and we process the insns in the basic block bb
  696. sequentially. */
  697. if (!dominated_by_p (CDI_DOMINATORS, bb, def_bb))
  698. return false;
  699. bitmap_set_bit (depends_on, def_data->invno);
  700. return true;
  701. }
  702. /* Finds the invariants INSN depends on and store them to the DEPENDS_ON
  703. bitmap. Returns true if all dependencies of INSN are known to be
  704. loop invariants, false otherwise. */
  705. static bool
  706. check_dependencies (rtx_insn *insn, bitmap depends_on)
  707. {
  708. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  709. df_ref use;
  710. basic_block bb = BLOCK_FOR_INSN (insn);
  711. FOR_EACH_INSN_INFO_USE (use, insn_info)
  712. if (!check_dependency (bb, use, depends_on))
  713. return false;
  714. FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
  715. if (!check_dependency (bb, use, depends_on))
  716. return false;
  717. return true;
  718. }
  719. /* Pre-check candidate DEST to skip the one which can not make a valid insn
  720. during move_invariant_reg. SIMPLE is to skip HARD_REGISTER. */
  721. static bool
  722. pre_check_invariant_p (bool simple, rtx dest)
  723. {
  724. if (simple && REG_P (dest) && DF_REG_DEF_COUNT (REGNO (dest)) > 1)
  725. {
  726. df_ref use;
  727. rtx ref;
  728. unsigned int i = REGNO (dest);
  729. struct df_insn_info *insn_info;
  730. df_ref def_rec;
  731. for (use = DF_REG_USE_CHAIN (i); use; use = DF_REF_NEXT_REG (use))
  732. {
  733. ref = DF_REF_INSN (use);
  734. insn_info = DF_INSN_INFO_GET (ref);
  735. FOR_EACH_INSN_INFO_DEF (def_rec, insn_info)
  736. if (DF_REF_REGNO (def_rec) == i)
  737. {
  738. /* Multi definitions at this stage, most likely are due to
  739. instruction constraints, which requires both read and write
  740. on the same register. Since move_invariant_reg is not
  741. powerful enough to handle such cases, just ignore the INV
  742. and leave the chance to others. */
  743. return false;
  744. }
  745. }
  746. }
  747. return true;
  748. }
  749. /* Finds invariant in INSN. ALWAYS_REACHED is true if the insn is always
  750. executed. ALWAYS_EXECUTED is true if the insn is always executed,
  751. unless the program ends due to a function call. */
  752. static void
  753. find_invariant_insn (rtx_insn *insn, bool always_reached, bool always_executed)
  754. {
  755. df_ref ref;
  756. struct def *def;
  757. bitmap depends_on;
  758. rtx set, dest;
  759. bool simple = true;
  760. struct invariant *inv;
  761. #ifdef HAVE_cc0
  762. /* We can't move a CC0 setter without the user. */
  763. if (sets_cc0_p (insn))
  764. return;
  765. #endif
  766. set = single_set (insn);
  767. if (!set)
  768. return;
  769. dest = SET_DEST (set);
  770. if (!REG_P (dest)
  771. || HARD_REGISTER_P (dest))
  772. simple = false;
  773. if (!may_assign_reg_p (dest)
  774. || !pre_check_invariant_p (simple, dest)
  775. || !check_maybe_invariant (SET_SRC (set)))
  776. return;
  777. /* If the insn can throw exception, we cannot move it at all without changing
  778. cfg. */
  779. if (can_throw_internal (insn))
  780. return;
  781. /* We cannot make trapping insn executed, unless it was executed before. */
  782. if (may_trap_or_fault_p (PATTERN (insn)) && !always_reached)
  783. return;
  784. depends_on = BITMAP_ALLOC (NULL);
  785. if (!check_dependencies (insn, depends_on))
  786. {
  787. BITMAP_FREE (depends_on);
  788. return;
  789. }
  790. if (simple)
  791. def = XCNEW (struct def);
  792. else
  793. def = NULL;
  794. inv = create_new_invariant (def, insn, depends_on, always_executed);
  795. if (simple)
  796. {
  797. ref = df_find_def (insn, dest);
  798. check_invariant_table_size ();
  799. invariant_table[DF_REF_ID (ref)] = inv;
  800. }
  801. }
  802. /* Record registers used in INSN that have a unique invariant definition. */
  803. static void
  804. record_uses (rtx_insn *insn)
  805. {
  806. struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
  807. df_ref use;
  808. struct invariant *inv;
  809. FOR_EACH_INSN_INFO_USE (use, insn_info)
  810. {
  811. inv = invariant_for_use (use);
  812. if (inv)
  813. record_use (inv->def, use);
  814. }
  815. FOR_EACH_INSN_INFO_EQ_USE (use, insn_info)
  816. {
  817. inv = invariant_for_use (use);
  818. if (inv)
  819. record_use (inv->def, use);
  820. }
  821. }
  822. /* Finds invariants in INSN. ALWAYS_REACHED is true if the insn is always
  823. executed. ALWAYS_EXECUTED is true if the insn is always executed,
  824. unless the program ends due to a function call. */
  825. static void
  826. find_invariants_insn (rtx_insn *insn, bool always_reached, bool always_executed)
  827. {
  828. find_invariant_insn (insn, always_reached, always_executed);
  829. record_uses (insn);
  830. }
  831. /* Finds invariants in basic block BB. ALWAYS_REACHED is true if the
  832. basic block is always executed. ALWAYS_EXECUTED is true if the basic
  833. block is always executed, unless the program ends due to a function
  834. call. */
  835. static void
  836. find_invariants_bb (basic_block bb, bool always_reached, bool always_executed)
  837. {
  838. rtx_insn *insn;
  839. FOR_BB_INSNS (bb, insn)
  840. {
  841. if (!NONDEBUG_INSN_P (insn))
  842. continue;
  843. find_invariants_insn (insn, always_reached, always_executed);
  844. if (always_reached
  845. && CALL_P (insn)
  846. && (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
  847. || ! RTL_CONST_OR_PURE_CALL_P (insn)))
  848. always_reached = false;
  849. }
  850. }
  851. /* Finds invariants in LOOP with body BODY. ALWAYS_REACHED is the bitmap of
  852. basic blocks in BODY that are always executed. ALWAYS_EXECUTED is the
  853. bitmap of basic blocks in BODY that are always executed unless the program
  854. ends due to a function call. */
  855. static void
  856. find_invariants_body (struct loop *loop, basic_block *body,
  857. bitmap always_reached, bitmap always_executed)
  858. {
  859. unsigned i;
  860. for (i = 0; i < loop->num_nodes; i++)
  861. find_invariants_bb (body[i],
  862. bitmap_bit_p (always_reached, i),
  863. bitmap_bit_p (always_executed, i));
  864. }
  865. /* Finds invariants in LOOP. */
  866. static void
  867. find_invariants (struct loop *loop)
  868. {
  869. bitmap may_exit = BITMAP_ALLOC (NULL);
  870. bitmap always_reached = BITMAP_ALLOC (NULL);
  871. bitmap has_exit = BITMAP_ALLOC (NULL);
  872. bitmap always_executed = BITMAP_ALLOC (NULL);
  873. basic_block *body = get_loop_body_in_dom_order (loop);
  874. find_exits (loop, body, may_exit, has_exit);
  875. compute_always_reached (loop, body, may_exit, always_reached);
  876. compute_always_reached (loop, body, has_exit, always_executed);
  877. find_defs (loop);
  878. find_invariants_body (loop, body, always_reached, always_executed);
  879. merge_identical_invariants ();
  880. BITMAP_FREE (always_reached);
  881. BITMAP_FREE (always_executed);
  882. BITMAP_FREE (may_exit);
  883. BITMAP_FREE (has_exit);
  884. free (body);
  885. }
  886. /* Frees a list of uses USE. */
  887. static void
  888. free_use_list (struct use *use)
  889. {
  890. struct use *next;
  891. for (; use; use = next)
  892. {
  893. next = use->next;
  894. free (use);
  895. }
  896. }
  897. /* Return pressure class and number of hard registers (through *NREGS)
  898. for destination of INSN. */
  899. static enum reg_class
  900. get_pressure_class_and_nregs (rtx_insn *insn, int *nregs)
  901. {
  902. rtx reg;
  903. enum reg_class pressure_class;
  904. rtx set = single_set (insn);
  905. /* Considered invariant insns have only one set. */
  906. gcc_assert (set != NULL_RTX);
  907. reg = SET_DEST (set);
  908. if (GET_CODE (reg) == SUBREG)
  909. reg = SUBREG_REG (reg);
  910. if (MEM_P (reg))
  911. {
  912. *nregs = 0;
  913. pressure_class = NO_REGS;
  914. }
  915. else
  916. {
  917. if (! REG_P (reg))
  918. reg = NULL_RTX;
  919. if (reg == NULL_RTX)
  920. pressure_class = GENERAL_REGS;
  921. else
  922. {
  923. pressure_class = reg_allocno_class (REGNO (reg));
  924. pressure_class = ira_pressure_class_translate[pressure_class];
  925. }
  926. *nregs
  927. = ira_reg_class_max_nregs[pressure_class][GET_MODE (SET_SRC (set))];
  928. }
  929. return pressure_class;
  930. }
  931. /* Calculates cost and number of registers needed for moving invariant INV
  932. out of the loop and stores them to *COST and *REGS_NEEDED. *CL will be
  933. the REG_CLASS of INV. Return
  934. -1: if INV is invalid.
  935. 0: if INV and its depends_on have same reg_class
  936. 1: if INV and its depends_on have different reg_classes. */
  937. static int
  938. get_inv_cost (struct invariant *inv, int *comp_cost, unsigned *regs_needed,
  939. enum reg_class *cl)
  940. {
  941. int i, acomp_cost;
  942. unsigned aregs_needed[N_REG_CLASSES];
  943. unsigned depno;
  944. struct invariant *dep;
  945. bitmap_iterator bi;
  946. int ret = 1;
  947. /* Find the representative of the class of the equivalent invariants. */
  948. inv = invariants[inv->eqto];
  949. *comp_cost = 0;
  950. if (! flag_ira_loop_pressure)
  951. regs_needed[0] = 0;
  952. else
  953. {
  954. for (i = 0; i < ira_pressure_classes_num; i++)
  955. regs_needed[ira_pressure_classes[i]] = 0;
  956. }
  957. if (inv->move
  958. || inv->stamp == actual_stamp)
  959. return -1;
  960. inv->stamp = actual_stamp;
  961. if (! flag_ira_loop_pressure)
  962. regs_needed[0]++;
  963. else
  964. {
  965. int nregs;
  966. enum reg_class pressure_class;
  967. pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
  968. regs_needed[pressure_class] += nregs;
  969. *cl = pressure_class;
  970. ret = 0;
  971. }
  972. if (!inv->cheap_address
  973. || inv->def->n_addr_uses < inv->def->n_uses)
  974. (*comp_cost) += inv->cost * inv->eqno;
  975. #ifdef STACK_REGS
  976. {
  977. /* Hoisting constant pool constants into stack regs may cost more than
  978. just single register. On x87, the balance is affected both by the
  979. small number of FP registers, and by its register stack organization,
  980. that forces us to add compensation code in and around the loop to
  981. shuffle the operands to the top of stack before use, and pop them
  982. from the stack after the loop finishes.
  983. To model this effect, we increase the number of registers needed for
  984. stack registers by two: one register push, and one register pop.
  985. This usually has the effect that FP constant loads from the constant
  986. pool are not moved out of the loop.
  987. Note that this also means that dependent invariants can not be moved.
  988. However, the primary purpose of this pass is to move loop invariant
  989. address arithmetic out of loops, and address arithmetic that depends
  990. on floating point constants is unlikely to ever occur. */
  991. rtx set = single_set (inv->insn);
  992. if (set
  993. && IS_STACK_MODE (GET_MODE (SET_SRC (set)))
  994. && constant_pool_constant_p (SET_SRC (set)))
  995. {
  996. if (flag_ira_loop_pressure)
  997. regs_needed[ira_stack_reg_pressure_class] += 2;
  998. else
  999. regs_needed[0] += 2;
  1000. }
  1001. }
  1002. #endif
  1003. EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, depno, bi)
  1004. {
  1005. bool check_p;
  1006. enum reg_class dep_cl = ALL_REGS;
  1007. int dep_ret;
  1008. dep = invariants[depno];
  1009. /* If DEP is moved out of the loop, it is not a depends_on any more. */
  1010. if (dep->move)
  1011. continue;
  1012. dep_ret = get_inv_cost (dep, &acomp_cost, aregs_needed, &dep_cl);
  1013. if (! flag_ira_loop_pressure)
  1014. check_p = aregs_needed[0] != 0;
  1015. else
  1016. {
  1017. for (i = 0; i < ira_pressure_classes_num; i++)
  1018. if (aregs_needed[ira_pressure_classes[i]] != 0)
  1019. break;
  1020. check_p = i < ira_pressure_classes_num;
  1021. if ((dep_ret == 1) || ((dep_ret == 0) && (*cl != dep_cl)))
  1022. {
  1023. *cl = ALL_REGS;
  1024. ret = 1;
  1025. }
  1026. }
  1027. if (check_p
  1028. /* We need to check always_executed, since if the original value of
  1029. the invariant may be preserved, we may need to keep it in a
  1030. separate register. TODO check whether the register has an
  1031. use outside of the loop. */
  1032. && dep->always_executed
  1033. && !dep->def->uses->next)
  1034. {
  1035. /* If this is a single use, after moving the dependency we will not
  1036. need a new register. */
  1037. if (! flag_ira_loop_pressure)
  1038. aregs_needed[0]--;
  1039. else
  1040. {
  1041. int nregs;
  1042. enum reg_class pressure_class;
  1043. pressure_class = get_pressure_class_and_nregs (inv->insn, &nregs);
  1044. aregs_needed[pressure_class] -= nregs;
  1045. }
  1046. }
  1047. if (! flag_ira_loop_pressure)
  1048. regs_needed[0] += aregs_needed[0];
  1049. else
  1050. {
  1051. for (i = 0; i < ira_pressure_classes_num; i++)
  1052. regs_needed[ira_pressure_classes[i]]
  1053. += aregs_needed[ira_pressure_classes[i]];
  1054. }
  1055. (*comp_cost) += acomp_cost;
  1056. }
  1057. return ret;
  1058. }
  1059. /* Calculates gain for eliminating invariant INV. REGS_USED is the number
  1060. of registers used in the loop, NEW_REGS is the number of new variables
  1061. already added due to the invariant motion. The number of registers needed
  1062. for it is stored in *REGS_NEEDED. SPEED and CALL_P are flags passed
  1063. through to estimate_reg_pressure_cost. */
  1064. static int
  1065. gain_for_invariant (struct invariant *inv, unsigned *regs_needed,
  1066. unsigned *new_regs, unsigned regs_used,
  1067. bool speed, bool call_p)
  1068. {
  1069. int comp_cost, size_cost;
  1070. /* Workaround -Wmaybe-uninitialized false positive during
  1071. profiledbootstrap by initializing it. */
  1072. enum reg_class cl = NO_REGS;
  1073. int ret;
  1074. actual_stamp++;
  1075. ret = get_inv_cost (inv, &comp_cost, regs_needed, &cl);
  1076. if (! flag_ira_loop_pressure)
  1077. {
  1078. size_cost = (estimate_reg_pressure_cost (new_regs[0] + regs_needed[0],
  1079. regs_used, speed, call_p)
  1080. - estimate_reg_pressure_cost (new_regs[0],
  1081. regs_used, speed, call_p));
  1082. }
  1083. else if (ret < 0)
  1084. return -1;
  1085. else if ((ret == 0) && (cl == NO_REGS))
  1086. /* Hoist it anyway since it does not impact register pressure. */
  1087. return 1;
  1088. else
  1089. {
  1090. int i;
  1091. enum reg_class pressure_class;
  1092. for (i = 0; i < ira_pressure_classes_num; i++)
  1093. {
  1094. pressure_class = ira_pressure_classes[i];
  1095. if (!reg_classes_intersect_p (pressure_class, cl))
  1096. continue;
  1097. if ((int) new_regs[pressure_class]
  1098. + (int) regs_needed[pressure_class]
  1099. + LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
  1100. + IRA_LOOP_RESERVED_REGS
  1101. > ira_class_hard_regs_num[pressure_class])
  1102. break;
  1103. }
  1104. if (i < ira_pressure_classes_num)
  1105. /* There will be register pressure excess and we want not to
  1106. make this loop invariant motion. All loop invariants with
  1107. non-positive gains will be rejected in function
  1108. find_invariants_to_move. Therefore we return the negative
  1109. number here.
  1110. One could think that this rejects also expensive loop
  1111. invariant motions and this will hurt code performance.
  1112. However numerous experiments with different heuristics
  1113. taking invariant cost into account did not confirm this
  1114. assumption. There are possible explanations for this
  1115. result:
  1116. o probably all expensive invariants were already moved out
  1117. of the loop by PRE and gimple invariant motion pass.
  1118. o expensive invariant execution will be hidden by insn
  1119. scheduling or OOO processor hardware because usually such
  1120. invariants have a lot of freedom to be executed
  1121. out-of-order.
  1122. Another reason for ignoring invariant cost vs spilling cost
  1123. heuristics is also in difficulties to evaluate accurately
  1124. spill cost at this stage. */
  1125. return -1;
  1126. else
  1127. size_cost = 0;
  1128. }
  1129. return comp_cost - size_cost;
  1130. }
  1131. /* Finds invariant with best gain for moving. Returns the gain, stores
  1132. the invariant in *BEST and number of registers needed for it to
  1133. *REGS_NEEDED. REGS_USED is the number of registers used in the loop.
  1134. NEW_REGS is the number of new variables already added due to invariant
  1135. motion. */
  1136. static int
  1137. best_gain_for_invariant (struct invariant **best, unsigned *regs_needed,
  1138. unsigned *new_regs, unsigned regs_used,
  1139. bool speed, bool call_p)
  1140. {
  1141. struct invariant *inv;
  1142. int i, gain = 0, again;
  1143. unsigned aregs_needed[N_REG_CLASSES], invno;
  1144. FOR_EACH_VEC_ELT (invariants, invno, inv)
  1145. {
  1146. if (inv->move)
  1147. continue;
  1148. /* Only consider the "representatives" of equivalent invariants. */
  1149. if (inv->eqto != inv->invno)
  1150. continue;
  1151. again = gain_for_invariant (inv, aregs_needed, new_regs, regs_used,
  1152. speed, call_p);
  1153. if (again > gain)
  1154. {
  1155. gain = again;
  1156. *best = inv;
  1157. if (! flag_ira_loop_pressure)
  1158. regs_needed[0] = aregs_needed[0];
  1159. else
  1160. {
  1161. for (i = 0; i < ira_pressure_classes_num; i++)
  1162. regs_needed[ira_pressure_classes[i]]
  1163. = aregs_needed[ira_pressure_classes[i]];
  1164. }
  1165. }
  1166. }
  1167. return gain;
  1168. }
  1169. /* Marks invariant INVNO and all its dependencies for moving. */
  1170. static void
  1171. set_move_mark (unsigned invno, int gain)
  1172. {
  1173. struct invariant *inv = invariants[invno];
  1174. bitmap_iterator bi;
  1175. /* Find the representative of the class of the equivalent invariants. */
  1176. inv = invariants[inv->eqto];
  1177. if (inv->move)
  1178. return;
  1179. inv->move = true;
  1180. if (dump_file)
  1181. {
  1182. if (gain >= 0)
  1183. fprintf (dump_file, "Decided to move invariant %d -- gain %d\n",
  1184. invno, gain);
  1185. else
  1186. fprintf (dump_file, "Decided to move dependent invariant %d\n",
  1187. invno);
  1188. };
  1189. EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, invno, bi)
  1190. {
  1191. set_move_mark (invno, -1);
  1192. }
  1193. }
  1194. /* Determines which invariants to move. */
  1195. static void
  1196. find_invariants_to_move (bool speed, bool call_p)
  1197. {
  1198. int gain;
  1199. unsigned i, regs_used, regs_needed[N_REG_CLASSES], new_regs[N_REG_CLASSES];
  1200. struct invariant *inv = NULL;
  1201. if (!invariants.length ())
  1202. return;
  1203. if (flag_ira_loop_pressure)
  1204. /* REGS_USED is actually never used when the flag is on. */
  1205. regs_used = 0;
  1206. else
  1207. /* We do not really do a good job in estimating number of
  1208. registers used; we put some initial bound here to stand for
  1209. induction variables etc. that we do not detect. */
  1210. {
  1211. unsigned int n_regs = DF_REG_SIZE (df);
  1212. regs_used = 2;
  1213. for (i = 0; i < n_regs; i++)
  1214. {
  1215. if (!DF_REGNO_FIRST_DEF (i) && DF_REGNO_LAST_USE (i))
  1216. {
  1217. /* This is a value that is used but not changed inside loop. */
  1218. regs_used++;
  1219. }
  1220. }
  1221. }
  1222. if (! flag_ira_loop_pressure)
  1223. new_regs[0] = regs_needed[0] = 0;
  1224. else
  1225. {
  1226. for (i = 0; (int) i < ira_pressure_classes_num; i++)
  1227. new_regs[ira_pressure_classes[i]] = 0;
  1228. }
  1229. while ((gain = best_gain_for_invariant (&inv, regs_needed,
  1230. new_regs, regs_used,
  1231. speed, call_p)) > 0)
  1232. {
  1233. set_move_mark (inv->invno, gain);
  1234. if (! flag_ira_loop_pressure)
  1235. new_regs[0] += regs_needed[0];
  1236. else
  1237. {
  1238. for (i = 0; (int) i < ira_pressure_classes_num; i++)
  1239. new_regs[ira_pressure_classes[i]]
  1240. += regs_needed[ira_pressure_classes[i]];
  1241. }
  1242. }
  1243. }
  1244. /* Replace the uses, reached by the definition of invariant INV, by REG.
  1245. IN_GROUP is nonzero if this is part of a group of changes that must be
  1246. performed as a group. In that case, the changes will be stored. The
  1247. function `apply_change_group' will validate and apply the changes. */
  1248. static int
  1249. replace_uses (struct invariant *inv, rtx reg, bool in_group)
  1250. {
  1251. /* Replace the uses we know to be dominated. It saves work for copy
  1252. propagation, and also it is necessary so that dependent invariants
  1253. are computed right. */
  1254. if (inv->def)
  1255. {
  1256. struct use *use;
  1257. for (use = inv->def->uses; use; use = use->next)
  1258. validate_change (use->insn, use->pos, reg, true);
  1259. /* If we aren't part of a larger group, apply the changes now. */
  1260. if (!in_group)
  1261. return apply_change_group ();
  1262. }
  1263. return 1;
  1264. }
  1265. /* Move invariant INVNO out of the LOOP. Returns true if this succeeds, false
  1266. otherwise. */
  1267. static bool
  1268. move_invariant_reg (struct loop *loop, unsigned invno)
  1269. {
  1270. struct invariant *inv = invariants[invno];
  1271. struct invariant *repr = invariants[inv->eqto];
  1272. unsigned i;
  1273. basic_block preheader = loop_preheader_edge (loop)->src;
  1274. rtx reg, set, dest, note;
  1275. bitmap_iterator bi;
  1276. int regno = -1;
  1277. if (inv->reg)
  1278. return true;
  1279. if (!repr->move)
  1280. return false;
  1281. /* If this is a representative of the class of equivalent invariants,
  1282. really move the invariant. Otherwise just replace its use with
  1283. the register used for the representative. */
  1284. if (inv == repr)
  1285. {
  1286. if (inv->depends_on)
  1287. {
  1288. EXECUTE_IF_SET_IN_BITMAP (inv->depends_on, 0, i, bi)
  1289. {
  1290. if (!move_invariant_reg (loop, i))
  1291. goto fail;
  1292. }
  1293. }
  1294. /* Move the set out of the loop. If the set is always executed (we could
  1295. omit this condition if we know that the register is unused outside of
  1296. the loop, but it does not seem worth finding out) and it has no uses
  1297. that would not be dominated by it, we may just move it (TODO).
  1298. Otherwise we need to create a temporary register. */
  1299. set = single_set (inv->insn);
  1300. reg = dest = SET_DEST (set);
  1301. if (GET_CODE (reg) == SUBREG)
  1302. reg = SUBREG_REG (reg);
  1303. if (REG_P (reg))
  1304. regno = REGNO (reg);
  1305. reg = gen_reg_rtx_and_attrs (dest);
  1306. /* Try replacing the destination by a new pseudoregister. */
  1307. validate_change (inv->insn, &SET_DEST (set), reg, true);
  1308. /* As well as all the dominated uses. */
  1309. replace_uses (inv, reg, true);
  1310. /* And validate all the changes. */
  1311. if (!apply_change_group ())
  1312. goto fail;
  1313. emit_insn_after (gen_move_insn (dest, reg), inv->insn);
  1314. reorder_insns (inv->insn, inv->insn, BB_END (preheader));
  1315. /* If there is a REG_EQUAL note on the insn we just moved, and the
  1316. insn is in a basic block that is not always executed or the note
  1317. contains something for which we don't know the invariant status,
  1318. the note may no longer be valid after we move the insn. Note that
  1319. uses in REG_EQUAL notes are taken into account in the computation
  1320. of invariants, so it is safe to retain the note even if it contains
  1321. register references for which we know the invariant status. */
  1322. if ((note = find_reg_note (inv->insn, REG_EQUAL, NULL_RTX))
  1323. && (!inv->always_executed
  1324. || !check_maybe_invariant (XEXP (note, 0))))
  1325. remove_note (inv->insn, note);
  1326. }
  1327. else
  1328. {
  1329. if (!move_invariant_reg (loop, repr->invno))
  1330. goto fail;
  1331. reg = repr->reg;
  1332. regno = repr->orig_regno;
  1333. if (!replace_uses (inv, reg, false))
  1334. goto fail;
  1335. set = single_set (inv->insn);
  1336. emit_insn_after (gen_move_insn (SET_DEST (set), reg), inv->insn);
  1337. delete_insn (inv->insn);
  1338. }
  1339. inv->reg = reg;
  1340. inv->orig_regno = regno;
  1341. return true;
  1342. fail:
  1343. /* If we failed, clear move flag, so that we do not try to move inv
  1344. again. */
  1345. if (dump_file)
  1346. fprintf (dump_file, "Failed to move invariant %d\n", invno);
  1347. inv->move = false;
  1348. inv->reg = NULL_RTX;
  1349. inv->orig_regno = -1;
  1350. return false;
  1351. }
  1352. /* Move selected invariant out of the LOOP. Newly created regs are marked
  1353. in TEMPORARY_REGS. */
  1354. static void
  1355. move_invariants (struct loop *loop)
  1356. {
  1357. struct invariant *inv;
  1358. unsigned i;
  1359. FOR_EACH_VEC_ELT (invariants, i, inv)
  1360. move_invariant_reg (loop, i);
  1361. if (flag_ira_loop_pressure && resize_reg_info ())
  1362. {
  1363. FOR_EACH_VEC_ELT (invariants, i, inv)
  1364. if (inv->reg != NULL_RTX)
  1365. {
  1366. if (inv->orig_regno >= 0)
  1367. setup_reg_classes (REGNO (inv->reg),
  1368. reg_preferred_class (inv->orig_regno),
  1369. reg_alternate_class (inv->orig_regno),
  1370. reg_allocno_class (inv->orig_regno));
  1371. else
  1372. setup_reg_classes (REGNO (inv->reg),
  1373. GENERAL_REGS, NO_REGS, GENERAL_REGS);
  1374. }
  1375. }
  1376. }
  1377. /* Initializes invariant motion data. */
  1378. static void
  1379. init_inv_motion_data (void)
  1380. {
  1381. actual_stamp = 1;
  1382. invariants.create (100);
  1383. }
  1384. /* Frees the data allocated by invariant motion. */
  1385. static void
  1386. free_inv_motion_data (void)
  1387. {
  1388. unsigned i;
  1389. struct def *def;
  1390. struct invariant *inv;
  1391. check_invariant_table_size ();
  1392. for (i = 0; i < DF_DEFS_TABLE_SIZE (); i++)
  1393. {
  1394. inv = invariant_table[i];
  1395. if (inv)
  1396. {
  1397. def = inv->def;
  1398. gcc_assert (def != NULL);
  1399. free_use_list (def->uses);
  1400. free (def);
  1401. invariant_table[i] = NULL;
  1402. }
  1403. }
  1404. FOR_EACH_VEC_ELT (invariants, i, inv)
  1405. {
  1406. BITMAP_FREE (inv->depends_on);
  1407. free (inv);
  1408. }
  1409. invariants.release ();
  1410. }
  1411. /* Move the invariants out of the LOOP. */
  1412. static void
  1413. move_single_loop_invariants (struct loop *loop)
  1414. {
  1415. init_inv_motion_data ();
  1416. find_invariants (loop);
  1417. find_invariants_to_move (optimize_loop_for_speed_p (loop),
  1418. LOOP_DATA (loop)->has_call);
  1419. move_invariants (loop);
  1420. free_inv_motion_data ();
  1421. }
  1422. /* Releases the auxiliary data for LOOP. */
  1423. static void
  1424. free_loop_data (struct loop *loop)
  1425. {
  1426. struct loop_data *data = LOOP_DATA (loop);
  1427. if (!data)
  1428. return;
  1429. bitmap_clear (&LOOP_DATA (loop)->regs_ref);
  1430. bitmap_clear (&LOOP_DATA (loop)->regs_live);
  1431. free (data);
  1432. loop->aux = NULL;
  1433. }
  1434. /* Registers currently living. */
  1435. static bitmap_head curr_regs_live;
  1436. /* Current reg pressure for each pressure class. */
  1437. static int curr_reg_pressure[N_REG_CLASSES];
  1438. /* Record all regs that are set in any one insn. Communication from
  1439. mark_reg_{store,clobber} and global_conflicts. Asm can refer to
  1440. all hard-registers. */
  1441. static rtx regs_set[(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS
  1442. ? FIRST_PSEUDO_REGISTER : MAX_RECOG_OPERANDS) * 2];
  1443. /* Number of regs stored in the previous array. */
  1444. static int n_regs_set;
  1445. /* Return pressure class and number of needed hard registers (through
  1446. *NREGS) of register REGNO. */
  1447. static enum reg_class
  1448. get_regno_pressure_class (int regno, int *nregs)
  1449. {
  1450. if (regno >= FIRST_PSEUDO_REGISTER)
  1451. {
  1452. enum reg_class pressure_class;
  1453. pressure_class = reg_allocno_class (regno);
  1454. pressure_class = ira_pressure_class_translate[pressure_class];
  1455. *nregs
  1456. = ira_reg_class_max_nregs[pressure_class][PSEUDO_REGNO_MODE (regno)];
  1457. return pressure_class;
  1458. }
  1459. else if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, regno)
  1460. && ! TEST_HARD_REG_BIT (eliminable_regset, regno))
  1461. {
  1462. *nregs = 1;
  1463. return ira_pressure_class_translate[REGNO_REG_CLASS (regno)];
  1464. }
  1465. else
  1466. {
  1467. *nregs = 0;
  1468. return NO_REGS;
  1469. }
  1470. }
  1471. /* Increase (if INCR_P) or decrease current register pressure for
  1472. register REGNO. */
  1473. static void
  1474. change_pressure (int regno, bool incr_p)
  1475. {
  1476. int nregs;
  1477. enum reg_class pressure_class;
  1478. pressure_class = get_regno_pressure_class (regno, &nregs);
  1479. if (! incr_p)
  1480. curr_reg_pressure[pressure_class] -= nregs;
  1481. else
  1482. {
  1483. curr_reg_pressure[pressure_class] += nregs;
  1484. if (LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
  1485. < curr_reg_pressure[pressure_class])
  1486. LOOP_DATA (curr_loop)->max_reg_pressure[pressure_class]
  1487. = curr_reg_pressure[pressure_class];
  1488. }
  1489. }
  1490. /* Mark REGNO birth. */
  1491. static void
  1492. mark_regno_live (int regno)
  1493. {
  1494. struct loop *loop;
  1495. for (loop = curr_loop;
  1496. loop != current_loops->tree_root;
  1497. loop = loop_outer (loop))
  1498. bitmap_set_bit (&LOOP_DATA (loop)->regs_live, regno);
  1499. if (!bitmap_set_bit (&curr_regs_live, regno))
  1500. return;
  1501. change_pressure (regno, true);
  1502. }
  1503. /* Mark REGNO death. */
  1504. static void
  1505. mark_regno_death (int regno)
  1506. {
  1507. if (! bitmap_clear_bit (&curr_regs_live, regno))
  1508. return;
  1509. change_pressure (regno, false);
  1510. }
  1511. /* Mark setting register REG. */
  1512. static void
  1513. mark_reg_store (rtx reg, const_rtx setter ATTRIBUTE_UNUSED,
  1514. void *data ATTRIBUTE_UNUSED)
  1515. {
  1516. int regno;
  1517. if (GET_CODE (reg) == SUBREG)
  1518. reg = SUBREG_REG (reg);
  1519. if (! REG_P (reg))
  1520. return;
  1521. regs_set[n_regs_set++] = reg;
  1522. regno = REGNO (reg);
  1523. if (regno >= FIRST_PSEUDO_REGISTER)
  1524. mark_regno_live (regno);
  1525. else
  1526. {
  1527. int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
  1528. while (regno < last)
  1529. {
  1530. mark_regno_live (regno);
  1531. regno++;
  1532. }
  1533. }
  1534. }
  1535. /* Mark clobbering register REG. */
  1536. static void
  1537. mark_reg_clobber (rtx reg, const_rtx setter, void *data)
  1538. {
  1539. if (GET_CODE (setter) == CLOBBER)
  1540. mark_reg_store (reg, setter, data);
  1541. }
  1542. /* Mark register REG death. */
  1543. static void
  1544. mark_reg_death (rtx reg)
  1545. {
  1546. int regno = REGNO (reg);
  1547. if (regno >= FIRST_PSEUDO_REGISTER)
  1548. mark_regno_death (regno);
  1549. else
  1550. {
  1551. int last = regno + hard_regno_nregs[regno][GET_MODE (reg)];
  1552. while (regno < last)
  1553. {
  1554. mark_regno_death (regno);
  1555. regno++;
  1556. }
  1557. }
  1558. }
  1559. /* Mark occurrence of registers in X for the current loop. */
  1560. static void
  1561. mark_ref_regs (rtx x)
  1562. {
  1563. RTX_CODE code;
  1564. int i;
  1565. const char *fmt;
  1566. if (!x)
  1567. return;
  1568. code = GET_CODE (x);
  1569. if (code == REG)
  1570. {
  1571. struct loop *loop;
  1572. for (loop = curr_loop;
  1573. loop != current_loops->tree_root;
  1574. loop = loop_outer (loop))
  1575. bitmap_set_bit (&LOOP_DATA (loop)->regs_ref, REGNO (x));
  1576. return;
  1577. }
  1578. fmt = GET_RTX_FORMAT (code);
  1579. for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  1580. if (fmt[i] == 'e')
  1581. mark_ref_regs (XEXP (x, i));
  1582. else if (fmt[i] == 'E')
  1583. {
  1584. int j;
  1585. for (j = 0; j < XVECLEN (x, i); j++)
  1586. mark_ref_regs (XVECEXP (x, i, j));
  1587. }
  1588. }
  1589. /* Calculate register pressure in the loops. */
  1590. static void
  1591. calculate_loop_reg_pressure (void)
  1592. {
  1593. int i;
  1594. unsigned int j;
  1595. bitmap_iterator bi;
  1596. basic_block bb;
  1597. rtx_insn *insn;
  1598. rtx link;
  1599. struct loop *loop, *parent;
  1600. FOR_EACH_LOOP (loop, 0)
  1601. if (loop->aux == NULL)
  1602. {
  1603. loop->aux = xcalloc (1, sizeof (struct loop_data));
  1604. bitmap_initialize (&LOOP_DATA (loop)->regs_ref, &reg_obstack);
  1605. bitmap_initialize (&LOOP_DATA (loop)->regs_live, &reg_obstack);
  1606. }
  1607. ira_setup_eliminable_regset ();
  1608. bitmap_initialize (&curr_regs_live, &reg_obstack);
  1609. FOR_EACH_BB_FN (bb, cfun)
  1610. {
  1611. curr_loop = bb->loop_father;
  1612. if (curr_loop == current_loops->tree_root)
  1613. continue;
  1614. for (loop = curr_loop;
  1615. loop != current_loops->tree_root;
  1616. loop = loop_outer (loop))
  1617. bitmap_ior_into (&LOOP_DATA (loop)->regs_live, DF_LR_IN (bb));
  1618. bitmap_copy (&curr_regs_live, DF_LR_IN (bb));
  1619. for (i = 0; i < ira_pressure_classes_num; i++)
  1620. curr_reg_pressure[ira_pressure_classes[i]] = 0;
  1621. EXECUTE_IF_SET_IN_BITMAP (&curr_regs_live, 0, j, bi)
  1622. change_pressure (j, true);
  1623. FOR_BB_INSNS (bb, insn)
  1624. {
  1625. if (! NONDEBUG_INSN_P (insn))
  1626. continue;
  1627. mark_ref_regs (PATTERN (insn));
  1628. n_regs_set = 0;
  1629. note_stores (PATTERN (insn), mark_reg_clobber, NULL);
  1630. /* Mark any registers dead after INSN as dead now. */
  1631. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1632. if (REG_NOTE_KIND (link) == REG_DEAD)
  1633. mark_reg_death (XEXP (link, 0));
  1634. /* Mark any registers set in INSN as live,
  1635. and mark them as conflicting with all other live regs.
  1636. Clobbers are processed again, so they conflict with
  1637. the registers that are set. */
  1638. note_stores (PATTERN (insn), mark_reg_store, NULL);
  1639. #ifdef AUTO_INC_DEC
  1640. for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  1641. if (REG_NOTE_KIND (link) == REG_INC)
  1642. mark_reg_store (XEXP (link, 0), NULL_RTX, NULL);
  1643. #endif
  1644. while (n_regs_set-- > 0)
  1645. {
  1646. rtx note = find_regno_note (insn, REG_UNUSED,
  1647. REGNO (regs_set[n_regs_set]));
  1648. if (! note)
  1649. continue;
  1650. mark_reg_death (XEXP (note, 0));
  1651. }
  1652. }
  1653. }
  1654. bitmap_clear (&curr_regs_live);
  1655. if (flag_ira_region == IRA_REGION_MIXED
  1656. || flag_ira_region == IRA_REGION_ALL)
  1657. FOR_EACH_LOOP (loop, 0)
  1658. {
  1659. EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
  1660. if (! bitmap_bit_p (&LOOP_DATA (loop)->regs_ref, j))
  1661. {
  1662. enum reg_class pressure_class;
  1663. int nregs;
  1664. pressure_class = get_regno_pressure_class (j, &nregs);
  1665. LOOP_DATA (loop)->max_reg_pressure[pressure_class] -= nregs;
  1666. }
  1667. }
  1668. if (dump_file == NULL)
  1669. return;
  1670. FOR_EACH_LOOP (loop, 0)
  1671. {
  1672. parent = loop_outer (loop);
  1673. fprintf (dump_file, "\n Loop %d (parent %d, header bb%d, depth %d)\n",
  1674. loop->num, (parent == NULL ? -1 : parent->num),
  1675. loop->header->index, loop_depth (loop));
  1676. fprintf (dump_file, "\n ref. regnos:");
  1677. EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_ref, 0, j, bi)
  1678. fprintf (dump_file, " %d", j);
  1679. fprintf (dump_file, "\n live regnos:");
  1680. EXECUTE_IF_SET_IN_BITMAP (&LOOP_DATA (loop)->regs_live, 0, j, bi)
  1681. fprintf (dump_file, " %d", j);
  1682. fprintf (dump_file, "\n Pressure:");
  1683. for (i = 0; (int) i < ira_pressure_classes_num; i++)
  1684. {
  1685. enum reg_class pressure_class;
  1686. pressure_class = ira_pressure_classes[i];
  1687. if (LOOP_DATA (loop)->max_reg_pressure[pressure_class] == 0)
  1688. continue;
  1689. fprintf (dump_file, " %s=%d", reg_class_names[pressure_class],
  1690. LOOP_DATA (loop)->max_reg_pressure[pressure_class]);
  1691. }
  1692. fprintf (dump_file, "\n");
  1693. }
  1694. }
  1695. /* Move the invariants out of the loops. */
  1696. void
  1697. move_loop_invariants (void)
  1698. {
  1699. struct loop *loop;
  1700. if (flag_ira_loop_pressure)
  1701. {
  1702. df_analyze ();
  1703. regstat_init_n_sets_and_refs ();
  1704. ira_set_pseudo_classes (true, dump_file);
  1705. calculate_loop_reg_pressure ();
  1706. regstat_free_n_sets_and_refs ();
  1707. }
  1708. df_set_flags (DF_EQ_NOTES + DF_DEFER_INSN_RESCAN);
  1709. /* Process the loops, innermost first. */
  1710. FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
  1711. {
  1712. curr_loop = loop;
  1713. /* move_single_loop_invariants for very large loops
  1714. is time consuming and might need a lot of memory. */
  1715. if (loop->num_nodes <= (unsigned) LOOP_INVARIANT_MAX_BBS_IN_LOOP)
  1716. move_single_loop_invariants (loop);
  1717. }
  1718. FOR_EACH_LOOP (loop, 0)
  1719. {
  1720. free_loop_data (loop);
  1721. }
  1722. if (flag_ira_loop_pressure)
  1723. /* There is no sense to keep this info because it was most
  1724. probably outdated by subsequent passes. */
  1725. free_reg_info ();
  1726. free (invariant_table);
  1727. invariant_table = NULL;
  1728. invariant_table_size = 0;
  1729. #ifdef ENABLE_CHECKING
  1730. verify_flow_info ();
  1731. #endif
  1732. }