Add C++11 header <cuchar>.
[official-gcc.git] / gcc / tree-ssa-loop-unswitch.c
bloba27363822df042d7a146c9a65ab0bf16af1982fa
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 "predict.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "hard-reg-set.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "tm_p.h"
32 #include "internal-fn.h"
33 #include "gimplify.h"
34 #include "tree-cfg.h"
35 #include "tree-ssa-loop-niter.h"
36 #include "tree-ssa-loop.h"
37 #include "tree-into-ssa.h"
38 #include "cfgloop.h"
39 #include "params.h"
40 #include "tree-pass.h"
41 #include "tree-inline.h"
43 /* This file implements the loop unswitching, i.e. transformation of loops like
45 while (A)
47 if (inv)
52 if (!inv)
56 where inv is the loop invariant, into
58 if (inv)
60 while (A)
66 else
68 while (A)
75 Inv is considered invariant iff the values it compares are both invariant;
76 tree-ssa-loop-im.c ensures that all the suitable conditions are in this
77 shape. */
79 static struct loop *tree_unswitch_loop (struct loop *, basic_block, tree);
80 static bool tree_unswitch_single_loop (struct loop *, int);
81 static tree tree_may_unswitch_on (basic_block, struct loop *);
83 /* Main entry point. Perform loop unswitching on all suitable loops. */
85 unsigned int
86 tree_ssa_unswitch_loops (void)
88 struct loop *loop;
89 bool changed = false;
90 HOST_WIDE_INT iterations;
92 /* Go through inner loops (only original ones). */
93 FOR_EACH_LOOP (loop, LI_ONLY_INNERMOST)
95 if (dump_file && (dump_flags & TDF_DETAILS))
96 fprintf (dump_file, ";; Considering loop %d\n", loop->num);
98 /* Do not unswitch in cold regions. */
99 if (optimize_loop_for_size_p (loop))
101 if (dump_file && (dump_flags & TDF_DETAILS))
102 fprintf (dump_file, ";; Not unswitching cold loops\n");
103 continue;
106 /* The loop should not be too large, to limit code growth. */
107 if (tree_num_loop_insns (loop, &eni_size_weights)
108 > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
110 if (dump_file && (dump_flags & TDF_DETAILS))
111 fprintf (dump_file, ";; Not unswitching, loop too big\n");
112 continue;
115 /* If the loop is not expected to iterate, there is no need
116 for unswitching. */
117 iterations = estimated_loop_iterations_int (loop);
118 if (iterations >= 0 && iterations <= 1)
120 if (dump_file && (dump_flags & TDF_DETAILS))
121 fprintf (dump_file, ";; Not unswitching, loop is not expected to iterate\n");
122 continue;
125 changed |= tree_unswitch_single_loop (loop, 0);
128 if (changed)
129 return TODO_cleanup_cfg;
130 return 0;
133 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
134 basic blocks (for what it means see comments below). */
136 static tree
137 tree_may_unswitch_on (basic_block bb, struct loop *loop)
139 gimple last, def;
140 gcond *stmt;
141 tree cond, use;
142 basic_block def_bb;
143 ssa_op_iter iter;
145 /* BB must end in a simple conditional jump. */
146 last = last_stmt (bb);
147 if (!last || gimple_code (last) != GIMPLE_COND)
148 return NULL_TREE;
149 stmt = as_a <gcond *> (last);
151 /* To keep the things simple, we do not directly remove the conditions,
152 but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
153 loop where we would unswitch again on such a condition. */
154 if (gimple_cond_true_p (stmt) || gimple_cond_false_p (stmt))
155 return NULL_TREE;
157 /* Condition must be invariant. */
158 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
160 def = SSA_NAME_DEF_STMT (use);
161 def_bb = gimple_bb (def);
162 if (def_bb
163 && flow_bb_inside_loop_p (loop, def_bb))
164 return NULL_TREE;
167 cond = build2 (gimple_cond_code (stmt), boolean_type_node,
168 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
170 return cond;
173 /* Simplifies COND using checks in front of the entry of the LOOP. Just very
174 simplish (sufficient to prevent us from duplicating loop in unswitching
175 unnecessarily). */
177 static tree
178 simplify_using_entry_checks (struct loop *loop, tree cond)
180 edge e = loop_preheader_edge (loop);
181 gimple stmt;
183 while (1)
185 stmt = last_stmt (e->src);
186 if (stmt
187 && gimple_code (stmt) == GIMPLE_COND
188 && gimple_cond_code (stmt) == TREE_CODE (cond)
189 && operand_equal_p (gimple_cond_lhs (stmt),
190 TREE_OPERAND (cond, 0), 0)
191 && operand_equal_p (gimple_cond_rhs (stmt),
192 TREE_OPERAND (cond, 1), 0))
193 return (e->flags & EDGE_TRUE_VALUE
194 ? boolean_true_node
195 : boolean_false_node);
197 if (!single_pred_p (e->src))
198 return cond;
200 e = single_pred_edge (e->src);
201 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
202 return cond;
206 /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
207 it to grow too much, it is too easy to create example on that the code would
208 grow exponentially. */
210 static bool
211 tree_unswitch_single_loop (struct loop *loop, int num)
213 basic_block *bbs;
214 struct loop *nloop;
215 unsigned i, found;
216 tree cond = NULL_TREE;
217 gimple stmt;
218 bool changed = false;
220 i = 0;
221 bbs = get_loop_body (loop);
222 found = loop->num_nodes;
224 while (1)
226 /* Find a bb to unswitch on. */
227 for (; i < loop->num_nodes; i++)
228 if ((cond = tree_may_unswitch_on (bbs[i], loop)))
229 break;
231 if (i == loop->num_nodes)
233 if (dump_file
234 && num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL)
235 && (dump_flags & TDF_DETAILS))
236 fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
238 if (found == loop->num_nodes)
240 free (bbs);
241 return changed;
243 break;
246 cond = simplify_using_entry_checks (loop, cond);
247 stmt = last_stmt (bbs[i]);
248 if (integer_nonzerop (cond))
250 /* Remove false path. */
251 gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
252 boolean_true_node);
253 changed = true;
255 else if (integer_zerop (cond))
257 /* Remove true path. */
258 gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
259 boolean_false_node);
260 changed = true;
262 /* Do not unswitch too much. */
263 else if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
265 i++;
266 continue;
268 /* In nested tree_unswitch_single_loop first optimize all conditions
269 using entry checks, then discover still reachable blocks in the
270 loop and find the condition only among those still reachable bbs. */
271 else if (num != 0)
273 if (found == loop->num_nodes)
274 found = i;
275 i++;
276 continue;
278 else
280 found = i;
281 break;
284 update_stmt (stmt);
285 i++;
288 if (num != 0)
290 basic_block *tos, *worklist;
292 /* When called recursively, first do a quick discovery
293 of reachable bbs after the above changes and only
294 consider conditions in still reachable bbs. */
295 tos = worklist = XNEWVEC (basic_block, loop->num_nodes);
297 for (i = 0; i < loop->num_nodes; i++)
298 bbs[i]->flags &= ~BB_REACHABLE;
300 /* Start with marking header. */
301 *tos++ = bbs[0];
302 bbs[0]->flags |= BB_REACHABLE;
304 /* Iterate: find everything reachable from what we've already seen
305 within the same innermost loop. Don't look through false edges
306 if condition is always true or true edges if condition is
307 always false. */
308 while (tos != worklist)
310 basic_block b = *--tos;
311 edge e;
312 edge_iterator ei;
313 int flags = 0;
315 if (EDGE_COUNT (b->succs) == 2)
317 gimple stmt = last_stmt (b);
318 if (stmt
319 && gimple_code (stmt) == GIMPLE_COND)
321 gcond *cond_stmt = as_a <gcond *> (stmt);
322 if (gimple_cond_true_p (cond_stmt))
323 flags = EDGE_FALSE_VALUE;
324 else if (gimple_cond_false_p (cond_stmt))
325 flags = EDGE_TRUE_VALUE;
329 FOR_EACH_EDGE (e, ei, b->succs)
331 basic_block dest = e->dest;
333 if (dest->loop_father == loop
334 && !(dest->flags & BB_REACHABLE)
335 && !(e->flags & flags))
337 *tos++ = dest;
338 dest->flags |= BB_REACHABLE;
343 free (worklist);
345 /* Find a bb to unswitch on. */
346 for (; found < loop->num_nodes; found++)
347 if ((bbs[found]->flags & BB_REACHABLE)
348 && (cond = tree_may_unswitch_on (bbs[found], loop)))
349 break;
351 if (found == loop->num_nodes)
353 free (bbs);
354 return changed;
358 if (dump_file && (dump_flags & TDF_DETAILS))
359 fprintf (dump_file, ";; Unswitching loop\n");
361 initialize_original_copy_tables ();
362 /* Unswitch the loop on this condition. */
363 nloop = tree_unswitch_loop (loop, bbs[found], cond);
364 if (!nloop)
366 free_original_copy_tables ();
367 free (bbs);
368 return changed;
371 /* Update the SSA form after unswitching. */
372 update_ssa (TODO_update_ssa);
373 free_original_copy_tables ();
375 /* Invoke itself on modified loops. */
376 tree_unswitch_single_loop (nloop, num + 1);
377 tree_unswitch_single_loop (loop, num + 1);
378 free (bbs);
379 return true;
382 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
383 unswitching of innermost loops. COND is the condition determining which
384 loop is entered -- the new loop is entered if COND is true. Returns NULL
385 if impossible, new loop otherwise. */
387 static struct loop *
388 tree_unswitch_loop (struct loop *loop,
389 basic_block unswitch_on, tree cond)
391 unsigned prob_true;
392 edge edge_true, edge_false;
394 /* Some sanity checking. */
395 gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
396 gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
397 gcc_assert (loop->inner == NULL);
399 extract_true_false_edges_from_block (unswitch_on, &edge_true, &edge_false);
400 prob_true = edge_true->probability;
401 return loop_version (loop, unshare_expr (cond),
402 NULL, prob_true, prob_true,
403 REG_BR_PROB_BASE - prob_true, false);
406 /* Loop unswitching pass. */
408 namespace {
410 const pass_data pass_data_tree_unswitch =
412 GIMPLE_PASS, /* type */
413 "unswitch", /* name */
414 OPTGROUP_LOOP, /* optinfo_flags */
415 TV_TREE_LOOP_UNSWITCH, /* tv_id */
416 PROP_cfg, /* properties_required */
417 0, /* properties_provided */
418 0, /* properties_destroyed */
419 0, /* todo_flags_start */
420 0, /* todo_flags_finish */
423 class pass_tree_unswitch : public gimple_opt_pass
425 public:
426 pass_tree_unswitch (gcc::context *ctxt)
427 : gimple_opt_pass (pass_data_tree_unswitch, ctxt)
430 /* opt_pass methods: */
431 virtual bool gate (function *) { return flag_unswitch_loops != 0; }
432 virtual unsigned int execute (function *);
434 }; // class pass_tree_unswitch
436 unsigned int
437 pass_tree_unswitch::execute (function *fun)
439 if (number_of_loops (fun) <= 1)
440 return 0;
442 return tree_ssa_unswitch_loops ();
445 } // anon namespace
447 gimple_opt_pass *
448 make_pass_tree_unswitch (gcc::context *ctxt)
450 return new pass_tree_unswitch (ctxt);