LCOV - code coverage report
Current view: top level - gcc - value-relation.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.7 % 769 705
Test Date: 2026-04-20 14:57:17 Functions: 84.9 % 73 62
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Header file for the value range relational processing.
       2              :    Copyright (C) 2020-2026 Free Software Foundation, Inc.
       3              :    Contributed by Andrew MacLeod <amacleod@redhat.com>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              :  for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "ssa.h"
      28              : 
      29              : #include "gimple-range.h"
      30              : #include "tree-pretty-print.h"
      31              : #include "gimple-pretty-print.h"
      32              : #include "alloc-pool.h"
      33              : #include "dominance.h"
      34              : 
      35              : static const char *const kind_string[VREL_LAST] =
      36              : { "varying", "undefined", "<", "<=", ">", ">=", "==", "!=", "pe8", "pe16",
      37              :   "pe32", "pe64" };
      38              : 
      39              : // Print a relation_kind REL to file F.
      40              : 
      41              : void
      42        38682 : print_relation (FILE *f, relation_kind rel)
      43              : {
      44        38682 :   fprintf (f, " %s ", kind_string[rel]);
      45        38682 : }
      46              : 
      47              : // This table is used to negate the operands.  op1 REL op2 -> !(op1 REL op2).
      48              : static const unsigned char rr_negate_table[VREL_LAST] = {
      49              :   VREL_VARYING, VREL_UNDEFINED, VREL_GE, VREL_GT, VREL_LE, VREL_LT, VREL_NE,
      50              :   VREL_EQ };
      51              : 
      52              : // Negate the relation, as in logical negation.
      53              : 
      54              : relation_kind
      55            0 : relation_negate (relation_kind r)
      56              : {
      57            0 :   return relation_kind (rr_negate_table [r]);
      58              : }
      59              : 
      60              : // This table is used to swap the operands.  op1 REL op2 -> op2 REL op1.
      61              : static const unsigned char rr_swap_table[VREL_LAST] = {
      62              :   VREL_VARYING, VREL_UNDEFINED, VREL_GT, VREL_GE, VREL_LT, VREL_LE, VREL_EQ,
      63              :   VREL_NE };
      64              : 
      65              : // Return the relation as if the operands were swapped.
      66              : 
      67              : relation_kind
      68     19261603 : relation_swap (relation_kind r)
      69              : {
      70     19261603 :   return relation_kind (rr_swap_table [r]);
      71              : }
      72              : 
      73              : // This table is used to perform an intersection between 2 relations.
      74              : 
      75              : static const unsigned char rr_intersect_table[VREL_LAST][VREL_LAST] = {
      76              : // VREL_VARYING
      77              :   { VREL_VARYING, VREL_UNDEFINED, VREL_LT, VREL_LE, VREL_GT, VREL_GE, VREL_EQ,
      78              :     VREL_NE },
      79              : // VREL_UNDEFINED
      80              :   { VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED,
      81              :     VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED },
      82              : // VREL_LT
      83              :   { VREL_LT, VREL_UNDEFINED, VREL_LT, VREL_LT, VREL_UNDEFINED, VREL_UNDEFINED,
      84              :     VREL_UNDEFINED, VREL_LT },
      85              : // VREL_LE
      86              :   { VREL_LE, VREL_UNDEFINED, VREL_LT, VREL_LE, VREL_UNDEFINED, VREL_EQ,
      87              :     VREL_EQ, VREL_LT },
      88              : // VREL_GT
      89              :   { VREL_GT, VREL_UNDEFINED, VREL_UNDEFINED, VREL_UNDEFINED, VREL_GT, VREL_GT,
      90              :     VREL_UNDEFINED, VREL_GT },
      91              : // VREL_GE
      92              :   { VREL_GE, VREL_UNDEFINED, VREL_UNDEFINED, VREL_EQ, VREL_GT, VREL_GE,
      93              :     VREL_EQ, VREL_GT },
      94              : // VREL_EQ
      95              :   { VREL_EQ, VREL_UNDEFINED, VREL_UNDEFINED, VREL_EQ, VREL_UNDEFINED, VREL_EQ,
      96              :     VREL_EQ, VREL_UNDEFINED },
      97              : // VREL_NE
      98              :   { VREL_NE, VREL_UNDEFINED, VREL_LT, VREL_LT, VREL_GT, VREL_GT,
      99              :     VREL_UNDEFINED, VREL_NE } };
     100              : 
     101              : 
     102              : // Intersect relation R1 with relation R2 and return the resulting relation.
     103              : 
     104              : relation_kind
     105     95605579 : relation_intersect (relation_kind r1, relation_kind r2)
     106              : {
     107     95605579 :   return relation_kind (rr_intersect_table[r1][r2]);
     108              : }
     109              : 
     110              : 
     111              : // This table is used to perform a union between 2 relations.
     112              : 
     113              : static const unsigned char rr_union_table[VREL_LAST][VREL_LAST] = {
     114              : // VREL_VARYING
     115              :   { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
     116              :     VREL_VARYING, VREL_VARYING, VREL_VARYING },
     117              : // VREL_UNDEFINED
     118              :   { VREL_VARYING, VREL_UNDEFINED, VREL_LT, VREL_LE, VREL_GT, VREL_GE,
     119              :     VREL_EQ, VREL_NE },
     120              : // VREL_LT
     121              :   { VREL_VARYING, VREL_LT, VREL_LT, VREL_LE, VREL_NE, VREL_VARYING, VREL_LE,
     122              :     VREL_NE },
     123              : // VREL_LE
     124              :   { VREL_VARYING, VREL_LE, VREL_LE, VREL_LE, VREL_VARYING, VREL_VARYING,
     125              :     VREL_LE, VREL_VARYING },
     126              : // VREL_GT
     127              :   { VREL_VARYING, VREL_GT, VREL_NE, VREL_VARYING, VREL_GT, VREL_GE, VREL_GE,
     128              :     VREL_NE },
     129              : // VREL_GE
     130              :   { VREL_VARYING, VREL_GE, VREL_VARYING, VREL_VARYING, VREL_GE, VREL_GE,
     131              :     VREL_GE, VREL_VARYING },
     132              : // VREL_EQ
     133              :   { VREL_VARYING, VREL_EQ, VREL_LE, VREL_LE, VREL_GE, VREL_GE, VREL_EQ,
     134              :     VREL_VARYING },
     135              : // VREL_NE
     136              :   { VREL_VARYING, VREL_NE, VREL_NE, VREL_VARYING, VREL_NE, VREL_VARYING,
     137              :     VREL_VARYING, VREL_NE } };
     138              : 
     139              : // Union relation R1 with relation R2 and return the result.
     140              : 
     141              : relation_kind
     142     87013221 : relation_union (relation_kind r1, relation_kind r2)
     143              : {
     144     87013221 :   return relation_kind (rr_union_table[r1][r2]);
     145              : }
     146              : 
     147              : 
     148              : // This table is used to determine transitivity between 2 relations.
     149              : // (A relation0 B) and (B relation1 C) implies  (A result C)
     150              : 
     151              : static const unsigned char rr_transitive_table[VREL_LAST][VREL_LAST] = {
     152              : // VREL_VARYING
     153              :   { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
     154              :     VREL_VARYING, VREL_VARYING, VREL_VARYING },
     155              : // VREL_UNDEFINED
     156              :   { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
     157              :     VREL_VARYING, VREL_VARYING, VREL_VARYING },
     158              : // VREL_LT
     159              :   { VREL_VARYING, VREL_VARYING, VREL_LT, VREL_LT, VREL_VARYING, VREL_VARYING,
     160              :     VREL_LT, VREL_VARYING },
     161              : // VREL_LE
     162              :   { VREL_VARYING, VREL_VARYING, VREL_LT, VREL_LE, VREL_VARYING, VREL_VARYING,
     163              :     VREL_LE, VREL_VARYING },
     164              : // VREL_GT
     165              :   { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_GT, VREL_GT,
     166              :     VREL_GT, VREL_VARYING },
     167              : // VREL_GE
     168              :   { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_GT, VREL_GE,
     169              :     VREL_GE, VREL_VARYING },
     170              : // VREL_EQ
     171              :   { VREL_VARYING, VREL_VARYING, VREL_LT, VREL_LE, VREL_GT, VREL_GE, VREL_EQ,
     172              :     VREL_VARYING },
     173              : // VREL_NE
     174              :   { VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING, VREL_VARYING,
     175              :     VREL_VARYING, VREL_VARYING, VREL_VARYING } };
     176              : 
     177              : // Apply transitive operation between relation R1 and relation R2, and
     178              : // return the resulting relation, if any.
     179              : 
     180              : relation_kind
     181      9006048 : relation_transitive (relation_kind r1, relation_kind r2)
     182              : {
     183      9006048 :   return relation_kind (rr_transitive_table[r1][r2]);
     184              : }
     185              : 
     186              : // When one name is an equivalence of another, ensure the equivalence
     187              : // range is correct.  Specifically for floating point, a +0 is also
     188              : // equivalent to a -0 which may not be reflected.  See PR 111694.
     189              : 
     190              : void
     191      1728880 : adjust_equivalence_range (vrange &range)
     192              : {
     193      1728880 :   if (range.undefined_p () || !is_a<frange> (range))
     194      1695143 :     return;
     195              : 
     196        33737 :   frange fr = as_a<frange> (range);
     197              :   // If range includes 0 make sure both signs of zero are included.
     198        33737 :   if (fr.contains_p (dconst0) || fr.contains_p (dconstm0))
     199              :     {
     200        17490 :       frange zeros (range.type (), dconstm0, dconst0);
     201        17490 :       range.union_ (zeros);
     202        17490 :     }
     203        33737 :  }
     204              : 
     205              : // Given an equivalence set EQUIV, set all the bits in B that are still valid
     206              : // members of EQUIV in basic block BB.
     207              : 
     208              : void
     209     20540579 : relation_oracle::valid_equivs (bitmap b, const_bitmap equivs, basic_block bb)
     210              : {
     211     20540579 :   unsigned i;
     212     20540579 :   bitmap_iterator bi;
     213     42792824 :   EXECUTE_IF_SET_IN_BITMAP (equivs, 0, i, bi)
     214              :     {
     215     22252245 :       tree ssa = ssa_name (i);
     216     44504489 :       if (ssa && !SSA_NAME_IN_FREE_LIST (ssa))
     217              :         {
     218     22252244 :           const_bitmap ssa_equiv = equiv_set (ssa, bb);
     219     22252244 :           if (ssa_equiv == equivs)
     220     21960643 :             bitmap_set_bit (b, i);
     221              :         }
     222              :     }
     223     20540579 : }
     224              : 
     225              : // Return any known relation between SSA1 and SSA2 before stmt S is executed.
     226              : // If GET_RANGE is true, query the range of both operands first to ensure
     227              : // the definitions have been processed and any relations have be created.
     228              : 
     229              : relation_kind
     230    112532180 : relation_oracle::query (gimple *s, tree ssa1, tree ssa2)
     231              : {
     232    112532180 :   if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
     233              :     return VREL_VARYING;
     234     41232583 :   return query (gimple_bb (s), ssa1, ssa2);
     235              : }
     236              : 
     237              : // Return any known relation between SSA1 and SSA2 on edge E.
     238              : // If GET_RANGE is true, query the range of both operands first to ensure
     239              : // the definitions have been processed and any relations have be created.
     240              : 
     241              : relation_kind
     242     54908543 : relation_oracle::query (edge e, tree ssa1, tree ssa2)
     243              : {
     244     54908543 :   basic_block bb;
     245     54908543 :   if (TREE_CODE (ssa1) != SSA_NAME || TREE_CODE (ssa2) != SSA_NAME)
     246              :     return VREL_VARYING;
     247              : 
     248              :   // Use destination block if it has a single predecessor, and this picks
     249              :   // up any relation on the edge.
     250              :   // Otherwise choose the src edge and the result is the same as on-exit.
     251     40736490 :   if (!single_pred_p (e->dest))
     252     38909257 :     bb = e->src;
     253              :   else
     254              :     bb = e->dest;
     255              : 
     256     40736490 :   return query (bb, ssa1, ssa2);
     257              : }
     258              : // -------------------------------------------------------------------------
     259              : 
     260              : // The very first element in the m_equiv chain is actually just a summary
     261              : // element in which the m_names bitmap is used to indicate that an ssa_name
     262              : // has an equivalence set in this block.
     263              : // This allows for much faster traversal of the DOM chain, as a search for
     264              : // SSA_NAME simply requires walking the DOM chain until a block is found
     265              : // which has the bit for SSA_NAME set. Then scan for the equivalency set in
     266              : // that block.   No previous lists need be searched.
     267              : 
     268              : // If SSA has an equivalence in this list, find and return it.
     269              : // Otherwise return NULL.
     270              : 
     271              : equiv_chain *
     272    176510779 : equiv_chain::find (unsigned ssa)
     273              : {
     274    176510779 :   equiv_chain *ptr = NULL;
     275              :   // If there are equiv sets and SSA is in one in this list, find it.
     276              :   // Otherwise return NULL.
     277    176510779 :   if (bitmap_bit_p (m_names, ssa))
     278              :     {
     279    216836547 :       for (ptr = m_next; ptr; ptr = ptr->m_next)
     280    216836547 :         if (bitmap_bit_p (ptr->m_names, ssa))
     281              :           break;
     282              :     }
     283    176510779 :   return ptr;
     284              : }
     285              : 
     286              : // Dump the names in this equivalence set.
     287              : 
     288              : void
     289           11 : equiv_chain::dump (FILE *f) const
     290              : {
     291           11 :   bitmap_iterator bi;
     292           11 :   unsigned i;
     293              : 
     294           11 :   if (!m_names || bitmap_empty_p (m_names))
     295            1 :     return;
     296           10 :   fprintf (f, "Equivalence set : [");
     297           10 :   unsigned c = 0;
     298           26 :   EXECUTE_IF_SET_IN_BITMAP (m_names, 0, i, bi)
     299              :     {
     300           16 :       if (ssa_name (i))
     301              :         {
     302           16 :           if (c++)
     303            6 :             fprintf (f, ", ");
     304           16 :           print_generic_expr (f, ssa_name (i), TDF_SLIM);
     305              :         }
     306              :     }
     307           10 :   fprintf (f, "]\n");
     308              : }
     309              : 
     310              : // Instantiate an equivalency oracle.
     311              : 
     312     25575492 : equiv_oracle::equiv_oracle ()
     313              : {
     314     25575492 :   bitmap_obstack_initialize (&m_bitmaps);
     315     25575492 :   m_equiv.create (0);
     316     25575492 :   m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
     317     25575492 :   m_equiv_set = BITMAP_ALLOC (&m_bitmaps);
     318     25575492 :   bitmap_tree_view (m_equiv_set);
     319     25575492 :   obstack_init (&m_chain_obstack);
     320     25575492 :   m_self_equiv.create (0);
     321     51150984 :   m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
     322     25575492 :   m_partial.create (0);
     323     51150984 :   m_partial.safe_grow_cleared (num_ssa_names + 1);
     324              :   // Create a bitmap to avoid registering multiple equivalences from a LHS.
     325              :   // See PR 124809.
     326     25575492 :   m_lhs_equiv_set_p = BITMAP_ALLOC (&m_bitmaps);
     327     25575492 :   bitmap_tree_view (m_lhs_equiv_set_p);
     328     25575492 : }
     329              : 
     330              : // Destruct an equivalency oracle.
     331              : 
     332     25575492 : equiv_oracle::~equiv_oracle ()
     333              : {
     334     25575492 :   m_partial.release ();
     335     25575492 :   m_self_equiv.release ();
     336     25575492 :   obstack_free (&m_chain_obstack, NULL);
     337     25575492 :   m_equiv.release ();
     338     25575492 :   bitmap_obstack_release (&m_bitmaps);
     339     25575492 : }
     340              : 
     341              : // Add a partial equivalence R between OP1 and OP2.  Return false if no
     342              : // new relation is added.
     343              : 
     344              : bool
     345      9748491 : equiv_oracle::add_partial_equiv (relation_kind r, tree op1, tree op2)
     346              : {
     347      9748491 :   int v1 = SSA_NAME_VERSION (op1);
     348      9748491 :   int v2 = SSA_NAME_VERSION (op2);
     349      9748491 :   int prec2 = TYPE_PRECISION (TREE_TYPE (op2));
     350      9748491 :   int bits = pe_to_bits (r);
     351      9748491 :   gcc_checking_assert (bits && prec2 >= bits);
     352              : 
     353     19496982 :   if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
     354          310 :     m_partial.safe_grow_cleared (num_ssa_names + 1);
     355     19496982 :   gcc_checking_assert (v1 < (int)m_partial.length ()
     356              :                        && v2 < (int)m_partial.length ());
     357              : 
     358      9748491 :   pe_slice &pe1 = m_partial[v1];
     359      9748491 :   pe_slice &pe2 = m_partial[v2];
     360              : 
     361      9748491 :   if (pe1.members)
     362              :     {
     363              :       // If the definition pe1 already has an entry, either the stmt is
     364              :       // being re-evaluated, or the def was used before being registered.
     365              :       // In either case, if PE2 has an entry, we simply do nothing.
     366          292 :       if (pe2.members)
     367              :         return false;
     368              :       // If there are no uses of op2, do not register.
     369          191 :       if (has_zero_uses (op2))
     370              :         return false;
     371              :       // PE1 is the LHS and already has members, so everything in the set
     372              :       // should be a slice of PE2 rather than PE1.
     373          191 :       pe2.code = pe_min (r, pe1.code);
     374          191 :       pe2.ssa_base = op2;
     375          191 :       pe2.members = pe1.members;
     376          191 :       bitmap_iterator bi;
     377          191 :       unsigned x;
     378          573 :       EXECUTE_IF_SET_IN_BITMAP (pe1.members, 0, x, bi)
     379              :         {
     380          382 :           m_partial[x].ssa_base = op2;
     381          382 :           m_partial[x].code = pe_min (m_partial[x].code, pe2.code);
     382              :         }
     383          191 :       bitmap_set_bit (pe1.members, v2);
     384          191 :       return true;
     385              :     }
     386      9748199 :   if (pe2.members)
     387              :     {
     388              :       // If there are no uses of op1, do not register.
     389       700586 :       if (has_zero_uses (op1))
     390              :         return false;
     391       694111 :       pe1.ssa_base = pe2.ssa_base;
     392              :       // If pe2 is a 16 bit value, but only an 8 bit copy, we can't be any
     393              :       // more than an 8 bit equivalence here, so choose MIN value.
     394       694111 :       pe1.code = pe_min (r, pe2.code);
     395       694111 :       pe1.members = pe2.members;
     396       694111 :       bitmap_set_bit (pe1.members, v1);
     397              :     }
     398              :   else
     399              :     {
     400              :       // If there are no uses of either operand, do not register.
     401      9047613 :       if (has_zero_uses (op1) || has_zero_uses (op2))
     402              :         return false;
     403              :       // Neither name has an entry, simply create op1 as slice of op2.
     404      8941935 :       pe2.code = bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2)));
     405      8941935 :       if (pe2.code == VREL_VARYING)
     406              :         return false;
     407      8915040 :       pe2.ssa_base = op2;
     408      8915040 :       pe2.members = BITMAP_ALLOC (&m_bitmaps);
     409      8915040 :       bitmap_set_bit (pe2.members, v2);
     410      8915040 :       pe1.ssa_base = op2;
     411      8915040 :       pe1.code = r;
     412      8915040 :       pe1.members = pe2.members;
     413      8915040 :       bitmap_set_bit (pe1.members, v1);
     414              :     }
     415              :   return true;
     416              : }
     417              : 
     418              : // Return the set of partial equivalences associated with NAME.  The bitmap
     419              : // will be NULL if there are none.
     420              : 
     421              : const pe_slice *
     422     57788351 : equiv_oracle::partial_equiv_set (tree name)
     423              : {
     424     57788351 :   int v = SSA_NAME_VERSION (name);
     425    115576702 :   if (v >= (int)m_partial.length ())
     426              :     return NULL;
     427     57788351 :   return &m_partial[v];
     428              : }
     429              : 
     430              : // Query if there is a partial equivalence between SSA1 and SSA2.  Return
     431              : // VREL_VARYING if there is not one.  If BASE is non-null, return the base
     432              : // ssa-name this is a slice of.
     433              : 
     434              : relation_kind
     435     58037820 : equiv_oracle::partial_equiv (tree ssa1, tree ssa2, tree *base) const
     436              : {
     437     58037820 :   int v1 = SSA_NAME_VERSION (ssa1);
     438     58037820 :   int v2 = SSA_NAME_VERSION (ssa2);
     439              : 
     440    116075640 :   if (v1 >= (int)m_partial.length () || v2 >= (int)m_partial.length ())
     441              :     return VREL_VARYING;
     442              : 
     443     58037753 :   const pe_slice &pe1 = m_partial[v1];
     444     58037753 :   const pe_slice &pe2 = m_partial[v2];
     445     58037753 :   if (pe1.members && pe2.members == pe1.members)
     446              :     {
     447          887 :       if (base)
     448            0 :         *base = pe1.ssa_base;
     449          887 :       return pe_min (pe1.code, pe2.code);
     450              :     }
     451              :   return VREL_VARYING;
     452              : }
     453              : 
     454              : 
     455              : // Find and return the equivalency set for SSA along the dominators of BB.
     456              : // This is the external API.
     457              : 
     458              : const_bitmap
     459    146860209 : equiv_oracle::equiv_set (tree ssa, basic_block bb)
     460              : {
     461              :   // Search the dominator tree for an equivalency.
     462    146860209 :   equiv_chain *equiv = find_equiv_dom (ssa, bb);
     463    146860209 :   if (equiv)
     464     17505387 :     return equiv->m_names;
     465              : 
     466              :   // Otherwise return a cached equiv set containing just this SSA.
     467    129354822 :   unsigned v = SSA_NAME_VERSION (ssa);
     468    129354822 :   if (v >= m_self_equiv.length ())
     469           74 :     m_self_equiv.safe_grow_cleared (num_ssa_names + 1);
     470              : 
     471    129354822 :   if (!m_self_equiv[v])
     472              :     {
     473     35125089 :       m_self_equiv[v] = BITMAP_ALLOC (&m_bitmaps);
     474     35125089 :       bitmap_set_bit (m_self_equiv[v], v);
     475              :     }
     476    129354822 :   return m_self_equiv[v];
     477              : }
     478              : 
     479              : // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
     480              : 
     481              : relation_kind
     482            0 : equiv_oracle::query (basic_block bb, tree ssa1, tree ssa2)
     483              : {
     484              :   // If the 2 ssa names share the same equiv set, they are equal.
     485            0 :   if (equiv_set (ssa1, bb) == equiv_set (ssa2, bb))
     486              :     return VREL_EQ;
     487              : 
     488              :   // Check if there is a partial equivalence.
     489            0 :   return partial_equiv (ssa1, ssa2);
     490              : }
     491              : 
     492              : // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
     493              : 
     494              : relation_kind
     495            0 : equiv_oracle::query (basic_block bb ATTRIBUTE_UNUSED, const_bitmap e1,
     496              :                      const_bitmap e2)
     497              : {
     498              :   // If the 2 ssa names share the same equiv set, they are equal.
     499            0 :   if (bitmap_equal_p (e1, e2))
     500            0 :     return VREL_EQ;
     501              :   return VREL_VARYING;
     502              : }
     503              : 
     504              : // If SSA has an equivalence in block BB, find and return it.
     505              : // Otherwise return NULL.
     506              : 
     507              : equiv_chain *
     508    106698872 : equiv_oracle::find_equiv_block (unsigned ssa, int bb) const
     509              : {
     510    213397744 :   if (bb >= (int)m_equiv.length () || !m_equiv[bb])
     511              :     return NULL;
     512              : 
     513     46348003 :   return m_equiv[bb]->find (ssa);
     514              : }
     515              : 
     516              : // Starting at block BB, walk the dominator chain looking for the nearest
     517              : // equivalence set containing NAME.
     518              : 
     519              : equiv_chain *
     520    161692041 : equiv_oracle::find_equiv_dom (tree name, basic_block bb) const
     521              : {
     522    161692041 :   unsigned v = SSA_NAME_VERSION (name);
     523              :   // Short circuit looking for names which have no equivalences.
     524              :   // Saves time looking for something which does not exist.
     525    161692041 :   if (!bitmap_bit_p (m_equiv_set, v))
     526              :     return NULL;
     527              : 
     528              :   // NAME has at least once equivalence set, check to see if it has one along
     529              :   // the dominator tree.
     530    107797325 :   for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
     531              :     {
     532    106698872 :       equiv_chain *ptr = find_equiv_block (v, bb->index);
     533    106698872 :       if (ptr)
     534              :         return ptr;
     535              :     }
     536              :   return NULL;
     537              : }
     538              : 
     539              : // Register equivalence between ssa_name V and set EQUIV in block BB,
     540              : 
     541              : bitmap
     542        75294 : equiv_oracle::register_equiv (basic_block bb, unsigned v, equiv_chain *equiv)
     543              : {
     544              :   // V will have an equivalency now.
     545        75294 :   bitmap_set_bit (m_equiv_set, v);
     546              : 
     547              :   // If that equiv chain is in this block, simply use it.
     548        75294 :   if (equiv->m_bb == bb)
     549              :     {
     550        15806 :       bitmap_set_bit (equiv->m_names, v);
     551        15806 :       bitmap_set_bit (m_equiv[bb->index]->m_names, v);
     552        15806 :       return NULL;
     553              :     }
     554              : 
     555              :   // Otherwise create an equivalence for this block which is a copy
     556              :   // of equiv, the add V to the set.
     557        59488 :   bitmap b = BITMAP_ALLOC (&m_bitmaps);
     558        59488 :   valid_equivs (b, equiv->m_names, bb);
     559        59488 :   bitmap_set_bit (b, v);
     560        59488 :   return b;
     561              : }
     562              : 
     563              : // Register equivalence between set equiv_1 and equiv_2 in block BB.
     564              : // Return NULL if either name can be merged with the other.  Otherwise
     565              : // return a pointer to the combined bitmap of names.  This allows the
     566              : // caller to do any setup required for a new element.
     567              : 
     568              : bitmap
     569      3824695 : equiv_oracle::register_equiv (basic_block bb, equiv_chain *equiv_1,
     570              :                               equiv_chain *equiv_2)
     571              : {
     572              :   // If equiv_1 is already in BB, use it as the combined set.
     573      3824695 :   if (equiv_1->m_bb == bb)
     574              :     {
     575      2108754 :       valid_equivs (equiv_1->m_names, equiv_2->m_names, bb);
     576              :       // Its hard to delete from a single linked list, so
     577              :       // just clear the second one.
     578      2108754 :       if (equiv_2->m_bb == bb)
     579       317417 :         bitmap_clear (equiv_2->m_names);
     580              :       else
     581              :         // Ensure the new names are in the summary for BB.
     582      1791337 :         bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_1->m_names);
     583      2108754 :       return NULL;
     584              :     }
     585              :   // If equiv_2 is in BB, use it for the combined set.
     586      1715941 :   if (equiv_2->m_bb == bb)
     587              :     {
     588         2613 :       valid_equivs (equiv_2->m_names, equiv_1->m_names, bb);
     589              :       // Ensure the new names are in the summary.
     590         2613 :       bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_2->m_names);
     591         2613 :       return NULL;
     592              :     }
     593              : 
     594              :   // At this point, neither equivalence is from this block.
     595      1713328 :   bitmap b = BITMAP_ALLOC (&m_bitmaps);
     596      1713328 :   valid_equivs (b, equiv_1->m_names, bb);
     597      1713328 :   valid_equivs (b, equiv_2->m_names, bb);
     598      1713328 :   return b;
     599              : }
     600              : 
     601              : // Create an equivalency set containing only SSA in its definition block.
     602              : // This is done the first time SSA is registered in an equivalency and blocks
     603              : // any DOM searches past the definition.
     604              : 
     605              : void
     606      7092386 : equiv_oracle::register_initial_def (tree ssa)
     607              : {
     608      7092386 :   if (SSA_NAME_IS_DEFAULT_DEF (ssa))
     609              :     return;
     610      7009179 :   basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (ssa));
     611              : 
     612              :   // If defining stmt is not in the IL, simply return.
     613      7009179 :   if (!bb)
     614              :     return;
     615      7009178 :   gcc_checking_assert (!find_equiv_dom (ssa, bb));
     616              : 
     617      7009178 :   unsigned v = SSA_NAME_VERSION (ssa);
     618      7009178 :   bitmap_set_bit (m_equiv_set, v);
     619      7009178 :   bitmap equiv_set = BITMAP_ALLOC (&m_bitmaps);
     620      7009178 :   bitmap_set_bit (equiv_set, v);
     621      7009178 :   add_equiv_to_block (bb, equiv_set);
     622              : }
     623              : 
     624              : // Register an equivalence between SSA1 and SSA2 in block BB.
     625              : // The equivalence oracle maintains a vector of equivalencies indexed by basic
     626              : // block. When an equivalence between SSA1 and SSA2 is registered in block BB,
     627              : // a query is made as to what equivalences both names have already, and
     628              : // any preexisting equivalences are merged to create a single equivalence
     629              : // containing all the ssa_names in this basic block.
     630              : // Return false if no new relation is added.
     631              : 
     632              : bool
     633     13659818 : equiv_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
     634              : {
     635              :   // Process partial equivalencies.
     636     13659818 :   if (relation_partial_equiv_p (k))
     637      9748491 :     return add_partial_equiv (k, ssa1, ssa2);
     638              : 
     639              :   // Only handle equality relations.
     640      3911327 :   if (k != VREL_EQ)
     641              :     return false;
     642              : 
     643      3911327 :   unsigned v1 = SSA_NAME_VERSION (ssa1);
     644      3911327 :   unsigned v2 = SSA_NAME_VERSION (ssa2);
     645              : 
     646              :   // If this is the first time an ssa_name has an equivalency registered
     647              :   // create a self-equivalency record in the def block.
     648      3911327 :   if (!bitmap_bit_p (m_equiv_set, v1))
     649      3761288 :     register_initial_def (ssa1);
     650      3911327 :   if (!bitmap_bit_p (m_equiv_set, v2))
     651      3331098 :     register_initial_def (ssa2);
     652              : 
     653      3911327 :   equiv_chain *equiv_1 = find_equiv_dom (ssa1, bb);
     654      3911327 :   equiv_chain *equiv_2 = find_equiv_dom (ssa2, bb);
     655              : 
     656              :   // Check if they are the same set
     657      3911327 :   if (equiv_1 && equiv_1 == equiv_2)
     658              :     return false;
     659              : 
     660      3909592 :   bitmap equiv_set;
     661              : 
     662              :   // Case where we have 2 SSA_NAMEs that are not in any set.
     663      3909592 :   if (!equiv_1 && !equiv_2)
     664              :     {
     665         9603 :       bitmap_set_bit (m_equiv_set, v1);
     666         9603 :       bitmap_set_bit (m_equiv_set, v2);
     667              : 
     668         9603 :       equiv_set = BITMAP_ALLOC (&m_bitmaps);
     669         9603 :       bitmap_set_bit (equiv_set, v1);
     670         9603 :       bitmap_set_bit (equiv_set, v2);
     671              :     }
     672      3899989 :   else if (!equiv_1 && equiv_2)
     673        22750 :     equiv_set = register_equiv (bb, v1, equiv_2);
     674      3877239 :   else if (equiv_1 && !equiv_2)
     675        52544 :     equiv_set = register_equiv (bb, v2, equiv_1);
     676              :   else
     677      3824695 :     equiv_set = register_equiv (bb, equiv_1, equiv_2);
     678              : 
     679              :   // A non-null return is a bitmap that is to be added to the current
     680              :   // block as a new equivalence.
     681      3909592 :   if (!equiv_set)
     682              :     return false;
     683              : 
     684      1782419 :   add_equiv_to_block (bb, equiv_set);
     685      1782419 :   return true;
     686              : }
     687              : 
     688              : // Add an equivalency record in block BB containing bitmap EQUIV_SET.
     689              : // Note the internal caller is responsible for allocating EQUIV_SET properly.
     690              : 
     691              : void
     692      8791597 : equiv_oracle::add_equiv_to_block (basic_block bb, bitmap equiv_set)
     693              : {
     694      8791597 :   equiv_chain *ptr;
     695              : 
     696              :   // Check if this is the first time a block has an equivalence added.
     697              :   // and create a header block. And set the summary for this block.
     698      8791597 :   limit_check (bb);
     699      8791597 :   if (!m_equiv[bb->index])
     700              :     {
     701      5646982 :       ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
     702              :                                            sizeof (equiv_chain));
     703      5646982 :       ptr->m_names = BITMAP_ALLOC (&m_bitmaps);
     704      5646982 :       bitmap_copy (ptr->m_names, equiv_set);
     705      5646982 :       ptr->m_bb = bb;
     706      5646982 :       ptr->m_next = NULL;
     707      5646982 :       m_equiv[bb->index] = ptr;
     708              :     }
     709              : 
     710              :   // Now create the element for this equiv set and initialize it.
     711      8791597 :   ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack, sizeof (equiv_chain));
     712      8791597 :   ptr->m_names = equiv_set;
     713      8791597 :   ptr->m_bb = bb;
     714     17583194 :   gcc_checking_assert (bb->index < (int)m_equiv.length ());
     715      8791597 :   ptr->m_next = m_equiv[bb->index]->m_next;
     716      8791597 :   m_equiv[bb->index]->m_next = ptr;
     717      8791597 :   bitmap_ior_into (m_equiv[bb->index]->m_names, equiv_set);
     718      8791597 : }
     719              : 
     720              : // Make sure the BB vector is big enough and grow it if needed.
     721              : 
     722              : void
     723      8791597 : equiv_oracle::limit_check (basic_block bb)
     724              : {
     725      8791597 :   int i = (bb) ? bb->index : last_basic_block_for_fn (cfun);
     726     17583194 :   if (i >= (int)m_equiv.length ())
     727           43 :     m_equiv.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
     728      8791597 : }
     729              : 
     730              : // Dump the equivalence sets in BB to file F.
     731              : 
     732              : void
     733          248 : equiv_oracle::dump (FILE *f, basic_block bb) const
     734              : {
     735          496 :   if (bb->index >= (int)m_equiv.length ())
     736              :     return;
     737              :   // Process equivalences.
     738          248 :   if (m_equiv[bb->index])
     739              :     {
     740            9 :       equiv_chain *ptr = m_equiv[bb->index]->m_next;
     741           20 :       for (; ptr; ptr = ptr->m_next)
     742           11 :         ptr->dump (f);
     743              :     }
     744              :   // Look for partial equivalences defined in this block..
     745        12841 :   for (unsigned i = 0; i < num_ssa_names; i++)
     746              :     {
     747        12593 :       tree name = ssa_name (i);
     748        17805 :       if (!gimple_range_ssa_p (name) || !SSA_NAME_DEF_STMT (name))
     749         7381 :         continue;
     750         5212 :       if (i >= m_partial.length ())
     751              :         break;
     752         5212 :      tree base = m_partial[i].ssa_base;
     753         5212 :       if (base && name != base && gimple_bb (SSA_NAME_DEF_STMT (name)) == bb)
     754              :         {
     755           40 :           relation_kind k = partial_equiv (name, base);
     756           40 :           if (k != VREL_VARYING)
     757              :             {
     758           40 :               value_relation vr (k, name, base);
     759           40 :               fprintf (f, "Partial equiv ");
     760           40 :               vr.dump (f);
     761           40 :               fputc ('\n',f);
     762              :             }
     763              :         }
     764              :     }
     765              : }
     766              : 
     767              : // Dump all equivalence sets known to the oracle.
     768              : 
     769              : void
     770            0 : equiv_oracle::dump (FILE *f) const
     771              : {
     772            0 :   fprintf (f, "Equivalency dump\n");
     773            0 :   for (unsigned i = 0; i < m_equiv.length (); i++)
     774            0 :     if (m_equiv[i] && BASIC_BLOCK_FOR_FN (cfun, i))
     775              :       {
     776            0 :         fprintf (f, "BB%d\n", i);
     777            0 :         dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
     778              :       }
     779            0 : }
     780              : 
     781              : 
     782              : // --------------------------------------------------------------------------
     783              : 
     784              : // Adjust the relation by Swapping the operands and relation.
     785              : 
     786              : void
     787            0 : value_relation::swap ()
     788              : {
     789            0 :   related = relation_swap (related);
     790            0 :   tree tmp = name1;
     791            0 :   name1 = name2;
     792            0 :   name2 = tmp;
     793            0 : }
     794              : 
     795              : // Perform an intersection between 2 relations. *this &&= p.
     796              : // Return false if the relations cannot be intersected.
     797              : 
     798              : bool
     799      4513478 : value_relation::intersect (value_relation &p)
     800              : {
     801              :   // Save previous value
     802      4513478 :   relation_kind old = related;
     803              : 
     804      4513478 :   if (p.op1 () == op1 () && p.op2 () == op2 ())
     805      4513208 :     related = relation_intersect (kind (), p.kind ());
     806          270 :   else if (p.op2 () == op1 () && p.op1 () == op2 ())
     807          270 :     related = relation_intersect (kind (), relation_swap (p.kind ()));
     808              :   else
     809              :     return false;
     810              : 
     811      4513478 :   return old != related;
     812              : }
     813              : 
     814              : // Perform a union between 2 relations. *this ||= p.
     815              : 
     816              : bool
     817            0 : value_relation::union_ (value_relation &p)
     818              : {
     819              :   // Save previous value
     820            0 :   relation_kind old = related;
     821              : 
     822            0 :   if (p.op1 () == op1 () && p.op2 () == op2 ())
     823            0 :     related = relation_union (kind(), p.kind());
     824            0 :   else if (p.op2 () == op1 () && p.op1 () == op2 ())
     825            0 :     related = relation_union (kind(), relation_swap (p.kind ()));
     826              :   else
     827              :     return false;
     828              : 
     829            0 :   return old != related;
     830              : }
     831              : 
     832              : // Identify and apply any transitive relations between REL
     833              : // and THIS.  Return true if there was a transformation.
     834              : 
     835              : bool
     836     13917840 : value_relation::apply_transitive (const value_relation &rel)
     837              : {
     838     13917840 :   relation_kind k = VREL_VARYING;
     839              : 
     840              :   // Identify any common operand, and normalize the relations to
     841              :   // the form : A < B  B < C produces A < C
     842     13917840 :   if (rel.op1 () == name2)
     843              :     {
     844              :       // A < B   B < C
     845      2292918 :       if (rel.op2 () == name1)
     846              :         return false;
     847      2223848 :       k = relation_transitive (kind (), rel.kind ());
     848      2223848 :       if (k != VREL_VARYING)
     849              :         {
     850       917058 :           related = k;
     851       917058 :           name2 = rel.op2 ();
     852       917058 :           return true;
     853              :         }
     854              :     }
     855     11624922 :   else if (rel.op1 () == name1)
     856              :     {
     857              :       // B > A   B < C
     858      6755374 :       if (rel.op2 () == name2)
     859              :         return false;
     860      1912652 :       k = relation_transitive (relation_swap (kind ()), rel.kind ());
     861      1912652 :       if (k != VREL_VARYING)
     862              :         {
     863       489346 :           related = k;
     864       489346 :           name1 = name2;
     865       489346 :           name2 = rel.op2 ();
     866       489346 :           return true;
     867              :         }
     868              :     }
     869      4869548 :   else if (rel.op2 () == name2)
     870              :     {
     871              :        // A < B   C > B
     872      4242601 :        if (rel.op1 () == name1)
     873              :          return false;
     874      4242601 :       k = relation_transitive (kind (), relation_swap (rel.kind ()));
     875      4242601 :       if (k != VREL_VARYING)
     876              :         {
     877       539905 :           related = k;
     878       539905 :           name2 = rel.op1 ();
     879       539905 :           return true;
     880              :         }
     881              :     }
     882       626947 :   else if (rel.op2 () == name1)
     883              :     {
     884              :       // B > A  C > B
     885       626947 :       if (rel.op1 () == name2)
     886              :         return false;
     887       626947 :       k = relation_transitive (relation_swap (kind ()),
     888              :                                relation_swap (rel.kind ()));
     889       626947 :       if (k != VREL_VARYING)
     890              :         {
     891       242577 :           related = k;
     892       242577 :           name1 = name2;
     893       242577 :           name2 = rel.op1 ();
     894       242577 :           return true;
     895              :         }
     896              :     }
     897              :   return false;
     898              : }
     899              : 
     900              : // Create a trio from this value relation given LHS, OP1 and OP2.
     901              : 
     902              : relation_trio
     903     54668568 : value_relation::create_trio (tree lhs, tree op1, tree op2)
     904              : {
     905     54668568 :   relation_kind lhs_1;
     906     54668568 :   if (lhs == name1 && op1 == name2)
     907        71668 :     lhs_1 = related;
     908     54596900 :   else if (lhs == name2 && op1 == name1)
     909       182262 :     lhs_1 = relation_swap (related);
     910              :   else
     911              :     lhs_1 = VREL_VARYING;
     912              : 
     913     54668568 :   relation_kind lhs_2;
     914     54668568 :   if (lhs == name1 && op2 == name2)
     915        54653 :     lhs_2 = related;
     916     54613915 :   else if (lhs == name2 && op2 == name1)
     917       154342 :     lhs_2 = relation_swap (related);
     918              :   else
     919              :     lhs_2 = VREL_VARYING;
     920              : 
     921     54668568 :   relation_kind op_op;
     922     54668568 :   if (op1 == name1 && op2 == name2)
     923     38366026 :     op_op = related;
     924     16302542 :   else if (op1 == name2 && op2 == name1)
     925            0 :     op_op = relation_swap (related);
     926     16302542 :   else if  (op1 == op2)
     927              :     op_op = VREL_EQ;
     928              :   else
     929     16287692 :     op_op = VREL_VARYING;
     930              : 
     931     54668568 :   return relation_trio (lhs_1, lhs_2, op_op);
     932              : }
     933              : 
     934              : // Dump the relation to file F.
     935              : 
     936              : void
     937        38559 : value_relation::dump (FILE *f) const
     938              : {
     939        38559 :   if (!name1 || !name2)
     940              :     {
     941            0 :       fprintf (f, "no relation registered");
     942            0 :       return;
     943              :     }
     944        38559 :   fputc ('(', f);
     945        38559 :   print_generic_expr (f, op1 (), TDF_SLIM);
     946        38559 :   print_relation (f, kind ());
     947        38559 :   print_generic_expr (f, op2 (), TDF_SLIM);
     948        38559 :   fputc(')', f);
     949              : }
     950              : 
     951              : // This container is used to link relations in a chain.
     952              : 
     953              : class relation_chain : public value_relation
     954              : {
     955              : public:
     956              :   relation_chain *m_next;
     957              : };
     958              : 
     959              : // Given relation record PTR in block BB, return the next relation in the
     960              : // list.  If PTR is NULL, retreive the first relation in BB.
     961              : // If NAME is sprecified, return only relations which include NAME.
     962              : // Return NULL when there are no relations left.
     963              : 
     964              : relation_chain *
     965           84 : dom_oracle::next_relation (basic_block bb, relation_chain *ptr,
     966              :                            tree name) const
     967              : {
     968           84 :   relation_chain *p;
     969              :   // No value_relation pointer is used to intialize the iterator.
     970           84 :   if (!ptr)
     971              :     {
     972           37 :       int bbi = bb->index;
     973           74 :       if (bbi >= (int)m_relations.length())
     974              :         return NULL;
     975              :       else
     976           37 :         p = m_relations[bbi].m_head;
     977              :     }
     978              :   else
     979           47 :     p = ptr->m_next;
     980              : 
     981           84 :   if (name)
     982            0 :     for ( ; p; p = p->m_next)
     983            0 :       if (p->op1 () == name || p->op2 () == name)
     984              :         break;
     985              :   return p;
     986              : }
     987              : 
     988              : // Instatiate a block relation iterator to iterate over the relations
     989              : // on exit from block BB in ORACLE.  Limit this to relations involving NAME
     990              : // if specified.  Return the first such relation in VR if there is one.
     991              : 
     992           37 : block_relation_iterator::block_relation_iterator (const relation_oracle *oracle,
     993              :                                                   basic_block bb,
     994              :                                                   value_relation &vr,
     995           37 :                                                   tree name)
     996              : {
     997           37 :   m_oracle = oracle;
     998           37 :   m_bb = bb;
     999           37 :   m_name = name;
    1000           37 :   m_ptr = oracle->next_relation (bb, NULL, m_name);
    1001           37 :   if (m_ptr)
    1002              :     {
    1003           37 :       m_done = false;
    1004           37 :       vr = *m_ptr;
    1005              :     }
    1006              :   else
    1007            0 :     m_done = true;
    1008           37 : }
    1009              : 
    1010              : // Retreive the next relation from the iterator and return it in VR.
    1011              : 
    1012              : void
    1013           47 : block_relation_iterator::get_next_relation (value_relation &vr)
    1014              : {
    1015           47 :   m_ptr = m_oracle->next_relation (m_bb, m_ptr, m_name);
    1016           47 :   if (m_ptr)
    1017              :     {
    1018           10 :       vr = *m_ptr;
    1019           10 :       if (m_name)
    1020              :         {
    1021            0 :           if (vr.op1 () != m_name)
    1022              :             {
    1023            0 :               gcc_checking_assert (vr.op2 () == m_name);
    1024            0 :               vr.swap ();
    1025              :             }
    1026              :         }
    1027              :     }
    1028              :   else
    1029           37 :     m_done = true;
    1030           47 : }
    1031              : 
    1032              : // ------------------------------------------------------------------------
    1033              : 
    1034              : // Find the relation between any ssa_name in B1 and any name in B2 in LIST.
    1035              : // This will allow equivalencies to be applied to any SSA_NAME in a relation.
    1036              : 
    1037              : relation_kind
    1038    293418162 : relation_chain_head::find_relation (const_bitmap b1, const_bitmap b2) const
    1039              : {
    1040    293418162 :   if (!m_names)
    1041              :     return VREL_VARYING;
    1042              : 
    1043              :   // If both b1 and b2 aren't referenced in this block, cant be a relation
    1044    177186496 :   if (!bitmap_intersect_p (m_names, b1) || !bitmap_intersect_p (m_names, b2))
    1045    172745081 :     return VREL_VARYING;
    1046              : 
    1047              :   // Search for the first relation that contains BOTH an element from B1
    1048              :   // and B2, and return that relation.
    1049     15422561 :   for (relation_chain *ptr = m_head; ptr ; ptr = ptr->m_next)
    1050              :     {
    1051     13611609 :       unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
    1052     13611609 :       unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
    1053     13611609 :       if (bitmap_bit_p (b1, op1) && bitmap_bit_p (b2, op2))
    1054      2490891 :         return ptr->kind ();
    1055     11120718 :       if (bitmap_bit_p (b1, op2) && bitmap_bit_p (b2, op1))
    1056       139572 :         return relation_swap (ptr->kind ());
    1057              :     }
    1058              : 
    1059              :   return VREL_VARYING;
    1060              : }
    1061              : 
    1062              : // Instantiate a relation oracle.
    1063              : 
    1064     25575492 : dom_oracle::dom_oracle (bool do_trans_p)
    1065              : {
    1066     25575492 :   m_do_trans_p = do_trans_p;
    1067     25575492 :   m_relations.create (0);
    1068     25575492 :   m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
    1069     25575492 :   m_relation_set = BITMAP_ALLOC (&m_bitmaps);
    1070     25575492 :   m_tmp = BITMAP_ALLOC (&m_bitmaps);
    1071     25575492 :   m_tmp2 = BITMAP_ALLOC (&m_bitmaps);
    1072     25575492 : }
    1073              : 
    1074              : // Destruct a relation oracle.
    1075              : 
    1076     51150984 : dom_oracle::~dom_oracle ()
    1077              : {
    1078     25575492 :   m_relations.release ();
    1079     51150984 : }
    1080              : 
    1081              : // Register relation K between ssa_name OP1 and OP2 on STMT.
    1082              : // Return false if no new relation is added.
    1083              : 
    1084              : bool
    1085     32917363 : relation_oracle::record (gimple *stmt, relation_kind k, tree op1, tree op2)
    1086              : {
    1087     32917363 :   gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
    1088     32917363 :   gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
    1089     32917363 :   gcc_checking_assert (stmt && gimple_bb (stmt));
    1090              : 
    1091              :   // Don't register lack of a relation.
    1092     32917363 :   if (k == VREL_VARYING)
    1093              :     return false;
    1094              : 
    1095              :   // If an equivalence is being added between a PHI and one of its arguments
    1096              :   // make sure that that argument is not defined in the same block.
    1097              :   // This can happen along back edges and the equivalence will not be
    1098              :   // applicable as it would require a use before def.
    1099     32917363 :   if (k == VREL_EQ && is_a<gphi *> (stmt))
    1100              :     {
    1101      1990652 :       tree phi_def = gimple_phi_result (stmt);
    1102      1990652 :       gcc_checking_assert (phi_def == op1 || phi_def == op2);
    1103      1990652 :       tree arg = op2;
    1104      1990652 :       if (phi_def == op2)
    1105            0 :         arg = op1;
    1106      1990652 :       if (gimple_bb (stmt) == gimple_bb (SSA_NAME_DEF_STMT (arg)))
    1107              :         return false;
    1108              :     }
    1109              : 
    1110              :   // If the LHS of a statement has already been processed and an equivalence
    1111              :   // registered, do not register another one.  See PR 124809.
    1112     77706532 :   if (m_lhs_equiv_set_p && relation_equiv_p (k)
    1113     45598154 :       && gimple_get_lhs (stmt) == op1)
    1114              :     {
    1115     12680791 :       if (!bitmap_set_bit (m_lhs_equiv_set_p, SSA_NAME_VERSION (op1)))
    1116              :         return false;
    1117              :     }
    1118     32108378 :   bool ret = record (gimple_bb (stmt), k, op1, op2);
    1119              : 
    1120     32108378 :   if (ret && dump_file && (dump_flags & TDF_DETAILS))
    1121              :     {
    1122        36748 :       value_relation vr (k, op1, op2);
    1123        36748 :       fprintf (dump_file, " Registering value_relation ");
    1124        36748 :       vr.dump (dump_file);
    1125        36748 :       fprintf (dump_file, " (bb%d) at ", gimple_bb (stmt)->index);
    1126        36748 :       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
    1127              :     }
    1128              :   return ret;
    1129              : }
    1130              : 
    1131              : // Register relation K between ssa_name OP1 and OP2 on edge E.
    1132              : // Return false if no new relation is added.
    1133              : 
    1134              : bool
    1135      6499694 : relation_oracle::record (edge e, relation_kind k, tree op1, tree op2)
    1136              : {
    1137      6499694 :   gcc_checking_assert (TREE_CODE (op1) == SSA_NAME);
    1138      6499694 :   gcc_checking_assert (TREE_CODE (op2) == SSA_NAME);
    1139              : 
    1140              :   // Do not register lack of relation, or blocks which have more than
    1141              :   // edge E for a predecessor.
    1142      6499694 :   if (k == VREL_VARYING || !single_pred_p (e->dest))
    1143              :     return false;
    1144              : 
    1145      6499694 :   bool ret = record (e->dest, k, op1, op2);
    1146              : 
    1147      6499694 :   if (ret && dump_file && (dump_flags & TDF_DETAILS))
    1148              :     {
    1149          112 :       value_relation vr (k, op1, op2);
    1150          112 :       fprintf (dump_file, " Registering value_relation ");
    1151          112 :       vr.dump (dump_file);
    1152          112 :       fprintf (dump_file, " on (%d->%d)\n", e->src->index, e->dest->index);
    1153              :     }
    1154              :   return ret;
    1155              : }
    1156              : 
    1157              : // Register relation K between OP! and OP2 in block BB.
    1158              : // This creates the record and searches for existing records in the dominator
    1159              : // tree to merge with.  Return false if no new relation is added.
    1160              : 
    1161              : bool
    1162     37486564 : dom_oracle::record (basic_block bb, relation_kind k, tree op1, tree op2)
    1163              : {
    1164              :   // If the 2 ssa_names are the same, do nothing.  An equivalence is implied,
    1165              :   // and no other relation makes sense.
    1166     37486564 :   if (op1 == op2)
    1167              :     return false;
    1168              : 
    1169              :   // Equivalencies are handled by the equivalence oracle.
    1170     37475855 :   if (relation_equiv_p (k))
    1171     13659818 :     return equiv_oracle::record (bb, k, op1, op2);
    1172              :   else
    1173              :     {
    1174              :       // if neither op1 nor op2 are in a relation before this is registered,
    1175              :       // there will be no transitive.
    1176     23816037 :       bool check = bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op1))
    1177     40064027 :                    || bitmap_bit_p (m_relation_set, SSA_NAME_VERSION (op2));
    1178     23816037 :       relation_chain *ptr = set_one_relation (bb, k, op1, op2);
    1179     23816037 :       if (ptr && check
    1180     23816037 :           && (m_relations[bb->index].m_num_relations
    1181      6383775 :               < param_relation_block_limit))
    1182      6383639 :         register_transitives (bb, *ptr);
    1183     23816037 :       return ptr != NULL;
    1184              :     }
    1185              : }
    1186              : 
    1187              : // Register relation K between OP! and OP2 in block BB.
    1188              : // This creates the record and searches for existing records in the dominator
    1189              : // tree to merge with.  Return the record, or NULL if no record was created.
    1190              : 
    1191              : relation_chain *
    1192     26004923 : dom_oracle::set_one_relation (basic_block bb, relation_kind k, tree op1,
    1193              :                               tree op2)
    1194              : {
    1195     26004923 :   gcc_checking_assert (k != VREL_VARYING && k != VREL_EQ);
    1196              : 
    1197     26004923 :   value_relation vr(k, op1, op2);
    1198     26004923 :   int bbi = bb->index;
    1199              : 
    1200     52009846 :   if (bbi >= (int)m_relations.length())
    1201          137 :     m_relations.safe_grow_cleared (last_basic_block_for_fn (cfun) + 1);
    1202              : 
    1203              :   // Summary bitmap indicating what ssa_names have relations in this BB.
    1204     26004923 :   bitmap bm = m_relations[bbi].m_names;
    1205     26004923 :   if (!bm)
    1206     14395295 :     bm = m_relations[bbi].m_names = BITMAP_ALLOC (&m_bitmaps);
    1207     26004923 :   unsigned v1 = SSA_NAME_VERSION (op1);
    1208     26004923 :   unsigned v2 = SSA_NAME_VERSION (op2);
    1209              : 
    1210     26004923 :   relation_kind curr;
    1211     26004923 :   relation_chain *ptr;
    1212     26004923 :   curr = find_relation_block (bbi, v1, v2, &ptr);
    1213              :   // There is an existing relation in this block, just intersect with it.
    1214     26004923 :   if (curr != VREL_VARYING)
    1215              :     {
    1216              :       // Check into whether we can simply replace the relation rather than
    1217              :       // intersecting it.  This may help with some optimistic iterative
    1218              :       // updating algorithms.  If there was no change, return no record..
    1219      4513478 :       if (!ptr->intersect (vr))
    1220              :         return NULL;
    1221              :     }
    1222              :   else
    1223              :     {
    1224     21491445 :       if (m_relations[bbi].m_num_relations >= param_relation_block_limit)
    1225              :         return NULL;
    1226     21384011 :       m_relations[bbi].m_num_relations++;
    1227              :       // Check for an existing relation further up the DOM chain.
    1228              :       // By including dominating relations, The first one found in any search
    1229              :       // will be the aggregate of all the previous ones.
    1230     21384011 :       curr = find_relation_dom (bb, v1, v2);
    1231     21384011 :       if (curr != VREL_VARYING)
    1232       618126 :         k = relation_intersect (curr, k);
    1233              : 
    1234     21384011 :       bitmap_set_bit (bm, v1);
    1235     21384011 :       bitmap_set_bit (bm, v2);
    1236     21384011 :       bitmap_set_bit (m_relation_set, v1);
    1237     21384011 :       bitmap_set_bit (m_relation_set, v2);
    1238              : 
    1239     21384011 :       ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
    1240              :                                               sizeof (relation_chain));
    1241     21384011 :       ptr->set_relation (k, op1, op2);
    1242     21384011 :       ptr->m_next = m_relations[bbi].m_head;
    1243     21384011 :       m_relations[bbi].m_head = ptr;
    1244              :     }
    1245     21578117 :   return ptr;
    1246              : }
    1247              : 
    1248              : // Starting at ROOT_BB search the DOM tree  looking for relations which
    1249              : // may produce transitive relations to RELATION.  EQUIV1 and EQUIV2 are
    1250              : // bitmaps for op1/op2 and any of their equivalences that should also be
    1251              : // considered.
    1252              : 
    1253              : void
    1254      6383639 : dom_oracle::register_transitives (basic_block root_bb,
    1255              :                                   const value_relation &relation)
    1256              : {
    1257              :   // Only register transitives if they are requested.
    1258      6383639 :   if (!m_do_trans_p)
    1259              :     return;
    1260      6383629 :   basic_block bb;
    1261              :   // Only apply transitives to certain kinds of operations.
    1262      6383629 :   switch (relation.kind ())
    1263              :     {
    1264      4691193 :       case VREL_LE:
    1265      4691193 :       case VREL_LT:
    1266      4691193 :       case VREL_GT:
    1267      4691193 :       case VREL_GE:
    1268      4691193 :         break;
    1269              :       default:
    1270              :         return;
    1271              :     }
    1272              : 
    1273      4691193 :   const_bitmap equiv1 = equiv_set (relation.op1 (), root_bb);
    1274      4691193 :   const_bitmap equiv2 = equiv_set (relation.op2 (), root_bb);
    1275              : 
    1276      4691193 :   const unsigned work_budget = param_transitive_relations_work_bound;
    1277      4691193 :   unsigned avail_budget = work_budget;
    1278     90787731 :   for (bb = root_bb; bb;
    1279              :        /* Advancing to the next immediate dominator eats from the budget,
    1280              :           if none is left after that there's no point to continue.  */
    1281              :        bb = (--avail_budget > 0
    1282     86154757 :              ? get_immediate_dominator (CDI_DOMINATORS, bb) : nullptr))
    1283              :     {
    1284     86231713 :       int bbi = bb->index;
    1285    172463426 :       if (bbi >= (int)m_relations.length())
    1286         4622 :         continue;
    1287     86227091 :       const_bitmap bm = m_relations[bbi].m_names;
    1288     86227091 :       if (!bm)
    1289     61637378 :         continue;
    1290     24589713 :       if (!bitmap_intersect_p (bm, equiv1) && !bitmap_intersect_p (bm, equiv2))
    1291     16152088 :         continue;
    1292              :       // At least one of the 2 ops has a relation in this block.
    1293      8437625 :       relation_chain *ptr;
    1294     39999323 :       for (ptr = m_relations[bbi].m_head; ptr ; ptr = ptr->m_next)
    1295              :         {
    1296              :           // In the presence of an equivalence, 2 operands may do not
    1297              :           // naturally match. ie  with equivalence a_2 == b_3
    1298              :           // given c_1 < a_2 && b_3 < d_4
    1299              :           // convert the second relation (b_3 < d_4) to match any
    1300              :           // equivalences to found in the first relation.
    1301              :           // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
    1302              :           // transitive operation:  c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
    1303              : 
    1304     31638654 :           tree r1, r2;
    1305     31638654 :           tree p1 = ptr->op1 ();
    1306     31638654 :           tree p2 = ptr->op2 ();
    1307              :           // Find which equivalence is in the first operand.
    1308     31638654 :           if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p1)))
    1309              :             r1 = p1;
    1310     24882781 :           else if (bitmap_bit_p (equiv1, SSA_NAME_VERSION (p2)))
    1311              :             r1 = p2;
    1312              :           else
    1313     24186638 :             r1 = NULL_TREE;
    1314              : 
    1315              :           // Find which equivalence is in the second operand.
    1316     31638654 :           if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p1)))
    1317              :             r2 = p1;
    1318     29345237 :           else if (bitmap_bit_p (equiv2, SSA_NAME_VERSION (p2)))
    1319              :             r2 = p2;
    1320              :           else
    1321     20259788 :             r2 = NULL_TREE;
    1322              : 
    1323              :           // Ignore if both NULL (not relevant relation) or the same,
    1324     31638654 :           if (r1 == r2)
    1325              :             ;
    1326              : 
    1327              :           else
    1328              :             {
    1329              :               // Any operand not an equivalence, just take the real operand.
    1330     13917840 :               if (!r1)
    1331      6466449 :                 r1 = relation.op1 ();
    1332     13917840 :               if (!r2)
    1333      2539599 :                 r2 = relation.op2 ();
    1334              : 
    1335     13917840 :               value_relation nr (relation.kind (), r1, r2);
    1336     13917840 :               if (nr.apply_transitive (*ptr))
    1337              :                 {
    1338              :                   // If the new relation is already present we know any
    1339              :                   // further processing is already reflected above it.
    1340              :                   // When we ran into the limit of relations on root_bb
    1341              :                   // we can give up as well.
    1342      2188886 :                   if (!set_one_relation (root_bb, nr.kind (),
    1343              :                                          nr.op1 (), nr.op2 ()))
    1344        70216 :                     return;
    1345      2118670 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1346              :                     {
    1347         1322 :                       fprintf (dump_file,
    1348              :                                "   Registering transitive relation ");
    1349         1322 :                       nr.dump (dump_file);
    1350         1322 :                       fputc ('\n', dump_file);
    1351              :                     }
    1352              :                 }
    1353              :             }
    1354              :           /* Processed one relation, abort if we've eaten up our budget.  */
    1355     31568438 :           if (--avail_budget == 0)
    1356              :             return;
    1357              :         }
    1358              :     }
    1359              : }
    1360              : 
    1361              : // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
    1362              : // This will allow equivalencies to be applied to any SSA_NAME in a relation.
    1363              : 
    1364              : relation_kind
    1365    244248114 : dom_oracle::find_relation_block (unsigned bb, const_bitmap b1,
    1366              :                                       const_bitmap b2) const
    1367              : {
    1368    244248114 :   if (bb >= m_relations.length())
    1369              :     return VREL_VARYING;
    1370              : 
    1371    244246611 :   return m_relations[bb].find_relation (b1, b2);
    1372              : }
    1373              : 
    1374              : // Search the DOM tree for a relation between an element of equivalency set B1
    1375              : // and B2, starting with block BB.
    1376              : 
    1377              : relation_kind
    1378     17979577 : dom_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
    1379              : {
    1380     17979577 :   relation_kind r;
    1381     17979577 :   if (bitmap_equal_p (b1, b2))
    1382              :     return VREL_EQ;
    1383              : 
    1384              :   // If either name does not occur in a relation anywhere, there isn't one.
    1385     17979577 :   if (!bitmap_intersect_p (m_relation_set, b1)
    1386     17979577 :       || !bitmap_intersect_p (m_relation_set, b2))
    1387      9186250 :     return VREL_VARYING;
    1388              : 
    1389              :   // Search each block in the DOM tree checking.
    1390    252363817 :   for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
    1391              :     {
    1392    244248114 :       r = find_relation_block (bb->index, b1, b2);
    1393    244248114 :       if (r != VREL_VARYING)
    1394              :         return r;
    1395              :     }
    1396              :   return VREL_VARYING;
    1397              : 
    1398              : }
    1399              : 
    1400              : // Find a relation in block BB between ssa version V1 and V2.  If a relation
    1401              : // is found, return a pointer to the chain object in OBJ.
    1402              : 
    1403              : relation_kind
    1404    559121252 : dom_oracle::find_relation_block (int bb, unsigned v1, unsigned v2,
    1405              :                                      relation_chain **obj) const
    1406              : {
    1407   1118242504 :   if (bb >= (int)m_relations.length())
    1408              :     return VREL_VARYING;
    1409              : 
    1410    559115608 :   const_bitmap bm = m_relations[bb].m_names;
    1411    559115608 :   if (!bm)
    1412              :     return VREL_VARYING;
    1413              : 
    1414              :   // If both b1 and b2 aren't referenced in this block, cant be a relation
    1415    407202729 :   if (!bitmap_bit_p (bm, v1) || !bitmap_bit_p (bm, v2))
    1416    393128608 :     return VREL_VARYING;
    1417              : 
    1418     14074121 :   relation_chain *ptr;
    1419     76978526 :   for (ptr = m_relations[bb].m_head; ptr ; ptr = ptr->m_next)
    1420              :     {
    1421     74178996 :       unsigned op1 = SSA_NAME_VERSION (ptr->op1 ());
    1422     74178996 :       unsigned op2 = SSA_NAME_VERSION (ptr->op2 ());
    1423     74178996 :       if (v1 == op1 && v2 == op2)
    1424              :         {
    1425     11000885 :           if (obj)
    1426      4513208 :             *obj = ptr;
    1427     11000885 :           return ptr->kind ();
    1428              :         }
    1429     63178111 :       if (v1 == op2 && v2 == op1)
    1430              :         {
    1431       273706 :           if (obj)
    1432          270 :             *obj = ptr;
    1433       273706 :           return relation_swap (ptr->kind ());
    1434              :         }
    1435              :     }
    1436              : 
    1437              :   return VREL_VARYING;
    1438              : }
    1439              : 
    1440              : // Find a relation between SSA version V1 and V2 in the dominator tree
    1441              : // starting with block BB
    1442              : 
    1443              : relation_kind
    1444     35168084 : dom_oracle::find_relation_dom (basic_block bb, unsigned v1, unsigned v2) const
    1445              : {
    1446     35168084 :   relation_kind r;
    1447              :   // IF either name does not occur in a relation anywhere, there isn't one.
    1448     35168084 :   if (!bitmap_bit_p (m_relation_set, v1) || !bitmap_bit_p (m_relation_set, v2))
    1449     18443468 :     return VREL_VARYING;
    1450              : 
    1451    543079832 :   for ( ; bb; bb = get_immediate_dominator (CDI_DOMINATORS, bb))
    1452              :     {
    1453    533116329 :       r = find_relation_block (bb->index, v1, v2);
    1454    533116329 :       if (r != VREL_VARYING)
    1455              :         return r;
    1456              :     }
    1457              :   return VREL_VARYING;
    1458              : 
    1459              : }
    1460              : 
    1461              : // Query if there is a relation between SSA1 and SS2 in block BB or a
    1462              : // dominator of BB
    1463              : 
    1464              : relation_kind
    1465     58436314 : dom_oracle::query (basic_block bb, tree ssa1, tree ssa2)
    1466              : {
    1467     58436314 :   relation_kind kind;
    1468     58436314 :   unsigned v1 = SSA_NAME_VERSION (ssa1);
    1469     58436314 :   unsigned v2 = SSA_NAME_VERSION (ssa2);
    1470     58436314 :   if (v1 == v2)
    1471              :     return VREL_EQ;
    1472              : 
    1473              :   // If v1 or v2 do not have any relations or equivalences, a partial
    1474              :   // equivalence is the only possibility.
    1475     96447353 :   if ((!bitmap_bit_p (m_relation_set, v1) && !has_equiv_p (v1))
    1476     61250237 :       || (!bitmap_bit_p (m_relation_set, v2) && !has_equiv_p (v2)))
    1477     44253518 :     return partial_equiv (ssa1, ssa2);
    1478              : 
    1479              :   // Check for equivalence first.  They must be in each equivalency set.
    1480     14082321 :   const_bitmap equiv1 = equiv_set (ssa1, bb);
    1481     14082321 :   const_bitmap equiv2 = equiv_set (ssa2, bb);
    1482     14082321 :   if (bitmap_bit_p (equiv1, v2) && bitmap_bit_p (equiv2, v1))
    1483              :     return VREL_EQ;
    1484              : 
    1485     13784262 :   kind = partial_equiv (ssa1, ssa2);
    1486     13784262 :   if (kind != VREL_VARYING)
    1487              :     return kind;
    1488              : 
    1489              :   // Initially look for a direct relationship and just return that.
    1490     13784073 :   kind = find_relation_dom (bb, v1, v2);
    1491     13784073 :   if (kind != VREL_VARYING)
    1492              :     return kind;
    1493              : 
    1494              :   // Query using the equivalence sets.
    1495      7641086 :   kind = query (bb, equiv1, equiv2);
    1496      7641086 :   return kind;
    1497              : }
    1498              : 
    1499              : // Dump all the relations in block BB to file F.
    1500              : 
    1501              : void
    1502          248 : dom_oracle::dump (FILE *f, basic_block bb) const
    1503              : {
    1504          248 :   equiv_oracle::dump (f,bb);
    1505              : 
    1506          496 :   if (bb->index >= (int)m_relations.length ())
    1507          211 :     return;
    1508          248 :   if (!m_relations[bb->index].m_names)
    1509              :     return;
    1510              : 
    1511           37 :   value_relation vr;
    1512           84 :   FOR_EACH_RELATION_BB (this, bb, vr)
    1513              :     {
    1514           47 :       fprintf (f, "Relational : ");
    1515           47 :       vr.dump (f);
    1516           47 :       fprintf (f, "\n");
    1517              :     }
    1518              : }
    1519              : 
    1520              : // Dump all the relations known to file F.
    1521              : 
    1522              : void
    1523            0 : dom_oracle::dump (FILE *f) const
    1524              : {
    1525            0 :   fprintf (f, "Relation dump\n");
    1526            0 :   for (unsigned i = 0; i < m_relations.length (); i++)
    1527            0 :     if (BASIC_BLOCK_FOR_FN (cfun, i))
    1528              :       {
    1529            0 :         fprintf (f, "BB%d\n", i);
    1530            0 :         dump (f, BASIC_BLOCK_FOR_FN (cfun, i));
    1531              :       }
    1532            0 : }
    1533              : 
    1534              : void
    1535            0 : relation_oracle::debug () const
    1536              : {
    1537            0 :   dump (stderr);
    1538            0 : }
    1539              : 
    1540     28778296 : path_oracle::path_oracle (relation_oracle *oracle)
    1541              : {
    1542     28778296 :   set_root_oracle (oracle);
    1543     28778296 :   bitmap_obstack_initialize (&m_bitmaps);
    1544     28778296 :   obstack_init (&m_chain_obstack);
    1545              : 
    1546              :   // Initialize header records.
    1547     28778296 :   m_equiv.m_names = BITMAP_ALLOC (&m_bitmaps);
    1548     28778296 :   m_equiv.m_bb = NULL;
    1549     28778296 :   m_equiv.m_next = NULL;
    1550     28778296 :   m_relations.m_names = BITMAP_ALLOC (&m_bitmaps);
    1551     28778296 :   m_relations.m_head = NULL;
    1552     28778296 :   m_killed_defs = BITMAP_ALLOC (&m_bitmaps);
    1553     28778296 : }
    1554              : 
    1555     57556592 : path_oracle::~path_oracle ()
    1556              : {
    1557     28778296 :   obstack_free (&m_chain_obstack, NULL);
    1558     28778296 :   bitmap_obstack_release (&m_bitmaps);
    1559     57556592 : }
    1560              : 
    1561              : // Return the equiv set for SSA, and if there isn't one, check for equivs
    1562              : // starting in block BB.
    1563              : 
    1564              : const_bitmap
    1565    130162776 : path_oracle::equiv_set (tree ssa, basic_block bb)
    1566              : {
    1567              :   // Check the list first.
    1568    130162776 :   equiv_chain *ptr = m_equiv.find (SSA_NAME_VERSION (ssa));
    1569    130162776 :   if (ptr)
    1570     70391904 :     return ptr->m_names;
    1571              : 
    1572              :   // Otherwise defer to the root oracle.
    1573     59770872 :   if (m_root)
    1574     52493394 :     return m_root->equiv_set (ssa, bb);
    1575              : 
    1576              :   // Allocate a throw away bitmap if there isn't a root oracle.
    1577      7277478 :   bitmap tmp = BITMAP_ALLOC (&m_bitmaps);
    1578      7277478 :   bitmap_set_bit (tmp, SSA_NAME_VERSION (ssa));
    1579      7277478 :   return tmp;
    1580              : }
    1581              : 
    1582              : // Register an equivalence between SSA1 and SSA2 resolving unknowns from
    1583              : // block BB.  Return false if no new equivalence was added.
    1584              : 
    1585              : bool
    1586      7484845 : path_oracle::register_equiv (basic_block bb, tree ssa1, tree ssa2)
    1587              : {
    1588      7484845 :   const_bitmap equiv_1 = equiv_set (ssa1, bb);
    1589      7484845 :   const_bitmap equiv_2 = equiv_set (ssa2, bb);
    1590              : 
    1591              :   // Check if they are the same set, if so, we're done.
    1592      7484845 :   if (bitmap_equal_p (equiv_1, equiv_2))
    1593              :     return false;
    1594              : 
    1595              :   // Don't mess around, simply create a new record and insert it first.
    1596      7471534 :   bitmap b = BITMAP_ALLOC (&m_bitmaps);
    1597      7471534 :   valid_equivs (b, equiv_1, bb);
    1598      7471534 :   valid_equivs (b, equiv_2, bb);
    1599              : 
    1600      7471534 :   equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
    1601              :                                                     sizeof (equiv_chain));
    1602      7471534 :   ptr->m_names = b;
    1603      7471534 :   ptr->m_bb = NULL;
    1604      7471534 :   ptr->m_next = m_equiv.m_next;
    1605      7471534 :   m_equiv.m_next = ptr;
    1606      7471534 :   bitmap_ior_into (m_equiv.m_names, b);
    1607      7471534 :   return true;
    1608              : }
    1609              : 
    1610              : // Register killing definition of an SSA_NAME.
    1611              : 
    1612              : void
    1613     55885891 : path_oracle::killing_def (tree ssa)
    1614              : {
    1615     55885891 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1616              :     {
    1617          835 :       fprintf (dump_file, " Registering killing_def (path_oracle) ");
    1618          835 :       print_generic_expr (dump_file, ssa, TDF_SLIM);
    1619          835 :       fprintf (dump_file, "\n");
    1620              :     }
    1621              : 
    1622     55885891 :   unsigned v = SSA_NAME_VERSION (ssa);
    1623              : 
    1624     55885891 :   bitmap_set_bit (m_killed_defs, v);
    1625     55885891 :   bitmap_set_bit (m_equiv.m_names, v);
    1626              : 
    1627              :   // Now add an equivalency with itself so we don't look to the root oracle.
    1628     55885891 :   bitmap b = BITMAP_ALLOC (&m_bitmaps);
    1629     55885891 :   bitmap_set_bit (b, v);
    1630     55885891 :   equiv_chain *ptr = (equiv_chain *) obstack_alloc (&m_chain_obstack,
    1631              :                                                     sizeof (equiv_chain));
    1632     55885891 :   ptr->m_names = b;
    1633     55885891 :   ptr->m_bb = NULL;
    1634     55885891 :   ptr->m_next = m_equiv.m_next;
    1635     55885891 :   m_equiv.m_next = ptr;
    1636              : 
    1637              :   // Walk the relation list and remove SSA from any relations.
    1638     55885891 :   if (!bitmap_bit_p (m_relations.m_names, v))
    1639              :     return;
    1640              : 
    1641       120360 :   bitmap_clear_bit (m_relations.m_names, v);
    1642       120360 :   relation_chain **prev = &(m_relations.m_head);
    1643       120360 :   relation_chain *next = NULL;
    1644       413056 :   for (relation_chain *ptr = m_relations.m_head; ptr; ptr = next)
    1645              :     {
    1646       292696 :       gcc_checking_assert (*prev == ptr);
    1647       292696 :       next = ptr->m_next;
    1648       292696 :       if (SSA_NAME_VERSION (ptr->op1 ()) == v
    1649       292696 :           || SSA_NAME_VERSION (ptr->op2 ()) == v)
    1650       119162 :         *prev = ptr->m_next;
    1651              :       else
    1652       173534 :         prev = &(ptr->m_next);
    1653              :     }
    1654              : }
    1655              : 
    1656              : // Register relation K between SSA1 and SSA2, resolving unknowns by
    1657              : // querying from BB.  Return false if no new relation is registered.
    1658              : 
    1659              : bool
    1660     27771423 : path_oracle::record (basic_block bb, relation_kind k, tree ssa1, tree ssa2)
    1661              : {
    1662              :   // If the 2 ssa_names are the same, do nothing.  An equivalence is implied,
    1663              :   // and no other relation makes sense.
    1664     27771423 :   if (ssa1 == ssa2)
    1665              :     return false;
    1666              : 
    1667     27727111 :   relation_kind curr = query (bb, ssa1, ssa2);
    1668     27727111 :   if (curr != VREL_VARYING)
    1669      2120598 :     k = relation_intersect (curr, k);
    1670              : 
    1671     27727111 :   bool ret;
    1672     27727111 :   if (k == VREL_EQ)
    1673      7484845 :     ret = register_equiv (bb, ssa1, ssa2);
    1674              :   else
    1675              :     {
    1676     20242266 :       bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa1));
    1677     20242266 :       bitmap_set_bit (m_relations.m_names, SSA_NAME_VERSION (ssa2));
    1678     20242266 :       relation_chain *ptr = (relation_chain *) obstack_alloc (&m_chain_obstack,
    1679              :                                                           sizeof (relation_chain));
    1680     20242266 :       ptr->set_relation (k, ssa1, ssa2);
    1681     20242266 :       ptr->m_next = m_relations.m_head;
    1682     20242266 :       m_relations.m_head = ptr;
    1683     20242266 :       ret = true;
    1684              :     }
    1685              : 
    1686     27727111 :   if (ret && dump_file && (dump_flags & TDF_DETAILS))
    1687              :     {
    1688          290 :       value_relation vr (k, ssa1, ssa2);
    1689          290 :       fprintf (dump_file, " Registering value_relation (path_oracle) ");
    1690          290 :       vr.dump (dump_file);
    1691          290 :       fprintf (dump_file, " (root: bb%d)\n", bb->index);
    1692              :     }
    1693              :   return ret;
    1694              : }
    1695              : 
    1696              : // Query for a relationship between equiv set B1 and B2, resolving unknowns
    1697              : // starting at block BB.
    1698              : 
    1699              : relation_kind
    1700     49171551 : path_oracle::query (basic_block bb, const_bitmap b1, const_bitmap b2)
    1701              : {
    1702     49171551 :   if (bitmap_equal_p (b1, b2))
    1703              :     return VREL_EQ;
    1704              : 
    1705     49171551 :   relation_kind k = m_relations.find_relation (b1, b2);
    1706              : 
    1707              :   // Do not look at the root oracle for names that have been killed
    1708              :   // along the path.
    1709     49171551 :   if (bitmap_intersect_p (m_killed_defs, b1)
    1710     49171551 :       || bitmap_intersect_p (m_killed_defs, b2))
    1711     36501405 :     return k;
    1712              : 
    1713     12670146 :   if (k == VREL_VARYING && m_root)
    1714     10338491 :     k = m_root->query (bb, b1, b2);
    1715              : 
    1716              :   return k;
    1717              : }
    1718              : 
    1719              : // Query for a relationship between SSA1 and SSA2, resolving unknowns
    1720              : // starting at block BB.
    1721              : 
    1722              : relation_kind
    1723     49794496 : path_oracle::query (basic_block bb, tree ssa1, tree ssa2)
    1724              : {
    1725     49794496 :   unsigned v1 = SSA_NAME_VERSION (ssa1);
    1726     49794496 :   unsigned v2 = SSA_NAME_VERSION (ssa2);
    1727              : 
    1728     49794496 :   if (v1 == v2)
    1729              :     return VREL_EQ;
    1730              : 
    1731     49702863 :   const_bitmap equiv_1 = equiv_set (ssa1, bb);
    1732     49702863 :   const_bitmap equiv_2 = equiv_set (ssa2, bb);
    1733     49702863 :   if (bitmap_bit_p (equiv_1, v2) && bitmap_bit_p (equiv_2, v1))
    1734              :     return VREL_EQ;
    1735              : 
    1736     49171551 :   return query (bb, equiv_1, equiv_2);
    1737              : }
    1738              : 
    1739              : // Reset any relations registered on this path.  ORACLE is the root
    1740              : // oracle to use.
    1741              : 
    1742              : void
    1743     23415258 : path_oracle::reset_path (relation_oracle *oracle)
    1744              : {
    1745     23415258 :   set_root_oracle (oracle);
    1746     23415258 :   m_equiv.m_next = NULL;
    1747     23415258 :   bitmap_clear (m_equiv.m_names);
    1748     23415258 :   m_relations.m_head = NULL;
    1749     23415258 :   bitmap_clear (m_relations.m_names);
    1750     23415258 :   bitmap_clear (m_killed_defs);
    1751     23415258 : }
    1752              : 
    1753              : // Dump relation in basic block... Do nothing here.
    1754              : 
    1755              : void
    1756            0 : path_oracle::dump (FILE *, basic_block) const
    1757              : {
    1758            0 : }
    1759              : 
    1760              : // Dump the relations and equivalencies found in the path.
    1761              : 
    1762              : void
    1763            0 : path_oracle::dump (FILE *f) const
    1764              : {
    1765            0 :   equiv_chain *ptr = m_equiv.m_next;
    1766            0 :   relation_chain *ptr2 = m_relations.m_head;
    1767              : 
    1768            0 :   if (ptr || ptr2)
    1769            0 :     fprintf (f, "\npath_oracle:\n");
    1770              : 
    1771            0 :   for (; ptr; ptr = ptr->m_next)
    1772            0 :     ptr->dump (f);
    1773              : 
    1774            0 :   for (; ptr2; ptr2 = ptr2->m_next)
    1775              :     {
    1776            0 :       fprintf (f, "Relational : ");
    1777            0 :       ptr2->dump (f);
    1778            0 :       fprintf (f, "\n");
    1779              :     }
    1780            0 : }
    1781              : 
    1782              : // ------------------------------------------------------------------------
    1783              : //  EQUIV iterator.  Although we have bitmap iterators, don't expose that it
    1784              : //  is currently a bitmap.  Use an export iterator to hide future changes.
    1785              : 
    1786              : // Construct a basic iterator over an equivalence bitmap.
    1787              : 
    1788     50354904 : equiv_relation_iterator::equiv_relation_iterator (relation_oracle *oracle,
    1789              :                                                   basic_block bb, tree name,
    1790     50354904 :                                                   bool full, bool partial)
    1791              : {
    1792     50354904 :   m_name = name;
    1793     50354904 :   m_oracle = oracle;
    1794     50354904 :   m_pe = partial ? oracle->partial_equiv_set (name) : NULL;
    1795     50354904 :   m_bm = NULL;
    1796     50354904 :   if (full)
    1797     50354904 :     m_bm = oracle->equiv_set (name, bb);
    1798     50354904 :   if (!m_bm && m_pe)
    1799            0 :     m_bm = m_pe->members;
    1800     50354904 :   if (m_bm)
    1801     50354903 :     bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
    1802     50354904 : }
    1803              : 
    1804              : // Move to the next export bitmap spot.
    1805              : 
    1806              : void
    1807     65483223 : equiv_relation_iterator::next ()
    1808              : {
    1809     65483223 :   bmp_iter_next (&m_bi, &m_y);
    1810     65483223 : }
    1811              : 
    1812              : // Fetch the name of the next export in the export list.  Return NULL if
    1813              : // iteration is done.
    1814              : 
    1815              : tree
    1816     59774441 : equiv_relation_iterator::get_name (relation_kind *rel)
    1817              : {
    1818     65483186 :   if (!m_bm)
    1819              :     return NULL_TREE;
    1820              : 
    1821    121546871 :   while (bmp_iter_set (&m_bi, &m_y))
    1822              :     {
    1823              :       // Do not return self.
    1824     65483223 :       tree t = ssa_name (m_y);
    1825     65483223 :       if (t && t != m_name)
    1826              :         {
    1827      9419537 :           relation_kind k = VREL_EQ;
    1828      9419537 :           if (m_pe && m_bm == m_pe->members)
    1829              :             {
    1830      7433448 :               const pe_slice *equiv_pe = m_oracle->partial_equiv_set (t);
    1831      7433448 :               if (equiv_pe && equiv_pe->members == m_pe->members)
    1832      7433448 :                 k = pe_min (m_pe->code, equiv_pe->code);
    1833              :               else
    1834              :                 k = VREL_VARYING;
    1835              :             }
    1836      7433448 :           if (relation_equiv_p (k))
    1837              :             {
    1838      9419537 :               if (rel)
    1839      9419537 :                 *rel = k;
    1840      9419537 :               return t;
    1841              :             }
    1842              :         }
    1843     56063686 :       next ();
    1844              :     }
    1845              : 
    1846              :   // Process partial equivs after full equivs if both were requested.
    1847     56063648 :   if (m_pe && m_bm != m_pe->members)
    1848              :     {
    1849     50354903 :       m_bm = m_pe->members;
    1850     50354903 :       if (m_bm)
    1851              :         {
    1852              :           // Recursively call back to process First PE.
    1853      5708745 :           bmp_iter_set_init (&m_bi, m_bm, 1, &m_y);
    1854      5708745 :           return get_name (rel);
    1855              :         }
    1856              :     }
    1857              :   return NULL_TREE;
    1858              : }
    1859              : 
    1860              : #if CHECKING_P
    1861              : #include "selftest.h"
    1862              : 
    1863              : namespace selftest
    1864              : {
    1865              : void
    1866            4 : relation_tests ()
    1867              : {
    1868              :   // rr_*_table tables use unsigned char rather than relation_kind.
    1869            4 :   ASSERT_LT (VREL_LAST, UCHAR_MAX);
    1870              :   // Verify commutativity of relation_intersect and relation_union.
    1871           36 :   for (relation_kind r1 = VREL_VARYING; r1 < VREL_PE8;
    1872           32 :        r1 = relation_kind (r1 + 1))
    1873          288 :     for (relation_kind r2 = VREL_VARYING; r2 < VREL_PE8;
    1874          256 :          r2 = relation_kind (r2 + 1))
    1875              :       {
    1876          256 :         ASSERT_EQ (relation_intersect (r1, r2), relation_intersect (r2, r1));
    1877          256 :         ASSERT_EQ (relation_union (r1, r2), relation_union (r2, r1));
    1878              :       }
    1879            4 : }
    1880              : 
    1881              : } // namespace selftest
    1882              : 
    1883              : #endif // CHECKING_P
        

Generated by: LCOV version 2.4-beta

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