1 /*
   2  * Copyright (c) 2016, 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_VALUETYPENODE_HPP
  26 #define SHARE_VM_OPTO_VALUETYPENODE_HPP
  27 
  28 #include "opto/node.hpp"
  29 #include "opto/connode.hpp"
  30 
  31 class GraphKit;
  32 
  33 class ValueTypeBaseNode : public TypeNode {
  34 protected:
  35   ValueTypeBaseNode(const Type* t, int nb_fields)
  36     : TypeNode(t, nb_fields) {
  37     init_class_id(Class_ValueTypeBase);
  38     Compile::current()->add_value_type(this);
  39   }
  40 
  41   enum { Control,   // Control input
  42          Oop,       // Oop of TypeValueTypePtr
  43          Values     // Nodes corresponding to values of the value type's fields.
  44                     // Nodes are connected in increasing order of the index of the field they correspond to.
  45   };
  46 
  47   virtual const TypeValueTypePtr* value_type_ptr() const = 0;
  48   // Get the klass defining the field layout of the value type
  49   virtual ciValueKlass* value_klass() const = 0;
  50 
  51   int make_scalar_in_safepoint(Unique_Node_List& worklist, SafePointNode* sfpt, Node* root, PhaseGVN* gvn);
  52 
  53   // Initialize the value type fields with the inputs or outputs of a MultiNode
  54   void initialize(PhaseGVN* gvn, Node*& ctl, Node* mem, MultiNode* multi, ciValueKlass* vk, int base_offset = 0, int base_input = TypeFunc::Parms+1, bool in = false);
  55 
  56   const TypePtr* field_adr_type(Node* base, int offset, ciInstanceKlass* holder, PhaseGVN& gvn) const;
  57 
  58 public:
  59   // Support for control flow merges
  60   bool has_phi_inputs(Node* region);
  61   ValueTypeBaseNode* clone_with_phis(PhaseGVN* gvn, Node* region);
  62   ValueTypeBaseNode* merge_with(PhaseGVN* gvn, const ValueTypeBaseNode* other, int pnum, bool transform);
  63 
  64   // Get oop for heap allocated value type (may be TypePtr::NULL_PTR)
  65   Node* get_oop() const    { return in(Oop); }
  66   void  set_oop(Node* oop) { set_req(Oop, oop); }
  67 
  68   // Value type fields
  69   uint          field_count() const { return req() - Values; }
  70   Node*         field_value(uint index) const;
  71   Node*         field_value_by_offset(int offset, bool recursive = false) const;
  72   void      set_field_value(uint index, Node* value);
  73   int           field_offset(uint index) const;
  74   ciType*       field_type(uint index) const;
  75   bool          field_is_flattened(uint index) const;
  76 
  77   // Replace ValueTypeNodes in debug info at safepoints with SafePointScalarObjectNodes
  78   void make_scalar_in_safepoints(Node* root, PhaseGVN* gvn);
  79 
  80   // Store the value type as a flattened (headerless) representation
  81   void store_flattened(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder = NULL, int holder_offset = 0) const;
  82   // Store the field values to memory
  83   void store(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset = 0) const;
  84   // Initialize the value type by loading its field values from memory
  85   void load(PhaseGVN& gvn, Node*& ctl, Node* mem, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset = 0);
  86 
  87   // Allocates the value type (if not yet allocated)
  88   ValueTypeBaseNode* allocate(GraphKit* kit);
  89   bool is_allocated(PhaseGVN* phase) const;
  90 
  91   void replace_call_results(GraphKit* kit, Node* call, Compile* C);
  92 };
  93 
  94 //------------------------------ValueTypeNode-------------------------------------
  95 // Node representing a value type in C2 IR
  96 class ValueTypeNode : public ValueTypeBaseNode {
  97   friend class ValueTypeBaseNode;
  98   friend class ValueTypePtrNode;
  99 private:
 100   ValueTypeNode(const TypeValueType* t, Node* oop)
 101     : ValueTypeBaseNode(t, Values + t->value_klass()->nof_declared_nonstatic_fields()) {
 102     init_class_id(Class_ValueType);
 103     init_req(Oop, oop);
 104   }
 105 
 106   // Checks if the value type is loaded from memory and if so returns the oop
 107   Node* is_loaded(PhaseGVN* phase, ciValueKlass* vk = NULL, Node* base = NULL, int holder_offset = 0);
 108 
 109   // Checks if the value type fields are all set to default values
 110   bool is_default(PhaseGVN& gvn) const;
 111 
 112   const TypeValueTypePtr* value_type_ptr() const { return TypeValueTypePtr::make(TypePtr::BotPTR, value_klass()); }
 113   ciValueKlass* value_klass() const { return type()->is_valuetype()->value_klass(); }
 114 
 115 public:
 116   // Create uninitialized
 117   static ValueTypeNode* make_uninitialized(PhaseGVN& gvn, ciValueKlass* klass);
 118   // Create with default field values
 119   static ValueTypeNode* make_default(PhaseGVN& gvn, ciValueKlass* vk);
 120   // Create and initialize by loading the field values from an oop
 121   static ValueTypeNode* make_from_oop(GraphKit* kit, Node* oop, ciValueKlass* vk, bool null_check = false, bool buffer_check = false);
 122   static ValueTypeNode* make_from_oop(PhaseGVN& gvn, Node*& ctl, Node* mem, Node* oop, ciValueKlass* vk, bool null_check = false, bool buffer_check = false);
 123   // Create and initialize by loading the field values from a flattened field or array
 124   static ValueTypeNode* make_from_flattened(GraphKit* kit, ciValueKlass* vk, Node* obj, Node* ptr, ciInstanceKlass* holder = NULL, int holder_offset = 0);
 125   static ValueTypeNode* make_from_flattened(PhaseGVN& gvn, ciValueKlass* vk, Node*& ctl, Node* mem, Node* obj, Node* ptr, ciInstanceKlass* holder = NULL, int holder_offset = 0);
 126   // Create and initialize with the inputs or outputs of a MultiNode (method entry or call)
 127   static ValueTypeNode* make_from_multi(PhaseGVN& gvn, Node*& ctl, Node* mem, MultiNode* multi, ciValueKlass* vk, int base_input, bool in);
 128 
 129   // Load the default oop from the java mirror of the given ValueKlass
 130   static Node* load_default_oop(PhaseGVN& gvn, ciValueKlass* vk);
 131 
 132   // Allocate all non-flattened value type fields
 133   Node* allocate_fields(GraphKit* kit);
 134 
 135   Node* tagged_klass(PhaseGVN& gvn);
 136   void pass_klass(Node* n, uint pos, const GraphKit& kit);
 137   uint pass_fields(Node* call, int base_input, GraphKit& kit, bool assert_allocated = false, ciValueKlass* base_vk = NULL, int base_offset = 0);
 138 
 139   // Allocation optimizations
 140   void remove_redundant_allocations(PhaseIterGVN* igvn, PhaseIdealLoop* phase);
 141 
 142   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
 143   virtual int Opcode() const;
 144 };
 145 
 146 //------------------------------ValueTypePtrNode-------------------------------------
 147 // Node representing a value type as a pointer in C2 IR
 148 class ValueTypePtrNode : public ValueTypeBaseNode {
 149 private:
 150   const TypeValueTypePtr* value_type_ptr() const { return type()->isa_valuetypeptr(); }
 151   ciValueKlass* value_klass() const { return value_type_ptr()->value_klass(); }
 152 
 153   ValueTypePtrNode(ciValueKlass* vk, Node* oop)
 154     : ValueTypeBaseNode(TypeValueTypePtr::make(TypePtr::NotNull, vk), Values + vk->nof_declared_nonstatic_fields()) {
 155     init_class_id(Class_ValueTypePtr);
 156     init_req(Oop, oop);
 157   }
 158 
 159 public:
 160   // Create and initialize with the values of a ValueTypeNode
 161   static ValueTypePtrNode* make_from_value_type(PhaseGVN& gvn, ValueTypeNode* vt);
 162   // Create and initialize with the result values of a call
 163   static ValueTypePtrNode* make_from_call(GraphKit* kit, ciValueKlass* vk, CallNode* call);
 164   // Create and initialize by loading the field values from an oop
 165   static ValueTypePtrNode* make_from_oop(PhaseGVN& gvn, Node*& ctl, Node* mem, Node* oop);
 166 
 167   virtual int Opcode() const;
 168 };
 169 
 170 #endif // SHARE_VM_OPTO_VALUETYPENODE_HPP