1 /* 2 * Copyright (c) 1997, 2017, 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 */ 24 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" 38 #if INCLUDE_ALL_GCS 39 #include "gc_implementation/shenandoah/c2/shenandoahSupport.hpp" 40 #endif 41 42 //============================================================================= 43 #define NODE_HASH_MINIMUM_SIZE 255 44 //------------------------------NodeHash--------------------------------------- 45 NodeHash::NodeHash(uint est_max_size) : 46 _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ), 47 _a(Thread::current()->resource_area()), 48 _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ), // (Node**)_a->Amalloc(_max * sizeof(Node*)) ), 49 _inserts(0), _insert_limit( insert_limit() ), 50 _look_probes(0), _lookup_hits(0), _lookup_misses(0), 51 _total_insert_probes(0), _total_inserts(0), 52 _insert_probes(0), _grows(0) { 53 // _sentinel must be in the current node space 54 _sentinel = new (Compile::current()) ProjNode(NULL, TypeFunc::Control); 55 memset(_table,0,sizeof(Node*)*_max); 56 } 57 58 //------------------------------NodeHash--------------------------------------- 59 NodeHash::NodeHash(Arena *arena, uint est_max_size) : 60 _max( round_up(est_max_size < NODE_HASH_MINIMUM_SIZE ? NODE_HASH_MINIMUM_SIZE : est_max_size) ), 61 _a(arena), 62 _table( NEW_ARENA_ARRAY( _a , Node* , _max ) ), 63 _inserts(0), _insert_limit( insert_limit() ), 64 _look_probes(0), _lookup_hits(0), _lookup_misses(0), 65 _delete_probes(0), _delete_hits(0), _delete_misses(0), 66 _total_insert_probes(0), _total_inserts(0), 67 _insert_probes(0), _grows(0) { 68 // _sentinel must be in the current node space 69 _sentinel = new (Compile::current()) ProjNode(NULL, TypeFunc::Control); 70 memset(_table,0,sizeof(Node*)*_max); 71 } 72 73 //------------------------------NodeHash--------------------------------------- 74 NodeHash::NodeHash(NodeHash *nh) { 75 debug_only(_table = (Node**)badAddress); // interact correctly w/ operator= 76 // just copy in all the fields 77 *this = *nh; 78 // nh->_sentinel must be in the current node space 79 } 80 81 void NodeHash::replace_with(NodeHash *nh) { 82 debug_only(_table = (Node**)badAddress); // interact correctly w/ operator= 83 // just copy in all the fields 84 *this = *nh; 85 // nh->_sentinel must be in the current node space 86 } 87 88 //------------------------------hash_find-------------------------------------- 89 // Find in hash table 90 Node *NodeHash::hash_find( const Node *n ) { 91 // ((Node*)n)->set_hash( n->hash() ); 92 uint hash = n->hash(); 93 if (hash == Node::NO_HASH) { 94 debug_only( _lookup_misses++ ); 95 return NULL; 96 } 97 uint key = hash & (_max-1); 98 uint stride = key | 0x01; 99 debug_only( _look_probes++ ); 100 Node *k = _table[key]; // Get hashed value 101 if( !k ) { // ?Miss? 102 debug_only( _lookup_misses++ ); 103 return NULL; // Miss! 104 } 105 106 int op = n->Opcode(); 107 uint req = n->req(); 108 while( 1 ) { // While probing hash table 109 if( k->req() == req && // Same count of inputs 110 k->Opcode() == op ) { // Same Opcode 111 for( uint i=0; i<req; i++ ) 112 if( n->in(i)!=k->in(i)) // Different inputs? 113 goto collision; // "goto" is a speed hack... 114 if( n->cmp(*k) ) { // Check for any special bits 115 debug_only( _lookup_hits++ ); 116 return k; // Hit! 117 } 118 } 119 collision: 120 debug_only( _look_probes++ ); 121 key = (key + stride/*7*/) & (_max-1); // Stride through table with relative prime 122 k = _table[key]; // Get hashed value 123 if( !k ) { // ?Miss? 124 debug_only( _lookup_misses++ ); 125 return NULL; // Miss! 126 } 127 } 128 ShouldNotReachHere(); 129 return NULL; 130 } 131 132 //------------------------------hash_find_insert------------------------------- 133 // Find in hash table, insert if not already present 134 // Used to preserve unique entries in hash table 135 Node *NodeHash::hash_find_insert( Node *n ) { 136 // n->set_hash( ); 137 uint hash = n->hash(); 138 if (hash == Node::NO_HASH) { 139 debug_only( _lookup_misses++ ); 140 return NULL; 141 } 142 uint key = hash & (_max-1); 143 uint stride = key | 0x01; // stride must be relatively prime to table siz 144 uint first_sentinel = 0; // replace a sentinel if seen. 145 debug_only( _look_probes++ ); 146 Node *k = _table[key]; // Get hashed value 147 if( !k ) { // ?Miss? 148 debug_only( _lookup_misses++ ); 149 _table[key] = n; // Insert into table! 150 debug_only(n->enter_hash_lock()); // Lock down the node while in the table. 151 check_grow(); // Grow table if insert hit limit 152 return NULL; // Miss! 153 } 154 else if( k == _sentinel ) { 155 first_sentinel = key; // Can insert here 156 } 157 158 int op = n->Opcode(); 159 uint req = n->req(); 160 while( 1 ) { // While probing hash table 161 if( k->req() == req && // Same count of inputs 162 k->Opcode() == op ) { // Same Opcode 163 for( uint i=0; i<req; i++ ) 164 if( n->in(i)!=k->in(i)) // Different inputs? 165 goto collision; // "goto" is a speed hack... 166 if( n->cmp(*k) ) { // Check for any special bits 167 debug_only( _lookup_hits++ ); 168 return k; // Hit! 169 } 170 } 171 collision: 172 debug_only( _look_probes++ ); 173 key = (key + stride) & (_max-1); // Stride through table w/ relative prime 174 k = _table[key]; // Get hashed value 175 if( !k ) { // ?Miss? 176 debug_only( _lookup_misses++ ); 177 key = (first_sentinel == 0) ? key : first_sentinel; // ?saw sentinel? 178 _table[key] = n; // Insert into table! 179 debug_only(n->enter_hash_lock()); // Lock down the node while in the table. 180 check_grow(); // Grow table if insert hit limit 181 return NULL; // Miss! 182 } 183 else if( first_sentinel == 0 && k == _sentinel ) { 184 first_sentinel = key; // Can insert here 185 } 186 187 } 188 ShouldNotReachHere(); 189 return NULL; 190 } 191 192 //------------------------------hash_insert------------------------------------ 193 // Insert into hash table 194 void NodeHash::hash_insert( Node *n ) { 195 // // "conflict" comments -- print nodes that conflict 196 // bool conflict = false; 197 // n->set_hash(); 198 uint hash = n->hash(); 199 if (hash == Node::NO_HASH) { 200 return; 201 } 202 check_grow(); 203 uint key = hash & (_max-1); 204 uint stride = key | 0x01; 205 206 while( 1 ) { // While probing hash table 207 debug_only( _insert_probes++ ); 208 Node *k = _table[key]; // Get hashed value 209 if( !k || (k == _sentinel) ) break; // Found a slot 210 assert( k != n, "already inserted" ); 211 // if( PrintCompilation && PrintOptoStatistics && Verbose ) { tty->print(" conflict: "); k->dump(); conflict = true; } 212 key = (key + stride) & (_max-1); // Stride through table w/ relative prime 213 } 214 _table[key] = n; // Insert into table! 215 debug_only(n->enter_hash_lock()); // Lock down the node while in the table. 216 // if( conflict ) { n->dump(); } 217 } 218 219 //------------------------------hash_delete------------------------------------ 220 // Replace in hash table with sentinel 221 bool NodeHash::hash_delete( const Node *n ) { 222 Node *k; 223 uint hash = n->hash(); 224 if (hash == Node::NO_HASH) { 225 debug_only( _delete_misses++ ); 226 return false; 227 } 228 uint key = hash & (_max-1); 229 uint stride = key | 0x01; 230 debug_only( uint counter = 0; ); 231 for( ; /* (k != NULL) && (k != _sentinel) */; ) { 232 debug_only( counter++ ); 233 debug_only( _delete_probes++ ); 234 k = _table[key]; // Get hashed value 235 if( !k ) { // Miss? 236 debug_only( _delete_misses++ ); 237 #ifdef ASSERT 238 if( VerifyOpto ) { 239 for( uint i=0; i < _max; i++ ) 240 assert( _table[i] != n, "changed edges with rehashing" ); 241 } 242 #endif 243 return false; // Miss! Not in chain 244 } 245 else if( n == k ) { 246 debug_only( _delete_hits++ ); 247 _table[key] = _sentinel; // Hit! Label as deleted entry 248 debug_only(((Node*)n)->exit_hash_lock()); // Unlock the node upon removal from table. 249 return true; 250 } 251 else { 252 // collision: move through table with prime offset 253 key = (key + stride/*7*/) & (_max-1); 254 assert( counter <= _insert_limit, "Cycle in hash-table"); 255 } 256 } 257 ShouldNotReachHere(); 258 return false; 259 } 260 261 //------------------------------round_up--------------------------------------- 262 // Round up to nearest power of 2 263 uint NodeHash::round_up( uint x ) { 264 x += (x>>2); // Add 25% slop 265 if( x <16 ) return 16; // Small stuff 266 uint i=16; 267 while( i < x ) i <<= 1; // Double to fit 268 return i; // Return hash table size 269 } 270 271 //------------------------------grow------------------------------------------- 272 // Grow _table to next power of 2 and insert old entries 273 void NodeHash::grow() { 274 // Record old state 275 uint old_max = _max; 276 Node **old_table = _table; 277 // Construct new table with twice the space 278 _grows++; 279 _total_inserts += _inserts; 280 _total_insert_probes += _insert_probes; 281 _inserts = 0; 282 _insert_probes = 0; 283 _max = _max << 1; 284 _table = NEW_ARENA_ARRAY( _a , Node* , _max ); // (Node**)_a->Amalloc( _max * sizeof(Node*) ); 285 memset(_table,0,sizeof(Node*)*_max); 286 _insert_limit = insert_limit(); 287 // Insert old entries into the new table 288 for( uint i = 0; i < old_max; i++ ) { 289 Node *m = *old_table++; 290 if( !m || m == _sentinel ) continue; 291 debug_only(m->exit_hash_lock()); // Unlock the node upon removal from old table. 292 hash_insert(m); 293 } 294 } 295 296 //------------------------------clear------------------------------------------ 297 // Clear all entries in _table to NULL but keep storage 298 void NodeHash::clear() { 299 #ifdef ASSERT 300 // Unlock all nodes upon removal from table. 301 for (uint i = 0; i < _max; i++) { 302 Node* n = _table[i]; 303 if (!n || n == _sentinel) continue; 304 n->exit_hash_lock(); 305 } 306 #endif 307 308 memset( _table, 0, _max * sizeof(Node*) ); 309 } 310 311 //-----------------------remove_useless_nodes---------------------------------- 312 // Remove useless nodes from value table, 313 // implementation does not depend on hash function 314 void NodeHash::remove_useless_nodes(VectorSet &useful) { 315 316 // Dead nodes in the hash table inherited from GVN should not replace 317 // existing nodes, remove dead nodes. 318 uint max = size(); 319 Node *sentinel_node = sentinel(); 320 for( uint i = 0; i < max; ++i ) { 321 Node *n = at(i); 322 if(n != NULL && n != sentinel_node && !useful.test(n->_idx)) { 323 debug_only(n->exit_hash_lock()); // Unlock the node when removed 324 _table[i] = sentinel_node; // Replace with placeholder 325 } 326 } 327 } 328 329 330 void NodeHash::check_no_speculative_types() { 331 #ifdef ASSERT 332 uint max = size(); 333 Node *sentinel_node = sentinel(); 334 for (uint i = 0; i < max; ++i) { 335 Node *n = at(i); 336 if(n != NULL && n != sentinel_node && n->is_Type()) { 337 TypeNode* tn = n->as_Type(); 338 const Type* t = tn->type(); 339 const Type* t_no_spec = t->remove_speculative(); 340 assert(t == t_no_spec, "dead node in hash table or missed node during speculative cleanup"); 341 } 342 } 343 #endif 344 } 345 346 #ifndef PRODUCT 347 //------------------------------dump------------------------------------------- 348 // Dump statistics for the hash table 349 void NodeHash::dump() { 350 _total_inserts += _inserts; 351 _total_insert_probes += _insert_probes; 352 if (PrintCompilation && PrintOptoStatistics && Verbose && (_inserts > 0)) { 353 if (WizardMode) { 354 for (uint i=0; i<_max; i++) { 355 if (_table[i]) 356 tty->print("%d/%d/%d ",i,_table[i]->hash()&(_max-1),_table[i]->_idx); 357 } 358 } 359 tty->print("\nGVN Hash stats: %d grows to %d max_size\n", _grows, _max); 360 tty->print(" %d/%d (%8.1f%% full)\n", _inserts, _max, (double)_inserts/_max*100.0); 361 tty->print(" %dp/(%dh+%dm) (%8.2f probes/lookup)\n", _look_probes, _lookup_hits, _lookup_misses, (double)_look_probes/(_lookup_hits+_lookup_misses)); 362 tty->print(" %dp/%di (%8.2f probes/insert)\n", _total_insert_probes, _total_inserts, (double)_total_insert_probes/_total_inserts); 363 // sentinels increase lookup cost, but not insert cost 364 assert((_lookup_misses+_lookup_hits)*4+100 >= _look_probes, "bad hash function"); 365 assert( _inserts+(_inserts>>3) < _max, "table too full" ); 366 assert( _inserts*3+100 >= _insert_probes, "bad hash function" ); 367 } 368 } 369 370 Node *NodeHash::find_index(uint idx) { // For debugging 371 // Find an entry by its index value 372 for( uint i = 0; i < _max; i++ ) { 373 Node *m = _table[i]; 374 if( !m || m == _sentinel ) continue; 375 if( m->_idx == (uint)idx ) return m; 376 } 377 return NULL; 378 } 379 #endif 380 381 #ifdef ASSERT 382 NodeHash::~NodeHash() { 383 // Unlock all nodes upon destruction of table. 384 if (_table != (Node**)badAddress) clear(); 385 } 386 387 void NodeHash::operator=(const NodeHash& nh) { 388 // Unlock all nodes upon replacement of table. 389 if (&nh == this) return; 390 if (_table != (Node**)badAddress) clear(); 391 memcpy(this, &nh, sizeof(*this)); 392 // Do not increment hash_lock counts again. 393 // Instead, be sure we never again use the source table. 394 ((NodeHash*)&nh)->_table = (Node**)badAddress; 395 } 396 397 398 #endif 399 400 401 //============================================================================= 402 //------------------------------PhaseRemoveUseless----------------------------- 403 // 1) Use a breadthfirst walk to collect useful nodes reachable from root. 404 PhaseRemoveUseless::PhaseRemoveUseless(PhaseGVN *gvn, Unique_Node_List *worklist, PhaseNumber phase_num) : Phase(phase_num), 405 _useful(Thread::current()->resource_area()) { 406 407 // Implementation requires 'UseLoopSafepoints == true' and an edge from root 408 // to each SafePointNode at a backward branch. Inserted in add_safepoint(). 409 if( !UseLoopSafepoints || !OptoRemoveUseless ) return; 410 411 // Identify nodes that are reachable from below, useful. 412 C->identify_useful_nodes(_useful); 413 // Update dead node list 414 C->update_dead_node_list(_useful); 415 416 // Remove all useless nodes from PhaseValues' recorded types 417 // Must be done before disconnecting nodes to preserve hash-table-invariant 418 gvn->remove_useless_nodes(_useful.member_set()); 419 420 // Remove all useless nodes from future worklist 421 worklist->remove_useless_nodes(_useful.member_set()); 422 423 // Disconnect 'useless' nodes that are adjacent to useful nodes 424 C->remove_useless_nodes(_useful); 425 } 426 427 //============================================================================= 428 //------------------------------PhaseRenumberLive------------------------------ 429 // First, remove useless nodes (equivalent to identifying live nodes). 430 // Then, renumber live nodes. 431 // 432 // The set of live nodes is returned by PhaseRemoveUseless in the _useful structure. 433 // If the number of live nodes is 'x' (where 'x' == _useful.size()), then the 434 // PhaseRenumberLive updates the node ID of each node (the _idx field) with a unique 435 // value in the range [0, x). 436 // 437 // At the end of the PhaseRenumberLive phase, the compiler's count of unique nodes is 438 // updated to 'x' and the list of dead nodes is reset (as there are no dead nodes). 439 // 440 // The PhaseRenumberLive phase updates two data structures with the new node IDs. 441 // (1) The worklist is used by the PhaseIterGVN phase to identify nodes that must be 442 // processed. A new worklist (with the updated node IDs) is returned in 'new_worklist'. 443 // (2) Type information (the field PhaseGVN::_types) maps type information to each 444 // node ID. The mapping is updated to use the new node IDs as well. Updated type 445 // information is returned in PhaseGVN::_types. 446 // 447 // The PhaseRenumberLive phase does not preserve the order of elements in the worklist. 448 // 449 // Other data structures used by the compiler are not updated. The hash table for value 450 // numbering (the field PhaseGVN::_table) is not updated because computing the hash 451 // values is not based on node IDs. The field PhaseGVN::_nodes is not updated either 452 // because it is empty wherever PhaseRenumberLive is used. 453 PhaseRenumberLive::PhaseRenumberLive(PhaseGVN* gvn, 454 Unique_Node_List* worklist, Unique_Node_List* new_worklist, 455 PhaseNumber phase_num) : 456 PhaseRemoveUseless(gvn, worklist, Remove_Useless_And_Renumber_Live) { 457 458 assert(RenumberLiveNodes, "RenumberLiveNodes must be set to true for node renumbering to take place"); 459 assert(C->live_nodes() == _useful.size(), "the number of live nodes must match the number of useful nodes"); 460 assert(gvn->nodes_size() == 0, "GVN must not contain any nodes at this point"); 461 462 uint old_unique_count = C->unique(); 463 uint live_node_count = C->live_nodes(); 464 uint worklist_size = worklist->size(); 465 466 // Storage for the updated type information. 467 Type_Array new_type_array(C->comp_arena()); 468 469 // Iterate over the set of live nodes. 470 uint current_idx = 0; // The current new node ID. Incremented after every assignment. 471 for (uint i = 0; i < _useful.size(); i++) { 472 Node* n = _useful.at(i); 473 // Sanity check that fails if we ever decide to execute this phase after EA 474 assert(!n->is_Phi() || n->as_Phi()->inst_mem_id() == -1, "should not be linked to data Phi"); 475 const Type* type = gvn->type_or_null(n); 476 new_type_array.map(current_idx, type); 477 478 bool in_worklist = false; 479 if (worklist->member(n)) { 480 in_worklist = true; 481 } 482 483 n->set_idx(current_idx); // Update node ID. 484 485 if (in_worklist) { 486 new_worklist->push(n); 487 } 488 489 current_idx++; 490 } 491 492 assert(worklist_size == new_worklist->size(), "the new worklist must have the same size as the original worklist"); 493 assert(live_node_count == current_idx, "all live nodes must be processed"); 494 495 // Replace the compiler's type information with the updated type information. 496 gvn->replace_types(new_type_array); 497 498 // Update the unique node count of the compilation to the number of currently live nodes. 499 C->set_unique(live_node_count); 500 501 // Set the dead node count to 0 and reset dead node list. 502 C->reset_dead_node_list(); 503 } 504 505 506 //============================================================================= 507 //------------------------------PhaseTransform--------------------------------- 508 PhaseTransform::PhaseTransform( PhaseNumber pnum ) : Phase(pnum), 509 _arena(Thread::current()->resource_area()), 510 _nodes(_arena), 511 _types(_arena) 512 { 513 init_con_caches(); 514 #ifndef PRODUCT 515 clear_progress(); 516 clear_transforms(); 517 set_allow_progress(true); 518 #endif 519 // Force allocation for currently existing nodes 520 _types.map(C->unique(), NULL); 521 } 522 523 //------------------------------PhaseTransform--------------------------------- 524 PhaseTransform::PhaseTransform( Arena *arena, PhaseNumber pnum ) : Phase(pnum), 525 _arena(arena), 526 _nodes(arena), 527 _types(arena) 528 { 529 init_con_caches(); 530 #ifndef PRODUCT 531 clear_progress(); 532 clear_transforms(); 533 set_allow_progress(true); 534 #endif 535 // Force allocation for currently existing nodes 536 _types.map(C->unique(), NULL); 537 } 538 539 //------------------------------PhaseTransform--------------------------------- 540 // Initialize with previously generated type information 541 PhaseTransform::PhaseTransform( PhaseTransform *pt, PhaseNumber pnum ) : Phase(pnum), 542 _arena(pt->_arena), 543 _nodes(pt->_nodes), 544 _types(pt->_types) 545 { 546 init_con_caches(); 547 #ifndef PRODUCT 548 clear_progress(); 549 clear_transforms(); 550 set_allow_progress(true); 551 #endif 552 } 553 554 void PhaseTransform::init_con_caches() { 555 memset(_icons,0,sizeof(_icons)); 556 memset(_lcons,0,sizeof(_lcons)); 557 memset(_zcons,0,sizeof(_zcons)); 558 } 559 560 561 //--------------------------------find_int_type-------------------------------- 562 const TypeInt* PhaseTransform::find_int_type(Node* n) { 563 if (n == NULL) return NULL; 564 // Call type_or_null(n) to determine node's type since we might be in 565 // parse phase and call n->Value() may return wrong type. 566 // (For example, a phi node at the beginning of loop parsing is not ready.) 567 const Type* t = type_or_null(n); 568 if (t == NULL) return NULL; 569 return t->isa_int(); 570 } 571 572 573 //-------------------------------find_long_type-------------------------------- 574 const TypeLong* PhaseTransform::find_long_type(Node* n) { 575 if (n == NULL) return NULL; 576 // (See comment above on type_or_null.) 577 const Type* t = type_or_null(n); 578 if (t == NULL) return NULL; 579 return t->isa_long(); 580 } 581 582 583 #ifndef PRODUCT 584 void PhaseTransform::dump_old2new_map() const { 585 _nodes.dump(); 586 } 587 588 void PhaseTransform::dump_new( uint nidx ) const { 589 for( uint i=0; i<_nodes.Size(); i++ ) 590 if( _nodes[i] && _nodes[i]->_idx == nidx ) { 591 _nodes[i]->dump(); 592 tty->cr(); 593 tty->print_cr("Old index= %d",i); 594 return; 595 } 596 tty->print_cr("Node %d not found in the new indices", nidx); 597 } 598 599 //------------------------------dump_types------------------------------------- 600 void PhaseTransform::dump_types( ) const { 601 _types.dump(); 602 } 603 604 //------------------------------dump_nodes_and_types--------------------------- 605 void PhaseTransform::dump_nodes_and_types(const Node *root, uint depth, bool only_ctrl) { 606 VectorSet visited(Thread::current()->resource_area()); 607 dump_nodes_and_types_recur( root, depth, only_ctrl, visited ); 608 } 609 610 //------------------------------dump_nodes_and_types_recur--------------------- 611 void PhaseTransform::dump_nodes_and_types_recur( const Node *n, uint depth, bool only_ctrl, VectorSet &visited) { 612 if( !n ) return; 613 if( depth == 0 ) return; 614 if( visited.test_set(n->_idx) ) return; 615 for( uint i=0; i<n->len(); i++ ) { 616 if( only_ctrl && !(n->is_Region()) && i != TypeFunc::Control ) continue; 617 dump_nodes_and_types_recur( n->in(i), depth-1, only_ctrl, visited ); 618 } 619 n->dump(); 620 if (type_or_null(n) != NULL) { 621 tty->print(" "); type(n)->dump(); tty->cr(); 622 } 623 } 624 625 #endif 626 627 628 //============================================================================= 629 //------------------------------PhaseValues------------------------------------ 630 // Set minimum table size to "255" 631 PhaseValues::PhaseValues( Arena *arena, uint est_max_size ) : PhaseTransform(arena, GVN), _table(arena, est_max_size) { 632 NOT_PRODUCT( clear_new_values(); ) 633 } 634 635 //------------------------------PhaseValues------------------------------------ 636 // Set minimum table size to "255" 637 PhaseValues::PhaseValues( PhaseValues *ptv ) : PhaseTransform( ptv, GVN ), 638 _table(&ptv->_table) { 639 NOT_PRODUCT( clear_new_values(); ) 640 } 641 642 //------------------------------PhaseValues------------------------------------ 643 // Used by +VerifyOpto. Clear out hash table but copy _types array. 644 PhaseValues::PhaseValues( PhaseValues *ptv, const char *dummy ) : PhaseTransform( ptv, GVN ), 645 _table(ptv->arena(),ptv->_table.size()) { 646 NOT_PRODUCT( clear_new_values(); ) 647 } 648 649 //------------------------------~PhaseValues----------------------------------- 650 #ifndef PRODUCT 651 PhaseValues::~PhaseValues() { 652 _table.dump(); 653 654 // Statistics for value progress and efficiency 655 if( PrintCompilation && Verbose && WizardMode ) { 656 tty->print("\n%sValues: %d nodes ---> %d/%d (%d)", 657 is_IterGVN() ? "Iter" : " ", C->unique(), made_progress(), made_transforms(), made_new_values()); 658 if( made_transforms() != 0 ) { 659 tty->print_cr(" ratio %f", made_progress()/(float)made_transforms() ); 660 } else { 661 tty->cr(); 662 } 663 } 664 } 665 #endif 666 667 //------------------------------makecon---------------------------------------- 668 ConNode* PhaseTransform::makecon(const Type *t) { 669 assert(t->singleton(), "must be a constant"); 670 assert(!t->empty() || t == Type::TOP, "must not be vacuous range"); 671 switch (t->base()) { // fast paths 672 case Type::Half: 673 case Type::Top: return (ConNode*) C->top(); 674 case Type::Int: return intcon( t->is_int()->get_con() ); 675 case Type::Long: return longcon( t->is_long()->get_con() ); 676 } 677 if (t->is_zero_type()) 678 return zerocon(t->basic_type()); 679 return uncached_makecon(t); 680 } 681 682 //--------------------------uncached_makecon----------------------------------- 683 // Make an idealized constant - one of ConINode, ConPNode, etc. 684 ConNode* PhaseValues::uncached_makecon(const Type *t) { 685 assert(t->singleton(), "must be a constant"); 686 ConNode* x = ConNode::make(C, t); 687 ConNode* k = (ConNode*)hash_find_insert(x); // Value numbering 688 if (k == NULL) { 689 set_type(x, t); // Missed, provide type mapping 690 GrowableArray<Node_Notes*>* nna = C->node_note_array(); 691 if (nna != NULL) { 692 Node_Notes* loc = C->locate_node_notes(nna, x->_idx, true); 693 loc->clear(); // do not put debug info on constants 694 } 695 } else { 696 x->destruct(); // Hit, destroy duplicate constant 697 x = k; // use existing constant 698 } 699 return x; 700 } 701 702 //------------------------------intcon----------------------------------------- 703 // Fast integer constant. Same as "transform(new ConINode(TypeInt::make(i)))" 704 ConINode* PhaseTransform::intcon(int i) { 705 // Small integer? Check cache! Check that cached node is not dead 706 if (i >= _icon_min && i <= _icon_max) { 707 ConINode* icon = _icons[i-_icon_min]; 708 if (icon != NULL && icon->in(TypeFunc::Control) != NULL) 709 return icon; 710 } 711 ConINode* icon = (ConINode*) uncached_makecon(TypeInt::make(i)); 712 assert(icon->is_Con(), ""); 713 if (i >= _icon_min && i <= _icon_max) 714 _icons[i-_icon_min] = icon; // Cache small integers 715 return icon; 716 } 717 718 //------------------------------longcon---------------------------------------- 719 // Fast long constant. 720 ConLNode* PhaseTransform::longcon(jlong l) { 721 // Small integer? Check cache! Check that cached node is not dead 722 if (l >= _lcon_min && l <= _lcon_max) { 723 ConLNode* lcon = _lcons[l-_lcon_min]; 724 if (lcon != NULL && lcon->in(TypeFunc::Control) != NULL) 725 return lcon; 726 } 727 ConLNode* lcon = (ConLNode*) uncached_makecon(TypeLong::make(l)); 728 assert(lcon->is_Con(), ""); 729 if (l >= _lcon_min && l <= _lcon_max) 730 _lcons[l-_lcon_min] = lcon; // Cache small integers 731 return lcon; 732 } 733 734 //------------------------------zerocon----------------------------------------- 735 // Fast zero or null constant. Same as "transform(ConNode::make(Type::get_zero_type(bt)))" 736 ConNode* PhaseTransform::zerocon(BasicType bt) { 737 assert((uint)bt <= _zcon_max, "domain check"); 738 ConNode* zcon = _zcons[bt]; 739 if (zcon != NULL && zcon->in(TypeFunc::Control) != NULL) 740 return zcon; 741 zcon = (ConNode*) uncached_makecon(Type::get_zero_type(bt)); 742 _zcons[bt] = zcon; 743 return zcon; 744 } 745 746 747 748 //============================================================================= 749 //------------------------------transform-------------------------------------- 750 // Return a node which computes the same function as this node, but in a 751 // faster or cheaper fashion. 752 Node *PhaseGVN::transform( Node *n ) { 753 return transform_no_reclaim(n); 754 } 755 756 //------------------------------transform-------------------------------------- 757 // Return a node which computes the same function as this node, but 758 // in a faster or cheaper fashion. 759 Node *PhaseGVN::transform_no_reclaim( Node *n ) { 760 NOT_PRODUCT( set_transforms(); ) 761 762 // Apply the Ideal call in a loop until it no longer applies 763 Node *k = n; 764 NOT_PRODUCT( uint loop_count = 0; ) 765 while( 1 ) { 766 Node *i = k->Ideal(this, /*can_reshape=*/false); 767 if( !i ) break; 768 assert( i->_idx >= k->_idx, "Idealize should return new nodes, use Identity to return old nodes" ); 769 k = i; 770 assert(loop_count++ < K, "infinite loop in PhaseGVN::transform"); 771 } 772 NOT_PRODUCT( if( loop_count != 0 ) { set_progress(); } ) 773 774 775 // If brand new node, make space in type array. 776 ensure_type_or_null(k); 777 778 // Since I just called 'Value' to compute the set of run-time values 779 // for this Node, and 'Value' is non-local (and therefore expensive) I'll 780 // cache Value. Later requests for the local phase->type of this Node can 781 // use the cached Value instead of suffering with 'bottom_type'. 782 const Type *t = k->Value(this); // Get runtime Value set 783 assert(t != NULL, "value sanity"); 784 if (type_or_null(k) != t) { 785 #ifndef PRODUCT 786 // Do not count initial visit to node as a transformation 787 if (type_or_null(k) == NULL) { 788 inc_new_values(); 789 set_progress(); 790 } 791 #endif 792 set_type(k, t); 793 // If k is a TypeNode, capture any more-precise type permanently into Node 794 k->raise_bottom_type(t); 795 } 796 797 if( t->singleton() && !k->is_Con() ) { 798 NOT_PRODUCT( set_progress(); ) 799 return makecon(t); // Turn into a constant 800 } 801 802 // Now check for Identities 803 Node *i = k->Identity(this); // Look for a nearby replacement 804 if( i != k ) { // Found? Return replacement! 805 NOT_PRODUCT( set_progress(); ) 806 return i; 807 } 808 809 // Global Value Numbering 810 i = hash_find_insert(k); // Insert if new 811 if( i && (i != k) ) { 812 // Return the pre-existing node 813 NOT_PRODUCT( set_progress(); ) 814 return i; 815 } 816 817 // Return Idealized original 818 return k; 819 } 820 821 #ifdef ASSERT 822 //------------------------------dead_loop_check-------------------------------- 823 // Check for a simple dead loop when a data node references itself directly 824 // or through an other data node excluding cons and phis. 825 void PhaseGVN::dead_loop_check( Node *n ) { 826 // Phi may reference itself in a loop 827 if (n != NULL && !n->is_dead_loop_safe() && !n->is_CFG()) { 828 // Do 2 levels check and only data inputs. 829 bool no_dead_loop = true; 830 uint cnt = n->req(); 831 for (uint i = 1; i < cnt && no_dead_loop; i++) { 832 Node *in = n->in(i); 833 if (in == n) { 834 no_dead_loop = false; 835 } else if (in != NULL && !in->is_dead_loop_safe()) { 836 uint icnt = in->req(); 837 for (uint j = 1; j < icnt && no_dead_loop; j++) { 838 if (in->in(j) == n || in->in(j) == in) 839 no_dead_loop = false; 840 } 841 } 842 } 843 if (!no_dead_loop) n->dump(3); 844 assert(no_dead_loop, "dead loop detected"); 845 } 846 } 847 #endif 848 849 //============================================================================= 850 //------------------------------PhaseIterGVN----------------------------------- 851 // Initialize hash table to fresh and clean for +VerifyOpto 852 PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn, const char *dummy ) : PhaseGVN(igvn,dummy), _worklist( ), 853 _stack(C->live_nodes() >> 1), 854 _delay_transform(false) { 855 } 856 857 //------------------------------PhaseIterGVN----------------------------------- 858 // Initialize with previous PhaseIterGVN info; used by PhaseCCP 859 PhaseIterGVN::PhaseIterGVN( PhaseIterGVN *igvn ) : PhaseGVN(igvn), 860 _worklist( igvn->_worklist ), 861 _stack( igvn->_stack ), 862 _delay_transform(igvn->_delay_transform) 863 { 864 } 865 866 //------------------------------PhaseIterGVN----------------------------------- 867 // Initialize with previous PhaseGVN info from Parser 868 PhaseIterGVN::PhaseIterGVN( PhaseGVN *gvn ) : PhaseGVN(gvn), 869 _worklist(*C->for_igvn()), 870 // TODO: Before incremental inlining it was allocated only once and it was fine. Now that 871 // the constructor is used in incremental inlining, this consumes too much memory: 872 // _stack(C->live_nodes() >> 1), 873 // So, as a band-aid, we replace this by: 874 _stack(C->comp_arena(), 32), 875 _delay_transform(false) 876 { 877 uint max; 878 879 // Dead nodes in the hash table inherited from GVN were not treated as 880 // roots during def-use info creation; hence they represent an invisible 881 // use. Clear them out. 882 max = _table.size(); 883 for( uint i = 0; i < max; ++i ) { 884 Node *n = _table.at(i); 885 if(n != NULL && n != _table.sentinel() && n->outcnt() == 0) { 886 if( n->is_top() ) continue; 887 assert( false, "Parse::remove_useless_nodes missed this node"); 888 hash_delete(n); 889 } 890 } 891 892 // Any Phis or Regions on the worklist probably had uses that could not 893 // make more progress because the uses were made while the Phis and Regions 894 // were in half-built states. Put all uses of Phis and Regions on worklist. 895 max = _worklist.size(); 896 for( uint j = 0; j < max; j++ ) { 897 Node *n = _worklist.at(j); 898 uint uop = n->Opcode(); 899 if( uop == Op_Phi || uop == Op_Region || 900 n->is_Type() || 901 n->is_Mem() ) 902 add_users_to_worklist(n); 903 } 904 } 905 906 907 #ifndef PRODUCT 908 void PhaseIterGVN::verify_step(Node* n) { 909 _verify_window[_verify_counter % _verify_window_size] = n; 910 ++_verify_counter; 911 ResourceMark rm; 912 ResourceArea *area = Thread::current()->resource_area(); 913 VectorSet old_space(area), new_space(area); 914 if (C->unique() < 1000 || 915 0 == _verify_counter % (C->unique() < 10000 ? 10 : 100)) { 916 ++_verify_full_passes; 917 Node::verify_recur(C->root(), -1, old_space, new_space); 918 } 919 const int verify_depth = 4; 920 for ( int i = 0; i < _verify_window_size; i++ ) { 921 Node* n = _verify_window[i]; 922 if ( n == NULL ) continue; 923 if( n->in(0) == NodeSentinel ) { // xform_idom 924 _verify_window[i] = n->in(1); 925 --i; continue; 926 } 927 // Typical fanout is 1-2, so this call visits about 6 nodes. 928 Node::verify_recur(n, verify_depth, old_space, new_space); 929 } 930 } 931 #endif 932 933 934 //------------------------------init_worklist---------------------------------- 935 // Initialize worklist for each node. 936 void PhaseIterGVN::init_worklist( Node *n ) { 937 if( _worklist.member(n) ) return; 938 _worklist.push(n); 939 uint cnt = n->req(); 940 for( uint i =0 ; i < cnt; i++ ) { 941 Node *m = n->in(i); 942 if( m ) init_worklist(m); 943 } 944 } 945 946 //------------------------------optimize--------------------------------------- 947 void PhaseIterGVN::optimize() { 948 debug_only(uint num_processed = 0;); 949 #ifndef PRODUCT 950 { 951 _verify_counter = 0; 952 _verify_full_passes = 0; 953 for ( int i = 0; i < _verify_window_size; i++ ) { 954 _verify_window[i] = NULL; 955 } 956 } 957 #endif 958 959 #ifdef ASSERT 960 Node* prev = NULL; 961 uint rep_cnt = 0; 962 #endif 963 uint loop_count = 0; 964 965 // Pull from worklist; transform node; 966 // If node has changed: update edge info and put uses on worklist. 967 while( _worklist.size() ) { 968 if (C->check_node_count(NodeLimitFudgeFactor * 2, 969 "out of nodes optimizing method")) { 970 return; 971 } 972 Node *n = _worklist.pop(); 973 if (++loop_count >= K * C->live_nodes()) { 974 debug_only(n->dump(4);) 975 assert(false, "infinite loop in PhaseIterGVN::optimize"); 976 C->record_method_not_compilable("infinite loop in PhaseIterGVN::optimize"); 977 return; 978 } 979 #ifdef ASSERT 980 if (n == prev) { 981 if (++rep_cnt > 3) { 982 n->dump(4); 983 assert(false, "loop in Ideal transformation"); 984 } 985 } else { 986 rep_cnt = 0; 987 } 988 prev = n; 989 #endif 990 if (TraceIterativeGVN && Verbose) { 991 tty->print(" Pop "); 992 NOT_PRODUCT( n->dump(); ) 993 debug_only(if( (num_processed++ % 100) == 0 ) _worklist.print_set();) 994 } 995 996 if (n->outcnt() != 0) { 997 998 #ifndef PRODUCT 999 uint wlsize = _worklist.size(); 1000 const Type* oldtype = type_or_null(n); 1001 #endif //PRODUCT 1002 1003 Node *nn = transform_old(n); 1004 1005 #ifndef PRODUCT 1006 if (TraceIterativeGVN) { 1007 const Type* newtype = type_or_null(n); 1008 if (nn != n) { 1009 // print old node 1010 tty->print("< "); 1011 if (oldtype != newtype && oldtype != NULL) { 1012 oldtype->dump(); 1013 } 1014 do { tty->print("\t"); } while (tty->position() < 16); 1015 tty->print("<"); 1016 n->dump(); 1017 } 1018 if (oldtype != newtype || nn != n) { 1019 // print new node and/or new type 1020 if (oldtype == NULL) { 1021 tty->print("* "); 1022 } else if (nn != n) { 1023 tty->print("> "); 1024 } else { 1025 tty->print("= "); 1026 } 1027 if (newtype == NULL) { 1028 tty->print("null"); 1029 } else { 1030 newtype->dump(); 1031 } 1032 do { tty->print("\t"); } while (tty->position() < 16); 1033 nn->dump(); 1034 } 1035 if (Verbose && wlsize < _worklist.size()) { 1036 tty->print(" Push {"); 1037 while (wlsize != _worklist.size()) { 1038 Node* pushed = _worklist.at(wlsize++); 1039 tty->print(" %d", pushed->_idx); 1040 } 1041 tty->print_cr(" }"); 1042 } 1043 } 1044 if( VerifyIterativeGVN && nn != n ) { 1045 verify_step((Node*) NULL); // ignore n, it might be subsumed 1046 } 1047 #endif 1048 } else if (!n->is_top()) { 1049 remove_dead_node(n); 1050 } 1051 } 1052 1053 #ifndef PRODUCT 1054 C->verify_graph_edges(); 1055 if( VerifyOpto && allow_progress() ) { 1056 // Must turn off allow_progress to enable assert and break recursion 1057 C->root()->verify(); 1058 { // Check if any progress was missed using IterGVN 1059 // Def-Use info enables transformations not attempted in wash-pass 1060 // e.g. Region/Phi cleanup, ... 1061 // Null-check elision -- may not have reached fixpoint 1062 // do not propagate to dominated nodes 1063 ResourceMark rm; 1064 PhaseIterGVN igvn2(this,"Verify"); // Fresh and clean! 1065 // Fill worklist completely 1066 igvn2.init_worklist(C->root()); 1067 1068 igvn2.set_allow_progress(false); 1069 igvn2.optimize(); 1070 igvn2.set_allow_progress(true); 1071 } 1072 } 1073 if ( VerifyIterativeGVN && PrintOpto ) { 1074 if ( _verify_counter == _verify_full_passes ) 1075 tty->print_cr("VerifyIterativeGVN: %d transforms and verify passes", 1076 (int) _verify_full_passes); 1077 else 1078 tty->print_cr("VerifyIterativeGVN: %d transforms, %d full verify passes", 1079 (int) _verify_counter, (int) _verify_full_passes); 1080 } 1081 #endif 1082 } 1083 1084 1085 //------------------register_new_node_with_optimizer--------------------------- 1086 // Register a new node with the optimizer. Update the types array, the def-use 1087 // info. Put on worklist. 1088 Node* PhaseIterGVN::register_new_node_with_optimizer(Node* n, Node* orig) { 1089 set_type_bottom(n); 1090 _worklist.push(n); 1091 if (orig != NULL) C->copy_node_notes_to(n, orig); 1092 return n; 1093 } 1094 1095 //------------------------------transform-------------------------------------- 1096 // Non-recursive: idealize Node 'n' with respect to its inputs and its value 1097 Node *PhaseIterGVN::transform( Node *n ) { 1098 if (_delay_transform) { 1099 // Register the node but don't optimize for now 1100 register_new_node_with_optimizer(n); 1101 return n; 1102 } 1103 1104 // If brand new node, make space in type array, and give it a type. 1105 ensure_type_or_null(n); 1106 if (type_or_null(n) == NULL) { 1107 set_type_bottom(n); 1108 } 1109 1110 return transform_old(n); 1111 } 1112 1113 //------------------------------transform_old---------------------------------- 1114 Node *PhaseIterGVN::transform_old( Node *n ) { 1115 #ifndef PRODUCT 1116 debug_only(uint loop_count = 0;); 1117 set_transforms(); 1118 #endif 1119 // Remove 'n' from hash table in case it gets modified 1120 _table.hash_delete(n); 1121 if( VerifyIterativeGVN ) { 1122 assert( !_table.find_index(n->_idx), "found duplicate entry in table"); 1123 } 1124 1125 // Apply the Ideal call in a loop until it no longer applies 1126 Node *k = n; 1127 DEBUG_ONLY(dead_loop_check(k);) 1128 DEBUG_ONLY(bool is_new = (k->outcnt() == 0);) 1129 Node *i = k->Ideal(this, /*can_reshape=*/true); 1130 assert(i != k || is_new || i->outcnt() > 0, "don't return dead nodes"); 1131 #ifndef PRODUCT 1132 if( VerifyIterativeGVN ) 1133 verify_step(k); 1134 if( i && VerifyOpto ) { 1135 if( !allow_progress() ) { 1136 if (i->is_Add() && i->outcnt() == 1) { 1137 // Switched input to left side because this is the only use 1138 } else if( i->is_If() && (i->in(0) == NULL) ) { 1139 // This IF is dead because it is dominated by an equivalent IF When 1140 // dominating if changed, info is not propagated sparsely to 'this' 1141 // Propagating this info further will spuriously identify other 1142 // progress. 1143 return i; 1144 } else 1145 set_progress(); 1146 } else 1147 set_progress(); 1148 } 1149 #endif 1150 1151 while( i ) { 1152 #ifndef PRODUCT 1153 debug_only( if( loop_count >= K ) i->dump(4); ) 1154 assert(loop_count < K, "infinite loop in PhaseIterGVN::transform"); 1155 debug_only( loop_count++; ) 1156 #endif 1157 assert((i->_idx >= k->_idx) || i->is_top(), "Idealize should return new nodes, use Identity to return old nodes"); 1158 // Made a change; put users of original Node on worklist 1159 add_users_to_worklist( k ); 1160 // Replacing root of transform tree? 1161 if( k != i ) { 1162 // Make users of old Node now use new. 1163 subsume_node( k, i ); 1164 k = i; 1165 } 1166 DEBUG_ONLY(dead_loop_check(k);) 1167 // Try idealizing again 1168 DEBUG_ONLY(is_new = (k->outcnt() == 0);) 1169 i = k->Ideal(this, /*can_reshape=*/true); 1170 assert(i != k || is_new || i->outcnt() > 0, "don't return dead nodes"); 1171 #ifndef PRODUCT 1172 if( VerifyIterativeGVN ) 1173 verify_step(k); 1174 if( i && VerifyOpto ) set_progress(); 1175 #endif 1176 } 1177 1178 // If brand new node, make space in type array. 1179 ensure_type_or_null(k); 1180 1181 // See what kind of values 'k' takes on at runtime 1182 const Type *t = k->Value(this); 1183 assert(t != NULL, "value sanity"); 1184 1185 // Since I just called 'Value' to compute the set of run-time values 1186 // for this Node, and 'Value' is non-local (and therefore expensive) I'll 1187 // cache Value. Later requests for the local phase->type of this Node can 1188 // use the cached Value instead of suffering with 'bottom_type'. 1189 if (t != type_or_null(k)) { 1190 NOT_PRODUCT( set_progress(); ) 1191 NOT_PRODUCT( inc_new_values();) 1192 set_type(k, t); 1193 // If k is a TypeNode, capture any more-precise type permanently into Node 1194 k->raise_bottom_type(t); 1195 // Move users of node to worklist 1196 add_users_to_worklist( k ); 1197 } 1198 1199 // If 'k' computes a constant, replace it with a constant 1200 if( t->singleton() && !k->is_Con() ) { 1201 NOT_PRODUCT( set_progress(); ) 1202 Node *con = makecon(t); // Make a constant 1203 add_users_to_worklist( k ); 1204 subsume_node( k, con ); // Everybody using k now uses con 1205 return con; 1206 } 1207 1208 // Now check for Identities 1209 i = k->Identity(this); // Look for a nearby replacement 1210 if( i != k ) { // Found? Return replacement! 1211 NOT_PRODUCT( set_progress(); ) 1212 add_users_to_worklist( k ); 1213 subsume_node( k, i ); // Everybody using k now uses i 1214 return i; 1215 } 1216 1217 // Global Value Numbering 1218 i = hash_find_insert(k); // Check for pre-existing node 1219 if( i && (i != k) ) { 1220 // Return the pre-existing node if it isn't dead 1221 NOT_PRODUCT( set_progress(); ) 1222 add_users_to_worklist( k ); 1223 subsume_node( k, i ); // Everybody using k now uses i 1224 return i; 1225 } 1226 1227 // Return Idealized original 1228 return k; 1229 } 1230 1231 //---------------------------------saturate------------------------------------ 1232 const Type* PhaseIterGVN::saturate(const Type* new_type, const Type* old_type, 1233 const Type* limit_type) const { 1234 return new_type->narrow(old_type); 1235 } 1236 1237 //------------------------------remove_globally_dead_node---------------------- 1238 // Kill a globally dead Node. All uses are also globally dead and are 1239 // aggressively trimmed. 1240 void PhaseIterGVN::remove_globally_dead_node( Node *dead ) { 1241 enum DeleteProgress { 1242 PROCESS_INPUTS, 1243 PROCESS_OUTPUTS 1244 }; 1245 assert(_stack.is_empty(), "not empty"); 1246 _stack.push(dead, PROCESS_INPUTS); 1247 1248 while (_stack.is_nonempty()) { 1249 dead = _stack.node(); 1250 if (dead->Opcode() == Op_SafePoint) { 1251 dead->as_SafePoint()->disconnect_from_root(this); 1252 } 1253 uint progress_state = _stack.index(); 1254 assert(dead != C->root(), "killing root, eh?"); 1255 assert(!dead->is_top(), "add check for top when pushing"); 1256 NOT_PRODUCT( set_progress(); ) 1257 if (progress_state == PROCESS_INPUTS) { 1258 // After following inputs, continue to outputs 1259 _stack.set_index(PROCESS_OUTPUTS); 1260 if (!dead->is_Con()) { // Don't kill cons but uses 1261 bool recurse = false; 1262 // Remove from hash table 1263 _table.hash_delete( dead ); 1264 // Smash all inputs to 'dead', isolating him completely 1265 for (uint i = 0; i < dead->req(); i++) { 1266 Node *in = dead->in(i); 1267 if (in != NULL && in != C->top()) { // Points to something? 1268 int nrep = dead->replace_edge(in, NULL); // Kill edges 1269 assert((nrep > 0), "sanity"); 1270 if (in->outcnt() == 0) { // Made input go dead? 1271 _stack.push(in, PROCESS_INPUTS); // Recursively remove 1272 recurse = true; 1273 } else if (in->outcnt() == 1 && 1274 in->has_special_unique_user()) { 1275 _worklist.push(in->unique_out()); 1276 } else if (in->outcnt() <= 2 && dead->is_Phi()) { 1277 if (in->Opcode() == Op_Region) { 1278 _worklist.push(in); 1279 } else if (in->is_Store()) { 1280 DUIterator_Fast imax, i = in->fast_outs(imax); 1281 _worklist.push(in->fast_out(i)); 1282 i++; 1283 if (in->outcnt() == 2) { 1284 _worklist.push(in->fast_out(i)); 1285 i++; 1286 } 1287 assert(!(i < imax), "sanity"); 1288 } 1289 } else if (in->Opcode() == Op_AddP && CallLeafNode::has_only_g1_wb_pre_uses(in)) { 1290 add_users_to_worklist(in); 1291 } 1292 if (ReduceFieldZeroing && dead->is_Load() && i == MemNode::Memory && 1293 in->is_Proj() && in->in(0) != NULL && in->in(0)->is_Initialize()) { 1294 // A Load that directly follows an InitializeNode is 1295 // going away. The Stores that follow are candidates 1296 // again to be captured by the InitializeNode. 1297 for (DUIterator_Fast jmax, j = in->fast_outs(jmax); j < jmax; j++) { 1298 Node *n = in->fast_out(j); 1299 if (n->is_Store()) { 1300 _worklist.push(n); 1301 } 1302 } 1303 } 1304 } // if (in != NULL && in != C->top()) 1305 } // for (uint i = 0; i < dead->req(); i++) 1306 if (recurse) { 1307 continue; 1308 } 1309 } // if (!dead->is_Con()) 1310 } // if (progress_state == PROCESS_INPUTS) 1311 1312 // Aggressively kill globally dead uses 1313 // (Rather than pushing all the outs at once, we push one at a time, 1314 // plus the parent to resume later, because of the indefinite number 1315 // of edge deletions per loop trip.) 1316 if (dead->outcnt() > 0) { 1317 // Recursively remove output edges 1318 _stack.push(dead->raw_out(0), PROCESS_INPUTS); 1319 } else { 1320 // Finished disconnecting all input and output edges. 1321 _stack.pop(); 1322 // Remove dead node from iterative worklist 1323 _worklist.remove(dead); 1324 // Constant node that has no out-edges and has only one in-edge from 1325 // root is usually dead. However, sometimes reshaping walk makes 1326 // it reachable by adding use edges. So, we will NOT count Con nodes 1327 // as dead to be conservative about the dead node count at any 1328 // given time. 1329 if (!dead->is_Con()) { 1330 C->record_dead_node(dead->_idx); 1331 } 1332 if (dead->is_macro()) { 1333 C->remove_macro_node(dead); 1334 } 1335 if (dead->is_expensive()) { 1336 C->remove_expensive_node(dead); 1337 } 1338 if (dead->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 1339 C->remove_shenandoah_barrier(reinterpret_cast<ShenandoahLoadReferenceBarrierNode*>(dead)); 1340 } 1341 CastIINode* cast = dead->isa_CastII(); 1342 if (cast != NULL && cast->has_range_check()) { 1343 C->remove_range_check_cast(cast); 1344 } 1345 } 1346 } // while (_stack.is_nonempty()) 1347 } 1348 1349 //------------------------------subsume_node----------------------------------- 1350 // Remove users from node 'old' and add them to node 'nn'. 1351 void PhaseIterGVN::subsume_node( Node *old, Node *nn ) { 1352 if (old->Opcode() == Op_SafePoint) { 1353 old->as_SafePoint()->disconnect_from_root(this); 1354 } 1355 assert( old != hash_find(old), "should already been removed" ); 1356 assert( old != C->top(), "cannot subsume top node"); 1357 // Copy debug or profile information to the new version: 1358 C->copy_node_notes_to(nn, old); 1359 // Move users of node 'old' to node 'nn' 1360 for (DUIterator_Last imin, i = old->last_outs(imin); i >= imin; ) { 1361 Node* use = old->last_out(i); // for each use... 1362 // use might need re-hashing (but it won't if it's a new node) 1363 bool is_in_table = _table.hash_delete( use ); 1364 // Update use-def info as well 1365 // We remove all occurrences of old within use->in, 1366 // so as to avoid rehashing any node more than once. 1367 // The hash table probe swamps any outer loop overhead. 1368 uint num_edges = 0; 1369 for (uint jmax = use->len(), j = 0; j < jmax; j++) { 1370 if (use->in(j) == old) { 1371 use->set_req(j, nn); 1372 ++num_edges; 1373 } 1374 } 1375 // Insert into GVN hash table if unique 1376 // If a duplicate, 'use' will be cleaned up when pulled off worklist 1377 if( is_in_table ) { 1378 hash_find_insert(use); 1379 } 1380 i -= num_edges; // we deleted 1 or more copies of this edge 1381 } 1382 1383 // Search for instance field data PhiNodes in the same region pointing to the old 1384 // memory PhiNode and update their instance memory ids to point to the new node. 1385 if (old->is_Phi() && old->as_Phi()->type()->has_memory() && old->in(0) != NULL) { 1386 Node* region = old->in(0); 1387 for (DUIterator_Fast imax, i = region->fast_outs(imax); i < imax; i++) { 1388 PhiNode* phi = region->fast_out(i)->isa_Phi(); 1389 if (phi != NULL && phi->inst_mem_id() == (int)old->_idx) { 1390 phi->set_inst_mem_id((int)nn->_idx); 1391 } 1392 } 1393 } 1394 1395 // Smash all inputs to 'old', isolating him completely 1396 Node *temp = new (C) Node(1); 1397 temp->init_req(0,nn); // Add a use to nn to prevent him from dying 1398 remove_dead_node( old ); 1399 temp->del_req(0); // Yank bogus edge 1400 #ifndef PRODUCT 1401 if( VerifyIterativeGVN ) { 1402 for ( int i = 0; i < _verify_window_size; i++ ) { 1403 if ( _verify_window[i] == old ) 1404 _verify_window[i] = nn; 1405 } 1406 } 1407 #endif 1408 _worklist.remove(temp); // this can be necessary 1409 temp->destruct(); // reuse the _idx of this little guy 1410 } 1411 1412 //------------------------------add_users_to_worklist-------------------------- 1413 void PhaseIterGVN::add_users_to_worklist0( Node *n ) { 1414 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1415 _worklist.push(n->fast_out(i)); // Push on worklist 1416 } 1417 } 1418 1419 // Return counted loop Phi if as a counted loop exit condition, cmp 1420 // compares the the induction variable with n 1421 static PhiNode* countedloop_phi_from_cmp(CmpINode* cmp, Node* n) { 1422 for (DUIterator_Fast imax, i = cmp->fast_outs(imax); i < imax; i++) { 1423 Node* bol = cmp->fast_out(i); 1424 for (DUIterator_Fast i2max, i2 = bol->fast_outs(i2max); i2 < i2max; i2++) { 1425 Node* iff = bol->fast_out(i2); 1426 if (iff->is_CountedLoopEnd()) { 1427 CountedLoopEndNode* cle = iff->as_CountedLoopEnd(); 1428 if (cle->limit() == n) { 1429 PhiNode* phi = cle->phi(); 1430 if (phi != NULL) { 1431 return phi; 1432 } 1433 } 1434 } 1435 } 1436 } 1437 return NULL; 1438 } 1439 1440 void PhaseIterGVN::add_users_to_worklist( Node *n ) { 1441 add_users_to_worklist0(n); 1442 1443 // Move users of node to worklist 1444 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1445 Node* use = n->fast_out(i); // Get use 1446 1447 if( use->is_Multi() || // Multi-definer? Push projs on worklist 1448 use->is_Store() ) // Enable store/load same address 1449 add_users_to_worklist0(use); 1450 1451 // If we changed the receiver type to a call, we need to revisit 1452 // the Catch following the call. It's looking for a non-NULL 1453 // receiver to know when to enable the regular fall-through path 1454 // in addition to the NullPtrException path. 1455 if (use->is_CallDynamicJava() && n == use->in(TypeFunc::Parms)) { 1456 Node* p = use->as_CallDynamicJava()->proj_out(TypeFunc::Control); 1457 if (p != NULL) { 1458 add_users_to_worklist0(p); 1459 } 1460 } 1461 1462 uint use_op = use->Opcode(); 1463 if(use->is_Cmp()) { // Enable CMP/BOOL optimization 1464 add_users_to_worklist(use); // Put Bool on worklist 1465 if (use->outcnt() > 0) { 1466 Node* bol = use->raw_out(0); 1467 if (bol->outcnt() > 0) { 1468 Node* iff = bol->raw_out(0); 1469 if (iff->outcnt() == 2) { 1470 // Look for the 'is_x2logic' pattern: "x ? : 0 : 1" and put the 1471 // phi merging either 0 or 1 onto the worklist 1472 Node* ifproj0 = iff->raw_out(0); 1473 Node* ifproj1 = iff->raw_out(1); 1474 if (ifproj0->outcnt() > 0 && ifproj1->outcnt() > 0) { 1475 Node* region0 = ifproj0->raw_out(0); 1476 Node* region1 = ifproj1->raw_out(0); 1477 if( region0 == region1 ) 1478 add_users_to_worklist0(region0); 1479 } 1480 } 1481 } 1482 } 1483 if (use_op == Op_CmpI) { 1484 Node* phi = countedloop_phi_from_cmp((CmpINode*)use, n); 1485 if (phi != NULL) { 1486 // If an opaque node feeds into the limit condition of a 1487 // CountedLoop, we need to process the Phi node for the 1488 // induction variable when the opaque node is removed: 1489 // the range of values taken by the Phi is now known and 1490 // so its type is also known. 1491 _worklist.push(phi); 1492 } 1493 Node* in1 = use->in(1); 1494 for (uint i = 0; i < in1->outcnt(); i++) { 1495 if (in1->raw_out(i)->Opcode() == Op_CastII) { 1496 Node* castii = in1->raw_out(i); 1497 if (castii->in(0) != NULL && castii->in(0)->in(0) != NULL && castii->in(0)->in(0)->is_If()) { 1498 Node* ifnode = castii->in(0)->in(0); 1499 if (ifnode->in(1) != NULL && ifnode->in(1)->is_Bool() && ifnode->in(1)->in(1) == use) { 1500 // Reprocess a CastII node that may depend on an 1501 // opaque node value when the opaque node is 1502 // removed. In case it carries a dependency we can do 1503 // a better job of computing its type. 1504 _worklist.push(castii); 1505 } 1506 } 1507 } 1508 } 1509 } 1510 } 1511 1512 // If changed Cast input, check Phi users for simple cycles 1513 if( use->is_ConstraintCast() || use->is_CheckCastPP() ) { 1514 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1515 Node* u = use->fast_out(i2); 1516 if (u->is_Phi()) 1517 _worklist.push(u); 1518 } 1519 } 1520 // If changed LShift inputs, check RShift users for useless sign-ext 1521 if( use_op == Op_LShiftI ) { 1522 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1523 Node* u = use->fast_out(i2); 1524 if (u->Opcode() == Op_RShiftI) 1525 _worklist.push(u); 1526 } 1527 } 1528 // If changed AddI/SubI inputs, check CmpU for range check optimization. 1529 if (use_op == Op_AddI || use_op == Op_SubI) { 1530 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1531 Node* u = use->fast_out(i2); 1532 if (u->is_Cmp() && (u->Opcode() == Op_CmpU)) { 1533 _worklist.push(u); 1534 } 1535 } 1536 } 1537 // If changed AddP inputs, check Stores for loop invariant 1538 if( use_op == Op_AddP ) { 1539 for (DUIterator_Fast i2max, i2 = use->fast_outs(i2max); i2 < i2max; i2++) { 1540 Node* u = use->fast_out(i2); 1541 if (u->is_Mem()) 1542 _worklist.push(u); 1543 } 1544 } 1545 // If changed initialization activity, check dependent Stores 1546 if (use_op == Op_Allocate || use_op == Op_AllocateArray) { 1547 InitializeNode* init = use->as_Allocate()->initialization(); 1548 if (init != NULL) { 1549 Node* imem = init->proj_out(TypeFunc::Memory); 1550 if (imem != NULL) add_users_to_worklist0(imem); 1551 } 1552 } 1553 if (use_op == Op_Initialize) { 1554 Node* imem = use->as_Initialize()->proj_out(TypeFunc::Memory); 1555 if (imem != NULL) add_users_to_worklist0(imem); 1556 } 1557 1558 if (use->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 1559 Node* cmp = use->find_out_with(Op_CmpP); 1560 if (cmp != NULL) { 1561 _worklist.push(cmp); 1562 } 1563 } 1564 } 1565 } 1566 1567 /** 1568 * Remove the speculative part of all types that we know of 1569 */ 1570 void PhaseIterGVN::remove_speculative_types() { 1571 assert(UseTypeSpeculation, "speculation is off"); 1572 for (uint i = 0; i < _types.Size(); i++) { 1573 const Type* t = _types.fast_lookup(i); 1574 if (t != NULL) { 1575 _types.map(i, t->remove_speculative()); 1576 } 1577 } 1578 _table.check_no_speculative_types(); 1579 } 1580 1581 //============================================================================= 1582 #ifndef PRODUCT 1583 uint PhaseCCP::_total_invokes = 0; 1584 uint PhaseCCP::_total_constants = 0; 1585 #endif 1586 //------------------------------PhaseCCP--------------------------------------- 1587 // Conditional Constant Propagation, ala Wegman & Zadeck 1588 PhaseCCP::PhaseCCP( PhaseIterGVN *igvn ) : PhaseIterGVN(igvn) { 1589 NOT_PRODUCT( clear_constants(); ) 1590 assert( _worklist.size() == 0, "" ); 1591 // Clear out _nodes from IterGVN. Must be clear to transform call. 1592 _nodes.clear(); // Clear out from IterGVN 1593 analyze(); 1594 } 1595 1596 #ifndef PRODUCT 1597 //------------------------------~PhaseCCP-------------------------------------- 1598 PhaseCCP::~PhaseCCP() { 1599 inc_invokes(); 1600 _total_constants += count_constants(); 1601 } 1602 #endif 1603 1604 1605 #ifdef ASSERT 1606 static bool ccp_type_widens(const Type* t, const Type* t0) { 1607 assert(t->meet(t0) == t, "Not monotonic"); 1608 switch (t->base() == t0->base() ? t->base() : Type::Top) { 1609 case Type::Int: 1610 assert(t0->isa_int()->_widen <= t->isa_int()->_widen, "widen increases"); 1611 break; 1612 case Type::Long: 1613 assert(t0->isa_long()->_widen <= t->isa_long()->_widen, "widen increases"); 1614 break; 1615 } 1616 return true; 1617 } 1618 #endif //ASSERT 1619 1620 //------------------------------analyze---------------------------------------- 1621 void PhaseCCP::analyze() { 1622 // Initialize all types to TOP, optimistic analysis 1623 for (int i = C->unique() - 1; i >= 0; i--) { 1624 _types.map(i,Type::TOP); 1625 } 1626 1627 // Push root onto worklist 1628 Unique_Node_List worklist; 1629 worklist.push(C->root()); 1630 1631 // Pull from worklist; compute new value; push changes out. 1632 // This loop is the meat of CCP. 1633 while( worklist.size() ) { 1634 Node *n = worklist.pop(); 1635 const Type *t = n->Value(this); 1636 if (t != type(n)) { 1637 assert(ccp_type_widens(t, type(n)), "ccp type must widen"); 1638 #ifndef PRODUCT 1639 if( TracePhaseCCP ) { 1640 t->dump(); 1641 do { tty->print("\t"); } while (tty->position() < 16); 1642 n->dump(); 1643 } 1644 #endif 1645 set_type(n, t); 1646 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 1647 Node* m = n->fast_out(i); // Get user 1648 if (m->is_Region()) { // New path to Region? Must recheck Phis too 1649 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) { 1650 Node* p = m->fast_out(i2); // Propagate changes to uses 1651 if (p->bottom_type() != type(p)) { // If not already bottomed out 1652 worklist.push(p); // Propagate change to user 1653 } 1654 } 1655 } 1656 // If we changed the receiver type to a call, we need to revisit 1657 // the Catch following the call. It's looking for a non-NULL 1658 // receiver to know when to enable the regular fall-through path 1659 // in addition to the NullPtrException path 1660 if (m->is_Call()) { 1661 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) { 1662 Node* p = m->fast_out(i2); // Propagate changes to uses 1663 if (p->is_Proj() && p->as_Proj()->_con == TypeFunc::Control) { 1664 Node* catch_node = p->find_out_with(Op_Catch); 1665 if (catch_node != NULL) { 1666 worklist.push(catch_node); 1667 } 1668 } 1669 } 1670 } 1671 if (m->bottom_type() != type(m)) { // If not already bottomed out 1672 worklist.push(m); // Propagate change to user 1673 } 1674 1675 // CmpU nodes can get their type information from two nodes up in the 1676 // graph (instead of from the nodes immediately above). Make sure they 1677 // are added to the worklist if nodes they depend on are updated, since 1678 // they could be missed and get wrong types otherwise. 1679 uint m_op = m->Opcode(); 1680 if (m_op == Op_AddI || m_op == Op_SubI) { 1681 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) { 1682 Node* p = m->fast_out(i2); // Propagate changes to uses 1683 if (p->Opcode() == Op_CmpU) { 1684 // Got a CmpU which might need the new type information from node n. 1685 if(p->bottom_type() != type(p)) { // If not already bottomed out 1686 worklist.push(p); // Propagate change to user 1687 } 1688 } 1689 } 1690 } 1691 if (m->Opcode() == Op_ShenandoahLoadReferenceBarrier) { 1692 for (DUIterator_Fast i2max, i2 = m->fast_outs(i2max); i2 < i2max; i2++) { 1693 Node* p = m->fast_out(i2); 1694 if (p->Opcode() == Op_CmpP) { 1695 if(p->bottom_type() != type(p)) { 1696 worklist.push(p); 1697 } 1698 } else if (p->Opcode() == Op_AddP) { 1699 for (DUIterator_Fast i3max, i3 = p->fast_outs(i3max); i3 < i3max; i3++) { 1700 Node* q = p->fast_out(i3); 1701 if (q->is_Load()) { 1702 if(q->bottom_type() != type(q)) { 1703 worklist.push(q); 1704 } 1705 } 1706 } 1707 } 1708 } 1709 } 1710 // If n is used in a counted loop exit condition then the type 1711 // of the counted loop's Phi depends on the type of n. See 1712 // PhiNode::Value(). 1713 if (m_op == Op_CmpI) { 1714 PhiNode* phi = countedloop_phi_from_cmp((CmpINode*)m, n); 1715 if (phi != NULL) { 1716 worklist.push(phi); 1717 } 1718 } 1719 } 1720 } 1721 } 1722 } 1723 1724 //------------------------------do_transform----------------------------------- 1725 // Top level driver for the recursive transformer 1726 void PhaseCCP::do_transform() { 1727 // Correct leaves of new-space Nodes; they point to old-space. 1728 C->set_root( transform(C->root())->as_Root() ); 1729 assert( C->top(), "missing TOP node" ); 1730 assert( C->root(), "missing root" ); 1731 } 1732 1733 //------------------------------transform-------------------------------------- 1734 // Given a Node in old-space, clone him into new-space. 1735 // Convert any of his old-space children into new-space children. 1736 Node *PhaseCCP::transform( Node *n ) { 1737 Node *new_node = _nodes[n->_idx]; // Check for transformed node 1738 if( new_node != NULL ) 1739 return new_node; // Been there, done that, return old answer 1740 new_node = transform_once(n); // Check for constant 1741 _nodes.map( n->_idx, new_node ); // Flag as having been cloned 1742 1743 // Allocate stack of size _nodes.Size()/2 to avoid frequent realloc 1744 GrowableArray <Node *> trstack(C->live_nodes() >> 1); 1745 1746 trstack.push(new_node); // Process children of cloned node 1747 while ( trstack.is_nonempty() ) { 1748 Node *clone = trstack.pop(); 1749 uint cnt = clone->req(); 1750 for( uint i = 0; i < cnt; i++ ) { // For all inputs do 1751 Node *input = clone->in(i); 1752 if( input != NULL ) { // Ignore NULLs 1753 Node *new_input = _nodes[input->_idx]; // Check for cloned input node 1754 if( new_input == NULL ) { 1755 new_input = transform_once(input); // Check for constant 1756 _nodes.map( input->_idx, new_input );// Flag as having been cloned 1757 trstack.push(new_input); 1758 } 1759 assert( new_input == clone->in(i), "insanity check"); 1760 } 1761 } 1762 } 1763 return new_node; 1764 } 1765 1766 1767 //------------------------------transform_once--------------------------------- 1768 // For PhaseCCP, transformation is IDENTITY unless Node computed a constant. 1769 Node *PhaseCCP::transform_once( Node *n ) { 1770 const Type *t = type(n); 1771 // Constant? Use constant Node instead 1772 if( t->singleton() ) { 1773 Node *nn = n; // Default is to return the original constant 1774 if( t == Type::TOP ) { 1775 // cache my top node on the Compile instance 1776 if( C->cached_top_node() == NULL || C->cached_top_node()->in(0) == NULL ) { 1777 C->set_cached_top_node( ConNode::make(C, Type::TOP) ); 1778 set_type(C->top(), Type::TOP); 1779 } 1780 nn = C->top(); 1781 } 1782 if( !n->is_Con() ) { 1783 if( t != Type::TOP ) { 1784 nn = makecon(t); // ConNode::make(t); 1785 NOT_PRODUCT( inc_constants(); ) 1786 } else if( n->is_Region() ) { // Unreachable region 1787 // Note: nn == C->top() 1788 n->set_req(0, NULL); // Cut selfreference 1789 // Eagerly remove dead phis to avoid phis copies creation. 1790 for (DUIterator i = n->outs(); n->has_out(i); i++) { 1791 Node* m = n->out(i); 1792 if( m->is_Phi() ) { 1793 assert(type(m) == Type::TOP, "Unreachable region should not have live phis."); 1794 replace_node(m, nn); 1795 --i; // deleted this phi; rescan starting with next position 1796 } 1797 } 1798 } 1799 replace_node(n,nn); // Update DefUse edges for new constant 1800 } 1801 return nn; 1802 } 1803 1804 // If x is a TypeNode, capture any more-precise type permanently into Node 1805 if (t != n->bottom_type()) { 1806 hash_delete(n); // changing bottom type may force a rehash 1807 n->raise_bottom_type(t); 1808 _worklist.push(n); // n re-enters the hash table via the worklist 1809 } 1810 1811 // TEMPORARY fix to ensure that 2nd GVN pass eliminates NULL checks 1812 switch( n->Opcode() ) { 1813 case Op_FastLock: // Revisit FastLocks for lock coarsening 1814 case Op_If: 1815 case Op_CountedLoopEnd: 1816 case Op_Region: 1817 case Op_Loop: 1818 case Op_CountedLoop: 1819 case Op_Conv2B: 1820 case Op_Opaque1: 1821 case Op_Opaque2: 1822 _worklist.push(n); 1823 break; 1824 default: 1825 break; 1826 } 1827 1828 return n; 1829 } 1830 1831 //---------------------------------saturate------------------------------------ 1832 const Type* PhaseCCP::saturate(const Type* new_type, const Type* old_type, 1833 const Type* limit_type) const { 1834 const Type* wide_type = new_type->widen(old_type, limit_type); 1835 if (wide_type != new_type) { // did we widen? 1836 // If so, we may have widened beyond the limit type. Clip it back down. 1837 new_type = wide_type->filter(limit_type); 1838 } 1839 return new_type; 1840 } 1841 1842 //------------------------------print_statistics------------------------------- 1843 #ifndef PRODUCT 1844 void PhaseCCP::print_statistics() { 1845 tty->print_cr("CCP: %d constants found: %d", _total_invokes, _total_constants); 1846 } 1847 #endif 1848 1849 1850 //============================================================================= 1851 #ifndef PRODUCT 1852 uint PhasePeephole::_total_peepholes = 0; 1853 #endif 1854 //------------------------------PhasePeephole---------------------------------- 1855 // Conditional Constant Propagation, ala Wegman & Zadeck 1856 PhasePeephole::PhasePeephole( PhaseRegAlloc *regalloc, PhaseCFG &cfg ) 1857 : PhaseTransform(Peephole), _regalloc(regalloc), _cfg(cfg) { 1858 NOT_PRODUCT( clear_peepholes(); ) 1859 } 1860 1861 #ifndef PRODUCT 1862 //------------------------------~PhasePeephole--------------------------------- 1863 PhasePeephole::~PhasePeephole() { 1864 _total_peepholes += count_peepholes(); 1865 } 1866 #endif 1867 1868 //------------------------------transform-------------------------------------- 1869 Node *PhasePeephole::transform( Node *n ) { 1870 ShouldNotCallThis(); 1871 return NULL; 1872 } 1873 1874 //------------------------------do_transform----------------------------------- 1875 void PhasePeephole::do_transform() { 1876 bool method_name_not_printed = true; 1877 1878 // Examine each basic block 1879 for (uint block_number = 1; block_number < _cfg.number_of_blocks(); ++block_number) { 1880 Block* block = _cfg.get_block(block_number); 1881 bool block_not_printed = true; 1882 1883 // and each instruction within a block 1884 uint end_index = block->number_of_nodes(); 1885 // block->end_idx() not valid after PhaseRegAlloc 1886 for( uint instruction_index = 1; instruction_index < end_index; ++instruction_index ) { 1887 Node *n = block->get_node(instruction_index); 1888 if( n->is_Mach() ) { 1889 MachNode *m = n->as_Mach(); 1890 int deleted_count = 0; 1891 // check for peephole opportunities 1892 MachNode *m2 = m->peephole( block, instruction_index, _regalloc, deleted_count, C ); 1893 if( m2 != NULL ) { 1894 #ifndef PRODUCT 1895 if( PrintOptoPeephole ) { 1896 // Print method, first time only 1897 if( C->method() && method_name_not_printed ) { 1898 C->method()->print_short_name(); tty->cr(); 1899 method_name_not_printed = false; 1900 } 1901 // Print this block 1902 if( Verbose && block_not_printed) { 1903 tty->print_cr("in block"); 1904 block->dump(); 1905 block_not_printed = false; 1906 } 1907 // Print instructions being deleted 1908 for( int i = (deleted_count - 1); i >= 0; --i ) { 1909 block->get_node(instruction_index-i)->as_Mach()->format(_regalloc); tty->cr(); 1910 } 1911 tty->print_cr("replaced with"); 1912 // Print new instruction 1913 m2->format(_regalloc); 1914 tty->print("\n\n"); 1915 } 1916 #endif 1917 // Remove old nodes from basic block and update instruction_index 1918 // (old nodes still exist and may have edges pointing to them 1919 // as register allocation info is stored in the allocator using 1920 // the node index to live range mappings.) 1921 uint safe_instruction_index = (instruction_index - deleted_count); 1922 for( ; (instruction_index > safe_instruction_index); --instruction_index ) { 1923 block->remove_node( instruction_index ); 1924 } 1925 // install new node after safe_instruction_index 1926 block->insert_node(m2, safe_instruction_index + 1); 1927 end_index = block->number_of_nodes() - 1; // Recompute new block size 1928 NOT_PRODUCT( inc_peepholes(); ) 1929 } 1930 } 1931 } 1932 } 1933 } 1934 1935 //------------------------------print_statistics------------------------------- 1936 #ifndef PRODUCT 1937 void PhasePeephole::print_statistics() { 1938 tty->print_cr("Peephole: peephole rules applied: %d", _total_peepholes); 1939 } 1940 #endif 1941 1942 1943 //============================================================================= 1944 //------------------------------set_req_X-------------------------------------- 1945 void Node::set_req_X( uint i, Node *n, PhaseIterGVN *igvn ) { 1946 assert( is_not_dead(n), "can not use dead node"); 1947 assert( igvn->hash_find(this) != this, "Need to remove from hash before changing edges" ); 1948 Node *old = in(i); 1949 set_req(i, n); 1950 1951 // old goes dead? 1952 if( old ) { 1953 switch (old->outcnt()) { 1954 case 0: 1955 // Put into the worklist to kill later. We do not kill it now because the 1956 // recursive kill will delete the current node (this) if dead-loop exists 1957 if (!old->is_top()) 1958 igvn->_worklist.push( old ); 1959 break; 1960 case 1: 1961 if( old->is_Store() || old->has_special_unique_user() ) 1962 igvn->add_users_to_worklist( old ); 1963 break; 1964 case 2: 1965 if( old->is_Store() ) 1966 igvn->add_users_to_worklist( old ); 1967 if( old->Opcode() == Op_Region ) 1968 igvn->_worklist.push(old); 1969 break; 1970 case 3: 1971 if( old->Opcode() == Op_Region ) { 1972 igvn->_worklist.push(old); 1973 igvn->add_users_to_worklist( old ); 1974 } 1975 break; 1976 default: 1977 break; 1978 } 1979 if (old->Opcode() == Op_AddP && CallLeafNode::has_only_g1_wb_pre_uses(old)) { 1980 igvn->add_users_to_worklist(old); 1981 } 1982 } 1983 1984 } 1985 1986 //-------------------------------replace_by----------------------------------- 1987 // Using def-use info, replace one node for another. Follow the def-use info 1988 // to all users of the OLD node. Then make all uses point to the NEW node. 1989 void Node::replace_by(Node *new_node) { 1990 assert(!is_top(), "top node has no DU info"); 1991 for (DUIterator_Last imin, i = last_outs(imin); i >= imin; ) { 1992 Node* use = last_out(i); 1993 uint uses_found = 0; 1994 for (uint j = 0; j < use->len(); j++) { 1995 if (use->in(j) == this) { 1996 if (j < use->req()) 1997 use->set_req(j, new_node); 1998 else use->set_prec(j, new_node); 1999 uses_found++; 2000 } 2001 } 2002 i -= uses_found; // we deleted 1 or more copies of this edge 2003 } 2004 } 2005 2006 //============================================================================= 2007 //----------------------------------------------------------------------------- 2008 void Type_Array::grow( uint i ) { 2009 if( !_max ) { 2010 _max = 1; 2011 _types = (const Type**)_a->Amalloc( _max * sizeof(Type*) ); 2012 _types[0] = NULL; 2013 } 2014 uint old = _max; 2015 while( i >= _max ) _max <<= 1; // Double to fit 2016 _types = (const Type**)_a->Arealloc( _types, old*sizeof(Type*),_max*sizeof(Type*)); 2017 memset( &_types[old], 0, (_max-old)*sizeof(Type*) ); 2018 } 2019 2020 //------------------------------dump------------------------------------------- 2021 #ifndef PRODUCT 2022 void Type_Array::dump() const { 2023 uint max = Size(); 2024 for( uint i = 0; i < max; i++ ) { 2025 if( _types[i] != NULL ) { 2026 tty->print(" %d\t== ", i); _types[i]->dump(); tty->cr(); 2027 } 2028 } 2029 } 2030 #endif