1 /*
   2  * Copyright (c) 1998, 2019, 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/idealKit.hpp"
  40 #include "opto/matcher.hpp"
  41 #include "opto/memnode.hpp"
  42 #include "opto/mulnode.hpp"
  43 #include "opto/opaquenode.hpp"
  44 #include "opto/parse.hpp"
  45 #include "opto/runtime.hpp"
  46 #include "opto/valuetypenode.hpp"
  47 #include "runtime/deoptimization.hpp"
  48 #include "runtime/sharedRuntime.hpp"
  49 
  50 #ifndef PRODUCT
  51 extern int explicit_null_checks_inserted,
  52            explicit_null_checks_elided;
  53 #endif
  54 
  55 //---------------------------------array_load----------------------------------
  56 void Parse::array_load(BasicType bt) {
  57   const Type* elemtype = Type::TOP;
  58   Node* adr = array_addressing(bt, 0, &elemtype);
  59   if (stopped())  return;     // guaranteed null or range check
  60 
  61   Node* idx = pop();
  62   Node* ary = pop();
  63 
  64   // Handle value type arrays
  65   const TypeOopPtr* elemptr = elemtype->make_oopptr();
  66   const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr();
  67   if (elemtype->isa_valuetype() != NULL) {
  68     C->set_flattened_accesses();
  69     // Load from flattened value type array
  70     Node* vt = ValueTypeNode::make_from_flattened(this, elemtype->value_klass(), ary, adr);
  71     push(vt);
  72     return;
  73   } else if (elemptr != NULL && elemptr->is_valuetypeptr() && !elemptr->maybe_null()) {
  74     // Load from non-flattened but flattenable value type array (elements can never be null)
  75     bt = T_VALUETYPE;
  76   } else if (!ary_t->is_not_flat() && !ary_t->klass_is_exact()) {
  77     // Cannot statically determine if array is flattened, emit runtime check
  78     assert(ValueArrayFlatten && elemptr != NULL && elemptr->can_be_value_type() &&
  79            (!elemptr->is_valuetypeptr() || elemptr->value_klass()->flatten_array()), "array can't be flattened");
  80     Node* ctl = control();
  81     IdealKit ideal(this);
  82     IdealVariable res(ideal);
  83     ideal.declarations_done();
  84     Node* kls = load_object_klass(ary);
  85     Node* tag = load_lh_array_tag(kls);
  86     ideal.if_then(tag, BoolTest::ne, intcon(Klass::_lh_array_tag_vt_value)); {
  87       // non-flattened
  88       sync_kit(ideal);
  89       const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
  90       Node* ld = access_load_at(ary, adr, adr_type, elemptr, bt,
  91                                 IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD, ctl);
  92       ideal.sync_kit(this);
  93       ideal.set(res, ld);
  94     } ideal.else_(); {
  95       // flattened
  96       sync_kit(ideal);
  97       if (elemptr->is_valuetypeptr()) {
  98         // Element type is known, cast and load from flattened representation
  99         assert(elemptr->maybe_null(), "must be nullable");
 100         ciValueKlass* vk = elemptr->value_klass();
 101         assert(vk->flatten_array(), "must be flattenable");
 102         ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* never_null */ true);
 103         const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
 104         Node* cast = _gvn.transform(new CheckCastPPNode(control(), ary, arytype));
 105         adr = array_element_address(cast, idx, T_VALUETYPE, ary_t->size(), control());
 106         Node* vt = ValueTypeNode::make_from_flattened(this, vk, cast, adr)->allocate(this, false, false)->get_oop();
 107         ideal.set(res, vt);
 108         ideal.sync_kit(this);
 109       } else {
 110         // Element type is unknown, emit runtime call
 111         Node* k_adr = basic_plus_adr(kls, in_bytes(ArrayKlass::element_klass_offset()));
 112         Node* elem_klass = _gvn.transform(LoadKlassNode::make(_gvn, NULL, immutable_memory(), k_adr, TypeInstPtr::KLASS));
 113         Node* obj_size  = NULL;
 114         kill_dead_locals();
 115         inc_sp(2);
 116         Node* alloc_obj = new_instance(elem_klass, NULL, &obj_size, /*deoptimize_on_exception=*/true);
 117         dec_sp(2);
 118 
 119         AllocateNode* alloc = AllocateNode::Ideal_allocation(alloc_obj, &_gvn);
 120         assert(alloc->maybe_set_complete(&_gvn), "");
 121         alloc->initialization()->set_complete_with_arraycopy();
 122 
 123         // This membar keeps this access to an unknown flattened array
 124         // correctly ordered with other unknown and known flattened
 125         // array accesses.
 126         insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
 127 
 128         BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
 129         // Unknown value type might contain reference fields
 130         if (!bs->array_copy_requires_gc_barriers(false, T_OBJECT, false, BarrierSetC2::Parsing)) {
 131           int base_off = sizeof(instanceOopDesc);
 132           Node* dst_base = basic_plus_adr(alloc_obj, base_off);
 133           Node* countx = obj_size;
 134           countx = _gvn.transform(new SubXNode(countx, MakeConX(base_off)));
 135           countx = _gvn.transform(new URShiftXNode(countx, intcon(LogBytesPerLong)));
 136 
 137           assert(Klass::_lh_log2_element_size_shift == 0, "use shift in place");
 138           Node* lhp = basic_plus_adr(kls, in_bytes(Klass::layout_helper_offset()));
 139           Node* elem_shift = make_load(NULL, lhp, TypeInt::INT, T_INT, MemNode::unordered);
 140           uint header = arrayOopDesc::base_offset_in_bytes(T_VALUETYPE);
 141           Node* base  = basic_plus_adr(ary, header);
 142           idx = Compile::conv_I2X_index(&_gvn, idx, TypeInt::POS, control());
 143           Node* scale = _gvn.transform(new LShiftXNode(idx, elem_shift));
 144           Node* adr = basic_plus_adr(ary, base, scale);
 145 
 146           access_clone(adr, dst_base, countx, false);
 147         } else {
 148           ideal.sync_kit(this);
 149           ideal.make_leaf_call(OptoRuntime::load_unknown_value_Type(),
 150                                CAST_FROM_FN_PTR(address, OptoRuntime::load_unknown_value),
 151                                "load_unknown_value",
 152                                ary, idx, alloc_obj);
 153           sync_kit(ideal);
 154         }
 155 
 156         // This makes sure no other thread sees a partially initialized buffered value
 157         insert_mem_bar_volatile(Op_MemBarStoreStore, Compile::AliasIdxRaw, alloc->proj_out_or_null(AllocateNode::RawAddress));
 158 
 159         // Same as MemBarCPUOrder above: keep this unknown flattened
 160         // array access correctly ordered with other flattened array
 161         // access
 162         insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
 163 
 164         // Prevent any use of the newly allocated value before it is
 165         // fully initialized
 166         alloc_obj = new CastPPNode(alloc_obj, _gvn.type(alloc_obj), true);
 167         alloc_obj->set_req(0, control());
 168         alloc_obj = _gvn.transform(alloc_obj);
 169 
 170         ideal.sync_kit(this);
 171 
 172         ideal.set(res, alloc_obj);
 173       }
 174     } ideal.end_if();
 175     sync_kit(ideal);
 176     push_node(bt, _gvn.transform(ideal.value(res)));
 177     return;
 178   }
 179 
 180   if (elemtype == TypeInt::BOOL) {
 181     bt = T_BOOLEAN;
 182   } else if (bt == T_OBJECT) {
 183     elemtype = ary_t->elem()->make_oopptr();
 184   }
 185 
 186   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
 187   Node* ld = access_load_at(ary, adr, adr_type, elemtype, bt,
 188                             IN_HEAP | IS_ARRAY | C2_CONTROL_DEPENDENT_LOAD);
 189   if (bt == T_VALUETYPE) {
 190     // Loading a non-flattened (but flattenable) value type from an array
 191     assert(!gvn().type(ld)->maybe_null(), "value type array elements should never be null");
 192     if (elemptr->value_klass()->is_scalarizable()) {
 193       ld = ValueTypeNode::make_from_oop(this, ld, elemptr->value_klass());
 194     }
 195   }
 196 
 197   push_node(bt, ld);
 198 }
 199 
 200 
 201 //--------------------------------array_store----------------------------------
 202 void Parse::array_store(BasicType bt) {
 203   const Type* elemtype = Type::TOP;
 204   Node* adr = array_addressing(bt, type2size[bt], &elemtype);
 205   if (stopped())  return;     // guaranteed null or range check
 206   Node* cast_val = NULL;
 207   if (bt == T_OBJECT) {
 208     cast_val = array_store_check();
 209     if (stopped()) return;
 210   }
 211   Node* val = pop_node(bt); // Value to store
 212   Node* idx = pop();        // Index in the array
 213   Node* ary = pop();        // The array itself
 214 
 215   const TypeAryPtr* ary_t = _gvn.type(ary)->is_aryptr();
 216   if (bt == T_OBJECT) {
 217     const TypeOopPtr* elemptr = elemtype->make_oopptr();
 218     const Type* val_t = _gvn.type(cast_val);
 219     if (elemtype->isa_valuetype() != NULL) {
 220       C->set_flattened_accesses();
 221       // Store to flattened value type array
 222       if (!cast_val->is_ValueType()) {
 223         inc_sp(3);
 224         cast_val = null_check(cast_val);
 225         if (stopped()) return;
 226         dec_sp(3);
 227         cast_val = ValueTypeNode::make_from_oop(this, cast_val, elemtype->value_klass());
 228       }
 229       cast_val->as_ValueType()->store_flattened(this, ary, adr);
 230       return;
 231     } else if (elemptr->is_valuetypeptr() && !elemptr->maybe_null()) {
 232       // Store to non-flattened but flattenable value type array (elements can never be null)
 233       if (!cast_val->is_ValueType() && val_t->maybe_null()) {
 234         inc_sp(3);
 235         cast_val = null_check(cast_val);
 236         if (stopped()) return;
 237         dec_sp(3);
 238       }
 239     } else if (elemptr->can_be_value_type() && !ary_t->klass_is_exact() &&
 240                (cast_val->is_ValueType() || val_t == TypePtr::NULL_PTR || val_t->is_oopptr()->can_be_value_type())) {
 241       // Cannot statically determine if array is flattened or null-free, emit runtime checks
 242       ciValueKlass* vk = NULL;
 243       // Try to determine the value klass
 244       if (cast_val->is_ValueType()) {
 245         vk = val_t->value_klass();
 246       } else if (elemptr->is_valuetypeptr()) {
 247         vk = elemptr->value_klass();
 248       }
 249       if (!ary_t->is_not_flat() && (vk == NULL || vk->flatten_array())) {
 250         // Array might be flattened
 251         assert(ValueArrayFlatten && !ary_t->is_not_null_free(), "a null-ok array can't be flattened");
 252         IdealKit ideal(this);
 253         Node* kls = load_object_klass(ary);
 254         Node* layout_val = load_lh_array_tag(kls);
 255         ideal.if_then(layout_val, BoolTest::ne, intcon(Klass::_lh_array_tag_vt_value));
 256         {
 257           // non-flattened
 258           sync_kit(ideal);
 259           gen_value_array_null_guard(ary, cast_val, 3);
 260           const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
 261           elemtype = ary_t->elem()->make_oopptr();
 262           access_store_at(ary, adr, adr_type, cast_val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY, false, false);
 263           ideal.sync_kit(this);
 264         }
 265         ideal.else_();
 266         {
 267           // flattened
 268           if (!cast_val->is_ValueType() && val_t->maybe_null()) {
 269             // Add null check
 270             sync_kit(ideal);
 271             Node* null_ctl = top();
 272             cast_val = null_check_oop(cast_val, &null_ctl);
 273             if (null_ctl != top()) {
 274               PreserveJVMState pjvms(this);
 275               inc_sp(3);
 276               set_control(null_ctl);
 277               uncommon_trap(Deoptimization::Reason_null_check, Deoptimization::Action_none);
 278               dec_sp(3);
 279             }
 280             ideal.sync_kit(this);
 281           }
 282           if (vk != NULL && !stopped()) {
 283             // Element type is known, cast and store to flattened representation
 284             sync_kit(ideal);
 285             assert(vk->flatten_array(), "must be flattenable");
 286             assert(elemptr->maybe_null(), "must be nullable");
 287             ciArrayKlass* array_klass = ciArrayKlass::make(vk, /* never_null */ true);
 288             const TypeAryPtr* arytype = TypeOopPtr::make_from_klass(array_klass)->isa_aryptr();
 289             ary = _gvn.transform(new CheckCastPPNode(control(), ary, arytype));
 290             adr = array_element_address(ary, idx, T_OBJECT, arytype->size(), control());
 291             if (!cast_val->is_ValueType()) {
 292               assert(!gvn().type(cast_val)->maybe_null(), "value type array elements should never be null");
 293               cast_val = ValueTypeNode::make_from_oop(this, cast_val, vk);
 294             }
 295             cast_val->as_ValueType()->store_flattened(this, ary, adr);
 296             ideal.sync_kit(this);
 297           } else if (!ideal.ctrl()->is_top()) {
 298             // Element type is unknown, emit runtime call
 299             sync_kit(ideal);
 300 
 301             // This membar keeps this access to an unknown flattened
 302             // array correctly ordered with other unknown and known
 303             // flattened array accesses.
 304             insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
 305             ideal.sync_kit(this);
 306 
 307             ideal.make_leaf_call(OptoRuntime::store_unknown_value_Type(),
 308                                  CAST_FROM_FN_PTR(address, OptoRuntime::store_unknown_value),
 309                                  "store_unknown_value",
 310                                  cast_val, ary, idx);
 311 
 312             sync_kit(ideal);
 313             // Same as MemBarCPUOrder above: keep this unknown
 314             // flattened array access correctly ordered with other
 315             // flattened array access
 316             insert_mem_bar_volatile(Op_MemBarCPUOrder, C->get_alias_index(TypeAryPtr::VALUES));
 317             ideal.sync_kit(this);
 318 
 319           }
 320         }
 321         ideal.end_if();
 322         sync_kit(ideal);
 323         return;
 324       } else if (!ary_t->is_not_null_free()) {
 325         // Array is never flattened
 326         gen_value_array_null_guard(ary, cast_val, 3);
 327       }
 328     }
 329   }
 330 
 331   if (elemtype == TypeInt::BOOL) {
 332     bt = T_BOOLEAN;
 333   } else if (bt == T_OBJECT) {
 334     elemtype = ary_t->elem()->make_oopptr();
 335   }
 336 
 337   const TypeAryPtr* adr_type = TypeAryPtr::get_array_body_type(bt);
 338 
 339   access_store_at(ary, adr, adr_type, val, elemtype, bt, MO_UNORDERED | IN_HEAP | IS_ARRAY);
 340 }
 341 
 342 
 343 //------------------------------array_addressing-------------------------------
 344 // Pull array and index from the stack.  Compute pointer-to-element.
 345 Node* Parse::array_addressing(BasicType type, int vals, const Type* *result2) {
 346   Node *idx   = peek(0+vals);   // Get from stack without popping
 347   Node *ary   = peek(1+vals);   // in case of exception
 348 
 349   // Null check the array base, with correct stack contents
 350   ary = null_check(ary, T_ARRAY);
 351   // Compile-time detect of null-exception?
 352   if (stopped())  return top();
 353 
 354   const TypeAryPtr* arytype  = _gvn.type(ary)->is_aryptr();
 355   const TypeInt*    sizetype = arytype->size();
 356   const Type*       elemtype = arytype->elem();
 357 
 358   if (UseUniqueSubclasses && result2 != NULL) {
 359     const Type* el = elemtype->make_ptr();
 360     if (el && el->isa_instptr()) {
 361       const TypeInstPtr* toop = el->is_instptr();
 362       if (toop->klass()->as_instance_klass()->unique_concrete_subklass()) {
 363         // If we load from "AbstractClass[]" we must see "ConcreteSubClass".
 364         const Type* subklass = Type::get_const_type(toop->klass());
 365         elemtype = subklass->join_speculative(el);
 366       }
 367     }
 368   }
 369 
 370   // Check for big class initializers with all constant offsets
 371   // feeding into a known-size array.
 372   const TypeInt* idxtype = _gvn.type(idx)->is_int();
 373   // See if the highest idx value is less than the lowest array bound,
 374   // and if the idx value cannot be negative:
 375   bool need_range_check = true;
 376   if (idxtype->_hi < sizetype->_lo && idxtype->_lo >= 0) {
 377     need_range_check = false;
 378     if (C->log() != NULL)   C->log()->elem("observe that='!need_range_check'");
 379   }
 380 
 381   ciKlass * arytype_klass = arytype->klass();
 382   if ((arytype_klass != NULL) && (!arytype_klass->is_loaded())) {
 383     // Only fails for some -Xcomp runs
 384     // The class is unloaded.  We have to run this bytecode in the interpreter.
 385     uncommon_trap(Deoptimization::Reason_unloaded,
 386                   Deoptimization::Action_reinterpret,
 387                   arytype->klass(), "!loaded array");
 388     return top();
 389   }
 390 
 391   // Do the range check
 392   if (GenerateRangeChecks && need_range_check) {
 393     Node* tst;
 394     if (sizetype->_hi <= 0) {
 395       // The greatest array bound is negative, so we can conclude that we're
 396       // compiling unreachable code, but the unsigned compare trick used below
 397       // only works with non-negative lengths.  Instead, hack "tst" to be zero so
 398       // the uncommon_trap path will always be taken.
 399       tst = _gvn.intcon(0);
 400     } else {
 401       // Range is constant in array-oop, so we can use the original state of mem
 402       Node* len = load_array_length(ary);
 403 
 404       // Test length vs index (standard trick using unsigned compare)
 405       Node* chk = _gvn.transform( new CmpUNode(idx, len) );
 406       BoolTest::mask btest = BoolTest::lt;
 407       tst = _gvn.transform( new BoolNode(chk, btest) );
 408     }
 409     RangeCheckNode* rc = new RangeCheckNode(control(), tst, PROB_MAX, COUNT_UNKNOWN);
 410     _gvn.set_type(rc, rc->Value(&_gvn));
 411     if (!tst->is_Con()) {
 412       record_for_igvn(rc);
 413     }
 414     set_control(_gvn.transform(new IfTrueNode(rc)));
 415     // Branch to failure if out of bounds
 416     {
 417       PreserveJVMState pjvms(this);
 418       set_control(_gvn.transform(new IfFalseNode(rc)));
 419       if (C->allow_range_check_smearing()) {
 420         // Do not use builtin_throw, since range checks are sometimes
 421         // made more stringent by an optimistic transformation.
 422         // This creates "tentative" range checks at this point,
 423         // which are not guaranteed to throw exceptions.
 424         // See IfNode::Ideal, is_range_check, adjust_check.
 425         uncommon_trap(Deoptimization::Reason_range_check,
 426                       Deoptimization::Action_make_not_entrant,
 427                       NULL, "range_check");
 428       } else {
 429         // If we have already recompiled with the range-check-widening
 430         // heroic optimization turned off, then we must really be throwing
 431         // range check exceptions.
 432         builtin_throw(Deoptimization::Reason_range_check, idx);
 433       }
 434     }
 435   }
 436   // Check for always knowing you are throwing a range-check exception
 437   if (stopped())  return top();
 438 
 439   // Speculate on the array not being null-free
 440   if (!arytype->is_not_null_free() && arytype->speculative() != NULL && arytype->speculative()->isa_aryptr() != NULL &&
 441       arytype->speculative()->is_aryptr()->is_not_null_free() &&
 442       !too_many_traps_or_recompiles(Deoptimization::Reason_speculate_class_check)) {
 443     Node* tst = gen_null_free_array_check(ary);
 444     {
 445       BuildCutout unless(this, tst, PROB_ALWAYS);
 446       uncommon_trap(Deoptimization::Reason_speculate_class_check,
 447                     Deoptimization::Action_maybe_recompile);
 448     }
 449     Node* cast = new CheckCastPPNode(control(), ary, arytype->cast_to_not_null_free());
 450     replace_in_map(ary, _gvn.transform(cast));
 451   }
 452 
 453   // Make array address computation control dependent to prevent it
 454   // from floating above the range check during loop optimizations.
 455   Node* ptr = array_element_address(ary, idx, type, sizetype, control());
 456 
 457   if (result2 != NULL)  *result2 = elemtype;
 458 
 459   assert(ptr != top(), "top should go hand-in-hand with stopped");
 460 
 461   return ptr;
 462 }
 463 
 464 
 465 // returns IfNode
 466 IfNode* Parse::jump_if_fork_int(Node* a, Node* b, BoolTest::mask mask, float prob, float cnt) {
 467   Node   *cmp = _gvn.transform(new CmpINode(a, b)); // two cases: shiftcount > 32 and shiftcount <= 32
 468   Node   *tst = _gvn.transform(new BoolNode(cmp, mask));
 469   IfNode *iff = create_and_map_if(control(), tst, prob, cnt);
 470   return iff;
 471 }
 472 
 473 // return Region node
 474 Node* Parse::jump_if_join(Node* iffalse, Node* iftrue) {
 475   Node *region  = new RegionNode(3); // 2 results
 476   record_for_igvn(region);
 477   region->init_req(1, iffalse);
 478   region->init_req(2, iftrue );
 479   _gvn.set_type(region, Type::CONTROL);
 480   region = _gvn.transform(region);
 481   set_control (region);
 482   return region;
 483 }
 484 
 485 // sentinel value for the target bci to mark never taken branches
 486 // (according to profiling)
 487 static const int never_reached = INT_MAX;
 488 
 489 //------------------------------helper for tableswitch-------------------------
 490 void Parse::jump_if_true_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index, bool unc) {
 491   // True branch, use existing map info
 492   { PreserveJVMState pjvms(this);
 493     Node *iftrue  = _gvn.transform( new IfTrueNode (iff) );
 494     set_control( iftrue );
 495     if (unc) {
 496       repush_if_args();
 497       uncommon_trap(Deoptimization::Reason_unstable_if,
 498                     Deoptimization::Action_reinterpret,
 499                     NULL,
 500                     "taken always");
 501     } else {
 502       assert(dest_bci_if_true != never_reached, "inconsistent dest");
 503       profile_switch_case(prof_table_index);
 504       merge_new_path(dest_bci_if_true);
 505     }
 506   }
 507 
 508   // False branch
 509   Node *iffalse = _gvn.transform( new IfFalseNode(iff) );
 510   set_control( iffalse );
 511 }
 512 
 513 void Parse::jump_if_false_fork(IfNode *iff, int dest_bci_if_true, int prof_table_index, bool unc) {
 514   // True branch, use existing map info
 515   { PreserveJVMState pjvms(this);
 516     Node *iffalse  = _gvn.transform( new IfFalseNode (iff) );
 517     set_control( iffalse );
 518     if (unc) {
 519       repush_if_args();
 520       uncommon_trap(Deoptimization::Reason_unstable_if,
 521                     Deoptimization::Action_reinterpret,
 522                     NULL,
 523                     "taken never");
 524     } else {
 525       assert(dest_bci_if_true != never_reached, "inconsistent dest");
 526       profile_switch_case(prof_table_index);
 527       merge_new_path(dest_bci_if_true);
 528     }
 529   }
 530 
 531   // False branch
 532   Node *iftrue = _gvn.transform( new IfTrueNode(iff) );
 533   set_control( iftrue );
 534 }
 535 
 536 void Parse::jump_if_always_fork(int dest_bci, int prof_table_index, bool unc) {
 537   // False branch, use existing map and control()
 538   if (unc) {
 539     repush_if_args();
 540     uncommon_trap(Deoptimization::Reason_unstable_if,
 541                   Deoptimization::Action_reinterpret,
 542                   NULL,
 543                   "taken never");
 544   } else {
 545     assert(dest_bci != never_reached, "inconsistent dest");
 546     profile_switch_case(prof_table_index);
 547     merge_new_path(dest_bci);
 548   }
 549 }
 550 
 551 
 552 extern "C" {
 553   static int jint_cmp(const void *i, const void *j) {
 554     int a = *(jint *)i;
 555     int b = *(jint *)j;
 556     return a > b ? 1 : a < b ? -1 : 0;
 557   }
 558 }
 559 
 560 
 561 // Default value for methodData switch indexing. Must be a negative value to avoid
 562 // conflict with any legal switch index.
 563 #define NullTableIndex -1
 564 
 565 class SwitchRange : public StackObj {
 566   // a range of integers coupled with a bci destination
 567   jint _lo;                     // inclusive lower limit
 568   jint _hi;                     // inclusive upper limit
 569   int _dest;
 570   int _table_index;             // index into method data table
 571   float _cnt;                   // how many times this range was hit according to profiling
 572 
 573 public:
 574   jint lo() const              { return _lo;   }
 575   jint hi() const              { return _hi;   }
 576   int  dest() const            { return _dest; }
 577   int  table_index() const     { return _table_index; }
 578   bool is_singleton() const    { return _lo == _hi; }
 579   float cnt() const            { return _cnt; }
 580 
 581   void setRange(jint lo, jint hi, int dest, int table_index, float cnt) {
 582     assert(lo <= hi, "must be a non-empty range");
 583     _lo = lo, _hi = hi; _dest = dest; _table_index = table_index; _cnt = cnt;
 584     assert(_cnt >= 0, "");
 585   }
 586   bool adjoinRange(jint lo, jint hi, int dest, int table_index, float cnt, bool trim_ranges) {
 587     assert(lo <= hi, "must be a non-empty range");
 588     if (lo == _hi+1 && table_index == _table_index) {
 589       // see merge_ranges() comment below
 590       if (trim_ranges) {
 591         if (cnt == 0) {
 592           if (_cnt != 0) {
 593             return false;
 594           }
 595           if (dest != _dest) {
 596             _dest = never_reached;
 597           }
 598         } else {
 599           if (_cnt == 0) {
 600             return false;
 601           }
 602           if (dest != _dest) {
 603             return false;
 604           }
 605         }
 606       } else {
 607         if (dest != _dest) {
 608           return false;
 609         }
 610       }
 611       _hi = hi;
 612       _cnt += cnt;
 613       return true;
 614     }
 615     return false;
 616   }
 617 
 618   void set (jint value, int dest, int table_index, float cnt) {
 619     setRange(value, value, dest, table_index, cnt);
 620   }
 621   bool adjoin(jint value, int dest, int table_index, float cnt, bool trim_ranges) {
 622     return adjoinRange(value, value, dest, table_index, cnt, trim_ranges);
 623   }
 624   bool adjoin(SwitchRange& other) {
 625     return adjoinRange(other._lo, other._hi, other._dest, other._table_index, other._cnt, false);
 626   }
 627 
 628   void print() {
 629     if (is_singleton())
 630       tty->print(" {%d}=>%d (cnt=%f)", lo(), dest(), cnt());
 631     else if (lo() == min_jint)
 632       tty->print(" {..%d}=>%d (cnt=%f)", hi(), dest(), cnt());
 633     else if (hi() == max_jint)
 634       tty->print(" {%d..}=>%d (cnt=%f)", lo(), dest(), cnt());
 635     else
 636       tty->print(" {%d..%d}=>%d (cnt=%f)", lo(), hi(), dest(), cnt());
 637   }
 638 };
 639 
 640 // We try to minimize the number of ranges and the size of the taken
 641 // ones using profiling data. When ranges are created,
 642 // SwitchRange::adjoinRange() only allows 2 adjoining ranges to merge
 643 // if both were never hit or both were hit to build longer unreached
 644 // ranges. Here, we now merge adjoining ranges with the same
 645 // destination and finally set destination of unreached ranges to the
 646 // special value never_reached because it can help minimize the number
 647 // of tests that are necessary.
 648 //
 649 // For instance:
 650 // [0, 1] to target1 sometimes taken
 651 // [1, 2] to target1 never taken
 652 // [2, 3] to target2 never taken
 653 // would lead to:
 654 // [0, 1] to target1 sometimes taken
 655 // [1, 3] never taken
 656 //
 657 // (first 2 ranges to target1 are not merged)
 658 static void merge_ranges(SwitchRange* ranges, int& rp) {
 659   if (rp == 0) {
 660     return;
 661   }
 662   int shift = 0;
 663   for (int j = 0; j < rp; j++) {
 664     SwitchRange& r1 = ranges[j-shift];
 665     SwitchRange& r2 = ranges[j+1];
 666     if (r1.adjoin(r2)) {
 667       shift++;
 668     } else if (shift > 0) {
 669       ranges[j+1-shift] = r2;
 670     }
 671   }
 672   rp -= shift;
 673   for (int j = 0; j <= rp; j++) {
 674     SwitchRange& r = ranges[j];
 675     if (r.cnt() == 0 && r.dest() != never_reached) {
 676       r.setRange(r.lo(), r.hi(), never_reached, r.table_index(), r.cnt());
 677     }
 678   }
 679 }
 680 
 681 //-------------------------------do_tableswitch--------------------------------
 682 void Parse::do_tableswitch() {
 683   Node* lookup = pop();
 684   // Get information about tableswitch
 685   int default_dest = iter().get_dest_table(0);
 686   int lo_index     = iter().get_int_table(1);
 687   int hi_index     = iter().get_int_table(2);
 688   int len          = hi_index - lo_index + 1;
 689 
 690   if (len < 1) {
 691     // If this is a backward branch, add safepoint
 692     maybe_add_safepoint(default_dest);
 693     merge(default_dest);
 694     return;
 695   }
 696 
 697   ciMethodData* methodData = method()->method_data();
 698   ciMultiBranchData* profile = NULL;
 699   if (methodData->is_mature() && UseSwitchProfiling) {
 700     ciProfileData* data = methodData->bci_to_data(bci());
 701     if (data != NULL && data->is_MultiBranchData()) {
 702       profile = (ciMultiBranchData*)data;
 703     }
 704   }
 705   bool trim_ranges = !method_data_update() && !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
 706 
 707   // generate decision tree, using trichotomy when possible
 708   int rnum = len+2;
 709   bool makes_backward_branch = false;
 710   SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
 711   int rp = -1;
 712   if (lo_index != min_jint) {
 713     uint cnt = 1;
 714     if (profile != NULL) {
 715       cnt = profile->default_count() / (hi_index != max_jint ? 2 : 1);
 716     }
 717     ranges[++rp].setRange(min_jint, lo_index-1, default_dest, NullTableIndex, cnt);
 718   }
 719   for (int j = 0; j < len; j++) {
 720     jint match_int = lo_index+j;
 721     int  dest      = iter().get_dest_table(j+3);
 722     makes_backward_branch |= (dest <= bci());
 723     int  table_index = method_data_update() ? j : NullTableIndex;
 724     uint cnt = 1;
 725     if (profile != NULL) {
 726       cnt = profile->count_at(j);
 727     }
 728     if (rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index, cnt, trim_ranges)) {
 729       ranges[++rp].set(match_int, dest, table_index, cnt);
 730     }
 731   }
 732   jint highest = lo_index+(len-1);
 733   assert(ranges[rp].hi() == highest, "");
 734   if (highest != max_jint) {
 735     uint cnt = 1;
 736     if (profile != NULL) {
 737       cnt = profile->default_count() / (lo_index != min_jint ? 2 : 1);
 738     }
 739     if (!ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex, cnt, trim_ranges)) {
 740       ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex, cnt);
 741     }
 742   }
 743   assert(rp < len+2, "not too many ranges");
 744 
 745   if (trim_ranges) {
 746     merge_ranges(ranges, rp);
 747   }
 748 
 749   // Safepoint in case if backward branch observed
 750   if( makes_backward_branch && UseLoopSafepoints )
 751     add_safepoint();
 752 
 753   jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
 754 }
 755 
 756 
 757 //------------------------------do_lookupswitch--------------------------------
 758 void Parse::do_lookupswitch() {
 759   Node *lookup = pop();         // lookup value
 760   // Get information about lookupswitch
 761   int default_dest = iter().get_dest_table(0);
 762   int len          = iter().get_int_table(1);
 763 
 764   if (len < 1) {    // If this is a backward branch, add safepoint
 765     maybe_add_safepoint(default_dest);
 766     merge(default_dest);
 767     return;
 768   }
 769 
 770   ciMethodData* methodData = method()->method_data();
 771   ciMultiBranchData* profile = NULL;
 772   if (methodData->is_mature() && UseSwitchProfiling) {
 773     ciProfileData* data = methodData->bci_to_data(bci());
 774     if (data != NULL && data->is_MultiBranchData()) {
 775       profile = (ciMultiBranchData*)data;
 776     }
 777   }
 778   bool trim_ranges = !method_data_update() && !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
 779 
 780   // generate decision tree, using trichotomy when possible
 781   jint* table = NEW_RESOURCE_ARRAY(jint, len*3);
 782   {
 783     for (int j = 0; j < len; j++) {
 784       table[3*j+0] = iter().get_int_table(2+2*j);
 785       table[3*j+1] = iter().get_dest_table(2+2*j+1);
 786       table[3*j+2] = profile == NULL ? 1 : profile->count_at(j);
 787     }
 788     qsort(table, len, 3*sizeof(table[0]), jint_cmp);
 789   }
 790 
 791   float defaults = 0;
 792   jint prev = min_jint;
 793   for (int j = 0; j < len; j++) {
 794     jint match_int = table[3*j+0];
 795     if (match_int != prev) {
 796       defaults += (float)match_int - prev;
 797     }
 798     prev = match_int+1;
 799   }
 800   if (prev-1 != max_jint) {
 801     defaults += (float)max_jint - prev + 1;
 802   }
 803   float default_cnt = 1;
 804   if (profile != NULL) {
 805     default_cnt = profile->default_count()/defaults;
 806   }
 807 
 808   int rnum = len*2+1;
 809   bool makes_backward_branch = false;
 810   SwitchRange* ranges = NEW_RESOURCE_ARRAY(SwitchRange, rnum);
 811   int rp = -1;
 812   for (int j = 0; j < len; j++) {
 813     jint match_int   = table[3*j+0];
 814     int  dest        = table[3*j+1];
 815     int  cnt         = table[3*j+2];
 816     int  next_lo     = rp < 0 ? min_jint : ranges[rp].hi()+1;
 817     int  table_index = method_data_update() ? j : NullTableIndex;
 818     makes_backward_branch |= (dest <= bci());
 819     float c = default_cnt * ((float)match_int - next_lo);
 820     if (match_int != next_lo && (rp < 0 || !ranges[rp].adjoinRange(next_lo, match_int-1, default_dest, NullTableIndex, c, trim_ranges))) {
 821       assert(default_dest != never_reached, "sentinel value for dead destinations");
 822       ranges[++rp].setRange(next_lo, match_int-1, default_dest, NullTableIndex, c);
 823     }
 824     if (rp < 0 || !ranges[rp].adjoin(match_int, dest, table_index, cnt, trim_ranges)) {
 825       assert(dest != never_reached, "sentinel value for dead destinations");
 826       ranges[++rp].set(match_int, dest, table_index, cnt);
 827     }
 828   }
 829   jint highest = table[3*(len-1)];
 830   assert(ranges[rp].hi() == highest, "");
 831   if (highest != max_jint &&
 832       !ranges[rp].adjoinRange(highest+1, max_jint, default_dest, NullTableIndex, default_cnt * ((float)max_jint - highest), trim_ranges)) {
 833     ranges[++rp].setRange(highest+1, max_jint, default_dest, NullTableIndex, default_cnt * ((float)max_jint - highest));
 834   }
 835   assert(rp < rnum, "not too many ranges");
 836 
 837   if (trim_ranges) {
 838     merge_ranges(ranges, rp);
 839   }
 840 
 841   // Safepoint in case backward branch observed
 842   if (makes_backward_branch && UseLoopSafepoints)
 843     add_safepoint();
 844 
 845   jump_switch_ranges(lookup, &ranges[0], &ranges[rp]);
 846 }
 847 
 848 static float if_prob(float taken_cnt, float total_cnt) {
 849   assert(taken_cnt <= total_cnt, "");
 850   if (total_cnt == 0) {
 851     return PROB_FAIR;
 852   }
 853   float p = taken_cnt / total_cnt;
 854   return MIN2(MAX2(p, PROB_MIN), PROB_MAX);
 855 }
 856 
 857 static float if_cnt(float cnt) {
 858   if (cnt == 0) {
 859     return COUNT_UNKNOWN;
 860   }
 861   return cnt;
 862 }
 863 
 864 static float sum_of_cnts(SwitchRange *lo, SwitchRange *hi) {
 865   float total_cnt = 0;
 866   for (SwitchRange* sr = lo; sr <= hi; sr++) {
 867     total_cnt += sr->cnt();
 868   }
 869   return total_cnt;
 870 }
 871 
 872 class SwitchRanges : public ResourceObj {
 873 public:
 874   SwitchRange* _lo;
 875   SwitchRange* _hi;
 876   SwitchRange* _mid;
 877   float _cost;
 878 
 879   enum {
 880     Start,
 881     LeftDone,
 882     RightDone,
 883     Done
 884   } _state;
 885 
 886   SwitchRanges(SwitchRange *lo, SwitchRange *hi)
 887     : _lo(lo), _hi(hi), _mid(NULL),
 888       _cost(0), _state(Start) {
 889   }
 890 
 891   SwitchRanges()
 892     : _lo(NULL), _hi(NULL), _mid(NULL),
 893       _cost(0), _state(Start) {}
 894 };
 895 
 896 // Estimate cost of performing a binary search on lo..hi
 897 static float compute_tree_cost(SwitchRange *lo, SwitchRange *hi, float total_cnt) {
 898   GrowableArray<SwitchRanges> tree;
 899   SwitchRanges root(lo, hi);
 900   tree.push(root);
 901 
 902   float cost = 0;
 903   do {
 904     SwitchRanges& r = *tree.adr_at(tree.length()-1);
 905     if (r._hi != r._lo) {
 906       if (r._mid == NULL) {
 907         float r_cnt = sum_of_cnts(r._lo, r._hi);
 908 
 909         if (r_cnt == 0) {
 910           tree.pop();
 911           cost = 0;
 912           continue;
 913         }
 914 
 915         SwitchRange* mid = NULL;
 916         mid = r._lo;
 917         for (float cnt = 0; ; ) {
 918           assert(mid <= r._hi, "out of bounds");
 919           cnt += mid->cnt();
 920           if (cnt > r_cnt / 2) {
 921             break;
 922           }
 923           mid++;
 924         }
 925         assert(mid <= r._hi, "out of bounds");
 926         r._mid = mid;
 927         r._cost = r_cnt / total_cnt;
 928       }
 929       r._cost += cost;
 930       if (r._state < SwitchRanges::LeftDone && r._mid > r._lo) {
 931         cost = 0;
 932         r._state = SwitchRanges::LeftDone;
 933         tree.push(SwitchRanges(r._lo, r._mid-1));
 934       } else if (r._state < SwitchRanges::RightDone) {
 935         cost = 0;
 936         r._state = SwitchRanges::RightDone;
 937         tree.push(SwitchRanges(r._mid == r._lo ? r._mid+1 : r._mid, r._hi));
 938       } else {
 939         tree.pop();
 940         cost = r._cost;
 941       }
 942     } else {
 943       tree.pop();
 944       cost = r._cost;
 945     }
 946   } while (tree.length() > 0);
 947 
 948 
 949   return cost;
 950 }
 951 
 952 // It sometimes pays off to test most common ranges before the binary search
 953 void Parse::linear_search_switch_ranges(Node* key_val, SwitchRange*& lo, SwitchRange*& hi) {
 954   uint nr = hi - lo + 1;
 955   float total_cnt = sum_of_cnts(lo, hi);
 956 
 957   float min = compute_tree_cost(lo, hi, total_cnt);
 958   float extra = 1;
 959   float sub = 0;
 960 
 961   SwitchRange* array1 = lo;
 962   SwitchRange* array2 = NEW_RESOURCE_ARRAY(SwitchRange, nr);
 963 
 964   SwitchRange* ranges = NULL;
 965 
 966   while (nr >= 2) {
 967     assert(lo == array1 || lo == array2, "one the 2 already allocated arrays");
 968     ranges = (lo == array1) ? array2 : array1;
 969 
 970     // Find highest frequency range
 971     SwitchRange* candidate = lo;
 972     for (SwitchRange* sr = lo+1; sr <= hi; sr++) {
 973       if (sr->cnt() > candidate->cnt()) {
 974         candidate = sr;
 975       }
 976     }
 977     SwitchRange most_freq = *candidate;
 978     if (most_freq.cnt() == 0) {
 979       break;
 980     }
 981 
 982     // Copy remaining ranges into another array
 983     int shift = 0;
 984     for (uint i = 0; i < nr; i++) {
 985       SwitchRange* sr = &lo[i];
 986       if (sr != candidate) {
 987         ranges[i-shift] = *sr;
 988       } else {
 989         shift++;
 990         if (i > 0 && i < nr-1) {
 991           SwitchRange prev = lo[i-1];
 992           prev.setRange(prev.lo(), sr->hi(), prev.dest(), prev.table_index(), prev.cnt());
 993           if (prev.adjoin(lo[i+1])) {
 994             shift++;
 995             i++;
 996           }
 997           ranges[i-shift] = prev;
 998         }
 999       }
1000     }
1001     nr -= shift;
1002 
1003     // Evaluate cost of testing the most common range and performing a
1004     // binary search on the other ranges
1005     float cost = extra + compute_tree_cost(&ranges[0], &ranges[nr-1], total_cnt);
1006     if (cost >= min) {
1007       break;
1008     }
1009     // swap arrays
1010     lo = &ranges[0];
1011     hi = &ranges[nr-1];
1012 
1013     // It pays off: emit the test for the most common range
1014     assert(most_freq.cnt() > 0, "must be taken");
1015     Node* val = _gvn.transform(new SubINode(key_val, _gvn.intcon(most_freq.lo())));
1016     Node* cmp = _gvn.transform(new CmpUNode(val, _gvn.intcon(most_freq.hi() - most_freq.lo())));
1017     Node* tst = _gvn.transform(new BoolNode(cmp, BoolTest::le));
1018     IfNode* iff = create_and_map_if(control(), tst, if_prob(most_freq.cnt(), total_cnt), if_cnt(most_freq.cnt()));
1019     jump_if_true_fork(iff, most_freq.dest(), most_freq.table_index(), false);
1020 
1021     sub += most_freq.cnt() / total_cnt;
1022     extra += 1 - sub;
1023     min = cost;
1024   }
1025 }
1026 
1027 //----------------------------create_jump_tables-------------------------------
1028 bool Parse::create_jump_tables(Node* key_val, SwitchRange* lo, SwitchRange* hi) {
1029   // Are jumptables enabled
1030   if (!UseJumpTables)  return false;
1031 
1032   // Are jumptables supported
1033   if (!Matcher::has_match_rule(Op_Jump))  return false;
1034 
1035   // Don't make jump table if profiling
1036   if (method_data_update())  return false;
1037 
1038   bool trim_ranges = !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
1039 
1040   // Decide if a guard is needed to lop off big ranges at either (or
1041   // both) end(s) of the input set. We'll call this the default target
1042   // even though we can't be sure that it is the true "default".
1043 
1044   bool needs_guard = false;
1045   int default_dest;
1046   int64_t total_outlier_size = 0;
1047   int64_t hi_size = ((int64_t)hi->hi()) - ((int64_t)hi->lo()) + 1;
1048   int64_t lo_size = ((int64_t)lo->hi()) - ((int64_t)lo->lo()) + 1;
1049 
1050   if (lo->dest() == hi->dest()) {
1051     total_outlier_size = hi_size + lo_size;
1052     default_dest = lo->dest();
1053   } else if (lo_size > hi_size) {
1054     total_outlier_size = lo_size;
1055     default_dest = lo->dest();
1056   } else {
1057     total_outlier_size = hi_size;
1058     default_dest = hi->dest();
1059   }
1060 
1061   float total = sum_of_cnts(lo, hi);
1062   float cost = compute_tree_cost(lo, hi, total);
1063 
1064   // If a guard test will eliminate very sparse end ranges, then
1065   // it is worth the cost of an extra jump.
1066   float trimmed_cnt = 0;
1067   if (total_outlier_size > (MaxJumpTableSparseness * 4)) {
1068     needs_guard = true;
1069     if (default_dest == lo->dest()) {
1070       trimmed_cnt += lo->cnt();
1071       lo++;
1072     }
1073     if (default_dest == hi->dest()) {
1074       trimmed_cnt += hi->cnt();
1075       hi--;
1076     }
1077   }
1078 
1079   // Find the total number of cases and ranges
1080   int64_t num_cases = ((int64_t)hi->hi()) - ((int64_t)lo->lo()) + 1;
1081   int num_range = hi - lo + 1;
1082 
1083   // Don't create table if: too large, too small, or too sparse.
1084   if (num_cases > MaxJumpTableSize)
1085     return false;
1086   if (UseSwitchProfiling) {
1087     // MinJumpTableSize is set so with a well balanced binary tree,
1088     // when the number of ranges is MinJumpTableSize, it's cheaper to
1089     // go through a JumpNode that a tree of IfNodes. Average cost of a
1090     // tree of IfNodes with MinJumpTableSize is
1091     // log2f(MinJumpTableSize) comparisons. So if the cost computed
1092     // from profile data is less than log2f(MinJumpTableSize) then
1093     // going with the binary search is cheaper.
1094     if (cost < log2f(MinJumpTableSize)) {
1095       return false;
1096     }
1097   } else {
1098     if (num_cases < MinJumpTableSize)
1099       return false;
1100   }
1101   if (num_cases > (MaxJumpTableSparseness * num_range))
1102     return false;
1103 
1104   // Normalize table lookups to zero
1105   int lowval = lo->lo();
1106   key_val = _gvn.transform( new SubINode(key_val, _gvn.intcon(lowval)) );
1107 
1108   // Generate a guard to protect against input keyvals that aren't
1109   // in the switch domain.
1110   if (needs_guard) {
1111     Node*   size = _gvn.intcon(num_cases);
1112     Node*   cmp = _gvn.transform(new CmpUNode(key_val, size));
1113     Node*   tst = _gvn.transform(new BoolNode(cmp, BoolTest::ge));
1114     IfNode* iff = create_and_map_if(control(), tst, if_prob(trimmed_cnt, total), if_cnt(trimmed_cnt));
1115     jump_if_true_fork(iff, default_dest, NullTableIndex, trim_ranges && trimmed_cnt == 0);
1116 
1117     total -= trimmed_cnt;
1118   }
1119 
1120   // Create an ideal node JumpTable that has projections
1121   // of all possible ranges for a switch statement
1122   // The key_val input must be converted to a pointer offset and scaled.
1123   // Compare Parse::array_addressing above.
1124 
1125   // Clean the 32-bit int into a real 64-bit offset.
1126   // Otherwise, the jint value 0 might turn into an offset of 0x0800000000.
1127   const TypeInt* ikeytype = TypeInt::make(0, num_cases, Type::WidenMin);
1128   // Make I2L conversion control dependent to prevent it from
1129   // floating above the range check during loop optimizations.
1130   key_val = C->conv_I2X_index(&_gvn, key_val, ikeytype, control());
1131 
1132   // Shift the value by wordsize so we have an index into the table, rather
1133   // than a switch value
1134   Node *shiftWord = _gvn.MakeConX(wordSize);
1135   key_val = _gvn.transform( new MulXNode( key_val, shiftWord));
1136 
1137   // Create the JumpNode
1138   Arena* arena = C->comp_arena();
1139   float* probs = (float*)arena->Amalloc(sizeof(float)*num_cases);
1140   int i = 0;
1141   if (total == 0) {
1142     for (SwitchRange* r = lo; r <= hi; r++) {
1143       for (int64_t j = r->lo(); j <= r->hi(); j++, i++) {
1144         probs[i] = 1.0F / num_cases;
1145       }
1146     }
1147   } else {
1148     for (SwitchRange* r = lo; r <= hi; r++) {
1149       float prob = r->cnt()/total;
1150       for (int64_t j = r->lo(); j <= r->hi(); j++, i++) {
1151         probs[i] = prob / (r->hi() - r->lo() + 1);
1152       }
1153     }
1154   }
1155 
1156   ciMethodData* methodData = method()->method_data();
1157   ciMultiBranchData* profile = NULL;
1158   if (methodData->is_mature()) {
1159     ciProfileData* data = methodData->bci_to_data(bci());
1160     if (data != NULL && data->is_MultiBranchData()) {
1161       profile = (ciMultiBranchData*)data;
1162     }
1163   }
1164 
1165   Node* jtn = _gvn.transform(new JumpNode(control(), key_val, num_cases, probs, profile == NULL ? COUNT_UNKNOWN : total));
1166 
1167   // These are the switch destinations hanging off the jumpnode
1168   i = 0;
1169   for (SwitchRange* r = lo; r <= hi; r++) {
1170     for (int64_t j = r->lo(); j <= r->hi(); j++, i++) {
1171       Node* input = _gvn.transform(new JumpProjNode(jtn, i, r->dest(), (int)(j - lowval)));
1172       {
1173         PreserveJVMState pjvms(this);
1174         set_control(input);
1175         jump_if_always_fork(r->dest(), r->table_index(), trim_ranges && r->cnt() == 0);
1176       }
1177     }
1178   }
1179   assert(i == num_cases, "miscount of cases");
1180   stop_and_kill_map();  // no more uses for this JVMS
1181   return true;
1182 }
1183 
1184 //----------------------------jump_switch_ranges-------------------------------
1185 void Parse::jump_switch_ranges(Node* key_val, SwitchRange *lo, SwitchRange *hi, int switch_depth) {
1186   Block* switch_block = block();
1187   bool trim_ranges = !method_data_update() && !C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if);
1188 
1189   if (switch_depth == 0) {
1190     // Do special processing for the top-level call.
1191     assert(lo->lo() == min_jint, "initial range must exhaust Type::INT");
1192     assert(hi->hi() == max_jint, "initial range must exhaust Type::INT");
1193 
1194     // Decrement pred-numbers for the unique set of nodes.
1195 #ifdef ASSERT
1196     if (!trim_ranges) {
1197       // Ensure that the block's successors are a (duplicate-free) set.
1198       int successors_counted = 0;  // block occurrences in [hi..lo]
1199       int unique_successors = switch_block->num_successors();
1200       for (int i = 0; i < unique_successors; i++) {
1201         Block* target = switch_block->successor_at(i);
1202 
1203         // Check that the set of successors is the same in both places.
1204         int successors_found = 0;
1205         for (SwitchRange* p = lo; p <= hi; p++) {
1206           if (p->dest() == target->start())  successors_found++;
1207         }
1208         assert(successors_found > 0, "successor must be known");
1209         successors_counted += successors_found;
1210       }
1211       assert(successors_counted == (hi-lo)+1, "no unexpected successors");
1212     }
1213 #endif
1214 
1215     // Maybe prune the inputs, based on the type of key_val.
1216     jint min_val = min_jint;
1217     jint max_val = max_jint;
1218     const TypeInt* ti = key_val->bottom_type()->isa_int();
1219     if (ti != NULL) {
1220       min_val = ti->_lo;
1221       max_val = ti->_hi;
1222       assert(min_val <= max_val, "invalid int type");
1223     }
1224     while (lo->hi() < min_val) {
1225       lo++;
1226     }
1227     if (lo->lo() < min_val)  {
1228       lo->setRange(min_val, lo->hi(), lo->dest(), lo->table_index(), lo->cnt());
1229     }
1230     while (hi->lo() > max_val) {
1231       hi--;
1232     }
1233     if (hi->hi() > max_val) {
1234       hi->setRange(hi->lo(), max_val, hi->dest(), hi->table_index(), hi->cnt());
1235     }
1236 
1237     linear_search_switch_ranges(key_val, lo, hi);
1238   }
1239 
1240 #ifndef PRODUCT
1241   if (switch_depth == 0) {
1242     _max_switch_depth = 0;
1243     _est_switch_depth = log2_intptr((hi-lo+1)-1)+1;
1244   }
1245 #endif
1246 
1247   assert(lo <= hi, "must be a non-empty set of ranges");
1248   if (lo == hi) {
1249     jump_if_always_fork(lo->dest(), lo->table_index(), trim_ranges && lo->cnt() == 0);
1250   } else {
1251     assert(lo->hi() == (lo+1)->lo()-1, "contiguous ranges");
1252     assert(hi->lo() == (hi-1)->hi()+1, "contiguous ranges");
1253 
1254     if (create_jump_tables(key_val, lo, hi)) return;
1255 
1256     SwitchRange* mid = NULL;
1257     float total_cnt = sum_of_cnts(lo, hi);
1258 
1259     int nr = hi - lo + 1;
1260     if (UseSwitchProfiling) {
1261       // Don't keep the binary search tree balanced: pick up mid point
1262       // that split frequencies in half.
1263       float cnt = 0;
1264       for (SwitchRange* sr = lo; sr <= hi; sr++) {
1265         cnt += sr->cnt();
1266         if (cnt >= total_cnt / 2) {
1267           mid = sr;
1268           break;
1269         }
1270       }
1271     } else {
1272       mid = lo + nr/2;
1273 
1274       // if there is an easy choice, pivot at a singleton:
1275       if (nr > 3 && !mid->is_singleton() && (mid-1)->is_singleton())  mid--;
1276 
1277       assert(lo < mid && mid <= hi, "good pivot choice");
1278       assert(nr != 2 || mid == hi,   "should pick higher of 2");
1279       assert(nr != 3 || mid == hi-1, "should pick middle of 3");
1280     }
1281 
1282 
1283     Node *test_val = _gvn.intcon(mid == lo ? mid->hi() : mid->lo());
1284 
1285     if (mid->is_singleton()) {
1286       IfNode *iff_ne = jump_if_fork_int(key_val, test_val, BoolTest::ne, 1-if_prob(mid->cnt(), total_cnt), if_cnt(mid->cnt()));
1287       jump_if_false_fork(iff_ne, mid->dest(), mid->table_index(), trim_ranges && mid->cnt() == 0);
1288 
1289       // Special Case:  If there are exactly three ranges, and the high
1290       // and low range each go to the same place, omit the "gt" test,
1291       // since it will not discriminate anything.
1292       bool eq_test_only = (hi == lo+2 && hi->dest() == lo->dest() && mid == hi-1) || mid == lo;
1293 
1294       // if there is a higher range, test for it and process it:
1295       if (mid < hi && !eq_test_only) {
1296         // two comparisons of same values--should enable 1 test for 2 branches
1297         // Use BoolTest::le instead of BoolTest::gt
1298         float cnt = sum_of_cnts(lo, mid-1);
1299         IfNode *iff_le  = jump_if_fork_int(key_val, test_val, BoolTest::le, if_prob(cnt, total_cnt), if_cnt(cnt));
1300         Node   *iftrue  = _gvn.transform( new IfTrueNode(iff_le) );
1301         Node   *iffalse = _gvn.transform( new IfFalseNode(iff_le) );
1302         { PreserveJVMState pjvms(this);
1303           set_control(iffalse);
1304           jump_switch_ranges(key_val, mid+1, hi, switch_depth+1);
1305         }
1306         set_control(iftrue);
1307       }
1308 
1309     } else {
1310       // mid is a range, not a singleton, so treat mid..hi as a unit
1311       float cnt = sum_of_cnts(mid == lo ? mid+1 : mid, hi);
1312       IfNode *iff_ge = jump_if_fork_int(key_val, test_val, mid == lo ? BoolTest::gt : BoolTest::ge, if_prob(cnt, total_cnt), if_cnt(cnt));
1313 
1314       // if there is a higher range, test for it and process it:
1315       if (mid == hi) {
1316         jump_if_true_fork(iff_ge, mid->dest(), mid->table_index(), trim_ranges && cnt == 0);
1317       } else {
1318         Node *iftrue  = _gvn.transform( new IfTrueNode(iff_ge) );
1319         Node *iffalse = _gvn.transform( new IfFalseNode(iff_ge) );
1320         { PreserveJVMState pjvms(this);
1321           set_control(iftrue);
1322           jump_switch_ranges(key_val, mid == lo ? mid+1 : mid, hi, switch_depth+1);
1323         }
1324         set_control(iffalse);
1325       }
1326     }
1327 
1328     // in any case, process the lower range
1329     if (mid == lo) {
1330       if (mid->is_singleton()) {
1331         jump_switch_ranges(key_val, lo+1, hi, switch_depth+1);
1332       } else {
1333         jump_if_always_fork(lo->dest(), lo->table_index(), trim_ranges && lo->cnt() == 0);
1334       }
1335     } else {
1336       jump_switch_ranges(key_val, lo, mid-1, switch_depth+1);
1337     }
1338   }
1339 
1340   // Decrease pred_count for each successor after all is done.
1341   if (switch_depth == 0) {
1342     int unique_successors = switch_block->num_successors();
1343     for (int i = 0; i < unique_successors; i++) {
1344       Block* target = switch_block->successor_at(i);
1345       // Throw away the pre-allocated path for each unique successor.
1346       target->next_path_num();
1347     }
1348   }
1349 
1350 #ifndef PRODUCT
1351   _max_switch_depth = MAX2(switch_depth, _max_switch_depth);
1352   if (TraceOptoParse && Verbose && WizardMode && switch_depth == 0) {
1353     SwitchRange* r;
1354     int nsing = 0;
1355     for( r = lo; r <= hi; r++ ) {
1356       if( r->is_singleton() )  nsing++;
1357     }
1358     tty->print(">>> ");
1359     _method->print_short_name();
1360     tty->print_cr(" switch decision tree");
1361     tty->print_cr("    %d ranges (%d singletons), max_depth=%d, est_depth=%d",
1362                   (int) (hi-lo+1), nsing, _max_switch_depth, _est_switch_depth);
1363     if (_max_switch_depth > _est_switch_depth) {
1364       tty->print_cr("******** BAD SWITCH DEPTH ********");
1365     }
1366     tty->print("   ");
1367     for( r = lo; r <= hi; r++ ) {
1368       r->print();
1369     }
1370     tty->cr();
1371   }
1372 #endif
1373 }
1374 
1375 void Parse::modf() {
1376   Node *f2 = pop();
1377   Node *f1 = pop();
1378   Node* c = make_runtime_call(RC_LEAF, OptoRuntime::modf_Type(),
1379                               CAST_FROM_FN_PTR(address, SharedRuntime::frem),
1380                               "frem", NULL, //no memory effects
1381                               f1, f2);
1382   Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0));
1383 
1384   push(res);
1385 }
1386 
1387 void Parse::modd() {
1388   Node *d2 = pop_pair();
1389   Node *d1 = pop_pair();
1390   Node* c = make_runtime_call(RC_LEAF, OptoRuntime::Math_DD_D_Type(),
1391                               CAST_FROM_FN_PTR(address, SharedRuntime::drem),
1392                               "drem", NULL, //no memory effects
1393                               d1, top(), d2, top());
1394   Node* res_d   = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0));
1395 
1396 #ifdef ASSERT
1397   Node* res_top = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 1));
1398   assert(res_top == top(), "second value must be top");
1399 #endif
1400 
1401   push_pair(res_d);
1402 }
1403 
1404 void Parse::l2f() {
1405   Node* f2 = pop();
1406   Node* f1 = pop();
1407   Node* c = make_runtime_call(RC_LEAF, OptoRuntime::l2f_Type(),
1408                               CAST_FROM_FN_PTR(address, SharedRuntime::l2f),
1409                               "l2f", NULL, //no memory effects
1410                               f1, f2);
1411   Node* res = _gvn.transform(new ProjNode(c, TypeFunc::Parms + 0));
1412 
1413   push(res);
1414 }
1415 
1416 void Parse::do_irem() {
1417   // Must keep both values on the expression-stack during null-check
1418   zero_check_int(peek());
1419   // Compile-time detect of null-exception?
1420   if (stopped())  return;
1421 
1422   Node* b = pop();
1423   Node* a = pop();
1424 
1425   const Type *t = _gvn.type(b);
1426   if (t != Type::TOP) {
1427     const TypeInt *ti = t->is_int();
1428     if (ti->is_con()) {
1429       int divisor = ti->get_con();
1430       // check for positive power of 2
1431       if (divisor > 0 &&
1432           (divisor & ~(divisor-1)) == divisor) {
1433         // yes !
1434         Node *mask = _gvn.intcon((divisor - 1));
1435         // Sigh, must handle negative dividends
1436         Node *zero = _gvn.intcon(0);
1437         IfNode *ifff = jump_if_fork_int(a, zero, BoolTest::lt, PROB_FAIR, COUNT_UNKNOWN);
1438         Node *iff = _gvn.transform( new IfFalseNode(ifff) );
1439         Node *ift = _gvn.transform( new IfTrueNode (ifff) );
1440         Node *reg = jump_if_join(ift, iff);
1441         Node *phi = PhiNode::make(reg, NULL, TypeInt::INT);
1442         // Negative path; negate/and/negate
1443         Node *neg = _gvn.transform( new SubINode(zero, a) );
1444         Node *andn= _gvn.transform( new AndINode(neg, mask) );
1445         Node *negn= _gvn.transform( new SubINode(zero, andn) );
1446         phi->init_req(1, negn);
1447         // Fast positive case
1448         Node *andx = _gvn.transform( new AndINode(a, mask) );
1449         phi->init_req(2, andx);
1450         // Push the merge
1451         push( _gvn.transform(phi) );
1452         return;
1453       }
1454     }
1455   }
1456   // Default case
1457   push( _gvn.transform( new ModINode(control(),a,b) ) );
1458 }
1459 
1460 // Handle jsr and jsr_w bytecode
1461 void Parse::do_jsr() {
1462   assert(bc() == Bytecodes::_jsr || bc() == Bytecodes::_jsr_w, "wrong bytecode");
1463 
1464   // Store information about current state, tagged with new _jsr_bci
1465   int return_bci = iter().next_bci();
1466   int jsr_bci    = (bc() == Bytecodes::_jsr) ? iter().get_dest() : iter().get_far_dest();
1467 
1468   // Update method data
1469   profile_taken_branch(jsr_bci);
1470 
1471   // The way we do things now, there is only one successor block
1472   // for the jsr, because the target code is cloned by ciTypeFlow.
1473   Block* target = successor_for_bci(jsr_bci);
1474 
1475   // What got pushed?
1476   const Type* ret_addr = target->peek();
1477   assert(ret_addr->singleton(), "must be a constant (cloned jsr body)");
1478 
1479   // Effect on jsr on stack
1480   push(_gvn.makecon(ret_addr));
1481 
1482   // Flow to the jsr.
1483   merge(jsr_bci);
1484 }
1485 
1486 // Handle ret bytecode
1487 void Parse::do_ret() {
1488   // Find to whom we return.
1489   assert(block()->num_successors() == 1, "a ret can only go one place now");
1490   Block* target = block()->successor_at(0);
1491   assert(!target->is_ready(), "our arrival must be expected");
1492   profile_ret(target->flow()->start());
1493   int pnum = target->next_path_num();
1494   merge_common(target, pnum);
1495 }
1496 
1497 static bool has_injected_profile(BoolTest::mask btest, Node* test, int& taken, int& not_taken) {
1498   if (btest != BoolTest::eq && btest != BoolTest::ne) {
1499     // Only ::eq and ::ne are supported for profile injection.
1500     return false;
1501   }
1502   if (test->is_Cmp() &&
1503       test->in(1)->Opcode() == Op_ProfileBoolean) {
1504     ProfileBooleanNode* profile = (ProfileBooleanNode*)test->in(1);
1505     int false_cnt = profile->false_count();
1506     int  true_cnt = profile->true_count();
1507 
1508     // Counts matching depends on the actual test operation (::eq or ::ne).
1509     // No need to scale the counts because profile injection was designed
1510     // to feed exact counts into VM.
1511     taken     = (btest == BoolTest::eq) ? false_cnt :  true_cnt;
1512     not_taken = (btest == BoolTest::eq) ?  true_cnt : false_cnt;
1513 
1514     profile->consume();
1515     return true;
1516   }
1517   return false;
1518 }
1519 //--------------------------dynamic_branch_prediction--------------------------
1520 // Try to gather dynamic branch prediction behavior.  Return a probability
1521 // of the branch being taken and set the "cnt" field.  Returns a -1.0
1522 // if we need to use static prediction for some reason.
1523 float Parse::dynamic_branch_prediction(float &cnt, BoolTest::mask btest, Node* test) {
1524   ResourceMark rm;
1525 
1526   cnt  = COUNT_UNKNOWN;
1527 
1528   int     taken = 0;
1529   int not_taken = 0;
1530 
1531   bool use_mdo = !has_injected_profile(btest, test, taken, not_taken);
1532 
1533   if (use_mdo) {
1534     // Use MethodData information if it is available
1535     // FIXME: free the ProfileData structure
1536     ciMethodData* methodData = method()->method_data();
1537     if (!methodData->is_mature())  return PROB_UNKNOWN;
1538     ciProfileData* data = methodData->bci_to_data(bci());
1539     if (data == NULL) {
1540       return PROB_UNKNOWN;
1541     }
1542     if (!data->is_JumpData())  return PROB_UNKNOWN;
1543 
1544     // get taken and not taken values
1545     taken = data->as_JumpData()->taken();
1546     not_taken = 0;
1547     if (data->is_BranchData()) {
1548       not_taken = data->as_BranchData()->not_taken();
1549     }
1550 
1551     // scale the counts to be commensurate with invocation counts:
1552     taken = method()->scale_count(taken);
1553     not_taken = method()->scale_count(not_taken);
1554   }
1555 
1556   // Give up if too few (or too many, in which case the sum will overflow) counts to be meaningful.
1557   // We also check that individual counters are positive first, otherwise the sum can become positive.
1558   if (taken < 0 || not_taken < 0 || taken + not_taken < 40) {
1559     if (C->log() != NULL) {
1560       C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d'", iter().get_dest(), taken, not_taken);
1561     }
1562     return PROB_UNKNOWN;
1563   }
1564 
1565   // Compute frequency that we arrive here
1566   float sum = taken + not_taken;
1567   // Adjust, if this block is a cloned private block but the
1568   // Jump counts are shared.  Taken the private counts for
1569   // just this path instead of the shared counts.
1570   if( block()->count() > 0 )
1571     sum = block()->count();
1572   cnt = sum / FreqCountInvocations;
1573 
1574   // Pin probability to sane limits
1575   float prob;
1576   if( !taken )
1577     prob = (0+PROB_MIN) / 2;
1578   else if( !not_taken )
1579     prob = (1+PROB_MAX) / 2;
1580   else {                         // Compute probability of true path
1581     prob = (float)taken / (float)(taken + not_taken);
1582     if (prob > PROB_MAX)  prob = PROB_MAX;
1583     if (prob < PROB_MIN)   prob = PROB_MIN;
1584   }
1585 
1586   assert((cnt > 0.0f) && (prob > 0.0f),
1587          "Bad frequency assignment in if");
1588 
1589   if (C->log() != NULL) {
1590     const char* prob_str = NULL;
1591     if (prob >= PROB_MAX)  prob_str = (prob == PROB_MAX) ? "max" : "always";
1592     if (prob <= PROB_MIN)  prob_str = (prob == PROB_MIN) ? "min" : "never";
1593     char prob_str_buf[30];
1594     if (prob_str == NULL) {
1595       jio_snprintf(prob_str_buf, sizeof(prob_str_buf), "%20.2f", prob);
1596       prob_str = prob_str_buf;
1597     }
1598     C->log()->elem("branch target_bci='%d' taken='%d' not_taken='%d' cnt='%f' prob='%s'",
1599                    iter().get_dest(), taken, not_taken, cnt, prob_str);
1600   }
1601   return prob;
1602 }
1603 
1604 //-----------------------------branch_prediction-------------------------------
1605 float Parse::branch_prediction(float& cnt,
1606                                BoolTest::mask btest,
1607                                int target_bci,
1608                                Node* test) {
1609   float prob = dynamic_branch_prediction(cnt, btest, test);
1610   // If prob is unknown, switch to static prediction
1611   if (prob != PROB_UNKNOWN)  return prob;
1612 
1613   prob = PROB_FAIR;                   // Set default value
1614   if (btest == BoolTest::eq)          // Exactly equal test?
1615     prob = PROB_STATIC_INFREQUENT;    // Assume its relatively infrequent
1616   else if (btest == BoolTest::ne)
1617     prob = PROB_STATIC_FREQUENT;      // Assume its relatively frequent
1618 
1619   // If this is a conditional test guarding a backwards branch,
1620   // assume its a loop-back edge.  Make it a likely taken branch.
1621   if (target_bci < bci()) {
1622     if (is_osr_parse()) {    // Could be a hot OSR'd loop; force deopt
1623       // Since it's an OSR, we probably have profile data, but since
1624       // branch_prediction returned PROB_UNKNOWN, the counts are too small.
1625       // Let's make a special check here for completely zero counts.
1626       ciMethodData* methodData = method()->method_data();
1627       if (!methodData->is_empty()) {
1628         ciProfileData* data = methodData->bci_to_data(bci());
1629         // Only stop for truly zero counts, which mean an unknown part
1630         // of the OSR-ed method, and we want to deopt to gather more stats.
1631         // If you have ANY counts, then this loop is simply 'cold' relative
1632         // to the OSR loop.
1633         if (data == NULL ||
1634             (data->as_BranchData()->taken() +  data->as_BranchData()->not_taken() == 0)) {
1635           // This is the only way to return PROB_UNKNOWN:
1636           return PROB_UNKNOWN;
1637         }
1638       }
1639     }
1640     prob = PROB_STATIC_FREQUENT;     // Likely to take backwards branch
1641   }
1642 
1643   assert(prob != PROB_UNKNOWN, "must have some guess at this point");
1644   return prob;
1645 }
1646 
1647 // The magic constants are chosen so as to match the output of
1648 // branch_prediction() when the profile reports a zero taken count.
1649 // It is important to distinguish zero counts unambiguously, because
1650 // some branches (e.g., _213_javac.Assembler.eliminate) validly produce
1651 // very small but nonzero probabilities, which if confused with zero
1652 // counts would keep the program recompiling indefinitely.
1653 bool Parse::seems_never_taken(float prob) const {
1654   return prob < PROB_MIN;
1655 }
1656 
1657 // True if the comparison seems to be the kind that will not change its
1658 // statistics from true to false.  See comments in adjust_map_after_if.
1659 // This question is only asked along paths which are already
1660 // classifed as untaken (by seems_never_taken), so really,
1661 // if a path is never taken, its controlling comparison is
1662 // already acting in a stable fashion.  If the comparison
1663 // seems stable, we will put an expensive uncommon trap
1664 // on the untaken path.
1665 bool Parse::seems_stable_comparison() const {
1666   if (C->too_many_traps(method(), bci(), Deoptimization::Reason_unstable_if)) {
1667     return false;
1668   }
1669   return true;
1670 }
1671 
1672 //-------------------------------repush_if_args--------------------------------
1673 // Push arguments of an "if" bytecode back onto the stack by adjusting _sp.
1674 inline int Parse::repush_if_args() {
1675   if (PrintOpto && WizardMode) {
1676     tty->print("defending against excessive implicit null exceptions on %s @%d in ",
1677                Bytecodes::name(iter().cur_bc()), iter().cur_bci());
1678     method()->print_name(); tty->cr();
1679   }
1680   int bc_depth = - Bytecodes::depth(iter().cur_bc());
1681   assert(bc_depth == 1 || bc_depth == 2, "only two kinds of branches");
1682   DEBUG_ONLY(sync_jvms());   // argument(n) requires a synced jvms
1683   assert(argument(0) != NULL, "must exist");
1684   assert(bc_depth == 1 || argument(1) != NULL, "two must exist");
1685   inc_sp(bc_depth);
1686   return bc_depth;
1687 }
1688 
1689 //----------------------------------do_ifnull----------------------------------
1690 void Parse::do_ifnull(BoolTest::mask btest, Node *c) {
1691   int target_bci = iter().get_dest();
1692 
1693   Block* branch_block = successor_for_bci(target_bci);
1694   Block* next_block   = successor_for_bci(iter().next_bci());
1695 
1696   float cnt;
1697   float prob = branch_prediction(cnt, btest, target_bci, c);
1698   if (prob == PROB_UNKNOWN) {
1699     // (An earlier version of do_ifnull omitted this trap for OSR methods.)
1700     if (PrintOpto && Verbose) {
1701       tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1702     }
1703     repush_if_args(); // to gather stats on loop
1704     // We need to mark this branch as taken so that if we recompile we will
1705     // see that it is possible. In the tiered system the interpreter doesn't
1706     // do profiling and by the time we get to the lower tier from the interpreter
1707     // the path may be cold again. Make sure it doesn't look untaken
1708     profile_taken_branch(target_bci, !ProfileInterpreter);
1709     uncommon_trap(Deoptimization::Reason_unreached,
1710                   Deoptimization::Action_reinterpret,
1711                   NULL, "cold");
1712     if (C->eliminate_boxing()) {
1713       // Mark the successor blocks as parsed
1714       branch_block->next_path_num();
1715       next_block->next_path_num();
1716     }
1717     return;
1718   }
1719 
1720   NOT_PRODUCT(explicit_null_checks_inserted++);
1721 
1722   // Generate real control flow
1723   Node   *tst = _gvn.transform( new BoolNode( c, btest ) );
1724 
1725   // Sanity check the probability value
1726   assert(prob > 0.0f,"Bad probability in Parser");
1727  // Need xform to put node in hash table
1728   IfNode *iff = create_and_xform_if( control(), tst, prob, cnt );
1729   assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1730   // True branch
1731   { PreserveJVMState pjvms(this);
1732     Node* iftrue  = _gvn.transform( new IfTrueNode (iff) );
1733     set_control(iftrue);
1734 
1735     if (stopped()) {            // Path is dead?
1736       NOT_PRODUCT(explicit_null_checks_elided++);
1737       if (C->eliminate_boxing()) {
1738         // Mark the successor block as parsed
1739         branch_block->next_path_num();
1740       }
1741     } else {                    // Path is live.
1742       // Update method data
1743       profile_taken_branch(target_bci);
1744       adjust_map_after_if(btest, c, prob, branch_block);
1745       if (!stopped()) {
1746         merge(target_bci);
1747       }
1748     }
1749   }
1750 
1751   // False branch
1752   Node* iffalse = _gvn.transform( new IfFalseNode(iff) );
1753   set_control(iffalse);
1754 
1755   if (stopped()) {              // Path is dead?
1756     NOT_PRODUCT(explicit_null_checks_elided++);
1757     if (C->eliminate_boxing()) {
1758       // Mark the successor block as parsed
1759       next_block->next_path_num();
1760     }
1761   } else  {                     // Path is live.
1762     // Update method data
1763     profile_not_taken_branch();
1764     adjust_map_after_if(BoolTest(btest).negate(), c, 1.0-prob, next_block);
1765   }
1766 }
1767 
1768 //------------------------------------do_if------------------------------------
1769 void Parse::do_if(BoolTest::mask btest, Node* c, bool new_path, Node** ctrl_taken) {
1770   int target_bci = iter().get_dest();
1771 
1772   Block* branch_block = successor_for_bci(target_bci);
1773   Block* next_block   = successor_for_bci(iter().next_bci());
1774 
1775   float cnt;
1776   float prob = branch_prediction(cnt, btest, target_bci, c);
1777   float untaken_prob = 1.0 - prob;
1778 
1779   if (prob == PROB_UNKNOWN) {
1780     if (PrintOpto && Verbose) {
1781       tty->print_cr("Never-taken edge stops compilation at bci %d", bci());
1782     }
1783     repush_if_args(); // to gather stats on loop
1784     // We need to mark this branch as taken so that if we recompile we will
1785     // see that it is possible. In the tiered system the interpreter doesn't
1786     // do profiling and by the time we get to the lower tier from the interpreter
1787     // the path may be cold again. Make sure it doesn't look untaken
1788     profile_taken_branch(target_bci, !ProfileInterpreter);
1789     uncommon_trap(Deoptimization::Reason_unreached,
1790                   Deoptimization::Action_reinterpret,
1791                   NULL, "cold");
1792     if (C->eliminate_boxing()) {
1793       // Mark the successor blocks as parsed
1794       branch_block->next_path_num();
1795       next_block->next_path_num();
1796     }
1797     return;
1798   }
1799 
1800   // Sanity check the probability value
1801   assert(0.0f < prob && prob < 1.0f,"Bad probability in Parser");
1802 
1803   bool taken_if_true = true;
1804   // Convert BoolTest to canonical form:
1805   if (!BoolTest(btest).is_canonical()) {
1806     btest         = BoolTest(btest).negate();
1807     taken_if_true = false;
1808     // prob is NOT updated here; it remains the probability of the taken
1809     // path (as opposed to the prob of the path guarded by an 'IfTrueNode').
1810   }
1811   assert(btest != BoolTest::eq, "!= is the only canonical exact test");
1812 
1813   Node* tst0 = new BoolNode(c, btest);
1814   Node* tst = _gvn.transform(tst0);
1815   BoolTest::mask taken_btest   = BoolTest::illegal;
1816   BoolTest::mask untaken_btest = BoolTest::illegal;
1817 
1818   if (tst->is_Bool()) {
1819     // Refresh c from the transformed bool node, since it may be
1820     // simpler than the original c.  Also re-canonicalize btest.
1821     // This wins when (Bool ne (Conv2B p) 0) => (Bool ne (CmpP p NULL)).
1822     // That can arise from statements like: if (x instanceof C) ...
1823     if (tst != tst0) {
1824       // Canonicalize one more time since transform can change it.
1825       btest = tst->as_Bool()->_test._test;
1826       if (!BoolTest(btest).is_canonical()) {
1827         // Reverse edges one more time...
1828         tst   = _gvn.transform( tst->as_Bool()->negate(&_gvn) );
1829         btest = tst->as_Bool()->_test._test;
1830         assert(BoolTest(btest).is_canonical(), "sanity");
1831         taken_if_true = !taken_if_true;
1832       }
1833       c = tst->in(1);
1834     }
1835     BoolTest::mask neg_btest = BoolTest(btest).negate();
1836     taken_btest   = taken_if_true ?     btest : neg_btest;
1837     untaken_btest = taken_if_true ? neg_btest :     btest;
1838   }
1839 
1840   // Generate real control flow
1841   float true_prob = (taken_if_true ? prob : untaken_prob);
1842   IfNode* iff = create_and_map_if(control(), tst, true_prob, cnt);
1843   assert(iff->_prob > 0.0f,"Optimizer made bad probability in parser");
1844   Node* taken_branch   = new IfTrueNode(iff);
1845   Node* untaken_branch = new IfFalseNode(iff);
1846   if (!taken_if_true) {  // Finish conversion to canonical form
1847     Node* tmp      = taken_branch;
1848     taken_branch   = untaken_branch;
1849     untaken_branch = tmp;
1850   }
1851 
1852   // Branch is taken:
1853   { PreserveJVMState pjvms(this);
1854     taken_branch = _gvn.transform(taken_branch);
1855     set_control(taken_branch);
1856 
1857     if (stopped()) {
1858       if (C->eliminate_boxing() && !new_path) {
1859         // Mark the successor block as parsed (if we haven't created a new path)
1860         branch_block->next_path_num();
1861       }
1862     } else {
1863       // Update method data
1864       profile_taken_branch(target_bci);
1865       adjust_map_after_if(taken_btest, c, prob, branch_block);
1866       if (!stopped()) {
1867         if (new_path) {
1868           // Merge by using a new path
1869           merge_new_path(target_bci);
1870         } else if (ctrl_taken != NULL) {
1871           // Don't merge but save taken branch to be wired by caller
1872           *ctrl_taken = control();
1873         } else {
1874           merge(target_bci);
1875         }
1876       }
1877     }
1878   }
1879 
1880   untaken_branch = _gvn.transform(untaken_branch);
1881   set_control(untaken_branch);
1882 
1883   // Branch not taken.
1884   if (stopped() && ctrl_taken == NULL) {
1885     if (C->eliminate_boxing()) {
1886       // Mark the successor block as parsed (if caller does not re-wire control flow)
1887       next_block->next_path_num();
1888     }
1889   } else {
1890     // Update method data
1891     profile_not_taken_branch();
1892     adjust_map_after_if(untaken_btest, c, untaken_prob, next_block);
1893   }
1894 }
1895 
1896 void Parse::do_acmp(BoolTest::mask btest, Node* a, Node* b) {
1897   ciMethod* subst_method = ciEnv::current()->ValueBootstrapMethods_klass()->find_method(ciSymbol::isSubstitutable_name(), ciSymbol::object_object_boolean_signature());
1898   // If current method is ValueBootstrapMethods::isSubstitutable(),
1899   // compile the acmp as a regular pointer comparison otherwise we
1900   // could call ValueBootstrapMethods::isSubstitutable() back
1901   if (!EnableValhalla || (method() == subst_method)) {
1902     Node* cmp = CmpP(a, b);
1903     cmp = optimize_cmp_with_klass(cmp);
1904     do_if(btest, cmp);
1905     return;
1906   }
1907 
1908   // Substitutability test
1909   if (a->is_ValueType()) {
1910     inc_sp(2);
1911     a = a->as_ValueType()->allocate(this, true)->get_oop();
1912     dec_sp(2);
1913   }
1914   if (b->is_ValueType()) {
1915     inc_sp(2);
1916     b = b->as_ValueType()->allocate(this, true)->get_oop();
1917     dec_sp(2);
1918   }
1919 
1920   const TypeOopPtr* ta = _gvn.type(a)->isa_oopptr();
1921   const TypeOopPtr* tb = _gvn.type(b)->isa_oopptr();
1922 
1923   if (ta == NULL || !ta->can_be_value_type_raw() ||
1924       tb == NULL || !tb->can_be_value_type_raw()) {
1925     Node* cmp = CmpP(a, b);
1926     cmp = optimize_cmp_with_klass(cmp);
1927     do_if(btest, cmp);
1928     return;
1929   }
1930 
1931   Node* cmp = CmpP(a, b);
1932   cmp = optimize_cmp_with_klass(cmp);
1933   Node* eq_region = NULL;
1934   if (btest == BoolTest::eq) {
1935     do_if(btest, cmp, true);
1936     if (stopped()) {
1937       return;
1938     }
1939   } else {
1940     assert(btest == BoolTest::ne, "only eq or ne");
1941     Node* is_not_equal = NULL;
1942     eq_region = new RegionNode(3);
1943     {
1944       PreserveJVMState pjvms(this);
1945       do_if(btest, cmp, false, &is_not_equal);
1946       if (!stopped()) {
1947         eq_region->init_req(1, control());
1948       }
1949     }
1950     if (is_not_equal == NULL || is_not_equal->is_top()) {
1951       record_for_igvn(eq_region);
1952       set_control(_gvn.transform(eq_region));
1953       return;
1954     }
1955     set_control(is_not_equal);
1956   }
1957   // Pointers not equal, check for values
1958   Node* ne_region = new RegionNode(6);
1959   inc_sp(2);
1960   Node* null_ctl = top();
1961   Node* not_null_a = null_check_oop(a, &null_ctl, !too_many_traps(Deoptimization::Reason_null_check), false, false);
1962   dec_sp(2);
1963   ne_region->init_req(1, null_ctl);
1964   if (stopped()) {
1965     record_for_igvn(ne_region);
1966     set_control(_gvn.transform(ne_region));
1967     if (btest == BoolTest::ne) {
1968       {
1969         PreserveJVMState pjvms(this);
1970         int target_bci = iter().get_dest();
1971         merge(target_bci);
1972       }
1973       record_for_igvn(eq_region);
1974       set_control(_gvn.transform(eq_region));
1975     }
1976     return;
1977   }
1978 
1979   Node* is_value = is_always_locked(not_null_a);
1980   Node* value_mask = _gvn.MakeConX(markOopDesc::always_locked_pattern);
1981   Node* is_value_cmp = _gvn.transform(new CmpXNode(is_value, value_mask));
1982   Node* is_value_bol = _gvn.transform(new BoolNode(is_value_cmp, BoolTest::ne));
1983   IfNode* is_value_iff = create_and_map_if(control(), is_value_bol, PROB_FAIR, COUNT_UNKNOWN);
1984   Node* not_value = _gvn.transform(new IfTrueNode(is_value_iff));
1985   set_control(_gvn.transform(new IfFalseNode(is_value_iff)));
1986   ne_region->init_req(2, not_value);
1987 
1988   // One of the 2 pointers refers to a value, check if both are of
1989   // the same class
1990   inc_sp(2);
1991   null_ctl = top();
1992   Node* not_null_b = null_check_oop(b, &null_ctl, !too_many_traps(Deoptimization::Reason_null_check), false, false);
1993   dec_sp(2);
1994   ne_region->init_req(3, null_ctl);
1995   if (stopped()) {
1996     record_for_igvn(ne_region);
1997     set_control(_gvn.transform(ne_region));
1998     if (btest == BoolTest::ne) {
1999       {
2000         PreserveJVMState pjvms(this);
2001         int target_bci = iter().get_dest();
2002         merge(target_bci);
2003       }
2004       record_for_igvn(eq_region);
2005       set_control(_gvn.transform(eq_region));
2006     }
2007     return;
2008   }
2009   Node* kls_a = load_object_klass(not_null_a);
2010   Node* kls_b = load_object_klass(not_null_b);
2011   Node* kls_cmp = CmpP(kls_a, kls_b);
2012   Node* kls_bol = _gvn.transform(new BoolNode(kls_cmp, BoolTest::ne));
2013   IfNode* kls_iff = create_and_map_if(control(), kls_bol, PROB_FAIR, COUNT_UNKNOWN);
2014   Node* kls_ne = _gvn.transform(new IfTrueNode(kls_iff));
2015   set_control(_gvn.transform(new IfFalseNode(kls_iff)));
2016   ne_region->init_req(4, kls_ne);
2017 
2018   if (stopped()) {
2019     record_for_igvn(ne_region);
2020     set_control(_gvn.transform(ne_region));
2021     if (btest == BoolTest::ne) {
2022       {
2023         PreserveJVMState pjvms(this);
2024         int target_bci = iter().get_dest();
2025         merge(target_bci);
2026       }
2027       record_for_igvn(eq_region);
2028       set_control(_gvn.transform(eq_region));
2029     }
2030     return;
2031   }
2032   // Both are values of the same class, we need to perform a
2033   // substitutability test. Delegate to
2034   // ValueBootstrapMethods::isSubstitutable().
2035 
2036   Node* ne_io_phi = PhiNode::make(ne_region, i_o());
2037   Node* mem = reset_memory();
2038   Node* ne_mem_phi = PhiNode::make(ne_region, mem);
2039 
2040   Node* eq_io_phi = NULL;
2041   Node* eq_mem_phi = NULL;
2042   if (eq_region != NULL) {
2043     eq_io_phi = PhiNode::make(eq_region, i_o());
2044     eq_mem_phi = PhiNode::make(eq_region, mem);
2045   }
2046 
2047   set_all_memory(mem);
2048 
2049   kill_dead_locals();
2050   CallStaticJavaNode *call = new CallStaticJavaNode(C, TypeFunc::make(subst_method), SharedRuntime::get_resolve_static_call_stub(), subst_method, bci());
2051   call->set_override_symbolic_info(true);
2052   call->init_req(TypeFunc::Parms, not_null_a);
2053   call->init_req(TypeFunc::Parms+1, not_null_b);
2054   inc_sp(2);
2055   set_edges_for_java_call(call, false, false);
2056   Node* ret = set_results_for_java_call(call, false, true);
2057   dec_sp(2);
2058 
2059   // Test the return value of ValueBootstrapMethods::isSubstitutable()
2060   Node* subst_cmp = _gvn.transform(new CmpINode(ret, intcon(1)));
2061   Node* ctl = C->top();
2062   if (btest == BoolTest::eq) {
2063     PreserveJVMState pjvms(this);
2064     do_if(btest, subst_cmp);
2065     if (!stopped()) {
2066       ctl = control();
2067     }
2068   } else {
2069     assert(btest == BoolTest::ne, "only eq or ne");
2070     PreserveJVMState pjvms(this);
2071     do_if(btest, subst_cmp, false, &ctl);
2072     if (!stopped()) {
2073       eq_region->init_req(2, control());
2074       eq_io_phi->init_req(2, i_o());
2075       eq_mem_phi->init_req(2, reset_memory());
2076     }
2077   }
2078   ne_region->init_req(5, ctl);
2079   ne_io_phi->init_req(5, i_o());
2080   ne_mem_phi->init_req(5, reset_memory());
2081 
2082   record_for_igvn(ne_region);
2083   set_control(_gvn.transform(ne_region));
2084   set_i_o(_gvn.transform(ne_io_phi));
2085   set_all_memory(_gvn.transform(ne_mem_phi));
2086 
2087   if (btest == BoolTest::ne) {
2088     {
2089       PreserveJVMState pjvms(this);
2090       int target_bci = iter().get_dest();
2091       merge(target_bci);
2092     }
2093 
2094     record_for_igvn(eq_region);
2095     set_control(_gvn.transform(eq_region));
2096     set_i_o(_gvn.transform(eq_io_phi));
2097     set_all_memory(_gvn.transform(eq_mem_phi));
2098   }
2099 }
2100 
2101 bool Parse::path_is_suitable_for_uncommon_trap(float prob) const {
2102   // Don't want to speculate on uncommon traps when running with -Xcomp
2103   if (!UseInterpreter) {
2104     return false;
2105   }
2106   return (seems_never_taken(prob) && seems_stable_comparison());
2107 }
2108 
2109 void Parse::maybe_add_predicate_after_if(Block* path) {
2110   if (path->is_SEL_head() && path->preds_parsed() == 0) {
2111     // Add predicates at bci of if dominating the loop so traps can be
2112     // recorded on the if's profile data
2113     int bc_depth = repush_if_args();
2114     add_predicate();
2115     dec_sp(bc_depth);
2116     path->set_has_predicates();
2117   }
2118 }
2119 
2120 
2121 //----------------------------adjust_map_after_if------------------------------
2122 // Adjust the JVM state to reflect the result of taking this path.
2123 // Basically, it means inspecting the CmpNode controlling this
2124 // branch, seeing how it constrains a tested value, and then
2125 // deciding if it's worth our while to encode this constraint
2126 // as graph nodes in the current abstract interpretation map.
2127 void Parse::adjust_map_after_if(BoolTest::mask btest, Node* c, float prob, Block* path) {
2128   if (!c->is_Cmp()) {
2129     maybe_add_predicate_after_if(path);
2130     return;
2131   }
2132 
2133   if (stopped() || btest == BoolTest::illegal) {
2134     return;                             // nothing to do
2135   }
2136 
2137   bool is_fallthrough = (path == successor_for_bci(iter().next_bci()));
2138 
2139   if (path_is_suitable_for_uncommon_trap(prob)) {
2140     repush_if_args();
2141     uncommon_trap(Deoptimization::Reason_unstable_if,
2142                   Deoptimization::Action_reinterpret,
2143                   NULL,
2144                   (is_fallthrough ? "taken always" : "taken never"));
2145     return;
2146   }
2147 
2148   Node* val = c->in(1);
2149   Node* con = c->in(2);
2150   const Type* tcon = _gvn.type(con);
2151   const Type* tval = _gvn.type(val);
2152   bool have_con = tcon->singleton();
2153   if (tval->singleton()) {
2154     if (!have_con) {
2155       // Swap, so constant is in con.
2156       con  = val;
2157       tcon = tval;
2158       val  = c->in(2);
2159       tval = _gvn.type(val);
2160       btest = BoolTest(btest).commute();
2161       have_con = true;
2162     } else {
2163       // Do we have two constants?  Then leave well enough alone.
2164       have_con = false;
2165     }
2166   }
2167   if (!have_con) {                        // remaining adjustments need a con
2168     maybe_add_predicate_after_if(path);
2169     return;
2170   }
2171 
2172   sharpen_type_after_if(btest, con, tcon, val, tval);
2173   maybe_add_predicate_after_if(path);
2174 }
2175 
2176 
2177 static Node* extract_obj_from_klass_load(PhaseGVN* gvn, Node* n) {
2178   Node* ldk;
2179   if (n->is_DecodeNKlass()) {
2180     if (n->in(1)->Opcode() != Op_LoadNKlass) {
2181       return NULL;
2182     } else {
2183       ldk = n->in(1);
2184     }
2185   } else if (n->Opcode() != Op_LoadKlass) {
2186     return NULL;
2187   } else {
2188     ldk = n;
2189   }
2190   assert(ldk != NULL && ldk->is_Load(), "should have found a LoadKlass or LoadNKlass node");
2191 
2192   Node* adr = ldk->in(MemNode::Address);
2193   intptr_t off = 0;
2194   Node* obj = AddPNode::Ideal_base_and_offset(adr, gvn, off);
2195   if (obj == NULL || off != oopDesc::klass_offset_in_bytes()) // loading oopDesc::_klass?
2196     return NULL;
2197   const TypePtr* tp = gvn->type(obj)->is_ptr();
2198   if (tp == NULL || !(tp->isa_instptr() || tp->isa_aryptr())) // is obj a Java object ptr?
2199     return NULL;
2200 
2201   return obj;
2202 }
2203 
2204 void Parse::sharpen_type_after_if(BoolTest::mask btest,
2205                                   Node* con, const Type* tcon,
2206                                   Node* val, const Type* tval) {
2207   // Look for opportunities to sharpen the type of a node
2208   // whose klass is compared with a constant klass.
2209   if (btest == BoolTest::eq && tcon->isa_klassptr()) {
2210     Node* obj = extract_obj_from_klass_load(&_gvn, val);
2211     const TypeOopPtr* con_type = tcon->isa_klassptr()->as_instance_type();
2212     if (obj != NULL && (con_type->isa_instptr() || con_type->isa_aryptr())) {
2213        // Found:
2214        //   Bool(CmpP(LoadKlass(obj._klass), ConP(Foo.klass)), [eq])
2215        // or the narrowOop equivalent.
2216        const Type* obj_type = _gvn.type(obj);
2217        const TypeOopPtr* tboth = obj_type->join_speculative(con_type)->isa_oopptr();
2218        if (tboth != NULL && tboth->klass_is_exact() && tboth != obj_type &&
2219            tboth->higher_equal(obj_type)) {
2220           // obj has to be of the exact type Foo if the CmpP succeeds.
2221           int obj_in_map = map()->find_edge(obj);
2222           JVMState* jvms = this->jvms();
2223           if (obj_in_map >= 0 &&
2224               (jvms->is_loc(obj_in_map) || jvms->is_stk(obj_in_map))) {
2225             TypeNode* ccast = new CheckCastPPNode(control(), obj, tboth);
2226             const Type* tcc = ccast->as_Type()->type();
2227             assert(tcc != obj_type && tcc->higher_equal(obj_type), "must improve");
2228             // Delay transform() call to allow recovery of pre-cast value
2229             // at the control merge.
2230             _gvn.set_type_bottom(ccast);
2231             record_for_igvn(ccast);
2232             // Here's the payoff.
2233             replace_in_map(obj, ccast);
2234           }
2235        }
2236     }
2237   }
2238 
2239   int val_in_map = map()->find_edge(val);
2240   if (val_in_map < 0)  return;          // replace_in_map would be useless
2241   {
2242     JVMState* jvms = this->jvms();
2243     if (!(jvms->is_loc(val_in_map) ||
2244           jvms->is_stk(val_in_map)))
2245       return;                           // again, it would be useless
2246   }
2247 
2248   // Check for a comparison to a constant, and "know" that the compared
2249   // value is constrained on this path.
2250   assert(tcon->singleton(), "");
2251   ConstraintCastNode* ccast = NULL;
2252   Node* cast = NULL;
2253 
2254   switch (btest) {
2255   case BoolTest::eq:                    // Constant test?
2256     {
2257       const Type* tboth = tcon->join_speculative(tval);
2258       if (tboth == tval)  break;        // Nothing to gain.
2259       if (tcon->isa_int()) {
2260         ccast = new CastIINode(val, tboth);
2261       } else if (tcon == TypePtr::NULL_PTR) {
2262         // Cast to null, but keep the pointer identity temporarily live.
2263         ccast = new CastPPNode(val, tboth);
2264       } else {
2265         const TypeF* tf = tcon->isa_float_constant();
2266         const TypeD* td = tcon->isa_double_constant();
2267         // Exclude tests vs float/double 0 as these could be
2268         // either +0 or -0.  Just because you are equal to +0
2269         // doesn't mean you ARE +0!
2270         // Note, following code also replaces Long and Oop values.
2271         if ((!tf || tf->_f != 0.0) &&
2272             (!td || td->_d != 0.0))
2273           cast = con;                   // Replace non-constant val by con.
2274       }
2275     }
2276     break;
2277 
2278   case BoolTest::ne:
2279     if (tcon == TypePtr::NULL_PTR) {
2280       cast = cast_not_null(val, false);
2281     }
2282     break;
2283 
2284   default:
2285     // (At this point we could record int range types with CastII.)
2286     break;
2287   }
2288 
2289   if (ccast != NULL) {
2290     const Type* tcc = ccast->as_Type()->type();
2291     assert(tcc != tval && tcc->higher_equal(tval), "must improve");
2292     // Delay transform() call to allow recovery of pre-cast value
2293     // at the control merge.
2294     ccast->set_req(0, control());
2295     _gvn.set_type_bottom(ccast);
2296     record_for_igvn(ccast);
2297     cast = ccast;
2298   }
2299 
2300   if (cast != NULL) {                   // Here's the payoff.
2301     replace_in_map(val, cast);
2302   }
2303 }
2304 
2305 /**
2306  * Use speculative type to optimize CmpP node: if comparison is
2307  * against the low level class, cast the object to the speculative
2308  * type if any. CmpP should then go away.
2309  *
2310  * @param c  expected CmpP node
2311  * @return   result of CmpP on object casted to speculative type
2312  *
2313  */
2314 Node* Parse::optimize_cmp_with_klass(Node* c) {
2315   // If this is transformed by the _gvn to a comparison with the low
2316   // level klass then we may be able to use speculation
2317   if (c->Opcode() == Op_CmpP &&
2318       (c->in(1)->Opcode() == Op_LoadKlass || c->in(1)->Opcode() == Op_DecodeNKlass) &&
2319       c->in(2)->is_Con()) {
2320     Node* load_klass = NULL;
2321     Node* decode = NULL;
2322     if (c->in(1)->Opcode() == Op_DecodeNKlass) {
2323       decode = c->in(1);
2324       load_klass = c->in(1)->in(1);
2325     } else {
2326       load_klass = c->in(1);
2327     }
2328     if (load_klass->in(2)->is_AddP()) {
2329       Node* addp = load_klass->in(2);
2330       Node* obj = addp->in(AddPNode::Address);
2331       const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
2332       if (obj_type->speculative_type_not_null() != NULL) {
2333         ciKlass* k = obj_type->speculative_type();
2334         inc_sp(2);
2335         obj = maybe_cast_profiled_obj(obj, k);
2336         dec_sp(2);
2337         if (obj->is_ValueType()) {
2338           assert(obj->as_ValueType()->is_allocated(&_gvn), "must be allocated");
2339           obj = obj->as_ValueType()->get_oop();
2340         }
2341         // Make the CmpP use the casted obj
2342         addp = basic_plus_adr(obj, addp->in(AddPNode::Offset));
2343         load_klass = load_klass->clone();
2344         load_klass->set_req(2, addp);
2345         load_klass = _gvn.transform(load_klass);
2346         if (decode != NULL) {
2347           decode = decode->clone();
2348           decode->set_req(1, load_klass);
2349           load_klass = _gvn.transform(decode);
2350         }
2351         c = c->clone();
2352         c->set_req(1, load_klass);
2353         c = _gvn.transform(c);
2354       }
2355     }
2356   }
2357   return c;
2358 }
2359 
2360 //------------------------------do_one_bytecode--------------------------------
2361 // Parse this bytecode, and alter the Parsers JVM->Node mapping
2362 void Parse::do_one_bytecode() {
2363   Node *a, *b, *c, *d;          // Handy temps
2364   BoolTest::mask btest;
2365   int i;
2366 
2367   assert(!has_exceptions(), "bytecode entry state must be clear of throws");
2368 
2369   if (C->check_node_count(NodeLimitFudgeFactor * 5,
2370                           "out of nodes parsing method")) {
2371     return;
2372   }
2373 
2374 #ifdef ASSERT
2375   // for setting breakpoints
2376   if (TraceOptoParse) {
2377     tty->print(" @");
2378     dump_bci(bci());
2379     tty->cr();
2380   }
2381 #endif
2382 
2383   switch (bc()) {
2384   case Bytecodes::_nop:
2385     // do nothing
2386     break;
2387   case Bytecodes::_lconst_0:
2388     push_pair(longcon(0));
2389     break;
2390 
2391   case Bytecodes::_lconst_1:
2392     push_pair(longcon(1));
2393     break;
2394 
2395   case Bytecodes::_fconst_0:
2396     push(zerocon(T_FLOAT));
2397     break;
2398 
2399   case Bytecodes::_fconst_1:
2400     push(makecon(TypeF::ONE));
2401     break;
2402 
2403   case Bytecodes::_fconst_2:
2404     push(makecon(TypeF::make(2.0f)));
2405     break;
2406 
2407   case Bytecodes::_dconst_0:
2408     push_pair(zerocon(T_DOUBLE));
2409     break;
2410 
2411   case Bytecodes::_dconst_1:
2412     push_pair(makecon(TypeD::ONE));
2413     break;
2414 
2415   case Bytecodes::_iconst_m1:push(intcon(-1)); break;
2416   case Bytecodes::_iconst_0: push(intcon( 0)); break;
2417   case Bytecodes::_iconst_1: push(intcon( 1)); break;
2418   case Bytecodes::_iconst_2: push(intcon( 2)); break;
2419   case Bytecodes::_iconst_3: push(intcon( 3)); break;
2420   case Bytecodes::_iconst_4: push(intcon( 4)); break;
2421   case Bytecodes::_iconst_5: push(intcon( 5)); break;
2422   case Bytecodes::_bipush:   push(intcon(iter().get_constant_u1())); break;
2423   case Bytecodes::_sipush:   push(intcon(iter().get_constant_u2())); break;
2424   case Bytecodes::_aconst_null: push(null());  break;
2425   case Bytecodes::_ldc:
2426   case Bytecodes::_ldc_w:
2427   case Bytecodes::_ldc2_w:
2428     // If the constant is unresolved, run this BC once in the interpreter.
2429     {
2430       ciConstant constant = iter().get_constant();
2431       if (!constant.is_valid() ||
2432           (constant.basic_type() == T_OBJECT &&
2433            !constant.as_object()->is_loaded())) {
2434         int index = iter().get_constant_pool_index();
2435         constantTag tag = iter().get_constant_pool_tag(index);
2436         uncommon_trap(Deoptimization::make_trap_request
2437                       (Deoptimization::Reason_unloaded,
2438                        Deoptimization::Action_reinterpret,
2439                        index),
2440                       NULL, tag.internal_name());
2441         break;
2442       }
2443       assert(constant.basic_type() != T_OBJECT || constant.as_object()->is_instance(),
2444              "must be java_mirror of klass");
2445       const Type* con_type = Type::make_from_constant(constant);
2446       if (con_type != NULL) {
2447         push_node(con_type->basic_type(), makecon(con_type));
2448       }
2449     }
2450 
2451     break;
2452 
2453   case Bytecodes::_aload_0:
2454     push( local(0) );
2455     break;
2456   case Bytecodes::_aload_1:
2457     push( local(1) );
2458     break;
2459   case Bytecodes::_aload_2:
2460     push( local(2) );
2461     break;
2462   case Bytecodes::_aload_3:
2463     push( local(3) );
2464     break;
2465   case Bytecodes::_aload:
2466     push( local(iter().get_index()) );
2467     break;
2468 
2469   case Bytecodes::_fload_0:
2470   case Bytecodes::_iload_0:
2471     push( local(0) );
2472     break;
2473   case Bytecodes::_fload_1:
2474   case Bytecodes::_iload_1:
2475     push( local(1) );
2476     break;
2477   case Bytecodes::_fload_2:
2478   case Bytecodes::_iload_2:
2479     push( local(2) );
2480     break;
2481   case Bytecodes::_fload_3:
2482   case Bytecodes::_iload_3:
2483     push( local(3) );
2484     break;
2485   case Bytecodes::_fload:
2486   case Bytecodes::_iload:
2487     push( local(iter().get_index()) );
2488     break;
2489   case Bytecodes::_lload_0:
2490     push_pair_local( 0 );
2491     break;
2492   case Bytecodes::_lload_1:
2493     push_pair_local( 1 );
2494     break;
2495   case Bytecodes::_lload_2:
2496     push_pair_local( 2 );
2497     break;
2498   case Bytecodes::_lload_3:
2499     push_pair_local( 3 );
2500     break;
2501   case Bytecodes::_lload:
2502     push_pair_local( iter().get_index() );
2503     break;
2504 
2505   case Bytecodes::_dload_0:
2506     push_pair_local(0);
2507     break;
2508   case Bytecodes::_dload_1:
2509     push_pair_local(1);
2510     break;
2511   case Bytecodes::_dload_2:
2512     push_pair_local(2);
2513     break;
2514   case Bytecodes::_dload_3:
2515     push_pair_local(3);
2516     break;
2517   case Bytecodes::_dload:
2518     push_pair_local(iter().get_index());
2519     break;
2520   case Bytecodes::_fstore_0:
2521   case Bytecodes::_istore_0:
2522   case Bytecodes::_astore_0:
2523     set_local( 0, pop() );
2524     break;
2525   case Bytecodes::_fstore_1:
2526   case Bytecodes::_istore_1:
2527   case Bytecodes::_astore_1:
2528     set_local( 1, pop() );
2529     break;
2530   case Bytecodes::_fstore_2:
2531   case Bytecodes::_istore_2:
2532   case Bytecodes::_astore_2:
2533     set_local( 2, pop() );
2534     break;
2535   case Bytecodes::_fstore_3:
2536   case Bytecodes::_istore_3:
2537   case Bytecodes::_astore_3:
2538     set_local( 3, pop() );
2539     break;
2540   case Bytecodes::_fstore:
2541   case Bytecodes::_istore:
2542   case Bytecodes::_astore:
2543     set_local( iter().get_index(), pop() );
2544     break;
2545   // long stores
2546   case Bytecodes::_lstore_0:
2547     set_pair_local( 0, pop_pair() );
2548     break;
2549   case Bytecodes::_lstore_1:
2550     set_pair_local( 1, pop_pair() );
2551     break;
2552   case Bytecodes::_lstore_2:
2553     set_pair_local( 2, pop_pair() );
2554     break;
2555   case Bytecodes::_lstore_3:
2556     set_pair_local( 3, pop_pair() );
2557     break;
2558   case Bytecodes::_lstore:
2559     set_pair_local( iter().get_index(), pop_pair() );
2560     break;
2561 
2562   // double stores
2563   case Bytecodes::_dstore_0:
2564     set_pair_local( 0, dstore_rounding(pop_pair()) );
2565     break;
2566   case Bytecodes::_dstore_1:
2567     set_pair_local( 1, dstore_rounding(pop_pair()) );
2568     break;
2569   case Bytecodes::_dstore_2:
2570     set_pair_local( 2, dstore_rounding(pop_pair()) );
2571     break;
2572   case Bytecodes::_dstore_3:
2573     set_pair_local( 3, dstore_rounding(pop_pair()) );
2574     break;
2575   case Bytecodes::_dstore:
2576     set_pair_local( iter().get_index(), dstore_rounding(pop_pair()) );
2577     break;
2578 
2579   case Bytecodes::_pop:  dec_sp(1);   break;
2580   case Bytecodes::_pop2: dec_sp(2);   break;
2581   case Bytecodes::_swap:
2582     a = pop();
2583     b = pop();
2584     push(a);
2585     push(b);
2586     break;
2587   case Bytecodes::_dup:
2588     a = pop();
2589     push(a);
2590     push(a);
2591     break;
2592   case Bytecodes::_dup_x1:
2593     a = pop();
2594     b = pop();
2595     push( a );
2596     push( b );
2597     push( a );
2598     break;
2599   case Bytecodes::_dup_x2:
2600     a = pop();
2601     b = pop();
2602     c = pop();
2603     push( a );
2604     push( c );
2605     push( b );
2606     push( a );
2607     break;
2608   case Bytecodes::_dup2:
2609     a = pop();
2610     b = pop();
2611     push( b );
2612     push( a );
2613     push( b );
2614     push( a );
2615     break;
2616 
2617   case Bytecodes::_dup2_x1:
2618     // before: .. c, b, a
2619     // after:  .. b, a, c, b, a
2620     // not tested
2621     a = pop();
2622     b = pop();
2623     c = pop();
2624     push( b );
2625     push( a );
2626     push( c );
2627     push( b );
2628     push( a );
2629     break;
2630   case Bytecodes::_dup2_x2:
2631     // before: .. d, c, b, a
2632     // after:  .. b, a, d, c, b, a
2633     // not tested
2634     a = pop();
2635     b = pop();
2636     c = pop();
2637     d = pop();
2638     push( b );
2639     push( a );
2640     push( d );
2641     push( c );
2642     push( b );
2643     push( a );
2644     break;
2645 
2646   case Bytecodes::_arraylength: {
2647     // Must do null-check with value on expression stack
2648     Node *ary = null_check(peek(), T_ARRAY);
2649     // Compile-time detect of null-exception?
2650     if (stopped())  return;
2651     a = pop();
2652     push(load_array_length(a));
2653     break;
2654   }
2655 
2656   case Bytecodes::_baload:  array_load(T_BYTE);    break;
2657   case Bytecodes::_caload:  array_load(T_CHAR);    break;
2658   case Bytecodes::_iaload:  array_load(T_INT);     break;
2659   case Bytecodes::_saload:  array_load(T_SHORT);   break;
2660   case Bytecodes::_faload:  array_load(T_FLOAT);   break;
2661   case Bytecodes::_aaload:  array_load(T_OBJECT);  break;
2662   case Bytecodes::_laload:  array_load(T_LONG);    break;
2663   case Bytecodes::_daload:  array_load(T_DOUBLE);  break;
2664   case Bytecodes::_bastore: array_store(T_BYTE);   break;
2665   case Bytecodes::_castore: array_store(T_CHAR);   break;
2666   case Bytecodes::_iastore: array_store(T_INT);    break;
2667   case Bytecodes::_sastore: array_store(T_SHORT);  break;
2668   case Bytecodes::_fastore: array_store(T_FLOAT);  break;
2669   case Bytecodes::_aastore: array_store(T_OBJECT); break;
2670   case Bytecodes::_lastore: array_store(T_LONG);   break;
2671   case Bytecodes::_dastore: array_store(T_DOUBLE); break;
2672 
2673   case Bytecodes::_getfield:
2674     do_getfield();
2675     break;
2676 
2677   case Bytecodes::_getstatic:
2678     do_getstatic();
2679     break;
2680 
2681   case Bytecodes::_putfield:
2682     do_putfield();
2683     break;
2684 
2685   case Bytecodes::_putstatic:
2686     do_putstatic();
2687     break;
2688 
2689   case Bytecodes::_irem:
2690     do_irem();
2691     break;
2692   case Bytecodes::_idiv:
2693     // Must keep both values on the expression-stack during null-check
2694     zero_check_int(peek());
2695     // Compile-time detect of null-exception?
2696     if (stopped())  return;
2697     b = pop();
2698     a = pop();
2699     push( _gvn.transform( new DivINode(control(),a,b) ) );
2700     break;
2701   case Bytecodes::_imul:
2702     b = pop(); a = pop();
2703     push( _gvn.transform( new MulINode(a,b) ) );
2704     break;
2705   case Bytecodes::_iadd:
2706     b = pop(); a = pop();
2707     push( _gvn.transform( new AddINode(a,b) ) );
2708     break;
2709   case Bytecodes::_ineg:
2710     a = pop();
2711     push( _gvn.transform( new SubINode(_gvn.intcon(0),a)) );
2712     break;
2713   case Bytecodes::_isub:
2714     b = pop(); a = pop();
2715     push( _gvn.transform( new SubINode(a,b) ) );
2716     break;
2717   case Bytecodes::_iand:
2718     b = pop(); a = pop();
2719     push( _gvn.transform( new AndINode(a,b) ) );
2720     break;
2721   case Bytecodes::_ior:
2722     b = pop(); a = pop();
2723     push( _gvn.transform( new OrINode(a,b) ) );
2724     break;
2725   case Bytecodes::_ixor:
2726     b = pop(); a = pop();
2727     push( _gvn.transform( new XorINode(a,b) ) );
2728     break;
2729   case Bytecodes::_ishl:
2730     b = pop(); a = pop();
2731     push( _gvn.transform( new LShiftINode(a,b) ) );
2732     break;
2733   case Bytecodes::_ishr:
2734     b = pop(); a = pop();
2735     push( _gvn.transform( new RShiftINode(a,b) ) );
2736     break;
2737   case Bytecodes::_iushr:
2738     b = pop(); a = pop();
2739     push( _gvn.transform( new URShiftINode(a,b) ) );
2740     break;
2741 
2742   case Bytecodes::_fneg:
2743     a = pop();
2744     b = _gvn.transform(new NegFNode (a));
2745     push(b);
2746     break;
2747 
2748   case Bytecodes::_fsub:
2749     b = pop();
2750     a = pop();
2751     c = _gvn.transform( new SubFNode(a,b) );
2752     d = precision_rounding(c);
2753     push( d );
2754     break;
2755 
2756   case Bytecodes::_fadd:
2757     b = pop();
2758     a = pop();
2759     c = _gvn.transform( new AddFNode(a,b) );
2760     d = precision_rounding(c);
2761     push( d );
2762     break;
2763 
2764   case Bytecodes::_fmul:
2765     b = pop();
2766     a = pop();
2767     c = _gvn.transform( new MulFNode(a,b) );
2768     d = precision_rounding(c);
2769     push( d );
2770     break;
2771 
2772   case Bytecodes::_fdiv:
2773     b = pop();
2774     a = pop();
2775     c = _gvn.transform( new DivFNode(0,a,b) );
2776     d = precision_rounding(c);
2777     push( d );
2778     break;
2779 
2780   case Bytecodes::_frem:
2781     if (Matcher::has_match_rule(Op_ModF)) {
2782       // Generate a ModF node.
2783       b = pop();
2784       a = pop();
2785       c = _gvn.transform( new ModFNode(0,a,b) );
2786       d = precision_rounding(c);
2787       push( d );
2788     }
2789     else {
2790       // Generate a call.
2791       modf();
2792     }
2793     break;
2794 
2795   case Bytecodes::_fcmpl:
2796     b = pop();
2797     a = pop();
2798     c = _gvn.transform( new CmpF3Node( a, b));
2799     push(c);
2800     break;
2801   case Bytecodes::_fcmpg:
2802     b = pop();
2803     a = pop();
2804 
2805     // Same as fcmpl but need to flip the unordered case.  Swap the inputs,
2806     // which negates the result sign except for unordered.  Flip the unordered
2807     // as well by using CmpF3 which implements unordered-lesser instead of
2808     // unordered-greater semantics.  Finally, commute the result bits.  Result
2809     // is same as using a CmpF3Greater except we did it with CmpF3 alone.
2810     c = _gvn.transform( new CmpF3Node( b, a));
2811     c = _gvn.transform( new SubINode(_gvn.intcon(0),c) );
2812     push(c);
2813     break;
2814 
2815   case Bytecodes::_f2i:
2816     a = pop();
2817     push(_gvn.transform(new ConvF2INode(a)));
2818     break;
2819 
2820   case Bytecodes::_d2i:
2821     a = pop_pair();
2822     b = _gvn.transform(new ConvD2INode(a));
2823     push( b );
2824     break;
2825 
2826   case Bytecodes::_f2d:
2827     a = pop();
2828     b = _gvn.transform( new ConvF2DNode(a));
2829     push_pair( b );
2830     break;
2831 
2832   case Bytecodes::_d2f:
2833     a = pop_pair();
2834     b = _gvn.transform( new ConvD2FNode(a));
2835     // This breaks _227_mtrt (speed & correctness) and _222_mpegaudio (speed)
2836     //b = _gvn.transform(new RoundFloatNode(0, b) );
2837     push( b );
2838     break;
2839 
2840   case Bytecodes::_l2f:
2841     if (Matcher::convL2FSupported()) {
2842       a = pop_pair();
2843       b = _gvn.transform( new ConvL2FNode(a));
2844       // For i486.ad, FILD doesn't restrict precision to 24 or 53 bits.
2845       // Rather than storing the result into an FP register then pushing
2846       // out to memory to round, the machine instruction that implements
2847       // ConvL2D is responsible for rounding.
2848       // c = precision_rounding(b);
2849       c = _gvn.transform(b);
2850       push(c);
2851     } else {
2852       l2f();
2853     }
2854     break;
2855 
2856   case Bytecodes::_l2d:
2857     a = pop_pair();
2858     b = _gvn.transform( new ConvL2DNode(a));
2859     // For i486.ad, rounding is always necessary (see _l2f above).
2860     // c = dprecision_rounding(b);
2861     c = _gvn.transform(b);
2862     push_pair(c);
2863     break;
2864 
2865   case Bytecodes::_f2l:
2866     a = pop();
2867     b = _gvn.transform( new ConvF2LNode(a));
2868     push_pair(b);
2869     break;
2870 
2871   case Bytecodes::_d2l:
2872     a = pop_pair();
2873     b = _gvn.transform( new ConvD2LNode(a));
2874     push_pair(b);
2875     break;
2876 
2877   case Bytecodes::_dsub:
2878     b = pop_pair();
2879     a = pop_pair();
2880     c = _gvn.transform( new SubDNode(a,b) );
2881     d = dprecision_rounding(c);
2882     push_pair( d );
2883     break;
2884 
2885   case Bytecodes::_dadd:
2886     b = pop_pair();
2887     a = pop_pair();
2888     c = _gvn.transform( new AddDNode(a,b) );
2889     d = dprecision_rounding(c);
2890     push_pair( d );
2891     break;
2892 
2893   case Bytecodes::_dmul:
2894     b = pop_pair();
2895     a = pop_pair();
2896     c = _gvn.transform( new MulDNode(a,b) );
2897     d = dprecision_rounding(c);
2898     push_pair( d );
2899     break;
2900 
2901   case Bytecodes::_ddiv:
2902     b = pop_pair();
2903     a = pop_pair();
2904     c = _gvn.transform( new DivDNode(0,a,b) );
2905     d = dprecision_rounding(c);
2906     push_pair( d );
2907     break;
2908 
2909   case Bytecodes::_dneg:
2910     a = pop_pair();
2911     b = _gvn.transform(new NegDNode (a));
2912     push_pair(b);
2913     break;
2914 
2915   case Bytecodes::_drem:
2916     if (Matcher::has_match_rule(Op_ModD)) {
2917       // Generate a ModD node.
2918       b = pop_pair();
2919       a = pop_pair();
2920       // a % b
2921 
2922       c = _gvn.transform( new ModDNode(0,a,b) );
2923       d = dprecision_rounding(c);
2924       push_pair( d );
2925     }
2926     else {
2927       // Generate a call.
2928       modd();
2929     }
2930     break;
2931 
2932   case Bytecodes::_dcmpl:
2933     b = pop_pair();
2934     a = pop_pair();
2935     c = _gvn.transform( new CmpD3Node( a, b));
2936     push(c);
2937     break;
2938 
2939   case Bytecodes::_dcmpg:
2940     b = pop_pair();
2941     a = pop_pair();
2942     // Same as dcmpl but need to flip the unordered case.
2943     // Commute the inputs, which negates the result sign except for unordered.
2944     // Flip the unordered as well by using CmpD3 which implements
2945     // unordered-lesser instead of unordered-greater semantics.
2946     // Finally, negate the result bits.  Result is same as using a
2947     // CmpD3Greater except we did it with CmpD3 alone.
2948     c = _gvn.transform( new CmpD3Node( b, a));
2949     c = _gvn.transform( new SubINode(_gvn.intcon(0),c) );
2950     push(c);
2951     break;
2952 
2953 
2954     // Note for longs -> lo word is on TOS, hi word is on TOS - 1
2955   case Bytecodes::_land:
2956     b = pop_pair();
2957     a = pop_pair();
2958     c = _gvn.transform( new AndLNode(a,b) );
2959     push_pair(c);
2960     break;
2961   case Bytecodes::_lor:
2962     b = pop_pair();
2963     a = pop_pair();
2964     c = _gvn.transform( new OrLNode(a,b) );
2965     push_pair(c);
2966     break;
2967   case Bytecodes::_lxor:
2968     b = pop_pair();
2969     a = pop_pair();
2970     c = _gvn.transform( new XorLNode(a,b) );
2971     push_pair(c);
2972     break;
2973 
2974   case Bytecodes::_lshl:
2975     b = pop();                  // the shift count
2976     a = pop_pair();             // value to be shifted
2977     c = _gvn.transform( new LShiftLNode(a,b) );
2978     push_pair(c);
2979     break;
2980   case Bytecodes::_lshr:
2981     b = pop();                  // the shift count
2982     a = pop_pair();             // value to be shifted
2983     c = _gvn.transform( new RShiftLNode(a,b) );
2984     push_pair(c);
2985     break;
2986   case Bytecodes::_lushr:
2987     b = pop();                  // the shift count
2988     a = pop_pair();             // value to be shifted
2989     c = _gvn.transform( new URShiftLNode(a,b) );
2990     push_pair(c);
2991     break;
2992   case Bytecodes::_lmul:
2993     b = pop_pair();
2994     a = pop_pair();
2995     c = _gvn.transform( new MulLNode(a,b) );
2996     push_pair(c);
2997     break;
2998 
2999   case Bytecodes::_lrem:
3000     // Must keep both values on the expression-stack during null-check
3001     assert(peek(0) == top(), "long word order");
3002     zero_check_long(peek(1));
3003     // Compile-time detect of null-exception?
3004     if (stopped())  return;
3005     b = pop_pair();
3006     a = pop_pair();
3007     c = _gvn.transform( new ModLNode(control(),a,b) );
3008     push_pair(c);
3009     break;
3010 
3011   case Bytecodes::_ldiv:
3012     // Must keep both values on the expression-stack during null-check
3013     assert(peek(0) == top(), "long word order");
3014     zero_check_long(peek(1));
3015     // Compile-time detect of null-exception?
3016     if (stopped())  return;
3017     b = pop_pair();
3018     a = pop_pair();
3019     c = _gvn.transform( new DivLNode(control(),a,b) );
3020     push_pair(c);
3021     break;
3022 
3023   case Bytecodes::_ladd:
3024     b = pop_pair();
3025     a = pop_pair();
3026     c = _gvn.transform( new AddLNode(a,b) );
3027     push_pair(c);
3028     break;
3029   case Bytecodes::_lsub:
3030     b = pop_pair();
3031     a = pop_pair();
3032     c = _gvn.transform( new SubLNode(a,b) );
3033     push_pair(c);
3034     break;
3035   case Bytecodes::_lcmp:
3036     // Safepoints are now inserted _before_ branches.  The long-compare
3037     // bytecode painfully produces a 3-way value (-1,0,+1) which requires a
3038     // slew of control flow.  These are usually followed by a CmpI vs zero and
3039     // a branch; this pattern then optimizes to the obvious long-compare and
3040     // branch.  However, if the branch is backwards there's a Safepoint
3041     // inserted.  The inserted Safepoint captures the JVM state at the
3042     // pre-branch point, i.e. it captures the 3-way value.  Thus if a
3043     // long-compare is used to control a loop the debug info will force
3044     // computation of the 3-way value, even though the generated code uses a
3045     // long-compare and branch.  We try to rectify the situation by inserting
3046     // a SafePoint here and have it dominate and kill the safepoint added at a
3047     // following backwards branch.  At this point the JVM state merely holds 2
3048     // longs but not the 3-way value.
3049     if( UseLoopSafepoints ) {
3050       switch( iter().next_bc() ) {
3051       case Bytecodes::_ifgt:
3052       case Bytecodes::_iflt:
3053       case Bytecodes::_ifge:
3054       case Bytecodes::_ifle:
3055       case Bytecodes::_ifne:
3056       case Bytecodes::_ifeq:
3057         // If this is a backwards branch in the bytecodes, add Safepoint
3058         maybe_add_safepoint(iter().next_get_dest());
3059       default:
3060         break;
3061       }
3062     }
3063     b = pop_pair();
3064     a = pop_pair();
3065     c = _gvn.transform( new CmpL3Node( a, b ));
3066     push(c);
3067     break;
3068 
3069   case Bytecodes::_lneg:
3070     a = pop_pair();
3071     b = _gvn.transform( new SubLNode(longcon(0),a));
3072     push_pair(b);
3073     break;
3074   case Bytecodes::_l2i:
3075     a = pop_pair();
3076     push( _gvn.transform( new ConvL2INode(a)));
3077     break;
3078   case Bytecodes::_i2l:
3079     a = pop();
3080     b = _gvn.transform( new ConvI2LNode(a));
3081     push_pair(b);
3082     break;
3083   case Bytecodes::_i2b:
3084     // Sign extend
3085     a = pop();
3086     a = _gvn.transform( new LShiftINode(a,_gvn.intcon(24)) );
3087     a = _gvn.transform( new RShiftINode(a,_gvn.intcon(24)) );
3088     push( a );
3089     break;
3090   case Bytecodes::_i2s:
3091     a = pop();
3092     a = _gvn.transform( new LShiftINode(a,_gvn.intcon(16)) );
3093     a = _gvn.transform( new RShiftINode(a,_gvn.intcon(16)) );
3094     push( a );
3095     break;
3096   case Bytecodes::_i2c:
3097     a = pop();
3098     push( _gvn.transform( new AndINode(a,_gvn.intcon(0xFFFF)) ) );
3099     break;
3100 
3101   case Bytecodes::_i2f:
3102     a = pop();
3103     b = _gvn.transform( new ConvI2FNode(a) ) ;
3104     c = precision_rounding(b);
3105     push (b);
3106     break;
3107 
3108   case Bytecodes::_i2d:
3109     a = pop();
3110     b = _gvn.transform( new ConvI2DNode(a));
3111     push_pair(b);
3112     break;
3113 
3114   case Bytecodes::_iinc:        // Increment local
3115     i = iter().get_index();     // Get local index
3116     set_local( i, _gvn.transform( new AddINode( _gvn.intcon(iter().get_iinc_con()), local(i) ) ) );
3117     break;
3118 
3119   // Exit points of synchronized methods must have an unlock node
3120   case Bytecodes::_return:
3121     return_current(NULL);
3122     break;
3123 
3124   case Bytecodes::_ireturn:
3125   case Bytecodes::_areturn:
3126   case Bytecodes::_freturn:
3127     return_current(pop());
3128     break;
3129   case Bytecodes::_lreturn:
3130     return_current(pop_pair());
3131     break;
3132   case Bytecodes::_dreturn:
3133     return_current(pop_pair());
3134     break;
3135 
3136   case Bytecodes::_athrow:
3137     // null exception oop throws NULL pointer exception
3138     null_check(peek());
3139     if (stopped())  return;
3140     // Hook the thrown exception directly to subsequent handlers.
3141     if (BailoutToInterpreterForThrows) {
3142       // Keep method interpreted from now on.
3143       uncommon_trap(Deoptimization::Reason_unhandled,
3144                     Deoptimization::Action_make_not_compilable);
3145       return;
3146     }
3147     if (env()->jvmti_can_post_on_exceptions()) {
3148       // check if we must post exception events, take uncommon trap if so (with must_throw = false)
3149       uncommon_trap_if_should_post_on_exceptions(Deoptimization::Reason_unhandled, false);
3150     }
3151     // Here if either can_post_on_exceptions or should_post_on_exceptions is false
3152     add_exception_state(make_exception_state(peek()));
3153     break;
3154 
3155   case Bytecodes::_goto:   // fall through
3156   case Bytecodes::_goto_w: {
3157     int target_bci = (bc() == Bytecodes::_goto) ? iter().get_dest() : iter().get_far_dest();
3158 
3159     // If this is a backwards branch in the bytecodes, add Safepoint
3160     maybe_add_safepoint(target_bci);
3161 
3162     // Update method data
3163     profile_taken_branch(target_bci);
3164 
3165     // Merge the current control into the target basic block
3166     merge(target_bci);
3167 
3168     // See if we can get some profile data and hand it off to the next block
3169     Block *target_block = block()->successor_for_bci(target_bci);
3170     if (target_block->pred_count() != 1)  break;
3171     ciMethodData* methodData = method()->method_data();
3172     if (!methodData->is_mature())  break;
3173     ciProfileData* data = methodData->bci_to_data(bci());
3174     assert(data != NULL && data->is_JumpData(), "need JumpData for taken branch");
3175     int taken = ((ciJumpData*)data)->taken();
3176     taken = method()->scale_count(taken);
3177     target_block->set_count(taken);
3178     break;
3179   }
3180 
3181   case Bytecodes::_ifnull:    btest = BoolTest::eq; goto handle_if_null;
3182   case Bytecodes::_ifnonnull: btest = BoolTest::ne; goto handle_if_null;
3183   handle_if_null:
3184     // If this is a backwards branch in the bytecodes, add Safepoint
3185     maybe_add_safepoint(iter().get_dest());
3186     a = null();
3187     b = pop();
3188     if (b->is_ValueType()) {
3189       // Return constant false because 'b' is always non-null
3190       c = _gvn.makecon(TypeInt::CC_GT);
3191     } else {
3192       if (!_gvn.type(b)->speculative_maybe_null() &&
3193           !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
3194         inc_sp(1);
3195         Node* null_ctl = top();
3196         b = null_check_oop(b, &null_ctl, true, true, true);
3197         assert(null_ctl->is_top(), "no null control here");
3198         dec_sp(1);
3199       } else if (_gvn.type(b)->speculative_always_null() &&
3200                  !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
3201         inc_sp(1);
3202         b = null_assert(b);
3203         dec_sp(1);
3204       }
3205       c = _gvn.transform( new CmpPNode(b, a) );
3206     }
3207     do_ifnull(btest, c);
3208     break;
3209 
3210   case Bytecodes::_if_acmpeq: btest = BoolTest::eq; goto handle_if_acmp;
3211   case Bytecodes::_if_acmpne: btest = BoolTest::ne; goto handle_if_acmp;
3212   handle_if_acmp:
3213     // If this is a backwards branch in the bytecodes, add Safepoint
3214     maybe_add_safepoint(iter().get_dest());
3215     a = access_resolve(pop(), 0);
3216     b = access_resolve(pop(), 0);
3217     do_acmp(btest, a, b);
3218     break;
3219 
3220   case Bytecodes::_ifeq: btest = BoolTest::eq; goto handle_ifxx;
3221   case Bytecodes::_ifne: btest = BoolTest::ne; goto handle_ifxx;
3222   case Bytecodes::_iflt: btest = BoolTest::lt; goto handle_ifxx;
3223   case Bytecodes::_ifle: btest = BoolTest::le; goto handle_ifxx;
3224   case Bytecodes::_ifgt: btest = BoolTest::gt; goto handle_ifxx;
3225   case Bytecodes::_ifge: btest = BoolTest::ge; goto handle_ifxx;
3226   handle_ifxx:
3227     // If this is a backwards branch in the bytecodes, add Safepoint
3228     maybe_add_safepoint(iter().get_dest());
3229     a = _gvn.intcon(0);
3230     b = pop();
3231     c = _gvn.transform( new CmpINode(b, a) );
3232     do_if(btest, c);
3233     break;
3234 
3235   case Bytecodes::_if_icmpeq: btest = BoolTest::eq; goto handle_if_icmp;
3236   case Bytecodes::_if_icmpne: btest = BoolTest::ne; goto handle_if_icmp;
3237   case Bytecodes::_if_icmplt: btest = BoolTest::lt; goto handle_if_icmp;
3238   case Bytecodes::_if_icmple: btest = BoolTest::le; goto handle_if_icmp;
3239   case Bytecodes::_if_icmpgt: btest = BoolTest::gt; goto handle_if_icmp;
3240   case Bytecodes::_if_icmpge: btest = BoolTest::ge; goto handle_if_icmp;
3241   handle_if_icmp:
3242     // If this is a backwards branch in the bytecodes, add Safepoint
3243     maybe_add_safepoint(iter().get_dest());
3244     a = pop();
3245     b = pop();
3246     c = _gvn.transform( new CmpINode( b, a ) );
3247     do_if(btest, c);
3248     break;
3249 
3250   case Bytecodes::_tableswitch:
3251     do_tableswitch();
3252     break;
3253 
3254   case Bytecodes::_lookupswitch:
3255     do_lookupswitch();
3256     break;
3257 
3258   case Bytecodes::_invokestatic:
3259   case Bytecodes::_invokedynamic:
3260   case Bytecodes::_invokespecial:
3261   case Bytecodes::_invokevirtual:
3262   case Bytecodes::_invokeinterface:
3263     do_call();
3264     break;
3265   case Bytecodes::_checkcast:
3266     do_checkcast();
3267     break;
3268   case Bytecodes::_instanceof:
3269     do_instanceof();
3270     break;
3271   case Bytecodes::_anewarray:
3272     do_newarray();
3273     break;
3274   case Bytecodes::_newarray:
3275     do_newarray((BasicType)iter().get_index());
3276     break;
3277   case Bytecodes::_multianewarray:
3278     do_multianewarray();
3279     break;
3280   case Bytecodes::_new:
3281     do_new();
3282     break;
3283   case Bytecodes::_defaultvalue:
3284     do_defaultvalue();
3285     break;
3286   case Bytecodes::_withfield:
3287     do_withfield();
3288     break;
3289 
3290   case Bytecodes::_jsr:
3291   case Bytecodes::_jsr_w:
3292     do_jsr();
3293     break;
3294 
3295   case Bytecodes::_ret:
3296     do_ret();
3297     break;
3298 
3299 
3300   case Bytecodes::_monitorenter:
3301     do_monitor_enter();
3302     break;
3303 
3304   case Bytecodes::_monitorexit:
3305     do_monitor_exit();
3306     break;
3307 
3308   case Bytecodes::_breakpoint:
3309     // Breakpoint set concurrently to compile
3310     // %%% use an uncommon trap?
3311     C->record_failure("breakpoint in method");
3312     return;
3313 
3314   default:
3315 #ifndef PRODUCT
3316     map()->dump(99);
3317 #endif
3318     tty->print("\nUnhandled bytecode %s\n", Bytecodes::name(bc()) );
3319     ShouldNotReachHere();
3320   }
3321 
3322 #ifndef PRODUCT
3323   IdealGraphPrinter *printer = C->printer();
3324   if (printer && printer->should_print(1)) {
3325     char buffer[256];
3326     jio_snprintf(buffer, sizeof(buffer), "Bytecode %d: %s", bci(), Bytecodes::name(bc()));
3327     bool old = printer->traverse_outs();
3328     printer->set_traverse_outs(true);
3329     printer->print_method(buffer, 4);
3330     printer->set_traverse_outs(old);
3331   }
3332 #endif
3333 }