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