1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/opto/phaseX.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,1834 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "precompiled.hpp" 1.29 +#include "memory/allocation.inline.hpp" 1.30 +#include "opto/block.hpp" 1.31 +#include "opto/callnode.hpp" 1.32 +#include "opto/cfgnode.hpp" 1.33 +#include "opto/connode.hpp" 1.34 +#include "opto/idealGraphPrinter.hpp" 1.35 +#include "opto/loopnode.hpp" 1.36 +#include "opto/machnode.hpp" 1.37 +#include "opto/opcodes.hpp" 1.38 +#include "opto/phaseX.hpp" 1.39 +#include "opto/regalloc.hpp" 1.40 +#include "opto/rootnode.hpp" 1.41 + 1.42 +//============================================================================= 1.43 +#define NODE_HASH_MINIMUM_SIZE 255 1.44 +//------------------------------NodeHash--------------------------------------- 1.45 +NodeHash::NodeHash(uint est_max_size) : 1.46 + _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ), 1.47 + _a(Thread::current()->resource_area()), 1.48 + _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ), // (Node**)_a->Amalloc(_max * sizeof(Node*)) ), 1.49 + _inserts(0), _insert_limit( insert_limit() ), 1.50 + _look_probes(0), _lookup_hits(0), _lookup_misses(0), 1.51 + _total_insert_probes(0), _total_inserts(0), 1.52 + _insert_probes(0), _grows(0) { 1.53 + // _sentinel must be in the current node space 1.54 + _sentinel = new (Compile::current()) ProjNode(NULL, TypeFunc::Control); 1.55 + memset(_table,0,sizeof(Node*)*_max); 1.56 +} 1.57 + 1.58 +//------------------------------NodeHash--------------------------------------- 1.59 +NodeHash::NodeHash(Arena *arena, uint est_max_size) : 1.60 + _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ), 1.61 + _a(arena), 1.62 + _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ), 1.63 + _inserts(0), _insert_limit( insert_limit() ), 1.64 + _look_probes(0), _lookup_hits(0), _lookup_misses(0), 1.65 + _delete_probes(0), _delete_hits(0), _delete_misses(0), 1.66 + _total_insert_probes(0), _total_inserts(0), 1.67 + _insert_probes(0), _grows(0) { 1.68 + // _sentinel must be in the current node space 1.69 + _sentinel = new (Compile::current()) ProjNode(NULL, TypeFunc::Control); 1.70 + memset(_table,0,sizeof(Node*)*_max); 1.71 +} 1.72 + 1.73 +//------------------------------NodeHash--------------------------------------- 1.74 +NodeHash::NodeHash(NodeHash *nh) { 1.75 + debug_only(_table = (Node**)badAddress); // interact correctly w/ operator= 1.76 + // just copy in all the fields 1.77 + *this = *nh; 1.78 + // nh->_sentinel must be in the current node space 1.79 +} 1.80 + 1.81 +void NodeHash::replace_with(NodeHash *nh) { 1.82 + debug_only(_table = (Node**)badAddress); // interact correctly w/ operator= 1.83 + // just copy in all the fields 1.84 + *this = *nh; 1.85 + // nh->_sentinel must be in the current node space 1.86 +} 1.87 + 1.88 +//------------------------------hash_find-------------------------------------- 1.89 +// Find in hash table 1.90 +Node *NodeHash::hash_find( const Node *n ) { 1.91 + // ((Node*)n)->set_hash( n->hash() ); 1.92 + uint hash = n->hash(); 1.93 + if (hash == Node::NO_HASH) { 1.94 + debug_only( _lookup_misses++ ); 1.95 + return NULL; 1.96 + } 1.97 + uint key = hash & (_max-1); 1.98 + uint stride = key | 0x01; 1.99 + debug_only( _look_probes++ ); 1.100 + Node *k = _table[key]; // Get hashed value 1.101 + if( !k ) { // ?Miss? 1.102 + debug_only( _lookup_misses++ ); 1.103 + return NULL; // Miss! 1.104 + } 1.105 + 1.106 + int op = n->Opcode(); 1.107 + uint req = n->req(); 1.108 + while( 1 ) { // While probing hash table 1.109 + if( k->req() == req && // Same count of inputs 1.110 + k->Opcode() == op ) { // Same Opcode 1.111 + for( uint i=0; i<req; i++ ) 1.112 + if( n->in(i)!=k->in(i)) // Different inputs? 1.113 + goto collision; // "goto" is a speed hack... 1.114 + if( n->cmp(*k) ) { // Check for any special bits 1.115 + debug_only( _lookup_hits++ ); 1.116 + return k; // Hit! 1.117 + } 1.118 + } 1.119 + collision: 1.120 + debug_only( _look_probes++ ); 1.121 + key = (key + stride/*7*/) & (_max-1); // Stride through table with relative prime 1.122 + k = _table[key]; // Get hashed value 1.123 + if( !k ) { // ?Miss? 1.124 + debug_only( _lookup_misses++ ); 1.125 + return NULL; // Miss! 1.126 + } 1.127 + } 1.128 + ShouldNotReachHere(); 1.129 + return NULL; 1.130 +} 1.131 + 1.132 +//------------------------------hash_find_insert------------------------------- 1.133 +// Find in hash table, insert if not already present 1.134 +// Used to preserve unique entries in hash table 1.135 +Node *NodeHash::hash_find_insert( Node *n ) { 1.136 + // n->set_hash( ); 1.137 + uint hash = n->hash(); 1.138 + if (hash == Node::NO_HASH) { 1.139 + debug_only( _lookup_misses++ ); 1.140 + return NULL; 1.141 + } 1.142 + uint key = hash & (_max-1); 1.143 + uint stride = key | 0x01; // stride must be relatively prime to table siz 1.144 + uint first_sentinel = 0; // replace a sentinel if seen. 1.145 + debug_only( _look_probes++ ); 1.146 + Node *k = _table[key]; // Get hashed value 1.147 + if( !k ) { // ?Miss? 1.148 + debug_only( _lookup_misses++ ); 1.149 + _table[key] = n; // Insert into table! 1.150 + debug_only(n->enter_hash_lock()); // Lock down the node while in the table. 1.151 + check_grow(); // Grow table if insert hit limit 1.152 + return NULL; // Miss! 1.153 + } 1.154 + else if( k == _sentinel ) { 1.155 + first_sentinel = key; // Can insert here 1.156 + } 1.157 + 1.158 + int op = n->Opcode(); 1.159 + uint req = n->req(); 1.160 + while( 1 ) { // While probing hash table 1.161 + if( k->req() == req && // Same count of inputs 1.162 + k->Opcode() == op ) { // Same Opcode 1.163 + for( uint i=0; i<req; i++ ) 1.164 + if( n->in(i)!=k->in(i)) // Different inputs? 1.165 + goto collision; // "goto" is a speed hack... 1.166 + if( n->cmp(*k) ) { // Check for any special bits 1.167 + debug_only( _lookup_hits++ ); 1.168 + return k; // Hit! 1.169 + } 1.170 + } 1.171 + collision: 1.172 + debug_only( _look_probes++ ); 1.173 + key = (key + stride) & (_max-1); // Stride through table w/ relative prime 1.174 + k = _table[key]; // Get hashed value 1.175 + if( !k ) { // ?Miss? 1.176 + debug_only( _lookup_misses++ ); 1.177 + key = (first_sentinel == 0) ? key : first_sentinel; // ?saw sentinel? 1.178 + _table[key] = n; // Insert into table! 1.179 + debug_only(n->enter_hash_lock()); // Lock down the node while in the table. 1.180 + check_grow(); // Grow table if insert hit limit 1.181 + return NULL; // Miss! 1.182 + } 1.183 + else if( first_sentinel == 0 && k == _sentinel ) { 1.184 + first_sentinel = key; // Can insert here 1.185 + } 1.186 + 1.187 + } 1.188 + ShouldNotReachHere(); 1.189 + return NULL; 1.190 +} 1.191 + 1.192 +//------------------------------hash_insert------------------------------------ 1.193 +// Insert into hash table 1.194 +void NodeHash::hash_insert( Node *n ) { 1.195 + // // "conflict" comments -- print nodes that conflict 1.196 + // bool conflict = false; 1.197 + // n->set_hash(); 1.198 + uint hash = n->hash(); 1.199 + if (hash == Node::NO_HASH) { 1.200 + return; 1.201 + } 1.202 + check_grow(); 1.203 + uint key = hash & (_max-1); 1.204 + uint stride = key | 0x01; 1.205 + 1.206 + while( 1 ) { // While probing hash table 1.207 + debug_only( _insert_probes++ ); 1.208 + Node *k = _table[key]; // Get hashed value 1.209 + if( !k || (k == _sentinel) ) break; // Found a slot 1.210 + assert( k != n, "already inserted" ); 1.211 + // if( PrintCompilation && PrintOptoStatistics && Verbose ) { tty->print(" conflict: "); k->dump(); conflict = true; } 1.212 + key = (key + stride) & (_max-1); // Stride through table w/ relative prime 1.213 + } 1.214 + _table[key] = n; // Insert into table! 1.215 + debug_only(n->enter_hash_lock()); // Lock down the node while in the table. 1.216 + // if( conflict ) { n->dump(); } 1.217 +} 1.218 + 1.219 +//------------------------------hash_delete------------------------------------ 1.220 +// Replace in hash table with sentinel 1.221 +bool NodeHash::hash_delete( const Node *n ) { 1.222 + Node *k; 1.223 + uint hash = n->hash(); 1.224 + if (hash == Node::NO_HASH) { 1.225 + debug_only( _delete_misses++ ); 1.226 + return false; 1.227 + } 1.228 + uint key = hash & (_max-1); 1.229 + uint stride = key | 0x01; 1.230 + debug_only( uint counter = 0; ); 1.231 + for( ; /* (k != NULL) && (k != _sentinel) */; ) { 1.232 + debug_only( counter++ ); 1.233 + debug_only( _delete_probes++ ); 1.234 + k = _table[key]; // Get hashed value 1.235 + if( !k ) { // Miss? 1.236 + debug_only( _delete_misses++ ); 1.237 +#ifdef ASSERT 1.238 + if( VerifyOpto ) { 1.239 + for( uint i=0; i < _max; i++ ) 1.240 + assert( _table[i] != n, "changed edges with rehashing" ); 1.241 + } 1.242 +#endif 1.243 + return false; // Miss! Not in chain 1.244 + } 1.245 + else if( n == k ) { 1.246 + debug_only( _delete_hits++ ); 1.247 + _table[key] = _sentinel; // Hit! Label as deleted entry 1.248 + debug_only(((Node*)n)->exit_hash_lock()); // Unlock the node upon removal from table. 1.249 + return true; 1.250 + } 1.251 + else { 1.252 + // collision: move through table with prime offset 1.253 + key = (key + stride/*7*/) & (_max-1); 1.254 + assert( counter <= _insert_limit, "Cycle in hash-table"); 1.255 + } 1.256 + } 1.257 + ShouldNotReachHere(); 1.258 + return false; 1.259 +} 1.260 + 1.261 +//------------------------------round_up--------------------------------------- 1.262 +// Round up to nearest power of 2 1.263 +uint NodeHash::round_up( uint x ) { 1.264 + x += (x>>2); // Add 25% slop 1.265 + if( x <16 ) return 16; // Small stuff 1.266 + uint i=16; 1.267 + while( i < x ) i <<= 1; // Double to fit 1.268 + return i; // Return hash table size 1.269 +} 1.270 + 1.271 +//------------------------------grow------------------------------------------- 1.272 +// Grow _table to next power of 2 and insert old entries 1.273 +void NodeHash::grow() { 1.274 + // Record old state 1.275 + uint old_max = _max; 1.276 + Node **old_table = _table; 1.277 + // Construct new table with twice the space 1.278 + _grows++; 1.279 + _total_inserts += _inserts; 1.280 + _total_insert_probes += _insert_probes; 1.281 + _inserts = 0; 1.282 + _insert_probes = 0; 1.283 + _max = _max << 1; 1.284 + _table = NEW_ARENA_ARRAY( _a , Node* , _max ); // (Node**)_a->Amalloc( _max * sizeof(Node*) ); 1.285 + memset(_table,0,sizeof(Node*)*_max); 1.286 + _insert_limit = insert_limit(); 1.287 + // Insert old entries into the new table 1.288 + for( uint i = 0; i < old_max; i++ ) { 1.289 + Node *m = *old_table++; 1.290 + if( !m || m == _sentinel ) continue; 1.291 + debug_only(m->exit_hash_lock()); // Unlock the node upon removal from old table. 1.292 + hash_insert(m); 1.293 + } 1.294 +} 1.295 + 1.296 +//------------------------------clear------------------------------------------ 1.297 +// Clear all entries in _table to NULL but keep storage 1.298 +void NodeHash::clear() { 1.299 +#ifdef ASSERT 1.300 + // Unlock all nodes upon removal from table. 1.301 + for (uint i = 0; i < _max; i++) { 1.302 + Node* n = _table[i]; 1.303 + if (!n || n == _sentinel) continue; 1.304 + n->exit_hash_lock(); 1.305 + } 1.306 +#endif 1.307 + 1.308 + memset( _table, 0, _max * sizeof(Node*) ); 1.309 +} 1.310 + 1.311 +//-----------------------remove_useless_nodes---------------------------------- 1.312 +// Remove useless nodes from value table, 1.313 +// implementation does not depend on hash function 1.314 +void NodeHash::remove_useless_nodes(VectorSet &useful) { 1.315 + 1.316 + // Dead nodes in the hash table inherited from GVN should not replace 1.317 + // existing nodes, remove dead nodes. 1.318 + uint max = size(); 1.319 + Node *sentinel_node = sentinel(); 1.320 + for( uint i = 0; i < max; ++i ) { 1.321 + Node *n = at(i); 1.322 + if(n != NULL && n != sentinel_node && !useful.test(n->_idx)) { 1.323 + debug_only(n->exit_hash_lock()); // Unlock the node when removed 1.324 + _table[i] = sentinel_node; // Replace with placeholder 1.325 + } 1.326 + } 1.327 +} 1.328 + 1.329 + 1.330 +void NodeHash::check_no_speculative_types() { 1.331 +#ifdef ASSERT 1.332 + uint max = size(); 1.333 + Node *sentinel_node = sentinel(); 1.334 + for (uint i = 0; i < max; ++i) { 1.335 + Node *n = at(i); 1.336 + if(n != NULL && n != sentinel_node && n->is_Type()) { 1.337 + TypeNode* tn = n->as_Type(); 1.338 + const Type* t = tn->type(); 1.339 + const Type* t_no_spec = t->remove_speculative(); 1.340 + assert(t == t_no_spec, "dead node in hash table or missed node during speculative cleanup"); 1.341 + } 1.342 + } 1.343 +#endif 1.344 +} 1.345 + 1.346 +#ifndef PRODUCT 1.347 +//------------------------------dump------------------------------------------- 1.348 +// Dump statistics for the hash table 1.349 +void NodeHash::dump() { 1.350 + _total_inserts += _inserts; 1.351 + _total_insert_probes += _insert_probes; 1.352 + if (PrintCompilation && PrintOptoStatistics && Verbose && (_inserts > 0)) { 1.353 + if (WizardMode) { 1.354 + for (uint i=0; i<_max; i++) { 1.355 + if (_table[i]) 1.356 + tty->print("%d/%d/%d ",i,_table[i]->hash()&(_max-1),_table[i]->_idx); 1.357 + } 1.358 + } 1.359 + tty->print("\nGVN Hash stats: %d grows to %d max_size\n", _grows, _max); 1.360 + tty->print(" %d/%d (%8.1f%% full)\n", _inserts, _max, (double)_inserts/_max*100.0); 1.361 + tty->print(" %dp/(%dh+%dm) (%8.2f probes/lookup)\n", _look_probes, _lookup_hits, _lookup_misses, (double)_look_probes/(_lookup_hits+_lookup_misses)); 1.362 + tty->print(" %dp/%di (%8.2f probes/insert)\n", _total_insert_probes, _total_inserts, (double)_total_insert_probes/_total_inserts); 1.363 + // sentinels increase lookup cost, but not insert cost 1.364 + assert((_lookup_misses+_lookup_hits)*4+100 >= _look_probes, "bad hash function"); 1.365 + assert( _inserts+(_inserts>>3) < _max, "table too full" ); 1.366 + assert( _inserts*3+100 >= _insert_probes, "bad hash function" ); 1.367 + } 1.368 +} 1.369 + 1.370 +Node *NodeHash::find_index(uint idx) { // For debugging 1.371 + // Find an entry by its index value 1.372 + for( uint i = 0; i < _max; i++ ) { 1.373 + Node *m = _table[i]; 1.374 + if( !m || m == _sentinel ) continue; 1.375 + if( m->_idx == (uint)idx ) return m; 1.376 + } 1.377 + return NULL; 1.378 +} 1.379 +#endif 1.380 + 1.381 +#ifdef ASSERT 1.382 +NodeHash::~NodeHash() { 1.383 + // Unlock all nodes upon destruction of table. 1.384 + if (_table != (Node**)badAddress) clear(); 1.385 +} 1.386 + 1.387 +void NodeHash::operator=(const NodeHash& nh) { 1.388 + // Unlock all nodes upon replacement of table. 1.389 + if (&nh == this) return; 1.390 + if (_table != (Node**)badAddress) clear(); 1.391 + memcpy(this, &nh, sizeof(*this)); 1.392 + // Do not increment hash_lock counts again. 1.393 + // Instead, be sure we never again use the source table. 1.394 + ((NodeHash*)&nh)->_table = (Node**)badAddress; 1.395 +} 1.396 + 1.397 + 1.398 +#endif 1.399 + 1.400 + 1.401 +//============================================================================= 1.402 +//------------------------------PhaseRemoveUseless----------------------------- 1.403 +// 1) Use a breadthfirst walk to collect useful nodes reachable from root. 1.404 +PhaseRemoveUseless::PhaseRemoveUseless( PhaseGVN *gvn, Unique_Node_List *worklist ) : Phase(Remove_Useless), 1.405 + _useful(Thread::current()->resource_area()) { 1.406 + 1.407 + // Implementation requires 'UseLoopSafepoints == true' and an edge from root 1.408 + // to each SafePointNode at a backward branch. Inserted in add_safepoint(). 1.409 + if( !UseLoopSafepoints || !OptoRemoveUseless ) return; 1.410 + 1.411 + // Identify nodes that are reachable from below, useful. 1.412 + C->identify_useful_nodes(_useful); 1.413 + // Update dead node list 1.414 + C->update_dead_node_list(_useful); 1.415 + 1.416 + // Remove all useless nodes from PhaseValues' recorded types 1.417 + // Must be done before disconnecting nodes to preserve hash-table-invariant 1.418 + gvn->remove_useless_nodes(_useful.member_set()); 1.419 + 1.420 + // Remove all useless nodes from future worklist 1.421 + worklist->remove_useless_nodes(_useful.member_set()); 1.422 + 1.423 + // Disconnect 'useless' nodes that are adjacent to useful nodes 1.424 + C->remove_useless_nodes(_useful); 1.425 + 1.426 + // Remove edges from "root" to each SafePoint at a backward branch. 1.427 + // They were inserted during parsing (see add_safepoint()) to make infinite 1.428 + // loops without calls or exceptions visible to root, i.e., useful. 1.429 + Node *root = C->root(); 1.430 + if( root != NULL ) { 1.431 + for( uint i = root->req(); i < root->len(); ++i ) { 1.432 + Node *n = root->in(i); 1.433 + if( n != NULL && n->is_SafePoint() ) { 1.434 + root->rm_prec(i); 1.435 + --i; 1.436 + } 1.437 + } 1.438 + } 1.439 +} 1.440 + 1.441 + 1.442 +//============================================================================= 1.443 +//------------------------------PhaseTransform--------------------------------- 1.444 +PhaseTransform::PhaseTransform( PhaseNumber pnum ) : Phase(pnum), 1.445 + _arena(Thread::current()->resource_area()), 1.446 + _nodes(_arena), 1.447 + _types(_arena) 1.448 +{ 1.449 + init_con_caches(); 1.450 +#ifndef PRODUCT 1.451 + clear_progress(); 1.452 + clear_transforms(); 1.453 + set_allow_progress(true); 1.454 +#endif 1.455 + // Force allocation for currently existing nodes 1.456 + _types.map(C->unique(), NULL); 1.457 +} 1.458 + 1.459 +//------------------------------PhaseTransform--------------------------------- 1.460 +PhaseTransform::PhaseTransform( Arena *arena, PhaseNumber pnum ) : Phase(pnum), 1.461 + _arena(arena), 1.462 + _nodes(arena), 1.463 + _types(arena) 1.464 +{ 1.465 + init_con_caches(); 1.466 +#ifndef PRODUCT 1.467 + clear_progress(); 1.468 + clear_transforms(); 1.469 + set_allow_progress(true); 1.470 +#endif 1.471 + // Force allocation for currently existing nodes 1.472 + _types.map(C->unique(), NULL); 1.473 +} 1.474 + 1.475 +//------------------------------PhaseTransform--------------------------------- 1.476 +// Initialize with previously generated type information 1.477 +PhaseTransform::PhaseTransform( PhaseTransform *pt, PhaseNumber pnum ) : Phase(pnum), 1.478 + _arena(pt->_arena), 1.479 + _nodes(pt->_nodes), 1.480 + _types(pt->_types) 1.481 +{ 1.482 + init_con_caches(); 1.483 +#ifndef PRODUCT 1.484 + clear_progress(); 1.485 + clear_transforms(); 1.486 + set_allow_progress(true); 1.487 +#endif 1.488 +} 1.489 + 1.490 +void PhaseTransform::init_con_caches() { 1.491 + memset(_icons,0,sizeof(_icons)); 1.492 + memset(_lcons,0,sizeof(_lcons)); 1.493 + memset(_zcons,0,sizeof(_zcons)); 1.494 +} 1.495 + 1.496 + 1.497 +//--------------------------------find_int_type-------------------------------- 1.498 +const TypeInt* PhaseTransform::find_int_type(Node* n) { 1.499 + if (n == NULL) return NULL; 1.500 + // Call type_or_null(n) to determine node's type since we might be in 1.501 + // parse phase and call n->Value() may return wrong type. 1.502 + // (For example, a phi node at the beginning of loop parsing is not ready.) 1.503 + const Type* t = type_or_null(n); 1.504 + if (t == NULL) return NULL; 1.505 + return t->isa_int(); 1.506 +} 1.507 + 1.508 + 1.509 +//-------------------------------find_long_type-------------------------------- 1.510 +const TypeLong* PhaseTransform::find_long_type(Node* n) { 1.511 + if (n == NULL) return NULL; 1.512 + // (See comment above on type_or_null.) 1.513 + const Type* t = type_or_null(n); 1.514 + if (t == NULL) return NULL; 1.515 + return t->isa_long(); 1.516 +} 1.517 + 1.518 + 1.519 +#ifndef PRODUCT 1.520 +void PhaseTransform::dump_old2new_map() const { 1.521 + _nodes.dump(); 1.522 +} 1.523 + 1.524 +void PhaseTransform::dump_new( uint nidx ) const { 1.525 + for( uint i=0; i<_nodes.Size(); i++ ) 1.526 + if( _nodes[i] && _nodes[i]->_idx == nidx ) { 1.527 + _nodes[i]->dump(); 1.528 + tty->cr(); 1.529 + tty->print_cr("Old index= %d",i); 1.530 + return; 1.531 + } 1.532 + tty->print_cr("Node %d not found in the new indices", nidx); 1.533 +} 1.534 + 1.535 +//------------------------------dump_types------------------------------------- 1.536 +void PhaseTransform::dump_types( ) const { 1.537 + _types.dump(); 1.538 +} 1.539 + 1.540 +//------------------------------dump_nodes_and_types--------------------------- 1.541 +void PhaseTransform::dump_nodes_and_types(const Node *root, uint depth, bool only_ctrl) { 1.542 + VectorSet visited(Thread::current()->resource_area()); 1.543 + dump_nodes_and_types_recur( root, depth, only_ctrl, visited ); 1.544 +} 1.545 + 1.546 +//------------------------------dump_nodes_and_types_recur--------------------- 1.547 +void PhaseTransform::dump_nodes_and_types_recur( const Node *n, uint depth, bool only_ctrl, VectorSet &visited) { 1.548 + if( !n ) return; 1.549 + if( depth == 0 ) return; 1.550 + if( visited.test_set(n->_idx) ) return; 1.551 + for( uint i=0; i<n->len(); i++ ) { 1.552 + if( only_ctrl && !(n->is_Region()) && i != TypeFunc::Control ) continue; 1.553 + dump_nodes_and_types_recur( n->in(i), depth-1, only_ctrl, visited ); 1.554 + } 1.555 + n->dump(); 1.556 + if (type_or_null(n) != NULL) { 1.557 + tty->print(" "); type(n)->dump(); tty->cr(); 1.558 + } 1.559 +} 1.560 + 1.561 +#endif 1.562 + 1.563 + 1.564 +//============================================================================= 1.565 +//------------------------------PhaseValues------------------------------------ 1.566 +// Set minimum table size to "255" 1.567 +PhaseValues::PhaseValues( Arena *arena, uint est_max_size ) : PhaseTransform(arena, GVN), _table(arena, est_max_size) { 1.568 + NOT_PRODUCT( clear_new_values(); ) 1.569 +} 1.570 + 1.571 +//------------------------------PhaseValues------------------------------------ 1.572 +// Set minimum table size to "255" 1.573 +PhaseValues::PhaseValues( PhaseValues *ptv ) : PhaseTransform( ptv, GVN ), 1.574 + _table(&ptv->_table) { 1.575 + NOT_PRODUCT( clear_new_values(); ) 1.576 +} 1.577 + 1.578 +//------------------------------PhaseValues------------------------------------ 1.579 +// Used by +VerifyOpto. Clear out hash table but copy _types array. 1.580 +PhaseValues::PhaseValues( PhaseValues *ptv, const char *dummy ) : PhaseTransform( ptv, GVN ), 1.581 + _table(ptv->arena(),ptv->_table.size()) { 1.582 + NOT_PRODUCT( clear_new_values(); ) 1.583 +} 1.584 + 1.585 +//------------------------------~PhaseValues----------------------------------- 1.586 +#ifndef PRODUCT 1.587 +PhaseValues::~PhaseValues() { 1.588 + _table.dump(); 1.589 + 1.590 + // Statistics for value progress and efficiency 1.591 + if( PrintCompilation && Verbose && WizardMode ) { 1.592 + tty->print("\n%sValues: %d nodes ---> %d/%d (%d)", 1.593 + is_IterGVN() ? "Iter" : " ", C->unique(), made_progress(), made_transforms(), made_new_values()); 1.594 + if( made_transforms() != 0 ) { 1.595 + tty->print_cr(" ratio %f", made_progress()/(float)made_transforms() ); 1.596 + } else { 1.597 + tty->cr(); 1.598 + } 1.599 + } 1.600 +} 1.601 +#endif 1.602 + 1.603 +//------------------------------makecon---------------------------------------- 1.604 +ConNode* PhaseTransform::makecon(const Type *t) { 1.605 + assert(t->singleton(), "must be a constant"); 1.606 + assert(!t->empty() || t == Type::TOP, "must not be vacuous range"); 1.607 + switch (t->base()) { // fast paths 1.608 + case Type::Half: 1.609 + case Type::Top: return (ConNode*) C->top(); 1.610 + case Type::Int: return intcon( t->is_int()->get_con() ); 1.611 + case Type::Long: return longcon( t->is_long()->get_con() ); 1.612 + } 1.613 + if (t->is_zero_type()) 1.614 + return zerocon(t->basic_type()); 1.615 + return uncached_makecon(t); 1.616 +} 1.617 + 1.618 +//--------------------------uncached_makecon----------------------------------- 1.619 +// Make an idealized constant - one of ConINode, ConPNode, etc. 1.620 +ConNode* PhaseValues::uncached_makecon(const Type *t) { 1.621 + assert(t->singleton(), "must be a constant"); 1.622 + ConNode* x = ConNode::make(C, t); 1.623 + ConNode* k = (ConNode*)hash_find_insert(x); // Value numbering 1.624 + if (k == NULL) { 1.625 + set_type(x, t); // Missed, provide type mapping 1.626 + GrowableArray<Node_Notes*>* nna = C->node_note_array(); 1.627 + if (nna != NULL) { 1.628 + Node_Notes* loc = C->locate_node_notes(nna, x->_idx, true); 1.629 + loc->clear(); // do not put debug info on constants 1.630 + } 1.631 + } else { 1.632 + x->destruct(); // Hit, destroy duplicate constant 1.633 + x = k; // use existing constant 1.634 + } 1.635 + return x; 1.636 +} 1.637 + 1.638 +//------------------------------intcon----------------------------------------- 1.639 +// Fast integer constant. Same as "transform(new ConINode(TypeInt::make(i)))" 1.640 +ConINode* PhaseTransform::intcon(int i) { 1.641 + // Small integer? Check cache! Check that cached node is not dead 1.642 + if (i >= _icon_min && i <= _icon_max) { 1.643 + ConINode* icon = _icons[i-_icon_min]; 1.644 + if (icon != NULL && icon->in(TypeFunc::Control) != NULL) 1.645 + return icon; 1.646 + } 1.647 + ConINode* icon = (ConINode*) uncached_makecon(TypeInt::make(i)); 1.648 + assert(icon->is_Con(), ""); 1.649 + if (i >= _icon_min && i <= _icon_max) 1.650 + _icons[i-_icon_min] = icon; // Cache small integers 1.651 + return icon; 1.652 +} 1.653 + 1.654 +//------------------------------longcon---------------------------------------- 1.655 +// Fast long constant. 1.656 +ConLNode* PhaseTransform::longcon(jlong l) { 1.657 + // Small integer? Check cache! Check that cached node is not dead 1.658 + if (l >= _lcon_min && l <= _lcon_max) { 1.659 + ConLNode* lcon = _lcons[l-_lcon_min]; 1.660 + if (lcon != NULL && lcon->in(TypeFunc::Control) != NULL) 1.661 + return lcon; 1.662 + } 1.663 + ConLNode* lcon = (ConLNode*) uncached_makecon(TypeLong::make(l)); 1.664 + assert(lcon->is_Con(), ""); 1.665 + if (l >= _lcon_min && l <= _lcon_max) 1.666 + _lcons[l-_lcon_min] = lcon; // Cache small integers 1.667 + return lcon; 1.668 +} 1.669 + 1.670 +//------------------------------zerocon----------------------------------------- 1.671 +// Fast zero or null constant. Same as "transform(ConNode::make(Type::get_zero_type(bt)))" 1.672 +ConNode* PhaseTransform::zerocon(BasicType bt) { 1.673 + assert((uint)bt <= _zcon_max, "domain check"); 1.674 + ConNode* zcon = _zcons[bt]; 1.675 + if (zcon != NULL && zcon->in(TypeFunc::Control) != NULL) 1.676 + return zcon; 1.677 + zcon = (ConNode*) uncached_makecon(Type::get_zero_type(bt)); 1.678 + _zcons[bt] = zcon; 1.679 + return zcon; 1.680 +} 1.681 + 1.682 + 1.683 + 1.684 +//============================================================================= 1.685 +//------------------------------transform-------------------------------------- 1.686 +// Return a node which computes the same function as this node, but in a 1.687 +// faster or cheaper fashion. 1.688 +Node *PhaseGVN::transform( Node *n ) { 1.689 + return transform_no_reclaim(n); 1.690 +} 1.691 + 1.692 +//------------------------------transform-------------------------------------- 1.693 +// Return a node which computes the same function as this node, but 1.694 +// in a faster or cheaper fashion. 1.695 +Node *PhaseGVN::transform_no_reclaim( Node *n ) { 1.696 + NOT_PRODUCT( set_transforms(); ) 1.697 + 1.698 + // Apply the Ideal call in a loop until it no longer applies 1.699 + Node *k = n; 1.700 + NOT_PRODUCT( uint loop_count = 0; ) 1.701 + while( 1 ) { 1.702 + Node *i = k->Ideal(this, /*can_reshape=*/false); 1.703 + if( !i ) break; 1.704 + assert( i->_idx >= k->_idx, "Idealize should return new nodes, use Identity to return old nodes" ); 1.705 + k = i; 1.706 + assert(loop_count++ < K, "infinite loop in PhaseGVN::transform"); 1.707 + } 1.708 + NOT_PRODUCT( if( loop_count != 0 ) { set_progress(); } ) 1.709 + 1.710 + 1.711 + // If brand new node, make space in type array. 1.712 + ensure_type_or_null(k); 1.713 + 1.714 + // Since I just called 'Value' to compute the set of run-time values 1.715 + // for this Node, and 'Value' is non-local (and therefore expensive) I'll 1.716 + // cache Value. Later requests for the local phase->type of this Node can 1.717 + // use the cached Value instead of suffering with 'bottom_type'. 1.718 + const Type *t = k->Value(this); // Get runtime Value set 1.719 + assert(t != NULL, "value sanity"); 1.720 + if (type_or_null(k) != t) { 1.721 +#ifndef PRODUCT 1.722 + // Do not count initial visit to node as a transformation 1.723 + if (type_or_null(k) == NULL) { 1.724 + inc_new_values(); 1.725 + set_progress(); 1.726 + } 1.727 +#endif 1.728 + set_type(k, t); 1.729 + // If k is a TypeNode, capture any more-precise type permanently into Node 1.730 + k->raise_bottom_type(t); 1.731 + } 1.732 + 1.733 + if( t->singleton() && !k->is_Con() ) { 1.734 + NOT_PRODUCT( set_progress(); ) 1.735 + return makecon(t); // Turn into a constant 1.736 + } 1.737 + 1.738 + // Now check for Identities 1.739 + Node *i = k->Identity(this); // Look for a nearby replacement 1.740 + if( i != k ) { // Found? Return replacement! 1.741 + NOT_PRODUCT( set_progress(); ) 1.742 + return i; 1.743 + } 1.744 + 1.745 + // Global Value Numbering 1.746 + i = hash_find_insert(k); // Insert if new 1.747 + if( i && (i != k) ) { 1.748 + // Return the pre-existing node 1.749 + NOT_PRODUCT( set_progress(); ) 1.750 + return i; 1.751 + } 1.752 + 1.753 + // Return Idealized original 1.754 + return k; 1.755 +} 1.756 + 1.757 +#ifdef ASSERT 1.758 +//------------------------------dead_loop_check-------------------------------- 1.759 +// Check for a simple dead loop when a data node references itself directly 1.760 +// or through an other data node excluding cons and phis. 1.761 +void PhaseGVN::dead_loop_check( Node *n ) { 1.762 + // Phi may reference itself in a loop 1.763 + if (n != NULL && !n->is_dead_loop_safe() && !n->is_CFG()) { 1.764 + // Do 2 levels check and only data inputs. 1.765 + bool no_dead_loop = true; 1.766 + uint cnt = n->req(); 1.767 + for (uint i = 1; i < cnt && no_dead_loop; i++) { 1.768 + Node *in = n->in(i); 1.769 + if (in == n) { 1.770 + no_dead_loop = false; 1.771 + } else if (in != NULL && !in->is_dead_loop_safe()) { 1.772 + uint icnt = in->req(); 1.773 + for (uint j = 1; j < icnt && no_dead_loop; j++) { 1.774 + if (in->in(j) == n || in->in(j) == in) 1.775 + no_dead_loop = false; 1.776 + } 1.777 + } 1.778 + } 1.779 + if (!no_dead_loop) n->dump(3); 1.780 + assert(no_dead_loop, "dead loop detected"); 1.781 + } 1.782 +} 1.783 +#endif 1.784 + 1.785 +//============================================================================= 1.786 +//------------------------------PhaseIterGVN----------------------------------- 1.787 +// Initialize hash table to fresh and clean for +VerifyOpto 1.788 +PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn, const char *dummy ) : PhaseGVN(igvn,dummy), _worklist( ), 1.789 + _stack(C->unique() >> 1), 1.790 + _delay_transform(false) { 1.791 +} 1.792 + 1.793 +//------------------------------PhaseIterGVN----------------------------------- 1.794 +// Initialize with previous PhaseIterGVN info; used by PhaseCCP 1.795 +PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn ) : PhaseGVN(igvn), 1.796 + _worklist( igvn->_worklist ), 1.797 + _stack( igvn->_stack ), 1.798 + _delay_transform(igvn->_delay_transform) 1.799 +{ 1.800 +} 1.801 + 1.802 +//------------------------------PhaseIterGVN----------------------------------- 1.803 +// Initialize with previous PhaseGVN info from Parser 1.804 +PhaseIterGVN::PhaseIterGVN( PhaseGVN *gvn ) : PhaseGVN(gvn), 1.805 + _worklist(*C->for_igvn()), 1.806 + _stack(C->unique() >> 1), 1.807 + _delay_transform(false) 1.808 +{ 1.809 + uint max; 1.810 + 1.811 + // Dead nodes in the hash table inherited from GVN were not treated as 1.812 + // roots during def-use info creation; hence they represent an invisible 1.813 + // use. Clear them out. 1.814 + max = _table.size(); 1.815 + for( uint i = 0; i < max; ++i ) { 1.816 + Node *n = _table.at(i); 1.817 + if(n != NULL && n != _table.sentinel() && n->outcnt() == 0) { 1.818 + if( n->is_top() ) continue; 1.819 + assert( false, "Parse::remove_useless_nodes missed this node"); 1.820 + hash_delete(n); 1.821 + } 1.822 + } 1.823 + 1.824 + // Any Phis or Regions on the worklist probably had uses that could not 1.825 + // make more progress because the uses were made while the Phis and Regions 1.826 + // were in half-built states. Put all uses of Phis and Regions on worklist. 1.827 + max = _worklist.size(); 1.828 + for( uint j = 0; j < max; j++ ) { 1.829 + Node *n = _worklist.at(j); 1.830 + uint uop = n->Opcode(); 1.831 + if( uop == Op_Phi || uop == Op_Region || 1.832 + n->is_Type() || 1.833 + n->is_Mem() ) 1.834 + add_users_to_worklist(n); 1.835 + } 1.836 +} 1.837 + 1.838 + 1.839 +#ifndef PRODUCT 1.840 +void PhaseIterGVN::verify_step(Node* n) { 1.841 + _verify_window[_verify_counter % _verify_window_size] = n; 1.842 + ++_verify_counter; 1.843 + ResourceMark rm; 1.844 + ResourceArea *area = Thread::current()->resource_area(); 1.845 + VectorSet old_space(area), new_space(area); 1.846 + if (C->unique() < 1000 || 1.847 + 0 == _verify_counter % (C->unique() < 10000 ? 10 : 100)) { 1.848 + ++_verify_full_passes; 1.849 + Node::verify_recur(C->root(), -1, old_space, new_space); 1.850 + } 1.851 + const int verify_depth = 4; 1.852 + for ( int i = 0; i < _verify_window_size; i++ ) { 1.853 + Node* n = _verify_window[i]; 1.854 + if ( n == NULL ) continue; 1.855 + if( n->in(0) == NodeSentinel ) { // xform_idom 1.856 + _verify_window[i] = n->in(1); 1.857 + --i; continue; 1.858 + } 1.859 + // Typical fanout is 1-2, so this call visits about 6 nodes. 1.860 + Node::verify_recur(n, verify_depth, old_space, new_space); 1.861 + } 1.862 +} 1.863 +#endif 1.864 + 1.865 + 1.866 +//------------------------------init_worklist---------------------------------- 1.867 +// Initialize worklist for each node. 1.868 +void PhaseIterGVN::init_worklist( Node *n ) { 1.869 + if( _worklist.member(n) ) return; 1.870 + _worklist.push(n); 1.871 + uint cnt = n->req(); 1.872 + for( uint i =0 ; i < cnt; i++ ) { 1.873 + Node *m = n->in(i); 1.874 + if( m ) init_worklist(m); 1.875 + } 1.876 +} 1.877 + 1.878 +//------------------------------optimize--------------------------------------- 1.879 +void PhaseIterGVN::optimize() { 1.880 + debug_only(uint num_processed = 0;); 1.881 +#ifndef PRODUCT 1.882 + { 1.883 + _verify_counter = 0; 1.884 + _verify_full_passes = 0; 1.885 + for ( int i = 0; i < _verify_window_size; i++ ) { 1.886 + _verify_window[i] = NULL; 1.887 + } 1.888 + } 1.889 +#endif 1.890 + 1.891 +#ifdef ASSERT 1.892 + Node* prev = NULL; 1.893 + uint rep_cnt = 0; 1.894 +#endif 1.895 + uint loop_count = 0; 1.896 + 1.897 + // Pull from worklist; transform node; 1.898 + // If node has changed: update edge info and put uses on worklist. 1.899 + while( _worklist.size() ) { 1.900 + if (C->check_node_count(NodeLimitFudgeFactor * 2, 1.901 + "out of nodes optimizing method")) { 1.902 + return; 1.903 + } 1.904 + Node *n = _worklist.pop(); 1.905 + if (++loop_count >= K * C->live_nodes()) { 1.906 + debug_only(n->dump(4);) 1.907 + assert(false, "infinite loop in PhaseIterGVN::optimize"); 1.908 + C->record_method_not_compilable("infinite loop in PhaseIterGVN::optimize"); 1.909 + return; 1.910 + } 1.911 +#ifdef ASSERT 1.912 + if (n == prev) { 1.913 + if (++rep_cnt > 3) { 1.914 + n->dump(4); 1.915 + assert(false, "loop in Ideal transformation"); 1.916 + } 1.917 + } else { 1.918 + rep_cnt = 0; 1.919 + } 1.920 + prev = n; 1.921 +#endif 1.922 + if (TraceIterativeGVN && Verbose) { 1.923 + tty->print(" Pop "); 1.924 + NOT_PRODUCT( n->dump(); ) 1.925 + debug_only(if( (num_processed++ % 100) == 0 ) _worklist.print_set();) 1.926 + } 1.927 + 1.928 + if (n->outcnt() != 0) { 1.929 + 1.930 +#ifndef PRODUCT 1.931 + uint wlsize = _worklist.size(); 1.932 + const Type* oldtype = type_or_null(n); 1.933 +#endif //PRODUCT 1.934 + 1.935 + Node *nn = transform_old(n); 1.936 + 1.937 +#ifndef PRODUCT 1.938 + if (TraceIterativeGVN) { 1.939 + const Type* newtype = type_or_null(n); 1.940 + if (nn != n) { 1.941 + // print old node 1.942 + tty->print("< "); 1.943 + if (oldtype != newtype && oldtype != NULL) { 1.944 + oldtype->dump(); 1.945 + } 1.946 + do { tty->print("\t"); } while (tty->position() < 16); 1.947 + tty->print("<"); 1.948 + n->dump(); 1.949 + } 1.950 + if (oldtype != newtype || nn != n) { 1.951 + // print new node and/or new type 1.952 + if (oldtype == NULL) { 1.953 + tty->print("* "); 1.954 + } else if (nn != n) { 1.955 + tty->print("> "); 1.956 + } else { 1.957 + tty->print("= "); 1.958 + } 1.959 + if (newtype == NULL) { 1.960 + tty->print("null"); 1.961 + } else { 1.962 + newtype->dump(); 1.963 + } 1.964 + do { tty->print("\t"); } while (tty->position() < 16); 1.965 + nn->dump(); 1.966 + } 1.967 + if (Verbose && wlsize < _worklist.size()) { 1.968 + tty->print(" Push {"); 1.969 + while (wlsize != _worklist.size()) { 1.970 + Node* pushed = _worklist.at(wlsize++); 1.971 + tty->print(" %d", pushed->_idx); 1.972 + } 1.973 + tty->print_cr(" }"); 1.974 + } 1.975 + } 1.976 + if( VerifyIterativeGVN && nn != n ) { 1.977 + verify_step((Node*) NULL); // ignore n, it might be subsumed 1.978 + } 1.979 +#endif 1.980 + } else if (!n->is_top()) { 1.981 + remove_dead_node(n); 1.982 + } 1.983 + } 1.984 + 1.985 +#ifndef PRODUCT 1.986 + C->verify_graph_edges(); 1.987 + if( VerifyOpto && allow_progress() ) { 1.988 + // Must turn off allow_progress to enable assert and break recursion 1.989 + C->root()->verify(); 1.990 + { // Check if any progress was missed using IterGVN 1.991 + // Def-Use info enables transformations not attempted in wash-pass 1.992 + // e.g. Region/Phi cleanup, ... 1.993 + // Null-check elision -- may not have reached fixpoint 1.994 + // do not propagate to dominated nodes 1.995 + ResourceMark rm; 1.996 + PhaseIterGVN igvn2(this,"Verify"); // Fresh and clean! 1.997 + // Fill worklist completely 1.998 + igvn2.init_worklist(C->root()); 1.999 + 1.1000 + igvn2.set_allow_progress(false); 1.1001 + igvn2.optimize(); 1.1002 + igvn2.set_allow_progress(true); 1.1003 + } 1.1004 + } 1.1005 + if ( VerifyIterativeGVN && PrintOpto ) { 1.1006 + if ( _verify_counter == _verify_full_passes ) 1.1007 + tty->print_cr("VerifyIterativeGVN: %d transforms and verify passes", 1.1008 + (int) _verify_full_passes); 1.1009 + else 1.1010 + tty->print_cr("VerifyIterativeGVN: %d transforms, %d full verify passes", 1.1011 + (int) _verify_counter, (int) _verify_full_passes); 1.1012 + } 1.1013 +#endif 1.1014 +} 1.1015 + 1.1016 + 1.1017 +//------------------register_new_node_with_optimizer--------------------------- 1.1018 +// Register a new node with the optimizer. Update the types array, the def-use 1.1019 +// info. Put on worklist. 1.1020 +Node* PhaseIterGVN::register_new_node_with_optimizer(Node* n, Node* orig) { 1.1021 + set_type_bottom(n); 1.1022 + _worklist.push(n); 1.1023 + if (orig != NULL) C->copy_node_notes_to(n, orig); 1.1024 + return n; 1.1025 +} 1.1026 + 1.1027 +//------------------------------transform-------------------------------------- 1.1028 +// Non-recursive: idealize Node 'n' with respect to its inputs and its value 1.1029 +Node *PhaseIterGVN::transform( Node *n ) { 1.1030 + if (_delay_transform) { 1.1031 + // Register the node but don't optimize for now 1.1032 + register_new_node_with_optimizer(n); 1.1033 + return n; 1.1034 + } 1.1035 + 1.1036 + // If brand new node, make space in type array, and give it a type. 1.1037 + ensure_type_or_null(n); 1.1038 + if (type_or_null(n) == NULL) { 1.1039 + set_type_bottom(n); 1.1040 + } 1.1041 + 1.1042 + return transform_old(n); 1.1043 +} 1.1044 + 1.1045 +//------------------------------transform_old---------------------------------- 1.1046 +Node *PhaseIterGVN::transform_old( Node *n ) { 1.1047 +#ifndef PRODUCT 1.1048 + debug_only(uint loop_count = 0;); 1.1049 + set_transforms(); 1.1050 +#endif 1.1051 + // Remove 'n' from hash table in case it gets modified 1.1052 + _table.hash_delete(n); 1.1053 + if( VerifyIterativeGVN ) { 1.1054 + assert( !_table.find_index(n->_idx), "found duplicate entry in table"); 1.1055 + } 1.1056 + 1.1057 + // Apply the Ideal call in a loop until it no longer applies 1.1058 + Node *k = n; 1.1059 + DEBUG_ONLY(dead_loop_check(k);) 1.1060 + DEBUG_ONLY(bool is_new = (k->outcnt() == 0);) 1.1061 + Node *i = k->Ideal(this, /*can_reshape=*/true); 1.1062 + assert(i != k || is_new || i->outcnt() > 0, "don't return dead nodes"); 1.1063 +#ifndef PRODUCT 1.1064 + if( VerifyIterativeGVN ) 1.1065 + verify_step(k); 1.1066 + if( i && VerifyOpto ) { 1.1067 + if( !allow_progress() ) { 1.1068 + if (i->is_Add() && i->outcnt() == 1) { 1.1069 + // Switched input to left side because this is the only use 1.1070 + } else if( i->is_If() && (i->in(0) == NULL) ) { 1.1071 + // This IF is dead because it is dominated by an equivalent IF When 1.1072 + // dominating if changed, info is not propagated sparsely to 'this' 1.1073 + // Propagating this info further will spuriously identify other 1.1074 + // progress. 1.1075 + return i; 1.1076 + } else 1.1077 + set_progress(); 1.1078 + } else 1.1079 + set_progress(); 1.1080 + } 1.1081 +#endif 1.1082 + 1.1083 + while( i ) { 1.1084 +#ifndef PRODUCT 1.1085 + debug_only( if( loop_count >= K ) i->dump(4); ) 1.1086 + assert(loop_count < K, "infinite loop in PhaseIterGVN::transform"); 1.1087 + debug_only( loop_count++; ) 1.1088 +#endif 1.1089 + assert((i->_idx >= k->_idx) || i->is_top(), "Idealize should return new nodes, use Identity to return old nodes"); 1.1090 + // Made a change; put users of original Node on worklist 1.1091 + add_users_to_worklist( k ); 1.1092 + // Replacing root of transform tree? 1.1093 + if( k != i ) { 1.1094 + // Make users of old Node now use new. 1.1095 + subsume_node( k, i ); 1.1096 + k = i; 1.1097 + } 1.1098 + DEBUG_ONLY(dead_loop_check(k);) 1.1099 + // Try idealizing again 1.1100 + DEBUG_ONLY(is_new = (k->outcnt() == 0);) 1.1101 + i = k->Ideal(this, /*can_reshape=*/true); 1.1102 + assert(i != k || is_new || i->outcnt() > 0, "don't return dead nodes"); 1.1103 +#ifndef PRODUCT 1.1104 + if( VerifyIterativeGVN ) 1.1105 + verify_step(k); 1.1106 + if( i && VerifyOpto ) set_progress(); 1.1107 +#endif 1.1108 + } 1.1109 + 1.1110 + // If brand new node, make space in type array. 1.1111 + ensure_type_or_null(k); 1.1112 + 1.1113 + // See what kind of values 'k' takes on at runtime 1.1114 + const Type *t = k->Value(this); 1.1115 + assert(t != NULL, "value sanity"); 1.1116 + 1.1117 + // Since I just called 'Value' to compute the set of run-time values 1.1118 + // for this Node, and 'Value' is non-local (and therefore expensive) I'll 1.1119 + // cache Value. Later requests for the local phase->type of this Node can 1.1120 + // use the cached Value instead of suffering with 'bottom_type'. 1.1121 + if (t != type_or_null(k)) { 1.1122 + NOT_PRODUCT( set_progress(); ) 1.1123 + NOT_PRODUCT( inc_new_values();) 1.1124 + set_type(k, t); 1.1125 + // If k is a TypeNode, capture any more-precise type permanently into Node 1.1126 + k->raise_bottom_type(t); 1.1127 + // Move users of node to worklist 1.1128 + add_users_to_worklist( k ); 1.1129 + } 1.1130 + 1.1131 + // If 'k' computes a constant, replace it with a constant 1.1132 + if( t->singleton() && !k->is_Con() ) { 1.1133 + NOT_PRODUCT( set_progress(); ) 1.1134 + Node *con = makecon(t); // Make a constant 1.1135 + add_users_to_worklist( k ); 1.1136 + subsume_node( k, con ); // Everybody using k now uses con 1.1137 + return con; 1.1138 + } 1.1139 + 1.1140 + // Now check for Identities 1.1141 + i = k->Identity(this); // Look for a nearby replacement 1.1142 + if( i != k ) { // Found? Return replacement! 1.1143 + NOT_PRODUCT( set_progress(); ) 1.1144 + add_users_to_worklist( k ); 1.1145 + subsume_node( k, i ); // Everybody using k now uses i 1.1146 + return i; 1.1147 + } 1.1148 + 1.1149 + // Global Value Numbering 1.1150 + i = hash_find_insert(k); // Check for pre-existing node 1.1151 + if( i && (i != k) ) { 1.1152 + // Return the pre-existing node if it isn't dead 1.1153 + NOT_PRODUCT( set_progress(); ) 1.1154 + add_users_to_worklist( k ); 1.1155 + subsume_node( k, i ); // Everybody using k now uses i 1.1156 + return i; 1.1157 + } 1.1158 + 1.1159 + // Return Idealized original 1.1160 + return k; 1.1161 +} 1.1162 + 1.1163 +//---------------------------------saturate------------------------------------ 1.1164 +const Type* PhaseIterGVN::saturate(const Type* new_type, const Type* old_type, 1.1165 + const Type* limit_type) const { 1.1166 + return new_type->narrow(old_type); 1.1167 +} 1.1168 + 1.1169 +//------------------------------remove_globally_dead_node---------------------- 1.1170 +// Kill a globally dead Node. All uses are also globally dead and are 1.1171 +// aggressively trimmed. 1.1172 +void PhaseIterGVN::remove_globally_dead_node( Node *dead ) { 1.1173 + enum DeleteProgress { 1.1174 + PROCESS_INPUTS, 1.1175 + PROCESS_OUTPUTS 1.1176 + }; 1.1177 + assert(_stack.is_empty(), "not empty"); 1.1178 + _stack.push(dead, PROCESS_INPUTS); 1.1179 + 1.1180 + while (_stack.is_nonempty()) { 1.1181 + dead = _stack.node(); 1.1182 + uint progress_state = _stack.index(); 1.1183 + assert(dead != C->root(), "killing root, eh?"); 1.1184 + assert(!dead->is_top(), "add check for top when pushing"); 1.1185 + NOT_PRODUCT( set_progress(); ) 1.1186 + if (progress_state == PROCESS_INPUTS) { 1.1187 + // After following inputs, continue to outputs 1.1188 + _stack.set_index(PROCESS_OUTPUTS); 1.1189 + if (!dead->is_Con()) { // Don't kill cons but uses 1.1190 + bool recurse = false; 1.1191 + // Remove from hash table 1.1192 + _table.hash_delete( dead ); 1.1193 + // Smash all inputs to 'dead', isolating him completely 1.1194 + for (uint i = 0; i < dead->req(); i++) { 1.1195 + Node *in = dead->in(i); 1.1196 + if (in != NULL && in != C->top()) { // Points to something? 1.1197 + int nrep = dead->replace_edge(in, NULL); // Kill edges 1.1198 + assert((nrep > 0), "sanity"); 1.1199 + if (in->outcnt() == 0) { // Made input go dead? 1.1200 + _stack.push(in, PROCESS_INPUTS); // Recursively remove 1.1201 + recurse = true; 1.1202 + } else if (in->outcnt() == 1 && 1.1203 + in->has_special_unique_user()) { 1.1204 + _worklist.push(in->unique_out()); 1.1205 + } else if (in->outcnt() <= 2 && dead->is_Phi()) { 1.1206 + if (in->Opcode() == Op_Region) { 1.1207 + _worklist.push(in); 1.1208 + } else if (in->is_Store()) { 1.1209 + DUIterator_Fast imax, i = in->fast_outs(imax); 1.1210 + _worklist.push(in->fast_out(i)); 1.1211 + i++; 1.1212 + if (in->outcnt() == 2) { 1.1213 + _worklist.push(in->fast_out(i)); 1.1214 + i++; 1.1215 + } 1.1216 + assert(!(i < imax), "sanity"); 1.1217 + } 1.1218 + } 1.1219 + if (ReduceFieldZeroing && dead->is_Load() && i == MemNode::Memory && 1.1220 + in->is_Proj() && in->in(0) != NULL && in->in(0)->is_Initialize()) { 1.1221 + // A Load that directly follows an InitializeNode is 1.1222 + // going away. The Stores that follow are candidates 1.1223 + // again to be captured by the InitializeNode. 1.1224 + for (DUIterator_Fast jmax, j = in->fast_outs(jmax); j < jmax; j++) { 1.1225 + Node *n = in->fast_out(j); 1.1226 + if (n->is_Store()) { 1.1227 + _worklist.push(n); 1.1228 + } 1.1229 + } 1.1230 + } 1.1231 + } // if (in != NULL && in != C->top()) 1.1232 + } // for (uint i = 0; i < dead->req(); i++) 1.1233 + if (recurse) { 1.1234 + continue; 1.1235 + } 1.1236 + } // if (!dead->is_Con()) 1.1237 + } // if (progress_state == PROCESS_INPUTS) 1.1238 + 1.1239 + // Aggressively kill globally dead uses 1.1240 + // (Rather than pushing all the outs at once, we push one at a time, 1.1241 + // plus the parent to resume later, because of the indefinite number 1.1242 + // of edge deletions per loop trip.) 1.1243 + if (dead->outcnt() > 0) { 1.1244 + // Recursively remove output edges 1.1245 + _stack.push(dead->raw_out(0), PROCESS_INPUTS); 1.1246 + } else { 1.1247 + // Finished disconnecting all input and output edges. 1.1248 + _stack.pop(); 1.1249 + // Remove dead node from iterative worklist 1.1250 + _worklist.remove(dead); 1.1251 + // Constant node that has no out-edges and has only one in-edge from 1.1252 + // root is usually dead. However, sometimes reshaping walk makes 1.1253 + // it reachable by adding use edges. So, we will NOT count Con nodes 1.1254 + // as dead to be conservative about the dead node count at any 1.1255 + // given time. 1.1256 + if (!dead->is_Con()) { 1.1257 + C->record_dead_node(dead->_idx); 1.1258 + } 1.1259 + if (dead->is_macro()) { 1.1260 + C->remove_macro_node(dead); 1.1261 + } 1.1262 + if (dead->is_expensive()) { 1.1263 + C->remove_expensive_node(dead); 1.1264 + } 1.1265 + } 1.1266 + } // while (_stack.is_nonempty()) 1.1267 +} 1.1268 + 1.1269 +//------------------------------subsume_node----------------------------------- 1.1270 +// Remove users from node 'old' and add them to node 'nn'. 1.1271 +void PhaseIterGVN::subsume_node( Node *old, Node *nn ) { 1.1272 + assert( old != hash_find(old), "should already been removed" ); 1.1273 + assert( old != C->top(), "cannot subsume top node"); 1.1274 + // Copy debug or profile information to the new version: 1.1275 + C->copy_node_notes_to(nn, old); 1.1276 + // Move users of node 'old' to node 'nn' 1.1277 + for (DUIterator_Last imin, i = old->last_outs(imin); i >= imin; ) { 1.1278 + Node* use = old->last_out(i); // for each use... 1.1279 + // use might need re-hashing (but it won't if it's a new node) 1.1280 + bool is_in_table = _table.hash_delete( use ); 1.1281 + // Update use-def info as well 1.1282 + // We remove all occurrences of old within use->in, 1.1283 + // so as to avoid rehashing any node more than once. 1.1284 + // The hash table probe swamps any outer loop overhead. 1.1285 + uint num_edges = 0; 1.1286 + for (uint jmax = use->len(), j = 0; j < jmax; j++) { 1.1287 + if (use->in(j) == old) { 1.1288 + use->set_req(j, nn); 1.1289 + ++num_edges; 1.1290 + } 1.1291 + } 1.1292 + // Insert into GVN hash table if unique 1.1293 + // If a duplicate, 'use' will be cleaned up when pulled off worklist 1.1294 + if( is_in_table ) { 1.1295 + hash_find_insert(use); 1.1296 + } 1.1297 + i -= num_edges; // we deleted 1 or more copies of this edge 1.1298 + } 1.1299 + 1.1300 + // Smash all inputs to 'old', isolating him completely 1.1301 + Node *temp = new (C) Node(1); 1.1302 + temp->init_req(0,nn); // Add a use to nn to prevent him from dying 1.1303 + remove_dead_node( old ); 1.1304 + temp->del_req(0); // Yank bogus edge 1.1305 +#ifndef PRODUCT 1.1306 + if( VerifyIterativeGVN ) { 1.1307 + for ( int i = 0; i < _verify_window_size; i++ ) { 1.1308 + if ( _verify_window[i] == old ) 1.1309 + _verify_window[i] = nn; 1.1310 + } 1.1311 + } 1.1312 +#endif 1.1313 + _worklist.remove(temp); // this can be necessary 1.1314 + temp->destruct(); // reuse the _idx of this little guy 1.1315 +} 1.1316 + 1.1317 +//------------------------------add_users_to_worklist-------------------------- 1.1318 +void PhaseIterGVN::add_users_to_worklist0( Node *n ) { 1.1319 + for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1.1320 + _worklist.push(n->fast_out(i)); // Push on worklist 1.1321 + } 1.1322 +} 1.1323 + 1.1324 +void PhaseIterGVN::add_users_to_worklist( Node *n ) { 1.1325 + add_users_to_worklist0(n); 1.1326 + 1.1327 + // Move users of node to worklist 1.1328 + for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1.1329 + Node* use = n->fast_out(i); // Get use 1.1330 + 1.1331 + if( use->is_Multi() || // Multi-definer? Push projs on worklist 1.1332 + use->is_Store() ) // Enable store/load same address 1.1333 + add_users_to_worklist0(use); 1.1334 + 1.1335 + // If we changed the receiver type to a call, we need to revisit 1.1336 + // the Catch following the call. It's looking for a non-NULL 1.1337 + // receiver to know when to enable the regular fall-through path 1.1338 + // in addition to the NullPtrException path. 1.1339 + if (use->is_CallDynamicJava() && n == use->in(TypeFunc::Parms)) { 1.1340 + Node* p = use->as_CallDynamicJava()->proj_out(TypeFunc::Control); 1.1341 + if (p != NULL) { 1.1342 + add_users_to_worklist0(p); 1.1343 + } 1.1344 + } 1.1345 + 1.1346 + if( use->is_Cmp() ) { // Enable CMP/BOOL optimization 1.1347 + add_users_to_worklist(use); // Put Bool on worklist 1.1348 + // Look for the 'is_x2logic' pattern: "x ? : 0 : 1" and put the 1.1349 + // phi merging either 0 or 1 onto the worklist 1.1350 + if (use->outcnt() > 0) { 1.1351 + Node* bol = use->raw_out(0); 1.1352 + if (bol->outcnt() > 0) { 1.1353 + Node* iff = bol->raw_out(0); 1.1354 + if (iff->outcnt() == 2) { 1.1355 + Node* ifproj0 = iff->raw_out(0); 1.1356 + Node* ifproj1 = iff->raw_out(1); 1.1357 + if (ifproj0->outcnt() > 0 && ifproj1->outcnt() > 0) { 1.1358 + Node* region0 = ifproj0->raw_out(0); 1.1359 + Node* region1 = ifproj1->raw_out(0); 1.1360 + if( region0 == region1 ) 1.1361 + add_users_to_worklist0(region0); 1.1362 + } 1.1363 + } 1.1364 + } 1.1365 + } 1.1366 + } 1.1367 + 1.1368 + uint use_op = use->Opcode(); 1.1369 + // If changed Cast input, check Phi users for simple cycles 1.1370 + if( use->is_ConstraintCast() || use->is_CheckCastPP() ) { 1.1371 + for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1.1372 + Node* u = use->fast_out(i2); 1.1373 + if (u->is_Phi()) 1.1374 + _worklist.push(u); 1.1375 + } 1.1376 + } 1.1377 + // If changed LShift inputs, check RShift users for useless sign-ext 1.1378 + if( use_op == Op_LShiftI ) { 1.1379 + for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1.1380 + Node* u = use->fast_out(i2); 1.1381 + if (u->Opcode() == Op_RShiftI) 1.1382 + _worklist.push(u); 1.1383 + } 1.1384 + } 1.1385 + // If changed AddI/SubI inputs, check CmpU for range check optimization. 1.1386 + if (use_op == Op_AddI || use_op == Op_SubI) { 1.1387 + for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1.1388 + Node* u = use->fast_out(i2); 1.1389 + if (u->is_Cmp() && (u->Opcode() == Op_CmpU)) { 1.1390 + _worklist.push(u); 1.1391 + } 1.1392 + } 1.1393 + } 1.1394 + // If changed AddP inputs, check Stores for loop invariant 1.1395 + if( use_op == Op_AddP ) { 1.1396 + for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1.1397 + Node* u = use->fast_out(i2); 1.1398 + if (u->is_Mem()) 1.1399 + _worklist.push(u); 1.1400 + } 1.1401 + } 1.1402 + // If changed initialization activity, check dependent Stores 1.1403 + if (use_op == Op_Allocate || use_op == Op_AllocateArray) { 1.1404 + InitializeNode* init = use->as_Allocate()->initialization(); 1.1405 + if (init != NULL) { 1.1406 + Node* imem = init->proj_out(TypeFunc::Memory); 1.1407 + if (imem != NULL) add_users_to_worklist0(imem); 1.1408 + } 1.1409 + } 1.1410 + if (use_op == Op_Initialize) { 1.1411 + Node* imem = use->as_Initialize()->proj_out(TypeFunc::Memory); 1.1412 + if (imem != NULL) add_users_to_worklist0(imem); 1.1413 + } 1.1414 + } 1.1415 +} 1.1416 + 1.1417 +/** 1.1418 + * Remove the speculative part of all types that we know of 1.1419 + */ 1.1420 +void PhaseIterGVN::remove_speculative_types() { 1.1421 + assert(UseTypeSpeculation, "speculation is off"); 1.1422 + for (uint i = 0; i < _types.Size(); i++) { 1.1423 + const Type* t = _types.fast_lookup(i); 1.1424 + if (t != NULL) { 1.1425 + _types.map(i, t->remove_speculative()); 1.1426 + } 1.1427 + } 1.1428 + _table.check_no_speculative_types(); 1.1429 +} 1.1430 + 1.1431 +//============================================================================= 1.1432 +#ifndef PRODUCT 1.1433 +uint PhaseCCP::_total_invokes = 0; 1.1434 +uint PhaseCCP::_total_constants = 0; 1.1435 +#endif 1.1436 +//------------------------------PhaseCCP--------------------------------------- 1.1437 +// Conditional Constant Propagation, ala Wegman & Zadeck 1.1438 +PhaseCCP::PhaseCCP( PhaseIterGVN *igvn ) : PhaseIterGVN(igvn) { 1.1439 + NOT_PRODUCT( clear_constants(); ) 1.1440 + assert( _worklist.size() == 0, "" ); 1.1441 + // Clear out _nodes from IterGVN. Must be clear to transform call. 1.1442 + _nodes.clear(); // Clear out from IterGVN 1.1443 + analyze(); 1.1444 +} 1.1445 + 1.1446 +#ifndef PRODUCT 1.1447 +//------------------------------~PhaseCCP-------------------------------------- 1.1448 +PhaseCCP::~PhaseCCP() { 1.1449 + inc_invokes(); 1.1450 + _total_constants += count_constants(); 1.1451 +} 1.1452 +#endif 1.1453 + 1.1454 + 1.1455 +#ifdef ASSERT 1.1456 +static bool ccp_type_widens(const Type* t, const Type* t0) { 1.1457 + assert(t->meet(t0) == t, "Not monotonic"); 1.1458 + switch (t->base() == t0->base() ? t->base() : Type::Top) { 1.1459 + case Type::Int: 1.1460 + assert(t0->isa_int()->_widen <= t->isa_int()->_widen, "widen increases"); 1.1461 + break; 1.1462 + case Type::Long: 1.1463 + assert(t0->isa_long()->_widen <= t->isa_long()->_widen, "widen increases"); 1.1464 + break; 1.1465 + } 1.1466 + return true; 1.1467 +} 1.1468 +#endif //ASSERT 1.1469 + 1.1470 +//------------------------------analyze---------------------------------------- 1.1471 +void PhaseCCP::analyze() { 1.1472 + // Initialize all types to TOP, optimistic analysis 1.1473 + for (int i = C->unique() - 1; i >= 0; i--) { 1.1474 + _types.map(i,Type::TOP); 1.1475 + } 1.1476 + 1.1477 + // Push root onto worklist 1.1478 + Unique_Node_List worklist; 1.1479 + worklist.push(C->root()); 1.1480 + 1.1481 + // Pull from worklist; compute new value; push changes out. 1.1482 + // This loop is the meat of CCP. 1.1483 + while( worklist.size() ) { 1.1484 + Node *n = worklist.pop(); 1.1485 + const Type *t = n->Value(this); 1.1486 + if (t != type(n)) { 1.1487 + assert(ccp_type_widens(t, type(n)), "ccp type must widen"); 1.1488 +#ifndef PRODUCT 1.1489 + if( TracePhaseCCP ) { 1.1490 + t->dump(); 1.1491 + do { tty->print("\t"); } while (tty->position() < 16); 1.1492 + n->dump(); 1.1493 + } 1.1494 +#endif 1.1495 + set_type(n, t); 1.1496 + for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1.1497 + Node* m = n->fast_out(i); // Get user 1.1498 + if( m->is_Region() ) { // New path to Region? Must recheck Phis too 1.1499 + for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) { 1.1500 + Node* p = m->fast_out(i2); // Propagate changes to uses 1.1501 + if( p->bottom_type() != type(p) ) // If not already bottomed out 1.1502 + worklist.push(p); // Propagate change to user 1.1503 + } 1.1504 + } 1.1505 + // If we changed the receiver type to a call, we need to revisit 1.1506 + // the Catch following the call. It's looking for a non-NULL 1.1507 + // receiver to know when to enable the regular fall-through path 1.1508 + // in addition to the NullPtrException path 1.1509 + if (m->is_Call()) { 1.1510 + for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) { 1.1511 + Node* p = m->fast_out(i2); // Propagate changes to uses 1.1512 + if (p->is_Proj() && p->as_Proj()->_con == TypeFunc::Control && p->outcnt() == 1) 1.1513 + worklist.push(p->unique_out()); 1.1514 + } 1.1515 + } 1.1516 + if( m->bottom_type() != type(m) ) // If not already bottomed out 1.1517 + worklist.push(m); // Propagate change to user 1.1518 + } 1.1519 + } 1.1520 + } 1.1521 +} 1.1522 + 1.1523 +//------------------------------do_transform----------------------------------- 1.1524 +// Top level driver for the recursive transformer 1.1525 +void PhaseCCP::do_transform() { 1.1526 + // Correct leaves of new-space Nodes; they point to old-space. 1.1527 + C->set_root( transform(C->root())->as_Root() ); 1.1528 + assert( C->top(), "missing TOP node" ); 1.1529 + assert( C->root(), "missing root" ); 1.1530 +} 1.1531 + 1.1532 +//------------------------------transform-------------------------------------- 1.1533 +// Given a Node in old-space, clone him into new-space. 1.1534 +// Convert any of his old-space children into new-space children. 1.1535 +Node *PhaseCCP::transform( Node *n ) { 1.1536 + Node *new_node = _nodes[n->_idx]; // Check for transformed node 1.1537 + if( new_node != NULL ) 1.1538 + return new_node; // Been there, done that, return old answer 1.1539 + new_node = transform_once(n); // Check for constant 1.1540 + _nodes.map( n->_idx, new_node ); // Flag as having been cloned 1.1541 + 1.1542 + // Allocate stack of size _nodes.Size()/2 to avoid frequent realloc 1.1543 + GrowableArray <Node *> trstack(C->unique() >> 1); 1.1544 + 1.1545 + trstack.push(new_node); // Process children of cloned node 1.1546 + while ( trstack.is_nonempty() ) { 1.1547 + Node *clone = trstack.pop(); 1.1548 + uint cnt = clone->req(); 1.1549 + for( uint i = 0; i < cnt; i++ ) { // For all inputs do 1.1550 + Node *input = clone->in(i); 1.1551 + if( input != NULL ) { // Ignore NULLs 1.1552 + Node *new_input = _nodes[input->_idx]; // Check for cloned input node 1.1553 + if( new_input == NULL ) { 1.1554 + new_input = transform_once(input); // Check for constant 1.1555 + _nodes.map( input->_idx, new_input );// Flag as having been cloned 1.1556 + trstack.push(new_input); 1.1557 + } 1.1558 + assert( new_input == clone->in(i), "insanity check"); 1.1559 + } 1.1560 + } 1.1561 + } 1.1562 + return new_node; 1.1563 +} 1.1564 + 1.1565 + 1.1566 +//------------------------------transform_once--------------------------------- 1.1567 +// For PhaseCCP, transformation is IDENTITY unless Node computed a constant. 1.1568 +Node *PhaseCCP::transform_once( Node *n ) { 1.1569 + const Type *t = type(n); 1.1570 + // Constant? Use constant Node instead 1.1571 + if( t->singleton() ) { 1.1572 + Node *nn = n; // Default is to return the original constant 1.1573 + if( t == Type::TOP ) { 1.1574 + // cache my top node on the Compile instance 1.1575 + if( C->cached_top_node() == NULL || C->cached_top_node()->in(0) == NULL ) { 1.1576 + C->set_cached_top_node( ConNode::make(C, Type::TOP) ); 1.1577 + set_type(C->top(), Type::TOP); 1.1578 + } 1.1579 + nn = C->top(); 1.1580 + } 1.1581 + if( !n->is_Con() ) { 1.1582 + if( t != Type::TOP ) { 1.1583 + nn = makecon(t); // ConNode::make(t); 1.1584 + NOT_PRODUCT( inc_constants(); ) 1.1585 + } else if( n->is_Region() ) { // Unreachable region 1.1586 + // Note: nn == C->top() 1.1587 + n->set_req(0, NULL); // Cut selfreference 1.1588 + // Eagerly remove dead phis to avoid phis copies creation. 1.1589 + for (DUIterator i = n->outs(); n->has_out(i); i++) { 1.1590 + Node* m = n->out(i); 1.1591 + if( m->is_Phi() ) { 1.1592 + assert(type(m) == Type::TOP, "Unreachable region should not have live phis."); 1.1593 + replace_node(m, nn); 1.1594 + --i; // deleted this phi; rescan starting with next position 1.1595 + } 1.1596 + } 1.1597 + } 1.1598 + replace_node(n,nn); // Update DefUse edges for new constant 1.1599 + } 1.1600 + return nn; 1.1601 + } 1.1602 + 1.1603 + // If x is a TypeNode, capture any more-precise type permanently into Node 1.1604 + if (t != n->bottom_type()) { 1.1605 + hash_delete(n); // changing bottom type may force a rehash 1.1606 + n->raise_bottom_type(t); 1.1607 + _worklist.push(n); // n re-enters the hash table via the worklist 1.1608 + } 1.1609 + 1.1610 + // Idealize graph using DU info. Must clone() into new-space. 1.1611 + // DU info is generally used to show profitability, progress or safety 1.1612 + // (but generally not needed for correctness). 1.1613 + Node *nn = n->Ideal_DU_postCCP(this); 1.1614 + 1.1615 + // TEMPORARY fix to ensure that 2nd GVN pass eliminates NULL checks 1.1616 + switch( n->Opcode() ) { 1.1617 + case Op_FastLock: // Revisit FastLocks for lock coarsening 1.1618 + case Op_If: 1.1619 + case Op_CountedLoopEnd: 1.1620 + case Op_Region: 1.1621 + case Op_Loop: 1.1622 + case Op_CountedLoop: 1.1623 + case Op_Conv2B: 1.1624 + case Op_Opaque1: 1.1625 + case Op_Opaque2: 1.1626 + _worklist.push(n); 1.1627 + break; 1.1628 + default: 1.1629 + break; 1.1630 + } 1.1631 + if( nn ) { 1.1632 + _worklist.push(n); 1.1633 + // Put users of 'n' onto worklist for second igvn transform 1.1634 + add_users_to_worklist(n); 1.1635 + return nn; 1.1636 + } 1.1637 + 1.1638 + return n; 1.1639 +} 1.1640 + 1.1641 +//---------------------------------saturate------------------------------------ 1.1642 +const Type* PhaseCCP::saturate(const Type* new_type, const Type* old_type, 1.1643 + const Type* limit_type) const { 1.1644 + const Type* wide_type = new_type->widen(old_type, limit_type); 1.1645 + if (wide_type != new_type) { // did we widen? 1.1646 + // If so, we may have widened beyond the limit type. Clip it back down. 1.1647 + new_type = wide_type->filter(limit_type); 1.1648 + } 1.1649 + return new_type; 1.1650 +} 1.1651 + 1.1652 +//------------------------------print_statistics------------------------------- 1.1653 +#ifndef PRODUCT 1.1654 +void PhaseCCP::print_statistics() { 1.1655 + tty->print_cr("CCP: %d constants found: %d", _total_invokes, _total_constants); 1.1656 +} 1.1657 +#endif 1.1658 + 1.1659 + 1.1660 +//============================================================================= 1.1661 +#ifndef PRODUCT 1.1662 +uint PhasePeephole::_total_peepholes = 0; 1.1663 +#endif 1.1664 +//------------------------------PhasePeephole---------------------------------- 1.1665 +// Conditional Constant Propagation, ala Wegman & Zadeck 1.1666 +PhasePeephole::PhasePeephole( PhaseRegAlloc *regalloc, PhaseCFG &cfg ) 1.1667 + : PhaseTransform(Peephole), _regalloc(regalloc), _cfg(cfg) { 1.1668 + NOT_PRODUCT( clear_peepholes(); ) 1.1669 +} 1.1670 + 1.1671 +#ifndef PRODUCT 1.1672 +//------------------------------~PhasePeephole--------------------------------- 1.1673 +PhasePeephole::~PhasePeephole() { 1.1674 + _total_peepholes += count_peepholes(); 1.1675 +} 1.1676 +#endif 1.1677 + 1.1678 +//------------------------------transform-------------------------------------- 1.1679 +Node *PhasePeephole::transform( Node *n ) { 1.1680 + ShouldNotCallThis(); 1.1681 + return NULL; 1.1682 +} 1.1683 + 1.1684 +//------------------------------do_transform----------------------------------- 1.1685 +void PhasePeephole::do_transform() { 1.1686 + bool method_name_not_printed = true; 1.1687 + 1.1688 + // Examine each basic block 1.1689 + for (uint block_number = 1; block_number < _cfg.number_of_blocks(); ++block_number) { 1.1690 + Block* block = _cfg.get_block(block_number); 1.1691 + bool block_not_printed = true; 1.1692 + 1.1693 + // and each instruction within a block 1.1694 + uint end_index = block->number_of_nodes(); 1.1695 + // block->end_idx() not valid after PhaseRegAlloc 1.1696 + for( uint instruction_index = 1; instruction_index < end_index; ++instruction_index ) { 1.1697 + Node *n = block->get_node(instruction_index); 1.1698 + if( n->is_Mach() ) { 1.1699 + MachNode *m = n->as_Mach(); 1.1700 + int deleted_count = 0; 1.1701 + // check for peephole opportunities 1.1702 + MachNode *m2 = m->peephole( block, instruction_index, _regalloc, deleted_count, C ); 1.1703 + if( m2 != NULL ) { 1.1704 +#ifndef PRODUCT 1.1705 + if( PrintOptoPeephole ) { 1.1706 + // Print method, first time only 1.1707 + if( C->method() && method_name_not_printed ) { 1.1708 + C->method()->print_short_name(); tty->cr(); 1.1709 + method_name_not_printed = false; 1.1710 + } 1.1711 + // Print this block 1.1712 + if( Verbose && block_not_printed) { 1.1713 + tty->print_cr("in block"); 1.1714 + block->dump(); 1.1715 + block_not_printed = false; 1.1716 + } 1.1717 + // Print instructions being deleted 1.1718 + for( int i = (deleted_count - 1); i >= 0; --i ) { 1.1719 + block->get_node(instruction_index-i)->as_Mach()->format(_regalloc); tty->cr(); 1.1720 + } 1.1721 + tty->print_cr("replaced with"); 1.1722 + // Print new instruction 1.1723 + m2->format(_regalloc); 1.1724 + tty->print("\n\n"); 1.1725 + } 1.1726 +#endif 1.1727 + // Remove old nodes from basic block and update instruction_index 1.1728 + // (old nodes still exist and may have edges pointing to them 1.1729 + // as register allocation info is stored in the allocator using 1.1730 + // the node index to live range mappings.) 1.1731 + uint safe_instruction_index = (instruction_index - deleted_count); 1.1732 + for( ; (instruction_index > safe_instruction_index); --instruction_index ) { 1.1733 + block->remove_node( instruction_index ); 1.1734 + } 1.1735 + // install new node after safe_instruction_index 1.1736 + block->insert_node(m2, safe_instruction_index + 1); 1.1737 + end_index = block->number_of_nodes() - 1; // Recompute new block size 1.1738 + NOT_PRODUCT( inc_peepholes(); ) 1.1739 + } 1.1740 + } 1.1741 + } 1.1742 + } 1.1743 +} 1.1744 + 1.1745 +//------------------------------print_statistics------------------------------- 1.1746 +#ifndef PRODUCT 1.1747 +void PhasePeephole::print_statistics() { 1.1748 + tty->print_cr("Peephole: peephole rules applied: %d", _total_peepholes); 1.1749 +} 1.1750 +#endif 1.1751 + 1.1752 + 1.1753 +//============================================================================= 1.1754 +//------------------------------set_req_X-------------------------------------- 1.1755 +void Node::set_req_X( uint i, Node *n, PhaseIterGVN *igvn ) { 1.1756 + assert( is_not_dead(n), "can not use dead node"); 1.1757 + assert( igvn->hash_find(this) != this, "Need to remove from hash before changing edges" ); 1.1758 + Node *old = in(i); 1.1759 + set_req(i, n); 1.1760 + 1.1761 + // old goes dead? 1.1762 + if( old ) { 1.1763 + switch (old->outcnt()) { 1.1764 + case 0: 1.1765 + // Put into the worklist to kill later. We do not kill it now because the 1.1766 + // recursive kill will delete the current node (this) if dead-loop exists 1.1767 + if (!old->is_top()) 1.1768 + igvn->_worklist.push( old ); 1.1769 + break; 1.1770 + case 1: 1.1771 + if( old->is_Store() || old->has_special_unique_user() ) 1.1772 + igvn->add_users_to_worklist( old ); 1.1773 + break; 1.1774 + case 2: 1.1775 + if( old->is_Store() ) 1.1776 + igvn->add_users_to_worklist( old ); 1.1777 + if( old->Opcode() == Op_Region ) 1.1778 + igvn->_worklist.push(old); 1.1779 + break; 1.1780 + case 3: 1.1781 + if( old->Opcode() == Op_Region ) { 1.1782 + igvn->_worklist.push(old); 1.1783 + igvn->add_users_to_worklist( old ); 1.1784 + } 1.1785 + break; 1.1786 + default: 1.1787 + break; 1.1788 + } 1.1789 + } 1.1790 + 1.1791 +} 1.1792 + 1.1793 +//-------------------------------replace_by----------------------------------- 1.1794 +// Using def-use info, replace one node for another. Follow the def-use info 1.1795 +// to all users of the OLD node. Then make all uses point to the NEW node. 1.1796 +void Node::replace_by(Node *new_node) { 1.1797 + assert(!is_top(), "top node has no DU info"); 1.1798 + for (DUIterator_Last imin, i = last_outs(imin); i >= imin; ) { 1.1799 + Node* use = last_out(i); 1.1800 + uint uses_found = 0; 1.1801 + for (uint j = 0; j < use->len(); j++) { 1.1802 + if (use->in(j) == this) { 1.1803 + if (j < use->req()) 1.1804 + use->set_req(j, new_node); 1.1805 + else use->set_prec(j, new_node); 1.1806 + uses_found++; 1.1807 + } 1.1808 + } 1.1809 + i -= uses_found; // we deleted 1 or more copies of this edge 1.1810 + } 1.1811 +} 1.1812 + 1.1813 +//============================================================================= 1.1814 +//----------------------------------------------------------------------------- 1.1815 +void Type_Array::grow( uint i ) { 1.1816 + if( !_max ) { 1.1817 + _max = 1; 1.1818 + _types = (const Type**)_a->Amalloc( _max * sizeof(Type*) ); 1.1819 + _types[0] = NULL; 1.1820 + } 1.1821 + uint old = _max; 1.1822 + while( i >= _max ) _max <<= 1; // Double to fit 1.1823 + _types = (const Type**)_a->Arealloc( _types, old*sizeof(Type*),_max*sizeof(Type*)); 1.1824 + memset( &_types[old], 0, (_max-old)*sizeof(Type*) ); 1.1825 +} 1.1826 + 1.1827 +//------------------------------dump------------------------------------------- 1.1828 +#ifndef PRODUCT 1.1829 +void Type_Array::dump() const { 1.1830 + uint max = Size(); 1.1831 + for( uint i = 0; i < max; i++ ) { 1.1832 + if( _types[i] != NULL ) { 1.1833 + tty->print(" %d\t== ", i); _types[i]->dump(); tty->cr(); 1.1834 + } 1.1835 + } 1.1836 +} 1.1837 +#endif