1 /* 2 * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "asm/assembler.inline.hpp" 27 #include "asm/macroAssembler.inline.hpp" 28 #include "code/compiledIC.hpp" 29 #include "code/debugInfo.hpp" 30 #include "code/debugInfoRec.hpp" 31 #include "compiler/compileBroker.hpp" 32 #include "compiler/compilerDirectives.hpp" 33 #include "compiler/disassembler.hpp" 34 #include "compiler/oopMap.hpp" 35 #include "gc/shared/barrierSet.hpp" 36 #include "gc/shared/c2/barrierSetC2.hpp" 37 #include "memory/allocation.inline.hpp" 38 #include "memory/allocation.hpp" 39 #include "opto/ad.hpp" 40 #include "opto/block.hpp" 41 #include "opto/c2compiler.hpp" 42 #include "opto/c2_MacroAssembler.hpp" 43 #include "opto/callnode.hpp" 44 #include "opto/cfgnode.hpp" 45 #include "opto/locknode.hpp" 46 #include "opto/machnode.hpp" 47 #include "opto/node.hpp" 48 #include "opto/optoreg.hpp" 49 #include "opto/output.hpp" 50 #include "opto/regalloc.hpp" 51 #include "opto/runtime.hpp" 52 #include "opto/subnode.hpp" 53 #include "opto/type.hpp" 54 #include "runtime/handles.inline.hpp" 55 #include "runtime/sharedRuntime.hpp" 56 #include "utilities/macros.hpp" 57 #include "utilities/powerOfTwo.hpp" 58 #include "utilities/xmlstream.hpp" 59 60 #ifndef PRODUCT 61 #define DEBUG_ARG(x) , x 62 #else 63 #define DEBUG_ARG(x) 64 #endif 65 66 //------------------------------Scheduling---------------------------------- 67 // This class contains all the information necessary to implement instruction 68 // scheduling and bundling. 69 class Scheduling { 70 71 private: 72 // Arena to use 73 Arena *_arena; 74 75 // Control-Flow Graph info 76 PhaseCFG *_cfg; 77 78 // Register Allocation info 79 PhaseRegAlloc *_regalloc; 80 81 // Number of nodes in the method 82 uint _node_bundling_limit; 83 84 // List of scheduled nodes. Generated in reverse order 85 Node_List _scheduled; 86 87 // List of nodes currently available for choosing for scheduling 88 Node_List _available; 89 90 // For each instruction beginning a bundle, the number of following 91 // nodes to be bundled with it. 92 Bundle *_node_bundling_base; 93 94 // Mapping from register to Node 95 Node_List _reg_node; 96 97 // Free list for pinch nodes. 98 Node_List _pinch_free_list; 99 100 // Number of uses of this node within the containing basic block. 101 short *_uses; 102 103 // Schedulable portion of current block. Skips Region/Phi/CreateEx up 104 // front, branch+proj at end. Also skips Catch/CProj (same as 105 // branch-at-end), plus just-prior exception-throwing call. 106 uint _bb_start, _bb_end; 107 108 // Latency from the end of the basic block as scheduled 109 unsigned short *_current_latency; 110 111 // Remember the next node 112 Node *_next_node; 113 114 // Use this for an unconditional branch delay slot 115 Node *_unconditional_delay_slot; 116 117 // Pointer to a Nop 118 MachNopNode *_nop; 119 120 // Length of the current bundle, in instructions 121 uint _bundle_instr_count; 122 123 // Current Cycle number, for computing latencies and bundling 124 uint _bundle_cycle_number; 125 126 // Bundle information 127 Pipeline_Use_Element _bundle_use_elements[resource_count]; 128 Pipeline_Use _bundle_use; 129 130 // Dump the available list 131 void dump_available() const; 132 133 public: 134 Scheduling(Arena *arena, Compile &compile); 135 136 // Destructor 137 NOT_PRODUCT( ~Scheduling(); ) 138 139 // Step ahead "i" cycles 140 void step(uint i); 141 142 // Step ahead 1 cycle, and clear the bundle state (for example, 143 // at a branch target) 144 void step_and_clear(); 145 146 Bundle* node_bundling(const Node *n) { 147 assert(valid_bundle_info(n), "oob"); 148 return (&_node_bundling_base[n->_idx]); 149 } 150 151 bool valid_bundle_info(const Node *n) const { 152 return (_node_bundling_limit > n->_idx); 153 } 154 155 bool starts_bundle(const Node *n) const { 156 return (_node_bundling_limit > n->_idx && _node_bundling_base[n->_idx].starts_bundle()); 157 } 158 159 // Do the scheduling 160 void DoScheduling(); 161 162 // Compute the register antidependencies within a basic block 163 void ComputeRegisterAntidependencies(Block *bb); 164 void verify_do_def( Node *n, OptoReg::Name def, const char *msg ); 165 void verify_good_schedule( Block *b, const char *msg ); 166 void anti_do_def( Block *b, Node *def, OptoReg::Name def_reg, int is_def ); 167 void anti_do_use( Block *b, Node *use, OptoReg::Name use_reg ); 168 169 // Add a node to the current bundle 170 void AddNodeToBundle(Node *n, const Block *bb); 171 172 // Add a node to the list of available nodes 173 void AddNodeToAvailableList(Node *n); 174 175 // Compute the local use count for the nodes in a block, and compute 176 // the list of instructions with no uses in the block as available 177 void ComputeUseCount(const Block *bb); 178 179 // Choose an instruction from the available list to add to the bundle 180 Node * ChooseNodeToBundle(); 181 182 // See if this Node fits into the currently accumulating bundle 183 bool NodeFitsInBundle(Node *n); 184 185 // Decrement the use count for a node 186 void DecrementUseCounts(Node *n, const Block *bb); 187 188 // Garbage collect pinch nodes for reuse by other blocks. 189 void garbage_collect_pinch_nodes(); 190 // Clean up a pinch node for reuse (helper for above). 191 void cleanup_pinch( Node *pinch ); 192 193 // Information for statistics gathering 194 #ifndef PRODUCT 195 private: 196 // Gather information on size of nops relative to total 197 uint _branches, _unconditional_delays; 198 199 static uint _total_nop_size, _total_method_size; 200 static uint _total_branches, _total_unconditional_delays; 201 static uint _total_instructions_per_bundle[Pipeline::_max_instrs_per_cycle+1]; 202 203 public: 204 static void print_statistics(); 205 206 static void increment_instructions_per_bundle(uint i) { 207 _total_instructions_per_bundle[i]++; 208 } 209 210 static void increment_nop_size(uint s) { 211 _total_nop_size += s; 212 } 213 214 static void increment_method_size(uint s) { 215 _total_method_size += s; 216 } 217 #endif 218 219 }; 220 221 PhaseOutput::PhaseOutput() 222 : Phase(Phase::Output), 223 _code_buffer("Compile::Fill_buffer"), 224 _first_block_size(0), 225 _handler_table(), 226 _inc_table(), 227 _stub_list(), 228 _oop_map_set(nullptr), 229 _scratch_buffer_blob(nullptr), 230 _scratch_locs_memory(nullptr), 231 _scratch_const_size(-1), 232 _in_scratch_emit_size(false), 233 _frame_slots(0), 234 _code_offsets(), 235 _node_bundling_limit(0), 236 _node_bundling_base(nullptr), 237 _orig_pc_slot(0), 238 _orig_pc_slot_offset_in_bytes(0), 239 _buf_sizes(), 240 _block(nullptr), 241 _index(0) { 242 C->set_output(this); 243 if (C->stub_name() == nullptr) { 244 _orig_pc_slot = C->fixed_slots() - (sizeof(address) / VMRegImpl::stack_slot_size); 245 } 246 } 247 248 PhaseOutput::~PhaseOutput() { 249 C->set_output(nullptr); 250 if (_scratch_buffer_blob != nullptr) { 251 BufferBlob::free(_scratch_buffer_blob); 252 } 253 } 254 255 void PhaseOutput::perform_mach_node_analysis() { 256 // Late barrier analysis must be done after schedule and bundle 257 // Otherwise liveness based spilling will fail 258 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 259 bs->late_barrier_analysis(); 260 261 pd_perform_mach_node_analysis(); 262 263 C->print_method(CompilerPhaseType::PHASE_MACH_ANALYSIS, 3); 264 } 265 266 // Convert Nodes to instruction bits and pass off to the VM 267 void PhaseOutput::Output() { 268 // RootNode goes 269 assert( C->cfg()->get_root_block()->number_of_nodes() == 0, "" ); 270 271 // The number of new nodes (mostly MachNop) is proportional to 272 // the number of java calls and inner loops which are aligned. 273 if ( C->check_node_count((NodeLimitFudgeFactor + C->java_calls()*3 + 274 C->inner_loops()*(OptoLoopAlignment-1)), 275 "out of nodes before code generation" ) ) { 276 return; 277 } 278 // Make sure I can find the Start Node 279 Block *entry = C->cfg()->get_block(1); 280 Block *broot = C->cfg()->get_root_block(); 281 282 const StartNode *start = entry->head()->as_Start(); 283 284 // Replace StartNode with prolog 285 MachPrologNode *prolog = new MachPrologNode(); 286 entry->map_node(prolog, 0); 287 C->cfg()->map_node_to_block(prolog, entry); 288 C->cfg()->unmap_node_from_block(start); // start is no longer in any block 289 290 // Virtual methods need an unverified entry point 291 292 if( C->is_osr_compilation() ) { 293 if( PoisonOSREntry ) { 294 // TODO: Should use a ShouldNotReachHereNode... 295 C->cfg()->insert( broot, 0, new MachBreakpointNode() ); 296 } 297 } else { 298 if( C->method() && !C->method()->flags().is_static() ) { 299 // Insert unvalidated entry point 300 C->cfg()->insert( broot, 0, new MachUEPNode() ); 301 } 302 303 } 304 305 // Break before main entry point 306 if ((C->method() && C->directive()->BreakAtExecuteOption) || 307 (OptoBreakpoint && C->is_method_compilation()) || 308 (OptoBreakpointOSR && C->is_osr_compilation()) || 309 (OptoBreakpointC2R && !C->method()) ) { 310 // checking for C->method() means that OptoBreakpoint does not apply to 311 // runtime stubs or frame converters 312 C->cfg()->insert( entry, 1, new MachBreakpointNode() ); 313 } 314 315 // Insert epilogs before every return 316 for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) { 317 Block* block = C->cfg()->get_block(i); 318 if (!block->is_connector() && block->non_connector_successor(0) == C->cfg()->get_root_block()) { // Found a program exit point? 319 Node* m = block->end(); 320 if (m->is_Mach() && m->as_Mach()->ideal_Opcode() != Op_Halt) { 321 MachEpilogNode* epilog = new MachEpilogNode(m->as_Mach()->ideal_Opcode() == Op_Return); 322 block->add_inst(epilog); 323 C->cfg()->map_node_to_block(epilog, block); 324 } 325 } 326 } 327 328 // Keeper of sizing aspects 329 _buf_sizes = BufferSizingData(); 330 331 // Initialize code buffer 332 estimate_buffer_size(_buf_sizes._const); 333 if (C->failing()) return; 334 335 // Pre-compute the length of blocks and replace 336 // long branches with short if machine supports it. 337 // Must be done before ScheduleAndBundle due to SPARC delay slots 338 uint* blk_starts = NEW_RESOURCE_ARRAY(uint, C->cfg()->number_of_blocks() + 1); 339 blk_starts[0] = 0; 340 shorten_branches(blk_starts); 341 342 ScheduleAndBundle(); 343 if (C->failing()) { 344 return; 345 } 346 347 perform_mach_node_analysis(); 348 349 // Complete sizing of codebuffer 350 CodeBuffer* cb = init_buffer(); 351 if (cb == nullptr || C->failing()) { 352 return; 353 } 354 355 BuildOopMaps(); 356 357 if (C->failing()) { 358 return; 359 } 360 361 fill_buffer(cb, blk_starts); 362 } 363 364 bool PhaseOutput::need_stack_bang(int frame_size_in_bytes) const { 365 // Determine if we need to generate a stack overflow check. 366 // Do it if the method is not a stub function and 367 // has java calls or has frame size > vm_page_size/8. 368 // The debug VM checks that deoptimization doesn't trigger an 369 // unexpected stack overflow (compiled method stack banging should 370 // guarantee it doesn't happen) so we always need the stack bang in 371 // a debug VM. 372 return (C->stub_function() == nullptr && 373 (C->has_java_calls() || frame_size_in_bytes > (int)(os::vm_page_size())>>3 374 DEBUG_ONLY(|| true))); 375 } 376 377 bool PhaseOutput::need_register_stack_bang() const { 378 // Determine if we need to generate a register stack overflow check. 379 // This is only used on architectures which have split register 380 // and memory stacks (ie. IA64). 381 // Bang if the method is not a stub function and has java calls 382 return (C->stub_function() == nullptr && C->has_java_calls()); 383 } 384 385 386 // Compute the size of first NumberOfLoopInstrToAlign instructions at the top 387 // of a loop. When aligning a loop we need to provide enough instructions 388 // in cpu's fetch buffer to feed decoders. The loop alignment could be 389 // avoided if we have enough instructions in fetch buffer at the head of a loop. 390 // By default, the size is set to 999999 by Block's constructor so that 391 // a loop will be aligned if the size is not reset here. 392 // 393 // Note: Mach instructions could contain several HW instructions 394 // so the size is estimated only. 395 // 396 void PhaseOutput::compute_loop_first_inst_sizes() { 397 // The next condition is used to gate the loop alignment optimization. 398 // Don't aligned a loop if there are enough instructions at the head of a loop 399 // or alignment padding is larger then MaxLoopPad. By default, MaxLoopPad 400 // is equal to OptoLoopAlignment-1 except on new Intel cpus, where it is 401 // equal to 11 bytes which is the largest address NOP instruction. 402 if (MaxLoopPad < OptoLoopAlignment - 1) { 403 uint last_block = C->cfg()->number_of_blocks() - 1; 404 for (uint i = 1; i <= last_block; i++) { 405 Block* block = C->cfg()->get_block(i); 406 // Check the first loop's block which requires an alignment. 407 if (block->loop_alignment() > (uint)relocInfo::addr_unit()) { 408 uint sum_size = 0; 409 uint inst_cnt = NumberOfLoopInstrToAlign; 410 inst_cnt = block->compute_first_inst_size(sum_size, inst_cnt, C->regalloc()); 411 412 // Check subsequent fallthrough blocks if the loop's first 413 // block(s) does not have enough instructions. 414 Block *nb = block; 415 while(inst_cnt > 0 && 416 i < last_block && 417 !C->cfg()->get_block(i + 1)->has_loop_alignment() && 418 !nb->has_successor(block)) { 419 i++; 420 nb = C->cfg()->get_block(i); 421 inst_cnt = nb->compute_first_inst_size(sum_size, inst_cnt, C->regalloc()); 422 } // while( inst_cnt > 0 && i < last_block ) 423 424 block->set_first_inst_size(sum_size); 425 } // f( b->head()->is_Loop() ) 426 } // for( i <= last_block ) 427 } // if( MaxLoopPad < OptoLoopAlignment-1 ) 428 } 429 430 // The architecture description provides short branch variants for some long 431 // branch instructions. Replace eligible long branches with short branches. 432 void PhaseOutput::shorten_branches(uint* blk_starts) { 433 434 Compile::TracePhase tp("shorten branches", &timers[_t_shortenBranches]); 435 436 // Compute size of each block, method size, and relocation information size 437 uint nblocks = C->cfg()->number_of_blocks(); 438 439 uint* jmp_offset = NEW_RESOURCE_ARRAY(uint,nblocks); 440 uint* jmp_size = NEW_RESOURCE_ARRAY(uint,nblocks); 441 int* jmp_nidx = NEW_RESOURCE_ARRAY(int ,nblocks); 442 443 // Collect worst case block paddings 444 int* block_worst_case_pad = NEW_RESOURCE_ARRAY(int, nblocks); 445 memset(block_worst_case_pad, 0, nblocks * sizeof(int)); 446 447 DEBUG_ONLY( uint *jmp_target = NEW_RESOURCE_ARRAY(uint,nblocks); ) 448 DEBUG_ONLY( uint *jmp_rule = NEW_RESOURCE_ARRAY(uint,nblocks); ) 449 450 bool has_short_branch_candidate = false; 451 452 // Initialize the sizes to 0 453 int code_size = 0; // Size in bytes of generated code 454 int stub_size = 0; // Size in bytes of all stub entries 455 // Size in bytes of all relocation entries, including those in local stubs. 456 // Start with 2-bytes of reloc info for the unvalidated entry point 457 int reloc_size = 1; // Number of relocation entries 458 459 // Make three passes. The first computes pessimistic blk_starts, 460 // relative jmp_offset and reloc_size information. The second performs 461 // short branch substitution using the pessimistic sizing. The 462 // third inserts nops where needed. 463 464 // Step one, perform a pessimistic sizing pass. 465 uint last_call_adr = max_juint; 466 uint last_avoid_back_to_back_adr = max_juint; 467 uint nop_size = (new MachNopNode())->size(C->regalloc()); 468 for (uint i = 0; i < nblocks; i++) { // For all blocks 469 Block* block = C->cfg()->get_block(i); 470 _block = block; 471 472 // During short branch replacement, we store the relative (to blk_starts) 473 // offset of jump in jmp_offset, rather than the absolute offset of jump. 474 // This is so that we do not need to recompute sizes of all nodes when 475 // we compute correct blk_starts in our next sizing pass. 476 jmp_offset[i] = 0; 477 jmp_size[i] = 0; 478 jmp_nidx[i] = -1; 479 DEBUG_ONLY( jmp_target[i] = 0; ) 480 DEBUG_ONLY( jmp_rule[i] = 0; ) 481 482 // Sum all instruction sizes to compute block size 483 uint last_inst = block->number_of_nodes(); 484 uint blk_size = 0; 485 for (uint j = 0; j < last_inst; j++) { 486 _index = j; 487 Node* nj = block->get_node(_index); 488 // Handle machine instruction nodes 489 if (nj->is_Mach()) { 490 MachNode* mach = nj->as_Mach(); 491 blk_size += (mach->alignment_required() - 1) * relocInfo::addr_unit(); // assume worst case padding 492 reloc_size += mach->reloc(); 493 if (mach->is_MachCall()) { 494 // add size information for trampoline stub 495 // class CallStubImpl is platform-specific and defined in the *.ad files. 496 stub_size += CallStubImpl::size_call_trampoline(); 497 reloc_size += CallStubImpl::reloc_call_trampoline(); 498 499 MachCallNode *mcall = mach->as_MachCall(); 500 // This destination address is NOT PC-relative 501 502 mcall->method_set((intptr_t)mcall->entry_point()); 503 504 if (mcall->is_MachCallJava() && mcall->as_MachCallJava()->_method) { 505 stub_size += CompiledStaticCall::to_interp_stub_size(); 506 reloc_size += CompiledStaticCall::reloc_to_interp_stub(); 507 } 508 } else if (mach->is_MachSafePoint()) { 509 // If call/safepoint are adjacent, account for possible 510 // nop to disambiguate the two safepoints. 511 // ScheduleAndBundle() can rearrange nodes in a block, 512 // check for all offsets inside this block. 513 if (last_call_adr >= blk_starts[i]) { 514 blk_size += nop_size; 515 } 516 } 517 if (mach->avoid_back_to_back(MachNode::AVOID_BEFORE)) { 518 // Nop is inserted between "avoid back to back" instructions. 519 // ScheduleAndBundle() can rearrange nodes in a block, 520 // check for all offsets inside this block. 521 if (last_avoid_back_to_back_adr >= blk_starts[i]) { 522 blk_size += nop_size; 523 } 524 } 525 if (mach->may_be_short_branch()) { 526 if (!nj->is_MachBranch()) { 527 #ifndef PRODUCT 528 nj->dump(3); 529 #endif 530 Unimplemented(); 531 } 532 assert(jmp_nidx[i] == -1, "block should have only one branch"); 533 jmp_offset[i] = blk_size; 534 jmp_size[i] = nj->size(C->regalloc()); 535 jmp_nidx[i] = j; 536 has_short_branch_candidate = true; 537 } 538 } 539 blk_size += nj->size(C->regalloc()); 540 // Remember end of call offset 541 if (nj->is_MachCall() && !nj->is_MachCallLeaf()) { 542 last_call_adr = blk_starts[i]+blk_size; 543 } 544 // Remember end of avoid_back_to_back offset 545 if (nj->is_Mach() && nj->as_Mach()->avoid_back_to_back(MachNode::AVOID_AFTER)) { 546 last_avoid_back_to_back_adr = blk_starts[i]+blk_size; 547 } 548 } 549 550 // When the next block starts a loop, we may insert pad NOP 551 // instructions. Since we cannot know our future alignment, 552 // assume the worst. 553 if (i < nblocks - 1) { 554 Block* nb = C->cfg()->get_block(i + 1); 555 int max_loop_pad = nb->code_alignment()-relocInfo::addr_unit(); 556 if (max_loop_pad > 0) { 557 assert(is_power_of_2(max_loop_pad+relocInfo::addr_unit()), ""); 558 // Adjust last_call_adr and/or last_avoid_back_to_back_adr. 559 // If either is the last instruction in this block, bump by 560 // max_loop_pad in lock-step with blk_size, so sizing 561 // calculations in subsequent blocks still can conservatively 562 // detect that it may the last instruction in this block. 563 if (last_call_adr == blk_starts[i]+blk_size) { 564 last_call_adr += max_loop_pad; 565 } 566 if (last_avoid_back_to_back_adr == blk_starts[i]+blk_size) { 567 last_avoid_back_to_back_adr += max_loop_pad; 568 } 569 blk_size += max_loop_pad; 570 block_worst_case_pad[i + 1] = max_loop_pad; 571 } 572 } 573 574 // Save block size; update total method size 575 blk_starts[i+1] = blk_starts[i]+blk_size; 576 } 577 578 // Step two, replace eligible long jumps. 579 bool progress = true; 580 uint last_may_be_short_branch_adr = max_juint; 581 while (has_short_branch_candidate && progress) { 582 progress = false; 583 has_short_branch_candidate = false; 584 int adjust_block_start = 0; 585 for (uint i = 0; i < nblocks; i++) { 586 Block* block = C->cfg()->get_block(i); 587 int idx = jmp_nidx[i]; 588 MachNode* mach = (idx == -1) ? nullptr: block->get_node(idx)->as_Mach(); 589 if (mach != nullptr && mach->may_be_short_branch()) { 590 #ifdef ASSERT 591 assert(jmp_size[i] > 0 && mach->is_MachBranch(), "sanity"); 592 int j; 593 // Find the branch; ignore trailing NOPs. 594 for (j = block->number_of_nodes()-1; j>=0; j--) { 595 Node* n = block->get_node(j); 596 if (!n->is_Mach() || n->as_Mach()->ideal_Opcode() != Op_Con) 597 break; 598 } 599 assert(j >= 0 && j == idx && block->get_node(j) == (Node*)mach, "sanity"); 600 #endif 601 int br_size = jmp_size[i]; 602 int br_offs = blk_starts[i] + jmp_offset[i]; 603 604 // This requires the TRUE branch target be in succs[0] 605 uint bnum = block->non_connector_successor(0)->_pre_order; 606 int offset = blk_starts[bnum] - br_offs; 607 if (bnum > i) { // adjust following block's offset 608 offset -= adjust_block_start; 609 } 610 611 // This block can be a loop header, account for the padding 612 // in the previous block. 613 int block_padding = block_worst_case_pad[i]; 614 assert(i == 0 || block_padding == 0 || br_offs >= block_padding, "Should have at least a padding on top"); 615 // In the following code a nop could be inserted before 616 // the branch which will increase the backward distance. 617 bool needs_padding = ((uint)(br_offs - block_padding) == last_may_be_short_branch_adr); 618 assert(!needs_padding || jmp_offset[i] == 0, "padding only branches at the beginning of block"); 619 620 if (needs_padding && offset <= 0) 621 offset -= nop_size; 622 623 if (C->matcher()->is_short_branch_offset(mach->rule(), br_size, offset)) { 624 // We've got a winner. Replace this branch. 625 MachNode* replacement = mach->as_MachBranch()->short_branch_version(); 626 627 // Update the jmp_size. 628 int new_size = replacement->size(C->regalloc()); 629 int diff = br_size - new_size; 630 assert(diff >= (int)nop_size, "short_branch size should be smaller"); 631 // Conservatively take into account padding between 632 // avoid_back_to_back branches. Previous branch could be 633 // converted into avoid_back_to_back branch during next 634 // rounds. 635 if (needs_padding && replacement->avoid_back_to_back(MachNode::AVOID_BEFORE)) { 636 jmp_offset[i] += nop_size; 637 diff -= nop_size; 638 } 639 adjust_block_start += diff; 640 block->map_node(replacement, idx); 641 mach->subsume_by(replacement, C); 642 mach = replacement; 643 progress = true; 644 645 jmp_size[i] = new_size; 646 DEBUG_ONLY( jmp_target[i] = bnum; ); 647 DEBUG_ONLY( jmp_rule[i] = mach->rule(); ); 648 } else { 649 // The jump distance is not short, try again during next iteration. 650 has_short_branch_candidate = true; 651 } 652 } // (mach->may_be_short_branch()) 653 if (mach != nullptr && (mach->may_be_short_branch() || 654 mach->avoid_back_to_back(MachNode::AVOID_AFTER))) { 655 last_may_be_short_branch_adr = blk_starts[i] + jmp_offset[i] + jmp_size[i]; 656 } 657 blk_starts[i+1] -= adjust_block_start; 658 } 659 } 660 661 #ifdef ASSERT 662 for (uint i = 0; i < nblocks; i++) { // For all blocks 663 if (jmp_target[i] != 0) { 664 int br_size = jmp_size[i]; 665 int offset = blk_starts[jmp_target[i]]-(blk_starts[i] + jmp_offset[i]); 666 if (!C->matcher()->is_short_branch_offset(jmp_rule[i], br_size, offset)) { 667 tty->print_cr("target (%d) - jmp_offset(%d) = offset (%d), jump_size(%d), jmp_block B%d, target_block B%d", blk_starts[jmp_target[i]], blk_starts[i] + jmp_offset[i], offset, br_size, i, jmp_target[i]); 668 } 669 assert(C->matcher()->is_short_branch_offset(jmp_rule[i], br_size, offset), "Displacement too large for short jmp"); 670 } 671 } 672 #endif 673 674 // Step 3, compute the offsets of all blocks, will be done in fill_buffer() 675 // after ScheduleAndBundle(). 676 677 // ------------------ 678 // Compute size for code buffer 679 code_size = blk_starts[nblocks]; 680 681 // Relocation records 682 reloc_size += 1; // Relo entry for exception handler 683 684 // Adjust reloc_size to number of record of relocation info 685 // Min is 2 bytes, max is probably 6 or 8, with a tax up to 25% for 686 // a relocation index. 687 // The CodeBuffer will expand the locs array if this estimate is too low. 688 reloc_size *= 10 / sizeof(relocInfo); 689 690 _buf_sizes._reloc = reloc_size; 691 _buf_sizes._code = code_size; 692 _buf_sizes._stub = stub_size; 693 } 694 695 //------------------------------FillLocArray----------------------------------- 696 // Create a bit of debug info and append it to the array. The mapping is from 697 // Java local or expression stack to constant, register or stack-slot. For 698 // doubles, insert 2 mappings and return 1 (to tell the caller that the next 699 // entry has been taken care of and caller should skip it). 700 static LocationValue *new_loc_value( PhaseRegAlloc *ra, OptoReg::Name regnum, Location::Type l_type ) { 701 // This should never have accepted Bad before 702 assert(OptoReg::is_valid(regnum), "location must be valid"); 703 return (OptoReg::is_reg(regnum)) 704 ? new LocationValue(Location::new_reg_loc(l_type, OptoReg::as_VMReg(regnum)) ) 705 : new LocationValue(Location::new_stk_loc(l_type, ra->reg2offset(regnum))); 706 } 707 708 709 ObjectValue* 710 PhaseOutput::sv_for_node_id(GrowableArray<ScopeValue*> *objs, int id) { 711 for (int i = 0; i < objs->length(); i++) { 712 assert(objs->at(i)->is_object(), "corrupt object cache"); 713 ObjectValue* sv = (ObjectValue*) objs->at(i); 714 if (sv->id() == id) { 715 return sv; 716 } 717 } 718 // Otherwise.. 719 return nullptr; 720 } 721 722 void PhaseOutput::set_sv_for_object_node(GrowableArray<ScopeValue*> *objs, 723 ObjectValue* sv ) { 724 assert(sv_for_node_id(objs, sv->id()) == nullptr, "Precondition"); 725 objs->append(sv); 726 } 727 728 729 void PhaseOutput::FillLocArray( int idx, MachSafePointNode* sfpt, Node *local, 730 GrowableArray<ScopeValue*> *array, 731 GrowableArray<ScopeValue*> *objs ) { 732 assert( local, "use _top instead of null" ); 733 if (array->length() != idx) { 734 assert(array->length() == idx + 1, "Unexpected array count"); 735 // Old functionality: 736 // return 737 // New functionality: 738 // Assert if the local is not top. In product mode let the new node 739 // override the old entry. 740 assert(local == C->top(), "LocArray collision"); 741 if (local == C->top()) { 742 return; 743 } 744 array->pop(); 745 } 746 const Type *t = local->bottom_type(); 747 748 // Is it a safepoint scalar object node? 749 if (local->is_SafePointScalarObject()) { 750 SafePointScalarObjectNode* spobj = local->as_SafePointScalarObject(); 751 752 ObjectValue* sv = (ObjectValue*) sv_for_node_id(objs, spobj->_idx); 753 if (sv == nullptr) { 754 ciKlass* cik = t->is_oopptr()->exact_klass(); 755 assert(cik->is_instance_klass() || 756 cik->is_array_klass(), "Not supported allocation."); 757 sv = new ObjectValue(spobj->_idx, 758 new ConstantOopWriteValue(cik->java_mirror()->constant_encoding())); 759 set_sv_for_object_node(objs, sv); 760 761 uint first_ind = spobj->first_index(sfpt->jvms()); 762 for (uint i = 0; i < spobj->n_fields(); i++) { 763 Node* fld_node = sfpt->in(first_ind+i); 764 (void)FillLocArray(sv->field_values()->length(), sfpt, fld_node, sv->field_values(), objs); 765 } 766 } 767 array->append(sv); 768 return; 769 } else if (local->is_SafePointScalarMerge()) { 770 SafePointScalarMergeNode* smerge = local->as_SafePointScalarMerge(); 771 ObjectMergeValue* mv = (ObjectMergeValue*) sv_for_node_id(objs, smerge->_idx); 772 773 if (mv == NULL) { 774 GrowableArray<ScopeValue*> deps; 775 776 int merge_pointer_idx = smerge->merge_pointer_idx(sfpt->jvms()); 777 (void)FillLocArray(0, sfpt, sfpt->in(merge_pointer_idx), &deps, objs); 778 assert(deps.length() == 1, "missing value"); 779 780 int selector_idx = smerge->selector_idx(sfpt->jvms()); 781 (void)FillLocArray(1, NULL, sfpt->in(selector_idx), &deps, NULL); 782 assert(deps.length() == 2, "missing value"); 783 784 mv = new ObjectMergeValue(smerge->_idx, deps.at(0), deps.at(1)); 785 set_sv_for_object_node(objs, mv); 786 787 for (uint i = 1; i < smerge->req(); i++) { 788 Node* obj_node = smerge->in(i); 789 (void)FillLocArray(mv->possible_objects()->length(), sfpt, obj_node, mv->possible_objects(), objs); 790 } 791 } 792 array->append(mv); 793 return; 794 } 795 796 // Grab the register number for the local 797 OptoReg::Name regnum = C->regalloc()->get_reg_first(local); 798 if( OptoReg::is_valid(regnum) ) {// Got a register/stack? 799 // Record the double as two float registers. 800 // The register mask for such a value always specifies two adjacent 801 // float registers, with the lower register number even. 802 // Normally, the allocation of high and low words to these registers 803 // is irrelevant, because nearly all operations on register pairs 804 // (e.g., StoreD) treat them as a single unit. 805 // Here, we assume in addition that the words in these two registers 806 // stored "naturally" (by operations like StoreD and double stores 807 // within the interpreter) such that the lower-numbered register 808 // is written to the lower memory address. This may seem like 809 // a machine dependency, but it is not--it is a requirement on 810 // the author of the <arch>.ad file to ensure that, for every 811 // even/odd double-register pair to which a double may be allocated, 812 // the word in the even single-register is stored to the first 813 // memory word. (Note that register numbers are completely 814 // arbitrary, and are not tied to any machine-level encodings.) 815 #ifdef _LP64 816 if( t->base() == Type::DoubleBot || t->base() == Type::DoubleCon ) { 817 array->append(new ConstantIntValue((jint)0)); 818 array->append(new_loc_value( C->regalloc(), regnum, Location::dbl )); 819 } else if ( t->base() == Type::Long ) { 820 array->append(new ConstantIntValue((jint)0)); 821 array->append(new_loc_value( C->regalloc(), regnum, Location::lng )); 822 } else if ( t->base() == Type::RawPtr ) { 823 // jsr/ret return address which must be restored into the full 824 // width 64-bit stack slot. 825 array->append(new_loc_value( C->regalloc(), regnum, Location::lng )); 826 } 827 #else //_LP64 828 if( t->base() == Type::DoubleBot || t->base() == Type::DoubleCon || t->base() == Type::Long ) { 829 // Repack the double/long as two jints. 830 // The convention the interpreter uses is that the second local 831 // holds the first raw word of the native double representation. 832 // This is actually reasonable, since locals and stack arrays 833 // grow downwards in all implementations. 834 // (If, on some machine, the interpreter's Java locals or stack 835 // were to grow upwards, the embedded doubles would be word-swapped.) 836 array->append(new_loc_value( C->regalloc(), OptoReg::add(regnum,1), Location::normal )); 837 array->append(new_loc_value( C->regalloc(), regnum , Location::normal )); 838 } 839 #endif //_LP64 840 else if( (t->base() == Type::FloatBot || t->base() == Type::FloatCon) && 841 OptoReg::is_reg(regnum) ) { 842 array->append(new_loc_value( C->regalloc(), regnum, Matcher::float_in_double() 843 ? Location::float_in_dbl : Location::normal )); 844 } else if( t->base() == Type::Int && OptoReg::is_reg(regnum) ) { 845 array->append(new_loc_value( C->regalloc(), regnum, Matcher::int_in_long 846 ? Location::int_in_long : Location::normal )); 847 } else if( t->base() == Type::NarrowOop ) { 848 array->append(new_loc_value( C->regalloc(), regnum, Location::narrowoop )); 849 } else if (t->base() == Type::VectorA || t->base() == Type::VectorS || 850 t->base() == Type::VectorD || t->base() == Type::VectorX || 851 t->base() == Type::VectorY || t->base() == Type::VectorZ) { 852 array->append(new_loc_value( C->regalloc(), regnum, Location::vector )); 853 } else if (C->regalloc()->is_oop(local)) { 854 assert(t->base() == Type::OopPtr || t->base() == Type::InstPtr || 855 t->base() == Type::AryPtr, 856 "Unexpected type: %s", t->msg()); 857 array->append(new_loc_value( C->regalloc(), regnum, Location::oop )); 858 } else { 859 assert(t->base() == Type::Int || t->base() == Type::Half || 860 t->base() == Type::FloatCon || t->base() == Type::FloatBot, 861 "Unexpected type: %s", t->msg()); 862 array->append(new_loc_value( C->regalloc(), regnum, Location::normal )); 863 } 864 return; 865 } 866 867 // No register. It must be constant data. 868 switch (t->base()) { 869 case Type::Half: // Second half of a double 870 ShouldNotReachHere(); // Caller should skip 2nd halves 871 break; 872 case Type::AnyPtr: 873 array->append(new ConstantOopWriteValue(nullptr)); 874 break; 875 case Type::AryPtr: 876 case Type::InstPtr: // fall through 877 array->append(new ConstantOopWriteValue(t->isa_oopptr()->const_oop()->constant_encoding())); 878 break; 879 case Type::NarrowOop: 880 if (t == TypeNarrowOop::NULL_PTR) { 881 array->append(new ConstantOopWriteValue(nullptr)); 882 } else { 883 array->append(new ConstantOopWriteValue(t->make_ptr()->isa_oopptr()->const_oop()->constant_encoding())); 884 } 885 break; 886 case Type::Int: 887 array->append(new ConstantIntValue(t->is_int()->get_con())); 888 break; 889 case Type::RawPtr: 890 // A return address (T_ADDRESS). 891 assert((intptr_t)t->is_ptr()->get_con() < (intptr_t)0x10000, "must be a valid BCI"); 892 #ifdef _LP64 893 // Must be restored to the full-width 64-bit stack slot. 894 array->append(new ConstantLongValue(t->is_ptr()->get_con())); 895 #else 896 array->append(new ConstantIntValue(t->is_ptr()->get_con())); 897 #endif 898 break; 899 case Type::FloatCon: { 900 float f = t->is_float_constant()->getf(); 901 array->append(new ConstantIntValue(jint_cast(f))); 902 break; 903 } 904 case Type::DoubleCon: { 905 jdouble d = t->is_double_constant()->getd(); 906 #ifdef _LP64 907 array->append(new ConstantIntValue((jint)0)); 908 array->append(new ConstantDoubleValue(d)); 909 #else 910 // Repack the double as two jints. 911 // The convention the interpreter uses is that the second local 912 // holds the first raw word of the native double representation. 913 // This is actually reasonable, since locals and stack arrays 914 // grow downwards in all implementations. 915 // (If, on some machine, the interpreter's Java locals or stack 916 // were to grow upwards, the embedded doubles would be word-swapped.) 917 jlong_accessor acc; 918 acc.long_value = jlong_cast(d); 919 array->append(new ConstantIntValue(acc.words[1])); 920 array->append(new ConstantIntValue(acc.words[0])); 921 #endif 922 break; 923 } 924 case Type::Long: { 925 jlong d = t->is_long()->get_con(); 926 #ifdef _LP64 927 array->append(new ConstantIntValue((jint)0)); 928 array->append(new ConstantLongValue(d)); 929 #else 930 // Repack the long as two jints. 931 // The convention the interpreter uses is that the second local 932 // holds the first raw word of the native double representation. 933 // This is actually reasonable, since locals and stack arrays 934 // grow downwards in all implementations. 935 // (If, on some machine, the interpreter's Java locals or stack 936 // were to grow upwards, the embedded doubles would be word-swapped.) 937 jlong_accessor acc; 938 acc.long_value = d; 939 array->append(new ConstantIntValue(acc.words[1])); 940 array->append(new ConstantIntValue(acc.words[0])); 941 #endif 942 break; 943 } 944 case Type::Top: // Add an illegal value here 945 array->append(new LocationValue(Location())); 946 break; 947 default: 948 ShouldNotReachHere(); 949 break; 950 } 951 } 952 953 // Determine if this node starts a bundle 954 bool PhaseOutput::starts_bundle(const Node *n) const { 955 return (_node_bundling_limit > n->_idx && 956 _node_bundling_base[n->_idx].starts_bundle()); 957 } 958 959 // Determine if there is a monitor that has 'ov' as its owner. 960 bool PhaseOutput::contains_as_owner(GrowableArray<MonitorValue*> *monarray, ObjectValue *ov) const { 961 for (int k = 0; k < monarray->length(); k++) { 962 MonitorValue* mv = monarray->at(k); 963 if (mv->owner() == ov) { 964 return true; 965 } 966 } 967 968 return false; 969 } 970 971 //--------------------------Process_OopMap_Node-------------------------------- 972 void PhaseOutput::Process_OopMap_Node(MachNode *mach, int current_offset) { 973 // Handle special safepoint nodes for synchronization 974 MachSafePointNode *sfn = mach->as_MachSafePoint(); 975 MachCallNode *mcall; 976 977 int safepoint_pc_offset = current_offset; 978 bool is_method_handle_invoke = false; 979 bool return_oop = false; 980 bool has_ea_local_in_scope = sfn->_has_ea_local_in_scope; 981 bool arg_escape = false; 982 983 // Add the safepoint in the DebugInfoRecorder 984 if( !mach->is_MachCall() ) { 985 mcall = nullptr; 986 C->debug_info()->add_safepoint(safepoint_pc_offset, sfn->_oop_map); 987 } else { 988 mcall = mach->as_MachCall(); 989 990 // Is the call a MethodHandle call? 991 if (mcall->is_MachCallJava()) { 992 if (mcall->as_MachCallJava()->_method_handle_invoke) { 993 assert(C->has_method_handle_invokes(), "must have been set during call generation"); 994 is_method_handle_invoke = true; 995 } 996 arg_escape = mcall->as_MachCallJava()->_arg_escape; 997 } 998 999 // Check if a call returns an object. 1000 if (mcall->returns_pointer()) { 1001 return_oop = true; 1002 } 1003 safepoint_pc_offset += mcall->ret_addr_offset(); 1004 C->debug_info()->add_safepoint(safepoint_pc_offset, mcall->_oop_map); 1005 } 1006 1007 // Loop over the JVMState list to add scope information 1008 // Do not skip safepoints with a null method, they need monitor info 1009 JVMState* youngest_jvms = sfn->jvms(); 1010 int max_depth = youngest_jvms->depth(); 1011 1012 // Allocate the object pool for scalar-replaced objects -- the map from 1013 // small-integer keys (which can be recorded in the local and ostack 1014 // arrays) to descriptions of the object state. 1015 GrowableArray<ScopeValue*> *objs = new GrowableArray<ScopeValue*>(); 1016 1017 // Visit scopes from oldest to youngest. 1018 for (int depth = 1; depth <= max_depth; depth++) { 1019 JVMState* jvms = youngest_jvms->of_depth(depth); 1020 int idx; 1021 ciMethod* method = jvms->has_method() ? jvms->method() : nullptr; 1022 // Safepoints that do not have method() set only provide oop-map and monitor info 1023 // to support GC; these do not support deoptimization. 1024 int num_locs = (method == nullptr) ? 0 : jvms->loc_size(); 1025 int num_exps = (method == nullptr) ? 0 : jvms->stk_size(); 1026 int num_mon = jvms->nof_monitors(); 1027 assert(method == nullptr || jvms->bci() < 0 || num_locs == method->max_locals(), 1028 "JVMS local count must match that of the method"); 1029 1030 // Add Local and Expression Stack Information 1031 1032 // Insert locals into the locarray 1033 GrowableArray<ScopeValue*> *locarray = new GrowableArray<ScopeValue*>(num_locs); 1034 for( idx = 0; idx < num_locs; idx++ ) { 1035 FillLocArray( idx, sfn, sfn->local(jvms, idx), locarray, objs ); 1036 } 1037 1038 // Insert expression stack entries into the exparray 1039 GrowableArray<ScopeValue*> *exparray = new GrowableArray<ScopeValue*>(num_exps); 1040 for( idx = 0; idx < num_exps; idx++ ) { 1041 FillLocArray( idx, sfn, sfn->stack(jvms, idx), exparray, objs ); 1042 } 1043 1044 // Add in mappings of the monitors 1045 assert( !method || 1046 !method->is_synchronized() || 1047 method->is_native() || 1048 num_mon > 0 || 1049 !GenerateSynchronizationCode, 1050 "monitors must always exist for synchronized methods"); 1051 1052 // Build the growable array of ScopeValues for exp stack 1053 GrowableArray<MonitorValue*> *monarray = new GrowableArray<MonitorValue*>(num_mon); 1054 1055 // Loop over monitors and insert into array 1056 for (idx = 0; idx < num_mon; idx++) { 1057 // Grab the node that defines this monitor 1058 Node* box_node = sfn->monitor_box(jvms, idx); 1059 Node* obj_node = sfn->monitor_obj(jvms, idx); 1060 1061 // Create ScopeValue for object 1062 ScopeValue *scval = nullptr; 1063 1064 if (obj_node->is_SafePointScalarObject()) { 1065 SafePointScalarObjectNode* spobj = obj_node->as_SafePointScalarObject(); 1066 scval = PhaseOutput::sv_for_node_id(objs, spobj->_idx); 1067 if (scval == nullptr) { 1068 const Type *t = spobj->bottom_type(); 1069 ciKlass* cik = t->is_oopptr()->exact_klass(); 1070 assert(cik->is_instance_klass() || 1071 cik->is_array_klass(), "Not supported allocation."); 1072 ObjectValue* sv = new ObjectValue(spobj->_idx, 1073 new ConstantOopWriteValue(cik->java_mirror()->constant_encoding())); 1074 PhaseOutput::set_sv_for_object_node(objs, sv); 1075 1076 uint first_ind = spobj->first_index(youngest_jvms); 1077 for (uint i = 0; i < spobj->n_fields(); i++) { 1078 Node* fld_node = sfn->in(first_ind+i); 1079 (void)FillLocArray(sv->field_values()->length(), sfn, fld_node, sv->field_values(), objs); 1080 } 1081 scval = sv; 1082 } 1083 } else if (!obj_node->is_Con()) { 1084 OptoReg::Name obj_reg = C->regalloc()->get_reg_first(obj_node); 1085 if( obj_node->bottom_type()->base() == Type::NarrowOop ) { 1086 scval = new_loc_value( C->regalloc(), obj_reg, Location::narrowoop ); 1087 } else { 1088 scval = new_loc_value( C->regalloc(), obj_reg, Location::oop ); 1089 } 1090 } else { 1091 const TypePtr *tp = obj_node->get_ptr_type(); 1092 scval = new ConstantOopWriteValue(tp->is_oopptr()->const_oop()->constant_encoding()); 1093 } 1094 1095 OptoReg::Name box_reg = BoxLockNode::reg(box_node); 1096 Location basic_lock = Location::new_stk_loc(Location::normal,C->regalloc()->reg2offset(box_reg)); 1097 bool eliminated = (box_node->is_BoxLock() && box_node->as_BoxLock()->is_eliminated()); 1098 monarray->append(new MonitorValue(scval, basic_lock, eliminated)); 1099 } 1100 1101 // Mark ObjectValue nodes as root nodes if they are directly 1102 // referenced in the JVMS. 1103 for (int i = 0; i < objs->length(); i++) { 1104 ScopeValue* sv = objs->at(i); 1105 if (sv->is_object_merge()) { 1106 ObjectMergeValue* merge = sv->as_ObjectMergeValue(); 1107 1108 for (int j = 0; j< merge->possible_objects()->length(); j++) { 1109 ObjectValue* ov = merge->possible_objects()->at(j)->as_ObjectValue(); 1110 bool is_root = locarray->contains(ov) || exparray->contains(ov) || contains_as_owner(monarray, ov); 1111 ov->set_root(is_root); 1112 } 1113 } 1114 } 1115 1116 // We dump the object pool first, since deoptimization reads it in first. 1117 C->debug_info()->dump_object_pool(objs); 1118 1119 // Build first class objects to pass to scope 1120 DebugToken *locvals = C->debug_info()->create_scope_values(locarray); 1121 DebugToken *expvals = C->debug_info()->create_scope_values(exparray); 1122 DebugToken *monvals = C->debug_info()->create_monitor_values(monarray); 1123 1124 // Make method available for all Safepoints 1125 ciMethod* scope_method = method ? method : C->method(); 1126 // Describe the scope here 1127 assert(jvms->bci() >= InvocationEntryBci && jvms->bci() <= 0x10000, "must be a valid or entry BCI"); 1128 assert(!jvms->should_reexecute() || depth == max_depth, "reexecute allowed only for the youngest"); 1129 // Now we can describe the scope. 1130 methodHandle null_mh; 1131 bool rethrow_exception = false; 1132 C->debug_info()->describe_scope( 1133 safepoint_pc_offset, 1134 null_mh, 1135 scope_method, 1136 jvms->bci(), 1137 jvms->should_reexecute(), 1138 rethrow_exception, 1139 is_method_handle_invoke, 1140 return_oop, 1141 has_ea_local_in_scope, 1142 arg_escape, 1143 locvals, 1144 expvals, 1145 monvals 1146 ); 1147 } // End jvms loop 1148 1149 // Mark the end of the scope set. 1150 C->debug_info()->end_safepoint(safepoint_pc_offset); 1151 } 1152 1153 1154 1155 // A simplified version of Process_OopMap_Node, to handle non-safepoints. 1156 class NonSafepointEmitter { 1157 Compile* C; 1158 JVMState* _pending_jvms; 1159 int _pending_offset; 1160 1161 void emit_non_safepoint(); 1162 1163 public: 1164 NonSafepointEmitter(Compile* compile) { 1165 this->C = compile; 1166 _pending_jvms = nullptr; 1167 _pending_offset = 0; 1168 } 1169 1170 void observe_instruction(Node* n, int pc_offset) { 1171 if (!C->debug_info()->recording_non_safepoints()) return; 1172 1173 Node_Notes* nn = C->node_notes_at(n->_idx); 1174 if (nn == nullptr || nn->jvms() == nullptr) return; 1175 if (_pending_jvms != nullptr && 1176 _pending_jvms->same_calls_as(nn->jvms())) { 1177 // Repeated JVMS? Stretch it up here. 1178 _pending_offset = pc_offset; 1179 } else { 1180 if (_pending_jvms != nullptr && 1181 _pending_offset < pc_offset) { 1182 emit_non_safepoint(); 1183 } 1184 _pending_jvms = nullptr; 1185 if (pc_offset > C->debug_info()->last_pc_offset()) { 1186 // This is the only way _pending_jvms can become non-null: 1187 _pending_jvms = nn->jvms(); 1188 _pending_offset = pc_offset; 1189 } 1190 } 1191 } 1192 1193 // Stay out of the way of real safepoints: 1194 void observe_safepoint(JVMState* jvms, int pc_offset) { 1195 if (_pending_jvms != nullptr && 1196 !_pending_jvms->same_calls_as(jvms) && 1197 _pending_offset < pc_offset) { 1198 emit_non_safepoint(); 1199 } 1200 _pending_jvms = nullptr; 1201 } 1202 1203 void flush_at_end() { 1204 if (_pending_jvms != nullptr) { 1205 emit_non_safepoint(); 1206 } 1207 _pending_jvms = nullptr; 1208 } 1209 }; 1210 1211 void NonSafepointEmitter::emit_non_safepoint() { 1212 JVMState* youngest_jvms = _pending_jvms; 1213 int pc_offset = _pending_offset; 1214 1215 // Clear it now: 1216 _pending_jvms = nullptr; 1217 1218 DebugInformationRecorder* debug_info = C->debug_info(); 1219 assert(debug_info->recording_non_safepoints(), "sanity"); 1220 1221 debug_info->add_non_safepoint(pc_offset); 1222 int max_depth = youngest_jvms->depth(); 1223 1224 // Visit scopes from oldest to youngest. 1225 for (int depth = 1; depth <= max_depth; depth++) { 1226 JVMState* jvms = youngest_jvms->of_depth(depth); 1227 ciMethod* method = jvms->has_method() ? jvms->method() : nullptr; 1228 assert(!jvms->should_reexecute() || depth==max_depth, "reexecute allowed only for the youngest"); 1229 methodHandle null_mh; 1230 debug_info->describe_scope(pc_offset, null_mh, method, jvms->bci(), jvms->should_reexecute()); 1231 } 1232 1233 // Mark the end of the scope set. 1234 debug_info->end_non_safepoint(pc_offset); 1235 } 1236 1237 //------------------------------init_buffer------------------------------------ 1238 void PhaseOutput::estimate_buffer_size(int& const_req) { 1239 1240 // Set the initially allocated size 1241 const_req = initial_const_capacity; 1242 1243 // The extra spacing after the code is necessary on some platforms. 1244 // Sometimes we need to patch in a jump after the last instruction, 1245 // if the nmethod has been deoptimized. (See 4932387, 4894843.) 1246 1247 // Compute the byte offset where we can store the deopt pc. 1248 if (C->fixed_slots() != 0) { 1249 _orig_pc_slot_offset_in_bytes = C->regalloc()->reg2offset(OptoReg::stack2reg(_orig_pc_slot)); 1250 } 1251 1252 // Compute prolog code size 1253 _method_size = 0; 1254 _frame_slots = OptoReg::reg2stack(C->matcher()->_old_SP) + C->regalloc()->_framesize; 1255 assert(_frame_slots >= 0 && _frame_slots < 1000000, "sanity check"); 1256 1257 if (C->has_mach_constant_base_node()) { 1258 uint add_size = 0; 1259 // Fill the constant table. 1260 // Note: This must happen before shorten_branches. 1261 for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) { 1262 Block* b = C->cfg()->get_block(i); 1263 1264 for (uint j = 0; j < b->number_of_nodes(); j++) { 1265 Node* n = b->get_node(j); 1266 1267 // If the node is a MachConstantNode evaluate the constant 1268 // value section. 1269 if (n->is_MachConstant()) { 1270 MachConstantNode* machcon = n->as_MachConstant(); 1271 machcon->eval_constant(C); 1272 } else if (n->is_Mach()) { 1273 // On Power there are more nodes that issue constants. 1274 add_size += (n->as_Mach()->ins_num_consts() * 8); 1275 } 1276 } 1277 } 1278 1279 // Calculate the offsets of the constants and the size of the 1280 // constant table (including the padding to the next section). 1281 constant_table().calculate_offsets_and_size(); 1282 const_req = constant_table().size() + add_size; 1283 } 1284 1285 // Initialize the space for the BufferBlob used to find and verify 1286 // instruction size in MachNode::emit_size() 1287 init_scratch_buffer_blob(const_req); 1288 } 1289 1290 CodeBuffer* PhaseOutput::init_buffer() { 1291 int stub_req = _buf_sizes._stub; 1292 int code_req = _buf_sizes._code; 1293 int const_req = _buf_sizes._const; 1294 1295 int pad_req = NativeCall::instruction_size; 1296 1297 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 1298 stub_req += bs->estimate_stub_size(); 1299 1300 // nmethod and CodeBuffer count stubs & constants as part of method's code. 1301 // class HandlerImpl is platform-specific and defined in the *.ad files. 1302 int exception_handler_req = HandlerImpl::size_exception_handler() + MAX_stubs_size; // add marginal slop for handler 1303 int deopt_handler_req = HandlerImpl::size_deopt_handler() + MAX_stubs_size; // add marginal slop for handler 1304 stub_req += MAX_stubs_size; // ensure per-stub margin 1305 code_req += MAX_inst_size; // ensure per-instruction margin 1306 1307 if (StressCodeBuffers) 1308 code_req = const_req = stub_req = exception_handler_req = deopt_handler_req = 0x10; // force expansion 1309 1310 int total_req = 1311 const_req + 1312 code_req + 1313 pad_req + 1314 stub_req + 1315 exception_handler_req + 1316 deopt_handler_req; // deopt handler 1317 1318 if (C->has_method_handle_invokes()) 1319 total_req += deopt_handler_req; // deopt MH handler 1320 1321 CodeBuffer* cb = code_buffer(); 1322 cb->initialize(total_req, _buf_sizes._reloc); 1323 1324 // Have we run out of code space? 1325 if ((cb->blob() == nullptr) || (!CompileBroker::should_compile_new_jobs())) { 1326 C->record_failure("CodeCache is full"); 1327 return nullptr; 1328 } 1329 // Configure the code buffer. 1330 cb->initialize_consts_size(const_req); 1331 cb->initialize_stubs_size(stub_req); 1332 cb->initialize_oop_recorder(C->env()->oop_recorder()); 1333 1334 // fill in the nop array for bundling computations 1335 MachNode *_nop_list[Bundle::_nop_count]; 1336 Bundle::initialize_nops(_nop_list); 1337 1338 return cb; 1339 } 1340 1341 //------------------------------fill_buffer------------------------------------ 1342 void PhaseOutput::fill_buffer(CodeBuffer* cb, uint* blk_starts) { 1343 // blk_starts[] contains offsets calculated during short branches processing, 1344 // offsets should not be increased during following steps. 1345 1346 // Compute the size of first NumberOfLoopInstrToAlign instructions at head 1347 // of a loop. It is used to determine the padding for loop alignment. 1348 Compile::TracePhase tp("fill buffer", &timers[_t_fillBuffer]); 1349 1350 compute_loop_first_inst_sizes(); 1351 1352 // Create oopmap set. 1353 _oop_map_set = new OopMapSet(); 1354 1355 // !!!!! This preserves old handling of oopmaps for now 1356 C->debug_info()->set_oopmaps(_oop_map_set); 1357 1358 uint nblocks = C->cfg()->number_of_blocks(); 1359 // Count and start of implicit null check instructions 1360 uint inct_cnt = 0; 1361 uint* inct_starts = NEW_RESOURCE_ARRAY(uint, nblocks+1); 1362 1363 // Count and start of calls 1364 uint* call_returns = NEW_RESOURCE_ARRAY(uint, nblocks+1); 1365 1366 uint return_offset = 0; 1367 int nop_size = (new MachNopNode())->size(C->regalloc()); 1368 1369 int previous_offset = 0; 1370 int current_offset = 0; 1371 int last_call_offset = -1; 1372 int last_avoid_back_to_back_offset = -1; 1373 #ifdef ASSERT 1374 uint* jmp_target = NEW_RESOURCE_ARRAY(uint,nblocks); 1375 uint* jmp_offset = NEW_RESOURCE_ARRAY(uint,nblocks); 1376 uint* jmp_size = NEW_RESOURCE_ARRAY(uint,nblocks); 1377 uint* jmp_rule = NEW_RESOURCE_ARRAY(uint,nblocks); 1378 #endif 1379 1380 // Create an array of unused labels, one for each basic block, if printing is enabled 1381 #if defined(SUPPORT_OPTO_ASSEMBLY) 1382 int* node_offsets = nullptr; 1383 uint node_offset_limit = C->unique(); 1384 1385 if (C->print_assembly()) { 1386 node_offsets = NEW_RESOURCE_ARRAY(int, node_offset_limit); 1387 } 1388 if (node_offsets != nullptr) { 1389 // We need to initialize. Unused array elements may contain garbage and mess up PrintOptoAssembly. 1390 memset(node_offsets, 0, node_offset_limit*sizeof(int)); 1391 } 1392 #endif 1393 1394 NonSafepointEmitter non_safepoints(C); // emit non-safepoints lazily 1395 1396 // Emit the constant table. 1397 if (C->has_mach_constant_base_node()) { 1398 if (!constant_table().emit(*cb)) { 1399 C->record_failure("consts section overflow"); 1400 return; 1401 } 1402 } 1403 1404 // Create an array of labels, one for each basic block 1405 Label* blk_labels = NEW_RESOURCE_ARRAY(Label, nblocks+1); 1406 for (uint i = 0; i <= nblocks; i++) { 1407 blk_labels[i].init(); 1408 } 1409 1410 // Now fill in the code buffer 1411 Node* delay_slot = nullptr; 1412 for (uint i = 0; i < nblocks; i++) { 1413 Block* block = C->cfg()->get_block(i); 1414 _block = block; 1415 Node* head = block->head(); 1416 1417 // If this block needs to start aligned (i.e, can be reached other 1418 // than by falling-thru from the previous block), then force the 1419 // start of a new bundle. 1420 if (Pipeline::requires_bundling() && starts_bundle(head)) { 1421 cb->flush_bundle(true); 1422 } 1423 1424 #ifdef ASSERT 1425 if (!block->is_connector()) { 1426 stringStream st; 1427 block->dump_head(C->cfg(), &st); 1428 MacroAssembler(cb).block_comment(st.freeze()); 1429 } 1430 jmp_target[i] = 0; 1431 jmp_offset[i] = 0; 1432 jmp_size[i] = 0; 1433 jmp_rule[i] = 0; 1434 #endif 1435 int blk_offset = current_offset; 1436 1437 // Define the label at the beginning of the basic block 1438 MacroAssembler(cb).bind(blk_labels[block->_pre_order]); 1439 1440 uint last_inst = block->number_of_nodes(); 1441 1442 // Emit block normally, except for last instruction. 1443 // Emit means "dump code bits into code buffer". 1444 for (uint j = 0; j<last_inst; j++) { 1445 _index = j; 1446 1447 // Get the node 1448 Node* n = block->get_node(j); 1449 1450 // See if delay slots are supported 1451 if (valid_bundle_info(n) && node_bundling(n)->used_in_unconditional_delay()) { 1452 assert(delay_slot == nullptr, "no use of delay slot node"); 1453 assert(n->size(C->regalloc()) == Pipeline::instr_unit_size(), "delay slot instruction wrong size"); 1454 1455 delay_slot = n; 1456 continue; 1457 } 1458 1459 // If this starts a new instruction group, then flush the current one 1460 // (but allow split bundles) 1461 if (Pipeline::requires_bundling() && starts_bundle(n)) 1462 cb->flush_bundle(false); 1463 1464 // Special handling for SafePoint/Call Nodes 1465 bool is_mcall = false; 1466 if (n->is_Mach()) { 1467 MachNode *mach = n->as_Mach(); 1468 is_mcall = n->is_MachCall(); 1469 bool is_sfn = n->is_MachSafePoint(); 1470 1471 // If this requires all previous instructions be flushed, then do so 1472 if (is_sfn || is_mcall || mach->alignment_required() != 1) { 1473 cb->flush_bundle(true); 1474 current_offset = cb->insts_size(); 1475 } 1476 1477 // A padding may be needed again since a previous instruction 1478 // could be moved to delay slot. 1479 1480 // align the instruction if necessary 1481 int padding = mach->compute_padding(current_offset); 1482 // Make sure safepoint node for polling is distinct from a call's 1483 // return by adding a nop if needed. 1484 if (is_sfn && !is_mcall && padding == 0 && current_offset == last_call_offset) { 1485 padding = nop_size; 1486 } 1487 if (padding == 0 && mach->avoid_back_to_back(MachNode::AVOID_BEFORE) && 1488 current_offset == last_avoid_back_to_back_offset) { 1489 // Avoid back to back some instructions. 1490 padding = nop_size; 1491 } 1492 1493 if (padding > 0) { 1494 assert((padding % nop_size) == 0, "padding is not a multiple of NOP size"); 1495 int nops_cnt = padding / nop_size; 1496 MachNode *nop = new MachNopNode(nops_cnt); 1497 block->insert_node(nop, j++); 1498 last_inst++; 1499 C->cfg()->map_node_to_block(nop, block); 1500 // Ensure enough space. 1501 cb->insts()->maybe_expand_to_ensure_remaining(MAX_inst_size); 1502 if ((cb->blob() == nullptr) || (!CompileBroker::should_compile_new_jobs())) { 1503 C->record_failure("CodeCache is full"); 1504 return; 1505 } 1506 nop->emit(*cb, C->regalloc()); 1507 cb->flush_bundle(true); 1508 current_offset = cb->insts_size(); 1509 } 1510 1511 bool observe_safepoint = is_sfn; 1512 // Remember the start of the last call in a basic block 1513 if (is_mcall) { 1514 MachCallNode *mcall = mach->as_MachCall(); 1515 1516 // This destination address is NOT PC-relative 1517 mcall->method_set((intptr_t)mcall->entry_point()); 1518 1519 // Save the return address 1520 call_returns[block->_pre_order] = current_offset + mcall->ret_addr_offset(); 1521 1522 observe_safepoint = mcall->guaranteed_safepoint(); 1523 } 1524 1525 // sfn will be valid whenever mcall is valid now because of inheritance 1526 if (observe_safepoint) { 1527 // Handle special safepoint nodes for synchronization 1528 if (!is_mcall) { 1529 MachSafePointNode *sfn = mach->as_MachSafePoint(); 1530 // !!!!! Stubs only need an oopmap right now, so bail out 1531 if (sfn->jvms()->method() == nullptr) { 1532 // Write the oopmap directly to the code blob??!! 1533 continue; 1534 } 1535 } // End synchronization 1536 1537 non_safepoints.observe_safepoint(mach->as_MachSafePoint()->jvms(), 1538 current_offset); 1539 Process_OopMap_Node(mach, current_offset); 1540 } // End if safepoint 1541 1542 // If this is a null check, then add the start of the previous instruction to the list 1543 else if( mach->is_MachNullCheck() ) { 1544 inct_starts[inct_cnt++] = previous_offset; 1545 } 1546 1547 // If this is a branch, then fill in the label with the target BB's label 1548 else if (mach->is_MachBranch()) { 1549 // This requires the TRUE branch target be in succs[0] 1550 uint block_num = block->non_connector_successor(0)->_pre_order; 1551 1552 // Try to replace long branch if delay slot is not used, 1553 // it is mostly for back branches since forward branch's 1554 // distance is not updated yet. 1555 bool delay_slot_is_used = valid_bundle_info(n) && 1556 C->output()->node_bundling(n)->use_unconditional_delay(); 1557 if (!delay_slot_is_used && mach->may_be_short_branch()) { 1558 assert(delay_slot == nullptr, "not expecting delay slot node"); 1559 int br_size = n->size(C->regalloc()); 1560 int offset = blk_starts[block_num] - current_offset; 1561 if (block_num >= i) { 1562 // Current and following block's offset are not 1563 // finalized yet, adjust distance by the difference 1564 // between calculated and final offsets of current block. 1565 offset -= (blk_starts[i] - blk_offset); 1566 } 1567 // In the following code a nop could be inserted before 1568 // the branch which will increase the backward distance. 1569 bool needs_padding = (current_offset == last_avoid_back_to_back_offset); 1570 if (needs_padding && offset <= 0) 1571 offset -= nop_size; 1572 1573 if (C->matcher()->is_short_branch_offset(mach->rule(), br_size, offset)) { 1574 // We've got a winner. Replace this branch. 1575 MachNode* replacement = mach->as_MachBranch()->short_branch_version(); 1576 1577 // Update the jmp_size. 1578 int new_size = replacement->size(C->regalloc()); 1579 assert((br_size - new_size) >= (int)nop_size, "short_branch size should be smaller"); 1580 // Insert padding between avoid_back_to_back branches. 1581 if (needs_padding && replacement->avoid_back_to_back(MachNode::AVOID_BEFORE)) { 1582 MachNode *nop = new MachNopNode(); 1583 block->insert_node(nop, j++); 1584 C->cfg()->map_node_to_block(nop, block); 1585 last_inst++; 1586 nop->emit(*cb, C->regalloc()); 1587 cb->flush_bundle(true); 1588 current_offset = cb->insts_size(); 1589 } 1590 #ifdef ASSERT 1591 jmp_target[i] = block_num; 1592 jmp_offset[i] = current_offset - blk_offset; 1593 jmp_size[i] = new_size; 1594 jmp_rule[i] = mach->rule(); 1595 #endif 1596 block->map_node(replacement, j); 1597 mach->subsume_by(replacement, C); 1598 n = replacement; 1599 mach = replacement; 1600 } 1601 } 1602 mach->as_MachBranch()->label_set( &blk_labels[block_num], block_num ); 1603 } else if (mach->ideal_Opcode() == Op_Jump) { 1604 for (uint h = 0; h < block->_num_succs; h++) { 1605 Block* succs_block = block->_succs[h]; 1606 for (uint j = 1; j < succs_block->num_preds(); j++) { 1607 Node* jpn = succs_block->pred(j); 1608 if (jpn->is_JumpProj() && jpn->in(0) == mach) { 1609 uint block_num = succs_block->non_connector()->_pre_order; 1610 Label *blkLabel = &blk_labels[block_num]; 1611 mach->add_case_label(jpn->as_JumpProj()->proj_no(), blkLabel); 1612 } 1613 } 1614 } 1615 } 1616 #ifdef ASSERT 1617 // Check that oop-store precedes the card-mark 1618 else if (mach->ideal_Opcode() == Op_StoreCM) { 1619 uint storeCM_idx = j; 1620 int count = 0; 1621 for (uint prec = mach->req(); prec < mach->len(); prec++) { 1622 Node *oop_store = mach->in(prec); // Precedence edge 1623 if (oop_store == nullptr) continue; 1624 count++; 1625 uint i4; 1626 for (i4 = 0; i4 < last_inst; ++i4) { 1627 if (block->get_node(i4) == oop_store) { 1628 break; 1629 } 1630 } 1631 // Note: This test can provide a false failure if other precedence 1632 // edges have been added to the storeCMNode. 1633 assert(i4 == last_inst || i4 < storeCM_idx, "CM card-mark executes before oop-store"); 1634 } 1635 assert(count > 0, "storeCM expects at least one precedence edge"); 1636 } 1637 #endif 1638 else if (!n->is_Proj()) { 1639 // Remember the beginning of the previous instruction, in case 1640 // it's followed by a flag-kill and a null-check. Happens on 1641 // Intel all the time, with add-to-memory kind of opcodes. 1642 previous_offset = current_offset; 1643 } 1644 1645 // Not an else-if! 1646 // If this is a trap based cmp then add its offset to the list. 1647 if (mach->is_TrapBasedCheckNode()) { 1648 inct_starts[inct_cnt++] = current_offset; 1649 } 1650 } 1651 1652 // Verify that there is sufficient space remaining 1653 cb->insts()->maybe_expand_to_ensure_remaining(MAX_inst_size); 1654 if ((cb->blob() == nullptr) || (!CompileBroker::should_compile_new_jobs())) { 1655 C->record_failure("CodeCache is full"); 1656 return; 1657 } 1658 1659 // Save the offset for the listing 1660 #if defined(SUPPORT_OPTO_ASSEMBLY) 1661 if ((node_offsets != nullptr) && (n->_idx < node_offset_limit)) { 1662 node_offsets[n->_idx] = cb->insts_size(); 1663 } 1664 #endif 1665 assert(!C->failing(), "Should not reach here if failing."); 1666 1667 // "Normal" instruction case 1668 DEBUG_ONLY(uint instr_offset = cb->insts_size()); 1669 n->emit(*cb, C->regalloc()); 1670 current_offset = cb->insts_size(); 1671 1672 // Above we only verified that there is enough space in the instruction section. 1673 // However, the instruction may emit stubs that cause code buffer expansion. 1674 // Bail out here if expansion failed due to a lack of code cache space. 1675 if (C->failing()) { 1676 return; 1677 } 1678 1679 assert(!is_mcall || (call_returns[block->_pre_order] <= (uint)current_offset), 1680 "ret_addr_offset() not within emitted code"); 1681 1682 #ifdef ASSERT 1683 uint n_size = n->size(C->regalloc()); 1684 if (n_size < (current_offset-instr_offset)) { 1685 MachNode* mach = n->as_Mach(); 1686 n->dump(); 1687 mach->dump_format(C->regalloc(), tty); 1688 tty->print_cr(" n_size (%d), current_offset (%d), instr_offset (%d)", n_size, current_offset, instr_offset); 1689 Disassembler::decode(cb->insts_begin() + instr_offset, cb->insts_begin() + current_offset + 1, tty); 1690 tty->print_cr(" ------------------- "); 1691 BufferBlob* blob = this->scratch_buffer_blob(); 1692 address blob_begin = blob->content_begin(); 1693 Disassembler::decode(blob_begin, blob_begin + n_size + 1, tty); 1694 assert(false, "wrong size of mach node"); 1695 } 1696 #endif 1697 non_safepoints.observe_instruction(n, current_offset); 1698 1699 // mcall is last "call" that can be a safepoint 1700 // record it so we can see if a poll will directly follow it 1701 // in which case we'll need a pad to make the PcDesc sites unique 1702 // see 5010568. This can be slightly inaccurate but conservative 1703 // in the case that return address is not actually at current_offset. 1704 // This is a small price to pay. 1705 1706 if (is_mcall) { 1707 last_call_offset = current_offset; 1708 } 1709 1710 if (n->is_Mach() && n->as_Mach()->avoid_back_to_back(MachNode::AVOID_AFTER)) { 1711 // Avoid back to back some instructions. 1712 last_avoid_back_to_back_offset = current_offset; 1713 } 1714 1715 // See if this instruction has a delay slot 1716 if (valid_bundle_info(n) && node_bundling(n)->use_unconditional_delay()) { 1717 guarantee(delay_slot != nullptr, "expecting delay slot node"); 1718 1719 // Back up 1 instruction 1720 cb->set_insts_end(cb->insts_end() - Pipeline::instr_unit_size()); 1721 1722 // Save the offset for the listing 1723 #if defined(SUPPORT_OPTO_ASSEMBLY) 1724 if ((node_offsets != nullptr) && (delay_slot->_idx < node_offset_limit)) { 1725 node_offsets[delay_slot->_idx] = cb->insts_size(); 1726 } 1727 #endif 1728 1729 // Support a SafePoint in the delay slot 1730 if (delay_slot->is_MachSafePoint()) { 1731 MachNode *mach = delay_slot->as_Mach(); 1732 // !!!!! Stubs only need an oopmap right now, so bail out 1733 if (!mach->is_MachCall() && mach->as_MachSafePoint()->jvms()->method() == nullptr) { 1734 // Write the oopmap directly to the code blob??!! 1735 delay_slot = nullptr; 1736 continue; 1737 } 1738 1739 int adjusted_offset = current_offset - Pipeline::instr_unit_size(); 1740 non_safepoints.observe_safepoint(mach->as_MachSafePoint()->jvms(), 1741 adjusted_offset); 1742 // Generate an OopMap entry 1743 Process_OopMap_Node(mach, adjusted_offset); 1744 } 1745 1746 // Insert the delay slot instruction 1747 delay_slot->emit(*cb, C->regalloc()); 1748 1749 // Don't reuse it 1750 delay_slot = nullptr; 1751 } 1752 1753 } // End for all instructions in block 1754 1755 // If the next block is the top of a loop, pad this block out to align 1756 // the loop top a little. Helps prevent pipe stalls at loop back branches. 1757 if (i < nblocks-1) { 1758 Block *nb = C->cfg()->get_block(i + 1); 1759 int padding = nb->alignment_padding(current_offset); 1760 if( padding > 0 ) { 1761 MachNode *nop = new MachNopNode(padding / nop_size); 1762 block->insert_node(nop, block->number_of_nodes()); 1763 C->cfg()->map_node_to_block(nop, block); 1764 nop->emit(*cb, C->regalloc()); 1765 current_offset = cb->insts_size(); 1766 } 1767 } 1768 // Verify that the distance for generated before forward 1769 // short branches is still valid. 1770 guarantee((int)(blk_starts[i+1] - blk_starts[i]) >= (current_offset - blk_offset), "shouldn't increase block size"); 1771 1772 // Save new block start offset 1773 blk_starts[i] = blk_offset; 1774 } // End of for all blocks 1775 blk_starts[nblocks] = current_offset; 1776 1777 non_safepoints.flush_at_end(); 1778 1779 // Offset too large? 1780 if (C->failing()) return; 1781 1782 // Define a pseudo-label at the end of the code 1783 MacroAssembler(cb).bind( blk_labels[nblocks] ); 1784 1785 // Compute the size of the first block 1786 _first_block_size = blk_labels[1].loc_pos() - blk_labels[0].loc_pos(); 1787 1788 #ifdef ASSERT 1789 for (uint i = 0; i < nblocks; i++) { // For all blocks 1790 if (jmp_target[i] != 0) { 1791 int br_size = jmp_size[i]; 1792 int offset = blk_starts[jmp_target[i]]-(blk_starts[i] + jmp_offset[i]); 1793 if (!C->matcher()->is_short_branch_offset(jmp_rule[i], br_size, offset)) { 1794 tty->print_cr("target (%d) - jmp_offset(%d) = offset (%d), jump_size(%d), jmp_block B%d, target_block B%d", blk_starts[jmp_target[i]], blk_starts[i] + jmp_offset[i], offset, br_size, i, jmp_target[i]); 1795 assert(false, "Displacement too large for short jmp"); 1796 } 1797 } 1798 } 1799 #endif 1800 1801 if (!cb->finalize_stubs()) { 1802 C->record_failure("CodeCache is full"); 1803 return; 1804 } 1805 1806 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2(); 1807 bs->emit_stubs(*cb); 1808 if (C->failing()) return; 1809 1810 // Fill in stubs. 1811 _stub_list.emit(*cb); 1812 if (C->failing()) return; 1813 1814 #ifndef PRODUCT 1815 // Information on the size of the method, without the extraneous code 1816 Scheduling::increment_method_size(cb->insts_size()); 1817 #endif 1818 1819 // ------------------ 1820 // Fill in exception table entries. 1821 FillExceptionTables(inct_cnt, call_returns, inct_starts, blk_labels); 1822 1823 // Only java methods have exception handlers and deopt handlers 1824 // class HandlerImpl is platform-specific and defined in the *.ad files. 1825 if (C->method()) { 1826 // Emit the exception handler code. 1827 _code_offsets.set_value(CodeOffsets::Exceptions, HandlerImpl::emit_exception_handler(*cb)); 1828 if (C->failing()) { 1829 return; // CodeBuffer::expand failed 1830 } 1831 // Emit the deopt handler code. 1832 _code_offsets.set_value(CodeOffsets::Deopt, HandlerImpl::emit_deopt_handler(*cb)); 1833 1834 // Emit the MethodHandle deopt handler code (if required). 1835 if (C->has_method_handle_invokes() && !C->failing()) { 1836 // We can use the same code as for the normal deopt handler, we 1837 // just need a different entry point address. 1838 _code_offsets.set_value(CodeOffsets::DeoptMH, HandlerImpl::emit_deopt_handler(*cb)); 1839 } 1840 } 1841 1842 // One last check for failed CodeBuffer::expand: 1843 if ((cb->blob() == nullptr) || (!CompileBroker::should_compile_new_jobs())) { 1844 C->record_failure("CodeCache is full"); 1845 return; 1846 } 1847 1848 #if defined(SUPPORT_ABSTRACT_ASSEMBLY) || defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_OPTO_ASSEMBLY) 1849 if (C->print_assembly()) { 1850 tty->cr(); 1851 tty->print_cr("============================= C2-compiled nmethod =============================="); 1852 } 1853 #endif 1854 1855 #if defined(SUPPORT_OPTO_ASSEMBLY) 1856 // Dump the assembly code, including basic-block numbers 1857 if (C->print_assembly()) { 1858 ttyLocker ttyl; // keep the following output all in one block 1859 if (!VMThread::should_terminate()) { // test this under the tty lock 1860 // print_metadata and dump_asm may safepoint which makes us loose the ttylock. 1861 // We call them first and write to a stringStream, then we retake the lock to 1862 // make sure the end tag is coherent, and that xmlStream->pop_tag is done thread safe. 1863 ResourceMark rm; 1864 stringStream method_metadata_str; 1865 if (C->method() != nullptr) { 1866 C->method()->print_metadata(&method_metadata_str); 1867 } 1868 stringStream dump_asm_str; 1869 dump_asm_on(&dump_asm_str, node_offsets, node_offset_limit); 1870 1871 NoSafepointVerifier nsv; 1872 ttyLocker ttyl2; 1873 // This output goes directly to the tty, not the compiler log. 1874 // To enable tools to match it up with the compilation activity, 1875 // be sure to tag this tty output with the compile ID. 1876 if (xtty != nullptr) { 1877 xtty->head("opto_assembly compile_id='%d'%s", C->compile_id(), 1878 C->is_osr_compilation() ? " compile_kind='osr'" : ""); 1879 } 1880 if (C->method() != nullptr) { 1881 tty->print_cr("----------------------- MetaData before Compile_id = %d ------------------------", C->compile_id()); 1882 tty->print_raw(method_metadata_str.freeze()); 1883 } else if (C->stub_name() != nullptr) { 1884 tty->print_cr("----------------------------- RuntimeStub %s -------------------------------", C->stub_name()); 1885 } 1886 tty->cr(); 1887 tty->print_cr("------------------------ OptoAssembly for Compile_id = %d -----------------------", C->compile_id()); 1888 tty->print_raw(dump_asm_str.freeze()); 1889 tty->print_cr("--------------------------------------------------------------------------------"); 1890 if (xtty != nullptr) { 1891 xtty->tail("opto_assembly"); 1892 } 1893 } 1894 } 1895 #endif 1896 } 1897 1898 void PhaseOutput::FillExceptionTables(uint cnt, uint *call_returns, uint *inct_starts, Label *blk_labels) { 1899 _inc_table.set_size(cnt); 1900 1901 uint inct_cnt = 0; 1902 for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) { 1903 Block* block = C->cfg()->get_block(i); 1904 Node *n = nullptr; 1905 int j; 1906 1907 // Find the branch; ignore trailing NOPs. 1908 for (j = block->number_of_nodes() - 1; j >= 0; j--) { 1909 n = block->get_node(j); 1910 if (!n->is_Mach() || n->as_Mach()->ideal_Opcode() != Op_Con) { 1911 break; 1912 } 1913 } 1914 1915 // If we didn't find anything, continue 1916 if (j < 0) { 1917 continue; 1918 } 1919 1920 // Compute ExceptionHandlerTable subtable entry and add it 1921 // (skip empty blocks) 1922 if (n->is_Catch()) { 1923 1924 // Get the offset of the return from the call 1925 uint call_return = call_returns[block->_pre_order]; 1926 #ifdef ASSERT 1927 assert( call_return > 0, "no call seen for this basic block" ); 1928 while (block->get_node(--j)->is_MachProj()) ; 1929 assert(block->get_node(j)->is_MachCall(), "CatchProj must follow call"); 1930 #endif 1931 // last instruction is a CatchNode, find it's CatchProjNodes 1932 int nof_succs = block->_num_succs; 1933 // allocate space 1934 GrowableArray<intptr_t> handler_bcis(nof_succs); 1935 GrowableArray<intptr_t> handler_pcos(nof_succs); 1936 // iterate through all successors 1937 for (int j = 0; j < nof_succs; j++) { 1938 Block* s = block->_succs[j]; 1939 bool found_p = false; 1940 for (uint k = 1; k < s->num_preds(); k++) { 1941 Node* pk = s->pred(k); 1942 if (pk->is_CatchProj() && pk->in(0) == n) { 1943 const CatchProjNode* p = pk->as_CatchProj(); 1944 found_p = true; 1945 // add the corresponding handler bci & pco information 1946 if (p->_con != CatchProjNode::fall_through_index) { 1947 // p leads to an exception handler (and is not fall through) 1948 assert(s == C->cfg()->get_block(s->_pre_order), "bad numbering"); 1949 // no duplicates, please 1950 if (!handler_bcis.contains(p->handler_bci())) { 1951 uint block_num = s->non_connector()->_pre_order; 1952 handler_bcis.append(p->handler_bci()); 1953 handler_pcos.append(blk_labels[block_num].loc_pos()); 1954 } 1955 } 1956 } 1957 } 1958 assert(found_p, "no matching predecessor found"); 1959 // Note: Due to empty block removal, one block may have 1960 // several CatchProj inputs, from the same Catch. 1961 } 1962 1963 // Set the offset of the return from the call 1964 assert(handler_bcis.find(-1) != -1, "must have default handler"); 1965 _handler_table.add_subtable(call_return, &handler_bcis, nullptr, &handler_pcos); 1966 continue; 1967 } 1968 1969 // Handle implicit null exception table updates 1970 if (n->is_MachNullCheck()) { 1971 uint block_num = block->non_connector_successor(0)->_pre_order; 1972 _inc_table.append(inct_starts[inct_cnt++], blk_labels[block_num].loc_pos()); 1973 continue; 1974 } 1975 // Handle implicit exception table updates: trap instructions. 1976 if (n->is_Mach() && n->as_Mach()->is_TrapBasedCheckNode()) { 1977 uint block_num = block->non_connector_successor(0)->_pre_order; 1978 _inc_table.append(inct_starts[inct_cnt++], blk_labels[block_num].loc_pos()); 1979 continue; 1980 } 1981 } // End of for all blocks fill in exception table entries 1982 } 1983 1984 // Static Variables 1985 #ifndef PRODUCT 1986 uint Scheduling::_total_nop_size = 0; 1987 uint Scheduling::_total_method_size = 0; 1988 uint Scheduling::_total_branches = 0; 1989 uint Scheduling::_total_unconditional_delays = 0; 1990 uint Scheduling::_total_instructions_per_bundle[Pipeline::_max_instrs_per_cycle+1]; 1991 #endif 1992 1993 // Initializer for class Scheduling 1994 1995 Scheduling::Scheduling(Arena *arena, Compile &compile) 1996 : _arena(arena), 1997 _cfg(compile.cfg()), 1998 _regalloc(compile.regalloc()), 1999 _scheduled(arena), 2000 _available(arena), 2001 _reg_node(arena), 2002 _pinch_free_list(arena), 2003 _next_node(nullptr), 2004 _bundle_instr_count(0), 2005 _bundle_cycle_number(0), 2006 _bundle_use(0, 0, resource_count, &_bundle_use_elements[0]) 2007 #ifndef PRODUCT 2008 , _branches(0) 2009 , _unconditional_delays(0) 2010 #endif 2011 { 2012 // Create a MachNopNode 2013 _nop = new MachNopNode(); 2014 2015 // Now that the nops are in the array, save the count 2016 // (but allow entries for the nops) 2017 _node_bundling_limit = compile.unique(); 2018 uint node_max = _regalloc->node_regs_max_index(); 2019 2020 compile.output()->set_node_bundling_limit(_node_bundling_limit); 2021 2022 // This one is persistent within the Compile class 2023 _node_bundling_base = NEW_ARENA_ARRAY(compile.comp_arena(), Bundle, node_max); 2024 2025 // Allocate space for fixed-size arrays 2026 _uses = NEW_ARENA_ARRAY(arena, short, node_max); 2027 _current_latency = NEW_ARENA_ARRAY(arena, unsigned short, node_max); 2028 2029 // Clear the arrays 2030 for (uint i = 0; i < node_max; i++) { 2031 ::new (&_node_bundling_base[i]) Bundle(); 2032 } 2033 memset(_uses, 0, node_max * sizeof(short)); 2034 memset(_current_latency, 0, node_max * sizeof(unsigned short)); 2035 2036 // Clear the bundling information 2037 memcpy(_bundle_use_elements, Pipeline_Use::elaborated_elements, sizeof(Pipeline_Use::elaborated_elements)); 2038 2039 // Get the last node 2040 Block* block = _cfg->get_block(_cfg->number_of_blocks() - 1); 2041 2042 _next_node = block->get_node(block->number_of_nodes() - 1); 2043 } 2044 2045 #ifndef PRODUCT 2046 // Scheduling destructor 2047 Scheduling::~Scheduling() { 2048 _total_branches += _branches; 2049 _total_unconditional_delays += _unconditional_delays; 2050 } 2051 #endif 2052 2053 // Step ahead "i" cycles 2054 void Scheduling::step(uint i) { 2055 2056 Bundle *bundle = node_bundling(_next_node); 2057 bundle->set_starts_bundle(); 2058 2059 // Update the bundle record, but leave the flags information alone 2060 if (_bundle_instr_count > 0) { 2061 bundle->set_instr_count(_bundle_instr_count); 2062 bundle->set_resources_used(_bundle_use.resourcesUsed()); 2063 } 2064 2065 // Update the state information 2066 _bundle_instr_count = 0; 2067 _bundle_cycle_number += i; 2068 _bundle_use.step(i); 2069 } 2070 2071 void Scheduling::step_and_clear() { 2072 Bundle *bundle = node_bundling(_next_node); 2073 bundle->set_starts_bundle(); 2074 2075 // Update the bundle record 2076 if (_bundle_instr_count > 0) { 2077 bundle->set_instr_count(_bundle_instr_count); 2078 bundle->set_resources_used(_bundle_use.resourcesUsed()); 2079 2080 _bundle_cycle_number += 1; 2081 } 2082 2083 // Clear the bundling information 2084 _bundle_instr_count = 0; 2085 _bundle_use.reset(); 2086 2087 memcpy(_bundle_use_elements, 2088 Pipeline_Use::elaborated_elements, 2089 sizeof(Pipeline_Use::elaborated_elements)); 2090 } 2091 2092 // Perform instruction scheduling and bundling over the sequence of 2093 // instructions in backwards order. 2094 void PhaseOutput::ScheduleAndBundle() { 2095 2096 // Don't optimize this if it isn't a method 2097 if (!C->method()) 2098 return; 2099 2100 // Don't optimize this if scheduling is disabled 2101 if (!C->do_scheduling()) 2102 return; 2103 2104 // Scheduling code works only with pairs (8 bytes) maximum. 2105 // And when the scalable vector register is used, we may spill/unspill 2106 // the whole reg regardless of the max vector size. 2107 if (C->max_vector_size() > 8 || 2108 (C->max_vector_size() > 0 && Matcher::supports_scalable_vector())) { 2109 return; 2110 } 2111 2112 Compile::TracePhase tp("isched", &timers[_t_instrSched]); 2113 2114 // Create a data structure for all the scheduling information 2115 Scheduling scheduling(Thread::current()->resource_area(), *C); 2116 2117 // Walk backwards over each basic block, computing the needed alignment 2118 // Walk over all the basic blocks 2119 scheduling.DoScheduling(); 2120 2121 #ifndef PRODUCT 2122 if (C->trace_opto_output()) { 2123 // Buffer and print all at once 2124 ResourceMark rm; 2125 stringStream ss; 2126 ss.print("\n---- After ScheduleAndBundle ----\n"); 2127 print_scheduling(&ss); 2128 tty->print("%s", ss.as_string()); 2129 } 2130 #endif 2131 } 2132 2133 #ifndef PRODUCT 2134 // Separated out so that it can be called directly from debugger 2135 void PhaseOutput::print_scheduling() { 2136 print_scheduling(tty); 2137 } 2138 2139 void PhaseOutput::print_scheduling(outputStream* output_stream) { 2140 for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) { 2141 output_stream->print("\nBB#%03d:\n", i); 2142 Block* block = C->cfg()->get_block(i); 2143 for (uint j = 0; j < block->number_of_nodes(); j++) { 2144 Node* n = block->get_node(j); 2145 OptoReg::Name reg = C->regalloc()->get_reg_first(n); 2146 output_stream->print(" %-6s ", reg >= 0 && reg < REG_COUNT ? Matcher::regName[reg] : ""); 2147 n->dump("\n", false, output_stream); 2148 } 2149 } 2150 } 2151 #endif 2152 2153 // See if this node fits into the present instruction bundle 2154 bool Scheduling::NodeFitsInBundle(Node *n) { 2155 uint n_idx = n->_idx; 2156 2157 // If this is the unconditional delay instruction, then it fits 2158 if (n == _unconditional_delay_slot) { 2159 #ifndef PRODUCT 2160 if (_cfg->C->trace_opto_output()) 2161 tty->print("# NodeFitsInBundle [%4d]: TRUE; is in unconditional delay slot\n", n->_idx); 2162 #endif 2163 return (true); 2164 } 2165 2166 // If the node cannot be scheduled this cycle, skip it 2167 if (_current_latency[n_idx] > _bundle_cycle_number) { 2168 #ifndef PRODUCT 2169 if (_cfg->C->trace_opto_output()) 2170 tty->print("# NodeFitsInBundle [%4d]: FALSE; latency %4d > %d\n", 2171 n->_idx, _current_latency[n_idx], _bundle_cycle_number); 2172 #endif 2173 return (false); 2174 } 2175 2176 const Pipeline *node_pipeline = n->pipeline(); 2177 2178 uint instruction_count = node_pipeline->instructionCount(); 2179 if (node_pipeline->mayHaveNoCode() && n->size(_regalloc) == 0) 2180 instruction_count = 0; 2181 else if (node_pipeline->hasBranchDelay() && !_unconditional_delay_slot) 2182 instruction_count++; 2183 2184 if (_bundle_instr_count + instruction_count > Pipeline::_max_instrs_per_cycle) { 2185 #ifndef PRODUCT 2186 if (_cfg->C->trace_opto_output()) 2187 tty->print("# NodeFitsInBundle [%4d]: FALSE; too many instructions: %d > %d\n", 2188 n->_idx, _bundle_instr_count + instruction_count, Pipeline::_max_instrs_per_cycle); 2189 #endif 2190 return (false); 2191 } 2192 2193 // Don't allow non-machine nodes to be handled this way 2194 if (!n->is_Mach() && instruction_count == 0) 2195 return (false); 2196 2197 // See if there is any overlap 2198 uint delay = _bundle_use.full_latency(0, node_pipeline->resourceUse()); 2199 2200 if (delay > 0) { 2201 #ifndef PRODUCT 2202 if (_cfg->C->trace_opto_output()) 2203 tty->print("# NodeFitsInBundle [%4d]: FALSE; functional units overlap\n", n_idx); 2204 #endif 2205 return false; 2206 } 2207 2208 #ifndef PRODUCT 2209 if (_cfg->C->trace_opto_output()) 2210 tty->print("# NodeFitsInBundle [%4d]: TRUE\n", n_idx); 2211 #endif 2212 2213 return true; 2214 } 2215 2216 Node * Scheduling::ChooseNodeToBundle() { 2217 uint siz = _available.size(); 2218 2219 if (siz == 0) { 2220 2221 #ifndef PRODUCT 2222 if (_cfg->C->trace_opto_output()) 2223 tty->print("# ChooseNodeToBundle: null\n"); 2224 #endif 2225 return (nullptr); 2226 } 2227 2228 // Fast path, if only 1 instruction in the bundle 2229 if (siz == 1) { 2230 #ifndef PRODUCT 2231 if (_cfg->C->trace_opto_output()) { 2232 tty->print("# ChooseNodeToBundle (only 1): "); 2233 _available[0]->dump(); 2234 } 2235 #endif 2236 return (_available[0]); 2237 } 2238 2239 // Don't bother, if the bundle is already full 2240 if (_bundle_instr_count < Pipeline::_max_instrs_per_cycle) { 2241 for ( uint i = 0; i < siz; i++ ) { 2242 Node *n = _available[i]; 2243 2244 // Skip projections, we'll handle them another way 2245 if (n->is_Proj()) 2246 continue; 2247 2248 // This presupposed that instructions are inserted into the 2249 // available list in a legality order; i.e. instructions that 2250 // must be inserted first are at the head of the list 2251 if (NodeFitsInBundle(n)) { 2252 #ifndef PRODUCT 2253 if (_cfg->C->trace_opto_output()) { 2254 tty->print("# ChooseNodeToBundle: "); 2255 n->dump(); 2256 } 2257 #endif 2258 return (n); 2259 } 2260 } 2261 } 2262 2263 // Nothing fits in this bundle, choose the highest priority 2264 #ifndef PRODUCT 2265 if (_cfg->C->trace_opto_output()) { 2266 tty->print("# ChooseNodeToBundle: "); 2267 _available[0]->dump(); 2268 } 2269 #endif 2270 2271 return _available[0]; 2272 } 2273 2274 void Scheduling::AddNodeToAvailableList(Node *n) { 2275 assert( !n->is_Proj(), "projections never directly made available" ); 2276 #ifndef PRODUCT 2277 if (_cfg->C->trace_opto_output()) { 2278 tty->print("# AddNodeToAvailableList: "); 2279 n->dump(); 2280 } 2281 #endif 2282 2283 int latency = _current_latency[n->_idx]; 2284 2285 // Insert in latency order (insertion sort) 2286 uint i; 2287 for ( i=0; i < _available.size(); i++ ) 2288 if (_current_latency[_available[i]->_idx] > latency) 2289 break; 2290 2291 // Special Check for compares following branches 2292 if( n->is_Mach() && _scheduled.size() > 0 ) { 2293 int op = n->as_Mach()->ideal_Opcode(); 2294 Node *last = _scheduled[0]; 2295 if( last->is_MachIf() && last->in(1) == n && 2296 ( op == Op_CmpI || 2297 op == Op_CmpU || 2298 op == Op_CmpUL || 2299 op == Op_CmpP || 2300 op == Op_CmpF || 2301 op == Op_CmpD || 2302 op == Op_CmpL ) ) { 2303 2304 // Recalculate position, moving to front of same latency 2305 for ( i=0 ; i < _available.size(); i++ ) 2306 if (_current_latency[_available[i]->_idx] >= latency) 2307 break; 2308 } 2309 } 2310 2311 // Insert the node in the available list 2312 _available.insert(i, n); 2313 2314 #ifndef PRODUCT 2315 if (_cfg->C->trace_opto_output()) 2316 dump_available(); 2317 #endif 2318 } 2319 2320 void Scheduling::DecrementUseCounts(Node *n, const Block *bb) { 2321 for ( uint i=0; i < n->len(); i++ ) { 2322 Node *def = n->in(i); 2323 if (!def) continue; 2324 if( def->is_Proj() ) // If this is a machine projection, then 2325 def = def->in(0); // propagate usage thru to the base instruction 2326 2327 if(_cfg->get_block_for_node(def) != bb) { // Ignore if not block-local 2328 continue; 2329 } 2330 2331 // Compute the latency 2332 uint l = _bundle_cycle_number + n->latency(i); 2333 if (_current_latency[def->_idx] < l) 2334 _current_latency[def->_idx] = l; 2335 2336 // If this does not have uses then schedule it 2337 if ((--_uses[def->_idx]) == 0) 2338 AddNodeToAvailableList(def); 2339 } 2340 } 2341 2342 void Scheduling::AddNodeToBundle(Node *n, const Block *bb) { 2343 #ifndef PRODUCT 2344 if (_cfg->C->trace_opto_output()) { 2345 tty->print("# AddNodeToBundle: "); 2346 n->dump(); 2347 } 2348 #endif 2349 2350 // Remove this from the available list 2351 uint i; 2352 for (i = 0; i < _available.size(); i++) 2353 if (_available[i] == n) 2354 break; 2355 assert(i < _available.size(), "entry in _available list not found"); 2356 _available.remove(i); 2357 2358 // See if this fits in the current bundle 2359 const Pipeline *node_pipeline = n->pipeline(); 2360 const Pipeline_Use& node_usage = node_pipeline->resourceUse(); 2361 2362 // Check for instructions to be placed in the delay slot. We 2363 // do this before we actually schedule the current instruction, 2364 // because the delay slot follows the current instruction. 2365 if (Pipeline::_branch_has_delay_slot && 2366 node_pipeline->hasBranchDelay() && 2367 !_unconditional_delay_slot) { 2368 2369 uint siz = _available.size(); 2370 2371 // Conditional branches can support an instruction that 2372 // is unconditionally executed and not dependent by the 2373 // branch, OR a conditionally executed instruction if 2374 // the branch is taken. In practice, this means that 2375 // the first instruction at the branch target is 2376 // copied to the delay slot, and the branch goes to 2377 // the instruction after that at the branch target 2378 if ( n->is_MachBranch() ) { 2379 2380 assert( !n->is_MachNullCheck(), "should not look for delay slot for Null Check" ); 2381 assert( !n->is_Catch(), "should not look for delay slot for Catch" ); 2382 2383 #ifndef PRODUCT 2384 _branches++; 2385 #endif 2386 2387 // At least 1 instruction is on the available list 2388 // that is not dependent on the branch 2389 for (uint i = 0; i < siz; i++) { 2390 Node *d = _available[i]; 2391 const Pipeline *avail_pipeline = d->pipeline(); 2392 2393 // Don't allow safepoints in the branch shadow, that will 2394 // cause a number of difficulties 2395 if ( avail_pipeline->instructionCount() == 1 && 2396 !avail_pipeline->hasMultipleBundles() && 2397 !avail_pipeline->hasBranchDelay() && 2398 Pipeline::instr_has_unit_size() && 2399 d->size(_regalloc) == Pipeline::instr_unit_size() && 2400 NodeFitsInBundle(d) && 2401 !node_bundling(d)->used_in_delay()) { 2402 2403 if (d->is_Mach() && !d->is_MachSafePoint()) { 2404 // A node that fits in the delay slot was found, so we need to 2405 // set the appropriate bits in the bundle pipeline information so 2406 // that it correctly indicates resource usage. Later, when we 2407 // attempt to add this instruction to the bundle, we will skip 2408 // setting the resource usage. 2409 _unconditional_delay_slot = d; 2410 node_bundling(n)->set_use_unconditional_delay(); 2411 node_bundling(d)->set_used_in_unconditional_delay(); 2412 _bundle_use.add_usage(avail_pipeline->resourceUse()); 2413 _current_latency[d->_idx] = _bundle_cycle_number; 2414 _next_node = d; 2415 ++_bundle_instr_count; 2416 #ifndef PRODUCT 2417 _unconditional_delays++; 2418 #endif 2419 break; 2420 } 2421 } 2422 } 2423 } 2424 2425 // No delay slot, add a nop to the usage 2426 if (!_unconditional_delay_slot) { 2427 // See if adding an instruction in the delay slot will overflow 2428 // the bundle. 2429 if (!NodeFitsInBundle(_nop)) { 2430 #ifndef PRODUCT 2431 if (_cfg->C->trace_opto_output()) 2432 tty->print("# *** STEP(1 instruction for delay slot) ***\n"); 2433 #endif 2434 step(1); 2435 } 2436 2437 _bundle_use.add_usage(_nop->pipeline()->resourceUse()); 2438 _next_node = _nop; 2439 ++_bundle_instr_count; 2440 } 2441 2442 // See if the instruction in the delay slot requires a 2443 // step of the bundles 2444 if (!NodeFitsInBundle(n)) { 2445 #ifndef PRODUCT 2446 if (_cfg->C->trace_opto_output()) 2447 tty->print("# *** STEP(branch won't fit) ***\n"); 2448 #endif 2449 // Update the state information 2450 _bundle_instr_count = 0; 2451 _bundle_cycle_number += 1; 2452 _bundle_use.step(1); 2453 } 2454 } 2455 2456 // Get the number of instructions 2457 uint instruction_count = node_pipeline->instructionCount(); 2458 if (node_pipeline->mayHaveNoCode() && n->size(_regalloc) == 0) 2459 instruction_count = 0; 2460 2461 // Compute the latency information 2462 uint delay = 0; 2463 2464 if (instruction_count > 0 || !node_pipeline->mayHaveNoCode()) { 2465 int relative_latency = _current_latency[n->_idx] - _bundle_cycle_number; 2466 if (relative_latency < 0) 2467 relative_latency = 0; 2468 2469 delay = _bundle_use.full_latency(relative_latency, node_usage); 2470 2471 // Does not fit in this bundle, start a new one 2472 if (delay > 0) { 2473 step(delay); 2474 2475 #ifndef PRODUCT 2476 if (_cfg->C->trace_opto_output()) 2477 tty->print("# *** STEP(%d) ***\n", delay); 2478 #endif 2479 } 2480 } 2481 2482 // If this was placed in the delay slot, ignore it 2483 if (n != _unconditional_delay_slot) { 2484 2485 if (delay == 0) { 2486 if (node_pipeline->hasMultipleBundles()) { 2487 #ifndef PRODUCT 2488 if (_cfg->C->trace_opto_output()) 2489 tty->print("# *** STEP(multiple instructions) ***\n"); 2490 #endif 2491 step(1); 2492 } 2493 2494 else if (instruction_count + _bundle_instr_count > Pipeline::_max_instrs_per_cycle) { 2495 #ifndef PRODUCT 2496 if (_cfg->C->trace_opto_output()) 2497 tty->print("# *** STEP(%d >= %d instructions) ***\n", 2498 instruction_count + _bundle_instr_count, 2499 Pipeline::_max_instrs_per_cycle); 2500 #endif 2501 step(1); 2502 } 2503 } 2504 2505 if (node_pipeline->hasBranchDelay() && !_unconditional_delay_slot) 2506 _bundle_instr_count++; 2507 2508 // Set the node's latency 2509 _current_latency[n->_idx] = _bundle_cycle_number; 2510 2511 // Now merge the functional unit information 2512 if (instruction_count > 0 || !node_pipeline->mayHaveNoCode()) 2513 _bundle_use.add_usage(node_usage); 2514 2515 // Increment the number of instructions in this bundle 2516 _bundle_instr_count += instruction_count; 2517 2518 // Remember this node for later 2519 if (n->is_Mach()) 2520 _next_node = n; 2521 } 2522 2523 // It's possible to have a BoxLock in the graph and in the _bbs mapping but 2524 // not in the bb->_nodes array. This happens for debug-info-only BoxLocks. 2525 // 'Schedule' them (basically ignore in the schedule) but do not insert them 2526 // into the block. All other scheduled nodes get put in the schedule here. 2527 int op = n->Opcode(); 2528 if( (op == Op_Node && n->req() == 0) || // anti-dependence node OR 2529 (op != Op_Node && // Not an unused antidepedence node and 2530 // not an unallocated boxlock 2531 (OptoReg::is_valid(_regalloc->get_reg_first(n)) || op != Op_BoxLock)) ) { 2532 2533 // Push any trailing projections 2534 if( bb->get_node(bb->number_of_nodes()-1) != n ) { 2535 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 2536 Node *foi = n->fast_out(i); 2537 if( foi->is_Proj() ) 2538 _scheduled.push(foi); 2539 } 2540 } 2541 2542 // Put the instruction in the schedule list 2543 _scheduled.push(n); 2544 } 2545 2546 #ifndef PRODUCT 2547 if (_cfg->C->trace_opto_output()) 2548 dump_available(); 2549 #endif 2550 2551 // Walk all the definitions, decrementing use counts, and 2552 // if a definition has a 0 use count, place it in the available list. 2553 DecrementUseCounts(n,bb); 2554 } 2555 2556 // This method sets the use count within a basic block. We will ignore all 2557 // uses outside the current basic block. As we are doing a backwards walk, 2558 // any node we reach that has a use count of 0 may be scheduled. This also 2559 // avoids the problem of cyclic references from phi nodes, as long as phi 2560 // nodes are at the front of the basic block. This method also initializes 2561 // the available list to the set of instructions that have no uses within this 2562 // basic block. 2563 void Scheduling::ComputeUseCount(const Block *bb) { 2564 #ifndef PRODUCT 2565 if (_cfg->C->trace_opto_output()) 2566 tty->print("# -> ComputeUseCount\n"); 2567 #endif 2568 2569 // Clear the list of available and scheduled instructions, just in case 2570 _available.clear(); 2571 _scheduled.clear(); 2572 2573 // No delay slot specified 2574 _unconditional_delay_slot = nullptr; 2575 2576 #ifdef ASSERT 2577 for( uint i=0; i < bb->number_of_nodes(); i++ ) 2578 assert( _uses[bb->get_node(i)->_idx] == 0, "_use array not clean" ); 2579 #endif 2580 2581 // Force the _uses count to never go to zero for unscheduable pieces 2582 // of the block 2583 for( uint k = 0; k < _bb_start; k++ ) 2584 _uses[bb->get_node(k)->_idx] = 1; 2585 for( uint l = _bb_end; l < bb->number_of_nodes(); l++ ) 2586 _uses[bb->get_node(l)->_idx] = 1; 2587 2588 // Iterate backwards over the instructions in the block. Don't count the 2589 // branch projections at end or the block header instructions. 2590 for( uint j = _bb_end-1; j >= _bb_start; j-- ) { 2591 Node *n = bb->get_node(j); 2592 if( n->is_Proj() ) continue; // Projections handled another way 2593 2594 // Account for all uses 2595 for ( uint k = 0; k < n->len(); k++ ) { 2596 Node *inp = n->in(k); 2597 if (!inp) continue; 2598 assert(inp != n, "no cycles allowed" ); 2599 if (_cfg->get_block_for_node(inp) == bb) { // Block-local use? 2600 if (inp->is_Proj()) { // Skip through Proj's 2601 inp = inp->in(0); 2602 } 2603 ++_uses[inp->_idx]; // Count 1 block-local use 2604 } 2605 } 2606 2607 // If this instruction has a 0 use count, then it is available 2608 if (!_uses[n->_idx]) { 2609 _current_latency[n->_idx] = _bundle_cycle_number; 2610 AddNodeToAvailableList(n); 2611 } 2612 2613 #ifndef PRODUCT 2614 if (_cfg->C->trace_opto_output()) { 2615 tty->print("# uses: %3d: ", _uses[n->_idx]); 2616 n->dump(); 2617 } 2618 #endif 2619 } 2620 2621 #ifndef PRODUCT 2622 if (_cfg->C->trace_opto_output()) 2623 tty->print("# <- ComputeUseCount\n"); 2624 #endif 2625 } 2626 2627 // This routine performs scheduling on each basic block in reverse order, 2628 // using instruction latencies and taking into account function unit 2629 // availability. 2630 void Scheduling::DoScheduling() { 2631 #ifndef PRODUCT 2632 if (_cfg->C->trace_opto_output()) 2633 tty->print("# -> DoScheduling\n"); 2634 #endif 2635 2636 Block *succ_bb = nullptr; 2637 Block *bb; 2638 Compile* C = Compile::current(); 2639 2640 // Walk over all the basic blocks in reverse order 2641 for (int i = _cfg->number_of_blocks() - 1; i >= 0; succ_bb = bb, i--) { 2642 bb = _cfg->get_block(i); 2643 2644 #ifndef PRODUCT 2645 if (_cfg->C->trace_opto_output()) { 2646 tty->print("# Schedule BB#%03d (initial)\n", i); 2647 for (uint j = 0; j < bb->number_of_nodes(); j++) { 2648 bb->get_node(j)->dump(); 2649 } 2650 } 2651 #endif 2652 2653 // On the head node, skip processing 2654 if (bb == _cfg->get_root_block()) { 2655 continue; 2656 } 2657 2658 // Skip empty, connector blocks 2659 if (bb->is_connector()) 2660 continue; 2661 2662 // If the following block is not the sole successor of 2663 // this one, then reset the pipeline information 2664 if (bb->_num_succs != 1 || bb->non_connector_successor(0) != succ_bb) { 2665 #ifndef PRODUCT 2666 if (_cfg->C->trace_opto_output()) { 2667 tty->print("*** bundle start of next BB, node %d, for %d instructions\n", 2668 _next_node->_idx, _bundle_instr_count); 2669 } 2670 #endif 2671 step_and_clear(); 2672 } 2673 2674 // Leave untouched the starting instruction, any Phis, a CreateEx node 2675 // or Top. bb->get_node(_bb_start) is the first schedulable instruction. 2676 _bb_end = bb->number_of_nodes()-1; 2677 for( _bb_start=1; _bb_start <= _bb_end; _bb_start++ ) { 2678 Node *n = bb->get_node(_bb_start); 2679 // Things not matched, like Phinodes and ProjNodes don't get scheduled. 2680 // Also, MachIdealNodes do not get scheduled 2681 if( !n->is_Mach() ) continue; // Skip non-machine nodes 2682 MachNode *mach = n->as_Mach(); 2683 int iop = mach->ideal_Opcode(); 2684 if( iop == Op_CreateEx ) continue; // CreateEx is pinned 2685 if( iop == Op_Con ) continue; // Do not schedule Top 2686 if( iop == Op_Node && // Do not schedule PhiNodes, ProjNodes 2687 mach->pipeline() == MachNode::pipeline_class() && 2688 !n->is_SpillCopy() && !n->is_MachMerge() ) // Breakpoints, Prolog, etc 2689 continue; 2690 break; // Funny loop structure to be sure... 2691 } 2692 // Compute last "interesting" instruction in block - last instruction we 2693 // might schedule. _bb_end points just after last schedulable inst. We 2694 // normally schedule conditional branches (despite them being forced last 2695 // in the block), because they have delay slots we can fill. Calls all 2696 // have their delay slots filled in the template expansions, so we don't 2697 // bother scheduling them. 2698 Node *last = bb->get_node(_bb_end); 2699 // Ignore trailing NOPs. 2700 while (_bb_end > 0 && last->is_Mach() && 2701 last->as_Mach()->ideal_Opcode() == Op_Con) { 2702 last = bb->get_node(--_bb_end); 2703 } 2704 assert(!last->is_Mach() || last->as_Mach()->ideal_Opcode() != Op_Con, ""); 2705 if( last->is_Catch() || 2706 (last->is_Mach() && last->as_Mach()->ideal_Opcode() == Op_Halt) ) { 2707 // There might be a prior call. Skip it. 2708 while (_bb_start < _bb_end && bb->get_node(--_bb_end)->is_MachProj()); 2709 } else if( last->is_MachNullCheck() ) { 2710 // Backup so the last null-checked memory instruction is 2711 // outside the schedulable range. Skip over the nullcheck, 2712 // projection, and the memory nodes. 2713 Node *mem = last->in(1); 2714 do { 2715 _bb_end--; 2716 } while (mem != bb->get_node(_bb_end)); 2717 } else { 2718 // Set _bb_end to point after last schedulable inst. 2719 _bb_end++; 2720 } 2721 2722 assert( _bb_start <= _bb_end, "inverted block ends" ); 2723 2724 // Compute the register antidependencies for the basic block 2725 ComputeRegisterAntidependencies(bb); 2726 if (C->failing()) return; // too many D-U pinch points 2727 2728 // Compute the usage within the block, and set the list of all nodes 2729 // in the block that have no uses within the block. 2730 ComputeUseCount(bb); 2731 2732 // Schedule the remaining instructions in the block 2733 while ( _available.size() > 0 ) { 2734 Node *n = ChooseNodeToBundle(); 2735 guarantee(n != nullptr, "no nodes available"); 2736 AddNodeToBundle(n,bb); 2737 } 2738 2739 assert( _scheduled.size() == _bb_end - _bb_start, "wrong number of instructions" ); 2740 #ifdef ASSERT 2741 for( uint l = _bb_start; l < _bb_end; l++ ) { 2742 Node *n = bb->get_node(l); 2743 uint m; 2744 for( m = 0; m < _bb_end-_bb_start; m++ ) 2745 if( _scheduled[m] == n ) 2746 break; 2747 assert( m < _bb_end-_bb_start, "instruction missing in schedule" ); 2748 } 2749 #endif 2750 2751 // Now copy the instructions (in reverse order) back to the block 2752 for ( uint k = _bb_start; k < _bb_end; k++ ) 2753 bb->map_node(_scheduled[_bb_end-k-1], k); 2754 2755 #ifndef PRODUCT 2756 if (_cfg->C->trace_opto_output()) { 2757 tty->print("# Schedule BB#%03d (final)\n", i); 2758 uint current = 0; 2759 for (uint j = 0; j < bb->number_of_nodes(); j++) { 2760 Node *n = bb->get_node(j); 2761 if( valid_bundle_info(n) ) { 2762 Bundle *bundle = node_bundling(n); 2763 if (bundle->instr_count() > 0 || bundle->flags() > 0) { 2764 tty->print("*** Bundle: "); 2765 bundle->dump(); 2766 } 2767 n->dump(); 2768 } 2769 } 2770 } 2771 #endif 2772 #ifdef ASSERT 2773 verify_good_schedule(bb,"after block local scheduling"); 2774 #endif 2775 } 2776 2777 #ifndef PRODUCT 2778 if (_cfg->C->trace_opto_output()) 2779 tty->print("# <- DoScheduling\n"); 2780 #endif 2781 2782 // Record final node-bundling array location 2783 _regalloc->C->output()->set_node_bundling_base(_node_bundling_base); 2784 2785 } // end DoScheduling 2786 2787 // Verify that no live-range used in the block is killed in the block by a 2788 // wrong DEF. This doesn't verify live-ranges that span blocks. 2789 2790 // Check for edge existence. Used to avoid adding redundant precedence edges. 2791 static bool edge_from_to( Node *from, Node *to ) { 2792 for( uint i=0; i<from->len(); i++ ) 2793 if( from->in(i) == to ) 2794 return true; 2795 return false; 2796 } 2797 2798 #ifdef ASSERT 2799 void Scheduling::verify_do_def( Node *n, OptoReg::Name def, const char *msg ) { 2800 // Check for bad kills 2801 if( OptoReg::is_valid(def) ) { // Ignore stores & control flow 2802 Node *prior_use = _reg_node[def]; 2803 if( prior_use && !edge_from_to(prior_use,n) ) { 2804 tty->print("%s = ",OptoReg::as_VMReg(def)->name()); 2805 n->dump(); 2806 tty->print_cr("..."); 2807 prior_use->dump(); 2808 assert(edge_from_to(prior_use,n), "%s", msg); 2809 } 2810 _reg_node.map(def,nullptr); // Kill live USEs 2811 } 2812 } 2813 2814 void Scheduling::verify_good_schedule( Block *b, const char *msg ) { 2815 2816 // Zap to something reasonable for the verify code 2817 _reg_node.clear(); 2818 2819 // Walk over the block backwards. Check to make sure each DEF doesn't 2820 // kill a live value (other than the one it's supposed to). Add each 2821 // USE to the live set. 2822 for( uint i = b->number_of_nodes()-1; i >= _bb_start; i-- ) { 2823 Node *n = b->get_node(i); 2824 int n_op = n->Opcode(); 2825 if( n_op == Op_MachProj && n->ideal_reg() == MachProjNode::fat_proj ) { 2826 // Fat-proj kills a slew of registers 2827 RegMaskIterator rmi(n->out_RegMask()); 2828 while (rmi.has_next()) { 2829 OptoReg::Name kill = rmi.next(); 2830 verify_do_def(n, kill, msg); 2831 } 2832 } else if( n_op != Op_Node ) { // Avoid brand new antidependence nodes 2833 // Get DEF'd registers the normal way 2834 verify_do_def( n, _regalloc->get_reg_first(n), msg ); 2835 verify_do_def( n, _regalloc->get_reg_second(n), msg ); 2836 } 2837 2838 // Now make all USEs live 2839 for( uint i=1; i<n->req(); i++ ) { 2840 Node *def = n->in(i); 2841 assert(def != 0, "input edge required"); 2842 OptoReg::Name reg_lo = _regalloc->get_reg_first(def); 2843 OptoReg::Name reg_hi = _regalloc->get_reg_second(def); 2844 if( OptoReg::is_valid(reg_lo) ) { 2845 assert(!_reg_node[reg_lo] || edge_from_to(_reg_node[reg_lo],def), "%s", msg); 2846 _reg_node.map(reg_lo,n); 2847 } 2848 if( OptoReg::is_valid(reg_hi) ) { 2849 assert(!_reg_node[reg_hi] || edge_from_to(_reg_node[reg_hi],def), "%s", msg); 2850 _reg_node.map(reg_hi,n); 2851 } 2852 } 2853 2854 } 2855 2856 // Zap to something reasonable for the Antidependence code 2857 _reg_node.clear(); 2858 } 2859 #endif 2860 2861 // Conditionally add precedence edges. Avoid putting edges on Projs. 2862 static void add_prec_edge_from_to( Node *from, Node *to ) { 2863 if( from->is_Proj() ) { // Put precedence edge on Proj's input 2864 assert( from->req() == 1 && (from->len() == 1 || from->in(1)==0), "no precedence edges on projections" ); 2865 from = from->in(0); 2866 } 2867 if( from != to && // No cycles (for things like LD L0,[L0+4] ) 2868 !edge_from_to( from, to ) ) // Avoid duplicate edge 2869 from->add_prec(to); 2870 } 2871 2872 void Scheduling::anti_do_def( Block *b, Node *def, OptoReg::Name def_reg, int is_def ) { 2873 if( !OptoReg::is_valid(def_reg) ) // Ignore stores & control flow 2874 return; 2875 2876 if (OptoReg::is_reg(def_reg)) { 2877 VMReg vmreg = OptoReg::as_VMReg(def_reg); 2878 if (vmreg->is_reg() && !vmreg->is_concrete() && !vmreg->prev()->is_concrete()) { 2879 // This is one of the high slots of a vector register. 2880 // ScheduleAndBundle already checked there are no live wide 2881 // vectors in this method so it can be safely ignored. 2882 return; 2883 } 2884 } 2885 2886 Node *pinch = _reg_node[def_reg]; // Get pinch point 2887 if ((pinch == nullptr) || _cfg->get_block_for_node(pinch) != b || // No pinch-point yet? 2888 is_def ) { // Check for a true def (not a kill) 2889 _reg_node.map(def_reg,def); // Record def/kill as the optimistic pinch-point 2890 return; 2891 } 2892 2893 Node *kill = def; // Rename 'def' to more descriptive 'kill' 2894 debug_only( def = (Node*)((intptr_t)0xdeadbeef); ) 2895 2896 // After some number of kills there _may_ be a later def 2897 Node *later_def = nullptr; 2898 2899 Compile* C = Compile::current(); 2900 2901 // Finding a kill requires a real pinch-point. 2902 // Check for not already having a pinch-point. 2903 // Pinch points are Op_Node's. 2904 if( pinch->Opcode() != Op_Node ) { // Or later-def/kill as pinch-point? 2905 later_def = pinch; // Must be def/kill as optimistic pinch-point 2906 if ( _pinch_free_list.size() > 0) { 2907 pinch = _pinch_free_list.pop(); 2908 } else { 2909 pinch = new Node(1); // Pinch point to-be 2910 } 2911 if (pinch->_idx >= _regalloc->node_regs_max_index()) { 2912 DEBUG_ONLY( pinch->dump(); ); 2913 assert(false, "too many D-U pinch points: %d >= %d", pinch->_idx, _regalloc->node_regs_max_index()); 2914 _cfg->C->record_method_not_compilable("too many D-U pinch points"); 2915 return; 2916 } 2917 _cfg->map_node_to_block(pinch, b); // Pretend it's valid in this block (lazy init) 2918 _reg_node.map(def_reg,pinch); // Record pinch-point 2919 //regalloc()->set_bad(pinch->_idx); // Already initialized this way. 2920 if( later_def->outcnt() == 0 || later_def->ideal_reg() == MachProjNode::fat_proj ) { // Distinguish def from kill 2921 pinch->init_req(0, C->top()); // set not null for the next call 2922 add_prec_edge_from_to(later_def,pinch); // Add edge from kill to pinch 2923 later_def = nullptr; // and no later def 2924 } 2925 pinch->set_req(0,later_def); // Hook later def so we can find it 2926 } else { // Else have valid pinch point 2927 if( pinch->in(0) ) // If there is a later-def 2928 later_def = pinch->in(0); // Get it 2929 } 2930 2931 // Add output-dependence edge from later def to kill 2932 if( later_def ) // If there is some original def 2933 add_prec_edge_from_to(later_def,kill); // Add edge from def to kill 2934 2935 // See if current kill is also a use, and so is forced to be the pinch-point. 2936 if( pinch->Opcode() == Op_Node ) { 2937 Node *uses = kill->is_Proj() ? kill->in(0) : kill; 2938 for( uint i=1; i<uses->req(); i++ ) { 2939 if( _regalloc->get_reg_first(uses->in(i)) == def_reg || 2940 _regalloc->get_reg_second(uses->in(i)) == def_reg ) { 2941 // Yes, found a use/kill pinch-point 2942 pinch->set_req(0,nullptr); // 2943 pinch->replace_by(kill); // Move anti-dep edges up 2944 pinch = kill; 2945 _reg_node.map(def_reg,pinch); 2946 return; 2947 } 2948 } 2949 } 2950 2951 // Add edge from kill to pinch-point 2952 add_prec_edge_from_to(kill,pinch); 2953 } 2954 2955 void Scheduling::anti_do_use( Block *b, Node *use, OptoReg::Name use_reg ) { 2956 if( !OptoReg::is_valid(use_reg) ) // Ignore stores & control flow 2957 return; 2958 Node *pinch = _reg_node[use_reg]; // Get pinch point 2959 // Check for no later def_reg/kill in block 2960 if ((pinch != nullptr) && _cfg->get_block_for_node(pinch) == b && 2961 // Use has to be block-local as well 2962 _cfg->get_block_for_node(use) == b) { 2963 if( pinch->Opcode() == Op_Node && // Real pinch-point (not optimistic?) 2964 pinch->req() == 1 ) { // pinch not yet in block? 2965 pinch->del_req(0); // yank pointer to later-def, also set flag 2966 // Insert the pinch-point in the block just after the last use 2967 b->insert_node(pinch, b->find_node(use) + 1); 2968 _bb_end++; // Increase size scheduled region in block 2969 } 2970 2971 add_prec_edge_from_to(pinch,use); 2972 } 2973 } 2974 2975 // We insert antidependences between the reads and following write of 2976 // allocated registers to prevent illegal code motion. Hopefully, the 2977 // number of added references should be fairly small, especially as we 2978 // are only adding references within the current basic block. 2979 void Scheduling::ComputeRegisterAntidependencies(Block *b) { 2980 2981 #ifdef ASSERT 2982 verify_good_schedule(b,"before block local scheduling"); 2983 #endif 2984 2985 // A valid schedule, for each register independently, is an endless cycle 2986 // of: a def, then some uses (connected to the def by true dependencies), 2987 // then some kills (defs with no uses), finally the cycle repeats with a new 2988 // def. The uses are allowed to float relative to each other, as are the 2989 // kills. No use is allowed to slide past a kill (or def). This requires 2990 // antidependencies between all uses of a single def and all kills that 2991 // follow, up to the next def. More edges are redundant, because later defs 2992 // & kills are already serialized with true or antidependencies. To keep 2993 // the edge count down, we add a 'pinch point' node if there's more than 2994 // one use or more than one kill/def. 2995 2996 // We add dependencies in one bottom-up pass. 2997 2998 // For each instruction we handle it's DEFs/KILLs, then it's USEs. 2999 3000 // For each DEF/KILL, we check to see if there's a prior DEF/KILL for this 3001 // register. If not, we record the DEF/KILL in _reg_node, the 3002 // register-to-def mapping. If there is a prior DEF/KILL, we insert a 3003 // "pinch point", a new Node that's in the graph but not in the block. 3004 // We put edges from the prior and current DEF/KILLs to the pinch point. 3005 // We put the pinch point in _reg_node. If there's already a pinch point 3006 // we merely add an edge from the current DEF/KILL to the pinch point. 3007 3008 // After doing the DEF/KILLs, we handle USEs. For each used register, we 3009 // put an edge from the pinch point to the USE. 3010 3011 // To be expedient, the _reg_node array is pre-allocated for the whole 3012 // compilation. _reg_node is lazily initialized; it either contains a null, 3013 // or a valid def/kill/pinch-point, or a leftover node from some prior 3014 // block. Leftover node from some prior block is treated like a null (no 3015 // prior def, so no anti-dependence needed). Valid def is distinguished by 3016 // it being in the current block. 3017 bool fat_proj_seen = false; 3018 uint last_safept = _bb_end-1; 3019 Node* end_node = (_bb_end-1 >= _bb_start) ? b->get_node(last_safept) : nullptr; 3020 Node* last_safept_node = end_node; 3021 for( uint i = _bb_end-1; i >= _bb_start; i-- ) { 3022 Node *n = b->get_node(i); 3023 int is_def = n->outcnt(); // def if some uses prior to adding precedence edges 3024 if( n->is_MachProj() && n->ideal_reg() == MachProjNode::fat_proj ) { 3025 // Fat-proj kills a slew of registers 3026 // This can add edges to 'n' and obscure whether or not it was a def, 3027 // hence the is_def flag. 3028 fat_proj_seen = true; 3029 RegMaskIterator rmi(n->out_RegMask()); 3030 while (rmi.has_next()) { 3031 OptoReg::Name kill = rmi.next(); 3032 anti_do_def(b, n, kill, is_def); 3033 } 3034 } else { 3035 // Get DEF'd registers the normal way 3036 anti_do_def( b, n, _regalloc->get_reg_first(n), is_def ); 3037 anti_do_def( b, n, _regalloc->get_reg_second(n), is_def ); 3038 } 3039 3040 // Kill projections on a branch should appear to occur on the 3041 // branch, not afterwards, so grab the masks from the projections 3042 // and process them. 3043 if (n->is_MachBranch() || (n->is_Mach() && n->as_Mach()->ideal_Opcode() == Op_Jump)) { 3044 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 3045 Node* use = n->fast_out(i); 3046 if (use->is_Proj()) { 3047 RegMaskIterator rmi(use->out_RegMask()); 3048 while (rmi.has_next()) { 3049 OptoReg::Name kill = rmi.next(); 3050 anti_do_def(b, n, kill, false); 3051 } 3052 } 3053 } 3054 } 3055 3056 // Check each register used by this instruction for a following DEF/KILL 3057 // that must occur afterward and requires an anti-dependence edge. 3058 for( uint j=0; j<n->req(); j++ ) { 3059 Node *def = n->in(j); 3060 if( def ) { 3061 assert( !def->is_MachProj() || def->ideal_reg() != MachProjNode::fat_proj, "" ); 3062 anti_do_use( b, n, _regalloc->get_reg_first(def) ); 3063 anti_do_use( b, n, _regalloc->get_reg_second(def) ); 3064 } 3065 } 3066 // Do not allow defs of new derived values to float above GC 3067 // points unless the base is definitely available at the GC point. 3068 3069 Node *m = b->get_node(i); 3070 3071 // Add precedence edge from following safepoint to use of derived pointer 3072 if( last_safept_node != end_node && 3073 m != last_safept_node) { 3074 for (uint k = 1; k < m->req(); k++) { 3075 const Type *t = m->in(k)->bottom_type(); 3076 if( t->isa_oop_ptr() && 3077 t->is_ptr()->offset() != 0 ) { 3078 last_safept_node->add_prec( m ); 3079 break; 3080 } 3081 } 3082 } 3083 3084 if( n->jvms() ) { // Precedence edge from derived to safept 3085 // Check if last_safept_node was moved by pinch-point insertion in anti_do_use() 3086 if( b->get_node(last_safept) != last_safept_node ) { 3087 last_safept = b->find_node(last_safept_node); 3088 } 3089 for( uint j=last_safept; j > i; j-- ) { 3090 Node *mach = b->get_node(j); 3091 if( mach->is_Mach() && mach->as_Mach()->ideal_Opcode() == Op_AddP ) 3092 mach->add_prec( n ); 3093 } 3094 last_safept = i; 3095 last_safept_node = m; 3096 } 3097 } 3098 3099 if (fat_proj_seen) { 3100 // Garbage collect pinch nodes that were not consumed. 3101 // They are usually created by a fat kill MachProj for a call. 3102 garbage_collect_pinch_nodes(); 3103 } 3104 } 3105 3106 // Garbage collect pinch nodes for reuse by other blocks. 3107 // 3108 // The block scheduler's insertion of anti-dependence 3109 // edges creates many pinch nodes when the block contains 3110 // 2 or more Calls. A pinch node is used to prevent a 3111 // combinatorial explosion of edges. If a set of kills for a 3112 // register is anti-dependent on a set of uses (or defs), rather 3113 // than adding an edge in the graph between each pair of kill 3114 // and use (or def), a pinch is inserted between them: 3115 // 3116 // use1 use2 use3 3117 // \ | / 3118 // \ | / 3119 // pinch 3120 // / | \ 3121 // / | \ 3122 // kill1 kill2 kill3 3123 // 3124 // One pinch node is created per register killed when 3125 // the second call is encountered during a backwards pass 3126 // over the block. Most of these pinch nodes are never 3127 // wired into the graph because the register is never 3128 // used or def'ed in the block. 3129 // 3130 void Scheduling::garbage_collect_pinch_nodes() { 3131 #ifndef PRODUCT 3132 if (_cfg->C->trace_opto_output()) tty->print("Reclaimed pinch nodes:"); 3133 #endif 3134 int trace_cnt = 0; 3135 for (uint k = 0; k < _reg_node.max(); k++) { 3136 Node* pinch = _reg_node[k]; 3137 if ((pinch != nullptr) && pinch->Opcode() == Op_Node && 3138 // no predecence input edges 3139 (pinch->req() == pinch->len() || pinch->in(pinch->req()) == nullptr) ) { 3140 cleanup_pinch(pinch); 3141 _pinch_free_list.push(pinch); 3142 _reg_node.map(k, nullptr); 3143 #ifndef PRODUCT 3144 if (_cfg->C->trace_opto_output()) { 3145 trace_cnt++; 3146 if (trace_cnt > 40) { 3147 tty->print("\n"); 3148 trace_cnt = 0; 3149 } 3150 tty->print(" %d", pinch->_idx); 3151 } 3152 #endif 3153 } 3154 } 3155 #ifndef PRODUCT 3156 if (_cfg->C->trace_opto_output()) tty->print("\n"); 3157 #endif 3158 } 3159 3160 // Clean up a pinch node for reuse. 3161 void Scheduling::cleanup_pinch( Node *pinch ) { 3162 assert (pinch && pinch->Opcode() == Op_Node && pinch->req() == 1, "just checking"); 3163 3164 for (DUIterator_Last imin, i = pinch->last_outs(imin); i >= imin; ) { 3165 Node* use = pinch->last_out(i); 3166 uint uses_found = 0; 3167 for (uint j = use->req(); j < use->len(); j++) { 3168 if (use->in(j) == pinch) { 3169 use->rm_prec(j); 3170 uses_found++; 3171 } 3172 } 3173 assert(uses_found > 0, "must be a precedence edge"); 3174 i -= uses_found; // we deleted 1 or more copies of this edge 3175 } 3176 // May have a later_def entry 3177 pinch->set_req(0, nullptr); 3178 } 3179 3180 #ifndef PRODUCT 3181 3182 void Scheduling::dump_available() const { 3183 tty->print("#Availist "); 3184 for (uint i = 0; i < _available.size(); i++) 3185 tty->print(" N%d/l%d", _available[i]->_idx,_current_latency[_available[i]->_idx]); 3186 tty->cr(); 3187 } 3188 3189 // Print Scheduling Statistics 3190 void Scheduling::print_statistics() { 3191 // Print the size added by nops for bundling 3192 tty->print("Nops added %d bytes to total of %d bytes", 3193 _total_nop_size, _total_method_size); 3194 if (_total_method_size > 0) 3195 tty->print(", for %.2f%%", 3196 ((double)_total_nop_size) / ((double) _total_method_size) * 100.0); 3197 tty->print("\n"); 3198 3199 // Print the number of branch shadows filled 3200 if (Pipeline::_branch_has_delay_slot) { 3201 tty->print("Of %d branches, %d had unconditional delay slots filled", 3202 _total_branches, _total_unconditional_delays); 3203 if (_total_branches > 0) 3204 tty->print(", for %.2f%%", 3205 ((double)_total_unconditional_delays) / ((double)_total_branches) * 100.0); 3206 tty->print("\n"); 3207 } 3208 3209 uint total_instructions = 0, total_bundles = 0; 3210 3211 for (uint i = 1; i <= Pipeline::_max_instrs_per_cycle; i++) { 3212 uint bundle_count = _total_instructions_per_bundle[i]; 3213 total_instructions += bundle_count * i; 3214 total_bundles += bundle_count; 3215 } 3216 3217 if (total_bundles > 0) 3218 tty->print("Average ILP (excluding nops) is %.2f\n", 3219 ((double)total_instructions) / ((double)total_bundles)); 3220 } 3221 #endif 3222 3223 //-----------------------init_scratch_buffer_blob------------------------------ 3224 // Construct a temporary BufferBlob and cache it for this compile. 3225 void PhaseOutput::init_scratch_buffer_blob(int const_size) { 3226 // If there is already a scratch buffer blob allocated and the 3227 // constant section is big enough, use it. Otherwise free the 3228 // current and allocate a new one. 3229 BufferBlob* blob = scratch_buffer_blob(); 3230 if ((blob != nullptr) && (const_size <= _scratch_const_size)) { 3231 // Use the current blob. 3232 } else { 3233 if (blob != nullptr) { 3234 BufferBlob::free(blob); 3235 } 3236 3237 ResourceMark rm; 3238 _scratch_const_size = const_size; 3239 int size = C2Compiler::initial_code_buffer_size(const_size); 3240 blob = BufferBlob::create("Compile::scratch_buffer", size); 3241 // Record the buffer blob for next time. 3242 set_scratch_buffer_blob(blob); 3243 // Have we run out of code space? 3244 if (scratch_buffer_blob() == nullptr) { 3245 // Let CompilerBroker disable further compilations. 3246 C->record_failure("Not enough space for scratch buffer in CodeCache"); 3247 return; 3248 } 3249 } 3250 3251 // Initialize the relocation buffers 3252 relocInfo* locs_buf = (relocInfo*) blob->content_end() - MAX_locs_size; 3253 set_scratch_locs_memory(locs_buf); 3254 } 3255 3256 3257 //-----------------------scratch_emit_size------------------------------------- 3258 // Helper function that computes size by emitting code 3259 uint PhaseOutput::scratch_emit_size(const Node* n) { 3260 // Start scratch_emit_size section. 3261 set_in_scratch_emit_size(true); 3262 3263 // Emit into a trash buffer and count bytes emitted. 3264 // This is a pretty expensive way to compute a size, 3265 // but it works well enough if seldom used. 3266 // All common fixed-size instructions are given a size 3267 // method by the AD file. 3268 // Note that the scratch buffer blob and locs memory are 3269 // allocated at the beginning of the compile task, and 3270 // may be shared by several calls to scratch_emit_size. 3271 // The allocation of the scratch buffer blob is particularly 3272 // expensive, since it has to grab the code cache lock. 3273 BufferBlob* blob = this->scratch_buffer_blob(); 3274 assert(blob != nullptr, "Initialize BufferBlob at start"); 3275 assert(blob->size() > MAX_inst_size, "sanity"); 3276 relocInfo* locs_buf = scratch_locs_memory(); 3277 address blob_begin = blob->content_begin(); 3278 address blob_end = (address)locs_buf; 3279 assert(blob->contains(blob_end), "sanity"); 3280 CodeBuffer buf(blob_begin, blob_end - blob_begin); 3281 buf.initialize_consts_size(_scratch_const_size); 3282 buf.initialize_stubs_size(MAX_stubs_size); 3283 assert(locs_buf != nullptr, "sanity"); 3284 int lsize = MAX_locs_size / 3; 3285 buf.consts()->initialize_shared_locs(&locs_buf[lsize * 0], lsize); 3286 buf.insts()->initialize_shared_locs( &locs_buf[lsize * 1], lsize); 3287 buf.stubs()->initialize_shared_locs( &locs_buf[lsize * 2], lsize); 3288 // Mark as scratch buffer. 3289 buf.consts()->set_scratch_emit(); 3290 buf.insts()->set_scratch_emit(); 3291 buf.stubs()->set_scratch_emit(); 3292 3293 // Do the emission. 3294 3295 Label fakeL; // Fake label for branch instructions. 3296 Label* saveL = nullptr; 3297 uint save_bnum = 0; 3298 bool is_branch = n->is_MachBranch(); 3299 if (is_branch) { 3300 MacroAssembler masm(&buf); 3301 masm.bind(fakeL); 3302 n->as_MachBranch()->save_label(&saveL, &save_bnum); 3303 n->as_MachBranch()->label_set(&fakeL, 0); 3304 } 3305 n->emit(buf, C->regalloc()); 3306 3307 // Emitting into the scratch buffer should not fail 3308 assert (!C->failing(), "Must not have pending failure. Reason is: %s", C->failure_reason()); 3309 3310 if (is_branch) // Restore label. 3311 n->as_MachBranch()->label_set(saveL, save_bnum); 3312 3313 // End scratch_emit_size section. 3314 set_in_scratch_emit_size(false); 3315 3316 return buf.insts_size(); 3317 } 3318 3319 void PhaseOutput::install() { 3320 if (!C->should_install_code()) { 3321 return; 3322 } else if (C->stub_function() != nullptr) { 3323 install_stub(C->stub_name()); 3324 } else { 3325 install_code(C->method(), 3326 C->entry_bci(), 3327 CompileBroker::compiler2(), 3328 C->has_unsafe_access(), 3329 SharedRuntime::is_wide_vector(C->max_vector_size()), 3330 C->rtm_state()); 3331 } 3332 } 3333 3334 void PhaseOutput::install_code(ciMethod* target, 3335 int entry_bci, 3336 AbstractCompiler* compiler, 3337 bool has_unsafe_access, 3338 bool has_wide_vectors, 3339 RTMState rtm_state) { 3340 // Check if we want to skip execution of all compiled code. 3341 { 3342 #ifndef PRODUCT 3343 if (OptoNoExecute) { 3344 C->record_method_not_compilable("+OptoNoExecute"); // Flag as failed 3345 return; 3346 } 3347 #endif 3348 Compile::TracePhase tp("install_code", &timers[_t_registerMethod]); 3349 3350 if (C->is_osr_compilation()) { 3351 _code_offsets.set_value(CodeOffsets::Verified_Entry, 0); 3352 _code_offsets.set_value(CodeOffsets::OSR_Entry, _first_block_size); 3353 } else { 3354 _code_offsets.set_value(CodeOffsets::Verified_Entry, _first_block_size); 3355 _code_offsets.set_value(CodeOffsets::OSR_Entry, 0); 3356 } 3357 3358 C->env()->register_method(target, 3359 entry_bci, 3360 &_code_offsets, 3361 _orig_pc_slot_offset_in_bytes, 3362 code_buffer(), 3363 frame_size_in_words(), 3364 oop_map_set(), 3365 &_handler_table, 3366 inc_table(), 3367 compiler, 3368 has_unsafe_access, 3369 SharedRuntime::is_wide_vector(C->max_vector_size()), 3370 C->has_monitors(), 3371 0, 3372 C->rtm_state()); 3373 3374 if (C->log() != nullptr) { // Print code cache state into compiler log 3375 C->log()->code_cache_state(); 3376 } 3377 } 3378 } 3379 void PhaseOutput::install_stub(const char* stub_name) { 3380 // Entry point will be accessed using stub_entry_point(); 3381 if (code_buffer() == nullptr) { 3382 Matcher::soft_match_failure(); 3383 } else { 3384 if (PrintAssembly && (WizardMode || Verbose)) 3385 tty->print_cr("### Stub::%s", stub_name); 3386 3387 if (!C->failing()) { 3388 assert(C->fixed_slots() == 0, "no fixed slots used for runtime stubs"); 3389 3390 // Make the NMethod 3391 // For now we mark the frame as never safe for profile stackwalking 3392 RuntimeStub *rs = RuntimeStub::new_runtime_stub(stub_name, 3393 code_buffer(), 3394 CodeOffsets::frame_never_safe, 3395 // _code_offsets.value(CodeOffsets::Frame_Complete), 3396 frame_size_in_words(), 3397 oop_map_set(), 3398 false); 3399 assert(rs != nullptr && rs->is_runtime_stub(), "sanity check"); 3400 3401 C->set_stub_entry_point(rs->entry_point()); 3402 } 3403 } 3404 } 3405 3406 // Support for bundling info 3407 Bundle* PhaseOutput::node_bundling(const Node *n) { 3408 assert(valid_bundle_info(n), "oob"); 3409 return &_node_bundling_base[n->_idx]; 3410 } 3411 3412 bool PhaseOutput::valid_bundle_info(const Node *n) { 3413 return (_node_bundling_limit > n->_idx); 3414 } 3415 3416 //------------------------------frame_size_in_words----------------------------- 3417 // frame_slots in units of words 3418 int PhaseOutput::frame_size_in_words() const { 3419 // shift is 0 in LP32 and 1 in LP64 3420 const int shift = (LogBytesPerWord - LogBytesPerInt); 3421 int words = _frame_slots >> shift; 3422 assert( words << shift == _frame_slots, "frame size must be properly aligned in LP64" ); 3423 return words; 3424 } 3425 3426 // To bang the stack of this compiled method we use the stack size 3427 // that the interpreter would need in case of a deoptimization. This 3428 // removes the need to bang the stack in the deoptimization blob which 3429 // in turn simplifies stack overflow handling. 3430 int PhaseOutput::bang_size_in_bytes() const { 3431 return MAX2(frame_size_in_bytes() + os::extra_bang_size_in_bytes(), C->interpreter_frame_size()); 3432 } 3433 3434 //------------------------------dump_asm--------------------------------------- 3435 // Dump formatted assembly 3436 #if defined(SUPPORT_OPTO_ASSEMBLY) 3437 void PhaseOutput::dump_asm_on(outputStream* st, int* pcs, uint pc_limit) { 3438 3439 int pc_digits = 3; // #chars required for pc 3440 int sb_chars = 3; // #chars for "start bundle" indicator 3441 int tab_size = 8; 3442 if (pcs != nullptr) { 3443 int max_pc = 0; 3444 for (uint i = 0; i < pc_limit; i++) { 3445 max_pc = (max_pc < pcs[i]) ? pcs[i] : max_pc; 3446 } 3447 pc_digits = ((max_pc < 4096) ? 3 : ((max_pc < 65536) ? 4 : ((max_pc < 65536*256) ? 6 : 8))); // #chars required for pc 3448 } 3449 int prefix_len = ((pc_digits + sb_chars + tab_size - 1)/tab_size)*tab_size; 3450 3451 bool cut_short = false; 3452 st->print_cr("#"); 3453 st->print("# "); C->tf()->dump_on(st); st->cr(); 3454 st->print_cr("#"); 3455 3456 // For all blocks 3457 int pc = 0x0; // Program counter 3458 char starts_bundle = ' '; 3459 C->regalloc()->dump_frame(); 3460 3461 Node *n = nullptr; 3462 for (uint i = 0; i < C->cfg()->number_of_blocks(); i++) { 3463 if (VMThread::should_terminate()) { 3464 cut_short = true; 3465 break; 3466 } 3467 Block* block = C->cfg()->get_block(i); 3468 if (block->is_connector() && !Verbose) { 3469 continue; 3470 } 3471 n = block->head(); 3472 if ((pcs != nullptr) && (n->_idx < pc_limit)) { 3473 pc = pcs[n->_idx]; 3474 st->print("%*.*x", pc_digits, pc_digits, pc); 3475 } 3476 st->fill_to(prefix_len); 3477 block->dump_head(C->cfg(), st); 3478 if (block->is_connector()) { 3479 st->fill_to(prefix_len); 3480 st->print_cr("# Empty connector block"); 3481 } else if (block->num_preds() == 2 && block->pred(1)->is_CatchProj() && block->pred(1)->as_CatchProj()->_con == CatchProjNode::fall_through_index) { 3482 st->fill_to(prefix_len); 3483 st->print_cr("# Block is sole successor of call"); 3484 } 3485 3486 // For all instructions 3487 Node *delay = nullptr; 3488 for (uint j = 0; j < block->number_of_nodes(); j++) { 3489 if (VMThread::should_terminate()) { 3490 cut_short = true; 3491 break; 3492 } 3493 n = block->get_node(j); 3494 if (valid_bundle_info(n)) { 3495 Bundle* bundle = node_bundling(n); 3496 if (bundle->used_in_unconditional_delay()) { 3497 delay = n; 3498 continue; 3499 } 3500 if (bundle->starts_bundle()) { 3501 starts_bundle = '+'; 3502 } 3503 } 3504 3505 if (WizardMode) { 3506 n->dump(); 3507 } 3508 3509 if( !n->is_Region() && // Dont print in the Assembly 3510 !n->is_Phi() && // a few noisely useless nodes 3511 !n->is_Proj() && 3512 !n->is_MachTemp() && 3513 !n->is_SafePointScalarObject() && 3514 !n->is_Catch() && // Would be nice to print exception table targets 3515 !n->is_MergeMem() && // Not very interesting 3516 !n->is_top() && // Debug info table constants 3517 !(n->is_Con() && !n->is_Mach())// Debug info table constants 3518 ) { 3519 if ((pcs != nullptr) && (n->_idx < pc_limit)) { 3520 pc = pcs[n->_idx]; 3521 st->print("%*.*x", pc_digits, pc_digits, pc); 3522 } else { 3523 st->fill_to(pc_digits); 3524 } 3525 st->print(" %c ", starts_bundle); 3526 starts_bundle = ' '; 3527 st->fill_to(prefix_len); 3528 n->format(C->regalloc(), st); 3529 st->cr(); 3530 } 3531 3532 // If we have an instruction with a delay slot, and have seen a delay, 3533 // then back up and print it 3534 if (valid_bundle_info(n) && node_bundling(n)->use_unconditional_delay()) { 3535 // Coverity finding - Explicit null dereferenced. 3536 guarantee(delay != nullptr, "no unconditional delay instruction"); 3537 if (WizardMode) delay->dump(); 3538 3539 if (node_bundling(delay)->starts_bundle()) 3540 starts_bundle = '+'; 3541 if ((pcs != nullptr) && (n->_idx < pc_limit)) { 3542 pc = pcs[n->_idx]; 3543 st->print("%*.*x", pc_digits, pc_digits, pc); 3544 } else { 3545 st->fill_to(pc_digits); 3546 } 3547 st->print(" %c ", starts_bundle); 3548 starts_bundle = ' '; 3549 st->fill_to(prefix_len); 3550 delay->format(C->regalloc(), st); 3551 st->cr(); 3552 delay = nullptr; 3553 } 3554 3555 // Dump the exception table as well 3556 if( n->is_Catch() && (Verbose || WizardMode) ) { 3557 // Print the exception table for this offset 3558 _handler_table.print_subtable_for(pc); 3559 } 3560 st->bol(); // Make sure we start on a new line 3561 } 3562 st->cr(); // one empty line between blocks 3563 assert(cut_short || delay == nullptr, "no unconditional delay branch"); 3564 } // End of per-block dump 3565 3566 if (cut_short) st->print_cr("*** disassembly is cut short ***"); 3567 } 3568 #endif 3569 3570 #ifndef PRODUCT 3571 void PhaseOutput::print_statistics() { 3572 Scheduling::print_statistics(); 3573 } 3574 #endif