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
, boolean_true_node
);
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 /* Free the predicate of BB and reinitialize it with the true
210 reset_bb_predicate (basic_block bb
)
212 free_bb_predicate (bb
);
213 init_bb_predicate (bb
);
216 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
217 the expression EXPR. Inserts the statement created for this
218 computation before GSI and leaves the iterator GSI at the same
222 ifc_temp_var (tree type
, tree expr
, gimple_stmt_iterator
*gsi
)
224 const char *name
= "_ifc_";
228 /* Create new temporary variable. */
229 var
= create_tmp_var (type
, name
);
230 add_referenced_var (var
);
232 /* Build new statement to assign EXPR to new variable. */
233 stmt
= gimple_build_assign (var
, expr
);
235 /* Get SSA name for the new variable and set make new statement
236 its definition statement. */
237 new_name
= make_ssa_name (var
, stmt
);
238 gimple_assign_set_lhs (stmt
, new_name
);
239 SSA_NAME_DEF_STMT (new_name
) = stmt
;
242 gsi_insert_before (gsi
, stmt
, GSI_SAME_STMT
);
243 return gimple_assign_lhs (stmt
);
246 /* Return true when COND is a true predicate. */
249 is_true_predicate (tree cond
)
251 return (cond
== NULL_TREE
252 || cond
== boolean_true_node
253 || integer_onep (cond
));
256 /* Returns true when BB has a predicate that is not trivial: true or
260 is_predicated (basic_block bb
)
262 return !is_true_predicate (bb_predicate (bb
));
265 /* Parses the predicate COND and returns its comparison code and
266 operands OP0 and OP1. */
268 static enum tree_code
269 parse_predicate (tree cond
, tree
*op0
, tree
*op1
)
273 if (TREE_CODE (cond
) == SSA_NAME
274 && is_gimple_assign (s
= SSA_NAME_DEF_STMT (cond
)))
276 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s
)) == tcc_comparison
)
278 *op0
= gimple_assign_rhs1 (s
);
279 *op1
= gimple_assign_rhs2 (s
);
280 return gimple_assign_rhs_code (s
);
283 else if (gimple_assign_rhs_code (s
) == TRUTH_NOT_EXPR
)
285 tree op
= gimple_assign_rhs1 (s
);
286 tree type
= TREE_TYPE (op
);
287 enum tree_code code
= parse_predicate (op
, op0
, op1
);
289 return code
== ERROR_MARK
? ERROR_MARK
290 : invert_tree_comparison (code
, HONOR_NANS (TYPE_MODE (type
)));
296 if (TREE_CODE_CLASS (TREE_CODE (cond
)) == tcc_comparison
)
298 *op0
= TREE_OPERAND (cond
, 0);
299 *op1
= TREE_OPERAND (cond
, 1);
300 return TREE_CODE (cond
);
306 /* Returns the fold of predicate C1 OR C2 at location LOC. */
309 fold_or_predicates (location_t loc
, tree c1
, tree c2
)
311 tree op1a
, op1b
, op2a
, op2b
;
312 enum tree_code code1
= parse_predicate (c1
, &op1a
, &op1b
);
313 enum tree_code code2
= parse_predicate (c2
, &op2a
, &op2b
);
315 if (code1
!= ERROR_MARK
&& code2
!= ERROR_MARK
)
317 tree t
= maybe_fold_or_comparisons (code1
, op1a
, op1b
,
323 return fold_build2_loc (loc
, TRUTH_OR_EXPR
, boolean_type_node
, c1
, c2
);
326 /* Add condition NC to the predicate list of basic block BB. */
329 add_to_predicate_list (basic_block bb
, tree nc
)
333 if (is_true_predicate (nc
))
336 if (!is_predicated (bb
))
340 bc
= bb_predicate (bb
);
341 bc
= fold_or_predicates (EXPR_LOCATION (bc
), nc
, bc
);
344 if (!is_gimple_condexpr (bc
))
347 bc
= force_gimple_operand (bc
, &stmts
, true, NULL_TREE
);
348 add_bb_predicate_gimplified_stmts (bb
, stmts
);
351 if (is_true_predicate (bc
))
352 reset_bb_predicate (bb
);
354 set_bb_predicate (bb
, bc
);
357 /* Add the condition COND to the previous condition PREV_COND, and add
358 this to the predicate list of the destination of edge E. LOOP is
359 the loop to be if-converted. */
362 add_to_dst_predicate_list (struct loop
*loop
, edge e
,
363 tree prev_cond
, tree cond
)
365 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
368 if (!is_true_predicate (prev_cond
))
369 cond
= fold_build2 (TRUTH_AND_EXPR
, boolean_type_node
,
372 add_to_predicate_list (e
->dest
, cond
);
375 /* Return true if one of the successor edges of BB exits LOOP. */
378 bb_with_exit_edge_p (struct loop
*loop
, basic_block bb
)
383 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
384 if (loop_exit_edge_p (loop
, e
))
390 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
391 and it belongs to basic block BB.
393 PHI is not if-convertible if:
394 - it has more than 2 arguments.
396 When the flag_tree_loop_if_convert_stores is not set, PHI is not
398 - a virtual PHI is immediately used in another PHI node,
399 - there is a virtual PHI in a BB other than the loop->header. */
402 if_convertible_phi_p (struct loop
*loop
, basic_block bb
, gimple phi
)
404 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
406 fprintf (dump_file
, "-------------------------\n");
407 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
410 if (bb
!= loop
->header
&& gimple_phi_num_args (phi
) != 2)
412 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
413 fprintf (dump_file
, "More than two phi node args.\n");
417 if (flag_tree_loop_if_convert_stores
)
420 /* When the flag_tree_loop_if_convert_stores is not set, check
421 that there are no memory writes in the branches of the loop to be
423 if (!is_gimple_reg (SSA_NAME_VAR (gimple_phi_result (phi
))))
425 imm_use_iterator imm_iter
;
428 if (bb
!= loop
->header
)
430 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
431 fprintf (dump_file
, "Virtual phi not on loop->header.\n");
435 FOR_EACH_IMM_USE_FAST (use_p
, imm_iter
, gimple_phi_result (phi
))
437 if (gimple_code (USE_STMT (use_p
)) == GIMPLE_PHI
)
439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
440 fprintf (dump_file
, "Difficult to handle this virtual phi.\n");
449 /* Records the status of a data reference. This struct is attached to
450 each DR->aux field. */
453 /* -1 when not initialized, 0 when false, 1 when true. */
454 int written_at_least_once
;
456 /* -1 when not initialized, 0 when false, 1 when true. */
457 int rw_unconditionally
;
460 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
461 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
462 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
464 /* Returns true when the memory references of STMT are read or written
465 unconditionally. In other words, this function returns true when
466 for every data reference A in STMT there exist other accesses to
467 the same data reference with predicates that add up (OR-up) to the
468 true predicate: this ensures that the data reference A is touched
469 (read or written) on every iteration of the if-converted loop. */
472 memrefs_read_or_written_unconditionally (gimple stmt
,
473 VEC (data_reference_p
, heap
) *drs
)
476 data_reference_p a
, b
;
477 tree ca
= bb_predicate (gimple_bb (stmt
));
479 for (i
= 0; VEC_iterate (data_reference_p
, drs
, i
, a
); i
++)
480 if (DR_STMT (a
) == stmt
)
483 int x
= DR_RW_UNCONDITIONALLY (a
);
491 for (j
= 0; VEC_iterate (data_reference_p
, drs
, j
, b
); j
++)
492 if (DR_STMT (b
) != stmt
493 && same_data_refs (a
, b
))
495 tree cb
= bb_predicate (gimple_bb (DR_STMT (b
)));
497 if (DR_RW_UNCONDITIONALLY (b
) == 1
498 || is_true_predicate (cb
)
499 || is_true_predicate (ca
= fold_or_predicates (EXPR_LOCATION (cb
),
502 DR_RW_UNCONDITIONALLY (a
) = 1;
503 DR_RW_UNCONDITIONALLY (b
) = 1;
511 DR_RW_UNCONDITIONALLY (a
) = 0;
519 /* Returns true when the memory references of STMT are unconditionally
520 written. In other words, this function returns true when for every
521 data reference A written in STMT, there exist other writes to the
522 same data reference with predicates that add up (OR-up) to the true
523 predicate: this ensures that the data reference A is written on
524 every iteration of the if-converted loop. */
527 write_memrefs_written_at_least_once (gimple stmt
,
528 VEC (data_reference_p
, heap
) *drs
)
531 data_reference_p a
, b
;
532 tree ca
= bb_predicate (gimple_bb (stmt
));
534 for (i
= 0; VEC_iterate (data_reference_p
, drs
, i
, a
); i
++)
535 if (DR_STMT (a
) == stmt
539 int x
= DR_WRITTEN_AT_LEAST_ONCE (a
);
547 for (j
= 0; VEC_iterate (data_reference_p
, drs
, j
, b
); j
++)
548 if (DR_STMT (b
) != stmt
550 && same_data_refs_base_objects (a
, b
))
552 tree cb
= bb_predicate (gimple_bb (DR_STMT (b
)));
554 if (DR_WRITTEN_AT_LEAST_ONCE (b
) == 1
555 || is_true_predicate (cb
)
556 || is_true_predicate (ca
= fold_or_predicates (EXPR_LOCATION (cb
),
559 DR_WRITTEN_AT_LEAST_ONCE (a
) = 1;
560 DR_WRITTEN_AT_LEAST_ONCE (b
) = 1;
568 DR_WRITTEN_AT_LEAST_ONCE (a
) = 0;
576 /* Return true when the memory references of STMT won't trap in the
577 if-converted code. There are two things that we have to check for:
579 - writes to memory occur to writable memory: if-conversion of
580 memory writes transforms the conditional memory writes into
581 unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
582 into "A[i] = cond ? foo : A[i]", and as the write to memory may not
583 be executed at all in the original code, it may be a readonly
584 memory. To check that A is not const-qualified, we check that
585 there exists at least an unconditional write to A in the current
588 - reads or writes to memory are valid memory accesses for every
589 iteration. To check that the memory accesses are correctly formed
590 and that we are allowed to read and write in these locations, we
591 check that the memory accesses to be if-converted occur at every
592 iteration unconditionally. */
595 ifcvt_memrefs_wont_trap (gimple stmt
, VEC (data_reference_p
, heap
) *refs
)
597 return write_memrefs_written_at_least_once (stmt
, refs
)
598 && memrefs_read_or_written_unconditionally (stmt
, refs
);
601 /* Wrapper around gimple_could_trap_p refined for the needs of the
602 if-conversion. Try to prove that the memory accesses of STMT could
603 not trap in the innermost loop containing STMT. */
606 ifcvt_could_trap_p (gimple stmt
, VEC (data_reference_p
, heap
) *refs
)
608 if (gimple_vuse (stmt
)
609 && !gimple_could_trap_p_1 (stmt
, false, false)
610 && ifcvt_memrefs_wont_trap (stmt
, refs
))
613 return gimple_could_trap_p (stmt
);
616 /* Return true when STMT is if-convertible.
618 GIMPLE_ASSIGN statement is not if-convertible if,
621 - LHS is not var decl. */
624 if_convertible_gimple_assign_stmt_p (gimple stmt
,
625 VEC (data_reference_p
, heap
) *refs
)
627 tree lhs
= gimple_assign_lhs (stmt
);
630 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
632 fprintf (dump_file
, "-------------------------\n");
633 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
636 if (!is_gimple_reg_type (TREE_TYPE (lhs
)))
639 /* Some of these constrains might be too conservative. */
640 if (stmt_ends_bb_p (stmt
)
641 || gimple_has_volatile_ops (stmt
)
642 || (TREE_CODE (lhs
) == SSA_NAME
643 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
644 || gimple_has_side_effects (stmt
))
646 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
647 fprintf (dump_file
, "stmt not suitable for ifcvt\n");
651 if (flag_tree_loop_if_convert_stores
)
653 if (ifcvt_could_trap_p (stmt
, refs
))
655 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
656 fprintf (dump_file
, "tree could trap...\n");
662 if (gimple_assign_rhs_could_trap_p (stmt
))
664 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
665 fprintf (dump_file
, "tree could trap...\n");
669 bb
= gimple_bb (stmt
);
671 if (TREE_CODE (lhs
) != SSA_NAME
672 && bb
!= bb
->loop_father
->header
673 && !bb_with_exit_edge_p (bb
->loop_father
, bb
))
675 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
677 fprintf (dump_file
, "LHS is not var\n");
678 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
686 /* Return true when STMT is if-convertible.
688 A statement is if-convertible if:
689 - it is an if-convertible GIMPLE_ASSGIN,
690 - it is a GIMPLE_LABEL or a GIMPLE_COND. */
693 if_convertible_stmt_p (gimple stmt
, VEC (data_reference_p
, heap
) *refs
)
695 switch (gimple_code (stmt
))
703 return if_convertible_gimple_assign_stmt_p (stmt
, refs
);
706 /* Don't know what to do with 'em so don't do anything. */
707 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
709 fprintf (dump_file
, "don't know what to do\n");
710 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
719 /* Return true when BB is if-convertible. This routine does not check
720 basic block's statements and phis.
722 A basic block is not if-convertible if:
723 - it is non-empty and it is after the exit block (in BFS order),
724 - it is after the exit block but before the latch,
725 - its edges are not normal.
727 EXIT_BB is the basic block containing the exit of the LOOP. BB is
731 if_convertible_bb_p (struct loop
*loop
, basic_block bb
, basic_block exit_bb
)
736 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
737 fprintf (dump_file
, "----------[%d]-------------\n", bb
->index
);
739 if (EDGE_COUNT (bb
->preds
) > 2
740 || EDGE_COUNT (bb
->succs
) > 2)
745 if (bb
!= loop
->latch
)
747 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
748 fprintf (dump_file
, "basic block after exit bb but before latch\n");
751 else if (!empty_block_p (bb
))
753 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
754 fprintf (dump_file
, "non empty basic block after exit bb\n");
757 else if (bb
== loop
->latch
759 && !dominated_by_p (CDI_DOMINATORS
, bb
, exit_bb
))
761 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
762 fprintf (dump_file
, "latch is not dominated by exit_block\n");
767 /* Be less adventurous and handle only normal edges. */
768 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
770 (EDGE_ABNORMAL_CALL
| EDGE_EH
| EDGE_ABNORMAL
| EDGE_IRREDUCIBLE_LOOP
))
772 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
773 fprintf (dump_file
, "Difficult to handle edges\n");
780 /* Return true when all predecessor blocks of BB are visited. The
781 VISITED bitmap keeps track of the visited blocks. */
784 pred_blocks_visited_p (basic_block bb
, bitmap
*visited
)
788 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
789 if (!bitmap_bit_p (*visited
, e
->src
->index
))
795 /* Get body of a LOOP in suitable order for if-conversion. It is
796 caller's responsibility to deallocate basic block list.
797 If-conversion suitable order is, breadth first sort (BFS) order
798 with an additional constraint: select a block only if all its
799 predecessors are already selected. */
802 get_loop_body_in_if_conv_order (const struct loop
*loop
)
804 basic_block
*blocks
, *blocks_in_bfs_order
;
807 unsigned int index
= 0;
808 unsigned int visited_count
= 0;
810 gcc_assert (loop
->num_nodes
);
811 gcc_assert (loop
->latch
!= EXIT_BLOCK_PTR
);
813 blocks
= XCNEWVEC (basic_block
, loop
->num_nodes
);
814 visited
= BITMAP_ALLOC (NULL
);
816 blocks_in_bfs_order
= get_loop_body_in_bfs_order (loop
);
819 while (index
< loop
->num_nodes
)
821 bb
= blocks_in_bfs_order
[index
];
823 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
825 free (blocks_in_bfs_order
);
826 BITMAP_FREE (visited
);
831 if (!bitmap_bit_p (visited
, bb
->index
))
833 if (pred_blocks_visited_p (bb
, &visited
)
834 || bb
== loop
->header
)
836 /* This block is now visited. */
837 bitmap_set_bit (visited
, bb
->index
);
838 blocks
[visited_count
++] = bb
;
844 if (index
== loop
->num_nodes
845 && visited_count
!= loop
->num_nodes
)
849 free (blocks_in_bfs_order
);
850 BITMAP_FREE (visited
);
854 /* Returns true when the analysis of the predicates for all the basic
855 blocks in LOOP succeeded.
857 predicate_bbs first allocates the predicates of the basic blocks.
858 These fields are then initialized with the tree expressions
859 representing the predicates under which a basic block is executed
860 in the LOOP. As the loop->header is executed at each iteration, it
861 has the "true" predicate. Other statements executed under a
862 condition are predicated with that condition, for example
869 S1 will be predicated with "x", and
870 S2 will be predicated with "!x". */
873 predicate_bbs (loop_p loop
)
877 for (i
= 0; i
< loop
->num_nodes
; i
++)
878 init_bb_predicate (ifc_bbs
[i
]);
880 for (i
= 0; i
< loop
->num_nodes
; i
++)
882 basic_block bb
= ifc_bbs
[i
];
884 gimple_stmt_iterator itr
;
886 /* The loop latch is always executed and has no extra conditions
887 to be processed: skip it. */
888 if (bb
== loop
->latch
)
890 reset_bb_predicate (loop
->latch
);
894 cond
= bb_predicate (bb
);
896 && bb
!= loop
->header
)
900 cond
= force_gimple_operand (cond
, &stmts
, true, NULL_TREE
);
901 add_bb_predicate_gimplified_stmts (bb
, stmts
);
904 for (itr
= gsi_start_bb (bb
); !gsi_end_p (itr
); gsi_next (&itr
))
906 gimple stmt
= gsi_stmt (itr
);
908 switch (gimple_code (stmt
))
919 edge true_edge
, false_edge
;
920 location_t loc
= gimple_location (stmt
);
921 tree c
= fold_build2_loc (loc
, gimple_cond_code (stmt
),
923 gimple_cond_lhs (stmt
),
924 gimple_cond_rhs (stmt
));
926 /* Add new condition into destination's predicate list. */
927 extract_true_false_edges_from_block (gimple_bb (stmt
),
928 &true_edge
, &false_edge
);
930 /* If C is true, then TRUE_EDGE is taken. */
931 add_to_dst_predicate_list (loop
, true_edge
, cond
, unshare_expr (c
));
933 /* If C is false, then FALSE_EDGE is taken. */
934 c2
= invert_truthvalue_loc (loc
, unshare_expr (c
));
935 tem
= canonicalize_cond_expr_cond (c2
);
938 add_to_dst_predicate_list (loop
, false_edge
, cond
, c2
);
945 /* Not handled yet in if-conversion. */
950 /* If current bb has only one successor, then consider it as an
951 unconditional goto. */
952 if (single_succ_p (bb
))
954 basic_block bb_n
= single_succ (bb
);
956 /* The successor bb inherits the predicate of its
957 predecessor. If there is no predicate in the predecessor
958 bb, then consider the successor bb as always executed. */
959 if (cond
== NULL_TREE
)
960 cond
= boolean_true_node
;
962 add_to_predicate_list (bb_n
, cond
);
966 /* The loop header is always executed. */
967 reset_bb_predicate (loop
->header
);
968 gcc_assert (bb_predicate_gimplified_stmts (loop
->header
) == NULL
969 && bb_predicate_gimplified_stmts (loop
->latch
) == NULL
);
974 /* Return true when LOOP is if-convertible. This is a helper function
975 for if_convertible_loop_p. REFS and DDRS are initialized and freed
976 in if_convertible_loop_p. */
979 if_convertible_loop_p_1 (struct loop
*loop
,
980 VEC (data_reference_p
, heap
) **refs
,
981 VEC (ddr_p
, heap
) **ddrs
)
985 basic_block exit_bb
= NULL
;
987 /* Don't if-convert the loop when the data dependences cannot be
988 computed: the loop won't be vectorized in that case. */
989 res
= compute_data_dependences_for_loop (loop
, true, refs
, ddrs
);
993 calculate_dominance_info (CDI_DOMINATORS
);
995 /* Allow statements that can be handled during if-conversion. */
996 ifc_bbs
= get_loop_body_in_if_conv_order (loop
);
999 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1000 fprintf (dump_file
, "Irreducible loop\n");
1004 for (i
= 0; i
< loop
->num_nodes
; i
++)
1006 basic_block bb
= ifc_bbs
[i
];
1008 if (!if_convertible_bb_p (loop
, bb
, exit_bb
))
1011 if (bb_with_exit_edge_p (loop
, bb
))
1015 res
= predicate_bbs (loop
);
1019 if (flag_tree_loop_if_convert_stores
)
1021 data_reference_p dr
;
1023 for (i
= 0; VEC_iterate (data_reference_p
, *refs
, i
, dr
); i
++)
1025 dr
->aux
= XNEW (struct ifc_dr
);
1026 DR_WRITTEN_AT_LEAST_ONCE (dr
) = -1;
1027 DR_RW_UNCONDITIONALLY (dr
) = -1;
1031 for (i
= 0; i
< loop
->num_nodes
; i
++)
1033 basic_block bb
= ifc_bbs
[i
];
1034 gimple_stmt_iterator itr
;
1036 for (itr
= gsi_start_phis (bb
); !gsi_end_p (itr
); gsi_next (&itr
))
1037 if (!if_convertible_phi_p (loop
, bb
, gsi_stmt (itr
)))
1040 /* Check the if-convertibility of statements in predicated BBs. */
1041 if (is_predicated (bb
))
1042 for (itr
= gsi_start_bb (bb
); !gsi_end_p (itr
); gsi_next (&itr
))
1043 if (!if_convertible_stmt_p (gsi_stmt (itr
), *refs
))
1048 fprintf (dump_file
, "Applying if-conversion\n");
1053 /* Return true when LOOP is if-convertible.
1054 LOOP is if-convertible if:
1056 - it has two or more basic blocks,
1057 - it has only one exit,
1058 - loop header is not the exit edge,
1059 - if its basic blocks and phi nodes are if convertible. */
1062 if_convertible_loop_p (struct loop
*loop
)
1067 VEC (data_reference_p
, heap
) *refs
;
1068 VEC (ddr_p
, heap
) *ddrs
;
1070 /* Handle only innermost loop. */
1071 if (!loop
|| loop
->inner
)
1073 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1074 fprintf (dump_file
, "not innermost loop\n");
1078 /* If only one block, no need for if-conversion. */
1079 if (loop
->num_nodes
<= 2)
1081 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1082 fprintf (dump_file
, "less than 2 basic blocks\n");
1086 /* More than one loop exit is too much to handle. */
1087 if (!single_exit (loop
))
1089 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1090 fprintf (dump_file
, "multiple exits\n");
1094 /* If one of the loop header's edge is an exit edge then do not
1095 apply if-conversion. */
1096 FOR_EACH_EDGE (e
, ei
, loop
->header
->succs
)
1097 if (loop_exit_edge_p (loop
, e
))
1100 refs
= VEC_alloc (data_reference_p
, heap
, 5);
1101 ddrs
= VEC_alloc (ddr_p
, heap
, 25);
1102 res
= if_convertible_loop_p_1 (loop
, &refs
, &ddrs
);
1104 if (flag_tree_loop_if_convert_stores
)
1106 data_reference_p dr
;
1109 for (i
= 0; VEC_iterate (data_reference_p
, refs
, i
, dr
); i
++)
1113 free_data_refs (refs
);
1114 free_dependence_relations (ddrs
);
1118 /* Basic block BB has two predecessors. Using predecessor's bb
1119 predicate, set an appropriate condition COND for the PHI node
1120 replacement. Return the true block whose phi arguments are
1121 selected when cond is true. LOOP is the loop containing the
1122 if-converted region, GSI is the place to insert the code for the
1126 find_phi_replacement_condition (struct loop
*loop
,
1127 basic_block bb
, tree
*cond
,
1128 gimple_stmt_iterator
*gsi
)
1130 edge first_edge
, second_edge
;
1133 gcc_assert (EDGE_COUNT (bb
->preds
) == 2);
1134 first_edge
= EDGE_PRED (bb
, 0);
1135 second_edge
= EDGE_PRED (bb
, 1);
1137 /* Use condition based on following criteria:
1143 S2 is preferred over S1. Make 'b' first_bb and use its condition.
1145 2) Do not make loop header first_bb.
1148 S1: x = !(c == d)? a : b;
1151 S22: x = t1 ? b : a;
1153 S3: x = (c == d) ? b : a;
1155 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
1158 4) If pred B is dominated by pred A then use pred B's condition.
1161 /* Select condition that is not TRUTH_NOT_EXPR. */
1162 tmp_cond
= bb_predicate (first_edge
->src
);
1163 gcc_assert (tmp_cond
);
1165 if (TREE_CODE (tmp_cond
) == TRUTH_NOT_EXPR
)
1169 tmp_edge
= first_edge
;
1170 first_edge
= second_edge
;
1171 second_edge
= tmp_edge
;
1174 /* Check if FIRST_BB is loop header or not and make sure that
1175 FIRST_BB does not dominate SECOND_BB. */
1176 if (first_edge
->src
== loop
->header
1177 || dominated_by_p (CDI_DOMINATORS
,
1178 second_edge
->src
, first_edge
->src
))
1180 *cond
= bb_predicate (second_edge
->src
);
1182 if (TREE_CODE (*cond
) == TRUTH_NOT_EXPR
)
1183 *cond
= invert_truthvalue (*cond
);
1185 /* Select non loop header bb. */
1186 first_edge
= second_edge
;
1189 *cond
= bb_predicate (first_edge
->src
);
1191 /* Gimplify the condition: the vectorizer prefers to have gimple
1192 values as conditions. Various targets use different means to
1193 communicate conditions in vector compare operations. Using a
1194 gimple value allows the compiler to emit vector compare and
1195 select RTL without exposing compare's result. */
1196 *cond
= force_gimple_operand_gsi (gsi
, unshare_expr (*cond
),
1198 true, GSI_SAME_STMT
);
1199 if (!is_gimple_reg (*cond
) && !is_gimple_condexpr (*cond
))
1200 *cond
= ifc_temp_var (TREE_TYPE (*cond
), unshare_expr (*cond
), gsi
);
1204 return first_edge
->src
;
1207 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1208 This routine does not handle PHI nodes with more than two
1212 S1: A = PHI <x1(1), x2(5)
1214 S2: A = cond ? x1 : x2;
1216 The generated code is inserted at GSI that points to the top of
1217 basic block's statement list. When COND is true, phi arg from
1218 TRUE_BB is selected. */
1221 predicate_scalar_phi (gimple phi
, tree cond
,
1222 basic_block true_bb
,
1223 gimple_stmt_iterator
*gsi
)
1227 tree rhs
, res
, arg
, scev
;
1229 gcc_assert (gimple_code (phi
) == GIMPLE_PHI
1230 && gimple_phi_num_args (phi
) == 2);
1232 res
= gimple_phi_result (phi
);
1233 /* Do not handle virtual phi nodes. */
1234 if (!is_gimple_reg (SSA_NAME_VAR (res
)))
1237 bb
= gimple_bb (phi
);
1239 if ((arg
= degenerate_phi_result (phi
))
1240 || ((scev
= analyze_scalar_evolution (gimple_bb (phi
)->loop_father
,
1242 && !chrec_contains_undetermined (scev
)
1244 && (arg
= gimple_phi_arg_def (phi
, 0))))
1249 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1250 if (EDGE_PRED (bb
, 1)->src
== true_bb
)
1252 arg_0
= gimple_phi_arg_def (phi
, 1);
1253 arg_1
= gimple_phi_arg_def (phi
, 0);
1257 arg_0
= gimple_phi_arg_def (phi
, 0);
1258 arg_1
= gimple_phi_arg_def (phi
, 1);
1261 /* Build new RHS using selected condition and arguments. */
1262 rhs
= build3 (COND_EXPR
, TREE_TYPE (res
),
1263 unshare_expr (cond
), arg_0
, arg_1
);
1266 new_stmt
= gimple_build_assign (res
, rhs
);
1267 SSA_NAME_DEF_STMT (gimple_phi_result (phi
)) = new_stmt
;
1268 gsi_insert_before (gsi
, new_stmt
, GSI_SAME_STMT
);
1269 update_stmt (new_stmt
);
1271 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1273 fprintf (dump_file
, "new phi replacement stmt\n");
1274 print_gimple_stmt (dump_file
, new_stmt
, 0, TDF_SLIM
);
1278 /* Replaces in LOOP all the scalar phi nodes other than those in the
1279 LOOP->header block with conditional modify expressions. */
1282 predicate_all_scalar_phis (struct loop
*loop
)
1285 unsigned int orig_loop_num_nodes
= loop
->num_nodes
;
1288 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
1291 tree cond
= NULL_TREE
;
1292 gimple_stmt_iterator gsi
, phi_gsi
;
1293 basic_block true_bb
= NULL
;
1296 if (bb
== loop
->header
)
1299 phi_gsi
= gsi_start_phis (bb
);
1300 if (gsi_end_p (phi_gsi
))
1303 /* BB has two predecessors. Using predecessor's aux field, set
1304 appropriate condition for the PHI node replacement. */
1305 gsi
= gsi_after_labels (bb
);
1306 true_bb
= find_phi_replacement_condition (loop
, bb
, &cond
, &gsi
);
1308 while (!gsi_end_p (phi_gsi
))
1310 phi
= gsi_stmt (phi_gsi
);
1311 predicate_scalar_phi (phi
, cond
, true_bb
, &gsi
);
1312 release_phi_node (phi
);
1313 gsi_next (&phi_gsi
);
1316 set_phi_nodes (bb
, NULL
);
1320 /* Insert in each basic block of LOOP the statements produced by the
1321 gimplification of the predicates. */
1324 insert_gimplified_predicates (loop_p loop
)
1328 for (i
= 0; i
< loop
->num_nodes
; i
++)
1330 basic_block bb
= ifc_bbs
[i
];
1333 if (!is_predicated (bb
))
1335 /* Do not insert statements for a basic block that is not
1336 predicated. Also make sure that the predicate of the
1337 basic block is set to true. */
1338 reset_bb_predicate (bb
);
1342 stmts
= bb_predicate_gimplified_stmts (bb
);
1345 if (flag_tree_loop_if_convert_stores
)
1347 /* Insert the predicate of the BB just after the label,
1348 as the if-conversion of memory writes will use this
1350 gimple_stmt_iterator gsi
= gsi_after_labels (bb
);
1351 gsi_insert_seq_before (&gsi
, stmts
, GSI_SAME_STMT
);
1355 /* Insert the predicate of the BB at the end of the BB
1356 as this would reduce the register pressure: the only
1357 use of this predicate will be in successor BBs. */
1358 gimple_stmt_iterator gsi
= gsi_last_bb (bb
);
1361 || stmt_ends_bb_p (gsi_stmt (gsi
)))
1362 gsi_insert_seq_before (&gsi
, stmts
, GSI_SAME_STMT
);
1364 gsi_insert_seq_after (&gsi
, stmts
, GSI_SAME_STMT
);
1367 /* Once the sequence is code generated, set it to NULL. */
1368 set_bb_predicate_gimplified_stmts (bb
, NULL
);
1373 /* Predicate each write to memory in LOOP.
1375 This function transforms control flow constructs containing memory
1378 | for (i = 0; i < N; i++)
1382 into the following form that does not contain control flow:
1384 | for (i = 0; i < N; i++)
1385 | A[i] = cond ? expr : A[i];
1387 The original CFG looks like this:
1394 | if (i < N) goto bb_5 else goto bb_2
1398 | cond = some_computation;
1399 | if (cond) goto bb_3 else goto bb_4
1411 insert_gimplified_predicates inserts the computation of the COND
1412 expression at the beginning of the destination basic block:
1419 | if (i < N) goto bb_5 else goto bb_2
1423 | cond = some_computation;
1424 | if (cond) goto bb_3 else goto bb_4
1428 | cond = some_computation;
1437 predicate_mem_writes is then predicating the memory write as follows:
1444 | if (i < N) goto bb_5 else goto bb_2
1448 | if (cond) goto bb_3 else goto bb_4
1452 | cond = some_computation;
1453 | A[i] = cond ? expr : A[i];
1461 and finally combine_blocks removes the basic block boundaries making
1462 the loop vectorizable:
1466 | if (i < N) goto bb_5 else goto bb_1
1470 | cond = some_computation;
1471 | A[i] = cond ? expr : A[i];
1472 | if (i < N) goto bb_5 else goto bb_4
1481 predicate_mem_writes (loop_p loop
)
1483 unsigned int i
, orig_loop_num_nodes
= loop
->num_nodes
;
1485 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
1487 gimple_stmt_iterator gsi
;
1488 basic_block bb
= ifc_bbs
[i
];
1489 tree cond
= bb_predicate (bb
);
1492 if (is_true_predicate (cond
))
1495 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1496 if ((stmt
= gsi_stmt (gsi
))
1497 && gimple_assign_single_p (stmt
)
1498 && gimple_vdef (stmt
))
1500 tree lhs
= gimple_assign_lhs (stmt
);
1501 tree rhs
= gimple_assign_rhs1 (stmt
);
1502 tree type
= TREE_TYPE (lhs
);
1504 lhs
= ifc_temp_var (type
, unshare_expr (lhs
), &gsi
);
1505 rhs
= ifc_temp_var (type
, unshare_expr (rhs
), &gsi
);
1506 rhs
= build3 (COND_EXPR
, type
, unshare_expr (cond
), rhs
, lhs
);
1507 gimple_assign_set_rhs1 (stmt
, ifc_temp_var (type
, rhs
, &gsi
));
1513 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1514 other than the exit and latch of the LOOP. Also resets the
1515 GIMPLE_DEBUG information. */
1518 remove_conditions_and_labels (loop_p loop
)
1520 gimple_stmt_iterator gsi
;
1523 for (i
= 0; i
< loop
->num_nodes
; i
++)
1525 basic_block bb
= ifc_bbs
[i
];
1527 if (bb_with_exit_edge_p (loop
, bb
)
1528 || bb
== loop
->latch
)
1531 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
1532 switch (gimple_code (gsi_stmt (gsi
)))
1536 gsi_remove (&gsi
, true);
1540 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1541 if (gimple_debug_bind_p (gsi_stmt (gsi
)))
1543 gimple_debug_bind_reset_value (gsi_stmt (gsi
));
1544 update_stmt (gsi_stmt (gsi
));
1555 /* Combine all the basic blocks from LOOP into one or two super basic
1556 blocks. Replace PHI nodes with conditional modify expressions. */
1559 combine_blocks (struct loop
*loop
)
1561 basic_block bb
, exit_bb
, merge_target_bb
;
1562 unsigned int orig_loop_num_nodes
= loop
->num_nodes
;
1567 remove_conditions_and_labels (loop
);
1568 insert_gimplified_predicates (loop
);
1569 predicate_all_scalar_phis (loop
);
1571 if (flag_tree_loop_if_convert_stores
)
1572 predicate_mem_writes (loop
);
1574 /* Merge basic blocks: first remove all the edges in the loop,
1575 except for those from the exit block. */
1577 for (i
= 0; i
< orig_loop_num_nodes
; i
++)
1580 if (bb_with_exit_edge_p (loop
, bb
))
1586 gcc_assert (exit_bb
!= loop
->latch
);
1588 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
1592 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
));)
1594 if (e
->src
== exit_bb
)
1601 if (exit_bb
!= NULL
)
1603 if (exit_bb
!= loop
->header
)
1605 /* Connect this node to loop header. */
1606 make_edge (loop
->header
, exit_bb
, EDGE_FALLTHRU
);
1607 set_immediate_dominator (CDI_DOMINATORS
, exit_bb
, loop
->header
);
1610 /* Redirect non-exit edges to loop->latch. */
1611 FOR_EACH_EDGE (e
, ei
, exit_bb
->succs
)
1613 if (!loop_exit_edge_p (loop
, e
))
1614 redirect_edge_and_branch (e
, loop
->latch
);
1616 set_immediate_dominator (CDI_DOMINATORS
, loop
->latch
, exit_bb
);
1620 /* If the loop does not have an exit, reconnect header and latch. */
1621 make_edge (loop
->header
, loop
->latch
, EDGE_FALLTHRU
);
1622 set_immediate_dominator (CDI_DOMINATORS
, loop
->latch
, loop
->header
);
1625 merge_target_bb
= loop
->header
;
1626 for (i
= 1; i
< orig_loop_num_nodes
; i
++)
1628 gimple_stmt_iterator gsi
;
1629 gimple_stmt_iterator last
;
1633 if (bb
== exit_bb
|| bb
== loop
->latch
)
1636 /* Make stmts member of loop->header. */
1637 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1638 gimple_set_bb (gsi_stmt (gsi
), merge_target_bb
);
1640 /* Update stmt list. */
1641 last
= gsi_last_bb (merge_target_bb
);
1642 gsi_insert_seq_after (&last
, bb_seq (bb
), GSI_NEW_STMT
);
1643 set_bb_seq (bb
, NULL
);
1645 delete_basic_block (bb
);
1648 /* If possible, merge loop header to the block with the exit edge.
1649 This reduces the number of basic blocks to two, to please the
1650 vectorizer that handles only loops with two nodes. */
1652 && exit_bb
!= loop
->header
1653 && can_merge_blocks_p (loop
->header
, exit_bb
))
1654 merge_blocks (loop
->header
, exit_bb
);
1657 /* If-convert LOOP when it is legal. For the moment this pass has no
1658 profitability analysis. Returns true when something changed. */
1661 tree_if_conversion (struct loop
*loop
)
1663 bool changed
= false;
1666 if (!if_convertible_loop_p (loop
)
1667 || !dbg_cnt (if_conversion_tree
))
1670 /* Now all statements are if-convertible. Combine all the basic
1671 blocks into one huge basic block doing the if-conversion
1673 combine_blocks (loop
);
1675 if (flag_tree_loop_if_convert_stores
)
1676 mark_sym_for_renaming (gimple_vop (cfun
));
1685 for (i
= 0; i
< loop
->num_nodes
; i
++)
1686 free_bb_predicate (ifc_bbs
[i
]);
1695 /* Tree if-conversion pass management. */
1698 main_tree_if_conversion (void)
1702 bool changed
= false;
1705 if (number_of_loops () <= 1)
1708 FOR_EACH_LOOP (li
, loop
, 0)
1709 changed
|= tree_if_conversion (loop
);
1712 todo
|= TODO_cleanup_cfg
;
1714 if (changed
&& flag_tree_loop_if_convert_stores
)
1715 todo
|= TODO_update_ssa_only_virtuals
;
1720 /* Returns true when the if-conversion pass is enabled. */
1723 gate_tree_if_conversion (void)
1725 return ((flag_tree_vectorize
&& flag_tree_loop_if_convert
!= 0)
1726 || flag_tree_loop_if_convert
== 1
1727 || flag_tree_loop_if_convert_stores
== 1);
1730 struct gimple_opt_pass pass_if_conversion
=
1735 gate_tree_if_conversion
, /* gate */
1736 main_tree_if_conversion
, /* execute */
1739 0, /* static_pass_number */
1740 TV_NONE
, /* tv_id */
1741 PROP_cfg
| PROP_ssa
, /* properties_required */
1742 0, /* properties_provided */
1743 0, /* properties_destroyed */
1744 0, /* todo_flags_start */
1745 TODO_dump_func
| TODO_verify_stmts
| TODO_verify_flow
1746 /* todo_flags_finish */