PR tree-optimization/43833
[official-gcc/alias-decl.git] / gcc / tree-ssa-loop-unswitch.c
blobbb3b1a5f40afa043104a9f5aa70ee8d320b1c080
1 /* Loop unswitching.
2 Copyright (C) 2004, 2005, 2007, 2008 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
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 "params.h"
36 #include "tree-pass.h"
37 #include "tree-inline.h"
39 /* This file implements the loop unswitching, i.e. transformation of loops like
41 while (A)
43 if (inv)
48 if (!inv)
52 where inv is the loop invariant, into
54 if (inv)
56 while (A)
62 else
64 while (A)
71 Inv is considered invariant iff the values it compares are both invariant;
72 tree-ssa-loop-im.c ensures that all the suitable conditions are in this
73 shape. */
75 static struct loop *tree_unswitch_loop (struct loop *, basic_block, tree);
76 static bool tree_unswitch_single_loop (struct loop *, int);
77 static tree tree_may_unswitch_on (basic_block, struct loop *);
79 /* Main entry point. Perform loop unswitching on all suitable loops. */
81 unsigned int
82 tree_ssa_unswitch_loops (void)
84 loop_iterator li;
85 struct loop *loop;
86 bool changed = false;
88 /* Go through inner loops (only original ones). */
89 FOR_EACH_LOOP (li, loop, LI_ONLY_INNERMOST)
91 if (dump_file && (dump_flags & TDF_DETAILS))
92 fprintf (dump_file, ";; Considering loop %d\n", loop->num);
94 /* Do not unswitch in cold regions. */
95 if (optimize_loop_for_size_p (loop))
97 if (dump_file && (dump_flags & TDF_DETAILS))
98 fprintf (dump_file, ";; Not unswitching cold loops\n");
99 continue;
102 /* The loop should not be too large, to limit code growth. */
103 if (tree_num_loop_insns (loop, &eni_size_weights)
104 > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
106 if (dump_file && (dump_flags & TDF_DETAILS))
107 fprintf (dump_file, ";; Not unswitching, loop too big\n");
108 continue;
111 changed |= tree_unswitch_single_loop (loop, 0);
114 if (changed)
115 return TODO_cleanup_cfg;
116 return 0;
119 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
120 basic blocks (for what it means see comments below). */
122 static tree
123 tree_may_unswitch_on (basic_block bb, struct loop *loop)
125 gimple stmt, def;
126 tree cond, use;
127 basic_block def_bb;
128 ssa_op_iter iter;
130 /* BB must end in a simple conditional jump. */
131 stmt = last_stmt (bb);
132 if (!stmt || gimple_code (stmt) != GIMPLE_COND)
133 return NULL_TREE;
135 /* Condition must be invariant. */
136 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
138 def = SSA_NAME_DEF_STMT (use);
139 def_bb = gimple_bb (def);
140 if (def_bb
141 && flow_bb_inside_loop_p (loop, def_bb))
142 return NULL_TREE;
145 cond = build2 (gimple_cond_code (stmt), boolean_type_node,
146 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
148 /* To keep the things simple, we do not directly remove the conditions,
149 but just replace tests with 0/1. Prevent the infinite loop where we
150 would unswitch again on such a condition. */
151 if (integer_zerop (cond) || integer_nonzerop (cond))
152 return NULL_TREE;
154 return cond;
157 /* Simplifies COND using checks in front of the entry of the LOOP. Just very
158 simplish (sufficient to prevent us from duplicating loop in unswitching
159 unnecessarily). */
161 static tree
162 simplify_using_entry_checks (struct loop *loop, tree cond)
164 edge e = loop_preheader_edge (loop);
165 gimple stmt;
167 while (1)
169 stmt = last_stmt (e->src);
170 if (stmt
171 && gimple_code (stmt) == GIMPLE_COND
172 && gimple_cond_code (stmt) == TREE_CODE (cond)
173 && operand_equal_p (gimple_cond_lhs (stmt),
174 TREE_OPERAND (cond, 0), 0)
175 && operand_equal_p (gimple_cond_rhs (stmt),
176 TREE_OPERAND (cond, 1), 0))
177 return (e->flags & EDGE_TRUE_VALUE
178 ? boolean_true_node
179 : boolean_false_node);
181 if (!single_pred_p (e->src))
182 return cond;
184 e = single_pred_edge (e->src);
185 if (e->src == ENTRY_BLOCK_PTR)
186 return cond;
190 /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
191 it to grow too much, it is too easy to create example on that the code would
192 grow exponentially. */
194 static bool
195 tree_unswitch_single_loop (struct loop *loop, int num)
197 basic_block *bbs;
198 struct loop *nloop;
199 unsigned i;
200 tree cond = NULL_TREE;
201 gimple stmt;
202 bool changed = false;
204 /* Do not unswitch too much. */
205 if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
207 if (dump_file && (dump_flags & TDF_DETAILS))
208 fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
209 return false;
212 i = 0;
213 bbs = get_loop_body (loop);
215 while (1)
217 /* Find a bb to unswitch on. */
218 for (; i < loop->num_nodes; i++)
219 if ((cond = tree_may_unswitch_on (bbs[i], loop)))
220 break;
222 if (i == loop->num_nodes)
224 free (bbs);
225 return changed;
228 cond = simplify_using_entry_checks (loop, cond);
229 stmt = last_stmt (bbs[i]);
230 if (integer_nonzerop (cond))
232 /* Remove false path. */
233 gimple_cond_set_condition_from_tree (stmt, boolean_true_node);
234 changed = true;
236 else if (integer_zerop (cond))
238 /* Remove true path. */
239 gimple_cond_set_condition_from_tree (stmt, boolean_false_node);
240 changed = true;
242 else
243 break;
245 update_stmt (stmt);
246 i++;
249 if (dump_file && (dump_flags & TDF_DETAILS))
250 fprintf (dump_file, ";; Unswitching loop\n");
252 initialize_original_copy_tables ();
253 /* Unswitch the loop on this condition. */
254 nloop = tree_unswitch_loop (loop, bbs[i], cond);
255 if (!nloop)
257 free_original_copy_tables ();
258 free (bbs);
259 return changed;
262 /* Update the SSA form after unswitching. */
263 update_ssa (TODO_update_ssa);
264 free_original_copy_tables ();
266 /* Invoke itself on modified loops. */
267 tree_unswitch_single_loop (nloop, num + 1);
268 tree_unswitch_single_loop (loop, num + 1);
269 free (bbs);
270 return true;
273 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
274 unswitching of innermost loops. COND is the condition determining which
275 loop is entered -- the new loop is entered if COND is true. Returns NULL
276 if impossible, new loop otherwise. */
278 static struct loop *
279 tree_unswitch_loop (struct loop *loop,
280 basic_block unswitch_on, tree cond)
282 unsigned prob_true;
283 edge edge_true, edge_false;
285 /* Some sanity checking. */
286 gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
287 gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
288 gcc_assert (loop->inner == NULL);
290 extract_true_false_edges_from_block (unswitch_on, &edge_true, &edge_false);
291 prob_true = edge_true->probability;
292 return loop_version (loop, unshare_expr (cond),
293 NULL, prob_true, prob_true,
294 REG_BR_PROB_BASE - prob_true, false);