LCOV - code coverage report
Current view: top level - gcc - gimple-range-cache.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 84.4 % 826 697
Test Date: 2026-06-20 15:32:29 Functions: 90.9 % 77 70
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     27956038 :   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     27869670 : sbr_vector::sbr_vector (tree t, vrange_allocator *allocator, bool zero_p)
     102     27869670 :   : ssa_block_ranges (t)
     103              : {
     104     27869670 :   gcc_checking_assert (TYPE_P (t));
     105     27869670 :   m_type = t;
     106     27869670 :   m_zero_p = zero_p;
     107     27869670 :   m_range_allocator = allocator;
     108     27869670 :   m_tab_size = last_basic_block_for_fn (cfun) + 1;
     109     55739340 :   m_tab = static_cast <vrange_storage **>
     110     27869670 :     (allocator->alloc (m_tab_size * sizeof (vrange_storage *)));
     111     27869670 :   if (zero_p)
     112     24552353 :     memset (m_tab, 0, m_tab_size * sizeof (vrange *));
     113              : 
     114              :   // Create the cached type range.
     115     27869670 :   m_varying = m_range_allocator->clone_varying (t);
     116     27869670 :   m_undefined = m_range_allocator->clone_undefined (t);
     117     27869670 : }
     118              : 
     119              : // Grow the vector when the CFG has increased in size.
     120              : 
     121              : void
     122        10283 : sbr_vector::grow ()
     123              : {
     124        10283 :   int curr_bb_size = last_basic_block_for_fn (cfun);
     125        10283 :   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        10283 :   int inc = MAX ((curr_bb_size - m_tab_size) * 2, 128);
     129        10283 :   inc = MAX (inc, curr_bb_size / 10);
     130        10283 :   int new_size = inc + curr_bb_size;
     131              : 
     132              :   // Allocate new memory, copy the old vector and clear the new space.
     133        10283 :   vrange_storage **t = static_cast <vrange_storage **>
     134        10283 :     (m_range_allocator->alloc (new_size * sizeof (vrange_storage *)));
     135        10283 :   memcpy (t, m_tab, m_tab_size * sizeof (vrange_storage *));
     136        10283 :   if (m_zero_p)
     137         7982 :     memset (t + m_tab_size, 0, (new_size - m_tab_size) * sizeof (vrange_storage *));
     138              : 
     139        10283 :   m_tab = t;
     140        10283 :   m_tab_size = new_size;
     141        10283 : }
     142              : 
     143              : // Set the range for block BB to be R.
     144              : 
     145              : bool
     146     72740495 : sbr_vector::set_bb_range (const_basic_block bb, const vrange &r)
     147              : {
     148     72740495 :   vrange_storage *m;
     149     72740495 :   if (bb->index >= m_tab_size)
     150        10283 :     grow ();
     151     72740495 :   if (r.varying_p ())
     152     22632822 :     m = m_varying;
     153     50107673 :   else if (r.undefined_p ())
     154      5340556 :     m = m_undefined;
     155              :   else
     156     44767117 :     m = m_range_allocator->clone (r);
     157     72740495 :   m_tab[bb->index] = m;
     158     72740495 :   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    321834317 : sbr_vector::get_bb_range (vrange &r, const_basic_block bb)
     166              : {
     167    321834317 :   if (bb->index >= m_tab_size)
     168              :     return false;
     169    321826424 :   vrange_storage *m = m_tab[bb->index];
     170    321826424 :   if (m)
     171              :     {
     172    243798699 :       m->get_vrange (r, m_type);
     173    243798699 :       return true;
     174              :     }
     175              :   return false;
     176              : }
     177              : 
     178              : // Return true if a range is present.
     179              : 
     180              : bool
     181    242698109 : sbr_vector::bb_range_p (const_basic_block bb)
     182              : {
     183    242698109 :   if (bb->index < m_tab_size)
     184    242687179 :     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      3317317 : sbr_lazy_vector::sbr_lazy_vector (tree t, vrange_allocator *allocator,
     204      3317317 :                                   bitmap_obstack *bm)
     205      3317317 :   : sbr_vector (t, allocator, false)
     206              : {
     207      3317317 :   m_has_value = BITMAP_ALLOC (bm);
     208      3317317 : }
     209              : 
     210              : bool
     211     11335848 : sbr_lazy_vector::set_bb_range (const_basic_block bb, const vrange &r)
     212              : {
     213     11335848 :   sbr_vector::set_bb_range (bb, r);
     214     11335848 :   bitmap_set_bit (m_has_value, bb->index);
     215     11335848 :   return true;
     216              : }
     217              : 
     218              : bool
     219    240563504 : sbr_lazy_vector::get_bb_range (vrange &r, const_basic_block bb)
     220              : {
     221    240563504 :   if (bitmap_bit_p (m_has_value, bb->index))
     222     39427461 :     return sbr_vector::get_bb_range (r, bb);
     223              :   return false;
     224              : }
     225              : 
     226              : bool
     227     43757583 : sbr_lazy_vector::bb_range_p (const_basic_block bb)
     228              : {
     229     43757583 :   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        86368 : sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, vrange_allocator *allocator,
     264        86368 :                                       bitmap_obstack *bm)
     265        86368 :   : ssa_block_ranges (t)
     266              : {
     267        86368 :   gcc_checking_assert (TYPE_P (t));
     268        86368 :   m_type = t;
     269        86368 :   bitmap_initialize (&bitvec, bm);
     270        86368 :   bitmap_tree_view (&bitvec);
     271        86368 :   m_range_allocator = allocator;
     272              :   // Pre-cache varying.
     273        86368 :   m_range[0] = m_range_allocator->clone_varying (t);
     274              :   // Pre-cache zero and non-zero values for pointers.
     275        86368 :   if (POINTER_TYPE_P (t))
     276              :     {
     277         1188 :       prange nonzero;
     278         1188 :       nonzero.set_nonzero (t);
     279         1188 :       m_range[1] = m_range_allocator->clone (nonzero);
     280         1188 :       prange zero;
     281         1188 :       zero.set_zero (t);
     282         1188 :       m_range[2] = m_range_allocator->clone (zero);
     283         1188 :     }
     284              :   else
     285        85180 :     m_range[1] = m_range[2] = NULL;
     286              :   // Clear SBR_NUM entries.
     287      1036416 :   for (int x = 3; x < SBR_NUM; x++)
     288       950048 :     m_range[x] = 0;
     289        86368 : }
     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       446716 : sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
     297              : {
     298       446716 :   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     15118733 : sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
     306              : {
     307     30237466 :   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       446716 : sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const vrange &r)
     314              : {
     315       446716 :   if (r.undefined_p ())
     316              :     {
     317        18339 :       bitmap_set_quad (&bitvec, bb->index, SBR_UNDEF);
     318        18339 :       return true;
     319              :     }
     320              : 
     321              :   // Loop thru the values to see if R is already present.
     322       787869 :   for (int x = 0; x < SBR_NUM; x++)
     323       776854 :     if (!m_range[x] || m_range[x]->equal_p (r))
     324              :       {
     325       417362 :         if (!m_range[x])
     326       100391 :           m_range[x] = m_range_allocator->clone (r);
     327       417362 :         bitmap_set_quad (&bitvec, bb->index, x + 1);
     328       417362 :         return true;
     329              :       }
     330              :   // All values are taken, default to VARYING.
     331        11015 :   bitmap_set_quad (&bitvec, bb->index, SBR_VARYING);
     332        11015 :   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     12743484 : sbr_sparse_bitmap::get_bb_range (vrange &r, const_basic_block bb)
     340              : {
     341     12743484 :   int value = bitmap_get_quad (&bitvec, bb->index);
     342              : 
     343     12743484 :   if (!value)
     344              :     return false;
     345              : 
     346      1830596 :   gcc_checking_assert (value <= SBR_UNDEF);
     347      1830596 :   if (value == SBR_UNDEF)
     348        36402 :     r.set_undefined ();
     349              :   else
     350      1794194 :     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      2375249 : sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
     358              : {
     359      2375249 :   return (bitmap_get_quad (&bitvec, bb->index) != 0);
     360              : }
     361              : 
     362              : // -------------------------------------------------------------------------
     363              : 
     364              : // Initialize the block cache.
     365              : 
     366     27888807 : block_range_cache::block_range_cache ()
     367              : {
     368     27888807 :   bitmap_obstack_initialize (&m_bitmaps);
     369     27888807 :   m_ssa_ranges.create (0);
     370     55777614 :   m_ssa_ranges.safe_grow_cleared (num_ssa_names);
     371     27888807 :   m_range_allocator = new vrange_allocator;
     372     27888807 : }
     373              : 
     374              : // Remove any m_block_caches which have been created.
     375              : 
     376     27888805 : block_range_cache::~block_range_cache ()
     377              : {
     378     27888805 :   delete m_range_allocator;
     379              :   // Release the vector itself.
     380     27888805 :   m_ssa_ranges.release ();
     381     27888805 :   bitmap_obstack_release (&m_bitmaps);
     382     27888805 : }
     383              : 
     384              : // Set the range for NAME on entry to block BB to R.
     385              : // If it has not been accessed yet, allocate it first.
     386              : 
     387              : bool
     388     73187211 : block_range_cache::set_bb_range (tree name, const_basic_block bb,
     389              :                                  const vrange &r)
     390              : {
     391     73187211 :   unsigned v = SSA_NAME_VERSION (name);
     392     73187211 :   if (v >= m_ssa_ranges.length ())
     393            2 :     m_ssa_ranges.safe_grow_cleared (num_ssa_names);
     394              : 
     395     73187211 :   if (!m_ssa_ranges[v])
     396              :     {
     397              :       // Use sparse bitmap representation if there are too many basic blocks.
     398     27956038 :       if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
     399              :         {
     400        86368 :           void *r = m_range_allocator->alloc (sizeof (sbr_sparse_bitmap));
     401        86368 :           m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
     402              :                                                        m_range_allocator,
     403        86368 :                                                        &m_bitmaps);
     404              :         }
     405     27869670 :       else if (last_basic_block_for_fn (cfun) < param_vrp_vector_threshold)
     406              :         {
     407              :           // For small CFGs use the basic vector implementation.
     408     24552353 :           void *r = m_range_allocator->alloc (sizeof (sbr_vector));
     409     24552353 :           m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
     410     24552353 :                                                 m_range_allocator);
     411              :         }
     412              :       else
     413              :         {
     414              :           // Otherwise use the sparse vector implementation.
     415      3317317 :           void *r = m_range_allocator->alloc (sizeof (sbr_lazy_vector));
     416      3317317 :           m_ssa_ranges[v] = new (r) sbr_lazy_vector (TREE_TYPE (name),
     417              :                                                      m_range_allocator,
     418      3317317 :                                                      &m_bitmaps);
     419              :         }
     420              :     }
     421     73187211 :   return m_ssa_ranges[v]->set_bb_range (bb, r);
     422              : }
     423              : 
     424              : 
     425              : // Return a pointer to the ssa_block_cache for NAME.  If it has not been
     426              : // accessed yet, return NULL.
     427              : 
     428              : inline ssa_block_ranges *
     429   1114475475 : block_range_cache::query_block_ranges (tree name)
     430              : {
     431   1114475475 :   unsigned v = SSA_NAME_VERSION (name);
     432   1114475475 :   if (v >= m_ssa_ranges.length () || !m_ssa_ranges[v])
     433              :     return NULL;
     434              :   return m_ssa_ranges[v];
     435              : }
     436              : 
     437              : 
     438              : 
     439              : // Return the range for NAME on entry to BB in R.  Return true if there
     440              : // is one.
     441              : 
     442              : bool
     443    732955573 : block_range_cache::get_bb_range (vrange &r, tree name, const_basic_block bb)
     444              : {
     445    732955573 :   ssa_block_ranges *ptr = query_block_ranges (name);
     446    732955573 :   if (ptr)
     447    535712583 :     return ptr->get_bb_range (r, bb);
     448              :   return false;
     449              : }
     450              : 
     451              : // Return true if NAME has a range set in block BB.
     452              : 
     453              : bool
     454    381519902 : block_range_cache::bb_range_p (tree name, const_basic_block bb)
     455              : {
     456    381519902 :   ssa_block_ranges *ptr = query_block_ranges (name);
     457    381519902 :   if (ptr)
     458    288830941 :     return ptr->bb_range_p (bb);
     459              :   return false;
     460              : }
     461              : 
     462              : // Print all known block caches to file F.
     463              : 
     464              : void
     465            0 : block_range_cache::dump (FILE *f)
     466              : {
     467            0 :   unsigned x;
     468            0 :   for (x = 1; x < m_ssa_ranges.length (); ++x)
     469              :     {
     470            0 :       if (m_ssa_ranges[x])
     471              :         {
     472            0 :           fprintf (f, " Ranges for ");
     473            0 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     474            0 :           fprintf (f, ":\n");
     475            0 :           m_ssa_ranges[x]->dump (f);
     476            0 :           fprintf (f, "\n");
     477              :         }
     478              :     }
     479            0 : }
     480              : 
     481              : // Print all known ranges on entry to block BB to file F.
     482              : 
     483              : void
     484          252 : block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
     485              : {
     486          252 :   unsigned x;
     487          252 :   bool summarize_varying = false;
     488        12652 :   for (x = 1; x < m_ssa_ranges.length (); ++x)
     489              :     {
     490        12400 :       if (!m_ssa_ranges[x])
     491        22278 :         continue;
     492              : 
     493         1261 :       if (!gimple_range_ssa_p (ssa_name (x)))
     494            0 :         continue;
     495              : 
     496         1261 :       value_range r (TREE_TYPE (ssa_name (x)));
     497         1261 :       if (m_ssa_ranges[x]->get_bb_range (r, bb))
     498              :         {
     499          224 :           if (!print_varying && r.varying_p ())
     500              :             {
     501            0 :               summarize_varying = true;
     502            0 :               continue;
     503              :             }
     504          224 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     505          224 :           fprintf (f, "\t");
     506          224 :           r.dump(f);
     507          224 :           fprintf (f, "\n");
     508              :         }
     509         1261 :     }
     510              :   // If there were any varying entries, lump them all together.
     511          252 :   if (summarize_varying)
     512              :     {
     513            0 :       fprintf (f, "VARYING_P on entry : ");
     514            0 :       for (x = 1; x < m_ssa_ranges.length (); ++x)
     515              :         {
     516            0 :           if (!m_ssa_ranges[x])
     517            0 :             continue;
     518              : 
     519            0 :           if (!gimple_range_ssa_p (ssa_name (x)))
     520            0 :             continue;
     521              : 
     522            0 :           value_range r (TREE_TYPE (ssa_name (x)));
     523            0 :           if (m_ssa_ranges[x]->get_bb_range (r, bb))
     524              :             {
     525            0 :               if (r.varying_p ())
     526              :                 {
     527            0 :                   print_generic_expr (f, ssa_name (x), TDF_NONE);
     528            0 :                   fprintf (f, "  ");
     529              :                 }
     530              :             }
     531            0 :         }
     532            0 :       fprintf (f, "\n");
     533              :     }
     534          252 : }
     535              : 
     536              : // -------------------------------------------------------------------------
     537              : 
     538              : // Initialize an ssa cache.
     539              : 
     540     55082370 : ssa_cache::ssa_cache ()
     541              : {
     542     55082370 :   m_tab.create (0);
     543     55082370 :   m_range_allocator = new vrange_allocator;
     544     55082370 : }
     545              : 
     546              : // Deconstruct an ssa cache.
     547              : 
     548     55082359 : ssa_cache::~ssa_cache ()
     549              : {
     550     55082359 :   m_tab.release ();
     551     55082359 :   delete m_range_allocator;
     552     55082359 : }
     553              : 
     554              : // Enable a query to evaluate staements/ramnges based on picking up ranges
     555              : // from just an ssa-cache.
     556              : 
     557              : bool
     558          517 : ssa_cache::range_of_expr (vrange &r, tree expr, gimple *stmt)
     559              : {
     560          517 :   if (!gimple_range_ssa_p (expr))
     561            0 :     return get_tree_range (r, expr, stmt);
     562              : 
     563          517 :   if (!get_range (r, expr))
     564           14 :     gimple_range_global (r, expr, cfun);
     565              :   return true;
     566              : }
     567              : 
     568              : // Return TRUE if the global range of NAME has a cache entry.
     569              : 
     570              : bool
     571      4121635 : ssa_cache::has_range (tree name) const
     572              : {
     573      4121635 :   unsigned v = SSA_NAME_VERSION (name);
     574      4121635 :   if (v >= m_tab.length ())
     575              :     return false;
     576      3680212 :   return m_tab[v] != NULL;
     577              : }
     578              : 
     579              : // Retrieve the global range of NAME from cache memory if it exists.
     580              : // Return the value in R.
     581              : 
     582              : bool
     583   1161098223 : ssa_cache::get_range (vrange &r, tree name) const
     584              : {
     585   1161098223 :   unsigned v = SSA_NAME_VERSION (name);
     586   1161098223 :   if (v >= m_tab.length ())
     587              :     return false;
     588              : 
     589   1149349689 :   vrange_storage *stow = m_tab[v];
     590   1149349689 :   if (!stow)
     591              :     return false;
     592    940248404 :   stow->get_vrange (r, TREE_TYPE (name));
     593    940248404 :   return true;
     594              : }
     595              : 
     596              : // Set the range for NAME to R in the ssa cache.
     597              : // Return TRUE if there was already a range set, otherwise false.
     598              : 
     599              : bool
     600    149302840 : ssa_cache::set_range (tree name, const vrange &r)
     601              : {
     602    149302840 :   unsigned v = SSA_NAME_VERSION (name);
     603    149302840 :   if (v >= m_tab.length ())
     604     15616812 :     m_tab.safe_grow_cleared (num_ssa_names + 1);
     605              : 
     606    149302840 :   vrange_storage *m = m_tab[v];
     607    149302840 :   if (m && m->fits_p (r))
     608     20763036 :     m->set_vrange (r);
     609              :   else
     610    128539804 :     m_tab[v] = m_range_allocator->clone (r);
     611    149302840 :   return m != NULL;
     612              : }
     613              : 
     614              : // If NAME has a range, intersect it with R, otherwise set it to R.
     615              : // Return TRUE if the range is new or changes.
     616              : 
     617              : bool
     618          117 : ssa_cache::merge_range (tree name, const vrange &r)
     619              : {
     620          117 :   unsigned v = SSA_NAME_VERSION (name);
     621          117 :   if (v >= m_tab.length ())
     622           12 :     m_tab.safe_grow_cleared (num_ssa_names + 1);
     623              : 
     624          117 :   vrange_storage *m = m_tab[v];
     625              :   // Check if this is a new value.
     626          117 :   if (!m)
     627          116 :     m_tab[v] = m_range_allocator->clone (r);
     628              :   else
     629              :     {
     630            1 :       value_range curr (TREE_TYPE (name));
     631            1 :       m->get_vrange (curr, TREE_TYPE (name));
     632              :       // If there is no change, return false.
     633            1 :       if (!curr.intersect (r))
     634            1 :         return false;
     635              : 
     636            0 :       if (m->fits_p (curr))
     637            0 :         m->set_vrange (curr);
     638              :       else
     639            0 :         m_tab[v] = m_range_allocator->clone (curr);
     640            1 :     }
     641              :   return true;
     642              : }
     643              : 
     644              : // Set the range for NAME to R in the ssa cache.
     645              : 
     646              : void
     647            0 : ssa_cache::clear_range (tree name)
     648              : {
     649            0 :   unsigned v = SSA_NAME_VERSION (name);
     650            0 :   if (v >= m_tab.length ())
     651              :     return;
     652            0 :   m_tab[v] = NULL;
     653              : }
     654              : 
     655              : // Clear the ssa cache.
     656              : 
     657              : void
     658            0 : ssa_cache::clear ()
     659              : {
     660            0 :   if (m_tab.address ())
     661            0 :     memset (m_tab.address(), 0, m_tab.length () * sizeof (vrange *));
     662            0 : }
     663              : 
     664              : // Dump the contents of the ssa cache to F.
     665              : 
     666              : void
     667           63 : ssa_cache::dump (FILE *f)
     668              : {
     669         3154 :   for (unsigned x = 1; x < num_ssa_names; x++)
     670              :     {
     671         3091 :       if (!gimple_range_ssa_p (ssa_name (x)))
     672         1348 :         continue;
     673         1743 :       value_range r (TREE_TYPE (ssa_name (x)));
     674              :       // Dump all non-varying ranges.
     675         1743 :       if (get_range (r, ssa_name (x)) && !r.varying_p ())
     676              :         {
     677          299 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     678          299 :           fprintf (f, "  : ");
     679          299 :           r.dump (f);
     680          299 :           fprintf (f, "\n");
     681              :         }
     682         1743 :     }
     683              : 
     684           63 : }
     685              : 
     686              : // Construct an ssa_lazy_cache. If OB is specified, us it, otherwise use
     687              : // a local bitmap obstack.
     688              : 
     689     27193557 : ssa_lazy_cache::ssa_lazy_cache (bitmap_obstack *ob)
     690              : {
     691     27193557 :   if (!ob)
     692              :     {
     693     27193548 :       bitmap_obstack_initialize (&m_bitmaps);
     694     27193548 :       m_ob = &m_bitmaps;
     695              :     }
     696              :   else
     697            9 :     m_ob = ob;
     698     27193557 :   active_p = BITMAP_ALLOC (m_ob);
     699     27193557 : }
     700              : 
     701              : // Destruct an sa_lazy_cache.  Free the bitmap if it came from a different
     702              : // obstack, or release the obstack if it was a local one.
     703              : 
     704     27193548 : ssa_lazy_cache::~ssa_lazy_cache ()
     705              : {
     706     27193548 :   if (m_ob == &m_bitmaps)
     707     27193548 :     bitmap_obstack_release (&m_bitmaps);
     708              :   else
     709            0 :     BITMAP_FREE (active_p);
     710     27193548 : }
     711              : 
     712              : // Return true if NAME has an active range in the cache.
     713              : 
     714              : bool
     715          245 : ssa_lazy_cache::has_range (tree name) const
     716              : {
     717          245 :   return bitmap_bit_p (active_p, SSA_NAME_VERSION (name));
     718              : }
     719              : 
     720              : // Set range of NAME to R in a lazy cache.  Return FALSE if it did not already
     721              : // have a range.
     722              : 
     723              : bool
     724     99208164 : ssa_lazy_cache::set_range (tree name, const vrange &r)
     725              : {
     726     99208164 :   unsigned v = SSA_NAME_VERSION (name);
     727     99208164 :   if (!bitmap_set_bit (active_p, v))
     728              :     {
     729              :       // There is already an entry, simply set it.
     730     12186389 :       gcc_checking_assert (v < m_tab.length ());
     731     12186389 :       return ssa_cache::set_range (name, r);
     732              :     }
     733     87021775 :   if (v >= m_tab.length ())
     734     45932748 :     m_tab.safe_grow (num_ssa_names + 1);
     735     87021775 :   m_tab[v] = m_range_allocator->clone (r);
     736     87021775 :   return false;
     737              : }
     738              : 
     739              : // If NAME has a range, intersect it with R, otherwise set it to R.
     740              : // Return TRUE if the range is new or changes.
     741              : 
     742              : bool
     743          213 : ssa_lazy_cache::merge_range (tree name, const vrange &r)
     744              : {
     745          213 :   unsigned v = SSA_NAME_VERSION (name);
     746          213 :   if (!bitmap_set_bit (active_p, v))
     747              :     {
     748              :       // There is already an entry, simply merge it.
     749            1 :       gcc_checking_assert (v < m_tab.length ());
     750            1 :       return ssa_cache::merge_range (name, r);
     751              :     }
     752          212 :   if (v >= m_tab.length ())
     753          160 :     m_tab.safe_grow (num_ssa_names + 1);
     754          212 :   m_tab[v] = m_range_allocator->clone (r);
     755          212 :   return true;
     756              : }
     757              : 
     758              : // Merge all elements of CACHE with this cache.
     759              : // Any names in CACHE that are not in this one are added.
     760              : // Any names in both are merged via merge_range..
     761              : 
     762              : void
     763            7 : ssa_lazy_cache::merge (const ssa_lazy_cache &cache)
     764              : {
     765            7 :   unsigned x;
     766            7 :   bitmap_iterator bi;
     767           57 :   EXECUTE_IF_SET_IN_BITMAP (cache.active_p, 0, x, bi)
     768              :     {
     769           50 :       tree name = ssa_name (x);
     770           50 :       value_range r(TREE_TYPE (name));
     771           50 :       cache.get_range (r, name);
     772           50 :       merge_range (ssa_name (x), r);
     773           50 :     }
     774            7 : }
     775              : 
     776              : // Return TRUE if NAME has a range, and return it in R.
     777              : 
     778              : bool
     779    257783778 : ssa_lazy_cache::get_range (vrange &r, tree name) const
     780              : {
     781    257783778 :   if (!bitmap_bit_p (active_p, SSA_NAME_VERSION (name)))
     782              :     return false;
     783    107765840 :   return ssa_cache::get_range (r, name);
     784              : }
     785              : 
     786              : // Remove NAME from the active range list.
     787              : 
     788              : void
     789     49723895 : ssa_lazy_cache::clear_range (tree name)
     790              : {
     791     49723895 :   bitmap_clear_bit (active_p, SSA_NAME_VERSION (name));
     792     49723895 : }
     793              : 
     794              : // Remove all ranges from the active range list.
     795              : 
     796              : void
     797     33531337 : ssa_lazy_cache::clear ()
     798              : {
     799     33531337 :   bitmap_clear (active_p);
     800     33531337 : }
     801              : 
     802              : // --------------------------------------------------------------------------
     803              : 
     804              : 
     805              : // This class will manage the timestamps for each ssa_name.
     806              : // When a value is calculated, the timestamp is set to the current time.
     807              : // Current time is then incremented.  Any dependencies will already have
     808              : // been calculated, and will thus have older timestamps.
     809              : // If one of those values is ever calculated again, it will get a newer
     810              : // timestamp, and the "current_p" check will fail.
     811              : 
     812              : class temporal_cache
     813              : {
     814              : public:
     815              :   temporal_cache ();
     816              :   ~temporal_cache ();
     817              :   bool current_p (tree name, tree dep1, tree dep2) const;
     818              :   void set_timestamp (tree name);
     819              :   void set_always_current (tree name, bool value);
     820              :   bool always_current_p (tree name) const;
     821              : private:
     822              :   int temporal_value (unsigned ssa) const;
     823              :   int m_current_time;
     824              :   vec <int> m_timestamp;
     825              : };
     826              : 
     827              : inline
     828     27888807 : temporal_cache::temporal_cache ()
     829              : {
     830     27888807 :   m_current_time = 1;
     831     27888807 :   m_timestamp.create (0);
     832     55777614 :   m_timestamp.safe_grow_cleared (num_ssa_names);
     833     27888807 : }
     834              : 
     835              : inline
     836     27888805 : temporal_cache::~temporal_cache ()
     837              : {
     838     27888805 :   m_timestamp.release ();
     839     27888805 : }
     840              : 
     841              : // Return the timestamp value for SSA, or 0 if there isn't one.
     842              : 
     843              : inline int
     844    582292018 : temporal_cache::temporal_value (unsigned ssa) const
     845              : {
     846    582292018 :   if (ssa >= m_timestamp.length ())
     847              :     return 0;
     848    582292018 :   return abs (m_timestamp[ssa]);
     849              : }
     850              : 
     851              : // Return TRUE if the timestamp for NAME is newer than any of its dependents.
     852              : // Up to 2 dependencies can be checked.
     853              : 
     854              : bool
     855    346903160 : temporal_cache::current_p (tree name, tree dep1, tree dep2) const
     856              : {
     857    346903160 :   if (always_current_p (name))
     858              :     return true;
     859              : 
     860              :   // Any non-registered dependencies will have a value of 0 and thus be older.
     861              :   // Return true if time is newer than either dependent.
     862    340188609 :   int ts = temporal_value (SSA_NAME_VERSION (name));
     863    528088637 :   if (dep1 && ts < temporal_value (SSA_NAME_VERSION (dep1)))
     864              :     return false;
     865    342703130 :   if (dep2 && ts < temporal_value (SSA_NAME_VERSION (dep2)))
     866      6380220 :     return false;
     867              : 
     868              :   return true;
     869              : }
     870              : 
     871              : // This increments the global timer and sets the timestamp for NAME.
     872              : 
     873              : inline void
     874    119810625 : temporal_cache::set_timestamp (tree name)
     875              : {
     876    119810625 :   unsigned v = SSA_NAME_VERSION (name);
     877    119810625 :   if (v >= m_timestamp.length ())
     878            0 :     m_timestamp.safe_grow_cleared (num_ssa_names + 20);
     879    119810625 :   m_timestamp[v] = ++m_current_time;
     880    119810625 : }
     881              : 
     882              : // Set the timestamp to 0, marking it as "always up to date".
     883              : 
     884              : inline void
     885    273327414 : temporal_cache::set_always_current (tree name, bool value)
     886              : {
     887    273327414 :   unsigned v = SSA_NAME_VERSION (name);
     888    273327414 :   if (v >= m_timestamp.length ())
     889         1846 :     m_timestamp.safe_grow_cleared (num_ssa_names + 20);
     890              : 
     891    273327414 :   int ts = abs (m_timestamp[v]);
     892              :   // If this does not have a timestamp, create one.
     893    273327414 :   if (ts == 0)
     894    124723775 :     ts = ++m_current_time;
     895    273327414 :   m_timestamp[v] = value ? -ts : ts;
     896    273327414 : }
     897              : 
     898              : // Return true if NAME is always current.
     899              : 
     900              : inline bool
     901    346903160 : temporal_cache::always_current_p (tree name) const
     902              : {
     903    346903160 :   unsigned v = SSA_NAME_VERSION (name);
     904    346903160 :   if (v >= m_timestamp.length ())
     905              :     return false;
     906    346903160 :   return m_timestamp[v] <= 0;
     907              : }
     908              : 
     909              : // --------------------------------------------------------------------------
     910              : 
     911              : // This class provides an abstraction of a list of blocks to be updated
     912              : // by the cache.  It is currently a stack but could be changed.  It also
     913              : // maintains a list of blocks which have failed propagation, and does not
     914              : // enter any of those blocks into the list.
     915              : 
     916              : // A vector over the BBs is maintained, and an entry of 0 means it is not in
     917              : // a list.  Otherwise, the entry is the next block in the list. -1 terminates
     918              : // the list.  m_head points to the top of the list, -1 if the list is empty.
     919              : 
     920              : class update_list
     921              : {
     922              : public:
     923              :   update_list ();
     924              :   ~update_list ();
     925              :   void add (basic_block bb);
     926              :   basic_block pop ();
     927    153725174 :   inline bool empty_p () { return m_update_head == -1; }
     928      5867262 :   inline void clear_failures () { bitmap_clear (m_propfail); }
     929            6 :   inline void propagation_failed (basic_block bb)
     930            6 :                                   { bitmap_set_bit (m_propfail, bb->index); }
     931              : private:
     932              :   vec<int> m_update_list;
     933              :   int m_update_head;
     934              :   bitmap m_propfail;
     935              :   bitmap_obstack m_bitmaps;
     936              : };
     937              : 
     938              : // Create an update list.
     939              : 
     940     27888807 : update_list::update_list ()
     941              : {
     942     27888807 :   m_update_list.create (0);
     943     27888807 :   m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun) + 64);
     944     27888807 :   m_update_head = -1;
     945     27888807 :   bitmap_obstack_initialize (&m_bitmaps);
     946     27888807 :   m_propfail = BITMAP_ALLOC (&m_bitmaps);
     947     27888807 : }
     948              : 
     949              : // Destroy an update list.
     950              : 
     951     27888805 : update_list::~update_list ()
     952              : {
     953     27888805 :   m_update_list.release ();
     954     27888805 :   bitmap_obstack_release (&m_bitmaps);
     955     27888805 : }
     956              : 
     957              : // Add BB to the list of blocks to update, unless it's already in the list.
     958              : 
     959              : void
     960     13352465 : update_list::add (basic_block bb)
     961              : {
     962     13352465 :   int i = bb->index;
     963              :   // If propagation has failed for BB, or its already in the list, don't
     964              :   // add it again.
     965     13352465 :   if ((unsigned)i >= m_update_list.length ())
     966           77 :     m_update_list.safe_grow_cleared (i + 64);
     967     13352465 :   if (!m_update_list[i] && !bitmap_bit_p (m_propfail, i))
     968              :     {
     969     12673524 :       if (empty_p ())
     970              :         {
     971      7271728 :           m_update_head = i;
     972      7271728 :           m_update_list[i] = -1;
     973              :         }
     974              :       else
     975              :         {
     976      5401796 :           gcc_checking_assert (m_update_head > 0);
     977      5401796 :           m_update_list[i] = m_update_head;
     978      5401796 :           m_update_head = i;
     979              :         }
     980              :     }
     981     13352465 : }
     982              : 
     983              : // Remove a block from the list.
     984              : 
     985              : basic_block
     986     12673524 : update_list::pop ()
     987              : {
     988     12673524 :   gcc_checking_assert (!empty_p ());
     989     12673524 :   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, m_update_head);
     990     12673524 :   int pop = m_update_head;
     991     12673524 :   m_update_head = m_update_list[pop];
     992     12673524 :   m_update_list[pop] = 0;
     993     12673524 :   return bb;
     994              : }
     995              : 
     996              : // --------------------------------------------------------------------------
     997              : 
     998     27888807 : ranger_cache::ranger_cache (int not_executable_flag, bool use_imm_uses)
     999              : {
    1000     27888807 :   m_workback = vNULL;
    1001     27888807 :   m_temporal = new temporal_cache;
    1002              : 
    1003              :   // If DOM info is available, spawn an oracle as well.
    1004     27888807 :   create_relation_oracle ();
    1005              :   // Create an infer oracle using this cache as the range query.  The cache
    1006              :   // version acts as a read-only query, and will spawn no additional lookups.
    1007              :   // It just ues what is already known.
    1008     27888807 :   create_infer_oracle (this, use_imm_uses);
    1009     27888807 :   create_gori (not_executable_flag, param_vrp_switch_limit);
    1010              : 
    1011     27888807 :   unsigned x, lim = last_basic_block_for_fn (cfun);
    1012              :   // Calculate outgoing range info upfront.  This will fully populate the
    1013              :   // m_maybe_variant bitmap which will help eliminate processing of names
    1014              :   // which never have their ranges adjusted.
    1015    355806659 :   for (x = 0; x < lim ; x++)
    1016              :     {
    1017    327917852 :       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
    1018    327917852 :       if (bb)
    1019    308969899 :         gori_ssa ()->exports (bb);
    1020              :     }
    1021     27888807 :   m_update = new update_list ();
    1022     27888807 :   m_stale = BITMAP_ALLOC (NULL);
    1023     27888807 : }
    1024              : 
    1025     27888805 : ranger_cache::~ranger_cache ()
    1026              : {
    1027     27888805 :   BITMAP_FREE (m_stale);
    1028     27888805 :   delete m_update;
    1029     27888805 :   destroy_infer_oracle ();
    1030     27888805 :   destroy_relation_oracle ();
    1031     55777610 :   delete m_temporal;
    1032     27888805 :   m_workback.release ();
    1033     27888805 : }
    1034              : 
    1035              : // Dump the global caches to file F.  if GORI_DUMP is true, dump the
    1036              : // gori map as well.
    1037              : 
    1038              : void
    1039           47 : ranger_cache::dump (FILE *f)
    1040              : {
    1041           47 :   fprintf (f, "Non-varying global ranges:\n");
    1042           47 :   fprintf (f, "=========================:\n");
    1043           47 :   m_globals.dump (f);
    1044           47 :   fprintf (f, "\n");
    1045           47 : }
    1046              : 
    1047              : // Dump the caches for basic block BB to file F.
    1048              : 
    1049              : void
    1050          252 : ranger_cache::dump_bb (FILE *f, basic_block bb)
    1051              : {
    1052          252 :   gori_ssa ()->dump (f, bb, false);
    1053          252 :   m_on_entry.dump (f, bb);
    1054          252 :   m_relation->dump (f, bb);
    1055          252 : }
    1056              : 
    1057              : // Get the global range for NAME, and return in R.  Return false if the
    1058              : // global range is not set, and return the legacy global value in R.
    1059              : 
    1060              : bool
    1061    841564719 : ranger_cache::get_global_range (vrange &r, tree name) const
    1062              : {
    1063    841564719 :   if (m_globals.get_range (r, name))
    1064              :     return true;
    1065    191434924 :   gimple_range_global (r, name);
    1066    191434924 :   return false;
    1067              : }
    1068              : 
    1069              : // Mark NAME as stale.  The next query of NAME forces a recalculation.
    1070              : 
    1071              : void
    1072      4121408 : ranger_cache::mark_stale (tree name)
    1073              : {
    1074              :   // Only mark it as stale if it has been processed. If it has no range
    1075              :   // it will be calculated at the next request anyway.
    1076      4121408 :   if (m_globals.has_range (name))
    1077      1553692 :     bitmap_set_bit (m_stale, SSA_NAME_VERSION (name));
    1078      4121408 : }
    1079              : 
    1080              : // Get the global range for NAME, and return in R.  Return false if the
    1081              : // global range is not set, and R will contain the legacy global value.
    1082              : // CURRENT_P is set to true if the value was in cache and not stale.
    1083              : // Otherwise, set CURRENT_P to false and mark as it always current.
    1084              : // If the global cache did not have a value, initialize it as well.
    1085              : // After this call, the global cache will have a value.
    1086              : 
    1087              : bool
    1088    347451865 : ranger_cache::get_global_range (vrange &r, tree name, bool &current_p)
    1089              : {
    1090    347451865 :   bool had_global = get_global_range (r, name);
    1091              : 
    1092              :   // If there was a global value, set current flag, otherwise set a value.
    1093    347451865 :   current_p = false;
    1094    347451865 :   if (had_global)
    1095    445565960 :     current_p = r.singleton_p ()
    1096    445361199 :                 || m_temporal->current_p (name, gori_ssa ()->depend1 (name),
    1097    222578219 :                                           gori_ssa ()->depend2 (name));
    1098              :   else
    1099              :     {
    1100              :       // If no global value has been set and value is VARYING, fold the stmt
    1101              :       // using just global ranges to get a better initial value.
    1102              :       // After inlining we tend to decide some things are constant, so
    1103              :       // so not do this evaluation after inlining.
    1104    124668885 :       if (r.varying_p () && !cfun->after_inlining)
    1105              :         {
    1106     20369067 :           gimple *s = SSA_NAME_DEF_STMT (name);
    1107              :           // Do not process PHIs as SCEV may be in use and it can
    1108              :           // spawn cyclic lookups.
    1109     20369067 :           if (gimple_get_lhs (s) == name && !is_a<gphi *> (s))
    1110              :             {
    1111     15930474 :               if (!fold_range (r, s, get_global_range_query ()))
    1112            0 :                 gimple_range_global (r, name);
    1113              :             }
    1114              :         }
    1115    124668885 :       m_globals.set_range (name, r);
    1116              :     }
    1117              : 
    1118              :   // If NAME is out of date, clear the bit and mark as not current.
    1119    347451865 :   if (bitmap_bit_p (m_stale, SSA_NAME_VERSION (name)))
    1120              :     {
    1121       410389 :       bitmap_clear_bit (m_stale, SSA_NAME_VERSION (name));
    1122       410389 :       current_p = false;
    1123              :     }
    1124              : 
    1125              :   // If the existing value was not current, mark it as always current.
    1126    347451865 :   if (!current_p)
    1127    136554907 :     m_temporal->set_always_current (name, true);
    1128    347451865 :   return had_global;
    1129              : }
    1130              : 
    1131              : // Consumers of NAME that have already calculated values should recalculate.
    1132              : // Accomplished by updating the timestamp.
    1133              : 
    1134              : void
    1135     60804292 : ranger_cache::update_consumers (tree name)
    1136              : {
    1137     60804292 :   m_temporal->set_timestamp (name);
    1138     60804292 : }
    1139              : 
    1140              : //  Set the global range of NAME to R and give it a timestamp.
    1141              : 
    1142              : void
    1143    136772507 : ranger_cache::set_global_range (tree name, const vrange &r, bool changed)
    1144              : {
    1145              :   // Setting a range always clears the always_current flag.
    1146    136772507 :   m_temporal->set_always_current (name, false);
    1147    136772507 :   if (!changed)
    1148              :     {
    1149              :       // If there are dependencies, make sure this is not out of date.
    1150    124324941 :       if (!m_temporal->current_p (name, gori_ssa ()->depend1 (name),
    1151    124324941 :                                  gori_ssa ()->depend2 (name)))
    1152     46558767 :         m_temporal->set_timestamp (name);
    1153    124324941 :       return;
    1154              :     }
    1155     12447566 :   if (m_globals.set_range (name, r))
    1156              :     {
    1157              :       // If there was already a range set, propagate the new value.
    1158     12392675 :       basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1159     12392675 :       if (!bb)
    1160         1518 :         bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1161              : 
    1162     12392675 :       if (DEBUG_RANGE_CACHE)
    1163            0 :         fprintf (dump_file, "   GLOBAL :");
    1164              : 
    1165     12392675 :       propagate_updated_value (name, bb);
    1166              :     }
    1167              :   // Constants no longer need to tracked.  Any further refinement has to be
    1168              :   // undefined. Propagation works better with constants. PR 100512.
    1169              :   // Pointers which resolve to non-zero also do not need
    1170              :   // tracking in the cache as they will never change.  See PR 98866.
    1171              :   // Timestamp must always be updated, or dependent calculations may
    1172              :   // not include this latest value. PR 100774.
    1173              : 
    1174              :   // With Points_to info in prange now, it is no longer acceptable to make
    1175              :   // [1, +INF] invariant, as most points to values will have that range,
    1176              :   // and then we lose the ability to propagate points to info.
    1177              : 
    1178     12447566 :   if (r.singleton_p ())
    1179       824381 :     gori_ssa ()->set_range_invariant (name);
    1180     12447566 :   m_temporal->set_timestamp (name);
    1181              : }
    1182              : 
    1183              : //  Provide lookup for the gori-computes class to access the best known range
    1184              : //  of an ssa_name in any given basic block.  Note, this does no additional
    1185              : //  lookups, just accesses the data that is already known.
    1186              : 
    1187              : // Get the range of NAME when the def occurs in block BB.  If BB is NULL
    1188              : // get the best global value available.
    1189              : 
    1190              : void
    1191    211766692 : ranger_cache::range_of_def (vrange &r, tree name, basic_block bb)
    1192              : {
    1193    211766692 :   gcc_checking_assert (gimple_range_ssa_p (name));
    1194    355711395 :   gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
    1195              : 
    1196              :   // Pick up the best global range available.
    1197    211766692 :   if (!m_globals.get_range (r, name))
    1198              :     {
    1199              :       // If that fails, try to calculate the range using just global values.
    1200     29414880 :       gimple *s = SSA_NAME_DEF_STMT (name);
    1201     29414880 :       if (gimple_get_lhs (s) == name)
    1202     26224283 :         fold_range (r, s, get_global_range_query ());
    1203              :       else
    1204      3190597 :         gimple_range_global (r, name);
    1205              :     }
    1206    211766692 : }
    1207              : 
    1208              : // Get the range of NAME as it occurs on entry to block BB.  Use MODE for
    1209              : // lookups.
    1210              : 
    1211              : void
    1212    150645022 : ranger_cache::entry_range (vrange &r, tree name, basic_block bb,
    1213              :                            enum rfd_mode mode)
    1214              : {
    1215    150645022 :   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1216              :     {
    1217            0 :       gimple_range_global (r, name);
    1218            0 :       return;
    1219              :     }
    1220              : 
    1221              :   // If NAME is invariant, simply return the defining range.
    1222    150645022 :   if (!gori ().has_edge_range_p (name))
    1223              :     {
    1224     32310653 :       range_of_def (r, name);
    1225     32310653 :       return;
    1226              :     }
    1227              : 
    1228              :   // Look for the on-entry value of name in BB from the cache.
    1229              :   // Otherwise pick up the best available global value.
    1230    118334369 :   if (!m_on_entry.get_bb_range (r, name, bb))
    1231     41889875 :     if (!range_from_dom (r, name, bb, mode))
    1232     35511336 :       range_of_def (r, name);
    1233              : }
    1234              : 
    1235              : // Get the range of NAME as it occurs on exit from block BB.  Use MODE for
    1236              : // lookups.
    1237              : 
    1238              : void
    1239    109909874 : ranger_cache::exit_range (vrange &r, tree name, basic_block bb,
    1240              :                           enum rfd_mode mode)
    1241              : {
    1242    109909874 :   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1243              :     {
    1244        61385 :       gimple_range_global (r, name);
    1245        61385 :       return;
    1246              :     }
    1247              : 
    1248    109848489 :   gimple *s = SSA_NAME_DEF_STMT (name);
    1249    109848489 :   basic_block def_bb = gimple_bb (s);
    1250    109848489 :   if (def_bb == bb)
    1251     45759885 :     range_of_def (r, name, bb);
    1252              :   else
    1253     64088604 :     entry_range (r, name, bb, mode);
    1254              : }
    1255              : 
    1256              : // Get the range of NAME on edge E using MODE, return the result in R.
    1257              : // Always returns a range and true.
    1258              : 
    1259              : bool
    1260     99824489 : ranger_cache::edge_range (vrange &r, edge e, tree name, enum rfd_mode mode)
    1261              : {
    1262     99824489 :   exit_range (r, name, e->src, mode);
    1263              :   // If this is not an abnormal edge, check for inferred ranges on exit.
    1264     99824489 :   if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
    1265     99541199 :     infer_oracle ().maybe_adjust_range (r, name, e->src);
    1266     99824489 :   value_range er (TREE_TYPE (name));
    1267     99824489 :   if (gori ().edge_range_p (er, e, name, *this))
    1268     22394711 :     r.intersect (er);
    1269    199648978 :   return true;
    1270     99824489 : }
    1271              : 
    1272              : 
    1273              : 
    1274              : // Implement range_of_expr.
    1275              : 
    1276              : bool
    1277    224675870 : ranger_cache::range_of_expr (vrange &r, tree name, gimple *stmt)
    1278              : {
    1279    224675870 :   if (!gimple_range_ssa_p (name))
    1280     39934634 :     get_tree_range (r, name, stmt);
    1281              :   /* If no context is provided, pick up the global value.  */
    1282    184741236 :   else if (!stmt)
    1283            0 :     get_global_range (r, name);
    1284              :   else
    1285              :     {
    1286    184741236 :       basic_block bb = gimple_bb (stmt);
    1287    184741236 :       gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1288    184741236 :       basic_block def_bb = gimple_bb (def_stmt);
    1289              : 
    1290    184741236 :       if (bb == def_bb)
    1291     98184818 :         range_of_def (r, name, bb);
    1292              :       else
    1293     86556418 :         entry_range (r, name, bb, RFD_NONE);
    1294              :     }
    1295    224675870 :   return true;
    1296              : }
    1297              : 
    1298              : 
    1299              : // Implement range_on_edge.  Always return the best available range using
    1300              : // the current cache values.
    1301              : 
    1302              : bool
    1303     74968344 : ranger_cache::range_on_edge (vrange &r, edge e, tree expr)
    1304              : {
    1305     74968344 :   if (gimple_range_ssa_p (expr))
    1306     72085023 :     return edge_range (r, e, expr, RFD_NONE);
    1307      2883321 :   return get_tree_range (r, expr, NULL);
    1308              : }
    1309              : 
    1310              : // Return a static range for NAME on entry to basic block BB in R.  If
    1311              : // calc is true, fill any cache entries required between BB and the
    1312              : // def block for NAME.  Otherwise, return false if the cache is empty.
    1313              : 
    1314              : bool
    1315    400019226 : ranger_cache::block_range (vrange &r, basic_block bb, tree name, bool calc)
    1316              : {
    1317    400019226 :   gcc_checking_assert (gimple_range_ssa_p (name));
    1318              : 
    1319              :   // If there are no range calculations anywhere in the IL, global range
    1320              :   // applies everywhere, so don't bother caching it.
    1321    400019226 :   if (!gori ().has_edge_range_p (name))
    1322              :     return false;
    1323              : 
    1324    250272714 :   if (calc)
    1325              :     {
    1326    123010517 :       gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1327    123010517 :       basic_block def_bb = NULL;
    1328    123010517 :       if (def_stmt)
    1329    123010517 :         def_bb = gimple_bb (def_stmt);
    1330    123010517 :       if (!def_bb)
    1331              :         {
    1332              :           // If we get to the entry block, this better be a default def
    1333              :           // or range_on_entry was called for a block not dominated by
    1334              :           // the def.  But it could be also SSA_NAME defined by a statement
    1335              :           // not yet in the IL (such as queued edge insertion), in that case
    1336              :           // just punt.
    1337     16265296 :           if (!SSA_NAME_IS_DEFAULT_DEF (name))
    1338              :             return false;
    1339     16265295 :           def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1340              :         }
    1341              : 
    1342              :       // There is no range on entry for the definition block.
    1343    123010516 :       if (def_bb == bb)
    1344              :         return false;
    1345              : 
    1346              :       // Otherwise, go figure out what is known in predecessor blocks.
    1347    122653916 :       fill_block_cache (name, bb, def_bb);
    1348    122653916 :       gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
    1349              :     }
    1350    249916113 :   return m_on_entry.get_bb_range (r, name, bb);
    1351              : }
    1352              : 
    1353              : // If there is anything in the propagation update_list, continue
    1354              : // processing NAME until the list of blocks is empty.
    1355              : 
    1356              : void
    1357      5867262 : ranger_cache::propagate_cache (tree name)
    1358              : {
    1359      5867262 :   basic_block bb;
    1360      5867262 :   edge_iterator ei;
    1361      5867262 :   edge e;
    1362      5867262 :   tree type = TREE_TYPE (name);
    1363      5867262 :   value_range new_range (type);
    1364      5867262 :   value_range current_range (type);
    1365      5867262 :   value_range e_range (type);
    1366              : 
    1367              :   // Process each block by seeing if its calculated range on entry is
    1368              :   // the same as its cached value. If there is a difference, update
    1369              :   // the cache to reflect the new value, and check to see if any
    1370              :   // successors have cache entries which may need to be checked for
    1371              :   // updates.
    1372              : 
    1373     24408048 :   while (!m_update->empty_p ())
    1374              :     {
    1375     12673524 :       bb = m_update->pop ();
    1376     12673524 :       gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
    1377     12673524 :       m_on_entry.get_bb_range (current_range, name, bb);
    1378              : 
    1379     12673524 :       if (DEBUG_RANGE_CACHE)
    1380              :         {
    1381            0 :           fprintf (dump_file, "FWD visiting block %d for ", bb->index);
    1382            0 :           print_generic_expr (dump_file, name, TDF_SLIM);
    1383            0 :           fprintf (dump_file, "  starting range : ");
    1384            0 :           current_range.dump (dump_file);
    1385            0 :           fprintf (dump_file, "\n");
    1386              :         }
    1387              : 
    1388              :       // Calculate the "new" range on entry by unioning the pred edges.
    1389     12673524 :       new_range.set_undefined ();
    1390     26786853 :       FOR_EACH_EDGE (e, ei, bb->preds)
    1391              :         {
    1392     17620405 :           edge_range (e_range, e, name, RFD_READ_ONLY);
    1393     17620405 :           if (DEBUG_RANGE_CACHE)
    1394              :             {
    1395            0 :               fprintf (dump_file, "   edge %d->%d :", e->src->index, bb->index);
    1396            0 :               e_range.dump (dump_file);
    1397            0 :               fprintf (dump_file, "\n");
    1398              :             }
    1399     17620405 :           new_range.union_ (e_range);
    1400     17620405 :           if (new_range.varying_p ())
    1401              :             break;
    1402              :         }
    1403              : 
    1404              :       // If the range on entry has changed, update it.
    1405     12673524 :       if (new_range != current_range)
    1406              :         {
    1407      7254901 :           bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
    1408              :           // If the cache couldn't set the value, mark it as failed.
    1409      7254901 :           if (!ok_p)
    1410            6 :             m_update->propagation_failed (bb);
    1411      7254901 :           if (DEBUG_RANGE_CACHE)
    1412              :             {
    1413            0 :               if (!ok_p)
    1414              :                 {
    1415            0 :                   fprintf (dump_file, "   Cache failure to store value:");
    1416            0 :                   print_generic_expr (dump_file, name, TDF_SLIM);
    1417            0 :                   fprintf (dump_file, "  ");
    1418              :                 }
    1419              :               else
    1420              :                 {
    1421            0 :                   fprintf (dump_file, "      Updating range to ");
    1422            0 :                   new_range.dump (dump_file);
    1423              :                 }
    1424            0 :               fprintf (dump_file, "\n      Updating blocks :");
    1425              :             }
    1426              :           // Mark each successor that has a range to re-check its range
    1427     18592978 :           FOR_EACH_EDGE (e, ei, bb->succs)
    1428     11338077 :             if (m_on_entry.bb_range_p (name, e->dest))
    1429              :               {
    1430      6879396 :                 if (DEBUG_RANGE_CACHE)
    1431            0 :                   fprintf (dump_file, " bb%d",e->dest->index);
    1432      6879396 :                 m_update->add (e->dest);
    1433              :               }
    1434      7254901 :           if (DEBUG_RANGE_CACHE)
    1435            0 :             fprintf (dump_file, "\n");
    1436              :         }
    1437              :     }
    1438      5867262 :   if (DEBUG_RANGE_CACHE)
    1439              :     {
    1440            0 :       fprintf (dump_file, "DONE visiting blocks for ");
    1441            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1442            0 :       fprintf (dump_file, "\n");
    1443              :     }
    1444      5867262 :   m_update->clear_failures ();
    1445      5867262 : }
    1446              : 
    1447              : // Check to see if an update to the value for NAME in BB has any effect
    1448              : // on values already in the on-entry cache for successor blocks.
    1449              : // If it does, update them.  Don't visit any blocks which don't have a cache
    1450              : // entry.
    1451              : 
    1452              : void
    1453     54443708 : ranger_cache::propagate_updated_value (tree name, basic_block bb)
    1454              : {
    1455     54443708 :   edge e;
    1456     54443708 :   edge_iterator ei;
    1457              : 
    1458              :   // The update work list should be empty at this point.
    1459     54443708 :   gcc_checking_assert (m_update->empty_p ());
    1460     54443708 :   gcc_checking_assert (bb);
    1461              : 
    1462     54443708 :   if (DEBUG_RANGE_CACHE)
    1463              :     {
    1464            0 :       fprintf (dump_file, " UPDATE cache for ");
    1465            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1466            0 :       fprintf (dump_file, " in BB %d : successors : ", bb->index);
    1467              :     }
    1468    157980845 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1469              :     {
    1470              :       // Only update active cache entries.
    1471    103537137 :       if (m_on_entry.bb_range_p (name, e->dest))
    1472              :         {
    1473      4988385 :           m_update->add (e->dest);
    1474      4988385 :           if (DEBUG_RANGE_CACHE)
    1475            0 :             fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
    1476              :         }
    1477              :     }
    1478     54443708 :     if (!m_update->empty_p ())
    1479              :       {
    1480      4917338 :         if (DEBUG_RANGE_CACHE)
    1481            0 :           fprintf (dump_file, "\n");
    1482      4917338 :         propagate_cache (name);
    1483              :       }
    1484              :     else
    1485              :       {
    1486     49526370 :         if (DEBUG_RANGE_CACHE)
    1487            0 :           fprintf (dump_file, "  : No updates!\n");
    1488              :       }
    1489     54443708 : }
    1490              : 
    1491              : // Make sure that the range-on-entry cache for NAME is set for block BB.
    1492              : // Work back through the CFG to DEF_BB ensuring the range is calculated
    1493              : // on the block/edges leading back to that point.
    1494              : 
    1495              : void
    1496    122653916 : ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
    1497              : {
    1498    122653916 :   edge_iterator ei;
    1499    122653916 :   edge e;
    1500    122653916 :   tree type = TREE_TYPE (name);
    1501    122653916 :   value_range block_result (type);
    1502    122653916 :   value_range undefined (type);
    1503              : 
    1504              :   // At this point we shouldn't be looking at the def, entry block.
    1505    122653916 :   gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
    1506    122653916 :   unsigned start_length = m_workback.length ();
    1507              : 
    1508              :   // If the block cache is set, then we've already visited this block.
    1509    122653916 :   if (m_on_entry.bb_range_p (name, bb))
    1510              :     return;
    1511              : 
    1512     51312992 :   if (DEBUG_RANGE_CACHE)
    1513              :     {
    1514            0 :       fprintf (dump_file, "\n");
    1515            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1516            0 :       fprintf (dump_file, " : ");
    1517              :     }
    1518              : 
    1519              :   // Check if a dominators can supply the range.
    1520     51312992 :   if (range_from_dom (block_result, name, bb, RFD_FILL))
    1521              :     {
    1522     50363068 :       if (DEBUG_RANGE_CACHE)
    1523              :         {
    1524            0 :           fprintf (dump_file, "Filled from dominator! :  ");
    1525            0 :           block_result.dump (dump_file);
    1526            0 :           fprintf (dump_file, "\n");
    1527              :         }
    1528              :       // See if any equivalences can refine it.
    1529              :       // PR 109462, like 108139 below, a one way equivalence introduced
    1530              :       // by a PHI node can also be through the definition side.  Disallow it.
    1531     50363068 :       tree equiv_name;
    1532     50363068 :       relation_kind rel;
    1533     50363068 :       int prec = TYPE_PRECISION (type);
    1534              :       // If there are too many basic blocks, do not attempt to process
    1535              :       // equivalencies.
    1536     50363068 :       if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
    1537              :         {
    1538       388347 :           m_on_entry.set_bb_range (name, bb, block_result);
    1539       776662 :           gcc_checking_assert (m_workback.length () == start_length);
    1540              :           return;
    1541              :         }
    1542     59329888 :       FOR_EACH_PARTIAL_AND_FULL_EQUIV (m_relation, bb, name, equiv_name, rel)
    1543              :         {
    1544      9355167 :           basic_block equiv_bb = gimple_bb (SSA_NAME_DEF_STMT (equiv_name));
    1545              : 
    1546              :           // Ignore partial equivs that are smaller than this object.
    1547     16726740 :           if (rel != VREL_EQ && prec > pe_to_bits (rel))
    1548      3574854 :             continue;
    1549              : 
    1550              :           // Check if the equiv has any ranges calculated.
    1551      8343270 :           if (!gori ().has_edge_range_p (equiv_name))
    1552       362848 :             continue;
    1553              : 
    1554              :           // Check if the equiv definition dominates this block
    1555      7980422 :           if (equiv_bb == bb ||
    1556      7767256 :               (equiv_bb && !dominated_by_p (CDI_DOMINATORS, bb, equiv_bb)))
    1557      2200109 :             continue;
    1558              : 
    1559      5780313 :           if (DEBUG_RANGE_CACHE)
    1560              :             {
    1561            0 :               if (rel == VREL_EQ)
    1562            0 :                 fprintf (dump_file, "Checking Equivalence (");
    1563              :               else
    1564            0 :                 fprintf (dump_file, "Checking Partial equiv (");
    1565            0 :               print_relation (dump_file, rel);
    1566            0 :               fprintf (dump_file, ") ");
    1567            0 :               print_generic_expr (dump_file, equiv_name, TDF_SLIM);
    1568            0 :               fprintf (dump_file, "\n");
    1569              :             }
    1570      5780313 :           value_range equiv_range (TREE_TYPE (equiv_name));
    1571      5780313 :           if (range_from_dom (equiv_range, equiv_name, bb, RFD_READ_ONLY))
    1572              :             {
    1573      5780313 :               if (rel != VREL_EQ)
    1574      4056036 :                 range_cast (equiv_range, type);
    1575              :               else
    1576      1724277 :                 adjust_equivalence_range (equiv_range);
    1577              : 
    1578      5780313 :               if (block_result.intersect (equiv_range))
    1579              :                 {
    1580       329197 :                   if (DEBUG_RANGE_CACHE)
    1581              :                     {
    1582            0 :                       if (rel == VREL_EQ)
    1583            0 :                         fprintf (dump_file, "Equivalence update! :  ");
    1584              :                       else
    1585            0 :                         fprintf (dump_file, "Partial equiv update! :  ");
    1586            0 :                       print_generic_expr (dump_file, equiv_name, TDF_SLIM);
    1587            0 :                       fprintf (dump_file, " has range  :  ");
    1588            0 :                       equiv_range.dump (dump_file);
    1589            0 :                       fprintf (dump_file, " refining range to :");
    1590            0 :                       block_result.dump (dump_file);
    1591            0 :                       fprintf (dump_file, "\n");
    1592              :                     }
    1593              :                 }
    1594              :             }
    1595      5780313 :         }
    1596              : 
    1597     49974721 :       m_on_entry.set_bb_range (name, bb, block_result);
    1598     97358078 :       gcc_checking_assert (m_workback.length () == start_length);
    1599              :       return;
    1600              :     }
    1601              : 
    1602              :   // Visit each block back to the DEF.  Initialize each one to UNDEFINED.
    1603              :   // m_visited at the end will contain all the blocks that we needed to set
    1604              :   // the range_on_entry cache for.
    1605       949924 :   m_workback.safe_push (bb);
    1606       949924 :   undefined.set_undefined ();
    1607       949924 :   m_on_entry.set_bb_range (name, bb, undefined);
    1608       949924 :   gcc_checking_assert (m_update->empty_p ());
    1609              : 
    1610      6243907 :   while (m_workback.length () > start_length)
    1611              :     {
    1612      5293983 :       basic_block node = m_workback.pop ();
    1613      5293983 :       if (DEBUG_RANGE_CACHE)
    1614              :         {
    1615            0 :           fprintf (dump_file, "BACK visiting block %d for ", node->index);
    1616            0 :           print_generic_expr (dump_file, name, TDF_SLIM);
    1617            0 :           fprintf (dump_file, "\n");
    1618              :         }
    1619              : 
    1620     12657221 :       FOR_EACH_EDGE (e, ei, node->preds)
    1621              :         {
    1622      7363238 :           basic_block pred = e->src;
    1623      7363238 :           value_range r (TREE_TYPE (name));
    1624              : 
    1625      7363238 :           if (DEBUG_RANGE_CACHE)
    1626            0 :             fprintf (dump_file, "  %d->%d ",e->src->index, e->dest->index);
    1627              : 
    1628              :           // If the pred block is the def block add this BB to update list.
    1629      7363238 :           if (pred == def_bb)
    1630              :             {
    1631       901050 :               m_update->add (node);
    1632       901050 :               continue;
    1633              :             }
    1634              : 
    1635              :           // If the pred is entry but NOT def, then it is used before
    1636              :           // defined, it'll get set to [] and no need to update it.
    1637      6462188 :           if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1638              :             {
    1639          100 :               if (DEBUG_RANGE_CACHE)
    1640            0 :                 fprintf (dump_file, "entry: bail.");
    1641          100 :               continue;
    1642              :             }
    1643              : 
    1644              :           // Regardless of whether we have visited pred or not, if the
    1645              :           // pred has inferred ranges, revisit this block.
    1646              :           // Don't search the DOM tree.
    1647      6462088 :           if (infer_oracle ().has_range_p (pred, name))
    1648              :             {
    1649        11898 :               if (DEBUG_RANGE_CACHE)
    1650            0 :                 fprintf (dump_file, "Inferred range: update ");
    1651        11898 :               m_update->add (node);
    1652              :             }
    1653              : 
    1654              :           // If the pred block already has a range, or if it can contribute
    1655              :           // something new. Ie, the edge generates a range of some sort.
    1656      6462088 :           if (m_on_entry.get_bb_range (r, name, pred))
    1657              :             {
    1658      2118029 :               if (DEBUG_RANGE_CACHE)
    1659              :                 {
    1660            0 :                   fprintf (dump_file, "has cache, ");
    1661            0 :                   r.dump (dump_file);
    1662            0 :                   fprintf (dump_file, ", ");
    1663              :                 }
    1664      2118029 :               if (!r.undefined_p () || gori ().has_edge_range_p (name, e))
    1665              :                 {
    1666       571736 :                   m_update->add (node);
    1667       571736 :                   if (DEBUG_RANGE_CACHE)
    1668            0 :                     fprintf (dump_file, "update. ");
    1669              :                 }
    1670      2118029 :               continue;
    1671              :             }
    1672              : 
    1673      4344059 :           if (DEBUG_RANGE_CACHE)
    1674            0 :             fprintf (dump_file, "pushing undefined pred block.\n");
    1675              :           // If the pred hasn't been visited (has no range), add it to
    1676              :           // the list.
    1677      4344059 :           gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
    1678      4344059 :           m_on_entry.set_bb_range (name, pred, undefined);
    1679      4344059 :           m_workback.safe_push (pred);
    1680      7363238 :         }
    1681              :     }
    1682              : 
    1683       949924 :   if (DEBUG_RANGE_CACHE)
    1684            0 :     fprintf (dump_file, "\n");
    1685              : 
    1686              :   // Now fill in the marked blocks with values.
    1687       949924 :   propagate_cache (name);
    1688       949924 :   if (DEBUG_RANGE_CACHE)
    1689            0 :     fprintf (dump_file, "  Propagation update done.\n");
    1690    122653916 : }
    1691              : 
    1692              : // Resolve the range of BB if the dominators range is R by calculating incoming
    1693              : // edges to this block.  All lead back to the dominator so should be cheap.
    1694              : // The range for BB is set and returned in R.
    1695              : 
    1696              : void
    1697      4319273 : ranger_cache::resolve_dom (vrange &r, tree name, basic_block bb)
    1698              : {
    1699      4319273 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1700      4319273 :   basic_block dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
    1701              : 
    1702              :   // if it doesn't already have a value, store the incoming range.
    1703      4319273 :   if (!m_on_entry.bb_range_p (name, dom_bb) && def_bb != dom_bb)
    1704              :     {
    1705              :       // If the range can't be store, don't try to accumulate
    1706              :       // the range in PREV_BB due to excessive recalculations.
    1707      1152078 :       if (!m_on_entry.set_bb_range (name, dom_bb, r))
    1708            0 :         return;
    1709              :     }
    1710              :   // With the dominator set, we should be able to cheaply query
    1711              :   // each incoming edge now and accumulate the results.
    1712      4319273 :   r.set_undefined ();
    1713      4319273 :   edge e;
    1714      4319273 :   edge_iterator ei;
    1715      4319273 :   value_range er (TREE_TYPE (name));
    1716     14460642 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1717              :     {
    1718              :       // If the predecessor is dominated by this block, then there is a back
    1719              :       // edge, and won't provide anything useful.  We'll actually end up with
    1720              :       // VARYING as we will not resolve this node.
    1721     10141369 :       if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
    1722        22308 :         continue;
    1723     10119061 :       edge_range (er, e, name, RFD_READ_ONLY);
    1724     10119061 :       r.union_ (er);
    1725              :     }
    1726              :   // Set the cache in PREV_BB so it is not calculated again.
    1727      4319273 :   m_on_entry.set_bb_range (name, bb, r);
    1728      4319273 : }
    1729              : 
    1730              : // Get the range of NAME from dominators of BB and return it in R.  Search the
    1731              : // dominator tree based on MODE.
    1732              : 
    1733              : bool
    1734     98983180 : ranger_cache::range_from_dom (vrange &r, tree name, basic_block start_bb,
    1735              :                               enum rfd_mode mode)
    1736              : {
    1737     98983180 :   if (mode == RFD_NONE || !dom_info_available_p (CDI_DOMINATORS))
    1738     36461260 :     return false;
    1739              : 
    1740              :   // Search back to the definition block or entry block.
    1741     62521920 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1742     62521920 :   if (def_bb == NULL)
    1743      7927256 :     def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1744              : 
    1745     62521920 :   basic_block bb;
    1746     62521920 :   basic_block prev_bb = start_bb;
    1747              : 
    1748              :   // Track any inferred ranges seen.
    1749     62521920 :   value_range infer (TREE_TYPE (name));
    1750     62521920 :   infer.set_varying (TREE_TYPE (name));
    1751              : 
    1752              :   // Range on entry to the DEF block should not be queried.
    1753     62521920 :   gcc_checking_assert (start_bb != def_bb);
    1754     62521920 :   unsigned start_limit = m_workback.length ();
    1755              : 
    1756              :   // Default value is global range.
    1757     62521920 :   get_global_range (r, name);
    1758              : 
    1759              :   // The dominator of EXIT_BLOCK doesn't seem to be set, so at least handle
    1760              :   // the common single exit cases.
    1761     62651255 :   if (start_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) && single_pred_p (start_bb))
    1762       129086 :     bb = single_pred_edge (start_bb)->src;
    1763              :   else
    1764     62392834 :     bb = get_immediate_dominator (CDI_DOMINATORS, start_bb);
    1765              : 
    1766              :   // Search until a value is found, pushing blocks which may need calculating.
    1767    368875265 :   for ( ; bb; prev_bb = bb, bb = get_immediate_dominator (CDI_DOMINATORS, bb))
    1768              :     {
    1769              :       // Accumulate any block exit inferred ranges.
    1770    368073222 :       infer_oracle ().maybe_adjust_range (infer, name, bb);
    1771              : 
    1772              :       // This block has an outgoing range.
    1773    368073222 :       if (gori ().has_edge_range_p (name, bb))
    1774     43878148 :         m_workback.safe_push (prev_bb);
    1775              :       else
    1776              :         {
    1777              :           // Normally join blocks don't carry any new range information on
    1778              :           // incoming edges.  If the first incoming edge to this block does
    1779              :           // generate a range, calculate the ranges if all incoming edges
    1780              :           // are also dominated by the dominator.  (Avoids backedges which
    1781              :           // will break the rule of moving only upward in the dominator tree).
    1782              :           // If the first pred does not generate a range, then we will be
    1783              :           // using the dominator range anyway, so that's all the check needed.
    1784    324195074 :           if (EDGE_COUNT (prev_bb->preds) > 1
    1785    324195074 :               && gori ().has_edge_range_p (name, EDGE_PRED (prev_bb, 0)->src))
    1786              :             {
    1787       724030 :               edge e;
    1788       724030 :               edge_iterator ei;
    1789       724030 :               bool all_dom = true;
    1790      2463828 :               FOR_EACH_EDGE (e, ei, prev_bb->preds)
    1791      1739798 :                 if (e->src != bb
    1792      1739798 :                     && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
    1793              :                   {
    1794              :                     all_dom = false;
    1795              :                     break;
    1796              :                   }
    1797       724030 :               if (all_dom)
    1798       724030 :                 m_workback.safe_push (prev_bb);
    1799              :             }
    1800              :         }
    1801              : 
    1802    368073222 :       if (def_bb == bb)
    1803              :         break;
    1804              : 
    1805    329389056 :       if (m_on_entry.get_bb_range (r, name, bb))
    1806              :         break;
    1807              :     }
    1808              : 
    1809     62521920 :   if (DEBUG_RANGE_CACHE)
    1810              :     {
    1811            0 :       fprintf (dump_file, "CACHE: BB %d DOM query for ", start_bb->index);
    1812            0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1813            0 :       fprintf (dump_file, ", found ");
    1814            0 :       r.dump (dump_file);
    1815            0 :       if (bb)
    1816            0 :         fprintf (dump_file, " at BB%d\n", bb->index);
    1817              :       else
    1818            0 :         fprintf (dump_file, " at function top\n");
    1819              :     }
    1820              : 
    1821              :   // Now process any blocks wit incoming edges that nay have adjustments.
    1822    107124098 :   while (m_workback.length () > start_limit)
    1823              :     {
    1824     44602178 :       value_range er (TREE_TYPE (name));
    1825     44602178 :       prev_bb = m_workback.pop ();
    1826     44602178 :       if (!single_pred_p (prev_bb))
    1827              :         {
    1828              :           // Non single pred means we need to cache a value in the dominator
    1829              :           // so we can cheaply calculate incoming edges to this block, and
    1830              :           // then store the resulting value.  If processing mode is not
    1831              :           // RFD_FILL, then the cache cant be stored to, so don't try.
    1832              :           // Otherwise this becomes a quadratic timed calculation.
    1833      6452556 :           if (mode == RFD_FILL)
    1834      4319273 :             resolve_dom (r, name, prev_bb);
    1835      6452556 :           continue;
    1836              :         }
    1837              : 
    1838     38149622 :       edge e = single_pred_edge (prev_bb);
    1839     38149622 :       bb = e->src;
    1840     38149622 :       if (gori ().edge_range_p (er, e, name, *this))
    1841              :         {
    1842     34522418 :           r.intersect (er);
    1843              :           // If this is a normal edge, apply any inferred ranges.
    1844     34522418 :           if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
    1845     34522418 :             infer_oracle ().maybe_adjust_range (r, name, bb);
    1846              : 
    1847     34522418 :           if (DEBUG_RANGE_CACHE)
    1848              :             {
    1849            0 :               fprintf (dump_file, "CACHE: Adjusted edge range for %d->%d : ",
    1850              :                        bb->index, prev_bb->index);
    1851            0 :               r.dump (dump_file);
    1852            0 :               fprintf (dump_file, "\n");
    1853              :             }
    1854              :         }
    1855     44602178 :     }
    1856              : 
    1857              :   // Apply non-null if appropriate.
    1858     62521920 :   if (!has_abnormal_call_or_eh_pred_edge_p (start_bb))
    1859     62366298 :     r.intersect (infer);
    1860              : 
    1861     62521920 :   if (DEBUG_RANGE_CACHE)
    1862              :     {
    1863            0 :       fprintf (dump_file, "CACHE: Range for DOM returns : ");
    1864            0 :       r.dump (dump_file);
    1865            0 :       fprintf (dump_file, "\n");
    1866              :     }
    1867     62521920 :   return true;
    1868     62521920 : }
    1869              : 
    1870              : // This routine will register an inferred value in block BB, and possibly
    1871              : // update the on-entry cache if appropriate.
    1872              : 
    1873              : void
    1874     16180423 : ranger_cache::register_inferred_value (const vrange &ir, tree name,
    1875              :                                        basic_block bb)
    1876              : {
    1877     16180423 :   value_range r (TREE_TYPE (name));
    1878     16180423 :   if (!m_on_entry.get_bb_range (r, name, bb))
    1879     10085385 :     exit_range (r, name, bb, RFD_READ_ONLY);
    1880     16180423 :   if (r.intersect (ir))
    1881              :     {
    1882      4803908 :       m_on_entry.set_bb_range (name, bb, r);
    1883              :       // If this range was invariant before, remove invariant.
    1884      4803908 :       if (!gori ().has_edge_range_p (name))
    1885      4011335 :         gori_ssa ()->set_range_invariant (name, false);
    1886              :     }
    1887     16180423 : }
    1888              : 
    1889              : // This routine is used during a block walk to adjust any inferred ranges
    1890              : // of operands on stmt S.
    1891              : 
    1892              : void
    1893    251126388 : ranger_cache::apply_inferred_ranges (gimple *s)
    1894              : {
    1895    251126388 :   bool update = true;
    1896              : 
    1897    251126388 :   basic_block bb = gimple_bb (s);
    1898    251126388 :   gimple_infer_range infer(s, this);
    1899    251126388 :   if (infer.num () == 0)
    1900              :     return;
    1901              : 
    1902              :   // Do not update the on-entry cache for block ending stmts.
    1903     15883647 :   if (stmt_ends_bb_p (s))
    1904              :     {
    1905      1137910 :       edge_iterator ei;
    1906      1137910 :       edge e;
    1907      2067810 :       FOR_EACH_EDGE (e, ei, gimple_bb (s)->succs)
    1908      2062331 :         if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
    1909              :           break;
    1910      1137910 :       if (e == NULL)
    1911         5479 :         update = false;
    1912              :     }
    1913              : 
    1914     15883647 :   infer_oracle ().add_ranges (s, infer);
    1915     15883647 :   if (update)
    1916     32038810 :     for (unsigned x = 0; x < infer.num (); x++)
    1917     16160642 :       register_inferred_value (infer.range (x), infer.name (x), bb);
    1918              : }
        

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.