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