genattrtab.c (write_header): Include hash-set.h...
[platform/upstream/gcc.git] / gcc / tree-if-conv.c
1 /* If-conversion for vectorizer.
2    Copyright (C) 2004-2015 Free Software Foundation, Inc.
3    Contributed by Devang Patel <dpatel@apple.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 /* This pass implements a tree level if-conversion of loops.  Its
22    initial goal is to help the vectorizer to vectorize loops with
23    conditions.
24
25    A short description of if-conversion:
26
27      o Decide if a loop is if-convertible or not.
28      o Walk all loop basic blocks in breadth first order (BFS order).
29        o Remove conditional statements (at the end of basic block)
30          and propagate condition into destination basic blocks'
31          predicate list.
32        o Replace modify expression with conditional modify expression
33          using current basic block's condition.
34      o Merge all basic blocks
35        o Replace phi nodes with conditional modify expr
36        o Merge all basic blocks into header
37
38      Sample transformation:
39
40      INPUT
41      -----
42
43      # i_23 = PHI <0(0), i_18(10)>;
44      <L0>:;
45      j_15 = A[i_23];
46      if (j_15 > 41) goto <L1>; else goto <L17>;
47
48      <L17>:;
49      goto <bb 3> (<L3>);
50
51      <L1>:;
52
53      # iftmp.2_4 = PHI <0(8), 42(2)>;
54      <L3>:;
55      A[i_23] = iftmp.2_4;
56      i_18 = i_23 + 1;
57      if (i_18 <= 15) goto <L19>; else goto <L18>;
58
59      <L19>:;
60      goto <bb 1> (<L0>);
61
62      <L18>:;
63
64      OUTPUT
65      ------
66
67      # i_23 = PHI <0(0), i_18(10)>;
68      <L0>:;
69      j_15 = A[i_23];
70
71      <L3>:;
72      iftmp.2_4 = j_15 > 41 ? 42 : 0;
73      A[i_23] = iftmp.2_4;
74      i_18 = i_23 + 1;
75      if (i_18 <= 15) goto <L19>; else goto <L18>;
76
77      <L19>:;
78      goto <bb 1> (<L0>);
79
80      <L18>:;
81 */
82
83 #include "config.h"
84 #include "system.h"
85 #include "coretypes.h"
86 #include "tm.h"
87 #include "hash-set.h"
88 #include "machmode.h"
89 #include "vec.h"
90 #include "double-int.h"
91 #include "input.h"
92 #include "alias.h"
93 #include "symtab.h"
94 #include "wide-int.h"
95 #include "inchash.h"
96 #include "tree.h"
97 #include "fold-const.h"
98 #include "stor-layout.h"
99 #include "flags.h"
100 #include "predict.h"
101 #include "hard-reg-set.h"
102 #include "input.h"
103 #include "function.h"
104 #include "dominance.h"
105 #include "cfg.h"
106 #include "basic-block.h"
107 #include "gimple-pretty-print.h"
108 #include "tree-ssa-alias.h"
109 #include "internal-fn.h"
110 #include "gimple-fold.h"
111 #include "gimple-expr.h"
112 #include "is-a.h"
113 #include "gimple.h"
114 #include "gimplify.h"
115 #include "gimple-iterator.h"
116 #include "gimplify-me.h"
117 #include "gimple-ssa.h"
118 #include "tree-cfg.h"
119 #include "tree-phinodes.h"
120 #include "ssa-iterators.h"
121 #include "stringpool.h"
122 #include "tree-ssanames.h"
123 #include "tree-into-ssa.h"
124 #include "tree-ssa.h"
125 #include "cfgloop.h"
126 #include "tree-chrec.h"
127 #include "tree-data-ref.h"
128 #include "tree-scalar-evolution.h"
129 #include "tree-ssa-loop-ivopts.h"
130 #include "tree-ssa-address.h"
131 #include "tree-pass.h"
132 #include "dbgcnt.h"
133 #include "expr.h"
134 #include "insn-codes.h"
135 #include "optabs.h"
136
137 /* List of basic blocks in if-conversion-suitable order.  */
138 static basic_block *ifc_bbs;
139
140 /* Structure used to predicate basic blocks.  This is attached to the
141    ->aux field of the BBs in the loop to be if-converted.  */
142 typedef struct bb_predicate_s {
143
144   /* The condition under which this basic block is executed.  */
145   tree predicate;
146
147   /* PREDICATE is gimplified, and the sequence of statements is
148      recorded here, in order to avoid the duplication of computations
149      that occur in previous conditions.  See PR44483.  */
150   gimple_seq predicate_gimplified_stmts;
151 } *bb_predicate_p;
152
153 /* Returns true when the basic block BB has a predicate.  */
154
155 static inline bool
156 bb_has_predicate (basic_block bb)
157 {
158   return bb->aux != NULL;
159 }
160
161 /* Returns the gimplified predicate for basic block BB.  */
162
163 static inline tree
164 bb_predicate (basic_block bb)
165 {
166   return ((bb_predicate_p) bb->aux)->predicate;
167 }
168
169 /* Sets the gimplified predicate COND for basic block BB.  */
170
171 static inline void
172 set_bb_predicate (basic_block bb, tree cond)
173 {
174   gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
175                && is_gimple_condexpr (TREE_OPERAND (cond, 0)))
176               || is_gimple_condexpr (cond));
177   ((bb_predicate_p) bb->aux)->predicate = cond;
178 }
179
180 /* Returns the sequence of statements of the gimplification of the
181    predicate for basic block BB.  */
182
183 static inline gimple_seq
184 bb_predicate_gimplified_stmts (basic_block bb)
185 {
186   return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
187 }
188
189 /* Sets the sequence of statements STMTS of the gimplification of the
190    predicate for basic block BB.  */
191
192 static inline void
193 set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
194 {
195   ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
196 }
197
198 /* Adds the sequence of statements STMTS to the sequence of statements
199    of the predicate for basic block BB.  */
200
201 static inline void
202 add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
203 {
204   gimple_seq_add_seq
205     (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
206 }
207
208 /* Initializes to TRUE the predicate of basic block BB.  */
209
210 static inline void
211 init_bb_predicate (basic_block bb)
212 {
213   bb->aux = XNEW (struct bb_predicate_s);
214   set_bb_predicate_gimplified_stmts (bb, NULL);
215   set_bb_predicate (bb, boolean_true_node);
216 }
217
218 /* Release the SSA_NAMEs associated with the predicate of basic block BB,
219    but don't actually free it.  */
220
221 static inline void
222 release_bb_predicate (basic_block bb)
223 {
224   gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
225   if (stmts)
226     {
227       gimple_stmt_iterator i;
228
229       for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
230         free_stmt_operands (cfun, gsi_stmt (i));
231       set_bb_predicate_gimplified_stmts (bb, NULL);
232     }
233 }
234
235 /* Free the predicate of basic block BB.  */
236
237 static inline void
238 free_bb_predicate (basic_block bb)
239 {
240   if (!bb_has_predicate (bb))
241     return;
242
243   release_bb_predicate (bb);
244   free (bb->aux);
245   bb->aux = NULL;
246 }
247
248 /* Reinitialize predicate of BB with the true predicate.  */
249
250 static inline void
251 reset_bb_predicate (basic_block bb)
252 {
253   if (!bb_has_predicate (bb))
254     init_bb_predicate (bb);
255   else
256     {
257       release_bb_predicate (bb);
258       set_bb_predicate (bb, boolean_true_node);
259     }
260 }
261
262 /* Returns a new SSA_NAME of type TYPE that is assigned the value of
263    the expression EXPR.  Inserts the statement created for this
264    computation before GSI and leaves the iterator GSI at the same
265    statement.  */
266
267 static tree
268 ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
269 {
270   tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
271   gimple stmt = gimple_build_assign (new_name, expr);
272   gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
273   return new_name;
274 }
275
276 /* Return true when COND is a true predicate.  */
277
278 static inline bool
279 is_true_predicate (tree cond)
280 {
281   return (cond == NULL_TREE
282           || cond == boolean_true_node
283           || integer_onep (cond));
284 }
285
286 /* Returns true when BB has a predicate that is not trivial: true or
287    NULL_TREE.  */
288
289 static inline bool
290 is_predicated (basic_block bb)
291 {
292   return !is_true_predicate (bb_predicate (bb));
293 }
294
295 /* Parses the predicate COND and returns its comparison code and
296    operands OP0 and OP1.  */
297
298 static enum tree_code
299 parse_predicate (tree cond, tree *op0, tree *op1)
300 {
301   gimple s;
302
303   if (TREE_CODE (cond) == SSA_NAME
304       && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
305     {
306       if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
307         {
308           *op0 = gimple_assign_rhs1 (s);
309           *op1 = gimple_assign_rhs2 (s);
310           return gimple_assign_rhs_code (s);
311         }
312
313       else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
314         {
315           tree op = gimple_assign_rhs1 (s);
316           tree type = TREE_TYPE (op);
317           enum tree_code code = parse_predicate (op, op0, op1);
318
319           return code == ERROR_MARK ? ERROR_MARK
320             : invert_tree_comparison (code, HONOR_NANS (type));
321         }
322
323       return ERROR_MARK;
324     }
325
326   if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
327     {
328       *op0 = TREE_OPERAND (cond, 0);
329       *op1 = TREE_OPERAND (cond, 1);
330       return TREE_CODE (cond);
331     }
332
333   return ERROR_MARK;
334 }
335
336 /* Returns the fold of predicate C1 OR C2 at location LOC.  */
337
338 static tree
339 fold_or_predicates (location_t loc, tree c1, tree c2)
340 {
341   tree op1a, op1b, op2a, op2b;
342   enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
343   enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
344
345   if (code1 != ERROR_MARK && code2 != ERROR_MARK)
346     {
347       tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
348                                           code2, op2a, op2b);
349       if (t)
350         return t;
351     }
352
353   return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
354 }
355
356 /* Returns true if N is either a constant or a SSA_NAME.  */
357
358 static bool
359 constant_or_ssa_name (tree n)
360 {
361   switch (TREE_CODE (n))
362     {
363       case SSA_NAME:
364       case INTEGER_CST:
365       case REAL_CST:
366       case COMPLEX_CST:
367       case VECTOR_CST:
368         return true;
369       default:
370         return false;
371     }
372 }
373
374 /* Returns either a COND_EXPR or the folded expression if the folded
375    expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
376    a constant or a SSA_NAME. */
377
378 static tree
379 fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
380 {
381   tree rhs1, lhs1, cond_expr;
382   cond_expr = fold_ternary (COND_EXPR, type, cond,
383                             rhs, lhs);
384
385   if (cond_expr == NULL_TREE)
386     return build3 (COND_EXPR, type, cond, rhs, lhs);
387
388   STRIP_USELESS_TYPE_CONVERSION (cond_expr);
389
390   if (constant_or_ssa_name (cond_expr))
391     return cond_expr;
392
393   if (TREE_CODE (cond_expr) == ABS_EXPR)
394     {
395       rhs1 = TREE_OPERAND (cond_expr, 1);
396       STRIP_USELESS_TYPE_CONVERSION (rhs1);
397       if (constant_or_ssa_name (rhs1))
398         return build1 (ABS_EXPR, type, rhs1);
399     }
400
401   if (TREE_CODE (cond_expr) == MIN_EXPR
402       || TREE_CODE (cond_expr) == MAX_EXPR)
403     {
404       lhs1 = TREE_OPERAND (cond_expr, 0);
405       STRIP_USELESS_TYPE_CONVERSION (lhs1);
406       rhs1 = TREE_OPERAND (cond_expr, 1);
407       STRIP_USELESS_TYPE_CONVERSION (rhs1);
408       if (constant_or_ssa_name (rhs1)
409           && constant_or_ssa_name (lhs1))
410         return build2 (TREE_CODE (cond_expr), type, lhs1, rhs1);
411     }
412   return build3 (COND_EXPR, type, cond, rhs, lhs);
413 }
414
415 /* Add condition NC to the predicate list of basic block BB.  LOOP is
416    the loop to be if-converted. Use predicate of cd-equivalent block
417    for join bb if it exists: we call basic blocks bb1 and bb2 
418    cd-equivalent if they are executed under the same condition.  */
419
420 static inline void
421 add_to_predicate_list (struct loop *loop, basic_block bb, tree nc)
422 {
423   tree bc, *tp;
424   basic_block dom_bb;
425
426   if (is_true_predicate (nc))
427     return;
428
429   /* If dominance tells us this basic block is always executed,
430      don't record any predicates for it.  */
431   if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
432     return;
433
434   dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
435   /* We use notion of cd equivalence to get simpler predicate for
436      join block, e.g. if join block has 2 predecessors with predicates
437      p1 & p2 and p1 & !p2, we'd like to get p1 for it instead of
438      p1 & p2 | p1 & !p2.  */
439   if (dom_bb != loop->header
440       && get_immediate_dominator (CDI_POST_DOMINATORS, dom_bb) == bb)
441     {
442       gcc_assert (flow_bb_inside_loop_p (loop, dom_bb));
443       bc = bb_predicate (dom_bb);
444       if (!is_true_predicate (bc))
445         set_bb_predicate (bb, bc);
446       else
447         gcc_assert (is_true_predicate (bb_predicate (bb)));
448       if (dump_file && (dump_flags & TDF_DETAILS))
449         fprintf (dump_file, "Use predicate of bb#%d for bb#%d\n",
450                  dom_bb->index, bb->index);
451       return;
452     }
453
454   if (!is_predicated (bb))
455     bc = nc;
456   else
457     {
458       bc = bb_predicate (bb);
459       bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
460       if (is_true_predicate (bc))
461         {
462           reset_bb_predicate (bb);
463           return;
464         }
465     }
466
467   /* Allow a TRUTH_NOT_EXPR around the main predicate.  */
468   if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
469     tp = &TREE_OPERAND (bc, 0);
470   else
471     tp = &bc;
472   if (!is_gimple_condexpr (*tp))
473     {
474       gimple_seq stmts;
475       *tp = force_gimple_operand_1 (*tp, &stmts, is_gimple_condexpr, NULL_TREE);
476       add_bb_predicate_gimplified_stmts (bb, stmts);
477     }
478   set_bb_predicate (bb, bc);
479 }
480
481 /* Add the condition COND to the previous condition PREV_COND, and add
482    this to the predicate list of the destination of edge E.  LOOP is
483    the loop to be if-converted.  */
484
485 static void
486 add_to_dst_predicate_list (struct loop *loop, edge e,
487                            tree prev_cond, tree cond)
488 {
489   if (!flow_bb_inside_loop_p (loop, e->dest))
490     return;
491
492   if (!is_true_predicate (prev_cond))
493     cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
494                         prev_cond, cond);
495
496   add_to_predicate_list (loop, e->dest, cond);
497 }
498
499 /* Return true if one of the successor edges of BB exits LOOP.  */
500
501 static bool
502 bb_with_exit_edge_p (struct loop *loop, basic_block bb)
503 {
504   edge e;
505   edge_iterator ei;
506
507   FOR_EACH_EDGE (e, ei, bb->succs)
508     if (loop_exit_edge_p (loop, e))
509       return true;
510
511   return false;
512 }
513
514 /* Return true when PHI is if-convertible.  PHI is part of loop LOOP
515    and it belongs to basic block BB.
516
517    PHI is not if-convertible if:
518    - it has more than 2 arguments.
519
520    When the flag_tree_loop_if_convert_stores is not set, PHI is not
521    if-convertible if:
522    - a virtual PHI is immediately used in another PHI node,
523    - there is a virtual PHI in a BB other than the loop->header.  */
524
525 static bool
526 if_convertible_phi_p (struct loop *loop, basic_block bb, gphi *phi,
527                       bool any_mask_load_store)
528 {
529   if (dump_file && (dump_flags & TDF_DETAILS))
530     {
531       fprintf (dump_file, "-------------------------\n");
532       print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
533     }
534
535   if (bb != loop->header && gimple_phi_num_args (phi) != 2)
536     {
537       if (dump_file && (dump_flags & TDF_DETAILS))
538         fprintf (dump_file, "More than two phi node args.\n");
539       return false;
540     }
541
542   if (flag_tree_loop_if_convert_stores || any_mask_load_store)
543     return true;
544
545   /* When the flag_tree_loop_if_convert_stores is not set, check
546      that there are no memory writes in the branches of the loop to be
547      if-converted.  */
548   if (virtual_operand_p (gimple_phi_result (phi)))
549     {
550       imm_use_iterator imm_iter;
551       use_operand_p use_p;
552
553       if (bb != loop->header)
554         {
555           if (dump_file && (dump_flags & TDF_DETAILS))
556             fprintf (dump_file, "Virtual phi not on loop->header.\n");
557           return false;
558         }
559
560       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, gimple_phi_result (phi))
561         {
562           if (gimple_code (USE_STMT (use_p)) == GIMPLE_PHI)
563             {
564               if (dump_file && (dump_flags & TDF_DETAILS))
565                 fprintf (dump_file, "Difficult to handle this virtual phi.\n");
566               return false;
567             }
568         }
569     }
570
571   return true;
572 }
573
574 /* Records the status of a data reference.  This struct is attached to
575    each DR->aux field.  */
576
577 struct ifc_dr {
578   /* -1 when not initialized, 0 when false, 1 when true.  */
579   int written_at_least_once;
580
581   /* -1 when not initialized, 0 when false, 1 when true.  */
582   int rw_unconditionally;
583 };
584
585 #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
586 #define DR_WRITTEN_AT_LEAST_ONCE(DR) (IFC_DR (DR)->written_at_least_once)
587 #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
588
589 /* Returns true when the memory references of STMT are read or written
590    unconditionally.  In other words, this function returns true when
591    for every data reference A in STMT there exist other accesses to
592    a data reference with the same base with predicates that add up (OR-up) to
593    the true predicate: this ensures that the data reference A is touched
594    (read or written) on every iteration of the if-converted loop.  */
595
596 static bool
597 memrefs_read_or_written_unconditionally (gimple stmt,
598                                          vec<data_reference_p> drs)
599 {
600   int i, j;
601   data_reference_p a, b;
602   tree ca = bb_predicate (gimple_bb (stmt));
603
604   for (i = 0; drs.iterate (i, &a); i++)
605     if (DR_STMT (a) == stmt)
606       {
607         bool found = false;
608         int x = DR_RW_UNCONDITIONALLY (a);
609
610         if (x == 0)
611           return false;
612
613         if (x == 1)
614           continue;
615
616         for (j = 0; drs.iterate (j, &b); j++)
617           {
618             tree ref_base_a = DR_REF (a);
619             tree ref_base_b = DR_REF (b);
620
621             if (DR_STMT (b) == stmt)
622               continue;
623
624             while (TREE_CODE (ref_base_a) == COMPONENT_REF
625                    || TREE_CODE (ref_base_a) == IMAGPART_EXPR
626                    || TREE_CODE (ref_base_a) == REALPART_EXPR)
627               ref_base_a = TREE_OPERAND (ref_base_a, 0);
628
629             while (TREE_CODE (ref_base_b) == COMPONENT_REF
630                    || TREE_CODE (ref_base_b) == IMAGPART_EXPR
631                    || TREE_CODE (ref_base_b) == REALPART_EXPR)
632               ref_base_b = TREE_OPERAND (ref_base_b, 0);
633
634             if (!operand_equal_p (ref_base_a, ref_base_b, 0))
635               {
636                 tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
637
638                 if (DR_RW_UNCONDITIONALLY (b) == 1
639                     || is_true_predicate (cb)
640                     || is_true_predicate (ca
641                         = fold_or_predicates (EXPR_LOCATION (cb), ca, cb)))
642                   {
643                     DR_RW_UNCONDITIONALLY (a) = 1;
644                     DR_RW_UNCONDITIONALLY (b) = 1;
645                     found = true;
646                     break;
647                   }
648                }
649             }
650
651         if (!found)
652           {
653             DR_RW_UNCONDITIONALLY (a) = 0;
654             return false;
655           }
656       }
657
658   return true;
659 }
660
661 /* Returns true when the memory references of STMT are unconditionally
662    written.  In other words, this function returns true when for every
663    data reference A written in STMT, there exist other writes to the
664    same data reference with predicates that add up (OR-up) to the true
665    predicate: this ensures that the data reference A is written on
666    every iteration of the if-converted loop.  */
667
668 static bool
669 write_memrefs_written_at_least_once (gimple stmt,
670                                      vec<data_reference_p> drs)
671 {
672   int i, j;
673   data_reference_p a, b;
674   tree ca = bb_predicate (gimple_bb (stmt));
675
676   for (i = 0; drs.iterate (i, &a); i++)
677     if (DR_STMT (a) == stmt
678         && DR_IS_WRITE (a))
679       {
680         bool found = false;
681         int x = DR_WRITTEN_AT_LEAST_ONCE (a);
682
683         if (x == 0)
684           return false;
685
686         if (x == 1)
687           continue;
688
689         for (j = 0; drs.iterate (j, &b); j++)
690           if (DR_STMT (b) != stmt
691               && DR_IS_WRITE (b)
692               && same_data_refs_base_objects (a, b))
693             {
694               tree cb = bb_predicate (gimple_bb (DR_STMT (b)));
695
696               if (DR_WRITTEN_AT_LEAST_ONCE (b) == 1
697                   || is_true_predicate (cb)
698                   || is_true_predicate (ca = fold_or_predicates (EXPR_LOCATION (cb),
699                                                                  ca, cb)))
700                 {
701                   DR_WRITTEN_AT_LEAST_ONCE (a) = 1;
702                   DR_WRITTEN_AT_LEAST_ONCE (b) = 1;
703                   found = true;
704                   break;
705                 }
706             }
707
708         if (!found)
709           {
710             DR_WRITTEN_AT_LEAST_ONCE (a) = 0;
711             return false;
712           }
713       }
714
715   return true;
716 }
717
718 /* Return true when the memory references of STMT won't trap in the
719    if-converted code.  There are two things that we have to check for:
720
721    - writes to memory occur to writable memory: if-conversion of
722    memory writes transforms the conditional memory writes into
723    unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
724    into "A[i] = cond ? foo : A[i]", and as the write to memory may not
725    be executed at all in the original code, it may be a readonly
726    memory.  To check that A is not const-qualified, we check that
727    there exists at least an unconditional write to A in the current
728    function.
729
730    - reads or writes to memory are valid memory accesses for every
731    iteration.  To check that the memory accesses are correctly formed
732    and that we are allowed to read and write in these locations, we
733    check that the memory accesses to be if-converted occur at every
734    iteration unconditionally.  */
735
736 static bool
737 ifcvt_memrefs_wont_trap (gimple stmt, vec<data_reference_p> refs)
738 {
739   return write_memrefs_written_at_least_once (stmt, refs)
740     && memrefs_read_or_written_unconditionally (stmt, refs);
741 }
742
743 /* Wrapper around gimple_could_trap_p refined for the needs of the
744    if-conversion.  Try to prove that the memory accesses of STMT could
745    not trap in the innermost loop containing STMT.  */
746
747 static bool
748 ifcvt_could_trap_p (gimple stmt, vec<data_reference_p> refs)
749 {
750   if (gimple_vuse (stmt)
751       && !gimple_could_trap_p_1 (stmt, false, false)
752       && ifcvt_memrefs_wont_trap (stmt, refs))
753     return false;
754
755   return gimple_could_trap_p (stmt);
756 }
757
758 /* Return true if STMT could be converted into a masked load or store
759    (conditional load or store based on a mask computed from bb predicate).  */
760
761 static bool
762 ifcvt_can_use_mask_load_store (gimple stmt)
763 {
764   tree lhs, ref;
765   machine_mode mode;
766   basic_block bb = gimple_bb (stmt);
767   bool is_load;
768
769   if (!(flag_tree_loop_vectorize || bb->loop_father->force_vectorize)
770       || bb->loop_father->dont_vectorize
771       || !gimple_assign_single_p (stmt)
772       || gimple_has_volatile_ops (stmt))
773     return false;
774
775   /* Check whether this is a load or store.  */
776   lhs = gimple_assign_lhs (stmt);
777   if (gimple_store_p (stmt))
778     {
779       if (!is_gimple_val (gimple_assign_rhs1 (stmt)))
780         return false;
781       is_load = false;
782       ref = lhs;
783     }
784   else if (gimple_assign_load_p (stmt))
785     {
786       is_load = true;
787       ref = gimple_assign_rhs1 (stmt);
788     }
789   else
790     return false;
791
792   if (may_be_nonaddressable_p (ref))
793     return false;
794
795   /* Mask should be integer mode of the same size as the load/store
796      mode.  */
797   mode = TYPE_MODE (TREE_TYPE (lhs));
798   if (int_mode_for_mode (mode) == BLKmode
799       || VECTOR_MODE_P (mode))
800     return false;
801
802   if (can_vec_mask_load_store_p (mode, is_load))
803     return true;
804
805   return false;
806 }
807
808 /* Return true when STMT is if-convertible.
809
810    GIMPLE_ASSIGN statement is not if-convertible if,
811    - it is not movable,
812    - it could trap,
813    - LHS is not var decl.  */
814
815 static bool
816 if_convertible_gimple_assign_stmt_p (gimple stmt,
817                                      vec<data_reference_p> refs,
818                                      bool *any_mask_load_store)
819 {
820   tree lhs = gimple_assign_lhs (stmt);
821   basic_block bb;
822
823   if (dump_file && (dump_flags & TDF_DETAILS))
824     {
825       fprintf (dump_file, "-------------------------\n");
826       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
827     }
828
829   if (!is_gimple_reg_type (TREE_TYPE (lhs)))
830     return false;
831
832   /* Some of these constrains might be too conservative.  */
833   if (stmt_ends_bb_p (stmt)
834       || gimple_has_volatile_ops (stmt)
835       || (TREE_CODE (lhs) == SSA_NAME
836           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
837       || gimple_has_side_effects (stmt))
838     {
839       if (dump_file && (dump_flags & TDF_DETAILS))
840         fprintf (dump_file, "stmt not suitable for ifcvt\n");
841       return false;
842     }
843
844   /* tree-into-ssa.c uses GF_PLF_1, so avoid it, because
845      in between if_convertible_loop_p and combine_blocks
846      we can perform loop versioning.  */
847   gimple_set_plf (stmt, GF_PLF_2, false);
848
849   if (flag_tree_loop_if_convert_stores)
850     {
851       if (ifcvt_could_trap_p (stmt, refs))
852         {
853           if (ifcvt_can_use_mask_load_store (stmt))
854             {
855               gimple_set_plf (stmt, GF_PLF_2, true);
856               *any_mask_load_store = true;
857               return true;
858             }
859           if (dump_file && (dump_flags & TDF_DETAILS))
860             fprintf (dump_file, "tree could trap...\n");
861           return false;
862         }
863       return true;
864     }
865
866   if (gimple_assign_rhs_could_trap_p (stmt))
867     {
868       if (ifcvt_can_use_mask_load_store (stmt))
869         {
870           gimple_set_plf (stmt, GF_PLF_2, true);
871           *any_mask_load_store = true;
872           return true;
873         }
874       if (dump_file && (dump_flags & TDF_DETAILS))
875         fprintf (dump_file, "tree could trap...\n");
876       return false;
877     }
878
879   bb = gimple_bb (stmt);
880
881   if (TREE_CODE (lhs) != SSA_NAME
882       && bb != bb->loop_father->header
883       && !bb_with_exit_edge_p (bb->loop_father, bb))
884     {
885       if (ifcvt_can_use_mask_load_store (stmt))
886         {
887           gimple_set_plf (stmt, GF_PLF_2, true);
888           *any_mask_load_store = true;
889           return true;
890         }
891       if (dump_file && (dump_flags & TDF_DETAILS))
892         {
893           fprintf (dump_file, "LHS is not var\n");
894           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
895         }
896       return false;
897     }
898
899   return true;
900 }
901
902 /* Return true when STMT is if-convertible.
903
904    A statement is if-convertible if:
905    - it is an if-convertible GIMPLE_ASSIGN,
906    - it is a GIMPLE_LABEL or a GIMPLE_COND.  */
907
908 static bool
909 if_convertible_stmt_p (gimple stmt, vec<data_reference_p> refs,
910                        bool *any_mask_load_store)
911 {
912   switch (gimple_code (stmt))
913     {
914     case GIMPLE_LABEL:
915     case GIMPLE_DEBUG:
916     case GIMPLE_COND:
917       return true;
918
919     case GIMPLE_ASSIGN:
920       return if_convertible_gimple_assign_stmt_p (stmt, refs,
921                                                   any_mask_load_store);
922
923     case GIMPLE_CALL:
924       {
925         tree fndecl = gimple_call_fndecl (stmt);
926         if (fndecl)
927           {
928             int flags = gimple_call_flags (stmt);
929             if ((flags & ECF_CONST)
930                 && !(flags & ECF_LOOPING_CONST_OR_PURE)
931                 /* We can only vectorize some builtins at the moment,
932                    so restrict if-conversion to those.  */
933                 && DECL_BUILT_IN (fndecl))
934               return true;
935           }
936         return false;
937       }
938
939     default:
940       /* Don't know what to do with 'em so don't do anything.  */
941       if (dump_file && (dump_flags & TDF_DETAILS))
942         {
943           fprintf (dump_file, "don't know what to do\n");
944           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
945         }
946       return false;
947       break;
948     }
949
950   return true;
951 }
952
953 /* Return true when BB is if-convertible.  This routine does not check
954    basic block's statements and phis.
955
956    A basic block is not if-convertible if:
957    - it is non-empty and it is after the exit block (in BFS order),
958    - it is after the exit block but before the latch,
959    - its edges are not normal.
960
961    EXIT_BB is the basic block containing the exit of the LOOP.  BB is
962    inside LOOP.  */
963
964 static bool
965 if_convertible_bb_p (struct loop *loop, basic_block bb, basic_block exit_bb)
966 {
967   edge e;
968   edge_iterator ei;
969
970   if (dump_file && (dump_flags & TDF_DETAILS))
971     fprintf (dump_file, "----------[%d]-------------\n", bb->index);
972
973   if (EDGE_COUNT (bb->preds) > 2
974       || EDGE_COUNT (bb->succs) > 2)
975     return false;
976
977   if (exit_bb)
978     {
979       if (bb != loop->latch)
980         {
981           if (dump_file && (dump_flags & TDF_DETAILS))
982             fprintf (dump_file, "basic block after exit bb but before latch\n");
983           return false;
984         }
985       else if (!empty_block_p (bb))
986         {
987           if (dump_file && (dump_flags & TDF_DETAILS))
988             fprintf (dump_file, "non empty basic block after exit bb\n");
989           return false;
990         }
991       else if (bb == loop->latch
992                && bb != exit_bb
993                && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
994           {
995             if (dump_file && (dump_flags & TDF_DETAILS))
996               fprintf (dump_file, "latch is not dominated by exit_block\n");
997             return false;
998           }
999     }
1000
1001   /* Be less adventurous and handle only normal edges.  */
1002   FOR_EACH_EDGE (e, ei, bb->succs)
1003     if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
1004       {
1005         if (dump_file && (dump_flags & TDF_DETAILS))
1006           fprintf (dump_file, "Difficult to handle edges\n");
1007         return false;
1008       }
1009
1010   /* At least one incoming edge has to be non-critical as otherwise edge
1011      predicates are not equal to basic-block predicates of the edge
1012      source.  */
1013   if (EDGE_COUNT (bb->preds) > 1
1014       && bb != loop->header)
1015     {
1016       bool found = false;
1017       FOR_EACH_EDGE (e, ei, bb->preds)
1018         if (EDGE_COUNT (e->src->succs) == 1)
1019           found = true;
1020       if (!found)
1021         {
1022           if (dump_file && (dump_flags & TDF_DETAILS))
1023             fprintf (dump_file, "only critical predecessors\n");
1024           return false;
1025         }
1026     }
1027
1028   return true;
1029 }
1030
1031 /* Return true when all predecessor blocks of BB are visited.  The
1032    VISITED bitmap keeps track of the visited blocks.  */
1033
1034 static bool
1035 pred_blocks_visited_p (basic_block bb, bitmap *visited)
1036 {
1037   edge e;
1038   edge_iterator ei;
1039   FOR_EACH_EDGE (e, ei, bb->preds)
1040     if (!bitmap_bit_p (*visited, e->src->index))
1041       return false;
1042
1043   return true;
1044 }
1045
1046 /* Get body of a LOOP in suitable order for if-conversion.  It is
1047    caller's responsibility to deallocate basic block list.
1048    If-conversion suitable order is, breadth first sort (BFS) order
1049    with an additional constraint: select a block only if all its
1050    predecessors are already selected.  */
1051
1052 static basic_block *
1053 get_loop_body_in_if_conv_order (const struct loop *loop)
1054 {
1055   basic_block *blocks, *blocks_in_bfs_order;
1056   basic_block bb;
1057   bitmap visited;
1058   unsigned int index = 0;
1059   unsigned int visited_count = 0;
1060
1061   gcc_assert (loop->num_nodes);
1062   gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
1063
1064   blocks = XCNEWVEC (basic_block, loop->num_nodes);
1065   visited = BITMAP_ALLOC (NULL);
1066
1067   blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
1068
1069   index = 0;
1070   while (index < loop->num_nodes)
1071     {
1072       bb = blocks_in_bfs_order [index];
1073
1074       if (bb->flags & BB_IRREDUCIBLE_LOOP)
1075         {
1076           free (blocks_in_bfs_order);
1077           BITMAP_FREE (visited);
1078           free (blocks);
1079           return NULL;
1080         }
1081
1082       if (!bitmap_bit_p (visited, bb->index))
1083         {
1084           if (pred_blocks_visited_p (bb, &visited)
1085               || bb == loop->header)
1086             {
1087               /* This block is now visited.  */
1088               bitmap_set_bit (visited, bb->index);
1089               blocks[visited_count++] = bb;
1090             }
1091         }
1092
1093       index++;
1094
1095       if (index == loop->num_nodes
1096           && visited_count != loop->num_nodes)
1097         /* Not done yet.  */
1098         index = 0;
1099     }
1100   free (blocks_in_bfs_order);
1101   BITMAP_FREE (visited);
1102   return blocks;
1103 }
1104
1105 /* Returns true when the analysis of the predicates for all the basic
1106    blocks in LOOP succeeded.
1107
1108    predicate_bbs first allocates the predicates of the basic blocks.
1109    These fields are then initialized with the tree expressions
1110    representing the predicates under which a basic block is executed
1111    in the LOOP.  As the loop->header is executed at each iteration, it
1112    has the "true" predicate.  Other statements executed under a
1113    condition are predicated with that condition, for example
1114
1115    | if (x)
1116    |   S1;
1117    | else
1118    |   S2;
1119
1120    S1 will be predicated with "x", and
1121    S2 will be predicated with "!x".  */
1122
1123 static void
1124 predicate_bbs (loop_p loop)
1125 {
1126   unsigned int i;
1127
1128   for (i = 0; i < loop->num_nodes; i++)
1129     init_bb_predicate (ifc_bbs[i]);
1130
1131   for (i = 0; i < loop->num_nodes; i++)
1132     {
1133       basic_block bb = ifc_bbs[i];
1134       tree cond;
1135       gimple stmt;
1136
1137       /* The loop latch is always executed and has no extra conditions
1138          to be processed: skip it.  */
1139       if (bb == loop->latch)
1140         {
1141           reset_bb_predicate (loop->latch);
1142           continue;
1143         }
1144
1145       cond = bb_predicate (bb);
1146       stmt = last_stmt (bb);
1147       if (stmt && gimple_code (stmt) == GIMPLE_COND)
1148         {
1149           tree c2;
1150           edge true_edge, false_edge;
1151           location_t loc = gimple_location (stmt);
1152           tree c = fold_build2_loc (loc, gimple_cond_code (stmt),
1153                                     boolean_type_node,
1154                                     gimple_cond_lhs (stmt),
1155                                     gimple_cond_rhs (stmt));
1156
1157           /* Add new condition into destination's predicate list.  */
1158           extract_true_false_edges_from_block (gimple_bb (stmt),
1159                                                &true_edge, &false_edge);
1160
1161           /* If C is true, then TRUE_EDGE is taken.  */
1162           add_to_dst_predicate_list (loop, true_edge, unshare_expr (cond),
1163                                      unshare_expr (c));
1164
1165           /* If C is false, then FALSE_EDGE is taken.  */
1166           c2 = build1_loc (loc, TRUTH_NOT_EXPR, boolean_type_node,
1167                            unshare_expr (c));
1168           add_to_dst_predicate_list (loop, false_edge,
1169                                      unshare_expr (cond), c2);
1170
1171           cond = NULL_TREE;
1172         }
1173
1174       /* If current bb has only one successor, then consider it as an
1175          unconditional goto.  */
1176       if (single_succ_p (bb))
1177         {
1178           basic_block bb_n = single_succ (bb);
1179
1180           /* The successor bb inherits the predicate of its
1181              predecessor.  If there is no predicate in the predecessor
1182              bb, then consider the successor bb as always executed.  */
1183           if (cond == NULL_TREE)
1184             cond = boolean_true_node;
1185
1186           add_to_predicate_list (loop, bb_n, cond);
1187         }
1188     }
1189
1190   /* The loop header is always executed.  */
1191   reset_bb_predicate (loop->header);
1192   gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
1193               && bb_predicate_gimplified_stmts (loop->latch) == NULL);
1194 }
1195
1196 /* Return true when LOOP is if-convertible.  This is a helper function
1197    for if_convertible_loop_p.  REFS and DDRS are initialized and freed
1198    in if_convertible_loop_p.  */
1199
1200 static bool
1201 if_convertible_loop_p_1 (struct loop *loop,
1202                          vec<loop_p> *loop_nest,
1203                          vec<data_reference_p> *refs,
1204                          vec<ddr_p> *ddrs, bool *any_mask_load_store)
1205 {
1206   bool res;
1207   unsigned int i;
1208   basic_block exit_bb = NULL;
1209
1210   /* Don't if-convert the loop when the data dependences cannot be
1211      computed: the loop won't be vectorized in that case.  */
1212   res = compute_data_dependences_for_loop (loop, true, loop_nest, refs, ddrs);
1213   if (!res)
1214     return false;
1215
1216   calculate_dominance_info (CDI_DOMINATORS);
1217   calculate_dominance_info (CDI_POST_DOMINATORS);
1218
1219   /* Allow statements that can be handled during if-conversion.  */
1220   ifc_bbs = get_loop_body_in_if_conv_order (loop);
1221   if (!ifc_bbs)
1222     {
1223       if (dump_file && (dump_flags & TDF_DETAILS))
1224         fprintf (dump_file, "Irreducible loop\n");
1225       return false;
1226     }
1227
1228   for (i = 0; i < loop->num_nodes; i++)
1229     {
1230       basic_block bb = ifc_bbs[i];
1231
1232       if (!if_convertible_bb_p (loop, bb, exit_bb))
1233         return false;
1234
1235       if (bb_with_exit_edge_p (loop, bb))
1236         exit_bb = bb;
1237     }
1238
1239   for (i = 0; i < loop->num_nodes; i++)
1240     {
1241       basic_block bb = ifc_bbs[i];
1242       gimple_stmt_iterator gsi;
1243
1244       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1245         switch (gimple_code (gsi_stmt (gsi)))
1246           {
1247           case GIMPLE_LABEL:
1248           case GIMPLE_ASSIGN:
1249           case GIMPLE_CALL:
1250           case GIMPLE_DEBUG:
1251           case GIMPLE_COND:
1252             break;
1253           default:
1254             return false;
1255           }
1256     }
1257
1258   if (flag_tree_loop_if_convert_stores)
1259     {
1260       data_reference_p dr;
1261
1262       for (i = 0; refs->iterate (i, &dr); i++)
1263         {
1264           dr->aux = XNEW (struct ifc_dr);
1265           DR_WRITTEN_AT_LEAST_ONCE (dr) = -1;
1266           DR_RW_UNCONDITIONALLY (dr) = -1;
1267         }
1268       predicate_bbs (loop);
1269     }
1270
1271   for (i = 0; i < loop->num_nodes; i++)
1272     {
1273       basic_block bb = ifc_bbs[i];
1274       gimple_stmt_iterator itr;
1275
1276       /* Check the if-convertibility of statements in predicated BBs.  */
1277       if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
1278         for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
1279           if (!if_convertible_stmt_p (gsi_stmt (itr), *refs,
1280                                       any_mask_load_store))
1281             return false;
1282     }
1283
1284   if (flag_tree_loop_if_convert_stores)
1285     for (i = 0; i < loop->num_nodes; i++)
1286       free_bb_predicate (ifc_bbs[i]);
1287
1288   /* Checking PHIs needs to be done after stmts, as the fact whether there
1289      are any masked loads or stores affects the tests.  */
1290   for (i = 0; i < loop->num_nodes; i++)
1291     {
1292       basic_block bb = ifc_bbs[i];
1293       gphi_iterator itr;
1294
1295       for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
1296         if (!if_convertible_phi_p (loop, bb, itr.phi (),
1297                                    *any_mask_load_store))
1298           return false;
1299     }
1300
1301   if (dump_file)
1302     fprintf (dump_file, "Applying if-conversion\n");
1303
1304   return true;
1305 }
1306
1307 /* Return true when LOOP is if-convertible.
1308    LOOP is if-convertible if:
1309    - it is innermost,
1310    - it has two or more basic blocks,
1311    - it has only one exit,
1312    - loop header is not the exit edge,
1313    - if its basic blocks and phi nodes are if convertible.  */
1314
1315 static bool
1316 if_convertible_loop_p (struct loop *loop, bool *any_mask_load_store)
1317 {
1318   edge e;
1319   edge_iterator ei;
1320   bool res = false;
1321   vec<data_reference_p> refs;
1322   vec<ddr_p> ddrs;
1323
1324   /* Handle only innermost loop.  */
1325   if (!loop || loop->inner)
1326     {
1327       if (dump_file && (dump_flags & TDF_DETAILS))
1328         fprintf (dump_file, "not innermost loop\n");
1329       return false;
1330     }
1331
1332   /* If only one block, no need for if-conversion.  */
1333   if (loop->num_nodes <= 2)
1334     {
1335       if (dump_file && (dump_flags & TDF_DETAILS))
1336         fprintf (dump_file, "less than 2 basic blocks\n");
1337       return false;
1338     }
1339
1340   /* More than one loop exit is too much to handle.  */
1341   if (!single_exit (loop))
1342     {
1343       if (dump_file && (dump_flags & TDF_DETAILS))
1344         fprintf (dump_file, "multiple exits\n");
1345       return false;
1346     }
1347
1348   /* If one of the loop header's edge is an exit edge then do not
1349      apply if-conversion.  */
1350   FOR_EACH_EDGE (e, ei, loop->header->succs)
1351     if (loop_exit_edge_p (loop, e))
1352       return false;
1353
1354   refs.create (5);
1355   ddrs.create (25);
1356   auto_vec<loop_p, 3> loop_nest;
1357   res = if_convertible_loop_p_1 (loop, &loop_nest, &refs, &ddrs,
1358                                  any_mask_load_store);
1359
1360   if (flag_tree_loop_if_convert_stores)
1361     {
1362       data_reference_p dr;
1363       unsigned int i;
1364
1365       for (i = 0; refs.iterate (i, &dr); i++)
1366         free (dr->aux);
1367     }
1368
1369   free_data_refs (refs);
1370   free_dependence_relations (ddrs);
1371   return res;
1372 }
1373
1374 /* Basic block BB has two predecessors.  Using predecessor's bb
1375    predicate, set an appropriate condition COND for the PHI node
1376    replacement.  Return the true block whose phi arguments are
1377    selected when cond is true.  LOOP is the loop containing the
1378    if-converted region, GSI is the place to insert the code for the
1379    if-conversion.  */
1380
1381 static basic_block
1382 find_phi_replacement_condition (basic_block bb, tree *cond,
1383                                 gimple_stmt_iterator *gsi)
1384 {
1385   edge first_edge, second_edge;
1386   tree tmp_cond;
1387
1388   gcc_assert (EDGE_COUNT (bb->preds) == 2);
1389   first_edge = EDGE_PRED (bb, 0);
1390   second_edge = EDGE_PRED (bb, 1);
1391
1392   /* Prefer an edge with a not negated predicate.
1393      ???  That's a very weak cost model.  */
1394   tmp_cond = bb_predicate (first_edge->src);
1395   gcc_assert (tmp_cond);
1396   if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
1397     {
1398       edge tmp_edge;
1399
1400       tmp_edge = first_edge;
1401       first_edge = second_edge;
1402       second_edge = tmp_edge;
1403     }
1404
1405   /* Check if the edge we take the condition from is not critical.
1406      We know that at least one non-critical edge exists.  */
1407   if (EDGE_COUNT (first_edge->src->succs) > 1)
1408     {
1409       *cond = bb_predicate (second_edge->src);
1410
1411       if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
1412         *cond = TREE_OPERAND (*cond, 0);
1413       else
1414         /* Select non loop header bb.  */
1415         first_edge = second_edge;
1416     }
1417   else
1418     *cond = bb_predicate (first_edge->src);
1419
1420   /* Gimplify the condition to a valid cond-expr conditonal operand.  */
1421   *cond = force_gimple_operand_gsi_1 (gsi, unshare_expr (*cond),
1422                                       is_gimple_condexpr, NULL_TREE,
1423                                       true, GSI_SAME_STMT);
1424
1425   return first_edge->src;
1426 }
1427
1428 /* Returns true if def-stmt for phi argument ARG is simple increment/decrement
1429    which is in predicated basic block.
1430    In fact, the following PHI pattern is searching:
1431       loop-header:
1432         reduc_1 = PHI <..., reduc_2>
1433       ...
1434         if (...)
1435           reduc_3 = ...
1436         reduc_2 = PHI <reduc_1, reduc_3>
1437
1438    REDUC, OP0 and OP1 contain reduction stmt and its operands.  */
1439
1440 static bool
1441 is_cond_scalar_reduction (gimple phi, gimple *reduc,
1442                           tree *op0, tree *op1)
1443 {
1444   tree lhs, r_op1, r_op2;
1445   tree arg_0, arg_1;
1446   gimple stmt;
1447   gimple header_phi = NULL;
1448   enum tree_code reduction_op;
1449   basic_block bb = gimple_bb (phi);
1450   struct loop *loop = bb->loop_father;
1451   edge latch_e = loop_latch_edge (loop);
1452   imm_use_iterator imm_iter;
1453   use_operand_p use_p;
1454
1455   arg_0 = PHI_ARG_DEF (phi, 0);
1456   arg_1 = PHI_ARG_DEF (phi, 1);
1457   if (TREE_CODE (arg_0) != SSA_NAME || TREE_CODE (arg_1) != SSA_NAME)
1458     return false;
1459
1460   if (gimple_code (SSA_NAME_DEF_STMT (arg_0)) == GIMPLE_PHI)
1461     {
1462       lhs = arg_1;
1463       header_phi = SSA_NAME_DEF_STMT (arg_0);
1464       stmt = SSA_NAME_DEF_STMT (arg_1);
1465     }
1466   else if (gimple_code (SSA_NAME_DEF_STMT (arg_1)) == GIMPLE_PHI)
1467     {
1468       lhs = arg_0;
1469       header_phi = SSA_NAME_DEF_STMT (arg_1);
1470       stmt = SSA_NAME_DEF_STMT (arg_0);
1471     }
1472   else
1473     return false;
1474   if (gimple_bb (header_phi) != loop->header)
1475     return false;
1476
1477   if (PHI_ARG_DEF_FROM_EDGE (header_phi, latch_e) != PHI_RESULT (phi))
1478     return false;
1479
1480   if (gimple_code (stmt) != GIMPLE_ASSIGN
1481       || gimple_has_volatile_ops (stmt))
1482     return false;
1483
1484   if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
1485     return false;
1486
1487   if (!is_predicated (gimple_bb (stmt)))
1488     return false;
1489
1490   /* Check that stmt-block is predecessor of phi-block.  */
1491   if (EDGE_PRED (bb, 0)->src != gimple_bb (stmt)
1492       && EDGE_PRED (bb, 1)->src != gimple_bb (stmt))
1493     return false;
1494
1495   if (!has_single_use (lhs))
1496     return false;
1497
1498   reduction_op = gimple_assign_rhs_code (stmt);
1499   if (reduction_op != PLUS_EXPR && reduction_op != MINUS_EXPR)
1500     return false;
1501   r_op1 = gimple_assign_rhs1 (stmt);
1502   r_op2 = gimple_assign_rhs2 (stmt);
1503
1504   /* Make R_OP1 to hold reduction variable.  */
1505   if (r_op2 == PHI_RESULT (header_phi)
1506       && reduction_op == PLUS_EXPR)
1507     {
1508       tree tmp = r_op1;
1509       r_op1 = r_op2;
1510       r_op2 = tmp;
1511     }
1512   else if (r_op1 != PHI_RESULT (header_phi))
1513     return false;
1514
1515   /* Check that R_OP1 is used in reduction stmt or in PHI only.  */
1516   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, r_op1)
1517     {
1518       gimple use_stmt = USE_STMT (use_p);
1519       if (is_gimple_debug (use_stmt))
1520         continue;
1521       if (use_stmt == stmt)
1522         continue;
1523       if (gimple_code (use_stmt) != GIMPLE_PHI)
1524         return false;
1525     }
1526
1527   *op0 = r_op1; *op1 = r_op2;
1528   *reduc = stmt;
1529   return true;
1530 }
1531
1532 /* Converts conditional scalar reduction into unconditional form, e.g.
1533      bb_4
1534        if (_5 != 0) goto bb_5 else goto bb_6
1535      end_bb_4
1536      bb_5
1537        res_6 = res_13 + 1;
1538      end_bb_5
1539      bb_6
1540        # res_2 = PHI <res_13(4), res_6(5)>
1541      end_bb_6
1542
1543    will be converted into sequence
1544     _ifc__1 = _5 != 0 ? 1 : 0;
1545     res_2 = res_13 + _ifc__1;
1546   Argument SWAP tells that arguments of conditional expression should be
1547   swapped.
1548   Returns rhs of resulting PHI assignment.  */
1549
1550 static tree
1551 convert_scalar_cond_reduction (gimple reduc, gimple_stmt_iterator *gsi,
1552                                tree cond, tree op0, tree op1, bool swap)
1553 {
1554   gimple_stmt_iterator stmt_it;
1555   gimple new_assign;
1556   tree rhs;
1557   tree rhs1 = gimple_assign_rhs1 (reduc);
1558   tree tmp = make_temp_ssa_name (TREE_TYPE (rhs1), NULL, "_ifc_");
1559   tree c;
1560   tree zero = build_zero_cst (TREE_TYPE (rhs1));
1561
1562   if (dump_file && (dump_flags & TDF_DETAILS))
1563     {
1564       fprintf (dump_file, "Found cond scalar reduction.\n");
1565       print_gimple_stmt (dump_file, reduc, 0, TDF_SLIM);
1566     }
1567
1568   /* Build cond expression using COND and constant operand
1569      of reduction rhs.  */
1570   c = fold_build_cond_expr (TREE_TYPE (rhs1),
1571                             unshare_expr (cond),
1572                             swap ? zero : op1,
1573                             swap ? op1 : zero);
1574
1575   /* Create assignment stmt and insert it at GSI.  */
1576   new_assign = gimple_build_assign (tmp, c);
1577   gsi_insert_before (gsi, new_assign, GSI_SAME_STMT);
1578   /* Build rhs for unconditional increment/decrement.  */
1579   rhs = fold_build2 (gimple_assign_rhs_code (reduc),
1580                      TREE_TYPE (rhs1), op0, tmp);
1581
1582   /* Delete original reduction stmt.  */
1583   stmt_it = gsi_for_stmt (reduc);
1584   gsi_remove (&stmt_it, true);
1585   release_defs (reduc);
1586   return rhs;
1587 }
1588
1589 /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
1590    This routine does not handle PHI nodes with more than two
1591    arguments.
1592
1593    For example,
1594      S1: A = PHI <x1(1), x2(5)>
1595    is converted into,
1596      S2: A = cond ? x1 : x2;
1597
1598    The generated code is inserted at GSI that points to the top of
1599    basic block's statement list.  When COND is true, phi arg from
1600    TRUE_BB is selected.  */
1601
1602 static void
1603 predicate_scalar_phi (gphi *phi, tree cond,
1604                       basic_block true_bb,
1605                       gimple_stmt_iterator *gsi)
1606 {
1607   gimple new_stmt;
1608   basic_block bb;
1609   tree rhs, res, arg, scev;
1610
1611   gcc_assert (gimple_code (phi) == GIMPLE_PHI
1612               && gimple_phi_num_args (phi) == 2);
1613
1614   res = gimple_phi_result (phi);
1615   /* Do not handle virtual phi nodes.  */
1616   if (virtual_operand_p (res))
1617     return;
1618
1619   bb = gimple_bb (phi);
1620
1621   if ((arg = degenerate_phi_result (phi))
1622       || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
1623                                             res))
1624           && !chrec_contains_undetermined (scev)
1625           && scev != res
1626           && (arg = gimple_phi_arg_def (phi, 0))))
1627     rhs = arg;
1628   else
1629     {
1630       tree arg_0, arg_1;
1631       tree op0, op1;
1632       gimple reduc;
1633
1634       /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr.  */
1635       if (EDGE_PRED (bb, 1)->src == true_bb)
1636         {
1637           arg_0 = gimple_phi_arg_def (phi, 1);
1638           arg_1 = gimple_phi_arg_def (phi, 0);
1639         }
1640       else
1641         {
1642           arg_0 = gimple_phi_arg_def (phi, 0);
1643           arg_1 = gimple_phi_arg_def (phi, 1);
1644         }
1645       if (is_cond_scalar_reduction (phi, &reduc, &op0, &op1))
1646         /* Convert reduction stmt into vectorizable form.  */
1647         rhs = convert_scalar_cond_reduction (reduc, gsi, cond, op0, op1,
1648                                              true_bb != gimple_bb (reduc));
1649       else
1650         /* Build new RHS using selected condition and arguments.  */
1651         rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
1652                                     arg_0, arg_1);
1653     }
1654
1655   new_stmt = gimple_build_assign (res, rhs);
1656   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
1657   update_stmt (new_stmt);
1658
1659   if (dump_file && (dump_flags & TDF_DETAILS))
1660     {
1661       fprintf (dump_file, "new phi replacement stmt\n");
1662       print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
1663     }
1664 }
1665
1666 /* Replaces in LOOP all the scalar phi nodes other than those in the
1667    LOOP->header block with conditional modify expressions.  */
1668
1669 static void
1670 predicate_all_scalar_phis (struct loop *loop)
1671 {
1672   basic_block bb;
1673   unsigned int orig_loop_num_nodes = loop->num_nodes;
1674   unsigned int i;
1675
1676   for (i = 1; i < orig_loop_num_nodes; i++)
1677     {
1678       gphi *phi;
1679       tree cond = NULL_TREE;
1680       gimple_stmt_iterator gsi;
1681       gphi_iterator phi_gsi;
1682       basic_block true_bb = NULL;
1683       bb = ifc_bbs[i];
1684
1685       if (bb == loop->header)
1686         continue;
1687
1688       phi_gsi = gsi_start_phis (bb);
1689       if (gsi_end_p (phi_gsi))
1690         continue;
1691
1692       /* BB has two predecessors.  Using predecessor's aux field, set
1693          appropriate condition for the PHI node replacement.  */
1694       gsi = gsi_after_labels (bb);
1695       true_bb = find_phi_replacement_condition (bb, &cond, &gsi);
1696
1697       while (!gsi_end_p (phi_gsi))
1698         {
1699           phi = phi_gsi.phi ();
1700           predicate_scalar_phi (phi, cond, true_bb, &gsi);
1701           release_phi_node (phi);
1702           gsi_next (&phi_gsi);
1703         }
1704
1705       set_phi_nodes (bb, NULL);
1706     }
1707 }
1708
1709 /* Insert in each basic block of LOOP the statements produced by the
1710    gimplification of the predicates.  */
1711
1712 static void
1713 insert_gimplified_predicates (loop_p loop, bool any_mask_load_store)
1714 {
1715   unsigned int i;
1716
1717   for (i = 0; i < loop->num_nodes; i++)
1718     {
1719       basic_block bb = ifc_bbs[i];
1720       gimple_seq stmts;
1721
1722       if (!is_predicated (bb))
1723         {
1724           /* Do not insert statements for a basic block that is not
1725              predicated.  Also make sure that the predicate of the
1726              basic block is set to true.  */
1727           reset_bb_predicate (bb);
1728           continue;
1729         }
1730
1731       stmts = bb_predicate_gimplified_stmts (bb);
1732       if (stmts)
1733         {
1734           if (flag_tree_loop_if_convert_stores
1735               || any_mask_load_store)
1736             {
1737               /* Insert the predicate of the BB just after the label,
1738                  as the if-conversion of memory writes will use this
1739                  predicate.  */
1740               gimple_stmt_iterator gsi = gsi_after_labels (bb);
1741               gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1742             }
1743           else
1744             {
1745               /* Insert the predicate of the BB at the end of the BB
1746                  as this would reduce the register pressure: the only
1747                  use of this predicate will be in successor BBs.  */
1748               gimple_stmt_iterator gsi = gsi_last_bb (bb);
1749
1750               if (gsi_end_p (gsi)
1751                   || stmt_ends_bb_p (gsi_stmt (gsi)))
1752                 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
1753               else
1754                 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
1755             }
1756
1757           /* Once the sequence is code generated, set it to NULL.  */
1758           set_bb_predicate_gimplified_stmts (bb, NULL);
1759         }
1760     }
1761 }
1762
1763 /* Predicate each write to memory in LOOP.
1764
1765    This function transforms control flow constructs containing memory
1766    writes of the form:
1767
1768    | for (i = 0; i < N; i++)
1769    |   if (cond)
1770    |     A[i] = expr;
1771
1772    into the following form that does not contain control flow:
1773
1774    | for (i = 0; i < N; i++)
1775    |   A[i] = cond ? expr : A[i];
1776
1777    The original CFG looks like this:
1778
1779    | bb_0
1780    |   i = 0
1781    | end_bb_0
1782    |
1783    | bb_1
1784    |   if (i < N) goto bb_5 else goto bb_2
1785    | end_bb_1
1786    |
1787    | bb_2
1788    |   cond = some_computation;
1789    |   if (cond) goto bb_3 else goto bb_4
1790    | end_bb_2
1791    |
1792    | bb_3
1793    |   A[i] = expr;
1794    |   goto bb_4
1795    | end_bb_3
1796    |
1797    | bb_4
1798    |   goto bb_1
1799    | end_bb_4
1800
1801    insert_gimplified_predicates inserts the computation of the COND
1802    expression at the beginning of the destination basic block:
1803
1804    | bb_0
1805    |   i = 0
1806    | end_bb_0
1807    |
1808    | bb_1
1809    |   if (i < N) goto bb_5 else goto bb_2
1810    | end_bb_1
1811    |
1812    | bb_2
1813    |   cond = some_computation;
1814    |   if (cond) goto bb_3 else goto bb_4
1815    | end_bb_2
1816    |
1817    | bb_3
1818    |   cond = some_computation;
1819    |   A[i] = expr;
1820    |   goto bb_4
1821    | end_bb_3
1822    |
1823    | bb_4
1824    |   goto bb_1
1825    | end_bb_4
1826
1827    predicate_mem_writes is then predicating the memory write as follows:
1828
1829    | bb_0
1830    |   i = 0
1831    | end_bb_0
1832    |
1833    | bb_1
1834    |   if (i < N) goto bb_5 else goto bb_2
1835    | end_bb_1
1836    |
1837    | bb_2
1838    |   if (cond) goto bb_3 else goto bb_4
1839    | end_bb_2
1840    |
1841    | bb_3
1842    |   cond = some_computation;
1843    |   A[i] = cond ? expr : A[i];
1844    |   goto bb_4
1845    | end_bb_3
1846    |
1847    | bb_4
1848    |   goto bb_1
1849    | end_bb_4
1850
1851    and finally combine_blocks removes the basic block boundaries making
1852    the loop vectorizable:
1853
1854    | bb_0
1855    |   i = 0
1856    |   if (i < N) goto bb_5 else goto bb_1
1857    | end_bb_0
1858    |
1859    | bb_1
1860    |   cond = some_computation;
1861    |   A[i] = cond ? expr : A[i];
1862    |   if (i < N) goto bb_5 else goto bb_4
1863    | end_bb_1
1864    |
1865    | bb_4
1866    |   goto bb_1
1867    | end_bb_4
1868 */
1869
1870 static void
1871 predicate_mem_writes (loop_p loop)
1872 {
1873   unsigned int i, orig_loop_num_nodes = loop->num_nodes;
1874
1875   for (i = 1; i < orig_loop_num_nodes; i++)
1876     {
1877       gimple_stmt_iterator gsi;
1878       basic_block bb = ifc_bbs[i];
1879       tree cond = bb_predicate (bb);
1880       bool swap;
1881       gimple stmt;
1882
1883       if (is_true_predicate (cond))
1884         continue;
1885
1886       swap = false;
1887       if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
1888         {
1889           swap = true;
1890           cond = TREE_OPERAND (cond, 0);
1891         }
1892
1893       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1894         if (!gimple_assign_single_p (stmt = gsi_stmt (gsi)))
1895           continue;
1896         else if (gimple_plf (stmt, GF_PLF_2))
1897           {
1898             tree lhs = gimple_assign_lhs (stmt);
1899             tree rhs = gimple_assign_rhs1 (stmt);
1900             tree ref, addr, ptr, masktype, mask_op0, mask_op1, mask;
1901             gimple new_stmt;
1902             int bitsize = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (lhs)));
1903
1904             masktype = build_nonstandard_integer_type (bitsize, 1);
1905             mask_op0 = build_int_cst (masktype, swap ? 0 : -1);
1906             mask_op1 = build_int_cst (masktype, swap ? -1 : 0);
1907             ref = TREE_CODE (lhs) == SSA_NAME ? rhs : lhs;
1908             mark_addressable (ref);
1909             addr = force_gimple_operand_gsi (&gsi, build_fold_addr_expr (ref),
1910                                              true, NULL_TREE, true,
1911                                              GSI_SAME_STMT);
1912             cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1913                                                is_gimple_condexpr, NULL_TREE,
1914                                                true, GSI_SAME_STMT);
1915             mask = fold_build_cond_expr (masktype, unshare_expr (cond),
1916                                          mask_op0, mask_op1);
1917             mask = ifc_temp_var (masktype, mask, &gsi);
1918             ptr = build_int_cst (reference_alias_ptr_type (ref), 0);
1919             /* Copy points-to info if possible.  */
1920             if (TREE_CODE (addr) == SSA_NAME && !SSA_NAME_PTR_INFO (addr))
1921               copy_ref_info (build2 (MEM_REF, TREE_TYPE (ref), addr, ptr),
1922                              ref);
1923             if (TREE_CODE (lhs) == SSA_NAME)
1924               {
1925                 new_stmt
1926                   = gimple_build_call_internal (IFN_MASK_LOAD, 3, addr,
1927                                                 ptr, mask);
1928                 gimple_call_set_lhs (new_stmt, lhs);
1929               }
1930             else
1931               new_stmt
1932                 = gimple_build_call_internal (IFN_MASK_STORE, 4, addr, ptr,
1933                                               mask, rhs);
1934             gsi_replace (&gsi, new_stmt, true);
1935           }
1936         else if (gimple_vdef (stmt))
1937           {
1938             tree lhs = gimple_assign_lhs (stmt);
1939             tree rhs = gimple_assign_rhs1 (stmt);
1940             tree type = TREE_TYPE (lhs);
1941
1942             lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
1943             rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
1944             if (swap)
1945               {
1946                 tree tem = lhs;
1947                 lhs = rhs;
1948                 rhs = tem;
1949               }
1950             cond = force_gimple_operand_gsi_1 (&gsi, unshare_expr (cond),
1951                                                is_gimple_condexpr, NULL_TREE,
1952                                                true, GSI_SAME_STMT);
1953             rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
1954             gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
1955             update_stmt (stmt);
1956           }
1957     }
1958 }
1959
1960 /* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
1961    other than the exit and latch of the LOOP.  Also resets the
1962    GIMPLE_DEBUG information.  */
1963
1964 static void
1965 remove_conditions_and_labels (loop_p loop)
1966 {
1967   gimple_stmt_iterator gsi;
1968   unsigned int i;
1969
1970   for (i = 0; i < loop->num_nodes; i++)
1971     {
1972       basic_block bb = ifc_bbs[i];
1973
1974       if (bb_with_exit_edge_p (loop, bb)
1975         || bb == loop->latch)
1976       continue;
1977
1978       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
1979         switch (gimple_code (gsi_stmt (gsi)))
1980           {
1981           case GIMPLE_COND:
1982           case GIMPLE_LABEL:
1983             gsi_remove (&gsi, true);
1984             break;
1985
1986           case GIMPLE_DEBUG:
1987             /* ??? Should there be conditional GIMPLE_DEBUG_BINDs?  */
1988             if (gimple_debug_bind_p (gsi_stmt (gsi)))
1989               {
1990                 gimple_debug_bind_reset_value (gsi_stmt (gsi));
1991                 update_stmt (gsi_stmt (gsi));
1992               }
1993             gsi_next (&gsi);
1994             break;
1995
1996           default:
1997             gsi_next (&gsi);
1998           }
1999     }
2000 }
2001
2002 /* Combine all the basic blocks from LOOP into one or two super basic
2003    blocks.  Replace PHI nodes with conditional modify expressions.  */
2004
2005 static void
2006 combine_blocks (struct loop *loop, bool any_mask_load_store)
2007 {
2008   basic_block bb, exit_bb, merge_target_bb;
2009   unsigned int orig_loop_num_nodes = loop->num_nodes;
2010   unsigned int i;
2011   edge e;
2012   edge_iterator ei;
2013
2014   predicate_bbs (loop);
2015   remove_conditions_and_labels (loop);
2016   insert_gimplified_predicates (loop, any_mask_load_store);
2017   predicate_all_scalar_phis (loop);
2018
2019   if (flag_tree_loop_if_convert_stores || any_mask_load_store)
2020     predicate_mem_writes (loop);
2021
2022   /* Merge basic blocks: first remove all the edges in the loop,
2023      except for those from the exit block.  */
2024   exit_bb = NULL;
2025   for (i = 0; i < orig_loop_num_nodes; i++)
2026     {
2027       bb = ifc_bbs[i];
2028       free_bb_predicate (bb);
2029       if (bb_with_exit_edge_p (loop, bb))
2030         {
2031           gcc_assert (exit_bb == NULL);
2032           exit_bb = bb;
2033         }
2034     }
2035   gcc_assert (exit_bb != loop->latch);
2036
2037   for (i = 1; i < orig_loop_num_nodes; i++)
2038     {
2039       bb = ifc_bbs[i];
2040
2041       for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
2042         {
2043           if (e->src == exit_bb)
2044             ei_next (&ei);
2045           else
2046             remove_edge (e);
2047         }
2048     }
2049
2050   if (exit_bb != NULL)
2051     {
2052       if (exit_bb != loop->header)
2053         {
2054           /* Connect this node to loop header.  */
2055           make_edge (loop->header, exit_bb, EDGE_FALLTHRU);
2056           set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
2057         }
2058
2059       /* Redirect non-exit edges to loop->latch.  */
2060       FOR_EACH_EDGE (e, ei, exit_bb->succs)
2061         {
2062           if (!loop_exit_edge_p (loop, e))
2063             redirect_edge_and_branch (e, loop->latch);
2064         }
2065       set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
2066     }
2067   else
2068     {
2069       /* If the loop does not have an exit, reconnect header and latch.  */
2070       make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
2071       set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
2072     }
2073
2074   merge_target_bb = loop->header;
2075   for (i = 1; i < orig_loop_num_nodes; i++)
2076     {
2077       gimple_stmt_iterator gsi;
2078       gimple_stmt_iterator last;
2079
2080       bb = ifc_bbs[i];
2081
2082       if (bb == exit_bb || bb == loop->latch)
2083         continue;
2084
2085       /* Make stmts member of loop->header.  */
2086       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2087         gimple_set_bb (gsi_stmt (gsi), merge_target_bb);
2088
2089       /* Update stmt list.  */
2090       last = gsi_last_bb (merge_target_bb);
2091       gsi_insert_seq_after (&last, bb_seq (bb), GSI_NEW_STMT);
2092       set_bb_seq (bb, NULL);
2093
2094       delete_basic_block (bb);
2095     }
2096
2097   /* If possible, merge loop header to the block with the exit edge.
2098      This reduces the number of basic blocks to two, to please the
2099      vectorizer that handles only loops with two nodes.  */
2100   if (exit_bb
2101       && exit_bb != loop->header
2102       && can_merge_blocks_p (loop->header, exit_bb))
2103     merge_blocks (loop->header, exit_bb);
2104
2105   free (ifc_bbs);
2106   ifc_bbs = NULL;
2107 }
2108
2109 /* Version LOOP before if-converting it, the original loop
2110    will be then if-converted, the new copy of the loop will not,
2111    and the LOOP_VECTORIZED internal call will be guarding which
2112    loop to execute.  The vectorizer pass will fold this
2113    internal call into either true or false.  */
2114
2115 static bool
2116 version_loop_for_if_conversion (struct loop *loop)
2117 {
2118   basic_block cond_bb;
2119   tree cond = make_ssa_name (boolean_type_node);
2120   struct loop *new_loop;
2121   gimple g;
2122   gimple_stmt_iterator gsi;
2123
2124   g = gimple_build_call_internal (IFN_LOOP_VECTORIZED, 2,
2125                                   build_int_cst (integer_type_node, loop->num),
2126                                   integer_zero_node);
2127   gimple_call_set_lhs (g, cond);
2128
2129   initialize_original_copy_tables ();
2130   new_loop = loop_version (loop, cond, &cond_bb,
2131                            REG_BR_PROB_BASE, REG_BR_PROB_BASE,
2132                            REG_BR_PROB_BASE, true);
2133   free_original_copy_tables ();
2134   if (new_loop == NULL)
2135     return false;
2136   new_loop->dont_vectorize = true;
2137   new_loop->force_vectorize = false;
2138   gsi = gsi_last_bb (cond_bb);
2139   gimple_call_set_arg (g, 1, build_int_cst (integer_type_node, new_loop->num));
2140   gsi_insert_before (&gsi, g, GSI_SAME_STMT);
2141   update_ssa (TODO_update_ssa);
2142   return true;
2143 }
2144
2145 /* If-convert LOOP when it is legal.  For the moment this pass has no
2146    profitability analysis.  Returns non-zero todo flags when something
2147    changed.  */
2148
2149 static unsigned int
2150 tree_if_conversion (struct loop *loop)
2151 {
2152   unsigned int todo = 0;
2153   ifc_bbs = NULL;
2154   bool any_mask_load_store = false;
2155
2156   if (!if_convertible_loop_p (loop, &any_mask_load_store)
2157       || !dbg_cnt (if_conversion_tree))
2158     goto cleanup;
2159
2160   if (any_mask_load_store
2161       && ((!flag_tree_loop_vectorize && !loop->force_vectorize)
2162           || loop->dont_vectorize))
2163     goto cleanup;
2164
2165   if (any_mask_load_store && !version_loop_for_if_conversion (loop))
2166     goto cleanup;
2167
2168   /* Now all statements are if-convertible.  Combine all the basic
2169      blocks into one huge basic block doing the if-conversion
2170      on-the-fly.  */
2171   combine_blocks (loop, any_mask_load_store);
2172
2173   todo |= TODO_cleanup_cfg;
2174   if (flag_tree_loop_if_convert_stores || any_mask_load_store)
2175     {
2176       mark_virtual_operands_for_renaming (cfun);
2177       todo |= TODO_update_ssa_only_virtuals;
2178     }
2179
2180  cleanup:
2181   if (ifc_bbs)
2182     {
2183       unsigned int i;
2184
2185       for (i = 0; i < loop->num_nodes; i++)
2186         free_bb_predicate (ifc_bbs[i]);
2187
2188       free (ifc_bbs);
2189       ifc_bbs = NULL;
2190     }
2191   free_dominance_info (CDI_POST_DOMINATORS);
2192
2193   return todo;
2194 }
2195
2196 /* Tree if-conversion pass management.  */
2197
2198 namespace {
2199
2200 const pass_data pass_data_if_conversion =
2201 {
2202   GIMPLE_PASS, /* type */
2203   "ifcvt", /* name */
2204   OPTGROUP_NONE, /* optinfo_flags */
2205   TV_NONE, /* tv_id */
2206   ( PROP_cfg | PROP_ssa ), /* properties_required */
2207   0, /* properties_provided */
2208   0, /* properties_destroyed */
2209   0, /* todo_flags_start */
2210   0, /* todo_flags_finish */
2211 };
2212
2213 class pass_if_conversion : public gimple_opt_pass
2214 {
2215 public:
2216   pass_if_conversion (gcc::context *ctxt)
2217     : gimple_opt_pass (pass_data_if_conversion, ctxt)
2218   {}
2219
2220   /* opt_pass methods: */
2221   virtual bool gate (function *);
2222   virtual unsigned int execute (function *);
2223
2224 }; // class pass_if_conversion
2225
2226 bool
2227 pass_if_conversion::gate (function *fun)
2228 {
2229   return (((flag_tree_loop_vectorize || fun->has_force_vectorize_loops)
2230            && flag_tree_loop_if_convert != 0)
2231           || flag_tree_loop_if_convert == 1
2232           || flag_tree_loop_if_convert_stores == 1);
2233 }
2234
2235 unsigned int
2236 pass_if_conversion::execute (function *fun)
2237 {
2238   struct loop *loop;
2239   unsigned todo = 0;
2240
2241   if (number_of_loops (fun) <= 1)
2242     return 0;
2243
2244   FOR_EACH_LOOP (loop, 0)
2245     if (flag_tree_loop_if_convert == 1
2246         || flag_tree_loop_if_convert_stores == 1
2247         || ((flag_tree_loop_vectorize || loop->force_vectorize)
2248             && !loop->dont_vectorize))
2249       todo |= tree_if_conversion (loop);
2250
2251 #ifdef ENABLE_CHECKING
2252   {
2253     basic_block bb;
2254     FOR_EACH_BB_FN (bb, fun)
2255       gcc_assert (!bb->aux);
2256   }
2257 #endif
2258
2259   return todo;
2260 }
2261
2262 } // anon namespace
2263
2264 gimple_opt_pass *
2265 make_pass_if_conversion (gcc::context *ctxt)
2266 {
2267   return new pass_if_conversion (ctxt);
2268 }