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 "ci/ciMethodData.hpp" 27 #include "ci/ciSymbols.hpp" 28 #include "classfile/vmSymbols.hpp" 29 #include "compiler/compileLog.hpp" 30 #include "interpreter/linkResolver.hpp" 31 #include "jvm_io.h" 32 #include "memory/resourceArea.hpp" 33 #include "memory/universe.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "opto/addnode.hpp" 36 #include "opto/castnode.hpp" 37 #include "opto/convertnode.hpp" 38 #include "opto/divnode.hpp" 39 #include "opto/idealGraphPrinter.hpp" 40 #include "opto/idealKit.hpp" 41 #include "opto/inlinetypenode.hpp" 42 #include "opto/matcher.hpp" 43 #include "opto/memnode.hpp" 44 #include "opto/mulnode.hpp" 45 #include "opto/opaquenode.hpp" 46 #include "opto/parse.hpp" 47 #include "opto/runtime.hpp" 48 #include "runtime/deoptimization.hpp" 49 #include "runtime/sharedRuntime.hpp" 50 51 #ifndef PRODUCT 52 extern uint explicit_null_checks_inserted, 53 explicit_null_checks_elided; 54 #endif 55 56 Node* Parse::record_profile_for_speculation_at_array_load(Node* ld) { 57 // Feed unused profile data to type speculation 58 if (UseTypeSpeculation && UseArrayLoadStoreProfile) { 59 ciKlass* array_type = nullptr; 60 ciKlass* element_type = nullptr; 61 ProfilePtrKind element_ptr = ProfileMaybeNull; 62 bool flat_array = true; 63 bool null_free_array = true; 64 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array); 65 if (element_type != nullptr || element_ptr != ProfileMaybeNull) { 66 ld = record_profile_for_speculation(ld, element_type, element_ptr); 67 } 68 } 69 return ld; 70 } 71 72 73 //---------------------------------array_load---------------------------------- 74 void Parse::array_load(BasicType bt) { 75 const Type* elemtype = Type::TOP; 76 Node* adr = array_addressing(bt, 0, elemtype); 77 if (stopped()) return; // guaranteed null or range check 78 79 Node* idx = pop(); 80 Node* ary = pop(); 81 82 // Handle inline type arrays 83 const TypeOopPtr* elemptr = elemtype->make_oopptr(); 84 const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr(); 85 if (ary_t->is_flat()) { 86 // Load from flat inline type array 87 Node* vt = InlineTypeNode::make_from_flat(this, elemtype->inline_klass(), ary, adr); 88 push(vt); 89 return; 90 } else if (ary_t->is_null_free()) { 91 // Load from non-flat inline type array (elements can never be null) 92 bt = T_PRIMITIVE_OBJECT; 93 } else if (!ary_t->is_not_flat()) { 94 // Cannot statically determine if array is a flat array, emit runtime check 95 assert(UseFlatArray && is_reference_type(bt) && elemptr->can_be_inline_type() && !ary_t->klass_is_exact() && !ary_t->is_not_null_free() && 96 (!elemptr->is_inlinetypeptr() || elemptr->inline_klass()->flat_array()), "array can't be flat"); 97 IdealKit ideal(this); 98 IdealVariable res(ideal); 99 ideal.declarations_done(); 100 ideal.if_then(flat_array_test(ary, /* flat = */ false)); { 101 // non-flat array 102 assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found"); 103 sync_kit(ideal); 104 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt); 105 Node* ld = access_load_at(ary, adr, adr_type, elemptr, bt, 106 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD); 107 if (elemptr->is_inlinetypeptr()) { 108 assert(elemptr->maybe_null(), "null free array should be handled above"); 109 ld = InlineTypeNode::make_from_oop(this, ld, elemptr->inline_klass(), false); 110 } 111 ideal.sync_kit(this); 112 ideal.set(res, ld); 113 } ideal.else_(); { 114 // flat array 115 sync_kit(ideal); 116 if (elemptr->is_inlinetypeptr()) { 117 // Element type is known, cast and load from flat representation 118 ciInlineKlass* vk = elemptr->inline_klass(); 119 assert(vk->flat_array() && elemptr->maybe_null(), "never/always flat - should be optimized"); 120 ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* null_free */ true); 121 const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr(); 122 Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, arytype)); 123 Node* casted_adr = array_element_address(cast, idx, T_PRIMITIVE_OBJECT, ary_t->size(), control()); 124 // Re-execute flat array load if buffering triggers deoptimization 125 PreserveReexecuteState preexecs(this); 126 jvms()->set_should_reexecute(true); 127 inc_sp(2); 128 Node* vt = InlineTypeNode::make_from_flat(this, vk, cast, casted_adr)->buffer(this, false); 129 ideal.set(res, vt); 130 ideal.sync_kit(this); 131 } else { 132 // Element type is unknown, emit runtime call 133 134 // Below membars keep this access to an unknown flat array correctly 135 // ordered with other unknown and known flat array accesses. 136 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES)); 137 138 Node* call = nullptr; 139 { 140 // Re-execute flat array load if runtime call triggers deoptimization 141 PreserveReexecuteState preexecs(this); 142 jvms()->set_bci(_bci); 143 jvms()->set_should_reexecute(true); 144 inc_sp(2); 145 kill_dead_locals(); 146 call = make_runtime_call(RC_NO_LEAF | RC_NO_IO, 147 OptoRuntime::load_unknown_inline_type(), 148 OptoRuntime::load_unknown_inline_Java(), 149 nullptr, TypeRawPtr::BOTTOM, 150 ary, idx); 151 } 152 make_slow_call_ex(call, env()->Throwable_klass(), false); 153 Node* buffer = _gvn.transform(new ProjNode(call, TypeFunc::Parms)); 154 155 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES)); 156 157 // Keep track of the information that the inline type is in flat arrays 158 const Type* unknown_value = elemptr->is_instptr()->cast_to_flat_array(); 159 buffer = _gvn.transform(new CheckCastPPNode(control(), buffer, unknown_value)); 160 161 ideal.sync_kit(this); 162 ideal.set(res, buffer); 163 } 164 } ideal.end_if(); 165 sync_kit(ideal); 166 Node* ld = _gvn.transform(ideal.value(res)); 167 ld = record_profile_for_speculation_at_array_load(ld); 168 push_node(bt, ld); 169 return; 170 } 171 172 if (elemtype == TypeInt::BOOL) { 173 bt = T_BOOLEAN; 174 } 175 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt); 176 Node* ld = access_load_at(ary, adr, adr_type, elemtype, bt, 177 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD); 178 ld = record_profile_for_speculation_at_array_load(ld); 179 // Loading an inline type from a non-flat array 180 if (elemptr != nullptr && elemptr->is_inlinetypeptr()) { 181 assert(!ary_t->is_null_free() || !elemptr->maybe_null(), "inline type array elements should never be null"); 182 ld = InlineTypeNode::make_from_oop(this, ld, elemptr->inline_klass(), !elemptr->maybe_null()); 183 } 184 push_node(bt, ld); 185 } 186 187 188 //--------------------------------array_store---------------------------------- 189 void Parse::array_store(BasicType bt) { 190 const Type* elemtype = Type::TOP; 191 Node* adr = array_addressing(bt, type2size[bt], elemtype); 192 if (stopped()) return; // guaranteed null or range check 193 Node* cast_val = nullptr; 194 if (bt == T_OBJECT) { 195 cast_val = array_store_check(adr, elemtype); 196 if (stopped()) return; 197 } 198 Node* val = pop_node(bt); // Value to store 199 Node* idx = pop(); // Index in the array 200 Node* ary = pop(); // The array itself 201 202 const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr(); 203 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt); 204 205 if (elemtype == TypeInt::BOOL) { 206 bt = T_BOOLEAN; 207 } else if (bt == T_OBJECT) { 208 elemtype = elemtype->make_oopptr(); 209 const Type* tval = _gvn.type(cast_val); 210 // Based on the value to be stored, try to determine if the array is not null-free and/or not flat. 211 // This is only legal for non-null stores because the array_store_check always passes for null, even 212 // if the array is null-free. Null stores are handled in GraphKit::gen_inline_array_null_guard(). 213 bool not_null_free = !tval->maybe_null() && !tval->is_oopptr()->can_be_inline_type(); 214 bool not_flat = not_null_free || (tval->is_inlinetypeptr() && !tval->inline_klass()->flat_array()); 215 if (!ary_t->is_not_null_free() && not_null_free) { 216 // Storing a non-inline type, mark array as not null-free (-> not flat). 217 ary_t = ary_t->cast_to_not_null_free(); 218 Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, ary_t)); 219 replace_in_map(ary, cast); 220 ary = cast; 221 } else if (!ary_t->is_not_flat() && not_flat) { 222 // Storing to a non-flat array, mark array as not flat. 223 ary_t = ary_t->cast_to_not_flat(); 224 Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, ary_t)); 225 replace_in_map(ary, cast); 226 ary = cast; 227 } 228 229 if (ary_t->is_flat()) { 230 // Store to flat inline type array 231 assert(!tval->maybe_null(), "should be guaranteed by array store check"); 232 // Re-execute flat array store if buffering triggers deoptimization 233 PreserveReexecuteState preexecs(this); 234 inc_sp(3); 235 jvms()->set_should_reexecute(true); 236 cast_val->as_InlineType()->store_flat(this, ary, adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY); 237 return; 238 } else if (ary_t->is_null_free()) { 239 // Store to non-flat inline type array (elements can never be null) 240 assert(!tval->maybe_null(), "should be guaranteed by array store check"); 241 if (elemtype->inline_klass()->is_empty()) { 242 // Ignore empty inline stores, array is already initialized. 243 return; 244 } 245 } else if (!ary_t->is_not_flat() && (tval != TypePtr::NULL_PTR || StressReflectiveCode)) { 246 // Array might be a flat array, emit runtime checks (for nullptr, a simple inline_array_null_guard is sufficient). 247 assert(UseFlatArray && !not_flat && elemtype->is_oopptr()->can_be_inline_type() && 248 !ary_t->klass_is_exact() && !ary_t->is_not_null_free(), "array can't be a flat array"); 249 IdealKit ideal(this); 250 ideal.if_then(flat_array_test(ary, /* flat = */ false)); { 251 // non-flat array 252 assert(ideal.ctrl()->in(0)->as_If()->is_flat_array_check(&_gvn), "Should be found"); 253 sync_kit(ideal); 254 Node* cast_ary = inline_array_null_guard(ary, cast_val, 3); 255 inc_sp(3); 256 access_store_at(cast_ary, adr, adr_type, cast_val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false); 257 dec_sp(3); 258 ideal.sync_kit(this); 259 } ideal.else_(); { 260 sync_kit(ideal); 261 // flat array 262 Node* null_ctl = top(); 263 Node* val = null_check_oop(cast_val, &null_ctl); 264 if (null_ctl != top()) { 265 PreserveJVMState pjvms(this); 266 inc_sp(3); 267 set_control(null_ctl); 268 uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none); 269 dec_sp(3); 270 } 271 // Try to determine the inline klass 272 ciInlineKlass* vk = nullptr; 273 if (tval->is_inlinetypeptr()) { 274 vk = tval->inline_klass(); 275 } else if (elemtype->is_inlinetypeptr()) { 276 vk = elemtype->inline_klass(); 277 } 278 Node* casted_ary = ary; 279 if (vk != nullptr && !stopped()) { 280 // Element type is known, cast and store to flat representation 281 assert(vk->flat_array() && elemtype->maybe_null(), "never/always flat - should be optimized"); 282 ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* null_free */ true); 283 const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr(); 284 casted_ary = _gvn.transform(new CheckCastPPNode(control(), casted_ary, arytype)); 285 Node* casted_adr = array_element_address(casted_ary, idx, T_OBJECT, arytype->size(), control()); 286 if (!val->is_InlineType()) { 287 assert(!gvn().type(val)->maybe_null(), "inline type array elements should never be null"); 288 val = InlineTypeNode::make_from_oop(this, val, vk); 289 } 290 // Re-execute flat array store if buffering triggers deoptimization 291 PreserveReexecuteState preexecs(this); 292 inc_sp(3); 293 jvms()->set_should_reexecute(true); 294 val->as_InlineType()->store_flat(this, casted_ary, casted_adr, nullptr, 0, MO_UNORDERED | IN_HEAP | IS_ARRAY); 295 } else if (!stopped()) { 296 // Element type is unknown, emit runtime call 297 298 // Below membars keep this access to an unknown flat array correctly 299 // ordered with other unknown and known flat array accesses. 300 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES)); 301 302 make_runtime_call(RC_LEAF, 303 OptoRuntime::store_unknown_inline_type(), 304 CAST_FROM_FN_PTR(address, OptoRuntime::store_unknown_inline), 305 "store_unknown_inline", TypeRawPtr::BOTTOM, 306 val, casted_ary, idx); 307 308 insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::INLINES)); 309 } 310 ideal.sync_kit(this); 311 } 312 ideal.end_if(); 313 sync_kit(ideal); 314 return; 315 } else if (!ary_t->is_not_null_free()) { 316 // Array is not flat but may be null free 317 assert(elemtype->is_oopptr()->can_be_inline_type() && !ary_t->klass_is_exact(), "array can't be null-free"); 318 ary = inline_array_null_guard(ary, cast_val, 3, true); 319 } 320 } 321 inc_sp(3); 322 access_store_at(ary, adr, adr_type, val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY); 323 dec_sp(3); 324 } 325 326 327 //------------------------------array_addressing------------------------------- 328 // Pull array and index from the stack. Compute pointer-to-element. 329 Node* Parse::array_addressing(BasicType type, int vals, const Type*& elemtype) { 330 Node *idx = peek(0+vals); // Get from stack without popping 331 Node *ary = peek(1+vals); // in case of exception 332 333 // Null check the array base, with correct stack contents 334 ary = null_check(ary, T_ARRAY); 335 // Compile-time detect of null-exception? 336 if (stopped()) return top(); 337 338 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr(); 339 const TypeInt* sizetype = arytype->size(); 340 elemtype = arytype->elem(); 341 342 if (UseUniqueSubclasses) { 343 const Type* el = elemtype->make_ptr(); 344 if (el && el->isa_instptr()) { 345 const TypeInstPtr* toop = el->is_instptr(); 346 if (toop->instance_klass()->unique_concrete_subklass()) { 347 // If we load from "AbstractClass[]" we must see "ConcreteSubClass". 348 const Type* subklass = Type::get_const_type(toop->instance_klass()); 349 elemtype = subklass->join_speculative(el); 350 } 351 } 352 } 353 354 // Check for big class initializers with all constant offsets 355 // feeding into a known-size array. 356 const TypeInt* idxtype = _gvn.type(idx)->is_int(); 357 // See if the highest idx value is less than the lowest array bound, 358 // and if the idx value cannot be negative: 359 bool need_range_check = true; 360 if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) { 361 need_range_check = false; 362 if (C->log() != nullptr) C->log()->elem("observe that='!need_range_check'"); 363 } 364 365 if (!arytype->is_loaded()) { 366 // Only fails for some -Xcomp runs 367 // The class is unloaded. We have to run this bytecode in the interpreter. 368 ciKlass* klass = arytype->unloaded_klass(); 369 370 uncommon_trap(Deoptimization::Reason_unloaded, 371 Deoptimization::Action_reinterpret, 372 klass, "!loaded array"); 373 return top(); 374 } 375 376 // Do the range check 377 if (need_range_check) { 378 Node* tst; 379 if (sizetype->_hi <= 0) { 380 // The greatest array bound is negative, so we can conclude that we're 381 // compiling unreachable code, but the unsigned compare trick used below 382 // only works with non-negative lengths. Instead, hack "tst" to be zero so 383 // the uncommon_trap path will always be taken. 384 tst = _gvn.intcon(0); 385 } else { 386 // Range is constant in array-oop, so we can use the original state of mem 387 Node* len = load_array_length(ary); 388 389 // Test length vs index (standard trick using unsigned compare) 390 Node* chk = _gvn.transform( new CmpUNode(idx, len) ); 391 BoolTest::mask btest = BoolTest::lt; 392 tst = _gvn.transform( new BoolNode(chk, btest) ); 393 } 394 RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN); 395 _gvn.set_type(rc, rc->Value(&_gvn)); 396 if (!tst->is_Con()) { 397 record_for_igvn(rc); 398 } 399 set_control(_gvn.transform(new IfTrueNode(rc))); 400 // Branch to failure if out of bounds 401 { 402 PreserveJVMState pjvms(this); 403 set_control(_gvn.transform(new IfFalseNode(rc))); 404 if (C->allow_range_check_smearing()) { 405 // Do not use builtin_throw, since range checks are sometimes 406 // made more stringent by an optimistic transformation. 407 // This creates "tentative" range checks at this point, 408 // which are not guaranteed to throw exceptions. 409 // See IfNode::Ideal, is_range_check, adjust_check. 410 uncommon_trap(Deoptimization::Reason_range_check, 411 Deoptimization::Action_make_not_entrant, 412 nullptr, "range_check"); 413 } else { 414 // If we have already recompiled with the range-check-widening 415 // heroic optimization turned off, then we must really be throwing 416 // range check exceptions. 417 builtin_throw(Deoptimization::Reason_range_check); 418 } 419 } 420 } 421 // Check for always knowing you are throwing a range-check exception 422 if (stopped()) return top(); 423 424 // This could be an access to an inline type array. We can't tell if it's 425 // flat or not. Knowing the exact type avoids runtime checks and leads to 426 // a much simpler graph shape. Check profile information. 427 if (!arytype->is_flat() && !arytype->is_not_flat()) { 428 // First check the speculative type 429 Deoptimization::DeoptReason reason = Deoptimization::Reason_speculate_class_check; 430 ciKlass* array_type = arytype->speculative_type(); 431 if (too_many_traps_or_recompiles(reason) || array_type == nullptr) { 432 // No speculative type, check profile data at this bci 433 array_type = nullptr; 434 reason = Deoptimization::Reason_class_check; 435 if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) { 436 ciKlass* element_type = nullptr; 437 ProfilePtrKind element_ptr = ProfileMaybeNull; 438 bool flat_array = true; 439 bool null_free_array = true; 440 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array); 441 } 442 } 443 if (array_type != nullptr) { 444 // Speculate that this array has the exact type reported by profile data 445 Node* better_ary = nullptr; 446 DEBUG_ONLY(Node* old_control = control();) 447 Node* slow_ctl = type_check_receiver(ary, array_type, 1.0, &better_ary); 448 if (stopped()) { 449 // The check always fails and therefore profile information is incorrect. Don't use it. 450 assert(old_control == slow_ctl, "type check should have been removed"); 451 set_control(slow_ctl); 452 } else if (!slow_ctl->is_top()) { 453 { PreserveJVMState pjvms(this); 454 set_control(slow_ctl); 455 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile); 456 } 457 replace_in_map(ary, better_ary); 458 ary = better_ary; 459 arytype = _gvn.type(ary)->is_aryptr(); 460 elemtype = arytype->elem(); 461 } 462 } 463 } else if (UseTypeSpeculation && UseArrayLoadStoreProfile) { 464 // No need to speculate: feed profile data at this bci for the 465 // array to type speculation 466 ciKlass* array_type = nullptr; 467 ciKlass* element_type = nullptr; 468 ProfilePtrKind element_ptr = ProfileMaybeNull; 469 bool flat_array = true; 470 bool null_free_array = true; 471 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array); 472 if (array_type != nullptr) { 473 ary = record_profile_for_speculation(ary, array_type, ProfileMaybeNull); 474 } 475 } 476 477 // We have no exact array type from profile data. Check profile data 478 // for a non null-free or non flat array. Non null-free implies non 479 // flat so check this one first. Speculating on a non null-free 480 // array doesn't help aaload but could be profitable for a 481 // subsequent aastore. 482 if (!arytype->is_null_free() && !arytype->is_not_null_free()) { 483 bool null_free_array = true; 484 Deoptimization::DeoptReason reason = Deoptimization::Reason_none; 485 if (arytype->speculative() != nullptr && 486 arytype->speculative()->is_aryptr()->is_not_null_free() && 487 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) { 488 null_free_array = false; 489 reason = Deoptimization::Reason_speculate_class_check; 490 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) { 491 ciKlass* array_type = nullptr; 492 ciKlass* element_type = nullptr; 493 ProfilePtrKind element_ptr = ProfileMaybeNull; 494 bool flat_array = true; 495 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array); 496 reason = Deoptimization::Reason_class_check; 497 } 498 if (!null_free_array) { 499 { // Deoptimize if null-free array 500 BuildCutout unless(this, null_free_array_test(load_object_klass(ary), /* null_free = */ false), PROB_MAX); 501 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile); 502 } 503 assert(!stopped(), "null-free array should have been caught earlier"); 504 Node* better_ary = _gvn.transform(new CheckCastPPNode(control(), ary, arytype->cast_to_not_null_free())); 505 replace_in_map(ary, better_ary); 506 ary = better_ary; 507 arytype = _gvn.type(ary)->is_aryptr(); 508 } 509 } 510 511 if (!arytype->is_flat() && !arytype->is_not_flat()) { 512 bool flat_array = true; 513 Deoptimization::DeoptReason reason = Deoptimization::Reason_none; 514 if (arytype->speculative() != nullptr && 515 arytype->speculative()->is_aryptr()->is_not_flat() && 516 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) { 517 flat_array = false; 518 reason = Deoptimization::Reason_speculate_class_check; 519 } else if (UseArrayLoadStoreProfile && !too_many_traps_or_recompiles(reason)) { 520 ciKlass* array_type = nullptr; 521 ciKlass* element_type = nullptr; 522 ProfilePtrKind element_ptr = ProfileMaybeNull; 523 bool null_free_array = true; 524 method()->array_access_profiled_type(bci(), array_type, element_type, element_ptr, flat_array, null_free_array); 525 reason = Deoptimization::Reason_class_check; 526 } 527 if (!flat_array) { 528 { // Deoptimize if flat array 529 BuildCutout unless(this, flat_array_test(ary, /* flat = */ false), PROB_MAX); 530 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile); 531 } 532 assert(!stopped(), "flat array should have been caught earlier"); 533 Node* better_ary = _gvn.transform(new CheckCastPPNode(control(), ary, arytype->cast_to_not_flat())); 534 replace_in_map(ary, better_ary); 535 ary = better_ary; 536 arytype = _gvn.type(ary)->is_aryptr(); 537 } 538 } 539 540 // Make array address computation control dependent to prevent it 541 // from floating above the range check during loop optimizations. 542 Node* ptr = array_element_address(ary, idx, type, sizetype, control()); 543 assert(ptr != top(), "top should go hand-in-hand with stopped"); 544 545 return ptr; 546 } 547 548 549 // returns IfNode 550 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) { 551 Node *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32 552 Node *tst = _gvn.transform(new BoolNode(cmp, mask)); 553 IfNode *iff = create_and_map_if(control(), tst, prob, cnt); 554 return iff; 555 } 556 557 558 // sentinel value for the target bci to mark never taken branches 559 // (according to profiling) 560 static const int never_reached = INT_MAX; 561 562 //------------------------------helper for tableswitch------------------------- 563 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, bool unc) { 564 // True branch, use existing map info 565 { PreserveJVMState pjvms(this); 566 Node *iftrue = _gvn.transform( new IfTrueNode (iff) ); 567 set_control( iftrue ); 568 if (unc) { 569 repush_if_args(); 570 uncommon_trap(Deoptimization::Reason_unstable_if, 571 Deoptimization::Action_reinterpret, 572 nullptr, 573 "taken always"); 574 } else { 575 assert(dest_bci_if_true != never_reached, "inconsistent dest"); 576 merge_new_path(dest_bci_if_true); 577 } 578 } 579 580 // False branch 581 Node *iffalse = _gvn.transform( new IfFalseNode(iff) ); 582 set_control( iffalse ); 583 } 584 585 void Parse::jump_if_false_fork(IfNode *iff, int dest_bci_if_true, bool unc) { 586 // True branch, use existing map info 587 { PreserveJVMState pjvms(this); 588 Node *iffalse = _gvn.transform( new IfFalseNode (iff) ); 589 set_control( iffalse ); 590 if (unc) { 591 repush_if_args(); 592 uncommon_trap(Deoptimization::Reason_unstable_if, 593 Deoptimization::Action_reinterpret, 594 nullptr, 595 "taken never"); 596 } else { 597 assert(dest_bci_if_true != never_reached, "inconsistent dest"); 598 merge_new_path(dest_bci_if_true); 599 } 600 } 601 602 // False branch 603 Node *iftrue = _gvn.transform( new IfTrueNode(iff) ); 604 set_control( iftrue ); 605 } 606 607 void Parse::jump_if_always_fork(int dest_bci, bool unc) { 608 // False branch, use existing map and control() 609 if (unc) { 610 repush_if_args(); 611 uncommon_trap(Deoptimization::Reason_unstable_if, 612 Deoptimization::Action_reinterpret, 613 nullptr, 614 "taken never"); 615 } else { 616 assert(dest_bci != never_reached, "inconsistent dest"); 617 merge_new_path(dest_bci); 618 } 619 } 620 621 622 extern "C" { 623 static int jint_cmp(const void *i, const void *j) { 624 int a = *(jint *)i; 625 int b = *(jint *)j; 626 return a > b ? 1 : a < b ? -1 : 0; 627 } 628 } 629 630 631 class SwitchRange : public StackObj { 632 // a range of integers coupled with a bci destination 633 jint _lo; // inclusive lower limit 634 jint _hi; // inclusive upper limit 635 int _dest; 636 float _cnt; // how many times this range was hit according to profiling 637 638 public: 639 jint lo() const { return _lo; } 640 jint hi() const { return _hi; } 641 int dest() const { return _dest; } 642 bool is_singleton() const { return _lo == _hi; } 643 float cnt() const { return _cnt; } 644 645 void setRange(jint lo, jint hi, int dest, float cnt) { 646 assert(lo <= hi, "must be a non-empty range"); 647 _lo = lo, _hi = hi; _dest = dest; _cnt = cnt; 648 assert(_cnt >= 0, ""); 649 } 650 bool adjoinRange(jint lo, jint hi, int dest, float cnt, bool trim_ranges) { 651 assert(lo <= hi, "must be a non-empty range"); 652 if (lo == _hi+1) { 653 // see merge_ranges() comment below 654 if (trim_ranges) { 655 if (cnt == 0) { 656 if (_cnt != 0) { 657 return false; 658 } 659 if (dest != _dest) { 660 _dest = never_reached; 661 } 662 } else { 663 if (_cnt == 0) { 664 return false; 665 } 666 if (dest != _dest) { 667 return false; 668 } 669 } 670 } else { 671 if (dest != _dest) { 672 return false; 673 } 674 } 675 _hi = hi; 676 _cnt += cnt; 677 return true; 678 } 679 return false; 680 } 681 682 void set (jint value, int dest, float cnt) { 683 setRange(value, value, dest, cnt); 684 } 685 bool adjoin(jint value, int dest, float cnt, bool trim_ranges) { 686 return adjoinRange(value, value, dest, cnt, trim_ranges); 687 } 688 bool adjoin(SwitchRange& other) { 689 return adjoinRange(other._lo, other._hi, other._dest, other._cnt, false); 690 } 691 692 void print() { 693 if (is_singleton()) 694 tty->print(" {%d}=>%d (cnt=%f)", lo(), dest(), cnt()); 695 else if (lo() == min_jint) 696 tty->print(" {..%d}=>%d (cnt=%f)", hi(), dest(), cnt()); 697 else if (hi() == max_jint) 698 tty->print(" {%d..}=>%d (cnt=%f)", lo(), dest(), cnt()); 699 else 700 tty->print(" {%d..%d}=>%d (cnt=%f)", lo(), hi(), dest(), cnt()); 701 } 702 }; 703 704 // We try to minimize the number of ranges and the size of the taken 705 // ones using profiling data. When ranges are created, 706 // SwitchRange::adjoinRange() only allows 2 adjoining ranges to merge 707 // if both were never hit or both were hit to build longer unreached 708 // ranges. Here, we now merge adjoining ranges with the same 709 // destination and finally set destination of unreached ranges to the 710 // special value never_reached because it can help minimize the number 711 // of tests that are necessary. 712 // 713 // For instance: 714 // [0, 1] to target1 sometimes taken 715 // [1, 2] to target1 never taken 716 // [2, 3] to target2 never taken 717 // would lead to: 718 // [0, 1] to target1 sometimes taken 719 // [1, 3] never taken 720 // 721 // (first 2 ranges to target1 are not merged) 722 static void merge_ranges(SwitchRange* ranges, int& rp) { 723 if (rp == 0) { 724 return; 725 } 726 int shift = 0; 727 for (int j = 0; j < rp; j++) { 728 SwitchRange& r1 = ranges[j-shift]; 729 SwitchRange& r2 = ranges[j+1]; 730 if (r1.adjoin(r2)) { 731 shift++; 732 } else if (shift > 0) { 733 ranges[j+1-shift] = r2; 734 } 735 } 736 rp -= shift; 737 for (int j = 0; j <= rp; j++) { 738 SwitchRange& r = ranges[j]; 739 if (r.cnt() == 0 && r.dest() != never_reached) { 740 r.setRange(r.lo(), r.hi(), never_reached, r.cnt()); 741 } 742 } 743 } 744 745 //-------------------------------do_tableswitch-------------------------------- 746 void Parse::do_tableswitch() { 747 // Get information about tableswitch 748 int default_dest = iter().get_dest_table(0); 749 jint lo_index = iter().get_int_table(1); 750 jint hi_index = iter().get_int_table(2); 751 int len = hi_index - lo_index + 1; 752 753 if (len < 1) { 754 // If this is a backward branch, add safepoint 755 maybe_add_safepoint(default_dest); 756 pop(); // the effect of the instruction execution on the operand stack 757 merge(default_dest); 758 return; 759 } 760 761 ciMethodData* methodData = method()->method_data(); 762 ciMultiBranchData* profile = nullptr; 763 if (methodData->is_mature() && UseSwitchProfiling) { 764 ciProfileData* data = methodData->bci_to_data(bci()); 765 if (data != nullptr && data->is_MultiBranchData()) { 766 profile = (ciMultiBranchData*)data; 767 } 768 } 769 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if); 770 771 // generate decision tree, using trichotomy when possible 772 int rnum = len+2; 773 bool makes_backward_branch = (default_dest <= bci()); 774 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum); 775 int rp = -1; 776 if (lo_index != min_jint) { 777 float cnt = 1.0F; 778 if (profile != nullptr) { 779 cnt = (float)profile->default_count() / (hi_index != max_jint ? 2.0F : 1.0F); 780 } 781 ranges[++rp].setRange(min_jint, lo_index-1, default_dest, cnt); 782 } 783 for (int j = 0; j < len; j++) { 784 jint match_int = lo_index+j; 785 int dest = iter().get_dest_table(j+3); 786 makes_backward_branch |= (dest <= bci()); 787 float cnt = 1.0F; 788 if (profile != nullptr) { 789 cnt = (float)profile->count_at(j); 790 } 791 if (rp < 0 || !ranges[rp].adjoin(match_int, dest, cnt, trim_ranges)) { 792 ranges[++rp].set(match_int, dest, cnt); 793 } 794 } 795 jint highest = lo_index+(len-1); 796 assert(ranges[rp].hi() == highest, ""); 797 if (highest != max_jint) { 798 float cnt = 1.0F; 799 if (profile != nullptr) { 800 cnt = (float)profile->default_count() / (lo_index != min_jint ? 2.0F : 1.0F); 801 } 802 if (!ranges[rp].adjoinRange(highest+1, max_jint, default_dest, cnt, trim_ranges)) { 803 ranges[++rp].setRange(highest+1, max_jint, default_dest, cnt); 804 } 805 } 806 assert(rp < len+2, "not too many ranges"); 807 808 if (trim_ranges) { 809 merge_ranges(ranges, rp); 810 } 811 812 // Safepoint in case if backward branch observed 813 if (makes_backward_branch) { 814 add_safepoint(); 815 } 816 817 Node* lookup = pop(); // lookup value 818 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]); 819 } 820 821 822 //------------------------------do_lookupswitch-------------------------------- 823 void Parse::do_lookupswitch() { 824 // Get information about lookupswitch 825 int default_dest = iter().get_dest_table(0); 826 jint len = iter().get_int_table(1); 827 828 if (len < 1) { // If this is a backward branch, add safepoint 829 maybe_add_safepoint(default_dest); 830 pop(); // the effect of the instruction execution on the operand stack 831 merge(default_dest); 832 return; 833 } 834 835 ciMethodData* methodData = method()->method_data(); 836 ciMultiBranchData* profile = nullptr; 837 if (methodData->is_mature() && UseSwitchProfiling) { 838 ciProfileData* data = methodData->bci_to_data(bci()); 839 if (data != nullptr && data->is_MultiBranchData()) { 840 profile = (ciMultiBranchData*)data; 841 } 842 } 843 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if); 844 845 // generate decision tree, using trichotomy when possible 846 jint* table = NEW_RESOURCE_ARRAY(jint, len*3); 847 { 848 for (int j = 0; j < len; j++) { 849 table[3*j+0] = iter().get_int_table(2+2*j); 850 table[3*j+1] = iter().get_dest_table(2+2*j+1); 851 // Handle overflow when converting from uint to jint 852 table[3*j+2] = (profile == nullptr) ? 1 : (jint)MIN2<uint>((uint)max_jint, profile->count_at(j)); 853 } 854 qsort(table, len, 3*sizeof(table[0]), jint_cmp); 855 } 856 857 float default_cnt = 1.0F; 858 if (profile != nullptr) { 859 juint defaults = max_juint - len; 860 default_cnt = (float)profile->default_count()/(float)defaults; 861 } 862 863 int rnum = len*2+1; 864 bool makes_backward_branch = (default_dest <= bci()); 865 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum); 866 int rp = -1; 867 for (int j = 0; j < len; j++) { 868 jint match_int = table[3*j+0]; 869 jint dest = table[3*j+1]; 870 jint cnt = table[3*j+2]; 871 jint next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1; 872 makes_backward_branch |= (dest <= bci()); 873 float c = default_cnt * ((float)match_int - (float)next_lo); 874 if (match_int != next_lo && (rp < 0 || !ranges[rp].adjoinRange(next_lo, match_int-1, default_dest, c, trim_ranges))) { 875 assert(default_dest != never_reached, "sentinel value for dead destinations"); 876 ranges[++rp].setRange(next_lo, match_int-1, default_dest, c); 877 } 878 if (rp < 0 || !ranges[rp].adjoin(match_int, dest, (float)cnt, trim_ranges)) { 879 assert(dest != never_reached, "sentinel value for dead destinations"); 880 ranges[++rp].set(match_int, dest, (float)cnt); 881 } 882 } 883 jint highest = table[3*(len-1)]; 884 assert(ranges[rp].hi() == highest, ""); 885 if (highest != max_jint && 886 !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - (float)highest), trim_ranges)) { 887 ranges[++rp].setRange(highest+1, max_jint, default_dest, default_cnt * ((float)max_jint - (float)highest)); 888 } 889 assert(rp < rnum, "not too many ranges"); 890 891 if (trim_ranges) { 892 merge_ranges(ranges, rp); 893 } 894 895 // Safepoint in case backward branch observed 896 if (makes_backward_branch) { 897 add_safepoint(); 898 } 899 900 Node *lookup = pop(); // lookup value 901 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]); 902 } 903 904 static float if_prob(float taken_cnt, float total_cnt) { 905 assert(taken_cnt <= total_cnt, ""); 906 if (total_cnt == 0) { 907 return PROB_FAIR; 908 } 909 float p = taken_cnt / total_cnt; 910 return clamp(p, PROB_MIN, PROB_MAX); 911 } 912 913 static float if_cnt(float cnt) { 914 if (cnt == 0) { 915 return COUNT_UNKNOWN; 916 } 917 return cnt; 918 } 919 920 static float sum_of_cnts(SwitchRange *lo, SwitchRange *hi) { 921 float total_cnt = 0; 922 for (SwitchRange* sr = lo; sr <= hi; sr++) { 923 total_cnt += sr->cnt(); 924 } 925 return total_cnt; 926 } 927 928 class SwitchRanges : public ResourceObj { 929 public: 930 SwitchRange* _lo; 931 SwitchRange* _hi; 932 SwitchRange* _mid; 933 float _cost; 934 935 enum { 936 Start, 937 LeftDone, 938 RightDone, 939 Done 940 } _state; 941 942 SwitchRanges(SwitchRange *lo, SwitchRange *hi) 943 : _lo(lo), _hi(hi), _mid(nullptr), 944 _cost(0), _state(Start) { 945 } 946 947 SwitchRanges() 948 : _lo(nullptr), _hi(nullptr), _mid(nullptr), 949 _cost(0), _state(Start) {} 950 }; 951 952 // Estimate cost of performing a binary search on lo..hi 953 static float compute_tree_cost(SwitchRange *lo, SwitchRange *hi, float total_cnt) { 954 GrowableArray<SwitchRanges> tree; 955 SwitchRanges root(lo, hi); 956 tree.push(root); 957 958 float cost = 0; 959 do { 960 SwitchRanges& r = *tree.adr_at(tree.length()-1); 961 if (r._hi != r._lo) { 962 if (r._mid == nullptr) { 963 float r_cnt = sum_of_cnts(r._lo, r._hi); 964 965 if (r_cnt == 0) { 966 tree.pop(); 967 cost = 0; 968 continue; 969 } 970 971 SwitchRange* mid = nullptr; 972 mid = r._lo; 973 for (float cnt = 0; ; ) { 974 assert(mid <= r._hi, "out of bounds"); 975 cnt += mid->cnt(); 976 if (cnt > r_cnt / 2) { 977 break; 978 } 979 mid++; 980 } 981 assert(mid <= r._hi, "out of bounds"); 982 r._mid = mid; 983 r._cost = r_cnt / total_cnt; 984 } 985 r._cost += cost; 986 if (r._state < SwitchRanges::LeftDone && r._mid > r._lo) { 987 cost = 0; 988 r._state = SwitchRanges::LeftDone; 989 tree.push(SwitchRanges(r._lo, r._mid-1)); 990 } else if (r._state < SwitchRanges::RightDone) { 991 cost = 0; 992 r._state = SwitchRanges::RightDone; 993 tree.push(SwitchRanges(r._mid == r._lo ? r._mid+1 : r._mid, r._hi)); 994 } else { 995 tree.pop(); 996 cost = r._cost; 997 } 998 } else { 999 tree.pop(); 1000 cost = r._cost; 1001 } 1002 } while (tree.length() > 0); 1003 1004 1005 return cost; 1006 } 1007 1008 // It sometimes pays off to test most common ranges before the binary search 1009 void Parse::linear_search_switch_ranges(Node* key_val, SwitchRange*& lo, SwitchRange*& hi) { 1010 uint nr = hi - lo + 1; 1011 float total_cnt = sum_of_cnts(lo, hi); 1012 1013 float min = compute_tree_cost(lo, hi, total_cnt); 1014 float extra = 1; 1015 float sub = 0; 1016 1017 SwitchRange* array1 = lo; 1018 SwitchRange* array2 = NEW_RESOURCE_ARRAY(SwitchRange, nr); 1019 1020 SwitchRange* ranges = nullptr; 1021 1022 while (nr >= 2) { 1023 assert(lo == array1 || lo == array2, "one the 2 already allocated arrays"); 1024 ranges = (lo == array1) ? array2 : array1; 1025 1026 // Find highest frequency range 1027 SwitchRange* candidate = lo; 1028 for (SwitchRange* sr = lo+1; sr <= hi; sr++) { 1029 if (sr->cnt() > candidate->cnt()) { 1030 candidate = sr; 1031 } 1032 } 1033 SwitchRange most_freq = *candidate; 1034 if (most_freq.cnt() == 0) { 1035 break; 1036 } 1037 1038 // Copy remaining ranges into another array 1039 int shift = 0; 1040 for (uint i = 0; i < nr; i++) { 1041 SwitchRange* sr = &lo[i]; 1042 if (sr != candidate) { 1043 ranges[i-shift] = *sr; 1044 } else { 1045 shift++; 1046 if (i > 0 && i < nr-1) { 1047 SwitchRange prev = lo[i-1]; 1048 prev.setRange(prev.lo(), sr->hi(), prev.dest(), prev.cnt()); 1049 if (prev.adjoin(lo[i+1])) { 1050 shift++; 1051 i++; 1052 } 1053 ranges[i-shift] = prev; 1054 } 1055 } 1056 } 1057 nr -= shift; 1058 1059 // Evaluate cost of testing the most common range and performing a 1060 // binary search on the other ranges 1061 float cost = extra + compute_tree_cost(&ranges[0], &ranges[nr-1], total_cnt); 1062 if (cost >= min) { 1063 break; 1064 } 1065 // swap arrays 1066 lo = &ranges[0]; 1067 hi = &ranges[nr-1]; 1068 1069 // It pays off: emit the test for the most common range 1070 assert(most_freq.cnt() > 0, "must be taken"); 1071 Node* val = _gvn.transform(new SubINode(key_val, _gvn.intcon(most_freq.lo()))); 1072 Node* cmp = _gvn.transform(new CmpUNode(val, _gvn.intcon(java_subtract(most_freq.hi(), most_freq.lo())))); 1073 Node* tst = _gvn.transform(new BoolNode(cmp, BoolTest::le)); 1074 IfNode* iff = create_and_map_if(control(), tst, if_prob(most_freq.cnt(), total_cnt), if_cnt(most_freq.cnt())); 1075 jump_if_true_fork(iff, most_freq.dest(), false); 1076 1077 sub += most_freq.cnt() / total_cnt; 1078 extra += 1 - sub; 1079 min = cost; 1080 } 1081 } 1082 1083 //----------------------------create_jump_tables------------------------------- 1084 bool Parse::create_jump_tables(Node* key_val, SwitchRange* lo, SwitchRange* hi) { 1085 // Are jumptables enabled 1086 if (!UseJumpTables) return false; 1087 1088 // Are jumptables supported 1089 if (!Matcher::has_match_rule(Op_Jump)) return false; 1090 1091 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if); 1092 1093 // Decide if a guard is needed to lop off big ranges at either (or 1094 // both) end(s) of the input set. We'll call this the default target 1095 // even though we can't be sure that it is the true "default". 1096 1097 bool needs_guard = false; 1098 int default_dest; 1099 int64_t total_outlier_size = 0; 1100 int64_t hi_size = ((int64_t)hi->hi()) - ((int64_t)hi->lo()) + 1; 1101 int64_t lo_size = ((int64_t)lo->hi()) - ((int64_t)lo->lo()) + 1; 1102 1103 if (lo->dest() == hi->dest()) { 1104 total_outlier_size = hi_size + lo_size; 1105 default_dest = lo->dest(); 1106 } else if (lo_size > hi_size) { 1107 total_outlier_size = lo_size; 1108 default_dest = lo->dest(); 1109 } else { 1110 total_outlier_size = hi_size; 1111 default_dest = hi->dest(); 1112 } 1113 1114 float total = sum_of_cnts(lo, hi); 1115 float cost = compute_tree_cost(lo, hi, total); 1116 1117 // If a guard test will eliminate very sparse end ranges, then 1118 // it is worth the cost of an extra jump. 1119 float trimmed_cnt = 0; 1120 if (total_outlier_size > (MaxJumpTableSparseness * 4)) { 1121 needs_guard = true; 1122 if (default_dest == lo->dest()) { 1123 trimmed_cnt += lo->cnt(); 1124 lo++; 1125 } 1126 if (default_dest == hi->dest()) { 1127 trimmed_cnt += hi->cnt(); 1128 hi--; 1129 } 1130 } 1131 1132 // Find the total number of cases and ranges 1133 int64_t num_cases = ((int64_t)hi->hi()) - ((int64_t)lo->lo()) + 1; 1134 int num_range = hi - lo + 1; 1135 1136 // Don't create table if: too large, too small, or too sparse. 1137 if (num_cases > MaxJumpTableSize) 1138 return false; 1139 if (UseSwitchProfiling) { 1140 // MinJumpTableSize is set so with a well balanced binary tree, 1141 // when the number of ranges is MinJumpTableSize, it's cheaper to 1142 // go through a JumpNode that a tree of IfNodes. Average cost of a 1143 // tree of IfNodes with MinJumpTableSize is 1144 // log2f(MinJumpTableSize) comparisons. So if the cost computed 1145 // from profile data is less than log2f(MinJumpTableSize) then 1146 // going with the binary search is cheaper. 1147 if (cost < log2f(MinJumpTableSize)) { 1148 return false; 1149 } 1150 } else { 1151 if (num_cases < MinJumpTableSize) 1152 return false; 1153 } 1154 if (num_cases > (MaxJumpTableSparseness * num_range)) 1155 return false; 1156 1157 // Normalize table lookups to zero 1158 int lowval = lo->lo(); 1159 key_val = _gvn.transform( new SubINode(key_val, _gvn.intcon(lowval)) ); 1160 1161 // Generate a guard to protect against input keyvals that aren't 1162 // in the switch domain. 1163 if (needs_guard) { 1164 Node* size = _gvn.intcon(num_cases); 1165 Node* cmp = _gvn.transform(new CmpUNode(key_val, size)); 1166 Node* tst = _gvn.transform(new BoolNode(cmp, BoolTest::ge)); 1167 IfNode* iff = create_and_map_if(control(), tst, if_prob(trimmed_cnt, total), if_cnt(trimmed_cnt)); 1168 jump_if_true_fork(iff, default_dest, trim_ranges && trimmed_cnt == 0); 1169 1170 total -= trimmed_cnt; 1171 } 1172 1173 // Create an ideal node JumpTable that has projections 1174 // of all possible ranges for a switch statement 1175 // The key_val input must be converted to a pointer offset and scaled. 1176 // Compare Parse::array_addressing above. 1177 1178 // Clean the 32-bit int into a real 64-bit offset. 1179 // Otherwise, the jint value 0 might turn into an offset of 0x0800000000. 1180 // Make I2L conversion control dependent to prevent it from 1181 // floating above the range check during loop optimizations. 1182 // Do not use a narrow int type here to prevent the data path from dying 1183 // while the control path is not removed. This can happen if the type of key_val 1184 // is later known to be out of bounds of [0, num_cases] and therefore a narrow cast 1185 // would be replaced by TOP while C2 is not able to fold the corresponding range checks. 1186 // Set _carry_dependency for the cast to avoid being removed by IGVN. 1187 #ifdef _LP64 1188 key_val = C->constrained_convI2L(&_gvn, key_val, TypeInt::INT, control(), true /* carry_dependency */); 1189 #endif 1190 1191 // Shift the value by wordsize so we have an index into the table, rather 1192 // than a switch value 1193 Node *shiftWord = _gvn.MakeConX(wordSize); 1194 key_val = _gvn.transform( new MulXNode( key_val, shiftWord)); 1195 1196 // Create the JumpNode 1197 Arena* arena = C->comp_arena(); 1198 float* probs = (float*)arena->Amalloc(sizeof(float)*num_cases); 1199 int i = 0; 1200 if (total == 0) { 1201 for (SwitchRange* r = lo; r <= hi; r++) { 1202 for (int64_t j = r->lo(); j <= r->hi(); j++, i++) { 1203 probs[i] = 1.0F / num_cases; 1204 } 1205 } 1206 } else { 1207 for (SwitchRange* r = lo; r <= hi; r++) { 1208 float prob = r->cnt()/total; 1209 for (int64_t j = r->lo(); j <= r->hi(); j++, i++) { 1210 probs[i] = prob / (r->hi() - r->lo() + 1); 1211 } 1212 } 1213 } 1214 1215 ciMethodData* methodData = method()->method_data(); 1216 ciMultiBranchData* profile = nullptr; 1217 if (methodData->is_mature()) { 1218 ciProfileData* data = methodData->bci_to_data(bci()); 1219 if (data != nullptr && data->is_MultiBranchData()) { 1220 profile = (ciMultiBranchData*)data; 1221 } 1222 } 1223 1224 Node* jtn = _gvn.transform(new JumpNode(control(), key_val, num_cases, probs, profile == nullptr ? COUNT_UNKNOWN : total)); 1225 1226 // These are the switch destinations hanging off the jumpnode 1227 i = 0; 1228 for (SwitchRange* r = lo; r <= hi; r++) { 1229 for (int64_t j = r->lo(); j <= r->hi(); j++, i++) { 1230 Node* input = _gvn.transform(new JumpProjNode(jtn, i, r->dest(), (int)(j - lowval))); 1231 { 1232 PreserveJVMState pjvms(this); 1233 set_control(input); 1234 jump_if_always_fork(r->dest(), trim_ranges && r->cnt() == 0); 1235 } 1236 } 1237 } 1238 assert(i == num_cases, "miscount of cases"); 1239 stop_and_kill_map(); // no more uses for this JVMS 1240 return true; 1241 } 1242 1243 //----------------------------jump_switch_ranges------------------------------- 1244 void Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, int switch_depth) { 1245 Block* switch_block = block(); 1246 bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if); 1247 1248 if (switch_depth == 0) { 1249 // Do special processing for the top-level call. 1250 assert(lo->lo() == min_jint, "initial range must exhaust Type::INT"); 1251 assert(hi->hi() == max_jint, "initial range must exhaust Type::INT"); 1252 1253 // Decrement pred-numbers for the unique set of nodes. 1254 #ifdef ASSERT 1255 if (!trim_ranges) { 1256 // Ensure that the block's successors are a (duplicate-free) set. 1257 int successors_counted = 0; // block occurrences in [hi..lo] 1258 int unique_successors = switch_block->num_successors(); 1259 for (int i = 0; i < unique_successors; i++) { 1260 Block* target = switch_block->successor_at(i); 1261 1262 // Check that the set of successors is the same in both places. 1263 int successors_found = 0; 1264 for (SwitchRange* p = lo; p <= hi; p++) { 1265 if (p->dest() == target->start()) successors_found++; 1266 } 1267 assert(successors_found > 0, "successor must be known"); 1268 successors_counted += successors_found; 1269 } 1270 assert(successors_counted == (hi-lo)+1, "no unexpected successors"); 1271 } 1272 #endif 1273 1274 // Maybe prune the inputs, based on the type of key_val. 1275 jint min_val = min_jint; 1276 jint max_val = max_jint; 1277 const TypeInt* ti = key_val->bottom_type()->isa_int(); 1278 if (ti != nullptr) { 1279 min_val = ti->_lo; 1280 max_val = ti->_hi; 1281 assert(min_val <= max_val, "invalid int type"); 1282 } 1283 while (lo->hi() < min_val) { 1284 lo++; 1285 } 1286 if (lo->lo() < min_val) { 1287 lo->setRange(min_val, lo->hi(), lo->dest(), lo->cnt()); 1288 } 1289 while (hi->lo() > max_val) { 1290 hi--; 1291 } 1292 if (hi->hi() > max_val) { 1293 hi->setRange(hi->lo(), max_val, hi->dest(), hi->cnt()); 1294 } 1295 1296 linear_search_switch_ranges(key_val, lo, hi); 1297 } 1298 1299 #ifndef PRODUCT 1300 if (switch_depth == 0) { 1301 _max_switch_depth = 0; 1302 _est_switch_depth = log2i_graceful((hi - lo + 1) - 1) + 1; 1303 } 1304 #endif 1305 1306 assert(lo <= hi, "must be a non-empty set of ranges"); 1307 if (lo == hi) { 1308 jump_if_always_fork(lo->dest(), trim_ranges && lo->cnt() == 0); 1309 } else { 1310 assert(lo->hi() == (lo+1)->lo()-1, "contiguous ranges"); 1311 assert(hi->lo() == (hi-1)->hi()+1, "contiguous ranges"); 1312 1313 if (create_jump_tables(key_val, lo, hi)) return; 1314 1315 SwitchRange* mid = nullptr; 1316 float total_cnt = sum_of_cnts(lo, hi); 1317 1318 int nr = hi - lo + 1; 1319 if (UseSwitchProfiling) { 1320 // Don't keep the binary search tree balanced: pick up mid point 1321 // that split frequencies in half. 1322 float cnt = 0; 1323 for (SwitchRange* sr = lo; sr <= hi; sr++) { 1324 cnt += sr->cnt(); 1325 if (cnt >= total_cnt / 2) { 1326 mid = sr; 1327 break; 1328 } 1329 } 1330 } else { 1331 mid = lo + nr/2; 1332 1333 // if there is an easy choice, pivot at a singleton: 1334 if (nr > 3 && !mid->is_singleton() && (mid-1)->is_singleton()) mid--; 1335 1336 assert(lo < mid && mid <= hi, "good pivot choice"); 1337 assert(nr != 2 || mid == hi, "should pick higher of 2"); 1338 assert(nr != 3 || mid == hi-1, "should pick middle of 3"); 1339 } 1340 1341 1342 Node *test_val = _gvn.intcon(mid == lo ? mid->hi() : mid->lo()); 1343 1344 if (mid->is_singleton()) { 1345 IfNode *iff_ne = jump_if_fork_int(key_val, test_val, BoolTest::ne, 1-if_prob(mid->cnt(), total_cnt), if_cnt(mid->cnt())); 1346 jump_if_false_fork(iff_ne, mid->dest(), trim_ranges && mid->cnt() == 0); 1347 1348 // Special Case: If there are exactly three ranges, and the high 1349 // and low range each go to the same place, omit the "gt" test, 1350 // since it will not discriminate anything. 1351 bool eq_test_only = (hi == lo+2 && hi->dest() == lo->dest() && mid == hi-1) || mid == lo; 1352 1353 // if there is a higher range, test for it and process it: 1354 if (mid < hi && !eq_test_only) { 1355 // two comparisons of same values--should enable 1 test for 2 branches 1356 // Use BoolTest::lt instead of BoolTest::gt 1357 float cnt = sum_of_cnts(lo, mid-1); 1358 IfNode *iff_lt = jump_if_fork_int(key_val, test_val, BoolTest::lt, if_prob(cnt, total_cnt), if_cnt(cnt)); 1359 Node *iftrue = _gvn.transform( new IfTrueNode(iff_lt) ); 1360 Node *iffalse = _gvn.transform( new IfFalseNode(iff_lt) ); 1361 { PreserveJVMState pjvms(this); 1362 set_control(iffalse); 1363 jump_switch_ranges(key_val, mid+1, hi, switch_depth+1); 1364 } 1365 set_control(iftrue); 1366 } 1367 1368 } else { 1369 // mid is a range, not a singleton, so treat mid..hi as a unit 1370 float cnt = sum_of_cnts(mid == lo ? mid+1 : mid, hi); 1371 IfNode *iff_ge = jump_if_fork_int(key_val, test_val, mid == lo ? BoolTest::gt : BoolTest::ge, if_prob(cnt, total_cnt), if_cnt(cnt)); 1372 1373 // if there is a higher range, test for it and process it: 1374 if (mid == hi) { 1375 jump_if_true_fork(iff_ge, mid->dest(), trim_ranges && cnt == 0); 1376 } else { 1377 Node *iftrue = _gvn.transform( new IfTrueNode(iff_ge) ); 1378 Node *iffalse = _gvn.transform( new IfFalseNode(iff_ge) ); 1379 { PreserveJVMState pjvms(this); 1380 set_control(iftrue); 1381 jump_switch_ranges(key_val, mid == lo ? mid+1 : mid, hi, switch_depth+1); 1382 } 1383 set_control(iffalse); 1384 } 1385 } 1386 1387 // in any case, process the lower range 1388 if (mid == lo) { 1389 if (mid->is_singleton()) { 1390 jump_switch_ranges(key_val, lo+1, hi, switch_depth+1); 1391 } else { 1392 jump_if_always_fork(lo->dest(), trim_ranges && lo->cnt() == 0); 1393 } 1394 } else { 1395 jump_switch_ranges(key_val, lo, mid-1, switch_depth+1); 1396 } 1397 } 1398 1399 // Decrease pred_count for each successor after all is done. 1400 if (switch_depth == 0) { 1401 int unique_successors = switch_block->num_successors(); 1402 for (int i = 0; i < unique_successors; i++) { 1403 Block* target = switch_block->successor_at(i); 1404 // Throw away the pre-allocated path for each unique successor. 1405 target->next_path_num(); 1406 } 1407 } 1408 1409 #ifndef PRODUCT 1410 _max_switch_depth = MAX2(switch_depth, _max_switch_depth); 1411 if (TraceOptoParse && Verbose && WizardMode && switch_depth == 0) { 1412 SwitchRange* r; 1413 int nsing = 0; 1414 for( r = lo; r <= hi; r++ ) { 1415 if( r->is_singleton() ) nsing++; 1416 } 1417 tty->print(">>> "); 1418 _method->print_short_name(); 1419 tty->print_cr(" switch decision tree"); 1420 tty->print_cr(" %d ranges (%d singletons), max_depth=%d, est_depth=%d", 1421 (int) (hi-lo+1), nsing, _max_switch_depth, _est_switch_depth); 1422 if (_max_switch_depth > _est_switch_depth) { 1423 tty->print_cr("******** BAD SWITCH DEPTH ********"); 1424 } 1425 tty->print(" "); 1426 for( r = lo; r <= hi; r++ ) { 1427 r->print(); 1428 } 1429 tty->cr(); 1430 } 1431 #endif 1432 } 1433 1434 void Parse::modf() { 1435 Node *f2 = pop(); 1436 Node *f1 = pop(); 1437 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::modf_Type(), 1438 CAST_FROM_FN_PTR(address, SharedRuntime::frem), 1439 "frem", nullptr, //no memory effects 1440 f1, f2); 1441 Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0)); 1442 1443 push(res); 1444 } 1445 1446 void Parse::modd() { 1447 Node *d2 = pop_pair(); 1448 Node *d1 = pop_pair(); 1449 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::Math_DD_D_Type(), 1450 CAST_FROM_FN_PTR(address, SharedRuntime::drem), 1451 "drem", nullptr, //no memory effects 1452 d1, top(), d2, top()); 1453 Node* res_d = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0)); 1454 1455 #ifdef ASSERT 1456 Node* res_top = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 1)); 1457 assert(res_top == top(), "second value must be top"); 1458 #endif 1459 1460 push_pair(res_d); 1461 } 1462 1463 void Parse::l2f() { 1464 Node* f2 = pop(); 1465 Node* f1 = pop(); 1466 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::l2f_Type(), 1467 CAST_FROM_FN_PTR(address, SharedRuntime::l2f), 1468 "l2f", nullptr, //no memory effects 1469 f1, f2); 1470 Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0)); 1471 1472 push(res); 1473 } 1474 1475 // Handle jsr and jsr_w bytecode 1476 void Parse::do_jsr() { 1477 assert(bc() == Bytecodes::_jsr || bc() == Bytecodes::_jsr_w, "wrong bytecode"); 1478 1479 // Store information about current state, tagged with new _jsr_bci 1480 int return_bci = iter().next_bci(); 1481 int jsr_bci = (bc() == Bytecodes::_jsr) ? iter().get_dest() : iter().get_far_dest(); 1482 1483 // The way we do things now, there is only one successor block 1484 // for the jsr, because the target code is cloned by ciTypeFlow. 1485 Block* target = successor_for_bci(jsr_bci); 1486 1487 // What got pushed? 1488 const Type* ret_addr = target->peek(); 1489 assert(ret_addr->singleton(), "must be a constant (cloned jsr body)"); 1490 1491 // Effect on jsr on stack 1492 push(_gvn.makecon(ret_addr)); 1493 1494 // Flow to the jsr. 1495 merge(jsr_bci); 1496 } 1497 1498 // Handle ret bytecode 1499 void Parse::do_ret() { 1500 // Find to whom we return. 1501 assert(block()->num_successors() == 1, "a ret can only go one place now"); 1502 Block* target = block()->successor_at(0); 1503 assert(!target->is_ready(), "our arrival must be expected"); 1504 int pnum = target->next_path_num(); 1505 merge_common(target, pnum); 1506 } 1507 1508 static bool has_injected_profile(BoolTest::mask btest, Node* test, int& taken, int& not_taken) { 1509 if (btest != BoolTest::eq && btest != BoolTest::ne) { 1510 // Only ::eq and ::ne are supported for profile injection. 1511 return false; 1512 } 1513 if (test->is_Cmp() && 1514 test->in(1)->Opcode() == Op_ProfileBoolean) { 1515 ProfileBooleanNode* profile = (ProfileBooleanNode*)test->in(1); 1516 int false_cnt = profile->false_count(); 1517 int true_cnt = profile->true_count(); 1518 1519 // Counts matching depends on the actual test operation (::eq or ::ne). 1520 // No need to scale the counts because profile injection was designed 1521 // to feed exact counts into VM. 1522 taken = (btest == BoolTest::eq) ? false_cnt : true_cnt; 1523 not_taken = (btest == BoolTest::eq) ? true_cnt : false_cnt; 1524 1525 profile->consume(); 1526 return true; 1527 } 1528 return false; 1529 } 1530 1531 // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful. 1532 // We also check that individual counters are positive first, otherwise the sum can become positive. 1533 // (check for saturation, integer overflow, and immature counts) 1534 static bool counters_are_meaningful(int counter1, int counter2, int min) { 1535 // check for saturation, including "uint" values too big to fit in "int" 1536 if (counter1 < 0 || counter2 < 0) { 1537 return false; 1538 } 1539 // check for integer overflow of the sum 1540 int64_t sum = (int64_t)counter1 + (int64_t)counter2; 1541 STATIC_ASSERT(sizeof(counter1) < sizeof(sum)); 1542 if (sum > INT_MAX) { 1543 return false; 1544 } 1545 // check if mature 1546 return (counter1 + counter2) >= min; 1547 } 1548 1549 //--------------------------dynamic_branch_prediction-------------------------- 1550 // Try to gather dynamic branch prediction behavior. Return a probability 1551 // of the branch being taken and set the "cnt" field. Returns a -1.0 1552 // if we need to use static prediction for some reason. 1553 float Parse::dynamic_branch_prediction(float &cnt, BoolTest::mask btest, Node* test) { 1554 ResourceMark rm; 1555 1556 cnt = COUNT_UNKNOWN; 1557 1558 int taken = 0; 1559 int not_taken = 0; 1560 1561 bool use_mdo = !has_injected_profile(btest, test, taken, not_taken); 1562 1563 if (use_mdo) { 1564 // Use MethodData information if it is available 1565 // FIXME: free the ProfileData structure 1566 ciMethodData* methodData = method()->method_data(); 1567 if (!methodData->is_mature()) return PROB_UNKNOWN; 1568 ciProfileData* data = methodData->bci_to_data(bci()); 1569 if (data == nullptr) { 1570 return PROB_UNKNOWN; 1571 } 1572 if (!data->is_JumpData()) return PROB_UNKNOWN; 1573 1574 // get taken and not taken values 1575 // NOTE: saturated UINT_MAX values become negative, 1576 // as do counts above INT_MAX. 1577 taken = data->as_JumpData()->taken(); 1578 not_taken = 0; 1579 if (data->is_BranchData()) { 1580 not_taken = data->as_BranchData()->not_taken(); 1581 } 1582 1583 // scale the counts to be commensurate with invocation counts: 1584 // NOTE: overflow for positive values is clamped at INT_MAX 1585 taken = method()->scale_count(taken); 1586 not_taken = method()->scale_count(not_taken); 1587 } 1588 // At this point, saturation or overflow is indicated by INT_MAX 1589 // or a negative value. 1590 1591 // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful. 1592 // We also check that individual counters are positive first, otherwise the sum can become positive. 1593 if (!counters_are_meaningful(taken, not_taken, 40)) { 1594 if (C->log() != nullptr) { 1595 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken); 1596 } 1597 return PROB_UNKNOWN; 1598 } 1599 1600 // Compute frequency that we arrive here 1601 float sum = taken + not_taken; 1602 // Adjust, if this block is a cloned private block but the 1603 // Jump counts are shared. Taken the private counts for 1604 // just this path instead of the shared counts. 1605 if( block()->count() > 0 ) 1606 sum = block()->count(); 1607 cnt = sum / FreqCountInvocations; 1608 1609 // Pin probability to sane limits 1610 float prob; 1611 if( !taken ) 1612 prob = (0+PROB_MIN) / 2; 1613 else if( !not_taken ) 1614 prob = (1+PROB_MAX) / 2; 1615 else { // Compute probability of true path 1616 prob = (float)taken / (float)(taken + not_taken); 1617 if (prob > PROB_MAX) prob = PROB_MAX; 1618 if (prob < PROB_MIN) prob = PROB_MIN; 1619 } 1620 1621 assert((cnt > 0.0f) && (prob > 0.0f), 1622 "Bad frequency assignment in if cnt=%g prob=%g taken=%d not_taken=%d", cnt, prob, taken, not_taken); 1623 1624 if (C->log() != nullptr) { 1625 const char* prob_str = nullptr; 1626 if (prob >= PROB_MAX) prob_str = (prob == PROB_MAX) ? "max" : "always"; 1627 if (prob <= PROB_MIN) prob_str = (prob == PROB_MIN) ? "min" : "never"; 1628 char prob_str_buf[30]; 1629 if (prob_str == nullptr) { 1630 jio_snprintf(prob_str_buf, sizeof(prob_str_buf), "%20.2f", prob); 1631 prob_str = prob_str_buf; 1632 } 1633 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%f' prob='%s'", 1634 iter().get_dest(), taken, not_taken, cnt, prob_str); 1635 } 1636 return prob; 1637 } 1638 1639 //-----------------------------branch_prediction------------------------------- 1640 float Parse::branch_prediction(float& cnt, 1641 BoolTest::mask btest, 1642 int target_bci, 1643 Node* test) { 1644 float prob = dynamic_branch_prediction(cnt, btest, test); 1645 // If prob is unknown, switch to static prediction 1646 if (prob != PROB_UNKNOWN) return prob; 1647 1648 prob = PROB_FAIR; // Set default value 1649 if (btest == BoolTest::eq) // Exactly equal test? 1650 prob = PROB_STATIC_INFREQUENT; // Assume its relatively infrequent 1651 else if (btest == BoolTest::ne) 1652 prob = PROB_STATIC_FREQUENT; // Assume its relatively frequent 1653 1654 // If this is a conditional test guarding a backwards branch, 1655 // assume its a loop-back edge. Make it a likely taken branch. 1656 if (target_bci < bci()) { 1657 if (is_osr_parse()) { // Could be a hot OSR'd loop; force deopt 1658 // Since it's an OSR, we probably have profile data, but since 1659 // branch_prediction returned PROB_UNKNOWN, the counts are too small. 1660 // Let's make a special check here for completely zero counts. 1661 ciMethodData* methodData = method()->method_data(); 1662 if (!methodData->is_empty()) { 1663 ciProfileData* data = methodData->bci_to_data(bci()); 1664 // Only stop for truly zero counts, which mean an unknown part 1665 // of the OSR-ed method, and we want to deopt to gather more stats. 1666 // If you have ANY counts, then this loop is simply 'cold' relative 1667 // to the OSR loop. 1668 if (data == nullptr || 1669 (data->as_BranchData()->taken() + data->as_BranchData()->not_taken() == 0)) { 1670 // This is the only way to return PROB_UNKNOWN: 1671 return PROB_UNKNOWN; 1672 } 1673 } 1674 } 1675 prob = PROB_STATIC_FREQUENT; // Likely to take backwards branch 1676 } 1677 1678 assert(prob != PROB_UNKNOWN, "must have some guess at this point"); 1679 return prob; 1680 } 1681 1682 // The magic constants are chosen so as to match the output of 1683 // branch_prediction() when the profile reports a zero taken count. 1684 // It is important to distinguish zero counts unambiguously, because 1685 // some branches (e.g., _213_javac.Assembler.eliminate) validly produce 1686 // very small but nonzero probabilities, which if confused with zero 1687 // counts would keep the program recompiling indefinitely. 1688 bool Parse::seems_never_taken(float prob) const { 1689 return prob < PROB_MIN; 1690 } 1691 1692 // True if the comparison seems to be the kind that will not change its 1693 // statistics from true to false. See comments in adjust_map_after_if. 1694 // This question is only asked along paths which are already 1695 // classified as untaken (by seems_never_taken), so really, 1696 // if a path is never taken, its controlling comparison is 1697 // already acting in a stable fashion. If the comparison 1698 // seems stable, we will put an expensive uncommon trap 1699 // on the untaken path. 1700 bool Parse::seems_stable_comparison() const { 1701 if (C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if)) { 1702 return false; 1703 } 1704 return true; 1705 } 1706 1707 //-------------------------------repush_if_args-------------------------------- 1708 // Push arguments of an "if" bytecode back onto the stack by adjusting _sp. 1709 inline int Parse::repush_if_args() { 1710 if (PrintOpto && WizardMode) { 1711 tty->print("defending against excessive implicit null exceptions on %s @%d in ", 1712 Bytecodes::name(iter().cur_bc()), iter().cur_bci()); 1713 method()->print_name(); tty->cr(); 1714 } 1715 int bc_depth = - Bytecodes::depth(iter().cur_bc()); 1716 assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches"); 1717 DEBUG_ONLY(sync_jvms()); // argument(n) requires a synced jvms 1718 assert(argument(0) != nullptr, "must exist"); 1719 assert(bc_depth == 1 || argument(1) != nullptr, "two must exist"); 1720 inc_sp(bc_depth); 1721 return bc_depth; 1722 } 1723 1724 //----------------------------------do_ifnull---------------------------------- 1725 void Parse::do_ifnull(BoolTest::mask btest, Node *c) { 1726 int target_bci = iter().get_dest(); 1727 1728 Block* branch_block = successor_for_bci(target_bci); 1729 Block* next_block = successor_for_bci(iter().next_bci()); 1730 1731 float cnt; 1732 float prob = branch_prediction(cnt, btest, target_bci, c); 1733 if (prob == PROB_UNKNOWN) { 1734 // (An earlier version of do_ifnull omitted this trap for OSR methods.) 1735 if (PrintOpto && Verbose) { 1736 tty->print_cr("Never-taken edge stops compilation at bci %d", bci()); 1737 } 1738 repush_if_args(); // to gather stats on loop 1739 uncommon_trap(Deoptimization::Reason_unreached, 1740 Deoptimization::Action_reinterpret, 1741 nullptr, "cold"); 1742 if (C->eliminate_boxing()) { 1743 // Mark the successor blocks as parsed 1744 branch_block->next_path_num(); 1745 next_block->next_path_num(); 1746 } 1747 return; 1748 } 1749 1750 NOT_PRODUCT(explicit_null_checks_inserted++); 1751 1752 // Generate real control flow 1753 Node *tst = _gvn.transform( new BoolNode( c, btest ) ); 1754 1755 // Sanity check the probability value 1756 assert(prob > 0.0f,"Bad probability in Parser"); 1757 // Need xform to put node in hash table 1758 IfNode *iff = create_and_xform_if( control(), tst, prob, cnt ); 1759 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser"); 1760 // True branch 1761 { PreserveJVMState pjvms(this); 1762 Node* iftrue = _gvn.transform( new IfTrueNode (iff) ); 1763 set_control(iftrue); 1764 1765 if (stopped()) { // Path is dead? 1766 NOT_PRODUCT(explicit_null_checks_elided++); 1767 if (C->eliminate_boxing()) { 1768 // Mark the successor block as parsed 1769 branch_block->next_path_num(); 1770 } 1771 } else { // Path is live. 1772 adjust_map_after_if(btest, c, prob, branch_block); 1773 if (!stopped()) { 1774 merge(target_bci); 1775 } 1776 } 1777 } 1778 1779 // False branch 1780 Node* iffalse = _gvn.transform( new IfFalseNode(iff) ); 1781 set_control(iffalse); 1782 1783 if (stopped()) { // Path is dead? 1784 NOT_PRODUCT(explicit_null_checks_elided++); 1785 if (C->eliminate_boxing()) { 1786 // Mark the successor block as parsed 1787 next_block->next_path_num(); 1788 } 1789 } else { // Path is live. 1790 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block); 1791 } 1792 } 1793 1794 //------------------------------------do_if------------------------------------ 1795 void Parse::do_if(BoolTest::mask btest, Node* c, bool new_path, Node** ctrl_taken) { 1796 int target_bci = iter().get_dest(); 1797 1798 Block* branch_block = successor_for_bci(target_bci); 1799 Block* next_block = successor_for_bci(iter().next_bci()); 1800 1801 float cnt; 1802 float prob = branch_prediction(cnt, btest, target_bci, c); 1803 float untaken_prob = 1.0 - prob; 1804 1805 if (prob == PROB_UNKNOWN) { 1806 if (PrintOpto && Verbose) { 1807 tty->print_cr("Never-taken edge stops compilation at bci %d", bci()); 1808 } 1809 repush_if_args(); // to gather stats on loop 1810 uncommon_trap(Deoptimization::Reason_unreached, 1811 Deoptimization::Action_reinterpret, 1812 nullptr, "cold"); 1813 if (C->eliminate_boxing()) { 1814 // Mark the successor blocks as parsed 1815 branch_block->next_path_num(); 1816 next_block->next_path_num(); 1817 } 1818 return; 1819 } 1820 1821 // Sanity check the probability value 1822 assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser"); 1823 1824 bool taken_if_true = true; 1825 // Convert BoolTest to canonical form: 1826 if (!BoolTest(btest).is_canonical()) { 1827 btest = BoolTest(btest).negate(); 1828 taken_if_true = false; 1829 // prob is NOT updated here; it remains the probability of the taken 1830 // path (as opposed to the prob of the path guarded by an 'IfTrueNode'). 1831 } 1832 assert(btest != BoolTest::eq, "!= is the only canonical exact test"); 1833 1834 Node* tst0 = new BoolNode(c, btest); 1835 Node* tst = _gvn.transform(tst0); 1836 BoolTest::mask taken_btest = BoolTest::illegal; 1837 BoolTest::mask untaken_btest = BoolTest::illegal; 1838 1839 if (tst->is_Bool()) { 1840 // Refresh c from the transformed bool node, since it may be 1841 // simpler than the original c. Also re-canonicalize btest. 1842 // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p null)). 1843 // That can arise from statements like: if (x instanceof C) ... 1844 if (tst != tst0) { 1845 // Canonicalize one more time since transform can change it. 1846 btest = tst->as_Bool()->_test._test; 1847 if (!BoolTest(btest).is_canonical()) { 1848 // Reverse edges one more time... 1849 tst = _gvn.transform( tst->as_Bool()->negate(&_gvn) ); 1850 btest = tst->as_Bool()->_test._test; 1851 assert(BoolTest(btest).is_canonical(), "sanity"); 1852 taken_if_true = !taken_if_true; 1853 } 1854 c = tst->in(1); 1855 } 1856 BoolTest::mask neg_btest = BoolTest(btest).negate(); 1857 taken_btest = taken_if_true ? btest : neg_btest; 1858 untaken_btest = taken_if_true ? neg_btest : btest; 1859 } 1860 1861 // Generate real control flow 1862 float true_prob = (taken_if_true ? prob : untaken_prob); 1863 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt); 1864 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser"); 1865 Node* taken_branch = new IfTrueNode(iff); 1866 Node* untaken_branch = new IfFalseNode(iff); 1867 if (!taken_if_true) { // Finish conversion to canonical form 1868 Node* tmp = taken_branch; 1869 taken_branch = untaken_branch; 1870 untaken_branch = tmp; 1871 } 1872 1873 // Branch is taken: 1874 { PreserveJVMState pjvms(this); 1875 taken_branch = _gvn.transform(taken_branch); 1876 set_control(taken_branch); 1877 1878 if (stopped()) { 1879 if (C->eliminate_boxing() && !new_path) { 1880 // Mark the successor block as parsed (if we haven't created a new path) 1881 branch_block->next_path_num(); 1882 } 1883 } else { 1884 adjust_map_after_if(taken_btest, c, prob, branch_block); 1885 if (!stopped()) { 1886 if (new_path) { 1887 // Merge by using a new path 1888 merge_new_path(target_bci); 1889 } else if (ctrl_taken != nullptr) { 1890 // Don't merge but save taken branch to be wired by caller 1891 *ctrl_taken = control(); 1892 } else { 1893 merge(target_bci); 1894 } 1895 } 1896 } 1897 } 1898 1899 untaken_branch = _gvn.transform(untaken_branch); 1900 set_control(untaken_branch); 1901 1902 // Branch not taken. 1903 if (stopped() && ctrl_taken == nullptr) { 1904 if (C->eliminate_boxing()) { 1905 // Mark the successor block as parsed (if caller does not re-wire control flow) 1906 next_block->next_path_num(); 1907 } 1908 } else { 1909 adjust_map_after_if(untaken_btest, c, untaken_prob, next_block); 1910 } 1911 } 1912 1913 1914 static ProfilePtrKind speculative_ptr_kind(const TypeOopPtr* t) { 1915 if (t->speculative() == nullptr) { 1916 return ProfileUnknownNull; 1917 } 1918 if (t->speculative_always_null()) { 1919 return ProfileAlwaysNull; 1920 } 1921 if (t->speculative_maybe_null()) { 1922 return ProfileMaybeNull; 1923 } 1924 return ProfileNeverNull; 1925 } 1926 1927 void Parse::acmp_always_null_input(Node* input, const TypeOopPtr* tinput, BoolTest::mask btest, Node* eq_region) { 1928 inc_sp(2); 1929 Node* cast = null_check_common(input, T_OBJECT, true, nullptr, 1930 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check) && 1931 speculative_ptr_kind(tinput) == ProfileAlwaysNull); 1932 dec_sp(2); 1933 if (btest == BoolTest::ne) { 1934 { 1935 PreserveJVMState pjvms(this); 1936 replace_in_map(input, cast); 1937 int target_bci = iter().get_dest(); 1938 merge(target_bci); 1939 } 1940 record_for_igvn(eq_region); 1941 set_control(_gvn.transform(eq_region)); 1942 } else { 1943 replace_in_map(input, cast); 1944 } 1945 } 1946 1947 Node* Parse::acmp_null_check(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, Node*& null_ctl) { 1948 inc_sp(2); 1949 null_ctl = top(); 1950 Node* cast = null_check_oop(input, &null_ctl, 1951 input_ptr == ProfileNeverNull || (input_ptr == ProfileUnknownNull && !too_many_traps_or_recompiles(Deoptimization::Reason_null_check)), 1952 false, 1953 speculative_ptr_kind(tinput) == ProfileNeverNull && 1954 !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)); 1955 dec_sp(2); 1956 assert(!stopped(), "null input should have been caught earlier"); 1957 return cast; 1958 } 1959 1960 void Parse::acmp_known_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, ciKlass* input_type, BoolTest::mask btest, Node* eq_region) { 1961 Node* ne_region = new RegionNode(1); 1962 Node* null_ctl; 1963 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl); 1964 ne_region->add_req(null_ctl); 1965 1966 Node* slow_ctl = type_check_receiver(cast, input_type, 1.0, &cast); 1967 { 1968 PreserveJVMState pjvms(this); 1969 inc_sp(2); 1970 set_control(slow_ctl); 1971 Deoptimization::DeoptReason reason; 1972 if (tinput->speculative_type() != nullptr && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) { 1973 reason = Deoptimization::Reason_speculate_class_check; 1974 } else { 1975 reason = Deoptimization::Reason_class_check; 1976 } 1977 uncommon_trap_exact(reason, Deoptimization::Action_maybe_recompile); 1978 } 1979 ne_region->add_req(control()); 1980 1981 record_for_igvn(ne_region); 1982 set_control(_gvn.transform(ne_region)); 1983 if (btest == BoolTest::ne) { 1984 { 1985 PreserveJVMState pjvms(this); 1986 if (null_ctl == top()) { 1987 replace_in_map(input, cast); 1988 } 1989 int target_bci = iter().get_dest(); 1990 merge(target_bci); 1991 } 1992 record_for_igvn(eq_region); 1993 set_control(_gvn.transform(eq_region)); 1994 } else { 1995 if (null_ctl == top()) { 1996 replace_in_map(input, cast); 1997 } 1998 set_control(_gvn.transform(ne_region)); 1999 } 2000 } 2001 2002 void Parse::acmp_unknown_non_inline_type_input(Node* input, const TypeOopPtr* tinput, ProfilePtrKind input_ptr, BoolTest::mask btest, Node* eq_region) { 2003 Node* ne_region = new RegionNode(1); 2004 Node* null_ctl; 2005 Node* cast = acmp_null_check(input, tinput, input_ptr, null_ctl); 2006 ne_region->add_req(null_ctl); 2007 2008 { 2009 BuildCutout unless(this, inline_type_test(cast, /* is_inline = */ false), PROB_MAX); 2010 inc_sp(2); 2011 uncommon_trap_exact(Deoptimization::Reason_class_check, Deoptimization::Action_maybe_recompile); 2012 } 2013 2014 ne_region->add_req(control()); 2015 2016 record_for_igvn(ne_region); 2017 set_control(_gvn.transform(ne_region)); 2018 if (btest == BoolTest::ne) { 2019 { 2020 PreserveJVMState pjvms(this); 2021 if (null_ctl == top()) { 2022 replace_in_map(input, cast); 2023 } 2024 int target_bci = iter().get_dest(); 2025 merge(target_bci); 2026 } 2027 record_for_igvn(eq_region); 2028 set_control(_gvn.transform(eq_region)); 2029 } else { 2030 if (null_ctl == top()) { 2031 replace_in_map(input, cast); 2032 } 2033 set_control(_gvn.transform(ne_region)); 2034 } 2035 } 2036 2037 void Parse::do_acmp(BoolTest::mask btest, Node* left, Node* right) { 2038 ciKlass* left_type = nullptr; 2039 ciKlass* right_type = nullptr; 2040 ProfilePtrKind left_ptr = ProfileUnknownNull; 2041 ProfilePtrKind right_ptr = ProfileUnknownNull; 2042 bool left_inline_type = true; 2043 bool right_inline_type = true; 2044 2045 // Leverage profiling at acmp 2046 if (UseACmpProfile) { 2047 method()->acmp_profiled_type(bci(), left_type, right_type, left_ptr, right_ptr, left_inline_type, right_inline_type); 2048 if (too_many_traps_or_recompiles(Deoptimization::Reason_class_check)) { 2049 left_type = nullptr; 2050 right_type = nullptr; 2051 left_inline_type = true; 2052 right_inline_type = true; 2053 } 2054 if (too_many_traps_or_recompiles(Deoptimization::Reason_null_check)) { 2055 left_ptr = ProfileUnknownNull; 2056 right_ptr = ProfileUnknownNull; 2057 } 2058 } 2059 2060 if (UseTypeSpeculation) { 2061 record_profile_for_speculation(left, left_type, left_ptr); 2062 record_profile_for_speculation(right, right_type, right_ptr); 2063 } 2064 2065 if (!EnableValhalla) { 2066 Node* cmp = CmpP(left, right); 2067 cmp = optimize_cmp_with_klass(cmp); 2068 do_if(btest, cmp); 2069 return; 2070 } 2071 2072 // Check for equality before potentially allocating 2073 if (left == right) { 2074 do_if(btest, makecon(TypeInt::CC_EQ)); 2075 return; 2076 } 2077 2078 // Allocate inline type operands and re-execute on deoptimization 2079 if (left->is_InlineType()) { 2080 if (_gvn.type(right)->is_zero_type() || 2081 (right->is_InlineType() && _gvn.type(right->as_InlineType()->get_is_init())->is_zero_type())) { 2082 // Null checking a scalarized but nullable inline type. Check the IsInit 2083 // input instead of the oop input to avoid keeping buffer allocations alive. 2084 Node* cmp = CmpI(left->as_InlineType()->get_is_init(), intcon(0)); 2085 do_if(btest, cmp); 2086 return; 2087 } else { 2088 PreserveReexecuteState preexecs(this); 2089 inc_sp(2); 2090 jvms()->set_should_reexecute(true); 2091 left = left->as_InlineType()->buffer(this)->get_oop(); 2092 } 2093 } 2094 if (right->is_InlineType()) { 2095 PreserveReexecuteState preexecs(this); 2096 inc_sp(2); 2097 jvms()->set_should_reexecute(true); 2098 right = right->as_InlineType()->buffer(this)->get_oop(); 2099 } 2100 2101 // First, do a normal pointer comparison 2102 const TypeOopPtr* tleft = _gvn.type(left)->isa_oopptr(); 2103 const TypeOopPtr* tright = _gvn.type(right)->isa_oopptr(); 2104 Node* cmp = CmpP(left, right); 2105 cmp = optimize_cmp_with_klass(cmp); 2106 if (tleft == nullptr || !tleft->can_be_inline_type() || 2107 tright == nullptr || !tright->can_be_inline_type()) { 2108 // This is sufficient, if one of the operands can't be an inline type 2109 do_if(btest, cmp); 2110 return; 2111 } 2112 Node* eq_region = nullptr; 2113 if (btest == BoolTest::eq) { 2114 do_if(btest, cmp, true); 2115 if (stopped()) { 2116 return; 2117 } 2118 } else { 2119 assert(btest == BoolTest::ne, "only eq or ne"); 2120 Node* is_not_equal = nullptr; 2121 eq_region = new RegionNode(3); 2122 { 2123 PreserveJVMState pjvms(this); 2124 do_if(btest, cmp, false, &is_not_equal); 2125 if (!stopped()) { 2126 eq_region->init_req(1, control()); 2127 } 2128 } 2129 if (is_not_equal == nullptr || is_not_equal->is_top()) { 2130 record_for_igvn(eq_region); 2131 set_control(_gvn.transform(eq_region)); 2132 return; 2133 } 2134 set_control(is_not_equal); 2135 } 2136 2137 // Prefer speculative types if available 2138 if (!too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) { 2139 if (tleft->speculative_type() != nullptr) { 2140 left_type = tleft->speculative_type(); 2141 } 2142 if (tright->speculative_type() != nullptr) { 2143 right_type = tright->speculative_type(); 2144 } 2145 } 2146 2147 if (speculative_ptr_kind(tleft) != ProfileMaybeNull && speculative_ptr_kind(tleft) != ProfileUnknownNull) { 2148 ProfilePtrKind speculative_left_ptr = speculative_ptr_kind(tleft); 2149 if (speculative_left_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) { 2150 left_ptr = speculative_left_ptr; 2151 } else if (speculative_left_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) { 2152 left_ptr = speculative_left_ptr; 2153 } 2154 } 2155 if (speculative_ptr_kind(tright) != ProfileMaybeNull && speculative_ptr_kind(tright) != ProfileUnknownNull) { 2156 ProfilePtrKind speculative_right_ptr = speculative_ptr_kind(tright); 2157 if (speculative_right_ptr == ProfileAlwaysNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_assert)) { 2158 right_ptr = speculative_right_ptr; 2159 } else if (speculative_right_ptr == ProfileNeverNull && !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_null_check)) { 2160 right_ptr = speculative_right_ptr; 2161 } 2162 } 2163 2164 if (left_ptr == ProfileAlwaysNull) { 2165 // Comparison with null. Assert the input is indeed null and we're done. 2166 acmp_always_null_input(left, tleft, btest, eq_region); 2167 return; 2168 } 2169 if (right_ptr == ProfileAlwaysNull) { 2170 // Comparison with null. Assert the input is indeed null and we're done. 2171 acmp_always_null_input(right, tright, btest, eq_region); 2172 return; 2173 } 2174 if (left_type != nullptr && !left_type->is_inlinetype()) { 2175 // Comparison with an object of known type 2176 acmp_known_non_inline_type_input(left, tleft, left_ptr, left_type, btest, eq_region); 2177 return; 2178 } 2179 if (right_type != nullptr && !right_type->is_inlinetype()) { 2180 // Comparison with an object of known type 2181 acmp_known_non_inline_type_input(right, tright, right_ptr, right_type, btest, eq_region); 2182 return; 2183 } 2184 if (!left_inline_type) { 2185 // Comparison with an object known not to be an inline type 2186 acmp_unknown_non_inline_type_input(left, tleft, left_ptr, btest, eq_region); 2187 return; 2188 } 2189 if (!right_inline_type) { 2190 // Comparison with an object known not to be an inline type 2191 acmp_unknown_non_inline_type_input(right, tright, right_ptr, btest, eq_region); 2192 return; 2193 } 2194 2195 // Pointers are not equal, check if first operand is non-null 2196 Node* ne_region = new RegionNode(6); 2197 Node* null_ctl; 2198 Node* not_null_right = acmp_null_check(right, tright, right_ptr, null_ctl); 2199 ne_region->init_req(1, null_ctl); 2200 2201 // First operand is non-null, check if it is an inline type 2202 Node* is_value = inline_type_test(not_null_right); 2203 IfNode* is_value_iff = create_and_map_if(control(), is_value, PROB_FAIR, COUNT_UNKNOWN); 2204 Node* not_value = _gvn.transform(new IfFalseNode(is_value_iff)); 2205 ne_region->init_req(2, not_value); 2206 set_control(_gvn.transform(new IfTrueNode(is_value_iff))); 2207 2208 // The first operand is an inline type, check if the second operand is non-null 2209 Node* not_null_left = acmp_null_check(left, tleft, left_ptr, null_ctl); 2210 ne_region->init_req(3, null_ctl); 2211 2212 // Check if both operands are of the same class. 2213 Node* kls_left = load_object_klass(not_null_left); 2214 Node* kls_right = load_object_klass(not_null_right); 2215 Node* kls_cmp = CmpP(kls_left, kls_right); 2216 Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne)); 2217 IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN); 2218 Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff)); 2219 set_control(_gvn.transform(new IfFalseNode(kls_iff))); 2220 ne_region->init_req(4, kls_ne); 2221 2222 if (stopped()) { 2223 record_for_igvn(ne_region); 2224 set_control(_gvn.transform(ne_region)); 2225 if (btest == BoolTest::ne) { 2226 { 2227 PreserveJVMState pjvms(this); 2228 int target_bci = iter().get_dest(); 2229 merge(target_bci); 2230 } 2231 record_for_igvn(eq_region); 2232 set_control(_gvn.transform(eq_region)); 2233 } 2234 return; 2235 } 2236 2237 // Both operands are values types of the same class, we need to perform a 2238 // substitutability test. Delegate to ValueObjectMethods::isSubstitutable(). 2239 Node* ne_io_phi = PhiNode::make(ne_region, i_o()); 2240 Node* mem = reset_memory(); 2241 Node* ne_mem_phi = PhiNode::make(ne_region, mem); 2242 2243 Node* eq_io_phi = nullptr; 2244 Node* eq_mem_phi = nullptr; 2245 if (eq_region != nullptr) { 2246 eq_io_phi = PhiNode::make(eq_region, i_o()); 2247 eq_mem_phi = PhiNode::make(eq_region, mem); 2248 } 2249 2250 set_all_memory(mem); 2251 2252 kill_dead_locals(); 2253 ciMethod* subst_method = ciEnv::current()->ValueObjectMethods_klass()->find_method(ciSymbols::isSubstitutable_name(), ciSymbols::object_object_boolean_signature()); 2254 CallStaticJavaNode *call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method); 2255 call->set_override_symbolic_info(true); 2256 call->init_req(TypeFunc::Parms, not_null_left); 2257 call->init_req(TypeFunc::Parms+1, not_null_right); 2258 inc_sp(2); 2259 set_edges_for_java_call(call, false, false); 2260 Node* ret = set_results_for_java_call(call, false, true); 2261 dec_sp(2); 2262 2263 // Test the return value of ValueObjectMethods::isSubstitutable() 2264 Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1))); 2265 Node* ctl = C->top(); 2266 if (btest == BoolTest::eq) { 2267 PreserveJVMState pjvms(this); 2268 do_if(btest, subst_cmp); 2269 if (!stopped()) { 2270 ctl = control(); 2271 } 2272 } else { 2273 assert(btest == BoolTest::ne, "only eq or ne"); 2274 PreserveJVMState pjvms(this); 2275 do_if(btest, subst_cmp, false, &ctl); 2276 if (!stopped()) { 2277 eq_region->init_req(2, control()); 2278 eq_io_phi->init_req(2, i_o()); 2279 eq_mem_phi->init_req(2, reset_memory()); 2280 } 2281 } 2282 ne_region->init_req(5, ctl); 2283 ne_io_phi->init_req(5, i_o()); 2284 ne_mem_phi->init_req(5, reset_memory()); 2285 2286 record_for_igvn(ne_region); 2287 set_control(_gvn.transform(ne_region)); 2288 set_i_o(_gvn.transform(ne_io_phi)); 2289 set_all_memory(_gvn.transform(ne_mem_phi)); 2290 2291 if (btest == BoolTest::ne) { 2292 { 2293 PreserveJVMState pjvms(this); 2294 int target_bci = iter().get_dest(); 2295 merge(target_bci); 2296 } 2297 2298 record_for_igvn(eq_region); 2299 set_control(_gvn.transform(eq_region)); 2300 set_i_o(_gvn.transform(eq_io_phi)); 2301 set_all_memory(_gvn.transform(eq_mem_phi)); 2302 } 2303 } 2304 2305 bool Parse::path_is_suitable_for_uncommon_trap(float prob) const { 2306 // Don't want to speculate on uncommon traps when running with -Xcomp 2307 if (!UseInterpreter) { 2308 return false; 2309 } 2310 return (seems_never_taken(prob) && seems_stable_comparison()); 2311 } 2312 2313 void Parse::maybe_add_predicate_after_if(Block* path) { 2314 if (path->is_SEL_head() && path->preds_parsed() == 0) { 2315 // Add predicates at bci of if dominating the loop so traps can be 2316 // recorded on the if's profile data 2317 int bc_depth = repush_if_args(); 2318 add_parse_predicates(); 2319 dec_sp(bc_depth); 2320 path->set_has_predicates(); 2321 } 2322 } 2323 2324 2325 //----------------------------adjust_map_after_if------------------------------ 2326 // Adjust the JVM state to reflect the result of taking this path. 2327 // Basically, it means inspecting the CmpNode controlling this 2328 // branch, seeing how it constrains a tested value, and then 2329 // deciding if it's worth our while to encode this constraint 2330 // as graph nodes in the current abstract interpretation map. 2331 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path) { 2332 if (!c->is_Cmp()) { 2333 maybe_add_predicate_after_if(path); 2334 return; 2335 } 2336 2337 if (stopped() || btest == BoolTest::illegal) { 2338 return; // nothing to do 2339 } 2340 2341 bool is_fallthrough = (path == successor_for_bci(iter().next_bci())); 2342 2343 if (path_is_suitable_for_uncommon_trap(prob)) { 2344 repush_if_args(); 2345 Node* call = uncommon_trap(Deoptimization::Reason_unstable_if, 2346 Deoptimization::Action_reinterpret, 2347 nullptr, 2348 (is_fallthrough ? "taken always" : "taken never")); 2349 2350 if (call != nullptr) { 2351 C->record_unstable_if_trap(new UnstableIfTrap(call->as_CallStaticJava(), path)); 2352 } 2353 return; 2354 } 2355 2356 Node* val = c->in(1); 2357 Node* con = c->in(2); 2358 const Type* tcon = _gvn.type(con); 2359 const Type* tval = _gvn.type(val); 2360 bool have_con = tcon->singleton(); 2361 if (tval->singleton()) { 2362 if (!have_con) { 2363 // Swap, so constant is in con. 2364 con = val; 2365 tcon = tval; 2366 val = c->in(2); 2367 tval = _gvn.type(val); 2368 btest = BoolTest(btest).commute(); 2369 have_con = true; 2370 } else { 2371 // Do we have two constants? Then leave well enough alone. 2372 have_con = false; 2373 } 2374 } 2375 if (!have_con) { // remaining adjustments need a con 2376 maybe_add_predicate_after_if(path); 2377 return; 2378 } 2379 2380 sharpen_type_after_if(btest, con, tcon, val, tval); 2381 maybe_add_predicate_after_if(path); 2382 } 2383 2384 2385 static Node* extract_obj_from_klass_load(PhaseGVN* gvn, Node* n) { 2386 Node* ldk; 2387 if (n->is_DecodeNKlass()) { 2388 if (n->in(1)->Opcode() != Op_LoadNKlass) { 2389 return nullptr; 2390 } else { 2391 ldk = n->in(1); 2392 } 2393 } else if (n->Opcode() != Op_LoadKlass) { 2394 return nullptr; 2395 } else { 2396 ldk = n; 2397 } 2398 assert(ldk != nullptr && ldk->is_Load(), "should have found a LoadKlass or LoadNKlass node"); 2399 2400 Node* adr = ldk->in(MemNode::Address); 2401 intptr_t off = 0; 2402 Node* obj = AddPNode::Ideal_base_and_offset(adr, gvn, off); 2403 if (obj == nullptr || off != oopDesc::klass_offset_in_bytes()) // loading oopDesc::_klass? 2404 return nullptr; 2405 const TypePtr* tp = gvn->type(obj)->is_ptr(); 2406 if (tp == nullptr || !(tp->isa_instptr() || tp->isa_aryptr())) // is obj a Java object ptr? 2407 return nullptr; 2408 2409 return obj; 2410 } 2411 2412 void Parse::sharpen_type_after_if(BoolTest::mask btest, 2413 Node* con, const Type* tcon, 2414 Node* val, const Type* tval) { 2415 // Look for opportunities to sharpen the type of a node 2416 // whose klass is compared with a constant klass. 2417 if (btest == BoolTest::eq && tcon->isa_klassptr()) { 2418 Node* obj = extract_obj_from_klass_load(&_gvn, val); 2419 const TypeOopPtr* con_type = tcon->isa_klassptr()->as_instance_type(); 2420 if (obj != nullptr && (con_type->isa_instptr() || con_type->isa_aryptr())) { 2421 // Found: 2422 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq]) 2423 // or the narrowOop equivalent. 2424 const Type* obj_type = _gvn.type(obj); 2425 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr(); 2426 if (tboth != nullptr && tboth->klass_is_exact() && tboth != obj_type && 2427 tboth->higher_equal(obj_type)) { 2428 // obj has to be of the exact type Foo if the CmpP succeeds. 2429 int obj_in_map = map()->find_edge(obj); 2430 JVMState* jvms = this->jvms(); 2431 if (obj_in_map >= 0 && 2432 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) { 2433 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth); 2434 const Type* tcc = ccast->as_Type()->type(); 2435 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve"); 2436 // Delay transform() call to allow recovery of pre-cast value 2437 // at the control merge. 2438 _gvn.set_type_bottom(ccast); 2439 record_for_igvn(ccast); 2440 if (tboth->is_inlinetypeptr()) { 2441 ccast = InlineTypeNode::make_from_oop(this, ccast, tboth->exact_klass(true)->as_inline_klass()); 2442 } 2443 // Here's the payoff. 2444 replace_in_map(obj, ccast); 2445 } 2446 } 2447 } 2448 } 2449 2450 int val_in_map = map()->find_edge(val); 2451 if (val_in_map < 0) return; // replace_in_map would be useless 2452 { 2453 JVMState* jvms = this->jvms(); 2454 if (!(jvms->is_loc(val_in_map) || 2455 jvms->is_stk(val_in_map))) 2456 return; // again, it would be useless 2457 } 2458 2459 // Check for a comparison to a constant, and "know" that the compared 2460 // value is constrained on this path. 2461 assert(tcon->singleton(), ""); 2462 ConstraintCastNode* ccast = nullptr; 2463 Node* cast = nullptr; 2464 2465 switch (btest) { 2466 case BoolTest::eq: // Constant test? 2467 { 2468 const Type* tboth = tcon->join_speculative(tval); 2469 if (tboth == tval) break; // Nothing to gain. 2470 if (tcon->isa_int()) { 2471 ccast = new CastIINode(val, tboth); 2472 } else if (tcon == TypePtr::NULL_PTR) { 2473 // Cast to null, but keep the pointer identity temporarily live. 2474 ccast = new CastPPNode(val, tboth); 2475 } else { 2476 const TypeF* tf = tcon->isa_float_constant(); 2477 const TypeD* td = tcon->isa_double_constant(); 2478 // Exclude tests vs float/double 0 as these could be 2479 // either +0 or -0. Just because you are equal to +0 2480 // doesn't mean you ARE +0! 2481 // Note, following code also replaces Long and Oop values. 2482 if ((!tf || tf->_f != 0.0) && 2483 (!td || td->_d != 0.0)) 2484 cast = con; // Replace non-constant val by con. 2485 } 2486 } 2487 break; 2488 2489 case BoolTest::ne: 2490 if (tcon == TypePtr::NULL_PTR) { 2491 cast = cast_not_null(val, false); 2492 } 2493 break; 2494 2495 default: 2496 // (At this point we could record int range types with CastII.) 2497 break; 2498 } 2499 2500 if (ccast != nullptr) { 2501 const Type* tcc = ccast->as_Type()->type(); 2502 assert(tcc != tval && tcc->higher_equal(tval), "must improve"); 2503 // Delay transform() call to allow recovery of pre-cast value 2504 // at the control merge. 2505 ccast->set_req(0, control()); 2506 _gvn.set_type_bottom(ccast); 2507 record_for_igvn(ccast); 2508 cast = ccast; 2509 } 2510 2511 if (cast != nullptr) { // Here's the payoff. 2512 replace_in_map(val, cast); 2513 } 2514 } 2515 2516 /** 2517 * Use speculative type to optimize CmpP node: if comparison is 2518 * against the low level class, cast the object to the speculative 2519 * type if any. CmpP should then go away. 2520 * 2521 * @param c expected CmpP node 2522 * @return result of CmpP on object casted to speculative type 2523 * 2524 */ 2525 Node* Parse::optimize_cmp_with_klass(Node* c) { 2526 // If this is transformed by the _gvn to a comparison with the low 2527 // level klass then we may be able to use speculation 2528 if (c->Opcode() == Op_CmpP && 2529 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) && 2530 c->in(2)->is_Con()) { 2531 Node* load_klass = nullptr; 2532 Node* decode = nullptr; 2533 if (c->in(1)->Opcode() == Op_DecodeNKlass) { 2534 decode = c->in(1); 2535 load_klass = c->in(1)->in(1); 2536 } else { 2537 load_klass = c->in(1); 2538 } 2539 if (load_klass->in(2)->is_AddP()) { 2540 Node* addp = load_klass->in(2); 2541 Node* obj = addp->in(AddPNode::Address); 2542 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr(); 2543 if (obj_type->speculative_type_not_null() != nullptr) { 2544 ciKlass* k = obj_type->speculative_type(); 2545 inc_sp(2); 2546 obj = maybe_cast_profiled_obj(obj, k); 2547 dec_sp(2); 2548 if (obj->is_InlineType()) { 2549 assert(obj->as_InlineType()->is_allocated(&_gvn), "must be allocated"); 2550 obj = obj->as_InlineType()->get_oop(); 2551 } 2552 // Make the CmpP use the casted obj 2553 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset)); 2554 load_klass = load_klass->clone(); 2555 load_klass->set_req(2, addp); 2556 load_klass = _gvn.transform(load_klass); 2557 if (decode != nullptr) { 2558 decode = decode->clone(); 2559 decode->set_req(1, load_klass); 2560 load_klass = _gvn.transform(decode); 2561 } 2562 c = c->clone(); 2563 c->set_req(1, load_klass); 2564 c = _gvn.transform(c); 2565 } 2566 } 2567 } 2568 return c; 2569 } 2570 2571 //------------------------------do_one_bytecode-------------------------------- 2572 // Parse this bytecode, and alter the Parsers JVM->Node mapping 2573 void Parse::do_one_bytecode() { 2574 Node *a, *b, *c, *d; // Handy temps 2575 BoolTest::mask btest; 2576 int i; 2577 2578 assert(!has_exceptions(), "bytecode entry state must be clear of throws"); 2579 2580 if (C->check_node_count(NodeLimitFudgeFactor * 5, 2581 "out of nodes parsing method")) { 2582 return; 2583 } 2584 2585 #ifdef ASSERT 2586 // for setting breakpoints 2587 if (TraceOptoParse) { 2588 tty->print(" @"); 2589 dump_bci(bci()); 2590 tty->print(" %s", Bytecodes::name(bc())); 2591 tty->cr(); 2592 } 2593 #endif 2594 2595 switch (bc()) { 2596 case Bytecodes::_nop: 2597 // do nothing 2598 break; 2599 case Bytecodes::_lconst_0: 2600 push_pair(longcon(0)); 2601 break; 2602 2603 case Bytecodes::_lconst_1: 2604 push_pair(longcon(1)); 2605 break; 2606 2607 case Bytecodes::_fconst_0: 2608 push(zerocon(T_FLOAT)); 2609 break; 2610 2611 case Bytecodes::_fconst_1: 2612 push(makecon(TypeF::ONE)); 2613 break; 2614 2615 case Bytecodes::_fconst_2: 2616 push(makecon(TypeF::make(2.0f))); 2617 break; 2618 2619 case Bytecodes::_dconst_0: 2620 push_pair(zerocon(T_DOUBLE)); 2621 break; 2622 2623 case Bytecodes::_dconst_1: 2624 push_pair(makecon(TypeD::ONE)); 2625 break; 2626 2627 case Bytecodes::_iconst_m1:push(intcon(-1)); break; 2628 case Bytecodes::_iconst_0: push(intcon( 0)); break; 2629 case Bytecodes::_iconst_1: push(intcon( 1)); break; 2630 case Bytecodes::_iconst_2: push(intcon( 2)); break; 2631 case Bytecodes::_iconst_3: push(intcon( 3)); break; 2632 case Bytecodes::_iconst_4: push(intcon( 4)); break; 2633 case Bytecodes::_iconst_5: push(intcon( 5)); break; 2634 case Bytecodes::_bipush: push(intcon(iter().get_constant_u1())); break; 2635 case Bytecodes::_sipush: push(intcon(iter().get_constant_u2())); break; 2636 case Bytecodes::_aconst_null: push(null()); break; 2637 2638 case Bytecodes::_ldc: 2639 case Bytecodes::_ldc_w: 2640 case Bytecodes::_ldc2_w: { 2641 ciConstant constant = iter().get_constant(); 2642 if (constant.is_loaded()) { 2643 const Type* con_type = Type::make_from_constant(constant); 2644 if (con_type != nullptr) { 2645 push_node(con_type->basic_type(), makecon(con_type)); 2646 } 2647 } else { 2648 // If the constant is unresolved or in error state, run this BC in the interpreter. 2649 if (iter().is_in_error()) { 2650 uncommon_trap(Deoptimization::make_trap_request(Deoptimization::Reason_unhandled, 2651 Deoptimization::Action_none), 2652 nullptr, "constant in error state", true /* must_throw */); 2653 2654 } else { 2655 int index = iter().get_constant_pool_index(); 2656 uncommon_trap(Deoptimization::make_trap_request(Deoptimization::Reason_unloaded, 2657 Deoptimization::Action_reinterpret, 2658 index), 2659 nullptr, "unresolved constant", false /* must_throw */); 2660 } 2661 } 2662 break; 2663 } 2664 2665 case Bytecodes::_aload_0: 2666 push( local(0) ); 2667 break; 2668 case Bytecodes::_aload_1: 2669 push( local(1) ); 2670 break; 2671 case Bytecodes::_aload_2: 2672 push( local(2) ); 2673 break; 2674 case Bytecodes::_aload_3: 2675 push( local(3) ); 2676 break; 2677 case Bytecodes::_aload: 2678 push( local(iter().get_index()) ); 2679 break; 2680 2681 case Bytecodes::_fload_0: 2682 case Bytecodes::_iload_0: 2683 push( local(0) ); 2684 break; 2685 case Bytecodes::_fload_1: 2686 case Bytecodes::_iload_1: 2687 push( local(1) ); 2688 break; 2689 case Bytecodes::_fload_2: 2690 case Bytecodes::_iload_2: 2691 push( local(2) ); 2692 break; 2693 case Bytecodes::_fload_3: 2694 case Bytecodes::_iload_3: 2695 push( local(3) ); 2696 break; 2697 case Bytecodes::_fload: 2698 case Bytecodes::_iload: 2699 push( local(iter().get_index()) ); 2700 break; 2701 case Bytecodes::_lload_0: 2702 push_pair_local( 0 ); 2703 break; 2704 case Bytecodes::_lload_1: 2705 push_pair_local( 1 ); 2706 break; 2707 case Bytecodes::_lload_2: 2708 push_pair_local( 2 ); 2709 break; 2710 case Bytecodes::_lload_3: 2711 push_pair_local( 3 ); 2712 break; 2713 case Bytecodes::_lload: 2714 push_pair_local( iter().get_index() ); 2715 break; 2716 2717 case Bytecodes::_dload_0: 2718 push_pair_local(0); 2719 break; 2720 case Bytecodes::_dload_1: 2721 push_pair_local(1); 2722 break; 2723 case Bytecodes::_dload_2: 2724 push_pair_local(2); 2725 break; 2726 case Bytecodes::_dload_3: 2727 push_pair_local(3); 2728 break; 2729 case Bytecodes::_dload: 2730 push_pair_local(iter().get_index()); 2731 break; 2732 case Bytecodes::_fstore_0: 2733 case Bytecodes::_istore_0: 2734 case Bytecodes::_astore_0: 2735 set_local( 0, pop() ); 2736 break; 2737 case Bytecodes::_fstore_1: 2738 case Bytecodes::_istore_1: 2739 case Bytecodes::_astore_1: 2740 set_local( 1, pop() ); 2741 break; 2742 case Bytecodes::_fstore_2: 2743 case Bytecodes::_istore_2: 2744 case Bytecodes::_astore_2: 2745 set_local( 2, pop() ); 2746 break; 2747 case Bytecodes::_fstore_3: 2748 case Bytecodes::_istore_3: 2749 case Bytecodes::_astore_3: 2750 set_local( 3, pop() ); 2751 break; 2752 case Bytecodes::_fstore: 2753 case Bytecodes::_istore: 2754 case Bytecodes::_astore: 2755 set_local( iter().get_index(), pop() ); 2756 break; 2757 // long stores 2758 case Bytecodes::_lstore_0: 2759 set_pair_local( 0, pop_pair() ); 2760 break; 2761 case Bytecodes::_lstore_1: 2762 set_pair_local( 1, pop_pair() ); 2763 break; 2764 case Bytecodes::_lstore_2: 2765 set_pair_local( 2, pop_pair() ); 2766 break; 2767 case Bytecodes::_lstore_3: 2768 set_pair_local( 3, pop_pair() ); 2769 break; 2770 case Bytecodes::_lstore: 2771 set_pair_local( iter().get_index(), pop_pair() ); 2772 break; 2773 2774 // double stores 2775 case Bytecodes::_dstore_0: 2776 set_pair_local( 0, dprecision_rounding(pop_pair()) ); 2777 break; 2778 case Bytecodes::_dstore_1: 2779 set_pair_local( 1, dprecision_rounding(pop_pair()) ); 2780 break; 2781 case Bytecodes::_dstore_2: 2782 set_pair_local( 2, dprecision_rounding(pop_pair()) ); 2783 break; 2784 case Bytecodes::_dstore_3: 2785 set_pair_local( 3, dprecision_rounding(pop_pair()) ); 2786 break; 2787 case Bytecodes::_dstore: 2788 set_pair_local( iter().get_index(), dprecision_rounding(pop_pair()) ); 2789 break; 2790 2791 case Bytecodes::_pop: dec_sp(1); break; 2792 case Bytecodes::_pop2: dec_sp(2); break; 2793 case Bytecodes::_swap: 2794 a = pop(); 2795 b = pop(); 2796 push(a); 2797 push(b); 2798 break; 2799 case Bytecodes::_dup: 2800 a = pop(); 2801 push(a); 2802 push(a); 2803 break; 2804 case Bytecodes::_dup_x1: 2805 a = pop(); 2806 b = pop(); 2807 push( a ); 2808 push( b ); 2809 push( a ); 2810 break; 2811 case Bytecodes::_dup_x2: 2812 a = pop(); 2813 b = pop(); 2814 c = pop(); 2815 push( a ); 2816 push( c ); 2817 push( b ); 2818 push( a ); 2819 break; 2820 case Bytecodes::_dup2: 2821 a = pop(); 2822 b = pop(); 2823 push( b ); 2824 push( a ); 2825 push( b ); 2826 push( a ); 2827 break; 2828 2829 case Bytecodes::_dup2_x1: 2830 // before: .. c, b, a 2831 // after: .. b, a, c, b, a 2832 // not tested 2833 a = pop(); 2834 b = pop(); 2835 c = pop(); 2836 push( b ); 2837 push( a ); 2838 push( c ); 2839 push( b ); 2840 push( a ); 2841 break; 2842 case Bytecodes::_dup2_x2: 2843 // before: .. d, c, b, a 2844 // after: .. b, a, d, c, b, a 2845 // not tested 2846 a = pop(); 2847 b = pop(); 2848 c = pop(); 2849 d = pop(); 2850 push( b ); 2851 push( a ); 2852 push( d ); 2853 push( c ); 2854 push( b ); 2855 push( a ); 2856 break; 2857 2858 case Bytecodes::_arraylength: { 2859 // Must do null-check with value on expression stack 2860 Node *ary = null_check(peek(), T_ARRAY); 2861 // Compile-time detect of null-exception? 2862 if (stopped()) return; 2863 a = pop(); 2864 push(load_array_length(a)); 2865 break; 2866 } 2867 2868 case Bytecodes::_baload: array_load(T_BYTE); break; 2869 case Bytecodes::_caload: array_load(T_CHAR); break; 2870 case Bytecodes::_iaload: array_load(T_INT); break; 2871 case Bytecodes::_saload: array_load(T_SHORT); break; 2872 case Bytecodes::_faload: array_load(T_FLOAT); break; 2873 case Bytecodes::_aaload: array_load(T_OBJECT); break; 2874 case Bytecodes::_laload: array_load(T_LONG); break; 2875 case Bytecodes::_daload: array_load(T_DOUBLE); break; 2876 case Bytecodes::_bastore: array_store(T_BYTE); break; 2877 case Bytecodes::_castore: array_store(T_CHAR); break; 2878 case Bytecodes::_iastore: array_store(T_INT); break; 2879 case Bytecodes::_sastore: array_store(T_SHORT); break; 2880 case Bytecodes::_fastore: array_store(T_FLOAT); break; 2881 case Bytecodes::_aastore: array_store(T_OBJECT); break; 2882 case Bytecodes::_lastore: array_store(T_LONG); break; 2883 case Bytecodes::_dastore: array_store(T_DOUBLE); break; 2884 2885 case Bytecodes::_getfield: 2886 do_getfield(); 2887 break; 2888 2889 case Bytecodes::_getstatic: 2890 do_getstatic(); 2891 break; 2892 2893 case Bytecodes::_putfield: 2894 do_putfield(); 2895 break; 2896 2897 case Bytecodes::_putstatic: 2898 do_putstatic(); 2899 break; 2900 2901 case Bytecodes::_irem: 2902 // Must keep both values on the expression-stack during null-check 2903 zero_check_int(peek()); 2904 // Compile-time detect of null-exception? 2905 if (stopped()) return; 2906 b = pop(); 2907 a = pop(); 2908 push(_gvn.transform(new ModINode(control(), a, b))); 2909 break; 2910 case Bytecodes::_idiv: 2911 // Must keep both values on the expression-stack during null-check 2912 zero_check_int(peek()); 2913 // Compile-time detect of null-exception? 2914 if (stopped()) return; 2915 b = pop(); 2916 a = pop(); 2917 push( _gvn.transform( new DivINode(control(),a,b) ) ); 2918 break; 2919 case Bytecodes::_imul: 2920 b = pop(); a = pop(); 2921 push( _gvn.transform( new MulINode(a,b) ) ); 2922 break; 2923 case Bytecodes::_iadd: 2924 b = pop(); a = pop(); 2925 push( _gvn.transform( new AddINode(a,b) ) ); 2926 break; 2927 case Bytecodes::_ineg: 2928 a = pop(); 2929 push( _gvn.transform( new SubINode(_gvn.intcon(0),a)) ); 2930 break; 2931 case Bytecodes::_isub: 2932 b = pop(); a = pop(); 2933 push( _gvn.transform( new SubINode(a,b) ) ); 2934 break; 2935 case Bytecodes::_iand: 2936 b = pop(); a = pop(); 2937 push( _gvn.transform( new AndINode(a,b) ) ); 2938 break; 2939 case Bytecodes::_ior: 2940 b = pop(); a = pop(); 2941 push( _gvn.transform( new OrINode(a,b) ) ); 2942 break; 2943 case Bytecodes::_ixor: 2944 b = pop(); a = pop(); 2945 push( _gvn.transform( new XorINode(a,b) ) ); 2946 break; 2947 case Bytecodes::_ishl: 2948 b = pop(); a = pop(); 2949 push( _gvn.transform( new LShiftINode(a,b) ) ); 2950 break; 2951 case Bytecodes::_ishr: 2952 b = pop(); a = pop(); 2953 push( _gvn.transform( new RShiftINode(a,b) ) ); 2954 break; 2955 case Bytecodes::_iushr: 2956 b = pop(); a = pop(); 2957 push( _gvn.transform( new URShiftINode(a,b) ) ); 2958 break; 2959 2960 case Bytecodes::_fneg: 2961 a = pop(); 2962 b = _gvn.transform(new NegFNode (a)); 2963 push(b); 2964 break; 2965 2966 case Bytecodes::_fsub: 2967 b = pop(); 2968 a = pop(); 2969 c = _gvn.transform( new SubFNode(a,b) ); 2970 d = precision_rounding(c); 2971 push( d ); 2972 break; 2973 2974 case Bytecodes::_fadd: 2975 b = pop(); 2976 a = pop(); 2977 c = _gvn.transform( new AddFNode(a,b) ); 2978 d = precision_rounding(c); 2979 push( d ); 2980 break; 2981 2982 case Bytecodes::_fmul: 2983 b = pop(); 2984 a = pop(); 2985 c = _gvn.transform( new MulFNode(a,b) ); 2986 d = precision_rounding(c); 2987 push( d ); 2988 break; 2989 2990 case Bytecodes::_fdiv: 2991 b = pop(); 2992 a = pop(); 2993 c = _gvn.transform( new DivFNode(0,a,b) ); 2994 d = precision_rounding(c); 2995 push( d ); 2996 break; 2997 2998 case Bytecodes::_frem: 2999 if (Matcher::has_match_rule(Op_ModF)) { 3000 // Generate a ModF node. 3001 b = pop(); 3002 a = pop(); 3003 c = _gvn.transform( new ModFNode(0,a,b) ); 3004 d = precision_rounding(c); 3005 push( d ); 3006 } 3007 else { 3008 // Generate a call. 3009 modf(); 3010 } 3011 break; 3012 3013 case Bytecodes::_fcmpl: 3014 b = pop(); 3015 a = pop(); 3016 c = _gvn.transform( new CmpF3Node( a, b)); 3017 push(c); 3018 break; 3019 case Bytecodes::_fcmpg: 3020 b = pop(); 3021 a = pop(); 3022 3023 // Same as fcmpl but need to flip the unordered case. Swap the inputs, 3024 // which negates the result sign except for unordered. Flip the unordered 3025 // as well by using CmpF3 which implements unordered-lesser instead of 3026 // unordered-greater semantics. Finally, commute the result bits. Result 3027 // is same as using a CmpF3Greater except we did it with CmpF3 alone. 3028 c = _gvn.transform( new CmpF3Node( b, a)); 3029 c = _gvn.transform( new SubINode(_gvn.intcon(0),c) ); 3030 push(c); 3031 break; 3032 3033 case Bytecodes::_f2i: 3034 a = pop(); 3035 push(_gvn.transform(new ConvF2INode(a))); 3036 break; 3037 3038 case Bytecodes::_d2i: 3039 a = pop_pair(); 3040 b = _gvn.transform(new ConvD2INode(a)); 3041 push( b ); 3042 break; 3043 3044 case Bytecodes::_f2d: 3045 a = pop(); 3046 b = _gvn.transform( new ConvF2DNode(a)); 3047 push_pair( b ); 3048 break; 3049 3050 case Bytecodes::_d2f: 3051 a = pop_pair(); 3052 b = _gvn.transform( new ConvD2FNode(a)); 3053 // This breaks _227_mtrt (speed & correctness) and _222_mpegaudio (speed) 3054 //b = _gvn.transform(new RoundFloatNode(0, b) ); 3055 push( b ); 3056 break; 3057 3058 case Bytecodes::_l2f: 3059 if (Matcher::convL2FSupported()) { 3060 a = pop_pair(); 3061 b = _gvn.transform( new ConvL2FNode(a)); 3062 // For x86_32.ad, FILD doesn't restrict precision to 24 or 53 bits. 3063 // Rather than storing the result into an FP register then pushing 3064 // out to memory to round, the machine instruction that implements 3065 // ConvL2D is responsible for rounding. 3066 // c = precision_rounding(b); 3067 push(b); 3068 } else { 3069 l2f(); 3070 } 3071 break; 3072 3073 case Bytecodes::_l2d: 3074 a = pop_pair(); 3075 b = _gvn.transform( new ConvL2DNode(a)); 3076 // For x86_32.ad, rounding is always necessary (see _l2f above). 3077 // c = dprecision_rounding(b); 3078 push_pair(b); 3079 break; 3080 3081 case Bytecodes::_f2l: 3082 a = pop(); 3083 b = _gvn.transform( new ConvF2LNode(a)); 3084 push_pair(b); 3085 break; 3086 3087 case Bytecodes::_d2l: 3088 a = pop_pair(); 3089 b = _gvn.transform( new ConvD2LNode(a)); 3090 push_pair(b); 3091 break; 3092 3093 case Bytecodes::_dsub: 3094 b = pop_pair(); 3095 a = pop_pair(); 3096 c = _gvn.transform( new SubDNode(a,b) ); 3097 d = dprecision_rounding(c); 3098 push_pair( d ); 3099 break; 3100 3101 case Bytecodes::_dadd: 3102 b = pop_pair(); 3103 a = pop_pair(); 3104 c = _gvn.transform( new AddDNode(a,b) ); 3105 d = dprecision_rounding(c); 3106 push_pair( d ); 3107 break; 3108 3109 case Bytecodes::_dmul: 3110 b = pop_pair(); 3111 a = pop_pair(); 3112 c = _gvn.transform( new MulDNode(a,b) ); 3113 d = dprecision_rounding(c); 3114 push_pair( d ); 3115 break; 3116 3117 case Bytecodes::_ddiv: 3118 b = pop_pair(); 3119 a = pop_pair(); 3120 c = _gvn.transform( new DivDNode(0,a,b) ); 3121 d = dprecision_rounding(c); 3122 push_pair( d ); 3123 break; 3124 3125 case Bytecodes::_dneg: 3126 a = pop_pair(); 3127 b = _gvn.transform(new NegDNode (a)); 3128 push_pair(b); 3129 break; 3130 3131 case Bytecodes::_drem: 3132 if (Matcher::has_match_rule(Op_ModD)) { 3133 // Generate a ModD node. 3134 b = pop_pair(); 3135 a = pop_pair(); 3136 // a % b 3137 3138 c = _gvn.transform( new ModDNode(0,a,b) ); 3139 d = dprecision_rounding(c); 3140 push_pair( d ); 3141 } 3142 else { 3143 // Generate a call. 3144 modd(); 3145 } 3146 break; 3147 3148 case Bytecodes::_dcmpl: 3149 b = pop_pair(); 3150 a = pop_pair(); 3151 c = _gvn.transform( new CmpD3Node( a, b)); 3152 push(c); 3153 break; 3154 3155 case Bytecodes::_dcmpg: 3156 b = pop_pair(); 3157 a = pop_pair(); 3158 // Same as dcmpl but need to flip the unordered case. 3159 // Commute the inputs, which negates the result sign except for unordered. 3160 // Flip the unordered as well by using CmpD3 which implements 3161 // unordered-lesser instead of unordered-greater semantics. 3162 // Finally, negate the result bits. Result is same as using a 3163 // CmpD3Greater except we did it with CmpD3 alone. 3164 c = _gvn.transform( new CmpD3Node( b, a)); 3165 c = _gvn.transform( new SubINode(_gvn.intcon(0),c) ); 3166 push(c); 3167 break; 3168 3169 3170 // Note for longs -> lo word is on TOS, hi word is on TOS - 1 3171 case Bytecodes::_land: 3172 b = pop_pair(); 3173 a = pop_pair(); 3174 c = _gvn.transform( new AndLNode(a,b) ); 3175 push_pair(c); 3176 break; 3177 case Bytecodes::_lor: 3178 b = pop_pair(); 3179 a = pop_pair(); 3180 c = _gvn.transform( new OrLNode(a,b) ); 3181 push_pair(c); 3182 break; 3183 case Bytecodes::_lxor: 3184 b = pop_pair(); 3185 a = pop_pair(); 3186 c = _gvn.transform( new XorLNode(a,b) ); 3187 push_pair(c); 3188 break; 3189 3190 case Bytecodes::_lshl: 3191 b = pop(); // the shift count 3192 a = pop_pair(); // value to be shifted 3193 c = _gvn.transform( new LShiftLNode(a,b) ); 3194 push_pair(c); 3195 break; 3196 case Bytecodes::_lshr: 3197 b = pop(); // the shift count 3198 a = pop_pair(); // value to be shifted 3199 c = _gvn.transform( new RShiftLNode(a,b) ); 3200 push_pair(c); 3201 break; 3202 case Bytecodes::_lushr: 3203 b = pop(); // the shift count 3204 a = pop_pair(); // value to be shifted 3205 c = _gvn.transform( new URShiftLNode(a,b) ); 3206 push_pair(c); 3207 break; 3208 case Bytecodes::_lmul: 3209 b = pop_pair(); 3210 a = pop_pair(); 3211 c = _gvn.transform( new MulLNode(a,b) ); 3212 push_pair(c); 3213 break; 3214 3215 case Bytecodes::_lrem: 3216 // Must keep both values on the expression-stack during null-check 3217 assert(peek(0) == top(), "long word order"); 3218 zero_check_long(peek(1)); 3219 // Compile-time detect of null-exception? 3220 if (stopped()) return; 3221 b = pop_pair(); 3222 a = pop_pair(); 3223 c = _gvn.transform( new ModLNode(control(),a,b) ); 3224 push_pair(c); 3225 break; 3226 3227 case Bytecodes::_ldiv: 3228 // Must keep both values on the expression-stack during null-check 3229 assert(peek(0) == top(), "long word order"); 3230 zero_check_long(peek(1)); 3231 // Compile-time detect of null-exception? 3232 if (stopped()) return; 3233 b = pop_pair(); 3234 a = pop_pair(); 3235 c = _gvn.transform( new DivLNode(control(),a,b) ); 3236 push_pair(c); 3237 break; 3238 3239 case Bytecodes::_ladd: 3240 b = pop_pair(); 3241 a = pop_pair(); 3242 c = _gvn.transform( new AddLNode(a,b) ); 3243 push_pair(c); 3244 break; 3245 case Bytecodes::_lsub: 3246 b = pop_pair(); 3247 a = pop_pair(); 3248 c = _gvn.transform( new SubLNode(a,b) ); 3249 push_pair(c); 3250 break; 3251 case Bytecodes::_lcmp: 3252 // Safepoints are now inserted _before_ branches. The long-compare 3253 // bytecode painfully produces a 3-way value (-1,0,+1) which requires a 3254 // slew of control flow. These are usually followed by a CmpI vs zero and 3255 // a branch; this pattern then optimizes to the obvious long-compare and 3256 // branch. However, if the branch is backwards there's a Safepoint 3257 // inserted. The inserted Safepoint captures the JVM state at the 3258 // pre-branch point, i.e. it captures the 3-way value. Thus if a 3259 // long-compare is used to control a loop the debug info will force 3260 // computation of the 3-way value, even though the generated code uses a 3261 // long-compare and branch. We try to rectify the situation by inserting 3262 // a SafePoint here and have it dominate and kill the safepoint added at a 3263 // following backwards branch. At this point the JVM state merely holds 2 3264 // longs but not the 3-way value. 3265 switch (iter().next_bc()) { 3266 case Bytecodes::_ifgt: 3267 case Bytecodes::_iflt: 3268 case Bytecodes::_ifge: 3269 case Bytecodes::_ifle: 3270 case Bytecodes::_ifne: 3271 case Bytecodes::_ifeq: 3272 // If this is a backwards branch in the bytecodes, add Safepoint 3273 maybe_add_safepoint(iter().next_get_dest()); 3274 default: 3275 break; 3276 } 3277 b = pop_pair(); 3278 a = pop_pair(); 3279 c = _gvn.transform( new CmpL3Node( a, b )); 3280 push(c); 3281 break; 3282 3283 case Bytecodes::_lneg: 3284 a = pop_pair(); 3285 b = _gvn.transform( new SubLNode(longcon(0),a)); 3286 push_pair(b); 3287 break; 3288 case Bytecodes::_l2i: 3289 a = pop_pair(); 3290 push( _gvn.transform( new ConvL2INode(a))); 3291 break; 3292 case Bytecodes::_i2l: 3293 a = pop(); 3294 b = _gvn.transform( new ConvI2LNode(a)); 3295 push_pair(b); 3296 break; 3297 case Bytecodes::_i2b: 3298 // Sign extend 3299 a = pop(); 3300 a = Compile::narrow_value(T_BYTE, a, nullptr, &_gvn, true); 3301 push(a); 3302 break; 3303 case Bytecodes::_i2s: 3304 a = pop(); 3305 a = Compile::narrow_value(T_SHORT, a, nullptr, &_gvn, true); 3306 push(a); 3307 break; 3308 case Bytecodes::_i2c: 3309 a = pop(); 3310 a = Compile::narrow_value(T_CHAR, a, nullptr, &_gvn, true); 3311 push(a); 3312 break; 3313 3314 case Bytecodes::_i2f: 3315 a = pop(); 3316 b = _gvn.transform( new ConvI2FNode(a) ) ; 3317 c = precision_rounding(b); 3318 push (b); 3319 break; 3320 3321 case Bytecodes::_i2d: 3322 a = pop(); 3323 b = _gvn.transform( new ConvI2DNode(a)); 3324 push_pair(b); 3325 break; 3326 3327 case Bytecodes::_iinc: // Increment local 3328 i = iter().get_index(); // Get local index 3329 set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) ); 3330 break; 3331 3332 // Exit points of synchronized methods must have an unlock node 3333 case Bytecodes::_return: 3334 return_current(nullptr); 3335 break; 3336 3337 case Bytecodes::_ireturn: 3338 case Bytecodes::_areturn: 3339 case Bytecodes::_freturn: 3340 return_current(pop()); 3341 break; 3342 case Bytecodes::_lreturn: 3343 return_current(pop_pair()); 3344 break; 3345 case Bytecodes::_dreturn: 3346 return_current(pop_pair()); 3347 break; 3348 3349 case Bytecodes::_athrow: 3350 // null exception oop throws null pointer exception 3351 null_check(peek()); 3352 if (stopped()) return; 3353 // Hook the thrown exception directly to subsequent handlers. 3354 if (BailoutToInterpreterForThrows) { 3355 // Keep method interpreted from now on. 3356 uncommon_trap(Deoptimization::Reason_unhandled, 3357 Deoptimization::Action_make_not_compilable); 3358 return; 3359 } 3360 if (env()->jvmti_can_post_on_exceptions()) { 3361 // check if we must post exception events, take uncommon trap if so (with must_throw = false) 3362 uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false); 3363 } 3364 // Here if either can_post_on_exceptions or should_post_on_exceptions is false 3365 add_exception_state(make_exception_state(peek())); 3366 break; 3367 3368 case Bytecodes::_goto: // fall through 3369 case Bytecodes::_goto_w: { 3370 int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest(); 3371 3372 // If this is a backwards branch in the bytecodes, add Safepoint 3373 maybe_add_safepoint(target_bci); 3374 3375 // Merge the current control into the target basic block 3376 merge(target_bci); 3377 3378 // See if we can get some profile data and hand it off to the next block 3379 Block *target_block = block()->successor_for_bci(target_bci); 3380 if (target_block->pred_count() != 1) break; 3381 ciMethodData* methodData = method()->method_data(); 3382 if (!methodData->is_mature()) break; 3383 ciProfileData* data = methodData->bci_to_data(bci()); 3384 assert(data != nullptr && data->is_JumpData(), "need JumpData for taken branch"); 3385 int taken = ((ciJumpData*)data)->taken(); 3386 taken = method()->scale_count(taken); 3387 target_block->set_count(taken); 3388 break; 3389 } 3390 3391 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null; 3392 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null; 3393 handle_if_null: 3394 // If this is a backwards branch in the bytecodes, add Safepoint 3395 maybe_add_safepoint(iter().get_dest()); 3396 a = null(); 3397 b = pop(); 3398 if (b->is_InlineType()) { 3399 // Null checking a scalarized but nullable inline type. Check the IsInit 3400 // input instead of the oop input to avoid keeping buffer allocations alive 3401 c = _gvn.transform(new CmpINode(b->as_InlineType()->get_is_init(), zerocon(T_INT))); 3402 } else { 3403 if (!_gvn.type(b)->speculative_maybe_null() && 3404 !too_many_traps(Deoptimization::Reason_speculate_null_check)) { 3405 inc_sp(1); 3406 Node* null_ctl = top(); 3407 b = null_check_oop(b, &null_ctl, true, true, true); 3408 assert(null_ctl->is_top(), "no null control here"); 3409 dec_sp(1); 3410 } else if (_gvn.type(b)->speculative_always_null() && 3411 !too_many_traps(Deoptimization::Reason_speculate_null_assert)) { 3412 inc_sp(1); 3413 b = null_assert(b); 3414 dec_sp(1); 3415 } 3416 c = _gvn.transform( new CmpPNode(b, a) ); 3417 } 3418 do_ifnull(btest, c); 3419 break; 3420 3421 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp; 3422 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp; 3423 handle_if_acmp: 3424 // If this is a backwards branch in the bytecodes, add Safepoint 3425 maybe_add_safepoint(iter().get_dest()); 3426 a = pop(); 3427 b = pop(); 3428 do_acmp(btest, b, a); 3429 break; 3430 3431 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx; 3432 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx; 3433 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx; 3434 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx; 3435 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx; 3436 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx; 3437 handle_ifxx: 3438 // If this is a backwards branch in the bytecodes, add Safepoint 3439 maybe_add_safepoint(iter().get_dest()); 3440 a = _gvn.intcon(0); 3441 b = pop(); 3442 c = _gvn.transform( new CmpINode(b, a) ); 3443 do_if(btest, c); 3444 break; 3445 3446 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp; 3447 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp; 3448 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp; 3449 case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp; 3450 case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp; 3451 case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp; 3452 handle_if_icmp: 3453 // If this is a backwards branch in the bytecodes, add Safepoint 3454 maybe_add_safepoint(iter().get_dest()); 3455 a = pop(); 3456 b = pop(); 3457 c = _gvn.transform( new CmpINode( b, a ) ); 3458 do_if(btest, c); 3459 break; 3460 3461 case Bytecodes::_tableswitch: 3462 do_tableswitch(); 3463 break; 3464 3465 case Bytecodes::_lookupswitch: 3466 do_lookupswitch(); 3467 break; 3468 3469 case Bytecodes::_invokestatic: 3470 case Bytecodes::_invokedynamic: 3471 case Bytecodes::_invokespecial: 3472 case Bytecodes::_invokevirtual: 3473 case Bytecodes::_invokeinterface: 3474 do_call(); 3475 break; 3476 case Bytecodes::_checkcast: 3477 do_checkcast(); 3478 break; 3479 case Bytecodes::_instanceof: 3480 do_instanceof(); 3481 break; 3482 case Bytecodes::_anewarray: 3483 do_newarray(); 3484 break; 3485 case Bytecodes::_newarray: 3486 do_newarray((BasicType)iter().get_index()); 3487 break; 3488 case Bytecodes::_multianewarray: 3489 do_multianewarray(); 3490 break; 3491 case Bytecodes::_new: 3492 do_new(); 3493 break; 3494 case Bytecodes::_aconst_init: 3495 do_aconst_init(); 3496 break; 3497 case Bytecodes::_withfield: 3498 do_withfield(); 3499 break; 3500 3501 case Bytecodes::_jsr: 3502 case Bytecodes::_jsr_w: 3503 do_jsr(); 3504 break; 3505 3506 case Bytecodes::_ret: 3507 do_ret(); 3508 break; 3509 3510 3511 case Bytecodes::_monitorenter: 3512 do_monitor_enter(); 3513 break; 3514 3515 case Bytecodes::_monitorexit: 3516 do_monitor_exit(); 3517 break; 3518 3519 case Bytecodes::_breakpoint: 3520 // Breakpoint set concurrently to compile 3521 // %%% use an uncommon trap? 3522 C->record_failure("breakpoint in method"); 3523 return; 3524 3525 default: 3526 #ifndef PRODUCT 3527 map()->dump(99); 3528 #endif 3529 tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) ); 3530 ShouldNotReachHere(); 3531 } 3532 3533 #ifndef PRODUCT 3534 if (C->should_print_igv(1)) { 3535 IdealGraphPrinter* printer = C->igv_printer(); 3536 char buffer[256]; 3537 jio_snprintf(buffer, sizeof(buffer), "Bytecode %d: %s", bci(), Bytecodes::name(bc())); 3538 bool old = printer->traverse_outs(); 3539 printer->set_traverse_outs(true); 3540 printer->print_method(buffer, 4); 3541 printer->set_traverse_outs(old); 3542 } 3543 #endif 3544 }