LCOV - code coverage report
Current view: top level - gcc - tree-ssa-loop-ch.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.6 % 523 505
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 22 22
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Loop header copying on trees.
       2              :    Copyright (C) 2004-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it
       7              : under the terms of the GNU General Public License as published by the
       8              : Free Software Foundation; either version 3, or (at your option) any
       9              : later version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT
      12              : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : #include "config.h"
      21              : #include "system.h"
      22              : #include "coretypes.h"
      23              : #include "backend.h"
      24              : #include "tree.h"
      25              : #include "gimple.h"
      26              : #include "cfghooks.h"
      27              : #include "tree-pass.h"
      28              : #include "gimple-ssa.h"
      29              : #include "gimple-iterator.h"
      30              : #include "tree-cfg.h"
      31              : #include "tree-into-ssa.h"
      32              : #include "cfgloop.h"
      33              : #include "tree-inline.h"
      34              : #include "tree-ssa-threadedge.h"
      35              : #include "tree-ssa-sccvn.h"
      36              : #include "tree-phinodes.h"
      37              : #include "ssa-iterators.h"
      38              : #include "value-range.h"
      39              : #include "gimple-range.h"
      40              : #include "gimple-range-path.h"
      41              : #include "gimple-pretty-print.h"
      42              : #include "cfganal.h"
      43              : #include "tree-ssa-loop-manip.h"
      44              : #include "tree-ssa-loop-niter.h"
      45              : #include "tree-scalar-evolution.h"
      46              : 
      47              : /* Return path query instance for testing ranges of statements
      48              :    in headers of LOOP contained in basic block BB.
      49              :    Use RANGER instance.  */
      50              : 
      51              : static path_range_query *
      52       826838 : get_range_query (class loop *loop,
      53              :                  basic_block bb,
      54              :                  gimple_ranger &ranger)
      55              : {
      56       826838 :   auto_vec<basic_block, 8> path;
      57      1059219 :   for (; bb != loop->header; bb = single_pred_edge (bb)->src)
      58       232381 :     path.safe_push (bb);
      59       826838 :   path.safe_push (loop->header);
      60       826838 :   path.safe_push (loop_preheader_edge (loop)->src);
      61       826838 :   return new path_range_query (ranger, path);
      62       826838 : }
      63              : 
      64              : /* Return edge that is true in the first iteration of the loop
      65              :    and NULL otherwise.
      66              :    Formulate corrent ranger query to RANGER.  */
      67              : 
      68              : static edge
      69       782885 : static_loop_exit (class loop *l, basic_block bb, gimple_ranger &ranger,
      70              :                   path_range_query *&query)
      71              : {
      72      1565770 :   gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
      73       782885 :   edge ret_e;
      74              : 
      75       782885 :   if (!last)
      76              :     return NULL;
      77              : 
      78       782885 :   edge true_e, false_e;
      79       782885 :   extract_true_false_edges_from_block (bb, &true_e, &false_e);
      80              : 
      81              :   /* If neither edge is the exit edge, this is not a case we'd like to
      82              :      special-case.  */
      83       782885 :   if (!loop_exit_edge_p (l, true_e) && !loop_exit_edge_p (l, false_e))
      84              :     return NULL;
      85              : 
      86       782885 :   int_range<1> desired_static_range;
      87       782885 :   if (loop_exit_edge_p (l, true_e))
      88              :     {
      89       253874 :       desired_static_range = range_false ();
      90       253874 :       ret_e = true_e;
      91              :     }
      92              :   else
      93              :    {
      94       529011 :       desired_static_range = range_true ();
      95       529011 :       ret_e = false_e;
      96              :    }
      97              : 
      98       782885 :   if (!query)
      99        97272 :     query = get_range_query (l, gimple_bb (last), ranger);
     100              : 
     101       782885 :   int_range<2> r;
     102       782885 :   if (!query->range_of_stmt (r, last))
     103              :     return NULL;
     104       782885 :   return r == desired_static_range ? ret_e : NULL;
     105       782885 : }
     106              : 
     107              : /* Return true if STMT is static in LOOP.  This means that its value
     108              :    is constant in the first iteration.
     109              :    Use RANGER and formulate query cached in QUERY.  */
     110              : 
     111              : static bool
     112      1414460 : loop_static_stmt_p (class loop *loop,
     113              :                     gimple_ranger &ranger,
     114              :                     path_range_query *&query,
     115              :                     gimple *stmt)
     116              : {
     117      1414460 :   tree type = gimple_range_type (stmt);
     118      1414460 :   if (!type || !value_range::supports_type_p (type))
     119              :     return false;
     120              : 
     121      1407301 :   if (!query)
     122       729566 :     query = get_range_query (loop, gimple_bb (stmt), ranger);
     123              : 
     124      1407301 :   value_range r (gimple_range_type (stmt));
     125      1407301 :   if (!query->range_of_stmt (r, stmt))
     126              :     return false;
     127      1407301 :   return r.singleton_p ();
     128      1407301 : }
     129              : 
     130              : /* Return true if OP is invariant.  */
     131              : 
     132              : static bool
     133      2231155 : loop_invariant_op_p (class loop *loop,
     134              :                      tree op)
     135              : {
     136      2231155 :   if (is_gimple_min_invariant (op))
     137              :     return true;
     138      1746008 :   if (SSA_NAME_IS_DEFAULT_DEF (op)
     139      1746008 :       || !flow_bb_inside_loop_p (loop, gimple_bb (SSA_NAME_DEF_STMT (op))))
     140              :     return true;
     141      1446175 :   return gimple_uid (SSA_NAME_DEF_STMT (op)) & 1;
     142              : }
     143              : 
     144              : /* Return true if OP combines outcome of static and
     145              :    loop invariant conditional.  */
     146              : 
     147              : static bool
     148        17339 : loop_static_op_p (class loop *loop, tree op)
     149              : {
     150              :   /* Always check for invariant first.  */
     151        34678 :   gcc_checking_assert (!is_gimple_min_invariant (op)
     152              :                        && !SSA_NAME_IS_DEFAULT_DEF (op)
     153              :                        && flow_bb_inside_loop_p (loop,
     154              :                                gimple_bb (SSA_NAME_DEF_STMT (op))));
     155        17339 :   return gimple_uid (SSA_NAME_DEF_STMT (op)) & 2;
     156              : }
     157              : 
     158              : 
     159              : /* Return true if OP combines outcome of static and
     160              :    loop invariant conditional.  */
     161              : 
     162              : static bool
     163       705503 : loop_combined_static_and_iv_p (class loop *loop,
     164              :                                tree op)
     165              : {
     166              :   /* Always check for invariant first.  */
     167      1411006 :   gcc_checking_assert (!is_gimple_min_invariant (op)
     168              :                        && !SSA_NAME_IS_DEFAULT_DEF (op)
     169              :                        && flow_bb_inside_loop_p (loop,
     170              :                                gimple_bb (SSA_NAME_DEF_STMT (op))));
     171       705503 :   return gimple_uid (SSA_NAME_DEF_STMT (op)) & 4;
     172              : }
     173              : 
     174              : /* Decision about possibility of copying a given header.  */
     175              : 
     176              : enum ch_decision
     177              : {
     178              :   /* We do not want to copy this header.  */
     179              :   ch_impossible,
     180              :   /* We can copy it if it enables wins.  */
     181              :   ch_possible,
     182              :   /* We can "copy" it if it enables wins and doing
     183              :      so will introduce no new code.  */
     184              :   ch_possible_zero_cost,
     185              :   /* We want to copy.  */
     186              :   ch_win,
     187              :   /* We want to copy and we will eliminate loop exit.  */
     188              :   ch_win_invariant_exit,
     189              :   
     190              : };
     191              : 
     192              : /* Check whether we should duplicate HEADER of LOOP.  At most *LIMIT
     193              :    instructions should be duplicated, limit is decreased by the actual
     194              :    amount.  In the case of *CANBE_NEVEREXECUTED, if there is a exit edge
     195              :    of the HEADER that is most likely never executed then consider that
     196              :    as invariant and continue. Set *CANBE_NEVEREXECUTED to false otherwise.  */
     197              : 
     198              : static ch_decision
     199      1523100 : should_duplicate_loop_header_p (basic_block header, class loop *loop,
     200              :                                 gimple_ranger *ranger,
     201              :                                 int *limit,
     202              :                                 hash_set <edge> *invariant_exits,
     203              :                                 hash_set <edge> *static_exits,
     204              :                                 bool *canbe_neverexecuted)
     205              : {
     206      1523100 :   gimple_stmt_iterator bsi;
     207              : 
     208      1523100 :   gcc_assert (!header->aux);
     209              : 
     210      1523100 :   gcc_assert (EDGE_COUNT (header->succs) > 0);
     211      1523100 :   if (single_succ_p (header))
     212              :     {
     213       470815 :       if (dump_file && (dump_flags & TDF_DETAILS))
     214           20 :         fprintf (dump_file,
     215              :                  "  Not duplicating bb %i: it is single succ.\n",
     216              :                  header->index);
     217              :       return ch_impossible;
     218              :     }
     219              : 
     220      1052285 :   if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest)
     221      1052285 :       && flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 1)->dest))
     222              :     {
     223       149041 :       if (dump_file && (dump_flags & TDF_DETAILS))
     224            2 :         fprintf (dump_file,
     225              :                  "  Not duplicating bb %i: both successors are in loop.\n",
     226              :                  loop->num);
     227              :       return ch_impossible;
     228              :     }
     229              : 
     230              :   /* If this is not the original loop header, we want it to have just
     231              :      one predecessor in order to match the && pattern.  */
     232      1099518 :   if (header != loop->header && !single_pred_p (header))
     233              :     {
     234            0 :       if (dump_file && (dump_flags & TDF_DETAILS))
     235            0 :         fprintf (dump_file,
     236              :                  "  Not duplicating bb %i: it has multiple predecestors.\n",
     237              :                  header->index);
     238              :       return ch_impossible;
     239              :     }
     240              : 
     241      1806488 :   gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (header));
     242       903244 :   if (!last)
     243              :     {
     244        50664 :       if (dump_file && (dump_flags & TDF_DETAILS))
     245            0 :         fprintf (dump_file,
     246              :                  "  Not duplicating bb %i: it does not end by conditional.\n",
     247              :                  header->index);
     248              :       return ch_impossible;
     249              :     }
     250              : 
     251       852580 :   path_range_query *query = NULL;
     252      2229084 :   for (gphi_iterator psi = gsi_start_phis (header); !gsi_end_p (psi);
     253      1376504 :        gsi_next (&psi))
     254      2753008 :     if (!virtual_operand_p (gimple_phi_result (psi.phi ())))
     255              :       {
     256      1876616 :         bool static_p = loop_static_stmt_p (loop, *ranger,
     257       938308 :                                             query, psi.phi ());
     258      1876616 :         gimple_set_uid (psi.phi (), static_p ? 2 : 0);
     259              :       }
     260       852580 :   bool code_size_cost = false;
     261              : 
     262              :   /* Count number of instructions and punt on calls.
     263              :      Populate stmts INV flag to later apply heuristics to the
     264              :      kind of conditions we want to copy.  */
     265      4235470 :   for (bsi = gsi_start_bb (header); !gsi_end_p (bsi); gsi_next (&bsi))
     266              :     {
     267      3382890 :       gimple *last = gsi_stmt (bsi);
     268              : 
     269      3382890 :       if (gimple_code (last) == GIMPLE_LABEL)
     270        11992 :         continue;
     271              : 
     272      3370898 :       if (is_gimple_debug (last))
     273      1629508 :         continue;
     274              : 
     275      1741390 :       if (gimple_code (last) == GIMPLE_COND)
     276              :         break;
     277              : 
     278       958446 :       if (dump_file && (dump_flags & TDF_DETAILS))
     279              :         {
     280           52 :           fprintf (dump_file, "  Analyzing: ");
     281           52 :           print_gimple_stmt (dump_file, last, 0, TDF_SLIM);
     282              :         }
     283              : 
     284       958446 :       if (gimple_code (last) == GIMPLE_CALL
     285       958446 :           && (!gimple_inexpensive_call_p (as_a <gcall *> (last))
     286              :               /* IFN_LOOP_DIST_ALIAS means that inner loop is distributed
     287              :                  at current loop's header.  Don't copy in this case.  */
     288         6325 :               || gimple_call_internal_p (last, IFN_LOOP_DIST_ALIAS)))
     289              :         {
     290        50825 :           if (dump_file && (dump_flags & TDF_DETAILS))
     291            0 :             fprintf (dump_file,
     292              :                      "  Not duplicating bb %i: it contains call.\n",
     293              :                      header->index);
     294        50825 :           if (query)
     295        35644 :             delete query;
     296              :           return ch_impossible;
     297              :         }
     298              : 
     299              :       /* Classify the stmt is invariant in the loop.  */
     300       907621 :       gimple_set_uid (last, 0);
     301      1402018 :       if (!gimple_vuse (last)
     302       494397 :           && gimple_code (last) != GIMPLE_ASM
     303      1401942 :           && (gimple_code (last) != GIMPLE_CALL
     304          550 :               || gimple_call_flags (last) & ECF_CONST))
     305              :         {
     306       494209 :           bool inv = true, static_p = false;
     307       494209 :           ssa_op_iter i;
     308       494209 :           tree op;
     309      1141250 :           FOR_EACH_SSA_TREE_OPERAND (op, last, i, SSA_OP_USE)
     310       647041 :             if (!loop_invariant_op_p (loop, op))
     311       544033 :               inv = false;
     312              :           /* Assume that code is reasonably optimized and invariant
     313              :              constants are already identified.  */
     314       494209 :           if (!inv)
     315       476152 :             static_p = loop_static_stmt_p (loop, *ranger, query, last);
     316       922500 :           gimple_set_uid (last, (inv ? 1 : 0) | (static_p ? 2 : 0));
     317       494209 :           if (dump_file && (dump_flags & TDF_DETAILS))
     318              :             {
     319           35 :               if (inv)
     320            6 :                 fprintf (dump_file, "    Stmt is loop invariant\n");
     321           35 :               if (static_p)
     322            3 :                 fprintf (dump_file, "    Stmt is static "
     323              :                          "(constant in the first iteration)\n");
     324              :             }
     325              :           /* Loop invariants will be optimized out in loop body after
     326              :              duplication; do not account invariant computation in code
     327              :              size costs.
     328              : 
     329              :              Similarly static computations will be optimized out in the
     330              :              duplicated header.  */
     331       494209 :           if (inv || static_p)
     332        84065 :             continue;
     333              : 
     334              :           /* Match the following:
     335              :              _1 = i_1 < 10   <- static condition
     336              :              _2 = n != 0     <- loop invariant condition
     337              :              _3 = _1 & _2    <- combined static and iv statement.  */
     338       410234 :           tree_code code;
     339       819954 :           auto assign = dyn_cast<gassign *> (last);
     340       409810 :           if (assign
     341       409810 :               && ((code = gimple_assign_rhs_code (assign)) == BIT_AND_EXPR
     342       397475 :                   || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR))
     343              :             {
     344        15919 :               tree op1 = gimple_assign_rhs1 (assign);
     345        15919 :               tree op2 = gimple_assign_rhs2 (assign);
     346              : 
     347        15919 :               if ((loop_invariant_op_p (loop, op1)
     348        15073 :                    || loop_combined_static_and_iv_p (loop, op1)
     349        15071 :                    || loop_static_op_p (loop, op1))
     350        17380 :                   && (loop_invariant_op_p (loop, op2)
     351         2268 :                       || loop_combined_static_and_iv_p (loop, op2)
     352         2268 :                       || loop_static_op_p (loop, op2)))
     353              :                 {
     354              :                   /* Duplicating loop header with combned conditional will
     355              :                      remove this statement in each copy.  But we account for
     356              :                      that later when seeing that condition.
     357              : 
     358              :                      Note that this may be overly optimistic for bit operations
     359              :                      where the static parameter may still result in non-trivial
     360              :                      bit operation.  */
     361           90 :                   gimple_set_uid (last, 4);
     362           90 :                   if (dump_file && (dump_flags & TDF_DETAILS))
     363            1 :                     fprintf (dump_file,
     364              :                              "    Stmt combines static and invariant op\n");
     365           90 :                   continue;
     366              :                 }
     367              :             }
     368              :         }
     369              : 
     370       823556 :       int insns = estimate_num_insns (last, &eni_size_weights);
     371       823556 :       *limit -= insns;
     372       823556 :       if (insns)
     373       705443 :         code_size_cost = true;
     374       823556 :       if (dump_file && (dump_flags & TDF_DETAILS))
     375           42 :         fprintf (dump_file,
     376              :                  "    Accounting stmt as %i insns\n", insns);
     377       823556 :       if (*limit < 0)
     378              :         {
     379        18811 :           if (dump_file && (dump_flags & TDF_DETAILS))
     380            0 :             fprintf (dump_file,
     381              :                      "  Not duplicating bb %i contains too many insns.\n",
     382              :                      header->index);
     383        18811 :           if (query)
     384         8250 :             delete query;
     385              :           return ch_impossible;
     386              :         }
     387              :     }
     388              : 
     389       782944 :   if (dump_file && (dump_flags & TDF_DETAILS))
     390              :     {
     391           35 :       fprintf (dump_file, "  Analyzing: ");
     392           35 :       print_gimple_stmt (dump_file, last, 0, TDF_SLIM);
     393              :     }
     394              : 
     395              :   /* If the condition tests a non-IV loop variant we do not want to rotate
     396              :      the loop further.  Unless this is the original loop header.  */
     397       782944 :   tree lhs = gimple_cond_lhs (last);
     398       782944 :   tree rhs = gimple_cond_rhs (last);
     399       782944 :   bool lhs_invariant = loop_invariant_op_p (loop, lhs);
     400       782944 :   bool rhs_invariant = loop_invariant_op_p (loop, rhs);
     401              : 
     402              :   /* Combined conditional is a result of if combining:
     403              : 
     404              :      _1 = i_1 < 10   <- static condition
     405              :      _2 = n != 0     <- loop invariant condition
     406              :      _3 = _1 & _2    <- combined static and iv statement
     407              :      if (_3 != 0)    <- combined conditional
     408              : 
     409              :      Combined conditionals will not be optimized out in either copy.
     410              :      However duplicaed header simplifies to:
     411              : 
     412              :      if (n < 10)
     413              : 
     414              :      while loop body to
     415              : 
     416              :      if (i_1 < 10)
     417              : 
     418              :      So effectively the resulting code sequence will be of same length as
     419              :      the original code.
     420              : 
     421              :      Combined conditionals are never static or invariant, so save some work
     422              :      below.  */
     423       782944 :   if (lhs_invariant != rhs_invariant
     424       688162 :       && (lhs_invariant
     425       583598 :           || loop_combined_static_and_iv_p (loop, lhs))
     426       887567 :       && (rhs_invariant
     427       104564 :           || loop_combined_static_and_iv_p (loop, rhs)))
     428              :    {
     429           59 :      if (query)
     430           59 :        delete query;
     431           59 :       if (dump_file && (dump_flags & TDF_DETAILS))
     432            1 :         fprintf (dump_file,
     433              :                  "  Conditional combines static and invariant op.\n");
     434              :      return ch_win;
     435              :    }
     436              : 
     437       782885 :   edge static_exit = static_loop_exit (loop, header, *ranger, query);
     438       782885 :   if (query)
     439       782885 :     delete query;
     440              : 
     441       782885 :   if (static_exit && static_exits)
     442              :     {
     443       338175 :       static_exits->add (static_exit);
     444       338175 :       if (dump_file && (dump_flags & TDF_DETAILS))
     445            5 :         fprintf (dump_file,
     446              :                  "    Will eliminate peeled conditional in bb %d.\n",
     447            5 :                  static_exit->src->index);
     448              :       /* Still look for invariant exits; exit may be both.  */
     449              :     }
     450       782885 :   if (lhs_invariant && rhs_invariant)
     451              :     {
     452         5009 :       if (invariant_exits)
     453              :         {
     454         5009 :           edge e;
     455         5009 :           edge_iterator ei;
     456        15027 :           FOR_EACH_EDGE (e, ei, header->succs)
     457        10018 :             if (loop_exit_edge_p (loop, e))
     458              :               {
     459         5009 :                 if (dump_file && (dump_flags & TDF_DETAILS))
     460            4 :                   fprintf (dump_file,
     461              :                            "    Will eliminate invariant exit %i->%i\n",
     462            4 :                            e->src->index, e->dest->index);
     463         5009 :                 invariant_exits->add (e);
     464              :               }
     465              :         }
     466              :       return ch_win_invariant_exit;
     467              :     }
     468       777876 :   if (!static_exit && *canbe_neverexecuted)
     469              :     {
     470              :       /* See if one of the edges are an exit edge that is probable
     471              :          never executed.
     472              :          If so treat it as invariant exit win.  */
     473       298211 :       edge e;
     474       298211 :       edge_iterator ei;
     475       298211 :       bool hasone = false;
     476       894633 :       FOR_EACH_EDGE (e, ei, header->succs)
     477       596422 :         if (loop_exit_edge_p (loop, e)
     478       596422 :             && probably_never_executed_edge_p (cfun, e))
     479              :           {
     480         7179 :             hasone = true;
     481         7179 :             if (dump_file && (dump_flags & TDF_DETAILS))
     482            5 :               fprintf (dump_file,
     483              :                        "    `never executed` exit %i->%i\n",
     484            5 :                        e->src->index, e->dest->index);
     485              :           }
     486       298211 :       if (hasone)
     487         7179 :         return ch_win_invariant_exit;
     488              :     }
     489       770697 :   *canbe_neverexecuted = false;
     490              : 
     491              :   /* If the static exit fully optimize out, it is win to "duplicate"
     492              :      it.
     493              : 
     494              :      TODO: Even if duplication costs some size we may opt to do so in case
     495              :      exit probability is significant enough (do partial peeling).  */
     496       770697 :   if (static_exit)
     497       337811 :     return !code_size_cost ? ch_possible_zero_cost : ch_possible;
     498              : 
     499              :   /* We was not able to prove that conditional will be eliminated.  */
     500       432886 :   int insns = estimate_num_insns (last, &eni_size_weights);
     501       432886 :   *limit -= insns;
     502       432886 :   if (dump_file && (dump_flags & TDF_DETAILS))
     503           20 :     fprintf (dump_file,
     504              :              "    Accounting stmt as %i insns\n", insns);
     505       432886 :   if (*limit < 0)
     506              :     {
     507        11595 :       if (dump_file && (dump_flags & TDF_DETAILS))
     508            0 :         fprintf (dump_file,
     509              :                  "  Not duplicating bb %i contains too many insns.\n",
     510              :                  header->index);
     511              :       return ch_impossible;
     512              :     }
     513              : 
     514              :   return ch_possible;
     515              : }
     516              : 
     517              : /* Checks whether LOOP is a do-while style loop.  */
     518              : 
     519              : static bool
     520       922758 : do_while_loop_p (class loop *loop)
     521              : {
     522       922758 :   gimple *stmt = last_nondebug_stmt (loop->latch);
     523              : 
     524              :   /* If the latch of the loop is not empty, it is not a do-while loop.  */
     525       922758 :   if (stmt
     526       922758 :       && gimple_code (stmt) != GIMPLE_LABEL)
     527              :     {
     528       585311 :       if (dump_file && (dump_flags & TDF_DETAILS))
     529           11 :         fprintf (dump_file,
     530              :                  "Loop %i is not do-while loop: latch is not empty.\n",
     531              :                  loop->num);
     532              :       return false;
     533              :     }
     534              : 
     535              :   /* If the latch does not have a single predecessor, it is not a
     536              :      do-while loop.  */
     537       337447 :   if (!single_pred_p (loop->latch))
     538              :     {
     539        25377 :       if (dump_file && (dump_flags & TDF_DETAILS))
     540            1 :         fprintf (dump_file,
     541              :                  "Loop %i is not do-while loop: latch has multiple "
     542              :                  "predecessors.\n", loop->num);
     543              :       return false;
     544              :     }
     545       312070 :   basic_block pred = single_pred (loop->latch);
     546              : 
     547              :   /* If the latch predecessor doesn't exit the loop, it is not a
     548              :      do-while loop.  */
     549       312070 :   if (!loop_exits_from_bb_p (loop, pred))
     550              :     {
     551         7631 :       if (dump_file && (dump_flags & TDF_DETAILS))
     552            0 :         fprintf (dump_file,
     553              :                  "Loop %i is not do-while loop: latch predecessor "
     554              :                  "does not exit loop.\n", loop->num);
     555              :       return false;
     556              :     }
     557       608878 :   gcond *last = safe_dyn_cast <gcond *> (*gsi_last_bb (pred));
     558       303352 :   if (last
     559       303352 :       && (gimple_cond_lhs (last) == boolean_false_node
     560       303352 :           || gimple_cond_lhs (last) == boolean_true_node)
     561            1 :       && gimple_cond_rhs (last) == boolean_false_node)
     562              :     {
     563            1 :       if (dump_file && (dump_flags & TDF_DETAILS))
     564            1 :         fprintf (dump_file,
     565              :                  "Loop %i is not do-while loop: latch predecessor "
     566              :                  "contains exit we optimized out.\n", loop->num);
     567              :       return false;
     568              :     }
     569              : 
     570       304438 :   if (dump_file && (dump_flags & TDF_DETAILS))
     571           20 :     fprintf (dump_file, "Loop %i is do-while loop\n", loop->num);
     572              : 
     573              :   return true;
     574              : }
     575              : 
     576              : /* Update profile after header copying of LOOP.
     577              :    REGION is the original (in loop) sequence, REGION_COPY is the
     578              :    duplicated header (now outside of loop). N_REGION is number of
     579              :    bbs duplicated.
     580              :    ELIMINATED_EDGE is edge to be removed from duplicated sequence.
     581              :    INVARIANT_EXITS are edges in the loop body to be elimianted
     582              :    since they are loop invariants
     583              : 
     584              :    So We expect the following:
     585              : 
     586              :       // region_copy_start entry will be scaled to entry_count
     587              :          if (cond1)         <- this condition will become false
     588              :                                and we update probabilities
     589              :            goto loop_exit;
     590              :          if (cond2)         <- this condition is loop invariant
     591              :            goto loop_exit;
     592              :          goto loop_header   <- this will be redirected to loop.
     593              :        // region_copy_end
     594              :      loop:
     595              :                <body>
     596              :        // region start
     597              :      loop_header:
     598              :                if (cond1)   <- we need to update probability here
     599              :                  goto loop_exit;
     600              :                if (cond2)   <- and determine scaling factor here.
     601              :                                moreover cond2 is now always true
     602              :                  goto loop_exit;
     603              :                else
     604              :                  goto loop;
     605              :        // region end
     606              : 
     607              :      Adding support for more exits can be done similarly,
     608              :      but only consumer so far is tree-ssa-loop-ch and it uses only this
     609              :      to handle the common case of peeling headers which have
     610              :      conditionals known to be always true upon entry.  */
     611              : 
     612              : static void
     613       589419 : update_profile_after_ch (class loop *loop,
     614              :                          basic_block *region, basic_block *region_copy,
     615              :                          unsigned n_region,
     616              :                          hash_set <edge> *invariant_exits,
     617              :                          hash_set <edge> *static_exits,
     618              :                          profile_count entry_count)
     619              : {
     620      1188554 :   for (unsigned int i = 0; i < n_region; i++)
     621              :     {
     622       599135 :       edge exit_e, exit_e_copy, e, e_copy;
     623       599135 :       if (EDGE_COUNT (region[i]->succs) == 1)
     624              :         {
     625            0 :           region_copy[i]->count = entry_count;
     626            0 :           region[i]->count -= entry_count;
     627            0 :           continue;
     628              :         }
     629              : 
     630       599135 :       gcc_checking_assert (EDGE_COUNT (region[i]->succs) == 2);
     631       599135 :       if (loop_exit_edge_p (loop,
     632       599135 :                             EDGE_SUCC (region[i], 0)))
     633              :         {
     634       142728 :           exit_e = EDGE_SUCC (region[i], 0);
     635       142728 :           exit_e_copy = EDGE_SUCC (region_copy[i], 0);
     636       142728 :           e = EDGE_SUCC (region[i], 1);
     637       142728 :           e_copy = EDGE_SUCC (region_copy[i], 1);
     638              :         }
     639              :       else
     640              :         {
     641       456407 :           exit_e = EDGE_SUCC (region[i], 1);
     642       456407 :           exit_e_copy = EDGE_SUCC (region_copy[i], 1);
     643       456407 :           e = EDGE_SUCC (region[i], 0);
     644       456407 :           e_copy = EDGE_SUCC (region_copy[i], 0);
     645              :         }
     646       599135 :       gcc_assert (i == n_region - 1
     647              :                   || (e->dest == region[i + 1]
     648              :                       && e_copy->dest == region_copy[i + 1]));
     649       599135 :       region_copy[i]->count = entry_count;
     650       599135 :       profile_count exit_e_count = exit_e->count ();
     651       599135 :       bool was_static = false;
     652       599135 :       if (static_exits->contains (exit_e))
     653              :         {
     654              :           /* Update profile and the conditional.
     655              :              CFG update is done by caller.  */
     656       330709 :           static_exits->remove (exit_e);
     657       330709 :           was_static = true;
     658       330709 :           e_copy->probability = profile_probability::always ();
     659       330709 :           exit_e_copy->probability = profile_probability::never ();
     660       330709 :           gcond *cond_stmt
     661       661418 :                   = as_a <gcond *>(*gsi_last_bb (region_copy[i]));
     662       330709 :           if (e_copy->flags & EDGE_TRUE_VALUE)
     663       238633 :             gimple_cond_make_true (cond_stmt);
     664              :           else
     665        92076 :             gimple_cond_make_false (cond_stmt);
     666       330709 :           update_stmt (cond_stmt);
     667              :           /* Header copying is a special case of jump threading, so use
     668              :              common code to update loop body exit condition.  */
     669       330709 :           update_bb_profile_for_threading (region[i], entry_count, e);
     670              :         }
     671              :       else
     672       268426 :         region[i]->count -= region_copy[i]->count;
     673       599135 :       if (invariant_exits->contains (exit_e))
     674              :         {
     675         5005 :           invariant_exits->remove (exit_e);
     676              :           /* All exits will happen in exit_e_copy which is out of the
     677              :              loop, so increase probability accordingly.
     678              :              If the edge is eliminated_edge we already corrected
     679              :              profile above.  */
     680         9960 :           if (entry_count.nonzero_p () && !was_static)
     681         4591 :             set_edge_probability_and_rescale_others
     682         4591 :                     (exit_e_copy, exit_e_count.probability_in
     683              :                                                         (entry_count));
     684              :           /* Eliminate in-loop conditional.  */
     685         5005 :           e->probability = profile_probability::always ();
     686         5005 :           exit_e->probability = profile_probability::never ();
     687        10010 :           gcond *cond_stmt = as_a <gcond *>(*gsi_last_bb (region[i]));
     688         5005 :           if (e->flags & EDGE_TRUE_VALUE)
     689         2793 :             gimple_cond_make_true (cond_stmt);
     690              :           else
     691         2212 :             gimple_cond_make_false (cond_stmt);
     692         5005 :           update_stmt (cond_stmt);
     693              :         }
     694       599135 :       entry_count = e_copy->count ();
     695              :     }
     696              :   /* Be sure that we seen all invariant exit edges we are supposed to update.
     697              :      We may have recorded some static exists we decided to not duplicate.  */
     698       589419 :   gcc_checking_assert (invariant_exits->is_empty ());
     699       589419 : }
     700              : 
     701              : namespace {
     702              : 
     703              : /* Common superclass for both header-copying phases.  */
     704              : class ch_base : public gimple_opt_pass
     705              : {
     706              :   protected:
     707       908955 :     ch_base (pass_data data, gcc::context *ctxt)
     708      1817910 :       : gimple_opt_pass (data, ctxt)
     709              :     {}
     710              : 
     711              :   /* Copies headers of all loops in FUN for which process_loop_p is true.  */
     712              :   unsigned int copy_headers (function *fun);
     713              : 
     714              :   /* Return true to copy headers of LOOP or false to skip.  */
     715              :   virtual bool process_loop_p (class loop *loop) = 0;
     716              : };
     717              : 
     718              : const pass_data pass_data_ch =
     719              : {
     720              :   GIMPLE_PASS, /* type */
     721              :   "ch", /* name */
     722              :   OPTGROUP_LOOP, /* optinfo_flags */
     723              :   TV_TREE_CH, /* tv_id */
     724              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
     725              :   0, /* properties_provided */
     726              :   0, /* properties_destroyed */
     727              :   0, /* todo_flags_start */
     728              :   0, /* todo_flags_finish */
     729              : };
     730              : 
     731              : class pass_ch : public ch_base
     732              : {
     733              : public:
     734       605970 :   pass_ch (gcc::context *ctxt)
     735       605970 :     : ch_base (pass_data_ch, ctxt)
     736       605970 :   {}
     737              : 
     738              :   /* opt_pass methods: */
     739      1069342 :   bool gate (function *) final override { return flag_tree_ch != 0; }
     740              : 
     741              :   /* Initialize and finalize loop structures, copying headers in between.  */
     742              :   unsigned int execute (function *) final override;
     743              : 
     744       302985 :   opt_pass * clone () final override { return new pass_ch (m_ctxt); }
     745              : 
     746              : protected:
     747              :   /* ch_base method: */
     748              :   bool process_loop_p (class loop *loop) final override;
     749              : }; // class pass_ch
     750              : 
     751              : const pass_data pass_data_ch_vect =
     752              : {
     753              :   GIMPLE_PASS, /* type */
     754              :   "ch_vect", /* name */
     755              :   OPTGROUP_LOOP, /* optinfo_flags */
     756              :   TV_TREE_CH, /* tv_id */
     757              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
     758              :   0, /* properties_provided */
     759              :   0, /* properties_destroyed */
     760              :   0, /* todo_flags_start */
     761              :   0, /* todo_flags_finish */
     762              : };
     763              : 
     764              : /* This is a more aggressive version of the same pass, designed to run just
     765              :    before if-conversion and vectorization, to put more loops into the form
     766              :    required for those phases.  */
     767              : class pass_ch_vect : public ch_base
     768              : {
     769              : public:
     770       302985 :   pass_ch_vect (gcc::context *ctxt)
     771       302985 :     : ch_base (pass_data_ch_vect, ctxt)
     772       302985 :   {}
     773              : 
     774              :   /* opt_pass methods: */
     775       251589 :   bool gate (function *fun) final override
     776              :   {
     777       251589 :     return flag_tree_ch != 0
     778       251589 :            && (flag_tree_loop_vectorize != 0 || fun->has_force_vectorize_loops);
     779              :   }
     780              : 
     781              :   /* Just copy headers, no initialization/finalization of loop structures.  */
     782              :   unsigned int execute (function *) final override;
     783              : 
     784              : protected:
     785              :   /* ch_base method: */
     786              :   bool process_loop_p (class loop *loop) final override;
     787              : }; // class pass_ch_vect
     788              : 
     789              : /* Sort comparator to order loops after the specified order.  */
     790              : 
     791              : static int
     792        45747 : ch_order_loops (const void *a_, const void *b_, void *order_)
     793              : {
     794        45747 :   int *order = (int *)order_;
     795        45747 :   const class loop *a = *(const class loop * const *)a_;
     796        45747 :   const class loop *b = *(const class loop * const *)b_;
     797        45747 :   if (a->num == b->num)
     798              :     return 0;
     799        45747 :   if (order[a->num] < order[b->num])
     800        22233 :     return -1;
     801              :   return 1;
     802              : }
     803              : 
     804              : /* For all loops, copy the condition at the end of the loop body in front
     805              :    of the loop.  This is beneficial since it increases efficiency of
     806              :    code motion optimizations.  It also saves one jump on entry to the loop.  */
     807              : 
     808              : unsigned int
     809      1285951 : ch_base::copy_headers (function *fun)
     810              : {
     811      1285951 :   basic_block *bbs, *copied_bbs;
     812      1285951 :   unsigned bbs_size;
     813      1285951 :   bool changed = false;
     814              : 
     815      2571902 :   if (number_of_loops (fun) <= 1)
     816              :     return 0;
     817              : 
     818       467920 :   bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
     819       467920 :   copied_bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
     820       467920 :   bbs_size = n_basic_blocks_for_fn (fun);
     821              : 
     822       467920 :   struct candidate
     823              :     {
     824              :       class loop *loop;
     825              :       unsigned int nheaders;
     826              :       hash_set <edge> *invariant_exits, *static_exits;
     827              :     };
     828       467920 :   auto_vec<struct candidate> candidates;
     829       467920 :   auto_vec<std::pair<edge, loop_p> > copied;
     830       467920 :   auto_vec<class loop *> loops_to_unloop;
     831       467920 :   auto_vec<int> loops_to_unloop_nunroll;
     832              : 
     833       467920 :   mark_dfs_back_edges ();
     834       467920 :   gimple_ranger *ranger = new gimple_ranger;
     835      2642767 :   for (auto loop : loops_list (cfun, 0))
     836              :     {
     837      1239007 :       int initial_limit = optimize_loop_for_speed_p (loop)
     838      1239007 :                           ? param_max_loop_header_insns : 0;
     839      1239007 :       int remaining_limit = initial_limit;
     840      1239007 :       if (dump_file && (dump_flags & TDF_DETAILS))
     841           22 :         fprintf (dump_file,
     842              :                  "Analyzing loop %i\n", loop->num);
     843              : 
     844              :       /* If the loop is already a do-while style one (either because it was
     845              :          written as such, or because jump threading transformed it into one),
     846              :          we might be in fact peeling the first iteration of the loop.  This
     847              :          in general is not a good idea.  Also avoid touching infinite loops.  */
     848      1239007 :       if (!loop_has_exit_edges (loop)
     849      1239007 :           || !process_loop_p (loop))
     850       487256 :         continue;
     851              : 
     852       751799 :       basic_block header = loop->header;
     853       751799 :       estimate_numbers_of_iterations (loop);
     854       751799 :       if (!get_max_loop_iterations_int (loop))
     855              :         {
     856           48 :           if (dump_file && (dump_flags & TDF_DETAILS))
     857            0 :             fprintf (dump_file, "Loop %d never loops.\n", loop->num);
     858           48 :           scale_loop_profile (loop, profile_probability::always (), 0);
     859           48 :           loops_to_unloop.safe_push (loop);
     860           48 :           loops_to_unloop_nunroll.safe_push (0);
     861           48 :           continue;
     862              :         }
     863              : 
     864              :       /* Iterate the header copying up to limit; this takes care of the cases
     865              :          like while (a && b) {...}, where we want to have both of the conditions
     866              :          copied.  TODO -- handle while (a || b) - like cases, by not requiring
     867              :          the header to have just a single successor and copying up to
     868              :          postdominator.  */
     869       751751 :       unsigned int nheaders = 0;
     870       751751 :       unsigned int last_win_nheaders = 0;
     871       751751 :       bool last_win_invariant_exit = false;
     872       751751 :       ch_decision ret;
     873       751751 :       auto_vec <ch_decision, 32> decision;
     874       751751 :       hash_set <edge> *invariant_exits = new hash_set <edge>;
     875       751751 :       hash_set <edge> *static_exits = new hash_set <edge>;
     876       751751 :       bool canbe_neverexecuted = true;
     877       751751 :       while ((ret = should_duplicate_loop_header_p (header, loop, ranger,
     878              :                                                     &remaining_limit,
     879              :                                                     invariant_exits,
     880              :                                                     static_exits,
     881              :                                                     &canbe_neverexecuted))
     882      1523100 :              != ch_impossible)
     883              :         {
     884       771349 :           nheaders++;
     885       771349 :           decision.safe_push (ret);
     886       771349 :           if (ret >= ch_win)
     887              :             {
     888        12247 :               last_win_nheaders = nheaders;
     889        12247 :               last_win_invariant_exit = (ret == ch_win_invariant_exit);
     890        12247 :               if (dump_file && (dump_flags & TDF_DETAILS))
     891           10 :                 fprintf (dump_file, "    Duplicating bb %i is a win\n",
     892              :                          header->index);
     893              :             }
     894              :           else
     895       759102 :             if (dump_file && (dump_flags & TDF_DETAILS))
     896           25 :               fprintf (dump_file, "    May duplicate bb %i\n", header->index);
     897              : 
     898              :           /* Find a successor of header that is inside a loop; i.e. the new
     899              :              header after the condition is copied.  */
     900       771349 :           if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest))
     901       521859 :             header = EDGE_SUCC (header, 0)->dest;
     902              :           else
     903       249490 :             header = EDGE_SUCC (header, 1)->dest;
     904              :         }
     905              : 
     906              :       /* Try to turn loop into do-while.  This means ensuring that
     907              :          last duplicated header will not have loop invariant exit.  */
     908       751751 :       if (last_win_nheaders && last_win_invariant_exit
     909        10377 :           && nheaders > last_win_nheaders)
     910              :         {
     911         2873 :           last_win_nheaders++;
     912         2873 :           if (dump_file && (dump_flags & TDF_DETAILS))
     913            6 :             fprintf (dump_file,
     914              :                      "    Duplicating additional BB to obtain do-while loop\n");
     915              :         }
     916       748878 :       else if (!last_win_nheaders && nheaders && !do_while_loop_p (loop))
     917              :         {
     918       574069 :           last_win_nheaders = 1;
     919       574069 :           if (dump_file && (dump_flags & TDF_DETAILS))
     920           12 :             fprintf (dump_file,
     921              :                      "    Duplicating header BB to obtain do-while loop\n");
     922              :         }
     923              :       /* "Duplicate" all BBs with zero cost following last basic blocks we
     924              :          decided to copy.  */
     925      1511782 :       while (last_win_nheaders < decision.length ()
     926       898427 :              && decision[last_win_nheaders] == ch_possible_zero_cost)
     927              :         {
     928         8280 :           if (dump_file && (dump_flags & TDF_DETAILS))
     929            0 :             fprintf (dump_file,
     930              :                      "    Duplicating extra bb is a win; it has zero cost\n");
     931         8280 :           last_win_nheaders++;
     932              :         }
     933              : 
     934       751751 :       if (last_win_nheaders)
     935       589542 :         candidates.safe_push ({loop, last_win_nheaders,
     936              :                               invariant_exits, static_exits});
     937              :       else
     938              :         {
     939       162209 :           delete invariant_exits;
     940       162209 :           delete static_exits;
     941              :         }
     942       751751 :     }
     943              :   /* Do not use ranger after we change the IL and not have updated SSA.  */
     944       467920 :   delete ranger;
     945              : 
     946      1440964 :   for (auto candidate : candidates)
     947              :     {
     948       589542 :       class loop *loop = candidate.loop;
     949       589542 :       if (dump_file && (dump_flags & TDF_DETAILS))
     950           21 :         fprintf (dump_file,
     951              :                  "Copying headers of loop %i\n", loop->num);
     952              : 
     953       589542 :       basic_block header = loop->header;
     954       589542 :       edge nonexit = NULL;
     955       589542 :       edge exit = NULL;
     956       589542 :       unsigned int n_bbs = 0;
     957       589542 :       int nexits = 0;
     958       589542 :       profile_count exit_count = profile_count::zero ();
     959       589542 :       profile_count entry_count = profile_count::zero ();
     960       589542 :       edge e;
     961       589542 :       edge_iterator ei;
     962              : 
     963      1768626 :       FOR_EACH_EDGE (e, ei, loop->header->preds)
     964      1179084 :         if (e->src != loop->latch)
     965       589542 :           entry_count += e->count ();
     966      1188801 :       while (n_bbs < candidate.nheaders)
     967              :         {
     968       599259 :             if (dump_file && (dump_flags & TDF_DETAILS))
     969           31 :               fprintf (dump_file, "    Will duplicate bb %i\n", header->index);
     970              :           /* Find a successor of header that is inside a loop; i.e. the new
     971              :              header after the condition is copied.  */
     972       599259 :           if (flow_bb_inside_loop_p (loop, EDGE_SUCC (header, 0)->dest))
     973              :             {
     974       456491 :               nonexit = EDGE_SUCC (header, 0);
     975       456491 :               exit = EDGE_SUCC (header, 1);
     976              :             }
     977              :           else
     978              :             {
     979       142768 :               nonexit = EDGE_SUCC (header, 1);
     980       142768 :               exit = EDGE_SUCC (header, 0);
     981              :             }
     982       599259 :           exit_count += exit->count ();
     983       599259 :           nexits++;
     984       599259 :           bbs[n_bbs++] = header;
     985       599259 :           gcc_assert (bbs_size > n_bbs);
     986       599259 :           header = nonexit->dest;
     987              :         }
     988              : 
     989       589542 :       if (dump_file && (dump_flags & TDF_DETAILS))
     990           21 :         fprintf (dump_file,
     991              :                  "Duplicating header of the loop %d up to edge %d->%d\n",
     992           21 :                  loop->num, exit->src->index, exit->dest->index);
     993              : 
     994              :       /* Ensure that the header will have just the latch as a predecessor
     995              :          inside the loop.  */
     996       589542 :       if (!single_pred_p (nonexit->dest))
     997              :         {
     998           17 :           header = split_edge (nonexit);
     999           17 :           exit = single_pred_edge (header);
    1000              :         }
    1001              : 
    1002       589542 :       edge entry = loop_preheader_edge (loop);
    1003              : 
    1004       589542 :       propagate_threaded_block_debug_into (nonexit->dest, entry->dest);
    1005       589542 :       if (!gimple_duplicate_seme_region (entry, exit, bbs, n_bbs, copied_bbs,
    1006              :                                          true))
    1007              :         {
    1008            0 :           delete candidate.static_exits;
    1009            0 :           delete candidate.invariant_exits;
    1010            0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1011            0 :             fprintf (dump_file, "Duplication failed.\n");
    1012            0 :           continue;
    1013              :         }
    1014       589542 :       if (loop->header->count.initialized_p ())
    1015       589419 :         update_profile_after_ch (loop, bbs, copied_bbs, n_bbs,
    1016              :                                  candidate.invariant_exits,
    1017              :                                  candidate.static_exits,
    1018              :                                  entry_count);
    1019      1179084 :       delete candidate.static_exits;
    1020      1179084 :       delete candidate.invariant_exits;
    1021       589542 :       copied.safe_push (std::make_pair (entry, loop));
    1022              : 
    1023              :       /* Update header of the loop.  */
    1024       589542 :       loop->header = header;
    1025              :       /* Find correct latch.  We only duplicate chain of conditionals so
    1026              :          there should be precisely two edges to the new header.  One entry
    1027              :          edge and one to latch.  */
    1028       589542 :       FOR_EACH_EDGE (e, ei, loop->header->preds)
    1029       589542 :         if (header != e->src)
    1030              :           {
    1031       589542 :             loop->latch = e->src;
    1032       589542 :             break;
    1033              :           }
    1034              :       /* Ensure that the latch is simple.  */
    1035       589542 :       if (!single_succ_p (loop_latch_edge (loop)->src))
    1036       589542 :         split_edge (loop_latch_edge (loop));
    1037              : 
    1038       589542 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1039              :         {
    1040           21 :           if (do_while_loop_p (loop))
    1041           20 :             fprintf (dump_file, "Loop %d is now do-while loop.\n", loop->num);
    1042              :           else
    1043            1 :             fprintf (dump_file, "Loop %d is still not do-while loop.\n",
    1044              :                      loop->num);
    1045           21 :           fprintf (dump_file, "Exit count: ");
    1046           21 :           exit_count.dump (dump_file);
    1047           21 :           fprintf (dump_file, "\nEntry count: ");
    1048           21 :           entry_count.dump (dump_file);
    1049           21 :           fprintf (dump_file, "\n");
    1050              :         }
    1051              : 
    1052              :       /* We possibly decreased number of iterations by 1.  */
    1053       589542 :       auto_vec<edge> exits = get_loop_exit_edges (loop);
    1054       589542 :       bool precise = (nexits == (int) exits.length ());
    1055              :       /* Check that loop may not terminate in other way than via
    1056              :          basic blocks we duplicated.  */
    1057       589542 :       if (precise)
    1058              :         {
    1059       397506 :           basic_block *bbs = get_loop_body (loop);
    1060      2378013 :           for (unsigned i = 0; i < loop->num_nodes && precise; ++i)
    1061              :            {
    1062      1583033 :              basic_block bb = bbs[i];
    1063      1583033 :              bool found_exit = false;
    1064      3313944 :              FOR_EACH_EDGE (e, ei, bb->succs)
    1065      2080915 :               if (!flow_bb_inside_loop_p (loop, e->dest))
    1066              :                 {
    1067              :                   found_exit = true;
    1068              :                   break;
    1069              :                 }
    1070              :              /* If BB has exit, it was duplicated.  */
    1071      1583033 :              if (found_exit)
    1072       350004 :                continue;
    1073              :              /* Give up on irreducible loops.  */
    1074      1233029 :              if (bb->flags & BB_IRREDUCIBLE_LOOP)
    1075              :                {
    1076              :                  precise = false;
    1077              :                  break;
    1078              :                }
    1079              :              /* Check that inner loops are finite.  */
    1080      1574935 :              for (class loop *l = bb->loop_father; l != loop && precise;
    1081       341938 :                   l = loop_outer (l))
    1082       352320 :                if (!l->finite_p)
    1083              :                  {
    1084              :                    precise = false;
    1085              :                    break;
    1086              :                  }
    1087              :              /* Verify that there is no statement that may be terminate
    1088              :                 execution in a way not visible to CFG.  */
    1089      2465994 :              for (gimple_stmt_iterator bsi = gsi_start_bb (bb);
    1090      9094097 :                   !gsi_end_p (bsi); gsi_next (&bsi))
    1091      7861100 :                if (stmt_can_terminate_bb_p (gsi_stmt (bsi)))
    1092       118147 :                  precise = false;
    1093              :            }
    1094       397506 :           free (bbs);
    1095              :         }
    1096       589542 :       if (precise
    1097       589542 :           && get_max_loop_iterations_int (loop) == 1)
    1098              :         {
    1099         2953 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1100            0 :             fprintf (dump_file, "Loop %d no longer loops.\n", loop->num);
    1101         2953 :           scale_loop_profile (loop, profile_probability::always (), 0);
    1102         2953 :           loops_to_unloop.safe_push (loop);
    1103         2953 :           loops_to_unloop_nunroll.safe_push (0);
    1104              :         }
    1105       586589 :       else if (precise)
    1106              :         {
    1107       311436 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1108           11 :             fprintf (dump_file,
    1109              :                      "Peeled all exits:"
    1110              :                      " decreased number of iterations of loop %d by 1.\n",
    1111              :                      loop->num);
    1112       311436 :           adjust_loop_info_after_peeling (loop, 1, true);
    1113              :         }
    1114       275153 :       else if (exit_count >= entry_count.apply_scale (9, 10))
    1115              :         {
    1116       160349 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1117            6 :             fprintf (dump_file,
    1118              :                      "Peeled likely exits: likely decreased number "
    1119              :                      "of iterations of loop %d by 1.\n", loop->num);
    1120       160349 :           adjust_loop_info_after_peeling (loop, 1, false);
    1121              :         }
    1122       114804 :       else if (dump_file && (dump_flags & TDF_DETAILS))
    1123            4 :         fprintf (dump_file,
    1124              :                  "Not decreased number"
    1125              :                  " of iterations of loop %d; likely exits remains.\n",
    1126              :                  loop->num);
    1127              : 
    1128       589542 :       changed = true;
    1129       589542 :     }
    1130              : 
    1131       467920 :   if (changed)
    1132              :     {
    1133       191751 :       update_ssa (TODO_update_ssa);
    1134              :       /* After updating SSA form perform CSE on the loop header
    1135              :          copies.  This is esp. required for the pass before
    1136              :          vectorization since nothing cleans up copied exit tests
    1137              :          that can now be simplified.  CSE from the entry of the
    1138              :          region we copied till all loop exit blocks but not
    1139              :          entering the loop itself.  */
    1140       973044 :       for (unsigned i = 0; i < copied.length (); ++i)
    1141              :         {
    1142       589542 :           edge entry = copied[i].first;
    1143       589542 :           loop_p loop = copied[i].second;
    1144       589542 :           auto_vec<edge> exit_edges = get_loop_exit_edges (loop);
    1145       589542 :           bitmap exit_bbs = BITMAP_ALLOC (NULL);
    1146      2243725 :           for (unsigned j = 0; j < exit_edges.length (); ++j)
    1147      1064641 :             bitmap_set_bit (exit_bbs, exit_edges[j]->dest->index);
    1148       589542 :           bitmap_set_bit (exit_bbs, loop->header->index);
    1149       589542 :           do_rpo_vn (cfun, entry, exit_bbs);
    1150       589542 :           BITMAP_FREE (exit_bbs);
    1151       589542 :         }
    1152              :     }
    1153       467920 :   if (!loops_to_unloop.is_empty ())
    1154              :     {
    1155              :       /* Make sure loops are ordered inner to outer for unlooping.  */
    1156          880 :       if (loops_to_unloop.length () != 1)
    1157              :         {
    1158          382 :           auto_vec<int, 8> order;
    1159          764 :           order.safe_grow (number_of_loops (cfun), true);
    1160          382 :           int i = 0;
    1161        11040 :           for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
    1162         9894 :             order[loop->num] = i++;
    1163         1146 :           loops_to_unloop.sort (ch_order_loops, order.address ());
    1164          382 :         }
    1165          880 :       bool irred_invalidated;
    1166          880 :       auto_bitmap lc_invalidated;
    1167          880 :       auto_vec<edge> edges_to_remove;
    1168          880 :       unloop_loops (loops_to_unloop, loops_to_unloop_nunroll, edges_to_remove,
    1169              :                     lc_invalidated, &irred_invalidated);
    1170          880 :       if (loops_state_satisfies_p (fun, LOOP_CLOSED_SSA)
    1171          880 :           && !bitmap_empty_p (lc_invalidated))
    1172           18 :         rewrite_into_loop_closed_ssa (NULL, 0);
    1173          880 :       changed = true;
    1174          880 :     }
    1175       467920 :   free (bbs);
    1176       467920 :   free (copied_bbs);
    1177              : 
    1178       467920 :   return changed ? TODO_cleanup_cfg : 0;
    1179       467920 : }
    1180              : 
    1181              : /* Initialize the loop structures we need, and finalize after.  */
    1182              : 
    1183              : unsigned int
    1184      1069055 : pass_ch::execute (function *fun)
    1185              : {
    1186      1069055 :   loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
    1187      1069055 :   scev_initialize ();
    1188              : 
    1189      1069055 :   unsigned int res = copy_headers (fun);
    1190              : 
    1191      1069055 :   scev_finalize ();
    1192      1069055 :   loop_optimizer_finalize ();
    1193      1069055 :   return res;
    1194              : }
    1195              : 
    1196              : /* Assume an earlier phase has already initialized all the loop structures that
    1197              :    we need here (and perhaps others too), and that these will be finalized by
    1198              :    a later phase.  */
    1199              : 
    1200              : unsigned int
    1201       216896 : pass_ch_vect::execute (function *fun)
    1202              : {
    1203       216896 :   return copy_headers (fun);
    1204              : }
    1205              : 
    1206              : /* Apply header copying according to a very simple test of do-while shape.  */
    1207              : 
    1208              : bool
    1209       707549 : pass_ch::process_loop_p (class loop *)
    1210              : {
    1211       707549 :   return true;
    1212              : }
    1213              : 
    1214              : /* Apply header-copying to loops where we might enable vectorization.  */
    1215              : 
    1216              : bool
    1217       521508 : pass_ch_vect::process_loop_p (class loop *loop)
    1218              : {
    1219       521508 :   if (!flag_tree_loop_vectorize && !loop->force_vectorize)
    1220              :     return false;
    1221              : 
    1222       518573 :   if (loop->dont_vectorize)
    1223              :     return false;
    1224              : 
    1225              :   /* The vectorizer won't handle anything with multiple exits, so skip.  */
    1226       513757 :   edge exit = single_exit (loop);
    1227       513757 :   if (!exit)
    1228              :     return false;
    1229              : 
    1230       312984 :   if (!do_while_loop_p (loop))
    1231        44250 :     return true;
    1232              : 
    1233              :   return false;
    1234              : }
    1235              : 
    1236              : } // anon namespace
    1237              : 
    1238              : gimple_opt_pass *
    1239       302985 : make_pass_ch_vect (gcc::context *ctxt)
    1240              : {
    1241       302985 :   return new pass_ch_vect (ctxt);
    1242              : }
    1243              : 
    1244              : gimple_opt_pass *
    1245       302985 : make_pass_ch (gcc::context *ctxt)
    1246              : {
    1247       302985 :   return new pass_ch (ctxt);
    1248              : }
        

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.