1 /*
2  * Copyright (c) 2016, 2018, 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 #include "precompiled.hpp"
26 #include "gc/shared/barrierSet.hpp"
27 #include "gc/shared/c2/barrierSetC2.hpp"
28 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
29 #include "opto/arraycopynode.hpp"
30 #include "opto/graphKit.hpp"
31 #include "runtime/sharedRuntime.hpp"
32 #include "utilities/macros.hpp"
33 #if INCLUDE_SHENANDOAHGC
34 #include "gc/shenandoah/c2/shenandoahBarrierSetC2.hpp"
35 #endif
36 
ArrayCopyNode(Compile * C,bool alloc_tightly_coupled,bool has_negative_length_guard)37 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
38   : CallNode(arraycopy_type(), NULL, TypeRawPtr::BOTTOM),
39     _alloc_tightly_coupled(alloc_tightly_coupled),
40     _has_negative_length_guard(has_negative_length_guard),
41     _kind(None),
42     _arguments_validated(false),
43     _src_type(TypeOopPtr::BOTTOM),
44     _dest_type(TypeOopPtr::BOTTOM) {
45   init_class_id(Class_ArrayCopy);
46   init_flags(Flag_is_macro);
47   C->add_macro_node(this);
48 }
49 
size_of() const50 uint ArrayCopyNode::size_of() const { return sizeof(*this); }
51 
make(GraphKit * kit,bool may_throw,Node * src,Node * src_offset,Node * dest,Node * dest_offset,Node * length,bool alloc_tightly_coupled,bool has_negative_length_guard,Node * src_klass,Node * dest_klass,Node * src_length,Node * dest_length)52 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,
53                                    Node* src, Node* src_offset,
54                                    Node* dest, Node* dest_offset,
55                                    Node* length,
56                                    bool alloc_tightly_coupled,
57                                    bool has_negative_length_guard,
58                                    Node* src_klass, Node* dest_klass,
59                                    Node* src_length, Node* dest_length) {
60 
61   ArrayCopyNode* ac = new ArrayCopyNode(kit->C, alloc_tightly_coupled, has_negative_length_guard);
62   Node* prev_mem = kit->set_predefined_input_for_runtime_call(ac);
63 
64   ac->init_req(ArrayCopyNode::Src, src);
65   ac->init_req(ArrayCopyNode::SrcPos, src_offset);
66   ac->init_req(ArrayCopyNode::Dest, dest);
67   ac->init_req(ArrayCopyNode::DestPos, dest_offset);
68   ac->init_req(ArrayCopyNode::Length, length);
69   ac->init_req(ArrayCopyNode::SrcLen, src_length);
70   ac->init_req(ArrayCopyNode::DestLen, dest_length);
71   ac->init_req(ArrayCopyNode::SrcKlass, src_klass);
72   ac->init_req(ArrayCopyNode::DestKlass, dest_klass);
73 
74   if (may_throw) {
75     ac->set_req(TypeFunc::I_O , kit->i_o());
76     kit->add_safepoint_edges(ac, false);
77   }
78 
79   return ac;
80 }
81 
connect_outputs(GraphKit * kit,bool deoptimize_on_exception)82 void ArrayCopyNode::connect_outputs(GraphKit* kit, bool deoptimize_on_exception) {
83   kit->set_all_memory_call(this, true);
84   kit->set_control(kit->gvn().transform(new ProjNode(this,TypeFunc::Control)));
85   kit->set_i_o(kit->gvn().transform(new ProjNode(this, TypeFunc::I_O)));
86   kit->make_slow_call_ex(this, kit->env()->Throwable_klass(), true, deoptimize_on_exception);
87   kit->set_all_memory_call(this);
88 }
89 
90 #ifndef PRODUCT
91 const char* ArrayCopyNode::_kind_names[] = {"arraycopy", "arraycopy, validated arguments", "clone", "oop array clone", "CopyOf", "CopyOfRange"};
92 
dump_spec(outputStream * st) const93 void ArrayCopyNode::dump_spec(outputStream *st) const {
94   CallNode::dump_spec(st);
95   st->print(" (%s%s)", _kind_names[_kind], _alloc_tightly_coupled ? ", tightly coupled allocation" : "");
96 }
97 
dump_compact_spec(outputStream * st) const98 void ArrayCopyNode::dump_compact_spec(outputStream* st) const {
99   st->print("%s%s", _kind_names[_kind], _alloc_tightly_coupled ? ",tight" : "");
100 }
101 #endif
102 
get_length_if_constant(PhaseGVN * phase) const103 intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const {
104   // check that length is constant
105   Node* length = in(ArrayCopyNode::Length);
106   const Type* length_type = phase->type(length);
107 
108   if (length_type == Type::TOP) {
109     return -1;
110   }
111 
112   assert(is_clonebasic() || is_arraycopy() || is_copyof() || is_copyofrange(), "unexpected array copy type");
113 
114   return is_clonebasic() ? length->find_intptr_t_con(-1) : length->find_int_con(-1);
115 }
116 
get_count(PhaseGVN * phase) const117 int ArrayCopyNode::get_count(PhaseGVN *phase) const {
118   Node* src = in(ArrayCopyNode::Src);
119   const Type* src_type = phase->type(src);
120 
121   if (is_clonebasic()) {
122     if (src_type->isa_instptr()) {
123       const TypeInstPtr* inst_src = src_type->is_instptr();
124       ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
125       // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
126       // fields into account. They are rare anyway so easier to simply
127       // skip instances with injected fields.
128       if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
129         return -1;
130       }
131       int nb_fields = ik->nof_nonstatic_fields();
132       return nb_fields;
133     } else {
134       const TypeAryPtr* ary_src = src_type->isa_aryptr();
135       assert (ary_src != NULL, "not an array or instance?");
136       // clone passes a length as a rounded number of longs. If we're
137       // cloning an array we'll do it element by element. If the
138       // length input to ArrayCopyNode is constant, length of input
139       // array must be too.
140 
141       assert((get_length_if_constant(phase) == -1) == !ary_src->size()->is_con() ||
142              phase->is_IterGVN() || StressReflectiveCode, "inconsistent");
143 
144       if (ary_src->size()->is_con()) {
145         return ary_src->size()->get_con();
146       }
147       return -1;
148     }
149   }
150 
151   return get_length_if_constant(phase);
152 }
153 
try_clone_instance(PhaseGVN * phase,bool can_reshape,int count)154 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
155   if (!is_clonebasic()) {
156     return NULL;
157   }
158 
159   Node* src = in(ArrayCopyNode::Src);
160   Node* dest = in(ArrayCopyNode::Dest);
161   Node* ctl = in(TypeFunc::Control);
162   Node* in_mem = in(TypeFunc::Memory);
163 
164   const Type* src_type = phase->type(src);
165 
166   assert(src->is_AddP(), "should be base + off");
167   assert(dest->is_AddP(), "should be base + off");
168   Node* base_src = src->in(AddPNode::Base);
169   Node* base_dest = dest->in(AddPNode::Base);
170 
171   MergeMemNode* mem = MergeMemNode::make(in_mem);
172 
173   const TypeInstPtr* inst_src = src_type->isa_instptr();
174 
175   if (inst_src == NULL) {
176     return NULL;
177   }
178 
179   if (!inst_src->klass_is_exact()) {
180     ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
181     assert(!ik->is_interface() && !ik->has_subklass(), "inconsistent klass hierarchy");
182     phase->C->dependencies()->assert_leaf_type(ik);
183   }
184 
185   ciInstanceKlass* ik = inst_src->klass()->as_instance_klass();
186   assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");
187 
188   for (int i = 0; i < count; i++) {
189     ciField* field = ik->nonstatic_field_at(i);
190     int fieldidx = phase->C->alias_type(field)->index();
191     const TypePtr* adr_type = phase->C->alias_type(field)->adr_type();
192     Node* off = phase->MakeConX(field->offset());
193     Node* next_src = phase->transform(new AddPNode(base_src,base_src,off));
194     Node* next_dest = phase->transform(new AddPNode(base_dest,base_dest,off));
195     BasicType bt = field->layout_type();
196 
197     const Type *type;
198     if (bt == T_OBJECT) {
199       if (!field->type()->is_loaded()) {
200         type = TypeInstPtr::BOTTOM;
201       } else {
202         ciType* field_klass = field->type();
203         type = TypeOopPtr::make_from_klass(field_klass->as_klass());
204       }
205     } else {
206       type = Type::get_const_basic_type(bt);
207     }
208 
209     Node* v = LoadNode::make(*phase, ctl, mem->memory_at(fieldidx), next_src, adr_type, type, bt, MemNode::unordered);
210     v = phase->transform(v);
211 #if INCLUDE_SHENANDOAHGC
212     if (UseShenandoahGC && bt == T_OBJECT) {
213       v = ShenandoahBarrierSetC2::bsc2()->arraycopy_load_reference_barrier(phase, v);
214     }
215 #endif
216     Node* s = StoreNode::make(*phase, ctl, mem->memory_at(fieldidx), next_dest, adr_type, v, bt, MemNode::unordered);
217     s = phase->transform(s);
218     mem->set_memory_at(fieldidx, s);
219   }
220 
221   if (!finish_transform(phase, can_reshape, ctl, mem)) {
222     // Return NodeSentinel to indicate that the transform failed
223     return NodeSentinel;
224   }
225 
226   return mem;
227 }
228 
prepare_array_copy(PhaseGVN * phase,bool can_reshape,Node * & adr_src,Node * & base_src,Node * & adr_dest,Node * & base_dest,BasicType & copy_type,const Type * & value_type,bool & disjoint_bases)229 bool ArrayCopyNode::prepare_array_copy(PhaseGVN *phase, bool can_reshape,
230                                        Node*& adr_src,
231                                        Node*& base_src,
232                                        Node*& adr_dest,
233                                        Node*& base_dest,
234                                        BasicType& copy_type,
235                                        const Type*& value_type,
236                                        bool& disjoint_bases) {
237   Node* src = in(ArrayCopyNode::Src);
238   Node* dest = in(ArrayCopyNode::Dest);
239   const Type* src_type = phase->type(src);
240   const TypeAryPtr* ary_src = src_type->isa_aryptr();
241 
242   if (is_arraycopy() || is_copyofrange() || is_copyof()) {
243     const Type* dest_type = phase->type(dest);
244     const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
245     Node* src_offset = in(ArrayCopyNode::SrcPos);
246     Node* dest_offset = in(ArrayCopyNode::DestPos);
247 
248     // newly allocated object is guaranteed to not overlap with source object
249     disjoint_bases = is_alloc_tightly_coupled();
250 
251     if (ary_src  == NULL || ary_src->klass()  == NULL ||
252         ary_dest == NULL || ary_dest->klass() == NULL) {
253       // We don't know if arguments are arrays
254       return false;
255     }
256 
257     BasicType src_elem  = ary_src->klass()->as_array_klass()->element_type()->basic_type();
258     BasicType dest_elem = ary_dest->klass()->as_array_klass()->element_type()->basic_type();
259     if (src_elem  == T_ARRAY)  src_elem  = T_OBJECT;
260     if (dest_elem == T_ARRAY)  dest_elem = T_OBJECT;
261 
262     if (src_elem != dest_elem || dest_elem == T_VOID) {
263       // We don't know if arguments are arrays of the same type
264       return false;
265     }
266 
267     BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
268     if (dest_elem == T_OBJECT && (!is_alloc_tightly_coupled() ||
269                                   bs->array_copy_requires_gc_barriers(T_OBJECT))) {
270       // It's an object array copy but we can't emit the card marking
271       // that is needed
272       return false;
273     }
274 
275     value_type = ary_src->elem();
276 
277     base_src = src;
278     base_dest = dest;
279 
280     uint shift  = exact_log2(type2aelembytes(dest_elem));
281     uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
282 
283     adr_src = src;
284     adr_dest = dest;
285 
286     src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
287     dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
288 
289     Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
290     Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
291 
292     adr_src = phase->transform(new AddPNode(base_src, adr_src, src_scale));
293     adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, dest_scale));
294 
295     adr_src = new AddPNode(base_src, adr_src, phase->MakeConX(header));
296     adr_dest = new AddPNode(base_dest, adr_dest, phase->MakeConX(header));
297 
298     adr_src = phase->transform(adr_src);
299     adr_dest = phase->transform(adr_dest);
300 
301     copy_type = dest_elem;
302   } else {
303     assert(ary_src != NULL, "should be a clone");
304     assert(is_clonebasic(), "should be");
305 
306     disjoint_bases = true;
307     assert(src->is_AddP(), "should be base + off");
308     assert(dest->is_AddP(), "should be base + off");
309     adr_src = src;
310     base_src = src->in(AddPNode::Base);
311     adr_dest = dest;
312     base_dest = dest->in(AddPNode::Base);
313 
314     assert(phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con() == phase->type(dest->in(AddPNode::Offset))->is_intptr_t()->get_con(), "same start offset?");
315     BasicType elem = ary_src->klass()->as_array_klass()->element_type()->basic_type();
316     if (elem == T_ARRAY)  elem = T_OBJECT;
317 
318     int diff = arrayOopDesc::base_offset_in_bytes(elem) - phase->type(src->in(AddPNode::Offset))->is_intptr_t()->get_con();
319     assert(diff >= 0, "clone should not start after 1st array element");
320     if (diff > 0) {
321       adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
322       adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
323     }
324 
325     copy_type = elem;
326     value_type = ary_src->elem();
327   }
328   return true;
329 }
330 
get_address_type(PhaseGVN * phase,const TypePtr * atp,Node * n)331 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
332   if (atp == TypeOopPtr::BOTTOM) {
333     atp = phase->type(n)->isa_ptr();
334   }
335   // adjust atp to be the correct array element address type
336   return atp->add_offset(Type::OffsetBot);
337 }
338 
array_copy_test_overlap(PhaseGVN * phase,bool can_reshape,bool disjoint_bases,int count,Node * & forward_ctl,Node * & backward_ctl)339 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
340   Node* ctl = in(TypeFunc::Control);
341   if (!disjoint_bases && count > 1) {
342     Node* src_offset = in(ArrayCopyNode::SrcPos);
343     Node* dest_offset = in(ArrayCopyNode::DestPos);
344     assert(src_offset != NULL && dest_offset != NULL, "should be");
345     Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
346     Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
347     IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
348 
349     phase->transform(iff);
350 
351     forward_ctl = phase->transform(new IfFalseNode(iff));
352     backward_ctl = phase->transform(new IfTrueNode(iff));
353   } else {
354     forward_ctl = ctl;
355   }
356 }
357 
array_copy_forward(PhaseGVN * phase,bool can_reshape,Node * forward_ctl,Node * start_mem_src,Node * start_mem_dest,const TypePtr * atp_src,const TypePtr * atp_dest,Node * adr_src,Node * base_src,Node * adr_dest,Node * base_dest,BasicType copy_type,const Type * value_type,int count)358 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
359                                         bool can_reshape,
360                                         Node* forward_ctl,
361                                         Node* start_mem_src,
362                                         Node* start_mem_dest,
363                                         const TypePtr* atp_src,
364                                         const TypePtr* atp_dest,
365                                         Node* adr_src,
366                                         Node* base_src,
367                                         Node* adr_dest,
368                                         Node* base_dest,
369                                         BasicType copy_type,
370                                         const Type* value_type,
371                                         int count) {
372   Node* mem = phase->C->top();
373   if (!forward_ctl->is_top()) {
374     // copy forward
375     mem = start_mem_dest;
376     uint alias_idx_src = phase->C->get_alias_index(atp_src);
377     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
378     bool same_alias = (alias_idx_src == alias_idx_dest);
379 
380     if (count > 0) {
381       Node* v = LoadNode::make(*phase, forward_ctl, start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
382       v = phase->transform(v);
383 #if INCLUDE_SHENANDOAHGC
384       if (UseShenandoahGC && copy_type == T_OBJECT) {
385         v = ShenandoahBarrierSetC2::bsc2()->arraycopy_load_reference_barrier(phase, v);
386       }
387 #endif
388       mem = StoreNode::make(*phase, forward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
389       mem = phase->transform(mem);
390       for (int i = 1; i < count; i++) {
391         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
392         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
393         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
394         v = LoadNode::make(*phase, forward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
395         v = phase->transform(v);
396 #if INCLUDE_SHENANDOAHGC
397         if (UseShenandoahGC && copy_type == T_OBJECT) {
398           v = ShenandoahBarrierSetC2::bsc2()->arraycopy_load_reference_barrier(phase, v);
399         }
400 #endif
401         mem = StoreNode::make(*phase, forward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
402         mem = phase->transform(mem);
403       }
404     } else if(can_reshape) {
405       PhaseIterGVN* igvn = phase->is_IterGVN();
406       igvn->_worklist.push(adr_src);
407       igvn->_worklist.push(adr_dest);
408     }
409   }
410   return mem;
411 }
412 
array_copy_backward(PhaseGVN * phase,bool can_reshape,Node * backward_ctl,Node * start_mem_src,Node * start_mem_dest,const TypePtr * atp_src,const TypePtr * atp_dest,Node * adr_src,Node * base_src,Node * adr_dest,Node * base_dest,BasicType copy_type,const Type * value_type,int count)413 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
414                                          bool can_reshape,
415                                          Node* backward_ctl,
416                                          Node* start_mem_src,
417                                          Node* start_mem_dest,
418                                          const TypePtr* atp_src,
419                                          const TypePtr* atp_dest,
420                                          Node* adr_src,
421                                          Node* base_src,
422                                          Node* adr_dest,
423                                          Node* base_dest,
424                                          BasicType copy_type,
425                                          const Type* value_type,
426                                          int count) {
427   Node* mem = phase->C->top();
428   if (!backward_ctl->is_top()) {
429     // copy backward
430     mem = start_mem_dest;
431     uint alias_idx_src = phase->C->get_alias_index(atp_src);
432     uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
433     bool same_alias = (alias_idx_src == alias_idx_dest);
434 
435     if (count > 0) {
436       for (int i = count-1; i >= 1; i--) {
437         Node* off  = phase->MakeConX(type2aelembytes(copy_type) * i);
438         Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
439         Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
440         Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, next_src, atp_src, value_type, copy_type, MemNode::unordered);
441         v = phase->transform(v);
442 #if INCLUDE_SHENANDOAHGC
443         if (UseShenandoahGC && copy_type == T_OBJECT) {
444           v = ShenandoahBarrierSetC2::bsc2()->arraycopy_load_reference_barrier(phase, v);
445         }
446 #endif
447         mem = StoreNode::make(*phase, backward_ctl,mem,next_dest,atp_dest,v, copy_type, MemNode::unordered);
448         mem = phase->transform(mem);
449       }
450       Node* v = LoadNode::make(*phase, backward_ctl, same_alias ? mem : start_mem_src, adr_src, atp_src, value_type, copy_type, MemNode::unordered);
451       v = phase->transform(v);
452 #if INCLUDE_SHENANDOAHGC
453       if (UseShenandoahGC && copy_type == T_OBJECT) {
454         v = ShenandoahBarrierSetC2::bsc2()->arraycopy_load_reference_barrier(phase, v);
455       }
456 #endif
457       mem = StoreNode::make(*phase, backward_ctl, mem, adr_dest, atp_dest, v, copy_type, MemNode::unordered);
458       mem = phase->transform(mem);
459     } else if(can_reshape) {
460       PhaseIterGVN* igvn = phase->is_IterGVN();
461       igvn->_worklist.push(adr_src);
462       igvn->_worklist.push(adr_dest);
463     }
464   }
465   return mem;
466 }
467 
finish_transform(PhaseGVN * phase,bool can_reshape,Node * ctl,Node * mem)468 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
469                                      Node* ctl, Node *mem) {
470   if (can_reshape) {
471     PhaseIterGVN* igvn = phase->is_IterGVN();
472     igvn->set_delay_transform(false);
473     if (is_clonebasic()) {
474       Node* out_mem = proj_out(TypeFunc::Memory);
475 
476       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
477       if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
478           out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
479         assert(bs->array_copy_requires_gc_barriers(T_OBJECT), "can only happen with card marking");
480         return false;
481       }
482 
483       igvn->replace_node(out_mem->raw_out(0), mem);
484 
485       Node* out_ctl = proj_out(TypeFunc::Control);
486       igvn->replace_node(out_ctl, ctl);
487     } else {
488       // replace fallthrough projections of the ArrayCopyNode by the
489       // new memory, control and the input IO.
490       CallProjections callprojs;
491       extract_projections(&callprojs, true, false);
492 
493       if (callprojs.fallthrough_ioproj != NULL) {
494         igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
495       }
496       if (callprojs.fallthrough_memproj != NULL) {
497         igvn->replace_node(callprojs.fallthrough_memproj, mem);
498       }
499       if (callprojs.fallthrough_catchproj != NULL) {
500         igvn->replace_node(callprojs.fallthrough_catchproj, ctl);
501       }
502 
503       // The ArrayCopyNode is not disconnected. It still has the
504       // projections for the exception case. Replace current
505       // ArrayCopyNode with a dummy new one with a top() control so
506       // that this part of the graph stays consistent but is
507       // eventually removed.
508 
509       set_req(0, phase->C->top());
510       remove_dead_region(phase, can_reshape);
511     }
512   } else {
513     if (in(TypeFunc::Control) != ctl) {
514       // we can't return new memory and control from Ideal at parse time
515       assert(!is_clonebasic() SHENANDOAHGC_ONLY(|| UseShenandoahGC), "added control for clone?");
516       SHENANDOAHGC_ONLY(phase->record_for_igvn(this);)
517       return false;
518     }
519   }
520   return true;
521 }
522 
523 
Ideal(PhaseGVN * phase,bool can_reshape)524 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
525   if (remove_dead_region(phase, can_reshape))  return this;
526 
527   if (StressArrayCopyMacroNode && !can_reshape) {
528     phase->record_for_igvn(this);
529     return NULL;
530   }
531 
532   // See if it's a small array copy and we can inline it as
533   // loads/stores
534   // Here we can only do:
535   // - arraycopy if all arguments were validated before and we don't
536   // need card marking
537   // - clone for which we don't need to do card marking
538 
539   if (!is_clonebasic() && !is_arraycopy_validated() &&
540       !is_copyofrange_validated() && !is_copyof_validated()) {
541     return NULL;
542   }
543 
544   assert(in(TypeFunc::Control) != NULL &&
545          in(TypeFunc::Memory) != NULL &&
546          in(ArrayCopyNode::Src) != NULL &&
547          in(ArrayCopyNode::Dest) != NULL &&
548          in(ArrayCopyNode::Length) != NULL &&
549          ((in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::DestPos) != NULL) ||
550           is_clonebasic()), "broken inputs");
551 
552   if (in(TypeFunc::Control)->is_top() ||
553       in(TypeFunc::Memory)->is_top() ||
554       phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
555       phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
556       (in(ArrayCopyNode::SrcPos) != NULL && in(ArrayCopyNode::SrcPos)->is_top()) ||
557       (in(ArrayCopyNode::DestPos) != NULL && in(ArrayCopyNode::DestPos)->is_top())) {
558     return NULL;
559   }
560 
561   int count = get_count(phase);
562 
563   if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
564     return NULL;
565   }
566 
567   Node* mem = try_clone_instance(phase, can_reshape, count);
568   if (mem != NULL) {
569     return (mem == NodeSentinel) ? NULL : mem;
570   }
571 
572   Node* adr_src = NULL;
573   Node* base_src = NULL;
574   Node* adr_dest = NULL;
575   Node* base_dest = NULL;
576   BasicType copy_type = T_ILLEGAL;
577   const Type* value_type = NULL;
578   bool disjoint_bases = false;
579 
580   if (!prepare_array_copy(phase, can_reshape,
581                           adr_src, base_src, adr_dest, base_dest,
582                           copy_type, value_type, disjoint_bases)) {
583     return NULL;
584   }
585 
586   Node* src = in(ArrayCopyNode::Src);
587   Node* dest = in(ArrayCopyNode::Dest);
588   const TypePtr* atp_src = get_address_type(phase, _src_type, src);
589   const TypePtr* atp_dest = get_address_type(phase, _dest_type, dest);
590   uint alias_idx_src = phase->C->get_alias_index(atp_src);
591   uint alias_idx_dest = phase->C->get_alias_index(atp_dest);
592 
593   Node *in_mem = in(TypeFunc::Memory);
594   Node *start_mem_src = in_mem;
595   Node *start_mem_dest = in_mem;
596   if (in_mem->is_MergeMem()) {
597     start_mem_src = in_mem->as_MergeMem()->memory_at(alias_idx_src);
598     start_mem_dest = in_mem->as_MergeMem()->memory_at(alias_idx_dest);
599   }
600 
601 
602   if (can_reshape) {
603     assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
604     phase->is_IterGVN()->set_delay_transform(true);
605   }
606 
607   Node* backward_ctl = phase->C->top();
608   Node* forward_ctl = phase->C->top();
609   array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
610 
611   Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
612                                          start_mem_src, start_mem_dest,
613                                          atp_src, atp_dest,
614                                          adr_src, base_src, adr_dest, base_dest,
615                                          copy_type, value_type, count);
616 
617   Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
618                                            start_mem_src, start_mem_dest,
619                                            atp_src, atp_dest,
620                                            adr_src, base_src, adr_dest, base_dest,
621                                            copy_type, value_type, count);
622 
623   Node* ctl = NULL;
624   if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
625     ctl = new RegionNode(3);
626     mem = new PhiNode(ctl, Type::MEMORY, atp_dest);
627     ctl->init_req(1, forward_ctl);
628     mem->init_req(1, forward_mem);
629     ctl->init_req(2, backward_ctl);
630     mem->init_req(2, backward_mem);
631     ctl = phase->transform(ctl);
632     mem = phase->transform(mem);
633   } else if (!forward_ctl->is_top()) {
634     ctl = forward_ctl;
635     mem = forward_mem;
636   } else {
637     assert(!backward_ctl->is_top(), "no copy?");
638     ctl = backward_ctl;
639     mem = backward_mem;
640   }
641 
642   if (can_reshape) {
643     assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
644     phase->is_IterGVN()->set_delay_transform(false);
645   }
646 
647   MergeMemNode* out_mem = MergeMemNode::make(in_mem);
648   out_mem->set_memory_at(alias_idx_dest, mem);
649   mem = out_mem;
650 
651   if (!finish_transform(phase, can_reshape, ctl, mem)) {
652     return NULL;
653   }
654 
655   return mem;
656 }
657 
may_modify(const TypeOopPtr * t_oop,PhaseTransform * phase)658 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, PhaseTransform *phase) {
659   Node* dest = in(ArrayCopyNode::Dest);
660   if (dest->is_top()) {
661     return false;
662   }
663   const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
664   assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
665   assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
666          _src_type->is_known_instance(), "result of EA not recorded");
667 
668   if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
669     assert(_dest_type == TypeOopPtr::BOTTOM || _dest_type->is_known_instance(), "result of EA is known instance");
670     return t_oop->instance_id() == _dest_type->instance_id();
671   }
672 
673   return CallNode::may_modify_arraycopy_helper(dest_t, t_oop, phase);
674 }
675 
may_modify_helper(const TypeOopPtr * t_oop,Node * n,PhaseTransform * phase,CallNode * & call)676 bool ArrayCopyNode::may_modify_helper(const TypeOopPtr *t_oop, Node* n, PhaseTransform *phase, CallNode*& call) {
677   if (n != NULL &&
678       n->is_Call() &&
679       n->as_Call()->may_modify(t_oop, phase) &&
680       (n->as_Call()->is_ArrayCopy() || n->as_Call()->is_call_to_arraycopystub())) {
681     call = n->as_Call();
682     return true;
683   }
684   return false;
685 }
686 
may_modify(const TypeOopPtr * t_oop,MemBarNode * mb,PhaseTransform * phase,ArrayCopyNode * & ac)687 bool ArrayCopyNode::may_modify(const TypeOopPtr *t_oop, MemBarNode* mb, PhaseTransform *phase, ArrayCopyNode*& ac) {
688 
689   Node* c = mb->in(0);
690 
691   BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
692   // step over g1 gc barrier if we're at e.g. a clone with ReduceInitialCardMarks off
693   c = bs->step_over_gc_barrier(c);
694 
695   CallNode* call = NULL;
696   guarantee(c != NULL, "step_over_gc_barrier failed, there must be something to step to.");
697   if (c->is_Region()) {
698     for (uint i = 1; i < c->req(); i++) {
699       if (c->in(i) != NULL) {
700         Node* n = c->in(i)->in(0);
701         if (may_modify_helper(t_oop, n, phase, call)) {
702           ac = call->isa_ArrayCopy();
703           assert(c == mb->in(0), "only for clone");
704           return true;
705         }
706       }
707     }
708   } else if (may_modify_helper(t_oop, c->in(0), phase, call)) {
709     ac = call->isa_ArrayCopy();
710 #ifdef ASSERT
711     bool use_ReduceInitialCardMarks = BarrierSet::barrier_set()->is_a(BarrierSet::CardTableBarrierSet) &&
712       static_cast<CardTableBarrierSetC2*>(bs)->use_ReduceInitialCardMarks();
713     assert(c == mb->in(0) || (ac != NULL && ac->is_clonebasic() && !use_ReduceInitialCardMarks), "only for clone");
714 #endif
715     return true;
716   }
717 
718   return false;
719 }
720 
721 // Does this array copy modify offsets between offset_lo and offset_hi
722 // in the destination array
723 // if must_modify is false, return true if the copy could write
724 // between offset_lo and offset_hi
725 // if must_modify is true, return true if the copy is guaranteed to
726 // write between offset_lo and offset_hi
modifies(intptr_t offset_lo,intptr_t offset_hi,PhaseTransform * phase,bool must_modify) const727 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseTransform* phase, bool must_modify) const {
728   assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
729 
730   Node* dest = in(Dest);
731   Node* dest_pos = in(DestPos);
732   Node* len = in(Length);
733 
734   const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
735   const TypeInt *len_t = phase->type(len)->isa_int();
736   const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
737 
738   if (dest_pos_t == NULL || len_t == NULL || ary_t == NULL) {
739     return !must_modify;
740   }
741 
742   BasicType ary_elem = ary_t->klass()->as_array_klass()->element_type()->basic_type();
743   uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
744   uint elemsize = type2aelembytes(ary_elem);
745 
746   jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
747   jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
748   jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
749   jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
750 
751   if (must_modify) {
752     if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
753       return true;
754     }
755   } else {
756     if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
757       return true;
758     }
759   }
760   return false;
761 }
762