1 /*
  2  * Copyright (c) 2016, 2024, 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 //------------------------------InlineTypeNode-------------------------------------
 35 // Node representing an inline type in C2 IR
 36 class InlineTypeNode : public TypeNode {
 37 protected:
 38   InlineTypeNode(ciInlineKlass* vk, Node* oop, bool null_free)
 39       : TypeNode(TypeInstPtr::make(null_free ? TypePtr::NotNull : TypePtr::BotPTR, vk), Values + vk->nof_declared_nonstatic_fields()) {
 40     init_class_id(Class_InlineType);
 41     init_req(Oop, oop);
 42     Compile::current()->add_inline_type(this);
 43     _is_larval = false;
 44   }
 45 
 46   enum { Control,    // Control input.
 47          Oop,        // Oop to heap allocated buffer.
 48          IsBuffered, // True if inline type is heap allocated (or nullptr), false otherwise.
 49          IsInit,     // Needs to be checked for nullptr before using the field values.
 50          Values      // Nodes corresponding to values of the inline type's fields.
 51                      // Nodes are connected in increasing order of the index of the field they correspond to.
 52   };
 53 
 54   bool _is_larval;
 55 
 56   virtual uint hash() const { return TypeNode::hash() + _is_larval; }
 57   // TODO 8325106 why can't we gvn larvals?
 58   virtual bool cmp(const Node &n) const { return TypeNode::cmp(n) && !((InlineTypeNode&)n)._is_larval && !_is_larval; }
 59   virtual uint size_of() const { return sizeof(*this); }
 60 
 61   // Get the klass defining the field layout of the inline type
 62   ciInlineKlass* inline_klass() const { return type()->inline_klass(); }
 63 
 64   void make_scalar_in_safepoint(PhaseIterGVN* igvn, Unique_Node_List& worklist, SafePointNode* sfpt);
 65 
 66   const TypePtr* field_adr_type(Node* base, int offset, ciInstanceKlass* holder, DecoratorSet decorators, PhaseGVN& gvn) const;
 67 
 68   // Checks if the inline type fields are all set to default values
 69   bool is_default(PhaseGVN* gvn) const;
 70 
 71   // Checks if the inline type oop is an allocated buffer with larval state
 72   bool is_larval(PhaseGVN* gvn) const;
 73 
 74   // Checks if the inline type is loaded from memory and if so returns the oop
 75   Node* is_loaded(PhaseGVN* phase, ciInlineKlass* vk = nullptr, Node* base = nullptr, int holder_offset = 0);
 76 
 77   // Initialize the inline type fields with the inputs or outputs of a MultiNode
 78   void initialize_fields(GraphKit* kit, MultiNode* multi, uint& base_input, bool in, bool null_free, Node* null_check_region, GrowableArray<ciType*>& visited);
 79 
 80   InlineTypeNode* adjust_scalarization_depth_impl(GraphKit* kit, GrowableArray<ciType*>& visited);
 81 
 82   static InlineTypeNode* make_default_impl(PhaseGVN& gvn, ciInlineKlass* vk, GrowableArray<ciType*>& visited, bool is_larval = false);
 83   static InlineTypeNode* make_from_oop_impl(GraphKit* kit, Node* oop, ciInlineKlass* vk, bool null_free, GrowableArray<ciType*>& visited, bool is_larval = false);
 84   static InlineTypeNode* make_null_impl(PhaseGVN& gvn, ciInlineKlass* vk, GrowableArray<ciType*>& visited, bool transform = true);
 85   static InlineTypeNode* make_from_flat_impl(GraphKit* kit, ciInlineKlass* vk, Node* obj, Node* ptr, ciInstanceKlass* holder, int holder_offset, DecoratorSet decorators, GrowableArray<ciType*>& visited);
 86 
 87 public:
 88   // Create with default field values
 89   static InlineTypeNode* make_default(PhaseGVN& gvn, ciInlineKlass* vk, bool is_larval = false);
 90   // Create uninitialized
 91   static InlineTypeNode* make_uninitialized(PhaseGVN& gvn, ciInlineKlass* vk, bool null_free = true);
 92   // Create and initialize by loading the field values from an oop
 93   static InlineTypeNode* make_from_oop(GraphKit* kit, Node* oop, ciInlineKlass* vk, bool null_free = true, bool is_larval = false);
 94   // Create and initialize by loading the field values from a flat field or array
 95   static InlineTypeNode* make_from_flat(GraphKit* kit, ciInlineKlass* vk, Node* obj, Node* ptr, ciInstanceKlass* holder = nullptr, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED);
 96   // Create and initialize with the inputs or outputs of a MultiNode (method entry or call)
 97   static InlineTypeNode* make_from_multi(GraphKit* kit, MultiNode* multi, ciInlineKlass* vk, uint& base_input, bool in, bool null_free = true);
 98   // Create with null field values
 99   static InlineTypeNode* make_null(PhaseGVN& gvn, ciInlineKlass* vk, bool transform = true);
100 
101   // Returns the constant oop of the default inline type allocation
102   static Node* default_oop(PhaseGVN& gvn, ciInlineKlass* vk);
103 
104   // Support for control flow merges
105   bool has_phi_inputs(Node* region);
106   InlineTypeNode* clone_with_phis(PhaseGVN* gvn, Node* region, SafePointNode* map = nullptr, bool is_init = false);
107   InlineTypeNode* merge_with(PhaseGVN* gvn, const InlineTypeNode* other, int pnum, bool transform);
108   void add_new_path(Node* region);
109 
110   // Get oop for heap allocated inline type (may be TypePtr::NULL_PTR)
111   Node* get_oop() const    { return in(Oop); }
112   void  set_oop(PhaseGVN& gvn, Node* oop) { set_req_X(Oop, oop, &gvn); }
113   Node* get_is_init() const { return in(IsInit); }
114   void  set_is_init(PhaseGVN& gvn, bool init = true) { set_req_X(IsInit, gvn.intcon(init ? 1 : 0), &gvn); }
115   Node* get_is_buffered() const { return in(IsBuffered); }
116   void  set_is_buffered(PhaseGVN& gvn, bool buffered = true) { set_req_X(IsBuffered, gvn.intcon(buffered ? 1 : 0), &gvn); }
117 
118   void set_is_larval(bool is_larval) { _is_larval = is_larval; }
119   bool is_larval() { return _is_larval; }
120 
121   // Inline type fields
122   uint          field_count() const { return req() - Values; }
123   Node*         field_value(uint index) const;
124   Node*         field_value_by_offset(int offset, bool recursive = false) const;
125   void      set_field_value(uint index, Node* value);
126   void      set_field_value_by_offset(int offset, Node* value);
127   int           field_offset(uint index) const;
128   uint          field_index(int offset) const;
129   ciType*       field_type(uint index) const;
130   bool          field_is_flat(uint index) const;
131   bool          field_is_null_free(uint index) const;
132 
133   // Replace InlineTypeNodes in debug info at safepoints with SafePointScalarObjectNodes
134   void make_scalar_in_safepoints(PhaseIterGVN* igvn, bool allow_oop = true);
135 
136   // Store the inline type as a flat (headerless) representation
137   void store_flat(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder = nullptr, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED) const;
138   // Store the field values to memory
139   void store(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED, int offset = -1) const;
140   // Initialize the inline type by loading its field values from memory
141   void load(GraphKit* kit, Node* base, Node* ptr, ciInstanceKlass* holder, GrowableArray<ciType*>& visited, int holder_offset = 0, DecoratorSet decorators = IN_HEAP | MO_UNORDERED);
142   // Make sure that inline type is fully scalarized
143   InlineTypeNode* adjust_scalarization_depth(GraphKit* kit);
144 
145   // Allocates the inline type (if not yet allocated)
146   InlineTypeNode* buffer(GraphKit* kit, bool safe_for_replace = true);
147   bool is_allocated(PhaseGVN* phase) const;
148 
149   void replace_call_results(GraphKit* kit, CallNode* call, Compile* C);
150 
151   // Allocate all non-flat inline type fields
152   Node* allocate_fields(GraphKit* kit);
153 
154   Node* tagged_klass(PhaseGVN& gvn) {
155     return tagged_klass(inline_klass(), gvn);
156   }
157   static Node* tagged_klass(ciInlineKlass* vk, PhaseGVN& gvn);
158   // Pass inline type as fields at a call or return
159   void pass_fields(GraphKit* kit, Node* n, uint& base_input, bool in, bool null_free = true);
160 
161   InlineTypeNode* make_larval(GraphKit* kit, bool allocate) const;
162   InlineTypeNode* finish_larval(GraphKit* kit) const;
163 
164   // Allocation optimizations
165   void remove_redundant_allocations(PhaseIdealLoop* phase);
166 
167   InlineTypeNode* clone_if_required(PhaseGVN* gvn, SafePointNode* map, bool safe_for_replace = true);
168 
169   virtual const Type* Value(PhaseGVN* phase) const;
170 
171   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
172 
173   virtual int Opcode() const;
174 };
175 
176 #endif // SHARE_VM_OPTO_INLINETYPENODE_HPP