1 /* If-conversion for vectorizer.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Devang Patel <dpatel@apple.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This pass implements a tree level if-conversion of loops. Its
23 initial goal is to help the vectorizer to vectorize loops with
26 A short description of if-conversion:
28 o Decide if a loop is if-convertible or not.
29 o Walk all loop basic blocks in breadth first order (BFS order).
30 o Remove conditional statements (at the end of basic block)
31 and propagate condition into destination basic blocks'
33 o Replace modify expression with conditional modify expression
34 using current basic block's condition.
35 o Merge all basic blocks
36 o Replace phi nodes with conditional modify expr
37 o Merge all basic blocks into header
39 Sample transformation:
44 # i_23 = PHI <0(0), i_18(10)>;
47 if (j_15 > 41) goto <L1>; else goto <L17>;
54 # iftmp.2_4 = PHI <0(8), 42(2)>;
58 if (i_18 <= 15) goto <L19>; else goto <L18>;
68 # i_23 = PHI <0(0), i_18(10)>;
73 iftmp.2_4 = j_15 > 41 ? 42 : 0;
76 if (i_18 <= 15) goto <L19>; else goto <L18>;
86 #include "coretypes.h"
91 #include "basic-block.h"
92 #include "tree-pretty-print.h"
93 #include "gimple-pretty-print.h"
94 #include "tree-flow.h"
95 #include "tree-dump.h"
97 #include "tree-chrec.h"
98 #include "tree-data-ref.h"
99 #include "tree-scalar-evolution.h"
100 #include "tree-pass.h"
103 /* List of basic blocks in if-conversion-suitable order. */
104 static basic_block *ifc_bbs;
106 /* Structure used to predicate basic blocks. This is attached to the
107 ->aux field of the BBs in the loop to be if-converted. */
108 typedef struct bb_predicate_s {
110 /* The condition under which this basic block is executed. */
113 /* PREDICATE is gimplified, and the sequence of statements is
114 recorded here, in order to avoid the duplication of computations
115 that occur in previous conditions. See PR44483. */
116 gimple_seq predicate_gimplified_stmts;
119 /* Returns true when the basic block BB has a predicate. */
122 bb_has_predicate (basic_block bb)
124 return bb->aux != NULL;
127 /* Returns the gimplified predicate for basic block BB. */
130 bb_predicate (basic_block bb)
132 return ((bb_predicate_p) bb->aux)->predicate;
135 /* Sets the gimplified predicate COND for basic block BB. */
138 set_bb_predicate (basic_block bb, tree cond)
140 ((bb_predicate_p) bb->aux)->predicate = cond;
143 /* Returns the sequence of statements of the gimplification of the
144 predicate for basic block BB. */
146 static inline gimple_seq
147 bb_predicate_gimplified_stmts (basic_block bb)
149 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
152 /* Sets the sequence of statements STMTS of the gimplification of the
153 predicate for basic block BB. */
156 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
158 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
161 /* Adds the sequence of statements STMTS to the sequence of statements
162 of the predicate for basic block BB. */
165 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
168 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
171 /* Initializes to TRUE the predicate of basic block BB. */
174 init_bb_predicate (basic_block bb)
176 bb->aux = XNEW (struct bb_predicate_s);
177 set_bb_predicate_gimplified_stmts (bb, NULL);
178 set_bb_predicate (bb, NULL_TREE);
181 /* Free the predicate of basic block BB. */
184 free_bb_predicate (basic_block bb)
188 if (!bb_has_predicate (bb))
191 /* Release the SSA_NAMEs created for the gimplification of the
193 stmts = bb_predicate_gimplified_stmts (bb);
196 gimple_stmt_iterator i;
198 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
199 free_stmt_operands (gsi_stmt (i));
206 /* Create a new temp variable of type TYPE. Add GIMPLE_ASSIGN to assign EXP
207 to the new variable. */
210 ifc_temp_var (tree type, tree exp)
212 const char *name = "_ifc_";
216 /* Create new temporary variable. */
217 var = create_tmp_var (type, name);
218 add_referenced_var (var);
220 /* Build new statement to assign EXP to new variable. */
221 stmt = gimple_build_assign (var, exp);
223 /* Get SSA name for the new variable and set make new statement
224 its definition statement. */
225 new_name = make_ssa_name (var, stmt);
226 gimple_assign_set_lhs (stmt, new_name);
227 SSA_NAME_DEF_STMT (new_name) = stmt;
233 /* Return true when COND is a true predicate. */
236 is_true_predicate (tree cond)
238 return (cond == NULL_TREE
239 || cond == boolean_true_node
240 || integer_onep (cond));
243 /* Returns true when BB has a predicate that is not trivial: true or
247 is_predicated (basic_block bb)
249 return !is_true_predicate (bb_predicate (bb));
252 /* Add condition NEW_COND to the predicate list of basic block BB. */
255 add_to_predicate_list (basic_block bb, tree new_cond)
257 tree cond = bb_predicate (bb);
259 set_bb_predicate (bb, is_true_predicate (cond) ? new_cond :
260 fold_build2_loc (EXPR_LOCATION (cond),
261 TRUTH_OR_EXPR, boolean_type_node,
265 /* Add the condition COND to the previous condition PREV_COND, and add
266 this to the predicate list of the destination of edge E. LOOP is
267 the loop to be if-converted. */
270 add_to_dst_predicate_list (struct loop *loop, edge e,
271 tree prev_cond, tree cond)
273 if (!flow_bb_inside_loop_p (loop, e->dest))
276 if (!is_true_predicate (prev_cond))
277 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
280 add_to_predicate_list (e->dest, cond);
283 /* Return true if one of the successor edges of BB exits LOOP. */
286 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
291 FOR_EACH_EDGE (e, ei, bb->succs)
292 if (loop_exit_edge_p (loop, e))
298 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
299 and it belongs to basic block BB.
301 PHI is not if-convertible if:
302 - it has more than 2 arguments,
303 - virtual PHI is immediately used in another PHI node,
304 - virtual PHI on BB other than header. */
307 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
309 if (dump_file && (dump_flags & TDF_DETAILS))
311 fprintf (dump_file, "-------------------------\n");
312 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
315 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
317 if (dump_file && (dump_flags & TDF_DETAILS))
318 fprintf (dump_file, "More than two phi node args.\n");
322 if (!is_gimple_reg (SSA_NAME_VAR (gimple_phi_result (phi))))
324 imm_use_iterator imm_iter;
327 if (bb != loop->header)
329 if (dump_file && (dump_flags & TDF_DETAILS))
330 fprintf (dump_file, "Virtual phi not on loop header.\n");
333 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
335 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
337 if (dump_file && (dump_flags & TDF_DETAILS))
338 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
347 /* Return true when STMT is if-convertible.
349 GIMPLE_ASSIGN statement is not if-convertible if,
352 - LHS is not var decl.
354 GIMPLE_ASSIGN is part of block BB, which is inside loop LOOP. */
357 if_convertible_gimple_assign_stmt_p (struct loop *loop, basic_block bb,
360 tree lhs = gimple_assign_lhs (stmt);
362 if (dump_file && (dump_flags & TDF_DETAILS))
364 fprintf (dump_file, "-------------------------\n");
365 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
368 /* Some of these constrains might be too conservative. */
369 if (stmt_ends_bb_p (stmt)
370 || gimple_has_volatile_ops (stmt)
371 || (TREE_CODE (lhs) == SSA_NAME
372 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
373 || gimple_has_side_effects (stmt))
375 if (dump_file && (dump_flags & TDF_DETAILS))
376 fprintf (dump_file, "stmt not suitable for ifcvt\n");
380 if (gimple_assign_rhs_could_trap_p (stmt))
382 if (dump_file && (dump_flags & TDF_DETAILS))
383 fprintf (dump_file, "tree could trap...\n");
387 if (TREE_CODE (lhs) != SSA_NAME
388 && bb != loop->header
389 && !bb_with_exit_edge_p (loop, bb))
391 if (dump_file && (dump_flags & TDF_DETAILS))
393 fprintf (dump_file, "LHS is not var\n");
394 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
402 /* Return true when STMT is if-convertible.
404 A statement is if-convertible if:
405 - it is an if-convertible GIMPLE_ASSGIN,
406 - it is a GIMPLE_LABEL or a GIMPLE_COND.
408 STMT is inside BB, which is inside loop LOOP. */
411 if_convertible_stmt_p (struct loop *loop, basic_block bb, gimple stmt)
413 switch (gimple_code (stmt))
421 return if_convertible_gimple_assign_stmt_p (loop, bb, stmt);
424 /* Don't know what to do with 'em so don't do anything. */
425 if (dump_file && (dump_flags & TDF_DETAILS))
427 fprintf (dump_file, "don't know what to do\n");
428 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
437 /* Return true when BB is if-convertible. This routine does not check
438 basic block's statements and phis.
440 A basic block is not if-convertible if:
441 - it is non-empty and it is after the exit block (in BFS order),
442 - it is after the exit block but before the latch,
443 - its edges are not normal.
445 EXIT_BB is the basic block containing the exit of the LOOP. BB is
449 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
454 if (dump_file && (dump_flags & TDF_DETAILS))
455 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
457 if (EDGE_COUNT (bb->preds) > 2
458 || EDGE_COUNT (bb->succs) > 2)
463 if (bb != loop->latch)
465 if (dump_file && (dump_flags & TDF_DETAILS))
466 fprintf (dump_file, "basic block after exit bb but before latch\n");
469 else if (!empty_block_p (bb))
471 if (dump_file && (dump_flags & TDF_DETAILS))
472 fprintf (dump_file, "non empty basic block after exit bb\n");
475 else if (bb == loop->latch
477 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
479 if (dump_file && (dump_flags & TDF_DETAILS))
480 fprintf (dump_file, "latch is not dominated by exit_block\n");
485 /* Be less adventurous and handle only normal edges. */
486 FOR_EACH_EDGE (e, ei, bb->succs)
488 (EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
490 if (dump_file && (dump_flags & TDF_DETAILS))
491 fprintf (dump_file, "Difficult to handle edges\n");
498 /* Return true when all predecessor blocks of BB are visited. The
499 VISITED bitmap keeps track of the visited blocks. */
502 pred_blocks_visited_p (basic_block bb, bitmap *visited)
506 FOR_EACH_EDGE (e, ei, bb->preds)
507 if (!bitmap_bit_p (*visited, e->src->index))
513 /* Get body of a LOOP in suitable order for if-conversion. It is
514 caller's responsibility to deallocate basic block list.
515 If-conversion suitable order is, breadth first sort (BFS) order
516 with an additional constraint: select a block only if all its
517 predecessors are already selected. */
520 get_loop_body_in_if_conv_order (const struct loop *loop)
522 basic_block *blocks, *blocks_in_bfs_order;
525 unsigned int index = 0;
526 unsigned int visited_count = 0;
528 gcc_assert (loop->num_nodes);
529 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
531 blocks = XCNEWVEC (basic_block, loop->num_nodes);
532 visited = BITMAP_ALLOC (NULL);
534 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
537 while (index < loop->num_nodes)
539 bb = blocks_in_bfs_order [index];
541 if (bb->flags & BB_IRREDUCIBLE_LOOP)
543 free (blocks_in_bfs_order);
544 BITMAP_FREE (visited);
549 if (!bitmap_bit_p (visited, bb->index))
551 if (pred_blocks_visited_p (bb, &visited)
552 || bb == loop->header)
554 /* This block is now visited. */
555 bitmap_set_bit (visited, bb->index);
556 blocks[visited_count++] = bb;
562 if (index == loop->num_nodes
563 && visited_count != loop->num_nodes)
567 free (blocks_in_bfs_order);
568 BITMAP_FREE (visited);
572 /* Returns true when the analysis of the predicates for all the basic
573 blocks in LOOP succeeded.
575 predicate_bbs first allocates the predicates of the basic blocks.
576 These fields are then initialized with the tree expressions
577 representing the predicates under which a basic block is executed
578 in the LOOP. As the loop->header is executed at each iteration, it
579 has the "true" predicate. Other statements executed under a
580 condition are predicated with that condition, for example
587 S1 will be predicated with "x", and
588 S2 will be predicated with "!x". */
591 predicate_bbs (loop_p loop)
595 for (i = 0; i < loop->num_nodes; i++)
596 init_bb_predicate (ifc_bbs[i]);
598 for (i = 0; i < loop->num_nodes; i++)
600 basic_block bb = ifc_bbs[i];
602 gimple_stmt_iterator itr;
604 /* The loop latch is always executed and has no extra conditions
605 to be processed: skip it. */
606 if (bb == loop->latch)
608 set_bb_predicate (loop->latch, boolean_true_node);
609 set_bb_predicate_gimplified_stmts (loop->latch, NULL);
613 cond = bb_predicate (bb);
615 && bb != loop->header)
619 cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
620 add_bb_predicate_gimplified_stmts (bb, stmts);
623 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
625 gimple stmt = gsi_stmt (itr);
627 switch (gimple_code (stmt))
638 edge true_edge, false_edge;
639 location_t loc = gimple_location (stmt);
640 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
642 gimple_cond_lhs (stmt),
643 gimple_cond_rhs (stmt));
645 /* Add new condition into destination's predicate list. */
646 extract_true_false_edges_from_block (gimple_bb (stmt),
647 &true_edge, &false_edge);
649 /* If C is true, then TRUE_EDGE is taken. */
650 add_to_dst_predicate_list (loop, true_edge, cond, c);
652 /* If C is false, then FALSE_EDGE is taken. */
653 c2 = invert_truthvalue_loc (loc, unshare_expr (c));
654 add_to_dst_predicate_list (loop, false_edge, cond, c2);
661 /* Not handled yet in if-conversion. */
666 /* If current bb has only one successor, then consider it as an
667 unconditional goto. */
668 if (single_succ_p (bb))
670 basic_block bb_n = single_succ (bb);
672 /* The successor bb inherits the predicate of its
673 predecessor. If there is no predicate in the predecessor
674 bb, then consider the successor bb as always executed. */
675 if (cond == NULL_TREE)
676 cond = boolean_true_node;
678 add_to_predicate_list (bb_n, cond);
682 /* The loop header is always executed. */
683 set_bb_predicate (loop->header, boolean_true_node);
684 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
685 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
690 /* Return true when LOOP is if-convertible.
691 LOOP is if-convertible if:
693 - it has two or more basic blocks,
694 - it has only one exit,
695 - loop header is not the exit edge,
696 - if its basic blocks and phi nodes are if convertible. */
699 if_convertible_loop_p (struct loop *loop)
704 basic_block exit_bb = NULL;
706 /* Handle only innermost loop. */
707 if (!loop || loop->inner)
709 if (dump_file && (dump_flags & TDF_DETAILS))
710 fprintf (dump_file, "not innermost loop\n");
714 /* If only one block, no need for if-conversion. */
715 if (loop->num_nodes <= 2)
717 if (dump_file && (dump_flags & TDF_DETAILS))
718 fprintf (dump_file, "less than 2 basic blocks\n");
722 /* More than one loop exit is too much to handle. */
723 if (!single_exit (loop))
725 if (dump_file && (dump_flags & TDF_DETAILS))
726 fprintf (dump_file, "multiple exits\n");
730 /* ??? Check target's vector conditional operation support for vectorizer. */
732 /* If one of the loop header's edge is exit edge then do not apply
734 FOR_EACH_EDGE (e, ei, loop->header->succs)
736 if (loop_exit_edge_p (loop, e))
740 /* Don't if-convert the loop when the data dependences cannot be
741 computed: the loop won't be vectorized in that case. */
743 VEC (data_reference_p, heap) *refs = VEC_alloc (data_reference_p, heap, 5);
744 VEC (ddr_p, heap) *ddrs = VEC_alloc (ddr_p, heap, 25);
745 bool res = compute_data_dependences_for_loop (loop, true, &refs, &ddrs);
747 free_data_refs (refs);
748 free_dependence_relations (ddrs);
754 calculate_dominance_info (CDI_DOMINATORS);
756 /* Allow statements that can be handled during if-conversion. */
757 ifc_bbs = get_loop_body_in_if_conv_order (loop);
760 if (dump_file && (dump_flags & TDF_DETAILS))
761 fprintf (dump_file, "Irreducible loop\n");
765 for (i = 0; i < loop->num_nodes; i++)
767 basic_block bb = ifc_bbs[i];
769 if (!if_convertible_bb_p (loop, bb, exit_bb))
772 if (bb_with_exit_edge_p (loop, bb))
776 if (!predicate_bbs (loop))
779 for (i = 0; i < loop->num_nodes; i++)
781 basic_block bb = ifc_bbs[i];
782 gimple_stmt_iterator itr;
784 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
785 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
788 /* For non predicated BBs, don't check their statements. */
789 if (!is_predicated (bb))
792 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
793 if (!if_convertible_stmt_p (loop, bb, gsi_stmt (itr)))
798 fprintf (dump_file, "Applying if-conversion\n");
803 /* Basic block BB has two predecessors. Using predecessor's bb
804 predicate, set an appropriate condition COND for the PHI node
805 replacement. Return the true block whose phi arguments are
806 selected when cond is true. LOOP is the loop containing the
807 if-converted region, GSI is the place to insert the code for the
811 find_phi_replacement_condition (struct loop *loop,
812 basic_block bb, tree *cond,
813 gimple_stmt_iterator *gsi)
815 edge first_edge, second_edge;
818 gcc_assert (EDGE_COUNT (bb->preds) == 2);
819 first_edge = EDGE_PRED (bb, 0);
820 second_edge = EDGE_PRED (bb, 1);
822 /* Use condition based on following criteria:
828 S2 is preferred over S1. Make 'b' first_bb and use its condition.
830 2) Do not make loop header first_bb.
833 S1: x = !(c == d)? a : b;
838 S3: x = (c == d) ? b : a;
840 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
843 4) If pred B is dominated by pred A then use pred B's condition.
846 /* Select condition that is not TRUTH_NOT_EXPR. */
847 tmp_cond = bb_predicate (first_edge->src);
848 gcc_assert (tmp_cond);
850 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
854 tmp_edge = first_edge;
855 first_edge = second_edge;
856 second_edge = tmp_edge;
859 /* Check if FIRST_BB is loop header or not and make sure that
860 FIRST_BB does not dominate SECOND_BB. */
861 if (first_edge->src == loop->header
862 || dominated_by_p (CDI_DOMINATORS,
863 second_edge->src, first_edge->src))
865 *cond = bb_predicate (second_edge->src);
867 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
868 *cond = invert_truthvalue (*cond);
870 /* Select non loop header bb. */
871 first_edge = second_edge;
874 *cond = bb_predicate (first_edge->src);
876 /* Gimplify the condition: the vectorizer prefers to have gimple
877 values as conditions. Various targets use different means to
878 communicate conditions in vector compare operations. Using a
879 gimple value allows the compiler to emit vector compare and
880 select RTL without exposing compare's result. */
881 *cond = force_gimple_operand_gsi (gsi, unshare_expr (*cond),
883 true, GSI_SAME_STMT);
884 if (!is_gimple_reg (*cond) && !is_gimple_condexpr (*cond))
888 new_stmt = ifc_temp_var (TREE_TYPE (*cond), unshare_expr (*cond));
889 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
890 *cond = gimple_assign_lhs (new_stmt);
895 return first_edge->src;
898 /* Replace PHI node with conditional modify expr using COND. This
899 routine does not handle PHI nodes with more than two arguments.
902 S1: A = PHI <x1(1), x2(5)
904 S2: A = cond ? x1 : x2;
906 The generated code is inserted at GSI that points to the top of
907 basic block's statement list. When COND is true, phi arg from
908 TRUE_BB is selected. */
911 replace_phi_with_cond_gimple_assign_stmt (gimple phi, tree cond,
913 gimple_stmt_iterator *gsi)
920 gcc_assert (gimple_code (phi) == GIMPLE_PHI
921 && gimple_phi_num_args (phi) == 2);
923 bb = gimple_bb (phi);
925 arg = degenerate_phi_result (phi);
931 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
932 if (EDGE_PRED (bb, 1)->src == true_bb)
934 arg_0 = gimple_phi_arg_def (phi, 1);
935 arg_1 = gimple_phi_arg_def (phi, 0);
939 arg_0 = gimple_phi_arg_def (phi, 0);
940 arg_1 = gimple_phi_arg_def (phi, 1);
943 /* Build new RHS using selected condition and arguments. */
944 rhs = build3 (COND_EXPR, TREE_TYPE (PHI_RESULT (phi)),
945 unshare_expr (cond), arg_0, arg_1);
948 new_stmt = gimple_build_assign (PHI_RESULT (phi), rhs);
949 SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
950 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
951 update_stmt (new_stmt);
953 if (dump_file && (dump_flags & TDF_DETAILS))
955 fprintf (dump_file, "new phi replacement stmt\n");
956 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
960 /* Replaces in LOOP all the phi nodes other than those in the
961 LOOP->header block with conditional modify expressions. */
964 ifconvert_phi_nodes (struct loop *loop)
967 unsigned int orig_loop_num_nodes = loop->num_nodes;
970 for (i = 1; i < orig_loop_num_nodes; i++)
973 tree cond = NULL_TREE;
974 gimple_stmt_iterator gsi, phi_gsi;
975 basic_block true_bb = NULL;
978 if (bb == loop->header)
981 phi_gsi = gsi_start_phis (bb);
982 if (gsi_end_p (phi_gsi))
985 /* BB has two predecessors. Using predecessor's aux field, set
986 appropriate condition for the PHI node replacement. */
987 gsi = gsi_after_labels (bb);
988 true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
990 while (!gsi_end_p (phi_gsi))
992 phi = gsi_stmt (phi_gsi);
993 replace_phi_with_cond_gimple_assign_stmt (phi, cond, true_bb, &gsi);
994 release_phi_node (phi);
998 set_phi_nodes (bb, NULL);
1002 /* Insert in each basic block of LOOP the statements produced by the
1003 gimplification of the predicates. */
1006 insert_gimplified_predicates (loop_p loop)
1010 for (i = 0; i < loop->num_nodes; i++)
1012 basic_block bb = ifc_bbs[i];
1013 gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
1017 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1020 || gimple_code (gsi_stmt (gsi)) == GIMPLE_COND)
1021 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1023 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1025 /* Once the sequence is code generated, set it to NULL. */
1026 set_bb_predicate_gimplified_stmts (bb, NULL);
1031 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1032 other than the exit and latch of the LOOP. Also resets the
1033 GIMPLE_DEBUG information. */
1036 remove_conditions_and_labels (loop_p loop)
1038 gimple_stmt_iterator gsi;
1041 for (i = 0; i < loop->num_nodes; i++)
1043 basic_block bb = ifc_bbs[i];
1045 if (bb_with_exit_edge_p (loop, bb)
1046 || bb == loop->latch)
1049 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1050 switch (gimple_code (gsi_stmt (gsi)))
1054 gsi_remove (&gsi, true);
1058 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1059 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1061 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1062 update_stmt (gsi_stmt (gsi));
1073 /* Combine all the basic blocks from LOOP into one or two super basic
1074 blocks. Replace PHI nodes with conditional modify expressions. */
1077 combine_blocks (struct loop *loop)
1079 basic_block bb, exit_bb, merge_target_bb;
1080 unsigned int orig_loop_num_nodes = loop->num_nodes;
1085 remove_conditions_and_labels (loop);
1086 insert_gimplified_predicates (loop);
1087 ifconvert_phi_nodes (loop);
1089 /* Merge basic blocks: first remove all the edges in the loop,
1090 except for those from the exit block. */
1092 for (i = 0; i < orig_loop_num_nodes; i++)
1095 if (bb_with_exit_edge_p (loop, bb))
1101 gcc_assert (exit_bb != loop->latch);
1103 for (i = 1; i < orig_loop_num_nodes; i++)
1107 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1109 if (e->src == exit_bb)
1116 if (exit_bb != NULL)
1118 if (exit_bb != loop->header)
1120 /* Connect this node to loop header. */
1121 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1122 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1125 /* Redirect non-exit edges to loop->latch. */
1126 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1128 if (!loop_exit_edge_p (loop, e))
1129 redirect_edge_and_branch (e, loop->latch);
1131 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1135 /* If the loop does not have an exit, reconnect header and latch. */
1136 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1137 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1140 merge_target_bb = loop->header;
1141 for (i = 1; i < orig_loop_num_nodes; i++)
1143 gimple_stmt_iterator gsi;
1144 gimple_stmt_iterator last;
1148 if (bb == exit_bb || bb == loop->latch)
1151 /* Make stmts member of loop->header. */
1152 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1153 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1155 /* Update stmt list. */
1156 last = gsi_last_bb (merge_target_bb);
1157 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1158 set_bb_seq (bb, NULL);
1160 delete_basic_block (bb);
1163 /* If possible, merge loop header to the block with the exit edge.
1164 This reduces the number of basic blocks to two, to please the
1165 vectorizer that handles only loops with two nodes. */
1167 && exit_bb != loop->header
1168 && can_merge_blocks_p (loop->header, exit_bb))
1169 merge_blocks (loop->header, exit_bb);
1172 /* If-convert LOOP when it is legal. For the moment this pass has no
1173 profitability analysis. Returns true when something changed. */
1176 tree_if_conversion (struct loop *loop)
1178 bool changed = false;
1181 if (!if_convertible_loop_p (loop)
1182 || !dbg_cnt (if_conversion_tree))
1185 /* Now all statements are if-convertible. Combine all the basic
1186 blocks into one huge basic block doing the if-conversion
1188 combine_blocks (loop);
1196 for (i = 0; i < loop->num_nodes; i++)
1197 free_bb_predicate (ifc_bbs[i]);
1206 /* Tree if-conversion pass management. */
1209 main_tree_if_conversion (void)
1213 bool changed = false;
1215 if (number_of_loops () <= 1)
1218 FOR_EACH_LOOP (li, loop, 0)
1219 changed |= tree_if_conversion (loop);
1221 return changed ? TODO_cleanup_cfg : 0;
1225 gate_tree_if_conversion (void)
1227 return flag_tree_vectorize != 0;
1230 struct gimple_opt_pass pass_if_conversion =
1235 gate_tree_if_conversion, /* gate */
1236 main_tree_if_conversion, /* execute */
1239 0, /* static_pass_number */
1240 TV_NONE, /* tv_id */
1241 PROP_cfg | PROP_ssa, /* properties_required */
1242 0, /* properties_provided */
1243 0, /* properties_destroyed */
1244 0, /* todo_flags_start */
1245 TODO_dump_func | TODO_verify_stmts | TODO_verify_flow
1246 /* todo_flags_finish */