1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_OPTO_IDEALKIT_HPP
  26 #define SHARE_VM_OPTO_IDEALKIT_HPP
  27 
  28 #include "opto/addnode.hpp"
  29 #include "opto/cfgnode.hpp"
  30 #include "opto/connode.hpp"
  31 #include "opto/divnode.hpp"
  32 #include "opto/mulnode.hpp"
  33 #include "opto/phaseX.hpp"
  34 #include "opto/subnode.hpp"
  35 #include "opto/type.hpp"
  36 
  37 //-----------------------------------------------------------------------------
  38 //----------------------------IdealKit-----------------------------------------
  39 // Set of utilities for creating control flow and scalar SSA data flow.
  40 // Control:
  41 //    if_then(left, relop, right)
  42 //    else_ (optional)
  43 //    end_if
  44 //    loop(iv variable, initial, relop, limit)
  45 //       - sets iv to initial for first trip
  46 //       - exits when relation on limit is true
  47 //       - the values of initial and limit should be loop invariant
  48 //       - no increment, must be explicitly coded
  49 //       - final value of iv is available after end_loop (until dead())
  50 //    end_loop
  51 //    make_label(number of gotos)
  52 //    goto_(label)
  53 //    bind(label)
  54 // Data:
  55 //    ConI(integer constant)     - create an integer constant
  56 //    set(variable, value)       - assignment
  57 //    value(variable)            - reference value
  58 //    dead(variable)             - variable's value is no longer live
  59 //    increment(variable, value) - increment variable by value
  60 //    simple operations: AddI, SubI, AndI, LShiftI, etc.
  61 // Example:
  62 //    Node* limit = ??
  63 //    IdealVariable i(kit), j(kit);
  64 //    declarations_done();
  65 //    Node* exit = make_label(1); // 1 goto
  66 //    set(j, ConI(0));
  67 //    loop(i, ConI(0), BoolTest::lt, limit); {
  68 //       if_then(value(i), BoolTest::gt, ConI(5)) {
  69 //         set(j, ConI(1));
  70 //         goto_(exit); dead(i);
  71 //       } end_if();
  72 //       increment(i, ConI(1));
  73 //    } end_loop(); dead(i);
  74 //    bind(exit);
  75 //
  76 // See string_indexOf for a more complete example.
  77 
  78 class IdealKit;
  79 
  80 // Variable definition for IdealKit
  81 class IdealVariable: public StackObj {
  82  friend class IdealKit;
  83  private:
  84   int _id;
  85   void set_id(int id) { _id = id; }
  86  public:
  87   IdealVariable(IdealKit &k);
  88   int id() { assert(has_id(),"uninitialized id"); return _id; }
  89   bool has_id() { return _id >= 0; }
  90 };
  91 
  92 class IdealKit: public StackObj {
  93  friend class IdealVariable;
  94   // The main state (called a cvstate for Control and Variables)
  95   // contains both the current values of the variables and the
  96   // current set of predecessor control edges.  The variable values
  97   // are managed via a Node [in(1)..in(_var_ct)], and the predecessor
  98   // control edges managed via a RegionNode. The in(0) of the Node
  99   // for variables points to the RegionNode for the control edges.
 100  protected:
 101   Compile * const C;
 102   PhaseGVN &_gvn;
 103   GrowableArray<Node*>* _pending_cvstates; // stack of cvstates
 104   GrowableArray<Node*>* _delay_transform;  // delay invoking gvn.transform until drain
 105   Node* _cvstate;                          // current cvstate (control, memory and variables)
 106   uint _var_ct;                            // number of variables
 107   bool _delay_all_transforms;              // flag forcing all transforms to be delayed
 108   Node* _initial_ctrl;                     // saves initial control until variables declared
 109   Node* _initial_memory;                   // saves initial memory  until variables declared
 110 
 111   PhaseGVN& gvn() const { return _gvn; }
 112   // Create a new cvstate filled with nulls
 113   Node* new_cvstate();                     // Create a new cvstate
 114   Node* cvstate() { return _cvstate; }     // current cvstate
 115   Node* copy_cvstate();                    // copy current cvstate
 116 
 117   void set_memory(Node* mem, uint alias_idx );
 118   void do_memory_merge(Node* merging, Node* join);
 119   void clear(Node* m);                     // clear a cvstate
 120   void stop() { clear(_cvstate); }         // clear current cvstate
 121   Node* delay_transform(Node* n);
 122   Node* transform(Node* n);                // gvn.transform or push node on delay list
 123   Node* promote_to_phi(Node* n, Node* reg);// Promote "n" to a phi on region "reg"
 124   bool was_promoted_to_phi(Node* n, Node* reg) {
 125     return (n->is_Phi() && n->in(0) == reg);
 126   }
 127   void declare(IdealVariable* v) { v->set_id(_var_ct++); }
 128   // This declares the position where vars are kept in the cvstate
 129   // For some degree of consistency we use the TypeFunc enum to
 130   // soak up spots in the inputs even though we only use early Control
 131   // and Memory slots. (So far.)
 132   static const uint first_var; // = TypeFunc::Parms + 1;
 133 
 134 #ifdef ASSERT
 135   enum State { NullS=0, BlockS=1, LoopS=2, IfThenS=4, ElseS=8, EndifS= 16 };
 136   GrowableArray<int>* _state;
 137   State state() { return (State)(_state->top()); }
 138 #endif
 139 
 140   // Users should not care about slices only MergedMem so no access for them.
 141   Node* memory(uint alias_idx);
 142 
 143  public:
 144   IdealKit(PhaseGVN &gvn, Node* control, Node* memory, bool delay_all_transforms = false, bool has_declarations = false);
 145   ~IdealKit() {
 146     stop();
 147     drain_delay_transform();
 148   }
 149   // Control
 150   Node* ctrl()                          { return _cvstate->in(TypeFunc::Control); }
 151   void set_ctrl(Node* ctrl)             { _cvstate->set_req(TypeFunc::Control, ctrl); }
 152   Node* top()                           { return C->top(); }
 153   MergeMemNode* merged_memory()         { return _cvstate->in(TypeFunc::Memory)->as_MergeMem(); }
 154   void set_all_memory(Node* mem)        { _cvstate->set_req(TypeFunc::Memory, mem); }
 155   void set(IdealVariable& v, Node* rhs) { _cvstate->set_req(first_var + v.id(), rhs); }
 156   Node* value(IdealVariable& v)         { return _cvstate->in(first_var + v.id()); }
 157   void dead(IdealVariable& v)           { set(v, (Node*)NULL); }
 158   void if_then(Node* left, BoolTest::mask relop, Node* right,
 159                float prob = PROB_FAIR, float cnt = COUNT_UNKNOWN,
 160                bool push_new_state = true);
 161   void else_();
 162   void end_if();
 163   void loop(IdealVariable& iv, Node* init, BoolTest::mask cmp, Node* limit,
 164             float prob = PROB_LIKELY(0.9), float cnt = COUNT_UNKNOWN);
 165   void end_loop();
 166   Node* make_label(int goto_ct);
 167   void bind(Node* lab);
 168   void goto_(Node* lab, bool bind = false);
 169   void declarations_done();
 170   void drain_delay_transform();
 171 
 172   Node* IfTrue(IfNode* iff)  { return transform(new (C,1) IfTrueNode(iff)); }
 173   Node* IfFalse(IfNode* iff) { return transform(new (C,1) IfFalseNode(iff)); }
 174 
 175   // Data
 176   Node* ConI(jint k) { return (Node*)gvn().intcon(k); }
 177   Node* makecon(const Type *t)  const { return _gvn.makecon(t); }
 178 
 179   Node* AddI(Node* l, Node* r) { return transform(new (C,3) AddINode(l, r)); }
 180   Node* SubI(Node* l, Node* r) { return transform(new (C,3) SubINode(l, r)); }
 181   Node* AndI(Node* l, Node* r) { return transform(new (C,3) AndINode(l, r)); }
 182   Node* MaxI(Node* l, Node* r) { return transform(new (C,3) MaxINode(l, r)); }
 183   Node* LShiftI(Node* l, Node* r) { return transform(new (C,3) LShiftINode(l, r)); }
 184   Node* CmpI(Node* l, Node* r) { return transform(new (C,3) CmpINode(l, r)); }
 185   Node* Bool(Node* cmp, BoolTest::mask relop) { return transform(new (C,2) BoolNode(cmp, relop)); }
 186   void  increment(IdealVariable& v, Node* j)  { set(v, AddI(value(v), j)); }
 187   void  decrement(IdealVariable& v, Node* j)  { set(v, SubI(value(v), j)); }
 188 
 189   Node* CmpL(Node* l, Node* r) { return transform(new (C,3) CmpLNode(l, r)); }
 190 
 191   // TLS
 192   Node* thread()  {  return gvn().transform(new (C, 1) ThreadLocalNode()); }
 193 
 194   // Pointers
 195   Node* AddP(Node *base, Node *ptr, Node *off) { return transform(new (C,4) AddPNode(base, ptr, off)); }
 196   Node* CmpP(Node* l, Node* r) { return transform(new (C,3) CmpPNode(l, r)); }
 197 #ifdef _LP64
 198   Node* XorX(Node* l, Node* r) { return transform(new (C,3) XorLNode(l, r)); }
 199 #else // _LP64
 200   Node* XorX(Node* l, Node* r) { return transform(new (C,3) XorINode(l, r)); }
 201 #endif // _LP64
 202   Node* URShiftX(Node* l, Node* r) { return transform(new (C,3) URShiftXNode(l, r)); }
 203   Node* ConX(jint k) { return (Node*)gvn().MakeConX(k); }
 204   Node* CastPX(Node* ctl, Node* p) { return transform(new (C,2) CastP2XNode(ctl, p)); }
 205   // Add a fixed offset to a pointer
 206   Node* basic_plus_adr(Node* base, Node* ptr, intptr_t offset);
 207 
 208   // Memory operations
 209 
 210   // This is the base version which is given an alias index.
 211   Node* load(Node* ctl,
 212              Node* adr,
 213              const Type* t,
 214              BasicType bt,
 215              int adr_idx,
 216              bool require_atomic_access = false);
 217 
 218   // Return the new StoreXNode
 219   Node* store(Node* ctl,
 220               Node* adr,
 221               Node* val,
 222               BasicType bt,
 223               int adr_idx,
 224               bool require_atomic_access = false);
 225 
 226   // Store a card mark ordered after store_oop
 227   Node* storeCM(Node* ctl,
 228                 Node* adr,
 229                 Node* val,
 230                 Node* oop_store,
 231                 int oop_adr_idx,
 232                 BasicType bt,
 233                 int adr_idx);
 234 
 235   // Trivial call
 236   void make_leaf_call(const TypeFunc *slow_call_type,
 237                       address slow_call,
 238                       const char *leaf_name,
 239                       Node* parm0,
 240                       Node* parm1 = NULL,
 241                       Node* parm2 = NULL);
 242 };
 243 
 244 #endif // SHARE_VM_OPTO_IDEALKIT_HPP