LCOV - code coverage report
Current view: top level - gcc - tree-if-conv.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.2 % 1840 1771
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 71 71
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* If-conversion for vectorizer.
       2              :    Copyright (C) 2004-2026 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 "backend.h"
      87              : #include "rtl.h"
      88              : #include "tree.h"
      89              : #include "gimple.h"
      90              : #include "cfghooks.h"
      91              : #include "tree-pass.h"
      92              : #include "ssa.h"
      93              : #include "expmed.h"
      94              : #include "expr.h"
      95              : #include "optabs-tree.h"
      96              : #include "gimple-pretty-print.h"
      97              : #include "alias.h"
      98              : #include "fold-const.h"
      99              : #include "stor-layout.h"
     100              : #include "gimple-iterator.h"
     101              : #include "gimple-fold.h"
     102              : #include "gimplify.h"
     103              : #include "gimplify-me.h"
     104              : #include "tree-cfg.h"
     105              : #include "tree-into-ssa.h"
     106              : #include "tree-ssa.h"
     107              : #include "cfgloop.h"
     108              : #include "tree-data-ref.h"
     109              : #include "tree-scalar-evolution.h"
     110              : #include "tree-ssa-loop.h"
     111              : #include "tree-ssa-loop-niter.h"
     112              : #include "tree-ssa-loop-ivopts.h"
     113              : #include "tree-ssa-address.h"
     114              : #include "dbgcnt.h"
     115              : #include "tree-hash-traits.h"
     116              : #include "varasm.h"
     117              : #include "builtins.h"
     118              : #include "cfganal.h"
     119              : #include "internal-fn.h"
     120              : #include "tree-ssa-sccvn.h"
     121              : #include "tree-cfgcleanup.h"
     122              : #include "tree-ssa-dse.h"
     123              : #include "tree-vectorizer.h"
     124              : #include "tree-eh.h"
     125              : #include "cgraph.h"
     126              : 
     127              : /* For lang_hooks.types.type_for_mode.  */
     128              : #include "langhooks.h"
     129              : 
     130              : /* Only handle PHIs with no more arguments unless we are asked to by
     131              :    simd pragma.  */
     132              : #define MAX_PHI_ARG_NUM \
     133              :   ((unsigned) param_max_tree_if_conversion_phi_args)
     134              : 
     135              : /* True if we've converted a statement that was only executed when some
     136              :    condition C was true, and if for correctness we need to predicate the
     137              :    statement to ensure that it is a no-op when C is false.  See
     138              :    predicate_statements for the kinds of predication we support.  */
     139              : static bool need_to_predicate;
     140              : 
     141              : /* True if we have to rewrite stmts that may invoke undefined behavior
     142              :    when a condition C was false so it doesn't if it is always executed.
     143              :    See predicate_statements for the kinds of predication we support.  */
     144              : static bool need_to_rewrite_undefined;
     145              : 
     146              : /* Indicate if there are any complicated PHIs that need to be handled in
     147              :    if-conversion.  Complicated PHI has more than two arguments and can't
     148              :    be degenerated to two arguments PHI.  See more information in comment
     149              :    before phi_convertible_by_degenerating_args.  */
     150              : static bool any_complicated_phi;
     151              : 
     152              : /* True if we have bitfield accesses we can lower.  */
     153              : static bool need_to_lower_bitfields;
     154              : 
     155              : /* True if there is any ifcvting to be done.  */
     156              : static bool need_to_ifcvt;
     157              : 
     158              : /* Hash for struct innermost_loop_behavior.  It depends on the user to
     159              :    free the memory.  */
     160              : 
     161              : struct innermost_loop_behavior_hash : nofree_ptr_hash <innermost_loop_behavior>
     162              : {
     163              :   static inline hashval_t hash (const value_type &);
     164              :   static inline bool equal (const value_type &,
     165              :                             const compare_type &);
     166              : };
     167              : 
     168              : inline hashval_t
     169       406399 : innermost_loop_behavior_hash::hash (const value_type &e)
     170              : {
     171       406399 :   hashval_t hash;
     172              : 
     173       406399 :   hash = iterative_hash_expr (e->base_address, 0);
     174       406399 :   hash = iterative_hash_expr (e->offset, hash);
     175       406399 :   hash = iterative_hash_expr (e->init, hash);
     176       406399 :   return iterative_hash_expr (e->step, hash);
     177              : }
     178              : 
     179              : inline bool
     180       293395 : innermost_loop_behavior_hash::equal (const value_type &e1,
     181              :                                      const compare_type &e2)
     182              : {
     183       293395 :   if ((e1->base_address && !e2->base_address)
     184       293395 :       || (!e1->base_address && e2->base_address)
     185       293395 :       || (!e1->offset && e2->offset)
     186       277149 :       || (e1->offset && !e2->offset)
     187       248109 :       || (!e1->init && e2->init)
     188       248109 :       || (e1->init && !e2->init)
     189       248109 :       || (!e1->step && e2->step)
     190       248109 :       || (e1->step && !e2->step))
     191              :     return false;
     192              : 
     193       248109 :   if (e1->base_address && e2->base_address
     194       496218 :       && !operand_equal_p (e1->base_address, e2->base_address, 0))
     195              :     return false;
     196        49748 :   if (e1->offset && e2->offset
     197       129955 :       && !operand_equal_p (e1->offset, e2->offset, 0))
     198              :     return false;
     199        49496 :   if (e1->init && e2->init
     200       129451 :       && !operand_equal_p (e1->init, e2->init, 0))
     201              :     return false;
     202        18084 :   if (e1->step && e2->step
     203        66627 :       && !operand_equal_p (e1->step, e2->step, 0))
     204              :     return false;
     205              : 
     206              :   return true;
     207              : }
     208              : 
     209              : /* List of basic blocks in if-conversion-suitable order.  */
     210              : static basic_block *ifc_bbs;
     211              : 
     212              : /* Hash table to store <DR's innermost loop behavior, DR> pairs.  */
     213              : static hash_map<innermost_loop_behavior_hash,
     214              :                 data_reference_p> *innermost_DR_map;
     215              : 
     216              : /* Hash table to store <base reference, DR> pairs.  */
     217              : static hash_map<tree_operand_hash, data_reference_p> *baseref_DR_map;
     218              : 
     219              : /* List of redundant SSA names: the first should be replaced by the second.  */
     220              : static vec< std::pair<tree, tree> > redundant_ssa_names;
     221              : 
     222              : /* Structure used to predicate basic blocks.  This is attached to the
     223              :    ->aux field of the BBs in the loop to be if-converted.  */
     224              : struct bb_predicate {
     225              : 
     226              :   /* The condition under which this basic block is executed.  */
     227              :   tree predicate;
     228              : 
     229              :   /* PREDICATE is gimplified, and the sequence of statements is
     230              :      recorded here, in order to avoid the duplication of computations
     231              :      that occur in previous conditions.  See PR44483.  */
     232              :   gimple_seq predicate_gimplified_stmts;
     233              : 
     234              :   /* Records the number of statements recorded into
     235              :      PREDICATE_GIMPLIFIED_STMTS.   */
     236              :   unsigned no_predicate_stmts;
     237              : };
     238              : 
     239              : /* Returns true when the basic block BB has a predicate.  */
     240              : 
     241              : static inline bool
     242      1985376 : bb_has_predicate (basic_block bb)
     243              : {
     244      1985376 :   return bb->aux != NULL;
     245              : }
     246              : 
     247              : /* Returns the gimplified predicate for basic block BB.  */
     248              : 
     249              : static inline tree
     250       905032 : bb_predicate (basic_block bb)
     251              : {
     252       905032 :   return ((struct bb_predicate *) bb->aux)->predicate;
     253              : }
     254              : 
     255              : /* Sets the gimplified predicate COND for basic block BB.  */
     256              : 
     257              : static inline void
     258       491994 : set_bb_predicate (basic_block bb, tree cond)
     259              : {
     260       491994 :   auto aux = (struct bb_predicate *) bb->aux;
     261       491994 :   gcc_assert ((TREE_CODE (cond) == TRUTH_NOT_EXPR
     262              :                && is_gimple_val (TREE_OPERAND (cond, 0)))
     263              :               || is_gimple_val (cond));
     264       491994 :   aux->predicate = cond;
     265       491994 :   aux->no_predicate_stmts++;
     266              : 
     267       491994 :   if (dump_file && (dump_flags & TDF_DETAILS))
     268          378 :     fprintf (dump_file, "Recording block %d value %d\n", bb->index,
     269              :              aux->no_predicate_stmts);
     270       491994 : }
     271              : 
     272              : /* Returns the sequence of statements of the gimplification of the
     273              :    predicate for basic block BB.  */
     274              : 
     275              : static inline gimple_seq
     276       605501 : bb_predicate_gimplified_stmts (basic_block bb)
     277              : {
     278       605501 :   return ((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts;
     279              : }
     280              : 
     281              : /* Sets the sequence of statements STMTS of the gimplification of the
     282              :    predicate for basic block BB.  If PRESERVE_COUNTS then don't clear the predicate
     283              :    counts.  */
     284              : 
     285              : static inline void
     286       291246 : set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts,
     287              :                                    bool preserve_counts)
     288              : {
     289       291246 :   ((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts = stmts;
     290       291246 :   if (stmts == NULL && !preserve_counts)
     291       239106 :     ((struct bb_predicate *) bb->aux)->no_predicate_stmts = 0;
     292              : }
     293              : 
     294              : /* Adds the sequence of statements STMTS to the sequence of statements
     295              :    of the predicate for basic block BB.  */
     296              : 
     297              : static inline void
     298        88424 : add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
     299              : {
     300              :   /* We might have updated some stmts in STMTS via force_gimple_operand
     301              :      calling fold_stmt and that producing multiple stmts.  Delink immediate
     302              :      uses so update_ssa after loop versioning doesn't get confused for
     303              :      the not yet inserted predicates.
     304              :      ???  This should go away once we reliably avoid updating stmts
     305              :      not in any BB.  */
     306        88424 :   for (gimple_stmt_iterator gsi = gsi_start (stmts);
     307       218735 :        !gsi_end_p (gsi); gsi_next (&gsi))
     308              :     {
     309       130311 :       gimple *stmt = gsi_stmt (gsi);
     310       130311 :       delink_stmt_imm_use (stmt);
     311       130311 :       gimple_set_modified (stmt, true);
     312       130311 :       ((struct bb_predicate *) bb->aux)->no_predicate_stmts++;
     313              :     }
     314        88424 :   gimple_seq_add_seq_without_update
     315        88424 :     (&(((struct bb_predicate *) bb->aux)->predicate_gimplified_stmts), stmts);
     316        88424 : }
     317              : 
     318              : /* Return the number of statements the predicate of the basic block consists
     319              :    of.  */
     320              : 
     321              : static inline unsigned
     322        16648 : get_bb_num_predicate_stmts (basic_block bb)
     323              : {
     324        16648 :   return ((struct bb_predicate *) bb->aux)->no_predicate_stmts;
     325              : }
     326              : 
     327              : /* Initializes to TRUE the predicate of basic block BB.  */
     328              : 
     329              : static inline void
     330       204948 : init_bb_predicate (basic_block bb)
     331              : {
     332       204948 :   bb->aux = XNEW (struct bb_predicate);
     333       204948 :   set_bb_predicate_gimplified_stmts (bb, NULL, false);
     334       204948 :   set_bb_predicate (bb, boolean_true_node);
     335       204948 : }
     336              : 
     337              : /* Release the SSA_NAMEs associated with the predicate of basic block BB.  */
     338              : 
     339              : static inline void
     340       397542 : release_bb_predicate (basic_block bb)
     341              : {
     342       397542 :   gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
     343       397542 :   if (stmts)
     344              :     {
     345              :       /* Ensure that these stmts haven't yet been added to a bb.  */
     346        34158 :       if (flag_checking)
     347        94864 :         for (gimple_stmt_iterator i = gsi_start (stmts);
     348        94864 :              !gsi_end_p (i); gsi_next (&i))
     349        60706 :           gcc_assert (! gimple_bb (gsi_stmt (i)));
     350              : 
     351              :       /* Discard them.  */
     352        34158 :       gimple_seq_discard (stmts);
     353        34158 :       set_bb_predicate_gimplified_stmts (bb, NULL, false);
     354              :     }
     355       397542 : }
     356              : 
     357              : /* Free the predicate of basic block BB.  */
     358              : 
     359              : static inline void
     360      1792782 : free_bb_predicate (basic_block bb)
     361              : {
     362      1792782 :   if (!bb_has_predicate (bb))
     363              :     return;
     364              : 
     365       204948 :   release_bb_predicate (bb);
     366       204948 :   free (bb->aux);
     367       204948 :   bb->aux = NULL;
     368              : }
     369              : 
     370              : /* Reinitialize predicate of BB with the true predicate.  */
     371              : 
     372              : static inline void
     373       192594 : reset_bb_predicate (basic_block bb)
     374              : {
     375       192594 :   if (!bb_has_predicate (bb))
     376            0 :     init_bb_predicate (bb);
     377              :   else
     378              :     {
     379       192594 :       release_bb_predicate (bb);
     380       192594 :       set_bb_predicate (bb, boolean_true_node);
     381              :     }
     382       192594 : }
     383              : 
     384              : /* Returns a new SSA_NAME of type TYPE that is assigned the value of
     385              :    the expression EXPR.  Inserts the statement created for this
     386              :    computation before GSI and leaves the iterator GSI at the same
     387              :    statement.  */
     388              : 
     389              : static tree
     390         5433 : ifc_temp_var (tree type, tree expr, gimple_stmt_iterator *gsi)
     391              : {
     392         5433 :   tree new_name = make_temp_ssa_name (type, NULL, "_ifc_");
     393         5433 :   gimple *stmt = gimple_build_assign (new_name, expr);
     394        10866 :   gimple_set_vuse (stmt, gimple_vuse (gsi_stmt (*gsi)));
     395         5433 :   gsi_insert_before (gsi, stmt, GSI_SAME_STMT);
     396         5433 :   return new_name;
     397              : }
     398              : 
     399              : /* Return true when COND is a false predicate.  */
     400              : 
     401              : static inline bool
     402        88891 : is_false_predicate (tree cond)
     403              : {
     404        88891 :   return (cond != NULL_TREE
     405        88891 :           && (cond == boolean_false_node
     406        88891 :               || integer_zerop (cond)));
     407              : }
     408              : 
     409              : /* Return true when COND is a true predicate.  */
     410              : 
     411              : static inline bool
     412      1032624 : is_true_predicate (tree cond)
     413              : {
     414      1032624 :   return (cond == NULL_TREE
     415      1032624 :           || cond == boolean_true_node
     416      1496404 :           || integer_onep (cond));
     417              : }
     418              : 
     419              : /* Returns true when BB has a predicate that is not trivial: true or
     420              :    NULL_TREE.  */
     421              : 
     422              : static inline bool
     423       504175 : is_predicated (basic_block bb)
     424              : {
     425        13890 :   return !is_true_predicate (bb_predicate (bb));
     426              : }
     427              : 
     428              : /* Parses the predicate COND and returns its comparison code and
     429              :    operands OP0 and OP1.  */
     430              : 
     431              : static enum tree_code
     432       400760 : parse_predicate (tree cond, tree *op0, tree *op1)
     433              : {
     434       400760 :   gassign *s;
     435              : 
     436       400760 :   if (TREE_CODE (cond) == SSA_NAME
     437       400760 :       && (s = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (cond))))
     438              :     {
     439        70601 :       if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
     440              :         {
     441        32444 :           *op0 = gimple_assign_rhs1 (s);
     442        32444 :           *op1 = gimple_assign_rhs2 (s);
     443        32444 :           return gimple_assign_rhs_code (s);
     444              :         }
     445              : 
     446        31764 :       else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
     447              :         {
     448            0 :           tree op = gimple_assign_rhs1 (s);
     449            0 :           tree type = TREE_TYPE (op);
     450            0 :           enum tree_code code = parse_predicate (op, op0, op1);
     451              : 
     452            0 :           return code == ERROR_MARK ? ERROR_MARK
     453            0 :             : invert_tree_comparison (code, HONOR_NANS (type));
     454              :         }
     455              : 
     456              :       return ERROR_MARK;
     457              :     }
     458              : 
     459       336552 :   if (COMPARISON_CLASS_P (cond))
     460              :     {
     461          483 :       *op0 = TREE_OPERAND (cond, 0);
     462          483 :       *op1 = TREE_OPERAND (cond, 1);
     463          483 :       return TREE_CODE (cond);
     464              :     }
     465              : 
     466              :   return ERROR_MARK;
     467              : }
     468              : 
     469              : /* Returns the fold of predicate C1 OR C2 at location LOC.  */
     470              : 
     471              : static tree
     472       200380 : fold_or_predicates (location_t loc, tree c1, tree c2)
     473              : {
     474       200380 :   tree op1a, op1b, op2a, op2b;
     475       200380 :   enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
     476       200380 :   enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
     477              : 
     478       200380 :   if (code1 != ERROR_MARK && code2 != ERROR_MARK)
     479              :     {
     480         2285 :       tree t = maybe_fold_or_comparisons (boolean_type_node, TRUTH_OR_EXPR,
     481              :                                           code1, op1a, op1b, code2, op2a, op2b);
     482         2285 :       if (t)
     483              :         return t;
     484              :     }
     485              : 
     486       198190 :   return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
     487              : }
     488              : 
     489              : /* Returns either a COND_EXPR or the folded expression if the folded
     490              :    expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
     491              :    a constant or a SSA_NAME. */
     492              : 
     493              : static tree
     494        50067 : fold_build_cond_expr (tree type, tree cond, tree rhs, tree lhs)
     495              : {
     496              :   /* Short cut the case where both rhs and lhs are the same. */
     497        50067 :   if (operand_equal_p (rhs, lhs))
     498              :     return rhs;
     499              : 
     500              :   /* If COND is comparison r != 0 and r has boolean type, convert COND
     501              :      to SSA_NAME to accept by vect bool pattern.  */
     502        50067 :   if (TREE_CODE (cond) == NE_EXPR)
     503              :     {
     504            0 :       tree op0 = TREE_OPERAND (cond, 0);
     505            0 :       tree op1 = TREE_OPERAND (cond, 1);
     506            0 :       if (TREE_CODE (op0) == SSA_NAME
     507            0 :           && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
     508            0 :           && (integer_zerop (op1)))
     509              :         cond = op0;
     510              :     }
     511              : 
     512        50067 :   gimple_match_op cexpr (gimple_match_cond::UNCOND, COND_EXPR,
     513        50067 :                          type, cond, rhs, lhs);
     514        50067 :   if (cexpr.resimplify (NULL, follow_all_ssa_edges))
     515              :     {
     516         6775 :       if (gimple_simplified_result_is_gimple_val (&cexpr))
     517          567 :         return cexpr.ops[0];
     518         6208 :       else if (cexpr.code == ABS_EXPR)
     519            2 :         return build1 (ABS_EXPR, type, cexpr.ops[0]);
     520         6206 :       else if (cexpr.code == MIN_EXPR
     521         6206 :                || cexpr.code == MAX_EXPR)
     522         3326 :         return build2 ((tree_code)cexpr.code, type, cexpr.ops[0], cexpr.ops[1]);
     523              :     }
     524              : 
     525        46172 :   return build3 (COND_EXPR, type, cond, rhs, lhs);
     526              : }
     527              : 
     528              : /* Add condition NC to the predicate list of basic block BB.  LOOP is
     529              :    the loop to be if-converted. Use predicate of cd-equivalent block
     530              :    for join bb if it exists: we call basic blocks bb1 and bb2
     531              :    cd-equivalent if they are executed under the same condition.  */
     532              : 
     533              : static inline void
     534       163049 : add_to_predicate_list (class loop *loop, basic_block bb, tree nc)
     535              : {
     536       163049 :   tree bc, *tp;
     537       163049 :   basic_block dom_bb;
     538              : 
     539       163049 :   if (is_true_predicate (nc))
     540        72967 :     return;
     541              : 
     542              :   /* If dominance tells us this basic block is always executed,
     543              :      don't record any predicates for it.  */
     544       163037 :   if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
     545              :     return;
     546              : 
     547        94452 :   dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
     548              :   /* We use notion of cd equivalence to get simpler predicate for
     549              :      join block, e.g. if join block has 2 predecessors with predicates
     550              :      p1 & p2 and p1 & !p2, we'd like to get p1 for it instead of
     551              :      p1 & p2 | p1 & !p2.  */
     552        94452 :   if (dom_bb != loop->header
     553        94452 :       && get_immediate_dominator (CDI_POST_DOMINATORS, dom_bb) == bb)
     554              :     {
     555         4370 :       gcc_assert (flow_bb_inside_loop_p (loop, dom_bb));
     556         4370 :       bc = bb_predicate (dom_bb);
     557         4370 :       if (!is_true_predicate (bc))
     558         4370 :         set_bb_predicate (bb, bc);
     559              :       else
     560            0 :         gcc_assert (is_true_predicate (bb_predicate (bb)));
     561         4370 :       if (dump_file && (dump_flags & TDF_DETAILS))
     562            4 :         fprintf (dump_file, "Use predicate of bb#%d for bb#%d\n",
     563              :                  dom_bb->index, bb->index);
     564              :       return;
     565              :     }
     566              : 
     567        90082 :   if (!is_predicated (bb))
     568        86298 :     bc = nc;
     569              :   else
     570              :     {
     571         3784 :       bc = bb_predicate (bb);
     572         3784 :       bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
     573         3784 :       if (is_true_predicate (bc))
     574              :         {
     575            0 :           reset_bb_predicate (bb);
     576            0 :           return;
     577              :         }
     578              :     }
     579              : 
     580              :   /* Allow a TRUTH_NOT_EXPR around the main predicate.  */
     581        90082 :   if (TREE_CODE (bc) == TRUTH_NOT_EXPR)
     582        32439 :     tp = &TREE_OPERAND (bc, 0);
     583              :   else
     584              :     tp = &bc;
     585        90082 :   if (!is_gimple_val (*tp))
     586              :     {
     587        88424 :       gimple_seq stmts;
     588        88424 :       *tp = force_gimple_operand (*tp, &stmts, true, NULL_TREE);
     589        88424 :       add_bb_predicate_gimplified_stmts (bb, stmts);
     590              :     }
     591        90082 :   set_bb_predicate (bb, bc);
     592              : }
     593              : 
     594              : /* Add the condition COND to the previous condition PREV_COND, and add
     595              :    this to the predicate list of the destination of edge E.  LOOP is
     596              :    the loop to be if-converted.  */
     597              : 
     598              : static void
     599       106730 : add_to_dst_predicate_list (class loop *loop, edge e,
     600              :                            tree prev_cond, tree cond)
     601              : {
     602       106730 :   if (!flow_bb_inside_loop_p (loop, e->dest))
     603              :     return;
     604              : 
     605       106730 :   if (!is_true_predicate (prev_cond))
     606        22860 :     cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
     607              :                         prev_cond, cond);
     608              : 
     609       106730 :   if (!dominated_by_p (CDI_DOMINATORS, loop->latch, e->dest))
     610        86024 :     add_to_predicate_list (loop, e->dest, cond);
     611              : }
     612              : 
     613              : /* Return true if one of the successor edges of BB exits LOOP.  */
     614              : 
     615              : static bool
     616      3455470 : bb_with_exit_edge_p (const class loop *loop, basic_block bb)
     617              : {
     618      3455470 :   edge e;
     619      3455470 :   edge_iterator ei;
     620              : 
     621      6977241 :   FOR_EACH_EDGE (e, ei, bb->succs)
     622      4922566 :     if (loop_exit_edge_p (loop, e))
     623              :       return true;
     624              : 
     625              :   return false;
     626              : }
     627              : 
     628              : /* Given PHI which has more than two arguments, this function checks if
     629              :    it's if-convertible by degenerating its arguments.  Specifically, if
     630              :    below two conditions are satisfied:
     631              : 
     632              :      1) Number of PHI arguments with different values equals to 2 and one
     633              :         argument has the only occurrence.
     634              :      2) The edge corresponding to the unique argument isn't critical edge.
     635              : 
     636              :    Such PHI can be handled as PHIs have only two arguments.  For example,
     637              :    below PHI:
     638              : 
     639              :      res = PHI <A_1(e1), A_1(e2), A_2(e3)>;
     640              : 
     641              :    can be transformed into:
     642              : 
     643              :      res = (predicate of e3) ? A_2 : A_1;
     644              : 
     645              :    Return TRUE if it is the case, FALSE otherwise.  */
     646              : 
     647              : static bool
     648         5667 : phi_convertible_by_degenerating_args (gphi *phi)
     649              : {
     650         5667 :   edge e;
     651         5667 :   tree arg, t1 = NULL, t2 = NULL;
     652         5667 :   unsigned int i, i1 = 0, i2 = 0, n1 = 0, n2 = 0;
     653         5667 :   unsigned int num_args = gimple_phi_num_args (phi);
     654              : 
     655         5667 :   gcc_assert (num_args > 2);
     656              : 
     657        20488 :   for (i = 0; i < num_args; i++)
     658              :     {
     659        17168 :       arg = gimple_phi_arg_def (phi, i);
     660        17168 :       if (t1 == NULL || operand_equal_p (t1, arg, 0))
     661              :         {
     662         7718 :           n1++;
     663         7718 :           i1 = i;
     664         7718 :           t1 = arg;
     665              :         }
     666         9450 :       else if (t2 == NULL || operand_equal_p (t2, arg, 0))
     667              :         {
     668         7103 :           n2++;
     669         7103 :           i2 = i;
     670         7103 :           t2 = arg;
     671              :         }
     672              :       else
     673              :         return false;
     674              :     }
     675              : 
     676         3320 :   if (n1 != 1 && n2 != 1)
     677              :     return false;
     678              : 
     679              :   /* Check if the edge corresponding to the unique arg is critical.  */
     680         3275 :   e = gimple_phi_arg_edge (phi, (n1 == 1) ? i1 : i2);
     681         3275 :   if (EDGE_COUNT (e->src->succs) > 1)
     682            0 :     return false;
     683              : 
     684              :   return true;
     685              : }
     686              : 
     687              : /* Return true when PHI is if-convertible.  PHI is part of loop LOOP
     688              :    and it belongs to basic block BB.  Note at this point, it is sure
     689              :    that PHI is if-convertible.  This function updates global variable
     690              :    ANY_COMPLICATED_PHI if PHI is complicated.  */
     691              : 
     692              : static bool
     693       127499 : if_convertible_phi_p (class loop *loop, basic_block bb, gphi *phi)
     694              : {
     695       127499 :   if (dump_file && (dump_flags & TDF_DETAILS))
     696              :     {
     697          103 :       fprintf (dump_file, "-------------------------\n");
     698          103 :       print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
     699              :     }
     700              : 
     701       127499 :   if (bb != loop->header
     702        50247 :       && gimple_phi_num_args (phi) > 2
     703       133166 :       && !phi_convertible_by_degenerating_args (phi))
     704         2392 :     any_complicated_phi = true;
     705              : 
     706       127499 :   return true;
     707              : }
     708              : 
     709              : /* Records the status of a data reference.  This struct is attached to
     710              :    each DR->aux field.  */
     711              : 
     712              : struct ifc_dr {
     713              :   bool rw_unconditionally;
     714              :   bool w_unconditionally;
     715              :   bool written_at_least_once;
     716              : 
     717              :   tree rw_predicate;
     718              :   tree w_predicate;
     719              :   tree base_w_predicate;
     720              : };
     721              : 
     722              : #define IFC_DR(DR) ((struct ifc_dr *) (DR)->aux)
     723              : #define DR_BASE_W_UNCONDITIONALLY(DR) (IFC_DR (DR)->written_at_least_once)
     724              : #define DR_RW_UNCONDITIONALLY(DR) (IFC_DR (DR)->rw_unconditionally)
     725              : #define DR_W_UNCONDITIONALLY(DR) (IFC_DR (DR)->w_unconditionally)
     726              : 
     727              : /* Iterates over DR's and stores refs, DR and base refs, DR pairs in
     728              :    HASH tables.  While storing them in HASH table, it checks if the
     729              :    reference is unconditionally read or written and stores that as a flag
     730              :    information.  For base reference it checks if it is written atlest once
     731              :    unconditionally and stores it as flag information along with DR.
     732              :    In other words for every data reference A in STMT there exist other
     733              :    accesses to a data reference with the same base with predicates that
     734              :    add up (OR-up) to the true predicate: this ensures that the data
     735              :    reference A is touched (read or written) on every iteration of the
     736              :    if-converted loop.  */
     737              : static void
     738       120505 : hash_memrefs_baserefs_and_store_DRs_read_written_info (data_reference_p a)
     739              : {
     740              : 
     741       120505 :   data_reference_p *master_dr, *base_master_dr;
     742       120505 :   tree base_ref = DR_BASE_OBJECT (a);
     743       120505 :   innermost_loop_behavior *innermost = &DR_INNERMOST (a);
     744       120505 :   tree ca = bb_predicate (gimple_bb (DR_STMT (a)));
     745       120505 :   bool exist1, exist2;
     746              : 
     747       120505 :   master_dr = &innermost_DR_map->get_or_insert (innermost, &exist1);
     748       120505 :   if (!exist1)
     749        92684 :     *master_dr = a;
     750              : 
     751       120505 :   if (DR_IS_WRITE (a))
     752              :     {
     753        38024 :       IFC_DR (*master_dr)->w_predicate
     754        76048 :         = fold_or_predicates (UNKNOWN_LOCATION, ca,
     755        38024 :                               IFC_DR (*master_dr)->w_predicate);
     756        38024 :       if (is_true_predicate (IFC_DR (*master_dr)->w_predicate))
     757        21800 :         DR_W_UNCONDITIONALLY (*master_dr) = true;
     758              :     }
     759       120505 :   IFC_DR (*master_dr)->rw_predicate
     760       241010 :     = fold_or_predicates (UNKNOWN_LOCATION, ca,
     761       120505 :                           IFC_DR (*master_dr)->rw_predicate);
     762       120505 :   if (is_true_predicate (IFC_DR (*master_dr)->rw_predicate))
     763        84064 :     DR_RW_UNCONDITIONALLY (*master_dr) = true;
     764              : 
     765       120505 :   if (DR_IS_WRITE (a))
     766              :     {
     767        38024 :       base_master_dr = &baseref_DR_map->get_or_insert (base_ref, &exist2);
     768        38024 :       if (!exist2)
     769        27752 :         *base_master_dr = a;
     770        38024 :       IFC_DR (*base_master_dr)->base_w_predicate
     771        76048 :         = fold_or_predicates (UNKNOWN_LOCATION, ca,
     772        38024 :                               IFC_DR (*base_master_dr)->base_w_predicate);
     773        38024 :       if (is_true_predicate (IFC_DR (*base_master_dr)->base_w_predicate))
     774        22063 :         DR_BASE_W_UNCONDITIONALLY (*base_master_dr) = true;
     775              :     }
     776       120505 : }
     777              : 
     778              : /* Return TRUE if can prove the index IDX of an array reference REF is
     779              :    within array bound.  Return false otherwise.  */
     780              : 
     781              : static bool
     782       273448 : idx_within_array_bound (tree ref, tree *idx, void *dta)
     783              : {
     784       273448 :   wi::overflow_type overflow;
     785       273448 :   widest_int niter, valid_niter, delta, wi_step;
     786       273448 :   tree ev, init, step;
     787       273448 :   tree low, high;
     788       273448 :   class loop *loop = (class loop*) dta;
     789              : 
     790              :   /* Only support within-bound access for array references.  */
     791       273448 :   if (TREE_CODE (ref) != ARRAY_REF)
     792              :     return false;
     793              : 
     794              :   /* For arrays that might have flexible sizes, it is not guaranteed that they
     795              :      do not extend over their declared size.  */
     796       151166 :   if (array_ref_flexible_size_p (ref))
     797              :     return false;
     798              : 
     799        97861 :   ev = analyze_scalar_evolution (loop, *idx);
     800        97861 :   ev = instantiate_parameters (loop, ev);
     801        97861 :   init = initial_condition (ev);
     802        97861 :   step = evolution_part_in_loop_num (ev, loop->num);
     803              : 
     804        97861 :   if (!init || TREE_CODE (init) != INTEGER_CST
     805        87023 :       || (step && TREE_CODE (step) != INTEGER_CST))
     806              :     return false;
     807              : 
     808        87017 :   low = array_ref_low_bound (ref);
     809        87017 :   high = array_ref_up_bound (ref);
     810              : 
     811              :   /* The case of nonconstant bounds could be handled, but it would be
     812              :      complicated.  */
     813        87017 :   if (TREE_CODE (low) != INTEGER_CST
     814        87017 :       || !high || TREE_CODE (high) != INTEGER_CST)
     815              :     return false;
     816              : 
     817              :   /* Check if the initial idx is within bound.  */
     818        86949 :   if (wi::to_widest (init) < wi::to_widest (low)
     819       173890 :       || wi::to_widest (init) > wi::to_widest (high))
     820              :     return false;
     821              : 
     822              :   /* The idx is always within bound.  */
     823        86933 :   if (!step || integer_zerop (step))
     824              :     return true;
     825              : 
     826        84813 :   if (!max_loop_iterations (loop, &niter))
     827              :     return false;
     828              : 
     829        84813 :   if (wi::to_widest (step) < 0)
     830              :     {
     831          303 :       delta = wi::to_widest (init) - wi::to_widest (low);
     832          303 :       wi_step = -wi::to_widest (step);
     833              :     }
     834              :   else
     835              :     {
     836        84510 :       delta = wi::to_widest (high) - wi::to_widest (init);
     837        84510 :       wi_step = wi::to_widest (step);
     838              :     }
     839              : 
     840        84813 :   valid_niter = wi::div_floor (delta, wi_step, SIGNED, &overflow);
     841              :   /* The iteration space of idx is within array bound.  */
     842       169626 :   if (!overflow && niter <= valid_niter)
     843        84252 :     return true;
     844              : 
     845              :   return false;
     846       273448 : }
     847              : 
     848              : /* Return TRUE if ref is a within bound array reference.  */
     849              : 
     850              : bool
     851       265254 : ref_within_array_bound (gimple *stmt, tree ref)
     852              : {
     853       265254 :   class loop *loop = loop_containing_stmt (stmt);
     854              : 
     855       265254 :   gcc_assert (loop != NULL);
     856       265254 :   return for_each_index (&ref, idx_within_array_bound, loop);
     857              : }
     858              : 
     859              : 
     860              : /* Given a memory reference expression T, return TRUE if base object
     861              :    it refers to is writable.  The base object of a memory reference
     862              :    is the main object being referenced, which is returned by function
     863              :    get_base_address.  */
     864              : 
     865              : static bool
     866         2350 : base_object_writable (tree ref)
     867              : {
     868         2350 :   tree base_tree = get_base_address (ref);
     869              : 
     870         2350 :   return (base_tree
     871         2350 :           && DECL_P (base_tree)
     872         1455 :           && decl_binds_to_current_def_p (base_tree)
     873         3801 :           && !TREE_READONLY (base_tree));
     874              : }
     875              : 
     876              : /* Return true when the memory references of STMT won't trap in the
     877              :    if-converted code.  There are two things that we have to check for:
     878              : 
     879              :    - writes to memory occur to writable memory: if-conversion of
     880              :    memory writes transforms the conditional memory writes into
     881              :    unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
     882              :    into "A[i] = cond ? foo : A[i]", and as the write to memory may not
     883              :    be executed at all in the original code, it may be a readonly
     884              :    memory.  To check that A is not const-qualified, we check that
     885              :    there exists at least an unconditional write to A in the current
     886              :    function.
     887              : 
     888              :    - reads or writes to memory are valid memory accesses for every
     889              :    iteration.  To check that the memory accesses are correctly formed
     890              :    and that we are allowed to read and write in these locations, we
     891              :    check that the memory accesses to be if-converted occur at every
     892              :    iteration unconditionally.
     893              : 
     894              :    Returns true for the memory reference in STMT, same memory reference
     895              :    is read or written unconditionally at least once and the base memory
     896              :    reference is written unconditionally once.  This is to check reference
     897              :    will not write fault.  Also returns true if the memory reference is
     898              :    unconditionally read once then we are conditionally writing to memory
     899              :    which is defined as read and write and is bound to the definition
     900              :    we are seeing.  */
     901              : static bool
     902        20540 : ifcvt_memrefs_wont_trap (gimple *stmt, vec<data_reference_p> drs)
     903              : {
     904              :   /* If DR didn't see a reference here we can't use it to tell
     905              :      whether the ref traps or not.  */
     906        20540 :   if (gimple_uid (stmt) == 0)
     907              :     return false;
     908              : 
     909        20539 :   data_reference_p *master_dr, *base_master_dr;
     910        20539 :   data_reference_p a = drs[gimple_uid (stmt) - 1];
     911              : 
     912        20539 :   tree base = DR_BASE_OBJECT (a);
     913        20539 :   innermost_loop_behavior *innermost = &DR_INNERMOST (a);
     914              : 
     915        20539 :   gcc_assert (DR_STMT (a) == stmt);
     916        20539 :   gcc_assert (DR_BASE_ADDRESS (a) || DR_OFFSET (a)
     917              :               || DR_INIT (a) || DR_STEP (a));
     918              : 
     919        20539 :   master_dr = innermost_DR_map->get (innermost);
     920        20539 :   gcc_assert (master_dr != NULL);
     921              : 
     922        20539 :   base_master_dr = baseref_DR_map->get (base);
     923              : 
     924              :   /* If a is unconditionally written to it doesn't trap.  */
     925        20539 :   if (DR_W_UNCONDITIONALLY (*master_dr))
     926              :     return true;
     927              : 
     928              :   /* If a is unconditionally accessed then ...
     929              : 
     930              :      Even a is conditional access, we can treat it as an unconditional
     931              :      one if it's an array reference and all its index are within array
     932              :      bound.  */
     933        18731 :   if (DR_RW_UNCONDITIONALLY (*master_dr)
     934        18731 :       || ref_within_array_bound (stmt, DR_REF (a)))
     935              :     {
     936              :       /* an unconditional read won't trap.  */
     937         6763 :       if (DR_IS_READ (a))
     938              :         return true;
     939              : 
     940              :       /* an unconditionally write won't trap if the base is written
     941              :          to unconditionally.  */
     942         2387 :       if ((base_master_dr
     943         2387 :            && DR_BASE_W_UNCONDITIONALLY (*base_master_dr))
     944              :           /* or the base is known to be not readonly.  */
     945         4737 :           || base_object_writable (DR_REF (a)))
     946         1488 :         return !ref_can_have_store_data_races (base);
     947              :     }
     948              : 
     949              :   return false;
     950              : }
     951              : 
     952              : /* Return true if STMT could be converted into a masked load or store
     953              :    (conditional load or store based on a mask computed from bb predicate).  */
     954              : 
     955              : static bool
     956        12252 : ifcvt_can_use_mask_load_store (gimple *stmt)
     957              : {
     958              :   /* Check whether this is a load or store.  */
     959        12252 :   tree lhs = gimple_assign_lhs (stmt);
     960        12252 :   bool is_load;
     961        12252 :   tree ref;
     962        12252 :   if (gimple_store_p (stmt))
     963              :     {
     964         2930 :       if (!is_gimple_val (gimple_assign_rhs1 (stmt)))
     965              :         return false;
     966              :       is_load = false;
     967              :       ref = lhs;
     968              :     }
     969         9322 :   else if (gimple_assign_load_p (stmt))
     970              :     {
     971         9321 :       is_load = true;
     972         9321 :       ref = gimple_assign_rhs1 (stmt);
     973              :     }
     974              :   else
     975              :     return false;
     976              : 
     977        12251 :   if (may_be_nonaddressable_p (ref))
     978              :     return false;
     979              : 
     980              :   /* Mask should be integer mode of the same size as the load/store
     981              :      mode.  */
     982        12190 :   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
     983        12190 :   if (!int_mode_for_mode (mode).exists () || VECTOR_MODE_P (mode))
     984              :     return false;
     985              : 
     986        12159 :   if (can_vec_mask_load_store_p (mode, VOIDmode, is_load))
     987              :     return true;
     988              : 
     989              :   return false;
     990              : }
     991              : 
     992              : /* Return true if STMT could be converted from an operation that is
     993              :    unconditional to one that is conditional on a bb predicate mask.  */
     994              : 
     995              : static bool
     996        14311 : ifcvt_can_predicate (gimple *stmt)
     997              : {
     998        14311 :   basic_block bb = gimple_bb (stmt);
     999              : 
    1000          290 :   if (!(flag_tree_loop_vectorize || bb->loop_father->force_vectorize)
    1001        14311 :       || bb->loop_father->dont_vectorize
    1002        28622 :       || gimple_has_volatile_ops (stmt))
    1003              :     return false;
    1004              : 
    1005        14311 :   if (gimple_assign_single_p (stmt))
    1006        12252 :     return ifcvt_can_use_mask_load_store (stmt);
    1007              : 
    1008         2059 :   tree callee;
    1009         2059 :   if (gimple_call_builtin_p (stmt))
    1010          172 :     if ((callee = gimple_call_fndecl (stmt))
    1011          172 :         && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
    1012              :       {
    1013          161 :         auto ifn = associated_internal_fn (callee);
    1014          161 :         auto cond_ifn = get_conditional_internal_fn (ifn);
    1015          161 :         tree type = TREE_TYPE (gimple_call_fntype (stmt));
    1016          161 :         return (cond_ifn != IFN_LAST
    1017          161 :                 && vectorized_internal_fn_supported_p (cond_ifn, type));
    1018              :       }
    1019              : 
    1020         1898 :   auto assign = dyn_cast<gassign *> (stmt);
    1021         1887 :   if (!assign)
    1022              :     return false;
    1023              : 
    1024         1887 :   tree_code code = gimple_assign_rhs_code (assign);
    1025         1887 :   tree lhs_type = TREE_TYPE (gimple_assign_lhs (assign));
    1026         1887 :   tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (assign));
    1027         1887 :   if (!types_compatible_p (lhs_type, rhs_type))
    1028              :     return false;
    1029         1193 :   internal_fn cond_fn = get_conditional_internal_fn (code);
    1030         1193 :   return (cond_fn != IFN_LAST
    1031         1193 :           && vectorized_internal_fn_supported_p (cond_fn, lhs_type));
    1032              : }
    1033              : 
    1034              : /* Return true when STMT is if-convertible.
    1035              : 
    1036              :    GIMPLE_ASSIGN statement is not if-convertible if,
    1037              :    - it is not movable,
    1038              :    - it could trap,
    1039              :    - LHS is not var decl.  */
    1040              : 
    1041              : static bool
    1042        77022 : if_convertible_gimple_assign_stmt_p (gimple *stmt,
    1043              :                                      vec<data_reference_p> refs)
    1044              : {
    1045        77022 :   tree lhs = gimple_assign_lhs (stmt);
    1046              : 
    1047        77022 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1048              :     {
    1049           38 :       fprintf (dump_file, "-------------------------\n");
    1050           38 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    1051              :     }
    1052              : 
    1053        77022 :   if (!is_gimple_reg_type (TREE_TYPE (lhs)))
    1054              :     return false;
    1055              : 
    1056              :   /* Some of these constrains might be too conservative.  */
    1057        76749 :   if (stmt_ends_bb_p (stmt)
    1058        76749 :       || gimple_has_volatile_ops (stmt)
    1059        76663 :       || (TREE_CODE (lhs) == SSA_NAME
    1060        72196 :           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
    1061       153412 :       || gimple_has_side_effects (stmt))
    1062              :     {
    1063           86 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1064            0 :         fprintf (dump_file, "stmt not suitable for ifcvt\n");
    1065              :       return false;
    1066              :     }
    1067              : 
    1068              :   /* tree-into-ssa.cc uses GF_PLF_1, so avoid it, because
    1069              :      in between if_convertible_loop_p and combine_blocks
    1070              :      we can perform loop versioning.  */
    1071        76663 :   gimple_set_plf (stmt, GF_PLF_2, false);
    1072              : 
    1073        76663 :   if ((! gimple_vuse (stmt)
    1074        20540 :        || gimple_could_trap_p_1 (stmt, false, false)
    1075        20540 :        || ! ifcvt_memrefs_wont_trap (stmt, refs))
    1076        89842 :       && gimple_could_trap_p (stmt))
    1077              :     {
    1078        14139 :       if (ifcvt_can_predicate (stmt))
    1079              :         {
    1080         3055 :           gimple_set_plf (stmt, GF_PLF_2, true);
    1081         3055 :           need_to_predicate = true;
    1082         3055 :           return true;
    1083              :         }
    1084        11084 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1085            0 :         fprintf (dump_file, "tree could trap...\n");
    1086              :       return false;
    1087              :     }
    1088        62524 :   else if (gimple_needing_rewrite_undefined (stmt))
    1089              :     /* We have to rewrite stmts with undefined overflow.  */
    1090        28672 :     need_to_rewrite_undefined = true;
    1091              : 
    1092              :   /* When if-converting stores force versioning, likewise if we
    1093              :      ended up generating store data races.  */
    1094       125048 :   if (gimple_vdef (stmt))
    1095         1537 :     need_to_predicate = true;
    1096              : 
    1097              :   return true;
    1098              : }
    1099              : 
    1100              : /* Return true when SW switch statement is equivalent to cond, that
    1101              :    all non default labels point to the same label.
    1102              : 
    1103              :    Fallthrough is not checked for and could even happen
    1104              :    with cond (using goto), so is handled.
    1105              : 
    1106              :    This is intended for switches created by the if-switch-conversion
    1107              :    pass, but can handle some programmer supplied cases too. */
    1108              : 
    1109              : static bool
    1110           70 : if_convertible_switch_p (gswitch *sw)
    1111              : {
    1112           70 :   if (gimple_switch_num_labels (sw) <= 1)
    1113              :     return false;
    1114           70 :   tree label = CASE_LABEL (gimple_switch_label (sw, 1));
    1115          762 :   for (unsigned i = 1; i < gimple_switch_num_labels (sw); i++)
    1116              :     {
    1117          698 :       if (CASE_LABEL (gimple_switch_label (sw, i)) != label)
    1118              :         return false;
    1119              :     }
    1120              :   return true;
    1121              : }
    1122              : 
    1123              : /* Return true when STMT is an if-convertible SIMD clone stmts.
    1124              : 
    1125              :    A SIMD clone statement is if-convertible if:
    1126              :     - it is an GIMPLE_CALL,
    1127              :     - it has a FNDECL,
    1128              :     - it has SIMD clones,
    1129              :     - it has at least one inbranch clone.  */
    1130              : static bool
    1131         1665 : if_convertible_simdclone_stmt_p (gimple *stmt)
    1132              : {
    1133         1665 :   if (!is_gimple_call (stmt))
    1134              :     return false;
    1135              : 
    1136         1665 :   tree fndecl = gimple_call_fndecl (stmt);
    1137         1665 :   if (fndecl)
    1138              :     {
    1139              :       /* We can vectorize some builtins and functions with SIMD "inbranch"
    1140              :          clones.  */
    1141         1385 :       struct cgraph_node *node = cgraph_node::get (fndecl);
    1142         1385 :       if (node && node->simd_clones != NULL)
    1143              :         /* Ensure that at least one clone can be "inbranch".  */
    1144         1972 :         for (struct cgraph_node *n = node->simd_clones; n != NULL;
    1145          963 :              n = n->simdclone->next_clone)
    1146         1970 :           if (n->simdclone->inbranch)
    1147              :             return true;
    1148              :     }
    1149              : 
    1150              :   return false;
    1151              : }
    1152              : 
    1153              : /* Return true when STMT is if-convertible.
    1154              : 
    1155              :    A statement is if-convertible if:
    1156              :    - it is an if-convertible GIMPLE_ASSIGN,
    1157              :    - it is a GIMPLE_LABEL or a GIMPLE_COND,
    1158              :    - it is a switch equivalent to COND
    1159              :    - it is builtins call,
    1160              :    - it is a call to a function with a SIMD clone.  */
    1161              : 
    1162              : static bool
    1163       147422 : if_convertible_stmt_p (gimple *stmt, vec<data_reference_p> refs)
    1164              : {
    1165       147422 :   switch (gimple_code (stmt))
    1166              :     {
    1167              :     case GIMPLE_LABEL:
    1168              :     case GIMPLE_DEBUG:
    1169              :     case GIMPLE_COND:
    1170              :       return true;
    1171              : 
    1172              :     case GIMPLE_SWITCH:
    1173              :       /* Checked elsewhere.  */
    1174              :       return true;
    1175              : 
    1176        77022 :     case GIMPLE_ASSIGN:
    1177        77022 :       return if_convertible_gimple_assign_stmt_p (stmt, refs);
    1178              : 
    1179         1557 :     case GIMPLE_CALL:
    1180         1557 :       {
    1181              :         /* Check if stmt is a simd clone first.  */
    1182         1557 :         if (if_convertible_simdclone_stmt_p (stmt))
    1183              :           {
    1184         1007 :             gimple_set_plf (stmt, GF_PLF_2, true);
    1185         1007 :             need_to_predicate = true;
    1186         1007 :             return true;
    1187              :           }
    1188              : 
    1189              :         /* Check if the call can trap and if so require predication.  */
    1190          550 :         if (gimple_could_trap_p (stmt))
    1191              :           {
    1192          172 :             if (ifcvt_can_predicate (stmt))
    1193              :               {
    1194          108 :                 gimple_set_plf (stmt, GF_PLF_2, true);
    1195          108 :                 need_to_predicate = true;
    1196          108 :                 return true;
    1197              :               }
    1198              :             else
    1199              :               {
    1200           64 :                 if (dump_file && (dump_flags & TDF_DETAILS))
    1201            0 :                   fprintf (dump_file, "stmt could trap...\n");
    1202              :                 return false;
    1203              :               }
    1204              :           }
    1205              : 
    1206              :         /* Check if it's a prefetch.  Many ISAs contain vectorized and/or
    1207              :            conditional prefetches so if-convert should convert them or remove
    1208              :            them.  Mark them as supported.  */
    1209          378 :         if (gimple_call_builtin_p (stmt, BUILT_IN_PREFETCH))
    1210              :           return true;
    1211              : 
    1212              :         /* A CLZ or CTZ call is made defined for a zero argument when it is
    1213              :            executed unconditionally.  */
    1214          376 :         if (gimple_needing_rewrite_undefined (stmt))
    1215           74 :           need_to_rewrite_undefined = true;
    1216              : 
    1217              :         /* There are some IFN_s that are used to replace builtins but have the
    1218              :            same semantics.  Even if MASK_CALL cannot handle them vectorable_call
    1219              :            will insert the proper selection, so do not block conversion.  */
    1220          376 :         int flags = gimple_call_flags (stmt);
    1221          376 :         if ((flags & ECF_CONST)
    1222          376 :             && !(flags & ECF_LOOPING_CONST_OR_PURE)
    1223          752 :             && gimple_call_combined_fn (stmt) != CFN_LAST)
    1224          360 :           return true;
    1225              : 
    1226              :         return false;
    1227              :       }
    1228              : 
    1229            0 :     default:
    1230              :       /* Don't know what to do with 'em so don't do anything.  */
    1231            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1232              :         {
    1233            0 :           fprintf (dump_file, "don't know what to do\n");
    1234            0 :           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    1235              :         }
    1236              :       return false;
    1237              :     }
    1238              : }
    1239              : 
    1240              : /* Assumes that BB has more than 1 predecessors.
    1241              :    Returns false if at least one successor is not on critical edge
    1242              :    and true otherwise.  */
    1243              : 
    1244              : static inline bool
    1245        83357 : all_preds_critical_p (basic_block bb)
    1246              : {
    1247        83357 :   edge e;
    1248        83357 :   edge_iterator ei;
    1249              : 
    1250       163368 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1251       144099 :     if (EDGE_COUNT (e->src->succs) == 1)
    1252              :       return false;
    1253              :   return true;
    1254              : }
    1255              : 
    1256              : /* Return true when BB is if-convertible.  This routine does not check
    1257              :    basic block's statements and phis.
    1258              : 
    1259              :    A basic block is not if-convertible if:
    1260              :    - it is non-empty and it is after the exit block (in BFS order),
    1261              :    - it is after the exit block but before the latch,
    1262              :    - its edges are not normal.
    1263              : 
    1264              :    EXIT_BB is the basic block containing the exit of the LOOP.  BB is
    1265              :    inside LOOP.  */
    1266              : 
    1267              : static bool
    1268       210683 : if_convertible_bb_p (class loop *loop, basic_block bb, basic_block exit_bb)
    1269              : {
    1270       210683 :   edge e;
    1271       210683 :   edge_iterator ei;
    1272              : 
    1273       210683 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1274          146 :     fprintf (dump_file, "----------[%d]-------------\n", bb->index);
    1275              : 
    1276       210683 :   if (EDGE_COUNT (bb->succs) > 2)
    1277              :     return false;
    1278              : 
    1279       421234 :   if (gcall *call = safe_dyn_cast <gcall *> (*gsi_last_bb (bb)))
    1280          211 :     if (gimple_call_ctrl_altering_p (call))
    1281              :       return false;
    1282              : 
    1283       210617 :   if (exit_bb)
    1284              :     {
    1285        38447 :       if (bb != loop->latch)
    1286              :         {
    1287          511 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1288            0 :             fprintf (dump_file, "basic block after exit bb but before latch\n");
    1289              :           return false;
    1290              :         }
    1291        37936 :       else if (!empty_block_p (bb))
    1292              :         {
    1293          646 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1294            0 :             fprintf (dump_file, "non empty basic block after exit bb\n");
    1295              :           return false;
    1296              :         }
    1297        37290 :       else if (bb == loop->latch
    1298        37290 :                && bb != exit_bb
    1299        74580 :                && !dominated_by_p (CDI_DOMINATORS, bb, exit_bb))
    1300              :           {
    1301           11 :             if (dump_file && (dump_flags & TDF_DETAILS))
    1302            0 :               fprintf (dump_file, "latch is not dominated by exit_block\n");
    1303              :             return false;
    1304              :           }
    1305              :     }
    1306              : 
    1307              :   /* Be less adventurous and handle only normal edges.  */
    1308       512343 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1309       302906 :     if (e->flags & (EDGE_EH | EDGE_ABNORMAL | EDGE_IRREDUCIBLE_LOOP))
    1310              :       {
    1311           12 :         if (dump_file && (dump_flags & TDF_DETAILS))
    1312            0 :           fprintf (dump_file, "Difficult to handle edges\n");
    1313              :         return false;
    1314              :       }
    1315              : 
    1316              :   return true;
    1317              : }
    1318              : 
    1319              : /* Return true when all predecessor blocks of BB are visited.  The
    1320              :    VISITED bitmap keeps track of the visited blocks.  */
    1321              : 
    1322              : static bool
    1323      2343672 : pred_blocks_visited_p (basic_block bb, bitmap *visited)
    1324              : {
    1325      2343672 :   edge e;
    1326      2343672 :   edge_iterator ei;
    1327      4040892 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1328      2654135 :     if (!bitmap_bit_p (*visited, e->src->index))
    1329              :       return false;
    1330              : 
    1331              :   return true;
    1332              : }
    1333              : 
    1334              : /* Get body of a LOOP in suitable order for if-conversion.  It is
    1335              :    caller's responsibility to deallocate basic block list.
    1336              :    If-conversion suitable order is, breadth first sort (BFS) order
    1337              :    with an additional constraint: select a block only if all its
    1338              :    predecessors are already selected.  */
    1339              : 
    1340              : static basic_block *
    1341       419542 : get_loop_body_in_if_conv_order (const class loop *loop)
    1342              : {
    1343       419542 :   basic_block *blocks, *blocks_in_bfs_order;
    1344       419542 :   basic_block bb;
    1345       419542 :   bitmap visited;
    1346       419542 :   unsigned int index = 0;
    1347       419542 :   unsigned int visited_count = 0;
    1348              : 
    1349       419542 :   gcc_assert (loop->num_nodes);
    1350       419542 :   gcc_assert (loop->latch != EXIT_BLOCK_PTR_FOR_FN (cfun));
    1351              : 
    1352       419542 :   blocks = XCNEWVEC (basic_block, loop->num_nodes);
    1353       419542 :   visited = BITMAP_ALLOC (NULL);
    1354              : 
    1355       419542 :   blocks_in_bfs_order = get_loop_body_in_bfs_order (loop);
    1356              : 
    1357       419542 :   index = 0;
    1358      4082495 :   while (index < loop->num_nodes)
    1359              :     {
    1360      3243463 :       bb = blocks_in_bfs_order [index];
    1361              : 
    1362      3243463 :       if (bb->flags & BB_IRREDUCIBLE_LOOP)
    1363              :         {
    1364           52 :           free (blocks_in_bfs_order);
    1365           52 :           BITMAP_FREE (visited);
    1366           52 :           free (blocks);
    1367           52 :           return NULL;
    1368              :         }
    1369              : 
    1370      3243411 :       if (!bitmap_bit_p (visited, bb->index))
    1371              :         {
    1372      2343672 :           if (pred_blocks_visited_p (bb, &visited)
    1373      2343672 :               || bb == loop->header)
    1374              :             {
    1375              :               /* This block is now visited.  */
    1376      1806299 :               bitmap_set_bit (visited, bb->index);
    1377      1806299 :               blocks[visited_count++] = bb;
    1378              :             }
    1379              :         }
    1380              : 
    1381      3243411 :       index++;
    1382              : 
    1383      3243411 :       if (index == loop->num_nodes
    1384       482788 :           && visited_count != loop->num_nodes)
    1385              :         /* Not done yet.  */
    1386      3243411 :         index = 0;
    1387              :     }
    1388       419490 :   free (blocks_in_bfs_order);
    1389       419490 :   BITMAP_FREE (visited);
    1390              : 
    1391              :   /* Go through loop and reject if-conversion or lowering of bitfields if we
    1392              :      encounter statements we do not believe the vectorizer will be able to
    1393              :      handle.  If adding a new type of statement here, make sure
    1394              :      'ifcvt_local_dce' is also able to handle it properly.  */
    1395      2633129 :   for (index = 0; index < loop->num_nodes; index++)
    1396              :     {
    1397      1796744 :       basic_block bb = blocks[index];
    1398      1796744 :       gimple_stmt_iterator gsi;
    1399              : 
    1400      1796744 :       bool may_have_nonlocal_labels
    1401      1796744 :         = bb_with_exit_edge_p (loop, bb) || bb == loop->latch;
    1402     16620678 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    1403     13029785 :         switch (gimple_code (gsi_stmt (gsi)))
    1404              :           {
    1405        41294 :           case GIMPLE_LABEL:
    1406        41294 :             if (!may_have_nonlocal_labels)
    1407              :               {
    1408         6931 :                 tree label
    1409         6931 :                   = gimple_label_label (as_a <glabel *> (gsi_stmt (gsi)));
    1410        13862 :                 if (DECL_NONLOCAL (label) || FORCED_LABEL (label))
    1411              :                   {
    1412           49 :                     free (blocks);
    1413           49 :                     return NULL;
    1414              :                   }
    1415              :               }
    1416              :             /* Fallthru.  */
    1417     13027190 :           case GIMPLE_ASSIGN:
    1418     13027190 :           case GIMPLE_CALL:
    1419     13027190 :           case GIMPLE_DEBUG:
    1420     13027190 :           case GIMPLE_COND:
    1421     13027190 :           case GIMPLE_SWITCH:
    1422     13027190 :             gimple_set_uid (gsi_stmt (gsi), 0);
    1423     13027190 :             break;
    1424         2546 :           default:
    1425         2546 :             free (blocks);
    1426         2546 :             return NULL;
    1427              :           }
    1428              :     }
    1429              :   return blocks;
    1430              : }
    1431              : 
    1432              : /* Returns true when the analysis of the predicates for all the basic
    1433              :    blocks in LOOP succeeded.
    1434              : 
    1435              :    predicate_bbs first allocates the predicates of the basic blocks.
    1436              :    These fields are then initialized with the tree expressions
    1437              :    representing the predicates under which a basic block is executed
    1438              :    in the LOOP.  As the loop->header is executed at each iteration, it
    1439              :    has the "true" predicate.  Other statements executed under a
    1440              :    condition are predicated with that condition, for example
    1441              : 
    1442              :    | if (x)
    1443              :    |   S1;
    1444              :    | else
    1445              :    |   S2;
    1446              : 
    1447              :    S1 will be predicated with "x", and
    1448              :    S2 will be predicated with "!x".  */
    1449              : 
    1450              : static void
    1451        37279 : predicate_bbs (loop_p loop)
    1452              : {
    1453        37279 :   unsigned int i;
    1454              : 
    1455       242227 :   for (i = 0; i < loop->num_nodes; i++)
    1456       204948 :     init_bb_predicate (ifc_bbs[i]);
    1457              : 
    1458       242227 :   for (i = 0; i < loop->num_nodes; i++)
    1459              :     {
    1460       204948 :       basic_block bb = ifc_bbs[i];
    1461       204948 :       tree cond;
    1462              : 
    1463              :       /* The loop latch and loop exit block are always executed and
    1464              :          have no extra conditions to be processed: skip them.  */
    1465       279506 :       if (bb == loop->latch
    1466       204948 :           || bb_with_exit_edge_p (loop, bb))
    1467              :         {
    1468        74558 :           reset_bb_predicate (bb);
    1469        74558 :           continue;
    1470              :         }
    1471              : 
    1472       130390 :       cond = bb_predicate (bb);
    1473       260780 :       if (gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (bb)))
    1474              :         {
    1475        53286 :           tree c2;
    1476        53286 :           edge true_edge, false_edge;
    1477        53286 :           location_t loc = gimple_location (stmt);
    1478        53286 :           tree c;
    1479              :           /* gcc.dg/fold-bopcond-1.c shows that despite all forwprop passes
    1480              :              conditions can remain unfolded because of multiple uses so
    1481              :              try to re-fold here, especially to get precision changing
    1482              :              conversions sorted out.  Do not simply fold the stmt since
    1483              :              this is analysis only.  When conditions were embedded in
    1484              :              COND_EXPRs those were folded separately before folding the
    1485              :              COND_EXPR but as they are now outside we have to make sure
    1486              :              to fold them.  Do it here - another opportunity would be to
    1487              :              fold predicates as they are inserted.  */
    1488        53286 :           gimple_match_op cexpr (gimple_match_cond::UNCOND,
    1489        53286 :                                  gimple_cond_code (stmt),
    1490              :                                  boolean_type_node,
    1491              :                                  gimple_cond_lhs (stmt),
    1492        53286 :                                  gimple_cond_rhs (stmt));
    1493        53286 :           if (cexpr.resimplify (NULL, follow_all_ssa_edges)
    1494         4198 :               && cexpr.code.is_tree_code ()
    1495        57484 :               && TREE_CODE_CLASS ((tree_code)cexpr.code) == tcc_comparison)
    1496          548 :             c = build2_loc (loc, (tree_code)cexpr.code, boolean_type_node,
    1497              :                             cexpr.ops[0], cexpr.ops[1]);
    1498              :           else
    1499        52738 :             c = build2_loc (loc, gimple_cond_code (stmt),
    1500              :                             boolean_type_node,
    1501              :                             gimple_cond_lhs (stmt),
    1502              :                             gimple_cond_rhs (stmt));
    1503              : 
    1504              :           /* Add new condition into destination's predicate list.  */
    1505        53286 :           extract_true_false_edges_from_block (gimple_bb (stmt),
    1506              :                                                &true_edge, &false_edge);
    1507              : 
    1508              :           /* If C is true, then TRUE_EDGE is taken.  */
    1509        53286 :           add_to_dst_predicate_list (loop, true_edge, unshare_expr (cond),
    1510              :                                      unshare_expr (c));
    1511              : 
    1512              :           /* If C is false, then FALSE_EDGE is taken.  */
    1513        53286 :           c2 = build1_loc (loc, TRUTH_NOT_EXPR, boolean_type_node,
    1514              :                            unshare_expr (c));
    1515        53286 :           add_to_dst_predicate_list (loop, false_edge,
    1516              :                                      unshare_expr (cond), c2);
    1517              : 
    1518        53286 :           cond = NULL_TREE;
    1519              :         }
    1520              : 
    1521              :       /* Assumes the limited COND like switches checked for earlier.  */
    1522        77104 :       else if (gswitch *sw = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)))
    1523              :         {
    1524           79 :           location_t loc = gimple_location (*gsi_last_bb (bb));
    1525              : 
    1526           79 :           tree default_label = CASE_LABEL (gimple_switch_default_label (sw));
    1527           79 :           tree cond_label = CASE_LABEL (gimple_switch_label (sw, 1));
    1528              : 
    1529           79 :           edge false_edge = find_edge (bb, label_to_block (cfun, default_label));
    1530           79 :           edge true_edge = find_edge (bb, label_to_block (cfun, cond_label));
    1531              : 
    1532              :           /* Create chain of switch tests for each case.  */
    1533           79 :           tree switch_cond = NULL_TREE;
    1534           79 :           tree index = gimple_switch_index (sw);
    1535          802 :           for (unsigned i = 1; i < gimple_switch_num_labels (sw); i++)
    1536              :             {
    1537          723 :               tree label = gimple_switch_label (sw, i);
    1538          723 :               tree case_cond;
    1539          723 :               if (CASE_HIGH (label))
    1540              :                 {
    1541            7 :                   tree low = build2_loc (loc, GE_EXPR,
    1542              :                                          boolean_type_node,
    1543            7 :                                          index, fold_convert_loc (loc, TREE_TYPE (index),
    1544            7 :                                                  CASE_LOW (label)));
    1545           14 :                   tree high = build2_loc (loc, LE_EXPR,
    1546              :                                           boolean_type_node,
    1547            7 :                                           index, fold_convert_loc (loc, TREE_TYPE (index),
    1548            7 :                                                   CASE_HIGH (label)));
    1549            7 :                   case_cond = build2_loc (loc, TRUTH_AND_EXPR,
    1550              :                                           boolean_type_node,
    1551              :                                           low, high);
    1552              :                 }
    1553              :               else
    1554          716 :                 case_cond = build2_loc (loc, EQ_EXPR,
    1555              :                                         boolean_type_node,
    1556              :                                         index,
    1557          716 :                                         fold_convert_loc (loc, TREE_TYPE (index),
    1558          716 :                                                           CASE_LOW (label)));
    1559          723 :               if (i > 1)
    1560          644 :                 switch_cond = build2_loc (loc, TRUTH_OR_EXPR,
    1561              :                                           boolean_type_node,
    1562              :                                           case_cond, switch_cond);
    1563              :               else
    1564              :                 switch_cond = case_cond;
    1565              :             }
    1566              : 
    1567           79 :           add_to_dst_predicate_list (loop, true_edge, unshare_expr (cond),
    1568              :                                      unshare_expr (switch_cond));
    1569           79 :           switch_cond = build1_loc (loc, TRUTH_NOT_EXPR, boolean_type_node,
    1570              :                                     unshare_expr (switch_cond));
    1571           79 :           add_to_dst_predicate_list (loop, false_edge,
    1572              :                                      unshare_expr (cond), switch_cond);
    1573           79 :           cond = NULL_TREE;
    1574              :         }
    1575              : 
    1576              :       /* If current bb has only one successor, then consider it as an
    1577              :          unconditional goto.  */
    1578       281973 :       if (single_succ_p (bb))
    1579              :         {
    1580        77025 :           basic_block bb_n = single_succ (bb);
    1581              : 
    1582              :           /* The successor bb inherits the predicate of its
    1583              :              predecessor.  If there is no predicate in the predecessor
    1584              :              bb, then consider the successor bb as always executed.  */
    1585        77025 :           if (cond == NULL_TREE)
    1586            0 :             cond = boolean_true_node;
    1587              : 
    1588        77025 :           add_to_predicate_list (loop, bb_n, cond);
    1589              :         }
    1590              :     }
    1591              : 
    1592              :   /* The loop header is always executed.  */
    1593        37279 :   reset_bb_predicate (loop->header);
    1594        37279 :   gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
    1595              :               && bb_predicate_gimplified_stmts (loop->latch) == NULL);
    1596        37279 : }
    1597              : 
    1598              : /* Build region by adding loop pre-header and post-header blocks.  */
    1599              : 
    1600              : static vec<basic_block>
    1601        37279 : build_region (class loop *loop)
    1602              : {
    1603        37279 :   vec<basic_block> region = vNULL;
    1604        37279 :   basic_block exit_bb = NULL;
    1605              : 
    1606        37279 :   gcc_assert (ifc_bbs);
    1607              :   /* The first element is loop pre-header.  */
    1608        37279 :   region.safe_push (loop_preheader_edge (loop)->src);
    1609              : 
    1610       279506 :   for (unsigned int i = 0; i < loop->num_nodes; i++)
    1611              :     {
    1612       204948 :       basic_block bb = ifc_bbs[i];
    1613       204948 :       region.safe_push (bb);
    1614              :       /* Find loop postheader.  */
    1615       204948 :       edge e;
    1616       204948 :       edge_iterator ei;
    1617       454745 :       FOR_EACH_EDGE (e, ei, bb->succs)
    1618       287076 :         if (loop_exit_edge_p (loop, e))
    1619              :           {
    1620        37279 :               exit_bb = e->dest;
    1621        37279 :               break;
    1622              :           }
    1623              :     }
    1624              :   /* The last element is loop post-header.  */
    1625        37279 :   gcc_assert (exit_bb);
    1626        37279 :   region.safe_push (exit_bb);
    1627        37279 :   return region;
    1628              : }
    1629              : 
    1630              : /* Return true when LOOP is if-convertible.  This is a helper function
    1631              :    for if_convertible_loop_p.  REFS and DDRS are initialized and freed
    1632              :    in if_convertible_loop_p.  */
    1633              : 
    1634              : static bool
    1635        38525 : if_convertible_loop_p_1 (class loop *loop, vec<data_reference_p> *refs)
    1636              : {
    1637        38525 :   unsigned int i;
    1638        38525 :   basic_block exit_bb = NULL;
    1639        38525 :   vec<basic_block> region;
    1640              : 
    1641        38525 :   calculate_dominance_info (CDI_DOMINATORS);
    1642              : 
    1643       286487 :   for (i = 0; i < loop->num_nodes; i++)
    1644              :     {
    1645       210683 :       basic_block bb = ifc_bbs[i];
    1646              : 
    1647       210683 :       if (!if_convertible_bb_p (loop, bb, exit_bb))
    1648              :         return false;
    1649              : 
    1650       209437 :       if (bb_with_exit_edge_p (loop, bb))
    1651        38447 :         exit_bb = bb;
    1652              :     }
    1653              : 
    1654        37279 :   data_reference_p dr;
    1655              : 
    1656        37279 :   innermost_DR_map
    1657        37279 :           = new hash_map<innermost_loop_behavior_hash, data_reference_p>;
    1658        37279 :   baseref_DR_map = new hash_map<tree_operand_hash, data_reference_p>;
    1659              : 
    1660              :   /* Compute post-dominator tree locally.  */
    1661        37279 :   region = build_region (loop);
    1662        37279 :   calculate_dominance_info_for_region (CDI_POST_DOMINATORS, region);
    1663              : 
    1664        37279 :   predicate_bbs (loop);
    1665              : 
    1666              :   /* Free post-dominator tree since it is not used after predication.  */
    1667        37279 :   free_dominance_info_for_region (cfun, CDI_POST_DOMINATORS, region);
    1668        37279 :   region.release ();
    1669              : 
    1670       195063 :   for (i = 0; refs->iterate (i, &dr); i++)
    1671              :     {
    1672       120505 :       tree ref = DR_REF (dr);
    1673              : 
    1674       120505 :       dr->aux = XNEW (struct ifc_dr);
    1675       120505 :       DR_BASE_W_UNCONDITIONALLY (dr) = false;
    1676       120505 :       DR_RW_UNCONDITIONALLY (dr) = false;
    1677       120505 :       DR_W_UNCONDITIONALLY (dr) = false;
    1678       120505 :       IFC_DR (dr)->rw_predicate = boolean_false_node;
    1679       120505 :       IFC_DR (dr)->w_predicate = boolean_false_node;
    1680       120505 :       IFC_DR (dr)->base_w_predicate = boolean_false_node;
    1681       120505 :       if (gimple_uid (DR_STMT (dr)) == 0)
    1682       119822 :         gimple_set_uid (DR_STMT (dr), i + 1);
    1683              : 
    1684              :       /* If DR doesn't have innermost loop behavior or it's a compound
    1685              :          memory reference, we synthesize its innermost loop behavior
    1686              :          for hashing.  */
    1687       120505 :       if (TREE_CODE (ref) == COMPONENT_REF
    1688              :           || TREE_CODE (ref) == IMAGPART_EXPR
    1689              :           || TREE_CODE (ref) == REALPART_EXPR
    1690        83149 :           || !(DR_BASE_ADDRESS (dr) || DR_OFFSET (dr)
    1691        25714 :                || DR_INIT (dr) || DR_STEP (dr)))
    1692              :         {
    1693       108423 :           while (TREE_CODE (ref) == COMPONENT_REF
    1694        64253 :                  || TREE_CODE (ref) == IMAGPART_EXPR
    1695       172086 :                  || TREE_CODE (ref) == REALPART_EXPR)
    1696        45353 :             ref = TREE_OPERAND (ref, 0);
    1697              : 
    1698        63070 :           memset (&DR_INNERMOST (dr), 0, sizeof (DR_INNERMOST (dr)));
    1699        63070 :           DR_BASE_ADDRESS (dr) = ref;
    1700              :         }
    1701       120505 :       hash_memrefs_baserefs_and_store_DRs_read_written_info (dr);
    1702              :     }
    1703              : 
    1704       184885 :   for (i = 0; i < loop->num_nodes; i++)
    1705              :     {
    1706       159129 :       basic_block bb = ifc_bbs[i];
    1707       159129 :       gimple_stmt_iterator itr;
    1708              : 
    1709              :       /* Check the if-convertibility of statements in predicated BBs.  */
    1710       159129 :       if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
    1711       269411 :         for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
    1712       147422 :           if (!if_convertible_stmt_p (gsi_stmt (itr), *refs))
    1713        38525 :             return false;
    1714              :     }
    1715              : 
    1716              :   /* Checking PHIs needs to be done after stmts, as the fact whether there
    1717              :      are any masked loads or stores affects the tests.  Also check switch
    1718              :      stmts for supported shape as that cannot be skipped for always
    1719              :      executed blocks.  */
    1720       159157 :   for (i = 0; i < loop->num_nodes; i++)
    1721              :     {
    1722       133407 :       basic_block bb = ifc_bbs[i];
    1723       133407 :       gphi_iterator itr;
    1724              : 
    1725       260906 :       for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
    1726       127499 :         if (!if_convertible_phi_p (loop, bb, itr.phi ()))
    1727            6 :           return false;
    1728       347380 :       if (gswitch *s = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)))
    1729           70 :         if (!if_convertible_switch_p (s))
    1730            6 :           return false;
    1731              :     }
    1732              : 
    1733        25750 :   if (dump_file)
    1734           42 :     fprintf (dump_file, "Applying if-conversion\n");
    1735              : 
    1736              :   return true;
    1737              : }
    1738              : 
    1739              : /* Return true when LOOP is if-convertible.
    1740              :    LOOP is if-convertible if:
    1741              :    - it is innermost,
    1742              :    - it has two or more basic blocks,
    1743              :    - it has only one exit,
    1744              :    - loop header is not the exit edge,
    1745              :    - if its basic blocks and phi nodes are if convertible.  */
    1746              : 
    1747              : static bool
    1748        38666 : if_convertible_loop_p (class loop *loop, vec<data_reference_p> *refs)
    1749              : {
    1750        38666 :   edge e;
    1751        38666 :   edge_iterator ei;
    1752        38666 :   bool res = false;
    1753              : 
    1754              :   /* Handle only innermost loop.  */
    1755        38666 :   if (!loop || loop->inner)
    1756              :     {
    1757            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1758            0 :         fprintf (dump_file, "not innermost loop\n");
    1759              :       return false;
    1760              :     }
    1761              : 
    1762              :   /* If only one block, no need for if-conversion.  */
    1763        38666 :   if (loop->num_nodes <= 2)
    1764              :     {
    1765            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1766            0 :         fprintf (dump_file, "less than 2 basic blocks\n");
    1767              :       return false;
    1768              :     }
    1769              : 
    1770              :   /* If one of the loop header's edge is an exit edge then do not
    1771              :      apply if-conversion.  */
    1772       115880 :   FOR_EACH_EDGE (e, ei, loop->header->succs)
    1773        77355 :     if (loop_exit_edge_p (loop, e))
    1774              :       return false;
    1775              : 
    1776        38525 :   res = if_convertible_loop_p_1 (loop, refs);
    1777              : 
    1778        75804 :   delete innermost_DR_map;
    1779        38525 :   innermost_DR_map = NULL;
    1780              : 
    1781        75804 :   delete baseref_DR_map;
    1782        38525 :   baseref_DR_map = NULL;
    1783              : 
    1784        38525 :   return res;
    1785              : }
    1786              : 
    1787              : /* Return reduc_1 if has_nop.
    1788              : 
    1789              :    if (...)
    1790              :      tmp1 = (unsigned type) reduc_1;
    1791              :      tmp2 = tmp1 + rhs2;
    1792              :      reduc_3 = (signed type) tmp2.  */
    1793              : static tree
    1794        11706 : strip_nop_cond_scalar_reduction (bool has_nop, tree op)
    1795              : {
    1796        11706 :   if (!has_nop)
    1797              :     return op;
    1798              : 
    1799          414 :   if (TREE_CODE (op) != SSA_NAME)
    1800              :     return NULL_TREE;
    1801              : 
    1802          368 :   gassign *stmt = safe_dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op));
    1803          368 :   if (!stmt
    1804          368 :       || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
    1805          230 :       || !tree_nop_conversion_p (TREE_TYPE (op), TREE_TYPE
    1806              :                                  (gimple_assign_rhs1 (stmt))))
    1807              :     return NULL_TREE;
    1808              : 
    1809          219 :   return gimple_assign_rhs1 (stmt);
    1810              : }
    1811              : 
    1812              : /* Returns true if def-stmt for phi argument ARG is simple increment/decrement
    1813              :    which is in predicated basic block.
    1814              :    In fact, the following PHI pattern is searching:
    1815              :       loop-header:
    1816              :         reduc_1 = PHI <..., reduc_2>
    1817              :       ...
    1818              :         if (...)
    1819              :           reduc_3 = ...
    1820              :         reduc_2 = PHI <reduc_1, reduc_3>
    1821              : 
    1822              :    ARG_0 and ARG_1 are correspondent PHI arguments.
    1823              :    REDUC, OP0 and OP1 contain reduction stmt and its operands.
    1824              :    INTERMEDIATE is true if PHI does not directly feed the loop-header PHI.
    1825              :    EXTENDED is true if PHI has > 2 arguments.  */
    1826              : 
    1827              : static bool
    1828        45543 : is_cond_scalar_reduction (gimple *phi, gimple **reduc, tree arg_0, tree arg_1,
    1829              :                           tree *op0, tree *op1, bool extended, bool* has_nop,
    1830              :                           gimple **nop_reduc, bool *intermediate)
    1831              : {
    1832        45543 :   tree lhs, r_op1, r_op2, r_nop1, r_nop2;
    1833        45543 :   gimple *stmt;
    1834        45543 :   gimple *header_phi = NULL;
    1835        45543 :   enum tree_code reduction_op;
    1836        45543 :   basic_block bb = gimple_bb (phi);
    1837        45543 :   class loop *loop = bb->loop_father;
    1838        45543 :   edge latch_e = loop_latch_edge (loop);
    1839        45543 :   imm_use_iterator imm_iter;
    1840        45543 :   use_operand_p use_p;
    1841        45543 :   edge e;
    1842        45543 :   edge_iterator ei;
    1843        45543 :   bool result = *has_nop = *intermediate = false;
    1844        45543 :   if (TREE_CODE (arg_0) != SSA_NAME || TREE_CODE (arg_1) != SSA_NAME)
    1845              :     return false;
    1846              : 
    1847        33594 :   if (!extended && gimple_code (SSA_NAME_DEF_STMT (arg_0)) == GIMPLE_PHI)
    1848              :     {
    1849         8549 :       lhs = arg_1;
    1850         8549 :       header_phi = SSA_NAME_DEF_STMT (arg_0);
    1851         8549 :       stmt = SSA_NAME_DEF_STMT (arg_1);
    1852              :     }
    1853        25045 :   else if (gimple_code (SSA_NAME_DEF_STMT (arg_1)) == GIMPLE_PHI)
    1854              :     {
    1855        10963 :       lhs = arg_0;
    1856        10963 :       header_phi = SSA_NAME_DEF_STMT (arg_1);
    1857        10963 :       stmt = SSA_NAME_DEF_STMT (arg_0);
    1858              :     }
    1859              :   else
    1860              :     return false;
    1861        19512 :   if (gimple_bb (header_phi) != loop->header)
    1862              :     return false;
    1863              : 
    1864        18626 :   tree latch_value = PHI_ARG_DEF_FROM_EDGE (header_phi, latch_e);
    1865        18626 :   *intermediate = latch_value != PHI_RESULT (phi);
    1866              : 
    1867        18626 :   gassign *assign = dyn_cast<gassign *> (stmt);
    1868        13999 :   if (!assign || gimple_has_volatile_ops (assign))
    1869              :     return false;
    1870              : 
    1871        13999 :   if (!flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
    1872              :     return false;
    1873              : 
    1874        13890 :   if (!is_predicated (gimple_bb (assign)))
    1875              :     return false;
    1876              : 
    1877              :   /* Check that assign-block is predecessor of phi-block.  */
    1878        10579 :   FOR_EACH_EDGE (e, ei, gimple_bb (assign)->succs)
    1879        10482 :     if (e->dest == bb)
    1880              :       {
    1881              :         result = true;
    1882              :         break;
    1883              :       }
    1884        10385 :   if (!result)
    1885              :     return false;
    1886              : 
    1887        10288 :   if (!has_single_use (lhs))
    1888              :     return false;
    1889              : 
    1890        10218 :   reduction_op = gimple_assign_rhs_code (stmt);
    1891        10218 :   gassign *a_stmt = dyn_cast<gassign *> (stmt);
    1892              : 
    1893              :     /* Catch something like below
    1894              : 
    1895              :      loop-header:
    1896              :      reduc_1 = PHI <..., reduc_2>
    1897              :      ...
    1898              :      if (...)
    1899              :      tmp1 = (unsigned type) reduc_1;
    1900              :      tmp2 = tmp1 + rhs2;
    1901              :      reduc_3 = (signed type) tmp2;
    1902              : 
    1903              :      reduc_2 = PHI <reduc_1, reduc_3>
    1904              : 
    1905              :      and convert to
    1906              : 
    1907              :      reduc_2 = PHI <0, reduc_1>
    1908              :      tmp1 = (unsigned type)reduc_1;
    1909              :      ifcvt = cond_expr ? rhs2 : 0
    1910              :      tmp2 = tmp1 +/- ifcvt;
    1911              :      reduc_1 = (signed type)tmp2;  */
    1912              : 
    1913        10218 :   if (CONVERT_EXPR_CODE_P (reduction_op))
    1914              :     {
    1915          452 :       lhs = gimple_assign_rhs1 (stmt);
    1916          452 :       if (TREE_CODE (lhs) != SSA_NAME
    1917          452 :           || !has_single_use (lhs))
    1918              :         return false;
    1919              : 
    1920          255 :       *nop_reduc = stmt;
    1921          255 :       a_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (lhs));
    1922          249 :       if (!a_stmt || gimple_bb (a_stmt) != gimple_bb (*nop_reduc))
    1923              :         return false;
    1924              : 
    1925          246 :       *has_nop = true;
    1926          246 :       reduction_op = gimple_assign_rhs_code (a_stmt);
    1927              :     }
    1928              : 
    1929              :   /* A reduction with a further latch update must keep the PLUS outside
    1930              :      the conditional expression.  The no-op conversion case is not needed
    1931              :      for this form.  */
    1932        10012 :   if (*intermediate && (*has_nop || reduction_op != PLUS_EXPR))
    1933              :     return false;
    1934              : 
    1935         9777 :   if (reduction_op != PLUS_EXPR
    1936              :       && reduction_op != MINUS_EXPR
    1937         9777 :       && reduction_op != MULT_EXPR
    1938         9777 :       && reduction_op != BIT_IOR_EXPR
    1939              :       && reduction_op != BIT_XOR_EXPR
    1940         4044 :       && reduction_op != BIT_AND_EXPR)
    1941              :     return false;
    1942         5853 :   r_op1 = gimple_assign_rhs1 (a_stmt);
    1943         5853 :   r_op2 = gimple_assign_rhs2 (a_stmt);
    1944              : 
    1945         5853 :   r_nop1 = strip_nop_cond_scalar_reduction (*has_nop, r_op1);
    1946         5853 :   r_nop2 = strip_nop_cond_scalar_reduction (*has_nop, r_op2);
    1947              : 
    1948              :   /* Make R_OP1 to hold reduction variable.  */
    1949         5853 :   if (r_nop2 == PHI_RESULT (header_phi)
    1950         5853 :       && commutative_tree_code (reduction_op))
    1951              :     {
    1952              :       std::swap (r_op1, r_op2);
    1953              :       std::swap (r_nop1, r_nop2);
    1954              :     }
    1955         4674 :   else if (r_nop1 != PHI_RESULT (header_phi))
    1956              :     return false;
    1957              : 
    1958         5521 :   if (*intermediate)
    1959              :     {
    1960          120 :       tree type = TREE_TYPE (gimple_assign_lhs (a_stmt));
    1961          120 :       if (!SCALAR_FLOAT_TYPE_P (type)
    1962          120 :           && (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_TRAPS (type)))
    1963              :         return false;
    1964              : 
    1965          120 :       tree zero = neutral_op_for_reduction (type, PLUS_EXPR, NULL_TREE,
    1966              :                                             false);
    1967              :       /* The inactive addend must leave the reduction value unchanged.  */
    1968          120 :       if (SCALAR_FLOAT_TYPE_P (type)
    1969          120 :           && !fold_real_zero_addition_p (type, r_op1, zero, 0))
    1970              :         return false;
    1971              :     }
    1972              : 
    1973         5519 :   if (*has_nop)
    1974              :     {
    1975              :       /* Check that R_NOP1 is used in nop_stmt or in PHI only.  */
    1976          430 :       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, r_nop1)
    1977              :         {
    1978          304 :           gimple *use_stmt = USE_STMT (use_p);
    1979          304 :           if (is_gimple_debug (use_stmt))
    1980            0 :             continue;
    1981          304 :           if (use_stmt == SSA_NAME_DEF_STMT (r_op1))
    1982          126 :             continue;
    1983          178 :           if (use_stmt != phi)
    1984           42 :             return false;
    1985          168 :         }
    1986              :     }
    1987              : 
    1988              :   /* Check that R_OP1 is used in reduction stmt or in PHI only.  */
    1989        16951 :   FOR_EACH_IMM_USE_FAST (use_p, imm_iter, r_op1)
    1990              :     {
    1991        11742 :       gimple *use_stmt = USE_STMT (use_p);
    1992        11742 :       if (is_gimple_debug (use_stmt))
    1993           29 :         continue;
    1994        11713 :       if (use_stmt == a_stmt)
    1995         5234 :         continue;
    1996         6479 :       if (gimple_code (use_stmt) != GIMPLE_PHI)
    1997          268 :         return false;
    1998          268 :     }
    1999              : 
    2000         5209 :   *op0 = r_op1; *op1 = r_op2;
    2001         5209 :   *reduc = a_stmt;
    2002         5209 :   return true;
    2003              : }
    2004              : 
    2005              : /* Converts conditional scalar reduction into unconditional form, e.g.
    2006              :      bb_4
    2007              :        if (_5 != 0) goto bb_5 else goto bb_6
    2008              :      end_bb_4
    2009              :      bb_5
    2010              :        res_6 = res_13 + 1;
    2011              :      end_bb_5
    2012              :      bb_6
    2013              :        # res_2 = PHI <res_13(4), res_6(5)>
    2014              :      end_bb_6
    2015              : 
    2016              :    will be converted into sequence
    2017              :     _ifc__1 = _5 != 0 ? 1 : 0;
    2018              :     res_2 = res_13 + _ifc__1;
    2019              :   Argument SWAP tells that arguments of conditional expression should be
    2020              :   swapped.  INTERMEDIATE says that this is not the latch value, so the
    2021              :   PLUS must remain unconditional.
    2022              :   We can assume that we versioned the loop for vectorization, so can
    2023              :   create a COND_OP.
    2024              :   Returns rhs of resulting PHI assignment.  */
    2025              : 
    2026              : static tree
    2027         5209 : convert_scalar_cond_reduction (gimple *reduc, gimple_stmt_iterator *gsi,
    2028              :                                tree cond, tree op0, tree op1, bool swap,
    2029              :                                bool has_nop, gimple* nop_reduc,
    2030              :                                bool intermediate)
    2031              : {
    2032         5209 :   gimple_stmt_iterator stmt_it;
    2033         5209 :   gimple *new_assign;
    2034         5209 :   tree rhs;
    2035         5209 :   tree rhs1 = gimple_assign_rhs1 (reduc);
    2036         5209 :   tree lhs = gimple_assign_lhs (reduc);
    2037         5209 :   tree tmp = make_temp_ssa_name (TREE_TYPE (rhs1), NULL, "_ifc_");
    2038         5209 :   tree c;
    2039         5209 :   enum tree_code reduction_op  = gimple_assign_rhs_code (reduc);
    2040         5209 :   tree op_nochange = neutral_op_for_reduction (TREE_TYPE (rhs1), reduction_op,
    2041              :                                                NULL, false);
    2042         5209 :   gimple_seq stmts = NULL;
    2043              : 
    2044         5209 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2045              :     {
    2046           12 :       fprintf (dump_file, "Found cond scalar reduction.\n");
    2047           12 :       print_gimple_stmt (dump_file, reduc, 0, TDF_SLIM);
    2048              :     }
    2049              : 
    2050              :   /* If possible create a COND_OP instead of a COND_EXPR and an OP_EXPR.
    2051              :      The COND_OP will have a neutral_op else value.  */
    2052         5209 :   internal_fn ifn;
    2053         5209 :   ifn = get_conditional_internal_fn (reduction_op);
    2054         5209 :   if (!intermediate
    2055         5209 :       && ifn != IFN_LAST
    2056         5100 :       && vectorized_internal_fn_supported_p (ifn, TREE_TYPE (lhs))
    2057         1363 :       && !VECTOR_TYPE_P (TREE_TYPE (lhs))
    2058         6572 :       && !swap)
    2059              :     {
    2060         1343 :       gcall *cond_call = gimple_build_call_internal (ifn, 4,
    2061              :                                                      unshare_expr (cond),
    2062              :                                                      op0, op1, op0);
    2063         1343 :       gsi_insert_before (gsi, cond_call, GSI_SAME_STMT);
    2064         1343 :       gimple_call_set_lhs (cond_call, tmp);
    2065              :       rhs = tmp;
    2066              :     }
    2067              :   else
    2068              :     {
    2069              :       /* Build cond expression using COND and constant operand
    2070              :          of reduction rhs.  */
    2071         7467 :       c = fold_build_cond_expr (TREE_TYPE (rhs1),
    2072              :                                 unshare_expr (cond),
    2073              :                                 swap ? op_nochange : op1,
    2074              :                                 swap ? op1 : op_nochange);
    2075              :       /* Create assignment stmt and insert it at GSI.  */
    2076         3866 :       new_assign = gimple_build_assign (tmp, c);
    2077         3866 :       gsi_insert_before (gsi, new_assign, GSI_SAME_STMT);
    2078              :       /* Build rhs for unconditional increment/decrement/logic_operation.  */
    2079         3866 :       rhs = gimple_build (&stmts, reduction_op,
    2080         3866 :                           TREE_TYPE (rhs1), op0, tmp);
    2081              :     }
    2082              : 
    2083         5209 :   if (has_nop)
    2084              :     {
    2085          126 :       rhs = gimple_convert (&stmts,
    2086          126 :                             TREE_TYPE (gimple_assign_lhs (nop_reduc)), rhs);
    2087          126 :       stmt_it = gsi_for_stmt (nop_reduc);
    2088          126 :       gsi_remove (&stmt_it, true);
    2089          126 :       release_defs (nop_reduc);
    2090              :     }
    2091         5209 :   gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
    2092              : 
    2093              :   /* Delete original reduction stmt.  */
    2094         5209 :   stmt_it = gsi_for_stmt (reduc);
    2095         5209 :   gsi_remove (&stmt_it, true);
    2096         5209 :   release_defs (reduc);
    2097         5209 :   return rhs;
    2098              : }
    2099              : 
    2100              : /* Generate a simplified conditional.  */
    2101              : 
    2102              : static tree
    2103        51153 : gen_simplified_condition (tree cond, scalar_cond_masked_set_type &cond_set)
    2104              : {
    2105              :   /* Check if the value is already live in a previous branch.  This resolves
    2106              :      nested conditionals from diamond PHI reductions.  */
    2107        51153 :   if (TREE_CODE (cond) == SSA_NAME)
    2108              :     {
    2109        51145 :       gimple *stmt = SSA_NAME_DEF_STMT (cond);
    2110        51145 :       gassign *assign = NULL;
    2111        51145 :       if ((assign = as_a <gassign *> (stmt))
    2112        51145 :            && gimple_assign_rhs_code (assign) == BIT_AND_EXPR)
    2113              :         {
    2114         3229 :           tree arg1 = gimple_assign_rhs1 (assign);
    2115         3229 :           tree arg2 = gimple_assign_rhs2 (assign);
    2116         3229 :           if (cond_set.contains ({ arg1, 1 }))
    2117          238 :             arg1 = boolean_true_node;
    2118              :           else
    2119         2991 :             arg1 = gen_simplified_condition (arg1, cond_set);
    2120              : 
    2121         3229 :           if (cond_set.contains ({ arg2, 1 }))
    2122         1884 :             arg2 = boolean_true_node;
    2123              :           else
    2124         1345 :             arg2 = gen_simplified_condition (arg2, cond_set);
    2125              : 
    2126         3229 :           cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, arg1, arg2);
    2127              :         }
    2128              :     }
    2129        51153 :   return cond;
    2130              : }
    2131              : 
    2132              : /* Structure used to track meta-data on PHI arguments used to generate
    2133              :    most efficient comparison sequence to slatten a PHI node.  */
    2134              : 
    2135              : typedef struct ifcvt_arg_entry
    2136              : {
    2137              :   /* The PHI node argument value.  */
    2138              :   tree arg;
    2139              : 
    2140              :   /* The number of compares required to reach this PHI node from start of the
    2141              :      BB being if-converted.  */
    2142              :   unsigned num_compares;
    2143              : 
    2144              :   /* The number of times this PHI node argument appears in the current PHI
    2145              :      node.  */
    2146              :   unsigned occurs;
    2147              : 
    2148              :   /* The indices at which this PHI arg occurs inside the PHI node.  */
    2149              :   vec <int> *indexes;
    2150              : } ifcvt_arg_entry_t;
    2151              : 
    2152              : /* Produce condition for all occurrences of ARG in PHI node.  Set *INVERT
    2153              :    as to whether the condition is inverted.  */
    2154              : 
    2155              : static tree
    2156         4472 : gen_phi_arg_condition (gphi *phi, ifcvt_arg_entry_t &arg,
    2157              :                        gimple_stmt_iterator *gsi,
    2158              :                        scalar_cond_masked_set_type &cond_set, bool *invert)
    2159              : {
    2160         4472 :   int len;
    2161         4472 :   int i;
    2162         4472 :   tree cond = NULL_TREE;
    2163         4472 :   tree c;
    2164         4472 :   edge e;
    2165              : 
    2166         4472 :   *invert = false;
    2167         4472 :   len = arg.indexes->length ();
    2168         4472 :   gcc_assert (len > 0);
    2169         8987 :   for (i = 0; i < len; i++)
    2170              :     {
    2171         4515 :       e = gimple_phi_arg_edge (phi, (*arg.indexes)[i]);
    2172         4515 :       c = bb_predicate (e->src);
    2173         4515 :       if (is_true_predicate (c))
    2174              :         {
    2175            0 :           cond = c;
    2176            0 :           break;
    2177              :         }
    2178              :       /* If we have just a single inverted predicate, signal that and
    2179              :          instead invert the COND_EXPR arms.  */
    2180         4515 :       if (len == 1 && TREE_CODE (c) == TRUTH_NOT_EXPR)
    2181              :         {
    2182          125 :           c = TREE_OPERAND (c, 0);
    2183          125 :           *invert = true;
    2184              :         }
    2185              : 
    2186         4515 :       c = gen_simplified_condition (c, cond_set);
    2187         4515 :       c = force_gimple_operand_gsi (gsi, unshare_expr (c),
    2188              :                                     true, NULL_TREE, true, GSI_SAME_STMT);
    2189         4515 :       if (cond != NULL_TREE)
    2190              :         {
    2191              :           /* Must build OR expression.  */
    2192           43 :           cond = fold_or_predicates (EXPR_LOCATION (c), c, cond);
    2193           43 :           cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
    2194              :                                            NULL_TREE, true, GSI_SAME_STMT);
    2195              :         }
    2196              :       else
    2197              :         cond = c;
    2198              : 
    2199              :       /* Register the new possibly simplified conditional.  When more than 2
    2200              :          entries in a phi node we chain entries in the false branch, so the
    2201              :          inverted condition is active.  */
    2202         4515 :       scalar_cond_masked_key pred_cond ({ cond, 1 });
    2203         4515 :       if (!*invert)
    2204         4390 :         pred_cond.inverted_p = !pred_cond.inverted_p;
    2205         4515 :       cond_set.add (pred_cond);
    2206              :     }
    2207         4472 :   gcc_assert (cond != NULL_TREE);
    2208         4472 :   return cond;
    2209              : }
    2210              : 
    2211              : /* Factors out an operation from *ARG0 and *ARG1 and
    2212              :    create the new statement at GSI. *RES is the
    2213              :    result of that new statement. Update *ARG0 and *ARG1
    2214              :    and *RES to the new values if the factoring happened.
    2215              :    Loops until all of the factoring is completed.  */
    2216              : 
    2217              : static void
    2218        42302 : factor_out_operators (tree *res, gimple_stmt_iterator *gsi,
    2219              :                       tree *arg0, tree *arg1, gphi *phi)
    2220              : {
    2221        42302 :   gimple_match_op arg0_op, arg1_op;
    2222              : 
    2223        42739 : again:
    2224        42739 :   if (TREE_CODE (*arg0) != SSA_NAME || TREE_CODE (*arg1) != SSA_NAME)
    2225              :     return;
    2226              : 
    2227        31795 :   if (operand_equal_p (*arg0, *arg1))
    2228              :     return;
    2229              : 
    2230              :   /* If either args have > 1 use, then this transformation actually
    2231              :      increases the number of expressions evaluated at runtime.  */
    2232        31795 :   if (!has_single_use (*arg0) || !has_single_use (*arg1))
    2233              :     return;
    2234              : 
    2235         4868 :   gimple *arg0_def_stmt = SSA_NAME_DEF_STMT (*arg0);
    2236         4868 :   if (!gimple_extract_op (arg0_def_stmt, &arg0_op))
    2237              :     return;
    2238              : 
    2239              :   /* Might pick up abnormals from previous bbs so stop the loop.  */
    2240         1477 :   if (arg0_op.operands_occurs_in_abnormal_phi ())
    2241              :     return;
    2242              : 
    2243         1475 :   gimple *arg1_def_stmt = SSA_NAME_DEF_STMT (*arg1);
    2244         1475 :   if (!gimple_extract_op (arg1_def_stmt, &arg1_op))
    2245              :     return;
    2246              : 
    2247              :   /* Might pick up abnormals from previous bbs so stop the loop.  */
    2248         1156 :   if (arg1_op.operands_occurs_in_abnormal_phi ())
    2249              :     return;
    2250              : 
    2251              :   /* No factoring can happen if the codes are different
    2252              :      or the number operands.  */
    2253         1156 :   if (arg1_op.code != arg0_op.code
    2254         1156 :       || arg1_op.num_ops != arg0_op.num_ops)
    2255              :     return;
    2256              : 
    2257          569 :   tree new_arg0, new_arg1;
    2258          569 :   int opnum = find_different_opnum (arg0_op, arg1_op, &new_arg0, &new_arg1);
    2259          569 :   if (opnum == -1)
    2260              :     return;
    2261              : 
    2262          458 :   tree args[2] = { new_arg0, new_arg1 };
    2263          458 :   location_t locs[2];
    2264          458 :   locs[0] = gimple_location (arg0_def_stmt);
    2265          458 :   locs[1] = gimple_location (arg1_def_stmt);
    2266          458 :   if (!factor_operation_ok (arg1_op.code, opnum, args, locs, 2, true, true))
    2267              :     return;
    2268              : 
    2269          437 :   tree new_res = make_ssa_name (TREE_TYPE (new_arg0), NULL);
    2270              : 
    2271              :   /* Create the operation stmt if possible and insert it.  */
    2272              : 
    2273          437 :   gimple_match_op new_op = arg0_op;
    2274          437 :   new_op.ops[opnum] = new_res;
    2275          437 :   gimple_seq seq = NULL;
    2276          437 :   tree result = *res;
    2277          437 :   result = maybe_push_res_to_seq (&new_op, &seq, result);
    2278              : 
    2279              :   /* If we can't create the new statement, release the temp name
    2280              :      and return back.  */
    2281          437 :   if (!result)
    2282              :     {
    2283            0 :       release_ssa_name (new_res);
    2284            0 :       return;
    2285              :     }
    2286          437 :   gsi_insert_seq_before (gsi, seq, GSI_CONTINUE_LINKING);
    2287              : 
    2288          437 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2289              :     {
    2290            1 :       fprintf (dump_file, "PHI ");
    2291            1 :       print_generic_expr (dump_file, gimple_phi_result (phi));
    2292            1 :       fprintf (dump_file,
    2293              :                " changed to factor operation out from COND_EXPR.\n");
    2294            1 :       fprintf (dump_file, "New stmt with OPERATION that defines ");
    2295            1 :       print_generic_expr (dump_file, result);
    2296            1 :       fprintf (dump_file, ".\n");
    2297              :     }
    2298              : 
    2299              :   /* Remove the old operation(s) that has single use.  */
    2300          437 :   gimple_stmt_iterator gsi_for_def;
    2301              : 
    2302          437 :   gsi_for_def = gsi_for_stmt (arg0_def_stmt);
    2303          437 :   gsi_remove (&gsi_for_def, true);
    2304          437 :   release_defs (arg0_def_stmt);
    2305          437 :   gsi_for_def = gsi_for_stmt (arg1_def_stmt);
    2306          437 :   gsi_remove (&gsi_for_def, true);
    2307          437 :   release_defs (arg1_def_stmt);
    2308              : 
    2309              :   /* Update the arguments and try again.  */
    2310          437 :   *arg0 = new_arg0;
    2311          437 :   *arg1 = new_arg1;
    2312          437 :   *res = new_res;
    2313              : 
    2314              :   /* Update the phi node too. */
    2315          437 :   gimple_phi_set_result (phi, new_res);
    2316          437 :   SET_PHI_ARG_DEF (phi, 0, new_arg0);
    2317          437 :   SET_PHI_ARG_DEF (phi, 1, new_arg1);
    2318          437 :   update_stmt (phi);
    2319              : 
    2320          437 :   goto again;
    2321              : }
    2322              : 
    2323              : /* Create the smallest nested conditional possible.  On pre-order we record
    2324              :    which conditionals are live, and on post-order rewrite the chain by removing
    2325              :    already active conditions.
    2326              : 
    2327              :    As an example we simplify:
    2328              : 
    2329              :   _7 = a_10 < 0;
    2330              :   _21 = a_10 >= 0;
    2331              :   _22 = a_10 < e_11(D);
    2332              :   _23 = _21 & _22;
    2333              :   _ifc__42 = _23 ? t_13 : 0;
    2334              :   t_6 = _7 ? 1 : _ifc__42
    2335              : 
    2336              :   into
    2337              : 
    2338              :   _7 = a_10 < 0;
    2339              :   _22 = a_10 < e_11(D);
    2340              :   _ifc__42 = _22 ? t_13 : 0;
    2341              :   t_6 = _7 ? 1 : _ifc__42;
    2342              : 
    2343              :   which produces better code.  */
    2344              : 
    2345              : static tree
    2346         6698 : gen_phi_nest_statement (gphi *phi, gimple_stmt_iterator *gsi,
    2347              :                         scalar_cond_masked_set_type &cond_set, tree type,
    2348              :                         gimple **res_stmt, tree lhs0,
    2349              :                         vec<struct ifcvt_arg_entry> &args, unsigned idx)
    2350              : {
    2351        13396 :   if (idx == args.length ())
    2352         2226 :     return args[idx - 1].arg;
    2353              : 
    2354         4472 :   bool invert;
    2355         4472 :   tree cond = gen_phi_arg_condition (phi, args[idx - 1], gsi, cond_set,
    2356              :                                      &invert);
    2357         4472 :   tree arg1 = gen_phi_nest_statement (phi, gsi, cond_set, type, res_stmt, lhs0,
    2358              :                                       args, idx + 1);
    2359              : 
    2360         4472 :   unsigned prev = idx;
    2361         4472 :   unsigned curr = prev - 1;
    2362         4472 :   tree arg0 = args[curr].arg;
    2363         4472 :   tree rhs, lhs;
    2364         4472 :   if (idx > 1)
    2365         2246 :     lhs = make_temp_ssa_name (type, NULL, "_ifc_");
    2366              :   else
    2367              :     lhs = lhs0;
    2368              : 
    2369         4472 :   if (invert)
    2370          125 :     rhs = fold_build_cond_expr (type, unshare_expr (cond),
    2371              :                                 arg1, arg0);
    2372              :   else
    2373         4347 :     rhs = fold_build_cond_expr (type, unshare_expr (cond),
    2374              :                                 arg0, arg1);
    2375         4472 :   gassign *new_stmt = gimple_build_assign (lhs, rhs);
    2376         4472 :   gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    2377         4472 :   update_stmt (new_stmt);
    2378         4472 :   *res_stmt = new_stmt;
    2379         4472 :   return lhs;
    2380              : }
    2381              : 
    2382              : /* When flattening a PHI node we have a choice of which conditions to test to
    2383              :    for all the paths from the start of the dominator block of the BB with the
    2384              :    PHI node.  If the PHI node has X arguments we have to only test X - 1
    2385              :    conditions as the last one is implicit.  It does matter which conditions we
    2386              :    test first.  We should test the shortest condition first (distance here is
    2387              :    measures in the number of logical operators in the condition) and the
    2388              :    longest one last.  This allows us to skip testing the most expensive
    2389              :    condition.  To accomplish this we need to sort the conditions.  P1 and P2
    2390              :    are sorted first based on the number of logical operations (num_compares)
    2391              :    and then by how often they occur in the PHI node.  */
    2392              : 
    2393              : static int
    2394        37743 : cmp_arg_entry (const void *p1, const void *p2, void * /* data.  */)
    2395              : {
    2396        37743 :   const ifcvt_arg_entry sval1 = *(const ifcvt_arg_entry *)p1;
    2397        37743 :   const ifcvt_arg_entry sval2 = *(const ifcvt_arg_entry *)p2;
    2398              : 
    2399        37743 :   if (sval1.num_compares < sval2.num_compares)
    2400              :     return -1;
    2401        12954 :   else if (sval1.num_compares > sval2.num_compares)
    2402              :     return 1;
    2403              : 
    2404          636 :   if (sval1.occurs < sval2.occurs)
    2405              :     return -1;
    2406          636 :   else if (sval1.occurs > sval2.occurs)
    2407            0 :     return 1;
    2408              : 
    2409              :   return 0;
    2410              : }
    2411              : 
    2412              : /* Replace a scalar PHI node with a COND_EXPR using COND as condition.
    2413              :    This routine can handle PHI nodes with more than two arguments.
    2414              : 
    2415              :    For example,
    2416              :      S1: A = PHI <x1(1), x2(5)>
    2417              :    is converted into,
    2418              :      S2: A = cond ? x1 : x2;
    2419              : 
    2420              :    The generated code is inserted at GSI that points to the top of
    2421              :    basic block's statement list.
    2422              :    If PHI node has more than two arguments a chain of conditional
    2423              :    expression is produced.  */
    2424              : 
    2425              : 
    2426              : static void
    2427        47824 : predicate_scalar_phi (gphi *phi, gimple_stmt_iterator *gsi)
    2428              : {
    2429        47824 :   gimple *new_stmt = NULL, *reduc, *nop_reduc;
    2430        47824 :   tree rhs, res, arg0, arg1, op0, op1, scev;
    2431        47824 :   tree cond;
    2432        47824 :   unsigned int index0;
    2433        47824 :   edge e;
    2434        47824 :   basic_block bb;
    2435        47824 :   unsigned int i;
    2436        47824 :   bool has_nop, intermediate;
    2437              : 
    2438        47824 :   res = gimple_phi_result (phi);
    2439        95648 :   if (virtual_operand_p (res))
    2440        42357 :     return;
    2441              : 
    2442        47824 :   if ((rhs = degenerate_phi_result (phi))
    2443        47824 :       || ((scev = analyze_scalar_evolution (gimple_bb (phi)->loop_father,
    2444              :                                             res))
    2445        47786 :           && !chrec_contains_undetermined (scev)
    2446        47786 :           && scev != res
    2447           17 :           && (rhs = gimple_phi_arg_def (phi, 0))))
    2448              :     {
    2449           55 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2450              :         {
    2451            0 :           fprintf (dump_file, "Degenerate phi!\n");
    2452            0 :           print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
    2453              :         }
    2454           55 :       new_stmt = gimple_build_assign (res, rhs);
    2455           55 :       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    2456           55 :       update_stmt (new_stmt);
    2457           55 :       return;
    2458              :     }
    2459              : 
    2460        47769 :   bb = gimple_bb (phi);
    2461              :   /* Keep track of conditionals already seen.  */
    2462        47769 :   scalar_cond_masked_set_type cond_set;
    2463        47769 :   if (EDGE_COUNT (bb->preds) == 2)
    2464              :     {
    2465              :       /* Predicate ordinary PHI node with 2 arguments.  */
    2466        42302 :       edge first_edge, second_edge;
    2467        42302 :       basic_block true_bb;
    2468        42302 :       first_edge = EDGE_PRED (bb, 0);
    2469        42302 :       second_edge = EDGE_PRED (bb, 1);
    2470        42302 :       cond = bb_predicate (first_edge->src);
    2471        42302 :       cond_set.add ({ cond, 1 });
    2472        42302 :       if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
    2473        21844 :         std::swap (first_edge, second_edge);
    2474        42302 :       if (EDGE_COUNT (first_edge->src->succs) > 1)
    2475              :         {
    2476        19825 :           cond = bb_predicate (second_edge->src);
    2477        19825 :           if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
    2478        10594 :             cond = TREE_OPERAND (cond, 0);
    2479              :           else
    2480              :             first_edge = second_edge;
    2481              :         }
    2482              :       else
    2483        22477 :         cond = bb_predicate (first_edge->src);
    2484              : 
    2485              :       /* Gimplify the condition to a valid cond-expr conditional operand.  */
    2486        42302 :       cond = gen_simplified_condition (cond, cond_set);
    2487        42302 :       cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
    2488              :                                        NULL_TREE, true, GSI_SAME_STMT);
    2489        42302 :       true_bb = first_edge->src;
    2490        42302 :       if (EDGE_PRED (bb, 1)->src == true_bb)
    2491              :         {
    2492        31075 :           arg0 = gimple_phi_arg_def (phi, 1);
    2493        31075 :           arg1 = gimple_phi_arg_def (phi, 0);
    2494              :         }
    2495              :       else
    2496              :         {
    2497        11227 :           arg0 = gimple_phi_arg_def (phi, 0);
    2498        11227 :           arg1 = gimple_phi_arg_def (phi, 1);
    2499              :         }
    2500              : 
    2501              :       /* Factor out operand if possible. This can only be done easily
    2502              :          for PHI with 2 elements.  */
    2503        42302 :       factor_out_operators (&res, gsi, &arg0, &arg1, phi);
    2504              : 
    2505        42302 :       if (is_cond_scalar_reduction (phi, &reduc, arg0, arg1,
    2506              :                                     &op0, &op1, false, &has_nop,
    2507              :                                     &nop_reduc, &intermediate))
    2508              :         {
    2509              :           /* Convert reduction stmt into vectorizable form.  */
    2510         8502 :           rhs = convert_scalar_cond_reduction (reduc, gsi, cond, op0, op1,
    2511         4251 :                                                true_bb != gimple_bb (reduc),
    2512              :                                                has_nop, nop_reduc, intermediate);
    2513         4251 :           redundant_ssa_names.safe_push (std::make_pair (res, rhs));
    2514              :         }
    2515              :       else
    2516              :         /* Build new RHS using selected condition and arguments.  */
    2517        38051 :         rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
    2518              :                                     arg0, arg1);
    2519        42302 :       new_stmt = gimple_build_assign (res, rhs);
    2520        42302 :       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    2521        42302 :       gimple_stmt_iterator new_gsi = gsi_for_stmt (new_stmt);
    2522        42302 :       if (fold_stmt (&new_gsi, follow_all_ssa_edges))
    2523              :         {
    2524         1597 :           new_stmt = gsi_stmt (new_gsi);
    2525         1597 :           update_stmt (new_stmt);
    2526              :         }
    2527              : 
    2528        42302 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2529              :         {
    2530           28 :           fprintf (dump_file, "new phi replacement stmt\n");
    2531           28 :           print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
    2532              :         }
    2533        42302 :       return;
    2534              :     }
    2535              : 
    2536              :   /* Create hashmap for PHI node which contain vector of argument indexes
    2537              :      having the same value.  */
    2538         5467 :   bool swap = false;
    2539         5467 :   hash_map<tree_operand_hash, auto_vec<int> > phi_arg_map;
    2540         5467 :   unsigned int num_args = gimple_phi_num_args (phi);
    2541              :   /* Vector of different PHI argument values.  */
    2542         5467 :   auto_vec<ifcvt_arg_entry_t> args;
    2543              : 
    2544              :   /* Compute phi_arg_map, determine the list of unique PHI args and the indices
    2545              :      where they are in the PHI node.  The indices will be used to determine
    2546              :      the conditions to apply and their complexity.  */
    2547        22115 :   for (i = 0; i < num_args; i++)
    2548              :     {
    2549        16648 :       tree arg;
    2550              : 
    2551        16648 :       arg = gimple_phi_arg_def (phi, i);
    2552        16648 :       if (!phi_arg_map.get (arg))
    2553        13180 :         args.safe_push ({ arg, 0, 0, NULL });
    2554        16648 :       phi_arg_map.get_or_insert (arg).safe_push (i);
    2555              :     }
    2556              : 
    2557              :   /* Determine element with max number of occurrences and complexity.  Looking
    2558              :      at only number of occurrences as a measure for complexity isn't enough as
    2559              :      all usages can be unique but the comparisons to reach the PHI node differ
    2560              :      per branch.  */
    2561        18647 :   for (unsigned i = 0; i < args.length (); i++)
    2562              :     {
    2563        13180 :       unsigned int len = 0;
    2564        13180 :       vec<int> *indices = phi_arg_map.get (args[i].arg);
    2565        56188 :       for (int index : *indices)
    2566              :         {
    2567        16648 :           edge e = gimple_phi_arg_edge (phi, index);
    2568        16648 :           len += get_bb_num_predicate_stmts (e->src);
    2569              :         }
    2570              : 
    2571        13180 :       unsigned occur = indices->length ();
    2572        13180 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2573            7 :         fprintf (dump_file, "Ranking %d as len=%d, idx=%d\n", i, len, occur);
    2574        13180 :       args[i].num_compares = len;
    2575        13180 :       args[i].occurs = occur;
    2576        13180 :       args[i].indexes = indices;
    2577              :     }
    2578              : 
    2579              :   /* Sort elements based on rankings ARGS.  */
    2580         5467 :   args.stablesort (cmp_arg_entry, NULL);
    2581              : 
    2582              :   /* Handle one special case when number of arguments with different values
    2583              :      is equal 2 and one argument has the only occurrence.  Such PHI can be
    2584              :      handled as if would have only 2 arguments.  */
    2585         5467 :   if (args.length () == 2
    2586         8751 :       && args[0].indexes->length () == 1)
    2587              :     {
    2588         3241 :       index0 = (*args[0].indexes)[0];
    2589         3241 :       arg0 = args[0].arg;
    2590         3241 :       arg1 = args[1].arg;
    2591         3241 :       e = gimple_phi_arg_edge (phi, index0);
    2592         3241 :       cond = bb_predicate (e->src);
    2593         3241 :       if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
    2594              :         {
    2595           36 :           swap = true;
    2596           36 :           cond = TREE_OPERAND (cond, 0);
    2597              :         }
    2598              :       /* Gimplify the condition to a valid cond-expr conditional operand.  */
    2599         3241 :       cond = force_gimple_operand_gsi (gsi, unshare_expr (cond), true,
    2600              :                                        NULL_TREE, true, GSI_SAME_STMT);
    2601         3241 :       if (!(is_cond_scalar_reduction (phi, &reduc, arg0 , arg1,
    2602              :                                       &op0, &op1, true, &has_nop, &nop_reduc,
    2603              :                                       &intermediate)))
    2604         4532 :         rhs = fold_build_cond_expr (TREE_TYPE (res), unshare_expr (cond),
    2605              :                                     swap ? arg1 : arg0,
    2606              :                                     swap ? arg0 : arg1);
    2607              :       else
    2608              :         {
    2609              :           /* Convert reduction stmt into vectorizable form.  */
    2610          958 :           rhs = convert_scalar_cond_reduction (reduc, gsi, cond, op0, op1,
    2611              :                                                swap, has_nop, nop_reduc,
    2612              :                                                intermediate);
    2613          958 :           redundant_ssa_names.safe_push (std::make_pair (res, rhs));
    2614              :         }
    2615         3241 :       new_stmt = gimple_build_assign (res, rhs);
    2616         3241 :       gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
    2617         3241 :       update_stmt (new_stmt);
    2618              :     }
    2619              :   else
    2620              :     {
    2621              :       /* Common case.  */
    2622         2226 :       tree type = TREE_TYPE (gimple_phi_result (phi));
    2623         2226 :       gen_phi_nest_statement (phi, gsi, cond_set, type, &new_stmt, res,
    2624              :                               args, 1);
    2625              :     }
    2626              : 
    2627         5467 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2628              :     {
    2629            3 :       fprintf (dump_file, "new extended phi replacement stmt\n");
    2630            3 :       print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
    2631              :     }
    2632        47769 : }
    2633              : 
    2634              : /* Replaces in LOOP all the scalar phi nodes other than those in the
    2635              :    LOOP->header block with conditional modify expressions.  */
    2636              : 
    2637              : static void
    2638        25750 : predicate_all_scalar_phis (class loop *loop)
    2639              : {
    2640        25750 :   basic_block bb;
    2641        25750 :   unsigned int orig_loop_num_nodes = loop->num_nodes;
    2642        25750 :   unsigned int i;
    2643              : 
    2644       133401 :   for (i = 1; i < orig_loop_num_nodes; i++)
    2645              :     {
    2646       107651 :       gphi *phi;
    2647       107651 :       gimple_stmt_iterator gsi;
    2648       107651 :       gphi_iterator phi_gsi;
    2649       107651 :       bb = ifc_bbs[i];
    2650              : 
    2651       107651 :       if (bb == loop->header)
    2652        77443 :         continue;
    2653              : 
    2654       107651 :       phi_gsi = gsi_start_phis (bb);
    2655       107651 :       if (gsi_end_p (phi_gsi))
    2656        77443 :         continue;
    2657              : 
    2658        30208 :       gsi = gsi_after_labels (bb);
    2659        80455 :       while (!gsi_end_p (phi_gsi))
    2660              :         {
    2661        50247 :           phi = phi_gsi.phi ();
    2662       100494 :           if (virtual_operand_p (gimple_phi_result (phi)))
    2663         2423 :             gsi_next (&phi_gsi);
    2664              :           else
    2665              :             {
    2666        47824 :               predicate_scalar_phi (phi, &gsi);
    2667        47824 :               remove_phi_node (&phi_gsi, false);
    2668              :             }
    2669              :         }
    2670              :     }
    2671        25750 : }
    2672              : 
    2673              : /* Insert in each basic block of LOOP the statements produced by the
    2674              :    gimplification of the predicates.  */
    2675              : 
    2676              : static void
    2677        25750 : insert_gimplified_predicates (loop_p loop)
    2678              : {
    2679        25750 :   unsigned int i;
    2680              : 
    2681       159151 :   for (i = 0; i < loop->num_nodes; i++)
    2682              :     {
    2683       133401 :       basic_block bb = ifc_bbs[i];
    2684       133401 :       gimple_seq stmts;
    2685       133401 :       if (!is_predicated (bb))
    2686        80757 :         gcc_assert (bb_predicate_gimplified_stmts (bb) == NULL);
    2687       133401 :       if (!is_predicated (bb))
    2688              :         {
    2689              :           /* Do not insert statements for a basic block that is not
    2690              :              predicated.  Also make sure that the predicate of the
    2691              :              basic block is set to true.  */
    2692        80757 :           reset_bb_predicate (bb);
    2693        80757 :           continue;
    2694              :         }
    2695              : 
    2696        52644 :       stmts = bb_predicate_gimplified_stmts (bb);
    2697        52644 :       if (stmts)
    2698              :         {
    2699        52140 :           if (need_to_predicate)
    2700              :             {
    2701              :               /* Insert the predicate of the BB just after the label,
    2702              :                  as the if-conversion of memory writes will use this
    2703              :                  predicate.  */
    2704         5888 :               gimple_stmt_iterator gsi = gsi_after_labels (bb);
    2705         5888 :               gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    2706              :             }
    2707              :           else
    2708              :             {
    2709              :               /* Insert the predicate of the BB at the end of the BB
    2710              :                  as this would reduce the register pressure: the only
    2711              :                  use of this predicate will be in successor BBs.  */
    2712        46252 :               gimple_stmt_iterator gsi = gsi_last_bb (bb);
    2713              : 
    2714        46252 :               if (gsi_end_p (gsi)
    2715        46252 :                   || stmt_ends_bb_p (gsi_stmt (gsi)))
    2716        25379 :                 gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    2717              :               else
    2718        20873 :                 gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
    2719              :             }
    2720              : 
    2721              :           /* Once the sequence is code generated, set it to NULL.  */
    2722       133401 :           set_bb_predicate_gimplified_stmts (bb, NULL, true);
    2723              :         }
    2724              :     }
    2725        25750 : }
    2726              : 
    2727              : /* Helper function for predicate_statements. Returns index of existent
    2728              :    mask if it was created for given SIZE and -1 otherwise.  */
    2729              : 
    2730              : static int
    2731         1034 : mask_exists (int size, const vec<int> &vec)
    2732              : {
    2733         1034 :   unsigned int ix;
    2734         1034 :   int v;
    2735         1123 :   FOR_EACH_VEC_ELT (vec, ix, v)
    2736         1062 :     if (v == size)
    2737          973 :       return (int) ix;
    2738              :   return -1;
    2739              : }
    2740              : 
    2741              : /* Helper function for predicate_statements.  STMT is a memory read or
    2742              :    write and it needs to be predicated by MASK.  Return a statement
    2743              :    that does so.  */
    2744              : 
    2745              : static gimple *
    2746         2288 : predicate_load_or_store (gimple_stmt_iterator *gsi, gassign *stmt, tree mask)
    2747              : {
    2748         2288 :   gcall *new_stmt;
    2749              : 
    2750         2288 :   tree lhs = gimple_assign_lhs (stmt);
    2751         2288 :   tree rhs = gimple_assign_rhs1 (stmt);
    2752         2288 :   tree ref = TREE_CODE (lhs) == SSA_NAME ? rhs : lhs;
    2753         2288 :   mark_addressable (ref);
    2754         2288 :   tree addr = force_gimple_operand_gsi (gsi, build_fold_addr_expr (ref),
    2755              :                                         true, NULL_TREE, true, GSI_SAME_STMT);
    2756         2288 :   tree ptr = build_int_cst (reference_alias_ptr_type (ref),
    2757         2288 :                             get_object_alignment (ref));
    2758              :   /* Copy points-to info if possible.  */
    2759         3713 :   if (TREE_CODE (addr) == SSA_NAME && !ssa_name_pt_solution (addr))
    2760          862 :     copy_ref_info (build2 (MEM_REF, TREE_TYPE (ref), addr, ptr),
    2761              :                    ref);
    2762         2288 :   if (TREE_CODE (lhs) == SSA_NAME)
    2763              :     {
    2764              :       /* Get a zero else value.  This might not be what a target actually uses
    2765              :          but we cannot be sure about which vector mode the vectorizer will
    2766              :          choose.  Therefore, leave the decision whether we need to force the
    2767              :          inactive elements to zero to the vectorizer.  */
    2768         1329 :       tree els = vect_get_mask_load_else (MASK_LOAD_ELSE_ZERO,
    2769         1329 :                                           TREE_TYPE (lhs));
    2770              : 
    2771         1329 :       new_stmt
    2772         1329 :         = gimple_build_call_internal (IFN_MASK_LOAD, 4, addr,
    2773              :                                       ptr, mask, els);
    2774              : 
    2775         1329 :       gimple_call_set_lhs (new_stmt, lhs);
    2776         2658 :       gimple_set_vuse (new_stmt, gimple_vuse (stmt));
    2777              :     }
    2778              :   else
    2779              :     {
    2780          959 :       new_stmt
    2781          959 :         = gimple_build_call_internal (IFN_MASK_STORE, 4, addr, ptr,
    2782              :                                       mask, rhs);
    2783          959 :       gimple_move_vops (new_stmt, stmt);
    2784              :     }
    2785         2288 :   gimple_call_set_nothrow (new_stmt, true);
    2786         2288 :   return new_stmt;
    2787              : }
    2788              : 
    2789              : /* STMT uses OP_LHS.  Check whether it is equivalent to:
    2790              : 
    2791              :      ... = OP_MASK ? OP_LHS : X;
    2792              : 
    2793              :    Return X if so, otherwise return null.  OP_MASK is an SSA_NAME that is
    2794              :    known to have value OP_COND.  */
    2795              : 
    2796              : static tree
    2797          879 : check_redundant_cond_expr (gimple *stmt, tree op_mask, tree op_cond,
    2798              :                            tree op_lhs)
    2799              : {
    2800          879 :   gassign *assign = dyn_cast <gassign *> (stmt);
    2801         1089 :   if (!assign || gimple_assign_rhs_code (assign) != COND_EXPR)
    2802              :     return NULL_TREE;
    2803              : 
    2804          230 :   tree use_cond = gimple_assign_rhs1 (assign);
    2805          230 :   tree if_true = gimple_assign_rhs2 (assign);
    2806          230 :   tree if_false = gimple_assign_rhs3 (assign);
    2807              : 
    2808           71 :   if ((use_cond == op_mask || operand_equal_p (use_cond, op_cond, 0))
    2809          230 :       && if_true == op_lhs)
    2810              :     return if_false;
    2811              : 
    2812           71 :   if (inverse_conditions_p (use_cond, op_cond) && if_false == op_lhs)
    2813            0 :     return if_true;
    2814              : 
    2815              :   return NULL_TREE;
    2816              : }
    2817              : 
    2818              : /* Return true if VALUE is available for use at STMT.  SSA_NAMES is
    2819              :    the set of SSA names defined earlier in STMT's block.  */
    2820              : 
    2821              : static bool
    2822          159 : value_available_p (gimple *stmt, hash_set<tree_ssa_name_hash> *ssa_names,
    2823              :                    tree value)
    2824              : {
    2825          159 :   if (is_gimple_min_invariant (value))
    2826              :     return true;
    2827              : 
    2828          115 :   if (TREE_CODE (value) == SSA_NAME)
    2829              :     {
    2830          115 :       if (SSA_NAME_IS_DEFAULT_DEF (value))
    2831              :         return true;
    2832              : 
    2833          107 :       basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (value));
    2834          107 :       basic_block use_bb = gimple_bb (stmt);
    2835          107 :       return (def_bb == use_bb
    2836          107 :               ? ssa_names->contains (value)
    2837          107 :               : dominated_by_p (CDI_DOMINATORS, use_bb, def_bb));
    2838              :     }
    2839              : 
    2840              :   return false;
    2841              : }
    2842              : 
    2843              : /* Helper function for predicate_statements.  STMT is a potentially-trapping
    2844              :    arithmetic operation that needs to be predicated by MASK, an SSA_NAME that
    2845              :    has value COND.  Return a statement that does so.  SSA_NAMES is the set of
    2846              :    SSA names defined earlier in STMT's block.  */
    2847              : 
    2848              : static gimple *
    2849          674 : predicate_rhs_code (gimple *stmt, tree mask, tree cond,
    2850              :                     hash_set<tree_ssa_name_hash> *ssa_names)
    2851              : {
    2852          674 :   internal_fn cond_fn;
    2853          674 :   if (is_gimple_assign (stmt))
    2854              :     {
    2855          566 :       tree_code code = gimple_assign_rhs_code (stmt);
    2856          566 :       cond_fn = get_conditional_internal_fn (code);
    2857              :     }
    2858          108 :   else if (tree callee = gimple_call_fndecl (stmt))
    2859              :     {
    2860          108 :       auto ifn = associated_internal_fn (callee);
    2861          108 :       cond_fn = get_conditional_internal_fn (ifn);
    2862              :     }
    2863              :   else
    2864              :     return NULL;
    2865              : 
    2866          674 :   if (cond_fn == IFN_LAST)
    2867              :     {
    2868            0 :       gcc_assert (!gimple_could_trap_p (stmt));
    2869              :       return NULL;
    2870              :     }
    2871              : 
    2872          674 :   tree lhs = gimple_get_lhs (stmt);
    2873          674 :   unsigned int nops = gimple_num_args (stmt) + 1;
    2874              : 
    2875              :   /* Construct the arguments to the conditional internal function.   */
    2876          674 :   auto_vec<tree, 8> args;
    2877          674 :   args.safe_grow (nops + 1, true);
    2878          674 :   args[0] = mask;
    2879         2130 :   for (unsigned int i = 0; i < nops - 1; ++i)
    2880         1456 :     args[i+1] = gimple_arg (stmt, i);
    2881          674 :   args[nops] = NULL_TREE;
    2882              : 
    2883              :   /* Look for uses of the result to see whether they are COND_EXPRs that can
    2884              :      be folded into the conditional call.  */
    2885          674 :   imm_use_iterator imm_iter;
    2886          674 :   gimple *use_stmt;
    2887         1553 :   FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, lhs)
    2888              :     {
    2889          879 :       tree new_else = check_redundant_cond_expr (use_stmt, mask, cond, lhs);
    2890          879 :       if (new_else && value_available_p (stmt, ssa_names, new_else))
    2891              :         {
    2892          138 :           if (!args[nops])
    2893          138 :             args[nops] = new_else;
    2894          138 :           if (operand_equal_p (new_else, args[nops], 0))
    2895              :             {
    2896              :               /* We have:
    2897              : 
    2898              :                    LHS = IFN_COND (MASK, ..., ELSE);
    2899              :                    X = MASK ? LHS : ELSE;
    2900              : 
    2901              :                  which makes X equivalent to LHS.  */
    2902          138 :               tree use_lhs = gimple_assign_lhs (use_stmt);
    2903          138 :               redundant_ssa_names.safe_push (std::make_pair (use_lhs, lhs));
    2904              :             }
    2905              :         }
    2906          674 :     }
    2907          674 :   if (!args[nops])
    2908         1072 :     args[nops] = targetm.preferred_else_value (cond_fn, TREE_TYPE (lhs),
    2909          536 :                                                nops - 1, &args[1]);
    2910              : 
    2911              :   /* Create and insert the call.  */
    2912          674 :   gcall *new_stmt = gimple_build_call_internal_vec (cond_fn, args);
    2913          674 :   gimple_call_set_lhs (new_stmt, lhs);
    2914          674 :   gimple_call_set_nothrow (new_stmt, true);
    2915              : 
    2916          674 :   return new_stmt;
    2917          674 : }
    2918              : 
    2919              : /* Predicate each write to memory in LOOP.
    2920              : 
    2921              :    This function transforms control flow constructs containing memory
    2922              :    writes of the form:
    2923              : 
    2924              :    | for (i = 0; i < N; i++)
    2925              :    |   if (cond)
    2926              :    |     A[i] = expr;
    2927              : 
    2928              :    into the following form that does not contain control flow:
    2929              : 
    2930              :    | for (i = 0; i < N; i++)
    2931              :    |   A[i] = cond ? expr : A[i];
    2932              : 
    2933              :    The original CFG looks like this:
    2934              : 
    2935              :    | bb_0
    2936              :    |   i = 0
    2937              :    | end_bb_0
    2938              :    |
    2939              :    | bb_1
    2940              :    |   if (i < N) goto bb_5 else goto bb_2
    2941              :    | end_bb_1
    2942              :    |
    2943              :    | bb_2
    2944              :    |   cond = some_computation;
    2945              :    |   if (cond) goto bb_3 else goto bb_4
    2946              :    | end_bb_2
    2947              :    |
    2948              :    | bb_3
    2949              :    |   A[i] = expr;
    2950              :    |   goto bb_4
    2951              :    | end_bb_3
    2952              :    |
    2953              :    | bb_4
    2954              :    |   goto bb_1
    2955              :    | end_bb_4
    2956              : 
    2957              :    insert_gimplified_predicates inserts the computation of the COND
    2958              :    expression at the beginning of the destination basic block:
    2959              : 
    2960              :    | bb_0
    2961              :    |   i = 0
    2962              :    | end_bb_0
    2963              :    |
    2964              :    | bb_1
    2965              :    |   if (i < N) goto bb_5 else goto bb_2
    2966              :    | end_bb_1
    2967              :    |
    2968              :    | bb_2
    2969              :    |   cond = some_computation;
    2970              :    |   if (cond) goto bb_3 else goto bb_4
    2971              :    | end_bb_2
    2972              :    |
    2973              :    | bb_3
    2974              :    |   cond = some_computation;
    2975              :    |   A[i] = expr;
    2976              :    |   goto bb_4
    2977              :    | end_bb_3
    2978              :    |
    2979              :    | bb_4
    2980              :    |   goto bb_1
    2981              :    | end_bb_4
    2982              : 
    2983              :    predicate_statements is then predicating the memory write as follows:
    2984              : 
    2985              :    | bb_0
    2986              :    |   i = 0
    2987              :    | end_bb_0
    2988              :    |
    2989              :    | bb_1
    2990              :    |   if (i < N) goto bb_5 else goto bb_2
    2991              :    | end_bb_1
    2992              :    |
    2993              :    | bb_2
    2994              :    |   if (cond) goto bb_3 else goto bb_4
    2995              :    | end_bb_2
    2996              :    |
    2997              :    | bb_3
    2998              :    |   cond = some_computation;
    2999              :    |   A[i] = cond ? expr : A[i];
    3000              :    |   goto bb_4
    3001              :    | end_bb_3
    3002              :    |
    3003              :    | bb_4
    3004              :    |   goto bb_1
    3005              :    | end_bb_4
    3006              : 
    3007              :    and finally combine_blocks removes the basic block boundaries making
    3008              :    the loop vectorizable:
    3009              : 
    3010              :    | bb_0
    3011              :    |   i = 0
    3012              :    |   if (i < N) goto bb_5 else goto bb_1
    3013              :    | end_bb_0
    3014              :    |
    3015              :    | bb_1
    3016              :    |   cond = some_computation;
    3017              :    |   A[i] = cond ? expr : A[i];
    3018              :    |   if (i < N) goto bb_5 else goto bb_4
    3019              :    | end_bb_1
    3020              :    |
    3021              :    | bb_4
    3022              :    |   goto bb_1
    3023              :    | end_bb_4
    3024              : */
    3025              : 
    3026              : static void
    3027        12129 : predicate_statements (loop_p loop)
    3028              : {
    3029        12129 :   unsigned int i, orig_loop_num_nodes = loop->num_nodes;
    3030        12129 :   auto_vec<int, 1> vect_sizes;
    3031        12129 :   auto_vec<tree, 1> vect_masks;
    3032        12129 :   hash_set<tree_ssa_name_hash> ssa_names;
    3033              : 
    3034        73706 :   for (i = 1; i < orig_loop_num_nodes; i++)
    3035              :     {
    3036        49448 :       gimple_stmt_iterator gsi;
    3037        49448 :       basic_block bb = ifc_bbs[i];
    3038        49448 :       tree cond = bb_predicate (bb);
    3039        49448 :       bool swap;
    3040        49448 :       int index;
    3041              : 
    3042        49448 :       if (is_true_predicate (cond))
    3043        24961 :         continue;
    3044              : 
    3045        24487 :       swap = false;
    3046        24487 :       if (TREE_CODE (cond) == TRUTH_NOT_EXPR)
    3047              :         {
    3048         9238 :           swap = true;
    3049         9238 :           cond = TREE_OPERAND (cond, 0);
    3050              :         }
    3051              : 
    3052        24487 :       vect_sizes.truncate (0);
    3053        24487 :       vect_masks.truncate (0);
    3054              : 
    3055       174752 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
    3056              :         {
    3057       125778 :           gimple *stmt = gsi_stmt (gsi);
    3058       125778 :           if (!is_gimple_assign (stmt)
    3059        37072 :               && !gimple_call_builtin_p (stmt)
    3060       162665 :               && !gimple_needing_rewrite_undefined (stmt))
    3061              :             ;
    3062        88891 :           else if (is_false_predicate (cond)
    3063        88891 :                    && gimple_vdef (stmt))
    3064              :             {
    3065            0 :               unlink_stmt_vdef (stmt);
    3066            0 :               gsi_remove (&gsi, true);
    3067            0 :               release_defs (stmt);
    3068            2 :               continue;
    3069              :             }
    3070              :           /* For now, just drop prefetches.  Do it now to remove any possible
    3071              :              aliasing check failures from the address calculations of the
    3072              :              prefetch.  Vect would be too late in that regard.  */
    3073        88891 :           else if (gimple_call_builtin_p (stmt, BUILT_IN_PREFETCH))
    3074              :             {
    3075            2 :               unlink_stmt_vdef (stmt);
    3076            2 :               gsi_remove (&gsi, true);
    3077            2 :               release_defs (stmt);
    3078            2 :               continue;
    3079              :             }
    3080        88889 :           else if (gimple_plf (stmt, GF_PLF_2)
    3081        88889 :                    && (is_gimple_assign (stmt)
    3082          108 :                        || (gimple_call_builtin_p (stmt)
    3083          108 :                            && !if_convertible_simdclone_stmt_p (stmt))))
    3084              :             {
    3085         2962 :               tree lhs = gimple_get_lhs (stmt);
    3086              :               /* ?? Assume that calls without an LHS are not data processing
    3087              :                  and so no issues with traps.  */
    3088         2962 :               if (!lhs)
    3089            0 :                 continue;
    3090         2962 :               tree mask;
    3091         2962 :               gimple *new_stmt;
    3092         2962 :               gimple_seq stmts = NULL;
    3093         2962 :               machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
    3094              :               /* We checked before setting GF_PLF_2 that an equivalent
    3095              :                  integer mode exists.  */
    3096         2962 :               int bitsize = GET_MODE_BITSIZE (mode).to_constant ();
    3097         2962 :               if (!vect_sizes.is_empty ()
    3098         1034 :                   && (index = mask_exists (bitsize, vect_sizes)) != -1)
    3099              :                 /* Use created mask.  */
    3100          973 :                 mask = vect_masks[index];
    3101              :               else
    3102              :                 {
    3103         1989 :                   if (COMPARISON_CLASS_P (cond))
    3104            0 :                     mask = gimple_build (&stmts, TREE_CODE (cond),
    3105              :                                          boolean_type_node,
    3106            0 :                                          TREE_OPERAND (cond, 0),
    3107            0 :                                          TREE_OPERAND (cond, 1));
    3108              :                   else
    3109              :                     mask = cond;
    3110              : 
    3111         1989 :                   if (swap)
    3112              :                     {
    3113          391 :                       tree true_val
    3114          391 :                         = constant_boolean_node (true, TREE_TYPE (mask));
    3115          391 :                       mask = gimple_build (&stmts, BIT_XOR_EXPR,
    3116          391 :                                            TREE_TYPE (mask), mask, true_val);
    3117              :                     }
    3118         1989 :                   gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    3119              : 
    3120              :                   /* Save mask and its size for further use.  */
    3121         1989 :                   vect_sizes.safe_push (bitsize);
    3122         1989 :                   vect_masks.safe_push (mask);
    3123              :                 }
    3124         2962 :               if (gimple_assign_single_p (stmt))
    3125         2288 :                 new_stmt = predicate_load_or_store (&gsi,
    3126              :                                                     as_a <gassign *> (stmt),
    3127              :                                                     mask);
    3128              :               else
    3129          674 :                 new_stmt = predicate_rhs_code (stmt, mask, cond, &ssa_names);
    3130              : 
    3131         2962 :               if (new_stmt)
    3132         2962 :                 gsi_replace (&gsi, new_stmt, true);
    3133              :             }
    3134        85927 :           else if (gimple_needing_rewrite_undefined (stmt))
    3135        18640 :             rewrite_to_defined_unconditional (&gsi);
    3136       134574 :           else if (gimple_vdef (stmt))
    3137              :             {
    3138         1395 :               tree lhs = gimple_assign_lhs (stmt);
    3139         1395 :               tree rhs = gimple_assign_rhs1 (stmt);
    3140         1395 :               tree type = TREE_TYPE (lhs);
    3141              : 
    3142         1395 :               lhs = ifc_temp_var (type, unshare_expr (lhs), &gsi);
    3143         1395 :               rhs = ifc_temp_var (type, unshare_expr (rhs), &gsi);
    3144         1395 :               if (swap)
    3145          479 :                 std::swap (lhs, rhs);
    3146         1395 :               cond = force_gimple_operand_gsi (&gsi, unshare_expr (cond), true,
    3147              :                                                NULL_TREE, true, GSI_SAME_STMT);
    3148         1395 :               rhs = fold_build_cond_expr (type, unshare_expr (cond), rhs, lhs);
    3149         1395 :               gimple_assign_set_rhs1 (stmt, ifc_temp_var (type, rhs, &gsi));
    3150         1395 :               update_stmt (stmt);
    3151              :             }
    3152              : 
    3153       125776 :           if (gimple_plf (gsi_stmt (gsi), GF_PLF_2)
    3154       125776 :               && is_gimple_call (gsi_stmt (gsi)))
    3155              :             {
    3156              :               /* Convert functions that have a SIMD clone to IFN_MASK_CALL.
    3157              :                  This will cause the vectorizer to match the "in branch"
    3158              :                  clone variants, and serves to build the mask vector
    3159              :                  in a natural way.  */
    3160         1007 :               tree mask = cond;
    3161         1007 :               gcall *call = dyn_cast <gcall *> (gsi_stmt (gsi));
    3162         1007 :               tree orig_fn = gimple_call_fn (call);
    3163         1007 :               int orig_nargs = gimple_call_num_args (call);
    3164         1007 :               auto_vec<tree> args;
    3165         1007 :               args.safe_push (orig_fn);
    3166         3036 :               for (int i = 0; i < orig_nargs; i++)
    3167         1022 :                 args.safe_push (gimple_call_arg (call, i));
    3168              :               /* If `swap', we invert the mask used for the if branch for use
    3169              :                  when masking the function call.  */
    3170         1007 :               if (swap)
    3171              :                 {
    3172          948 :                   gimple_seq stmts = NULL;
    3173          948 :                   tree true_val
    3174          948 :                     = constant_boolean_node (true, TREE_TYPE (mask));
    3175          948 :                   mask = gimple_build (&stmts, BIT_XOR_EXPR,
    3176          948 :                                        TREE_TYPE (mask), mask, true_val);
    3177          948 :                   gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
    3178              :                 }
    3179         1007 :               args.safe_push (mask);
    3180              : 
    3181              :               /* Replace the call with a IFN_MASK_CALL that has the extra
    3182              :                  condition parameter. */
    3183         1007 :               gcall *new_call = gimple_build_call_internal_vec (IFN_MASK_CALL,
    3184              :                                                                 args);
    3185         1007 :               gimple_call_set_lhs (new_call, gimple_call_lhs (call));
    3186         1007 :               gsi_replace (&gsi, new_call, true);
    3187         1007 :             }
    3188              : 
    3189       125776 :           tree lhs = gimple_get_lhs (gsi_stmt (gsi));
    3190       125776 :           if (lhs && TREE_CODE (lhs) == SSA_NAME)
    3191        86644 :             ssa_names.add (lhs);
    3192       125776 :           gsi_next (&gsi);
    3193              :         }
    3194        48952 :       ssa_names.empty ();
    3195              :     }
    3196        12129 : }
    3197              : 
    3198              : /* Remove all GIMPLE_CONDs and GIMPLE_LABELs and GIMPLE_SWITCH of all
    3199              :    the basic blocks other than the exit and latch of the LOOP.  Also
    3200              :    resets the GIMPLE_DEBUG information.  */
    3201              : 
    3202              : static void
    3203        25750 : remove_conditions_and_labels (loop_p loop)
    3204              : {
    3205        25750 :   gimple_stmt_iterator gsi;
    3206        25750 :   unsigned int i;
    3207              : 
    3208       159151 :   for (i = 0; i < loop->num_nodes; i++)
    3209              :     {
    3210       133401 :       basic_block bb = ifc_bbs[i];
    3211              : 
    3212       133401 :       if (bb_with_exit_edge_p (loop, bb)
    3213       133401 :           || bb == loop->latch)
    3214        51500 :         continue;
    3215              : 
    3216       656514 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
    3217       492712 :         switch (gimple_code (gsi_stmt (gsi)))
    3218              :           {
    3219        33296 :           case GIMPLE_COND:
    3220        33296 :           case GIMPLE_LABEL:
    3221        33296 :           case GIMPLE_SWITCH:
    3222        33296 :             gsi_remove (&gsi, true);
    3223        33296 :             break;
    3224              : 
    3225       293305 :           case GIMPLE_DEBUG:
    3226              :             /* ??? Should there be conditional GIMPLE_DEBUG_BINDs?  */
    3227       293305 :             if (gimple_debug_bind_p (gsi_stmt (gsi)))
    3228              :               {
    3229       241805 :                 gimple_debug_bind_reset_value (gsi_stmt (gsi));
    3230       241805 :                 update_stmt (gsi_stmt (gsi));
    3231              :               }
    3232       293305 :             gsi_next (&gsi);
    3233       293305 :             break;
    3234              : 
    3235       166111 :           default:
    3236       166111 :             gsi_next (&gsi);
    3237              :           }
    3238              :     }
    3239        25750 : }
    3240              : 
    3241              : /* Combine all the basic blocks from LOOP into one or two super basic
    3242              :    blocks.  Replace PHI nodes with conditional modify expressions.  */
    3243              : 
    3244              : static void
    3245        25750 : combine_blocks (class loop *loop)
    3246              : {
    3247        25750 :   basic_block bb, exit_bb, merge_target_bb;
    3248        25750 :   unsigned int orig_loop_num_nodes = loop->num_nodes;
    3249        25750 :   unsigned int i;
    3250        25750 :   edge e;
    3251        25750 :   edge_iterator ei;
    3252              : 
    3253              :   /* Reset flow-sensitive info before predicating stmts or PHIs we
    3254              :      might fold.  */
    3255       159151 :   for (i = 0; i < orig_loop_num_nodes; i++)
    3256              :     {
    3257       133401 :       bb = ifc_bbs[i];
    3258       133401 :       if (is_predicated (bb))
    3259              :         {
    3260        52644 :           for (auto gsi = gsi_start_phis (bb);
    3261        53826 :                !gsi_end_p (gsi); gsi_next (&gsi))
    3262         1182 :             reset_flow_sensitive_info (gimple_phi_result (*gsi));
    3263       203619 :           for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    3264              :             {
    3265        98331 :               gimple *stmt = gsi_stmt (gsi);
    3266        98331 :               ssa_op_iter i;
    3267        98331 :               tree op;
    3268       148827 :               FOR_EACH_SSA_TREE_OPERAND (op, stmt, i, SSA_OP_DEF)
    3269        50496 :                 reset_flow_sensitive_info (op);
    3270              :             }
    3271              :         }
    3272              :     }
    3273              : 
    3274        25750 :   remove_conditions_and_labels (loop);
    3275        25750 :   insert_gimplified_predicates (loop);
    3276        25750 :   predicate_all_scalar_phis (loop);
    3277              : 
    3278        25750 :   if (need_to_predicate || need_to_rewrite_undefined)
    3279        12129 :     predicate_statements (loop);
    3280              : 
    3281              :   /* Merge basic blocks.  */
    3282        25750 :   exit_bb = single_exit (loop)->src;
    3283        25750 :   gcc_assert (exit_bb != loop->latch);
    3284       159151 :   for (i = 0; i < orig_loop_num_nodes; i++)
    3285              :     {
    3286       133401 :       bb = ifc_bbs[i];
    3287       133401 :       free_bb_predicate (bb);
    3288              :     }
    3289              : 
    3290        25750 :   merge_target_bb = loop->header;
    3291              : 
    3292              :   /* Get at the virtual def valid for uses starting at the first block
    3293              :      we merge into the header.  Without a virtual PHI the loop has the
    3294              :      same virtual use on all stmts.  */
    3295        25750 :   gphi *vphi = get_virtual_phi (loop->header);
    3296        25750 :   tree last_vdef = NULL_TREE;
    3297        25750 :   if (vphi)
    3298              :     {
    3299        13696 :       last_vdef = gimple_phi_result (vphi);
    3300        27392 :       for (gimple_stmt_iterator gsi = gsi_start_bb (loop->header);
    3301       217688 :            ! gsi_end_p (gsi); gsi_next (&gsi))
    3302       268436 :         if (gimple_vdef (gsi_stmt (gsi)))
    3303       203992 :           last_vdef = gimple_vdef (gsi_stmt (gsi));
    3304              :     }
    3305       133401 :   for (i = 1; i < orig_loop_num_nodes; i++)
    3306              :     {
    3307       107651 :       gimple_stmt_iterator gsi;
    3308       107651 :       gimple_stmt_iterator last;
    3309              : 
    3310       107651 :       bb = ifc_bbs[i];
    3311              : 
    3312       107651 :       if (bb == exit_bb || bb == loop->latch)
    3313        51500 :         continue;
    3314              : 
    3315              :       /* We release virtual PHIs late because we have to propagate them
    3316              :          out using the current VUSE.  The def might be the one used
    3317              :          after the loop.  */
    3318        56151 :       vphi = get_virtual_phi (bb);
    3319        56151 :       if (vphi)
    3320              :         {
    3321              :           /* When there's just loads inside the loop a stray virtual
    3322              :              PHI merging the uses can appear, update last_vdef from
    3323              :              it.  */
    3324          716 :           if (!last_vdef)
    3325            0 :             last_vdef = gimple_phi_arg_def (vphi, 0);
    3326          716 :           imm_use_iterator iter;
    3327          716 :           use_operand_p use_p;
    3328          716 :           gimple *use_stmt;
    3329         2529 :           FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_phi_result (vphi))
    3330              :             {
    3331         3628 :               FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
    3332         1814 :                 SET_USE (use_p, last_vdef);
    3333          716 :             }
    3334          716 :           if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (vphi)))
    3335            0 :             SSA_NAME_OCCURS_IN_ABNORMAL_PHI (last_vdef) = 1;
    3336          716 :           gsi = gsi_for_stmt (vphi);
    3337          716 :           remove_phi_node (&gsi, true);
    3338              :         }
    3339              : 
    3340              :       /* Make stmts member of loop->header and clear range info from all stmts
    3341              :          in BB which is now no longer executed conditional on a predicate we
    3342              :          could have derived it from.  */
    3343       341244 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    3344              :         {
    3345       228942 :           gimple *stmt = gsi_stmt (gsi);
    3346       228942 :           gimple_set_bb (stmt, merge_target_bb);
    3347              :           /* Update virtual operands.  */
    3348       228942 :           if (last_vdef)
    3349              :             {
    3350       167195 :               use_operand_p use_p = ssa_vuse_operand (stmt);
    3351        14339 :               if (use_p
    3352        14339 :                   && USE_FROM_PTR (use_p) != last_vdef)
    3353          518 :                 SET_USE (use_p, last_vdef);
    3354       537213 :               if (gimple_vdef (stmt))
    3355              :                 last_vdef = gimple_vdef (stmt);
    3356              :             }
    3357              :           else
    3358              :             /* If this is the first load we arrive at update last_vdef
    3359              :                so we handle stray PHIs correctly.  */
    3360       271189 :             last_vdef = gimple_vuse (stmt);
    3361              :         }
    3362              : 
    3363              :       /* Update stmt list.  */
    3364        56151 :       last = gsi_last_bb (merge_target_bb);
    3365       112302 :       gsi_insert_seq_after_without_update (&last, bb_seq (bb), GSI_NEW_STMT);
    3366        56151 :       set_bb_seq (bb, NULL);
    3367              :     }
    3368              : 
    3369              :   /* Fixup virtual operands in the exit block.  */
    3370        25750 :   if (exit_bb
    3371        25750 :       && exit_bb != loop->header)
    3372              :     {
    3373              :       /* We release virtual PHIs late because we have to propagate them
    3374              :          out using the current VUSE.  The def might be the one used
    3375              :          after the loop.  */
    3376        25750 :       vphi = get_virtual_phi (exit_bb);
    3377        25750 :       if (vphi)
    3378              :         {
    3379              :           /* When there's just loads inside the loop a stray virtual
    3380              :              PHI merging the uses can appear, update last_vdef from
    3381              :              it.  */
    3382         1707 :           if (!last_vdef)
    3383            0 :             last_vdef = gimple_phi_arg_def (vphi, 0);
    3384         1707 :           imm_use_iterator iter;
    3385         1707 :           use_operand_p use_p;
    3386         1707 :           gimple *use_stmt;
    3387         5115 :           FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_phi_result (vphi))
    3388              :             {
    3389         6816 :               FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
    3390         3408 :                 SET_USE (use_p, last_vdef);
    3391         1707 :             }
    3392         1707 :           if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (vphi)))
    3393            0 :             SSA_NAME_OCCURS_IN_ABNORMAL_PHI (last_vdef) = 1;
    3394         1707 :           gimple_stmt_iterator gsi = gsi_for_stmt (vphi);
    3395         1707 :           remove_phi_node (&gsi, true);
    3396              :         }
    3397              :     }
    3398              : 
    3399              :   /* Now remove all the edges in the loop, except for those from the exit
    3400              :      block and delete the blocks we elided.  */
    3401       133401 :   for (i = 1; i < orig_loop_num_nodes; i++)
    3402              :     {
    3403       107651 :       bb = ifc_bbs[i];
    3404              : 
    3405       248216 :       for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei));)
    3406              :         {
    3407       140565 :           if (e->src == exit_bb)
    3408        25750 :             ei_next (&ei);
    3409              :           else
    3410       114815 :             remove_edge (e);
    3411              :         }
    3412              :     }
    3413       133401 :   for (i = 1; i < orig_loop_num_nodes; i++)
    3414              :     {
    3415       107651 :       bb = ifc_bbs[i];
    3416              : 
    3417       107651 :       if (bb == exit_bb || bb == loop->latch)
    3418        51500 :         continue;
    3419              : 
    3420        56151 :       delete_basic_block (bb);
    3421              :     }
    3422              : 
    3423              :   /* Re-connect the exit block.  */
    3424        25750 :   if (exit_bb != NULL)
    3425              :     {
    3426        25750 :       if (exit_bb != loop->header)
    3427              :         {
    3428              :           /* Connect this node to loop header.  */
    3429        25750 :           make_single_succ_edge (loop->header, exit_bb, EDGE_FALLTHRU);
    3430        25750 :           set_immediate_dominator (CDI_DOMINATORS, exit_bb, loop->header);
    3431              :         }
    3432              : 
    3433              :       /* Redirect non-exit edges to loop->latch.  */
    3434        77250 :       FOR_EACH_EDGE (e, ei, exit_bb->succs)
    3435              :         {
    3436        51500 :           if (!loop_exit_edge_p (loop, e))
    3437        25750 :             redirect_edge_and_branch (e, loop->latch);
    3438              :         }
    3439        25750 :       set_immediate_dominator (CDI_DOMINATORS, loop->latch, exit_bb);
    3440              :     }
    3441              :   else
    3442              :     {
    3443              :       /* If the loop does not have an exit, reconnect header and latch.  */
    3444            0 :       make_edge (loop->header, loop->latch, EDGE_FALLTHRU);
    3445            0 :       set_immediate_dominator (CDI_DOMINATORS, loop->latch, loop->header);
    3446              :     }
    3447              : 
    3448              :   /* If possible, merge loop header to the block with the exit edge.
    3449              :      This reduces the number of basic blocks to two, to please the
    3450              :      vectorizer that handles only loops with two nodes.  */
    3451        25750 :   if (exit_bb
    3452        25750 :       && exit_bb != loop->header)
    3453              :     {
    3454        25750 :       if (can_merge_blocks_p (loop->header, exit_bb))
    3455        25748 :         merge_blocks (loop->header, exit_bb);
    3456              :     }
    3457              : 
    3458        25750 :   free (ifc_bbs);
    3459        25750 :   ifc_bbs = NULL;
    3460        25750 : }
    3461              : 
    3462              : /* Version LOOP before if-converting it; the original loop
    3463              :    will be if-converted, the new copy of the loop will not,
    3464              :    and the LOOP_VECTORIZED internal call will be guarding which
    3465              :    loop to execute.  The vectorizer pass will fold this
    3466              :    internal call into either true or false.
    3467              : 
    3468              :    Note that this function intentionally invalidates profile.  Both edges
    3469              :    out of LOOP_VECTORIZED must have 100% probability so the profile remains
    3470              :    consistent after the condition is folded in the vectorizer.  */
    3471              : 
    3472              : static class loop *
    3473        26019 : version_loop_for_if_conversion (class loop *loop, vec<gimple *> *preds)
    3474              : {
    3475        26019 :   basic_block cond_bb;
    3476        26019 :   tree cond = make_ssa_name (boolean_type_node);
    3477        26019 :   class loop *new_loop;
    3478        26019 :   gimple *g;
    3479        26019 :   gimple_stmt_iterator gsi;
    3480        26019 :   unsigned int save_length = 0;
    3481              : 
    3482        26019 :   g = gimple_build_call_internal (IFN_LOOP_VECTORIZED, 2,
    3483        26019 :                                   build_int_cst (integer_type_node, loop->num),
    3484              :                                   integer_zero_node);
    3485        26019 :   gimple_call_set_lhs (g, cond);
    3486              : 
    3487        26019 :   void **saved_preds = NULL;
    3488        26019 :   if (any_complicated_phi || need_to_predicate)
    3489              :     {
    3490              :       /* Save BB->aux around loop_version as that uses the same field.  */
    3491         3577 :       save_length = loop->inner ? loop->inner->num_nodes : loop->num_nodes;
    3492         3577 :       saved_preds = XALLOCAVEC (void *, save_length);
    3493        27305 :       for (unsigned i = 0; i < save_length; i++)
    3494        23728 :         saved_preds[i] = ifc_bbs[i]->aux;
    3495              :     }
    3496              : 
    3497        26019 :   initialize_original_copy_tables ();
    3498              :   /* At this point we invalidate profile consistency until IFN_LOOP_VECTORIZED
    3499              :      is re-merged in the vectorizer.  */
    3500        26019 :   new_loop = loop_version (loop, cond, &cond_bb,
    3501              :                            profile_probability::always (),
    3502              :                            profile_probability::always (),
    3503              :                            profile_probability::always (),
    3504              :                            profile_probability::always (), true);
    3505        26019 :   free_original_copy_tables ();
    3506              : 
    3507        26019 :   if (any_complicated_phi || need_to_predicate)
    3508        27305 :     for (unsigned i = 0; i < save_length; i++)
    3509        23728 :       ifc_bbs[i]->aux = saved_preds[i];
    3510              : 
    3511        26019 :   if (new_loop == NULL)
    3512              :     return NULL;
    3513              : 
    3514        26019 :   new_loop->dont_vectorize = true;
    3515        26019 :   new_loop->force_vectorize = false;
    3516        26019 :   gsi = gsi_last_bb (cond_bb);
    3517        26019 :   gimple_call_set_arg (g, 1, build_int_cst (integer_type_node, new_loop->num));
    3518        26019 :   if (preds)
    3519        26019 :     preds->safe_push (g);
    3520        26019 :   gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    3521        26019 :   update_ssa (TODO_update_ssa_no_phi);
    3522        26019 :   return new_loop;
    3523              : }
    3524              : 
    3525              : /* Return true when LOOP satisfies the follow conditions that will
    3526              :    allow it to be recognized by the vectorizer for outer-loop
    3527              :    vectorization:
    3528              :     - The loop is not the root node of the loop tree.
    3529              :     - The loop has exactly one inner loop.
    3530              :     - The loop has a single exit.
    3531              :     - The loop header has a single successor, which is the inner
    3532              :       loop header.
    3533              :     - Each of the inner and outer loop latches have a single
    3534              :       predecessor.
    3535              :     - The loop exit block has a single predecessor, which is the
    3536              :       inner loop's exit block.  */
    3537              : 
    3538              : static bool
    3539        26019 : versionable_outer_loop_p (class loop *loop)
    3540              : {
    3541        26019 :   if (!loop_outer (loop)
    3542         8176 :       || loop->dont_vectorize
    3543         7190 :       || !loop->inner
    3544         7190 :       || loop->inner->next
    3545         3024 :       || !single_exit (loop)
    3546         2346 :       || !single_succ_p (loop->header)
    3547          936 :       || single_succ (loop->header) != loop->inner->header
    3548          936 :       || !single_pred_p (loop->latch)
    3549        26955 :       || !single_pred_p (loop->inner->latch))
    3550              :     return false;
    3551              : 
    3552          936 :   basic_block outer_exit = single_pred (loop->latch);
    3553          936 :   basic_block inner_exit = single_pred (loop->inner->latch);
    3554              : 
    3555         1863 :   if (!single_pred_p (outer_exit) || single_pred (outer_exit) != inner_exit)
    3556              :     return false;
    3557              : 
    3558          926 :   if (dump_file)
    3559            0 :     fprintf (dump_file, "Found vectorizable outer loop for versioning\n");
    3560              : 
    3561              :   return true;
    3562              : }
    3563              : 
    3564              : /* Performs splitting of critical edges.  Skip splitting and return false
    3565              :    if LOOP will not be converted because:
    3566              : 
    3567              :      - LOOP is not well formed.
    3568              :      - LOOP has PHI with more than MAX_PHI_ARG_NUM arguments.
    3569              : 
    3570              :    Last restriction is valid only if AGGRESSIVE_IF_CONV is false.  */
    3571              : 
    3572              : static bool
    3573       325154 : ifcvt_split_critical_edges (class loop *loop, bool aggressive_if_conv)
    3574              : {
    3575       325154 :   basic_block *body;
    3576       325154 :   basic_block bb;
    3577       325154 :   unsigned int num = loop->num_nodes;
    3578       325154 :   unsigned int i;
    3579       325154 :   edge e;
    3580       325154 :   edge_iterator ei;
    3581       325154 :   auto_vec<edge> critical_edges;
    3582              : 
    3583              :   /* Loop is not well formed.  */
    3584       325154 :   if (loop->inner)
    3585              :     return false;
    3586              : 
    3587       231359 :   body = get_loop_body (loop);
    3588      1840996 :   for (i = 0; i < num; i++)
    3589              :     {
    3590      1384852 :       bb = body[i];
    3591      1384852 :       if (!aggressive_if_conv
    3592      1376039 :           && phi_nodes (bb)
    3593      1816293 :           && EDGE_COUNT (bb->preds) > MAX_PHI_ARG_NUM)
    3594              :         {
    3595         6574 :           if (dump_file && (dump_flags & TDF_DETAILS))
    3596            0 :             fprintf (dump_file,
    3597              :                      "BB %d has complicated PHI with more than %u args.\n",
    3598              :                      bb->index, MAX_PHI_ARG_NUM);
    3599              : 
    3600         6574 :           free (body);
    3601         6574 :           return false;
    3602              :         }
    3603      1378278 :       if (bb == loop->latch || bb_with_exit_edge_p (loop, bb))
    3604       788257 :         continue;
    3605              : 
    3606              :       /* Skip basic blocks not ending with conditional branch.  */
    3607      1413975 :       if (!safe_is_a <gcond *> (*gsi_last_bb (bb))
    3608       590021 :           && !safe_is_a <gswitch *> (*gsi_last_bb (bb)))
    3609       326619 :         continue;
    3610              : 
    3611       802274 :       FOR_EACH_EDGE (e, ei, bb->succs)
    3612       665016 :         if (EDGE_CRITICAL_P (e) && e->dest->loop_father == loop)
    3613       126144 :           critical_edges.safe_push (e);
    3614              :     }
    3615       224785 :   free (body);
    3616              : 
    3617       447184 :   while (critical_edges.length () > 0)
    3618              :     {
    3619       122030 :       e = critical_edges.pop ();
    3620              :       /* Don't split if bb can be predicated along non-critical edge.  */
    3621       122030 :       if (EDGE_COUNT (e->dest->preds) > 2 || all_preds_critical_p (e->dest))
    3622        57942 :         split_edge (e);
    3623              :     }
    3624              : 
    3625              :   return true;
    3626       325154 : }
    3627              : 
    3628              : /* Delete redundant statements produced by predication which prevents
    3629              :    loop vectorization.  */
    3630              : 
    3631              : static void
    3632        26019 : ifcvt_local_dce (class loop *loop)
    3633              : {
    3634        26019 :   gimple *stmt;
    3635        26019 :   gimple *stmt1;
    3636        26019 :   gimple *phi;
    3637        26019 :   gimple_stmt_iterator gsi;
    3638        26019 :   auto_vec<gimple *> worklist;
    3639        26019 :   enum gimple_code code;
    3640        26019 :   use_operand_p use_p;
    3641        26019 :   imm_use_iterator imm_iter;
    3642              : 
    3643              :   /* The loop has a single BB only.  */
    3644        26019 :   basic_block bb = loop->header;
    3645        26019 :   tree latch_vdef = NULL_TREE;
    3646              : 
    3647        26019 :   worklist.create (64);
    3648              :   /* Consider all phi as live statements.  */
    3649       104067 :   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    3650              :     {
    3651        78048 :       phi = gsi_stmt (gsi);
    3652        78048 :       gimple_set_plf (phi, GF_PLF_2, true);
    3653        78048 :       worklist.safe_push (phi);
    3654       169994 :       if (virtual_operand_p (gimple_phi_result (phi)))
    3655        13898 :         latch_vdef = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
    3656              :     }
    3657              :   /* Consider load/store statements, CALL and COND as live.  */
    3658       814807 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    3659              :     {
    3660       762769 :       stmt = gsi_stmt (gsi);
    3661       762769 :       if (is_gimple_debug (stmt))
    3662              :         {
    3663       383575 :           gimple_set_plf (stmt, GF_PLF_2, true);
    3664       383575 :           continue;
    3665              :         }
    3666       379194 :       if (gimple_store_p (stmt) || gimple_assign_load_p (stmt))
    3667              :         {
    3668        59153 :           gimple_set_plf (stmt, GF_PLF_2, true);
    3669        59153 :           worklist.safe_push (stmt);
    3670        59153 :           continue;
    3671              :         }
    3672       320041 :       code = gimple_code (stmt);
    3673       320041 :       if (code == GIMPLE_COND || code == GIMPLE_CALL || code == GIMPLE_SWITCH)
    3674              :         {
    3675        31970 :           gimple_set_plf (stmt, GF_PLF_2, true);
    3676        31970 :           worklist.safe_push (stmt);
    3677        31970 :           continue;
    3678              :         }
    3679       288071 :       gimple_set_plf (stmt, GF_PLF_2, false);
    3680              : 
    3681       288071 :       if (code == GIMPLE_ASSIGN)
    3682              :         {
    3683       288062 :           tree lhs = gimple_assign_lhs (stmt);
    3684       667686 :           FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
    3685              :             {
    3686       399233 :               stmt1 = USE_STMT (use_p);
    3687       399233 :               if (!is_gimple_debug (stmt1) && gimple_bb (stmt1) != bb)
    3688              :                 {
    3689        19609 :                   gimple_set_plf (stmt, GF_PLF_2, true);
    3690        19609 :                   worklist.safe_push (stmt);
    3691        19609 :                   break;
    3692              :                 }
    3693       288062 :             }
    3694              :         }
    3695              :     }
    3696              :   /* Propagate liveness through arguments of live stmt.  */
    3697       470570 :   while (worklist.length () > 0)
    3698              :     {
    3699       444551 :       ssa_op_iter iter;
    3700       444551 :       use_operand_p use_p;
    3701       444551 :       tree use;
    3702              : 
    3703       444551 :       stmt = worklist.pop ();
    3704      1563505 :       FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
    3705              :         {
    3706       674403 :           use = USE_FROM_PTR (use_p);
    3707       674403 :           if (TREE_CODE (use) != SSA_NAME)
    3708        43590 :             continue;
    3709       630813 :           stmt1 = SSA_NAME_DEF_STMT (use);
    3710       630813 :           if (gimple_bb (stmt1) != bb || gimple_plf (stmt1, GF_PLF_2))
    3711       375042 :             continue;
    3712       255771 :           gimple_set_plf (stmt1, GF_PLF_2, true);
    3713       255771 :           worklist.safe_push (stmt1);
    3714              :         }
    3715              :     }
    3716              :   /* Delete dead statements.  */
    3717        26019 :   gsi = gsi_last_bb (bb);
    3718       788788 :   while (!gsi_end_p (gsi))
    3719              :     {
    3720       762769 :       gimple_stmt_iterator gsiprev = gsi;
    3721       762769 :       gsi_prev (&gsiprev);
    3722       762769 :       stmt = gsi_stmt (gsi);
    3723       762769 :       if (!gimple_has_volatile_ops (stmt)
    3724       762624 :           && gimple_store_p (stmt)
    3725       379122 :           && gimple_vdef (stmt))
    3726              :         {
    3727        12976 :           tree lhs = gimple_get_lhs (stmt);
    3728        12976 :           ao_ref write;
    3729        12976 :           ao_ref_init (&write, lhs);
    3730              : 
    3731        12976 :           if (dse_classify_store (&write, stmt, false, NULL, NULL, latch_vdef)
    3732              :               == DSE_STORE_DEAD)
    3733          267 :             delete_dead_or_redundant_assignment (&gsi, "dead");
    3734        12976 :           gsi = gsiprev;
    3735        12976 :           continue;
    3736        12976 :         }
    3737              : 
    3738       749793 :       if (gimple_plf (stmt, GF_PLF_2))
    3739              :         {
    3740       737102 :           gsi = gsiprev;
    3741       737102 :           continue;
    3742              :         }
    3743        12691 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3744              :         {
    3745           28 :           fprintf (dump_file, "Delete dead stmt in bb#%d\n", bb->index);
    3746           28 :           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    3747              :         }
    3748        12691 :       gsi_remove (&gsi, true);
    3749        12691 :       release_defs (stmt);
    3750        12691 :       gsi = gsiprev;
    3751              :     }
    3752        26019 : }
    3753              : 
    3754              : /* Return true if VALUE is already available on edge PE.  */
    3755              : 
    3756              : static bool
    3757       247867 : ifcvt_available_on_edge_p (edge pe, tree value)
    3758              : {
    3759       247867 :   if (is_gimple_min_invariant (value))
    3760              :     return true;
    3761              : 
    3762       241767 :   if (TREE_CODE (value) == SSA_NAME)
    3763              :     {
    3764       240429 :       basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (value));
    3765       240429 :       if (!def_bb || dominated_by_p (CDI_DOMINATORS, pe->dest, def_bb))
    3766        26684 :         return true;
    3767              :     }
    3768              : 
    3769              :   return false;
    3770              : }
    3771              : 
    3772              : /* Return true if STMT can be hoisted from if-converted loop LOOP to
    3773              :    edge PE.  */
    3774              : 
    3775              : static bool
    3776       749811 : ifcvt_can_hoist (class loop *loop, edge pe, gimple *stmt)
    3777              : {
    3778       749811 :   if (auto *call = dyn_cast<gcall *> (stmt))
    3779              :     {
    3780         5957 :       if (gimple_call_internal_p (call)
    3781         5957 :           && internal_fn_mask_index (gimple_call_internal_fn (call)) >= 0)
    3782              :         return false;
    3783              :     }
    3784       743854 :   else if (auto *assign = dyn_cast<gassign *> (stmt))
    3785              :     {
    3786       394698 :       if (gimple_assign_rhs_code (assign) == COND_EXPR)
    3787              :         return false;
    3788              :     }
    3789              :   else
    3790              :     return false;
    3791              : 
    3792       291677 :   if (gimple_has_side_effects (stmt)
    3793       290401 :       || gimple_could_trap_p (stmt)
    3794       228714 :       || stmt_could_throw_p (cfun, stmt)
    3795       228712 :       || gimple_vdef (stmt)
    3796       519782 :       || gimple_vuse (stmt))
    3797              :     return false;
    3798              : 
    3799       227035 :   int num_args = gimple_num_args (stmt);
    3800       227035 :   if (pe != loop_preheader_edge (loop))
    3801              :     {
    3802       252894 :       for (int i = 0; i < num_args; ++i)
    3803       247867 :         if (!ifcvt_available_on_edge_p (pe, gimple_arg (stmt, i)))
    3804              :           return false;
    3805              :     }
    3806              :   else
    3807              :     {
    3808         8517 :       for (int i = 0; i < num_args; ++i)
    3809         8322 :         if (!expr_invariant_in_loop_p (loop, gimple_arg (stmt, i)))
    3810              :           return false;
    3811              :     }
    3812              : 
    3813              :   return true;
    3814              : }
    3815              : 
    3816              : /* Hoist invariant statements from LOOP to edge PE.  */
    3817              : 
    3818              : static void
    3819        26019 : ifcvt_hoist_invariants (class loop *loop, edge pe)
    3820              : {
    3821              :   /* Only hoist from the now unconditionally executed part of the loop.  */
    3822        26019 :   basic_block bb = loop->header;
    3823        26019 :   gimple_stmt_iterator hoist_gsi = {};
    3824       801849 :   for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
    3825              :     {
    3826       749811 :       gimple *stmt = gsi_stmt (gsi);
    3827       749811 :       if (ifcvt_can_hoist (loop, pe, stmt))
    3828              :         {
    3829              :           /* Once we've hoisted one statement, insert other statements
    3830              :              after it.  */
    3831         5222 :           gsi_remove (&gsi, false);
    3832         5222 :           if (hoist_gsi.ptr)
    3833         2762 :             gsi_insert_after (&hoist_gsi, stmt, GSI_NEW_STMT);
    3834              :           else
    3835              :             {
    3836         2460 :               gsi_insert_on_edge_immediate (pe, stmt);
    3837         2460 :               hoist_gsi = gsi_for_stmt (stmt);
    3838              :             }
    3839         5222 :           continue;
    3840              :         }
    3841       744589 :       gsi_next (&gsi);
    3842              :     }
    3843        26019 : }
    3844              : 
    3845              : /* Returns the DECL_FIELD_BIT_OFFSET of the bitfield accesse in stmt iff its
    3846              :    type mode is not BLKmode.  If BITPOS is not NULL it will hold the poly_int64
    3847              :    value of the DECL_FIELD_BIT_OFFSET of the bitfield access and STRUCT_EXPR,
    3848              :    if not NULL, will hold the tree representing the base struct of this
    3849              :    bitfield.  */
    3850              : 
    3851              : static tree
    3852         1265 : get_bitfield_rep (gassign *stmt, bool write, tree *bitpos,
    3853              :                   tree *struct_expr)
    3854              : {
    3855         1265 :   tree comp_ref = write ? gimple_assign_lhs (stmt)
    3856          384 :                         : gimple_assign_rhs1 (stmt);
    3857              : 
    3858         1265 :   tree field_decl = TREE_OPERAND (comp_ref, 1);
    3859         1265 :   tree ref_offset = component_ref_field_offset (comp_ref);
    3860         1265 :   tree rep_decl = DECL_BIT_FIELD_REPRESENTATIVE (field_decl);
    3861              : 
    3862              :   /* Bail out if the representative is not a suitable type for a scalar
    3863              :      register variable.  */
    3864         1265 :   if (!is_gimple_reg_type (TREE_TYPE (rep_decl)))
    3865              :     return NULL_TREE;
    3866              : 
    3867              :   /* Bail out if the DECL_SIZE of the field_decl isn't the same as the BF's
    3868              :      precision.  */
    3869         1250 :   unsigned HOST_WIDE_INT bf_prec
    3870         1250 :     = TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (stmt)));
    3871         1250 :   if (compare_tree_int (DECL_SIZE (field_decl), bf_prec) != 0)
    3872              :     return NULL_TREE;
    3873              : 
    3874         1250 :   if (TREE_CODE (DECL_FIELD_OFFSET (rep_decl)) != INTEGER_CST
    3875         1250 :       || TREE_CODE (ref_offset) != INTEGER_CST)
    3876              :     {
    3877            2 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3878            2 :         fprintf (dump_file, "\t Bitfield NOT OK to lower,"
    3879              :                             " offset is non-constant.\n");
    3880              :       return NULL_TREE;
    3881              :     }
    3882              : 
    3883         1248 :   if (struct_expr)
    3884          624 :     *struct_expr = TREE_OPERAND (comp_ref, 0);
    3885              : 
    3886         1248 :   if (bitpos)
    3887              :     {
    3888              :       /* To calculate the bitposition of the BITFIELD_REF we have to determine
    3889              :          where our bitfield starts in relation to the container REP_DECL. The
    3890              :          DECL_FIELD_OFFSET of the original bitfield's member FIELD_DECL tells
    3891              :          us how many bytes from the start of the structure there are until the
    3892              :          start of the group of bitfield members the FIELD_DECL belongs to,
    3893              :          whereas DECL_FIELD_BIT_OFFSET will tell us how many bits from that
    3894              :          position our actual bitfield member starts.  For the container
    3895              :          REP_DECL adding DECL_FIELD_OFFSET and DECL_FIELD_BIT_OFFSET will tell
    3896              :          us the distance between the start of the structure and the start of
    3897              :          the container, though the first is in bytes and the later other in
    3898              :          bits.  With this in mind we calculate the bit position of our new
    3899              :          BITFIELD_REF by subtracting the number of bits between the start of
    3900              :          the structure and the container from the number of bits from the start
    3901              :          of the structure and the actual bitfield member. */
    3902          624 :       tree bf_pos = fold_build2 (MULT_EXPR, bitsizetype,
    3903              :                                  ref_offset,
    3904              :                                  build_int_cst (bitsizetype, BITS_PER_UNIT));
    3905          624 :       bf_pos = fold_build2 (PLUS_EXPR, bitsizetype, bf_pos,
    3906              :                             DECL_FIELD_BIT_OFFSET (field_decl));
    3907          624 :       tree rep_pos = fold_build2 (MULT_EXPR, bitsizetype,
    3908              :                                   DECL_FIELD_OFFSET (rep_decl),
    3909              :                                   build_int_cst (bitsizetype, BITS_PER_UNIT));
    3910          624 :       rep_pos = fold_build2 (PLUS_EXPR, bitsizetype, rep_pos,
    3911              :                              DECL_FIELD_BIT_OFFSET (rep_decl));
    3912              : 
    3913          624 :       *bitpos = fold_build2 (MINUS_EXPR, bitsizetype, bf_pos, rep_pos);
    3914              :     }
    3915              : 
    3916              :   return rep_decl;
    3917              : 
    3918              : }
    3919              : 
    3920              : /* Lowers the bitfield described by DATA.
    3921              :    For a write like:
    3922              : 
    3923              :    struct.bf = _1;
    3924              : 
    3925              :    lower to:
    3926              : 
    3927              :    __ifc_1 = struct.<representative>;
    3928              :    __ifc_2 = BIT_INSERT_EXPR (__ifc_1, _1, bitpos);
    3929              :    struct.<representative> = __ifc_2;
    3930              : 
    3931              :    For a read:
    3932              : 
    3933              :    _1 = struct.bf;
    3934              : 
    3935              :     lower to:
    3936              : 
    3937              :     __ifc_1 = struct.<representative>;
    3938              :     _1 =  BIT_FIELD_REF (__ifc_1, bitsize, bitpos);
    3939              : 
    3940              :     where representative is a legal load that contains the bitfield value,
    3941              :     bitsize is the size of the bitfield and bitpos the offset to the start of
    3942              :     the bitfield within the representative.  */
    3943              : 
    3944              : static void
    3945          624 : lower_bitfield (gassign *stmt, bool write)
    3946              : {
    3947          624 :   tree struct_expr;
    3948          624 :   tree bitpos;
    3949          624 :   tree rep_decl = get_bitfield_rep (stmt, write, &bitpos, &struct_expr);
    3950          624 :   tree rep_type = TREE_TYPE (rep_decl);
    3951          624 :   tree bf_type = TREE_TYPE (gimple_assign_lhs (stmt));
    3952              : 
    3953          624 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    3954          624 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3955              :     {
    3956            9 :       fprintf (dump_file, "Lowering:\n");
    3957            9 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    3958            9 :       fprintf (dump_file, "to:\n");
    3959              :     }
    3960              : 
    3961              :   /* REP_COMP_REF is a COMPONENT_REF for the representative.  NEW_VAL is it's
    3962              :      defining SSA_NAME.  */
    3963          624 :   tree rep_comp_ref = build3 (COMPONENT_REF, rep_type, struct_expr, rep_decl,
    3964              :                               NULL_TREE);
    3965          624 :   tree new_val = ifc_temp_var (rep_type, rep_comp_ref, &gsi);
    3966              : 
    3967          624 :   if (dump_file && (dump_flags & TDF_DETAILS))
    3968            9 :     print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (new_val), 0, TDF_SLIM);
    3969              : 
    3970          624 :   if (write)
    3971              :     {
    3972          436 :       new_val = ifc_temp_var (rep_type,
    3973              :                               build3 (BIT_INSERT_EXPR, rep_type, new_val,
    3974              :                                       unshare_expr (gimple_assign_rhs1 (stmt)),
    3975              :                                       bitpos), &gsi);
    3976              : 
    3977          436 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3978            0 :         print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (new_val), 0, TDF_SLIM);
    3979              : 
    3980          436 :       gimple *new_stmt = gimple_build_assign (unshare_expr (rep_comp_ref),
    3981              :                                               new_val);
    3982          436 :       gimple_move_vops (new_stmt, stmt);
    3983          436 :       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
    3984              : 
    3985          436 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3986            0 :         print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
    3987              :     }
    3988              :   else
    3989              :     {
    3990          188 :       tree bfr = build3 (BIT_FIELD_REF, bf_type, new_val,
    3991          188 :                          build_int_cst (bitsizetype, TYPE_PRECISION (bf_type)),
    3992              :                          bitpos);
    3993          188 :       new_val = ifc_temp_var (bf_type, bfr, &gsi);
    3994              : 
    3995          188 :       gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (stmt),
    3996              :                                               new_val);
    3997          188 :       gimple_move_vops (new_stmt, stmt);
    3998          188 :       gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
    3999              : 
    4000          188 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4001            9 :         print_gimple_stmt (dump_file, new_stmt, 0, TDF_SLIM);
    4002              :     }
    4003              : 
    4004          624 :   gsi_remove (&gsi, true);
    4005          624 : }
    4006              : 
    4007              : /* Return TRUE if there are bitfields to lower in this LOOP.  Fill TO_LOWER
    4008              :    with data structures representing these bitfields.  */
    4009              : 
    4010              : static bool
    4011       251973 : bitfields_to_lower_p (class loop *loop,
    4012              :                       vec <gassign *> &reads_to_lower,
    4013              :                       vec <gassign *> &writes_to_lower)
    4014              : {
    4015       251973 :   gimple_stmt_iterator gsi;
    4016              : 
    4017       251973 :   if (dump_file && (dump_flags & TDF_DETAILS))
    4018              :     {
    4019           40 :       fprintf (dump_file, "Analyzing loop %d for bitfields:\n", loop->num);
    4020              :     }
    4021              : 
    4022      1054459 :   for (unsigned i = 0; i < loop->num_nodes; ++i)
    4023              :     {
    4024       802510 :       basic_block bb = ifc_bbs[i];
    4025      6922639 :       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    4026              :         {
    4027      5317643 :           gassign *stmt = dyn_cast<gassign*> (gsi_stmt (gsi));
    4028      5317643 :           if (!stmt)
    4029      5188542 :             continue;
    4030              : 
    4031      2129331 :           tree op = gimple_assign_lhs (stmt);
    4032      2129331 :           bool write = TREE_CODE (op) == COMPONENT_REF;
    4033              : 
    4034      2129331 :           if (!write)
    4035      2097495 :             op = gimple_assign_rhs1 (stmt);
    4036              : 
    4037      2129331 :           if (TREE_CODE (op) != COMPONENT_REF)
    4038      2000230 :             continue;
    4039              : 
    4040       129101 :           if (DECL_BIT_FIELD_TYPE (TREE_OPERAND (op, 1)))
    4041              :             {
    4042          648 :               if (dump_file && (dump_flags & TDF_DETAILS))
    4043           11 :                 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    4044              : 
    4045          648 :               if (TREE_THIS_VOLATILE (op)
    4046          648 :                   || reverse_storage_order_for_component_p (op))
    4047              :                 {
    4048            7 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    4049            0 :                     fprintf (dump_file, "\t Bitfield NO OK to lower,"
    4050              :                                         " the access is volatile or"
    4051              :                                         " has reverse storage-order.\n");
    4052           24 :                   return false;
    4053              :                 }
    4054              : 
    4055          641 :               if (!INTEGRAL_TYPE_P (TREE_TYPE (op)))
    4056              :                 {
    4057            0 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    4058            0 :                     fprintf (dump_file, "\t Bitfield NO OK to lower,"
    4059              :                                         " field type is not Integral.\n");
    4060              :                   return false;
    4061              :                 }
    4062              : 
    4063          641 :               if (!get_bitfield_rep (stmt, write, NULL, NULL))
    4064              :                 {
    4065           17 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    4066            2 :                     fprintf (dump_file, "\t Bitfield NOT OK to lower,"
    4067              :                                         " representative is BLKmode.\n");
    4068              :                   return false;
    4069              :                 }
    4070              : 
    4071          624 :               if (dump_file && (dump_flags & TDF_DETAILS))
    4072            9 :                 fprintf (dump_file, "\tBitfield OK to lower.\n");
    4073          624 :               if (write)
    4074          436 :                 writes_to_lower.safe_push (stmt);
    4075              :               else
    4076          188 :                 reads_to_lower.safe_push (stmt);
    4077              :             }
    4078              :         }
    4079              :     }
    4080       503763 :   return !reads_to_lower.is_empty () || !writes_to_lower.is_empty ();
    4081              : }
    4082              : 
    4083              : 
    4084              : /* If-convert LOOP when it is legal.  For the moment this pass has no
    4085              :    profitability analysis.  Returns non-zero todo flags when something
    4086              :    changed.  */
    4087              : 
    4088              : unsigned int
    4089       518985 : tree_if_conversion (class loop *loop, vec<gimple *> *preds)
    4090              : {
    4091       518985 :   unsigned int todo = 0;
    4092       518985 :   bool aggressive_if_conv;
    4093       518985 :   class loop *rloop;
    4094       518985 :   auto_vec <gassign *, 4> reads_to_lower;
    4095       518985 :   auto_vec <gassign *, 4> writes_to_lower;
    4096       518985 :   bitmap exit_bbs;
    4097       518985 :   edge pe;
    4098       518985 :   auto_vec<data_reference_p, 10> refs;
    4099       519911 :   class loop *vloop, *nloop;
    4100              : 
    4101       519911 :  again:
    4102       519911 :   rloop = NULL;
    4103       519911 :   ifc_bbs = NULL;
    4104       519911 :   need_to_lower_bitfields = false;
    4105       519911 :   need_to_ifcvt = false;
    4106       519911 :   need_to_predicate = false;
    4107       519911 :   need_to_rewrite_undefined = false;
    4108       519911 :   any_complicated_phi = false;
    4109              : 
    4110              :   /* Apply more aggressive if-conversion when loop or its outer loop were
    4111              :      marked with simd pragma.  When that's the case, we try to if-convert
    4112              :      loop containing PHIs with more than MAX_PHI_ARG_NUM arguments.  */
    4113       519911 :   aggressive_if_conv = loop->force_vectorize;
    4114       519911 :   if (!aggressive_if_conv)
    4115              :     {
    4116       510453 :       class loop *outer_loop = loop_outer (loop);
    4117       510453 :       if (outer_loop && outer_loop->force_vectorize)
    4118         9734 :         aggressive_if_conv = true;
    4119              :     }
    4120              : 
    4121              :   /* If there are more than two BBs in the loop then there is at least one if
    4122              :      to convert.  */
    4123       519911 :   if (loop->num_nodes > 2
    4124       519911 :       && !ifcvt_split_critical_edges (loop, aggressive_if_conv))
    4125       100369 :     goto cleanup;
    4126              : 
    4127       419542 :   ifc_bbs = get_loop_body_in_if_conv_order (loop);
    4128       419542 :   if (!ifc_bbs)
    4129              :     {
    4130         2647 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4131            3 :         fprintf (dump_file, "Irreducible loop\n");
    4132         2647 :       goto cleanup;
    4133              :     }
    4134              : 
    4135       416895 :   if (find_data_references_in_loop (loop, &refs) == chrec_dont_know)
    4136       152006 :     goto cleanup;
    4137              : 
    4138       264889 :   if (loop->num_nodes > 2)
    4139              :     {
    4140              :       /* More than one loop exit is too much to handle.  */
    4141       139399 :       if (!single_exit (loop))
    4142              :         {
    4143       100733 :           if (dump_file && (dump_flags & TDF_DETAILS))
    4144           10 :             fprintf (dump_file, "Can not ifcvt due to multiple exits\n");
    4145              :         }
    4146              :       else
    4147              :         {
    4148        38666 :           need_to_ifcvt = true;
    4149              : 
    4150        38666 :           if (!if_convertible_loop_p (loop, &refs)
    4151        38666 :               || !dbg_cnt (if_conversion_tree))
    4152        12916 :             goto cleanup;
    4153              :         }
    4154              :     }
    4155              : 
    4156       251973 :   need_to_lower_bitfields = bitfields_to_lower_p (loop, reads_to_lower,
    4157              :                                                   writes_to_lower);
    4158              : 
    4159       251973 :   if (!need_to_ifcvt && !need_to_lower_bitfields)
    4160       225954 :     goto cleanup;
    4161              : 
    4162              :   /* The edge to insert invariant stmts on.  */
    4163        26019 :   pe = loop_preheader_edge (loop);
    4164              : 
    4165              :   /* Since we have no cost model, always version loops.
    4166              :      Either version this loop, or if the pattern is right for outer-loop
    4167              :      vectorization, version the outer loop.  In the latter case we will
    4168              :      still if-convert the original inner loop.  */
    4169        26019 :   vloop = (versionable_outer_loop_p (loop_outer (loop))
    4170        26019 :            ? loop_outer (loop) : loop);
    4171        26019 :   nloop = version_loop_for_if_conversion (vloop, preds);
    4172        26019 :   if (nloop == NULL)
    4173            0 :     goto cleanup;
    4174        26019 :   if (vloop != loop)
    4175              :     {
    4176              :       /* If versionable_outer_loop_p decided to version the
    4177              :          outer loop, version also the inner loop of the non-vectorized
    4178              :          loop copy.  So we transform:
    4179              :              loop1
    4180              :                 loop2
    4181              :              into:
    4182              :               if (LOOP_VECTORIZED (1, 3))
    4183              :                 {
    4184              :                   loop1
    4185              :                     loop2
    4186              :                 }
    4187              :               else
    4188              :                 loop3 (copy of loop1)
    4189              :                   if (LOOP_VECTORIZED (4, 5))
    4190              :                     loop4 (copy of loop2)
    4191              :                   else
    4192              :                     loop5 (copy of loop4)  */
    4193          926 :       gcc_assert (nloop->inner && nloop->inner->next == NULL);
    4194              :       rloop = nloop->inner;
    4195              :     }
    4196              :   else
    4197              :     /* If we versioned loop then make sure to insert invariant
    4198              :        stmts before the .LOOP_VECTORIZED check since the vectorizer
    4199              :        will re-use that for things like runtime alias versioning
    4200              :        whose condition can end up using those invariants.  */
    4201        25093 :     pe = single_pred_edge (gimple_bb (preds->last ()));
    4202              : 
    4203        26019 :   if (need_to_lower_bitfields)
    4204              :     {
    4205          270 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4206              :         {
    4207            9 :           fprintf (dump_file, "-------------------------\n");
    4208            9 :           fprintf (dump_file, "Start lowering bitfields\n");
    4209              :         }
    4210          458 :       while (!reads_to_lower.is_empty ())
    4211          188 :         lower_bitfield (reads_to_lower.pop (), false);
    4212          706 :       while (!writes_to_lower.is_empty ())
    4213          436 :         lower_bitfield (writes_to_lower.pop (), true);
    4214              : 
    4215          270 :       if (dump_file && (dump_flags & TDF_DETAILS))
    4216              :         {
    4217            9 :           fprintf (dump_file, "Done lowering bitfields\n");
    4218            9 :           fprintf (dump_file, "-------------------------\n");
    4219              :         }
    4220              :     }
    4221        26019 :   if (need_to_ifcvt)
    4222              :     {
    4223              :       /* Before we rewrite edges we'll record their original position in the
    4224              :          edge map such that we can map the edges between the ifcvt and the
    4225              :          non-ifcvt loop during peeling.  */
    4226        25750 :       uintptr_t idx = 0;
    4227       103000 :       for (edge exit : get_loop_exit_edges (loop))
    4228        25750 :         exit->aux = (void*)idx++;
    4229              : 
    4230              :       /* Now all statements are if-convertible.  Combine all the basic
    4231              :          blocks into one huge basic block doing the if-conversion
    4232              :          on-the-fly.  */
    4233        25750 :       combine_blocks (loop);
    4234              :     }
    4235              : 
    4236        26019 :   std::pair <tree, tree> *name_pair;
    4237        26019 :   unsigned ssa_names_idx;
    4238        31366 :   FOR_EACH_VEC_ELT (redundant_ssa_names, ssa_names_idx, name_pair)
    4239         5347 :     replace_uses_by (name_pair->first, name_pair->second);
    4240        26019 :   redundant_ssa_names.release ();
    4241              : 
    4242              :   /* Perform local CSE, this esp. helps the vectorizer analysis if loads
    4243              :      and stores are involved.  CSE only the loop body, not the entry
    4244              :      PHIs, those are to be kept in sync with the non-if-converted copy.
    4245              :      ???  We'll still keep dead stores though.  */
    4246        26019 :   exit_bbs = BITMAP_ALLOC (NULL);
    4247       104262 :   for (edge exit : get_loop_exit_edges (loop))
    4248        26223 :     bitmap_set_bit (exit_bbs, exit->dest->index);
    4249        26019 :   todo |= do_rpo_vn (cfun, loop_preheader_edge (loop), exit_bbs,
    4250              :                      false, true, true);
    4251              : 
    4252              :   /* Delete dead predicate computations.  */
    4253        26019 :   ifcvt_local_dce (loop);
    4254        26019 :   BITMAP_FREE (exit_bbs);
    4255              : 
    4256        26019 :   ifcvt_hoist_invariants (loop, pe);
    4257              : 
    4258        26019 :   todo |= TODO_cleanup_cfg;
    4259              : 
    4260       519911 :  cleanup:
    4261       519911 :   data_reference_p dr;
    4262       519911 :   unsigned int i;
    4263      1704170 :   for (i = 0; refs.iterate (i, &dr); i++)
    4264              :     {
    4265      1184259 :       free (dr->aux);
    4266      1184259 :       free_data_ref (dr);
    4267              :     }
    4268       519911 :   refs.truncate (0);
    4269              : 
    4270       519911 :   if (ifc_bbs)
    4271              :     {
    4272              :       unsigned int i;
    4273              : 
    4274      2050526 :       for (i = 0; i < loop->num_nodes; i++)
    4275      1659381 :         free_bb_predicate (ifc_bbs[i]);
    4276              : 
    4277       391145 :       free (ifc_bbs);
    4278       391145 :       ifc_bbs = NULL;
    4279              :     }
    4280       519911 :   if (rloop != NULL)
    4281              :     {
    4282          926 :       loop = rloop;
    4283          926 :       reads_to_lower.truncate (0);
    4284          926 :       writes_to_lower.truncate (0);
    4285          926 :       goto again;
    4286              :     }
    4287              : 
    4288       518985 :   return todo;
    4289       518985 : }
    4290              : 
    4291              : /* Tree if-conversion pass management.  */
    4292              : 
    4293              : namespace {
    4294              : 
    4295              : const pass_data pass_data_if_conversion =
    4296              : {
    4297              :   GIMPLE_PASS, /* type */
    4298              :   "ifcvt", /* name */
    4299              :   OPTGROUP_NONE, /* optinfo_flags */
    4300              :   TV_TREE_LOOP_IFCVT, /* tv_id */
    4301              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    4302              :   0, /* properties_provided */
    4303              :   0, /* properties_destroyed */
    4304              :   0, /* todo_flags_start */
    4305              :   0, /* todo_flags_finish */
    4306              : };
    4307              : 
    4308              : class pass_if_conversion : public gimple_opt_pass
    4309              : {
    4310              : public:
    4311       302985 :   pass_if_conversion (gcc::context *ctxt)
    4312       605970 :     : gimple_opt_pass (pass_data_if_conversion, ctxt)
    4313              :   {}
    4314              : 
    4315              :   /* opt_pass methods: */
    4316              :   bool gate (function *) final override;
    4317              :   unsigned int execute (function *) final override;
    4318              : 
    4319              : }; // class pass_if_conversion
    4320              : 
    4321              : bool
    4322       251589 : pass_if_conversion::gate (function *fun)
    4323              : {
    4324       251589 :   return flag_tree_loop_vectorize || fun->has_force_vectorize_loops;
    4325              : }
    4326              : 
    4327              : unsigned int
    4328       216966 : pass_if_conversion::execute (function *fun)
    4329              : {
    4330       216966 :   unsigned todo = 0;
    4331              : 
    4332       433932 :   if (number_of_loops (fun) <= 1)
    4333              :     return 0;
    4334              : 
    4335       216966 :   auto_vec<gimple *> preds;
    4336      1177674 :   for (auto loop : loops_list (cfun, 0))
    4337       526776 :     if ((flag_tree_loop_vectorize || loop->force_vectorize)
    4338       523801 :         && !loop->dont_vectorize)
    4339       518985 :       todo |= tree_if_conversion (loop, &preds);
    4340              : 
    4341       216966 :   if (todo)
    4342              :     {
    4343        17875 :       free_numbers_of_iterations_estimates (fun);
    4344        17875 :       scev_reset ();
    4345              :     }
    4346              : 
    4347       216966 :   if (flag_checking)
    4348              :     {
    4349       216962 :       basic_block bb;
    4350      7248724 :       FOR_EACH_BB_FN (bb, fun)
    4351      7031762 :         gcc_assert (!bb->aux);
    4352              :     }
    4353              : 
    4354              :   /* Perform IL update now, it might elide some loops.  */
    4355       216966 :   if (todo & TODO_cleanup_cfg)
    4356              :     {
    4357        17875 :       cleanup_tree_cfg ();
    4358        17875 :       if (need_ssa_update_p (fun))
    4359            0 :         todo |= TODO_update_ssa;
    4360              :     }
    4361       216966 :   if (todo & TODO_update_ssa_any)
    4362            0 :     update_ssa (todo & TODO_update_ssa_any);
    4363              : 
    4364              :   /* If if-conversion elided the loop fall back to the original one.  Likewise
    4365              :      if the loops are not nested in the same outer loop.  */
    4366       242985 :   for (unsigned i = 0; i < preds.length (); ++i)
    4367              :     {
    4368        26019 :       gimple *g = preds[i];
    4369        26019 :       if (!gimple_bb (g))
    4370            0 :         continue;
    4371        26019 :       auto ifcvt_loop = get_loop (fun, tree_to_uhwi (gimple_call_arg (g, 0)));
    4372        26019 :       auto orig_loop = get_loop (fun, tree_to_uhwi (gimple_call_arg (g, 1)));
    4373        26019 :       if (!ifcvt_loop || !orig_loop)
    4374              :         {
    4375            2 :           if (dump_file)
    4376            0 :             fprintf (dump_file, "If-converted loop vanished\n");
    4377            2 :           fold_loop_internal_call (g, boolean_false_node);
    4378              :         }
    4379        26017 :       else if (loop_outer (ifcvt_loop) != loop_outer (orig_loop))
    4380              :         {
    4381            0 :           if (dump_file)
    4382            0 :             fprintf (dump_file, "If-converted loop in different outer loop\n");
    4383            0 :           fold_loop_internal_call (g, boolean_false_node);
    4384              :         }
    4385              :     }
    4386              : 
    4387       216966 :   return 0;
    4388       216966 : }
    4389              : 
    4390              : } // anon namespace
    4391              : 
    4392              : gimple_opt_pass *
    4393       302985 : make_pass_if_conversion (gcc::context *ctxt)
    4394              : {
    4395       302985 :   return new pass_if_conversion (ctxt);
    4396              : }
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.