1 /* If-conversion for vectorizer.
2 Copyright (C) 2004, 2005 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 2, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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"
94 #include "basic-block.h"
95 #include "diagnostic.h"
96 #include "tree-flow.h"
97 #include "tree-dump.h"
99 #include "tree-chrec.h"
100 #include "tree-data-ref.h"
101 #include "tree-scalar-evolution.h"
102 #include "tree-pass.h"
105 /* local function prototypes */
106 static void main_tree_if_conversion (void);
107 static tree
tree_if_convert_stmt (struct loop
*loop
, tree
, tree
,
108 block_stmt_iterator
*);
109 static void tree_if_convert_cond_expr (struct loop
*, tree
, tree
,
110 block_stmt_iterator
*);
111 static bool if_convertible_phi_p (struct loop
*, basic_block
, tree
);
112 static bool if_convertible_modify_expr_p (struct loop
*, basic_block
, tree
);
113 static bool if_convertible_stmt_p (struct loop
*, basic_block
, tree
);
114 static bool if_convertible_bb_p (struct loop
*, basic_block
, bool);
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
, basic_block
, tree
, tree
,
118 block_stmt_iterator
*);
119 static void clean_predicate_lists (struct loop
*loop
);
120 static basic_block
find_phi_replacement_condition (struct loop
*loop
,
122 block_stmt_iterator
*);
123 static void replace_phi_with_cond_modify_expr (tree
, tree
, basic_block
,
124 block_stmt_iterator
*);
125 static void process_phi_nodes (struct loop
*);
126 static void combine_blocks (struct loop
*);
127 static tree
ifc_temp_var (tree
, tree
);
128 static bool pred_blocks_visited_p (basic_block
, bitmap
*);
129 static basic_block
* get_loop_body_in_if_conv_order (const struct loop
*loop
);
130 static bool bb_with_exit_edge_p (struct loop
*, basic_block
);
132 /* List of basic blocks in if-conversion-suitable order. */
133 static basic_block
*ifc_bbs
;
136 Apply if-conversion to the LOOP. Return true if successful otherwise return
137 false. If false is returned then loop remains unchanged.
138 FOR_VECTORIZER is a boolean flag. It indicates whether if-conversion is used
139 for vectorizer or not. If it is used for vectorizer, additional checks are
140 used. (Vectorization checks are not yet implemented). */
143 tree_if_conversion (struct loop
*loop
, bool for_vectorizer
)
146 block_stmt_iterator itr
;
152 /* if-conversion is not appropriate for all loops. First, check if loop is
153 if-convertible or not. */
154 if (!if_convertible_loop_p (loop
, for_vectorizer
))
156 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
157 fprintf (dump_file
,"-------------------------\n");
163 free_dominance_info (CDI_POST_DOMINATORS
);
169 /* Do actual work now. */
170 for (i
= 0; i
< loop
->num_nodes
; i
++)
174 /* Update condition using predicate list. */
177 /* Process all statements in this basic block.
178 Remove conditional expression, if any, and annotate
179 destination basic block(s) appropriately. */
180 for (itr
= bsi_start (bb
); !bsi_end_p (itr
); /* empty */)
182 tree t
= bsi_stmt (itr
);
183 cond
= tree_if_convert_stmt (loop
, t
, cond
, &itr
);
184 if (!bsi_end_p (itr
))
188 /* If current bb has only one successor, then consider it as an
189 unconditional goto. */
190 if (single_succ_p (bb
))
192 basic_block bb_n
= single_succ (bb
);
193 if (cond
!= NULL_TREE
)
194 add_to_predicate_list (bb_n
, cond
);
199 /* Now, all statements are if-converted and basic blocks are
200 annotated appropriately. Combine all basic block into one huge
202 combine_blocks (loop
);
205 clean_predicate_lists (loop
);
212 /* if-convert stmt T which is part of LOOP.
213 If T is a MODIFY_EXPR than it is converted into conditional modify
214 expression using COND. For conditional expressions, add condition in the
215 destination basic block's predicate list and remove conditional
216 expression itself. BSI is the iterator used to traverse statements of
217 loop. It is used here when it is required to delete current statement. */
220 tree_if_convert_stmt (struct loop
* loop
, tree t
, tree cond
,
221 block_stmt_iterator
*bsi
)
223 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
225 fprintf (dump_file
, "------if-convert stmt\n");
226 print_generic_stmt (dump_file
, t
, TDF_SLIM
);
227 print_generic_stmt (dump_file
, cond
, TDF_SLIM
);
230 switch (TREE_CODE (t
))
232 /* Labels are harmless here. */
237 /* This modify_expr is killing previous value of LHS. Appropriate value will
238 be selected by PHI node based on condition. It is possible that before
239 this transformation, PHI nodes was selecting default value and now it will
240 use this new value. This is OK because it does not change validity the
245 /* Unconditional goto */
246 add_to_predicate_list (bb_for_stmt (TREE_OPERAND (t
, 1)), cond
);
252 /* Update destination blocks' predicate list and remove this
253 condition expression. */
254 tree_if_convert_cond_expr (loop
, t
, cond
, bsi
);
264 /* STMT is COND_EXPR. Update two destination's predicate list.
265 Remove COND_EXPR, if it is not the loop exit condition. Otherwise
266 update loop exit condition appropriately. BSI is the iterator
267 used to traverse statement list. STMT is part of loop LOOP. */
270 tree_if_convert_cond_expr (struct loop
*loop
, tree stmt
, tree cond
,
271 block_stmt_iterator
*bsi
)
274 edge true_edge
, false_edge
;
276 gcc_assert (TREE_CODE (stmt
) == COND_EXPR
);
278 c
= COND_EXPR_COND (stmt
);
280 /* Create temp. for condition. */
281 if (!is_gimple_condexpr (c
))
284 new_stmt
= ifc_temp_var (TREE_TYPE (c
), unshare_expr (c
));
285 bsi_insert_before (bsi
, new_stmt
, BSI_SAME_STMT
);
286 c
= TREE_OPERAND (new_stmt
, 0);
289 extract_true_false_edges_from_block (bb_for_stmt (stmt
),
290 &true_edge
, &false_edge
);
292 /* Add new condition into destination's predicate list. */
294 /* If 'c' is true then TRUE_EDGE is taken. */
295 add_to_dst_predicate_list (loop
, true_edge
->dest
, cond
,
296 unshare_expr (c
), bsi
);
298 if (!is_gimple_reg(c
) && is_gimple_condexpr (c
))
301 new_stmt
= ifc_temp_var (TREE_TYPE (c
), unshare_expr (c
));
302 bsi_insert_before (bsi
, new_stmt
, BSI_SAME_STMT
);
303 c
= TREE_OPERAND (new_stmt
, 0);
306 /* If 'c' is false then FALSE_EDGE is taken. */
307 c2
= invert_truthvalue (unshare_expr (c
));
308 add_to_dst_predicate_list (loop
, false_edge
->dest
, cond
, c2
, bsi
);
310 /* Now this conditional statement is redundant. Remove it.
311 But, do not remove exit condition! Update exit condition
312 using new condition. */
313 if (!bb_with_exit_edge_p (loop
, bb_for_stmt (stmt
)))
321 /* Return true, iff PHI is if-convertible. PHI is part of loop LOOP
322 and it belongs to basic block BB.
323 PHI is not if-convertible
324 - if it has more than 2 arguments.
325 - Virtual PHI is immediately used in another PHI node. */
328 if_convertible_phi_p (struct loop
*loop
, basic_block bb
, tree phi
)
330 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
332 fprintf (dump_file
, "-------------------------\n");
333 print_generic_stmt (dump_file
, phi
, TDF_SLIM
);
336 if (bb
!= loop
->header
&& PHI_NUM_ARGS (phi
) != 2)
338 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
339 fprintf (dump_file
, "More than two phi node args.\n");
343 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi
))))
345 imm_use_iterator imm_iter
;
347 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, PHI_RESULT (phi
))
349 if (TREE_CODE (USE_STMT (use_p
)) == PHI_NODE
)
351 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
352 fprintf (dump_file
, "Difficult to handle this virtual phi.\n");
361 /* Return true, if M_EXPR is if-convertible.
362 MODIFY_EXPR is not if-convertible if,
365 - LHS is not var decl.
366 MODIFY_EXPR is part of block BB, which is inside loop LOOP.
370 if_convertible_modify_expr_p (struct loop
*loop
, basic_block bb
, tree m_expr
)
372 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
374 fprintf (dump_file
, "-------------------------\n");
375 print_generic_stmt (dump_file
, m_expr
, TDF_SLIM
);
378 /* Be conservative and do not handle immovable expressions. */
379 if (movement_possibility (m_expr
) == MOVE_IMPOSSIBLE
)
381 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
382 fprintf (dump_file
, "stmt is movable. Don't take risk\n");
386 /* See if it needs speculative loading or not. */
387 if (bb
!= loop
->header
388 && tree_could_trap_p (TREE_OPERAND (m_expr
, 1)))
390 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
391 fprintf (dump_file
, "tree could trap...\n");
395 if (TREE_CODE (TREE_OPERAND (m_expr
, 1)) == CALL_EXPR
)
397 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
398 fprintf (dump_file
, "CALL_EXPR \n");
402 if (TREE_CODE (TREE_OPERAND (m_expr
, 0)) != SSA_NAME
403 && bb
!= loop
->header
404 && !bb_with_exit_edge_p (loop
, bb
))
406 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
408 fprintf (dump_file
, "LHS is not var\n");
409 print_generic_stmt (dump_file
, m_expr
, TDF_SLIM
);
418 /* Return true, iff STMT is if-convertible.
419 Statement is if-convertible if,
420 - It is if-convertible MODIFY_EXPR
421 - IT is LABEL_EXPR, GOTO_EXPR or COND_EXPR.
422 STMT is inside block BB, which is inside loop LOOP. */
425 if_convertible_stmt_p (struct loop
*loop
, basic_block bb
, tree stmt
)
427 switch (TREE_CODE (stmt
))
434 if (!if_convertible_modify_expr_p (loop
, bb
, stmt
))
443 /* Don't know what to do with 'em so don't do anything. */
444 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
446 fprintf (dump_file
, "don't know what to do\n");
447 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
456 /* Return true, iff BB is if-convertible.
457 Note: This routine does _not_ check basic block statements and phis.
458 Basic block is not if-convertible if,
459 - Basic block is non-empty and it is after exit block (in BFS order).
460 - Basic block is after exit block but before latch.
461 - Basic block edge(s) is not normal.
462 EXIT_BB_SEEN is true if basic block with exit edge is already seen.
463 BB is inside loop LOOP. */
466 if_convertible_bb_p (struct loop
*loop
, basic_block bb
, bool exit_bb_seen
)
471 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
472 fprintf (dump_file
, "----------[%d]-------------\n", bb
->index
);
476 if (bb
!= loop
->latch
)
478 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
479 fprintf (dump_file
, "basic block after exit bb but before latch\n");
482 else if (!empty_block_p (bb
))
484 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
485 fprintf (dump_file
, "non empty basic block after exit bb\n");
490 /* Be less adventurous and handle only normal edges. */
491 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
493 (EDGE_ABNORMAL_CALL
| EDGE_EH
| EDGE_ABNORMAL
| EDGE_IRREDUCIBLE_LOOP
))
495 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
496 fprintf (dump_file
,"Difficult to handle edges\n");
503 /* Return true, iff LOOP is if-convertible.
504 LOOP is if-convertible if,
506 - It has two or more basic blocks.
507 - It has only one exit.
508 - Loop header is not the exit edge.
509 - If its basic blocks and phi nodes are if convertible. See above for
511 FOR_VECTORIZER enables vectorizer specific checks. For example, support
512 for vector conditions, data dependency checks etc.. (Not implemented yet). */
515 if_convertible_loop_p (struct loop
*loop
, bool for_vectorizer ATTRIBUTE_UNUSED
)
519 block_stmt_iterator itr
;
523 bool exit_bb_seen
= false;
525 /* Handle only inner most loop. */
526 if (!loop
|| loop
->inner
)
528 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
529 fprintf (dump_file
, "not inner most loop\n");
533 /* If only one block, no need for if-conversion. */
534 if (loop
->num_nodes
<= 2)
536 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
537 fprintf (dump_file
, "less than 2 basic blocks\n");
541 /* More than one loop exit is too much to handle. */
542 if (!loop
->single_exit
)
544 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
545 fprintf (dump_file
, "multiple exits\n");
549 /* ??? Check target's vector conditional operation support for vectorizer. */
551 /* If one of the loop header's edge is exit edge then do not apply
553 FOR_EACH_EDGE (e
, ei
, loop
->header
->succs
)
555 if (loop_exit_edge_p (loop
, e
))
559 calculate_dominance_info (CDI_DOMINATORS
);
560 calculate_dominance_info (CDI_POST_DOMINATORS
);
562 /* Allow statements that can be handled during if-conversion. */
563 ifc_bbs
= get_loop_body_in_if_conv_order (loop
);
566 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
567 fprintf (dump_file
,"Irreducible loop\n");
568 free_dominance_info (CDI_POST_DOMINATORS
);
572 for (i
= 0; i
< loop
->num_nodes
; i
++)
576 if (!if_convertible_bb_p (loop
, bb
, exit_bb_seen
))
579 /* Check statements. */
580 for (itr
= bsi_start (bb
); !bsi_end_p (itr
); bsi_next (&itr
))
581 if (!if_convertible_stmt_p (loop
, bb
, bsi_stmt (itr
)))
583 /* ??? Check data dependency for vectorizer. */
585 /* What about phi nodes ? */
586 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
587 if (!if_convertible_phi_p (loop
, bb
, phi
))
590 if (bb_with_exit_edge_p (loop
, bb
))
594 /* OK. Did not find any potential issues so go ahead in if-convert
595 this loop. Now there is no looking back. */
597 fprintf (dump_file
,"Applying if-conversion\n");
599 free_dominance_info (CDI_POST_DOMINATORS
);
603 /* Add condition COND into predicate list of basic block BB. */
606 add_to_predicate_list (basic_block bb
, tree new_cond
)
611 cond
= fold (build (TRUTH_OR_EXPR
, boolean_type_node
,
612 unshare_expr (cond
), new_cond
));
619 /* Add condition COND into BB's predicate list. PREV_COND is
620 existing condition. */
623 add_to_dst_predicate_list (struct loop
* loop
, basic_block bb
,
624 tree prev_cond
, tree cond
,
625 block_stmt_iterator
*bsi
)
627 tree new_cond
= NULL_TREE
;
629 if (!flow_bb_inside_loop_p (loop
, bb
))
632 if (prev_cond
== boolean_true_node
|| !prev_cond
)
633 new_cond
= unshare_expr (cond
);
637 tree tmp_stmt
= NULL_TREE
;
638 tree tmp_stmts1
= NULL_TREE
;
639 tree tmp_stmts2
= NULL_TREE
;
640 prev_cond
= force_gimple_operand (unshare_expr (prev_cond
),
641 &tmp_stmts1
, true, NULL
);
643 bsi_insert_before (bsi
, tmp_stmts1
, BSI_SAME_STMT
);
645 cond
= force_gimple_operand (unshare_expr (cond
),
646 &tmp_stmts2
, true, NULL
);
648 bsi_insert_before (bsi
, tmp_stmts2
, BSI_SAME_STMT
);
650 /* new_cond == prev_cond AND cond */
651 tmp
= build (TRUTH_AND_EXPR
, boolean_type_node
,
652 unshare_expr (prev_cond
), cond
);
653 tmp_stmt
= ifc_temp_var (boolean_type_node
, tmp
);
654 bsi_insert_before (bsi
, tmp_stmt
, BSI_SAME_STMT
);
655 new_cond
= TREE_OPERAND (tmp_stmt
, 0);
657 add_to_predicate_list (bb
, new_cond
);
661 /* During if-conversion aux field from basic block is used to hold predicate
662 list. Clean each basic block's predicate list for the given LOOP. */
665 clean_predicate_lists (struct loop
*loop
)
669 bb
= get_loop_body (loop
);
670 for (i
= 0; i
< loop
->num_nodes
; i
++)
676 /* Basic block BB has two predecessors. Using predecessor's aux field, set
677 appropriate condition COND for the PHI node replacement. Return true block
678 whose phi arguments are selected when cond is true. */
681 find_phi_replacement_condition (struct loop
*loop
,
682 basic_block bb
, tree
*cond
,
683 block_stmt_iterator
*bsi
)
686 basic_block p1
= NULL
;
687 basic_block p2
= NULL
;
688 basic_block true_bb
= NULL
;
692 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
703 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
705 if (TREE_CODE (tmp_cond
) == TRUTH_NOT_EXPR
)
707 /* If p2 is loop->header than its aux field does not have useful
708 info. Instead use !(cond) where cond is p1's aux field. */
709 if (p2
== loop
->header
)
710 *cond
= invert_truthvalue (unshare_expr (p1
->aux
));
717 /* If p1 is loop->header than its aux field does not have useful
718 info. Instead use !(cond) where cond is p2's aux field. */
719 if (p1
== loop
->header
)
720 *cond
= invert_truthvalue (unshare_expr (p2
->aux
));
726 /* Create temp. for the condition. Vectorizer prefers to have gimple
727 value as condition. Various targets use different means to communicate
728 condition in vector compare operation. Using gimple value allows compiler
729 to emit vector compare and select RTL without exposing compare's result. */
730 if (!is_gimple_reg (*cond
) && !is_gimple_condexpr (*cond
))
734 new_stmt
= ifc_temp_var (TREE_TYPE (*cond
), unshare_expr (*cond
));
735 bsi_insert_after (bsi
, new_stmt
, BSI_SAME_STMT
);
737 *cond
= TREE_OPERAND (new_stmt
, 0);
746 /* Replace PHI node with conditional modify expr using COND.
747 This routine does not handle PHI nodes with more than two arguments.
749 S1: A = PHI <x1(1), x2(5)
751 S2: A = cond ? x1 : x2;
752 S2 is inserted at the top of basic block's statement list.
753 When COND is true, phi arg from TRUE_BB is selected.
757 replace_phi_with_cond_modify_expr (tree phi
, tree cond
, basic_block true_bb
,
758 block_stmt_iterator
*bsi
)
765 gcc_assert (TREE_CODE (phi
) == PHI_NODE
);
767 /* If this is not filtered earlier, then now it is too late. */
768 gcc_assert (PHI_NUM_ARGS (phi
) == 2);
770 /* Find basic block and initialize iterator. */
771 bb
= bb_for_stmt (phi
);
773 new_stmt
= NULL_TREE
;
777 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
778 if (EDGE_PRED (bb
, 1)->src
== true_bb
)
780 arg_0
= PHI_ARG_DEF (phi
, 1);
781 arg_1
= PHI_ARG_DEF (phi
, 0);
785 arg_0
= PHI_ARG_DEF (phi
, 0);
786 arg_1
= PHI_ARG_DEF (phi
, 1);
789 /* Build new RHS using selected condition and arguments. */
790 rhs
= build (COND_EXPR
, TREE_TYPE (PHI_RESULT (phi
)),
791 unshare_expr (cond
), unshare_expr (arg_0
),
792 unshare_expr (arg_1
));
794 /* Create new MODIFY expression using RHS. */
795 new_stmt
= build (MODIFY_EXPR
, TREE_TYPE (PHI_RESULT (phi
)),
796 unshare_expr (PHI_RESULT (phi
)), rhs
);
798 /* Make new statement definition of the original phi result. */
799 SSA_NAME_DEF_STMT (PHI_RESULT (phi
)) = new_stmt
;
801 /* Set basic block and insert using iterator. */
802 set_bb_for_stmt (new_stmt
, bb
);
804 bsi_insert_after (bsi
, new_stmt
, BSI_SAME_STMT
);
807 update_stmt (new_stmt
);
809 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
811 fprintf (dump_file
, "new phi replacement stmt\n");
812 print_generic_stmt (dump_file
, new_stmt
, TDF_SLIM
);
816 /* Process phi nodes for the given LOOP. Replace phi nodes with cond
820 process_phi_nodes (struct loop
*loop
)
823 unsigned int orig_loop_num_nodes
= loop
->num_nodes
;
826 /* Replace phi nodes with cond. modify expr. */
827 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
830 block_stmt_iterator bsi
;
831 basic_block true_bb
= NULL
;
834 if (bb
== loop
->header
)
837 phi
= phi_nodes (bb
);
838 bsi
= bsi_after_labels (bb
);
840 /* BB has two predecessors. Using predecessor's aux field, set
841 appropriate condition for the PHI node replacement. */
843 true_bb
= find_phi_replacement_condition (loop
, bb
, &cond
, &bsi
);
847 tree next
= PHI_CHAIN (phi
);
848 replace_phi_with_cond_modify_expr (phi
, cond
, true_bb
, &bsi
);
849 release_phi_node (phi
);
852 bb_ann (bb
)->phi_nodes
= NULL
;
857 /* Combine all basic block from the given LOOP into one or two super
858 basic block. Replace PHI nodes with conditional modify expression. */
861 combine_blocks (struct loop
*loop
)
863 basic_block bb
, exit_bb
, merge_target_bb
;
864 unsigned int orig_loop_num_nodes
= loop
->num_nodes
;
866 unsigned int n_exits
;
868 get_loop_exit_edges (loop
, &n_exits
);
869 /* Process phi nodes to prepare blocks for merge. */
870 process_phi_nodes (loop
);
874 /* Merge basic blocks */
875 merge_target_bb
= loop
->header
;
876 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
879 block_stmt_iterator bsi
;
880 tree_stmt_iterator last
;
884 if (!exit_bb
&& bb_with_exit_edge_p (loop
, bb
))
891 /* Connect this node with loop header. */
892 make_edge (ifc_bbs
[0], bb
, EDGE_FALLTHRU
);
893 set_immediate_dominator (CDI_DOMINATORS
, bb
, ifc_bbs
[0]);
895 if (exit_bb
!= loop
->latch
)
897 /* Redirect non-exit edge to loop->latch. */
898 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
900 if (!loop_exit_edge_p (loop
, e
))
902 redirect_edge_and_branch (e
, loop
->latch
);
903 set_immediate_dominator (CDI_DOMINATORS
, loop
->latch
, bb
);
910 if (bb
== loop
->latch
&& empty_block_p (bb
))
913 /* It is time to remove this basic block. First remove edges. */
914 while (EDGE_COUNT (bb
->preds
) > 0)
915 remove_edge (EDGE_PRED (bb
, 0));
917 /* This is loop latch and loop does not have exit then do not
918 delete this basic block. Just remove its PREDS and reconnect
919 loop->header and loop->latch blocks. */
920 if (bb
== loop
->latch
&& n_exits
== 0)
922 make_edge (loop
->header
, loop
->latch
, EDGE_FALLTHRU
);
923 set_immediate_dominator (CDI_DOMINATORS
, loop
->latch
, loop
->header
);
927 while (EDGE_COUNT (bb
->succs
) > 0)
928 remove_edge (EDGE_SUCC (bb
, 0));
930 /* Remove labels and make stmts member of loop->header. */
931 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); )
933 if (TREE_CODE (bsi_stmt (bsi
)) == LABEL_EXPR
)
937 set_bb_for_stmt (bsi_stmt (bsi
), merge_target_bb
);
942 /* Update stmt list. */
943 last
= tsi_last (merge_target_bb
->stmt_list
);
944 tsi_link_after (&last
, bb
->stmt_list
, TSI_NEW_STMT
);
945 bb
->stmt_list
= NULL
;
947 /* Update dominator info. */
948 if (dom_computed
[CDI_DOMINATORS
])
949 delete_from_dominance_info (CDI_DOMINATORS
, bb
);
950 if (dom_computed
[CDI_POST_DOMINATORS
])
951 delete_from_dominance_info (CDI_POST_DOMINATORS
, bb
);
953 /* Remove basic block. */
954 if (bb
== loop
->latch
)
955 loop
->latch
= merge_target_bb
;
956 remove_bb_from_loops (bb
);
960 /* Now if possible, merge loop header and block with exit edge.
961 This reduces number of basic blocks to 2. Auto vectorizer addresses
962 loops with two nodes only. FIXME: Use cleanup_tree_cfg(). */
964 && loop
->header
!= loop
->latch
965 && exit_bb
!= loop
->latch
966 && empty_block_p (loop
->latch
))
968 if (can_merge_blocks_p (loop
->header
, exit_bb
))
970 remove_bb_from_loops (exit_bb
);
971 merge_blocks (loop
->header
, exit_bb
);
976 /* Make new temp variable of type TYPE. Add MODIFY_EXPR to assign EXP
977 to the new variable. */
980 ifc_temp_var (tree type
, tree exp
)
982 const char *name
= "_ifc_";
983 tree var
, stmt
, new_name
;
985 if (is_gimple_reg (exp
))
988 /* Create new temporary variable. */
989 var
= create_tmp_var (type
, name
);
990 add_referenced_tmp_var (var
);
992 /* Build new statement to assign EXP to new variable. */
993 stmt
= build (MODIFY_EXPR
, type
, var
, exp
);
995 /* Get SSA name for the new variable and set make new statement
996 its definition statement. */
997 new_name
= make_ssa_name (var
, stmt
);
998 TREE_OPERAND (stmt
, 0) = new_name
;
999 SSA_NAME_DEF_STMT (new_name
) = stmt
;
1005 /* Return TRUE iff, all pred blocks of BB are visited.
1006 Bitmap VISITED keeps history of visited blocks. */
1009 pred_blocks_visited_p (basic_block bb
, bitmap
*visited
)
1013 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1014 if (!bitmap_bit_p (*visited
, e
->src
->index
))
1020 /* Get body of a LOOP in suitable order for if-conversion.
1021 It is caller's responsibility to deallocate basic block
1022 list. If-conversion suitable order is, BFS order with one
1023 additional constraint. Select block in BFS block, if all
1024 pred are already selected. */
1026 static basic_block
*
1027 get_loop_body_in_if_conv_order (const struct loop
*loop
)
1029 basic_block
*blocks
, *blocks_in_bfs_order
;
1032 unsigned int index
= 0;
1033 unsigned int visited_count
= 0;
1035 gcc_assert (loop
->num_nodes
);
1036 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
1038 blocks
= xcalloc (loop
->num_nodes
, sizeof (basic_block
));
1039 visited
= BITMAP_ALLOC (NULL
);
1041 blocks_in_bfs_order
= get_loop_body_in_bfs_order (loop
);
1044 while (index
< loop
->num_nodes
)
1046 bb
= blocks_in_bfs_order
[index
];
1048 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
1050 free (blocks_in_bfs_order
);
1051 BITMAP_FREE (visited
);
1055 if (!bitmap_bit_p (visited
, bb
->index
))
1057 if (pred_blocks_visited_p (bb
, &visited
)
1058 || bb
== loop
->header
)
1060 /* This block is now visited. */
1061 bitmap_set_bit (visited
, bb
->index
);
1062 blocks
[visited_count
++] = bb
;
1066 if (index
== loop
->num_nodes
1067 && visited_count
!= loop
->num_nodes
)
1073 free (blocks_in_bfs_order
);
1074 BITMAP_FREE (visited
);
1078 /* Return true if one of the basic block BB edge is exit of LOOP. */
1081 bb_with_exit_edge_p (struct loop
*loop
, basic_block bb
)
1085 bool exit_edge_found
= false;
1087 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1088 if (loop_exit_edge_p (loop
, e
))
1090 exit_edge_found
= true;
1094 return exit_edge_found
;
1097 /* Tree if-conversion pass management. */
1100 main_tree_if_conversion (void)
1102 unsigned i
, loop_num
;
1108 loop_num
= current_loops
->num
;
1109 for (i
= 0; i
< loop_num
; i
++)
1111 loop
= current_loops
->parray
[i
];
1115 tree_if_conversion (loop
, true);
1121 gate_tree_if_conversion (void)
1123 return flag_tree_vectorize
!= 0;
1126 struct tree_opt_pass pass_if_conversion
=
1129 gate_tree_if_conversion
, /* gate */
1130 main_tree_if_conversion
, /* execute */
1133 0, /* static_pass_number */
1135 PROP_cfg
| PROP_ssa
| PROP_alias
, /* properties_required */
1136 0, /* properties_provided */
1137 0, /* properties_destroyed */
1138 0, /* todo_flags_start */
1139 TODO_dump_func
| TODO_verify_loops
, /* todo_flags_finish */