]> gcc.gnu.org Git - gcc.git/blame - gcc/tree-ssa-loop-manip.c
c-common.h (enum rid): Add RID_CXX_COMPAT_WARN.
[gcc.git] / gcc / tree-ssa-loop-manip.c
CommitLineData
c913f08a 1/* High-level loop manipulation functions.
6ac01510 2 Copyright (C) 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
c913f08a
ZD
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
9dcd6f09 8Free Software Foundation; either version 3, or (at your option) any
c913f08a
ZD
9later version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT
12ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
c913f08a
ZD
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
23#include "tm.h"
24#include "tree.h"
25#include "rtl.h"
26#include "tm_p.h"
27#include "hard-reg-set.h"
28#include "basic-block.h"
29#include "output.h"
30#include "diagnostic.h"
31#include "tree-flow.h"
32#include "tree-dump.h"
33#include "timevar.h"
34#include "cfgloop.h"
35#include "tree-pass.h"
36#include "cfglayout.h"
37#include "tree-scalar-evolution.h"
17684618 38#include "params.h"
7f9bc51b 39#include "tree-inline.h"
c913f08a 40
82b85a85
ZD
41/* Creates an induction variable with value BASE + STEP * iteration in LOOP.
42 It is expected that neither BASE nor STEP are shared with other expressions
43 (unless the sharing rules allow this). Use VAR as a base var_decl for it
44 (if NULL, a new temporary will be created). The increment will occur at
92d2b330
SP
45 INCR_POS (after it if AFTER is true, before it otherwise). INCR_POS and
46 AFTER can be computed using standard_iv_increment_position. The ssa versions
82b85a85
ZD
47 of the variable before and after increment will be stored in VAR_BEFORE and
48 VAR_AFTER (unless they are NULL). */
49
50void
51create_iv (tree base, tree step, tree var, struct loop *loop,
52 block_stmt_iterator *incr_pos, bool after,
53 tree *var_before, tree *var_after)
54{
8b11a64c 55 tree stmt, initial, step1, stmts;
82b85a85
ZD
56 tree vb, va;
57 enum tree_code incr_op = PLUS_EXPR;
9be872b7 58 edge pe = loop_preheader_edge (loop);
82b85a85
ZD
59
60 if (!var)
61 {
62 var = create_tmp_var (TREE_TYPE (base), "ivtmp");
f004ab02 63 add_referenced_var (var);
82b85a85
ZD
64 }
65
66 vb = make_ssa_name (var, NULL_TREE);
67 if (var_before)
68 *var_before = vb;
69 va = make_ssa_name (var, NULL_TREE);
70 if (var_after)
71 *var_after = va;
72
73 /* For easier readability of the created code, produce MINUS_EXPRs
74 when suitable. */
75 if (TREE_CODE (step) == INTEGER_CST)
76 {
77 if (TYPE_UNSIGNED (TREE_TYPE (step)))
78 {
987b67bc 79 step1 = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
82b85a85
ZD
80 if (tree_int_cst_lt (step1, step))
81 {
82 incr_op = MINUS_EXPR;
83 step = step1;
84 }
85 }
86 else
87 {
6ac01510
ILT
88 bool ovf;
89
90 if (!tree_expr_nonnegative_warnv_p (step, &ovf)
82b85a85
ZD
91 && may_negate_without_overflow_p (step))
92 {
93 incr_op = MINUS_EXPR;
987b67bc 94 step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
82b85a85
ZD
95 }
96 }
97 }
5be014d5
AP
98 if (POINTER_TYPE_P (TREE_TYPE (base)))
99 {
100 step = fold_convert (sizetype, step);
101 if (incr_op == MINUS_EXPR)
102 step = fold_build1 (NEGATE_EXPR, sizetype, step);
103 incr_op = POINTER_PLUS_EXPR;
104 }
9be872b7
ZD
105 /* Gimplify the step if necessary. We put the computations in front of the
106 loop (i.e. the step should be loop invariant). */
1ffe34d9 107 step = force_gimple_operand (step, &stmts, true, NULL_TREE);
9be872b7 108 if (stmts)
598ec7bd 109 bsi_insert_on_edge_immediate (pe, stmts);
9be872b7 110
939409af
RS
111 stmt = build_gimple_modify_stmt (va,
112 build2 (incr_op, TREE_TYPE (base),
113 vb, step));
82b85a85
ZD
114 SSA_NAME_DEF_STMT (va) = stmt;
115 if (after)
116 bsi_insert_after (incr_pos, stmt, BSI_NEW_STMT);
117 else
118 bsi_insert_before (incr_pos, stmt, BSI_NEW_STMT);
119
8b11a64c
ZD
120 initial = force_gimple_operand (base, &stmts, true, var);
121 if (stmts)
598ec7bd 122 bsi_insert_on_edge_immediate (pe, stmts);
82b85a85
ZD
123
124 stmt = create_phi_node (vb, loop->header);
125 SSA_NAME_DEF_STMT (vb) = stmt;
d2e398df
KH
126 add_phi_arg (stmt, initial, loop_preheader_edge (loop));
127 add_phi_arg (stmt, va, loop_latch_edge (loop));
82b85a85
ZD
128}
129
c913f08a
ZD
130/* Add exit phis for the USE on EXIT. */
131
132static void
133add_exit_phis_edge (basic_block exit, tree use)
134{
135 tree phi, def_stmt = SSA_NAME_DEF_STMT (use);
136 basic_block def_bb = bb_for_stmt (def_stmt);
137 struct loop *def_loop;
138 edge e;
628f6a4e 139 edge_iterator ei;
c913f08a
ZD
140
141 /* Check that some of the edges entering the EXIT block exits a loop in
142 that USE is defined. */
628f6a4e 143 FOR_EACH_EDGE (e, ei, exit->preds)
c913f08a
ZD
144 {
145 def_loop = find_common_loop (def_bb->loop_father, e->src->loop_father);
146 if (!flow_bb_inside_loop_p (def_loop, e->dest))
147 break;
148 }
149
150 if (!e)
151 return;
152
153 phi = create_phi_node (use, exit);
84d65814 154 create_new_def_for (PHI_RESULT (phi), phi, PHI_RESULT_PTR (phi));
628f6a4e 155 FOR_EACH_EDGE (e, ei, exit->preds)
d2e398df 156 add_phi_arg (phi, use, e);
c913f08a
ZD
157}
158
159/* Add exit phis for VAR that is used in LIVEIN.
160 Exits of the loops are stored in EXITS. */
161
162static void
163add_exit_phis_var (tree var, bitmap livein, bitmap exits)
164{
165 bitmap def;
3cd8c58a 166 unsigned index;
c913f08a 167 basic_block def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
87c476a2 168 bitmap_iterator bi;
c913f08a 169
0bca51f0
DN
170 if (is_gimple_reg (var))
171 bitmap_clear_bit (livein, def_bb->index);
172 else
173 bitmap_set_bit (livein, def_bb->index);
c913f08a 174
8bdbfff5 175 def = BITMAP_ALLOC (NULL);
c913f08a
ZD
176 bitmap_set_bit (def, def_bb->index);
177 compute_global_livein (livein, def);
8bdbfff5 178 BITMAP_FREE (def);
c913f08a 179
87c476a2
ZD
180 EXECUTE_IF_AND_IN_BITMAP (exits, livein, 0, index, bi)
181 {
182 add_exit_phis_edge (BASIC_BLOCK (index), var);
183 }
c913f08a
ZD
184}
185
186/* Add exit phis for the names marked in NAMES_TO_RENAME.
187 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
188 names are used are stored in USE_BLOCKS. */
189
190static void
191add_exit_phis (bitmap names_to_rename, bitmap *use_blocks, bitmap loop_exits)
192{
193 unsigned i;
87c476a2 194 bitmap_iterator bi;
c913f08a 195
87c476a2 196 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
c913f08a
ZD
197 {
198 add_exit_phis_var (ssa_name (i), use_blocks[i], loop_exits);
87c476a2 199 }
c913f08a
ZD
200}
201
202/* Returns a bitmap of all loop exit edge targets. */
203
204static bitmap
205get_loops_exits (void)
206{
8bdbfff5 207 bitmap exits = BITMAP_ALLOC (NULL);
c913f08a
ZD
208 basic_block bb;
209 edge e;
628f6a4e 210 edge_iterator ei;
c913f08a
ZD
211
212 FOR_EACH_BB (bb)
213 {
628f6a4e 214 FOR_EACH_EDGE (e, ei, bb->preds)
c913f08a
ZD
215 if (e->src != ENTRY_BLOCK_PTR
216 && !flow_bb_inside_loop_p (e->src->loop_father, bb))
217 {
218 bitmap_set_bit (exits, bb->index);
219 break;
220 }
221 }
222
223 return exits;
224}
225
226/* For USE in BB, if it is used outside of the loop it is defined in,
227 mark it for rewrite. Record basic block BB where it is used
84d65814 228 to USE_BLOCKS. Record the ssa name index to NEED_PHIS bitmap. */
c913f08a
ZD
229
230static void
84d65814
DN
231find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks,
232 bitmap need_phis)
c913f08a
ZD
233{
234 unsigned ver;
235 basic_block def_bb;
236 struct loop *def_loop;
237
238 if (TREE_CODE (use) != SSA_NAME)
239 return;
240
84d65814
DN
241 /* We don't need to keep virtual operands in loop-closed form. */
242 if (!is_gimple_reg (use))
243 return;
244
c913f08a
ZD
245 ver = SSA_NAME_VERSION (use);
246 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use));
247 if (!def_bb)
248 return;
249 def_loop = def_bb->loop_father;
250
d6e840ee 251 /* If the definition is not inside a loop, it is not interesting. */
9ba025a2 252 if (!loop_outer (def_loop))
c913f08a
ZD
253 return;
254
d6e840ee
RG
255 /* If the use is not outside of the loop it is defined in, it is not
256 interesting. */
257 if (flow_bb_inside_loop_p (def_loop, bb))
258 return;
259
c913f08a 260 if (!use_blocks[ver])
8bdbfff5 261 use_blocks[ver] = BITMAP_ALLOC (NULL);
c913f08a
ZD
262 bitmap_set_bit (use_blocks[ver], bb->index);
263
84d65814 264 bitmap_set_bit (need_phis, ver);
c913f08a
ZD
265}
266
267/* For uses in STMT, mark names that are used outside of the loop they are
268 defined to rewrite. Record the set of blocks in that the ssa
84d65814
DN
269 names are defined to USE_BLOCKS and the ssa names themselves to
270 NEED_PHIS. */
c913f08a
ZD
271
272static void
84d65814 273find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks, bitmap need_phis)
c913f08a 274{
4c124b4c
AM
275 ssa_op_iter iter;
276 tree var;
c913f08a
ZD
277 basic_block bb = bb_for_stmt (stmt);
278
38635499 279 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
84d65814 280 find_uses_to_rename_use (bb, var, use_blocks, need_phis);
c913f08a
ZD
281}
282
2b271002
ZD
283/* Marks names that are used in BB and outside of the loop they are
284 defined in for rewrite. Records the set of blocks in that the ssa
84d65814
DN
285 names are defined to USE_BLOCKS. Record the SSA names that will
286 need exit PHIs in NEED_PHIS. */
c913f08a
ZD
287
288static void
84d65814 289find_uses_to_rename_bb (basic_block bb, bitmap *use_blocks, bitmap need_phis)
c913f08a 290{
c913f08a 291 block_stmt_iterator bsi;
2b271002
ZD
292 edge e;
293 edge_iterator ei;
c913f08a 294 tree phi;
c913f08a 295
2b271002
ZD
296 FOR_EACH_EDGE (e, ei, bb->succs)
297 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
298 find_uses_to_rename_use (bb, PHI_ARG_DEF_FROM_EDGE (phi, e),
84d65814 299 use_blocks, need_phis);
2b271002
ZD
300
301 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
84d65814 302 find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks, need_phis);
2b271002
ZD
303}
304
305/* Marks names that are used outside of the loop they are defined in
306 for rewrite. Records the set of blocks in that the ssa
307 names are defined to USE_BLOCKS. If CHANGED_BBS is not NULL,
308 scan only blocks in this set. */
309
310static void
84d65814 311find_uses_to_rename (bitmap changed_bbs, bitmap *use_blocks, bitmap need_phis)
2b271002
ZD
312{
313 basic_block bb;
314 unsigned index;
315 bitmap_iterator bi;
c913f08a 316
84d65814 317 if (changed_bbs && !bitmap_empty_p (changed_bbs))
2b271002
ZD
318 {
319 EXECUTE_IF_SET_IN_BITMAP (changed_bbs, 0, index, bi)
320 {
84d65814 321 find_uses_to_rename_bb (BASIC_BLOCK (index), use_blocks, need_phis);
2b271002
ZD
322 }
323 }
324 else
325 {
326 FOR_EACH_BB (bb)
327 {
84d65814 328 find_uses_to_rename_bb (bb, use_blocks, need_phis);
2b271002 329 }
c913f08a
ZD
330 }
331}
332
333/* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
334 phi nodes to ensure that no variable is used outside the loop it is
335 defined in.
336
337 This strengthening of the basic ssa form has several advantages:
338
339 1) Updating it during unrolling/peeling/versioning is trivial, since
340 we do not need to care about the uses outside of the loop.
341 2) The behavior of all uses of an induction variable is the same.
342 Without this, you need to distinguish the case when the variable
343 is used outside of the loop it is defined in, for example
344
345 for (i = 0; i < 100; i++)
346 {
347 for (j = 0; j < 100; j++)
348 {
349 k = i + j;
350 use1 (k);
351 }
352 use2 (k);
353 }
354
355 Looking from the outer loop with the normal SSA form, the first use of k
356 is not well-behaved, while the second one is an induction variable with
2b271002
ZD
357 base 99 and step 1.
358
359 If CHANGED_BBS is not NULL, we look for uses outside loops only in
84d65814
DN
360 the basic blocks in this set.
361
362 UPDATE_FLAG is used in the call to update_ssa. See
363 TODO_update_ssa* for documentation. */
c913f08a
ZD
364
365void
84d65814 366rewrite_into_loop_closed_ssa (bitmap changed_bbs, unsigned update_flag)
c913f08a 367{
c7b852c8 368 bitmap loop_exits;
c913f08a 369 bitmap *use_blocks;
84d65814 370 unsigned i, old_num_ssa_names;
c7b852c8
ZD
371 bitmap names_to_rename;
372
f87000d0 373 loops_state_set (LOOP_CLOSED_SSA);
d51157de 374 if (number_of_loops () <= 1)
c7b852c8
ZD
375 return;
376
377 loop_exits = get_loops_exits ();
378 names_to_rename = BITMAP_ALLOC (NULL);
c913f08a 379
84d65814
DN
380 /* If the pass has caused the SSA form to be out-of-date, update it
381 now. */
382 update_ssa (update_flag);
c913f08a 383
84d65814 384 old_num_ssa_names = num_ssa_names;
5ed6ace5 385 use_blocks = XCNEWVEC (bitmap, old_num_ssa_names);
c913f08a
ZD
386
387 /* Find the uses outside loops. */
84d65814 388 find_uses_to_rename (changed_bbs, use_blocks, names_to_rename);
2b271002 389
84d65814 390 /* Add the PHI nodes on exits of the loops for the names we need to
c913f08a 391 rewrite. */
c913f08a
ZD
392 add_exit_phis (names_to_rename, use_blocks, loop_exits);
393
84d65814 394 for (i = 0; i < old_num_ssa_names; i++)
8bdbfff5 395 BITMAP_FREE (use_blocks[i]);
c913f08a 396 free (use_blocks);
8bdbfff5
NS
397 BITMAP_FREE (loop_exits);
398 BITMAP_FREE (names_to_rename);
c913f08a 399
84d65814
DN
400 /* Fix up all the names found to be used outside their original
401 loops. */
402 update_ssa (TODO_update_ssa);
c913f08a
ZD
403}
404
405/* Check invariants of the loop closed ssa form for the USE in BB. */
406
407static void
408check_loop_closed_ssa_use (basic_block bb, tree use)
409{
410 tree def;
411 basic_block def_bb;
412
84d65814 413 if (TREE_CODE (use) != SSA_NAME || !is_gimple_reg (use))
c913f08a
ZD
414 return;
415
416 def = SSA_NAME_DEF_STMT (use);
417 def_bb = bb_for_stmt (def);
1e128c5f
GB
418 gcc_assert (!def_bb
419 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
c913f08a
ZD
420}
421
422/* Checks invariants of loop closed ssa form in statement STMT in BB. */
423
424static void
425check_loop_closed_ssa_stmt (basic_block bb, tree stmt)
426{
4c124b4c
AM
427 ssa_op_iter iter;
428 tree var;
c913f08a 429
38635499 430 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
4c124b4c 431 check_loop_closed_ssa_use (bb, var);
c913f08a
ZD
432}
433
434/* Checks that invariants of the loop closed ssa form are preserved. */
435
436void
437verify_loop_closed_ssa (void)
438{
439 basic_block bb;
440 block_stmt_iterator bsi;
441 tree phi;
442 unsigned i;
443
d51157de 444 if (number_of_loops () <= 1)
84d65814
DN
445 return;
446
f430bae8 447 verify_ssa (false);
c913f08a
ZD
448
449 FOR_EACH_BB (bb)
450 {
bb29d951 451 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
c913f08a
ZD
452 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
453 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi, i)->src,
454 PHI_ARG_DEF (phi, i));
455
456 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
457 check_loop_closed_ssa_stmt (bb, bsi_stmt (bsi));
458 }
459}
8b11a64c
ZD
460
461/* Split loop exit edge EXIT. The things are a bit complicated by a need to
5f40b3cb 462 preserve the loop closed ssa form. The newly created block is returned. */
8b11a64c 463
5f40b3cb 464basic_block
8b11a64c
ZD
465split_loop_exit_edge (edge exit)
466{
467 basic_block dest = exit->dest;
598ec7bd 468 basic_block bb = split_edge (exit);
7fac6722 469 tree phi, new_phi, new_name, name;
8b11a64c
ZD
470 use_operand_p op_p;
471
bb29d951 472 for (phi = phi_nodes (dest); phi; phi = PHI_CHAIN (phi))
8b11a64c 473 {
c5cbcccf 474 op_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, single_succ_edge (bb));
8b11a64c 475
7fac6722
ZD
476 name = USE_FROM_PTR (op_p);
477
38635499 478 /* If the argument of the PHI node is a constant, we do not need
7fac6722
ZD
479 to keep it inside loop. */
480 if (TREE_CODE (name) != SSA_NAME)
481 continue;
482
483 /* Otherwise create an auxiliary phi node that will copy the value
38635499 484 of the SSA name out of the loop. */
7fac6722 485 new_name = duplicate_ssa_name (name, NULL);
8b11a64c
ZD
486 new_phi = create_phi_node (new_name, bb);
487 SSA_NAME_DEF_STMT (new_name) = new_phi;
d2e398df 488 add_phi_arg (new_phi, name, exit);
8b11a64c
ZD
489 SET_USE (op_p, new_name);
490 }
5f40b3cb
ZD
491
492 return bb;
8b11a64c
ZD
493}
494
8b11a64c
ZD
495/* Returns the basic block in that statements should be emitted for induction
496 variables incremented at the end of the LOOP. */
497
498basic_block
499ip_end_pos (struct loop *loop)
500{
501 return loop->latch;
502}
503
504/* Returns the basic block in that statements should be emitted for induction
505 variables incremented just before exit condition of a LOOP. */
506
507basic_block
508ip_normal_pos (struct loop *loop)
509{
510 tree last;
511 basic_block bb;
512 edge exit;
513
c5cbcccf 514 if (!single_pred_p (loop->latch))
8b11a64c
ZD
515 return NULL;
516
c5cbcccf 517 bb = single_pred (loop->latch);
8b11a64c 518 last = last_stmt (bb);
ae2cf11b
RG
519 if (!last
520 || TREE_CODE (last) != COND_EXPR)
8b11a64c
ZD
521 return NULL;
522
628f6a4e 523 exit = EDGE_SUCC (bb, 0);
8b11a64c 524 if (exit->dest == loop->latch)
628f6a4e 525 exit = EDGE_SUCC (bb, 1);
8b11a64c
ZD
526
527 if (flow_bb_inside_loop_p (loop, exit->dest))
528 return NULL;
529
530 return bb;
531}
532
533/* Stores the standard position for induction variable increment in LOOP
534 (just before the exit condition if it is available and latch block is empty,
535 end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
536 the increment should be inserted after *BSI. */
537
538void
539standard_iv_increment_position (struct loop *loop, block_stmt_iterator *bsi,
540 bool *insert_after)
541{
542 basic_block bb = ip_normal_pos (loop), latch = ip_end_pos (loop);
543 tree last = last_stmt (latch);
544
545 if (!bb
546 || (last && TREE_CODE (last) != LABEL_EXPR))
547 {
548 *bsi = bsi_last (latch);
549 *insert_after = true;
550 }
551 else
552 {
553 *bsi = bsi_last (bb);
554 *insert_after = false;
555 }
556}
92fc4a2f
ZD
557
558/* Copies phi node arguments for duplicated blocks. The index of the first
559 duplicated block is FIRST_NEW_BLOCK. */
560
561static void
562copy_phi_node_args (unsigned first_new_block)
563{
564 unsigned i;
565
566 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
6580ee77 567 BASIC_BLOCK (i)->flags |= BB_DUPLICATED;
92fc4a2f
ZD
568
569 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
570 add_phi_args_after_copy_bb (BASIC_BLOCK (i));
571
572 for (i = first_new_block; i < (unsigned) last_basic_block; i++)
6580ee77 573 BASIC_BLOCK (i)->flags &= ~BB_DUPLICATED;
92fc4a2f
ZD
574}
575
92fc4a2f 576
84d65814
DN
577/* The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also
578 updates the PHI nodes at start of the copied region. In order to
579 achieve this, only loops whose exits all lead to the same location
580 are handled.
92fc4a2f 581
84d65814
DN
582 Notice that we do not completely update the SSA web after
583 duplication. The caller is responsible for calling update_ssa
584 after the loop has been duplicated. */
92fc4a2f
ZD
585
586bool
587tree_duplicate_loop_to_header_edge (struct loop *loop, edge e,
92fc4a2f 588 unsigned int ndupl, sbitmap wont_exit,
ee8c1b05
ZD
589 edge orig, VEC (edge, heap) **to_remove,
590 int flags)
92fc4a2f
ZD
591{
592 unsigned first_new_block;
92fc4a2f 593
f87000d0 594 if (!loops_state_satisfies_p (LOOPS_HAVE_SIMPLE_LATCHES))
92fc4a2f 595 return false;
f87000d0 596 if (!loops_state_satisfies_p (LOOPS_HAVE_PREHEADERS))
92fc4a2f
ZD
597 return false;
598
599#ifdef ENABLE_CHECKING
d6e840ee
RG
600 if (loops_state_satisfies_p (LOOP_CLOSED_SSA))
601 verify_loop_closed_ssa ();
92fc4a2f
ZD
602#endif
603
92fc4a2f 604 first_new_block = last_basic_block;
d73be268 605 if (!duplicate_loop_to_header_edge (loop, e, ndupl, wont_exit,
ee8c1b05 606 orig, to_remove, flags))
92fc4a2f
ZD
607 return false;
608
609 /* Readd the removed phi args for e. */
71882046 610 flush_pending_stmts (e);
92fc4a2f
ZD
611
612 /* Copy the phi node arguments. */
613 copy_phi_node_args (first_new_block);
614
92fc4a2f 615 scev_reset ();
92fc4a2f
ZD
616
617 return true;
618}
17684618 619
17684618
ZD
620/* Returns true if we can unroll LOOP FACTOR times. Number
621 of iterations of the loop is returned in NITER. */
622
623bool
624can_unroll_loop_p (struct loop *loop, unsigned factor,
625 struct tree_niter_desc *niter)
626{
627 edge exit;
628
629 /* Check whether unrolling is possible. We only want to unroll loops
630 for that we are able to determine number of iterations. We also
631 want to split the extra iterations of the loop from its end,
632 therefore we require that the loop has precisely one
633 exit. */
634
635 exit = single_dom_exit (loop);
636 if (!exit)
637 return false;
638
639 if (!number_of_iterations_exit (loop, exit, niter, false)
bf8dbe38
ZD
640 || niter->cmp == ERROR_MARK
641 /* Scalar evolutions analysis might have copy propagated
642 the abnormal ssa names into these expressions, hence
2f8e468b 643 emitting the computations based on them during loop
bf8dbe38
ZD
644 unrolling might create overlapping life ranges for
645 them, and failures in out-of-ssa. */
646 || contains_abnormal_ssa_name_p (niter->may_be_zero)
647 || contains_abnormal_ssa_name_p (niter->control.base)
648 || contains_abnormal_ssa_name_p (niter->control.step)
649 || contains_abnormal_ssa_name_p (niter->bound))
17684618
ZD
650 return false;
651
652 /* And of course, we must be able to duplicate the loop. */
653 if (!can_duplicate_loop_p (loop))
654 return false;
655
656 /* The final loop should be small enough. */
7f9bc51b 657 if (tree_num_loop_insns (loop, &eni_size_weights) * factor
17684618
ZD
658 > (unsigned) PARAM_VALUE (PARAM_MAX_UNROLLED_INSNS))
659 return false;
660
661 return true;
662}
663
664/* Determines the conditions that control execution of LOOP unrolled FACTOR
665 times. DESC is number of iterations of LOOP. ENTER_COND is set to
666 condition that must be true if the main loop can be entered.
667 EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
668 how the exit from the unrolled loop should be controlled. */
669
670static void
671determine_exit_conditions (struct loop *loop, struct tree_niter_desc *desc,
672 unsigned factor, tree *enter_cond,
673 tree *exit_base, tree *exit_step,
674 enum tree_code *exit_cmp, tree *exit_bound)
675{
676 tree stmts;
677 tree base = desc->control.base;
678 tree step = desc->control.step;
679 tree bound = desc->bound;
d24a32a1 680 tree type = TREE_TYPE (step);
17684618
ZD
681 tree bigstep, delta;
682 tree min = lower_bound_in_type (type, type);
683 tree max = upper_bound_in_type (type, type);
684 enum tree_code cmp = desc->cmp;
685 tree cond = boolean_true_node, assum;
686
d24a32a1
ZD
687 /* For pointers, do the arithmetics in the type of step (sizetype). */
688 base = fold_convert (type, base);
689 bound = fold_convert (type, bound);
690
17684618
ZD
691 *enter_cond = boolean_false_node;
692 *exit_base = NULL_TREE;
693 *exit_step = NULL_TREE;
694 *exit_cmp = ERROR_MARK;
695 *exit_bound = NULL_TREE;
696 gcc_assert (cmp != ERROR_MARK);
697
698 /* We only need to be correct when we answer question
699 "Do at least FACTOR more iterations remain?" in the unrolled loop.
700 Thus, transforming BASE + STEP * i <> BOUND to
701 BASE + STEP * i < BOUND is ok. */
702 if (cmp == NE_EXPR)
703 {
704 if (tree_int_cst_sign_bit (step))
705 cmp = GT_EXPR;
706 else
707 cmp = LT_EXPR;
708 }
709 else if (cmp == LT_EXPR)
710 {
711 gcc_assert (!tree_int_cst_sign_bit (step));
712 }
713 else if (cmp == GT_EXPR)
714 {
715 gcc_assert (tree_int_cst_sign_bit (step));
716 }
717 else
718 gcc_unreachable ();
719
720 /* The main body of the loop may be entered iff:
721
722 1) desc->may_be_zero is false.
723 2) it is possible to check that there are at least FACTOR iterations
724 of the loop, i.e., BOUND - step * FACTOR does not overflow.
725 3) # of iterations is at least FACTOR */
726
6e682d7e 727 if (!integer_zerop (desc->may_be_zero))
17684618
ZD
728 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
729 invert_truthvalue (desc->may_be_zero),
730 cond);
731
732 bigstep = fold_build2 (MULT_EXPR, type, step,
733 build_int_cst_type (type, factor));
734 delta = fold_build2 (MINUS_EXPR, type, bigstep, step);
735 if (cmp == LT_EXPR)
736 assum = fold_build2 (GE_EXPR, boolean_type_node,
737 bound,
738 fold_build2 (PLUS_EXPR, type, min, delta));
739 else
740 assum = fold_build2 (LE_EXPR, boolean_type_node,
741 bound,
742 fold_build2 (PLUS_EXPR, type, max, delta));
743 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
744
745 bound = fold_build2 (MINUS_EXPR, type, bound, delta);
746 assum = fold_build2 (cmp, boolean_type_node, base, bound);
747 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, assum, cond);
748
749 cond = force_gimple_operand (unshare_expr (cond), &stmts, false, NULL_TREE);
750 if (stmts)
598ec7bd 751 bsi_insert_on_edge_immediate (loop_preheader_edge (loop), stmts);
17684618
ZD
752 /* cond now may be a gimple comparison, which would be OK, but also any
753 other gimple rhs (say a && b). In this case we need to force it to
754 operand. */
755 if (!is_gimple_condexpr (cond))
756 {
757 cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
758 if (stmts)
598ec7bd 759 bsi_insert_on_edge_immediate (loop_preheader_edge (loop), stmts);
17684618
ZD
760 }
761 *enter_cond = cond;
762
763 base = force_gimple_operand (unshare_expr (base), &stmts, true, NULL_TREE);
764 if (stmts)
598ec7bd 765 bsi_insert_on_edge_immediate (loop_preheader_edge (loop), stmts);
17684618
ZD
766 bound = force_gimple_operand (unshare_expr (bound), &stmts, true, NULL_TREE);
767 if (stmts)
598ec7bd 768 bsi_insert_on_edge_immediate (loop_preheader_edge (loop), stmts);
17684618
ZD
769
770 *exit_base = base;
771 *exit_step = bigstep;
772 *exit_cmp = cmp;
773 *exit_bound = bound;
774}
775
14fa2cc0
ZD
776/* Scales the frequencies of all basic blocks in LOOP that are strictly
777 dominated by BB by NUM/DEN. */
778
779static void
780scale_dominated_blocks_in_loop (struct loop *loop, basic_block bb,
781 int num, int den)
782{
783 basic_block son;
784
785 if (den == 0)
786 return;
787
788 for (son = first_dom_son (CDI_DOMINATORS, bb);
789 son;
790 son = next_dom_son (CDI_DOMINATORS, son))
791 {
792 if (!flow_bb_inside_loop_p (loop, son))
793 continue;
794 scale_bbs_frequencies_int (&son, 1, num, den);
795 scale_dominated_blocks_in_loop (loop, son, num, den);
796 }
797}
798
d73be268
ZD
799/* Unroll LOOP FACTOR times. DESC describes number of iterations of LOOP.
800 EXIT is the exit of the loop to that DESC corresponds.
801
17684618
ZD
802 If N is number of iterations of the loop and MAY_BE_ZERO is the condition
803 under that loop exits in the first iteration even if N != 0,
804
805 while (1)
806 {
807 x = phi (init, next);
808
809 pre;
810 if (st)
811 break;
812 post;
813 }
814
815 becomes (with possibly the exit conditions formulated a bit differently,
816 avoiding the need to create a new iv):
817
818 if (MAY_BE_ZERO || N < FACTOR)
819 goto rest;
820
821 do
822 {
823 x = phi (init, next);
824
825 pre;
826 post;
827 pre;
828 post;
829 ...
830 pre;
831 post;
832 N -= FACTOR;
833
834 } while (N >= FACTOR);
835
836 rest:
837 init' = phi (init, x);
838
839 while (1)
840 {
841 x = phi (init', next);
842
843 pre;
844 if (st)
845 break;
846 post;
567b96ed
ZD
847 }
848
849 Before the loop is unrolled, TRANSFORM is called for it (only for the
850 unrolled loop, but not for its versioned copy). DATA is passed to
851 TRANSFORM. */
17684618 852
03cb2019
ZD
853/* Probability in % that the unrolled loop is entered. Just a guess. */
854#define PROB_UNROLLED_LOOP_ENTERED 90
855
17684618 856void
567b96ed
ZD
857tree_transform_and_unroll_loop (struct loop *loop, unsigned factor,
858 edge exit, struct tree_niter_desc *desc,
859 transform_callback transform,
860 void *data)
17684618 861{
14fa2cc0 862 tree exit_if, ctr_before, ctr_after;
17684618
ZD
863 tree enter_main_cond, exit_base, exit_step, exit_bound;
864 enum tree_code exit_cmp;
865 tree phi_old_loop, phi_new_loop, phi_rest, init, next, new_init, var;
866 struct loop *new_loop;
867 basic_block rest, exit_bb;
868 edge old_entry, new_entry, old_latch, precond_edge, new_exit;
14fa2cc0 869 edge new_nonexit, e;
17684618
ZD
870 block_stmt_iterator bsi;
871 use_operand_p op;
872 bool ok;
03cb2019 873 unsigned est_niter, prob_entry, scale_unrolled, scale_rest, freq_e, freq_h;
14fa2cc0 874 unsigned new_est_niter, i, prob;
8e08deeb 875 unsigned irr = loop_preheader_edge (loop)->flags & EDGE_IRREDUCIBLE_LOOP;
17684618 876 sbitmap wont_exit;
14fa2cc0 877 VEC (edge, heap) *to_remove = NULL;
17684618
ZD
878
879 est_niter = expected_loop_iterations (loop);
880 determine_exit_conditions (loop, desc, factor,
881 &enter_main_cond, &exit_base, &exit_step,
882 &exit_cmp, &exit_bound);
883
03cb2019
ZD
884 /* Let us assume that the unrolled loop is quite likely to be entered. */
885 if (integer_nonzerop (enter_main_cond))
886 prob_entry = REG_BR_PROB_BASE;
887 else
888 prob_entry = PROB_UNROLLED_LOOP_ENTERED * REG_BR_PROB_BASE / 100;
889
890 /* The values for scales should keep profile consistent, and somewhat close
891 to correct.
892
893 TODO: The current value of SCALE_REST makes it appear that the loop that
894 is created by splitting the remaining iterations of the unrolled loop is
895 executed the same number of times as the original loop, and with the same
896 frequencies, which is obviously wrong. This does not appear to cause
897 problems, so we do not bother with fixing it for now. To make the profile
898 correct, we would need to change the probability of the exit edge of the
899 loop, and recompute the distribution of frequencies in its body because
900 of this change (scale the frequencies of blocks before and after the exit
901 by appropriate factors). */
902 scale_unrolled = prob_entry;
903 scale_rest = REG_BR_PROB_BASE;
904
905 new_loop = loop_version (loop, enter_main_cond, NULL,
906 prob_entry, scale_unrolled, scale_rest, true);
17684618
ZD
907 gcc_assert (new_loop != NULL);
908 update_ssa (TODO_update_ssa);
909
567b96ed 910 /* Determine the probability of the exit edge of the unrolled loop. */
03cb2019
ZD
911 new_est_niter = est_niter / factor;
912
913 /* Without profile feedback, loops for that we do not know a better estimate
914 are assumed to roll 10 times. When we unroll such loop, it appears to
915 roll too little, and it may even seem to be cold. To avoid this, we
916 ensure that the created loop appears to roll at least 5 times (but at
917 most as many times as before unrolling). */
918 if (new_est_niter < 5)
919 {
920 if (est_niter < 5)
921 new_est_niter = est_niter;
922 else
923 new_est_niter = 5;
924 }
925
14fa2cc0
ZD
926 /* Prepare the cfg and update the phi nodes. Move the loop exit to the
927 loop latch (and make its condition dummy, for the moment). */
17684618
ZD
928 rest = loop_preheader_edge (new_loop)->src;
929 precond_edge = single_pred_edge (rest);
598ec7bd 930 split_edge (loop_latch_edge (loop));
17684618
ZD
931 exit_bb = single_pred (loop->latch);
932
14fa2cc0
ZD
933 /* Since the exit edge will be removed, the frequency of all the blocks
934 in the loop that are dominated by it must be scaled by
935 1 / (1 - exit->probability). */
936 scale_dominated_blocks_in_loop (loop, exit->src,
937 REG_BR_PROB_BASE,
938 REG_BR_PROB_BASE - exit->probability);
939
567b96ed 940 bsi = bsi_last (exit_bb);
a9b77cd1
ZD
941 exit_if = build3 (COND_EXPR, void_type_node, boolean_true_node,
942 NULL_TREE, NULL_TREE);
943
567b96ed 944 bsi_insert_after (&bsi, exit_if, BSI_NEW_STMT);
8e08deeb 945 new_exit = make_edge (exit_bb, rest, EDGE_FALSE_VALUE | irr);
6270df4c 946 rescan_loop_exit (new_exit, true, false);
14fa2cc0
ZD
947
948 /* Set the probability of new exit to the same of the old one. Fix
949 the frequency of the latch block, by scaling it back by
950 1 - exit->probability. */
951 new_exit->count = exit->count;
952 new_exit->probability = exit->probability;
17684618 953 new_nonexit = single_pred_edge (loop->latch);
14fa2cc0 954 new_nonexit->probability = REG_BR_PROB_BASE - exit->probability;
17684618 955 new_nonexit->flags = EDGE_TRUE_VALUE;
14fa2cc0
ZD
956 new_nonexit->count -= exit->count;
957 if (new_nonexit->count < 0)
958 new_nonexit->count = 0;
959 scale_bbs_frequencies_int (&loop->latch, 1, new_nonexit->probability,
960 REG_BR_PROB_BASE);
17684618
ZD
961
962 old_entry = loop_preheader_edge (loop);
963 new_entry = loop_preheader_edge (new_loop);
964 old_latch = loop_latch_edge (loop);
965 for (phi_old_loop = phi_nodes (loop->header),
966 phi_new_loop = phi_nodes (new_loop->header);
967 phi_old_loop;
968 phi_old_loop = PHI_CHAIN (phi_old_loop),
969 phi_new_loop = PHI_CHAIN (phi_new_loop))
970 {
971 init = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_entry);
972 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi_new_loop, new_entry);
973 gcc_assert (operand_equal_for_phi_arg_p (init, USE_FROM_PTR (op)));
974 next = PHI_ARG_DEF_FROM_EDGE (phi_old_loop, old_latch);
975
976 /* Prefer using original variable as a base for the new ssa name.
977 This is necessary for virtual ops, and useful in order to avoid
978 losing debug info for real ops. */
979 if (TREE_CODE (next) == SSA_NAME)
980 var = SSA_NAME_VAR (next);
981 else if (TREE_CODE (init) == SSA_NAME)
982 var = SSA_NAME_VAR (init);
983 else
984 {
985 var = create_tmp_var (TREE_TYPE (init), "unrinittmp");
f004ab02 986 add_referenced_var (var);
17684618
ZD
987 }
988
989 new_init = make_ssa_name (var, NULL_TREE);
990 phi_rest = create_phi_node (new_init, rest);
991 SSA_NAME_DEF_STMT (new_init) = phi_rest;
992
993 add_phi_arg (phi_rest, init, precond_edge);
994 add_phi_arg (phi_rest, next, new_exit);
995 SET_USE (op, new_init);
996 }
997
14fa2cc0
ZD
998 remove_path (exit);
999
567b96ed
ZD
1000 /* Transform the loop. */
1001 if (transform)
1002 (*transform) (loop, data);
1003
14fa2cc0
ZD
1004 /* Unroll the loop and remove the exits in all iterations except for the
1005 last one. */
567b96ed
ZD
1006 wont_exit = sbitmap_alloc (factor);
1007 sbitmap_ones (wont_exit);
14fa2cc0
ZD
1008 RESET_BIT (wont_exit, factor - 1);
1009
567b96ed
ZD
1010 ok = tree_duplicate_loop_to_header_edge
1011 (loop, loop_latch_edge (loop), factor - 1,
14fa2cc0 1012 wont_exit, new_exit, &to_remove, DLTHE_FLAG_UPDATE_FREQ);
567b96ed
ZD
1013 free (wont_exit);
1014 gcc_assert (ok);
14fa2cc0
ZD
1015
1016 for (i = 0; VEC_iterate (edge, to_remove, i, e); i++)
1017 {
1018 ok = remove_path (e);
1019 gcc_assert (ok);
1020 }
1021 VEC_free (edge, heap, to_remove);
567b96ed
ZD
1022 update_ssa (TODO_update_ssa);
1023
1024 /* Ensure that the frequencies in the loop match the new estimated
1025 number of iterations, and change the probability of the new
1026 exit edge. */
1027 freq_h = loop->header->frequency;
1028 freq_e = EDGE_FREQUENCY (loop_preheader_edge (loop));
1029 if (freq_h != 0)
1030 scale_loop_frequencies (loop, freq_e * (new_est_niter + 1), freq_h);
1031
1032 exit_bb = single_pred (loop->latch);
1033 new_exit = find_edge (exit_bb, rest);
1034 new_exit->count = loop_preheader_edge (loop)->count;
1035 new_exit->probability = REG_BR_PROB_BASE / (new_est_niter + 1);
1036
1037 rest->count += new_exit->count;
1038 rest->frequency += EDGE_FREQUENCY (new_exit);
1039
1040 new_nonexit = single_pred_edge (loop->latch);
14fa2cc0 1041 prob = new_nonexit->probability;
567b96ed 1042 new_nonexit->probability = REG_BR_PROB_BASE - new_exit->probability;
14fa2cc0
ZD
1043 new_nonexit->count = exit_bb->count - new_exit->count;
1044 if (new_nonexit->count < 0)
1045 new_nonexit->count = 0;
87621e5f
SE
1046 if (prob > 0)
1047 scale_bbs_frequencies_int (&loop->latch, 1, new_nonexit->probability,
1048 prob);
567b96ed 1049
17684618
ZD
1050 /* Finally create the new counter for number of iterations and add the new
1051 exit instruction. */
1052 bsi = bsi_last (exit_bb);
567b96ed 1053 exit_if = bsi_stmt (bsi);
17684618 1054 create_iv (exit_base, exit_step, NULL_TREE, loop,
567b96ed
ZD
1055 &bsi, false, &ctr_before, &ctr_after);
1056 COND_EXPR_COND (exit_if) = build2 (exit_cmp, boolean_type_node, ctr_after,
1057 exit_bound);
1058 update_stmt (exit_if);
17684618 1059
c0493b13 1060#ifdef ENABLE_CHECKING
17684618
ZD
1061 verify_flow_info ();
1062 verify_dominators (CDI_DOMINATORS);
d73be268 1063 verify_loop_structure ();
17684618 1064 verify_loop_closed_ssa ();
c0493b13 1065#endif
17684618 1066}
567b96ed
ZD
1067
1068/* Wrapper over tree_transform_and_unroll_loop for case we do not
1069 want to transform the loop before unrolling. The meaning
1070 of the arguments is the same as for tree_transform_and_unroll_loop. */
1071
1072void
1073tree_unroll_loop (struct loop *loop, unsigned factor,
1074 edge exit, struct tree_niter_desc *desc)
1075{
1076 tree_transform_and_unroll_loop (loop, factor, exit, desc,
1077 NULL, NULL);
1078}
This page took 1.821488 seconds and 5 git commands to generate.