2
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "compiler/compileLog.hpp"
27
#include "interpreter/linkResolver.hpp"
28
#include "memory/universe.hpp"
29
#include "oops/objArrayKlass.hpp"
30
#include "opto/addnode.hpp"
31
#include "opto/castnode.hpp"
32
#include "opto/memnode.hpp"
33
#include "opto/parse.hpp"
34
#include "opto/rootnode.hpp"
35
#include "opto/runtime.hpp"
36
#include "opto/subnode.hpp"
37
#include "runtime/deoptimization.hpp"
38
#include "runtime/handles.inline.hpp"
40
//=============================================================================
41
// Helper methods for _get* and _put* bytecodes
42
//=============================================================================
43
void Parse::do_field_access(bool is_get, bool is_field) {
45
ciField* field = iter().get_field(will_link);
46
assert(will_link, "getfield: typeflow responsibility");
48
ciInstanceKlass* field_holder = field->holder();
50
if (is_field == field->is_static()) {
51
// Interpreter will throw java_lang_IncompatibleClassChangeError
52
// Check this before allowing <clinit> methods to access static fields
53
uncommon_trap(Deoptimization::Reason_unhandled,
54
Deoptimization::Action_none);
58
// Deoptimize on putfield writes to call site target field outside of CallSite ctor.
59
if (!is_get && field->is_call_site_target() &&
60
!(method()->holder() == field_holder && method()->is_object_initializer())) {
61
uncommon_trap(Deoptimization::Reason_unhandled,
62
Deoptimization::Action_reinterpret,
63
nullptr, "put to call site target field");
67
if (C->needs_clinit_barrier(field, method())) {
68
clinit_barrier(field_holder, method());
69
if (stopped()) return;
72
assert(field->will_link(method(), bc()), "getfield: typeflow responsibility");
74
// Note: We do not check for an unloaded field type here any more.
76
// Generate code for the object pointer.
79
int obj_depth = is_get ? 0 : field->type()->size();
80
obj = null_check(peek(obj_depth));
81
// Compile-time detect of null-exception?
82
if (stopped()) return;
85
const TypeInstPtr *tjp = TypeInstPtr::make(TypePtr::NotNull, iter().get_declared_field_holder());
86
assert(_gvn.type(obj)->higher_equal(tjp), "cast_up is no longer needed");
90
(void) pop(); // pop receiver before getting
91
do_get_xxx(obj, field, is_field);
93
do_put_xxx(obj, field, is_field);
94
(void) pop(); // pop receiver after putting
97
const TypeInstPtr* tip = TypeInstPtr::make(field_holder->java_mirror());
98
obj = _gvn.makecon(tip);
100
do_get_xxx(obj, field, is_field);
102
do_put_xxx(obj, field, is_field);
108
void Parse::do_get_xxx(Node* obj, ciField* field, bool is_field) {
109
BasicType bt = field->layout_type();
111
// Does this field have a constant value? If so, just push the value.
112
if (field->is_constant() &&
113
// Keep consistent with types found by ciTypeFlow: for an
114
// unloaded field type, ciTypeFlow::StateVector::do_getstatic()
115
// speculates the field is null. The code in the rest of this
116
// method does the same. We must not bypass it and use a non
117
// null constant here.
118
(bt != T_OBJECT || field->type()->is_loaded())) {
119
// final or stable field
120
Node* con = make_constant_from_field(field, obj);
121
if (con != nullptr) {
122
push_node(field->layout_type(), con);
127
ciType* field_klass = field->type();
128
bool is_vol = field->is_volatile();
130
// Compute address and memory type.
131
int offset = field->offset_in_bytes();
132
const TypePtr* adr_type = C->alias_type(field)->adr_type();
133
Node *adr = basic_plus_adr(obj, obj, offset);
135
// Build the resultant type of the load
138
bool must_assert_null = false;
140
DecoratorSet decorators = IN_HEAP;
141
decorators |= is_vol ? MO_SEQ_CST : MO_UNORDERED;
143
bool is_obj = is_reference_type(bt);
146
if (!field->type()->is_loaded()) {
147
type = TypeInstPtr::BOTTOM;
148
must_assert_null = true;
149
} else if (field->is_static_constant()) {
150
// This can happen if the constant oop is non-perm.
151
ciObject* con = field->constant_value().as_object();
152
// Do not "join" in the previous type; it doesn't add value,
153
// and may yield a vacuous result if the field is of interface type.
154
if (con->is_null_object()) {
155
type = TypePtr::NULL_PTR;
157
type = TypeOopPtr::make_from_constant(con)->isa_oopptr();
159
assert(type != nullptr, "field singleton type must be consistent");
161
type = TypeOopPtr::make_from_klass(field_klass->as_klass());
164
type = Type::get_const_basic_type(bt);
167
Node* ld = access_load_at(obj, adr, adr_type, type, bt, decorators);
170
if (type2size[bt] == 1)
175
if (must_assert_null) {
176
// Do not take a trap here. It's possible that the program
177
// will never load the field's class, and will happily see
178
// null values in this field forever. Don't stumble into a
179
// trap for such a program, or we might get a long series
180
// of useless recompilations. (Or, we might load a class
181
// which should not be loaded.) If we ever see a non-null
182
// value, we will then trap and recompile. (The trap will
183
// not need to mention the class index, since the class will
184
// already have been loaded if we ever see a non-null value.)
185
// uncommon_trap(iter().get_field_signature_index());
186
if (PrintOpto && (Verbose || WizardMode)) {
187
method()->print_name(); tty->print_cr(" asserting nullness of field at bci: %d", bci());
189
if (C->log() != nullptr) {
190
C->log()->elem("assert_null reason='field' klass='%d'",
191
C->log()->identify(field->type()));
193
// If there is going to be a trap, put it at the next bytecode:
194
set_bci(iter().next_bci());
196
set_bci(iter().cur_bci()); // put it back
200
void Parse::do_put_xxx(Node* obj, ciField* field, bool is_field) {
201
bool is_vol = field->is_volatile();
203
// Compute address and memory type.
204
int offset = field->offset_in_bytes();
205
const TypePtr* adr_type = C->alias_type(field)->adr_type();
206
Node* adr = basic_plus_adr(obj, obj, offset);
207
BasicType bt = field->layout_type();
208
// Value to be stored
209
Node* val = type2size[bt] == 1 ? pop() : pop_pair();
211
DecoratorSet decorators = IN_HEAP;
212
decorators |= is_vol ? MO_SEQ_CST : MO_UNORDERED;
214
bool is_obj = is_reference_type(bt);
217
const Type* field_type;
218
if (!field->type()->is_loaded()) {
219
field_type = TypeInstPtr::BOTTOM;
222
field_type = TypeOopPtr::make_from_klass(field->type()->as_klass());
224
field_type = Type::BOTTOM;
227
access_store_at(obj, adr, adr_type, val, field_type, bt, decorators);
230
// Remember we wrote a volatile field.
231
// For not multiple copy atomic cpu (ppc64) a barrier should be issued
232
// in constructors which have such stores. See do_exits() in parse1.cpp.
234
set_wrote_volatile(true);
236
set_wrote_fields(true);
238
// If the field is final, the rules of Java say we are in <init> or <clinit>.
239
// Note the presence of writes to final non-static fields, so that we
240
// can insert a memory barrier later on to keep the writes from floating
241
// out of the constructor.
242
// Any method can write a @Stable field; insert memory barriers after those also.
243
if (field->is_final()) {
244
set_wrote_final(true);
245
if (AllocateNode::Ideal_allocation(obj) != nullptr) {
246
// Preserve allocation ptr to create precedent edge to it in membar
247
// generated on exit from constructor.
248
// Can't bind stable with its allocation, only record allocation for final field.
249
set_alloc_with_final(obj);
252
if (field->is_stable()) {
253
set_wrote_stable(true);
258
//=============================================================================
259
void Parse::do_anewarray() {
261
ciKlass* klass = iter().get_klass(will_link);
263
// Uncommon Trap when class that array contains is not loaded
264
// we need the loaded class for the rest of graph; do not
265
// initialize the container class (see Java spec)!!!
266
assert(will_link, "anewarray: typeflow responsibility");
268
ciObjArrayKlass* array_klass = ciObjArrayKlass::make(klass);
269
// Check that array_klass object is loaded
270
if (!array_klass->is_loaded()) {
271
// Generate uncommon_trap for unloaded array_class
272
uncommon_trap(Deoptimization::Reason_unloaded,
273
Deoptimization::Action_reinterpret,
280
const TypeKlassPtr* array_klass_type = TypeKlassPtr::make(array_klass, Type::trust_interfaces);
281
Node* count_val = pop();
282
Node* obj = new_array(makecon(array_klass_type), count_val, 1);
287
void Parse::do_newarray(BasicType elem_type) {
290
Node* count_val = pop();
291
const TypeKlassPtr* array_klass = TypeKlassPtr::make(ciTypeArrayKlass::make(elem_type));
292
Node* obj = new_array(makecon(array_klass), count_val, 1);
293
// Push resultant oop onto stack
297
// Expand simple expressions like new int[3][5] and new Object[2][nonConLen].
298
// Also handle the degenerate 1-dimensional case of anewarray.
299
Node* Parse::expand_multianewarray(ciArrayKlass* array_klass, Node* *lengths, int ndimensions, int nargs) {
300
Node* length = lengths[0];
301
assert(length != nullptr, "");
302
Node* array = new_array(makecon(TypeKlassPtr::make(array_klass, Type::trust_interfaces)), length, nargs);
303
if (ndimensions > 1) {
304
jint length_con = find_int_con(length, -1);
305
guarantee(length_con >= 0, "non-constant multianewarray");
306
ciArrayKlass* array_klass_1 = array_klass->as_obj_array_klass()->element_klass()->as_array_klass();
307
const TypePtr* adr_type = TypeAryPtr::OOPS;
308
const TypeOopPtr* elemtype = _gvn.type(array)->is_aryptr()->elem()->make_oopptr();
309
const intptr_t header = arrayOopDesc::base_offset_in_bytes(T_OBJECT);
310
for (jint i = 0; i < length_con; i++) {
311
Node* elem = expand_multianewarray(array_klass_1, &lengths[1], ndimensions-1, nargs);
312
intptr_t offset = header + ((intptr_t)i << LogBytesPerHeapOop);
313
Node* eaddr = basic_plus_adr(array, offset);
314
access_store_at(array, eaddr, adr_type, elem, elemtype, T_OBJECT, IN_HEAP | IS_ARRAY);
320
void Parse::do_multianewarray() {
321
int ndimensions = iter().get_dimensions();
323
// the m-dimensional array
325
ciArrayKlass* array_klass = iter().get_klass(will_link)->as_array_klass();
326
assert(will_link, "multianewarray: typeflow responsibility");
328
// Note: Array classes are always initialized; no is_initialized check.
332
// get the lengths from the stack (first dimension is on top)
333
Node** length = NEW_RESOURCE_ARRAY(Node*, ndimensions + 1);
334
length[ndimensions] = nullptr; // terminating null for make_runtime_call
336
for (j = ndimensions-1; j >= 0 ; j--) length[j] = pop();
338
// The original expression was of this form: new T[length0][length1]...
339
// It is often the case that the lengths are small (except the last).
340
// If that happens, use the fast 1-d creator a constant number of times.
341
const int expand_limit = MIN2((int)MultiArrayExpandLimit, 100);
342
int64_t expand_count = 1; // count of allocations in the expansion
343
int64_t expand_fanout = 1; // running total fanout
344
for (j = 0; j < ndimensions-1; j++) {
345
int dim_con = find_int_con(length[j], -1);
346
// To prevent overflow, we use 64-bit values. Alternatively,
347
// we could clamp dim_con like so:
348
// dim_con = MIN2(dim_con, expand_limit);
349
expand_fanout *= dim_con;
350
expand_count += expand_fanout; // count the level-J sub-arrays
352
|| dim_con > expand_limit
353
|| expand_count > expand_limit) {
359
// Can use multianewarray instead of [a]newarray if only one dimension,
360
// or if all non-final dimensions are small constants.
361
if (ndimensions == 1 || (1 <= expand_count && expand_count <= expand_limit)) {
363
// Set the original stack and the reexecute bit for the interpreter
364
// to reexecute the multianewarray bytecode if deoptimization happens.
365
// Do it unconditionally even for one dimension multianewarray.
366
// Note: the reexecute bit will be set in GraphKit::add_safepoint_edges()
367
// when AllocateArray node for newarray is created.
368
{ PreserveReexecuteState preexecs(this);
370
// Pass 0 as nargs since uncommon trap code does not need to restore stack.
371
obj = expand_multianewarray(array_klass, &length[0], ndimensions, 0);
372
} //original reexecute and sp are set back here
377
address fun = nullptr;
378
switch (ndimensions) {
379
case 1: ShouldNotReachHere(); break;
380
case 2: fun = OptoRuntime::multianewarray2_Java(); break;
381
case 3: fun = OptoRuntime::multianewarray3_Java(); break;
382
case 4: fun = OptoRuntime::multianewarray4_Java(); break;
383
case 5: fun = OptoRuntime::multianewarray5_Java(); break;
387
if (fun != nullptr) {
388
c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
389
OptoRuntime::multianewarray_Type(ndimensions),
390
fun, nullptr, TypeRawPtr::BOTTOM,
391
makecon(TypeKlassPtr::make(array_klass, Type::trust_interfaces)),
392
length[0], length[1], length[2],
393
(ndimensions > 2) ? length[3] : nullptr,
394
(ndimensions > 3) ? length[4] : nullptr);
396
// Create a java array for dimension sizes
397
Node* dims = nullptr;
398
{ PreserveReexecuteState preexecs(this);
400
Node* dims_array_klass = makecon(TypeKlassPtr::make(ciArrayKlass::make(ciType::make(T_INT))));
401
dims = new_array(dims_array_klass, intcon(ndimensions), 0);
403
// Fill-in it with values
404
for (j = 0; j < ndimensions; j++) {
405
Node *dims_elem = array_element_address(dims, intcon(j), T_INT);
406
store_to_memory(control(), dims_elem, length[j], T_INT, TypeAryPtr::INTS, MemNode::unordered);
410
c = make_runtime_call(RC_NO_LEAF | RC_NO_IO,
411
OptoRuntime::multianewarrayN_Type(),
412
OptoRuntime::multianewarrayN_Java(), nullptr, TypeRawPtr::BOTTOM,
413
makecon(TypeKlassPtr::make(array_klass, Type::trust_interfaces)),
416
make_slow_call_ex(c, env()->Throwable_klass(), false);
418
Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms));
420
const Type* type = TypeOopPtr::make_from_klass_raw(array_klass, Type::trust_interfaces);
422
// Improve the type: We know it's not null, exact, and of a given length.
423
type = type->is_ptr()->cast_to_ptr_type(TypePtr::NotNull);
424
type = type->is_aryptr()->cast_to_exactness(true);
426
const TypeInt* ltype = _gvn.find_int_type(length[0]);
427
if (ltype != nullptr)
428
type = type->is_aryptr()->cast_to_size(ltype);
430
// We cannot sharpen the nested sub-arrays, since the top level is mutable.
432
Node* cast = _gvn.transform( new CheckCastPPNode(control(), res, type) );
435
// Possible improvements:
436
// - Make a fast path for small multi-arrays. (W/ implicit init. loops.)
437
// - Issue CastII against length[*] values, to TypeInt::POS.