PR tree-optimization/ 79376 - wrong lower bound with %s and non-constant
[official-gcc.git] / gcc / tree-ssa-loop-unswitch.c
blob1d0305a2a8bfec119e6a51ad9631363d00fcd1ba
1 /* Loop unswitching.
2 Copyright (C) 2004-2017 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 "tree-pass.h"
27 #include "ssa.h"
28 #include "fold-const.h"
29 #include "gimplify.h"
30 #include "tree-cfg.h"
31 #include "tree-ssa.h"
32 #include "tree-ssa-loop-niter.h"
33 #include "tree-ssa-loop.h"
34 #include "tree-into-ssa.h"
35 #include "cfgloop.h"
36 #include "params.h"
37 #include "tree-inline.h"
38 #include "gimple-iterator.h"
39 #include "cfghooks.h"
40 #include "tree-ssa-loop-manip.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 *);
81 static bool tree_unswitch_outer_loop (struct loop *);
82 static edge find_loop_guard (struct loop *);
83 static bool empty_bb_without_guard_p (struct loop *, basic_block);
84 static bool used_outside_loop_p (struct loop *, tree);
85 static void hoist_guard (struct loop *, edge);
86 static bool check_exit_phi (struct loop *);
87 static tree get_vop_from_header (struct loop *);
89 /* Main entry point. Perform loop unswitching on all suitable loops. */
91 unsigned int
92 tree_ssa_unswitch_loops (void)
94 struct loop *loop;
95 bool changed = false;
97 /* Go through all loops starting from innermost. */
98 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
100 if (!loop->inner)
101 /* Unswitch innermost loop. */
102 changed |= tree_unswitch_single_loop (loop, 0);
103 else
104 changed |= tree_unswitch_outer_loop (loop);
107 if (changed)
108 return TODO_cleanup_cfg;
109 return 0;
112 /* Return TRUE if an SSA_NAME maybe undefined and is therefore
113 unsuitable for unswitching. STMT is the statement we are
114 considering for unswitching and LOOP is the loop it appears in. */
116 static bool
117 is_maybe_undefined (const tree name, gimple *stmt, struct loop *loop)
119 /* The loop header is the only block we can trivially determine that
120 will always be executed. If the comparison is in the loop
121 header, we know it's OK to unswitch on it. */
122 if (gimple_bb (stmt) == loop->header)
123 return false;
125 auto_bitmap visited_ssa;
126 auto_vec<tree> worklist;
127 worklist.safe_push (name);
128 bitmap_set_bit (visited_ssa, SSA_NAME_VERSION (name));
129 while (!worklist.is_empty ())
131 tree t = worklist.pop ();
133 /* If it's obviously undefined, avoid further computations. */
134 if (ssa_undefined_value_p (t, true))
135 return true;
137 /* A PARM_DECL will not have an SSA_NAME_DEF_STMT. Parameters
138 get their initial value from function entry. */
139 if (SSA_NAME_VAR (t) && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL)
140 continue;
142 gimple *def = SSA_NAME_DEF_STMT (t);
144 /* Check that all the PHI args are fully defined. */
145 if (gphi *phi = dyn_cast <gphi *> (def))
147 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
149 tree t = gimple_phi_arg_def (phi, i);
150 /* If an SSA has already been seen, it may be a loop,
151 but we can continue and ignore this use. Otherwise,
152 add the SSA_NAME to the queue and visit it later. */
153 if (TREE_CODE (t) == SSA_NAME
154 && bitmap_set_bit (visited_ssa, SSA_NAME_VERSION (t)))
155 worklist.safe_push (t);
157 continue;
160 /* Uses in stmts always executed when the region header executes
161 are fine. */
162 if (dominated_by_p (CDI_DOMINATORS, loop->header, gimple_bb (def)))
163 continue;
165 /* Handle calls and memory loads conservatively. */
166 if (!is_gimple_assign (def)
167 || (gimple_assign_single_p (def)
168 && gimple_vuse (def)))
169 return true;
171 /* Check that any SSA names used to define NAME are also fully
172 defined. */
173 use_operand_p use_p;
174 ssa_op_iter iter;
175 FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
177 tree t = USE_FROM_PTR (use_p);
178 /* If an SSA has already been seen, it may be a loop,
179 but we can continue and ignore this use. Otherwise,
180 add the SSA_NAME to the queue and visit it later. */
181 if (bitmap_set_bit (visited_ssa, SSA_NAME_VERSION (t)))
182 worklist.safe_push (t);
185 return false;
188 /* Checks whether we can unswitch LOOP on condition at end of BB -- one of its
189 basic blocks (for what it means see comments below). */
191 static tree
192 tree_may_unswitch_on (basic_block bb, struct loop *loop)
194 gimple *last, *def;
195 gcond *stmt;
196 tree cond, use;
197 basic_block def_bb;
198 ssa_op_iter iter;
200 /* BB must end in a simple conditional jump. */
201 last = last_stmt (bb);
202 if (!last || gimple_code (last) != GIMPLE_COND)
203 return NULL_TREE;
204 stmt = as_a <gcond *> (last);
206 /* To keep the things simple, we do not directly remove the conditions,
207 but just replace tests with 0 != 0 resp. 1 != 0. Prevent the infinite
208 loop where we would unswitch again on such a condition. */
209 if (gimple_cond_true_p (stmt) || gimple_cond_false_p (stmt))
210 return NULL_TREE;
212 /* Condition must be invariant. */
213 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
215 def = SSA_NAME_DEF_STMT (use);
216 def_bb = gimple_bb (def);
217 if (def_bb
218 && flow_bb_inside_loop_p (loop, def_bb))
219 return NULL_TREE;
220 /* Unswitching on undefined values would introduce undefined
221 behavior that the original program might never exercise. */
222 if (is_maybe_undefined (use, stmt, loop))
223 return NULL_TREE;
226 cond = build2 (gimple_cond_code (stmt), boolean_type_node,
227 gimple_cond_lhs (stmt), gimple_cond_rhs (stmt));
229 return cond;
232 /* Simplifies COND using checks in front of the entry of the LOOP. Just very
233 simplish (sufficient to prevent us from duplicating loop in unswitching
234 unnecessarily). */
236 static tree
237 simplify_using_entry_checks (struct loop *loop, tree cond)
239 edge e = loop_preheader_edge (loop);
240 gimple *stmt;
242 while (1)
244 stmt = last_stmt (e->src);
245 if (stmt
246 && gimple_code (stmt) == GIMPLE_COND
247 && gimple_cond_code (stmt) == TREE_CODE (cond)
248 && operand_equal_p (gimple_cond_lhs (stmt),
249 TREE_OPERAND (cond, 0), 0)
250 && operand_equal_p (gimple_cond_rhs (stmt),
251 TREE_OPERAND (cond, 1), 0))
252 return (e->flags & EDGE_TRUE_VALUE
253 ? boolean_true_node
254 : boolean_false_node);
256 if (!single_pred_p (e->src))
257 return cond;
259 e = single_pred_edge (e->src);
260 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
261 return cond;
265 /* Unswitch single LOOP. NUM is number of unswitchings done; we do not allow
266 it to grow too much, it is too easy to create example on that the code would
267 grow exponentially. */
269 static bool
270 tree_unswitch_single_loop (struct loop *loop, int num)
272 basic_block *bbs;
273 struct loop *nloop;
274 unsigned i, found;
275 tree cond = NULL_TREE;
276 gimple *stmt;
277 bool changed = false;
278 HOST_WIDE_INT iterations;
280 /* Perform initial tests if unswitch is eligible. */
281 if (num == 0)
283 /* Do not unswitch in cold regions. */
284 if (optimize_loop_for_size_p (loop))
286 if (dump_file && (dump_flags & TDF_DETAILS))
287 fprintf (dump_file, ";; Not unswitching cold loops\n");
288 return false;
291 /* The loop should not be too large, to limit code growth. */
292 if (tree_num_loop_insns (loop, &eni_size_weights)
293 > (unsigned) PARAM_VALUE (PARAM_MAX_UNSWITCH_INSNS))
295 if (dump_file && (dump_flags & TDF_DETAILS))
296 fprintf (dump_file, ";; Not unswitching, loop too big\n");
297 return false;
300 /* If the loop is not expected to iterate, there is no need
301 for unswitching. */
302 iterations = estimated_loop_iterations_int (loop);
303 if (iterations < 0)
304 iterations = likely_max_loop_iterations_int (loop);
305 if (iterations >= 0 && iterations <= 1)
307 if (dump_file && (dump_flags & TDF_DETAILS))
308 fprintf (dump_file, ";; Not unswitching, loop is not expected"
309 " to iterate\n");
310 return false;
314 i = 0;
315 bbs = get_loop_body (loop);
316 found = loop->num_nodes;
318 while (1)
320 /* Find a bb to unswitch on. */
321 for (; i < loop->num_nodes; i++)
322 if ((cond = tree_may_unswitch_on (bbs[i], loop)))
323 break;
325 if (i == loop->num_nodes)
327 if (dump_file
328 && num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL)
329 && (dump_flags & TDF_DETAILS))
330 fprintf (dump_file, ";; Not unswitching anymore, hit max level\n");
332 if (found == loop->num_nodes)
334 free (bbs);
335 return changed;
337 break;
340 cond = simplify_using_entry_checks (loop, cond);
341 stmt = last_stmt (bbs[i]);
342 if (integer_nonzerop (cond))
344 /* Remove false path. */
345 gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
346 boolean_true_node);
347 changed = true;
349 else if (integer_zerop (cond))
351 /* Remove true path. */
352 gimple_cond_set_condition_from_tree (as_a <gcond *> (stmt),
353 boolean_false_node);
354 changed = true;
356 /* Do not unswitch too much. */
357 else if (num > PARAM_VALUE (PARAM_MAX_UNSWITCH_LEVEL))
359 i++;
360 continue;
362 /* In nested tree_unswitch_single_loop first optimize all conditions
363 using entry checks, then discover still reachable blocks in the
364 loop and find the condition only among those still reachable bbs. */
365 else if (num != 0)
367 if (found == loop->num_nodes)
368 found = i;
369 i++;
370 continue;
372 else
374 found = i;
375 break;
378 update_stmt (stmt);
379 i++;
382 if (num != 0)
384 basic_block *tos, *worklist;
386 /* When called recursively, first do a quick discovery
387 of reachable bbs after the above changes and only
388 consider conditions in still reachable bbs. */
389 tos = worklist = XNEWVEC (basic_block, loop->num_nodes);
391 for (i = 0; i < loop->num_nodes; i++)
392 bbs[i]->flags &= ~BB_REACHABLE;
394 /* Start with marking header. */
395 *tos++ = bbs[0];
396 bbs[0]->flags |= BB_REACHABLE;
398 /* Iterate: find everything reachable from what we've already seen
399 within the same innermost loop. Don't look through false edges
400 if condition is always true or true edges if condition is
401 always false. */
402 while (tos != worklist)
404 basic_block b = *--tos;
405 edge e;
406 edge_iterator ei;
407 int flags = 0;
409 if (EDGE_COUNT (b->succs) == 2)
411 gimple *stmt = last_stmt (b);
412 if (stmt
413 && gimple_code (stmt) == GIMPLE_COND)
415 gcond *cond_stmt = as_a <gcond *> (stmt);
416 if (gimple_cond_true_p (cond_stmt))
417 flags = EDGE_FALSE_VALUE;
418 else if (gimple_cond_false_p (cond_stmt))
419 flags = EDGE_TRUE_VALUE;
423 FOR_EACH_EDGE (e, ei, b->succs)
425 basic_block dest = e->dest;
427 if (dest->loop_father == loop
428 && !(dest->flags & BB_REACHABLE)
429 && !(e->flags & flags))
431 *tos++ = dest;
432 dest->flags |= BB_REACHABLE;
437 free (worklist);
439 /* Find a bb to unswitch on. */
440 for (; found < loop->num_nodes; found++)
441 if ((bbs[found]->flags & BB_REACHABLE)
442 && (cond = tree_may_unswitch_on (bbs[found], loop)))
443 break;
445 if (found == loop->num_nodes)
447 free (bbs);
448 return changed;
452 if (dump_file && (dump_flags & TDF_DETAILS))
453 fprintf (dump_file, ";; Unswitching loop\n");
455 initialize_original_copy_tables ();
456 /* Unswitch the loop on this condition. */
457 nloop = tree_unswitch_loop (loop, bbs[found], cond);
458 if (!nloop)
460 free_original_copy_tables ();
461 free (bbs);
462 return changed;
465 /* Update the SSA form after unswitching. */
466 update_ssa (TODO_update_ssa);
467 free_original_copy_tables ();
469 /* Invoke itself on modified loops. */
470 tree_unswitch_single_loop (nloop, num + 1);
471 tree_unswitch_single_loop (loop, num + 1);
472 free (bbs);
473 return true;
476 /* Unswitch a LOOP w.r. to given basic block UNSWITCH_ON. We only support
477 unswitching of innermost loops. COND is the condition determining which
478 loop is entered -- the new loop is entered if COND is true. Returns NULL
479 if impossible, new loop otherwise. */
481 static struct loop *
482 tree_unswitch_loop (struct loop *loop,
483 basic_block unswitch_on, tree cond)
485 unsigned prob_true;
486 edge edge_true, edge_false;
488 /* Some sanity checking. */
489 gcc_assert (flow_bb_inside_loop_p (loop, unswitch_on));
490 gcc_assert (EDGE_COUNT (unswitch_on->succs) == 2);
491 gcc_assert (loop->inner == NULL);
493 extract_true_false_edges_from_block (unswitch_on, &edge_true, &edge_false);
494 prob_true = edge_true->probability;
495 return loop_version (loop, unshare_expr (cond),
496 NULL, prob_true, REG_BR_PROB_BASE - prob_true, prob_true,
497 REG_BR_PROB_BASE - prob_true, false);
500 /* Unswitch outer loops by hoisting invariant guard on
501 inner loop without code duplication. */
502 static bool
503 tree_unswitch_outer_loop (struct loop *loop)
505 edge exit, guard;
506 HOST_WIDE_INT iterations;
508 gcc_assert (loop->inner);
509 if (loop->inner->next)
510 return false;
511 /* Accept loops with single exit only which is not from inner loop. */
512 exit = single_exit (loop);
513 if (!exit || exit->src->loop_father != loop)
514 return false;
515 /* Check that phi argument of exit edge is not defined inside loop. */
516 if (!check_exit_phi (loop))
517 return false;
518 /* If the loop is not expected to iterate, there is no need
519 for unswitching. */
520 iterations = estimated_loop_iterations_int (loop);
521 if (iterations < 0)
522 iterations = likely_max_loop_iterations_int (loop);
523 if (iterations >= 0 && iterations <= 1)
525 if (dump_file && (dump_flags & TDF_DETAILS))
526 fprintf (dump_file, ";; Not unswitching, loop is not expected"
527 " to iterate\n");
528 return false;
531 bool changed = false;
532 while ((guard = find_loop_guard (loop)))
534 if (! changed)
535 rewrite_virtuals_into_loop_closed_ssa (loop);
536 hoist_guard (loop, guard);
537 changed = true;
539 return changed;
542 /* Checks if the body of the LOOP is within an invariant guard. If this
543 is the case, returns the edge that jumps over the real body of the loop,
544 otherwise returns NULL. */
546 static edge
547 find_loop_guard (struct loop *loop)
549 basic_block header = loop->header;
550 edge guard_edge, te, fe;
551 basic_block *body = NULL;
552 unsigned i;
553 tree use;
554 ssa_op_iter iter;
556 /* We check for the following situation:
558 while (1)
560 [header]]
561 loop_phi_nodes;
562 something1;
563 if (cond1)
564 body;
565 nvar = phi(orig, bvar) ... for all variables changed in body;
566 [guard_end]
567 something2;
568 if (cond2)
569 break;
570 something3;
573 where:
575 1) cond1 is loop invariant
576 2) If cond1 is false, then the loop is essentially empty; i.e.,
577 a) nothing in something1, something2 and something3 has side
578 effects
579 b) anything defined in something1, something2 and something3
580 is not used outside of the loop. */
582 gcond *cond;
585 if (single_succ_p (header))
586 header = single_succ (header);
587 else
589 cond = dyn_cast <gcond *> (last_stmt (header));
590 if (! cond)
591 return NULL;
592 extract_true_false_edges_from_block (header, &te, &fe);
593 /* Make sure to skip earlier hoisted guards that are left
594 in place as if (true). */
595 if (gimple_cond_true_p (cond))
596 header = te->dest;
597 else if (gimple_cond_false_p (cond))
598 header = fe->dest;
599 else
600 break;
603 while (1);
604 if (!flow_bb_inside_loop_p (loop, te->dest)
605 || !flow_bb_inside_loop_p (loop, fe->dest))
606 return NULL;
608 if (just_once_each_iteration_p (loop, te->dest)
609 || (single_succ_p (te->dest)
610 && just_once_each_iteration_p (loop, single_succ (te->dest))))
612 if (just_once_each_iteration_p (loop, fe->dest))
613 return NULL;
614 guard_edge = te;
616 else if (just_once_each_iteration_p (loop, fe->dest)
617 || (single_succ_p (fe->dest)
618 && just_once_each_iteration_p (loop, single_succ (fe->dest))))
619 guard_edge = fe;
620 else
621 return NULL;
623 /* Guard edge must skip inner loop. */
624 if (!dominated_by_p (CDI_DOMINATORS, loop->inner->header,
625 guard_edge == fe ? te->dest : fe->dest))
627 if (dump_file && (dump_flags & TDF_DETAILS))
628 fprintf (dump_file, "Guard edge %d --> %d is not around the loop!\n",
629 guard_edge->src->index, guard_edge->dest->index);
630 return NULL;
632 if (guard_edge->dest == loop->latch)
634 if (dump_file && (dump_flags & TDF_DETAILS))
635 fprintf (dump_file, "Guard edge destination is loop latch.\n");
636 return NULL;
639 if (dump_file && (dump_flags & TDF_DETAILS))
640 fprintf (dump_file,
641 "Considering guard %d -> %d in loop %d\n",
642 guard_edge->src->index, guard_edge->dest->index, loop->num);
643 /* Check if condition operands do not have definitions inside loop since
644 any bb copying is not performed. */
645 FOR_EACH_SSA_TREE_OPERAND (use, cond, iter, SSA_OP_USE)
647 gimple *def = SSA_NAME_DEF_STMT (use);
648 basic_block def_bb = gimple_bb (def);
649 if (def_bb
650 && flow_bb_inside_loop_p (loop, def_bb))
652 if (dump_file && (dump_flags & TDF_DETAILS))
653 fprintf (dump_file, " guard operands have definitions"
654 " inside loop\n");
655 return NULL;
659 body = get_loop_body (loop);
660 for (i = 0; i < loop->num_nodes; i++)
662 basic_block bb = body[i];
663 if (bb->loop_father != loop)
664 continue;
665 if (bb->flags & BB_IRREDUCIBLE_LOOP)
667 if (dump_file && (dump_flags & TDF_DETAILS))
668 fprintf (dump_file, "Block %d is marked as irreducible in loop\n",
669 bb->index);
670 guard_edge = NULL;
671 goto end;
673 if (!empty_bb_without_guard_p (loop, bb))
675 if (dump_file && (dump_flags & TDF_DETAILS))
676 fprintf (dump_file, " block %d has side effects\n", bb->index);
677 guard_edge = NULL;
678 goto end;
682 if (dump_file && (dump_flags & TDF_DETAILS))
683 fprintf (dump_file, " suitable to hoist\n");
684 end:
685 if (body)
686 free (body);
687 return guard_edge;
690 /* Returns true if
691 1) no statement in BB has side effects
692 2) assuming that edge GUARD is always taken, all definitions in BB
693 are noy used outside of the loop.
694 KNOWN_INVARIANTS is a set of ssa names we know to be invariant, and
695 PROCESSED is a set of ssa names for that we already tested whether they
696 are invariant or not. */
698 static bool
699 empty_bb_without_guard_p (struct loop *loop, basic_block bb)
701 basic_block exit_bb = single_exit (loop)->src;
702 bool may_be_used_outside = (bb == exit_bb
703 || !dominated_by_p (CDI_DOMINATORS, bb, exit_bb));
704 tree name;
705 ssa_op_iter op_iter;
707 /* Phi nodes do not have side effects, but their results might be used
708 outside of the loop. */
709 if (may_be_used_outside)
711 for (gphi_iterator gsi = gsi_start_phis (bb);
712 !gsi_end_p (gsi); gsi_next (&gsi))
714 gphi *phi = gsi.phi ();
715 name = PHI_RESULT (phi);
716 if (virtual_operand_p (name))
717 continue;
719 if (used_outside_loop_p (loop, name))
720 return false;
724 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
725 !gsi_end_p (gsi); gsi_next (&gsi))
727 gimple *stmt = gsi_stmt (gsi);
728 if (gimple_has_side_effects (stmt))
729 return false;
731 if (gimple_vdef(stmt))
732 return false;
734 FOR_EACH_SSA_TREE_OPERAND (name, stmt, op_iter, SSA_OP_DEF)
736 if (may_be_used_outside
737 && used_outside_loop_p (loop, name))
738 return false;
741 return true;
744 /* Return true if NAME is used outside of LOOP. */
746 static bool
747 used_outside_loop_p (struct loop *loop, tree name)
749 imm_use_iterator it;
750 use_operand_p use;
752 FOR_EACH_IMM_USE_FAST (use, it, name)
754 gimple *stmt = USE_STMT (use);
755 if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
756 return true;
759 return false;
762 /* Return argument for loop preheader edge in header virtual phi if any. */
764 static tree
765 get_vop_from_header (struct loop *loop)
767 for (gphi_iterator gsi = gsi_start_phis (loop->header);
768 !gsi_end_p (gsi); gsi_next (&gsi))
770 gphi *phi = gsi.phi ();
771 if (!virtual_operand_p (gimple_phi_result (phi)))
772 continue;
773 return PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
775 return NULL_TREE;
778 /* Move the check of GUARD outside of LOOP. */
780 static void
781 hoist_guard (struct loop *loop, edge guard)
783 edge exit = single_exit (loop);
784 edge preh = loop_preheader_edge (loop);
785 basic_block pre_header = preh->src;
786 basic_block bb;
787 edge te, fe, e, new_edge;
788 gimple *stmt;
789 basic_block guard_bb = guard->src;
790 gimple_stmt_iterator gsi;
791 int flags = 0;
792 bool fix_dom_of_exit;
793 gcond *cond_stmt, *new_cond_stmt;
795 bb = get_immediate_dominator (CDI_DOMINATORS, exit->dest);
796 fix_dom_of_exit = flow_bb_inside_loop_p (loop, bb);
797 gsi = gsi_last_bb (guard_bb);
798 stmt = gsi_stmt (gsi);
799 gcc_assert (gimple_code (stmt) == GIMPLE_COND);
800 cond_stmt = as_a <gcond *> (stmt);
801 extract_true_false_edges_from_block (guard_bb, &te, &fe);
802 /* Insert guard to PRE_HEADER. */
803 if (!empty_block_p (pre_header))
804 gsi = gsi_last_bb (pre_header);
805 else
806 gsi = gsi_start_bb (pre_header);
807 /* Create copy of COND_STMT. */
808 new_cond_stmt = gimple_build_cond (gimple_cond_code (cond_stmt),
809 gimple_cond_lhs (cond_stmt),
810 gimple_cond_rhs (cond_stmt),
811 NULL_TREE, NULL_TREE);
812 gsi_insert_after (&gsi, new_cond_stmt, GSI_NEW_STMT);
813 /* Convert COND_STMT to true/false conditional. */
814 if (guard == te)
815 gimple_cond_make_false (cond_stmt);
816 else
817 gimple_cond_make_true (cond_stmt);
818 update_stmt (cond_stmt);
819 /* Create new loop pre-header. */
820 e = split_block (pre_header, last_stmt (pre_header));
821 gcc_assert (loop_preheader_edge (loop)->src == e->dest);
822 if (guard == fe)
824 e->flags = EDGE_TRUE_VALUE;
825 flags |= EDGE_FALSE_VALUE;
827 else
829 e->flags = EDGE_FALSE_VALUE;
830 flags |= EDGE_TRUE_VALUE;
832 new_edge = make_edge (pre_header, exit->dest, flags);
833 if (fix_dom_of_exit)
834 set_immediate_dominator (CDI_DOMINATORS, exit->dest, pre_header);
835 /* Add NEW_ADGE argument for all phi in post-header block. */
836 bb = exit->dest;
837 for (gphi_iterator gsi = gsi_start_phis (bb);
838 !gsi_end_p (gsi); gsi_next (&gsi))
840 gphi *phi = gsi.phi ();
841 tree arg;
842 if (virtual_operand_p (gimple_phi_result (phi)))
844 arg = get_vop_from_header (loop);
845 if (arg == NULL_TREE)
846 /* Use exit edge argument. */
847 arg = PHI_ARG_DEF_FROM_EDGE (phi, exit);
848 add_phi_arg (phi, arg, new_edge, UNKNOWN_LOCATION);
850 else
852 /* Use exit edge argument. */
853 arg = PHI_ARG_DEF_FROM_EDGE (phi, exit);
854 add_phi_arg (phi, arg, new_edge, UNKNOWN_LOCATION);
858 if (dump_file && (dump_flags & TDF_DETAILS))
859 fprintf (dump_file, " guard hoisted.\n");
862 /* Return true if phi argument for exit edge can be used
863 for edge around loop. */
865 static bool
866 check_exit_phi (struct loop *loop)
868 edge exit = single_exit (loop);
869 basic_block pre_header = loop_preheader_edge (loop)->src;
871 for (gphi_iterator gsi = gsi_start_phis (exit->dest);
872 !gsi_end_p (gsi); gsi_next (&gsi))
874 gphi *phi = gsi.phi ();
875 tree arg;
876 gimple *def;
877 basic_block def_bb;
878 if (virtual_operand_p (gimple_phi_result (phi)))
879 continue;
880 arg = PHI_ARG_DEF_FROM_EDGE (phi, exit);
881 if (TREE_CODE (arg) != SSA_NAME)
882 continue;
883 def = SSA_NAME_DEF_STMT (arg);
884 if (!def)
885 continue;
886 def_bb = gimple_bb (def);
887 if (!def_bb)
888 continue;
889 if (!dominated_by_p (CDI_DOMINATORS, pre_header, def_bb))
890 /* Definition inside loop! */
891 return false;
892 /* Check loop closed phi invariant. */
893 if (!flow_bb_inside_loop_p (def_bb->loop_father, pre_header))
894 return false;
896 return true;
899 /* Loop unswitching pass. */
901 namespace {
903 const pass_data pass_data_tree_unswitch =
905 GIMPLE_PASS, /* type */
906 "unswitch", /* name */
907 OPTGROUP_LOOP, /* optinfo_flags */
908 TV_TREE_LOOP_UNSWITCH, /* tv_id */
909 PROP_cfg, /* properties_required */
910 0, /* properties_provided */
911 0, /* properties_destroyed */
912 0, /* todo_flags_start */
913 0, /* todo_flags_finish */
916 class pass_tree_unswitch : public gimple_opt_pass
918 public:
919 pass_tree_unswitch (gcc::context *ctxt)
920 : gimple_opt_pass (pass_data_tree_unswitch, ctxt)
923 /* opt_pass methods: */
924 virtual bool gate (function *) { return flag_unswitch_loops != 0; }
925 virtual unsigned int execute (function *);
927 }; // class pass_tree_unswitch
929 unsigned int
930 pass_tree_unswitch::execute (function *fun)
932 if (number_of_loops (fun) <= 1)
933 return 0;
935 return tree_ssa_unswitch_loops ();
938 } // anon namespace
940 gimple_opt_pass *
941 make_pass_tree_unswitch (gcc::context *ctxt)
943 return new pass_tree_unswitch (ctxt);