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 "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 (void) pop(); // pop receiver after putting 146 } 147 } else { 148 const TypeInstPtr* tip = TypeInstPtr::make(field_holder->java_mirror()); 149 obj = _gvn.makecon(tip); 150 if (is_get) { 151 do_get_xxx(obj, field, is_field); 152 } else { 153 do_put_xxx(obj, field, is_field); 154 } 155 } 156 } 157 158 void Parse::do_get_xxx(Node* obj, ciField* field, bool is_field) { 159 BasicType bt = field->layout_type(); 160 161 // Does this field have a constant value? If so, just push the value. 162 if (field->is_constant() && 163 // Keep consistent with types found by ciTypeFlow: for an 164 // unloaded field type, ciTypeFlow::StateVector::do_getstatic() 165 // speculates the field is null. The code in the rest of this 166 // method does the same. We must not bypass it and use a non 167 // null constant here. 168 (bt != T_OBJECT || field->type()->is_loaded())) { 169 // final or stable field 170 Node* con = make_constant_from_field(field, obj); 171 if (con != NULL) { 172 push_node(field->layout_type(), con); 173 return; 174 } 175 } 176 177 ciType* field_klass = field->type(); 178 bool is_vol = field->is_volatile(); 179 bool flattened = field->is_flattened(); 180 181 // Compute address and memory type. 182 int offset = field->offset_in_bytes(); 183 const TypePtr* adr_type = C->alias_type(field)->adr_type(); 184 Node *adr = basic_plus_adr(obj, obj, offset); 185 186 // Build the resultant type of the load 187 const Type *type; 188 189 bool must_assert_null = false; 190 if (bt == T_OBJECT || bt == T_VALUETYPE) { 191 if (!field->type()->is_loaded()) { 192 type = TypeInstPtr::BOTTOM; 193 must_assert_null = true; 194 } else if (field->is_static_constant()) { 195 // This can happen if the constant oop is non-perm. 196 ciObject* con = field->constant_value().as_object(); 197 // Do not "join" in the previous type; it doesn't add value, 198 // and may yield a vacuous result if the field is of interface type. 199 if (con->is_null_object()) { 200 type = TypePtr::NULL_PTR; 201 } else { 202 type = TypeOopPtr::make_from_constant(con)->isa_oopptr(); 203 } 204 assert(type != NULL, "field singleton type must be consistent"); 205 } else { 206 type = TypeOopPtr::make_from_klass(field_klass->as_klass()); 207 if (bt == T_VALUETYPE && field->is_static()) { 208 // Check if static value type field is already initialized 209 assert(!flattened, "static fields should not be flattened"); 210 ciInstance* mirror = field->holder()->java_mirror(); 211 ciObject* val = mirror->field_value(field).as_object(); 212 if (!val->is_null_object()) { 213 type = type->join_speculative(TypePtr::NOTNULL); 214 } 215 } 216 } 217 } else { 218 type = Type::get_const_basic_type(bt); 219 } 220 if (support_IRIW_for_not_multiple_copy_atomic_cpu && field->is_volatile()) { 221 insert_mem_bar(Op_MemBarVolatile); // StoreLoad barrier 222 } 223 224 // Build the load. 225 // 226 MemNode::MemOrd mo = is_vol ? MemNode::acquire : MemNode::unordered; 227 bool needs_atomic_access = is_vol || AlwaysAtomicAccesses; 228 Node* ld = NULL; 229 if (flattened) { 230 // Load flattened value type 231 ld = ValueTypeNode::make_from_flattened(this, field_klass->as_value_klass(), obj, obj, field->holder(), offset); 232 } else { 233 ld = make_load(NULL, adr, type, bt, adr_type, mo, LoadNode::DependsOnlyOnTest, needs_atomic_access); 234 } 235 236 // Adjust Java stack 237 if (type2size[bt] == 1) 238 push(ld); 239 else 240 push_pair(ld); 241 242 if (must_assert_null) { 243 // Do not take a trap here. It's possible that the program 244 // will never load the field's class, and will happily see 245 // null values in this field forever. Don't stumble into a 246 // trap for such a program, or we might get a long series 247 // of useless recompilations. (Or, we might load a class 248 // which should not be loaded.) If we ever see a non-null 249 // value, we will then trap and recompile. (The trap will 250 // not need to mention the class index, since the class will 251 // already have been loaded if we ever see a non-null value.) 252 // uncommon_trap(iter().get_field_signature_index()); 253 if (PrintOpto && (Verbose || WizardMode)) { 254 method()->print_name(); tty->print_cr(" asserting nullness of field at bci: %d", bci()); 255 } 256 if (C->log() != NULL) { 257 C->log()->elem("assert_null reason='field' klass='%d'", 258 C->log()->identify(field->type())); 259 } 260 // If there is going to be a trap, put it at the next bytecode: 261 set_bci(iter().next_bci()); 262 null_assert(peek()); 263 set_bci(iter().cur_bci()); // put it back 264 } 265 266 // If reference is volatile, prevent following memory ops from 267 // floating up past the volatile read. Also prevents commoning 268 // another volatile read. 269 if (field->is_volatile()) { 270 // Memory barrier includes bogus read of value to force load BEFORE membar 271 insert_mem_bar(Op_MemBarAcquire, ld); 272 } 273 } 274 275 void Parse::do_put_xxx(Node* obj, ciField* field, bool is_field) { 276 bool is_vol = field->is_volatile(); 277 bool is_flattened = field->is_flattened(); 278 // If reference is volatile, prevent following memory ops from 279 // floating down past the volatile write. Also prevents commoning 280 // another volatile read. 281 if (is_vol) insert_mem_bar(Op_MemBarRelease); 282 283 // Compute address and memory type. 284 int offset = field->offset_in_bytes(); 285 const TypePtr* adr_type = C->alias_type(field)->adr_type(); 286 Node* adr = basic_plus_adr(obj, obj, offset); 287 BasicType bt = field->layout_type(); 288 // Value to be stored 289 Node* val = type2size[bt] == 1 ? pop() : pop_pair(); 290 // Round doubles before storing 291 if (bt == T_DOUBLE) val = dstore_rounding(val); 292 293 // Conservatively release stores of object references. 294 const MemNode::MemOrd mo = 295 is_vol ? 296 // Volatile fields need releasing stores. 297 MemNode::release : 298 // Non-volatile fields also need releasing stores if they hold an 299 // object reference, because the object reference might point to 300 // a freshly created object. 301 StoreNode::release_if_reference(bt); 302 303 // Store the value. 304 if (bt == T_OBJECT || bt == T_VALUETYPE) { 305 const TypeOopPtr* field_type; 306 if (!field->type()->is_loaded()) { 307 field_type = TypeInstPtr::BOTTOM; 308 } else { 309 field_type = TypeOopPtr::make_from_klass(field->type()->as_klass()); 310 } 311 if (is_flattened) { 312 // Store flattened value type to a non-static field 313 assert(bt == T_VALUETYPE, "flattening is only supported for value type fields"); 314 val->as_ValueType()->store_flattened(this, obj, obj, field->holder(), offset); 315 } else { 316 store_oop_to_object(control(), obj, adr, adr_type, val, field_type, bt, mo); 317 } 318 } else { 319 bool needs_atomic_access = is_vol || AlwaysAtomicAccesses; 320 store_to_memory(control(), adr, val, bt, adr_type, mo, needs_atomic_access); 321 } 322 323 // If reference is volatile, prevent following volatiles ops from 324 // floating up before the volatile write. 325 if (is_vol) { 326 // If not multiple copy atomic, we do the MemBarVolatile before the load. 327 if (!support_IRIW_for_not_multiple_copy_atomic_cpu) { 328 insert_mem_bar(Op_MemBarVolatile); // Use fat membar 329 } 330 // Remember we wrote a volatile field. 331 // For not multiple copy atomic cpu (ppc64) a barrier should be issued 332 // in constructors which have such stores. See do_exits() in parse1.cpp. 333 if (is_field) { 334 set_wrote_volatile(true); 335 } 336 } 337 338 if (is_field) { 339 set_wrote_fields(true); 340 } 341 342 // If the field is final, the rules of Java say we are in <init> or <clinit>. 343 // Note the presence of writes to final non-static fields, so that we 344 // can insert a memory barrier later on to keep the writes from floating 345 // out of the constructor. 346 // Any method can write a @Stable field; insert memory barriers after those also. 347 if (is_field && (field->is_final() || field->is_stable())) { 348 if (field->is_final()) { 349 set_wrote_final(true); 350 } 351 if (field->is_stable()) { 352 set_wrote_stable(true); 353 } 354 355 // Preserve allocation ptr to create precedent edge to it in membar 356 // generated on exit from constructor. 357 // Can't bind stable with its allocation, only record allocation for final field. 358 if (field->is_final() && AllocateNode::Ideal_allocation(obj, &_gvn) != NULL) { 359 set_alloc_with_final(obj); 360 } 361 } 362 } 363 364 //============================================================================= 365 366 void Parse::do_newarray() { 367 bool will_link; 368 ciKlass* klass = iter().get_klass(will_link); 369 370 // Uncommon Trap when class that array contains is not loaded 371 // we need the loaded class for the rest of graph; do not 372 // initialize the container class (see Java spec)!!! 373 assert(will_link, "newarray: typeflow responsibility"); 374 375 ciArrayKlass* array_klass = ciArrayKlass::make(klass); 376 // Check that array_klass object is loaded 377 if (!array_klass->is_loaded()) { 378 // Generate uncommon_trap for unloaded array_class 379 uncommon_trap(Deoptimization::Reason_unloaded, 380 Deoptimization::Action_reinterpret, 381 array_klass); 382 return; 383 } else if (array_klass->element_klass() != NULL && 384 array_klass->element_klass()->is_valuetype() && 385 !array_klass->element_klass()->as_value_klass()->is_initialized()) { 386 uncommon_trap(Deoptimization::Reason_uninitialized, 387 Deoptimization::Action_reinterpret, 388 NULL); 389 return; 390 } 391 392 kill_dead_locals(); 393 394 const TypeKlassPtr* array_klass_type = TypeKlassPtr::make(array_klass); 395 Node* count_val = pop(); 396 Node* obj = new_array(makecon(array_klass_type), count_val, 1); 397 push(obj); 398 } 399 400 401 void Parse::do_newarray(BasicType elem_type) { 402 kill_dead_locals(); 403 404 Node* count_val = pop(); 405 const TypeKlassPtr* array_klass = TypeKlassPtr::make(ciTypeArrayKlass::make(elem_type)); 406 Node* obj = new_array(makecon(array_klass), count_val, 1); 407 // Push resultant oop onto stack 408 push(obj); 409 } 410 411 // Expand simple expressions like new int[3][5] and new Object[2][nonConLen]. 412 // Also handle the degenerate 1-dimensional case of anewarray. 413 Node* Parse::expand_multianewarray(ciArrayKlass* array_klass, Node* *lengths, int ndimensions, int nargs) { 414 Node* length = lengths[0]; 415 assert(length != NULL, ""); 416 Node* array = new_array(makecon(TypeKlassPtr::make(array_klass)), length, nargs); 417 if (ndimensions > 1) { 418 jint length_con = find_int_con(length, -1); 419 guarantee(length_con >= 0, "non-constant multianewarray"); 420 ciArrayKlass* array_klass_1 = array_klass->as_obj_array_klass()->element_klass()->as_array_klass(); 421 const TypePtr* adr_type = TypeAryPtr::OOPS; 422 const TypeOopPtr* elemtype = _gvn.type(array)->is_aryptr()->elem()->make_oopptr(); 423 const intptr_t header = arrayOopDesc::base_offset_in_bytes(T_OBJECT); 424 for (jint i = 0; i < length_con; i++) { 425 Node* elem = expand_multianewarray(array_klass_1, &lengths[1], ndimensions-1, nargs); 426 intptr_t offset = header + ((intptr_t)i << LogBytesPerHeapOop); 427 Node* eaddr = basic_plus_adr(array, offset); 428 store_oop_to_array(control(), array, eaddr, adr_type, elem, elemtype, T_OBJECT, MemNode::unordered); 429 } 430 } 431 return array; 432 } 433 434 void Parse::do_multianewarray() { 435 int ndimensions = iter().get_dimensions(); 436 437 // the m-dimensional array 438 bool will_link; 439 ciArrayKlass* array_klass = iter().get_klass(will_link)->as_array_klass(); 440 assert(will_link, "multianewarray: typeflow responsibility"); 441 442 // Note: Array classes are always initialized; no is_initialized check. 443 444 kill_dead_locals(); 445 446 // get the lengths from the stack (first dimension is on top) 447 Node** length = NEW_RESOURCE_ARRAY(Node*, ndimensions + 1); 448 length[ndimensions] = NULL; // terminating null for make_runtime_call 449 int j; 450 for (j = ndimensions-1; j >= 0 ; j--) length[j] = pop(); 451 452 // The original expression was of this form: new T[length0][length1]... 453 // It is often the case that the lengths are small (except the last). 454 // If that happens, use the fast 1-d creator a constant number of times. 455 const int expand_limit = MIN2((int)MultiArrayExpandLimit, 100); 456 int expand_count = 1; // count of allocations in the expansion 457 int expand_fanout = 1; // running total fanout 458 for (j = 0; j < ndimensions-1; j++) { 459 int dim_con = find_int_con(length[j], -1); 460 expand_fanout *= dim_con; 461 expand_count += expand_fanout; // count the level-J sub-arrays 462 if (dim_con <= 0 463 || dim_con > expand_limit 464 || expand_count > expand_limit) { 465 expand_count = 0; 466 break; 467 } 468 } 469 470 // Can use multianewarray instead of [a]newarray if only one dimension, 471 // or if all non-final dimensions are small constants. 472 if (ndimensions == 1 || (1 <= expand_count && expand_count <= expand_limit)) { 473 Node* obj = NULL; 474 // Set the original stack and the reexecute bit for the interpreter 475 // to reexecute the multianewarray bytecode if deoptimization happens. 476 // Do it unconditionally even for one dimension multianewarray. 477 // Note: the reexecute bit will be set in GraphKit::add_safepoint_edges() 478 // when AllocateArray node for newarray is created. 479 { PreserveReexecuteState preexecs(this); 480 inc_sp(ndimensions); 481 // Pass 0 as nargs since uncommon trap code does not need to restore stack. 482 obj = expand_multianewarray(array_klass, &length[0], ndimensions, 0); 483 } //original reexecute and sp are set back here 484 push(obj); 485 return; 486 } 487 488 address fun = NULL; 489 switch (ndimensions) { 490 case 1: ShouldNotReachHere(); break; 491 case 2: fun = OptoRuntime::multianewarray2_Java(); break; 492 case 3: fun = OptoRuntime::multianewarray3_Java(); break; 493 case 4: fun = OptoRuntime::multianewarray4_Java(); break; 494 case 5: fun = OptoRuntime::multianewarray5_Java(); break; 495 }; 496 Node* c = NULL; 497 498 if (fun != NULL) { 499 c = make_runtime_call(RC_NO_LEAF | RC_NO_IO, 500 OptoRuntime::multianewarray_Type(ndimensions), 501 fun, NULL, TypeRawPtr::BOTTOM, 502 makecon(TypeKlassPtr::make(array_klass)), 503 length[0], length[1], length[2], 504 (ndimensions > 2) ? length[3] : NULL, 505 (ndimensions > 3) ? length[4] : NULL); 506 } else { 507 // Create a java array for dimension sizes 508 Node* dims = NULL; 509 { PreserveReexecuteState preexecs(this); 510 inc_sp(ndimensions); 511 Node* dims_array_klass = makecon(TypeKlassPtr::make(ciArrayKlass::make(ciType::make(T_INT)))); 512 dims = new_array(dims_array_klass, intcon(ndimensions), 0); 513 514 // Fill-in it with values 515 for (j = 0; j < ndimensions; j++) { 516 Node *dims_elem = array_element_address(dims, intcon(j), T_INT); 517 store_to_memory(control(), dims_elem, length[j], T_INT, TypeAryPtr::INTS, MemNode::unordered); 518 } 519 } 520 521 c = make_runtime_call(RC_NO_LEAF | RC_NO_IO, 522 OptoRuntime::multianewarrayN_Type(), 523 OptoRuntime::multianewarrayN_Java(), NULL, TypeRawPtr::BOTTOM, 524 makecon(TypeKlassPtr::make(array_klass)), 525 dims); 526 } 527 make_slow_call_ex(c, env()->Throwable_klass(), false); 528 529 Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms)); 530 531 const Type* type = TypeOopPtr::make_from_klass_raw(array_klass); 532 533 // Improve the type: We know it's not null, exact, and of a given length. 534 type = type->is_ptr()->cast_to_ptr_type(TypePtr::NotNull); 535 type = type->is_aryptr()->cast_to_exactness(true); 536 537 const TypeInt* ltype = _gvn.find_int_type(length[0]); 538 if (ltype != NULL) 539 type = type->is_aryptr()->cast_to_size(ltype); 540 541 // We cannot sharpen the nested sub-arrays, since the top level is mutable. 542 543 Node* cast = _gvn.transform( new CheckCastPPNode(control(), res, type) ); 544 push(cast); 545 546 // Possible improvements: 547 // - Make a fast path for small multi-arrays. (W/ implicit init. loops.) 548 // - Issue CastII against length[*] values, to TypeInt::POS. 549 }