1 /* 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. 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 "compiler/compileLog.hpp" 27 #include "interpreter/linkResolver.hpp" 28 #include "memory/universe.hpp" 29 #include "oops/flatArrayKlass.hpp" 30 #include "oops/objArrayKlass.hpp" 31 #include "opto/addnode.hpp" 32 #include "opto/castnode.hpp" 33 #include "opto/inlinetypenode.hpp" 34 #include "opto/memnode.hpp" 35 #include "opto/parse.hpp" 36 #include "opto/rootnode.hpp" 37 #include "opto/runtime.hpp" 38 #include "opto/subnode.hpp" 39 #include "runtime/deoptimization.hpp" 40 #include "runtime/handles.inline.hpp" 41 42 //============================================================================= 43 // Helper methods for _get* and _put* bytecodes 44 //============================================================================= 45 46 void Parse::do_field_access(bool is_get, bool is_field) { 47 bool will_link; 48 ciField* field = iter().get_field(will_link); 49 assert(will_link, "getfield: typeflow responsibility"); 50 51 ciInstanceKlass* field_holder = field->holder(); 52 53 if (is_get && is_field && field_holder->is_inlinetype() && peek()->is_InlineType()) { 54 InlineTypeNode* vt = peek()->as_InlineType(); 55 null_check(vt); 56 Node* value = vt->field_value_by_offset(field->offset_in_bytes()); 57 if (value->is_InlineType()) { 58 value = value->as_InlineType()->adjust_scalarization_depth(this); 59 } 60 pop(); 61 push_node(field->layout_type(), value); 62 return; 63 } 64 65 if (is_field == field->is_static()) { 66 // Interpreter will throw java_lang_IncompatibleClassChangeError 67 // Check this before allowing <clinit> methods to access static fields 68 uncommon_trap(Deoptimization::Reason_unhandled, 69 Deoptimization::Action_none); 70 return; 71 } 72 73 // Deoptimize on putfield writes to call site target field outside of CallSite ctor. 74 if (!is_get && field->is_call_site_target() && 75 !(method()->holder() == field_holder && method()->is_object_constructor())) { 76 uncommon_trap(Deoptimization::Reason_unhandled, 77 Deoptimization::Action_reinterpret, 78 nullptr, "put to call site target field"); 79 return; 80 } 81 82 if (C->needs_clinit_barrier(field, method())) { 83 clinit_barrier(field_holder, method()); 84 if (stopped()) return; 85 } 86 87 assert(field->will_link(method(), bc()), "getfield: typeflow responsibility"); 88 89 // Note: We do not check for an unloaded field type here any more. 90 91 // Generate code for the object pointer. 92 Node* obj; 93 if (is_field) { 94 int obj_depth = is_get ? 0 : field->type()->size(); 95 obj = null_check(peek(obj_depth)); 96 // Compile-time detect of null-exception? 97 if (stopped()) return; 98 99 #ifdef ASSERT 100 const TypeInstPtr *tjp = TypeInstPtr::make(TypePtr::NotNull, iter().get_declared_field_holder()); 101 assert(_gvn.type(obj)->higher_equal(tjp), "cast_up is no longer needed"); 102 #endif 103 104 if (is_get) { 105 (void) pop(); // pop receiver before getting 106 do_get_xxx(obj, field); 107 } else { 108 do_put_xxx(obj, field, is_field); 109 if (stopped()) { 110 return; 111 } 112 (void) pop(); // pop receiver after putting 113 } 114 } else { 115 const TypeInstPtr* tip = TypeInstPtr::make(field_holder->java_mirror()); 116 obj = _gvn.makecon(tip); 117 if (is_get) { 118 do_get_xxx(obj, field); 119 } else { 120 do_put_xxx(obj, field, is_field); 121 } 122 } 123 } 124 125 void Parse::do_get_xxx(Node* obj, ciField* field) { 126 BasicType bt = field->layout_type(); 127 // Does this field have a constant value? If so, just push the value. 128 if (field->is_constant() && !field->is_flat() && 129 // Keep consistent with types found by ciTypeFlow: for an 130 // unloaded field type, ciTypeFlow::StateVector::do_getstatic() 131 // speculates the field is null. The code in the rest of this 132 // method does the same. We must not bypass it and use a non 133 // null constant here. 134 (bt != T_OBJECT || field->type()->is_loaded())) { 135 // final or stable field 136 Node* con = make_constant_from_field(field, obj); 137 if (con != nullptr) { 138 push_node(field->layout_type(), con); 139 return; 140 } 141 } 142 143 ciType* field_klass = field->type(); 144 field_klass = improve_abstract_inline_type_klass(field_klass); 145 int offset = field->offset_in_bytes(); 146 bool must_assert_null = false; 147 148 Node* ld = nullptr; 149 if (field->is_null_free() && field_klass->as_inline_klass()->is_empty()) { 150 // Loading from a field of an empty inline type. Just return the default instance. 151 ld = InlineTypeNode::make_default(_gvn, field_klass->as_inline_klass()); 152 } else if (field->is_flat()) { 153 // Loading from a flat inline type field. 154 ld = InlineTypeNode::make_from_flat(this, field_klass->as_inline_klass(), obj, obj, field->holder(), offset); 155 } else { 156 // Build the resultant type of the load 157 const Type* type; 158 if (is_reference_type(bt)) { 159 if (!field_klass->is_loaded()) { 160 type = TypeInstPtr::BOTTOM; 161 must_assert_null = true; 162 } else if (field->is_static_constant()) { 163 // This can happen if the constant oop is non-perm. 164 ciObject* con = field->constant_value().as_object(); 165 // Do not "join" in the previous type; it doesn't add value, 166 // and may yield a vacuous result if the field is of interface type. 167 if (con->is_null_object()) { 168 type = TypePtr::NULL_PTR; 169 } else { 170 type = TypeOopPtr::make_from_constant(con)->isa_oopptr(); 171 } 172 assert(type != nullptr, "field singleton type must be consistent"); 173 } else { 174 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); 175 if (field->is_null_free() && field->is_static()) { 176 // Check if static inline type field is already initialized 177 ciInstance* mirror = field->holder()->java_mirror(); 178 ciObject* val = mirror->field_value(field).as_object(); 179 if (!val->is_null_object()) { 180 type = type->join_speculative(TypePtr::NOTNULL); 181 } 182 } 183 } 184 } else { 185 type = Type::get_const_basic_type(bt); 186 } 187 Node* adr = basic_plus_adr(obj, obj, offset); 188 const TypePtr* adr_type = C->alias_type(field)->adr_type(); 189 DecoratorSet decorators = IN_HEAP; 190 decorators |= field->is_volatile() ? MO_SEQ_CST : MO_UNORDERED; 191 ld = access_load_at(obj, adr, adr_type, type, bt, decorators); 192 if (field_klass->is_inlinetype()) { 193 // Load a non-flattened inline type from memory 194 ld = InlineTypeNode::make_from_oop(this, ld, field_klass->as_inline_klass(), field->is_null_free()); 195 } 196 } 197 198 // Adjust Java stack 199 if (type2size[bt] == 1) 200 push(ld); 201 else 202 push_pair(ld); 203 204 if (must_assert_null) { 205 // Do not take a trap here. It's possible that the program 206 // will never load the field's class, and will happily see 207 // null values in this field forever. Don't stumble into a 208 // trap for such a program, or we might get a long series 209 // of useless recompilations. (Or, we might load a class 210 // which should not be loaded.) If we ever see a non-null 211 // value, we will then trap and recompile. (The trap will 212 // not need to mention the class index, since the class will 213 // already have been loaded if we ever see a non-null value.) 214 // uncommon_trap(iter().get_field_signature_index()); 215 if (PrintOpto && (Verbose || WizardMode)) { 216 method()->print_name(); tty->print_cr(" asserting nullness of field at bci: %d", bci()); 217 } 218 if (C->log() != nullptr) { 219 C->log()->elem("assert_null reason='field' klass='%d'", 220 C->log()->identify(field_klass)); 221 } 222 // If there is going to be a trap, put it at the next bytecode: 223 set_bci(iter().next_bci()); 224 null_assert(peek()); 225 set_bci(iter().cur_bci()); // put it back 226 } 227 } 228 229 // If the field klass is an abstract value klass (for which we do not know the layout, yet), it could have a unique 230 // concrete sub klass for which we have a fixed layout. This allows us to use InlineTypeNodes instead. 231 ciType* Parse::improve_abstract_inline_type_klass(ciType* field_klass) { 232 Dependencies* dependencies = C->dependencies(); 233 if (UseUniqueSubclasses && dependencies != nullptr && field_klass->is_instance_klass()) { 234 ciInstanceKlass* instance_klass = field_klass->as_instance_klass(); 235 if (instance_klass->is_loaded() && instance_klass->is_abstract_value_klass()) { 236 ciInstanceKlass* sub_klass = instance_klass->unique_concrete_subklass(); 237 if (sub_klass != nullptr && sub_klass != field_klass) { 238 field_klass = sub_klass; 239 dependencies->assert_abstract_with_unique_concrete_subtype(instance_klass, sub_klass); 240 } 241 } 242 } 243 return field_klass; 244 } 245 246 void Parse::do_put_xxx(Node* obj, ciField* field, bool is_field) { 247 bool is_vol = field->is_volatile(); 248 int offset = field->offset_in_bytes(); 249 BasicType bt = field->layout_type(); 250 Node* val = type2size[bt] == 1 ? pop() : pop_pair(); 251 252 if (field->is_null_free()) { 253 PreserveReexecuteState preexecs(this); 254 jvms()->set_should_reexecute(true); 255 inc_sp(1); 256 val = null_check(val); 257 if (stopped()) { 258 return; 259 } 260 } 261 if (obj->is_InlineType()) { 262 set_inline_type_field(obj, field, val); 263 return; 264 } 265 if (field->is_null_free() && field->type()->as_inline_klass()->is_empty()) { 266 // Storing to a field of an empty inline type. Ignore. 267 return; 268 } else if (field->is_flat()) { 269 // Storing to a flat inline type field. 270 if (!val->is_InlineType()) { 271 val = InlineTypeNode::make_from_oop(this, val, field->type()->as_inline_klass()); 272 } 273 inc_sp(1); 274 val->as_InlineType()->store_flat(this, obj, obj, field->holder(), offset, IN_HEAP | MO_UNORDERED); 275 dec_sp(1); 276 } else { 277 // Store the value. 278 const Type* field_type; 279 if (!field->type()->is_loaded()) { 280 field_type = TypeInstPtr::BOTTOM; 281 } else { 282 if (is_reference_type(bt)) { 283 field_type = TypeOopPtr::make_from_klass(field->type()->as_klass()); 284 } else { 285 field_type = Type::BOTTOM; 286 } 287 } 288 Node* adr = basic_plus_adr(obj, obj, offset); 289 const TypePtr* adr_type = C->alias_type(field)->adr_type(); 290 DecoratorSet decorators = IN_HEAP; 291 decorators |= is_vol ? MO_SEQ_CST : MO_UNORDERED; 292 inc_sp(1); 293 access_store_at(obj, adr, adr_type, val, field_type, bt, decorators); 294 dec_sp(1); 295 } 296 297 if (is_field) { 298 // Remember we wrote a volatile field. 299 // For not multiple copy atomic cpu (ppc64) a barrier should be issued 300 // in constructors which have such stores. See do_exits() in parse1.cpp. 301 if (is_vol) { 302 set_wrote_volatile(true); 303 } 304 set_wrote_fields(true); 305 306 // If the field is final, the rules of Java say we are in <init> or <clinit>. 307 // If the field is @Stable, we can be in any method, but we only care about 308 // constructors at this point. 309 // 310 // Note the presence of writes to final/@Stable non-static fields, so that we 311 // can insert a memory barrier later on to keep the writes from floating 312 // out of the constructor. 313 if (field->is_final() || field->is_stable()) { 314 if (field->is_final()) { 315 set_wrote_final(true); 316 } 317 if (field->is_stable()) { 318 set_wrote_stable(true); 319 } 320 if (AllocateNode::Ideal_allocation(obj) != nullptr) { 321 // Preserve allocation ptr to create precedent edge to it in membar 322 // generated on exit from constructor. 323 set_alloc_with_final_or_stable(obj); 324 } 325 } 326 } 327 } 328 329 void Parse::set_inline_type_field(Node* obj, ciField* field, Node* val) { 330 assert(_method->is_object_constructor(), "inline type is initialized outside of constructor"); 331 assert(obj->as_InlineType()->is_larval(), "must be larval"); 332 assert(!_gvn.type(obj)->maybe_null(), "should never be null"); 333 334 // Re-execute if buffering in below code triggers deoptimization. 335 PreserveReexecuteState preexecs(this); 336 jvms()->set_should_reexecute(true); 337 inc_sp(1); 338 339 if (!val->is_InlineType() && field->type()->is_inlinetype()) { 340 // Scalarize inline type field value 341 val = InlineTypeNode::make_from_oop(this, val, field->type()->as_inline_klass(), field->is_null_free()); 342 } else if (val->is_InlineType() && !field->is_flat()) { 343 // Field value needs to be allocated because it can be merged with a non-inline type. 344 val = val->as_InlineType()->buffer(this); 345 } 346 347 // Clone the inline type node and set the new field value 348 InlineTypeNode* new_vt = obj->as_InlineType()->clone_if_required(&_gvn, _map); 349 new_vt->set_field_value_by_offset(field->offset_in_bytes(), val); 350 new_vt = new_vt->adjust_scalarization_depth(this); 351 352 // If the inline type is buffered and the caller might use the buffer, update it. 353 if (new_vt->is_allocated(&gvn()) && (!_caller->has_method() || C->inlining_incrementally() || _caller->method()->is_object_constructor())) { 354 new_vt->store(this, new_vt->get_oop(), new_vt->get_oop(), new_vt->bottom_type()->inline_klass(), 0, field->offset_in_bytes()); 355 356 // Preserve allocation ptr to create precedent edge to it in membar 357 // generated on exit from constructor. 358 AllocateNode* alloc = AllocateNode::Ideal_allocation(new_vt->get_oop()); 359 if (alloc != nullptr) { 360 set_alloc_with_final_or_stable(new_vt->get_oop()); 361 } 362 set_wrote_final(true); 363 } 364 365 replace_in_map(obj, _gvn.transform(new_vt)); 366 return; 367 } 368 369 //============================================================================= 370 371 void Parse::do_newarray() { 372 bool will_link; 373 ciKlass* klass = iter().get_klass(will_link); 374 375 // Uncommon Trap when class that array contains is not loaded 376 // we need the loaded class for the rest of graph; do not 377 // initialize the container class (see Java spec)!!! 378 assert(will_link, "newarray: typeflow responsibility"); 379 380 ciArrayKlass* array_klass = ciArrayKlass::make(klass); 381 382 // Check that array_klass object is loaded 383 if (!array_klass->is_loaded()) { 384 // Generate uncommon_trap for unloaded array_class 385 uncommon_trap(Deoptimization::Reason_unloaded, 386 Deoptimization::Action_reinterpret, 387 array_klass); 388 return; 389 } else if (array_klass->element_klass() != nullptr && 390 array_klass->element_klass()->is_inlinetype() && 391 !array_klass->element_klass()->as_inline_klass()->is_initialized()) { 392 uncommon_trap(Deoptimization::Reason_uninitialized, 393 Deoptimization::Action_reinterpret, 394 nullptr); 395 return; 396 } 397 398 kill_dead_locals(); 399 400 const TypeKlassPtr* array_klass_type = TypeKlassPtr::make(array_klass, Type::trust_interfaces); 401 Node* count_val = pop(); 402 Node* obj = new_array(makecon(array_klass_type), count_val, 1); 403 push(obj); 404 } 405 406 407 void Parse::do_newarray(BasicType elem_type) { 408 kill_dead_locals(); 409 410 Node* count_val = pop(); 411 const TypeKlassPtr* array_klass = TypeKlassPtr::make(ciTypeArrayKlass::make(elem_type)); 412 Node* obj = new_array(makecon(array_klass), count_val, 1); 413 // Push resultant oop onto stack 414 push(obj); 415 } 416 417 // Expand simple expressions like new int[3][5] and new Object[2][nonConLen]. 418 // Also handle the degenerate 1-dimensional case of anewarray. 419 Node* Parse::expand_multianewarray(ciArrayKlass* array_klass, Node* *lengths, int ndimensions, int nargs) { 420 Node* length = lengths[0]; 421 assert(length != nullptr, ""); 422 Node* array = new_array(makecon(TypeKlassPtr::make(array_klass, Type::trust_interfaces)), length, nargs); 423 if (ndimensions > 1) { 424 jint length_con = find_int_con(length, -1); 425 guarantee(length_con >= 0, "non-constant multianewarray"); 426 ciArrayKlass* array_klass_1 = array_klass->as_obj_array_klass()->element_klass()->as_array_klass(); 427 const TypePtr* adr_type = TypeAryPtr::OOPS; 428 const TypeOopPtr* elemtype = _gvn.type(array)->is_aryptr()->elem()->make_oopptr(); 429 const intptr_t header = arrayOopDesc::base_offset_in_bytes(T_OBJECT); 430 for (jint i = 0; i < length_con; i++) { 431 Node* elem = expand_multianewarray(array_klass_1, &lengths[1], ndimensions-1, nargs); 432 intptr_t offset = header + ((intptr_t)i << LogBytesPerHeapOop); 433 Node* eaddr = basic_plus_adr(array, offset); 434 access_store_at(array, eaddr, adr_type, elem, elemtype, T_OBJECT, IN_HEAP | IS_ARRAY); 435 } 436 } 437 return array; 438 } 439 440 void Parse::do_multianewarray() { 441 int ndimensions = iter().get_dimensions(); 442 443 // the m-dimensional array 444 bool will_link; 445 ciArrayKlass* array_klass = iter().get_klass(will_link)->as_array_klass(); 446 assert(will_link, "multianewarray: typeflow responsibility"); 447 448 // Note: Array classes are always initialized; no is_initialized check. 449 450 kill_dead_locals(); 451 452 // get the lengths from the stack (first dimension is on top) 453 Node** length = NEW_RESOURCE_ARRAY(Node*, ndimensions + 1); 454 length[ndimensions] = nullptr; // terminating null for make_runtime_call 455 int j; 456 ciKlass* elem_klass = array_klass; 457 for (j = ndimensions-1; j >= 0; j--) { 458 length[j] = pop(); 459 elem_klass = elem_klass->as_array_klass()->element_klass(); 460 } 461 if (elem_klass != nullptr && elem_klass->is_inlinetype() && !elem_klass->as_inline_klass()->is_initialized()) { 462 inc_sp(ndimensions); 463 uncommon_trap(Deoptimization::Reason_uninitialized, 464 Deoptimization::Action_reinterpret, 465 nullptr); 466 return; 467 } 468 469 // The original expression was of this form: new T[length0][length1]... 470 // It is often the case that the lengths are small (except the last). 471 // If that happens, use the fast 1-d creator a constant number of times. 472 const int expand_limit = MIN2((int)MultiArrayExpandLimit, 100); 473 int64_t expand_count = 1; // count of allocations in the expansion 474 int64_t expand_fanout = 1; // running total fanout 475 for (j = 0; j < ndimensions-1; j++) { 476 int dim_con = find_int_con(length[j], -1); 477 // To prevent overflow, we use 64-bit values. Alternatively, 478 // we could clamp dim_con like so: 479 // dim_con = MIN2(dim_con, expand_limit); 480 expand_fanout *= dim_con; 481 expand_count += expand_fanout; // count the level-J sub-arrays 482 if (dim_con <= 0 483 || dim_con > expand_limit 484 || expand_count > expand_limit) { 485 expand_count = 0; 486 break; 487 } 488 } 489 490 // Can use multianewarray instead of [a]newarray if only one dimension, 491 // or if all non-final dimensions are small constants. 492 if (ndimensions == 1 || (1 <= expand_count && expand_count <= expand_limit)) { 493 Node* obj = nullptr; 494 // Set the original stack and the reexecute bit for the interpreter 495 // to reexecute the multianewarray bytecode if deoptimization happens. 496 // Do it unconditionally even for one dimension multianewarray. 497 // Note: the reexecute bit will be set in GraphKit::add_safepoint_edges() 498 // when AllocateArray node for newarray is created. 499 { PreserveReexecuteState preexecs(this); 500 inc_sp(ndimensions); 501 // Pass 0 as nargs since uncommon trap code does not need to restore stack. 502 obj = expand_multianewarray(array_klass, &length[0], ndimensions, 0); 503 } //original reexecute and sp are set back here 504 push(obj); 505 return; 506 } 507 508 address fun = nullptr; 509 switch (ndimensions) { 510 case 1: ShouldNotReachHere(); break; 511 case 2: fun = OptoRuntime::multianewarray2_Java(); break; 512 case 3: fun = OptoRuntime::multianewarray3_Java(); break; 513 case 4: fun = OptoRuntime::multianewarray4_Java(); break; 514 case 5: fun = OptoRuntime::multianewarray5_Java(); break; 515 }; 516 Node* c = nullptr; 517 518 if (fun != nullptr) { 519 c = make_runtime_call(RC_NO_LEAF | RC_NO_IO, 520 OptoRuntime::multianewarray_Type(ndimensions), 521 fun, nullptr, TypeRawPtr::BOTTOM, 522 makecon(TypeKlassPtr::make(array_klass, Type::trust_interfaces)), 523 length[0], length[1], length[2], 524 (ndimensions > 2) ? length[3] : nullptr, 525 (ndimensions > 3) ? length[4] : nullptr); 526 } else { 527 // Create a java array for dimension sizes 528 Node* dims = nullptr; 529 { PreserveReexecuteState preexecs(this); 530 inc_sp(ndimensions); 531 Node* dims_array_klass = makecon(TypeKlassPtr::make(ciArrayKlass::make(ciType::make(T_INT)))); 532 dims = new_array(dims_array_klass, intcon(ndimensions), 0); 533 534 // Fill-in it with values 535 for (j = 0; j < ndimensions; j++) { 536 Node *dims_elem = array_element_address(dims, intcon(j), T_INT); 537 store_to_memory(control(), dims_elem, length[j], T_INT, TypeAryPtr::INTS, MemNode::unordered); 538 } 539 } 540 541 c = make_runtime_call(RC_NO_LEAF | RC_NO_IO, 542 OptoRuntime::multianewarrayN_Type(), 543 OptoRuntime::multianewarrayN_Java(), nullptr, TypeRawPtr::BOTTOM, 544 makecon(TypeKlassPtr::make(array_klass, Type::trust_interfaces)), 545 dims); 546 } 547 make_slow_call_ex(c, env()->Throwable_klass(), false); 548 549 Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms)); 550 551 const Type* type = TypeOopPtr::make_from_klass_raw(array_klass, Type::trust_interfaces); 552 553 // Improve the type: We know it's not null, exact, and of a given length. 554 type = type->is_ptr()->cast_to_ptr_type(TypePtr::NotNull); 555 type = type->is_aryptr()->cast_to_exactness(true); 556 557 const TypeInt* ltype = _gvn.find_int_type(length[0]); 558 if (ltype != nullptr) 559 type = type->is_aryptr()->cast_to_size(ltype); 560 561 // We cannot sharpen the nested sub-arrays, since the top level is mutable. 562 563 Node* cast = _gvn.transform( new CheckCastPPNode(control(), res, type) ); 564 push(cast); 565 566 // Possible improvements: 567 // - Make a fast path for small multi-arrays. (W/ implicit init. loops.) 568 // - Issue CastII against length[*] values, to TypeInt::POS. 569 }