LCOV - code coverage report
Current view: top level - gcc - gimple-range-cache.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 80.2 % 788 632
Test Date: 2024-04-13 14:00:49 Functions: 87.3 % 71 62
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Gimple ranger SSA cache implementation.
       2                 :             :    Copyright (C) 2017-2024 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                 :    20980564 :   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                 :    20978623 : sbr_vector::sbr_vector (tree t, vrange_allocator *allocator, bool zero_p)
     102                 :    20978623 :   : ssa_block_ranges (t)
     103                 :             : {
     104                 :    20978623 :   gcc_checking_assert (TYPE_P (t));
     105                 :    20978623 :   m_type = t;
     106                 :    20978623 :   m_zero_p = zero_p;
     107                 :    20978623 :   m_range_allocator = allocator;
     108                 :    20978623 :   m_tab_size = last_basic_block_for_fn (cfun) + 1;
     109                 :    41957246 :   m_tab = static_cast <vrange_storage **>
     110                 :    20978623 :     (allocator->alloc (m_tab_size * sizeof (vrange_storage *)));
     111                 :    20978623 :   if (zero_p)
     112                 :    18535499 :     memset (m_tab, 0, m_tab_size * sizeof (vrange *));
     113                 :             : 
     114                 :             :   // Create the cached type range.
     115                 :    20978623 :   m_varying = m_range_allocator->clone_varying (t);
     116                 :    20978623 :   m_undefined = m_range_allocator->clone_undefined (t);
     117                 :    20978623 : }
     118                 :             : 
     119                 :             : // Grow the vector when the CFG has increased in size.
     120                 :             : 
     121                 :             : void
     122                 :           0 : sbr_vector::grow ()
     123                 :             : {
     124                 :           0 :   int curr_bb_size = last_basic_block_for_fn (cfun);
     125                 :           0 :   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                 :           0 :   int inc = MAX ((curr_bb_size - m_tab_size) * 2, 128);
     129                 :           0 :   inc = MAX (inc, curr_bb_size / 10);
     130                 :           0 :   int new_size = inc + curr_bb_size;
     131                 :             : 
     132                 :             :   // Allocate new memory, copy the old vector and clear the new space.
     133                 :           0 :   vrange_storage **t = static_cast <vrange_storage **>
     134                 :           0 :     (m_range_allocator->alloc (new_size * sizeof (vrange_storage *)));
     135                 :           0 :   memcpy (t, m_tab, m_tab_size * sizeof (vrange_storage *));
     136                 :           0 :   if (m_zero_p)
     137                 :           0 :     memset (t + m_tab_size, 0, (new_size - m_tab_size) * sizeof (vrange_storage *));
     138                 :             : 
     139                 :           0 :   m_tab = t;
     140                 :           0 :   m_tab_size = new_size;
     141                 :           0 : }
     142                 :             : 
     143                 :             : // Set the range for block BB to be R.
     144                 :             : 
     145                 :             : bool
     146                 :    47979109 : sbr_vector::set_bb_range (const_basic_block bb, const vrange &r)
     147                 :             : {
     148                 :    47979109 :   vrange_storage *m;
     149                 :    47979109 :   if (bb->index >= m_tab_size)
     150                 :           0 :     grow ();
     151                 :    47979109 :   if (r.varying_p ())
     152                 :    17148253 :     m = m_varying;
     153                 :    30830856 :   else if (r.undefined_p ())
     154                 :      249575 :     m = m_undefined;
     155                 :             :   else
     156                 :    30581281 :     m = m_range_allocator->clone (r);
     157                 :    47979109 :   m_tab[bb->index] = m;
     158                 :    47979109 :   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                 :   221471392 : sbr_vector::get_bb_range (vrange &r, const_basic_block bb)
     166                 :             : {
     167                 :   221471392 :   if (bb->index >= m_tab_size)
     168                 :             :     return false;
     169                 :   221471392 :   vrange_storage *m = m_tab[bb->index];
     170                 :   221471392 :   if (m)
     171                 :             :     {
     172                 :   167223426 :       m->get_vrange (r, m_type);
     173                 :   167223426 :       return true;
     174                 :             :     }
     175                 :             :   return false;
     176                 :             : }
     177                 :             : 
     178                 :             : // Return true if a range is present.
     179                 :             : 
     180                 :             : bool
     181                 :   185165520 : sbr_vector::bb_range_p (const_basic_block bb)
     182                 :             : {
     183                 :   185165520 :   if (bb->index < m_tab_size)
     184                 :   185165520 :     return m_tab[bb->index] != NULL;
     185                 :             :   return false;
     186                 :             : }
     187                 :             : 
     188                 :             : // Like an sbr_vector, except it uses a bitmap to manage whetehr  vale 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                 :     2443124 : sbr_lazy_vector::sbr_lazy_vector (tree t, vrange_allocator *allocator,
     204                 :     2443124 :                                   bitmap_obstack *bm)
     205                 :     2443124 :   : sbr_vector (t, allocator, false)
     206                 :             : {
     207                 :     2443124 :   m_has_value = BITMAP_ALLOC (bm);
     208                 :     2443124 : }
     209                 :             : 
     210                 :             : bool
     211                 :     6248813 : sbr_lazy_vector::set_bb_range (const_basic_block bb, const vrange &r)
     212                 :             : {
     213                 :     6248813 :   sbr_vector::set_bb_range (bb, r);
     214                 :     6248813 :   bitmap_set_bit (m_has_value, bb->index);
     215                 :     6248813 :   return true;
     216                 :             : }
     217                 :             : 
     218                 :             : bool
     219                 :   139203060 : sbr_lazy_vector::get_bb_range (vrange &r, const_basic_block bb)
     220                 :             : {
     221                 :   139203060 :   if (bitmap_bit_p (m_has_value, bb->index))
     222                 :    23218406 :     return sbr_vector::get_bb_range (r, bb);
     223                 :             :   return false;
     224                 :             : }
     225                 :             : 
     226                 :             : bool
     227                 :    29161012 : sbr_lazy_vector::bb_range_p (const_basic_block bb)
     228                 :             : {
     229                 :    29161012 :   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                 :        1941 : sbr_sparse_bitmap::sbr_sparse_bitmap (tree t, vrange_allocator *allocator,
     264                 :        1941 :                                       bitmap_obstack *bm)
     265                 :        1941 :   : ssa_block_ranges (t)
     266                 :             : {
     267                 :        1941 :   gcc_checking_assert (TYPE_P (t));
     268                 :        1941 :   m_type = t;
     269                 :        1941 :   bitmap_initialize (&bitvec, bm);
     270                 :        1941 :   bitmap_tree_view (&bitvec);
     271                 :        1941 :   m_range_allocator = allocator;
     272                 :             :   // Pre-cache varying.
     273                 :        1941 :   m_range[0] = m_range_allocator->clone_varying (t);
     274                 :             :   // Pre-cache zero and non-zero values for pointers.
     275                 :        1941 :   if (POINTER_TYPE_P (t))
     276                 :             :     {
     277                 :          18 :       int_range<2> nonzero;
     278                 :          18 :       nonzero.set_nonzero (t);
     279                 :          18 :       m_range[1] = m_range_allocator->clone (nonzero);
     280                 :          18 :       int_range<2> zero;
     281                 :          18 :       zero.set_zero (t);
     282                 :          18 :       m_range[2] = m_range_allocator->clone (zero);
     283                 :          18 :     }
     284                 :             :   else
     285                 :        1923 :     m_range[1] = m_range[2] = NULL;
     286                 :             :   // Clear SBR_NUM entries.
     287                 :       23292 :   for (int x = 3; x < SBR_NUM; x++)
     288                 :       21351 :     m_range[x] = 0;
     289                 :        1941 : }
     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                 :      265743 : sbr_sparse_bitmap::bitmap_set_quad (bitmap head, int quad, int quad_value)
     297                 :             : {
     298                 :      265743 :   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                 :    12851264 : sbr_sparse_bitmap::bitmap_get_quad (const_bitmap head, int quad)
     306                 :             : {
     307                 :    25702528 :   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                 :      265743 : sbr_sparse_bitmap::set_bb_range (const_basic_block bb, const vrange &r)
     314                 :             : {
     315                 :      265743 :   if (r.undefined_p ())
     316                 :             :     {
     317                 :           0 :       bitmap_set_quad (&bitvec, bb->index, SBR_UNDEF);
     318                 :           0 :       return true;
     319                 :             :     }
     320                 :             : 
     321                 :             :   // Loop thru the values to see if R is already present.
     322                 :      421210 :   for (int x = 0; x < SBR_NUM; x++)
     323                 :      410207 :     if (!m_range[x] || m_range[x]->equal_p (r))
     324                 :             :       {
     325                 :      254740 :         if (!m_range[x])
     326                 :          39 :           m_range[x] = m_range_allocator->clone (r);
     327                 :      254740 :         bitmap_set_quad (&bitvec, bb->index, x + 1);
     328                 :      254740 :         return true;
     329                 :             :       }
     330                 :             :   // All values are taken, default to VARYING.
     331                 :       11003 :   bitmap_set_quad (&bitvec, bb->index, SBR_VARYING);
     332                 :       11003 :   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                 :    10886651 : sbr_sparse_bitmap::get_bb_range (vrange &r, const_basic_block bb)
     340                 :             : {
     341                 :    10886651 :   int value = bitmap_get_quad (&bitvec, bb->index);
     342                 :             : 
     343                 :    10886651 :   if (!value)
     344                 :             :     return false;
     345                 :             : 
     346                 :     1454782 :   gcc_checking_assert (value <= SBR_UNDEF);
     347                 :     1454782 :   if (value == SBR_UNDEF)
     348                 :           0 :     r.set_undefined ();
     349                 :             :   else
     350                 :     1454782 :     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                 :     1964613 : sbr_sparse_bitmap::bb_range_p (const_basic_block bb)
     358                 :             : {
     359                 :     1964613 :   return (bitmap_get_quad (&bitvec, bb->index) != 0);
     360                 :             : }
     361                 :             : 
     362                 :             : // -------------------------------------------------------------------------
     363                 :             : 
     364                 :             : // Initialize the block cache.
     365                 :             : 
     366                 :    23775692 : block_range_cache::block_range_cache ()
     367                 :             : {
     368                 :    23775692 :   bitmap_obstack_initialize (&m_bitmaps);
     369                 :    23775692 :   m_ssa_ranges.create (0);
     370                 :    47551384 :   m_ssa_ranges.safe_grow_cleared (num_ssa_names);
     371                 :    23775692 :   m_range_allocator = new vrange_allocator;
     372                 :    23775692 : }
     373                 :             : 
     374                 :             : // Remove any m_block_caches which have been created.
     375                 :             : 
     376                 :    23775692 : block_range_cache::~block_range_cache ()
     377                 :             : {
     378                 :    23775692 :   delete m_range_allocator;
     379                 :             :   // Release the vector itself.
     380                 :    23775692 :   m_ssa_ranges.release ();
     381                 :    23775692 :   bitmap_obstack_release (&m_bitmaps);
     382                 :    23775692 : }
     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                 :    48244852 : block_range_cache::set_bb_range (tree name, const_basic_block bb,
     389                 :             :                                  const vrange &r)
     390                 :             : {
     391                 :    48244852 :   unsigned v = SSA_NAME_VERSION (name);
     392                 :    96489704 :   if (v >= m_ssa_ranges.length ())
     393                 :           2 :     m_ssa_ranges.safe_grow_cleared (num_ssa_names);
     394                 :             : 
     395                 :    48244852 :   if (!m_ssa_ranges[v])
     396                 :             :     {
     397                 :             :       // Use sparse bitmap representation if there are too many basic blocks.
     398                 :    20980564 :       if (last_basic_block_for_fn (cfun) > param_vrp_sparse_threshold)
     399                 :             :         {
     400                 :        1941 :           void *r = m_range_allocator->alloc (sizeof (sbr_sparse_bitmap));
     401                 :        1941 :           m_ssa_ranges[v] = new (r) sbr_sparse_bitmap (TREE_TYPE (name),
     402                 :             :                                                        m_range_allocator,
     403                 :        1941 :                                                        &m_bitmaps);
     404                 :             :         }
     405                 :    20978623 :       else if (last_basic_block_for_fn (cfun) < param_vrp_vector_threshold)
     406                 :             :         {
     407                 :             :           // For small CFGs use the basic vector implemntation.
     408                 :    18535499 :           void *r = m_range_allocator->alloc (sizeof (sbr_vector));
     409                 :    18535499 :           m_ssa_ranges[v] = new (r) sbr_vector (TREE_TYPE (name),
     410                 :    18535499 :                                                 m_range_allocator);
     411                 :             :         }
     412                 :             :       else
     413                 :             :         {
     414                 :             :           // Otherwise use the sparse vector implementation.
     415                 :     2443124 :           void *r = m_range_allocator->alloc (sizeof (sbr_lazy_vector));
     416                 :     2443124 :           m_ssa_ranges[v] = new (r) sbr_lazy_vector (TREE_TYPE (name),
     417                 :             :                                                      m_range_allocator,
     418                 :     2443124 :                                                      &m_bitmaps);
     419                 :             :         }
     420                 :             :     }
     421                 :    48244852 :   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                 :   828735588 : block_range_cache::query_block_ranges (tree name)
     430                 :             : {
     431                 :   828735588 :   unsigned v = SSA_NAME_VERSION (name);
     432                 :   828735588 :   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                 :   538913981 : block_range_cache::get_bb_range (vrange &r, tree name, const_basic_block bb)
     444                 :             : {
     445                 :   538913981 :   ssa_block_ranges *ptr = query_block_ranges (name);
     446                 :   538913981 :   if (ptr)
     447                 :   348341441 :     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                 :   289821607 : block_range_cache::bb_range_p (tree name, const_basic_block bb)
     455                 :             : {
     456                 :   289821607 :   ssa_block_ranges *ptr = query_block_ranges (name);
     457                 :   289821607 :   if (ptr)
     458                 :   216291145 :     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                 :         259 : block_range_cache::dump (FILE *f, basic_block bb, bool print_varying)
     485                 :             : {
     486                 :         259 :   unsigned x;
     487                 :         259 :   bool summarize_varying = false;
     488                 :       27492 :   for (x = 1; x < m_ssa_ranges.length (); ++x)
     489                 :             :     {
     490                 :       13487 :       if (!m_ssa_ranges[x])
     491                 :       24347 :         continue;
     492                 :             : 
     493                 :        1371 :       if (!gimple_range_ssa_p (ssa_name (x)))
     494                 :         115 :         continue;
     495                 :             : 
     496                 :        1256 :       Value_Range r (TREE_TYPE (ssa_name (x)));
     497                 :        1256 :       if (m_ssa_ranges[x]->get_bb_range (r, bb))
     498                 :             :         {
     499                 :         217 :           if (!print_varying && r.varying_p ())
     500                 :             :             {
     501                 :           0 :               summarize_varying = true;
     502                 :           0 :               continue;
     503                 :             :             }
     504                 :         217 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     505                 :         217 :           fprintf (f, "\t");
     506                 :         217 :           r.dump(f);
     507                 :         217 :           fprintf (f, "\n");
     508                 :             :         }
     509                 :        1256 :     }
     510                 :             :   // If there were any varying entries, lump them all together.
     511                 :         259 :   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                 :         259 : }
     535                 :             : 
     536                 :             : // -------------------------------------------------------------------------
     537                 :             : 
     538                 :             : // Initialize an ssa cache.
     539                 :             : 
     540                 :    46610094 : ssa_cache::ssa_cache ()
     541                 :             : {
     542                 :    46610094 :   m_tab.create (0);
     543                 :    46610094 :   m_range_allocator = new vrange_allocator;
     544                 :    46610094 : }
     545                 :             : 
     546                 :             : // Deconstruct an ssa cache.
     547                 :             : 
     548                 :    46610094 : ssa_cache::~ssa_cache ()
     549                 :             : {
     550                 :    46610094 :   m_tab.release ();
     551                 :    46610094 :   delete m_range_allocator;
     552                 :    46610094 : }
     553                 :             : 
     554                 :             : // Enable a query to evaluate staements/ramnges based on picking up ranges
     555                 :             : // from just an ssa-cache.
     556                 :             : 
     557                 :             : bool
     558                 :     1636431 : ssa_cache::range_of_expr (vrange &r, tree expr, gimple *stmt)
     559                 :             : {
     560                 :     1636431 :   if (!gimple_range_ssa_p (expr))
     561                 :     1297598 :     return get_tree_range (r, expr, stmt);
     562                 :             : 
     563                 :      338833 :   if (!get_range (r, expr))
     564                 :       38184 :     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                 :           0 : ssa_cache::has_range (tree name) const
     572                 :             : {
     573                 :           0 :   unsigned v = SSA_NAME_VERSION (name);
     574                 :           0 :   if (v >= m_tab.length ())
     575                 :             :     return false;
     576                 :           0 :   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                 :   941554185 : ssa_cache::get_range (vrange &r, tree name) const
     584                 :             : {
     585                 :   941554185 :   unsigned v = SSA_NAME_VERSION (name);
     586                 :  1871507743 :   if (v >= m_tab.length ())
     587                 :             :     return false;
     588                 :             : 
     589                 :   929951363 :   vrange_storage *stow = m_tab[v];
     590                 :   929951363 :   if (!stow)
     591                 :             :     return false;
     592                 :   769562534 :   stow->get_vrange (r, TREE_TYPE (name));
     593                 :   769562534 :   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                 :   119669362 : ssa_cache::set_range (tree name, const vrange &r)
     601                 :             : {
     602                 :   119669362 :   unsigned v = SSA_NAME_VERSION (name);
     603                 :   232952418 :   if (v >= m_tab.length ())
     604                 :    12772694 :     m_tab.safe_grow_cleared (num_ssa_names + 1);
     605                 :             : 
     606                 :   119669362 :   vrange_storage *m = m_tab[v];
     607                 :   119669362 :   if (m && m->fits_p (r))
     608                 :    14974308 :     m->set_vrange (r);
     609                 :             :   else
     610                 :   104695054 :     m_tab[v] = m_range_allocator->clone (r);
     611                 :   119669362 :   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                 :         106 : ssa_cache::merge_range (tree name, const vrange &r)
     619                 :             : {
     620                 :         106 :   unsigned v = SSA_NAME_VERSION (name);
     621                 :         212 :   if (v >= m_tab.length ())
     622                 :           0 :     m_tab.safe_grow_cleared (num_ssa_names + 1);
     623                 :             : 
     624                 :         106 :   vrange_storage *m = m_tab[v];
     625                 :             :   // Check if this is a new value.
     626                 :         106 :   if (!m)
     627                 :           0 :     m_tab[v] = m_range_allocator->clone (r);
     628                 :             :   else
     629                 :             :     {
     630                 :         106 :       Value_Range curr (TREE_TYPE (name));
     631                 :         106 :       m->get_vrange (curr, TREE_TYPE (name));
     632                 :             :       // If there is no change, return false.
     633                 :         106 :       if (!curr.intersect (r))
     634                 :          91 :         return false;
     635                 :             : 
     636                 :          15 :       if (m->fits_p (curr))
     637                 :          15 :         m->set_vrange (curr);
     638                 :             :       else
     639                 :           0 :         m_tab[v] = m_range_allocator->clone (curr);
     640                 :         106 :     }
     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                 :          46 : ssa_cache::dump (FILE *f)
     668                 :             : {
     669                 :        1800 :   for (unsigned x = 1; x < num_ssa_names; x++)
     670                 :             :     {
     671                 :         854 :       if (!gimple_range_ssa_p (ssa_name (x)))
     672                 :         482 :         continue;
     673                 :         372 :       Value_Range r (TREE_TYPE (ssa_name (x)));
     674                 :             :       // Dump all non-varying ranges.
     675                 :         372 :       if (get_range (r, ssa_name (x)) && !r.varying_p ())
     676                 :             :         {
     677                 :         153 :           print_generic_expr (f, ssa_name (x), TDF_NONE);
     678                 :         153 :           fprintf (f, "  : ");
     679                 :         153 :           r.dump (f);
     680                 :         153 :           fprintf (f, "\n");
     681                 :             :         }
     682                 :         372 :     }
     683                 :             : 
     684                 :          46 : }
     685                 :             : 
     686                 :             : // Return true if NAME has an active range in the cache.
     687                 :             : 
     688                 :             : bool
     689                 :           0 : ssa_lazy_cache::has_range (tree name) const
     690                 :             : {
     691                 :           0 :   return bitmap_bit_p (active_p, SSA_NAME_VERSION (name));
     692                 :             : }
     693                 :             : 
     694                 :             : // Set range of NAME to R in a lazy cache.  Return FALSE if it did not already
     695                 :             : // have a range.
     696                 :             : 
     697                 :             : bool
     698                 :    86202540 : ssa_lazy_cache::set_range (tree name, const vrange &r)
     699                 :             : {
     700                 :    86202540 :   unsigned v = SSA_NAME_VERSION (name);
     701                 :    86202540 :   if (!bitmap_set_bit (active_p, v))
     702                 :             :     {
     703                 :             :       // There is already an entry, simply set it.
     704                 :     9826698 :       gcc_checking_assert (v < m_tab.length ());
     705                 :     9826698 :       return ssa_cache::set_range (name, r);
     706                 :             :     }
     707                 :   133671959 :   if (v >= m_tab.length ())
     708                 :    38159450 :     m_tab.safe_grow (num_ssa_names + 1);
     709                 :    76375842 :   m_tab[v] = m_range_allocator->clone (r);
     710                 :    76375842 :   return false;
     711                 :             : }
     712                 :             : 
     713                 :             : // If NAME has a range, intersect it with R, otherwise set it to R.
     714                 :             : // Return TRUE if the range is new or changes.
     715                 :             : 
     716                 :             : bool
     717                 :         301 : ssa_lazy_cache::merge_range (tree name, const vrange &r)
     718                 :             : {
     719                 :         301 :   unsigned v = SSA_NAME_VERSION (name);
     720                 :         301 :   if (!bitmap_set_bit (active_p, v))
     721                 :             :     {
     722                 :             :       // There is already an entry, simply merge it.
     723                 :         106 :       gcc_checking_assert (v < m_tab.length ());
     724                 :         106 :       return ssa_cache::merge_range (name, r);
     725                 :             :     }
     726                 :         390 :   if (v >= m_tab.length ())
     727                 :           0 :     m_tab.safe_grow (num_ssa_names + 1);
     728                 :         195 :   m_tab[v] = m_range_allocator->clone (r);
     729                 :         195 :   return true;
     730                 :             : }
     731                 :             : 
     732                 :             : // Return TRUE if NAME has a range, and return it in R.
     733                 :             : 
     734                 :             : bool
     735                 :   230702066 : ssa_lazy_cache::get_range (vrange &r, tree name) const
     736                 :             : {
     737                 :   230702066 :   if (!bitmap_bit_p (active_p, SSA_NAME_VERSION (name)))
     738                 :             :     return false;
     739                 :    98367695 :   return ssa_cache::get_range (r, name);
     740                 :             : }
     741                 :             : 
     742                 :             : // Remove NAME from the active range list.
     743                 :             : 
     744                 :             : void
     745                 :    43919541 : ssa_lazy_cache::clear_range (tree name)
     746                 :             : {
     747                 :    43919541 :   bitmap_clear_bit (active_p, SSA_NAME_VERSION (name));
     748                 :    43919541 : }
     749                 :             : 
     750                 :             : // Remove all ranges from the active range list.
     751                 :             : 
     752                 :             : void
     753                 :    29157724 : ssa_lazy_cache::clear ()
     754                 :             : {
     755                 :    29157724 :   bitmap_clear (active_p);
     756                 :    29157724 : }
     757                 :             : 
     758                 :             : // --------------------------------------------------------------------------
     759                 :             : 
     760                 :             : 
     761                 :             : // This class will manage the timestamps for each ssa_name.
     762                 :             : // When a value is calculated, the timestamp is set to the current time.
     763                 :             : // Current time is then incremented.  Any dependencies will already have
     764                 :             : // been calculated, and will thus have older timestamps.
     765                 :             : // If one of those values is ever calculated again, it will get a newer
     766                 :             : // timestamp, and the "current_p" check will fail.
     767                 :             : 
     768                 :             : class temporal_cache
     769                 :             : {
     770                 :             : public:
     771                 :             :   temporal_cache ();
     772                 :             :   ~temporal_cache ();
     773                 :             :   bool current_p (tree name, tree dep1, tree dep2) const;
     774                 :             :   void set_timestamp (tree name);
     775                 :             :   void set_always_current (tree name, bool value);
     776                 :             :   bool always_current_p (tree name) const;
     777                 :             : private:
     778                 :             :   int temporal_value (unsigned ssa) const;
     779                 :             :   int m_current_time;
     780                 :             :   vec <int> m_timestamp;
     781                 :             : };
     782                 :             : 
     783                 :             : inline
     784                 :    23775692 : temporal_cache::temporal_cache ()
     785                 :             : {
     786                 :    23775692 :   m_current_time = 1;
     787                 :    23775692 :   m_timestamp.create (0);
     788                 :    47551384 :   m_timestamp.safe_grow_cleared (num_ssa_names);
     789                 :    23775692 : }
     790                 :             : 
     791                 :             : inline
     792                 :    23775692 : temporal_cache::~temporal_cache ()
     793                 :             : {
     794                 :    23775692 :   m_timestamp.release ();
     795                 :    23775692 : }
     796                 :             : 
     797                 :             : // Return the timestamp value for SSA, or 0 if there isn't one.
     798                 :             : 
     799                 :             : inline int
     800                 :   473223909 : temporal_cache::temporal_value (unsigned ssa) const
     801                 :             : {
     802                 :   946447818 :   if (ssa >= m_timestamp.length ())
     803                 :             :     return 0;
     804                 :   473223909 :   return abs (m_timestamp[ssa]);
     805                 :             : }
     806                 :             : 
     807                 :             : // Return TRUE if the timestamp for NAME is newer than any of its dependents.
     808                 :             : // Up to 2 dependencies can be checked.
     809                 :             : 
     810                 :             : bool
     811                 :   288870366 : temporal_cache::current_p (tree name, tree dep1, tree dep2) const
     812                 :             : {
     813                 :   288870366 :   if (always_current_p (name))
     814                 :             :     return true;
     815                 :             : 
     816                 :             :   // Any non-registered dependencies will have a value of 0 and thus be older.
     817                 :             :   // Return true if time is newer than either dependent.
     818                 :   283874797 :   int ts = temporal_value (SSA_NAME_VERSION (name));
     819                 :   428834412 :   if (dep1 && ts < temporal_value (SSA_NAME_VERSION (dep1)))
     820                 :             :     return false;
     821                 :   305251904 :   if (dep2 && ts < temporal_value (SSA_NAME_VERSION (dep2)))
     822                 :             :     return false;
     823                 :             : 
     824                 :             :   return true;
     825                 :             : }
     826                 :             : 
     827                 :             : // This increments the global timer and sets the timestamp for NAME.
     828                 :             : 
     829                 :             : inline void
     830                 :    27728944 : temporal_cache::set_timestamp (tree name)
     831                 :             : {
     832                 :    27728944 :   unsigned v = SSA_NAME_VERSION (name);
     833                 :    55457888 :   if (v >= m_timestamp.length ())
     834                 :           0 :     m_timestamp.safe_grow_cleared (num_ssa_names + 20);
     835                 :    27728944 :   m_timestamp[v] = ++m_current_time;
     836                 :    27728944 : }
     837                 :             : 
     838                 :             : // Set the timestamp to 0, marking it as "always up to date".
     839                 :             : 
     840                 :             : inline void
     841                 :   218711446 : temporal_cache::set_always_current (tree name, bool value)
     842                 :             : {
     843                 :   218711446 :   unsigned v = SSA_NAME_VERSION (name);
     844                 :   437422892 :   if (v >= m_timestamp.length ())
     845                 :         308 :     m_timestamp.safe_grow_cleared (num_ssa_names + 20);
     846                 :             : 
     847                 :   218711446 :   int ts = abs (m_timestamp[v]);
     848                 :             :   // If this does not have a timestamp, create one.
     849                 :   218711446 :   if (ts == 0)
     850                 :   102001169 :     ts = ++m_current_time;
     851                 :   218711446 :   m_timestamp[v] = value ? -ts : ts;
     852                 :   218711446 : }
     853                 :             : 
     854                 :             : // Return true if NAME is always current.
     855                 :             : 
     856                 :             : inline bool
     857                 :   288870366 : temporal_cache::always_current_p (tree name) const
     858                 :             : {
     859                 :   288870366 :   unsigned v = SSA_NAME_VERSION (name);
     860                 :   288870366 :   if (v >= m_timestamp.length ())
     861                 :             :     return false;
     862                 :   288870366 :   return m_timestamp[v] <= 0;
     863                 :             : }
     864                 :             : 
     865                 :             : // --------------------------------------------------------------------------
     866                 :             : 
     867                 :             : // This class provides an abstraction of a list of blocks to be updated
     868                 :             : // by the cache.  It is currently a stack but could be changed.  It also
     869                 :             : // maintains a list of blocks which have failed propagation, and does not
     870                 :             : // enter any of those blocks into the list.
     871                 :             : 
     872                 :             : // A vector over the BBs is maintained, and an entry of 0 means it is not in
     873                 :             : // a list.  Otherwise, the entry is the next block in the list. -1 terminates
     874                 :             : // the list.  m_head points to the top of the list, -1 if the list is empty.
     875                 :             : 
     876                 :             : class update_list
     877                 :             : {
     878                 :             : public:
     879                 :             :   update_list ();
     880                 :             :   ~update_list ();
     881                 :             :   void add (basic_block bb);
     882                 :             :   basic_block pop ();
     883                 :   100090259 :   inline bool empty_p () { return m_update_head == -1; }
     884                 :     3135011 :   inline void clear_failures () { bitmap_clear (m_propfail); }
     885                 :           0 :   inline void propagation_failed (basic_block bb)
     886                 :           0 :                                   { bitmap_set_bit (m_propfail, bb->index); }
     887                 :             : private:
     888                 :             :   vec<int> m_update_list;
     889                 :             :   int m_update_head;
     890                 :             :   bitmap m_propfail;
     891                 :             : };
     892                 :             : 
     893                 :             : // Create an update list.
     894                 :             : 
     895                 :    23775692 : update_list::update_list ()
     896                 :             : {
     897                 :    23775692 :   m_update_list.create (0);
     898                 :    23775692 :   m_update_list.safe_grow_cleared (last_basic_block_for_fn (cfun) + 64);
     899                 :    23775692 :   m_update_head = -1;
     900                 :    23775692 :   m_propfail = BITMAP_ALLOC (NULL);
     901                 :    23775692 : }
     902                 :             : 
     903                 :             : // Destroy an update list.
     904                 :             : 
     905                 :    23775692 : update_list::~update_list ()
     906                 :             : {
     907                 :    23775692 :   m_update_list.release ();
     908                 :    23775692 :   BITMAP_FREE (m_propfail);
     909                 :    23775692 : }
     910                 :             : 
     911                 :             : // Add BB to the list of blocks to update, unless it's already in the list.
     912                 :             : 
     913                 :             : void
     914                 :     3410875 : update_list::add (basic_block bb)
     915                 :             : {
     916                 :     3410875 :   int i = bb->index;
     917                 :             :   // If propagation has failed for BB, or its already in the list, don't
     918                 :             :   // add it again.
     919                 :     6821750 :   if ((unsigned)i >= m_update_list.length ())
     920                 :           0 :     m_update_list.safe_grow_cleared (i + 64);
     921                 :     3410875 :   if (!m_update_list[i] && !bitmap_bit_p (m_propfail, i))
     922                 :             :     {
     923                 :     3389894 :       if (empty_p ())
     924                 :             :         {
     925                 :     3241683 :           m_update_head = i;
     926                 :     3241683 :           m_update_list[i] = -1;
     927                 :             :         }
     928                 :             :       else
     929                 :             :         {
     930                 :      148211 :           gcc_checking_assert (m_update_head > 0);
     931                 :      148211 :           m_update_list[i] = m_update_head;
     932                 :      148211 :           m_update_head = i;
     933                 :             :         }
     934                 :             :     }
     935                 :     3410875 : }
     936                 :             : 
     937                 :             : // Remove a block from the list.
     938                 :             : 
     939                 :             : basic_block
     940                 :     3389894 : update_list::pop ()
     941                 :             : {
     942                 :     3389894 :   gcc_checking_assert (!empty_p ());
     943                 :     3389894 :   basic_block bb = BASIC_BLOCK_FOR_FN (cfun, m_update_head);
     944                 :     3389894 :   int pop = m_update_head;
     945                 :     3389894 :   m_update_head = m_update_list[pop];
     946                 :     3389894 :   m_update_list[pop] = 0;
     947                 :     3389894 :   return bb;
     948                 :             : }
     949                 :             : 
     950                 :             : // --------------------------------------------------------------------------
     951                 :             : 
     952                 :    23775692 : ranger_cache::ranger_cache (int not_executable_flag, bool use_imm_uses)
     953                 :    23775692 :                                                 : m_gori (not_executable_flag),
     954                 :    23775692 :                                                   m_exit (use_imm_uses)
     955                 :             : {
     956                 :    23775692 :   m_workback.create (0);
     957                 :    23775692 :   m_workback.safe_grow_cleared (last_basic_block_for_fn (cfun));
     958                 :    23775692 :   m_workback.truncate (0);
     959                 :    23775692 :   m_temporal = new temporal_cache;
     960                 :             :   // If DOM info is available, spawn an oracle as well.
     961                 :    23775692 :   if (dom_info_available_p (CDI_DOMINATORS))
     962                 :    22870739 :       m_oracle = new dom_oracle ();
     963                 :             :     else
     964                 :      904953 :       m_oracle = NULL;
     965                 :             : 
     966                 :    23775692 :   unsigned x, lim = last_basic_block_for_fn (cfun);
     967                 :             :   // Calculate outgoing range info upfront.  This will fully populate the
     968                 :             :   // m_maybe_variant bitmap which will help eliminate processing of names
     969                 :             :   // which never have their ranges adjusted.
     970                 :   289221478 :   for (x = 0; x < lim ; x++)
     971                 :             :     {
     972                 :   265445786 :       basic_block bb = BASIC_BLOCK_FOR_FN (cfun, x);
     973                 :   265445786 :       if (bb)
     974                 :   248830460 :         m_gori.exports (bb);
     975                 :             :     }
     976                 :    23775692 :   m_update = new update_list ();
     977                 :    23775692 : }
     978                 :             : 
     979                 :    23775692 : ranger_cache::~ranger_cache ()
     980                 :             : {
     981                 :    23775692 :   delete m_update;
     982                 :    23775692 :   if (m_oracle)
     983                 :    22870739 :     delete m_oracle;
     984                 :    47551384 :   delete m_temporal;
     985                 :    23775692 :   m_workback.release ();
     986                 :    23775692 : }
     987                 :             : 
     988                 :             : // Dump the global caches to file F.  if GORI_DUMP is true, dump the
     989                 :             : // gori map as well.
     990                 :             : 
     991                 :             : void
     992                 :          46 : ranger_cache::dump (FILE *f)
     993                 :             : {
     994                 :          46 :   fprintf (f, "Non-varying global ranges:\n");
     995                 :          46 :   fprintf (f, "=========================:\n");
     996                 :          46 :   m_globals.dump (f);
     997                 :          46 :   fprintf (f, "\n");
     998                 :          46 : }
     999                 :             : 
    1000                 :             : // Dump the caches for basic block BB to file F.
    1001                 :             : 
    1002                 :             : void
    1003                 :         259 : ranger_cache::dump_bb (FILE *f, basic_block bb)
    1004                 :             : {
    1005                 :         259 :   m_gori.gori_map::dump (f, bb, false);
    1006                 :         259 :   m_on_entry.dump (f, bb);
    1007                 :         259 :   if (m_oracle)
    1008                 :         259 :     m_oracle->dump (f, bb);
    1009                 :         259 : }
    1010                 :             : 
    1011                 :             : // Get the global range for NAME, and return in R.  Return false if the
    1012                 :             : // global range is not set, and return the legacy global value in R.
    1013                 :             : 
    1014                 :             : bool
    1015                 :   705106952 : ranger_cache::get_global_range (vrange &r, tree name) const
    1016                 :             : {
    1017                 :   705106952 :   if (m_globals.get_range (r, name))
    1018                 :             :     return true;
    1019                 :   155071438 :   gimple_range_global (r, name);
    1020                 :   155071438 :   return false;
    1021                 :             : }
    1022                 :             : 
    1023                 :             : // Get the global range for NAME, and return in R.  Return false if the
    1024                 :             : // global range is not set, and R will contain the legacy global value.
    1025                 :             : // CURRENT_P is set to true if the value was in cache and not stale.
    1026                 :             : // Otherwise, set CURRENT_P to false and mark as it always current.
    1027                 :             : // If the global cache did not have a value, initialize it as well.
    1028                 :             : // After this call, the global cache will have a value.
    1029                 :             : 
    1030                 :             : bool
    1031                 :   289502203 : ranger_cache::get_global_range (vrange &r, tree name, bool &current_p)
    1032                 :             : {
    1033                 :   289502203 :   bool had_global = get_global_range (r, name);
    1034                 :             : 
    1035                 :             :   // If there was a global value, set current flag, otherwise set a value.
    1036                 :   289502203 :   current_p = false;
    1037                 :   289502203 :   if (had_global)
    1038                 :   375002068 :     current_p = r.singleton_p ()
    1039                 :   187501034 :                 || m_temporal->current_p (name, m_gori.depend1 (name),
    1040                 :             :                                           m_gori.depend2 (name));
    1041                 :             :   else
    1042                 :             :     {
    1043                 :             :       // If no global value has been set and value is VARYING, fold the stmt
    1044                 :             :       // using just global ranges to get a better initial value.
    1045                 :             :       // After inlining we tend to decide some things are constant, so
    1046                 :             :       // so not do this evaluation after inlining.
    1047                 :   102001169 :       if (r.varying_p () && !cfun->after_inlining)
    1048                 :             :         {
    1049                 :    20218125 :           gimple *s = SSA_NAME_DEF_STMT (name);
    1050                 :    20218125 :           if (gimple_get_lhs (s) == name)
    1051                 :             :             {
    1052                 :    18130607 :               if (!fold_range (r, s, get_global_range_query ()))
    1053                 :           0 :                 gimple_range_global (r, name);
    1054                 :             :             }
    1055                 :             :         }
    1056                 :   102001169 :       m_globals.set_range (name, r);
    1057                 :             :     }
    1058                 :             : 
    1059                 :             :   // If the existing value was not current, mark it as always current.
    1060                 :   289502203 :   if (!current_p)
    1061                 :   109355723 :     m_temporal->set_always_current (name, true);
    1062                 :   289502203 :   return had_global;
    1063                 :             : }
    1064                 :             : 
    1065                 :             : //  Set the global range of NAME to R and give it a timestamp.
    1066                 :             : 
    1067                 :             : void
    1068                 :   109355723 : ranger_cache::set_global_range (tree name, const vrange &r, bool changed)
    1069                 :             : {
    1070                 :             :   // Setting a range always clears the always_current flag.
    1071                 :   109355723 :   m_temporal->set_always_current (name, false);
    1072                 :   109355723 :   if (!changed)
    1073                 :             :     {
    1074                 :             :       // If there are dependencies, make sure this is not out of date.
    1075                 :   101514228 :       if (!m_temporal->current_p (name, m_gori.depend1 (name),
    1076                 :             :                                  m_gori.depend2 (name)))
    1077                 :    19887449 :         m_temporal->set_timestamp (name);
    1078                 :   101514228 :       return;
    1079                 :             :     }
    1080                 :     7841495 :   if (m_globals.set_range (name, r))
    1081                 :             :     {
    1082                 :             :       // If there was already a range set, propagate the new value.
    1083                 :     7841495 :       basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1084                 :     7841495 :       if (!bb)
    1085                 :          36 :         bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1086                 :             : 
    1087                 :     7841495 :       if (DEBUG_RANGE_CACHE)
    1088                 :           0 :         fprintf (dump_file, "   GLOBAL :");
    1089                 :             : 
    1090                 :     7841495 :       propagate_updated_value (name, bb);
    1091                 :             :     }
    1092                 :             :   // Constants no longer need to tracked.  Any further refinement has to be
    1093                 :             :   // undefined. Propagation works better with constants. PR 100512.
    1094                 :             :   // Pointers which resolve to non-zero also do not need
    1095                 :             :   // tracking in the cache as they will never change.  See PR 98866.
    1096                 :             :   // Timestamp must always be updated, or dependent calculations may
    1097                 :             :   // not include this latest value. PR 100774.
    1098                 :             : 
    1099                 :     7841495 :   if (r.singleton_p ()
    1100                 :     7841495 :       || (POINTER_TYPE_P (TREE_TYPE (name)) && r.nonzero_p ()))
    1101                 :     1711700 :     m_gori.set_range_invariant (name);
    1102                 :     7841495 :   m_temporal->set_timestamp (name);
    1103                 :             : }
    1104                 :             : 
    1105                 :             : //  Provide lookup for the gori-computes class to access the best known range
    1106                 :             : //  of an ssa_name in any given basic block.  Note, this does no additional
    1107                 :             : //  lookups, just accesses the data that is already known.
    1108                 :             : 
    1109                 :             : // Get the range of NAME when the def occurs in block BB.  If BB is NULL
    1110                 :             : // get the best global value available.
    1111                 :             : 
    1112                 :             : void
    1113                 :   137740333 : ranger_cache::range_of_def (vrange &r, tree name, basic_block bb)
    1114                 :             : {
    1115                 :   137740333 :   gcc_checking_assert (gimple_range_ssa_p (name));
    1116                 :   240923901 :   gcc_checking_assert (!bb || bb == gimple_bb (SSA_NAME_DEF_STMT (name)));
    1117                 :             : 
    1118                 :             :   // Pick up the best global range available.
    1119                 :   137740333 :   if (!m_globals.get_range (r, name))
    1120                 :             :     {
    1121                 :             :       // If that fails, try to calculate the range using just global values.
    1122                 :    16882028 :       gimple *s = SSA_NAME_DEF_STMT (name);
    1123                 :    16882028 :       if (gimple_get_lhs (s) == name)
    1124                 :    14490175 :         fold_range (r, s, get_global_range_query ());
    1125                 :             :       else
    1126                 :     2391853 :         gimple_range_global (r, name);
    1127                 :             :     }
    1128                 :   137740333 : }
    1129                 :             : 
    1130                 :             : // Get the range of NAME as it occurs on entry to block BB.  Use MODE for
    1131                 :             : // lookups.
    1132                 :             : 
    1133                 :             : void
    1134                 :    84536967 : ranger_cache::entry_range (vrange &r, tree name, basic_block bb,
    1135                 :             :                            enum rfd_mode mode)
    1136                 :             : {
    1137                 :    84536967 :   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1138                 :             :     {
    1139                 :           0 :       gimple_range_global (r, name);
    1140                 :           0 :       return;
    1141                 :             :     }
    1142                 :             : 
    1143                 :             :   // Look for the on-entry value of name in BB from the cache.
    1144                 :             :   // Otherwise pick up the best available global value.
    1145                 :    84536967 :   if (!m_on_entry.get_bb_range (r, name, bb))
    1146                 :    43798114 :     if (!range_from_dom (r, name, bb, mode))
    1147                 :    34556765 :       range_of_def (r, name);
    1148                 :             : }
    1149                 :             : 
    1150                 :             : // Get the range of NAME as it occurs on exit from block BB.  Use MODE for
    1151                 :             : // lookups.
    1152                 :             : 
    1153                 :             : void
    1154                 :    73510414 : ranger_cache::exit_range (vrange &r, tree name, basic_block bb,
    1155                 :             :                           enum rfd_mode mode)
    1156                 :             : {
    1157                 :    73510414 :   if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1158                 :             :     {
    1159                 :        9886 :       gimple_range_global (r, name);
    1160                 :        9886 :       return;
    1161                 :             :     }
    1162                 :             : 
    1163                 :    73500528 :   gimple *s = SSA_NAME_DEF_STMT (name);
    1164                 :    73500528 :   basic_block def_bb = gimple_bb (s);
    1165                 :    73500528 :   if (def_bb == bb)
    1166                 :    35757410 :     range_of_def (r, name, bb);
    1167                 :             :   else
    1168                 :    37743118 :     entry_range (r, name, bb, mode);
    1169                 :             : }
    1170                 :             : 
    1171                 :             : // Get the range of NAME on edge E using MODE, return the result in R.
    1172                 :             : // Always returns a range and true.
    1173                 :             : 
    1174                 :             : bool
    1175                 :    64584330 : ranger_cache::edge_range (vrange &r, edge e, tree name, enum rfd_mode mode)
    1176                 :             : {
    1177                 :    64584330 :   exit_range (r, name, e->src, mode);
    1178                 :             :   // If this is not an abnormal edge, check for inferred ranges on exit.
    1179                 :    64584330 :   if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
    1180                 :    64298394 :     m_exit.maybe_adjust_range (r, name, e->src);
    1181                 :    64584330 :   Value_Range er (TREE_TYPE (name));
    1182                 :    64584330 :   if (m_gori.outgoing_edge_range_p (er, e, name, *this))
    1183                 :    13756324 :     r.intersect (er);
    1184                 :    64584330 :   return true;
    1185                 :    64584330 : }
    1186                 :             : 
    1187                 :             : 
    1188                 :             : 
    1189                 :             : // Implement range_of_expr.
    1190                 :             : 
    1191                 :             : bool
    1192                 :   140630521 : ranger_cache::range_of_expr (vrange &r, tree name, gimple *stmt)
    1193                 :             : {
    1194                 :   140630521 :   if (!gimple_range_ssa_p (name))
    1195                 :             :     {
    1196                 :    26410514 :       get_tree_range (r, name, stmt);
    1197                 :    26410514 :       return true;
    1198                 :             :     }
    1199                 :             : 
    1200                 :   114220007 :   basic_block bb = gimple_bb (stmt);
    1201                 :   114220007 :   gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1202                 :   114220007 :   basic_block def_bb = gimple_bb (def_stmt);
    1203                 :             : 
    1204                 :   114220007 :   if (bb == def_bb)
    1205                 :    67426158 :     range_of_def (r, name, bb);
    1206                 :             :   else
    1207                 :    46793849 :     entry_range (r, name, bb, RFD_NONE);
    1208                 :             :   return true;
    1209                 :             : }
    1210                 :             : 
    1211                 :             : 
    1212                 :             : // Implement range_on_edge.  Always return the best available range using
    1213                 :             : // the current cache values.
    1214                 :             : 
    1215                 :             : bool
    1216                 :    55695822 : ranger_cache::range_on_edge (vrange &r, edge e, tree expr)
    1217                 :             : {
    1218                 :    55695822 :   if (gimple_range_ssa_p (expr))
    1219                 :    53772680 :     return edge_range (r, e, expr, RFD_NONE);
    1220                 :     1923142 :   return get_tree_range (r, expr, NULL);
    1221                 :             : }
    1222                 :             : 
    1223                 :             : // Return a static range for NAME on entry to basic block BB in R.  If
    1224                 :             : // calc is true, fill any cache entries required between BB and the
    1225                 :             : // def block for NAME.  Otherwise, return false if the cache is empty.
    1226                 :             : 
    1227                 :             : bool
    1228                 :   348824005 : ranger_cache::block_range (vrange &r, basic_block bb, tree name, bool calc)
    1229                 :             : {
    1230                 :   348824005 :   gcc_checking_assert (gimple_range_ssa_p (name));
    1231                 :             : 
    1232                 :             :   // If there are no range calculations anywhere in the IL, global range
    1233                 :             :   // applies everywhere, so don't bother caching it.
    1234                 :   348824005 :   if (!m_gori.has_edge_range_p (name))
    1235                 :             :     return false;
    1236                 :             : 
    1237                 :   211603767 :   if (calc)
    1238                 :             :     {
    1239                 :    99701353 :       gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1240                 :    99701353 :       basic_block def_bb = NULL;
    1241                 :    99701353 :       if (def_stmt)
    1242                 :    99701353 :         def_bb = gimple_bb (def_stmt);;
    1243                 :    99701353 :       if (!def_bb)
    1244                 :             :         {
    1245                 :             :           // If we get to the entry block, this better be a default def
    1246                 :             :           // or range_on_entry was called for a block not dominated by
    1247                 :             :           // the def.  
    1248                 :    16065393 :           gcc_checking_assert (SSA_NAME_IS_DEFAULT_DEF (name));
    1249                 :    16065393 :           def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1250                 :             :         }
    1251                 :             : 
    1252                 :             :       // There is no range on entry for the definition block.
    1253                 :    99701353 :       if (def_bb == bb)
    1254                 :             :         return false;
    1255                 :             : 
    1256                 :             :       // Otherwise, go figure out what is known in predecessor blocks.
    1257                 :    99370461 :       fill_block_cache (name, bb, def_bb);
    1258                 :    99370461 :       gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
    1259                 :             :     }
    1260                 :   211272875 :   return m_on_entry.get_bb_range (r, name, bb);
    1261                 :             : }
    1262                 :             : 
    1263                 :             : // If there is anything in the propagation update_list, continue
    1264                 :             : // processing NAME until the list of blocks is empty.
    1265                 :             : 
    1266                 :             : void
    1267                 :     3135011 : ranger_cache::propagate_cache (tree name)
    1268                 :             : {
    1269                 :     3135011 :   basic_block bb;
    1270                 :     3135011 :   edge_iterator ei;
    1271                 :     3135011 :   edge e;
    1272                 :     3135011 :   tree type = TREE_TYPE (name);
    1273                 :     3135011 :   Value_Range new_range (type);
    1274                 :     3135011 :   Value_Range current_range (type);
    1275                 :     3135011 :   Value_Range e_range (type);
    1276                 :             : 
    1277                 :             :   // Process each block by seeing if its calculated range on entry is
    1278                 :             :   // the same as its cached value. If there is a difference, update
    1279                 :             :   // the cache to reflect the new value, and check to see if any
    1280                 :             :   // successors have cache entries which may need to be checked for
    1281                 :             :   // updates.
    1282                 :             : 
    1283                 :     9659916 :   while (!m_update->empty_p ())
    1284                 :             :     {
    1285                 :     3389894 :       bb = m_update->pop ();
    1286                 :     3389894 :       gcc_checking_assert (m_on_entry.bb_range_p (name, bb));
    1287                 :     3389894 :       m_on_entry.get_bb_range (current_range, name, bb);
    1288                 :             : 
    1289                 :     3389894 :       if (DEBUG_RANGE_CACHE)
    1290                 :             :         {
    1291                 :           0 :           fprintf (dump_file, "FWD visiting block %d for ", bb->index);
    1292                 :           0 :           print_generic_expr (dump_file, name, TDF_SLIM);
    1293                 :           0 :           fprintf (dump_file, "  starting range : ");
    1294                 :           0 :           current_range.dump (dump_file);
    1295                 :           0 :           fprintf (dump_file, "\n");
    1296                 :             :         }
    1297                 :             : 
    1298                 :             :       // Calculate the "new" range on entry by unioning the pred edges.
    1299                 :     3389894 :       new_range.set_undefined ();
    1300                 :     6567123 :       FOR_EACH_EDGE (e, ei, bb->preds)
    1301                 :             :         {
    1302                 :     3768465 :           edge_range (e_range, e, name, RFD_READ_ONLY);
    1303                 :     3768465 :           if (DEBUG_RANGE_CACHE)
    1304                 :             :             {
    1305                 :           0 :               fprintf (dump_file, "   edge %d->%d :", e->src->index, bb->index);
    1306                 :           0 :               e_range.dump (dump_file);
    1307                 :           0 :               fprintf (dump_file, "\n");
    1308                 :             :             }
    1309                 :     3768465 :           new_range.union_ (e_range);
    1310                 :     3768465 :           if (new_range.varying_p ())
    1311                 :             :             break;
    1312                 :             :         }
    1313                 :             : 
    1314                 :             :       // If the range on entry has changed, update it.
    1315                 :     3389894 :       if (new_range != current_range)
    1316                 :             :         {
    1317                 :     1026391 :           bool ok_p = m_on_entry.set_bb_range (name, bb, new_range);
    1318                 :             :           // If the cache couldn't set the value, mark it as failed.
    1319                 :     1026391 :           if (!ok_p)
    1320                 :           0 :             m_update->propagation_failed (bb);
    1321                 :     1026391 :           if (DEBUG_RANGE_CACHE) 
    1322                 :             :             {
    1323                 :           0 :               if (!ok_p)
    1324                 :             :                 {
    1325                 :           0 :                   fprintf (dump_file, "   Cache failure to store value:");
    1326                 :           0 :                   print_generic_expr (dump_file, name, TDF_SLIM);
    1327                 :           0 :                   fprintf (dump_file, "  ");
    1328                 :             :                 }
    1329                 :             :               else
    1330                 :             :                 {
    1331                 :           0 :                   fprintf (dump_file, "      Updating range to ");
    1332                 :           0 :                   new_range.dump (dump_file);
    1333                 :             :                 }
    1334                 :           0 :               fprintf (dump_file, "\n      Updating blocks :");
    1335                 :             :             }
    1336                 :             :           // Mark each successor that has a range to re-check its range
    1337                 :     2482806 :           FOR_EACH_EDGE (e, ei, bb->succs)
    1338                 :     1456415 :             if (m_on_entry.bb_range_p (name, e->dest))
    1339                 :             :               {
    1340                 :      219071 :                 if (DEBUG_RANGE_CACHE) 
    1341                 :           0 :                   fprintf (dump_file, " bb%d",e->dest->index);
    1342                 :      219071 :                 m_update->add (e->dest);
    1343                 :             :               }
    1344                 :     1026391 :           if (DEBUG_RANGE_CACHE) 
    1345                 :           0 :             fprintf (dump_file, "\n");
    1346                 :             :         }
    1347                 :             :     }
    1348                 :     3135011 :   if (DEBUG_RANGE_CACHE)
    1349                 :             :     {
    1350                 :           0 :       fprintf (dump_file, "DONE visiting blocks for ");
    1351                 :           0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1352                 :           0 :       fprintf (dump_file, "\n");
    1353                 :             :     }
    1354                 :     3135011 :   m_update->clear_failures ();
    1355                 :     3135011 : }
    1356                 :             : 
    1357                 :             : // Check to see if an update to the value for NAME in BB has any effect
    1358                 :             : // on values already in the on-entry cache for successor blocks.
    1359                 :             : // If it does, update them.  Don't visit any blocks which don't have a cache
    1360                 :             : // entry.
    1361                 :             : 
    1362                 :             : void
    1363                 :    43364274 : ranger_cache::propagate_updated_value (tree name, basic_block bb)
    1364                 :             : {
    1365                 :    43364274 :   edge e;
    1366                 :    43364274 :   edge_iterator ei;
    1367                 :             : 
    1368                 :             :   // The update work list should be empty at this point.
    1369                 :    43364274 :   gcc_checking_assert (m_update->empty_p ());
    1370                 :    43364274 :   gcc_checking_assert (bb);
    1371                 :             : 
    1372                 :    43364274 :   if (DEBUG_RANGE_CACHE)
    1373                 :             :     {
    1374                 :           0 :       fprintf (dump_file, " UPDATE cache for ");
    1375                 :           0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1376                 :           0 :       fprintf (dump_file, " in BB %d : successors : ", bb->index);
    1377                 :             :     }
    1378                 :   126348591 :   FOR_EACH_EDGE (e, ei, bb->succs)
    1379                 :             :     {
    1380                 :             :       // Only update active cache entries.
    1381                 :    82984317 :       if (m_on_entry.bb_range_p (name, e->dest))
    1382                 :             :         {
    1383                 :     3108685 :           m_update->add (e->dest);
    1384                 :     3108685 :           if (DEBUG_RANGE_CACHE)
    1385                 :           0 :             fprintf (dump_file, " UPDATE: bb%d", e->dest->index);
    1386                 :             :         }
    1387                 :             :     }
    1388                 :    43364274 :     if (!m_update->empty_p ())
    1389                 :             :       {
    1390                 :     3077993 :         if (DEBUG_RANGE_CACHE)
    1391                 :           0 :           fprintf (dump_file, "\n");
    1392                 :     3077993 :         propagate_cache (name);
    1393                 :             :       }
    1394                 :             :     else
    1395                 :             :       {
    1396                 :    40286281 :         if (DEBUG_RANGE_CACHE)
    1397                 :           0 :           fprintf (dump_file, "  : No updates!\n");
    1398                 :             :       }
    1399                 :    43364274 : }
    1400                 :             : 
    1401                 :             : // Make sure that the range-on-entry cache for NAME is set for block BB.
    1402                 :             : // Work back through the CFG to DEF_BB ensuring the range is calculated
    1403                 :             : // on the block/edges leading back to that point.
    1404                 :             : 
    1405                 :             : void
    1406                 :    99370461 : ranger_cache::fill_block_cache (tree name, basic_block bb, basic_block def_bb)
    1407                 :             : {
    1408                 :    99370461 :   edge_iterator ei;
    1409                 :    99370461 :   edge e;
    1410                 :    99370461 :   tree type = TREE_TYPE (name);
    1411                 :    99370461 :   Value_Range block_result (type);
    1412                 :    99370461 :   Value_Range undefined (type);
    1413                 :             : 
    1414                 :             :   // At this point we shouldn't be looking at the def, entry block.
    1415                 :    99370461 :   gcc_checking_assert (bb != def_bb && bb != ENTRY_BLOCK_PTR_FOR_FN (cfun));
    1416                 :    99370461 :   gcc_checking_assert (m_workback.length () == 0);
    1417                 :             : 
    1418                 :             :   // If the block cache is set, then we've already visited this block.
    1419                 :    99370461 :   if (m_on_entry.bb_range_p (name, bb))
    1420                 :             :     return;
    1421                 :             : 
    1422                 :    38887491 :   if (DEBUG_RANGE_CACHE)
    1423                 :             :     {
    1424                 :           0 :       fprintf (dump_file, "\n");
    1425                 :           0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1426                 :           0 :       fprintf (dump_file, " : ");
    1427                 :             :     }
    1428                 :             : 
    1429                 :             :   // Check if a dominators can supply the range.
    1430                 :    38887491 :   if (range_from_dom (block_result, name, bb, RFD_FILL))
    1431                 :             :     {
    1432                 :    38830473 :       if (DEBUG_RANGE_CACHE)
    1433                 :             :         {
    1434                 :           0 :           fprintf (dump_file, "Filled from dominator! :  ");
    1435                 :           0 :           block_result.dump (dump_file);
    1436                 :           0 :           fprintf (dump_file, "\n");
    1437                 :             :         }
    1438                 :             :       // See if any equivalences can refine it.
    1439                 :             :       // PR 109462, like 108139 below, a one way equivalence introduced
    1440                 :             :       // by a PHI node can also be through the definition side.  Disallow it.
    1441                 :    38830473 :       if (m_oracle)
    1442                 :             :         {
    1443                 :    38830473 :           tree equiv_name;
    1444                 :    38830473 :           relation_kind rel;
    1445                 :    38830473 :           int prec = TYPE_PRECISION (type);
    1446                 :    46071896 :           FOR_EACH_PARTIAL_AND_FULL_EQUIV (m_oracle, bb, name, equiv_name, rel)
    1447                 :             :             {
    1448                 :     7241423 :               basic_block equiv_bb = gimple_bb (SSA_NAME_DEF_STMT (equiv_name));
    1449                 :             : 
    1450                 :             :               // Ignore partial equivs that are smaller than this object.
    1451                 :    13134018 :               if (rel != VREL_EQ && prec > pe_to_bits (rel))
    1452                 :     2840260 :                 continue;
    1453                 :             : 
    1454                 :             :               // Check if the equiv has any ranges calculated.
    1455                 :     6368381 :               if (!m_gori.has_edge_range_p (equiv_name))
    1456                 :      266286 :                 continue;
    1457                 :             : 
    1458                 :             :               // Check if the equiv definition dominates this block
    1459                 :     6102095 :               if (equiv_bb == bb ||
    1460                 :     5922985 :                   (equiv_bb && !dominated_by_p (CDI_DOMINATORS, bb, equiv_bb)))
    1461                 :     1700932 :                 continue;
    1462                 :             : 
    1463                 :     4401163 :               if (DEBUG_RANGE_CACHE)
    1464                 :             :                 {
    1465                 :           0 :                   if (rel == VREL_EQ)
    1466                 :           0 :                     fprintf (dump_file, "Checking Equivalence (");
    1467                 :             :                   else
    1468                 :           0 :                     fprintf (dump_file, "Checking Partial equiv (");
    1469                 :           0 :                   print_relation (dump_file, rel);
    1470                 :           0 :                   fprintf (dump_file, ") ");
    1471                 :           0 :                   print_generic_expr (dump_file, equiv_name, TDF_SLIM);
    1472                 :           0 :                   fprintf (dump_file, "\n");
    1473                 :             :                 }
    1474                 :     4401163 :               Value_Range equiv_range (TREE_TYPE (equiv_name));
    1475                 :     4401163 :               if (range_from_dom (equiv_range, equiv_name, bb, RFD_READ_ONLY))
    1476                 :             :                 {
    1477                 :     4401163 :                   if (rel != VREL_EQ)
    1478                 :     3235442 :                     range_cast (equiv_range, type);
    1479                 :             :                   else
    1480                 :     1165721 :                     adjust_equivalence_range (equiv_range);
    1481                 :             : 
    1482                 :     4401163 :                   if (block_result.intersect (equiv_range))
    1483                 :             :                     {
    1484                 :       87229 :                       if (DEBUG_RANGE_CACHE)
    1485                 :             :                         {
    1486                 :           0 :                           if (rel == VREL_EQ)
    1487                 :           0 :                             fprintf (dump_file, "Equivalence update! :  ");
    1488                 :             :                           else
    1489                 :           0 :                             fprintf (dump_file, "Partial equiv update! :  ");
    1490                 :           0 :                           print_generic_expr (dump_file, equiv_name, TDF_SLIM);
    1491                 :           0 :                           fprintf (dump_file, " has range  :  ");
    1492                 :           0 :                           equiv_range.dump (dump_file);
    1493                 :           0 :                           fprintf (dump_file, " refining range to :");
    1494                 :           0 :                           block_result.dump (dump_file);
    1495                 :           0 :                           fprintf (dump_file, "\n");
    1496                 :             :                         }
    1497                 :             :                     }
    1498                 :             :                 }
    1499                 :     4401163 :             }
    1500                 :             :         }
    1501                 :             : 
    1502                 :    38830473 :       m_on_entry.set_bb_range (name, bb, block_result);
    1503                 :    99313443 :       gcc_checking_assert (m_workback.length () == 0);
    1504                 :             :       return;
    1505                 :             :     }
    1506                 :             : 
    1507                 :             :   // Visit each block back to the DEF.  Initialize each one to UNDEFINED.
    1508                 :             :   // m_visited at the end will contain all the blocks that we needed to set
    1509                 :             :   // the range_on_entry cache for.
    1510                 :       57018 :   m_workback.quick_push (bb);
    1511                 :       57018 :   undefined.set_undefined ();
    1512                 :       57018 :   m_on_entry.set_bb_range (name, bb, undefined);
    1513                 :       57018 :   gcc_checking_assert (m_update->empty_p ());
    1514                 :             : 
    1515                 :      257234 :   while (m_workback.length () > 0)
    1516                 :             :     {
    1517                 :      200216 :       basic_block node = m_workback.pop ();
    1518                 :      200216 :       if (DEBUG_RANGE_CACHE)
    1519                 :             :         {
    1520                 :           0 :           fprintf (dump_file, "BACK visiting block %d for ", node->index);
    1521                 :           0 :           print_generic_expr (dump_file, name, TDF_SLIM);
    1522                 :           0 :           fprintf (dump_file, "\n");
    1523                 :             :         }
    1524                 :             : 
    1525                 :      446135 :       FOR_EACH_EDGE (e, ei, node->preds)
    1526                 :             :         {
    1527                 :      245919 :           basic_block pred = e->src;
    1528                 :      245919 :           Value_Range r (TREE_TYPE (name));
    1529                 :             : 
    1530                 :      245919 :           if (DEBUG_RANGE_CACHE)
    1531                 :           0 :             fprintf (dump_file, "  %d->%d ",e->src->index, e->dest->index);
    1532                 :             : 
    1533                 :             :           // If the pred block is the def block add this BB to update list.
    1534                 :      245919 :           if (pred == def_bb)
    1535                 :             :             {
    1536                 :       57748 :               m_update->add (node);
    1537                 :       57748 :               continue;
    1538                 :             :             }
    1539                 :             : 
    1540                 :             :           // If the pred is entry but NOT def, then it is used before
    1541                 :             :           // defined, it'll get set to [] and no need to update it.
    1542                 :      188171 :           if (pred == ENTRY_BLOCK_PTR_FOR_FN (cfun))
    1543                 :             :             {
    1544                 :           0 :               if (DEBUG_RANGE_CACHE)
    1545                 :           0 :                 fprintf (dump_file, "entry: bail.");
    1546                 :           0 :               continue;
    1547                 :             :             }
    1548                 :             : 
    1549                 :             :           // Regardless of whether we have visited pred or not, if the
    1550                 :             :           // pred has inferred ranges, revisit this block.
    1551                 :             :           // Don't search the DOM tree.
    1552                 :      188171 :           if (m_exit.has_range_p (name, pred))
    1553                 :             :             {
    1554                 :        1611 :               if (DEBUG_RANGE_CACHE)
    1555                 :           0 :                 fprintf (dump_file, "Inferred range: update ");
    1556                 :        1611 :               m_update->add (node);
    1557                 :             :             }
    1558                 :             : 
    1559                 :             :           // If the pred block already has a range, or if it can contribute
    1560                 :             :           // something new. Ie, the edge generates a range of some sort.
    1561                 :      188171 :           if (m_on_entry.get_bb_range (r, name, pred))
    1562                 :             :             {
    1563                 :       44973 :               if (DEBUG_RANGE_CACHE)
    1564                 :             :                 {
    1565                 :           0 :                   fprintf (dump_file, "has cache, ");
    1566                 :           0 :                   r.dump (dump_file);
    1567                 :           0 :                   fprintf (dump_file, ", ");
    1568                 :             :                 }
    1569                 :       44973 :               if (!r.undefined_p () || m_gori.has_edge_range_p (name, e))
    1570                 :             :                 {
    1571                 :       23760 :                   m_update->add (node);
    1572                 :       23760 :                   if (DEBUG_RANGE_CACHE)
    1573                 :           0 :                     fprintf (dump_file, "update. ");
    1574                 :             :                 }
    1575                 :       44973 :               continue;
    1576                 :             :             }
    1577                 :             : 
    1578                 :      143198 :           if (DEBUG_RANGE_CACHE)
    1579                 :           0 :             fprintf (dump_file, "pushing undefined pred block.\n");
    1580                 :             :           // If the pred hasn't been visited (has no range), add it to
    1581                 :             :           // the list.
    1582                 :      143198 :           gcc_checking_assert (!m_on_entry.bb_range_p (name, pred));
    1583                 :      143198 :           m_on_entry.set_bb_range (name, pred, undefined);
    1584                 :      143198 :           m_workback.quick_push (pred);
    1585                 :      245919 :         }
    1586                 :             :     }
    1587                 :             : 
    1588                 :       57018 :   if (DEBUG_RANGE_CACHE)
    1589                 :           0 :     fprintf (dump_file, "\n");
    1590                 :             : 
    1591                 :             :   // Now fill in the marked blocks with values.
    1592                 :       57018 :   propagate_cache (name);
    1593                 :       57018 :   if (DEBUG_RANGE_CACHE)
    1594                 :           0 :     fprintf (dump_file, "  Propagation update done.\n");
    1595                 :    99370461 : }
    1596                 :             : 
    1597                 :             : // Resolve the range of BB if the dominators range is R by calculating incoming
    1598                 :             : // edges to this block.  All lead back to the dominator so should be cheap.
    1599                 :             : // The range for BB is set and returned in R.
    1600                 :             : 
    1601                 :             : void
    1602                 :     3106861 : ranger_cache::resolve_dom (vrange &r, tree name, basic_block bb)
    1603                 :             : {
    1604                 :     3106861 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1605                 :     3106861 :   basic_block dom_bb = get_immediate_dominator (CDI_DOMINATORS, bb);
    1606                 :             : 
    1607                 :             :   // if it doesn't already have a value, store the incoming range.
    1608                 :     3106861 :   if (!m_on_entry.bb_range_p (name, dom_bb) && def_bb != dom_bb)
    1609                 :             :     {
    1610                 :             :       // If the range can't be store, don't try to accumulate
    1611                 :             :       // the range in PREV_BB due to excessive recalculations.
    1612                 :      725813 :       if (!m_on_entry.set_bb_range (name, dom_bb, r))
    1613                 :           0 :         return;
    1614                 :             :     }
    1615                 :             :   // With the dominator set, we should be able to cheaply query
    1616                 :             :   // each incoming edge now and accumulate the results.
    1617                 :     3106861 :   r.set_undefined ();
    1618                 :     3106861 :   edge e;
    1619                 :     3106861 :   edge_iterator ei;
    1620                 :     3106861 :   Value_Range er (TREE_TYPE (name));
    1621                 :    10162401 :   FOR_EACH_EDGE (e, ei, bb->preds)
    1622                 :             :     {
    1623                 :             :       // If the predecessor is dominated by this block, then there is a back
    1624                 :             :       // edge, and won't provide anything useful.  We'll actually end up with
    1625                 :             :       // VARYING as we will not resolve this node.
    1626                 :     7055540 :       if (dominated_by_p (CDI_DOMINATORS, e->src, bb))
    1627                 :       12355 :         continue;
    1628                 :     7043185 :       edge_range (er, e, name, RFD_READ_ONLY);
    1629                 :     7043185 :       r.union_ (er);
    1630                 :             :     }
    1631                 :             :   // Set the cache in PREV_BB so it is not calculated again.
    1632                 :     3106861 :   m_on_entry.set_bb_range (name, bb, r);
    1633                 :     3106861 : }
    1634                 :             : 
    1635                 :             : // Get the range of NAME from dominators of BB and return it in R.  Search the
    1636                 :             : // dominator tree based on MODE.
    1637                 :             : 
    1638                 :             : bool
    1639                 :    87086768 : ranger_cache::range_from_dom (vrange &r, tree name, basic_block start_bb,
    1640                 :             :                               enum rfd_mode mode)
    1641                 :             : {
    1642                 :    87086768 :   if (mode == RFD_NONE || !dom_info_available_p (CDI_DOMINATORS))
    1643                 :    34613783 :     return false;
    1644                 :             : 
    1645                 :             :   // Search back to the definition block or entry block.
    1646                 :    52472985 :   basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
    1647                 :    52472985 :   if (def_bb == NULL)
    1648                 :    10733389 :     def_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
    1649                 :             : 
    1650                 :    52472985 :   basic_block bb;
    1651                 :    52472985 :   basic_block prev_bb = start_bb;
    1652                 :             : 
    1653                 :             :   // Track any inferred ranges seen.
    1654                 :    52472985 :   Value_Range infer (TREE_TYPE (name));
    1655                 :    52472985 :   infer.set_varying (TREE_TYPE (name));
    1656                 :             : 
    1657                 :             :   // Range on entry to the DEF block should not be queried.
    1658                 :    52472985 :   gcc_checking_assert (start_bb != def_bb);
    1659                 :    52472985 :   unsigned start_limit = m_workback.length ();
    1660                 :             : 
    1661                 :             :   // Default value is global range.
    1662                 :    52472985 :   get_global_range (r, name);
    1663                 :             : 
    1664                 :             :   // The dominator of EXIT_BLOCK doesn't seem to be set, so at least handle
    1665                 :             :   // the common single exit cases.
    1666                 :    52541886 :   if (start_bb == EXIT_BLOCK_PTR_FOR_FN (cfun) && single_pred_p (start_bb))
    1667                 :       68773 :     bb = single_pred_edge (start_bb)->src;
    1668                 :             :   else
    1669                 :    52404212 :     bb = get_immediate_dominator (CDI_DOMINATORS, start_bb);
    1670                 :             : 
    1671                 :             :   // Search until a value is found, pushing blocks which may need calculating.
    1672                 :   260178450 :   for ( ; bb; prev_bb = bb, bb = get_immediate_dominator (CDI_DOMINATORS, bb))
    1673                 :             :     {
    1674                 :             :       // Accumulate any block exit inferred ranges.
    1675                 :   259637234 :       m_exit.maybe_adjust_range (infer, name, bb);
    1676                 :             : 
    1677                 :             :       // This block has an outgoing range.
    1678                 :   259637234 :       if (m_gori.has_edge_range_p (name, bb))
    1679                 :    30454053 :         m_workback.quick_push (prev_bb);
    1680                 :             :       else
    1681                 :             :         {
    1682                 :             :           // Normally join blocks don't carry any new range information on
    1683                 :             :           // incoming edges.  If the first incoming edge to this block does
    1684                 :             :           // generate a range, calculate the ranges if all incoming edges
    1685                 :             :           // are also dominated by the dominator.  (Avoids backedges which
    1686                 :             :           // will break the rule of moving only upward in the dominator tree).
    1687                 :             :           // If the first pred does not generate a range, then we will be
    1688                 :             :           // using the dominator range anyway, so that's all the check needed.
    1689                 :   229183181 :           if (EDGE_COUNT (prev_bb->preds) > 1
    1690                 :   229183181 :               && m_gori.has_edge_range_p (name, EDGE_PRED (prev_bb, 0)->src))
    1691                 :             :             {
    1692                 :      437606 :               edge e;
    1693                 :      437606 :               edge_iterator ei;
    1694                 :      437606 :               bool all_dom = true;
    1695                 :     1438519 :               FOR_EACH_EDGE (e, ei, prev_bb->preds)
    1696                 :     1000913 :                 if (e->src != bb
    1697                 :     1000913 :                     && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
    1698                 :             :                   {
    1699                 :             :                     all_dom = false;
    1700                 :             :                     break;
    1701                 :             :                   }
    1702                 :      437606 :               if (all_dom)
    1703                 :      437606 :                 m_workback.quick_push (prev_bb);
    1704                 :             :             }
    1705                 :             :         }
    1706                 :             : 
    1707                 :   259637234 :       if (def_bb == bb)
    1708                 :             :         break;
    1709                 :             : 
    1710                 :   225071072 :       if (m_on_entry.get_bb_range (r, name, bb))
    1711                 :             :         break;
    1712                 :             :     }
    1713                 :             : 
    1714                 :    52472985 :   if (DEBUG_RANGE_CACHE)
    1715                 :             :     {
    1716                 :           0 :       fprintf (dump_file, "CACHE: BB %d DOM query for ", start_bb->index);
    1717                 :           0 :       print_generic_expr (dump_file, name, TDF_SLIM);
    1718                 :           0 :       fprintf (dump_file, ", found ");
    1719                 :           0 :       r.dump (dump_file);
    1720                 :           0 :       if (bb)
    1721                 :           0 :         fprintf (dump_file, " at BB%d\n", bb->index);
    1722                 :             :       else
    1723                 :           0 :         fprintf (dump_file, " at function top\n");
    1724                 :             :     }
    1725                 :             : 
    1726                 :             :   // Now process any blocks wit incoming edges that nay have adjustments.
    1727                 :   166729288 :   while (m_workback.length () > start_limit)
    1728                 :             :     {
    1729                 :    30891659 :       Value_Range er (TREE_TYPE (name));
    1730                 :    30891659 :       prev_bb = m_workback.pop ();
    1731                 :    61783318 :       if (!single_pred_p (prev_bb))
    1732                 :             :         {
    1733                 :             :           // Non single pred means we need to cache a value in the dominator
    1734                 :             :           // so we can cheaply calculate incoming edges to this block, and
    1735                 :             :           // then store the resulting value.  If processing mode is not
    1736                 :             :           // RFD_FILL, then the cache cant be stored to, so don't try.
    1737                 :             :           // Otherwise this becomes a quadratic timed calculation.
    1738                 :     4292446 :           if (mode == RFD_FILL)
    1739                 :     3106861 :             resolve_dom (r, name, prev_bb);
    1740                 :     4292446 :           continue;
    1741                 :             :         }
    1742                 :             : 
    1743                 :    26599213 :       edge e = single_pred_edge (prev_bb);
    1744                 :    26599213 :       bb = e->src;
    1745                 :    26599213 :       if (m_gori.outgoing_edge_range_p (er, e, name, *this))
    1746                 :             :         {
    1747                 :    24292244 :           r.intersect (er);
    1748                 :             :           // If this is a normal edge, apply any inferred ranges.
    1749                 :    24292244 :           if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
    1750                 :    24292244 :             m_exit.maybe_adjust_range (r, name, bb);
    1751                 :             : 
    1752                 :    24292244 :           if (DEBUG_RANGE_CACHE)
    1753                 :             :             {
    1754                 :           0 :               fprintf (dump_file, "CACHE: Adjusted edge range for %d->%d : ",
    1755                 :             :                        bb->index, prev_bb->index);
    1756                 :           0 :               r.dump (dump_file);
    1757                 :           0 :               fprintf (dump_file, "\n");
    1758                 :             :             }
    1759                 :             :         }
    1760                 :    30891659 :     }
    1761                 :             : 
    1762                 :             :   // Apply non-null if appropriate.
    1763                 :    52472985 :   if (!has_abnormal_call_or_eh_pred_edge_p (start_bb))
    1764                 :    52372168 :     r.intersect (infer);
    1765                 :             : 
    1766                 :    52472985 :   if (DEBUG_RANGE_CACHE)
    1767                 :             :     {
    1768                 :           0 :       fprintf (dump_file, "CACHE: Range for DOM returns : ");
    1769                 :           0 :       r.dump (dump_file);
    1770                 :           0 :       fprintf (dump_file, "\n");
    1771                 :             :     }
    1772                 :    52472985 :   return true;
    1773                 :    52472985 : }
    1774                 :             : 
    1775                 :             : // This routine will register an inferred value in block BB, and possibly
    1776                 :             : // update the on-entry cache if appropriate.
    1777                 :             : 
    1778                 :             : void
    1779                 :    14455002 : ranger_cache::register_inferred_value (const vrange &ir, tree name,
    1780                 :             :                                        basic_block bb)
    1781                 :             : {
    1782                 :    14455002 :   Value_Range r (TREE_TYPE (name));
    1783                 :    14455002 :   if (!m_on_entry.get_bb_range (r, name, bb))
    1784                 :     8926084 :     exit_range (r, name, bb, RFD_READ_ONLY);
    1785                 :    14455002 :   if (r.intersect (ir))
    1786                 :             :     {
    1787                 :     4355098 :       m_on_entry.set_bb_range (name, bb, r);
    1788                 :             :       // If this range was invariant before, remove invariant.
    1789                 :     4355098 :       if (!m_gori.has_edge_range_p (name))
    1790                 :     3717186 :         m_gori.set_range_invariant (name, false);
    1791                 :             :     }
    1792                 :    14455002 : }
    1793                 :             : 
    1794                 :             : // This routine is used during a block walk to adjust any inferred ranges
    1795                 :             : // of operands on stmt S.
    1796                 :             : 
    1797                 :             : void
    1798                 :   205402214 : ranger_cache::apply_inferred_ranges (gimple *s)
    1799                 :             : {
    1800                 :   205402214 :   bool update = true;
    1801                 :             : 
    1802                 :   205402214 :   basic_block bb = gimple_bb (s);
    1803                 :   205402214 :   gimple_infer_range infer(s);
    1804                 :   205402214 :   if (infer.num () == 0)
    1805                 :             :     return;
    1806                 :             : 
    1807                 :             :   // Do not update the on-entry cache for block ending stmts.
    1808                 :    14215517 :   if (stmt_ends_bb_p (s))
    1809                 :             :     {
    1810                 :     1089138 :       edge_iterator ei;
    1811                 :     1089138 :       edge e;
    1812                 :     1974736 :       FOR_EACH_EDGE (e, ei, gimple_bb (s)->succs)
    1813                 :     1969451 :         if (!(e->flags & (EDGE_ABNORMAL|EDGE_EH)))
    1814                 :             :           break;
    1815                 :     1089138 :       if (e == NULL)
    1816                 :        5285 :         update = false;
    1817                 :             :     }
    1818                 :             : 
    1819                 :    28654131 :   for (unsigned x = 0; x < infer.num (); x++)
    1820                 :             :     {
    1821                 :    14438614 :       tree name = infer.name (x);
    1822                 :    14438614 :       m_exit.add_range (name, bb, infer.range (x));
    1823                 :    14438614 :       if (update)
    1824                 :    14433205 :         register_inferred_value (infer.range (x), name, bb);
    1825                 :             :     }
    1826                 :             : }
        

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