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