LCOV - code coverage report
Current view: top level - gcc - gimple-range.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 88.6 % 457 405
Test Date: 2025-06-21 16:26:05 Functions: 93.3 % 30 28
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Code for GIMPLE range related routines.
       2                 :             :    Copyright (C) 2019-2025 Free Software Foundation, Inc.
       3                 :             :    Contributed by Andrew MacLeod <amacleod@redhat.com>
       4                 :             :    and Aldy Hernandez <aldyh@redhat.com>.
       5                 :             : 
       6                 :             : This file is part of GCC.
       7                 :             : 
       8                 :             : GCC is free software; you can redistribute it and/or modify
       9                 :             : it under the terms of the GNU General Public License as published by
      10                 :             : the Free Software Foundation; either version 3, or (at your option)
      11                 :             : any later version.
      12                 :             : 
      13                 :             : GCC is distributed in the hope that it will be useful,
      14                 :             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16                 :             : GNU General Public License for more details.
      17                 :             : 
      18                 :             : You should have received a copy of the GNU General Public License
      19                 :             : along with GCC; see the file COPYING3.  If not see
      20                 :             : <http://www.gnu.org/licenses/>.  */
      21                 :             : 
      22                 :             : #include "config.h"
      23                 :             : #include "system.h"
      24                 :             : #include "coretypes.h"
      25                 :             : #include "backend.h"
      26                 :             : #include "tree.h"
      27                 :             : #include "gimple.h"
      28                 :             : #include "ssa.h"
      29                 :             : #include "gimple-pretty-print.h"
      30                 :             : #include "gimple-iterator.h"
      31                 :             : #include "tree-cfg.h"
      32                 :             : #include "fold-const.h"
      33                 :             : #include "tree-cfg.h"
      34                 :             : #include "cfgloop.h"
      35                 :             : #include "tree-scalar-evolution.h"
      36                 :             : #include "gimple-range.h"
      37                 :             : #include "gimple-fold.h"
      38                 :             : #include "gimple-walk.h"
      39                 :             : 
      40                 :    27271053 : gimple_ranger::gimple_ranger (bool use_imm_uses) :
      41                 :    54542106 :         non_executable_edge_flag (cfun),
      42                 :    27271053 :         m_cache (non_executable_edge_flag, use_imm_uses),
      43                 :    27271053 :         tracer (""),
      44                 :    27271053 :         current_bb (NULL)
      45                 :             : {
      46                 :             :   // Share the oracle from the cache.
      47                 :    27271053 :   share_query (m_cache);
      48                 :    27271053 :   if (dump_file && (param_ranger_debug & RANGER_DEBUG_TRACE))
      49                 :           0 :     tracer.enable_trace ();
      50                 :    27271053 :   m_stmt_list.create (0);
      51                 :    54542106 :   m_stmt_list.safe_grow (num_ssa_names);
      52                 :    27271053 :   m_stmt_list.truncate (0);
      53                 :             : 
      54                 :             :   // Ensure the not_executable flag is clear everywhere.
      55                 :    27271053 :   if (flag_checking)
      56                 :             :     {
      57                 :    27270673 :       basic_block bb;
      58                 :   325041809 :       FOR_ALL_BB_FN (bb, cfun)
      59                 :             :         {
      60                 :   297771136 :           edge_iterator ei;
      61                 :   297771136 :           edge e;
      62                 :   664149317 :           FOR_EACH_EDGE (e, ei, bb->succs)
      63                 :   366378181 :             gcc_checking_assert ((e->flags & non_executable_edge_flag) == 0);
      64                 :             :         }
      65                 :             :     }
      66                 :    27271053 : }
      67                 :             : 
      68                 :    54541530 : gimple_ranger::~gimple_ranger ()
      69                 :             : {
      70                 :    27271053 :   m_stmt_list.release ();
      71                 :    54541530 : }
      72                 :             : 
      73                 :             : // Return a range_query which accesses just the known global values.
      74                 :             : 
      75                 :             : range_query &
      76                 :     4235156 : gimple_ranger::const_query ()
      77                 :             : {
      78                 :     4235156 :   return m_cache.const_query ();
      79                 :             : }
      80                 :             : 
      81                 :             : bool
      82                 :   530562350 : gimple_ranger::range_of_expr (vrange &r, tree expr, gimple *stmt)
      83                 :             : {
      84                 :   530562350 :   unsigned idx;
      85                 :   530562350 :   if (!gimple_range_ssa_p (expr))
      86                 :    83280025 :     return get_tree_range (r, expr, stmt);
      87                 :             : 
      88                 :   447282325 :   if ((idx = tracer.header ("range_of_expr(")))
      89                 :             :     {
      90                 :           0 :       print_generic_expr (dump_file, expr, TDF_SLIM);
      91                 :           0 :       fputs (")", dump_file);
      92                 :           0 :       if (stmt)
      93                 :             :         {
      94                 :           0 :           fputs (" at stmt ", dump_file);
      95                 :           0 :           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
      96                 :             :         }
      97                 :             :       else
      98                 :           0 :         fputs ("\n", dump_file);
      99                 :             :     }
     100                 :             : 
     101                 :             :   // If there is no statement, just get the global value.
     102                 :   447282325 :   if (!stmt)
     103                 :             :     {
     104                 :    38912864 :       value_range tmp (TREE_TYPE (expr));
     105                 :             :       // If there is no global range for EXPR yet, try to evaluate it.
     106                 :             :       // This call sets R to a global range regardless.
     107                 :    38912864 :       if (!m_cache.get_global_range (r, expr))
     108                 :             :         {
     109                 :     3333514 :           gimple *s = SSA_NAME_DEF_STMT (expr);
     110                 :             :           // Calculate a range for S if it is safe to do so.
     111                 :     3333514 :           if (s && gimple_bb (s) && gimple_get_lhs (s) == expr)
     112                 :     3187178 :             return range_of_stmt (r, s);
     113                 :             :         }
     114                 :             :       // Pick up implied context information from the on-entry cache
     115                 :             :       // if current_bb is set.  Do not attempt any new calculations.
     116                 :    35725686 :       if (current_bb && m_cache.block_range (tmp, current_bb, expr, false))
     117                 :             :         {
     118                 :     1038767 :           r.intersect (tmp);
     119                 :     1038767 :           char str[80];
     120                 :     1038767 :           sprintf (str, "picked up range from bb %d\n",current_bb->index);
     121                 :     1038767 :           if (idx)
     122                 :           0 :             tracer.print (idx, str);
     123                 :             :         }
     124                 :    38912864 :     }
     125                 :             :   // For a debug stmt, pick the best value currently available, do not
     126                 :             :   // trigger new value calculations.  PR 100781.
     127                 :   408369461 :   else if (is_gimple_debug (stmt))
     128                 :    40231705 :     m_cache.range_of_expr (r, expr, stmt);
     129                 :             :   else
     130                 :             :     {
     131                 :   368137756 :       basic_block bb = gimple_bb (stmt);
     132                 :   368137756 :       gimple *def_stmt = SSA_NAME_DEF_STMT (expr);
     133                 :             : 
     134                 :             :       // If name is defined in this block, try to get an range from S.
     135                 :   368137756 :       if (def_stmt && gimple_bb (def_stmt) == bb)
     136                 :             :         {
     137                 :             :           // Declared in this block, if it has a global set, check for an
     138                 :             :           // override from a block walk, otherwise calculate it.
     139                 :   227779678 :           if (m_cache.get_global_range (r, expr))
     140                 :   194621843 :             m_cache.block_range (r, bb, expr, false);
     141                 :             :           else
     142                 :    33157835 :             range_of_stmt (r, def_stmt, expr);
     143                 :             :         }
     144                 :             :       // Otherwise OP comes from outside this block, use range on entry.
     145                 :             :       else
     146                 :   140358078 :         range_on_entry (r, bb, expr);
     147                 :             :     }
     148                 :   444095147 :   if (idx)
     149                 :           0 :     tracer.trailer (idx, "range_of_expr", true, expr, r);
     150                 :             :   return true;
     151                 :             : }
     152                 :             : 
     153                 :             : // Return the range of NAME on entry to block BB in R.
     154                 :             : 
     155                 :             : bool
     156                 :   172726075 : gimple_ranger::range_on_entry (vrange &r, basic_block bb, tree name)
     157                 :             : {
     158                 :   172726075 :   if (!gimple_range_ssa_p (name))
     159                 :           0 :     return get_tree_range (r, name, NULL, bb, NULL);
     160                 :             : 
     161                 :   172726075 :   value_range entry_range (TREE_TYPE (name));
     162                 :             : 
     163                 :   172726075 :   unsigned idx;
     164                 :   172726075 :   if ((idx = tracer.header ("range_on_entry (")))
     165                 :             :     {
     166                 :           0 :       print_generic_expr (dump_file, name, TDF_SLIM);
     167                 :           0 :       fprintf (dump_file, ") to BB %d\n", bb->index);
     168                 :             :     }
     169                 :             : 
     170                 :             :   // Start with any known range
     171                 :   172726075 :   range_of_stmt (r, SSA_NAME_DEF_STMT (name), name);
     172                 :             : 
     173                 :             :   // Now see if there is any on_entry value which may refine it.
     174                 :   172726075 :   if (m_cache.block_range (entry_range, bb, name))
     175                 :   111175818 :     r.intersect (entry_range);
     176                 :             : 
     177                 :   172726075 :   if (idx)
     178                 :           0 :     tracer.trailer (idx, "range_on_entry", true, name, r);
     179                 :   172726075 :   return true;
     180                 :   172726075 : }
     181                 :             : 
     182                 :             : // Calculate the range for NAME at the end of block BB and return it in R.
     183                 :             : // Return false if no range can be calculated.
     184                 :             : 
     185                 :             : bool
     186                 :    61120808 : gimple_ranger::range_on_exit (vrange &r, basic_block bb, tree name)
     187                 :             : {
     188                 :    61120808 :   if (!gimple_range_ssa_p (name))
     189                 :           0 :     return get_tree_range (r, name, NULL, NULL, bb);
     190                 :             : 
     191                 :    61120808 :   unsigned idx;
     192                 :    61120808 :   if ((idx = tracer.header ("range_on_exit (")))
     193                 :             :     {
     194                 :           0 :       print_generic_expr (dump_file, name, TDF_SLIM);
     195                 :           0 :       fprintf (dump_file, ") from BB %d\n", bb->index);
     196                 :             :     }
     197                 :             : 
     198                 :    61120808 :   gimple *s = SSA_NAME_DEF_STMT (name);
     199                 :    61120808 :   basic_block def_bb = gimple_bb (s);
     200                 :             :   // If this is not the definition block, get the range on the last stmt in
     201                 :             :   // the block... if there is one.
     202                 :    61120808 :   if (def_bb != bb)
     203                 :             :     {
     204                 :    27672907 :       if (bb->flags & BB_RTL)
     205                 :             :         s = NULL;
     206                 :             :       else
     207                 :    27657470 :         s = last_nondebug_stmt (bb);
     208                 :             :     }
     209                 :             :   // If there is no statement provided, get the range_on_entry for this block.
     210                 :    27657470 :   if (s)
     211                 :    48793679 :     range_of_expr (r, name, s);
     212                 :             :   else
     213                 :    12327129 :     range_on_entry (r, bb, name);
     214                 :    61120808 :   gcc_checking_assert (r.undefined_p ()
     215                 :             :                        || range_compatible_p (r.type (), TREE_TYPE (name)));
     216                 :             : 
     217                 :    61120808 :   if (idx)
     218                 :           0 :     tracer.trailer (idx, "range_on_exit", true, name, r);
     219                 :             :   return true;
     220                 :             : }
     221                 :             : 
     222                 :             : // Calculate a range for NAME on edge E and return it in R.
     223                 :             : 
     224                 :             : bool
     225                 :    73343817 : gimple_ranger::range_on_edge (vrange &r, edge e, tree name)
     226                 :             : {
     227                 :    73343817 :   value_range edge_range (TREE_TYPE (name));
     228                 :             : 
     229                 :    73343817 :   if (!r.supports_type_p (TREE_TYPE (name)))
     230                 :             :     return false;
     231                 :             : 
     232                 :             :   // Do not process values along abnormal edges.
     233                 :    73343817 :   if (e->flags & EDGE_ABNORMAL)
     234                 :           3 :     return get_tree_range (r, name, NULL);
     235                 :             : 
     236                 :    73343814 :   unsigned idx;
     237                 :    73343814 :   if ((idx = tracer.header ("range_on_edge (")))
     238                 :             :     {
     239                 :           0 :       print_generic_expr (dump_file, name, TDF_SLIM);
     240                 :           0 :       fprintf (dump_file, ") on edge %d->%d\n", e->src->index, e->dest->index);
     241                 :             :     }
     242                 :             : 
     243                 :             :   // Check to see if the edge is executable.
     244                 :    73343814 :   if ((e->flags & non_executable_edge_flag))
     245                 :             :     {
     246                 :      228962 :       r.set_undefined ();
     247                 :      228962 :       if (idx)
     248                 :           0 :         tracer.trailer (idx, "range_on_edge [Unexecutable] ", true,
     249                 :             :                         name, r);
     250                 :      228962 :       return true;
     251                 :             :     }
     252                 :             : 
     253                 :    73114852 :   bool res = true;
     254                 :    73114852 :   if (!gimple_range_ssa_p (name))
     255                 :    11994044 :     res = get_tree_range (r, name, NULL);
     256                 :             :   else
     257                 :             :     {
     258                 :    61120808 :       range_on_exit (r, e->src, name);
     259                 :             :       // If this is not an abnormal edge, check for a non-null exit .
     260                 :    61120808 :       if ((e->flags & (EDGE_EH | EDGE_ABNORMAL)) == 0)
     261                 :    60780614 :         infer_oracle ().maybe_adjust_range (r, name, e->src);
     262                 :    61120808 :       gcc_checking_assert  (r.undefined_p ()
     263                 :             :                             || range_compatible_p (r.type(), TREE_TYPE (name)));
     264                 :             : 
     265                 :             :       // Check to see if NAME is defined on edge e.
     266                 :    61120808 :       if (m_cache.range_on_edge (edge_range, e, name))
     267                 :    61120808 :         r.intersect (edge_range);
     268                 :             :     }
     269                 :             : 
     270                 :    73114852 :   if (idx)
     271                 :           0 :     tracer.trailer (idx, "range_on_edge", res, name, r);
     272                 :             :   return res;
     273                 :    73343817 : }
     274                 :             : 
     275                 :             : // fold_range wrapper for range_of_stmt to use as an internal client.
     276                 :             : 
     277                 :             : bool
     278                 :   153282917 : gimple_ranger::fold_range_internal (vrange &r, gimple *s, tree name)
     279                 :             : {
     280                 :   153282917 :   fold_using_range f;
     281                 :   153282917 :   fur_depend src (s, this);
     282                 :   153282917 :   return f.fold_stmt (r, s, src, name);
     283                 :             : }
     284                 :             : 
     285                 :             : // Calculate a range for statement S and return it in R.  If NAME is
     286                 :             : // provided it represents the SSA_NAME on the LHS of the statement.
     287                 :             : // It is only required if there is more than one lhs/output.  Check
     288                 :             : // the global cache for NAME first to see if the evaluation can be
     289                 :             : // avoided.  If a range cannot be calculated, return false and UNDEFINED.
     290                 :             : 
     291                 :             : bool
     292                 :   365770374 : gimple_ranger::range_of_stmt (vrange &r, gimple *s, tree name)
     293                 :             : {
     294                 :   365770374 :   bool res;
     295                 :   365770374 :   r.set_undefined ();
     296                 :             : 
     297                 :   365770374 :   unsigned idx;
     298                 :   365770374 :   if ((idx = tracer.header ("range_of_stmt (")))
     299                 :             :     {
     300                 :           0 :       if (name)
     301                 :           0 :         print_generic_expr (dump_file, name, TDF_SLIM);
     302                 :           0 :       fputs (") at stmt ", dump_file);
     303                 :           0 :       print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
     304                 :             :     }
     305                 :             : 
     306                 :   365770374 :   if (!name)
     307                 :    24196740 :     name = gimple_get_lhs (s);
     308                 :             : 
     309                 :             :   // If no name, simply call the base routine.
     310                 :    24196740 :   if (!name)
     311                 :             :     {
     312                 :    21009562 :       res = fold_range_internal (r, s, NULL_TREE);
     313                 :    21009562 :       if (res && is_a <gcond *> (s))
     314                 :             :         {
     315                 :             :           // Update any exports in the cache if this is a gimple cond statement.
     316                 :    20964911 :           tree exp;
     317                 :    20964911 :           basic_block bb = gimple_bb (s);
     318                 :    62916197 :           FOR_EACH_GORI_EXPORT_NAME (gori_ssa (), bb, exp)
     319                 :    41951286 :             m_cache.propagate_updated_value (exp, bb);
     320                 :             :         }
     321                 :             :     }
     322                 :   344760812 :   else if (!gimple_range_ssa_p (name))
     323                 :    36385924 :     res = get_tree_range (r, name, NULL);
     324                 :             :   else
     325                 :             :     {
     326                 :   308374888 :       bool current;
     327                 :             :       // Check if the stmt has already been processed.
     328                 :   308374888 :       if (m_cache.get_global_range (r, name, current))
     329                 :             :         {
     330                 :             :           // If it isn't stale, use this cached value.
     331                 :   206439723 :           if (current)
     332                 :             :             {
     333                 :   197461290 :               if (idx)
     334                 :           0 :                 tracer.trailer (idx, " cached", true, name, r);
     335                 :   197461290 :               return true;
     336                 :             :             }
     337                 :             :         }
     338                 :             :       else
     339                 :   101935165 :         prefill_stmt_dependencies (name);
     340                 :             : 
     341                 :             :       // Calculate a new value.
     342                 :   110913598 :       value_range tmp (TREE_TYPE (name));
     343                 :   110913598 :       fold_range_internal (tmp, s, name);
     344                 :             : 
     345                 :             :       // Combine the new value with the old value.  This is required because
     346                 :             :       // the way value propagation works, when the IL changes on the fly we
     347                 :             :       // can sometimes get different results.  See PR 97741.
     348                 :   110913598 :       bool changed = r.intersect (tmp);
     349                 :   110913598 :       m_cache.set_global_range (name, r, changed);
     350                 :   110913598 :       res = true;
     351                 :   110913598 :     }
     352                 :             : 
     353                 :   168309084 :   if (idx)
     354                 :           0 :     tracer.trailer (idx, "range_of_stmt", res, name, r);
     355                 :             :   return res;
     356                 :             : }
     357                 :             : 
     358                 :             : 
     359                 :             : // Check if NAME is a dependency that needs resolving, and push it on the
     360                 :             : // stack if so.  R is a scratch range.
     361                 :             : 
     362                 :             : inline void
     363                 :   131317173 : gimple_ranger::prefill_name (vrange &r, tree name)
     364                 :             : {
     365                 :   131317173 :   if (!gimple_range_ssa_p (name))
     366                 :             :     return;
     367                 :    96302775 :   gimple *stmt = SSA_NAME_DEF_STMT (name);
     368                 :    96302775 :   if (!gimple_range_op_handler::supported_p (stmt) && !is_a<gphi *> (stmt))
     369                 :             :     return;
     370                 :             : 
     371                 :             :   // If this op has not been processed yet, then push it on the stack
     372                 :    68419669 :   if (!m_cache.get_global_range (r, name))
     373                 :             :     {
     374                 :    21359757 :       bool current;
     375                 :             :       // Set the global cache value and mark as alway_current.
     376                 :    21359757 :       m_cache.get_global_range (r, name, current);
     377                 :    21359757 :       m_stmt_list.safe_push (name);
     378                 :             :     }
     379                 :             : }
     380                 :             : 
     381                 :             : // This routine will seed the global cache with most of the dependencies of
     382                 :             : // NAME.  This prevents excessive call depth through the normal API.
     383                 :             : 
     384                 :             : void
     385                 :   101935165 : gimple_ranger::prefill_stmt_dependencies (tree ssa)
     386                 :             : {
     387                 :   101935165 :   if (SSA_NAME_IS_DEFAULT_DEF (ssa))
     388                 :             :     return;
     389                 :             : 
     390                 :    91836590 :   unsigned idx;
     391                 :    91836590 :   gimple *stmt = SSA_NAME_DEF_STMT (ssa);
     392                 :    91836590 :   gcc_checking_assert (stmt && gimple_bb (stmt));
     393                 :             : 
     394                 :             :   // Only pre-process range-ops and phis.
     395                 :    91836590 :   if (!gimple_range_op_handler::supported_p (stmt) && !is_a<gphi *> (stmt))
     396                 :             :     return;
     397                 :             : 
     398                 :             :   // Mark where on the stack we are starting.
     399                 :    51172002 :   unsigned start = m_stmt_list.length ();
     400                 :    51172002 :   m_stmt_list.safe_push (ssa);
     401                 :             : 
     402                 :    51172002 :   idx = tracer.header ("ROS dependence fill\n");
     403                 :             : 
     404                 :             :   // Loop until back at the start point.
     405                 :   196235520 :   while (m_stmt_list.length () > start)
     406                 :             :     {
     407                 :   145063518 :       tree name = m_stmt_list.last ();
     408                 :             :       // NULL is a marker which indicates the next name in the stack has now
     409                 :             :       // been fully resolved, so we can fold it.
     410                 :   145063518 :       if (!name)
     411                 :             :         {
     412                 :             :           // Pop the NULL, then pop the name.
     413                 :    72531759 :           m_stmt_list.pop ();
     414                 :    72531759 :           name = m_stmt_list.pop ();
     415                 :             :           // Don't fold initial request, it will be calculated upon return.
     416                 :    72531759 :           if (m_stmt_list.length () > start)
     417                 :             :             {
     418                 :             :               // Fold and save the value for NAME.
     419                 :    21359757 :               stmt = SSA_NAME_DEF_STMT (name);
     420                 :    21359757 :               value_range r (TREE_TYPE (name));
     421                 :    21359757 :               fold_range_internal (r, stmt, name);
     422                 :             :               // Make sure we don't lose any current global info.
     423                 :    21359757 :               value_range tmp (TREE_TYPE (name));
     424                 :    21359757 :               m_cache.get_global_range (tmp, name);
     425                 :    21359757 :               bool changed = tmp.intersect (r);
     426                 :    21359757 :               m_cache.set_global_range (name, tmp, changed);
     427                 :    21359757 :             }
     428                 :    72531759 :           continue;
     429                 :    72531759 :         }
     430                 :             : 
     431                 :             :       // Add marker indicating previous NAME in list should be folded
     432                 :             :       // when we get to this NULL.
     433                 :    72531759 :       m_stmt_list.safe_push (NULL_TREE);
     434                 :    72531759 :       stmt = SSA_NAME_DEF_STMT (name);
     435                 :             : 
     436                 :    72531759 :       if (idx)
     437                 :             :         {
     438                 :           0 :           tracer.print (idx, "ROS dep fill (");
     439                 :           0 :           print_generic_expr (dump_file, name, TDF_SLIM);
     440                 :           0 :           fputs (") at stmt ", dump_file);
     441                 :           0 :           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
     442                 :             :         }
     443                 :             : 
     444                 :    72531759 :       gphi *phi = dyn_cast <gphi *> (stmt);
     445                 :    72531759 :       if (phi)
     446                 :             :         {
     447                 :    18784658 :           value_range r (TREE_TYPE (gimple_phi_result (phi)));
     448                 :    61017703 :           for (unsigned x = 0; x < gimple_phi_num_args (phi); x++)
     449                 :    42233045 :             prefill_name (r, gimple_phi_arg_def (phi, x));
     450                 :    18784658 :         }
     451                 :             :       else
     452                 :             :         {
     453                 :    53747101 :           gimple_range_op_handler handler (stmt);
     454                 :    53747101 :           if (handler)
     455                 :             :             {
     456                 :    53643982 :               tree op = handler.operand2 ();
     457                 :    53643982 :               if (op)
     458                 :             :                 {
     459                 :    35440974 :                   value_range r (TREE_TYPE (op));
     460                 :    35440974 :                   prefill_name (r, op);
     461                 :    35440974 :                 }
     462                 :    53643982 :               op = handler.operand1 ();
     463                 :    53643982 :               if (op)
     464                 :             :                 {
     465                 :    53643154 :                   value_range r (TREE_TYPE (op));
     466                 :    53643154 :                   prefill_name (r, op);
     467                 :    53643154 :                 }
     468                 :             :             }
     469                 :             :         }
     470                 :             :     }
     471                 :    51172002 :   if (idx)
     472                 :             :     {
     473                 :           0 :       unsupported_range r;
     474                 :           0 :       tracer.trailer (idx, "ROS ", false, ssa, r);
     475                 :           0 :     }
     476                 :             : }
     477                 :             : 
     478                 :             : 
     479                 :             : // This routine will invoke the gimple fold_stmt routine, providing context to
     480                 :             : // range_of_expr calls via an private internal API.
     481                 :             : 
     482                 :             : bool
     483                 :   238389456 : gimple_ranger::fold_stmt (gimple_stmt_iterator *gsi, tree (*valueize) (tree))
     484                 :             : {
     485                 :   238389456 :   gimple *stmt = gsi_stmt (*gsi);
     486                 :   238389456 :   current_bb = gimple_bb (stmt);
     487                 :   238389456 :   bool ret = ::fold_stmt (gsi, valueize);
     488                 :   238389456 :   current_bb = NULL;
     489                 :   238389456 :   return ret;
     490                 :             : }
     491                 :             : 
     492                 :             : // Called during dominator walks to register any inferred ranges that take
     493                 :             : // effect from this point forward.
     494                 :             : 
     495                 :             : void
     496                 :   254334193 : gimple_ranger::register_inferred_ranges (gimple *s)
     497                 :             : {
     498                 :             :   // First, export the LHS if it is a new global range.
     499                 :   254334193 :   tree lhs = gimple_get_lhs (s);
     500                 :   254334193 :   if (lhs)
     501                 :             :     {
     502                 :    90006729 :       value_range tmp (TREE_TYPE (lhs));
     503                 :    90006729 :       if (range_of_stmt (tmp, s, lhs) && !tmp.varying_p ())
     504                 :    18460587 :         set_range_info (lhs, tmp);
     505                 :    90006729 :     }
     506                 :   254334193 :   m_cache.apply_inferred_ranges (s);
     507                 :   254334193 : }
     508                 :             : 
     509                 :             : // This function will walk the statements in BB to determine if any
     510                 :             : // discovered inferred ranges in the block have any transitive effects,
     511                 :             : // and if so, register those effects in BB.
     512                 :             : 
     513                 :             : void
     514                 :    30900838 : gimple_ranger::register_transitive_inferred_ranges (basic_block bb)
     515                 :             : {
     516                 :             :   // Return if there are no inferred ranges in BB.
     517                 :    30900838 :   if (!infer_oracle ().has_range_p (bb))
     518                 :             :     return;
     519                 :             : 
     520                 :     2476115 :   if (dump_file && (dump_flags & TDF_DETAILS))
     521                 :          12 :     fprintf (dump_file, "Checking for transitive inferred ranges in BB %d\n",
     522                 :             :              bb->index);
     523                 :             : 
     524                 :    47175157 :   for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
     525                 :    42222927 :        gsi_next (&si))
     526                 :             :     {
     527                 :    42222927 :       gimple *s = gsi_stmt (si);
     528                 :    42222927 :       tree lhs = gimple_get_lhs (s);
     529                 :             :       // If the LHS already has an inferred effect, leave it be.
     530                 :    42222927 :       if (!gimple_range_ssa_p (lhs) || infer_oracle ().has_range_p (bb, lhs))
     531                 :    33044692 :         continue;
     532                 :             :       // Pick up global value.
     533                 :     9178235 :       value_range g (TREE_TYPE (lhs));
     534                 :     9178235 :       range_of_expr (g, lhs);
     535                 :             : 
     536                 :             :       // If either dependency has an inferred range, check if recalculating
     537                 :             :       // the LHS is different than the global value. If so, register it as
     538                 :             :       // an inferred range as well.
     539                 :     9178235 :       value_range r (TREE_TYPE (lhs));
     540                 :     9178235 :       r.set_undefined ();
     541                 :     9178235 :       tree name1 = gori_ssa ()->depend1 (lhs);
     542                 :     9178235 :       tree name2 = gori_ssa ()->depend2 (lhs);
     543                 :     4573562 :       if ((name1 && infer_oracle ().has_range_p (bb, name1))
     544                 :    13450007 :           || (name2 && infer_oracle ().has_range_p (bb, name2)))
     545                 :             :         {
     546                 :             :           // Check if folding S produces a different result.
     547                 :      609216 :           if (fold_range (r, s, this) && g != r)
     548                 :             :             {
     549                 :       19933 :               gimple_infer_range ir (lhs, r);
     550                 :       19933 :               infer_oracle ().add_ranges (s, ir);
     551                 :       19933 :               m_cache.register_inferred_value (r, lhs, bb);
     552                 :       19933 :             }
     553                 :             :         }
     554                 :     9178235 :     }
     555                 :             : }
     556                 :             : 
     557                 :             : // This routine will export whatever global ranges are known to GCC
     558                 :             : // SSA_RANGE_NAME_INFO and SSA_NAME_PTR_INFO fields.
     559                 :             : 
     560                 :             : void
     561                 :     2043507 : gimple_ranger::export_global_ranges ()
     562                 :             : {
     563                 :     2043507 :   if (dump_file)
     564                 :             :     {
     565                 :             :       /* Print the header only when there's something else
     566                 :             :          to print below.  */
     567                 :         296 :       fprintf (dump_file, "Exporting new  global ranges:\n");
     568                 :         296 :       fprintf (dump_file, "============================\n");
     569                 :             :     }
     570                 :   106362382 :   for (unsigned x = 1; x < num_ssa_names; x++)
     571                 :             :     {
     572                 :   104318875 :       tree name = ssa_name (x);
     573                 :   104318875 :       if (!name)
     574                 :    27710139 :         continue;
     575                 :    76608736 :       value_range r (TREE_TYPE (name));
     576                 :    76608736 :       if (name && !SSA_NAME_IN_FREE_LIST (name) && gimple_range_ssa_p (name)
     577                 :    43742629 :           && m_cache.get_global_range (r, name) && !r.varying_p())
     578                 :    12371974 :         set_range_info (name, r);
     579                 :    76608736 :     }
     580                 :     2043507 :   if (dump_file)
     581                 :         296 :     fprintf (dump_file, "========= Done =============\n");
     582                 :     2043507 : }
     583                 :             : 
     584                 :             : // Print the known table values to file F.
     585                 :             : 
     586                 :             : void
     587                 :         251 : gimple_ranger::dump_bb (FILE *f, basic_block bb)
     588                 :             : {
     589                 :         251 :   unsigned x;
     590                 :         251 :   edge_iterator ei;
     591                 :         251 :   edge e;
     592                 :         251 :   fprintf (f, "\n=========== BB %d ============\n", bb->index);
     593                 :         251 :   m_cache.dump_bb (f, bb);
     594                 :             : 
     595                 :         251 :   ::dump_bb (f, bb, 4, TDF_NONE);
     596                 :             : 
     597                 :             :   // Now find any globals defined in this block.
     598                 :       13509 :   for (x = 1; x < num_ssa_names; x++)
     599                 :             :     {
     600                 :       13258 :       tree name = ssa_name (x);
     601                 :       18531 :       if (!gimple_range_ssa_p (name) || !SSA_NAME_DEF_STMT (name))
     602                 :        7985 :         continue;
     603                 :        5273 :       value_range range (TREE_TYPE (name));
     604                 :        5273 :       if (gimple_bb (SSA_NAME_DEF_STMT (name)) == bb
     605                 :        5273 :           && m_cache.get_global_range (range, name))
     606                 :             :         {
     607                 :         284 :           if (!range.varying_p ())
     608                 :             :             {
     609                 :         149 :               print_generic_expr (f, name, TDF_SLIM);
     610                 :         149 :               fprintf (f, " : ");
     611                 :         149 :               range.dump (f);
     612                 :         149 :               fprintf (f, "\n");
     613                 :             :             }
     614                 :             : 
     615                 :             :         }
     616                 :        5273 :     }
     617                 :             : 
     618                 :             :   // And now outgoing edges, if they define anything.
     619                 :         615 :   FOR_EACH_EDGE (e, ei, bb->succs)
     620                 :             :     {
     621                 :       21972 :       for (x = 1; x < num_ssa_names; x++)
     622                 :             :         {
     623                 :       21608 :           tree name = gimple_range_ssa_p (ssa_name (x));
     624                 :       21608 :           if (!name || !gori ().has_edge_range_p (name, e))
     625                 :       20996 :             continue;
     626                 :             : 
     627                 :         612 :           value_range range (TREE_TYPE (name));
     628                 :         612 :           if (m_cache.range_on_edge (range, e, name))
     629                 :             :             {
     630                 :         612 :               gimple *s = SSA_NAME_DEF_STMT (name);
     631                 :         612 :               value_range tmp_range (TREE_TYPE (name));
     632                 :             :               // Only print the range if this is the def block, or
     633                 :             :               // the on entry cache for either end of the edge is
     634                 :             :               // set.
     635                 :         936 :               if ((s && bb == gimple_bb (s)) ||
     636                 :        1070 :                   m_cache.block_range (tmp_range, bb, name, false) ||
     637                 :         134 :                   m_cache.block_range (tmp_range, e->dest, name, false))
     638                 :             :                 {
     639                 :         478 :                   if (!range.varying_p ())
     640                 :             :                     {
     641                 :         420 :                       fprintf (f, "%d->%d ", e->src->index,
     642                 :         420 :                                e->dest->index);
     643                 :         420 :                       char c = ' ';
     644                 :         420 :                       if (e->flags & EDGE_TRUE_VALUE)
     645                 :         201 :                         fprintf (f, " (T)%c", c);
     646                 :         219 :                       else if (e->flags & EDGE_FALSE_VALUE)
     647                 :         219 :                         fprintf (f, " (F)%c", c);
     648                 :             :                       else
     649                 :           0 :                         fprintf (f, "     ");
     650                 :         420 :                       print_generic_expr (f, name, TDF_SLIM);
     651                 :         420 :                       fprintf(f, " : \t");
     652                 :         420 :                       range.dump(f);
     653                 :         420 :                       fprintf (f, "\n");
     654                 :             :                     }
     655                 :             :                 }
     656                 :         612 :             }
     657                 :         612 :         }
     658                 :             :     }
     659                 :         251 : }
     660                 :             : 
     661                 :             : // Print the known table values to file F.
     662                 :             : 
     663                 :             : void
     664                 :          45 : gimple_ranger::dump (FILE *f)
     665                 :             : {
     666                 :          45 :   basic_block bb;
     667                 :             : 
     668                 :         296 :   FOR_EACH_BB_FN (bb, cfun)
     669                 :         251 :     dump_bb (f, bb);
     670                 :             : 
     671                 :          45 :   m_cache.dump (f);
     672                 :          45 : }
     673                 :             : 
     674                 :             : void
     675                 :           0 : gimple_ranger::debug ()
     676                 :             : {
     677                 :           0 :   dump (stderr);
     678                 :           0 : }
     679                 :             : 
     680                 :             : /* Create a new ranger instance and associate it with function FUN.
     681                 :             :    Each call must be paired with a call to disable_ranger to release
     682                 :             :    resources.  */
     683                 :             : 
     684                 :             : gimple_ranger *
     685                 :    20593157 : enable_ranger (struct function *fun, bool use_imm_uses)
     686                 :             : {
     687                 :    20593157 :   gimple_ranger *r;
     688                 :             : 
     689                 :    20593157 :   gcc_checking_assert (!fun->x_range_query);
     690                 :    20593157 :   r = new gimple_ranger (use_imm_uses);
     691                 :    20593157 :   fun->x_range_query = r;
     692                 :             : 
     693                 :    20593157 :   return r;
     694                 :             : }
     695                 :             : 
     696                 :             : /* Destroy and release the ranger instance associated with function FUN
     697                 :             :    and replace it the global ranger.  */
     698                 :             : 
     699                 :             : void
     700                 :    20593157 : disable_ranger (struct function *fun)
     701                 :             : {
     702                 :    20593157 :   gcc_checking_assert (fun->x_range_query);
     703                 :    20593157 :   delete fun->x_range_query;
     704                 :    20593157 :   fun->x_range_query = NULL;
     705                 :    20593157 : }
     706                 :             : 
     707                 :             : // ---------------------------------------------------------------------------
     708                 :             : //
     709                 :             : // The DOM based ranger assumes a single DOM walk through the IL, and is
     710                 :             : // used by the fvrp_folder as a fast VRP.
     711                 :             : // During the dom walk, the current block has an ssa_lazy_cache pointer
     712                 :             : // m_bb[bb->index] which represents all the cumulative contextual ranges
     713                 :             : // active in the block.
     714                 :             : // These ranges are pure static ranges generated by branches, and must be
     715                 :             : // combined with the equivlaent global range to produce the final range.
     716                 :             : // A NULL pointer means there are no contextual ranges.
     717                 :             : 
     718                 :             : // Create a DOM based ranger for use by a DOM walk pass.
     719                 :             : 
     720                 :           6 : dom_ranger::dom_ranger () : m_global ()
     721                 :             : {
     722                 :           6 :   bitmap_obstack_initialize (&m_bitmaps);
     723                 :           6 :   m_freelist.create (0);
     724                 :           6 :   m_freelist.truncate (0);
     725                 :           6 :   m_bb.create (0);
     726                 :           6 :   m_bb.safe_grow_cleared (last_basic_block_for_fn (cfun));
     727                 :           6 :   if (dump_file && (param_ranger_debug & RANGER_DEBUG_TRACE))
     728                 :           0 :     tracer.enable_trace ();
     729                 :           6 : }
     730                 :             : 
     731                 :             : // Dispose of a DOM ranger.
     732                 :             : 
     733                 :           6 : dom_ranger::~dom_ranger ()
     734                 :             : {
     735                 :           6 :   if (dump_file && (dump_flags & TDF_DETAILS))
     736                 :             :     {
     737                 :           2 :       fprintf (dump_file, "Non-varying global ranges:\n");
     738                 :           2 :       fprintf (dump_file, "=========================:\n");
     739                 :           2 :       m_global.dump (dump_file);
     740                 :             :     }
     741                 :           6 :   m_bb.release ();
     742                 :           6 :   m_freelist.release ();
     743                 :           6 :   bitmap_obstack_release (&m_bitmaps);
     744                 :           6 : }
     745                 :             : 
     746                 :             : // Implement range of EXPR on stmt S, and return it in R.
     747                 :             : // Return false if no range can be calculated.
     748                 :             : 
     749                 :             : bool
     750                 :         514 : dom_ranger::range_of_expr (vrange &r, tree expr, gimple *s)
     751                 :             : {
     752                 :         514 :   unsigned idx;
     753                 :         514 :   if (!gimple_range_ssa_p (expr))
     754                 :         107 :     return get_tree_range (r, expr, s);
     755                 :             : 
     756                 :         407 :   if ((idx = tracer.header ("range_of_expr ")))
     757                 :             :     {
     758                 :           0 :       print_generic_expr (dump_file, expr, TDF_SLIM);
     759                 :           0 :       if (s)
     760                 :             :         {
     761                 :           0 :           fprintf (dump_file, " at ");
     762                 :           0 :           print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
     763                 :             :         }
     764                 :             :       else
     765                 :           0 :           fprintf (dump_file, "\n");
     766                 :             :     }
     767                 :             : 
     768                 :             :   // If there is a statement, return the range in that statements block.
     769                 :         407 :   if (s)
     770                 :         396 :     range_in_bb (r, gimple_bb (s), expr);
     771                 :             :   else
     772                 :          11 :     m_global.range_of_expr (r, expr, s);
     773                 :             : 
     774                 :         407 :   if (idx)
     775                 :           0 :     tracer.trailer (idx, " ", true, expr, r);
     776                 :             :   return true;
     777                 :             : }
     778                 :             : 
     779                 :             : // Return the range of EXPR on edge E in R.
     780                 :             : // Return false if no range can be calculated.
     781                 :             : 
     782                 :             : bool
     783                 :          38 : dom_ranger::range_on_edge (vrange &r, edge e, tree expr)
     784                 :             : {
     785                 :          38 :   if (!gimple_range_ssa_p (expr))
     786                 :           9 :     return get_tree_range (r, expr, NULL);
     787                 :             : 
     788                 :          29 :   basic_block bb = e->src;
     789                 :          29 :   unsigned idx;
     790                 :          29 :   if ((idx = tracer.header ("range_on_edge ")))
     791                 :             :     {
     792                 :           0 :       fprintf (dump_file, "%d->%d for ",e->src->index, e->dest->index);
     793                 :           0 :       print_generic_expr (dump_file, expr, TDF_SLIM);
     794                 :           0 :       fputc ('\n',dump_file);
     795                 :             :     }
     796                 :             : 
     797                 :          29 :   range_in_bb (r, bb, expr);
     798                 :          29 :   value_range vr(TREE_TYPE (expr));
     799                 :          29 :   if (gori_name_on_edge (vr, expr, e, this))
     800                 :           4 :     r.intersect (vr);
     801                 :             : 
     802                 :          29 :   if (idx)
     803                 :           0 :     tracer.trailer (idx, " ", true, expr, r);
     804                 :          29 :   return true;
     805                 :          29 : }
     806                 :             : 
     807                 :             : // Return the range of NAME as it exists at the end of block BB in R.
     808                 :             : 
     809                 :             : void
     810                 :         425 : dom_ranger::range_in_bb (vrange &r, basic_block bb, tree name)
     811                 :             : {
     812                 :             :   // Start with the global value.
     813                 :         425 :   m_global.range_of_expr (r, name);
     814                 :             : 
     815                 :             :   // If there is a contextual range, intersect it with the global range
     816                 :         425 :   ssa_lazy_cache *context = m_bb[bb->index];
     817                 :         425 :   if (context && context->has_range (name))
     818                 :             :     {
     819                 :          27 :       value_range cr (TREE_TYPE (name));
     820                 :          27 :       context->get_range (cr, name);
     821                 :          27 :       r.intersect (cr);
     822                 :          27 :     }
     823                 :         425 : }
     824                 :             : 
     825                 :             : // Calculate the range of NAME, as the def of stmt S and return it in R.
     826                 :             : // Return FALSE if no range can be calculated.
     827                 :             : // Also set the global range for NAME as this should only be called within
     828                 :             : // the def block during a DOM walk.
     829                 :             : 
     830                 :             : bool
     831                 :         267 : dom_ranger::range_of_stmt (vrange &r, gimple *s, tree name)
     832                 :             : {
     833                 :         267 :   unsigned idx;
     834                 :         267 :   bool ret;
     835                 :         267 :   if (!name)
     836                 :         142 :     name = gimple_get_lhs (s);
     837                 :             : 
     838                 :         267 :   if (name && !gimple_range_ssa_p (name))
     839                 :           3 :     return get_tree_range (r, name, NULL);
     840                 :             : 
     841                 :         264 :   if ((idx = tracer.header ("range_of_stmt ")))
     842                 :           0 :     print_gimple_stmt (dump_file, s, 0, TDF_SLIM);
     843                 :             : 
     844                 :             :   // Its already been calculated.
     845                 :         264 :   if (name && m_global.has_range (name))
     846                 :             :     {
     847                 :         115 :       ret = m_global.range_of_expr (r, name, s);
     848                 :         115 :       if (idx)
     849                 :           0 :         tracer.trailer (idx, " Already had value ", ret, name, r);
     850                 :         115 :       return ret;
     851                 :             :     }
     852                 :             : 
     853                 :             :   // Fold using a fur_depend object so that relations are registered.
     854                 :         149 :   fold_using_range f;
     855                 :         149 :   fur_depend src (s, this);
     856                 :         149 :   ret = f.fold_stmt (r, s, src, name);
     857                 :             : 
     858                 :             :   // If there is a new calculated range and it is not varying, set
     859                 :             :   // a global range.
     860                 :         149 :   if (ret && name && m_global.merge_range (name, r) && !r.varying_p ())
     861                 :          68 :     set_range_info (name, r);
     862                 :             : 
     863                 :         149 :   if (idx)
     864                 :           0 :     tracer.trailer (idx, " ", ret, name, r);
     865                 :             :   return ret;
     866                 :             : }
     867                 :             : 
     868                 :             : // Preprocess block BB.  If there is a single predecessor, start with any
     869                 :             : // contextual ranges on the incoming edge, otherwise the initial list
     870                 :             : // of ranges i empty for this block.  Then Merge in any contextual ranges
     871                 :             : // from the dominator block.  Tihs will become the contextual ranges
     872                 :             : // that apply to this block.
     873                 :             : 
     874                 :             : void
     875                 :          27 : dom_ranger::pre_bb (basic_block bb)
     876                 :             : {
     877                 :          27 :   if (dump_file && (dump_flags & TDF_DETAILS))
     878                 :          17 :     fprintf (dump_file, "#FVRP entering BB %d\n", bb->index);
     879                 :             : 
     880                 :          27 :   m_bb[bb->index] = NULL;
     881                 :          27 :   basic_block dom_bb  = get_immediate_dominator (CDI_DOMINATORS, bb);
     882                 :             : 
     883                 :          27 :   ssa_lazy_cache *e_cache;
     884                 :          27 :   if (!m_freelist.is_empty ())
     885                 :          17 :     e_cache = m_freelist.pop ();
     886                 :             :   else
     887                 :          10 :     e_cache = new ssa_lazy_cache (&m_bitmaps);
     888                 :          27 :   gcc_checking_assert (e_cache->empty_p ());
     889                 :             : 
     890                 :             :   // If there is a single pred, check if there are any ranges on
     891                 :             :   // the edge and start with those.
     892                 :          27 :   if (single_pred_p (bb))
     893                 :             :     {
     894                 :          16 :       gori_on_edge (*e_cache, EDGE_PRED (bb, 0), this);
     895                 :          16 :       if (!e_cache->empty_p () && dump_file && (dump_flags & TDF_DETAILS))
     896                 :             :         {
     897                 :          14 :           fprintf (dump_file, "\nEdge ranges BB %d->%d\n",
     898                 :           7 :                    EDGE_PRED (bb, 0)->src->index, bb->index);
     899                 :           7 :           e_cache->dump(dump_file);
     900                 :             :         }
     901                 :             :     }
     902                 :             :   // If the dominator had any ranges registered, integrate those.
     903                 :          27 :   if (dom_bb && m_bb [dom_bb->index])
     904                 :           9 :     e_cache->merge (*(m_bb[dom_bb->index]));
     905                 :             : 
     906                 :             :   // If there are no ranges, this block has no contextual ranges.
     907                 :          27 :   if (e_cache->empty_p ())
     908                 :          14 :     m_freelist.safe_push (e_cache);
     909                 :             :   else
     910                 :          13 :     m_bb[bb->index] = e_cache;
     911                 :             : 
     912                 :          27 :   if (dump_file && (dump_flags & TDF_DETAILS))
     913                 :             :     {
     914                 :          17 :       if (m_bb[bb->index])
     915                 :             :         {
     916                 :          11 :           fprintf (dump_file, "all contextual ranges active:\n");
     917                 :          11 :           m_bb[bb->index]->dump (dump_file);
     918                 :             :         }
     919                 :             :       else
     920                 :           6 :         fprintf (dump_file, " NO contextual ranges active:\n");
     921                 :             :     }
     922                 :          27 : }
     923                 :             : 
     924                 :             : // Perform any post block processing.
     925                 :             : 
     926                 :             : void
     927                 :          27 : dom_ranger::post_bb (basic_block bb)
     928                 :             : {
     929                 :          27 :   if (dump_file && (dump_flags & TDF_DETAILS))
     930                 :          17 :     fprintf (dump_file, "#FVRP POST BB %d\n", bb->index);
     931                 :             :   // If there were contextual ranges, clear them and put the
     932                 :             :   // object on the freelist.
     933                 :          27 :   if (m_bb[bb->index])
     934                 :             :     {
     935                 :          13 :       m_bb[bb->index]->clear ();
     936                 :          13 :       m_freelist.safe_push (m_bb[bb->index]);
     937                 :          13 :       m_bb[bb->index] = NULL;
     938                 :             :     }
     939                 :          27 : }
        

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.