Reverting merge from trunk
[official-gcc.git] / gcc / tree-if-conv.c
blob81403f25aa09da5b9c1017b2e770c7385f60f2af
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 "flags.h"
89 #include "basic-block.h"
90 #include "gimple-pretty-print.h"
91 #include "gimple.h"
92 #include "gimplify.h"
93 #include "gimple-iterator.h"
94 #include "gimplify-me.h"
95 #include "gimple-ssa.h"
96 #include "tree-cfg.h"
97 #include "tree-phinodes.h"
98 #include "ssa-iterators.h"
99 #include "tree-ssanames.h"
100 #include "tree-into-ssa.h"
101 #include "tree-ssa.h"
102 #include "cfgloop.h"
103 #include "tree-chrec.h"
104 #include "tree-data-ref.h"
105 #include "tree-scalar-evolution.h"
106 #include "tree-pass.h"
107 #include "dbgcnt.h"
109 /* List of basic blocks in if-conversion-suitable order. */
110 static basic_block *ifc_bbs;
112 /* Structure used to predicate basic blocks. This is attached to the
113 ->aux field of the BBs in the loop to be if-converted. */
114 typedef struct bb_predicate_s {
116 /* The condition under which this basic block is executed. */
117 tree predicate;
119 /* PREDICATE is gimplified, and the sequence of statements is
120 recorded here, in order to avoid the duplication of computations
121 that occur in previous conditions. See PR44483. */
122 gimple_seq predicate_gimplified_stmts;
123 } *bb_predicate_p;
125 /* Returns true when the basic block BB has a predicate. */
127 static inline bool
128 bb_has_predicate (basic_block bb)
130 return bb->aux != NULL;
133 /* Returns the gimplified predicate for basic block BB. */
135 static inline tree
136 bb_predicate (basic_block bb)
138 return ((bb_predicate_p) bb->aux)->predicate;
141 /* Sets the gimplified predicate COND for basic block BB. */
143 static inline void
144 set_bb_predicate (basic_block bb, tree cond)
146 gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
147 && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
148 || is_gimple_condexpr (cond));
149 ((bb_predicate_p) bb->aux)->predicate = cond;
152 /* Returns the sequence of statements of the gimplification of the
153 predicate for basic block BB. */
155 static inline gimple_seq
156 bb_predicate_gimplified_stmts (basic_block bb)
158 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
161 /* Sets the sequence of statements STMTS of the gimplification of the
162 predicate for basic block BB. */
164 static inline void
165 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
167 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
170 /* Adds the sequence of statements STMTS to the sequence of statements
171 of the predicate for basic block BB. */
173 static inline void
174 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
176 gimple_seq_add_seq
177 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
180 /* Initializes to TRUE the predicate of basic block BB. */
182 static inline void
183 init_bb_predicate (basic_block bb)
185 bb->aux = XNEW (struct bb_predicate_s);
186 set_bb_predicate_gimplified_stmts (bb, NULL);
187 set_bb_predicate (bb, boolean_true_node);
190 /* Free the predicate of basic block BB. */
192 static inline void
193 free_bb_predicate (basic_block bb)
195 gimple_seq stmts;
197 if (!bb_has_predicate (bb))
198 return;
200 /* Release the SSA_NAMEs created for the gimplification of the
201 predicate. */
202 stmts = bb_predicate_gimplified_stmts (bb);
203 if (stmts)
205 gimple_stmt_iterator i;
207 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
208 free_stmt_operands (gsi_stmt (i));
211 free (bb->aux);
212 bb->aux = NULL;
215 /* Free the predicate of BB and reinitialize it with the true
216 predicate. */
218 static inline void
219 reset_bb_predicate (basic_block bb)
221 free_bb_predicate (bb);
222 init_bb_predicate (bb);
225 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
226 the expression EXPR. Inserts the statement created for this
227 computation before GSI and leaves the iterator GSI at the same
228 statement. */
230 static tree
231 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
233 tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
234 gimple stmt = gimple_build_assign (new_name, expr);
235 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
236 return new_name;
239 /* Return true when COND is a true predicate. */
241 static inline bool
242 is_true_predicate (tree cond)
244 return (cond == NULL_TREE
245 || cond == boolean_true_node
246 || integer_onep (cond));
249 /* Returns true when BB has a predicate that is not trivial: true or
250 NULL_TREE. */
252 static inline bool
253 is_predicated (basic_block bb)
255 return !is_true_predicate (bb_predicate (bb));
258 /* Parses the predicate COND and returns its comparison code and
259 operands OP0 and OP1. */
261 static enum tree_code
262 parse_predicate (tree cond, tree *op0, tree *op1)
264 gimple s;
266 if (TREE_CODE (cond) == SSA_NAME
267 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
269 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
271 *op0 = gimple_assign_rhs1 (s);
272 *op1 = gimple_assign_rhs2 (s);
273 return gimple_assign_rhs_code (s);
276 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
278 tree op = gimple_assign_rhs1 (s);
279 tree type = TREE_TYPE (op);
280 enum tree_code code = parse_predicate (op, op0, op1);
282 return code == ERROR_MARK ? ERROR_MARK
283 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
286 return ERROR_MARK;
289 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
291 *op0 = TREE_OPERAND (cond, 0);
292 *op1 = TREE_OPERAND (cond, 1);
293 return TREE_CODE (cond);
296 return ERROR_MARK;
299 /* Returns the fold of predicate C1 OR C2 at location LOC. */
301 static tree
302 fold_or_predicates (location_t loc, tree c1, tree c2)
304 tree op1a, op1b, op2a, op2b;
305 enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
306 enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
308 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
310 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
311 code2, op2a, op2b);
312 if (t)
313 return t;
316 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
319 /* Returns true if N is either a constant or a SSA_NAME. */
321 static bool
322 constant_or_ssa_name (tree n)
324 switch (TREE_CODE (n))
326 case SSA_NAME:
327 case INTEGER_CST:
328 case REAL_CST:
329 case COMPLEX_CST:
330 case VECTOR_CST:
331 return true;
332 default:
333 return false;
337 /* Returns either a COND_EXPR or the folded expression if the folded
338 expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
339 a constant or a SSA_NAME. */
341 static tree
342 fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
344 tree rhs1, lhs1, cond_expr;
345 cond_expr = fold_ternary (COND_EXPR, type, cond,
346 rhs, lhs);
348 if (cond_expr == NULL_TREE)
349 return build3 (COND_EXPR, type, cond, rhs, lhs);
351 STRIP_USELESS_TYPE_CONVERSION (cond_expr);
353 if (constant_or_ssa_name (cond_expr))
354 return cond_expr;
356 if (TREE_CODE (cond_expr) == ABS_EXPR)
358 rhs1 = TREE_OPERAND (cond_expr, 1);
359 STRIP_USELESS_TYPE_CONVERSION (rhs1);
360 if (constant_or_ssa_name (rhs1))
361 return build1 (ABS_EXPR, type, rhs1);
364 if (TREE_CODE (cond_expr) == MIN_EXPR
365 || TREE_CODE (cond_expr) == MAX_EXPR)
367 lhs1 = TREE_OPERAND (cond_expr, 0);
368 STRIP_USELESS_TYPE_CONVERSION (lhs1);
369 rhs1 = TREE_OPERAND (cond_expr, 1);
370 STRIP_USELESS_TYPE_CONVERSION (rhs1);
371 if (constant_or_ssa_name (rhs1)
372 && constant_or_ssa_name (lhs1))
373 return build2 (TREE_CODE (cond_expr), type, lhs1, rhs1);
375 return build3 (COND_EXPR, type, cond, rhs, lhs);
378 /* Add condition NC to the predicate list of basic block BB. */
380 static inline void
381 add_to_predicate_list (basic_block bb, tree nc)
383 tree bc, *tp;
385 if (is_true_predicate (nc))
386 return;
388 if (!is_predicated (bb))
389 bc = nc;
390 else
392 bc = bb_predicate (bb);
393 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
394 if (is_true_predicate (bc))
396 reset_bb_predicate (bb);
397 return;
401 /* Allow a TRUTH_NOT_EXPR around the main predicate. */
402 if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
403 tp = &TREE_OPERAND (bc, 0);
404 else
405 tp = &bc;
406 if (!is_gimple_condexpr (*tp))
408 gimple_seq stmts;
409 *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
410 add_bb_predicate_gimplified_stmts (bb, stmts);
412 set_bb_predicate (bb, bc);
415 /* Add the condition COND to the previous condition PREV_COND, and add
416 this to the predicate list of the destination of edge E. LOOP is
417 the loop to be if-converted. */
419 static void
420 add_to_dst_predicate_list (struct loop *loop, edge e,
421 tree prev_cond, tree cond)
423 if (!flow_bb_inside_loop_p (loop, e->dest))
424 return;
426 if (!is_true_predicate (prev_cond))
427 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
428 prev_cond, cond);
430 add_to_predicate_list (e->dest, cond);
433 /* Return true if one of the successor edges of BB exits LOOP. */
435 static bool
436 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
438 edge e;
439 edge_iterator ei;
441 FOR_EACH_EDGE (e, ei, bb->succs)
442 if (loop_exit_edge_p (loop, e))
443 return true;
445 return false;
448 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
449 and it belongs to basic block BB.
451 PHI is not if-convertible if:
452 - it has more than 2 arguments.
454 When the flag_tree_loop_if_convert_stores is not set, PHI is not
455 if-convertible if:
456 - a virtual PHI is immediately used in another PHI node,
457 - there is a virtual PHI in a BB other than the loop->header. */
459 static bool
460 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
462 if (dump_file && (dump_flags & TDF_DETAILS))
464 fprintf (dump_file, "-------------------------\n");
465 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
468 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
470 if (dump_file && (dump_flags & TDF_DETAILS))
471 fprintf (dump_file, "More than two phi node args.\n");
472 return false;
475 if (flag_tree_loop_if_convert_stores)
476 return true;
478 /* When the flag_tree_loop_if_convert_stores is not set, check
479 that there are no memory writes in the branches of the loop to be
480 if-converted. */
481 if (virtual_operand_p (gimple_phi_result (phi)))
483 imm_use_iterator imm_iter;
484 use_operand_p use_p;
486 if (bb != loop->header)
488 if (dump_file && (dump_flags & TDF_DETAILS))
489 fprintf (dump_file, "Virtual phi not on loop->header.\n");
490 return false;
493 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
495 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
497 if (dump_file && (dump_flags & TDF_DETAILS))
498 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
499 return false;
504 return true;
507 /* Records the status of a data reference. This struct is attached to
508 each DR->aux field. */
510 struct ifc_dr {
511 /* -1 when not initialized, 0 when false, 1 when true. */
512 int written_at_least_once;
514 /* -1 when not initialized, 0 when false, 1 when true. */
515 int rw_unconditionally;
518 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
519 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
520 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
522 /* Returns true when the memory references of STMT are read or written
523 unconditionally. In other words, this function returns true when
524 for every data reference A in STMT there exist other accesses to
525 a data reference with the same base with predicates that add up (OR-up) to
526 the true predicate: this ensures that the data reference A is touched
527 (read or written) on every iteration of the if-converted loop. */
529 static bool
530 memrefs_read_or_written_unconditionally (gimple stmt,
531 vec<data_reference_p> drs)
533 int i, j;
534 data_reference_p a, b;
535 tree ca = bb_predicate (gimple_bb (stmt));
537 for (i = 0; drs.iterate (i, &a); i++)
538 if (DR_STMT (a) == stmt)
540 bool found = false;
541 int x = DR_RW_UNCONDITIONALLY (a);
543 if (x == 0)
544 return false;
546 if (x == 1)
547 continue;
549 for (j = 0; drs.iterate (j, &b); j++)
551 tree ref_base_a = DR_REF (a);
552 tree ref_base_b = DR_REF (b);
554 if (DR_STMT (b) == stmt)
555 continue;
557 while (TREE_CODE (ref_base_a) == COMPONENT_REF
558 || TREE_CODE (ref_base_a) == IMAGPART_EXPR
559 || TREE_CODE (ref_base_a) == REALPART_EXPR)
560 ref_base_a = TREE_OPERAND (ref_base_a, 0);
562 while (TREE_CODE (ref_base_b) == COMPONENT_REF
563 || TREE_CODE (ref_base_b) == IMAGPART_EXPR
564 || TREE_CODE (ref_base_b) == REALPART_EXPR)
565 ref_base_b = TREE_OPERAND (ref_base_b, 0);
567 if (!operand_equal_p (ref_base_a, ref_base_b, 0))
569 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
571 if (DR_RW_UNCONDITIONALLY (b) == 1
572 || is_true_predicate (cb)
573 || is_true_predicate (ca
574 = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
576 DR_RW_UNCONDITIONALLY (a) = 1;
577 DR_RW_UNCONDITIONALLY (b) = 1;
578 found = true;
579 break;
584 if (!found)
586 DR_RW_UNCONDITIONALLY (a) = 0;
587 return false;
591 return true;
594 /* Returns true when the memory references of STMT are unconditionally
595 written. In other words, this function returns true when for every
596 data reference A written in STMT, there exist other writes to the
597 same data reference with predicates that add up (OR-up) to the true
598 predicate: this ensures that the data reference A is written on
599 every iteration of the if-converted loop. */
601 static bool
602 write_memrefs_written_at_least_once (gimple stmt,
603 vec<data_reference_p> drs)
605 int i, j;
606 data_reference_p a, b;
607 tree ca = bb_predicate (gimple_bb (stmt));
609 for (i = 0; drs.iterate (i, &a); i++)
610 if (DR_STMT (a) == stmt
611 && DR_IS_WRITE (a))
613 bool found = false;
614 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
616 if (x == 0)
617 return false;
619 if (x == 1)
620 continue;
622 for (j = 0; drs.iterate (j, &b); j++)
623 if (DR_STMT (b) != stmt
624 && DR_IS_WRITE (b)
625 && same_data_refs_base_objects (a, b))
627 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
629 if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
630 || is_true_predicate (cb)
631 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
632 ca, cb)))
634 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
635 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
636 found = true;
637 break;
641 if (!found)
643 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
644 return false;
648 return true;
651 /* Return true when the memory references of STMT won't trap in the
652 if-converted code. There are two things that we have to check for:
654 - writes to memory occur to writable memory: if-conversion of
655 memory writes transforms the conditional memory writes into
656 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
657 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
658 be executed at all in the original code, it may be a readonly
659 memory. To check that A is not const-qualified, we check that
660 there exists at least an unconditional write to A in the current
661 function.
663 - reads or writes to memory are valid memory accesses for every
664 iteration. To check that the memory accesses are correctly formed
665 and that we are allowed to read and write in these locations, we
666 check that the memory accesses to be if-converted occur at every
667 iteration unconditionally. */
669 static bool
670 ifcvt_memrefs_wont_trap (gimple stmt, vec<data_reference_p> refs)
672 return write_memrefs_written_at_least_once (stmt, refs)
673 && memrefs_read_or_written_unconditionally (stmt, refs);
676 /* Wrapper around gimple_could_trap_p refined for the needs of the
677 if-conversion. Try to prove that the memory accesses of STMT could
678 not trap in the innermost loop containing STMT. */
680 static bool
681 ifcvt_could_trap_p (gimple stmt, vec<data_reference_p> refs)
683 if (gimple_vuse (stmt)
684 && !gimple_could_trap_p_1 (stmt, false, false)
685 && ifcvt_memrefs_wont_trap (stmt, refs))
686 return false;
688 return gimple_could_trap_p (stmt);
691 /* Return true when STMT is if-convertible.
693 GIMPLE_ASSIGN statement is not if-convertible if,
694 - it is not movable,
695 - it could trap,
696 - LHS is not var decl. */
698 static bool
699 if_convertible_gimple_assign_stmt_p (gimple stmt,
700 vec<data_reference_p> refs)
702 tree lhs = gimple_assign_lhs (stmt);
703 basic_block bb;
705 if (dump_file && (dump_flags & TDF_DETAILS))
707 fprintf (dump_file, "-------------------------\n");
708 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
711 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
712 return false;
714 /* Some of these constrains might be too conservative. */
715 if (stmt_ends_bb_p (stmt)
716 || gimple_has_volatile_ops (stmt)
717 || (TREE_CODE (lhs) == SSA_NAME
718 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
719 || gimple_has_side_effects (stmt))
721 if (dump_file && (dump_flags & TDF_DETAILS))
722 fprintf (dump_file, "stmt not suitable for ifcvt\n");
723 return false;
726 if (flag_tree_loop_if_convert_stores)
728 if (ifcvt_could_trap_p (stmt, refs))
730 if (dump_file && (dump_flags & TDF_DETAILS))
731 fprintf (dump_file, "tree could trap...\n");
732 return false;
734 return true;
737 if (gimple_assign_rhs_could_trap_p (stmt))
739 if (dump_file && (dump_flags & TDF_DETAILS))
740 fprintf (dump_file, "tree could trap...\n");
741 return false;
744 bb = gimple_bb (stmt);
746 if (TREE_CODE (lhs) != SSA_NAME
747 && bb != bb->loop_father->header
748 && !bb_with_exit_edge_p (bb->loop_father, bb))
750 if (dump_file && (dump_flags & TDF_DETAILS))
752 fprintf (dump_file, "LHS is not var\n");
753 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
755 return false;
758 return true;
761 /* Return true when STMT is if-convertible.
763 A statement is if-convertible if:
764 - it is an if-convertible GIMPLE_ASSIGN,
765 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
767 static bool
768 if_convertible_stmt_p (gimple stmt, vec<data_reference_p> refs)
770 switch (gimple_code (stmt))
772 case GIMPLE_LABEL:
773 case GIMPLE_DEBUG:
774 case GIMPLE_COND:
775 return true;
777 case GIMPLE_ASSIGN:
778 return if_convertible_gimple_assign_stmt_p (stmt, refs);
780 case GIMPLE_CALL:
782 tree fndecl = gimple_call_fndecl (stmt);
783 if (fndecl)
785 int flags = gimple_call_flags (stmt);
786 if ((flags & ECF_CONST)
787 && !(flags & ECF_LOOPING_CONST_OR_PURE)
788 /* We can only vectorize some builtins at the moment,
789 so restrict if-conversion to those. */
790 && DECL_BUILT_IN (fndecl))
791 return true;
793 return false;
796 default:
797 /* Don't know what to do with 'em so don't do anything. */
798 if (dump_file && (dump_flags & TDF_DETAILS))
800 fprintf (dump_file, "don't know what to do\n");
801 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
803 return false;
804 break;
807 return true;
810 /* Return true when BB is if-convertible. This routine does not check
811 basic block's statements and phis.
813 A basic block is not if-convertible if:
814 - it is non-empty and it is after the exit block (in BFS order),
815 - it is after the exit block but before the latch,
816 - its edges are not normal.
818 EXIT_BB is the basic block containing the exit of the LOOP. BB is
819 inside LOOP. */
821 static bool
822 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
824 edge e;
825 edge_iterator ei;
827 if (dump_file && (dump_flags & TDF_DETAILS))
828 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
830 if (EDGE_COUNT (bb->preds) > 2
831 || EDGE_COUNT (bb->succs) > 2)
832 return false;
834 if (exit_bb)
836 if (bb != loop->latch)
838 if (dump_file && (dump_flags & TDF_DETAILS))
839 fprintf (dump_file, "basic block after exit bb but before latch\n");
840 return false;
842 else if (!empty_block_p (bb))
844 if (dump_file && (dump_flags & TDF_DETAILS))
845 fprintf (dump_file, "non empty basic block after exit bb\n");
846 return false;
848 else if (bb == loop->latch
849 && bb != exit_bb
850 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
852 if (dump_file && (dump_flags & TDF_DETAILS))
853 fprintf (dump_file, "latch is not dominated by exit_block\n");
854 return false;
858 /* Be less adventurous and handle only normal edges. */
859 FOR_EACH_EDGE (e, ei, bb->succs)
860 if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
862 if (dump_file && (dump_flags & TDF_DETAILS))
863 fprintf (dump_file, "Difficult to handle edges\n");
864 return false;
867 /* At least one incoming edge has to be non-critical as otherwise edge
868 predicates are not equal to basic-block predicates of the edge
869 source. */
870 if (EDGE_COUNT (bb->preds) > 1
871 && bb != loop->header)
873 bool found = false;
874 FOR_EACH_EDGE (e, ei, bb->preds)
875 if (EDGE_COUNT (e->src->succs) == 1)
876 found = true;
877 if (!found)
879 if (dump_file && (dump_flags & TDF_DETAILS))
880 fprintf (dump_file, "only critical predecessors\n");
881 return false;
885 return true;
888 /* Return true when all predecessor blocks of BB are visited. The
889 VISITED bitmap keeps track of the visited blocks. */
891 static bool
892 pred_blocks_visited_p (basic_block bb, bitmap *visited)
894 edge e;
895 edge_iterator ei;
896 FOR_EACH_EDGE (e, ei, bb->preds)
897 if (!bitmap_bit_p (*visited, e->src->index))
898 return false;
900 return true;
903 /* Get body of a LOOP in suitable order for if-conversion. It is
904 caller's responsibility to deallocate basic block list.
905 If-conversion suitable order is, breadth first sort (BFS) order
906 with an additional constraint: select a block only if all its
907 predecessors are already selected. */
909 static basic_block *
910 get_loop_body_in_if_conv_order (const struct loop *loop)
912 basic_block *blocks, *blocks_in_bfs_order;
913 basic_block bb;
914 bitmap visited;
915 unsigned int index = 0;
916 unsigned int visited_count = 0;
918 gcc_assert (loop->num_nodes);
919 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
921 blocks = XCNEWVEC (basic_block, loop->num_nodes);
922 visited = BITMAP_ALLOC (NULL);
924 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
926 index = 0;
927 while (index < loop->num_nodes)
929 bb = blocks_in_bfs_order [index];
931 if (bb->flags & BB_IRREDUCIBLE_LOOP)
933 free (blocks_in_bfs_order);
934 BITMAP_FREE (visited);
935 free (blocks);
936 return NULL;
939 if (!bitmap_bit_p (visited, bb->index))
941 if (pred_blocks_visited_p (bb, &visited)
942 || bb == loop->header)
944 /* This block is now visited. */
945 bitmap_set_bit (visited, bb->index);
946 blocks[visited_count++] = bb;
950 index++;
952 if (index == loop->num_nodes
953 && visited_count != loop->num_nodes)
954 /* Not done yet. */
955 index = 0;
957 free (blocks_in_bfs_order);
958 BITMAP_FREE (visited);
959 return blocks;
962 /* Returns true when the analysis of the predicates for all the basic
963 blocks in LOOP succeeded.
965 predicate_bbs first allocates the predicates of the basic blocks.
966 These fields are then initialized with the tree expressions
967 representing the predicates under which a basic block is executed
968 in the LOOP. As the loop->header is executed at each iteration, it
969 has the "true" predicate. Other statements executed under a
970 condition are predicated with that condition, for example
972 | if (x)
973 | S1;
974 | else
975 | S2;
977 S1 will be predicated with "x", and
978 S2 will be predicated with "!x". */
980 static bool
981 predicate_bbs (loop_p loop)
983 unsigned int i;
985 for (i = 0; i < loop->num_nodes; i++)
986 init_bb_predicate (ifc_bbs[i]);
988 for (i = 0; i < loop->num_nodes; i++)
990 basic_block bb = ifc_bbs[i];
991 tree cond;
992 gimple_stmt_iterator itr;
994 /* The loop latch is always executed and has no extra conditions
995 to be processed: skip it. */
996 if (bb == loop->latch)
998 reset_bb_predicate (loop->latch);
999 continue;
1002 cond = bb_predicate (bb);
1004 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1006 gimple stmt = gsi_stmt (itr);
1008 switch (gimple_code (stmt))
1010 case GIMPLE_LABEL:
1011 case GIMPLE_ASSIGN:
1012 case GIMPLE_CALL:
1013 case GIMPLE_DEBUG:
1014 break;
1016 case GIMPLE_COND:
1018 tree c2;
1019 edge true_edge, false_edge;
1020 location_t loc = gimple_location (stmt);
1021 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
1022 boolean_type_node,
1023 gimple_cond_lhs (stmt),
1024 gimple_cond_rhs (stmt));
1026 /* Add new condition into destination's predicate list. */
1027 extract_true_false_edges_from_block (gimple_bb (stmt),
1028 &true_edge, &false_edge);
1030 /* If C is true, then TRUE_EDGE is taken. */
1031 add_to_dst_predicate_list (loop, true_edge,
1032 unshare_expr (cond),
1033 unshare_expr (c));
1035 /* If C is false, then FALSE_EDGE is taken. */
1036 c2 = build1_loc (loc, TRUTH_NOT_EXPR,
1037 boolean_type_node, unshare_expr (c));
1038 add_to_dst_predicate_list (loop, false_edge,
1039 unshare_expr (cond), c2);
1041 cond = NULL_TREE;
1042 break;
1045 default:
1046 /* Not handled yet in if-conversion. */
1047 return false;
1051 /* If current bb has only one successor, then consider it as an
1052 unconditional goto. */
1053 if (single_succ_p (bb))
1055 basic_block bb_n = single_succ (bb);
1057 /* The successor bb inherits the predicate of its
1058 predecessor. If there is no predicate in the predecessor
1059 bb, then consider the successor bb as always executed. */
1060 if (cond == NULL_TREE)
1061 cond = boolean_true_node;
1063 add_to_predicate_list (bb_n, cond);
1067 /* The loop header is always executed. */
1068 reset_bb_predicate (loop->header);
1069 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1070 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1072 return true;
1075 /* Return true when LOOP is if-convertible. This is a helper function
1076 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1077 in if_convertible_loop_p. */
1079 static bool
1080 if_convertible_loop_p_1 (struct loop *loop,
1081 vec<loop_p> *loop_nest,
1082 vec<data_reference_p> *refs,
1083 vec<ddr_p> *ddrs)
1085 bool res;
1086 unsigned int i;
1087 basic_block exit_bb = NULL;
1089 /* Don't if-convert the loop when the data dependences cannot be
1090 computed: the loop won't be vectorized in that case. */
1091 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1092 if (!res)
1093 return false;
1095 calculate_dominance_info (CDI_DOMINATORS);
1097 /* Allow statements that can be handled during if-conversion. */
1098 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1099 if (!ifc_bbs)
1101 if (dump_file && (dump_flags & TDF_DETAILS))
1102 fprintf (dump_file, "Irreducible loop\n");
1103 return false;
1106 for (i = 0; i < loop->num_nodes; i++)
1108 basic_block bb = ifc_bbs[i];
1110 if (!if_convertible_bb_p (loop, bb, exit_bb))
1111 return false;
1113 if (bb_with_exit_edge_p (loop, bb))
1114 exit_bb = bb;
1117 res = predicate_bbs (loop);
1118 if (!res)
1119 return false;
1121 if (flag_tree_loop_if_convert_stores)
1123 data_reference_p dr;
1125 for (i = 0; refs->iterate (i, &dr); i++)
1127 dr->aux = XNEW (struct ifc_dr);
1128 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1129 DR_RW_UNCONDITIONALLY (dr) = -1;
1133 for (i = 0; i < loop->num_nodes; i++)
1135 basic_block bb = ifc_bbs[i];
1136 gimple_stmt_iterator itr;
1138 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1139 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1140 return false;
1142 /* Check the if-convertibility of statements in predicated BBs. */
1143 if (is_predicated (bb))
1144 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1145 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1146 return false;
1149 if (dump_file)
1150 fprintf (dump_file, "Applying if-conversion\n");
1152 return true;
1155 /* Return true when LOOP is if-convertible.
1156 LOOP is if-convertible if:
1157 - it is innermost,
1158 - it has two or more basic blocks,
1159 - it has only one exit,
1160 - loop header is not the exit edge,
1161 - if its basic blocks and phi nodes are if convertible. */
1163 static bool
1164 if_convertible_loop_p (struct loop *loop)
1166 edge e;
1167 edge_iterator ei;
1168 bool res = false;
1169 vec<data_reference_p> refs;
1170 vec<ddr_p> ddrs;
1172 /* Handle only innermost loop. */
1173 if (!loop || loop->inner)
1175 if (dump_file && (dump_flags & TDF_DETAILS))
1176 fprintf (dump_file, "not innermost loop\n");
1177 return false;
1180 /* If only one block, no need for if-conversion. */
1181 if (loop->num_nodes <= 2)
1183 if (dump_file && (dump_flags & TDF_DETAILS))
1184 fprintf (dump_file, "less than 2 basic blocks\n");
1185 return false;
1188 /* More than one loop exit is too much to handle. */
1189 if (!single_exit (loop))
1191 if (dump_file && (dump_flags & TDF_DETAILS))
1192 fprintf (dump_file, "multiple exits\n");
1193 return false;
1196 /* If one of the loop header's edge is an exit edge then do not
1197 apply if-conversion. */
1198 FOR_EACH_EDGE (e, ei, loop->header->succs)
1199 if (loop_exit_edge_p (loop, e))
1200 return false;
1202 refs.create (5);
1203 ddrs.create (25);
1204 stack_vec<loop_p, 3> loop_nest;
1205 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1207 if (flag_tree_loop_if_convert_stores)
1209 data_reference_p dr;
1210 unsigned int i;
1212 for (i = 0; refs.iterate (i, &dr); i++)
1213 free (dr->aux);
1216 loop_nest.release ();
1217 free_data_refs (refs);
1218 free_dependence_relations (ddrs);
1219 return res;
1222 /* Basic block BB has two predecessors. Using predecessor's bb
1223 predicate, set an appropriate condition COND for the PHI node
1224 replacement. Return the true block whose phi arguments are
1225 selected when cond is true. LOOP is the loop containing the
1226 if-converted region, GSI is the place to insert the code for the
1227 if-conversion. */
1229 static basic_block
1230 find_phi_replacement_condition (basic_block bb, tree *cond,
1231 gimple_stmt_iterator *gsi)
1233 edge first_edge, second_edge;
1234 tree tmp_cond;
1236 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1237 first_edge = EDGE_PRED (bb, 0);
1238 second_edge = EDGE_PRED (bb, 1);
1240 /* Prefer an edge with a not negated predicate.
1241 ??? That's a very weak cost model. */
1242 tmp_cond = bb_predicate (first_edge->src);
1243 gcc_assert (tmp_cond);
1244 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1246 edge tmp_edge;
1248 tmp_edge = first_edge;
1249 first_edge = second_edge;
1250 second_edge = tmp_edge;
1253 /* Check if the edge we take the condition from is not critical.
1254 We know that at least one non-critical edge exists. */
1255 if (EDGE_COUNT (first_edge->src->succs) > 1)
1257 *cond = bb_predicate (second_edge->src);
1259 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1260 *cond = TREE_OPERAND (*cond, 0);
1261 else
1262 /* Select non loop header bb. */
1263 first_edge = second_edge;
1265 else
1266 *cond = bb_predicate (first_edge->src);
1268 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1269 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1270 is_gimple_condexpr, NULL_TREE,
1271 true, GSI_SAME_STMT);
1273 return first_edge->src;
1276 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1277 This routine does not handle PHI nodes with more than two
1278 arguments.
1280 For example,
1281 S1: A = PHI <x1(1), x2(5)>
1282 is converted into,
1283 S2: A = cond ? x1 : x2;
1285 The generated code is inserted at GSI that points to the top of
1286 basic block's statement list. When COND is true, phi arg from
1287 TRUE_BB is selected. */
1289 static void
1290 predicate_scalar_phi (gimple phi, tree cond,
1291 basic_block true_bb,
1292 gimple_stmt_iterator *gsi)
1294 gimple new_stmt;
1295 basic_block bb;
1296 tree rhs, res, arg, scev;
1298 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1299 && gimple_phi_num_args (phi) == 2);
1301 res = gimple_phi_result (phi);
1302 /* Do not handle virtual phi nodes. */
1303 if (virtual_operand_p (res))
1304 return;
1306 bb = gimple_bb (phi);
1308 if ((arg = degenerate_phi_result (phi))
1309 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1310 res))
1311 && !chrec_contains_undetermined (scev)
1312 && scev != res
1313 && (arg = gimple_phi_arg_def (phi, 0))))
1314 rhs = arg;
1315 else
1317 tree arg_0, arg_1;
1318 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1319 if (EDGE_PRED (bb, 1)->src == true_bb)
1321 arg_0 = gimple_phi_arg_def (phi, 1);
1322 arg_1 = gimple_phi_arg_def (phi, 0);
1324 else
1326 arg_0 = gimple_phi_arg_def (phi, 0);
1327 arg_1 = gimple_phi_arg_def (phi, 1);
1330 /* Build new RHS using selected condition and arguments. */
1331 rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
1332 arg_0, arg_1);
1335 new_stmt = gimple_build_assign (res, rhs);
1336 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1337 update_stmt (new_stmt);
1339 if (dump_file && (dump_flags & TDF_DETAILS))
1341 fprintf (dump_file, "new phi replacement stmt\n");
1342 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1346 /* Replaces in LOOP all the scalar phi nodes other than those in the
1347 LOOP->header block with conditional modify expressions. */
1349 static void
1350 predicate_all_scalar_phis (struct loop *loop)
1352 basic_block bb;
1353 unsigned int orig_loop_num_nodes = loop->num_nodes;
1354 unsigned int i;
1356 for (i = 1; i < orig_loop_num_nodes; i++)
1358 gimple phi;
1359 tree cond = NULL_TREE;
1360 gimple_stmt_iterator gsi, phi_gsi;
1361 basic_block true_bb = NULL;
1362 bb = ifc_bbs[i];
1364 if (bb == loop->header)
1365 continue;
1367 phi_gsi = gsi_start_phis (bb);
1368 if (gsi_end_p (phi_gsi))
1369 continue;
1371 /* BB has two predecessors. Using predecessor's aux field, set
1372 appropriate condition for the PHI node replacement. */
1373 gsi = gsi_after_labels (bb);
1374 true_bb = find_phi_replacement_condition (bb, &cond, &gsi);
1376 while (!gsi_end_p (phi_gsi))
1378 phi = gsi_stmt (phi_gsi);
1379 predicate_scalar_phi (phi, cond, true_bb, &gsi);
1380 release_phi_node (phi);
1381 gsi_next (&phi_gsi);
1384 set_phi_nodes (bb, NULL);
1388 /* Insert in each basic block of LOOP the statements produced by the
1389 gimplification of the predicates. */
1391 static void
1392 insert_gimplified_predicates (loop_p loop)
1394 unsigned int i;
1396 for (i = 0; i < loop->num_nodes; i++)
1398 basic_block bb = ifc_bbs[i];
1399 gimple_seq stmts;
1401 if (!is_predicated (bb))
1403 /* Do not insert statements for a basic block that is not
1404 predicated. Also make sure that the predicate of the
1405 basic block is set to true. */
1406 reset_bb_predicate (bb);
1407 continue;
1410 stmts = bb_predicate_gimplified_stmts (bb);
1411 if (stmts)
1413 if (flag_tree_loop_if_convert_stores)
1415 /* Insert the predicate of the BB just after the label,
1416 as the if-conversion of memory writes will use this
1417 predicate. */
1418 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1419 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1421 else
1423 /* Insert the predicate of the BB at the end of the BB
1424 as this would reduce the register pressure: the only
1425 use of this predicate will be in successor BBs. */
1426 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1428 if (gsi_end_p (gsi)
1429 || stmt_ends_bb_p (gsi_stmt (gsi)))
1430 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1431 else
1432 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1435 /* Once the sequence is code generated, set it to NULL. */
1436 set_bb_predicate_gimplified_stmts (bb, NULL);
1441 /* Predicate each write to memory in LOOP.
1443 This function transforms control flow constructs containing memory
1444 writes of the form:
1446 | for (i = 0; i < N; i++)
1447 | if (cond)
1448 | A[i] = expr;
1450 into the following form that does not contain control flow:
1452 | for (i = 0; i < N; i++)
1453 | A[i] = cond ? expr : A[i];
1455 The original CFG looks like this:
1457 | bb_0
1458 | i = 0
1459 | end_bb_0
1461 | bb_1
1462 | if (i < N) goto bb_5 else goto bb_2
1463 | end_bb_1
1465 | bb_2
1466 | cond = some_computation;
1467 | if (cond) goto bb_3 else goto bb_4
1468 | end_bb_2
1470 | bb_3
1471 | A[i] = expr;
1472 | goto bb_4
1473 | end_bb_3
1475 | bb_4
1476 | goto bb_1
1477 | end_bb_4
1479 insert_gimplified_predicates inserts the computation of the COND
1480 expression at the beginning of the destination basic block:
1482 | bb_0
1483 | i = 0
1484 | end_bb_0
1486 | bb_1
1487 | if (i < N) goto bb_5 else goto bb_2
1488 | end_bb_1
1490 | bb_2
1491 | cond = some_computation;
1492 | if (cond) goto bb_3 else goto bb_4
1493 | end_bb_2
1495 | bb_3
1496 | cond = some_computation;
1497 | A[i] = expr;
1498 | goto bb_4
1499 | end_bb_3
1501 | bb_4
1502 | goto bb_1
1503 | end_bb_4
1505 predicate_mem_writes is then predicating the memory write as follows:
1507 | bb_0
1508 | i = 0
1509 | end_bb_0
1511 | bb_1
1512 | if (i < N) goto bb_5 else goto bb_2
1513 | end_bb_1
1515 | bb_2
1516 | if (cond) goto bb_3 else goto bb_4
1517 | end_bb_2
1519 | bb_3
1520 | cond = some_computation;
1521 | A[i] = cond ? expr : A[i];
1522 | goto bb_4
1523 | end_bb_3
1525 | bb_4
1526 | goto bb_1
1527 | end_bb_4
1529 and finally combine_blocks removes the basic block boundaries making
1530 the loop vectorizable:
1532 | bb_0
1533 | i = 0
1534 | if (i < N) goto bb_5 else goto bb_1
1535 | end_bb_0
1537 | bb_1
1538 | cond = some_computation;
1539 | A[i] = cond ? expr : A[i];
1540 | if (i < N) goto bb_5 else goto bb_4
1541 | end_bb_1
1543 | bb_4
1544 | goto bb_1
1545 | end_bb_4
1548 static void
1549 predicate_mem_writes (loop_p loop)
1551 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1553 for (i = 1; i < orig_loop_num_nodes; i++)
1555 gimple_stmt_iterator gsi;
1556 basic_block bb = ifc_bbs[i];
1557 tree cond = bb_predicate (bb);
1558 bool swap;
1559 gimple stmt;
1561 if (is_true_predicate (cond))
1562 continue;
1564 swap = false;
1565 if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1567 swap = true;
1568 cond = TREE_OPERAND (cond, 0);
1571 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1572 if ((stmt = gsi_stmt (gsi))
1573 && gimple_assign_single_p (stmt)
1574 && gimple_vdef (stmt))
1576 tree lhs = gimple_assign_lhs (stmt);
1577 tree rhs = gimple_assign_rhs1 (stmt);
1578 tree type = TREE_TYPE (lhs);
1580 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1581 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1582 if (swap)
1584 tree tem = lhs;
1585 lhs = rhs;
1586 rhs = tem;
1588 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1589 is_gimple_condexpr, NULL_TREE,
1590 true, GSI_SAME_STMT);
1591 rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
1592 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1593 update_stmt (stmt);
1598 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1599 other than the exit and latch of the LOOP. Also resets the
1600 GIMPLE_DEBUG information. */
1602 static void
1603 remove_conditions_and_labels (loop_p loop)
1605 gimple_stmt_iterator gsi;
1606 unsigned int i;
1608 for (i = 0; i < loop->num_nodes; i++)
1610 basic_block bb = ifc_bbs[i];
1612 if (bb_with_exit_edge_p (loop, bb)
1613 || bb == loop->latch)
1614 continue;
1616 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1617 switch (gimple_code (gsi_stmt (gsi)))
1619 case GIMPLE_COND:
1620 case GIMPLE_LABEL:
1621 gsi_remove (&gsi, true);
1622 break;
1624 case GIMPLE_DEBUG:
1625 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1626 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1628 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1629 update_stmt (gsi_stmt (gsi));
1631 gsi_next (&gsi);
1632 break;
1634 default:
1635 gsi_next (&gsi);
1640 /* Combine all the basic blocks from LOOP into one or two super basic
1641 blocks. Replace PHI nodes with conditional modify expressions. */
1643 static void
1644 combine_blocks (struct loop *loop)
1646 basic_block bb, exit_bb, merge_target_bb;
1647 unsigned int orig_loop_num_nodes = loop->num_nodes;
1648 unsigned int i;
1649 edge e;
1650 edge_iterator ei;
1652 remove_conditions_and_labels (loop);
1653 insert_gimplified_predicates (loop);
1654 predicate_all_scalar_phis (loop);
1656 if (flag_tree_loop_if_convert_stores)
1657 predicate_mem_writes (loop);
1659 /* Merge basic blocks: first remove all the edges in the loop,
1660 except for those from the exit block. */
1661 exit_bb = NULL;
1662 for (i = 0; i < orig_loop_num_nodes; i++)
1664 bb = ifc_bbs[i];
1665 free_bb_predicate (bb);
1666 if (bb_with_exit_edge_p (loop, bb))
1668 gcc_assert (exit_bb == NULL);
1669 exit_bb = bb;
1672 gcc_assert (exit_bb != loop->latch);
1674 for (i = 1; i < orig_loop_num_nodes; i++)
1676 bb = ifc_bbs[i];
1678 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1680 if (e->src == exit_bb)
1681 ei_next (&ei);
1682 else
1683 remove_edge (e);
1687 if (exit_bb != NULL)
1689 if (exit_bb != loop->header)
1691 /* Connect this node to loop header. */
1692 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1693 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1696 /* Redirect non-exit edges to loop->latch. */
1697 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1699 if (!loop_exit_edge_p (loop, e))
1700 redirect_edge_and_branch (e, loop->latch);
1702 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1704 else
1706 /* If the loop does not have an exit, reconnect header and latch. */
1707 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1708 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1711 merge_target_bb = loop->header;
1712 for (i = 1; i < orig_loop_num_nodes; i++)
1714 gimple_stmt_iterator gsi;
1715 gimple_stmt_iterator last;
1717 bb = ifc_bbs[i];
1719 if (bb == exit_bb || bb == loop->latch)
1720 continue;
1722 /* Make stmts member of loop->header. */
1723 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1724 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1726 /* Update stmt list. */
1727 last = gsi_last_bb (merge_target_bb);
1728 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1729 set_bb_seq (bb, NULL);
1731 delete_basic_block (bb);
1734 /* If possible, merge loop header to the block with the exit edge.
1735 This reduces the number of basic blocks to two, to please the
1736 vectorizer that handles only loops with two nodes. */
1737 if (exit_bb
1738 && exit_bb != loop->header
1739 && can_merge_blocks_p (loop->header, exit_bb))
1740 merge_blocks (loop->header, exit_bb);
1742 free (ifc_bbs);
1743 ifc_bbs = NULL;
1746 /* If-convert LOOP when it is legal. For the moment this pass has no
1747 profitability analysis. Returns true when something changed. */
1749 static bool
1750 tree_if_conversion (struct loop *loop)
1752 bool changed = false;
1753 ifc_bbs = NULL;
1755 if (!if_convertible_loop_p (loop)
1756 || !dbg_cnt (if_conversion_tree))
1757 goto cleanup;
1759 /* Now all statements are if-convertible. Combine all the basic
1760 blocks into one huge basic block doing the if-conversion
1761 on-the-fly. */
1762 combine_blocks (loop);
1764 if (flag_tree_loop_if_convert_stores)
1765 mark_virtual_operands_for_renaming (cfun);
1767 changed = true;
1769 cleanup:
1770 if (ifc_bbs)
1772 unsigned int i;
1774 for (i = 0; i < loop->num_nodes; i++)
1775 free_bb_predicate (ifc_bbs[i]);
1777 free (ifc_bbs);
1778 ifc_bbs = NULL;
1781 return changed;
1784 /* Tree if-conversion pass management. */
1786 static unsigned int
1787 main_tree_if_conversion (void)
1789 loop_iterator li;
1790 struct loop *loop;
1791 bool changed = false;
1792 unsigned todo = 0;
1794 if (number_of_loops (cfun) <= 1)
1795 return 0;
1797 FOR_EACH_LOOP (li, loop, 0)
1798 if (flag_tree_loop_if_convert == 1
1799 || flag_tree_loop_if_convert_stores == 1
1800 || flag_tree_loop_vectorize
1801 || loop->force_vect)
1802 changed |= tree_if_conversion (loop);
1804 if (changed)
1805 todo |= TODO_cleanup_cfg;
1807 if (changed && flag_tree_loop_if_convert_stores)
1808 todo |= TODO_update_ssa_only_virtuals;
1810 #ifdef ENABLE_CHECKING
1812 basic_block bb;
1813 FOR_EACH_BB (bb)
1814 gcc_assert (!bb->aux);
1816 #endif
1818 return todo;
1821 /* Returns true when the if-conversion pass is enabled. */
1823 static bool
1824 gate_tree_if_conversion (void)
1826 return (((flag_tree_loop_vectorize || cfun->has_force_vect_loops)
1827 && flag_tree_loop_if_convert != 0)
1828 || flag_tree_loop_if_convert == 1
1829 || flag_tree_loop_if_convert_stores == 1);
1832 namespace {
1834 const pass_data pass_data_if_conversion =
1836 GIMPLE_PASS, /* type */
1837 "ifcvt", /* name */
1838 OPTGROUP_NONE, /* optinfo_flags */
1839 true, /* has_gate */
1840 true, /* has_execute */
1841 TV_NONE, /* tv_id */
1842 ( PROP_cfg | PROP_ssa ), /* properties_required */
1843 0, /* properties_provided */
1844 0, /* properties_destroyed */
1845 0, /* todo_flags_start */
1846 ( TODO_verify_stmts | TODO_verify_flow
1847 | TODO_verify_ssa ), /* todo_flags_finish */
1850 class pass_if_conversion : public gimple_opt_pass
1852 public:
1853 pass_if_conversion (gcc::context *ctxt)
1854 : gimple_opt_pass (pass_data_if_conversion, ctxt)
1857 /* opt_pass methods: */
1858 bool gate () { return gate_tree_if_conversion (); }
1859 unsigned int execute () { return main_tree_if_conversion (); }
1861 }; // class pass_if_conversion
1863 } // anon namespace
1865 gimple_opt_pass *
1866 make_pass_if_conversion (gcc::context *ctxt)
1868 return new pass_if_conversion (ctxt);