2012-07-06 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / tree-if-conv.c
blob107c7e3da127d5e93bedd864a729a47c4e5433cf
1 /* If-conversion for vectorizer.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Devang Patel <dpatel@apple.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This pass implements a tree level if-conversion of loops. Its
23 initial goal is to help the vectorizer to vectorize loops with
24 conditions.
26 A short description of if-conversion:
28 o Decide if a loop is if-convertible or not.
29 o Walk all loop basic blocks in breadth first order (BFS order).
30 o Remove conditional statements (at the end of basic block)
31 and propagate condition into destination basic blocks'
32 predicate list.
33 o Replace modify expression with conditional modify expression
34 using current basic block's condition.
35 o Merge all basic blocks
36 o Replace phi nodes with conditional modify expr
37 o Merge all basic blocks into header
39 Sample transformation:
41 INPUT
42 -----
44 # i_23 = PHI <0(0), i_18(10)>;
45 <L0>:;
46 j_15 = A[i_23];
47 if (j_15 > 41) goto <L1>; else goto <L17>;
49 <L17>:;
50 goto <bb 3> (<L3>);
52 <L1>:;
54 # iftmp.2_4 = PHI <0(8), 42(2)>;
55 <L3>:;
56 A[i_23] = iftmp.2_4;
57 i_18 = i_23 + 1;
58 if (i_18 <= 15) goto <L19>; else goto <L18>;
60 <L19>:;
61 goto <bb 1> (<L0>);
63 <L18>:;
65 OUTPUT
66 ------
68 # i_23 = PHI <0(0), i_18(10)>;
69 <L0>:;
70 j_15 = A[i_23];
72 <L3>:;
73 iftmp.2_4 = j_15 > 41 ? 42 : 0;
74 A[i_23] = iftmp.2_4;
75 i_18 = i_23 + 1;
76 if (i_18 <= 15) goto <L19>; else goto <L18>;
78 <L19>:;
79 goto <bb 1> (<L0>);
81 <L18>:;
84 #include "config.h"
85 #include "system.h"
86 #include "coretypes.h"
87 #include "tm.h"
88 #include "tree.h"
89 #include "flags.h"
90 #include "timevar.h"
91 #include "basic-block.h"
92 #include "tree-pretty-print.h"
93 #include "gimple-pretty-print.h"
94 #include "tree-flow.h"
95 #include "tree-dump.h"
96 #include "cfgloop.h"
97 #include "tree-chrec.h"
98 #include "tree-data-ref.h"
99 #include "tree-scalar-evolution.h"
100 #include "tree-pass.h"
101 #include "dbgcnt.h"
103 /* List of basic blocks in if-conversion-suitable order. */
104 static basic_block *ifc_bbs;
106 /* Structure used to predicate basic blocks. This is attached to the
107 ->aux field of the BBs in the loop to be if-converted. */
108 typedef struct bb_predicate_s {
110 /* The condition under which this basic block is executed. */
111 tree predicate;
113 /* PREDICATE is gimplified, and the sequence of statements is
114 recorded here, in order to avoid the duplication of computations
115 that occur in previous conditions. See PR44483. */
116 gimple_seq predicate_gimplified_stmts;
117 } *bb_predicate_p;
119 /* Returns true when the basic block BB has a predicate. */
121 static inline bool
122 bb_has_predicate (basic_block bb)
124 return bb->aux != NULL;
127 /* Returns the gimplified predicate for basic block BB. */
129 static inline tree
130 bb_predicate (basic_block bb)
132 return ((bb_predicate_p) bb->aux)->predicate;
135 /* Sets the gimplified predicate COND for basic block BB. */
137 static inline void
138 set_bb_predicate (basic_block bb, tree cond)
140 gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
141 && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
142 || is_gimple_condexpr (cond));
143 ((bb_predicate_p) bb->aux)->predicate = cond;
146 /* Returns the sequence of statements of the gimplification of the
147 predicate for basic block BB. */
149 static inline gimple_seq
150 bb_predicate_gimplified_stmts (basic_block bb)
152 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
155 /* Sets the sequence of statements STMTS of the gimplification of the
156 predicate for basic block BB. */
158 static inline void
159 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
161 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
164 /* Adds the sequence of statements STMTS to the sequence of statements
165 of the predicate for basic block BB. */
167 static inline void
168 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
170 gimple_seq_add_seq
171 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
174 /* Initializes to TRUE the predicate of basic block BB. */
176 static inline void
177 init_bb_predicate (basic_block bb)
179 bb->aux = XNEW (struct bb_predicate_s);
180 set_bb_predicate_gimplified_stmts (bb, NULL);
181 set_bb_predicate (bb, boolean_true_node);
184 /* Free the predicate of basic block BB. */
186 static inline void
187 free_bb_predicate (basic_block bb)
189 gimple_seq stmts;
191 if (!bb_has_predicate (bb))
192 return;
194 /* Release the SSA_NAMEs created for the gimplification of the
195 predicate. */
196 stmts = bb_predicate_gimplified_stmts (bb);
197 if (stmts)
199 gimple_stmt_iterator i;
201 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
202 free_stmt_operands (gsi_stmt (i));
205 free (bb->aux);
206 bb->aux = NULL;
209 /* Free the predicate of BB and reinitialize it with the true
210 predicate. */
212 static inline void
213 reset_bb_predicate (basic_block bb)
215 free_bb_predicate (bb);
216 init_bb_predicate (bb);
219 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
220 the expression EXPR. Inserts the statement created for this
221 computation before GSI and leaves the iterator GSI at the same
222 statement. */
224 static tree
225 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
227 const char *name = "_ifc_";
228 tree var, new_name;
229 gimple stmt;
231 /* Create new temporary variable. */
232 var = create_tmp_var (type, name);
233 add_referenced_var (var);
235 /* Build new statement to assign EXPR to new variable. */
236 stmt = gimple_build_assign (var, expr);
238 /* Get SSA name for the new variable and set make new statement
239 its definition statement. */
240 new_name = make_ssa_name (var, stmt);
241 gimple_assign_set_lhs (stmt, new_name);
242 SSA_NAME_DEF_STMT (new_name) = stmt;
243 update_stmt (stmt);
245 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
246 return gimple_assign_lhs (stmt);
249 /* Return true when COND is a true predicate. */
251 static inline bool
252 is_true_predicate (tree cond)
254 return (cond == NULL_TREE
255 || cond == boolean_true_node
256 || integer_onep (cond));
259 /* Returns true when BB has a predicate that is not trivial: true or
260 NULL_TREE. */
262 static inline bool
263 is_predicated (basic_block bb)
265 return !is_true_predicate (bb_predicate (bb));
268 /* Parses the predicate COND and returns its comparison code and
269 operands OP0 and OP1. */
271 static enum tree_code
272 parse_predicate (tree cond, tree *op0, tree *op1)
274 gimple s;
276 if (TREE_CODE (cond) == SSA_NAME
277 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
279 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
281 *op0 = gimple_assign_rhs1 (s);
282 *op1 = gimple_assign_rhs2 (s);
283 return gimple_assign_rhs_code (s);
286 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
288 tree op = gimple_assign_rhs1 (s);
289 tree type = TREE_TYPE (op);
290 enum tree_code code = parse_predicate (op, op0, op1);
292 return code == ERROR_MARK ? ERROR_MARK
293 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
296 return ERROR_MARK;
299 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
301 *op0 = TREE_OPERAND (cond, 0);
302 *op1 = TREE_OPERAND (cond, 1);
303 return TREE_CODE (cond);
306 return ERROR_MARK;
309 /* Returns the fold of predicate C1 OR C2 at location LOC. */
311 static tree
312 fold_or_predicates (location_t loc, tree c1, tree c2)
314 tree op1a, op1b, op2a, op2b;
315 enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
316 enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
318 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
320 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
321 code2, op2a, op2b);
322 if (t)
323 return t;
326 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
329 /* Add condition NC to the predicate list of basic block BB. */
331 static inline void
332 add_to_predicate_list (basic_block bb, tree nc)
334 tree bc, *tp;
336 if (is_true_predicate (nc))
337 return;
339 if (!is_predicated (bb))
340 bc = nc;
341 else
343 bc = bb_predicate (bb);
344 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
345 if (is_true_predicate (bc))
347 reset_bb_predicate (bb);
348 return;
352 /* Allow a TRUTH_NOT_EXPR around the main predicate. */
353 if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
354 tp = &TREE_OPERAND (bc, 0);
355 else
356 tp = &bc;
357 if (!is_gimple_condexpr (*tp))
359 gimple_seq stmts;
360 *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
361 add_bb_predicate_gimplified_stmts (bb, stmts);
363 set_bb_predicate (bb, bc);
366 /* Add the condition COND to the previous condition PREV_COND, and add
367 this to the predicate list of the destination of edge E. LOOP is
368 the loop to be if-converted. */
370 static void
371 add_to_dst_predicate_list (struct loop *loop, edge e,
372 tree prev_cond, tree cond)
374 if (!flow_bb_inside_loop_p (loop, e->dest))
375 return;
377 if (!is_true_predicate (prev_cond))
378 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
379 prev_cond, cond);
381 add_to_predicate_list (e->dest, cond);
384 /* Return true if one of the successor edges of BB exits LOOP. */
386 static bool
387 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
389 edge e;
390 edge_iterator ei;
392 FOR_EACH_EDGE (e, ei, bb->succs)
393 if (loop_exit_edge_p (loop, e))
394 return true;
396 return false;
399 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
400 and it belongs to basic block BB.
402 PHI is not if-convertible if:
403 - it has more than 2 arguments.
405 When the flag_tree_loop_if_convert_stores is not set, PHI is not
406 if-convertible if:
407 - a virtual PHI is immediately used in another PHI node,
408 - there is a virtual PHI in a BB other than the loop->header. */
410 static bool
411 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
413 if (dump_file && (dump_flags & TDF_DETAILS))
415 fprintf (dump_file, "-------------------------\n");
416 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
419 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
421 if (dump_file && (dump_flags & TDF_DETAILS))
422 fprintf (dump_file, "More than two phi node args.\n");
423 return false;
426 if (flag_tree_loop_if_convert_stores)
427 return true;
429 /* When the flag_tree_loop_if_convert_stores is not set, check
430 that there are no memory writes in the branches of the loop to be
431 if-converted. */
432 if (!is_gimple_reg (SSA_NAME_VAR (gimple_phi_result (phi))))
434 imm_use_iterator imm_iter;
435 use_operand_p use_p;
437 if (bb != loop->header)
439 if (dump_file && (dump_flags & TDF_DETAILS))
440 fprintf (dump_file, "Virtual phi not on loop->header.\n");
441 return false;
444 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
446 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
448 if (dump_file && (dump_flags & TDF_DETAILS))
449 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
450 return false;
455 return true;
458 /* Records the status of a data reference. This struct is attached to
459 each DR->aux field. */
461 struct ifc_dr {
462 /* -1 when not initialized, 0 when false, 1 when true. */
463 int written_at_least_once;
465 /* -1 when not initialized, 0 when false, 1 when true. */
466 int rw_unconditionally;
469 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
470 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
471 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
473 /* Returns true when the memory references of STMT are read or written
474 unconditionally. In other words, this function returns true when
475 for every data reference A in STMT there exist other accesses to
476 a data reference with the same base with predicates that add up (OR-up) to
477 the true predicate: this ensures that the data reference A is touched
478 (read or written) on every iteration of the if-converted loop. */
480 static bool
481 memrefs_read_or_written_unconditionally (gimple stmt,
482 VEC (data_reference_p, heap) *drs)
484 int i, j;
485 data_reference_p a, b;
486 tree ca = bb_predicate (gimple_bb (stmt));
488 for (i = 0; VEC_iterate (data_reference_p, drs, i, a); i++)
489 if (DR_STMT (a) == stmt)
491 bool found = false;
492 int x = DR_RW_UNCONDITIONALLY (a);
494 if (x == 0)
495 return false;
497 if (x == 1)
498 continue;
500 for (j = 0; VEC_iterate (data_reference_p, drs, j, b); j++)
502 tree ref_base_a = DR_REF (a);
503 tree ref_base_b = DR_REF (b);
505 if (DR_STMT (b) == stmt)
506 continue;
508 while (TREE_CODE (ref_base_a) == COMPONENT_REF
509 || TREE_CODE (ref_base_a) == IMAGPART_EXPR
510 || TREE_CODE (ref_base_a) == REALPART_EXPR)
511 ref_base_a = TREE_OPERAND (ref_base_a, 0);
513 while (TREE_CODE (ref_base_b) == COMPONENT_REF
514 || TREE_CODE (ref_base_b) == IMAGPART_EXPR
515 || TREE_CODE (ref_base_b) == REALPART_EXPR)
516 ref_base_b = TREE_OPERAND (ref_base_b, 0);
518 if (!operand_equal_p (ref_base_a, ref_base_b, 0))
520 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
522 if (DR_RW_UNCONDITIONALLY (b) == 1
523 || is_true_predicate (cb)
524 || is_true_predicate (ca
525 = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
527 DR_RW_UNCONDITIONALLY (a) = 1;
528 DR_RW_UNCONDITIONALLY (b) = 1;
529 found = true;
530 break;
535 if (!found)
537 DR_RW_UNCONDITIONALLY (a) = 0;
538 return false;
542 return true;
545 /* Returns true when the memory references of STMT are unconditionally
546 written. In other words, this function returns true when for every
547 data reference A written in STMT, there exist other writes to the
548 same data reference with predicates that add up (OR-up) to the true
549 predicate: this ensures that the data reference A is written on
550 every iteration of the if-converted loop. */
552 static bool
553 write_memrefs_written_at_least_once (gimple stmt,
554 VEC (data_reference_p, heap) *drs)
556 int i, j;
557 data_reference_p a, b;
558 tree ca = bb_predicate (gimple_bb (stmt));
560 for (i = 0; VEC_iterate (data_reference_p, drs, i, a); i++)
561 if (DR_STMT (a) == stmt
562 && DR_IS_WRITE (a))
564 bool found = false;
565 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
567 if (x == 0)
568 return false;
570 if (x == 1)
571 continue;
573 for (j = 0; VEC_iterate (data_reference_p, drs, j, b); j++)
574 if (DR_STMT (b) != stmt
575 && DR_IS_WRITE (b)
576 && same_data_refs_base_objects (a, b))
578 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
580 if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
581 || is_true_predicate (cb)
582 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
583 ca, cb)))
585 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
586 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
587 found = true;
588 break;
592 if (!found)
594 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
595 return false;
599 return true;
602 /* Return true when the memory references of STMT won't trap in the
603 if-converted code. There are two things that we have to check for:
605 - writes to memory occur to writable memory: if-conversion of
606 memory writes transforms the conditional memory writes into
607 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
608 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
609 be executed at all in the original code, it may be a readonly
610 memory. To check that A is not const-qualified, we check that
611 there exists at least an unconditional write to A in the current
612 function.
614 - reads or writes to memory are valid memory accesses for every
615 iteration. To check that the memory accesses are correctly formed
616 and that we are allowed to read and write in these locations, we
617 check that the memory accesses to be if-converted occur at every
618 iteration unconditionally. */
620 static bool
621 ifcvt_memrefs_wont_trap (gimple stmt, VEC (data_reference_p, heap) *refs)
623 return write_memrefs_written_at_least_once (stmt, refs)
624 && memrefs_read_or_written_unconditionally (stmt, refs);
627 /* Wrapper around gimple_could_trap_p refined for the needs of the
628 if-conversion. Try to prove that the memory accesses of STMT could
629 not trap in the innermost loop containing STMT. */
631 static bool
632 ifcvt_could_trap_p (gimple stmt, VEC (data_reference_p, heap) *refs)
634 if (gimple_vuse (stmt)
635 && !gimple_could_trap_p_1 (stmt, false, false)
636 && ifcvt_memrefs_wont_trap (stmt, refs))
637 return false;
639 return gimple_could_trap_p (stmt);
642 /* Return true when STMT is if-convertible.
644 GIMPLE_ASSIGN statement is not if-convertible if,
645 - it is not movable,
646 - it could trap,
647 - LHS is not var decl. */
649 static bool
650 if_convertible_gimple_assign_stmt_p (gimple stmt,
651 VEC (data_reference_p, heap) *refs)
653 tree lhs = gimple_assign_lhs (stmt);
654 basic_block bb;
656 if (dump_file && (dump_flags & TDF_DETAILS))
658 fprintf (dump_file, "-------------------------\n");
659 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
662 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
663 return false;
665 /* Some of these constrains might be too conservative. */
666 if (stmt_ends_bb_p (stmt)
667 || gimple_has_volatile_ops (stmt)
668 || (TREE_CODE (lhs) == SSA_NAME
669 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
670 || gimple_has_side_effects (stmt))
672 if (dump_file && (dump_flags & TDF_DETAILS))
673 fprintf (dump_file, "stmt not suitable for ifcvt\n");
674 return false;
677 if (flag_tree_loop_if_convert_stores)
679 if (ifcvt_could_trap_p (stmt, refs))
681 if (dump_file && (dump_flags & TDF_DETAILS))
682 fprintf (dump_file, "tree could trap...\n");
683 return false;
685 return true;
688 if (gimple_assign_rhs_could_trap_p (stmt))
690 if (dump_file && (dump_flags & TDF_DETAILS))
691 fprintf (dump_file, "tree could trap...\n");
692 return false;
695 bb = gimple_bb (stmt);
697 if (TREE_CODE (lhs) != SSA_NAME
698 && bb != bb->loop_father->header
699 && !bb_with_exit_edge_p (bb->loop_father, bb))
701 if (dump_file && (dump_flags & TDF_DETAILS))
703 fprintf (dump_file, "LHS is not var\n");
704 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
706 return false;
709 return true;
712 /* Return true when STMT is if-convertible.
714 A statement is if-convertible if:
715 - it is an if-convertible GIMPLE_ASSIGN,
716 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
718 static bool
719 if_convertible_stmt_p (gimple stmt, VEC (data_reference_p, heap) *refs)
721 switch (gimple_code (stmt))
723 case GIMPLE_LABEL:
724 case GIMPLE_DEBUG:
725 case GIMPLE_COND:
726 return true;
728 case GIMPLE_ASSIGN:
729 return if_convertible_gimple_assign_stmt_p (stmt, refs);
731 case GIMPLE_CALL:
733 tree fndecl = gimple_call_fndecl (stmt);
734 if (fndecl)
736 int flags = gimple_call_flags (stmt);
737 if ((flags & ECF_CONST)
738 && !(flags & ECF_LOOPING_CONST_OR_PURE)
739 /* We can only vectorize some builtins at the moment,
740 so restrict if-conversion to those. */
741 && DECL_BUILT_IN (fndecl))
742 return true;
744 return false;
747 default:
748 /* Don't know what to do with 'em so don't do anything. */
749 if (dump_file && (dump_flags & TDF_DETAILS))
751 fprintf (dump_file, "don't know what to do\n");
752 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
754 return false;
755 break;
758 return true;
761 /* Return true when BB post-dominates all its predecessors. */
763 static bool
764 bb_postdominates_preds (basic_block bb)
766 unsigned i;
768 for (i = 0; i < EDGE_COUNT (bb->preds); i++)
769 if (!dominated_by_p (CDI_POST_DOMINATORS, EDGE_PRED (bb, i)->src, bb))
770 return false;
772 return true;
775 /* Return true when BB is if-convertible. This routine does not check
776 basic block's statements and phis.
778 A basic block is not if-convertible if:
779 - it is non-empty and it is after the exit block (in BFS order),
780 - it is after the exit block but before the latch,
781 - its edges are not normal.
783 EXIT_BB is the basic block containing the exit of the LOOP. BB is
784 inside LOOP. */
786 static bool
787 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
789 edge e;
790 edge_iterator ei;
792 if (dump_file && (dump_flags & TDF_DETAILS))
793 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
795 if (EDGE_COUNT (bb->preds) > 2
796 || EDGE_COUNT (bb->succs) > 2)
797 return false;
799 if (exit_bb)
801 if (bb != loop->latch)
803 if (dump_file && (dump_flags & TDF_DETAILS))
804 fprintf (dump_file, "basic block after exit bb but before latch\n");
805 return false;
807 else if (!empty_block_p (bb))
809 if (dump_file && (dump_flags & TDF_DETAILS))
810 fprintf (dump_file, "non empty basic block after exit bb\n");
811 return false;
813 else if (bb == loop->latch
814 && bb != exit_bb
815 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
817 if (dump_file && (dump_flags & TDF_DETAILS))
818 fprintf (dump_file, "latch is not dominated by exit_block\n");
819 return false;
823 /* Be less adventurous and handle only normal edges. */
824 FOR_EACH_EDGE (e, ei, bb->succs)
825 if (e->flags &
826 (EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
828 if (dump_file && (dump_flags & TDF_DETAILS))
829 fprintf (dump_file, "Difficult to handle edges\n");
830 return false;
833 if (EDGE_COUNT (bb->preds) == 2
834 && bb != loop->header
835 && !bb_postdominates_preds (bb))
836 return false;
838 return true;
841 /* Return true when all predecessor blocks of BB are visited. The
842 VISITED bitmap keeps track of the visited blocks. */
844 static bool
845 pred_blocks_visited_p (basic_block bb, bitmap *visited)
847 edge e;
848 edge_iterator ei;
849 FOR_EACH_EDGE (e, ei, bb->preds)
850 if (!bitmap_bit_p (*visited, e->src->index))
851 return false;
853 return true;
856 /* Get body of a LOOP in suitable order for if-conversion. It is
857 caller's responsibility to deallocate basic block list.
858 If-conversion suitable order is, breadth first sort (BFS) order
859 with an additional constraint: select a block only if all its
860 predecessors are already selected. */
862 static basic_block *
863 get_loop_body_in_if_conv_order (const struct loop *loop)
865 basic_block *blocks, *blocks_in_bfs_order;
866 basic_block bb;
867 bitmap visited;
868 unsigned int index = 0;
869 unsigned int visited_count = 0;
871 gcc_assert (loop->num_nodes);
872 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
874 blocks = XCNEWVEC (basic_block, loop->num_nodes);
875 visited = BITMAP_ALLOC (NULL);
877 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
879 index = 0;
880 while (index < loop->num_nodes)
882 bb = blocks_in_bfs_order [index];
884 if (bb->flags & BB_IRREDUCIBLE_LOOP)
886 free (blocks_in_bfs_order);
887 BITMAP_FREE (visited);
888 free (blocks);
889 return NULL;
892 if (!bitmap_bit_p (visited, bb->index))
894 if (pred_blocks_visited_p (bb, &visited)
895 || bb == loop->header)
897 /* This block is now visited. */
898 bitmap_set_bit (visited, bb->index);
899 blocks[visited_count++] = bb;
903 index++;
905 if (index == loop->num_nodes
906 && visited_count != loop->num_nodes)
907 /* Not done yet. */
908 index = 0;
910 free (blocks_in_bfs_order);
911 BITMAP_FREE (visited);
912 return blocks;
915 /* Returns true when the analysis of the predicates for all the basic
916 blocks in LOOP succeeded.
918 predicate_bbs first allocates the predicates of the basic blocks.
919 These fields are then initialized with the tree expressions
920 representing the predicates under which a basic block is executed
921 in the LOOP. As the loop->header is executed at each iteration, it
922 has the "true" predicate. Other statements executed under a
923 condition are predicated with that condition, for example
925 | if (x)
926 | S1;
927 | else
928 | S2;
930 S1 will be predicated with "x", and
931 S2 will be predicated with "!x". */
933 static bool
934 predicate_bbs (loop_p loop)
936 unsigned int i;
938 for (i = 0; i < loop->num_nodes; i++)
939 init_bb_predicate (ifc_bbs[i]);
941 for (i = 0; i < loop->num_nodes; i++)
943 basic_block bb = ifc_bbs[i];
944 tree cond;
945 gimple_stmt_iterator itr;
947 /* The loop latch is always executed and has no extra conditions
948 to be processed: skip it. */
949 if (bb == loop->latch)
951 reset_bb_predicate (loop->latch);
952 continue;
955 cond = bb_predicate (bb);
957 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
959 gimple stmt = gsi_stmt (itr);
961 switch (gimple_code (stmt))
963 case GIMPLE_LABEL:
964 case GIMPLE_ASSIGN:
965 case GIMPLE_CALL:
966 case GIMPLE_DEBUG:
967 break;
969 case GIMPLE_COND:
971 tree c2;
972 edge true_edge, false_edge;
973 location_t loc = gimple_location (stmt);
974 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
975 boolean_type_node,
976 gimple_cond_lhs (stmt),
977 gimple_cond_rhs (stmt));
979 /* Add new condition into destination's predicate list. */
980 extract_true_false_edges_from_block (gimple_bb (stmt),
981 &true_edge, &false_edge);
983 /* If C is true, then TRUE_EDGE is taken. */
984 add_to_dst_predicate_list (loop, true_edge,
985 unshare_expr (cond),
986 unshare_expr (c));
988 /* If C is false, then FALSE_EDGE is taken. */
989 c2 = build1_loc (loc, TRUTH_NOT_EXPR,
990 boolean_type_node, unshare_expr (c));
991 add_to_dst_predicate_list (loop, false_edge,
992 unshare_expr (cond), c2);
994 cond = NULL_TREE;
995 break;
998 default:
999 /* Not handled yet in if-conversion. */
1000 return false;
1004 /* If current bb has only one successor, then consider it as an
1005 unconditional goto. */
1006 if (single_succ_p (bb))
1008 basic_block bb_n = single_succ (bb);
1010 /* The successor bb inherits the predicate of its
1011 predecessor. If there is no predicate in the predecessor
1012 bb, then consider the successor bb as always executed. */
1013 if (cond == NULL_TREE)
1014 cond = boolean_true_node;
1016 add_to_predicate_list (bb_n, cond);
1020 /* The loop header is always executed. */
1021 reset_bb_predicate (loop->header);
1022 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1023 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1025 return true;
1028 /* Return true when LOOP is if-convertible. This is a helper function
1029 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1030 in if_convertible_loop_p. */
1032 static bool
1033 if_convertible_loop_p_1 (struct loop *loop,
1034 VEC (loop_p, heap) **loop_nest,
1035 VEC (data_reference_p, heap) **refs,
1036 VEC (ddr_p, heap) **ddrs)
1038 bool res;
1039 unsigned int i;
1040 basic_block exit_bb = NULL;
1042 /* Don't if-convert the loop when the data dependences cannot be
1043 computed: the loop won't be vectorized in that case. */
1044 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1045 if (!res)
1046 return false;
1048 calculate_dominance_info (CDI_DOMINATORS);
1049 calculate_dominance_info (CDI_POST_DOMINATORS);
1051 /* Allow statements that can be handled during if-conversion. */
1052 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1053 if (!ifc_bbs)
1055 if (dump_file && (dump_flags & TDF_DETAILS))
1056 fprintf (dump_file, "Irreducible loop\n");
1057 return false;
1060 for (i = 0; i < loop->num_nodes; i++)
1062 basic_block bb = ifc_bbs[i];
1064 if (!if_convertible_bb_p (loop, bb, exit_bb))
1065 return false;
1067 if (bb_with_exit_edge_p (loop, bb))
1068 exit_bb = bb;
1071 res = predicate_bbs (loop);
1072 if (!res)
1073 return false;
1075 if (flag_tree_loop_if_convert_stores)
1077 data_reference_p dr;
1079 for (i = 0; VEC_iterate (data_reference_p, *refs, i, dr); i++)
1081 dr->aux = XNEW (struct ifc_dr);
1082 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1083 DR_RW_UNCONDITIONALLY (dr) = -1;
1087 for (i = 0; i < loop->num_nodes; i++)
1089 basic_block bb = ifc_bbs[i];
1090 gimple_stmt_iterator itr;
1092 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1093 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1094 return false;
1096 /* Check the if-convertibility of statements in predicated BBs. */
1097 if (is_predicated (bb))
1098 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1099 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1100 return false;
1103 if (dump_file)
1104 fprintf (dump_file, "Applying if-conversion\n");
1106 return true;
1109 /* Return true when LOOP is if-convertible.
1110 LOOP is if-convertible if:
1111 - it is innermost,
1112 - it has two or more basic blocks,
1113 - it has only one exit,
1114 - loop header is not the exit edge,
1115 - if its basic blocks and phi nodes are if convertible. */
1117 static bool
1118 if_convertible_loop_p (struct loop *loop)
1120 edge e;
1121 edge_iterator ei;
1122 bool res = false;
1123 VEC (data_reference_p, heap) *refs;
1124 VEC (ddr_p, heap) *ddrs;
1125 VEC (loop_p, heap) *loop_nest;
1127 /* Handle only innermost loop. */
1128 if (!loop || loop->inner)
1130 if (dump_file && (dump_flags & TDF_DETAILS))
1131 fprintf (dump_file, "not innermost loop\n");
1132 return false;
1135 /* If only one block, no need for if-conversion. */
1136 if (loop->num_nodes <= 2)
1138 if (dump_file && (dump_flags & TDF_DETAILS))
1139 fprintf (dump_file, "less than 2 basic blocks\n");
1140 return false;
1143 /* More than one loop exit is too much to handle. */
1144 if (!single_exit (loop))
1146 if (dump_file && (dump_flags & TDF_DETAILS))
1147 fprintf (dump_file, "multiple exits\n");
1148 return false;
1151 /* If one of the loop header's edge is an exit edge then do not
1152 apply if-conversion. */
1153 FOR_EACH_EDGE (e, ei, loop->header->succs)
1154 if (loop_exit_edge_p (loop, e))
1155 return false;
1157 refs = VEC_alloc (data_reference_p, heap, 5);
1158 ddrs = VEC_alloc (ddr_p, heap, 25);
1159 loop_nest = VEC_alloc (loop_p, heap, 3);
1160 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1162 if (flag_tree_loop_if_convert_stores)
1164 data_reference_p dr;
1165 unsigned int i;
1167 for (i = 0; VEC_iterate (data_reference_p, refs, i, dr); i++)
1168 free (dr->aux);
1171 VEC_free (loop_p, heap, loop_nest);
1172 free_data_refs (refs);
1173 free_dependence_relations (ddrs);
1174 return res;
1177 /* Basic block BB has two predecessors. Using predecessor's bb
1178 predicate, set an appropriate condition COND for the PHI node
1179 replacement. Return the true block whose phi arguments are
1180 selected when cond is true. LOOP is the loop containing the
1181 if-converted region, GSI is the place to insert the code for the
1182 if-conversion. */
1184 static basic_block
1185 find_phi_replacement_condition (struct loop *loop,
1186 basic_block bb, tree *cond,
1187 gimple_stmt_iterator *gsi)
1189 edge first_edge, second_edge;
1190 tree tmp_cond;
1192 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1193 first_edge = EDGE_PRED (bb, 0);
1194 second_edge = EDGE_PRED (bb, 1);
1196 /* Use condition based on following criteria:
1198 S1: x = !c ? a : b;
1200 S2: x = c ? b : a;
1202 S2 is preferred over S1. Make 'b' first_bb and use its condition.
1204 2) Do not make loop header first_bb.
1207 S1: x = !(c == d)? a : b;
1209 S21: t1 = c == d;
1210 S22: x = t1 ? b : a;
1212 S3: x = (c == d) ? b : a;
1214 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
1215 its condition.
1217 4) If pred B is dominated by pred A then use pred B's condition.
1218 See PR23115. */
1220 /* Select condition that is not TRUTH_NOT_EXPR. */
1221 tmp_cond = bb_predicate (first_edge->src);
1222 gcc_assert (tmp_cond);
1224 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1226 edge tmp_edge;
1228 tmp_edge = first_edge;
1229 first_edge = second_edge;
1230 second_edge = tmp_edge;
1233 /* Check if FIRST_BB is loop header or not and make sure that
1234 FIRST_BB does not dominate SECOND_BB. */
1235 if (first_edge->src == loop->header
1236 || dominated_by_p (CDI_DOMINATORS,
1237 second_edge->src, first_edge->src))
1239 *cond = bb_predicate (second_edge->src);
1241 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1242 *cond = TREE_OPERAND (*cond, 0);
1243 else
1244 /* Select non loop header bb. */
1245 first_edge = second_edge;
1247 else
1248 *cond = bb_predicate (first_edge->src);
1250 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1251 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1252 is_gimple_condexpr, NULL_TREE,
1253 true, GSI_SAME_STMT);
1255 return first_edge->src;
1258 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1259 This routine does not handle PHI nodes with more than two
1260 arguments.
1262 For example,
1263 S1: A = PHI <x1(1), x2(5)>
1264 is converted into,
1265 S2: A = cond ? x1 : x2;
1267 The generated code is inserted at GSI that points to the top of
1268 basic block's statement list. When COND is true, phi arg from
1269 TRUE_BB is selected. */
1271 static void
1272 predicate_scalar_phi (gimple phi, tree cond,
1273 basic_block true_bb,
1274 gimple_stmt_iterator *gsi)
1276 gimple new_stmt;
1277 basic_block bb;
1278 tree rhs, res, arg, scev;
1280 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1281 && gimple_phi_num_args (phi) == 2);
1283 res = gimple_phi_result (phi);
1284 /* Do not handle virtual phi nodes. */
1285 if (!is_gimple_reg (SSA_NAME_VAR (res)))
1286 return;
1288 bb = gimple_bb (phi);
1290 if ((arg = degenerate_phi_result (phi))
1291 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1292 res))
1293 && !chrec_contains_undetermined (scev)
1294 && scev != res
1295 && (arg = gimple_phi_arg_def (phi, 0))))
1296 rhs = arg;
1297 else
1299 tree arg_0, arg_1;
1300 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1301 if (EDGE_PRED (bb, 1)->src == true_bb)
1303 arg_0 = gimple_phi_arg_def (phi, 1);
1304 arg_1 = gimple_phi_arg_def (phi, 0);
1306 else
1308 arg_0 = gimple_phi_arg_def (phi, 0);
1309 arg_1 = gimple_phi_arg_def (phi, 1);
1312 gcc_checking_assert (bb == bb->loop_father->header
1313 || bb_postdominates_preds (bb));
1315 /* Build new RHS using selected condition and arguments. */
1316 rhs = build3 (COND_EXPR, TREE_TYPE (res),
1317 unshare_expr (cond), arg_0, arg_1);
1320 new_stmt = gimple_build_assign (res, rhs);
1321 SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
1322 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1323 update_stmt (new_stmt);
1325 if (dump_file && (dump_flags & TDF_DETAILS))
1327 fprintf (dump_file, "new phi replacement stmt\n");
1328 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1332 /* Replaces in LOOP all the scalar phi nodes other than those in the
1333 LOOP->header block with conditional modify expressions. */
1335 static void
1336 predicate_all_scalar_phis (struct loop *loop)
1338 basic_block bb;
1339 unsigned int orig_loop_num_nodes = loop->num_nodes;
1340 unsigned int i;
1342 for (i = 1; i < orig_loop_num_nodes; i++)
1344 gimple phi;
1345 tree cond = NULL_TREE;
1346 gimple_stmt_iterator gsi, phi_gsi;
1347 basic_block true_bb = NULL;
1348 bb = ifc_bbs[i];
1350 if (bb == loop->header)
1351 continue;
1353 phi_gsi = gsi_start_phis (bb);
1354 if (gsi_end_p (phi_gsi))
1355 continue;
1357 /* BB has two predecessors. Using predecessor's aux field, set
1358 appropriate condition for the PHI node replacement. */
1359 gsi = gsi_after_labels (bb);
1360 true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
1362 while (!gsi_end_p (phi_gsi))
1364 phi = gsi_stmt (phi_gsi);
1365 predicate_scalar_phi (phi, cond, true_bb, &gsi);
1366 release_phi_node (phi);
1367 gsi_next (&phi_gsi);
1370 set_phi_nodes (bb, NULL);
1374 /* Insert in each basic block of LOOP the statements produced by the
1375 gimplification of the predicates. */
1377 static void
1378 insert_gimplified_predicates (loop_p loop)
1380 unsigned int i;
1382 for (i = 0; i < loop->num_nodes; i++)
1384 basic_block bb = ifc_bbs[i];
1385 gimple_seq stmts;
1387 if (!is_predicated (bb))
1389 /* Do not insert statements for a basic block that is not
1390 predicated. Also make sure that the predicate of the
1391 basic block is set to true. */
1392 reset_bb_predicate (bb);
1393 continue;
1396 stmts = bb_predicate_gimplified_stmts (bb);
1397 if (stmts)
1399 if (flag_tree_loop_if_convert_stores)
1401 /* Insert the predicate of the BB just after the label,
1402 as the if-conversion of memory writes will use this
1403 predicate. */
1404 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1405 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1407 else
1409 /* Insert the predicate of the BB at the end of the BB
1410 as this would reduce the register pressure: the only
1411 use of this predicate will be in successor BBs. */
1412 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1414 if (gsi_end_p (gsi)
1415 || stmt_ends_bb_p (gsi_stmt (gsi)))
1416 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1417 else
1418 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1421 /* Once the sequence is code generated, set it to NULL. */
1422 set_bb_predicate_gimplified_stmts (bb, NULL);
1427 /* Predicate each write to memory in LOOP.
1429 This function transforms control flow constructs containing memory
1430 writes of the form:
1432 | for (i = 0; i < N; i++)
1433 | if (cond)
1434 | A[i] = expr;
1436 into the following form that does not contain control flow:
1438 | for (i = 0; i < N; i++)
1439 | A[i] = cond ? expr : A[i];
1441 The original CFG looks like this:
1443 | bb_0
1444 | i = 0
1445 | end_bb_0
1447 | bb_1
1448 | if (i < N) goto bb_5 else goto bb_2
1449 | end_bb_1
1451 | bb_2
1452 | cond = some_computation;
1453 | if (cond) goto bb_3 else goto bb_4
1454 | end_bb_2
1456 | bb_3
1457 | A[i] = expr;
1458 | goto bb_4
1459 | end_bb_3
1461 | bb_4
1462 | goto bb_1
1463 | end_bb_4
1465 insert_gimplified_predicates inserts the computation of the COND
1466 expression at the beginning of the destination basic block:
1468 | bb_0
1469 | i = 0
1470 | end_bb_0
1472 | bb_1
1473 | if (i < N) goto bb_5 else goto bb_2
1474 | end_bb_1
1476 | bb_2
1477 | cond = some_computation;
1478 | if (cond) goto bb_3 else goto bb_4
1479 | end_bb_2
1481 | bb_3
1482 | cond = some_computation;
1483 | A[i] = expr;
1484 | goto bb_4
1485 | end_bb_3
1487 | bb_4
1488 | goto bb_1
1489 | end_bb_4
1491 predicate_mem_writes is then predicating the memory write as follows:
1493 | bb_0
1494 | i = 0
1495 | end_bb_0
1497 | bb_1
1498 | if (i < N) goto bb_5 else goto bb_2
1499 | end_bb_1
1501 | bb_2
1502 | if (cond) goto bb_3 else goto bb_4
1503 | end_bb_2
1505 | bb_3
1506 | cond = some_computation;
1507 | A[i] = cond ? expr : A[i];
1508 | goto bb_4
1509 | end_bb_3
1511 | bb_4
1512 | goto bb_1
1513 | end_bb_4
1515 and finally combine_blocks removes the basic block boundaries making
1516 the loop vectorizable:
1518 | bb_0
1519 | i = 0
1520 | if (i < N) goto bb_5 else goto bb_1
1521 | end_bb_0
1523 | bb_1
1524 | cond = some_computation;
1525 | A[i] = cond ? expr : A[i];
1526 | if (i < N) goto bb_5 else goto bb_4
1527 | end_bb_1
1529 | bb_4
1530 | goto bb_1
1531 | end_bb_4
1534 static void
1535 predicate_mem_writes (loop_p loop)
1537 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1539 for (i = 1; i < orig_loop_num_nodes; i++)
1541 gimple_stmt_iterator gsi;
1542 basic_block bb = ifc_bbs[i];
1543 tree cond = bb_predicate (bb);
1544 bool swap;
1545 gimple stmt;
1547 if (is_true_predicate (cond))
1548 continue;
1550 swap = false;
1551 if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1553 swap = true;
1554 cond = TREE_OPERAND (cond, 0);
1557 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1558 if ((stmt = gsi_stmt (gsi))
1559 && gimple_assign_single_p (stmt)
1560 && gimple_vdef (stmt))
1562 tree lhs = gimple_assign_lhs (stmt);
1563 tree rhs = gimple_assign_rhs1 (stmt);
1564 tree type = TREE_TYPE (lhs);
1566 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1567 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1568 if (swap)
1570 tree tem = lhs;
1571 lhs = rhs;
1572 rhs = tem;
1574 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1575 is_gimple_condexpr, NULL_TREE,
1576 true, GSI_SAME_STMT);
1577 rhs = build3 (COND_EXPR, type, unshare_expr (cond), rhs, lhs);
1578 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1579 update_stmt (stmt);
1584 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1585 other than the exit and latch of the LOOP. Also resets the
1586 GIMPLE_DEBUG information. */
1588 static void
1589 remove_conditions_and_labels (loop_p loop)
1591 gimple_stmt_iterator gsi;
1592 unsigned int i;
1594 for (i = 0; i < loop->num_nodes; i++)
1596 basic_block bb = ifc_bbs[i];
1598 if (bb_with_exit_edge_p (loop, bb)
1599 || bb == loop->latch)
1600 continue;
1602 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1603 switch (gimple_code (gsi_stmt (gsi)))
1605 case GIMPLE_COND:
1606 case GIMPLE_LABEL:
1607 gsi_remove (&gsi, true);
1608 break;
1610 case GIMPLE_DEBUG:
1611 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1612 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1614 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1615 update_stmt (gsi_stmt (gsi));
1617 gsi_next (&gsi);
1618 break;
1620 default:
1621 gsi_next (&gsi);
1626 /* Combine all the basic blocks from LOOP into one or two super basic
1627 blocks. Replace PHI nodes with conditional modify expressions. */
1629 static void
1630 combine_blocks (struct loop *loop)
1632 basic_block bb, exit_bb, merge_target_bb;
1633 unsigned int orig_loop_num_nodes = loop->num_nodes;
1634 unsigned int i;
1635 edge e;
1636 edge_iterator ei;
1638 remove_conditions_and_labels (loop);
1639 insert_gimplified_predicates (loop);
1640 predicate_all_scalar_phis (loop);
1642 if (flag_tree_loop_if_convert_stores)
1643 predicate_mem_writes (loop);
1645 /* Merge basic blocks: first remove all the edges in the loop,
1646 except for those from the exit block. */
1647 exit_bb = NULL;
1648 for (i = 0; i < orig_loop_num_nodes; i++)
1650 bb = ifc_bbs[i];
1651 free_bb_predicate (bb);
1652 if (bb_with_exit_edge_p (loop, bb))
1654 gcc_assert (exit_bb == NULL);
1655 exit_bb = bb;
1658 gcc_assert (exit_bb != loop->latch);
1660 for (i = 1; i < orig_loop_num_nodes; i++)
1662 bb = ifc_bbs[i];
1664 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1666 if (e->src == exit_bb)
1667 ei_next (&ei);
1668 else
1669 remove_edge (e);
1673 if (exit_bb != NULL)
1675 if (exit_bb != loop->header)
1677 /* Connect this node to loop header. */
1678 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1679 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1682 /* Redirect non-exit edges to loop->latch. */
1683 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1685 if (!loop_exit_edge_p (loop, e))
1686 redirect_edge_and_branch (e, loop->latch);
1688 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1690 else
1692 /* If the loop does not have an exit, reconnect header and latch. */
1693 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1694 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1697 merge_target_bb = loop->header;
1698 for (i = 1; i < orig_loop_num_nodes; i++)
1700 gimple_stmt_iterator gsi;
1701 gimple_stmt_iterator last;
1703 bb = ifc_bbs[i];
1705 if (bb == exit_bb || bb == loop->latch)
1706 continue;
1708 /* Make stmts member of loop->header. */
1709 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1710 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1712 /* Update stmt list. */
1713 last = gsi_last_bb (merge_target_bb);
1714 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1715 set_bb_seq (bb, NULL);
1717 delete_basic_block (bb);
1720 /* If possible, merge loop header to the block with the exit edge.
1721 This reduces the number of basic blocks to two, to please the
1722 vectorizer that handles only loops with two nodes. */
1723 if (exit_bb
1724 && exit_bb != loop->header
1725 && can_merge_blocks_p (loop->header, exit_bb))
1726 merge_blocks (loop->header, exit_bb);
1728 free (ifc_bbs);
1729 ifc_bbs = NULL;
1731 /* Post-dominators are corrupt now. */
1732 free_dominance_info (CDI_POST_DOMINATORS);
1735 /* If-convert LOOP when it is legal. For the moment this pass has no
1736 profitability analysis. Returns true when something changed. */
1738 static bool
1739 tree_if_conversion (struct loop *loop)
1741 bool changed = false;
1742 ifc_bbs = NULL;
1744 if (!if_convertible_loop_p (loop)
1745 || !dbg_cnt (if_conversion_tree))
1746 goto cleanup;
1748 /* Now all statements are if-convertible. Combine all the basic
1749 blocks into one huge basic block doing the if-conversion
1750 on-the-fly. */
1751 combine_blocks (loop);
1753 if (flag_tree_loop_if_convert_stores)
1754 mark_sym_for_renaming (gimple_vop (cfun));
1756 changed = true;
1758 cleanup:
1759 if (ifc_bbs)
1761 unsigned int i;
1763 for (i = 0; i < loop->num_nodes; i++)
1764 free_bb_predicate (ifc_bbs[i]);
1766 free (ifc_bbs);
1767 ifc_bbs = NULL;
1770 return changed;
1773 /* Tree if-conversion pass management. */
1775 static unsigned int
1776 main_tree_if_conversion (void)
1778 loop_iterator li;
1779 struct loop *loop;
1780 bool changed = false;
1781 unsigned todo = 0;
1783 if (number_of_loops () <= 1)
1784 return 0;
1786 FOR_EACH_LOOP (li, loop, 0)
1787 changed |= tree_if_conversion (loop);
1789 if (changed)
1790 todo |= TODO_cleanup_cfg;
1792 if (changed && flag_tree_loop_if_convert_stores)
1793 todo |= TODO_update_ssa_only_virtuals;
1795 free_dominance_info (CDI_POST_DOMINATORS);
1797 #ifdef ENABLE_CHECKING
1799 basic_block bb;
1800 FOR_EACH_BB (bb)
1801 gcc_assert (!bb->aux);
1803 #endif
1805 return todo;
1808 /* Returns true when the if-conversion pass is enabled. */
1810 static bool
1811 gate_tree_if_conversion (void)
1813 return ((flag_tree_vectorize && flag_tree_loop_if_convert != 0)
1814 || flag_tree_loop_if_convert == 1
1815 || flag_tree_loop_if_convert_stores == 1);
1818 struct gimple_opt_pass pass_if_conversion =
1821 GIMPLE_PASS,
1822 "ifcvt", /* name */
1823 gate_tree_if_conversion, /* gate */
1824 main_tree_if_conversion, /* execute */
1825 NULL, /* sub */
1826 NULL, /* next */
1827 0, /* static_pass_number */
1828 TV_NONE, /* tv_id */
1829 PROP_cfg | PROP_ssa, /* properties_required */
1830 0, /* properties_provided */
1831 0, /* properties_destroyed */
1832 0, /* todo_flags_start */
1833 TODO_verify_stmts | TODO_verify_flow
1834 /* todo_flags_finish */