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