src/share/vm/opto/output.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 1
2d8a650513c2
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial