duke@435: /* duke@435: * Copyright 2005-2006 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: //----------------------------------------------------------------------------- duke@435: //----------------------------IdealKit----------------------------------------- duke@435: // Set of utilities for creating control flow and scalar SSA data flow. duke@435: // Control: duke@435: // if_then(left, relop, right) duke@435: // else_ (optional) duke@435: // end_if duke@435: // loop(iv variable, initial, relop, limit) duke@435: // - sets iv to initial for first trip duke@435: // - exits when relation on limit is true duke@435: // - the values of initial and limit should be loop invariant duke@435: // - no increment, must be explicitly coded duke@435: // - final value of iv is available after end_loop (until dead()) duke@435: // end_loop duke@435: // make_label(number of gotos) duke@435: // goto_(label) duke@435: // bind(label) duke@435: // Data: duke@435: // ConI(integer constant) - create an integer constant duke@435: // set(variable, value) - assignment duke@435: // value(variable) - reference value duke@435: // dead(variable) - variable's value is no longer live duke@435: // increment(variable, value) - increment variable by value duke@435: // simple operations: AddI, SubI, AndI, LShiftI, etc. duke@435: // Example: duke@435: // Node* limit = ?? duke@435: // IdealVariable i(kit), j(kit); duke@435: // declares_done(); duke@435: // Node* exit = make_label(1); // 1 goto duke@435: // set(j, ConI(0)); duke@435: // loop(i, ConI(0), BoolTest::lt, limit); { duke@435: // if_then(value(i), BoolTest::gt, ConI(5)) { duke@435: // set(j, ConI(1)); duke@435: // goto_(exit); dead(i); duke@435: // } end_if(); duke@435: // increment(i, ConI(1)); duke@435: // } end_loop(); dead(i); duke@435: // bind(exit); duke@435: // duke@435: // See string_indexOf for a more complete example. duke@435: duke@435: class IdealKit; duke@435: duke@435: // Variable definition for IdealKit duke@435: class IdealVariable: public StackObj { duke@435: friend class IdealKit; duke@435: private: duke@435: int _id; duke@435: void set_id(int id) { _id = id; } duke@435: public: duke@435: IdealVariable(IdealKit &k); duke@435: int id() { assert(has_id(),"uninitialized id"); return _id; } duke@435: bool has_id() { return _id >= 0; } duke@435: }; duke@435: duke@435: class IdealKit: public StackObj { duke@435: friend class IdealVariable; duke@435: // The main state (called a cvstate for Control and Variables) duke@435: // contains both the current values of the variables and the duke@435: // current set of predecessor control edges. The variable values duke@435: // are managed via a Node [in(1)..in(_var_ct)], and the predecessor duke@435: // control edges managed via a RegionNode. The in(0) of the Node duke@435: // for variables points to the RegionNode for the control edges. duke@435: protected: duke@435: Compile * const C; duke@435: PhaseGVN &_gvn; duke@435: GrowableArray* _pending_cvstates; // stack of cvstates duke@435: GrowableArray* _delay_transform; // delay invoking gvn.transform until drain duke@435: Node* _cvstate; // current cvstate (control, memory and variables) duke@435: uint _var_ct; // number of variables duke@435: bool _delay_all_transforms; // flag forcing all transforms to be delayed duke@435: Node* _initial_ctrl; // saves initial control until variables declared duke@435: Node* _initial_memory; // saves initial memory until variables declared duke@435: duke@435: PhaseGVN& gvn() const { return _gvn; } duke@435: // Create a new cvstate filled with nulls duke@435: Node* new_cvstate(); // Create a new cvstate duke@435: Node* cvstate() { return _cvstate; } // current cvstate duke@435: Node* copy_cvstate(); // copy current cvstate duke@435: void set_ctrl(Node* ctrl) { _cvstate->set_req(TypeFunc::Control, ctrl); } duke@435: duke@435: // Should this assert this is a MergeMem??? duke@435: void set_all_memory(Node* mem){ _cvstate->set_req(TypeFunc::Memory, mem); } duke@435: void set_memory(Node* mem, uint alias_idx ); duke@435: void do_memory_merge(Node* merging, Node* join); duke@435: void clear(Node* m); // clear a cvstate duke@435: void stop() { clear(_cvstate); } // clear current cvstate duke@435: Node* delay_transform(Node* n); duke@435: Node* transform(Node* n); // gvn.transform or push node on delay list duke@435: Node* promote_to_phi(Node* n, Node* reg);// Promote "n" to a phi on region "reg" duke@435: bool was_promoted_to_phi(Node* n, Node* reg) { duke@435: return (n->is_Phi() && n->in(0) == reg); duke@435: } duke@435: void declare(IdealVariable* v) { v->set_id(_var_ct++); } duke@435: // This declares the position where vars are kept in the cvstate duke@435: // For some degree of consistency we use the TypeFunc enum to duke@435: // soak up spots in the inputs even though we only use early Control duke@435: // and Memory slots. (So far.) duke@435: static const uint first_var; // = TypeFunc::Parms + 1; duke@435: duke@435: #ifdef ASSERT duke@435: enum State { NullS=0, BlockS=1, LoopS=2, IfThenS=4, ElseS=8, EndifS= 16 }; duke@435: GrowableArray* _state; duke@435: State state() { return (State)(_state->top()); } duke@435: #endif duke@435: duke@435: // Users should not care about slices only MergedMem so no access for them. duke@435: Node* memory(uint alias_idx); duke@435: duke@435: public: duke@435: IdealKit(PhaseGVN &gvn, Node* control, Node* memory, bool delay_all_transforms = false); duke@435: ~IdealKit() { duke@435: stop(); duke@435: drain_delay_transform(); duke@435: } duke@435: // Control duke@435: Node* ctrl() { return _cvstate->in(TypeFunc::Control); } duke@435: Node* top() { return C->top(); } duke@435: MergeMemNode* merged_memory() { return _cvstate->in(TypeFunc::Memory)->as_MergeMem(); } duke@435: void set(IdealVariable& v, Node* rhs) { _cvstate->set_req(first_var + v.id(), rhs); } duke@435: Node* value(IdealVariable& v) { return _cvstate->in(first_var + v.id()); } duke@435: void dead(IdealVariable& v) { set(v, (Node*)NULL); } duke@435: void if_then(Node* left, BoolTest::mask relop, Node* right, duke@435: float prob = PROB_FAIR, float cnt = COUNT_UNKNOWN, duke@435: bool push_new_state = true); duke@435: void else_(); duke@435: void end_if(); duke@435: void loop(IdealVariable& iv, Node* init, BoolTest::mask cmp, Node* limit, duke@435: float prob = PROB_LIKELY(0.9), float cnt = COUNT_UNKNOWN); duke@435: void end_loop(); duke@435: Node* make_label(int goto_ct); duke@435: void bind(Node* lab); duke@435: void goto_(Node* lab, bool bind = false); duke@435: void declares_done(); duke@435: void drain_delay_transform(); duke@435: duke@435: Node* IfTrue(IfNode* iff) { return transform(new (C,1) IfTrueNode(iff)); } duke@435: Node* IfFalse(IfNode* iff) { return transform(new (C,1) IfFalseNode(iff)); } duke@435: duke@435: // Data duke@435: Node* ConI(jint k) { return (Node*)gvn().intcon(k); } duke@435: Node* makecon(const Type *t) const { return _gvn.makecon(t); } duke@435: duke@435: Node* AddI(Node* l, Node* r) { return transform(new (C,3) AddINode(l, r)); } duke@435: Node* SubI(Node* l, Node* r) { return transform(new (C,3) SubINode(l, r)); } duke@435: Node* AndI(Node* l, Node* r) { return transform(new (C,3) AndINode(l, r)); } duke@435: Node* MaxI(Node* l, Node* r) { return transform(new (C,3) MaxINode(l, r)); } duke@435: Node* LShiftI(Node* l, Node* r) { return transform(new (C,3) LShiftINode(l, r)); } duke@435: Node* CmpI(Node* l, Node* r) { return transform(new (C,3) CmpINode(l, r)); } duke@435: Node* Bool(Node* cmp, BoolTest::mask relop) { return transform(new (C,2) BoolNode(cmp, relop)); } duke@435: void increment(IdealVariable& v, Node* j) { set(v, AddI(value(v), j)); } duke@435: void decrement(IdealVariable& v, Node* j) { set(v, SubI(value(v), j)); } duke@435: duke@435: Node* CmpL(Node* l, Node* r) { return transform(new (C,3) CmpLNode(l, r)); } duke@435: duke@435: // TLS duke@435: Node* thread() { return gvn().transform(new (C, 1) ThreadLocalNode()); } duke@435: duke@435: // Pointers duke@435: Node* AddP(Node *base, Node *ptr, Node *off) { return transform(new (C,4) AddPNode(base, ptr, off)); } duke@435: Node* CmpP(Node* l, Node* r) { return transform(new (C,3) CmpPNode(l, r)); } duke@435: #ifdef _LP64 duke@435: Node* XorX(Node* l, Node* r) { return transform(new (C,3) XorLNode(l, r)); } duke@435: #else // _LP64 duke@435: Node* XorX(Node* l, Node* r) { return transform(new (C,3) XorINode(l, r)); } duke@435: #endif // _LP64 duke@435: Node* URShiftX(Node* l, Node* r) { return transform(new (C,3) URShiftXNode(l, r)); } duke@435: Node* ConX(jint k) { return (Node*)gvn().MakeConX(k); } duke@435: Node* CastPX(Node* ctl, Node* p) { return transform(new (C,2) CastP2XNode(ctl, p)); } duke@435: // Add a fixed offset to a pointer duke@435: Node* basic_plus_adr(Node* base, Node* ptr, intptr_t offset); duke@435: duke@435: // Memory operations duke@435: duke@435: // This is the base version which is given an alias index. duke@435: Node* load(Node* ctl, duke@435: Node* adr, duke@435: const Type* t, duke@435: BasicType bt, duke@435: int adr_idx, duke@435: bool require_atomic_access = false); duke@435: duke@435: // Return the new StoreXNode duke@435: Node* store(Node* ctl, duke@435: Node* adr, duke@435: Node* val, duke@435: BasicType bt, duke@435: int adr_idx, duke@435: bool require_atomic_access = false); duke@435: duke@435: // Store a card mark ordered after store_oop duke@435: Node* storeCM(Node* ctl, duke@435: Node* adr, duke@435: Node* val, duke@435: Node* oop_store, duke@435: BasicType bt, duke@435: int adr_idx); duke@435: duke@435: // Trivial call duke@435: void make_leaf_call(const TypeFunc *slow_call_type, duke@435: address slow_call, duke@435: const char *leaf_name, duke@435: Node* parm0, duke@435: Node* parm1 = NULL, duke@435: Node* parm2 = NULL); duke@435: };