OSDN Git Service

7ca6deec123634583c92b44adcdfdc8c9ffcdae8
[pf3gnuchains/gcc-fork.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    a data reference with the same base with predicates that add up (OR-up) to
468    the 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           {
493             tree ref_base_a = DR_REF (a);
494             tree ref_base_b = DR_REF (b);
495
496             if (DR_STMT (b) == stmt)
497               continue;
498
499             while (TREE_CODE (ref_base_a) == COMPONENT_REF
500                    || TREE_CODE (ref_base_a) == IMAGPART_EXPR
501                    || TREE_CODE (ref_base_a) == REALPART_EXPR)
502               ref_base_a = TREE_OPERAND (ref_base_a, 0);
503
504             while (TREE_CODE (ref_base_b) == COMPONENT_REF
505                    || TREE_CODE (ref_base_b) == IMAGPART_EXPR
506                    || TREE_CODE (ref_base_b) == REALPART_EXPR)
507               ref_base_b = TREE_OPERAND (ref_base_b, 0);
508
509             if (!operand_equal_p (ref_base_a, ref_base_b, 0))
510               {
511                 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
512
513                 if (DR_RW_UNCONDITIONALLY (b) == 1
514                     || is_true_predicate (cb)
515                     || is_true_predicate (ca
516                         = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
517                   {
518                     DR_RW_UNCONDITIONALLY (a) = 1;
519                     DR_RW_UNCONDITIONALLY (b) = 1;
520                     found = true;
521                     break;
522                   }
523                }
524             }
525
526         if (!found)
527           {
528             DR_RW_UNCONDITIONALLY (a) = 0;
529             return false;
530           }
531       }
532
533   return true;
534 }
535
536 /* Returns true when the memory references of STMT are unconditionally
537    written.  In other words, this function returns true when for every
538    data reference A written in STMT, there exist other writes to the
539    same data reference with predicates that add up (OR-up) to the true
540    predicate: this ensures that the data reference A is written on
541    every iteration of the if-converted loop.  */
542
543 static bool
544 write_memrefs_written_at_least_once (gimple stmt,
545                                      VEC (data_reference_p, heap) *drs)
546 {
547   int i, j;
548   data_reference_p a, b;
549   tree ca = bb_predicate (gimple_bb (stmt));
550
551   for (i = 0; VEC_iterate (data_reference_p, drs, i, a); i++)
552     if (DR_STMT (a) == stmt
553         && DR_IS_WRITE (a))
554       {
555         bool found = false;
556         int x = DR_WRITTEN_AT_LEAST_ONCE (a);
557
558         if (x == 0)
559           return false;
560
561         if (x == 1)
562           continue;
563
564         for (j = 0; VEC_iterate (data_reference_p, drs, j, b); j++)
565           if (DR_STMT (b) != stmt
566               && DR_IS_WRITE (b)
567               && same_data_refs_base_objects (a, b))
568             {
569               tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
570
571               if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
572                   || is_true_predicate (cb)
573                   || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
574                                                                  ca, cb)))
575                 {
576                   DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
577                   DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
578                   found = true;
579                   break;
580                 }
581             }
582
583         if (!found)
584           {
585             DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
586             return false;
587           }
588       }
589
590   return true;
591 }
592
593 /* Return true when the memory references of STMT won't trap in the
594    if-converted code.  There are two things that we have to check for:
595
596    - writes to memory occur to writable memory: if-conversion of
597    memory writes transforms the conditional memory writes into
598    unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
599    into "A[i] = cond ? foo : A[i]", and as the write to memory may not
600    be executed at all in the original code, it may be a readonly
601    memory.  To check that A is not const-qualified, we check that
602    there exists at least an unconditional write to A in the current
603    function.
604
605    - reads or writes to memory are valid memory accesses for every
606    iteration.  To check that the memory accesses are correctly formed
607    and that we are allowed to read and write in these locations, we
608    check that the memory accesses to be if-converted occur at every
609    iteration unconditionally.  */
610
611 static bool
612 ifcvt_memrefs_wont_trap (gimple stmt, VEC (data_reference_p, heap) *refs)
613 {
614   return write_memrefs_written_at_least_once (stmt, refs)
615     && memrefs_read_or_written_unconditionally (stmt, refs);
616 }
617
618 /* Wrapper around gimple_could_trap_p refined for the needs of the
619    if-conversion.  Try to prove that the memory accesses of STMT could
620    not trap in the innermost loop containing STMT.  */
621
622 static bool
623 ifcvt_could_trap_p (gimple stmt, VEC (data_reference_p, heap) *refs)
624 {
625   if (gimple_vuse (stmt)
626       && !gimple_could_trap_p_1 (stmt, false, false)
627       && ifcvt_memrefs_wont_trap (stmt, refs))
628     return false;
629
630   return gimple_could_trap_p (stmt);
631 }
632
633 /* Return true when STMT is if-convertible.
634
635    GIMPLE_ASSIGN statement is not if-convertible if,
636    - it is not movable,
637    - it could trap,
638    - LHS is not var decl.  */
639
640 static bool
641 if_convertible_gimple_assign_stmt_p (gimple stmt,
642                                      VEC (data_reference_p, heap) *refs)
643 {
644   tree lhs = gimple_assign_lhs (stmt);
645   basic_block bb;
646
647   if (dump_file && (dump_flags & TDF_DETAILS))
648     {
649       fprintf (dump_file, "-------------------------\n");
650       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
651     }
652
653   if (!is_gimple_reg_type (TREE_TYPE (lhs)))
654     return false;
655
656   /* Some of these constrains might be too conservative.  */
657   if (stmt_ends_bb_p (stmt)
658       || gimple_has_volatile_ops (stmt)
659       || (TREE_CODE (lhs) == SSA_NAME
660           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
661       || gimple_has_side_effects (stmt))
662     {
663       if (dump_file && (dump_flags & TDF_DETAILS))
664         fprintf (dump_file, "stmt not suitable for ifcvt\n");
665       return false;
666     }
667
668   if (flag_tree_loop_if_convert_stores)
669     {
670       if (ifcvt_could_trap_p (stmt, refs))
671         {
672           if (dump_file && (dump_flags & TDF_DETAILS))
673             fprintf (dump_file, "tree could trap...\n");
674           return false;
675         }
676       return true;
677     }
678
679   if (gimple_assign_rhs_could_trap_p (stmt))
680     {
681       if (dump_file && (dump_flags & TDF_DETAILS))
682         fprintf (dump_file, "tree could trap...\n");
683       return false;
684     }
685
686   bb = gimple_bb (stmt);
687
688   if (TREE_CODE (lhs) != SSA_NAME
689       && bb != bb->loop_father->header
690       && !bb_with_exit_edge_p (bb->loop_father, bb))
691     {
692       if (dump_file && (dump_flags & TDF_DETAILS))
693         {
694           fprintf (dump_file, "LHS is not var\n");
695           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
696         }
697       return false;
698     }
699
700   return true;
701 }
702
703 /* Return true when STMT is if-convertible.
704
705    A statement is if-convertible if:
706    - it is an if-convertible GIMPLE_ASSGIN,
707    - it is a GIMPLE_LABEL or a GIMPLE_COND.  */
708
709 static bool
710 if_convertible_stmt_p (gimple stmt, VEC (data_reference_p, heap) *refs)
711 {
712   switch (gimple_code (stmt))
713     {
714     case GIMPLE_LABEL:
715     case GIMPLE_DEBUG:
716     case GIMPLE_COND:
717       return true;
718
719     case GIMPLE_ASSIGN:
720       return if_convertible_gimple_assign_stmt_p (stmt, refs);
721
722     default:
723       /* Don't know what to do with 'em so don't do anything.  */
724       if (dump_file && (dump_flags & TDF_DETAILS))
725         {
726           fprintf (dump_file, "don't know what to do\n");
727           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
728         }
729       return false;
730       break;
731     }
732
733   return true;
734 }
735
736 /* Return true when BB post-dominates all its predecessors.  */
737
738 static bool
739 bb_postdominates_preds (basic_block bb)
740 {
741   unsigned i;
742
743   for (i = 0; i < EDGE_COUNT (bb->preds); i++)
744     if (!dominated_by_p (CDI_POST_DOMINATORS, EDGE_PRED (bb, i)->src, bb))
745       return false;
746
747   return true;
748 }
749
750 /* Return true when BB is if-convertible.  This routine does not check
751    basic block's statements and phis.
752
753    A basic block is not if-convertible if:
754    - it is non-empty and it is after the exit block (in BFS order),
755    - it is after the exit block but before the latch,
756    - its edges are not normal.
757
758    EXIT_BB is the basic block containing the exit of the LOOP.  BB is
759    inside LOOP.  */
760
761 static bool
762 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
763 {
764   edge e;
765   edge_iterator ei;
766
767   if (dump_file && (dump_flags & TDF_DETAILS))
768     fprintf (dump_file, "----------[%d]-------------\n", bb->index);
769
770   if (EDGE_COUNT (bb->preds) > 2
771       || EDGE_COUNT (bb->succs) > 2)
772     return false;
773
774   if (exit_bb)
775     {
776       if (bb != loop->latch)
777         {
778           if (dump_file && (dump_flags & TDF_DETAILS))
779             fprintf (dump_file, "basic block after exit bb but before latch\n");
780           return false;
781         }
782       else if (!empty_block_p (bb))
783         {
784           if (dump_file && (dump_flags & TDF_DETAILS))
785             fprintf (dump_file, "non empty basic block after exit bb\n");
786           return false;
787         }
788       else if (bb == loop->latch
789                && bb != exit_bb
790                && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
791           {
792             if (dump_file && (dump_flags & TDF_DETAILS))
793               fprintf (dump_file, "latch is not dominated by exit_block\n");
794             return false;
795           }
796     }
797
798   /* Be less adventurous and handle only normal edges.  */
799   FOR_EACH_EDGE (e, ei, bb->succs)
800     if (e->flags &
801         (EDGE_ABNORMAL_CALL | EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
802       {
803         if (dump_file && (dump_flags & TDF_DETAILS))
804           fprintf (dump_file, "Difficult to handle edges\n");
805         return false;
806       }
807
808   if (EDGE_COUNT (bb->preds) == 2
809       && bb != loop->header
810       && !bb_postdominates_preds (bb))
811     return false;
812
813   return true;
814 }
815
816 /* Return true when all predecessor blocks of BB are visited.  The
817    VISITED bitmap keeps track of the visited blocks.  */
818
819 static bool
820 pred_blocks_visited_p (basic_block bb, bitmap *visited)
821 {
822   edge e;
823   edge_iterator ei;
824   FOR_EACH_EDGE (e, ei, bb->preds)
825     if (!bitmap_bit_p (*visited, e->src->index))
826       return false;
827
828   return true;
829 }
830
831 /* Get body of a LOOP in suitable order for if-conversion.  It is
832    caller's responsibility to deallocate basic block list.
833    If-conversion suitable order is, breadth first sort (BFS) order
834    with an additional constraint: select a block only if all its
835    predecessors are already selected.  */
836
837 static basic_block *
838 get_loop_body_in_if_conv_order (const struct loop *loop)
839 {
840   basic_block *blocks, *blocks_in_bfs_order;
841   basic_block bb;
842   bitmap visited;
843   unsigned int index = 0;
844   unsigned int visited_count = 0;
845
846   gcc_assert (loop->num_nodes);
847   gcc_assert (loop->latch != EXIT_BLOCK_PTR);
848
849   blocks = XCNEWVEC (basic_block, loop->num_nodes);
850   visited = BITMAP_ALLOC (NULL);
851
852   blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
853
854   index = 0;
855   while (index < loop->num_nodes)
856     {
857       bb = blocks_in_bfs_order [index];
858
859       if (bb->flags & BB_IRREDUCIBLE_LOOP)
860         {
861           free (blocks_in_bfs_order);
862           BITMAP_FREE (visited);
863           free (blocks);
864           return NULL;
865         }
866
867       if (!bitmap_bit_p (visited, bb->index))
868         {
869           if (pred_blocks_visited_p (bb, &visited)
870               || bb == loop->header)
871             {
872               /* This block is now visited.  */
873               bitmap_set_bit (visited, bb->index);
874               blocks[visited_count++] = bb;
875             }
876         }
877
878       index++;
879
880       if (index == loop->num_nodes
881           && visited_count != loop->num_nodes)
882         /* Not done yet.  */
883         index = 0;
884     }
885   free (blocks_in_bfs_order);
886   BITMAP_FREE (visited);
887   return blocks;
888 }
889
890 /* Returns true when the analysis of the predicates for all the basic
891    blocks in LOOP succeeded.
892
893    predicate_bbs first allocates the predicates of the basic blocks.
894    These fields are then initialized with the tree expressions
895    representing the predicates under which a basic block is executed
896    in the LOOP.  As the loop->header is executed at each iteration, it
897    has the "true" predicate.  Other statements executed under a
898    condition are predicated with that condition, for example
899
900    | if (x)
901    |   S1;
902    | else
903    |   S2;
904
905    S1 will be predicated with "x", and
906    S2 will be predicated with "!x".  */
907
908 static bool
909 predicate_bbs (loop_p loop)
910 {
911   unsigned int i;
912
913   for (i = 0; i < loop->num_nodes; i++)
914     init_bb_predicate (ifc_bbs[i]);
915
916   for (i = 0; i < loop->num_nodes; i++)
917     {
918       basic_block bb = ifc_bbs[i];
919       tree cond;
920       gimple_stmt_iterator itr;
921
922       /* The loop latch is always executed and has no extra conditions
923          to be processed: skip it.  */
924       if (bb == loop->latch)
925         {
926           reset_bb_predicate (loop->latch);
927           continue;
928         }
929
930       cond = bb_predicate (bb);
931       if (cond
932           && bb != loop->header)
933         {
934           gimple_seq stmts;
935
936           cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
937           add_bb_predicate_gimplified_stmts (bb, stmts);
938         }
939
940       for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
941         {
942           gimple stmt = gsi_stmt (itr);
943
944           switch (gimple_code (stmt))
945             {
946             case GIMPLE_LABEL:
947             case GIMPLE_ASSIGN:
948             case GIMPLE_CALL:
949             case GIMPLE_DEBUG:
950               break;
951
952             case GIMPLE_COND:
953               {
954                 tree c2, tem;
955                 edge true_edge, false_edge;
956                 location_t loc = gimple_location (stmt);
957                 tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
958                                           boolean_type_node,
959                                           gimple_cond_lhs (stmt),
960                                           gimple_cond_rhs (stmt));
961
962                 /* Add new condition into destination's predicate list.  */
963                 extract_true_false_edges_from_block (gimple_bb (stmt),
964                                                      &true_edge, &false_edge);
965
966                 /* If C is true, then TRUE_EDGE is taken.  */
967                 add_to_dst_predicate_list (loop, true_edge, cond, unshare_expr (c));
968
969                 /* If C is false, then FALSE_EDGE is taken.  */
970                 c2 = invert_truthvalue_loc (loc, unshare_expr (c));
971                 tem = canonicalize_cond_expr_cond (c2);
972                 if (tem)
973                   c2 = tem;
974                 add_to_dst_predicate_list (loop, false_edge, cond, c2);
975
976                 cond = NULL_TREE;
977                 break;
978               }
979
980             default:
981               /* Not handled yet in if-conversion.  */
982               return false;
983             }
984         }
985
986       /* If current bb has only one successor, then consider it as an
987          unconditional goto.  */
988       if (single_succ_p (bb))
989         {
990           basic_block bb_n = single_succ (bb);
991
992           /* The successor bb inherits the predicate of its
993              predecessor.  If there is no predicate in the predecessor
994              bb, then consider the successor bb as always executed.  */
995           if (cond == NULL_TREE)
996             cond = boolean_true_node;
997
998           add_to_predicate_list (bb_n, cond);
999         }
1000     }
1001
1002   /* The loop header is always executed.  */
1003   reset_bb_predicate (loop->header);
1004   gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1005               && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1006
1007   return true;
1008 }
1009
1010 /* Return true when LOOP is if-convertible.  This is a helper function
1011    for if_convertible_loop_p.  REFS and DDRS are initialized and freed
1012    in if_convertible_loop_p.  */
1013
1014 static bool
1015 if_convertible_loop_p_1 (struct loop *loop,
1016                          VEC (loop_p, heap) **loop_nest,
1017                          VEC (data_reference_p, heap) **refs,
1018                          VEC (ddr_p, heap) **ddrs)
1019 {
1020   bool res;
1021   unsigned int i;
1022   basic_block exit_bb = NULL;
1023
1024   /* Don't if-convert the loop when the data dependences cannot be
1025      computed: the loop won't be vectorized in that case.  */
1026   res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1027   if (!res)
1028     return false;
1029
1030   calculate_dominance_info (CDI_DOMINATORS);
1031   calculate_dominance_info (CDI_POST_DOMINATORS);
1032
1033   /* Allow statements that can be handled during if-conversion.  */
1034   ifc_bbs = get_loop_body_in_if_conv_order (loop);
1035   if (!ifc_bbs)
1036     {
1037       if (dump_file && (dump_flags & TDF_DETAILS))
1038         fprintf (dump_file, "Irreducible loop\n");
1039       return false;
1040     }
1041
1042   for (i = 0; i < loop->num_nodes; i++)
1043     {
1044       basic_block bb = ifc_bbs[i];
1045
1046       if (!if_convertible_bb_p (loop, bb, exit_bb))
1047         return false;
1048
1049       if (bb_with_exit_edge_p (loop, bb))
1050         exit_bb = bb;
1051     }
1052
1053   res = predicate_bbs (loop);
1054   if (!res)
1055     return false;
1056
1057   if (flag_tree_loop_if_convert_stores)
1058     {
1059       data_reference_p dr;
1060
1061       for (i = 0; VEC_iterate (data_reference_p, *refs, i, dr); i++)
1062         {
1063           dr->aux = XNEW (struct ifc_dr);
1064           DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1065           DR_RW_UNCONDITIONALLY (dr) = -1;
1066         }
1067     }
1068
1069   for (i = 0; i < loop->num_nodes; i++)
1070     {
1071       basic_block bb = ifc_bbs[i];
1072       gimple_stmt_iterator itr;
1073
1074       for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1075         if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
1076           return false;
1077
1078       /* Check the if-convertibility of statements in predicated BBs.  */
1079       if (is_predicated (bb))
1080         for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1081           if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
1082             return false;
1083     }
1084
1085   if (dump_file)
1086     fprintf (dump_file, "Applying if-conversion\n");
1087
1088   return true;
1089 }
1090
1091 /* Return true when LOOP is if-convertible.
1092    LOOP is if-convertible if:
1093    - it is innermost,
1094    - it has two or more basic blocks,
1095    - it has only one exit,
1096    - loop header is not the exit edge,
1097    - if its basic blocks and phi nodes are if convertible.  */
1098
1099 static bool
1100 if_convertible_loop_p (struct loop *loop)
1101 {
1102   edge e;
1103   edge_iterator ei;
1104   bool res = false;
1105   VEC (data_reference_p, heap) *refs;
1106   VEC (ddr_p, heap) *ddrs;
1107   VEC (loop_p, heap) *loop_nest;
1108
1109   /* Handle only innermost loop.  */
1110   if (!loop || loop->inner)
1111     {
1112       if (dump_file && (dump_flags & TDF_DETAILS))
1113         fprintf (dump_file, "not innermost loop\n");
1114       return false;
1115     }
1116
1117   /* If only one block, no need for if-conversion.  */
1118   if (loop->num_nodes <= 2)
1119     {
1120       if (dump_file && (dump_flags & TDF_DETAILS))
1121         fprintf (dump_file, "less than 2 basic blocks\n");
1122       return false;
1123     }
1124
1125   /* More than one loop exit is too much to handle.  */
1126   if (!single_exit (loop))
1127     {
1128       if (dump_file && (dump_flags & TDF_DETAILS))
1129         fprintf (dump_file, "multiple exits\n");
1130       return false;
1131     }
1132
1133   /* If one of the loop header's edge is an exit edge then do not
1134      apply if-conversion.  */
1135   FOR_EACH_EDGE (e, ei, loop->header->succs)
1136     if (loop_exit_edge_p (loop, e))
1137       return false;
1138
1139   refs = VEC_alloc (data_reference_p, heap, 5);
1140   ddrs = VEC_alloc (ddr_p, heap, 25);
1141   loop_nest = VEC_alloc (loop_p, heap, 3);
1142   res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs);
1143
1144   if (flag_tree_loop_if_convert_stores)
1145     {
1146       data_reference_p dr;
1147       unsigned int i;
1148
1149       for (i = 0; VEC_iterate (data_reference_p, refs, i, dr); i++)
1150         free (dr->aux);
1151     }
1152
1153   VEC_free (loop_p, heap, loop_nest);
1154   free_data_refs (refs);
1155   free_dependence_relations (ddrs);
1156   return res;
1157 }
1158
1159 /* Basic block BB has two predecessors.  Using predecessor's bb
1160    predicate, set an appropriate condition COND for the PHI node
1161    replacement.  Return the true block whose phi arguments are
1162    selected when cond is true.  LOOP is the loop containing the
1163    if-converted region, GSI is the place to insert the code for the
1164    if-conversion.  */
1165
1166 static basic_block
1167 find_phi_replacement_condition (struct loop *loop,
1168                                 basic_block bb, tree *cond,
1169                                 gimple_stmt_iterator *gsi)
1170 {
1171   edge first_edge, second_edge;
1172   tree tmp_cond;
1173
1174   gcc_assert (EDGE_COUNT (bb->preds) == 2);
1175   first_edge = EDGE_PRED (bb, 0);
1176   second_edge = EDGE_PRED (bb, 1);
1177
1178   /* Use condition based on following criteria:
1179      1)
1180        S1: x = !c ? a : b;
1181
1182        S2: x = c ? b : a;
1183
1184        S2 is preferred over S1. Make 'b' first_bb and use its condition.
1185
1186      2) Do not make loop header first_bb.
1187
1188      3)
1189        S1: x = !(c == d)? a : b;
1190
1191        S21: t1 = c == d;
1192        S22: x = t1 ? b : a;
1193
1194        S3: x = (c == d) ? b : a;
1195
1196        S3 is preferred over S1 and S2*, Make 'b' first_bb and use
1197        its condition.
1198
1199      4) If  pred B is dominated by pred A then use pred B's condition.
1200         See PR23115.  */
1201
1202   /* Select condition that is not TRUTH_NOT_EXPR.  */
1203   tmp_cond = bb_predicate (first_edge->src);
1204   gcc_assert (tmp_cond);
1205
1206   if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1207     {
1208       edge tmp_edge;
1209
1210       tmp_edge = first_edge;
1211       first_edge = second_edge;
1212       second_edge = tmp_edge;
1213     }
1214
1215   /* Check if FIRST_BB is loop header or not and make sure that
1216      FIRST_BB does not dominate SECOND_BB.  */
1217   if (first_edge->src == loop->header
1218       || dominated_by_p (CDI_DOMINATORS,
1219                          second_edge->src, first_edge->src))
1220     {
1221       *cond = bb_predicate (second_edge->src);
1222
1223       if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1224         *cond = invert_truthvalue (*cond);
1225       else
1226         /* Select non loop header bb.  */
1227         first_edge = second_edge;
1228     }
1229   else
1230     *cond = bb_predicate (first_edge->src);
1231
1232   /* Gimplify the condition: the vectorizer prefers to have gimple
1233      values as conditions.  Various targets use different means to
1234      communicate conditions in vector compare operations.  Using a
1235      gimple value allows the compiler to emit vector compare and
1236      select RTL without exposing compare's result.  */
1237   *cond = force_gimple_operand_gsi (gsi, unshare_expr (*cond),
1238                                     false, NULL_TREE,
1239                                     true, GSI_SAME_STMT);
1240   if (!is_gimple_reg (*cond) && !is_gimple_condexpr (*cond))
1241     *cond = ifc_temp_var (TREE_TYPE (*cond), unshare_expr (*cond), gsi);
1242
1243   gcc_assert (*cond);
1244
1245   return first_edge->src;
1246 }
1247
1248 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1249    This routine does not handle PHI nodes with more than two
1250    arguments.
1251
1252    For example,
1253      S1: A = PHI <x1(1), x2(5)
1254    is converted into,
1255      S2: A = cond ? x1 : x2;
1256
1257    The generated code is inserted at GSI that points to the top of
1258    basic block's statement list.  When COND is true, phi arg from
1259    TRUE_BB is selected.  */
1260
1261 static void
1262 predicate_scalar_phi (gimple phi, tree cond,
1263                       basic_block true_bb,
1264                       gimple_stmt_iterator *gsi)
1265 {
1266   gimple new_stmt;
1267   basic_block bb;
1268   tree rhs, res, arg, scev;
1269
1270   gcc_assert (gimple_code (phi) == GIMPLE_PHI
1271               && gimple_phi_num_args (phi) == 2);
1272
1273   res = gimple_phi_result (phi);
1274   /* Do not handle virtual phi nodes.  */
1275   if (!is_gimple_reg (SSA_NAME_VAR (res)))
1276     return;
1277
1278   bb = gimple_bb (phi);
1279
1280   if ((arg = degenerate_phi_result (phi))
1281       || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1282                                             res))
1283           && !chrec_contains_undetermined (scev)
1284           && scev != res
1285           && (arg = gimple_phi_arg_def (phi, 0))))
1286     rhs = arg;
1287   else
1288     {
1289       tree arg_0, arg_1;
1290       /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr.  */
1291       if (EDGE_PRED (bb, 1)->src == true_bb)
1292         {
1293           arg_0 = gimple_phi_arg_def (phi, 1);
1294           arg_1 = gimple_phi_arg_def (phi, 0);
1295         }
1296       else
1297         {
1298           arg_0 = gimple_phi_arg_def (phi, 0);
1299           arg_1 = gimple_phi_arg_def (phi, 1);
1300         }
1301
1302       gcc_checking_assert (bb == bb->loop_father->header
1303                            || bb_postdominates_preds (bb));
1304
1305       /* Build new RHS using selected condition and arguments.  */
1306       rhs = build3 (COND_EXPR, TREE_TYPE (res),
1307                     unshare_expr (cond), arg_0, arg_1);
1308     }
1309
1310   new_stmt = gimple_build_assign (res, rhs);
1311   SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
1312   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1313   update_stmt (new_stmt);
1314
1315   if (dump_file && (dump_flags & TDF_DETAILS))
1316     {
1317       fprintf (dump_file, "new phi replacement stmt\n");
1318       print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1319     }
1320 }
1321
1322 /* Replaces in LOOP all the scalar phi nodes other than those in the
1323    LOOP->header block with conditional modify expressions.  */
1324
1325 static void
1326 predicate_all_scalar_phis (struct loop *loop)
1327 {
1328   basic_block bb;
1329   unsigned int orig_loop_num_nodes = loop->num_nodes;
1330   unsigned int i;
1331
1332   for (i = 1; i < orig_loop_num_nodes; i++)
1333     {
1334       gimple phi;
1335       tree cond = NULL_TREE;
1336       gimple_stmt_iterator gsi, phi_gsi;
1337       basic_block true_bb = NULL;
1338       bb = ifc_bbs[i];
1339
1340       if (bb == loop->header)
1341         continue;
1342
1343       phi_gsi = gsi_start_phis (bb);
1344       if (gsi_end_p (phi_gsi))
1345         continue;
1346
1347       /* BB has two predecessors.  Using predecessor's aux field, set
1348          appropriate condition for the PHI node replacement.  */
1349       gsi = gsi_after_labels (bb);
1350       true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
1351
1352       while (!gsi_end_p (phi_gsi))
1353         {
1354           phi = gsi_stmt (phi_gsi);
1355           predicate_scalar_phi (phi, cond, true_bb, &gsi);
1356           release_phi_node (phi);
1357           gsi_next (&phi_gsi);
1358         }
1359
1360       set_phi_nodes (bb, NULL);
1361     }
1362 }
1363
1364 /* Insert in each basic block of LOOP the statements produced by the
1365    gimplification of the predicates.  */
1366
1367 static void
1368 insert_gimplified_predicates (loop_p loop)
1369 {
1370   unsigned int i;
1371
1372   for (i = 0; i < loop->num_nodes; i++)
1373     {
1374       basic_block bb = ifc_bbs[i];
1375       gimple_seq stmts;
1376
1377       if (!is_predicated (bb))
1378         {
1379           /* Do not insert statements for a basic block that is not
1380              predicated.  Also make sure that the predicate of the
1381              basic block is set to true.  */
1382           reset_bb_predicate (bb);
1383           continue;
1384         }
1385
1386       stmts = bb_predicate_gimplified_stmts (bb);
1387       if (stmts)
1388         {
1389           if (flag_tree_loop_if_convert_stores)
1390             {
1391               /* Insert the predicate of the BB just after the label,
1392                  as the if-conversion of memory writes will use this
1393                  predicate.  */
1394               gimple_stmt_iterator gsi = gsi_after_labels (bb);
1395               gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1396             }
1397           else
1398             {
1399               /* Insert the predicate of the BB at the end of the BB
1400                  as this would reduce the register pressure: the only
1401                  use of this predicate will be in successor BBs.  */
1402               gimple_stmt_iterator gsi = gsi_last_bb (bb);
1403
1404               if (gsi_end_p (gsi)
1405                   || stmt_ends_bb_p (gsi_stmt (gsi)))
1406                 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1407               else
1408                 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1409             }
1410
1411           /* Once the sequence is code generated, set it to NULL.  */
1412           set_bb_predicate_gimplified_stmts (bb, NULL);
1413         }
1414     }
1415 }
1416
1417 /* Predicate each write to memory in LOOP.
1418
1419    This function transforms control flow constructs containing memory
1420    writes of the form:
1421
1422    | for (i = 0; i < N; i++)
1423    |   if (cond)
1424    |     A[i] = expr;
1425
1426    into the following form that does not contain control flow:
1427
1428    | for (i = 0; i < N; i++)
1429    |   A[i] = cond ? expr : A[i];
1430
1431    The original CFG looks like this:
1432
1433    | bb_0
1434    |   i = 0
1435    | end_bb_0
1436    |
1437    | bb_1
1438    |   if (i < N) goto bb_5 else goto bb_2
1439    | end_bb_1
1440    |
1441    | bb_2
1442    |   cond = some_computation;
1443    |   if (cond) goto bb_3 else goto bb_4
1444    | end_bb_2
1445    |
1446    | bb_3
1447    |   A[i] = expr;
1448    |   goto bb_4
1449    | end_bb_3
1450    |
1451    | bb_4
1452    |   goto bb_1
1453    | end_bb_4
1454
1455    insert_gimplified_predicates inserts the computation of the COND
1456    expression at the beginning of the destination basic block:
1457
1458    | bb_0
1459    |   i = 0
1460    | end_bb_0
1461    |
1462    | bb_1
1463    |   if (i < N) goto bb_5 else goto bb_2
1464    | end_bb_1
1465    |
1466    | bb_2
1467    |   cond = some_computation;
1468    |   if (cond) goto bb_3 else goto bb_4
1469    | end_bb_2
1470    |
1471    | bb_3
1472    |   cond = some_computation;
1473    |   A[i] = expr;
1474    |   goto bb_4
1475    | end_bb_3
1476    |
1477    | bb_4
1478    |   goto bb_1
1479    | end_bb_4
1480
1481    predicate_mem_writes is then predicating the memory write as follows:
1482
1483    | bb_0
1484    |   i = 0
1485    | end_bb_0
1486    |
1487    | bb_1
1488    |   if (i < N) goto bb_5 else goto bb_2
1489    | end_bb_1
1490    |
1491    | bb_2
1492    |   if (cond) goto bb_3 else goto bb_4
1493    | end_bb_2
1494    |
1495    | bb_3
1496    |   cond = some_computation;
1497    |   A[i] = cond ? expr : A[i];
1498    |   goto bb_4
1499    | end_bb_3
1500    |
1501    | bb_4
1502    |   goto bb_1
1503    | end_bb_4
1504
1505    and finally combine_blocks removes the basic block boundaries making
1506    the loop vectorizable:
1507
1508    | bb_0
1509    |   i = 0
1510    |   if (i < N) goto bb_5 else goto bb_1
1511    | end_bb_0
1512    |
1513    | bb_1
1514    |   cond = some_computation;
1515    |   A[i] = cond ? expr : A[i];
1516    |   if (i < N) goto bb_5 else goto bb_4
1517    | end_bb_1
1518    |
1519    | bb_4
1520    |   goto bb_1
1521    | end_bb_4
1522 */
1523
1524 static void
1525 predicate_mem_writes (loop_p loop)
1526 {
1527   unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1528
1529   for (i = 1; i < orig_loop_num_nodes; i++)
1530     {
1531       gimple_stmt_iterator gsi;
1532       basic_block bb = ifc_bbs[i];
1533       tree cond = bb_predicate (bb);
1534       gimple stmt;
1535
1536       if (is_true_predicate (cond))
1537         continue;
1538
1539       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1540         if ((stmt = gsi_stmt (gsi))
1541             && gimple_assign_single_p (stmt)
1542             && gimple_vdef (stmt))
1543           {
1544             tree lhs = gimple_assign_lhs (stmt);
1545             tree rhs = gimple_assign_rhs1 (stmt);
1546             tree type = TREE_TYPE (lhs);
1547
1548             lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1549             rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1550             rhs = build3 (COND_EXPR, type, unshare_expr (cond), rhs, lhs);
1551             gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1552             update_stmt (stmt);
1553           }
1554     }
1555 }
1556
1557 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1558    other than the exit and latch of the LOOP.  Also resets the
1559    GIMPLE_DEBUG information.  */
1560
1561 static void
1562 remove_conditions_and_labels (loop_p loop)
1563 {
1564   gimple_stmt_iterator gsi;
1565   unsigned int i;
1566
1567   for (i = 0; i < loop->num_nodes; i++)
1568     {
1569       basic_block bb = ifc_bbs[i];
1570
1571       if (bb_with_exit_edge_p (loop, bb)
1572         || bb == loop->latch)
1573       continue;
1574
1575       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1576         switch (gimple_code (gsi_stmt (gsi)))
1577           {
1578           case GIMPLE_COND:
1579           case GIMPLE_LABEL:
1580             gsi_remove (&gsi, true);
1581             break;
1582
1583           case GIMPLE_DEBUG:
1584             /* ??? Should there be conditional GIMPLE_DEBUG_BINDs?  */
1585             if (gimple_debug_bind_p (gsi_stmt (gsi)))
1586               {
1587                 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1588                 update_stmt (gsi_stmt (gsi));
1589               }
1590             gsi_next (&gsi);
1591             break;
1592
1593           default:
1594             gsi_next (&gsi);
1595           }
1596     }
1597 }
1598
1599 /* Combine all the basic blocks from LOOP into one or two super basic
1600    blocks.  Replace PHI nodes with conditional modify expressions.  */
1601
1602 static void
1603 combine_blocks (struct loop *loop)
1604 {
1605   basic_block bb, exit_bb, merge_target_bb;
1606   unsigned int orig_loop_num_nodes = loop->num_nodes;
1607   unsigned int i;
1608   edge e;
1609   edge_iterator ei;
1610
1611   remove_conditions_and_labels (loop);
1612   insert_gimplified_predicates (loop);
1613   predicate_all_scalar_phis (loop);
1614
1615   if (flag_tree_loop_if_convert_stores)
1616     predicate_mem_writes (loop);
1617
1618   /* Merge basic blocks: first remove all the edges in the loop,
1619      except for those from the exit block.  */
1620   exit_bb = NULL;
1621   for (i = 0; i < orig_loop_num_nodes; i++)
1622     {
1623       bb = ifc_bbs[i];
1624       if (bb_with_exit_edge_p (loop, bb))
1625         {
1626           exit_bb = bb;
1627           break;
1628         }
1629     }
1630   gcc_assert (exit_bb != loop->latch);
1631
1632   for (i = 1; i < orig_loop_num_nodes; i++)
1633     {
1634       bb = ifc_bbs[i];
1635
1636       for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
1637         {
1638           if (e->src == exit_bb)
1639             ei_next (&ei);
1640           else
1641             remove_edge (e);
1642         }
1643     }
1644
1645   if (exit_bb != NULL)
1646     {
1647       if (exit_bb != loop->header)
1648         {
1649           /* Connect this node to loop header.  */
1650           make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
1651           set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
1652         }
1653
1654       /* Redirect non-exit edges to loop->latch.  */
1655       FOR_EACH_EDGE (e, ei, exit_bb->succs)
1656         {
1657           if (!loop_exit_edge_p (loop, e))
1658             redirect_edge_and_branch (e, loop->latch);
1659         }
1660       set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
1661     }
1662   else
1663     {
1664       /* If the loop does not have an exit, reconnect header and latch.  */
1665       make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
1666       set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
1667     }
1668
1669   merge_target_bb = loop->header;
1670   for (i = 1; i < orig_loop_num_nodes; i++)
1671     {
1672       gimple_stmt_iterator gsi;
1673       gimple_stmt_iterator last;
1674
1675       bb = ifc_bbs[i];
1676
1677       if (bb == exit_bb || bb == loop->latch)
1678         continue;
1679
1680       /* Make stmts member of loop->header.  */
1681       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1682         gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
1683
1684       /* Update stmt list.  */
1685       last = gsi_last_bb (merge_target_bb);
1686       gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
1687       set_bb_seq (bb, NULL);
1688
1689       delete_basic_block (bb);
1690     }
1691
1692   /* If possible, merge loop header to the block with the exit edge.
1693      This reduces the number of basic blocks to two, to please the
1694      vectorizer that handles only loops with two nodes.  */
1695   if (exit_bb
1696       && exit_bb != loop->header
1697       && can_merge_blocks_p (loop->header, exit_bb))
1698     merge_blocks (loop->header, exit_bb);
1699 }
1700
1701 /* If-convert LOOP when it is legal.  For the moment this pass has no
1702    profitability analysis.  Returns true when something changed.  */
1703
1704 static bool
1705 tree_if_conversion (struct loop *loop)
1706 {
1707   bool changed = false;
1708   ifc_bbs = NULL;
1709
1710   if (!if_convertible_loop_p (loop)
1711       || !dbg_cnt (if_conversion_tree))
1712     goto cleanup;
1713
1714   /* Now all statements are if-convertible.  Combine all the basic
1715      blocks into one huge basic block doing the if-conversion
1716      on-the-fly.  */
1717   combine_blocks (loop);
1718
1719   if (flag_tree_loop_if_convert_stores)
1720     mark_sym_for_renaming (gimple_vop (cfun));
1721
1722   changed = true;
1723
1724  cleanup:
1725   if (ifc_bbs)
1726     {
1727       unsigned int i;
1728
1729       for (i = 0; i < loop->num_nodes; i++)
1730         free_bb_predicate (ifc_bbs[i]);
1731
1732       free (ifc_bbs);
1733       ifc_bbs = NULL;
1734     }
1735
1736   return changed;
1737 }
1738
1739 /* Tree if-conversion pass management.  */
1740
1741 static unsigned int
1742 main_tree_if_conversion (void)
1743 {
1744   loop_iterator li;
1745   struct loop *loop;
1746   bool changed = false;
1747   unsigned todo = 0;
1748
1749   if (number_of_loops () <= 1)
1750     return 0;
1751
1752   FOR_EACH_LOOP (li, loop, 0)
1753     changed |= tree_if_conversion (loop);
1754
1755   if (changed)
1756     todo |= TODO_cleanup_cfg;
1757
1758   if (changed && flag_tree_loop_if_convert_stores)
1759     todo |= TODO_update_ssa_only_virtuals;
1760
1761   free_dominance_info (CDI_POST_DOMINATORS);
1762
1763   return todo;
1764 }
1765
1766 /* Returns true when the if-conversion pass is enabled.  */
1767
1768 static bool
1769 gate_tree_if_conversion (void)
1770 {
1771   return ((flag_tree_vectorize && flag_tree_loop_if_convert != 0)
1772           || flag_tree_loop_if_convert == 1
1773           || flag_tree_loop_if_convert_stores == 1);
1774 }
1775
1776 struct gimple_opt_pass pass_if_conversion =
1777 {
1778  {
1779   GIMPLE_PASS,
1780   "ifcvt",                              /* name */
1781   gate_tree_if_conversion,              /* gate */
1782   main_tree_if_conversion,              /* execute */
1783   NULL,                                 /* sub */
1784   NULL,                                 /* next */
1785   0,                                    /* static_pass_number */
1786   TV_NONE,                              /* tv_id */
1787   PROP_cfg | PROP_ssa,                  /* properties_required */
1788   0,                                    /* properties_provided */
1789   0,                                    /* properties_destroyed */
1790   0,                                    /* todo_flags_start */
1791   TODO_dump_func | TODO_verify_stmts | TODO_verify_flow
1792                                         /* todo_flags_finish */
1793  }
1794 };