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