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