1 /* If-conversion for vectorizer.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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 tree level if-conversion transformation of loops.
23 Initial goal is to help vectorizer vectorize loops with 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'
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:
43 # i_23 = PHI <0(0), i_18(10)>;
46 if (j_15 > 41) goto <L1>; else goto <L17>;
53 # iftmp.2_4 = PHI <0(8), 42(2)>;
57 if (i_18 <= 15) goto <L19>; else goto <L18>;
67 # i_23 = PHI <0(0), i_18(10)>;
72 iftmp.2_4 = j_15 > 41 ? 42 : 0;
75 if (i_18 <= 15) goto <L19>; else goto <L18>;
85 #include "coretypes.h"
92 #include "basic-block.h"
93 #include "diagnostic.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"
104 /* local function prototypes */
105 static unsigned int main_tree_if_conversion (void);
106 static tree
tree_if_convert_stmt (struct loop
*loop
, gimple
, tree
,
107 gimple_stmt_iterator
*);
108 static void tree_if_convert_cond_stmt (struct loop
*, gimple
, tree
,
109 gimple_stmt_iterator
*);
110 static bool if_convertible_phi_p (struct loop
*, basic_block
, gimple
);
111 static bool if_convertible_gimple_assign_stmt_p (struct loop
*, basic_block
,
113 static bool if_convertible_stmt_p (struct loop
*, basic_block
, gimple
);
114 static bool if_convertible_bb_p (struct loop
*, basic_block
, basic_block
);
115 static bool if_convertible_loop_p (struct loop
*, bool);
116 static void add_to_predicate_list (basic_block
, tree
);
117 static tree
add_to_dst_predicate_list (struct loop
* loop
, edge
,
119 gimple_stmt_iterator
*);
120 static void clean_predicate_lists (struct loop
*loop
);
121 static basic_block
find_phi_replacement_condition (struct loop
*loop
,
123 gimple_stmt_iterator
*);
124 static void replace_phi_with_cond_gimple_assign_stmt (gimple
, tree
,
126 gimple_stmt_iterator
*);
127 static void process_phi_nodes (struct loop
*);
128 static void combine_blocks (struct loop
*);
129 static gimple
ifc_temp_var (tree
, tree
);
130 static bool pred_blocks_visited_p (basic_block
, bitmap
*);
131 static basic_block
* get_loop_body_in_if_conv_order (const struct loop
*loop
);
132 static bool bb_with_exit_edge_p (struct loop
*, basic_block
);
134 /* List of basic blocks in if-conversion-suitable order. */
135 static basic_block
*ifc_bbs
;
138 Apply if-conversion to the LOOP. Return true if successful otherwise return
139 false. If false is returned then loop remains unchanged.
140 FOR_VECTORIZER is a boolean flag. It indicates whether if-conversion is used
141 for vectorizer or not. If it is used for vectorizer, additional checks are
142 used. (Vectorization checks are not yet implemented). */
145 tree_if_conversion (struct loop
*loop
, bool for_vectorizer
)
148 gimple_stmt_iterator itr
;
153 /* if-conversion is not appropriate for all loops. First, check if loop is
154 if-convertible or not. */
155 if (!if_convertible_loop_p (loop
, for_vectorizer
))
157 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
158 fprintf (dump_file
,"-------------------------\n");
164 free_dominance_info (CDI_POST_DOMINATORS
);
168 /* Do actual work now. */
169 for (i
= 0; i
< loop
->num_nodes
; i
++)
175 /* Update condition using predicate list. */
176 cond
= (tree
) bb
->aux
;
178 /* Process all statements in this basic block.
179 Remove conditional expression, if any, and annotate
180 destination basic block(s) appropriately. */
181 for (itr
= gsi_start_bb (bb
); !gsi_end_p (itr
); /* empty */)
183 gimple t
= gsi_stmt (itr
);
184 cond
= tree_if_convert_stmt (loop
, t
, cond
, &itr
);
185 if (!gsi_end_p (itr
))
189 /* If current bb has only one successor, then consider it as an
190 unconditional goto. */
191 if (single_succ_p (bb
))
193 basic_block bb_n
= single_succ (bb
);
195 /* Successor bb inherits predicate of its predecessor. If there
196 is no predicate in predecessor bb, then consider successor bb
197 as always executed. */
198 if (cond
== NULL_TREE
)
199 cond
= boolean_true_node
;
201 add_to_predicate_list (bb_n
, cond
);
205 /* Now, all statements are if-converted and basic blocks are
206 annotated appropriately. Combine all basic block into one huge
208 combine_blocks (loop
);
211 clean_predicate_lists (loop
);
218 /* if-convert stmt T which is part of LOOP.
219 If T is a GIMPLE_ASSIGN then it is converted into conditional modify
220 expression using COND. For conditional expressions, add condition in the
221 destination basic block's predicate list and remove conditional
222 expression itself. BSI is the iterator used to traverse statements of
223 loop. It is used here when it is required to delete current statement. */
226 tree_if_convert_stmt (struct loop
* loop
, gimple t
, tree cond
,
227 gimple_stmt_iterator
*gsi
)
229 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
231 fprintf (dump_file
, "------if-convert stmt\n");
232 print_gimple_stmt (dump_file
, t
, 0, TDF_SLIM
);
233 print_generic_stmt (dump_file
, cond
, TDF_SLIM
);
236 switch (gimple_code (t
))
238 /* Labels are harmless here. */
243 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
244 if (gimple_debug_bind_p (gsi_stmt (*gsi
)))
246 gimple_debug_bind_reset_value (gsi_stmt (*gsi
));
247 update_stmt (gsi_stmt (*gsi
));
252 /* This GIMPLE_ASSIGN is killing previous value of LHS. Appropriate
253 value will be selected by PHI node based on condition. It is possible
254 that before this transformation, PHI nodes was selecting default
255 value and now it will use this new value. This is OK because it does
256 not change validity the program. */
260 /* Update destination blocks' predicate list and remove this
261 condition expression. */
262 tree_if_convert_cond_stmt (loop
, t
, cond
, gsi
);
272 /* STMT is a GIMPLE_COND. Update two destination's predicate list.
273 Remove COND_EXPR, if it is not the loop exit condition. Otherwise
274 update loop exit condition appropriately. GSI is the iterator
275 used to traverse statement list. STMT is part of loop LOOP. */
278 tree_if_convert_cond_stmt (struct loop
*loop
, gimple stmt
, tree cond
,
279 gimple_stmt_iterator
*gsi
)
282 edge true_edge
, false_edge
;
283 location_t loc
= gimple_location (stmt
);
285 gcc_assert (gimple_code (stmt
) == GIMPLE_COND
);
287 c
= fold_build2_loc (loc
, gimple_cond_code (stmt
), boolean_type_node
,
288 gimple_cond_lhs (stmt
), gimple_cond_rhs (stmt
));
290 extract_true_false_edges_from_block (gimple_bb (stmt
),
291 &true_edge
, &false_edge
);
293 /* Add new condition into destination's predicate list. */
295 /* If C is true then TRUE_EDGE is taken. */
296 add_to_dst_predicate_list (loop
, true_edge
, cond
, c
, gsi
);
298 /* If 'c' is false then FALSE_EDGE is taken. */
299 c2
= invert_truthvalue_loc (loc
, unshare_expr (c
));
300 add_to_dst_predicate_list (loop
, false_edge
, cond
, c2
, gsi
);
302 /* Now this conditional statement is redundant. Remove it.
303 But, do not remove exit condition! Update exit condition
304 using new condition. */
305 if (!bb_with_exit_edge_p (loop
, gimple_bb (stmt
)))
307 gsi_remove (gsi
, true);
313 /* Return true, iff PHI is if-convertible. PHI is part of loop LOOP
314 and it belongs to basic block BB.
315 PHI is not if-convertible
316 - if it has more than 2 arguments.
317 - Virtual PHI is immediately used in another PHI node.
318 - Virtual PHI on BB other than header. */
321 if_convertible_phi_p (struct loop
*loop
, basic_block bb
, gimple phi
)
323 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
325 fprintf (dump_file
, "-------------------------\n");
326 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
329 if (bb
!= loop
->header
&& gimple_phi_num_args (phi
) != 2)
331 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
332 fprintf (dump_file
, "More than two phi node args.\n");
336 if (!is_gimple_reg (SSA_NAME_VAR (gimple_phi_result (phi
))))
338 imm_use_iterator imm_iter
;
341 if (bb
!= loop
->header
)
343 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
344 fprintf (dump_file
, "Virtual phi not on loop header.\n");
347 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, gimple_phi_result (phi
))
349 if (gimple_code (USE_STMT (use_p
)) == GIMPLE_PHI
)
351 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
352 fprintf (dump_file
, "Difficult to handle this virtual phi.\n");
361 /* Return true, if STMT is if-convertible.
362 GIMPLE_ASSIGN statement is not if-convertible if,
365 - LHS is not var decl.
366 GIMPLE_ASSIGN is part of block BB, which is inside loop LOOP. */
369 if_convertible_gimple_assign_stmt_p (struct loop
*loop
, basic_block bb
,
374 if (!is_gimple_assign (stmt
))
377 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
379 fprintf (dump_file
, "-------------------------\n");
380 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
383 lhs
= gimple_assign_lhs (stmt
);
385 /* Some of these constrains might be too conservative. */
386 if (stmt_ends_bb_p (stmt
)
387 || gimple_has_volatile_ops (stmt
)
388 || (TREE_CODE (lhs
) == SSA_NAME
389 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
390 || gimple_has_side_effects (stmt
))
392 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
393 fprintf (dump_file
, "stmt not suitable for ifcvt\n");
397 /* See if it needs speculative loading or not. */
398 if (bb
!= loop
->header
399 && gimple_assign_rhs_could_trap_p (stmt
))
401 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
402 fprintf (dump_file
, "tree could trap...\n");
406 if (TREE_CODE (lhs
) != SSA_NAME
407 && bb
!= loop
->header
408 && !bb_with_exit_edge_p (loop
, bb
))
410 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
412 fprintf (dump_file
, "LHS is not var\n");
413 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
421 /* Return true, iff STMT is if-convertible.
422 Statement is if-convertible if,
423 - It is if-convertible GIMPLE_ASSGIN
424 - It is GIMPLE_LABEL or GIMPLE_COND.
425 STMT is inside block BB, which is inside loop LOOP. */
428 if_convertible_stmt_p (struct loop
*loop
, basic_block bb
, gimple stmt
)
430 switch (gimple_code (stmt
))
439 if (!if_convertible_gimple_assign_stmt_p (loop
, bb
, stmt
))
447 /* Don't know what to do with 'em so don't do anything. */
448 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
450 fprintf (dump_file
, "don't know what to do\n");
451 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
460 /* Return true, iff BB is if-convertible.
461 Note: This routine does _not_ check basic block statements and phis.
462 Basic block is not if-convertible if,
463 - Basic block is non-empty and it is after exit block (in BFS order).
464 - Basic block is after exit block but before latch.
465 - Basic block edge(s) is not normal.
466 EXIT_BB_SEEN is true if basic block with exit edge is already seen.
467 BB is inside loop LOOP. */
470 if_convertible_bb_p (struct loop
*loop
, basic_block bb
, basic_block exit_bb
)
475 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
476 fprintf (dump_file
, "----------[%d]-------------\n", bb
->index
);
480 if (bb
!= loop
->latch
)
482 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
483 fprintf (dump_file
, "basic block after exit bb but before latch\n");
486 else if (!empty_block_p (bb
))
488 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
489 fprintf (dump_file
, "non empty basic block after exit bb\n");
492 else if (bb
== loop
->latch
494 && !dominated_by_p (CDI_DOMINATORS
, bb
, exit_bb
))
496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
497 fprintf (dump_file
, "latch is not dominated by exit_block\n");
502 /* Be less adventurous and handle only normal edges. */
503 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
505 (EDGE_ABNORMAL_CALL
| EDGE_EH
| EDGE_ABNORMAL
| EDGE_IRREDUCIBLE_LOOP
))
507 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
508 fprintf (dump_file
,"Difficult to handle edges\n");
515 /* Return true, iff LOOP is if-convertible.
516 LOOP is if-convertible if,
518 - It has two or more basic blocks.
519 - It has only one exit.
520 - Loop header is not the exit edge.
521 - If its basic blocks and phi nodes are if convertible. See above for
523 FOR_VECTORIZER enables vectorizer specific checks. For example, support
524 for vector conditions, data dependency checks etc.. (Not implemented yet). */
527 if_convertible_loop_p (struct loop
*loop
, bool for_vectorizer ATTRIBUTE_UNUSED
)
530 gimple_stmt_iterator itr
;
534 basic_block exit_bb
= NULL
;
536 /* Handle only inner most loop. */
537 if (!loop
|| loop
->inner
)
539 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
540 fprintf (dump_file
, "not inner most loop\n");
544 /* If only one block, no need for if-conversion. */
545 if (loop
->num_nodes
<= 2)
547 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
548 fprintf (dump_file
, "less than 2 basic blocks\n");
552 /* More than one loop exit is too much to handle. */
553 if (!single_exit (loop
))
555 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
556 fprintf (dump_file
, "multiple exits\n");
560 /* ??? Check target's vector conditional operation support for vectorizer. */
562 /* If one of the loop header's edge is exit edge then do not apply
564 FOR_EACH_EDGE (e
, ei
, loop
->header
->succs
)
566 if (loop_exit_edge_p (loop
, e
))
570 calculate_dominance_info (CDI_DOMINATORS
);
571 calculate_dominance_info (CDI_POST_DOMINATORS
);
573 /* Allow statements that can be handled during if-conversion. */
574 ifc_bbs
= get_loop_body_in_if_conv_order (loop
);
577 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
578 fprintf (dump_file
,"Irreducible loop\n");
579 free_dominance_info (CDI_POST_DOMINATORS
);
583 for (i
= 0; i
< loop
->num_nodes
; i
++)
587 if (!if_convertible_bb_p (loop
, bb
, exit_bb
))
590 /* Check statements. */
591 for (itr
= gsi_start_bb (bb
); !gsi_end_p (itr
); gsi_next (&itr
))
592 if (!if_convertible_stmt_p (loop
, bb
, gsi_stmt (itr
)))
594 /* ??? Check data dependency for vectorizer. */
596 /* What about phi nodes ? */
597 itr
= gsi_start_phis (bb
);
599 /* Clear aux field of incoming edges to a bb with a phi node. */
600 if (!gsi_end_p (itr
))
601 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
604 /* Check statements. */
605 for (; !gsi_end_p (itr
); gsi_next (&itr
))
606 if (!if_convertible_phi_p (loop
, bb
, gsi_stmt (itr
)))
609 if (bb_with_exit_edge_p (loop
, bb
))
613 /* OK. Did not find any potential issues so go ahead in if-convert
614 this loop. Now there is no looking back. */
616 fprintf (dump_file
,"Applying if-conversion\n");
618 free_dominance_info (CDI_POST_DOMINATORS
);
622 /* Add condition COND into predicate list of basic block BB. */
625 add_to_predicate_list (basic_block bb
, tree new_cond
)
627 tree cond
= (tree
) bb
->aux
;
630 cond
= fold_build2_loc (EXPR_LOCATION (cond
),
631 TRUTH_OR_EXPR
, boolean_type_node
,
632 unshare_expr (cond
), new_cond
);
639 /* Add condition COND into BB's predicate list. PREV_COND is
640 existing condition. */
643 add_to_dst_predicate_list (struct loop
* loop
, edge e
,
644 tree prev_cond
, tree cond
,
645 gimple_stmt_iterator
*gsi
)
647 tree new_cond
= NULL_TREE
;
649 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
652 if (prev_cond
== boolean_true_node
|| !prev_cond
)
653 new_cond
= unshare_expr (cond
);
657 gimple tmp_stmt
= NULL
;
659 prev_cond
= force_gimple_operand_gsi (gsi
, unshare_expr (prev_cond
),
660 true, NULL
, true, GSI_SAME_STMT
);
662 cond
= force_gimple_operand_gsi (gsi
, unshare_expr (cond
),
663 true, NULL
, true, GSI_SAME_STMT
);
665 /* Add the condition to aux field of the edge. In case edge
666 destination is a PHI node, this condition will be ANDed with
667 block predicate to construct complete condition. */
670 /* new_cond == prev_cond AND cond */
671 tmp
= build2 (TRUTH_AND_EXPR
, boolean_type_node
,
672 unshare_expr (prev_cond
), cond
);
673 tmp_stmt
= ifc_temp_var (boolean_type_node
, tmp
);
674 gsi_insert_before (gsi
, tmp_stmt
, GSI_SAME_STMT
);
675 new_cond
= gimple_assign_lhs (tmp_stmt
);
677 add_to_predicate_list (e
->dest
, new_cond
);
681 /* During if-conversion aux field from basic block structure is used to hold
682 predicate list. Clean each basic block's predicate list for the given LOOP.
683 Also clean aux field of successor edges, used to hold true and false
684 condition from conditional expression. */
687 clean_predicate_lists (struct loop
*loop
)
694 bb
= get_loop_body (loop
);
695 for (i
= 0; i
< loop
->num_nodes
; i
++)
698 FOR_EACH_EDGE (e
, ei
, bb
[i
]->succs
)
704 /* Basic block BB has two predecessors. Using predecessor's aux field, set
705 appropriate condition COND for the PHI node replacement. Return true block
706 whose phi arguments are selected when cond is true. */
709 find_phi_replacement_condition (struct loop
*loop
,
710 basic_block bb
, tree
*cond
,
711 gimple_stmt_iterator
*gsi
)
713 edge first_edge
, second_edge
;
716 gcc_assert (EDGE_COUNT (bb
->preds
) == 2);
717 first_edge
= EDGE_PRED (bb
, 0);
718 second_edge
= EDGE_PRED (bb
, 1);
720 /* Use condition based on following criteria:
726 S2 is preferred over S1. Make 'b' first_bb and use its condition.
728 2) Do not make loop header first_bb.
731 S1: x = !(c == d)? a : b;
736 S3: x = (c == d) ? b : a;
738 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
741 4) If pred B is dominated by pred A then use pred B's condition.
744 /* Select condition that is not TRUTH_NOT_EXPR. */
745 tmp_cond
= (tree
) (first_edge
->src
)->aux
;
746 gcc_assert (tmp_cond
);
748 if (TREE_CODE (tmp_cond
) == TRUTH_NOT_EXPR
)
752 tmp_edge
= first_edge
;
753 first_edge
= second_edge
;
754 second_edge
= tmp_edge
;
757 /* Check if FIRST_BB is loop header or not and make sure that
758 FIRST_BB does not dominate SECOND_BB. */
759 if (first_edge
->src
== loop
->header
760 || dominated_by_p (CDI_DOMINATORS
,
761 second_edge
->src
, first_edge
->src
))
763 *cond
= (tree
) (second_edge
->src
)->aux
;
765 /* If there is a condition on an incoming edge,
766 AND it with the incoming bb predicate. */
767 if (second_edge
->aux
)
768 *cond
= build2 (TRUTH_AND_EXPR
, boolean_type_node
,
769 *cond
, (tree
) second_edge
->aux
);
771 if (TREE_CODE (*cond
) == TRUTH_NOT_EXPR
)
772 /* We can be smart here and choose inverted
773 condition without switching bbs. */
774 *cond
= invert_truthvalue (*cond
);
776 /* Select non loop header bb. */
777 first_edge
= second_edge
;
781 /* FIRST_BB is not loop header */
782 *cond
= (tree
) (first_edge
->src
)->aux
;
784 /* If there is a condition on an incoming edge,
785 AND it with the incoming bb predicate. */
787 *cond
= build2 (TRUTH_AND_EXPR
, boolean_type_node
,
788 *cond
, (tree
) first_edge
->aux
);
791 /* Create temp. for the condition. Vectorizer prefers to have gimple
792 value as condition. Various targets use different means to communicate
793 condition in vector compare operation. Using gimple value allows
794 compiler to emit vector compare and select RTL without exposing
796 *cond
= force_gimple_operand_gsi (gsi
, unshare_expr (*cond
),
798 true, GSI_SAME_STMT
);
799 if (!is_gimple_reg (*cond
) && !is_gimple_condexpr (*cond
))
803 new_stmt
= ifc_temp_var (TREE_TYPE (*cond
), unshare_expr (*cond
));
804 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
805 *cond
= gimple_assign_lhs (new_stmt
);
810 return first_edge
->src
;
814 /* Replace PHI node with conditional modify expr using COND.
815 This routine does not handle PHI nodes with more than two arguments.
817 S1: A = PHI <x1(1), x2(5)
819 S2: A = cond ? x1 : x2;
820 S2 is inserted at the top of basic block's statement list.
821 When COND is true, phi arg from TRUE_BB is selected.
825 replace_phi_with_cond_gimple_assign_stmt (gimple phi
, tree cond
,
827 gimple_stmt_iterator
*gsi
)
834 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
);
836 /* If this is not filtered earlier, then now it is too late. */
837 gcc_assert (gimple_phi_num_args (phi
) == 2);
839 /* Find basic block and initialize iterator. */
840 bb
= gimple_bb (phi
);
842 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
843 if (EDGE_PRED (bb
, 1)->src
== true_bb
)
845 arg_0
= gimple_phi_arg_def (phi
, 1);
846 arg_1
= gimple_phi_arg_def (phi
, 0);
850 arg_0
= gimple_phi_arg_def (phi
, 0);
851 arg_1
= gimple_phi_arg_def (phi
, 1);
854 /* Build new RHS using selected condition and arguments. */
855 rhs
= build3 (COND_EXPR
, TREE_TYPE (PHI_RESULT (phi
)),
856 unshare_expr (cond
), unshare_expr (arg_0
),
857 unshare_expr (arg_1
));
859 /* Create new GIMPLE_ASSIGN statement using RHS. */
860 new_stmt
= gimple_build_assign (unshare_expr (PHI_RESULT (phi
)), rhs
);
862 /* Make new statement definition of the original phi result. */
863 SSA_NAME_DEF_STMT (gimple_phi_result (phi
)) = new_stmt
;
865 /* Insert using iterator. */
866 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
867 update_stmt (new_stmt
);
869 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
871 fprintf (dump_file
, "new phi replacement stmt\n");
872 print_gimple_stmt (dump_file
, new_stmt
, 0, TDF_SLIM
);
876 /* Process phi nodes for the given LOOP. Replace phi nodes with cond
880 process_phi_nodes (struct loop
*loop
)
883 unsigned int orig_loop_num_nodes
= loop
->num_nodes
;
886 /* Replace phi nodes with cond. modify expr. */
887 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
890 tree cond
= NULL_TREE
;
891 gimple_stmt_iterator gsi
, phi_gsi
;
892 basic_block true_bb
= NULL
;
895 if (bb
== loop
->header
)
898 phi_gsi
= gsi_start_phis (bb
);
899 gsi
= gsi_after_labels (bb
);
901 /* BB has two predecessors. Using predecessor's aux field, set
902 appropriate condition for the PHI node replacement. */
903 if (!gsi_end_p (phi_gsi
))
904 true_bb
= find_phi_replacement_condition (loop
, bb
, &cond
, &gsi
);
906 while (!gsi_end_p (phi_gsi
))
908 phi
= gsi_stmt (phi_gsi
);
909 replace_phi_with_cond_gimple_assign_stmt (phi
, cond
, true_bb
, &gsi
);
910 release_phi_node (phi
);
913 set_phi_nodes (bb
, NULL
);
918 /* Combine all basic block from the given LOOP into one or two super
919 basic block. Replace PHI nodes with conditional modify expression. */
922 combine_blocks (struct loop
*loop
)
924 basic_block bb
, exit_bb
, merge_target_bb
;
925 unsigned int orig_loop_num_nodes
= loop
->num_nodes
;
930 /* Process phi nodes to prepare blocks for merge. */
931 process_phi_nodes (loop
);
933 /* Merge basic blocks. First remove all the edges in the loop, except
934 for those from the exit block. */
936 for (i
= 0; i
< orig_loop_num_nodes
; i
++)
939 if (bb_with_exit_edge_p (loop
, bb
))
945 gcc_assert (exit_bb
!= loop
->latch
);
947 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
951 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
));)
953 if (e
->src
== exit_bb
)
962 if (exit_bb
!= loop
->header
)
964 /* Connect this node with loop header. */
965 make_edge (loop
->header
, exit_bb
, EDGE_FALLTHRU
);
966 set_immediate_dominator (CDI_DOMINATORS
, exit_bb
, loop
->header
);
969 /* Redirect non-exit edges to loop->latch. */
970 FOR_EACH_EDGE (e
, ei
, exit_bb
->succs
)
972 if (!loop_exit_edge_p (loop
, e
))
973 redirect_edge_and_branch (e
, loop
->latch
);
975 set_immediate_dominator (CDI_DOMINATORS
, loop
->latch
, exit_bb
);
979 /* If the loop does not have exit then reconnect header and latch. */
980 make_edge (loop
->header
, loop
->latch
, EDGE_FALLTHRU
);
981 set_immediate_dominator (CDI_DOMINATORS
, loop
->latch
, loop
->header
);
984 merge_target_bb
= loop
->header
;
985 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
987 gimple_stmt_iterator gsi
;
988 gimple_stmt_iterator last
;
992 if (bb
== exit_bb
|| bb
== loop
->latch
)
995 /* Remove labels and make stmts member of loop->header. */
996 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
998 if (gimple_code (gsi_stmt (gsi
)) == GIMPLE_LABEL
)
999 gsi_remove (&gsi
, true);
1002 gimple_set_bb (gsi_stmt (gsi
), merge_target_bb
);
1007 /* Update stmt list. */
1008 last
= gsi_last_bb (merge_target_bb
);
1009 gsi_insert_seq_after (&last
, bb_seq (bb
), GSI_NEW_STMT
);
1010 set_bb_seq (bb
, NULL
);
1012 delete_basic_block (bb
);
1015 /* Now if possible, merge loop header and block with exit edge.
1016 This reduces number of basic blocks to 2. Auto vectorizer addresses
1017 loops with two nodes only. FIXME: Use cleanup_tree_cfg(). */
1019 && exit_bb
!= loop
->header
1020 && can_merge_blocks_p (loop
->header
, exit_bb
))
1021 merge_blocks (loop
->header
, exit_bb
);
1024 /* Make a new temp variable of type TYPE. Add GIMPLE_ASSIGN to assign EXP
1025 to the new variable. */
1028 ifc_temp_var (tree type
, tree exp
)
1030 const char *name
= "_ifc_";
1034 /* Create new temporary variable. */
1035 var
= create_tmp_var (type
, name
);
1036 add_referenced_var (var
);
1038 /* Build new statement to assign EXP to new variable. */
1039 stmt
= gimple_build_assign (var
, exp
);
1041 /* Get SSA name for the new variable and set make new statement
1042 its definition statement. */
1043 new_name
= make_ssa_name (var
, stmt
);
1044 gimple_assign_set_lhs (stmt
, new_name
);
1045 SSA_NAME_DEF_STMT (new_name
) = stmt
;
1052 /* Return TRUE iff, all pred blocks of BB are visited.
1053 Bitmap VISITED keeps history of visited blocks. */
1056 pred_blocks_visited_p (basic_block bb
, bitmap
*visited
)
1060 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1061 if (!bitmap_bit_p (*visited
, e
->src
->index
))
1067 /* Get body of a LOOP in suitable order for if-conversion.
1068 It is caller's responsibility to deallocate basic block
1069 list. If-conversion suitable order is, BFS order with one
1070 additional constraint. Select block in BFS block, if all
1071 pred are already selected. */
1073 static basic_block
*
1074 get_loop_body_in_if_conv_order (const struct loop
*loop
)
1076 basic_block
*blocks
, *blocks_in_bfs_order
;
1079 unsigned int index
= 0;
1080 unsigned int visited_count
= 0;
1082 gcc_assert (loop
->num_nodes
);
1083 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
1085 blocks
= XCNEWVEC (basic_block
, loop
->num_nodes
);
1086 visited
= BITMAP_ALLOC (NULL
);
1088 blocks_in_bfs_order
= get_loop_body_in_bfs_order (loop
);
1091 while (index
< loop
->num_nodes
)
1093 bb
= blocks_in_bfs_order
[index
];
1095 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1097 free (blocks_in_bfs_order
);
1098 BITMAP_FREE (visited
);
1102 if (!bitmap_bit_p (visited
, bb
->index
))
1104 if (pred_blocks_visited_p (bb
, &visited
)
1105 || bb
== loop
->header
)
1107 /* This block is now visited. */
1108 bitmap_set_bit (visited
, bb
->index
);
1109 blocks
[visited_count
++] = bb
;
1113 if (index
== loop
->num_nodes
1114 && visited_count
!= loop
->num_nodes
)
1120 free (blocks_in_bfs_order
);
1121 BITMAP_FREE (visited
);
1125 /* Return true if one of the basic block BB edge is exit of LOOP. */
1128 bb_with_exit_edge_p (struct loop
*loop
, basic_block bb
)
1132 bool exit_edge_found
= false;
1134 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1135 if (loop_exit_edge_p (loop
, e
))
1137 exit_edge_found
= true;
1141 return exit_edge_found
;
1144 /* Tree if-conversion pass management. */
1147 main_tree_if_conversion (void)
1152 if (number_of_loops () <= 1)
1155 FOR_EACH_LOOP (li
, loop
, 0)
1157 tree_if_conversion (loop
, true);
1163 gate_tree_if_conversion (void)
1165 return flag_tree_vectorize
!= 0;
1168 struct gimple_opt_pass pass_if_conversion
=
1173 gate_tree_if_conversion
, /* gate */
1174 main_tree_if_conversion
, /* execute */
1177 0, /* static_pass_number */
1178 TV_NONE
, /* tv_id */
1179 PROP_cfg
| PROP_ssa
, /* properties_required */
1180 0, /* properties_provided */
1181 0, /* properties_destroyed */
1182 0, /* todo_flags_start */
1183 TODO_dump_func
| TODO_verify_loops
| TODO_verify_stmts
| TODO_verify_flow
1184 /* todo_flags_finish */