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