* arm.h (REVERSE_CONDITION): Define.
[official-gcc.git] / gcc / tree-ssa-loop-manip.c
blobedb821d24f7266de823560d4d31619340e2be138
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
9 later version.
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.
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
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30 #include "output.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-dump.h"
34 #include "timevar.h"
35 #include "cfgloop.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). */
48 void
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;
54 tree vb, va;
55 enum tree_code incr_op = PLUS_EXPR;
57 if (!var)
59 var = create_tmp_var (TREE_TYPE (base), "ivtmp");
60 add_referenced_tmp_var (var);
63 vb = make_ssa_name (var, NULL_TREE);
64 if (var_before)
65 *var_before = vb;
66 va = make_ssa_name (var, NULL_TREE);
67 if (var_after)
68 *var_after = va;
70 /* For easier readability of the created code, produce MINUS_EXPRs
71 when suitable. */
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))
79 incr_op = MINUS_EXPR;
80 step = step1;
83 else
85 if (!tree_expr_nonnegative_p (step)
86 && may_negate_without_overflow_p (step))
88 incr_op = MINUS_EXPR;
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),
96 vb, step));
97 SSA_NAME_DEF_STMT (va) = stmt;
98 if (after)
99 bsi_insert_after (incr_pos, stmt, BSI_NEW_STMT);
100 else
101 bsi_insert_before (incr_pos, stmt, BSI_NEW_STMT);
103 initial = base;
105 stmt = create_phi_node (vb, loop->header);
106 SSA_NAME_DEF_STMT (vb) = stmt;
107 add_phi_arg (&stmt, initial, loop_preheader_edge (loop));
108 add_phi_arg (&stmt, va, loop_latch_edge (loop));
111 /* Add exit phis for the USE on EXIT. */
113 static void
114 add_exit_phis_edge (basic_block exit, tree use)
116 tree phi, def_stmt = SSA_NAME_DEF_STMT (use);
117 basic_block def_bb = bb_for_stmt (def_stmt);
118 struct loop *def_loop;
119 edge e;
121 /* Check that some of the edges entering the EXIT block exits a loop in
122 that USE is defined. */
123 for (e = exit->pred; e; e = e->pred_next)
125 def_loop = find_common_loop (def_bb->loop_father, e->src->loop_father);
126 if (!flow_bb_inside_loop_p (def_loop, e->dest))
127 break;
130 if (!e)
131 return;
133 phi = create_phi_node (use, exit);
135 for (e = exit->pred; e; e = e->pred_next)
136 add_phi_arg (&phi, use, e);
138 SSA_NAME_DEF_STMT (use) = def_stmt;
141 /* Add exit phis for VAR that is used in LIVEIN.
142 Exits of the loops are stored in EXITS. */
144 static void
145 add_exit_phis_var (tree var, bitmap livein, bitmap exits)
147 bitmap def;
148 int index;
149 basic_block def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var));
151 bitmap_clear_bit (livein, def_bb->index);
153 def = BITMAP_XMALLOC ();
154 bitmap_set_bit (def, def_bb->index);
155 compute_global_livein (livein, def);
156 BITMAP_XFREE (def);
158 EXECUTE_IF_AND_IN_BITMAP (exits, livein, 0, index,
159 add_exit_phis_edge (BASIC_BLOCK (index), var));
162 /* Add exit phis for the names marked in NAMES_TO_RENAME.
163 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
164 names are used are stored in USE_BLOCKS. */
166 static void
167 add_exit_phis (bitmap names_to_rename, bitmap *use_blocks, bitmap loop_exits)
169 unsigned i;
171 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i,
173 add_exit_phis_var (ssa_name (i), use_blocks[i], loop_exits);
177 /* Returns a bitmap of all loop exit edge targets. */
179 static bitmap
180 get_loops_exits (void)
182 bitmap exits = BITMAP_XMALLOC ();
183 basic_block bb;
184 edge e;
186 FOR_EACH_BB (bb)
188 for (e = bb->pred; e; e = e->pred_next)
189 if (e->src != ENTRY_BLOCK_PTR
190 && !flow_bb_inside_loop_p (e->src->loop_father, bb))
192 bitmap_set_bit (exits, bb->index);
193 break;
197 return exits;
200 /* For USE in BB, if it is used outside of the loop it is defined in,
201 mark it for rewrite. Record basic block BB where it is used
202 to USE_BLOCKS. */
204 static void
205 find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks)
207 unsigned ver;
208 basic_block def_bb;
209 struct loop *def_loop;
211 if (TREE_CODE (use) != SSA_NAME)
212 return;
214 ver = SSA_NAME_VERSION (use);
215 def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use));
216 if (!def_bb)
217 return;
218 def_loop = def_bb->loop_father;
220 /* If the definition is not inside loop, it is not interesting. */
221 if (!def_loop->outer)
222 return;
224 if (!use_blocks[ver])
225 use_blocks[ver] = BITMAP_XMALLOC ();
226 bitmap_set_bit (use_blocks[ver], bb->index);
228 if (!flow_bb_inside_loop_p (def_loop, bb))
229 mark_for_rewrite (use);
232 /* For uses in STMT, mark names that are used outside of the loop they are
233 defined to rewrite. Record the set of blocks in that the ssa
234 names are defined to USE_BLOCKS. */
236 static void
237 find_uses_to_rename_stmt (tree stmt, bitmap *use_blocks)
239 ssa_op_iter iter;
240 tree var;
241 basic_block bb = bb_for_stmt (stmt);
243 get_stmt_operands (stmt);
245 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
246 find_uses_to_rename_use (bb, var, use_blocks);
249 /* Marks names that are used outside of the loop they are defined in
250 for rewrite. Records the set of blocks in that the ssa
251 names are defined to USE_BLOCKS. */
253 static void
254 find_uses_to_rename (bitmap *use_blocks)
256 basic_block bb;
257 block_stmt_iterator bsi;
258 tree phi;
259 unsigned i;
261 FOR_EACH_BB (bb)
263 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
264 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
265 find_uses_to_rename_use (PHI_ARG_EDGE (phi, i)->src,
266 PHI_ARG_DEF (phi, i), use_blocks);
268 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
269 find_uses_to_rename_stmt (bsi_stmt (bsi), use_blocks);
273 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
274 phi nodes to ensure that no variable is used outside the loop it is
275 defined in.
277 This strengthening of the basic ssa form has several advantages:
279 1) Updating it during unrolling/peeling/versioning is trivial, since
280 we do not need to care about the uses outside of the loop.
281 2) The behavior of all uses of an induction variable is the same.
282 Without this, you need to distinguish the case when the variable
283 is used outside of the loop it is defined in, for example
285 for (i = 0; i < 100; i++)
287 for (j = 0; j < 100; j++)
289 k = i + j;
290 use1 (k);
292 use2 (k);
295 Looking from the outer loop with the normal SSA form, the first use of k
296 is not well-behaved, while the second one is an induction variable with
297 base 99 and step 1. */
299 void
300 rewrite_into_loop_closed_ssa (void)
302 bitmap loop_exits = get_loops_exits ();
303 bitmap *use_blocks;
304 unsigned i;
305 bitmap names_to_rename;
307 if (any_marked_for_rewrite_p ())
308 abort ();
310 use_blocks = xcalloc (num_ssa_names, sizeof (bitmap));
312 /* Find the uses outside loops. */
313 find_uses_to_rename (use_blocks);
315 /* Add the phi nodes on exits of the loops for the names we need to
316 rewrite. */
317 names_to_rename = marked_ssa_names ();
318 add_exit_phis (names_to_rename, use_blocks, loop_exits);
320 for (i = 0; i < num_ssa_names; i++)
321 BITMAP_XFREE (use_blocks[i]);
322 free (use_blocks);
323 BITMAP_XFREE (loop_exits);
324 BITMAP_XFREE (names_to_rename);
326 /* Do the rewriting. */
327 rewrite_ssa_into_ssa ();
330 /* Check invariants of the loop closed ssa form for the USE in BB. */
332 static void
333 check_loop_closed_ssa_use (basic_block bb, tree use)
335 tree def;
336 basic_block def_bb;
338 if (TREE_CODE (use) != SSA_NAME)
339 return;
341 def = SSA_NAME_DEF_STMT (use);
342 def_bb = bb_for_stmt (def);
343 if (def_bb
344 && !flow_bb_inside_loop_p (def_bb->loop_father, bb))
345 abort ();
348 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
350 static void
351 check_loop_closed_ssa_stmt (basic_block bb, tree stmt)
353 ssa_op_iter iter;
354 tree var;
356 get_stmt_operands (stmt);
358 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_ALL_USES)
359 check_loop_closed_ssa_use (bb, var);
362 /* Checks that invariants of the loop closed ssa form are preserved. */
364 void
365 verify_loop_closed_ssa (void)
367 basic_block bb;
368 block_stmt_iterator bsi;
369 tree phi;
370 unsigned i;
372 verify_ssa ();
374 FOR_EACH_BB (bb)
376 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
377 for (i = 0; i < (unsigned) PHI_NUM_ARGS (phi); i++)
378 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi, i)->src,
379 PHI_ARG_DEF (phi, i));
381 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
382 check_loop_closed_ssa_stmt (bb, bsi_stmt (bsi));