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