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