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_IR_HPP aoqi@0: #define SHARE_VM_C1_C1_IR_HPP aoqi@0: aoqi@0: #include "c1/c1_Instruction.hpp" aoqi@0: #include "ci/ciExceptionHandler.hpp" aoqi@0: #include "ci/ciMethod.hpp" aoqi@0: #include "ci/ciStreams.hpp" aoqi@0: #include "memory/allocation.hpp" aoqi@0: aoqi@0: // An XHandler is a C1 internal description for an exception handler aoqi@0: aoqi@0: class XHandler: public CompilationResourceObj { aoqi@0: private: aoqi@0: ciExceptionHandler* _desc; aoqi@0: aoqi@0: BlockBegin* _entry_block; // Entry block of xhandler aoqi@0: LIR_List* _entry_code; // LIR-operations that must be executed before jumping to entry_block aoqi@0: int _entry_pco; // pco where entry_code (or entry_block if no entry_code) starts aoqi@0: int _phi_operand; // For resolving of phi functions at begin of entry_block aoqi@0: int _scope_count; // for filling ExceptionRangeEntry::scope_count aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: int _lir_op_id; // op_id of the LIR-operation throwing to this handler aoqi@0: #endif aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: XHandler(ciExceptionHandler* desc) aoqi@0: : _desc(desc) aoqi@0: , _entry_block(NULL) aoqi@0: , _entry_code(NULL) aoqi@0: , _entry_pco(-1) aoqi@0: , _phi_operand(-1) aoqi@0: , _scope_count(-1) aoqi@0: #ifdef ASSERT aoqi@0: , _lir_op_id(-1) aoqi@0: #endif aoqi@0: { } aoqi@0: aoqi@0: XHandler(XHandler* other) aoqi@0: : _desc(other->_desc) aoqi@0: , _entry_block(other->_entry_block) aoqi@0: , _entry_code(other->_entry_code) aoqi@0: , _entry_pco(other->_entry_pco) aoqi@0: , _phi_operand(other->_phi_operand) aoqi@0: , _scope_count(other->_scope_count) aoqi@0: #ifdef ASSERT aoqi@0: , _lir_op_id(other->_lir_op_id) aoqi@0: #endif aoqi@0: { } aoqi@0: aoqi@0: // accessors for data of ciExceptionHandler aoqi@0: int beg_bci() const { return _desc->start(); } aoqi@0: int end_bci() const { return _desc->limit(); } aoqi@0: int handler_bci() const { return _desc->handler_bci(); } aoqi@0: bool is_catch_all() const { return _desc->is_catch_all(); } aoqi@0: int catch_type() const { return _desc->catch_klass_index(); } aoqi@0: ciInstanceKlass* catch_klass() const { return _desc->catch_klass(); } aoqi@0: bool covers(int bci) const { return beg_bci() <= bci && bci < end_bci(); } aoqi@0: aoqi@0: // accessors for additional fields aoqi@0: BlockBegin* entry_block() const { return _entry_block; } aoqi@0: LIR_List* entry_code() const { return _entry_code; } aoqi@0: int entry_pco() const { return _entry_pco; } aoqi@0: int phi_operand() const { assert(_phi_operand != -1, "not set"); return _phi_operand; } aoqi@0: int scope_count() const { assert(_scope_count != -1, "not set"); return _scope_count; } aoqi@0: DEBUG_ONLY(int lir_op_id() const { return _lir_op_id; }); aoqi@0: aoqi@0: void set_entry_block(BlockBegin* entry_block) { aoqi@0: assert(entry_block->is_set(BlockBegin::exception_entry_flag), "must be an exception handler entry"); aoqi@0: assert(entry_block->bci() == handler_bci(), "bci's must correspond"); aoqi@0: _entry_block = entry_block; aoqi@0: } aoqi@0: void set_entry_code(LIR_List* entry_code) { _entry_code = entry_code; } aoqi@0: void set_entry_pco(int entry_pco) { _entry_pco = entry_pco; } aoqi@0: void set_phi_operand(int phi_operand) { _phi_operand = phi_operand; } aoqi@0: void set_scope_count(int scope_count) { _scope_count = scope_count; } aoqi@0: DEBUG_ONLY(void set_lir_op_id(int lir_op_id) { _lir_op_id = lir_op_id; }); aoqi@0: aoqi@0: bool equals(XHandler* other) const; aoqi@0: }; aoqi@0: aoqi@0: define_array(_XHandlerArray, XHandler*) aoqi@0: define_stack(_XHandlerList, _XHandlerArray) aoqi@0: aoqi@0: aoqi@0: // XHandlers is the C1 internal list of exception handlers for a method aoqi@0: class XHandlers: public CompilationResourceObj { aoqi@0: private: aoqi@0: _XHandlerList _list; aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: XHandlers() : _list() { } aoqi@0: XHandlers(ciMethod* method); aoqi@0: XHandlers(XHandlers* other); aoqi@0: aoqi@0: // accessors aoqi@0: int length() const { return _list.length(); } aoqi@0: XHandler* handler_at(int i) const { return _list.at(i); } aoqi@0: bool has_handlers() const { return _list.length() > 0; } aoqi@0: void append(XHandler* h) { _list.append(h); } aoqi@0: XHandler* remove_last() { return _list.pop(); } aoqi@0: aoqi@0: bool could_catch(ciInstanceKlass* klass, bool type_is_exact) const; aoqi@0: bool equals(XHandlers* others) const; aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class IRScope; aoqi@0: define_array(IRScopeArray, IRScope*) aoqi@0: define_stack(IRScopeList, IRScopeArray) aoqi@0: aoqi@0: class Compilation; aoqi@0: class IRScope: public CompilationResourceObj { aoqi@0: private: aoqi@0: // hierarchy aoqi@0: Compilation* _compilation; // the current compilation aoqi@0: IRScope* _caller; // the caller scope, or NULL aoqi@0: int _level; // the inlining level aoqi@0: ciMethod* _method; // the corresponding method aoqi@0: IRScopeList _callees; // the inlined method scopes aoqi@0: aoqi@0: // graph aoqi@0: XHandlers* _xhandlers; // the exception handlers aoqi@0: int _number_of_locks; // the number of monitor lock slots needed aoqi@0: bool _monitor_pairing_ok; // the monitor pairing info aoqi@0: bool _wrote_final; // has written final field aoqi@0: BlockBegin* _start; // the start block, successsors are method entries aoqi@0: aoqi@0: BitMap _requires_phi_function; // bit is set if phi functions at loop headers are necessary for a local variable aoqi@0: aoqi@0: // helper functions aoqi@0: BlockBegin* build_graph(Compilation* compilation, int osr_bci); aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph = false); aoqi@0: aoqi@0: // accessors aoqi@0: Compilation* compilation() const { return _compilation; } aoqi@0: IRScope* caller() const { return _caller; } aoqi@0: int level() const { return _level; } aoqi@0: ciMethod* method() const { return _method; } aoqi@0: int max_stack() const; // NOTE: expensive aoqi@0: BitMap& requires_phi_function() { return _requires_phi_function; } aoqi@0: aoqi@0: // hierarchy aoqi@0: bool is_top_scope() const { return _caller == NULL; } aoqi@0: void add_callee(IRScope* callee) { _callees.append(callee); } aoqi@0: int number_of_callees() const { return _callees.length(); } aoqi@0: IRScope* callee_no(int i) const { return _callees.at(i); } aoqi@0: aoqi@0: // accessors, graph aoqi@0: bool is_valid() const { return start() != NULL; } aoqi@0: XHandlers* xhandlers() const { return _xhandlers; } aoqi@0: int number_of_locks() const { return _number_of_locks; } aoqi@0: void set_min_number_of_locks(int n) { if (n > _number_of_locks) _number_of_locks = n; } aoqi@0: bool monitor_pairing_ok() const { return _monitor_pairing_ok; } aoqi@0: BlockBegin* start() const { return _start; } aoqi@0: void set_wrote_final() { _wrote_final = true; } aoqi@0: bool wrote_final () const { return _wrote_final; } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // aoqi@0: // IRScopeDebugInfo records the debug information for a particular IRScope aoqi@0: // in a particular CodeEmitInfo. This allows the information to be computed aoqi@0: // once early enough for the OopMap to be available to the LIR and also to be aoqi@0: // reemited for different pcs using the same CodeEmitInfo without recomputing aoqi@0: // everything. aoqi@0: // aoqi@0: aoqi@0: class IRScopeDebugInfo: public CompilationResourceObj { aoqi@0: private: aoqi@0: IRScope* _scope; aoqi@0: int _bci; aoqi@0: GrowableArray* _locals; aoqi@0: GrowableArray* _expressions; aoqi@0: GrowableArray* _monitors; aoqi@0: IRScopeDebugInfo* _caller; aoqi@0: aoqi@0: public: aoqi@0: IRScopeDebugInfo(IRScope* scope, aoqi@0: int bci, aoqi@0: GrowableArray* locals, aoqi@0: GrowableArray* expressions, aoqi@0: GrowableArray* monitors, aoqi@0: IRScopeDebugInfo* caller): aoqi@0: _scope(scope) aoqi@0: , _locals(locals) aoqi@0: , _bci(bci) aoqi@0: , _expressions(expressions) aoqi@0: , _monitors(monitors) aoqi@0: , _caller(caller) {} aoqi@0: aoqi@0: aoqi@0: IRScope* scope() { return _scope; } aoqi@0: int bci() { return _bci; } aoqi@0: GrowableArray* locals() { return _locals; } aoqi@0: GrowableArray* expressions() { return _expressions; } aoqi@0: GrowableArray* monitors() { return _monitors; } aoqi@0: IRScopeDebugInfo* caller() { return _caller; } aoqi@0: aoqi@0: //Whether we should reexecute this bytecode for deopt aoqi@0: bool should_reexecute(); aoqi@0: aoqi@0: void record_debug_info(DebugInformationRecorder* recorder, int pc_offset, bool topmost, bool is_method_handle_invoke = false) { aoqi@0: if (caller() != NULL) { aoqi@0: // Order is significant: Must record caller first. aoqi@0: caller()->record_debug_info(recorder, pc_offset, false/*topmost*/); aoqi@0: } aoqi@0: DebugToken* locvals = recorder->create_scope_values(locals()); aoqi@0: DebugToken* expvals = recorder->create_scope_values(expressions()); aoqi@0: DebugToken* monvals = recorder->create_monitor_values(monitors()); aoqi@0: // reexecute allowed only for the topmost frame aoqi@0: bool reexecute = topmost ? should_reexecute() : false; aoqi@0: bool return_oop = false; // This flag will be ignored since it used only for C2 with escape analysis. aoqi@0: recorder->describe_scope(pc_offset, scope()->method(), bci(), reexecute, is_method_handle_invoke, return_oop, locvals, expvals, monvals); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class CodeEmitInfo: public CompilationResourceObj { aoqi@0: friend class LinearScan; aoqi@0: private: aoqi@0: IRScopeDebugInfo* _scope_debug_info; aoqi@0: IRScope* _scope; aoqi@0: XHandlers* _exception_handlers; aoqi@0: OopMap* _oop_map; aoqi@0: ValueStack* _stack; // used by deoptimization (contains also monitors aoqi@0: bool _is_method_handle_invoke; // true if the associated call site is a MethodHandle call site. aoqi@0: bool _deoptimize_on_exception; aoqi@0: aoqi@0: FrameMap* frame_map() const { return scope()->compilation()->frame_map(); } aoqi@0: Compilation* compilation() const { return scope()->compilation(); } aoqi@0: aoqi@0: public: aoqi@0: aoqi@0: // use scope from ValueStack aoqi@0: CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers, bool deoptimize_on_exception = false); aoqi@0: aoqi@0: // make a copy aoqi@0: CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack = NULL); aoqi@0: aoqi@0: // accessors aoqi@0: OopMap* oop_map() { return _oop_map; } aoqi@0: ciMethod* method() const { return _scope->method(); } aoqi@0: IRScope* scope() const { return _scope; } aoqi@0: XHandlers* exception_handlers() const { return _exception_handlers; } aoqi@0: ValueStack* stack() const { return _stack; } aoqi@0: bool deoptimize_on_exception() const { return _deoptimize_on_exception; } aoqi@0: aoqi@0: void add_register_oop(LIR_Opr opr); aoqi@0: void record_debug_info(DebugInformationRecorder* recorder, int pc_offset); aoqi@0: aoqi@0: bool is_method_handle_invoke() const { return _is_method_handle_invoke; } aoqi@0: void set_is_method_handle_invoke(bool x) { _is_method_handle_invoke = x; } aoqi@0: aoqi@0: int interpreter_frame_size() const; aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: class IR: public CompilationResourceObj { aoqi@0: private: aoqi@0: Compilation* _compilation; // the current compilation aoqi@0: IRScope* _top_scope; // the root of the scope hierarchy aoqi@0: WordSize _locals_size; // the space required for all locals aoqi@0: int _num_loops; // Total number of loops aoqi@0: BlockList* _code; // the blocks in code generation order w/ use counts aoqi@0: aoqi@0: public: aoqi@0: // creation aoqi@0: IR(Compilation* compilation, ciMethod* method, int osr_bci); aoqi@0: aoqi@0: // accessors aoqi@0: bool is_valid() const { return top_scope()->is_valid(); } aoqi@0: Compilation* compilation() const { return _compilation; } aoqi@0: IRScope* top_scope() const { return _top_scope; } aoqi@0: int number_of_locks() const { return top_scope()->number_of_locks(); } aoqi@0: ciMethod* method() const { return top_scope()->method(); } aoqi@0: BlockBegin* start() const { return top_scope()->start(); } aoqi@0: BlockBegin* std_entry() const { return start()->end()->as_Base()->std_entry(); } aoqi@0: BlockBegin* osr_entry() const { return start()->end()->as_Base()->osr_entry(); } aoqi@0: WordSize locals_size() const { return _locals_size; } aoqi@0: int locals_size_in_words() const { return in_words(_locals_size); } aoqi@0: BlockList* code() const { return _code; } aoqi@0: int num_loops() const { return _num_loops; } aoqi@0: int max_stack() const { return top_scope()->max_stack(); } // expensive aoqi@0: aoqi@0: // ir manipulation aoqi@0: void optimize_blocks(); aoqi@0: void eliminate_null_checks(); aoqi@0: void compute_predecessors(); aoqi@0: void split_critical_edges(); aoqi@0: void compute_code(); aoqi@0: void compute_use_counts(); aoqi@0: aoqi@0: // The linear-scan order and the code emission order are equal, but aoqi@0: // this may change in future aoqi@0: BlockList* linear_scan_order() { assert(_code != NULL, "not computed"); return _code; } aoqi@0: aoqi@0: // iteration aoqi@0: void iterate_preorder (BlockClosure* closure); aoqi@0: void iterate_postorder (BlockClosure* closure); aoqi@0: void iterate_linear_scan_order(BlockClosure* closure); aoqi@0: aoqi@0: // debugging aoqi@0: static void print(BlockBegin* start, bool cfg_only, bool live_only = false) PRODUCT_RETURN; aoqi@0: void print(bool cfg_only, bool live_only = false) PRODUCT_RETURN; aoqi@0: void verify() PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: // Globally do instruction substitution and remove substituted aoqi@0: // instructions from the instruction list. aoqi@0: // aoqi@0: aoqi@0: class SubstitutionResolver: public BlockClosure, ValueVisitor { aoqi@0: virtual void visit(Value* v); aoqi@0: aoqi@0: public: aoqi@0: SubstitutionResolver(IR* hir) { aoqi@0: hir->iterate_preorder(this); aoqi@0: } aoqi@0: aoqi@0: SubstitutionResolver(BlockBegin* block) { aoqi@0: block->iterate_preorder(this); aoqi@0: } aoqi@0: aoqi@0: virtual void block_do(BlockBegin* block); aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_C1_C1_IR_HPP