2013-11-29 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-if-conv.c
blob7f6a1503085bfbc0eb2531250049f00d61055dfc
1 /* If-conversion for vectorizer.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Devang Patel <dpatel@apple.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* This pass implements a tree level if-conversion of loops. Its
22 initial goal is to help the vectorizer to vectorize loops with
23 conditions.
25 A short description of if-conversion:
27 o Decide if a loop is if-convertible or not.
28 o Walk all loop basic blocks in breadth first order (BFS order).
29 o Remove conditional statements (at the end of basic block)
30 and propagate condition into destination basic blocks'
31 predicate list.
32 o Replace modify expression with conditional modify expression
33 using current basic block's condition.
34 o Merge all basic blocks
35 o Replace phi nodes with conditional modify expr
36 o Merge all basic blocks into header
38 Sample transformation:
40 INPUT
41 -----
43 # i_23 = PHI <0(0), i_18(10)>;
44 <L0>:;
45 j_15 = A[i_23];
46 if (j_15 > 41) goto <L1>; else goto <L17>;
48 <L17>:;
49 goto <bb 3> (<L3>);
51 <L1>:;
53 # iftmp.2_4 = PHI <0(8), 42(2)>;
54 <L3>:;
55 A[i_23] = iftmp.2_4;
56 i_18 = i_23 + 1;
57 if (i_18 <= 15) goto <L19>; else goto <L18>;
59 <L19>:;
60 goto <bb 1> (<L0>);
62 <L18>:;
64 OUTPUT
65 ------
67 # i_23 = PHI <0(0), i_18(10)>;
68 <L0>:;
69 j_15 = A[i_23];
71 <L3>:;
72 iftmp.2_4 = j_15 > 41 ? 42 : 0;
73 A[i_23] = iftmp.2_4;
74 i_18 = i_23 + 1;
75 if (i_18 <= 15) goto <L19>; else goto <L18>;
77 <L19>:;
78 goto <bb 1> (<L0>);
80 <L18>:;
83 #include "config.h"
84 #include "system.h"
85 #include "coretypes.h"
86 #include "tm.h"
87 #include "tree.h"
88 #include "stor-layout.h"
89 #include "flags.h"
90 #include "basic-block.h"
91 #include "gimple-pretty-print.h"
92 #include "tree-ssa-alias.h"
93 #include "internal-fn.h"
94 #include "gimple-fold.h"
95 #include "gimple-expr.h"
96 #include "is-a.h"
97 #include "gimple.h"
98 #include "gimplify.h"
99 #include "gimple-iterator.h"
100 #include "gimplify-me.h"
101 #include "gimple-ssa.h"
102 #include "tree-cfg.h"
103 #include "tree-phinodes.h"
104 #include "ssa-iterators.h"
105 #include "stringpool.h"
106 #include "tree-ssanames.h"
107 #include "tree-into-ssa.h"
108 #include "tree-ssa.h"
109 #include "cfgloop.h"
110 #include "tree-chrec.h"
111 #include "tree-data-ref.h"
112 #include "tree-scalar-evolution.h"
113 #include "tree-pass.h"
114 #include "dbgcnt.h"
116 /* List of basic blocks in if-conversion-suitable order. */
117 static basic_block *ifc_bbs;
119 /* Structure used to predicate basic blocks. This is attached to the
120 ->aux field of the BBs in the loop to be if-converted. */
121 typedef struct bb_predicate_s {
123 /* The condition under which this basic block is executed. */
124 tree predicate;
126 /* PREDICATE is gimplified, and the sequence of statements is
127 recorded here, in order to avoid the duplication of computations
128 that occur in previous conditions. See PR44483. */
129 gimple_seq predicate_gimplified_stmts;
130 } *bb_predicate_p;
132 /* Returns true when the basic block BB has a predicate. */
134 static inline bool
135 bb_has_predicate (basic_block bb)
137 return bb->aux != NULL;
140 /* Returns the gimplified predicate for basic block BB. */
142 static inline tree
143 bb_predicate (basic_block bb)
145 return ((bb_predicate_p) bb->aux)->predicate;
148 /* Sets the gimplified predicate COND for basic block BB. */
150 static inline void
151 set_bb_predicate (basic_block bb, tree cond)
153 gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
154 && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
155 || is_gimple_condexpr (cond));
156 ((bb_predicate_p) bb->aux)->predicate = cond;
159 /* Returns the sequence of statements of the gimplification of the
160 predicate for basic block BB. */
162 static inline gimple_seq
163 bb_predicate_gimplified_stmts (basic_block bb)
165 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
168 /* Sets the sequence of statements STMTS of the gimplification of the
169 predicate for basic block BB. */
171 static inline void
172 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
174 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
177 /* Adds the sequence of statements STMTS to the sequence of statements
178 of the predicate for basic block BB. */
180 static inline void
181 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
183 gimple_seq_add_seq
184 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
187 /* Initializes to TRUE the predicate of basic block BB. */
189 static inline void
190 init_bb_predicate (basic_block bb)
192 bb->aux = XNEW (struct bb_predicate_s);
193 set_bb_predicate_gimplified_stmts (bb, NULL);
194 set_bb_predicate (bb, boolean_true_node);
197 /* Free the predicate of basic block BB. */
199 static inline void
200 free_bb_predicate (basic_block bb)
202 gimple_seq stmts;
204 if (!bb_has_predicate (bb))
205 return;
207 /* Release the SSA_NAMEs created for the gimplification of the
208 predicate. */
209 stmts = bb_predicate_gimplified_stmts (bb);
210 if (stmts)
212 gimple_stmt_iterator i;
214 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
215 free_stmt_operands (cfun, gsi_stmt (i));
218 free (bb->aux);
219 bb->aux = NULL;
222 /* Free the predicate of BB and reinitialize it with the true
223 predicate. */
225 static inline void
226 reset_bb_predicate (basic_block bb)
228 free_bb_predicate (bb);
229 init_bb_predicate (bb);
232 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
233 the expression EXPR. Inserts the statement created for this
234 computation before GSI and leaves the iterator GSI at the same
235 statement. */
237 static tree
238 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
240 tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
241 gimple stmt = gimple_build_assign (new_name, expr);
242 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
243 return new_name;
246 /* Return true when COND is a true predicate. */
248 static inline bool
249 is_true_predicate (tree cond)
251 return (cond == NULL_TREE
252 || cond == boolean_true_node
253 || integer_onep (cond));
256 /* Returns true when BB has a predicate that is not trivial: true or
257 NULL_TREE. */
259 static inline bool
260 is_predicated (basic_block bb)
262 return !is_true_predicate (bb_predicate (bb));
265 /* Parses the predicate COND and returns its comparison code and
266 operands OP0 and OP1. */
268 static enum tree_code
269 parse_predicate (tree cond, tree *op0, tree *op1)
271 gimple s;
273 if (TREE_CODE (cond) == SSA_NAME
274 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
276 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
278 *op0 = gimple_assign_rhs1 (s);
279 *op1 = gimple_assign_rhs2 (s);
280 return gimple_assign_rhs_code (s);
283 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
285 tree op = gimple_assign_rhs1 (s);
286 tree type = TREE_TYPE (op);
287 enum tree_code code = parse_predicate (op, op0, op1);
289 return code == ERROR_MARK ? ERROR_MARK
290 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
293 return ERROR_MARK;
296 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
298 *op0 = TREE_OPERAND (cond, 0);
299 *op1 = TREE_OPERAND (cond, 1);
300 return TREE_CODE (cond);
303 return ERROR_MARK;
306 /* Returns the fold of predicate C1 OR C2 at location LOC. */
308 static tree
309 fold_or_predicates (location_t loc, tree c1, tree c2)
311 tree op1a, op1b, op2a, op2b;
312 enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
313 enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
315 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
317 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
318 code2, op2a, op2b);
319 if (t)
320 return t;
323 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
326 /* Returns true if N is either a constant or a SSA_NAME. */
328 static bool
329 constant_or_ssa_name (tree n)
331 switch (TREE_CODE (n))
333 case SSA_NAME:
334 case INTEGER_CST:
335 case REAL_CST:
336 case COMPLEX_CST:
337 case VECTOR_CST:
338 return true;
339 default:
340 return false;
344 /* Returns either a COND_EXPR or the folded expression if the folded
345 expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
346 a constant or a SSA_NAME. */
348 static tree
349 fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
351 tree rhs1, lhs1, cond_expr;
352 cond_expr = fold_ternary (COND_EXPR, type, cond,
353 rhs, lhs);
355 if (cond_expr == NULL_TREE)
356 return build3 (COND_EXPR, type, cond, rhs, lhs);
358 STRIP_USELESS_TYPE_CONVERSION (cond_expr);
360 if (constant_or_ssa_name (cond_expr))
361 return cond_expr;
363 if (TREE_CODE (cond_expr) == ABS_EXPR)
365 rhs1 = TREE_OPERAND (cond_expr, 1);
366 STRIP_USELESS_TYPE_CONVERSION (rhs1);
367 if (constant_or_ssa_name (rhs1))
368 return build1 (ABS_EXPR, type, rhs1);
371 if (TREE_CODE (cond_expr) == MIN_EXPR
372 || TREE_CODE (cond_expr) == MAX_EXPR)
374 lhs1 = TREE_OPERAND (cond_expr, 0);
375 STRIP_USELESS_TYPE_CONVERSION (lhs1);
376 rhs1 = TREE_OPERAND (cond_expr, 1);
377 STRIP_USELESS_TYPE_CONVERSION (rhs1);
378 if (constant_or_ssa_name (rhs1)
379 && constant_or_ssa_name (lhs1))
380 return build2 (TREE_CODE (cond_expr), type, lhs1, rhs1);
382 return build3 (COND_EXPR, type, cond, rhs, lhs);
385 /* Add condition NC to the predicate list of basic block BB. */
387 static inline void
388 add_to_predicate_list (basic_block bb, tree nc)
390 tree bc, *tp;
392 if (is_true_predicate (nc))
393 return;
395 if (!is_predicated (bb))
396 bc = nc;
397 else
399 bc = bb_predicate (bb);
400 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
401 if (is_true_predicate (bc))
403 reset_bb_predicate (bb);
404 return;
408 /* Allow a TRUTH_NOT_EXPR around the main predicate. */
409 if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
410 tp = &TREE_OPERAND (bc, 0);
411 else
412 tp = &bc;
413 if (!is_gimple_condexpr (*tp))
415 gimple_seq stmts;
416 *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
417 add_bb_predicate_gimplified_stmts (bb, stmts);
419 set_bb_predicate (bb, bc);
422 /* Add the condition COND to the previous condition PREV_COND, and add
423 this to the predicate list of the destination of edge E. LOOP is
424 the loop to be if-converted. */
426 static void
427 add_to_dst_predicate_list (struct loop *loop, edge e,
428 tree prev_cond, tree cond)
430 if (!flow_bb_inside_loop_p (loop, e->dest))
431 return;
433 if (!is_true_predicate (prev_cond))
434 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
435 prev_cond, cond);
437 add_to_predicate_list (e->dest, cond);
440 /* Return true if one of the successor edges of BB exits LOOP. */
442 static bool
443 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
445 edge e;
446 edge_iterator ei;
448 FOR_EACH_EDGE (e, ei, bb->succs)
449 if (loop_exit_edge_p (loop, e))
450 return true;
452 return false;
455 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
456 and it belongs to basic block BB.
458 PHI is not if-convertible if:
459 - it has more than 2 arguments.
461 When the flag_tree_loop_if_convert_stores is not set, PHI is not
462 if-convertible if:
463 - a virtual PHI is immediately used in another PHI node,
464 - there is a virtual PHI in a BB other than the loop->header. */
466 static bool
467 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
469 if (dump_file && (dump_flags & TDF_DETAILS))
471 fprintf (dump_file, "-------------------------\n");
472 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
475 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
477 if (dump_file && (dump_flags & TDF_DETAILS))
478 fprintf (dump_file, "More than two phi node args.\n");
479 return false;
482 if (flag_tree_loop_if_convert_stores)
483 return true;
485 /* When the flag_tree_loop_if_convert_stores is not set, check
486 that there are no memory writes in the branches of the loop to be
487 if-converted. */
488 if (virtual_operand_p (gimple_phi_result (phi)))
490 imm_use_iterator imm_iter;
491 use_operand_p use_p;
493 if (bb != loop->header)
495 if (dump_file && (dump_flags & TDF_DETAILS))
496 fprintf (dump_file, "Virtual phi not on loop->header.\n");
497 return false;
500 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
502 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
504 if (dump_file && (dump_flags & TDF_DETAILS))
505 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
506 return false;
511 return true;
514 /* Records the status of a data reference. This struct is attached to
515 each DR->aux field. */
517 struct ifc_dr {
518 /* -1 when not initialized, 0 when false, 1 when true. */
519 int written_at_least_once;
521 /* -1 when not initialized, 0 when false, 1 when true. */
522 int rw_unconditionally;
525 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
526 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
527 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
529 /* Returns true when the memory references of STMT are read or written
530 unconditionally. In other words, this function returns true when
531 for every data reference A in STMT there exist other accesses to
532 a data reference with the same base with predicates that add up (OR-up) to
533 the true predicate: this ensures that the data reference A is touched
534 (read or written) on every iteration of the if-converted loop. */
536 static bool
537 memrefs_read_or_written_unconditionally (gimple stmt,
538 vec<data_reference_p> drs)
540 int i, j;
541 data_reference_p a, b;
542 tree ca = bb_predicate (gimple_bb (stmt));
544 for (i = 0; drs.iterate (i, &a); i++)
545 if (DR_STMT (a) == stmt)
547 bool found = false;
548 int x = DR_RW_UNCONDITIONALLY (a);
550 if (x == 0)
551 return false;
553 if (x == 1)
554 continue;
556 for (j = 0; drs.iterate (j, &b); j++)
558 tree ref_base_a = DR_REF (a);
559 tree ref_base_b = DR_REF (b);
561 if (DR_STMT (b) == stmt)
562 continue;
564 while (TREE_CODE (ref_base_a) == COMPONENT_REF
565 || TREE_CODE (ref_base_a) == IMAGPART_EXPR
566 || TREE_CODE (ref_base_a) == REALPART_EXPR)
567 ref_base_a = TREE_OPERAND (ref_base_a, 0);
569 while (TREE_CODE (ref_base_b) == COMPONENT_REF
570 || TREE_CODE (ref_base_b) == IMAGPART_EXPR
571 || TREE_CODE (ref_base_b) == REALPART_EXPR)
572 ref_base_b = TREE_OPERAND (ref_base_b, 0);
574 if (!operand_equal_p (ref_base_a, ref_base_b, 0))
576 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
578 if (DR_RW_UNCONDITIONALLY (b) == 1
579 || is_true_predicate (cb)
580 || is_true_predicate (ca
581 = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
583 DR_RW_UNCONDITIONALLY (a) = 1;
584 DR_RW_UNCONDITIONALLY (b) = 1;
585 found = true;
586 break;
591 if (!found)
593 DR_RW_UNCONDITIONALLY (a) = 0;
594 return false;
598 return true;
601 /* Returns true when the memory references of STMT are unconditionally
602 written. In other words, this function returns true when for every
603 data reference A written in STMT, there exist other writes to the
604 same data reference with predicates that add up (OR-up) to the true
605 predicate: this ensures that the data reference A is written on
606 every iteration of the if-converted loop. */
608 static bool
609 write_memrefs_written_at_least_once (gimple stmt,
610 vec<data_reference_p> drs)
612 int i, j;
613 data_reference_p a, b;
614 tree ca = bb_predicate (gimple_bb (stmt));
616 for (i = 0; drs.iterate (i, &a); i++)
617 if (DR_STMT (a) == stmt
618 && DR_IS_WRITE (a))
620 bool found = false;
621 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
623 if (x == 0)
624 return false;
626 if (x == 1)
627 continue;
629 for (j = 0; drs.iterate (j, &b); j++)
630 if (DR_STMT (b) != stmt
631 && DR_IS_WRITE (b)
632 && same_data_refs_base_objects (a, b))
634 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
636 if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
637 || is_true_predicate (cb)
638 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
639 ca, cb)))
641 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
642 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
643 found = true;
644 break;
648 if (!found)
650 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
651 return false;
655 return true;
658 /* Return true when the memory references of STMT won't trap in the
659 if-converted code. There are two things that we have to check for:
661 - writes to memory occur to writable memory: if-conversion of
662 memory writes transforms the conditional memory writes into
663 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
664 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
665 be executed at all in the original code, it may be a readonly
666 memory. To check that A is not const-qualified, we check that
667 there exists at least an unconditional write to A in the current
668 function.
670 - reads or writes to memory are valid memory accesses for every
671 iteration. To check that the memory accesses are correctly formed
672 and that we are allowed to read and write in these locations, we
673 check that the memory accesses to be if-converted occur at every
674 iteration unconditionally. */
676 static bool
677 ifcvt_memrefs_wont_trap (gimple stmt, vec<data_reference_p> refs)
679 return write_memrefs_written_at_least_once (stmt, refs)
680 && memrefs_read_or_written_unconditionally (stmt, refs);
683 /* Wrapper around gimple_could_trap_p refined for the needs of the
684 if-conversion. Try to prove that the memory accesses of STMT could
685 not trap in the innermost loop containing STMT. */
687 static bool
688 ifcvt_could_trap_p (gimple stmt, vec<data_reference_p> refs)
690 if (gimple_vuse (stmt)
691 && !gimple_could_trap_p_1 (stmt, false, false)
692 && ifcvt_memrefs_wont_trap (stmt, refs))
693 return false;
695 return gimple_could_trap_p (stmt);
698 /* Return true when STMT is if-convertible.
700 GIMPLE_ASSIGN statement is not if-convertible if,
701 - it is not movable,
702 - it could trap,
703 - LHS is not var decl. */
705 static bool
706 if_convertible_gimple_assign_stmt_p (gimple stmt,
707 vec<data_reference_p> refs)
709 tree lhs = gimple_assign_lhs (stmt);
710 basic_block bb;
712 if (dump_file && (dump_flags & TDF_DETAILS))
714 fprintf (dump_file, "-------------------------\n");
715 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
718 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
719 return false;
721 /* Some of these constrains might be too conservative. */
722 if (stmt_ends_bb_p (stmt)
723 || gimple_has_volatile_ops (stmt)
724 || (TREE_CODE (lhs) == SSA_NAME
725 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
726 || gimple_has_side_effects (stmt))
728 if (dump_file && (dump_flags & TDF_DETAILS))
729 fprintf (dump_file, "stmt not suitable for ifcvt\n");
730 return false;
733 if (flag_tree_loop_if_convert_stores)
735 if (ifcvt_could_trap_p (stmt, refs))
737 if (dump_file && (dump_flags & TDF_DETAILS))
738 fprintf (dump_file, "tree could trap...\n");
739 return false;
741 return true;
744 if (gimple_assign_rhs_could_trap_p (stmt))
746 if (dump_file && (dump_flags & TDF_DETAILS))
747 fprintf (dump_file, "tree could trap...\n");
748 return false;
751 bb = gimple_bb (stmt);
753 if (TREE_CODE (lhs) != SSA_NAME
754 && bb != bb->loop_father->header
755 && !bb_with_exit_edge_p (bb->loop_father, bb))
757 if (dump_file && (dump_flags & TDF_DETAILS))
759 fprintf (dump_file, "LHS is not var\n");
760 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
762 return false;
765 return true;
768 /* Return true when STMT is if-convertible.
770 A statement is if-convertible if:
771 - it is an if-convertible GIMPLE_ASSIGN,
772 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
774 static bool
775 if_convertible_stmt_p (gimple stmt, vec<data_reference_p> refs)
777 switch (gimple_code (stmt))
779 case GIMPLE_LABEL:
780 case GIMPLE_DEBUG:
781 case GIMPLE_COND:
782 return true;
784 case GIMPLE_ASSIGN:
785 return if_convertible_gimple_assign_stmt_p (stmt, refs);
787 case GIMPLE_CALL:
789 tree fndecl = gimple_call_fndecl (stmt);
790 if (fndecl)
792 int flags = gimple_call_flags (stmt);
793 if ((flags & ECF_CONST)
794 && !(flags & ECF_LOOPING_CONST_OR_PURE)
795 /* We can only vectorize some builtins at the moment,
796 so restrict if-conversion to those. */
797 && DECL_BUILT_IN (fndecl))
798 return true;
800 return false;
803 default:
804 /* Don't know what to do with 'em so don't do anything. */
805 if (dump_file && (dump_flags & TDF_DETAILS))
807 fprintf (dump_file, "don't know what to do\n");
808 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
810 return false;
811 break;
814 return true;
817 /* Return true when BB is if-convertible. This routine does not check
818 basic block's statements and phis.
820 A basic block is not if-convertible if:
821 - it is non-empty and it is after the exit block (in BFS order),
822 - it is after the exit block but before the latch,
823 - its edges are not normal.
825 EXIT_BB is the basic block containing the exit of the LOOP. BB is
826 inside LOOP. */
828 static bool
829 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
831 edge e;
832 edge_iterator ei;
834 if (dump_file && (dump_flags & TDF_DETAILS))
835 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
837 if (EDGE_COUNT (bb->preds) > 2
838 || EDGE_COUNT (bb->succs) > 2)
839 return false;
841 if (exit_bb)
843 if (bb != loop->latch)
845 if (dump_file && (dump_flags & TDF_DETAILS))
846 fprintf (dump_file, "basic block after exit bb but before latch\n");
847 return false;
849 else if (!empty_block_p (bb))
851 if (dump_file && (dump_flags & TDF_DETAILS))
852 fprintf (dump_file, "non empty basic block after exit bb\n");
853 return false;
855 else if (bb == loop->latch
856 && bb != exit_bb
857 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
859 if (dump_file && (dump_flags & TDF_DETAILS))
860 fprintf (dump_file, "latch is not dominated by exit_block\n");
861 return false;
865 /* Be less adventurous and handle only normal edges. */
866 FOR_EACH_EDGE (e, ei, bb->succs)
867 if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
869 if (dump_file && (dump_flags & TDF_DETAILS))
870 fprintf (dump_file, "Difficult to handle edges\n");
871 return false;
874 /* At least one incoming edge has to be non-critical as otherwise edge
875 predicates are not equal to basic-block predicates of the edge
876 source. */
877 if (EDGE_COUNT (bb->preds) > 1
878 && bb != loop->header)
880 bool found = false;
881 FOR_EACH_EDGE (e, ei, bb->preds)
882 if (EDGE_COUNT (e->src->succs) == 1)
883 found = true;
884 if (!found)
886 if (dump_file && (dump_flags & TDF_DETAILS))
887 fprintf (dump_file, "only critical predecessors\n");
888 return false;
892 return true;
895 /* Return true when all predecessor blocks of BB are visited. The
896 VISITED bitmap keeps track of the visited blocks. */
898 static bool
899 pred_blocks_visited_p (basic_block bb, bitmap *visited)
901 edge e;
902 edge_iterator ei;
903 FOR_EACH_EDGE (e, ei, bb->preds)
904 if (!bitmap_bit_p (*visited, e->src->index))
905 return false;
907 return true;
910 /* Get body of a LOOP in suitable order for if-conversion. It is
911 caller's responsibility to deallocate basic block list.
912 If-conversion suitable order is, breadth first sort (BFS) order
913 with an additional constraint: select a block only if all its
914 predecessors are already selected. */
916 static basic_block *
917 get_loop_body_in_if_conv_order (const struct loop *loop)
919 basic_block *blocks, *blocks_in_bfs_order;
920 basic_block bb;
921 bitmap visited;
922 unsigned int index = 0;
923 unsigned int visited_count = 0;
925 gcc_assert (loop->num_nodes);
926 gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
928 blocks = XCNEWVEC (basic_block, loop->num_nodes);
929 visited = BITMAP_ALLOC (NULL);
931 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
933 index = 0;
934 while (index < loop->num_nodes)
936 bb = blocks_in_bfs_order [index];
938 if (bb->flags & BB_IRREDUCIBLE_LOOP)
940 free (blocks_in_bfs_order);
941 BITMAP_FREE (visited);
942 free (blocks);
943 return NULL;
946 if (!bitmap_bit_p (visited, bb->index))
948 if (pred_blocks_visited_p (bb, &visited)
949 || bb == loop->header)
951 /* This block is now visited. */
952 bitmap_set_bit (visited, bb->index);
953 blocks[visited_count++] = bb;
957 index++;
959 if (index == loop->num_nodes
960 && visited_count != loop->num_nodes)
961 /* Not done yet. */
962 index = 0;
964 free (blocks_in_bfs_order);
965 BITMAP_FREE (visited);
966 return blocks;
969 /* Returns true when the analysis of the predicates for all the basic
970 blocks in LOOP succeeded.
972 predicate_bbs first allocates the predicates of the basic blocks.
973 These fields are then initialized with the tree expressions
974 representing the predicates under which a basic block is executed
975 in the LOOP. As the loop->header is executed at each iteration, it
976 has the "true" predicate. Other statements executed under a
977 condition are predicated with that condition, for example
979 | if (x)
980 | S1;
981 | else
982 | S2;
984 S1 will be predicated with "x", and
985 S2 will be predicated with "!x". */
987 static bool
988 predicate_bbs (loop_p loop)
990 unsigned int i;
992 for (i = 0; i < loop->num_nodes; i++)
993 init_bb_predicate (ifc_bbs[i]);
995 for (i = 0; i < loop->num_nodes; i++)
997 basic_block bb = ifc_bbs[i];
998 tree cond;
999 gimple_stmt_iterator itr;
1001 /* The loop latch is always executed and has no extra conditions
1002 to be processed: skip it. */
1003 if (bb == loop->latch)
1005 reset_bb_predicate (loop->latch);
1006 continue;
1009 cond = bb_predicate (bb);
1011 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1013 gimple stmt = gsi_stmt (itr);
1015 switch (gimple_code (stmt))
1017 case GIMPLE_LABEL:
1018 case GIMPLE_ASSIGN:
1019 case GIMPLE_CALL:
1020 case GIMPLE_DEBUG:
1021 break;
1023 case GIMPLE_COND:
1025 tree c2;
1026 edge true_edge, false_edge;
1027 location_t loc = gimple_location (stmt);
1028 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
1029 boolean_type_node,
1030 gimple_cond_lhs (stmt),
1031 gimple_cond_rhs (stmt));
1033 /* Add new condition into destination's predicate list. */
1034 extract_true_false_edges_from_block (gimple_bb (stmt),
1035 &true_edge, &false_edge);
1037 /* If C is true, then TRUE_EDGE is taken. */
1038 add_to_dst_predicate_list (loop, true_edge,
1039 unshare_expr (cond),
1040 unshare_expr (c));
1042 /* If C is false, then FALSE_EDGE is taken. */
1043 c2 = build1_loc (loc, TRUTH_NOT_EXPR,
1044 boolean_type_node, unshare_expr (c));
1045 add_to_dst_predicate_list (loop, false_edge,
1046 unshare_expr (cond), c2);
1048 cond = NULL_TREE;
1049 break;
1052 default:
1053 /* Not handled yet in if-conversion. */
1054 return false;
1058 /* If current bb has only one successor, then consider it as an
1059 unconditional goto. */
1060 if (single_succ_p (bb))
1062 basic_block bb_n = single_succ (bb);
1064 /* The successor bb inherits the predicate of its
1065 predecessor. If there is no predicate in the predecessor
1066 bb, then consider the successor bb as always executed. */
1067 if (cond == NULL_TREE)
1068 cond = boolean_true_node;
1070 add_to_predicate_list (bb_n, cond);
1074 /* The loop header is always executed. */
1075 reset_bb_predicate (loop->header);
1076 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1077 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1079 return true;
1082 /* Return true when LOOP is if-convertible. This is a helper function
1083 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1084 in if_convertible_loop_p. */
1086 static bool
1087 if_convertible_loop_p_1 (struct loop *loop,
1088 vec<loop_p> *loop_nest,
1089 vec<data_reference_p> *refs,
1090 vec<ddr_p> *ddrs)
1092 bool res;
1093 unsigned int i;
1094 basic_block exit_bb = NULL;
1096 /* Don't if-convert the loop when the data dependences cannot be
1097 computed: the loop won't be vectorized in that case. */
1098 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1099 if (!res)
1100 return false;
1102 calculate_dominance_info (CDI_DOMINATORS);
1104 /* Allow statements that can be handled during if-conversion. */
1105 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1106 if (!ifc_bbs)
1108 if (dump_file && (dump_flags & TDF_DETAILS))
1109 fprintf (dump_file, "Irreducible loop\n");
1110 return false;
1113 for (i = 0; i < loop->num_nodes; i++)
1115 basic_block bb = ifc_bbs[i];
1117 if (!if_convertible_bb_p (loop, bb, exit_bb))
1118 return false;
1120 if (bb_with_exit_edge_p (loop, bb))
1121 exit_bb = bb;
1124 res = predicate_bbs (loop);
1125 if (!res)
1126 return false;
1128 if (flag_tree_loop_if_convert_stores)
1130 data_reference_p dr;
1132 for (i = 0; refs->iterate (i, &dr); i++)
1134 dr->aux = XNEW (struct ifc_dr);
1135 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1136 DR_RW_UNCONDITIONALLY (dr) = -1;
1140 for (i = 0; i < loop->num_nodes; i++)
1142 basic_block bb = ifc_bbs[i];
1143 gimple_stmt_iterator itr;
1145 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1146 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1147 return false;
1149 /* Check the if-convertibility of statements in predicated BBs. */
1150 if (is_predicated (bb))
1151 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1152 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1153 return false;
1156 if (dump_file)
1157 fprintf (dump_file, "Applying if-conversion\n");
1159 return true;
1162 /* Return true when LOOP is if-convertible.
1163 LOOP is if-convertible if:
1164 - it is innermost,
1165 - it has two or more basic blocks,
1166 - it has only one exit,
1167 - loop header is not the exit edge,
1168 - if its basic blocks and phi nodes are if convertible. */
1170 static bool
1171 if_convertible_loop_p (struct loop *loop)
1173 edge e;
1174 edge_iterator ei;
1175 bool res = false;
1176 vec<data_reference_p> refs;
1177 vec<ddr_p> ddrs;
1179 /* Handle only innermost loop. */
1180 if (!loop || loop->inner)
1182 if (dump_file && (dump_flags & TDF_DETAILS))
1183 fprintf (dump_file, "not innermost loop\n");
1184 return false;
1187 /* If only one block, no need for if-conversion. */
1188 if (loop->num_nodes <= 2)
1190 if (dump_file && (dump_flags & TDF_DETAILS))
1191 fprintf (dump_file, "less than 2 basic blocks\n");
1192 return false;
1195 /* More than one loop exit is too much to handle. */
1196 if (!single_exit (loop))
1198 if (dump_file && (dump_flags & TDF_DETAILS))
1199 fprintf (dump_file, "multiple exits\n");
1200 return false;
1203 /* If one of the loop header's edge is an exit edge then do not
1204 apply if-conversion. */
1205 FOR_EACH_EDGE (e, ei, loop->header->succs)
1206 if (loop_exit_edge_p (loop, e))
1207 return false;
1209 refs.create (5);
1210 ddrs.create (25);
1211 stack_vec<loop_p, 3> loop_nest;
1212 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1214 if (flag_tree_loop_if_convert_stores)
1216 data_reference_p dr;
1217 unsigned int i;
1219 for (i = 0; refs.iterate (i, &dr); i++)
1220 free (dr->aux);
1223 free_data_refs (refs);
1224 free_dependence_relations (ddrs);
1225 return res;
1228 /* Basic block BB has two predecessors. Using predecessor's bb
1229 predicate, set an appropriate condition COND for the PHI node
1230 replacement. Return the true block whose phi arguments are
1231 selected when cond is true. LOOP is the loop containing the
1232 if-converted region, GSI is the place to insert the code for the
1233 if-conversion. */
1235 static basic_block
1236 find_phi_replacement_condition (basic_block bb, tree *cond,
1237 gimple_stmt_iterator *gsi)
1239 edge first_edge, second_edge;
1240 tree tmp_cond;
1242 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1243 first_edge = EDGE_PRED (bb, 0);
1244 second_edge = EDGE_PRED (bb, 1);
1246 /* Prefer an edge with a not negated predicate.
1247 ??? That's a very weak cost model. */
1248 tmp_cond = bb_predicate (first_edge->src);
1249 gcc_assert (tmp_cond);
1250 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1252 edge tmp_edge;
1254 tmp_edge = first_edge;
1255 first_edge = second_edge;
1256 second_edge = tmp_edge;
1259 /* Check if the edge we take the condition from is not critical.
1260 We know that at least one non-critical edge exists. */
1261 if (EDGE_COUNT (first_edge->src->succs) > 1)
1263 *cond = bb_predicate (second_edge->src);
1265 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1266 *cond = TREE_OPERAND (*cond, 0);
1267 else
1268 /* Select non loop header bb. */
1269 first_edge = second_edge;
1271 else
1272 *cond = bb_predicate (first_edge->src);
1274 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1275 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1276 is_gimple_condexpr, NULL_TREE,
1277 true, GSI_SAME_STMT);
1279 return first_edge->src;
1282 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1283 This routine does not handle PHI nodes with more than two
1284 arguments.
1286 For example,
1287 S1: A = PHI <x1(1), x2(5)>
1288 is converted into,
1289 S2: A = cond ? x1 : x2;
1291 The generated code is inserted at GSI that points to the top of
1292 basic block's statement list. When COND is true, phi arg from
1293 TRUE_BB is selected. */
1295 static void
1296 predicate_scalar_phi (gimple phi, tree cond,
1297 basic_block true_bb,
1298 gimple_stmt_iterator *gsi)
1300 gimple new_stmt;
1301 basic_block bb;
1302 tree rhs, res, arg, scev;
1304 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1305 && gimple_phi_num_args (phi) == 2);
1307 res = gimple_phi_result (phi);
1308 /* Do not handle virtual phi nodes. */
1309 if (virtual_operand_p (res))
1310 return;
1312 bb = gimple_bb (phi);
1314 if ((arg = degenerate_phi_result (phi))
1315 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1316 res))
1317 && !chrec_contains_undetermined (scev)
1318 && scev != res
1319 && (arg = gimple_phi_arg_def (phi, 0))))
1320 rhs = arg;
1321 else
1323 tree arg_0, arg_1;
1324 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1325 if (EDGE_PRED (bb, 1)->src == true_bb)
1327 arg_0 = gimple_phi_arg_def (phi, 1);
1328 arg_1 = gimple_phi_arg_def (phi, 0);
1330 else
1332 arg_0 = gimple_phi_arg_def (phi, 0);
1333 arg_1 = gimple_phi_arg_def (phi, 1);
1336 /* Build new RHS using selected condition and arguments. */
1337 rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
1338 arg_0, arg_1);
1341 new_stmt = gimple_build_assign (res, rhs);
1342 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1343 update_stmt (new_stmt);
1345 if (dump_file && (dump_flags & TDF_DETAILS))
1347 fprintf (dump_file, "new phi replacement stmt\n");
1348 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1352 /* Replaces in LOOP all the scalar phi nodes other than those in the
1353 LOOP->header block with conditional modify expressions. */
1355 static void
1356 predicate_all_scalar_phis (struct loop *loop)
1358 basic_block bb;
1359 unsigned int orig_loop_num_nodes = loop->num_nodes;
1360 unsigned int i;
1362 for (i = 1; i < orig_loop_num_nodes; i++)
1364 gimple phi;
1365 tree cond = NULL_TREE;
1366 gimple_stmt_iterator gsi, phi_gsi;
1367 basic_block true_bb = NULL;
1368 bb = ifc_bbs[i];
1370 if (bb == loop->header)
1371 continue;
1373 phi_gsi = gsi_start_phis (bb);
1374 if (gsi_end_p (phi_gsi))
1375 continue;
1377 /* BB has two predecessors. Using predecessor's aux field, set
1378 appropriate condition for the PHI node replacement. */
1379 gsi = gsi_after_labels (bb);
1380 true_bb = find_phi_replacement_condition (bb, &cond, &gsi);
1382 while (!gsi_end_p (phi_gsi))
1384 phi = gsi_stmt (phi_gsi);
1385 predicate_scalar_phi (phi, cond, true_bb, &gsi);
1386 release_phi_node (phi);
1387 gsi_next (&phi_gsi);
1390 set_phi_nodes (bb, NULL);
1394 /* Insert in each basic block of LOOP the statements produced by the
1395 gimplification of the predicates. */
1397 static void
1398 insert_gimplified_predicates (loop_p loop)
1400 unsigned int i;
1402 for (i = 0; i < loop->num_nodes; i++)
1404 basic_block bb = ifc_bbs[i];
1405 gimple_seq stmts;
1407 if (!is_predicated (bb))
1409 /* Do not insert statements for a basic block that is not
1410 predicated. Also make sure that the predicate of the
1411 basic block is set to true. */
1412 reset_bb_predicate (bb);
1413 continue;
1416 stmts = bb_predicate_gimplified_stmts (bb);
1417 if (stmts)
1419 if (flag_tree_loop_if_convert_stores)
1421 /* Insert the predicate of the BB just after the label,
1422 as the if-conversion of memory writes will use this
1423 predicate. */
1424 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1425 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1427 else
1429 /* Insert the predicate of the BB at the end of the BB
1430 as this would reduce the register pressure: the only
1431 use of this predicate will be in successor BBs. */
1432 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1434 if (gsi_end_p (gsi)
1435 || stmt_ends_bb_p (gsi_stmt (gsi)))
1436 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1437 else
1438 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1441 /* Once the sequence is code generated, set it to NULL. */
1442 set_bb_predicate_gimplified_stmts (bb, NULL);
1447 /* Predicate each write to memory in LOOP.
1449 This function transforms control flow constructs containing memory
1450 writes of the form:
1452 | for (i = 0; i < N; i++)
1453 | if (cond)
1454 | A[i] = expr;
1456 into the following form that does not contain control flow:
1458 | for (i = 0; i < N; i++)
1459 | A[i] = cond ? expr : A[i];
1461 The original CFG looks like this:
1463 | bb_0
1464 | i = 0
1465 | end_bb_0
1467 | bb_1
1468 | if (i < N) goto bb_5 else goto bb_2
1469 | end_bb_1
1471 | bb_2
1472 | cond = some_computation;
1473 | if (cond) goto bb_3 else goto bb_4
1474 | end_bb_2
1476 | bb_3
1477 | A[i] = expr;
1478 | goto bb_4
1479 | end_bb_3
1481 | bb_4
1482 | goto bb_1
1483 | end_bb_4
1485 insert_gimplified_predicates inserts the computation of the COND
1486 expression at the beginning of the destination basic block:
1488 | bb_0
1489 | i = 0
1490 | end_bb_0
1492 | bb_1
1493 | if (i < N) goto bb_5 else goto bb_2
1494 | end_bb_1
1496 | bb_2
1497 | cond = some_computation;
1498 | if (cond) goto bb_3 else goto bb_4
1499 | end_bb_2
1501 | bb_3
1502 | cond = some_computation;
1503 | A[i] = expr;
1504 | goto bb_4
1505 | end_bb_3
1507 | bb_4
1508 | goto bb_1
1509 | end_bb_4
1511 predicate_mem_writes is then predicating the memory write as follows:
1513 | bb_0
1514 | i = 0
1515 | end_bb_0
1517 | bb_1
1518 | if (i < N) goto bb_5 else goto bb_2
1519 | end_bb_1
1521 | bb_2
1522 | if (cond) goto bb_3 else goto bb_4
1523 | end_bb_2
1525 | bb_3
1526 | cond = some_computation;
1527 | A[i] = cond ? expr : A[i];
1528 | goto bb_4
1529 | end_bb_3
1531 | bb_4
1532 | goto bb_1
1533 | end_bb_4
1535 and finally combine_blocks removes the basic block boundaries making
1536 the loop vectorizable:
1538 | bb_0
1539 | i = 0
1540 | if (i < N) goto bb_5 else goto bb_1
1541 | end_bb_0
1543 | bb_1
1544 | cond = some_computation;
1545 | A[i] = cond ? expr : A[i];
1546 | if (i < N) goto bb_5 else goto bb_4
1547 | end_bb_1
1549 | bb_4
1550 | goto bb_1
1551 | end_bb_4
1554 static void
1555 predicate_mem_writes (loop_p loop)
1557 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1559 for (i = 1; i < orig_loop_num_nodes; i++)
1561 gimple_stmt_iterator gsi;
1562 basic_block bb = ifc_bbs[i];
1563 tree cond = bb_predicate (bb);
1564 bool swap;
1565 gimple stmt;
1567 if (is_true_predicate (cond))
1568 continue;
1570 swap = false;
1571 if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1573 swap = true;
1574 cond = TREE_OPERAND (cond, 0);
1577 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1578 if ((stmt = gsi_stmt (gsi))
1579 && gimple_assign_single_p (stmt)
1580 && gimple_vdef (stmt))
1582 tree lhs = gimple_assign_lhs (stmt);
1583 tree rhs = gimple_assign_rhs1 (stmt);
1584 tree type = TREE_TYPE (lhs);
1586 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1587 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1588 if (swap)
1590 tree tem = lhs;
1591 lhs = rhs;
1592 rhs = tem;
1594 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1595 is_gimple_condexpr, NULL_TREE,
1596 true, GSI_SAME_STMT);
1597 rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
1598 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1599 update_stmt (stmt);
1604 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1605 other than the exit and latch of the LOOP. Also resets the
1606 GIMPLE_DEBUG information. */
1608 static void
1609 remove_conditions_and_labels (loop_p loop)
1611 gimple_stmt_iterator gsi;
1612 unsigned int i;
1614 for (i = 0; i < loop->num_nodes; i++)
1616 basic_block bb = ifc_bbs[i];
1618 if (bb_with_exit_edge_p (loop, bb)
1619 || bb == loop->latch)
1620 continue;
1622 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1623 switch (gimple_code (gsi_stmt (gsi)))
1625 case GIMPLE_COND:
1626 case GIMPLE_LABEL:
1627 gsi_remove (&gsi, true);
1628 break;
1630 case GIMPLE_DEBUG:
1631 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1632 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1634 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1635 update_stmt (gsi_stmt (gsi));
1637 gsi_next (&gsi);
1638 break;
1640 default:
1641 gsi_next (&gsi);
1646 /* Combine all the basic blocks from LOOP into one or two super basic
1647 blocks. Replace PHI nodes with conditional modify expressions. */
1649 static void
1650 combine_blocks (struct loop *loop)
1652 basic_block bb, exit_bb, merge_target_bb;
1653 unsigned int orig_loop_num_nodes = loop->num_nodes;
1654 unsigned int i;
1655 edge e;
1656 edge_iterator ei;
1658 remove_conditions_and_labels (loop);
1659 insert_gimplified_predicates (loop);
1660 predicate_all_scalar_phis (loop);
1662 if (flag_tree_loop_if_convert_stores)
1663 predicate_mem_writes (loop);
1665 /* Merge basic blocks: first remove all the edges in the loop,
1666 except for those from the exit block. */
1667 exit_bb = NULL;
1668 for (i = 0; i < orig_loop_num_nodes; i++)
1670 bb = ifc_bbs[i];
1671 free_bb_predicate (bb);
1672 if (bb_with_exit_edge_p (loop, bb))
1674 gcc_assert (exit_bb == NULL);
1675 exit_bb = bb;
1678 gcc_assert (exit_bb != loop->latch);
1680 for (i = 1; i < orig_loop_num_nodes; i++)
1682 bb = ifc_bbs[i];
1684 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1686 if (e->src == exit_bb)
1687 ei_next (&ei);
1688 else
1689 remove_edge (e);
1693 if (exit_bb != NULL)
1695 if (exit_bb != loop->header)
1697 /* Connect this node to loop header. */
1698 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1699 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1702 /* Redirect non-exit edges to loop->latch. */
1703 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1705 if (!loop_exit_edge_p (loop, e))
1706 redirect_edge_and_branch (e, loop->latch);
1708 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1710 else
1712 /* If the loop does not have an exit, reconnect header and latch. */
1713 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1714 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1717 merge_target_bb = loop->header;
1718 for (i = 1; i < orig_loop_num_nodes; i++)
1720 gimple_stmt_iterator gsi;
1721 gimple_stmt_iterator last;
1723 bb = ifc_bbs[i];
1725 if (bb == exit_bb || bb == loop->latch)
1726 continue;
1728 /* Make stmts member of loop->header. */
1729 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1730 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1732 /* Update stmt list. */
1733 last = gsi_last_bb (merge_target_bb);
1734 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1735 set_bb_seq (bb, NULL);
1737 delete_basic_block (bb);
1740 /* If possible, merge loop header to the block with the exit edge.
1741 This reduces the number of basic blocks to two, to please the
1742 vectorizer that handles only loops with two nodes. */
1743 if (exit_bb
1744 && exit_bb != loop->header
1745 && can_merge_blocks_p (loop->header, exit_bb))
1746 merge_blocks (loop->header, exit_bb);
1748 free (ifc_bbs);
1749 ifc_bbs = NULL;
1752 /* If-convert LOOP when it is legal. For the moment this pass has no
1753 profitability analysis. Returns true when something changed. */
1755 static bool
1756 tree_if_conversion (struct loop *loop)
1758 bool changed = false;
1759 ifc_bbs = NULL;
1761 if (!if_convertible_loop_p (loop)
1762 || !dbg_cnt (if_conversion_tree))
1763 goto cleanup;
1765 /* Now all statements are if-convertible. Combine all the basic
1766 blocks into one huge basic block doing the if-conversion
1767 on-the-fly. */
1768 combine_blocks (loop);
1770 if (flag_tree_loop_if_convert_stores)
1771 mark_virtual_operands_for_renaming (cfun);
1773 changed = true;
1775 cleanup:
1776 if (ifc_bbs)
1778 unsigned int i;
1780 for (i = 0; i < loop->num_nodes; i++)
1781 free_bb_predicate (ifc_bbs[i]);
1783 free (ifc_bbs);
1784 ifc_bbs = NULL;
1787 return changed;
1790 /* Tree if-conversion pass management. */
1792 static unsigned int
1793 main_tree_if_conversion (void)
1795 struct loop *loop;
1796 bool changed = false;
1797 unsigned todo = 0;
1799 if (number_of_loops (cfun) <= 1)
1800 return 0;
1802 FOR_EACH_LOOP (loop, 0)
1803 if (flag_tree_loop_if_convert == 1
1804 || flag_tree_loop_if_convert_stores == 1
1805 || flag_tree_loop_vectorize
1806 || loop->force_vect)
1807 changed |= tree_if_conversion (loop);
1809 if (changed)
1810 todo |= TODO_cleanup_cfg;
1812 if (changed && flag_tree_loop_if_convert_stores)
1813 todo |= TODO_update_ssa_only_virtuals;
1815 #ifdef ENABLE_CHECKING
1817 basic_block bb;
1818 FOR_EACH_BB (bb)
1819 gcc_assert (!bb->aux);
1821 #endif
1823 return todo;
1826 /* Returns true when the if-conversion pass is enabled. */
1828 static bool
1829 gate_tree_if_conversion (void)
1831 return (((flag_tree_loop_vectorize || cfun->has_force_vect_loops)
1832 && flag_tree_loop_if_convert != 0)
1833 || flag_tree_loop_if_convert == 1
1834 || flag_tree_loop_if_convert_stores == 1);
1837 namespace {
1839 const pass_data pass_data_if_conversion =
1841 GIMPLE_PASS, /* type */
1842 "ifcvt", /* name */
1843 OPTGROUP_NONE, /* optinfo_flags */
1844 true, /* has_gate */
1845 true, /* has_execute */
1846 TV_NONE, /* tv_id */
1847 ( PROP_cfg | PROP_ssa ), /* properties_required */
1848 0, /* properties_provided */
1849 0, /* properties_destroyed */
1850 0, /* todo_flags_start */
1851 ( TODO_verify_stmts | TODO_verify_flow
1852 | TODO_verify_ssa ), /* todo_flags_finish */
1855 class pass_if_conversion : public gimple_opt_pass
1857 public:
1858 pass_if_conversion (gcc::context *ctxt)
1859 : gimple_opt_pass (pass_data_if_conversion, ctxt)
1862 /* opt_pass methods: */
1863 bool gate () { return gate_tree_if_conversion (); }
1864 unsigned int execute () { return main_tree_if_conversion (); }
1866 }; // class pass_if_conversion
1868 } // anon namespace
1870 gimple_opt_pass *
1871 make_pass_if_conversion (gcc::context *ctxt)
1873 return new pass_if_conversion (ctxt);