twisti@2047: /* stefank@2314: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. twisti@2047: * Copyright 2008, 2009 Red Hat, Inc. twisti@2047: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. twisti@2047: * twisti@2047: * This code is free software; you can redistribute it and/or modify it twisti@2047: * under the terms of the GNU General Public License version 2 only, as twisti@2047: * published by the Free Software Foundation. twisti@2047: * twisti@2047: * This code is distributed in the hope that it will be useful, but WITHOUT twisti@2047: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or twisti@2047: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License twisti@2047: * version 2 for more details (a copy is included in the LICENSE file that twisti@2047: * accompanied this code). twisti@2047: * twisti@2047: * You should have received a copy of the GNU General Public License version twisti@2047: * 2 along with this work; if not, write to the Free Software Foundation, twisti@2047: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. twisti@2047: * twisti@2047: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA twisti@2047: * or visit www.oracle.com if you need additional information or have any twisti@2047: * questions. twisti@2047: * twisti@2047: */ twisti@2047: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "ci/ciTypeFlow.hpp" stefank@2314: #include "memory/allocation.hpp" stefank@2314: #include "shark/llvmHeaders.hpp" stefank@2314: #include "shark/llvmValue.hpp" stefank@2314: #include "shark/sharkBuilder.hpp" stefank@2314: #include "shark/sharkEntry.hpp" stefank@2314: #include "shark/sharkFunction.hpp" stefank@2314: #include "shark/sharkState.hpp" stefank@2314: #include "shark/sharkTopLevelBlock.hpp" stefank@2314: #include "shark/shark_globals.hpp" stefank@2314: #include "utilities/debug.hpp" twisti@2047: twisti@2047: using namespace llvm; twisti@2047: twisti@2047: void SharkFunction::initialize(const char *name) { twisti@2047: // Create the function twisti@2047: _function = Function::Create( twisti@2047: entry_point_type(), twisti@2047: GlobalVariable::InternalLinkage, twisti@2047: name); twisti@2047: twisti@2047: // Get our arguments twisti@2047: Function::arg_iterator ai = function()->arg_begin(); twisti@2047: Argument *method = ai++; twisti@2047: method->setName("method"); twisti@2047: Argument *osr_buf = NULL; twisti@2047: if (is_osr()) { twisti@2047: osr_buf = ai++; twisti@2047: osr_buf->setName("osr_buf"); twisti@2047: } twisti@2047: Argument *base_pc = ai++; twisti@2047: base_pc->setName("base_pc"); twisti@2047: code_buffer()->set_base_pc(base_pc); twisti@2047: Argument *thread = ai++; twisti@2047: thread->setName("thread"); twisti@2047: set_thread(thread); twisti@2047: twisti@2047: // Create the list of blocks twisti@2047: set_block_insertion_point(NULL); twisti@2047: _blocks = NEW_RESOURCE_ARRAY(SharkTopLevelBlock*, block_count()); twisti@2047: for (int i = 0; i < block_count(); i++) { twisti@2047: ciTypeFlow::Block *b = flow()->pre_order_at(i); twisti@2047: twisti@2047: // Work around a bug in pre_order_at() that does not return twisti@2047: // the correct pre-ordering. If pre_order_at() were correct twisti@2047: // this line could simply be: twisti@2047: // _blocks[i] = new SharkTopLevelBlock(this, b); twisti@2047: _blocks[b->pre_order()] = new SharkTopLevelBlock(this, b); twisti@2047: } twisti@2047: twisti@2047: // Walk the tree from the start block to determine which twisti@2047: // blocks are entered and which blocks require phis twisti@2047: SharkTopLevelBlock *start_block = block(flow()->start_block_num()); twisti@4443: if (is_osr() && start_block->stack_depth_at_entry() != 0) { twisti@4443: env()->record_method_not_compilable("can't compile OSR block with incoming stack-depth > 0"); twisti@4443: return; twisti@4443: } twisti@2047: assert(start_block->start() == flow()->start_bci(), "blocks out of order"); twisti@2047: start_block->enter(); twisti@2047: twisti@2047: // Initialize all entered blocks twisti@2047: for (int i = 0; i < block_count(); i++) { twisti@2047: if (block(i)->entered()) twisti@2047: block(i)->initialize(); twisti@2047: } twisti@2047: twisti@2047: // Create and push our stack frame twisti@2047: set_block_insertion_point(&function()->front()); twisti@2047: builder()->SetInsertPoint(CreateBlock()); twisti@2047: _stack = SharkStack::CreateBuildAndPushFrame(this, method); twisti@2047: twisti@2047: // Create the entry state twisti@2047: SharkState *entry_state; twisti@2047: if (is_osr()) { twisti@2047: entry_state = new SharkOSREntryState(start_block, method, osr_buf); twisti@2047: twisti@2047: // Free the OSR buffer twisti@2047: builder()->CreateCall(builder()->osr_migration_end(), osr_buf); twisti@2047: } twisti@2047: else { twisti@2047: entry_state = new SharkNormalEntryState(start_block, method); twisti@2047: twisti@2047: // Lock if necessary twisti@2047: if (is_synchronized()) { twisti@2047: SharkTopLevelBlock *locker = twisti@2047: new SharkTopLevelBlock(this, start_block->ciblock()); twisti@2047: locker->add_incoming(entry_state); twisti@2047: twisti@2047: set_block_insertion_point(start_block->entry_block()); twisti@2047: locker->acquire_method_lock(); twisti@2047: twisti@2047: entry_state = locker->current_state(); twisti@2047: } twisti@2047: } twisti@2047: twisti@2047: // Transition into the method proper twisti@2047: start_block->add_incoming(entry_state); twisti@2047: builder()->CreateBr(start_block->entry_block()); twisti@2047: twisti@2047: // Parse the blocks twisti@2047: for (int i = 0; i < block_count(); i++) { twisti@2047: if (!block(i)->entered()) twisti@2047: continue; twisti@2047: twisti@2047: if (i + 1 < block_count()) twisti@2047: set_block_insertion_point(block(i + 1)->entry_block()); twisti@2047: else twisti@2047: set_block_insertion_point(NULL); twisti@2047: twisti@2047: block(i)->emit_IR(); twisti@2047: } twisti@2047: do_deferred_zero_checks(); twisti@2047: } twisti@2047: twisti@2047: class DeferredZeroCheck : public SharkTargetInvariants { twisti@2047: public: twisti@2047: DeferredZeroCheck(SharkTopLevelBlock* block, SharkValue* value) twisti@2047: : SharkTargetInvariants(block), twisti@2047: _block(block), twisti@2047: _value(value), twisti@2047: _bci(block->bci()), twisti@2047: _state(block->current_state()->copy()), twisti@2047: _check_block(builder()->GetInsertBlock()), twisti@2047: _continue_block(function()->CreateBlock("not_zero")) { twisti@2047: builder()->SetInsertPoint(continue_block()); twisti@2047: } twisti@2047: twisti@2047: private: twisti@2047: SharkTopLevelBlock* _block; twisti@2047: SharkValue* _value; twisti@2047: int _bci; twisti@2047: SharkState* _state; twisti@2047: BasicBlock* _check_block; twisti@2047: BasicBlock* _continue_block; twisti@2047: twisti@2047: public: twisti@2047: SharkTopLevelBlock* block() const { twisti@2047: return _block; twisti@2047: } twisti@2047: SharkValue* value() const { twisti@2047: return _value; twisti@2047: } twisti@2047: int bci() const { twisti@2047: return _bci; twisti@2047: } twisti@2047: SharkState* state() const { twisti@2047: return _state; twisti@2047: } twisti@2047: BasicBlock* check_block() const { twisti@2047: return _check_block; twisti@2047: } twisti@2047: BasicBlock* continue_block() const { twisti@2047: return _continue_block; twisti@2047: } twisti@2047: twisti@2047: public: twisti@2047: SharkFunction* function() const { twisti@2047: return block()->function(); twisti@2047: } twisti@2047: twisti@2047: public: twisti@2047: void process() const { twisti@2047: builder()->SetInsertPoint(check_block()); twisti@2047: block()->do_deferred_zero_check(value(), bci(), state(), continue_block()); twisti@2047: } twisti@2047: }; twisti@2047: twisti@2047: void SharkFunction::add_deferred_zero_check(SharkTopLevelBlock* block, twisti@2047: SharkValue* value) { twisti@2047: deferred_zero_checks()->append(new DeferredZeroCheck(block, value)); twisti@2047: } twisti@2047: twisti@2047: void SharkFunction::do_deferred_zero_checks() { twisti@2047: for (int i = 0; i < deferred_zero_checks()->length(); i++) twisti@2047: deferred_zero_checks()->at(i)->process(); twisti@2047: }