aoqi@0: /* aoqi@0: * Copyright (c) 2000, 2012, 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_CI_CITYPEFLOW_HPP aoqi@0: #define SHARE_VM_CI_CITYPEFLOW_HPP aoqi@0: aoqi@0: #ifdef COMPILER2 aoqi@0: #include "ci/ciEnv.hpp" aoqi@0: #include "ci/ciKlass.hpp" aoqi@0: #include "ci/ciMethodBlocks.hpp" aoqi@0: #endif aoqi@0: #ifdef SHARK aoqi@0: #include "ci/ciEnv.hpp" aoqi@0: #include "ci/ciKlass.hpp" aoqi@0: #include "ci/ciMethodBlocks.hpp" aoqi@0: #include "shark/shark_globals.hpp" aoqi@0: #endif aoqi@0: aoqi@0: aoqi@0: class ciTypeFlow : public ResourceObj { aoqi@0: private: aoqi@0: ciEnv* _env; aoqi@0: ciMethod* _method; aoqi@0: ciMethodBlocks* _methodBlocks; aoqi@0: int _osr_bci; aoqi@0: aoqi@0: // information cached from the method: aoqi@0: int _max_locals; aoqi@0: int _max_stack; aoqi@0: int _code_size; aoqi@0: bool _has_irreducible_entry; aoqi@0: aoqi@0: const char* _failure_reason; aoqi@0: aoqi@0: public: aoqi@0: class StateVector; aoqi@0: class Loop; aoqi@0: class Block; aoqi@0: aoqi@0: // Build a type flow analyzer aoqi@0: // Do an OSR analysis if osr_bci >= 0. aoqi@0: ciTypeFlow(ciEnv* env, ciMethod* method, int osr_bci = InvocationEntryBci); aoqi@0: aoqi@0: // Accessors aoqi@0: ciMethod* method() const { return _method; } aoqi@0: ciEnv* env() { return _env; } aoqi@0: Arena* arena() { return _env->arena(); } aoqi@0: bool is_osr_flow() const{ return _osr_bci != InvocationEntryBci; } aoqi@0: int start_bci() const { return is_osr_flow()? _osr_bci: 0; } aoqi@0: int max_locals() const { return _max_locals; } aoqi@0: int max_stack() const { return _max_stack; } aoqi@0: int max_cells() const { return _max_locals + _max_stack; } aoqi@0: int code_size() const { return _code_size; } aoqi@0: bool has_irreducible_entry() const { return _has_irreducible_entry; } aoqi@0: aoqi@0: // Represents information about an "active" jsr call. This aoqi@0: // class represents a call to the routine at some entry address aoqi@0: // with some distinct return address. aoqi@0: class JsrRecord : public ResourceObj { aoqi@0: private: aoqi@0: int _entry_address; aoqi@0: int _return_address; aoqi@0: public: aoqi@0: JsrRecord(int entry_address, int return_address) { aoqi@0: _entry_address = entry_address; aoqi@0: _return_address = return_address; aoqi@0: } aoqi@0: aoqi@0: int entry_address() const { return _entry_address; } aoqi@0: int return_address() const { return _return_address; } aoqi@0: aoqi@0: void print_on(outputStream* st) const { aoqi@0: #ifndef PRODUCT aoqi@0: st->print("%d->%d", entry_address(), return_address()); aoqi@0: #endif aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: // A JsrSet represents some set of JsrRecords. This class aoqi@0: // is used to record a set of all jsr routines which we permit aoqi@0: // execution to return (ret) from. aoqi@0: // aoqi@0: // During abstract interpretation, JsrSets are used to determine aoqi@0: // whether two paths which reach a given block are unique, and aoqi@0: // should be cloned apart, or are compatible, and should merge aoqi@0: // together. aoqi@0: // aoqi@0: // Note that different amounts of effort can be expended determining aoqi@0: // if paths are compatible. aoqi@0: class JsrSet : public ResourceObj { aoqi@0: private: aoqi@0: GrowableArray* _set; aoqi@0: aoqi@0: JsrRecord* record_at(int i) { aoqi@0: return _set->at(i); aoqi@0: } aoqi@0: aoqi@0: // Insert the given JsrRecord into the JsrSet, maintaining the order aoqi@0: // of the set and replacing any element with the same entry address. aoqi@0: void insert_jsr_record(JsrRecord* record); aoqi@0: aoqi@0: // Remove the JsrRecord with the given return address from the JsrSet. aoqi@0: void remove_jsr_record(int return_address); aoqi@0: aoqi@0: public: aoqi@0: JsrSet(Arena* arena, int default_len = 4); aoqi@0: aoqi@0: // Copy this JsrSet. aoqi@0: void copy_into(JsrSet* jsrs); aoqi@0: aoqi@0: // Is this JsrSet compatible with some other JsrSet? aoqi@0: bool is_compatible_with(JsrSet* other); aoqi@0: aoqi@0: // Apply the effect of a single bytecode to the JsrSet. aoqi@0: void apply_control(ciTypeFlow* analyzer, aoqi@0: ciBytecodeStream* str, aoqi@0: StateVector* state); aoqi@0: aoqi@0: // What is the cardinality of this set? aoqi@0: int size() const { return _set->length(); } aoqi@0: aoqi@0: void print_on(outputStream* st) const PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: class LocalSet VALUE_OBJ_CLASS_SPEC { aoqi@0: private: aoqi@0: enum Constants { max = 63 }; aoqi@0: uint64_t _bits; aoqi@0: public: aoqi@0: LocalSet() : _bits(0) {} aoqi@0: void add(uint32_t i) { if (i < (uint32_t)max) _bits |= (1LL << i); } aoqi@0: void add(LocalSet* ls) { _bits |= ls->_bits; } aoqi@0: bool test(uint32_t i) const { return i < (uint32_t)max ? (_bits>>i)&1U : true; } aoqi@0: void clear() { _bits = 0; } aoqi@0: void print_on(outputStream* st, int limit) const PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: // Used as a combined index for locals and temps aoqi@0: enum Cell { aoqi@0: Cell_0, Cell_max = INT_MAX aoqi@0: }; aoqi@0: aoqi@0: // A StateVector summarizes the type information at some aoqi@0: // point in the program aoqi@0: class StateVector : public ResourceObj { aoqi@0: private: aoqi@0: ciType** _types; aoqi@0: int _stack_size; aoqi@0: int _monitor_count; aoqi@0: ciTypeFlow* _outer; aoqi@0: aoqi@0: int _trap_bci; aoqi@0: int _trap_index; aoqi@0: aoqi@0: LocalSet _def_locals; // For entire block aoqi@0: aoqi@0: static ciType* type_meet_internal(ciType* t1, ciType* t2, ciTypeFlow* analyzer); aoqi@0: aoqi@0: public: aoqi@0: // Special elements in our type lattice. aoqi@0: enum { aoqi@0: T_TOP = T_VOID, // why not? aoqi@0: T_BOTTOM = T_CONFLICT, aoqi@0: T_LONG2 = T_SHORT, // 2nd word of T_LONG aoqi@0: T_DOUBLE2 = T_CHAR, // 2nd word of T_DOUBLE aoqi@0: T_NULL = T_BYTE // for now. aoqi@0: }; aoqi@0: static ciType* top_type() { return ciType::make((BasicType)T_TOP); } aoqi@0: static ciType* bottom_type() { return ciType::make((BasicType)T_BOTTOM); } aoqi@0: static ciType* long2_type() { return ciType::make((BasicType)T_LONG2); } aoqi@0: static ciType* double2_type(){ return ciType::make((BasicType)T_DOUBLE2); } aoqi@0: static ciType* null_type() { return ciType::make((BasicType)T_NULL); } aoqi@0: aoqi@0: static ciType* half_type(ciType* t) { aoqi@0: switch (t->basic_type()) { aoqi@0: case T_LONG: return long2_type(); aoqi@0: case T_DOUBLE: return double2_type(); aoqi@0: default: ShouldNotReachHere(); return NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // The meet operation for our type lattice. aoqi@0: ciType* type_meet(ciType* t1, ciType* t2) { aoqi@0: return type_meet_internal(t1, t2, outer()); aoqi@0: } aoqi@0: aoqi@0: // Accessors aoqi@0: ciTypeFlow* outer() const { return _outer; } aoqi@0: aoqi@0: int stack_size() const { return _stack_size; } aoqi@0: void set_stack_size(int ss) { _stack_size = ss; } aoqi@0: aoqi@0: int monitor_count() const { return _monitor_count; } aoqi@0: void set_monitor_count(int mc) { _monitor_count = mc; } aoqi@0: aoqi@0: LocalSet* def_locals() { return &_def_locals; } aoqi@0: const LocalSet* def_locals() const { return &_def_locals; } aoqi@0: aoqi@0: static Cell start_cell() { return (Cell)0; } aoqi@0: static Cell next_cell(Cell c) { return (Cell)(((int)c) + 1); } aoqi@0: Cell limit_cell() const { aoqi@0: return (Cell)(outer()->max_locals() + stack_size()); aoqi@0: } aoqi@0: aoqi@0: // Cell creation aoqi@0: Cell local(int lnum) const { aoqi@0: assert(lnum < outer()->max_locals(), "index check"); aoqi@0: return (Cell)(lnum); aoqi@0: } aoqi@0: aoqi@0: Cell stack(int snum) const { aoqi@0: assert(snum < stack_size(), "index check"); aoqi@0: return (Cell)(outer()->max_locals() + snum); aoqi@0: } aoqi@0: aoqi@0: Cell tos() const { return stack(stack_size()-1); } aoqi@0: aoqi@0: // For external use only: aoqi@0: ciType* local_type_at(int i) const { return type_at(local(i)); } aoqi@0: ciType* stack_type_at(int i) const { return type_at(stack(i)); } aoqi@0: aoqi@0: // Accessors for the type of some Cell c aoqi@0: ciType* type_at(Cell c) const { aoqi@0: assert(start_cell() <= c && c < limit_cell(), "out of bounds"); aoqi@0: return _types[c]; aoqi@0: } aoqi@0: aoqi@0: void set_type_at(Cell c, ciType* type) { aoqi@0: assert(start_cell() <= c && c < limit_cell(), "out of bounds"); aoqi@0: _types[c] = type; aoqi@0: } aoqi@0: aoqi@0: // Top-of-stack operations. aoqi@0: void set_type_at_tos(ciType* type) { set_type_at(tos(), type); } aoqi@0: ciType* type_at_tos() const { return type_at(tos()); } aoqi@0: aoqi@0: void push(ciType* type) { aoqi@0: _stack_size++; aoqi@0: set_type_at_tos(type); aoqi@0: } aoqi@0: void pop() { aoqi@0: debug_only(set_type_at_tos(bottom_type())); aoqi@0: _stack_size--; aoqi@0: } aoqi@0: ciType* pop_value() { aoqi@0: ciType* t = type_at_tos(); aoqi@0: pop(); aoqi@0: return t; aoqi@0: } aoqi@0: aoqi@0: // Convenience operations. aoqi@0: bool is_reference(ciType* type) const { aoqi@0: return type == null_type() || !type->is_primitive_type(); aoqi@0: } aoqi@0: bool is_int(ciType* type) const { aoqi@0: return type->basic_type() == T_INT; aoqi@0: } aoqi@0: bool is_long(ciType* type) const { aoqi@0: return type->basic_type() == T_LONG; aoqi@0: } aoqi@0: bool is_float(ciType* type) const { aoqi@0: return type->basic_type() == T_FLOAT; aoqi@0: } aoqi@0: bool is_double(ciType* type) const { aoqi@0: return type->basic_type() == T_DOUBLE; aoqi@0: } aoqi@0: aoqi@0: void store_to_local(int lnum) { aoqi@0: _def_locals.add((uint) lnum); aoqi@0: } aoqi@0: aoqi@0: void push_translate(ciType* type); aoqi@0: aoqi@0: void push_int() { aoqi@0: push(ciType::make(T_INT)); aoqi@0: } aoqi@0: void pop_int() { aoqi@0: assert(is_int(type_at_tos()), "must be integer"); aoqi@0: pop(); aoqi@0: } aoqi@0: void check_int(Cell c) { aoqi@0: assert(is_int(type_at(c)), "must be integer"); aoqi@0: } aoqi@0: void push_double() { aoqi@0: push(ciType::make(T_DOUBLE)); aoqi@0: push(double2_type()); aoqi@0: } aoqi@0: void pop_double() { aoqi@0: assert(type_at_tos() == double2_type(), "must be 2nd half"); aoqi@0: pop(); aoqi@0: assert(is_double(type_at_tos()), "must be double"); aoqi@0: pop(); aoqi@0: } aoqi@0: void push_float() { aoqi@0: push(ciType::make(T_FLOAT)); aoqi@0: } aoqi@0: void pop_float() { aoqi@0: assert(is_float(type_at_tos()), "must be float"); aoqi@0: pop(); aoqi@0: } aoqi@0: void push_long() { aoqi@0: push(ciType::make(T_LONG)); aoqi@0: push(long2_type()); aoqi@0: } aoqi@0: void pop_long() { aoqi@0: assert(type_at_tos() == long2_type(), "must be 2nd half"); aoqi@0: pop(); aoqi@0: assert(is_long(type_at_tos()), "must be long"); aoqi@0: pop(); aoqi@0: } aoqi@0: void push_object(ciKlass* klass) { aoqi@0: push(klass); aoqi@0: } aoqi@0: void pop_object() { aoqi@0: assert(is_reference(type_at_tos()), "must be reference type"); aoqi@0: pop(); aoqi@0: } aoqi@0: void pop_array() { aoqi@0: assert(type_at_tos() == null_type() || aoqi@0: type_at_tos()->is_array_klass(), "must be array type"); aoqi@0: pop(); aoqi@0: } aoqi@0: // pop_objArray and pop_typeArray narrow the tos to ciObjArrayKlass aoqi@0: // or ciTypeArrayKlass (resp.). In the rare case that an explicit aoqi@0: // null is popped from the stack, we return NULL. Caller beware. aoqi@0: ciObjArrayKlass* pop_objArray() { aoqi@0: ciType* array = pop_value(); aoqi@0: if (array == null_type()) return NULL; aoqi@0: assert(array->is_obj_array_klass(), "must be object array type"); aoqi@0: return array->as_obj_array_klass(); aoqi@0: } aoqi@0: ciTypeArrayKlass* pop_typeArray() { aoqi@0: ciType* array = pop_value(); aoqi@0: if (array == null_type()) return NULL; aoqi@0: assert(array->is_type_array_klass(), "must be prim array type"); aoqi@0: return array->as_type_array_klass(); aoqi@0: } aoqi@0: void push_null() { aoqi@0: push(null_type()); aoqi@0: } aoqi@0: void do_null_assert(ciKlass* unloaded_klass); aoqi@0: aoqi@0: // Helper convenience routines. aoqi@0: void do_aaload(ciBytecodeStream* str); aoqi@0: void do_checkcast(ciBytecodeStream* str); aoqi@0: void do_getfield(ciBytecodeStream* str); aoqi@0: void do_getstatic(ciBytecodeStream* str); aoqi@0: void do_invoke(ciBytecodeStream* str, bool has_receiver); aoqi@0: void do_jsr(ciBytecodeStream* str); aoqi@0: void do_ldc(ciBytecodeStream* str); aoqi@0: void do_multianewarray(ciBytecodeStream* str); aoqi@0: void do_new(ciBytecodeStream* str); aoqi@0: void do_newarray(ciBytecodeStream* str); aoqi@0: void do_putfield(ciBytecodeStream* str); aoqi@0: void do_putstatic(ciBytecodeStream* str); aoqi@0: void do_ret(ciBytecodeStream* str); aoqi@0: aoqi@0: void overwrite_local_double_long(int index) { aoqi@0: // Invalidate the previous local if it contains first half of aoqi@0: // a double or long value since it's seconf half is being overwritten. aoqi@0: int prev_index = index - 1; aoqi@0: if (prev_index >= 0 && aoqi@0: (is_double(type_at(local(prev_index))) || aoqi@0: is_long(type_at(local(prev_index))))) { aoqi@0: set_type_at(local(prev_index), bottom_type()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void load_local_object(int index) { aoqi@0: ciType* type = type_at(local(index)); aoqi@0: assert(is_reference(type), "must be reference type"); aoqi@0: push(type); aoqi@0: } aoqi@0: void store_local_object(int index) { aoqi@0: ciType* type = pop_value(); aoqi@0: assert(is_reference(type) || type->is_return_address(), aoqi@0: "must be reference type or return address"); aoqi@0: overwrite_local_double_long(index); aoqi@0: set_type_at(local(index), type); aoqi@0: store_to_local(index); aoqi@0: } aoqi@0: aoqi@0: void load_local_double(int index) { aoqi@0: ciType* type = type_at(local(index)); aoqi@0: ciType* type2 = type_at(local(index+1)); aoqi@0: assert(is_double(type), "must be double type"); aoqi@0: assert(type2 == double2_type(), "must be 2nd half"); aoqi@0: push(type); aoqi@0: push(double2_type()); aoqi@0: } aoqi@0: void store_local_double(int index) { aoqi@0: ciType* type2 = pop_value(); aoqi@0: ciType* type = pop_value(); aoqi@0: assert(is_double(type), "must be double"); aoqi@0: assert(type2 == double2_type(), "must be 2nd half"); aoqi@0: overwrite_local_double_long(index); aoqi@0: set_type_at(local(index), type); aoqi@0: set_type_at(local(index+1), type2); aoqi@0: store_to_local(index); aoqi@0: store_to_local(index+1); aoqi@0: } aoqi@0: aoqi@0: void load_local_float(int index) { aoqi@0: ciType* type = type_at(local(index)); aoqi@0: assert(is_float(type), "must be float type"); aoqi@0: push(type); aoqi@0: } aoqi@0: void store_local_float(int index) { aoqi@0: ciType* type = pop_value(); aoqi@0: assert(is_float(type), "must be float type"); aoqi@0: overwrite_local_double_long(index); aoqi@0: set_type_at(local(index), type); aoqi@0: store_to_local(index); aoqi@0: } aoqi@0: aoqi@0: void load_local_int(int index) { aoqi@0: ciType* type = type_at(local(index)); aoqi@0: assert(is_int(type), "must be int type"); aoqi@0: push(type); aoqi@0: } aoqi@0: void store_local_int(int index) { aoqi@0: ciType* type = pop_value(); aoqi@0: assert(is_int(type), "must be int type"); aoqi@0: overwrite_local_double_long(index); aoqi@0: set_type_at(local(index), type); aoqi@0: store_to_local(index); aoqi@0: } aoqi@0: aoqi@0: void load_local_long(int index) { aoqi@0: ciType* type = type_at(local(index)); aoqi@0: ciType* type2 = type_at(local(index+1)); aoqi@0: assert(is_long(type), "must be long type"); aoqi@0: assert(type2 == long2_type(), "must be 2nd half"); aoqi@0: push(type); aoqi@0: push(long2_type()); aoqi@0: } aoqi@0: void store_local_long(int index) { aoqi@0: ciType* type2 = pop_value(); aoqi@0: ciType* type = pop_value(); aoqi@0: assert(is_long(type), "must be long"); aoqi@0: assert(type2 == long2_type(), "must be 2nd half"); aoqi@0: overwrite_local_double_long(index); aoqi@0: set_type_at(local(index), type); aoqi@0: set_type_at(local(index+1), type2); aoqi@0: store_to_local(index); aoqi@0: store_to_local(index+1); aoqi@0: } aoqi@0: aoqi@0: // Stop interpretation of this path with a trap. aoqi@0: void trap(ciBytecodeStream* str, ciKlass* klass, int index); aoqi@0: aoqi@0: public: aoqi@0: StateVector(ciTypeFlow* outer); aoqi@0: aoqi@0: // Copy our value into some other StateVector aoqi@0: void copy_into(StateVector* copy) const; aoqi@0: aoqi@0: // Meets this StateVector with another, destructively modifying this aoqi@0: // one. Returns true if any modification takes place. aoqi@0: bool meet(const StateVector* incoming); aoqi@0: aoqi@0: // Ditto, except that the incoming state is coming from an exception. aoqi@0: bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming); aoqi@0: aoqi@0: // Apply the effect of one bytecode to this StateVector aoqi@0: bool apply_one_bytecode(ciBytecodeStream* stream); aoqi@0: aoqi@0: // What is the bci of the trap? aoqi@0: int trap_bci() { return _trap_bci; } aoqi@0: aoqi@0: // What is the index associated with the trap? aoqi@0: int trap_index() { return _trap_index; } aoqi@0: aoqi@0: void print_cell_on(outputStream* st, Cell c) const PRODUCT_RETURN; aoqi@0: void print_on(outputStream* st) const PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: // Parameter for "find_block" calls: aoqi@0: // Describes the difference between a public and backedge copy. aoqi@0: enum CreateOption { aoqi@0: create_public_copy, aoqi@0: create_backedge_copy, aoqi@0: no_create aoqi@0: }; aoqi@0: aoqi@0: // Successor iterator aoqi@0: class SuccIter : public StackObj { aoqi@0: private: aoqi@0: Block* _pred; aoqi@0: int _index; aoqi@0: Block* _succ; aoqi@0: public: aoqi@0: SuccIter() : _pred(NULL), _index(-1), _succ(NULL) {} aoqi@0: SuccIter(Block* pred) : _pred(pred), _index(-1), _succ(NULL) { next(); } aoqi@0: int index() { return _index; } aoqi@0: Block* pred() { return _pred; } // Return predecessor aoqi@0: bool done() { return _index < 0; } // Finished? aoqi@0: Block* succ() { return _succ; } // Return current successor aoqi@0: void next(); // Advance aoqi@0: void set_succ(Block* succ); // Update current successor aoqi@0: bool is_normal_ctrl() { return index() < _pred->successors()->length(); } aoqi@0: }; aoqi@0: aoqi@0: // A basic block aoqi@0: class Block : public ResourceObj { aoqi@0: private: aoqi@0: ciBlock* _ciblock; aoqi@0: GrowableArray* _exceptions; aoqi@0: GrowableArray* _exc_klasses; aoqi@0: GrowableArray* _successors; aoqi@0: StateVector* _state; aoqi@0: JsrSet* _jsrs; aoqi@0: aoqi@0: int _trap_bci; aoqi@0: int _trap_index; aoqi@0: aoqi@0: // pre_order, assigned at first visit. Used as block ID and "visited" tag aoqi@0: int _pre_order; aoqi@0: aoqi@0: // A post-order, used to compute the reverse post order (RPO) provided to the client aoqi@0: int _post_order; // used to compute rpo aoqi@0: aoqi@0: // Has this block been cloned for a loop backedge? aoqi@0: bool _backedge_copy; aoqi@0: aoqi@0: // This block is entry to irreducible loop. aoqi@0: bool _irreducible_entry; aoqi@0: aoqi@0: // This block has monitor entry point. aoqi@0: bool _has_monitorenter; aoqi@0: aoqi@0: // A pointer used for our internal work list aoqi@0: bool _on_work_list; // on the work list aoqi@0: Block* _next; aoqi@0: Block* _rpo_next; // Reverse post order list aoqi@0: aoqi@0: // Loop info aoqi@0: Loop* _loop; // nearest loop aoqi@0: aoqi@0: ciBlock* ciblock() const { return _ciblock; } aoqi@0: StateVector* state() const { return _state; } aoqi@0: aoqi@0: // Compute the exceptional successors and types for this Block. aoqi@0: void compute_exceptions(); aoqi@0: aoqi@0: public: aoqi@0: // constructors aoqi@0: Block(ciTypeFlow* outer, ciBlock* ciblk, JsrSet* jsrs); aoqi@0: aoqi@0: void set_trap(int trap_bci, int trap_index) { aoqi@0: _trap_bci = trap_bci; aoqi@0: _trap_index = trap_index; aoqi@0: assert(has_trap(), ""); aoqi@0: } aoqi@0: bool has_trap() const { return _trap_bci != -1; } aoqi@0: int trap_bci() const { assert(has_trap(), ""); return _trap_bci; } aoqi@0: int trap_index() const { assert(has_trap(), ""); return _trap_index; } aoqi@0: aoqi@0: // accessors aoqi@0: ciTypeFlow* outer() const { return state()->outer(); } aoqi@0: int start() const { return _ciblock->start_bci(); } aoqi@0: int limit() const { return _ciblock->limit_bci(); } aoqi@0: int control() const { return _ciblock->control_bci(); } aoqi@0: JsrSet* jsrs() const { return _jsrs; } aoqi@0: aoqi@0: bool is_backedge_copy() const { return _backedge_copy; } aoqi@0: void set_backedge_copy(bool z); aoqi@0: int backedge_copy_count() const { return outer()->backedge_copy_count(ciblock()->index(), _jsrs); } aoqi@0: aoqi@0: // access to entry state aoqi@0: int stack_size() const { return _state->stack_size(); } aoqi@0: int monitor_count() const { return _state->monitor_count(); } aoqi@0: ciType* local_type_at(int i) const { return _state->local_type_at(i); } aoqi@0: ciType* stack_type_at(int i) const { return _state->stack_type_at(i); } aoqi@0: aoqi@0: // Data flow on locals aoqi@0: bool is_invariant_local(uint v) const { aoqi@0: assert(is_loop_head(), "only loop heads"); aoqi@0: // Find outermost loop with same loop head aoqi@0: Loop* lp = loop(); aoqi@0: while (lp->parent() != NULL) { aoqi@0: if (lp->parent()->head() != lp->head()) break; aoqi@0: lp = lp->parent(); aoqi@0: } aoqi@0: return !lp->def_locals()->test(v); aoqi@0: } aoqi@0: LocalSet* def_locals() { return _state->def_locals(); } aoqi@0: const LocalSet* def_locals() const { return _state->def_locals(); } aoqi@0: aoqi@0: // Get the successors for this Block. aoqi@0: GrowableArray* successors(ciBytecodeStream* str, aoqi@0: StateVector* state, aoqi@0: JsrSet* jsrs); aoqi@0: GrowableArray* successors() { aoqi@0: assert(_successors != NULL, "must be filled in"); aoqi@0: return _successors; aoqi@0: } aoqi@0: aoqi@0: // Get the exceptional successors for this Block. aoqi@0: GrowableArray* exceptions() { aoqi@0: if (_exceptions == NULL) { aoqi@0: compute_exceptions(); aoqi@0: } aoqi@0: return _exceptions; aoqi@0: } aoqi@0: aoqi@0: // Get the exception klasses corresponding to the aoqi@0: // exceptional successors for this Block. aoqi@0: GrowableArray* exc_klasses() { aoqi@0: if (_exc_klasses == NULL) { aoqi@0: compute_exceptions(); aoqi@0: } aoqi@0: return _exc_klasses; aoqi@0: } aoqi@0: aoqi@0: // Is this Block compatible with a given JsrSet? aoqi@0: bool is_compatible_with(JsrSet* other) { aoqi@0: return _jsrs->is_compatible_with(other); aoqi@0: } aoqi@0: aoqi@0: // Copy the value of our state vector into another. aoqi@0: void copy_state_into(StateVector* copy) const { aoqi@0: _state->copy_into(copy); aoqi@0: } aoqi@0: aoqi@0: // Copy the value of our JsrSet into another aoqi@0: void copy_jsrs_into(JsrSet* copy) const { aoqi@0: _jsrs->copy_into(copy); aoqi@0: } aoqi@0: aoqi@0: // Meets the start state of this block with another state, destructively aoqi@0: // modifying this one. Returns true if any modification takes place. aoqi@0: bool meet(const StateVector* incoming) { aoqi@0: return state()->meet(incoming); aoqi@0: } aoqi@0: aoqi@0: // Ditto, except that the incoming state is coming from an aoqi@0: // exception path. This means the stack is replaced by the aoqi@0: // appropriate exception type. aoqi@0: bool meet_exception(ciInstanceKlass* exc, const StateVector* incoming) { aoqi@0: return state()->meet_exception(exc, incoming); aoqi@0: } aoqi@0: aoqi@0: // Work list manipulation aoqi@0: void set_next(Block* block) { _next = block; } aoqi@0: Block* next() const { return _next; } aoqi@0: aoqi@0: void set_on_work_list(bool c) { _on_work_list = c; } aoqi@0: bool is_on_work_list() const { return _on_work_list; } aoqi@0: aoqi@0: bool has_pre_order() const { return _pre_order >= 0; } aoqi@0: void set_pre_order(int po) { assert(!has_pre_order(), ""); _pre_order = po; } aoqi@0: int pre_order() const { assert(has_pre_order(), ""); return _pre_order; } aoqi@0: void set_next_pre_order() { set_pre_order(outer()->inc_next_pre_order()); } aoqi@0: bool is_start() const { return _pre_order == outer()->start_block_num(); } aoqi@0: aoqi@0: // Reverse post order aoqi@0: void df_init(); aoqi@0: bool has_post_order() const { return _post_order >= 0; } aoqi@0: void set_post_order(int po) { assert(!has_post_order() && po >= 0, ""); _post_order = po; } aoqi@0: void reset_post_order(int o){ _post_order = o; } aoqi@0: int post_order() const { assert(has_post_order(), ""); return _post_order; } aoqi@0: aoqi@0: bool has_rpo() const { return has_post_order() && outer()->have_block_count(); } aoqi@0: int rpo() const { assert(has_rpo(), ""); return outer()->block_count() - post_order() - 1; } aoqi@0: void set_rpo_next(Block* b) { _rpo_next = b; } aoqi@0: Block* rpo_next() { return _rpo_next; } aoqi@0: aoqi@0: // Loops aoqi@0: Loop* loop() const { return _loop; } aoqi@0: void set_loop(Loop* lp) { _loop = lp; } aoqi@0: bool is_loop_head() const { return _loop && _loop->head() == this; } aoqi@0: void set_irreducible_entry(bool c) { _irreducible_entry = c; } aoqi@0: bool is_irreducible_entry() const { return _irreducible_entry; } aoqi@0: void set_has_monitorenter() { _has_monitorenter = true; } aoqi@0: bool has_monitorenter() const { return _has_monitorenter; } aoqi@0: bool is_visited() const { return has_pre_order(); } aoqi@0: bool is_post_visited() const { return has_post_order(); } aoqi@0: bool is_clonable_exit(Loop* lp); aoqi@0: Block* looping_succ(Loop* lp); // Successor inside of loop aoqi@0: bool is_single_entry_loop_head() const { aoqi@0: if (!is_loop_head()) return false; aoqi@0: for (Loop* lp = loop(); lp != NULL && lp->head() == this; lp = lp->parent()) aoqi@0: if (lp->is_irreducible()) return false; aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void print_value_on(outputStream* st) const PRODUCT_RETURN; aoqi@0: void print_on(outputStream* st) const PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: // Loop aoqi@0: class Loop : public ResourceObj { aoqi@0: private: aoqi@0: Loop* _parent; aoqi@0: Loop* _sibling; // List of siblings, null terminated aoqi@0: Loop* _child; // Head of child list threaded thru sibling pointer aoqi@0: Block* _head; // Head of loop aoqi@0: Block* _tail; // Tail of loop aoqi@0: bool _irreducible; aoqi@0: LocalSet _def_locals; aoqi@0: aoqi@0: public: aoqi@0: Loop(Block* head, Block* tail) : aoqi@0: _head(head), _tail(tail), aoqi@0: _parent(NULL), _sibling(NULL), _child(NULL), aoqi@0: _irreducible(false), _def_locals() {} aoqi@0: aoqi@0: Loop* parent() const { return _parent; } aoqi@0: Loop* sibling() const { return _sibling; } aoqi@0: Loop* child() const { return _child; } aoqi@0: Block* head() const { return _head; } aoqi@0: Block* tail() const { return _tail; } aoqi@0: void set_parent(Loop* p) { _parent = p; } aoqi@0: void set_sibling(Loop* s) { _sibling = s; } aoqi@0: void set_child(Loop* c) { _child = c; } aoqi@0: void set_head(Block* hd) { _head = hd; } aoqi@0: void set_tail(Block* tl) { _tail = tl; } aoqi@0: aoqi@0: int depth() const; // nesting depth aoqi@0: aoqi@0: // Returns true if lp is a nested loop or us. aoqi@0: bool contains(Loop* lp) const; aoqi@0: bool contains(Block* blk) const { return contains(blk->loop()); } aoqi@0: aoqi@0: // Data flow on locals aoqi@0: LocalSet* def_locals() { return &_def_locals; } aoqi@0: const LocalSet* def_locals() const { return &_def_locals; } aoqi@0: aoqi@0: // Merge the branch lp into this branch, sorting on the loop head aoqi@0: // pre_orders. Returns the new branch. aoqi@0: Loop* sorted_merge(Loop* lp); aoqi@0: aoqi@0: // Mark non-single entry to loop aoqi@0: void set_irreducible(Block* entry) { aoqi@0: _irreducible = true; aoqi@0: entry->set_irreducible_entry(true); aoqi@0: } aoqi@0: bool is_irreducible() const { return _irreducible; } aoqi@0: aoqi@0: bool is_root() const { return _tail->pre_order() == max_jint; } aoqi@0: aoqi@0: void print(outputStream* st = tty, int indent = 0) const PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: // Postorder iteration over the loop tree. aoqi@0: class PostorderLoops : public StackObj { aoqi@0: private: aoqi@0: Loop* _root; aoqi@0: Loop* _current; aoqi@0: public: aoqi@0: PostorderLoops(Loop* root) : _root(root), _current(root) { aoqi@0: while (_current->child() != NULL) { aoqi@0: _current = _current->child(); aoqi@0: } aoqi@0: } aoqi@0: bool done() { return _current == NULL; } // Finished iterating? aoqi@0: void next(); // Advance to next loop aoqi@0: Loop* current() { return _current; } // Return current loop. aoqi@0: }; aoqi@0: aoqi@0: // Preorder iteration over the loop tree. aoqi@0: class PreorderLoops : public StackObj { aoqi@0: private: aoqi@0: Loop* _root; aoqi@0: Loop* _current; aoqi@0: public: aoqi@0: PreorderLoops(Loop* root) : _root(root), _current(root) {} aoqi@0: bool done() { return _current == NULL; } // Finished iterating? aoqi@0: void next(); // Advance to next loop aoqi@0: Loop* current() { return _current; } // Return current loop. aoqi@0: }; aoqi@0: aoqi@0: // Standard indexes of successors, for various bytecodes. aoqi@0: enum { aoqi@0: FALL_THROUGH = 0, // normal control aoqi@0: IF_NOT_TAKEN = 0, // the not-taken branch of an if (i.e., fall-through) aoqi@0: IF_TAKEN = 1, // the taken branch of an if aoqi@0: GOTO_TARGET = 0, // unique successor for goto, jsr, or ret aoqi@0: SWITCH_DEFAULT = 0, // default branch of a switch aoqi@0: SWITCH_CASES = 1 // first index for any non-default switch branches aoqi@0: // Unlike in other blocks, the successors of a switch are listed uniquely. aoqi@0: }; aoqi@0: aoqi@0: private: aoqi@0: // A mapping from pre_order to Blocks. This array is created aoqi@0: // only at the end of the flow. aoqi@0: Block** _block_map; aoqi@0: aoqi@0: // For each ciBlock index, a list of Blocks which share this ciBlock. aoqi@0: GrowableArray** _idx_to_blocklist; aoqi@0: // count of ciBlocks aoqi@0: int _ciblock_count; aoqi@0: aoqi@0: // Tells if a given instruction is able to generate an exception edge. aoqi@0: bool can_trap(ciBytecodeStream& str); aoqi@0: aoqi@0: // Clone the loop heads. Returns true if any cloning occurred. aoqi@0: bool clone_loop_heads(Loop* lp, StateVector* temp_vector, JsrSet* temp_set); aoqi@0: aoqi@0: // Clone lp's head and replace tail's successors with clone. aoqi@0: Block* clone_loop_head(Loop* lp, StateVector* temp_vector, JsrSet* temp_set); aoqi@0: aoqi@0: public: aoqi@0: // Return the block beginning at bci which has a JsrSet compatible aoqi@0: // with jsrs. aoqi@0: Block* block_at(int bci, JsrSet* set, CreateOption option = create_public_copy); aoqi@0: aoqi@0: // block factory aoqi@0: Block* get_block_for(int ciBlockIndex, JsrSet* jsrs, CreateOption option = create_public_copy); aoqi@0: aoqi@0: // How many of the blocks have the backedge_copy bit set? aoqi@0: int backedge_copy_count(int ciBlockIndex, JsrSet* jsrs) const; aoqi@0: aoqi@0: // Return an existing block containing bci which has a JsrSet compatible aoqi@0: // with jsrs, or NULL if there is none. aoqi@0: Block* existing_block_at(int bci, JsrSet* set) { return block_at(bci, set, no_create); } aoqi@0: aoqi@0: // Tell whether the flow analysis has encountered an error of some sort. aoqi@0: bool failing() { return env()->failing() || _failure_reason != NULL; } aoqi@0: aoqi@0: // Reason this compilation is failing, such as "too many basic blocks". aoqi@0: const char* failure_reason() { return _failure_reason; } aoqi@0: aoqi@0: // Note a failure. aoqi@0: void record_failure(const char* reason); aoqi@0: aoqi@0: // Return the block of a given pre-order number. aoqi@0: int have_block_count() const { return _block_map != NULL; } aoqi@0: int block_count() const { assert(have_block_count(), ""); aoqi@0: return _next_pre_order; } aoqi@0: Block* pre_order_at(int po) const { assert(0 <= po && po < block_count(), "out of bounds"); aoqi@0: return _block_map[po]; } aoqi@0: Block* start_block() const { return pre_order_at(start_block_num()); } aoqi@0: int start_block_num() const { return 0; } aoqi@0: Block* rpo_at(int rpo) const { assert(0 <= rpo && rpo < block_count(), "out of bounds"); aoqi@0: return _block_map[rpo]; } aoqi@0: int next_pre_order() { return _next_pre_order; } aoqi@0: int inc_next_pre_order() { return _next_pre_order++; } aoqi@0: aoqi@0: private: aoqi@0: // A work list used during flow analysis. aoqi@0: Block* _work_list; aoqi@0: aoqi@0: // List of blocks in reverse post order aoqi@0: Block* _rpo_list; aoqi@0: aoqi@0: // Next Block::_pre_order. After mapping, doubles as block_count. aoqi@0: int _next_pre_order; aoqi@0: aoqi@0: // Are there more blocks on the work list? aoqi@0: bool work_list_empty() { return _work_list == NULL; } aoqi@0: aoqi@0: // Get the next basic block from our work list. aoqi@0: Block* work_list_next(); aoqi@0: aoqi@0: // Add a basic block to our work list. aoqi@0: void add_to_work_list(Block* block); aoqi@0: aoqi@0: // Prepend a basic block to rpo list. aoqi@0: void prepend_to_rpo_list(Block* blk) { aoqi@0: blk->set_rpo_next(_rpo_list); aoqi@0: _rpo_list = blk; aoqi@0: } aoqi@0: aoqi@0: // Root of the loop tree aoqi@0: Loop* _loop_tree_root; aoqi@0: aoqi@0: // State used for make_jsr_record aoqi@0: int _jsr_count; aoqi@0: GrowableArray* _jsr_records; aoqi@0: aoqi@0: public: aoqi@0: // Make a JsrRecord for a given (entry, return) pair, if such a record aoqi@0: // does not already exist. aoqi@0: JsrRecord* make_jsr_record(int entry_address, int return_address); aoqi@0: aoqi@0: void set_loop_tree_root(Loop* ltr) { _loop_tree_root = ltr; } aoqi@0: Loop* loop_tree_root() { return _loop_tree_root; } aoqi@0: aoqi@0: private: aoqi@0: // Get the initial state for start_bci: aoqi@0: const StateVector* get_start_state(); aoqi@0: aoqi@0: // Merge the current state into all exceptional successors at the aoqi@0: // current point in the code. aoqi@0: void flow_exceptions(GrowableArray* exceptions, aoqi@0: GrowableArray* exc_klasses, aoqi@0: StateVector* state); aoqi@0: aoqi@0: // Merge the current state into all successors at the current point aoqi@0: // in the code. aoqi@0: void flow_successors(GrowableArray* successors, aoqi@0: StateVector* state); aoqi@0: aoqi@0: // Interpret the effects of the bytecodes on the incoming state aoqi@0: // vector of a basic block. Push the changed state to succeeding aoqi@0: // basic blocks. aoqi@0: void flow_block(Block* block, aoqi@0: StateVector* scratch_state, aoqi@0: JsrSet* scratch_jsrs); aoqi@0: aoqi@0: // Perform the type flow analysis, creating and cloning Blocks as aoqi@0: // necessary. aoqi@0: void flow_types(); aoqi@0: aoqi@0: // Perform the depth first type flow analysis. Helper for flow_types. aoqi@0: void df_flow_types(Block* start, aoqi@0: bool do_flow, aoqi@0: StateVector* temp_vector, aoqi@0: JsrSet* temp_set); aoqi@0: aoqi@0: // Incrementally build loop tree. aoqi@0: void build_loop_tree(Block* blk); aoqi@0: aoqi@0: // Create the block map, which indexes blocks in pre_order. aoqi@0: void map_blocks(); aoqi@0: aoqi@0: public: aoqi@0: // Perform type inference flow analysis. aoqi@0: void do_flow(); aoqi@0: aoqi@0: void print_on(outputStream* st) const PRODUCT_RETURN; aoqi@0: aoqi@0: void rpo_print_on(outputStream* st) const PRODUCT_RETURN; aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_CI_CITYPEFLOW_HPP