* ipa/devirt9.C: Fix previous change.
[official-gcc.git] / gcc / tree-if-conv.c
blobdd3925ad859cf7c6698695fd6dd38855489acfad
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 "gimple.h"
93 #include "gimplify.h"
94 #include "gimple-iterator.h"
95 #include "gimplify-me.h"
96 #include "gimple-ssa.h"
97 #include "tree-cfg.h"
98 #include "tree-phinodes.h"
99 #include "ssa-iterators.h"
100 #include "stringpool.h"
101 #include "tree-ssanames.h"
102 #include "tree-into-ssa.h"
103 #include "tree-ssa.h"
104 #include "cfgloop.h"
105 #include "tree-chrec.h"
106 #include "tree-data-ref.h"
107 #include "tree-scalar-evolution.h"
108 #include "tree-pass.h"
109 #include "dbgcnt.h"
111 /* List of basic blocks in if-conversion-suitable order. */
112 static basic_block *ifc_bbs;
114 /* Structure used to predicate basic blocks. This is attached to the
115 ->aux field of the BBs in the loop to be if-converted. */
116 typedef struct bb_predicate_s {
118 /* The condition under which this basic block is executed. */
119 tree predicate;
121 /* PREDICATE is gimplified, and the sequence of statements is
122 recorded here, in order to avoid the duplication of computations
123 that occur in previous conditions. See PR44483. */
124 gimple_seq predicate_gimplified_stmts;
125 } *bb_predicate_p;
127 /* Returns true when the basic block BB has a predicate. */
129 static inline bool
130 bb_has_predicate (basic_block bb)
132 return bb->aux != NULL;
135 /* Returns the gimplified predicate for basic block BB. */
137 static inline tree
138 bb_predicate (basic_block bb)
140 return ((bb_predicate_p) bb->aux)->predicate;
143 /* Sets the gimplified predicate COND for basic block BB. */
145 static inline void
146 set_bb_predicate (basic_block bb, tree cond)
148 gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
149 && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
150 || is_gimple_condexpr (cond));
151 ((bb_predicate_p) bb->aux)->predicate = cond;
154 /* Returns the sequence of statements of the gimplification of the
155 predicate for basic block BB. */
157 static inline gimple_seq
158 bb_predicate_gimplified_stmts (basic_block bb)
160 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
163 /* Sets the sequence of statements STMTS of the gimplification of the
164 predicate for basic block BB. */
166 static inline void
167 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
169 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
172 /* Adds the sequence of statements STMTS to the sequence of statements
173 of the predicate for basic block BB. */
175 static inline void
176 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
178 gimple_seq_add_seq
179 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
182 /* Initializes to TRUE the predicate of basic block BB. */
184 static inline void
185 init_bb_predicate (basic_block bb)
187 bb->aux = XNEW (struct bb_predicate_s);
188 set_bb_predicate_gimplified_stmts (bb, NULL);
189 set_bb_predicate (bb, boolean_true_node);
192 /* Free the predicate of basic block BB. */
194 static inline void
195 free_bb_predicate (basic_block bb)
197 gimple_seq stmts;
199 if (!bb_has_predicate (bb))
200 return;
202 /* Release the SSA_NAMEs created for the gimplification of the
203 predicate. */
204 stmts = bb_predicate_gimplified_stmts (bb);
205 if (stmts)
207 gimple_stmt_iterator i;
209 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
210 free_stmt_operands (gsi_stmt (i));
213 free (bb->aux);
214 bb->aux = NULL;
217 /* Free the predicate of BB and reinitialize it with the true
218 predicate. */
220 static inline void
221 reset_bb_predicate (basic_block bb)
223 free_bb_predicate (bb);
224 init_bb_predicate (bb);
227 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
228 the expression EXPR. Inserts the statement created for this
229 computation before GSI and leaves the iterator GSI at the same
230 statement. */
232 static tree
233 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
235 tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
236 gimple stmt = gimple_build_assign (new_name, expr);
237 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
238 return new_name;
241 /* Return true when COND is a true predicate. */
243 static inline bool
244 is_true_predicate (tree cond)
246 return (cond == NULL_TREE
247 || cond == boolean_true_node
248 || integer_onep (cond));
251 /* Returns true when BB has a predicate that is not trivial: true or
252 NULL_TREE. */
254 static inline bool
255 is_predicated (basic_block bb)
257 return !is_true_predicate (bb_predicate (bb));
260 /* Parses the predicate COND and returns its comparison code and
261 operands OP0 and OP1. */
263 static enum tree_code
264 parse_predicate (tree cond, tree *op0, tree *op1)
266 gimple s;
268 if (TREE_CODE (cond) == SSA_NAME
269 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
271 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
273 *op0 = gimple_assign_rhs1 (s);
274 *op1 = gimple_assign_rhs2 (s);
275 return gimple_assign_rhs_code (s);
278 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
280 tree op = gimple_assign_rhs1 (s);
281 tree type = TREE_TYPE (op);
282 enum tree_code code = parse_predicate (op, op0, op1);
284 return code == ERROR_MARK ? ERROR_MARK
285 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
288 return ERROR_MARK;
291 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
293 *op0 = TREE_OPERAND (cond, 0);
294 *op1 = TREE_OPERAND (cond, 1);
295 return TREE_CODE (cond);
298 return ERROR_MARK;
301 /* Returns the fold of predicate C1 OR C2 at location LOC. */
303 static tree
304 fold_or_predicates (location_t loc, tree c1, tree c2)
306 tree op1a, op1b, op2a, op2b;
307 enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
308 enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
310 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
312 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
313 code2, op2a, op2b);
314 if (t)
315 return t;
318 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
321 /* Returns true if N is either a constant or a SSA_NAME. */
323 static bool
324 constant_or_ssa_name (tree n)
326 switch (TREE_CODE (n))
328 case SSA_NAME:
329 case INTEGER_CST:
330 case REAL_CST:
331 case COMPLEX_CST:
332 case VECTOR_CST:
333 return true;
334 default:
335 return false;
339 /* Returns either a COND_EXPR or the folded expression if the folded
340 expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
341 a constant or a SSA_NAME. */
343 static tree
344 fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
346 tree rhs1, lhs1, cond_expr;
347 cond_expr = fold_ternary (COND_EXPR, type, cond,
348 rhs, lhs);
350 if (cond_expr == NULL_TREE)
351 return build3 (COND_EXPR, type, cond, rhs, lhs);
353 STRIP_USELESS_TYPE_CONVERSION (cond_expr);
355 if (constant_or_ssa_name (cond_expr))
356 return cond_expr;
358 if (TREE_CODE (cond_expr) == ABS_EXPR)
360 rhs1 = TREE_OPERAND (cond_expr, 1);
361 STRIP_USELESS_TYPE_CONVERSION (rhs1);
362 if (constant_or_ssa_name (rhs1))
363 return build1 (ABS_EXPR, type, rhs1);
366 if (TREE_CODE (cond_expr) == MIN_EXPR
367 || TREE_CODE (cond_expr) == MAX_EXPR)
369 lhs1 = TREE_OPERAND (cond_expr, 0);
370 STRIP_USELESS_TYPE_CONVERSION (lhs1);
371 rhs1 = TREE_OPERAND (cond_expr, 1);
372 STRIP_USELESS_TYPE_CONVERSION (rhs1);
373 if (constant_or_ssa_name (rhs1)
374 && constant_or_ssa_name (lhs1))
375 return build2 (TREE_CODE (cond_expr), type, lhs1, rhs1);
377 return build3 (COND_EXPR, type, cond, rhs, lhs);
380 /* Add condition NC to the predicate list of basic block BB. */
382 static inline void
383 add_to_predicate_list (basic_block bb, tree nc)
385 tree bc, *tp;
387 if (is_true_predicate (nc))
388 return;
390 if (!is_predicated (bb))
391 bc = nc;
392 else
394 bc = bb_predicate (bb);
395 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
396 if (is_true_predicate (bc))
398 reset_bb_predicate (bb);
399 return;
403 /* Allow a TRUTH_NOT_EXPR around the main predicate. */
404 if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
405 tp = &TREE_OPERAND (bc, 0);
406 else
407 tp = &bc;
408 if (!is_gimple_condexpr (*tp))
410 gimple_seq stmts;
411 *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
412 add_bb_predicate_gimplified_stmts (bb, stmts);
414 set_bb_predicate (bb, bc);
417 /* Add the condition COND to the previous condition PREV_COND, and add
418 this to the predicate list of the destination of edge E. LOOP is
419 the loop to be if-converted. */
421 static void
422 add_to_dst_predicate_list (struct loop *loop, edge e,
423 tree prev_cond, tree cond)
425 if (!flow_bb_inside_loop_p (loop, e->dest))
426 return;
428 if (!is_true_predicate (prev_cond))
429 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
430 prev_cond, cond);
432 add_to_predicate_list (e->dest, cond);
435 /* Return true if one of the successor edges of BB exits LOOP. */
437 static bool
438 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
440 edge e;
441 edge_iterator ei;
443 FOR_EACH_EDGE (e, ei, bb->succs)
444 if (loop_exit_edge_p (loop, e))
445 return true;
447 return false;
450 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
451 and it belongs to basic block BB.
453 PHI is not if-convertible if:
454 - it has more than 2 arguments.
456 When the flag_tree_loop_if_convert_stores is not set, PHI is not
457 if-convertible if:
458 - a virtual PHI is immediately used in another PHI node,
459 - there is a virtual PHI in a BB other than the loop->header. */
461 static bool
462 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
464 if (dump_file && (dump_flags & TDF_DETAILS))
466 fprintf (dump_file, "-------------------------\n");
467 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
470 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
472 if (dump_file && (dump_flags & TDF_DETAILS))
473 fprintf (dump_file, "More than two phi node args.\n");
474 return false;
477 if (flag_tree_loop_if_convert_stores)
478 return true;
480 /* When the flag_tree_loop_if_convert_stores is not set, check
481 that there are no memory writes in the branches of the loop to be
482 if-converted. */
483 if (virtual_operand_p (gimple_phi_result (phi)))
485 imm_use_iterator imm_iter;
486 use_operand_p use_p;
488 if (bb != loop->header)
490 if (dump_file && (dump_flags & TDF_DETAILS))
491 fprintf (dump_file, "Virtual phi not on loop->header.\n");
492 return false;
495 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
497 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
499 if (dump_file && (dump_flags & TDF_DETAILS))
500 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
501 return false;
506 return true;
509 /* Records the status of a data reference. This struct is attached to
510 each DR->aux field. */
512 struct ifc_dr {
513 /* -1 when not initialized, 0 when false, 1 when true. */
514 int written_at_least_once;
516 /* -1 when not initialized, 0 when false, 1 when true. */
517 int rw_unconditionally;
520 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
521 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
522 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
524 /* Returns true when the memory references of STMT are read or written
525 unconditionally. In other words, this function returns true when
526 for every data reference A in STMT there exist other accesses to
527 a data reference with the same base with predicates that add up (OR-up) to
528 the true predicate: this ensures that the data reference A is touched
529 (read or written) on every iteration of the if-converted loop. */
531 static bool
532 memrefs_read_or_written_unconditionally (gimple stmt,
533 vec<data_reference_p> drs)
535 int i, j;
536 data_reference_p a, b;
537 tree ca = bb_predicate (gimple_bb (stmt));
539 for (i = 0; drs.iterate (i, &a); i++)
540 if (DR_STMT (a) == stmt)
542 bool found = false;
543 int x = DR_RW_UNCONDITIONALLY (a);
545 if (x == 0)
546 return false;
548 if (x == 1)
549 continue;
551 for (j = 0; drs.iterate (j, &b); j++)
553 tree ref_base_a = DR_REF (a);
554 tree ref_base_b = DR_REF (b);
556 if (DR_STMT (b) == stmt)
557 continue;
559 while (TREE_CODE (ref_base_a) == COMPONENT_REF
560 || TREE_CODE (ref_base_a) == IMAGPART_EXPR
561 || TREE_CODE (ref_base_a) == REALPART_EXPR)
562 ref_base_a = TREE_OPERAND (ref_base_a, 0);
564 while (TREE_CODE (ref_base_b) == COMPONENT_REF
565 || TREE_CODE (ref_base_b) == IMAGPART_EXPR
566 || TREE_CODE (ref_base_b) == REALPART_EXPR)
567 ref_base_b = TREE_OPERAND (ref_base_b, 0);
569 if (!operand_equal_p (ref_base_a, ref_base_b, 0))
571 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
573 if (DR_RW_UNCONDITIONALLY (b) == 1
574 || is_true_predicate (cb)
575 || is_true_predicate (ca
576 = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
578 DR_RW_UNCONDITIONALLY (a) = 1;
579 DR_RW_UNCONDITIONALLY (b) = 1;
580 found = true;
581 break;
586 if (!found)
588 DR_RW_UNCONDITIONALLY (a) = 0;
589 return false;
593 return true;
596 /* Returns true when the memory references of STMT are unconditionally
597 written. In other words, this function returns true when for every
598 data reference A written in STMT, there exist other writes to the
599 same data reference with predicates that add up (OR-up) to the true
600 predicate: this ensures that the data reference A is written on
601 every iteration of the if-converted loop. */
603 static bool
604 write_memrefs_written_at_least_once (gimple stmt,
605 vec<data_reference_p> drs)
607 int i, j;
608 data_reference_p a, b;
609 tree ca = bb_predicate (gimple_bb (stmt));
611 for (i = 0; drs.iterate (i, &a); i++)
612 if (DR_STMT (a) == stmt
613 && DR_IS_WRITE (a))
615 bool found = false;
616 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
618 if (x == 0)
619 return false;
621 if (x == 1)
622 continue;
624 for (j = 0; drs.iterate (j, &b); j++)
625 if (DR_STMT (b) != stmt
626 && DR_IS_WRITE (b)
627 && same_data_refs_base_objects (a, b))
629 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
631 if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
632 || is_true_predicate (cb)
633 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
634 ca, cb)))
636 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
637 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
638 found = true;
639 break;
643 if (!found)
645 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
646 return false;
650 return true;
653 /* Return true when the memory references of STMT won't trap in the
654 if-converted code. There are two things that we have to check for:
656 - writes to memory occur to writable memory: if-conversion of
657 memory writes transforms the conditional memory writes into
658 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
659 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
660 be executed at all in the original code, it may be a readonly
661 memory. To check that A is not const-qualified, we check that
662 there exists at least an unconditional write to A in the current
663 function.
665 - reads or writes to memory are valid memory accesses for every
666 iteration. To check that the memory accesses are correctly formed
667 and that we are allowed to read and write in these locations, we
668 check that the memory accesses to be if-converted occur at every
669 iteration unconditionally. */
671 static bool
672 ifcvt_memrefs_wont_trap (gimple stmt, vec<data_reference_p> refs)
674 return write_memrefs_written_at_least_once (stmt, refs)
675 && memrefs_read_or_written_unconditionally (stmt, refs);
678 /* Wrapper around gimple_could_trap_p refined for the needs of the
679 if-conversion. Try to prove that the memory accesses of STMT could
680 not trap in the innermost loop containing STMT. */
682 static bool
683 ifcvt_could_trap_p (gimple stmt, vec<data_reference_p> refs)
685 if (gimple_vuse (stmt)
686 && !gimple_could_trap_p_1 (stmt, false, false)
687 && ifcvt_memrefs_wont_trap (stmt, refs))
688 return false;
690 return gimple_could_trap_p (stmt);
693 /* Return true when STMT is if-convertible.
695 GIMPLE_ASSIGN statement is not if-convertible if,
696 - it is not movable,
697 - it could trap,
698 - LHS is not var decl. */
700 static bool
701 if_convertible_gimple_assign_stmt_p (gimple stmt,
702 vec<data_reference_p> refs)
704 tree lhs = gimple_assign_lhs (stmt);
705 basic_block bb;
707 if (dump_file && (dump_flags & TDF_DETAILS))
709 fprintf (dump_file, "-------------------------\n");
710 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
713 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
714 return false;
716 /* Some of these constrains might be too conservative. */
717 if (stmt_ends_bb_p (stmt)
718 || gimple_has_volatile_ops (stmt)
719 || (TREE_CODE (lhs) == SSA_NAME
720 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
721 || gimple_has_side_effects (stmt))
723 if (dump_file && (dump_flags & TDF_DETAILS))
724 fprintf (dump_file, "stmt not suitable for ifcvt\n");
725 return false;
728 if (flag_tree_loop_if_convert_stores)
730 if (ifcvt_could_trap_p (stmt, refs))
732 if (dump_file && (dump_flags & TDF_DETAILS))
733 fprintf (dump_file, "tree could trap...\n");
734 return false;
736 return true;
739 if (gimple_assign_rhs_could_trap_p (stmt))
741 if (dump_file && (dump_flags & TDF_DETAILS))
742 fprintf (dump_file, "tree could trap...\n");
743 return false;
746 bb = gimple_bb (stmt);
748 if (TREE_CODE (lhs) != SSA_NAME
749 && bb != bb->loop_father->header
750 && !bb_with_exit_edge_p (bb->loop_father, bb))
752 if (dump_file && (dump_flags & TDF_DETAILS))
754 fprintf (dump_file, "LHS is not var\n");
755 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
757 return false;
760 return true;
763 /* Return true when STMT is if-convertible.
765 A statement is if-convertible if:
766 - it is an if-convertible GIMPLE_ASSIGN,
767 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
769 static bool
770 if_convertible_stmt_p (gimple stmt, vec<data_reference_p> refs)
772 switch (gimple_code (stmt))
774 case GIMPLE_LABEL:
775 case GIMPLE_DEBUG:
776 case GIMPLE_COND:
777 return true;
779 case GIMPLE_ASSIGN:
780 return if_convertible_gimple_assign_stmt_p (stmt, refs);
782 case GIMPLE_CALL:
784 tree fndecl = gimple_call_fndecl (stmt);
785 if (fndecl)
787 int flags = gimple_call_flags (stmt);
788 if ((flags & ECF_CONST)
789 && !(flags & ECF_LOOPING_CONST_OR_PURE)
790 /* We can only vectorize some builtins at the moment,
791 so restrict if-conversion to those. */
792 && DECL_BUILT_IN (fndecl))
793 return true;
795 return false;
798 default:
799 /* Don't know what to do with 'em so don't do anything. */
800 if (dump_file && (dump_flags & TDF_DETAILS))
802 fprintf (dump_file, "don't know what to do\n");
803 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
805 return false;
806 break;
809 return true;
812 /* Return true when BB is if-convertible. This routine does not check
813 basic block's statements and phis.
815 A basic block is not if-convertible if:
816 - it is non-empty and it is after the exit block (in BFS order),
817 - it is after the exit block but before the latch,
818 - its edges are not normal.
820 EXIT_BB is the basic block containing the exit of the LOOP. BB is
821 inside LOOP. */
823 static bool
824 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
826 edge e;
827 edge_iterator ei;
829 if (dump_file && (dump_flags & TDF_DETAILS))
830 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
832 if (EDGE_COUNT (bb->preds) > 2
833 || EDGE_COUNT (bb->succs) > 2)
834 return false;
836 if (exit_bb)
838 if (bb != loop->latch)
840 if (dump_file && (dump_flags & TDF_DETAILS))
841 fprintf (dump_file, "basic block after exit bb but before latch\n");
842 return false;
844 else if (!empty_block_p (bb))
846 if (dump_file && (dump_flags & TDF_DETAILS))
847 fprintf (dump_file, "non empty basic block after exit bb\n");
848 return false;
850 else if (bb == loop->latch
851 && bb != exit_bb
852 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
854 if (dump_file && (dump_flags & TDF_DETAILS))
855 fprintf (dump_file, "latch is not dominated by exit_block\n");
856 return false;
860 /* Be less adventurous and handle only normal edges. */
861 FOR_EACH_EDGE (e, ei, bb->succs)
862 if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
864 if (dump_file && (dump_flags & TDF_DETAILS))
865 fprintf (dump_file, "Difficult to handle edges\n");
866 return false;
869 /* At least one incoming edge has to be non-critical as otherwise edge
870 predicates are not equal to basic-block predicates of the edge
871 source. */
872 if (EDGE_COUNT (bb->preds) > 1
873 && bb != loop->header)
875 bool found = false;
876 FOR_EACH_EDGE (e, ei, bb->preds)
877 if (EDGE_COUNT (e->src->succs) == 1)
878 found = true;
879 if (!found)
881 if (dump_file && (dump_flags & TDF_DETAILS))
882 fprintf (dump_file, "only critical predecessors\n");
883 return false;
887 return true;
890 /* Return true when all predecessor blocks of BB are visited. The
891 VISITED bitmap keeps track of the visited blocks. */
893 static bool
894 pred_blocks_visited_p (basic_block bb, bitmap *visited)
896 edge e;
897 edge_iterator ei;
898 FOR_EACH_EDGE (e, ei, bb->preds)
899 if (!bitmap_bit_p (*visited, e->src->index))
900 return false;
902 return true;
905 /* Get body of a LOOP in suitable order for if-conversion. It is
906 caller's responsibility to deallocate basic block list.
907 If-conversion suitable order is, breadth first sort (BFS) order
908 with an additional constraint: select a block only if all its
909 predecessors are already selected. */
911 static basic_block *
912 get_loop_body_in_if_conv_order (const struct loop *loop)
914 basic_block *blocks, *blocks_in_bfs_order;
915 basic_block bb;
916 bitmap visited;
917 unsigned int index = 0;
918 unsigned int visited_count = 0;
920 gcc_assert (loop->num_nodes);
921 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
923 blocks = XCNEWVEC (basic_block, loop->num_nodes);
924 visited = BITMAP_ALLOC (NULL);
926 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
928 index = 0;
929 while (index < loop->num_nodes)
931 bb = blocks_in_bfs_order [index];
933 if (bb->flags & BB_IRREDUCIBLE_LOOP)
935 free (blocks_in_bfs_order);
936 BITMAP_FREE (visited);
937 free (blocks);
938 return NULL;
941 if (!bitmap_bit_p (visited, bb->index))
943 if (pred_blocks_visited_p (bb, &visited)
944 || bb == loop->header)
946 /* This block is now visited. */
947 bitmap_set_bit (visited, bb->index);
948 blocks[visited_count++] = bb;
952 index++;
954 if (index == loop->num_nodes
955 && visited_count != loop->num_nodes)
956 /* Not done yet. */
957 index = 0;
959 free (blocks_in_bfs_order);
960 BITMAP_FREE (visited);
961 return blocks;
964 /* Returns true when the analysis of the predicates for all the basic
965 blocks in LOOP succeeded.
967 predicate_bbs first allocates the predicates of the basic blocks.
968 These fields are then initialized with the tree expressions
969 representing the predicates under which a basic block is executed
970 in the LOOP. As the loop->header is executed at each iteration, it
971 has the "true" predicate. Other statements executed under a
972 condition are predicated with that condition, for example
974 | if (x)
975 | S1;
976 | else
977 | S2;
979 S1 will be predicated with "x", and
980 S2 will be predicated with "!x". */
982 static bool
983 predicate_bbs (loop_p loop)
985 unsigned int i;
987 for (i = 0; i < loop->num_nodes; i++)
988 init_bb_predicate (ifc_bbs[i]);
990 for (i = 0; i < loop->num_nodes; i++)
992 basic_block bb = ifc_bbs[i];
993 tree cond;
994 gimple_stmt_iterator itr;
996 /* The loop latch is always executed and has no extra conditions
997 to be processed: skip it. */
998 if (bb == loop->latch)
1000 reset_bb_predicate (loop->latch);
1001 continue;
1004 cond = bb_predicate (bb);
1006 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1008 gimple stmt = gsi_stmt (itr);
1010 switch (gimple_code (stmt))
1012 case GIMPLE_LABEL:
1013 case GIMPLE_ASSIGN:
1014 case GIMPLE_CALL:
1015 case GIMPLE_DEBUG:
1016 break;
1018 case GIMPLE_COND:
1020 tree c2;
1021 edge true_edge, false_edge;
1022 location_t loc = gimple_location (stmt);
1023 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
1024 boolean_type_node,
1025 gimple_cond_lhs (stmt),
1026 gimple_cond_rhs (stmt));
1028 /* Add new condition into destination's predicate list. */
1029 extract_true_false_edges_from_block (gimple_bb (stmt),
1030 &true_edge, &false_edge);
1032 /* If C is true, then TRUE_EDGE is taken. */
1033 add_to_dst_predicate_list (loop, true_edge,
1034 unshare_expr (cond),
1035 unshare_expr (c));
1037 /* If C is false, then FALSE_EDGE is taken. */
1038 c2 = build1_loc (loc, TRUTH_NOT_EXPR,
1039 boolean_type_node, unshare_expr (c));
1040 add_to_dst_predicate_list (loop, false_edge,
1041 unshare_expr (cond), c2);
1043 cond = NULL_TREE;
1044 break;
1047 default:
1048 /* Not handled yet in if-conversion. */
1049 return false;
1053 /* If current bb has only one successor, then consider it as an
1054 unconditional goto. */
1055 if (single_succ_p (bb))
1057 basic_block bb_n = single_succ (bb);
1059 /* The successor bb inherits the predicate of its
1060 predecessor. If there is no predicate in the predecessor
1061 bb, then consider the successor bb as always executed. */
1062 if (cond == NULL_TREE)
1063 cond = boolean_true_node;
1065 add_to_predicate_list (bb_n, cond);
1069 /* The loop header is always executed. */
1070 reset_bb_predicate (loop->header);
1071 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1072 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1074 return true;
1077 /* Return true when LOOP is if-convertible. This is a helper function
1078 for if_convertible_loop_p. REFS and DDRS are initialized and freed
1079 in if_convertible_loop_p. */
1081 static bool
1082 if_convertible_loop_p_1 (struct loop *loop,
1083 vec<loop_p> *loop_nest,
1084 vec<data_reference_p> *refs,
1085 vec<ddr_p> *ddrs)
1087 bool res;
1088 unsigned int i;
1089 basic_block exit_bb = NULL;
1091 /* Don't if-convert the loop when the data dependences cannot be
1092 computed: the loop won't be vectorized in that case. */
1093 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1094 if (!res)
1095 return false;
1097 calculate_dominance_info (CDI_DOMINATORS);
1099 /* Allow statements that can be handled during if-conversion. */
1100 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1101 if (!ifc_bbs)
1103 if (dump_file && (dump_flags & TDF_DETAILS))
1104 fprintf (dump_file, "Irreducible loop\n");
1105 return false;
1108 for (i = 0; i < loop->num_nodes; i++)
1110 basic_block bb = ifc_bbs[i];
1112 if (!if_convertible_bb_p (loop, bb, exit_bb))
1113 return false;
1115 if (bb_with_exit_edge_p (loop, bb))
1116 exit_bb = bb;
1119 res = predicate_bbs (loop);
1120 if (!res)
1121 return false;
1123 if (flag_tree_loop_if_convert_stores)
1125 data_reference_p dr;
1127 for (i = 0; refs->iterate (i, &dr); i++)
1129 dr->aux = XNEW (struct ifc_dr);
1130 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1131 DR_RW_UNCONDITIONALLY (dr) = -1;
1135 for (i = 0; i < loop->num_nodes; i++)
1137 basic_block bb = ifc_bbs[i];
1138 gimple_stmt_iterator itr;
1140 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1141 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1142 return false;
1144 /* Check the if-convertibility of statements in predicated BBs. */
1145 if (is_predicated (bb))
1146 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1147 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1148 return false;
1151 if (dump_file)
1152 fprintf (dump_file, "Applying if-conversion\n");
1154 return true;
1157 /* Return true when LOOP is if-convertible.
1158 LOOP is if-convertible if:
1159 - it is innermost,
1160 - it has two or more basic blocks,
1161 - it has only one exit,
1162 - loop header is not the exit edge,
1163 - if its basic blocks and phi nodes are if convertible. */
1165 static bool
1166 if_convertible_loop_p (struct loop *loop)
1168 edge e;
1169 edge_iterator ei;
1170 bool res = false;
1171 vec<data_reference_p> refs;
1172 vec<ddr_p> ddrs;
1174 /* Handle only innermost loop. */
1175 if (!loop || loop->inner)
1177 if (dump_file && (dump_flags & TDF_DETAILS))
1178 fprintf (dump_file, "not innermost loop\n");
1179 return false;
1182 /* If only one block, no need for if-conversion. */
1183 if (loop->num_nodes <= 2)
1185 if (dump_file && (dump_flags & TDF_DETAILS))
1186 fprintf (dump_file, "less than 2 basic blocks\n");
1187 return false;
1190 /* More than one loop exit is too much to handle. */
1191 if (!single_exit (loop))
1193 if (dump_file && (dump_flags & TDF_DETAILS))
1194 fprintf (dump_file, "multiple exits\n");
1195 return false;
1198 /* If one of the loop header's edge is an exit edge then do not
1199 apply if-conversion. */
1200 FOR_EACH_EDGE (e, ei, loop->header->succs)
1201 if (loop_exit_edge_p (loop, e))
1202 return false;
1204 refs.create (5);
1205 ddrs.create (25);
1206 stack_vec<loop_p, 3> loop_nest;
1207 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1209 if (flag_tree_loop_if_convert_stores)
1211 data_reference_p dr;
1212 unsigned int i;
1214 for (i = 0; refs.iterate (i, &dr); i++)
1215 free (dr->aux);
1218 loop_nest.release ();
1219 free_data_refs (refs);
1220 free_dependence_relations (ddrs);
1221 return res;
1224 /* Basic block BB has two predecessors. Using predecessor's bb
1225 predicate, set an appropriate condition COND for the PHI node
1226 replacement. Return the true block whose phi arguments are
1227 selected when cond is true. LOOP is the loop containing the
1228 if-converted region, GSI is the place to insert the code for the
1229 if-conversion. */
1231 static basic_block
1232 find_phi_replacement_condition (basic_block bb, tree *cond,
1233 gimple_stmt_iterator *gsi)
1235 edge first_edge, second_edge;
1236 tree tmp_cond;
1238 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1239 first_edge = EDGE_PRED (bb, 0);
1240 second_edge = EDGE_PRED (bb, 1);
1242 /* Prefer an edge with a not negated predicate.
1243 ??? That's a very weak cost model. */
1244 tmp_cond = bb_predicate (first_edge->src);
1245 gcc_assert (tmp_cond);
1246 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1248 edge tmp_edge;
1250 tmp_edge = first_edge;
1251 first_edge = second_edge;
1252 second_edge = tmp_edge;
1255 /* Check if the edge we take the condition from is not critical.
1256 We know that at least one non-critical edge exists. */
1257 if (EDGE_COUNT (first_edge->src->succs) > 1)
1259 *cond = bb_predicate (second_edge->src);
1261 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1262 *cond = TREE_OPERAND (*cond, 0);
1263 else
1264 /* Select non loop header bb. */
1265 first_edge = second_edge;
1267 else
1268 *cond = bb_predicate (first_edge->src);
1270 /* Gimplify the condition to a valid cond-expr conditonal operand. */
1271 *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1272 is_gimple_condexpr, NULL_TREE,
1273 true, GSI_SAME_STMT);
1275 return first_edge->src;
1278 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1279 This routine does not handle PHI nodes with more than two
1280 arguments.
1282 For example,
1283 S1: A = PHI <x1(1), x2(5)>
1284 is converted into,
1285 S2: A = cond ? x1 : x2;
1287 The generated code is inserted at GSI that points to the top of
1288 basic block's statement list. When COND is true, phi arg from
1289 TRUE_BB is selected. */
1291 static void
1292 predicate_scalar_phi (gimple phi, tree cond,
1293 basic_block true_bb,
1294 gimple_stmt_iterator *gsi)
1296 gimple new_stmt;
1297 basic_block bb;
1298 tree rhs, res, arg, scev;
1300 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1301 && gimple_phi_num_args (phi) == 2);
1303 res = gimple_phi_result (phi);
1304 /* Do not handle virtual phi nodes. */
1305 if (virtual_operand_p (res))
1306 return;
1308 bb = gimple_bb (phi);
1310 if ((arg = degenerate_phi_result (phi))
1311 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1312 res))
1313 && !chrec_contains_undetermined (scev)
1314 && scev != res
1315 && (arg = gimple_phi_arg_def (phi, 0))))
1316 rhs = arg;
1317 else
1319 tree arg_0, arg_1;
1320 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1321 if (EDGE_PRED (bb, 1)->src == true_bb)
1323 arg_0 = gimple_phi_arg_def (phi, 1);
1324 arg_1 = gimple_phi_arg_def (phi, 0);
1326 else
1328 arg_0 = gimple_phi_arg_def (phi, 0);
1329 arg_1 = gimple_phi_arg_def (phi, 1);
1332 /* Build new RHS using selected condition and arguments. */
1333 rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
1334 arg_0, arg_1);
1337 new_stmt = gimple_build_assign (res, rhs);
1338 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1339 update_stmt (new_stmt);
1341 if (dump_file && (dump_flags & TDF_DETAILS))
1343 fprintf (dump_file, "new phi replacement stmt\n");
1344 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1348 /* Replaces in LOOP all the scalar phi nodes other than those in the
1349 LOOP->header block with conditional modify expressions. */
1351 static void
1352 predicate_all_scalar_phis (struct loop *loop)
1354 basic_block bb;
1355 unsigned int orig_loop_num_nodes = loop->num_nodes;
1356 unsigned int i;
1358 for (i = 1; i < orig_loop_num_nodes; i++)
1360 gimple phi;
1361 tree cond = NULL_TREE;
1362 gimple_stmt_iterator gsi, phi_gsi;
1363 basic_block true_bb = NULL;
1364 bb = ifc_bbs[i];
1366 if (bb == loop->header)
1367 continue;
1369 phi_gsi = gsi_start_phis (bb);
1370 if (gsi_end_p (phi_gsi))
1371 continue;
1373 /* BB has two predecessors. Using predecessor's aux field, set
1374 appropriate condition for the PHI node replacement. */
1375 gsi = gsi_after_labels (bb);
1376 true_bb = find_phi_replacement_condition (bb, &cond, &gsi);
1378 while (!gsi_end_p (phi_gsi))
1380 phi = gsi_stmt (phi_gsi);
1381 predicate_scalar_phi (phi, cond, true_bb, &gsi);
1382 release_phi_node (phi);
1383 gsi_next (&phi_gsi);
1386 set_phi_nodes (bb, NULL);
1390 /* Insert in each basic block of LOOP the statements produced by the
1391 gimplification of the predicates. */
1393 static void
1394 insert_gimplified_predicates (loop_p loop)
1396 unsigned int i;
1398 for (i = 0; i < loop->num_nodes; i++)
1400 basic_block bb = ifc_bbs[i];
1401 gimple_seq stmts;
1403 if (!is_predicated (bb))
1405 /* Do not insert statements for a basic block that is not
1406 predicated. Also make sure that the predicate of the
1407 basic block is set to true. */
1408 reset_bb_predicate (bb);
1409 continue;
1412 stmts = bb_predicate_gimplified_stmts (bb);
1413 if (stmts)
1415 if (flag_tree_loop_if_convert_stores)
1417 /* Insert the predicate of the BB just after the label,
1418 as the if-conversion of memory writes will use this
1419 predicate. */
1420 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1421 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1423 else
1425 /* Insert the predicate of the BB at the end of the BB
1426 as this would reduce the register pressure: the only
1427 use of this predicate will be in successor BBs. */
1428 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1430 if (gsi_end_p (gsi)
1431 || stmt_ends_bb_p (gsi_stmt (gsi)))
1432 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1433 else
1434 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1437 /* Once the sequence is code generated, set it to NULL. */
1438 set_bb_predicate_gimplified_stmts (bb, NULL);
1443 /* Predicate each write to memory in LOOP.
1445 This function transforms control flow constructs containing memory
1446 writes of the form:
1448 | for (i = 0; i < N; i++)
1449 | if (cond)
1450 | A[i] = expr;
1452 into the following form that does not contain control flow:
1454 | for (i = 0; i < N; i++)
1455 | A[i] = cond ? expr : A[i];
1457 The original CFG looks like this:
1459 | bb_0
1460 | i = 0
1461 | end_bb_0
1463 | bb_1
1464 | if (i < N) goto bb_5 else goto bb_2
1465 | end_bb_1
1467 | bb_2
1468 | cond = some_computation;
1469 | if (cond) goto bb_3 else goto bb_4
1470 | end_bb_2
1472 | bb_3
1473 | A[i] = expr;
1474 | goto bb_4
1475 | end_bb_3
1477 | bb_4
1478 | goto bb_1
1479 | end_bb_4
1481 insert_gimplified_predicates inserts the computation of the COND
1482 expression at the beginning of the destination basic block:
1484 | bb_0
1485 | i = 0
1486 | end_bb_0
1488 | bb_1
1489 | if (i < N) goto bb_5 else goto bb_2
1490 | end_bb_1
1492 | bb_2
1493 | cond = some_computation;
1494 | if (cond) goto bb_3 else goto bb_4
1495 | end_bb_2
1497 | bb_3
1498 | cond = some_computation;
1499 | A[i] = expr;
1500 | goto bb_4
1501 | end_bb_3
1503 | bb_4
1504 | goto bb_1
1505 | end_bb_4
1507 predicate_mem_writes is then predicating the memory write as follows:
1509 | bb_0
1510 | i = 0
1511 | end_bb_0
1513 | bb_1
1514 | if (i < N) goto bb_5 else goto bb_2
1515 | end_bb_1
1517 | bb_2
1518 | if (cond) goto bb_3 else goto bb_4
1519 | end_bb_2
1521 | bb_3
1522 | cond = some_computation;
1523 | A[i] = cond ? expr : A[i];
1524 | goto bb_4
1525 | end_bb_3
1527 | bb_4
1528 | goto bb_1
1529 | end_bb_4
1531 and finally combine_blocks removes the basic block boundaries making
1532 the loop vectorizable:
1534 | bb_0
1535 | i = 0
1536 | if (i < N) goto bb_5 else goto bb_1
1537 | end_bb_0
1539 | bb_1
1540 | cond = some_computation;
1541 | A[i] = cond ? expr : A[i];
1542 | if (i < N) goto bb_5 else goto bb_4
1543 | end_bb_1
1545 | bb_4
1546 | goto bb_1
1547 | end_bb_4
1550 static void
1551 predicate_mem_writes (loop_p loop)
1553 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1555 for (i = 1; i < orig_loop_num_nodes; i++)
1557 gimple_stmt_iterator gsi;
1558 basic_block bb = ifc_bbs[i];
1559 tree cond = bb_predicate (bb);
1560 bool swap;
1561 gimple stmt;
1563 if (is_true_predicate (cond))
1564 continue;
1566 swap = false;
1567 if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1569 swap = true;
1570 cond = TREE_OPERAND (cond, 0);
1573 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1574 if ((stmt = gsi_stmt (gsi))
1575 && gimple_assign_single_p (stmt)
1576 && gimple_vdef (stmt))
1578 tree lhs = gimple_assign_lhs (stmt);
1579 tree rhs = gimple_assign_rhs1 (stmt);
1580 tree type = TREE_TYPE (lhs);
1582 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1583 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1584 if (swap)
1586 tree tem = lhs;
1587 lhs = rhs;
1588 rhs = tem;
1590 cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1591 is_gimple_condexpr, NULL_TREE,
1592 true, GSI_SAME_STMT);
1593 rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
1594 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1595 update_stmt (stmt);
1600 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1601 other than the exit and latch of the LOOP. Also resets the
1602 GIMPLE_DEBUG information. */
1604 static void
1605 remove_conditions_and_labels (loop_p loop)
1607 gimple_stmt_iterator gsi;
1608 unsigned int i;
1610 for (i = 0; i < loop->num_nodes; i++)
1612 basic_block bb = ifc_bbs[i];
1614 if (bb_with_exit_edge_p (loop, bb)
1615 || bb == loop->latch)
1616 continue;
1618 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1619 switch (gimple_code (gsi_stmt (gsi)))
1621 case GIMPLE_COND:
1622 case GIMPLE_LABEL:
1623 gsi_remove (&gsi, true);
1624 break;
1626 case GIMPLE_DEBUG:
1627 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1628 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1630 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1631 update_stmt (gsi_stmt (gsi));
1633 gsi_next (&gsi);
1634 break;
1636 default:
1637 gsi_next (&gsi);
1642 /* Combine all the basic blocks from LOOP into one or two super basic
1643 blocks. Replace PHI nodes with conditional modify expressions. */
1645 static void
1646 combine_blocks (struct loop *loop)
1648 basic_block bb, exit_bb, merge_target_bb;
1649 unsigned int orig_loop_num_nodes = loop->num_nodes;
1650 unsigned int i;
1651 edge e;
1652 edge_iterator ei;
1654 remove_conditions_and_labels (loop);
1655 insert_gimplified_predicates (loop);
1656 predicate_all_scalar_phis (loop);
1658 if (flag_tree_loop_if_convert_stores)
1659 predicate_mem_writes (loop);
1661 /* Merge basic blocks: first remove all the edges in the loop,
1662 except for those from the exit block. */
1663 exit_bb = NULL;
1664 for (i = 0; i < orig_loop_num_nodes; i++)
1666 bb = ifc_bbs[i];
1667 free_bb_predicate (bb);
1668 if (bb_with_exit_edge_p (loop, bb))
1670 gcc_assert (exit_bb == NULL);
1671 exit_bb = bb;
1674 gcc_assert (exit_bb != loop->latch);
1676 for (i = 1; i < orig_loop_num_nodes; i++)
1678 bb = ifc_bbs[i];
1680 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1682 if (e->src == exit_bb)
1683 ei_next (&ei);
1684 else
1685 remove_edge (e);
1689 if (exit_bb != NULL)
1691 if (exit_bb != loop->header)
1693 /* Connect this node to loop header. */
1694 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1695 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1698 /* Redirect non-exit edges to loop->latch. */
1699 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1701 if (!loop_exit_edge_p (loop, e))
1702 redirect_edge_and_branch (e, loop->latch);
1704 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1706 else
1708 /* If the loop does not have an exit, reconnect header and latch. */
1709 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1710 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1713 merge_target_bb = loop->header;
1714 for (i = 1; i < orig_loop_num_nodes; i++)
1716 gimple_stmt_iterator gsi;
1717 gimple_stmt_iterator last;
1719 bb = ifc_bbs[i];
1721 if (bb == exit_bb || bb == loop->latch)
1722 continue;
1724 /* Make stmts member of loop->header. */
1725 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1726 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1728 /* Update stmt list. */
1729 last = gsi_last_bb (merge_target_bb);
1730 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1731 set_bb_seq (bb, NULL);
1733 delete_basic_block (bb);
1736 /* If possible, merge loop header to the block with the exit edge.
1737 This reduces the number of basic blocks to two, to please the
1738 vectorizer that handles only loops with two nodes. */
1739 if (exit_bb
1740 && exit_bb != loop->header
1741 && can_merge_blocks_p (loop->header, exit_bb))
1742 merge_blocks (loop->header, exit_bb);
1744 free (ifc_bbs);
1745 ifc_bbs = NULL;
1748 /* If-convert LOOP when it is legal. For the moment this pass has no
1749 profitability analysis. Returns true when something changed. */
1751 static bool
1752 tree_if_conversion (struct loop *loop)
1754 bool changed = false;
1755 ifc_bbs = NULL;
1757 if (!if_convertible_loop_p (loop)
1758 || !dbg_cnt (if_conversion_tree))
1759 goto cleanup;
1761 /* Now all statements are if-convertible. Combine all the basic
1762 blocks into one huge basic block doing the if-conversion
1763 on-the-fly. */
1764 combine_blocks (loop);
1766 if (flag_tree_loop_if_convert_stores)
1767 mark_virtual_operands_for_renaming (cfun);
1769 changed = true;
1771 cleanup:
1772 if (ifc_bbs)
1774 unsigned int i;
1776 for (i = 0; i < loop->num_nodes; i++)
1777 free_bb_predicate (ifc_bbs[i]);
1779 free (ifc_bbs);
1780 ifc_bbs = NULL;
1783 return changed;
1786 /* Tree if-conversion pass management. */
1788 static unsigned int
1789 main_tree_if_conversion (void)
1791 struct loop *loop;
1792 bool changed = false;
1793 unsigned todo = 0;
1795 if (number_of_loops (cfun) <= 1)
1796 return 0;
1798 FOR_EACH_LOOP (loop, 0)
1799 if (flag_tree_loop_if_convert == 1
1800 || flag_tree_loop_if_convert_stores == 1
1801 || flag_tree_loop_vectorize
1802 || loop->force_vect)
1803 changed |= tree_if_conversion (loop);
1805 if (changed)
1806 todo |= TODO_cleanup_cfg;
1808 if (changed && flag_tree_loop_if_convert_stores)
1809 todo |= TODO_update_ssa_only_virtuals;
1811 #ifdef ENABLE_CHECKING
1813 basic_block bb;
1814 FOR_EACH_BB (bb)
1815 gcc_assert (!bb->aux);
1817 #endif
1819 return todo;
1822 /* Returns true when the if-conversion pass is enabled. */
1824 static bool
1825 gate_tree_if_conversion (void)
1827 return (((flag_tree_loop_vectorize || cfun->has_force_vect_loops)
1828 && flag_tree_loop_if_convert != 0)
1829 || flag_tree_loop_if_convert == 1
1830 || flag_tree_loop_if_convert_stores == 1);
1833 namespace {
1835 const pass_data pass_data_if_conversion =
1837 GIMPLE_PASS, /* type */
1838 "ifcvt", /* name */
1839 OPTGROUP_NONE, /* optinfo_flags */
1840 true, /* has_gate */
1841 true, /* has_execute */
1842 TV_NONE, /* tv_id */
1843 ( PROP_cfg | PROP_ssa ), /* properties_required */
1844 0, /* properties_provided */
1845 0, /* properties_destroyed */
1846 0, /* todo_flags_start */
1847 ( TODO_verify_stmts | TODO_verify_flow
1848 | TODO_verify_ssa ), /* todo_flags_finish */
1851 class pass_if_conversion : public gimple_opt_pass
1853 public:
1854 pass_if_conversion (gcc::context *ctxt)
1855 : gimple_opt_pass (pass_data_if_conversion, ctxt)
1858 /* opt_pass methods: */
1859 bool gate () { return gate_tree_if_conversion (); }
1860 unsigned int execute () { return main_tree_if_conversion (); }
1862 }; // class pass_if_conversion
1864 } // anon namespace
1866 gimple_opt_pass *
1867 make_pass_if_conversion (gcc::context *ctxt)
1869 return new pass_if_conversion (ctxt);