src/share/vm/opto/phaseX.cpp

Tue, 09 Oct 2012 10:11:38 +0200

author
roland
date
Tue, 09 Oct 2012 10:11:38 +0200
changeset 4159
8e47bac5643a
parent 4115
e626685e9f6c
child 4153
b9a9ed0f8eeb
permissions
-rw-r--r--

7054512: Compress class pointers after perm gen removal
Summary: support of compress class pointers in the compilers.
Reviewed-by: kvn, twisti

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

mercurial