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