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