1 /*
  2  * Copyright (c) 2016, 2020, 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_INLINETYPENODE_HPP
 26 #define SHARE_VM_OPTO_INLINETYPENODE_HPP
 27 
 28 #include "opto/connode.hpp"
 29 #include "opto/loopnode.hpp"
 30 #include "opto/node.hpp"
 31 
 32 class GraphKit;
 33 
 34 class InlineTypeBaseNode : public TypeNode {
 35 protected:
 36   InlineTypeBaseNode(const Type* t, int nb_fields)
 37     : TypeNode(t, nb_fields) {
 38     init_class_id(Class_InlineTypeBase);
 39     Compile::current()->add_inline_type(this);
 40   }
 41 
 42   enum { Control,   // Control input.
 43          Oop,       // Oop to heap allocated buffer (NULL if not buffered).
 44          IsInit,    // Needs to be checked for NULL before using the field values.
 45          Values     // Nodes corresponding to values of the inline type's fields.
 46                     // Nodes are connected in increasing order of the index of the field they correspond to.
 47   };
 48 
 49   virtual const TypeInstPtr* inline_ptr() const = 0;
 50   // Get the klass defining the field layout of the inline type
 51   ciInlineKlass* inline_klass() const { return type()->inline_klass(); }
 52 
 53   void make_scalar_in_safepoint(PhaseIterGVN* igvn, Unique_Node_List& worklist, SafePointNode* sfpt);
 54 
 55   const TypePtr* field_adr_type(Node* base, int offset, ciInstanceKlass* holder, DecoratorSet decorators, PhaseGVN& gvn) const;
 56 
 57   // Checks if the inline type fields are all set to default values
 58   bool is_default(PhaseGVN* gvn) const;
 59 
 60 public:
 61 
 62   // Returns the constant oop of the default inline type allocation
 63   static Node* default_oop(PhaseGVN& gvn, ciInlineKlass* vk);
 64 
 65   // Support for control flow merges
 66   bool has_phi_inputs(Node* region);
 67   InlineTypeBaseNode* clone_with_phis(PhaseGVN* gvn, Node* region, bool is_init = false);
 68   bool can_merge();
 69   InlineTypeBaseNode* merge_with(PhaseGVN* gvn, const InlineTypeBaseNode* other, int pnum, bool transform);
 70   void add_new_path(Node* region);
 71 
 72   // Get oop for heap allocated inline type (may be TypePtr::NULL_PTR)
 73   Node* get_oop() const    { return in(Oop); }
 74   void  set_oop(Node* oop) { set_req(Oop, oop); }
 75   Node* get_is_init() const { return in(IsInit); }
 76   void  set_is_init(PhaseGVN& gvn) { set_req(IsInit, gvn.intcon(1)); }
 77 
 78   // Inline type fields
 79   uint          field_count() const { return req() - Values; }
 80   Node*         field_value(uint index) const;
 81   Node*         field_value_by_offset(int offset, bool recursive = false) const;
 82   void      set_field_value(uint index, Node* value);
 83   void      set_field_value_by_offset(int offset, Node* value);
 84   int           field_offset(uint index) const;
 85   uint          field_index(int offset) const;
 86   ciType*       field_type(uint index) const;
 87   bool          field_is_flattened(uint index) const;
 88   bool          field_is_null_free(uint index) const;
 89 
 90   // Replace InlineTypeNodes in debug info at safepoints with SafePointScalarObjectNodes
 91   void make_scalar_in_safepoints(PhaseIterGVN* igvn, bool allow_oop = true);
 92 
 93   // Store the inline type as a flattened (headerless) representation
 94   void store_flattened(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder = NULL, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED) const;
 95   // Store the field values to memory
 96   void store(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED) const;
 97   // Initialize the inline type by loading its field values from memory
 98   void load(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED);
 99 
100   // Allocates the inline type (if not yet allocated)
101   InlineTypePtrNode* buffer(GraphKit* kit, bool safe_for_replace = true);
102   bool is_allocated(PhaseGVN* phase) const;
103   InlineTypePtrNode* as_ptr(PhaseGVN* phase) const;
104 
105   void replace_call_results(GraphKit* kit, Node* call, Compile* C);
106 
107   // Allocate all non-flattened inline type fields
108   Node* allocate_fields(GraphKit* kit);
109 
110   Node* tagged_klass(PhaseGVN& gvn) {
111     return tagged_klass(inline_klass(), gvn);
112   }
113   static Node* tagged_klass(ciInlineKlass* vk, PhaseGVN& gvn);
114   // Pass inline type as fields at a call or return
115   void pass_fields(GraphKit* kit, Node* n, uint& base_input);
116 
117   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
118 };
119 
120 //------------------------------InlineTypeNode-------------------------------------
121 // Node representing an inline type in C2 IR
122 class InlineTypeNode : public InlineTypeBaseNode {
123   friend class InlineTypeBaseNode;
124   friend class InlineTypePtrNode;
125 private:
126   InlineTypeNode(ciInlineKlass* vk, Node* oop)
127     : InlineTypeBaseNode(TypeInlineType::make(vk), Values + vk->nof_declared_nonstatic_fields()) {
128     init_class_id(Class_InlineType);
129     init_req(Oop, oop);
130   }
131 
132   // Checks if the inline type is loaded from memory and if so returns the oop
133   Node* is_loaded(PhaseGVN* phase, ciInlineKlass* vk = NULL, Node* base = NULL, int holder_offset = 0);
134 
135   const TypeInstPtr* inline_ptr() const { return TypeInstPtr::make(TypePtr::BotPTR, inline_klass()); }
136 
137 public:
138   // Create uninitialized
139   static InlineTypeNode* make_uninitialized(PhaseGVN& gvn, ciInlineKlass* vk);
140   // Create with default field values
141   static InlineTypeNode* make_default(PhaseGVN& gvn, ciInlineKlass* vk);
142   // Create with null field values
143   static InlineTypeNode* make_null(PhaseGVN& gvn, ciInlineKlass* vk);
144   // Create and initialize by loading the field values from an oop
145   static Node* make_from_oop(GraphKit* kit, Node* oop, ciInlineKlass* vk, bool null_free = true);
146   // Create and initialize by loading the field values from a flattened field or array
147   static InlineTypeNode* make_from_flattened(GraphKit* kit, ciInlineKlass* vk, Node* obj, Node* ptr, ciInstanceKlass* holder = NULL, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED);
148   // Create and initialize with the inputs or outputs of a MultiNode (method entry or call)
149   static InlineTypeNode* make_from_multi(GraphKit* kit, MultiNode* multi, ciInlineKlass* vk, uint& base_input, bool in);
150 
151   InlineTypeNode* make_larval(GraphKit* kit, bool allocate) const;
152   InlineTypeNode* finish_larval(GraphKit* kit) const;
153 
154   // Initialize the inline type fields with the inputs or outputs of a MultiNode
155   void initialize_fields(GraphKit* kit, MultiNode* multi, uint& base_input, bool in);
156 
157   // Allocation optimizations
158   void remove_redundant_allocations(PhaseIterGVN* igvn, PhaseIdealLoop* phase);
159 
160   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
161   virtual int Opcode() const;
162 };
163 
164 //------------------------------InlineTypePtrNode-------------------------------------
165 // Node representing an inline type as a pointer in C2 IR
166 class InlineTypePtrNode : public InlineTypeBaseNode {
167   friend class InlineTypeBaseNode;
168 private:
169   const TypeInstPtr* inline_ptr() const { return type()->isa_instptr(); }
170 
171 public:
172   InlineTypePtrNode(const InlineTypeBaseNode* vt, bool null_free = true)
173     : InlineTypeBaseNode(TypeInstPtr::make(null_free ? TypePtr::NotNull : TypePtr::BotPTR, vt->type()->inline_klass()), vt->req()) {
174     init_class_id(Class_InlineTypePtr);
175     init_req(Oop, vt->get_oop());
176     init_req(IsInit, vt->get_is_init());
177     for (uint i = Values; i < vt->req(); i++) {
178       init_req(i, vt->in(i));
179     }
180   }
181 
182   InlineTypePtrNode(ciInlineKlass* vk, Node* oop)
183       : InlineTypeBaseNode(TypeInstPtr::make(TypePtr::BotPTR, vk), Values + vk->nof_declared_nonstatic_fields()) {
184     init_class_id(Class_InlineTypePtr);
185     init_req(Oop, oop);
186   }
187 
188   static InlineTypePtrNode* make_null(PhaseGVN& gvn, ciInlineKlass* vk);
189 
190   virtual Node* Identity(PhaseGVN* phase);
191 
192   virtual const Type* Value(PhaseGVN* phase) const;
193 
194   virtual int Opcode() const;
195 };
196 
197 #endif // SHARE_VM_OPTO_INLINETYPENODE_HPP