LCOV - code coverage report
Current view: top level - gcc - gimple-range-gori.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 83.6 % 768 642
Test Date: 2026-12-05 16:10:02 Functions: 90.6 % 53 48
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Gimple range GORI functions.
       2              :    Copyright (C) 2017-2026 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-range.h"
      31              : 
      32              : // Return TRUE if GS is a logical && or || expression.
      33              : 
      34              : static inline bool
      35     71193672 : is_gimple_logical_p (const gimple *gs)
      36              : {
      37              :   // Look for boolean and/or condition.
      38     71193672 :   auto stmt = dyn_cast<const gassign *> (gs);
      39     22366674 :   if (!stmt)
      40              :           return false;
      41              : 
      42     22366674 :     switch (gimple_expr_code (stmt))
      43              :       {
      44              :         case TRUTH_AND_EXPR:
      45              :         case TRUTH_OR_EXPR:
      46              :           return true;
      47              : 
      48      6731687 :         case BIT_AND_EXPR:
      49      6731687 :         case BIT_IOR_EXPR:
      50              :           // Bitwise operations on single bits are logical too.
      51      6731687 :           if (types_compatible_p (TREE_TYPE (gimple_assign_rhs1 (stmt)),
      52              :                                   boolean_type_node))
      53              :             return true;
      54              :           break;
      55              : 
      56              :         default:
      57              :           break;
      58              :       }
      59              :   return false;
      60              : }
      61              : 
      62              : /* RANGE_DEF_CHAIN is used to determine which SSA names in a block can
      63              :    have range information calculated for them, and what the
      64              :    dependencies on each other are.
      65              : 
      66              :    Information for a basic block is calculated once and stored.  It is
      67              :    only calculated the first time a query is made, so if no queries
      68              :    are made, there is little overhead.
      69              : 
      70              :    The def_chain bitmap is indexed by SSA_NAME_VERSION.  Bits are set
      71              :    within this bitmap to indicate SSA names that are defined in the
      72              :    SAME block and used to calculate this SSA name.
      73              : 
      74              : 
      75              :     <bb 2> :
      76              :       _1 = x_4(D) + -2;
      77              :       _2 = _1 * 4;
      78              :       j_7 = foo ();
      79              :       q_5 = _2 + 3;
      80              :       if (q_5 <= 13)
      81              : 
      82              :     _1  : x_4(D)
      83              :     _2  : 1  x_4(D)
      84              :     q_5  : _1  _2  x_4(D)
      85              : 
      86              :     This dump indicates the bits set in the def_chain vector.
      87              :     as well as demonstrates the def_chain bits for the related ssa_names.
      88              : 
      89              :     Checking the chain for _2 indicates that _1 and x_4 are used in
      90              :     its evaluation.
      91              : 
      92              :     Def chains also only include statements which are valid gimple
      93              :     so a def chain will only span statements for which the range
      94              :     engine implements operations for.  */
      95              : 
      96              : 
      97              : // Construct a range_def_chain.
      98              : 
      99     29776596 : range_def_chain::range_def_chain ()
     100              : {
     101     29776596 :   bitmap_obstack_initialize (&m_bitmaps);
     102     29776596 :   m_def_chain.create (0);
     103     59553192 :   m_def_chain.safe_grow_cleared (num_ssa_names);
     104     29776596 :   m_logical_depth = 0;
     105     29776596 : }
     106              : 
     107              : // Destruct a range_def_chain.
     108              : 
     109     29776596 : range_def_chain::~range_def_chain ()
     110              : {
     111     29776596 :   m_def_chain.release ();
     112     29776596 :   bitmap_obstack_release (&m_bitmaps);
     113     29776596 : }
     114              : 
     115              : // Return true if NAME is in the def chain of DEF.  If BB is provided,
     116              : // only return true if the defining statement of DEF is in BB.
     117              : 
     118              : bool
     119    198328154 : range_def_chain::in_chain_p (tree name, tree def)
     120              : {
     121    198328154 :   gcc_checking_assert (gimple_range_ssa_p (def));
     122    198328154 :   gcc_checking_assert (gimple_range_ssa_p (name));
     123              : 
     124              :   // Get the definition chain for DEF.
     125    198328154 :   bitmap chain = get_def_chain (def);
     126              : 
     127    198328154 :   if (chain == NULL)
     128              :     return false;
     129    145295492 :   return bitmap_bit_p (chain, SSA_NAME_VERSION (name));
     130              : }
     131              : 
     132              : // Add either IMP or the import list B to the import set of DATA.
     133              : 
     134              : void
     135    347038515 : range_def_chain::set_import (struct rdc &data, tree imp, bitmap b)
     136              : {
     137              :   // If there are no imports, just return
     138    347038515 :   if (imp == NULL_TREE && !b)
     139              :     return;
     140    346867257 :   if (!data.m_import)
     141    157312330 :     data.m_import = BITMAP_ALLOC (&m_bitmaps);
     142    346867257 :   if (imp != NULL_TREE)
     143    301451469 :     bitmap_set_bit (data.m_import, SSA_NAME_VERSION (imp));
     144              :   else
     145     45415788 :     bitmap_ior_into (data.m_import, b);
     146              : }
     147              : 
     148              : // Return the import list for NAME.
     149              : 
     150              : bitmap
     151    184736765 : range_def_chain::get_imports (tree name)
     152              : {
     153    184736765 :   if (!has_def_chain (name))
     154    108287465 :     get_def_chain (name);
     155    184736765 :   bitmap i = m_def_chain[SSA_NAME_VERSION (name)].m_import;
     156    184736765 :   return i;
     157              : }
     158              : 
     159              : // Return true if IMPORT is an import to NAMEs def chain.
     160              : 
     161              : bool
     162      7202152 : range_def_chain::chain_import_p (tree name, tree import)
     163              : {
     164      7202152 :   bitmap b = get_imports (name);
     165      7202152 :   if (b)
     166      7193628 :     return bitmap_bit_p (b, SSA_NAME_VERSION (import));
     167              :   return false;
     168              : }
     169              : 
     170              : // Build def_chains for NAME if it is in BB.  Copy the def chain into RESULT.
     171              : 
     172              : void
     173    263213658 : range_def_chain::register_dependency (tree name, tree dep, basic_block bb)
     174              : {
     175    263213658 :   if (!gimple_range_ssa_p (dep))
     176              :     return;
     177              : 
     178    223924168 :   unsigned v = SSA_NAME_VERSION (name);
     179    223924168 :   if (v >= m_def_chain.length ())
     180        14384 :     m_def_chain.safe_grow_cleared (num_ssa_names + 1);
     181    223924168 :   struct rdc &src = m_def_chain[v];
     182    223924168 :   gimple *def_stmt = SSA_NAME_DEF_STMT (dep);
     183    223924168 :   unsigned dep_v = SSA_NAME_VERSION (dep);
     184    223924168 :   bitmap b;
     185              : 
     186              :   // Set the direct dependency cache entries.
     187    223924168 :   if (!src.ssa1)
     188    123139523 :     src.ssa1 = SSA_NAME_VERSION (dep);
     189    100784645 :   else if (!src.ssa2 && src.ssa1 != SSA_NAME_VERSION (dep))
     190     36034910 :     src.ssa2 = SSA_NAME_VERSION (dep);
     191              : 
     192              :   // Don't calculate imports or export/dep chains if BB is not provided.
     193              :   // This is usually the case for when the temporal cache wants the direct
     194              :   // dependencies of a stmt.
     195    223924168 :   if (!bb)
     196              :     return;
     197              : 
     198     77337958 :   if (!src.bm)
     199     62001757 :     src.bm = BITMAP_ALLOC (&m_bitmaps);
     200              : 
     201              :   // Add this operand into the result.
     202     77337958 :   bitmap_set_bit (src.bm, dep_v);
     203              : 
     204     77337958 :   if (gimple_bb (def_stmt) == bb && !is_a<gphi *>(def_stmt))
     205              :     {
     206              :       // Get the def chain for the operand.
     207     45587046 :       b = get_def_chain (dep);
     208              :       // If there was one, copy it into result.  Access def_chain directly
     209              :       // as the get_def_chain request above could reallocate the vector.
     210     45587046 :       if (b)
     211     27461837 :         bitmap_ior_into (m_def_chain[v].bm, b);
     212              :       // And copy the import list.
     213     45587046 :       set_import (m_def_chain[v], NULL_TREE, get_imports (dep));
     214              :     }
     215              :   else
     216              :     // Originated outside the block, so it is an import.
     217     31750912 :     set_import (src, dep, NULL);
     218              : }
     219              : 
     220              : bool
     221            0 : range_def_chain::def_chain_in_bitmap_p (tree name, bitmap b)
     222              : {
     223            0 :   bitmap a = get_def_chain (name);
     224            0 :   if (a && b)
     225            0 :     return bitmap_intersect_p (a, b);
     226              :   return false;
     227              : }
     228              : 
     229              : void
     230    131947435 : range_def_chain::add_def_chain_to_bitmap (bitmap b, tree name)
     231              : {
     232    131947435 :   bitmap r = get_def_chain (name);
     233    131947435 :   if (r)
     234     41784706 :     bitmap_ior_into (b, r);
     235    131947435 : }
     236              : 
     237              : 
     238              : // Return TRUE if NAME has been processed for a def_chain.
     239              : 
     240              : inline bool
     241    669236485 : range_def_chain::has_def_chain (tree name)
     242              : {
     243              :   // Ensure there is an entry in the internal vector.
     244    669236485 :   unsigned v = SSA_NAME_VERSION (name);
     245    669236485 :   if (v >= m_def_chain.length ())
     246          628 :     m_def_chain.safe_grow_cleared (num_ssa_names + 1);
     247    669236485 :   return (m_def_chain[v].ssa1 != 0);
     248              : }
     249              : 
     250              : 
     251              : 
     252              : // Calculate the def chain for NAME and all of its dependent
     253              : // operands. Only using names in the same BB.  Return the bitmap of
     254              : // all names in the m_def_chain.  This only works for supported range
     255              : // statements.
     256              : 
     257              : bitmap
     258    484499463 : range_def_chain::get_def_chain (tree name)
     259              : {
     260    484499463 :   tree ssa[3];
     261    484499463 :   unsigned v = SSA_NAME_VERSION (name);
     262              : 
     263              :   // If it has already been processed, just return the cached value.
     264    484499463 :   if (has_def_chain (name) && m_def_chain[v].bm)
     265              :     return m_def_chain[v].bm;
     266              : 
     267              :   // No definition chain for default defs.
     268    331884437 :   if (SSA_NAME_IS_DEFAULT_DEF (name))
     269              :     {
     270              :       // A Default def is always an import.
     271     16983350 :       set_import (m_def_chain[v], name, NULL);
     272     16983350 :       return NULL;
     273              :     }
     274              : 
     275    314901087 :   gimple *stmt = SSA_NAME_DEF_STMT (name);
     276    314901087 :   unsigned count = gimple_range_ssa_names (ssa, 3, stmt);
     277    314901087 :   if (count == 0)
     278              :     {
     279              :       // Stmts not understood or with no operands are always imports.
     280    252717207 :       set_import (m_def_chain[v], name, NULL);
     281    252717207 :       return NULL;
     282              :     }
     283              : 
     284              :   // Terminate the def chains if we see too many cascading stmts.
     285     62183880 :   if (m_logical_depth == param_ranger_logical_depth)
     286              :     return NULL;
     287              : 
     288              :   // Increase the depth if we have a pair of ssa-names.
     289     62001759 :   if (count > 1)
     290     15305186 :     m_logical_depth++;
     291              : 
     292    139339719 :   for (unsigned x = 0; x < count; x++)
     293     77337960 :     register_dependency (name, ssa[x], gimple_bb (stmt));
     294              : 
     295     62001759 :   if (count > 1)
     296     15305186 :     m_logical_depth--;
     297              : 
     298     62001759 :   return m_def_chain[v].bm;
     299              : }
     300              : 
     301              : // Clear def chain info for NAME.
     302              : 
     303              : void
     304       270818 : range_def_chain::clear (tree name)
     305              : {
     306       270818 :   unsigned v = SSA_NAME_VERSION (name);
     307       270818 :   if (v >= m_def_chain.length ())
     308              :     return;
     309              : 
     310       208753 :   m_def_chain[v].ssa1 = 0;
     311       208753 :   m_def_chain[v].ssa2 = 0;
     312       208753 :   m_def_chain[v].bm = NULL;
     313       208753 :   m_def_chain[v].m_import = NULL;
     314              : }
     315              : 
     316              : // Dump what we know for basic block BB to file F.
     317              : 
     318              : void
     319          119 : range_def_chain::dump (FILE *f, basic_block bb, const char *prefix)
     320              : {
     321          119 :   unsigned x, y;
     322          119 :   bitmap_iterator bi;
     323              : 
     324              :   // Dump the def chain for each SSA_NAME defined in BB.
     325         8220 :   for (x = 1; x < num_ssa_names; x++)
     326              :     {
     327         8101 :       tree name = ssa_name (x);
     328         8101 :       if (!name)
     329         3578 :         continue;
     330         4523 :       gimple *stmt = SSA_NAME_DEF_STMT (name);
     331         4523 :       if (!stmt || (bb && gimple_bb (stmt) != bb))
     332         4266 :         continue;
     333          257 :       bitmap chain = (has_def_chain (name) ? get_def_chain (name) : NULL);
     334          151 :       if (chain && !bitmap_empty_p (chain))
     335              :         {
     336          132 :           fprintf (f, prefix);
     337          132 :           print_generic_expr (f, name, TDF_SLIM);
     338          132 :           fprintf (f, " : ");
     339              : 
     340          132 :           bitmap imports = get_imports (name);
     341          465 :           EXECUTE_IF_SET_IN_BITMAP (chain, 0, y, bi)
     342              :             {
     343          333 :               print_generic_expr (f, ssa_name (y), TDF_SLIM);
     344          333 :               if (imports && bitmap_bit_p (imports, y))
     345          164 :                 fprintf (f, "(I)");
     346          333 :               fprintf (f, "  ");
     347              :             }
     348          132 :           fprintf (f, "\n");
     349              :         }
     350              :     }
     351          119 : }
     352              : 
     353              : 
     354              : // -------------------------------------------------------------------
     355              : 
     356              : /* GORI_MAP is used to accumulate what SSA names in a block can
     357              :    generate range information, and provides tools for the block ranger
     358              :    to enable it to efficiently calculate these ranges.
     359              : 
     360              :    GORI stands for "Generates Outgoing Range Information."
     361              : 
     362              :    It utilizes the range_def_chain class to construct def_chains.
     363              :    Information for a basic block is calculated once and stored.  It is
     364              :    only calculated the first time a query is made.  If no queries are
     365              :    made, there is little overhead.
     366              : 
     367              :    one bitmap is maintained for each basic block:
     368              :    m_outgoing  : a set bit indicates a range can be generated for a name.
     369              : 
     370              :    Generally speaking, the m_outgoing vector is the union of the
     371              :    entire def_chain of all SSA names used in the last statement of the
     372              :    block which generate ranges.  */
     373              : 
     374              : 
     375              : // Initialize a gori-map structure.
     376              : 
     377     29776596 : gori_map::gori_map ()
     378              : {
     379     29776596 :   m_outgoing.create (0);
     380     29776596 :   m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
     381     29776596 :   m_incoming.create (0);
     382     29776596 :   m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
     383     29776596 :   m_maybe_variant = BITMAP_ALLOC (&m_bitmaps);
     384     29776596 : }
     385              : 
     386              : // Free any memory the GORI map allocated.
     387              : 
     388     29776596 : gori_map::~gori_map ()
     389              : {
     390     29776596 :   m_incoming.release ();
     391     29776596 :   m_outgoing.release ();
     392     29776596 : }
     393              : 
     394              : // Return the bitmap vector of all export from BB.  Calculate if necessary.
     395              : 
     396              : bitmap
     397   1636742065 : gori_map::exports (basic_block bb)
     398              : {
     399   3273484130 :   if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
     400    340537150 :     calculate_gori (bb);
     401   1636742065 :   return m_outgoing[bb->index];
     402              : }
     403              : 
     404              : // Return the bitmap vector of all exports AND their dependencies from BB
     405              : // in TMPBIT.  Calculate if necessary.  Return TMPBIT.
     406              : 
     407              : bitmap
     408       230461 : gori_map::exports_and_deps (basic_block bb, bitmap tmpbit)
     409              : {
     410       460922 :   if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
     411            0 :     calculate_gori (bb);
     412       230461 :   bitmap_copy (tmpbit, m_outgoing[bb->index]);
     413       230461 :   if (!bitmap_empty_p (tmpbit))
     414              :     {
     415       230461 :       tree name;
     416       579673 :       FOR_EACH_GORI_EXPORT_NAME (this, bb, name)
     417              :         {
     418       349212 :           bitmap dep = get_def_chain (name);
     419       349212 :           if (dep)
     420        74616 :             bitmap_ior_into (tmpbit, dep);
     421              :         }
     422              :     }
     423       230461 :   return tmpbit;
     424              : }
     425              : 
     426              : // Return the bitmap vector of all imports to BB.  Calculate if necessary.
     427              : 
     428              : bitmap
     429      9622844 : gori_map::imports (basic_block bb)
     430              : {
     431     19245688 :   if (bb->index >= (signed int)m_outgoing.length () || !m_outgoing[bb->index])
     432            0 :     calculate_gori (bb);
     433      9622844 :   return m_incoming[bb->index];
     434              : }
     435              : 
     436              : // Return true if NAME is can have ranges generated for it from basic
     437              : // block BB.
     438              : 
     439              : bool
     440   2035359377 : gori_map::is_export_p (tree name, basic_block bb)
     441              : {
     442              :   // If no BB is specified, test if it is exported anywhere in the IL.
     443   2035359377 :   if (!bb)
     444    884742559 :     return bitmap_bit_p (m_maybe_variant, SSA_NAME_VERSION (name));
     445   1150616818 :   return bitmap_bit_p (exports (bb), SSA_NAME_VERSION (name));
     446              : }
     447              : 
     448              : // Set or clear the m_maybe_variant bit to determine if ranges will be tracked
     449              : // for NAME.  A clear bit means they will NOT be tracked.
     450              : 
     451              : void
     452      4988850 : gori_map::set_range_invariant (tree name, bool invariant)
     453              : {
     454      4988850 :   if (invariant)
     455      1353713 :     bitmap_clear_bit (m_maybe_variant, SSA_NAME_VERSION (name));
     456              :   else
     457      3635137 :     bitmap_set_bit (m_maybe_variant, SSA_NAME_VERSION (name));
     458      4988850 : }
     459              : 
     460              : // Return true if NAME is an import to block BB.
     461              : 
     462              : bool
     463            0 : gori_map::is_import_p (tree name, basic_block bb)
     464              : {
     465              :   // If no BB is specified, test if it is exported anywhere in the IL.
     466            0 :   return bitmap_bit_p (imports (bb), SSA_NAME_VERSION (name));
     467              : }
     468              : 
     469              : // If NAME is non-NULL and defined in block BB, calculate the def
     470              : // chain and add it to m_outgoing.
     471              : 
     472              : void
     473    214122137 : gori_map::maybe_add_gori (tree name, basic_block bb)
     474              : {
     475    214122137 :   if (name)
     476              :     {
     477              :       // Check if there is a def chain, regardless of the block.
     478    131947435 :       add_def_chain_to_bitmap (m_outgoing[bb->index], name);
     479              :       // Check for any imports.
     480    131947435 :       bitmap imp = get_imports (name);
     481              :       // If there were imports, add them so we can recompute
     482    131947435 :       if (imp)
     483    131946137 :         bitmap_ior_into (m_incoming[bb->index], imp);
     484              :       // This name is always an import.
     485    131947435 :       if (gimple_bb (SSA_NAME_DEF_STMT (name)) != bb)
     486     34327706 :         bitmap_set_bit (m_incoming[bb->index], SSA_NAME_VERSION (name));
     487              : 
     488              :       // Def chain doesn't include itself, and even if there isn't a
     489              :       // def chain, this name should be added to exports.
     490    131947435 :       bitmap_set_bit (m_outgoing[bb->index], SSA_NAME_VERSION (name));
     491              :     }
     492    214122137 : }
     493              : 
     494              : // Calculate all the required information for BB.
     495              : 
     496              : void
     497    340537150 : gori_map::calculate_gori (basic_block bb)
     498              : {
     499    340537150 :   tree name;
     500    681074300 :   if (bb->index >= (signed int)m_outgoing.length ())
     501              :     {
     502         1437 :       m_outgoing.safe_grow_cleared (last_basic_block_for_fn (cfun));
     503         1437 :       m_incoming.safe_grow_cleared (last_basic_block_for_fn (cfun));
     504              :     }
     505    340537150 :   gcc_checking_assert (m_outgoing[bb->index] == NULL);
     506    340537150 :   m_outgoing[bb->index] = BITMAP_ALLOC (&m_bitmaps);
     507    340537150 :   m_incoming[bb->index] = BITMAP_ALLOC (&m_bitmaps);
     508              : 
     509    340537150 :   if (single_succ_p (bb))
     510              :     return;
     511              : 
     512              :   // If this block's last statement may generate range information, go
     513              :   // calculate it.
     514    176488455 :   gimple *stmt = gimple_outgoing_range_stmt_p (bb);
     515    176488455 :   if (!stmt)
     516              :     return;
     517    107355850 :   if (is_a<gcond *> (stmt))
     518              :     {
     519    106767700 :       gcond *gc = as_a<gcond *>(stmt);
     520    106767700 :       name = gimple_range_ssa_p (gimple_cond_lhs (gc));
     521    106767700 :       maybe_add_gori (name, gimple_bb (stmt));
     522              : 
     523    106767700 :       name = gimple_range_ssa_p (gimple_cond_rhs (gc));
     524    106767700 :       maybe_add_gori (name, gimple_bb (stmt));
     525              :     }
     526              :   else
     527              :     {
     528              :       // Do not process switches if they are too large.
     529       588150 :       if (EDGE_COUNT (bb->succs) > (unsigned)param_vrp_switch_limit)
     530              :         return;
     531       586737 :       gswitch *gs = as_a<gswitch *>(stmt);
     532       586737 :       name = gimple_range_ssa_p (gimple_switch_index (gs));
     533       586737 :       maybe_add_gori (name, gimple_bb (stmt));
     534              :     }
     535              :   // Add this bitmap to the aggregate list of all outgoing names.
     536    107354437 :   bitmap_ior_into (m_maybe_variant, m_outgoing[bb->index]);
     537              : }
     538              : 
     539              : // Dump the table information for BB to file F.
     540              : 
     541              : void
     542          261 : gori_map::dump (FILE *f, basic_block bb, bool verbose)
     543              : {
     544              :   // BB was not processed.
     545          261 :   if (!m_outgoing[bb->index] || bitmap_empty_p (m_outgoing[bb->index]))
     546              :     return;
     547              : 
     548          119 :   tree name;
     549              : 
     550          119 :   bitmap imp = imports (bb);
     551          119 :   if (!bitmap_empty_p (imp))
     552              :     {
     553          119 :       if (verbose)
     554            0 :         fprintf (f, "bb<%u> Imports: ",bb->index);
     555              :       else
     556          119 :         fprintf (f, "Imports: ");
     557          285 :       FOR_EACH_GORI_IMPORT_NAME (this, bb, name)
     558              :         {
     559          166 :           print_generic_expr (f, name, TDF_SLIM);
     560          166 :           fprintf (f, "  ");
     561              :         }
     562          119 :       fputc ('\n', f);
     563              :     }
     564              : 
     565          119 :   if (verbose)
     566            0 :     fprintf (f, "bb<%u> Exports: ",bb->index);
     567              :   else
     568          119 :     fprintf (f, "Exports: ");
     569              :   // Dump the export vector.
     570          396 :   FOR_EACH_GORI_EXPORT_NAME (this, bb, name)
     571              :     {
     572          277 :       print_generic_expr (f, name, TDF_SLIM);
     573          277 :       fprintf (f, "  ");
     574              :     }
     575          119 :   fputc ('\n', f);
     576              : 
     577          119 :   range_def_chain::dump (f, bb, "         ");
     578              : }
     579              : 
     580              : // Dump the entire GORI map structure to file F.
     581              : 
     582              : void
     583            0 : gori_map::dump (FILE *f)
     584              : {
     585            0 :   basic_block bb;
     586            0 :   FOR_EACH_BB_FN (bb, cfun)
     587            0 :     dump (f, bb);
     588            0 : }
     589              : 
     590              : DEBUG_FUNCTION void
     591            0 : debug (gori_map &g)
     592              : {
     593            0 :   g.dump (stderr);
     594            0 : }
     595              : 
     596              : // -------------------------------------------------------------------
     597              : 
     598              : // Construct a gori_compute object.
     599              : 
     600     29776596 : gori_compute::gori_compute (gori_map &map, int not_executable_flag,
     601              :                             int sw_max_edges)
     602     29776596 :   : gimple_outgoing_range (sw_max_edges), m_map (map), tracer ("GORI ")
     603              : {
     604     29776596 :   m_not_executable_flag = not_executable_flag;
     605              :   // Create a boolean_type true and false range.
     606     29776596 :   m_bool_zero = range_false ();
     607     29776596 :   m_bool_one = range_true ();
     608     29776596 :   if (dump_file && (param_ranger_debug & RANGER_DEBUG_GORI))
     609            0 :     tracer.enable_trace ();
     610              : 
     611              :   // Reduce maximum recompute depth based on the size of the CFG to avoid
     612              :   // excessive compuations in large CFGs.
     613     29776596 :   m_recompute_depth = (int) param_ranger_recompute_depth
     614     29776596 :                       - (int) last_basic_block_for_fn (cfun) / 4096;
     615     29776596 :   if (m_recompute_depth < 1)
     616            3 :     m_recompute_depth = 1;
     617     29776596 : }
     618              : 
     619     59553192 : gori_compute::~gori_compute ()
     620              : {
     621     59553192 : }
     622              : 
     623              : // Given the switch S, return an evaluation in R for NAME when the lhs
     624              : // evaluates to LHS.  Returning false means the name being looked for
     625              : // was not resolvable.
     626              : 
     627              : bool
     628       188289 : gori_compute::compute_operand_range_switch (vrange &r, gswitch *s,
     629              :                                             const vrange &lhs,
     630              :                                             tree name, fur_source &src)
     631              : {
     632       188289 :   tree op1 = gimple_switch_index (s);
     633              : 
     634              :   // If name matches, the range is simply the range from the edge.
     635              :   // Empty ranges are viral as they are on a path which isn't
     636              :   // executable.
     637       188289 :   if (op1 == name || lhs.undefined_p ())
     638              :     {
     639       138882 :       r = lhs;
     640       138882 :       return true;
     641              :     }
     642              : 
     643              :   // If op1 is in the definition chain, pass lhs back.
     644        49407 :   if (gimple_range_ssa_p (op1) && m_map.in_chain_p (name, op1))
     645        48872 :     return compute_operand_range (r, SSA_NAME_DEF_STMT (op1), lhs, name, src);
     646              : 
     647              :   return false;
     648              : }
     649              : 
     650              : 
     651              : // Return an evaluation for NAME as it would appear in STMT when the
     652              : // statement's lhs evaluates to LHS.  If successful, return TRUE and
     653              : // store the evaluation in R, otherwise return FALSE.
     654              : 
     655              : bool
     656    164413992 : gori_compute::compute_operand_range (vrange &r, gimple *stmt,
     657              :                                      const vrange &lhs, tree name,
     658              :                                      fur_source &src, value_relation *rel)
     659              : {
     660    164413992 :   value_relation vrel;
     661    164413992 :   value_relation *vrel_ptr = rel;
     662              :   // Empty ranges are viral as they are on an unexecutable path.
     663    164413992 :   if (lhs.undefined_p ())
     664              :     {
     665       134653 :       r.set_undefined ();
     666       134653 :       return true;
     667              :     }
     668    164279339 :   if (is_a<gswitch *> (stmt))
     669       188289 :     return compute_operand_range_switch (r, as_a<gswitch *> (stmt), lhs, name,
     670       188289 :                                          src);
     671    164091050 :   gimple_range_op_handler handler (stmt);
     672    164091050 :   if (!handler)
     673              :     return false;
     674              : 
     675    163928571 :   tree op1 = gimple_range_ssa_p (handler.operand1 ());
     676    163928571 :   tree op2 = gimple_range_ssa_p (handler.operand2 ());
     677              : 
     678              :   // If there is a relation between op1 and op2, use it instead as it is
     679              :   // likely to be more applicable.
     680    163928571 :   if (op1 && op2)
     681              :     {
     682     78754303 :       value_range r1, r2;
     683     78754303 :       r1.set_varying (TREE_TYPE (op1));
     684     78754303 :       r2.set_varying (TREE_TYPE (op2));
     685     78754303 :       relation_kind k = handler.op1_op2_relation (lhs, r1, r2);
     686     78754303 :       if (k != VREL_VARYING)
     687              :         {
     688     54967529 :           vrel.set_relation (k, op1, op2);
     689     54967529 :           vrel_ptr = &vrel;
     690              :         }
     691     78754303 :     }
     692              : 
     693              :   // Handle end of lookup first.
     694    163928571 :   if (op1 == name)
     695     67431528 :     return compute_operand1_range (r, handler, lhs, src, vrel_ptr);
     696     96497043 :   if (op2 == name)
     697     24092095 :     return compute_operand2_range (r, handler, lhs, src, vrel_ptr);
     698              : 
     699              :   // NAME is not in this stmt, but one of the names in it ought to be
     700              :   // derived from it.
     701     72404948 :   bool op1_in_chain = op1 && m_map.in_chain_p (name, op1);
     702     72404948 :   bool op2_in_chain = op2 && m_map.in_chain_p (name, op2);
     703              : 
     704              :   // If neither operand is derived, then this stmt tells us nothing.
     705     55574744 :   if (!op1_in_chain && !op2_in_chain)
     706              :     return false;
     707              : 
     708              :   // If either operand is in the def chain of the other (or they are equal), it
     709              :   // will be evaluated twice and can result in an exponential time calculation.
     710              :   // Instead just evaluate the one operand.
     711     71192870 :   if (op1_in_chain && op2_in_chain)
     712              :     {
     713      3324624 :       if (m_map.in_chain_p (op1, op2) || op1 == op2)
     714              :         op1_in_chain = false;
     715      2539856 :       else if (m_map.in_chain_p (op2, op1))
     716       137307 :         op2_in_chain = false;
     717              :     }
     718              : 
     719     71192870 :   bool res = false;
     720              :   // If the lhs doesn't tell us anything only a relation can possibly enhance
     721              :   // the result.
     722     71192870 :   if (lhs.varying_p ())
     723              :     {
     724          656 :       if (!vrel_ptr)
     725              :         return false;
     726              :       // If there is a relation (ie: x != y) , it can only be relevant if
     727              :       // a) both elements are in the defchain
     728              :       //    c = x > y   // (x and y are in c's defchain)
     729          656 :       if (op1_in_chain)
     730          656 :         res = m_map.in_chain_p (vrel_ptr->op1 (), op1)
     731          656 :               && m_map.in_chain_p (vrel_ptr->op2 (), op1);
     732          656 :       if (!res && op2_in_chain)
     733            0 :         res = m_map.in_chain_p (vrel_ptr->op1 (), op2)
     734            0 :               || m_map.in_chain_p (vrel_ptr->op2 (), op2);
     735          656 :       if (!res)
     736              :         {
     737              :           // or b) one relation element is in the defchain of the other and the
     738              :           //       other is the LHS of this stmt.
     739              :           //  x = y + 2
     740          656 :           if (vrel_ptr->op1 () == handler.lhs ()
     741          656 :               && (vrel_ptr->op2 () == op1 || vrel_ptr->op2 () == op2))
     742              :             res = true;
     743          656 :           else if (vrel_ptr->op2 () == handler.lhs ()
     744          656 :                    && (vrel_ptr->op1 () == op1 || vrel_ptr->op1 () == op2))
     745              :             res = true;
     746              :         }
     747              :       if (!res)
     748              :         return false;
     749              :     }
     750              : 
     751              :   // Process logicals as they have special handling.
     752     71192214 :   if (is_gimple_logical_p (stmt))
     753              :     {
     754              :       // If the lhs doesn't tell us anything, neither will combining operands.
     755      5348993 :       if (lhs.varying_p ())
     756              :         return false;
     757              : 
     758      5348993 :       unsigned idx;
     759      5348993 :       if ((idx = tracer.header ("compute_operand ")))
     760              :         {
     761            0 :           print_generic_expr (dump_file, name, TDF_SLIM);
     762            0 :           fprintf (dump_file, " with LHS = ");
     763            0 :           lhs.dump (dump_file);
     764            0 :           fprintf (dump_file, " at stmt ");
     765            0 :           print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
     766              :         }
     767              : 
     768      5348993 :       tree type = TREE_TYPE (name);
     769      5348993 :       value_range op1_trange (type), op1_frange (type);
     770      5348993 :       value_range op2_trange (type), op2_frange (type);
     771      5348993 :       compute_logical_operands (op1_trange, op1_frange, handler,
     772              :                                 as_a <irange> (lhs),
     773              :                                 name, src, op1, op1_in_chain);
     774      5348993 :       compute_logical_operands (op2_trange, op2_frange, handler,
     775              :                                 as_a <irange> (lhs),
     776              :                                 name, src, op2, op2_in_chain);
     777      5348993 :       res = logical_combine (r,
     778              :                              gimple_expr_code (stmt),
     779              :                              as_a <irange> (lhs),
     780      5348993 :                              op1_trange, op1_frange, op2_trange, op2_frange);
     781      5348993 :       if (idx)
     782            0 :         tracer.trailer (idx, "compute_operand", res, name, r);
     783      5348993 :       return res;
     784      5348993 :     }
     785              :   // Follow the appropriate operands now.
     786     65843221 :   if (op1_in_chain && op2_in_chain)
     787       549390 :     return compute_operand1_and_operand2_range (r, handler, lhs, name, src,
     788       549390 :                                                 vrel_ptr);
     789     65293831 :   value_range vr;
     790     65293831 :   gimple *src_stmt;
     791     65293831 :   if (op1_in_chain)
     792              :     {
     793     52966260 :       vr.set_range_class (TREE_TYPE (op1));
     794     52966260 :       if (!compute_operand1_range (vr, handler, lhs, src, vrel_ptr))
     795              :         return false;
     796     45747573 :       src_stmt = SSA_NAME_DEF_STMT (op1);
     797              :     }
     798              :   else
     799              :     {
     800     12327571 :       gcc_checking_assert (op2_in_chain);
     801     12327571 :       vr.set_range_class (TREE_TYPE (op2));
     802     12327571 :       if (!compute_operand2_range (vr, handler, lhs, src, vrel_ptr))
     803              :         return false;
     804      4660795 :       src_stmt = SSA_NAME_DEF_STMT (op2);
     805              :     }
     806              : 
     807     50408368 :   gcc_checking_assert (src_stmt);
     808              :   // Then feed this range back as the LHS of the defining statement.
     809     50408368 :   return compute_operand_range (r, src_stmt, vr, name, src, vrel_ptr);
     810              :   // If neither operand is derived, this statement tells us nothing.
     811     65293831 : }
     812              : 
     813              : 
     814              : // Return TRUE if range R is either a true or false compatible range.
     815              : 
     816              : static bool
     817      4365530 : range_is_either_true_or_false (const irange &r)
     818              : {
     819      4365530 :   if (r.undefined_p ())
     820              :     return false;
     821              : 
     822              :   // This is complicated by the fact that Ada has multi-bit booleans,
     823              :   // so true can be ~[0, 0] (i.e. [1,MAX]).
     824      4365530 :   tree type = r.type ();
     825      4365530 :   gcc_checking_assert (range_compatible_p (type, boolean_type_node));
     826      4365530 :   return (r.singleton_p ()
     827      4365530 :           || !r.contains_p (wi::zero (TYPE_PRECISION (type))));
     828              : }
     829              : 
     830              : // Evaluate a binary logical expression by combining the true and
     831              : // false ranges for each of the operands based on the result value in
     832              : // the LHS.
     833              : 
     834              : bool
     835      5348993 : gori_compute::logical_combine (vrange &r, enum tree_code code,
     836              :                                const irange &lhs,
     837              :                                const vrange &op1_true, const vrange &op1_false,
     838              :                                const vrange &op2_true, const vrange &op2_false)
     839              : {
     840      5348993 :   if (op1_true.varying_p () && op1_false.varying_p ()
     841      7084948 :       && op2_true.varying_p () && op2_false.varying_p ())
     842              :     return false;
     843              : 
     844      4365530 :   unsigned idx;
     845      4365530 :   if ((idx = tracer.header ("logical_combine")))
     846              :     {
     847            0 :       switch (code)
     848              :         {
     849            0 :           case TRUTH_OR_EXPR:
     850            0 :           case BIT_IOR_EXPR:
     851            0 :             fprintf (dump_file, " || ");
     852            0 :             break;
     853            0 :           case TRUTH_AND_EXPR:
     854            0 :           case BIT_AND_EXPR:
     855            0 :             fprintf (dump_file, " && ");
     856            0 :             break;
     857              :           default:
     858              :             break;
     859              :         }
     860            0 :       fprintf (dump_file, " with LHS = ");
     861            0 :       lhs.dump (dump_file);
     862            0 :       fputc ('\n', dump_file);
     863              : 
     864            0 :       tracer.print (idx, "op1_true = ");
     865            0 :       op1_true.dump (dump_file);
     866            0 :       fprintf (dump_file, "  op1_false = ");
     867            0 :       op1_false.dump (dump_file);
     868            0 :       fputc ('\n', dump_file);
     869            0 :       tracer.print (idx, "op2_true = ");
     870            0 :       op2_true.dump (dump_file);
     871            0 :       fprintf (dump_file, "  op2_false = ");
     872            0 :       op2_false.dump (dump_file);
     873            0 :       fputc ('\n', dump_file);
     874              :     }
     875              : 
     876              :   // This is not a simple fold of a logical expression, rather it
     877              :   // determines ranges which flow through the logical expression.
     878              :   //
     879              :   // Assuming x_8 is an unsigned char, and relational statements:
     880              :   //          b_1 = x_8 < 20
     881              :   //          b_2 = x_8 > 5
     882              :   // consider the logical expression and branch:
     883              :   //          c_2 = b_1 && b_2
     884              :   //          if (c_2)
     885              :   //
     886              :   // To determine the range of x_8 on either edge of the branch, one
     887              :   // must first determine what the range of x_8 is when the boolean
     888              :   // values of b_1 and b_2 are both true and false.
     889              :   //    b_1 TRUE      x_8 = [0, 19]
     890              :   //    b_1 FALSE     x_8 = [20, 255]
     891              :   //    b_2 TRUE      x_8 = [6, 255]
     892              :   //    b_2 FALSE     x_8 = [0,5].
     893              :   //
     894              :   // These ranges are then combined based on the expected outcome of
     895              :   // the branch.  The range on the TRUE side of the branch must satisfy
     896              :   //     b_1 == true && b_2 == true
     897              :   //
     898              :   // In terms of x_8, that means both x_8 == [0, 19] and x_8 = [6, 255]
     899              :   // must be true.  The range of x_8 on the true side must be the
     900              :   // intersection of both ranges since both must be true.  Thus the
     901              :   // range of x_8 on the true side is [6, 19].
     902              :   //
     903              :   // To determine the ranges on the FALSE side, all 3 combinations of
     904              :   // failing ranges must be considered, and combined as any of them
     905              :   // can cause the false result.
     906              :   //
     907              :   // If the LHS can be TRUE or FALSE, then evaluate both a TRUE and
     908              :   // FALSE results and combine them.  If we fell back to VARYING any
     909              :   // range restrictions that have been discovered up to this point
     910              :   // would be lost.
     911      4365530 :   if (!range_is_either_true_or_false (lhs))
     912              :     {
     913            0 :       bool res;
     914            0 :       value_range r1 (r);
     915            0 :       if (logical_combine (r1, code, m_bool_zero, op1_true, op1_false,
     916              :                            op2_true, op2_false)
     917            0 :           && logical_combine (r, code, m_bool_one, op1_true, op1_false,
     918              :                               op2_true, op2_false))
     919              :         {
     920            0 :           r.union_ (r1);
     921            0 :           res = true;
     922              :         }
     923              :       else
     924              :         res = false;
     925            0 :       if (idx && res)
     926              :         {
     927            0 :           tracer.print (idx, "logical_combine produced ");
     928            0 :           r.dump (dump_file);
     929            0 :           fputc ('\n', dump_file);
     930              :         }
     931            0 :       return res;
     932            0 :     }
     933              : 
     934      4365530 :   switch (code)
     935              :     {
     936              :       //  A logical AND combines ranges from 2 boolean conditions.
     937              :       //       c_2 = b_1 && b_2
     938      2913138 :       case TRUTH_AND_EXPR:
     939      2913138 :       case BIT_AND_EXPR:
     940      2913138 :         if (!lhs.zero_p ())
     941              :           {
     942              :             // The TRUE side is the intersection of the 2 true ranges.
     943      1836186 :             r = op1_true;
     944      1836186 :             r.intersect (op2_true);
     945              :           }
     946              :         else
     947              :           {
     948              :             // The FALSE side is the union of the other 3 cases.
     949      1076952 :             value_range ff (op1_false);
     950      1076952 :             ff.intersect (op2_false);
     951      1076952 :             value_range tf (op1_true);
     952      1076952 :             tf.intersect (op2_false);
     953      1076952 :             value_range ft (op1_false);
     954      1076952 :             ft.intersect (op2_true);
     955      1076952 :             r = ff;
     956      1076952 :             r.union_ (tf);
     957      1076952 :             r.union_ (ft);
     958      1076952 :           }
     959              :         break;
     960              :       //  A logical OR combines ranges from 2 boolean conditions.
     961              :       //        c_2 = b_1 || b_2
     962      1452392 :       case TRUTH_OR_EXPR:
     963      1452392 :       case BIT_IOR_EXPR:
     964      1452392 :         if (lhs.zero_p ())
     965              :           {
     966              :             // An OR operation will only take the FALSE path if both
     967              :             // operands are false simultaneously, which means they should
     968              :             // be intersected.  !(x || y) == !x && !y
     969      1023883 :             r = op1_false;
     970      1023883 :             r.intersect (op2_false);
     971              :           }
     972              :         else
     973              :           {
     974              :             // The TRUE side of an OR operation will be the union of
     975              :             // the other three combinations.
     976       428509 :             value_range tt (op1_true);
     977       428509 :             tt.intersect (op2_true);
     978       428509 :             value_range tf (op1_true);
     979       428509 :             tf.intersect (op2_false);
     980       428509 :             value_range ft (op1_false);
     981       428509 :             ft.intersect (op2_true);
     982       428509 :             r = tt;
     983       428509 :             r.union_ (tf);
     984       428509 :             r.union_ (ft);
     985       428509 :           }
     986              :         break;
     987            0 :       default:
     988            0 :         gcc_unreachable ();
     989              :     }
     990              : 
     991      4365530 :   if (idx)
     992            0 :     tracer.trailer (idx, "logical_combine", true, NULL_TREE, r);
     993              :   return true;
     994              : }
     995              : 
     996              : 
     997              : // Given a logical STMT, calculate true and false ranges for each
     998              : // potential path of NAME, assuming NAME came through the OP chain if
     999              : // OP_IN_CHAIN is true.
    1000              : 
    1001              : void
    1002     10697986 : gori_compute::compute_logical_operands (vrange &true_range, vrange &false_range,
    1003              :                                         gimple_range_op_handler &handler,
    1004              :                                         const irange &lhs,
    1005              :                                         tree name, fur_source &src,
    1006              :                                         tree op, bool op_in_chain)
    1007              : {
    1008     10697986 :   gimple *stmt = handler.stmt ();
    1009     21395972 :   gimple *src_stmt = gimple_range_ssa_p (op) ? SSA_NAME_DEF_STMT (op) : NULL;
    1010     10697986 :   if (!op_in_chain || !src_stmt || m_map.chain_import_p (handler.lhs (), op))
    1011              :     {
    1012              :       // If op is not in the def chain, or defined in this block,
    1013              :       // use its known value on entry to the block.
    1014      3539622 :       src.get_operand (true_range, name);
    1015      3539622 :       false_range = true_range;
    1016      3539622 :       unsigned idx;
    1017      3539622 :       if ((idx = tracer.header ("logical_operand")))
    1018              :         {
    1019            0 :           print_generic_expr (dump_file, op, TDF_SLIM);
    1020            0 :           fprintf (dump_file, " not in computation chain. Queried.\n");
    1021            0 :           tracer.trailer (idx, "logical_operand", true, NULL_TREE, true_range);
    1022              :         }
    1023              :       return;
    1024              :     }
    1025              : 
    1026      7158364 :   enum tree_code code = gimple_expr_code (stmt);
    1027              :   // Optimize [0 = x | y], since neither operand can ever be non-zero.
    1028      7158364 :   if ((code == BIT_IOR_EXPR || code == TRUTH_OR_EXPR) && lhs.zero_p ())
    1029              :     {
    1030      1662424 :       if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name,
    1031              :                                   src))
    1032       720586 :         src.get_operand (false_range, name);
    1033      1662424 :       true_range = false_range;
    1034      1662424 :       return;
    1035              :     }
    1036              : 
    1037              :   // Optimize [1 = x & y], since neither operand can ever be zero.
    1038      5495940 :   if ((code == BIT_AND_EXPR || code == TRUTH_AND_EXPR) && lhs == m_bool_one)
    1039              :     {
    1040      3362828 :       if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
    1041      1806029 :         src.get_operand (true_range, name);
    1042      3362828 :       false_range = true_range;
    1043      3362828 :       return;
    1044              :     }
    1045              : 
    1046              :   // Calculate ranges for true and false on both sides, since the false
    1047              :   // path is not always a simple inversion of the true side.
    1048      2133112 :   if (!compute_operand_range (true_range, src_stmt, m_bool_one, name, src))
    1049       927524 :     src.get_operand (true_range, name);
    1050      2133112 :   if (!compute_operand_range (false_range, src_stmt, m_bool_zero, name, src))
    1051       826288 :     src.get_operand (false_range, name);
    1052              : }
    1053              : 
    1054              : 
    1055              : // This routine will try to refine the ranges of OP1 and OP2 given a relation
    1056              : // K between them.  In order to perform this refinement, one of the operands
    1057              : // must be in the definition chain of the other.  The use is refined using
    1058              : // op1/op2_range on the statement, and the definition is then recalculated
    1059              : // using the relation.
    1060              : 
    1061              : bool
    1062     54742573 : gori_compute::refine_using_relation (tree op1, vrange &op1_range,
    1063              :                                tree op2, vrange &op2_range,
    1064              :                                fur_source &src, relation_kind k)
    1065              : {
    1066     54742573 :   gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
    1067     54742573 :   gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
    1068              : 
    1069     54742573 :   if (k == VREL_VARYING || k == VREL_EQ || k == VREL_UNDEFINED)
    1070              :     return false;
    1071              : 
    1072     46099659 :   bool change = false;
    1073     46099659 :   bool op1_def_p = m_map.in_chain_p (op2, op1);
    1074     46099659 :   if (!op1_def_p)
    1075     44898449 :     if (!m_map.in_chain_p (op1, op2))
    1076              :       return false;
    1077              : 
    1078      3056511 :   tree def_op = op1_def_p ? op1 : op2;
    1079              :   tree use_op = op1_def_p ? op2 : op1;
    1080              : 
    1081      3056511 :   if (!op1_def_p)
    1082      3056511 :     k = relation_swap (k);
    1083              : 
    1084              :   // op1_def is true if we want to look up op1, otherwise we want op2.
    1085              :   // if neither is the case, we returned in the above check.
    1086              : 
    1087      4257721 :   gimple *def_stmt = SSA_NAME_DEF_STMT (def_op);
    1088      4257721 :   gimple_range_op_handler op_handler (def_stmt);
    1089      4257721 :   if (!op_handler)
    1090              :     return false;
    1091      4254631 :   tree def_op1 = op_handler.operand1 ();
    1092      4254631 :   tree def_op2 = op_handler.operand2 ();
    1093              :   // if the def isn't binary, the relation will not be useful.
    1094      4254631 :   if (!def_op2)
    1095              :     return false;
    1096              : 
    1097              :   // Determine if op2 is directly referenced as an operand.
    1098      4178202 :   if (def_op1 == use_op)
    1099              :     {
    1100              :       // def_stmt has op1 in the 1st operand position.
    1101      1331356 :       value_range other_op (TREE_TYPE (def_op2));
    1102      1331356 :       src.get_operand (other_op, def_op2);
    1103              : 
    1104              :       // Using op1_range as the LHS, and relation REL, evaluate op2.
    1105      1331356 :       tree type = TREE_TYPE (def_op1);
    1106      1331356 :       value_range new_result (type);
    1107      2335088 :       if (!op_handler.op1_range (new_result, type,
    1108              :                                  op1_def_p ? op1_range : op2_range,
    1109      1331356 :                                  other_op, relation_trio::lhs_op1 (k)))
    1110        78192 :         return false;
    1111              :       // Note: K arrives as an op1/op2 relation, and because of
    1112              :       // the definition chain requirement here, it is also an LHS relation on the
    1113              :       // defining statement, which is how it is passed to the range operators below.
    1114      1253164 :       if (op1_def_p)
    1115              :         {
    1116       303124 :           change |= op2_range.intersect (new_result);
    1117              :           // Recalculate the LHS passing the lhs-op1 relation so that
    1118              :           // operator_plus::fold_range can tighten the no-overflow range.
    1119       303124 :           if (op_handler.fold_range (new_result, type, op2_range, other_op,
    1120              :                                      relation_trio::lhs_op1 (k)))
    1121              :             {
    1122       303124 :               change |= op1_range.intersect (new_result);
    1123              :             }
    1124              :         }
    1125              :       else
    1126              :         {
    1127       950040 :           change |= op1_range.intersect (new_result);
    1128              :           // Recalculate the LHS passing the lhs-op1 relation.
    1129       950040 :           if (op_handler.fold_range (new_result, type, op1_range, other_op,
    1130              :                                      relation_trio::lhs_op1 (k)))
    1131              :             {
    1132       950040 :               change |= op2_range.intersect (new_result);
    1133              :             }
    1134              :         }
    1135      1331356 :     }
    1136      2846846 :   else if (def_op2 == use_op)
    1137              :     {
    1138              :       // def_stmt has op1 in the 1st operand position.
    1139      1982864 :       value_range other_op (TREE_TYPE (def_op1));
    1140      1982864 :       src.get_operand (other_op, def_op1);
    1141              : 
    1142              :       // Using op1_range as the LHS, and relation REL, evaluate op2.
    1143      1982864 :       tree type = TREE_TYPE (def_op2);
    1144      1982864 :       value_range new_result (type);
    1145      3693514 :       if (!op_handler.op2_range (new_result, type,
    1146              :                                  op1_def_p ? op1_range : op2_range,
    1147      1982864 :                                  other_op, relation_trio::lhs_op2 (k)))
    1148        78303 :         return false;
    1149              :       // Note: K arrives as an op1/op2 relation, and because of
    1150              :       // the definition chain requirement here, it is also an LHS relation on the
    1151              :       // defining statement, which is how it is passed to the range operators below.
    1152      1904561 :       if (op1_def_p)
    1153              :         {
    1154       266370 :           change |= op2_range.intersect (new_result);
    1155              :           // Recalculate the LHS passing the lhs-op2 relation.
    1156       266370 :           if (op_handler.fold_range (new_result, type, other_op, op2_range,
    1157              :                                      relation_trio::lhs_op2 (k)))
    1158              :             {
    1159       266370 :               change |= op1_range.intersect (new_result);
    1160              :             }
    1161              :         }
    1162              :       else
    1163              :         {
    1164      1638191 :           change |= op1_range.intersect (new_result);
    1165              :           // Recalculate the LHS passing the lhs-op2 relation.
    1166      1638191 :           if (op_handler.fold_range (new_result, type, other_op, op1_range,
    1167              :                                      relation_trio::lhs_op2 (k)))
    1168              :             {
    1169      1638191 :               change |= op2_range.intersect (new_result);
    1170              :             }
    1171              :         }
    1172      1982864 :     }
    1173              :   return change;
    1174              : }
    1175              : 
    1176              : // Calculate a range for NAME from the operand 1 position of STMT
    1177              : // assuming the result of the statement is LHS.  Return the range in
    1178              : // R, or false if no range could be calculated.
    1179              : 
    1180              : bool
    1181    120460326 : gori_compute::compute_operand1_range (vrange &r,
    1182              :                                       gimple_range_op_handler &handler,
    1183              :                                       const vrange &lhs,
    1184              :                                       fur_source &src, value_relation *rel)
    1185              : {
    1186    120460326 :   gimple *stmt = handler.stmt ();
    1187    120460326 :   tree op1 = handler.operand1 ();
    1188    120460326 :   tree op2 = handler.operand2 ();
    1189    120460326 :   tree lhs_name = gimple_get_lhs (stmt);
    1190    120460326 :   bool relation_change = false;
    1191              : 
    1192    120460326 :   relation_trio trio;
    1193    120460326 :   if (rel)
    1194     35176643 :     trio = rel->create_trio (lhs_name, op1, op2);
    1195              : 
    1196    120460326 :   value_range op1_range (TREE_TYPE (op1));
    1197    120460326 :   value_range op2_range (op2 ? TREE_TYPE (op2) : TREE_TYPE (op1));
    1198              : 
    1199              :   // Fetch the known range for op1 in this block.
    1200    120460326 :   src.get_operand (op1_range, op1);
    1201              : 
    1202              :   // Now range-op calculate and put that result in r.
    1203    120460326 :   if (op2)
    1204              :     {
    1205    109299674 :       src.get_operand (op2_range, op2);
    1206              : 
    1207    109299674 :       relation_kind op_op = trio.op1_op2 ();
    1208    109299674 :       if (op_op != VREL_VARYING)
    1209     27508172 :         relation_change = refine_using_relation (op1, op1_range, op2,
    1210              :                                                  op2_range, src, op_op);
    1211              : 
    1212              :       // If op1 == op2, create a new trio for just this call.
    1213    109299674 :       if (op1 == op2 && gimple_range_ssa_p (op1))
    1214       109509 :         trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
    1215    109299674 :       if (!handler.calc_op1 (r, lhs, op2_range, trio))
    1216              :         return false;
    1217              :     }
    1218              :   else
    1219              :     {
    1220              :       // We pass op1_range to the unary operation.  Normally it's a
    1221              :       // hidden range_for_type parameter, but sometimes having the
    1222              :       // actual range can result in better information.
    1223     11160652 :       if (!handler.calc_op1 (r, lhs, op1_range, trio))
    1224              :         return false;
    1225              :     }
    1226              : 
    1227    116915775 :   unsigned idx;
    1228    116915775 :   if ((idx = tracer.header ("compute op 1 (")))
    1229              :     {
    1230            0 :       print_generic_expr (dump_file, op1, TDF_SLIM);
    1231            0 :       fprintf (dump_file, ") at ");
    1232            0 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    1233            0 :       tracer.print (idx, "LHS =");
    1234            0 :       lhs.dump (dump_file);
    1235            0 :       if (op2 && TREE_CODE (op2) == SSA_NAME)
    1236              :         {
    1237            0 :           fprintf (dump_file, ", ");
    1238            0 :           print_generic_expr (dump_file, op2, TDF_SLIM);
    1239            0 :           fprintf (dump_file, " = ");
    1240            0 :           op2_range.dump (dump_file);
    1241              :         }
    1242            0 :       fprintf (dump_file, "\n");
    1243            0 :       tracer.print (idx, "Computes ");
    1244            0 :       print_generic_expr (dump_file, op1, TDF_SLIM);
    1245            0 :       fprintf (dump_file, " = ");
    1246            0 :       r.dump (dump_file);
    1247            0 :       fprintf (dump_file, " intersect Known range : ");
    1248            0 :       op1_range.dump (dump_file);
    1249            0 :       fputc ('\n', dump_file);
    1250              :     }
    1251              : 
    1252    116915775 :   r.intersect (op1_range);
    1253    116915775 :   bool changed = relation_change || r.undefined_p () || r != op1_range;
    1254    116915775 :   if (idx)
    1255            0 :     tracer.trailer (idx, "produces ", changed, op1, r);
    1256              :   return changed;
    1257    120460326 : }
    1258              : 
    1259              : 
    1260              : // Calculate a range for NAME from the operand 2 position of S
    1261              : // assuming the result of the statement is LHS.  Return the range in
    1262              : // R, or false if no range could be calculated.
    1263              : 
    1264              : bool
    1265     36969056 : gori_compute::compute_operand2_range (vrange &r,
    1266              :                                       gimple_range_op_handler &handler,
    1267              :                                       const vrange &lhs,
    1268              :                                       fur_source &src, value_relation *rel)
    1269              : {
    1270     36969056 :   gimple *stmt = handler.stmt ();
    1271     36969056 :   tree op1 = handler.operand1 ();
    1272     36969056 :   tree op2 = handler.operand2 ();
    1273     36969056 :   tree lhs_name = gimple_get_lhs (stmt);
    1274     36969056 :   bool relation_change = false;
    1275              : 
    1276     36969056 :   value_range op1_range (TREE_TYPE (op1));
    1277     36969056 :   value_range op2_range (TREE_TYPE (op2));
    1278              : 
    1279     36969056 :   src.get_operand (op1_range, op1);
    1280     36969056 :   src.get_operand (op2_range, op2);
    1281              : 
    1282     36969056 :   relation_trio trio;
    1283     36969056 :   if (rel)
    1284     29914085 :     trio = rel->create_trio (lhs_name, op1, op2);
    1285     36969056 :   relation_kind op_op = trio.op1_op2 ();
    1286              : 
    1287     36969056 :   if (op_op != VREL_VARYING)
    1288     27234401 :     relation_change = refine_using_relation (op1, op1_range, op2,
    1289              :                                              op2_range, src, op_op);
    1290              : 
    1291              :   // If op1 == op2, create a new trio for this stmt.
    1292     36969056 :   if (op1 == op2 && gimple_range_ssa_p (op1))
    1293        30516 :     trio = relation_trio (trio.lhs_op1 (), trio.lhs_op2 (), VREL_EQ);
    1294              :   // Intersect with range for op2 based on lhs and op1.
    1295     36969056 :   if (!handler.calc_op2 (r, lhs, op1_range, trio))
    1296              :     return false;
    1297              : 
    1298     33854782 :   unsigned idx;
    1299     33854782 :   if ((idx = tracer.header ("compute op 2 (")))
    1300              :     {
    1301            0 :       print_generic_expr (dump_file, op2, TDF_SLIM);
    1302            0 :       fprintf (dump_file, ") at ");
    1303            0 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    1304            0 :       tracer.print (idx, "LHS = ");
    1305            0 :       lhs.dump (dump_file);
    1306            0 :       if (TREE_CODE (op1) == SSA_NAME)
    1307              :         {
    1308            0 :           fprintf (dump_file, ", ");
    1309            0 :           print_generic_expr (dump_file, op1, TDF_SLIM);
    1310            0 :           fprintf (dump_file, " = ");
    1311            0 :           op1_range.dump (dump_file);
    1312              :         }
    1313            0 :       fprintf (dump_file, "\n");
    1314            0 :       tracer.print (idx, "Computes ");
    1315            0 :       print_generic_expr (dump_file, op2, TDF_SLIM);
    1316            0 :       fprintf (dump_file, " = ");
    1317            0 :       r.dump (dump_file);
    1318            0 :       fprintf (dump_file, " intersect Known range : ");
    1319            0 :       op2_range.dump (dump_file);
    1320            0 :       fputc ('\n', dump_file);
    1321              :     }
    1322              :   // Intersect the calculated result with the known result and return if done.
    1323     33854782 :   r.intersect (op2_range);
    1324     33854782 :   bool changed = relation_change || r.undefined_p () || r != op2_range;
    1325     33854782 :   if (idx)
    1326            0 :     tracer.trailer (idx, " produces ", changed, op2, r);
    1327              :   return changed;
    1328     36969056 : }
    1329              : 
    1330              : // Calculate a range for NAME from both operand positions of S
    1331              : // assuming the result of the statement is LHS.  Return the range in
    1332              : // R, or false if no range could be calculated.
    1333              : 
    1334              : bool
    1335       549390 : gori_compute::compute_operand1_and_operand2_range (vrange &r,
    1336              :                                                    gimple_range_op_handler
    1337              :                                                                      &handler,
    1338              :                                                    const vrange &lhs,
    1339              :                                                    tree name,
    1340              :                                                    fur_source &src,
    1341              :                                                    value_relation *rel)
    1342              : {
    1343       549390 :   value_range op_range (TREE_TYPE (name));
    1344              : 
    1345       549390 :   value_range vr (TREE_TYPE (handler.operand2 ()));
    1346              :   // Calculate a good a range through op2.
    1347       549390 :   if (!compute_operand2_range (vr, handler, lhs, src, rel))
    1348              :     return false;
    1349       242366 :   gimple *src_stmt = SSA_NAME_DEF_STMT (handler.operand2 ());
    1350       242366 :   gcc_checking_assert (src_stmt);
    1351              :   // Then feed this range back as the LHS of the defining statement.
    1352       242366 :   if (!compute_operand_range (r, src_stmt, vr, name, src, rel))
    1353              :     return false;
    1354              : 
    1355              :   // Now get the range thru op1.
    1356        62538 :   vr.set_range_class (TREE_TYPE (handler.operand1 ()));
    1357        62538 :   if (!compute_operand1_range (vr, handler, lhs, src, rel))
    1358              :     return false;
    1359        54933 :   src_stmt = SSA_NAME_DEF_STMT (handler.operand1 ());
    1360        54933 :   gcc_checking_assert (src_stmt);
    1361              :   // Then feed this range back as the LHS of the defining statement.
    1362        54933 :   if (!compute_operand_range (op_range, src_stmt, vr, name, src, rel))
    1363              :     return false;
    1364              : 
    1365              :   // Both operands have to be simultaneously true, so perform an intersection.
    1366        27200 :   r.intersect (op_range);
    1367        27200 :   return true;
    1368       549390 : }
    1369              : 
    1370              : // Return TRUE if NAME can be recomputed on any edge exiting BB.  If any
    1371              : // direct dependent is exported, it may also change the computed value of NAME.
    1372              : 
    1373              : bool
    1374   1294488140 : gori_compute::may_recompute_p (tree name, basic_block bb, int depth)
    1375              : {
    1376   1294488140 :   tree dep1 = m_map.depend1 (name);
    1377   1294488140 :   tree dep2 = m_map.depend2 (name);
    1378              : 
    1379              :   // If the first dependency is not set, there is no recomputation.
    1380              :   // Dependencies reflect original IL, not current state.   Check if the
    1381              :   // SSA_NAME is still valid as well.
    1382   1294488140 :   if (!dep1)
    1383              :     return false;
    1384              : 
    1385              :   // Only recalculate range-op statements that are recomputable.
    1386   1007881054 :   gimple *s = SSA_NAME_DEF_STMT (name);
    1387   1007881054 :   gimple_range_op_handler handler (s);
    1388   1007881054 :   if (!handler || !handler.recomputable_p ())
    1389              :     return false;
    1390              : 
    1391    587519630 :   if (!dep2)
    1392              :     {
    1393              :       // -1 indicates a default param, convert it to the real default.
    1394    519611999 :       if (depth == -1)
    1395    452167714 :         depth = m_recompute_depth;
    1396              : 
    1397    519611999 :       bool res = m_map.is_export_p (dep1, bb);
    1398    519611999 :       if (res || depth <= 1)
    1399              :         return res;
    1400              :       // Check another level of recomputation.
    1401    468185210 :       return may_recompute_p (dep1, bb, --depth);
    1402              :     }
    1403              :   // Two dependencies terminate the depth of the search.
    1404     67907631 :   return m_map.is_export_p (dep1, bb) || m_map.is_export_p (dep2, bb);
    1405              : }
    1406              : 
    1407              : // Return TRUE if NAME can be recomputed on edge E.  If any direct dependent
    1408              : // is exported on edge E, it may change the computed value of NAME.
    1409              : 
    1410              : bool
    1411     32694969 : gori_compute::may_recompute_p (tree name, edge e, int depth)
    1412              : {
    1413     32694969 :   gcc_checking_assert (e);
    1414     32694969 :   return may_recompute_p (name, e->src, depth);
    1415              : }
    1416              : 
    1417              : 
    1418              : // Return TRUE if a range can be calculated or recomputed for NAME on any
    1419              : // edge exiting BB.
    1420              : 
    1421              : bool
    1422   1224457399 : gori_compute::has_edge_range_p (tree name, basic_block bb)
    1423              : {
    1424              :   // Check if NAME is an export or can be recomputed.
    1425   1224457399 :   if (bb)
    1426    570569025 :     return m_map.is_export_p (name, bb) || may_recompute_p (name, bb);
    1427              : 
    1428              :   // If no block is specified, check for anywhere in the IL.
    1429    653888374 :   return m_map.is_export_p (name) || may_recompute_p (name);
    1430              : }
    1431              : 
    1432              : // Return TRUE if a range can be calculated or recomputed for NAME on edge E.
    1433              : 
    1434              : bool
    1435      1843553 : gori_compute::has_edge_range_p (tree name, edge e)
    1436              : {
    1437      1843553 :   gcc_checking_assert (e);
    1438      1843553 :   return has_edge_range_p (name, e->src);
    1439              : }
    1440              : 
    1441              : // Calculate a range on edge E and return it in R.  Try to evaluate a
    1442              : // range for NAME on this edge.  Return FALSE if this is either not a
    1443              : // control edge or NAME is not defined by this edge.
    1444              : 
    1445              : bool
    1446    193943483 : gori_compute::edge_range_p (vrange &r, edge e, tree name, range_query &q)
    1447              : {
    1448    193943483 :   static bool recomputing = false;
    1449    193943483 :   unsigned idx;
    1450              : 
    1451    193943483 :   if ((e->flags & m_not_executable_flag))
    1452              :     {
    1453        45768 :       r.set_undefined ();
    1454        45768 :       if (dump_file && (dump_flags & TDF_DETAILS))
    1455           24 :           fprintf (dump_file, "Outgoing edge %d->%d unexecutable.\n",
    1456           24 :                    e->src->index, e->dest->index);
    1457              :       return true;
    1458              :     }
    1459              : 
    1460    193897715 :   gcc_checking_assert (gimple_range_ssa_p (name));
    1461    193897715 :   int_range_max lhs;
    1462              :   // Determine if there is an outgoing edge.
    1463    193897715 :   gimple *stmt = gimple_outgoing_range::edge_range_p (lhs, e);
    1464    193897715 :   if (!stmt)
    1465              :     return false;
    1466              : 
    1467    139863423 :   fur_stmt src (stmt, &q);
    1468              :   // If NAME can be calculated on the edge, use that.
    1469    139863423 :   if (m_map.is_export_p (name, e->src))
    1470              :     {
    1471    104365373 :       bool res;
    1472    104365373 :       if ((idx = tracer.header ("outgoing_edge")))
    1473              :         {
    1474            0 :           fprintf (dump_file, " for ");
    1475            0 :           print_generic_expr (dump_file, name, TDF_SLIM);
    1476            0 :           fprintf (dump_file, " on edge %d->%d\n",
    1477            0 :                    e->src->index, e->dest->index);
    1478              :         }
    1479    104365373 :       if ((res = compute_operand_range (r, stmt, lhs, name, src)))
    1480              :         {
    1481              :           // Sometimes compatible types get interchanged. See PR97360.
    1482              :           // Make sure we are returning the type of the thing we asked for.
    1483     56083734 :           if (!r.undefined_p () && r.type () != TREE_TYPE (name))
    1484              :             {
    1485      4609841 :               gcc_checking_assert (range_compatible_p (r.type (),
    1486              :                                                        TREE_TYPE (name)));
    1487      4609841 :               range_cast (r, TREE_TYPE (name));
    1488              :             }
    1489              :         }
    1490    104365373 :       if (idx)
    1491            0 :         tracer.trailer (idx, "outgoing_edge", res, name, r);
    1492              :       return res;
    1493              :     }
    1494              :   // If NAME isn't exported, check if it can be recomputed.
    1495     35498050 :   else if (!recomputing && may_recompute_p (name, e))
    1496              :     {
    1497      6502437 :       recomputing = true;
    1498      6502437 :       gimple *def_stmt = SSA_NAME_DEF_STMT (name);
    1499              : 
    1500      6502437 :       if ((idx = tracer.header ("recomputation")))
    1501              :         {
    1502            0 :           fprintf (dump_file, " attempt on edge %d->%d for ",
    1503            0 :                    e->src->index, e->dest->index);
    1504            0 :           print_gimple_stmt (dump_file, def_stmt, 0, TDF_SLIM);
    1505              :         }
    1506              :       // Simply calculate DEF_STMT on edge E using the range query Q.
    1507      6502437 :       fold_range (r, def_stmt, e, &q);
    1508      6502437 :       if (idx)
    1509            0 :         tracer.trailer (idx, "recomputation", true, name, r);
    1510      6502437 :       recomputing = false;
    1511      6502437 :       return true;
    1512              :     }
    1513              :   return false;
    1514    193897715 : }
    1515              : 
    1516              : // Dump what is known to GORI computes to listing file F.
    1517              : 
    1518              : void
    1519            0 : gori_compute::dump (FILE *f)
    1520              : {
    1521            0 :   m_map.gori_map::dump (f);
    1522            0 : }
    1523              : 
    1524              : // ------------------------------------------------------------------------
    1525              : //  GORI iterator.  Although we have bitmap iterators, don't expose that it
    1526              : //  is currently a bitmap.  Use an export iterator to hide future changes.
    1527              : 
    1528              : // Construct a basic iterator over an export bitmap.
    1529              : 
    1530     80725904 : gori_export_iterator::gori_export_iterator (bitmap b)
    1531              : {
    1532     80725904 :   bm = b;
    1533     80725904 :   if (b)
    1534     80725904 :     bmp_iter_set_init (&bi, b, 1, &y);
    1535     80725904 : }
    1536              : 
    1537              : 
    1538              : // Move to the next export bitmap spot.
    1539              : 
    1540              : void
    1541    170973527 : gori_export_iterator::next ()
    1542              : {
    1543    170973527 :   bmp_iter_next (&bi, &y);
    1544    170973527 : }
    1545              : 
    1546              : 
    1547              : // Fetch the name of the next export in the export list.  Return NULL if
    1548              : // iteration is done.
    1549              : 
    1550              : tree
    1551    251699264 : gori_export_iterator::get_name ()
    1552              : {
    1553    251699264 :   if (!bm)
    1554              :     return NULL_TREE;
    1555              : 
    1556    251699431 :   while (bmp_iter_set (&bi, &y))
    1557              :     {
    1558    171202255 :       tree t = ssa_name (y);
    1559    171202255 :       if (t)
    1560              :         return t;
    1561          167 :       next ();
    1562              :     }
    1563              :   return NULL_TREE;
    1564              : }
    1565              : 
    1566              : // This is a helper class to set up STMT with a known LHS for further GORI
    1567              : // processing.
    1568              : 
    1569         1466 : class gori_stmt_info : public gimple_range_op_handler
    1570              : {
    1571              : public:
    1572              :   gori_stmt_info (vrange &lhs, gimple *stmt, range_query *q);
    1573              :   value_range op1_range;
    1574              :   value_range op2_range;
    1575              :   tree ssa1;
    1576              :   tree ssa2;
    1577              : };
    1578              : 
    1579              : 
    1580              : // Uses query Q to get the known ranges on STMT with a LHS range
    1581              : // for op1_range and op2_range and set ssa1 and ssa2 if either or both of
    1582              : // those operands are SSA_NAMES.
    1583              : 
    1584         1466 : gori_stmt_info::gori_stmt_info (vrange &lhs, gimple *stmt, range_query *q)
    1585         1466 :   : gimple_range_op_handler (stmt)
    1586              : {
    1587         1466 :   ssa1 = NULL;
    1588         1466 :   ssa2 = NULL;
    1589              :   // Don't handle switches as yet for vector processing.
    1590         1466 :   if (is_a<gswitch *> (stmt))
    1591              :     return;
    1592              : 
    1593              :   // No frther processing for VARYING or undefined.
    1594         1466 :   if (lhs.undefined_p () || lhs.varying_p ())
    1595              :     return;
    1596              : 
    1597              :   // If there is no range-op handler, we are also done.
    1598         1466 :   if (!*this)
    1599              :     return;
    1600              : 
    1601              :   // Only evaluate logical cases if both operands must be the same as the LHS.
    1602              :   // Otherwise its becomes exponential in time, as well as more complicated.
    1603         1458 :   if (is_gimple_logical_p (stmt))
    1604              :     {
    1605            0 :       gcc_checking_assert (range_compatible_p (lhs.type (), boolean_type_node));
    1606            0 :       enum tree_code code = gimple_expr_code (stmt);
    1607            0 :       if (code == TRUTH_OR_EXPR ||  code == BIT_IOR_EXPR)
    1608              :         {
    1609              :           // [0, 0] = x || y  means both x and y must be zero.
    1610            0 :           if (!lhs.singleton_p () || !lhs.zero_p ())
    1611              :             return;
    1612              :         }
    1613            0 :       else if (code == TRUTH_AND_EXPR ||  code == BIT_AND_EXPR)
    1614              :         {
    1615              :           // [1, 1] = x && y  means both x and y must be one.
    1616            0 :           if (!lhs.singleton_p () || lhs.zero_p ())
    1617              :             return;
    1618              :         }
    1619              :     }
    1620              : 
    1621         1458 :   tree op1 = operand1 ();
    1622         1458 :   tree op2 = operand2 ();
    1623         1458 :   ssa1 = gimple_range_ssa_p (op1);
    1624         1458 :   ssa2 = gimple_range_ssa_p (op2);
    1625              :   // If both operands are the same, only process one of them.
    1626         1458 :   if (ssa1 && ssa1 == ssa2)
    1627            0 :     ssa2 = NULL_TREE;
    1628              : 
    1629              :   // Extract current ranges for the operands.
    1630         1458 :   fur_stmt src (stmt, q);
    1631         1458 :   if (op1)
    1632              :     {
    1633         1458 :       op1_range.set_range_class (TREE_TYPE (op1));
    1634         1458 :       src.get_operand (op1_range, op1);
    1635              :     }
    1636              : 
    1637              :   // And satisfy the second operand for single op statements.
    1638         1458 :   if (op2)
    1639              :     {
    1640         1444 :       op2_range.set_range_class (TREE_TYPE (op2));
    1641         1444 :       src.get_operand (op2_range, op2);
    1642              :     }
    1643           14 :   else if (op1)
    1644           14 :     op2_range = op1_range;
    1645              :   return;
    1646              : }
    1647              : 
    1648              : 
    1649              : // Process STMT using LHS as the range of the LHS. Invoke GORI processing
    1650              : // to resolve ranges for all SSA_NAMES feeding STMT which may be altered
    1651              : // based on LHS.  Fill R with the results, and resolve all incoming
    1652              : // ranges using range-query Q.
    1653              : 
    1654              : static void
    1655          295 : gori_calc_operands (vrange &lhs, gimple *stmt, ssa_cache &r, range_query *q)
    1656              : {
    1657          295 :   struct gori_stmt_info si(lhs, stmt, q);
    1658          295 :   if (!si)
    1659            5 :     return;
    1660              : 
    1661          290 :   value_range tmp;
    1662              :   // Now evaluate operand ranges, and set them in the edge cache.
    1663              :   // If there was already a range, leave it and do no further evaluation.
    1664          290 :   if (si.ssa1 && !r.has_range (si.ssa1))
    1665              :     {
    1666          224 :       tmp.set_range_class (TREE_TYPE (si.ssa1));
    1667          224 :       if (si.calc_op1 (tmp, lhs, si.op2_range))
    1668          158 :         si.op1_range.intersect (tmp);
    1669          224 :       if (!si.op1_range.varying_p ())
    1670              :         {
    1671          216 :           r.set_range (si.ssa1, si.op1_range);
    1672          216 :           gimple *src = SSA_NAME_DEF_STMT (si.ssa1);
    1673              :           // If definition is in the same basic block, evaluate it.
    1674          216 :           if (src && gimple_bb (src) == gimple_bb (stmt))
    1675          213 :             gori_calc_operands (si.op1_range, src, r, q);
    1676              :         }
    1677              :     }
    1678              : 
    1679          290 :   if (si.ssa2 && !r.has_range (si.ssa2))
    1680              :     {
    1681           71 :       tmp.set_range_class (TREE_TYPE (si.ssa2));
    1682           71 :       if (si.calc_op2 (tmp, lhs, si.op1_range))
    1683            5 :         si.op2_range.intersect (tmp);
    1684           71 :       if (!si.op2_range.varying_p ())
    1685              :         {
    1686           71 :           r.set_range (si.ssa2, si.op2_range);
    1687           71 :           gimple *src = SSA_NAME_DEF_STMT (si.ssa2);
    1688           71 :           if (src && gimple_bb (src) == gimple_bb (stmt))
    1689           71 :             gori_calc_operands (si.op2_range, src, r, q);
    1690              :         }
    1691              :     }
    1692          585 : }
    1693              : 
    1694              : // Use ssa_cache R as a repository for all outgoing ranges on edge E that
    1695              : // can be calculated.  Use Q to establish starting edge ranges and to resolve
    1696              : // operand values.  If Q is NULL use the current range
    1697              : // query available to the system.
    1698              : 
    1699              : bool
    1700           20 : gori_on_edge (ssa_cache &r, edge e, range_query *q)
    1701              : {
    1702           20 :   if (!q)
    1703            0 :     q = get_range_query (cfun);
    1704              :   // Start with an empty vector
    1705           20 :   r.clear ();
    1706           20 :   int_range_max lhs;
    1707              :   // Determine if there is an outgoing edge.
    1708           20 :   gimple *stmt = q->gori ().edge_range_p (lhs, e);
    1709           20 :   if (!stmt)
    1710              :     return false;
    1711           11 :   gori_calc_operands (lhs, stmt, r, q);
    1712           11 :   return true;
    1713           20 : }
    1714              : 
    1715              : // Helper for GORI_NAME_ON_EDGE which uses query Q to determine if STMT
    1716              : // provides a range for NAME, and returns it in R if so. If it does not,
    1717              : // continue processing feeding statements until we run out of statements
    1718              : // or fine a range for NAME.
    1719              : 
    1720              : bool
    1721         1171 : gori_name_helper (vrange &r, tree name, vrange &lhs, gimple *stmt,
    1722              :                   range_query *q, int depth = 0)
    1723              : {
    1724         1171 :   struct gori_stmt_info si(lhs, stmt, q);
    1725         1171 :   if (!si)
    1726              :     return false;
    1727              : 
    1728         1168 :   if (si.ssa1 == name)
    1729            4 :     return si.calc_op1 (r, lhs, si.op2_range);
    1730         1164 :   if (si.ssa2 == name)
    1731            0 :     return si.calc_op2 (r, lhs, si.op1_range);
    1732              : 
    1733              :   // Limit the exponential growth via the logical depth limit.
    1734         1164 :   if (si.ssa1 && si.ssa2)
    1735          573 :     if (++depth >= param_ranger_logical_depth)
    1736              :       return false;
    1737              : 
    1738          876 :   value_range tmp;
    1739              :   // Now evaluate operand ranges, and set them in the edge cache.
    1740              :   // If there was already a range, leave it and do no further evaluation.
    1741          876 :   if (si.ssa1)
    1742              :     {
    1743          875 :       tmp.set_range_class (TREE_TYPE (si.ssa1));
    1744          875 :       if (si.calc_op1 (tmp, lhs, si.op2_range))
    1745          776 :         si.op1_range.intersect (tmp);
    1746          875 :       gimple *src = SSA_NAME_DEF_STMT (si.ssa1);
    1747              :       // If definition is in the same basic block, evaluate it.
    1748          875 :       if (src && gimple_bb (src) == gimple_bb (stmt))
    1749          862 :         if (gori_name_helper (r, name, si.op1_range, src, q, depth))
    1750              :           return true;
    1751              :     }
    1752              : 
    1753          875 :   if (si.ssa2)
    1754              :     {
    1755          285 :       tmp.set_range_class (TREE_TYPE (si.ssa2));
    1756          285 :       if (si.calc_op2 (tmp, lhs, si.op1_range))
    1757          186 :         si.op2_range.intersect (tmp);
    1758          285 :       gimple *src = SSA_NAME_DEF_STMT (si.ssa2);
    1759          285 :       if (src && gimple_bb (src) == gimple_bb (stmt))
    1760          285 :         if (gori_name_helper (r, name, si.op2_range, src, q, depth))
    1761              :           return true;
    1762              :     }
    1763              :   return false;
    1764          876 : }
    1765              : 
    1766              : // Check if NAME has an outgoing range on edge E.  Use query Q to evaluate
    1767              : // the operands.  Return TRUE and the range in R if there is an outgoing range.
    1768              : // This is like gori_on_edge except it only looks for the single name and
    1769              : // does not require an ssa_cache.
    1770              : 
    1771              : bool
    1772           39 : gori_name_on_edge (vrange &r, tree name, edge e, range_query *q)
    1773              : {
    1774           39 :   int_range_max lhs;
    1775           39 :   gimple *stmt = gimple_outgoing_range_stmt_p (e->src);
    1776           39 :   if (!stmt || !is_a<gcond *> (stmt))
    1777              :     return false;
    1778           24 :   gcond_edge_range (lhs, e);
    1779           24 :   return gori_name_helper (r, name, lhs, stmt, q);
    1780           39 : }
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.