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 "gc/shared/gc_globals.hpp"
30 #include "opto/arraycopynode.hpp"
31 #include "opto/graphKit.hpp"
32 #include "runtime/sharedRuntime.hpp"
33 #include "utilities/macros.hpp"
34 #include "utilities/powerOfTwo.hpp"
35
36 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
37 : CallNode(arraycopy_type(), nullptr, TypePtr::BOTTOM),
38 _kind(None),
39 _alloc_tightly_coupled(alloc_tightly_coupled),
40 _has_negative_length_guard(has_negative_length_guard),
41 _arguments_validated(false),
42 _src_type(TypeOopPtr::BOTTOM),
43 _dest_type(TypeOopPtr::BOTTOM) {
44 init_class_id(Class_ArrayCopy);
45 init_flags(Flag_is_macro);
46 C->add_macro_node(this);
47 }
48
49 uint ArrayCopyNode::size_of() const { return sizeof(*this); }
50
51 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,
97 void ArrayCopyNode::dump_compact_spec(outputStream* st) const {
98 st->print("%s%s", _kind_names[_kind], _alloc_tightly_coupled ? ",tight" : "");
99 }
100 #endif
101
102 intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const {
103 // check that length is constant
104 Node* length = in(ArrayCopyNode::Length);
105 const Type* length_type = phase->type(length);
106
107 if (length_type == Type::TOP) {
108 return -1;
109 }
110
111 assert(is_clonebasic() || is_arraycopy() || is_copyof() || is_copyofrange(), "unexpected array copy type");
112
113 return is_clonebasic() ? length->find_intptr_t_con(-1) : length->find_int_con(-1);
114 }
115
116 int ArrayCopyNode::get_count(PhaseGVN *phase) const {
117 Node* src = in(ArrayCopyNode::Src);
118 const Type* src_type = phase->type(src);
119
120 if (is_clonebasic()) {
121 if (src_type->isa_instptr()) {
122 const TypeInstPtr* inst_src = src_type->is_instptr();
123 ciInstanceKlass* ik = inst_src->instance_klass();
124 // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
125 // fields into account. They are rare anyway so easier to simply
126 // skip instances with injected fields.
127 if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
128 return -1;
129 }
130 int nb_fields = ik->nof_nonstatic_fields();
131 return nb_fields;
132 } else {
133 const TypeAryPtr* ary_src = src_type->isa_aryptr();
134 assert (ary_src != nullptr, "not an array or instance?");
135 // clone passes a length as a rounded number of longs. If we're
136 // cloning an array we'll do it element by element. If the
137 // length of the input array is constant, ArrayCopyNode::Length
138 // must be too. Note that the opposite does not need to hold,
139 // because different input array lengths (e.g. int arrays with
140 // 3 or 4 elements) might lead to the same length input
141 // (e.g. 2 double-words).
142 assert(!ary_src->size()->is_con() || (get_length_if_constant(phase) >= 0) ||
143 phase->is_IterGVN() || phase->C->inlining_incrementally() || StressReflectiveCode, "inconsistent");
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
154 Node* ArrayCopyNode::load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, const Type *type, BasicType bt) {
155 DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY;
156 C2AccessValuePtr addr(adr, adr_type);
157 C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
158 Node* res = bs->load_at(access, type);
159 ctl = access.ctl();
160 return res;
161 }
162
173 }
174
175
176 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
177 if (!is_clonebasic()) {
178 return nullptr;
179 }
180
181 Node* base_src = in(ArrayCopyNode::Src);
182 Node* base_dest = in(ArrayCopyNode::Dest);
183 Node* ctl = in(TypeFunc::Control);
184 Node* in_mem = in(TypeFunc::Memory);
185
186 const Type* src_type = phase->type(base_src);
187 const TypeInstPtr* inst_src = src_type->isa_instptr();
188 if (inst_src == nullptr) {
189 return nullptr;
190 }
191
192 MergeMemNode* mem = phase->transform(MergeMemNode::make(in_mem))->as_MergeMem();
193 if (can_reshape) {
194 phase->is_IterGVN()->_worklist.push(mem);
195 }
196
197
198 ciInstanceKlass* ik = inst_src->instance_klass();
199
200 if (!inst_src->klass_is_exact()) {
201 assert(!ik->is_interface(), "inconsistent klass hierarchy");
202 if (ik->has_subklass()) {
203 // Concurrent class loading.
204 // Fail fast and return NodeSentinel to indicate that the transform failed.
205 return NodeSentinel;
206 } else {
207 phase->C->dependencies()->assert_leaf_type(ik);
208 }
209 }
210
211 assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");
212
259 Node* src_offset = in(ArrayCopyNode::SrcPos);
260 Node* dest_offset = in(ArrayCopyNode::DestPos);
261
262 if (is_arraycopy() || is_copyofrange() || is_copyof()) {
263 const Type* dest_type = phase->type(base_dest);
264 const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
265
266 // newly allocated object is guaranteed to not overlap with source object
267 disjoint_bases = is_alloc_tightly_coupled();
268 if (ary_src == nullptr || ary_src->elem() == Type::BOTTOM ||
269 ary_dest == nullptr || ary_dest->elem() == Type::BOTTOM) {
270 // We don't know if arguments are arrays
271 return false;
272 }
273
274 BasicType src_elem = ary_src->elem()->array_element_basic_type();
275 BasicType dest_elem = ary_dest->elem()->array_element_basic_type();
276 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
277 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
278
279 if (src_elem != dest_elem || dest_elem == T_VOID) {
280 // We don't know if arguments are arrays of the same type
281 return false;
282 }
283
284 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
285 if (bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) {
286 // It's an object array copy but we can't emit the card marking
287 // that is needed
288 return false;
289 }
290
291 value_type = ary_src->elem();
292
293 uint shift = exact_log2(type2aelembytes(dest_elem));
294 uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
295
296 src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
297 if (src_offset->is_top()) {
298 // Offset is out of bounds (the ArrayCopyNode will be removed)
299 return false;
300 }
301 dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
302 if (dest_offset->is_top()) {
303 // Offset is out of bounds (the ArrayCopyNode will be removed)
304 if (can_reshape) {
305 // record src_offset, so it can be deleted later (if it is dead)
306 phase->is_IterGVN()->_worklist.push(src_offset);
307 }
308 return false;
309 }
310
311 Node* hook = new Node(1);
312 hook->init_req(0, dest_offset);
313
314 Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
315
316 hook->destruct(phase);
317
318 Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
319
320 adr_src = phase->transform(new AddPNode(base_src, base_src, src_scale));
321 adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_scale));
322
323 adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(header)));
324 adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(header)));
325
326 copy_type = dest_elem;
327 } else {
328 assert(ary_src != nullptr, "should be a clone");
329 assert(is_clonebasic(), "should be");
330
331 disjoint_bases = true;
332
333 BasicType elem = ary_src->isa_aryptr()->elem()->array_element_basic_type();
334 if (is_reference_type(elem, true)) {
335 elem = T_OBJECT;
336 }
337
338 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
339 if (bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) {
340 return false;
341 }
342
343 adr_src = phase->transform(new AddPNode(base_src, base_src, src_offset));
344 adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_offset));
345
346 // The address is offsetted to an aligned address where a raw copy would start.
347 // If the clone copy is decomposed into load-stores - the address is adjusted to
348 // point at where the array starts.
349 const Type* toff = phase->type(src_offset);
350 int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
351 int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
352 assert(diff >= 0, "clone should not start after 1st array element");
353 if (diff > 0) {
354 adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
355 adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
356 }
357 copy_type = elem;
358 value_type = ary_src->elem();
359 }
360 return true;
361 }
362
363 const TypePtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
364 if (atp == TypeOopPtr::BOTTOM) {
365 atp = phase->type(n)->isa_ptr();
366 }
367 // adjust atp to be the correct array element address type
368 return atp->add_offset(Type::OffsetBot);
369 }
370
371 void ArrayCopyNode::array_copy_test_overlap(PhaseGVN *phase, bool can_reshape, bool disjoint_bases, int count, Node*& forward_ctl, Node*& backward_ctl) {
372 Node* ctl = in(TypeFunc::Control);
373 if (!disjoint_bases && count > 1) {
374 Node* src_offset = in(ArrayCopyNode::SrcPos);
375 Node* dest_offset = in(ArrayCopyNode::DestPos);
376 assert(src_offset != nullptr && dest_offset != nullptr, "should be");
377 Node* cmp = phase->transform(new CmpINode(src_offset, dest_offset));
378 Node *bol = phase->transform(new BoolNode(cmp, BoolTest::lt));
379 IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
380
381 phase->transform(iff);
382
383 forward_ctl = phase->transform(new IfFalseNode(iff));
384 backward_ctl = phase->transform(new IfTrueNode(iff));
385 } else {
386 forward_ctl = ctl;
387 }
388 }
389
390 Node* ArrayCopyNode::array_copy_forward(PhaseGVN *phase,
391 bool can_reshape,
392 Node*& forward_ctl,
393 Node* mem,
394 const TypePtr* atp_src,
395 const TypePtr* atp_dest,
396 Node* adr_src,
397 Node* base_src,
398 Node* adr_dest,
399 Node* base_dest,
400 BasicType copy_type,
401 const Type* value_type,
402 int count) {
403 if (!forward_ctl->is_top()) {
404 // copy forward
405 MergeMemNode* mm = MergeMemNode::make(mem);
406
407 if (count > 0) {
408 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
409 Node* v = load(bs, phase, forward_ctl, mm, adr_src, atp_src, value_type, copy_type);
410 store(bs, phase, forward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
411 for (int i = 1; i < count; i++) {
412 Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);
413 Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
414 Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
415 v = load(bs, phase, forward_ctl, mm, next_src, atp_src, value_type, copy_type);
416 store(bs, phase, forward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
417 }
418 } else if (can_reshape) {
419 PhaseIterGVN* igvn = phase->is_IterGVN();
420 igvn->_worklist.push(adr_src);
421 igvn->_worklist.push(adr_dest);
422 }
423 return mm;
424 }
425 return phase->C->top();
426 }
427
428 Node* ArrayCopyNode::array_copy_backward(PhaseGVN *phase,
429 bool can_reshape,
430 Node*& backward_ctl,
431 Node* mem,
432 const TypePtr* atp_src,
433 const TypePtr* atp_dest,
434 Node* adr_src,
435 Node* base_src,
436 Node* adr_dest,
437 Node* base_dest,
438 BasicType copy_type,
439 const Type* value_type,
440 int count) {
441 if (!backward_ctl->is_top()) {
442 // copy backward
443 MergeMemNode* mm = MergeMemNode::make(mem);
444
445 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
446 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");
447
448 if (count > 0) {
449 for (int i = count-1; i >= 1; i--) {
450 Node* off = phase->MakeConX(type2aelembytes(copy_type) * i);
451 Node* next_src = phase->transform(new AddPNode(base_src,adr_src,off));
452 Node* next_dest = phase->transform(new AddPNode(base_dest,adr_dest,off));
453 Node* v = load(bs, phase, backward_ctl, mm, next_src, atp_src, value_type, copy_type);
454 store(bs, phase, backward_ctl, mm, next_dest, atp_dest, v, value_type, copy_type);
455 }
456 Node* v = load(bs, phase, backward_ctl, mm, adr_src, atp_src, value_type, copy_type);
457 store(bs, phase, backward_ctl, mm, adr_dest, atp_dest, v, value_type, copy_type);
458 } else if (can_reshape) {
459 PhaseIterGVN* igvn = phase->is_IterGVN();
460 igvn->_worklist.push(adr_src);
461 igvn->_worklist.push(adr_dest);
462 }
463 return phase->transform(mm);
464 }
465 return phase->C->top();
466 }
467
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(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "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 != nullptr) {
494 igvn->replace_node(callprojs.fallthrough_ioproj, in(TypeFunc::I_O));
495 }
496 if (callprojs.fallthrough_memproj != nullptr) {
497 igvn->replace_node(callprojs.fallthrough_memproj, mem);
498 }
499 if (callprojs.fallthrough_catchproj != nullptr) {
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() || UseShenandoahGC, "added control for clone?");
516 phase->record_for_igvn(this);
517 return false;
518 }
519 }
520 return true;
521 }
522
523
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 nullptr;
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 nullptr;
542 }
543
544 assert(in(TypeFunc::Control) != nullptr &&
545 in(TypeFunc::Memory) != nullptr &&
547 in(ArrayCopyNode::Dest) != nullptr &&
548 in(ArrayCopyNode::Length) != nullptr &&
549 in(ArrayCopyNode::SrcPos) != nullptr &&
550 in(ArrayCopyNode::DestPos) != nullptr, "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) != nullptr && in(ArrayCopyNode::SrcPos)->is_top()) ||
557 (in(ArrayCopyNode::DestPos) != nullptr && in(ArrayCopyNode::DestPos)->is_top())) {
558 return nullptr;
559 }
560
561 int count = get_count(phase);
562
563 if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
564 return nullptr;
565 }
566
567 Node* mem = try_clone_instance(phase, can_reshape, count);
568 if (mem != nullptr) {
569 return (mem == NodeSentinel) ? nullptr : mem;
570 }
571
572 Node* adr_src = nullptr;
573 Node* base_src = nullptr;
574 Node* adr_dest = nullptr;
575 Node* base_dest = nullptr;
576 BasicType copy_type = T_ILLEGAL;
577 const Type* value_type = nullptr;
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 assert(adr_src == nullptr, "no node can be left behind");
584 assert(adr_dest == nullptr, "no node can be left behind");
585 return nullptr;
586 }
587
588 Node* src = in(ArrayCopyNode::Src);
589 Node* dest = in(ArrayCopyNode::Dest);
590 const TypePtr* atp_src = get_address_type(phase, _src_type, src);
591 const TypePtr* atp_dest = get_address_type(phase, _dest_type, dest);
592 Node* in_mem = in(TypeFunc::Memory);
593
594 if (can_reshape) {
595 assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
596 phase->is_IterGVN()->set_delay_transform(true);
597 }
598
599 Node* backward_ctl = phase->C->top();
600 Node* forward_ctl = phase->C->top();
601 array_copy_test_overlap(phase, can_reshape, disjoint_bases, count, forward_ctl, backward_ctl);
602
603 Node* forward_mem = array_copy_forward(phase, can_reshape, forward_ctl,
604 in_mem,
605 atp_src, atp_dest,
606 adr_src, base_src, adr_dest, base_dest,
607 copy_type, value_type, count);
608
609 Node* backward_mem = array_copy_backward(phase, can_reshape, backward_ctl,
610 in_mem,
611 atp_src, atp_dest,
612 adr_src, base_src, adr_dest, base_dest,
613 copy_type, value_type, count);
614
615 Node* ctl = nullptr;
616 if (!forward_ctl->is_top() && !backward_ctl->is_top()) {
617 ctl = new RegionNode(3);
618 ctl->init_req(1, forward_ctl);
619 ctl->init_req(2, backward_ctl);
620 ctl = phase->transform(ctl);
621 MergeMemNode* forward_mm = forward_mem->as_MergeMem();
622 MergeMemNode* backward_mm = backward_mem->as_MergeMem();
623 for (MergeMemStream mms(forward_mm, backward_mm); mms.next_non_empty2(); ) {
624 if (mms.memory() != mms.memory2()) {
625 Node* phi = new PhiNode(ctl, Type::MEMORY, phase->C->get_adr_type(mms.alias_idx()));
626 phi->init_req(1, mms.memory());
627 phi->init_req(2, mms.memory2());
628 phi = phase->transform(phi);
629 mms.set_memory(phi);
630 }
631 }
632 mem = forward_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 if (!finish_transform(phase, can_reshape, ctl, mem)) {
648 if (can_reshape) {
649 // put in worklist, so that if it happens to be dead it is removed
650 phase->is_IterGVN()->_worklist.push(mem);
651 }
652 return nullptr;
653 }
654
655 return mem;
656 }
657
658 bool ArrayCopyNode::may_modify(const TypeOopPtr* t_oop, PhaseValues* 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()) {
727 // write between offset_lo and offset_hi
728 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseValues* phase, bool must_modify) const {
729 assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
730
731 Node* dest = in(Dest);
732 Node* dest_pos = in(DestPos);
733 Node* len = in(Length);
734
735 const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
736 const TypeInt *len_t = phase->type(len)->isa_int();
737 const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
738
739 if (dest_pos_t == nullptr || len_t == nullptr || ary_t == nullptr) {
740 return !must_modify;
741 }
742
743 BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
744 if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
745
746 uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
747 uint elemsize = type2aelembytes(ary_elem);
748
749 jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
750 jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
751 jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
752 jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
753
754 if (must_modify) {
755 if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
756 return true;
757 }
758 } else {
759 if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
760 return true;
761 }
762 }
763 return false;
764 }
765
766 // As an optimization, choose optimum vector size for copy length known at compile time.
767 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, int const_len) {
|
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 "ci/ciFlatArrayKlass.hpp"
27 #include "gc/shared/barrierSet.hpp"
28 #include "gc/shared/c2/barrierSetC2.hpp"
29 #include "gc/shared/c2/cardTableBarrierSetC2.hpp"
30 #include "gc/shared/gc_globals.hpp"
31 #include "opto/arraycopynode.hpp"
32 #include "opto/graphKit.hpp"
33 #include "opto/inlinetypenode.hpp"
34 #include "runtime/sharedRuntime.hpp"
35 #include "utilities/macros.hpp"
36 #include "utilities/powerOfTwo.hpp"
37
38 ArrayCopyNode::ArrayCopyNode(Compile* C, bool alloc_tightly_coupled, bool has_negative_length_guard)
39 : CallNode(arraycopy_type(), nullptr, TypePtr::BOTTOM),
40 _kind(None),
41 _alloc_tightly_coupled(alloc_tightly_coupled),
42 _has_negative_length_guard(has_negative_length_guard),
43 _arguments_validated(false),
44 _src_type(TypeOopPtr::BOTTOM),
45 _dest_type(TypeOopPtr::BOTTOM) {
46 init_class_id(Class_ArrayCopy);
47 init_flags(Flag_is_macro);
48 C->add_macro_node(this);
49 }
50
51 uint ArrayCopyNode::size_of() const { return sizeof(*this); }
52
53 ArrayCopyNode* ArrayCopyNode::make(GraphKit* kit, bool may_throw,
99 void ArrayCopyNode::dump_compact_spec(outputStream* st) const {
100 st->print("%s%s", _kind_names[_kind], _alloc_tightly_coupled ? ",tight" : "");
101 }
102 #endif
103
104 intptr_t ArrayCopyNode::get_length_if_constant(PhaseGVN *phase) const {
105 // check that length is constant
106 Node* length = in(ArrayCopyNode::Length);
107 const Type* length_type = phase->type(length);
108
109 if (length_type == Type::TOP) {
110 return -1;
111 }
112
113 assert(is_clonebasic() || is_arraycopy() || is_copyof() || is_copyofrange(), "unexpected array copy type");
114
115 return is_clonebasic() ? length->find_intptr_t_con(-1) : length->find_int_con(-1);
116 }
117
118 int ArrayCopyNode::get_count(PhaseGVN *phase) const {
119 if (is_clonebasic()) {
120 Node* src = in(ArrayCopyNode::Src);
121 const Type* src_type = phase->type(src);
122
123 if (src_type == Type::TOP) {
124 return -1;
125 }
126
127 if (src_type->isa_instptr()) {
128 const TypeInstPtr* inst_src = src_type->is_instptr();
129 ciInstanceKlass* ik = inst_src->instance_klass();
130 // ciInstanceKlass::nof_nonstatic_fields() doesn't take injected
131 // fields into account. They are rare anyway so easier to simply
132 // skip instances with injected fields.
133 if ((!inst_src->klass_is_exact() && (ik->is_interface() || ik->has_subklass())) || ik->has_injected_fields()) {
134 return -1;
135 }
136 int nb_fields = ik->nof_nonstatic_fields();
137 return nb_fields;
138 } else {
139 const TypeAryPtr* ary_src = src_type->isa_aryptr();
140 assert (ary_src != nullptr, "not an array or instance?");
141 // clone passes a length as a rounded number of longs. If we're
142 // cloning an array we'll do it element by element. If the
143 // length of the input array is constant, ArrayCopyNode::Length
144 // must be too. Note that the opposite does not need to hold,
145 // because different input array lengths (e.g. int arrays with
146 // 3 or 4 elements) might lead to the same length input
147 // (e.g. 2 double-words).
148 assert(!ary_src->size()->is_con() || (get_length_if_constant(phase) >= 0) ||
149 (UseFlatArray && ary_src->elem()->make_oopptr() != nullptr && ary_src->elem()->make_oopptr()->can_be_inline_type()) ||
150 phase->is_IterGVN() || phase->C->inlining_incrementally() || StressReflectiveCode, "inconsistent");
151 if (ary_src->size()->is_con()) {
152 return ary_src->size()->get_con();
153 }
154 return -1;
155 }
156 }
157
158 return get_length_if_constant(phase);
159 }
160
161 Node* ArrayCopyNode::load(BarrierSetC2* bs, PhaseGVN *phase, Node*& ctl, MergeMemNode* mem, Node* adr, const TypePtr* adr_type, const Type *type, BasicType bt) {
162 DecoratorSet decorators = C2_READ_ACCESS | C2_CONTROL_DEPENDENT_LOAD | IN_HEAP | C2_ARRAY_COPY;
163 C2AccessValuePtr addr(adr, adr_type);
164 C2OptAccess access(*phase, ctl, mem, decorators, bt, adr->in(AddPNode::Base), addr);
165 Node* res = bs->load_at(access, type);
166 ctl = access.ctl();
167 return res;
168 }
169
180 }
181
182
183 Node* ArrayCopyNode::try_clone_instance(PhaseGVN *phase, bool can_reshape, int count) {
184 if (!is_clonebasic()) {
185 return nullptr;
186 }
187
188 Node* base_src = in(ArrayCopyNode::Src);
189 Node* base_dest = in(ArrayCopyNode::Dest);
190 Node* ctl = in(TypeFunc::Control);
191 Node* in_mem = in(TypeFunc::Memory);
192
193 const Type* src_type = phase->type(base_src);
194 const TypeInstPtr* inst_src = src_type->isa_instptr();
195 if (inst_src == nullptr) {
196 return nullptr;
197 }
198
199 MergeMemNode* mem = phase->transform(MergeMemNode::make(in_mem))->as_MergeMem();
200 phase->record_for_igvn(mem);
201 if (can_reshape) {
202 phase->is_IterGVN()->_worklist.push(mem);
203 }
204
205
206 ciInstanceKlass* ik = inst_src->instance_klass();
207
208 if (!inst_src->klass_is_exact()) {
209 assert(!ik->is_interface(), "inconsistent klass hierarchy");
210 if (ik->has_subklass()) {
211 // Concurrent class loading.
212 // Fail fast and return NodeSentinel to indicate that the transform failed.
213 return NodeSentinel;
214 } else {
215 phase->C->dependencies()->assert_leaf_type(ik);
216 }
217 }
218
219 assert(ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem, "too many fields");
220
267 Node* src_offset = in(ArrayCopyNode::SrcPos);
268 Node* dest_offset = in(ArrayCopyNode::DestPos);
269
270 if (is_arraycopy() || is_copyofrange() || is_copyof()) {
271 const Type* dest_type = phase->type(base_dest);
272 const TypeAryPtr* ary_dest = dest_type->isa_aryptr();
273
274 // newly allocated object is guaranteed to not overlap with source object
275 disjoint_bases = is_alloc_tightly_coupled();
276 if (ary_src == nullptr || ary_src->elem() == Type::BOTTOM ||
277 ary_dest == nullptr || ary_dest->elem() == Type::BOTTOM) {
278 // We don't know if arguments are arrays
279 return false;
280 }
281
282 BasicType src_elem = ary_src->elem()->array_element_basic_type();
283 BasicType dest_elem = ary_dest->elem()->array_element_basic_type();
284 if (is_reference_type(src_elem, true)) src_elem = T_OBJECT;
285 if (is_reference_type(dest_elem, true)) dest_elem = T_OBJECT;
286
287 if (src_elem != dest_elem || ary_src->is_flat() != ary_dest->is_flat() || dest_elem == T_VOID) {
288 // We don't know if arguments are arrays of the same type
289 return false;
290 }
291
292 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
293 if ((!ary_dest->is_flat() && bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), dest_elem, false, false, BarrierSetC2::Optimization)) ||
294 (ary_dest->is_flat() && ary_src->elem()->inline_klass()->contains_oops() &&
295 bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), T_OBJECT, false, false, BarrierSetC2::Optimization))) {
296 // It's an object array copy but we can't emit the card marking that is needed
297 return false;
298 }
299
300 value_type = ary_src->elem();
301
302 uint shift = exact_log2(type2aelembytes(dest_elem));
303 if (ary_dest->is_flat()) {
304 shift = ary_src->flat_log_elem_size();
305 }
306 uint header = arrayOopDesc::base_offset_in_bytes(dest_elem);
307
308 src_offset = Compile::conv_I2X_index(phase, src_offset, ary_src->size());
309 if (src_offset->is_top()) {
310 // Offset is out of bounds (the ArrayCopyNode will be removed)
311 return false;
312 }
313 dest_offset = Compile::conv_I2X_index(phase, dest_offset, ary_dest->size());
314 if (dest_offset->is_top()) {
315 // Offset is out of bounds (the ArrayCopyNode will be removed)
316 if (can_reshape) {
317 // record src_offset, so it can be deleted later (if it is dead)
318 phase->is_IterGVN()->_worklist.push(src_offset);
319 }
320 return false;
321 }
322
323 Node* hook = new Node(1);
324 hook->init_req(0, dest_offset);
325
326 Node* src_scale = phase->transform(new LShiftXNode(src_offset, phase->intcon(shift)));
327
328 hook->destruct(phase);
329
330 Node* dest_scale = phase->transform(new LShiftXNode(dest_offset, phase->intcon(shift)));
331
332 adr_src = phase->transform(new AddPNode(base_src, base_src, src_scale));
333 adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_scale));
334
335 adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(header)));
336 adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(header)));
337
338 copy_type = dest_elem;
339 } else {
340 assert(ary_src != nullptr, "should be a clone");
341 assert(is_clonebasic(), "should be");
342
343 disjoint_bases = true;
344
345 if (ary_src->elem()->make_oopptr() != nullptr &&
346 ary_src->elem()->make_oopptr()->can_be_inline_type()) {
347 return false;
348 }
349
350 BasicType elem = ary_src->isa_aryptr()->elem()->array_element_basic_type();
351 if (is_reference_type(elem, true)) {
352 elem = T_OBJECT;
353 }
354
355 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
356 if ((!ary_src->is_flat() && bs->array_copy_requires_gc_barriers(true, elem, true, is_clone_inst(), BarrierSetC2::Optimization)) ||
357 (ary_src->is_flat() && ary_src->elem()->inline_klass()->contains_oops() &&
358 bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization))) {
359 // It's an object array copy but we can't emit the card marking that is needed
360 return false;
361 }
362
363 adr_src = phase->transform(new AddPNode(base_src, base_src, src_offset));
364 adr_dest = phase->transform(new AddPNode(base_dest, base_dest, dest_offset));
365
366 // The address is offsetted to an aligned address where a raw copy would start.
367 // If the clone copy is decomposed into load-stores - the address is adjusted to
368 // point at where the array starts.
369 const Type* toff = phase->type(src_offset);
370 int offset = toff->isa_long() ? (int) toff->is_long()->get_con() : (int) toff->is_int()->get_con();
371 int diff = arrayOopDesc::base_offset_in_bytes(elem) - offset;
372 assert(diff >= 0, "clone should not start after 1st array element");
373 if (diff > 0) {
374 adr_src = phase->transform(new AddPNode(base_src, adr_src, phase->MakeConX(diff)));
375 adr_dest = phase->transform(new AddPNode(base_dest, adr_dest, phase->MakeConX(diff)));
376 }
377 copy_type = elem;
378 value_type = ary_src->elem();
379 }
380 return true;
381 }
382
383 const TypeAryPtr* ArrayCopyNode::get_address_type(PhaseGVN* phase, const TypePtr* atp, Node* n) {
384 if (atp == TypeOopPtr::BOTTOM) {
385 atp = phase->type(n)->isa_ptr();
386 }
387 // adjust atp to be the correct array element address type
388 return atp->add_offset(Type::OffsetBot)->is_aryptr();
389 }
390
391 void ArrayCopyNode::array_copy_test_overlap(GraphKit& kit, bool disjoint_bases, int count, Node*& backward_ctl) {
392 Node* ctl = kit.control();
393 if (!disjoint_bases && count > 1) {
394 PhaseGVN& gvn = kit.gvn();
395 Node* src_offset = in(ArrayCopyNode::SrcPos);
396 Node* dest_offset = in(ArrayCopyNode::DestPos);
397 assert(src_offset != nullptr && dest_offset != nullptr, "should be");
398 Node* cmp = gvn.transform(new CmpINode(src_offset, dest_offset));
399 Node *bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
400 IfNode *iff = new IfNode(ctl, bol, PROB_FAIR, COUNT_UNKNOWN);
401
402 gvn.transform(iff);
403
404 kit.set_control(gvn.transform(new IfFalseNode(iff)));
405 backward_ctl = gvn.transform(new IfTrueNode(iff));
406 }
407 }
408
409 void ArrayCopyNode::copy(GraphKit& kit,
410 const TypeAryPtr* atp_src,
411 const TypeAryPtr* atp_dest,
412 int i,
413 Node* base_src,
414 Node* base_dest,
415 Node* adr_src,
416 Node* adr_dest,
417 BasicType copy_type,
418 const Type* value_type) {
419 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
420 Node* ctl = kit.control();
421 if (atp_dest->is_flat()) {
422 ciInlineKlass* vk = atp_src->elem()->inline_klass();
423 for (int j = 0; j < vk->nof_nonstatic_fields(); j++) {
424 ciField* field = vk->nonstatic_field_at(j);
425 int off_in_vt = field->offset_in_bytes() - vk->first_field_offset();
426 Node* off = kit.MakeConX(off_in_vt + i * atp_src->flat_elem_size());
427 ciType* ft = field->type();
428 BasicType bt = type2field[ft->basic_type()];
429 assert(!field->is_flat(), "flat field encountered");
430 const Type* rt = Type::get_const_type(ft);
431 const TypePtr* adr_type = atp_src->with_field_offset(off_in_vt)->add_offset(Type::OffsetBot);
432 assert(!bs->array_copy_requires_gc_barriers(is_alloc_tightly_coupled(), bt, false, false, BarrierSetC2::Optimization), "GC barriers required");
433 Node* next_src = kit.gvn().transform(new AddPNode(base_src, adr_src, off));
434 Node* next_dest = kit.gvn().transform(new AddPNode(base_dest, adr_dest, off));
435 Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, adr_type, rt, bt);
436 store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, adr_type, v, rt, bt);
437 }
438 } else {
439 Node* off = kit.MakeConX(type2aelembytes(copy_type) * i);
440 Node* next_src = kit.gvn().transform(new AddPNode(base_src, adr_src, off));
441 Node* next_dest = kit.gvn().transform(new AddPNode(base_dest, adr_dest, off));
442 Node* v = load(bs, &kit.gvn(), ctl, kit.merged_memory(), next_src, atp_src, value_type, copy_type);
443 store(bs, &kit.gvn(), ctl, kit.merged_memory(), next_dest, atp_dest, v, value_type, copy_type);
444 }
445 kit.set_control(ctl);
446 }
447
448
449 void ArrayCopyNode::array_copy_forward(GraphKit& kit,
450 bool can_reshape,
451 const TypeAryPtr* atp_src,
452 const TypeAryPtr* atp_dest,
453 Node* adr_src,
454 Node* base_src,
455 Node* adr_dest,
456 Node* base_dest,
457 BasicType copy_type,
458 const Type* value_type,
459 int count) {
460 if (!kit.stopped()) {
461 // copy forward
462 if (count > 0) {
463 for (int i = 0; i < count; i++) {
464 copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);
465 }
466 } else if (can_reshape) {
467 PhaseGVN& gvn = kit.gvn();
468 assert(gvn.is_IterGVN(), "");
469 gvn.record_for_igvn(adr_src);
470 gvn.record_for_igvn(adr_dest);
471 }
472 }
473 }
474
475 void ArrayCopyNode::array_copy_backward(GraphKit& kit,
476 bool can_reshape,
477 const TypeAryPtr* atp_src,
478 const TypeAryPtr* atp_dest,
479 Node* adr_src,
480 Node* base_src,
481 Node* adr_dest,
482 Node* base_dest,
483 BasicType copy_type,
484 const Type* value_type,
485 int count) {
486 if (!kit.stopped()) {
487 // copy backward
488 PhaseGVN& gvn = kit.gvn();
489
490 if (count > 0) {
491 for (int i = count-1; i >= 0; i--) {
492 copy(kit, atp_src, atp_dest, i, base_src, base_dest, adr_src, adr_dest, copy_type, value_type);
493 }
494 } else if(can_reshape) {
495 PhaseGVN& gvn = kit.gvn();
496 assert(gvn.is_IterGVN(), "");
497 gvn.record_for_igvn(adr_src);
498 gvn.record_for_igvn(adr_dest);
499 }
500 }
501 }
502
503 bool ArrayCopyNode::finish_transform(PhaseGVN *phase, bool can_reshape,
504 Node* ctl, Node *mem) {
505 if (can_reshape) {
506 PhaseIterGVN* igvn = phase->is_IterGVN();
507 igvn->set_delay_transform(false);
508 if (is_clonebasic()) {
509 Node* out_mem = proj_out(TypeFunc::Memory);
510
511 BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
512 if (out_mem->outcnt() != 1 || !out_mem->raw_out(0)->is_MergeMem() ||
513 out_mem->raw_out(0)->outcnt() != 1 || !out_mem->raw_out(0)->raw_out(0)->is_MemBar()) {
514 assert(bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, is_clone_inst(), BarrierSetC2::Optimization), "can only happen with card marking");
515 return false;
516 }
517
518 igvn->replace_node(out_mem->raw_out(0), mem);
519
520 Node* out_ctl = proj_out(TypeFunc::Control);
521 igvn->replace_node(out_ctl, ctl);
522 } else {
523 // replace fallthrough projections of the ArrayCopyNode by the
524 // new memory, control and the input IO.
525 CallProjections* callprojs = extract_projections(true, false);
526
527 if (callprojs->fallthrough_ioproj != nullptr) {
528 igvn->replace_node(callprojs->fallthrough_ioproj, in(TypeFunc::I_O));
529 }
530 if (callprojs->fallthrough_memproj != nullptr) {
531 igvn->replace_node(callprojs->fallthrough_memproj, mem);
532 }
533 if (callprojs->fallthrough_catchproj != nullptr) {
534 igvn->replace_node(callprojs->fallthrough_catchproj, ctl);
535 }
536
537 // The ArrayCopyNode is not disconnected. It still has the
538 // projections for the exception case. Replace current
539 // ArrayCopyNode with a dummy new one with a top() control so
540 // that this part of the graph stays consistent but is
541 // eventually removed.
542
543 set_req(0, phase->C->top());
544 remove_dead_region(phase, can_reshape);
545 }
546 } else {
547 if (in(TypeFunc::Control) != ctl) {
548 // we can't return new memory and control from Ideal at parse time
549 assert(!is_clonebasic() || UseShenandoahGC, "added control for clone?");
550 phase->record_for_igvn(this);
551 return false;
552 }
553 }
554 return true;
555 }
556
557
558 Node *ArrayCopyNode::Ideal(PhaseGVN *phase, bool can_reshape) {
559 // Perform any generic optimizations first
560 Node* result = SafePointNode::Ideal(phase, can_reshape);
561 if (result != nullptr) {
562 return result;
563 }
564
565 if (StressArrayCopyMacroNode && !can_reshape) {
566 phase->record_for_igvn(this);
567 return nullptr;
568 }
569
570 // See if it's a small array copy and we can inline it as
571 // loads/stores
572 // Here we can only do:
573 // - arraycopy if all arguments were validated before and we don't
574 // need card marking
575 // - clone for which we don't need to do card marking
576
577 if (!is_clonebasic() && !is_arraycopy_validated() &&
578 !is_copyofrange_validated() && !is_copyof_validated()) {
579 return nullptr;
580 }
581
582 assert(in(TypeFunc::Control) != nullptr &&
583 in(TypeFunc::Memory) != nullptr &&
585 in(ArrayCopyNode::Dest) != nullptr &&
586 in(ArrayCopyNode::Length) != nullptr &&
587 in(ArrayCopyNode::SrcPos) != nullptr &&
588 in(ArrayCopyNode::DestPos) != nullptr, "broken inputs");
589
590 if (in(TypeFunc::Control)->is_top() ||
591 in(TypeFunc::Memory)->is_top() ||
592 phase->type(in(ArrayCopyNode::Src)) == Type::TOP ||
593 phase->type(in(ArrayCopyNode::Dest)) == Type::TOP ||
594 (in(ArrayCopyNode::SrcPos) != nullptr && in(ArrayCopyNode::SrcPos)->is_top()) ||
595 (in(ArrayCopyNode::DestPos) != nullptr && in(ArrayCopyNode::DestPos)->is_top())) {
596 return nullptr;
597 }
598
599 int count = get_count(phase);
600
601 if (count < 0 || count > ArrayCopyLoadStoreMaxElem) {
602 return nullptr;
603 }
604
605 Node* src = in(ArrayCopyNode::Src);
606 Node* dest = in(ArrayCopyNode::Dest);
607 const Type* src_type = phase->type(src);
608 const Type* dest_type = phase->type(dest);
609
610 if (src_type->isa_aryptr() && dest_type->isa_instptr()) {
611 // clone used for load of unknown inline type can't be optimized at
612 // this point
613 return nullptr;
614 }
615
616 Node* mem = try_clone_instance(phase, can_reshape, count);
617 if (mem != nullptr) {
618 return (mem == NodeSentinel) ? nullptr : mem;
619 }
620
621 Node* adr_src = nullptr;
622 Node* base_src = nullptr;
623 Node* adr_dest = nullptr;
624 Node* base_dest = nullptr;
625 BasicType copy_type = T_ILLEGAL;
626 const Type* value_type = nullptr;
627 bool disjoint_bases = false;
628
629 if (!prepare_array_copy(phase, can_reshape,
630 adr_src, base_src, adr_dest, base_dest,
631 copy_type, value_type, disjoint_bases)) {
632 assert(adr_src == nullptr, "no node can be left behind");
633 assert(adr_dest == nullptr, "no node can be left behind");
634 return nullptr;
635 }
636
637 JVMState* new_jvms = nullptr;
638 SafePointNode* new_map = nullptr;
639 if (!is_clonebasic()) {
640 new_jvms = jvms()->clone_shallow(phase->C);
641 new_map = new SafePointNode(req(), new_jvms);
642 for (uint i = TypeFunc::FramePtr; i < req(); i++) {
643 new_map->init_req(i, in(i));
644 }
645 new_jvms->set_map(new_map);
646 } else {
647 new_jvms = new (phase->C) JVMState(0);
648 new_map = new SafePointNode(TypeFunc::Parms, new_jvms);
649 new_jvms->set_map(new_map);
650 }
651 new_map->set_control(in(TypeFunc::Control));
652 new_map->set_memory(MergeMemNode::make(in(TypeFunc::Memory)));
653 new_map->set_i_o(in(TypeFunc::I_O));
654 phase->record_for_igvn(new_map);
655
656 const TypeAryPtr* atp_src = get_address_type(phase, _src_type, src);
657 const TypeAryPtr* atp_dest = get_address_type(phase, _dest_type, dest);
658
659 if (can_reshape) {
660 assert(!phase->is_IterGVN()->delay_transform(), "cannot delay transforms");
661 phase->is_IterGVN()->set_delay_transform(true);
662 }
663
664 GraphKit kit(new_jvms, phase);
665
666 SafePointNode* backward_map = nullptr;
667 SafePointNode* forward_map = nullptr;
668 Node* backward_ctl = phase->C->top();
669
670 array_copy_test_overlap(kit, disjoint_bases, count, backward_ctl);
671
672 {
673 PreserveJVMState pjvms(&kit);
674
675 array_copy_forward(kit, can_reshape,
676 atp_src, atp_dest,
677 adr_src, base_src, adr_dest, base_dest,
678 copy_type, value_type, count);
679
680 forward_map = kit.stop();
681 }
682
683 kit.set_control(backward_ctl);
684 array_copy_backward(kit, can_reshape,
685 atp_src, atp_dest,
686 adr_src, base_src, adr_dest, base_dest,
687 copy_type, value_type, count);
688
689 backward_map = kit.stop();
690
691 if (!forward_map->control()->is_top() && !backward_map->control()->is_top()) {
692 assert(forward_map->i_o() == backward_map->i_o(), "need a phi on IO?");
693 Node* ctl = new RegionNode(3);
694 Node* mem = new PhiNode(ctl, Type::MEMORY, TypePtr::BOTTOM);
695 kit.set_map(forward_map);
696 ctl->init_req(1, kit.control());
697 mem->init_req(1, kit.reset_memory());
698 kit.set_map(backward_map);
699 ctl->init_req(2, kit.control());
700 mem->init_req(2, kit.reset_memory());
701 kit.set_control(phase->transform(ctl));
702 kit.set_all_memory(phase->transform(mem));
703 } else if (!forward_map->control()->is_top()) {
704 kit.set_map(forward_map);
705 } else {
706 assert(!backward_map->control()->is_top(), "no copy?");
707 kit.set_map(backward_map);
708 }
709
710 if (can_reshape) {
711 assert(phase->is_IterGVN()->delay_transform(), "should be delaying transforms");
712 phase->is_IterGVN()->set_delay_transform(false);
713 }
714
715 mem = kit.map()->memory();
716 if (!finish_transform(phase, can_reshape, kit.control(), mem)) {
717 if (!can_reshape) {
718 phase->record_for_igvn(this);
719 } else {
720 // put in worklist, so that if it happens to be dead it is removed
721 phase->is_IterGVN()->_worklist.push(mem);
722 }
723 return nullptr;
724 }
725
726 return mem;
727 }
728
729 bool ArrayCopyNode::may_modify(const TypeOopPtr* t_oop, PhaseValues* phase) {
730 Node* dest = in(ArrayCopyNode::Dest);
731 if (dest->is_top()) {
732 return false;
733 }
734 const TypeOopPtr* dest_t = phase->type(dest)->is_oopptr();
735 assert(!dest_t->is_known_instance() || _dest_type->is_known_instance(), "result of EA not recorded");
736 assert(in(ArrayCopyNode::Src)->is_top() || !phase->type(in(ArrayCopyNode::Src))->is_oopptr()->is_known_instance() ||
737 _src_type->is_known_instance(), "result of EA not recorded");
738
739 if (_dest_type != TypeOopPtr::BOTTOM || t_oop->is_known_instance()) {
798 // write between offset_lo and offset_hi
799 bool ArrayCopyNode::modifies(intptr_t offset_lo, intptr_t offset_hi, PhaseValues* phase, bool must_modify) const {
800 assert(_kind == ArrayCopy || _kind == CopyOf || _kind == CopyOfRange, "only for real array copies");
801
802 Node* dest = in(Dest);
803 Node* dest_pos = in(DestPos);
804 Node* len = in(Length);
805
806 const TypeInt *dest_pos_t = phase->type(dest_pos)->isa_int();
807 const TypeInt *len_t = phase->type(len)->isa_int();
808 const TypeAryPtr* ary_t = phase->type(dest)->isa_aryptr();
809
810 if (dest_pos_t == nullptr || len_t == nullptr || ary_t == nullptr) {
811 return !must_modify;
812 }
813
814 BasicType ary_elem = ary_t->isa_aryptr()->elem()->array_element_basic_type();
815 if (is_reference_type(ary_elem, true)) ary_elem = T_OBJECT;
816
817 uint header = arrayOopDesc::base_offset_in_bytes(ary_elem);
818 uint elemsize = ary_t->is_flat() ? ary_t->flat_elem_size() : type2aelembytes(ary_elem);
819
820 jlong dest_pos_plus_len_lo = (((jlong)dest_pos_t->_lo) + len_t->_lo) * elemsize + header;
821 jlong dest_pos_plus_len_hi = (((jlong)dest_pos_t->_hi) + len_t->_hi) * elemsize + header;
822 jlong dest_pos_lo = ((jlong)dest_pos_t->_lo) * elemsize + header;
823 jlong dest_pos_hi = ((jlong)dest_pos_t->_hi) * elemsize + header;
824
825 if (must_modify) {
826 if (offset_lo >= dest_pos_hi && offset_hi < dest_pos_plus_len_lo) {
827 return true;
828 }
829 } else {
830 if (offset_hi >= dest_pos_lo && offset_lo < dest_pos_plus_len_hi) {
831 return true;
832 }
833 }
834 return false;
835 }
836
837 // As an optimization, choose optimum vector size for copy length known at compile time.
838 int ArrayCopyNode::get_partial_inline_vector_lane_count(BasicType type, int const_len) {
|