1 /*
2  * Copyright (c) 2016, 2019, 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_OPTO_ARRAYCOPYNODE_HPP
26 #define SHARE_OPTO_ARRAYCOPYNODE_HPP
27 
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "opto/callnode.hpp"
30 
31 class GraphKit;
32 
33 class ArrayCopyNode : public CallNode {
34 private:
35 
36   // What kind of arraycopy variant is this?
37   enum {
38     None,            // not set yet
39     ArrayCopy,       // System.arraycopy()
40     CloneInst,       // A clone of instances
41     CloneArray,      // A clone of arrays that don't require a barrier
42                      // - depends on GC - some need to treat oop arrays separately
43     CloneOopArray,   // An oop array clone that requires GC barriers
44     CopyOf,          // Arrays.copyOf()
45     CopyOfRange      // Arrays.copyOfRange()
46   } _kind;
47 
48 #ifndef PRODUCT
49   static const char* _kind_names[CopyOfRange+1];
50 #endif
51   // Is the alloc obtained with
52   // AllocateArrayNode::Ideal_array_allocation() tightly coupled
53   // (arraycopy follows immediately the allocation)?
54   // We cache the result of LibraryCallKit::tightly_coupled_allocation
55   // here because it's much easier to find whether there's a tightly
56   // couple allocation at parse time than at macro expansion time. At
57   // macro expansion time, for every use of the allocation node we
58   // would need to figure out whether it happens after the arraycopy (and
59   // can be ignored) or between the allocation and the arraycopy. At
60   // parse time, it's straightforward because whatever happens after
61   // the arraycopy is not parsed yet so doesn't exist when
62   // LibraryCallKit::tightly_coupled_allocation() is called.
63   bool _alloc_tightly_coupled;
64   bool _has_negative_length_guard;
65 
66   bool _arguments_validated;
67 
arraycopy_type()68   static const TypeFunc* arraycopy_type() {
69     const Type** fields = TypeTuple::fields(ParmLimit - TypeFunc::Parms);
70     fields[Src]       = TypeInstPtr::BOTTOM;
71     fields[SrcPos]    = TypeInt::INT;
72     fields[Dest]      = TypeInstPtr::BOTTOM;
73     fields[DestPos]   = TypeInt::INT;
74     fields[Length]    = TypeInt::INT;
75     fields[SrcLen]    = TypeInt::INT;
76     fields[DestLen]   = TypeInt::INT;
77     fields[SrcKlass]  = TypeKlassPtr::BOTTOM;
78     fields[DestKlass] = TypeKlassPtr::BOTTOM;
79     const TypeTuple *domain = TypeTuple::make(ParmLimit, fields);
80 
81     // create result type (range)
82     fields = TypeTuple::fields(0);
83 
84     const TypeTuple *range = TypeTuple::make(TypeFunc::Parms+0, fields);
85 
86     return TypeFunc::make(domain, range);
87   }
88 
89   ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard);
90 
91   intptr_t get_length_if_constant(PhaseGVN *phase) const;
92   int get_count(PhaseGVN *phase) const;
93   static const TypePtr* get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n);
94 
95   Node* try_clone_instance(PhaseGVN *phase, bool can_reshape, int count);
96   bool prepare_array_copy(PhaseGVN *phase, bool can_reshape,
97                           Node*& adr_src, Node*& base_src, Node*& adr_dest, Node*& base_dest,
98                           BasicType& copy_type, const Type*& value_type, bool& disjoint_bases);
99   void array_copy_test_overlap(PhaseGVN *phase, bool can_reshape,
100                                bool disjoint_bases, int count,
101                                Node*& forward_ctl, Node*& backward_ctl);
102   Node* array_copy_forward(PhaseGVN *phase, bool can_reshape, Node*& ctl,
103                            Node* mem,
104                            const TypePtr* atp_src, const TypePtr* atp_dest,
105                            Node* adr_src, Node* base_src, Node* adr_dest, Node* base_dest,
106                            BasicType copy_type, const Type* value_type, int count);
107   Node* array_copy_backward(PhaseGVN *phase, bool can_reshape, Node*& ctl,
108                             Node* mem,
109                             const TypePtr* atp_src, const TypePtr* atp_dest,
110                             Node* adr_src, Node* base_src, Node* adr_dest, Node* base_dest,
111                             BasicType copy_type, const Type* value_type, int count);
112   bool finish_transform(PhaseGVN *phase, bool can_reshape,
113                         Node* ctl, Node *mem);
114   static bool may_modify_helper(const TypeOopPtr *t_oop, Node* n, PhaseTransform *phase, CallNode*& call);
115 public:
116   static Node* load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* addr, const TypePtr* adr_type, const Type *type, BasicType bt);
117 private:
118   void store(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* addr, const TypePtr* adr_type, Node* val, const Type *type, BasicType bt);
119 
120 public:
121 
122   enum {
123     Src   = TypeFunc::Parms,
124     SrcPos,
125     Dest,
126     DestPos,
127     Length,
128     SrcLen,
129     DestLen,
130     SrcKlass,
131     DestKlass,
132     ParmLimit
133   };
134 
135   // Results from escape analysis for non escaping inputs
136   const TypeOopPtr* _src_type;
137   const TypeOopPtr* _dest_type;
138 
139   static ArrayCopyNode* make(GraphKit* kit, bool may_throw,
140                              Node* src, Node* src_offset,
141                              Node* dest,  Node* dest_offset,
142                              Node* length,
143                              bool alloc_tightly_coupled,
144                              bool has_negative_length_guard,
145                              Node* src_klass = NULL, Node* dest_klass = NULL,
146                              Node* src_length = NULL, Node* dest_length = NULL);
147 
148   void connect_outputs(GraphKit* kit, bool deoptimize_on_exception = false);
149 
is_arraycopy() const150   bool is_arraycopy()             const  { assert(_kind != None, "should bet set"); return _kind == ArrayCopy; }
is_arraycopy_validated() const151   bool is_arraycopy_validated()   const  { assert(_kind != None, "should bet set"); return _kind == ArrayCopy && _arguments_validated; }
is_clone_inst() const152   bool is_clone_inst()            const  { assert(_kind != None, "should bet set"); return _kind == CloneInst; }
153   // is_clone_array - true for all arrays when using GCs that has no barriers
is_clone_array() const154   bool is_clone_array()           const  { assert(_kind != None, "should bet set"); return _kind == CloneArray; }
155   // is_clone_oop_array is used when oop arrays need GC barriers
is_clone_oop_array() const156   bool is_clone_oop_array()       const  { assert(_kind != None, "should bet set"); return _kind == CloneOopArray; }
157   // is_clonebasic - is true for any type of clone that doesn't need a writebarrier.
is_clonebasic() const158   bool is_clonebasic()            const  { assert(_kind != None, "should bet set"); return _kind == CloneInst || _kind == CloneArray; }
is_copyof() const159   bool is_copyof()                const  { assert(_kind != None, "should bet set"); return _kind == CopyOf; }
is_copyof_validated() const160   bool is_copyof_validated()      const  { assert(_kind != None, "should bet set"); return _kind == CopyOf && _arguments_validated; }
is_copyofrange() const161   bool is_copyofrange()           const  { assert(_kind != None, "should bet set"); return _kind == CopyOfRange; }
is_copyofrange_validated() const162   bool is_copyofrange_validated() const  { assert(_kind != None, "should bet set"); return _kind == CopyOfRange && _arguments_validated; }
163 
set_arraycopy(bool validated)164   void set_arraycopy(bool validated)   { assert(_kind == None, "shouldn't bet set yet"); _kind = ArrayCopy; _arguments_validated = validated; }
set_clone_inst()165   void set_clone_inst()                { assert(_kind == None, "shouldn't bet set yet"); _kind = CloneInst; }
set_clone_array()166   void set_clone_array()               { assert(_kind == None, "shouldn't bet set yet"); _kind = CloneArray; }
set_clone_oop_array()167   void set_clone_oop_array()           { assert(_kind == None, "shouldn't bet set yet"); _kind = CloneOopArray; }
set_copyof(bool validated)168   void set_copyof(bool validated)      { assert(_kind == None, "shouldn't bet set yet"); _kind = CopyOf; _arguments_validated = validated; }
set_copyofrange(bool validated)169   void set_copyofrange(bool validated) { assert(_kind == None, "shouldn't bet set yet"); _kind = CopyOfRange; _arguments_validated = validated; }
170 
171   virtual int Opcode() const;
172   virtual uint size_of() const; // Size is bigger
guaranteed_safepoint()173   virtual bool guaranteed_safepoint()  { return false; }
174   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
175 
176   virtual bool may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase);
177 
is_alloc_tightly_coupled() const178   bool is_alloc_tightly_coupled() const { return _alloc_tightly_coupled; }
179 
has_negative_length_guard() const180   bool has_negative_length_guard() const { return _has_negative_length_guard; }
181 
182   static bool may_modify(const TypeOopPtr *t_oop, MemBarNode* mb, PhaseTransform *phase, ArrayCopyNode*& ac);
183 
184   static int get_partial_inline_vector_lane_count(BasicType type, int const_len);
185 
186   bool modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const;
187 
188 #ifndef PRODUCT
189   virtual void dump_spec(outputStream *st) const;
190   virtual void dump_compact_spec(outputStream* st) const;
191 #endif
192 };
193 #endif // SHARE_OPTO_ARRAYCOPYNODE_HPP
194