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