1 /* High-level loop manipulation functions.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
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 2, or (at your option) any
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
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
36 #include "tree-pass.h"
37 #include "cfglayout.h"
38 #include "tree-scalar-evolution.h"
40 /* Creates an induction variable with value BASE + STEP * iteration in LOOP.
41 It is expected that neither BASE nor STEP are shared with other expressions
42 (unless the sharing rules allow this). Use VAR as a base var_decl for it
43 (if NULL, a new temporary will be created). The increment will occur at
44 INCR_POS (after it if AFTER is true, before it otherwise). The ssa versions
45 of the variable before and after increment will be stored in VAR_BEFORE and
46 VAR_AFTER (unless they are NULL). */
49 create_iv (tree base
, tree step
, tree var
, struct loop
*loop
,
50 block_stmt_iterator
*incr_pos
, bool after
,
51 tree
*var_before
, tree
*var_after
)
53 tree stmt
, initial
, step1
, stmts
;
55 enum tree_code incr_op
= PLUS_EXPR
;
59 var
= create_tmp_var (TREE_TYPE (base
), "ivtmp");
60 add_referenced_tmp_var (var
);
63 vb
= make_ssa_name (var
, NULL_TREE
);
66 va
= make_ssa_name (var
, NULL_TREE
);
70 /* For easier readability of the created code, produce MINUS_EXPRs
72 if (TREE_CODE (step
) == INTEGER_CST
)
74 if (TYPE_UNSIGNED (TREE_TYPE (step
)))
76 step1
= fold (build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
));
77 if (tree_int_cst_lt (step1
, step
))
85 if (!tree_expr_nonnegative_p (step
)
86 && may_negate_without_overflow_p (step
))
89 step
= fold (build1 (NEGATE_EXPR
, TREE_TYPE (step
), step
));
94 stmt
= build2 (MODIFY_EXPR
, void_type_node
, va
,
95 build2 (incr_op
, TREE_TYPE (base
),
97 SSA_NAME_DEF_STMT (va
) = stmt
;
99 bsi_insert_after (incr_pos
, stmt
, BSI_NEW_STMT
);
101 bsi_insert_before (incr_pos
, stmt
, BSI_NEW_STMT
);
103 initial
= force_gimple_operand (base
, &stmts
, true, var
);
106 edge pe
= loop_preheader_edge (loop
);
108 bsi_insert_on_edge_immediate_loop (pe
, stmts
);
111 stmt
= create_phi_node (vb
, loop
->header
);
112 SSA_NAME_DEF_STMT (vb
) = stmt
;
113 add_phi_arg (&stmt
, initial
, loop_preheader_edge (loop
));
114 add_phi_arg (&stmt
, va
, loop_latch_edge (loop
));
117 /* Add exit phis for the USE on EXIT. */
120 add_exit_phis_edge (basic_block exit
, tree use
)
122 tree phi
, def_stmt
= SSA_NAME_DEF_STMT (use
);
123 basic_block def_bb
= bb_for_stmt (def_stmt
);
124 struct loop
*def_loop
;
128 /* Check that some of the edges entering the EXIT block exits a loop in
129 that USE is defined. */
130 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
132 def_loop
= find_common_loop (def_bb
->loop_father
, e
->src
->loop_father
);
133 if (!flow_bb_inside_loop_p (def_loop
, e
->dest
))
140 phi
= create_phi_node (use
, exit
);
142 FOR_EACH_EDGE (e
, ei
, exit
->preds
)
143 add_phi_arg (&phi
, use
, e
);
145 SSA_NAME_DEF_STMT (use
) = def_stmt
;
148 /* Add exit phis for VAR that is used in LIVEIN.
149 Exits of the loops are stored in EXITS. */
152 add_exit_phis_var (tree var
, bitmap livein
, bitmap exits
)
156 basic_block def_bb
= bb_for_stmt (SSA_NAME_DEF_STMT (var
));
158 bitmap_clear_bit (livein
, def_bb
->index
);
160 def
= BITMAP_XMALLOC ();
161 bitmap_set_bit (def
, def_bb
->index
);
162 compute_global_livein (livein
, def
);
165 EXECUTE_IF_AND_IN_BITMAP (exits
, livein
, 0, index
,
166 add_exit_phis_edge (BASIC_BLOCK (index
), var
));
169 /* Add exit phis for the names marked in NAMES_TO_RENAME.
170 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
171 names are used are stored in USE_BLOCKS. */
174 add_exit_phis (bitmap names_to_rename
, bitmap
*use_blocks
, bitmap loop_exits
)
178 EXECUTE_IF_SET_IN_BITMAP (names_to_rename
, 0, i
,
180 add_exit_phis_var (ssa_name (i
), use_blocks
[i
], loop_exits
);
184 /* Returns a bitmap of all loop exit edge targets. */
187 get_loops_exits (void)
189 bitmap exits
= BITMAP_XMALLOC ();
196 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
197 if (e
->src
!= ENTRY_BLOCK_PTR
198 && !flow_bb_inside_loop_p (e
->src
->loop_father
, bb
))
200 bitmap_set_bit (exits
, bb
->index
);
208 /* For USE in BB, if it is used outside of the loop it is defined in,
209 mark it for rewrite. Record basic block BB where it is used
213 find_uses_to_rename_use (basic_block bb
, tree use
, bitmap
*use_blocks
)
217 struct loop
*def_loop
;
219 if (TREE_CODE (use
) != SSA_NAME
)
222 ver
= SSA_NAME_VERSION (use
);
223 def_bb
= bb_for_stmt (SSA_NAME_DEF_STMT (use
));
226 def_loop
= def_bb
->loop_father
;
228 /* If the definition is not inside loop, it is not interesting. */
229 if (!def_loop
->outer
)
232 if (!use_blocks
[ver
])
233 use_blocks
[ver
] = BITMAP_XMALLOC ();
234 bitmap_set_bit (use_blocks
[ver
], bb
->index
);
236 if (!flow_bb_inside_loop_p (def_loop
, bb
))
237 mark_for_rewrite (use
);
240 /* For uses in STMT, mark names that are used outside of the loop they are
241 defined to rewrite. Record the set of blocks in that the ssa
242 names are defined to USE_BLOCKS. */
245 find_uses_to_rename_stmt (tree stmt
, bitmap
*use_blocks
)
249 basic_block bb
= bb_for_stmt (stmt
);
251 get_stmt_operands (stmt
);
253 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
254 find_uses_to_rename_use (bb
, var
, use_blocks
);
257 /* Marks names that are used outside of the loop they are defined in
258 for rewrite. Records the set of blocks in that the ssa
259 names are defined to USE_BLOCKS. */
262 find_uses_to_rename (bitmap
*use_blocks
)
265 block_stmt_iterator bsi
;
271 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
272 for (i
= 0; i
< (unsigned) PHI_NUM_ARGS (phi
); i
++)
273 find_uses_to_rename_use (PHI_ARG_EDGE (phi
, i
)->src
,
274 PHI_ARG_DEF (phi
, i
), use_blocks
);
276 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
277 find_uses_to_rename_stmt (bsi_stmt (bsi
), use_blocks
);
281 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
282 phi nodes to ensure that no variable is used outside the loop it is
285 This strengthening of the basic ssa form has several advantages:
287 1) Updating it during unrolling/peeling/versioning is trivial, since
288 we do not need to care about the uses outside of the loop.
289 2) The behavior of all uses of an induction variable is the same.
290 Without this, you need to distinguish the case when the variable
291 is used outside of the loop it is defined in, for example
293 for (i = 0; i < 100; i++)
295 for (j = 0; j < 100; j++)
303 Looking from the outer loop with the normal SSA form, the first use of k
304 is not well-behaved, while the second one is an induction variable with
305 base 99 and step 1. */
308 rewrite_into_loop_closed_ssa (void)
310 bitmap loop_exits
= get_loops_exits ();
313 bitmap names_to_rename
;
315 gcc_assert (!any_marked_for_rewrite_p ());
317 use_blocks
= xcalloc (num_ssa_names
, sizeof (bitmap
));
319 /* Find the uses outside loops. */
320 find_uses_to_rename (use_blocks
);
322 /* Add the phi nodes on exits of the loops for the names we need to
324 names_to_rename
= marked_ssa_names ();
325 add_exit_phis (names_to_rename
, use_blocks
, loop_exits
);
327 for (i
= 0; i
< num_ssa_names
; i
++)
328 BITMAP_XFREE (use_blocks
[i
]);
330 BITMAP_XFREE (loop_exits
);
331 BITMAP_XFREE (names_to_rename
);
333 /* Do the rewriting. */
334 rewrite_ssa_into_ssa ();
337 /* Check invariants of the loop closed ssa form for the USE in BB. */
340 check_loop_closed_ssa_use (basic_block bb
, tree use
)
345 if (TREE_CODE (use
) != SSA_NAME
)
348 def
= SSA_NAME_DEF_STMT (use
);
349 def_bb
= bb_for_stmt (def
);
351 || flow_bb_inside_loop_p (def_bb
->loop_father
, bb
));
354 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
357 check_loop_closed_ssa_stmt (basic_block bb
, tree stmt
)
362 get_stmt_operands (stmt
);
364 FOR_EACH_SSA_TREE_OPERAND (var
, stmt
, iter
, SSA_OP_ALL_USES
)
365 check_loop_closed_ssa_use (bb
, var
);
368 /* Checks that invariants of the loop closed ssa form are preserved. */
371 verify_loop_closed_ssa (void)
374 block_stmt_iterator bsi
;
382 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
383 for (i
= 0; i
< (unsigned) PHI_NUM_ARGS (phi
); i
++)
384 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi
, i
)->src
,
385 PHI_ARG_DEF (phi
, i
));
387 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
388 check_loop_closed_ssa_stmt (bb
, bsi_stmt (bsi
));
392 /* Split loop exit edge EXIT. The things are a bit complicated by a need to
393 preserve the loop closed ssa form. */
396 split_loop_exit_edge (edge exit
)
398 basic_block dest
= exit
->dest
;
399 basic_block bb
= loop_split_edge_with (exit
, NULL
);
400 tree phi
, new_phi
, new_name
;
403 for (phi
= phi_nodes (dest
); phi
; phi
= TREE_CHAIN (phi
))
405 op_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, EDGE_SUCC (bb
, 0));
407 new_name
= duplicate_ssa_name (USE_FROM_PTR (op_p
), NULL
);
408 new_phi
= create_phi_node (new_name
, bb
);
409 SSA_NAME_DEF_STMT (new_name
) = new_phi
;
410 add_phi_arg (&new_phi
, USE_FROM_PTR (op_p
), exit
);
411 SET_USE (op_p
, new_name
);
415 /* Insert statement STMT to the edge E and update the loop structures.
416 Returns the newly created block (if any). */
419 bsi_insert_on_edge_immediate_loop (edge e
, tree stmt
)
421 basic_block src
, dest
, new_bb
;
427 loop_c
= find_common_loop (src
->loop_father
, dest
->loop_father
);
429 new_bb
= bsi_insert_on_edge_immediate (e
, stmt
);
434 add_bb_to_loop (new_bb
, loop_c
);
435 if (dest
->loop_father
->latch
== src
)
436 dest
->loop_father
->latch
= new_bb
;
441 /* Returns the basic block in that statements should be emitted for induction
442 variables incremented at the end of the LOOP. */
445 ip_end_pos (struct loop
*loop
)
450 /* Returns the basic block in that statements should be emitted for induction
451 variables incremented just before exit condition of a LOOP. */
454 ip_normal_pos (struct loop
*loop
)
460 if (EDGE_COUNT (loop
->latch
->preds
) > 1)
463 bb
= EDGE_PRED (loop
->latch
, 0)->src
;
464 last
= last_stmt (bb
);
465 if (TREE_CODE (last
) != COND_EXPR
)
468 exit
= EDGE_SUCC (bb
, 0);
469 if (exit
->dest
== loop
->latch
)
470 exit
= EDGE_SUCC (bb
, 1);
472 if (flow_bb_inside_loop_p (loop
, exit
->dest
))
478 /* Stores the standard position for induction variable increment in LOOP
479 (just before the exit condition if it is available and latch block is empty,
480 end of the latch block otherwise) to BSI. INSERT_AFTER is set to true if
481 the increment should be inserted after *BSI. */
484 standard_iv_increment_position (struct loop
*loop
, block_stmt_iterator
*bsi
,
487 basic_block bb
= ip_normal_pos (loop
), latch
= ip_end_pos (loop
);
488 tree last
= last_stmt (latch
);
491 || (last
&& TREE_CODE (last
) != LABEL_EXPR
))
493 *bsi
= bsi_last (latch
);
494 *insert_after
= true;
498 *bsi
= bsi_last (bb
);
499 *insert_after
= false;