Path: blob/master/src/hotspot/share/c1/c1_GraphBuilder.hpp
40931 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_C1_C1_GRAPHBUILDER_HPP25#define SHARE_C1_C1_GRAPHBUILDER_HPP2627#include "c1/c1_IR.hpp"28#include "c1/c1_Instruction.hpp"29#include "c1/c1_ValueMap.hpp"30#include "c1/c1_ValueStack.hpp"31#include "ci/ciMethodData.hpp"32#include "ci/ciStreams.hpp"33#include "compiler/compileLog.hpp"3435class MemoryBuffer;3637class GraphBuilder {38private:39// Per-scope data. These are pushed and popped as we descend into40// inlined methods. Currently in order to generate good code in the41// inliner we have to attempt to inline methods directly into the42// basic block we are parsing; this adds complexity.43class ScopeData: public CompilationResourceObj {44private:45ScopeData* _parent;46// bci-to-block mapping47BlockList* _bci2block;48// Scope49IRScope* _scope;50// Whether this scope or any parent scope has exception handlers51bool _has_handler;52// The bytecodes53ciBytecodeStream* _stream;5455// Work list56BlockList* _work_list;5758// Maximum inline size for this scope59intx _max_inline_size;60// Expression stack depth at point where inline occurred61int _caller_stack_size;6263// The continuation point for the inline. Currently only used in64// multi-block inlines, but eventually would like to use this for65// all inlines for uniformity and simplicity; in this case would66// get the continuation point from the BlockList instead of67// fabricating it anew because Invokes would be considered to be68// BlockEnds.69BlockBegin* _continuation;7071// Was this ScopeData created only for the parsing and inlining of72// a jsr?73bool _parsing_jsr;74// We track the destination bci of the jsr only to determine75// bailout conditions, since we only handle a subset of all of the76// possible jsr-ret control structures. Recursive invocations of a77// jsr are disallowed by the verifier.78int _jsr_entry_bci;79// We need to track the local variable in which the return address80// was stored to ensure we can handle inlining the jsr, because we81// don't handle arbitrary jsr/ret constructs.82int _jsr_ret_addr_local;83// If we are parsing a jsr, the continuation point for rets84BlockBegin* _jsr_continuation;85// Cloned XHandlers for jsr-related ScopeDatas86XHandlers* _jsr_xhandlers;8788// Number of returns seen in this scope89int _num_returns;9091// In order to generate profitable code for inlining, we currently92// have to perform an optimization for single-block inlined93// methods where we continue parsing into the same block. This94// allows us to perform CSE across inlined scopes and to avoid95// storing parameters to the stack. Having a global register96// allocator and being able to perform global CSE would allow this97// code to be removed and thereby simplify the inliner.98BlockBegin* _cleanup_block; // The block to which the return was added99Instruction* _cleanup_return_prev; // Instruction before return instruction100ValueStack* _cleanup_state; // State of that block (not yet pinned)101102// When inlining do not push the result on the stack103bool _ignore_return;104105public:106ScopeData(ScopeData* parent);107108ScopeData* parent() const { return _parent; }109110BlockList* bci2block() const { return _bci2block; }111void set_bci2block(BlockList* bci2block) { _bci2block = bci2block; }112113// NOTE: this has a different effect when parsing jsrs114BlockBegin* block_at(int bci);115116IRScope* scope() const { return _scope; }117// Has side-effect of setting has_handler flag118void set_scope(IRScope* scope);119120// Whether this or any parent scope has exception handlers121bool has_handler() const { return _has_handler; }122void set_has_handler() { _has_handler = true; }123124// Exception handlers list to be used for this scope125XHandlers* xhandlers() const;126127// How to get a block to be parsed128void add_to_work_list(BlockBegin* block);129// How to remove the next block to be parsed; returns NULL if none left130BlockBegin* remove_from_work_list();131// Indicates parse is over132bool is_work_list_empty() const;133134ciBytecodeStream* stream() { return _stream; }135void set_stream(ciBytecodeStream* stream) { _stream = stream; }136137intx max_inline_size() const { return _max_inline_size; }138139BlockBegin* continuation() const { return _continuation; }140void set_continuation(BlockBegin* cont) { _continuation = cont; }141142// Indicates whether this ScopeData was pushed only for the143// parsing and inlining of a jsr144bool parsing_jsr() const { return _parsing_jsr; }145void set_parsing_jsr() { _parsing_jsr = true; }146int jsr_entry_bci() const { return _jsr_entry_bci; }147void set_jsr_entry_bci(int bci) { _jsr_entry_bci = bci; }148void set_jsr_return_address_local(int local_no){ _jsr_ret_addr_local = local_no; }149int jsr_return_address_local() const { return _jsr_ret_addr_local; }150// Must be called after scope is set up for jsr ScopeData151void setup_jsr_xhandlers();152153// The jsr continuation is only used when parsing_jsr is true, and154// is different from the "normal" continuation since we can end up155// doing a return (rather than a ret) from within a subroutine156BlockBegin* jsr_continuation() const { return _jsr_continuation; }157void set_jsr_continuation(BlockBegin* cont) { _jsr_continuation = cont; }158159int num_returns();160void incr_num_returns();161162void set_inline_cleanup_info(BlockBegin* block,163Instruction* return_prev,164ValueStack* return_state);165BlockBegin* inline_cleanup_block() const { return _cleanup_block; }166Instruction* inline_cleanup_return_prev() const{ return _cleanup_return_prev; }167ValueStack* inline_cleanup_state() const { return _cleanup_state; }168169bool ignore_return() const { return _ignore_return; }170void set_ignore_return(bool ignore_return) { _ignore_return = ignore_return; }171};172173// for all GraphBuilders174static bool _can_trap[Bytecodes::number_of_java_codes];175176// for each instance of GraphBuilder177ScopeData* _scope_data; // Per-scope data; used for inlining178Compilation* _compilation; // the current compilation179ValueMap* _vmap; // the map of values encountered (for CSE)180MemoryBuffer* _memory;181const char* _inline_bailout_msg; // non-null if most recent inline attempt failed182int _instruction_count; // for bailing out in pathological jsr/ret cases183BlockBegin* _start; // the start block184BlockBegin* _osr_entry; // the osr entry block block185ValueStack* _initial_state; // The state for the start block186187// for each call to connect_to_end; can also be set by inliner188BlockBegin* _block; // the current block189ValueStack* _state; // the current execution state190Instruction* _last; // the last instruction added191bool _skip_block; // skip processing of the rest of this block192193// accessors194ScopeData* scope_data() const { return _scope_data; }195Compilation* compilation() const { return _compilation; }196BlockList* bci2block() const { return scope_data()->bci2block(); }197ValueMap* vmap() const { assert(UseLocalValueNumbering, "should not access otherwise"); return _vmap; }198bool has_handler() const { return scope_data()->has_handler(); }199200BlockBegin* block() const { return _block; }201ValueStack* state() const { return _state; }202void set_state(ValueStack* state) { _state = state; }203IRScope* scope() const { return scope_data()->scope(); }204ciMethod* method() const { return scope()->method(); }205ciBytecodeStream* stream() const { return scope_data()->stream(); }206Instruction* last() const { return _last; }207Bytecodes::Code code() const { return stream()->cur_bc(); }208int bci() const { return stream()->cur_bci(); }209int next_bci() const { return stream()->next_bci(); }210211// unified bailout support212void bailout(const char* msg) const { compilation()->bailout(msg); }213bool bailed_out() const { return compilation()->bailed_out(); }214215// stack manipulation helpers216void ipush(Value t) const { state()->ipush(t); }217void lpush(Value t) const { state()->lpush(t); }218void fpush(Value t) const { state()->fpush(t); }219void dpush(Value t) const { state()->dpush(t); }220void apush(Value t) const { state()->apush(t); }221void push(ValueType* type, Value t) const { state()-> push(type, t); }222223Value ipop() { return state()->ipop(); }224Value lpop() { return state()->lpop(); }225Value fpop() { return state()->fpop(); }226Value dpop() { return state()->dpop(); }227Value apop() { return state()->apop(); }228Value pop(ValueType* type) { return state()-> pop(type); }229230// instruction helpers231void load_constant();232void load_local(ValueType* type, int index);233void store_local(ValueType* type, int index);234void store_local(ValueStack* state, Value value, int index);235void load_indexed (BasicType type);236void store_indexed(BasicType type);237void stack_op(Bytecodes::Code code);238void arithmetic_op(ValueType* type, Bytecodes::Code code, ValueStack* state_before = NULL);239void negate_op(ValueType* type);240void shift_op(ValueType* type, Bytecodes::Code code);241void logic_op(ValueType* type, Bytecodes::Code code);242void compare_op(ValueType* type, Bytecodes::Code code);243void convert(Bytecodes::Code op, BasicType from, BasicType to);244void increment();245void _goto(int from_bci, int to_bci);246void if_node(Value x, If::Condition cond, Value y, ValueStack* stack_before);247void if_zero(ValueType* type, If::Condition cond);248void if_null(ValueType* type, If::Condition cond);249void if_same(ValueType* type, If::Condition cond);250void jsr(int dest);251void ret(int local_index);252void table_switch();253void lookup_switch();254void method_return(Value x, bool ignore_return = false);255void call_register_finalizer();256void access_field(Bytecodes::Code code);257void invoke(Bytecodes::Code code);258void new_instance(int klass_index);259void new_type_array();260void new_object_array();261void check_cast(int klass_index);262void instance_of(int klass_index);263void monitorenter(Value x, int bci);264void monitorexit(Value x, int bci);265void new_multi_array(int dimensions);266void throw_op(int bci);267Value round_fp(Value fp_value);268269// stack/code manipulation helpers270Instruction* append_with_bci(Instruction* instr, int bci);271Instruction* append(Instruction* instr);272Instruction* append_split(StateSplit* instr);273274// other helpers275BlockBegin* block_at(int bci) { return scope_data()->block_at(bci); }276XHandlers* handle_exception(Instruction* instruction);277void connect_to_end(BlockBegin* beg);278void null_check(Value value);279void eliminate_redundant_phis(BlockBegin* start);280BlockEnd* iterate_bytecodes_for_block(int bci);281void iterate_all_blocks(bool start_in_current_block_for_inlining = false);282Dependencies* dependency_recorder() const; // = compilation()->dependencies()283bool direct_compare(ciKlass* k);284Value make_constant(ciConstant value, ciField* field);285286void kill_all();287288// use of state copy routines (try to minimize unnecessary state289// object allocations):290291// - if the instruction unconditionally needs a full copy of the292// state (for patching for example), then use copy_state_before*293294// - if the instruction needs a full copy of the state only for295// handler generation (Instruction::needs_exception_state() returns296// false) then use copy_state_exhandling*297298// - if the instruction needs either a full copy of the state for299// handler generation and a least a minimal copy of the state (as300// returned by Instruction::exception_state()) for debug info301// generation (that is when Instruction::needs_exception_state()302// returns true) then use copy_state_for_exception*303304ValueStack* copy_state_before_with_bci(int bci);305ValueStack* copy_state_before();306ValueStack* copy_state_exhandling_with_bci(int bci);307ValueStack* copy_state_exhandling();308ValueStack* copy_state_for_exception_with_bci(int bci);309ValueStack* copy_state_for_exception();310ValueStack* copy_state_if_bb(bool is_bb) { return (is_bb || compilation()->is_optimistic()) ? copy_state_before() : NULL; }311ValueStack* copy_state_indexed_access() { return compilation()->is_optimistic() ? copy_state_before() : copy_state_for_exception(); }312313//314// Inlining support315//316317// accessors318bool parsing_jsr() const { return scope_data()->parsing_jsr(); }319BlockBegin* continuation() const { return scope_data()->continuation(); }320BlockBegin* jsr_continuation() const { return scope_data()->jsr_continuation(); }321void set_continuation(BlockBegin* continuation) { scope_data()->set_continuation(continuation); }322void set_inline_cleanup_info(BlockBegin* block,323Instruction* return_prev,324ValueStack* return_state) { scope_data()->set_inline_cleanup_info(block,325return_prev,326return_state); }327void set_inline_cleanup_info() {328set_inline_cleanup_info(_block, _last, _state);329}330BlockBegin* inline_cleanup_block() const { return scope_data()->inline_cleanup_block(); }331Instruction* inline_cleanup_return_prev() const { return scope_data()->inline_cleanup_return_prev(); }332ValueStack* inline_cleanup_state() const { return scope_data()->inline_cleanup_state(); }333void restore_inline_cleanup_info() {334_block = inline_cleanup_block();335_last = inline_cleanup_return_prev();336_state = inline_cleanup_state();337}338void incr_num_returns() { scope_data()->incr_num_returns(); }339int num_returns() const { return scope_data()->num_returns(); }340intx max_inline_size() const { return scope_data()->max_inline_size(); }341int inline_level() const { return scope()->level(); }342int recursive_inline_level(ciMethod* callee) const;343344// inlining of synchronized methods345void inline_sync_entry(Value lock, BlockBegin* sync_handler);346void fill_sync_handler(Value lock, BlockBegin* sync_handler, bool default_handler = false);347348void build_graph_for_intrinsic(ciMethod* callee, bool ignore_return);349350// inliners351bool try_inline( ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL);352bool try_inline_intrinsics(ciMethod* callee, bool ignore_return = false);353bool try_inline_full( ciMethod* callee, bool holder_known, bool ignore_return, Bytecodes::Code bc = Bytecodes::_illegal, Value receiver = NULL);354bool try_inline_jsr(int jsr_dest_bci);355356const char* check_can_parse(ciMethod* callee) const;357const char* should_not_inline(ciMethod* callee) const;358359// JSR 292 support360bool try_method_handle_inline(ciMethod* callee, bool ignore_return);361362// helpers363void inline_bailout(const char* msg);364BlockBegin* header_block(BlockBegin* entry, BlockBegin::Flag f, ValueStack* state);365BlockBegin* setup_start_block(int osr_bci, BlockBegin* std_entry, BlockBegin* osr_entry, ValueStack* init_state);366void setup_osr_entry_block();367void clear_inline_bailout();368ValueStack* state_at_entry();369void push_root_scope(IRScope* scope, BlockList* bci2block, BlockBegin* start);370void push_scope(ciMethod* callee, BlockBegin* continuation);371void push_scope_for_jsr(BlockBegin* jsr_continuation, int jsr_dest_bci);372void pop_scope();373void pop_scope_for_jsr();374375void append_unsafe_get_obj(ciMethod* callee, BasicType t, bool is_volatile);376void append_unsafe_put_obj(ciMethod* callee, BasicType t, bool is_volatile);377void append_unsafe_get_raw(ciMethod* callee, BasicType t);378void append_unsafe_put_raw(ciMethod* callee, BasicType t);379void append_unsafe_CAS(ciMethod* callee);380void append_unsafe_get_and_set_obj(ciMethod* callee, bool is_add);381void append_char_access(ciMethod* callee, bool is_store);382383void print_inlining(ciMethod* callee, const char* msg, bool success = true);384385void profile_call(ciMethod* callee, Value recv, ciKlass* predicted_holder, Values* obj_args, bool inlined);386void profile_return_type(Value ret, ciMethod* callee, ciMethod* m = NULL, int bci = -1);387void profile_invocation(ciMethod* inlinee, ValueStack* state);388389// Shortcuts to profiling control.390bool is_profiling() { return _compilation->is_profiling(); }391bool count_invocations() { return _compilation->count_invocations(); }392bool count_backedges() { return _compilation->count_backedges(); }393bool profile_branches() { return _compilation->profile_branches(); }394bool profile_calls() { return _compilation->profile_calls(); }395bool profile_inlined_calls() { return _compilation->profile_inlined_calls(); }396bool profile_checkcasts() { return _compilation->profile_checkcasts(); }397bool profile_parameters() { return _compilation->profile_parameters(); }398bool profile_arguments() { return _compilation->profile_arguments(); }399bool profile_return() { return _compilation->profile_return(); }400401Values* args_list_for_profiling(ciMethod* target, int& start, bool may_have_receiver);402Values* collect_args_for_profiling(Values* args, ciMethod* target, bool may_have_receiver);403void check_args_for_profiling(Values* obj_args, int expected);404405public:406NOT_PRODUCT(void print_stats();)407408// initialization409static void initialize();410411// public412static bool can_trap(ciMethod* method, Bytecodes::Code code) {413assert(0 <= code && code < Bytecodes::number_of_java_codes, "illegal bytecode");414if (_can_trap[code]) return true;415// special handling for finalizer registration416return code == Bytecodes::_return && method->intrinsic_id() == vmIntrinsics::_Object_init;417}418419// creation420GraphBuilder(Compilation* compilation, IRScope* scope);421static void sort_top_into_worklist(BlockList* worklist, BlockBegin* top);422423BlockBegin* start() const { return _start; }424};425426#endif // SHARE_C1_C1_GRAPHBUILDER_HPP427428429