Line data Source code
1 : /* High-level loop manipulation functions.
2 : Copyright (C) 2004-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU General Public License as published by the
8 : Free Software Foundation; either version 3, or (at your option) any
9 : later version.
10 :
11 : GCC is distributed in the hope that it will be useful, but WITHOUT
12 : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "tree.h"
25 : #include "gimple.h"
26 : #include "cfghooks.h"
27 : #include "tree-pass.h" /* ??? for TODO_update_ssa but this isn't a pass. */
28 : #include "ssa.h"
29 : #include "gimple-pretty-print.h"
30 : #include "fold-const.h"
31 : #include "cfganal.h"
32 : #include "gimplify.h"
33 : #include "gimple-iterator.h"
34 : #include "gimplify-me.h"
35 : #include "tree-cfg.h"
36 : #include "tree-ssa-loop-ivopts.h"
37 : #include "tree-ssa-loop-manip.h"
38 : #include "tree-ssa-loop-niter.h"
39 : #include "tree-ssa-loop.h"
40 : #include "tree-into-ssa.h"
41 : #include "tree-ssa.h"
42 : #include "cfgloop.h"
43 : #include "tree-scalar-evolution.h"
44 : #include "tree-inline.h"
45 :
46 : /* All bitmaps for rewriting into loop-closed SSA go on this obstack,
47 : so that we can free them all at once. */
48 : static bitmap_obstack loop_renamer_obstack;
49 :
50 : /* Insert IV increment statements STMTS before or after INCR_POS;
51 : AFTER selects which. INCR_POS and AFTER can be computed using
52 : standard_iv_increment_position. */
53 :
54 : void
55 1018252 : insert_iv_increment (gimple_stmt_iterator *incr_pos, bool after,
56 : gimple_seq stmts)
57 : {
58 : /* Prevent the increment from inheriting a bogus location if it is not put
59 : immediately after a statement whose location is known. */
60 1018252 : if (after)
61 : {
62 11092 : gimple_stmt_iterator gsi = *incr_pos;
63 11092 : if (!gsi_end_p (gsi))
64 9577 : gsi_next_nondebug (&gsi);
65 11092 : if (gsi_end_p (gsi))
66 : {
67 11092 : edge e = single_succ_edge (gsi_bb (*incr_pos));
68 11092 : gimple_seq_set_location (stmts, e->goto_locus);
69 : }
70 11092 : gsi_insert_seq_after (incr_pos, stmts, GSI_NEW_STMT);
71 : }
72 : else
73 : {
74 1007160 : gimple_stmt_iterator gsi = *incr_pos;
75 1007160 : if (!gsi_end_p (gsi) && is_gimple_debug (gsi_stmt (gsi)))
76 0 : gsi_next_nondebug (&gsi);
77 1007160 : if (!gsi_end_p (gsi))
78 1006660 : gimple_seq_set_location (stmts, gimple_location (gsi_stmt (gsi)));
79 1007160 : gsi_insert_seq_before (incr_pos, stmts, GSI_NEW_STMT);
80 : }
81 1018252 : }
82 :
83 : /* Creates an induction variable with value BASE (+/-) STEP * iteration in LOOP.
84 : If INCR_OP is PLUS_EXPR, the induction variable is BASE + STEP * iteration.
85 : If INCR_OP is MINUS_EXPR, the induction variable is BASE - STEP * iteration.
86 : It is expected that neither BASE nor STEP are shared with other expressions
87 : (unless the sharing rules allow this). Use VAR as a base var_decl for it
88 : (if NULL, a new temporary will be created). The increment will occur at
89 : INCR_POS (after it if AFTER is true, before it otherwise). INCR_POS and
90 : AFTER can be computed using standard_iv_increment_position. The ssa versions
91 : of the variable before and after increment will be stored in VAR_BEFORE and
92 : VAR_AFTER (unless they are NULL). If LOOP_INVARIANT is true then the
93 : calculation step will be added to the pre-header. Otherwise, the step
94 : calculation is added to incr_pos. */
95 :
96 : void
97 1002270 : create_iv (tree base, tree_code incr_op, tree step, tree var, class loop *loop,
98 : gimple_stmt_iterator *incr_pos, bool after, tree *var_before,
99 : tree *var_after, bool loop_invariant)
100 : {
101 1002270 : gphi *phi;
102 1002270 : tree initial, step1;
103 1002270 : gimple_seq stmts;
104 1002270 : tree vb, va;
105 1002270 : gcc_assert (incr_op == PLUS_EXPR || incr_op == MINUS_EXPR);
106 1002270 : edge pe = loop_preheader_edge (loop);
107 :
108 1002270 : if (var != NULL_TREE)
109 : {
110 611782 : vb = make_ssa_name (var);
111 611782 : va = make_ssa_name (var);
112 : }
113 : else
114 : {
115 390488 : vb = make_temp_ssa_name (TREE_TYPE (base), NULL, "ivtmp");
116 390488 : va = make_temp_ssa_name (TREE_TYPE (base), NULL, "ivtmp");
117 : }
118 1002270 : if (var_before)
119 682572 : *var_before = vb;
120 1002270 : if (var_after)
121 994881 : *var_after = va;
122 :
123 : /* For easier readability of the created code, produce MINUS_EXPRs
124 : when suitable. */
125 1002270 : if (TREE_CODE (step) == INTEGER_CST)
126 : {
127 943854 : if (TYPE_UNSIGNED (TREE_TYPE (step)))
128 : {
129 941882 : step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
130 941882 : if (tree_int_cst_lt (step1, step))
131 : {
132 334667 : incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
133 : step = step1;
134 : }
135 : }
136 : else
137 : {
138 1972 : if (!tree_expr_nonnegative_p (step)
139 1972 : && may_negate_without_overflow_p (step))
140 : {
141 21 : incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
142 21 : step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
143 : }
144 : }
145 : }
146 1002270 : if (POINTER_TYPE_P (TREE_TYPE (base)))
147 : {
148 135882 : if (TREE_CODE (base) == ADDR_EXPR)
149 49076 : mark_addressable (TREE_OPERAND (base, 0));
150 135882 : step = convert_to_ptrofftype (step);
151 135882 : if (incr_op == MINUS_EXPR)
152 2974 : step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
153 : incr_op = POINTER_PLUS_EXPR;
154 : }
155 :
156 1002270 : gimple_seq incr_stmts = nullptr;
157 : /* Gimplify the step if necessary. */
158 1002270 : if (loop_invariant)
159 : {
160 1002270 : step = force_gimple_operand (step, &stmts, true, NULL_TREE);
161 1002270 : if (stmts)
162 0 : gsi_insert_seq_on_edge_immediate (pe, stmts);
163 : }
164 : else
165 0 : step = force_gimple_operand (step, &incr_stmts, true, NULL_TREE);
166 :
167 1002270 : gimple_seq_add_stmt (&incr_stmts,
168 1002270 : gimple_build_assign (va, incr_op, vb, step));
169 1002270 : insert_iv_increment (incr_pos, after, incr_stmts);
170 :
171 1002270 : initial = force_gimple_operand (base, &stmts, true, var);
172 1002270 : if (stmts)
173 261978 : gsi_insert_seq_on_edge_immediate (pe, stmts);
174 :
175 1002270 : phi = create_phi_node (vb, loop->header);
176 1002270 : add_phi_arg (phi, initial, loop_preheader_edge (loop), UNKNOWN_LOCATION);
177 1002270 : add_phi_arg (phi, va, loop_latch_edge (loop), UNKNOWN_LOCATION);
178 1002270 : }
179 :
180 : /* Return the innermost superloop LOOP of USE_LOOP that is a superloop of
181 : both DEF_LOOP and USE_LOOP. */
182 :
183 : static inline class loop *
184 143544 : find_sibling_superloop (class loop *use_loop, class loop *def_loop)
185 : {
186 143544 : unsigned ud = loop_depth (use_loop);
187 143544 : unsigned dd = loop_depth (def_loop);
188 143544 : gcc_assert (ud > 0 && dd > 0);
189 143544 : if (ud > dd)
190 17816 : use_loop = superloop_at_depth (use_loop, dd);
191 143544 : if (ud < dd)
192 25604 : def_loop = superloop_at_depth (def_loop, ud);
193 146023 : while (loop_outer (use_loop) != loop_outer (def_loop))
194 : {
195 2479 : use_loop = loop_outer (use_loop);
196 2479 : def_loop = loop_outer (def_loop);
197 2479 : gcc_assert (use_loop && def_loop);
198 : }
199 143544 : return use_loop;
200 : }
201 :
202 : /* DEF_BB is a basic block containing a DEF that needs rewriting into
203 : loop-closed SSA form. USE_BLOCKS is the set of basic blocks containing
204 : uses of DEF that "escape" from the loop containing DEF_BB (i.e. blocks in
205 : USE_BLOCKS are dominated by DEF_BB but not in the loop father of DEF_BB).
206 : ALL_EXITS[I] is the set of all basic blocks that exit loop I.
207 : DEF_LOOP_EXITS is a bitmap of loop exit blocks that exit the loop
208 : containing DEF_BB or its outer loops.
209 :
210 : Compute the subset of loop exit destinations that exit the loop
211 : containing DEF_BB or one of its loop fathers, in which DEF is live.
212 : This set is returned in the bitmap LIVE_EXITS.
213 :
214 : Instead of computing the complete livein set of the def, we use the loop
215 : nesting tree as a form of poor man's structure analysis. This greatly
216 : speeds up the analysis, which is important because this function may be
217 : called on all SSA names that need rewriting, one at a time. */
218 :
219 : static void
220 3169399 : compute_live_loop_exits (bitmap live_exits, bitmap use_blocks,
221 : basic_block def_bb, bitmap def_loop_exits)
222 : {
223 3169399 : unsigned i;
224 3169399 : bitmap_iterator bi;
225 3169399 : class loop *def_loop = def_bb->loop_father;
226 3169399 : unsigned def_loop_depth = loop_depth (def_loop);
227 :
228 : /* Normally the work list size is bounded by the number of basic
229 : blocks in the largest loop. We don't know this number, but we
230 : can be fairly sure that it will be relatively small. */
231 3169399 : auto_vec<basic_block, 8> worklist (MAX (8, n_basic_blocks_for_fn (cfun) / 128));
232 :
233 7651367 : EXECUTE_IF_SET_IN_BITMAP (use_blocks, 0, i, bi)
234 : {
235 4481968 : basic_block use_bb = BASIC_BLOCK_FOR_FN (cfun, i);
236 4481968 : class loop *use_loop = use_bb->loop_father;
237 4481968 : gcc_checking_assert (def_loop != use_loop
238 : && ! flow_loop_nested_p (def_loop, use_loop));
239 4481968 : if (! flow_loop_nested_p (use_loop, def_loop))
240 121183 : use_bb = find_sibling_superloop (use_loop, def_loop)->header;
241 4481968 : if (bitmap_set_bit (live_exits, use_bb->index))
242 4440978 : worklist.safe_push (use_bb);
243 : }
244 :
245 : /* Iterate until the worklist is empty. */
246 9900181 : while (! worklist.is_empty ())
247 : {
248 6730782 : edge e;
249 6730782 : edge_iterator ei;
250 :
251 : /* Pull a block off the worklist. */
252 6730782 : basic_block bb = worklist.pop ();
253 :
254 : /* Make sure we have at least enough room in the work list
255 : for all predecessors of this block. */
256 6730782 : worklist.reserve (EDGE_COUNT (bb->preds));
257 :
258 : /* For each predecessor block. */
259 14532458 : FOR_EACH_EDGE (e, ei, bb->preds)
260 : {
261 7801676 : basic_block pred = e->src;
262 7801676 : class loop *pred_loop = pred->loop_father;
263 7801676 : unsigned pred_loop_depth = loop_depth (pred_loop);
264 7801676 : bool pred_visited;
265 :
266 : /* We should have met DEF_BB along the way. */
267 7801676 : gcc_assert (pred != ENTRY_BLOCK_PTR_FOR_FN (cfun));
268 :
269 7801676 : if (pred_loop_depth >= def_loop_depth)
270 : {
271 4746521 : if (pred_loop_depth > def_loop_depth)
272 157490 : pred_loop = superloop_at_depth (pred_loop, def_loop_depth);
273 : /* If we've reached DEF_LOOP, our train ends here. */
274 4746521 : if (pred_loop == def_loop)
275 5511872 : continue;
276 : }
277 3055155 : else if (! flow_loop_nested_p (pred_loop, def_loop))
278 22361 : pred = find_sibling_superloop (pred_loop, def_loop)->header;
279 :
280 : /* Add PRED to the LIVEIN set. PRED_VISITED is true if
281 : we had already added PRED to LIVEIN before. */
282 3948155 : pred_visited = !bitmap_set_bit (live_exits, pred->index);
283 :
284 : /* If we have visited PRED before, don't add it to the worklist.
285 : If BB dominates PRED, then we're probably looking at a loop.
286 : We're only interested in looking up in the dominance tree
287 : because DEF_BB dominates all the uses. */
288 3948155 : if (pred_visited || dominated_by_p (CDI_DOMINATORS, pred, bb))
289 1658351 : continue;
290 :
291 2289804 : worklist.quick_push (pred);
292 : }
293 : }
294 :
295 3169399 : bitmap_and_into (live_exits, def_loop_exits);
296 3169399 : }
297 :
298 : /* Add a loop-closing PHI for VAR in basic block EXIT. */
299 :
300 : static void
301 3779369 : add_exit_phi (basic_block exit, tree var)
302 : {
303 3779369 : gphi *phi;
304 3779369 : edge e;
305 3779369 : edge_iterator ei;
306 :
307 : /* Check that at least one of the edges entering the EXIT block exits
308 : the loop, or a superloop of that loop, that VAR is defined in. */
309 3779369 : if (flag_checking)
310 : {
311 3779267 : gimple *def_stmt = SSA_NAME_DEF_STMT (var);
312 3779267 : basic_block def_bb = gimple_bb (def_stmt);
313 3789163 : FOR_EACH_EDGE (e, ei, exit->preds)
314 : {
315 7578326 : class loop *aloop = find_common_loop (def_bb->loop_father,
316 3789163 : e->src->loop_father);
317 3789163 : if (!flow_bb_inside_loop_p (aloop, e->dest))
318 : break;
319 : }
320 3779267 : gcc_assert (e);
321 : }
322 :
323 3779369 : phi = create_phi_node (NULL_TREE, exit);
324 3779369 : create_new_def_for (var, phi, gimple_phi_result_ptr (phi));
325 7755866 : FOR_EACH_EDGE (e, ei, exit->preds)
326 3976497 : add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
327 :
328 3779369 : if (dump_file && (dump_flags & TDF_DETAILS))
329 : {
330 1900 : fprintf (dump_file, ";; Created LCSSA PHI: ");
331 1900 : print_gimple_stmt (dump_file, phi, 0, dump_flags);
332 : }
333 3779369 : }
334 :
335 : /* Add exit phis for VAR that is used in LIVEIN.
336 : Exits of the loops are stored in LOOP_EXITS. Returns the number
337 : of PHIs added for VAR. */
338 :
339 : static unsigned
340 3169399 : add_exit_phis_var (tree var, bitmap use_blocks, bitmap def_loop_exits)
341 : {
342 3169399 : unsigned index;
343 3169399 : bitmap_iterator bi;
344 3169399 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
345 :
346 3169399 : gcc_checking_assert (! bitmap_bit_p (use_blocks, def_bb->index));
347 :
348 3169399 : auto_bitmap live_exits (&loop_renamer_obstack);
349 3169399 : compute_live_loop_exits (live_exits, use_blocks, def_bb, def_loop_exits);
350 :
351 3169399 : unsigned cnt = 0;
352 6948768 : EXECUTE_IF_SET_IN_BITMAP (live_exits, 0, index, bi)
353 : {
354 3779369 : add_exit_phi (BASIC_BLOCK_FOR_FN (cfun, index), var);
355 3779369 : cnt++;
356 : }
357 3169399 : return cnt;
358 3169399 : }
359 :
360 : static int
361 47660874 : loop_name_cmp (const void *p1, const void *p2)
362 : {
363 47660874 : auto l1 = (const std::pair<int, int> *)p1;
364 47660874 : auto l2 = (const std::pair<int, int> *)p2;
365 47660874 : if (l1->first < l2->first)
366 : return -1;
367 28657123 : else if (l1->first > l2->first)
368 17876491 : return 1;
369 : return 0;
370 : }
371 :
372 : /* Add exit phis for the names marked in NAMES_TO_RENAME.
373 : Exits of the loops are stored in EXITS. Sets of blocks where the ssa
374 : names are used are stored in USE_BLOCKS. Returns whether any name
375 : required multiple LC PHI nodes. */
376 :
377 : static bool
378 731354 : add_exit_phis (bitmap names_to_rename, bitmap *use_blocks)
379 : {
380 731354 : unsigned i;
381 731354 : bitmap_iterator bi;
382 731354 : bool multiple_p = false;
383 :
384 : /* Sort names_to_rename after definition loop so we can avoid re-computing
385 : def_loop_exits. */
386 731354 : auto_vec<std::pair<int, int> > names (bitmap_count_bits (names_to_rename));
387 3900753 : EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
388 : {
389 3169399 : tree name = ssa_name (i);
390 3169399 : loop_p def_loop = gimple_bb (SSA_NAME_DEF_STMT (name))->loop_father;
391 3169399 : names.quick_push (std::make_pair (def_loop->num, i));
392 : }
393 731354 : names.qsort (loop_name_cmp);
394 :
395 731354 : auto_bitmap def_loop_exits (&loop_renamer_obstack);
396 731354 : loop_p last_def_loop = NULL;
397 5363461 : for (auto p : names)
398 : {
399 3169399 : loop_p def_loop = get_loop (cfun, p.first);
400 3169399 : if (def_loop != last_def_loop)
401 : {
402 1753891 : bitmap_clear (def_loop_exits);
403 1753891 : last_def_loop = def_loop;
404 4101978 : for (class loop *loop = def_loop; loop != current_loops->tree_root;
405 2348087 : loop = loop_outer (loop))
406 8557731 : for (auto exit = loop->exits->next; exit->e; exit = exit->next)
407 6209644 : bitmap_set_bit (def_loop_exits, exit->e->dest->index);
408 : }
409 3169399 : if (add_exit_phis_var (ssa_name (p.second), use_blocks[p.second],
410 : def_loop_exits) > 1)
411 356239 : multiple_p = true;
412 : }
413 :
414 731354 : return multiple_p;
415 731354 : }
416 :
417 : /* For USE in BB, if it is used outside of the loop it is defined in,
418 : mark it for rewrite. Record basic block BB where it is used
419 : to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap.
420 : Note that for USEs in phis, BB should be the src of the edge corresponding to
421 : the use, rather than the bb containing the phi. */
422 :
423 : static void
424 164798027 : find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks,
425 : bitmap need_phis)
426 : {
427 164798027 : unsigned ver;
428 164798027 : basic_block def_bb;
429 164798027 : class loop *def_loop;
430 :
431 164798027 : if (TREE_CODE (use) != SSA_NAME)
432 : return;
433 :
434 158943072 : ver = SSA_NAME_VERSION (use);
435 158943072 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (use));
436 158943072 : if (!def_bb)
437 : return;
438 147517190 : def_loop = def_bb->loop_father;
439 :
440 : /* If the definition is not inside a loop, it is not interesting. */
441 147517190 : if (!loop_outer (def_loop))
442 : return;
443 :
444 : /* If the use is not outside of the loop it is defined in, it is not
445 : interesting. */
446 77060829 : if (flow_bb_inside_loop_p (def_loop, bb))
447 : return;
448 :
449 : /* If we're seeing VER for the first time, we still have to allocate
450 : a bitmap for its uses. */
451 5113805 : if (bitmap_set_bit (need_phis, ver))
452 3169399 : use_blocks[ver] = BITMAP_ALLOC (&loop_renamer_obstack);
453 5113805 : bitmap_set_bit (use_blocks[ver], bb->index);
454 : }
455 :
456 : /* For uses matching USE_FLAGS in STMT, mark names that are used outside of the
457 : loop they are defined to rewrite. Record the set of blocks in which the ssa
458 : names are used to USE_BLOCKS, and the ssa names themselves to NEED_PHIS. */
459 :
460 : static void
461 219374410 : find_uses_to_rename_stmt (gimple *stmt, bitmap *use_blocks, bitmap need_phis,
462 : int use_flags)
463 : {
464 219374410 : ssa_op_iter iter;
465 219374410 : tree var;
466 219374410 : basic_block bb = gimple_bb (stmt);
467 :
468 219374410 : if (is_gimple_debug (stmt))
469 129469646 : return;
470 :
471 : /* FOR_EACH_SSA_TREE_OPERAND iterator does not allows SSA_OP_VIRTUAL_USES
472 : only. */
473 89904764 : if (use_flags == SSA_OP_VIRTUAL_USES)
474 : {
475 89904764 : tree vuse = gimple_vuse (stmt);
476 0 : if (vuse != NULL_TREE)
477 0 : find_uses_to_rename_use (bb, gimple_vuse (stmt), use_blocks, need_phis);
478 : }
479 : else
480 217325731 : FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, use_flags)
481 127420967 : find_uses_to_rename_use (bb, var, use_blocks, need_phis);
482 : }
483 :
484 : /* Marks names matching USE_FLAGS that are used in BB and outside of the loop
485 : they are defined in for rewrite. Records the set of blocks in which the ssa
486 : names are used to USE_BLOCKS. Record the SSA names that will
487 : need exit PHIs in NEED_PHIS. */
488 :
489 : static void
490 30643112 : find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks, bitmap need_phis,
491 : int use_flags)
492 : {
493 30643112 : edge e;
494 30643112 : edge_iterator ei;
495 30643112 : bool do_virtuals = (use_flags & SSA_OP_VIRTUAL_USES) != 0;
496 30643112 : bool do_nonvirtuals = (use_flags & SSA_OP_USE) != 0;
497 :
498 74230787 : FOR_EACH_EDGE (e, ei, bb->succs)
499 80964735 : for (gphi_iterator bsi = gsi_start_phis (e->dest); !gsi_end_p (bsi);
500 37377060 : gsi_next (&bsi))
501 : {
502 37377060 : gphi *phi = bsi.phi ();
503 37377060 : bool virtual_p = virtual_operand_p (gimple_phi_result (phi));
504 37377060 : if ((virtual_p && do_virtuals)
505 22260680 : || (!virtual_p && do_nonvirtuals))
506 37377060 : find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (phi, e),
507 : use_blocks, need_phis);
508 : }
509 :
510 280660634 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi);
511 219374410 : gsi_next (&bsi))
512 219374410 : find_uses_to_rename_stmt (gsi_stmt (bsi), use_blocks, need_phis,
513 : use_flags);
514 30643112 : }
515 :
516 : /* Marks names matching USE_FLAGS that are used outside of the loop they are
517 : defined in for rewrite. Records the set of blocks in which the ssa names are
518 : used to USE_BLOCKS. Record the SSA names that will need exit PHIs in
519 : NEED_PHIS. If CHANGED_BBS is not NULL, scan only blocks in this set. */
520 :
521 : static void
522 1050581 : find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks, bitmap need_phis,
523 : int use_flags)
524 : {
525 1050581 : basic_block bb;
526 1050581 : unsigned index;
527 1050581 : bitmap_iterator bi;
528 :
529 1050581 : if (changed_bbs)
530 28539 : EXECUTE_IF_SET_IN_BITMAP (changed_bbs, 0, index, bi)
531 : {
532 20935 : bb = BASIC_BLOCK_FOR_FN (cfun, index);
533 20935 : if (bb)
534 20935 : find_uses_to_rename_bb (bb, use_blocks, need_phis, use_flags);
535 : }
536 : else
537 31665154 : FOR_EACH_BB_FN (bb, cfun)
538 30622177 : find_uses_to_rename_bb (bb, use_blocks, need_phis, use_flags);
539 1050581 : }
540 :
541 : /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
542 : phi nodes to ensure that no variable is used outside the loop it is
543 : defined in.
544 :
545 : This strengthening of the basic ssa form has several advantages:
546 :
547 : 1) Updating it during unrolling/peeling/versioning is trivial, since
548 : we do not need to care about the uses outside of the loop.
549 : The same applies to virtual operands which are also rewritten into
550 : loop closed SSA form. Note that virtual operands are always live
551 : until function exit.
552 : 2) The behavior of all uses of an induction variable is the same.
553 : Without this, you need to distinguish the case when the variable
554 : is used outside of the loop it is defined in, for example
555 :
556 : for (i = 0; i < 100; i++)
557 : {
558 : for (j = 0; j < 100; j++)
559 : {
560 : k = i + j;
561 : use1 (k);
562 : }
563 : use2 (k);
564 : }
565 :
566 : Looking from the outer loop with the normal SSA form, the first use of k
567 : is not well-behaved, while the second one is an induction variable with
568 : base 99 and step 1.
569 :
570 : If CHANGED_BBS is not NULL, we look for uses outside loops only in the
571 : basic blocks in this set.
572 :
573 : USE_FLAGS allows us to specify whether we want virtual, non-virtual or
574 : both variables rewritten.
575 :
576 : UPDATE_FLAG is used in the call to update_ssa. See
577 : TODO_update_ssa* for documentation. */
578 :
579 : static void
580 4712029 : rewrite_into_loop_closed_ssa_1 (bitmap changed_bbs, unsigned update_flag,
581 : int use_flags)
582 : {
583 4712029 : bitmap *use_blocks;
584 4712029 : bitmap names_to_rename;
585 :
586 4712029 : loops_state_set (LOOP_CLOSED_SSA);
587 4712029 : if (number_of_loops (cfun) <= 1)
588 : return;
589 :
590 : /* If the pass has caused the SSA form to be out-of-date, update it
591 : now. */
592 1050581 : if (update_flag != 0)
593 1050197 : update_ssa (update_flag);
594 384 : else if (flag_checking)
595 384 : verify_ssa (true, true);
596 :
597 1050581 : bitmap_obstack_initialize (&loop_renamer_obstack);
598 :
599 1050581 : names_to_rename = BITMAP_ALLOC (&loop_renamer_obstack);
600 :
601 : /* Uses of names to rename. We don't have to initialize this array,
602 : because we know that we will only have entries for the SSA names
603 : in NAMES_TO_RENAME. */
604 2101162 : use_blocks = XNEWVEC (bitmap, num_ssa_names);
605 1050581 : find_uses_to_rename (changed_bbs, use_blocks, names_to_rename, use_flags);
606 :
607 1050581 : if (!bitmap_empty_p (names_to_rename))
608 : {
609 731354 : bool release_recorded_exits_p = false;
610 731354 : if (!loops_state_satisfies_p (LOOPS_HAVE_RECORDED_EXITS))
611 : {
612 : /* Doing one scan over the whole function is cheaper than
613 : traversing the loop tree and gathering BBs of each loop. */
614 0 : record_loop_exits ();
615 0 : release_recorded_exits_p = true;
616 : }
617 :
618 : /* Add the PHI nodes on exits of the loops for the names we need to
619 : rewrite. When no variable required multiple LC PHI nodes to be
620 : inserted then we know that all uses outside of the loop are
621 : dominated by the single LC SSA definition and no further PHI
622 : node insertions are required. */
623 731354 : bool need_phis_p = add_exit_phis (names_to_rename, use_blocks);
624 :
625 731354 : if (release_recorded_exits_p)
626 0 : release_recorded_exits (cfun);
627 :
628 : /* Fix up all the names found to be used outside their original
629 : loops. */
630 1317630 : update_ssa (need_phis_p ? TODO_update_ssa : TODO_update_ssa_no_phi);
631 : }
632 :
633 1050581 : bitmap_obstack_release (&loop_renamer_obstack);
634 1050581 : free (use_blocks);
635 : }
636 :
637 : /* Rewrites the defs and uses into a loop closed ssa form.
638 : If CHANGED_BBS is not NULL, we look for uses outside loops only in the basic
639 : blocks in this set. UPDATE_FLAG is used in the call to update_ssa. See
640 : TODO_update_ssa* for documentation. */
641 :
642 : void
643 4712029 : rewrite_into_loop_closed_ssa (bitmap changed_bbs, unsigned update_flag)
644 : {
645 4712029 : rewrite_into_loop_closed_ssa_1 (changed_bbs, update_flag, SSA_OP_ALL_USES);
646 4712029 : }
647 :
648 : /* Check invariants of the loop closed ssa form for the def in DEF_BB. */
649 :
650 : static void
651 176870149 : check_loop_closed_ssa_def (basic_block def_bb, tree def)
652 : {
653 176870149 : use_operand_p use_p;
654 176870149 : imm_use_iterator iterator;
655 698949931 : FOR_EACH_IMM_USE_FAST (use_p, iterator, def)
656 : {
657 345209633 : if (is_gimple_debug (USE_STMT (use_p)))
658 45654609 : continue;
659 :
660 299555024 : basic_block use_bb = gimple_bb (USE_STMT (use_p));
661 299555024 : if (is_a <gphi *> (USE_STMT (use_p)))
662 80502797 : use_bb = EDGE_PRED (use_bb, PHI_ARG_INDEX_FROM_USE (use_p))->src;
663 :
664 299555024 : gcc_assert (flow_bb_inside_loop_p (def_bb->loop_father, use_bb));
665 176870149 : }
666 176870149 : }
667 :
668 : /* Checks invariants of loop closed ssa form in BB. */
669 :
670 : static void
671 51992811 : check_loop_closed_ssa_bb (basic_block bb)
672 : {
673 94447922 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi);
674 42455111 : gsi_next (&bsi))
675 : {
676 42455111 : gphi *phi = bsi.phi ();
677 :
678 42455111 : check_loop_closed_ssa_def (bb, PHI_RESULT (phi));
679 : }
680 :
681 210887536 : for (gimple_stmt_iterator bsi = gsi_start_nondebug_bb (bb); !gsi_end_p (bsi);
682 158894725 : gsi_next_nondebug (&bsi))
683 : {
684 158894725 : ssa_op_iter iter;
685 158894725 : tree var;
686 158894725 : gimple *stmt = gsi_stmt (bsi);
687 :
688 293309763 : FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_DEFS)
689 134415038 : check_loop_closed_ssa_def (bb, var);
690 : }
691 51992811 : }
692 :
693 : /* Checks that invariants of the loop closed ssa form are preserved.
694 : Call verify_ssa when VERIFY_SSA_P is true. Note all loops are checked
695 : if LOOP is NULL, otherwise, only LOOP is checked. */
696 :
697 : DEBUG_FUNCTION void
698 3941939 : verify_loop_closed_ssa (bool verify_ssa_p, class loop *loop)
699 : {
700 7883878 : if (number_of_loops (cfun) <= 1)
701 : return;
702 :
703 3941939 : timevar_push (TV_VERIFY_LOOP_CLOSED);
704 :
705 3941939 : if (loop == NULL)
706 : {
707 3940307 : basic_block bb;
708 :
709 3940307 : if (verify_ssa_p)
710 34010 : verify_ssa (false, true);
711 :
712 131018604 : FOR_EACH_BB_FN (bb, cfun)
713 127078297 : if (bb->loop_father && bb->loop_father->num > 0)
714 51984993 : check_loop_closed_ssa_bb (bb);
715 : }
716 : else
717 : {
718 1632 : basic_block *bbs = get_loop_body (loop);
719 :
720 : /* We do not have loop-local SSA verification so just
721 : check there's no update queued. */
722 1632 : if (verify_ssa_p)
723 1632 : gcc_assert (!need_ssa_update_p (cfun));
724 :
725 9450 : for (unsigned i = 0; i < loop->num_nodes; ++i)
726 7818 : check_loop_closed_ssa_bb (bbs[i]);
727 :
728 1632 : free (bbs);
729 : }
730 :
731 3941939 : timevar_pop (TV_VERIFY_LOOP_CLOSED);
732 : }
733 :
734 : /* Split loop exit edge EXIT. The things are a bit complicated by a need to
735 : preserve the loop closed ssa form. If COPY_CONSTANTS_P is true then
736 : forwarder PHIs are also created for constant arguments.
737 : The newly created block is returned. */
738 :
739 : basic_block
740 65326 : split_loop_exit_edge (edge exit, bool copy_constants_p)
741 : {
742 65326 : basic_block dest = exit->dest;
743 65326 : basic_block bb = split_edge (exit);
744 65326 : gphi *phi, *new_phi;
745 65326 : tree new_name, name;
746 65326 : use_operand_p op_p;
747 65326 : gphi_iterator psi;
748 65326 : location_t locus;
749 :
750 155161 : for (psi = gsi_start_phis (dest); !gsi_end_p (psi); gsi_next (&psi))
751 : {
752 89835 : phi = psi.phi ();
753 89835 : op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb));
754 89835 : locus = gimple_phi_arg_location_from_edge (phi, single_succ_edge (bb));
755 :
756 89835 : name = USE_FROM_PTR (op_p);
757 :
758 : /* If the argument of the PHI node is a constant, we do not need
759 : to keep it inside loop. */
760 89835 : if (TREE_CODE (name) != SSA_NAME
761 2254 : && !copy_constants_p)
762 2117 : continue;
763 :
764 : /* Otherwise create an auxiliary phi node that will copy the value
765 : of the SSA name out of the loop. */
766 87718 : new_name = duplicate_ssa_name (PHI_RESULT (phi), NULL);
767 87718 : new_phi = create_phi_node (new_name, bb);
768 87718 : add_phi_arg (new_phi, name, exit, locus);
769 87718 : SET_USE (op_p, new_name);
770 : }
771 :
772 65326 : return bb;
773 : }
774 :
775 : /* Returns the basic block in that statements should be emitted for induction
776 : variables incremented at the end of the LOOP. */
777 :
778 : basic_block
779 24127324 : ip_end_pos (class loop *loop)
780 : {
781 24127324 : return loop->latch;
782 : }
783 :
784 : /* Returns the basic block in that statements should be emitted for induction
785 : variables incremented just before exit condition of a LOOP. */
786 :
787 : basic_block
788 50292539 : ip_normal_pos (class loop *loop)
789 : {
790 50292539 : basic_block bb;
791 50292539 : edge exit;
792 :
793 50343697 : if (!single_pred_p (loop->latch))
794 : return NULL;
795 :
796 50142705 : bb = single_pred (loop->latch);
797 100473668 : if (!safe_is_a <gcond *> (*gsi_last_bb (bb)))
798 : return NULL;
799 :
800 50129971 : exit = EDGE_SUCC (bb, 0);
801 50129971 : if (exit->dest == loop->latch)
802 38038067 : exit = EDGE_SUCC (bb, 1);
803 :
804 50129971 : if (flow_bb_inside_loop_p (loop, exit->dest))
805 : return NULL;
806 :
807 : return bb;
808 : }
809 :
810 : /* Stores the standard position for induction variable increment in LOOP
811 : (just before the exit condition if it is available and latch block is empty,
812 : end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
813 : the increment should be inserted after *BSI. */
814 :
815 : void
816 151288 : standard_iv_increment_position (class loop *loop, gimple_stmt_iterator *bsi,
817 : bool *insert_after)
818 : {
819 151288 : basic_block bb = ip_normal_pos (loop), latch = ip_end_pos (loop);
820 151288 : gimple *last = last_nondebug_stmt (latch);
821 :
822 151288 : if (!bb
823 151288 : || (last && gimple_code (last) != GIMPLE_LABEL))
824 : {
825 39 : *bsi = gsi_last_bb (latch);
826 39 : *insert_after = true;
827 : }
828 : else
829 : {
830 151249 : *bsi = gsi_last_bb (bb);
831 151249 : *insert_after = false;
832 : }
833 151288 : }
834 :
835 : /* Copies phi node arguments for duplicated blocks. The index of the first
836 : duplicated block is FIRST_NEW_BLOCK. */
837 :
838 : static void
839 152050 : copy_phi_node_args (unsigned first_new_block)
840 : {
841 152050 : unsigned i;
842 :
843 1358476 : for (i = first_new_block; i < (unsigned) last_basic_block_for_fn (cfun); i++)
844 1206426 : BASIC_BLOCK_FOR_FN (cfun, i)->flags |= BB_DUPLICATED;
845 :
846 1358476 : for (i = first_new_block; i < (unsigned) last_basic_block_for_fn (cfun); i++)
847 1206426 : add_phi_args_after_copy_bb (BASIC_BLOCK_FOR_FN (cfun, i));
848 :
849 1358476 : for (i = first_new_block; i < (unsigned) last_basic_block_for_fn (cfun); i++)
850 1206426 : BASIC_BLOCK_FOR_FN (cfun, i)->flags &= ~BB_DUPLICATED;
851 152050 : }
852 :
853 :
854 : /* The same as cfgloopmanip.cc:duplicate_loop_body_to_header_edge, but also
855 : updates the PHI nodes at start of the copied region. In order to
856 : achieve this, only loops whose exits all lead to the same location
857 : are handled.
858 :
859 : Notice that we do not completely update the SSA web after
860 : duplication. The caller is responsible for calling update_ssa
861 : after the loop has been duplicated. */
862 :
863 : bool
864 152070 : gimple_duplicate_loop_body_to_header_edge (class loop *loop, edge e,
865 : unsigned int ndupl,
866 : sbitmap wont_exit, edge orig,
867 : vec<edge> *to_remove, int flags)
868 : {
869 152070 : unsigned first_new_block;
870 :
871 152070 : if (!loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
872 : return false;
873 152070 : if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
874 : return false;
875 :
876 152070 : first_new_block = last_basic_block_for_fn (cfun);
877 152070 : if (!duplicate_loop_body_to_header_edge (loop, e, ndupl, wont_exit, orig,
878 : to_remove, flags))
879 : return false;
880 :
881 : /* Readd the removed phi args for e. */
882 152050 : flush_pending_stmts (e);
883 :
884 : /* Copy the phi node arguments. */
885 152050 : copy_phi_node_args (first_new_block);
886 :
887 152050 : scev_reset ();
888 :
889 152050 : return true;
890 : }
891 :
892 : /* Returns true if we can unroll LOOP FACTOR times. Number
893 : of iterations of the loop is returned in NITER. */
894 :
895 : bool
896 871 : can_unroll_loop_p (class loop *loop, unsigned factor,
897 : class tree_niter_desc *niter)
898 : {
899 871 : edge exit;
900 :
901 : /* Check whether unrolling is possible. We only want to unroll loops
902 : for that we are able to determine number of iterations. We also
903 : want to split the extra iterations of the loop from its end,
904 : therefore we require that the loop has precisely one
905 : exit. */
906 :
907 871 : exit = single_dom_exit (loop);
908 871 : if (!exit)
909 : return false;
910 :
911 850 : if (!number_of_iterations_exit (loop, exit, niter, false)
912 849 : || niter->cmp == ERROR_MARK
913 : /* Scalar evolutions analysis might have copy propagated
914 : the abnormal ssa names into these expressions, hence
915 : emitting the computations based on them during loop
916 : unrolling might create overlapping life ranges for
917 : them, and failures in out-of-ssa. */
918 847 : || contains_abnormal_ssa_name_p (niter->may_be_zero)
919 847 : || contains_abnormal_ssa_name_p (niter->control.base)
920 847 : || contains_abnormal_ssa_name_p (niter->control.step)
921 1697 : || contains_abnormal_ssa_name_p (niter->bound))
922 3 : return false;
923 :
924 : /* And of course, we must be able to duplicate the loop. */
925 847 : if (!can_duplicate_loop_p (loop))
926 : return false;
927 :
928 : /* The final loop should be small enough. */
929 847 : if (tree_num_loop_insns (loop, &eni_size_weights) * factor
930 847 : > (unsigned) param_max_unrolled_insns)
931 : return false;
932 :
933 : return true;
934 : }
935 :
936 : /* Determines the conditions that control execution of LOOP unrolled FACTOR
937 : times. DESC is number of iterations of LOOP. ENTER_COND is set to
938 : condition that must be true if the main loop can be entered.
939 : If the loop does not always iterate an exact multiple of FACTOR times,
940 : EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
941 : how the exit from the unrolled loop should be controlled. Otherwise,
942 : the trees are set to null and EXIT_CMP is set to ERROR_MARK. */
943 :
944 : static void
945 831 : determine_exit_conditions (class loop *loop, class tree_niter_desc *desc,
946 : unsigned factor, tree *enter_cond,
947 : tree *exit_base, tree *exit_step,
948 : enum tree_code *exit_cmp, tree *exit_bound)
949 : {
950 831 : gimple_seq stmts;
951 831 : tree base = desc->control.base;
952 831 : tree step = desc->control.step;
953 831 : tree bound = desc->bound;
954 831 : tree type = TREE_TYPE (step);
955 831 : tree bigstep, delta;
956 831 : tree min = lower_bound_in_type (type, type);
957 831 : tree max = upper_bound_in_type (type, type);
958 831 : enum tree_code cmp = desc->cmp;
959 831 : tree cond = boolean_true_node, assum;
960 :
961 : /* For pointers, do the arithmetics in the type of step. */
962 831 : base = fold_convert (type, base);
963 831 : bound = fold_convert (type, bound);
964 :
965 831 : *enter_cond = boolean_false_node;
966 831 : *exit_base = NULL_TREE;
967 831 : *exit_step = NULL_TREE;
968 831 : *exit_cmp = ERROR_MARK;
969 831 : *exit_bound = NULL_TREE;
970 831 : gcc_assert (cmp != ERROR_MARK);
971 :
972 : /* We only need to be correct when we answer question
973 : "Do at least FACTOR more iterations remain?" in the unrolled loop.
974 : Thus, transforming BASE + STEP * i <> BOUND to
975 : BASE + STEP * i < BOUND is ok. */
976 831 : if (cmp == NE_EXPR)
977 : {
978 84 : if (tree_int_cst_sign_bit (step))
979 : cmp = GT_EXPR;
980 : else
981 783 : cmp = LT_EXPR;
982 : }
983 747 : else if (cmp == LT_EXPR)
984 : {
985 745 : gcc_assert (!tree_int_cst_sign_bit (step));
986 : }
987 2 : else if (cmp == GT_EXPR)
988 : {
989 2 : gcc_assert (tree_int_cst_sign_bit (step));
990 : }
991 : else
992 0 : gcc_unreachable ();
993 :
994 : /* The main body of the loop may be entered iff:
995 :
996 : 1) desc->may_be_zero is false.
997 : 2) it is possible to check that there are at least FACTOR iterations
998 : of the loop, i.e., BOUND - step * FACTOR does not overflow.
999 : 3) # of iterations is at least FACTOR */
1000 :
1001 831 : if (!integer_zerop (desc->may_be_zero))
1002 6 : cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
1003 : invert_truthvalue (desc->may_be_zero),
1004 : cond);
1005 :
1006 831 : bigstep = fold_build2 (MULT_EXPR, type, step,
1007 : build_int_cst_type (type, factor));
1008 831 : delta = fold_build2 (MINUS_EXPR, type, bigstep, step);
1009 831 : if (cmp == LT_EXPR)
1010 783 : assum = fold_build2 (GE_EXPR, boolean_type_node,
1011 : bound,
1012 : fold_build2 (PLUS_EXPR, type, min, delta));
1013 : else
1014 48 : assum = fold_build2 (LE_EXPR, boolean_type_node,
1015 : bound,
1016 : fold_build2 (PLUS_EXPR, type, max, delta));
1017 831 : cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
1018 :
1019 831 : bound = fold_build2 (MINUS_EXPR, type, bound, delta);
1020 831 : assum = fold_build2 (cmp, boolean_type_node, base, bound);
1021 831 : cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
1022 :
1023 831 : if (integer_nonzerop (cond)
1024 831 : && integer_zerop (desc->may_be_zero))
1025 : {
1026 : /* Convert the latch count to an iteration count. */
1027 77 : tree niter = fold_build2 (PLUS_EXPR, type, desc->niter,
1028 : build_one_cst (type));
1029 77 : if (multiple_of_p (type, niter, build_int_cst (type, factor)))
1030 30 : return;
1031 : }
1032 :
1033 801 : cond = force_gimple_operand (unshare_expr (cond), &stmts, false, NULL_TREE);
1034 801 : if (stmts)
1035 17 : gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
1036 : /* cond now may be a gimple comparison, which would be OK, but also any
1037 : other gimple rhs (say a && b). In this case we need to force it to
1038 : operand. */
1039 801 : if (!is_gimple_condexpr_for_cond (cond))
1040 : {
1041 14 : cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
1042 14 : if (stmts)
1043 14 : gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
1044 : }
1045 801 : *enter_cond = cond;
1046 :
1047 801 : base = force_gimple_operand (unshare_expr (base), &stmts, true, NULL_TREE);
1048 801 : if (stmts)
1049 29 : gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
1050 801 : bound = force_gimple_operand (unshare_expr (bound), &stmts, true, NULL_TREE);
1051 801 : if (stmts)
1052 721 : gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
1053 :
1054 801 : *exit_base = base;
1055 801 : *exit_step = bigstep;
1056 801 : *exit_cmp = cmp;
1057 801 : *exit_bound = bound;
1058 : }
1059 :
1060 : /* Unroll LOOP FACTOR times. LOOP is known to have a single exit edge
1061 : whose source block dominates the latch. DESC describes the number of
1062 : iterations of LOOP.
1063 :
1064 : If N is number of iterations of the loop and MAY_BE_ZERO is the condition
1065 : under that loop exits in the first iteration even if N != 0,
1066 :
1067 : while (1)
1068 : {
1069 : x = phi (init, next);
1070 :
1071 : pre;
1072 : if (st)
1073 : break;
1074 : post;
1075 : }
1076 :
1077 : becomes (with possibly the exit conditions formulated a bit differently,
1078 : avoiding the need to create a new iv):
1079 :
1080 : if (MAY_BE_ZERO || N < FACTOR)
1081 : goto rest;
1082 :
1083 : do
1084 : {
1085 : x = phi (init, next);
1086 :
1087 : pre;
1088 : post;
1089 : pre;
1090 : post;
1091 : ...
1092 : pre;
1093 : post;
1094 : N -= FACTOR;
1095 :
1096 : } while (N >= FACTOR);
1097 :
1098 : rest:
1099 : init' = phi (init, x);
1100 :
1101 : while (1)
1102 : {
1103 : x = phi (init', next);
1104 :
1105 : pre;
1106 : if (st)
1107 : break;
1108 : post;
1109 : }
1110 :
1111 : Before the loop is unrolled, TRANSFORM is called for it (only for the
1112 : unrolled loop, but not for its versioned copy). DATA is passed to
1113 : TRANSFORM. */
1114 :
1115 : /* Probability in % that the unrolled loop is entered. Just a guess. */
1116 : #define PROB_UNROLLED_LOOP_ENTERED 90
1117 :
1118 : void
1119 831 : tree_transform_and_unroll_loop (class loop *loop, unsigned factor,
1120 : class tree_niter_desc *desc,
1121 : transform_callback transform,
1122 : void *data)
1123 : {
1124 831 : unsigned irr = loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP;
1125 :
1126 831 : enum tree_code exit_cmp;
1127 831 : tree enter_main_cond, exit_base, exit_step, exit_bound;
1128 831 : bool flat = maybe_flat_loop_profile (loop);
1129 831 : determine_exit_conditions (loop, desc, factor,
1130 : &enter_main_cond, &exit_base, &exit_step,
1131 : &exit_cmp, &exit_bound);
1132 831 : bool single_loop_p = !exit_base;
1133 :
1134 831 : gcond *exit_if = nullptr;
1135 831 : class loop *new_loop = nullptr;
1136 831 : edge new_exit;
1137 831 : if (!single_loop_p)
1138 : {
1139 801 : profile_count entry_count = loop_preheader_edge (loop)->src->count;
1140 : /* Let us assume that the unrolled loop is quite likely to be entered. */
1141 801 : profile_probability prob_entry;
1142 801 : if (integer_nonzerop (enter_main_cond))
1143 47 : prob_entry = profile_probability::always ();
1144 : else
1145 754 : prob_entry = profile_probability::guessed_always ()
1146 754 : .apply_scale (PROB_UNROLLED_LOOP_ENTERED, 100);
1147 :
1148 :
1149 : /* The values for scales should keep profile consistent, and somewhat
1150 : close to correct. */
1151 801 : new_loop = loop_version (loop, enter_main_cond, NULL, prob_entry,
1152 : prob_entry.invert (),
1153 : prob_entry,
1154 : /* We will later redirect exit from vectorized
1155 : loop to new_loop. */
1156 : profile_probability::always (),
1157 : true);
1158 801 : gcc_assert (new_loop != NULL);
1159 801 : update_ssa (TODO_update_ssa_no_phi);
1160 :
1161 : /* Prepare the cfg and update the phi nodes. Move the loop exit to the
1162 : loop latch (and make its condition dummy, for the moment). */
1163 801 : basic_block rest = loop_preheader_edge (new_loop)->src;
1164 801 : edge precond_edge = single_pred_edge (rest);
1165 801 : split_edge (loop_latch_edge (loop));
1166 801 : basic_block exit_bb = single_pred (loop->latch);
1167 801 : edge exit = single_dom_exit (loop);
1168 :
1169 : /* Since the exit edge will be removed, the frequency of all the blocks
1170 : in the loop that are dominated by it must be scaled. */
1171 801 : if (exit->probability.initialized_p ())
1172 801 : scale_dominated_blocks_in_loop (loop, exit->src,
1173 : /* We are scaling up here so
1174 : probability does not fit. */
1175 801 : exit->src->count,
1176 1602 : exit->src->count - exit->count ());
1177 :
1178 801 : gimple_stmt_iterator bsi = gsi_last_bb (exit_bb);
1179 801 : exit_if = gimple_build_cond (EQ_EXPR, integer_zero_node,
1180 : integer_zero_node,
1181 : NULL_TREE, NULL_TREE);
1182 :
1183 801 : gsi_insert_after (&bsi, exit_if, GSI_NEW_STMT);
1184 801 : new_exit = make_edge (exit_bb, rest, EDGE_FALSE_VALUE | irr);
1185 801 : rescan_loop_exit (new_exit, true, false);
1186 :
1187 : /* Set the probability of new exit to the same of the old one. Fix
1188 : the count of the latch block. */
1189 801 : new_exit->probability = exit->probability;
1190 801 : edge new_nonexit = single_pred_edge (loop->latch);
1191 801 : new_nonexit->probability = exit->probability.invert ();
1192 801 : new_nonexit->flags = EDGE_TRUE_VALUE;
1193 801 : set_edge_probability_and_rescale_others
1194 801 : (exit, profile_probability::never ());
1195 801 : loop->latch->count = new_nonexit->count ();
1196 :
1197 801 : edge old_entry = loop_preheader_edge (loop);
1198 801 : edge new_entry = loop_preheader_edge (new_loop);
1199 801 : edge old_latch = loop_latch_edge (loop);
1200 801 : for (gphi_iterator psi_old_loop = gsi_start_phis (loop->header),
1201 801 : psi_new_loop = gsi_start_phis (new_loop->header);
1202 2496 : !gsi_end_p (psi_old_loop);
1203 1695 : gsi_next (&psi_old_loop), gsi_next (&psi_new_loop))
1204 : {
1205 1695 : gphi *phi_old_loop = psi_old_loop.phi ();
1206 1695 : gphi *phi_new_loop = psi_new_loop.phi ();
1207 :
1208 1695 : tree init = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_entry);
1209 1695 : use_operand_p op
1210 1695 : = PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop, new_entry);
1211 1695 : gcc_assert (operand_equal_for_phi_arg_p (init, USE_FROM_PTR (op)));
1212 1695 : tree next = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_latch);
1213 :
1214 : /* Prefer using original variable as a base for the new ssa name.
1215 : This is necessary for virtual ops, and useful in order to avoid
1216 : losing debug info for real ops. */
1217 1695 : tree new_init;
1218 1695 : if (TREE_CODE (next) == SSA_NAME
1219 3390 : && useless_type_conversion_p (TREE_TYPE (next),
1220 1695 : TREE_TYPE (init)))
1221 1695 : new_init = copy_ssa_name (next);
1222 0 : else if (TREE_CODE (init) == SSA_NAME
1223 0 : && useless_type_conversion_p (TREE_TYPE (init),
1224 0 : TREE_TYPE (next)))
1225 0 : new_init = copy_ssa_name (init);
1226 0 : else if (useless_type_conversion_p (TREE_TYPE (next),
1227 0 : TREE_TYPE (init)))
1228 0 : new_init = make_temp_ssa_name (TREE_TYPE (next), NULL,
1229 : "unrinittmp");
1230 : else
1231 0 : new_init = make_temp_ssa_name (TREE_TYPE (init), NULL,
1232 : "unrinittmp");
1233 :
1234 1695 : gphi *phi_rest = create_phi_node (new_init, rest);
1235 1695 : add_phi_arg (phi_rest, init, precond_edge, UNKNOWN_LOCATION);
1236 1695 : add_phi_arg (phi_rest, next, new_exit, UNKNOWN_LOCATION);
1237 1695 : SET_USE (op, new_init);
1238 : }
1239 :
1240 801 : remove_path (exit);
1241 : /* We will later redirect exit from vectorized loop to new_loop. */
1242 801 : loop_preheader_edge (new_loop)->src->count = entry_count;
1243 :
1244 : /* The epilog loop latch executes at most factor - 1 times.
1245 : Since the epilog is entered unconditionally it will need to handle
1246 : up to factor executions of its body. */
1247 801 : new_loop->any_upper_bound = true;
1248 801 : new_loop->nb_iterations_upper_bound = factor - 1;
1249 : /* We do not really know estimate on number of iterations, since we do not
1250 : track any estimates modulo unroll factor.
1251 : Drop estimate from loop_info and scale loop profile.
1252 : It may be more realistic to scale loop profile to factor / 2 - 1,
1253 : but vectorizer also uses factor - 1. */
1254 801 : new_loop->any_estimate = false;
1255 801 : scale_loop_profile (new_loop, profile_probability::always (), factor - 1);
1256 : }
1257 : else
1258 30 : new_exit = single_dom_exit (loop);
1259 :
1260 : /* Transform the loop. */
1261 831 : if (transform)
1262 740 : (*transform) (loop, data);
1263 :
1264 : /* Unroll the loop and remove the exits in all iterations except for the
1265 : last one. */
1266 831 : auto_sbitmap wont_exit (factor);
1267 831 : bitmap_ones (wont_exit);
1268 831 : bitmap_clear_bit (wont_exit, factor - 1);
1269 :
1270 831 : auto_vec<edge> to_remove;
1271 831 : bool ok
1272 : = gimple_duplicate_loop_body_to_header_edge
1273 903 : (loop, loop_latch_edge (loop), factor - 1, wont_exit,
1274 : new_exit, &to_remove,
1275 : DLTHE_FLAG_UPDATE_FREQ | (flat ? DLTHE_FLAG_FLAT_PROFILE : 0));
1276 831 : gcc_assert (ok);
1277 :
1278 3579 : for (edge e : to_remove)
1279 : {
1280 1086 : ok = remove_path (e);
1281 1086 : gcc_assert (ok);
1282 : }
1283 831 : update_ssa (TODO_update_ssa);
1284 :
1285 831 : new_exit = single_dom_exit (loop);
1286 : /* gimple_duplicate_loop_body_to_header_edge depending on
1287 : DLTHE_FLAG_UPDATE_FREQ either keeps original frequency of the loop header
1288 : or scales it down accordingly.
1289 : However exit edge probability is kept as original. Fix it if needed
1290 : and compensate. */
1291 831 : update_loop_exit_probability_scale_dom_bbs (loop, new_exit);
1292 831 : if (!single_loop_p)
1293 : {
1294 : /* Finally create the new counter for number of iterations and add
1295 : the new exit instruction. */
1296 801 : tree ctr_before, ctr_after;
1297 801 : gimple_stmt_iterator bsi = gsi_last_nondebug_bb (new_exit->src);
1298 801 : exit_if = as_a <gcond *> (gsi_stmt (bsi));
1299 801 : create_iv (exit_base, PLUS_EXPR, exit_step, NULL_TREE, loop,
1300 : &bsi, false, &ctr_before, &ctr_after);
1301 801 : gimple_cond_set_code (exit_if, exit_cmp);
1302 801 : gimple_cond_set_lhs (exit_if, ctr_after);
1303 801 : gimple_cond_set_rhs (exit_if, exit_bound);
1304 801 : update_stmt (exit_if);
1305 : }
1306 831 : if (loop->any_upper_bound)
1307 1640 : loop->nb_iterations_upper_bound = wi::udiv_floor
1308 820 : (loop->nb_iterations_upper_bound + 1, factor) - 1;
1309 831 : if (loop->any_likely_upper_bound)
1310 1640 : loop->nb_iterations_likely_upper_bound = wi::udiv_floor
1311 820 : (loop->nb_iterations_likely_upper_bound + 1, factor) - 1;
1312 831 : if (loop->any_estimate)
1313 172 : loop->nb_iterations_estimate = wi::udiv_floor
1314 86 : (loop->nb_iterations_estimate + 1, factor) - 1;
1315 :
1316 831 : checking_verify_flow_info ();
1317 831 : checking_verify_loop_structure ();
1318 831 : checking_verify_loop_closed_ssa (true, loop);
1319 831 : if (new_loop)
1320 1632 : checking_verify_loop_closed_ssa (true, new_loop);
1321 831 : }
1322 :
1323 : /* Wrapper over tree_transform_and_unroll_loop for case we do not
1324 : want to transform the loop before unrolling. The meaning
1325 : of the arguments is the same as for tree_transform_and_unroll_loop. */
1326 :
1327 : void
1328 91 : tree_unroll_loop (class loop *loop, unsigned factor,
1329 : class tree_niter_desc *desc)
1330 : {
1331 91 : tree_transform_and_unroll_loop (loop, factor, desc, NULL, NULL);
1332 91 : }
1333 :
1334 : /* Rewrite the phi node at position PSI in function of the main
1335 : induction variable MAIN_IV and insert the generated code at GSI. */
1336 :
1337 : static void
1338 2038 : rewrite_phi_with_iv (loop_p loop,
1339 : gphi_iterator *psi,
1340 : gimple_stmt_iterator *gsi,
1341 : tree main_iv)
1342 : {
1343 2038 : affine_iv iv;
1344 2038 : gassign *stmt;
1345 2038 : gphi *phi = psi->phi ();
1346 2038 : tree atype, mtype, val, res = PHI_RESULT (phi);
1347 :
1348 4076 : if (virtual_operand_p (res) || res == main_iv)
1349 : {
1350 1160 : gsi_next (psi);
1351 1243 : return;
1352 : }
1353 :
1354 878 : if (!simple_iv (loop, loop, res, &iv, true))
1355 : {
1356 83 : gsi_next (psi);
1357 83 : return;
1358 : }
1359 :
1360 795 : remove_phi_node (psi, false);
1361 :
1362 795 : atype = TREE_TYPE (res);
1363 795 : mtype = POINTER_TYPE_P (atype) ? sizetype : atype;
1364 795 : val = fold_build2 (MULT_EXPR, mtype, unshare_expr (iv.step),
1365 : fold_convert (mtype, main_iv));
1366 1588 : val = fold_build2 (POINTER_TYPE_P (atype)
1367 : ? POINTER_PLUS_EXPR : PLUS_EXPR,
1368 : atype, unshare_expr (iv.base), val);
1369 795 : val = force_gimple_operand_gsi (gsi, val, false, NULL_TREE, true,
1370 : GSI_SAME_STMT);
1371 795 : stmt = gimple_build_assign (res, val);
1372 795 : gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
1373 : }
1374 :
1375 : /* Rewrite all the phi nodes of LOOP in function of the main induction
1376 : variable MAIN_IV. */
1377 :
1378 : static void
1379 583 : rewrite_all_phi_nodes_with_iv (loop_p loop, tree main_iv)
1380 : {
1381 583 : unsigned i;
1382 583 : basic_block *bbs = get_loop_body_in_dom_order (loop);
1383 583 : gphi_iterator psi;
1384 :
1385 2065 : for (i = 0; i < loop->num_nodes; i++)
1386 : {
1387 1482 : basic_block bb = bbs[i];
1388 1482 : gimple_stmt_iterator gsi = gsi_after_labels (bb);
1389 :
1390 1482 : if (bb->loop_father != loop)
1391 97 : continue;
1392 :
1393 3423 : for (psi = gsi_start_phis (bb); !gsi_end_p (psi); )
1394 2038 : rewrite_phi_with_iv (loop, &psi, &gsi, main_iv);
1395 : }
1396 :
1397 583 : free (bbs);
1398 583 : }
1399 :
1400 : /* Bases all the induction variables in LOOP on a single induction variable
1401 : (with base 0 and step 1), whose final value is compared with *NIT. When the
1402 : IV type precision has to be larger than *NIT type precision, *NIT is
1403 : converted to the larger type, the conversion code is inserted before the
1404 : loop, and *NIT is updated to the new definition. When BUMP_IN_LATCH is true,
1405 : the induction variable is incremented in the loop latch, otherwise it is
1406 : incremented in the loop header. Return the induction variable that was
1407 : created. */
1408 :
1409 : tree
1410 583 : canonicalize_loop_ivs (class loop *loop, tree *nit, bool bump_in_latch)
1411 : {
1412 583 : unsigned precision = TYPE_PRECISION (TREE_TYPE (*nit));
1413 583 : unsigned original_precision = precision;
1414 583 : tree type, var_before;
1415 583 : gimple_stmt_iterator gsi;
1416 583 : gphi_iterator psi;
1417 583 : gcond *stmt;
1418 583 : edge exit = single_dom_exit (loop);
1419 583 : gimple_seq stmts;
1420 583 : bool unsigned_p = false;
1421 :
1422 583 : for (psi = gsi_start_phis (loop->header);
1423 1934 : !gsi_end_p (psi); gsi_next (&psi))
1424 : {
1425 1351 : gphi *phi = psi.phi ();
1426 1351 : tree res = PHI_RESULT (phi);
1427 1351 : bool uns;
1428 :
1429 1351 : type = TREE_TYPE (res);
1430 1351 : if (virtual_operand_p (res)
1431 857 : || (!INTEGRAL_TYPE_P (type)
1432 21 : && !POINTER_TYPE_P (type))
1433 2193 : || TYPE_PRECISION (type) < precision)
1434 531 : continue;
1435 :
1436 820 : uns = POINTER_TYPE_P (type) | TYPE_UNSIGNED (type);
1437 :
1438 820 : if (TYPE_PRECISION (type) > precision)
1439 : unsigned_p = uns;
1440 : else
1441 815 : unsigned_p |= uns;
1442 :
1443 820 : precision = TYPE_PRECISION (type);
1444 : }
1445 :
1446 583 : scalar_int_mode mode = smallest_int_mode_for_size (precision).require ();
1447 583 : precision = GET_MODE_PRECISION (mode);
1448 583 : type = build_nonstandard_integer_type (precision, unsigned_p);
1449 :
1450 583 : if (original_precision != precision
1451 583 : || TYPE_UNSIGNED (TREE_TYPE (*nit)) != unsigned_p)
1452 : {
1453 301 : *nit = fold_convert (type, *nit);
1454 301 : *nit = force_gimple_operand (*nit, &stmts, true, NULL_TREE);
1455 301 : if (stmts)
1456 115 : gsi_insert_seq_on_edge_immediate (loop_preheader_edge (loop), stmts);
1457 : }
1458 :
1459 583 : if (bump_in_latch)
1460 1166 : gsi = gsi_last_bb (loop->latch);
1461 : else
1462 0 : gsi = gsi_last_nondebug_bb (loop->header);
1463 583 : create_iv (build_int_cst_type (type, 0), PLUS_EXPR, build_int_cst (type, 1),
1464 : NULL_TREE, loop, &gsi, bump_in_latch, &var_before, NULL);
1465 :
1466 583 : rewrite_all_phi_nodes_with_iv (loop, var_before);
1467 :
1468 1166 : stmt = as_a <gcond *> (*gsi_last_bb (exit->src));
1469 : /* Make the loop exit if the control condition is not satisfied. */
1470 583 : if (exit->flags & EDGE_TRUE_VALUE)
1471 : {
1472 100 : edge te, fe;
1473 :
1474 100 : extract_true_false_edges_from_block (exit->src, &te, &fe);
1475 100 : te->flags = EDGE_FALSE_VALUE;
1476 100 : fe->flags = EDGE_TRUE_VALUE;
1477 : }
1478 583 : gimple_cond_set_code (stmt, LT_EXPR);
1479 583 : gimple_cond_set_lhs (stmt, var_before);
1480 583 : gimple_cond_set_rhs (stmt, *nit);
1481 583 : update_stmt (stmt);
1482 :
1483 583 : return var_before;
1484 : }
|