1 /* 2 * Copyright (c) 1998, 2014, 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/universe.inline.hpp" 32 #include "opto/addnode.hpp" 33 #include "opto/castnode.hpp" 34 #include "opto/convertnode.hpp" 35 #include "opto/divnode.hpp" 36 #include "opto/idealGraphPrinter.hpp" 37 #include "opto/matcher.hpp" 38 #include "opto/memnode.hpp" 39 #include "opto/mulnode.hpp" 40 #include "opto/opaquenode.hpp" 41 #include "opto/parse.hpp" 42 #include "opto/runtime.hpp" 43 #include "runtime/deoptimization.hpp" 44 #include "runtime/sharedRuntime.hpp" 45 46 extern int explicit_null_checks_inserted, 47 explicit_null_checks_elided; 48 49 //---------------------------------array_load---------------------------------- 50 void Parse::array_load(BasicType elem_type) { 51 const Type* elem = Type::TOP; 52 Node* adr = array_addressing(elem_type, 0, &elem); 53 if (stopped()) return; // guaranteed null or range check 54 dec_sp(2); // Pop array and index 55 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type); 56 Node* ld = make_load(control(), adr, elem, elem_type, adr_type, MemNode::unordered); 57 push(ld); 58 } 59 60 61 //--------------------------------array_store---------------------------------- 62 void Parse::array_store(BasicType elem_type) { 63 Node* adr = array_addressing(elem_type, 1); 64 if (stopped()) return; // guaranteed null or range check 65 Node* val = pop(); 66 dec_sp(2); // Pop array and index 67 const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(elem_type); 68 store_to_memory(control(), adr, val, elem_type, adr_type, StoreNode::release_if_reference(elem_type)); 69 } 70 71 72 //------------------------------array_addressing------------------------------- 73 // Pull array and index from the stack. Compute pointer-to-element. 74 Node* Parse::array_addressing(BasicType type, int vals, const Type* *result2) { 75 Node *idx = peek(0+vals); // Get from stack without popping 76 Node *ary = peek(1+vals); // in case of exception 77 78 // Null check the array base, with correct stack contents 79 ary = null_check(ary, T_ARRAY); 80 // Compile-time detect of null-exception? 81 if (stopped()) return top(); 82 83 const TypeAryPtr* arytype = _gvn.type(ary)->is_aryptr(); 84 const TypeInt* sizetype = arytype->size(); 85 const Type* elemtype = arytype->elem(); 86 87 if (UseUniqueSubclasses && result2 != NULL) { 88 const Type* el = elemtype->make_ptr(); 89 if (el && el->isa_instptr()) { 90 const TypeInstPtr* toop = el->is_instptr(); 91 if (toop->klass()->as_instance_klass()->unique_concrete_subklass()) { 92 // If we load from "AbstractClass[]" we must see "ConcreteSubClass". 93 const Type* subklass = Type::get_const_type(toop->klass()); 94 elemtype = subklass->join_speculative(el); 95 } 96 } 97 } 98 99 // Check for big class initializers with all constant offsets 100 // feeding into a known-size array. 101 const TypeInt* idxtype = _gvn.type(idx)->is_int(); 102 // See if the highest idx value is less than the lowest array bound, 103 // and if the idx value cannot be negative: 104 bool need_range_check = true; 105 if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) { 106 need_range_check = false; 107 if (C->log() != NULL) C->log()->elem("observe that='!need_range_check'"); 108 } 109 110 ciKlass * arytype_klass = arytype->klass(); 111 if ((arytype_klass != NULL) && (!arytype_klass->is_loaded())) { 112 // Only fails for some -Xcomp runs 113 // The class is unloaded. We have to run this bytecode in the interpreter. 114 uncommon_trap(Deoptimization::Reason_unloaded, 115 Deoptimization::Action_reinterpret, 116 arytype->klass(), "!loaded array"); 117 return top(); 118 } 119 120 // Do the range check 121 if (GenerateRangeChecks && need_range_check) { 122 Node* tst; 123 if (sizetype->_hi <= 0) { 124 // The greatest array bound is negative, so we can conclude that we're 125 // compiling unreachable code, but the unsigned compare trick used below 126 // only works with non-negative lengths. Instead, hack "tst" to be zero so 127 // the uncommon_trap path will always be taken. 128 tst = _gvn.intcon(0); 129 } else { 130 // Range is constant in array-oop, so we can use the original state of mem 131 Node* len = load_array_length(ary); 132 133 // Test length vs index (standard trick using unsigned compare) 134 Node* chk = _gvn.transform( new CmpUNode(idx, len) ); 135 BoolTest::mask btest = BoolTest::lt; 136 tst = _gvn.transform( new BoolNode(chk, btest) ); 137 } 138 // Branch to failure if out of bounds 139 { BuildCutout unless(this, tst, PROB_MAX); 140 if (C->allow_range_check_smearing()) { 141 // Do not use builtin_throw, since range checks are sometimes 142 // made more stringent by an optimistic transformation. 143 // This creates "tentative" range checks at this point, 144 // which are not guaranteed to throw exceptions. 145 // See IfNode::Ideal, is_range_check, adjust_check. 146 uncommon_trap(Deoptimization::Reason_range_check, 147 Deoptimization::Action_make_not_entrant, 148 NULL, "range_check"); 149 } else { 150 // If we have already recompiled with the range-check-widening 151 // heroic optimization turned off, then we must really be throwing 152 // range check exceptions. 153 builtin_throw(Deoptimization::Reason_range_check, idx); 154 } 155 } 156 } 157 // Check for always knowing you are throwing a range-check exception 158 if (stopped()) return top(); 159 160 Node* ptr = array_element_address(ary, idx, type, sizetype); 161 162 if (result2 != NULL) *result2 = elemtype; 163 164 assert(ptr != top(), "top should go hand-in-hand with stopped"); 165 166 return ptr; 167 } 168 169 170 // returns IfNode 171 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask) { 172 Node *cmp = _gvn.transform( new CmpINode( a, b)); // two cases: shiftcount > 32 and shiftcount <= 32 173 Node *tst = _gvn.transform( new BoolNode( cmp, mask)); 174 IfNode *iff = create_and_map_if( control(), tst, ((mask == BoolTest::eq) ? PROB_STATIC_INFREQUENT : PROB_FAIR), COUNT_UNKNOWN ); 175 return iff; 176 } 177 178 // return Region node 179 Node* Parse::jump_if_join(Node* iffalse, Node* iftrue) { 180 Node *region = new RegionNode(3); // 2 results 181 record_for_igvn(region); 182 region->init_req(1, iffalse); 183 region->init_req(2, iftrue ); 184 _gvn.set_type(region, Type::CONTROL); 185 region = _gvn.transform(region); 186 set_control (region); 187 return region; 188 } 189 190 191 //------------------------------helper for tableswitch------------------------- 192 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index) { 193 // True branch, use existing map info 194 { PreserveJVMState pjvms(this); 195 Node *iftrue = _gvn.transform( new IfTrueNode (iff) ); 196 set_control( iftrue ); 197 profile_switch_case(prof_table_index); 198 merge_new_path(dest_bci_if_true); 199 } 200 201 // False branch 202 Node *iffalse = _gvn.transform( new IfFalseNode(iff) ); 203 set_control( iffalse ); 204 } 205 206 void Parse::jump_if_false_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index) { 207 // True branch, use existing map info 208 { PreserveJVMState pjvms(this); 209 Node *iffalse = _gvn.transform( new IfFalseNode (iff) ); 210 set_control( iffalse ); 211 profile_switch_case(prof_table_index); 212 merge_new_path(dest_bci_if_true); 213 } 214 215 // False branch 216 Node *iftrue = _gvn.transform( new IfTrueNode(iff) ); 217 set_control( iftrue ); 218 } 219 220 void Parse::jump_if_always_fork(int dest_bci, int prof_table_index) { 221 // False branch, use existing map and control() 222 profile_switch_case(prof_table_index); 223 merge_new_path(dest_bci); 224 } 225 226 227 extern "C" { 228 static int jint_cmp(const void *i, const void *j) { 229 int a = *(jint *)i; 230 int b = *(jint *)j; 231 return a > b ? 1 : a < b ? -1 : 0; 232 } 233 } 234 235 236 // Default value for methodData switch indexing. Must be a negative value to avoid 237 // conflict with any legal switch index. 238 #define NullTableIndex -1 239 240 class SwitchRange : public StackObj { 241 // a range of integers coupled with a bci destination 242 jint _lo; // inclusive lower limit 243 jint _hi; // inclusive upper limit 244 int _dest; 245 int _table_index; // index into method data table 246 247 public: 248 jint lo() const { return _lo; } 249 jint hi() const { return _hi; } 250 int dest() const { return _dest; } 251 int table_index() const { return _table_index; } 252 bool is_singleton() const { return _lo == _hi; } 253 254 void setRange(jint lo, jint hi, int dest, int table_index) { 255 assert(lo <= hi, "must be a non-empty range"); 256 _lo = lo, _hi = hi; _dest = dest; _table_index = table_index; 257 } 258 bool adjoinRange(jint lo, jint hi, int dest, int table_index) { 259 assert(lo <= hi, "must be a non-empty range"); 260 if (lo == _hi+1 && dest == _dest && table_index == _table_index) { 261 _hi = hi; 262 return true; 263 } 264 return false; 265 } 266 267 void set (jint value, int dest, int table_index) { 268 setRange(value, value, dest, table_index); 269 } 270 bool adjoin(jint value, int dest, int table_index) { 271 return adjoinRange(value, value, dest, table_index); 272 } 273 274 void print() { 275 if (is_singleton()) 276 tty->print(" {%d}=>%d", lo(), dest()); 277 else if (lo() == min_jint) 278 tty->print(" {..%d}=>%d", hi(), dest()); 279 else if (hi() == max_jint) 280 tty->print(" {%d..}=>%d", lo(), dest()); 281 else 282 tty->print(" {%d..%d}=>%d", lo(), hi(), dest()); 283 } 284 }; 285 286 287 //-------------------------------do_tableswitch-------------------------------- 288 void Parse::do_tableswitch() { 289 Node* lookup = pop(); 290 291 // Get information about tableswitch 292 int default_dest = iter().get_dest_table(0); 293 int lo_index = iter().get_int_table(1); 294 int hi_index = iter().get_int_table(2); 295 int len = hi_index - lo_index + 1; 296 297 if (len < 1) { 298 // If this is a backward branch, add safepoint 299 maybe_add_safepoint(default_dest); 300 merge(default_dest); 301 return; 302 } 303 304 // generate decision tree, using trichotomy when possible 305 int rnum = len+2; 306 bool makes_backward_branch = false; 307 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum); 308 int rp = -1; 309 if (lo_index != min_jint) { 310 ranges[++rp].setRange(min_jint, lo_index-1, default_dest, NullTableIndex); 311 } 312 for (int j = 0; j < len; j++) { 313 jint match_int = lo_index+j; 314 int dest = iter().get_dest_table(j+3); 315 makes_backward_branch |= (dest <= bci()); 316 int table_index = method_data_update() ? j : NullTableIndex; 317 if (rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index)) { 318 ranges[++rp].set(match_int, dest, table_index); 319 } 320 } 321 jint highest = lo_index+(len-1); 322 assert(ranges[rp].hi() == highest, ""); 323 if (highest != max_jint 324 && !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex)) { 325 ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex); 326 } 327 assert(rp < len+2, "not too many ranges"); 328 329 // Safepoint in case if backward branch observed 330 if( makes_backward_branch && UseLoopSafepoints ) 331 add_safepoint(); 332 333 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]); 334 } 335 336 337 //------------------------------do_lookupswitch-------------------------------- 338 void Parse::do_lookupswitch() { 339 Node *lookup = pop(); // lookup value 340 // Get information about lookupswitch 341 int default_dest = iter().get_dest_table(0); 342 int len = iter().get_int_table(1); 343 344 if (len < 1) { // If this is a backward branch, add safepoint 345 maybe_add_safepoint(default_dest); 346 merge(default_dest); 347 return; 348 } 349 350 // generate decision tree, using trichotomy when possible 351 jint* table = NEW_RESOURCE_ARRAY(jint, len*2); 352 { 353 for( int j = 0; j < len; j++ ) { 354 table[j+j+0] = iter().get_int_table(2+j+j); 355 table[j+j+1] = iter().get_dest_table(2+j+j+1); 356 } 357 qsort( table, len, 2*sizeof(table[0]), jint_cmp ); 358 } 359 360 int rnum = len*2+1; 361 bool makes_backward_branch = false; 362 SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum); 363 int rp = -1; 364 for( int j = 0; j < len; j++ ) { 365 jint match_int = table[j+j+0]; 366 int dest = table[j+j+1]; 367 int next_lo = rp < 0 ? min_jint : ranges[rp].hi()+1; 368 int table_index = method_data_update() ? j : NullTableIndex; 369 makes_backward_branch |= (dest <= bci()); 370 if( match_int != next_lo ) { 371 ranges[++rp].setRange(next_lo, match_int-1, default_dest, NullTableIndex); 372 } 373 if( rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index) ) { 374 ranges[++rp].set(match_int, dest, table_index); 375 } 376 } 377 jint highest = table[2*(len-1)]; 378 assert(ranges[rp].hi() == highest, ""); 379 if( highest != max_jint 380 && !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex) ) { 381 ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex); 382 } 383 assert(rp < rnum, "not too many ranges"); 384 385 // Safepoint in case backward branch observed 386 if( makes_backward_branch && UseLoopSafepoints ) 387 add_safepoint(); 388 389 jump_switch_ranges(lookup, &ranges[0], &ranges[rp]); 390 } 391 392 //----------------------------create_jump_tables------------------------------- 393 bool Parse::create_jump_tables(Node* key_val, SwitchRange* lo, SwitchRange* hi) { 394 // Are jumptables enabled 395 if (!UseJumpTables) return false; 396 397 // Are jumptables supported 398 if (!Matcher::has_match_rule(Op_Jump)) return false; 399 400 // Don't make jump table if profiling 401 if (method_data_update()) return false; 402 403 // Decide if a guard is needed to lop off big ranges at either (or 404 // both) end(s) of the input set. We'll call this the default target 405 // even though we can't be sure that it is the true "default". 406 407 bool needs_guard = false; 408 int default_dest; 409 int64_t total_outlier_size = 0; 410 int64_t hi_size = ((int64_t)hi->hi()) - ((int64_t)hi->lo()) + 1; 411 int64_t lo_size = ((int64_t)lo->hi()) - ((int64_t)lo->lo()) + 1; 412 413 if (lo->dest() == hi->dest()) { 414 total_outlier_size = hi_size + lo_size; 415 default_dest = lo->dest(); 416 } else if (lo_size > hi_size) { 417 total_outlier_size = lo_size; 418 default_dest = lo->dest(); 419 } else { 420 total_outlier_size = hi_size; 421 default_dest = hi->dest(); 422 } 423 424 // If a guard test will eliminate very sparse end ranges, then 425 // it is worth the cost of an extra jump. 426 if (total_outlier_size > (MaxJumpTableSparseness * 4)) { 427 needs_guard = true; 428 if (default_dest == lo->dest()) lo++; 429 if (default_dest == hi->dest()) hi--; 430 } 431 432 // Find the total number of cases and ranges 433 int64_t num_cases = ((int64_t)hi->hi()) - ((int64_t)lo->lo()) + 1; 434 int num_range = hi - lo + 1; 435 436 // Don't create table if: too large, too small, or too sparse. 437 if (num_cases < MinJumpTableSize || num_cases > MaxJumpTableSize) 438 return false; 439 if (num_cases > (MaxJumpTableSparseness * num_range)) 440 return false; 441 442 // Normalize table lookups to zero 443 int lowval = lo->lo(); 444 key_val = _gvn.transform( new SubINode(key_val, _gvn.intcon(lowval)) ); 445 446 // Generate a guard to protect against input keyvals that aren't 447 // in the switch domain. 448 if (needs_guard) { 449 Node* size = _gvn.intcon(num_cases); 450 Node* cmp = _gvn.transform( new CmpUNode(key_val, size) ); 451 Node* tst = _gvn.transform( new BoolNode(cmp, BoolTest::ge) ); 452 IfNode* iff = create_and_map_if( control(), tst, PROB_FAIR, COUNT_UNKNOWN); 453 jump_if_true_fork(iff, default_dest, NullTableIndex); 454 } 455 456 // Create an ideal node JumpTable that has projections 457 // of all possible ranges for a switch statement 458 // The key_val input must be converted to a pointer offset and scaled. 459 // Compare Parse::array_addressing above. 460 #ifdef _LP64 461 // Clean the 32-bit int into a real 64-bit offset. 462 // Otherwise, the jint value 0 might turn into an offset of 0x0800000000. 463 const TypeLong* lkeytype = TypeLong::make(CONST64(0), num_cases-1, Type::WidenMin); 464 key_val = _gvn.transform( new ConvI2LNode(key_val, lkeytype) ); 465 #endif 466 // Shift the value by wordsize so we have an index into the table, rather 467 // than a switch value 468 Node *shiftWord = _gvn.MakeConX(wordSize); 469 key_val = _gvn.transform( new MulXNode( key_val, shiftWord)); 470 471 // Create the JumpNode 472 Node* jtn = _gvn.transform( new JumpNode(control(), key_val, num_cases) ); 473 474 // These are the switch destinations hanging off the jumpnode 475 int i = 0; 476 for (SwitchRange* r = lo; r <= hi; r++) { 477 for (int64_t j = r->lo(); j <= r->hi(); j++, i++) { 478 Node* input = _gvn.transform(new JumpProjNode(jtn, i, r->dest(), (int)(j - lowval))); 479 { 480 PreserveJVMState pjvms(this); 481 set_control(input); 482 jump_if_always_fork(r->dest(), r->table_index()); 483 } 484 } 485 } 486 assert(i == num_cases, "miscount of cases"); 487 stop_and_kill_map(); // no more uses for this JVMS 488 return true; 489 } 490 491 //----------------------------jump_switch_ranges------------------------------- 492 void Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, int switch_depth) { 493 Block* switch_block = block(); 494 495 if (switch_depth == 0) { 496 // Do special processing for the top-level call. 497 assert(lo->lo() == min_jint, "initial range must exhaust Type::INT"); 498 assert(hi->hi() == max_jint, "initial range must exhaust Type::INT"); 499 500 // Decrement pred-numbers for the unique set of nodes. 501 #ifdef ASSERT 502 // Ensure that the block's successors are a (duplicate-free) set. 503 int successors_counted = 0; // block occurrences in [hi..lo] 504 int unique_successors = switch_block->num_successors(); 505 for (int i = 0; i < unique_successors; i++) { 506 Block* target = switch_block->successor_at(i); 507 508 // Check that the set of successors is the same in both places. 509 int successors_found = 0; 510 for (SwitchRange* p = lo; p <= hi; p++) { 511 if (p->dest() == target->start()) successors_found++; 512 } 513 assert(successors_found > 0, "successor must be known"); 514 successors_counted += successors_found; 515 } 516 assert(successors_counted == (hi-lo)+1, "no unexpected successors"); 517 #endif 518 519 // Maybe prune the inputs, based on the type of key_val. 520 jint min_val = min_jint; 521 jint max_val = max_jint; 522 const TypeInt* ti = key_val->bottom_type()->isa_int(); 523 if (ti != NULL) { 524 min_val = ti->_lo; 525 max_val = ti->_hi; 526 assert(min_val <= max_val, "invalid int type"); 527 } 528 while (lo->hi() < min_val) lo++; 529 if (lo->lo() < min_val) lo->setRange(min_val, lo->hi(), lo->dest(), lo->table_index()); 530 while (hi->lo() > max_val) hi--; 531 if (hi->hi() > max_val) hi->setRange(hi->lo(), max_val, hi->dest(), hi->table_index()); 532 } 533 534 #ifndef PRODUCT 535 if (switch_depth == 0) { 536 _max_switch_depth = 0; 537 _est_switch_depth = log2_intptr((hi-lo+1)-1)+1; 538 } 539 #endif 540 541 assert(lo <= hi, "must be a non-empty set of ranges"); 542 if (lo == hi) { 543 jump_if_always_fork(lo->dest(), lo->table_index()); 544 } else { 545 assert(lo->hi() == (lo+1)->lo()-1, "contiguous ranges"); 546 assert(hi->lo() == (hi-1)->hi()+1, "contiguous ranges"); 547 548 if (create_jump_tables(key_val, lo, hi)) return; 549 550 int nr = hi - lo + 1; 551 552 SwitchRange* mid = lo + nr/2; 553 // if there is an easy choice, pivot at a singleton: 554 if (nr > 3 && !mid->is_singleton() && (mid-1)->is_singleton()) mid--; 555 556 assert(lo < mid && mid <= hi, "good pivot choice"); 557 assert(nr != 2 || mid == hi, "should pick higher of 2"); 558 assert(nr != 3 || mid == hi-1, "should pick middle of 3"); 559 560 Node *test_val = _gvn.intcon(mid->lo()); 561 562 if (mid->is_singleton()) { 563 IfNode *iff_ne = jump_if_fork_int(key_val, test_val, BoolTest::ne); 564 jump_if_false_fork(iff_ne, mid->dest(), mid->table_index()); 565 566 // Special Case: If there are exactly three ranges, and the high 567 // and low range each go to the same place, omit the "gt" test, 568 // since it will not discriminate anything. 569 bool eq_test_only = (hi == lo+2 && hi->dest() == lo->dest()); 570 if (eq_test_only) { 571 assert(mid == hi-1, ""); 572 } 573 574 // if there is a higher range, test for it and process it: 575 if (mid < hi && !eq_test_only) { 576 // two comparisons of same values--should enable 1 test for 2 branches 577 // Use BoolTest::le instead of BoolTest::gt 578 IfNode *iff_le = jump_if_fork_int(key_val, test_val, BoolTest::le); 579 Node *iftrue = _gvn.transform( new IfTrueNode(iff_le) ); 580 Node *iffalse = _gvn.transform( new IfFalseNode(iff_le) ); 581 { PreserveJVMState pjvms(this); 582 set_control(iffalse); 583 jump_switch_ranges(key_val, mid+1, hi, switch_depth+1); 584 } 585 set_control(iftrue); 586 } 587 588 } else { 589 // mid is a range, not a singleton, so treat mid..hi as a unit 590 IfNode *iff_ge = jump_if_fork_int(key_val, test_val, BoolTest::ge); 591 592 // if there is a higher range, test for it and process it: 593 if (mid == hi) { 594 jump_if_true_fork(iff_ge, mid->dest(), mid->table_index()); 595 } else { 596 Node *iftrue = _gvn.transform( new IfTrueNode(iff_ge) ); 597 Node *iffalse = _gvn.transform( new IfFalseNode(iff_ge) ); 598 { PreserveJVMState pjvms(this); 599 set_control(iftrue); 600 jump_switch_ranges(key_val, mid, hi, switch_depth+1); 601 } 602 set_control(iffalse); 603 } 604 } 605 606 // in any case, process the lower range 607 jump_switch_ranges(key_val, lo, mid-1, switch_depth+1); 608 } 609 610 // Decrease pred_count for each successor after all is done. 611 if (switch_depth == 0) { 612 int unique_successors = switch_block->num_successors(); 613 for (int i = 0; i < unique_successors; i++) { 614 Block* target = switch_block->successor_at(i); 615 // Throw away the pre-allocated path for each unique successor. 616 target->next_path_num(); 617 } 618 } 619 620 #ifndef PRODUCT 621 _max_switch_depth = MAX2(switch_depth, _max_switch_depth); 622 if (TraceOptoParse && Verbose && WizardMode && switch_depth == 0) { 623 SwitchRange* r; 624 int nsing = 0; 625 for( r = lo; r <= hi; r++ ) { 626 if( r->is_singleton() ) nsing++; 627 } 628 tty->print(">>> "); 629 _method->print_short_name(); 630 tty->print_cr(" switch decision tree"); 631 tty->print_cr(" %d ranges (%d singletons), max_depth=%d, est_depth=%d", 632 (int) (hi-lo+1), nsing, _max_switch_depth, _est_switch_depth); 633 if (_max_switch_depth > _est_switch_depth) { 634 tty->print_cr("******** BAD SWITCH DEPTH ********"); 635 } 636 tty->print(" "); 637 for( r = lo; r <= hi; r++ ) { 638 r->print(); 639 } 640 tty->cr(); 641 } 642 #endif 643 } 644 645 void Parse::modf() { 646 Node *f2 = pop(); 647 Node *f1 = pop(); 648 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::modf_Type(), 649 CAST_FROM_FN_PTR(address, SharedRuntime::frem), 650 "frem", NULL, //no memory effects 651 f1, f2); 652 Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0)); 653 654 push(res); 655 } 656 657 void Parse::modd() { 658 Node *d2 = pop_pair(); 659 Node *d1 = pop_pair(); 660 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::Math_DD_D_Type(), 661 CAST_FROM_FN_PTR(address, SharedRuntime::drem), 662 "drem", NULL, //no memory effects 663 d1, top(), d2, top()); 664 Node* res_d = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0)); 665 666 #ifdef ASSERT 667 Node* res_top = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 1)); 668 assert(res_top == top(), "second value must be top"); 669 #endif 670 671 push_pair(res_d); 672 } 673 674 void Parse::l2f() { 675 Node* f2 = pop(); 676 Node* f1 = pop(); 677 Node* c = make_runtime_call(RC_LEAF, OptoRuntime::l2f_Type(), 678 CAST_FROM_FN_PTR(address, SharedRuntime::l2f), 679 "l2f", NULL, //no memory effects 680 f1, f2); 681 Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0)); 682 683 push(res); 684 } 685 686 void Parse::do_irem() { 687 // Must keep both values on the expression-stack during null-check 688 zero_check_int(peek()); 689 // Compile-time detect of null-exception? 690 if (stopped()) return; 691 692 Node* b = pop(); 693 Node* a = pop(); 694 695 const Type *t = _gvn.type(b); 696 if (t != Type::TOP) { 697 const TypeInt *ti = t->is_int(); 698 if (ti->is_con()) { 699 int divisor = ti->get_con(); 700 // check for positive power of 2 701 if (divisor > 0 && 702 (divisor & ~(divisor-1)) == divisor) { 703 // yes ! 704 Node *mask = _gvn.intcon((divisor - 1)); 705 // Sigh, must handle negative dividends 706 Node *zero = _gvn.intcon(0); 707 IfNode *ifff = jump_if_fork_int(a, zero, BoolTest::lt); 708 Node *iff = _gvn.transform( new IfFalseNode(ifff) ); 709 Node *ift = _gvn.transform( new IfTrueNode (ifff) ); 710 Node *reg = jump_if_join(ift, iff); 711 Node *phi = PhiNode::make(reg, NULL, TypeInt::INT); 712 // Negative path; negate/and/negate 713 Node *neg = _gvn.transform( new SubINode(zero, a) ); 714 Node *andn= _gvn.transform( new AndINode(neg, mask) ); 715 Node *negn= _gvn.transform( new SubINode(zero, andn) ); 716 phi->init_req(1, negn); 717 // Fast positive case 718 Node *andx = _gvn.transform( new AndINode(a, mask) ); 719 phi->init_req(2, andx); 720 // Push the merge 721 push( _gvn.transform(phi) ); 722 return; 723 } 724 } 725 } 726 // Default case 727 push( _gvn.transform( new ModINode(control(),a,b) ) ); 728 } 729 730 // Handle jsr and jsr_w bytecode 731 void Parse::do_jsr() { 732 assert(bc() == Bytecodes::_jsr || bc() == Bytecodes::_jsr_w, "wrong bytecode"); 733 734 // Store information about current state, tagged with new _jsr_bci 735 int return_bci = iter().next_bci(); 736 int jsr_bci = (bc() == Bytecodes::_jsr) ? iter().get_dest() : iter().get_far_dest(); 737 738 // Update method data 739 profile_taken_branch(jsr_bci); 740 741 // The way we do things now, there is only one successor block 742 // for the jsr, because the target code is cloned by ciTypeFlow. 743 Block* target = successor_for_bci(jsr_bci); 744 745 // What got pushed? 746 const Type* ret_addr = target->peek(); 747 assert(ret_addr->singleton(), "must be a constant (cloned jsr body)"); 748 749 // Effect on jsr on stack 750 push(_gvn.makecon(ret_addr)); 751 752 // Flow to the jsr. 753 merge(jsr_bci); 754 } 755 756 // Handle ret bytecode 757 void Parse::do_ret() { 758 // Find to whom we return. 759 assert(block()->num_successors() == 1, "a ret can only go one place now"); 760 Block* target = block()->successor_at(0); 761 assert(!target->is_ready(), "our arrival must be expected"); 762 profile_ret(target->flow()->start()); 763 int pnum = target->next_path_num(); 764 merge_common(target, pnum); 765 } 766 767 //--------------------------dynamic_branch_prediction-------------------------- 768 // Try to gather dynamic branch prediction behavior. Return a probability 769 // of the branch being taken and set the "cnt" field. Returns a -1.0 770 // if we need to use static prediction for some reason. 771 float Parse::dynamic_branch_prediction(float &cnt, BoolTest::mask btest, Node* test) { 772 ResourceMark rm; 773 774 cnt = COUNT_UNKNOWN; 775 776 int taken = 0; 777 int not_taken = 0; 778 779 if (btest == BoolTest::eq && test->is_Cmp() && test->in(1)->Opcode() == Op_ProfileBranch) { 780 ProfileBranchNode* opq = (ProfileBranchNode*)test->in(1); 781 taken = opq->taken(); 782 not_taken = opq->not_taken(); 783 opq->consume(); 784 } else { 785 // Use MethodData information if it is available 786 // FIXME: free the ProfileData structure 787 ciMethodData* methodData = method()->method_data(); 788 if (!methodData->is_mature()) return PROB_UNKNOWN; 789 ciProfileData* data = methodData->bci_to_data(bci()); 790 if (!data->is_JumpData()) return PROB_UNKNOWN; 791 792 // get taken and not taken values 793 taken = data->as_JumpData()->taken(); 794 not_taken = 0; 795 if (data->is_BranchData()) { 796 not_taken = data->as_BranchData()->not_taken(); 797 } 798 799 // scale the counts to be commensurate with invocation counts: 800 taken = method()->scale_count(taken); 801 not_taken = method()->scale_count(not_taken); 802 } 803 804 // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful. 805 // We also check that individual counters are positive first, overwise the sum can become positive. 806 if (taken < 0 || not_taken < 0 || taken + not_taken < 40) { 807 if (C->log() != NULL) { 808 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken); 809 } 810 return PROB_UNKNOWN; 811 } 812 813 // Compute frequency that we arrive here 814 float sum = taken + not_taken; 815 // Adjust, if this block is a cloned private block but the 816 // Jump counts are shared. Taken the private counts for 817 // just this path instead of the shared counts. 818 if( block()->count() > 0 ) 819 sum = block()->count(); 820 cnt = sum / FreqCountInvocations; 821 822 // Pin probability to sane limits 823 float prob; 824 if( !taken ) 825 prob = (0+PROB_MIN) / 2; 826 else if( !not_taken ) 827 prob = (1+PROB_MAX) / 2; 828 else { // Compute probability of true path 829 prob = (float)taken / (float)(taken + not_taken); 830 if (prob > PROB_MAX) prob = PROB_MAX; 831 if (prob < PROB_MIN) prob = PROB_MIN; 832 } 833 834 assert((cnt > 0.0f) && (prob > 0.0f), 835 "Bad frequency assignment in if"); 836 837 if (C->log() != NULL) { 838 const char* prob_str = NULL; 839 if (prob >= PROB_MAX) prob_str = (prob == PROB_MAX) ? "max" : "always"; 840 if (prob <= PROB_MIN) prob_str = (prob == PROB_MIN) ? "min" : "never"; 841 char prob_str_buf[30]; 842 if (prob_str == NULL) { 843 sprintf(prob_str_buf, "%g", prob); 844 prob_str = prob_str_buf; 845 } 846 C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%g' prob='%s'", 847 iter().get_dest(), taken, not_taken, cnt, prob_str); 848 } 849 return prob; 850 } 851 852 //-----------------------------branch_prediction------------------------------- 853 float Parse::branch_prediction(float& cnt, 854 BoolTest::mask btest, 855 int target_bci, 856 Node* test) { 857 float prob = dynamic_branch_prediction(cnt, btest, test); 858 // If prob is unknown, switch to static prediction 859 if (prob != PROB_UNKNOWN) return prob; 860 861 prob = PROB_FAIR; // Set default value 862 if (btest == BoolTest::eq) // Exactly equal test? 863 prob = PROB_STATIC_INFREQUENT; // Assume its relatively infrequent 864 else if (btest == BoolTest::ne) 865 prob = PROB_STATIC_FREQUENT; // Assume its relatively frequent 866 867 // If this is a conditional test guarding a backwards branch, 868 // assume its a loop-back edge. Make it a likely taken branch. 869 if (target_bci < bci()) { 870 if (is_osr_parse()) { // Could be a hot OSR'd loop; force deopt 871 // Since it's an OSR, we probably have profile data, but since 872 // branch_prediction returned PROB_UNKNOWN, the counts are too small. 873 // Let's make a special check here for completely zero counts. 874 ciMethodData* methodData = method()->method_data(); 875 if (!methodData->is_empty()) { 876 ciProfileData* data = methodData->bci_to_data(bci()); 877 // Only stop for truly zero counts, which mean an unknown part 878 // of the OSR-ed method, and we want to deopt to gather more stats. 879 // If you have ANY counts, then this loop is simply 'cold' relative 880 // to the OSR loop. 881 if (data->as_BranchData()->taken() + 882 data->as_BranchData()->not_taken() == 0 ) { 883 // This is the only way to return PROB_UNKNOWN: 884 return PROB_UNKNOWN; 885 } 886 } 887 } 888 prob = PROB_STATIC_FREQUENT; // Likely to take backwards branch 889 } 890 891 assert(prob != PROB_UNKNOWN, "must have some guess at this point"); 892 return prob; 893 } 894 895 // The magic constants are chosen so as to match the output of 896 // branch_prediction() when the profile reports a zero taken count. 897 // It is important to distinguish zero counts unambiguously, because 898 // some branches (e.g., _213_javac.Assembler.eliminate) validly produce 899 // very small but nonzero probabilities, which if confused with zero 900 // counts would keep the program recompiling indefinitely. 901 bool Parse::seems_never_taken(float prob) const { 902 return prob < PROB_MIN; 903 } 904 905 // True if the comparison seems to be the kind that will not change its 906 // statistics from true to false. See comments in adjust_map_after_if. 907 // This question is only asked along paths which are already 908 // classifed as untaken (by seems_never_taken), so really, 909 // if a path is never taken, its controlling comparison is 910 // already acting in a stable fashion. If the comparison 911 // seems stable, we will put an expensive uncommon trap 912 // on the untaken path. 913 bool Parse::seems_stable_comparison() const { 914 if (C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if)) { 915 return false; 916 } 917 return true; 918 } 919 920 //-------------------------------repush_if_args-------------------------------- 921 // Push arguments of an "if" bytecode back onto the stack by adjusting _sp. 922 inline int Parse::repush_if_args() { 923 #ifndef PRODUCT 924 if (PrintOpto && WizardMode) { 925 tty->print("defending against excessive implicit null exceptions on %s @%d in ", 926 Bytecodes::name(iter().cur_bc()), iter().cur_bci()); 927 method()->print_name(); tty->cr(); 928 } 929 #endif 930 int bc_depth = - Bytecodes::depth(iter().cur_bc()); 931 assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches"); 932 DEBUG_ONLY(sync_jvms()); // argument(n) requires a synced jvms 933 assert(argument(0) != NULL, "must exist"); 934 assert(bc_depth == 1 || argument(1) != NULL, "two must exist"); 935 inc_sp(bc_depth); 936 return bc_depth; 937 } 938 939 //----------------------------------do_ifnull---------------------------------- 940 void Parse::do_ifnull(BoolTest::mask btest, Node *c) { 941 int target_bci = iter().get_dest(); 942 943 Block* branch_block = successor_for_bci(target_bci); 944 Block* next_block = successor_for_bci(iter().next_bci()); 945 946 float cnt; 947 float prob = branch_prediction(cnt, btest, target_bci, c); 948 if (prob == PROB_UNKNOWN) { 949 // (An earlier version of do_ifnull omitted this trap for OSR methods.) 950 #ifndef PRODUCT 951 if (PrintOpto && Verbose) 952 tty->print_cr("Never-taken edge stops compilation at bci %d",bci()); 953 #endif 954 repush_if_args(); // to gather stats on loop 955 // We need to mark this branch as taken so that if we recompile we will 956 // see that it is possible. In the tiered system the interpreter doesn't 957 // do profiling and by the time we get to the lower tier from the interpreter 958 // the path may be cold again. Make sure it doesn't look untaken 959 profile_taken_branch(target_bci, !ProfileInterpreter); 960 uncommon_trap(Deoptimization::Reason_unreached, 961 Deoptimization::Action_reinterpret, 962 NULL, "cold"); 963 if (C->eliminate_boxing()) { 964 // Mark the successor blocks as parsed 965 branch_block->next_path_num(); 966 next_block->next_path_num(); 967 } 968 return; 969 } 970 971 explicit_null_checks_inserted++; 972 973 // Generate real control flow 974 Node *tst = _gvn.transform( new BoolNode( c, btest ) ); 975 976 // Sanity check the probability value 977 assert(prob > 0.0f,"Bad probability in Parser"); 978 // Need xform to put node in hash table 979 IfNode *iff = create_and_xform_if( control(), tst, prob, cnt ); 980 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser"); 981 // True branch 982 { PreserveJVMState pjvms(this); 983 Node* iftrue = _gvn.transform( new IfTrueNode (iff) ); 984 set_control(iftrue); 985 986 if (stopped()) { // Path is dead? 987 explicit_null_checks_elided++; 988 if (C->eliminate_boxing()) { 989 // Mark the successor block as parsed 990 branch_block->next_path_num(); 991 } 992 } else { // Path is live. 993 // Update method data 994 profile_taken_branch(target_bci); 995 adjust_map_after_if(btest, c, prob, branch_block, next_block); 996 if (!stopped()) { 997 merge(target_bci); 998 } 999 } 1000 } 1001 1002 // False branch 1003 Node* iffalse = _gvn.transform( new IfFalseNode(iff) ); 1004 set_control(iffalse); 1005 1006 if (stopped()) { // Path is dead? 1007 explicit_null_checks_elided++; 1008 if (C->eliminate_boxing()) { 1009 // Mark the successor block as parsed 1010 next_block->next_path_num(); 1011 } 1012 } else { // Path is live. 1013 // Update method data 1014 profile_not_taken_branch(); 1015 adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, 1016 next_block, branch_block); 1017 } 1018 } 1019 1020 //------------------------------------do_if------------------------------------ 1021 void Parse::do_if(BoolTest::mask btest, Node* c) { 1022 int target_bci = iter().get_dest(); 1023 1024 Block* branch_block = successor_for_bci(target_bci); 1025 Block* next_block = successor_for_bci(iter().next_bci()); 1026 1027 float cnt; 1028 float prob = branch_prediction(cnt, btest, target_bci, c); 1029 float untaken_prob = 1.0 - prob; 1030 1031 if (prob == PROB_UNKNOWN) { 1032 #ifndef PRODUCT 1033 if (PrintOpto && Verbose) 1034 tty->print_cr("Never-taken edge stops compilation at bci %d",bci()); 1035 #endif 1036 repush_if_args(); // to gather stats on loop 1037 // We need to mark this branch as taken so that if we recompile we will 1038 // see that it is possible. In the tiered system the interpreter doesn't 1039 // do profiling and by the time we get to the lower tier from the interpreter 1040 // the path may be cold again. Make sure it doesn't look untaken 1041 profile_taken_branch(target_bci, !ProfileInterpreter); 1042 uncommon_trap(Deoptimization::Reason_unreached, 1043 Deoptimization::Action_reinterpret, 1044 NULL, "cold"); 1045 if (C->eliminate_boxing()) { 1046 // Mark the successor blocks as parsed 1047 branch_block->next_path_num(); 1048 next_block->next_path_num(); 1049 } 1050 return; 1051 } 1052 1053 // Sanity check the probability value 1054 assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser"); 1055 1056 bool taken_if_true = true; 1057 // Convert BoolTest to canonical form: 1058 if (!BoolTest(btest).is_canonical()) { 1059 btest = BoolTest(btest).negate(); 1060 taken_if_true = false; 1061 // prob is NOT updated here; it remains the probability of the taken 1062 // path (as opposed to the prob of the path guarded by an 'IfTrueNode'). 1063 } 1064 assert(btest != BoolTest::eq, "!= is the only canonical exact test"); 1065 1066 Node* tst0 = new BoolNode(c, btest); 1067 Node* tst = _gvn.transform(tst0); 1068 BoolTest::mask taken_btest = BoolTest::illegal; 1069 BoolTest::mask untaken_btest = BoolTest::illegal; 1070 1071 if (tst->is_Bool()) { 1072 // Refresh c from the transformed bool node, since it may be 1073 // simpler than the original c. Also re-canonicalize btest. 1074 // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p NULL)). 1075 // That can arise from statements like: if (x instanceof C) ... 1076 if (tst != tst0) { 1077 // Canonicalize one more time since transform can change it. 1078 btest = tst->as_Bool()->_test._test; 1079 if (!BoolTest(btest).is_canonical()) { 1080 // Reverse edges one more time... 1081 tst = _gvn.transform( tst->as_Bool()->negate(&_gvn) ); 1082 btest = tst->as_Bool()->_test._test; 1083 assert(BoolTest(btest).is_canonical(), "sanity"); 1084 taken_if_true = !taken_if_true; 1085 } 1086 c = tst->in(1); 1087 } 1088 BoolTest::mask neg_btest = BoolTest(btest).negate(); 1089 taken_btest = taken_if_true ? btest : neg_btest; 1090 untaken_btest = taken_if_true ? neg_btest : btest; 1091 } 1092 1093 // Generate real control flow 1094 float true_prob = (taken_if_true ? prob : untaken_prob); 1095 IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt); 1096 assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser"); 1097 Node* taken_branch = new IfTrueNode(iff); 1098 Node* untaken_branch = new IfFalseNode(iff); 1099 if (!taken_if_true) { // Finish conversion to canonical form 1100 Node* tmp = taken_branch; 1101 taken_branch = untaken_branch; 1102 untaken_branch = tmp; 1103 } 1104 1105 // Branch is taken: 1106 { PreserveJVMState pjvms(this); 1107 taken_branch = _gvn.transform(taken_branch); 1108 set_control(taken_branch); 1109 1110 if (stopped()) { 1111 if (C->eliminate_boxing()) { 1112 // Mark the successor block as parsed 1113 branch_block->next_path_num(); 1114 } 1115 } else { 1116 // Update method data 1117 profile_taken_branch(target_bci); 1118 adjust_map_after_if(taken_btest, c, prob, branch_block, next_block); 1119 if (!stopped()) { 1120 merge(target_bci); 1121 } 1122 } 1123 } 1124 1125 untaken_branch = _gvn.transform(untaken_branch); 1126 set_control(untaken_branch); 1127 1128 // Branch not taken. 1129 if (stopped()) { 1130 if (C->eliminate_boxing()) { 1131 // Mark the successor block as parsed 1132 next_block->next_path_num(); 1133 } 1134 } else { 1135 // Update method data 1136 profile_not_taken_branch(); 1137 adjust_map_after_if(untaken_btest, c, untaken_prob, 1138 next_block, branch_block); 1139 } 1140 } 1141 1142 bool Parse::path_is_suitable_for_uncommon_trap(float prob) const { 1143 // Don't want to speculate on uncommon traps when running with -Xcomp 1144 if (!UseInterpreter) { 1145 return false; 1146 } 1147 return (seems_never_taken(prob) && seems_stable_comparison()); 1148 } 1149 1150 //----------------------------adjust_map_after_if------------------------------ 1151 // Adjust the JVM state to reflect the result of taking this path. 1152 // Basically, it means inspecting the CmpNode controlling this 1153 // branch, seeing how it constrains a tested value, and then 1154 // deciding if it's worth our while to encode this constraint 1155 // as graph nodes in the current abstract interpretation map. 1156 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, 1157 Block* path, Block* other_path) { 1158 if (stopped() || !c->is_Cmp() || btest == BoolTest::illegal) 1159 return; // nothing to do 1160 1161 bool is_fallthrough = (path == successor_for_bci(iter().next_bci())); 1162 1163 if (path_is_suitable_for_uncommon_trap(prob)) { 1164 repush_if_args(); 1165 uncommon_trap(Deoptimization::Reason_unstable_if, 1166 Deoptimization::Action_reinterpret, 1167 NULL, 1168 (is_fallthrough ? "taken always" : "taken never")); 1169 return; 1170 } 1171 1172 Node* val = c->in(1); 1173 Node* con = c->in(2); 1174 const Type* tcon = _gvn.type(con); 1175 const Type* tval = _gvn.type(val); 1176 bool have_con = tcon->singleton(); 1177 if (tval->singleton()) { 1178 if (!have_con) { 1179 // Swap, so constant is in con. 1180 con = val; 1181 tcon = tval; 1182 val = c->in(2); 1183 tval = _gvn.type(val); 1184 btest = BoolTest(btest).commute(); 1185 have_con = true; 1186 } else { 1187 // Do we have two constants? Then leave well enough alone. 1188 have_con = false; 1189 } 1190 } 1191 if (!have_con) // remaining adjustments need a con 1192 return; 1193 1194 sharpen_type_after_if(btest, con, tcon, val, tval); 1195 } 1196 1197 1198 static Node* extract_obj_from_klass_load(PhaseGVN* gvn, Node* n) { 1199 Node* ldk; 1200 if (n->is_DecodeNKlass()) { 1201 if (n->in(1)->Opcode() != Op_LoadNKlass) { 1202 return NULL; 1203 } else { 1204 ldk = n->in(1); 1205 } 1206 } else if (n->Opcode() != Op_LoadKlass) { 1207 return NULL; 1208 } else { 1209 ldk = n; 1210 } 1211 assert(ldk != NULL && ldk->is_Load(), "should have found a LoadKlass or LoadNKlass node"); 1212 1213 Node* adr = ldk->in(MemNode::Address); 1214 intptr_t off = 0; 1215 Node* obj = AddPNode::Ideal_base_and_offset(adr, gvn, off); 1216 if (obj == NULL || off != oopDesc::klass_offset_in_bytes()) // loading oopDesc::_klass? 1217 return NULL; 1218 const TypePtr* tp = gvn->type(obj)->is_ptr(); 1219 if (tp == NULL || !(tp->isa_instptr() || tp->isa_aryptr())) // is obj a Java object ptr? 1220 return NULL; 1221 1222 return obj; 1223 } 1224 1225 void Parse::sharpen_type_after_if(BoolTest::mask btest, 1226 Node* con, const Type* tcon, 1227 Node* val, const Type* tval) { 1228 // Look for opportunities to sharpen the type of a node 1229 // whose klass is compared with a constant klass. 1230 if (btest == BoolTest::eq && tcon->isa_klassptr()) { 1231 Node* obj = extract_obj_from_klass_load(&_gvn, val); 1232 const TypeOopPtr* con_type = tcon->isa_klassptr()->as_instance_type(); 1233 if (obj != NULL && (con_type->isa_instptr() || con_type->isa_aryptr())) { 1234 // Found: 1235 // Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq]) 1236 // or the narrowOop equivalent. 1237 const Type* obj_type = _gvn.type(obj); 1238 const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr(); 1239 if (tboth != NULL && tboth->klass_is_exact() && tboth != obj_type && 1240 tboth->higher_equal(obj_type)) { 1241 // obj has to be of the exact type Foo if the CmpP succeeds. 1242 int obj_in_map = map()->find_edge(obj); 1243 JVMState* jvms = this->jvms(); 1244 if (obj_in_map >= 0 && 1245 (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) { 1246 TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth); 1247 const Type* tcc = ccast->as_Type()->type(); 1248 assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve"); 1249 // Delay transform() call to allow recovery of pre-cast value 1250 // at the control merge. 1251 _gvn.set_type_bottom(ccast); 1252 record_for_igvn(ccast); 1253 // Here's the payoff. 1254 replace_in_map(obj, ccast); 1255 } 1256 } 1257 } 1258 } 1259 1260 int val_in_map = map()->find_edge(val); 1261 if (val_in_map < 0) return; // replace_in_map would be useless 1262 { 1263 JVMState* jvms = this->jvms(); 1264 if (!(jvms->is_loc(val_in_map) || 1265 jvms->is_stk(val_in_map))) 1266 return; // again, it would be useless 1267 } 1268 1269 // Check for a comparison to a constant, and "know" that the compared 1270 // value is constrained on this path. 1271 assert(tcon->singleton(), ""); 1272 ConstraintCastNode* ccast = NULL; 1273 Node* cast = NULL; 1274 1275 switch (btest) { 1276 case BoolTest::eq: // Constant test? 1277 { 1278 const Type* tboth = tcon->join_speculative(tval); 1279 if (tboth == tval) break; // Nothing to gain. 1280 if (tcon->isa_int()) { 1281 ccast = new CastIINode(val, tboth); 1282 } else if (tcon == TypePtr::NULL_PTR) { 1283 // Cast to null, but keep the pointer identity temporarily live. 1284 ccast = new CastPPNode(val, tboth); 1285 } else { 1286 const TypeF* tf = tcon->isa_float_constant(); 1287 const TypeD* td = tcon->isa_double_constant(); 1288 // Exclude tests vs float/double 0 as these could be 1289 // either +0 or -0. Just because you are equal to +0 1290 // doesn't mean you ARE +0! 1291 // Note, following code also replaces Long and Oop values. 1292 if ((!tf || tf->_f != 0.0) && 1293 (!td || td->_d != 0.0)) 1294 cast = con; // Replace non-constant val by con. 1295 } 1296 } 1297 break; 1298 1299 case BoolTest::ne: 1300 if (tcon == TypePtr::NULL_PTR) { 1301 cast = cast_not_null(val, false); 1302 } 1303 break; 1304 1305 default: 1306 // (At this point we could record int range types with CastII.) 1307 break; 1308 } 1309 1310 if (ccast != NULL) { 1311 const Type* tcc = ccast->as_Type()->type(); 1312 assert(tcc != tval && tcc->higher_equal(tval), "must improve"); 1313 // Delay transform() call to allow recovery of pre-cast value 1314 // at the control merge. 1315 ccast->set_req(0, control()); 1316 _gvn.set_type_bottom(ccast); 1317 record_for_igvn(ccast); 1318 cast = ccast; 1319 } 1320 1321 if (cast != NULL) { // Here's the payoff. 1322 replace_in_map(val, cast); 1323 } 1324 } 1325 1326 /** 1327 * Use speculative type to optimize CmpP node: if comparison is 1328 * against the low level class, cast the object to the speculative 1329 * type if any. CmpP should then go away. 1330 * 1331 * @param c expected CmpP node 1332 * @return result of CmpP on object casted to speculative type 1333 * 1334 */ 1335 Node* Parse::optimize_cmp_with_klass(Node* c) { 1336 // If this is transformed by the _gvn to a comparison with the low 1337 // level klass then we may be able to use speculation 1338 if (c->Opcode() == Op_CmpP && 1339 (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) && 1340 c->in(2)->is_Con()) { 1341 Node* load_klass = NULL; 1342 Node* decode = NULL; 1343 if (c->in(1)->Opcode() == Op_DecodeNKlass) { 1344 decode = c->in(1); 1345 load_klass = c->in(1)->in(1); 1346 } else { 1347 load_klass = c->in(1); 1348 } 1349 if (load_klass->in(2)->is_AddP()) { 1350 Node* addp = load_klass->in(2); 1351 Node* obj = addp->in(AddPNode::Address); 1352 const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr(); 1353 if (obj_type->speculative_type_not_null() != NULL) { 1354 ciKlass* k = obj_type->speculative_type(); 1355 inc_sp(2); 1356 obj = maybe_cast_profiled_obj(obj, k); 1357 dec_sp(2); 1358 // Make the CmpP use the casted obj 1359 addp = basic_plus_adr(obj, addp->in(AddPNode::Offset)); 1360 load_klass = load_klass->clone(); 1361 load_klass->set_req(2, addp); 1362 load_klass = _gvn.transform(load_klass); 1363 if (decode != NULL) { 1364 decode = decode->clone(); 1365 decode->set_req(1, load_klass); 1366 load_klass = _gvn.transform(decode); 1367 } 1368 c = c->clone(); 1369 c->set_req(1, load_klass); 1370 c = _gvn.transform(c); 1371 } 1372 } 1373 } 1374 return c; 1375 } 1376 1377 //------------------------------do_one_bytecode-------------------------------- 1378 // Parse this bytecode, and alter the Parsers JVM->Node mapping 1379 void Parse::do_one_bytecode() { 1380 Node *a, *b, *c, *d; // Handy temps 1381 BoolTest::mask btest; 1382 int i; 1383 1384 assert(!has_exceptions(), "bytecode entry state must be clear of throws"); 1385 1386 if (C->check_node_count(NodeLimitFudgeFactor * 5, 1387 "out of nodes parsing method")) { 1388 return; 1389 } 1390 1391 #ifdef ASSERT 1392 // for setting breakpoints 1393 if (TraceOptoParse) { 1394 tty->print(" @"); 1395 dump_bci(bci()); 1396 tty->cr(); 1397 } 1398 #endif 1399 1400 switch (bc()) { 1401 case Bytecodes::_nop: 1402 // do nothing 1403 break; 1404 case Bytecodes::_lconst_0: 1405 push_pair(longcon(0)); 1406 break; 1407 1408 case Bytecodes::_lconst_1: 1409 push_pair(longcon(1)); 1410 break; 1411 1412 case Bytecodes::_fconst_0: 1413 push(zerocon(T_FLOAT)); 1414 break; 1415 1416 case Bytecodes::_fconst_1: 1417 push(makecon(TypeF::ONE)); 1418 break; 1419 1420 case Bytecodes::_fconst_2: 1421 push(makecon(TypeF::make(2.0f))); 1422 break; 1423 1424 case Bytecodes::_dconst_0: 1425 push_pair(zerocon(T_DOUBLE)); 1426 break; 1427 1428 case Bytecodes::_dconst_1: 1429 push_pair(makecon(TypeD::ONE)); 1430 break; 1431 1432 case Bytecodes::_iconst_m1:push(intcon(-1)); break; 1433 case Bytecodes::_iconst_0: push(intcon( 0)); break; 1434 case Bytecodes::_iconst_1: push(intcon( 1)); break; 1435 case Bytecodes::_iconst_2: push(intcon( 2)); break; 1436 case Bytecodes::_iconst_3: push(intcon( 3)); break; 1437 case Bytecodes::_iconst_4: push(intcon( 4)); break; 1438 case Bytecodes::_iconst_5: push(intcon( 5)); break; 1439 case Bytecodes::_bipush: push(intcon(iter().get_constant_u1())); break; 1440 case Bytecodes::_sipush: push(intcon(iter().get_constant_u2())); break; 1441 case Bytecodes::_aconst_null: push(null()); break; 1442 case Bytecodes::_ldc: 1443 case Bytecodes::_ldc_w: 1444 case Bytecodes::_ldc2_w: 1445 // If the constant is unresolved, run this BC once in the interpreter. 1446 { 1447 ciConstant constant = iter().get_constant(); 1448 if (constant.basic_type() == T_OBJECT && 1449 !constant.as_object()->is_loaded()) { 1450 int index = iter().get_constant_pool_index(); 1451 constantTag tag = iter().get_constant_pool_tag(index); 1452 uncommon_trap(Deoptimization::make_trap_request 1453 (Deoptimization::Reason_unloaded, 1454 Deoptimization::Action_reinterpret, 1455 index), 1456 NULL, tag.internal_name()); 1457 break; 1458 } 1459 assert(constant.basic_type() != T_OBJECT || constant.as_object()->is_instance(), 1460 "must be java_mirror of klass"); 1461 bool pushed = push_constant(constant, true); 1462 guarantee(pushed, "must be possible to push this constant"); 1463 } 1464 1465 break; 1466 1467 case Bytecodes::_aload_0: 1468 push( local(0) ); 1469 break; 1470 case Bytecodes::_aload_1: 1471 push( local(1) ); 1472 break; 1473 case Bytecodes::_aload_2: 1474 push( local(2) ); 1475 break; 1476 case Bytecodes::_aload_3: 1477 push( local(3) ); 1478 break; 1479 case Bytecodes::_aload: 1480 push( local(iter().get_index()) ); 1481 break; 1482 1483 case Bytecodes::_fload_0: 1484 case Bytecodes::_iload_0: 1485 push( local(0) ); 1486 break; 1487 case Bytecodes::_fload_1: 1488 case Bytecodes::_iload_1: 1489 push( local(1) ); 1490 break; 1491 case Bytecodes::_fload_2: 1492 case Bytecodes::_iload_2: 1493 push( local(2) ); 1494 break; 1495 case Bytecodes::_fload_3: 1496 case Bytecodes::_iload_3: 1497 push( local(3) ); 1498 break; 1499 case Bytecodes::_fload: 1500 case Bytecodes::_iload: 1501 push( local(iter().get_index()) ); 1502 break; 1503 case Bytecodes::_lload_0: 1504 push_pair_local( 0 ); 1505 break; 1506 case Bytecodes::_lload_1: 1507 push_pair_local( 1 ); 1508 break; 1509 case Bytecodes::_lload_2: 1510 push_pair_local( 2 ); 1511 break; 1512 case Bytecodes::_lload_3: 1513 push_pair_local( 3 ); 1514 break; 1515 case Bytecodes::_lload: 1516 push_pair_local( iter().get_index() ); 1517 break; 1518 1519 case Bytecodes::_dload_0: 1520 push_pair_local(0); 1521 break; 1522 case Bytecodes::_dload_1: 1523 push_pair_local(1); 1524 break; 1525 case Bytecodes::_dload_2: 1526 push_pair_local(2); 1527 break; 1528 case Bytecodes::_dload_3: 1529 push_pair_local(3); 1530 break; 1531 case Bytecodes::_dload: 1532 push_pair_local(iter().get_index()); 1533 break; 1534 case Bytecodes::_fstore_0: 1535 case Bytecodes::_istore_0: 1536 case Bytecodes::_astore_0: 1537 set_local( 0, pop() ); 1538 break; 1539 case Bytecodes::_fstore_1: 1540 case Bytecodes::_istore_1: 1541 case Bytecodes::_astore_1: 1542 set_local( 1, pop() ); 1543 break; 1544 case Bytecodes::_fstore_2: 1545 case Bytecodes::_istore_2: 1546 case Bytecodes::_astore_2: 1547 set_local( 2, pop() ); 1548 break; 1549 case Bytecodes::_fstore_3: 1550 case Bytecodes::_istore_3: 1551 case Bytecodes::_astore_3: 1552 set_local( 3, pop() ); 1553 break; 1554 case Bytecodes::_fstore: 1555 case Bytecodes::_istore: 1556 case Bytecodes::_astore: 1557 set_local( iter().get_index(), pop() ); 1558 break; 1559 // long stores 1560 case Bytecodes::_lstore_0: 1561 set_pair_local( 0, pop_pair() ); 1562 break; 1563 case Bytecodes::_lstore_1: 1564 set_pair_local( 1, pop_pair() ); 1565 break; 1566 case Bytecodes::_lstore_2: 1567 set_pair_local( 2, pop_pair() ); 1568 break; 1569 case Bytecodes::_lstore_3: 1570 set_pair_local( 3, pop_pair() ); 1571 break; 1572 case Bytecodes::_lstore: 1573 set_pair_local( iter().get_index(), pop_pair() ); 1574 break; 1575 1576 // double stores 1577 case Bytecodes::_dstore_0: 1578 set_pair_local( 0, dstore_rounding(pop_pair()) ); 1579 break; 1580 case Bytecodes::_dstore_1: 1581 set_pair_local( 1, dstore_rounding(pop_pair()) ); 1582 break; 1583 case Bytecodes::_dstore_2: 1584 set_pair_local( 2, dstore_rounding(pop_pair()) ); 1585 break; 1586 case Bytecodes::_dstore_3: 1587 set_pair_local( 3, dstore_rounding(pop_pair()) ); 1588 break; 1589 case Bytecodes::_dstore: 1590 set_pair_local( iter().get_index(), dstore_rounding(pop_pair()) ); 1591 break; 1592 1593 case Bytecodes::_pop: dec_sp(1); break; 1594 case Bytecodes::_pop2: dec_sp(2); break; 1595 case Bytecodes::_swap: 1596 a = pop(); 1597 b = pop(); 1598 push(a); 1599 push(b); 1600 break; 1601 case Bytecodes::_dup: 1602 a = pop(); 1603 push(a); 1604 push(a); 1605 break; 1606 case Bytecodes::_dup_x1: 1607 a = pop(); 1608 b = pop(); 1609 push( a ); 1610 push( b ); 1611 push( a ); 1612 break; 1613 case Bytecodes::_dup_x2: 1614 a = pop(); 1615 b = pop(); 1616 c = pop(); 1617 push( a ); 1618 push( c ); 1619 push( b ); 1620 push( a ); 1621 break; 1622 case Bytecodes::_dup2: 1623 a = pop(); 1624 b = pop(); 1625 push( b ); 1626 push( a ); 1627 push( b ); 1628 push( a ); 1629 break; 1630 1631 case Bytecodes::_dup2_x1: 1632 // before: .. c, b, a 1633 // after: .. b, a, c, b, a 1634 // not tested 1635 a = pop(); 1636 b = pop(); 1637 c = pop(); 1638 push( b ); 1639 push( a ); 1640 push( c ); 1641 push( b ); 1642 push( a ); 1643 break; 1644 case Bytecodes::_dup2_x2: 1645 // before: .. d, c, b, a 1646 // after: .. b, a, d, c, b, a 1647 // not tested 1648 a = pop(); 1649 b = pop(); 1650 c = pop(); 1651 d = pop(); 1652 push( b ); 1653 push( a ); 1654 push( d ); 1655 push( c ); 1656 push( b ); 1657 push( a ); 1658 break; 1659 1660 case Bytecodes::_arraylength: { 1661 // Must do null-check with value on expression stack 1662 Node *ary = null_check(peek(), T_ARRAY); 1663 // Compile-time detect of null-exception? 1664 if (stopped()) return; 1665 a = pop(); 1666 push(load_array_length(a)); 1667 break; 1668 } 1669 1670 case Bytecodes::_baload: array_load(T_BYTE); break; 1671 case Bytecodes::_caload: array_load(T_CHAR); break; 1672 case Bytecodes::_iaload: array_load(T_INT); break; 1673 case Bytecodes::_saload: array_load(T_SHORT); break; 1674 case Bytecodes::_faload: array_load(T_FLOAT); break; 1675 case Bytecodes::_aaload: array_load(T_OBJECT); break; 1676 case Bytecodes::_laload: { 1677 a = array_addressing(T_LONG, 0); 1678 if (stopped()) return; // guaranteed null or range check 1679 dec_sp(2); // Pop array and index 1680 push_pair(make_load(control(), a, TypeLong::LONG, T_LONG, TypeAryPtr::LONGS, MemNode::unordered)); 1681 break; 1682 } 1683 case Bytecodes::_daload: { 1684 a = array_addressing(T_DOUBLE, 0); 1685 if (stopped()) return; // guaranteed null or range check 1686 dec_sp(2); // Pop array and index 1687 push_pair(make_load(control(), a, Type::DOUBLE, T_DOUBLE, TypeAryPtr::DOUBLES, MemNode::unordered)); 1688 break; 1689 } 1690 case Bytecodes::_bastore: array_store(T_BYTE); break; 1691 case Bytecodes::_castore: array_store(T_CHAR); break; 1692 case Bytecodes::_iastore: array_store(T_INT); break; 1693 case Bytecodes::_sastore: array_store(T_SHORT); break; 1694 case Bytecodes::_fastore: array_store(T_FLOAT); break; 1695 case Bytecodes::_aastore: { 1696 d = array_addressing(T_OBJECT, 1); 1697 if (stopped()) return; // guaranteed null or range check 1698 array_store_check(); 1699 c = pop(); // Oop to store 1700 b = pop(); // index (already used) 1701 a = pop(); // the array itself 1702 const TypeOopPtr* elemtype = _gvn.type(a)->is_aryptr()->elem()->make_oopptr(); 1703 const TypeAryPtr* adr_type = TypeAryPtr::OOPS; 1704 Node* store = store_oop_to_array(control(), a, d, adr_type, c, elemtype, T_OBJECT, MemNode::release); 1705 break; 1706 } 1707 case Bytecodes::_lastore: { 1708 a = array_addressing(T_LONG, 2); 1709 if (stopped()) return; // guaranteed null or range check 1710 c = pop_pair(); 1711 dec_sp(2); // Pop array and index 1712 store_to_memory(control(), a, c, T_LONG, TypeAryPtr::LONGS, MemNode::unordered); 1713 break; 1714 } 1715 case Bytecodes::_dastore: { 1716 a = array_addressing(T_DOUBLE, 2); 1717 if (stopped()) return; // guaranteed null or range check 1718 c = pop_pair(); 1719 dec_sp(2); // Pop array and index 1720 c = dstore_rounding(c); 1721 store_to_memory(control(), a, c, T_DOUBLE, TypeAryPtr::DOUBLES, MemNode::unordered); 1722 break; 1723 } 1724 case Bytecodes::_getfield: 1725 do_getfield(); 1726 break; 1727 1728 case Bytecodes::_getstatic: 1729 do_getstatic(); 1730 break; 1731 1732 case Bytecodes::_putfield: 1733 do_putfield(); 1734 break; 1735 1736 case Bytecodes::_putstatic: 1737 do_putstatic(); 1738 break; 1739 1740 case Bytecodes::_irem: 1741 do_irem(); 1742 break; 1743 case Bytecodes::_idiv: 1744 // Must keep both values on the expression-stack during null-check 1745 zero_check_int(peek()); 1746 // Compile-time detect of null-exception? 1747 if (stopped()) return; 1748 b = pop(); 1749 a = pop(); 1750 push( _gvn.transform( new DivINode(control(),a,b) ) ); 1751 break; 1752 case Bytecodes::_imul: 1753 b = pop(); a = pop(); 1754 push( _gvn.transform( new MulINode(a,b) ) ); 1755 break; 1756 case Bytecodes::_iadd: 1757 b = pop(); a = pop(); 1758 push( _gvn.transform( new AddINode(a,b) ) ); 1759 break; 1760 case Bytecodes::_ineg: 1761 a = pop(); 1762 push( _gvn.transform( new SubINode(_gvn.intcon(0),a)) ); 1763 break; 1764 case Bytecodes::_isub: 1765 b = pop(); a = pop(); 1766 push( _gvn.transform( new SubINode(a,b) ) ); 1767 break; 1768 case Bytecodes::_iand: 1769 b = pop(); a = pop(); 1770 push( _gvn.transform( new AndINode(a,b) ) ); 1771 break; 1772 case Bytecodes::_ior: 1773 b = pop(); a = pop(); 1774 push( _gvn.transform( new OrINode(a,b) ) ); 1775 break; 1776 case Bytecodes::_ixor: 1777 b = pop(); a = pop(); 1778 push( _gvn.transform( new XorINode(a,b) ) ); 1779 break; 1780 case Bytecodes::_ishl: 1781 b = pop(); a = pop(); 1782 push( _gvn.transform( new LShiftINode(a,b) ) ); 1783 break; 1784 case Bytecodes::_ishr: 1785 b = pop(); a = pop(); 1786 push( _gvn.transform( new RShiftINode(a,b) ) ); 1787 break; 1788 case Bytecodes::_iushr: 1789 b = pop(); a = pop(); 1790 push( _gvn.transform( new URShiftINode(a,b) ) ); 1791 break; 1792 1793 case Bytecodes::_fneg: 1794 a = pop(); 1795 b = _gvn.transform(new NegFNode (a)); 1796 push(b); 1797 break; 1798 1799 case Bytecodes::_fsub: 1800 b = pop(); 1801 a = pop(); 1802 c = _gvn.transform( new SubFNode(a,b) ); 1803 d = precision_rounding(c); 1804 push( d ); 1805 break; 1806 1807 case Bytecodes::_fadd: 1808 b = pop(); 1809 a = pop(); 1810 c = _gvn.transform( new AddFNode(a,b) ); 1811 d = precision_rounding(c); 1812 push( d ); 1813 break; 1814 1815 case Bytecodes::_fmul: 1816 b = pop(); 1817 a = pop(); 1818 c = _gvn.transform( new MulFNode(a,b) ); 1819 d = precision_rounding(c); 1820 push( d ); 1821 break; 1822 1823 case Bytecodes::_fdiv: 1824 b = pop(); 1825 a = pop(); 1826 c = _gvn.transform( new DivFNode(0,a,b) ); 1827 d = precision_rounding(c); 1828 push( d ); 1829 break; 1830 1831 case Bytecodes::_frem: 1832 if (Matcher::has_match_rule(Op_ModF)) { 1833 // Generate a ModF node. 1834 b = pop(); 1835 a = pop(); 1836 c = _gvn.transform( new ModFNode(0,a,b) ); 1837 d = precision_rounding(c); 1838 push( d ); 1839 } 1840 else { 1841 // Generate a call. 1842 modf(); 1843 } 1844 break; 1845 1846 case Bytecodes::_fcmpl: 1847 b = pop(); 1848 a = pop(); 1849 c = _gvn.transform( new CmpF3Node( a, b)); 1850 push(c); 1851 break; 1852 case Bytecodes::_fcmpg: 1853 b = pop(); 1854 a = pop(); 1855 1856 // Same as fcmpl but need to flip the unordered case. Swap the inputs, 1857 // which negates the result sign except for unordered. Flip the unordered 1858 // as well by using CmpF3 which implements unordered-lesser instead of 1859 // unordered-greater semantics. Finally, commute the result bits. Result 1860 // is same as using a CmpF3Greater except we did it with CmpF3 alone. 1861 c = _gvn.transform( new CmpF3Node( b, a)); 1862 c = _gvn.transform( new SubINode(_gvn.intcon(0),c) ); 1863 push(c); 1864 break; 1865 1866 case Bytecodes::_f2i: 1867 a = pop(); 1868 push(_gvn.transform(new ConvF2INode(a))); 1869 break; 1870 1871 case Bytecodes::_d2i: 1872 a = pop_pair(); 1873 b = _gvn.transform(new ConvD2INode(a)); 1874 push( b ); 1875 break; 1876 1877 case Bytecodes::_f2d: 1878 a = pop(); 1879 b = _gvn.transform( new ConvF2DNode(a)); 1880 push_pair( b ); 1881 break; 1882 1883 case Bytecodes::_d2f: 1884 a = pop_pair(); 1885 b = _gvn.transform( new ConvD2FNode(a)); 1886 // This breaks _227_mtrt (speed & correctness) and _222_mpegaudio (speed) 1887 //b = _gvn.transform(new RoundFloatNode(0, b) ); 1888 push( b ); 1889 break; 1890 1891 case Bytecodes::_l2f: 1892 if (Matcher::convL2FSupported()) { 1893 a = pop_pair(); 1894 b = _gvn.transform( new ConvL2FNode(a)); 1895 // For i486.ad, FILD doesn't restrict precision to 24 or 53 bits. 1896 // Rather than storing the result into an FP register then pushing 1897 // out to memory to round, the machine instruction that implements 1898 // ConvL2D is responsible for rounding. 1899 // c = precision_rounding(b); 1900 c = _gvn.transform(b); 1901 push(c); 1902 } else { 1903 l2f(); 1904 } 1905 break; 1906 1907 case Bytecodes::_l2d: 1908 a = pop_pair(); 1909 b = _gvn.transform( new ConvL2DNode(a)); 1910 // For i486.ad, rounding is always necessary (see _l2f above). 1911 // c = dprecision_rounding(b); 1912 c = _gvn.transform(b); 1913 push_pair(c); 1914 break; 1915 1916 case Bytecodes::_f2l: 1917 a = pop(); 1918 b = _gvn.transform( new ConvF2LNode(a)); 1919 push_pair(b); 1920 break; 1921 1922 case Bytecodes::_d2l: 1923 a = pop_pair(); 1924 b = _gvn.transform( new ConvD2LNode(a)); 1925 push_pair(b); 1926 break; 1927 1928 case Bytecodes::_dsub: 1929 b = pop_pair(); 1930 a = pop_pair(); 1931 c = _gvn.transform( new SubDNode(a,b) ); 1932 d = dprecision_rounding(c); 1933 push_pair( d ); 1934 break; 1935 1936 case Bytecodes::_dadd: 1937 b = pop_pair(); 1938 a = pop_pair(); 1939 c = _gvn.transform( new AddDNode(a,b) ); 1940 d = dprecision_rounding(c); 1941 push_pair( d ); 1942 break; 1943 1944 case Bytecodes::_dmul: 1945 b = pop_pair(); 1946 a = pop_pair(); 1947 c = _gvn.transform( new MulDNode(a,b) ); 1948 d = dprecision_rounding(c); 1949 push_pair( d ); 1950 break; 1951 1952 case Bytecodes::_ddiv: 1953 b = pop_pair(); 1954 a = pop_pair(); 1955 c = _gvn.transform( new DivDNode(0,a,b) ); 1956 d = dprecision_rounding(c); 1957 push_pair( d ); 1958 break; 1959 1960 case Bytecodes::_dneg: 1961 a = pop_pair(); 1962 b = _gvn.transform(new NegDNode (a)); 1963 push_pair(b); 1964 break; 1965 1966 case Bytecodes::_drem: 1967 if (Matcher::has_match_rule(Op_ModD)) { 1968 // Generate a ModD node. 1969 b = pop_pair(); 1970 a = pop_pair(); 1971 // a % b 1972 1973 c = _gvn.transform( new ModDNode(0,a,b) ); 1974 d = dprecision_rounding(c); 1975 push_pair( d ); 1976 } 1977 else { 1978 // Generate a call. 1979 modd(); 1980 } 1981 break; 1982 1983 case Bytecodes::_dcmpl: 1984 b = pop_pair(); 1985 a = pop_pair(); 1986 c = _gvn.transform( new CmpD3Node( a, b)); 1987 push(c); 1988 break; 1989 1990 case Bytecodes::_dcmpg: 1991 b = pop_pair(); 1992 a = pop_pair(); 1993 // Same as dcmpl but need to flip the unordered case. 1994 // Commute the inputs, which negates the result sign except for unordered. 1995 // Flip the unordered as well by using CmpD3 which implements 1996 // unordered-lesser instead of unordered-greater semantics. 1997 // Finally, negate the result bits. Result is same as using a 1998 // CmpD3Greater except we did it with CmpD3 alone. 1999 c = _gvn.transform( new CmpD3Node( b, a)); 2000 c = _gvn.transform( new SubINode(_gvn.intcon(0),c) ); 2001 push(c); 2002 break; 2003 2004 2005 // Note for longs -> lo word is on TOS, hi word is on TOS - 1 2006 case Bytecodes::_land: 2007 b = pop_pair(); 2008 a = pop_pair(); 2009 c = _gvn.transform( new AndLNode(a,b) ); 2010 push_pair(c); 2011 break; 2012 case Bytecodes::_lor: 2013 b = pop_pair(); 2014 a = pop_pair(); 2015 c = _gvn.transform( new OrLNode(a,b) ); 2016 push_pair(c); 2017 break; 2018 case Bytecodes::_lxor: 2019 b = pop_pair(); 2020 a = pop_pair(); 2021 c = _gvn.transform( new XorLNode(a,b) ); 2022 push_pair(c); 2023 break; 2024 2025 case Bytecodes::_lshl: 2026 b = pop(); // the shift count 2027 a = pop_pair(); // value to be shifted 2028 c = _gvn.transform( new LShiftLNode(a,b) ); 2029 push_pair(c); 2030 break; 2031 case Bytecodes::_lshr: 2032 b = pop(); // the shift count 2033 a = pop_pair(); // value to be shifted 2034 c = _gvn.transform( new RShiftLNode(a,b) ); 2035 push_pair(c); 2036 break; 2037 case Bytecodes::_lushr: 2038 b = pop(); // the shift count 2039 a = pop_pair(); // value to be shifted 2040 c = _gvn.transform( new URShiftLNode(a,b) ); 2041 push_pair(c); 2042 break; 2043 case Bytecodes::_lmul: 2044 b = pop_pair(); 2045 a = pop_pair(); 2046 c = _gvn.transform( new MulLNode(a,b) ); 2047 push_pair(c); 2048 break; 2049 2050 case Bytecodes::_lrem: 2051 // Must keep both values on the expression-stack during null-check 2052 assert(peek(0) == top(), "long word order"); 2053 zero_check_long(peek(1)); 2054 // Compile-time detect of null-exception? 2055 if (stopped()) return; 2056 b = pop_pair(); 2057 a = pop_pair(); 2058 c = _gvn.transform( new ModLNode(control(),a,b) ); 2059 push_pair(c); 2060 break; 2061 2062 case Bytecodes::_ldiv: 2063 // Must keep both values on the expression-stack during null-check 2064 assert(peek(0) == top(), "long word order"); 2065 zero_check_long(peek(1)); 2066 // Compile-time detect of null-exception? 2067 if (stopped()) return; 2068 b = pop_pair(); 2069 a = pop_pair(); 2070 c = _gvn.transform( new DivLNode(control(),a,b) ); 2071 push_pair(c); 2072 break; 2073 2074 case Bytecodes::_ladd: 2075 b = pop_pair(); 2076 a = pop_pair(); 2077 c = _gvn.transform( new AddLNode(a,b) ); 2078 push_pair(c); 2079 break; 2080 case Bytecodes::_lsub: 2081 b = pop_pair(); 2082 a = pop_pair(); 2083 c = _gvn.transform( new SubLNode(a,b) ); 2084 push_pair(c); 2085 break; 2086 case Bytecodes::_lcmp: 2087 // Safepoints are now inserted _before_ branches. The long-compare 2088 // bytecode painfully produces a 3-way value (-1,0,+1) which requires a 2089 // slew of control flow. These are usually followed by a CmpI vs zero and 2090 // a branch; this pattern then optimizes to the obvious long-compare and 2091 // branch. However, if the branch is backwards there's a Safepoint 2092 // inserted. The inserted Safepoint captures the JVM state at the 2093 // pre-branch point, i.e. it captures the 3-way value. Thus if a 2094 // long-compare is used to control a loop the debug info will force 2095 // computation of the 3-way value, even though the generated code uses a 2096 // long-compare and branch. We try to rectify the situation by inserting 2097 // a SafePoint here and have it dominate and kill the safepoint added at a 2098 // following backwards branch. At this point the JVM state merely holds 2 2099 // longs but not the 3-way value. 2100 if( UseLoopSafepoints ) { 2101 switch( iter().next_bc() ) { 2102 case Bytecodes::_ifgt: 2103 case Bytecodes::_iflt: 2104 case Bytecodes::_ifge: 2105 case Bytecodes::_ifle: 2106 case Bytecodes::_ifne: 2107 case Bytecodes::_ifeq: 2108 // If this is a backwards branch in the bytecodes, add Safepoint 2109 maybe_add_safepoint(iter().next_get_dest()); 2110 } 2111 } 2112 b = pop_pair(); 2113 a = pop_pair(); 2114 c = _gvn.transform( new CmpL3Node( a, b )); 2115 push(c); 2116 break; 2117 2118 case Bytecodes::_lneg: 2119 a = pop_pair(); 2120 b = _gvn.transform( new SubLNode(longcon(0),a)); 2121 push_pair(b); 2122 break; 2123 case Bytecodes::_l2i: 2124 a = pop_pair(); 2125 push( _gvn.transform( new ConvL2INode(a))); 2126 break; 2127 case Bytecodes::_i2l: 2128 a = pop(); 2129 b = _gvn.transform( new ConvI2LNode(a)); 2130 push_pair(b); 2131 break; 2132 case Bytecodes::_i2b: 2133 // Sign extend 2134 a = pop(); 2135 a = _gvn.transform( new LShiftINode(a,_gvn.intcon(24)) ); 2136 a = _gvn.transform( new RShiftINode(a,_gvn.intcon(24)) ); 2137 push( a ); 2138 break; 2139 case Bytecodes::_i2s: 2140 a = pop(); 2141 a = _gvn.transform( new LShiftINode(a,_gvn.intcon(16)) ); 2142 a = _gvn.transform( new RShiftINode(a,_gvn.intcon(16)) ); 2143 push( a ); 2144 break; 2145 case Bytecodes::_i2c: 2146 a = pop(); 2147 push( _gvn.transform( new AndINode(a,_gvn.intcon(0xFFFF)) ) ); 2148 break; 2149 2150 case Bytecodes::_i2f: 2151 a = pop(); 2152 b = _gvn.transform( new ConvI2FNode(a) ) ; 2153 c = precision_rounding(b); 2154 push (b); 2155 break; 2156 2157 case Bytecodes::_i2d: 2158 a = pop(); 2159 b = _gvn.transform( new ConvI2DNode(a)); 2160 push_pair(b); 2161 break; 2162 2163 case Bytecodes::_iinc: // Increment local 2164 i = iter().get_index(); // Get local index 2165 set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) ); 2166 break; 2167 2168 // Exit points of synchronized methods must have an unlock node 2169 case Bytecodes::_return: 2170 return_current(NULL); 2171 break; 2172 2173 case Bytecodes::_ireturn: 2174 case Bytecodes::_areturn: 2175 case Bytecodes::_freturn: 2176 return_current(pop()); 2177 break; 2178 case Bytecodes::_lreturn: 2179 return_current(pop_pair()); 2180 break; 2181 case Bytecodes::_dreturn: 2182 return_current(pop_pair()); 2183 break; 2184 2185 case Bytecodes::_athrow: 2186 // null exception oop throws NULL pointer exception 2187 null_check(peek()); 2188 if (stopped()) return; 2189 // Hook the thrown exception directly to subsequent handlers. 2190 if (BailoutToInterpreterForThrows) { 2191 // Keep method interpreted from now on. 2192 uncommon_trap(Deoptimization::Reason_unhandled, 2193 Deoptimization::Action_make_not_compilable); 2194 return; 2195 } 2196 if (env()->jvmti_can_post_on_exceptions()) { 2197 // check if we must post exception events, take uncommon trap if so (with must_throw = false) 2198 uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false); 2199 } 2200 // Here if either can_post_on_exceptions or should_post_on_exceptions is false 2201 add_exception_state(make_exception_state(peek())); 2202 break; 2203 2204 case Bytecodes::_goto: // fall through 2205 case Bytecodes::_goto_w: { 2206 int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest(); 2207 2208 // If this is a backwards branch in the bytecodes, add Safepoint 2209 maybe_add_safepoint(target_bci); 2210 2211 // Update method data 2212 profile_taken_branch(target_bci); 2213 2214 // Merge the current control into the target basic block 2215 merge(target_bci); 2216 2217 // See if we can get some profile data and hand it off to the next block 2218 Block *target_block = block()->successor_for_bci(target_bci); 2219 if (target_block->pred_count() != 1) break; 2220 ciMethodData* methodData = method()->method_data(); 2221 if (!methodData->is_mature()) break; 2222 ciProfileData* data = methodData->bci_to_data(bci()); 2223 assert( data->is_JumpData(), "" ); 2224 int taken = ((ciJumpData*)data)->taken(); 2225 taken = method()->scale_count(taken); 2226 target_block->set_count(taken); 2227 break; 2228 } 2229 2230 case Bytecodes::_ifnull: btest = BoolTest::eq; goto handle_if_null; 2231 case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null; 2232 handle_if_null: 2233 // If this is a backwards branch in the bytecodes, add Safepoint 2234 maybe_add_safepoint(iter().get_dest()); 2235 a = null(); 2236 b = pop(); 2237 if (!_gvn.type(b)->speculative_maybe_null() && 2238 !too_many_traps(Deoptimization::Reason_speculate_null_check)) { 2239 inc_sp(1); 2240 Node* null_ctl = top(); 2241 b = null_check_oop(b, &null_ctl, true, true, true); 2242 assert(null_ctl->is_top(), "no null control here"); 2243 dec_sp(1); 2244 } 2245 c = _gvn.transform( new CmpPNode(b, a) ); 2246 do_ifnull(btest, c); 2247 break; 2248 2249 case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp; 2250 case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp; 2251 handle_if_acmp: 2252 // If this is a backwards branch in the bytecodes, add Safepoint 2253 maybe_add_safepoint(iter().get_dest()); 2254 a = pop(); 2255 b = pop(); 2256 c = _gvn.transform( new CmpPNode(b, a) ); 2257 c = optimize_cmp_with_klass(c); 2258 do_if(btest, c); 2259 break; 2260 2261 case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx; 2262 case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx; 2263 case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx; 2264 case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx; 2265 case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx; 2266 case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx; 2267 handle_ifxx: 2268 // If this is a backwards branch in the bytecodes, add Safepoint 2269 maybe_add_safepoint(iter().get_dest()); 2270 a = _gvn.intcon(0); 2271 b = pop(); 2272 c = _gvn.transform( new CmpINode(b, a) ); 2273 do_if(btest, c); 2274 break; 2275 2276 case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp; 2277 case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp; 2278 case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp; 2279 case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp; 2280 case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp; 2281 case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp; 2282 handle_if_icmp: 2283 // If this is a backwards branch in the bytecodes, add Safepoint 2284 maybe_add_safepoint(iter().get_dest()); 2285 a = pop(); 2286 b = pop(); 2287 c = _gvn.transform( new CmpINode( b, a ) ); 2288 do_if(btest, c); 2289 break; 2290 2291 case Bytecodes::_tableswitch: 2292 do_tableswitch(); 2293 break; 2294 2295 case Bytecodes::_lookupswitch: 2296 do_lookupswitch(); 2297 break; 2298 2299 case Bytecodes::_invokestatic: 2300 case Bytecodes::_invokedynamic: 2301 case Bytecodes::_invokespecial: 2302 case Bytecodes::_invokevirtual: 2303 case Bytecodes::_invokeinterface: 2304 do_call(); 2305 break; 2306 case Bytecodes::_checkcast: 2307 do_checkcast(); 2308 break; 2309 case Bytecodes::_instanceof: 2310 do_instanceof(); 2311 break; 2312 case Bytecodes::_anewarray: 2313 do_anewarray(); 2314 break; 2315 case Bytecodes::_newarray: 2316 do_newarray((BasicType)iter().get_index()); 2317 break; 2318 case Bytecodes::_multianewarray: 2319 do_multianewarray(); 2320 break; 2321 case Bytecodes::_new: 2322 do_new(); 2323 break; 2324 2325 case Bytecodes::_jsr: 2326 case Bytecodes::_jsr_w: 2327 do_jsr(); 2328 break; 2329 2330 case Bytecodes::_ret: 2331 do_ret(); 2332 break; 2333 2334 2335 case Bytecodes::_monitorenter: 2336 do_monitor_enter(); 2337 break; 2338 2339 case Bytecodes::_monitorexit: 2340 do_monitor_exit(); 2341 break; 2342 2343 case Bytecodes::_breakpoint: 2344 // Breakpoint set concurrently to compile 2345 // %%% use an uncommon trap? 2346 C->record_failure("breakpoint in method"); 2347 return; 2348 2349 default: 2350 #ifndef PRODUCT 2351 map()->dump(99); 2352 #endif 2353 tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) ); 2354 ShouldNotReachHere(); 2355 } 2356 2357 #ifndef PRODUCT 2358 IdealGraphPrinter *printer = IdealGraphPrinter::printer(); 2359 if (printer && printer->should_print(_method)) { 2360 char buffer[256]; 2361 sprintf(buffer, "Bytecode %d: %s", bci(), Bytecodes::name(bc())); 2362 bool old = printer->traverse_outs(); 2363 printer->set_traverse_outs(true); 2364 printer->print_method(C, buffer, 4); 2365 printer->set_traverse_outs(old); 2366 } 2367 #endif 2368 }