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