src/share/vm/opto/phaseX.cpp

Sat, 16 Mar 2013 07:39:14 -0700

author
morris
date
Sat, 16 Mar 2013 07:39:14 -0700
changeset 4760
96ef09c26978
parent 4657
6931f425c517
child 4868
30f42e691e70
permissions
-rw-r--r--

8009166: [parfait] Null pointer deference in hotspot/src/share/vm/opto/type.cpp
Summary: add guarantee() to as_instance_type()
Reviewed-by: kvn, twisti

     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "memory/allocation.inline.hpp"
    27 #include "opto/block.hpp"
    28 #include "opto/callnode.hpp"
    29 #include "opto/cfgnode.hpp"
    30 #include "opto/connode.hpp"
    31 #include "opto/idealGraphPrinter.hpp"
    32 #include "opto/loopnode.hpp"
    33 #include "opto/machnode.hpp"
    34 #include "opto/opcodes.hpp"
    35 #include "opto/phaseX.hpp"
    36 #include "opto/regalloc.hpp"
    37 #include "opto/rootnode.hpp"
    39 //=============================================================================
    40 #define NODE_HASH_MINIMUM_SIZE    255
    41 //------------------------------NodeHash---------------------------------------
    42 NodeHash::NodeHash(uint est_max_size) :
    43   _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ),
    44   _a(Thread::current()->resource_area()),
    45   _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ), // (Node**)_a->Amalloc(_max * sizeof(Node*)) ),
    46   _inserts(0), _insert_limit( insert_limit() ),
    47   _look_probes(0), _lookup_hits(0), _lookup_misses(0),
    48   _total_insert_probes(0), _total_inserts(0),
    49   _insert_probes(0), _grows(0) {
    50   // _sentinel must be in the current node space
    51   _sentinel = new (Compile::current()) ProjNode(NULL, TypeFunc::Control);
    52   memset(_table,0,sizeof(Node*)*_max);
    53 }
    55 //------------------------------NodeHash---------------------------------------
    56 NodeHash::NodeHash(Arena *arena, uint est_max_size) :
    57   _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ),
    58   _a(arena),
    59   _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ),
    60   _inserts(0), _insert_limit( insert_limit() ),
    61   _look_probes(0), _lookup_hits(0), _lookup_misses(0),
    62   _delete_probes(0), _delete_hits(0), _delete_misses(0),
    63   _total_insert_probes(0), _total_inserts(0),
    64   _insert_probes(0), _grows(0) {
    65   // _sentinel must be in the current node space
    66   _sentinel = new (Compile::current()) ProjNode(NULL, TypeFunc::Control);
    67   memset(_table,0,sizeof(Node*)*_max);
    68 }
    70 //------------------------------NodeHash---------------------------------------
    71 NodeHash::NodeHash(NodeHash *nh) {
    72   debug_only(_table = (Node**)badAddress);   // interact correctly w/ operator=
    73   // just copy in all the fields
    74   *this = *nh;
    75   // nh->_sentinel must be in the current node space
    76 }
    78 void NodeHash::replace_with(NodeHash *nh) {
    79   debug_only(_table = (Node**)badAddress);   // interact correctly w/ operator=
    80   // just copy in all the fields
    81   *this = *nh;
    82   // nh->_sentinel must be in the current node space
    83 }
    85 //------------------------------hash_find--------------------------------------
    86 // Find in hash table
    87 Node *NodeHash::hash_find( const Node *n ) {
    88   // ((Node*)n)->set_hash( n->hash() );
    89   uint hash = n->hash();
    90   if (hash == Node::NO_HASH) {
    91     debug_only( _lookup_misses++ );
    92     return NULL;
    93   }
    94   uint key = hash & (_max-1);
    95   uint stride = key | 0x01;
    96   debug_only( _look_probes++ );
    97   Node *k = _table[key];        // Get hashed value
    98   if( !k ) {                    // ?Miss?
    99     debug_only( _lookup_misses++ );
   100     return NULL;                // Miss!
   101   }
   103   int op = n->Opcode();
   104   uint req = n->req();
   105   while( 1 ) {                  // While probing hash table
   106     if( k->req() == req &&      // Same count of inputs
   107         k->Opcode() == op ) {   // Same Opcode
   108       for( uint i=0; i<req; i++ )
   109         if( n->in(i)!=k->in(i)) // Different inputs?
   110           goto collision;       // "goto" is a speed hack...
   111       if( n->cmp(*k) ) {        // Check for any special bits
   112         debug_only( _lookup_hits++ );
   113         return k;               // Hit!
   114       }
   115     }
   116   collision:
   117     debug_only( _look_probes++ );
   118     key = (key + stride/*7*/) & (_max-1); // Stride through table with relative prime
   119     k = _table[key];            // Get hashed value
   120     if( !k ) {                  // ?Miss?
   121       debug_only( _lookup_misses++ );
   122       return NULL;              // Miss!
   123     }
   124   }
   125   ShouldNotReachHere();
   126   return NULL;
   127 }
   129 //------------------------------hash_find_insert-------------------------------
   130 // Find in hash table, insert if not already present
   131 // Used to preserve unique entries in hash table
   132 Node *NodeHash::hash_find_insert( Node *n ) {
   133   // n->set_hash( );
   134   uint hash = n->hash();
   135   if (hash == Node::NO_HASH) {
   136     debug_only( _lookup_misses++ );
   137     return NULL;
   138   }
   139   uint key = hash & (_max-1);
   140   uint stride = key | 0x01;     // stride must be relatively prime to table siz
   141   uint first_sentinel = 0;      // replace a sentinel if seen.
   142   debug_only( _look_probes++ );
   143   Node *k = _table[key];        // Get hashed value
   144   if( !k ) {                    // ?Miss?
   145     debug_only( _lookup_misses++ );
   146     _table[key] = n;            // Insert into table!
   147     debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
   148     check_grow();               // Grow table if insert hit limit
   149     return NULL;                // Miss!
   150   }
   151   else if( k == _sentinel ) {
   152     first_sentinel = key;      // Can insert here
   153   }
   155   int op = n->Opcode();
   156   uint req = n->req();
   157   while( 1 ) {                  // While probing hash table
   158     if( k->req() == req &&      // Same count of inputs
   159         k->Opcode() == op ) {   // Same Opcode
   160       for( uint i=0; i<req; i++ )
   161         if( n->in(i)!=k->in(i)) // Different inputs?
   162           goto collision;       // "goto" is a speed hack...
   163       if( n->cmp(*k) ) {        // Check for any special bits
   164         debug_only( _lookup_hits++ );
   165         return k;               // Hit!
   166       }
   167     }
   168   collision:
   169     debug_only( _look_probes++ );
   170     key = (key + stride) & (_max-1); // Stride through table w/ relative prime
   171     k = _table[key];            // Get hashed value
   172     if( !k ) {                  // ?Miss?
   173       debug_only( _lookup_misses++ );
   174       key = (first_sentinel == 0) ? key : first_sentinel; // ?saw sentinel?
   175       _table[key] = n;          // Insert into table!
   176       debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
   177       check_grow();             // Grow table if insert hit limit
   178       return NULL;              // Miss!
   179     }
   180     else if( first_sentinel == 0 && k == _sentinel ) {
   181       first_sentinel = key;    // Can insert here
   182     }
   184   }
   185   ShouldNotReachHere();
   186   return NULL;
   187 }
   189 //------------------------------hash_insert------------------------------------
   190 // Insert into hash table
   191 void NodeHash::hash_insert( Node *n ) {
   192   // // "conflict" comments -- print nodes that conflict
   193   // bool conflict = false;
   194   // n->set_hash();
   195   uint hash = n->hash();
   196   if (hash == Node::NO_HASH) {
   197     return;
   198   }
   199   check_grow();
   200   uint key = hash & (_max-1);
   201   uint stride = key | 0x01;
   203   while( 1 ) {                  // While probing hash table
   204     debug_only( _insert_probes++ );
   205     Node *k = _table[key];      // Get hashed value
   206     if( !k || (k == _sentinel) ) break;       // Found a slot
   207     assert( k != n, "already inserted" );
   208     // if( PrintCompilation && PrintOptoStatistics && Verbose ) { tty->print("  conflict: "); k->dump(); conflict = true; }
   209     key = (key + stride) & (_max-1); // Stride through table w/ relative prime
   210   }
   211   _table[key] = n;              // Insert into table!
   212   debug_only(n->enter_hash_lock()); // Lock down the node while in the table.
   213   // if( conflict ) { n->dump(); }
   214 }
   216 //------------------------------hash_delete------------------------------------
   217 // Replace in hash table with sentinel
   218 bool NodeHash::hash_delete( const Node *n ) {
   219   Node *k;
   220   uint hash = n->hash();
   221   if (hash == Node::NO_HASH) {
   222     debug_only( _delete_misses++ );
   223     return false;
   224   }
   225   uint key = hash & (_max-1);
   226   uint stride = key | 0x01;
   227   debug_only( uint counter = 0; );
   228   for( ; /* (k != NULL) && (k != _sentinel) */; ) {
   229     debug_only( counter++ );
   230     debug_only( _delete_probes++ );
   231     k = _table[key];            // Get hashed value
   232     if( !k ) {                  // Miss?
   233       debug_only( _delete_misses++ );
   234 #ifdef ASSERT
   235       if( VerifyOpto ) {
   236         for( uint i=0; i < _max; i++ )
   237           assert( _table[i] != n, "changed edges with rehashing" );
   238       }
   239 #endif
   240       return false;             // Miss! Not in chain
   241     }
   242     else if( n == k ) {
   243       debug_only( _delete_hits++ );
   244       _table[key] = _sentinel;  // Hit! Label as deleted entry
   245       debug_only(((Node*)n)->exit_hash_lock()); // Unlock the node upon removal from table.
   246       return true;
   247     }
   248     else {
   249       // collision: move through table with prime offset
   250       key = (key + stride/*7*/) & (_max-1);
   251       assert( counter <= _insert_limit, "Cycle in hash-table");
   252     }
   253   }
   254   ShouldNotReachHere();
   255   return false;
   256 }
   258 //------------------------------round_up---------------------------------------
   259 // Round up to nearest power of 2
   260 uint NodeHash::round_up( uint x ) {
   261   x += (x>>2);                  // Add 25% slop
   262   if( x <16 ) return 16;        // Small stuff
   263   uint i=16;
   264   while( i < x ) i <<= 1;       // Double to fit
   265   return i;                     // Return hash table size
   266 }
   268 //------------------------------grow-------------------------------------------
   269 // Grow _table to next power of 2 and insert old entries
   270 void  NodeHash::grow() {
   271   // Record old state
   272   uint   old_max   = _max;
   273   Node **old_table = _table;
   274   // Construct new table with twice the space
   275   _grows++;
   276   _total_inserts       += _inserts;
   277   _total_insert_probes += _insert_probes;
   278   _inserts         = 0;
   279   _insert_probes   = 0;
   280   _max     = _max << 1;
   281   _table   = NEW_ARENA_ARRAY( _a , Node* , _max ); // (Node**)_a->Amalloc( _max * sizeof(Node*) );
   282   memset(_table,0,sizeof(Node*)*_max);
   283   _insert_limit = insert_limit();
   284   // Insert old entries into the new table
   285   for( uint i = 0; i < old_max; i++ ) {
   286     Node *m = *old_table++;
   287     if( !m || m == _sentinel ) continue;
   288     debug_only(m->exit_hash_lock()); // Unlock the node upon removal from old table.
   289     hash_insert(m);
   290   }
   291 }
   293 //------------------------------clear------------------------------------------
   294 // Clear all entries in _table to NULL but keep storage
   295 void  NodeHash::clear() {
   296 #ifdef ASSERT
   297   // Unlock all nodes upon removal from table.
   298   for (uint i = 0; i < _max; i++) {
   299     Node* n = _table[i];
   300     if (!n || n == _sentinel)  continue;
   301     n->exit_hash_lock();
   302   }
   303 #endif
   305   memset( _table, 0, _max * sizeof(Node*) );
   306 }
   308 //-----------------------remove_useless_nodes----------------------------------
   309 // Remove useless nodes from value table,
   310 // implementation does not depend on hash function
   311 void NodeHash::remove_useless_nodes(VectorSet &useful) {
   313   // Dead nodes in the hash table inherited from GVN should not replace
   314   // existing nodes, remove dead nodes.
   315   uint max = size();
   316   Node *sentinel_node = sentinel();
   317   for( uint i = 0; i < max; ++i ) {
   318     Node *n = at(i);
   319     if(n != NULL && n != sentinel_node && !useful.test(n->_idx)) {
   320       debug_only(n->exit_hash_lock()); // Unlock the node when removed
   321       _table[i] = sentinel_node;       // Replace with placeholder
   322     }
   323   }
   324 }
   326 #ifndef PRODUCT
   327 //------------------------------dump-------------------------------------------
   328 // Dump statistics for the hash table
   329 void NodeHash::dump() {
   330   _total_inserts       += _inserts;
   331   _total_insert_probes += _insert_probes;
   332   if (PrintCompilation && PrintOptoStatistics && Verbose && (_inserts > 0)) {
   333     if (WizardMode) {
   334       for (uint i=0; i<_max; i++) {
   335         if (_table[i])
   336           tty->print("%d/%d/%d ",i,_table[i]->hash()&(_max-1),_table[i]->_idx);
   337       }
   338     }
   339     tty->print("\nGVN Hash stats:  %d grows to %d max_size\n", _grows, _max);
   340     tty->print("  %d/%d (%8.1f%% full)\n", _inserts, _max, (double)_inserts/_max*100.0);
   341     tty->print("  %dp/(%dh+%dm) (%8.2f probes/lookup)\n", _look_probes, _lookup_hits, _lookup_misses, (double)_look_probes/(_lookup_hits+_lookup_misses));
   342     tty->print("  %dp/%di (%8.2f probes/insert)\n", _total_insert_probes, _total_inserts, (double)_total_insert_probes/_total_inserts);
   343     // sentinels increase lookup cost, but not insert cost
   344     assert((_lookup_misses+_lookup_hits)*4+100 >= _look_probes, "bad hash function");
   345     assert( _inserts+(_inserts>>3) < _max, "table too full" );
   346     assert( _inserts*3+100 >= _insert_probes, "bad hash function" );
   347   }
   348 }
   350 Node *NodeHash::find_index(uint idx) { // For debugging
   351   // Find an entry by its index value
   352   for( uint i = 0; i < _max; i++ ) {
   353     Node *m = _table[i];
   354     if( !m || m == _sentinel ) continue;
   355     if( m->_idx == (uint)idx ) return m;
   356   }
   357   return NULL;
   358 }
   359 #endif
   361 #ifdef ASSERT
   362 NodeHash::~NodeHash() {
   363   // Unlock all nodes upon destruction of table.
   364   if (_table != (Node**)badAddress)  clear();
   365 }
   367 void NodeHash::operator=(const NodeHash& nh) {
   368   // Unlock all nodes upon replacement of table.
   369   if (&nh == this)  return;
   370   if (_table != (Node**)badAddress)  clear();
   371   memcpy(this, &nh, sizeof(*this));
   372   // Do not increment hash_lock counts again.
   373   // Instead, be sure we never again use the source table.
   374   ((NodeHash*)&nh)->_table = (Node**)badAddress;
   375 }
   378 #endif
   381 //=============================================================================
   382 //------------------------------PhaseRemoveUseless-----------------------------
   383 // 1) Use a breadthfirst walk to collect useful nodes reachable from root.
   384 PhaseRemoveUseless::PhaseRemoveUseless( PhaseGVN *gvn, Unique_Node_List *worklist ) : Phase(Remove_Useless),
   385   _useful(Thread::current()->resource_area()) {
   387   // Implementation requires 'UseLoopSafepoints == true' and an edge from root
   388   // to each SafePointNode at a backward branch.  Inserted in add_safepoint().
   389   if( !UseLoopSafepoints || !OptoRemoveUseless ) return;
   391   // Identify nodes that are reachable from below, useful.
   392   C->identify_useful_nodes(_useful);
   393   // Update dead node list
   394   C->update_dead_node_list(_useful);
   396   // Remove all useless nodes from PhaseValues' recorded types
   397   // Must be done before disconnecting nodes to preserve hash-table-invariant
   398   gvn->remove_useless_nodes(_useful.member_set());
   400   // Remove all useless nodes from future worklist
   401   worklist->remove_useless_nodes(_useful.member_set());
   403   // Disconnect 'useless' nodes that are adjacent to useful nodes
   404   C->remove_useless_nodes(_useful);
   406   // Remove edges from "root" to each SafePoint at a backward branch.
   407   // They were inserted during parsing (see add_safepoint()) to make infinite
   408   // loops without calls or exceptions visible to root, i.e., useful.
   409   Node *root = C->root();
   410   if( root != NULL ) {
   411     for( uint i = root->req(); i < root->len(); ++i ) {
   412       Node *n = root->in(i);
   413       if( n != NULL && n->is_SafePoint() ) {
   414         root->rm_prec(i);
   415         --i;
   416       }
   417     }
   418   }
   419 }
   422 //=============================================================================
   423 //------------------------------PhaseTransform---------------------------------
   424 PhaseTransform::PhaseTransform( PhaseNumber pnum ) : Phase(pnum),
   425   _arena(Thread::current()->resource_area()),
   426   _nodes(_arena),
   427   _types(_arena)
   428 {
   429   init_con_caches();
   430 #ifndef PRODUCT
   431   clear_progress();
   432   clear_transforms();
   433   set_allow_progress(true);
   434 #endif
   435   // Force allocation for currently existing nodes
   436   _types.map(C->unique(), NULL);
   437 }
   439 //------------------------------PhaseTransform---------------------------------
   440 PhaseTransform::PhaseTransform( Arena *arena, PhaseNumber pnum ) : Phase(pnum),
   441   _arena(arena),
   442   _nodes(arena),
   443   _types(arena)
   444 {
   445   init_con_caches();
   446 #ifndef PRODUCT
   447   clear_progress();
   448   clear_transforms();
   449   set_allow_progress(true);
   450 #endif
   451   // Force allocation for currently existing nodes
   452   _types.map(C->unique(), NULL);
   453 }
   455 //------------------------------PhaseTransform---------------------------------
   456 // Initialize with previously generated type information
   457 PhaseTransform::PhaseTransform( PhaseTransform *pt, PhaseNumber pnum ) : Phase(pnum),
   458   _arena(pt->_arena),
   459   _nodes(pt->_nodes),
   460   _types(pt->_types)
   461 {
   462   init_con_caches();
   463 #ifndef PRODUCT
   464   clear_progress();
   465   clear_transforms();
   466   set_allow_progress(true);
   467 #endif
   468 }
   470 void PhaseTransform::init_con_caches() {
   471   memset(_icons,0,sizeof(_icons));
   472   memset(_lcons,0,sizeof(_lcons));
   473   memset(_zcons,0,sizeof(_zcons));
   474 }
   477 //--------------------------------find_int_type--------------------------------
   478 const TypeInt* PhaseTransform::find_int_type(Node* n) {
   479   if (n == NULL)  return NULL;
   480   // Call type_or_null(n) to determine node's type since we might be in
   481   // parse phase and call n->Value() may return wrong type.
   482   // (For example, a phi node at the beginning of loop parsing is not ready.)
   483   const Type* t = type_or_null(n);
   484   if (t == NULL)  return NULL;
   485   return t->isa_int();
   486 }
   489 //-------------------------------find_long_type--------------------------------
   490 const TypeLong* PhaseTransform::find_long_type(Node* n) {
   491   if (n == NULL)  return NULL;
   492   // (See comment above on type_or_null.)
   493   const Type* t = type_or_null(n);
   494   if (t == NULL)  return NULL;
   495   return t->isa_long();
   496 }
   499 #ifndef PRODUCT
   500 void PhaseTransform::dump_old2new_map() const {
   501   _nodes.dump();
   502 }
   504 void PhaseTransform::dump_new( uint nidx ) const {
   505   for( uint i=0; i<_nodes.Size(); i++ )
   506     if( _nodes[i] && _nodes[i]->_idx == nidx ) {
   507       _nodes[i]->dump();
   508       tty->cr();
   509       tty->print_cr("Old index= %d",i);
   510       return;
   511     }
   512   tty->print_cr("Node %d not found in the new indices", nidx);
   513 }
   515 //------------------------------dump_types-------------------------------------
   516 void PhaseTransform::dump_types( ) const {
   517   _types.dump();
   518 }
   520 //------------------------------dump_nodes_and_types---------------------------
   521 void PhaseTransform::dump_nodes_and_types(const Node *root, uint depth, bool only_ctrl) {
   522   VectorSet visited(Thread::current()->resource_area());
   523   dump_nodes_and_types_recur( root, depth, only_ctrl, visited );
   524 }
   526 //------------------------------dump_nodes_and_types_recur---------------------
   527 void PhaseTransform::dump_nodes_and_types_recur( const Node *n, uint depth, bool only_ctrl, VectorSet &visited) {
   528   if( !n ) return;
   529   if( depth == 0 ) return;
   530   if( visited.test_set(n->_idx) ) return;
   531   for( uint i=0; i<n->len(); i++ ) {
   532     if( only_ctrl && !(n->is_Region()) && i != TypeFunc::Control ) continue;
   533     dump_nodes_and_types_recur( n->in(i), depth-1, only_ctrl, visited );
   534   }
   535   n->dump();
   536   if (type_or_null(n) != NULL) {
   537     tty->print("      "); type(n)->dump(); tty->cr();
   538   }
   539 }
   541 #endif
   544 //=============================================================================
   545 //------------------------------PhaseValues------------------------------------
   546 // Set minimum table size to "255"
   547 PhaseValues::PhaseValues( Arena *arena, uint est_max_size ) : PhaseTransform(arena, GVN), _table(arena, est_max_size) {
   548   NOT_PRODUCT( clear_new_values(); )
   549 }
   551 //------------------------------PhaseValues------------------------------------
   552 // Set minimum table size to "255"
   553 PhaseValues::PhaseValues( PhaseValues *ptv ) : PhaseTransform( ptv, GVN ),
   554   _table(&ptv->_table) {
   555   NOT_PRODUCT( clear_new_values(); )
   556 }
   558 //------------------------------PhaseValues------------------------------------
   559 // Used by +VerifyOpto.  Clear out hash table but copy _types array.
   560 PhaseValues::PhaseValues( PhaseValues *ptv, const char *dummy ) : PhaseTransform( ptv, GVN ),
   561   _table(ptv->arena(),ptv->_table.size()) {
   562   NOT_PRODUCT( clear_new_values(); )
   563 }
   565 //------------------------------~PhaseValues-----------------------------------
   566 #ifndef PRODUCT
   567 PhaseValues::~PhaseValues() {
   568   _table.dump();
   570   // Statistics for value progress and efficiency
   571   if( PrintCompilation && Verbose && WizardMode ) {
   572     tty->print("\n%sValues: %d nodes ---> %d/%d (%d)",
   573       is_IterGVN() ? "Iter" : "    ", C->unique(), made_progress(), made_transforms(), made_new_values());
   574     if( made_transforms() != 0 ) {
   575       tty->print_cr("  ratio %f", made_progress()/(float)made_transforms() );
   576     } else {
   577       tty->cr();
   578     }
   579   }
   580 }
   581 #endif
   583 //------------------------------makecon----------------------------------------
   584 ConNode* PhaseTransform::makecon(const Type *t) {
   585   assert(t->singleton(), "must be a constant");
   586   assert(!t->empty() || t == Type::TOP, "must not be vacuous range");
   587   switch (t->base()) {  // fast paths
   588   case Type::Half:
   589   case Type::Top:  return (ConNode*) C->top();
   590   case Type::Int:  return intcon( t->is_int()->get_con() );
   591   case Type::Long: return longcon( t->is_long()->get_con() );
   592   }
   593   if (t->is_zero_type())
   594     return zerocon(t->basic_type());
   595   return uncached_makecon(t);
   596 }
   598 //--------------------------uncached_makecon-----------------------------------
   599 // Make an idealized constant - one of ConINode, ConPNode, etc.
   600 ConNode* PhaseValues::uncached_makecon(const Type *t) {
   601   assert(t->singleton(), "must be a constant");
   602   ConNode* x = ConNode::make(C, t);
   603   ConNode* k = (ConNode*)hash_find_insert(x); // Value numbering
   604   if (k == NULL) {
   605     set_type(x, t);             // Missed, provide type mapping
   606     GrowableArray<Node_Notes*>* nna = C->node_note_array();
   607     if (nna != NULL) {
   608       Node_Notes* loc = C->locate_node_notes(nna, x->_idx, true);
   609       loc->clear(); // do not put debug info on constants
   610     }
   611   } else {
   612     x->destruct();              // Hit, destroy duplicate constant
   613     x = k;                      // use existing constant
   614   }
   615   return x;
   616 }
   618 //------------------------------intcon-----------------------------------------
   619 // Fast integer constant.  Same as "transform(new ConINode(TypeInt::make(i)))"
   620 ConINode* PhaseTransform::intcon(int i) {
   621   // Small integer?  Check cache! Check that cached node is not dead
   622   if (i >= _icon_min && i <= _icon_max) {
   623     ConINode* icon = _icons[i-_icon_min];
   624     if (icon != NULL && icon->in(TypeFunc::Control) != NULL)
   625       return icon;
   626   }
   627   ConINode* icon = (ConINode*) uncached_makecon(TypeInt::make(i));
   628   assert(icon->is_Con(), "");
   629   if (i >= _icon_min && i <= _icon_max)
   630     _icons[i-_icon_min] = icon;   // Cache small integers
   631   return icon;
   632 }
   634 //------------------------------longcon----------------------------------------
   635 // Fast long constant.
   636 ConLNode* PhaseTransform::longcon(jlong l) {
   637   // Small integer?  Check cache! Check that cached node is not dead
   638   if (l >= _lcon_min && l <= _lcon_max) {
   639     ConLNode* lcon = _lcons[l-_lcon_min];
   640     if (lcon != NULL && lcon->in(TypeFunc::Control) != NULL)
   641       return lcon;
   642   }
   643   ConLNode* lcon = (ConLNode*) uncached_makecon(TypeLong::make(l));
   644   assert(lcon->is_Con(), "");
   645   if (l >= _lcon_min && l <= _lcon_max)
   646     _lcons[l-_lcon_min] = lcon;      // Cache small integers
   647   return lcon;
   648 }
   650 //------------------------------zerocon-----------------------------------------
   651 // Fast zero or null constant. Same as "transform(ConNode::make(Type::get_zero_type(bt)))"
   652 ConNode* PhaseTransform::zerocon(BasicType bt) {
   653   assert((uint)bt <= _zcon_max, "domain check");
   654   ConNode* zcon = _zcons[bt];
   655   if (zcon != NULL && zcon->in(TypeFunc::Control) != NULL)
   656     return zcon;
   657   zcon = (ConNode*) uncached_makecon(Type::get_zero_type(bt));
   658   _zcons[bt] = zcon;
   659   return zcon;
   660 }
   664 //=============================================================================
   665 //------------------------------transform--------------------------------------
   666 // Return a node which computes the same function as this node, but in a
   667 // faster or cheaper fashion.
   668 Node *PhaseGVN::transform( Node *n ) {
   669   return transform_no_reclaim(n);
   670 }
   672 //------------------------------transform--------------------------------------
   673 // Return a node which computes the same function as this node, but
   674 // in a faster or cheaper fashion.
   675 Node *PhaseGVN::transform_no_reclaim( Node *n ) {
   676   NOT_PRODUCT( set_transforms(); )
   678   // Apply the Ideal call in a loop until it no longer applies
   679   Node *k = n;
   680   NOT_PRODUCT( uint loop_count = 0; )
   681   while( 1 ) {
   682     Node *i = k->Ideal(this, /*can_reshape=*/false);
   683     if( !i ) break;
   684     assert( i->_idx >= k->_idx, "Idealize should return new nodes, use Identity to return old nodes" );
   685     k = i;
   686     assert(loop_count++ < K, "infinite loop in PhaseGVN::transform");
   687   }
   688   NOT_PRODUCT( if( loop_count != 0 ) { set_progress(); } )
   691   // If brand new node, make space in type array.
   692   ensure_type_or_null(k);
   694   // Since I just called 'Value' to compute the set of run-time values
   695   // for this Node, and 'Value' is non-local (and therefore expensive) I'll
   696   // cache Value.  Later requests for the local phase->type of this Node can
   697   // use the cached Value instead of suffering with 'bottom_type'.
   698   const Type *t = k->Value(this); // Get runtime Value set
   699   assert(t != NULL, "value sanity");
   700   if (type_or_null(k) != t) {
   701 #ifndef PRODUCT
   702     // Do not count initial visit to node as a transformation
   703     if (type_or_null(k) == NULL) {
   704       inc_new_values();
   705       set_progress();
   706     }
   707 #endif
   708     set_type(k, t);
   709     // If k is a TypeNode, capture any more-precise type permanently into Node
   710     k->raise_bottom_type(t);
   711   }
   713   if( t->singleton() && !k->is_Con() ) {
   714     NOT_PRODUCT( set_progress(); )
   715     return makecon(t);          // Turn into a constant
   716   }
   718   // Now check for Identities
   719   Node *i = k->Identity(this);  // Look for a nearby replacement
   720   if( i != k ) {                // Found? Return replacement!
   721     NOT_PRODUCT( set_progress(); )
   722     return i;
   723   }
   725   // Global Value Numbering
   726   i = hash_find_insert(k);      // Insert if new
   727   if( i && (i != k) ) {
   728     // Return the pre-existing node
   729     NOT_PRODUCT( set_progress(); )
   730     return i;
   731   }
   733   // Return Idealized original
   734   return k;
   735 }
   737 #ifdef ASSERT
   738 //------------------------------dead_loop_check--------------------------------
   739 // Check for a simple dead loop when a data node references itself directly
   740 // or through an other data node excluding cons and phis.
   741 void PhaseGVN::dead_loop_check( Node *n ) {
   742   // Phi may reference itself in a loop
   743   if (n != NULL && !n->is_dead_loop_safe() && !n->is_CFG()) {
   744     // Do 2 levels check and only data inputs.
   745     bool no_dead_loop = true;
   746     uint cnt = n->req();
   747     for (uint i = 1; i < cnt && no_dead_loop; i++) {
   748       Node *in = n->in(i);
   749       if (in == n) {
   750         no_dead_loop = false;
   751       } else if (in != NULL && !in->is_dead_loop_safe()) {
   752         uint icnt = in->req();
   753         for (uint j = 1; j < icnt && no_dead_loop; j++) {
   754           if (in->in(j) == n || in->in(j) == in)
   755             no_dead_loop = false;
   756         }
   757       }
   758     }
   759     if (!no_dead_loop) n->dump(3);
   760     assert(no_dead_loop, "dead loop detected");
   761   }
   762 }
   763 #endif
   765 //=============================================================================
   766 //------------------------------PhaseIterGVN-----------------------------------
   767 // Initialize hash table to fresh and clean for +VerifyOpto
   768 PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn, const char *dummy ) : PhaseGVN(igvn,dummy), _worklist( ),
   769                                                                       _stack(C->unique() >> 1),
   770                                                                       _delay_transform(false) {
   771 }
   773 //------------------------------PhaseIterGVN-----------------------------------
   774 // Initialize with previous PhaseIterGVN info; used by PhaseCCP
   775 PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn ) : PhaseGVN(igvn),
   776                                                    _worklist( igvn->_worklist ),
   777                                                    _stack( igvn->_stack ),
   778                                                    _delay_transform(igvn->_delay_transform)
   779 {
   780 }
   782 //------------------------------PhaseIterGVN-----------------------------------
   783 // Initialize with previous PhaseGVN info from Parser
   784 PhaseIterGVN::PhaseIterGVN( PhaseGVN *gvn ) : PhaseGVN(gvn),
   785                                               _worklist(*C->for_igvn()),
   786                                               _stack(C->unique() >> 1),
   787                                               _delay_transform(false)
   788 {
   789   uint max;
   791   // Dead nodes in the hash table inherited from GVN were not treated as
   792   // roots during def-use info creation; hence they represent an invisible
   793   // use.  Clear them out.
   794   max = _table.size();
   795   for( uint i = 0; i < max; ++i ) {
   796     Node *n = _table.at(i);
   797     if(n != NULL && n != _table.sentinel() && n->outcnt() == 0) {
   798       if( n->is_top() ) continue;
   799       assert( false, "Parse::remove_useless_nodes missed this node");
   800       hash_delete(n);
   801     }
   802   }
   804   // Any Phis or Regions on the worklist probably had uses that could not
   805   // make more progress because the uses were made while the Phis and Regions
   806   // were in half-built states.  Put all uses of Phis and Regions on worklist.
   807   max = _worklist.size();
   808   for( uint j = 0; j < max; j++ ) {
   809     Node *n = _worklist.at(j);
   810     uint uop = n->Opcode();
   811     if( uop == Op_Phi || uop == Op_Region ||
   812         n->is_Type() ||
   813         n->is_Mem() )
   814       add_users_to_worklist(n);
   815   }
   816 }
   819 #ifndef PRODUCT
   820 void PhaseIterGVN::verify_step(Node* n) {
   821   _verify_window[_verify_counter % _verify_window_size] = n;
   822   ++_verify_counter;
   823   ResourceMark rm;
   824   ResourceArea *area = Thread::current()->resource_area();
   825   VectorSet old_space(area), new_space(area);
   826   if (C->unique() < 1000 ||
   827       0 == _verify_counter % (C->unique() < 10000 ? 10 : 100)) {
   828     ++_verify_full_passes;
   829     Node::verify_recur(C->root(), -1, old_space, new_space);
   830   }
   831   const int verify_depth = 4;
   832   for ( int i = 0; i < _verify_window_size; i++ ) {
   833     Node* n = _verify_window[i];
   834     if ( n == NULL )  continue;
   835     if( n->in(0) == NodeSentinel ) {  // xform_idom
   836       _verify_window[i] = n->in(1);
   837       --i; continue;
   838     }
   839     // Typical fanout is 1-2, so this call visits about 6 nodes.
   840     Node::verify_recur(n, verify_depth, old_space, new_space);
   841   }
   842 }
   843 #endif
   846 //------------------------------init_worklist----------------------------------
   847 // Initialize worklist for each node.
   848 void PhaseIterGVN::init_worklist( Node *n ) {
   849   if( _worklist.member(n) ) return;
   850   _worklist.push(n);
   851   uint cnt = n->req();
   852   for( uint i =0 ; i < cnt; i++ ) {
   853     Node *m = n->in(i);
   854     if( m ) init_worklist(m);
   855   }
   856 }
   858 //------------------------------optimize---------------------------------------
   859 void PhaseIterGVN::optimize() {
   860   debug_only(uint num_processed  = 0;);
   861 #ifndef PRODUCT
   862   {
   863     _verify_counter = 0;
   864     _verify_full_passes = 0;
   865     for ( int i = 0; i < _verify_window_size; i++ ) {
   866       _verify_window[i] = NULL;
   867     }
   868   }
   869 #endif
   871 #ifdef ASSERT
   872   Node* prev = NULL;
   873   uint rep_cnt = 0;
   874 #endif
   875   uint loop_count = 0;
   877   // Pull from worklist; transform node;
   878   // If node has changed: update edge info and put uses on worklist.
   879   while( _worklist.size() ) {
   880     if (C->check_node_count(NodeLimitFudgeFactor * 2,
   881                             "out of nodes optimizing method")) {
   882       return;
   883     }
   884     Node *n  = _worklist.pop();
   885     if (++loop_count >= K * C->unique()) {
   886       debug_only(n->dump(4);)
   887       assert(false, "infinite loop in PhaseIterGVN::optimize");
   888       C->record_method_not_compilable("infinite loop in PhaseIterGVN::optimize");
   889       return;
   890     }
   891 #ifdef ASSERT
   892     if (n == prev) {
   893       if (++rep_cnt > 3) {
   894         n->dump(4);
   895         assert(false, "loop in Ideal transformation");
   896       }
   897     } else {
   898       rep_cnt = 0;
   899     }
   900     prev = n;
   901 #endif
   902     if (TraceIterativeGVN && Verbose) {
   903       tty->print("  Pop ");
   904       NOT_PRODUCT( n->dump(); )
   905       debug_only(if( (num_processed++ % 100) == 0 ) _worklist.print_set();)
   906     }
   908     if (n->outcnt() != 0) {
   910 #ifndef PRODUCT
   911       uint wlsize = _worklist.size();
   912       const Type* oldtype = type_or_null(n);
   913 #endif //PRODUCT
   915       Node *nn = transform_old(n);
   917 #ifndef PRODUCT
   918       if (TraceIterativeGVN) {
   919         const Type* newtype = type_or_null(n);
   920         if (nn != n) {
   921           // print old node
   922           tty->print("< ");
   923           if (oldtype != newtype && oldtype != NULL) {
   924             oldtype->dump();
   925           }
   926           do { tty->print("\t"); } while (tty->position() < 16);
   927           tty->print("<");
   928           n->dump();
   929         }
   930         if (oldtype != newtype || nn != n) {
   931           // print new node and/or new type
   932           if (oldtype == NULL) {
   933             tty->print("* ");
   934           } else if (nn != n) {
   935             tty->print("> ");
   936           } else {
   937             tty->print("= ");
   938           }
   939           if (newtype == NULL) {
   940             tty->print("null");
   941           } else {
   942             newtype->dump();
   943           }
   944           do { tty->print("\t"); } while (tty->position() < 16);
   945           nn->dump();
   946         }
   947         if (Verbose && wlsize < _worklist.size()) {
   948           tty->print("  Push {");
   949           while (wlsize != _worklist.size()) {
   950             Node* pushed = _worklist.at(wlsize++);
   951             tty->print(" %d", pushed->_idx);
   952           }
   953           tty->print_cr(" }");
   954         }
   955       }
   956       if( VerifyIterativeGVN && nn != n ) {
   957         verify_step((Node*) NULL);  // ignore n, it might be subsumed
   958       }
   959 #endif
   960     } else if (!n->is_top()) {
   961       remove_dead_node(n);
   962     }
   963   }
   965 #ifndef PRODUCT
   966   C->verify_graph_edges();
   967   if( VerifyOpto && allow_progress() ) {
   968     // Must turn off allow_progress to enable assert and break recursion
   969     C->root()->verify();
   970     { // Check if any progress was missed using IterGVN
   971       // Def-Use info enables transformations not attempted in wash-pass
   972       // e.g. Region/Phi cleanup, ...
   973       // Null-check elision -- may not have reached fixpoint
   974       //                       do not propagate to dominated nodes
   975       ResourceMark rm;
   976       PhaseIterGVN igvn2(this,"Verify"); // Fresh and clean!
   977       // Fill worklist completely
   978       igvn2.init_worklist(C->root());
   980       igvn2.set_allow_progress(false);
   981       igvn2.optimize();
   982       igvn2.set_allow_progress(true);
   983     }
   984   }
   985   if ( VerifyIterativeGVN && PrintOpto ) {
   986     if ( _verify_counter == _verify_full_passes )
   987       tty->print_cr("VerifyIterativeGVN: %d transforms and verify passes",
   988                     _verify_full_passes);
   989     else
   990       tty->print_cr("VerifyIterativeGVN: %d transforms, %d full verify passes",
   991                   _verify_counter, _verify_full_passes);
   992   }
   993 #endif
   994 }
   997 //------------------register_new_node_with_optimizer---------------------------
   998 // Register a new node with the optimizer.  Update the types array, the def-use
   999 // info.  Put on worklist.
  1000 Node* PhaseIterGVN::register_new_node_with_optimizer(Node* n, Node* orig) {
  1001   set_type_bottom(n);
  1002   _worklist.push(n);
  1003   if (orig != NULL)  C->copy_node_notes_to(n, orig);
  1004   return n;
  1007 //------------------------------transform--------------------------------------
  1008 // Non-recursive: idealize Node 'n' with respect to its inputs and its value
  1009 Node *PhaseIterGVN::transform( Node *n ) {
  1010   if (_delay_transform) {
  1011     // Register the node but don't optimize for now
  1012     register_new_node_with_optimizer(n);
  1013     return n;
  1016   // If brand new node, make space in type array, and give it a type.
  1017   ensure_type_or_null(n);
  1018   if (type_or_null(n) == NULL) {
  1019     set_type_bottom(n);
  1022   return transform_old(n);
  1025 //------------------------------transform_old----------------------------------
  1026 Node *PhaseIterGVN::transform_old( Node *n ) {
  1027 #ifndef PRODUCT
  1028   debug_only(uint loop_count = 0;);
  1029   set_transforms();
  1030 #endif
  1031   // Remove 'n' from hash table in case it gets modified
  1032   _table.hash_delete(n);
  1033   if( VerifyIterativeGVN ) {
  1034    assert( !_table.find_index(n->_idx), "found duplicate entry in table");
  1037   // Apply the Ideal call in a loop until it no longer applies
  1038   Node *k = n;
  1039   DEBUG_ONLY(dead_loop_check(k);)
  1040   DEBUG_ONLY(bool is_new = (k->outcnt() == 0);)
  1041   Node *i = k->Ideal(this, /*can_reshape=*/true);
  1042   assert(i != k || is_new || i->outcnt() > 0, "don't return dead nodes");
  1043 #ifndef PRODUCT
  1044   if( VerifyIterativeGVN )
  1045     verify_step(k);
  1046   if( i && VerifyOpto ) {
  1047     if( !allow_progress() ) {
  1048       if (i->is_Add() && i->outcnt() == 1) {
  1049         // Switched input to left side because this is the only use
  1050       } else if( i->is_If() && (i->in(0) == NULL) ) {
  1051         // This IF is dead because it is dominated by an equivalent IF When
  1052         // dominating if changed, info is not propagated sparsely to 'this'
  1053         // Propagating this info further will spuriously identify other
  1054         // progress.
  1055         return i;
  1056       } else
  1057         set_progress();
  1058     } else
  1059       set_progress();
  1061 #endif
  1063   while( i ) {
  1064 #ifndef PRODUCT
  1065     debug_only( if( loop_count >= K ) i->dump(4); )
  1066     assert(loop_count < K, "infinite loop in PhaseIterGVN::transform");
  1067     debug_only( loop_count++; )
  1068 #endif
  1069     assert((i->_idx >= k->_idx) || i->is_top(), "Idealize should return new nodes, use Identity to return old nodes");
  1070     // Made a change; put users of original Node on worklist
  1071     add_users_to_worklist( k );
  1072     // Replacing root of transform tree?
  1073     if( k != i ) {
  1074       // Make users of old Node now use new.
  1075       subsume_node( k, i );
  1076       k = i;
  1078     DEBUG_ONLY(dead_loop_check(k);)
  1079     // Try idealizing again
  1080     DEBUG_ONLY(is_new = (k->outcnt() == 0);)
  1081     i = k->Ideal(this, /*can_reshape=*/true);
  1082     assert(i != k || is_new || i->outcnt() > 0, "don't return dead nodes");
  1083 #ifndef PRODUCT
  1084     if( VerifyIterativeGVN )
  1085       verify_step(k);
  1086     if( i && VerifyOpto ) set_progress();
  1087 #endif
  1090   // If brand new node, make space in type array.
  1091   ensure_type_or_null(k);
  1093   // See what kind of values 'k' takes on at runtime
  1094   const Type *t = k->Value(this);
  1095   assert(t != NULL, "value sanity");
  1097   // Since I just called 'Value' to compute the set of run-time values
  1098   // for this Node, and 'Value' is non-local (and therefore expensive) I'll
  1099   // cache Value.  Later requests for the local phase->type of this Node can
  1100   // use the cached Value instead of suffering with 'bottom_type'.
  1101   if (t != type_or_null(k)) {
  1102     NOT_PRODUCT( set_progress(); )
  1103     NOT_PRODUCT( inc_new_values();)
  1104     set_type(k, t);
  1105     // If k is a TypeNode, capture any more-precise type permanently into Node
  1106     k->raise_bottom_type(t);
  1107     // Move users of node to worklist
  1108     add_users_to_worklist( k );
  1111   // If 'k' computes a constant, replace it with a constant
  1112   if( t->singleton() && !k->is_Con() ) {
  1113     NOT_PRODUCT( set_progress(); )
  1114     Node *con = makecon(t);     // Make a constant
  1115     add_users_to_worklist( k );
  1116     subsume_node( k, con );     // Everybody using k now uses con
  1117     return con;
  1120   // Now check for Identities
  1121   i = k->Identity(this);        // Look for a nearby replacement
  1122   if( i != k ) {                // Found? Return replacement!
  1123     NOT_PRODUCT( set_progress(); )
  1124     add_users_to_worklist( k );
  1125     subsume_node( k, i );       // Everybody using k now uses i
  1126     return i;
  1129   // Global Value Numbering
  1130   i = hash_find_insert(k);      // Check for pre-existing node
  1131   if( i && (i != k) ) {
  1132     // Return the pre-existing node if it isn't dead
  1133     NOT_PRODUCT( set_progress(); )
  1134     add_users_to_worklist( k );
  1135     subsume_node( k, i );       // Everybody using k now uses i
  1136     return i;
  1139   // Return Idealized original
  1140   return k;
  1143 //---------------------------------saturate------------------------------------
  1144 const Type* PhaseIterGVN::saturate(const Type* new_type, const Type* old_type,
  1145                                    const Type* limit_type) const {
  1146   return new_type->narrow(old_type);
  1149 //------------------------------remove_globally_dead_node----------------------
  1150 // Kill a globally dead Node.  All uses are also globally dead and are
  1151 // aggressively trimmed.
  1152 void PhaseIterGVN::remove_globally_dead_node( Node *dead ) {
  1153   enum DeleteProgress {
  1154     PROCESS_INPUTS,
  1155     PROCESS_OUTPUTS
  1156   };
  1157   assert(_stack.is_empty(), "not empty");
  1158   _stack.push(dead, PROCESS_INPUTS);
  1160   while (_stack.is_nonempty()) {
  1161     dead = _stack.node();
  1162     uint progress_state = _stack.index();
  1163     assert(dead != C->root(), "killing root, eh?");
  1164     assert(!dead->is_top(), "add check for top when pushing");
  1165     NOT_PRODUCT( set_progress(); )
  1166     if (progress_state == PROCESS_INPUTS) {
  1167       // After following inputs, continue to outputs
  1168       _stack.set_index(PROCESS_OUTPUTS);
  1169       // Remove from iterative worklist
  1170       _worklist.remove(dead);
  1171       if (!dead->is_Con()) { // Don't kill cons but uses
  1172         bool recurse = false;
  1173         // Remove from hash table
  1174         _table.hash_delete( dead );
  1175         // Smash all inputs to 'dead', isolating him completely
  1176         for( uint i = 0; i < dead->req(); i++ ) {
  1177           Node *in = dead->in(i);
  1178           if( in ) {                 // Points to something?
  1179             dead->set_req(i,NULL);  // Kill the edge
  1180             if (in->outcnt() == 0 && in != C->top()) {// Made input go dead?
  1181               _stack.push(in, PROCESS_INPUTS); // Recursively remove
  1182               recurse = true;
  1183             } else if (in->outcnt() == 1 &&
  1184                        in->has_special_unique_user()) {
  1185               _worklist.push(in->unique_out());
  1186             } else if (in->outcnt() <= 2 && dead->is_Phi()) {
  1187               if( in->Opcode() == Op_Region )
  1188                 _worklist.push(in);
  1189               else if( in->is_Store() ) {
  1190                 DUIterator_Fast imax, i = in->fast_outs(imax);
  1191                 _worklist.push(in->fast_out(i));
  1192                 i++;
  1193                 if(in->outcnt() == 2) {
  1194                   _worklist.push(in->fast_out(i));
  1195                   i++;
  1197                 assert(!(i < imax), "sanity");
  1200             if (ReduceFieldZeroing && dead->is_Load() && i == MemNode::Memory &&
  1201                 in->is_Proj() && in->in(0) != NULL && in->in(0)->is_Initialize()) {
  1202               // A Load that directly follows an InitializeNode is
  1203               // going away. The Stores that follow are candidates
  1204               // again to be captured by the InitializeNode.
  1205               for (DUIterator_Fast jmax, j = in->fast_outs(jmax); j < jmax; j++) {
  1206                 Node *n = in->fast_out(j);
  1207                 if (n->is_Store()) {
  1208                   _worklist.push(n);
  1214         C->record_dead_node(dead->_idx);
  1215         if (dead->is_macro()) {
  1216           C->remove_macro_node(dead);
  1218         if (dead->is_expensive()) {
  1219           C->remove_expensive_node(dead);
  1222         if (recurse) {
  1223           continue;
  1226       // Constant node that has no out-edges and has only one in-edge from
  1227       // root is usually dead. However, sometimes reshaping walk makes
  1228       // it reachable by adding use edges. So, we will NOT count Con nodes
  1229       // as dead to be conservative about the dead node count at any
  1230       // given time.
  1233     // Aggressively kill globally dead uses
  1234     // (Rather than pushing all the outs at once, we push one at a time,
  1235     // plus the parent to resume later, because of the indefinite number
  1236     // of edge deletions per loop trip.)
  1237     if (dead->outcnt() > 0) {
  1238       // Recursively remove
  1239       _stack.push(dead->raw_out(0), PROCESS_INPUTS);
  1240     } else {
  1241       _stack.pop();
  1246 //------------------------------subsume_node-----------------------------------
  1247 // Remove users from node 'old' and add them to node 'nn'.
  1248 void PhaseIterGVN::subsume_node( Node *old, Node *nn ) {
  1249   assert( old != hash_find(old), "should already been removed" );
  1250   assert( old != C->top(), "cannot subsume top node");
  1251   // Copy debug or profile information to the new version:
  1252   C->copy_node_notes_to(nn, old);
  1253   // Move users of node 'old' to node 'nn'
  1254   for (DUIterator_Last imin, i = old->last_outs(imin); i >= imin; ) {
  1255     Node* use = old->last_out(i);  // for each use...
  1256     // use might need re-hashing (but it won't if it's a new node)
  1257     bool is_in_table = _table.hash_delete( use );
  1258     // Update use-def info as well
  1259     // We remove all occurrences of old within use->in,
  1260     // so as to avoid rehashing any node more than once.
  1261     // The hash table probe swamps any outer loop overhead.
  1262     uint num_edges = 0;
  1263     for (uint jmax = use->len(), j = 0; j < jmax; j++) {
  1264       if (use->in(j) == old) {
  1265         use->set_req(j, nn);
  1266         ++num_edges;
  1269     // Insert into GVN hash table if unique
  1270     // If a duplicate, 'use' will be cleaned up when pulled off worklist
  1271     if( is_in_table ) {
  1272       hash_find_insert(use);
  1274     i -= num_edges;    // we deleted 1 or more copies of this edge
  1277   // Smash all inputs to 'old', isolating him completely
  1278   Node *temp = new (C) Node(1);
  1279   temp->init_req(0,nn);     // Add a use to nn to prevent him from dying
  1280   remove_dead_node( old );
  1281   temp->del_req(0);         // Yank bogus edge
  1282 #ifndef PRODUCT
  1283   if( VerifyIterativeGVN ) {
  1284     for ( int i = 0; i < _verify_window_size; i++ ) {
  1285       if ( _verify_window[i] == old )
  1286         _verify_window[i] = nn;
  1289 #endif
  1290   _worklist.remove(temp);   // this can be necessary
  1291   temp->destruct();         // reuse the _idx of this little guy
  1294 //------------------------------add_users_to_worklist--------------------------
  1295 void PhaseIterGVN::add_users_to_worklist0( Node *n ) {
  1296   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1297     _worklist.push(n->fast_out(i));  // Push on worklist
  1301 void PhaseIterGVN::add_users_to_worklist( Node *n ) {
  1302   add_users_to_worklist0(n);
  1304   // Move users of node to worklist
  1305   for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1306     Node* use = n->fast_out(i); // Get use
  1308     if( use->is_Multi() ||      // Multi-definer?  Push projs on worklist
  1309         use->is_Store() )       // Enable store/load same address
  1310       add_users_to_worklist0(use);
  1312     // If we changed the receiver type to a call, we need to revisit
  1313     // the Catch following the call.  It's looking for a non-NULL
  1314     // receiver to know when to enable the regular fall-through path
  1315     // in addition to the NullPtrException path.
  1316     if (use->is_CallDynamicJava() && n == use->in(TypeFunc::Parms)) {
  1317       Node* p = use->as_CallDynamicJava()->proj_out(TypeFunc::Control);
  1318       if (p != NULL) {
  1319         add_users_to_worklist0(p);
  1323     if( use->is_Cmp() ) {       // Enable CMP/BOOL optimization
  1324       add_users_to_worklist(use); // Put Bool on worklist
  1325       // Look for the 'is_x2logic' pattern: "x ? : 0 : 1" and put the
  1326       // phi merging either 0 or 1 onto the worklist
  1327       if (use->outcnt() > 0) {
  1328         Node* bol = use->raw_out(0);
  1329         if (bol->outcnt() > 0) {
  1330           Node* iff = bol->raw_out(0);
  1331           if (iff->outcnt() == 2) {
  1332             Node* ifproj0 = iff->raw_out(0);
  1333             Node* ifproj1 = iff->raw_out(1);
  1334             if (ifproj0->outcnt() > 0 && ifproj1->outcnt() > 0) {
  1335               Node* region0 = ifproj0->raw_out(0);
  1336               Node* region1 = ifproj1->raw_out(0);
  1337               if( region0 == region1 )
  1338                 add_users_to_worklist0(region0);
  1345     uint use_op = use->Opcode();
  1346     // If changed Cast input, check Phi users for simple cycles
  1347     if( use->is_ConstraintCast() || use->is_CheckCastPP() ) {
  1348       for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
  1349         Node* u = use->fast_out(i2);
  1350         if (u->is_Phi())
  1351           _worklist.push(u);
  1354     // If changed LShift inputs, check RShift users for useless sign-ext
  1355     if( use_op == Op_LShiftI ) {
  1356       for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
  1357         Node* u = use->fast_out(i2);
  1358         if (u->Opcode() == Op_RShiftI)
  1359           _worklist.push(u);
  1362     // If changed AddP inputs, check Stores for loop invariant
  1363     if( use_op == Op_AddP ) {
  1364       for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) {
  1365         Node* u = use->fast_out(i2);
  1366         if (u->is_Mem())
  1367           _worklist.push(u);
  1370     // If changed initialization activity, check dependent Stores
  1371     if (use_op == Op_Allocate || use_op == Op_AllocateArray) {
  1372       InitializeNode* init = use->as_Allocate()->initialization();
  1373       if (init != NULL) {
  1374         Node* imem = init->proj_out(TypeFunc::Memory);
  1375         if (imem != NULL)  add_users_to_worklist0(imem);
  1378     if (use_op == Op_Initialize) {
  1379       Node* imem = use->as_Initialize()->proj_out(TypeFunc::Memory);
  1380       if (imem != NULL)  add_users_to_worklist0(imem);
  1385 //=============================================================================
  1386 #ifndef PRODUCT
  1387 uint PhaseCCP::_total_invokes   = 0;
  1388 uint PhaseCCP::_total_constants = 0;
  1389 #endif
  1390 //------------------------------PhaseCCP---------------------------------------
  1391 // Conditional Constant Propagation, ala Wegman & Zadeck
  1392 PhaseCCP::PhaseCCP( PhaseIterGVN *igvn ) : PhaseIterGVN(igvn) {
  1393   NOT_PRODUCT( clear_constants(); )
  1394   assert( _worklist.size() == 0, "" );
  1395   // Clear out _nodes from IterGVN.  Must be clear to transform call.
  1396   _nodes.clear();               // Clear out from IterGVN
  1397   analyze();
  1400 #ifndef PRODUCT
  1401 //------------------------------~PhaseCCP--------------------------------------
  1402 PhaseCCP::~PhaseCCP() {
  1403   inc_invokes();
  1404   _total_constants += count_constants();
  1406 #endif
  1409 #ifdef ASSERT
  1410 static bool ccp_type_widens(const Type* t, const Type* t0) {
  1411   assert(t->meet(t0) == t, "Not monotonic");
  1412   switch (t->base() == t0->base() ? t->base() : Type::Top) {
  1413   case Type::Int:
  1414     assert(t0->isa_int()->_widen <= t->isa_int()->_widen, "widen increases");
  1415     break;
  1416   case Type::Long:
  1417     assert(t0->isa_long()->_widen <= t->isa_long()->_widen, "widen increases");
  1418     break;
  1420   return true;
  1422 #endif //ASSERT
  1424 //------------------------------analyze----------------------------------------
  1425 void PhaseCCP::analyze() {
  1426   // Initialize all types to TOP, optimistic analysis
  1427   for (int i = C->unique() - 1; i >= 0; i--)  {
  1428     _types.map(i,Type::TOP);
  1431   // Push root onto worklist
  1432   Unique_Node_List worklist;
  1433   worklist.push(C->root());
  1435   // Pull from worklist; compute new value; push changes out.
  1436   // This loop is the meat of CCP.
  1437   while( worklist.size() ) {
  1438     Node *n = worklist.pop();
  1439     const Type *t = n->Value(this);
  1440     if (t != type(n)) {
  1441       assert(ccp_type_widens(t, type(n)), "ccp type must widen");
  1442 #ifndef PRODUCT
  1443       if( TracePhaseCCP ) {
  1444         t->dump();
  1445         do { tty->print("\t"); } while (tty->position() < 16);
  1446         n->dump();
  1448 #endif
  1449       set_type(n, t);
  1450       for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
  1451         Node* m = n->fast_out(i);   // Get user
  1452         if( m->is_Region() ) {  // New path to Region?  Must recheck Phis too
  1453           for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) {
  1454             Node* p = m->fast_out(i2); // Propagate changes to uses
  1455             if( p->bottom_type() != type(p) ) // If not already bottomed out
  1456               worklist.push(p); // Propagate change to user
  1459         // If we changed the receiver type to a call, we need to revisit
  1460         // the Catch following the call.  It's looking for a non-NULL
  1461         // receiver to know when to enable the regular fall-through path
  1462         // in addition to the NullPtrException path
  1463         if (m->is_Call()) {
  1464           for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) {
  1465             Node* p = m->fast_out(i2);  // Propagate changes to uses
  1466             if (p->is_Proj() && p->as_Proj()->_con == TypeFunc::Control && p->outcnt() == 1)
  1467               worklist.push(p->unique_out());
  1470         if( m->bottom_type() != type(m) ) // If not already bottomed out
  1471           worklist.push(m);     // Propagate change to user
  1477 //------------------------------do_transform-----------------------------------
  1478 // Top level driver for the recursive transformer
  1479 void PhaseCCP::do_transform() {
  1480   // Correct leaves of new-space Nodes; they point to old-space.
  1481   C->set_root( transform(C->root())->as_Root() );
  1482   assert( C->top(),  "missing TOP node" );
  1483   assert( C->root(), "missing root" );
  1486 //------------------------------transform--------------------------------------
  1487 // Given a Node in old-space, clone him into new-space.
  1488 // Convert any of his old-space children into new-space children.
  1489 Node *PhaseCCP::transform( Node *n ) {
  1490   Node *new_node = _nodes[n->_idx]; // Check for transformed node
  1491   if( new_node != NULL )
  1492     return new_node;                // Been there, done that, return old answer
  1493   new_node = transform_once(n);     // Check for constant
  1494   _nodes.map( n->_idx, new_node );  // Flag as having been cloned
  1496   // Allocate stack of size _nodes.Size()/2 to avoid frequent realloc
  1497   GrowableArray <Node *> trstack(C->unique() >> 1);
  1499   trstack.push(new_node);           // Process children of cloned node
  1500   while ( trstack.is_nonempty() ) {
  1501     Node *clone = trstack.pop();
  1502     uint cnt = clone->req();
  1503     for( uint i = 0; i < cnt; i++ ) {          // For all inputs do
  1504       Node *input = clone->in(i);
  1505       if( input != NULL ) {                    // Ignore NULLs
  1506         Node *new_input = _nodes[input->_idx]; // Check for cloned input node
  1507         if( new_input == NULL ) {
  1508           new_input = transform_once(input);   // Check for constant
  1509           _nodes.map( input->_idx, new_input );// Flag as having been cloned
  1510           trstack.push(new_input);
  1512         assert( new_input == clone->in(i), "insanity check");
  1516   return new_node;
  1520 //------------------------------transform_once---------------------------------
  1521 // For PhaseCCP, transformation is IDENTITY unless Node computed a constant.
  1522 Node *PhaseCCP::transform_once( Node *n ) {
  1523   const Type *t = type(n);
  1524   // Constant?  Use constant Node instead
  1525   if( t->singleton() ) {
  1526     Node *nn = n;               // Default is to return the original constant
  1527     if( t == Type::TOP ) {
  1528       // cache my top node on the Compile instance
  1529       if( C->cached_top_node() == NULL || C->cached_top_node()->in(0) == NULL ) {
  1530         C->set_cached_top_node( ConNode::make(C, Type::TOP) );
  1531         set_type(C->top(), Type::TOP);
  1533       nn = C->top();
  1535     if( !n->is_Con() ) {
  1536       if( t != Type::TOP ) {
  1537         nn = makecon(t);        // ConNode::make(t);
  1538         NOT_PRODUCT( inc_constants(); )
  1539       } else if( n->is_Region() ) { // Unreachable region
  1540         // Note: nn == C->top()
  1541         n->set_req(0, NULL);        // Cut selfreference
  1542         // Eagerly remove dead phis to avoid phis copies creation.
  1543         for (DUIterator i = n->outs(); n->has_out(i); i++) {
  1544           Node* m = n->out(i);
  1545           if( m->is_Phi() ) {
  1546             assert(type(m) == Type::TOP, "Unreachable region should not have live phis.");
  1547             replace_node(m, nn);
  1548             --i; // deleted this phi; rescan starting with next position
  1552       replace_node(n,nn);       // Update DefUse edges for new constant
  1554     return nn;
  1557   // If x is a TypeNode, capture any more-precise type permanently into Node
  1558   if (t != n->bottom_type()) {
  1559     hash_delete(n);             // changing bottom type may force a rehash
  1560     n->raise_bottom_type(t);
  1561     _worklist.push(n);          // n re-enters the hash table via the worklist
  1564   // Idealize graph using DU info.  Must clone() into new-space.
  1565   // DU info is generally used to show profitability, progress or safety
  1566   // (but generally not needed for correctness).
  1567   Node *nn = n->Ideal_DU_postCCP(this);
  1569   // TEMPORARY fix to ensure that 2nd GVN pass eliminates NULL checks
  1570   switch( n->Opcode() ) {
  1571   case Op_FastLock:      // Revisit FastLocks for lock coarsening
  1572   case Op_If:
  1573   case Op_CountedLoopEnd:
  1574   case Op_Region:
  1575   case Op_Loop:
  1576   case Op_CountedLoop:
  1577   case Op_Conv2B:
  1578   case Op_Opaque1:
  1579   case Op_Opaque2:
  1580     _worklist.push(n);
  1581     break;
  1582   default:
  1583     break;
  1585   if( nn ) {
  1586     _worklist.push(n);
  1587     // Put users of 'n' onto worklist for second igvn transform
  1588     add_users_to_worklist(n);
  1589     return nn;
  1592   return  n;
  1595 //---------------------------------saturate------------------------------------
  1596 const Type* PhaseCCP::saturate(const Type* new_type, const Type* old_type,
  1597                                const Type* limit_type) const {
  1598   const Type* wide_type = new_type->widen(old_type, limit_type);
  1599   if (wide_type != new_type) {          // did we widen?
  1600     // If so, we may have widened beyond the limit type.  Clip it back down.
  1601     new_type = wide_type->filter(limit_type);
  1603   return new_type;
  1606 //------------------------------print_statistics-------------------------------
  1607 #ifndef PRODUCT
  1608 void PhaseCCP::print_statistics() {
  1609   tty->print_cr("CCP: %d  constants found: %d", _total_invokes, _total_constants);
  1611 #endif
  1614 //=============================================================================
  1615 #ifndef PRODUCT
  1616 uint PhasePeephole::_total_peepholes = 0;
  1617 #endif
  1618 //------------------------------PhasePeephole----------------------------------
  1619 // Conditional Constant Propagation, ala Wegman & Zadeck
  1620 PhasePeephole::PhasePeephole( PhaseRegAlloc *regalloc, PhaseCFG &cfg )
  1621   : PhaseTransform(Peephole), _regalloc(regalloc), _cfg(cfg) {
  1622   NOT_PRODUCT( clear_peepholes(); )
  1625 #ifndef PRODUCT
  1626 //------------------------------~PhasePeephole---------------------------------
  1627 PhasePeephole::~PhasePeephole() {
  1628   _total_peepholes += count_peepholes();
  1630 #endif
  1632 //------------------------------transform--------------------------------------
  1633 Node *PhasePeephole::transform( Node *n ) {
  1634   ShouldNotCallThis();
  1635   return NULL;
  1638 //------------------------------do_transform-----------------------------------
  1639 void PhasePeephole::do_transform() {
  1640   bool method_name_not_printed = true;
  1642   // Examine each basic block
  1643   for( uint block_number = 1; block_number < _cfg._num_blocks; ++block_number ) {
  1644     Block *block = _cfg._blocks[block_number];
  1645     bool block_not_printed = true;
  1647     // and each instruction within a block
  1648     uint end_index = block->_nodes.size();
  1649     // block->end_idx() not valid after PhaseRegAlloc
  1650     for( uint instruction_index = 1; instruction_index < end_index; ++instruction_index ) {
  1651       Node     *n = block->_nodes.at(instruction_index);
  1652       if( n->is_Mach() ) {
  1653         MachNode *m = n->as_Mach();
  1654         int deleted_count = 0;
  1655         // check for peephole opportunities
  1656         MachNode *m2 = m->peephole( block, instruction_index, _regalloc, deleted_count, C );
  1657         if( m2 != NULL ) {
  1658 #ifndef PRODUCT
  1659           if( PrintOptoPeephole ) {
  1660             // Print method, first time only
  1661             if( C->method() && method_name_not_printed ) {
  1662               C->method()->print_short_name(); tty->cr();
  1663               method_name_not_printed = false;
  1665             // Print this block
  1666             if( Verbose && block_not_printed) {
  1667               tty->print_cr("in block");
  1668               block->dump();
  1669               block_not_printed = false;
  1671             // Print instructions being deleted
  1672             for( int i = (deleted_count - 1); i >= 0; --i ) {
  1673               block->_nodes.at(instruction_index-i)->as_Mach()->format(_regalloc); tty->cr();
  1675             tty->print_cr("replaced with");
  1676             // Print new instruction
  1677             m2->format(_regalloc);
  1678             tty->print("\n\n");
  1680 #endif
  1681           // Remove old nodes from basic block and update instruction_index
  1682           // (old nodes still exist and may have edges pointing to them
  1683           //  as register allocation info is stored in the allocator using
  1684           //  the node index to live range mappings.)
  1685           uint safe_instruction_index = (instruction_index - deleted_count);
  1686           for( ; (instruction_index > safe_instruction_index); --instruction_index ) {
  1687             block->_nodes.remove( instruction_index );
  1689           // install new node after safe_instruction_index
  1690           block->_nodes.insert( safe_instruction_index + 1, m2 );
  1691           end_index = block->_nodes.size() - 1; // Recompute new block size
  1692           NOT_PRODUCT( inc_peepholes(); )
  1699 //------------------------------print_statistics-------------------------------
  1700 #ifndef PRODUCT
  1701 void PhasePeephole::print_statistics() {
  1702   tty->print_cr("Peephole: peephole rules applied: %d",  _total_peepholes);
  1704 #endif
  1707 //=============================================================================
  1708 //------------------------------set_req_X--------------------------------------
  1709 void Node::set_req_X( uint i, Node *n, PhaseIterGVN *igvn ) {
  1710   assert( is_not_dead(n), "can not use dead node");
  1711   assert( igvn->hash_find(this) != this, "Need to remove from hash before changing edges" );
  1712   Node *old = in(i);
  1713   set_req(i, n);
  1715   // old goes dead?
  1716   if( old ) {
  1717     switch (old->outcnt()) {
  1718     case 0:
  1719       // Put into the worklist to kill later. We do not kill it now because the
  1720       // recursive kill will delete the current node (this) if dead-loop exists
  1721       if (!old->is_top())
  1722         igvn->_worklist.push( old );
  1723       break;
  1724     case 1:
  1725       if( old->is_Store() || old->has_special_unique_user() )
  1726         igvn->add_users_to_worklist( old );
  1727       break;
  1728     case 2:
  1729       if( old->is_Store() )
  1730         igvn->add_users_to_worklist( old );
  1731       if( old->Opcode() == Op_Region )
  1732         igvn->_worklist.push(old);
  1733       break;
  1734     case 3:
  1735       if( old->Opcode() == Op_Region ) {
  1736         igvn->_worklist.push(old);
  1737         igvn->add_users_to_worklist( old );
  1739       break;
  1740     default:
  1741       break;
  1747 //-------------------------------replace_by-----------------------------------
  1748 // Using def-use info, replace one node for another.  Follow the def-use info
  1749 // to all users of the OLD node.  Then make all uses point to the NEW node.
  1750 void Node::replace_by(Node *new_node) {
  1751   assert(!is_top(), "top node has no DU info");
  1752   for (DUIterator_Last imin, i = last_outs(imin); i >= imin; ) {
  1753     Node* use = last_out(i);
  1754     uint uses_found = 0;
  1755     for (uint j = 0; j < use->len(); j++) {
  1756       if (use->in(j) == this) {
  1757         if (j < use->req())
  1758               use->set_req(j, new_node);
  1759         else  use->set_prec(j, new_node);
  1760         uses_found++;
  1763     i -= uses_found;    // we deleted 1 or more copies of this edge
  1767 //=============================================================================
  1768 //-----------------------------------------------------------------------------
  1769 void Type_Array::grow( uint i ) {
  1770   if( !_max ) {
  1771     _max = 1;
  1772     _types = (const Type**)_a->Amalloc( _max * sizeof(Type*) );
  1773     _types[0] = NULL;
  1775   uint old = _max;
  1776   while( i >= _max ) _max <<= 1;        // Double to fit
  1777   _types = (const Type**)_a->Arealloc( _types, old*sizeof(Type*),_max*sizeof(Type*));
  1778   memset( &_types[old], 0, (_max-old)*sizeof(Type*) );
  1781 //------------------------------dump-------------------------------------------
  1782 #ifndef PRODUCT
  1783 void Type_Array::dump() const {
  1784   uint max = Size();
  1785   for( uint i = 0; i < max; i++ ) {
  1786     if( _types[i] != NULL ) {
  1787       tty->print("  %d\t== ", i); _types[i]->dump(); tty->cr();
  1791 #endif

mercurial