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