LCOV - code coverage report
Current view: top level - gcc - tree-vrp.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 86.1 % 575 495
Test Date: 2026-12-05 16:10:02 Functions: 95.1 % 41 39
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Support routines for Value Range Propagation (VRP).
       2              :    Copyright (C) 2005-2026 Free Software Foundation, Inc.
       3              :    Contributed by Diego Novillo <dnovillo@redhat.com>.
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify
       8              : it under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful,
      13              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15              : GNU General Public License 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              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "basic-block.h"
      25              : #include "bitmap.h"
      26              : #include "sbitmap.h"
      27              : #include "options.h"
      28              : #include "dominance.h"
      29              : #include "function.h"
      30              : #include "cfg.h"
      31              : #include "tree.h"
      32              : #include "gimple.h"
      33              : #include "tree-pass.h"
      34              : #include "ssa.h"
      35              : #include "gimple-pretty-print.h"
      36              : #include "fold-const.h"
      37              : #include "cfganal.h"
      38              : #include "gimple-iterator.h"
      39              : #include "tree-cfg.h"
      40              : #include "tree-ssa-loop-manip.h"
      41              : #include "tree-ssa-loop-niter.h"
      42              : #include "tree-into-ssa.h"
      43              : #include "cfgloop.h"
      44              : #include "tree-scalar-evolution.h"
      45              : #include "tree-ssa-propagate.h"
      46              : #include "domwalk.h"
      47              : #include "vr-values.h"
      48              : #include "gimple-array-bounds.h"
      49              : #include "gimple-range.h"
      50              : #include "gimple-range-path.h"
      51              : #include "gimple-fold.h"
      52              : #include "tree-dfa.h"
      53              : #include "tree-ssa-dce.h"
      54              : #include "alloc-pool.h"
      55              : #include "cgraph.h"
      56              : #include "symbol-summary.h"
      57              : #include "ipa-utils.h"
      58              : #include "sreal.h"
      59              : #include "ipa-cp.h"
      60              : #include "ipa-prop.h"
      61              : #include "attribs.h"
      62              : #include "diagnostic-core.h"
      63              : 
      64              : // This class is utilized by VRP and ranger to remove __builtin_unreachable
      65              : // calls, and reflect any resulting global ranges.
      66              : //
      67              : // maybe_register() is called on condition statements , and if that
      68              : // matches the pattern of one branch being a builtin_unreachable, either check
      69              : // for early removal or register the resulting executable edge in a list.
      70              : //
      71              : // During early/non-final processing, we check to see if ALL exports from the
      72              : // block can be safely updated with a new global value.  If they can, then
      73              : // we rewrite the condition and update those values immediately.  Otherwise
      74              : // the unreachable condition is left in the IL until the final pass.
      75              : //
      76              : // During final processing, after all blocks have been registered,
      77              : // remove_and_update_globals() will
      78              : // - check all exports from registered blocks
      79              : // - ensure the cache entry of each export is set with the appropriate range
      80              : // - rewrite the conditions to take the executable edge
      81              : // - perform DCE on any feeding instructions to those rewritten conditions
      82              : //
      83              : // Then each of the immediate use chain of each export is walked, and a new
      84              : // global range created by unioning the ranges at all remaining use locations.
      85              : 
      86              : class remove_unreachable {
      87              : public:
      88      4390633 :   remove_unreachable (range_query &r, bool all) : m_ranger (r), final_p (all)
      89      4390633 :     { m_list.create (30); m_tmp = BITMAP_ALLOC (NULL); }
      90      4390630 :   ~remove_unreachable () { BITMAP_FREE (m_tmp); m_list.release (); }
      91              :   void handle_early (gimple *s, edge e);
      92              :   void maybe_register (gimple *s);
      93              :   bool remove ();
      94              :   bool remove_and_update_globals ();
      95              :   bool fully_replaceable (tree name, basic_block bb);
      96              :   vec<edge> m_list;
      97              :   range_query &m_ranger;
      98              :   bool final_p;
      99              :   bitmap m_tmp;
     100              : };
     101              : 
     102              : // Check if block BB has a __builtin_unreachable () call on one arm, and
     103              : // register the executable edge if so.
     104              : 
     105              : void
     106      8692684 : remove_unreachable::maybe_register (gimple *s)
     107              : {
     108      8692684 :   gcc_checking_assert  (gimple_code (s) == GIMPLE_COND);
     109      8692684 :   basic_block bb = gimple_bb (s);
     110              : 
     111      8692684 :   edge e0 = EDGE_SUCC (bb, 0);
     112      8692684 :   basic_block bb0 = e0->dest;
     113      9963760 :   bool un0 = EDGE_COUNT (bb0->succs) == 0
     114      9963760 :              && gimple_seq_unreachable_p (bb_seq (bb0));
     115      8692684 :   edge e1 = EDGE_SUCC (bb, 1);
     116      8692684 :   basic_block bb1 = e1->dest;
     117      9007616 :   bool un1 = EDGE_COUNT (bb1->succs) == 0
     118      9007616 :              && gimple_seq_unreachable_p (bb_seq (bb1));
     119              : 
     120              :   // If the 2 blocks are not different, ignore.
     121      8692684 :   if (un0 == un1)
     122      8351804 :     return;
     123              : 
     124              :   // Constant expressions are ignored.
     125       340968 :   if (TREE_CODE (gimple_cond_lhs (s)) != SSA_NAME
     126       340968 :       && TREE_CODE (gimple_cond_rhs (s)) != SSA_NAME)
     127              :     return;
     128              : 
     129       340880 :   edge e = un0 ? e1 : e0;
     130              : 
     131       340880 :   if (!final_p)
     132       232137 :     handle_early (s, e);
     133              :   else
     134       108743 :     m_list.safe_push (e);
     135              : }
     136              : 
     137              : // Return true if all uses of NAME are dominated by block BB.  1 use
     138              : // is allowed in block BB, This is one we hope to remove.
     139              : // ie
     140              : //  _2 = _1 & 7;
     141              : //  if (_2 != 0)
     142              : //    goto <bb 3>; [0.00%]
     143              : //  Any additional use of _1 or _2 in this block invalidates early replacement.
     144              : 
     145              : bool
     146       233697 : remove_unreachable::fully_replaceable (tree name, basic_block bb)
     147              : {
     148       233697 :   use_operand_p use_p;
     149       233697 :   imm_use_iterator iter;
     150       233697 :   bool saw_in_bb = false;
     151              : 
     152              :   // If a name loads from memory, we may lose information used in
     153              :   // commoning opportunities later.  See tree-ssa/ssa-pre-34.c.
     154       233697 :   gimple *def_stmt = SSA_NAME_DEF_STMT (name);
     155       463473 :   if (gimple_vuse (def_stmt))
     156              :     return false;
     157              : 
     158        37284 :   FOR_EACH_IMM_USE_FAST (use_p, iter, name)
     159              :     {
     160        32315 :       gimple *use_stmt = USE_STMT (use_p);
     161              :       // Ignore debug stmts and the branch.
     162        32315 :       if (is_gimple_debug (use_stmt))
     163        10383 :         continue;
     164        21932 :       basic_block use_bb = gimple_bb (use_stmt);
     165              :       // Only one use in the block allowed to avoid complicated cases.
     166        21932 :       if (use_bb == bb)
     167              :         {
     168        10785 :           if (saw_in_bb)
     169              :             return false;
     170              :           else
     171              :             saw_in_bb = true;
     172              :         }
     173        11147 :       else if (!dominated_by_p (CDI_DOMINATORS, use_bb, bb))
     174              :         return false;
     175         4673 :     }
     176         4969 :   return true;
     177              : }
     178              : 
     179              : // This routine is called to check builtin_unreachable calls during any
     180              : // time before final removal.  The only way we can be sure it does not
     181              : // provide any additional information is to expect that we can update the
     182              : // global values of all exports from a block.   This means the branch
     183              : // to the unreachable call must dominate all uses of those ssa-names, with
     184              : // the exception that there can be a single use in the block containing
     185              : // the branch. IF the name used in the branch is defined in the block, it may
     186              : // contain the name of something else that will be an export.  And likewise
     187              : // that may also use another name that is an export etc.
     188              : //
     189              : // As long as there is only a single use, we can be sure that there are no other
     190              : // side effects (like being passed to a call, or stored to a global, etc.
     191              : // This means we will miss cases where there are 2 or more uses that have
     192              : // no interveneing statements that may had side effects, but it catches most
     193              : // of the cases we care about, and prevents expensive in depth analysis.
     194              : //
     195              : // Ranger will still reflect the proper ranges at other places in these missed
     196              : // cases, we simply will not remove/set globals early.
     197              : 
     198              : void
     199       232137 : remove_unreachable::handle_early (gimple *s, edge e)
     200              : {
     201              :   // If there is no gori_ssa, there is no early processing.
     202       232137 :   if (!m_ranger.gori_ssa ())
     203       230404 :     return ;
     204       232137 :   bool lhs_p = TREE_CODE (gimple_cond_lhs (s)) == SSA_NAME;
     205       232137 :   bool rhs_p = TREE_CODE (gimple_cond_rhs (s)) == SSA_NAME;
     206              :   // Do not remove __builtin_unreachable if it confers a relation, or
     207              :   // that relation may be lost in subsequent passes.
     208       232137 :   if (lhs_p && rhs_p)
     209              :     return;
     210              :   // Do not remove addresses early. ie if (x == &y)
     211       230825 :   if (lhs_p && TREE_CODE (gimple_cond_rhs (s)) == ADDR_EXPR)
     212              :     return;
     213              : 
     214       230461 :   gcc_checking_assert (gimple_outgoing_range_stmt_p (e->src) == s);
     215       230461 :   gcc_checking_assert (!final_p);
     216              : 
     217              :   // Check if every export and its dependencies are dominated by this branch.
     218              :   // Dependencies are required as it needs to dominate potential
     219              :   // recalculations.  See PR 123300.
     220       230461 :   tree name;
     221       235430 :   FOR_EACH_GORI_EXPORT_AND_DEP_NAME (m_ranger.gori_ssa (), e->src, name, m_tmp)
     222              :     {
     223       233697 :       if (!fully_replaceable (name, e->src))
     224       228728 :         return;
     225              :     }
     226              : 
     227              :   // Exporting a global range due to a __builtin_unreachable may cause the
     228              :   // next range query in the export list to terminate in GORI earlier than
     229              :   // desired.  The entire point is to bypass the need for the unreachable by
     230              :   // setting globals, but we need to make sure all the ranges are correct first.
     231              :   // Save the ranges as they are caluculated and export them at once.
     232              : 
     233         1733 :   struct range_pair
     234              :   {
     235              :     tree name;
     236              :     vrange_storage *range;
     237              :   };
     238         1733 :   vrange_allocator allocator;
     239         1733 :   auto_vec<range_pair, 8> ranges;
     240              : 
     241         1733 :   value_range r;
     242         3942 :   FOR_EACH_GORI_EXPORT_NAME (m_ranger.gori_ssa (), e->src, name)
     243              :     {
     244         2209 :       r.set_range_class (TREE_TYPE (name));
     245         2209 :       m_ranger.range_on_entry (r, e->dest, name);
     246         2209 :       vrange_storage *p = allocator.clone (r);
     247         2209 :       ranges.safe_push ( {name, p} );
     248              :     }
     249              : 
     250         3942 :   for (unsigned i = 0; i < ranges.length (); ++i)
     251              :     {
     252         2209 :       tree name = ranges[i].name;
     253         2209 :       tree type = TREE_TYPE (name);
     254         2209 :       r.set_range_class (type);
     255         2209 :       ranges[i].range->get_vrange (r, type);
     256              :       // Nothing at this late stage we can do if the write fails.
     257         2209 :       set_range_info (name, r);
     258              :     }
     259              : 
     260         1733 :   tree ssa = lhs_p ? gimple_cond_lhs (s) : gimple_cond_rhs (s);
     261              : 
     262              :   // Rewrite the condition.
     263         1733 :   if (e->flags & EDGE_TRUE_VALUE)
     264          368 :     gimple_cond_make_true (as_a<gcond *> (s));
     265              :   else
     266         1365 :     gimple_cond_make_false (as_a<gcond *> (s));
     267         1733 :   update_stmt (s);
     268              : 
     269              :   // If the name on S is defined in this block, see if there is DCE work to do.
     270         1733 :   if (gimple_bb (SSA_NAME_DEF_STMT (ssa)) == e->src)
     271              :     {
     272          525 :       auto_bitmap dce;
     273          525 :       bitmap_set_bit (dce, SSA_NAME_VERSION (ssa));
     274          525 :       simple_dce_from_worklist (dce, nullptr, true);
     275          525 :     }
     276         1733 : }
     277              : 
     278              : // Process the edges in the list, change the conditions and removing any
     279              : // dead code feeding those conditions.   This removes the unreachables, but
     280              : // makes no attempt to set globals values.
     281              : 
     282              : bool
     283            3 : remove_unreachable::remove ()
     284              : {
     285            3 :   if (!final_p || m_list.length () == 0)
     286              :     return false;
     287              : 
     288              :   bool change = false;
     289              :   unsigned i;
     290            0 :   for (i = 0; i < m_list.length (); i++)
     291              :     {
     292            0 :       edge e = m_list[i];
     293            0 :       gimple *s = gimple_outgoing_range_stmt_p (e->src);
     294            0 :       gcc_checking_assert (gimple_code (s) == GIMPLE_COND);
     295              : 
     296            0 :       tree name = gimple_range_ssa_p (gimple_cond_lhs (s));
     297            0 :       if (!name)
     298            0 :         name = gimple_range_ssa_p (gimple_cond_rhs (s));
     299              :       // Check if global value can be set for NAME.
     300            0 :       if (name && fully_replaceable (name, e->src))
     301              :         {
     302            0 :           value_range r (TREE_TYPE (name));
     303            0 :           if (gori_name_on_edge (r, name, e, &m_ranger))
     304            0 :             set_range_info (name, r);
     305            0 :         }
     306              : 
     307            0 :       change = true;
     308              :       // Rewrite the condition.
     309            0 :       if (e->flags & EDGE_TRUE_VALUE)
     310            0 :         gimple_cond_make_true (as_a<gcond *> (s));
     311              :       else
     312            0 :         gimple_cond_make_false (as_a<gcond *> (s));
     313            0 :       update_stmt (s);
     314              :     }
     315              : 
     316              :   return change;
     317              : }
     318              : 
     319              : 
     320              : // Process the edges in the list, change the conditions and removing any
     321              : // dead code feeding those conditions.  Calculate the range of any
     322              : // names that may have been exported from those blocks, and determine if
     323              : // there is any updates to their global ranges..
     324              : // Return true if any builtin_unreachables/globals eliminated/updated.
     325              : 
     326              : bool
     327      4390630 : remove_unreachable::remove_and_update_globals ()
     328              : {
     329      4390630 :   if (m_list.length () == 0)
     330              :     return false;
     331              : 
     332              :   // If there is no import/export info, Do basic removal.
     333        29067 :   if (!m_ranger.gori_ssa ())
     334            0 :     return remove ();
     335              : 
     336        29067 :   bool change = false;
     337        29067 :   tree name;
     338        29067 :   unsigned i;
     339        29067 :   bitmap_iterator bi;
     340        29067 :   auto_bitmap all_exports;
     341       137810 :   for (i = 0; i < m_list.length (); i++)
     342              :     {
     343       108743 :       edge e = m_list[i];
     344       108743 :       gimple *s = gimple_outgoing_range_stmt_p (e->src);
     345       108743 :       gcc_checking_assert (gimple_code (s) == GIMPLE_COND);
     346              : 
     347       108743 :       bool dominate_exit_p = true;
     348       262662 :       FOR_EACH_GORI_EXPORT_NAME (m_ranger.gori_ssa (), e->src, name)
     349              :         {
     350              :           // Ensure the cache is set for NAME in the succ block.
     351       153919 :           value_range r(TREE_TYPE (name));
     352       153919 :           value_range ex(TREE_TYPE (name));
     353       153919 :           m_ranger.range_on_entry (r, e->dest, name);
     354       153919 :           m_ranger.range_on_entry (ex, EXIT_BLOCK_PTR_FOR_FN (cfun), name);
     355              :           // If the range produced by this __builtin_unreachacble expression
     356              :           // is not fully reflected in the range at exit, then it does not
     357              :           // dominate the exit of the function.
     358       153919 :           if (ex.intersect (r))
     359        34762 :             dominate_exit_p = false;
     360       153919 :         }
     361              : 
     362              :       // If the exit is dominated, add to the export list.  Otherwise if this
     363              :       // isn't the final VRP pass, leave the call in the IL.
     364       108743 :       if (dominate_exit_p)
     365        78404 :         bitmap_ior_into (all_exports,
     366        78404 :                          m_ranger.gori_ssa ()->exports (e->src));
     367        30339 :       else if (!final_p)
     368            0 :         continue;
     369              : 
     370       108743 :       change = true;
     371              :       // Rewrite the condition.
     372       108743 :       if (e->flags & EDGE_TRUE_VALUE)
     373         3210 :         gimple_cond_make_true (as_a<gcond *> (s));
     374              :       else
     375       105533 :         gimple_cond_make_false (as_a<gcond *> (s));
     376       108743 :       update_stmt (s);
     377              :     }
     378              : 
     379        29067 :   if (bitmap_empty_p (all_exports))
     380              :     return false;
     381              :   // Invoke DCE on all exported names to eliminate dead feeding defs.
     382        23621 :   auto_bitmap dce;
     383        23621 :   bitmap_copy (dce, all_exports);
     384              :   // Don't attempt to DCE parameters.
     385       122457 :   EXECUTE_IF_SET_IN_BITMAP (all_exports, 0, i, bi)
     386        98836 :     if (!ssa_name (i) || SSA_NAME_IS_DEFAULT_DEF (ssa_name (i)))
     387         1035 :       bitmap_clear_bit (dce, i);
     388        23621 :   simple_dce_from_worklist (dce);
     389              : 
     390              :   // Loop over all uses of each name and find maximal range. This is the
     391              :   // new global range.
     392        23621 :   use_operand_p use_p;
     393        23621 :   imm_use_iterator iter;
     394       122457 :   EXECUTE_IF_SET_IN_BITMAP (all_exports, 0, i, bi)
     395              :     {
     396        98836 :       name = ssa_name (i);
     397       139487 :       if (!name || SSA_NAME_IN_FREE_LIST (name))
     398        97936 :         continue;
     399        40651 :       value_range r (TREE_TYPE (name));
     400        40651 :       value_range exp_range (TREE_TYPE (name));
     401        40651 :       r.set_undefined ();
     402       169938 :       FOR_EACH_IMM_USE_FAST (use_p, iter, name)
     403              :         {
     404       154658 :           gimple *use_stmt = USE_STMT (use_p);
     405       154658 :           if (is_gimple_debug (use_stmt))
     406        63527 :             continue;
     407        91131 :           if (!m_ranger.range_of_expr (exp_range, name, use_stmt))
     408            0 :             exp_range.set_varying (TREE_TYPE (name));
     409        91131 :           r.union_ (exp_range);
     410        91131 :           if (r.varying_p ())
     411              :             break;
     412        40651 :         }
     413              :       // Include the on-exit range to ensure non-dominated unreachables
     414              :       // don't incorrectly impact the global range.
     415        40651 :       m_ranger.range_on_entry (exp_range, EXIT_BLOCK_PTR_FOR_FN (cfun), name);
     416        40651 :       r.union_ (exp_range);
     417        40651 :       if (r.varying_p () || r.undefined_p ())
     418        25410 :         continue;
     419        15241 :       if (!set_range_info (name, r))
     420        14341 :         continue;
     421          900 :       change = true;
     422        40651 :     }
     423        23621 :   return change;
     424        52688 : }
     425              : 
     426              : /* VR_TYPE describes a range with minimum value *MIN and maximum
     427              :    value *MAX.  Restrict the range to the set of values that have
     428              :    no bits set outside NONZERO_BITS.  Update *MIN and *MAX and
     429              :    return the new range type.
     430              : 
     431              :    SGN gives the sign of the values described by the range.  */
     432              : 
     433              : enum value_range_kind
     434     18760658 : intersect_range_with_nonzero_bits (enum value_range_kind vr_type,
     435              :                                    wide_int *min, wide_int *max,
     436              :                                    const wide_int &nonzero_bits,
     437              :                                    signop sgn)
     438              : {
     439     18760658 :   if (vr_type == VR_ANTI_RANGE)
     440              :     {
     441              :       /* The VR_ANTI_RANGE is equivalent to the union of the ranges
     442              :          A: [-INF, *MIN) and B: (*MAX, +INF].  First use NONZERO_BITS
     443              :          to create an inclusive upper bound for A and an inclusive lower
     444              :          bound for B.  */
     445       537017 :       wide_int a_max = wi::round_down_for_mask (*min - 1, nonzero_bits);
     446       537017 :       wide_int b_min = wi::round_up_for_mask (*max + 1, nonzero_bits);
     447              : 
     448              :       /* If the calculation of A_MAX wrapped, A is effectively empty
     449              :          and A_MAX is the highest value that satisfies NONZERO_BITS.
     450              :          Likewise if the calculation of B_MIN wrapped, B is effectively
     451              :          empty and B_MIN is the lowest value that satisfies NONZERO_BITS.  */
     452       537017 :       bool a_empty = wi::ge_p (a_max, *min, sgn);
     453       537017 :       bool b_empty = wi::le_p (b_min, *max, sgn);
     454              : 
     455              :       /* If both A and B are empty, there are no valid values.  */
     456       537017 :       if (a_empty && b_empty)
     457              :         return VR_UNDEFINED;
     458              : 
     459              :       /* If exactly one of A or B is empty, return a VR_RANGE for the
     460              :          other one.  */
     461       537017 :       if (a_empty || b_empty)
     462              :         {
     463            0 :           *min = b_min;
     464            0 :           *max = a_max;
     465            0 :           gcc_checking_assert (wi::le_p (*min, *max, sgn));
     466              :           return VR_RANGE;
     467              :         }
     468              : 
     469              :       /* Update the VR_ANTI_RANGE bounds.  */
     470       537017 :       *min = a_max + 1;
     471       537017 :       *max = b_min - 1;
     472       537017 :       gcc_checking_assert (wi::le_p (*min, *max, sgn));
     473              : 
     474              :       /* Now check whether the excluded range includes any values that
     475              :          satisfy NONZERO_BITS.  If not, switch to a full VR_RANGE.  */
     476       537017 :       if (wi::round_up_for_mask (*min, nonzero_bits) == b_min)
     477              :         {
     478            0 :           unsigned int precision = min->get_precision ();
     479            0 :           *min = wi::min_value (precision, sgn);
     480            0 :           *max = wi::max_value (precision, sgn);
     481            0 :           vr_type = VR_RANGE;
     482              :         }
     483       537017 :     }
     484     18760658 :   if (vr_type == VR_RANGE || vr_type == VR_VARYING)
     485              :     {
     486     18223641 :       *max = wi::round_down_for_mask (*max, nonzero_bits);
     487              : 
     488              :       /* Check that the range contains at least one valid value.  */
     489     18223641 :       if (wi::gt_p (*min, *max, sgn))
     490              :         return VR_UNDEFINED;
     491              : 
     492     18223641 :       *min = wi::round_up_for_mask (*min, nonzero_bits);
     493     18223641 :       gcc_checking_assert (wi::le_p (*min, *max, sgn));
     494              :     }
     495              :   return vr_type;
     496              : }
     497              : 
     498              : /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
     499              :    otherwise.  We only handle additive operations and set NEG to true if the
     500              :    symbol is negated and INV to the invariant part, if any.  */
     501              : 
     502              : static tree
     503      5389130 : get_single_symbol (tree t, bool *neg, tree *inv)
     504              : {
     505      5389130 :   bool neg_;
     506      5389130 :   tree inv_;
     507              : 
     508      5389130 :   *inv = NULL_TREE;
     509      5389130 :   *neg = false;
     510              : 
     511      5389130 :   if (TREE_CODE (t) == PLUS_EXPR
     512      5389130 :       || TREE_CODE (t) == POINTER_PLUS_EXPR
     513      5389130 :       || TREE_CODE (t) == MINUS_EXPR)
     514              :     {
     515            0 :       if (is_gimple_min_invariant (TREE_OPERAND (t, 0)))
     516              :         {
     517            0 :           neg_ = (TREE_CODE (t) == MINUS_EXPR);
     518            0 :           inv_ = TREE_OPERAND (t, 0);
     519            0 :           t = TREE_OPERAND (t, 1);
     520              :         }
     521            0 :       else if (is_gimple_min_invariant (TREE_OPERAND (t, 1)))
     522              :         {
     523            0 :           neg_ = false;
     524            0 :           inv_ = TREE_OPERAND (t, 1);
     525            0 :           t = TREE_OPERAND (t, 0);
     526              :         }
     527              :       else
     528              :         return NULL_TREE;
     529              :     }
     530              :   else
     531              :     {
     532              :       neg_ = false;
     533              :       inv_ = NULL_TREE;
     534              :     }
     535              : 
     536      5389130 :   if (TREE_CODE (t) == NEGATE_EXPR)
     537              :     {
     538            0 :       t = TREE_OPERAND (t, 0);
     539            0 :       neg_ = !neg_;
     540              :     }
     541              : 
     542      5389130 :   if (TREE_CODE (t) != SSA_NAME)
     543              :     return NULL_TREE;
     544              : 
     545            0 :   if (inv_ && TREE_OVERFLOW_P (inv_))
     546            0 :     inv_ = drop_tree_overflow (inv_);
     547              : 
     548            0 :   *neg = neg_;
     549            0 :   *inv = inv_;
     550            0 :   return t;
     551              : }
     552              : 
     553              : /* Compare two values VAL1 and VAL2.  Return
     554              : 
     555              :         -2 if VAL1 and VAL2 cannot be compared at compile-time,
     556              :         -1 if VAL1 < VAL2,
     557              :          0 if VAL1 == VAL2,
     558              :         +1 if VAL1 > VAL2, and
     559              :         +2 if VAL1 != VAL2
     560              : 
     561              :    This is similar to tree_int_cst_compare but supports pointer values
     562              :    and values that cannot be compared at compile time.  */
     563              : 
     564              : int
     565      3032826 : compare_values (tree val1, tree val2)
     566              : {
     567      3032826 :   if (val1 == val2)
     568              :     return 0;
     569              : 
     570              :   /* Below we rely on the fact that VAL1 and VAL2 are both pointers or
     571              :      both integers.  */
     572      2694565 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
     573              :               == POINTER_TYPE_P (TREE_TYPE (val2)));
     574              : 
     575              :   /* Convert the two values into the same type.  This is needed because
     576              :      sizetype causes sign extension even for unsigned types.  */
     577      2694565 :   if (!useless_type_conversion_p (TREE_TYPE (val1), TREE_TYPE (val2)))
     578            0 :     val2 = fold_convert (TREE_TYPE (val1), val2);
     579              : 
     580      2694565 :   const bool overflow_undefined
     581      5383063 :     = INTEGRAL_TYPE_P (TREE_TYPE (val1))
     582      5383063 :       && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
     583      2694565 :   tree inv1, inv2;
     584      2694565 :   bool neg1, neg2;
     585      2694565 :   tree sym1 = get_single_symbol (val1, &neg1, &inv1);
     586      2694565 :   tree sym2 = get_single_symbol (val2, &neg2, &inv2);
     587              : 
     588              :   /* If VAL1 and VAL2 are of the form '[-]NAME [+ CST]', return -1 or +1
     589              :      accordingly.  If VAL1 and VAL2 don't use the same name, return -2.  */
     590      2694565 :   if (sym1 && sym2)
     591              :     {
     592              :       /* Both values must use the same name with the same sign.  */
     593            0 :       if (sym1 != sym2 || neg1 != neg2)
     594              :         return -2;
     595              : 
     596              :       /* [-]NAME + CST == [-]NAME + CST.  */
     597            0 :       if (inv1 == inv2)
     598              :         return 0;
     599              : 
     600              :       /* If overflow is defined we cannot simplify more.  */
     601            0 :       if (!overflow_undefined)
     602              :         return -2;
     603              : 
     604            0 :       if (!inv1)
     605            0 :         inv1 = build_int_cst (TREE_TYPE (val1), 0);
     606            0 :       if (!inv2)
     607            0 :         inv2 = build_int_cst (TREE_TYPE (val2), 0);
     608              : 
     609            0 :       return wi::cmp (wi::to_wide (inv1), wi::to_wide (inv2),
     610            0 :                       TYPE_SIGN (TREE_TYPE (val1)));
     611              :     }
     612              : 
     613      2694565 :   const bool cst1 = is_gimple_min_invariant (val1);
     614      2694565 :   const bool cst2 = is_gimple_min_invariant (val2);
     615              : 
     616              :   /* If one is of the form '[-]NAME + CST' and the other is constant, then
     617              :      it might be possible to say something depending on the constants.  */
     618      2694565 :   if ((sym1 && inv1 && cst2) || (sym2 && inv2 && cst1))
     619              :     {
     620            0 :       if (!overflow_undefined)
     621              :         return -2;
     622              : 
     623            0 :       const signop sgn = TYPE_SIGN (TREE_TYPE (val1));
     624            0 :       tree cst = cst1 ? val1 : val2;
     625            0 :       tree inv = cst1 ? inv2 : inv1;
     626              : 
     627              :       /* Compute the difference between the constants.  If it overflows or
     628              :          underflows, this means that we can trivially compare the NAME with
     629              :          it and, consequently, the two values with each other.  */
     630            0 :       wide_int diff = wi::to_wide (cst) - wi::to_wide (inv);
     631            0 :       if (wi::cmp (0, wi::to_wide (inv), sgn)
     632            0 :           != wi::cmp (diff, wi::to_wide (cst), sgn))
     633              :         {
     634            0 :           const int res = wi::cmp (wi::to_wide (cst), wi::to_wide (inv), sgn);
     635            0 :           return cst1 ? res : -res;
     636              :         }
     637              : 
     638              :       return -2;
     639            0 :     }
     640              : 
     641              :   /* We cannot say anything more for non-constants.  */
     642      2694565 :   if (!cst1 || !cst2)
     643              :     return -2;
     644              : 
     645      2694565 :   if (!POINTER_TYPE_P (TREE_TYPE (val1)))
     646              :     {
     647              :       /* We cannot compare overflowed values.  */
     648      2694565 :       if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
     649              :         return -2;
     650              : 
     651      2694565 :       if (TREE_CODE (val1) == INTEGER_CST
     652      2694565 :           && TREE_CODE (val2) == INTEGER_CST)
     653      2694565 :         return tree_int_cst_compare (val1, val2);
     654              : 
     655            0 :       if (poly_int_tree_p (val1) && poly_int_tree_p (val2))
     656              :         {
     657            0 :           if (known_eq (wi::to_poly_widest (val1),
     658              :                         wi::to_poly_widest (val2)))
     659              :             return 0;
     660            0 :           if (known_lt (wi::to_poly_widest (val1),
     661              :                         wi::to_poly_widest (val2)))
     662              :             return -1;
     663            0 :           if (known_gt (wi::to_poly_widest (val1),
     664              :                         wi::to_poly_widest (val2)))
     665              :             return 1;
     666              :         }
     667              : 
     668            0 :       return -2;
     669              :     }
     670              :   else
     671              :     {
     672            0 :       if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
     673              :         {
     674              :           /* We cannot compare overflowed values.  */
     675            0 :           if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
     676              :             return -2;
     677              : 
     678            0 :           return tree_int_cst_compare (val1, val2);
     679              :         }
     680              : 
     681              :       /* First see if VAL1 and VAL2 are not the same.  */
     682            0 :       if (operand_equal_p (val1, val2, 0))
     683              :         return 0;
     684              : 
     685              :       /* If VAL1 is a lower address than VAL2, return -1.  */
     686            0 :       tree t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val1, val2);
     687            0 :       if (t && integer_onep (t))
     688              :         return -1;
     689              : 
     690              :       /* If VAL1 is a higher address than VAL2, return +1.  */
     691            0 :       t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val2, val1);
     692            0 :       if (t && integer_onep (t))
     693              :         return 1;
     694              : 
     695              :       /* If VAL1 is different than VAL2, return +2.  */
     696            0 :       t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
     697            0 :       if (t && integer_onep (t))
     698            0 :         return 2;
     699              : 
     700              :       return -2;
     701              :     }
     702              : }
     703              : 
     704              : /* Helper for overflow_comparison_p
     705              : 
     706              :    OP0 CODE OP1 is a comparison.  Examine the comparison and potentially
     707              :    OP1's defining statement to see if it ultimately has the form
     708              :    OP0 CODE (OP0 PLUS INTEGER_CST)
     709              : 
     710              :    If so, return TRUE indicating this is an overflow test and store into
     711              :    *NEW_CST an updated constant that can be used in a narrowed range test.
     712              : 
     713              :    REVERSED indicates if the comparison was originally:
     714              : 
     715              :    OP1 CODE' OP0.
     716              : 
     717              :    This affects how we build the updated constant.  */
     718              : 
     719              : static bool
     720     40904013 : overflow_comparison_p_1 (enum tree_code code, tree op0, tree op1,
     721              :                          bool reversed, tree *new_cst)
     722              : {
     723              :   /* See if this is a relational operation between two SSA_NAMES with
     724              :      unsigned, overflow wrapping values.  If so, check it more deeply.  */
     725     40904013 :   if ((code == LT_EXPR || code == LE_EXPR
     726     34776041 :        || code == GE_EXPR || code == GT_EXPR)
     727      9590609 :       && TREE_CODE (op0) == SSA_NAME
     728      6742292 :       && TREE_CODE (op1) == SSA_NAME
     729      3894305 :       && INTEGRAL_TYPE_P (TREE_TYPE (op0))
     730      3535347 :       && TYPE_UNSIGNED (TREE_TYPE (op0))
     731     42402924 :       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
     732              :     {
     733      1498911 :       auto *op1_def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (op1));
     734              : 
     735              :       /* Now look at the defining statement of OP1 to see if it adds
     736              :          or subtracts a nonzero constant from another operand.  */
     737      1148560 :       if (op1_def
     738      1148560 :           && gimple_assign_rhs_code (op1_def) == PLUS_EXPR
     739       339545 :           && TREE_CODE (gimple_assign_rhs2 (op1_def)) == INTEGER_CST
     740       179053 :           && !integer_zerop (gimple_assign_rhs2 (op1_def)))
     741              :         {
     742       179053 :           tree target = gimple_assign_rhs1 (op1_def);
     743              : 
     744              :           /* If we did not find our target SSA_NAME, then this is not
     745              :              an overflow test.  */
     746       179053 :           if (op0 != target)
     747              :             return false;
     748              : 
     749         1236 :           tree type = TREE_TYPE (op0);
     750         1236 :           wide_int max = wi::max_value (TYPE_PRECISION (type), UNSIGNED);
     751         1236 :           tree inc = gimple_assign_rhs2 (op1_def);
     752         1236 :           if (reversed)
     753          225 :             *new_cst = wide_int_to_tree (type, max + wi::to_wide (inc));
     754              :           else
     755         1011 :             *new_cst = wide_int_to_tree (type, max - wi::to_wide (inc));
     756         1236 :           return true;
     757         1236 :         }
     758              :     }
     759              :   return false;
     760              : }
     761              : 
     762              : /* OP0 CODE OP1 is a comparison.  Examine the comparison and potentially
     763              :    OP1's defining statement to see if it ultimately has the form
     764              :    OP0 CODE (OP0 PLUS INTEGER_CST)
     765              : 
     766              :    If so, return TRUE indicating this is an overflow test and store into
     767              :    *NEW_CST an updated constant that can be used in a narrowed range test.
     768              : 
     769              :    These statements are left as-is in the IL to facilitate discovery of
     770              :    {ADD,SUB}_OVERFLOW sequences later in the optimizer pipeline.  But
     771              :    the alternate range representation is often useful within VRP.  */
     772              : 
     773              : bool
     774     20452512 : overflow_comparison_p (tree_code code, tree name, tree val, tree *new_cst)
     775              : {
     776     20452512 :   if (overflow_comparison_p_1 (code, name, val, false, new_cst))
     777              :     return true;
     778     20451501 :   return overflow_comparison_p_1 (swap_tree_comparison (code), val, name,
     779     20451501 :                                   true, new_cst);
     780              : }
     781              : 
     782              : /* Searches the case label vector VEC for the index *IDX of the CASE_LABEL
     783              :    that includes the value VAL.  The search is restricted to the range
     784              :    [START_IDX, n - 1] where n is the size of VEC.
     785              : 
     786              :    If there is a CASE_LABEL for VAL, its index is placed in IDX and true is
     787              :    returned.
     788              : 
     789              :    If there is no CASE_LABEL for VAL and there is one that is larger than VAL,
     790              :    it is placed in IDX and false is returned.
     791              : 
     792              :    If VAL is larger than any CASE_LABEL, n is placed on IDX and false is
     793              :    returned. */
     794              : 
     795              : bool
     796       211174 : find_case_label_index (gswitch *stmt, size_t start_idx, tree val, size_t *idx)
     797              : {
     798       211174 :   size_t n = gimple_switch_num_labels (stmt);
     799       211174 :   size_t low, high;
     800              : 
     801              :   /* Find case label for minimum of the value range or the next one.
     802              :      At each iteration we are searching in [low, high - 1]. */
     803              : 
     804       850723 :   for (low = start_idx, high = n; high != low; )
     805              :     {
     806       498782 :       tree t;
     807       498782 :       int cmp;
     808              :       /* Note that i != high, so we never ask for n. */
     809       498782 :       size_t i = (high + low) / 2;
     810       498782 :       t = gimple_switch_label (stmt, i);
     811              : 
     812              :       /* Cache the result of comparing CASE_LOW and val.  */
     813       498782 :       cmp = tree_int_cst_compare (CASE_LOW (t), val);
     814              : 
     815       498782 :       if (cmp == 0)
     816              :         {
     817              :           /* Ranges cannot be empty. */
     818        68819 :           *idx = i;
     819        68819 :           return true;
     820              :         }
     821       429963 :       else if (cmp > 0)
     822              :         high = i;
     823              :       else
     824              :         {
     825       189769 :           low = i + 1;
     826       189769 :           if (CASE_HIGH (t) != NULL
     827       189769 :               && tree_int_cst_compare (CASE_HIGH (t), val) >= 0)
     828              :             {
     829         1588 :               *idx = i;
     830         1588 :               return true;
     831              :             }
     832              :         }
     833              :     }
     834              : 
     835       140767 :   *idx = high;
     836       140767 :   return false;
     837              : }
     838              : 
     839              : /* Searches the case label vector VEC for the range of CASE_LABELs that is used
     840              :    for values between MIN and MAX. The first index is placed in MIN_IDX. The
     841              :    last index is placed in MAX_IDX. If the range of CASE_LABELs is empty
     842              :    then MAX_IDX < MIN_IDX.
     843              :    Returns true if the default label is not needed. */
     844              : 
     845              : bool
     846       105587 : find_case_label_range (gswitch *stmt, tree min, tree max, size_t *min_idx,
     847              :                        size_t *max_idx)
     848              : {
     849       105587 :   size_t i, j;
     850       105587 :   bool min_take_default = !find_case_label_index (stmt, 1, min, &i);
     851       105587 :   bool max_take_default = !find_case_label_index (stmt, i, max, &j);
     852              : 
     853       105587 :   if (i == j
     854              :       && min_take_default
     855         9498 :       && max_take_default)
     856              :     {
     857              :       /* Only the default case label reached.
     858              :          Return an empty range. */
     859         3913 :       *min_idx = 1;
     860         3913 :       *max_idx = 0;
     861         3913 :       return false;
     862              :     }
     863              :   else
     864              :     {
     865       101674 :       bool take_default = min_take_default || max_take_default;
     866       101674 :       tree low, high;
     867       101674 :       size_t k;
     868              : 
     869       101674 :       if (max_take_default)
     870        77128 :         j--;
     871              : 
     872              :       /* If the case label range is continuous, we do not need
     873              :          the default case label.  Verify that.  */
     874       101674 :       high = CASE_LOW (gimple_switch_label (stmt, i));
     875       101674 :       if (CASE_HIGH (gimple_switch_label (stmt, i)))
     876         4475 :         high = CASE_HIGH (gimple_switch_label (stmt, i));
     877       252875 :       for (k = i + 1; k <= j; ++k)
     878              :         {
     879       211869 :           low = CASE_LOW (gimple_switch_label (stmt, k));
     880       211869 :           if (!integer_onep (int_const_binop (MINUS_EXPR, low, high)))
     881              :             {
     882              :               take_default = true;
     883              :               break;
     884              :             }
     885       151201 :           high = low;
     886       151201 :           if (CASE_HIGH (gimple_switch_label (stmt, k)))
     887         4759 :             high = CASE_HIGH (gimple_switch_label (stmt, k));
     888              :         }
     889              : 
     890       101674 :       *min_idx = i;
     891       101674 :       *max_idx = j;
     892       101674 :       return !take_default;
     893              :     }
     894              : }
     895              : 
     896              : /* Given a SWITCH_STMT, return the case label that encompasses the
     897              :    known possible values for the switch operand.  RANGE_OF_OP is a
     898              :    range for the known values of the switch operand.  */
     899              : 
     900              : tree
     901       138470 : find_case_label_range (gswitch *switch_stmt, const irange *range_of_op)
     902              : {
     903       138470 :   if (range_of_op->undefined_p ()
     904       138470 :       || range_of_op->varying_p ())
     905              :     return NULL_TREE;
     906              : 
     907       105587 :   size_t i, j;
     908       105587 :   tree op = gimple_switch_index (switch_stmt);
     909       105587 :   tree type = TREE_TYPE (op);
     910       105587 :   tree tmin = wide_int_to_tree (type, range_of_op->lower_bound ());
     911       105587 :   tree tmax = wide_int_to_tree (type, range_of_op->upper_bound ());
     912       105587 :   find_case_label_range (switch_stmt, tmin, tmax, &i, &j);
     913       105587 :   if (i == j)
     914              :     {
     915              :       /* Look for exactly one label that encompasses the range of
     916              :          the operand.  */
     917         7848 :       tree label = gimple_switch_label (switch_stmt, i);
     918         7848 :       tree case_high
     919         7848 :         = CASE_HIGH (label) ? CASE_HIGH (label) : CASE_LOW (label);
     920         7848 :       wide_int wlow = wi::to_wide (CASE_LOW (label));
     921         7848 :       wide_int whigh = wi::to_wide (case_high);
     922         7848 :       int_range_max label_range (TREE_TYPE (case_high), wlow, whigh);
     923         7848 :       if (!types_compatible_p (label_range.type (), range_of_op->type ()))
     924           10 :         range_cast (label_range, range_of_op->type ());
     925         7848 :       label_range.intersect (*range_of_op);
     926         7848 :       if (label_range == *range_of_op)
     927         4770 :         return label;
     928         7848 :     }
     929        97739 :   else if (i > j)
     930              :     {
     931              :       /* If there are no labels at all, take the default.  */
     932         3913 :       return gimple_switch_label (switch_stmt, 0);
     933              :     }
     934              :   else
     935              :     {
     936              :       /* Otherwise, there are various labels that can encompass
     937              :          the range of operand.  In which case, see if the range of
     938              :          the operand is entirely *outside* the bounds of all the
     939              :          (non-default) case labels.  If so, take the default.  */
     940        93826 :       unsigned n = gimple_switch_num_labels (switch_stmt);
     941        93826 :       tree min_label = gimple_switch_label (switch_stmt, 1);
     942        93826 :       tree max_label = gimple_switch_label (switch_stmt, n - 1);
     943        93826 :       tree case_high = CASE_HIGH (max_label);
     944        93826 :       if (!case_high)
     945        90187 :         case_high = CASE_LOW (max_label);
     946        93826 :       int_range_max label_range (TREE_TYPE (CASE_LOW (min_label)),
     947       187652 :                                  wi::to_wide (CASE_LOW (min_label)),
     948       187652 :                                  wi::to_wide (case_high));
     949        93826 :       if (!types_compatible_p (label_range.type (), range_of_op->type ()))
     950           38 :         range_cast (label_range, range_of_op->type ());
     951        93826 :       label_range.intersect (*range_of_op);
     952        93826 :       if (label_range.undefined_p ())
     953          635 :         return gimple_switch_label (switch_stmt, 0);
     954        93826 :     }
     955              :   return NULL_TREE;
     956              : }
     957              : 
     958              : struct case_info
     959              : {
     960              :   tree expr;
     961              :   basic_block bb;
     962              : };
     963              : 
     964              : // This is a ranger based folder which continues to use the dominator
     965              : // walk to access the substitute and fold machinery.  Ranges are calculated
     966              : // on demand.
     967              : 
     968              : class rvrp_folder : public substitute_and_fold_engine
     969              : {
     970              : public:
     971              : 
     972      4390630 :   rvrp_folder (gimple_ranger *r, bool all)
     973              :     : substitute_and_fold_engine (),
     974      4390630 :       m_unreachable (*r, all),
     975      4390630 :       m_simplifier (r, r->non_executable_edge_flag)
     976              :   {
     977      4390630 :     m_ranger = r;
     978      4390630 :     m_last_bb_stmt = NULL;
     979      4390630 :   }
     980              : 
     981    124510558 :   tree value_of_expr (tree name, gimple *s = NULL) override
     982              :   {
     983              :     // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
     984    124510558 :     if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
     985              :       return NULL_TREE;
     986    124496679 :     if (!value_range::supports_type_p (TREE_TYPE (name)))
     987              :       return NULL_TREE;
     988              : 
     989    121279299 :     value_range r (TREE_TYPE (name));
     990    121279299 :     if (!m_ranger->range_of_expr (r, name, s))
     991              :       return NULL_TREE;
     992              : 
     993              :     // A constant used in an unreachable block often returns as UNDEFINED.
     994              :     // If the result is undefined, check the global value for a constant.
     995    121279299 :     if (r.undefined_p ())
     996       229589 :       range_of_expr (r, name);
     997              : 
     998    121279299 :     tree ret;
     999    121279299 :     if (r.singleton_p (&ret))
    1000       890261 :       return ret;
    1001              :     else
    1002    120389038 :       ret = NULL_TREE;
    1003    120389038 :     if (is_a <prange> (r))
    1004              :       {
    1005     53704192 :         prange &p = as_a <prange> (r);
    1006     53704192 :         ret = p.addr_known ();
    1007              :         // A const points has to be gimple_min_invariant.
    1008     53704192 :         gcc_checking_assert (!ret || is_gimple_min_invariant (ret));
    1009              :       }
    1010              : 
    1011    120389038 :     return ret;
    1012    121279299 :   }
    1013              : 
    1014     25176830 :   tree value_on_edge (edge e, tree name) override
    1015              :   {
    1016              :     // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
    1017     25176830 :     if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
    1018              :       return NULL;
    1019     25149285 :     if (!value_range::supports_type_p (TREE_TYPE (name)))
    1020              :       return NULL_TREE;
    1021              : 
    1022     24879279 :     value_range r (TREE_TYPE (name));
    1023     24879279 :     if (!m_ranger->range_on_edge (r, e, name))
    1024              :       return NULL_TREE;
    1025              : 
    1026              :     // A constant used in an unreachable block often returns as UNDEFINED.
    1027              :     // If the result is undefined, check the global value for a constant.
    1028     24879279 :     if (r.undefined_p ())
    1029       284775 :       range_of_expr (r, name);
    1030              : 
    1031     24879279 :     tree ret;
    1032     24879279 :     if (r.singleton_p (&ret))
    1033       334482 :       return ret;
    1034              :     else
    1035     24544797 :       ret = NULL_TREE;
    1036     24544797 :     if (is_a <prange> (r))
    1037              :       {
    1038      7454804 :         prange &p = as_a <prange> (r);
    1039      7454804 :         ret = p.addr_known ();
    1040              :         // A const points has to be gimple_min_invariant.
    1041      7454804 :         gcc_checking_assert (!ret || is_gimple_min_invariant (ret));
    1042              :       }
    1043              : 
    1044     24544797 :     return ret;
    1045     24879279 :   }
    1046              : 
    1047     49419941 :   tree value_of_stmt (gimple *s, tree name = NULL) override
    1048              :   {
    1049              :     // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
    1050     49419941 :     if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
    1051              :       return NULL;
    1052     49417769 :     return m_ranger->value_of_stmt (s, name);
    1053              :   }
    1054              : 
    1055     38266903 :   void pre_fold_bb (basic_block bb) override
    1056              :   {
    1057     53327474 :     for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
    1058     15060571 :          gsi_next (&gsi))
    1059     15060571 :       m_ranger->register_inferred_ranges (gsi.phi ());
    1060     38266903 :     m_last_bb_stmt = last_nondebug_stmt (bb);
    1061     38266903 :   }
    1062              : 
    1063    258039036 :   void pre_fold_stmt (gimple *stmt) override
    1064              :   {
    1065              :     // If this is the last stmt and there are inferred ranges, reparse the
    1066              :     // block for transitive inferred ranges that occur earlier in the block.
    1067    258039036 :     if (stmt == m_last_bb_stmt)
    1068              :       {
    1069     31847789 :         m_ranger->register_transitive_inferred_ranges (gimple_bb (stmt));
    1070              :         // Also check for builtin_unreachable calls.
    1071     31847789 :         if (cfun->after_inlining && gimple_code (stmt) == GIMPLE_COND)
    1072      8692675 :           m_unreachable.maybe_register (stmt);
    1073              :       }
    1074    258039036 :   }
    1075              : 
    1076    257743595 :   bool fold_stmt (gimple_stmt_iterator *gsi) override
    1077              :   {
    1078    257743595 :     bool ret = m_simplifier.simplify (gsi);
    1079    257743595 :     if (!ret)
    1080    257110154 :       ret = m_ranger->fold_stmt (gsi, follow_single_use_edges);
    1081    257743595 :     m_ranger->register_inferred_ranges (gsi_stmt (*gsi));
    1082    257743595 :     return ret;
    1083              :   }
    1084              : 
    1085              :   remove_unreachable m_unreachable;
    1086              : private:
    1087              :   DISABLE_COPY_AND_ASSIGN (rvrp_folder);
    1088              :   gimple_ranger *m_ranger;
    1089              :   simplify_using_ranges m_simplifier;
    1090              :   gimple *m_last_bb_stmt;
    1091              : };
    1092              : 
    1093              : /* Main entry point for a VRP pass using just ranger. This can be called
    1094              :   from anywhere to perform a VRP pass, including from EVRP.  */
    1095              : 
    1096              : unsigned int
    1097      4390630 : execute_ranger_vrp (struct function *fun, bool final_p)
    1098              : {
    1099      4390630 :   loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
    1100      4390630 :   rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
    1101      4390630 :   scev_initialize ();
    1102      4390630 :   calculate_dominance_info (CDI_DOMINATORS);
    1103              : 
    1104      4390630 :   set_all_edges_as_executable (fun);
    1105      4390630 :   gimple_ranger *ranger = enable_ranger (fun, false);
    1106      4390630 :   phi_analysis (*ranger);
    1107      4390630 :   rvrp_folder folder (ranger, final_p);
    1108      4390630 :   folder.substitute_and_fold ();
    1109              :   // Ensure the cache in SCEV has been cleared before processing
    1110              :   // globals to be removed.
    1111      4390630 :   scev_reset ();
    1112              :   // Remove tagged builtin-unreachable and maybe update globals.
    1113      4390630 :   folder.m_unreachable.remove_and_update_globals ();
    1114      4390630 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1115           49 :     ranger->dump (dump_file);
    1116              : 
    1117      4390630 :   if (value_range::supports_type_p (TREE_TYPE
    1118              :                                      (TREE_TYPE (current_function_decl)))
    1119      1880946 :       && flag_ipa_vrp
    1120      6270770 :       && !lookup_attribute ("noipa", DECL_ATTRIBUTES (current_function_decl)))
    1121              :     {
    1122      1840545 :       edge e;
    1123      1840545 :       edge_iterator ei;
    1124      1840545 :       bool found = false;
    1125      1840545 :       value_range return_range (TREE_TYPE (TREE_TYPE (current_function_decl)));
    1126      3654511 :       FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
    1127      5441048 :         if (greturn *ret = dyn_cast <greturn *> (*gsi_last_bb (e->src)))
    1128              :           {
    1129      1813116 :             tree retval = gimple_return_retval (ret);
    1130      1813116 :             if (!retval)
    1131              :               {
    1132        10899 :                 return_range.set_varying (TREE_TYPE (TREE_TYPE (current_function_decl)));
    1133        10899 :                 found = true;
    1134        10899 :                 continue;
    1135              :               }
    1136      1802217 :             value_range r (TREE_TYPE (retval));
    1137      1802217 :             if (ranger->range_of_expr (r, retval, ret)
    1138      1802217 :                 && !r.undefined_p ()
    1139      3603743 :                 && !r.varying_p ())
    1140              :               {
    1141       754522 :                 if (!found)
    1142       752258 :                   return_range = r;
    1143              :                 else
    1144         2264 :                   return_range.union_ (r);
    1145              :               }
    1146              :             else
    1147      1047695 :               return_range.set_varying (TREE_TYPE (retval));
    1148      1802217 :             found = true;
    1149      1802217 :           }
    1150      1840545 :       if (found && !return_range.varying_p ())
    1151              :         {
    1152       752198 :           ipa_record_return_value_range (return_range);
    1153      1382356 :           if (POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (current_function_decl)))
    1154       260828 :               && !return_range.contains_zero_p ()
    1155      1003418 :               && cgraph_node::get (current_function_decl)
    1156       251220 :                         ->add_detected_attribute ("returns_nonnull"))
    1157       223034 :             warn_function_returns_nonnull (current_function_decl);
    1158              :         }
    1159      1840545 :     }
    1160              : 
    1161      4390630 :   disable_ranger (fun);
    1162      4390630 :   scev_finalize ();
    1163      4390630 :   loop_optimizer_finalize ();
    1164      8781260 :   return 0;
    1165      4390630 : }
    1166              : 
    1167              : // Implement a Fast VRP folder.  Not quite as effective but faster.
    1168              : 
    1169              : class fvrp_folder : public substitute_and_fold_engine
    1170              : {
    1171              : public:
    1172            9 :   fvrp_folder (dom_ranger *dr, bool final_p) : substitute_and_fold_engine (),
    1173            9 :                                                m_simplifier (dr)
    1174              :   {
    1175            9 :     m_dom_ranger = dr;
    1176            9 :     if (final_p)
    1177            3 :       m_unreachable = new remove_unreachable (*dr, final_p);
    1178              :     else
    1179            6 :       m_unreachable = NULL;
    1180            9 :   }
    1181              : 
    1182            9 :   ~fvrp_folder () { }
    1183              : 
    1184          579 :   tree value_of_expr (tree name, gimple *s = NULL) override
    1185              :   {
    1186              :     // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
    1187          579 :     if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
    1188              :       return NULL;
    1189          579 :     return m_dom_ranger->value_of_expr (name, s);
    1190              :   }
    1191              : 
    1192           27 :   tree value_on_edge (edge e, tree name) override
    1193              :   {
    1194              :     // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
    1195           27 :     if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
    1196              :       return NULL;
    1197           27 :     return m_dom_ranger->value_on_edge (e, name);
    1198              :   }
    1199              : 
    1200          399 :   tree value_of_stmt (gimple *s, tree name = NULL) override
    1201              :   {
    1202              :     // Shortcircuit subst_and_fold callbacks for abnormal ssa_names.
    1203          399 :     if (TREE_CODE (name) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
    1204              :       return NULL;
    1205          399 :     return m_dom_ranger->value_of_stmt (s, name);
    1206              :   }
    1207              : 
    1208           37 :   void pre_fold_bb (basic_block bb) override
    1209              :   {
    1210           37 :     m_dom_ranger->pre_bb (bb);
    1211              :     // Now process the PHIs in advance.
    1212           37 :     gphi_iterator psi = gsi_start_phis (bb);
    1213           57 :     for ( ; !gsi_end_p (psi); gsi_next (&psi))
    1214              :       {
    1215           20 :         tree name = gimple_range_ssa_p (PHI_RESULT (psi.phi ()));
    1216           20 :         if (name)
    1217              :           {
    1218            9 :             value_range vr(TREE_TYPE (name));
    1219            9 :             m_dom_ranger->range_of_stmt (vr, psi.phi (), name);
    1220            9 :           }
    1221              :       }
    1222           37 :   }
    1223              : 
    1224           37 :   void post_fold_bb (basic_block bb) override
    1225              :   {
    1226           37 :     m_dom_ranger->post_bb (bb);
    1227           37 :   }
    1228              : 
    1229          431 :   void pre_fold_stmt (gimple *s) override
    1230              :   {
    1231              :     // Ensure range_of_stmt has been called.
    1232          431 :     tree type = gimple_range_type (s);
    1233          431 :     if (type)
    1234              :       {
    1235          412 :         value_range vr(type);
    1236          412 :         m_dom_ranger->range_of_stmt (vr, s);
    1237          412 :       }
    1238          431 :     if (m_unreachable && gimple_code (s) == GIMPLE_COND)
    1239            9 :       m_unreachable->maybe_register (s);
    1240              : 
    1241          431 :   }
    1242              : 
    1243          422 :   bool fold_stmt (gimple_stmt_iterator *gsi) override
    1244              :   {
    1245          422 :     bool ret = m_simplifier.simplify (gsi);
    1246          422 :     if (!ret)
    1247          419 :       ret = ::fold_stmt (gsi, follow_single_use_edges);
    1248          422 :     return ret;
    1249              :   }
    1250              : 
    1251              :   remove_unreachable *m_unreachable;
    1252              : private:
    1253              :   DISABLE_COPY_AND_ASSIGN (fvrp_folder);
    1254              :   simplify_using_ranges m_simplifier;
    1255              :   dom_ranger *m_dom_ranger;
    1256              : };
    1257              : 
    1258              : 
    1259              : // Main entry point for a FAST VRP pass using a dom ranger.
    1260              : 
    1261              : unsigned int
    1262            9 : execute_fast_vrp (struct function *fun, bool final_p)
    1263              : {
    1264            9 :   calculate_dominance_info (CDI_DOMINATORS);
    1265            9 :   dom_ranger dr;
    1266              :   // The relation oracle will be destroyed when the destructor for 'dr' runs.
    1267            9 :   dr.create_relation_oracle ();
    1268            9 :   fvrp_folder folder (&dr, final_p);
    1269              : 
    1270            9 :   set_all_edges_as_executable (fun);
    1271              :   // Make DR the current range_query.
    1272            9 :   range_query *saved = set_range_query (fun, &dr);
    1273              : 
    1274            9 :   folder.substitute_and_fold ();
    1275            9 :   if (folder.m_unreachable)
    1276            3 :     folder.m_unreachable->remove ();
    1277              : 
    1278            9 :   range_query *q = set_range_query (fun, saved);
    1279            9 :   gcc_checking_assert (q == &dr);
    1280           18 :   return 0;
    1281            9 : }
    1282              : 
    1283              : namespace {
    1284              : 
    1285              : const pass_data pass_data_vrp =
    1286              : {
    1287              :   GIMPLE_PASS, /* type */
    1288              :   "vrp", /* name */
    1289              :   OPTGROUP_NONE, /* optinfo_flags */
    1290              :   TV_TREE_VRP, /* tv_id */
    1291              :   PROP_ssa, /* properties_required */
    1292              :   0, /* properties_provided */
    1293              :   0, /* properties_destroyed */
    1294              :   0, /* todo_flags_start */
    1295              :   ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
    1296              : };
    1297              : 
    1298              : const pass_data pass_data_early_vrp =
    1299              : {
    1300              :   GIMPLE_PASS, /* type */
    1301              :   "evrp", /* name */
    1302              :   OPTGROUP_NONE, /* optinfo_flags */
    1303              :   TV_TREE_EARLY_VRP, /* tv_id */
    1304              :   PROP_ssa, /* properties_required */
    1305              :   0, /* properties_provided */
    1306              :   0, /* properties_destroyed */
    1307              :   0, /* todo_flags_start */
    1308              :   ( TODO_cleanup_cfg | TODO_update_ssa ),
    1309              : };
    1310              : 
    1311              : const pass_data pass_data_fast_vrp =
    1312              : {
    1313              :   GIMPLE_PASS, /* type */
    1314              :   "fvrp", /* name */
    1315              :   OPTGROUP_NONE, /* optinfo_flags */
    1316              :   TV_TREE_FAST_VRP, /* tv_id */
    1317              :   PROP_ssa, /* properties_required */
    1318              :   0, /* properties_provided */
    1319              :   0, /* properties_destroyed */
    1320              :   0, /* todo_flags_start */
    1321              :   ( TODO_cleanup_cfg | TODO_update_ssa ),
    1322              : };
    1323              : 
    1324              : 
    1325              : class pass_vrp : public gimple_opt_pass
    1326              : {
    1327              : public:
    1328       908955 :   pass_vrp (gcc::context *ctxt, const pass_data &data_)
    1329      1817910 :     : gimple_opt_pass (data_, ctxt), data (data_), final_p (false)
    1330              :     { }
    1331              : 
    1332              :   /* opt_pass methods: */
    1333       302985 :   opt_pass * clone () final override
    1334       302985 :     { return new pass_vrp (m_ctxt, data); }
    1335       605970 :   void set_pass_param (unsigned int n, bool param) final override
    1336              :     {
    1337       605970 :       gcc_assert (n == 0);
    1338       605970 :       final_p = param;
    1339       605970 :     }
    1340      4663834 :   bool gate (function *) final override { return flag_tree_vrp != 0; }
    1341      4390639 :   unsigned int execute (function *fun) final override
    1342              :     {
    1343              :       // Check for fast vrp.
    1344      4390639 :       bool use_fvrp = (&data == &pass_data_fast_vrp);
    1345      4390639 :       if (!use_fvrp && last_basic_block_for_fn (fun) > param_vrp_block_limit)
    1346              :         {
    1347            9 :           use_fvrp = true;
    1348            9 :           warning (OPT_Wdisabled_optimization,
    1349              :                    "using fast VRP algorithm; %d basic blocks"
    1350              :                    " exceeds %<--param=vrp-block-limit=%d%> limit",
    1351              :                    n_basic_blocks_for_fn (fun),
    1352              :                    param_vrp_block_limit);
    1353              :         }
    1354      4390639 :       if (use_fvrp)
    1355            9 :         return execute_fast_vrp (fun, final_p);
    1356      4390630 :       return execute_ranger_vrp (fun, final_p);
    1357              :     }
    1358              : 
    1359              :  private:
    1360              :   const pass_data &data;
    1361              :   bool final_p;
    1362              : }; // class pass_vrp
    1363              : } // anon namespace
    1364              : 
    1365              : gimple_opt_pass *
    1366       302985 : make_pass_vrp (gcc::context *ctxt)
    1367              : {
    1368       302985 :   return new pass_vrp (ctxt, pass_data_vrp);
    1369              : }
    1370              : 
    1371              : gimple_opt_pass *
    1372       302985 : make_pass_early_vrp (gcc::context *ctxt)
    1373              : {
    1374       302985 :   return new pass_vrp (ctxt, pass_data_early_vrp);
    1375              : }
    1376              : 
    1377              : gimple_opt_pass *
    1378            0 : make_pass_fast_vrp (gcc::context *ctxt)
    1379              : {
    1380            0 :   return new pass_vrp (ctxt, pass_data_fast_vrp);
    1381              : }
        

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.