LCOV - code coverage report
Current view: top level - gcc - gimple-range-cache.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 85.3 % 850 725
Test Date: 2026-09-19 16:22:48 Functions: 92.6 % 81 75
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Gimple ranger SSA cache implementation.
       2              :    Copyright (C) 2017-2026 Free Software Foundation, Inc.
       3              :    Contributed by Andrew MacLeod <amacleod@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 "backend.h"
      25              : #include "insn-codes.h"
      26              : #include "tree.h"
      27              : #include "gimple.h"
      28              : #include "ssa.h"
      29              : #include "gimple-pretty-print.h"
      30              : #include "gimple-range.h"
      31              : #include "value-range-storage.h"
      32              : #include "tree-cfg.h"
      33              : #include "target.h"
      34              : #include "attribs.h"
      35              : #include "gimple-iterator.h"
      36              : #include "gimple-walk.h"
      37              : #include "cfganal.h"
      38              : 
      39              : #define DEBUG_RANGE_CACHE (dump_file                                    \
      40              :                            && (param_ranger_debug & RANGER_DEBUG_CACHE))
      41              : 
      42              : // This class represents the API into a cache of ranges for an SSA_NAME.
      43              : // Routines must be implemented to set, get, and query if a value is set.
      44              : 
      45              : class ssa_block_ranges
      46              : {
      47              : public:
      48     29066446 :   ssa_block_ranges (tree t) : m_type (t) { }
      49              :   virtual bool set_bb_range (const_basic_block bb, const vrange &r) = 0;
      50              :   virtual bool get_bb_range (vrange &r, const_basic_block bb) = 0;
      51              :   virtual bool bb_range_p (const_basic_block bb) = 0;
      52              : 
      53              :   void dump(FILE *f);
      54              : private:
      55              :   tree m_type;
      56              : };
      57              : 
      58              : // Print the list of known ranges for file F in a nice format.
      59              : 
      60              : void
      61            0 : ssa_block_ranges::dump (FILE *f)
      62              : {
      63            0 :   basic_block bb;
      64            0 :   value_range r (m_type);
      65              : 
      66            0 :   FOR_EACH_BB_FN (bb, cfun)
      67            0 :     if (get_bb_range (r, bb))
      68              :       {
      69            0 :         fprintf (f, "BB%d  -> ", bb->index);
      70            0 :         r.dump (f);
      71            0 :         fprintf (f, "\n");
      72              :       }
      73            0 : }
      74              : 
      75              : // This class implements the range cache as a linear vector, indexed by BB.
      76              : // It caches a varying and undefined range which are used instead of
      77              : // allocating new ones each time.
      78              : 
      79              : class sbr_vector : public ssa_block_ranges
      80              : {
      81              : public:
      82              :   sbr_vector (tree t, vrange_allocator *allocator, bool zero_p = true);
      83              : 
      84              :   virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
      85              :   virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
      86              :   virtual bool bb_range_p (const_basic_block bb) override;
      87              : protected:
      88              :   vrange_storage **m_tab;       // Non growing vector.
      89              :   int m_tab_size;
      90              :   vrange_storage *m_varying;
      91              :   vrange_storage *m_undefined;
      92              :   tree m_type;
      93              :   vrange_allocator *m_range_allocator;
      94              :   bool m_zero_p;
      95              :   void grow ();
      96              : };
      97              : 
      98              : 
      99              : // Initialize a block cache for an ssa_name of type T.
     100              : 
     101     28961232 : sbr_vector::sbr_vector (tree t, vrange_allocator *allocator, bool zero_p)
     102     28961232 :   : ssa_block_ranges (t)
     103              : {
     104     28961232 :   gcc_checking_assert (TYPE_P (t));
     105     28961232 :   m_type = t;
     106     28961232 :   m_zero_p = zero_p;
     107     28961232 :   m_range_allocator = allocator;
     108     28961232 :   m_tab_size = last_basic_block_for_fn (cfun) + 1;
     109     57922464 :   m_tab = static_cast <vrange_storage **>
     110     28961232 :     (allocator->alloc (m_tab_size * sizeof (vrange_storage *)));
     111     28961232 :   if (zero_p)
     112     25458727 :     memset (m_tab, 0, m_tab_size * sizeof (vrange *));
     113              : 
     114              :   // Create the cached type range.
     115     28961232 :   m_varying = m_range_allocator->clone_varying (t);
     116     28961232 :   m_undefined = m_range_allocator->clone_undefined (t);
     117     28961232 : }
     118              : 
     119              : // Grow the vector when the CFG has increased in size.
     120              : 
     121              : void
     122         9667 : sbr_vector::grow ()
     123              : {
     124         9667 :   int curr_bb_size = last_basic_block_for_fn (cfun);
     125         9667 :   gcc_checking_assert (curr_bb_size > m_tab_size);
     126              : 
     127              :   // Increase the max of a)128, b)needed increase * 2, c)10% of current_size.
     128         9667 :   int inc = MAX ((curr_bb_size - m_tab_size) * 2, 128);
     129         9667 :   inc = MAX (inc, curr_bb_size / 10);
     130         9667 :   int new_size = inc + curr_bb_size;
     131              : 
     132              :   // Allocate new memory, copy the old vector and clear the new space.
     133         9667 :   vrange_storage **t = static_cast <vrange_storage **>
     134         9667 :     (m_range_allocator->alloc (new_size * sizeof (vrange_storage *)));
     135         9667 :   memcpy (t, m_tab, m_tab_size * sizeof (vrange_storage *));
     136         9667 :   if (m_zero_p)
     137         7503 :     memset (t + m_tab_size, 0, (new_size - m_tab_size) * sizeof (vrange_storage *));
     138              : 
     139         9667 :   m_tab = t;
     140         9667 :   m_tab_size = new_size;
     141         9667 : }
     142              : 
     143              : // Set the range for block BB to be R.
     144              : 
     145              : bool
     146     75864143 : sbr_vector::set_bb_range (const_basic_block bb, const vrange &r)
     147              : {
     148     75864143 :   vrange_storage *m;
     149     75864143 :   if (bb->index >= m_tab_size)
     150         9667 :     grow ();
     151     75864143 :   if (r.varying_p ())
     152     23569200 :     m = m_varying;
     153     52294943 :   else if (r.undefined_p ())
     154      5241627 :     m = m_undefined;
     155              :   else
     156     47053316 :     m = m_range_allocator->clone (r);
     157     75864143 :   m_tab[bb->index] = m;
     158     75864143 :   return true;
     159              : }
     160              : 
     161              : // Return the range associated with block BB in R.  Return false if
     162              : // there is no range.
     163              : 
     164              : bool
     165    339519711 : sbr_vector::get_bb_range (vrange &r, const_basic_block bb)
     166              : {
     167    339519711 :   if (bb->index >= m_tab_size)
     168              :     return false;
     169    339512250 :   vrange_storage *m = m_tab[bb->index];
     170    339512250 :   if (m)
     171              :     {
     172    256349028 :       m->get_vrange (r, m_type);
     173    256349028 :       return true;
     174              :     }
     175              :   return false;
     176              : }
     177              : 
     178              : // Return true if a range is present.
     179              : 
     180              : bool
     181    251386610 : sbr_vector::bb_range_p (const_basic_block bb)
     182              : {
     183    251386610 :   if (bb->index < m_tab_size)
     184    251376263 :     return m_tab[bb->index] != NULL;
     185              :   return false;
     186              : }
     187              : 
     188              : // Like an sbr_vector, except it uses a bitmap to manage whether value is set
     189              : // or not rather than cleared memory.
     190              : 
     191              : class sbr_lazy_vector : public sbr_vector
     192              : {
     193              : public:
     194              :   sbr_lazy_vector (tree t, vrange_allocator *allocator, bitmap_obstack *bm);
     195              : 
     196              :   virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
     197              :   virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
     198              :   virtual bool bb_range_p (const_basic_block bb) override;
     199              : protected:
     200              :   bitmap m_has_value;
     201              : };
     202              : 
     203      3502505 : sbr_lazy_vector::sbr_lazy_vector (tree t, vrange_allocator *allocator,
     204              :                                   bitmap_obstack *bm)
     205      3502505 :   : sbr_vector (t, allocator, false)
     206              : {
     207      3502505 :   m_has_value = BITMAP_ALLOC (bm);
     208      3502505 : }
     209              : 
     210              : bool
     211     11920797 : sbr_lazy_vector::set_bb_range (const_basic_block bb, const vrange &r)
     212              : {
     213     11920797 :   sbr_vector::set_bb_range (bb, r);
     214     11920797 :   bitmap_set_bit (m_has_value, bb->index);
     215     11920797 :   return true;
     216              : }
     217              : 
     218              : bool
     219    294266427 : sbr_lazy_vector::get_bb_range (vrange &r, const_basic_block bb)
     220              : {
     221    294266427 :   if (bitmap_bit_p (m_has_value, bb->index))
     222     42512304 :     return sbr_vector::get_bb_range (r, bb);
     223              :   return false;
     224              : }
     225              : 
     226              : bool
     227     45632161 : sbr_lazy_vector::bb_range_p (const_basic_block bb)
     228              : {
     229     45632161 :   return bitmap_bit_p (m_has_value, bb->index);
     230              : }
     231              : 
     232              : // This class implements the on entry cache via a sparse bitmap.
     233              : // It uses the quad bit routines to access 4 bits at a time.
     234              : // A value of 0 (the default) means there is no entry, and a value of
     235              : // 1 thru SBR_NUM represents an element in the m_range vector.
     236              : // Varying is given the first value (1) and pre-cached.
     237              : // SBR_NUM + 1 represents the value of UNDEFINED, and is never stored.
     238              : // SBR_NUM is the number of values that can be cached.
     239              : // Indexes are 1..SBR_NUM and are stored locally at m_range[0..SBR_NUM-1]
     240              : 
     241              : #define SBR_NUM         14
     242              : #define SBR_UNDEF       SBR_NUM + 1
     243              : #define SBR_VARYING     1
     244              : 
     245              : class sbr_sparse_bitmap : public ssa_block_ranges
     246              : {
     247              : public:
     248              :   sbr_sparse_bitmap (tree t, vrange_allocator *allocator, bitmap_obstack *bm);
     249              :   virtual bool set_bb_range (const_basic_block bb, const vrange &r) override;
     250              :   virtual bool get_bb_range (vrange &r, const_basic_block bb) override;
     251              :   virtual bool bb_range_p (const_basic_block bb) override;
     252              : private:
     253              :   void bitmap_set_quad (bitmap head, int quad, int quad_value);
     254              :   int bitmap_get_quad (const_bitmap head, int quad);
     255              :   vrange_allocator *m_range_allocator;
     256              :   vrange_storage *m_range[SBR_NUM];
     257              :   bitmap_head bitvec;
     258              :   tree m_type;
     259              : };
     260              : 
     261              : // Initialize a block cache for an ssa_name of type T.
     262              : 
     263       105214 : sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, vrange_allocator *allocator,
     264              :                                       bitmap_obstack *bm)
     265       105214 :   : ssa_block_ranges (t)
     266              : {
     267       105214 :   gcc_checking_assert (TYPE_P (t));
     268       105214 :   m_type = t;
     269       105214 :   bitmap_initialize (&bitvec, bm);
     270       105214 :   bitmap_tree_view (&bitvec);
     271       105214 :   m_range_allocator = allocator;
     272              :   // Pre-cache varying.
     273       105214 :   m_range[0] = m_range_allocator->clone_varying (t);
     274              :   // Pre-cache zero and non-zero values for pointers.
     275       105214 :   if (POINTER_TYPE_P (t))
     276              :     {
     277         1517 :       prange nonzero;
     278         1517 :       nonzero.set_nonzero (t);
     279         1517 :       m_range[1] = m_range_allocator->clone (nonzero);
     280         1517 :       prange zero;
     281         1517 :       zero.set_zero (t);
     282         1517 :       m_range[2] = m_range_allocator->clone (zero);
     283         1517 :     }
     284              :   else
     285       103697 :     m_range[1] = m_range[2] = NULL;
     286              :   // Clear SBR_NUM entries.
     287      1262568 :   for (int x = 3; x < SBR_NUM; x++)
     288      1157354 :     m_range[x] = 0;
     289       105214 : }
     290              : 
     291              : // Set 4 bit values in a sparse bitmap. This allows a bitmap to
     292              : // function as a sparse array of 4 bit values.
     293              : // QUAD is the index, QUAD_VALUE is the 4 bit value to set.
     294              : 
     295              : inline void
     296       498529 : sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
     297              : {
     298       498529 :   bitmap_set_aligned_chunk (head, quad, 4, (BITMAP_WORD) quad_value);
     299              : }
     300              : 
     301              : // Get a 4 bit value from a sparse bitmap. This allows a bitmap to
     302              : // function as a sparse array of 4 bit values.
     303              : // QUAD is the index.
     304              : inline int
     305     15425763 : sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
     306              : {
     307     30851526 :   return (int) bitmap_get_aligned_chunk (head, quad, 4);
     308              : }
     309              : 
     310              : // Set the range on entry to basic block BB to R.
     311              : 
     312              : bool
     313       498529 : sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const vrange &r)
     314              : {
     315       498529 :   if (r.undefined_p ())
     316              :     {
     317        29045 :       bitmap_set_quad (&bitvec, bb->index, SBR_UNDEF);
     318        29045 :       return true;
     319              :     }
     320              : 
     321              :   // Loop thru the values to see if R is already present.
     322       879386 :   for (int x = 0; x < SBR_NUM; x++)
     323       868380 :     if (!m_range[x] || m_range[x]->equal_p (r))
     324              :       {
     325       458478 :         if (!m_range[x])
     326       119017 :           m_range[x] = m_range_allocator->clone (r);
     327       458478 :         bitmap_set_quad (&bitvec, bb->index, x + 1);
     328       458478 :         return true;
     329              :       }
     330              :   // All values are taken, default to VARYING.
     331        11006 :   bitmap_set_quad (&bitvec, bb->index, SBR_VARYING);
     332        11006 :   return false;
     333              : }
     334              : 
     335              : // Return the range associated with block BB in R.  Return false if
     336              : // there is no range.
     337              : 
     338              : bool
     339     12928822 : sbr_sparse_bitmap::get_bb_range (vrange &r, const_basic_block bb)
     340              : {
     341     12928822 :   int value = bitmap_get_quad (&bitvec, bb->index);
     342              : 
     343     12928822 :   if (!value)
     344              :     return false;
     345              : 
     346      1955228 :   gcc_checking_assert (value <= SBR_UNDEF);
     347      1955228 :   if (value == SBR_UNDEF)
     348        70272 :     r.set_undefined ();
     349              :   else
     350      1884956 :     m_range[value - 1]->get_vrange (r, m_type);
     351              :   return true;
     352              : }
     353              : 
     354              : // Return true if a range is present.
     355              : 
     356              : bool
     357      2496941 : sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
     358              : {
     359      2496941 :   return (bitmap_get_quad (&bitvec, bb->index) != 0);
     360              : }
     361              : 
     362              : // -------------------------------------------------------------------------
     363              : 
     364              : // Initialize the block cache.
     365              : 
     366     29449302 : block_range_cache::block_range_cache ()
     367              : {
     368     29449302 :   bitmap_obstack_initialize (&m_bitmaps);
     369     29449302 :   m_ssa_ranges.create (0);
     370     58898604 :   m_ssa_ranges.safe_grow_cleared (num_ssa_names);
     371     29449302 :   m_range_allocator = new vrange_allocator;
     372     29449302 : }
     373              : 
     374              : // Remove any m_block_caches which have been created.
     375              : 
     376     29449302 : block_range_cache::~block_range_cache ()
     377              : {
     378     29449302 :   delete m_range_allocator;
     379              :   // Release the vector itself.
     380     29449302 :   m_ssa_ranges.release ();
     381     29449302 :   bitmap_obstack_release (&m_bitmaps);
     382     29449302 : }
     383              : 
     384              : // Clear block info for NAME.
     385              : 
     386              : void
     387          440 : block_range_cache::clear (tree name)
     388              : {
     389          440 :   unsigned v = SSA_NAME_VERSION (name);
     390          440 :   if (v >= m_ssa_ranges.length ())
     391              :     return;
     392          440 :   m_ssa_ranges[v] = NULL;
     393              : }
     394              : 
     395              : // Set the range for NAME on entry to block BB to R.
     396              : // If it has not been accessed yet, allocate it first.
     397              : 
     398              : bool
     399     76362672 : block_range_cache::set_bb_range (tree name, const_basic_block bb,
     400              :                                  const vrange &r)
     401              : {
     402     76362672 :   unsigned v = SSA_NAME_VERSION (name);
     403     76362672 :   if (v >= m_ssa_ranges.length ())
     404            2 :     m_ssa_ranges.safe_grow_cleared (num_ssa_names);
     405              : 
     406     76362672 :   if (!m_ssa_ranges[v])
     407              :     {
     408              :       // Use sparse bitmap representation if there are too many basic blocks.
     409     29066446 :       if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
     410              :         {
     411       105214 :           void *r = m_range_allocator->alloc (sizeof (sbr_sparse_bitmap));
     412       105214 :           m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
     413              :                                                        m_range_allocator,
     414       105214 :                                                        &m_bitmaps);
     415              :         }
     416     28961232 :       else if (last_basic_block_for_fn (cfun) < param_vrp_vector_threshold)
     417              :         {
     418              :           // For small CFGs use the basic vector implementation.
     419     25458727 :           void *r = m_range_allocator->alloc (sizeof (sbr_vector));
     420     25458727 :           m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
     421     25458727 :                                                 m_range_allocator);
     422              :         }
     423              :       else
     424              :         {
     425              :           // Otherwise use the sparse vector implementation.
     426      3502505 :           void *r = m_range_allocator->alloc (sizeof (sbr_lazy_vector));
     427      3502505 :           m_ssa_ranges[v] = new (r) sbr_lazy_vector (TREE_TYPE (name),
     428              :                                                      m_range_allocator,
     429      3502505 :                                                      &m_bitmaps);
     430              :         }
     431              :     }
     432     76362672 :   return m_ssa_ranges[v]->set_bb_range (bb, r);
     433              : }
     434              : 
     435              : 
     436              : // Return a pointer to the ssa_block_cache for NAME.  If it has not been
     437              : // accessed yet, return NULL.
     438              : 
     439              : inline ssa_block_ranges *
     440   1208987073 : block_range_cache::query_block_ranges (tree name)
     441              : {
     442   1208987073 :   unsigned v = SSA_NAME_VERSION (name);
     443   1208987073 :   if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
     444              :     return NULL;
     445    903717122 :   return m_ssa_ranges[v];
     446              : }
     447              : 
     448              : 
     449              : 
     450              : // Return the range for NAME on entry to BB in R.  Return true if there
     451              : // is one.
     452              : 
     453              : bool
     454    813664739 : block_range_cache::get_bb_range (vrange &r, tree name, const_basic_block bb)
     455              : {
     456    813664739 :   ssa_block_ranges *ptr = query_block_ranges (name);
     457    813664739 :   if (ptr)
     458    604201410 :     return ptr->get_bb_range (r, bb);
     459              :   return false;
     460              : }
     461              : 
     462              : // Return true if NAME has a range set in block BB.
     463              : 
     464              : bool
     465    395322334 : block_range_cache::bb_range_p (tree name, const_basic_block bb)
     466              : {
     467    395322334 :   ssa_block_ranges *ptr = query_block_ranges (name);
     468    395322334 :   if (ptr)
     469    299515712 :     return ptr->bb_range_p (bb);
     470              :   return false;
     471              : }
     472              : 
     473              : // Print all known block caches to file F.
     474              : 
     475              : void
     476            0 : block_range_cache::dump (FILE *f)
     477              : {
     478            0 :   unsigned x;
     479            0 :   for (x = 1; x < m_ssa_ranges.length (); ++x)
     480              :     {
     481            0 :       if (m_ssa_ranges[x])
     482              :         {
     483            0 :           fprintf (f, " Ranges for ");
     484            0 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     485            0 :           fprintf (f, ":\n");
     486            0 :           m_ssa_ranges[x]->dump (f);
     487            0 :           fprintf (f, "\n");
     488              :         }
     489              :     }
     490            0 : }
     491              : 
     492              : // Print all known ranges on entry to block BB to file F.
     493              : 
     494              : void
     495          257 : block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
     496              : {
     497          257 :   unsigned x;
     498          257 :   bool summarize_varying = false;
     499        12507 :   for (x = 1; x < m_ssa_ranges.length (); ++x)
     500              :     {
     501        12250 :       if (!m_ssa_ranges[x])
     502        11004 :         continue;
     503              : 
     504         1246 :       if (!gimple_range_ssa_p (ssa_name (x)))
     505            0 :         continue;
     506              : 
     507         1246 :       value_range r (TREE_TYPE (ssa_name (x)));
     508         1246 :       if (m_ssa_ranges[x]->get_bb_range (r, bb))
     509              :         {
     510          222 :           if (!print_varying && r.varying_p ())
     511              :             {
     512            0 :               summarize_varying = true;
     513            0 :               continue;
     514              :             }
     515          222 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     516          222 :           fprintf (f, "\t");
     517          222 :           r.dump(f);
     518          222 :           fprintf (f, "\n");
     519              :         }
     520         1246 :     }
     521              :   // If there were any varying entries, lump them all together.
     522          257 :   if (summarize_varying)
     523              :     {
     524            0 :       fprintf (f, "VARYING_P on entry : ");
     525            0 :       for (x = 1; x < m_ssa_ranges.length (); ++x)
     526              :         {
     527            0 :           if (!m_ssa_ranges[x])
     528            0 :             continue;
     529              : 
     530            0 :           if (!gimple_range_ssa_p (ssa_name (x)))
     531            0 :             continue;
     532              : 
     533            0 :           value_range r (TREE_TYPE (ssa_name (x)));
     534            0 :           if (m_ssa_ranges[x]->get_bb_range (r, bb))
     535              :             {
     536            0 :               if (r.varying_p ())
     537              :                 {
     538            0 :                   print_generic_expr (f, ssa_name (x), TDF_NONE);
     539            0 :                   fprintf (f, "  ");
     540              :                 }
     541              :             }
     542            0 :         }
     543            0 :       fprintf (f, "\n");
     544              :     }
     545          257 : }
     546              : 
     547              : // -------------------------------------------------------------------------
     548              : 
     549              : // Initialize an ssa cache.
     550              : 
     551     38860787 : ssa_cache::ssa_cache ()
     552              : {
     553     38860787 :   m_tab.create (0);
     554     38860787 :   m_range_allocator = new vrange_allocator;
     555     38860787 : }
     556              : 
     557              : // Deconstruct an ssa cache.
     558              : 
     559     38860775 : ssa_cache::~ssa_cache ()
     560              : {
     561     38860775 :   m_tab.release ();
     562     38860775 :   delete m_range_allocator;
     563     38860775 : }
     564              : 
     565              : // Enable a query to evaluate staements/ramnges based on picking up ranges
     566              : // from just an ssa-cache.
     567              : 
     568              : bool
     569         3960 : ssa_cache::range_of_expr (vrange &r, tree expr, gimple *stmt)
     570              : {
     571         3960 :   if (!gimple_range_ssa_p (expr))
     572            0 :     return get_tree_range (r, expr, stmt);
     573              : 
     574         3960 :   if (!get_range (r, expr))
     575           53 :     gimple_range_global (r, expr, cfun);
     576              :   return true;
     577              : }
     578              : 
     579              : // Return TRUE if the global range of NAME has a cache entry.
     580              : 
     581              : bool
     582     11230038 : ssa_cache::has_range (tree name) const
     583              : {
     584     11230038 :   unsigned v = SSA_NAME_VERSION (name);
     585     11230038 :   if (v >= m_tab.length ())
     586              :     return false;
     587     10765528 :   return m_tab[v] != NULL;
     588              : }
     589              : 
     590              : // Retrieve the global range of NAME from cache memory if it exists.
     591              : // Return the value in R.
     592              : 
     593              : bool
     594   1195813123 : ssa_cache::get_range (vrange &r, tree name) const
     595              : {
     596   1195813123 :   unsigned v = SSA_NAME_VERSION (name);
     597   1195813123 :   if (v >= m_tab.length ())
     598              :     return false;
     599              : 
     600   1183755032 :   vrange_storage *stow = m_tab[v];
     601   1183755032 :   if (!stow)
     602              :     return false;
     603    965437619 :   stow->get_vrange (r, TREE_TYPE (name));
     604    965437619 :   return true;
     605              : }
     606              : 
     607              : // Set the range for NAME to R in the ssa cache.
     608              : // Return TRUE if there was already a range set, otherwise false.
     609              : 
     610              : bool
     611    156485148 : ssa_cache::set_range (tree name, const vrange &r)
     612              : {
     613    156485148 :   unsigned v = SSA_NAME_VERSION (name);
     614    156485148 :   if (v >= m_tab.length ())
     615     15858986 :     m_tab.safe_grow_cleared (num_ssa_names + 1);
     616              : 
     617    156485148 :   vrange_storage *m = m_tab[v];
     618    156485148 :   if (m && m->fits_p (r))
     619     22555065 :     m->set_vrange (r);
     620              :   else
     621    133930083 :     m_tab[v] = m_range_allocator->clone (r);
     622    156485148 :   return m != NULL;
     623              : }
     624              : 
     625              : // If NAME has a range, intersect it with R, otherwise set it to R.
     626              : // Return TRUE if the range is new or changes.
     627              : 
     628              : bool
     629          401 : ssa_cache::merge_range (tree name, const vrange &r)
     630              : {
     631          401 :   unsigned v = SSA_NAME_VERSION (name);
     632          401 :   if (v >= m_tab.length ())
     633           18 :     m_tab.safe_grow_cleared (num_ssa_names + 1);
     634              : 
     635          401 :   vrange_storage *m = m_tab[v];
     636              :   // Check if this is a new value.
     637          401 :   if (!m)
     638          400 :     m_tab[v] = m_range_allocator->clone (r);
     639              :   else
     640              :     {
     641            1 :       value_range curr (TREE_TYPE (name));
     642            1 :       m->get_vrange (curr, TREE_TYPE (name));
     643              :       // If there is no change, return false.
     644            1 :       if (!curr.intersect (r))
     645            1 :         return false;
     646              : 
     647            0 :       if (m->fits_p (curr))
     648            0 :         m->set_vrange (curr);
     649              :       else
     650            0 :         m_tab[v] = m_range_allocator->clone (curr);
     651            1 :     }
     652              :   return true;
     653              : }
     654              : 
     655              : // Set the range for NAME to R in the ssa cache.
     656              : 
     657              : void
     658          440 : ssa_cache::clear_range (tree name)
     659              : {
     660          440 :   unsigned v = SSA_NAME_VERSION (name);
     661          440 :   if (v >= m_tab.length ())
     662              :     return;
     663          440 :   m_tab[v] = NULL;
     664              : }
     665              : 
     666              : // Clear the ssa cache.
     667              : 
     668              : void
     669            0 : ssa_cache::clear ()
     670              : {
     671            0 :   if (m_tab.address ())
     672            0 :     memset (m_tab.address(), 0, m_tab.length () * sizeof (vrange *));
     673            0 : }
     674              : 
     675              : // Dump the contents of the ssa cache to F.
     676              : 
     677              : void
     678           64 : ssa_cache::dump (FILE *f)
     679              : {
     680         3223 :   for (unsigned x = 1; x < num_ssa_names; x++)
     681              :     {
     682         3159 :       if (!gimple_range_ssa_p (ssa_name (x)))
     683         1275 :         continue;
     684         1884 :       value_range r (TREE_TYPE (ssa_name (x)));
     685              :       // Dump all non-varying ranges.
     686         1884 :       if (get_range (r, ssa_name (x)) && !r.varying_p ())
     687              :         {
     688          304 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     689          304 :           fprintf (f, "  : ");
     690          304 :           r.dump (f);
     691          304 :           fprintf (f, "\n");
     692              :         }
     693         1884 :     }
     694              : 
     695           64 : }
     696              : 
     697              : // Construct an ssa_lazy_cache. If OB is specified, us it, otherwise use
     698              : // a local bitmap obstack.
     699              : 
     700      9411476 : ssa_lazy_cache::ssa_lazy_cache (bitmap_obstack *ob)
     701              : {
     702      9411476 :   if (!ob)
     703              :     {
     704      9411464 :       bitmap_obstack_initialize (&m_bitmaps);
     705      9411464 :       m_ob = &m_bitmaps;
     706              :     }
     707              :   else
     708           12 :     m_ob = ob;
     709      9411476 :   active_p = BITMAP_ALLOC (m_ob);
     710      9411476 : }
     711              : 
     712              : // Destruct an sa_lazy_cache.  Free the bitmap if it came from a different
     713              : // obstack, or release the obstack if it was a local one.
     714              : 
     715      9411464 : ssa_lazy_cache::~ssa_lazy_cache ()
     716              : {
     717      9411464 :   if (m_ob == &m_bitmaps)
     718      9411464 :     bitmap_obstack_release (&m_bitmaps);
     719              :   else
     720            0 :     BITMAP_FREE (active_p);
     721      9411464 : }
     722              : 
     723              : // Return true if NAME has an active range in the cache.
     724              : 
     725              : bool
     726          719 : ssa_lazy_cache::has_range (tree name) const
     727              : {
     728          719 :   return bitmap_bit_p (active_p, SSA_NAME_VERSION (name));
     729              : }
     730              : 
     731              : // Set range of NAME to R in a lazy cache.  Return FALSE if it did not already
     732              : // have a range.
     733              : 
     734              : bool
     735    115091064 : ssa_lazy_cache::set_range (tree name, const vrange &r)
     736              : {
     737    115091064 :   unsigned v = SSA_NAME_VERSION (name);
     738    115091064 :   if (!bitmap_set_bit (active_p, v))
     739              :     {
     740              :       // There is already an entry, simply set it.
     741     13765046 :       gcc_checking_assert (v < m_tab.length ());
     742     13765046 :       return ssa_cache::set_range (name, r);
     743              :     }
     744    101326018 :   if (v >= m_tab.length ())
     745      5424424 :     m_tab.safe_grow (num_ssa_names + 1);
     746    101326018 :   m_tab[v] = m_range_allocator->clone (r);
     747    101326018 :   return false;
     748              : }
     749              : 
     750              : // If NAME has a range, intersect it with R, otherwise set it to R.
     751              : // Return TRUE if the range is new or changes.
     752              : 
     753              : bool
     754          213 : ssa_lazy_cache::merge_range (tree name, const vrange &r)
     755              : {
     756          213 :   unsigned v = SSA_NAME_VERSION (name);
     757          213 :   if (!bitmap_set_bit (active_p, v))
     758              :     {
     759              :       // There is already an entry, simply merge it.
     760            1 :       gcc_checking_assert (v < m_tab.length ());
     761            1 :       return ssa_cache::merge_range (name, r);
     762              :     }
     763          212 :   if (v >= m_tab.length ())
     764          160 :     m_tab.safe_grow (num_ssa_names + 1);
     765          212 :   m_tab[v] = m_range_allocator->clone (r);
     766          212 :   return true;
     767              : }
     768              : 
     769              : // Merge all elements of CACHE with this cache.
     770              : // Any names in CACHE that are not in this one are added.
     771              : // Any names in both are merged via merge_range..
     772              : 
     773              : void
     774            7 : ssa_lazy_cache::merge (const ssa_lazy_cache &cache)
     775              : {
     776            7 :   unsigned x;
     777            7 :   bitmap_iterator bi;
     778           57 :   EXECUTE_IF_SET_IN_BITMAP (cache.active_p, 0, x, bi)
     779              :     {
     780           50 :       tree name = ssa_name (x);
     781           50 :       value_range r(TREE_TYPE (name));
     782           50 :       cache.get_range (r, name);
     783           50 :       merge_range (ssa_name (x), r);
     784           50 :     }
     785            7 : }
     786              : 
     787              : // Return TRUE if NAME has a range, and return it in R.
     788              : 
     789              : bool
     790    258642159 : ssa_lazy_cache::get_range (vrange &r, tree name) const
     791              : {
     792    258642159 :   if (!bitmap_bit_p (active_p, SSA_NAME_VERSION (name)))
     793              :     return false;
     794    109966641 :   return ssa_cache::get_range (r, name);
     795              : }
     796              : 
     797              : // Remove NAME from the active range list.
     798              : 
     799              : void
     800     60215856 : ssa_lazy_cache::clear_range (tree name)
     801              : {
     802     60215856 :   bitmap_clear_bit (active_p, SSA_NAME_VERSION (name));
     803     60215856 : }
     804              : 
     805              : // Remove all ranges from the active range list.
     806              : 
     807              : void
     808     38174067 : ssa_lazy_cache::clear ()
     809              : {
     810     38174067 :   bitmap_clear (active_p);
     811     38174067 : }
     812              : 
     813              : // --------------------------------------------------------------------------
     814              : 
     815              : // A cache timestamp has two components.
     816              : //
     817              : // STORED and CALC are maintained separately.  STORED is updated only when
     818              : // the cached value actually changes, while CALC is updated every time the
     819              : // value is recalculated.
     820              : //
     821              : // This allows stale values to be recalculated without forcing dependent
     822              : // values to be recalculated as well.  If a recalculation produces the same
     823              : // value, only CALC changes and the STORED timestamp remains unchanged,
     824              : // indicating that the observable value has not changed.
     825              : 
     826              : struct time_stamp
     827              : {
     828              :   unsigned stored;      // Timestamp of last time value was SET.
     829              :   unsigned calc;        // Timestamp when the value was calcuclated last.
     830              : };
     831              : 
     832              : // Manage dependency timestamps for SSA names.
     833              : //
     834              : // Each SSA name records when its value last changed (stored) and when it
     835              : // was last recalculated (calc).  Dependencies are current if their stored
     836              : // timestamps are no newer than the dependent value.  Recalculating a value
     837              : // without changing it updates only the calc timestamp, avoiding unnecessary
     838              : // invalidation of dependent values.
     839              : // always_current is managed by setting the calcualted timestamp to 0.
     840              : 
     841              : class temporal_cache
     842              : {
     843              : public:
     844              :   temporal_cache ();
     845              :   ~temporal_cache ();
     846              :   bool current_p (tree name, tree dep1, tree dep2) const;
     847              :   void set_timestamp_stored (tree name);
     848              :   void set_timestamp_calc (tree name);
     849              :   void set_always_current (tree name);
     850              :   bool always_current_p (tree name) const;
     851              : private:
     852              :   unsigned temporal_value_stored (unsigned ssa) const;
     853              :   unsigned temporal_value_calc (unsigned ssa) const;
     854              :   unsigned m_current_time;
     855              :   vec <struct time_stamp> m_timestamp;
     856              : };
     857              : 
     858              : inline
     859     29449302 : temporal_cache::temporal_cache ()
     860              : {
     861     29449302 :   m_current_time = 1;
     862     29449302 :   m_timestamp.create (0);
     863     58898604 :   m_timestamp.safe_grow_cleared (num_ssa_names + 1);
     864     29449302 : }
     865              : 
     866              : inline
     867     29449302 : temporal_cache::~temporal_cache ()
     868              : {
     869     29449302 :   m_timestamp.release ();
     870     29449302 : }
     871              : 
     872              : // Return the timestamp value for SSA when it was last stored to
     873              : // or 0 if there isn't one.
     874              : 
     875              : inline unsigned
     876    158976938 : temporal_cache::temporal_value_stored (unsigned ssa) const
     877              : {
     878    158976938 :   if (ssa >= m_timestamp.length ())
     879              :     return 0;
     880    158976938 :   return m_timestamp[ssa].stored;
     881              : }
     882              : 
     883              : // Return the timestamp value for SSA when it was last calculated
     884              : // or 0 if there isn't one.
     885              : 
     886              : inline unsigned
     887    222756149 : temporal_cache::temporal_value_calc (unsigned ssa) const
     888              : {
     889    222756149 :   if (ssa >= m_timestamp.length ())
     890              :     return 0;
     891    222756149 :   return m_timestamp[ssa].calc;
     892              : }
     893              : 
     894              : // Return TRUE if the timestamp for when NAME was calculated is newer
     895              : // than the last time any of its dependents were stored.  This indicates
     896              : // it dos not need to be calculated again.
     897              : // Up to 2 dependencies can be checked.
     898              : 
     899              : bool
     900    229509097 : temporal_cache::current_p (tree name, tree dep1, tree dep2) const
     901              : {
     902    229509097 :   if (always_current_p (name))
     903              :     return true;
     904              : 
     905              :   // Any non-registered dependencies will have a value of 0 and thus be older.
     906              :   // Return true if the last time this was calculated is newer than either
     907              :   // dependent value.
     908    222756149 :   unsigned ts = temporal_value_calc (SSA_NAME_VERSION (name));
     909    341141499 :   if (dep1 && ts < temporal_value_stored (SSA_NAME_VERSION (dep1)))
     910              :     return false;
     911    259204102 :   if (dep2 && ts < temporal_value_stored (SSA_NAME_VERSION (dep2)))
     912       458968 :     return false;
     913              : 
     914              :   return true;
     915              : }
     916              : 
     917              : // This increments the global timer and sets both timestamps for NAME.
     918              : 
     919              : inline void
     920     77574616 : temporal_cache::set_timestamp_stored (tree name)
     921              : {
     922     77574616 :   unsigned v = SSA_NAME_VERSION (name);
     923     77574616 :   if (v >= m_timestamp.length ())
     924            0 :     m_timestamp.safe_grow_cleared (num_ssa_names + 20);
     925     77574616 :   m_timestamp[v].stored = ++m_current_time;
     926     77574616 :   m_timestamp[v].calc = m_current_time;
     927     77574616 : }
     928              : 
     929              : // This increments the global timer and sets the calculated timestamp for NAME.
     930              : 
     931              : inline void
     932    124118323 : temporal_cache::set_timestamp_calc (tree name)
     933              : {
     934    124118323 :   unsigned v = SSA_NAME_VERSION (name);
     935    124118323 :   if (v >= m_timestamp.length ())
     936            0 :     m_timestamp.safe_grow_cleared (num_ssa_names + 20);
     937    124118323 :   m_timestamp[v].calc = ++m_current_time;
     938    124118323 : }
     939              : 
     940              : // Set the calculated timestamp to 0, marking it as "always up to date".
     941              : 
     942              : inline void
     943    136618983 : temporal_cache::set_always_current (tree name)
     944              : {
     945    136618983 :   unsigned v = SSA_NAME_VERSION (name);
     946    136618983 :   if (v >= m_timestamp.length ())
     947         1370 :     m_timestamp.safe_grow_cleared (num_ssa_names + 20);
     948              :   // If stored timestamp hasn't been set, set it now.
     949    136618983 :   if (m_timestamp[v].stored == 0)
     950    129835114 :     m_timestamp[v].stored = ++m_current_time;
     951    136618983 :   m_timestamp[v].calc = 0;
     952    136618983 : }
     953              : 
     954              : // Return true if NAME is always current.
     955              : 
     956              : inline bool
     957    229509097 : temporal_cache::always_current_p (tree name) const
     958              : {
     959    229509097 :   unsigned v = SSA_NAME_VERSION (name);
     960    229509097 :   if (v >= m_timestamp.length ())
     961              :     return false;
     962    229509097 :   return m_timestamp[v].calc == 0;
     963              : }
     964              : 
     965              : // --------------------------------------------------------------------------
     966              : 
     967              : // This class provides an abstraction of a list of blocks to be updated
     968              : // by the cache.  It is currently a stack but could be changed.  It also
     969              : // maintains a list of blocks which have failed propagation, and does not
     970              : // enter any of those blocks into the list.
     971              : 
     972              : // A vector over the BBs is maintained, and an entry of 0 means it is not in
     973              : // a list.  Otherwise, the entry is the next block in the list. -1 terminates
     974              : // the list.  m_head points to the top of the list, -1 if the list is empty.
     975              : 
     976              : class update_list
     977              : {
     978              : public:
     979              :   update_list ();
     980              :   ~update_list ();
     981              :   void add (basic_block bb);
     982              :   basic_block pop ();
     983    157435541 :   inline bool empty_p () { return m_update_head == -1; }
     984      5956374 :   inline void clear_failures () { bitmap_clear (m_propfail); }
     985            3 :   inline void propagation_failed (basic_block bb)
     986            3 :                                   { bitmap_set_bit (m_propfail, bb->index); }
     987              : private:
     988              :   vec<int> m_update_list;
     989              :   int m_update_head;
     990              :   bitmap m_propfail;
     991              :   bitmap_obstack m_bitmaps;
     992              : };
     993              : 
     994              : // Create an update list.
     995              : 
     996     29449302 : update_list::update_list ()
     997              : {
     998     29449302 :   m_update_list.create (0);
     999     29449302 :   m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun) + 64);
    1000     29449302 :   m_update_head = -1;
    1001     29449302 :   bitmap_obstack_initialize (&m_bitmaps);
    1002     29449302 :   m_propfail = BITMAP_ALLOC (&m_bitmaps);
    1003     29449302 : }
    1004              : 
    1005              : // Destroy an update list.
    1006              : 
    1007     29449302 : update_list::~update_list ()
    1008              : {
    1009     29449302 :   m_update_list.release ();
    1010     29449302 :   bitmap_obstack_release (&m_bitmaps);
    1011     29449302 : }
    1012              : 
    1013              : // Add BB to the list of blocks to update, unless it's already in the list.
    1014              : 
    1015              : void
    1016     13420290 : update_list::add (basic_block bb)
    1017              : {
    1018     13420290 :   int i = bb->index;
    1019              :   // If propagation has failed for BB, or its already in the list, don't
    1020              :   // add it again.
    1021     13420290 :   if ((unsigned)i >= m_update_list.length ())
    1022           68 :     m_update_list.safe_grow_cleared (i + 64);
    1023     13420290 :   if (!m_update_list[i] && !bitmap_bit_p (m_propfail, i))
    1024              :     {
    1025     12718689 :       if (empty_p ())
    1026              :         {
    1027      7293830 :           m_update_head = i;
    1028      7293830 :           m_update_list[i] = -1;
    1029              :         }
    1030              :       else
    1031              :         {
    1032      5424859 :           gcc_checking_assert (m_update_head > 0);
    1033      5424859 :           m_update_list[i] = m_update_head;
    1034      5424859 :           m_update_head = i;
    1035              :         }
    1036              :     }
    1037     13420290 : }
    1038              : 
    1039              : // Remove a block from the list.
    1040              : 
    1041              : basic_block
    1042     12718689 : update_list::pop ()
    1043              : {
    1044     12718689 :   gcc_checking_assert (!empty_p ());
    1045     12718689 :   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, m_update_head);
    1046     12718689 :   int pop = m_update_head;
    1047     12718689 :   m_update_head = m_update_list[pop];
    1048     12718689 :   m_update_list[pop] = 0;
    1049     12718689 :   return bb;
    1050              : }
    1051              : 
    1052              : // --------------------------------------------------------------------------
    1053              : 
    1054     29449302 : ranger_cache::ranger_cache (int not_executable_flag, bool use_imm_uses)
    1055              : {
    1056     29449302 :   m_workback = vNULL;
    1057     29449302 :   m_temporal = new temporal_cache;
    1058              : 
    1059              :   // If DOM info is available, spawn an oracle as well.
    1060     29449302 :   create_relation_oracle ();
    1061              :   // Create an infer oracle using this cache as the range query.  The cache
    1062              :   // version acts as a read-only query, and will spawn no additional lookups.
    1063              :   // It just ues what is already known.
    1064     29449302 :   create_infer_oracle (this, use_imm_uses);
    1065     29449302 :   create_gori (not_executable_flag, param_vrp_switch_limit);
    1066              : 
    1067     29449302 :   unsigned x, lim = last_basic_block_for_fn (cfun);
    1068              :   // Calculate outgoing range info upfront.  This will fully populate the
    1069              :   // m_maybe_variant bitmap which will help eliminate processing of names
    1070              :   // which never have their ranges adjusted.
    1071    378361744 :   for (x = 0; x < lim ; x++)
    1072              :     {
    1073    348912442 :       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
    1074    348912442 :       if (bb)
    1075    329826479 :         gori_ssa ()->exports (bb);
    1076              :     }
    1077     29449302 :   m_update = new update_list ();
    1078     29449302 :   m_stale = BITMAP_ALLOC (NULL);
    1079     29449302 : }
    1080              : 
    1081     29449302 : ranger_cache::~ranger_cache ()
    1082              : {
    1083     29449302 :   BITMAP_FREE (m_stale);
    1084     29449302 :   delete m_update;
    1085     29449302 :   destroy_infer_oracle ();
    1086     29449302 :   destroy_relation_oracle ();
    1087     58898604 :   delete m_temporal;
    1088     29449302 :   m_workback.release ();
    1089     29449302 : }
    1090              : 
    1091              : // Dump the global caches to file F.  if GORI_DUMP is true, dump the
    1092              : // gori map as well.
    1093              : 
    1094              : void
    1095           48 : ranger_cache::dump (FILE *f)
    1096              : {
    1097           48 :   fprintf (f, "Non-varying global ranges:\n");
    1098           48 :   fprintf (f, "=========================:\n");
    1099           48 :   m_globals.dump (f);
    1100           48 :   fprintf (f, "\n");
    1101           48 : }
    1102              : 
    1103              : // Dump the caches for basic block BB to file F.
    1104              : 
    1105              : void
    1106          257 : ranger_cache::dump_bb (FILE *f, basic_block bb)
    1107              : {
    1108          257 :   gori_ssa ()->dump (f, bb, false);
    1109          257 :   m_on_entry.dump (f, bb);
    1110          257 :   m_relation->dump (f, bb);
    1111          257 : }
    1112              : 
    1113              : // Get the global range for NAME, and return in R.  Return false if the
    1114              : // global range is not set, and return the legacy global value in R.
    1115              : 
    1116              : bool
    1117    864849567 : ranger_cache::get_global_range (vrange &r, tree name) const
    1118              : {
    1119    864849567 :   if (m_globals.get_range (r, name))
    1120              :     return true;
    1121    199563306 :   gimple_range_global (r, name);
    1122    199563306 :   return false;
    1123              : }
    1124              : 
    1125              : // Mark NAME as stale.  The next query of NAME forces a recalculation.
    1126              : 
    1127              : void
    1128     12839343 : ranger_cache::mark_stale (tree name)
    1129              : {
    1130     12839343 :   if (SSA_NAME_IS_DEFAULT_DEF (name))
    1131              :     {
    1132              :       // Default defs have no DEF to recalculate, just create a new timestamp.
    1133      1610096 :       m_temporal->set_timestamp_stored (name);
    1134              :     }
    1135     11229247 :   else if (m_globals.has_range (name))
    1136              :     {
    1137              :       // Otherwise Only mark it as stale if it has been processed. If it has no
    1138              :       // range it will be calculated at the next request anyway.
    1139      8248002 :       bitmap_set_bit (m_stale, SSA_NAME_VERSION (name));
    1140              :     }
    1141     12839343 : }
    1142              : 
    1143              : // Get the global range for NAME, and return in R.  Return false if the
    1144              : // global range is not set, and R will contain the legacy global value.
    1145              : // CURRENT_P is set to true if the value was in cache and not stale.
    1146              : // Otherwise, set CURRENT_P to false and mark as it always current.
    1147              : // If the global cache did not have a value, initialize it as well.
    1148              : // After this call, the global cache will have a value.
    1149              : 
    1150              : bool
    1151    359576677 : ranger_cache::get_global_range (vrange &r, tree name, bool &current_p)
    1152              : {
    1153    359576677 :   bool had_global = get_global_range (r, name);
    1154              : 
    1155              :   // If there was a global value, set current flag, otherwise set a value.
    1156    359576677 :   current_p = false;
    1157    359576677 :   if (had_global)
    1158    459471938 :     current_p = r.singleton_p ()
    1159    459245066 :                 || m_temporal->current_p (name, gori_ssa ()->depend1 (name),
    1160    229509097 :                                           gori_ssa ()->depend2 (name));
    1161              :   else
    1162              :     {
    1163              :       // If no global value has been set and value is VARYING, fold the stmt
    1164              :       // using just global ranges to get a better initial value.
    1165              :       // After inlining we tend to decide some things are constant, so
    1166              :       // so not do this evaluation after inlining.
    1167    129840708 :       if (r.varying_p () && !cfun->after_inlining)
    1168              :         {
    1169     21253350 :           gimple *s = SSA_NAME_DEF_STMT (name);
    1170              :           // Do not process PHIs as SCEV may be in use and it can
    1171              :           // spawn cyclic lookups.
    1172     21253350 :           if (gimple_get_lhs (s) == name && !is_a<gphi *> (s))
    1173              :             {
    1174     16633664 :               if (!fold_range (r, s, get_global_range_query ()))
    1175            0 :                 gimple_range_global (r, name);
    1176              :             }
    1177              :         }
    1178    129840708 :       m_globals.set_range (name, r);
    1179              :     }
    1180              : 
    1181              :   // If NAME is out of date, clear the bit and mark as not current.
    1182    359576677 :   if (bitmap_bit_p (m_stale, SSA_NAME_VERSION (name)))
    1183              :     {
    1184      2217272 :       bitmap_clear_bit (m_stale, SSA_NAME_VERSION (name));
    1185      2217272 :       current_p = false;
    1186              :     }
    1187              : 
    1188              :   // If the existing value was not current, mark it as always current.
    1189    359576677 :   if (!current_p)
    1190    136618983 :     m_temporal->set_always_current (name);
    1191    359576677 :   return had_global;
    1192              : }
    1193              : 
    1194              : // Consumers of NAME that have already calculated values should recalculate.
    1195              : // Accomplished by updating the timestamp.
    1196              : 
    1197              : void
    1198     63085126 : ranger_cache::update_consumers (tree name)
    1199              : {
    1200     63085126 :   m_temporal->set_timestamp_stored (name);
    1201     63085126 : }
    1202              : 
    1203              : //  Set the global range of NAME to R and give it a timestamp.
    1204              : 
    1205              : void
    1206    136997717 : ranger_cache::set_global_range (tree name, const vrange &r, bool changed)
    1207              : {
    1208    136997717 :   if (!changed)
    1209              :     {
    1210              :       // If the value did not change, simply update the calculated timestamp.
    1211    124118323 :       m_temporal->set_timestamp_calc (name);
    1212    124118323 :       return;
    1213              :     }
    1214     12879394 :   if (m_globals.set_range (name, r))
    1215              :     {
    1216              :       // If there was already a range set, propagate the new value.
    1217     12822402 :       basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1218     12822402 :       if (!bb)
    1219         1558 :         bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1220              : 
    1221     12822402 :       if (DEBUG_RANGE_CACHE)
    1222            0 :         fprintf (dump_file, "   GLOBAL :");
    1223              : 
    1224     12822402 :       propagate_updated_value (name, bb);
    1225              :     }
    1226              :   // Constants no longer need to tracked.  Any further refinement has to be
    1227              :   // undefined. Propagation works better with constants. PR 100512.
    1228              :   // Pointers which resolve to non-zero also do not need
    1229              :   // tracking in the cache as they will never change.  See PR 98866.
    1230              :   // Timestamp must always be updated, or dependent calculations may
    1231              :   // not include this latest value. PR 100774.
    1232              : 
    1233              :   // With Points_to info in prange now, it is no longer acceptable to make
    1234              :   // [1, +INF] invariant, as most points to values will have that range,
    1235              :   // and then we lose the ability to propagate points to info.
    1236              : 
    1237     12879394 :   if (r.singleton_p ())
    1238       863444 :     gori_ssa ()->set_range_invariant (name);
    1239              : 
    1240              :   // update the stored and calucalted timestamp now.
    1241     12879394 :   m_temporal->set_timestamp_stored (name);
    1242              : }
    1243              : 
    1244              : //  Provide lookup for the gori-computes class to access the best known range
    1245              : //  of an ssa_name in any given basic block.  Note, this does no additional
    1246              : //  lookups, just accesses the data that is already known.
    1247              : 
    1248              : // Get the range of NAME when the def occurs in block BB.  If BB is NULL
    1249              : // get the best global value available.
    1250              : 
    1251              : void
    1252    220992485 : ranger_cache::range_of_def (vrange &r, tree name, basic_block bb)
    1253              : {
    1254    220992485 :   gcc_checking_assert (gimple_range_ssa_p (name));
    1255    369282460 :   gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
    1256              : 
    1257              :   // Pick up the best global range available.
    1258    220992485 :   if (!m_globals.get_range (r, name))
    1259              :     {
    1260              :       // If that fails, try to calculate the range using just global values.
    1261     30812144 :       gimple *s = SSA_NAME_DEF_STMT (name);
    1262     30812144 :       if (gimple_get_lhs (s) == name)
    1263     27357019 :         fold_range (r, s, get_global_range_query ());
    1264              :       else
    1265      3455125 :         gimple_range_global (r, name);
    1266              :     }
    1267    220992485 : }
    1268              : 
    1269              : // Get the range of NAME as it occurs on entry to block BB.  Use MODE for
    1270              : // lookups.
    1271              : 
    1272              : void
    1273    161227793 : ranger_cache::entry_range (vrange &r, tree name, basic_block bb,
    1274              :                            enum rfd_mode mode)
    1275              : {
    1276    161227793 :   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1277              :     {
    1278            0 :       gimple_range_global (r, name);
    1279            0 :       return;
    1280              :     }
    1281              : 
    1282              :   // If NAME is invariant, simply return the defining range.
    1283    161227793 :   if (!gori ().has_edge_range_p (name))
    1284              :     {
    1285     33553735 :       range_of_def (r, name);
    1286     33553735 :       return;
    1287              :     }
    1288              : 
    1289              :   // Look for the on-entry value of name in BB from the cache.
    1290              :   // Otherwise pick up the best available global value.
    1291    127674058 :   if (!m_on_entry.get_bb_range (r, name, bb))
    1292     45925051 :     if (!range_from_dom (r, name, bb, mode))
    1293     39148775 :       range_of_def (r, name);
    1294              : }
    1295              : 
    1296              : // Get the range of NAME as it occurs on exit from block BB.  Use MODE for
    1297              : // lookups.
    1298              : 
    1299              : void
    1300    112107832 : ranger_cache::exit_range (vrange &r, tree name, basic_block bb,
    1301              :                           enum rfd_mode mode)
    1302              : {
    1303    112107832 :   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1304              :     {
    1305        62174 :       gimple_range_global (r, name);
    1306        62174 :       return;
    1307              :     }
    1308              : 
    1309    112045658 :   gimple *s = SSA_NAME_DEF_STMT (name);
    1310    112045658 :   basic_block def_bb = gimple_bb (s);
    1311    112045658 :   if (def_bb == bb)
    1312     45287289 :     range_of_def (r, name, bb);
    1313              :   else
    1314     66758369 :     entry_range (r, name, bb, mode);
    1315              : }
    1316              : 
    1317              : // Get the range of NAME on edge E using MODE, return the result in R.
    1318              : // Always returns a range and true.
    1319              : 
    1320              : bool
    1321    101496446 : ranger_cache::edge_range (vrange &r, edge e, tree name, enum rfd_mode mode)
    1322              : {
    1323    101496446 :   exit_range (r, name, e->src, mode);
    1324              :   // If this is not an abnormal edge, check for inferred ranges on exit.
    1325    101496446 :   if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
    1326    101170202 :     infer_oracle ().maybe_adjust_range (r, name, e->src);
    1327    101496446 :   value_range er (TREE_TYPE (name));
    1328    101496446 :   if (gori ().edge_range_p (er, e, name, *this))
    1329     24070230 :     r.intersect (er);
    1330    202992892 :   return true;
    1331    101496446 : }
    1332              : 
    1333              : 
    1334              : 
    1335              : // Implement range_of_expr.
    1336              : 
    1337              : bool
    1338    240214644 : ranger_cache::range_of_expr (vrange &r, tree name, gimple *stmt)
    1339              : {
    1340    240214644 :   if (!gimple_range_ssa_p (name))
    1341     42742164 :     get_tree_range (r, name, stmt);
    1342              :   /* If no context is provided, pick up the global value.  */
    1343    197472480 :   else if (!stmt)
    1344          370 :     get_global_range (r, name);
    1345              :   else
    1346              :     {
    1347    197472110 :       basic_block bb = gimple_bb (stmt);
    1348    197472110 :       gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1349    197472110 :       basic_block def_bb = gimple_bb (def_stmt);
    1350              : 
    1351    197472110 :       if (bb == def_bb)
    1352    103002686 :         range_of_def (r, name, bb);
    1353              :       else
    1354     94469424 :         entry_range (r, name, bb, RFD_NONE);
    1355              :     }
    1356    240214644 :   return true;
    1357              : }
    1358              : 
    1359              : 
    1360              : // Implement range_on_edge.  Always return the best available range using
    1361              : // the current cache values.
    1362              : 
    1363              : bool
    1364     76388627 : ranger_cache::range_on_edge (vrange &r, edge e, tree expr)
    1365              : {
    1366     76388627 :   if (gimple_range_ssa_p (expr))
    1367     72828856 :     return edge_range (r, e, expr, RFD_NONE);
    1368      3559771 :   return get_tree_range (r, expr, NULL);
    1369              : }
    1370              : 
    1371              : // Return a static range for NAME on entry to basic block BB in R.  If
    1372              : // calc is true, fill any cache entries required between BB and the
    1373              : // def block for NAME.  Otherwise, return false if the cache is empty.
    1374              : 
    1375              : bool
    1376    411133667 : ranger_cache::block_range (vrange &r, basic_block bb, tree name, bool calc)
    1377              : {
    1378    411133667 :   gcc_checking_assert (gimple_range_ssa_p (name));
    1379              : 
    1380              :   // If there are no range calculations anywhere in the IL, global range
    1381              :   // applies everywhere, so don't bother caching it.
    1382    411133667 :   if (!gori ().has_edge_range_p (name))
    1383              :     return false;
    1384              : 
    1385    259506619 :   if (calc)
    1386              :     {
    1387    127798513 :       gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1388    127798513 :       basic_block def_bb = NULL;
    1389    127798513 :       if (def_stmt)
    1390    127798513 :         def_bb = gimple_bb (def_stmt);
    1391    127798513 :       if (!def_bb)
    1392              :         {
    1393              :           // If we get to the entry block, this better be a default def
    1394              :           // or range_on_entry was called for a block not dominated by
    1395              :           // the def.  But it could be also SSA_NAME defined by a statement
    1396              :           // not yet in the IL (such as queued edge insertion), in that case
    1397              :           // just punt.
    1398     16843457 :           if (!SSA_NAME_IS_DEFAULT_DEF (name))
    1399              :             return false;
    1400     16843456 :           def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1401              :         }
    1402              : 
    1403              :       // There is no range on entry for the definition block.
    1404    127798512 :       if (def_bb == bb)
    1405              :         return false;
    1406              : 
    1407              :       // Otherwise, go figure out what is known in predecessor blocks.
    1408    127798505 :       fill_block_cache (name, bb, def_bb);
    1409    127798505 :       gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
    1410              :     }
    1411    259506611 :   return m_on_entry.get_bb_range (r, name, bb);
    1412              : }
    1413              : 
    1414              : // If there is anything in the propagation update_list, continue
    1415              : // processing NAME until the list of blocks is empty.
    1416              : 
    1417              : void
    1418      5956374 : ranger_cache::propagate_cache (tree name)
    1419              : {
    1420      5956374 :   basic_block bb;
    1421      5956374 :   edge_iterator ei;
    1422      5956374 :   edge e;
    1423      5956374 :   tree type = TREE_TYPE (name);
    1424      5956374 :   value_range new_range (type);
    1425      5956374 :   value_range current_range (type);
    1426      5956374 :   value_range e_range (type);
    1427              : 
    1428              :   // Process each block by seeing if its calculated range on entry is
    1429              :   // the same as its cached value. If there is a difference, update
    1430              :   // the cache to reflect the new value, and check to see if any
    1431              :   // successors have cache entries which may need to be checked for
    1432              :   // updates.
    1433              : 
    1434     24631437 :   while (!m_update->empty_p ())
    1435              :     {
    1436     12718689 :       bb = m_update->pop ();
    1437     12718689 :       gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
    1438     12718689 :       m_on_entry.get_bb_range (current_range, name, bb);
    1439              : 
    1440     12718689 :       if (DEBUG_RANGE_CACHE)
    1441              :         {
    1442            0 :           fprintf (dump_file, "FWD visiting block %d for ", bb->index);
    1443            0 :           print_generic_expr (dump_file, name, TDF_SLIM);
    1444            0 :           fprintf (dump_file, "  starting range : ");
    1445            0 :           current_range.dump (dump_file);
    1446            0 :           fprintf (dump_file, "\n");
    1447              :         }
    1448              : 
    1449              :       // Calculate the "new" range on entry by unioning the pred edges.
    1450     12718689 :       new_range.set_undefined ();
    1451     27270231 :       FOR_EACH_EDGE (e, ei, bb->preds)
    1452              :         {
    1453     17949892 :           edge_range (e_range, e, name, RFD_READ_ONLY);
    1454     17949892 :           if (DEBUG_RANGE_CACHE)
    1455              :             {
    1456            0 :               fprintf (dump_file, "   edge %d->%d :", e->src->index, bb->index);
    1457            0 :               e_range.dump (dump_file);
    1458            0 :               fprintf (dump_file, "\n");
    1459              :             }
    1460     17949892 :           new_range.union_ (e_range);
    1461     17949892 :           if (new_range.varying_p ())
    1462              :             break;
    1463              :         }
    1464              : 
    1465              :       // If the range on entry has changed, update it.
    1466     12718689 :       if (new_range != current_range)
    1467              :         {
    1468      7227496 :           bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
    1469              :           // If the cache couldn't set the value, mark it as failed.
    1470      7227496 :           if (!ok_p)
    1471            3 :             m_update->propagation_failed (bb);
    1472      7227496 :           if (DEBUG_RANGE_CACHE)
    1473              :             {
    1474            0 :               if (!ok_p)
    1475              :                 {
    1476            0 :                   fprintf (dump_file, "   Cache failure to store value:");
    1477            0 :                   print_generic_expr (dump_file, name, TDF_SLIM);
    1478            0 :                   fprintf (dump_file, "  ");
    1479              :                 }
    1480              :               else
    1481              :                 {
    1482            0 :                   fprintf (dump_file, "      Updating range to ");
    1483            0 :                   new_range.dump (dump_file);
    1484              :                 }
    1485            0 :               fprintf (dump_file, "\n      Updating blocks :");
    1486              :             }
    1487              :           // Mark each successor that has a range to re-check its range
    1488     18576847 :           FOR_EACH_EDGE (e, ei, bb->succs)
    1489     11349351 :             if (m_on_entry.bb_range_p (name, e->dest))
    1490              :               {
    1491      6850425 :                 if (DEBUG_RANGE_CACHE)
    1492            0 :                   fprintf (dump_file, " bb%d",e->dest->index);
    1493      6850425 :                 m_update->add (e->dest);
    1494              :               }
    1495      7227496 :           if (DEBUG_RANGE_CACHE)
    1496            0 :             fprintf (dump_file, "\n");
    1497              :         }
    1498              :     }
    1499      5956374 :   if (DEBUG_RANGE_CACHE)
    1500              :     {
    1501            0 :       fprintf (dump_file, "DONE visiting blocks for ");
    1502            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1503            0 :       fprintf (dump_file, "\n");
    1504              :     }
    1505      5956374 :   m_update->clear_failures ();
    1506      5956374 : }
    1507              : 
    1508              : // Check to see if an update to the value for NAME in BB has any effect
    1509              : // on values already in the on-entry cache for successor blocks.
    1510              : // If it does, update them.  Don't visit any blocks which don't have a cache
    1511              : // entry.
    1512              : 
    1513              : void
    1514     56199661 : ranger_cache::propagate_updated_value (tree name, basic_block bb)
    1515              : {
    1516     56199661 :   edge e;
    1517     56199661 :   edge_iterator ei;
    1518              : 
    1519              :   // The update work list should be empty at this point.
    1520     56199661 :   gcc_checking_assert (m_update->empty_p ());
    1521     56199661 :   gcc_checking_assert (bb);
    1522              : 
    1523     56199661 :   if (DEBUG_RANGE_CACHE)
    1524              :     {
    1525            0 :       fprintf (dump_file, " UPDATE cache for ");
    1526            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1527            0 :       fprintf (dump_file, " in BB %d : successors : ", bb->index);
    1528              :     }
    1529    163048022 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1530              :     {
    1531              :       // Only update active cache entries.
    1532    106848361 :       if (m_on_entry.bb_range_p (name, e->dest))
    1533              :         {
    1534      5105250 :           m_update->add (e->dest);
    1535      5105250 :           if (DEBUG_RANGE_CACHE)
    1536            0 :             fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
    1537              :         }
    1538              :     }
    1539     56199661 :     if (!m_update->empty_p ())
    1540              :       {
    1541      5032596 :         if (DEBUG_RANGE_CACHE)
    1542            0 :           fprintf (dump_file, "\n");
    1543      5032596 :         propagate_cache (name);
    1544              :       }
    1545              :     else
    1546              :       {
    1547     51167065 :         if (DEBUG_RANGE_CACHE)
    1548            0 :           fprintf (dump_file, "  : No updates!\n");
    1549              :       }
    1550     56199661 : }
    1551              : 
    1552              : // Make sure that the range-on-entry cache for NAME is set for block BB.
    1553              : // Work back through the CFG to DEF_BB ensuring the range is calculated
    1554              : // on the block/edges leading back to that point.
    1555              : 
    1556              : void
    1557    127798505 : ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
    1558              : {
    1559    127798505 :   edge_iterator ei;
    1560    127798505 :   edge e;
    1561    127798505 :   tree type = TREE_TYPE (name);
    1562    127798505 :   value_range block_result (type);
    1563    127798505 :   value_range undefined (type);
    1564              : 
    1565              :   // At this point we shouldn't be looking at the def, entry block.
    1566    127798505 :   gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
    1567    127798505 :   unsigned start_length = m_workback.length ();
    1568              : 
    1569              :   // If the block cache is set, then we've already visited this block.
    1570    127798505 :   if (m_on_entry.bb_range_p (name, bb))
    1571              :     return;
    1572              : 
    1573     54174121 :   if (DEBUG_RANGE_CACHE)
    1574              :     {
    1575            0 :       fprintf (dump_file, "\n");
    1576            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1577            0 :       fprintf (dump_file, " : ");
    1578              :     }
    1579              : 
    1580              :   // Check if a dominators can supply the range.
    1581     54174121 :   if (range_from_dom (block_result, name, bb, RFD_FILL))
    1582              :     {
    1583     53250343 :       if (DEBUG_RANGE_CACHE)
    1584              :         {
    1585            0 :           fprintf (dump_file, "Filled from dominator! :  ");
    1586            0 :           block_result.dump (dump_file);
    1587            0 :           fprintf (dump_file, "\n");
    1588              :         }
    1589              :       // See if any equivalences can refine it.
    1590              :       // PR 109462, like 108139 below, a one way equivalence introduced
    1591              :       // by a PHI node can also be through the definition side.  Disallow it.
    1592     53250343 :       tree equiv_name;
    1593     53250343 :       relation_kind rel;
    1594     53250343 :       int prec = TYPE_PRECISION (type);
    1595              :       // If there are too many basic blocks, do not attempt to process
    1596              :       // equivalencies.
    1597     53250343 :       if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
    1598              :         {
    1599       420705 :           m_on_entry.set_bb_range (name, bb, block_result);
    1600       841378 :           gcc_checking_assert (m_workback.length () == start_length);
    1601              :           return;
    1602              :         }
    1603     62804129 :       FOR_EACH_PARTIAL_AND_FULL_EQUIV (m_relation, bb, name, equiv_name, rel)
    1604              :         {
    1605      9974491 :           basic_block equiv_bb = gimple_bb (SSA_NAME_DEF_STMT (equiv_name));
    1606              : 
    1607              :           // Ignore partial equivs that are smaller than this object.
    1608     17641254 :           if (rel != VREL_EQ && prec > pe_to_bits (rel))
    1609      3697069 :             continue;
    1610              : 
    1611              :           // Check if the equiv has any ranges calculated.
    1612      8904147 :           if (!gori ().has_edge_range_p (equiv_name))
    1613       387255 :             continue;
    1614              : 
    1615              :           // Check if the equiv definition dominates this block
    1616      8516892 :           if (equiv_bb == bb ||
    1617      8290930 :               (equiv_bb && !dominated_by_p (CDI_DOMINATORS, bb, equiv_bb)))
    1618      2239470 :             continue;
    1619              : 
    1620      6277422 :           if (DEBUG_RANGE_CACHE)
    1621              :             {
    1622            0 :               if (rel == VREL_EQ)
    1623            0 :                 fprintf (dump_file, "Checking Equivalence (");
    1624              :               else
    1625            0 :                 fprintf (dump_file, "Checking Partial equiv (");
    1626            0 :               print_relation (dump_file, rel);
    1627            0 :               fprintf (dump_file, ") ");
    1628            0 :               print_generic_expr (dump_file, equiv_name, TDF_SLIM);
    1629            0 :               fprintf (dump_file, "\n");
    1630              :             }
    1631      6277422 :           value_range equiv_range (TREE_TYPE (equiv_name));
    1632      6277422 :           if (range_from_dom (equiv_range, equiv_name, bb, RFD_READ_ONLY))
    1633              :             {
    1634      6277422 :               if (rel != VREL_EQ)
    1635      4249903 :                 range_cast (equiv_range, type);
    1636              :               else
    1637      2027519 :                 adjust_equivalence_range (equiv_range);
    1638              : 
    1639      6277422 :               if (block_result.intersect (equiv_range))
    1640              :                 {
    1641       348331 :                   if (DEBUG_RANGE_CACHE)
    1642              :                     {
    1643            0 :                       if (rel == VREL_EQ)
    1644            0 :                         fprintf (dump_file, "Equivalence update! :  ");
    1645              :                       else
    1646            0 :                         fprintf (dump_file, "Partial equiv update! :  ");
    1647            0 :                       print_generic_expr (dump_file, equiv_name, TDF_SLIM);
    1648            0 :                       fprintf (dump_file, " has range  :  ");
    1649            0 :                       equiv_range.dump (dump_file);
    1650            0 :                       fprintf (dump_file, " refining range to :");
    1651            0 :                       block_result.dump (dump_file);
    1652            0 :                       fprintf (dump_file, "\n");
    1653              :                     }
    1654              :                 }
    1655              :             }
    1656      6277422 :         }
    1657              : 
    1658     52829638 :       m_on_entry.set_bb_range (name, bb, block_result);
    1659    102974681 :       gcc_checking_assert (m_workback.length () == start_length);
    1660              :       return;
    1661              :     }
    1662              : 
    1663              :   // Visit each block back to the DEF.  Initialize each one to UNDEFINED.
    1664              :   // m_visited at the end will contain all the blocks that we needed to set
    1665              :   // the range_on_entry cache for.
    1666       923778 :   m_workback.safe_push (bb);
    1667       923778 :   undefined.set_undefined ();
    1668       923778 :   m_on_entry.set_bb_range (name, bb, undefined);
    1669       923778 :   gcc_checking_assert (m_update->empty_p ());
    1670              : 
    1671      6125900 :   while (m_workback.length () > start_length)
    1672              :     {
    1673      5202122 :       basic_block node = m_workback.pop ();
    1674      5202122 :       if (DEBUG_RANGE_CACHE)
    1675              :         {
    1676            0 :           fprintf (dump_file, "BACK visiting block %d for ", node->index);
    1677            0 :           print_generic_expr (dump_file, name, TDF_SLIM);
    1678            0 :           fprintf (dump_file, "\n");
    1679              :         }
    1680              : 
    1681     12455353 :       FOR_EACH_EDGE (e, ei, node->preds)
    1682              :         {
    1683      7253231 :           basic_block pred = e->src;
    1684      7253231 :           value_range r (TREE_TYPE (name));
    1685              : 
    1686      7253231 :           if (DEBUG_RANGE_CACHE)
    1687            0 :             fprintf (dump_file, "  %d->%d ",e->src->index, e->dest->index);
    1688              : 
    1689              :           // If the pred block is the def block add this BB to update list.
    1690      7253231 :           if (pred == def_bb)
    1691              :             {
    1692       863060 :               m_update->add (node);
    1693       863060 :               continue;
    1694              :             }
    1695              : 
    1696              :           // If the pred is entry but NOT def, then it is used before
    1697              :           // defined, it'll get set to [] and no need to update it.
    1698      6390171 :           if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1699              :             {
    1700          349 :               if (DEBUG_RANGE_CACHE)
    1701            0 :                 fprintf (dump_file, "entry: bail.");
    1702          349 :               continue;
    1703              :             }
    1704              : 
    1705              :           // Regardless of whether we have visited pred or not, if the
    1706              :           // pred has inferred ranges, revisit this block.
    1707              :           // Don't search the DOM tree.
    1708      6389822 :           if (infer_oracle ().has_range_p (pred, name))
    1709              :             {
    1710        13094 :               if (DEBUG_RANGE_CACHE)
    1711            0 :                 fprintf (dump_file, "Inferred range: update ");
    1712        13094 :               m_update->add (node);
    1713              :             }
    1714              : 
    1715              :           // If the pred block already has a range, or if it can contribute
    1716              :           // something new. Ie, the edge generates a range of some sort.
    1717      6389822 :           if (m_on_entry.get_bb_range (r, name, pred))
    1718              :             {
    1719      2111478 :               if (DEBUG_RANGE_CACHE)
    1720              :                 {
    1721            0 :                   fprintf (dump_file, "has cache, ");
    1722            0 :                   r.dump (dump_file);
    1723            0 :                   fprintf (dump_file, ", ");
    1724              :                 }
    1725      2111478 :               if (!r.undefined_p () || gori ().has_edge_range_p (name, e))
    1726              :                 {
    1727       588461 :                   m_update->add (node);
    1728       588461 :                   if (DEBUG_RANGE_CACHE)
    1729            0 :                     fprintf (dump_file, "update. ");
    1730              :                 }
    1731      2111478 :               continue;
    1732              :             }
    1733              : 
    1734      4278344 :           if (DEBUG_RANGE_CACHE)
    1735            0 :             fprintf (dump_file, "pushing undefined pred block.\n");
    1736              :           // If the pred hasn't been visited (has no range), add it to
    1737              :           // the list.
    1738      4278344 :           gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
    1739      4278344 :           m_on_entry.set_bb_range (name, pred, undefined);
    1740      4278344 :           m_workback.safe_push (pred);
    1741      7253231 :         }
    1742              :     }
    1743              : 
    1744       923778 :   if (DEBUG_RANGE_CACHE)
    1745            0 :     fprintf (dump_file, "\n");
    1746              : 
    1747              :   // Now fill in the marked blocks with values.
    1748       923778 :   propagate_cache (name);
    1749       923778 :   if (DEBUG_RANGE_CACHE)
    1750            0 :     fprintf (dump_file, "  Propagation update done.\n");
    1751    127798505 : }
    1752              : 
    1753              : // Resolve the range of BB if the dominators range is R by calculating incoming
    1754              : // edges to this block.  All lead back to the dominator so should be cheap.
    1755              : // The range for BB is set and returned in R.
    1756              : 
    1757              : void
    1758      4530579 : ranger_cache::resolve_dom (vrange &r, tree name, basic_block bb)
    1759              : {
    1760      4530579 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1761      4530579 :   basic_block dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
    1762              : 
    1763              :   // if it doesn't already have a value, store the incoming range.
    1764      4530579 :   if (!m_on_entry.bb_range_p (name, dom_bb) && def_bb != dom_bb)
    1765              :     {
    1766              :       // If the range can't be store, don't try to accumulate
    1767              :       // the range in PREV_BB due to excessive recalculations.
    1768      1223179 :       if (!m_on_entry.set_bb_range (name, dom_bb, r))
    1769            0 :         return;
    1770              :     }
    1771              :   // With the dominator set, we should be able to cheaply query
    1772              :   // each incoming edge now and accumulate the results.
    1773      4530579 :   r.set_undefined ();
    1774      4530579 :   edge e;
    1775      4530579 :   edge_iterator ei;
    1776      4530579 :   value_range er (TREE_TYPE (name));
    1777     15272495 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1778              :     {
    1779              :       // If the predecessor is dominated by this block, then there is a back
    1780              :       // edge, and won't provide anything useful.  We'll actually end up with
    1781              :       // VARYING as we will not resolve this node.
    1782     10741916 :       if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
    1783        24218 :         continue;
    1784     10717698 :       edge_range (er, e, name, RFD_READ_ONLY);
    1785     10717698 :       r.union_ (er);
    1786              :     }
    1787              :   // Set the cache in PREV_BB so it is not calculated again.
    1788      4530579 :   m_on_entry.set_bb_range (name, bb, r);
    1789      4530579 : }
    1790              : 
    1791              : // Get the range of NAME from dominators of BB and return it in R.  Search the
    1792              : // dominator tree based on MODE.
    1793              : 
    1794              : bool
    1795    106376594 : ranger_cache::range_from_dom (vrange &r, tree name, basic_block start_bb,
    1796              :                               enum rfd_mode mode)
    1797              : {
    1798    106376594 :   if (mode == RFD_NONE || !dom_info_available_p (CDI_DOMINATORS))
    1799              :     return false;
    1800              : 
    1801              :   // Search back to the definition block or entry block.
    1802     66304041 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1803     66304041 :   if (def_bb == NULL)
    1804      8296181 :     def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1805              : 
    1806     66304041 :   basic_block bb;
    1807     66304041 :   basic_block prev_bb = start_bb;
    1808              : 
    1809              :   // Track any inferred ranges seen.
    1810     66304041 :   value_range infer (TREE_TYPE (name));
    1811     66304041 :   infer.set_varying (TREE_TYPE (name));
    1812              : 
    1813              :   // Range on entry to the DEF block should not be queried.
    1814     66304041 :   gcc_checking_assert (start_bb != def_bb);
    1815     66304041 :   unsigned start_limit = m_workback.length ();
    1816              : 
    1817              :   // Default value is global range.
    1818     66304041 :   get_global_range (r, name);
    1819              : 
    1820              :   // The dominator of EXIT_BLOCK doesn't seem to be set, so at least handle
    1821              :   // the common single exit cases.
    1822     66456793 :   if (start_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) && single_pred_p (start_bb))
    1823       152500 :     bb = single_pred_edge (start_bb)->src;
    1824              :   else
    1825     66151541 :     bb = get_immediate_dominator (CDI_DOMINATORS, start_bb);
    1826              : 
    1827     66304041 :   bool abnormal_dominator = false;
    1828              :   // Search until a value is found, pushing blocks which may need calculating.
    1829    431938572 :   for ( ; bb; prev_bb = bb, bb = get_immediate_dominator (CDI_DOMINATORS, bb))
    1830              :     {
    1831    431084753 :       if (has_abnormal_call_or_eh_pred_edge_p (prev_bb))
    1832              :         abnormal_dominator = true;
    1833              : 
    1834              :       // find the taken outgoing edge and check if it is abnormal.
    1835    430846613 :       if (!abnormal_dominator)
    1836              :         {
    1837    429125996 :           edge e;
    1838    429125996 :           edge_iterator ei;
    1839    656245429 :           FOR_EACH_EDGE (e, ei, bb->succs)
    1840    640774750 :             if (dominated_by_p (CDI_DOMINATORS, prev_bb, e->dest))
    1841              :               {
    1842    413655317 :                 if (e->flags & (EDGE_ABNORMAL | EDGE_EH))
    1843    429125996 :                   abnormal_dominator = true;
    1844              :                 break;
    1845              :               }
    1846              :           // Accumulate any block exit inferred ranges.
    1847    429125996 :           infer_oracle ().maybe_adjust_range (infer, name, bb);
    1848              :         }
    1849              : 
    1850              :       // This block has an outgoing range.
    1851    431084753 :       if (gori ().has_edge_range_p (name, bb))
    1852     46897534 :         m_workback.safe_push (prev_bb);
    1853              :       else
    1854              :         {
    1855              :           // Normally join blocks don't carry any new range information on
    1856              :           // incoming edges.  If the first incoming edge to this block does
    1857              :           // generate a range, calculate the ranges if all incoming edges
    1858              :           // are also dominated by the dominator.  (Avoids backedges which
    1859              :           // will break the rule of moving only upward in the dominator tree).
    1860              :           // If the first pred does not generate a range, then we will be
    1861              :           // using the dominator range anyway, so that's all the check needed.
    1862    384187219 :           if (EDGE_COUNT (prev_bb->preds) > 1
    1863    384187219 :               && gori ().has_edge_range_p (name, EDGE_PRED (prev_bb, 0)->src))
    1864              :             {
    1865       757907 :               edge e;
    1866       757907 :               edge_iterator ei;
    1867       757907 :               bool all_dom = true;
    1868      2585373 :               FOR_EACH_EDGE (e, ei, prev_bb->preds)
    1869      1827466 :                 if (e->src != bb
    1870      1827466 :                     && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
    1871              :                   {
    1872              :                     all_dom = false;
    1873              :                     break;
    1874              :                   }
    1875       757907 :               if (all_dom)
    1876       757907 :                 m_workback.safe_push (prev_bb);
    1877              :             }
    1878              :         }
    1879              : 
    1880    431084753 :       if (def_bb == bb)
    1881              :         break;
    1882              : 
    1883    390479334 :       if (m_on_entry.get_bb_range (r, name, bb))
    1884              :         break;
    1885              :     }
    1886              : 
    1887     66304041 :   if (DEBUG_RANGE_CACHE)
    1888              :     {
    1889            0 :       fprintf (dump_file, "CACHE: BB %d DOM query for ", start_bb->index);
    1890            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1891            0 :       fprintf (dump_file, ", found ");
    1892            0 :       r.dump (dump_file);
    1893            0 :       if (bb)
    1894            0 :         fprintf (dump_file, " at BB%d\n", bb->index);
    1895              :       else
    1896            0 :         fprintf (dump_file, " at function top\n");
    1897              :     }
    1898              : 
    1899              :   // Now process any blocks wit incoming edges that nay have adjustments.
    1900    113959482 :   while (m_workback.length () > start_limit)
    1901              :     {
    1902     47655441 :       value_range er (TREE_TYPE (name));
    1903     47655441 :       prev_bb = m_workback.pop ();
    1904     47655441 :       if (!single_pred_p (prev_bb))
    1905              :         {
    1906              :           // Non single pred means we need to cache a value in the dominator
    1907              :           // so we can cheaply calculate incoming edges to this block, and
    1908              :           // then store the resulting value.  If processing mode is not
    1909              :           // RFD_FILL, then the cache cant be stored to, so don't try.
    1910              :           // Otherwise this becomes a quadratic timed calculation.
    1911      6796643 :           if (mode == RFD_FILL)
    1912      4530579 :             resolve_dom (r, name, prev_bb);
    1913      6796643 :           continue;
    1914              :         }
    1915              : 
    1916     40858798 :       edge e = single_pred_edge (prev_bb);
    1917     40858798 :       bb = e->src;
    1918     40858798 :       if (gori ().edge_range_p (er, e, name, *this))
    1919              :         {
    1920     36967763 :           r.intersect (er);
    1921              :           // If this is a normal edge, apply any inferred ranges.
    1922     36967763 :           if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
    1923     36967763 :             infer_oracle ().maybe_adjust_range (r, name, bb);
    1924              : 
    1925     36967763 :           if (DEBUG_RANGE_CACHE)
    1926              :             {
    1927            0 :               fprintf (dump_file, "CACHE: Adjusted edge range for %d->%d : ",
    1928              :                        bb->index, prev_bb->index);
    1929            0 :               r.dump (dump_file);
    1930            0 :               fprintf (dump_file, "\n");
    1931              :             }
    1932              :         }
    1933     47655441 :     }
    1934              : 
    1935              :   // Apply any inferred ranges discovered.
    1936     66304041 :   r.intersect (infer);
    1937              : 
    1938     66304041 :   if (DEBUG_RANGE_CACHE)
    1939              :     {
    1940            0 :       fprintf (dump_file, "CACHE: Range for DOM returns : ");
    1941            0 :       r.dump (dump_file);
    1942            0 :       fprintf (dump_file, "\n");
    1943              :     }
    1944     66304041 :   return true;
    1945     66304041 : }
    1946              : 
    1947              : // This routine will register an inferred value in block BB, and possibly
    1948              : // update the on-entry cache if appropriate.
    1949              : 
    1950              : void
    1951     16896225 : ranger_cache::register_inferred_value (const vrange &ir, tree name,
    1952              :                                        basic_block bb)
    1953              : {
    1954     16896225 :   value_range r (TREE_TYPE (name));
    1955     16896225 :   if (!m_on_entry.get_bb_range (r, name, bb))
    1956     10611386 :     exit_range (r, name, bb, RFD_READ_ONLY);
    1957     16896225 :   if (r.intersect (ir))
    1958              :     {
    1959      4928953 :       m_on_entry.set_bb_range (name, bb, r);
    1960              :       // If this range was invariant before, remove invariant.
    1961      4928953 :       if (!gori ().has_edge_range_p (name))
    1962      4116502 :         gori_ssa ()->set_range_invariant (name, false);
    1963              :     }
    1964     16896225 : }
    1965              : 
    1966              : // This routine is used during a block walk to adjust any inferred ranges
    1967              : // of operands on stmt S.
    1968              : 
    1969              : void
    1970    274233913 : ranger_cache::apply_inferred_ranges (gimple *s)
    1971              : {
    1972    274233913 :   bool update = true;
    1973              : 
    1974    274233913 :   basic_block bb = gimple_bb (s);
    1975    274233913 :   gimple_infer_range infer(s, this);
    1976    274233913 :   if (infer.num () == 0)
    1977              :     return;
    1978              : 
    1979              :   // Do not update the on-entry cache for block ending stmts.
    1980     16574801 :   if (stmt_ends_bb_p (s))
    1981              :     {
    1982      1185996 :       edge_iterator ei;
    1983      1185996 :       edge e;
    1984      2139295 :       FOR_EACH_EDGE (e, ei, gimple_bb (s)->succs)
    1985      2133484 :         if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
    1986              :           break;
    1987      1185996 :       if (e == NULL)
    1988         5811 :         update = false;
    1989              :     }
    1990              : 
    1991     16574801 :   infer_oracle ().add_ranges (s, infer);
    1992     16574801 :   if (update)
    1993     33439508 :     for (unsigned x = 0; x < infer.num (); x++)
    1994     16870518 :       register_inferred_value (infer.range (x), infer.name (x), bb);
    1995     16574801 : }
    1996              : 
    1997              : // Reset range info for NAME.
    1998              : 
    1999              : void
    2000          440 : ranger_cache::reset_range_info (tree name)
    2001              : {
    2002          440 :   m_on_entry.clear (name);
    2003          440 :   m_globals.clear_range (name);
    2004          440 :   range_query::reset_range_info (name);
    2005          440 : }
        

Generated by: LCOV version 2.4-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.