src/share/vm/opto/phaseX.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7395
4c228230f1d6
parent 6876
710a3c8b516e
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial