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