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