duke@435: /* trims@1907: * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: // An XHandler is a C1 internal description for an exception handler duke@435: duke@435: class XHandler: public CompilationResourceObj { duke@435: private: duke@435: ciExceptionHandler* _desc; duke@435: duke@435: BlockBegin* _entry_block; // Entry block of xhandler duke@435: LIR_List* _entry_code; // LIR-operations that must be executed before jumping to entry_block duke@435: int _entry_pco; // pco where entry_code (or entry_block if no entry_code) starts duke@435: int _phi_operand; // For resolving of phi functions at begin of entry_block duke@435: int _scope_count; // for filling ExceptionRangeEntry::scope_count duke@435: duke@435: #ifdef ASSERT duke@435: int _lir_op_id; // op_id of the LIR-operation throwing to this handler duke@435: #endif duke@435: duke@435: public: duke@435: // creation duke@435: XHandler(ciExceptionHandler* desc) duke@435: : _desc(desc) duke@435: , _entry_block(NULL) duke@435: , _entry_code(NULL) duke@435: , _entry_pco(-1) duke@435: , _phi_operand(-1) duke@435: , _scope_count(-1) duke@435: #ifdef ASSERT duke@435: , _lir_op_id(-1) duke@435: #endif duke@435: { } duke@435: duke@435: XHandler(XHandler* other) duke@435: : _desc(other->_desc) duke@435: , _entry_block(other->_entry_block) duke@435: , _entry_code(other->_entry_code) duke@435: , _entry_pco(other->_entry_pco) duke@435: , _phi_operand(other->_phi_operand) duke@435: , _scope_count(other->_scope_count) duke@435: #ifdef ASSERT duke@435: , _lir_op_id(other->_lir_op_id) duke@435: #endif duke@435: { } duke@435: duke@435: // accessors for data of ciExceptionHandler duke@435: int beg_bci() const { return _desc->start(); } duke@435: int end_bci() const { return _desc->limit(); } duke@435: int handler_bci() const { return _desc->handler_bci(); } duke@435: bool is_catch_all() const { return _desc->is_catch_all(); } duke@435: int catch_type() const { return _desc->catch_klass_index(); } duke@435: ciInstanceKlass* catch_klass() const { return _desc->catch_klass(); } duke@435: bool covers(int bci) const { return beg_bci() <= bci && bci < end_bci(); } duke@435: duke@435: // accessors for additional fields duke@435: BlockBegin* entry_block() const { return _entry_block; } duke@435: LIR_List* entry_code() const { return _entry_code; } duke@435: int entry_pco() const { return _entry_pco; } duke@435: int phi_operand() const { assert(_phi_operand != -1, "not set"); return _phi_operand; } duke@435: int scope_count() const { assert(_scope_count != -1, "not set"); return _scope_count; } duke@435: DEBUG_ONLY(int lir_op_id() const { return _lir_op_id; }); duke@435: duke@435: void set_entry_block(BlockBegin* entry_block) { duke@435: assert(entry_block->is_set(BlockBegin::exception_entry_flag), "must be an exception handler entry"); duke@435: assert(entry_block->bci() == handler_bci(), "bci's must correspond"); duke@435: _entry_block = entry_block; duke@435: } duke@435: void set_entry_code(LIR_List* entry_code) { _entry_code = entry_code; } duke@435: void set_entry_pco(int entry_pco) { _entry_pco = entry_pco; } duke@435: void set_phi_operand(int phi_operand) { _phi_operand = phi_operand; } duke@435: void set_scope_count(int scope_count) { _scope_count = scope_count; } duke@435: DEBUG_ONLY(void set_lir_op_id(int lir_op_id) { _lir_op_id = lir_op_id; }); duke@435: duke@435: bool equals(XHandler* other) const; duke@435: }; duke@435: duke@435: define_array(_XHandlerArray, XHandler*) duke@435: define_stack(_XHandlerList, _XHandlerArray) duke@435: duke@435: duke@435: // XHandlers is the C1 internal list of exception handlers for a method duke@435: class XHandlers: public CompilationResourceObj { duke@435: private: duke@435: _XHandlerList _list; duke@435: duke@435: public: duke@435: // creation duke@435: XHandlers() : _list() { } duke@435: XHandlers(ciMethod* method); duke@435: XHandlers(XHandlers* other); duke@435: duke@435: // accessors duke@435: int length() const { return _list.length(); } duke@435: XHandler* handler_at(int i) const { return _list.at(i); } duke@435: bool has_handlers() const { return _list.length() > 0; } duke@435: void append(XHandler* h) { _list.append(h); } duke@435: XHandler* remove_last() { return _list.pop(); } duke@435: duke@435: bool could_catch(ciInstanceKlass* klass, bool type_is_exact) const; duke@435: bool equals(XHandlers* others) const; duke@435: }; duke@435: duke@435: duke@435: class IRScope; duke@435: define_array(IRScopeArray, IRScope*) duke@435: define_stack(IRScopeList, IRScopeArray) duke@435: duke@435: class Compilation; duke@435: class IRScope: public CompilationResourceObj { duke@435: private: duke@435: // hierarchy duke@435: Compilation* _compilation; // the current compilation duke@435: IRScope* _caller; // the caller scope, or NULL duke@435: int _level; // the inlining level duke@435: ciMethod* _method; // the corresponding method duke@435: IRScopeList _callees; // the inlined method scopes duke@435: duke@435: // graph duke@435: XHandlers* _xhandlers; // the exception handlers duke@435: int _number_of_locks; // the number of monitor lock slots needed duke@435: bool _monitor_pairing_ok; // the monitor pairing info duke@435: BlockBegin* _start; // the start block, successsors are method entries duke@435: duke@435: BitMap _requires_phi_function; // bit is set if phi functions at loop headers are necessary for a local variable duke@435: duke@435: // helper functions duke@435: BlockBegin* build_graph(Compilation* compilation, int osr_bci); duke@435: duke@435: public: duke@435: // creation duke@435: IRScope(Compilation* compilation, IRScope* caller, int caller_bci, ciMethod* method, int osr_bci, bool create_graph = false); duke@435: duke@435: // accessors duke@435: Compilation* compilation() const { return _compilation; } duke@435: IRScope* caller() const { return _caller; } duke@435: int level() const { return _level; } duke@435: ciMethod* method() const { return _method; } duke@435: int max_stack() const; // NOTE: expensive duke@435: BitMap& requires_phi_function() { return _requires_phi_function; } duke@435: duke@435: // hierarchy duke@435: bool is_top_scope() const { return _caller == NULL; } duke@435: void add_callee(IRScope* callee) { _callees.append(callee); } duke@435: int number_of_callees() const { return _callees.length(); } duke@435: IRScope* callee_no(int i) const { return _callees.at(i); } duke@435: duke@435: // accessors, graph duke@435: bool is_valid() const { return start() != NULL; } duke@435: XHandlers* xhandlers() const { return _xhandlers; } duke@435: int number_of_locks() const { return _number_of_locks; } duke@435: void set_min_number_of_locks(int n) { if (n > _number_of_locks) _number_of_locks = n; } duke@435: bool monitor_pairing_ok() const { return _monitor_pairing_ok; } duke@435: BlockBegin* start() const { return _start; } duke@435: }; duke@435: duke@435: duke@435: // duke@435: // IRScopeDebugInfo records the debug information for a particular IRScope duke@435: // in a particular CodeEmitInfo. This allows the information to be computed duke@435: // once early enough for the OopMap to be available to the LIR and also to be duke@435: // reemited for different pcs using the same CodeEmitInfo without recomputing duke@435: // everything. duke@435: // duke@435: duke@435: class IRScopeDebugInfo: public CompilationResourceObj { duke@435: private: duke@435: IRScope* _scope; duke@435: int _bci; duke@435: GrowableArray* _locals; duke@435: GrowableArray* _expressions; duke@435: GrowableArray* _monitors; duke@435: IRScopeDebugInfo* _caller; duke@435: duke@435: public: duke@435: IRScopeDebugInfo(IRScope* scope, duke@435: int bci, duke@435: GrowableArray* locals, duke@435: GrowableArray* expressions, duke@435: GrowableArray* monitors, duke@435: IRScopeDebugInfo* caller): duke@435: _scope(scope) duke@435: , _locals(locals) duke@435: , _bci(bci) duke@435: , _expressions(expressions) duke@435: , _monitors(monitors) duke@435: , _caller(caller) {} duke@435: duke@435: duke@435: IRScope* scope() { return _scope; } duke@435: int bci() { return _bci; } duke@435: GrowableArray* locals() { return _locals; } duke@435: GrowableArray* expressions() { return _expressions; } duke@435: GrowableArray* monitors() { return _monitors; } duke@435: IRScopeDebugInfo* caller() { return _caller; } duke@435: cfang@1335: //Whether we should reexecute this bytecode for deopt cfang@1335: bool should_reexecute(); cfang@1335: twisti@1730: void record_debug_info(DebugInformationRecorder* recorder, int pc_offset, bool topmost, bool is_method_handle_invoke = false) { duke@435: if (caller() != NULL) { duke@435: // Order is significant: Must record caller first. cfang@1335: caller()->record_debug_info(recorder, pc_offset, false/*topmost*/); duke@435: } duke@435: DebugToken* locvals = recorder->create_scope_values(locals()); duke@435: DebugToken* expvals = recorder->create_scope_values(expressions()); duke@435: DebugToken* monvals = recorder->create_monitor_values(monitors()); cfang@1335: // reexecute allowed only for the topmost frame twisti@1570: bool reexecute = topmost ? should_reexecute() : false; kvn@1688: bool return_oop = false; // This flag will be ignored since it used only for C2 with escape analysis. kvn@1688: recorder->describe_scope(pc_offset, scope()->method(), bci(), reexecute, is_method_handle_invoke, return_oop, locvals, expvals, monvals); duke@435: } duke@435: }; duke@435: duke@435: duke@435: class CodeEmitInfo: public CompilationResourceObj { duke@435: friend class LinearScan; duke@435: private: duke@435: IRScopeDebugInfo* _scope_debug_info; duke@435: IRScope* _scope; duke@435: XHandlers* _exception_handlers; duke@435: OopMap* _oop_map; duke@435: ValueStack* _stack; // used by deoptimization (contains also monitors twisti@1919: bool _is_method_handle_invoke; // true if the associated call site is a MethodHandle call site. duke@435: duke@435: FrameMap* frame_map() const { return scope()->compilation()->frame_map(); } duke@435: Compilation* compilation() const { return scope()->compilation(); } duke@435: duke@435: public: duke@435: duke@435: // use scope from ValueStack roland@2174: CodeEmitInfo(ValueStack* stack, XHandlers* exception_handlers); duke@435: duke@435: // make a copy roland@2174: CodeEmitInfo(CodeEmitInfo* info, ValueStack* stack = NULL); duke@435: duke@435: // accessors duke@435: OopMap* oop_map() { return _oop_map; } duke@435: ciMethod* method() const { return _scope->method(); } duke@435: IRScope* scope() const { return _scope; } duke@435: XHandlers* exception_handlers() const { return _exception_handlers; } duke@435: ValueStack* stack() const { return _stack; } duke@435: duke@435: void add_register_oop(LIR_Opr opr); twisti@1919: void record_debug_info(DebugInformationRecorder* recorder, int pc_offset); duke@435: twisti@1919: bool is_method_handle_invoke() const { return _is_method_handle_invoke; } twisti@1919: void set_is_method_handle_invoke(bool x) { _is_method_handle_invoke = x; } duke@435: }; duke@435: duke@435: duke@435: class IR: public CompilationResourceObj { duke@435: private: duke@435: Compilation* _compilation; // the current compilation duke@435: IRScope* _top_scope; // the root of the scope hierarchy duke@435: WordSize _locals_size; // the space required for all locals duke@435: int _num_loops; // Total number of loops duke@435: BlockList* _code; // the blocks in code generation order w/ use counts duke@435: duke@435: public: duke@435: // creation duke@435: IR(Compilation* compilation, ciMethod* method, int osr_bci); duke@435: duke@435: // accessors duke@435: bool is_valid() const { return top_scope()->is_valid(); } duke@435: Compilation* compilation() const { return _compilation; } duke@435: IRScope* top_scope() const { return _top_scope; } duke@435: int number_of_locks() const { return top_scope()->number_of_locks(); } duke@435: ciMethod* method() const { return top_scope()->method(); } duke@435: BlockBegin* start() const { return top_scope()->start(); } duke@435: BlockBegin* std_entry() const { return start()->end()->as_Base()->std_entry(); } duke@435: BlockBegin* osr_entry() const { return start()->end()->as_Base()->osr_entry(); } duke@435: WordSize locals_size() const { return _locals_size; } duke@435: int locals_size_in_words() const { return in_words(_locals_size); } duke@435: BlockList* code() const { return _code; } duke@435: int num_loops() const { return _num_loops; } duke@435: int max_stack() const { return top_scope()->max_stack(); } // expensive duke@435: duke@435: // ir manipulation duke@435: void optimize(); duke@435: void compute_predecessors(); duke@435: void split_critical_edges(); duke@435: void compute_code(); duke@435: void compute_use_counts(); duke@435: duke@435: // The linear-scan order and the code emission order are equal, but duke@435: // this may change in future duke@435: BlockList* linear_scan_order() { assert(_code != NULL, "not computed"); return _code; } duke@435: duke@435: // iteration duke@435: void iterate_preorder (BlockClosure* closure); duke@435: void iterate_postorder (BlockClosure* closure); duke@435: void iterate_linear_scan_order(BlockClosure* closure); duke@435: duke@435: // debugging duke@435: static void print(BlockBegin* start, bool cfg_only, bool live_only = false) PRODUCT_RETURN; duke@435: void print(bool cfg_only, bool live_only = false) PRODUCT_RETURN; duke@435: void verify() PRODUCT_RETURN; duke@435: }; duke@435: duke@435: duke@435: // Globally do instruction substitution and remove substituted duke@435: // instructions from the instruction list. duke@435: // duke@435: iveresov@1939: class SubstitutionResolver: public BlockClosure, ValueVisitor { iveresov@1939: virtual void visit(Value* v); duke@435: duke@435: public: duke@435: SubstitutionResolver(IR* hir) { duke@435: hir->iterate_preorder(this); duke@435: } duke@435: duke@435: SubstitutionResolver(BlockBegin* block) { duke@435: block->iterate_preorder(this); duke@435: } duke@435: duke@435: virtual void block_do(BlockBegin* block); duke@435: };