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