tree-ssa-loop-manip.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /* High-level loop manipulation functions.
  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. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "tree.h"
  29. #include "fold-const.h"
  30. #include "tm_p.h"
  31. #include "predict.h"
  32. #include "hard-reg-set.h"
  33. #include "input.h"
  34. #include "function.h"
  35. #include "dominance.h"
  36. #include "cfg.h"
  37. #include "cfganal.h"
  38. #include "basic-block.h"
  39. #include "tree-ssa-alias.h"
  40. #include "internal-fn.h"
  41. #include "gimple-expr.h"
  42. #include "is-a.h"
  43. #include "gimple.h"
  44. #include "gimplify.h"
  45. #include "gimple-iterator.h"
  46. #include "gimplify-me.h"
  47. #include "gimple-ssa.h"
  48. #include "tree-cfg.h"
  49. #include "tree-phinodes.h"
  50. #include "ssa-iterators.h"
  51. #include "stringpool.h"
  52. #include "tree-ssanames.h"
  53. #include "tree-ssa-loop-ivopts.h"
  54. #include "tree-ssa-loop-manip.h"
  55. #include "tree-ssa-loop-niter.h"
  56. #include "tree-ssa-loop.h"
  57. #include "tree-into-ssa.h"
  58. #include "tree-ssa.h"
  59. #include "dumpfile.h"
  60. #include "gimple-pretty-print.h"
  61. #include "cfgloop.h"
  62. #include "tree-pass.h" /* ??? for TODO_update_ssa but this isn't a pass. */
  63. #include "tree-scalar-evolution.h"
  64. #include "params.h"
  65. #include "tree-inline.h"
  66. #include "langhooks.h"
  67. /* All bitmaps for rewriting into loop-closed SSA go on this obstack,
  68. so that we can free them all at once. */
  69. static bitmap_obstack loop_renamer_obstack;
  70. /* Creates an induction variable with value BASE + STEP * iteration in LOOP.
  71. It is expected that neither BASE nor STEP are shared with other expressions
  72. (unless the sharing rules allow this). Use VAR as a base var_decl for it
  73. (if NULL, a new temporary will be created). The increment will occur at
  74. INCR_POS (after it if AFTER is true, before it otherwise). INCR_POS and
  75. AFTER can be computed using standard_iv_increment_position. The ssa versions
  76. of the variable before and after increment will be stored in VAR_BEFORE and
  77. VAR_AFTER (unless they are NULL). */
  78. void
  79. create_iv (tree base, tree step, tree var, struct loop *loop,
  80. gimple_stmt_iterator *incr_pos, bool after,
  81. tree *var_before, tree *var_after)
  82. {
  83. gassign *stmt;
  84. gphi *phi;
  85. tree initial, step1;
  86. gimple_seq stmts;
  87. tree vb, va;
  88. enum tree_code incr_op = PLUS_EXPR;
  89. edge pe = loop_preheader_edge (loop);
  90. if (var != NULL_TREE)
  91. {
  92. vb = make_ssa_name (var);
  93. va = make_ssa_name (var);
  94. }
  95. else
  96. {
  97. vb = make_temp_ssa_name (TREE_TYPE (base), NULL, "ivtmp");
  98. va = make_temp_ssa_name (TREE_TYPE (base), NULL, "ivtmp");
  99. }
  100. if (var_before)
  101. *var_before = vb;
  102. if (var_after)
  103. *var_after = va;
  104. /* For easier readability of the created code, produce MINUS_EXPRs
  105. when suitable. */
  106. if (TREE_CODE (step) == INTEGER_CST)
  107. {
  108. if (TYPE_UNSIGNED (TREE_TYPE (step)))
  109. {
  110. step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
  111. if (tree_int_cst_lt (step1, step))
  112. {
  113. incr_op = MINUS_EXPR;
  114. step = step1;
  115. }
  116. }
  117. else
  118. {
  119. bool ovf;
  120. if (!tree_expr_nonnegative_warnv_p (step, &ovf)
  121. && may_negate_without_overflow_p (step))
  122. {
  123. incr_op = MINUS_EXPR;
  124. step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
  125. }
  126. }
  127. }
  128. if (POINTER_TYPE_P (TREE_TYPE (base)))
  129. {
  130. if (TREE_CODE (base) == ADDR_EXPR)
  131. mark_addressable (TREE_OPERAND (base, 0));
  132. step = convert_to_ptrofftype (step);
  133. if (incr_op == MINUS_EXPR)
  134. step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
  135. incr_op = POINTER_PLUS_EXPR;
  136. }
  137. /* Gimplify the step if necessary. We put the computations in front of the
  138. loop (i.e. the step should be loop invariant). */
  139. step = force_gimple_operand (step, &stmts, true, NULL_TREE);
  140. if (stmts)
  141. gsi_insert_seq_on_edge_immediate (pe, stmts);
  142. stmt = gimple_build_assign (va, incr_op, vb, step);
  143. if (after)
  144. gsi_insert_after (incr_pos, stmt, GSI_NEW_STMT);
  145. else
  146. gsi_insert_before (incr_pos, stmt, GSI_NEW_STMT);
  147. initial = force_gimple_operand (base, &stmts, true, var);
  148. if (stmts)
  149. gsi_insert_seq_on_edge_immediate (pe, stmts);
  150. phi = create_phi_node (vb, loop->header);
  151. add_phi_arg (phi, initial, loop_preheader_edge (loop), UNKNOWN_LOCATION);
  152. add_phi_arg (phi, va, loop_latch_edge (loop), UNKNOWN_LOCATION);
  153. }
  154. /* Return the innermost superloop LOOP of USE_LOOP that is a superloop of
  155. both DEF_LOOP and USE_LOOP. */
  156. static inline struct loop *
  157. find_sibling_superloop (struct loop *use_loop, struct loop *def_loop)
  158. {
  159. unsigned ud = loop_depth (use_loop);
  160. unsigned dd = loop_depth (def_loop);
  161. gcc_assert (ud > 0 && dd > 0);
  162. if (ud > dd)
  163. use_loop = superloop_at_depth (use_loop, dd);
  164. if (ud < dd)
  165. def_loop = superloop_at_depth (def_loop, ud);
  166. while (loop_outer (use_loop) != loop_outer (def_loop))
  167. {
  168. use_loop = loop_outer (use_loop);
  169. def_loop = loop_outer (def_loop);
  170. gcc_assert (use_loop && def_loop);
  171. }
  172. return use_loop;
  173. }
  174. /* DEF_BB is a basic block containing a DEF that needs rewriting into
  175. loop-closed SSA form. USE_BLOCKS is the set of basic blocks containing
  176. uses of DEF that "escape" from the loop containing DEF_BB (i.e. blocks in
  177. USE_BLOCKS are dominated by DEF_BB but not in the loop father of DEF_B).
  178. ALL_EXITS[I] is the set of all basic blocks that exit loop I.
  179. Compute the subset of LOOP_EXITS that exit the loop containing DEF_BB
  180. or one of its loop fathers, in which DEF is live. This set is returned
  181. in the bitmap LIVE_EXITS.
  182. Instead of computing the complete livein set of the def, we use the loop
  183. nesting tree as a form of poor man's structure analysis. This greatly
  184. speeds up the analysis, which is important because this function may be
  185. called on all SSA names that need rewriting, one at a time. */
  186. static void
  187. compute_live_loop_exits (bitmap live_exits, bitmap use_blocks,
  188. bitmap *loop_exits, basic_block def_bb)
  189. {
  190. unsigned i;
  191. bitmap_iterator bi;
  192. struct loop *def_loop = def_bb->loop_father;
  193. unsigned def_loop_depth = loop_depth (def_loop);
  194. bitmap def_loop_exits;
  195. /* Normally the work list size is bounded by the number of basic
  196. blocks in the largest loop. We don't know this number, but we
  197. can be fairly sure that it will be relatively small. */
  198. auto_vec<basic_block> worklist (MAX (8, n_basic_blocks_for_fn (cfun) / 128));
  199. EXECUTE_IF_SET_IN_BITMAP (use_blocks, 0, i, bi)
  200. {
  201. basic_block use_bb = BASIC_BLOCK_FOR_FN (cfun, i);
  202. struct loop *use_loop = use_bb->loop_father;
  203. gcc_checking_assert (def_loop != use_loop
  204. && ! flow_loop_nested_p (def_loop, use_loop));
  205. if (! flow_loop_nested_p (use_loop, def_loop))
  206. use_bb = find_sibling_superloop (use_loop, def_loop)->header;
  207. if (bitmap_set_bit (live_exits, use_bb->index))
  208. worklist.safe_push (use_bb);
  209. }
  210. /* Iterate until the worklist is empty. */
  211. while (! worklist.is_empty ())
  212. {
  213. edge e;
  214. edge_iterator ei;
  215. /* Pull a block off the worklist. */
  216. basic_block bb = worklist.pop ();
  217. /* Make sure we have at least enough room in the work list
  218. for all predecessors of this block. */
  219. worklist.reserve (EDGE_COUNT (bb->preds));
  220. /* For each predecessor block. */
  221. FOR_EACH_EDGE (e, ei, bb->preds)
  222. {
  223. basic_block pred = e->src;
  224. struct loop *pred_loop = pred->loop_father;
  225. unsigned pred_loop_depth = loop_depth (pred_loop);
  226. bool pred_visited;
  227. /* We should have met DEF_BB along the way. */
  228. gcc_assert (pred != ENTRY_BLOCK_PTR_FOR_FN (cfun));
  229. if (pred_loop_depth >= def_loop_depth)
  230. {
  231. if (pred_loop_depth > def_loop_depth)
  232. pred_loop = superloop_at_depth (pred_loop, def_loop_depth);
  233. /* If we've reached DEF_LOOP, our train ends here. */
  234. if (pred_loop == def_loop)
  235. continue;
  236. }
  237. else if (! flow_loop_nested_p (pred_loop, def_loop))
  238. pred = find_sibling_superloop (pred_loop, def_loop)->header;
  239. /* Add PRED to the LIVEIN set. PRED_VISITED is true if
  240. we had already added PRED to LIVEIN before. */
  241. pred_visited = !bitmap_set_bit (live_exits, pred->index);
  242. /* If we have visited PRED before, don't add it to the worklist.
  243. If BB dominates PRED, then we're probably looking at a loop.
  244. We're only interested in looking up in the dominance tree
  245. because DEF_BB dominates all the uses. */
  246. if (pred_visited || dominated_by_p (CDI_DOMINATORS, pred, bb))
  247. continue;
  248. worklist.quick_push (pred);
  249. }
  250. }
  251. def_loop_exits = BITMAP_ALLOC (&loop_renamer_obstack);
  252. for (struct loop *loop = def_loop;
  253. loop != current_loops->tree_root;
  254. loop = loop_outer (loop))
  255. bitmap_ior_into (def_loop_exits, loop_exits[loop->num]);
  256. bitmap_and_into (live_exits, def_loop_exits);
  257. BITMAP_FREE (def_loop_exits);
  258. }
  259. /* Add a loop-closing PHI for VAR in basic block EXIT. */
  260. static void
  261. add_exit_phi (basic_block exit, tree var)
  262. {
  263. gphi *phi;
  264. edge e;
  265. edge_iterator ei;
  266. #ifdef ENABLE_CHECKING
  267. /* Check that at least one of the edges entering the EXIT block exits
  268. the loop, or a superloop of that loop, that VAR is defined in. */
  269. gimple def_stmt = SSA_NAME_DEF_STMT (var);
  270. basic_block def_bb = gimple_bb (def_stmt);
  271. FOR_EACH_EDGE (e, ei, exit->preds)
  272. {
  273. struct loop *aloop = find_common_loop (def_bb->loop_father,
  274. e->src->loop_father);
  275. if (!flow_bb_inside_loop_p (aloop, e->dest))
  276. break;
  277. }
  278. gcc_checking_assert (e);
  279. #endif
  280. phi = create_phi_node (NULL_TREE, exit);
  281. create_new_def_for (var, phi, gimple_phi_result_ptr (phi));
  282. FOR_EACH_EDGE (e, ei, exit->preds)
  283. add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
  284. if (dump_file && (dump_flags & TDF_DETAILS))
  285. {
  286. fprintf (dump_file, ";; Created LCSSA PHI: ");
  287. print_gimple_stmt (dump_file, phi, 0, dump_flags);
  288. }
  289. }
  290. /* Add exit phis for VAR that is used in LIVEIN.
  291. Exits of the loops are stored in LOOP_EXITS. */
  292. static void
  293. add_exit_phis_var (tree var, bitmap use_blocks, bitmap *loop_exits)
  294. {
  295. unsigned index;
  296. bitmap_iterator bi;
  297. basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
  298. bitmap live_exits = BITMAP_ALLOC (&loop_renamer_obstack);
  299. gcc_checking_assert (! bitmap_bit_p (use_blocks, def_bb->index));
  300. compute_live_loop_exits (live_exits, use_blocks, loop_exits, def_bb);
  301. EXECUTE_IF_SET_IN_BITMAP (live_exits, 0, index, bi)
  302. {
  303. add_exit_phi (BASIC_BLOCK_FOR_FN (cfun, index), var);
  304. }
  305. BITMAP_FREE (live_exits);
  306. }
  307. /* Add exit phis for the names marked in NAMES_TO_RENAME.
  308. Exits of the loops are stored in EXITS. Sets of blocks where the ssa
  309. names are used are stored in USE_BLOCKS. */
  310. static void
  311. add_exit_phis (bitmap names_to_rename, bitmap *use_blocks, bitmap *loop_exits)
  312. {
  313. unsigned i;
  314. bitmap_iterator bi;
  315. EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
  316. {
  317. add_exit_phis_var (ssa_name (i), use_blocks[i], loop_exits);
  318. }
  319. }
  320. /* Fill the array of bitmaps LOOP_EXITS with all loop exit edge targets. */
  321. static void
  322. get_loops_exits (bitmap *loop_exits)
  323. {
  324. struct loop *loop;
  325. unsigned j;
  326. edge e;
  327. FOR_EACH_LOOP (loop, 0)
  328. {
  329. vec<edge> exit_edges = get_loop_exit_edges (loop);
  330. loop_exits[loop->num] = BITMAP_ALLOC (&loop_renamer_obstack);
  331. FOR_EACH_VEC_ELT (exit_edges, j, e)
  332. bitmap_set_bit (loop_exits[loop->num], e->dest->index);
  333. exit_edges.release ();
  334. }
  335. }
  336. /* For USE in BB, if it is used outside of the loop it is defined in,
  337. mark it for rewrite. Record basic block BB where it is used
  338. to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap. */
  339. static void
  340. find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks,
  341. bitmap need_phis)
  342. {
  343. unsigned ver;
  344. basic_block def_bb;
  345. struct loop *def_loop;
  346. if (TREE_CODE (use) != SSA_NAME)
  347. return;
  348. ver = SSA_NAME_VERSION (use);
  349. def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
  350. if (!def_bb)
  351. return;
  352. def_loop = def_bb->loop_father;
  353. /* If the definition is not inside a loop, it is not interesting. */
  354. if (!loop_outer (def_loop))
  355. return;
  356. /* If the use is not outside of the loop it is defined in, it is not
  357. interesting. */
  358. if (flow_bb_inside_loop_p (def_loop, bb))
  359. return;
  360. /* If we're seeing VER for the first time, we still have to allocate
  361. a bitmap for its uses. */
  362. if (bitmap_set_bit (need_phis, ver))
  363. use_blocks[ver] = BITMAP_ALLOC (&loop_renamer_obstack);
  364. bitmap_set_bit (use_blocks[ver], bb->index);
  365. }
  366. /* For uses in STMT, mark names that are used outside of the loop they are
  367. defined to rewrite. Record the set of blocks in that the ssa
  368. names are defined to USE_BLOCKS and the ssa names themselves to
  369. NEED_PHIS. */
  370. static void
  371. find_uses_to_rename_stmt (gimple stmt, bitmap *use_blocks, bitmap need_phis)
  372. {
  373. ssa_op_iter iter;
  374. tree var;
  375. basic_block bb = gimple_bb (stmt);
  376. if (is_gimple_debug (stmt))
  377. return;
  378. FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
  379. find_uses_to_rename_use (bb, var, use_blocks, need_phis);
  380. }
  381. /* Marks names that are used in BB and outside of the loop they are
  382. defined in for rewrite. Records the set of blocks in that the ssa
  383. names are defined to USE_BLOCKS. Record the SSA names that will
  384. need exit PHIs in NEED_PHIS. */
  385. static void
  386. find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks, bitmap need_phis)
  387. {
  388. edge e;
  389. edge_iterator ei;
  390. FOR_EACH_EDGE (e, ei, bb->succs)
  391. for (gphi_iterator bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi);
  392. gsi_next (&bsi))
  393. {
  394. gphi *phi = bsi.phi ();
  395. if (! virtual_operand_p (gimple_phi_result (phi)))
  396. find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (phi, e),
  397. use_blocks, need_phis);
  398. }
  399. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
  400. gsi_next (&bsi))
  401. find_uses_to_rename_stmt (gsi_stmt (bsi), use_blocks, need_phis);
  402. }
  403. /* Marks names that are used outside of the loop they are defined in
  404. for rewrite. Records the set of blocks in that the ssa
  405. names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
  406. scan only blocks in this set. */
  407. static void
  408. find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks, bitmap need_phis)
  409. {
  410. basic_block bb;
  411. unsigned index;
  412. bitmap_iterator bi;
  413. if (changed_bbs)
  414. EXECUTE_IF_SET_IN_BITMAP (changed_bbs, 0, index, bi)
  415. find_uses_to_rename_bb (BASIC_BLOCK_FOR_FN (cfun, index), use_blocks, need_phis);
  416. else
  417. FOR_EACH_BB_FN (bb, cfun)
  418. find_uses_to_rename_bb (bb, use_blocks, need_phis);
  419. }
  420. /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
  421. phi nodes to ensure that no variable is used outside the loop it is
  422. defined in.
  423. This strengthening of the basic ssa form has several advantages:
  424. 1) Updating it during unrolling/peeling/versioning is trivial, since
  425. we do not need to care about the uses outside of the loop.
  426. The same applies to virtual operands which are also rewritten into
  427. loop closed SSA form. Note that virtual operands are always live
  428. until function exit.
  429. 2) The behavior of all uses of an induction variable is the same.
  430. Without this, you need to distinguish the case when the variable
  431. is used outside of the loop it is defined in, for example
  432. for (i = 0; i < 100; i++)
  433. {
  434. for (j = 0; j < 100; j++)
  435. {
  436. k = i + j;
  437. use1 (k);
  438. }
  439. use2 (k);
  440. }
  441. Looking from the outer loop with the normal SSA form, the first use of k
  442. is not well-behaved, while the second one is an induction variable with
  443. base 99 and step 1.
  444. If CHANGED_BBS is not NULL, we look for uses outside loops only in
  445. the basic blocks in this set.
  446. UPDATE_FLAG is used in the call to update_ssa. See
  447. TODO_update_ssa* for documentation. */
  448. void
  449. rewrite_into_loop_closed_ssa (bitmap changed_bbs, unsigned update_flag)
  450. {
  451. bitmap *use_blocks;
  452. bitmap names_to_rename;
  453. loops_state_set (LOOP_CLOSED_SSA);
  454. if (number_of_loops (cfun) <= 1)
  455. return;
  456. /* If the pass has caused the SSA form to be out-of-date, update it
  457. now. */
  458. update_ssa (update_flag);
  459. bitmap_obstack_initialize (&loop_renamer_obstack);
  460. names_to_rename = BITMAP_ALLOC (&loop_renamer_obstack);
  461. /* Uses of names to rename. We don't have to initialize this array,
  462. because we know that we will only have entries for the SSA names
  463. in NAMES_TO_RENAME. */
  464. use_blocks = XNEWVEC (bitmap, num_ssa_names);
  465. /* Find the uses outside loops. */
  466. find_uses_to_rename (changed_bbs, use_blocks, names_to_rename);
  467. if (!bitmap_empty_p (names_to_rename))
  468. {
  469. /* An array of bitmaps where LOOP_EXITS[I] is the set of basic blocks
  470. that are the destination of an edge exiting loop number I. */
  471. bitmap *loop_exits = XNEWVEC (bitmap, number_of_loops (cfun));
  472. get_loops_exits (loop_exits);
  473. /* Add the PHI nodes on exits of the loops for the names we need to
  474. rewrite. */
  475. add_exit_phis (names_to_rename, use_blocks, loop_exits);
  476. free (loop_exits);
  477. /* Fix up all the names found to be used outside their original
  478. loops. */
  479. update_ssa (TODO_update_ssa);
  480. }
  481. bitmap_obstack_release (&loop_renamer_obstack);
  482. free (use_blocks);
  483. }
  484. /* Check invariants of the loop closed ssa form for the USE in BB. */
  485. static void
  486. check_loop_closed_ssa_use (basic_block bb, tree use)
  487. {
  488. gimple def;
  489. basic_block def_bb;
  490. if (TREE_CODE (use) != SSA_NAME || virtual_operand_p (use))
  491. return;
  492. def = SSA_NAME_DEF_STMT (use);
  493. def_bb = gimple_bb (def);
  494. gcc_assert (!def_bb
  495. || flow_bb_inside_loop_p (def_bb->loop_father, bb));
  496. }
  497. /* Checks invariants of loop closed ssa form in statement STMT in BB. */
  498. static void
  499. check_loop_closed_ssa_stmt (basic_block bb, gimple stmt)
  500. {
  501. ssa_op_iter iter;
  502. tree var;
  503. if (is_gimple_debug (stmt))
  504. return;
  505. FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
  506. check_loop_closed_ssa_use (bb, var);
  507. }
  508. /* Checks that invariants of the loop closed ssa form are preserved.
  509. Call verify_ssa when VERIFY_SSA_P is true. */
  510. DEBUG_FUNCTION void
  511. verify_loop_closed_ssa (bool verify_ssa_p)
  512. {
  513. basic_block bb;
  514. edge e;
  515. edge_iterator ei;
  516. if (number_of_loops (cfun) <= 1)
  517. return;
  518. if (verify_ssa_p)
  519. verify_ssa (false, true);
  520. timevar_push (TV_VERIFY_LOOP_CLOSED);
  521. FOR_EACH_BB_FN (bb, cfun)
  522. {
  523. for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
  524. gsi_next (&bsi))
  525. {
  526. gphi *phi = bsi.phi ();
  527. FOR_EACH_EDGE (e, ei, bb->preds)
  528. check_loop_closed_ssa_use (e->src,
  529. PHI_ARG_DEF_FROM_EDGE (phi, e));
  530. }
  531. for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
  532. gsi_next (&bsi))
  533. check_loop_closed_ssa_stmt (bb, gsi_stmt (bsi));
  534. }
  535. timevar_pop (TV_VERIFY_LOOP_CLOSED);
  536. }
  537. /* Split loop exit edge EXIT. The things are a bit complicated by a need to
  538. preserve the loop closed ssa form. The newly created block is returned. */
  539. basic_block
  540. split_loop_exit_edge (edge exit)
  541. {
  542. basic_block dest = exit->dest;
  543. basic_block bb = split_edge (exit);
  544. gphi *phi, *new_phi;
  545. tree new_name, name;
  546. use_operand_p op_p;
  547. gphi_iterator psi;
  548. source_location locus;
  549. for (psi = gsi_start_phis (dest); !gsi_end_p (psi); gsi_next (&psi))
  550. {
  551. phi = psi.phi ();
  552. op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb));
  553. locus = gimple_phi_arg_location_from_edge (phi, single_succ_edge (bb));
  554. name = USE_FROM_PTR (op_p);
  555. /* If the argument of the PHI node is a constant, we do not need
  556. to keep it inside loop. */
  557. if (TREE_CODE (name) != SSA_NAME)
  558. continue;
  559. /* Otherwise create an auxiliary phi node that will copy the value
  560. of the SSA name out of the loop. */
  561. new_name = duplicate_ssa_name (name, NULL);
  562. new_phi = create_phi_node (new_name, bb);
  563. add_phi_arg (new_phi, name, exit, locus);
  564. SET_USE (op_p, new_name);
  565. }
  566. return bb;
  567. }
  568. /* Returns the basic block in that statements should be emitted for induction
  569. variables incremented at the end of the LOOP. */
  570. basic_block
  571. ip_end_pos (struct loop *loop)
  572. {
  573. return loop->latch;
  574. }
  575. /* Returns the basic block in that statements should be emitted for induction
  576. variables incremented just before exit condition of a LOOP. */
  577. basic_block
  578. ip_normal_pos (struct loop *loop)
  579. {
  580. gimple last;
  581. basic_block bb;
  582. edge exit;
  583. if (!single_pred_p (loop->latch))
  584. return NULL;
  585. bb = single_pred (loop->latch);
  586. last = last_stmt (bb);
  587. if (!last
  588. || gimple_code (last) != GIMPLE_COND)
  589. return NULL;
  590. exit = EDGE_SUCC (bb, 0);
  591. if (exit->dest == loop->latch)
  592. exit = EDGE_SUCC (bb, 1);
  593. if (flow_bb_inside_loop_p (loop, exit->dest))
  594. return NULL;
  595. return bb;
  596. }
  597. /* Stores the standard position for induction variable increment in LOOP
  598. (just before the exit condition if it is available and latch block is empty,
  599. end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
  600. the increment should be inserted after *BSI. */
  601. void
  602. standard_iv_increment_position (struct loop *loop, gimple_stmt_iterator *bsi,
  603. bool *insert_after)
  604. {
  605. basic_block bb = ip_normal_pos (loop), latch = ip_end_pos (loop);
  606. gimple last = last_stmt (latch);
  607. if (!bb
  608. || (last && gimple_code (last) != GIMPLE_LABEL))
  609. {
  610. *bsi = gsi_last_bb (latch);
  611. *insert_after = true;
  612. }
  613. else
  614. {
  615. *bsi = gsi_last_bb (bb);
  616. *insert_after = false;
  617. }
  618. }
  619. /* Copies phi node arguments for duplicated blocks. The index of the first
  620. duplicated block is FIRST_NEW_BLOCK. */
  621. static void
  622. copy_phi_node_args (unsigned first_new_block)
  623. {
  624. unsigned i;
  625. for (i = first_new_block; i < (unsigned) last_basic_block_for_fn (cfun); i++)
  626. BASIC_BLOCK_FOR_FN (cfun, i)->flags |= BB_DUPLICATED;
  627. for (i = first_new_block; i < (unsigned) last_basic_block_for_fn (cfun); i++)
  628. add_phi_args_after_copy_bb (BASIC_BLOCK_FOR_FN (cfun, i));
  629. for (i = first_new_block; i < (unsigned) last_basic_block_for_fn (cfun); i++)
  630. BASIC_BLOCK_FOR_FN (cfun, i)->flags &= ~BB_DUPLICATED;
  631. }
  632. /* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also
  633. updates the PHI nodes at start of the copied region. In order to
  634. achieve this, only loops whose exits all lead to the same location
  635. are handled.
  636. Notice that we do not completely update the SSA web after
  637. duplication. The caller is responsible for calling update_ssa
  638. after the loop has been duplicated. */
  639. bool
  640. gimple_duplicate_loop_to_header_edge (struct loop *loop, edge e,
  641. unsigned int ndupl, sbitmap wont_exit,
  642. edge orig, vec<edge> *to_remove,
  643. int flags)
  644. {
  645. unsigned first_new_block;
  646. if (!loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
  647. return false;
  648. if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
  649. return false;
  650. first_new_block = last_basic_block_for_fn (cfun);
  651. if (!duplicate_loop_to_header_edge (loop, e, ndupl, wont_exit,
  652. orig, to_remove, flags))
  653. return false;
  654. /* Readd the removed phi args for e. */
  655. flush_pending_stmts (e);
  656. /* Copy the phi node arguments. */
  657. copy_phi_node_args (first_new_block);
  658. scev_reset ();
  659. return true;
  660. }
  661. /* Returns true if we can unroll LOOP FACTOR times. Number
  662. of iterations of the loop is returned in NITER. */
  663. bool
  664. can_unroll_loop_p (struct loop *loop, unsigned factor,
  665. struct tree_niter_desc *niter)
  666. {
  667. edge exit;
  668. /* Check whether unrolling is possible. We only want to unroll loops
  669. for that we are able to determine number of iterations. We also
  670. want to split the extra iterations of the loop from its end,
  671. therefore we require that the loop has precisely one
  672. exit. */
  673. exit = single_dom_exit (loop);
  674. if (!exit)
  675. return false;
  676. if (!number_of_iterations_exit (loop, exit, niter, false)
  677. || niter->cmp == ERROR_MARK
  678. /* Scalar evolutions analysis might have copy propagated
  679. the abnormal ssa names into these expressions, hence
  680. emitting the computations based on them during loop
  681. unrolling might create overlapping life ranges for
  682. them, and failures in out-of-ssa. */
  683. || contains_abnormal_ssa_name_p (niter->may_be_zero)
  684. || contains_abnormal_ssa_name_p (niter->control.base)
  685. || contains_abnormal_ssa_name_p (niter->control.step)
  686. || contains_abnormal_ssa_name_p (niter->bound))
  687. return false;
  688. /* And of course, we must be able to duplicate the loop. */
  689. if (!can_duplicate_loop_p (loop))
  690. return false;
  691. /* The final loop should be small enough. */
  692. if (tree_num_loop_insns (loop, &eni_size_weights) * factor
  693. > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS))
  694. return false;
  695. return true;
  696. }
  697. /* Determines the conditions that control execution of LOOP unrolled FACTOR
  698. times. DESC is number of iterations of LOOP. ENTER_COND is set to
  699. condition that must be true if the main loop can be entered.
  700. EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
  701. how the exit from the unrolled loop should be controlled. */
  702. static void
  703. determine_exit_conditions (struct loop *loop, struct tree_niter_desc *desc,
  704. unsigned factor, tree *enter_cond,
  705. tree *exit_base, tree *exit_step,
  706. enum tree_code *exit_cmp, tree *exit_bound)
  707. {
  708. gimple_seq stmts;
  709. tree base = desc->control.base;
  710. tree step = desc->control.step;
  711. tree bound = desc->bound;
  712. tree type = TREE_TYPE (step);
  713. tree bigstep, delta;
  714. tree min = lower_bound_in_type (type, type);
  715. tree max = upper_bound_in_type (type, type);
  716. enum tree_code cmp = desc->cmp;
  717. tree cond = boolean_true_node, assum;
  718. /* For pointers, do the arithmetics in the type of step. */
  719. base = fold_convert (type, base);
  720. bound = fold_convert (type, bound);
  721. *enter_cond = boolean_false_node;
  722. *exit_base = NULL_TREE;
  723. *exit_step = NULL_TREE;
  724. *exit_cmp = ERROR_MARK;
  725. *exit_bound = NULL_TREE;
  726. gcc_assert (cmp != ERROR_MARK);
  727. /* We only need to be correct when we answer question
  728. "Do at least FACTOR more iterations remain?" in the unrolled loop.
  729. Thus, transforming BASE + STEP * i <> BOUND to
  730. BASE + STEP * i < BOUND is ok. */
  731. if (cmp == NE_EXPR)
  732. {
  733. if (tree_int_cst_sign_bit (step))
  734. cmp = GT_EXPR;
  735. else
  736. cmp = LT_EXPR;
  737. }
  738. else if (cmp == LT_EXPR)
  739. {
  740. gcc_assert (!tree_int_cst_sign_bit (step));
  741. }
  742. else if (cmp == GT_EXPR)
  743. {
  744. gcc_assert (tree_int_cst_sign_bit (step));
  745. }
  746. else
  747. gcc_unreachable ();
  748. /* The main body of the loop may be entered iff:
  749. 1) desc->may_be_zero is false.
  750. 2) it is possible to check that there are at least FACTOR iterations
  751. of the loop, i.e., BOUND - step * FACTOR does not overflow.
  752. 3) # of iterations is at least FACTOR */
  753. if (!integer_zerop (desc->may_be_zero))
  754. cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
  755. invert_truthvalue (desc->may_be_zero),
  756. cond);
  757. bigstep = fold_build2 (MULT_EXPR, type, step,
  758. build_int_cst_type (type, factor));
  759. delta = fold_build2 (MINUS_EXPR, type, bigstep, step);
  760. if (cmp == LT_EXPR)
  761. assum = fold_build2 (GE_EXPR, boolean_type_node,
  762. bound,
  763. fold_build2 (PLUS_EXPR, type, min, delta));
  764. else
  765. assum = fold_build2 (LE_EXPR, boolean_type_node,
  766. bound,
  767. fold_build2 (PLUS_EXPR, type, max, delta));
  768. cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
  769. bound = fold_build2 (MINUS_EXPR, type, bound, delta);
  770. assum = fold_build2 (cmp, boolean_type_node, base, bound);
  771. cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
  772. cond = force_gimple_operand (unshare_expr (cond), &stmts, false, NULL_TREE);
  773. if (stmts)
  774. gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
  775. /* cond now may be a gimple comparison, which would be OK, but also any
  776. other gimple rhs (say a && b). In this case we need to force it to
  777. operand. */
  778. if (!is_gimple_condexpr (cond))
  779. {
  780. cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
  781. if (stmts)
  782. gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
  783. }
  784. *enter_cond = cond;
  785. base = force_gimple_operand (unshare_expr (base), &stmts, true, NULL_TREE);
  786. if (stmts)
  787. gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
  788. bound = force_gimple_operand (unshare_expr (bound), &stmts, true, NULL_TREE);
  789. if (stmts)
  790. gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
  791. *exit_base = base;
  792. *exit_step = bigstep;
  793. *exit_cmp = cmp;
  794. *exit_bound = bound;
  795. }
  796. /* Scales the frequencies of all basic blocks in LOOP that are strictly
  797. dominated by BB by NUM/DEN. */
  798. static void
  799. scale_dominated_blocks_in_loop (struct loop *loop, basic_block bb,
  800. int num, int den)
  801. {
  802. basic_block son;
  803. if (den == 0)
  804. return;
  805. for (son = first_dom_son (CDI_DOMINATORS, bb);
  806. son;
  807. son = next_dom_son (CDI_DOMINATORS, son))
  808. {
  809. if (!flow_bb_inside_loop_p (loop, son))
  810. continue;
  811. scale_bbs_frequencies_int (&son, 1, num, den);
  812. scale_dominated_blocks_in_loop (loop, son, num, den);
  813. }
  814. }
  815. /* Unroll LOOP FACTOR times. DESC describes number of iterations of LOOP.
  816. EXIT is the exit of the loop to that DESC corresponds.
  817. If N is number of iterations of the loop and MAY_BE_ZERO is the condition
  818. under that loop exits in the first iteration even if N != 0,
  819. while (1)
  820. {
  821. x = phi (init, next);
  822. pre;
  823. if (st)
  824. break;
  825. post;
  826. }
  827. becomes (with possibly the exit conditions formulated a bit differently,
  828. avoiding the need to create a new iv):
  829. if (MAY_BE_ZERO || N < FACTOR)
  830. goto rest;
  831. do
  832. {
  833. x = phi (init, next);
  834. pre;
  835. post;
  836. pre;
  837. post;
  838. ...
  839. pre;
  840. post;
  841. N -= FACTOR;
  842. } while (N >= FACTOR);
  843. rest:
  844. init' = phi (init, x);
  845. while (1)
  846. {
  847. x = phi (init', next);
  848. pre;
  849. if (st)
  850. break;
  851. post;
  852. }
  853. Before the loop is unrolled, TRANSFORM is called for it (only for the
  854. unrolled loop, but not for its versioned copy). DATA is passed to
  855. TRANSFORM. */
  856. /* Probability in % that the unrolled loop is entered. Just a guess. */
  857. #define PROB_UNROLLED_LOOP_ENTERED 90
  858. void
  859. tree_transform_and_unroll_loop (struct loop *loop, unsigned factor,
  860. edge exit, struct tree_niter_desc *desc,
  861. transform_callback transform,
  862. void *data)
  863. {
  864. gcond *exit_if;
  865. tree ctr_before, ctr_after;
  866. tree enter_main_cond, exit_base, exit_step, exit_bound;
  867. enum tree_code exit_cmp;
  868. gphi *phi_old_loop, *phi_new_loop, *phi_rest;
  869. gphi_iterator psi_old_loop, psi_new_loop;
  870. tree init, next, new_init;
  871. struct loop *new_loop;
  872. basic_block rest, exit_bb;
  873. edge old_entry, new_entry, old_latch, precond_edge, new_exit;
  874. edge new_nonexit, e;
  875. gimple_stmt_iterator bsi;
  876. use_operand_p op;
  877. bool ok;
  878. unsigned est_niter, prob_entry, scale_unrolled, scale_rest, freq_e, freq_h;
  879. unsigned new_est_niter, i, prob;
  880. unsigned irr = loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP;
  881. sbitmap wont_exit;
  882. auto_vec<edge> to_remove;
  883. est_niter = expected_loop_iterations (loop);
  884. determine_exit_conditions (loop, desc, factor,
  885. &enter_main_cond, &exit_base, &exit_step,
  886. &exit_cmp, &exit_bound);
  887. /* Let us assume that the unrolled loop is quite likely to be entered. */
  888. if (integer_nonzerop (enter_main_cond))
  889. prob_entry = REG_BR_PROB_BASE;
  890. else
  891. prob_entry = PROB_UNROLLED_LOOP_ENTERED * REG_BR_PROB_BASE / 100;
  892. /* The values for scales should keep profile consistent, and somewhat close
  893. to correct.
  894. TODO: The current value of SCALE_REST makes it appear that the loop that
  895. is created by splitting the remaining iterations of the unrolled loop is
  896. executed the same number of times as the original loop, and with the same
  897. frequencies, which is obviously wrong. This does not appear to cause
  898. problems, so we do not bother with fixing it for now. To make the profile
  899. correct, we would need to change the probability of the exit edge of the
  900. loop, and recompute the distribution of frequencies in its body because
  901. of this change (scale the frequencies of blocks before and after the exit
  902. by appropriate factors). */
  903. scale_unrolled = prob_entry;
  904. scale_rest = REG_BR_PROB_BASE;
  905. new_loop = loop_version (loop, enter_main_cond, NULL,
  906. prob_entry, scale_unrolled, scale_rest, true);
  907. gcc_assert (new_loop != NULL);
  908. update_ssa (TODO_update_ssa);
  909. /* Determine the probability of the exit edge of the unrolled loop. */
  910. new_est_niter = est_niter / factor;
  911. /* Without profile feedback, loops for that we do not know a better estimate
  912. are assumed to roll 10 times. When we unroll such loop, it appears to
  913. roll too little, and it may even seem to be cold. To avoid this, we
  914. ensure that the created loop appears to roll at least 5 times (but at
  915. most as many times as before unrolling). */
  916. if (new_est_niter < 5)
  917. {
  918. if (est_niter < 5)
  919. new_est_niter = est_niter;
  920. else
  921. new_est_niter = 5;
  922. }
  923. /* Prepare the cfg and update the phi nodes. Move the loop exit to the
  924. loop latch (and make its condition dummy, for the moment). */
  925. rest = loop_preheader_edge (new_loop)->src;
  926. precond_edge = single_pred_edge (rest);
  927. split_edge (loop_latch_edge (loop));
  928. exit_bb = single_pred (loop->latch);
  929. /* Since the exit edge will be removed, the frequency of all the blocks
  930. in the loop that are dominated by it must be scaled by
  931. 1 / (1 - exit->probability). */
  932. scale_dominated_blocks_in_loop (loop, exit->src,
  933. REG_BR_PROB_BASE,
  934. REG_BR_PROB_BASE - exit->probability);
  935. bsi = gsi_last_bb (exit_bb);
  936. exit_if = gimple_build_cond (EQ_EXPR, integer_zero_node,
  937. integer_zero_node,
  938. NULL_TREE, NULL_TREE);
  939. gsi_insert_after (&bsi, exit_if, GSI_NEW_STMT);
  940. new_exit = make_edge (exit_bb, rest, EDGE_FALSE_VALUE | irr);
  941. rescan_loop_exit (new_exit, true, false);
  942. /* Set the probability of new exit to the same of the old one. Fix
  943. the frequency of the latch block, by scaling it back by
  944. 1 - exit->probability. */
  945. new_exit->count = exit->count;
  946. new_exit->probability = exit->probability;
  947. new_nonexit = single_pred_edge (loop->latch);
  948. new_nonexit->probability = REG_BR_PROB_BASE - exit->probability;
  949. new_nonexit->flags = EDGE_TRUE_VALUE;
  950. new_nonexit->count -= exit->count;
  951. if (new_nonexit->count < 0)
  952. new_nonexit->count = 0;
  953. scale_bbs_frequencies_int (&loop->latch, 1, new_nonexit->probability,
  954. REG_BR_PROB_BASE);
  955. old_entry = loop_preheader_edge (loop);
  956. new_entry = loop_preheader_edge (new_loop);
  957. old_latch = loop_latch_edge (loop);
  958. for (psi_old_loop = gsi_start_phis (loop->header),
  959. psi_new_loop = gsi_start_phis (new_loop->header);
  960. !gsi_end_p (psi_old_loop);
  961. gsi_next (&psi_old_loop), gsi_next (&psi_new_loop))
  962. {
  963. phi_old_loop = psi_old_loop.phi ();
  964. phi_new_loop = psi_new_loop.phi ();
  965. init = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_entry);
  966. op = PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop, new_entry);
  967. gcc_assert (operand_equal_for_phi_arg_p (init, USE_FROM_PTR (op)));
  968. next = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_latch);
  969. /* Prefer using original variable as a base for the new ssa name.
  970. This is necessary for virtual ops, and useful in order to avoid
  971. losing debug info for real ops. */
  972. if (TREE_CODE (next) == SSA_NAME
  973. && useless_type_conversion_p (TREE_TYPE (next),
  974. TREE_TYPE (init)))
  975. new_init = copy_ssa_name (next);
  976. else if (TREE_CODE (init) == SSA_NAME
  977. && useless_type_conversion_p (TREE_TYPE (init),
  978. TREE_TYPE (next)))
  979. new_init = copy_ssa_name (init);
  980. else if (useless_type_conversion_p (TREE_TYPE (next), TREE_TYPE (init)))
  981. new_init = make_temp_ssa_name (TREE_TYPE (next), NULL, "unrinittmp");
  982. else
  983. new_init = make_temp_ssa_name (TREE_TYPE (init), NULL, "unrinittmp");
  984. phi_rest = create_phi_node (new_init, rest);
  985. add_phi_arg (phi_rest, init, precond_edge, UNKNOWN_LOCATION);
  986. add_phi_arg (phi_rest, next, new_exit, UNKNOWN_LOCATION);
  987. SET_USE (op, new_init);
  988. }
  989. remove_path (exit);
  990. /* Transform the loop. */
  991. if (transform)
  992. (*transform) (loop, data);
  993. /* Unroll the loop and remove the exits in all iterations except for the
  994. last one. */
  995. wont_exit = sbitmap_alloc (factor);
  996. bitmap_ones (wont_exit);
  997. bitmap_clear_bit (wont_exit, factor - 1);
  998. ok = gimple_duplicate_loop_to_header_edge
  999. (loop, loop_latch_edge (loop), factor - 1,
  1000. wont_exit, new_exit, &to_remove, DLTHE_FLAG_UPDATE_FREQ);
  1001. free (wont_exit);
  1002. gcc_assert (ok);
  1003. FOR_EACH_VEC_ELT (to_remove, i, e)
  1004. {
  1005. ok = remove_path (e);
  1006. gcc_assert (ok);
  1007. }
  1008. update_ssa (TODO_update_ssa);
  1009. /* Ensure that the frequencies in the loop match the new estimated
  1010. number of iterations, and change the probability of the new
  1011. exit edge. */
  1012. freq_h = loop->header->frequency;
  1013. freq_e = EDGE_FREQUENCY (loop_preheader_edge (loop));
  1014. if (freq_h != 0)
  1015. scale_loop_frequencies (loop, freq_e * (new_est_niter + 1), freq_h);
  1016. exit_bb = single_pred (loop->latch);
  1017. new_exit = find_edge (exit_bb, rest);
  1018. new_exit->count = loop_preheader_edge (loop)->count;
  1019. new_exit->probability = REG_BR_PROB_BASE / (new_est_niter + 1);
  1020. rest->count += new_exit->count;
  1021. rest->frequency += EDGE_FREQUENCY (new_exit);
  1022. new_nonexit = single_pred_edge (loop->latch);
  1023. prob = new_nonexit->probability;
  1024. new_nonexit->probability = REG_BR_PROB_BASE - new_exit->probability;
  1025. new_nonexit->count = exit_bb->count - new_exit->count;
  1026. if (new_nonexit->count < 0)
  1027. new_nonexit->count = 0;
  1028. if (prob > 0)
  1029. scale_bbs_frequencies_int (&loop->latch, 1, new_nonexit->probability,
  1030. prob);
  1031. /* Finally create the new counter for number of iterations and add the new
  1032. exit instruction. */
  1033. bsi = gsi_last_nondebug_bb (exit_bb);
  1034. exit_if = as_a <gcond *> (gsi_stmt (bsi));
  1035. create_iv (exit_base, exit_step, NULL_TREE, loop,
  1036. &bsi, false, &ctr_before, &ctr_after);
  1037. gimple_cond_set_code (exit_if, exit_cmp);
  1038. gimple_cond_set_lhs (exit_if, ctr_after);
  1039. gimple_cond_set_rhs (exit_if, exit_bound);
  1040. update_stmt (exit_if);
  1041. #ifdef ENABLE_CHECKING
  1042. verify_flow_info ();
  1043. verify_loop_structure ();
  1044. verify_loop_closed_ssa (true);
  1045. #endif
  1046. }
  1047. /* Wrapper over tree_transform_and_unroll_loop for case we do not
  1048. want to transform the loop before unrolling. The meaning
  1049. of the arguments is the same as for tree_transform_and_unroll_loop. */
  1050. void
  1051. tree_unroll_loop (struct loop *loop, unsigned factor,
  1052. edge exit, struct tree_niter_desc *desc)
  1053. {
  1054. tree_transform_and_unroll_loop (loop, factor, exit, desc,
  1055. NULL, NULL);
  1056. }
  1057. /* Rewrite the phi node at position PSI in function of the main
  1058. induction variable MAIN_IV and insert the generated code at GSI. */
  1059. static void
  1060. rewrite_phi_with_iv (loop_p loop,
  1061. gphi_iterator *psi,
  1062. gimple_stmt_iterator *gsi,
  1063. tree main_iv)
  1064. {
  1065. affine_iv iv;
  1066. gassign *stmt;
  1067. gphi *phi = psi->phi ();
  1068. tree atype, mtype, val, res = PHI_RESULT (phi);
  1069. if (virtual_operand_p (res) || res == main_iv)
  1070. {
  1071. gsi_next (psi);
  1072. return;
  1073. }
  1074. if (!simple_iv (loop, loop, res, &iv, true))
  1075. {
  1076. gsi_next (psi);
  1077. return;
  1078. }
  1079. remove_phi_node (psi, false);
  1080. atype = TREE_TYPE (res);
  1081. mtype = POINTER_TYPE_P (atype) ? sizetype : atype;
  1082. val = fold_build2 (MULT_EXPR, mtype, unshare_expr (iv.step),
  1083. fold_convert (mtype, main_iv));
  1084. val = fold_build2 (POINTER_TYPE_P (atype)
  1085. ? POINTER_PLUS_EXPR : PLUS_EXPR,
  1086. atype, unshare_expr (iv.base), val);
  1087. val = force_gimple_operand_gsi (gsi, val, false, NULL_TREE, true,
  1088. GSI_SAME_STMT);
  1089. stmt = gimple_build_assign (res, val);
  1090. gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
  1091. }
  1092. /* Rewrite all the phi nodes of LOOP in function of the main induction
  1093. variable MAIN_IV. */
  1094. static void
  1095. rewrite_all_phi_nodes_with_iv (loop_p loop, tree main_iv)
  1096. {
  1097. unsigned i;
  1098. basic_block *bbs = get_loop_body_in_dom_order (loop);
  1099. gphi_iterator psi;
  1100. for (i = 0; i < loop->num_nodes; i++)
  1101. {
  1102. basic_block bb = bbs[i];
  1103. gimple_stmt_iterator gsi = gsi_after_labels (bb);
  1104. if (bb->loop_father != loop)
  1105. continue;
  1106. for (psi = gsi_start_phis (bb); !gsi_end_p (psi); )
  1107. rewrite_phi_with_iv (loop, &psi, &gsi, main_iv);
  1108. }
  1109. free (bbs);
  1110. }
  1111. /* Bases all the induction variables in LOOP on a single induction
  1112. variable (unsigned with base 0 and step 1), whose final value is
  1113. compared with *NIT. When the IV type precision has to be larger
  1114. than *NIT type precision, *NIT is converted to the larger type, the
  1115. conversion code is inserted before the loop, and *NIT is updated to
  1116. the new definition. When BUMP_IN_LATCH is true, the induction
  1117. variable is incremented in the loop latch, otherwise it is
  1118. incremented in the loop header. Return the induction variable that
  1119. was created. */
  1120. tree
  1121. canonicalize_loop_ivs (struct loop *loop, tree *nit, bool bump_in_latch)
  1122. {
  1123. unsigned precision = TYPE_PRECISION (TREE_TYPE (*nit));
  1124. unsigned original_precision = precision;
  1125. tree type, var_before;
  1126. gimple_stmt_iterator gsi;
  1127. gphi_iterator psi;
  1128. gcond *stmt;
  1129. edge exit = single_dom_exit (loop);
  1130. gimple_seq stmts;
  1131. machine_mode mode;
  1132. bool unsigned_p = false;
  1133. for (psi = gsi_start_phis (loop->header);
  1134. !gsi_end_p (psi); gsi_next (&psi))
  1135. {
  1136. gphi *phi = psi.phi ();
  1137. tree res = PHI_RESULT (phi);
  1138. bool uns;
  1139. type = TREE_TYPE (res);
  1140. if (virtual_operand_p (res)
  1141. || (!INTEGRAL_TYPE_P (type)
  1142. && !POINTER_TYPE_P (type))
  1143. || TYPE_PRECISION (type) < precision)
  1144. continue;
  1145. uns = POINTER_TYPE_P (type) | TYPE_UNSIGNED (type);
  1146. if (TYPE_PRECISION (type) > precision)
  1147. unsigned_p = uns;
  1148. else
  1149. unsigned_p |= uns;
  1150. precision = TYPE_PRECISION (type);
  1151. }
  1152. mode = smallest_mode_for_size (precision, MODE_INT);
  1153. precision = GET_MODE_PRECISION (mode);
  1154. type = build_nonstandard_integer_type (precision, unsigned_p);
  1155. if (original_precision != precision)
  1156. {
  1157. *nit = fold_convert (type, *nit);
  1158. *nit = force_gimple_operand (*nit, &stmts, true, NULL_TREE);
  1159. if (stmts)
  1160. gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
  1161. }
  1162. if (bump_in_latch)
  1163. gsi = gsi_last_bb (loop->latch);
  1164. else
  1165. gsi = gsi_last_nondebug_bb (loop->header);
  1166. create_iv (build_int_cst_type (type, 0), build_int_cst (type, 1), NULL_TREE,
  1167. loop, &gsi, bump_in_latch, &var_before, NULL);
  1168. rewrite_all_phi_nodes_with_iv (loop, var_before);
  1169. stmt = as_a <gcond *> (last_stmt (exit->src));
  1170. /* Make the loop exit if the control condition is not satisfied. */
  1171. if (exit->flags & EDGE_TRUE_VALUE)
  1172. {
  1173. edge te, fe;
  1174. extract_true_false_edges_from_block (exit->src, &te, &fe);
  1175. te->flags = EDGE_FALSE_VALUE;
  1176. fe->flags = EDGE_TRUE_VALUE;
  1177. }
  1178. gimple_cond_set_code (stmt, LT_EXPR);
  1179. gimple_cond_set_lhs (stmt, var_before);
  1180. gimple_cond_set_rhs (stmt, *nit);
  1181. update_stmt (stmt);
  1182. return var_before;
  1183. }