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