2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / tree-ssa-loop-unswitch.c
blobe00fe189514e3ee4248eacc707fbf1a48664e1b4
1 /* Loop unswitching.
2 Copyright (C) 2004, 2005 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 "domwalk.h"
37 #include "params.h"
38 #include "tree-pass.h"
40 /* This file implements the loop unswitching, i.e. transformation of loops like
42 while (A)
44 if (inv)
49 if (!inv)
53 where inv is the loop invariant, into
55 if (inv)
57 while (A)
63 else
65 while (A)
72 Inv is considered invariant iff the values it compares are both invariant;
73 tree-ssa-loop-im.c ensures that all the suitable conditions are in this
74 shape. */
76 static struct loop *tree_unswitch_loop (struct loops *, struct loop *, basic_block,
77 tree);
78 static bool tree_unswitch_single_loop (struct loops *, struct loop *, int);
79 static tree tree_may_unswitch_on (basic_block, struct loop *);
81 /* Main entry point. Perform loop unswitching on all suitable LOOPS. */
83 void
84 tree_ssa_unswitch_loops (struct loops *loops)
86 int i, num;
87 struct loop *loop;
88 bool changed = false;
90 /* Go through inner loops (only original ones). */
91 num = loops->num;
93 for (i = 1; i < num; i++)
95 /* Removed loop? */
96 loop = loops->parray[i];
97 if (!loop)
98 continue;
100 if (loop->inner)
101 continue;
103 changed |= tree_unswitch_single_loop (loops, loop, 0);
106 if (changed)
107 cleanup_tree_cfg_loop ();
110 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
111 basic blocks (for what it means see comments below). */
113 static tree
114 tree_may_unswitch_on (basic_block bb, struct loop *loop)
116 tree stmt, def, cond;
117 basic_block def_bb;
118 use_optype uses;
119 unsigned i;
121 /* BB must end in a simple conditional jump. */
122 stmt = last_stmt (bb);
123 if (!stmt || TREE_CODE (stmt) != COND_EXPR)
124 return NULL_TREE;
126 /* Condition must be invariant. */
127 uses = STMT_USE_OPS (stmt);
128 for (i = 0; i < NUM_USES (uses); i++)
130 def = SSA_NAME_DEF_STMT (USE_OP (uses, i));
131 def_bb = bb_for_stmt (def);
132 if (def_bb
133 && flow_bb_inside_loop_p (loop, def_bb))
134 return NULL_TREE;
137 cond = COND_EXPR_COND (stmt);
138 /* To keep the things simple, we do not directly remove the conditions,
139 but just replace tests with 0/1. Prevent the infinite loop where we
140 would unswitch again on such a condition. */
141 if (integer_zerop (cond) || integer_nonzerop (cond))
142 return NULL_TREE;
144 return cond;
147 /* Simplifies COND using checks in front of the entry of the LOOP. Just very
148 simplish (sufficient to prevent us from duplicating loop in unswitching
149 unnecessarily). */
151 static tree
152 simplify_using_entry_checks (struct loop *loop, tree cond)
154 edge e = loop_preheader_edge (loop);
155 tree stmt;
157 while (1)
159 stmt = last_stmt (e->src);
160 if (stmt
161 && TREE_CODE (stmt) == COND_EXPR
162 && operand_equal_p (COND_EXPR_COND (stmt), cond, 0))
163 return (e->flags & EDGE_TRUE_VALUE
164 ? boolean_true_node
165 : boolean_false_node);
167 if (!single_pred_p (e->src))
168 return cond;
170 e = single_pred_edge (e->src);
171 if (e->src == ENTRY_BLOCK_PTR)
172 return cond;
176 /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
177 it to grow too much, it is too easy to create example on that the code would
178 grow exponentially. */
180 static bool
181 tree_unswitch_single_loop (struct loops *loops, struct loop *loop, int num)
183 basic_block *bbs;
184 struct loop *nloop;
185 unsigned i;
186 tree cond = NULL_TREE, stmt;
187 bool changed = false;
189 /* Do not unswitch too much. */
190 if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
192 if (dump_file && (dump_flags & TDF_DETAILS))
193 fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
194 return false;
197 /* Only unswitch innermost loops. */
198 if (loop->inner)
200 if (dump_file && (dump_flags & TDF_DETAILS))
201 fprintf (dump_file, ";; Not unswitching, not innermost loop\n");
202 return false;
205 /* The loop should not be too large, to limit code growth. */
206 if (tree_num_loop_insns (loop)
207 > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
209 if (dump_file && (dump_flags & TDF_DETAILS))
210 fprintf (dump_file, ";; Not unswitching, loop too big\n");
211 return false;
214 i = 0;
215 bbs = get_loop_body (loop);
217 while (1)
219 /* Find a bb to unswitch on. */
220 for (; i < loop->num_nodes; i++)
221 if ((cond = tree_may_unswitch_on (bbs[i], loop)))
222 break;
224 if (i == loop->num_nodes)
226 free (bbs);
227 return changed;
230 cond = simplify_using_entry_checks (loop, cond);
231 stmt = last_stmt (bbs[i]);
232 if (integer_nonzerop (cond))
234 /* Remove false path. */
235 COND_EXPR_COND (stmt) = boolean_true_node;
236 changed = true;
238 else if (integer_zerop (cond))
240 /* Remove true path. */
241 COND_EXPR_COND (stmt) = boolean_false_node;
242 changed = true;
244 else
245 break;
247 update_stmt (stmt);
248 i++;
251 if (dump_file && (dump_flags & TDF_DETAILS))
252 fprintf (dump_file, ";; Unswitching loop\n");
254 /* Unswitch the loop on this condition. */
255 nloop = tree_unswitch_loop (loops, loop, bbs[i], cond);
256 if (!nloop)
257 return changed;
259 /* Update the SSA form after unswitching. */
260 update_ssa (TODO_update_ssa);
262 /* Invoke itself on modified loops. */
263 tree_unswitch_single_loop (loops, nloop, num + 1);
264 tree_unswitch_single_loop (loops, loop, num + 1);
265 return true;
268 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
269 unswitching of innermost loops. COND is the condition determining which
270 loop is entered -- the new loop is entered if COND is true. Returns NULL
271 if impossible, new loop otherwise. */
273 static struct loop *
274 tree_unswitch_loop (struct loops *loops, struct loop *loop,
275 basic_block unswitch_on, tree cond)
277 basic_block condition_bb;
279 /* Some sanity checking. */
280 gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
281 gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
282 gcc_assert (loop->inner == NULL);
284 return loop_version (loops, loop, unshare_expr (cond),
285 &condition_bb);