src/share/vm/opto/phaseX.cpp

Thu, 24 May 2018 19:26:50 +0800

author
aoqi
date
Thu, 24 May 2018 19:26:50 +0800
changeset 8862
fd13a567f179
parent 8856
ac27a9c85bea
child 9041
95a08233f46c
permissions
-rw-r--r--

#7046 C2 supports long branch
Contributed-by: fujie

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

mercurial