* config/i386/predicates.md (general_reg_operand): Use GENERAL_REGNO_P.
[official-gcc.git] / gcc / tree-ssa-loop-unswitch.c
blob596593bc6233cb81b1e395351efcac11fec5da8c
1 /* Loop unswitching.
2 Copyright (C) 2004-2015 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 "backend.h"
24 #include "tree.h"
25 #include "gimple.h"
26 #include "hard-reg-set.h"
27 #include "ssa.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "tm_p.h"
31 #include "internal-fn.h"
32 #include "gimplify.h"
33 #include "tree-cfg.h"
34 #include "tree-ssa-loop-niter.h"
35 #include "tree-ssa-loop.h"
36 #include "tree-into-ssa.h"
37 #include "cfgloop.h"
38 #include "params.h"
39 #include "tree-pass.h"
40 #include "tree-inline.h"
42 /* This file implements the loop unswitching, i.e. transformation of loops like
44 while (A)
46 if (inv)
51 if (!inv)
55 where inv is the loop invariant, into
57 if (inv)
59 while (A)
65 else
67 while (A)
74 Inv is considered invariant iff the values it compares are both invariant;
75 tree-ssa-loop-im.c ensures that all the suitable conditions are in this
76 shape. */
78 static struct loop *tree_unswitch_loop (struct loop *, basic_block, tree);
79 static bool tree_unswitch_single_loop (struct loop *, int);
80 static tree tree_may_unswitch_on (basic_block, struct loop *);
82 /* Main entry point. Perform loop unswitching on all suitable loops. */
84 unsigned int
85 tree_ssa_unswitch_loops (void)
87 struct loop *loop;
88 bool changed = false;
89 HOST_WIDE_INT iterations;
91 /* Go through inner loops (only original ones). */
92 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
94 if (dump_file && (dump_flags & TDF_DETAILS))
95 fprintf (dump_file, ";; Considering loop %d\n", loop->num);
97 /* Do not unswitch in cold regions. */
98 if (optimize_loop_for_size_p (loop))
100 if (dump_file && (dump_flags & TDF_DETAILS))
101 fprintf (dump_file, ";; Not unswitching cold loops\n");
102 continue;
105 /* The loop should not be too large, to limit code growth. */
106 if (tree_num_loop_insns (loop, &eni_size_weights)
107 > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
109 if (dump_file && (dump_flags & TDF_DETAILS))
110 fprintf (dump_file, ";; Not unswitching, loop too big\n");
111 continue;
114 /* If the loop is not expected to iterate, there is no need
115 for unswitching. */
116 iterations = estimated_loop_iterations_int (loop);
117 if (iterations >= 0 && iterations <= 1)
119 if (dump_file && (dump_flags & TDF_DETAILS))
120 fprintf (dump_file, ";; Not unswitching, loop is not expected to iterate\n");
121 continue;
124 changed |= tree_unswitch_single_loop (loop, 0);
127 if (changed)
128 return TODO_cleanup_cfg;
129 return 0;
132 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
133 basic blocks (for what it means see comments below). */
135 static tree
136 tree_may_unswitch_on (basic_block bb, struct loop *loop)
138 gimple last, def;
139 gcond *stmt;
140 tree cond, use;
141 basic_block def_bb;
142 ssa_op_iter iter;
144 /* BB must end in a simple conditional jump. */
145 last = last_stmt (bb);
146 if (!last || gimple_code (last) != GIMPLE_COND)
147 return NULL_TREE;
148 stmt = as_a <gcond *> (last);
150 /* To keep the things simple, we do not directly remove the conditions,
151 but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
152 loop where we would unswitch again on such a condition. */
153 if (gimple_cond_true_p (stmt) || gimple_cond_false_p (stmt))
154 return NULL_TREE;
156 /* Condition must be invariant. */
157 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
159 def = SSA_NAME_DEF_STMT (use);
160 def_bb = gimple_bb (def);
161 if (def_bb
162 && flow_bb_inside_loop_p (loop, def_bb))
163 return NULL_TREE;
166 cond = build2 (gimple_cond_code (stmt), boolean_type_node,
167 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
169 return cond;
172 /* Simplifies COND using checks in front of the entry of the LOOP. Just very
173 simplish (sufficient to prevent us from duplicating loop in unswitching
174 unnecessarily). */
176 static tree
177 simplify_using_entry_checks (struct loop *loop, tree cond)
179 edge e = loop_preheader_edge (loop);
180 gimple stmt;
182 while (1)
184 stmt = last_stmt (e->src);
185 if (stmt
186 && gimple_code (stmt) == GIMPLE_COND
187 && gimple_cond_code (stmt) == TREE_CODE (cond)
188 && operand_equal_p (gimple_cond_lhs (stmt),
189 TREE_OPERAND (cond, 0), 0)
190 && operand_equal_p (gimple_cond_rhs (stmt),
191 TREE_OPERAND (cond, 1), 0))
192 return (e->flags & EDGE_TRUE_VALUE
193 ? boolean_true_node
194 : boolean_false_node);
196 if (!single_pred_p (e->src))
197 return cond;
199 e = single_pred_edge (e->src);
200 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
201 return cond;
205 /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
206 it to grow too much, it is too easy to create example on that the code would
207 grow exponentially. */
209 static bool
210 tree_unswitch_single_loop (struct loop *loop, int num)
212 basic_block *bbs;
213 struct loop *nloop;
214 unsigned i, found;
215 tree cond = NULL_TREE;
216 gimple stmt;
217 bool changed = false;
219 i = 0;
220 bbs = get_loop_body (loop);
221 found = loop->num_nodes;
223 while (1)
225 /* Find a bb to unswitch on. */
226 for (; i < loop->num_nodes; i++)
227 if ((cond = tree_may_unswitch_on (bbs[i], loop)))
228 break;
230 if (i == loop->num_nodes)
232 if (dump_file
233 && num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL)
234 && (dump_flags & TDF_DETAILS))
235 fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
237 if (found == loop->num_nodes)
239 free (bbs);
240 return changed;
242 break;
245 cond = simplify_using_entry_checks (loop, cond);
246 stmt = last_stmt (bbs[i]);
247 if (integer_nonzerop (cond))
249 /* Remove false path. */
250 gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
251 boolean_true_node);
252 changed = true;
254 else if (integer_zerop (cond))
256 /* Remove true path. */
257 gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
258 boolean_false_node);
259 changed = true;
261 /* Do not unswitch too much. */
262 else if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
264 i++;
265 continue;
267 /* In nested tree_unswitch_single_loop first optimize all conditions
268 using entry checks, then discover still reachable blocks in the
269 loop and find the condition only among those still reachable bbs. */
270 else if (num != 0)
272 if (found == loop->num_nodes)
273 found = i;
274 i++;
275 continue;
277 else
279 found = i;
280 break;
283 update_stmt (stmt);
284 i++;
287 if (num != 0)
289 basic_block *tos, *worklist;
291 /* When called recursively, first do a quick discovery
292 of reachable bbs after the above changes and only
293 consider conditions in still reachable bbs. */
294 tos = worklist = XNEWVEC (basic_block, loop->num_nodes);
296 for (i = 0; i < loop->num_nodes; i++)
297 bbs[i]->flags &= ~BB_REACHABLE;
299 /* Start with marking header. */
300 *tos++ = bbs[0];
301 bbs[0]->flags |= BB_REACHABLE;
303 /* Iterate: find everything reachable from what we've already seen
304 within the same innermost loop. Don't look through false edges
305 if condition is always true or true edges if condition is
306 always false. */
307 while (tos != worklist)
309 basic_block b = *--tos;
310 edge e;
311 edge_iterator ei;
312 int flags = 0;
314 if (EDGE_COUNT (b->succs) == 2)
316 gimple stmt = last_stmt (b);
317 if (stmt
318 && gimple_code (stmt) == GIMPLE_COND)
320 gcond *cond_stmt = as_a <gcond *> (stmt);
321 if (gimple_cond_true_p (cond_stmt))
322 flags = EDGE_FALSE_VALUE;
323 else if (gimple_cond_false_p (cond_stmt))
324 flags = EDGE_TRUE_VALUE;
328 FOR_EACH_EDGE (e, ei, b->succs)
330 basic_block dest = e->dest;
332 if (dest->loop_father == loop
333 && !(dest->flags & BB_REACHABLE)
334 && !(e->flags & flags))
336 *tos++ = dest;
337 dest->flags |= BB_REACHABLE;
342 free (worklist);
344 /* Find a bb to unswitch on. */
345 for (; found < loop->num_nodes; found++)
346 if ((bbs[found]->flags & BB_REACHABLE)
347 && (cond = tree_may_unswitch_on (bbs[found], loop)))
348 break;
350 if (found == loop->num_nodes)
352 free (bbs);
353 return changed;
357 if (dump_file && (dump_flags & TDF_DETAILS))
358 fprintf (dump_file, ";; Unswitching loop\n");
360 initialize_original_copy_tables ();
361 /* Unswitch the loop on this condition. */
362 nloop = tree_unswitch_loop (loop, bbs[found], cond);
363 if (!nloop)
365 free_original_copy_tables ();
366 free (bbs);
367 return changed;
370 /* Update the SSA form after unswitching. */
371 update_ssa (TODO_update_ssa);
372 free_original_copy_tables ();
374 /* Invoke itself on modified loops. */
375 tree_unswitch_single_loop (nloop, num + 1);
376 tree_unswitch_single_loop (loop, num + 1);
377 free (bbs);
378 return true;
381 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
382 unswitching of innermost loops. COND is the condition determining which
383 loop is entered -- the new loop is entered if COND is true. Returns NULL
384 if impossible, new loop otherwise. */
386 static struct loop *
387 tree_unswitch_loop (struct loop *loop,
388 basic_block unswitch_on, tree cond)
390 unsigned prob_true;
391 edge edge_true, edge_false;
393 /* Some sanity checking. */
394 gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
395 gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
396 gcc_assert (loop->inner == NULL);
398 extract_true_false_edges_from_block (unswitch_on, &edge_true, &edge_false);
399 prob_true = edge_true->probability;
400 return loop_version (loop, unshare_expr (cond),
401 NULL, prob_true, prob_true,
402 REG_BR_PROB_BASE - prob_true, false);
405 /* Loop unswitching pass. */
407 namespace {
409 const pass_data pass_data_tree_unswitch =
411 GIMPLE_PASS, /* type */
412 "unswitch", /* name */
413 OPTGROUP_LOOP, /* optinfo_flags */
414 TV_TREE_LOOP_UNSWITCH, /* tv_id */
415 PROP_cfg, /* properties_required */
416 0, /* properties_provided */
417 0, /* properties_destroyed */
418 0, /* todo_flags_start */
419 0, /* todo_flags_finish */
422 class pass_tree_unswitch : public gimple_opt_pass
424 public:
425 pass_tree_unswitch (gcc::context *ctxt)
426 : gimple_opt_pass (pass_data_tree_unswitch, ctxt)
429 /* opt_pass methods: */
430 virtual bool gate (function *) { return flag_unswitch_loops != 0; }
431 virtual unsigned int execute (function *);
433 }; // class pass_tree_unswitch
435 unsigned int
436 pass_tree_unswitch::execute (function *fun)
438 if (number_of_loops (fun) <= 1)
439 return 0;
441 return tree_ssa_unswitch_loops ();
444 } // anon namespace
446 gimple_opt_pass *
447 make_pass_tree_unswitch (gcc::context *ctxt)
449 return new pass_tree_unswitch (ctxt);