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