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