Fix PR47271: only if-convert full writes.
[gcc.git] / gcc / tree-if-conv.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
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.
25
26 A short description of if-conversion:
27
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
38
39 Sample transformation:
40
41 INPUT
42 -----
43
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>;
48
49 <L17>:;
50 goto <bb 3> (<L3>);
51
52 <L1>:;
53
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>;
59
60 <L19>:;
61 goto <bb 1> (<L0>);
62
63 <L18>:;
64
65 OUTPUT
66 ------
67
68 # i_23 = PHI <0(0), i_18(10)>;
69 <L0>:;
70 j_15 = A[i_23];
71
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>;
77
78 <L19>:;
79 goto <bb 1> (<L0>);
80
81 <L18>:;
82 */
83
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"
102
103 /* List of basic blocks in if-conversion-suitable order. */
104 static basic_block *ifc_bbs;
105
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 {
109
110 /* The condition under which this basic block is executed. */
111 tree predicate;
112
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;
118
119 /* Returns true when the basic block BB has a predicate. */
120
121 static inline bool
122 bb_has_predicate (basic_block bb)
123 {
124 return bb->aux != NULL;
125 }
126
127 /* Returns the gimplified predicate for basic block BB. */
128
129 static inline tree
130 bb_predicate (basic_block bb)
131 {
132 return ((bb_predicate_p) bb->aux)->predicate;
133 }
134
135 /* Sets the gimplified predicate COND for basic block BB. */
136
137 static inline void
138 set_bb_predicate (basic_block bb, tree cond)
139 {
140 ((bb_predicate_p) bb->aux)->predicate = cond;
141 }
142
143 /* Returns the sequence of statements of the gimplification of the
144 predicate for basic block BB. */
145
146 static inline gimple_seq
147 bb_predicate_gimplified_stmts (basic_block bb)
148 {
149 return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
150 }
151
152 /* Sets the sequence of statements STMTS of the gimplification of the
153 predicate for basic block BB. */
154
155 static inline void
156 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
157 {
158 ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
159 }
160
161 /* Adds the sequence of statements STMTS to the sequence of statements
162 of the predicate for basic block BB. */
163
164 static inline void
165 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
166 {
167 gimple_seq_add_seq
168 (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
169 }
170
171 /* Initializes to TRUE the predicate of basic block BB. */
172
173 static inline void
174 init_bb_predicate (basic_block bb)
175 {
176 bb->aux = XNEW (struct bb_predicate_s);
177 set_bb_predicate_gimplified_stmts (bb, NULL);
178 set_bb_predicate (bb, boolean_true_node);
179 }
180
181 /* Free the predicate of basic block BB. */
182
183 static inline void
184 free_bb_predicate (basic_block bb)
185 {
186 gimple_seq stmts;
187
188 if (!bb_has_predicate (bb))
189 return;
190
191 /* Release the SSA_NAMEs created for the gimplification of the
192 predicate. */
193 stmts = bb_predicate_gimplified_stmts (bb);
194 if (stmts)
195 {
196 gimple_stmt_iterator i;
197
198 for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
199 free_stmt_operands (gsi_stmt (i));
200 }
201
202 free (bb->aux);
203 bb->aux = NULL;
204 }
205
206 /* Free the predicate of BB and reinitialize it with the true
207 predicate. */
208
209 static inline void
210 reset_bb_predicate (basic_block bb)
211 {
212 free_bb_predicate (bb);
213 init_bb_predicate (bb);
214 }
215
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
219 statement. */
220
221 static tree
222 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
223 {
224 const char *name = "_ifc_";
225 tree var, new_name;
226 gimple stmt;
227
228 /* Create new temporary variable. */
229 var = create_tmp_var (type, name);
230 add_referenced_var (var);
231
232 /* Build new statement to assign EXPR to new variable. */
233 stmt = gimple_build_assign (var, expr);
234
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;
240 update_stmt (stmt);
241
242 gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
243 return gimple_assign_lhs (stmt);
244 }
245
246 /* Return true when COND is a true predicate. */
247
248 static inline bool
249 is_true_predicate (tree cond)
250 {
251 return (cond == NULL_TREE
252 || cond == boolean_true_node
253 || integer_onep (cond));
254 }
255
256 /* Returns true when BB has a predicate that is not trivial: true or
257 NULL_TREE. */
258
259 static inline bool
260 is_predicated (basic_block bb)
261 {
262 return !is_true_predicate (bb_predicate (bb));
263 }
264
265 /* Parses the predicate COND and returns its comparison code and
266 operands OP0 and OP1. */
267
268 static enum tree_code
269 parse_predicate (tree cond, tree *op0, tree *op1)
270 {
271 gimple s;
272
273 if (TREE_CODE (cond) == SSA_NAME
274 && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
275 {
276 if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
277 {
278 *op0 = gimple_assign_rhs1 (s);
279 *op1 = gimple_assign_rhs2 (s);
280 return gimple_assign_rhs_code (s);
281 }
282
283 else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
284 {
285 tree op = gimple_assign_rhs1 (s);
286 tree type = TREE_TYPE (op);
287 enum tree_code code = parse_predicate (op, op0, op1);
288
289 return code == ERROR_MARK ? ERROR_MARK
290 : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
291 }
292
293 return ERROR_MARK;
294 }
295
296 if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
297 {
298 *op0 = TREE_OPERAND (cond, 0);
299 *op1 = TREE_OPERAND (cond, 1);
300 return TREE_CODE (cond);
301 }
302
303 return ERROR_MARK;
304 }
305
306 /* Returns the fold of predicate C1 OR C2 at location LOC. */
307
308 static tree
309 fold_or_predicates (location_t loc, tree c1, tree c2)
310 {
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);
314
315 if (code1 != ERROR_MARK && code2 != ERROR_MARK)
316 {
317 tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
318 code2, op2a, op2b);
319 if (t)
320 return t;
321 }
322
323 return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
324 }
325
326 /* Add condition NC to the predicate list of basic block BB. */
327
328 static inline void
329 add_to_predicate_list (basic_block bb, tree nc)
330 {
331 tree bc;
332
333 if (is_true_predicate (nc))
334 return;
335
336 if (!is_predicated (bb))
337 bc = nc;
338 else
339 {
340 bc = bb_predicate (bb);
341 bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
342 }
343
344 if (!is_gimple_condexpr (bc))
345 {
346 gimple_seq stmts;
347 bc = force_gimple_operand (bc, &stmts, true, NULL_TREE);
348 add_bb_predicate_gimplified_stmts (bb, stmts);
349 }
350
351 if (is_true_predicate (bc))
352 reset_bb_predicate (bb);
353 else
354 set_bb_predicate (bb, bc);
355 }
356
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. */
360
361 static void
362 add_to_dst_predicate_list (struct loop *loop, edge e,
363 tree prev_cond, tree cond)
364 {
365 if (!flow_bb_inside_loop_p (loop, e->dest))
366 return;
367
368 if (!is_true_predicate (prev_cond))
369 cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
370 prev_cond, cond);
371
372 add_to_predicate_list (e->dest, cond);
373 }
374
375 /* Return true if one of the successor edges of BB exits LOOP. */
376
377 static bool
378 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
379 {
380 edge e;
381 edge_iterator ei;
382
383 FOR_EACH_EDGE (e, ei, bb->succs)
384 if (loop_exit_edge_p (loop, e))
385 return true;
386
387 return false;
388 }
389
390 /* Return true when PHI is if-convertible. PHI is part of loop LOOP
391 and it belongs to basic block BB.
392
393 PHI is not if-convertible if:
394 - it has more than 2 arguments.
395
396 When the flag_tree_loop_if_convert_stores is not set, PHI is not
397 if-convertible if:
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. */
400
401 static bool
402 if_convertible_phi_p (struct loop *loop, basic_block bb, gimple phi)
403 {
404 if (dump_file && (dump_flags & TDF_DETAILS))
405 {
406 fprintf (dump_file, "-------------------------\n");
407 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
408 }
409
410 if (bb != loop->header && gimple_phi_num_args (phi) != 2)
411 {
412 if (dump_file && (dump_flags & TDF_DETAILS))
413 fprintf (dump_file, "More than two phi node args.\n");
414 return false;
415 }
416
417 if (flag_tree_loop_if_convert_stores)
418 return true;
419
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
422 if-converted. */
423 if (!is_gimple_reg (SSA_NAME_VAR (gimple_phi_result (phi))))
424 {
425 imm_use_iterator imm_iter;
426 use_operand_p use_p;
427
428 if (bb != loop->header)
429 {
430 if (dump_file && (dump_flags & TDF_DETAILS))
431 fprintf (dump_file, "Virtual phi not on loop->header.\n");
432 return false;
433 }
434
435 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
436 {
437 if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
438 {
439 if (dump_file && (dump_flags & TDF_DETAILS))
440 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
441 return false;
442 }
443 }
444 }
445
446 return true;
447 }
448
449 /* Records the status of a data reference. This struct is attached to
450 each DR->aux field. */
451
452 struct ifc_dr {
453 /* -1 when not initialized, 0 when false, 1 when true. */
454 int written_at_least_once;
455
456 /* -1 when not initialized, 0 when false, 1 when true. */
457 int rw_unconditionally;
458 };
459
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)
463
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. */
470
471 static bool
472 memrefs_read_or_written_unconditionally (gimple stmt,
473 VEC (data_reference_p, heap) *drs)
474 {
475 int i, j;
476 data_reference_p a, b;
477 tree ca = bb_predicate (gimple_bb (stmt));
478
479 for (i = 0; VEC_iterate (data_reference_p, drs, i, a); i++)
480 if (DR_STMT (a) == stmt)
481 {
482 bool found = false;
483 int x = DR_RW_UNCONDITIONALLY (a);
484
485 if (x == 0)
486 return false;
487
488 if (x == 1)
489 continue;
490
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))
494 {
495 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
496
497 if (DR_RW_UNCONDITIONALLY (b) == 1
498 || is_true_predicate (cb)
499 || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
500 ca, cb)))
501 {
502 DR_RW_UNCONDITIONALLY (a) = 1;
503 DR_RW_UNCONDITIONALLY (b) = 1;
504 found = true;
505 break;
506 }
507 }
508
509 if (!found)
510 {
511 DR_RW_UNCONDITIONALLY (a) = 0;
512 return false;
513 }
514 }
515
516 return true;
517 }
518
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. */
525
526 static bool
527 write_memrefs_written_at_least_once (gimple stmt,
528 VEC (data_reference_p, heap) *drs)
529 {
530 int i, j;
531 data_reference_p a, b;
532 tree ca = bb_predicate (gimple_bb (stmt));
533
534 for (i = 0; VEC_iterate (data_reference_p, drs, i, a); i++)
535 if (DR_STMT (a) == stmt
536 && DR_IS_WRITE (a))
537 {
538 bool found = false;
539 int x = DR_WRITTEN_AT_LEAST_ONCE (a);
540
541 if (x == 0)
542 return false;
543
544 if (x == 1)
545 continue;
546
547 for (j = 0; VEC_iterate (data_reference_p, drs, j, b); j++)
548 if (DR_STMT (b) != stmt
549 && DR_IS_WRITE (b)
550 && same_data_refs_base_objects (a, b))
551 {
552 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
553
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),
557 ca, cb)))
558 {
559 DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
560 DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
561 found = true;
562 break;
563 }
564 }
565
566 if (!found)
567 {
568 DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
569 return false;
570 }
571 }
572
573 return true;
574 }
575
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:
578
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
586 function.
587
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. */
593
594 static bool
595 ifcvt_memrefs_wont_trap (gimple stmt, VEC (data_reference_p, heap) *refs)
596 {
597 return write_memrefs_written_at_least_once (stmt, refs)
598 && memrefs_read_or_written_unconditionally (stmt, refs);
599 }
600
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. */
604
605 static bool
606 ifcvt_could_trap_p (gimple stmt, VEC (data_reference_p, heap) *refs)
607 {
608 if (gimple_vuse (stmt)
609 && !gimple_could_trap_p_1 (stmt, false, false)
610 && ifcvt_memrefs_wont_trap (stmt, refs))
611 return false;
612
613 return gimple_could_trap_p (stmt);
614 }
615
616 /* Return true when STMT is if-convertible.
617
618 GIMPLE_ASSIGN statement is not if-convertible if,
619 - it is not movable,
620 - it could trap,
621 - LHS is not var decl. */
622
623 static bool
624 if_convertible_gimple_assign_stmt_p (gimple stmt,
625 VEC (data_reference_p, heap) *refs)
626 {
627 tree lhs = gimple_assign_lhs (stmt);
628 basic_block bb;
629
630 if (dump_file && (dump_flags & TDF_DETAILS))
631 {
632 fprintf (dump_file, "-------------------------\n");
633 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
634 }
635
636 if (!is_gimple_reg_type (TREE_TYPE (lhs)))
637 return false;
638
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))
645 {
646 if (dump_file && (dump_flags & TDF_DETAILS))
647 fprintf (dump_file, "stmt not suitable for ifcvt\n");
648 return false;
649 }
650
651 if (flag_tree_loop_if_convert_stores)
652 {
653 if (ifcvt_could_trap_p (stmt, refs))
654 {
655 if (dump_file && (dump_flags & TDF_DETAILS))
656 fprintf (dump_file, "tree could trap...\n");
657 return false;
658 }
659 return true;
660 }
661
662 if (gimple_assign_rhs_could_trap_p (stmt))
663 {
664 if (dump_file && (dump_flags & TDF_DETAILS))
665 fprintf (dump_file, "tree could trap...\n");
666 return false;
667 }
668
669 bb = gimple_bb (stmt);
670
671 if (TREE_CODE (lhs) != SSA_NAME
672 && bb != bb->loop_father->header
673 && !bb_with_exit_edge_p (bb->loop_father, bb))
674 {
675 if (dump_file && (dump_flags & TDF_DETAILS))
676 {
677 fprintf (dump_file, "LHS is not var\n");
678 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
679 }
680 return false;
681 }
682
683 return true;
684 }
685
686 /* Return true when STMT is if-convertible.
687
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. */
691
692 static bool
693 if_convertible_stmt_p (gimple stmt, VEC (data_reference_p, heap) *refs)
694 {
695 switch (gimple_code (stmt))
696 {
697 case GIMPLE_LABEL:
698 case GIMPLE_DEBUG:
699 case GIMPLE_COND:
700 return true;
701
702 case GIMPLE_ASSIGN:
703 return if_convertible_gimple_assign_stmt_p (stmt, refs);
704
705 default:
706 /* Don't know what to do with 'em so don't do anything. */
707 if (dump_file && (dump_flags & TDF_DETAILS))
708 {
709 fprintf (dump_file, "don't know what to do\n");
710 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
711 }
712 return false;
713 break;
714 }
715
716 return true;
717 }
718
719 /* Return true when BB post-dominates all its predecessors. */
720
721 static bool
722 bb_postdominates_preds (basic_block bb)
723 {
724 unsigned i;
725
726 for (i = 0; i < EDGE_COUNT (bb->preds); i++)
727 if (!dominated_by_p (CDI_POST_DOMINATORS, EDGE_PRED (bb, i)->src, bb))
728 return false;
729
730 return true;
731 }
732
733 /* Return true when BB is if-convertible. This routine does not check
734 basic block's statements and phis.
735
736 A basic block is not if-convertible if:
737 - it is non-empty and it is after the exit block (in BFS order),
738 - it is after the exit block but before the latch,
739 - its edges are not normal.
740
741 EXIT_BB is the basic block containing the exit of the LOOP. BB is
742 inside LOOP. */
743
744 static bool
745 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
746 {
747 edge e;
748 edge_iterator ei;
749
750 if (dump_file && (dump_flags & TDF_DETAILS))
751 fprintf (dump_file, "----------[%d]-------------\n", bb->index);
752
753 if (EDGE_COUNT (bb->preds) > 2
754 || EDGE_COUNT (bb->succs) > 2)
755 return false;
756
757 if (exit_bb)
758 {
759 if (bb != loop->latch)
760 {
761 if (dump_file && (dump_flags & TDF_DETAILS))
762 fprintf (dump_file, "basic block after exit bb but before latch\n");
763 return false;
764 }
765 else if (!empty_block_p (bb))
766 {
767 if (dump_file && (dump_flags & TDF_DETAILS))
768 fprintf (dump_file, "non empty basic block after exit bb\n");
769 return false;
770 }
771 else if (bb == loop->latch
772 && bb != exit_bb
773 && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
774 {
775 if (dump_file && (dump_flags & TDF_DETAILS))
776 fprintf (dump_file, "latch is not dominated by exit_block\n");
777 return false;
778 }
779 }
780
781 /* Be less adventurous and handle only normal edges. */
782 FOR_EACH_EDGE (e, ei, bb->succs)
783 if (e->flags &
784 (EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
785 {
786 if (dump_file && (dump_flags & TDF_DETAILS))
787 fprintf (dump_file, "Difficult to handle edges\n");
788 return false;
789 }
790
791 if (EDGE_COUNT (bb->preds) == 2
792 && bb != loop->header
793 && !bb_postdominates_preds (bb))
794 return false;
795
796 return true;
797 }
798
799 /* Return true when all predecessor blocks of BB are visited. The
800 VISITED bitmap keeps track of the visited blocks. */
801
802 static bool
803 pred_blocks_visited_p (basic_block bb, bitmap *visited)
804 {
805 edge e;
806 edge_iterator ei;
807 FOR_EACH_EDGE (e, ei, bb->preds)
808 if (!bitmap_bit_p (*visited, e->src->index))
809 return false;
810
811 return true;
812 }
813
814 /* Get body of a LOOP in suitable order for if-conversion. It is
815 caller's responsibility to deallocate basic block list.
816 If-conversion suitable order is, breadth first sort (BFS) order
817 with an additional constraint: select a block only if all its
818 predecessors are already selected. */
819
820 static basic_block *
821 get_loop_body_in_if_conv_order (const struct loop *loop)
822 {
823 basic_block *blocks, *blocks_in_bfs_order;
824 basic_block bb;
825 bitmap visited;
826 unsigned int index = 0;
827 unsigned int visited_count = 0;
828
829 gcc_assert (loop->num_nodes);
830 gcc_assert (loop->latch != EXIT_BLOCK_PTR);
831
832 blocks = XCNEWVEC (basic_block, loop->num_nodes);
833 visited = BITMAP_ALLOC (NULL);
834
835 blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
836
837 index = 0;
838 while (index < loop->num_nodes)
839 {
840 bb = blocks_in_bfs_order [index];
841
842 if (bb->flags & BB_IRREDUCIBLE_LOOP)
843 {
844 free (blocks_in_bfs_order);
845 BITMAP_FREE (visited);
846 free (blocks);
847 return NULL;
848 }
849
850 if (!bitmap_bit_p (visited, bb->index))
851 {
852 if (pred_blocks_visited_p (bb, &visited)
853 || bb == loop->header)
854 {
855 /* This block is now visited. */
856 bitmap_set_bit (visited, bb->index);
857 blocks[visited_count++] = bb;
858 }
859 }
860
861 index++;
862
863 if (index == loop->num_nodes
864 && visited_count != loop->num_nodes)
865 /* Not done yet. */
866 index = 0;
867 }
868 free (blocks_in_bfs_order);
869 BITMAP_FREE (visited);
870 return blocks;
871 }
872
873 /* Returns true when the analysis of the predicates for all the basic
874 blocks in LOOP succeeded.
875
876 predicate_bbs first allocates the predicates of the basic blocks.
877 These fields are then initialized with the tree expressions
878 representing the predicates under which a basic block is executed
879 in the LOOP. As the loop->header is executed at each iteration, it
880 has the "true" predicate. Other statements executed under a
881 condition are predicated with that condition, for example
882
883 | if (x)
884 | S1;
885 | else
886 | S2;
887
888 S1 will be predicated with "x", and
889 S2 will be predicated with "!x". */
890
891 static bool
892 predicate_bbs (loop_p loop)
893 {
894 unsigned int i;
895
896 for (i = 0; i < loop->num_nodes; i++)
897 init_bb_predicate (ifc_bbs[i]);
898
899 for (i = 0; i < loop->num_nodes; i++)
900 {
901 basic_block bb = ifc_bbs[i];
902 tree cond;
903 gimple_stmt_iterator itr;
904
905 /* The loop latch is always executed and has no extra conditions
906 to be processed: skip it. */
907 if (bb == loop->latch)
908 {
909 reset_bb_predicate (loop->latch);
910 continue;
911 }
912
913 cond = bb_predicate (bb);
914 if (cond
915 && bb != loop->header)
916 {
917 gimple_seq stmts;
918
919 cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
920 add_bb_predicate_gimplified_stmts (bb, stmts);
921 }
922
923 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
924 {
925 gimple stmt = gsi_stmt (itr);
926
927 switch (gimple_code (stmt))
928 {
929 case GIMPLE_LABEL:
930 case GIMPLE_ASSIGN:
931 case GIMPLE_CALL:
932 case GIMPLE_DEBUG:
933 break;
934
935 case GIMPLE_COND:
936 {
937 tree c2, tem;
938 edge true_edge, false_edge;
939 location_t loc = gimple_location (stmt);
940 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
941 boolean_type_node,
942 gimple_cond_lhs (stmt),
943 gimple_cond_rhs (stmt));
944
945 /* Add new condition into destination's predicate list. */
946 extract_true_false_edges_from_block (gimple_bb (stmt),
947 &true_edge, &false_edge);
948
949 /* If C is true, then TRUE_EDGE is taken. */
950 add_to_dst_predicate_list (loop, true_edge, cond, unshare_expr (c));
951
952 /* If C is false, then FALSE_EDGE is taken. */
953 c2 = invert_truthvalue_loc (loc, unshare_expr (c));
954 tem = canonicalize_cond_expr_cond (c2);
955 if (tem)
956 c2 = tem;
957 add_to_dst_predicate_list (loop, false_edge, cond, c2);
958
959 cond = NULL_TREE;
960 break;
961 }
962
963 default:
964 /* Not handled yet in if-conversion. */
965 return false;
966 }
967 }
968
969 /* If current bb has only one successor, then consider it as an
970 unconditional goto. */
971 if (single_succ_p (bb))
972 {
973 basic_block bb_n = single_succ (bb);
974
975 /* The successor bb inherits the predicate of its
976 predecessor. If there is no predicate in the predecessor
977 bb, then consider the successor bb as always executed. */
978 if (cond == NULL_TREE)
979 cond = boolean_true_node;
980
981 add_to_predicate_list (bb_n, cond);
982 }
983 }
984
985 /* The loop header is always executed. */
986 reset_bb_predicate (loop->header);
987 gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
988 && bb_predicate_gimplified_stmts (loop->latch) == NULL);
989
990 return true;
991 }
992
993 /* Return true when LOOP is if-convertible. This is a helper function
994 for if_convertible_loop_p. REFS and DDRS are initialized and freed
995 in if_convertible_loop_p. */
996
997 static bool
998 if_convertible_loop_p_1 (struct loop *loop,
999 VEC (loop_p, heap) **loop_nest,
1000 VEC (data_reference_p, heap) **refs,
1001 VEC (ddr_p, heap) **ddrs)
1002 {
1003 bool res;
1004 unsigned int i;
1005 basic_block exit_bb = NULL;
1006
1007 /* Don't if-convert the loop when the data dependences cannot be
1008 computed: the loop won't be vectorized in that case. */
1009 res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1010 if (!res)
1011 return false;
1012
1013 calculate_dominance_info (CDI_DOMINATORS);
1014 calculate_dominance_info (CDI_POST_DOMINATORS);
1015
1016 /* Allow statements that can be handled during if-conversion. */
1017 ifc_bbs = get_loop_body_in_if_conv_order (loop);
1018 if (!ifc_bbs)
1019 {
1020 if (dump_file && (dump_flags & TDF_DETAILS))
1021 fprintf (dump_file, "Irreducible loop\n");
1022 return false;
1023 }
1024
1025 for (i = 0; i < loop->num_nodes; i++)
1026 {
1027 basic_block bb = ifc_bbs[i];
1028
1029 if (!if_convertible_bb_p (loop, bb, exit_bb))
1030 return false;
1031
1032 if (bb_with_exit_edge_p (loop, bb))
1033 exit_bb = bb;
1034 }
1035
1036 res = predicate_bbs (loop);
1037 if (!res)
1038 return false;
1039
1040 if (flag_tree_loop_if_convert_stores)
1041 {
1042 data_reference_p dr;
1043
1044 for (i = 0; VEC_iterate (data_reference_p, *refs, i, dr); i++)
1045 {
1046 dr->aux = XNEW (struct ifc_dr);
1047 DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1048 DR_RW_UNCONDITIONALLY (dr) = -1;
1049 }
1050 }
1051
1052 for (i = 0; i < loop->num_nodes; i++)
1053 {
1054 basic_block bb = ifc_bbs[i];
1055 gimple_stmt_iterator itr;
1056
1057 for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1058 if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1059 return false;
1060
1061 /* Check the if-convertibility of statements in predicated BBs. */
1062 if (is_predicated (bb))
1063 for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1064 if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1065 return false;
1066 }
1067
1068 if (dump_file)
1069 fprintf (dump_file, "Applying if-conversion\n");
1070
1071 return true;
1072 }
1073
1074 /* Return true when LOOP is if-convertible.
1075 LOOP is if-convertible if:
1076 - it is innermost,
1077 - it has two or more basic blocks,
1078 - it has only one exit,
1079 - loop header is not the exit edge,
1080 - if its basic blocks and phi nodes are if convertible. */
1081
1082 static bool
1083 if_convertible_loop_p (struct loop *loop)
1084 {
1085 edge e;
1086 edge_iterator ei;
1087 bool res = false;
1088 VEC (data_reference_p, heap) *refs;
1089 VEC (ddr_p, heap) *ddrs;
1090 VEC (loop_p, heap) *loop_nest;
1091
1092 /* Handle only innermost loop. */
1093 if (!loop || loop->inner)
1094 {
1095 if (dump_file && (dump_flags & TDF_DETAILS))
1096 fprintf (dump_file, "not innermost loop\n");
1097 return false;
1098 }
1099
1100 /* If only one block, no need for if-conversion. */
1101 if (loop->num_nodes <= 2)
1102 {
1103 if (dump_file && (dump_flags & TDF_DETAILS))
1104 fprintf (dump_file, "less than 2 basic blocks\n");
1105 return false;
1106 }
1107
1108 /* More than one loop exit is too much to handle. */
1109 if (!single_exit (loop))
1110 {
1111 if (dump_file && (dump_flags & TDF_DETAILS))
1112 fprintf (dump_file, "multiple exits\n");
1113 return false;
1114 }
1115
1116 /* If one of the loop header's edge is an exit edge then do not
1117 apply if-conversion. */
1118 FOR_EACH_EDGE (e, ei, loop->header->succs)
1119 if (loop_exit_edge_p (loop, e))
1120 return false;
1121
1122 refs = VEC_alloc (data_reference_p, heap, 5);
1123 ddrs = VEC_alloc (ddr_p, heap, 25);
1124 loop_nest = VEC_alloc (loop_p, heap, 3);
1125 res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1126
1127 if (flag_tree_loop_if_convert_stores)
1128 {
1129 data_reference_p dr;
1130 unsigned int i;
1131
1132 for (i = 0; VEC_iterate (data_reference_p, refs, i, dr); i++)
1133 free (dr->aux);
1134 }
1135
1136 VEC_free (loop_p, heap, loop_nest);
1137 free_data_refs (refs);
1138 free_dependence_relations (ddrs);
1139 return res;
1140 }
1141
1142 /* Basic block BB has two predecessors. Using predecessor's bb
1143 predicate, set an appropriate condition COND for the PHI node
1144 replacement. Return the true block whose phi arguments are
1145 selected when cond is true. LOOP is the loop containing the
1146 if-converted region, GSI is the place to insert the code for the
1147 if-conversion. */
1148
1149 static basic_block
1150 find_phi_replacement_condition (struct loop *loop,
1151 basic_block bb, tree *cond,
1152 gimple_stmt_iterator *gsi)
1153 {
1154 edge first_edge, second_edge;
1155 tree tmp_cond;
1156
1157 gcc_assert (EDGE_COUNT (bb->preds) == 2);
1158 first_edge = EDGE_PRED (bb, 0);
1159 second_edge = EDGE_PRED (bb, 1);
1160
1161 /* Use condition based on following criteria:
1162 1)
1163 S1: x = !c ? a : b;
1164
1165 S2: x = c ? b : a;
1166
1167 S2 is preferred over S1. Make 'b' first_bb and use its condition.
1168
1169 2) Do not make loop header first_bb.
1170
1171 3)
1172 S1: x = !(c == d)? a : b;
1173
1174 S21: t1 = c == d;
1175 S22: x = t1 ? b : a;
1176
1177 S3: x = (c == d) ? b : a;
1178
1179 S3 is preferred over S1 and S2*, Make 'b' first_bb and use
1180 its condition.
1181
1182 4) If pred B is dominated by pred A then use pred B's condition.
1183 See PR23115. */
1184
1185 /* Select condition that is not TRUTH_NOT_EXPR. */
1186 tmp_cond = bb_predicate (first_edge->src);
1187 gcc_assert (tmp_cond);
1188
1189 if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1190 {
1191 edge tmp_edge;
1192
1193 tmp_edge = first_edge;
1194 first_edge = second_edge;
1195 second_edge = tmp_edge;
1196 }
1197
1198 /* Check if FIRST_BB is loop header or not and make sure that
1199 FIRST_BB does not dominate SECOND_BB. */
1200 if (first_edge->src == loop->header
1201 || dominated_by_p (CDI_DOMINATORS,
1202 second_edge->src, first_edge->src))
1203 {
1204 *cond = bb_predicate (second_edge->src);
1205
1206 if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1207 *cond = invert_truthvalue (*cond);
1208 else
1209 /* Select non loop header bb. */
1210 first_edge = second_edge;
1211 }
1212 else
1213 *cond = bb_predicate (first_edge->src);
1214
1215 /* Gimplify the condition: the vectorizer prefers to have gimple
1216 values as conditions. Various targets use different means to
1217 communicate conditions in vector compare operations. Using a
1218 gimple value allows the compiler to emit vector compare and
1219 select RTL without exposing compare's result. */
1220 *cond = force_gimple_operand_gsi (gsi, unshare_expr (*cond),
1221 false, NULL_TREE,
1222 true, GSI_SAME_STMT);
1223 if (!is_gimple_reg (*cond) && !is_gimple_condexpr (*cond))
1224 *cond = ifc_temp_var (TREE_TYPE (*cond), unshare_expr (*cond), gsi);
1225
1226 gcc_assert (*cond);
1227
1228 return first_edge->src;
1229 }
1230
1231 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1232 This routine does not handle PHI nodes with more than two
1233 arguments.
1234
1235 For example,
1236 S1: A = PHI <x1(1), x2(5)
1237 is converted into,
1238 S2: A = cond ? x1 : x2;
1239
1240 The generated code is inserted at GSI that points to the top of
1241 basic block's statement list. When COND is true, phi arg from
1242 TRUE_BB is selected. */
1243
1244 static void
1245 predicate_scalar_phi (gimple phi, tree cond,
1246 basic_block true_bb,
1247 gimple_stmt_iterator *gsi)
1248 {
1249 gimple new_stmt;
1250 basic_block bb;
1251 tree rhs, res, arg, scev;
1252
1253 gcc_assert (gimple_code (phi) == GIMPLE_PHI
1254 && gimple_phi_num_args (phi) == 2);
1255
1256 res = gimple_phi_result (phi);
1257 /* Do not handle virtual phi nodes. */
1258 if (!is_gimple_reg (SSA_NAME_VAR (res)))
1259 return;
1260
1261 bb = gimple_bb (phi);
1262
1263 if ((arg = degenerate_phi_result (phi))
1264 || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1265 res))
1266 && !chrec_contains_undetermined (scev)
1267 && scev != res
1268 && (arg = gimple_phi_arg_def (phi, 0))))
1269 rhs = arg;
1270 else
1271 {
1272 tree arg_0, arg_1;
1273 /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
1274 if (EDGE_PRED (bb, 1)->src == true_bb)
1275 {
1276 arg_0 = gimple_phi_arg_def (phi, 1);
1277 arg_1 = gimple_phi_arg_def (phi, 0);
1278 }
1279 else
1280 {
1281 arg_0 = gimple_phi_arg_def (phi, 0);
1282 arg_1 = gimple_phi_arg_def (phi, 1);
1283 }
1284
1285 gcc_checking_assert (bb == bb->loop_father->header
1286 || bb_postdominates_preds (bb));
1287
1288 /* Build new RHS using selected condition and arguments. */
1289 rhs = build3 (COND_EXPR, TREE_TYPE (res),
1290 unshare_expr (cond), arg_0, arg_1);
1291 }
1292
1293 new_stmt = gimple_build_assign (res, rhs);
1294 SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
1295 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1296 update_stmt (new_stmt);
1297
1298 if (dump_file && (dump_flags & TDF_DETAILS))
1299 {
1300 fprintf (dump_file, "new phi replacement stmt\n");
1301 print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1302 }
1303 }
1304
1305 /* Replaces in LOOP all the scalar phi nodes other than those in the
1306 LOOP->header block with conditional modify expressions. */
1307
1308 static void
1309 predicate_all_scalar_phis (struct loop *loop)
1310 {
1311 basic_block bb;
1312 unsigned int orig_loop_num_nodes = loop->num_nodes;
1313 unsigned int i;
1314
1315 for (i = 1; i < orig_loop_num_nodes; i++)
1316 {
1317 gimple phi;
1318 tree cond = NULL_TREE;
1319 gimple_stmt_iterator gsi, phi_gsi;
1320 basic_block true_bb = NULL;
1321 bb = ifc_bbs[i];
1322
1323 if (bb == loop->header)
1324 continue;
1325
1326 phi_gsi = gsi_start_phis (bb);
1327 if (gsi_end_p (phi_gsi))
1328 continue;
1329
1330 /* BB has two predecessors. Using predecessor's aux field, set
1331 appropriate condition for the PHI node replacement. */
1332 gsi = gsi_after_labels (bb);
1333 true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
1334
1335 while (!gsi_end_p (phi_gsi))
1336 {
1337 phi = gsi_stmt (phi_gsi);
1338 predicate_scalar_phi (phi, cond, true_bb, &gsi);
1339 release_phi_node (phi);
1340 gsi_next (&phi_gsi);
1341 }
1342
1343 set_phi_nodes (bb, NULL);
1344 }
1345 }
1346
1347 /* Insert in each basic block of LOOP the statements produced by the
1348 gimplification of the predicates. */
1349
1350 static void
1351 insert_gimplified_predicates (loop_p loop)
1352 {
1353 unsigned int i;
1354
1355 for (i = 0; i < loop->num_nodes; i++)
1356 {
1357 basic_block bb = ifc_bbs[i];
1358 gimple_seq stmts;
1359
1360 if (!is_predicated (bb))
1361 {
1362 /* Do not insert statements for a basic block that is not
1363 predicated. Also make sure that the predicate of the
1364 basic block is set to true. */
1365 reset_bb_predicate (bb);
1366 continue;
1367 }
1368
1369 stmts = bb_predicate_gimplified_stmts (bb);
1370 if (stmts)
1371 {
1372 if (flag_tree_loop_if_convert_stores)
1373 {
1374 /* Insert the predicate of the BB just after the label,
1375 as the if-conversion of memory writes will use this
1376 predicate. */
1377 gimple_stmt_iterator gsi = gsi_after_labels (bb);
1378 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1379 }
1380 else
1381 {
1382 /* Insert the predicate of the BB at the end of the BB
1383 as this would reduce the register pressure: the only
1384 use of this predicate will be in successor BBs. */
1385 gimple_stmt_iterator gsi = gsi_last_bb (bb);
1386
1387 if (gsi_end_p (gsi)
1388 || stmt_ends_bb_p (gsi_stmt (gsi)))
1389 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1390 else
1391 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1392 }
1393
1394 /* Once the sequence is code generated, set it to NULL. */
1395 set_bb_predicate_gimplified_stmts (bb, NULL);
1396 }
1397 }
1398 }
1399
1400 /* Predicate each write to memory in LOOP.
1401
1402 This function transforms control flow constructs containing memory
1403 writes of the form:
1404
1405 | for (i = 0; i < N; i++)
1406 | if (cond)
1407 | A[i] = expr;
1408
1409 into the following form that does not contain control flow:
1410
1411 | for (i = 0; i < N; i++)
1412 | A[i] = cond ? expr : A[i];
1413
1414 The original CFG looks like this:
1415
1416 | bb_0
1417 | i = 0
1418 | end_bb_0
1419 |
1420 | bb_1
1421 | if (i < N) goto bb_5 else goto bb_2
1422 | end_bb_1
1423 |
1424 | bb_2
1425 | cond = some_computation;
1426 | if (cond) goto bb_3 else goto bb_4
1427 | end_bb_2
1428 |
1429 | bb_3
1430 | A[i] = expr;
1431 | goto bb_4
1432 | end_bb_3
1433 |
1434 | bb_4
1435 | goto bb_1
1436 | end_bb_4
1437
1438 insert_gimplified_predicates inserts the computation of the COND
1439 expression at the beginning of the destination basic block:
1440
1441 | bb_0
1442 | i = 0
1443 | end_bb_0
1444 |
1445 | bb_1
1446 | if (i < N) goto bb_5 else goto bb_2
1447 | end_bb_1
1448 |
1449 | bb_2
1450 | cond = some_computation;
1451 | if (cond) goto bb_3 else goto bb_4
1452 | end_bb_2
1453 |
1454 | bb_3
1455 | cond = some_computation;
1456 | A[i] = expr;
1457 | goto bb_4
1458 | end_bb_3
1459 |
1460 | bb_4
1461 | goto bb_1
1462 | end_bb_4
1463
1464 predicate_mem_writes is then predicating the memory write as follows:
1465
1466 | bb_0
1467 | i = 0
1468 | end_bb_0
1469 |
1470 | bb_1
1471 | if (i < N) goto bb_5 else goto bb_2
1472 | end_bb_1
1473 |
1474 | bb_2
1475 | if (cond) goto bb_3 else goto bb_4
1476 | end_bb_2
1477 |
1478 | bb_3
1479 | cond = some_computation;
1480 | A[i] = cond ? expr : A[i];
1481 | goto bb_4
1482 | end_bb_3
1483 |
1484 | bb_4
1485 | goto bb_1
1486 | end_bb_4
1487
1488 and finally combine_blocks removes the basic block boundaries making
1489 the loop vectorizable:
1490
1491 | bb_0
1492 | i = 0
1493 | if (i < N) goto bb_5 else goto bb_1
1494 | end_bb_0
1495 |
1496 | bb_1
1497 | cond = some_computation;
1498 | A[i] = cond ? expr : A[i];
1499 | if (i < N) goto bb_5 else goto bb_4
1500 | end_bb_1
1501 |
1502 | bb_4
1503 | goto bb_1
1504 | end_bb_4
1505 */
1506
1507 static void
1508 predicate_mem_writes (loop_p loop)
1509 {
1510 unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1511
1512 for (i = 1; i < orig_loop_num_nodes; i++)
1513 {
1514 gimple_stmt_iterator gsi;
1515 basic_block bb = ifc_bbs[i];
1516 tree cond = bb_predicate (bb);
1517 gimple stmt;
1518
1519 if (is_true_predicate (cond))
1520 continue;
1521
1522 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1523 if ((stmt = gsi_stmt (gsi))
1524 && gimple_assign_single_p (stmt)
1525 && gimple_vdef (stmt))
1526 {
1527 tree lhs = gimple_assign_lhs (stmt);
1528 tree rhs = gimple_assign_rhs1 (stmt);
1529 tree type = TREE_TYPE (lhs);
1530
1531 lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1532 rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1533 rhs = build3 (COND_EXPR, type, unshare_expr (cond), rhs, lhs);
1534 gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1535 update_stmt (stmt);
1536 }
1537 }
1538 }
1539
1540 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1541 other than the exit and latch of the LOOP. Also resets the
1542 GIMPLE_DEBUG information. */
1543
1544 static void
1545 remove_conditions_and_labels (loop_p loop)
1546 {
1547 gimple_stmt_iterator gsi;
1548 unsigned int i;
1549
1550 for (i = 0; i < loop->num_nodes; i++)
1551 {
1552 basic_block bb = ifc_bbs[i];
1553
1554 if (bb_with_exit_edge_p (loop, bb)
1555 || bb == loop->latch)
1556 continue;
1557
1558 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1559 switch (gimple_code (gsi_stmt (gsi)))
1560 {
1561 case GIMPLE_COND:
1562 case GIMPLE_LABEL:
1563 gsi_remove (&gsi, true);
1564 break;
1565
1566 case GIMPLE_DEBUG:
1567 /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
1568 if (gimple_debug_bind_p (gsi_stmt (gsi)))
1569 {
1570 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1571 update_stmt (gsi_stmt (gsi));
1572 }
1573 gsi_next (&gsi);
1574 break;
1575
1576 default:
1577 gsi_next (&gsi);
1578 }
1579 }
1580 }
1581
1582 /* Combine all the basic blocks from LOOP into one or two super basic
1583 blocks. Replace PHI nodes with conditional modify expressions. */
1584
1585 static void
1586 combine_blocks (struct loop *loop)
1587 {
1588 basic_block bb, exit_bb, merge_target_bb;
1589 unsigned int orig_loop_num_nodes = loop->num_nodes;
1590 unsigned int i;
1591 edge e;
1592 edge_iterator ei;
1593
1594 remove_conditions_and_labels (loop);
1595 insert_gimplified_predicates (loop);
1596 predicate_all_scalar_phis (loop);
1597
1598 if (flag_tree_loop_if_convert_stores)
1599 predicate_mem_writes (loop);
1600
1601 /* Merge basic blocks: first remove all the edges in the loop,
1602 except for those from the exit block. */
1603 exit_bb = NULL;
1604 for (i = 0; i < orig_loop_num_nodes; i++)
1605 {
1606 bb = ifc_bbs[i];
1607 if (bb_with_exit_edge_p (loop, bb))
1608 {
1609 exit_bb = bb;
1610 break;
1611 }
1612 }
1613 gcc_assert (exit_bb != loop->latch);
1614
1615 for (i = 1; i < orig_loop_num_nodes; i++)
1616 {
1617 bb = ifc_bbs[i];
1618
1619 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1620 {
1621 if (e->src == exit_bb)
1622 ei_next (&ei);
1623 else
1624 remove_edge (e);
1625 }
1626 }
1627
1628 if (exit_bb != NULL)
1629 {
1630 if (exit_bb != loop->header)
1631 {
1632 /* Connect this node to loop header. */
1633 make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1634 set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1635 }
1636
1637 /* Redirect non-exit edges to loop->latch. */
1638 FOR_EACH_EDGE (e, ei, exit_bb->succs)
1639 {
1640 if (!loop_exit_edge_p (loop, e))
1641 redirect_edge_and_branch (e, loop->latch);
1642 }
1643 set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1644 }
1645 else
1646 {
1647 /* If the loop does not have an exit, reconnect header and latch. */
1648 make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1649 set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1650 }
1651
1652 merge_target_bb = loop->header;
1653 for (i = 1; i < orig_loop_num_nodes; i++)
1654 {
1655 gimple_stmt_iterator gsi;
1656 gimple_stmt_iterator last;
1657
1658 bb = ifc_bbs[i];
1659
1660 if (bb == exit_bb || bb == loop->latch)
1661 continue;
1662
1663 /* Make stmts member of loop->header. */
1664 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1665 gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1666
1667 /* Update stmt list. */
1668 last = gsi_last_bb (merge_target_bb);
1669 gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1670 set_bb_seq (bb, NULL);
1671
1672 delete_basic_block (bb);
1673 }
1674
1675 /* If possible, merge loop header to the block with the exit edge.
1676 This reduces the number of basic blocks to two, to please the
1677 vectorizer that handles only loops with two nodes. */
1678 if (exit_bb
1679 && exit_bb != loop->header
1680 && can_merge_blocks_p (loop->header, exit_bb))
1681 merge_blocks (loop->header, exit_bb);
1682 }
1683
1684 /* If-convert LOOP when it is legal. For the moment this pass has no
1685 profitability analysis. Returns true when something changed. */
1686
1687 static bool
1688 tree_if_conversion (struct loop *loop)
1689 {
1690 bool changed = false;
1691 ifc_bbs = NULL;
1692
1693 if (!if_convertible_loop_p (loop)
1694 || !dbg_cnt (if_conversion_tree))
1695 goto cleanup;
1696
1697 /* Now all statements are if-convertible. Combine all the basic
1698 blocks into one huge basic block doing the if-conversion
1699 on-the-fly. */
1700 combine_blocks (loop);
1701
1702 if (flag_tree_loop_if_convert_stores)
1703 mark_sym_for_renaming (gimple_vop (cfun));
1704
1705 changed = true;
1706
1707 cleanup:
1708 if (ifc_bbs)
1709 {
1710 unsigned int i;
1711
1712 for (i = 0; i < loop->num_nodes; i++)
1713 free_bb_predicate (ifc_bbs[i]);
1714
1715 free (ifc_bbs);
1716 ifc_bbs = NULL;
1717 }
1718
1719 return changed;
1720 }
1721
1722 /* Tree if-conversion pass management. */
1723
1724 static unsigned int
1725 main_tree_if_conversion (void)
1726 {
1727 loop_iterator li;
1728 struct loop *loop;
1729 bool changed = false;
1730 unsigned todo = 0;
1731
1732 if (number_of_loops () <= 1)
1733 return 0;
1734
1735 FOR_EACH_LOOP (li, loop, 0)
1736 changed |= tree_if_conversion (loop);
1737
1738 if (changed)
1739 todo |= TODO_cleanup_cfg;
1740
1741 if (changed && flag_tree_loop_if_convert_stores)
1742 todo |= TODO_update_ssa_only_virtuals;
1743
1744 return todo;
1745 }
1746
1747 /* Returns true when the if-conversion pass is enabled. */
1748
1749 static bool
1750 gate_tree_if_conversion (void)
1751 {
1752 return ((flag_tree_vectorize && flag_tree_loop_if_convert != 0)
1753 || flag_tree_loop_if_convert == 1
1754 || flag_tree_loop_if_convert_stores == 1);
1755 }
1756
1757 struct gimple_opt_pass pass_if_conversion =
1758 {
1759 {
1760 GIMPLE_PASS,
1761 "ifcvt", /* name */
1762 gate_tree_if_conversion, /* gate */
1763 main_tree_if_conversion, /* execute */
1764 NULL, /* sub */
1765 NULL, /* next */
1766 0, /* static_pass_number */
1767 TV_NONE, /* tv_id */
1768 PROP_cfg | PROP_ssa, /* properties_required */
1769 0, /* properties_provided */
1770 0, /* properties_destroyed */
1771 0, /* todo_flags_start */
1772 TODO_dump_func | TODO_verify_stmts | TODO_verify_flow
1773 /* todo_flags_finish */
1774 }
1775 };