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