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 #ifndef PRODUCT
 952   if (PrintOpto && WizardMode) {
 953     tty->print("defending against excessive implicit null exceptions on %s @%d in ",
 954                Bytecodes::name(iter().cur_bc()), iter().cur_bci());
 955     method()->print_name(); tty->cr();
 956   }
 957 #endif
 958   int bc_depth = - Bytecodes::depth(iter().cur_bc());
 959   assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches");
 960   DEBUG_ONLY(sync_jvms());   // argument(n) requires a synced jvms
 961   assert(argument(0) != NULL, "must exist");
 962   assert(bc_depth == 1 || argument(1) != NULL, "two must exist");
 963   inc_sp(bc_depth);
 964   return bc_depth;
 965 }
 966 
 967 //----------------------------------do_ifnull----------------------------------
 968 void Parse::do_ifnull(BoolTest::mask btest, Node *c) {
 969   int target_bci = iter().get_dest();
 970 
 971   Block* branch_block = successor_for_bci(target_bci);
 972   Block* next_block   = successor_for_bci(iter().next_bci());
 973 
 974   float cnt;
 975   float prob = branch_prediction(cnt, btest, target_bci, c);
 976   if (prob == PROB_UNKNOWN) {
 977     // (An earlier version of do_ifnull omitted this trap for OSR methods.)
 978 #ifndef PRODUCT
 979     if (PrintOpto && Verbose)
 980       tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
 981 #endif
 982     repush_if_args(); // to gather stats on loop
 983     // We need to mark this branch as taken so that if we recompile we will
 984     // see that it is possible. In the tiered system the interpreter doesn't
 985     // do profiling and by the time we get to the lower tier from the interpreter
 986     // the path may be cold again. Make sure it doesn't look untaken
 987     profile_taken_branch(target_bci, !ProfileInterpreter);
 988     uncommon_trap(Deoptimization::Reason_unreached,
 989                   Deoptimization::Action_reinterpret,
 990                   NULL, "cold");
 991     if (C->eliminate_boxing()) {
 992       // Mark the successor blocks as parsed
 993       branch_block->next_path_num();
 994       next_block->next_path_num();
 995     }
 996     return;
 997   }
 998 
 999   explicit_null_checks_inserted++;
1000 
1001   // Generate real control flow
1002   Node   *tst = _gvn.transform( new BoolNode( c, btest ) );
1003 
1004   // Sanity check the probability value
1005   assert(prob > 0.0f,"Bad probability in Parser");
1006  // Need xform to put node in hash table
1007   IfNode *iff = create_and_xform_if( control(), tst, prob, cnt );
1008   assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1009   // True branch
1010   { PreserveJVMState pjvms(this);
1011     Node* iftrue  = _gvn.transform( new IfTrueNode (iff) );
1012     set_control(iftrue);
1013 
1014     if (stopped()) {            // Path is dead?
1015       explicit_null_checks_elided++;
1016       if (C->eliminate_boxing()) {
1017         // Mark the successor block as parsed
1018         branch_block->next_path_num();
1019       }
1020     } else {                    // Path is live.
1021       // Update method data
1022       profile_taken_branch(target_bci);
1023       adjust_map_after_if(btest, c, prob, branch_block, next_block);
1024       if (!stopped()) {
1025         merge(target_bci);
1026       }
1027     }
1028   }
1029 
1030   // False branch
1031   Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1032   set_control(iffalse);
1033 
1034   if (stopped()) {              // Path is dead?
1035     explicit_null_checks_elided++;
1036     if (C->eliminate_boxing()) {
1037       // Mark the successor block as parsed
1038       next_block->next_path_num();
1039     }
1040   } else  {                     // Path is live.
1041     // Update method data
1042     profile_not_taken_branch();
1043     adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob,
1044                         next_block, branch_block);
1045   }
1046 }
1047 
1048 //------------------------------------do_if------------------------------------
1049 void Parse::do_if(BoolTest::mask btest, Node* c) {
1050   int target_bci = iter().get_dest();
1051 
1052   Block* branch_block = successor_for_bci(target_bci);
1053   Block* next_block   = successor_for_bci(iter().next_bci());
1054 
1055   float cnt;
1056   float prob = branch_prediction(cnt, btest, target_bci, c);
1057   float untaken_prob = 1.0 - prob;
1058 
1059   if (prob == PROB_UNKNOWN) {
1060 #ifndef PRODUCT
1061     if (PrintOpto && Verbose)
1062       tty->print_cr("Never-taken edge stops compilation at bci %d",bci());
1063 #endif
1064     repush_if_args(); // to gather stats on loop
1065     // We need to mark this branch as taken so that if we recompile we will
1066     // see that it is possible. In the tiered system the interpreter doesn't
1067     // do profiling and by the time we get to the lower tier from the interpreter
1068     // the path may be cold again. Make sure it doesn't look untaken
1069     profile_taken_branch(target_bci, !ProfileInterpreter);
1070     uncommon_trap(Deoptimization::Reason_unreached,
1071                   Deoptimization::Action_reinterpret,
1072                   NULL, "cold");
1073     if (C->eliminate_boxing()) {
1074       // Mark the successor blocks as parsed
1075       branch_block->next_path_num();
1076       next_block->next_path_num();
1077     }
1078     return;
1079   }
1080 
1081   // Sanity check the probability value
1082   assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1083 
1084   bool taken_if_true = true;
1085   // Convert BoolTest to canonical form:
1086   if (!BoolTest(btest).is_canonical()) {
1087     btest         = BoolTest(btest).negate();
1088     taken_if_true = false;
1089     // prob is NOT updated here; it remains the probability of the taken
1090     // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1091   }
1092   assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1093 
1094   Node* tst0 = new BoolNode(c, btest);
1095   Node* tst = _gvn.transform(tst0);
1096   BoolTest::mask taken_btest   = BoolTest::illegal;
1097   BoolTest::mask untaken_btest = BoolTest::illegal;
1098 
1099   if (tst->is_Bool()) {
1100     // Refresh c from the transformed bool node, since it may be
1101     // simpler than the original c.  Also re-canonicalize btest.
1102     // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p NULL)).
1103     // That can arise from statements like: if (x instanceof C) ...
1104     if (tst != tst0) {
1105       // Canonicalize one more time since transform can change it.
1106       btest = tst->as_Bool()->_test._test;
1107       if (!BoolTest(btest).is_canonical()) {
1108         // Reverse edges one more time...
1109         tst   = _gvn.transform( tst->as_Bool()->negate(&_gvn) );
1110         btest = tst->as_Bool()->_test._test;
1111         assert(BoolTest(btest).is_canonical(), "sanity");
1112         taken_if_true = !taken_if_true;
1113       }
1114       c = tst->in(1);
1115     }
1116     BoolTest::mask neg_btest = BoolTest(btest).negate();
1117     taken_btest   = taken_if_true ?     btest : neg_btest;
1118     untaken_btest = taken_if_true ? neg_btest :     btest;
1119   }
1120 
1121   // Generate real control flow
1122   float true_prob = (taken_if_true ? prob : untaken_prob);
1123   IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1124   assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1125   Node* taken_branch   = new IfTrueNode(iff);
1126   Node* untaken_branch = new IfFalseNode(iff);
1127   if (!taken_if_true) {  // Finish conversion to canonical form
1128     Node* tmp      = taken_branch;
1129     taken_branch   = untaken_branch;
1130     untaken_branch = tmp;
1131   }
1132 
1133   // Branch is taken:
1134   { PreserveJVMState pjvms(this);
1135     taken_branch = _gvn.transform(taken_branch);
1136     set_control(taken_branch);
1137 
1138     if (stopped()) {
1139       if (C->eliminate_boxing()) {
1140         // Mark the successor block as parsed
1141         branch_block->next_path_num();
1142       }
1143     } else {
1144       // Update method data
1145       profile_taken_branch(target_bci);
1146       adjust_map_after_if(taken_btest, c, prob, branch_block, next_block);
1147       if (!stopped()) {
1148         merge(target_bci);
1149       }
1150     }
1151   }
1152 
1153   untaken_branch = _gvn.transform(untaken_branch);
1154   set_control(untaken_branch);
1155 
1156   // Branch not taken.
1157   if (stopped()) {
1158     if (C->eliminate_boxing()) {
1159       // Mark the successor block as parsed
1160       next_block->next_path_num();
1161     }
1162   } else {
1163     // Update method data
1164     profile_not_taken_branch();
1165     adjust_map_after_if(untaken_btest, c, untaken_prob,
1166                         next_block, branch_block);
1167   }
1168 }
1169 
1170 bool Parse::path_is_suitable_for_uncommon_trap(float prob) const {
1171   // Don't want to speculate on uncommon traps when running with -Xcomp
1172   if (!UseInterpreter) {
1173     return false;
1174   }
1175   return (seems_never_taken(prob) && seems_stable_comparison());
1176 }
1177 
1178 //----------------------------adjust_map_after_if------------------------------
1179 // Adjust the JVM state to reflect the result of taking this path.
1180 // Basically, it means inspecting the CmpNode controlling this
1181 // branch, seeing how it constrains a tested value, and then
1182 // deciding if it's worth our while to encode this constraint
1183 // as graph nodes in the current abstract interpretation map.
1184 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob,
1185                                 Block* path, Block* other_path) {
1186   if (stopped() || !c->is_Cmp() || btest == BoolTest::illegal)
1187     return;                             // nothing to do
1188 
1189   bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
1190 
1191   if (path_is_suitable_for_uncommon_trap(prob)) {
1192     repush_if_args();
1193     uncommon_trap(Deoptimization::Reason_unstable_if,
1194                   Deoptimization::Action_reinterpret,
1195                   NULL,
1196                   (is_fallthrough ? "taken always" : "taken never"));
1197     return;
1198   }
1199 
1200   Node* val = c->in(1);
1201   Node* con = c->in(2);
1202   const Type* tcon = _gvn.type(con);
1203   const Type* tval = _gvn.type(val);
1204   bool have_con = tcon->singleton();
1205   if (tval->singleton()) {
1206     if (!have_con) {
1207       // Swap, so constant is in con.
1208       con  = val;
1209       tcon = tval;
1210       val  = c->in(2);
1211       tval = _gvn.type(val);
1212       btest = BoolTest(btest).commute();
1213       have_con = true;
1214     } else {
1215       // Do we have two constants?  Then leave well enough alone.
1216       have_con = false;
1217     }
1218   }
1219   if (!have_con)                        // remaining adjustments need a con
1220     return;
1221 
1222   sharpen_type_after_if(btest, con, tcon, val, tval);
1223 }
1224 
1225 
1226 static Node* extract_obj_from_klass_load(PhaseGVN* gvn, Node* n) {
1227   Node* ldk;
1228   if (n->is_DecodeNKlass()) {
1229     if (n->in(1)->Opcode() != Op_LoadNKlass) {
1230       return NULL;
1231     } else {
1232       ldk = n->in(1);
1233     }
1234   } else if (n->Opcode() != Op_LoadKlass) {
1235     return NULL;
1236   } else {
1237     ldk = n;
1238   }
1239   assert(ldk != NULL && ldk->is_Load(), "should have found a LoadKlass or LoadNKlass node");
1240 
1241   Node* adr = ldk->in(MemNode::Address);
1242   intptr_t off = 0;
1243   Node* obj = AddPNode::Ideal_base_and_offset(adr, gvn, off);
1244   if (obj == NULL || off != oopDesc::klass_offset_in_bytes()) // loading oopDesc::_klass?
1245     return NULL;
1246   const TypePtr* tp = gvn->type(obj)->is_ptr();
1247   if (tp == NULL || !(tp->isa_instptr() || tp->isa_aryptr())) // is obj a Java object ptr?
1248     return NULL;
1249 
1250   return obj;
1251 }
1252 
1253 void Parse::sharpen_type_after_if(BoolTest::mask btest,
1254                                   Node* con, const Type* tcon,
1255                                   Node* val, const Type* tval) {
1256   // Look for opportunities to sharpen the type of a node
1257   // whose klass is compared with a constant klass.
1258   if (btest == BoolTest::eq && tcon->isa_klassptr()) {
1259     Node* obj = extract_obj_from_klass_load(&_gvn, val);
1260     const TypeOopPtr* con_type = tcon->isa_klassptr()->as_instance_type();
1261     if (obj != NULL && (con_type->isa_instptr() || con_type->isa_aryptr())) {
1262        // Found:
1263        //   Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
1264        // or the narrowOop equivalent.
1265        const Type* obj_type = _gvn.type(obj);
1266        const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
1267        if (tboth != NULL && tboth->klass_is_exact() && tboth != obj_type &&
1268            tboth->higher_equal(obj_type)) {
1269           // obj has to be of the exact type Foo if the CmpP succeeds.
1270           int obj_in_map = map()->find_edge(obj);
1271           JVMState* jvms = this->jvms();
1272           if (obj_in_map >= 0 &&
1273               (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
1274             TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
1275             const Type* tcc = ccast->as_Type()->type();
1276             assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
1277             // Delay transform() call to allow recovery of pre-cast value
1278             // at the control merge.
1279             _gvn.set_type_bottom(ccast);
1280             record_for_igvn(ccast);
1281             // Here's the payoff.
1282             replace_in_map(obj, ccast);
1283           }
1284        }
1285     }
1286   }
1287 
1288   int val_in_map = map()->find_edge(val);
1289   if (val_in_map < 0)  return;          // replace_in_map would be useless
1290   {
1291     JVMState* jvms = this->jvms();
1292     if (!(jvms->is_loc(val_in_map) ||
1293           jvms->is_stk(val_in_map)))
1294       return;                           // again, it would be useless
1295   }
1296 
1297   // Check for a comparison to a constant, and "know" that the compared
1298   // value is constrained on this path.
1299   assert(tcon->singleton(), "");
1300   ConstraintCastNode* ccast = NULL;
1301   Node* cast = NULL;
1302 
1303   switch (btest) {
1304   case BoolTest::eq:                    // Constant test?
1305     {
1306       const Type* tboth = tcon->join_speculative(tval);
1307       if (tboth == tval)  break;        // Nothing to gain.
1308       if (tcon->isa_int()) {
1309         ccast = new CastIINode(val, tboth);
1310       } else if (tcon == TypePtr::NULL_PTR) {
1311         // Cast to null, but keep the pointer identity temporarily live.
1312         ccast = new CastPPNode(val, tboth);
1313       } else {
1314         const TypeF* tf = tcon->isa_float_constant();
1315         const TypeD* td = tcon->isa_double_constant();
1316         // Exclude tests vs float/double 0 as these could be
1317         // either +0 or -0.  Just because you are equal to +0
1318         // doesn't mean you ARE +0!
1319         // Note, following code also replaces Long and Oop values.
1320         if ((!tf || tf->_f != 0.0) &&
1321             (!td || td->_d != 0.0))
1322           cast = con;                   // Replace non-constant val by con.
1323       }
1324     }
1325     break;
1326 
1327   case BoolTest::ne:
1328     if (tcon == TypePtr::NULL_PTR) {
1329       cast = cast_not_null(val, false);
1330     }
1331     break;
1332 
1333   default:
1334     // (At this point we could record int range types with CastII.)
1335     break;
1336   }
1337 
1338   if (ccast != NULL) {
1339     const Type* tcc = ccast->as_Type()->type();
1340     assert(tcc != tval && tcc->higher_equal(tval), "must improve");
1341     // Delay transform() call to allow recovery of pre-cast value
1342     // at the control merge.
1343     ccast->set_req(0, control());
1344     _gvn.set_type_bottom(ccast);
1345     record_for_igvn(ccast);
1346     cast = ccast;
1347   }
1348 
1349   if (cast != NULL) {                   // Here's the payoff.
1350     replace_in_map(val, cast);
1351   }
1352 }
1353 
1354 /**
1355  * Use speculative type to optimize CmpP node: if comparison is
1356  * against the low level class, cast the object to the speculative
1357  * type if any. CmpP should then go away.
1358  *
1359  * @param c  expected CmpP node
1360  * @return   result of CmpP on object casted to speculative type
1361  *
1362  */
1363 Node* Parse::optimize_cmp_with_klass(Node* c) {
1364   // If this is transformed by the _gvn to a comparison with the low
1365   // level klass then we may be able to use speculation
1366   if (c->Opcode() == Op_CmpP &&
1367       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
1368       c->in(2)->is_Con()) {
1369     Node* load_klass = NULL;
1370     Node* decode = NULL;
1371     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
1372       decode = c->in(1);
1373       load_klass = c->in(1)->in(1);
1374     } else {
1375       load_klass = c->in(1);
1376     }
1377     if (load_klass->in(2)->is_AddP()) {
1378       Node* addp = load_klass->in(2);
1379       Node* obj = addp->in(AddPNode::Address);
1380       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
1381       if (obj_type->speculative_type_not_null() != NULL) {
1382         ciKlass* k = obj_type->speculative_type();
1383         inc_sp(2);
1384         obj = maybe_cast_profiled_obj(obj, k);
1385         dec_sp(2);
1386         // Make the CmpP use the casted obj
1387         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
1388         load_klass = load_klass->clone();
1389         load_klass->set_req(2, addp);
1390         load_klass = _gvn.transform(load_klass);
1391         if (decode != NULL) {
1392           decode = decode->clone();
1393           decode->set_req(1, load_klass);
1394           load_klass = _gvn.transform(decode);
1395         }
1396         c = c->clone();
1397         c->set_req(1, load_klass);
1398         c = _gvn.transform(c);
1399       }
1400     }
1401   }
1402   return c;
1403 }
1404 
1405 //------------------------------do_one_bytecode--------------------------------
1406 // Parse this bytecode, and alter the Parsers JVM->Node mapping
1407 void Parse::do_one_bytecode() {
1408   Node *a, *b, *c, *d;          // Handy temps
1409   BoolTest::mask btest;
1410   int i;
1411 
1412   assert(!has_exceptions(), "bytecode entry state must be clear of throws");
1413 
1414   if (C->check_node_count(NodeLimitFudgeFactor * 5,
1415                           "out of nodes parsing method")) {
1416     return;
1417   }
1418 
1419 #ifdef ASSERT
1420   // for setting breakpoints
1421   if (TraceOptoParse) {
1422     tty->print(" @");
1423     dump_bci(bci());
1424     tty->cr();
1425   }
1426 #endif
1427 
1428   switch (bc()) {
1429   case Bytecodes::_nop:
1430     // do nothing
1431     break;
1432   case Bytecodes::_lconst_0:
1433     push_pair(longcon(0));
1434     break;
1435 
1436   case Bytecodes::_lconst_1:
1437     push_pair(longcon(1));
1438     break;
1439 
1440   case Bytecodes::_fconst_0:
1441     push(zerocon(T_FLOAT));
1442     break;
1443 
1444   case Bytecodes::_fconst_1:
1445     push(makecon(TypeF::ONE));
1446     break;
1447 
1448   case Bytecodes::_fconst_2:
1449     push(makecon(TypeF::make(2.0f)));
1450     break;
1451 
1452   case Bytecodes::_dconst_0:
1453     push_pair(zerocon(T_DOUBLE));
1454     break;
1455 
1456   case Bytecodes::_dconst_1:
1457     push_pair(makecon(TypeD::ONE));
1458     break;
1459 
1460   case Bytecodes::_iconst_m1:push(intcon(-1)); break;
1461   case Bytecodes::_iconst_0: push(intcon( 0)); break;
1462   case Bytecodes::_iconst_1: push(intcon( 1)); break;
1463   case Bytecodes::_iconst_2: push(intcon( 2)); break;
1464   case Bytecodes::_iconst_3: push(intcon( 3)); break;
1465   case Bytecodes::_iconst_4: push(intcon( 4)); break;
1466   case Bytecodes::_iconst_5: push(intcon( 5)); break;
1467   case Bytecodes::_bipush:   push(intcon(iter().get_constant_u1())); break;
1468   case Bytecodes::_sipush:   push(intcon(iter().get_constant_u2())); break;
1469   case Bytecodes::_aconst_null: push(null());  break;
1470   case Bytecodes::_ldc:
1471   case Bytecodes::_ldc_w:
1472   case Bytecodes::_ldc2_w:
1473     // If the constant is unresolved, run this BC once in the interpreter.
1474     {
1475       ciConstant constant = iter().get_constant();
1476       if (constant.basic_type() == T_OBJECT &&
1477           !constant.as_object()->is_loaded()) {
1478         int index = iter().get_constant_pool_index();
1479         constantTag tag = iter().get_constant_pool_tag(index);
1480         uncommon_trap(Deoptimization::make_trap_request
1481                       (Deoptimization::Reason_unloaded,
1482                        Deoptimization::Action_reinterpret,
1483                        index),
1484                       NULL, tag.internal_name());
1485         break;
1486       }
1487       assert(constant.basic_type() != T_OBJECT || constant.as_object()->is_instance(),
1488              "must be java_mirror of klass");
1489       const Type* con_type = Type::make_from_constant(constant);
1490       if (con_type != NULL) {
1491         push_node(con_type->basic_type(), makecon(con_type));
1492       }
1493     }
1494 
1495     break;
1496 
1497   case Bytecodes::_aload_0:
1498     push( local(0) );
1499     break;
1500   case Bytecodes::_aload_1:
1501     push( local(1) );
1502     break;
1503   case Bytecodes::_aload_2:
1504     push( local(2) );
1505     break;
1506   case Bytecodes::_aload_3:
1507     push( local(3) );
1508     break;
1509   case Bytecodes::_aload:
1510     push( local(iter().get_index()) );
1511     break;
1512 
1513   case Bytecodes::_fload_0:
1514   case Bytecodes::_iload_0:
1515     push( local(0) );
1516     break;
1517   case Bytecodes::_fload_1:
1518   case Bytecodes::_iload_1:
1519     push( local(1) );
1520     break;
1521   case Bytecodes::_fload_2:
1522   case Bytecodes::_iload_2:
1523     push( local(2) );
1524     break;
1525   case Bytecodes::_fload_3:
1526   case Bytecodes::_iload_3:
1527     push( local(3) );
1528     break;
1529   case Bytecodes::_fload:
1530   case Bytecodes::_iload:
1531     push( local(iter().get_index()) );
1532     break;
1533   case Bytecodes::_lload_0:
1534     push_pair_local( 0 );
1535     break;
1536   case Bytecodes::_lload_1:
1537     push_pair_local( 1 );
1538     break;
1539   case Bytecodes::_lload_2:
1540     push_pair_local( 2 );
1541     break;
1542   case Bytecodes::_lload_3:
1543     push_pair_local( 3 );
1544     break;
1545   case Bytecodes::_lload:
1546     push_pair_local( iter().get_index() );
1547     break;
1548 
1549   case Bytecodes::_dload_0:
1550     push_pair_local(0);
1551     break;
1552   case Bytecodes::_dload_1:
1553     push_pair_local(1);
1554     break;
1555   case Bytecodes::_dload_2:
1556     push_pair_local(2);
1557     break;
1558   case Bytecodes::_dload_3:
1559     push_pair_local(3);
1560     break;
1561   case Bytecodes::_dload:
1562     push_pair_local(iter().get_index());
1563     break;
1564   case Bytecodes::_fstore_0:
1565   case Bytecodes::_istore_0:
1566   case Bytecodes::_astore_0:
1567     set_local( 0, pop() );
1568     break;
1569   case Bytecodes::_fstore_1:
1570   case Bytecodes::_istore_1:
1571   case Bytecodes::_astore_1:
1572     set_local( 1, pop() );
1573     break;
1574   case Bytecodes::_fstore_2:
1575   case Bytecodes::_istore_2:
1576   case Bytecodes::_astore_2:
1577     set_local( 2, pop() );
1578     break;
1579   case Bytecodes::_fstore_3:
1580   case Bytecodes::_istore_3:
1581   case Bytecodes::_astore_3:
1582     set_local( 3, pop() );
1583     break;
1584   case Bytecodes::_fstore:
1585   case Bytecodes::_istore:
1586   case Bytecodes::_astore:
1587     set_local( iter().get_index(), pop() );
1588     break;
1589   // long stores
1590   case Bytecodes::_lstore_0:
1591     set_pair_local( 0, pop_pair() );
1592     break;
1593   case Bytecodes::_lstore_1:
1594     set_pair_local( 1, pop_pair() );
1595     break;
1596   case Bytecodes::_lstore_2:
1597     set_pair_local( 2, pop_pair() );
1598     break;
1599   case Bytecodes::_lstore_3:
1600     set_pair_local( 3, pop_pair() );
1601     break;
1602   case Bytecodes::_lstore:
1603     set_pair_local( iter().get_index(), pop_pair() );
1604     break;
1605 
1606   // double stores
1607   case Bytecodes::_dstore_0:
1608     set_pair_local( 0, dstore_rounding(pop_pair()) );
1609     break;
1610   case Bytecodes::_dstore_1:
1611     set_pair_local( 1, dstore_rounding(pop_pair()) );
1612     break;
1613   case Bytecodes::_dstore_2:
1614     set_pair_local( 2, dstore_rounding(pop_pair()) );
1615     break;
1616   case Bytecodes::_dstore_3:
1617     set_pair_local( 3, dstore_rounding(pop_pair()) );
1618     break;
1619   case Bytecodes::_dstore:
1620     set_pair_local( iter().get_index(), dstore_rounding(pop_pair()) );
1621     break;
1622 
1623   case Bytecodes::_pop:  dec_sp(1);   break;
1624   case Bytecodes::_pop2: dec_sp(2);   break;
1625   case Bytecodes::_swap:
1626     a = pop();
1627     b = pop();
1628     push(a);
1629     push(b);
1630     break;
1631   case Bytecodes::_dup:
1632     a = pop();
1633     push(a);
1634     push(a);
1635     break;
1636   case Bytecodes::_dup_x1:
1637     a = pop();
1638     b = pop();
1639     push( a );
1640     push( b );
1641     push( a );
1642     break;
1643   case Bytecodes::_dup_x2:
1644     a = pop();
1645     b = pop();
1646     c = pop();
1647     push( a );
1648     push( c );
1649     push( b );
1650     push( a );
1651     break;
1652   case Bytecodes::_dup2:
1653     a = pop();
1654     b = pop();
1655     push( b );
1656     push( a );
1657     push( b );
1658     push( a );
1659     break;
1660 
1661   case Bytecodes::_dup2_x1:
1662     // before: .. c, b, a
1663     // after:  .. b, a, c, b, a
1664     // not tested
1665     a = pop();
1666     b = pop();
1667     c = pop();
1668     push( b );
1669     push( a );
1670     push( c );
1671     push( b );
1672     push( a );
1673     break;
1674   case Bytecodes::_dup2_x2:
1675     // before: .. d, c, b, a
1676     // after:  .. b, a, d, c, b, a
1677     // not tested
1678     a = pop();
1679     b = pop();
1680     c = pop();
1681     d = pop();
1682     push( b );
1683     push( a );
1684     push( d );
1685     push( c );
1686     push( b );
1687     push( a );
1688     break;
1689 
1690   case Bytecodes::_arraylength: {
1691     // Must do null-check with value on expression stack
1692     Node *ary = null_check(peek(), T_ARRAY);
1693     // Compile-time detect of null-exception?
1694     if (stopped())  return;
1695     a = pop();
1696     push(load_array_length(a));
1697     break;
1698   }
1699 
1700   case Bytecodes::_baload: array_load(T_BYTE);   break;
1701   case Bytecodes::_caload: array_load(T_CHAR);   break;
1702   case Bytecodes::_iaload: array_load(T_INT);    break;
1703   case Bytecodes::_saload: array_load(T_SHORT);  break;
1704   case Bytecodes::_faload: array_load(T_FLOAT);  break;
1705   case Bytecodes::_aaload: array_load(T_OBJECT); break;
1706   case Bytecodes::_laload: {
1707     a = array_addressing(T_LONG, 0);
1708     if (stopped())  return;     // guaranteed null or range check
1709     dec_sp(2);                  // Pop array and index
1710     push_pair(make_load(control(), a, TypeLong::LONG, T_LONG, TypeAryPtr::LONGS, MemNode::unordered));
1711     break;
1712   }
1713   case Bytecodes::_daload: {
1714     a = array_addressing(T_DOUBLE, 0);
1715     if (stopped())  return;     // guaranteed null or range check
1716     dec_sp(2);                  // Pop array and index
1717     push_pair(make_load(control(), a, Type::DOUBLE, T_DOUBLE, TypeAryPtr::DOUBLES, MemNode::unordered));
1718     break;
1719   }
1720   case Bytecodes::_bastore: array_store(T_BYTE);  break;
1721   case Bytecodes::_castore: array_store(T_CHAR);  break;
1722   case Bytecodes::_iastore: array_store(T_INT);   break;
1723   case Bytecodes::_sastore: array_store(T_SHORT); break;
1724   case Bytecodes::_fastore: array_store(T_FLOAT); break;
1725   case Bytecodes::_aastore: {
1726     d = array_addressing(T_OBJECT, 1);
1727     if (stopped())  return;     // guaranteed null or range check
1728     array_store_check();
1729     c = pop();                  // Oop to store
1730     b = pop();                  // index (already used)
1731     a = pop();                  // the array itself
1732     const TypeOopPtr* elemtype  = _gvn.type(a)->is_aryptr()->elem()->make_oopptr();
1733     const TypeAryPtr* adr_type = TypeAryPtr::OOPS;
1734     Node* store = store_oop_to_array(control(), a, d, adr_type, c, elemtype, T_OBJECT,
1735                                      StoreNode::release_if_reference(T_OBJECT));
1736     break;
1737   }
1738   case Bytecodes::_lastore: {
1739     a = array_addressing(T_LONG, 2);
1740     if (stopped())  return;     // guaranteed null or range check
1741     c = pop_pair();
1742     dec_sp(2);                  // Pop array and index
1743     store_to_memory(control(), a, c, T_LONG, TypeAryPtr::LONGS, MemNode::unordered);
1744     break;
1745   }
1746   case Bytecodes::_dastore: {
1747     a = array_addressing(T_DOUBLE, 2);
1748     if (stopped())  return;     // guaranteed null or range check
1749     c = pop_pair();
1750     dec_sp(2);                  // Pop array and index
1751     c = dstore_rounding(c);
1752     store_to_memory(control(), a, c, T_DOUBLE, TypeAryPtr::DOUBLES, MemNode::unordered);
1753     break;
1754   }
1755   case Bytecodes::_getfield:
1756     do_getfield();
1757     break;
1758 
1759   case Bytecodes::_getstatic:
1760     do_getstatic();
1761     break;
1762 
1763   case Bytecodes::_putfield:
1764     do_putfield();
1765     break;
1766 
1767   case Bytecodes::_putstatic:
1768     do_putstatic();
1769     break;
1770 
1771   case Bytecodes::_irem:
1772     do_irem();
1773     break;
1774   case Bytecodes::_idiv:
1775     // Must keep both values on the expression-stack during null-check
1776     zero_check_int(peek());
1777     // Compile-time detect of null-exception?
1778     if (stopped())  return;
1779     b = pop();
1780     a = pop();
1781     push( _gvn.transform( new DivINode(control(),a,b) ) );
1782     break;
1783   case Bytecodes::_imul:
1784     b = pop(); a = pop();
1785     push( _gvn.transform( new MulINode(a,b) ) );
1786     break;
1787   case Bytecodes::_iadd:
1788     b = pop(); a = pop();
1789     push( _gvn.transform( new AddINode(a,b) ) );
1790     break;
1791   case Bytecodes::_ineg:
1792     a = pop();
1793     push( _gvn.transform( new SubINode(_gvn.intcon(0),a)) );
1794     break;
1795   case Bytecodes::_isub:
1796     b = pop(); a = pop();
1797     push( _gvn.transform( new SubINode(a,b) ) );
1798     break;
1799   case Bytecodes::_iand:
1800     b = pop(); a = pop();
1801     push( _gvn.transform( new AndINode(a,b) ) );
1802     break;
1803   case Bytecodes::_ior:
1804     b = pop(); a = pop();
1805     push( _gvn.transform( new OrINode(a,b) ) );
1806     break;
1807   case Bytecodes::_ixor:
1808     b = pop(); a = pop();
1809     push( _gvn.transform( new XorINode(a,b) ) );
1810     break;
1811   case Bytecodes::_ishl:
1812     b = pop(); a = pop();
1813     push( _gvn.transform( new LShiftINode(a,b) ) );
1814     break;
1815   case Bytecodes::_ishr:
1816     b = pop(); a = pop();
1817     push( _gvn.transform( new RShiftINode(a,b) ) );
1818     break;
1819   case Bytecodes::_iushr:
1820     b = pop(); a = pop();
1821     push( _gvn.transform( new URShiftINode(a,b) ) );
1822     break;
1823 
1824   case Bytecodes::_fneg:
1825     a = pop();
1826     b = _gvn.transform(new NegFNode (a));
1827     push(b);
1828     break;
1829 
1830   case Bytecodes::_fsub:
1831     b = pop();
1832     a = pop();
1833     c = _gvn.transform( new SubFNode(a,b) );
1834     d = precision_rounding(c);
1835     push( d );
1836     break;
1837 
1838   case Bytecodes::_fadd:
1839     b = pop();
1840     a = pop();
1841     c = _gvn.transform( new AddFNode(a,b) );
1842     d = precision_rounding(c);
1843     push( d );
1844     break;
1845 
1846   case Bytecodes::_fmul:
1847     b = pop();
1848     a = pop();
1849     c = _gvn.transform( new MulFNode(a,b) );
1850     d = precision_rounding(c);
1851     push( d );
1852     break;
1853 
1854   case Bytecodes::_fdiv:
1855     b = pop();
1856     a = pop();
1857     c = _gvn.transform( new DivFNode(0,a,b) );
1858     d = precision_rounding(c);
1859     push( d );
1860     break;
1861 
1862   case Bytecodes::_frem:
1863     if (Matcher::has_match_rule(Op_ModF)) {
1864       // Generate a ModF node.
1865       b = pop();
1866       a = pop();
1867       c = _gvn.transform( new ModFNode(0,a,b) );
1868       d = precision_rounding(c);
1869       push( d );
1870     }
1871     else {
1872       // Generate a call.
1873       modf();
1874     }
1875     break;
1876 
1877   case Bytecodes::_fcmpl:
1878     b = pop();
1879     a = pop();
1880     c = _gvn.transform( new CmpF3Node( a, b));
1881     push(c);
1882     break;
1883   case Bytecodes::_fcmpg:
1884     b = pop();
1885     a = pop();
1886 
1887     // Same as fcmpl but need to flip the unordered case.  Swap the inputs,
1888     // which negates the result sign except for unordered.  Flip the unordered
1889     // as well by using CmpF3 which implements unordered-lesser instead of
1890     // unordered-greater semantics.  Finally, commute the result bits.  Result
1891     // is same as using a CmpF3Greater except we did it with CmpF3 alone.
1892     c = _gvn.transform( new CmpF3Node( b, a));
1893     c = _gvn.transform( new SubINode(_gvn.intcon(0),c) );
1894     push(c);
1895     break;
1896 
1897   case Bytecodes::_f2i:
1898     a = pop();
1899     push(_gvn.transform(new ConvF2INode(a)));
1900     break;
1901 
1902   case Bytecodes::_d2i:
1903     a = pop_pair();
1904     b = _gvn.transform(new ConvD2INode(a));
1905     push( b );
1906     break;
1907 
1908   case Bytecodes::_f2d:
1909     a = pop();
1910     b = _gvn.transform( new ConvF2DNode(a));
1911     push_pair( b );
1912     break;
1913 
1914   case Bytecodes::_d2f:
1915     a = pop_pair();
1916     b = _gvn.transform( new ConvD2FNode(a));
1917     // This breaks _227_mtrt (speed & correctness) and _222_mpegaudio (speed)
1918     //b = _gvn.transform(new RoundFloatNode(0, b) );
1919     push( b );
1920     break;
1921 
1922   case Bytecodes::_l2f:
1923     if (Matcher::convL2FSupported()) {
1924       a = pop_pair();
1925       b = _gvn.transform( new ConvL2FNode(a));
1926       // For i486.ad, FILD doesn't restrict precision to 24 or 53 bits.
1927       // Rather than storing the result into an FP register then pushing
1928       // out to memory to round, the machine instruction that implements
1929       // ConvL2D is responsible for rounding.
1930       // c = precision_rounding(b);
1931       c = _gvn.transform(b);
1932       push(c);
1933     } else {
1934       l2f();
1935     }
1936     break;
1937 
1938   case Bytecodes::_l2d:
1939     a = pop_pair();
1940     b = _gvn.transform( new ConvL2DNode(a));
1941     // For i486.ad, rounding is always necessary (see _l2f above).
1942     // c = dprecision_rounding(b);
1943     c = _gvn.transform(b);
1944     push_pair(c);
1945     break;
1946 
1947   case Bytecodes::_f2l:
1948     a = pop();
1949     b = _gvn.transform( new ConvF2LNode(a));
1950     push_pair(b);
1951     break;
1952 
1953   case Bytecodes::_d2l:
1954     a = pop_pair();
1955     b = _gvn.transform( new ConvD2LNode(a));
1956     push_pair(b);
1957     break;
1958 
1959   case Bytecodes::_dsub:
1960     b = pop_pair();
1961     a = pop_pair();
1962     c = _gvn.transform( new SubDNode(a,b) );
1963     d = dprecision_rounding(c);
1964     push_pair( d );
1965     break;
1966 
1967   case Bytecodes::_dadd:
1968     b = pop_pair();
1969     a = pop_pair();
1970     c = _gvn.transform( new AddDNode(a,b) );
1971     d = dprecision_rounding(c);
1972     push_pair( d );
1973     break;
1974 
1975   case Bytecodes::_dmul:
1976     b = pop_pair();
1977     a = pop_pair();
1978     c = _gvn.transform( new MulDNode(a,b) );
1979     d = dprecision_rounding(c);
1980     push_pair( d );
1981     break;
1982 
1983   case Bytecodes::_ddiv:
1984     b = pop_pair();
1985     a = pop_pair();
1986     c = _gvn.transform( new DivDNode(0,a,b) );
1987     d = dprecision_rounding(c);
1988     push_pair( d );
1989     break;
1990 
1991   case Bytecodes::_dneg:
1992     a = pop_pair();
1993     b = _gvn.transform(new NegDNode (a));
1994     push_pair(b);
1995     break;
1996 
1997   case Bytecodes::_drem:
1998     if (Matcher::has_match_rule(Op_ModD)) {
1999       // Generate a ModD node.
2000       b = pop_pair();
2001       a = pop_pair();
2002       // a % b
2003 
2004       c = _gvn.transform( new ModDNode(0,a,b) );
2005       d = dprecision_rounding(c);
2006       push_pair( d );
2007     }
2008     else {
2009       // Generate a call.
2010       modd();
2011     }
2012     break;
2013 
2014   case Bytecodes::_dcmpl:
2015     b = pop_pair();
2016     a = pop_pair();
2017     c = _gvn.transform( new CmpD3Node( a, b));
2018     push(c);
2019     break;
2020 
2021   case Bytecodes::_dcmpg:
2022     b = pop_pair();
2023     a = pop_pair();
2024     // Same as dcmpl but need to flip the unordered case.
2025     // Commute the inputs, which negates the result sign except for unordered.
2026     // Flip the unordered as well by using CmpD3 which implements
2027     // unordered-lesser instead of unordered-greater semantics.
2028     // Finally, negate the result bits.  Result is same as using a
2029     // CmpD3Greater except we did it with CmpD3 alone.
2030     c = _gvn.transform( new CmpD3Node( b, a));
2031     c = _gvn.transform( new SubINode(_gvn.intcon(0),c) );
2032     push(c);
2033     break;
2034 
2035 
2036     // Note for longs -> lo word is on TOS, hi word is on TOS - 1
2037   case Bytecodes::_land:
2038     b = pop_pair();
2039     a = pop_pair();
2040     c = _gvn.transform( new AndLNode(a,b) );
2041     push_pair(c);
2042     break;
2043   case Bytecodes::_lor:
2044     b = pop_pair();
2045     a = pop_pair();
2046     c = _gvn.transform( new OrLNode(a,b) );
2047     push_pair(c);
2048     break;
2049   case Bytecodes::_lxor:
2050     b = pop_pair();
2051     a = pop_pair();
2052     c = _gvn.transform( new XorLNode(a,b) );
2053     push_pair(c);
2054     break;
2055 
2056   case Bytecodes::_lshl:
2057     b = pop();                  // the shift count
2058     a = pop_pair();             // value to be shifted
2059     c = _gvn.transform( new LShiftLNode(a,b) );
2060     push_pair(c);
2061     break;
2062   case Bytecodes::_lshr:
2063     b = pop();                  // the shift count
2064     a = pop_pair();             // value to be shifted
2065     c = _gvn.transform( new RShiftLNode(a,b) );
2066     push_pair(c);
2067     break;
2068   case Bytecodes::_lushr:
2069     b = pop();                  // the shift count
2070     a = pop_pair();             // value to be shifted
2071     c = _gvn.transform( new URShiftLNode(a,b) );
2072     push_pair(c);
2073     break;
2074   case Bytecodes::_lmul:
2075     b = pop_pair();
2076     a = pop_pair();
2077     c = _gvn.transform( new MulLNode(a,b) );
2078     push_pair(c);
2079     break;
2080 
2081   case Bytecodes::_lrem:
2082     // Must keep both values on the expression-stack during null-check
2083     assert(peek(0) == top(), "long word order");
2084     zero_check_long(peek(1));
2085     // Compile-time detect of null-exception?
2086     if (stopped())  return;
2087     b = pop_pair();
2088     a = pop_pair();
2089     c = _gvn.transform( new ModLNode(control(),a,b) );
2090     push_pair(c);
2091     break;
2092 
2093   case Bytecodes::_ldiv:
2094     // Must keep both values on the expression-stack during null-check
2095     assert(peek(0) == top(), "long word order");
2096     zero_check_long(peek(1));
2097     // Compile-time detect of null-exception?
2098     if (stopped())  return;
2099     b = pop_pair();
2100     a = pop_pair();
2101     c = _gvn.transform( new DivLNode(control(),a,b) );
2102     push_pair(c);
2103     break;
2104 
2105   case Bytecodes::_ladd:
2106     b = pop_pair();
2107     a = pop_pair();
2108     c = _gvn.transform( new AddLNode(a,b) );
2109     push_pair(c);
2110     break;
2111   case Bytecodes::_lsub:
2112     b = pop_pair();
2113     a = pop_pair();
2114     c = _gvn.transform( new SubLNode(a,b) );
2115     push_pair(c);
2116     break;
2117   case Bytecodes::_lcmp:
2118     // Safepoints are now inserted _before_ branches.  The long-compare
2119     // bytecode painfully produces a 3-way value (-1,0,+1) which requires a
2120     // slew of control flow.  These are usually followed by a CmpI vs zero and
2121     // a branch; this pattern then optimizes to the obvious long-compare and
2122     // branch.  However, if the branch is backwards there's a Safepoint
2123     // inserted.  The inserted Safepoint captures the JVM state at the
2124     // pre-branch point, i.e. it captures the 3-way value.  Thus if a
2125     // long-compare is used to control a loop the debug info will force
2126     // computation of the 3-way value, even though the generated code uses a
2127     // long-compare and branch.  We try to rectify the situation by inserting
2128     // a SafePoint here and have it dominate and kill the safepoint added at a
2129     // following backwards branch.  At this point the JVM state merely holds 2
2130     // longs but not the 3-way value.
2131     if( UseLoopSafepoints ) {
2132       switch( iter().next_bc() ) {
2133       case Bytecodes::_ifgt:
2134       case Bytecodes::_iflt:
2135       case Bytecodes::_ifge:
2136       case Bytecodes::_ifle:
2137       case Bytecodes::_ifne:
2138       case Bytecodes::_ifeq:
2139         // If this is a backwards branch in the bytecodes, add Safepoint
2140         maybe_add_safepoint(iter().next_get_dest());
2141       }
2142     }
2143     b = pop_pair();
2144     a = pop_pair();
2145     c = _gvn.transform( new CmpL3Node( a, b ));
2146     push(c);
2147     break;
2148 
2149   case Bytecodes::_lneg:
2150     a = pop_pair();
2151     b = _gvn.transform( new SubLNode(longcon(0),a));
2152     push_pair(b);
2153     break;
2154   case Bytecodes::_l2i:
2155     a = pop_pair();
2156     push( _gvn.transform( new ConvL2INode(a)));
2157     break;
2158   case Bytecodes::_i2l:
2159     a = pop();
2160     b = _gvn.transform( new ConvI2LNode(a));
2161     push_pair(b);
2162     break;
2163   case Bytecodes::_i2b:
2164     // Sign extend
2165     a = pop();
2166     a = _gvn.transform( new LShiftINode(a,_gvn.intcon(24)) );
2167     a = _gvn.transform( new RShiftINode(a,_gvn.intcon(24)) );
2168     push( a );
2169     break;
2170   case Bytecodes::_i2s:
2171     a = pop();
2172     a = _gvn.transform( new LShiftINode(a,_gvn.intcon(16)) );
2173     a = _gvn.transform( new RShiftINode(a,_gvn.intcon(16)) );
2174     push( a );
2175     break;
2176   case Bytecodes::_i2c:
2177     a = pop();
2178     push( _gvn.transform( new AndINode(a,_gvn.intcon(0xFFFF)) ) );
2179     break;
2180 
2181   case Bytecodes::_i2f:
2182     a = pop();
2183     b = _gvn.transform( new ConvI2FNode(a) ) ;
2184     c = precision_rounding(b);
2185     push (b);
2186     break;
2187 
2188   case Bytecodes::_i2d:
2189     a = pop();
2190     b = _gvn.transform( new ConvI2DNode(a));
2191     push_pair(b);
2192     break;
2193 
2194   case Bytecodes::_iinc:        // Increment local
2195     i = iter().get_index();     // Get local index
2196     set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
2197     break;
2198 
2199   // Exit points of synchronized methods must have an unlock node
2200   case Bytecodes::_return:
2201     return_current(NULL);
2202     break;
2203 
2204   case Bytecodes::_ireturn:
2205   case Bytecodes::_areturn:
2206   case Bytecodes::_freturn:
2207     return_current(pop());
2208     break;
2209   case Bytecodes::_lreturn:
2210     return_current(pop_pair());
2211     break;
2212   case Bytecodes::_dreturn:
2213     return_current(pop_pair());
2214     break;
2215 
2216   case Bytecodes::_athrow:
2217     // null exception oop throws NULL pointer exception
2218     null_check(peek());
2219     if (stopped())  return;
2220     // Hook the thrown exception directly to subsequent handlers.
2221     if (BailoutToInterpreterForThrows) {
2222       // Keep method interpreted from now on.
2223       uncommon_trap(Deoptimization::Reason_unhandled,
2224                     Deoptimization::Action_make_not_compilable);
2225       return;
2226     }
2227     if (env()->jvmti_can_post_on_exceptions()) {
2228       // check if we must post exception events, take uncommon trap if so (with must_throw = false)
2229       uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
2230     }
2231     // Here if either can_post_on_exceptions or should_post_on_exceptions is false
2232     add_exception_state(make_exception_state(peek()));
2233     break;
2234 
2235   case Bytecodes::_goto:   // fall through
2236   case Bytecodes::_goto_w: {
2237     int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest();
2238 
2239     // If this is a backwards branch in the bytecodes, add Safepoint
2240     maybe_add_safepoint(target_bci);
2241 
2242     // Update method data
2243     profile_taken_branch(target_bci);
2244 
2245     // Merge the current control into the target basic block
2246     merge(target_bci);
2247 
2248     // See if we can get some profile data and hand it off to the next block
2249     Block *target_block = block()->successor_for_bci(target_bci);
2250     if (target_block->pred_count() != 1)  break;
2251     ciMethodData* methodData = method()->method_data();
2252     if (!methodData->is_mature())  break;
2253     ciProfileData* data = methodData->bci_to_data(bci());
2254     assert( data->is_JumpData(), "" );
2255     int taken = ((ciJumpData*)data)->taken();
2256     taken = method()->scale_count(taken);
2257     target_block->set_count(taken);
2258     break;
2259   }
2260 
2261   case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
2262   case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
2263   handle_if_null:
2264     // If this is a backwards branch in the bytecodes, add Safepoint
2265     maybe_add_safepoint(iter().get_dest());
2266     a = null();
2267     b = pop();
2268     if (!_gvn.type(b)->speculative_maybe_null() &&
2269         !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2270       inc_sp(1);
2271       Node* null_ctl = top();
2272       b = null_check_oop(b, &null_ctl, true, true, true);
2273       assert(null_ctl->is_top(), "no null control here");
2274       dec_sp(1);
2275     }
2276     c = _gvn.transform( new CmpPNode(b, a) );
2277     do_ifnull(btest, c);
2278     break;
2279 
2280   case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
2281   case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
2282   handle_if_acmp:
2283     // If this is a backwards branch in the bytecodes, add Safepoint
2284     maybe_add_safepoint(iter().get_dest());
2285     a = pop();
2286     b = pop();
2287     c = _gvn.transform( new CmpPNode(b, a) );
2288     c = optimize_cmp_with_klass(c);
2289     do_if(btest, c);
2290     break;
2291 
2292   case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
2293   case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
2294   case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
2295   case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
2296   case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
2297   case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
2298   handle_ifxx:
2299     // If this is a backwards branch in the bytecodes, add Safepoint
2300     maybe_add_safepoint(iter().get_dest());
2301     a = _gvn.intcon(0);
2302     b = pop();
2303     c = _gvn.transform( new CmpINode(b, a) );
2304     do_if(btest, c);
2305     break;
2306 
2307   case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
2308   case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
2309   case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
2310   case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp;
2311   case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp;
2312   case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp;
2313   handle_if_icmp:
2314     // If this is a backwards branch in the bytecodes, add Safepoint
2315     maybe_add_safepoint(iter().get_dest());
2316     a = pop();
2317     b = pop();
2318     c = _gvn.transform( new CmpINode( b, a ) );
2319     do_if(btest, c);
2320     break;
2321 
2322   case Bytecodes::_tableswitch:
2323     do_tableswitch();
2324     break;
2325 
2326   case Bytecodes::_lookupswitch:
2327     do_lookupswitch();
2328     break;
2329 
2330   case Bytecodes::_invokestatic:
2331   case Bytecodes::_invokedynamic:
2332   case Bytecodes::_invokespecial:
2333   case Bytecodes::_invokevirtual:
2334   case Bytecodes::_invokeinterface:
2335     do_call();
2336     break;
2337   case Bytecodes::_checkcast:
2338     do_checkcast();
2339     break;
2340   case Bytecodes::_instanceof:
2341     do_instanceof();
2342     break;
2343   case Bytecodes::_anewarray:
2344     do_anewarray();
2345     break;
2346   case Bytecodes::_newarray:
2347     do_newarray((BasicType)iter().get_index());
2348     break;
2349   case Bytecodes::_multianewarray:
2350     do_multianewarray();
2351     break;
2352   case Bytecodes::_new:
2353     do_new();
2354     break;
2355 
2356   case Bytecodes::_jsr:
2357   case Bytecodes::_jsr_w:
2358     do_jsr();
2359     break;
2360 
2361   case Bytecodes::_ret:
2362     do_ret();
2363     break;
2364 
2365 
2366   case Bytecodes::_monitorenter:
2367     do_monitor_enter();
2368     break;
2369 
2370   case Bytecodes::_monitorexit:
2371     do_monitor_exit();
2372     break;
2373 
2374   case Bytecodes::_breakpoint:
2375     // Breakpoint set concurrently to compile
2376     // %%% use an uncommon trap?
2377     C->record_failure("breakpoint in method");
2378     return;
2379 
2380   default:
2381 #ifndef PRODUCT
2382     map()->dump(99);
2383 #endif
2384     tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) );
2385     ShouldNotReachHere();
2386   }
2387 
2388 #ifndef PRODUCT
2389   IdealGraphPrinter *printer = IdealGraphPrinter::printer();
2390   if (printer && printer->should_print(_method)) {
2391     char buffer[256];
2392     sprintf(buffer, "Bytecode %d: %s", bci(), Bytecodes::name(bc()));
2393     bool old = printer->traverse_outs();
2394     printer->set_traverse_outs(true);
2395     printer->print_method(C, buffer, 4);
2396     printer->set_traverse_outs(old);
2397   }
2398 #endif
2399 }