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