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 /* Add exit phis for the USE on EXIT. */
43 add_exit_phis_edge (basic_block exit
, tree use
)
45 tree phi
, def_stmt
= SSA_NAME_DEF_STMT (use
);
46 basic_block def_bb
= bb_for_stmt (def_stmt
);
47 struct loop
*def_loop
;
50 /* Check that some of the edges entering the EXIT block exits a loop in
51 that USE is defined. */
52 for (e
= exit
->pred
; e
; e
= e
->pred_next
)
54 def_loop
= find_common_loop (def_bb
->loop_father
, e
->src
->loop_father
);
55 if (!flow_bb_inside_loop_p (def_loop
, e
->dest
))
62 phi
= create_phi_node (use
, exit
);
64 for (e
= exit
->pred
; e
; e
= e
->pred_next
)
65 add_phi_arg (&phi
, use
, e
);
67 SSA_NAME_DEF_STMT (use
) = def_stmt
;
70 /* Add exit phis for VAR that is used in LIVEIN.
71 Exits of the loops are stored in EXITS. */
74 add_exit_phis_var (tree var
, bitmap livein
, bitmap exits
)
78 basic_block def_bb
= bb_for_stmt (SSA_NAME_DEF_STMT (var
));
80 bitmap_clear_bit (livein
, def_bb
->index
);
82 def
= BITMAP_XMALLOC ();
83 bitmap_set_bit (def
, def_bb
->index
);
84 compute_global_livein (livein
, def
);
87 EXECUTE_IF_AND_IN_BITMAP (exits
, livein
, 0, index
,
88 add_exit_phis_edge (BASIC_BLOCK (index
), var
));
91 /* Add exit phis for the names marked in NAMES_TO_RENAME.
92 Exits of the loops are stored in EXITS. Sets of blocks where the ssa
93 names are used are stored in USE_BLOCKS. */
96 add_exit_phis (bitmap names_to_rename
, bitmap
*use_blocks
, bitmap loop_exits
)
100 EXECUTE_IF_SET_IN_BITMAP (names_to_rename
, 0, i
,
102 add_exit_phis_var (ssa_name (i
), use_blocks
[i
], loop_exits
);
106 /* Returns a bitmap of all loop exit edge targets. */
109 get_loops_exits (void)
111 bitmap exits
= BITMAP_XMALLOC ();
117 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
118 if (e
->src
!= ENTRY_BLOCK_PTR
119 && !flow_bb_inside_loop_p (e
->src
->loop_father
, bb
))
121 bitmap_set_bit (exits
, bb
->index
);
129 /* For USE in BB, if it is used outside of the loop it is defined in,
130 mark it for rewrite. Record basic block BB where it is used
134 find_uses_to_rename_use (basic_block bb
, tree use
, bitmap
*use_blocks
)
138 struct loop
*def_loop
;
140 if (TREE_CODE (use
) != SSA_NAME
)
143 ver
= SSA_NAME_VERSION (use
);
144 def_bb
= bb_for_stmt (SSA_NAME_DEF_STMT (use
));
147 def_loop
= def_bb
->loop_father
;
149 /* If the definition is not inside loop, it is not interesting. */
150 if (!def_loop
->outer
)
153 if (!use_blocks
[ver
])
154 use_blocks
[ver
] = BITMAP_XMALLOC ();
155 bitmap_set_bit (use_blocks
[ver
], bb
->index
);
157 if (!flow_bb_inside_loop_p (def_loop
, bb
))
158 mark_for_rewrite (use
);
161 /* For uses in STMT, mark names that are used outside of the loop they are
162 defined to rewrite. Record the set of blocks in that the ssa
163 names are defined to USE_BLOCKS. */
166 find_uses_to_rename_stmt (tree stmt
, bitmap
*use_blocks
)
170 v_may_def_optype v_may_defs
;
173 basic_block bb
= bb_for_stmt (stmt
);
175 get_stmt_operands (stmt
);
176 ann
= stmt_ann (stmt
);
178 uses
= USE_OPS (ann
);
179 for (i
= 0; i
< NUM_USES (uses
); i
++)
180 find_uses_to_rename_use (bb
, USE_OP (uses
, i
), use_blocks
);
182 vuses
= VUSE_OPS (ann
);
183 for (i
= 0; i
< NUM_VUSES (vuses
); i
++)
184 find_uses_to_rename_use (bb
, VUSE_OP (vuses
, i
),use_blocks
);
186 v_may_defs
= V_MAY_DEF_OPS (ann
);
187 for (i
= 0; i
< NUM_V_MAY_DEFS (v_may_defs
); i
++)
188 find_uses_to_rename_use (bb
, V_MAY_DEF_OP (v_may_defs
, i
), use_blocks
);
191 /* Marks names that are used outside of the loop they are defined in
192 for rewrite. Records the set of blocks in that the ssa
193 names are defined to USE_BLOCKS. */
196 find_uses_to_rename (bitmap
*use_blocks
)
199 block_stmt_iterator bsi
;
205 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
206 for (i
= 0; i
< (unsigned) PHI_NUM_ARGS (phi
); i
++)
207 find_uses_to_rename_use (PHI_ARG_EDGE (phi
, i
)->src
,
208 PHI_ARG_DEF (phi
, i
), use_blocks
);
210 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
211 find_uses_to_rename_stmt (bsi_stmt (bsi
), use_blocks
);
215 /* Rewrites the program into a loop closed ssa form -- i.e. inserts extra
216 phi nodes to ensure that no variable is used outside the loop it is
219 This strengthening of the basic ssa form has several advantages:
221 1) Updating it during unrolling/peeling/versioning is trivial, since
222 we do not need to care about the uses outside of the loop.
223 2) The behavior of all uses of an induction variable is the same.
224 Without this, you need to distinguish the case when the variable
225 is used outside of the loop it is defined in, for example
227 for (i = 0; i < 100; i++)
229 for (j = 0; j < 100; j++)
237 Looking from the outer loop with the normal SSA form, the first use of k
238 is not well-behaved, while the second one is an induction variable with
239 base 99 and step 1. */
242 rewrite_into_loop_closed_ssa (void)
244 bitmap loop_exits
= get_loops_exits ();
247 bitmap names_to_rename
;
249 if (any_marked_for_rewrite_p ())
252 use_blocks
= xcalloc (num_ssa_names
, sizeof (bitmap
));
254 /* Find the uses outside loops. */
255 find_uses_to_rename (use_blocks
);
257 /* Add the phi nodes on exits of the loops for the names we need to
259 names_to_rename
= marked_ssa_names ();
260 add_exit_phis (names_to_rename
, use_blocks
, loop_exits
);
262 for (i
= 0; i
< num_ssa_names
; i
++)
263 BITMAP_XFREE (use_blocks
[i
]);
265 BITMAP_XFREE (loop_exits
);
266 BITMAP_XFREE (names_to_rename
);
268 /* Do the rewriting. */
269 rewrite_ssa_into_ssa ();
272 /* Check invariants of the loop closed ssa form for the USE in BB. */
275 check_loop_closed_ssa_use (basic_block bb
, tree use
)
280 if (TREE_CODE (use
) != SSA_NAME
)
283 def
= SSA_NAME_DEF_STMT (use
);
284 def_bb
= bb_for_stmt (def
);
286 && !flow_bb_inside_loop_p (def_bb
->loop_father
, bb
))
290 /* Checks invariants of loop closed ssa form in statement STMT in BB. */
293 check_loop_closed_ssa_stmt (basic_block bb
, tree stmt
)
297 v_may_def_optype v_may_defs
;
301 get_stmt_operands (stmt
);
302 ann
= stmt_ann (stmt
);
304 uses
= USE_OPS (ann
);
305 for (i
= 0; i
< NUM_USES (uses
); i
++)
306 check_loop_closed_ssa_use (bb
, USE_OP (uses
, i
));
308 vuses
= VUSE_OPS (ann
);
309 for (i
= 0; i
< NUM_VUSES (vuses
); i
++)
310 check_loop_closed_ssa_use (bb
, VUSE_OP (vuses
, i
));
312 v_may_defs
= V_MAY_DEF_OPS (ann
);
313 for (i
= 0; i
< NUM_V_MAY_DEFS (v_may_defs
); i
++)
314 check_loop_closed_ssa_use (bb
, V_MAY_DEF_OP (v_may_defs
, i
));
317 /* Checks that invariants of the loop closed ssa form are preserved. */
320 verify_loop_closed_ssa (void)
323 block_stmt_iterator bsi
;
331 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
332 for (i
= 0; i
< (unsigned) PHI_NUM_ARGS (phi
); i
++)
333 check_loop_closed_ssa_use (PHI_ARG_EDGE (phi
, i
)->src
,
334 PHI_ARG_DEF (phi
, i
));
336 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
337 check_loop_closed_ssa_stmt (bb
, bsi_stmt (bsi
));