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