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