1 /* 2 * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "gc/shared/barrierSet.hpp" 27 #include "gc/shared/c2/barrierSetC2.hpp" 28 #include "libadt/vectset.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "opto/ad.hpp" 32 #include "opto/callGenerator.hpp" 33 #include "opto/castnode.hpp" 34 #include "opto/cfgnode.hpp" 35 #include "opto/connode.hpp" 36 #include "opto/loopnode.hpp" 37 #include "opto/machnode.hpp" 38 #include "opto/matcher.hpp" 39 #include "opto/node.hpp" 40 #include "opto/opcodes.hpp" 41 #include "opto/regmask.hpp" 42 #include "opto/rootnode.hpp" 43 #include "opto/type.hpp" 44 #include "utilities/copy.hpp" 45 #include "utilities/macros.hpp" 46 #include "utilities/powerOfTwo.hpp" 47 #include "utilities/stringUtils.hpp" 48 49 class RegMask; 50 // #include "phase.hpp" 51 class PhaseTransform; 52 class PhaseGVN; 53 54 // Arena we are currently building Nodes in 55 const uint Node::NotAMachineReg = 0xffff0000; 56 57 #ifndef PRODUCT 58 extern uint nodes_created; 59 #endif 60 #ifdef __clang__ 61 #pragma clang diagnostic push 62 #pragma GCC diagnostic ignored "-Wuninitialized" 63 #endif 64 65 #ifdef ASSERT 66 67 //-------------------------- construct_node------------------------------------ 68 // Set a breakpoint here to identify where a particular node index is built. 69 void Node::verify_construction() { 70 _debug_orig = nullptr; 71 // The decimal digits of _debug_idx are <compile_id> followed by 10 digits of <_idx> 72 Compile* C = Compile::current(); 73 assert(C->unique() < (INT_MAX - 1), "Node limit exceeded INT_MAX"); 74 uint64_t new_debug_idx = (uint64_t)C->compile_id() * 10000000000 + _idx; 75 set_debug_idx(new_debug_idx); 76 if (!C->phase_optimize_finished()) { 77 // Only check assert during parsing and optimization phase. Skip it while generating code. 78 assert(C->live_nodes() <= C->max_node_limit(), "Live Node limit exceeded limit"); 79 } 80 if (BreakAtNode != 0 && (_debug_idx == BreakAtNode || (uint64_t)_idx == BreakAtNode)) { 81 tty->print_cr("BreakAtNode: _idx=%d _debug_idx=" UINT64_FORMAT, _idx, _debug_idx); 82 BREAKPOINT; 83 } 84 #if OPTO_DU_ITERATOR_ASSERT 85 _last_del = nullptr; 86 _del_tick = 0; 87 #endif 88 _hash_lock = 0; 89 } 90 91 92 // #ifdef ASSERT ... 93 94 #if OPTO_DU_ITERATOR_ASSERT 95 void DUIterator_Common::sample(const Node* node) { 96 _vdui = VerifyDUIterators; 97 _node = node; 98 _outcnt = node->_outcnt; 99 _del_tick = node->_del_tick; 100 _last = nullptr; 101 } 102 103 void DUIterator_Common::verify(const Node* node, bool at_end_ok) { 104 assert(_node == node, "consistent iterator source"); 105 assert(_del_tick == node->_del_tick, "no unexpected deletions allowed"); 106 } 107 108 void DUIterator_Common::verify_resync() { 109 // Ensure that the loop body has just deleted the last guy produced. 110 const Node* node = _node; 111 // Ensure that at least one copy of the last-seen edge was deleted. 112 // Note: It is OK to delete multiple copies of the last-seen edge. 113 // Unfortunately, we have no way to verify that all the deletions delete 114 // that same edge. On this point we must use the Honor System. 115 assert(node->_del_tick >= _del_tick+1, "must have deleted an edge"); 116 assert(node->_last_del == _last, "must have deleted the edge just produced"); 117 // We liked this deletion, so accept the resulting outcnt and tick. 118 _outcnt = node->_outcnt; 119 _del_tick = node->_del_tick; 120 } 121 122 void DUIterator_Common::reset(const DUIterator_Common& that) { 123 if (this == &that) return; // ignore assignment to self 124 if (!_vdui) { 125 // We need to initialize everything, overwriting garbage values. 126 _last = that._last; 127 _vdui = that._vdui; 128 } 129 // Note: It is legal (though odd) for an iterator over some node x 130 // to be reassigned to iterate over another node y. Some doubly-nested 131 // progress loops depend on being able to do this. 132 const Node* node = that._node; 133 // Re-initialize everything, except _last. 134 _node = node; 135 _outcnt = node->_outcnt; 136 _del_tick = node->_del_tick; 137 } 138 139 void DUIterator::sample(const Node* node) { 140 DUIterator_Common::sample(node); // Initialize the assertion data. 141 _refresh_tick = 0; // No refreshes have happened, as yet. 142 } 143 144 void DUIterator::verify(const Node* node, bool at_end_ok) { 145 DUIterator_Common::verify(node, at_end_ok); 146 assert(_idx < node->_outcnt + (uint)at_end_ok, "idx in range"); 147 } 148 149 void DUIterator::verify_increment() { 150 if (_refresh_tick & 1) { 151 // We have refreshed the index during this loop. 152 // Fix up _idx to meet asserts. 153 if (_idx > _outcnt) _idx = _outcnt; 154 } 155 verify(_node, true); 156 } 157 158 void DUIterator::verify_resync() { 159 // Note: We do not assert on _outcnt, because insertions are OK here. 160 DUIterator_Common::verify_resync(); 161 // Make sure we are still in sync, possibly with no more out-edges: 162 verify(_node, true); 163 } 164 165 void DUIterator::reset(const DUIterator& that) { 166 if (this == &that) return; // self assignment is always a no-op 167 assert(that._refresh_tick == 0, "assign only the result of Node::outs()"); 168 assert(that._idx == 0, "assign only the result of Node::outs()"); 169 assert(_idx == that._idx, "already assigned _idx"); 170 if (!_vdui) { 171 // We need to initialize everything, overwriting garbage values. 172 sample(that._node); 173 } else { 174 DUIterator_Common::reset(that); 175 if (_refresh_tick & 1) { 176 _refresh_tick++; // Clear the "was refreshed" flag. 177 } 178 assert(_refresh_tick < 2*100000, "DU iteration must converge quickly"); 179 } 180 } 181 182 void DUIterator::refresh() { 183 DUIterator_Common::sample(_node); // Re-fetch assertion data. 184 _refresh_tick |= 1; // Set the "was refreshed" flag. 185 } 186 187 void DUIterator::verify_finish() { 188 // If the loop has killed the node, do not require it to re-run. 189 if (_node->_outcnt == 0) _refresh_tick &= ~1; 190 // If this assert triggers, it means that a loop used refresh_out_pos 191 // to re-synch an iteration index, but the loop did not correctly 192 // re-run itself, using a "while (progress)" construct. 193 // This iterator enforces the rule that you must keep trying the loop 194 // until it "runs clean" without any need for refreshing. 195 assert(!(_refresh_tick & 1), "the loop must run once with no refreshing"); 196 } 197 198 199 void DUIterator_Fast::verify(const Node* node, bool at_end_ok) { 200 DUIterator_Common::verify(node, at_end_ok); 201 Node** out = node->_out; 202 uint cnt = node->_outcnt; 203 assert(cnt == _outcnt, "no insertions allowed"); 204 assert(_outp >= out && _outp <= out + cnt - !at_end_ok, "outp in range"); 205 // This last check is carefully designed to work for NO_OUT_ARRAY. 206 } 207 208 void DUIterator_Fast::verify_limit() { 209 const Node* node = _node; 210 verify(node, true); 211 assert(_outp == node->_out + node->_outcnt, "limit still correct"); 212 } 213 214 void DUIterator_Fast::verify_resync() { 215 const Node* node = _node; 216 if (_outp == node->_out + _outcnt) { 217 // Note that the limit imax, not the pointer i, gets updated with the 218 // exact count of deletions. (For the pointer it's always "--i".) 219 assert(node->_outcnt+node->_del_tick == _outcnt+_del_tick, "no insertions allowed with deletion(s)"); 220 // This is a limit pointer, with a name like "imax". 221 // Fudge the _last field so that the common assert will be happy. 222 _last = (Node*) node->_last_del; 223 DUIterator_Common::verify_resync(); 224 } else { 225 assert(node->_outcnt < _outcnt, "no insertions allowed with deletion(s)"); 226 // A normal internal pointer. 227 DUIterator_Common::verify_resync(); 228 // Make sure we are still in sync, possibly with no more out-edges: 229 verify(node, true); 230 } 231 } 232 233 void DUIterator_Fast::verify_relimit(uint n) { 234 const Node* node = _node; 235 assert((int)n > 0, "use imax -= n only with a positive count"); 236 // This must be a limit pointer, with a name like "imax". 237 assert(_outp == node->_out + node->_outcnt, "apply -= only to a limit (imax)"); 238 // The reported number of deletions must match what the node saw. 239 assert(node->_del_tick == _del_tick + n, "must have deleted n edges"); 240 // Fudge the _last field so that the common assert will be happy. 241 _last = (Node*) node->_last_del; 242 DUIterator_Common::verify_resync(); 243 } 244 245 void DUIterator_Fast::reset(const DUIterator_Fast& that) { 246 assert(_outp == that._outp, "already assigned _outp"); 247 DUIterator_Common::reset(that); 248 } 249 250 void DUIterator_Last::verify(const Node* node, bool at_end_ok) { 251 // at_end_ok means the _outp is allowed to underflow by 1 252 _outp += at_end_ok; 253 DUIterator_Fast::verify(node, at_end_ok); // check _del_tick, etc. 254 _outp -= at_end_ok; 255 assert(_outp == (node->_out + node->_outcnt) - 1, "pointer must point to end of nodes"); 256 } 257 258 void DUIterator_Last::verify_limit() { 259 // Do not require the limit address to be resynched. 260 //verify(node, true); 261 assert(_outp == _node->_out, "limit still correct"); 262 } 263 264 void DUIterator_Last::verify_step(uint num_edges) { 265 assert((int)num_edges > 0, "need non-zero edge count for loop progress"); 266 _outcnt -= num_edges; 267 _del_tick += num_edges; 268 // Make sure we are still in sync, possibly with no more out-edges: 269 const Node* node = _node; 270 verify(node, true); 271 assert(node->_last_del == _last, "must have deleted the edge just produced"); 272 } 273 274 #endif //OPTO_DU_ITERATOR_ASSERT 275 276 277 #endif //ASSERT 278 279 280 // This constant used to initialize _out may be any non-null value. 281 // The value null is reserved for the top node only. 282 #define NO_OUT_ARRAY ((Node**)-1) 283 284 // Out-of-line code from node constructors. 285 // Executed only when extra debug info. is being passed around. 286 static void init_node_notes(Compile* C, int idx, Node_Notes* nn) { 287 C->set_node_notes_at(idx, nn); 288 } 289 290 // Shared initialization code. 291 inline int Node::Init(int req) { 292 Compile* C = Compile::current(); 293 int idx = C->next_unique(); 294 NOT_PRODUCT(_igv_idx = C->next_igv_idx()); 295 296 // Allocate memory for the necessary number of edges. 297 if (req > 0) { 298 // Allocate space for _in array to have double alignment. 299 _in = (Node **) ((char *) (C->node_arena()->AmallocWords(req * sizeof(void*)))); 300 } 301 // If there are default notes floating around, capture them: 302 Node_Notes* nn = C->default_node_notes(); 303 if (nn != nullptr) init_node_notes(C, idx, nn); 304 305 // Note: At this point, C is dead, 306 // and we begin to initialize the new Node. 307 308 _cnt = _max = req; 309 _outcnt = _outmax = 0; 310 _class_id = Class_Node; 311 _flags = 0; 312 _out = NO_OUT_ARRAY; 313 return idx; 314 } 315 316 //------------------------------Node------------------------------------------- 317 // Create a Node, with a given number of required edges. 318 Node::Node(uint req) 319 : _idx(Init(req)) 320 #ifdef ASSERT 321 , _parse_idx(_idx) 322 #endif 323 { 324 assert( req < Compile::current()->max_node_limit() - NodeLimitFudgeFactor, "Input limit exceeded" ); 325 debug_only( verify_construction() ); 326 NOT_PRODUCT(nodes_created++); 327 if (req == 0) { 328 _in = nullptr; 329 } else { 330 Node** to = _in; 331 for(uint i = 0; i < req; i++) { 332 to[i] = nullptr; 333 } 334 } 335 } 336 337 //------------------------------Node------------------------------------------- 338 Node::Node(Node *n0) 339 : _idx(Init(1)) 340 #ifdef ASSERT 341 , _parse_idx(_idx) 342 #endif 343 { 344 debug_only( verify_construction() ); 345 NOT_PRODUCT(nodes_created++); 346 assert( is_not_dead(n0), "can not use dead node"); 347 _in[0] = n0; if (n0 != nullptr) n0->add_out((Node *)this); 348 } 349 350 //------------------------------Node------------------------------------------- 351 Node::Node(Node *n0, Node *n1) 352 : _idx(Init(2)) 353 #ifdef ASSERT 354 , _parse_idx(_idx) 355 #endif 356 { 357 debug_only( verify_construction() ); 358 NOT_PRODUCT(nodes_created++); 359 assert( is_not_dead(n0), "can not use dead node"); 360 assert( is_not_dead(n1), "can not use dead node"); 361 _in[0] = n0; if (n0 != nullptr) n0->add_out((Node *)this); 362 _in[1] = n1; if (n1 != nullptr) n1->add_out((Node *)this); 363 } 364 365 //------------------------------Node------------------------------------------- 366 Node::Node(Node *n0, Node *n1, Node *n2) 367 : _idx(Init(3)) 368 #ifdef ASSERT 369 , _parse_idx(_idx) 370 #endif 371 { 372 debug_only( verify_construction() ); 373 NOT_PRODUCT(nodes_created++); 374 assert( is_not_dead(n0), "can not use dead node"); 375 assert( is_not_dead(n1), "can not use dead node"); 376 assert( is_not_dead(n2), "can not use dead node"); 377 _in[0] = n0; if (n0 != nullptr) n0->add_out((Node *)this); 378 _in[1] = n1; if (n1 != nullptr) n1->add_out((Node *)this); 379 _in[2] = n2; if (n2 != nullptr) n2->add_out((Node *)this); 380 } 381 382 //------------------------------Node------------------------------------------- 383 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3) 384 : _idx(Init(4)) 385 #ifdef ASSERT 386 , _parse_idx(_idx) 387 #endif 388 { 389 debug_only( verify_construction() ); 390 NOT_PRODUCT(nodes_created++); 391 assert( is_not_dead(n0), "can not use dead node"); 392 assert( is_not_dead(n1), "can not use dead node"); 393 assert( is_not_dead(n2), "can not use dead node"); 394 assert( is_not_dead(n3), "can not use dead node"); 395 _in[0] = n0; if (n0 != nullptr) n0->add_out((Node *)this); 396 _in[1] = n1; if (n1 != nullptr) n1->add_out((Node *)this); 397 _in[2] = n2; if (n2 != nullptr) n2->add_out((Node *)this); 398 _in[3] = n3; if (n3 != nullptr) n3->add_out((Node *)this); 399 } 400 401 //------------------------------Node------------------------------------------- 402 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3, Node *n4) 403 : _idx(Init(5)) 404 #ifdef ASSERT 405 , _parse_idx(_idx) 406 #endif 407 { 408 debug_only( verify_construction() ); 409 NOT_PRODUCT(nodes_created++); 410 assert( is_not_dead(n0), "can not use dead node"); 411 assert( is_not_dead(n1), "can not use dead node"); 412 assert( is_not_dead(n2), "can not use dead node"); 413 assert( is_not_dead(n3), "can not use dead node"); 414 assert( is_not_dead(n4), "can not use dead node"); 415 _in[0] = n0; if (n0 != nullptr) n0->add_out((Node *)this); 416 _in[1] = n1; if (n1 != nullptr) n1->add_out((Node *)this); 417 _in[2] = n2; if (n2 != nullptr) n2->add_out((Node *)this); 418 _in[3] = n3; if (n3 != nullptr) n3->add_out((Node *)this); 419 _in[4] = n4; if (n4 != nullptr) n4->add_out((Node *)this); 420 } 421 422 //------------------------------Node------------------------------------------- 423 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3, 424 Node *n4, Node *n5) 425 : _idx(Init(6)) 426 #ifdef ASSERT 427 , _parse_idx(_idx) 428 #endif 429 { 430 debug_only( verify_construction() ); 431 NOT_PRODUCT(nodes_created++); 432 assert( is_not_dead(n0), "can not use dead node"); 433 assert( is_not_dead(n1), "can not use dead node"); 434 assert( is_not_dead(n2), "can not use dead node"); 435 assert( is_not_dead(n3), "can not use dead node"); 436 assert( is_not_dead(n4), "can not use dead node"); 437 assert( is_not_dead(n5), "can not use dead node"); 438 _in[0] = n0; if (n0 != nullptr) n0->add_out((Node *)this); 439 _in[1] = n1; if (n1 != nullptr) n1->add_out((Node *)this); 440 _in[2] = n2; if (n2 != nullptr) n2->add_out((Node *)this); 441 _in[3] = n3; if (n3 != nullptr) n3->add_out((Node *)this); 442 _in[4] = n4; if (n4 != nullptr) n4->add_out((Node *)this); 443 _in[5] = n5; if (n5 != nullptr) n5->add_out((Node *)this); 444 } 445 446 //------------------------------Node------------------------------------------- 447 Node::Node(Node *n0, Node *n1, Node *n2, Node *n3, 448 Node *n4, Node *n5, Node *n6) 449 : _idx(Init(7)) 450 #ifdef ASSERT 451 , _parse_idx(_idx) 452 #endif 453 { 454 debug_only( verify_construction() ); 455 NOT_PRODUCT(nodes_created++); 456 assert( is_not_dead(n0), "can not use dead node"); 457 assert( is_not_dead(n1), "can not use dead node"); 458 assert( is_not_dead(n2), "can not use dead node"); 459 assert( is_not_dead(n3), "can not use dead node"); 460 assert( is_not_dead(n4), "can not use dead node"); 461 assert( is_not_dead(n5), "can not use dead node"); 462 assert( is_not_dead(n6), "can not use dead node"); 463 _in[0] = n0; if (n0 != nullptr) n0->add_out((Node *)this); 464 _in[1] = n1; if (n1 != nullptr) n1->add_out((Node *)this); 465 _in[2] = n2; if (n2 != nullptr) n2->add_out((Node *)this); 466 _in[3] = n3; if (n3 != nullptr) n3->add_out((Node *)this); 467 _in[4] = n4; if (n4 != nullptr) n4->add_out((Node *)this); 468 _in[5] = n5; if (n5 != nullptr) n5->add_out((Node *)this); 469 _in[6] = n6; if (n6 != nullptr) n6->add_out((Node *)this); 470 } 471 472 #ifdef __clang__ 473 #pragma clang diagnostic pop 474 #endif 475 476 477 //------------------------------clone------------------------------------------ 478 // Clone a Node. 479 Node *Node::clone() const { 480 Compile* C = Compile::current(); 481 uint s = size_of(); // Size of inherited Node 482 Node *n = (Node*)C->node_arena()->AmallocWords(size_of() + _max*sizeof(Node*)); 483 Copy::conjoint_words_to_lower((HeapWord*)this, (HeapWord*)n, s); 484 // Set the new input pointer array 485 n->_in = (Node**)(((char*)n)+s); 486 // Cannot share the old output pointer array, so kill it 487 n->_out = NO_OUT_ARRAY; 488 // And reset the counters to 0 489 n->_outcnt = 0; 490 n->_outmax = 0; 491 // Unlock this guy, since he is not in any hash table. 492 debug_only(n->_hash_lock = 0); 493 // Walk the old node's input list to duplicate its edges 494 uint i; 495 for( i = 0; i < len(); i++ ) { 496 Node *x = in(i); 497 n->_in[i] = x; 498 if (x != nullptr) x->add_out(n); 499 } 500 if (is_macro()) { 501 C->add_macro_node(n); 502 } 503 if (is_expensive()) { 504 C->add_expensive_node(n); 505 } 506 if (for_post_loop_opts_igvn()) { 507 // Don't add cloned node to Compile::_for_post_loop_opts_igvn list automatically. 508 // If it is applicable, it will happen anyway when the cloned node is registered with IGVN. 509 n->remove_flag(Node::NodeFlags::Flag_for_post_loop_opts_igvn); 510 } 511 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 512 bs->register_potential_barrier_node(n); 513 514 n->set_idx(C->next_unique()); // Get new unique index as well 515 NOT_PRODUCT(n->_igv_idx = C->next_igv_idx()); 516 debug_only( n->verify_construction() ); 517 NOT_PRODUCT(nodes_created++); 518 // Do not patch over the debug_idx of a clone, because it makes it 519 // impossible to break on the clone's moment of creation. 520 //debug_only( n->set_debug_idx( debug_idx() ) ); 521 522 C->copy_node_notes_to(n, (Node*) this); 523 524 // MachNode clone 525 uint nopnds; 526 if (this->is_Mach() && (nopnds = this->as_Mach()->num_opnds()) > 0) { 527 MachNode *mach = n->as_Mach(); 528 MachNode *mthis = this->as_Mach(); 529 // Get address of _opnd_array. 530 // It should be the same offset since it is the clone of this node. 531 MachOper **from = mthis->_opnds; 532 MachOper **to = (MachOper **)((size_t)(&mach->_opnds) + 533 pointer_delta((const void*)from, 534 (const void*)(&mthis->_opnds), 1)); 535 mach->_opnds = to; 536 for ( uint i = 0; i < nopnds; ++i ) { 537 to[i] = from[i]->clone(); 538 } 539 } 540 if (n->is_Call()) { 541 // CallGenerator is linked to the original node. 542 CallGenerator* cg = n->as_Call()->generator(); 543 if (cg != nullptr) { 544 CallGenerator* cloned_cg = cg->with_call_node(n->as_Call()); 545 n->as_Call()->set_generator(cloned_cg); 546 547 C->print_inlining_assert_ready(); 548 C->print_inlining_move_to(cg); 549 C->print_inlining_update(cloned_cg); 550 } 551 } 552 if (n->is_SafePoint()) { 553 // Scalar replacement and macro expansion might modify the JVMState. 554 // Clone it to make sure it's not shared between SafePointNodes. 555 n->as_SafePoint()->clone_jvms(C); 556 n->as_SafePoint()->clone_replaced_nodes(); 557 } 558 Compile::current()->record_modified_node(n); 559 return n; // Return the clone 560 } 561 562 //---------------------------setup_is_top-------------------------------------- 563 // Call this when changing the top node, to reassert the invariants 564 // required by Node::is_top. See Compile::set_cached_top_node. 565 void Node::setup_is_top() { 566 if (this == (Node*)Compile::current()->top()) { 567 // This node has just become top. Kill its out array. 568 _outcnt = _outmax = 0; 569 _out = nullptr; // marker value for top 570 assert(is_top(), "must be top"); 571 } else { 572 if (_out == nullptr) _out = NO_OUT_ARRAY; 573 assert(!is_top(), "must not be top"); 574 } 575 } 576 577 //------------------------------~Node------------------------------------------ 578 // Fancy destructor; eagerly attempt to reclaim Node numberings and storage 579 void Node::destruct(PhaseValues* phase) { 580 Compile* compile = (phase != nullptr) ? phase->C : Compile::current(); 581 if (phase != nullptr && phase->is_IterGVN()) { 582 phase->is_IterGVN()->_worklist.remove(this); 583 } 584 // If this is the most recently created node, reclaim its index. Otherwise, 585 // record the node as dead to keep liveness information accurate. 586 if ((uint)_idx+1 == compile->unique()) { 587 compile->set_unique(compile->unique()-1); 588 } else { 589 compile->record_dead_node(_idx); 590 } 591 // Clear debug info: 592 Node_Notes* nn = compile->node_notes_at(_idx); 593 if (nn != nullptr) nn->clear(); 594 // Walk the input array, freeing the corresponding output edges 595 _cnt = _max; // forget req/prec distinction 596 uint i; 597 for( i = 0; i < _max; i++ ) { 598 set_req(i, nullptr); 599 //assert(def->out(def->outcnt()-1) == (Node *)this,"bad def-use hacking in reclaim"); 600 } 601 assert(outcnt() == 0, "deleting a node must not leave a dangling use"); 602 603 if (is_macro()) { 604 compile->remove_macro_node(this); 605 } 606 if (is_expensive()) { 607 compile->remove_expensive_node(this); 608 } 609 if (Opcode() == Op_Opaque4) { 610 compile->remove_template_assertion_predicate_opaq(this); 611 } 612 if (for_post_loop_opts_igvn()) { 613 compile->remove_from_post_loop_opts_igvn(this); 614 } 615 616 if (is_SafePoint()) { 617 as_SafePoint()->delete_replaced_nodes(); 618 619 if (is_CallStaticJava()) { 620 compile->remove_unstable_if_trap(as_CallStaticJava(), false); 621 } 622 } 623 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 624 bs->unregister_potential_barrier_node(this); 625 626 // See if the input array was allocated just prior to the object 627 int edge_size = _max*sizeof(void*); 628 int out_edge_size = _outmax*sizeof(void*); 629 char *in_array = ((char*)_in); 630 char *edge_end = in_array + edge_size; 631 char *out_array = (char*)(_out == NO_OUT_ARRAY? nullptr: _out); 632 int node_size = size_of(); 633 634 #ifdef ASSERT 635 // We will not actually delete the storage, but we'll make the node unusable. 636 compile->remove_modified_node(this); 637 *(address*)this = badAddress; // smash the C++ vtbl, probably 638 _in = _out = (Node**) badAddress; 639 _max = _cnt = _outmax = _outcnt = 0; 640 #endif 641 642 // Free the output edge array 643 if (out_edge_size > 0) { 644 compile->node_arena()->Afree(out_array, out_edge_size); 645 } 646 647 // Free the input edge array and the node itself 648 if( edge_end == (char*)this ) { 649 // It was; free the input array and object all in one hit 650 #ifndef ASSERT 651 compile->node_arena()->Afree(in_array, edge_size+node_size); 652 #endif 653 } else { 654 // Free just the input array 655 compile->node_arena()->Afree(in_array, edge_size); 656 657 // Free just the object 658 #ifndef ASSERT 659 compile->node_arena()->Afree(this, node_size); 660 #endif 661 } 662 } 663 664 //------------------------------grow------------------------------------------- 665 // Grow the input array, making space for more edges 666 void Node::grow(uint len) { 667 Arena* arena = Compile::current()->node_arena(); 668 uint new_max = _max; 669 if( new_max == 0 ) { 670 _max = 4; 671 _in = (Node**)arena->Amalloc(4*sizeof(Node*)); 672 Node** to = _in; 673 to[0] = nullptr; 674 to[1] = nullptr; 675 to[2] = nullptr; 676 to[3] = nullptr; 677 return; 678 } 679 new_max = next_power_of_2(len); 680 // Trimming to limit allows a uint8 to handle up to 255 edges. 681 // Previously I was using only powers-of-2 which peaked at 128 edges. 682 //if( new_max >= limit ) new_max = limit-1; 683 _in = (Node**)arena->Arealloc(_in, _max*sizeof(Node*), new_max*sizeof(Node*)); 684 Copy::zero_to_bytes(&_in[_max], (new_max-_max)*sizeof(Node*)); // null all new space 685 _max = new_max; // Record new max length 686 // This assertion makes sure that Node::_max is wide enough to 687 // represent the numerical value of new_max. 688 assert(_max == new_max && _max > len, "int width of _max is too small"); 689 } 690 691 //-----------------------------out_grow---------------------------------------- 692 // Grow the input array, making space for more edges 693 void Node::out_grow( uint len ) { 694 assert(!is_top(), "cannot grow a top node's out array"); 695 Arena* arena = Compile::current()->node_arena(); 696 uint new_max = _outmax; 697 if( new_max == 0 ) { 698 _outmax = 4; 699 _out = (Node **)arena->Amalloc(4*sizeof(Node*)); 700 return; 701 } 702 new_max = next_power_of_2(len); 703 // Trimming to limit allows a uint8 to handle up to 255 edges. 704 // Previously I was using only powers-of-2 which peaked at 128 edges. 705 //if( new_max >= limit ) new_max = limit-1; 706 assert(_out != nullptr && _out != NO_OUT_ARRAY, "out must have sensible value"); 707 _out = (Node**)arena->Arealloc(_out,_outmax*sizeof(Node*),new_max*sizeof(Node*)); 708 //Copy::zero_to_bytes(&_out[_outmax], (new_max-_outmax)*sizeof(Node*)); // null all new space 709 _outmax = new_max; // Record new max length 710 // This assertion makes sure that Node::_max is wide enough to 711 // represent the numerical value of new_max. 712 assert(_outmax == new_max && _outmax > len, "int width of _outmax is too small"); 713 } 714 715 #ifdef ASSERT 716 //------------------------------is_dead---------------------------------------- 717 bool Node::is_dead() const { 718 // Mach and pinch point nodes may look like dead. 719 if( is_top() || is_Mach() || (Opcode() == Op_Node && _outcnt > 0) ) 720 return false; 721 for( uint i = 0; i < _max; i++ ) 722 if( _in[i] != nullptr ) 723 return false; 724 return true; 725 } 726 727 bool Node::is_not_dead(const Node* n) { 728 return n == nullptr || !PhaseIterGVN::is_verify_def_use() || !(n->is_dead()); 729 } 730 731 bool Node::is_reachable_from_root() const { 732 ResourceMark rm; 733 Unique_Node_List wq; 734 wq.push((Node*)this); 735 RootNode* root = Compile::current()->root(); 736 for (uint i = 0; i < wq.size(); i++) { 737 Node* m = wq.at(i); 738 if (m == root) { 739 return true; 740 } 741 for (DUIterator_Fast jmax, j = m->fast_outs(jmax); j < jmax; j++) { 742 Node* u = m->fast_out(j); 743 wq.push(u); 744 } 745 } 746 return false; 747 } 748 #endif 749 750 //------------------------------is_unreachable--------------------------------- 751 bool Node::is_unreachable(PhaseIterGVN &igvn) const { 752 assert(!is_Mach(), "doesn't work with MachNodes"); 753 return outcnt() == 0 || igvn.type(this) == Type::TOP || (in(0) != nullptr && in(0)->is_top()); 754 } 755 756 //------------------------------add_req---------------------------------------- 757 // Add a new required input at the end 758 void Node::add_req( Node *n ) { 759 assert( is_not_dead(n), "can not use dead node"); 760 761 // Look to see if I can move precedence down one without reallocating 762 if( (_cnt >= _max) || (in(_max-1) != nullptr) ) 763 grow( _max+1 ); 764 765 // Find a precedence edge to move 766 if( in(_cnt) != nullptr ) { // Next precedence edge is busy? 767 uint i; 768 for( i=_cnt; i<_max; i++ ) 769 if( in(i) == nullptr ) // Find the null at end of prec edge list 770 break; // There must be one, since we grew the array 771 _in[i] = in(_cnt); // Move prec over, making space for req edge 772 } 773 _in[_cnt++] = n; // Stuff over old prec edge 774 if (n != nullptr) n->add_out((Node *)this); 775 Compile::current()->record_modified_node(this); 776 } 777 778 //---------------------------add_req_batch------------------------------------- 779 // Add a new required input at the end 780 void Node::add_req_batch( Node *n, uint m ) { 781 assert( is_not_dead(n), "can not use dead node"); 782 // check various edge cases 783 if ((int)m <= 1) { 784 assert((int)m >= 0, "oob"); 785 if (m != 0) add_req(n); 786 return; 787 } 788 789 // Look to see if I can move precedence down one without reallocating 790 if( (_cnt+m) > _max || _in[_max-m] ) 791 grow( _max+m ); 792 793 // Find a precedence edge to move 794 if( _in[_cnt] != nullptr ) { // Next precedence edge is busy? 795 uint i; 796 for( i=_cnt; i<_max; i++ ) 797 if( _in[i] == nullptr ) // Find the null at end of prec edge list 798 break; // There must be one, since we grew the array 799 // Slide all the precs over by m positions (assume #prec << m). 800 Copy::conjoint_words_to_higher((HeapWord*)&_in[_cnt], (HeapWord*)&_in[_cnt+m], ((i-_cnt)*sizeof(Node*))); 801 } 802 803 // Stuff over the old prec edges 804 for(uint i=0; i<m; i++ ) { 805 _in[_cnt++] = n; 806 } 807 808 // Insert multiple out edges on the node. 809 if (n != nullptr && !n->is_top()) { 810 for(uint i=0; i<m; i++ ) { 811 n->add_out((Node *)this); 812 } 813 } 814 Compile::current()->record_modified_node(this); 815 } 816 817 //------------------------------del_req---------------------------------------- 818 // Delete the required edge and compact the edge array 819 void Node::del_req( uint idx ) { 820 assert( idx < _cnt, "oob"); 821 assert( !VerifyHashTableKeys || _hash_lock == 0, 822 "remove node from hash table before modifying it"); 823 // First remove corresponding def-use edge 824 Node *n = in(idx); 825 if (n != nullptr) n->del_out((Node *)this); 826 _in[idx] = in(--_cnt); // Compact the array 827 // Avoid spec violation: Gap in prec edges. 828 close_prec_gap_at(_cnt); 829 Compile::current()->record_modified_node(this); 830 } 831 832 //------------------------------del_req_ordered-------------------------------- 833 // Delete the required edge and compact the edge array with preserved order 834 void Node::del_req_ordered( uint idx ) { 835 assert( idx < _cnt, "oob"); 836 assert( !VerifyHashTableKeys || _hash_lock == 0, 837 "remove node from hash table before modifying it"); 838 // First remove corresponding def-use edge 839 Node *n = in(idx); 840 if (n != nullptr) n->del_out((Node *)this); 841 if (idx < --_cnt) { // Not last edge ? 842 Copy::conjoint_words_to_lower((HeapWord*)&_in[idx+1], (HeapWord*)&_in[idx], ((_cnt-idx)*sizeof(Node*))); 843 } 844 // Avoid spec violation: Gap in prec edges. 845 close_prec_gap_at(_cnt); 846 Compile::current()->record_modified_node(this); 847 } 848 849 //------------------------------ins_req---------------------------------------- 850 // Insert a new required input at the end 851 void Node::ins_req( uint idx, Node *n ) { 852 assert( is_not_dead(n), "can not use dead node"); 853 add_req(nullptr); // Make space 854 assert( idx < _max, "Must have allocated enough space"); 855 // Slide over 856 if(_cnt-idx-1 > 0) { 857 Copy::conjoint_words_to_higher((HeapWord*)&_in[idx], (HeapWord*)&_in[idx+1], ((_cnt-idx-1)*sizeof(Node*))); 858 } 859 _in[idx] = n; // Stuff over old required edge 860 if (n != nullptr) n->add_out((Node *)this); // Add reciprocal def-use edge 861 Compile::current()->record_modified_node(this); 862 } 863 864 //-----------------------------find_edge--------------------------------------- 865 int Node::find_edge(Node* n) { 866 for (uint i = 0; i < len(); i++) { 867 if (_in[i] == n) return i; 868 } 869 return -1; 870 } 871 872 //----------------------------replace_edge------------------------------------- 873 int Node::replace_edge(Node* old, Node* neww, PhaseGVN* gvn) { 874 if (old == neww) return 0; // nothing to do 875 uint nrep = 0; 876 for (uint i = 0; i < len(); i++) { 877 if (in(i) == old) { 878 if (i < req()) { 879 if (gvn != nullptr) { 880 set_req_X(i, neww, gvn); 881 } else { 882 set_req(i, neww); 883 } 884 } else { 885 assert(gvn == nullptr || gvn->is_IterGVN() == nullptr, "no support for igvn here"); 886 assert(find_prec_edge(neww) == -1, "spec violation: duplicated prec edge (node %d -> %d)", _idx, neww->_idx); 887 set_prec(i, neww); 888 } 889 nrep++; 890 } 891 } 892 return nrep; 893 } 894 895 /** 896 * Replace input edges in the range pointing to 'old' node. 897 */ 898 int Node::replace_edges_in_range(Node* old, Node* neww, int start, int end, PhaseGVN* gvn) { 899 if (old == neww) return 0; // nothing to do 900 uint nrep = 0; 901 for (int i = start; i < end; i++) { 902 if (in(i) == old) { 903 set_req_X(i, neww, gvn); 904 nrep++; 905 } 906 } 907 return nrep; 908 } 909 910 //-------------------------disconnect_inputs----------------------------------- 911 // null out all inputs to eliminate incoming Def-Use edges. 912 void Node::disconnect_inputs(Compile* C) { 913 // the layout of Node::_in 914 // r: a required input, null is allowed 915 // p: a precedence, null values are all at the end 916 // ----------------------------------- 917 // |r|...|r|p|...|p|null|...|null| 918 // | | 919 // req() len() 920 // ----------------------------------- 921 for (uint i = 0; i < req(); ++i) { 922 if (in(i) != nullptr) { 923 set_req(i, nullptr); 924 } 925 } 926 927 // Remove precedence edges if any exist 928 // Note: Safepoints may have precedence edges, even during parsing 929 for (uint i = len(); i > req(); ) { 930 rm_prec(--i); // no-op if _in[i] is null 931 } 932 933 #ifdef ASSERT 934 // sanity check 935 for (uint i = 0; i < len(); ++i) { 936 assert(_in[i] == nullptr, "disconnect_inputs() failed!"); 937 } 938 #endif 939 940 // Node::destruct requires all out edges be deleted first 941 // debug_only(destruct();) // no reuse benefit expected 942 C->record_dead_node(_idx); 943 } 944 945 //-----------------------------uncast--------------------------------------- 946 // %%% Temporary, until we sort out CheckCastPP vs. CastPP. 947 // Strip away casting. (It is depth-limited.) 948 // Optionally, keep casts with dependencies. 949 Node* Node::uncast(bool keep_deps) const { 950 // Should be inline: 951 //return is_ConstraintCast() ? uncast_helper(this) : (Node*) this; 952 if (is_ConstraintCast()) { 953 return uncast_helper(this, keep_deps); 954 } else { 955 return (Node*) this; 956 } 957 } 958 959 // Find out of current node that matches opcode. 960 Node* Node::find_out_with(int opcode) { 961 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 962 Node* use = fast_out(i); 963 if (use->Opcode() == opcode) { 964 return use; 965 } 966 } 967 return nullptr; 968 } 969 970 // Return true if the current node has an out that matches opcode. 971 bool Node::has_out_with(int opcode) { 972 return (find_out_with(opcode) != nullptr); 973 } 974 975 // Return true if the current node has an out that matches any of the opcodes. 976 bool Node::has_out_with(int opcode1, int opcode2, int opcode3, int opcode4) { 977 for (DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++) { 978 int opcode = fast_out(i)->Opcode(); 979 if (opcode == opcode1 || opcode == opcode2 || opcode == opcode3 || opcode == opcode4) { 980 return true; 981 } 982 } 983 return false; 984 } 985 986 987 //---------------------------uncast_helper------------------------------------- 988 Node* Node::uncast_helper(const Node* p, bool keep_deps) { 989 #ifdef ASSERT 990 uint depth_count = 0; 991 const Node* orig_p = p; 992 #endif 993 994 while (true) { 995 #ifdef ASSERT 996 if (depth_count >= K) { 997 orig_p->dump(4); 998 if (p != orig_p) 999 p->dump(1); 1000 } 1001 assert(depth_count++ < K, "infinite loop in Node::uncast_helper"); 1002 #endif 1003 if (p == nullptr || p->req() != 2) { 1004 break; 1005 } else if (p->is_ConstraintCast()) { 1006 if (keep_deps && p->as_ConstraintCast()->carry_dependency()) { 1007 break; // stop at casts with dependencies 1008 } 1009 p = p->in(1); 1010 } else { 1011 break; 1012 } 1013 } 1014 return (Node*) p; 1015 } 1016 1017 //------------------------------add_prec--------------------------------------- 1018 // Add a new precedence input. Precedence inputs are unordered, with 1019 // duplicates removed and nulls packed down at the end. 1020 void Node::add_prec( Node *n ) { 1021 assert( is_not_dead(n), "can not use dead node"); 1022 1023 // Check for null at end 1024 if( _cnt >= _max || in(_max-1) ) 1025 grow( _max+1 ); 1026 1027 // Find a precedence edge to move 1028 uint i = _cnt; 1029 while( in(i) != nullptr ) { 1030 if (in(i) == n) return; // Avoid spec violation: duplicated prec edge. 1031 i++; 1032 } 1033 _in[i] = n; // Stuff prec edge over null 1034 if ( n != nullptr) n->add_out((Node *)this); // Add mirror edge 1035 1036 #ifdef ASSERT 1037 while ((++i)<_max) { assert(_in[i] == nullptr, "spec violation: Gap in prec edges (node %d)", _idx); } 1038 #endif 1039 Compile::current()->record_modified_node(this); 1040 } 1041 1042 //------------------------------rm_prec---------------------------------------- 1043 // Remove a precedence input. Precedence inputs are unordered, with 1044 // duplicates removed and nulls packed down at the end. 1045 void Node::rm_prec( uint j ) { 1046 assert(j < _max, "oob: i=%d, _max=%d", j, _max); 1047 assert(j >= _cnt, "not a precedence edge"); 1048 if (_in[j] == nullptr) return; // Avoid spec violation: Gap in prec edges. 1049 _in[j]->del_out((Node *)this); 1050 close_prec_gap_at(j); 1051 Compile::current()->record_modified_node(this); 1052 } 1053 1054 //------------------------------size_of---------------------------------------- 1055 uint Node::size_of() const { return sizeof(*this); } 1056 1057 //------------------------------ideal_reg-------------------------------------- 1058 uint Node::ideal_reg() const { return 0; } 1059 1060 //------------------------------jvms------------------------------------------- 1061 JVMState* Node::jvms() const { return nullptr; } 1062 1063 #ifdef ASSERT 1064 //------------------------------jvms------------------------------------------- 1065 bool Node::verify_jvms(const JVMState* using_jvms) const { 1066 for (JVMState* jvms = this->jvms(); jvms != nullptr; jvms = jvms->caller()) { 1067 if (jvms == using_jvms) return true; 1068 } 1069 return false; 1070 } 1071 1072 //------------------------------init_NodeProperty------------------------------ 1073 void Node::init_NodeProperty() { 1074 assert(_max_classes <= max_juint, "too many NodeProperty classes"); 1075 assert(max_flags() <= max_juint, "too many NodeProperty flags"); 1076 } 1077 1078 //-----------------------------max_flags--------------------------------------- 1079 juint Node::max_flags() { 1080 return (PD::_last_flag << 1) - 1; // allow flags combination 1081 } 1082 #endif 1083 1084 //------------------------------format----------------------------------------- 1085 // Print as assembly 1086 void Node::format( PhaseRegAlloc *, outputStream *st ) const {} 1087 //------------------------------emit------------------------------------------- 1088 // Emit bytes starting at parameter 'ptr'. 1089 void Node::emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const {} 1090 //------------------------------size------------------------------------------- 1091 // Size of instruction in bytes 1092 uint Node::size(PhaseRegAlloc *ra_) const { return 0; } 1093 1094 //------------------------------CFG Construction------------------------------- 1095 // Nodes that end basic blocks, e.g. IfTrue/IfFalse, JumpProjNode, Root, 1096 // Goto and Return. 1097 const Node *Node::is_block_proj() const { return 0; } 1098 1099 // Minimum guaranteed type 1100 const Type *Node::bottom_type() const { return Type::BOTTOM; } 1101 1102 1103 //------------------------------raise_bottom_type------------------------------ 1104 // Get the worst-case Type output for this Node. 1105 void Node::raise_bottom_type(const Type* new_type) { 1106 if (is_Type()) { 1107 TypeNode *n = this->as_Type(); 1108 if (VerifyAliases) { 1109 assert(new_type->higher_equal_speculative(n->type()), "new type must refine old type"); 1110 } 1111 n->set_type(new_type); 1112 } else if (is_Load()) { 1113 LoadNode *n = this->as_Load(); 1114 if (VerifyAliases) { 1115 assert(new_type->higher_equal_speculative(n->type()), "new type must refine old type"); 1116 } 1117 n->set_type(new_type); 1118 } 1119 } 1120 1121 //------------------------------Identity--------------------------------------- 1122 // Return a node that the given node is equivalent to. 1123 Node* Node::Identity(PhaseGVN* phase) { 1124 return this; // Default to no identities 1125 } 1126 1127 //------------------------------Value------------------------------------------ 1128 // Compute a new Type for a node using the Type of the inputs. 1129 const Type* Node::Value(PhaseGVN* phase) const { 1130 return bottom_type(); // Default to worst-case Type 1131 } 1132 1133 //------------------------------Ideal------------------------------------------ 1134 // 1135 // 'Idealize' the graph rooted at this Node. 1136 // 1137 // In order to be efficient and flexible there are some subtle invariants 1138 // these Ideal calls need to hold. Running with '-XX:VerifyIterativeGVN=1' checks 1139 // these invariants, although its too slow to have on by default. If you are 1140 // hacking an Ideal call, be sure to test with '-XX:VerifyIterativeGVN=1' 1141 // 1142 // The Ideal call almost arbitrarily reshape the graph rooted at the 'this' 1143 // pointer. If ANY change is made, it must return the root of the reshaped 1144 // graph - even if the root is the same Node. Example: swapping the inputs 1145 // to an AddINode gives the same answer and same root, but you still have to 1146 // return the 'this' pointer instead of null. 1147 // 1148 // You cannot return an OLD Node, except for the 'this' pointer. Use the 1149 // Identity call to return an old Node; basically if Identity can find 1150 // another Node have the Ideal call make no change and return null. 1151 // Example: AddINode::Ideal must check for add of zero; in this case it 1152 // returns null instead of doing any graph reshaping. 1153 // 1154 // You cannot modify any old Nodes except for the 'this' pointer. Due to 1155 // sharing there may be other users of the old Nodes relying on their current 1156 // semantics. Modifying them will break the other users. 1157 // Example: when reshape "(X+3)+4" into "X+7" you must leave the Node for 1158 // "X+3" unchanged in case it is shared. 1159 // 1160 // If you modify the 'this' pointer's inputs, you should use 1161 // 'set_req'. If you are making a new Node (either as the new root or 1162 // some new internal piece) you may use 'init_req' to set the initial 1163 // value. You can make a new Node with either 'new' or 'clone'. In 1164 // either case, def-use info is correctly maintained. 1165 // 1166 // Example: reshape "(X+3)+4" into "X+7": 1167 // set_req(1, in(1)->in(1)); 1168 // set_req(2, phase->intcon(7)); 1169 // return this; 1170 // Example: reshape "X*4" into "X<<2" 1171 // return new LShiftINode(in(1), phase->intcon(2)); 1172 // 1173 // You must call 'phase->transform(X)' on any new Nodes X you make, except 1174 // for the returned root node. Example: reshape "X*31" with "(X<<5)-X". 1175 // Node *shift=phase->transform(new LShiftINode(in(1),phase->intcon(5))); 1176 // return new AddINode(shift, in(1)); 1177 // 1178 // When making a Node for a constant use 'phase->makecon' or 'phase->intcon'. 1179 // These forms are faster than 'phase->transform(new ConNode())' and Do 1180 // The Right Thing with def-use info. 1181 // 1182 // You cannot bury the 'this' Node inside of a graph reshape. If the reshaped 1183 // graph uses the 'this' Node it must be the root. If you want a Node with 1184 // the same Opcode as the 'this' pointer use 'clone'. 1185 // 1186 Node *Node::Ideal(PhaseGVN *phase, bool can_reshape) { 1187 return nullptr; // Default to being Ideal already 1188 } 1189 1190 // Some nodes have specific Ideal subgraph transformations only if they are 1191 // unique users of specific nodes. Such nodes should be put on IGVN worklist 1192 // for the transformations to happen. 1193 bool Node::has_special_unique_user() const { 1194 assert(outcnt() == 1, "match only for unique out"); 1195 Node* n = unique_out(); 1196 int op = Opcode(); 1197 if (this->is_Store()) { 1198 // Condition for back-to-back stores folding. 1199 return n->Opcode() == op && n->in(MemNode::Memory) == this; 1200 } else if (this->is_Load() || this->is_DecodeN() || this->is_Phi()) { 1201 // Condition for removing an unused LoadNode or DecodeNNode from the MemBarAcquire precedence input 1202 return n->Opcode() == Op_MemBarAcquire; 1203 } else if (op == Op_AddL) { 1204 // Condition for convL2I(addL(x,y)) ==> addI(convL2I(x),convL2I(y)) 1205 return n->Opcode() == Op_ConvL2I && n->in(1) == this; 1206 } else if (op == Op_SubI || op == Op_SubL) { 1207 // Condition for subI(x,subI(y,z)) ==> subI(addI(x,z),y) 1208 return n->Opcode() == op && n->in(2) == this; 1209 } else if (is_If() && (n->is_IfFalse() || n->is_IfTrue())) { 1210 // See IfProjNode::Identity() 1211 return true; 1212 } else if ((is_IfFalse() || is_IfTrue()) && n->is_If()) { 1213 // See IfNode::fold_compares 1214 return true; 1215 } else { 1216 return false; 1217 } 1218 }; 1219 1220 //--------------------------find_exact_control--------------------------------- 1221 // Skip Proj and CatchProj nodes chains. Check for Null and Top. 1222 Node* Node::find_exact_control(Node* ctrl) { 1223 if (ctrl == nullptr && this->is_Region()) 1224 ctrl = this->as_Region()->is_copy(); 1225 1226 if (ctrl != nullptr && ctrl->is_CatchProj()) { 1227 if (ctrl->as_CatchProj()->_con == CatchProjNode::fall_through_index) 1228 ctrl = ctrl->in(0); 1229 if (ctrl != nullptr && !ctrl->is_top()) 1230 ctrl = ctrl->in(0); 1231 } 1232 1233 if (ctrl != nullptr && ctrl->is_Proj()) 1234 ctrl = ctrl->in(0); 1235 1236 return ctrl; 1237 } 1238 1239 //--------------------------dominates------------------------------------------ 1240 // Helper function for MemNode::all_controls_dominate(). 1241 // Check if 'this' control node dominates or equal to 'sub' control node. 1242 // We already know that if any path back to Root or Start reaches 'this', 1243 // then all paths so, so this is a simple search for one example, 1244 // not an exhaustive search for a counterexample. 1245 bool Node::dominates(Node* sub, Node_List &nlist) { 1246 assert(this->is_CFG(), "expecting control"); 1247 assert(sub != nullptr && sub->is_CFG(), "expecting control"); 1248 1249 // detect dead cycle without regions 1250 int iterations_without_region_limit = DominatorSearchLimit; 1251 1252 Node* orig_sub = sub; 1253 Node* dom = this; 1254 bool met_dom = false; 1255 nlist.clear(); 1256 1257 // Walk 'sub' backward up the chain to 'dom', watching for regions. 1258 // After seeing 'dom', continue up to Root or Start. 1259 // If we hit a region (backward split point), it may be a loop head. 1260 // Keep going through one of the region's inputs. If we reach the 1261 // same region again, go through a different input. Eventually we 1262 // will either exit through the loop head, or give up. 1263 // (If we get confused, break out and return a conservative 'false'.) 1264 while (sub != nullptr) { 1265 if (sub->is_top()) break; // Conservative answer for dead code. 1266 if (sub == dom) { 1267 if (nlist.size() == 0) { 1268 // No Region nodes except loops were visited before and the EntryControl 1269 // path was taken for loops: it did not walk in a cycle. 1270 return true; 1271 } else if (met_dom) { 1272 break; // already met before: walk in a cycle 1273 } else { 1274 // Region nodes were visited. Continue walk up to Start or Root 1275 // to make sure that it did not walk in a cycle. 1276 met_dom = true; // first time meet 1277 iterations_without_region_limit = DominatorSearchLimit; // Reset 1278 } 1279 } 1280 if (sub->is_Start() || sub->is_Root()) { 1281 // Success if we met 'dom' along a path to Start or Root. 1282 // We assume there are no alternative paths that avoid 'dom'. 1283 // (This assumption is up to the caller to ensure!) 1284 return met_dom; 1285 } 1286 Node* up = sub->in(0); 1287 // Normalize simple pass-through regions and projections: 1288 up = sub->find_exact_control(up); 1289 // If sub == up, we found a self-loop. Try to push past it. 1290 if (sub == up && sub->is_Loop()) { 1291 // Take loop entry path on the way up to 'dom'. 1292 up = sub->in(1); // in(LoopNode::EntryControl); 1293 } else if (sub == up && sub->is_Region() && sub->req() == 2) { 1294 // Take in(1) path on the way up to 'dom' for regions with only one input 1295 up = sub->in(1); 1296 } else if (sub == up && sub->is_Region()) { 1297 // Try both paths for Regions with 2 input paths (it may be a loop head). 1298 // It could give conservative 'false' answer without information 1299 // which region's input is the entry path. 1300 iterations_without_region_limit = DominatorSearchLimit; // Reset 1301 1302 bool region_was_visited_before = false; 1303 // Was this Region node visited before? 1304 // If so, we have reached it because we accidentally took a 1305 // loop-back edge from 'sub' back into the body of the loop, 1306 // and worked our way up again to the loop header 'sub'. 1307 // So, take the first unexplored path on the way up to 'dom'. 1308 for (int j = nlist.size() - 1; j >= 0; j--) { 1309 intptr_t ni = (intptr_t)nlist.at(j); 1310 Node* visited = (Node*)(ni & ~1); 1311 bool visited_twice_already = ((ni & 1) != 0); 1312 if (visited == sub) { 1313 if (visited_twice_already) { 1314 // Visited 2 paths, but still stuck in loop body. Give up. 1315 return false; 1316 } 1317 // The Region node was visited before only once. 1318 // (We will repush with the low bit set, below.) 1319 nlist.remove(j); 1320 // We will find a new edge and re-insert. 1321 region_was_visited_before = true; 1322 break; 1323 } 1324 } 1325 1326 // Find an incoming edge which has not been seen yet; walk through it. 1327 assert(up == sub, ""); 1328 uint skip = region_was_visited_before ? 1 : 0; 1329 for (uint i = 1; i < sub->req(); i++) { 1330 Node* in = sub->in(i); 1331 if (in != nullptr && !in->is_top() && in != sub) { 1332 if (skip == 0) { 1333 up = in; 1334 break; 1335 } 1336 --skip; // skip this nontrivial input 1337 } 1338 } 1339 1340 // Set 0 bit to indicate that both paths were taken. 1341 nlist.push((Node*)((intptr_t)sub + (region_was_visited_before ? 1 : 0))); 1342 } 1343 1344 if (up == sub) { 1345 break; // some kind of tight cycle 1346 } 1347 if (up == orig_sub && met_dom) { 1348 // returned back after visiting 'dom' 1349 break; // some kind of cycle 1350 } 1351 if (--iterations_without_region_limit < 0) { 1352 break; // dead cycle 1353 } 1354 sub = up; 1355 } 1356 1357 // Did not meet Root or Start node in pred. chain. 1358 // Conservative answer for dead code. 1359 return false; 1360 } 1361 1362 //------------------------------remove_dead_region----------------------------- 1363 // This control node is dead. Follow the subgraph below it making everything 1364 // using it dead as well. This will happen normally via the usual IterGVN 1365 // worklist but this call is more efficient. Do not update use-def info 1366 // inside the dead region, just at the borders. 1367 static void kill_dead_code( Node *dead, PhaseIterGVN *igvn ) { 1368 // Con's are a popular node to re-hit in the hash table again. 1369 if( dead->is_Con() ) return; 1370 1371 ResourceMark rm; 1372 Node_List nstack; 1373 VectorSet dead_set; // notify uses only once 1374 1375 Node *top = igvn->C->top(); 1376 nstack.push(dead); 1377 bool has_irreducible_loop = igvn->C->has_irreducible_loop(); 1378 1379 while (nstack.size() > 0) { 1380 dead = nstack.pop(); 1381 if (!dead_set.test_set(dead->_idx)) { 1382 // If dead has any live uses, those are now still attached. Notify them before we lose them. 1383 igvn->add_users_to_worklist(dead); 1384 } 1385 if (dead->Opcode() == Op_SafePoint) { 1386 dead->as_SafePoint()->disconnect_from_root(igvn); 1387 } 1388 if (dead->outcnt() > 0) { 1389 // Keep dead node on stack until all uses are processed. 1390 nstack.push(dead); 1391 // For all Users of the Dead... ;-) 1392 for (DUIterator_Last kmin, k = dead->last_outs(kmin); k >= kmin; ) { 1393 Node* use = dead->last_out(k); 1394 igvn->hash_delete(use); // Yank from hash table prior to mod 1395 if (use->in(0) == dead) { // Found another dead node 1396 assert (!use->is_Con(), "Control for Con node should be Root node."); 1397 use->set_req(0, top); // Cut dead edge to prevent processing 1398 nstack.push(use); // the dead node again. 1399 } else if (!has_irreducible_loop && // Backedge could be alive in irreducible loop 1400 use->is_Loop() && !use->is_Root() && // Don't kill Root (RootNode extends LoopNode) 1401 use->in(LoopNode::EntryControl) == dead) { // Dead loop if its entry is dead 1402 use->set_req(LoopNode::EntryControl, top); // Cut dead edge to prevent processing 1403 use->set_req(0, top); // Cut self edge 1404 nstack.push(use); 1405 } else { // Else found a not-dead user 1406 // Dead if all inputs are top or null 1407 bool dead_use = !use->is_Root(); // Keep empty graph alive 1408 for (uint j = 1; j < use->req(); j++) { 1409 Node* in = use->in(j); 1410 if (in == dead) { // Turn all dead inputs into TOP 1411 use->set_req(j, top); 1412 } else if (in != nullptr && !in->is_top()) { 1413 dead_use = false; 1414 } 1415 } 1416 if (dead_use) { 1417 if (use->is_Region()) { 1418 use->set_req(0, top); // Cut self edge 1419 } 1420 nstack.push(use); 1421 } else { 1422 igvn->_worklist.push(use); 1423 } 1424 } 1425 // Refresh the iterator, since any number of kills might have happened. 1426 k = dead->last_outs(kmin); 1427 } 1428 } else { // (dead->outcnt() == 0) 1429 // Done with outputs. 1430 igvn->hash_delete(dead); 1431 igvn->_worklist.remove(dead); 1432 igvn->set_type(dead, Type::TOP); 1433 // Kill all inputs to the dead guy 1434 for (uint i=0; i < dead->req(); i++) { 1435 Node *n = dead->in(i); // Get input to dead guy 1436 if (n != nullptr && !n->is_top()) { // Input is valid? 1437 dead->set_req(i, top); // Smash input away 1438 if (n->outcnt() == 0) { // Input also goes dead? 1439 if (!n->is_Con()) 1440 nstack.push(n); // Clear it out as well 1441 } else if (n->outcnt() == 1 && 1442 n->has_special_unique_user()) { 1443 igvn->add_users_to_worklist( n ); 1444 } else if (n->outcnt() <= 2 && n->is_Store()) { 1445 // Push store's uses on worklist to enable folding optimization for 1446 // store/store and store/load to the same address. 1447 // The restriction (outcnt() <= 2) is the same as in set_req_X() 1448 // and remove_globally_dead_node(). 1449 igvn->add_users_to_worklist( n ); 1450 } else { 1451 BarrierSet::barrier_set()->barrier_set_c2()->enqueue_useful_gc_barrier(igvn, n); 1452 } 1453 } 1454 } 1455 igvn->C->remove_useless_node(dead); 1456 } // (dead->outcnt() == 0) 1457 } // while (nstack.size() > 0) for outputs 1458 return; 1459 } 1460 1461 //------------------------------remove_dead_region----------------------------- 1462 bool Node::remove_dead_region(PhaseGVN *phase, bool can_reshape) { 1463 Node *n = in(0); 1464 if( !n ) return false; 1465 // Lost control into this guy? I.e., it became unreachable? 1466 // Aggressively kill all unreachable code. 1467 if (can_reshape && n->is_top()) { 1468 kill_dead_code(this, phase->is_IterGVN()); 1469 return false; // Node is dead. 1470 } 1471 1472 if( n->is_Region() && n->as_Region()->is_copy() ) { 1473 Node *m = n->nonnull_req(); 1474 set_req(0, m); 1475 return true; 1476 } 1477 return false; 1478 } 1479 1480 //------------------------------hash------------------------------------------- 1481 // Hash function over Nodes. 1482 uint Node::hash() const { 1483 uint sum = 0; 1484 for( uint i=0; i<_cnt; i++ ) // Add in all inputs 1485 sum = (sum<<1)-(uintptr_t)in(i); // Ignore embedded nulls 1486 return (sum>>2) + _cnt + Opcode(); 1487 } 1488 1489 //------------------------------cmp-------------------------------------------- 1490 // Compare special parts of simple Nodes 1491 bool Node::cmp( const Node &n ) const { 1492 return true; // Must be same 1493 } 1494 1495 //------------------------------rematerialize----------------------------------- 1496 // Should we clone rather than spill this instruction? 1497 bool Node::rematerialize() const { 1498 if ( is_Mach() ) 1499 return this->as_Mach()->rematerialize(); 1500 else 1501 return (_flags & Flag_rematerialize) != 0; 1502 } 1503 1504 //------------------------------needs_anti_dependence_check--------------------- 1505 // Nodes which use memory without consuming it, hence need antidependences. 1506 bool Node::needs_anti_dependence_check() const { 1507 if (req() < 2 || (_flags & Flag_needs_anti_dependence_check) == 0) { 1508 return false; 1509 } 1510 return in(1)->bottom_type()->has_memory(); 1511 } 1512 1513 // Get an integer constant from a ConNode (or CastIINode). 1514 // Return a default value if there is no apparent constant here. 1515 const TypeInt* Node::find_int_type() const { 1516 if (this->is_Type()) { 1517 return this->as_Type()->type()->isa_int(); 1518 } else if (this->is_Con()) { 1519 assert(is_Mach(), "should be ConNode(TypeNode) or else a MachNode"); 1520 return this->bottom_type()->isa_int(); 1521 } 1522 return nullptr; 1523 } 1524 1525 const TypeInteger* Node::find_integer_type(BasicType bt) const { 1526 if (this->is_Type()) { 1527 return this->as_Type()->type()->isa_integer(bt); 1528 } else if (this->is_Con()) { 1529 assert(is_Mach(), "should be ConNode(TypeNode) or else a MachNode"); 1530 return this->bottom_type()->isa_integer(bt); 1531 } 1532 return nullptr; 1533 } 1534 1535 // Get a pointer constant from a ConstNode. 1536 // Returns the constant if it is a pointer ConstNode 1537 intptr_t Node::get_ptr() const { 1538 assert( Opcode() == Op_ConP, "" ); 1539 return ((ConPNode*)this)->type()->is_ptr()->get_con(); 1540 } 1541 1542 // Get a narrow oop constant from a ConNNode. 1543 intptr_t Node::get_narrowcon() const { 1544 assert( Opcode() == Op_ConN, "" ); 1545 return ((ConNNode*)this)->type()->is_narrowoop()->get_con(); 1546 } 1547 1548 // Get a long constant from a ConNode. 1549 // Return a default value if there is no apparent constant here. 1550 const TypeLong* Node::find_long_type() const { 1551 if (this->is_Type()) { 1552 return this->as_Type()->type()->isa_long(); 1553 } else if (this->is_Con()) { 1554 assert(is_Mach(), "should be ConNode(TypeNode) or else a MachNode"); 1555 return this->bottom_type()->isa_long(); 1556 } 1557 return nullptr; 1558 } 1559 1560 1561 /** 1562 * Return a ptr type for nodes which should have it. 1563 */ 1564 const TypePtr* Node::get_ptr_type() const { 1565 const TypePtr* tp = this->bottom_type()->make_ptr(); 1566 #ifdef ASSERT 1567 if (tp == nullptr) { 1568 this->dump(1); 1569 assert((tp != nullptr), "unexpected node type"); 1570 } 1571 #endif 1572 return tp; 1573 } 1574 1575 // Get a double constant from a ConstNode. 1576 // Returns the constant if it is a double ConstNode 1577 jdouble Node::getd() const { 1578 assert( Opcode() == Op_ConD, "" ); 1579 return ((ConDNode*)this)->type()->is_double_constant()->getd(); 1580 } 1581 1582 // Get a float constant from a ConstNode. 1583 // Returns the constant if it is a float ConstNode 1584 jfloat Node::getf() const { 1585 assert( Opcode() == Op_ConF, "" ); 1586 return ((ConFNode*)this)->type()->is_float_constant()->getf(); 1587 } 1588 1589 #ifndef PRODUCT 1590 1591 // Call this from debugger: 1592 Node* old_root() { 1593 Matcher* matcher = Compile::current()->matcher(); 1594 if (matcher != nullptr) { 1595 Node* new_root = Compile::current()->root(); 1596 Node* old_root = matcher->find_old_node(new_root); 1597 if (old_root != nullptr) { 1598 return old_root; 1599 } 1600 } 1601 tty->print("old_root: not found.\n"); 1602 return nullptr; 1603 } 1604 1605 // BFS traverse all reachable nodes from start, call callback on them 1606 template <typename Callback> 1607 void visit_nodes(Node* start, Callback callback, bool traverse_output, bool only_ctrl) { 1608 Unique_Mixed_Node_List worklist; 1609 worklist.add(start); 1610 for (uint i = 0; i < worklist.size(); i++) { 1611 Node* n = worklist[i]; 1612 callback(n); 1613 for (uint i = 0; i < n->len(); i++) { 1614 if (!only_ctrl || n->is_Region() || (n->Opcode() == Op_Root) || (i == TypeFunc::Control)) { 1615 // If only_ctrl is set: Add regions, the root node, or control inputs only 1616 worklist.add(n->in(i)); 1617 } 1618 } 1619 if (traverse_output && !only_ctrl) { 1620 for (uint i = 0; i < n->outcnt(); i++) { 1621 worklist.add(n->raw_out(i)); 1622 } 1623 } 1624 } 1625 } 1626 1627 // BFS traverse from start, return node with idx 1628 Node* find_node_by_idx(Node* start, uint idx, bool traverse_output, bool only_ctrl) { 1629 ResourceMark rm; 1630 Node* result = nullptr; 1631 auto callback = [&] (Node* n) { 1632 if (n->_idx == idx) { 1633 if (result != nullptr) { 1634 tty->print("find_node_by_idx: " INTPTR_FORMAT " and " INTPTR_FORMAT " both have idx==%d\n", 1635 (uintptr_t)result, (uintptr_t)n, idx); 1636 } 1637 result = n; 1638 } 1639 }; 1640 visit_nodes(start, callback, traverse_output, only_ctrl); 1641 return result; 1642 } 1643 1644 int node_idx_cmp(const Node** n1, const Node** n2) { 1645 return (*n1)->_idx - (*n2)->_idx; 1646 } 1647 1648 void find_nodes_by_name(Node* start, const char* name) { 1649 ResourceMark rm; 1650 GrowableArray<const Node*> ns; 1651 auto callback = [&] (const Node* n) { 1652 if (StringUtils::is_star_match(name, n->Name())) { 1653 ns.push(n); 1654 } 1655 }; 1656 visit_nodes(start, callback, true, false); 1657 ns.sort(node_idx_cmp); 1658 for (int i = 0; i < ns.length(); i++) { 1659 ns.at(i)->dump(); 1660 } 1661 } 1662 1663 void find_nodes_by_dump(Node* start, const char* pattern) { 1664 ResourceMark rm; 1665 GrowableArray<const Node*> ns; 1666 auto callback = [&] (const Node* n) { 1667 stringStream stream; 1668 n->dump("", false, &stream); 1669 if (StringUtils::is_star_match(pattern, stream.base())) { 1670 ns.push(n); 1671 } 1672 }; 1673 visit_nodes(start, callback, true, false); 1674 ns.sort(node_idx_cmp); 1675 for (int i = 0; i < ns.length(); i++) { 1676 ns.at(i)->dump(); 1677 } 1678 } 1679 1680 // call from debugger: find node with name pattern in new/current graph 1681 // name can contain "*" in match pattern to match any characters 1682 // the matching is case insensitive 1683 void find_nodes_by_name(const char* name) { 1684 Node* root = Compile::current()->root(); 1685 find_nodes_by_name(root, name); 1686 } 1687 1688 // call from debugger: find node with name pattern in old graph 1689 // name can contain "*" in match pattern to match any characters 1690 // the matching is case insensitive 1691 void find_old_nodes_by_name(const char* name) { 1692 Node* root = old_root(); 1693 find_nodes_by_name(root, name); 1694 } 1695 1696 // call from debugger: find node with dump pattern in new/current graph 1697 // can contain "*" in match pattern to match any characters 1698 // the matching is case insensitive 1699 void find_nodes_by_dump(const char* pattern) { 1700 Node* root = Compile::current()->root(); 1701 find_nodes_by_dump(root, pattern); 1702 } 1703 1704 // call from debugger: find node with name pattern in old graph 1705 // can contain "*" in match pattern to match any characters 1706 // the matching is case insensitive 1707 void find_old_nodes_by_dump(const char* pattern) { 1708 Node* root = old_root(); 1709 find_nodes_by_dump(root, pattern); 1710 } 1711 1712 // Call this from debugger, search in same graph as n: 1713 Node* find_node(Node* n, const int idx) { 1714 return n->find(idx); 1715 } 1716 1717 // Call this from debugger, search in new nodes: 1718 Node* find_node(const int idx) { 1719 return Compile::current()->root()->find(idx); 1720 } 1721 1722 // Call this from debugger, search in old nodes: 1723 Node* find_old_node(const int idx) { 1724 Node* root = old_root(); 1725 return (root == nullptr) ? nullptr : root->find(idx); 1726 } 1727 1728 // Call this from debugger, search in same graph as n: 1729 Node* find_ctrl(Node* n, const int idx) { 1730 return n->find_ctrl(idx); 1731 } 1732 1733 // Call this from debugger, search in new nodes: 1734 Node* find_ctrl(const int idx) { 1735 return Compile::current()->root()->find_ctrl(idx); 1736 } 1737 1738 // Call this from debugger, search in old nodes: 1739 Node* find_old_ctrl(const int idx) { 1740 Node* root = old_root(); 1741 return (root == nullptr) ? nullptr : root->find_ctrl(idx); 1742 } 1743 1744 //------------------------------find_ctrl-------------------------------------- 1745 // Find an ancestor to this node in the control history with given _idx 1746 Node* Node::find_ctrl(int idx) { 1747 return find(idx, true); 1748 } 1749 1750 //------------------------------find------------------------------------------- 1751 // Tries to find the node with the index |idx| starting from this node. If idx is negative, 1752 // the search also includes forward (out) edges. Returns null if not found. 1753 // If only_ctrl is set, the search will only be done on control nodes. Returns null if 1754 // not found or if the node to be found is not a control node (search will not find it). 1755 Node* Node::find(const int idx, bool only_ctrl) { 1756 ResourceMark rm; 1757 return find_node_by_idx(this, abs(idx), (idx < 0), only_ctrl); 1758 } 1759 1760 class PrintBFS { 1761 public: 1762 PrintBFS(const Node* start, const int max_distance, const Node* target, const char* options, outputStream* st) 1763 : _start(start), _max_distance(max_distance), _target(target), _options(options), _output(st), 1764 _dcc(this), _info_uid(cmpkey, hashkey) {} 1765 1766 void run(); 1767 private: 1768 // pipeline steps 1769 bool configure(); 1770 void collect(); 1771 void select(); 1772 void select_all(); 1773 void select_all_paths(); 1774 void select_shortest_path(); 1775 void sort(); 1776 void print(); 1777 1778 // inputs 1779 const Node* _start; 1780 const int _max_distance; 1781 const Node* _target; 1782 const char* _options; 1783 outputStream* _output; 1784 1785 // options 1786 bool _traverse_inputs = false; 1787 bool _traverse_outputs = false; 1788 struct Filter { 1789 bool _control = false; 1790 bool _memory = false; 1791 bool _data = false; 1792 bool _mixed = false; 1793 bool _other = false; 1794 bool is_empty() const { 1795 return !(_control || _memory || _data || _mixed || _other); 1796 } 1797 void set_all() { 1798 _control = true; 1799 _memory = true; 1800 _data = true; 1801 _mixed = true; 1802 _other = true; 1803 } 1804 // Check if the filter accepts the node. Go by the type categories, but also all CFG nodes 1805 // are considered to have control. 1806 bool accepts(const Node* n) { 1807 const Type* t = n->bottom_type(); 1808 return ( _data && t->has_category(Type::Category::Data) ) || 1809 ( _memory && t->has_category(Type::Category::Memory) ) || 1810 ( _mixed && t->has_category(Type::Category::Mixed) ) || 1811 ( _control && (t->has_category(Type::Category::Control) || n->is_CFG()) ) || 1812 ( _other && t->has_category(Type::Category::Other) ); 1813 } 1814 }; 1815 Filter _filter_visit; 1816 Filter _filter_boundary; 1817 bool _sort_idx = false; 1818 bool _all_paths = false; 1819 bool _use_color = false; 1820 bool _print_blocks = false; 1821 bool _print_old = false; 1822 bool _dump_only = false; 1823 void print_options_help(bool print_examples); 1824 bool parse_options(); 1825 1826 public: 1827 class DumpConfigColored : public Node::DumpConfig { 1828 public: 1829 DumpConfigColored(PrintBFS* bfs) : _bfs(bfs) {}; 1830 virtual void pre_dump(outputStream* st, const Node* n); 1831 virtual void post_dump(outputStream* st); 1832 private: 1833 PrintBFS* _bfs; 1834 }; 1835 private: 1836 DumpConfigColored _dcc; 1837 1838 // node info 1839 static Node* old_node(const Node* n); // mach node -> prior IR node 1840 void print_node_idx(const Node* n); 1841 void print_block_id(const Block* b); 1842 void print_node_block(const Node* n); // _pre_order, head idx, _idom, _dom_depth 1843 1844 // traversal data structures 1845 GrowableArray<const Node*> _worklist; // BFS queue 1846 void maybe_traverse(const Node* src, const Node* dst); 1847 1848 // node info annotation 1849 class Info { 1850 public: 1851 Info() : Info(nullptr, 0) {}; 1852 Info(const Node* node, int distance) 1853 : _node(node), _distance_from_start(distance) {}; 1854 const Node* node() const { return _node; }; 1855 int distance() const { return _distance_from_start; }; 1856 int distance_from_target() const { return _distance_from_target; } 1857 void set_distance_from_target(int d) { _distance_from_target = d; } 1858 GrowableArray<const Node*> edge_bwd; // pointing toward _start 1859 bool is_marked() const { return _mark; } // marked to keep during select 1860 void set_mark() { _mark = true; } 1861 private: 1862 const Node* _node; 1863 int _distance_from_start; // distance from _start 1864 int _distance_from_target = 0; // distance from _target if _all_paths 1865 bool _mark = false; 1866 }; 1867 Dict _info_uid; // Node -> uid 1868 GrowableArray<Info> _info; // uid -> info 1869 1870 Info* find_info(const Node* n) { 1871 size_t uid = (size_t)_info_uid[n]; 1872 if (uid == 0) { 1873 return nullptr; 1874 } 1875 return &_info.at((int)uid); 1876 } 1877 1878 void make_info(const Node* node, const int distance) { 1879 assert(find_info(node) == nullptr, "node does not yet have info"); 1880 size_t uid = _info.length() + 1; 1881 _info_uid.Insert((void*)node, (void*)uid); 1882 _info.at_put_grow((int)uid, Info(node, distance)); 1883 assert(find_info(node)->node() == node, "stored correct node"); 1884 }; 1885 1886 // filled by sort, printed by print 1887 GrowableArray<const Node*> _print_list; 1888 1889 // print header + node table 1890 void print_header() const; 1891 void print_node(const Node* n); 1892 }; 1893 1894 void PrintBFS::run() { 1895 if (!configure()) { 1896 return; 1897 } 1898 collect(); 1899 select(); 1900 sort(); 1901 print(); 1902 } 1903 1904 // set up configuration for BFS and print 1905 bool PrintBFS::configure() { 1906 if (_max_distance < 0) { 1907 _output->print_cr("dump_bfs: max_distance must be non-negative!"); 1908 return false; 1909 } 1910 return parse_options(); 1911 } 1912 1913 // BFS traverse according to configuration, fill worklist and info 1914 void PrintBFS::collect() { 1915 maybe_traverse(_start, _start); 1916 int pos = 0; 1917 while (pos < _worklist.length()) { 1918 const Node* n = _worklist.at(pos++); // next node to traverse 1919 Info* info = find_info(n); 1920 if (!_filter_visit.accepts(n) && n != _start) { 1921 continue; // we hit boundary, do not traverse further 1922 } 1923 if (n != _start && n->is_Root()) { 1924 continue; // traversing through root node would lead to unrelated nodes 1925 } 1926 if (_traverse_inputs && _max_distance > info->distance()) { 1927 for (uint i = 0; i < n->req(); i++) { 1928 maybe_traverse(n, n->in(i)); 1929 } 1930 } 1931 if (_traverse_outputs && _max_distance > info->distance()) { 1932 for (uint i = 0; i < n->outcnt(); i++) { 1933 maybe_traverse(n, n->raw_out(i)); 1934 } 1935 } 1936 } 1937 } 1938 1939 // go through work list, mark those that we want to print 1940 void PrintBFS::select() { 1941 if (_target == nullptr ) { 1942 select_all(); 1943 } else { 1944 if (find_info(_target) == nullptr) { 1945 _output->print_cr("Could not find target in BFS."); 1946 return; 1947 } 1948 if (_all_paths) { 1949 select_all_paths(); 1950 } else { 1951 select_shortest_path(); 1952 } 1953 } 1954 } 1955 1956 // take all nodes from BFS 1957 void PrintBFS::select_all() { 1958 for (int i = 0; i < _worklist.length(); i++) { 1959 const Node* n = _worklist.at(i); 1960 Info* info = find_info(n); 1961 info->set_mark(); 1962 } 1963 } 1964 1965 // traverse backward from target, along edges found in BFS 1966 void PrintBFS::select_all_paths() { 1967 int pos = 0; 1968 GrowableArray<const Node*> backtrace; 1969 // start from target 1970 backtrace.push(_target); 1971 find_info(_target)->set_mark(); 1972 // traverse backward 1973 while (pos < backtrace.length()) { 1974 const Node* n = backtrace.at(pos++); 1975 Info* info = find_info(n); 1976 for (int i = 0; i < info->edge_bwd.length(); i++) { 1977 // all backward edges 1978 const Node* back = info->edge_bwd.at(i); 1979 Info* back_info = find_info(back); 1980 if (!back_info->is_marked()) { 1981 // not yet found this on way back. 1982 back_info->set_distance_from_target(info->distance_from_target() + 1); 1983 if (back_info->distance_from_target() + back_info->distance() <= _max_distance) { 1984 // total distance is small enough 1985 back_info->set_mark(); 1986 backtrace.push(back); 1987 } 1988 } 1989 } 1990 } 1991 } 1992 1993 void PrintBFS::select_shortest_path() { 1994 const Node* current = _target; 1995 while (true) { 1996 Info* info = find_info(current); 1997 info->set_mark(); 1998 if (current == _start) { 1999 break; 2000 } 2001 // first edge -> leads us one step closer to _start 2002 current = info->edge_bwd.at(0); 2003 } 2004 } 2005 2006 // go through worklist in desired order, put the marked ones in print list 2007 void PrintBFS::sort() { 2008 if (_traverse_inputs && !_traverse_outputs) { 2009 // reverse order 2010 for (int i = _worklist.length() - 1; i >= 0; i--) { 2011 const Node* n = _worklist.at(i); 2012 Info* info = find_info(n); 2013 if (info->is_marked()) { 2014 _print_list.push(n); 2015 } 2016 } 2017 } else { 2018 // same order as worklist 2019 for (int i = 0; i < _worklist.length(); i++) { 2020 const Node* n = _worklist.at(i); 2021 Info* info = find_info(n); 2022 if (info->is_marked()) { 2023 _print_list.push(n); 2024 } 2025 } 2026 } 2027 if (_sort_idx) { 2028 _print_list.sort(node_idx_cmp); 2029 } 2030 } 2031 2032 // go through printlist and print 2033 void PrintBFS::print() { 2034 if (_print_list.length() > 0 ) { 2035 print_header(); 2036 for (int i = 0; i < _print_list.length(); i++) { 2037 const Node* n = _print_list.at(i); 2038 print_node(n); 2039 } 2040 } else { 2041 _output->print_cr("No nodes to print."); 2042 } 2043 } 2044 2045 void PrintBFS::print_options_help(bool print_examples) { 2046 _output->print_cr("Usage: node->dump_bfs(int max_distance, Node* target, char* options)"); 2047 _output->print_cr(""); 2048 _output->print_cr("Use cases:"); 2049 _output->print_cr(" BFS traversal: no target required"); 2050 _output->print_cr(" shortest path: set target"); 2051 _output->print_cr(" all paths: set target and put 'A' in options"); 2052 _output->print_cr(" detect loop: subcase of all paths, have start==target"); 2053 _output->print_cr(""); 2054 _output->print_cr("Arguments:"); 2055 _output->print_cr(" this/start: staring point of BFS"); 2056 _output->print_cr(" target:"); 2057 _output->print_cr(" if null: simple BFS"); 2058 _output->print_cr(" else: shortest path or all paths between this/start and target"); 2059 _output->print_cr(" options:"); 2060 _output->print_cr(" if null: same as \"cdmox@B\""); 2061 _output->print_cr(" else: use combination of following characters"); 2062 _output->print_cr(" h: display this help info"); 2063 _output->print_cr(" H: display this help info, with examples"); 2064 _output->print_cr(" +: traverse in-edges (on if neither + nor -)"); 2065 _output->print_cr(" -: traverse out-edges"); 2066 _output->print_cr(" c: visit control nodes"); 2067 _output->print_cr(" d: visit data nodes"); 2068 _output->print_cr(" m: visit memory nodes"); 2069 _output->print_cr(" o: visit other nodes"); 2070 _output->print_cr(" x: visit mixed nodes"); 2071 _output->print_cr(" C: boundary control nodes"); 2072 _output->print_cr(" D: boundary data nodes"); 2073 _output->print_cr(" M: boundary memory nodes"); 2074 _output->print_cr(" O: boundary other nodes"); 2075 _output->print_cr(" X: boundary mixed nodes"); 2076 _output->print_cr(" #: display node category in color (not supported in all terminals)"); 2077 _output->print_cr(" S: sort displayed nodes by node idx"); 2078 _output->print_cr(" A: all paths (not just shortest path to target)"); 2079 _output->print_cr(" @: print old nodes - before matching (if available)"); 2080 _output->print_cr(" B: print scheduling blocks (if available)"); 2081 _output->print_cr(" $: dump only, no header, no other columns"); 2082 _output->print_cr(""); 2083 _output->print_cr("recursively follow edges to nodes with permitted visit types,"); 2084 _output->print_cr("on the boundary additionally display nodes allowed in boundary types"); 2085 _output->print_cr("Note: the categories can be overlapping. For example a mixed node"); 2086 _output->print_cr(" can contain control and memory output. Some from the other"); 2087 _output->print_cr(" category are also control (Halt, Return, etc)."); 2088 _output->print_cr(""); 2089 _output->print_cr("output columns:"); 2090 _output->print_cr(" dist: BFS distance to this/start"); 2091 _output->print_cr(" apd: all paths distance (d_outputart + d_target)"); 2092 _output->print_cr(" block: block identifier, based on _pre_order"); 2093 _output->print_cr(" head: first node in block"); 2094 _output->print_cr(" idom: head node of idom block"); 2095 _output->print_cr(" depth: depth of block (_dom_depth)"); 2096 _output->print_cr(" old: old IR node - before matching"); 2097 _output->print_cr(" dump: node->dump()"); 2098 _output->print_cr(""); 2099 _output->print_cr("Note: if none of the \"cmdxo\" characters are in the options string"); 2100 _output->print_cr(" then we set all of them."); 2101 _output->print_cr(" This allows for short strings like \"#\" for colored input traversal"); 2102 _output->print_cr(" or \"-#\" for colored output traversal."); 2103 if (print_examples) { 2104 _output->print_cr(""); 2105 _output->print_cr("Examples:"); 2106 _output->print_cr(" if->dump_bfs(10, 0, \"+cxo\")"); 2107 _output->print_cr(" starting at some if node, traverse inputs recursively"); 2108 _output->print_cr(" only along control (mixed and other can also be control)"); 2109 _output->print_cr(" phi->dump_bfs(5, 0, \"-dxo\")"); 2110 _output->print_cr(" starting at phi node, traverse outputs recursively"); 2111 _output->print_cr(" only along data (mixed and other can also have data flow)"); 2112 _output->print_cr(" find_node(385)->dump_bfs(3, 0, \"cdmox+#@B\")"); 2113 _output->print_cr(" find inputs of node 385, up to 3 nodes up (+)"); 2114 _output->print_cr(" traverse all nodes (cdmox), use colors (#)"); 2115 _output->print_cr(" display old nodes and blocks, if they exist"); 2116 _output->print_cr(" useful call to start with"); 2117 _output->print_cr(" find_node(102)->dump_bfs(10, 0, \"dCDMOX-\")"); 2118 _output->print_cr(" find non-data dependencies of a data node"); 2119 _output->print_cr(" follow data node outputs until we find another category"); 2120 _output->print_cr(" node as the boundary"); 2121 _output->print_cr(" x->dump_bfs(10, y, 0)"); 2122 _output->print_cr(" find shortest path from x to y, along any edge or node"); 2123 _output->print_cr(" will not find a path if it is longer than 10"); 2124 _output->print_cr(" useful to find how x and y are related"); 2125 _output->print_cr(" find_node(741)->dump_bfs(20, find_node(746), \"c+\")"); 2126 _output->print_cr(" find shortest control path between two nodes"); 2127 _output->print_cr(" find_node(741)->dump_bfs(8, find_node(746), \"cdmox+A\")"); 2128 _output->print_cr(" find all paths (A) between two nodes of length at most 8"); 2129 _output->print_cr(" find_node(741)->dump_bfs(7, find_node(741), \"c+A\")"); 2130 _output->print_cr(" find all control loops for this node"); 2131 } 2132 } 2133 2134 bool PrintBFS::parse_options() { 2135 if (_options == nullptr) { 2136 _options = "cdmox@B"; // default options 2137 } 2138 size_t len = strlen(_options); 2139 for (size_t i = 0; i < len; i++) { 2140 switch (_options[i]) { 2141 case '+': 2142 _traverse_inputs = true; 2143 break; 2144 case '-': 2145 _traverse_outputs = true; 2146 break; 2147 case 'c': 2148 _filter_visit._control = true; 2149 break; 2150 case 'm': 2151 _filter_visit._memory = true; 2152 break; 2153 case 'd': 2154 _filter_visit._data = true; 2155 break; 2156 case 'x': 2157 _filter_visit._mixed = true; 2158 break; 2159 case 'o': 2160 _filter_visit._other = true; 2161 break; 2162 case 'C': 2163 _filter_boundary._control = true; 2164 break; 2165 case 'M': 2166 _filter_boundary._memory = true; 2167 break; 2168 case 'D': 2169 _filter_boundary._data = true; 2170 break; 2171 case 'X': 2172 _filter_boundary._mixed = true; 2173 break; 2174 case 'O': 2175 _filter_boundary._other = true; 2176 break; 2177 case 'S': 2178 _sort_idx = true; 2179 break; 2180 case 'A': 2181 _all_paths = true; 2182 break; 2183 case '#': 2184 _use_color = true; 2185 break; 2186 case 'B': 2187 _print_blocks = true; 2188 break; 2189 case '@': 2190 _print_old = true; 2191 break; 2192 case '$': 2193 _dump_only = true; 2194 break; 2195 case 'h': 2196 print_options_help(false); 2197 return false; 2198 case 'H': 2199 print_options_help(true); 2200 return false; 2201 default: 2202 _output->print_cr("dump_bfs: Unrecognized option \'%c\'", _options[i]); 2203 _output->print_cr("for help, run: find_node(0)->dump_bfs(0,0,\"H\")"); 2204 return false; 2205 } 2206 } 2207 if (!_traverse_inputs && !_traverse_outputs) { 2208 _traverse_inputs = true; 2209 } 2210 if (_filter_visit.is_empty()) { 2211 _filter_visit.set_all(); 2212 } 2213 Compile* C = Compile::current(); 2214 _print_old &= (C->matcher() != nullptr); // only show old if there are new 2215 _print_blocks &= (C->cfg() != nullptr); // only show blocks if available 2216 return true; 2217 } 2218 2219 void PrintBFS::DumpConfigColored::pre_dump(outputStream* st, const Node* n) { 2220 if (!_bfs->_use_color) { 2221 return; 2222 } 2223 Info* info = _bfs->find_info(n); 2224 if (info == nullptr || !info->is_marked()) { 2225 return; 2226 } 2227 2228 const Type* t = n->bottom_type(); 2229 switch (t->category()) { 2230 case Type::Category::Data: 2231 st->print("\u001b[34m"); 2232 break; 2233 case Type::Category::Memory: 2234 st->print("\u001b[32m"); 2235 break; 2236 case Type::Category::Mixed: 2237 st->print("\u001b[35m"); 2238 break; 2239 case Type::Category::Control: 2240 st->print("\u001b[31m"); 2241 break; 2242 case Type::Category::Other: 2243 st->print("\u001b[33m"); 2244 break; 2245 case Type::Category::Undef: 2246 n->dump(); 2247 assert(false, "category undef ??"); 2248 break; 2249 default: 2250 n->dump(); 2251 assert(false, "not covered"); 2252 break; 2253 } 2254 } 2255 2256 void PrintBFS::DumpConfigColored::post_dump(outputStream* st) { 2257 if (!_bfs->_use_color) { 2258 return; 2259 } 2260 st->print("\u001b[0m"); // white 2261 } 2262 2263 Node* PrintBFS::old_node(const Node* n) { 2264 Compile* C = Compile::current(); 2265 if (C->matcher() == nullptr || !C->node_arena()->contains(n)) { 2266 return (Node*)nullptr; 2267 } else { 2268 return C->matcher()->find_old_node(n); 2269 } 2270 } 2271 2272 void PrintBFS::print_node_idx(const Node* n) { 2273 Compile* C = Compile::current(); 2274 char buf[30]; 2275 if (n == nullptr) { 2276 os::snprintf_checked(buf, sizeof(buf), "_"); // null 2277 } else if (C->node_arena()->contains(n)) { 2278 os::snprintf_checked(buf, sizeof(buf), "%d", n->_idx); // new node 2279 } else { 2280 os::snprintf_checked(buf, sizeof(buf), "o%d", n->_idx); // old node 2281 } 2282 _output->print("%6s", buf); 2283 } 2284 2285 void PrintBFS::print_block_id(const Block* b) { 2286 Compile* C = Compile::current(); 2287 char buf[30]; 2288 os::snprintf_checked(buf, sizeof(buf), "B%d", b->_pre_order); 2289 _output->print("%7s", buf); 2290 } 2291 2292 void PrintBFS::print_node_block(const Node* n) { 2293 Compile* C = Compile::current(); 2294 Block* b = C->node_arena()->contains(n) 2295 ? C->cfg()->get_block_for_node(n) 2296 : nullptr; // guard against old nodes 2297 if (b == nullptr) { 2298 _output->print(" _"); // Block 2299 _output->print(" _"); // head 2300 _output->print(" _"); // idom 2301 _output->print(" _"); // depth 2302 } else { 2303 print_block_id(b); 2304 print_node_idx(b->head()); 2305 if (b->_idom) { 2306 print_node_idx(b->_idom->head()); 2307 } else { 2308 _output->print(" _"); // idom 2309 } 2310 _output->print("%6d ", b->_dom_depth); 2311 } 2312 } 2313 2314 // filter, and add to worklist, add info, note traversal edges 2315 void PrintBFS::maybe_traverse(const Node* src, const Node* dst) { 2316 if (dst != nullptr && 2317 (_filter_visit.accepts(dst) || 2318 _filter_boundary.accepts(dst) || 2319 dst == _start)) { // correct category or start? 2320 if (find_info(dst) == nullptr) { 2321 // never visited - set up info 2322 _worklist.push(dst); 2323 int d = 0; 2324 if (dst != _start) { 2325 d = find_info(src)->distance() + 1; 2326 } 2327 make_info(dst, d); 2328 } 2329 if (src != dst) { 2330 // traversal edges useful during select 2331 find_info(dst)->edge_bwd.push(src); 2332 } 2333 } 2334 } 2335 2336 void PrintBFS::print_header() const { 2337 if (_dump_only) { 2338 return; // no header in dump only mode 2339 } 2340 _output->print("dist"); // distance 2341 if (_all_paths) { 2342 _output->print(" apd"); // all paths distance 2343 } 2344 if (_print_blocks) { 2345 _output->print(" [block head idom depth]"); // block 2346 } 2347 if (_print_old) { 2348 _output->print(" old"); // old node 2349 } 2350 _output->print(" dump\n"); // node dump 2351 _output->print_cr("---------------------------------------------"); 2352 } 2353 2354 void PrintBFS::print_node(const Node* n) { 2355 if (_dump_only) { 2356 n->dump("\n", false, _output, &_dcc); 2357 return; 2358 } 2359 _output->print("%4d", find_info(n)->distance());// distance 2360 if (_all_paths) { 2361 Info* info = find_info(n); 2362 int apd = info->distance() + info->distance_from_target(); 2363 _output->print("%4d", apd); // all paths distance 2364 } 2365 if (_print_blocks) { 2366 print_node_block(n); // block 2367 } 2368 if (_print_old) { 2369 print_node_idx(old_node(n)); // old node 2370 } 2371 _output->print(" "); 2372 n->dump("\n", false, _output, &_dcc); // node dump 2373 } 2374 2375 //------------------------------dump_bfs-------------------------------------- 2376 // Call this from debugger 2377 // Useful for BFS traversal, shortest path, all path, loop detection, etc 2378 // Designed to be more readable, and provide additional info 2379 // To find all options, run: 2380 // find_node(0)->dump_bfs(0,0,"H") 2381 void Node::dump_bfs(const int max_distance, Node* target, const char* options) const { 2382 dump_bfs(max_distance, target, options, tty); 2383 } 2384 2385 // Used to dump to stream. 2386 void Node::dump_bfs(const int max_distance, Node* target, const char* options, outputStream* st) const { 2387 PrintBFS bfs(this, max_distance, target, options, st); 2388 bfs.run(); 2389 } 2390 2391 // Call this from debugger, with default arguments 2392 void Node::dump_bfs(const int max_distance) const { 2393 dump_bfs(max_distance, nullptr, nullptr); 2394 } 2395 2396 // -----------------------------dump_idx--------------------------------------- 2397 void Node::dump_idx(bool align, outputStream* st, DumpConfig* dc) const { 2398 if (dc != nullptr) { 2399 dc->pre_dump(st, this); 2400 } 2401 Compile* C = Compile::current(); 2402 bool is_new = C->node_arena()->contains(this); 2403 if (align) { // print prefix empty spaces$ 2404 // +1 for leading digit, +1 for "o" 2405 uint max_width = static_cast<uint>(log10(static_cast<double>(C->unique()))) + 2; 2406 // +1 for leading digit, maybe +1 for "o" 2407 uint width = static_cast<uint>(log10(static_cast<double>(_idx))) + 1 + (is_new ? 0 : 1); 2408 while (max_width > width) { 2409 st->print(" "); 2410 width++; 2411 } 2412 } 2413 if (!is_new) { 2414 st->print("o"); 2415 } 2416 st->print("%d", _idx); 2417 if (dc != nullptr) { 2418 dc->post_dump(st); 2419 } 2420 } 2421 2422 // -----------------------------dump_name-------------------------------------- 2423 void Node::dump_name(outputStream* st, DumpConfig* dc) const { 2424 if (dc != nullptr) { 2425 dc->pre_dump(st, this); 2426 } 2427 st->print("%s", Name()); 2428 if (dc != nullptr) { 2429 dc->post_dump(st); 2430 } 2431 } 2432 2433 // -----------------------------Name------------------------------------------- 2434 extern const char *NodeClassNames[]; 2435 const char *Node::Name() const { return NodeClassNames[Opcode()]; } 2436 2437 static bool is_disconnected(const Node* n) { 2438 for (uint i = 0; i < n->req(); i++) { 2439 if (n->in(i) != nullptr) return false; 2440 } 2441 return true; 2442 } 2443 2444 #ifdef ASSERT 2445 void Node::dump_orig(outputStream *st, bool print_key) const { 2446 Compile* C = Compile::current(); 2447 Node* orig = _debug_orig; 2448 if (not_a_node(orig)) orig = nullptr; 2449 if (orig != nullptr && !C->node_arena()->contains(orig)) orig = nullptr; 2450 if (orig == nullptr) return; 2451 if (print_key) { 2452 st->print(" !orig="); 2453 } 2454 Node* fast = orig->debug_orig(); // tortoise & hare algorithm to detect loops 2455 if (not_a_node(fast)) fast = nullptr; 2456 while (orig != nullptr) { 2457 bool discon = is_disconnected(orig); // if discon, print [123] else 123 2458 if (discon) st->print("["); 2459 if (!Compile::current()->node_arena()->contains(orig)) 2460 st->print("o"); 2461 st->print("%d", orig->_idx); 2462 if (discon) st->print("]"); 2463 orig = orig->debug_orig(); 2464 if (not_a_node(orig)) orig = nullptr; 2465 if (orig != nullptr && !C->node_arena()->contains(orig)) orig = nullptr; 2466 if (orig != nullptr) st->print(","); 2467 if (fast != nullptr) { 2468 // Step fast twice for each single step of orig: 2469 fast = fast->debug_orig(); 2470 if (not_a_node(fast)) fast = nullptr; 2471 if (fast != nullptr && fast != orig) { 2472 fast = fast->debug_orig(); 2473 if (not_a_node(fast)) fast = nullptr; 2474 } 2475 if (fast == orig) { 2476 st->print("..."); 2477 break; 2478 } 2479 } 2480 } 2481 } 2482 2483 void Node::set_debug_orig(Node* orig) { 2484 _debug_orig = orig; 2485 if (BreakAtNode == 0) return; 2486 if (not_a_node(orig)) orig = nullptr; 2487 int trip = 10; 2488 while (orig != nullptr) { 2489 if (orig->debug_idx() == BreakAtNode || (uintx)orig->_idx == BreakAtNode) { 2490 tty->print_cr("BreakAtNode: _idx=%d _debug_idx=" UINT64_FORMAT " orig._idx=%d orig._debug_idx=" UINT64_FORMAT, 2491 this->_idx, this->debug_idx(), orig->_idx, orig->debug_idx()); 2492 BREAKPOINT; 2493 } 2494 orig = orig->debug_orig(); 2495 if (not_a_node(orig)) orig = nullptr; 2496 if (trip-- <= 0) break; 2497 } 2498 } 2499 #endif //ASSERT 2500 2501 //------------------------------dump------------------------------------------ 2502 // Dump a Node 2503 void Node::dump(const char* suffix, bool mark, outputStream* st, DumpConfig* dc) const { 2504 Compile* C = Compile::current(); 2505 bool is_new = C->node_arena()->contains(this); 2506 C->_in_dump_cnt++; 2507 2508 // idx mark name === 2509 dump_idx(true, st, dc); 2510 st->print(mark ? " >" : " "); 2511 dump_name(st, dc); 2512 st->print(" === "); 2513 2514 // Dump the required and precedence inputs 2515 dump_req(st, dc); 2516 dump_prec(st, dc); 2517 // Dump the outputs 2518 dump_out(st, dc); 2519 2520 if (is_disconnected(this)) { 2521 #ifdef ASSERT 2522 st->print(" [" UINT64_FORMAT "]", debug_idx()); 2523 dump_orig(st); 2524 #endif 2525 st->cr(); 2526 C->_in_dump_cnt--; 2527 return; // don't process dead nodes 2528 } 2529 2530 if (C->clone_map().value(_idx) != 0) { 2531 C->clone_map().dump(_idx, st); 2532 } 2533 // Dump node-specific info 2534 dump_spec(st); 2535 #ifdef ASSERT 2536 // Dump the non-reset _debug_idx 2537 if (Verbose && WizardMode) { 2538 st->print(" [" UINT64_FORMAT "]", debug_idx()); 2539 } 2540 #endif 2541 2542 const Type *t = bottom_type(); 2543 2544 if (t != nullptr && (t->isa_instptr() || t->isa_instklassptr())) { 2545 const TypeInstPtr *toop = t->isa_instptr(); 2546 const TypeInstKlassPtr *tkls = t->isa_instklassptr(); 2547 if (toop) { 2548 st->print(" Oop:"); 2549 } else if (tkls) { 2550 st->print(" Klass:"); 2551 } 2552 t->dump_on(st); 2553 } else if (t == Type::MEMORY) { 2554 st->print(" Memory:"); 2555 MemNode::dump_adr_type(this, adr_type(), st); 2556 } else if (Verbose || WizardMode) { 2557 st->print(" Type:"); 2558 if (t) { 2559 t->dump_on(st); 2560 } else { 2561 st->print("no type"); 2562 } 2563 } else if (t->isa_vect() && this->is_MachSpillCopy()) { 2564 // Dump MachSpillcopy vector type. 2565 t->dump_on(st); 2566 } 2567 if (is_new) { 2568 DEBUG_ONLY(dump_orig(st)); 2569 Node_Notes* nn = C->node_notes_at(_idx); 2570 if (nn != nullptr && !nn->is_clear()) { 2571 if (nn->jvms() != nullptr) { 2572 st->print(" !jvms:"); 2573 nn->jvms()->dump_spec(st); 2574 } 2575 } 2576 } 2577 if (suffix) st->print("%s", suffix); 2578 C->_in_dump_cnt--; 2579 } 2580 2581 // call from debugger: dump node to tty with newline 2582 void Node::dump() const { 2583 dump("\n"); 2584 } 2585 2586 //------------------------------dump_req-------------------------------------- 2587 void Node::dump_req(outputStream* st, DumpConfig* dc) const { 2588 // Dump the required input edges 2589 for (uint i = 0; i < req(); i++) { // For all required inputs 2590 Node* d = in(i); 2591 if (d == nullptr) { 2592 st->print("_ "); 2593 } else if (not_a_node(d)) { 2594 st->print("not_a_node "); // uninitialized, sentinel, garbage, etc. 2595 } else { 2596 d->dump_idx(false, st, dc); 2597 st->print(" "); 2598 } 2599 } 2600 } 2601 2602 2603 //------------------------------dump_prec------------------------------------- 2604 void Node::dump_prec(outputStream* st, DumpConfig* dc) const { 2605 // Dump the precedence edges 2606 int any_prec = 0; 2607 for (uint i = req(); i < len(); i++) { // For all precedence inputs 2608 Node* p = in(i); 2609 if (p != nullptr) { 2610 if (!any_prec++) st->print(" |"); 2611 if (not_a_node(p)) { st->print("not_a_node "); continue; } 2612 p->dump_idx(false, st, dc); 2613 st->print(" "); 2614 } 2615 } 2616 } 2617 2618 //------------------------------dump_out-------------------------------------- 2619 void Node::dump_out(outputStream* st, DumpConfig* dc) const { 2620 // Delimit the output edges 2621 st->print(" [[ "); 2622 // Dump the output edges 2623 for (uint i = 0; i < _outcnt; i++) { // For all outputs 2624 Node* u = _out[i]; 2625 if (u == nullptr) { 2626 st->print("_ "); 2627 } else if (not_a_node(u)) { 2628 st->print("not_a_node "); 2629 } else { 2630 u->dump_idx(false, st, dc); 2631 st->print(" "); 2632 } 2633 } 2634 st->print("]] "); 2635 } 2636 2637 //------------------------------dump------------------------------------------- 2638 // call from debugger: dump Node's inputs (or outputs if d negative) 2639 void Node::dump(int d) const { 2640 dump_bfs(abs(d), nullptr, (d > 0) ? "+$" : "-$"); 2641 } 2642 2643 //------------------------------dump_ctrl-------------------------------------- 2644 // call from debugger: dump Node's control inputs (or outputs if d negative) 2645 void Node::dump_ctrl(int d) const { 2646 dump_bfs(abs(d), nullptr, (d > 0) ? "+$c" : "-$c"); 2647 } 2648 2649 //-----------------------------dump_compact------------------------------------ 2650 void Node::dump_comp() const { 2651 this->dump_comp("\n"); 2652 } 2653 2654 //-----------------------------dump_compact------------------------------------ 2655 // Dump a Node in compact representation, i.e., just print its name and index. 2656 // Nodes can specify additional specifics to print in compact representation by 2657 // implementing dump_compact_spec. 2658 void Node::dump_comp(const char* suffix, outputStream *st) const { 2659 Compile* C = Compile::current(); 2660 C->_in_dump_cnt++; 2661 st->print("%s(%d)", Name(), _idx); 2662 this->dump_compact_spec(st); 2663 if (suffix) { 2664 st->print("%s", suffix); 2665 } 2666 C->_in_dump_cnt--; 2667 } 2668 2669 // VERIFICATION CODE 2670 // Verify all nodes if verify_depth is negative 2671 void Node::verify(int verify_depth, VectorSet& visited, Node_List& worklist) { 2672 assert(verify_depth != 0, "depth should not be 0"); 2673 Compile* C = Compile::current(); 2674 uint last_index_on_current_depth = worklist.size() - 1; 2675 verify_depth--; // Visiting the first node on depth 1 2676 // Only add nodes to worklist if verify_depth is negative (visit all nodes) or greater than 0 2677 bool add_to_worklist = verify_depth != 0; 2678 2679 for (uint list_index = 0; list_index < worklist.size(); list_index++) { 2680 Node* n = worklist[list_index]; 2681 2682 if (n->is_Con() && n->bottom_type() == Type::TOP) { 2683 if (C->cached_top_node() == nullptr) { 2684 C->set_cached_top_node((Node*)n); 2685 } 2686 assert(C->cached_top_node() == n, "TOP node must be unique"); 2687 } 2688 2689 uint in_len = n->len(); 2690 for (uint i = 0; i < in_len; i++) { 2691 Node* x = n->_in[i]; 2692 if (!x || x->is_top()) { 2693 continue; 2694 } 2695 2696 // Verify my input has a def-use edge to me 2697 // Count use-def edges from n to x 2698 int cnt = 1; 2699 for (uint j = 0; j < i; j++) { 2700 if (n->_in[j] == x) { 2701 cnt++; 2702 break; 2703 } 2704 } 2705 if (cnt == 2) { 2706 // x is already checked as n's previous input, skip its duplicated def-use count checking 2707 continue; 2708 } 2709 for (uint j = i + 1; j < in_len; j++) { 2710 if (n->_in[j] == x) { 2711 cnt++; 2712 } 2713 } 2714 2715 // Count def-use edges from x to n 2716 uint max = x->_outcnt; 2717 for (uint k = 0; k < max; k++) { 2718 if (x->_out[k] == n) { 2719 cnt--; 2720 } 2721 } 2722 assert(cnt == 0, "mismatched def-use edge counts"); 2723 2724 if (add_to_worklist && !visited.test_set(x->_idx)) { 2725 worklist.push(x); 2726 } 2727 } 2728 2729 if (verify_depth > 0 && list_index == last_index_on_current_depth) { 2730 // All nodes on this depth were processed and its inputs are on the worklist. Decrement verify_depth and 2731 // store the current last list index which is the last node in the list with the new depth. All nodes 2732 // added afterwards will have a new depth again. Stop adding new nodes if depth limit is reached (=0). 2733 verify_depth--; 2734 if (verify_depth == 0) { 2735 add_to_worklist = false; 2736 } 2737 last_index_on_current_depth = worklist.size() - 1; 2738 } 2739 } 2740 } 2741 #endif // not PRODUCT 2742 2743 //------------------------------Registers-------------------------------------- 2744 // Do we Match on this edge index or not? Generally false for Control 2745 // and true for everything else. Weird for calls & returns. 2746 uint Node::match_edge(uint idx) const { 2747 return idx; // True for other than index 0 (control) 2748 } 2749 2750 // Register classes are defined for specific machines 2751 const RegMask &Node::out_RegMask() const { 2752 ShouldNotCallThis(); 2753 return RegMask::Empty; 2754 } 2755 2756 const RegMask &Node::in_RegMask(uint) const { 2757 ShouldNotCallThis(); 2758 return RegMask::Empty; 2759 } 2760 2761 void Node_Array::grow(uint i) { 2762 assert(_max > 0, "invariant"); 2763 uint old = _max; 2764 _max = next_power_of_2(i); 2765 _nodes = (Node**)_a->Arealloc( _nodes, old*sizeof(Node*),_max*sizeof(Node*)); 2766 Copy::zero_to_bytes( &_nodes[old], (_max-old)*sizeof(Node*) ); 2767 } 2768 2769 void Node_Array::insert(uint i, Node* n) { 2770 if (_nodes[_max - 1]) { 2771 grow(_max); 2772 } 2773 Copy::conjoint_words_to_higher((HeapWord*)&_nodes[i], (HeapWord*)&_nodes[i + 1], ((_max - i - 1) * sizeof(Node*))); 2774 _nodes[i] = n; 2775 } 2776 2777 void Node_Array::remove(uint i) { 2778 Copy::conjoint_words_to_lower((HeapWord*)&_nodes[i + 1], (HeapWord*)&_nodes[i], ((_max - i - 1) * sizeof(Node*))); 2779 _nodes[_max - 1] = nullptr; 2780 } 2781 2782 void Node_Array::dump() const { 2783 #ifndef PRODUCT 2784 for (uint i = 0; i < _max; i++) { 2785 Node* nn = _nodes[i]; 2786 if (nn != nullptr) { 2787 tty->print("%5d--> ",i); nn->dump(); 2788 } 2789 } 2790 #endif 2791 } 2792 2793 //--------------------------is_iteratively_computed------------------------------ 2794 // Operation appears to be iteratively computed (such as an induction variable) 2795 // It is possible for this operation to return false for a loop-varying 2796 // value, if it appears (by local graph inspection) to be computed by a simple conditional. 2797 bool Node::is_iteratively_computed() { 2798 if (ideal_reg()) { // does operation have a result register? 2799 for (uint i = 1; i < req(); i++) { 2800 Node* n = in(i); 2801 if (n != nullptr && n->is_Phi()) { 2802 for (uint j = 1; j < n->req(); j++) { 2803 if (n->in(j) == this) { 2804 return true; 2805 } 2806 } 2807 } 2808 } 2809 } 2810 return false; 2811 } 2812 2813 //--------------------------find_similar------------------------------ 2814 // Return a node with opcode "opc" and same inputs as "this" if one can 2815 // be found; Otherwise return null; 2816 Node* Node::find_similar(int opc) { 2817 if (req() >= 2) { 2818 Node* def = in(1); 2819 if (def && def->outcnt() >= 2) { 2820 for (DUIterator_Fast dmax, i = def->fast_outs(dmax); i < dmax; i++) { 2821 Node* use = def->fast_out(i); 2822 if (use != this && 2823 use->Opcode() == opc && 2824 use->req() == req()) { 2825 uint j; 2826 for (j = 0; j < use->req(); j++) { 2827 if (use->in(j) != in(j)) { 2828 break; 2829 } 2830 } 2831 if (j == use->req()) { 2832 return use; 2833 } 2834 } 2835 } 2836 } 2837 } 2838 return nullptr; 2839 } 2840 2841 2842 //--------------------------unique_ctrl_out_or_null------------------------- 2843 // Return the unique control out if only one. Null if none or more than one. 2844 Node* Node::unique_ctrl_out_or_null() const { 2845 Node* found = nullptr; 2846 for (uint i = 0; i < outcnt(); i++) { 2847 Node* use = raw_out(i); 2848 if (use->is_CFG() && use != this) { 2849 if (found != nullptr) { 2850 return nullptr; 2851 } 2852 found = use; 2853 } 2854 } 2855 return found; 2856 } 2857 2858 //--------------------------unique_ctrl_out------------------------------ 2859 // Return the unique control out. Asserts if none or more than one control out. 2860 Node* Node::unique_ctrl_out() const { 2861 Node* ctrl = unique_ctrl_out_or_null(); 2862 assert(ctrl != nullptr, "control out is assumed to be unique"); 2863 return ctrl; 2864 } 2865 2866 void Node::ensure_control_or_add_prec(Node* c) { 2867 if (in(0) == nullptr) { 2868 set_req(0, c); 2869 } else if (in(0) != c) { 2870 add_prec(c); 2871 } 2872 } 2873 2874 bool Node::is_dead_loop_safe() const { 2875 if (is_Phi()) { 2876 return true; 2877 } 2878 if (is_Proj() && in(0) == nullptr) { 2879 return true; 2880 } 2881 if ((_flags & (Flag_is_dead_loop_safe | Flag_is_Con)) != 0) { 2882 if (!is_Proj()) { 2883 return true; 2884 } 2885 if (in(0)->is_Allocate()) { 2886 return false; 2887 } 2888 // MemNode::can_see_stored_value() peeks through the boxing call 2889 if (in(0)->is_CallStaticJava() && in(0)->as_CallStaticJava()->is_boxing_method()) { 2890 return false; 2891 } 2892 return true; 2893 } 2894 return false; 2895 } 2896 2897 //============================================================================= 2898 //------------------------------yank------------------------------------------- 2899 // Find and remove 2900 void Node_List::yank( Node *n ) { 2901 uint i; 2902 for (i = 0; i < _cnt; i++) { 2903 if (_nodes[i] == n) { 2904 break; 2905 } 2906 } 2907 2908 if (i < _cnt) { 2909 _nodes[i] = _nodes[--_cnt]; 2910 } 2911 } 2912 2913 //------------------------------dump------------------------------------------- 2914 void Node_List::dump() const { 2915 #ifndef PRODUCT 2916 for (uint i = 0; i < _cnt; i++) { 2917 if (_nodes[i]) { 2918 tty->print("%5d--> ", i); 2919 _nodes[i]->dump(); 2920 } 2921 } 2922 #endif 2923 } 2924 2925 void Node_List::dump_simple() const { 2926 #ifndef PRODUCT 2927 for (uint i = 0; i < _cnt; i++) { 2928 if( _nodes[i] ) { 2929 tty->print(" %d", _nodes[i]->_idx); 2930 } else { 2931 tty->print(" null"); 2932 } 2933 } 2934 #endif 2935 } 2936 2937 //============================================================================= 2938 //------------------------------remove----------------------------------------- 2939 void Unique_Node_List::remove(Node* n) { 2940 if (_in_worklist.test(n->_idx)) { 2941 for (uint i = 0; i < size(); i++) { 2942 if (_nodes[i] == n) { 2943 map(i, Node_List::pop()); 2944 _in_worklist.remove(n->_idx); 2945 return; 2946 } 2947 } 2948 ShouldNotReachHere(); 2949 } 2950 } 2951 2952 //-----------------------remove_useless_nodes---------------------------------- 2953 // Remove useless nodes from worklist 2954 void Unique_Node_List::remove_useless_nodes(VectorSet &useful) { 2955 for (uint i = 0; i < size(); ++i) { 2956 Node *n = at(i); 2957 assert( n != nullptr, "Did not expect null entries in worklist"); 2958 if (!useful.test(n->_idx)) { 2959 _in_worklist.remove(n->_idx); 2960 map(i, Node_List::pop()); 2961 --i; // Visit popped node 2962 // If it was last entry, loop terminates since size() was also reduced 2963 } 2964 } 2965 } 2966 2967 //============================================================================= 2968 void Node_Stack::grow() { 2969 size_t old_top = pointer_delta(_inode_top,_inodes,sizeof(INode)); // save _top 2970 size_t old_max = pointer_delta(_inode_max,_inodes,sizeof(INode)); 2971 size_t max = old_max << 1; // max * 2 2972 _inodes = REALLOC_ARENA_ARRAY(_a, INode, _inodes, old_max, max); 2973 _inode_max = _inodes + max; 2974 _inode_top = _inodes + old_top; // restore _top 2975 } 2976 2977 // Node_Stack is used to map nodes. 2978 Node* Node_Stack::find(uint idx) const { 2979 uint sz = size(); 2980 for (uint i = 0; i < sz; i++) { 2981 if (idx == index_at(i)) { 2982 return node_at(i); 2983 } 2984 } 2985 return nullptr; 2986 } 2987 2988 //============================================================================= 2989 uint TypeNode::size_of() const { return sizeof(*this); } 2990 #ifndef PRODUCT 2991 void TypeNode::dump_spec(outputStream *st) const { 2992 if (!Verbose && !WizardMode) { 2993 // standard dump does this in Verbose and WizardMode 2994 st->print(" #"); _type->dump_on(st); 2995 } 2996 } 2997 2998 void TypeNode::dump_compact_spec(outputStream *st) const { 2999 st->print("#"); 3000 _type->dump_on(st); 3001 } 3002 #endif 3003 uint TypeNode::hash() const { 3004 return Node::hash() + _type->hash(); 3005 } 3006 bool TypeNode::cmp(const Node& n) const { 3007 return !Type::cmp(_type, ((TypeNode&)n)._type); 3008 } 3009 const Type* TypeNode::bottom_type() const { return _type; } 3010 const Type* TypeNode::Value(PhaseGVN* phase) const { return _type; } 3011 3012 //------------------------------ideal_reg-------------------------------------- 3013 uint TypeNode::ideal_reg() const { 3014 return _type->ideal_reg(); 3015 }