1612N/A/*
1879N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
1612N/A * Copyright 2008, 2009, 2010 Red Hat, Inc.
1612N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1612N/A *
1612N/A * This code is free software; you can redistribute it and/or modify it
1612N/A * under the terms of the GNU General Public License version 2 only, as
1612N/A * published by the Free Software Foundation.
1612N/A *
1612N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1612N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1612N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1612N/A * version 2 for more details (a copy is included in the LICENSE file that
1612N/A * accompanied this code).
1612N/A *
1612N/A * You should have received a copy of the GNU General Public License version
1612N/A * 2 along with this work; if not, write to the Free Software Foundation,
1612N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1612N/A *
1612N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1612N/A * or visit www.oracle.com if you need additional information or have any
1612N/A * questions.
1612N/A *
1612N/A */
1612N/A
1879N/A#ifndef SHARE_VM_SHARK_SHARKTOPLEVELBLOCK_HPP
1879N/A#define SHARE_VM_SHARK_SHARKTOPLEVELBLOCK_HPP
1879N/A
1879N/A#include "ci/ciStreams.hpp"
1879N/A#include "ci/ciType.hpp"
1879N/A#include "ci/ciTypeFlow.hpp"
1879N/A#include "interpreter/bytecodes.hpp"
1879N/A#include "memory/allocation.hpp"
1879N/A#include "shark/llvmHeaders.hpp"
1879N/A#include "shark/sharkBlock.hpp"
1879N/A#include "shark/sharkBuilder.hpp"
1879N/A#include "shark/sharkFunction.hpp"
1879N/A#include "shark/sharkState.hpp"
1879N/A#include "shark/sharkValue.hpp"
1879N/A
1612N/Aclass SharkTopLevelBlock : public SharkBlock {
1612N/A public:
1612N/A SharkTopLevelBlock(SharkFunction* function, ciTypeFlow::Block* ciblock)
1612N/A : SharkBlock(function),
1612N/A _function(function),
1612N/A _ciblock(ciblock),
1612N/A _entered(false),
1612N/A _has_trap(false),
1612N/A _needs_phis(false),
1612N/A _entry_state(NULL),
1612N/A _entry_block(NULL) {}
1612N/A
1612N/A private:
1612N/A SharkFunction* _function;
1612N/A ciTypeFlow::Block* _ciblock;
1612N/A
1612N/A public:
1612N/A SharkFunction* function() const {
1612N/A return _function;
1612N/A }
1612N/A ciTypeFlow::Block* ciblock() const {
1612N/A return _ciblock;
1612N/A }
1612N/A
1612N/A // Function properties
1612N/A public:
1612N/A SharkStack* stack() const {
1612N/A return function()->stack();
1612N/A }
1612N/A
1612N/A // Typeflow properties
1612N/A public:
1612N/A int index() const {
1612N/A return ciblock()->pre_order();
1612N/A }
1612N/A bool is_backedge_copy() const {
1612N/A return ciblock()->is_backedge_copy();
1612N/A }
1612N/A int stack_depth_at_entry() const {
1612N/A return ciblock()->stack_size();
1612N/A }
1612N/A ciType* local_type_at_entry(int index) const {
1612N/A return ciblock()->local_type_at(index);
1612N/A }
1612N/A ciType* stack_type_at_entry(int slot) const {
1612N/A return ciblock()->stack_type_at(slot);
1612N/A }
1612N/A int start() const {
1612N/A return ciblock()->start();
1612N/A }
1612N/A int limit() const {
1612N/A return ciblock()->limit();
1612N/A }
1612N/A bool falls_through() const {
1612N/A return ciblock()->control() == ciBlock::fall_through_bci;
1612N/A }
1612N/A int num_successors() const {
1612N/A return ciblock()->successors()->length();
1612N/A }
1612N/A SharkTopLevelBlock* successor(int index) const {
1612N/A return function()->block(ciblock()->successors()->at(index)->pre_order());
1612N/A }
1612N/A SharkTopLevelBlock* bci_successor(int bci) const;
1612N/A
1612N/A // Exceptions
1612N/A private:
1612N/A GrowableArray<ciExceptionHandler*>* _exc_handlers;
1612N/A GrowableArray<SharkTopLevelBlock*>* _exceptions;
1612N/A
1612N/A private:
1612N/A void compute_exceptions();
1612N/A
1612N/A private:
1612N/A int num_exceptions() const {
1612N/A return _exc_handlers->length();
1612N/A }
1612N/A ciExceptionHandler* exc_handler(int index) const {
1612N/A return _exc_handlers->at(index);
1612N/A }
1612N/A SharkTopLevelBlock* exception(int index) const {
1612N/A return _exceptions->at(index);
1612N/A }
1612N/A
1612N/A // Traps
1612N/A private:
1612N/A bool _has_trap;
1612N/A int _trap_request;
1612N/A int _trap_bci;
1612N/A
1612N/A void set_trap(int trap_request, int trap_bci) {
1612N/A assert(!has_trap(), "shouldn't have");
1612N/A _has_trap = true;
1612N/A _trap_request = trap_request;
1612N/A _trap_bci = trap_bci;
1612N/A }
1612N/A
1612N/A private:
1612N/A bool has_trap() {
1612N/A return _has_trap;
1612N/A }
1612N/A int trap_request() {
1612N/A assert(has_trap(), "should have");
1612N/A return _trap_request;
1612N/A }
1612N/A int trap_bci() {
1612N/A assert(has_trap(), "should have");
1612N/A return _trap_bci;
1612N/A }
1612N/A
1612N/A private:
1612N/A void scan_for_traps();
1612N/A
1612N/A private:
1612N/A bool static_field_ok_in_clinit(ciField* field);
1612N/A
1612N/A // Entry state
1612N/A private:
1612N/A bool _entered;
1612N/A bool _needs_phis;
1612N/A
1612N/A public:
1612N/A bool entered() const {
1612N/A return _entered;
1612N/A }
1612N/A bool needs_phis() const {
1612N/A return _needs_phis;
1612N/A }
1612N/A
1612N/A private:
1612N/A void enter(SharkTopLevelBlock* predecessor, bool is_exception);
1612N/A
1612N/A public:
1612N/A void enter() {
1612N/A enter(NULL, false);
1612N/A }
1612N/A
1612N/A private:
1612N/A SharkState* _entry_state;
1612N/A
1612N/A private:
1612N/A SharkState* entry_state();
1612N/A
1612N/A private:
1612N/A llvm::BasicBlock* _entry_block;
1612N/A
1612N/A public:
1612N/A llvm::BasicBlock* entry_block() const {
1612N/A return _entry_block;
1612N/A }
1612N/A
1612N/A public:
1612N/A void initialize();
1612N/A
1612N/A public:
1612N/A void add_incoming(SharkState* incoming_state);
1612N/A
1612N/A // Method
1612N/A public:
1612N/A llvm::Value* method() {
1612N/A return current_state()->method();
1612N/A }
1612N/A
1612N/A // Temporary oop storage
1612N/A public:
1612N/A void set_oop_tmp(llvm::Value* value) {
1612N/A assert(value, "value must be non-NULL (will be reset by get_oop_tmp)");
1612N/A assert(!current_state()->oop_tmp(), "oop_tmp gets and sets must match");
1612N/A current_state()->set_oop_tmp(value);
1612N/A }
1612N/A llvm::Value* get_oop_tmp() {
1612N/A llvm::Value* value = current_state()->oop_tmp();
1612N/A assert(value, "oop_tmp gets and sets must match");
1612N/A current_state()->set_oop_tmp(NULL);
1612N/A return value;
1612N/A }
1612N/A
1612N/A // Cache and decache
1612N/A private:
1612N/A void decache_for_Java_call(ciMethod* callee);
1612N/A void cache_after_Java_call(ciMethod* callee);
1612N/A void decache_for_VM_call();
1612N/A void cache_after_VM_call();
1612N/A void decache_for_trap();
1612N/A
1612N/A // Monitors
1612N/A private:
1612N/A int num_monitors() {
1612N/A return current_state()->num_monitors();
1612N/A }
1612N/A int set_num_monitors(int num_monitors) {
1612N/A current_state()->set_num_monitors(num_monitors);
1612N/A }
1612N/A
1612N/A // Code generation
1612N/A public:
1612N/A void emit_IR();
1612N/A
1612N/A // Branch helpers
1612N/A private:
1612N/A void do_branch(int successor_index);
1612N/A
1612N/A // Zero checks
1612N/A private:
1612N/A void do_zero_check(SharkValue* value);
1612N/A void zero_check_value(SharkValue* value, llvm::BasicBlock* continue_block);
1612N/A
1612N/A public:
1612N/A void do_deferred_zero_check(SharkValue* value,
1612N/A int bci,
1612N/A SharkState* saved_state,
1612N/A llvm::BasicBlock* continue_block);
1612N/A // Exceptions
1612N/A private:
1612N/A llvm::Value* pending_exception_address() const {
1612N/A return builder()->CreateAddressOfStructEntry(
1612N/A thread(), Thread::pending_exception_offset(),
1612N/A llvm::PointerType::getUnqual(SharkType::oop_type()),
1612N/A "pending_exception_addr");
1612N/A }
1612N/A llvm::LoadInst* get_pending_exception() const {
1612N/A return builder()->CreateLoad(
1612N/A pending_exception_address(), "pending_exception");
1612N/A }
1612N/A void clear_pending_exception() const {
1612N/A builder()->CreateStore(LLVMValue::null(), pending_exception_address());
1612N/A }
1612N/A public:
1612N/A enum ExceptionActionMask {
1612N/A // The actual bitmasks that things test against
1612N/A EAM_CHECK = 1, // whether to check for pending exceptions
1612N/A EAM_HANDLE = 2, // whether to attempt to handle pending exceptions
1612N/A EAM_MONITOR_FUDGE = 4, // whether the monitor count needs adjusting
1612N/A
1612N/A // More convenient values for passing
1612N/A EX_CHECK_NONE = 0,
1612N/A EX_CHECK_NO_CATCH = EAM_CHECK,
1612N/A EX_CHECK_FULL = EAM_CHECK | EAM_HANDLE
1612N/A };
1612N/A void check_pending_exception(int action);
1612N/A void handle_exception(llvm::Value* exception, int action);
1612N/A void marshal_exception_fast(int num_options);
1612N/A void marshal_exception_slow(int num_options);
1612N/A llvm::BasicBlock* handler_for_exception(int index);
1612N/A
1612N/A // VM calls
1612N/A private:
1612N/A llvm::CallInst* call_vm(llvm::Value* callee,
1612N/A llvm::Value** args_start,
1612N/A llvm::Value** args_end,
1612N/A int exception_action) {
1612N/A decache_for_VM_call();
1612N/A stack()->CreateSetLastJavaFrame();
1612N/A llvm::CallInst *res = builder()->CreateCall(callee, args_start, args_end);
1612N/A stack()->CreateResetLastJavaFrame();
1612N/A cache_after_VM_call();
1612N/A if (exception_action & EAM_CHECK) {
1612N/A check_pending_exception(exception_action);
1612N/A current_state()->set_has_safepointed(true);
1612N/A }
1612N/A return res;
1612N/A }
1612N/A
1612N/A public:
1612N/A llvm::CallInst* call_vm(llvm::Value* callee,
1612N/A int exception_action) {
1612N/A llvm::Value *args[] = {thread()};
1612N/A return call_vm(callee, args, args + 1, exception_action);
1612N/A }
1612N/A llvm::CallInst* call_vm(llvm::Value* callee,
1612N/A llvm::Value* arg1,
1612N/A int exception_action) {
1612N/A llvm::Value *args[] = {thread(), arg1};
1612N/A return call_vm(callee, args, args + 2, exception_action);
1612N/A }
1612N/A llvm::CallInst* call_vm(llvm::Value* callee,
1612N/A llvm::Value* arg1,
1612N/A llvm::Value* arg2,
1612N/A int exception_action) {
1612N/A llvm::Value *args[] = {thread(), arg1, arg2};
1612N/A return call_vm(callee, args, args + 3, exception_action);
1612N/A }
1612N/A llvm::CallInst* call_vm(llvm::Value* callee,
1612N/A llvm::Value* arg1,
1612N/A llvm::Value* arg2,
1612N/A llvm::Value* arg3,
1612N/A int exception_action) {
1612N/A llvm::Value *args[] = {thread(), arg1, arg2, arg3};
1612N/A return call_vm(callee, args, args + 4, exception_action);
1612N/A }
1612N/A
1612N/A // VM call oop return handling
1612N/A private:
1612N/A llvm::LoadInst* get_vm_result() const {
1612N/A llvm::Value *addr = builder()->CreateAddressOfStructEntry(
1612N/A thread(), JavaThread::vm_result_offset(),
1612N/A llvm::PointerType::getUnqual(SharkType::oop_type()),
1612N/A "vm_result_addr");
1612N/A llvm::LoadInst *result = builder()->CreateLoad(addr, "vm_result");
1612N/A builder()->CreateStore(LLVMValue::null(), addr);
1612N/A return result;
1612N/A }
1612N/A
1612N/A // Synchronization
1612N/A private:
1612N/A void acquire_lock(llvm::Value* lockee, int exception_action);
1612N/A void release_lock(int exception_action);
1612N/A
1612N/A public:
1612N/A void acquire_method_lock();
1612N/A
1612N/A // Bounds checks
1612N/A private:
1612N/A void check_bounds(SharkValue* array, SharkValue* index);
1612N/A
1612N/A // Safepoints
1612N/A private:
1612N/A void maybe_add_safepoint();
1612N/A void maybe_add_backedge_safepoint();
1612N/A
1612N/A // Loop safepoint removal
1612N/A private:
1612N/A bool _can_reach_visited;
1612N/A
1612N/A bool can_reach(SharkTopLevelBlock* other);
1612N/A bool can_reach_helper(SharkTopLevelBlock* other);
1612N/A
1612N/A // Traps
1612N/A private:
1612N/A llvm::BasicBlock* make_trap(int trap_bci, int trap_request);
1612N/A void do_trap(int trap_request);
1612N/A
1612N/A // Returns
1612N/A private:
1612N/A void call_register_finalizer(llvm::Value* receiver);
1612N/A void handle_return(BasicType type, llvm::Value* exception);
1612N/A
1612N/A // arraylength
1612N/A private:
1612N/A void do_arraylength();
1612N/A
1612N/A // *aload and *astore
1612N/A private:
1612N/A void do_aload(BasicType basic_type);
1612N/A void do_astore(BasicType basic_type);
1612N/A
1612N/A // *return and athrow
1612N/A private:
1612N/A void do_return(BasicType type);
1612N/A void do_athrow();
1612N/A
1612N/A // goto*
1612N/A private:
1612N/A void do_goto();
1612N/A
1612N/A // jsr* and ret
1612N/A private:
1612N/A void do_jsr();
1612N/A void do_ret();
1612N/A
1612N/A // if*
1612N/A private:
1612N/A void do_if_helper(llvm::ICmpInst::Predicate p,
1612N/A llvm::Value* b,
1612N/A llvm::Value* a,
1612N/A SharkState* if_taken_state,
1612N/A SharkState* not_taken_state);
1612N/A void do_if(llvm::ICmpInst::Predicate p, SharkValue* b, SharkValue* a);
1612N/A
1612N/A // tableswitch and lookupswitch
1612N/A private:
1612N/A void do_switch();
1612N/A
1612N/A // invoke*
1612N/A private:
1612N/A ciMethod* improve_virtual_call(ciMethod* caller,
1612N/A ciInstanceKlass* klass,
1612N/A ciMethod* dest_method,
1612N/A ciType* receiver_type);
1612N/A llvm::Value* get_direct_callee(ciMethod* method);
1612N/A llvm::Value* get_virtual_callee(SharkValue* receiver, int vtable_index);
1612N/A llvm::Value* get_interface_callee(SharkValue* receiver, ciMethod* method);
1612N/A
1612N/A void do_call();
1612N/A
1612N/A // checkcast and instanceof
1612N/A private:
1612N/A bool static_subtype_check(ciKlass* check_klass, ciKlass* object_klass);
1612N/A void do_full_instance_check(ciKlass* klass);
1612N/A void do_trapping_instance_check(ciKlass* klass);
1612N/A
1612N/A void do_instance_check();
1612N/A bool maybe_do_instanceof_if();
1612N/A
1612N/A // new and *newarray
1612N/A private:
1612N/A void do_new();
1612N/A void do_newarray();
1612N/A void do_anewarray();
1612N/A void do_multianewarray();
1612N/A
1612N/A // monitorenter and monitorexit
1612N/A private:
1612N/A void do_monitorenter();
1612N/A void do_monitorexit();
1612N/A};
1879N/A
1879N/A#endif // SHARE_VM_SHARK_SHARKTOPLEVELBLOCK_HPP