Merged trunk at revision 161680 into branch.
[official-gcc.git] / gcc / tree-if-conv.c
blob8d5d2268ec55a6eb74121feaa32106caf792693a
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
11 version.
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
16 for more details.
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
24 conditions.
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'
32 predicate list.
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:
41 INPUT
42 -----
44 # i_23 = PHI <0(0), i_18(10)>;
45 <L0>:;
46 j_15 = A[i_23];
47 if (j_15 > 41) goto <L1>; else goto <L17>;
49 <L17>:;
50 goto <bb 3> (<L3>);
52 <L1>:;
54 # iftmp.2_4 = PHI <0(8), 42(2)>;
55 <L3>:;
56 A[i_23] = iftmp.2_4;
57 i_18 = i_23 + 1;
58 if (i_18 <= 15) goto <L19>; else goto <L18>;
60 <L19>:;
61 goto <bb 1> (<L0>);
63 <L18>:;
65 OUTPUT
66 ------
68 # i_23 = PHI <0(0), i_18(10)>;
69 <L0>:;
70 j_15 = A[i_23];
72 <L3>:;
73 iftmp.2_4 = j_15 > 41 ? 42 : 0;
74 A[i_23] = iftmp.2_4;
75 i_18 = i_23 + 1;
76 if (i_18 <= 15) goto <L19>; else goto <L18>;
78 <L19>:;
79 goto <bb 1> (<L0>);
81 <L18>:;
84 #include "config.h"
85 #include "system.h"
86 #include "coretypes.h"
87 #include "tm.h"
88 #include "tree.h"
89 #include "flags.h"
90 #include "timevar.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"
96 #include "cfgloop.h"
97 #include "tree-chrec.h"
98 #include "tree-data-ref.h"
99 #include "tree-scalar-evolution.h"
100 #include "tree-pass.h"
101 #include "dbgcnt.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. */
111 tree predicate;
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;
117 } *bb_predicate_p;
119 /* Returns true when the basic block BB has a predicate. */
121 static inline bool
122 bb_has_predicate (basic_block bb)
124 return bb->aux != NULL;
127 /* Returns the gimplified predicate for basic block BB. */
129 static inline tree
130 bb_predicate (basic_block bb)
132 return ((bb_predicate_p) bb->aux)->predicate;
135 /* Sets the gimplified predicate COND for basic block BB. */
137 static inline void
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. */
155 static inline void
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. */
164 static inline void
165 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
167 gimple_seq_add_seq
168 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
171 /* Initializes to TRUE the predicate of basic block BB. */
173 static inline void
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. */
183 static inline void
184 free_bb_predicate (basic_block bb)
186 gimple_seq stmts;
188 if (!bb_has_predicate (bb))
189 return;
191 /* Release the SSA_NAMEs created for the gimplification of the
192 predicate. */
193 stmts = bb_predicate_gimplified_stmts (bb);
194 if (stmts)
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));
202 free (bb->aux);
203 bb->aux = NULL;
206 /* Free the predicate of BB and reinitialize it with the true
207 predicate. */
209 static inline void
210 reset_bb_predicate (basic_block bb)
212 free_bb_predicate (bb);
213 init_bb_predicate (bb);
216 /* Create a new temp variable of type TYPE. Add GIMPLE_ASSIGN to assign EXP
217 to the new variable. */
219 static gimple
220 ifc_temp_var (tree type, tree exp)
222 const char *name = "_ifc_";
223 tree var, new_name;
224 gimple stmt;
226 /* Create new temporary variable. */
227 var = create_tmp_var (type, name);
228 add_referenced_var (var);
230 /* Build new statement to assign EXP to new variable. */
231 stmt = gimple_build_assign (var, exp);
233 /* Get SSA name for the new variable and set make new statement
234 its definition statement. */
235 new_name = make_ssa_name (var, stmt);
236 gimple_assign_set_lhs (stmt, new_name);
237 SSA_NAME_DEF_STMT (new_name) = stmt;
238 update_stmt (stmt);
240 return stmt;
243 /* Return true when COND is a true predicate. */
245 static inline bool
246 is_true_predicate (tree cond)
248 return (cond == NULL_TREE
249 || cond == boolean_true_node
250 || integer_onep (cond));
253 /* Returns true when BB has a predicate that is not trivial: true or
254 NULL_TREE. */
256 static inline bool
257 is_predicated (basic_block bb)
259 return !is_true_predicate (bb_predicate (bb));
262 /* Add condition NEW_COND to the predicate list of basic block BB. */
264 static inline void
265 add_to_predicate_list (basic_block bb, tree new_cond)
267 tree cond = bb_predicate (bb);
269 set_bb_predicate (bb, is_true_predicate (cond) ? new_cond :
270 fold_build2_loc (EXPR_LOCATION (cond),
271 TRUTH_OR_EXPR, boolean_type_node,
272 cond, new_cond));
275 /* Add the condition COND to the previous condition PREV_COND, and add
276 this to the predicate list of the destination of edge E. LOOP is
277 the loop to be if-converted. */
279 static void
280 add_to_dst_predicate_list (struct loop *loop, edge e,
281 tree prev_cond, tree cond)
283 if (!flow_bb_inside_loop_p (loop, e->dest))
284 return;
286 if (!is_true_predicate (prev_cond))
287 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
288 prev_cond, cond);
290 add_to_predicate_list (e->dest, cond);
293 /* Return true if one of the successor edges of BB exits LOOP. */
295 static bool
296 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
298 edge e;
299 edge_iterator ei;
301 FOR_EACH_EDGE (e, ei, bb->succs)
302 if (loop_exit_edge_p (loop, e))
303 return true;
305 return false;
308 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
309 and it belongs to basic block BB.
311 PHI is not if-convertible if:
312 - it has more than 2 arguments,
313 - virtual PHI is immediately used in another PHI node,
314 - virtual PHI on BB other than header. */
316 static bool
317 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
319 if (dump_file && (dump_flags & TDF_DETAILS))
321 fprintf (dump_file, "-------------------------\n");
322 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
325 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
327 if (dump_file && (dump_flags & TDF_DETAILS))
328 fprintf (dump_file, "More than two phi node args.\n");
329 return false;
332 if (!is_gimple_reg (SSA_NAME_VAR (gimple_phi_result (phi))))
334 imm_use_iterator imm_iter;
335 use_operand_p use_p;
337 if (bb != loop->header)
339 if (dump_file && (dump_flags & TDF_DETAILS))
340 fprintf (dump_file, "Virtual phi not on loop header.\n");
341 return false;
343 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
345 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
347 if (dump_file && (dump_flags & TDF_DETAILS))
348 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
349 return false;
354 return true;
357 /* Return true when STMT is if-convertible.
359 GIMPLE_ASSIGN statement is not if-convertible if,
360 - it is not movable,
361 - it could trap,
362 - LHS is not var decl.
364 GIMPLE_ASSIGN is part of block BB, which is inside loop LOOP. */
366 static bool
367 if_convertible_gimple_assign_stmt_p (struct loop *loop, basic_block bb,
368 gimple stmt)
370 tree lhs = gimple_assign_lhs (stmt);
372 if (dump_file && (dump_flags & TDF_DETAILS))
374 fprintf (dump_file, "-------------------------\n");
375 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
378 /* Some of these constrains might be too conservative. */
379 if (stmt_ends_bb_p (stmt)
380 || gimple_has_volatile_ops (stmt)
381 || (TREE_CODE (lhs) == SSA_NAME
382 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
383 || gimple_has_side_effects (stmt))
385 if (dump_file && (dump_flags & TDF_DETAILS))
386 fprintf (dump_file, "stmt not suitable for ifcvt\n");
387 return false;
390 if (gimple_assign_rhs_could_trap_p (stmt))
392 if (dump_file && (dump_flags & TDF_DETAILS))
393 fprintf (dump_file, "tree could trap...\n");
394 return false;
397 if (TREE_CODE (lhs) != SSA_NAME
398 && bb != loop->header
399 && !bb_with_exit_edge_p (loop, bb))
401 if (dump_file && (dump_flags & TDF_DETAILS))
403 fprintf (dump_file, "LHS is not var\n");
404 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
406 return false;
409 return true;
412 /* Return true when STMT is if-convertible.
414 A statement is if-convertible if:
415 - it is an if-convertible GIMPLE_ASSGIN,
416 - it is a GIMPLE_LABEL or a GIMPLE_COND.
418 STMT is inside BB, which is inside loop LOOP. */
420 static bool
421 if_convertible_stmt_p (struct loop *loop, basic_block bb, gimple stmt)
423 switch (gimple_code (stmt))
425 case GIMPLE_LABEL:
426 case GIMPLE_DEBUG:
427 case GIMPLE_COND:
428 return true;
430 case GIMPLE_ASSIGN:
431 return if_convertible_gimple_assign_stmt_p (loop, bb, stmt);
433 default:
434 /* Don't know what to do with 'em so don't do anything. */
435 if (dump_file && (dump_flags & TDF_DETAILS))
437 fprintf (dump_file, "don't know what to do\n");
438 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
440 return false;
441 break;
444 return true;
447 /* Return true when BB is if-convertible. This routine does not check
448 basic block's statements and phis.
450 A basic block is not if-convertible if:
451 - it is non-empty and it is after the exit block (in BFS order),
452 - it is after the exit block but before the latch,
453 - its edges are not normal.
455 EXIT_BB is the basic block containing the exit of the LOOP. BB is
456 inside LOOP. */
458 static bool
459 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
461 edge e;
462 edge_iterator ei;
464 if (dump_file && (dump_flags & TDF_DETAILS))
465 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
467 if (EDGE_COUNT (bb->preds) > 2
468 || EDGE_COUNT (bb->succs) > 2)
469 return false;
471 if (exit_bb)
473 if (bb != loop->latch)
475 if (dump_file && (dump_flags & TDF_DETAILS))
476 fprintf (dump_file, "basic block after exit bb but before latch\n");
477 return false;
479 else if (!empty_block_p (bb))
481 if (dump_file && (dump_flags & TDF_DETAILS))
482 fprintf (dump_file, "non empty basic block after exit bb\n");
483 return false;
485 else if (bb == loop->latch
486 && bb != exit_bb
487 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
489 if (dump_file && (dump_flags & TDF_DETAILS))
490 fprintf (dump_file, "latch is not dominated by exit_block\n");
491 return false;
495 /* Be less adventurous and handle only normal edges. */
496 FOR_EACH_EDGE (e, ei, bb->succs)
497 if (e->flags &
498 (EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
500 if (dump_file && (dump_flags & TDF_DETAILS))
501 fprintf (dump_file, "Difficult to handle edges\n");
502 return false;
505 return true;
508 /* Return true when all predecessor blocks of BB are visited. The
509 VISITED bitmap keeps track of the visited blocks. */
511 static bool
512 pred_blocks_visited_p (basic_block bb, bitmap *visited)
514 edge e;
515 edge_iterator ei;
516 FOR_EACH_EDGE (e, ei, bb->preds)
517 if (!bitmap_bit_p (*visited, e->src->index))
518 return false;
520 return true;
523 /* Get body of a LOOP in suitable order for if-conversion. It is
524 caller's responsibility to deallocate basic block list.
525 If-conversion suitable order is, breadth first sort (BFS) order
526 with an additional constraint: select a block only if all its
527 predecessors are already selected. */
529 static basic_block *
530 get_loop_body_in_if_conv_order (const struct loop *loop)
532 basic_block *blocks, *blocks_in_bfs_order;
533 basic_block bb;
534 bitmap visited;
535 unsigned int index = 0;
536 unsigned int visited_count = 0;
538 gcc_assert (loop->num_nodes);
539 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
541 blocks = XCNEWVEC (basic_block, loop->num_nodes);
542 visited = BITMAP_ALLOC (NULL);
544 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
546 index = 0;
547 while (index < loop->num_nodes)
549 bb = blocks_in_bfs_order [index];
551 if (bb->flags & BB_IRREDUCIBLE_LOOP)
553 free (blocks_in_bfs_order);
554 BITMAP_FREE (visited);
555 free (blocks);
556 return NULL;
559 if (!bitmap_bit_p (visited, bb->index))
561 if (pred_blocks_visited_p (bb, &visited)
562 || bb == loop->header)
564 /* This block is now visited. */
565 bitmap_set_bit (visited, bb->index);
566 blocks[visited_count++] = bb;
570 index++;
572 if (index == loop->num_nodes
573 && visited_count != loop->num_nodes)
574 /* Not done yet. */
575 index = 0;
577 free (blocks_in_bfs_order);
578 BITMAP_FREE (visited);
579 return blocks;
582 /* Returns true when the analysis of the predicates for all the basic
583 blocks in LOOP succeeded.
585 predicate_bbs first allocates the predicates of the basic blocks.
586 These fields are then initialized with the tree expressions
587 representing the predicates under which a basic block is executed
588 in the LOOP. As the loop->header is executed at each iteration, it
589 has the "true" predicate. Other statements executed under a
590 condition are predicated with that condition, for example
592 | if (x)
593 | S1;
594 | else
595 | S2;
597 S1 will be predicated with "x", and
598 S2 will be predicated with "!x". */
600 static bool
601 predicate_bbs (loop_p loop)
603 unsigned int i;
605 for (i = 0; i < loop->num_nodes; i++)
606 init_bb_predicate (ifc_bbs[i]);
608 for (i = 0; i < loop->num_nodes; i++)
610 basic_block bb = ifc_bbs[i];
611 tree cond;
612 gimple_stmt_iterator itr;
614 /* The loop latch is always executed and has no extra conditions
615 to be processed: skip it. */
616 if (bb == loop->latch)
618 reset_bb_predicate (loop->latch);
619 continue;
622 cond = bb_predicate (bb);
623 if (cond
624 && bb != loop->header)
626 gimple_seq stmts;
628 cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
629 add_bb_predicate_gimplified_stmts (bb, stmts);
632 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
634 gimple stmt = gsi_stmt (itr);
636 switch (gimple_code (stmt))
638 case GIMPLE_LABEL:
639 case GIMPLE_ASSIGN:
640 case GIMPLE_CALL:
641 case GIMPLE_DEBUG:
642 break;
644 case GIMPLE_COND:
646 tree c2;
647 edge true_edge, false_edge;
648 location_t loc = gimple_location (stmt);
649 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
650 boolean_type_node,
651 gimple_cond_lhs (stmt),
652 gimple_cond_rhs (stmt));
654 /* Add new condition into destination's predicate list. */
655 extract_true_false_edges_from_block (gimple_bb (stmt),
656 &true_edge, &false_edge);
658 /* If C is true, then TRUE_EDGE is taken. */
659 add_to_dst_predicate_list (loop, true_edge, cond, c);
661 /* If C is false, then FALSE_EDGE is taken. */
662 c2 = invert_truthvalue_loc (loc, unshare_expr (c));
663 add_to_dst_predicate_list (loop, false_edge, cond, c2);
665 cond = NULL_TREE;
666 break;
669 default:
670 /* Not handled yet in if-conversion. */
671 return false;
675 /* If current bb has only one successor, then consider it as an
676 unconditional goto. */
677 if (single_succ_p (bb))
679 basic_block bb_n = single_succ (bb);
681 /* The successor bb inherits the predicate of its
682 predecessor. If there is no predicate in the predecessor
683 bb, then consider the successor bb as always executed. */
684 if (cond == NULL_TREE)
685 cond = boolean_true_node;
687 add_to_predicate_list (bb_n, cond);
691 /* The loop header is always executed. */
692 reset_bb_predicate (loop->header);
693 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
694 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
696 return true;
699 /* Return true when LOOP is if-convertible.
700 LOOP is if-convertible if:
701 - it is innermost,
702 - it has two or more basic blocks,
703 - it has only one exit,
704 - loop header is not the exit edge,
705 - if its basic blocks and phi nodes are if convertible. */
707 static bool
708 if_convertible_loop_p (struct loop *loop)
710 unsigned int i;
711 edge e;
712 edge_iterator ei;
713 basic_block exit_bb = NULL;
715 /* Handle only innermost loop. */
716 if (!loop || loop->inner)
718 if (dump_file && (dump_flags & TDF_DETAILS))
719 fprintf (dump_file, "not innermost loop\n");
720 return false;
723 /* If only one block, no need for if-conversion. */
724 if (loop->num_nodes <= 2)
726 if (dump_file && (dump_flags & TDF_DETAILS))
727 fprintf (dump_file, "less than 2 basic blocks\n");
728 return false;
731 /* More than one loop exit is too much to handle. */
732 if (!single_exit (loop))
734 if (dump_file && (dump_flags & TDF_DETAILS))
735 fprintf (dump_file, "multiple exits\n");
736 return false;
739 /* ??? Check target's vector conditional operation support for vectorizer. */
741 /* If one of the loop header's edge is exit edge then do not apply
742 if-conversion. */
743 FOR_EACH_EDGE (e, ei, loop->header->succs)
745 if (loop_exit_edge_p (loop, e))
746 return false;
749 /* Don't if-convert the loop when the data dependences cannot be
750 computed: the loop won't be vectorized in that case. */
752 VEC (data_reference_p, heap) *refs = VEC_alloc (data_reference_p, heap, 5);
753 VEC (ddr_p, heap) *ddrs = VEC_alloc (ddr_p, heap, 25);
754 bool res = compute_data_dependences_for_loop (loop, true, &refs, &ddrs);
756 free_data_refs (refs);
757 free_dependence_relations (ddrs);
759 if (!res)
760 return false;
763 calculate_dominance_info (CDI_DOMINATORS);
765 /* Allow statements that can be handled during if-conversion. */
766 ifc_bbs = get_loop_body_in_if_conv_order (loop);
767 if (!ifc_bbs)
769 if (dump_file && (dump_flags & TDF_DETAILS))
770 fprintf (dump_file, "Irreducible loop\n");
771 return false;
774 for (i = 0; i < loop->num_nodes; i++)
776 basic_block bb = ifc_bbs[i];
778 if (!if_convertible_bb_p (loop, bb, exit_bb))
779 return false;
781 if (bb_with_exit_edge_p (loop, bb))
782 exit_bb = bb;
785 if (!predicate_bbs (loop))
786 return false;
788 for (i = 0; i < loop->num_nodes; i++)
790 basic_block bb = ifc_bbs[i];
791 gimple_stmt_iterator itr;
793 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
794 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
795 return false;
797 /* For non predicated BBs, don't check their statements. */
798 if (!is_predicated (bb))
799 continue;
801 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
802 if (!if_convertible_stmt_p (loop, bb, gsi_stmt (itr)))
803 return false;
806 if (dump_file)
807 fprintf (dump_file, "Applying if-conversion\n");
809 return true;
812 /* Basic block BB has two predecessors. Using predecessor's bb
813 predicate, set an appropriate condition COND for the PHI node
814 replacement. Return the true block whose phi arguments are
815 selected when cond is true. LOOP is the loop containing the
816 if-converted region, GSI is the place to insert the code for the
817 if-conversion. */
819 static basic_block
820 find_phi_replacement_condition (struct loop *loop,
821 basic_block bb, tree *cond,
822 gimple_stmt_iterator *gsi)
824 edge first_edge, second_edge;
825 tree tmp_cond;
827 gcc_assert (EDGE_COUNT (bb->preds) == 2);
828 first_edge = EDGE_PRED (bb, 0);
829 second_edge = EDGE_PRED (bb, 1);
831 /* Use condition based on following criteria:
833 S1: x = !c ? a : b;
835 S2: x = c ? b : a;
837 S2 is preferred over S1. Make 'b' first_bb and use its condition.
839 2) Do not make loop header first_bb.
842 S1: x = !(c == d)? a : b;
844 S21: t1 = c == d;
845 S22: x = t1 ? b : a;
847 S3: x = (c == d) ? b : a;
849 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
850 its condition.
852 4) If pred B is dominated by pred A then use pred B's condition.
853 See PR23115. */
855 /* Select condition that is not TRUTH_NOT_EXPR. */
856 tmp_cond = bb_predicate (first_edge->src);
857 gcc_assert (tmp_cond);
859 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
861 edge tmp_edge;
863 tmp_edge = first_edge;
864 first_edge = second_edge;
865 second_edge = tmp_edge;
868 /* Check if FIRST_BB is loop header or not and make sure that
869 FIRST_BB does not dominate SECOND_BB. */
870 if (first_edge->src == loop->header
871 || dominated_by_p (CDI_DOMINATORS,
872 second_edge->src, first_edge->src))
874 *cond = bb_predicate (second_edge->src);
876 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
877 *cond = invert_truthvalue (*cond);
878 else
879 /* Select non loop header bb. */
880 first_edge = second_edge;
882 else
883 *cond = bb_predicate (first_edge->src);
885 /* Gimplify the condition: the vectorizer prefers to have gimple
886 values as conditions. Various targets use different means to
887 communicate conditions in vector compare operations. Using a
888 gimple value allows the compiler to emit vector compare and
889 select RTL without exposing compare's result. */
890 *cond = force_gimple_operand_gsi (gsi, unshare_expr (*cond),
891 false, NULL_TREE,
892 true, GSI_SAME_STMT);
893 if (!is_gimple_reg (*cond) && !is_gimple_condexpr (*cond))
895 gimple new_stmt;
897 new_stmt = ifc_temp_var (TREE_TYPE (*cond), unshare_expr (*cond));
898 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
899 *cond = gimple_assign_lhs (new_stmt);
902 gcc_assert (*cond);
904 return first_edge->src;
907 /* Replace PHI node with conditional modify expr using COND. This
908 routine does not handle PHI nodes with more than two arguments.
910 For example,
911 S1: A = PHI <x1(1), x2(5)
912 is converted into,
913 S2: A = cond ? x1 : x2;
915 The generated code is inserted at GSI that points to the top of
916 basic block's statement list. When COND is true, phi arg from
917 TRUE_BB is selected. */
919 static void
920 replace_phi_with_cond_gimple_assign_stmt (gimple phi, tree cond,
921 basic_block true_bb,
922 gimple_stmt_iterator *gsi)
924 gimple new_stmt;
925 basic_block bb;
926 tree rhs;
927 tree arg;
929 gcc_assert (gimple_code (phi) == GIMPLE_PHI
930 && gimple_phi_num_args (phi) == 2);
932 bb = gimple_bb (phi);
934 arg = degenerate_phi_result (phi);
935 if (arg)
936 rhs = arg;
937 else
939 tree arg_0, arg_1;
940 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
941 if (EDGE_PRED (bb, 1)->src == true_bb)
943 arg_0 = gimple_phi_arg_def (phi, 1);
944 arg_1 = gimple_phi_arg_def (phi, 0);
946 else
948 arg_0 = gimple_phi_arg_def (phi, 0);
949 arg_1 = gimple_phi_arg_def (phi, 1);
952 /* Build new RHS using selected condition and arguments. */
953 rhs = build3 (COND_EXPR, TREE_TYPE (PHI_RESULT (phi)),
954 unshare_expr (cond), arg_0, arg_1);
957 new_stmt = gimple_build_assign (PHI_RESULT (phi), rhs);
958 SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
959 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
960 update_stmt (new_stmt);
962 if (dump_file && (dump_flags & TDF_DETAILS))
964 fprintf (dump_file, "new phi replacement stmt\n");
965 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
969 /* Replaces in LOOP all the phi nodes other than those in the
970 LOOP->header block with conditional modify expressions. */
972 static void
973 ifconvert_phi_nodes (struct loop *loop)
975 basic_block bb;
976 unsigned int orig_loop_num_nodes = loop->num_nodes;
977 unsigned int i;
979 for (i = 1; i < orig_loop_num_nodes; i++)
981 gimple phi;
982 tree cond = NULL_TREE;
983 gimple_stmt_iterator gsi, phi_gsi;
984 basic_block true_bb = NULL;
985 bb = ifc_bbs[i];
987 if (bb == loop->header)
988 continue;
990 phi_gsi = gsi_start_phis (bb);
991 if (gsi_end_p (phi_gsi))
992 continue;
994 /* BB has two predecessors. Using predecessor's aux field, set
995 appropriate condition for the PHI node replacement. */
996 gsi = gsi_after_labels (bb);
997 true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
999 while (!gsi_end_p (phi_gsi))
1001 phi = gsi_stmt (phi_gsi);
1002 replace_phi_with_cond_gimple_assign_stmt (phi, cond, true_bb, &gsi);
1003 release_phi_node (phi);
1004 gsi_next (&phi_gsi);
1007 set_phi_nodes (bb, NULL);
1011 /* Insert in each basic block of LOOP the statements produced by the
1012 gimplification of the predicates. */
1014 static void
1015 insert_gimplified_predicates (loop_p loop)
1017 unsigned int i;
1019 for (i = 0; i < loop->num_nodes; i++)
1021 basic_block bb = ifc_bbs[i];
1022 gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
1024 if (!is_predicated (bb))
1026 /* Do not insert statements for a basic block that is not
1027 predicated. Also make sure that the predicate of the
1028 basic block is set to true. */
1029 reset_bb_predicate (bb);
1030 continue;
1033 if (stmts)
1035 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1037 if (gsi_end_p (gsi)
1038 || gimple_code (gsi_stmt (gsi)) == GIMPLE_COND)
1039 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1040 else
1041 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1043 /* Once the sequence is code generated, set it to NULL. */
1044 set_bb_predicate_gimplified_stmts (bb, NULL);
1049 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1050 other than the exit and latch of the LOOP. Also resets the
1051 GIMPLE_DEBUG information. */
1053 static void
1054 remove_conditions_and_labels (loop_p loop)
1056 gimple_stmt_iterator gsi;
1057 unsigned int i;
1059 for (i = 0; i < loop->num_nodes; i++)
1061 basic_block bb = ifc_bbs[i];
1063 if (bb_with_exit_edge_p (loop, bb)
1064 || bb == loop->latch)
1065 continue;
1067 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1068 switch (gimple_code (gsi_stmt (gsi)))
1070 case GIMPLE_COND:
1071 case GIMPLE_LABEL:
1072 gsi_remove (&gsi, true);
1073 break;
1075 case GIMPLE_DEBUG:
1076 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1077 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1079 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1080 update_stmt (gsi_stmt (gsi));
1082 gsi_next (&gsi);
1083 break;
1085 default:
1086 gsi_next (&gsi);
1091 /* Combine all the basic blocks from LOOP into one or two super basic
1092 blocks. Replace PHI nodes with conditional modify expressions. */
1094 static void
1095 combine_blocks (struct loop *loop)
1097 basic_block bb, exit_bb, merge_target_bb;
1098 unsigned int orig_loop_num_nodes = loop->num_nodes;
1099 unsigned int i;
1100 edge e;
1101 edge_iterator ei;
1103 remove_conditions_and_labels (loop);
1104 insert_gimplified_predicates (loop);
1105 ifconvert_phi_nodes (loop);
1107 /* Merge basic blocks: first remove all the edges in the loop,
1108 except for those from the exit block. */
1109 exit_bb = NULL;
1110 for (i = 0; i < orig_loop_num_nodes; i++)
1112 bb = ifc_bbs[i];
1113 if (bb_with_exit_edge_p (loop, bb))
1115 exit_bb = bb;
1116 break;
1119 gcc_assert (exit_bb != loop->latch);
1121 for (i = 1; i < orig_loop_num_nodes; i++)
1123 bb = ifc_bbs[i];
1125 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1127 if (e->src == exit_bb)
1128 ei_next (&ei);
1129 else
1130 remove_edge (e);
1134 if (exit_bb != NULL)
1136 if (exit_bb != loop->header)
1138 /* Connect this node to loop header. */
1139 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1140 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1143 /* Redirect non-exit edges to loop->latch. */
1144 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1146 if (!loop_exit_edge_p (loop, e))
1147 redirect_edge_and_branch (e, loop->latch);
1149 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1151 else
1153 /* If the loop does not have an exit, reconnect header and latch. */
1154 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1155 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1158 merge_target_bb = loop->header;
1159 for (i = 1; i < orig_loop_num_nodes; i++)
1161 gimple_stmt_iterator gsi;
1162 gimple_stmt_iterator last;
1164 bb = ifc_bbs[i];
1166 if (bb == exit_bb || bb == loop->latch)
1167 continue;
1169 /* Make stmts member of loop->header. */
1170 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1171 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1173 /* Update stmt list. */
1174 last = gsi_last_bb (merge_target_bb);
1175 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1176 set_bb_seq (bb, NULL);
1178 delete_basic_block (bb);
1181 /* If possible, merge loop header to the block with the exit edge.
1182 This reduces the number of basic blocks to two, to please the
1183 vectorizer that handles only loops with two nodes. */
1184 if (exit_bb
1185 && exit_bb != loop->header
1186 && can_merge_blocks_p (loop->header, exit_bb))
1187 merge_blocks (loop->header, exit_bb);
1190 /* If-convert LOOP when it is legal. For the moment this pass has no
1191 profitability analysis. Returns true when something changed. */
1193 static bool
1194 tree_if_conversion (struct loop *loop)
1196 bool changed = false;
1197 ifc_bbs = NULL;
1199 if (!if_convertible_loop_p (loop)
1200 || !dbg_cnt (if_conversion_tree))
1201 goto cleanup;
1203 /* Now all statements are if-convertible. Combine all the basic
1204 blocks into one huge basic block doing the if-conversion
1205 on-the-fly. */
1206 combine_blocks (loop);
1207 changed = true;
1209 cleanup:
1210 if (ifc_bbs)
1212 unsigned int i;
1214 for (i = 0; i < loop->num_nodes; i++)
1215 free_bb_predicate (ifc_bbs[i]);
1217 free (ifc_bbs);
1218 ifc_bbs = NULL;
1221 return changed;
1224 /* Tree if-conversion pass management. */
1226 static unsigned int
1227 main_tree_if_conversion (void)
1229 loop_iterator li;
1230 struct loop *loop;
1231 bool changed = false;
1233 if (number_of_loops () <= 1)
1234 return 0;
1236 FOR_EACH_LOOP (li, loop, 0)
1237 changed |= tree_if_conversion (loop);
1239 return changed ? TODO_cleanup_cfg : 0;
1242 static bool
1243 gate_tree_if_conversion (void)
1245 return flag_tree_vectorize != 0;
1248 struct gimple_opt_pass pass_if_conversion =
1251 GIMPLE_PASS,
1252 "ifcvt", /* name */
1253 gate_tree_if_conversion, /* gate */
1254 main_tree_if_conversion, /* execute */
1255 NULL, /* sub */
1256 NULL, /* next */
1257 0, /* static_pass_number */
1258 TV_NONE, /* tv_id */
1259 PROP_cfg | PROP_ssa, /* properties_required */
1260 0, /* properties_provided */
1261 0, /* properties_destroyed */
1262 0, /* todo_flags_start */
1263 TODO_dump_func | TODO_verify_stmts | TODO_verify_flow
1264 /* todo_flags_finish */