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