aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_C1_C1_GRAPHBUILDER_HPP aoqi@0: #define SHARE_VM_C1_C1_GRAPHBUILDER_HPP aoqi@0: aoqi@0: #include "c1/c1_IR.hpp" aoqi@0: #include "c1/c1_Instruction.hpp" aoqi@0: #include "c1/c1_ValueMap.hpp" aoqi@0: #include "c1/c1_ValueStack.hpp" aoqi@0: #include "ci/ciMethodData.hpp" aoqi@0: #include "ci/ciStreams.hpp" aoqi@0: #include "compiler/compileLog.hpp" aoqi@0: aoqi@0: class MemoryBuffer; aoqi@0: aoqi@0: class GraphBuilder VALUE_OBJ_CLASS_SPEC { aoqi@0: private: aoqi@0: // Per-scope data. These are pushed and popped as we descend into aoqi@0: // inlined methods. Currently in order to generate good code in the aoqi@0: // inliner we have to attempt to inline methods directly into the aoqi@0: // basic block we are parsing; this adds complexity. aoqi@0: class ScopeData: public CompilationResourceObj { aoqi@0: private: aoqi@0: ScopeData* _parent; aoqi@0: // bci-to-block mapping aoqi@0: BlockList* _bci2block; aoqi@0: // Scope aoqi@0: IRScope* _scope; aoqi@0: // Whether this scope or any parent scope has exception handlers aoqi@0: bool _has_handler; aoqi@0: // The bytecodes aoqi@0: ciBytecodeStream* _stream; aoqi@0: aoqi@0: // Work list aoqi@0: BlockList* _work_list; aoqi@0: aoqi@0: // Maximum inline size for this scope aoqi@0: intx _max_inline_size; aoqi@0: // Expression stack depth at point where inline occurred aoqi@0: int _caller_stack_size; aoqi@0: aoqi@0: // The continuation point for the inline. Currently only used in aoqi@0: // multi-block inlines, but eventually would like to use this for aoqi@0: // all inlines for uniformity and simplicity; in this case would aoqi@0: // get the continuation point from the BlockList instead of aoqi@0: // fabricating it anew because Invokes would be considered to be aoqi@0: // BlockEnds. aoqi@0: BlockBegin* _continuation; aoqi@0: aoqi@0: // Was this ScopeData created only for the parsing and inlining of aoqi@0: // a jsr? aoqi@0: bool _parsing_jsr; aoqi@0: // We track the destination bci of the jsr only to determine aoqi@0: // bailout conditions, since we only handle a subset of all of the aoqi@0: // possible jsr-ret control structures. Recursive invocations of a aoqi@0: // jsr are disallowed by the verifier. aoqi@0: int _jsr_entry_bci; aoqi@0: // We need to track the local variable in which the return address aoqi@0: // was stored to ensure we can handle inlining the jsr, because we aoqi@0: // don't handle arbitrary jsr/ret constructs. aoqi@0: int _jsr_ret_addr_local; aoqi@0: // If we are parsing a jsr, the continuation point for rets aoqi@0: BlockBegin* _jsr_continuation; aoqi@0: // Cloned XHandlers for jsr-related ScopeDatas aoqi@0: XHandlers* _jsr_xhandlers; aoqi@0: aoqi@0: // Number of returns seen in this scope aoqi@0: int _num_returns; aoqi@0: aoqi@0: // In order to generate profitable code for inlining, we currently aoqi@0: // have to perform an optimization for single-block inlined aoqi@0: // methods where we continue parsing into the same block. This aoqi@0: // allows us to perform CSE across inlined scopes and to avoid aoqi@0: // storing parameters to the stack. Having a global register aoqi@0: // allocator and being able to perform global CSE would allow this aoqi@0: // code to be removed and thereby simplify the inliner. aoqi@0: BlockBegin* _cleanup_block; // The block to which the return was added aoqi@0: Instruction* _cleanup_return_prev; // Instruction before return instruction aoqi@0: ValueStack* _cleanup_state; // State of that block (not yet pinned) aoqi@0: aoqi@0: public: aoqi@0: ScopeData(ScopeData* parent); aoqi@0: aoqi@0: ScopeData* parent() const { return _parent; } aoqi@0: aoqi@0: BlockList* bci2block() const { return _bci2block; } aoqi@0: void set_bci2block(BlockList* bci2block) { _bci2block = bci2block; } aoqi@0: aoqi@0: // NOTE: this has a different effect when parsing jsrs aoqi@0: BlockBegin* block_at(int bci); aoqi@0: aoqi@0: IRScope* scope() const { return _scope; } aoqi@0: // Has side-effect of setting has_handler flag aoqi@0: void set_scope(IRScope* scope); aoqi@0: aoqi@0: // Whether this or any parent scope has exception handlers aoqi@0: bool has_handler() const { return _has_handler; } aoqi@0: void set_has_handler() { _has_handler = true; } aoqi@0: aoqi@0: // Exception handlers list to be used for this scope aoqi@0: XHandlers* xhandlers() const; aoqi@0: aoqi@0: // How to get a block to be parsed aoqi@0: void add_to_work_list(BlockBegin* block); aoqi@0: // How to remove the next block to be parsed; returns NULL if none left aoqi@0: BlockBegin* remove_from_work_list(); aoqi@0: // Indicates parse is over aoqi@0: bool is_work_list_empty() const; aoqi@0: aoqi@0: ciBytecodeStream* stream() { return _stream; } aoqi@0: void set_stream(ciBytecodeStream* stream) { _stream = stream; } aoqi@0: aoqi@0: intx max_inline_size() const { return _max_inline_size; } aoqi@0: aoqi@0: BlockBegin* continuation() const { return _continuation; } aoqi@0: void set_continuation(BlockBegin* cont) { _continuation = cont; } aoqi@0: aoqi@0: // Indicates whether this ScopeData was pushed only for the aoqi@0: // parsing and inlining of a jsr aoqi@0: bool parsing_jsr() const { return _parsing_jsr; } aoqi@0: void set_parsing_jsr() { _parsing_jsr = true; } aoqi@0: int jsr_entry_bci() const { return _jsr_entry_bci; } aoqi@0: void set_jsr_entry_bci(int bci) { _jsr_entry_bci = bci; } aoqi@0: void set_jsr_return_address_local(int local_no){ _jsr_ret_addr_local = local_no; } aoqi@0: int jsr_return_address_local() const { return _jsr_ret_addr_local; } aoqi@0: // Must be called after scope is set up for jsr ScopeData aoqi@0: void setup_jsr_xhandlers(); aoqi@0: aoqi@0: // The jsr continuation is only used when parsing_jsr is true, and aoqi@0: // is different from the "normal" continuation since we can end up aoqi@0: // doing a return (rather than a ret) from within a subroutine aoqi@0: BlockBegin* jsr_continuation() const { return _jsr_continuation; } aoqi@0: void set_jsr_continuation(BlockBegin* cont) { _jsr_continuation = cont; } aoqi@0: aoqi@0: int num_returns(); aoqi@0: void incr_num_returns(); aoqi@0: aoqi@0: void set_inline_cleanup_info(BlockBegin* block, aoqi@0: Instruction* return_prev, aoqi@0: ValueStack* return_state); aoqi@0: BlockBegin* inline_cleanup_block() const { return _cleanup_block; } aoqi@0: Instruction* inline_cleanup_return_prev() const{ return _cleanup_return_prev; } aoqi@0: ValueStack* inline_cleanup_state() const { return _cleanup_state; } aoqi@0: }; aoqi@0: aoqi@0: // for all GraphBuilders aoqi@0: static bool _can_trap[Bytecodes::number_of_java_codes]; aoqi@0: aoqi@0: // for each instance of GraphBuilder aoqi@0: ScopeData* _scope_data; // Per-scope data; used for inlining aoqi@0: Compilation* _compilation; // the current compilation aoqi@0: ValueMap* _vmap; // the map of values encountered (for CSE) aoqi@0: MemoryBuffer* _memory; aoqi@0: const char* _inline_bailout_msg; // non-null if most recent inline attempt failed aoqi@0: int _instruction_count; // for bailing out in pathological jsr/ret cases aoqi@0: BlockBegin* _start; // the start block aoqi@0: BlockBegin* _osr_entry; // the osr entry block block aoqi@0: ValueStack* _initial_state; // The state for the start block aoqi@0: aoqi@0: // for each call to connect_to_end; can also be set by inliner aoqi@0: BlockBegin* _block; // the current block aoqi@0: ValueStack* _state; // the current execution state aoqi@0: Instruction* _last; // the last instruction added aoqi@0: bool _skip_block; // skip processing of the rest of this block aoqi@0: aoqi@0: // accessors aoqi@0: ScopeData* scope_data() const { return _scope_data; } aoqi@0: Compilation* compilation() const { return _compilation; } aoqi@0: BlockList* bci2block() const { return scope_data()->bci2block(); } aoqi@0: ValueMap* vmap() const { assert(UseLocalValueNumbering, "should not access otherwise"); return _vmap; } aoqi@0: bool has_handler() const { return scope_data()->has_handler(); } aoqi@0: aoqi@0: BlockBegin* block() const { return _block; } aoqi@0: ValueStack* state() const { return _state; } aoqi@0: void set_state(ValueStack* state) { _state = state; } aoqi@0: IRScope* scope() const { return scope_data()->scope(); } aoqi@0: ciMethod* method() const { return scope()->method(); } aoqi@0: ciBytecodeStream* stream() const { return scope_data()->stream(); } aoqi@0: Instruction* last() const { return _last; } aoqi@0: Bytecodes::Code code() const { return stream()->cur_bc(); } aoqi@0: int bci() const { return stream()->cur_bci(); } aoqi@0: int next_bci() const { return stream()->next_bci(); } aoqi@0: aoqi@0: // unified bailout support aoqi@0: void bailout(const char* msg) const { compilation()->bailout(msg); } aoqi@0: bool bailed_out() const { return compilation()->bailed_out(); } aoqi@0: aoqi@0: // stack manipulation helpers aoqi@0: void ipush(Value t) const { state()->ipush(t); } aoqi@0: void lpush(Value t) const { state()->lpush(t); } aoqi@0: void fpush(Value t) const { state()->fpush(t); } aoqi@0: void dpush(Value t) const { state()->dpush(t); } aoqi@0: void apush(Value t) const { state()->apush(t); } aoqi@0: void push(ValueType* type, Value t) const { state()-> push(type, t); } aoqi@0: aoqi@0: Value ipop() { return state()->ipop(); } aoqi@0: Value lpop() { return state()->lpop(); } aoqi@0: Value fpop() { return state()->fpop(); } aoqi@0: Value dpop() { return state()->dpop(); } aoqi@0: Value apop() { return state()->apop(); } aoqi@0: Value pop(ValueType* type) { return state()-> pop(type); } aoqi@0: aoqi@0: // instruction helpers aoqi@0: void load_constant(); aoqi@0: void load_local(ValueType* type, int index); aoqi@0: void store_local(ValueType* type, int index); aoqi@0: void store_local(ValueStack* state, Value value, int index); aoqi@0: void load_indexed (BasicType type); aoqi@0: void store_indexed(BasicType type); aoqi@0: void stack_op(Bytecodes::Code code); aoqi@0: void arithmetic_op(ValueType* type, Bytecodes::Code code, ValueStack* state_before = NULL); aoqi@0: void negate_op(ValueType* type); aoqi@0: void shift_op(ValueType* type, Bytecodes::Code code); aoqi@0: void logic_op(ValueType* type, Bytecodes::Code code); aoqi@0: void compare_op(ValueType* type, Bytecodes::Code code); aoqi@0: void convert(Bytecodes::Code op, BasicType from, BasicType to); aoqi@0: void increment(); aoqi@0: void _goto(int from_bci, int to_bci); aoqi@0: void if_node(Value x, If::Condition cond, Value y, ValueStack* stack_before); aoqi@0: void if_zero(ValueType* type, If::Condition cond); aoqi@0: void if_null(ValueType* type, If::Condition cond); aoqi@0: void if_same(ValueType* type, If::Condition cond); aoqi@0: void jsr(int dest); aoqi@0: void ret(int local_index); aoqi@0: void table_switch(); aoqi@0: void lookup_switch(); aoqi@0: void method_return(Value x); aoqi@0: void call_register_finalizer(); aoqi@0: void access_field(Bytecodes::Code code); aoqi@0: void invoke(Bytecodes::Code code); aoqi@0: void new_instance(int klass_index); aoqi@0: void new_type_array(); aoqi@0: void new_object_array(); aoqi@0: void check_cast(int klass_index); aoqi@0: void instance_of(int klass_index); aoqi@0: void monitorenter(Value x, int bci); aoqi@0: void monitorexit(Value x, int bci); aoqi@0: void new_multi_array(int dimensions); aoqi@0: void throw_op(int bci); aoqi@0: Value round_fp(Value fp_value); aoqi@0: aoqi@0: // stack/code manipulation helpers aoqi@0: Instruction* append_with_bci(Instruction* instr, int bci); aoqi@0: Instruction* append(Instruction* instr); aoqi@0: Instruction* append_split(StateSplit* instr); aoqi@0: aoqi@0: // other helpers aoqi@0: BlockBegin* block_at(int bci) { return scope_data()->block_at(bci); } aoqi@0: XHandlers* handle_exception(Instruction* instruction); aoqi@0: void connect_to_end(BlockBegin* beg); aoqi@0: void null_check(Value value); aoqi@0: void eliminate_redundant_phis(BlockBegin* start); aoqi@0: BlockEnd* iterate_bytecodes_for_block(int bci); aoqi@0: void iterate_all_blocks(bool start_in_current_block_for_inlining = false); aoqi@0: Dependencies* dependency_recorder() const; // = compilation()->dependencies() aoqi@0: bool direct_compare(ciKlass* k); aoqi@0: aoqi@0: void kill_all(); aoqi@0: aoqi@0: // use of state copy routines (try to minimize unnecessary state aoqi@0: // object allocations): aoqi@0: aoqi@0: // - if the instruction unconditionally needs a full copy of the aoqi@0: // state (for patching for example), then use copy_state_before* aoqi@0: aoqi@0: // - if the instruction needs a full copy of the state only for aoqi@0: // handler generation (Instruction::needs_exception_state() returns aoqi@0: // false) then use copy_state_exhandling* aoqi@0: aoqi@0: // - if the instruction needs either a full copy of the state for aoqi@0: // handler generation and a least a minimal copy of the state (as aoqi@0: // returned by Instruction::exception_state()) for debug info aoqi@0: // generation (that is when Instruction::needs_exception_state() aoqi@0: // returns true) then use copy_state_for_exception* aoqi@0: aoqi@0: ValueStack* copy_state_before_with_bci(int bci); aoqi@0: ValueStack* copy_state_before(); aoqi@0: ValueStack* copy_state_exhandling_with_bci(int bci); aoqi@0: ValueStack* copy_state_exhandling(); aoqi@0: ValueStack* copy_state_for_exception_with_bci(int bci); aoqi@0: ValueStack* copy_state_for_exception(); aoqi@0: ValueStack* copy_state_if_bb(bool is_bb) { return (is_bb || compilation()->is_optimistic()) ? copy_state_before() : NULL; } aoqi@0: ValueStack* copy_state_indexed_access() { return compilation()->is_optimistic() ? copy_state_before() : copy_state_for_exception(); } aoqi@0: aoqi@0: // aoqi@0: // Inlining support aoqi@0: // aoqi@0: aoqi@0: // accessors aoqi@0: bool parsing_jsr() const { return scope_data()->parsing_jsr(); } aoqi@0: BlockBegin* continuation() const { return scope_data()->continuation(); } aoqi@0: BlockBegin* jsr_continuation() const { return scope_data()->jsr_continuation(); } aoqi@0: void set_continuation(BlockBegin* continuation) { scope_data()->set_continuation(continuation); } aoqi@0: void set_inline_cleanup_info(BlockBegin* block, aoqi@0: Instruction* return_prev, aoqi@0: ValueStack* return_state) { scope_data()->set_inline_cleanup_info(block, aoqi@0: return_prev, aoqi@0: return_state); } aoqi@0: void set_inline_cleanup_info() { aoqi@0: set_inline_cleanup_info(_block, _last, _state); aoqi@0: } aoqi@0: BlockBegin* inline_cleanup_block() const { return scope_data()->inline_cleanup_block(); } aoqi@0: Instruction* inline_cleanup_return_prev() const { return scope_data()->inline_cleanup_return_prev(); } aoqi@0: ValueStack* inline_cleanup_state() const { return scope_data()->inline_cleanup_state(); } aoqi@0: void restore_inline_cleanup_info() { aoqi@0: _block = inline_cleanup_block(); aoqi@0: _last = inline_cleanup_return_prev(); aoqi@0: _state = inline_cleanup_state(); aoqi@0: } aoqi@0: void incr_num_returns() { scope_data()->incr_num_returns(); } aoqi@0: int num_returns() const { return scope_data()->num_returns(); } aoqi@0: intx max_inline_size() const { return scope_data()->max_inline_size(); } aoqi@0: int inline_level() const { return scope()->level(); } aoqi@0: int recursive_inline_level(ciMethod* callee) const; aoqi@0: aoqi@0: // inlining of synchronized methods aoqi@0: void inline_sync_entry(Value lock, BlockBegin* sync_handler); aoqi@0: void fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler = false); aoqi@0: aoqi@0: // inliners aoqi@0: bool try_inline( ciMethod* callee, bool holder_known, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL); aoqi@0: bool try_inline_intrinsics(ciMethod* callee); aoqi@0: bool try_inline_full( ciMethod* callee, bool holder_known, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL); aoqi@0: bool try_inline_jsr(int jsr_dest_bci); aoqi@0: aoqi@0: const char* check_can_parse(ciMethod* callee) const; aoqi@0: const char* should_not_inline(ciMethod* callee) const; aoqi@0: aoqi@0: // JSR 292 support aoqi@0: bool try_method_handle_inline(ciMethod* callee); aoqi@0: aoqi@0: // helpers aoqi@0: void inline_bailout(const char* msg); aoqi@0: BlockBegin* header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state); aoqi@0: BlockBegin* setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* init_state); aoqi@0: void setup_osr_entry_block(); aoqi@0: void clear_inline_bailout(); aoqi@0: ValueStack* state_at_entry(); aoqi@0: void push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start); aoqi@0: void push_scope(ciMethod* callee, BlockBegin* continuation); aoqi@0: void push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci); aoqi@0: void pop_scope(); aoqi@0: void pop_scope_for_jsr(); aoqi@0: aoqi@0: bool append_unsafe_get_obj(ciMethod* callee, BasicType t, bool is_volatile); aoqi@0: bool append_unsafe_put_obj(ciMethod* callee, BasicType t, bool is_volatile); aoqi@0: bool append_unsafe_get_raw(ciMethod* callee, BasicType t); aoqi@0: bool append_unsafe_put_raw(ciMethod* callee, BasicType t); aoqi@0: bool append_unsafe_prefetch(ciMethod* callee, bool is_store, bool is_static); aoqi@0: void append_unsafe_CAS(ciMethod* callee); aoqi@0: bool append_unsafe_get_and_set_obj(ciMethod* callee, bool is_add); aoqi@0: aoqi@0: void print_inlining(ciMethod* callee, const char* msg = NULL, bool success = true); aoqi@0: aoqi@0: void profile_call(ciMethod* callee, Value recv, ciKlass* predicted_holder, Values* obj_args, bool inlined); aoqi@0: void profile_return_type(Value ret, ciMethod* callee, ciMethod* m = NULL, int bci = -1); aoqi@0: void profile_invocation(ciMethod* inlinee, ValueStack* state); aoqi@0: aoqi@0: // Shortcuts to profiling control. aoqi@0: bool is_profiling() { return _compilation->is_profiling(); } aoqi@0: bool count_invocations() { return _compilation->count_invocations(); } aoqi@0: bool count_backedges() { return _compilation->count_backedges(); } aoqi@0: bool profile_branches() { return _compilation->profile_branches(); } aoqi@0: bool profile_calls() { return _compilation->profile_calls(); } aoqi@0: bool profile_inlined_calls() { return _compilation->profile_inlined_calls(); } aoqi@0: bool profile_checkcasts() { return _compilation->profile_checkcasts(); } aoqi@0: bool profile_parameters() { return _compilation->profile_parameters(); } aoqi@0: bool profile_arguments() { return _compilation->profile_arguments(); } aoqi@0: bool profile_return() { return _compilation->profile_return(); } aoqi@0: aoqi@0: Values* args_list_for_profiling(ciMethod* target, int& start, bool may_have_receiver); aoqi@0: Values* collect_args_for_profiling(Values* args, ciMethod* target, bool may_have_receiver); aoqi@0: void check_args_for_profiling(Values* obj_args, int expected); aoqi@0: aoqi@0: public: aoqi@0: NOT_PRODUCT(void print_stats();) aoqi@0: aoqi@0: // initialization aoqi@0: static void initialize(); aoqi@0: aoqi@0: // public aoqi@0: static bool can_trap(ciMethod* method, Bytecodes::Code code) { aoqi@0: assert(0 <= code && code < Bytecodes::number_of_java_codes, "illegal bytecode"); aoqi@0: if (_can_trap[code]) return true; aoqi@0: // special handling for finalizer registration aoqi@0: return code == Bytecodes::_return && method->intrinsic_id() == vmIntrinsics::_Object_init; aoqi@0: } aoqi@0: aoqi@0: // creation aoqi@0: GraphBuilder(Compilation* compilation, IRScope* scope); aoqi@0: static void sort_top_into_worklist(BlockList* worklist, BlockBegin* top); aoqi@0: aoqi@0: BlockBegin* start() const { return _start; } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_C1_C1_GRAPHBUILDER_HPP