src/share/vm/opto/output.cpp

Fri, 29 Apr 2016 00:06:10 +0800

author
aoqi
date
Fri, 29 Apr 2016 00:06:10 +0800
changeset 1
2d8a650513c2
parent 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Added MIPS 64-bit port.

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

mercurial