Fri, 11 Jan 2013 16:47:23 -0800
8005818: Shark: fix OSR for non-empty incoming stack
Reviewed-by: twisti
Contributed-by: Roman Kennke <rkennke@redhat.com>
twisti@2047 | 1 | /* |
stefank@2314 | 2 | * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. |
twisti@2047 | 3 | * Copyright 2008, 2009 Red Hat, Inc. |
twisti@2047 | 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
twisti@2047 | 5 | * |
twisti@2047 | 6 | * This code is free software; you can redistribute it and/or modify it |
twisti@2047 | 7 | * under the terms of the GNU General Public License version 2 only, as |
twisti@2047 | 8 | * published by the Free Software Foundation. |
twisti@2047 | 9 | * |
twisti@2047 | 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
twisti@2047 | 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
twisti@2047 | 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
twisti@2047 | 13 | * version 2 for more details (a copy is included in the LICENSE file that |
twisti@2047 | 14 | * accompanied this code). |
twisti@2047 | 15 | * |
twisti@2047 | 16 | * You should have received a copy of the GNU General Public License version |
twisti@2047 | 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
twisti@2047 | 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
twisti@2047 | 19 | * |
twisti@2047 | 20 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
twisti@2047 | 21 | * or visit www.oracle.com if you need additional information or have any |
twisti@2047 | 22 | * questions. |
twisti@2047 | 23 | * |
twisti@2047 | 24 | */ |
twisti@2047 | 25 | |
stefank@2314 | 26 | #include "precompiled.hpp" |
stefank@2314 | 27 | #include "ci/ciTypeFlow.hpp" |
stefank@2314 | 28 | #include "memory/allocation.hpp" |
stefank@2314 | 29 | #include "shark/llvmHeaders.hpp" |
stefank@2314 | 30 | #include "shark/llvmValue.hpp" |
stefank@2314 | 31 | #include "shark/sharkBuilder.hpp" |
stefank@2314 | 32 | #include "shark/sharkEntry.hpp" |
stefank@2314 | 33 | #include "shark/sharkFunction.hpp" |
stefank@2314 | 34 | #include "shark/sharkState.hpp" |
stefank@2314 | 35 | #include "shark/sharkTopLevelBlock.hpp" |
stefank@2314 | 36 | #include "shark/shark_globals.hpp" |
stefank@2314 | 37 | #include "utilities/debug.hpp" |
twisti@2047 | 38 | |
twisti@2047 | 39 | using namespace llvm; |
twisti@2047 | 40 | |
twisti@2047 | 41 | void SharkFunction::initialize(const char *name) { |
twisti@2047 | 42 | // Create the function |
twisti@2047 | 43 | _function = Function::Create( |
twisti@2047 | 44 | entry_point_type(), |
twisti@2047 | 45 | GlobalVariable::InternalLinkage, |
twisti@2047 | 46 | name); |
twisti@2047 | 47 | |
twisti@2047 | 48 | // Get our arguments |
twisti@2047 | 49 | Function::arg_iterator ai = function()->arg_begin(); |
twisti@2047 | 50 | Argument *method = ai++; |
twisti@2047 | 51 | method->setName("method"); |
twisti@2047 | 52 | Argument *osr_buf = NULL; |
twisti@2047 | 53 | if (is_osr()) { |
twisti@2047 | 54 | osr_buf = ai++; |
twisti@2047 | 55 | osr_buf->setName("osr_buf"); |
twisti@2047 | 56 | } |
twisti@2047 | 57 | Argument *base_pc = ai++; |
twisti@2047 | 58 | base_pc->setName("base_pc"); |
twisti@2047 | 59 | code_buffer()->set_base_pc(base_pc); |
twisti@2047 | 60 | Argument *thread = ai++; |
twisti@2047 | 61 | thread->setName("thread"); |
twisti@2047 | 62 | set_thread(thread); |
twisti@2047 | 63 | |
twisti@2047 | 64 | // Create the list of blocks |
twisti@2047 | 65 | set_block_insertion_point(NULL); |
twisti@2047 | 66 | _blocks = NEW_RESOURCE_ARRAY(SharkTopLevelBlock*, block_count()); |
twisti@2047 | 67 | for (int i = 0; i < block_count(); i++) { |
twisti@2047 | 68 | ciTypeFlow::Block *b = flow()->pre_order_at(i); |
twisti@2047 | 69 | |
twisti@2047 | 70 | // Work around a bug in pre_order_at() that does not return |
twisti@2047 | 71 | // the correct pre-ordering. If pre_order_at() were correct |
twisti@2047 | 72 | // this line could simply be: |
twisti@2047 | 73 | // _blocks[i] = new SharkTopLevelBlock(this, b); |
twisti@2047 | 74 | _blocks[b->pre_order()] = new SharkTopLevelBlock(this, b); |
twisti@2047 | 75 | } |
twisti@2047 | 76 | |
twisti@2047 | 77 | // Walk the tree from the start block to determine which |
twisti@2047 | 78 | // blocks are entered and which blocks require phis |
twisti@2047 | 79 | SharkTopLevelBlock *start_block = block(flow()->start_block_num()); |
twisti@4443 | 80 | if (is_osr() && start_block->stack_depth_at_entry() != 0) { |
twisti@4443 | 81 | env()->record_method_not_compilable("can't compile OSR block with incoming stack-depth > 0"); |
twisti@4443 | 82 | return; |
twisti@4443 | 83 | } |
twisti@2047 | 84 | assert(start_block->start() == flow()->start_bci(), "blocks out of order"); |
twisti@2047 | 85 | start_block->enter(); |
twisti@2047 | 86 | |
twisti@2047 | 87 | // Initialize all entered blocks |
twisti@2047 | 88 | for (int i = 0; i < block_count(); i++) { |
twisti@2047 | 89 | if (block(i)->entered()) |
twisti@2047 | 90 | block(i)->initialize(); |
twisti@2047 | 91 | } |
twisti@2047 | 92 | |
twisti@2047 | 93 | // Create and push our stack frame |
twisti@2047 | 94 | set_block_insertion_point(&function()->front()); |
twisti@2047 | 95 | builder()->SetInsertPoint(CreateBlock()); |
twisti@2047 | 96 | _stack = SharkStack::CreateBuildAndPushFrame(this, method); |
twisti@2047 | 97 | |
twisti@2047 | 98 | // Create the entry state |
twisti@2047 | 99 | SharkState *entry_state; |
twisti@2047 | 100 | if (is_osr()) { |
twisti@2047 | 101 | entry_state = new SharkOSREntryState(start_block, method, osr_buf); |
twisti@2047 | 102 | |
twisti@2047 | 103 | // Free the OSR buffer |
twisti@2047 | 104 | builder()->CreateCall(builder()->osr_migration_end(), osr_buf); |
twisti@2047 | 105 | } |
twisti@2047 | 106 | else { |
twisti@2047 | 107 | entry_state = new SharkNormalEntryState(start_block, method); |
twisti@2047 | 108 | |
twisti@2047 | 109 | // Lock if necessary |
twisti@2047 | 110 | if (is_synchronized()) { |
twisti@2047 | 111 | SharkTopLevelBlock *locker = |
twisti@2047 | 112 | new SharkTopLevelBlock(this, start_block->ciblock()); |
twisti@2047 | 113 | locker->add_incoming(entry_state); |
twisti@2047 | 114 | |
twisti@2047 | 115 | set_block_insertion_point(start_block->entry_block()); |
twisti@2047 | 116 | locker->acquire_method_lock(); |
twisti@2047 | 117 | |
twisti@2047 | 118 | entry_state = locker->current_state(); |
twisti@2047 | 119 | } |
twisti@2047 | 120 | } |
twisti@2047 | 121 | |
twisti@2047 | 122 | // Transition into the method proper |
twisti@2047 | 123 | start_block->add_incoming(entry_state); |
twisti@2047 | 124 | builder()->CreateBr(start_block->entry_block()); |
twisti@2047 | 125 | |
twisti@2047 | 126 | // Parse the blocks |
twisti@2047 | 127 | for (int i = 0; i < block_count(); i++) { |
twisti@2047 | 128 | if (!block(i)->entered()) |
twisti@2047 | 129 | continue; |
twisti@2047 | 130 | |
twisti@2047 | 131 | if (i + 1 < block_count()) |
twisti@2047 | 132 | set_block_insertion_point(block(i + 1)->entry_block()); |
twisti@2047 | 133 | else |
twisti@2047 | 134 | set_block_insertion_point(NULL); |
twisti@2047 | 135 | |
twisti@2047 | 136 | block(i)->emit_IR(); |
twisti@2047 | 137 | } |
twisti@2047 | 138 | do_deferred_zero_checks(); |
twisti@2047 | 139 | } |
twisti@2047 | 140 | |
twisti@2047 | 141 | class DeferredZeroCheck : public SharkTargetInvariants { |
twisti@2047 | 142 | public: |
twisti@2047 | 143 | DeferredZeroCheck(SharkTopLevelBlock* block, SharkValue* value) |
twisti@2047 | 144 | : SharkTargetInvariants(block), |
twisti@2047 | 145 | _block(block), |
twisti@2047 | 146 | _value(value), |
twisti@2047 | 147 | _bci(block->bci()), |
twisti@2047 | 148 | _state(block->current_state()->copy()), |
twisti@2047 | 149 | _check_block(builder()->GetInsertBlock()), |
twisti@2047 | 150 | _continue_block(function()->CreateBlock("not_zero")) { |
twisti@2047 | 151 | builder()->SetInsertPoint(continue_block()); |
twisti@2047 | 152 | } |
twisti@2047 | 153 | |
twisti@2047 | 154 | private: |
twisti@2047 | 155 | SharkTopLevelBlock* _block; |
twisti@2047 | 156 | SharkValue* _value; |
twisti@2047 | 157 | int _bci; |
twisti@2047 | 158 | SharkState* _state; |
twisti@2047 | 159 | BasicBlock* _check_block; |
twisti@2047 | 160 | BasicBlock* _continue_block; |
twisti@2047 | 161 | |
twisti@2047 | 162 | public: |
twisti@2047 | 163 | SharkTopLevelBlock* block() const { |
twisti@2047 | 164 | return _block; |
twisti@2047 | 165 | } |
twisti@2047 | 166 | SharkValue* value() const { |
twisti@2047 | 167 | return _value; |
twisti@2047 | 168 | } |
twisti@2047 | 169 | int bci() const { |
twisti@2047 | 170 | return _bci; |
twisti@2047 | 171 | } |
twisti@2047 | 172 | SharkState* state() const { |
twisti@2047 | 173 | return _state; |
twisti@2047 | 174 | } |
twisti@2047 | 175 | BasicBlock* check_block() const { |
twisti@2047 | 176 | return _check_block; |
twisti@2047 | 177 | } |
twisti@2047 | 178 | BasicBlock* continue_block() const { |
twisti@2047 | 179 | return _continue_block; |
twisti@2047 | 180 | } |
twisti@2047 | 181 | |
twisti@2047 | 182 | public: |
twisti@2047 | 183 | SharkFunction* function() const { |
twisti@2047 | 184 | return block()->function(); |
twisti@2047 | 185 | } |
twisti@2047 | 186 | |
twisti@2047 | 187 | public: |
twisti@2047 | 188 | void process() const { |
twisti@2047 | 189 | builder()->SetInsertPoint(check_block()); |
twisti@2047 | 190 | block()->do_deferred_zero_check(value(), bci(), state(), continue_block()); |
twisti@2047 | 191 | } |
twisti@2047 | 192 | }; |
twisti@2047 | 193 | |
twisti@2047 | 194 | void SharkFunction::add_deferred_zero_check(SharkTopLevelBlock* block, |
twisti@2047 | 195 | SharkValue* value) { |
twisti@2047 | 196 | deferred_zero_checks()->append(new DeferredZeroCheck(block, value)); |
twisti@2047 | 197 | } |
twisti@2047 | 198 | |
twisti@2047 | 199 | void SharkFunction::do_deferred_zero_checks() { |
twisti@2047 | 200 | for (int i = 0; i < deferred_zero_checks()->length(); i++) |
twisti@2047 | 201 | deferred_zero_checks()->at(i)->process(); |
twisti@2047 | 202 | } |