1 /*
   2  * Copyright (c) 1999, 2022, 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 "asm/macroAssembler.hpp"
  27 #include "ci/ciUtilities.inline.hpp"
  28 #include "classfile/vmIntrinsics.hpp"
  29 #include "compiler/compileBroker.hpp"
  30 #include "compiler/compileLog.hpp"
  31 #include "gc/shared/barrierSet.hpp"
  32 #include "jfr/support/jfrIntrinsics.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "oops/klass.inline.hpp"
  35 #include "oops/objArrayKlass.hpp"
  36 #include "opto/addnode.hpp"
  37 #include "opto/arraycopynode.hpp"
  38 #include "opto/c2compiler.hpp"
  39 #include "opto/castnode.hpp"
  40 #include "opto/cfgnode.hpp"
  41 #include "opto/convertnode.hpp"
  42 #include "opto/countbitsnode.hpp"
  43 #include "opto/idealKit.hpp"
  44 #include "opto/library_call.hpp"
  45 #include "opto/mathexactnode.hpp"
  46 #include "opto/mulnode.hpp"
  47 #include "opto/narrowptrnode.hpp"
  48 #include "opto/opaquenode.hpp"
  49 #include "opto/parse.hpp"
  50 #include "opto/runtime.hpp"
  51 #include "opto/rootnode.hpp"
  52 #include "opto/subnode.hpp"
  53 #include "prims/unsafe.hpp"
  54 #include "runtime/objectMonitor.hpp"
  55 #include "runtime/sharedRuntime.hpp"
  56 #include "runtime/stubRoutines.hpp"
  57 #include "utilities/macros.hpp"
  58 #include "utilities/powerOfTwo.hpp"
  59 
  60 #if INCLUDE_JFR
  61 #include "jfr/jfr.hpp"
  62 #endif
  63 
  64 //---------------------------make_vm_intrinsic----------------------------
  65 CallGenerator* Compile::make_vm_intrinsic(ciMethod* m, bool is_virtual) {
  66   vmIntrinsicID id = m->intrinsic_id();
  67   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
  68 
  69   if (!m->is_loaded()) {
  70     // Do not attempt to inline unloaded methods.
  71     return nullptr;
  72   }
  73 
  74   C2Compiler* compiler = (C2Compiler*)CompileBroker::compiler(CompLevel_full_optimization);
  75   bool is_available = false;
  76 
  77   {
  78     // For calling is_intrinsic_supported and is_intrinsic_disabled_by_flag
  79     // the compiler must transition to '_thread_in_vm' state because both
  80     // methods access VM-internal data.
  81     VM_ENTRY_MARK;
  82     methodHandle mh(THREAD, m->get_Method());
  83     is_available = compiler != nullptr && compiler->is_intrinsic_supported(mh, is_virtual) &&
  84                    !C->directive()->is_intrinsic_disabled(mh) &&
  85                    !vmIntrinsics::is_disabled_by_flags(mh);
  86 
  87   }
  88 
  89   if (is_available) {
  90     assert(id <= vmIntrinsics::LAST_COMPILER_INLINE, "caller responsibility");
  91     assert(id != vmIntrinsics::_Object_init && id != vmIntrinsics::_invoke, "enum out of order?");
  92     return new LibraryIntrinsic(m, is_virtual,
  93                                 vmIntrinsics::predicates_needed(id),
  94                                 vmIntrinsics::does_virtual_dispatch(id),
  95                                 id);
  96   } else {
  97     return nullptr;
  98   }
  99 }
 100 
 101 JVMState* LibraryIntrinsic::generate(JVMState* jvms) {
 102   LibraryCallKit kit(jvms, this);
 103   Compile* C = kit.C;
 104   int nodes = C->unique();
 105 #ifndef PRODUCT
 106   if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
 107     char buf[1000];
 108     const char* str = vmIntrinsics::short_name_as_C_string(intrinsic_id(), buf, sizeof(buf));
 109     tty->print_cr("Intrinsic %s", str);
 110   }
 111 #endif
 112   ciMethod* callee = kit.callee();
 113   const int bci    = kit.bci();
 114 #ifdef ASSERT
 115   Node* ctrl = kit.control();
 116 #endif
 117   // Try to inline the intrinsic.
 118   if (callee->check_intrinsic_candidate() &&
 119       kit.try_to_inline(_last_predicate)) {
 120     const char *inline_msg = is_virtual() ? "(intrinsic, virtual)"
 121                                           : "(intrinsic)";
 122     CompileTask::print_inlining_ul(callee, jvms->depth() - 1, bci, inline_msg);
 123     if (C->print_intrinsics() || C->print_inlining()) {
 124       C->print_inlining(callee, jvms->depth() - 1, bci, inline_msg);
 125     }
 126     C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_worked);
 127     if (C->log()) {
 128       C->log()->elem("intrinsic id='%s'%s nodes='%d'",
 129                      vmIntrinsics::name_at(intrinsic_id()),
 130                      (is_virtual() ? " virtual='1'" : ""),
 131                      C->unique() - nodes);
 132     }
 133     // Push the result from the inlined method onto the stack.
 134     kit.push_result();
 135     C->print_inlining_update(this);
 136     return kit.transfer_exceptions_into_jvms();
 137   }
 138 
 139   // The intrinsic bailed out
 140   assert(ctrl == kit.control(), "Control flow was added although the intrinsic bailed out");
 141   if (jvms->has_method()) {
 142     // Not a root compile.
 143     const char* msg;
 144     if (callee->intrinsic_candidate()) {
 145       msg = is_virtual() ? "failed to inline (intrinsic, virtual)" : "failed to inline (intrinsic)";
 146     } else {
 147       msg = is_virtual() ? "failed to inline (intrinsic, virtual), method not annotated"
 148                          : "failed to inline (intrinsic), method not annotated";
 149     }
 150     CompileTask::print_inlining_ul(callee, jvms->depth() - 1, bci, msg);
 151     if (C->print_intrinsics() || C->print_inlining()) {
 152       C->print_inlining(callee, jvms->depth() - 1, bci, msg);
 153     }
 154   } else {
 155     // Root compile
 156     ResourceMark rm;
 157     stringStream msg_stream;
 158     msg_stream.print("Did not generate intrinsic %s%s at bci:%d in",
 159                      vmIntrinsics::name_at(intrinsic_id()),
 160                      is_virtual() ? " (virtual)" : "", bci);
 161     const char *msg = msg_stream.as_string();
 162     log_debug(jit, inlining)("%s", msg);
 163     if (C->print_intrinsics() || C->print_inlining()) {
 164       tty->print("%s", msg);
 165     }
 166   }
 167   C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_failed);
 168   C->print_inlining_update(this);
 169 
 170   return nullptr;
 171 }
 172 
 173 Node* LibraryIntrinsic::generate_predicate(JVMState* jvms, int predicate) {
 174   LibraryCallKit kit(jvms, this);
 175   Compile* C = kit.C;
 176   int nodes = C->unique();
 177   _last_predicate = predicate;
 178 #ifndef PRODUCT
 179   assert(is_predicated() && predicate < predicates_count(), "sanity");
 180   if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
 181     char buf[1000];
 182     const char* str = vmIntrinsics::short_name_as_C_string(intrinsic_id(), buf, sizeof(buf));
 183     tty->print_cr("Predicate for intrinsic %s", str);
 184   }
 185 #endif
 186   ciMethod* callee = kit.callee();
 187   const int bci    = kit.bci();
 188 
 189   Node* slow_ctl = kit.try_to_predicate(predicate);
 190   if (!kit.failing()) {
 191     const char *inline_msg = is_virtual() ? "(intrinsic, virtual, predicate)"
 192                                           : "(intrinsic, predicate)";
 193     CompileTask::print_inlining_ul(callee, jvms->depth() - 1, bci, inline_msg);
 194     if (C->print_intrinsics() || C->print_inlining()) {
 195       C->print_inlining(callee, jvms->depth() - 1, bci, inline_msg);
 196     }
 197     C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_worked);
 198     if (C->log()) {
 199       C->log()->elem("predicate_intrinsic id='%s'%s nodes='%d'",
 200                      vmIntrinsics::name_at(intrinsic_id()),
 201                      (is_virtual() ? " virtual='1'" : ""),
 202                      C->unique() - nodes);
 203     }
 204     return slow_ctl; // Could be null if the check folds.
 205   }
 206 
 207   // The intrinsic bailed out
 208   if (jvms->has_method()) {
 209     // Not a root compile.
 210     const char* msg = "failed to generate predicate for intrinsic";
 211     CompileTask::print_inlining_ul(kit.callee(), jvms->depth() - 1, bci, msg);
 212     if (C->print_intrinsics() || C->print_inlining()) {
 213       C->print_inlining(kit.callee(), jvms->depth() - 1, bci, msg);
 214     }
 215   } else {
 216     // Root compile
 217     ResourceMark rm;
 218     stringStream msg_stream;
 219     msg_stream.print("Did not generate intrinsic %s%s at bci:%d in",
 220                      vmIntrinsics::name_at(intrinsic_id()),
 221                      is_virtual() ? " (virtual)" : "", bci);
 222     const char *msg = msg_stream.as_string();
 223     log_debug(jit, inlining)("%s", msg);
 224     if (C->print_intrinsics() || C->print_inlining()) {
 225       C->print_inlining_stream()->print("%s", msg);
 226     }
 227   }
 228   C->gather_intrinsic_statistics(intrinsic_id(), is_virtual(), Compile::_intrinsic_failed);
 229   return nullptr;
 230 }
 231 
 232 bool LibraryCallKit::try_to_inline(int predicate) {
 233   // Handle symbolic names for otherwise undistinguished boolean switches:
 234   const bool is_store       = true;
 235   const bool is_compress    = true;
 236   const bool is_static      = true;
 237   const bool is_volatile    = true;
 238 
 239   if (!jvms()->has_method()) {
 240     // Root JVMState has a null method.
 241     assert(map()->memory()->Opcode() == Op_Parm, "");
 242     // Insert the memory aliasing node
 243     set_all_memory(reset_memory());
 244   }
 245   assert(merged_memory(), "");
 246 
 247   switch (intrinsic_id()) {
 248   case vmIntrinsics::_hashCode:                 return inline_native_hashcode(intrinsic()->is_virtual(), !is_static);
 249   case vmIntrinsics::_identityHashCode:         return inline_native_hashcode(/*!virtual*/ false,         is_static);
 250   case vmIntrinsics::_getClass:                 return inline_native_getClass();
 251 
 252   case vmIntrinsics::_ceil:
 253   case vmIntrinsics::_floor:
 254   case vmIntrinsics::_rint:
 255   case vmIntrinsics::_dsin:
 256   case vmIntrinsics::_dcos:
 257   case vmIntrinsics::_dtan:
 258   case vmIntrinsics::_dabs:
 259   case vmIntrinsics::_fabs:
 260   case vmIntrinsics::_iabs:
 261   case vmIntrinsics::_labs:
 262   case vmIntrinsics::_datan2:
 263   case vmIntrinsics::_dsqrt:
 264   case vmIntrinsics::_dexp:
 265   case vmIntrinsics::_dlog:
 266   case vmIntrinsics::_dlog10:
 267   case vmIntrinsics::_dpow:
 268   case vmIntrinsics::_dcopySign:
 269   case vmIntrinsics::_fcopySign:
 270   case vmIntrinsics::_dsignum:
 271   case vmIntrinsics::_fsignum:                  return inline_math_native(intrinsic_id());
 272 
 273   case vmIntrinsics::_min:
 274   case vmIntrinsics::_max:                      return inline_min_max(intrinsic_id());
 275 
 276   case vmIntrinsics::_notify:
 277   case vmIntrinsics::_notifyAll:
 278     return inline_notify(intrinsic_id());
 279 
 280   case vmIntrinsics::_addExactI:                return inline_math_addExactI(false /* add */);
 281   case vmIntrinsics::_addExactL:                return inline_math_addExactL(false /* add */);
 282   case vmIntrinsics::_decrementExactI:          return inline_math_subtractExactI(true /* decrement */);
 283   case vmIntrinsics::_decrementExactL:          return inline_math_subtractExactL(true /* decrement */);
 284   case vmIntrinsics::_incrementExactI:          return inline_math_addExactI(true /* increment */);
 285   case vmIntrinsics::_incrementExactL:          return inline_math_addExactL(true /* increment */);
 286   case vmIntrinsics::_multiplyExactI:           return inline_math_multiplyExactI();
 287   case vmIntrinsics::_multiplyExactL:           return inline_math_multiplyExactL();
 288   case vmIntrinsics::_multiplyHigh:             return inline_math_multiplyHigh();
 289   case vmIntrinsics::_negateExactI:             return inline_math_negateExactI();
 290   case vmIntrinsics::_negateExactL:             return inline_math_negateExactL();
 291   case vmIntrinsics::_subtractExactI:           return inline_math_subtractExactI(false /* subtract */);
 292   case vmIntrinsics::_subtractExactL:           return inline_math_subtractExactL(false /* subtract */);
 293 
 294   case vmIntrinsics::_arraycopy:                return inline_arraycopy();
 295 
 296   case vmIntrinsics::_compareToL:               return inline_string_compareTo(StrIntrinsicNode::LL);
 297   case vmIntrinsics::_compareToU:               return inline_string_compareTo(StrIntrinsicNode::UU);
 298   case vmIntrinsics::_compareToLU:              return inline_string_compareTo(StrIntrinsicNode::LU);
 299   case vmIntrinsics::_compareToUL:              return inline_string_compareTo(StrIntrinsicNode::UL);
 300 
 301   case vmIntrinsics::_indexOfL:                 return inline_string_indexOf(StrIntrinsicNode::LL);
 302   case vmIntrinsics::_indexOfU:                 return inline_string_indexOf(StrIntrinsicNode::UU);
 303   case vmIntrinsics::_indexOfUL:                return inline_string_indexOf(StrIntrinsicNode::UL);
 304   case vmIntrinsics::_indexOfIL:                return inline_string_indexOfI(StrIntrinsicNode::LL);
 305   case vmIntrinsics::_indexOfIU:                return inline_string_indexOfI(StrIntrinsicNode::UU);
 306   case vmIntrinsics::_indexOfIUL:               return inline_string_indexOfI(StrIntrinsicNode::UL);
 307   case vmIntrinsics::_indexOfU_char:            return inline_string_indexOfChar(StrIntrinsicNode::U);
 308   case vmIntrinsics::_indexOfL_char:            return inline_string_indexOfChar(StrIntrinsicNode::L);
 309 
 310   case vmIntrinsics::_equalsL:                  return inline_string_equals(StrIntrinsicNode::LL);
 311   case vmIntrinsics::_equalsU:                  return inline_string_equals(StrIntrinsicNode::UU);
 312 
 313   case vmIntrinsics::_toBytesStringU:           return inline_string_toBytesU();
 314   case vmIntrinsics::_getCharsStringU:          return inline_string_getCharsU();
 315   case vmIntrinsics::_getCharStringU:           return inline_string_char_access(!is_store);
 316   case vmIntrinsics::_putCharStringU:           return inline_string_char_access( is_store);
 317 
 318   case vmIntrinsics::_compressStringC:
 319   case vmIntrinsics::_compressStringB:          return inline_string_copy( is_compress);
 320   case vmIntrinsics::_inflateStringC:
 321   case vmIntrinsics::_inflateStringB:           return inline_string_copy(!is_compress);
 322 
 323   case vmIntrinsics::_getReference:             return inline_unsafe_access(!is_store, T_OBJECT,   Relaxed, false);
 324   case vmIntrinsics::_getBoolean:               return inline_unsafe_access(!is_store, T_BOOLEAN,  Relaxed, false);
 325   case vmIntrinsics::_getByte:                  return inline_unsafe_access(!is_store, T_BYTE,     Relaxed, false);
 326   case vmIntrinsics::_getShort:                 return inline_unsafe_access(!is_store, T_SHORT,    Relaxed, false);
 327   case vmIntrinsics::_getChar:                  return inline_unsafe_access(!is_store, T_CHAR,     Relaxed, false);
 328   case vmIntrinsics::_getInt:                   return inline_unsafe_access(!is_store, T_INT,      Relaxed, false);
 329   case vmIntrinsics::_getLong:                  return inline_unsafe_access(!is_store, T_LONG,     Relaxed, false);
 330   case vmIntrinsics::_getFloat:                 return inline_unsafe_access(!is_store, T_FLOAT,    Relaxed, false);
 331   case vmIntrinsics::_getDouble:                return inline_unsafe_access(!is_store, T_DOUBLE,   Relaxed, false);
 332 
 333   case vmIntrinsics::_putReference:             return inline_unsafe_access( is_store, T_OBJECT,   Relaxed, false);
 334   case vmIntrinsics::_putBoolean:               return inline_unsafe_access( is_store, T_BOOLEAN,  Relaxed, false);
 335   case vmIntrinsics::_putByte:                  return inline_unsafe_access( is_store, T_BYTE,     Relaxed, false);
 336   case vmIntrinsics::_putShort:                 return inline_unsafe_access( is_store, T_SHORT,    Relaxed, false);
 337   case vmIntrinsics::_putChar:                  return inline_unsafe_access( is_store, T_CHAR,     Relaxed, false);
 338   case vmIntrinsics::_putInt:                   return inline_unsafe_access( is_store, T_INT,      Relaxed, false);
 339   case vmIntrinsics::_putLong:                  return inline_unsafe_access( is_store, T_LONG,     Relaxed, false);
 340   case vmIntrinsics::_putFloat:                 return inline_unsafe_access( is_store, T_FLOAT,    Relaxed, false);
 341   case vmIntrinsics::_putDouble:                return inline_unsafe_access( is_store, T_DOUBLE,   Relaxed, false);
 342 
 343   case vmIntrinsics::_getReferenceVolatile:     return inline_unsafe_access(!is_store, T_OBJECT,   Volatile, false);
 344   case vmIntrinsics::_getBooleanVolatile:       return inline_unsafe_access(!is_store, T_BOOLEAN,  Volatile, false);
 345   case vmIntrinsics::_getByteVolatile:          return inline_unsafe_access(!is_store, T_BYTE,     Volatile, false);
 346   case vmIntrinsics::_getShortVolatile:         return inline_unsafe_access(!is_store, T_SHORT,    Volatile, false);
 347   case vmIntrinsics::_getCharVolatile:          return inline_unsafe_access(!is_store, T_CHAR,     Volatile, false);
 348   case vmIntrinsics::_getIntVolatile:           return inline_unsafe_access(!is_store, T_INT,      Volatile, false);
 349   case vmIntrinsics::_getLongVolatile:          return inline_unsafe_access(!is_store, T_LONG,     Volatile, false);
 350   case vmIntrinsics::_getFloatVolatile:         return inline_unsafe_access(!is_store, T_FLOAT,    Volatile, false);
 351   case vmIntrinsics::_getDoubleVolatile:        return inline_unsafe_access(!is_store, T_DOUBLE,   Volatile, false);
 352 
 353   case vmIntrinsics::_putReferenceVolatile:     return inline_unsafe_access( is_store, T_OBJECT,   Volatile, false);
 354   case vmIntrinsics::_putBooleanVolatile:       return inline_unsafe_access( is_store, T_BOOLEAN,  Volatile, false);
 355   case vmIntrinsics::_putByteVolatile:          return inline_unsafe_access( is_store, T_BYTE,     Volatile, false);
 356   case vmIntrinsics::_putShortVolatile:         return inline_unsafe_access( is_store, T_SHORT,    Volatile, false);
 357   case vmIntrinsics::_putCharVolatile:          return inline_unsafe_access( is_store, T_CHAR,     Volatile, false);
 358   case vmIntrinsics::_putIntVolatile:           return inline_unsafe_access( is_store, T_INT,      Volatile, false);
 359   case vmIntrinsics::_putLongVolatile:          return inline_unsafe_access( is_store, T_LONG,     Volatile, false);
 360   case vmIntrinsics::_putFloatVolatile:         return inline_unsafe_access( is_store, T_FLOAT,    Volatile, false);
 361   case vmIntrinsics::_putDoubleVolatile:        return inline_unsafe_access( is_store, T_DOUBLE,   Volatile, false);
 362 
 363   case vmIntrinsics::_getShortUnaligned:        return inline_unsafe_access(!is_store, T_SHORT,    Relaxed, true);
 364   case vmIntrinsics::_getCharUnaligned:         return inline_unsafe_access(!is_store, T_CHAR,     Relaxed, true);
 365   case vmIntrinsics::_getIntUnaligned:          return inline_unsafe_access(!is_store, T_INT,      Relaxed, true);
 366   case vmIntrinsics::_getLongUnaligned:         return inline_unsafe_access(!is_store, T_LONG,     Relaxed, true);
 367 
 368   case vmIntrinsics::_putShortUnaligned:        return inline_unsafe_access( is_store, T_SHORT,    Relaxed, true);
 369   case vmIntrinsics::_putCharUnaligned:         return inline_unsafe_access( is_store, T_CHAR,     Relaxed, true);
 370   case vmIntrinsics::_putIntUnaligned:          return inline_unsafe_access( is_store, T_INT,      Relaxed, true);
 371   case vmIntrinsics::_putLongUnaligned:         return inline_unsafe_access( is_store, T_LONG,     Relaxed, true);
 372 
 373   case vmIntrinsics::_getReferenceAcquire:      return inline_unsafe_access(!is_store, T_OBJECT,   Acquire, false);
 374   case vmIntrinsics::_getBooleanAcquire:        return inline_unsafe_access(!is_store, T_BOOLEAN,  Acquire, false);
 375   case vmIntrinsics::_getByteAcquire:           return inline_unsafe_access(!is_store, T_BYTE,     Acquire, false);
 376   case vmIntrinsics::_getShortAcquire:          return inline_unsafe_access(!is_store, T_SHORT,    Acquire, false);
 377   case vmIntrinsics::_getCharAcquire:           return inline_unsafe_access(!is_store, T_CHAR,     Acquire, false);
 378   case vmIntrinsics::_getIntAcquire:            return inline_unsafe_access(!is_store, T_INT,      Acquire, false);
 379   case vmIntrinsics::_getLongAcquire:           return inline_unsafe_access(!is_store, T_LONG,     Acquire, false);
 380   case vmIntrinsics::_getFloatAcquire:          return inline_unsafe_access(!is_store, T_FLOAT,    Acquire, false);
 381   case vmIntrinsics::_getDoubleAcquire:         return inline_unsafe_access(!is_store, T_DOUBLE,   Acquire, false);
 382 
 383   case vmIntrinsics::_putReferenceRelease:      return inline_unsafe_access( is_store, T_OBJECT,   Release, false);
 384   case vmIntrinsics::_putBooleanRelease:        return inline_unsafe_access( is_store, T_BOOLEAN,  Release, false);
 385   case vmIntrinsics::_putByteRelease:           return inline_unsafe_access( is_store, T_BYTE,     Release, false);
 386   case vmIntrinsics::_putShortRelease:          return inline_unsafe_access( is_store, T_SHORT,    Release, false);
 387   case vmIntrinsics::_putCharRelease:           return inline_unsafe_access( is_store, T_CHAR,     Release, false);
 388   case vmIntrinsics::_putIntRelease:            return inline_unsafe_access( is_store, T_INT,      Release, false);
 389   case vmIntrinsics::_putLongRelease:           return inline_unsafe_access( is_store, T_LONG,     Release, false);
 390   case vmIntrinsics::_putFloatRelease:          return inline_unsafe_access( is_store, T_FLOAT,    Release, false);
 391   case vmIntrinsics::_putDoubleRelease:         return inline_unsafe_access( is_store, T_DOUBLE,   Release, false);
 392 
 393   case vmIntrinsics::_getReferenceOpaque:       return inline_unsafe_access(!is_store, T_OBJECT,   Opaque, false);
 394   case vmIntrinsics::_getBooleanOpaque:         return inline_unsafe_access(!is_store, T_BOOLEAN,  Opaque, false);
 395   case vmIntrinsics::_getByteOpaque:            return inline_unsafe_access(!is_store, T_BYTE,     Opaque, false);
 396   case vmIntrinsics::_getShortOpaque:           return inline_unsafe_access(!is_store, T_SHORT,    Opaque, false);
 397   case vmIntrinsics::_getCharOpaque:            return inline_unsafe_access(!is_store, T_CHAR,     Opaque, false);
 398   case vmIntrinsics::_getIntOpaque:             return inline_unsafe_access(!is_store, T_INT,      Opaque, false);
 399   case vmIntrinsics::_getLongOpaque:            return inline_unsafe_access(!is_store, T_LONG,     Opaque, false);
 400   case vmIntrinsics::_getFloatOpaque:           return inline_unsafe_access(!is_store, T_FLOAT,    Opaque, false);
 401   case vmIntrinsics::_getDoubleOpaque:          return inline_unsafe_access(!is_store, T_DOUBLE,   Opaque, false);
 402 
 403   case vmIntrinsics::_putReferenceOpaque:       return inline_unsafe_access( is_store, T_OBJECT,   Opaque, false);
 404   case vmIntrinsics::_putBooleanOpaque:         return inline_unsafe_access( is_store, T_BOOLEAN,  Opaque, false);
 405   case vmIntrinsics::_putByteOpaque:            return inline_unsafe_access( is_store, T_BYTE,     Opaque, false);
 406   case vmIntrinsics::_putShortOpaque:           return inline_unsafe_access( is_store, T_SHORT,    Opaque, false);
 407   case vmIntrinsics::_putCharOpaque:            return inline_unsafe_access( is_store, T_CHAR,     Opaque, false);
 408   case vmIntrinsics::_putIntOpaque:             return inline_unsafe_access( is_store, T_INT,      Opaque, false);
 409   case vmIntrinsics::_putLongOpaque:            return inline_unsafe_access( is_store, T_LONG,     Opaque, false);
 410   case vmIntrinsics::_putFloatOpaque:           return inline_unsafe_access( is_store, T_FLOAT,    Opaque, false);
 411   case vmIntrinsics::_putDoubleOpaque:          return inline_unsafe_access( is_store, T_DOUBLE,   Opaque, false);
 412 
 413   case vmIntrinsics::_compareAndSetReference:   return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap,      Volatile);
 414   case vmIntrinsics::_compareAndSetByte:        return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap,      Volatile);
 415   case vmIntrinsics::_compareAndSetShort:       return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap,      Volatile);
 416   case vmIntrinsics::_compareAndSetInt:         return inline_unsafe_load_store(T_INT,    LS_cmp_swap,      Volatile);
 417   case vmIntrinsics::_compareAndSetLong:        return inline_unsafe_load_store(T_LONG,   LS_cmp_swap,      Volatile);
 418 
 419   case vmIntrinsics::_weakCompareAndSetReferencePlain:     return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Relaxed);
 420   case vmIntrinsics::_weakCompareAndSetReferenceAcquire:   return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Acquire);
 421   case vmIntrinsics::_weakCompareAndSetReferenceRelease:   return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Release);
 422   case vmIntrinsics::_weakCompareAndSetReference:          return inline_unsafe_load_store(T_OBJECT, LS_cmp_swap_weak, Volatile);
 423   case vmIntrinsics::_weakCompareAndSetBytePlain:          return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Relaxed);
 424   case vmIntrinsics::_weakCompareAndSetByteAcquire:        return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Acquire);
 425   case vmIntrinsics::_weakCompareAndSetByteRelease:        return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Release);
 426   case vmIntrinsics::_weakCompareAndSetByte:               return inline_unsafe_load_store(T_BYTE,   LS_cmp_swap_weak, Volatile);
 427   case vmIntrinsics::_weakCompareAndSetShortPlain:         return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Relaxed);
 428   case vmIntrinsics::_weakCompareAndSetShortAcquire:       return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Acquire);
 429   case vmIntrinsics::_weakCompareAndSetShortRelease:       return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Release);
 430   case vmIntrinsics::_weakCompareAndSetShort:              return inline_unsafe_load_store(T_SHORT,  LS_cmp_swap_weak, Volatile);
 431   case vmIntrinsics::_weakCompareAndSetIntPlain:           return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Relaxed);
 432   case vmIntrinsics::_weakCompareAndSetIntAcquire:         return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Acquire);
 433   case vmIntrinsics::_weakCompareAndSetIntRelease:         return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Release);
 434   case vmIntrinsics::_weakCompareAndSetInt:                return inline_unsafe_load_store(T_INT,    LS_cmp_swap_weak, Volatile);
 435   case vmIntrinsics::_weakCompareAndSetLongPlain:          return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Relaxed);
 436   case vmIntrinsics::_weakCompareAndSetLongAcquire:        return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Acquire);
 437   case vmIntrinsics::_weakCompareAndSetLongRelease:        return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Release);
 438   case vmIntrinsics::_weakCompareAndSetLong:               return inline_unsafe_load_store(T_LONG,   LS_cmp_swap_weak, Volatile);
 439 
 440   case vmIntrinsics::_compareAndExchangeReference:         return inline_unsafe_load_store(T_OBJECT, LS_cmp_exchange,  Volatile);
 441   case vmIntrinsics::_compareAndExchangeReferenceAcquire:  return inline_unsafe_load_store(T_OBJECT, LS_cmp_exchange,  Acquire);
 442   case vmIntrinsics::_compareAndExchangeReferenceRelease:  return inline_unsafe_load_store(T_OBJECT, LS_cmp_exchange,  Release);
 443   case vmIntrinsics::_compareAndExchangeByte:              return inline_unsafe_load_store(T_BYTE,   LS_cmp_exchange,  Volatile);
 444   case vmIntrinsics::_compareAndExchangeByteAcquire:       return inline_unsafe_load_store(T_BYTE,   LS_cmp_exchange,  Acquire);
 445   case vmIntrinsics::_compareAndExchangeByteRelease:       return inline_unsafe_load_store(T_BYTE,   LS_cmp_exchange,  Release);
 446   case vmIntrinsics::_compareAndExchangeShort:             return inline_unsafe_load_store(T_SHORT,  LS_cmp_exchange,  Volatile);
 447   case vmIntrinsics::_compareAndExchangeShortAcquire:      return inline_unsafe_load_store(T_SHORT,  LS_cmp_exchange,  Acquire);
 448   case vmIntrinsics::_compareAndExchangeShortRelease:      return inline_unsafe_load_store(T_SHORT,  LS_cmp_exchange,  Release);
 449   case vmIntrinsics::_compareAndExchangeInt:               return inline_unsafe_load_store(T_INT,    LS_cmp_exchange,  Volatile);
 450   case vmIntrinsics::_compareAndExchangeIntAcquire:        return inline_unsafe_load_store(T_INT,    LS_cmp_exchange,  Acquire);
 451   case vmIntrinsics::_compareAndExchangeIntRelease:        return inline_unsafe_load_store(T_INT,    LS_cmp_exchange,  Release);
 452   case vmIntrinsics::_compareAndExchangeLong:              return inline_unsafe_load_store(T_LONG,   LS_cmp_exchange,  Volatile);
 453   case vmIntrinsics::_compareAndExchangeLongAcquire:       return inline_unsafe_load_store(T_LONG,   LS_cmp_exchange,  Acquire);
 454   case vmIntrinsics::_compareAndExchangeLongRelease:       return inline_unsafe_load_store(T_LONG,   LS_cmp_exchange,  Release);
 455 
 456   case vmIntrinsics::_getAndAddByte:                    return inline_unsafe_load_store(T_BYTE,   LS_get_add,       Volatile);
 457   case vmIntrinsics::_getAndAddShort:                   return inline_unsafe_load_store(T_SHORT,  LS_get_add,       Volatile);
 458   case vmIntrinsics::_getAndAddInt:                     return inline_unsafe_load_store(T_INT,    LS_get_add,       Volatile);
 459   case vmIntrinsics::_getAndAddLong:                    return inline_unsafe_load_store(T_LONG,   LS_get_add,       Volatile);
 460 
 461   case vmIntrinsics::_getAndSetByte:                    return inline_unsafe_load_store(T_BYTE,   LS_get_set,       Volatile);
 462   case vmIntrinsics::_getAndSetShort:                   return inline_unsafe_load_store(T_SHORT,  LS_get_set,       Volatile);
 463   case vmIntrinsics::_getAndSetInt:                     return inline_unsafe_load_store(T_INT,    LS_get_set,       Volatile);
 464   case vmIntrinsics::_getAndSetLong:                    return inline_unsafe_load_store(T_LONG,   LS_get_set,       Volatile);
 465   case vmIntrinsics::_getAndSetReference:               return inline_unsafe_load_store(T_OBJECT, LS_get_set,       Volatile);
 466 
 467   case vmIntrinsics::_loadFence:
 468   case vmIntrinsics::_storeFence:
 469   case vmIntrinsics::_storeStoreFence:
 470   case vmIntrinsics::_fullFence:                return inline_unsafe_fence(intrinsic_id());
 471 
 472   case vmIntrinsics::_onSpinWait:               return inline_onspinwait();
 473 
 474   case vmIntrinsics::_currentThread:            return inline_native_currentThread();
 475 
 476 #ifdef JFR_HAVE_INTRINSICS
 477   case vmIntrinsics::_counterTime:              return inline_native_time_funcs(CAST_FROM_FN_PTR(address, JFR_TIME_FUNCTION), "counterTime");
 478   case vmIntrinsics::_getClassId:               return inline_native_classID();
 479   case vmIntrinsics::_getEventWriter:           return inline_native_getEventWriter();
 480 #endif
 481   case vmIntrinsics::_currentTimeMillis:        return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeMillis), "currentTimeMillis");
 482   case vmIntrinsics::_nanoTime:                 return inline_native_time_funcs(CAST_FROM_FN_PTR(address, os::javaTimeNanos), "nanoTime");
 483   case vmIntrinsics::_writeback0:               return inline_unsafe_writeback0();
 484   case vmIntrinsics::_writebackPreSync0:        return inline_unsafe_writebackSync0(true);
 485   case vmIntrinsics::_writebackPostSync0:       return inline_unsafe_writebackSync0(false);
 486   case vmIntrinsics::_allocateInstance:         return inline_unsafe_allocate();
 487   case vmIntrinsics::_copyMemory:               return inline_unsafe_copyMemory();
 488   case vmIntrinsics::_getLength:                return inline_native_getLength();
 489   case vmIntrinsics::_copyOf:                   return inline_array_copyOf(false);
 490   case vmIntrinsics::_copyOfRange:              return inline_array_copyOf(true);
 491   case vmIntrinsics::_equalsB:                  return inline_array_equals(StrIntrinsicNode::LL);
 492   case vmIntrinsics::_equalsC:                  return inline_array_equals(StrIntrinsicNode::UU);
 493   case vmIntrinsics::_Preconditions_checkIndex: return inline_preconditions_checkIndex(T_INT);
 494   case vmIntrinsics::_Preconditions_checkLongIndex: return inline_preconditions_checkIndex(T_LONG);
 495   case vmIntrinsics::_clone:                    return inline_native_clone(intrinsic()->is_virtual());
 496 
 497   case vmIntrinsics::_allocateUninitializedArray: return inline_unsafe_newArray(true);
 498   case vmIntrinsics::_newArray:                   return inline_unsafe_newArray(false);
 499 
 500   case vmIntrinsics::_isAssignableFrom:         return inline_native_subtype_check();
 501 
 502   case vmIntrinsics::_isInstance:
 503   case vmIntrinsics::_getModifiers:
 504   case vmIntrinsics::_isInterface:
 505   case vmIntrinsics::_isArray:
 506   case vmIntrinsics::_isPrimitive:
 507   case vmIntrinsics::_isHidden:
 508   case vmIntrinsics::_getSuperclass:
 509   case vmIntrinsics::_getClassAccessFlags:      return inline_native_Class_query(intrinsic_id());
 510 
 511   case vmIntrinsics::_floatToRawIntBits:
 512   case vmIntrinsics::_floatToIntBits:
 513   case vmIntrinsics::_intBitsToFloat:
 514   case vmIntrinsics::_doubleToRawLongBits:
 515   case vmIntrinsics::_doubleToLongBits:
 516   case vmIntrinsics::_longBitsToDouble:         return inline_fp_conversions(intrinsic_id());
 517 
 518   case vmIntrinsics::_numberOfLeadingZeros_i:
 519   case vmIntrinsics::_numberOfLeadingZeros_l:
 520   case vmIntrinsics::_numberOfTrailingZeros_i:
 521   case vmIntrinsics::_numberOfTrailingZeros_l:
 522   case vmIntrinsics::_bitCount_i:
 523   case vmIntrinsics::_bitCount_l:
 524   case vmIntrinsics::_reverseBytes_i:
 525   case vmIntrinsics::_reverseBytes_l:
 526   case vmIntrinsics::_reverseBytes_s:
 527   case vmIntrinsics::_reverseBytes_c:           return inline_number_methods(intrinsic_id());
 528 
 529   case vmIntrinsics::_getCallerClass:           return inline_native_Reflection_getCallerClass();
 530 
 531   case vmIntrinsics::_Reference_get:            return inline_reference_get();
 532   case vmIntrinsics::_Reference_refersTo0:      return inline_reference_refersTo0(false);
 533   case vmIntrinsics::_PhantomReference_refersTo0: return inline_reference_refersTo0(true);
 534 
 535   case vmIntrinsics::_Class_cast:               return inline_Class_cast();
 536 
 537   case vmIntrinsics::_aescrypt_encryptBlock:
 538   case vmIntrinsics::_aescrypt_decryptBlock:    return inline_aescrypt_Block(intrinsic_id());
 539 
 540   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
 541   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
 542     return inline_cipherBlockChaining_AESCrypt(intrinsic_id());
 543 
 544   case vmIntrinsics::_electronicCodeBook_encryptAESCrypt:
 545   case vmIntrinsics::_electronicCodeBook_decryptAESCrypt:
 546     return inline_electronicCodeBook_AESCrypt(intrinsic_id());
 547 
 548   case vmIntrinsics::_counterMode_AESCrypt:
 549     return inline_counterMode_AESCrypt(intrinsic_id());
 550 
 551   case vmIntrinsics::_md5_implCompress:
 552   case vmIntrinsics::_sha_implCompress:
 553   case vmIntrinsics::_sha2_implCompress:
 554   case vmIntrinsics::_sha5_implCompress:
 555   case vmIntrinsics::_sha3_implCompress:
 556     return inline_digestBase_implCompress(intrinsic_id());
 557 
 558   case vmIntrinsics::_digestBase_implCompressMB:
 559     return inline_digestBase_implCompressMB(predicate);
 560 
 561   case vmIntrinsics::_multiplyToLen:
 562     return inline_multiplyToLen();
 563 
 564   case vmIntrinsics::_squareToLen:
 565     return inline_squareToLen();
 566 
 567   case vmIntrinsics::_mulAdd:
 568     return inline_mulAdd();
 569 
 570   case vmIntrinsics::_montgomeryMultiply:
 571     return inline_montgomeryMultiply();
 572   case vmIntrinsics::_montgomerySquare:
 573     return inline_montgomerySquare();
 574 
 575   case vmIntrinsics::_bigIntegerRightShiftWorker:
 576     return inline_bigIntegerShift(true);
 577   case vmIntrinsics::_bigIntegerLeftShiftWorker:
 578     return inline_bigIntegerShift(false);
 579 
 580   case vmIntrinsics::_vectorizedMismatch:
 581     return inline_vectorizedMismatch();
 582 
 583   case vmIntrinsics::_ghash_processBlocks:
 584     return inline_ghash_processBlocks();
 585   case vmIntrinsics::_base64_encodeBlock:
 586     return inline_base64_encodeBlock();
 587   case vmIntrinsics::_base64_decodeBlock:
 588     return inline_base64_decodeBlock();
 589 
 590   case vmIntrinsics::_encodeISOArray:
 591   case vmIntrinsics::_encodeByteISOArray:
 592     return inline_encodeISOArray(false);
 593   case vmIntrinsics::_encodeAsciiArray:
 594     return inline_encodeISOArray(true);
 595 
 596   case vmIntrinsics::_updateCRC32:
 597     return inline_updateCRC32();
 598   case vmIntrinsics::_updateBytesCRC32:
 599     return inline_updateBytesCRC32();
 600   case vmIntrinsics::_updateByteBufferCRC32:
 601     return inline_updateByteBufferCRC32();
 602 
 603   case vmIntrinsics::_updateBytesCRC32C:
 604     return inline_updateBytesCRC32C();
 605   case vmIntrinsics::_updateDirectByteBufferCRC32C:
 606     return inline_updateDirectByteBufferCRC32C();
 607 
 608   case vmIntrinsics::_updateBytesAdler32:
 609     return inline_updateBytesAdler32();
 610   case vmIntrinsics::_updateByteBufferAdler32:
 611     return inline_updateByteBufferAdler32();
 612 
 613   case vmIntrinsics::_profileBoolean:
 614     return inline_profileBoolean();
 615   case vmIntrinsics::_isCompileConstant:
 616     return inline_isCompileConstant();
 617 
 618   case vmIntrinsics::_hasNegatives:
 619     return inline_hasNegatives();
 620 
 621   case vmIntrinsics::_fmaD:
 622   case vmIntrinsics::_fmaF:
 623     return inline_fma(intrinsic_id());
 624 
 625   case vmIntrinsics::_isDigit:
 626   case vmIntrinsics::_isLowerCase:
 627   case vmIntrinsics::_isUpperCase:
 628   case vmIntrinsics::_isWhitespace:
 629     return inline_character_compare(intrinsic_id());
 630 
 631   case vmIntrinsics::_maxF:
 632   case vmIntrinsics::_minF:
 633   case vmIntrinsics::_maxD:
 634   case vmIntrinsics::_minD:
 635     return inline_fp_min_max(intrinsic_id());
 636 
 637   case vmIntrinsics::_VectorUnaryOp:
 638     return inline_vector_nary_operation(1);
 639   case vmIntrinsics::_VectorBinaryOp:
 640     return inline_vector_nary_operation(2);
 641   case vmIntrinsics::_VectorTernaryOp:
 642     return inline_vector_nary_operation(3);
 643   case vmIntrinsics::_VectorBroadcastCoerced:
 644     return inline_vector_broadcast_coerced();
 645   case vmIntrinsics::_VectorShuffleIota:
 646     return inline_vector_shuffle_iota();
 647   case vmIntrinsics::_VectorMaskOp:
 648     return inline_vector_mask_operation();
 649   case vmIntrinsics::_VectorShuffleToVector:
 650     return inline_vector_shuffle_to_vector();
 651   case vmIntrinsics::_VectorLoadOp:
 652     return inline_vector_mem_operation(/*is_store=*/false);
 653   case vmIntrinsics::_VectorStoreOp:
 654     return inline_vector_mem_operation(/*is_store=*/true);
 655   case vmIntrinsics::_VectorGatherOp:
 656     return inline_vector_gather_scatter(/*is_scatter*/ false);
 657   case vmIntrinsics::_VectorScatterOp:
 658     return inline_vector_gather_scatter(/*is_scatter*/ true);
 659   case vmIntrinsics::_VectorReductionCoerced:
 660     return inline_vector_reduction();
 661   case vmIntrinsics::_VectorTest:
 662     return inline_vector_test();
 663   case vmIntrinsics::_VectorBlend:
 664     return inline_vector_blend();
 665   case vmIntrinsics::_VectorRearrange:
 666     return inline_vector_rearrange();
 667   case vmIntrinsics::_VectorCompare:
 668     return inline_vector_compare();
 669   case vmIntrinsics::_VectorBroadcastInt:
 670     return inline_vector_broadcast_int();
 671   case vmIntrinsics::_VectorConvert:
 672     return inline_vector_convert();
 673   case vmIntrinsics::_VectorInsert:
 674     return inline_vector_insert();
 675   case vmIntrinsics::_VectorExtract:
 676     return inline_vector_extract();
 677 
 678   case vmIntrinsics::_getObjectSize:
 679     return inline_getObjectSize();
 680 
 681   case vmIntrinsics::_blackhole:
 682     return inline_blackhole();
 683 
 684   default:
 685     // If you get here, it may be that someone has added a new intrinsic
 686     // to the list in vmIntrinsics.hpp without implementing it here.
 687 #ifndef PRODUCT
 688     if ((PrintMiscellaneous && (Verbose || WizardMode)) || PrintOpto) {
 689       tty->print_cr("*** Warning: Unimplemented intrinsic %s(%d)",
 690                     vmIntrinsics::name_at(intrinsic_id()), vmIntrinsics::as_int(intrinsic_id()));
 691     }
 692 #endif
 693     return false;
 694   }
 695 }
 696 
 697 Node* LibraryCallKit::try_to_predicate(int predicate) {
 698   if (!jvms()->has_method()) {
 699     // Root JVMState has a null method.
 700     assert(map()->memory()->Opcode() == Op_Parm, "");
 701     // Insert the memory aliasing node
 702     set_all_memory(reset_memory());
 703   }
 704   assert(merged_memory(), "");
 705 
 706   switch (intrinsic_id()) {
 707   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
 708     return inline_cipherBlockChaining_AESCrypt_predicate(false);
 709   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
 710     return inline_cipherBlockChaining_AESCrypt_predicate(true);
 711   case vmIntrinsics::_electronicCodeBook_encryptAESCrypt:
 712     return inline_electronicCodeBook_AESCrypt_predicate(false);
 713   case vmIntrinsics::_electronicCodeBook_decryptAESCrypt:
 714     return inline_electronicCodeBook_AESCrypt_predicate(true);
 715   case vmIntrinsics::_counterMode_AESCrypt:
 716     return inline_counterMode_AESCrypt_predicate();
 717   case vmIntrinsics::_digestBase_implCompressMB:
 718     return inline_digestBase_implCompressMB_predicate(predicate);
 719 
 720   default:
 721     // If you get here, it may be that someone has added a new intrinsic
 722     // to the list in vmIntrinsics.hpp without implementing it here.
 723 #ifndef PRODUCT
 724     if ((PrintMiscellaneous && (Verbose || WizardMode)) || PrintOpto) {
 725       tty->print_cr("*** Warning: Unimplemented predicate for intrinsic %s(%d)",
 726                     vmIntrinsics::name_at(intrinsic_id()), vmIntrinsics::as_int(intrinsic_id()));
 727     }
 728 #endif
 729     Node* slow_ctl = control();
 730     set_control(top()); // No fast path instrinsic
 731     return slow_ctl;
 732   }
 733 }
 734 
 735 //------------------------------set_result-------------------------------
 736 // Helper function for finishing intrinsics.
 737 void LibraryCallKit::set_result(RegionNode* region, PhiNode* value) {
 738   record_for_igvn(region);
 739   set_control(_gvn.transform(region));
 740   set_result( _gvn.transform(value));
 741   assert(value->type()->basic_type() == result()->bottom_type()->basic_type(), "sanity");
 742 }
 743 
 744 //------------------------------generate_guard---------------------------
 745 // Helper function for generating guarded fast-slow graph structures.
 746 // The given 'test', if true, guards a slow path.  If the test fails
 747 // then a fast path can be taken.  (We generally hope it fails.)
 748 // In all cases, GraphKit::control() is updated to the fast path.
 749 // The returned value represents the control for the slow path.
 750 // The return value is never 'top'; it is either a valid control
 751 // or null if it is obvious that the slow path can never be taken.
 752 // Also, if region and the slow control are not null, the slow edge
 753 // is appended to the region.
 754 Node* LibraryCallKit::generate_guard(Node* test, RegionNode* region, float true_prob) {
 755   if (stopped()) {
 756     // Already short circuited.
 757     return nullptr;
 758   }
 759 
 760   // Build an if node and its projections.
 761   // If test is true we take the slow path, which we assume is uncommon.
 762   if (_gvn.type(test) == TypeInt::ZERO) {
 763     // The slow branch is never taken.  No need to build this guard.
 764     return nullptr;
 765   }
 766 
 767   IfNode* iff = create_and_map_if(control(), test, true_prob, COUNT_UNKNOWN);
 768 
 769   Node* if_slow = _gvn.transform(new IfTrueNode(iff));
 770   if (if_slow == top()) {
 771     // The slow branch is never taken.  No need to build this guard.
 772     return nullptr;
 773   }
 774 
 775   if (region != nullptr)
 776     region->add_req(if_slow);
 777 
 778   Node* if_fast = _gvn.transform(new IfFalseNode(iff));
 779   set_control(if_fast);
 780 
 781   return if_slow;
 782 }
 783 
 784 inline Node* LibraryCallKit::generate_slow_guard(Node* test, RegionNode* region) {
 785   return generate_guard(test, region, PROB_UNLIKELY_MAG(3));
 786 }
 787 inline Node* LibraryCallKit::generate_fair_guard(Node* test, RegionNode* region) {
 788   return generate_guard(test, region, PROB_FAIR);
 789 }
 790 
 791 inline Node* LibraryCallKit::generate_negative_guard(Node* index, RegionNode* region,
 792                                                      Node* *pos_index) {
 793   if (stopped())
 794     return nullptr;                // already stopped
 795   if (_gvn.type(index)->higher_equal(TypeInt::POS)) // [0,maxint]
 796     return nullptr;                // index is already adequately typed
 797   Node* cmp_lt = _gvn.transform(new CmpINode(index, intcon(0)));
 798   Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
 799   Node* is_neg = generate_guard(bol_lt, region, PROB_MIN);
 800   if (is_neg != nullptr && pos_index != nullptr) {
 801     // Emulate effect of Parse::adjust_map_after_if.
 802     Node* ccast = new CastIINode(index, TypeInt::POS);
 803     ccast->set_req(0, control());
 804     (*pos_index) = _gvn.transform(ccast);
 805   }
 806   return is_neg;
 807 }
 808 
 809 // Make sure that 'position' is a valid limit index, in [0..length].
 810 // There are two equivalent plans for checking this:
 811 //   A. (offset + copyLength)  unsigned<=  arrayLength
 812 //   B. offset  <=  (arrayLength - copyLength)
 813 // We require that all of the values above, except for the sum and
 814 // difference, are already known to be non-negative.
 815 // Plan A is robust in the face of overflow, if offset and copyLength
 816 // are both hugely positive.
 817 //
 818 // Plan B is less direct and intuitive, but it does not overflow at
 819 // all, since the difference of two non-negatives is always
 820 // representable.  Whenever Java methods must perform the equivalent
 821 // check they generally use Plan B instead of Plan A.
 822 // For the moment we use Plan A.
 823 inline Node* LibraryCallKit::generate_limit_guard(Node* offset,
 824                                                   Node* subseq_length,
 825                                                   Node* array_length,
 826                                                   RegionNode* region) {
 827   if (stopped())
 828     return nullptr;                // already stopped
 829   bool zero_offset = _gvn.type(offset) == TypeInt::ZERO;
 830   if (zero_offset && subseq_length->eqv_uncast(array_length))
 831     return nullptr;                // common case of whole-array copy
 832   Node* last = subseq_length;
 833   if (!zero_offset)             // last += offset
 834     last = _gvn.transform(new AddINode(last, offset));
 835   Node* cmp_lt = _gvn.transform(new CmpUNode(array_length, last));
 836   Node* bol_lt = _gvn.transform(new BoolNode(cmp_lt, BoolTest::lt));
 837   Node* is_over = generate_guard(bol_lt, region, PROB_MIN);
 838   return is_over;
 839 }
 840 
 841 // Emit range checks for the given String.value byte array
 842 void LibraryCallKit::generate_string_range_check(Node* array, Node* offset, Node* count, bool char_count) {
 843   if (stopped()) {
 844     return; // already stopped
 845   }
 846   RegionNode* bailout = new RegionNode(1);
 847   record_for_igvn(bailout);
 848   if (char_count) {
 849     // Convert char count to byte count
 850     count = _gvn.transform(new LShiftINode(count, intcon(1)));
 851   }
 852 
 853   // Offset and count must not be negative
 854   generate_negative_guard(offset, bailout);
 855   generate_negative_guard(count, bailout);
 856   // Offset + count must not exceed length of array
 857   generate_limit_guard(offset, count, load_array_length(array), bailout);
 858 
 859   if (bailout->req() > 1) {
 860     PreserveJVMState pjvms(this);
 861     set_control(_gvn.transform(bailout));
 862     uncommon_trap(Deoptimization::Reason_intrinsic,
 863                   Deoptimization::Action_maybe_recompile);
 864   }
 865 }
 866 
 867 //--------------------------generate_current_thread--------------------
 868 Node* LibraryCallKit::generate_current_thread(Node* &tls_output) {
 869   ciKlass*    thread_klass = env()->Thread_klass();
 870   const Type* thread_type  = TypeOopPtr::make_from_klass(thread_klass)->cast_to_ptr_type(TypePtr::NotNull);
 871   Node* thread = _gvn.transform(new ThreadLocalNode());
 872   Node* p = basic_plus_adr(top()/*!oop*/, thread, in_bytes(JavaThread::threadObj_offset()));
 873   tls_output = thread;
 874   Node* thread_obj_handle = LoadNode::make(_gvn, nullptr, immutable_memory(), p, p->bottom_type()->is_ptr(), TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered);
 875   thread_obj_handle = _gvn.transform(thread_obj_handle);
 876   return access_load(thread_obj_handle, thread_type, T_OBJECT, IN_NATIVE | C2_IMMUTABLE_MEMORY);
 877 }
 878 
 879 
 880 //------------------------------make_string_method_node------------------------
 881 // Helper method for String intrinsic functions. This version is called with
 882 // str1 and str2 pointing to byte[] nodes containing Latin1 or UTF16 encoded
 883 // characters (depending on 'is_byte'). cnt1 and cnt2 are pointing to Int nodes
 884 // containing the lengths of str1 and str2.
 885 Node* LibraryCallKit::make_string_method_node(int opcode, Node* str1_start, Node* cnt1, Node* str2_start, Node* cnt2, StrIntrinsicNode::ArgEnc ae) {
 886   Node* result = nullptr;
 887   switch (opcode) {
 888   case Op_StrIndexOf:
 889     result = new StrIndexOfNode(control(), memory(TypeAryPtr::BYTES),
 890                                 str1_start, cnt1, str2_start, cnt2, ae);
 891     break;
 892   case Op_StrComp:
 893     result = new StrCompNode(control(), memory(TypeAryPtr::BYTES),
 894                              str1_start, cnt1, str2_start, cnt2, ae);
 895     break;
 896   case Op_StrEquals:
 897     // We already know that cnt1 == cnt2 here (checked in 'inline_string_equals').
 898     // Use the constant length if there is one because optimized match rule may exist.
 899     result = new StrEqualsNode(control(), memory(TypeAryPtr::BYTES),
 900                                str1_start, str2_start, cnt2->is_Con() ? cnt2 : cnt1, ae);
 901     break;
 902   default:
 903     ShouldNotReachHere();
 904     return nullptr;
 905   }
 906 
 907   // All these intrinsics have checks.
 908   C->set_has_split_ifs(true); // Has chance for split-if optimization
 909   clear_upper_avx();
 910 
 911   return _gvn.transform(result);
 912 }
 913 
 914 //------------------------------inline_string_compareTo------------------------
 915 bool LibraryCallKit::inline_string_compareTo(StrIntrinsicNode::ArgEnc ae) {
 916   Node* arg1 = argument(0);
 917   Node* arg2 = argument(1);
 918 
 919   arg1 = must_be_not_null(arg1, true);
 920   arg2 = must_be_not_null(arg2, true);
 921 
 922   // Get start addr and length of first argument
 923   Node* arg1_start  = array_element_address(arg1, intcon(0), T_BYTE);
 924   Node* arg1_cnt    = load_array_length(arg1);
 925 
 926   // Get start addr and length of second argument
 927   Node* arg2_start  = array_element_address(arg2, intcon(0), T_BYTE);
 928   Node* arg2_cnt    = load_array_length(arg2);
 929 
 930   Node* result = make_string_method_node(Op_StrComp, arg1_start, arg1_cnt, arg2_start, arg2_cnt, ae);
 931   set_result(result);
 932   return true;
 933 }
 934 
 935 //------------------------------inline_string_equals------------------------
 936 bool LibraryCallKit::inline_string_equals(StrIntrinsicNode::ArgEnc ae) {
 937   Node* arg1 = argument(0);
 938   Node* arg2 = argument(1);
 939 
 940   // paths (plus control) merge
 941   RegionNode* region = new RegionNode(3);
 942   Node* phi = new PhiNode(region, TypeInt::BOOL);
 943 
 944   if (!stopped()) {
 945 
 946     arg1 = must_be_not_null(arg1, true);
 947     arg2 = must_be_not_null(arg2, true);
 948 
 949     // Get start addr and length of first argument
 950     Node* arg1_start  = array_element_address(arg1, intcon(0), T_BYTE);
 951     Node* arg1_cnt    = load_array_length(arg1);
 952 
 953     // Get start addr and length of second argument
 954     Node* arg2_start  = array_element_address(arg2, intcon(0), T_BYTE);
 955     Node* arg2_cnt    = load_array_length(arg2);
 956 
 957     // Check for arg1_cnt != arg2_cnt
 958     Node* cmp = _gvn.transform(new CmpINode(arg1_cnt, arg2_cnt));
 959     Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
 960     Node* if_ne = generate_slow_guard(bol, nullptr);
 961     if (if_ne != nullptr) {
 962       phi->init_req(2, intcon(0));
 963       region->init_req(2, if_ne);
 964     }
 965 
 966     // Check for count == 0 is done by assembler code for StrEquals.
 967 
 968     if (!stopped()) {
 969       Node* equals = make_string_method_node(Op_StrEquals, arg1_start, arg1_cnt, arg2_start, arg2_cnt, ae);
 970       phi->init_req(1, equals);
 971       region->init_req(1, control());
 972     }
 973   }
 974 
 975   // post merge
 976   set_control(_gvn.transform(region));
 977   record_for_igvn(region);
 978 
 979   set_result(_gvn.transform(phi));
 980   return true;
 981 }
 982 
 983 //------------------------------inline_array_equals----------------------------
 984 bool LibraryCallKit::inline_array_equals(StrIntrinsicNode::ArgEnc ae) {
 985   assert(ae == StrIntrinsicNode::UU || ae == StrIntrinsicNode::LL, "unsupported array types");
 986   Node* arg1 = argument(0);
 987   Node* arg2 = argument(1);
 988 
 989   const TypeAryPtr* mtype = (ae == StrIntrinsicNode::UU) ? TypeAryPtr::CHARS : TypeAryPtr::BYTES;
 990   set_result(_gvn.transform(new AryEqNode(control(), memory(mtype), arg1, arg2, ae)));
 991   clear_upper_avx();
 992 
 993   return true;
 994 }
 995 
 996 //------------------------------inline_hasNegatives------------------------------
 997 bool LibraryCallKit::inline_hasNegatives() {
 998   if (too_many_traps(Deoptimization::Reason_intrinsic)) {
 999     return false;
1000   }
1001 
1002   assert(callee()->signature()->size() == 3, "hasNegatives has 3 parameters");
1003   // no receiver since it is static method
1004   Node* ba         = argument(0);
1005   Node* offset     = argument(1);
1006   Node* len        = argument(2);
1007 
1008   ba = must_be_not_null(ba, true);
1009 
1010   // Range checks
1011   generate_string_range_check(ba, offset, len, false);
1012   if (stopped()) {
1013     return true;
1014   }
1015   Node* ba_start = array_element_address(ba, offset, T_BYTE);
1016   Node* result = new HasNegativesNode(control(), memory(TypeAryPtr::BYTES), ba_start, len);
1017   set_result(_gvn.transform(result));
1018   return true;
1019 }
1020 
1021 bool LibraryCallKit::inline_preconditions_checkIndex(BasicType bt) {
1022   Node* index = argument(0);
1023   Node* length = bt == T_INT ? argument(1) : argument(2);
1024   if (too_many_traps(Deoptimization::Reason_intrinsic) || too_many_traps(Deoptimization::Reason_range_check)) {
1025     return false;
1026   }
1027 
1028   // check that length is positive
1029   Node* len_pos_cmp = _gvn.transform(CmpNode::make(length, integercon(0, bt), bt));
1030   Node* len_pos_bol = _gvn.transform(new BoolNode(len_pos_cmp, BoolTest::ge));
1031 
1032   {
1033     BuildCutout unless(this, len_pos_bol, PROB_MAX);
1034     uncommon_trap(Deoptimization::Reason_intrinsic,
1035                   Deoptimization::Action_make_not_entrant);
1036   }
1037 
1038   if (stopped()) {
1039     // Length is known to be always negative during compilation and the IR graph so far constructed is good so return success
1040     return true;
1041   }
1042 
1043   // length is now known postive, add a cast node to make this explicit
1044   jlong upper_bound = _gvn.type(length)->is_integer(bt)->hi_as_long();
1045   Node* casted_length = ConstraintCastNode::make(control(), length, TypeInteger::make(0, upper_bound, Type::WidenMax, bt), bt);
1046   casted_length = _gvn.transform(casted_length);
1047   replace_in_map(length, casted_length);
1048   length = casted_length;
1049 
1050   // Use an unsigned comparison for the range check itself
1051   Node* rc_cmp = _gvn.transform(CmpNode::make(index, length, bt, true));
1052   BoolTest::mask btest = BoolTest::lt;
1053   Node* rc_bool = _gvn.transform(new BoolNode(rc_cmp, btest));
1054   RangeCheckNode* rc = new RangeCheckNode(control(), rc_bool, PROB_MAX, COUNT_UNKNOWN);
1055   _gvn.set_type(rc, rc->Value(&_gvn));
1056   if (!rc_bool->is_Con()) {
1057     record_for_igvn(rc);
1058   }
1059   set_control(_gvn.transform(new IfTrueNode(rc)));
1060   {
1061     PreserveJVMState pjvms(this);
1062     set_control(_gvn.transform(new IfFalseNode(rc)));
1063     uncommon_trap(Deoptimization::Reason_range_check,
1064                   Deoptimization::Action_make_not_entrant);
1065   }
1066 
1067   if (stopped()) {
1068     // Range check is known to always fail during compilation and the IR graph so far constructed is good so return success
1069     return true;
1070   }
1071 
1072   // index is now known to be >= 0 and < length, cast it
1073   Node* result = ConstraintCastNode::make(control(), index, TypeInteger::make(0, upper_bound, Type::WidenMax, bt), bt);
1074   result = _gvn.transform(result);
1075   set_result(result);
1076   replace_in_map(index, result);
1077   return true;
1078 }
1079 
1080 //------------------------------inline_string_indexOf------------------------
1081 bool LibraryCallKit::inline_string_indexOf(StrIntrinsicNode::ArgEnc ae) {
1082   if (!Matcher::match_rule_supported(Op_StrIndexOf)) {
1083     return false;
1084   }
1085   Node* src = argument(0);
1086   Node* tgt = argument(1);
1087 
1088   // Make the merge point
1089   RegionNode* result_rgn = new RegionNode(4);
1090   Node*       result_phi = new PhiNode(result_rgn, TypeInt::INT);
1091 
1092   src = must_be_not_null(src, true);
1093   tgt = must_be_not_null(tgt, true);
1094 
1095   // Get start addr and length of source string
1096   Node* src_start = array_element_address(src, intcon(0), T_BYTE);
1097   Node* src_count = load_array_length(src);
1098 
1099   // Get start addr and length of substring
1100   Node* tgt_start = array_element_address(tgt, intcon(0), T_BYTE);
1101   Node* tgt_count = load_array_length(tgt);
1102 
1103   if (ae == StrIntrinsicNode::UU || ae == StrIntrinsicNode::UL) {
1104     // Divide src size by 2 if String is UTF16 encoded
1105     src_count = _gvn.transform(new RShiftINode(src_count, intcon(1)));
1106   }
1107   if (ae == StrIntrinsicNode::UU) {
1108     // Divide substring size by 2 if String is UTF16 encoded
1109     tgt_count = _gvn.transform(new RShiftINode(tgt_count, intcon(1)));
1110   }
1111 
1112   Node* result = make_indexOf_node(src_start, src_count, tgt_start, tgt_count, result_rgn, result_phi, ae);
1113   if (result != nullptr) {
1114     result_phi->init_req(3, result);
1115     result_rgn->init_req(3, control());
1116   }
1117   set_control(_gvn.transform(result_rgn));
1118   record_for_igvn(result_rgn);
1119   set_result(_gvn.transform(result_phi));
1120 
1121   return true;
1122 }
1123 
1124 //-----------------------------inline_string_indexOf-----------------------
1125 bool LibraryCallKit::inline_string_indexOfI(StrIntrinsicNode::ArgEnc ae) {
1126   if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1127     return false;
1128   }
1129   if (!Matcher::match_rule_supported(Op_StrIndexOf)) {
1130     return false;
1131   }
1132   assert(callee()->signature()->size() == 5, "String.indexOf() has 5 arguments");
1133   Node* src         = argument(0); // byte[]
1134   Node* src_count   = argument(1); // char count
1135   Node* tgt         = argument(2); // byte[]
1136   Node* tgt_count   = argument(3); // char count
1137   Node* from_index  = argument(4); // char index
1138 
1139   src = must_be_not_null(src, true);
1140   tgt = must_be_not_null(tgt, true);
1141 
1142   // Multiply byte array index by 2 if String is UTF16 encoded
1143   Node* src_offset = (ae == StrIntrinsicNode::LL) ? from_index : _gvn.transform(new LShiftINode(from_index, intcon(1)));
1144   src_count = _gvn.transform(new SubINode(src_count, from_index));
1145   Node* src_start = array_element_address(src, src_offset, T_BYTE);
1146   Node* tgt_start = array_element_address(tgt, intcon(0), T_BYTE);
1147 
1148   // Range checks
1149   generate_string_range_check(src, src_offset, src_count, ae != StrIntrinsicNode::LL);
1150   generate_string_range_check(tgt, intcon(0), tgt_count, ae == StrIntrinsicNode::UU);
1151   if (stopped()) {
1152     return true;
1153   }
1154 
1155   RegionNode* region = new RegionNode(5);
1156   Node* phi = new PhiNode(region, TypeInt::INT);
1157 
1158   Node* result = make_indexOf_node(src_start, src_count, tgt_start, tgt_count, region, phi, ae);
1159   if (result != nullptr) {
1160     // The result is index relative to from_index if substring was found, -1 otherwise.
1161     // Generate code which will fold into cmove.
1162     Node* cmp = _gvn.transform(new CmpINode(result, intcon(0)));
1163     Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::lt));
1164 
1165     Node* if_lt = generate_slow_guard(bol, nullptr);
1166     if (if_lt != nullptr) {
1167       // result == -1
1168       phi->init_req(3, result);
1169       region->init_req(3, if_lt);
1170     }
1171     if (!stopped()) {
1172       result = _gvn.transform(new AddINode(result, from_index));
1173       phi->init_req(4, result);
1174       region->init_req(4, control());
1175     }
1176   }
1177 
1178   set_control(_gvn.transform(region));
1179   record_for_igvn(region);
1180   set_result(_gvn.transform(phi));
1181   clear_upper_avx();
1182 
1183   return true;
1184 }
1185 
1186 // Create StrIndexOfNode with fast path checks
1187 Node* LibraryCallKit::make_indexOf_node(Node* src_start, Node* src_count, Node* tgt_start, Node* tgt_count,
1188                                         RegionNode* region, Node* phi, StrIntrinsicNode::ArgEnc ae) {
1189   // Check for substr count > string count
1190   Node* cmp = _gvn.transform(new CmpINode(tgt_count, src_count));
1191   Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::gt));
1192   Node* if_gt = generate_slow_guard(bol, nullptr);
1193   if (if_gt != nullptr) {
1194     phi->init_req(1, intcon(-1));
1195     region->init_req(1, if_gt);
1196   }
1197   if (!stopped()) {
1198     // Check for substr count == 0
1199     cmp = _gvn.transform(new CmpINode(tgt_count, intcon(0)));
1200     bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
1201     Node* if_zero = generate_slow_guard(bol, nullptr);
1202     if (if_zero != nullptr) {
1203       phi->init_req(2, intcon(0));
1204       region->init_req(2, if_zero);
1205     }
1206   }
1207   if (!stopped()) {
1208     return make_string_method_node(Op_StrIndexOf, src_start, src_count, tgt_start, tgt_count, ae);
1209   }
1210   return nullptr;
1211 }
1212 
1213 //-----------------------------inline_string_indexOfChar-----------------------
1214 bool LibraryCallKit::inline_string_indexOfChar(StrIntrinsicNode::ArgEnc ae) {
1215   if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1216     return false;
1217   }
1218   if (!Matcher::match_rule_supported(Op_StrIndexOfChar)) {
1219     return false;
1220   }
1221   assert(callee()->signature()->size() == 4, "String.indexOfChar() has 4 arguments");
1222   Node* src         = argument(0); // byte[]
1223   Node* int_ch      = argument(1);
1224   Node* from_index  = argument(2);
1225   Node* max         = argument(3);
1226 
1227   src = must_be_not_null(src, true);
1228 
1229   Node* src_offset = ae == StrIntrinsicNode::L ? from_index : _gvn.transform(new LShiftINode(from_index, intcon(1)));
1230   Node* src_start = array_element_address(src, src_offset, T_BYTE);
1231   Node* src_count = _gvn.transform(new SubINode(max, from_index));
1232 
1233   // Range checks
1234   generate_string_range_check(src, src_offset, src_count, ae == StrIntrinsicNode::U);
1235 
1236   // Check for int_ch >= 0
1237   Node* int_ch_cmp = _gvn.transform(new CmpINode(int_ch, intcon(0)));
1238   Node* int_ch_bol = _gvn.transform(new BoolNode(int_ch_cmp, BoolTest::ge));
1239   {
1240     BuildCutout unless(this, int_ch_bol, PROB_MAX);
1241     uncommon_trap(Deoptimization::Reason_intrinsic,
1242                   Deoptimization::Action_maybe_recompile);
1243   }
1244   if (stopped()) {
1245     return true;
1246   }
1247 
1248   RegionNode* region = new RegionNode(3);
1249   Node* phi = new PhiNode(region, TypeInt::INT);
1250 
1251   Node* result = new StrIndexOfCharNode(control(), memory(TypeAryPtr::BYTES), src_start, src_count, int_ch, ae);
1252   C->set_has_split_ifs(true); // Has chance for split-if optimization
1253   _gvn.transform(result);
1254 
1255   Node* cmp = _gvn.transform(new CmpINode(result, intcon(0)));
1256   Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::lt));
1257 
1258   Node* if_lt = generate_slow_guard(bol, nullptr);
1259   if (if_lt != nullptr) {
1260     // result == -1
1261     phi->init_req(2, result);
1262     region->init_req(2, if_lt);
1263   }
1264   if (!stopped()) {
1265     result = _gvn.transform(new AddINode(result, from_index));
1266     phi->init_req(1, result);
1267     region->init_req(1, control());
1268   }
1269   set_control(_gvn.transform(region));
1270   record_for_igvn(region);
1271   set_result(_gvn.transform(phi));
1272 
1273   return true;
1274 }
1275 //---------------------------inline_string_copy---------------------
1276 // compressIt == true --> generate a compressed copy operation (compress char[]/byte[] to byte[])
1277 //   int StringUTF16.compress(char[] src, int srcOff, byte[] dst, int dstOff, int len)
1278 //   int StringUTF16.compress(byte[] src, int srcOff, byte[] dst, int dstOff, int len)
1279 // compressIt == false --> generate an inflated copy operation (inflate byte[] to char[]/byte[])
1280 //   void StringLatin1.inflate(byte[] src, int srcOff, char[] dst, int dstOff, int len)
1281 //   void StringLatin1.inflate(byte[] src, int srcOff, byte[] dst, int dstOff, int len)
1282 bool LibraryCallKit::inline_string_copy(bool compress) {
1283   if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1284     return false;
1285   }
1286   int nargs = 5;  // 2 oops, 3 ints
1287   assert(callee()->signature()->size() == nargs, "string copy has 5 arguments");
1288 
1289   Node* src         = argument(0);
1290   Node* src_offset  = argument(1);
1291   Node* dst         = argument(2);
1292   Node* dst_offset  = argument(3);
1293   Node* length      = argument(4);
1294 
1295   // Check for allocation before we add nodes that would confuse
1296   // tightly_coupled_allocation()
1297   AllocateArrayNode* alloc = tightly_coupled_allocation(dst);
1298 
1299   // Figure out the size and type of the elements we will be copying.
1300   const TypeAryPtr* src_type = src->Value(&_gvn)->isa_aryptr();
1301   const TypeAryPtr* dst_type = dst->Value(&_gvn)->isa_aryptr();
1302   if (src_type == nullptr || dst_type == nullptr) {
1303     return false;
1304   }
1305   BasicType src_elem = src_type->klass()->as_array_klass()->element_type()->basic_type();
1306   BasicType dst_elem = dst_type->klass()->as_array_klass()->element_type()->basic_type();
1307   assert((compress && dst_elem == T_BYTE && (src_elem == T_BYTE || src_elem == T_CHAR)) ||
1308          (!compress && src_elem == T_BYTE && (dst_elem == T_BYTE || dst_elem == T_CHAR)),
1309          "Unsupported array types for inline_string_copy");
1310 
1311   src = must_be_not_null(src, true);
1312   dst = must_be_not_null(dst, true);
1313 
1314   // Convert char[] offsets to byte[] offsets
1315   bool convert_src = (compress && src_elem == T_BYTE);
1316   bool convert_dst = (!compress && dst_elem == T_BYTE);
1317   if (convert_src) {
1318     src_offset = _gvn.transform(new LShiftINode(src_offset, intcon(1)));
1319   } else if (convert_dst) {
1320     dst_offset = _gvn.transform(new LShiftINode(dst_offset, intcon(1)));
1321   }
1322 
1323   // Range checks
1324   generate_string_range_check(src, src_offset, length, convert_src);
1325   generate_string_range_check(dst, dst_offset, length, convert_dst);
1326   if (stopped()) {
1327     return true;
1328   }
1329 
1330   Node* src_start = array_element_address(src, src_offset, src_elem);
1331   Node* dst_start = array_element_address(dst, dst_offset, dst_elem);
1332   // 'src_start' points to src array + scaled offset
1333   // 'dst_start' points to dst array + scaled offset
1334   Node* count = nullptr;
1335   if (compress) {
1336     count = compress_string(src_start, TypeAryPtr::get_array_body_type(src_elem), dst_start, length);
1337   } else {
1338     inflate_string(src_start, dst_start, TypeAryPtr::get_array_body_type(dst_elem), length);
1339   }
1340 
1341   if (alloc != nullptr) {
1342     if (alloc->maybe_set_complete(&_gvn)) {
1343       // "You break it, you buy it."
1344       InitializeNode* init = alloc->initialization();
1345       assert(init->is_complete(), "we just did this");
1346       init->set_complete_with_arraycopy();
1347       assert(dst->is_CheckCastPP(), "sanity");
1348       assert(dst->in(0)->in(0) == init, "dest pinned");
1349     }
1350     // Do not let stores that initialize this object be reordered with
1351     // a subsequent store that would make this object accessible by
1352     // other threads.
1353     // Record what AllocateNode this StoreStore protects so that
1354     // escape analysis can go from the MemBarStoreStoreNode to the
1355     // AllocateNode and eliminate the MemBarStoreStoreNode if possible
1356     // based on the escape status of the AllocateNode.
1357     insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress));
1358   }
1359   if (compress) {
1360     set_result(_gvn.transform(count));
1361   }
1362   clear_upper_avx();
1363 
1364   return true;
1365 }
1366 
1367 #ifdef _LP64
1368 #define XTOP ,top() /*additional argument*/
1369 #else  //_LP64
1370 #define XTOP        /*no additional argument*/
1371 #endif //_LP64
1372 
1373 //------------------------inline_string_toBytesU--------------------------
1374 // public static byte[] StringUTF16.toBytes(char[] value, int off, int len)
1375 bool LibraryCallKit::inline_string_toBytesU() {
1376   if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1377     return false;
1378   }
1379   // Get the arguments.
1380   Node* value     = argument(0);
1381   Node* offset    = argument(1);
1382   Node* length    = argument(2);
1383 
1384   Node* newcopy = nullptr;
1385 
1386   // Set the original stack and the reexecute bit for the interpreter to reexecute
1387   // the bytecode that invokes StringUTF16.toBytes() if deoptimization happens.
1388   { PreserveReexecuteState preexecs(this);
1389     jvms()->set_should_reexecute(true);
1390 
1391     // Check if a null path was taken unconditionally.
1392     value = null_check(value);
1393 
1394     RegionNode* bailout = new RegionNode(1);
1395     record_for_igvn(bailout);
1396 
1397     // Range checks
1398     generate_negative_guard(offset, bailout);
1399     generate_negative_guard(length, bailout);
1400     generate_limit_guard(offset, length, load_array_length(value), bailout);
1401     // Make sure that resulting byte[] length does not overflow Integer.MAX_VALUE
1402     generate_limit_guard(length, intcon(0), intcon(max_jint/2), bailout);
1403 
1404     if (bailout->req() > 1) {
1405       PreserveJVMState pjvms(this);
1406       set_control(_gvn.transform(bailout));
1407       uncommon_trap(Deoptimization::Reason_intrinsic,
1408                     Deoptimization::Action_maybe_recompile);
1409     }
1410     if (stopped()) {
1411       return true;
1412     }
1413 
1414     Node* size = _gvn.transform(new LShiftINode(length, intcon(1)));
1415     Node* klass_node = makecon(TypeKlassPtr::make(ciTypeArrayKlass::make(T_BYTE)));
1416     newcopy = new_array(klass_node, size, 0);  // no arguments to push
1417     AllocateArrayNode* alloc = tightly_coupled_allocation(newcopy);
1418     guarantee(alloc != nullptr, "created above");
1419 
1420     // Calculate starting addresses.
1421     Node* src_start = array_element_address(value, offset, T_CHAR);
1422     Node* dst_start = basic_plus_adr(newcopy, arrayOopDesc::base_offset_in_bytes(T_BYTE));
1423 
1424     // Check if src array address is aligned to HeapWordSize (dst is always aligned)
1425     const TypeInt* toffset = gvn().type(offset)->is_int();
1426     bool aligned = toffset->is_con() && ((toffset->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0);
1427 
1428     // Figure out which arraycopy runtime method to call (disjoint, uninitialized).
1429     const char* copyfunc_name = "arraycopy";
1430     address     copyfunc_addr = StubRoutines::select_arraycopy_function(T_CHAR, aligned, true, copyfunc_name, true);
1431     Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
1432                       OptoRuntime::fast_arraycopy_Type(),
1433                       copyfunc_addr, copyfunc_name, TypeRawPtr::BOTTOM,
1434                       src_start, dst_start, ConvI2X(length) XTOP);
1435     // Do not let reads from the cloned object float above the arraycopy.
1436     if (alloc->maybe_set_complete(&_gvn)) {
1437       // "You break it, you buy it."
1438       InitializeNode* init = alloc->initialization();
1439       assert(init->is_complete(), "we just did this");
1440       init->set_complete_with_arraycopy();
1441       assert(newcopy->is_CheckCastPP(), "sanity");
1442       assert(newcopy->in(0)->in(0) == init, "dest pinned");
1443     }
1444     // Do not let stores that initialize this object be reordered with
1445     // a subsequent store that would make this object accessible by
1446     // other threads.
1447     // Record what AllocateNode this StoreStore protects so that
1448     // escape analysis can go from the MemBarStoreStoreNode to the
1449     // AllocateNode and eliminate the MemBarStoreStoreNode if possible
1450     // based on the escape status of the AllocateNode.
1451     insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress));
1452   } // original reexecute is set back here
1453 
1454   C->set_has_split_ifs(true); // Has chance for split-if optimization
1455   if (!stopped()) {
1456     set_result(newcopy);
1457   }
1458   clear_upper_avx();
1459 
1460   return true;
1461 }
1462 
1463 //------------------------inline_string_getCharsU--------------------------
1464 // public void StringUTF16.getChars(byte[] src, int srcBegin, int srcEnd, char dst[], int dstBegin)
1465 bool LibraryCallKit::inline_string_getCharsU() {
1466   if (too_many_traps(Deoptimization::Reason_intrinsic)) {
1467     return false;
1468   }
1469 
1470   // Get the arguments.
1471   Node* src       = argument(0);
1472   Node* src_begin = argument(1);
1473   Node* src_end   = argument(2); // exclusive offset (i < src_end)
1474   Node* dst       = argument(3);
1475   Node* dst_begin = argument(4);
1476 
1477   // Check for allocation before we add nodes that would confuse
1478   // tightly_coupled_allocation()
1479   AllocateArrayNode* alloc = tightly_coupled_allocation(dst);
1480 
1481   // Check if a null path was taken unconditionally.
1482   src = null_check(src);
1483   dst = null_check(dst);
1484   if (stopped()) {
1485     return true;
1486   }
1487 
1488   // Get length and convert char[] offset to byte[] offset
1489   Node* length = _gvn.transform(new SubINode(src_end, src_begin));
1490   src_begin = _gvn.transform(new LShiftINode(src_begin, intcon(1)));
1491 
1492   // Range checks
1493   generate_string_range_check(src, src_begin, length, true);
1494   generate_string_range_check(dst, dst_begin, length, false);
1495   if (stopped()) {
1496     return true;
1497   }
1498 
1499   if (!stopped()) {
1500     // Calculate starting addresses.
1501     Node* src_start = array_element_address(src, src_begin, T_BYTE);
1502     Node* dst_start = array_element_address(dst, dst_begin, T_CHAR);
1503 
1504     // Check if array addresses are aligned to HeapWordSize
1505     const TypeInt* tsrc = gvn().type(src_begin)->is_int();
1506     const TypeInt* tdst = gvn().type(dst_begin)->is_int();
1507     bool aligned = tsrc->is_con() && ((tsrc->get_con() * type2aelembytes(T_BYTE)) % HeapWordSize == 0) &&
1508                    tdst->is_con() && ((tdst->get_con() * type2aelembytes(T_CHAR)) % HeapWordSize == 0);
1509 
1510     // Figure out which arraycopy runtime method to call (disjoint, uninitialized).
1511     const char* copyfunc_name = "arraycopy";
1512     address     copyfunc_addr = StubRoutines::select_arraycopy_function(T_CHAR, aligned, true, copyfunc_name, true);
1513     Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
1514                       OptoRuntime::fast_arraycopy_Type(),
1515                       copyfunc_addr, copyfunc_name, TypeRawPtr::BOTTOM,
1516                       src_start, dst_start, ConvI2X(length) XTOP);
1517     // Do not let reads from the cloned object float above the arraycopy.
1518     if (alloc != nullptr) {
1519       if (alloc->maybe_set_complete(&_gvn)) {
1520         // "You break it, you buy it."
1521         InitializeNode* init = alloc->initialization();
1522         assert(init->is_complete(), "we just did this");
1523         init->set_complete_with_arraycopy();
1524         assert(dst->is_CheckCastPP(), "sanity");
1525         assert(dst->in(0)->in(0) == init, "dest pinned");
1526       }
1527       // Do not let stores that initialize this object be reordered with
1528       // a subsequent store that would make this object accessible by
1529       // other threads.
1530       // Record what AllocateNode this StoreStore protects so that
1531       // escape analysis can go from the MemBarStoreStoreNode to the
1532       // AllocateNode and eliminate the MemBarStoreStoreNode if possible
1533       // based on the escape status of the AllocateNode.
1534       insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress));
1535     } else {
1536       insert_mem_bar(Op_MemBarCPUOrder);
1537     }
1538   }
1539 
1540   C->set_has_split_ifs(true); // Has chance for split-if optimization
1541   return true;
1542 }
1543 
1544 //----------------------inline_string_char_access----------------------------
1545 // Store/Load char to/from byte[] array.
1546 // static void StringUTF16.putChar(byte[] val, int index, int c)
1547 // static char StringUTF16.getChar(byte[] val, int index)
1548 bool LibraryCallKit::inline_string_char_access(bool is_store) {
1549   Node* value  = argument(0);
1550   Node* index  = argument(1);
1551   Node* ch = is_store ? argument(2) : nullptr;
1552 
1553   // This intrinsic accesses byte[] array as char[] array. Computing the offsets
1554   // correctly requires matched array shapes.
1555   assert (arrayOopDesc::base_offset_in_bytes(T_CHAR) == arrayOopDesc::base_offset_in_bytes(T_BYTE),
1556           "sanity: byte[] and char[] bases agree");
1557   assert (type2aelembytes(T_CHAR) == type2aelembytes(T_BYTE)*2,
1558           "sanity: byte[] and char[] scales agree");
1559 
1560   // Bail when getChar over constants is requested: constant folding would
1561   // reject folding mismatched char access over byte[]. A normal inlining for getChar
1562   // Java method would constant fold nicely instead.
1563   if (!is_store && value->is_Con() && index->is_Con()) {
1564     return false;
1565   }
1566 
1567   // Save state and restore on bailout
1568   uint old_sp = sp();
1569   SafePointNode* old_map = clone_map();
1570 
1571   value = must_be_not_null(value, true);
1572 
1573   Node* adr = array_element_address(value, index, T_CHAR);
1574   if (adr->is_top()) {
1575     set_map(old_map);
1576     set_sp(old_sp);
1577     return false;
1578   }
1579   destruct_map_clone(old_map);
1580   if (is_store) {
1581     access_store_at(value, adr, TypeAryPtr::BYTES, ch, TypeInt::CHAR, T_CHAR, IN_HEAP | MO_UNORDERED | C2_MISMATCHED);
1582   } else {
1583     ch = access_load_at(value, adr, TypeAryPtr::BYTES, TypeInt::CHAR, T_CHAR, IN_HEAP | MO_UNORDERED | C2_MISMATCHED | C2_CONTROL_DEPENDENT_LOAD | C2_UNKNOWN_CONTROL_LOAD);
1584     set_result(ch);
1585   }
1586   return true;
1587 }
1588 
1589 //--------------------------round_double_node--------------------------------
1590 // Round a double node if necessary.
1591 Node* LibraryCallKit::round_double_node(Node* n) {
1592   if (Matcher::strict_fp_requires_explicit_rounding) {
1593 #ifdef IA32
1594     if (UseSSE < 2) {
1595       n = _gvn.transform(new RoundDoubleNode(nullptr, n));
1596     }
1597 #else
1598     Unimplemented();
1599 #endif // IA32
1600   }
1601   return n;
1602 }
1603 
1604 //------------------------------inline_math-----------------------------------
1605 // public static double Math.abs(double)
1606 // public static double Math.sqrt(double)
1607 // public static double Math.log(double)
1608 // public static double Math.log10(double)
1609 bool LibraryCallKit::inline_double_math(vmIntrinsics::ID id) {
1610   Node* arg = round_double_node(argument(0));
1611   Node* n = nullptr;
1612   switch (id) {
1613   case vmIntrinsics::_dabs:   n = new AbsDNode(                arg);  break;
1614   case vmIntrinsics::_dsqrt:  n = new SqrtDNode(C, control(),  arg);  break;
1615   case vmIntrinsics::_ceil:   n = RoundDoubleModeNode::make(_gvn, arg, RoundDoubleModeNode::rmode_ceil); break;
1616   case vmIntrinsics::_floor:  n = RoundDoubleModeNode::make(_gvn, arg, RoundDoubleModeNode::rmode_floor); break;
1617   case vmIntrinsics::_rint:   n = RoundDoubleModeNode::make(_gvn, arg, RoundDoubleModeNode::rmode_rint); break;
1618   case vmIntrinsics::_dcopySign: n = CopySignDNode::make(_gvn, arg, round_double_node(argument(2))); break;
1619   case vmIntrinsics::_dsignum: n = SignumDNode::make(_gvn, arg); break;
1620   default:  fatal_unexpected_iid(id);  break;
1621   }
1622   set_result(_gvn.transform(n));
1623   return true;
1624 }
1625 
1626 //------------------------------inline_math-----------------------------------
1627 // public static float Math.abs(float)
1628 // public static int Math.abs(int)
1629 // public static long Math.abs(long)
1630 bool LibraryCallKit::inline_math(vmIntrinsics::ID id) {
1631   Node* arg = argument(0);
1632   Node* n = nullptr;
1633   switch (id) {
1634   case vmIntrinsics::_fabs:   n = new AbsFNode(                arg);  break;
1635   case vmIntrinsics::_iabs:   n = new AbsINode(                arg);  break;
1636   case vmIntrinsics::_labs:   n = new AbsLNode(                arg);  break;
1637   case vmIntrinsics::_fcopySign: n = new CopySignFNode(arg, argument(1)); break;
1638   case vmIntrinsics::_fsignum: n = SignumFNode::make(_gvn, arg); break;
1639   default:  fatal_unexpected_iid(id);  break;
1640   }
1641   set_result(_gvn.transform(n));
1642   return true;
1643 }
1644 
1645 //------------------------------runtime_math-----------------------------
1646 bool LibraryCallKit::runtime_math(const TypeFunc* call_type, address funcAddr, const char* funcName) {
1647   assert(call_type == OptoRuntime::Math_DD_D_Type() || call_type == OptoRuntime::Math_D_D_Type(),
1648          "must be (DD)D or (D)D type");
1649 
1650   // Inputs
1651   Node* a = round_double_node(argument(0));
1652   Node* b = (call_type == OptoRuntime::Math_DD_D_Type()) ? round_double_node(argument(2)) : nullptr;
1653 
1654   const TypePtr* no_memory_effects = nullptr;
1655   Node* trig = make_runtime_call(RC_LEAF, call_type, funcAddr, funcName,
1656                                  no_memory_effects,
1657                                  a, top(), b, b ? top() : nullptr);
1658   Node* value = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+0));
1659 #ifdef ASSERT
1660   Node* value_top = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+1));
1661   assert(value_top == top(), "second value must be top");
1662 #endif
1663 
1664   set_result(value);
1665   return true;
1666 }
1667 
1668 //------------------------------inline_math_pow-----------------------------
1669 bool LibraryCallKit::inline_math_pow() {
1670   Node* exp = round_double_node(argument(2));
1671   const TypeD* d = _gvn.type(exp)->isa_double_constant();
1672   if (d != nullptr) {
1673     if (d->getd() == 2.0) {
1674       // Special case: pow(x, 2.0) => x * x
1675       Node* base = round_double_node(argument(0));
1676       set_result(_gvn.transform(new MulDNode(base, base)));
1677       return true;
1678     } else if (d->getd() == 0.5 && Matcher::match_rule_supported(Op_SqrtD)) {
1679       // Special case: pow(x, 0.5) => sqrt(x)
1680       Node* base = round_double_node(argument(0));
1681       Node* zero = _gvn.zerocon(T_DOUBLE);
1682 
1683       RegionNode* region = new RegionNode(3);
1684       Node* phi = new PhiNode(region, Type::DOUBLE);
1685 
1686       Node* cmp  = _gvn.transform(new CmpDNode(base, zero));
1687       // According to the API specs, pow(-0.0, 0.5) = 0.0 and sqrt(-0.0) = -0.0.
1688       // So pow(-0.0, 0.5) shouldn't be replaced with sqrt(-0.0).
1689       // -0.0/+0.0 are both excluded since floating-point comparison doesn't distinguish -0.0 from +0.0.
1690       Node* test = _gvn.transform(new BoolNode(cmp, BoolTest::le));
1691 
1692       Node* if_pow = generate_slow_guard(test, nullptr);
1693       Node* value_sqrt = _gvn.transform(new SqrtDNode(C, control(), base));
1694       phi->init_req(1, value_sqrt);
1695       region->init_req(1, control());
1696 
1697       if (if_pow != nullptr) {
1698         set_control(if_pow);
1699         address target = StubRoutines::dpow() != nullptr ? StubRoutines::dpow() :
1700                                                         CAST_FROM_FN_PTR(address, SharedRuntime::dpow);
1701         const TypePtr* no_memory_effects = nullptr;
1702         Node* trig = make_runtime_call(RC_LEAF, OptoRuntime::Math_DD_D_Type(), target, "POW",
1703                                        no_memory_effects, base, top(), exp, top());
1704         Node* value_pow = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+0));
1705 #ifdef ASSERT
1706         Node* value_top = _gvn.transform(new ProjNode(trig, TypeFunc::Parms+1));
1707         assert(value_top == top(), "second value must be top");
1708 #endif
1709         phi->init_req(2, value_pow);
1710         region->init_req(2, _gvn.transform(new ProjNode(trig, TypeFunc::Control)));
1711       }
1712 
1713       C->set_has_split_ifs(true); // Has chance for split-if optimization
1714       set_control(_gvn.transform(region));
1715       record_for_igvn(region);
1716       set_result(_gvn.transform(phi));
1717 
1718       return true;
1719     }
1720   }
1721 
1722   return StubRoutines::dpow() != nullptr ?
1723     runtime_math(OptoRuntime::Math_DD_D_Type(), StubRoutines::dpow(),  "dpow") :
1724     runtime_math(OptoRuntime::Math_DD_D_Type(), CAST_FROM_FN_PTR(address, SharedRuntime::dpow),  "POW");
1725 }
1726 
1727 //------------------------------inline_math_native-----------------------------
1728 bool LibraryCallKit::inline_math_native(vmIntrinsics::ID id) {
1729 #define FN_PTR(f) CAST_FROM_FN_PTR(address, f)
1730   switch (id) {
1731     // These intrinsics are not properly supported on all hardware
1732   case vmIntrinsics::_dsin:
1733     return StubRoutines::dsin() != nullptr ?
1734       runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dsin(), "dsin") :
1735       runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dsin),   "SIN");
1736   case vmIntrinsics::_dcos:
1737     return StubRoutines::dcos() != nullptr ?
1738       runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dcos(), "dcos") :
1739       runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dcos),   "COS");
1740   case vmIntrinsics::_dtan:
1741     return StubRoutines::dtan() != nullptr ?
1742       runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dtan(), "dtan") :
1743       runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dtan), "TAN");
1744   case vmIntrinsics::_dlog:
1745     return StubRoutines::dlog() != nullptr ?
1746       runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dlog(), "dlog") :
1747       runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dlog),   "LOG");
1748   case vmIntrinsics::_dlog10:
1749     return StubRoutines::dlog10() != nullptr ?
1750       runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dlog10(), "dlog10") :
1751       runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dlog10), "LOG10");
1752 
1753     // These intrinsics are supported on all hardware
1754   case vmIntrinsics::_ceil:
1755   case vmIntrinsics::_floor:
1756   case vmIntrinsics::_rint:   return Matcher::match_rule_supported(Op_RoundDoubleMode) ? inline_double_math(id) : false;
1757   case vmIntrinsics::_dsqrt:  return Matcher::match_rule_supported(Op_SqrtD) ? inline_double_math(id) : false;
1758   case vmIntrinsics::_dabs:   return Matcher::has_match_rule(Op_AbsD)   ? inline_double_math(id) : false;
1759   case vmIntrinsics::_fabs:   return Matcher::match_rule_supported(Op_AbsF)   ? inline_math(id) : false;
1760   case vmIntrinsics::_iabs:   return Matcher::match_rule_supported(Op_AbsI)   ? inline_math(id) : false;
1761   case vmIntrinsics::_labs:   return Matcher::match_rule_supported(Op_AbsL)   ? inline_math(id) : false;
1762 
1763   case vmIntrinsics::_dexp:
1764     return StubRoutines::dexp() != nullptr ?
1765       runtime_math(OptoRuntime::Math_D_D_Type(), StubRoutines::dexp(),  "dexp") :
1766       runtime_math(OptoRuntime::Math_D_D_Type(), FN_PTR(SharedRuntime::dexp),  "EXP");
1767 #undef FN_PTR
1768 
1769   case vmIntrinsics::_dpow:      return inline_math_pow();
1770   case vmIntrinsics::_dcopySign: return inline_double_math(id);
1771   case vmIntrinsics::_fcopySign: return inline_math(id);
1772   case vmIntrinsics::_dsignum: return Matcher::match_rule_supported(Op_SignumD) ? inline_double_math(id) : false;
1773   case vmIntrinsics::_fsignum: return Matcher::match_rule_supported(Op_SignumF) ? inline_math(id) : false;
1774 
1775    // These intrinsics are not yet correctly implemented
1776   case vmIntrinsics::_datan2:
1777     return false;
1778 
1779   default:
1780     fatal_unexpected_iid(id);
1781     return false;
1782   }
1783 }
1784 
1785 static bool is_simple_name(Node* n) {
1786   return (n->req() == 1         // constant
1787           || (n->is_Type() && n->as_Type()->type()->singleton())
1788           || n->is_Proj()       // parameter or return value
1789           || n->is_Phi()        // local of some sort
1790           );
1791 }
1792 
1793 //----------------------------inline_notify-----------------------------------*
1794 bool LibraryCallKit::inline_notify(vmIntrinsics::ID id) {
1795   const TypeFunc* ftype = OptoRuntime::monitor_notify_Type();
1796   address func;
1797   if (id == vmIntrinsics::_notify) {
1798     func = OptoRuntime::monitor_notify_Java();
1799   } else {
1800     func = OptoRuntime::monitor_notifyAll_Java();
1801   }
1802   Node* call = make_runtime_call(RC_NO_LEAF, ftype, func, nullptr, TypeRawPtr::BOTTOM, argument(0));
1803   make_slow_call_ex(call, env()->Throwable_klass(), false);
1804   return true;
1805 }
1806 
1807 
1808 //----------------------------inline_min_max-----------------------------------
1809 bool LibraryCallKit::inline_min_max(vmIntrinsics::ID id) {
1810   set_result(generate_min_max(id, argument(0), argument(1)));
1811   return true;
1812 }
1813 
1814 void LibraryCallKit::inline_math_mathExact(Node* math, Node *test) {
1815   Node* bol = _gvn.transform( new BoolNode(test, BoolTest::overflow) );
1816   IfNode* check = create_and_map_if(control(), bol, PROB_UNLIKELY_MAG(3), COUNT_UNKNOWN);
1817   Node* fast_path = _gvn.transform( new IfFalseNode(check));
1818   Node* slow_path = _gvn.transform( new IfTrueNode(check) );
1819 
1820   {
1821     PreserveJVMState pjvms(this);
1822     PreserveReexecuteState preexecs(this);
1823     jvms()->set_should_reexecute(true);
1824 
1825     set_control(slow_path);
1826     set_i_o(i_o());
1827 
1828     uncommon_trap(Deoptimization::Reason_intrinsic,
1829                   Deoptimization::Action_none);
1830   }
1831 
1832   set_control(fast_path);
1833   set_result(math);
1834 }
1835 
1836 template <typename OverflowOp>
1837 bool LibraryCallKit::inline_math_overflow(Node* arg1, Node* arg2) {
1838   typedef typename OverflowOp::MathOp MathOp;
1839 
1840   MathOp* mathOp = new MathOp(arg1, arg2);
1841   Node* operation = _gvn.transform( mathOp );
1842   Node* ofcheck = _gvn.transform( new OverflowOp(arg1, arg2) );
1843   inline_math_mathExact(operation, ofcheck);
1844   return true;
1845 }
1846 
1847 bool LibraryCallKit::inline_math_addExactI(bool is_increment) {
1848   return inline_math_overflow<OverflowAddINode>(argument(0), is_increment ? intcon(1) : argument(1));
1849 }
1850 
1851 bool LibraryCallKit::inline_math_addExactL(bool is_increment) {
1852   return inline_math_overflow<OverflowAddLNode>(argument(0), is_increment ? longcon(1) : argument(2));
1853 }
1854 
1855 bool LibraryCallKit::inline_math_subtractExactI(bool is_decrement) {
1856   return inline_math_overflow<OverflowSubINode>(argument(0), is_decrement ? intcon(1) : argument(1));
1857 }
1858 
1859 bool LibraryCallKit::inline_math_subtractExactL(bool is_decrement) {
1860   return inline_math_overflow<OverflowSubLNode>(argument(0), is_decrement ? longcon(1) : argument(2));
1861 }
1862 
1863 bool LibraryCallKit::inline_math_negateExactI() {
1864   return inline_math_overflow<OverflowSubINode>(intcon(0), argument(0));
1865 }
1866 
1867 bool LibraryCallKit::inline_math_negateExactL() {
1868   return inline_math_overflow<OverflowSubLNode>(longcon(0), argument(0));
1869 }
1870 
1871 bool LibraryCallKit::inline_math_multiplyExactI() {
1872   return inline_math_overflow<OverflowMulINode>(argument(0), argument(1));
1873 }
1874 
1875 bool LibraryCallKit::inline_math_multiplyExactL() {
1876   return inline_math_overflow<OverflowMulLNode>(argument(0), argument(2));
1877 }
1878 
1879 bool LibraryCallKit::inline_math_multiplyHigh() {
1880   set_result(_gvn.transform(new MulHiLNode(argument(0), argument(2))));
1881   return true;
1882 }
1883 
1884 Node*
1885 LibraryCallKit::generate_min_max(vmIntrinsics::ID id, Node* x0, Node* y0) {
1886   // These are the candidate return value:
1887   Node* xvalue = x0;
1888   Node* yvalue = y0;
1889 
1890   if (xvalue == yvalue) {
1891     return xvalue;
1892   }
1893 
1894   bool want_max = (id == vmIntrinsics::_max);
1895 
1896   const TypeInt* txvalue = _gvn.type(xvalue)->isa_int();
1897   const TypeInt* tyvalue = _gvn.type(yvalue)->isa_int();
1898   if (txvalue == NULL || tyvalue == NULL)  return top();
1899   // This is not really necessary, but it is consistent with a
1900   // hypothetical MaxINode::Value method:
1901   int widen = MAX2(txvalue->_widen, tyvalue->_widen);
1902 
1903   // %%% This folding logic should (ideally) be in a different place.
1904   // Some should be inside IfNode, and there to be a more reliable
1905   // transformation of ?: style patterns into cmoves.  We also want
1906   // more powerful optimizations around cmove and min/max.
1907 
1908   // Try to find a dominating comparison of these guys.
1909   // It can simplify the index computation for Arrays.copyOf
1910   // and similar uses of System.arraycopy.
1911   // First, compute the normalized version of CmpI(x, y).
1912   int   cmp_op = Op_CmpI;
1913   Node* xkey = xvalue;
1914   Node* ykey = yvalue;
1915   Node* ideal_cmpxy = _gvn.transform(new CmpINode(xkey, ykey));
1916   if (ideal_cmpxy->is_Cmp()) {
1917     // E.g., if we have CmpI(length - offset, count),
1918     // it might idealize to CmpI(length, count + offset)
1919     cmp_op = ideal_cmpxy->Opcode();
1920     xkey = ideal_cmpxy->in(1);
1921     ykey = ideal_cmpxy->in(2);
1922   }
1923 
1924   // Start by locating any relevant comparisons.
1925   Node* start_from = (xkey->outcnt() < ykey->outcnt()) ? xkey : ykey;
1926   Node* cmpxy = NULL;
1927   Node* cmpyx = NULL;
1928   for (DUIterator_Fast kmax, k = start_from->fast_outs(kmax); k < kmax; k++) {
1929     Node* cmp = start_from->fast_out(k);
1930     if (cmp->outcnt() > 0 &&            // must have prior uses
1931         cmp->in(0) == NULL &&           // must be context-independent
1932         cmp->Opcode() == cmp_op) {      // right kind of compare
1933       if (cmp->in(1) == xkey && cmp->in(2) == ykey)  cmpxy = cmp;
1934       if (cmp->in(1) == ykey && cmp->in(2) == xkey)  cmpyx = cmp;
1935     }
1936   }
1937 
1938   const int NCMPS = 2;
1939   Node* cmps[NCMPS] = { cmpxy, cmpyx };
1940   int cmpn;
1941   for (cmpn = 0; cmpn < NCMPS; cmpn++) {
1942     if (cmps[cmpn] != NULL)  break;     // find a result
1943   }
1944   if (cmpn < NCMPS) {
1945     // Look for a dominating test that tells us the min and max.
1946     int depth = 0;                // Limit search depth for speed
1947     Node* dom = control();
1948     for (; dom != NULL; dom = IfNode::up_one_dom(dom, true)) {
1949       if (++depth >= 100)  break;
1950       Node* ifproj = dom;
1951       if (!ifproj->is_Proj())  continue;
1952       Node* iff = ifproj->in(0);
1953       if (!iff->is_If())  continue;
1954       Node* bol = iff->in(1);
1955       if (!bol->is_Bool())  continue;
1956       Node* cmp = bol->in(1);
1957       if (cmp == NULL)  continue;
1958       for (cmpn = 0; cmpn < NCMPS; cmpn++)
1959         if (cmps[cmpn] == cmp)  break;
1960       if (cmpn == NCMPS)  continue;
1961       BoolTest::mask btest = bol->as_Bool()->_test._test;
1962       if (ifproj->is_IfFalse())  btest = BoolTest(btest).negate();
1963       if (cmp->in(1) == ykey)    btest = BoolTest(btest).commute();
1964       // At this point, we know that 'x btest y' is true.
1965       switch (btest) {
1966       case BoolTest::eq:
1967         // They are proven equal, so we can collapse the min/max.
1968         // Either value is the answer.  Choose the simpler.
1969         if (is_simple_name(yvalue) && !is_simple_name(xvalue))
1970           return yvalue;
1971         return xvalue;
1972       case BoolTest::lt:          // x < y
1973       case BoolTest::le:          // x <= y
1974         return (want_max ? yvalue : xvalue);
1975       case BoolTest::gt:          // x > y
1976       case BoolTest::ge:          // x >= y
1977         return (want_max ? xvalue : yvalue);
1978       default:
1979         break;
1980       }
1981     }
1982   }
1983 
1984   // We failed to find a dominating test.
1985   // Let's pick a test that might GVN with prior tests.
1986   Node*          best_bol   = NULL;
1987   BoolTest::mask best_btest = BoolTest::illegal;
1988   for (cmpn = 0; cmpn < NCMPS; cmpn++) {
1989     Node* cmp = cmps[cmpn];
1990     if (cmp == NULL)  continue;
1991     for (DUIterator_Fast jmax, j = cmp->fast_outs(jmax); j < jmax; j++) {
1992       Node* bol = cmp->fast_out(j);
1993       if (!bol->is_Bool())  continue;
1994       BoolTest::mask btest = bol->as_Bool()->_test._test;
1995       if (btest == BoolTest::eq || btest == BoolTest::ne)  continue;
1996       if (cmp->in(1) == ykey)   btest = BoolTest(btest).commute();
1997       if (bol->outcnt() > (best_bol == NULL ? 0 : best_bol->outcnt())) {
1998         best_bol   = bol->as_Bool();
1999         best_btest = btest;
2000       }
2001     }
2002   }
2003 
2004   Node* answer_if_true  = NULL;
2005   Node* answer_if_false = NULL;
2006   switch (best_btest) {
2007   default:
2008     if (cmpxy == NULL)
2009       cmpxy = ideal_cmpxy;
2010     best_bol = _gvn.transform(new BoolNode(cmpxy, BoolTest::lt));
2011     // and fall through:
2012   case BoolTest::lt:          // x < y
2013   case BoolTest::le:          // x <= y
2014     answer_if_true  = (want_max ? yvalue : xvalue);
2015     answer_if_false = (want_max ? xvalue : yvalue);
2016     break;
2017   case BoolTest::gt:          // x > y
2018   case BoolTest::ge:          // x >= y
2019     answer_if_true  = (want_max ? xvalue : yvalue);
2020     answer_if_false = (want_max ? yvalue : xvalue);
2021     break;
2022   }
2023 
2024   jint hi, lo;
2025   if (want_max) {
2026     // We can sharpen the minimum.
2027     hi = MAX2(txvalue->_hi, tyvalue->_hi);
2028     lo = MAX2(txvalue->_lo, tyvalue->_lo);
2029   } else {
2030     // We can sharpen the maximum.
2031     hi = MIN2(txvalue->_hi, tyvalue->_hi);
2032     lo = MIN2(txvalue->_lo, tyvalue->_lo);
2033   }
2034 
2035   // Use a flow-free graph structure, to avoid creating excess control edges
2036   // which could hinder other optimizations.
2037   // Since Math.min/max is often used with arraycopy, we want
2038   // tightly_coupled_allocation to be able to see beyond min/max expressions.
2039   Node* cmov = CMoveNode::make(NULL, best_bol,
2040                                answer_if_false, answer_if_true,
2041                                TypeInt::make(lo, hi, widen));
2042 
2043   return _gvn.transform(cmov);
2044 
2045   /*
2046   // This is not as desirable as it may seem, since Min and Max
2047   // nodes do not have a full set of optimizations.
2048   // And they would interfere, anyway, with 'if' optimizations
2049   // and with CMoveI canonical forms.
2050   switch (id) {
2051   case vmIntrinsics::_min:
2052     result_val = _gvn.transform(new (C, 3) MinINode(x,y)); break;
2053   case vmIntrinsics::_max:
2054     result_val = _gvn.transform(new (C, 3) MaxINode(x,y)); break;
2055   default:
2056     ShouldNotReachHere();
2057   }
2058   */
2059 }
2060 
2061 inline int
2062 LibraryCallKit::classify_unsafe_addr(Node* &base, Node* &offset, BasicType type) {
2063   const TypePtr* base_type = TypePtr::NULL_PTR;
2064   if (base != nullptr)  base_type = _gvn.type(base)->isa_ptr();
2065   if (base_type == nullptr) {
2066     // Unknown type.
2067     return Type::AnyPtr;
2068   } else if (base_type == TypePtr::NULL_PTR) {
2069     // Since this is a null+long form, we have to switch to a rawptr.
2070     base   = _gvn.transform(new CastX2PNode(offset));
2071     offset = MakeConX(0);
2072     return Type::RawPtr;
2073   } else if (base_type->base() == Type::RawPtr) {
2074     return Type::RawPtr;
2075   } else if (base_type->isa_oopptr()) {
2076     // Base is never null => always a heap address.
2077     if (!TypePtr::NULL_PTR->higher_equal(base_type)) {
2078       return Type::OopPtr;
2079     }
2080     // Offset is small => always a heap address.
2081     const TypeX* offset_type = _gvn.type(offset)->isa_intptr_t();
2082     if (offset_type != nullptr &&
2083         base_type->offset() == 0 &&     // (should always be?)
2084         offset_type->_lo >= 0 &&
2085         !MacroAssembler::needs_explicit_null_check(offset_type->_hi)) {
2086       return Type::OopPtr;
2087     } else if (type == T_OBJECT) {
2088       // off heap access to an oop doesn't make any sense. Has to be on
2089       // heap.
2090       return Type::OopPtr;
2091     }
2092     // Otherwise, it might either be oop+off or null+addr.
2093     return Type::AnyPtr;
2094   } else {
2095     // No information:
2096     return Type::AnyPtr;
2097   }
2098 }
2099 
2100 Node* LibraryCallKit::make_unsafe_address(Node*& base, Node* offset, BasicType type, bool can_cast) {
2101   Node* uncasted_base = base;
2102   int kind = classify_unsafe_addr(uncasted_base, offset, type);
2103   if (kind == Type::RawPtr) {
2104     return basic_plus_adr(top(), uncasted_base, offset);
2105   } else if (kind == Type::AnyPtr) {
2106     assert(base == uncasted_base, "unexpected base change");
2107     if (can_cast) {
2108       if (!_gvn.type(base)->speculative_maybe_null() &&
2109           !too_many_traps(Deoptimization::Reason_speculate_null_check)) {
2110         // According to profiling, this access is always on
2111         // heap. Casting the base to not null and thus avoiding membars
2112         // around the access should allow better optimizations
2113         Node* null_ctl = top();
2114         base = null_check_oop(base, &null_ctl, true, true, true);
2115         assert(null_ctl->is_top(), "no null control here");
2116         return basic_plus_adr(base, offset);
2117       } else if (_gvn.type(base)->speculative_always_null() &&
2118                  !too_many_traps(Deoptimization::Reason_speculate_null_assert)) {
2119         // According to profiling, this access is always off
2120         // heap.
2121         base = null_assert(base);
2122         Node* raw_base = _gvn.transform(new CastX2PNode(offset));
2123         offset = MakeConX(0);
2124         return basic_plus_adr(top(), raw_base, offset);
2125       }
2126     }
2127     // We don't know if it's an on heap or off heap access. Fall back
2128     // to raw memory access.
2129     Node* raw = _gvn.transform(new CheckCastPPNode(control(), base, TypeRawPtr::BOTTOM));
2130     return basic_plus_adr(top(), raw, offset);
2131   } else {
2132     assert(base == uncasted_base, "unexpected base change");
2133     // We know it's an on heap access so base can't be null
2134     if (TypePtr::NULL_PTR->higher_equal(_gvn.type(base))) {
2135       base = must_be_not_null(base, true);
2136     }
2137     return basic_plus_adr(base, offset);
2138   }
2139 }
2140 
2141 //--------------------------inline_number_methods-----------------------------
2142 // inline int     Integer.numberOfLeadingZeros(int)
2143 // inline int        Long.numberOfLeadingZeros(long)
2144 //
2145 // inline int     Integer.numberOfTrailingZeros(int)
2146 // inline int        Long.numberOfTrailingZeros(long)
2147 //
2148 // inline int     Integer.bitCount(int)
2149 // inline int        Long.bitCount(long)
2150 //
2151 // inline char  Character.reverseBytes(char)
2152 // inline short     Short.reverseBytes(short)
2153 // inline int     Integer.reverseBytes(int)
2154 // inline long       Long.reverseBytes(long)
2155 bool LibraryCallKit::inline_number_methods(vmIntrinsics::ID id) {
2156   Node* arg = argument(0);
2157   Node* n = nullptr;
2158   switch (id) {
2159   case vmIntrinsics::_numberOfLeadingZeros_i:   n = new CountLeadingZerosINode( arg);  break;
2160   case vmIntrinsics::_numberOfLeadingZeros_l:   n = new CountLeadingZerosLNode( arg);  break;
2161   case vmIntrinsics::_numberOfTrailingZeros_i:  n = new CountTrailingZerosINode(arg);  break;
2162   case vmIntrinsics::_numberOfTrailingZeros_l:  n = new CountTrailingZerosLNode(arg);  break;
2163   case vmIntrinsics::_bitCount_i:               n = new PopCountINode(          arg);  break;
2164   case vmIntrinsics::_bitCount_l:               n = new PopCountLNode(          arg);  break;
2165   case vmIntrinsics::_reverseBytes_c:           n = new ReverseBytesUSNode(0,   arg);  break;
2166   case vmIntrinsics::_reverseBytes_s:           n = new ReverseBytesSNode( 0,   arg);  break;
2167   case vmIntrinsics::_reverseBytes_i:           n = new ReverseBytesINode( 0,   arg);  break;
2168   case vmIntrinsics::_reverseBytes_l:           n = new ReverseBytesLNode( 0,   arg);  break;
2169   default:  fatal_unexpected_iid(id);  break;
2170   }
2171   set_result(_gvn.transform(n));
2172   return true;
2173 }
2174 
2175 //----------------------------inline_unsafe_access----------------------------
2176 
2177 const TypeOopPtr* LibraryCallKit::sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type) {
2178   // Attempt to infer a sharper value type from the offset and base type.
2179   ciKlass* sharpened_klass = nullptr;
2180 
2181   // See if it is an instance field, with an object type.
2182   if (alias_type->field() != nullptr) {
2183     if (alias_type->field()->type()->is_klass()) {
2184       sharpened_klass = alias_type->field()->type()->as_klass();
2185     }
2186   }
2187 
2188   // See if it is a narrow oop array.
2189   if (adr_type->isa_aryptr()) {
2190     if (adr_type->offset() >= objArrayOopDesc::base_offset_in_bytes()) {
2191       const TypeOopPtr *elem_type = adr_type->is_aryptr()->elem()->isa_oopptr();
2192       if (elem_type != nullptr) {
2193         sharpened_klass = elem_type->klass();
2194       }
2195     }
2196   }
2197 
2198   // The sharpened class might be unloaded if there is no class loader
2199   // contraint in place.
2200   if (sharpened_klass != nullptr && sharpened_klass->is_loaded()) {
2201     const TypeOopPtr* tjp = TypeOopPtr::make_from_klass(sharpened_klass);
2202 
2203 #ifndef PRODUCT
2204     if (C->print_intrinsics() || C->print_inlining()) {
2205       tty->print("  from base type:  ");  adr_type->dump(); tty->cr();
2206       tty->print("  sharpened value: ");  tjp->dump();      tty->cr();
2207     }
2208 #endif
2209     // Sharpen the value type.
2210     return tjp;
2211   }
2212   return nullptr;
2213 }
2214 
2215 DecoratorSet LibraryCallKit::mo_decorator_for_access_kind(AccessKind kind) {
2216   switch (kind) {
2217       case Relaxed:
2218         return MO_UNORDERED;
2219       case Opaque:
2220         return MO_RELAXED;
2221       case Acquire:
2222         return MO_ACQUIRE;
2223       case Release:
2224         return MO_RELEASE;
2225       case Volatile:
2226         return MO_SEQ_CST;
2227       default:
2228         ShouldNotReachHere();
2229         return 0;
2230   }
2231 }
2232 
2233 bool LibraryCallKit::inline_unsafe_access(bool is_store, const BasicType type, const AccessKind kind, const bool unaligned) {
2234   if (callee()->is_static())  return false;  // caller must have the capability!
2235   DecoratorSet decorators = C2_UNSAFE_ACCESS;
2236   guarantee(!is_store || kind != Acquire, "Acquire accesses can be produced only for loads");
2237   guarantee( is_store || kind != Release, "Release accesses can be produced only for stores");
2238   assert(type != T_OBJECT || !unaligned, "unaligned access not supported with object type");
2239 
2240   if (is_reference_type(type)) {
2241     decorators |= ON_UNKNOWN_OOP_REF;
2242   }
2243 
2244   if (unaligned) {
2245     decorators |= C2_UNALIGNED;
2246   }
2247 
2248 #ifndef PRODUCT
2249   {
2250     ResourceMark rm;
2251     // Check the signatures.
2252     ciSignature* sig = callee()->signature();
2253 #ifdef ASSERT
2254     if (!is_store) {
2255       // Object getReference(Object base, int/long offset), etc.
2256       BasicType rtype = sig->return_type()->basic_type();
2257       assert(rtype == type, "getter must return the expected value");
2258       assert(sig->count() == 2, "oop getter has 2 arguments");
2259       assert(sig->type_at(0)->basic_type() == T_OBJECT, "getter base is object");
2260       assert(sig->type_at(1)->basic_type() == T_LONG, "getter offset is correct");
2261     } else {
2262       // void putReference(Object base, int/long offset, Object x), etc.
2263       assert(sig->return_type()->basic_type() == T_VOID, "putter must not return a value");
2264       assert(sig->count() == 3, "oop putter has 3 arguments");
2265       assert(sig->type_at(0)->basic_type() == T_OBJECT, "putter base is object");
2266       assert(sig->type_at(1)->basic_type() == T_LONG, "putter offset is correct");
2267       BasicType vtype = sig->type_at(sig->count()-1)->basic_type();
2268       assert(vtype == type, "putter must accept the expected value");
2269     }
2270 #endif // ASSERT
2271  }
2272 #endif //PRODUCT
2273 
2274   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2275 
2276   Node* receiver = argument(0);  // type: oop
2277 
2278   // Build address expression.
2279   Node* heap_base_oop = top();
2280 
2281   // The base is either a Java object or a value produced by Unsafe.staticFieldBase
2282   Node* base = argument(1);  // type: oop
2283   // The offset is a value produced by Unsafe.staticFieldOffset or Unsafe.objectFieldOffset
2284   Node* offset = argument(2);  // type: long
2285   // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2286   // to be plain byte offsets, which are also the same as those accepted
2287   // by oopDesc::field_addr.
2288   assert(Unsafe_field_offset_to_byte_offset(11) == 11,
2289          "fieldOffset must be byte-scaled");
2290   // 32-bit machines ignore the high half!
2291   offset = ConvL2X(offset);
2292 
2293   // Save state and restore on bailout
2294   uint old_sp = sp();
2295   SafePointNode* old_map = clone_map();
2296 
2297   Node* adr = make_unsafe_address(base, offset, type, kind == Relaxed);
2298 
2299   if (_gvn.type(base)->isa_ptr() == TypePtr::NULL_PTR) {
2300     if (type != T_OBJECT) {
2301       decorators |= IN_NATIVE; // off-heap primitive access
2302     } else {
2303       set_map(old_map);
2304       set_sp(old_sp);
2305       return false; // off-heap oop accesses are not supported
2306     }
2307   } else {
2308     heap_base_oop = base; // on-heap or mixed access
2309   }
2310 
2311   // Can base be null? Otherwise, always on-heap access.
2312   bool can_access_non_heap = TypePtr::NULL_PTR->higher_equal(_gvn.type(base));
2313 
2314   if (!can_access_non_heap) {
2315     decorators |= IN_HEAP;
2316   }
2317 
2318   Node* val = is_store ? argument(4) : nullptr;
2319 
2320   const TypePtr* adr_type = _gvn.type(adr)->isa_ptr();
2321   if (adr_type == TypePtr::NULL_PTR) {
2322     set_map(old_map);
2323     set_sp(old_sp);
2324     return false; // off-heap access with zero address
2325   }
2326 
2327   // Try to categorize the address.
2328   Compile::AliasType* alias_type = C->alias_type(adr_type);
2329   assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2330 
2331   if (alias_type->adr_type() == TypeInstPtr::KLASS ||
2332       alias_type->adr_type() == TypeAryPtr::RANGE) {
2333     set_map(old_map);
2334     set_sp(old_sp);
2335     return false; // not supported
2336   }
2337 
2338   bool mismatched = false;
2339   BasicType bt = alias_type->basic_type();
2340   if (bt != T_ILLEGAL) {
2341     assert(alias_type->adr_type()->is_oopptr(), "should be on-heap access");
2342     if (bt == T_BYTE && adr_type->isa_aryptr()) {
2343       // Alias type doesn't differentiate between byte[] and boolean[]).
2344       // Use address type to get the element type.
2345       bt = adr_type->is_aryptr()->elem()->array_element_basic_type();
2346     }
2347     if (bt == T_ARRAY || bt == T_NARROWOOP) {
2348       // accessing an array field with getReference is not a mismatch
2349       bt = T_OBJECT;
2350     }
2351     if ((bt == T_OBJECT) != (type == T_OBJECT)) {
2352       // Don't intrinsify mismatched object accesses
2353       set_map(old_map);
2354       set_sp(old_sp);
2355       return false;
2356     }
2357     mismatched = (bt != type);
2358   } else if (alias_type->adr_type()->isa_oopptr()) {
2359     mismatched = true; // conservatively mark all "wide" on-heap accesses as mismatched
2360   }
2361 
2362   destruct_map_clone(old_map);
2363   assert(!mismatched || alias_type->adr_type()->is_oopptr(), "off-heap access can't be mismatched");
2364 
2365   if (mismatched) {
2366     decorators |= C2_MISMATCHED;
2367   }
2368 
2369   // First guess at the value type.
2370   const Type *value_type = Type::get_const_basic_type(type);
2371 
2372   // Figure out the memory ordering.
2373   decorators |= mo_decorator_for_access_kind(kind);
2374 
2375   if (!is_store && type == T_OBJECT) {
2376     const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2377     if (tjp != nullptr) {
2378       value_type = tjp;
2379     }
2380   }
2381 
2382   receiver = null_check(receiver);
2383   if (stopped()) {
2384     return true;
2385   }
2386   // Heap pointers get a null-check from the interpreter,
2387   // as a courtesy.  However, this is not guaranteed by Unsafe,
2388   // and it is not possible to fully distinguish unintended nulls
2389   // from intended ones in this API.
2390 
2391   if (!is_store) {
2392     Node* p = nullptr;
2393     // Try to constant fold a load from a constant field
2394     ciField* field = alias_type->field();
2395     if (heap_base_oop != top() && field != nullptr && field->is_constant() && !mismatched) {
2396       // final or stable field
2397       p = make_constant_from_field(field, heap_base_oop);
2398     }
2399 
2400     if (p == nullptr) { // Could not constant fold the load
2401       p = access_load_at(heap_base_oop, adr, adr_type, value_type, type, decorators);
2402       // Normalize the value returned by getBoolean in the following cases
2403       if (type == T_BOOLEAN &&
2404           (mismatched ||
2405            heap_base_oop == top() ||                  // - heap_base_oop is null or
2406            (can_access_non_heap && field == nullptr)) // - heap_base_oop is potentially null
2407                                                       //   and the unsafe access is made to large offset
2408                                                       //   (i.e., larger than the maximum offset necessary for any
2409                                                       //   field access)
2410             ) {
2411           IdealKit ideal = IdealKit(this);
2412 #define __ ideal.
2413           IdealVariable normalized_result(ideal);
2414           __ declarations_done();
2415           __ set(normalized_result, p);
2416           __ if_then(p, BoolTest::ne, ideal.ConI(0));
2417           __ set(normalized_result, ideal.ConI(1));
2418           ideal.end_if();
2419           final_sync(ideal);
2420           p = __ value(normalized_result);
2421 #undef __
2422       }
2423     }
2424     if (type == T_ADDRESS) {
2425       p = gvn().transform(new CastP2XNode(nullptr, p));
2426       p = ConvX2UL(p);
2427     }
2428     // The load node has the control of the preceding MemBarCPUOrder.  All
2429     // following nodes will have the control of the MemBarCPUOrder inserted at
2430     // the end of this method.  So, pushing the load onto the stack at a later
2431     // point is fine.
2432     set_result(p);
2433   } else {
2434     if (bt == T_ADDRESS) {
2435       // Repackage the long as a pointer.
2436       val = ConvL2X(val);
2437       val = gvn().transform(new CastX2PNode(val));
2438     }
2439     access_store_at(heap_base_oop, adr, adr_type, val, value_type, type, decorators);
2440   }
2441 
2442   return true;
2443 }
2444 
2445 //----------------------------inline_unsafe_load_store----------------------------
2446 // This method serves a couple of different customers (depending on LoadStoreKind):
2447 //
2448 // LS_cmp_swap:
2449 //
2450 //   boolean compareAndSetReference(Object o, long offset, Object expected, Object x);
2451 //   boolean compareAndSetInt(   Object o, long offset, int    expected, int    x);
2452 //   boolean compareAndSetLong(  Object o, long offset, long   expected, long   x);
2453 //
2454 // LS_cmp_swap_weak:
2455 //
2456 //   boolean weakCompareAndSetReference(       Object o, long offset, Object expected, Object x);
2457 //   boolean weakCompareAndSetReferencePlain(  Object o, long offset, Object expected, Object x);
2458 //   boolean weakCompareAndSetReferenceAcquire(Object o, long offset, Object expected, Object x);
2459 //   boolean weakCompareAndSetReferenceRelease(Object o, long offset, Object expected, Object x);
2460 //
2461 //   boolean weakCompareAndSetInt(          Object o, long offset, int    expected, int    x);
2462 //   boolean weakCompareAndSetIntPlain(     Object o, long offset, int    expected, int    x);
2463 //   boolean weakCompareAndSetIntAcquire(   Object o, long offset, int    expected, int    x);
2464 //   boolean weakCompareAndSetIntRelease(   Object o, long offset, int    expected, int    x);
2465 //
2466 //   boolean weakCompareAndSetLong(         Object o, long offset, long   expected, long   x);
2467 //   boolean weakCompareAndSetLongPlain(    Object o, long offset, long   expected, long   x);
2468 //   boolean weakCompareAndSetLongAcquire(  Object o, long offset, long   expected, long   x);
2469 //   boolean weakCompareAndSetLongRelease(  Object o, long offset, long   expected, long   x);
2470 //
2471 // LS_cmp_exchange:
2472 //
2473 //   Object compareAndExchangeReferenceVolatile(Object o, long offset, Object expected, Object x);
2474 //   Object compareAndExchangeReferenceAcquire( Object o, long offset, Object expected, Object x);
2475 //   Object compareAndExchangeReferenceRelease( Object o, long offset, Object expected, Object x);
2476 //
2477 //   Object compareAndExchangeIntVolatile(   Object o, long offset, Object expected, Object x);
2478 //   Object compareAndExchangeIntAcquire(    Object o, long offset, Object expected, Object x);
2479 //   Object compareAndExchangeIntRelease(    Object o, long offset, Object expected, Object x);
2480 //
2481 //   Object compareAndExchangeLongVolatile(  Object o, long offset, Object expected, Object x);
2482 //   Object compareAndExchangeLongAcquire(   Object o, long offset, Object expected, Object x);
2483 //   Object compareAndExchangeLongRelease(   Object o, long offset, Object expected, Object x);
2484 //
2485 // LS_get_add:
2486 //
2487 //   int  getAndAddInt( Object o, long offset, int  delta)
2488 //   long getAndAddLong(Object o, long offset, long delta)
2489 //
2490 // LS_get_set:
2491 //
2492 //   int    getAndSet(Object o, long offset, int    newValue)
2493 //   long   getAndSet(Object o, long offset, long   newValue)
2494 //   Object getAndSet(Object o, long offset, Object newValue)
2495 //
2496 bool LibraryCallKit::inline_unsafe_load_store(const BasicType type, const LoadStoreKind kind, const AccessKind access_kind) {
2497   // This basic scheme here is the same as inline_unsafe_access, but
2498   // differs in enough details that combining them would make the code
2499   // overly confusing.  (This is a true fact! I originally combined
2500   // them, but even I was confused by it!) As much code/comments as
2501   // possible are retained from inline_unsafe_access though to make
2502   // the correspondences clearer. - dl
2503 
2504   if (callee()->is_static())  return false;  // caller must have the capability!
2505 
2506   DecoratorSet decorators = C2_UNSAFE_ACCESS;
2507   decorators |= mo_decorator_for_access_kind(access_kind);
2508 
2509 #ifndef PRODUCT
2510   BasicType rtype;
2511   {
2512     ResourceMark rm;
2513     // Check the signatures.
2514     ciSignature* sig = callee()->signature();
2515     rtype = sig->return_type()->basic_type();
2516     switch(kind) {
2517       case LS_get_add:
2518       case LS_get_set: {
2519       // Check the signatures.
2520 #ifdef ASSERT
2521       assert(rtype == type, "get and set must return the expected type");
2522       assert(sig->count() == 3, "get and set has 3 arguments");
2523       assert(sig->type_at(0)->basic_type() == T_OBJECT, "get and set base is object");
2524       assert(sig->type_at(1)->basic_type() == T_LONG, "get and set offset is long");
2525       assert(sig->type_at(2)->basic_type() == type, "get and set must take expected type as new value/delta");
2526       assert(access_kind == Volatile, "mo is not passed to intrinsic nodes in current implementation");
2527 #endif // ASSERT
2528         break;
2529       }
2530       case LS_cmp_swap:
2531       case LS_cmp_swap_weak: {
2532       // Check the signatures.
2533 #ifdef ASSERT
2534       assert(rtype == T_BOOLEAN, "CAS must return boolean");
2535       assert(sig->count() == 4, "CAS has 4 arguments");
2536       assert(sig->type_at(0)->basic_type() == T_OBJECT, "CAS base is object");
2537       assert(sig->type_at(1)->basic_type() == T_LONG, "CAS offset is long");
2538 #endif // ASSERT
2539         break;
2540       }
2541       case LS_cmp_exchange: {
2542       // Check the signatures.
2543 #ifdef ASSERT
2544       assert(rtype == type, "CAS must return the expected type");
2545       assert(sig->count() == 4, "CAS has 4 arguments");
2546       assert(sig->type_at(0)->basic_type() == T_OBJECT, "CAS base is object");
2547       assert(sig->type_at(1)->basic_type() == T_LONG, "CAS offset is long");
2548 #endif // ASSERT
2549         break;
2550       }
2551       default:
2552         ShouldNotReachHere();
2553     }
2554   }
2555 #endif //PRODUCT
2556 
2557   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
2558 
2559   // Get arguments:
2560   Node* receiver = nullptr;
2561   Node* base     = nullptr;
2562   Node* offset   = nullptr;
2563   Node* oldval   = nullptr;
2564   Node* newval   = nullptr;
2565   switch(kind) {
2566     case LS_cmp_swap:
2567     case LS_cmp_swap_weak:
2568     case LS_cmp_exchange: {
2569       const bool two_slot_type = type2size[type] == 2;
2570       receiver = argument(0);  // type: oop
2571       base     = argument(1);  // type: oop
2572       offset   = argument(2);  // type: long
2573       oldval   = argument(4);  // type: oop, int, or long
2574       newval   = argument(two_slot_type ? 6 : 5);  // type: oop, int, or long
2575       break;
2576     }
2577     case LS_get_add:
2578     case LS_get_set: {
2579       receiver = argument(0);  // type: oop
2580       base     = argument(1);  // type: oop
2581       offset   = argument(2);  // type: long
2582       oldval   = nullptr;
2583       newval   = argument(4);  // type: oop, int, or long
2584       break;
2585     }
2586     default:
2587       ShouldNotReachHere();
2588   }
2589 
2590   // Build field offset expression.
2591   // We currently rely on the cookies produced by Unsafe.xxxFieldOffset
2592   // to be plain byte offsets, which are also the same as those accepted
2593   // by oopDesc::field_addr.
2594   assert(Unsafe_field_offset_to_byte_offset(11) == 11, "fieldOffset must be byte-scaled");
2595   // 32-bit machines ignore the high half of long offsets
2596   offset = ConvL2X(offset);
2597   // Save state and restore on bailout
2598   uint old_sp = sp();
2599   SafePointNode* old_map = clone_map();
2600   Node* adr = make_unsafe_address(base, offset,type, false);
2601   const TypePtr *adr_type = _gvn.type(adr)->isa_ptr();
2602 
2603   Compile::AliasType* alias_type = C->alias_type(adr_type);
2604   BasicType bt = alias_type->basic_type();
2605   if (bt != T_ILLEGAL &&
2606       (is_reference_type(bt) != (type == T_OBJECT))) {
2607     // Don't intrinsify mismatched object accesses.
2608     set_map(old_map);
2609     set_sp(old_sp);
2610     return false;
2611   }
2612 
2613   destruct_map_clone(old_map);
2614 
2615   // For CAS, unlike inline_unsafe_access, there seems no point in
2616   // trying to refine types. Just use the coarse types here.
2617   assert(alias_type->index() != Compile::AliasIdxBot, "no bare pointers here");
2618   const Type *value_type = Type::get_const_basic_type(type);
2619 
2620   switch (kind) {
2621     case LS_get_set:
2622     case LS_cmp_exchange: {
2623       if (type == T_OBJECT) {
2624         const TypeOopPtr* tjp = sharpen_unsafe_type(alias_type, adr_type);
2625         if (tjp != nullptr) {
2626           value_type = tjp;
2627         }
2628       }
2629       break;
2630     }
2631     case LS_cmp_swap:
2632     case LS_cmp_swap_weak:
2633     case LS_get_add:
2634       break;
2635     default:
2636       ShouldNotReachHere();
2637   }
2638 
2639   // Null check receiver.
2640   receiver = null_check(receiver);
2641   if (stopped()) {
2642     return true;
2643   }
2644 
2645   int alias_idx = C->get_alias_index(adr_type);
2646 
2647   if (is_reference_type(type)) {
2648     decorators |= IN_HEAP | ON_UNKNOWN_OOP_REF;
2649 
2650     // Transformation of a value which could be null pointer (CastPP #null)
2651     // could be delayed during Parse (for example, in adjust_map_after_if()).
2652     // Execute transformation here to avoid barrier generation in such case.
2653     if (_gvn.type(newval) == TypePtr::NULL_PTR)
2654       newval = _gvn.makecon(TypePtr::NULL_PTR);
2655 
2656     if (oldval != nullptr && _gvn.type(oldval) == TypePtr::NULL_PTR) {
2657       // Refine the value to a null constant, when it is known to be null
2658       oldval = _gvn.makecon(TypePtr::NULL_PTR);
2659     }
2660   }
2661 
2662   Node* result = nullptr;
2663   switch (kind) {
2664     case LS_cmp_exchange: {
2665       result = access_atomic_cmpxchg_val_at(base, adr, adr_type, alias_idx,
2666                                             oldval, newval, value_type, type, decorators);
2667       break;
2668     }
2669     case LS_cmp_swap_weak:
2670       decorators |= C2_WEAK_CMPXCHG;
2671     case LS_cmp_swap: {
2672       result = access_atomic_cmpxchg_bool_at(base, adr, adr_type, alias_idx,
2673                                              oldval, newval, value_type, type, decorators);
2674       break;
2675     }
2676     case LS_get_set: {
2677       result = access_atomic_xchg_at(base, adr, adr_type, alias_idx,
2678                                      newval, value_type, type, decorators);
2679       break;
2680     }
2681     case LS_get_add: {
2682       result = access_atomic_add_at(base, adr, adr_type, alias_idx,
2683                                     newval, value_type, type, decorators);
2684       break;
2685     }
2686     default:
2687       ShouldNotReachHere();
2688   }
2689 
2690   assert(type2size[result->bottom_type()->basic_type()] == type2size[rtype], "result type should match");
2691   set_result(result);
2692   return true;
2693 }
2694 
2695 bool LibraryCallKit::inline_unsafe_fence(vmIntrinsics::ID id) {
2696   // Regardless of form, don't allow previous ld/st to move down,
2697   // then issue acquire, release, or volatile mem_bar.
2698   insert_mem_bar(Op_MemBarCPUOrder);
2699   switch(id) {
2700     case vmIntrinsics::_loadFence:
2701       insert_mem_bar(Op_LoadFence);
2702       return true;
2703     case vmIntrinsics::_storeFence:
2704       insert_mem_bar(Op_StoreFence);
2705       return true;
2706     case vmIntrinsics::_storeStoreFence:
2707       insert_mem_bar(Op_StoreStoreFence);
2708       return true;
2709     case vmIntrinsics::_fullFence:
2710       insert_mem_bar(Op_MemBarVolatile);
2711       return true;
2712     default:
2713       fatal_unexpected_iid(id);
2714       return false;
2715   }
2716 }
2717 
2718 bool LibraryCallKit::inline_onspinwait() {
2719   insert_mem_bar(Op_OnSpinWait);
2720   return true;
2721 }
2722 
2723 bool LibraryCallKit::klass_needs_init_guard(Node* kls) {
2724   if (!kls->is_Con()) {
2725     return true;
2726   }
2727   const TypeKlassPtr* klsptr = kls->bottom_type()->isa_klassptr();
2728   if (klsptr == nullptr) {
2729     return true;
2730   }
2731   ciInstanceKlass* ik = klsptr->klass()->as_instance_klass();
2732   // don't need a guard for a klass that is already initialized
2733   return !ik->is_initialized();
2734 }
2735 
2736 //----------------------------inline_unsafe_writeback0-------------------------
2737 // public native void Unsafe.writeback0(long address)
2738 bool LibraryCallKit::inline_unsafe_writeback0() {
2739   if (!Matcher::has_match_rule(Op_CacheWB)) {
2740     return false;
2741   }
2742 #ifndef PRODUCT
2743   assert(Matcher::has_match_rule(Op_CacheWBPreSync), "found match rule for CacheWB but not CacheWBPreSync");
2744   assert(Matcher::has_match_rule(Op_CacheWBPostSync), "found match rule for CacheWB but not CacheWBPostSync");
2745   ciSignature* sig = callee()->signature();
2746   assert(sig->type_at(0)->basic_type() == T_LONG, "Unsafe_writeback0 address is long!");
2747 #endif
2748   null_check_receiver();  // null-check, then ignore
2749   Node *addr = argument(1);
2750   addr = new CastX2PNode(addr);
2751   addr = _gvn.transform(addr);
2752   Node *flush = new CacheWBNode(control(), memory(TypeRawPtr::BOTTOM), addr);
2753   flush = _gvn.transform(flush);
2754   set_memory(flush, TypeRawPtr::BOTTOM);
2755   return true;
2756 }
2757 
2758 //----------------------------inline_unsafe_writeback0-------------------------
2759 // public native void Unsafe.writeback0(long address)
2760 bool LibraryCallKit::inline_unsafe_writebackSync0(bool is_pre) {
2761   if (is_pre && !Matcher::has_match_rule(Op_CacheWBPreSync)) {
2762     return false;
2763   }
2764   if (!is_pre && !Matcher::has_match_rule(Op_CacheWBPostSync)) {
2765     return false;
2766   }
2767 #ifndef PRODUCT
2768   assert(Matcher::has_match_rule(Op_CacheWB),
2769          (is_pre ? "found match rule for CacheWBPreSync but not CacheWB"
2770                 : "found match rule for CacheWBPostSync but not CacheWB"));
2771 
2772 #endif
2773   null_check_receiver();  // null-check, then ignore
2774   Node *sync;
2775   if (is_pre) {
2776     sync = new CacheWBPreSyncNode(control(), memory(TypeRawPtr::BOTTOM));
2777   } else {
2778     sync = new CacheWBPostSyncNode(control(), memory(TypeRawPtr::BOTTOM));
2779   }
2780   sync = _gvn.transform(sync);
2781   set_memory(sync, TypeRawPtr::BOTTOM);
2782   return true;
2783 }
2784 
2785 //----------------------------inline_unsafe_allocate---------------------------
2786 // public native Object Unsafe.allocateInstance(Class<?> cls);
2787 bool LibraryCallKit::inline_unsafe_allocate() {
2788   if (callee()->is_static())  return false;  // caller must have the capability!
2789 
2790   null_check_receiver();  // null-check, then ignore
2791   Node* cls = null_check(argument(1));
2792   if (stopped())  return true;
2793 
2794   Node* kls = load_klass_from_mirror(cls, false, nullptr, 0);
2795   kls = null_check(kls);
2796   if (stopped())  return true;  // argument was like int.class
2797 
2798   Node* test = nullptr;
2799   if (LibraryCallKit::klass_needs_init_guard(kls)) {
2800     // Note:  The argument might still be an illegal value like
2801     // Serializable.class or Object[].class.   The runtime will handle it.
2802     // But we must make an explicit check for initialization.
2803     Node* insp = basic_plus_adr(kls, in_bytes(InstanceKlass::init_state_offset()));
2804     // Use T_BOOLEAN for InstanceKlass::_init_state so the compiler
2805     // can generate code to load it as unsigned byte.
2806     Node* inst = make_load(nullptr, insp, TypeInt::UBYTE, T_BOOLEAN, MemNode::unordered);
2807     Node* bits = intcon(InstanceKlass::fully_initialized);
2808     test = _gvn.transform(new SubINode(inst, bits));
2809     // The 'test' is non-zero if we need to take a slow path.
2810   }
2811 
2812   Node* obj = new_instance(kls, test);
2813   set_result(obj);
2814   return true;
2815 }
2816 
2817 //------------------------inline_native_time_funcs--------------
2818 // inline code for System.currentTimeMillis() and System.nanoTime()
2819 // these have the same type and signature
2820 bool LibraryCallKit::inline_native_time_funcs(address funcAddr, const char* funcName) {
2821   const TypeFunc* tf = OptoRuntime::void_long_Type();
2822   const TypePtr* no_memory_effects = nullptr;
2823   Node* time = make_runtime_call(RC_LEAF, tf, funcAddr, funcName, no_memory_effects);
2824   Node* value = _gvn.transform(new ProjNode(time, TypeFunc::Parms+0));
2825 #ifdef ASSERT
2826   Node* value_top = _gvn.transform(new ProjNode(time, TypeFunc::Parms+1));
2827   assert(value_top == top(), "second value must be top");
2828 #endif
2829   set_result(value);
2830   return true;
2831 }
2832 
2833 #ifdef JFR_HAVE_INTRINSICS
2834 
2835 /**
2836  * if oop->klass != null
2837  *   // normal class
2838  *   epoch = _epoch_state ? 2 : 1
2839  *   if oop->klass->trace_id & ((epoch << META_SHIFT) | epoch)) != epoch {
2840  *     ... // enter slow path when the klass is first recorded or the epoch of JFR shifts
2841  *   }
2842  *   id = oop->klass->trace_id >> TRACE_ID_SHIFT // normal class path
2843  * else
2844  *   // primitive class
2845  *   if oop->array_klass != null
2846  *     id = (oop->array_klass->trace_id >> TRACE_ID_SHIFT) + 1 // primitive class path
2847  *   else
2848  *     id = LAST_TYPE_ID + 1 // void class path
2849  *   if (!signaled)
2850  *     signaled = true
2851  */
2852 bool LibraryCallKit::inline_native_classID() {
2853   Node* cls = argument(0);
2854 
2855   IdealKit ideal(this);
2856 #define __ ideal.
2857   IdealVariable result(ideal); __ declarations_done();
2858   Node* kls = _gvn.transform(LoadKlassNode::make(_gvn, nullptr, immutable_memory(),
2859                                                  basic_plus_adr(cls, java_lang_Class::klass_offset()),
2860                                                  TypeRawPtr::BOTTOM, TypeKlassPtr::OBJECT_OR_NULL));
2861 
2862 
2863   __ if_then(kls, BoolTest::ne, null()); {
2864     Node* kls_trace_id_addr = basic_plus_adr(kls, in_bytes(KLASS_TRACE_ID_OFFSET));
2865     Node* kls_trace_id_raw = ideal.load(ideal.ctrl(), kls_trace_id_addr,TypeLong::LONG, T_LONG, Compile::AliasIdxRaw);
2866 
2867     Node* epoch_address = makecon(TypeRawPtr::make(Jfr::epoch_address()));
2868     Node* epoch = ideal.load(ideal.ctrl(), epoch_address, TypeInt::BOOL, T_BOOLEAN, Compile::AliasIdxRaw);
2869     epoch = _gvn.transform(new LShiftLNode(longcon(1), epoch));
2870     Node* mask = _gvn.transform(new LShiftLNode(epoch, intcon(META_SHIFT)));
2871     mask = _gvn.transform(new OrLNode(mask, epoch));
2872     Node* kls_trace_id_raw_and_mask = _gvn.transform(new AndLNode(kls_trace_id_raw, mask));
2873 
2874     float unlikely  = PROB_UNLIKELY(0.999);
2875     __ if_then(kls_trace_id_raw_and_mask, BoolTest::ne, epoch, unlikely); {
2876       sync_kit(ideal);
2877       make_runtime_call(RC_LEAF,
2878                         OptoRuntime::get_class_id_intrinsic_Type(),
2879                         CAST_FROM_FN_PTR(address, Jfr::get_class_id_intrinsic),
2880                         "get_class_id_intrinsic",
2881                         TypePtr::BOTTOM,
2882                         kls);
2883       ideal.sync_kit(this);
2884     } __ end_if();
2885 
2886     ideal.set(result,  _gvn.transform(new URShiftLNode(kls_trace_id_raw, ideal.ConI(TRACE_ID_SHIFT))));
2887   } __ else_(); {
2888     Node* array_kls = _gvn.transform(LoadKlassNode::make(_gvn, nullptr, immutable_memory(),
2889                                                    basic_plus_adr(cls, java_lang_Class::array_klass_offset()),
2890                                                    TypeRawPtr::BOTTOM, TypeKlassPtr::OBJECT_OR_NULL));
2891     __ if_then(array_kls, BoolTest::ne, null()); {
2892       Node* array_kls_trace_id_addr = basic_plus_adr(array_kls, in_bytes(KLASS_TRACE_ID_OFFSET));
2893       Node* array_kls_trace_id_raw = ideal.load(ideal.ctrl(), array_kls_trace_id_addr, TypeLong::LONG, T_LONG, Compile::AliasIdxRaw);
2894       Node* array_kls_trace_id = _gvn.transform(new URShiftLNode(array_kls_trace_id_raw, ideal.ConI(TRACE_ID_SHIFT)));
2895       ideal.set(result, _gvn.transform(new AddLNode(array_kls_trace_id, longcon(1))));
2896     } __ else_(); {
2897       // void class case
2898       ideal.set(result, _gvn.transform(longcon(LAST_TYPE_ID + 1)));
2899     } __ end_if();
2900 
2901     Node* signaled_flag_address = makecon(TypeRawPtr::make(Jfr::signal_address()));
2902     Node* signaled = ideal.load(ideal.ctrl(), signaled_flag_address, TypeInt::BOOL, T_BOOLEAN, Compile::AliasIdxRaw, true, MemNode::acquire);
2903     __ if_then(signaled, BoolTest::ne, ideal.ConI(1)); {
2904       ideal.store(ideal.ctrl(), signaled_flag_address, ideal.ConI(1), T_BOOLEAN, Compile::AliasIdxRaw, MemNode::release, true);
2905     } __ end_if();
2906   } __ end_if();
2907 
2908   final_sync(ideal);
2909   set_result(ideal.value(result));
2910 #undef __
2911   return true;
2912 }
2913 
2914 bool LibraryCallKit::inline_native_getEventWriter() {
2915   Node* tls_ptr = _gvn.transform(new ThreadLocalNode());
2916 
2917   Node* jobj_ptr = basic_plus_adr(top(), tls_ptr,
2918                                   in_bytes(THREAD_LOCAL_WRITER_OFFSET_JFR));
2919 
2920   Node* jobj = make_load(control(), jobj_ptr, TypeRawPtr::BOTTOM, T_ADDRESS, MemNode::unordered);
2921 
2922   Node* jobj_cmp_null = _gvn.transform( new CmpPNode(jobj, null()) );
2923   Node* test_jobj_eq_null  = _gvn.transform( new BoolNode(jobj_cmp_null, BoolTest::eq) );
2924 
2925   IfNode* iff_jobj_null =
2926     create_and_map_if(control(), test_jobj_eq_null, PROB_MIN, COUNT_UNKNOWN);
2927 
2928   enum { _normal_path = 1,
2929          _null_path = 2,
2930          PATH_LIMIT };
2931 
2932   RegionNode* result_rgn = new RegionNode(PATH_LIMIT);
2933   PhiNode*    result_val = new PhiNode(result_rgn, TypeInstPtr::BOTTOM);
2934 
2935   Node* jobj_is_null = _gvn.transform(new IfTrueNode(iff_jobj_null));
2936   result_rgn->init_req(_null_path, jobj_is_null);
2937   result_val->init_req(_null_path, null());
2938 
2939   Node* jobj_is_not_null = _gvn.transform(new IfFalseNode(iff_jobj_null));
2940   set_control(jobj_is_not_null);
2941   Node* res = access_load(jobj, TypeInstPtr::NOTNULL, T_OBJECT,
2942                           IN_NATIVE | C2_CONTROL_DEPENDENT_LOAD);
2943   result_rgn->init_req(_normal_path, control());
2944   result_val->init_req(_normal_path, res);
2945 
2946   set_result(result_rgn, result_val);
2947 
2948   return true;
2949 }
2950 
2951 #endif // JFR_HAVE_INTRINSICS
2952 
2953 //------------------------inline_native_currentThread------------------
2954 bool LibraryCallKit::inline_native_currentThread() {
2955   Node* junk = nullptr;
2956   set_result(generate_current_thread(junk));
2957   return true;
2958 }
2959 
2960 //---------------------------load_mirror_from_klass----------------------------
2961 // Given a klass oop, load its java mirror (a java.lang.Class oop).
2962 Node* LibraryCallKit::load_mirror_from_klass(Node* klass) {
2963   Node* p = basic_plus_adr(klass, in_bytes(Klass::java_mirror_offset()));
2964   Node* load = make_load(nullptr, p, TypeRawPtr::NOTNULL, T_ADDRESS, MemNode::unordered);
2965   // mirror = ((OopHandle)mirror)->resolve();
2966   return access_load(load, TypeInstPtr::MIRROR, T_OBJECT, IN_NATIVE);
2967 }
2968 
2969 //-----------------------load_klass_from_mirror_common-------------------------
2970 // Given a java mirror (a java.lang.Class oop), load its corresponding klass oop.
2971 // Test the klass oop for null (signifying a primitive Class like Integer.TYPE),
2972 // and branch to the given path on the region.
2973 // If never_see_null, take an uncommon trap on null, so we can optimistically
2974 // compile for the non-null case.
2975 // If the region is null, force never_see_null = true.
2976 Node* LibraryCallKit::load_klass_from_mirror_common(Node* mirror,
2977                                                     bool never_see_null,
2978                                                     RegionNode* region,
2979                                                     int null_path,
2980                                                     int offset) {
2981   if (region == nullptr)  never_see_null = true;
2982   Node* p = basic_plus_adr(mirror, offset);
2983   const TypeKlassPtr*  kls_type = TypeKlassPtr::OBJECT_OR_NULL;
2984   Node* kls = _gvn.transform(LoadKlassNode::make(_gvn, nullptr, immutable_memory(), p, TypeRawPtr::BOTTOM, kls_type));
2985   Node* null_ctl = top();
2986   kls = null_check_oop(kls, &null_ctl, never_see_null);
2987   if (region != nullptr) {
2988     // Set region->in(null_path) if the mirror is a primitive (e.g, int.class).
2989     region->init_req(null_path, null_ctl);
2990   } else {
2991     assert(null_ctl == top(), "no loose ends");
2992   }
2993   return kls;
2994 }
2995 
2996 //--------------------(inline_native_Class_query helpers)---------------------
2997 // Use this for JVM_ACC_INTERFACE, JVM_ACC_IS_CLONEABLE_FAST, JVM_ACC_HAS_FINALIZER.
2998 // Fall through if (mods & mask) == bits, take the guard otherwise.
2999 Node* LibraryCallKit::generate_access_flags_guard(Node* kls, int modifier_mask, int modifier_bits, RegionNode* region) {
3000   // Branch around if the given klass has the given modifier bit set.
3001   // Like generate_guard, adds a new path onto the region.
3002   Node* modp = basic_plus_adr(kls, in_bytes(Klass::access_flags_offset()));
3003   Node* mods = make_load(nullptr, modp, TypeInt::INT, T_INT, MemNode::unordered);
3004   Node* mask = intcon(modifier_mask);
3005   Node* bits = intcon(modifier_bits);
3006   Node* mbit = _gvn.transform(new AndINode(mods, mask));
3007   Node* cmp  = _gvn.transform(new CmpINode(mbit, bits));
3008   Node* bol  = _gvn.transform(new BoolNode(cmp, BoolTest::ne));
3009   return generate_fair_guard(bol, region);
3010 }
3011 Node* LibraryCallKit::generate_interface_guard(Node* kls, RegionNode* region) {
3012   return generate_access_flags_guard(kls, JVM_ACC_INTERFACE, 0, region);
3013 }
3014 Node* LibraryCallKit::generate_hidden_class_guard(Node* kls, RegionNode* region) {
3015   return generate_access_flags_guard(kls, JVM_ACC_IS_HIDDEN_CLASS, 0, region);
3016 }
3017 
3018 //-------------------------inline_native_Class_query-------------------
3019 bool LibraryCallKit::inline_native_Class_query(vmIntrinsics::ID id) {
3020   const Type* return_type = TypeInt::BOOL;
3021   Node* prim_return_value = top();  // what happens if it's a primitive class?
3022   bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3023   bool expect_prim = false;     // most of these guys expect to work on refs
3024 
3025   enum { _normal_path = 1, _prim_path = 2, PATH_LIMIT };
3026 
3027   Node* mirror = argument(0);
3028   Node* obj    = top();
3029 
3030   switch (id) {
3031   case vmIntrinsics::_isInstance:
3032     // nothing is an instance of a primitive type
3033     prim_return_value = intcon(0);
3034     obj = argument(1);
3035     break;
3036   case vmIntrinsics::_getModifiers:
3037     prim_return_value = intcon(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
3038     assert(is_power_of_2((int)JVM_ACC_WRITTEN_FLAGS+1), "change next line");
3039     return_type = TypeInt::make(0, JVM_ACC_WRITTEN_FLAGS, Type::WidenMin);
3040     break;
3041   case vmIntrinsics::_isInterface:
3042     prim_return_value = intcon(0);
3043     break;
3044   case vmIntrinsics::_isArray:
3045     prim_return_value = intcon(0);
3046     expect_prim = true;  // cf. ObjectStreamClass.getClassSignature
3047     break;
3048   case vmIntrinsics::_isPrimitive:
3049     prim_return_value = intcon(1);
3050     expect_prim = true;  // obviously
3051     break;
3052   case vmIntrinsics::_isHidden:
3053     prim_return_value = intcon(0);
3054     break;
3055   case vmIntrinsics::_getSuperclass:
3056     prim_return_value = null();
3057     return_type = TypeInstPtr::MIRROR->cast_to_ptr_type(TypePtr::BotPTR);
3058     break;
3059   case vmIntrinsics::_getClassAccessFlags:
3060     prim_return_value = intcon(JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC);
3061     return_type = TypeInt::INT;  // not bool!  6297094
3062     break;
3063   default:
3064     fatal_unexpected_iid(id);
3065     break;
3066   }
3067 
3068   const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
3069   if (mirror_con == nullptr)  return false;  // cannot happen?
3070 
3071 #ifndef PRODUCT
3072   if (C->print_intrinsics() || C->print_inlining()) {
3073     ciType* k = mirror_con->java_mirror_type();
3074     if (k) {
3075       tty->print("Inlining %s on constant Class ", vmIntrinsics::name_at(intrinsic_id()));
3076       k->print_name();
3077       tty->cr();
3078     }
3079   }
3080 #endif
3081 
3082   // Null-check the mirror, and the mirror's klass ptr (in case it is a primitive).
3083   RegionNode* region = new RegionNode(PATH_LIMIT);
3084   record_for_igvn(region);
3085   PhiNode* phi = new PhiNode(region, return_type);
3086 
3087   // The mirror will never be null of Reflection.getClassAccessFlags, however
3088   // it may be null for Class.isInstance or Class.getModifiers. Throw a NPE
3089   // if it is. See bug 4774291.
3090 
3091   // For Reflection.getClassAccessFlags(), the null check occurs in
3092   // the wrong place; see inline_unsafe_access(), above, for a similar
3093   // situation.
3094   mirror = null_check(mirror);
3095   // If mirror or obj is dead, only null-path is taken.
3096   if (stopped())  return true;
3097 
3098   if (expect_prim)  never_see_null = false;  // expect nulls (meaning prims)
3099 
3100   // Now load the mirror's klass metaobject, and null-check it.
3101   // Side-effects region with the control path if the klass is null.
3102   Node* kls = load_klass_from_mirror(mirror, never_see_null, region, _prim_path);
3103   // If kls is null, we have a primitive mirror.
3104   phi->init_req(_prim_path, prim_return_value);
3105   if (stopped()) { set_result(region, phi); return true; }
3106   bool safe_for_replace = (region->in(_prim_path) == top());
3107 
3108   Node* p;  // handy temp
3109   Node* null_ctl;
3110 
3111   // Now that we have the non-null klass, we can perform the real query.
3112   // For constant classes, the query will constant-fold in LoadNode::Value.
3113   Node* query_value = top();
3114   switch (id) {
3115   case vmIntrinsics::_isInstance:
3116     // nothing is an instance of a primitive type
3117     query_value = gen_instanceof(obj, kls, safe_for_replace);
3118     break;
3119 
3120   case vmIntrinsics::_getModifiers:
3121     p = basic_plus_adr(kls, in_bytes(Klass::modifier_flags_offset()));
3122     query_value = make_load(nullptr, p, TypeInt::INT, T_INT, MemNode::unordered);
3123     break;
3124 
3125   case vmIntrinsics::_isInterface:
3126     // (To verify this code sequence, check the asserts in JVM_IsInterface.)
3127     if (generate_interface_guard(kls, region) != nullptr)
3128       // A guard was added.  If the guard is taken, it was an interface.
3129       phi->add_req(intcon(1));
3130     // If we fall through, it's a plain class.
3131     query_value = intcon(0);
3132     break;
3133 
3134   case vmIntrinsics::_isArray:
3135     // (To verify this code sequence, check the asserts in JVM_IsArrayClass.)
3136     if (generate_array_guard(kls, region) != nullptr)
3137       // A guard was added.  If the guard is taken, it was an array.
3138       phi->add_req(intcon(1));
3139     // If we fall through, it's a plain class.
3140     query_value = intcon(0);
3141     break;
3142 
3143   case vmIntrinsics::_isPrimitive:
3144     query_value = intcon(0); // "normal" path produces false
3145     break;
3146 
3147   case vmIntrinsics::_isHidden:
3148     // (To verify this code sequence, check the asserts in JVM_IsHiddenClass.)
3149     if (generate_hidden_class_guard(kls, region) != nullptr)
3150       // A guard was added.  If the guard is taken, it was an hidden class.
3151       phi->add_req(intcon(1));
3152     // If we fall through, it's a plain class.
3153     query_value = intcon(0);
3154     break;
3155 
3156 
3157   case vmIntrinsics::_getSuperclass:
3158     // The rules here are somewhat unfortunate, but we can still do better
3159     // with random logic than with a JNI call.
3160     // Interfaces store null or Object as _super, but must report null.
3161     // Arrays store an intermediate super as _super, but must report Object.
3162     // Other types can report the actual _super.
3163     // (To verify this code sequence, check the asserts in JVM_IsInterface.)
3164     if (generate_interface_guard(kls, region) != nullptr)
3165       // A guard was added.  If the guard is taken, it was an interface.
3166       phi->add_req(null());
3167     if (generate_array_guard(kls, region) != nullptr)
3168       // A guard was added.  If the guard is taken, it was an array.
3169       phi->add_req(makecon(TypeInstPtr::make(env()->Object_klass()->java_mirror())));
3170     // If we fall through, it's a plain class.  Get its _super.
3171     p = basic_plus_adr(kls, in_bytes(Klass::super_offset()));
3172     kls = _gvn.transform(LoadKlassNode::make(_gvn, nullptr, immutable_memory(), p, TypeRawPtr::BOTTOM, TypeKlassPtr::OBJECT_OR_NULL));
3173     null_ctl = top();
3174     kls = null_check_oop(kls, &null_ctl);
3175     if (null_ctl != top()) {
3176       // If the guard is taken, Object.superClass is null (both klass and mirror).
3177       region->add_req(null_ctl);
3178       phi   ->add_req(null());
3179     }
3180     if (!stopped()) {
3181       query_value = load_mirror_from_klass(kls);
3182     }
3183     break;
3184 
3185   case vmIntrinsics::_getClassAccessFlags:
3186     p = basic_plus_adr(kls, in_bytes(Klass::access_flags_offset()));
3187     query_value = make_load(nullptr, p, TypeInt::INT, T_INT, MemNode::unordered);
3188     break;
3189 
3190   default:
3191     fatal_unexpected_iid(id);
3192     break;
3193   }
3194 
3195   // Fall-through is the normal case of a query to a real class.
3196   phi->init_req(1, query_value);
3197   region->init_req(1, control());
3198 
3199   C->set_has_split_ifs(true); // Has chance for split-if optimization
3200   set_result(region, phi);
3201   return true;
3202 }
3203 
3204 //-------------------------inline_Class_cast-------------------
3205 bool LibraryCallKit::inline_Class_cast() {
3206   Node* mirror = argument(0); // Class
3207   Node* obj    = argument(1);
3208   const TypeInstPtr* mirror_con = _gvn.type(mirror)->isa_instptr();
3209   if (mirror_con == nullptr) {
3210     return false;  // dead path (mirror->is_top()).
3211   }
3212   if (obj == nullptr || obj->is_top()) {
3213     return false;  // dead path
3214   }
3215   const TypeOopPtr* tp = _gvn.type(obj)->isa_oopptr();
3216 
3217   // First, see if Class.cast() can be folded statically.
3218   // java_mirror_type() returns non-null for compile-time Class constants.
3219   ciType* tm = mirror_con->java_mirror_type();
3220   if (tm != nullptr && tm->is_klass() &&
3221       tp != NULL && tp->klass() != NULL) {
3222     if (!tp->klass()->is_loaded()) {
3223       // Don't use intrinsic when class is not loaded.
3224       return false;
3225     } else {
3226       int static_res = C->static_subtype_check(tm->as_klass(), tp->klass());
3227       if (static_res == Compile::SSC_always_true) {
3228         // isInstance() is true - fold the code.
3229         set_result(obj);
3230         return true;
3231       } else if (static_res == Compile::SSC_always_false) {
3232         // Don't use intrinsic, have to throw ClassCastException.
3233         // If the reference is null, the non-intrinsic bytecode will
3234         // be optimized appropriately.
3235         return false;
3236       }
3237     }
3238   }
3239 
3240   // Bailout intrinsic and do normal inlining if exception path is frequent.
3241   if (too_many_traps(Deoptimization::Reason_intrinsic)) {
3242     return false;
3243   }
3244 
3245   // Generate dynamic checks.
3246   // Class.cast() is java implementation of _checkcast bytecode.
3247   // Do checkcast (Parse::do_checkcast()) optimizations here.
3248 
3249   mirror = null_check(mirror);
3250   // If mirror is dead, only null-path is taken.
3251   if (stopped()) {
3252     return true;
3253   }
3254 
3255   // Not-subtype or the mirror's klass ptr is null (in case it is a primitive).
3256   enum { _bad_type_path = 1, _prim_path = 2, PATH_LIMIT };
3257   RegionNode* region = new RegionNode(PATH_LIMIT);
3258   record_for_igvn(region);
3259 
3260   // Now load the mirror's klass metaobject, and null-check it.
3261   // If kls is null, we have a primitive mirror and
3262   // nothing is an instance of a primitive type.
3263   Node* kls = load_klass_from_mirror(mirror, false, region, _prim_path);
3264 
3265   Node* res = top();
3266   if (!stopped()) {
3267     Node* bad_type_ctrl = top();
3268     // Do checkcast optimizations.
3269     res = gen_checkcast(obj, kls, &bad_type_ctrl);
3270     region->init_req(_bad_type_path, bad_type_ctrl);
3271   }
3272   if (region->in(_prim_path) != top() ||
3273       region->in(_bad_type_path) != top()) {
3274     // Let Interpreter throw ClassCastException.
3275     PreserveJVMState pjvms(this);
3276     set_control(_gvn.transform(region));
3277     uncommon_trap(Deoptimization::Reason_intrinsic,
3278                   Deoptimization::Action_maybe_recompile);
3279   }
3280   if (!stopped()) {
3281     set_result(res);
3282   }
3283   return true;
3284 }
3285 
3286 
3287 //--------------------------inline_native_subtype_check------------------------
3288 // This intrinsic takes the JNI calls out of the heart of
3289 // UnsafeFieldAccessorImpl.set, which improves Field.set, readObject, etc.
3290 bool LibraryCallKit::inline_native_subtype_check() {
3291   // Pull both arguments off the stack.
3292   Node* args[2];                // two java.lang.Class mirrors: superc, subc
3293   args[0] = argument(0);
3294   args[1] = argument(1);
3295   Node* klasses[2];             // corresponding Klasses: superk, subk
3296   klasses[0] = klasses[1] = top();
3297 
3298   enum {
3299     // A full decision tree on {superc is prim, subc is prim}:
3300     _prim_0_path = 1,           // {P,N} => false
3301                                 // {P,P} & superc!=subc => false
3302     _prim_same_path,            // {P,P} & superc==subc => true
3303     _prim_1_path,               // {N,P} => false
3304     _ref_subtype_path,          // {N,N} & subtype check wins => true
3305     _both_ref_path,             // {N,N} & subtype check loses => false
3306     PATH_LIMIT
3307   };
3308 
3309   RegionNode* region = new RegionNode(PATH_LIMIT);
3310   Node*       phi    = new PhiNode(region, TypeInt::BOOL);
3311   record_for_igvn(region);
3312 
3313   const TypePtr* adr_type = TypeRawPtr::BOTTOM;   // memory type of loads
3314   const TypeKlassPtr* kls_type = TypeKlassPtr::OBJECT_OR_NULL;
3315   int class_klass_offset = java_lang_Class::klass_offset();
3316 
3317   // First null-check both mirrors and load each mirror's klass metaobject.
3318   int which_arg;
3319   for (which_arg = 0; which_arg <= 1; which_arg++) {
3320     Node* arg = args[which_arg];
3321     arg = null_check(arg);
3322     if (stopped())  break;
3323     args[which_arg] = arg;
3324 
3325     Node* p = basic_plus_adr(arg, class_klass_offset);
3326     Node* kls = LoadKlassNode::make(_gvn, nullptr, immutable_memory(), p, adr_type, kls_type);
3327     klasses[which_arg] = _gvn.transform(kls);
3328   }
3329 
3330   // Having loaded both klasses, test each for null.
3331   bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3332   for (which_arg = 0; which_arg <= 1; which_arg++) {
3333     Node* kls = klasses[which_arg];
3334     Node* null_ctl = top();
3335     kls = null_check_oop(kls, &null_ctl, never_see_null);
3336     int prim_path = (which_arg == 0 ? _prim_0_path : _prim_1_path);
3337     region->init_req(prim_path, null_ctl);
3338     if (stopped())  break;
3339     klasses[which_arg] = kls;
3340   }
3341 
3342   if (!stopped()) {
3343     // now we have two reference types, in klasses[0..1]
3344     Node* subk   = klasses[1];  // the argument to isAssignableFrom
3345     Node* superk = klasses[0];  // the receiver
3346     region->set_req(_both_ref_path, gen_subtype_check(subk, superk));
3347     // now we have a successful reference subtype check
3348     region->set_req(_ref_subtype_path, control());
3349   }
3350 
3351   // If both operands are primitive (both klasses null), then
3352   // we must return true when they are identical primitives.
3353   // It is convenient to test this after the first null klass check.
3354   set_control(region->in(_prim_0_path)); // go back to first null check
3355   if (!stopped()) {
3356     // Since superc is primitive, make a guard for the superc==subc case.
3357     Node* cmp_eq = _gvn.transform(new CmpPNode(args[0], args[1]));
3358     Node* bol_eq = _gvn.transform(new BoolNode(cmp_eq, BoolTest::eq));
3359     generate_guard(bol_eq, region, PROB_FAIR);
3360     if (region->req() == PATH_LIMIT+1) {
3361       // A guard was added.  If the added guard is taken, superc==subc.
3362       region->swap_edges(PATH_LIMIT, _prim_same_path);
3363       region->del_req(PATH_LIMIT);
3364     }
3365     region->set_req(_prim_0_path, control()); // Not equal after all.
3366   }
3367 
3368   // these are the only paths that produce 'true':
3369   phi->set_req(_prim_same_path,   intcon(1));
3370   phi->set_req(_ref_subtype_path, intcon(1));
3371 
3372   // pull together the cases:
3373   assert(region->req() == PATH_LIMIT, "sane region");
3374   for (uint i = 1; i < region->req(); i++) {
3375     Node* ctl = region->in(i);
3376     if (ctl == nullptr || ctl == top()) {
3377       region->set_req(i, top());
3378       phi   ->set_req(i, top());
3379     } else if (phi->in(i) == nullptr) {
3380       phi->set_req(i, intcon(0)); // all other paths produce 'false'
3381     }
3382   }
3383 
3384   set_control(_gvn.transform(region));
3385   set_result(_gvn.transform(phi));
3386   return true;
3387 }
3388 
3389 //---------------------generate_array_guard_common------------------------
3390 Node* LibraryCallKit::generate_array_guard_common(Node* kls, RegionNode* region,
3391                                                   bool obj_array, bool not_array) {
3392 
3393   if (stopped()) {
3394     return nullptr;
3395   }
3396 
3397   // If obj_array/non_array==false/false:
3398   // Branch around if the given klass is in fact an array (either obj or prim).
3399   // If obj_array/non_array==false/true:
3400   // Branch around if the given klass is not an array klass of any kind.
3401   // If obj_array/non_array==true/true:
3402   // Branch around if the kls is not an oop array (kls is int[], String, etc.)
3403   // If obj_array/non_array==true/false:
3404   // Branch around if the kls is an oop array (Object[] or subtype)
3405   //
3406   // Like generate_guard, adds a new path onto the region.
3407   jint  layout_con = 0;
3408   Node* layout_val = get_layout_helper(kls, layout_con);
3409   if (layout_val == nullptr) {
3410     bool query = (obj_array
3411                   ? Klass::layout_helper_is_objArray(layout_con)
3412                   : Klass::layout_helper_is_array(layout_con));
3413     if (query == not_array) {
3414       return nullptr;                       // never a branch
3415     } else {                             // always a branch
3416       Node* always_branch = control();
3417       if (region != nullptr)
3418         region->add_req(always_branch);
3419       set_control(top());
3420       return always_branch;
3421     }
3422   }
3423   // Now test the correct condition.
3424   jint  nval = (obj_array
3425                 ? (jint)(Klass::_lh_array_tag_type_value
3426                    <<    Klass::_lh_array_tag_shift)
3427                 : Klass::_lh_neutral_value);
3428   Node* cmp = _gvn.transform(new CmpINode(layout_val, intcon(nval)));
3429   BoolTest::mask btest = BoolTest::lt;  // correct for testing is_[obj]array
3430   // invert the test if we are looking for a non-array
3431   if (not_array)  btest = BoolTest(btest).negate();
3432   Node* bol = _gvn.transform(new BoolNode(cmp, btest));
3433   return generate_fair_guard(bol, region);
3434 }
3435 
3436 
3437 //-----------------------inline_native_newArray--------------------------
3438 // private static native Object java.lang.reflect.newArray(Class<?> componentType, int length);
3439 // private        native Object Unsafe.allocateUninitializedArray0(Class<?> cls, int size);
3440 bool LibraryCallKit::inline_unsafe_newArray(bool uninitialized) {
3441   Node* mirror;
3442   Node* count_val;
3443   if (uninitialized) {
3444     mirror    = argument(1);
3445     count_val = argument(2);
3446   } else {
3447     mirror    = argument(0);
3448     count_val = argument(1);
3449   }
3450 
3451   mirror = null_check(mirror);
3452   // If mirror or obj is dead, only null-path is taken.
3453   if (stopped())  return true;
3454 
3455   enum { _normal_path = 1, _slow_path = 2, PATH_LIMIT };
3456   RegionNode* result_reg = new RegionNode(PATH_LIMIT);
3457   PhiNode*    result_val = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
3458   PhiNode*    result_io  = new PhiNode(result_reg, Type::ABIO);
3459   PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
3460 
3461   bool never_see_null = !too_many_traps(Deoptimization::Reason_null_check);
3462   Node* klass_node = load_array_klass_from_mirror(mirror, never_see_null,
3463                                                   result_reg, _slow_path);
3464   Node* normal_ctl   = control();
3465   Node* no_array_ctl = result_reg->in(_slow_path);
3466 
3467   // Generate code for the slow case.  We make a call to newArray().
3468   set_control(no_array_ctl);
3469   if (!stopped()) {
3470     // Either the input type is void.class, or else the
3471     // array klass has not yet been cached.  Either the
3472     // ensuing call will throw an exception, or else it
3473     // will cache the array klass for next time.
3474     PreserveJVMState pjvms(this);
3475     CallJavaNode* slow_call = nullptr;
3476     if (uninitialized) {
3477       // Generate optimized virtual call (holder class 'Unsafe' is final)
3478       slow_call = generate_method_call(vmIntrinsics::_allocateUninitializedArray, false, false, true);
3479     } else {
3480       slow_call = generate_method_call_static(vmIntrinsics::_newArray, true);
3481     }
3482     Node* slow_result = set_results_for_java_call(slow_call);
3483     // this->control() comes from set_results_for_java_call
3484     result_reg->set_req(_slow_path, control());
3485     result_val->set_req(_slow_path, slow_result);
3486     result_io ->set_req(_slow_path, i_o());
3487     result_mem->set_req(_slow_path, reset_memory());
3488   }
3489 
3490   set_control(normal_ctl);
3491   if (!stopped()) {
3492     // Normal case:  The array type has been cached in the java.lang.Class.
3493     // The following call works fine even if the array type is polymorphic.
3494     // It could be a dynamic mix of int[], boolean[], Object[], etc.
3495     Node* obj = new_array(klass_node, count_val, 0);  // no arguments to push
3496     result_reg->init_req(_normal_path, control());
3497     result_val->init_req(_normal_path, obj);
3498     result_io ->init_req(_normal_path, i_o());
3499     result_mem->init_req(_normal_path, reset_memory());
3500 
3501     if (uninitialized) {
3502       // Mark the allocation so that zeroing is skipped
3503       AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(obj, &_gvn);
3504       alloc->maybe_set_complete(&_gvn);
3505     }
3506   }
3507 
3508   // Return the combined state.
3509   set_i_o(        _gvn.transform(result_io)  );
3510   set_all_memory( _gvn.transform(result_mem));
3511 
3512   C->set_has_split_ifs(true); // Has chance for split-if optimization
3513   set_result(result_reg, result_val);
3514   return true;
3515 }
3516 
3517 //----------------------inline_native_getLength--------------------------
3518 // public static native int java.lang.reflect.Array.getLength(Object array);
3519 bool LibraryCallKit::inline_native_getLength() {
3520   if (too_many_traps(Deoptimization::Reason_intrinsic))  return false;
3521 
3522   Node* array = null_check(argument(0));
3523   // If array is dead, only null-path is taken.
3524   if (stopped())  return true;
3525 
3526   // Deoptimize if it is a non-array.
3527   Node* non_array = generate_non_array_guard(load_object_klass(array), nullptr);
3528 
3529   if (non_array != nullptr) {
3530     PreserveJVMState pjvms(this);
3531     set_control(non_array);
3532     uncommon_trap(Deoptimization::Reason_intrinsic,
3533                   Deoptimization::Action_maybe_recompile);
3534   }
3535 
3536   // If control is dead, only non-array-path is taken.
3537   if (stopped())  return true;
3538 
3539   // The works fine even if the array type is polymorphic.
3540   // It could be a dynamic mix of int[], boolean[], Object[], etc.
3541   Node* result = load_array_length(array);
3542 
3543   C->set_has_split_ifs(true);  // Has chance for split-if optimization
3544   set_result(result);
3545   return true;
3546 }
3547 
3548 //------------------------inline_array_copyOf----------------------------
3549 // public static <T,U> T[] java.util.Arrays.copyOf(     U[] original, int newLength,         Class<? extends T[]> newType);
3550 // public static <T,U> T[] java.util.Arrays.copyOfRange(U[] original, int from,      int to, Class<? extends T[]> newType);
3551 bool LibraryCallKit::inline_array_copyOf(bool is_copyOfRange) {
3552   if (too_many_traps(Deoptimization::Reason_intrinsic))  return false;
3553 
3554   // Get the arguments.
3555   Node* original          = argument(0);
3556   Node* start             = is_copyOfRange? argument(1): intcon(0);
3557   Node* end               = is_copyOfRange? argument(2): argument(1);
3558   Node* array_type_mirror = is_copyOfRange? argument(3): argument(2);
3559 
3560   Node* newcopy = nullptr;
3561 
3562   // Set the original stack and the reexecute bit for the interpreter to reexecute
3563   // the bytecode that invokes Arrays.copyOf if deoptimization happens.
3564   { PreserveReexecuteState preexecs(this);
3565     jvms()->set_should_reexecute(true);
3566 
3567     array_type_mirror = null_check(array_type_mirror);
3568     original          = null_check(original);
3569 
3570     // Check if a null path was taken unconditionally.
3571     if (stopped())  return true;
3572 
3573     Node* orig_length = load_array_length(original);
3574 
3575     Node* klass_node = load_klass_from_mirror(array_type_mirror, false, nullptr, 0);
3576     klass_node = null_check(klass_node);
3577 
3578     RegionNode* bailout = new RegionNode(1);
3579     record_for_igvn(bailout);
3580 
3581     // Despite the generic type of Arrays.copyOf, the mirror might be int, int[], etc.
3582     // Bail out if that is so.
3583     Node* not_objArray = generate_non_objArray_guard(klass_node, bailout);
3584     if (not_objArray != nullptr) {
3585       // Improve the klass node's type from the new optimistic assumption:
3586       ciKlass* ak = ciArrayKlass::make(env()->Object_klass());
3587       const Type* akls = TypeKlassPtr::make(TypePtr::NotNull, ak, 0/*offset*/);
3588       Node* cast = new CastPPNode(klass_node, akls);
3589       cast->init_req(0, control());
3590       klass_node = _gvn.transform(cast);
3591     }
3592 
3593     // Bail out if either start or end is negative.
3594     generate_negative_guard(start, bailout, &start);
3595     generate_negative_guard(end,   bailout, &end);
3596 
3597     Node* length = end;
3598     if (_gvn.type(start) != TypeInt::ZERO) {
3599       length = _gvn.transform(new SubINode(end, start));
3600     }
3601 
3602     // Bail out if length is negative.
3603     // Without this the new_array would throw
3604     // NegativeArraySizeException but IllegalArgumentException is what
3605     // should be thrown
3606     generate_negative_guard(length, bailout, &length);
3607 
3608     if (bailout->req() > 1) {
3609       PreserveJVMState pjvms(this);
3610       set_control(_gvn.transform(bailout));
3611       uncommon_trap(Deoptimization::Reason_intrinsic,
3612                     Deoptimization::Action_maybe_recompile);
3613     }
3614 
3615     if (!stopped()) {
3616       // How many elements will we copy from the original?
3617       // The answer is MinI(orig_length - start, length).
3618       Node* orig_tail = _gvn.transform(new SubINode(orig_length, start));
3619       Node* moved = generate_min_max(vmIntrinsics::_min, orig_tail, length);
3620 
3621       // Generate a direct call to the right arraycopy function(s).
3622       // We know the copy is disjoint but we might not know if the
3623       // oop stores need checking.
3624       // Extreme case:  Arrays.copyOf((Integer[])x, 10, String[].class).
3625       // This will fail a store-check if x contains any non-nulls.
3626 
3627       // ArrayCopyNode:Ideal may transform the ArrayCopyNode to
3628       // loads/stores but it is legal only if we're sure the
3629       // Arrays.copyOf would succeed. So we need all input arguments
3630       // to the copyOf to be validated, including that the copy to the
3631       // new array won't trigger an ArrayStoreException. That subtype
3632       // check can be optimized if we know something on the type of
3633       // the input array from type speculation.
3634       if (_gvn.type(klass_node)->singleton()) {
3635         ciKlass* subk   = _gvn.type(load_object_klass(original))->is_klassptr()->klass();
3636         ciKlass* superk = _gvn.type(klass_node)->is_klassptr()->klass();
3637 
3638         int test = C->static_subtype_check(superk, subk);
3639         if (test != Compile::SSC_always_true && test != Compile::SSC_always_false) {
3640           const TypeOopPtr* t_original = _gvn.type(original)->is_oopptr();
3641           if (t_original->speculative_type() != nullptr) {
3642             original = maybe_cast_profiled_obj(original, t_original->speculative_type(), true);
3643           }
3644         }
3645       }
3646 
3647       bool validated = false;
3648       // Reason_class_check rather than Reason_intrinsic because we
3649       // want to intrinsify even if this traps.
3650       if (!too_many_traps(Deoptimization::Reason_class_check)) {
3651         Node* not_subtype_ctrl = gen_subtype_check(original, klass_node);
3652 
3653         if (not_subtype_ctrl != top()) {
3654           PreserveJVMState pjvms(this);
3655           set_control(not_subtype_ctrl);
3656           uncommon_trap(Deoptimization::Reason_class_check,
3657                         Deoptimization::Action_make_not_entrant);
3658           assert(stopped(), "Should be stopped");
3659         }
3660         validated = true;
3661       }
3662 
3663       if (!stopped()) {
3664         newcopy = new_array(klass_node, length, 0);  // no arguments to push
3665 
3666         ArrayCopyNode* ac = ArrayCopyNode::make(this, true, original, start, newcopy, intcon(0), moved, true, false,
3667                                                 load_object_klass(original), klass_node);
3668         if (!is_copyOfRange) {
3669           ac->set_copyof(validated);
3670         } else {
3671           ac->set_copyofrange(validated);
3672         }
3673         Node* n = _gvn.transform(ac);
3674         if (n == ac) {
3675           ac->connect_outputs(this);
3676         } else {
3677           assert(validated, "shouldn't transform if all arguments not validated");
3678           set_all_memory(n);
3679         }
3680       }
3681     }
3682   } // original reexecute is set back here
3683 
3684   C->set_has_split_ifs(true); // Has chance for split-if optimization
3685   if (!stopped()) {
3686     set_result(newcopy);
3687   }
3688   return true;
3689 }
3690 
3691 
3692 //----------------------generate_virtual_guard---------------------------
3693 // Helper for hashCode and clone.  Peeks inside the vtable to avoid a call.
3694 Node* LibraryCallKit::generate_virtual_guard(Node* obj_klass,
3695                                              RegionNode* slow_region) {
3696   ciMethod* method = callee();
3697   int vtable_index = method->vtable_index();
3698   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
3699          "bad index %d", vtable_index);
3700   // Get the Method* out of the appropriate vtable entry.
3701   int entry_offset  = in_bytes(Klass::vtable_start_offset()) +
3702                      vtable_index*vtableEntry::size_in_bytes() +
3703                      vtableEntry::method_offset_in_bytes();
3704   Node* entry_addr  = basic_plus_adr(obj_klass, entry_offset);
3705   Node* target_call = make_load(nullptr, entry_addr, TypePtr::NOTNULL, T_ADDRESS, MemNode::unordered);
3706 
3707   // Compare the target method with the expected method (e.g., Object.hashCode).
3708   const TypePtr* native_call_addr = TypeMetadataPtr::make(method);
3709 
3710   Node* native_call = makecon(native_call_addr);
3711   Node* chk_native  = _gvn.transform(new CmpPNode(target_call, native_call));
3712   Node* test_native = _gvn.transform(new BoolNode(chk_native, BoolTest::ne));
3713 
3714   return generate_slow_guard(test_native, slow_region);
3715 }
3716 
3717 //-----------------------generate_method_call----------------------------
3718 // Use generate_method_call to make a slow-call to the real
3719 // method if the fast path fails.  An alternative would be to
3720 // use a stub like OptoRuntime::slow_arraycopy_Java.
3721 // This only works for expanding the current library call,
3722 // not another intrinsic.  (E.g., don't use this for making an
3723 // arraycopy call inside of the copyOf intrinsic.)
3724 CallJavaNode*
3725 LibraryCallKit::generate_method_call(vmIntrinsicID method_id, bool is_virtual, bool is_static, bool res_not_null) {
3726   // When compiling the intrinsic method itself, do not use this technique.
3727   guarantee(callee() != C->method(), "cannot make slow-call to self");
3728 
3729   ciMethod* method = callee();
3730   // ensure the JVMS we have will be correct for this call
3731   guarantee(method_id == method->intrinsic_id(), "must match");
3732 
3733   const TypeFunc* tf = TypeFunc::make(method);
3734   if (res_not_null) {
3735     assert(tf->return_type() == T_OBJECT, "");
3736     const TypeTuple* range = tf->range();
3737     const Type** fields = TypeTuple::fields(range->cnt());
3738     fields[TypeFunc::Parms] = range->field_at(TypeFunc::Parms)->filter_speculative(TypePtr::NOTNULL);
3739     const TypeTuple* new_range = TypeTuple::make(range->cnt(), fields);
3740     tf = TypeFunc::make(tf->domain(), new_range);
3741   }
3742   CallJavaNode* slow_call;
3743   if (is_static) {
3744     assert(!is_virtual, "");
3745     slow_call = new CallStaticJavaNode(C, tf,
3746                            SharedRuntime::get_resolve_static_call_stub(), method);
3747   } else if (is_virtual) {
3748     null_check_receiver();
3749     int vtable_index = Method::invalid_vtable_index;
3750     if (UseInlineCaches) {
3751       // Suppress the vtable call
3752     } else {
3753       // hashCode and clone are not a miranda methods,
3754       // so the vtable index is fixed.
3755       // No need to use the linkResolver to get it.
3756        vtable_index = method->vtable_index();
3757        assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index,
3758               "bad index %d", vtable_index);
3759     }
3760     slow_call = new CallDynamicJavaNode(tf,
3761                           SharedRuntime::get_resolve_virtual_call_stub(),
3762                           method, vtable_index);
3763   } else {  // neither virtual nor static:  opt_virtual
3764     null_check_receiver();
3765     slow_call = new CallStaticJavaNode(C, tf,
3766                                 SharedRuntime::get_resolve_opt_virtual_call_stub(), method);
3767     slow_call->set_optimized_virtual(true);
3768   }
3769   if (CallGenerator::is_inlined_method_handle_intrinsic(this->method(), bci(), callee())) {
3770     // To be able to issue a direct call (optimized virtual or virtual)
3771     // and skip a call to MH.linkTo*/invokeBasic adapter, additional information
3772     // about the method being invoked should be attached to the call site to
3773     // make resolution logic work (see SharedRuntime::resolve_{virtual,opt_virtual}_call_C).
3774     slow_call->set_override_symbolic_info(true);
3775   }
3776   set_arguments_for_java_call(slow_call);
3777   set_edges_for_java_call(slow_call);
3778   return slow_call;
3779 }
3780 
3781 
3782 /**
3783  * Build special case code for calls to hashCode on an object. This call may
3784  * be virtual (invokevirtual) or bound (invokespecial). For each case we generate
3785  * slightly different code.
3786  */
3787 bool LibraryCallKit::inline_native_hashcode(bool is_virtual, bool is_static) {
3788   assert(is_static == callee()->is_static(), "correct intrinsic selection");
3789   assert(!(is_virtual && is_static), "either virtual, special, or static");
3790 
3791   enum { _slow_path = 1, _fast_path, _null_path, PATH_LIMIT };
3792 
3793   RegionNode* result_reg = new RegionNode(PATH_LIMIT);
3794   PhiNode*    result_val = new PhiNode(result_reg, TypeInt::INT);
3795   PhiNode*    result_io  = new PhiNode(result_reg, Type::ABIO);
3796   PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
3797   Node* obj = nullptr;
3798   if (!is_static) {
3799     // Check for hashing null object
3800     obj = null_check_receiver();
3801     if (stopped())  return true;        // unconditionally null
3802     result_reg->init_req(_null_path, top());
3803     result_val->init_req(_null_path, top());
3804   } else {
3805     // Do a null check, and return zero if null.
3806     // System.identityHashCode(null) == 0
3807     obj = argument(0);
3808     Node* null_ctl = top();
3809     obj = null_check_oop(obj, &null_ctl);
3810     result_reg->init_req(_null_path, null_ctl);
3811     result_val->init_req(_null_path, _gvn.intcon(0));
3812   }
3813 
3814   // Unconditionally null?  Then return right away.
3815   if (stopped()) {
3816     set_control( result_reg->in(_null_path));
3817     if (!stopped())
3818       set_result(result_val->in(_null_path));
3819     return true;
3820   }
3821 
3822   // We only go to the fast case code if we pass a number of guards.  The
3823   // paths which do not pass are accumulated in the slow_region.
3824   RegionNode* slow_region = new RegionNode(1);
3825   record_for_igvn(slow_region);
3826 
3827   // If this is a virtual call, we generate a funny guard.  We pull out
3828   // the vtable entry corresponding to hashCode() from the target object.
3829   // If the target method which we are calling happens to be the native
3830   // Object hashCode() method, we pass the guard.  We do not need this
3831   // guard for non-virtual calls -- the caller is known to be the native
3832   // Object hashCode().
3833   if (is_virtual) {
3834     // After null check, get the object's klass.
3835     Node* obj_klass = load_object_klass(obj);
3836     generate_virtual_guard(obj_klass, slow_region);
3837   }
3838 
3839   // Get the header out of the object, use LoadMarkNode when available
3840   Node* header_addr = basic_plus_adr(obj, oopDesc::mark_offset_in_bytes());
3841   // The control of the load must be null. Otherwise, the load can move before
3842   // the null check after castPP removal.
3843   Node* no_ctrl = nullptr;
3844   Node* header = make_load(no_ctrl, header_addr, TypeX_X, TypeX_X->basic_type(), MemNode::unordered);
3845 
3846   // Test the header to see if it is safe to read w.r.t. locking.
3847   Node *lock_mask      = _gvn.MakeConX(markWord::biased_lock_mask_in_place);
3848   Node *lmasked_header = _gvn.transform(new AndXNode(header, lock_mask));
3849   if (LockingMode == LM_LIGHTWEIGHT) {
3850     Node *monitor_val   = _gvn.MakeConX(markWord::monitor_value);
3851     Node *chk_monitor   = _gvn.transform(new CmpXNode(lmasked_header, monitor_val));
3852     Node *test_monitor  = _gvn.transform(new BoolNode(chk_monitor, BoolTest::eq));
3853 
3854     generate_slow_guard(test_monitor, slow_region);
3855   } else {
3856     Node *unlocked_val      = _gvn.MakeConX(markWord::unlocked_value);
3857     Node *chk_unlocked      = _gvn.transform(new CmpXNode(lmasked_header, unlocked_val));
3858     Node *test_not_unlocked = _gvn.transform(new BoolNode(chk_unlocked, BoolTest::ne));
3859 
3860     generate_slow_guard(test_not_unlocked, slow_region);
3861   }
3862 
3863   // Get the hash value and check to see that it has been properly assigned.
3864   // We depend on hash_mask being at most 32 bits and avoid the use of
3865   // hash_mask_in_place because it could be larger than 32 bits in a 64-bit
3866   // vm: see markWord.hpp.
3867   Node *hash_mask      = _gvn.intcon(UseCompactObjectHeaders ? markWord::hash_mask_compact  : markWord::hash_mask);
3868   Node *hash_shift     = _gvn.intcon(UseCompactObjectHeaders ? markWord::hash_shift_compact : markWord::hash_shift);
3869   Node *hshifted_header= _gvn.transform(new URShiftXNode(header, hash_shift));
3870   // This hack lets the hash bits live anywhere in the mark object now, as long
3871   // as the shift drops the relevant bits into the low 32 bits.  Note that
3872   // Java spec says that HashCode is an int so there's no point in capturing
3873   // an 'X'-sized hashcode (32 in 32-bit build or 64 in 64-bit build).
3874   hshifted_header      = ConvX2I(hshifted_header);
3875   Node *hash_val       = _gvn.transform(new AndINode(hshifted_header, hash_mask));
3876 
3877   Node *no_hash_val    = _gvn.intcon(markWord::no_hash);
3878   Node *chk_assigned   = _gvn.transform(new CmpINode( hash_val, no_hash_val));
3879   Node *test_assigned  = _gvn.transform(new BoolNode( chk_assigned, BoolTest::eq));
3880 
3881   generate_slow_guard(test_assigned, slow_region);
3882 
3883   Node* init_mem = reset_memory();
3884   // fill in the rest of the null path:
3885   result_io ->init_req(_null_path, i_o());
3886   result_mem->init_req(_null_path, init_mem);
3887 
3888   result_val->init_req(_fast_path, hash_val);
3889   result_reg->init_req(_fast_path, control());
3890   result_io ->init_req(_fast_path, i_o());
3891   result_mem->init_req(_fast_path, init_mem);
3892 
3893   // Generate code for the slow case.  We make a call to hashCode().
3894   set_control(_gvn.transform(slow_region));
3895   if (!stopped()) {
3896     // No need for PreserveJVMState, because we're using up the present state.
3897     set_all_memory(init_mem);
3898     vmIntrinsics::ID hashCode_id = is_static ? vmIntrinsics::_identityHashCode : vmIntrinsics::_hashCode;
3899     CallJavaNode* slow_call = generate_method_call(hashCode_id, is_virtual, is_static, false);
3900     Node* slow_result = set_results_for_java_call(slow_call);
3901     // this->control() comes from set_results_for_java_call
3902     result_reg->init_req(_slow_path, control());
3903     result_val->init_req(_slow_path, slow_result);
3904     result_io  ->set_req(_slow_path, i_o());
3905     result_mem ->set_req(_slow_path, reset_memory());
3906   }
3907 
3908   // Return the combined state.
3909   set_i_o(        _gvn.transform(result_io)  );
3910   set_all_memory( _gvn.transform(result_mem));
3911 
3912   set_result(result_reg, result_val);
3913   return true;
3914 }
3915 
3916 //---------------------------inline_native_getClass----------------------------
3917 // public final native Class<?> java.lang.Object.getClass();
3918 //
3919 // Build special case code for calls to getClass on an object.
3920 bool LibraryCallKit::inline_native_getClass() {
3921   Node* obj = null_check_receiver();
3922   if (stopped())  return true;
3923   set_result(load_mirror_from_klass(load_object_klass(obj)));
3924   return true;
3925 }
3926 
3927 //-----------------inline_native_Reflection_getCallerClass---------------------
3928 // public static native Class<?> sun.reflect.Reflection.getCallerClass();
3929 //
3930 // In the presence of deep enough inlining, getCallerClass() becomes a no-op.
3931 //
3932 // NOTE: This code must perform the same logic as JVM_GetCallerClass
3933 // in that it must skip particular security frames and checks for
3934 // caller sensitive methods.
3935 bool LibraryCallKit::inline_native_Reflection_getCallerClass() {
3936 #ifndef PRODUCT
3937   if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
3938     tty->print_cr("Attempting to inline sun.reflect.Reflection.getCallerClass");
3939   }
3940 #endif
3941 
3942   if (!jvms()->has_method()) {
3943 #ifndef PRODUCT
3944     if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
3945       tty->print_cr("  Bailing out because intrinsic was inlined at top level");
3946     }
3947 #endif
3948     return false;
3949   }
3950 
3951   // Walk back up the JVM state to find the caller at the required
3952   // depth.
3953   JVMState* caller_jvms = jvms();
3954 
3955   // Cf. JVM_GetCallerClass
3956   // NOTE: Start the loop at depth 1 because the current JVM state does
3957   // not include the Reflection.getCallerClass() frame.
3958   for (int n = 1; caller_jvms != nullptr; caller_jvms = caller_jvms->caller(), n++) {
3959     ciMethod* m = caller_jvms->method();
3960     switch (n) {
3961     case 0:
3962       fatal("current JVM state does not include the Reflection.getCallerClass frame");
3963       break;
3964     case 1:
3965       // Frame 0 and 1 must be caller sensitive (see JVM_GetCallerClass).
3966       if (!m->caller_sensitive()) {
3967 #ifndef PRODUCT
3968         if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
3969           tty->print_cr("  Bailing out: CallerSensitive annotation expected at frame %d", n);
3970         }
3971 #endif
3972         return false;  // bail-out; let JVM_GetCallerClass do the work
3973       }
3974       break;
3975     default:
3976       if (!m->is_ignored_by_security_stack_walk()) {
3977         // We have reached the desired frame; return the holder class.
3978         // Acquire method holder as java.lang.Class and push as constant.
3979         ciInstanceKlass* caller_klass = caller_jvms->method()->holder();
3980         ciInstance* caller_mirror = caller_klass->java_mirror();
3981         set_result(makecon(TypeInstPtr::make(caller_mirror)));
3982 
3983 #ifndef PRODUCT
3984         if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
3985           tty->print_cr("  Succeeded: caller = %d) %s.%s, JVMS depth = %d", n, caller_klass->name()->as_utf8(), caller_jvms->method()->name()->as_utf8(), jvms()->depth());
3986           tty->print_cr("  JVM state at this point:");
3987           for (int i = jvms()->depth(), n = 1; i >= 1; i--, n++) {
3988             ciMethod* m = jvms()->of_depth(i)->method();
3989             tty->print_cr("   %d) %s.%s", n, m->holder()->name()->as_utf8(), m->name()->as_utf8());
3990           }
3991         }
3992 #endif
3993         return true;
3994       }
3995       break;
3996     }
3997   }
3998 
3999 #ifndef PRODUCT
4000   if ((C->print_intrinsics() || C->print_inlining()) && Verbose) {
4001     tty->print_cr("  Bailing out because caller depth exceeded inlining depth = %d", jvms()->depth());
4002     tty->print_cr("  JVM state at this point:");
4003     for (int i = jvms()->depth(), n = 1; i >= 1; i--, n++) {
4004       ciMethod* m = jvms()->of_depth(i)->method();
4005       tty->print_cr("   %d) %s.%s", n, m->holder()->name()->as_utf8(), m->name()->as_utf8());
4006     }
4007   }
4008 #endif
4009 
4010   return false;  // bail-out; let JVM_GetCallerClass do the work
4011 }
4012 
4013 bool LibraryCallKit::inline_fp_conversions(vmIntrinsics::ID id) {
4014   Node* arg = argument(0);
4015   Node* result = nullptr;
4016 
4017   switch (id) {
4018   case vmIntrinsics::_floatToRawIntBits:    result = new MoveF2INode(arg);  break;
4019   case vmIntrinsics::_intBitsToFloat:       result = new MoveI2FNode(arg);  break;
4020   case vmIntrinsics::_doubleToRawLongBits:  result = new MoveD2LNode(arg);  break;
4021   case vmIntrinsics::_longBitsToDouble:     result = new MoveL2DNode(arg);  break;
4022 
4023   case vmIntrinsics::_doubleToLongBits: {
4024     // two paths (plus control) merge in a wood
4025     RegionNode *r = new RegionNode(3);
4026     Node *phi = new PhiNode(r, TypeLong::LONG);
4027 
4028     Node *cmpisnan = _gvn.transform(new CmpDNode(arg, arg));
4029     // Build the boolean node
4030     Node *bolisnan = _gvn.transform(new BoolNode(cmpisnan, BoolTest::ne));
4031 
4032     // Branch either way.
4033     // NaN case is less traveled, which makes all the difference.
4034     IfNode *ifisnan = create_and_xform_if(control(), bolisnan, PROB_STATIC_FREQUENT, COUNT_UNKNOWN);
4035     Node *opt_isnan = _gvn.transform(ifisnan);
4036     assert( opt_isnan->is_If(), "Expect an IfNode");
4037     IfNode *opt_ifisnan = (IfNode*)opt_isnan;
4038     Node *iftrue = _gvn.transform(new IfTrueNode(opt_ifisnan));
4039 
4040     set_control(iftrue);
4041 
4042     static const jlong nan_bits = CONST64(0x7ff8000000000000);
4043     Node *slow_result = longcon(nan_bits); // return NaN
4044     phi->init_req(1, _gvn.transform( slow_result ));
4045     r->init_req(1, iftrue);
4046 
4047     // Else fall through
4048     Node *iffalse = _gvn.transform(new IfFalseNode(opt_ifisnan));
4049     set_control(iffalse);
4050 
4051     phi->init_req(2, _gvn.transform(new MoveD2LNode(arg)));
4052     r->init_req(2, iffalse);
4053 
4054     // Post merge
4055     set_control(_gvn.transform(r));
4056     record_for_igvn(r);
4057 
4058     C->set_has_split_ifs(true); // Has chance for split-if optimization
4059     result = phi;
4060     assert(result->bottom_type()->isa_long(), "must be");
4061     break;
4062   }
4063 
4064   case vmIntrinsics::_floatToIntBits: {
4065     // two paths (plus control) merge in a wood
4066     RegionNode *r = new RegionNode(3);
4067     Node *phi = new PhiNode(r, TypeInt::INT);
4068 
4069     Node *cmpisnan = _gvn.transform(new CmpFNode(arg, arg));
4070     // Build the boolean node
4071     Node *bolisnan = _gvn.transform(new BoolNode(cmpisnan, BoolTest::ne));
4072 
4073     // Branch either way.
4074     // NaN case is less traveled, which makes all the difference.
4075     IfNode *ifisnan = create_and_xform_if(control(), bolisnan, PROB_STATIC_FREQUENT, COUNT_UNKNOWN);
4076     Node *opt_isnan = _gvn.transform(ifisnan);
4077     assert( opt_isnan->is_If(), "Expect an IfNode");
4078     IfNode *opt_ifisnan = (IfNode*)opt_isnan;
4079     Node *iftrue = _gvn.transform(new IfTrueNode(opt_ifisnan));
4080 
4081     set_control(iftrue);
4082 
4083     static const jint nan_bits = 0x7fc00000;
4084     Node *slow_result = makecon(TypeInt::make(nan_bits)); // return NaN
4085     phi->init_req(1, _gvn.transform( slow_result ));
4086     r->init_req(1, iftrue);
4087 
4088     // Else fall through
4089     Node *iffalse = _gvn.transform(new IfFalseNode(opt_ifisnan));
4090     set_control(iffalse);
4091 
4092     phi->init_req(2, _gvn.transform(new MoveF2INode(arg)));
4093     r->init_req(2, iffalse);
4094 
4095     // Post merge
4096     set_control(_gvn.transform(r));
4097     record_for_igvn(r);
4098 
4099     C->set_has_split_ifs(true); // Has chance for split-if optimization
4100     result = phi;
4101     assert(result->bottom_type()->isa_int(), "must be");
4102     break;
4103   }
4104 
4105   default:
4106     fatal_unexpected_iid(id);
4107     break;
4108   }
4109   set_result(_gvn.transform(result));
4110   return true;
4111 }
4112 
4113 //----------------------inline_unsafe_copyMemory-------------------------
4114 // public native void Unsafe.copyMemory0(Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes);
4115 bool LibraryCallKit::inline_unsafe_copyMemory() {
4116   if (callee()->is_static())  return false;  // caller must have the capability!
4117   null_check_receiver();  // null-check receiver
4118   if (stopped())  return true;
4119 
4120   C->set_has_unsafe_access(true);  // Mark eventual nmethod as "unsafe".
4121 
4122   Node* src_ptr =         argument(1);   // type: oop
4123   Node* src_off = ConvL2X(argument(2));  // type: long
4124   Node* dst_ptr =         argument(4);   // type: oop
4125   Node* dst_off = ConvL2X(argument(5));  // type: long
4126   Node* size    = ConvL2X(argument(7));  // type: long
4127 
4128   assert(Unsafe_field_offset_to_byte_offset(11) == 11,
4129          "fieldOffset must be byte-scaled");
4130 
4131   Node* src = make_unsafe_address(src_ptr, src_off);
4132   Node* dst = make_unsafe_address(dst_ptr, dst_off);
4133 
4134   // Conservatively insert a memory barrier on all memory slices.
4135   // Do not let writes of the copy source or destination float below the copy.
4136   insert_mem_bar(Op_MemBarCPUOrder);
4137 
4138   Node* thread = _gvn.transform(new ThreadLocalNode());
4139   Node* doing_unsafe_access_addr = basic_plus_adr(top(), thread, in_bytes(JavaThread::doing_unsafe_access_offset()));
4140   BasicType doing_unsafe_access_bt = T_BYTE;
4141   assert((sizeof(bool) * CHAR_BIT) == 8, "not implemented");
4142 
4143   // update volatile field
4144   store_to_memory(control(), doing_unsafe_access_addr, intcon(1), doing_unsafe_access_bt, Compile::AliasIdxRaw, MemNode::unordered);
4145 
4146   // Call it.  Note that the length argument is not scaled.
4147   make_runtime_call(RC_LEAF|RC_NO_FP,
4148                     OptoRuntime::fast_arraycopy_Type(),
4149                     StubRoutines::unsafe_arraycopy(),
4150                     "unsafe_arraycopy",
4151                     TypeRawPtr::BOTTOM,
4152                     src, dst, size XTOP);
4153 
4154   store_to_memory(control(), doing_unsafe_access_addr, intcon(0), doing_unsafe_access_bt, Compile::AliasIdxRaw, MemNode::unordered);
4155 
4156   // Do not let reads of the copy destination float above the copy.
4157   insert_mem_bar(Op_MemBarCPUOrder);
4158 
4159   return true;
4160 }
4161 
4162 #undef XTOP
4163 
4164 //------------------------clone_coping-----------------------------------
4165 // Helper function for inline_native_clone.
4166 void LibraryCallKit::copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array) {
4167   assert(obj_size != nullptr, "");
4168   Node* raw_obj = alloc_obj->in(1);
4169   assert(alloc_obj->is_CheckCastPP() && raw_obj->is_Proj() && raw_obj->in(0)->is_Allocate(), "");
4170 
4171   AllocateNode* alloc = nullptr;
4172   if (ReduceBulkZeroing) {
4173     // We will be completely responsible for initializing this object -
4174     // mark Initialize node as complete.
4175     alloc = AllocateNode::Ideal_allocation(alloc_obj, &_gvn);
4176     // The object was just allocated - there should be no any stores!
4177     guarantee(alloc != nullptr && alloc->maybe_set_complete(&_gvn), "");
4178     // Mark as complete_with_arraycopy so that on AllocateNode
4179     // expansion, we know this AllocateNode is initialized by an array
4180     // copy and a StoreStore barrier exists after the array copy.
4181     alloc->initialization()->set_complete_with_arraycopy();
4182   }
4183 
4184   Node* size = _gvn.transform(obj_size);
4185   access_clone(obj, alloc_obj, size, is_array);
4186 
4187   // Do not let reads from the cloned object float above the arraycopy.
4188   if (alloc != nullptr) {
4189     // Do not let stores that initialize this object be reordered with
4190     // a subsequent store that would make this object accessible by
4191     // other threads.
4192     // Record what AllocateNode this StoreStore protects so that
4193     // escape analysis can go from the MemBarStoreStoreNode to the
4194     // AllocateNode and eliminate the MemBarStoreStoreNode if possible
4195     // based on the escape status of the AllocateNode.
4196     insert_mem_bar(Op_MemBarStoreStore, alloc->proj_out_or_null(AllocateNode::RawAddress));
4197   } else {
4198     insert_mem_bar(Op_MemBarCPUOrder);
4199   }
4200 }
4201 
4202 //------------------------inline_native_clone----------------------------
4203 // protected native Object java.lang.Object.clone();
4204 //
4205 // Here are the simple edge cases:
4206 //  null receiver => normal trap
4207 //  virtual and clone was overridden => slow path to out-of-line clone
4208 //  not cloneable or finalizer => slow path to out-of-line Object.clone
4209 //
4210 // The general case has two steps, allocation and copying.
4211 // Allocation has two cases, and uses GraphKit::new_instance or new_array.
4212 //
4213 // Copying also has two cases, oop arrays and everything else.
4214 // Oop arrays use arrayof_oop_arraycopy (same as System.arraycopy).
4215 // Everything else uses the tight inline loop supplied by CopyArrayNode.
4216 //
4217 // These steps fold up nicely if and when the cloned object's klass
4218 // can be sharply typed as an object array, a type array, or an instance.
4219 //
4220 bool LibraryCallKit::inline_native_clone(bool is_virtual) {
4221   PhiNode* result_val;
4222 
4223   // Set the reexecute bit for the interpreter to reexecute
4224   // the bytecode that invokes Object.clone if deoptimization happens.
4225   { PreserveReexecuteState preexecs(this);
4226     jvms()->set_should_reexecute(true);
4227 
4228     Node* obj = null_check_receiver();
4229     if (stopped())  return true;
4230 
4231     const TypeOopPtr* obj_type = _gvn.type(obj)->is_oopptr();
4232 
4233     // If we are going to clone an instance, we need its exact type to
4234     // know the number and types of fields to convert the clone to
4235     // loads/stores. Maybe a speculative type can help us.
4236     if (!obj_type->klass_is_exact() &&
4237         obj_type->speculative_type() != nullptr &&
4238         obj_type->speculative_type()->is_instance_klass()) {
4239       ciInstanceKlass* spec_ik = obj_type->speculative_type()->as_instance_klass();
4240       if (spec_ik->nof_nonstatic_fields() <= ArrayCopyLoadStoreMaxElem &&
4241           !spec_ik->has_injected_fields()) {
4242         ciKlass* k = obj_type->klass();
4243         if (!k->is_instance_klass() ||
4244             k->as_instance_klass()->is_interface() ||
4245             k->as_instance_klass()->has_subklass()) {
4246           obj = maybe_cast_profiled_obj(obj, obj_type->speculative_type(), false);
4247         }
4248       }
4249     }
4250 
4251     // Conservatively insert a memory barrier on all memory slices.
4252     // Do not let writes into the original float below the clone.
4253     insert_mem_bar(Op_MemBarCPUOrder);
4254 
4255     // paths into result_reg:
4256     enum {
4257       _slow_path = 1,     // out-of-line call to clone method (virtual or not)
4258       _objArray_path,     // plain array allocation, plus arrayof_oop_arraycopy
4259       _array_path,        // plain array allocation, plus arrayof_long_arraycopy
4260       _instance_path,     // plain instance allocation, plus arrayof_long_arraycopy
4261       PATH_LIMIT
4262     };
4263     RegionNode* result_reg = new RegionNode(PATH_LIMIT);
4264     result_val             = new PhiNode(result_reg, TypeInstPtr::NOTNULL);
4265     PhiNode*    result_i_o = new PhiNode(result_reg, Type::ABIO);
4266     PhiNode*    result_mem = new PhiNode(result_reg, Type::MEMORY, TypePtr::BOTTOM);
4267     record_for_igvn(result_reg);
4268 
4269     Node* obj_klass = load_object_klass(obj);
4270     Node* array_ctl = generate_array_guard(obj_klass, (RegionNode*)nullptr);
4271     if (array_ctl != nullptr) {
4272       // It's an array.
4273       PreserveJVMState pjvms(this);
4274       set_control(array_ctl);
4275       Node* obj_length = load_array_length(obj);
4276       Node* obj_size  = nullptr;
4277       Node* alloc_obj = new_array(obj_klass, obj_length, 0, &obj_size, /*deoptimize_on_exception=*/true);
4278 
4279       BarrierSetC2* bs = BarrierSet::barrier_set()->barrier_set_c2();
4280       if (bs->array_copy_requires_gc_barriers(true, T_OBJECT, true, false, BarrierSetC2::Parsing)) {
4281         // If it is an oop array, it requires very special treatment,
4282         // because gc barriers are required when accessing the array.
4283         Node* is_obja = generate_objArray_guard(obj_klass, (RegionNode*)nullptr);
4284         if (is_obja != nullptr) {
4285           PreserveJVMState pjvms2(this);
4286           set_control(is_obja);
4287           // Generate a direct call to the right arraycopy function(s).
4288           // Clones are always tightly coupled.
4289           ArrayCopyNode* ac = ArrayCopyNode::make(this, true, obj, intcon(0), alloc_obj, intcon(0), obj_length, true, false);
4290           ac->set_clone_oop_array();
4291           Node* n = _gvn.transform(ac);
4292           assert(n == ac, "cannot disappear");
4293           ac->connect_outputs(this, /*deoptimize_on_exception=*/true);
4294 
4295           result_reg->init_req(_objArray_path, control());
4296           result_val->init_req(_objArray_path, alloc_obj);
4297           result_i_o ->set_req(_objArray_path, i_o());
4298           result_mem ->set_req(_objArray_path, reset_memory());
4299         }
4300       }
4301       // Otherwise, there are no barriers to worry about.
4302       // (We can dispense with card marks if we know the allocation
4303       //  comes out of eden (TLAB)...  In fact, ReduceInitialCardMarks
4304       //  causes the non-eden paths to take compensating steps to
4305       //  simulate a fresh allocation, so that no further
4306       //  card marks are required in compiled code to initialize
4307       //  the object.)
4308 
4309       if (!stopped()) {
4310         copy_to_clone(obj, alloc_obj, obj_size, true);
4311 
4312         // Present the results of the copy.
4313         result_reg->init_req(_array_path, control());
4314         result_val->init_req(_array_path, alloc_obj);
4315         result_i_o ->set_req(_array_path, i_o());
4316         result_mem ->set_req(_array_path, reset_memory());
4317       }
4318     }
4319 
4320     // We only go to the instance fast case code if we pass a number of guards.
4321     // The paths which do not pass are accumulated in the slow_region.
4322     RegionNode* slow_region = new RegionNode(1);
4323     record_for_igvn(slow_region);
4324     if (!stopped()) {
4325       // It's an instance (we did array above).  Make the slow-path tests.
4326       // If this is a virtual call, we generate a funny guard.  We grab
4327       // the vtable entry corresponding to clone() from the target object.
4328       // If the target method which we are calling happens to be the
4329       // Object clone() method, we pass the guard.  We do not need this
4330       // guard for non-virtual calls; the caller is known to be the native
4331       // Object clone().
4332       if (is_virtual) {
4333         generate_virtual_guard(obj_klass, slow_region);
4334       }
4335 
4336       // The object must be easily cloneable and must not have a finalizer.
4337       // Both of these conditions may be checked in a single test.
4338       // We could optimize the test further, but we don't care.
4339       generate_access_flags_guard(obj_klass,
4340                                   // Test both conditions:
4341                                   JVM_ACC_IS_CLONEABLE_FAST | JVM_ACC_HAS_FINALIZER,
4342                                   // Must be cloneable but not finalizer:
4343                                   JVM_ACC_IS_CLONEABLE_FAST,
4344                                   slow_region);
4345     }
4346 
4347     if (!stopped()) {
4348       // It's an instance, and it passed the slow-path tests.
4349       PreserveJVMState pjvms(this);
4350       Node* obj_size  = nullptr;
4351       // Need to deoptimize on exception from allocation since Object.clone intrinsic
4352       // is reexecuted if deoptimization occurs and there could be problems when merging
4353       // exception state between multiple Object.clone versions (reexecute=true vs reexecute=false).
4354       Node* alloc_obj = new_instance(obj_klass, nullptr, &obj_size, /*deoptimize_on_exception=*/true);
4355 
4356       copy_to_clone(obj, alloc_obj, obj_size, false);
4357 
4358       // Present the results of the slow call.
4359       result_reg->init_req(_instance_path, control());
4360       result_val->init_req(_instance_path, alloc_obj);
4361       result_i_o ->set_req(_instance_path, i_o());
4362       result_mem ->set_req(_instance_path, reset_memory());
4363     }
4364 
4365     // Generate code for the slow case.  We make a call to clone().
4366     set_control(_gvn.transform(slow_region));
4367     if (!stopped()) {
4368       PreserveJVMState pjvms(this);
4369       CallJavaNode* slow_call = generate_method_call(vmIntrinsics::_clone, is_virtual, false, true);
4370       // We need to deoptimize on exception (see comment above)
4371       Node* slow_result = set_results_for_java_call(slow_call, false, /* deoptimize */ true);
4372       // this->control() comes from set_results_for_java_call
4373       result_reg->init_req(_slow_path, control());
4374       result_val->init_req(_slow_path, slow_result);
4375       result_i_o ->set_req(_slow_path, i_o());
4376       result_mem ->set_req(_slow_path, reset_memory());
4377     }
4378 
4379     // Return the combined state.
4380     set_control(    _gvn.transform(result_reg));
4381     set_i_o(        _gvn.transform(result_i_o));
4382     set_all_memory( _gvn.transform(result_mem));
4383   } // original reexecute is set back here
4384 
4385   set_result(_gvn.transform(result_val));
4386   return true;
4387 }
4388 
4389 // If we have a tightly coupled allocation, the arraycopy may take care
4390 // of the array initialization. If one of the guards we insert between
4391 // the allocation and the arraycopy causes a deoptimization, an
4392 // unitialized array will escape the compiled method. To prevent that
4393 // we set the JVM state for uncommon traps between the allocation and
4394 // the arraycopy to the state before the allocation so, in case of
4395 // deoptimization, we'll reexecute the allocation and the
4396 // initialization.
4397 JVMState* LibraryCallKit::arraycopy_restore_alloc_state(AllocateArrayNode* alloc, int& saved_reexecute_sp) {
4398   if (alloc != nullptr) {
4399     ciMethod* trap_method = alloc->jvms()->method();
4400     int trap_bci = alloc->jvms()->bci();
4401 
4402     if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &&
4403         !C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_null_check)) {
4404       // Make sure there's no store between the allocation and the
4405       // arraycopy otherwise visible side effects could be rexecuted
4406       // in case of deoptimization and cause incorrect execution.
4407       bool no_interfering_store = true;
4408       Node* mem = alloc->in(TypeFunc::Memory);
4409       if (mem->is_MergeMem()) {
4410         for (MergeMemStream mms(merged_memory(), mem->as_MergeMem()); mms.next_non_empty2(); ) {
4411           Node* n = mms.memory();
4412           if (n != mms.memory2() && !(n->is_Proj() && n->in(0) == alloc->initialization())) {
4413             assert(n->is_Store(), "what else?");
4414             no_interfering_store = false;
4415             break;
4416           }
4417         }
4418       } else {
4419         for (MergeMemStream mms(merged_memory()); mms.next_non_empty(); ) {
4420           Node* n = mms.memory();
4421           if (n != mem && !(n->is_Proj() && n->in(0) == alloc->initialization())) {
4422             assert(n->is_Store(), "what else?");
4423             no_interfering_store = false;
4424             break;
4425           }
4426         }
4427       }
4428 
4429       if (no_interfering_store) {
4430         SafePointNode* sfpt = create_safepoint_with_state_before_array_allocation(alloc);
4431 
4432         JVMState* saved_jvms = jvms();
4433         saved_reexecute_sp = _reexecute_sp;
4434 
4435         set_jvms(sfpt->jvms());
4436         _reexecute_sp = jvms()->sp();
4437 
4438         return saved_jvms;
4439       }
4440     }
4441   }
4442   return nullptr;
4443 }
4444 
4445 // Clone the JVMState of the array allocation and create a new safepoint with it. Re-push the array length to the stack
4446 // such that uncommon traps can be emitted to re-execute the array allocation in the interpreter.
4447 SafePointNode* LibraryCallKit::create_safepoint_with_state_before_array_allocation(const AllocateArrayNode* alloc) const {
4448   JVMState* old_jvms = alloc->jvms()->clone_shallow(C);
4449   uint size = alloc->req();
4450   SafePointNode* sfpt = new SafePointNode(size, old_jvms);
4451   old_jvms->set_map(sfpt);
4452   for (uint i = 0; i < size; i++) {
4453     sfpt->init_req(i, alloc->in(i));
4454   }
4455   // re-push array length for deoptimization
4456   sfpt->ins_req(old_jvms->stkoff() + old_jvms->sp(), alloc->in(AllocateNode::ALength));
4457   old_jvms->set_sp(old_jvms->sp()+1);
4458   old_jvms->set_monoff(old_jvms->monoff()+1);
4459   old_jvms->set_scloff(old_jvms->scloff()+1);
4460   old_jvms->set_endoff(old_jvms->endoff()+1);
4461   old_jvms->set_should_reexecute(true);
4462 
4463   sfpt->set_i_o(map()->i_o());
4464   sfpt->set_memory(map()->memory());
4465   sfpt->set_control(map()->control());
4466   return sfpt;
4467 }
4468 
4469 // In case of a deoptimization, we restart execution at the
4470 // allocation, allocating a new array. We would leave an uninitialized
4471 // array in the heap that GCs wouldn't expect. Move the allocation
4472 // after the traps so we don't allocate the array if we
4473 // deoptimize. This is possible because tightly_coupled_allocation()
4474 // guarantees there's no observer of the allocated array at this point
4475 // and the control flow is simple enough.
4476 void LibraryCallKit::arraycopy_move_allocation_here(AllocateArrayNode* alloc, Node* dest, JVMState* saved_jvms_before_guards,
4477                                                     int saved_reexecute_sp, uint new_idx) {
4478   if (saved_jvms_before_guards != nullptr && !stopped()) {
4479     replace_unrelated_uncommon_traps_with_alloc_state(alloc, saved_jvms_before_guards);
4480 
4481     assert(alloc != nullptr, "only with a tightly coupled allocation");
4482     // restore JVM state to the state at the arraycopy
4483     saved_jvms_before_guards->map()->set_control(map()->control());
4484     assert(saved_jvms_before_guards->map()->memory() == map()->memory(), "memory state changed?");
4485     assert(saved_jvms_before_guards->map()->i_o() == map()->i_o(), "IO state changed?");
4486     // If we've improved the types of some nodes (null check) while
4487     // emitting the guards, propagate them to the current state
4488     map()->replaced_nodes().apply(saved_jvms_before_guards->map(), new_idx);
4489     set_jvms(saved_jvms_before_guards);
4490     _reexecute_sp = saved_reexecute_sp;
4491 
4492     // Remove the allocation from above the guards
4493     CallProjections callprojs;
4494     alloc->extract_projections(&callprojs, true);
4495     InitializeNode* init = alloc->initialization();
4496     Node* alloc_mem = alloc->in(TypeFunc::Memory);
4497     C->gvn_replace_by(callprojs.fallthrough_ioproj, alloc->in(TypeFunc::I_O));
4498     C->gvn_replace_by(init->proj_out(TypeFunc::Memory), alloc_mem);
4499 
4500     // The CastIINode created in GraphKit::new_array (in AllocateArrayNode::make_ideal_length) must stay below
4501     // the allocation (i.e. is only valid if the allocation succeeds):
4502     // 1) replace CastIINode with AllocateArrayNode's length here
4503     // 2) Create CastIINode again once allocation has moved (see below) at the end of this method
4504     //
4505     // Multiple identical CastIINodes might exist here. Each GraphKit::load_array_length() call will generate
4506     // new separate CastIINode (arraycopy guard checks or any array length use between array allocation and ararycopy)
4507     Node* init_control = init->proj_out(TypeFunc::Control);
4508     Node* alloc_length = alloc->Ideal_length();
4509 #ifdef ASSERT
4510     Node* prev_cast = nullptr;
4511 #endif
4512     for (uint i = 0; i < init_control->outcnt(); i++) {
4513       Node* init_out = init_control->raw_out(i);
4514       if (init_out->is_CastII() && init_out->in(TypeFunc::Control) == init_control && init_out->in(1) == alloc_length) {
4515 #ifdef ASSERT
4516         if (prev_cast == nullptr) {
4517           prev_cast = init_out;
4518         } else {
4519           if (prev_cast->cmp(*init_out) == false) {
4520             prev_cast->dump();
4521             init_out->dump();
4522             assert(false, "not equal CastIINode");
4523           }
4524         }
4525 #endif
4526         C->gvn_replace_by(init_out, alloc_length);
4527       }
4528     }
4529     C->gvn_replace_by(init->proj_out(TypeFunc::Control), alloc->in(0));
4530 
4531     // move the allocation here (after the guards)
4532     _gvn.hash_delete(alloc);
4533     alloc->set_req(TypeFunc::Control, control());
4534     alloc->set_req(TypeFunc::I_O, i_o());
4535     Node *mem = reset_memory();
4536     set_all_memory(mem);
4537     alloc->set_req(TypeFunc::Memory, mem);
4538     set_control(init->proj_out_or_null(TypeFunc::Control));
4539     set_i_o(callprojs.fallthrough_ioproj);
4540 
4541     // Update memory as done in GraphKit::set_output_for_allocation()
4542     const TypeInt* length_type = _gvn.find_int_type(alloc->in(AllocateNode::ALength));
4543     const TypeOopPtr* ary_type = _gvn.type(alloc->in(AllocateNode::KlassNode))->is_klassptr()->as_instance_type();
4544     if (ary_type->isa_aryptr() && length_type != nullptr) {
4545       ary_type = ary_type->is_aryptr()->cast_to_size(length_type);
4546     }
4547     const TypePtr* telemref = ary_type->add_offset(Type::OffsetBot);
4548     int            elemidx  = C->get_alias_index(telemref);
4549     set_memory(init->proj_out_or_null(TypeFunc::Memory), Compile::AliasIdxRaw);
4550     set_memory(init->proj_out_or_null(TypeFunc::Memory), elemidx);
4551 
4552     Node* allocx = _gvn.transform(alloc);
4553     assert(allocx == alloc, "where has the allocation gone?");
4554     assert(dest->is_CheckCastPP(), "not an allocation result?");
4555 
4556     _gvn.hash_delete(dest);
4557     dest->set_req(0, control());
4558     Node* destx = _gvn.transform(dest);
4559     assert(destx == dest, "where has the allocation result gone?");
4560 
4561     array_ideal_length(alloc, ary_type, true);
4562   }
4563 }
4564 
4565 // Unrelated UCTs between the array allocation and the array copy, which are considered safe by tightly_coupled_allocation(),
4566 // need to be replaced by an UCT with a state before the array allocation (including the array length). This is necessary
4567 // because we could hit one of these UCTs (which are executed before the emitted array copy guards and the actual array
4568 // allocation which is moved down in arraycopy_move_allocation_here()). When later resuming execution in the interpreter,
4569 // we would have wrongly skipped the array allocation. To prevent this, we resume execution at the array allocation in
4570 // the interpreter similar to what we are doing for the newly emitted guards for the array copy.
4571 void LibraryCallKit::replace_unrelated_uncommon_traps_with_alloc_state(AllocateArrayNode* alloc,
4572                                                                        JVMState* saved_jvms_before_guards) {
4573   if (saved_jvms_before_guards->map()->control()->is_IfProj()) {
4574     // There is at least one unrelated uncommon trap which needs to be replaced.
4575     SafePointNode* sfpt = create_safepoint_with_state_before_array_allocation(alloc);
4576 
4577     JVMState* saved_jvms = jvms();
4578     const int saved_reexecute_sp = _reexecute_sp;
4579     set_jvms(sfpt->jvms());
4580     _reexecute_sp = jvms()->sp();
4581 
4582     replace_unrelated_uncommon_traps_with_alloc_state(saved_jvms_before_guards);
4583 
4584     // Restore state
4585     set_jvms(saved_jvms);
4586     _reexecute_sp = saved_reexecute_sp;
4587   }
4588 }
4589 
4590 // Replace the unrelated uncommon traps with new uncommon trap nodes by reusing the action and reason. The new uncommon
4591 // traps will have the state of the array allocation. Let the old uncommon trap nodes die.
4592 void LibraryCallKit::replace_unrelated_uncommon_traps_with_alloc_state(JVMState* saved_jvms_before_guards) {
4593   Node* if_proj = saved_jvms_before_guards->map()->control(); // Start the search right before the newly emitted guards
4594   while (if_proj->is_IfProj()) {
4595     CallStaticJavaNode* uncommon_trap = get_uncommon_trap_from_success_proj(if_proj);
4596     if (uncommon_trap != nullptr) {
4597       create_new_uncommon_trap(uncommon_trap);
4598     }
4599     assert(if_proj->in(0)->is_If(), "must be If");
4600     if_proj = if_proj->in(0)->in(0);
4601   }
4602   assert(if_proj->is_Proj() && if_proj->in(0)->is_Initialize(),
4603          "must have reached control projection of init node");
4604 }
4605 
4606 void LibraryCallKit::create_new_uncommon_trap(CallStaticJavaNode* uncommon_trap_call) {
4607   const int trap_request = uncommon_trap_call->uncommon_trap_request();
4608   assert(trap_request != 0, "no valid UCT trap request");
4609   PreserveJVMState pjvms(this);
4610   set_control(uncommon_trap_call->in(0));
4611   uncommon_trap(Deoptimization::trap_request_reason(trap_request),
4612                 Deoptimization::trap_request_action(trap_request));
4613   assert(stopped(), "Should be stopped");
4614   _gvn.hash_delete(uncommon_trap_call);
4615   uncommon_trap_call->set_req(0, top()); // not used anymore, kill it
4616 }
4617 
4618 //------------------------------inline_arraycopy-----------------------
4619 // public static native void java.lang.System.arraycopy(Object src,  int  srcPos,
4620 //                                                      Object dest, int destPos,
4621 //                                                      int length);
4622 bool LibraryCallKit::inline_arraycopy() {
4623   // Get the arguments.
4624   Node* src         = argument(0);  // type: oop
4625   Node* src_offset  = argument(1);  // type: int
4626   Node* dest        = argument(2);  // type: oop
4627   Node* dest_offset = argument(3);  // type: int
4628   Node* length      = argument(4);  // type: int
4629 
4630   uint new_idx = C->unique();
4631 
4632   // Check for allocation before we add nodes that would confuse
4633   // tightly_coupled_allocation()
4634   AllocateArrayNode* alloc = tightly_coupled_allocation(dest);
4635 
4636   int saved_reexecute_sp = -1;
4637   JVMState* saved_jvms_before_guards = arraycopy_restore_alloc_state(alloc, saved_reexecute_sp);
4638   // See arraycopy_restore_alloc_state() comment
4639   // if alloc == null we don't have to worry about a tightly coupled allocation so we can emit all needed guards
4640   // if saved_jvms_before_guards is not null (then alloc is not null) then we can handle guards and a tightly coupled allocation
4641   // if saved_jvms_before_guards is null and alloc is not null, we can't emit any guards
4642   bool can_emit_guards = (alloc == nullptr || saved_jvms_before_guards != nullptr);
4643 
4644   // The following tests must be performed
4645   // (1) src and dest are arrays.
4646   // (2) src and dest arrays must have elements of the same BasicType
4647   // (3) src and dest must not be null.
4648   // (4) src_offset must not be negative.
4649   // (5) dest_offset must not be negative.
4650   // (6) length must not be negative.
4651   // (7) src_offset + length must not exceed length of src.
4652   // (8) dest_offset + length must not exceed length of dest.
4653   // (9) each element of an oop array must be assignable
4654 
4655   // (3) src and dest must not be null.
4656   // always do this here because we need the JVM state for uncommon traps
4657   Node* null_ctl = top();
4658   src  = saved_jvms_before_guards != nullptr ? null_check_oop(src, &null_ctl, true, true) : null_check(src, T_ARRAY);
4659   assert(null_ctl->is_top(), "no null control here");
4660   dest = null_check(dest, T_ARRAY);
4661 
4662   if (!can_emit_guards) {
4663     // if saved_jvms_before_guards is null and alloc is not null, we don't emit any
4664     // guards but the arraycopy node could still take advantage of a
4665     // tightly allocated allocation. tightly_coupled_allocation() is
4666     // called again to make sure it takes the null check above into
4667     // account: the null check is mandatory and if it caused an
4668     // uncommon trap to be emitted then the allocation can't be
4669     // considered tightly coupled in this context.
4670     alloc = tightly_coupled_allocation(dest);
4671   }
4672 
4673   bool validated = false;
4674 
4675   const Type* src_type  = _gvn.type(src);
4676   const Type* dest_type = _gvn.type(dest);
4677   const TypeAryPtr* top_src  = src_type->isa_aryptr();
4678   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
4679 
4680   // Do we have the type of src?
4681   bool has_src = (top_src != nullptr && top_src->klass() != nullptr);
4682   // Do we have the type of dest?
4683   bool has_dest = (top_dest != nullptr && top_dest->klass() != nullptr);
4684   // Is the type for src from speculation?
4685   bool src_spec = false;
4686   // Is the type for dest from speculation?
4687   bool dest_spec = false;
4688 
4689   if ((!has_src || !has_dest) && can_emit_guards) {
4690     // We don't have sufficient type information, let's see if
4691     // speculative types can help. We need to have types for both src
4692     // and dest so that it pays off.
4693 
4694     // Do we already have or could we have type information for src
4695     bool could_have_src = has_src;
4696     // Do we already have or could we have type information for dest
4697     bool could_have_dest = has_dest;
4698 
4699     ciKlass* src_k = nullptr;
4700     if (!has_src) {
4701       src_k = src_type->speculative_type_not_null();
4702       if (src_k != nullptr && src_k->is_array_klass()) {
4703         could_have_src = true;
4704       }
4705     }
4706 
4707     ciKlass* dest_k = nullptr;
4708     if (!has_dest) {
4709       dest_k = dest_type->speculative_type_not_null();
4710       if (dest_k != nullptr && dest_k->is_array_klass()) {
4711         could_have_dest = true;
4712       }
4713     }
4714 
4715     if (could_have_src && could_have_dest) {
4716       // This is going to pay off so emit the required guards
4717       if (!has_src) {
4718         src = maybe_cast_profiled_obj(src, src_k, true);
4719         src_type  = _gvn.type(src);
4720         top_src  = src_type->isa_aryptr();
4721         has_src = (top_src != nullptr && top_src->klass() != nullptr);
4722         src_spec = true;
4723       }
4724       if (!has_dest) {
4725         dest = maybe_cast_profiled_obj(dest, dest_k, true);
4726         dest_type  = _gvn.type(dest);
4727         top_dest  = dest_type->isa_aryptr();
4728         has_dest = (top_dest != nullptr && top_dest->klass() != nullptr);
4729         dest_spec = true;
4730       }
4731     }
4732   }
4733 
4734   if (has_src && has_dest && can_emit_guards) {
4735     BasicType src_elem  = top_src->klass()->as_array_klass()->element_type()->basic_type();
4736     BasicType dest_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
4737     if (is_reference_type(src_elem))   src_elem  = T_OBJECT;
4738     if (is_reference_type(dest_elem))  dest_elem = T_OBJECT;
4739 
4740     if (src_elem == dest_elem && src_elem == T_OBJECT) {
4741       // If both arrays are object arrays then having the exact types
4742       // for both will remove the need for a subtype check at runtime
4743       // before the call and may make it possible to pick a faster copy
4744       // routine (without a subtype check on every element)
4745       // Do we have the exact type of src?
4746       bool could_have_src = src_spec;
4747       // Do we have the exact type of dest?
4748       bool could_have_dest = dest_spec;
4749       ciKlass* src_k = top_src->klass();
4750       ciKlass* dest_k = top_dest->klass();
4751       if (!src_spec) {
4752         src_k = src_type->speculative_type_not_null();
4753         if (src_k != nullptr && src_k->is_array_klass()) {
4754           could_have_src = true;
4755         }
4756       }
4757       if (!dest_spec) {
4758         dest_k = dest_type->speculative_type_not_null();
4759         if (dest_k != nullptr && dest_k->is_array_klass()) {
4760           could_have_dest = true;
4761         }
4762       }
4763       if (could_have_src && could_have_dest) {
4764         // If we can have both exact types, emit the missing guards
4765         if (could_have_src && !src_spec) {
4766           src = maybe_cast_profiled_obj(src, src_k, true);
4767         }
4768         if (could_have_dest && !dest_spec) {
4769           dest = maybe_cast_profiled_obj(dest, dest_k, true);
4770         }
4771       }
4772     }
4773   }
4774 
4775   ciMethod* trap_method = method();
4776   int trap_bci = bci();
4777   if (saved_jvms_before_guards != nullptr) {
4778     trap_method = alloc->jvms()->method();
4779     trap_bci = alloc->jvms()->bci();
4780   }
4781 
4782   bool negative_length_guard_generated = false;
4783 
4784   if (!C->too_many_traps(trap_method, trap_bci, Deoptimization::Reason_intrinsic) &&
4785       can_emit_guards &&
4786       !src->is_top() && !dest->is_top()) {
4787     // validate arguments: enables transformation the ArrayCopyNode
4788     validated = true;
4789 
4790     RegionNode* slow_region = new RegionNode(1);
4791     record_for_igvn(slow_region);
4792 
4793     // (1) src and dest are arrays.
4794     generate_non_array_guard(load_object_klass(src), slow_region);
4795     generate_non_array_guard(load_object_klass(dest), slow_region);
4796 
4797     // (2) src and dest arrays must have elements of the same BasicType
4798     // done at macro expansion or at Ideal transformation time
4799 
4800     // (4) src_offset must not be negative.
4801     generate_negative_guard(src_offset, slow_region);
4802 
4803     // (5) dest_offset must not be negative.
4804     generate_negative_guard(dest_offset, slow_region);
4805 
4806     // (7) src_offset + length must not exceed length of src.
4807     generate_limit_guard(src_offset, length,
4808                          load_array_length(src),
4809                          slow_region);
4810 
4811     // (8) dest_offset + length must not exceed length of dest.
4812     generate_limit_guard(dest_offset, length,
4813                          load_array_length(dest),
4814                          slow_region);
4815 
4816     // (6) length must not be negative.
4817     // This is also checked in generate_arraycopy() during macro expansion, but
4818     // we also have to check it here for the case where the ArrayCopyNode will
4819     // be eliminated by Escape Analysis.
4820     if (EliminateAllocations) {
4821       generate_negative_guard(length, slow_region);
4822       negative_length_guard_generated = true;
4823     }
4824 
4825     // (9) each element of an oop array must be assignable
4826     Node* dest_klass = load_object_klass(dest);
4827     if (src != dest) {
4828       Node* not_subtype_ctrl = gen_subtype_check(src, dest_klass);
4829 
4830       if (not_subtype_ctrl != top()) {
4831         PreserveJVMState pjvms(this);
4832         set_control(not_subtype_ctrl);
4833         uncommon_trap(Deoptimization::Reason_intrinsic,
4834                       Deoptimization::Action_make_not_entrant);
4835         assert(stopped(), "Should be stopped");
4836       }
4837     }
4838     {
4839       PreserveJVMState pjvms(this);
4840       set_control(_gvn.transform(slow_region));
4841       uncommon_trap(Deoptimization::Reason_intrinsic,
4842                     Deoptimization::Action_make_not_entrant);
4843       assert(stopped(), "Should be stopped");
4844     }
4845 
4846     const TypeKlassPtr* dest_klass_t = _gvn.type(dest_klass)->is_klassptr();
4847     const Type *toop = TypeOopPtr::make_from_klass(dest_klass_t->klass());
4848     src = _gvn.transform(new CheckCastPPNode(control(), src, toop));
4849     arraycopy_move_allocation_here(alloc, dest, saved_jvms_before_guards, saved_reexecute_sp, new_idx);
4850   }
4851 
4852   if (stopped()) {
4853     return true;
4854   }
4855 
4856   ArrayCopyNode* ac = ArrayCopyNode::make(this, true, src, src_offset, dest, dest_offset, length, alloc != nullptr, negative_length_guard_generated,
4857                                           // Create LoadRange and LoadKlass nodes for use during macro expansion here
4858                                           // so the compiler has a chance to eliminate them: during macro expansion,
4859                                           // we have to set their control (CastPP nodes are eliminated).
4860                                           load_object_klass(src), load_object_klass(dest),
4861                                           load_array_length(src), load_array_length(dest));
4862 
4863   ac->set_arraycopy(validated);
4864 
4865   Node* n = _gvn.transform(ac);
4866   if (n == ac) {
4867     ac->connect_outputs(this);
4868   } else {
4869     assert(validated, "shouldn't transform if all arguments not validated");
4870     set_all_memory(n);
4871   }
4872   clear_upper_avx();
4873 
4874 
4875   return true;
4876 }
4877 
4878 
4879 // Helper function which determines if an arraycopy immediately follows
4880 // an allocation, with no intervening tests or other escapes for the object.
4881 AllocateArrayNode*
4882 LibraryCallKit::tightly_coupled_allocation(Node* ptr) {
4883   if (stopped())             return nullptr;  // no fast path
4884   if (C->AliasLevel() == 0)  return nullptr;  // no MergeMems around
4885 
4886   AllocateArrayNode* alloc = AllocateArrayNode::Ideal_array_allocation(ptr, &_gvn);
4887   if (alloc == nullptr)  return nullptr;
4888 
4889   Node* rawmem = memory(Compile::AliasIdxRaw);
4890   // Is the allocation's memory state untouched?
4891   if (!(rawmem->is_Proj() && rawmem->in(0)->is_Initialize())) {
4892     // Bail out if there have been raw-memory effects since the allocation.
4893     // (Example:  There might have been a call or safepoint.)
4894     return nullptr;
4895   }
4896   rawmem = rawmem->in(0)->as_Initialize()->memory(Compile::AliasIdxRaw);
4897   if (!(rawmem->is_Proj() && rawmem->in(0) == alloc)) {
4898     return nullptr;
4899   }
4900 
4901   // There must be no unexpected observers of this allocation.
4902   for (DUIterator_Fast imax, i = ptr->fast_outs(imax); i < imax; i++) {
4903     Node* obs = ptr->fast_out(i);
4904     if (obs != this->map()) {
4905       return nullptr;
4906     }
4907   }
4908 
4909   // This arraycopy must unconditionally follow the allocation of the ptr.
4910   Node* alloc_ctl = ptr->in(0);
4911   Node* ctl = control();
4912   while (ctl != alloc_ctl) {
4913     // There may be guards which feed into the slow_region.
4914     // Any other control flow means that we might not get a chance
4915     // to finish initializing the allocated object.
4916     // Various low-level checks bottom out in uncommon traps. These
4917     // are considered safe since we've already checked above that
4918     // there is no unexpected observer of this allocation.
4919     if (get_uncommon_trap_from_success_proj(ctl) != nullptr) {
4920       assert(ctl->in(0)->is_If(), "must be If");
4921       ctl = ctl->in(0)->in(0);
4922     } else {
4923       return nullptr;
4924     }
4925   }
4926 
4927   // If we get this far, we have an allocation which immediately
4928   // precedes the arraycopy, and we can take over zeroing the new object.
4929   // The arraycopy will finish the initialization, and provide
4930   // a new control state to which we will anchor the destination pointer.
4931 
4932   return alloc;
4933 }
4934 
4935 CallStaticJavaNode* LibraryCallKit::get_uncommon_trap_from_success_proj(Node* node) {
4936   if (node->is_IfProj()) {
4937     Node* other_proj = node->as_IfProj()->other_if_proj();
4938     for (DUIterator_Fast jmax, j = other_proj->fast_outs(jmax); j < jmax; j++) {
4939       Node* obs = other_proj->fast_out(j);
4940       if (obs->in(0) == other_proj && obs->is_CallStaticJava() &&
4941           (obs->as_CallStaticJava()->entry_point() == SharedRuntime::uncommon_trap_blob()->entry_point())) {
4942         return obs->as_CallStaticJava();
4943       }
4944     }
4945   }
4946   return nullptr;
4947 }
4948 
4949 //-------------inline_encodeISOArray-----------------------------------
4950 // encode char[] to byte[] in ISO_8859_1 or ASCII
4951 bool LibraryCallKit::inline_encodeISOArray(bool ascii) {
4952   assert(callee()->signature()->size() == 5, "encodeISOArray has 5 parameters");
4953   // no receiver since it is static method
4954   Node *src         = argument(0);
4955   Node *src_offset  = argument(1);
4956   Node *dst         = argument(2);
4957   Node *dst_offset  = argument(3);
4958   Node *length      = argument(4);
4959 
4960   src = must_be_not_null(src, true);
4961   dst = must_be_not_null(dst, true);
4962 
4963   const Type* src_type = src->Value(&_gvn);
4964   const Type* dst_type = dst->Value(&_gvn);
4965   const TypeAryPtr* top_src = src_type->isa_aryptr();
4966   const TypeAryPtr* top_dest = dst_type->isa_aryptr();
4967   if (top_src  == NULL || top_src->klass()  == NULL ||
4968       top_dest == NULL || top_dest->klass() == NULL) {
4969     // failed array check
4970     return false;
4971   }
4972 
4973   // Figure out the size and type of the elements we will be copying.
4974   BasicType src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
4975   BasicType dst_elem = top_dest->klass()->as_array_klass()->element_type()->basic_type();
4976   if (!((src_elem == T_CHAR) || (src_elem== T_BYTE)) || dst_elem != T_BYTE) {
4977     return false;
4978   }
4979 
4980   Node* src_start = array_element_address(src, src_offset, T_CHAR);
4981   Node* dst_start = array_element_address(dst, dst_offset, dst_elem);
4982   // 'src_start' points to src array + scaled offset
4983   // 'dst_start' points to dst array + scaled offset
4984 
4985   const TypeAryPtr* mtype = TypeAryPtr::BYTES;
4986   Node* enc = new EncodeISOArrayNode(control(), memory(mtype), src_start, dst_start, length, ascii);
4987   enc = _gvn.transform(enc);
4988   Node* res_mem = _gvn.transform(new SCMemProjNode(enc));
4989   set_memory(res_mem, mtype);
4990   set_result(enc);
4991   clear_upper_avx();
4992 
4993   return true;
4994 }
4995 
4996 //-------------inline_multiplyToLen-----------------------------------
4997 bool LibraryCallKit::inline_multiplyToLen() {
4998   assert(UseMultiplyToLenIntrinsic, "not implemented on this platform");
4999 
5000   address stubAddr = StubRoutines::multiplyToLen();
5001   if (stubAddr == nullptr) {
5002     return false; // Intrinsic's stub is not implemented on this platform
5003   }
5004   const char* stubName = "multiplyToLen";
5005 
5006   assert(callee()->signature()->size() == 5, "multiplyToLen has 5 parameters");
5007 
5008   // no receiver because it is a static method
5009   Node* x    = argument(0);
5010   Node* xlen = argument(1);
5011   Node* y    = argument(2);
5012   Node* ylen = argument(3);
5013   Node* z    = argument(4);
5014 
5015   x = must_be_not_null(x, true);
5016   y = must_be_not_null(y, true);
5017 
5018   const Type* x_type = x->Value(&_gvn);
5019   const Type* y_type = y->Value(&_gvn);
5020   const TypeAryPtr* top_x = x_type->isa_aryptr();
5021   const TypeAryPtr* top_y = y_type->isa_aryptr();
5022   if (top_x  == NULL || top_x->klass()  == NULL ||
5023       top_y == NULL || top_y->klass() == NULL) {
5024     // failed array check
5025     return false;
5026   }
5027 
5028   BasicType x_elem = top_x->klass()->as_array_klass()->element_type()->basic_type();
5029   BasicType y_elem = top_y->klass()->as_array_klass()->element_type()->basic_type();
5030   if (x_elem != T_INT || y_elem != T_INT) {
5031     return false;
5032   }
5033 
5034   // Set the original stack and the reexecute bit for the interpreter to reexecute
5035   // the bytecode that invokes BigInteger.multiplyToLen() if deoptimization happens
5036   // on the return from z array allocation in runtime.
5037   { PreserveReexecuteState preexecs(this);
5038     jvms()->set_should_reexecute(true);
5039 
5040     Node* x_start = array_element_address(x, intcon(0), x_elem);
5041     Node* y_start = array_element_address(y, intcon(0), y_elem);
5042     // 'x_start' points to x array + scaled xlen
5043     // 'y_start' points to y array + scaled ylen
5044 
5045     // Allocate the result array
5046     Node* zlen = _gvn.transform(new AddINode(xlen, ylen));
5047     ciKlass* klass = ciTypeArrayKlass::make(T_INT);
5048     Node* klass_node = makecon(TypeKlassPtr::make(klass));
5049 
5050     IdealKit ideal(this);
5051 
5052 #define __ ideal.
5053      Node* one = __ ConI(1);
5054      Node* zero = __ ConI(0);
5055      IdealVariable need_alloc(ideal), z_alloc(ideal);  __ declarations_done();
5056      __ set(need_alloc, zero);
5057      __ set(z_alloc, z);
5058      __ if_then(z, BoolTest::eq, null()); {
5059        __ increment (need_alloc, one);
5060      } __ else_(); {
5061        // Update graphKit memory and control from IdealKit.
5062        sync_kit(ideal);
5063        Node *cast = new CastPPNode(z, TypePtr::NOTNULL);
5064        cast->init_req(0, control());
5065        _gvn.set_type(cast, cast->bottom_type());
5066        C->record_for_igvn(cast);
5067 
5068        Node* zlen_arg = load_array_length(cast);
5069        // Update IdealKit memory and control from graphKit.
5070        __ sync_kit(this);
5071        __ if_then(zlen_arg, BoolTest::lt, zlen); {
5072          __ increment (need_alloc, one);
5073        } __ end_if();
5074      } __ end_if();
5075 
5076      __ if_then(__ value(need_alloc), BoolTest::ne, zero); {
5077        // Update graphKit memory and control from IdealKit.
5078        sync_kit(ideal);
5079        Node * narr = new_array(klass_node, zlen, 1);
5080        // Update IdealKit memory and control from graphKit.
5081        __ sync_kit(this);
5082        __ set(z_alloc, narr);
5083      } __ end_if();
5084 
5085      sync_kit(ideal);
5086      z = __ value(z_alloc);
5087      // Can't use TypeAryPtr::INTS which uses Bottom offset.
5088      _gvn.set_type(z, TypeOopPtr::make_from_klass(klass));
5089      // Final sync IdealKit and GraphKit.
5090      final_sync(ideal);
5091 #undef __
5092 
5093     Node* z_start = array_element_address(z, intcon(0), T_INT);
5094 
5095     Node* call = make_runtime_call(RC_LEAF|RC_NO_FP,
5096                                    OptoRuntime::multiplyToLen_Type(),
5097                                    stubAddr, stubName, TypePtr::BOTTOM,
5098                                    x_start, xlen, y_start, ylen, z_start, zlen);
5099   } // original reexecute is set back here
5100 
5101   C->set_has_split_ifs(true); // Has chance for split-if optimization
5102   set_result(z);
5103   return true;
5104 }
5105 
5106 //-------------inline_squareToLen------------------------------------
5107 bool LibraryCallKit::inline_squareToLen() {
5108   assert(UseSquareToLenIntrinsic, "not implemented on this platform");
5109 
5110   address stubAddr = StubRoutines::squareToLen();
5111   if (stubAddr == nullptr) {
5112     return false; // Intrinsic's stub is not implemented on this platform
5113   }
5114   const char* stubName = "squareToLen";
5115 
5116   assert(callee()->signature()->size() == 4, "implSquareToLen has 4 parameters");
5117 
5118   Node* x    = argument(0);
5119   Node* len  = argument(1);
5120   Node* z    = argument(2);
5121   Node* zlen = argument(3);
5122 
5123   x = must_be_not_null(x, true);
5124   z = must_be_not_null(z, true);
5125 
5126   const Type* x_type = x->Value(&_gvn);
5127   const Type* z_type = z->Value(&_gvn);
5128   const TypeAryPtr* top_x = x_type->isa_aryptr();
5129   const TypeAryPtr* top_z = z_type->isa_aryptr();
5130   if (top_x  == NULL || top_x->klass()  == NULL ||
5131       top_z  == NULL || top_z->klass()  == NULL) {
5132     // failed array check
5133     return false;
5134   }
5135 
5136   BasicType x_elem = top_x->klass()->as_array_klass()->element_type()->basic_type();
5137   BasicType z_elem = top_z->klass()->as_array_klass()->element_type()->basic_type();
5138   if (x_elem != T_INT || z_elem != T_INT) {
5139     return false;
5140   }
5141 
5142 
5143   Node* x_start = array_element_address(x, intcon(0), x_elem);
5144   Node* z_start = array_element_address(z, intcon(0), z_elem);
5145 
5146   Node*  call = make_runtime_call(RC_LEAF|RC_NO_FP,
5147                                   OptoRuntime::squareToLen_Type(),
5148                                   stubAddr, stubName, TypePtr::BOTTOM,
5149                                   x_start, len, z_start, zlen);
5150 
5151   set_result(z);
5152   return true;
5153 }
5154 
5155 //-------------inline_mulAdd------------------------------------------
5156 bool LibraryCallKit::inline_mulAdd() {
5157   assert(UseMulAddIntrinsic, "not implemented on this platform");
5158 
5159   address stubAddr = StubRoutines::mulAdd();
5160   if (stubAddr == nullptr) {
5161     return false; // Intrinsic's stub is not implemented on this platform
5162   }
5163   const char* stubName = "mulAdd";
5164 
5165   assert(callee()->signature()->size() == 5, "mulAdd has 5 parameters");
5166 
5167   Node* out      = argument(0);
5168   Node* in       = argument(1);
5169   Node* offset   = argument(2);
5170   Node* len      = argument(3);
5171   Node* k        = argument(4);
5172 
5173   out = must_be_not_null(out, true);
5174 
5175   const Type* out_type = out->Value(&_gvn);
5176   const Type* in_type = in->Value(&_gvn);
5177   const TypeAryPtr* top_out = out_type->isa_aryptr();
5178   const TypeAryPtr* top_in = in_type->isa_aryptr();
5179   if (top_out  == NULL || top_out->klass()  == NULL ||
5180       top_in == NULL || top_in->klass() == NULL) {
5181     // failed array check
5182     return false;
5183   }
5184 
5185   BasicType out_elem = top_out->klass()->as_array_klass()->element_type()->basic_type();
5186   BasicType in_elem = top_in->klass()->as_array_klass()->element_type()->basic_type();
5187   if (out_elem != T_INT || in_elem != T_INT) {
5188     return false;
5189   }
5190 
5191   Node* outlen = load_array_length(out);
5192   Node* new_offset = _gvn.transform(new SubINode(outlen, offset));
5193   Node* out_start = array_element_address(out, intcon(0), out_elem);
5194   Node* in_start = array_element_address(in, intcon(0), in_elem);
5195 
5196   Node*  call = make_runtime_call(RC_LEAF|RC_NO_FP,
5197                                   OptoRuntime::mulAdd_Type(),
5198                                   stubAddr, stubName, TypePtr::BOTTOM,
5199                                   out_start,in_start, new_offset, len, k);
5200   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5201   set_result(result);
5202   return true;
5203 }
5204 
5205 //-------------inline_montgomeryMultiply-----------------------------------
5206 bool LibraryCallKit::inline_montgomeryMultiply() {
5207   address stubAddr = StubRoutines::montgomeryMultiply();
5208   if (stubAddr == nullptr) {
5209     return false; // Intrinsic's stub is not implemented on this platform
5210   }
5211 
5212   assert(UseMontgomeryMultiplyIntrinsic, "not implemented on this platform");
5213   const char* stubName = "montgomery_multiply";
5214 
5215   assert(callee()->signature()->size() == 7, "montgomeryMultiply has 7 parameters");
5216 
5217   Node* a    = argument(0);
5218   Node* b    = argument(1);
5219   Node* n    = argument(2);
5220   Node* len  = argument(3);
5221   Node* inv  = argument(4);
5222   Node* m    = argument(6);
5223 
5224   const Type* a_type = a->Value(&_gvn);
5225   const TypeAryPtr* top_a = a_type->isa_aryptr();
5226   const Type* b_type = b->Value(&_gvn);
5227   const TypeAryPtr* top_b = b_type->isa_aryptr();
5228   const Type* n_type = a->Value(&_gvn);
5229   const TypeAryPtr* top_n = n_type->isa_aryptr();
5230   const Type* m_type = a->Value(&_gvn);
5231   const TypeAryPtr* top_m = m_type->isa_aryptr();
5232   if (top_a  == NULL || top_a->klass()  == NULL ||
5233       top_b == NULL || top_b->klass()  == NULL ||
5234       top_n == NULL || top_n->klass()  == NULL ||
5235       top_m == NULL || top_m->klass()  == NULL) {
5236     // failed array check
5237     return false;
5238   }
5239 
5240   BasicType a_elem = top_a->klass()->as_array_klass()->element_type()->basic_type();
5241   BasicType b_elem = top_b->klass()->as_array_klass()->element_type()->basic_type();
5242   BasicType n_elem = top_n->klass()->as_array_klass()->element_type()->basic_type();
5243   BasicType m_elem = top_m->klass()->as_array_klass()->element_type()->basic_type();
5244   if (a_elem != T_INT || b_elem != T_INT || n_elem != T_INT || m_elem != T_INT) {
5245     return false;
5246   }
5247 
5248   // Make the call
5249   {
5250     Node* a_start = array_element_address(a, intcon(0), a_elem);
5251     Node* b_start = array_element_address(b, intcon(0), b_elem);
5252     Node* n_start = array_element_address(n, intcon(0), n_elem);
5253     Node* m_start = array_element_address(m, intcon(0), m_elem);
5254 
5255     Node* call = make_runtime_call(RC_LEAF,
5256                                    OptoRuntime::montgomeryMultiply_Type(),
5257                                    stubAddr, stubName, TypePtr::BOTTOM,
5258                                    a_start, b_start, n_start, len, inv, top(),
5259                                    m_start);
5260     set_result(m);
5261   }
5262 
5263   return true;
5264 }
5265 
5266 bool LibraryCallKit::inline_montgomerySquare() {
5267   address stubAddr = StubRoutines::montgomerySquare();
5268   if (stubAddr == nullptr) {
5269     return false; // Intrinsic's stub is not implemented on this platform
5270   }
5271 
5272   assert(UseMontgomerySquareIntrinsic, "not implemented on this platform");
5273   const char* stubName = "montgomery_square";
5274 
5275   assert(callee()->signature()->size() == 6, "montgomerySquare has 6 parameters");
5276 
5277   Node* a    = argument(0);
5278   Node* n    = argument(1);
5279   Node* len  = argument(2);
5280   Node* inv  = argument(3);
5281   Node* m    = argument(5);
5282 
5283   const Type* a_type = a->Value(&_gvn);
5284   const TypeAryPtr* top_a = a_type->isa_aryptr();
5285   const Type* n_type = a->Value(&_gvn);
5286   const TypeAryPtr* top_n = n_type->isa_aryptr();
5287   const Type* m_type = a->Value(&_gvn);
5288   const TypeAryPtr* top_m = m_type->isa_aryptr();
5289   if (top_a  == NULL || top_a->klass()  == NULL ||
5290       top_n == NULL || top_n->klass()  == NULL ||
5291       top_m == NULL || top_m->klass()  == NULL) {
5292     // failed array check
5293     return false;
5294   }
5295 
5296   BasicType a_elem = top_a->klass()->as_array_klass()->element_type()->basic_type();
5297   BasicType n_elem = top_n->klass()->as_array_klass()->element_type()->basic_type();
5298   BasicType m_elem = top_m->klass()->as_array_klass()->element_type()->basic_type();
5299   if (a_elem != T_INT || n_elem != T_INT || m_elem != T_INT) {
5300     return false;
5301   }
5302 
5303   // Make the call
5304   {
5305     Node* a_start = array_element_address(a, intcon(0), a_elem);
5306     Node* n_start = array_element_address(n, intcon(0), n_elem);
5307     Node* m_start = array_element_address(m, intcon(0), m_elem);
5308 
5309     Node* call = make_runtime_call(RC_LEAF,
5310                                    OptoRuntime::montgomerySquare_Type(),
5311                                    stubAddr, stubName, TypePtr::BOTTOM,
5312                                    a_start, n_start, len, inv, top(),
5313                                    m_start);
5314     set_result(m);
5315   }
5316 
5317   return true;
5318 }
5319 
5320 bool LibraryCallKit::inline_bigIntegerShift(bool isRightShift) {
5321   address stubAddr = nullptr;
5322   const char* stubName = nullptr;
5323 
5324   stubAddr = isRightShift? StubRoutines::bigIntegerRightShift(): StubRoutines::bigIntegerLeftShift();
5325   if (stubAddr == nullptr) {
5326     return false; // Intrinsic's stub is not implemented on this platform
5327   }
5328 
5329   stubName = isRightShift? "bigIntegerRightShiftWorker" : "bigIntegerLeftShiftWorker";
5330 
5331   assert(callee()->signature()->size() == 5, "expected 5 arguments");
5332 
5333   Node* newArr = argument(0);
5334   Node* oldArr = argument(1);
5335   Node* newIdx = argument(2);
5336   Node* shiftCount = argument(3);
5337   Node* numIter = argument(4);
5338 
5339   const Type* newArr_type = newArr->Value(&_gvn);
5340   const TypeAryPtr* top_newArr = newArr_type->isa_aryptr();
5341   const Type* oldArr_type = oldArr->Value(&_gvn);
5342   const TypeAryPtr* top_oldArr = oldArr_type->isa_aryptr();
5343   if (top_newArr == NULL || top_newArr->klass() == NULL || top_oldArr == NULL
5344       || top_oldArr->klass() == NULL) {
5345     return false;
5346   }
5347 
5348   BasicType newArr_elem = top_newArr->klass()->as_array_klass()->element_type()->basic_type();
5349   BasicType oldArr_elem = top_oldArr->klass()->as_array_klass()->element_type()->basic_type();
5350   if (newArr_elem != T_INT || oldArr_elem != T_INT) {
5351     return false;
5352   }
5353 
5354   // Make the call
5355   {
5356     Node* newArr_start = array_element_address(newArr, intcon(0), newArr_elem);
5357     Node* oldArr_start = array_element_address(oldArr, intcon(0), oldArr_elem);
5358 
5359     Node* call = make_runtime_call(RC_LEAF,
5360                                    OptoRuntime::bigIntegerShift_Type(),
5361                                    stubAddr,
5362                                    stubName,
5363                                    TypePtr::BOTTOM,
5364                                    newArr_start,
5365                                    oldArr_start,
5366                                    newIdx,
5367                                    shiftCount,
5368                                    numIter);
5369   }
5370 
5371   return true;
5372 }
5373 
5374 //-------------inline_vectorizedMismatch------------------------------
5375 bool LibraryCallKit::inline_vectorizedMismatch() {
5376   assert(UseVectorizedMismatchIntrinsic, "not implemented on this platform");
5377 
5378   assert(callee()->signature()->size() == 8, "vectorizedMismatch has 6 parameters");
5379   Node* obja    = argument(0); // Object
5380   Node* aoffset = argument(1); // long
5381   Node* objb    = argument(3); // Object
5382   Node* boffset = argument(4); // long
5383   Node* length  = argument(6); // int
5384   Node* scale   = argument(7); // int
5385 
5386   const TypeAryPtr* obja_t = _gvn.type(obja)->isa_aryptr();
5387   const TypeAryPtr* objb_t = _gvn.type(objb)->isa_aryptr();
5388   if (obja_t == NULL || obja_t->klass() == NULL ||
5389       objb_t == NULL || objb_t->klass() == NULL ||
5390       scale == top()) {
5391     return false; // failed input validation
5392   }
5393 
5394   Node* obja_adr = make_unsafe_address(obja, aoffset);
5395   Node* objb_adr = make_unsafe_address(objb, boffset);
5396 
5397   // Partial inlining handling for inputs smaller than ArrayOperationPartialInlineSize bytes in size.
5398   //
5399   //    inline_limit = ArrayOperationPartialInlineSize / element_size;
5400   //    if (length <= inline_limit) {
5401   //      inline_path:
5402   //        vmask   = VectorMaskGen length
5403   //        vload1  = LoadVectorMasked obja, vmask
5404   //        vload2  = LoadVectorMasked objb, vmask
5405   //        result1 = VectorCmpMasked vload1, vload2, vmask
5406   //    } else {
5407   //      call_stub_path:
5408   //        result2 = call vectorizedMismatch_stub(obja, objb, length, scale)
5409   //    }
5410   //    exit_block:
5411   //      return Phi(result1, result2);
5412   //
5413   enum { inline_path = 1,  // input is small enough to process it all at once
5414          stub_path   = 2,  // input is too large; call into the VM
5415          PATH_LIMIT  = 3
5416   };
5417 
5418   Node* exit_block = new RegionNode(PATH_LIMIT);
5419   Node* result_phi = new PhiNode(exit_block, TypeInt::INT);
5420   Node* memory_phi = new PhiNode(exit_block, Type::MEMORY, TypePtr::BOTTOM);
5421 
5422   Node* call_stub_path = control();
5423 
5424   BasicType elem_bt = T_ILLEGAL;
5425 
5426   const TypeInt* scale_t = _gvn.type(scale)->is_int();
5427   if (scale_t->is_con()) {
5428     switch (scale_t->get_con()) {
5429       case 0: elem_bt = T_BYTE;  break;
5430       case 1: elem_bt = T_SHORT; break;
5431       case 2: elem_bt = T_INT;   break;
5432       case 3: elem_bt = T_LONG;  break;
5433 
5434       default: elem_bt = T_ILLEGAL; break; // not supported
5435     }
5436   }
5437 
5438   int inline_limit = 0;
5439   bool do_partial_inline = false;
5440 
5441   if (elem_bt != T_ILLEGAL && ArrayOperationPartialInlineSize > 0) {
5442     inline_limit = ArrayOperationPartialInlineSize / type2aelembytes(elem_bt);
5443     do_partial_inline = inline_limit >= 16;
5444   }
5445 
5446   if (do_partial_inline) {
5447     assert(elem_bt != T_ILLEGAL, "sanity");
5448 
5449     if (Matcher::match_rule_supported_vector(Op_VectorMaskGen,    inline_limit, elem_bt) &&
5450         Matcher::match_rule_supported_vector(Op_LoadVectorMasked, inline_limit, elem_bt) &&
5451         Matcher::match_rule_supported_vector(Op_VectorCmpMasked,  inline_limit, elem_bt)) {
5452 
5453       const TypeVect* vt = TypeVect::make(elem_bt, inline_limit);
5454       Node* cmp_length = _gvn.transform(new CmpINode(length, intcon(inline_limit)));
5455       Node* bol_gt     = _gvn.transform(new BoolNode(cmp_length, BoolTest::gt));
5456 
5457       call_stub_path = generate_guard(bol_gt, nullptr, PROB_MIN);
5458 
5459       if (!stopped()) {
5460         Node* casted_length = _gvn.transform(new CastIINode(control(), length, TypeInt::make(0, inline_limit, Type::WidenMin)));
5461 
5462         const TypePtr* obja_adr_t = _gvn.type(obja_adr)->isa_ptr();
5463         const TypePtr* objb_adr_t = _gvn.type(objb_adr)->isa_ptr();
5464         Node* obja_adr_mem = memory(C->get_alias_index(obja_adr_t));
5465         Node* objb_adr_mem = memory(C->get_alias_index(objb_adr_t));
5466 
5467         Node* vmask      = _gvn.transform(new VectorMaskGenNode(ConvI2X(casted_length), TypeVect::VECTMASK, elem_bt));
5468         Node* vload_obja = _gvn.transform(new LoadVectorMaskedNode(control(), obja_adr_mem, obja_adr, obja_adr_t, vt, vmask));
5469         Node* vload_objb = _gvn.transform(new LoadVectorMaskedNode(control(), objb_adr_mem, objb_adr, objb_adr_t, vt, vmask));
5470         Node* result     = _gvn.transform(new VectorCmpMaskedNode(vload_obja, vload_objb, vmask, TypeInt::INT));
5471 
5472         exit_block->init_req(inline_path, control());
5473         memory_phi->init_req(inline_path, map()->memory());
5474         result_phi->init_req(inline_path, result);
5475 
5476         C->set_max_vector_size(MAX2((uint)ArrayOperationPartialInlineSize, C->max_vector_size()));
5477         clear_upper_avx();
5478       }
5479     }
5480   }
5481 
5482   if (call_stub_path != nullptr) {
5483     set_control(call_stub_path);
5484 
5485     Node* call = make_runtime_call(RC_LEAF,
5486                                    OptoRuntime::vectorizedMismatch_Type(),
5487                                    StubRoutines::vectorizedMismatch(), "vectorizedMismatch", TypePtr::BOTTOM,
5488                                    obja_adr, objb_adr, length, scale);
5489 
5490     exit_block->init_req(stub_path, control());
5491     memory_phi->init_req(stub_path, map()->memory());
5492     result_phi->init_req(stub_path, _gvn.transform(new ProjNode(call, TypeFunc::Parms)));
5493   }
5494 
5495   exit_block = _gvn.transform(exit_block);
5496   memory_phi = _gvn.transform(memory_phi);
5497   result_phi = _gvn.transform(result_phi);
5498 
5499   set_control(exit_block);
5500   set_all_memory(memory_phi);
5501   set_result(result_phi);
5502 
5503   return true;
5504 }
5505 
5506 /**
5507  * Calculate CRC32 for byte.
5508  * int java.util.zip.CRC32.update(int crc, int b)
5509  */
5510 bool LibraryCallKit::inline_updateCRC32() {
5511   assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5512   assert(callee()->signature()->size() == 2, "update has 2 parameters");
5513   // no receiver since it is static method
5514   Node* crc  = argument(0); // type: int
5515   Node* b    = argument(1); // type: int
5516 
5517   /*
5518    *    int c = ~ crc;
5519    *    b = timesXtoThe32[(b ^ c) & 0xFF];
5520    *    b = b ^ (c >>> 8);
5521    *    crc = ~b;
5522    */
5523 
5524   Node* M1 = intcon(-1);
5525   crc = _gvn.transform(new XorINode(crc, M1));
5526   Node* result = _gvn.transform(new XorINode(crc, b));
5527   result = _gvn.transform(new AndINode(result, intcon(0xFF)));
5528 
5529   Node* base = makecon(TypeRawPtr::make(StubRoutines::crc_table_addr()));
5530   Node* offset = _gvn.transform(new LShiftINode(result, intcon(0x2)));
5531   Node* adr = basic_plus_adr(top(), base, ConvI2X(offset));
5532   result = make_load(control(), adr, TypeInt::INT, T_INT, MemNode::unordered);
5533 
5534   crc = _gvn.transform(new URShiftINode(crc, intcon(8)));
5535   result = _gvn.transform(new XorINode(crc, result));
5536   result = _gvn.transform(new XorINode(result, M1));
5537   set_result(result);
5538   return true;
5539 }
5540 
5541 /**
5542  * Calculate CRC32 for byte[] array.
5543  * int java.util.zip.CRC32.updateBytes(int crc, byte[] buf, int off, int len)
5544  */
5545 bool LibraryCallKit::inline_updateBytesCRC32() {
5546   assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5547   assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5548   // no receiver since it is static method
5549   Node* crc     = argument(0); // type: int
5550   Node* src     = argument(1); // type: oop
5551   Node* offset  = argument(2); // type: int
5552   Node* length  = argument(3); // type: int
5553 
5554   const Type* src_type = src->Value(&_gvn);
5555   const TypeAryPtr* top_src = src_type->isa_aryptr();
5556   if (top_src  == NULL || top_src->klass()  == NULL) {
5557     // failed array check
5558     return false;
5559   }
5560 
5561   // Figure out the size and type of the elements we will be copying.
5562   BasicType src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
5563   if (src_elem != T_BYTE) {
5564     return false;
5565   }
5566 
5567   // 'src_start' points to src array + scaled offset
5568   src = must_be_not_null(src, true);
5569   Node* src_start = array_element_address(src, offset, src_elem);
5570 
5571   // We assume that range check is done by caller.
5572   // TODO: generate range check (offset+length < src.length) in debug VM.
5573 
5574   // Call the stub.
5575   address stubAddr = StubRoutines::updateBytesCRC32();
5576   const char *stubName = "updateBytesCRC32";
5577 
5578   Node* call = make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::updateBytesCRC32_Type(),
5579                                  stubAddr, stubName, TypePtr::BOTTOM,
5580                                  crc, src_start, length);
5581   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5582   set_result(result);
5583   return true;
5584 }
5585 
5586 /**
5587  * Calculate CRC32 for ByteBuffer.
5588  * int java.util.zip.CRC32.updateByteBuffer(int crc, long buf, int off, int len)
5589  */
5590 bool LibraryCallKit::inline_updateByteBufferCRC32() {
5591   assert(UseCRC32Intrinsics, "need AVX and LCMUL instructions support");
5592   assert(callee()->signature()->size() == 5, "updateByteBuffer has 4 parameters and one is long");
5593   // no receiver since it is static method
5594   Node* crc     = argument(0); // type: int
5595   Node* src     = argument(1); // type: long
5596   Node* offset  = argument(3); // type: int
5597   Node* length  = argument(4); // type: int
5598 
5599   src = ConvL2X(src);  // adjust Java long to machine word
5600   Node* base = _gvn.transform(new CastX2PNode(src));
5601   offset = ConvI2X(offset);
5602 
5603   // 'src_start' points to src array + scaled offset
5604   Node* src_start = basic_plus_adr(top(), base, offset);
5605 
5606   // Call the stub.
5607   address stubAddr = StubRoutines::updateBytesCRC32();
5608   const char *stubName = "updateBytesCRC32";
5609 
5610   Node* call = make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::updateBytesCRC32_Type(),
5611                                  stubAddr, stubName, TypePtr::BOTTOM,
5612                                  crc, src_start, length);
5613   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5614   set_result(result);
5615   return true;
5616 }
5617 
5618 //------------------------------get_table_from_crc32c_class-----------------------
5619 Node * LibraryCallKit::get_table_from_crc32c_class(ciInstanceKlass *crc32c_class) {
5620   Node* table = load_field_from_object(nullptr, "byteTable", "[I", /*decorators*/ IN_HEAP, /*is_static*/ true, crc32c_class);
5621   assert (table != nullptr, "wrong version of java.util.zip.CRC32C");
5622 
5623   return table;
5624 }
5625 
5626 //------------------------------inline_updateBytesCRC32C-----------------------
5627 //
5628 // Calculate CRC32C for byte[] array.
5629 // int java.util.zip.CRC32C.updateBytes(int crc, byte[] buf, int off, int end)
5630 //
5631 bool LibraryCallKit::inline_updateBytesCRC32C() {
5632   assert(UseCRC32CIntrinsics, "need CRC32C instruction support");
5633   assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5634   assert(callee()->holder()->is_loaded(), "CRC32C class must be loaded");
5635   // no receiver since it is a static method
5636   Node* crc     = argument(0); // type: int
5637   Node* src     = argument(1); // type: oop
5638   Node* offset  = argument(2); // type: int
5639   Node* end     = argument(3); // type: int
5640 
5641   Node* length = _gvn.transform(new SubINode(end, offset));
5642 
5643   const Type* src_type = src->Value(&_gvn);
5644   const TypeAryPtr* top_src = src_type->isa_aryptr();
5645   if (top_src  == NULL || top_src->klass()  == NULL) {
5646     // failed array check
5647     return false;
5648   }
5649 
5650   // Figure out the size and type of the elements we will be copying.
5651   BasicType src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
5652   if (src_elem != T_BYTE) {
5653     return false;
5654   }
5655 
5656   // 'src_start' points to src array + scaled offset
5657   src = must_be_not_null(src, true);
5658   Node* src_start = array_element_address(src, offset, src_elem);
5659 
5660   // static final int[] byteTable in class CRC32C
5661   Node* table = get_table_from_crc32c_class(callee()->holder());
5662   table = must_be_not_null(table, true);
5663   Node* table_start = array_element_address(table, intcon(0), T_INT);
5664 
5665   // We assume that range check is done by caller.
5666   // TODO: generate range check (offset+length < src.length) in debug VM.
5667 
5668   // Call the stub.
5669   address stubAddr = StubRoutines::updateBytesCRC32C();
5670   const char *stubName = "updateBytesCRC32C";
5671 
5672   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5673                                  stubAddr, stubName, TypePtr::BOTTOM,
5674                                  crc, src_start, length, table_start);
5675   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5676   set_result(result);
5677   return true;
5678 }
5679 
5680 //------------------------------inline_updateDirectByteBufferCRC32C-----------------------
5681 //
5682 // Calculate CRC32C for DirectByteBuffer.
5683 // int java.util.zip.CRC32C.updateDirectByteBuffer(int crc, long buf, int off, int end)
5684 //
5685 bool LibraryCallKit::inline_updateDirectByteBufferCRC32C() {
5686   assert(UseCRC32CIntrinsics, "need CRC32C instruction support");
5687   assert(callee()->signature()->size() == 5, "updateDirectByteBuffer has 4 parameters and one is long");
5688   assert(callee()->holder()->is_loaded(), "CRC32C class must be loaded");
5689   // no receiver since it is a static method
5690   Node* crc     = argument(0); // type: int
5691   Node* src     = argument(1); // type: long
5692   Node* offset  = argument(3); // type: int
5693   Node* end     = argument(4); // type: int
5694 
5695   Node* length = _gvn.transform(new SubINode(end, offset));
5696 
5697   src = ConvL2X(src);  // adjust Java long to machine word
5698   Node* base = _gvn.transform(new CastX2PNode(src));
5699   offset = ConvI2X(offset);
5700 
5701   // 'src_start' points to src array + scaled offset
5702   Node* src_start = basic_plus_adr(top(), base, offset);
5703 
5704   // static final int[] byteTable in class CRC32C
5705   Node* table = get_table_from_crc32c_class(callee()->holder());
5706   table = must_be_not_null(table, true);
5707   Node* table_start = array_element_address(table, intcon(0), T_INT);
5708 
5709   // Call the stub.
5710   address stubAddr = StubRoutines::updateBytesCRC32C();
5711   const char *stubName = "updateBytesCRC32C";
5712 
5713   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesCRC32C_Type(),
5714                                  stubAddr, stubName, TypePtr::BOTTOM,
5715                                  crc, src_start, length, table_start);
5716   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5717   set_result(result);
5718   return true;
5719 }
5720 
5721 //------------------------------inline_updateBytesAdler32----------------------
5722 //
5723 // Calculate Adler32 checksum for byte[] array.
5724 // int java.util.zip.Adler32.updateBytes(int crc, byte[] buf, int off, int len)
5725 //
5726 bool LibraryCallKit::inline_updateBytesAdler32() {
5727   assert(UseAdler32Intrinsics, "Adler32 Instrinsic support need"); // check if we actually need to check this flag or check a different one
5728   assert(callee()->signature()->size() == 4, "updateBytes has 4 parameters");
5729   assert(callee()->holder()->is_loaded(), "Adler32 class must be loaded");
5730   // no receiver since it is static method
5731   Node* crc     = argument(0); // type: int
5732   Node* src     = argument(1); // type: oop
5733   Node* offset  = argument(2); // type: int
5734   Node* length  = argument(3); // type: int
5735 
5736   const Type* src_type = src->Value(&_gvn);
5737   const TypeAryPtr* top_src = src_type->isa_aryptr();
5738   if (top_src  == NULL || top_src->klass()  == NULL) {
5739     // failed array check
5740     return false;
5741   }
5742 
5743   // Figure out the size and type of the elements we will be copying.
5744   BasicType src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
5745   if (src_elem != T_BYTE) {
5746     return false;
5747   }
5748 
5749   // 'src_start' points to src array + scaled offset
5750   Node* src_start = array_element_address(src, offset, src_elem);
5751 
5752   // We assume that range check is done by caller.
5753   // TODO: generate range check (offset+length < src.length) in debug VM.
5754 
5755   // Call the stub.
5756   address stubAddr = StubRoutines::updateBytesAdler32();
5757   const char *stubName = "updateBytesAdler32";
5758 
5759   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5760                                  stubAddr, stubName, TypePtr::BOTTOM,
5761                                  crc, src_start, length);
5762   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5763   set_result(result);
5764   return true;
5765 }
5766 
5767 //------------------------------inline_updateByteBufferAdler32---------------
5768 //
5769 // Calculate Adler32 checksum for DirectByteBuffer.
5770 // int java.util.zip.Adler32.updateByteBuffer(int crc, long buf, int off, int len)
5771 //
5772 bool LibraryCallKit::inline_updateByteBufferAdler32() {
5773   assert(UseAdler32Intrinsics, "Adler32 Instrinsic support need"); // check if we actually need to check this flag or check a different one
5774   assert(callee()->signature()->size() == 5, "updateByteBuffer has 4 parameters and one is long");
5775   assert(callee()->holder()->is_loaded(), "Adler32 class must be loaded");
5776   // no receiver since it is static method
5777   Node* crc     = argument(0); // type: int
5778   Node* src     = argument(1); // type: long
5779   Node* offset  = argument(3); // type: int
5780   Node* length  = argument(4); // type: int
5781 
5782   src = ConvL2X(src);  // adjust Java long to machine word
5783   Node* base = _gvn.transform(new CastX2PNode(src));
5784   offset = ConvI2X(offset);
5785 
5786   // 'src_start' points to src array + scaled offset
5787   Node* src_start = basic_plus_adr(top(), base, offset);
5788 
5789   // Call the stub.
5790   address stubAddr = StubRoutines::updateBytesAdler32();
5791   const char *stubName = "updateBytesAdler32";
5792 
5793   Node* call = make_runtime_call(RC_LEAF, OptoRuntime::updateBytesAdler32_Type(),
5794                                  stubAddr, stubName, TypePtr::BOTTOM,
5795                                  crc, src_start, length);
5796 
5797   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
5798   set_result(result);
5799   return true;
5800 }
5801 
5802 //----------------------------inline_reference_get----------------------------
5803 // public T java.lang.ref.Reference.get();
5804 bool LibraryCallKit::inline_reference_get() {
5805   const int referent_offset = java_lang_ref_Reference::referent_offset();
5806 
5807   // Get the argument:
5808   Node* reference_obj = null_check_receiver();
5809   if (stopped()) return true;
5810 
5811   DecoratorSet decorators = IN_HEAP | ON_WEAK_OOP_REF;
5812   Node* result = load_field_from_object(reference_obj, "referent", "Ljava/lang/Object;",
5813                                         decorators, /*is_static*/ false, nullptr);
5814   if (result == nullptr) return false;
5815 
5816   // Add memory barrier to prevent commoning reads from this field
5817   // across safepoint since GC can change its value.
5818   insert_mem_bar(Op_MemBarCPUOrder);
5819 
5820   set_result(result);
5821   return true;
5822 }
5823 
5824 //----------------------------inline_reference_refersTo0----------------------------
5825 // bool java.lang.ref.Reference.refersTo0();
5826 // bool java.lang.ref.PhantomReference.refersTo0();
5827 bool LibraryCallKit::inline_reference_refersTo0(bool is_phantom) {
5828   // Get arguments:
5829   Node* reference_obj = null_check_receiver();
5830   Node* other_obj = argument(1);
5831   if (stopped()) return true;
5832 
5833   DecoratorSet decorators = IN_HEAP | AS_NO_KEEPALIVE;
5834   decorators |= (is_phantom ? ON_PHANTOM_OOP_REF : ON_WEAK_OOP_REF);
5835   Node* referent = load_field_from_object(reference_obj, "referent", "Ljava/lang/Object;",
5836                                           decorators, /*is_static*/ false, nullptr);
5837   if (referent == nullptr) return false;
5838 
5839   // Add memory barrier to prevent commoning reads from this field
5840   // across safepoint since GC can change its value.
5841   insert_mem_bar(Op_MemBarCPUOrder);
5842 
5843   Node* cmp = _gvn.transform(new CmpPNode(referent, other_obj));
5844   Node* bol = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
5845   IfNode* if_node = create_and_map_if(control(), bol, PROB_FAIR, COUNT_UNKNOWN);
5846 
5847   RegionNode* region = new RegionNode(3);
5848   PhiNode* phi = new PhiNode(region, TypeInt::BOOL);
5849 
5850   Node* if_true = _gvn.transform(new IfTrueNode(if_node));
5851   region->init_req(1, if_true);
5852   phi->init_req(1, intcon(1));
5853 
5854   Node* if_false = _gvn.transform(new IfFalseNode(if_node));
5855   region->init_req(2, if_false);
5856   phi->init_req(2, intcon(0));
5857 
5858   set_control(_gvn.transform(region));
5859   record_for_igvn(region);
5860   set_result(_gvn.transform(phi));
5861   return true;
5862 }
5863 
5864 
5865 Node* LibraryCallKit::load_field_from_object(Node* fromObj, const char* fieldName, const char* fieldTypeString,
5866                                              DecoratorSet decorators = IN_HEAP, bool is_static = false,
5867                                              ciInstanceKlass* fromKls = NULL) {
5868   if (fromKls == nullptr) {
5869     const TypeInstPtr* tinst = _gvn.type(fromObj)->isa_instptr();
5870     assert(tinst != nullptr, "obj is null");
5871     assert(tinst->klass()->is_loaded(), "obj is not loaded");
5872     fromKls = tinst->klass()->as_instance_klass();
5873   } else {
5874     assert(is_static, "only for static field access");
5875   }
5876   ciField* field = fromKls->get_field_by_name(ciSymbol::make(fieldName),
5877                                               ciSymbol::make(fieldTypeString),
5878                                               is_static);
5879 
5880   assert (field != NULL, "undefined field");
5881   if (field == nullptr) return (Node *) nullptr;
5882 
5883   if (is_static) {
5884     const TypeInstPtr* tip = TypeInstPtr::make(fromKls->java_mirror());
5885     fromObj = makecon(tip);
5886   }
5887 
5888   // Next code  copied from Parse::do_get_xxx():
5889 
5890   // Compute address and memory type.
5891   int offset  = field->offset_in_bytes();
5892   bool is_vol = field->is_volatile();
5893   ciType* field_klass = field->type();
5894   assert(field_klass->is_loaded(), "should be loaded");
5895   const TypePtr* adr_type = C->alias_type(field)->adr_type();
5896   Node *adr = basic_plus_adr(fromObj, fromObj, offset);
5897   BasicType bt = field->layout_type();
5898 
5899   // Build the resultant type of the load
5900   const Type *type;
5901   if (bt == T_OBJECT) {
5902     type = TypeOopPtr::make_from_klass(field_klass->as_klass());
5903   } else {
5904     type = Type::get_const_basic_type(bt);
5905   }
5906 
5907   if (is_vol) {
5908     decorators |= MO_SEQ_CST;
5909   }
5910 
5911   return access_load_at(fromObj, adr, adr_type, type, bt, decorators);
5912 }
5913 
5914 Node * LibraryCallKit::field_address_from_object(Node * fromObj, const char * fieldName, const char * fieldTypeString,
5915                                                  bool is_exact = true, bool is_static = false,
5916                                                  ciInstanceKlass * fromKls = NULL) {
5917   if (fromKls == nullptr) {
5918     const TypeInstPtr* tinst = _gvn.type(fromObj)->isa_instptr();
5919     assert(tinst != nullptr, "obj is null");
5920     assert(tinst->klass()->is_loaded(), "obj is not loaded");
5921     assert(!is_exact || tinst->klass_is_exact(), "klass not exact");
5922     fromKls = tinst->klass()->as_instance_klass();
5923   }
5924   else {
5925     assert(is_static, "only for static field access");
5926   }
5927   ciField* field = fromKls->get_field_by_name(ciSymbol::make(fieldName),
5928     ciSymbol::make(fieldTypeString),
5929     is_static);
5930 
5931   assert(field != nullptr, "undefined field");
5932   assert(!field->is_volatile(), "not defined for volatile fields");
5933 
5934   if (is_static) {
5935     const TypeInstPtr* tip = TypeInstPtr::make(fromKls->java_mirror());
5936     fromObj = makecon(tip);
5937   }
5938 
5939   // Next code  copied from Parse::do_get_xxx():
5940 
5941   // Compute address and memory type.
5942   int offset = field->offset_in_bytes();
5943   Node *adr = basic_plus_adr(fromObj, fromObj, offset);
5944 
5945   return adr;
5946 }
5947 
5948 //------------------------------inline_aescrypt_Block-----------------------
5949 bool LibraryCallKit::inline_aescrypt_Block(vmIntrinsics::ID id) {
5950   address stubAddr = nullptr;
5951   const char *stubName;
5952   assert(UseAES, "need AES instruction support");
5953 
5954   switch(id) {
5955   case vmIntrinsics::_aescrypt_encryptBlock:
5956     stubAddr = StubRoutines::aescrypt_encryptBlock();
5957     stubName = "aescrypt_encryptBlock";
5958     break;
5959   case vmIntrinsics::_aescrypt_decryptBlock:
5960     stubAddr = StubRoutines::aescrypt_decryptBlock();
5961     stubName = "aescrypt_decryptBlock";
5962     break;
5963   default:
5964     break;
5965   }
5966   if (stubAddr == nullptr) return false;
5967 
5968   Node* aescrypt_object = argument(0);
5969   Node* src             = argument(1);
5970   Node* src_offset      = argument(2);
5971   Node* dest            = argument(3);
5972   Node* dest_offset     = argument(4);
5973 
5974   src = must_be_not_null(src, true);
5975   dest = must_be_not_null(dest, true);
5976 
5977   // (1) src and dest are arrays.
5978   const Type* src_type = src->Value(&_gvn);
5979   const Type* dest_type = dest->Value(&_gvn);
5980   const TypeAryPtr* top_src = src_type->isa_aryptr();
5981   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
5982   assert (top_src  != NULL && top_src->klass()  != NULL &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
5983 
5984   // for the quick and dirty code we will skip all the checks.
5985   // we are just trying to get the call to be generated.
5986   Node* src_start  = src;
5987   Node* dest_start = dest;
5988   if (src_offset != nullptr || dest_offset != nullptr) {
5989     assert(src_offset != nullptr && dest_offset != nullptr, "");
5990     src_start  = array_element_address(src,  src_offset,  T_BYTE);
5991     dest_start = array_element_address(dest, dest_offset, T_BYTE);
5992   }
5993 
5994   // now need to get the start of its expanded key array
5995   // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
5996   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
5997   if (k_start == nullptr) return false;
5998 
5999   // Call the stub.
6000   make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::aescrypt_block_Type(),
6001                     stubAddr, stubName, TypePtr::BOTTOM,
6002                     src_start, dest_start, k_start);
6003 
6004   return true;
6005 }
6006 
6007 //------------------------------inline_cipherBlockChaining_AESCrypt-----------------------
6008 bool LibraryCallKit::inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id) {
6009   address stubAddr = nullptr;
6010   const char *stubName = nullptr;
6011 
6012   assert(UseAES, "need AES instruction support");
6013 
6014   switch(id) {
6015   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
6016     stubAddr = StubRoutines::cipherBlockChaining_encryptAESCrypt();
6017     stubName = "cipherBlockChaining_encryptAESCrypt";
6018     break;
6019   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
6020     stubAddr = StubRoutines::cipherBlockChaining_decryptAESCrypt();
6021     stubName = "cipherBlockChaining_decryptAESCrypt";
6022     break;
6023   default:
6024     break;
6025   }
6026   if (stubAddr == nullptr) return false;
6027 
6028   Node* cipherBlockChaining_object = argument(0);
6029   Node* src                        = argument(1);
6030   Node* src_offset                 = argument(2);
6031   Node* len                        = argument(3);
6032   Node* dest                       = argument(4);
6033   Node* dest_offset                = argument(5);
6034 
6035   src = must_be_not_null(src, false);
6036   dest = must_be_not_null(dest, false);
6037 
6038   // (1) src and dest are arrays.
6039   const Type* src_type = src->Value(&_gvn);
6040   const Type* dest_type = dest->Value(&_gvn);
6041   const TypeAryPtr* top_src = src_type->isa_aryptr();
6042   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
6043   assert (top_src  != NULL && top_src->klass()  != NULL
6044           &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
6045 
6046   // checks are the responsibility of the caller
6047   Node* src_start  = src;
6048   Node* dest_start = dest;
6049   if (src_offset != nullptr || dest_offset != nullptr) {
6050     assert(src_offset != nullptr && dest_offset != nullptr, "");
6051     src_start  = array_element_address(src,  src_offset,  T_BYTE);
6052     dest_start = array_element_address(dest, dest_offset, T_BYTE);
6053   }
6054 
6055   // if we are in this set of code, we "know" the embeddedCipher is an AESCrypt object
6056   // (because of the predicated logic executed earlier).
6057   // so we cast it here safely.
6058   // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
6059 
6060   Node* embeddedCipherObj = load_field_from_object(cipherBlockChaining_object, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;");
6061   if (embeddedCipherObj == nullptr) return false;
6062 
6063   // cast it to what we know it will be at runtime
6064   const TypeInstPtr* tinst = _gvn.type(cipherBlockChaining_object)->isa_instptr();
6065   assert(tinst != nullptr, "CBC obj is null");
6066   assert(tinst->klass()->is_loaded(), "CBC obj is not loaded");
6067   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6068   assert(klass_AESCrypt->is_loaded(), "predicate checks that this class is loaded");
6069 
6070   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6071   const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_AESCrypt);
6072   const TypeOopPtr* xtype = aklass->as_instance_type();
6073   Node* aescrypt_object = new CheckCastPPNode(control(), embeddedCipherObj, xtype);
6074   aescrypt_object = _gvn.transform(aescrypt_object);
6075 
6076   // we need to get the start of the aescrypt_object's expanded key array
6077   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
6078   if (k_start == nullptr) return false;
6079 
6080   // similarly, get the start address of the r vector
6081   Node* objRvec = load_field_from_object(cipherBlockChaining_object, "r", "[B");
6082   if (objRvec == nullptr) return false;
6083   Node* r_start = array_element_address(objRvec, intcon(0), T_BYTE);
6084 
6085   // Call the stub, passing src_start, dest_start, k_start, r_start and src_len
6086   Node* cbcCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
6087                                      OptoRuntime::cipherBlockChaining_aescrypt_Type(),
6088                                      stubAddr, stubName, TypePtr::BOTTOM,
6089                                      src_start, dest_start, k_start, r_start, len);
6090 
6091   // return cipher length (int)
6092   Node* retvalue = _gvn.transform(new ProjNode(cbcCrypt, TypeFunc::Parms));
6093   set_result(retvalue);
6094   return true;
6095 }
6096 
6097 //------------------------------inline_electronicCodeBook_AESCrypt-----------------------
6098 bool LibraryCallKit::inline_electronicCodeBook_AESCrypt(vmIntrinsics::ID id) {
6099   address stubAddr = nullptr;
6100   const char *stubName = nullptr;
6101 
6102   assert(UseAES, "need AES instruction support");
6103 
6104   switch (id) {
6105   case vmIntrinsics::_electronicCodeBook_encryptAESCrypt:
6106     stubAddr = StubRoutines::electronicCodeBook_encryptAESCrypt();
6107     stubName = "electronicCodeBook_encryptAESCrypt";
6108     break;
6109   case vmIntrinsics::_electronicCodeBook_decryptAESCrypt:
6110     stubAddr = StubRoutines::electronicCodeBook_decryptAESCrypt();
6111     stubName = "electronicCodeBook_decryptAESCrypt";
6112     break;
6113   default:
6114     break;
6115   }
6116 
6117   if (stubAddr == nullptr) return false;
6118 
6119   Node* electronicCodeBook_object = argument(0);
6120   Node* src                       = argument(1);
6121   Node* src_offset                = argument(2);
6122   Node* len                       = argument(3);
6123   Node* dest                      = argument(4);
6124   Node* dest_offset               = argument(5);
6125 
6126   // (1) src and dest are arrays.
6127   const Type* src_type = src->Value(&_gvn);
6128   const Type* dest_type = dest->Value(&_gvn);
6129   const TypeAryPtr* top_src = src_type->isa_aryptr();
6130   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
6131   assert(top_src != NULL && top_src->klass() != NULL
6132          &&  top_dest != NULL && top_dest->klass() != NULL, "args are strange");
6133 
6134   // checks are the responsibility of the caller
6135   Node* src_start = src;
6136   Node* dest_start = dest;
6137   if (src_offset != nullptr || dest_offset != nullptr) {
6138     assert(src_offset != nullptr && dest_offset != nullptr, "");
6139     src_start = array_element_address(src, src_offset, T_BYTE);
6140     dest_start = array_element_address(dest, dest_offset, T_BYTE);
6141   }
6142 
6143   // if we are in this set of code, we "know" the embeddedCipher is an AESCrypt object
6144   // (because of the predicated logic executed earlier).
6145   // so we cast it here safely.
6146   // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
6147 
6148   Node* embeddedCipherObj = load_field_from_object(electronicCodeBook_object, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;");
6149   if (embeddedCipherObj == nullptr) return false;
6150 
6151   // cast it to what we know it will be at runtime
6152   const TypeInstPtr* tinst = _gvn.type(electronicCodeBook_object)->isa_instptr();
6153   assert(tinst != nullptr, "ECB obj is null");
6154   assert(tinst->klass()->is_loaded(), "ECB obj is not loaded");
6155   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6156   assert(klass_AESCrypt->is_loaded(), "predicate checks that this class is loaded");
6157 
6158   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6159   const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_AESCrypt);
6160   const TypeOopPtr* xtype = aklass->as_instance_type();
6161   Node* aescrypt_object = new CheckCastPPNode(control(), embeddedCipherObj, xtype);
6162   aescrypt_object = _gvn.transform(aescrypt_object);
6163 
6164   // we need to get the start of the aescrypt_object's expanded key array
6165   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
6166   if (k_start == nullptr) return false;
6167 
6168   // Call the stub, passing src_start, dest_start, k_start, r_start and src_len
6169   Node* ecbCrypt = make_runtime_call(RC_LEAF | RC_NO_FP,
6170                                      OptoRuntime::electronicCodeBook_aescrypt_Type(),
6171                                      stubAddr, stubName, TypePtr::BOTTOM,
6172                                      src_start, dest_start, k_start, len);
6173 
6174   // return cipher length (int)
6175   Node* retvalue = _gvn.transform(new ProjNode(ecbCrypt, TypeFunc::Parms));
6176   set_result(retvalue);
6177   return true;
6178 }
6179 
6180 //------------------------------inline_counterMode_AESCrypt-----------------------
6181 bool LibraryCallKit::inline_counterMode_AESCrypt(vmIntrinsics::ID id) {
6182   assert(UseAES, "need AES instruction support");
6183   if (!UseAESCTRIntrinsics) return false;
6184 
6185   address stubAddr = nullptr;
6186   const char *stubName = nullptr;
6187   if (id == vmIntrinsics::_counterMode_AESCrypt) {
6188     stubAddr = StubRoutines::counterMode_AESCrypt();
6189     stubName = "counterMode_AESCrypt";
6190   }
6191   if (stubAddr == nullptr) return false;
6192 
6193   Node* counterMode_object = argument(0);
6194   Node* src = argument(1);
6195   Node* src_offset = argument(2);
6196   Node* len = argument(3);
6197   Node* dest = argument(4);
6198   Node* dest_offset = argument(5);
6199 
6200   // (1) src and dest are arrays.
6201   const Type* src_type = src->Value(&_gvn);
6202   const Type* dest_type = dest->Value(&_gvn);
6203   const TypeAryPtr* top_src = src_type->isa_aryptr();
6204   const TypeAryPtr* top_dest = dest_type->isa_aryptr();
6205   assert(top_src != NULL && top_src->klass() != NULL &&
6206          top_dest != NULL && top_dest->klass() != NULL, "args are strange");
6207 
6208   // checks are the responsibility of the caller
6209   Node* src_start = src;
6210   Node* dest_start = dest;
6211   if (src_offset != nullptr || dest_offset != nullptr) {
6212     assert(src_offset != nullptr && dest_offset != nullptr, "");
6213     src_start = array_element_address(src, src_offset, T_BYTE);
6214     dest_start = array_element_address(dest, dest_offset, T_BYTE);
6215   }
6216 
6217   // if we are in this set of code, we "know" the embeddedCipher is an AESCrypt object
6218   // (because of the predicated logic executed earlier).
6219   // so we cast it here safely.
6220   // this requires a newer class file that has this array as littleEndian ints, otherwise we revert to java
6221   Node* embeddedCipherObj = load_field_from_object(counterMode_object, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;");
6222   if (embeddedCipherObj == nullptr) return false;
6223   // cast it to what we know it will be at runtime
6224   const TypeInstPtr* tinst = _gvn.type(counterMode_object)->isa_instptr();
6225   assert(tinst != nullptr, "CTR obj is null");
6226   assert(tinst->klass()->is_loaded(), "CTR obj is not loaded");
6227   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6228   assert(klass_AESCrypt->is_loaded(), "predicate checks that this class is loaded");
6229   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6230   const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_AESCrypt);
6231   const TypeOopPtr* xtype = aklass->as_instance_type();
6232   Node* aescrypt_object = new CheckCastPPNode(control(), embeddedCipherObj, xtype);
6233   aescrypt_object = _gvn.transform(aescrypt_object);
6234   // we need to get the start of the aescrypt_object's expanded key array
6235   Node* k_start = get_key_start_from_aescrypt_object(aescrypt_object);
6236   if (k_start == nullptr) return false;
6237   // similarly, get the start address of the r vector
6238   Node* obj_counter = load_field_from_object(counterMode_object, "counter", "[B");
6239   if (obj_counter == nullptr) return false;
6240   Node* cnt_start = array_element_address(obj_counter, intcon(0), T_BYTE);
6241 
6242   Node* saved_encCounter = load_field_from_object(counterMode_object, "encryptedCounter", "[B");
6243   if (saved_encCounter == nullptr) return false;
6244   Node* saved_encCounter_start = array_element_address(saved_encCounter, intcon(0), T_BYTE);
6245   Node* used = field_address_from_object(counterMode_object, "used", "I", /*is_exact*/ false);
6246 
6247   // Call the stub, passing src_start, dest_start, k_start, r_start and src_len
6248   Node* ctrCrypt = make_runtime_call(RC_LEAF|RC_NO_FP,
6249                                      OptoRuntime::counterMode_aescrypt_Type(),
6250                                      stubAddr, stubName, TypePtr::BOTTOM,
6251                                      src_start, dest_start, k_start, cnt_start, len, saved_encCounter_start, used);
6252 
6253   // return cipher length (int)
6254   Node* retvalue = _gvn.transform(new ProjNode(ctrCrypt, TypeFunc::Parms));
6255   set_result(retvalue);
6256   return true;
6257 }
6258 
6259 //------------------------------get_key_start_from_aescrypt_object-----------------------
6260 Node * LibraryCallKit::get_key_start_from_aescrypt_object(Node *aescrypt_object) {
6261 #if defined(PPC64) || defined(S390)
6262   // MixColumns for decryption can be reduced by preprocessing MixColumns with round keys.
6263   // Intel's extention is based on this optimization and AESCrypt generates round keys by preprocessing MixColumns.
6264   // However, ppc64 vncipher processes MixColumns and requires the same round keys with encryption.
6265   // The ppc64 stubs of encryption and decryption use the same round keys (sessionK[0]).
6266   Node* objSessionK = load_field_from_object(aescrypt_object, "sessionK", "[[I");
6267   assert (objSessionK != nullptr, "wrong version of com.sun.crypto.provider.AESCrypt");
6268   if (objSessionK == nullptr) {
6269     return (Node *) nullptr;
6270   }
6271   Node* objAESCryptKey = load_array_element(objSessionK, intcon(0), TypeAryPtr::OOPS, /* set_ctrl */ true);
6272 #else
6273   Node* objAESCryptKey = load_field_from_object(aescrypt_object, "K", "[I");
6274 #endif // PPC64
6275   assert (objAESCryptKey != nullptr, "wrong version of com.sun.crypto.provider.AESCrypt");
6276   if (objAESCryptKey == nullptr) return (Node *) nullptr;
6277 
6278   // now have the array, need to get the start address of the K array
6279   Node* k_start = array_element_address(objAESCryptKey, intcon(0), T_INT);
6280   return k_start;
6281 }
6282 
6283 //----------------------------inline_cipherBlockChaining_AESCrypt_predicate----------------------------
6284 // Return node representing slow path of predicate check.
6285 // the pseudo code we want to emulate with this predicate is:
6286 // for encryption:
6287 //    if (embeddedCipherObj instanceof AESCrypt) do_intrinsic, else do_javapath
6288 // for decryption:
6289 //    if ((embeddedCipherObj instanceof AESCrypt) && (cipher!=plain)) do_intrinsic, else do_javapath
6290 //    note cipher==plain is more conservative than the original java code but that's OK
6291 //
6292 Node* LibraryCallKit::inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting) {
6293   // The receiver was checked for null already.
6294   Node* objCBC = argument(0);
6295 
6296   Node* src = argument(1);
6297   Node* dest = argument(4);
6298 
6299   // Load embeddedCipher field of CipherBlockChaining object.
6300   Node* embeddedCipherObj = load_field_from_object(objCBC, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;");
6301 
6302   // get AESCrypt klass for instanceOf check
6303   // AESCrypt might not be loaded yet if some other SymmetricCipher got us to this compile point
6304   // will have same classloader as CipherBlockChaining object
6305   const TypeInstPtr* tinst = _gvn.type(objCBC)->isa_instptr();
6306   assert(tinst != nullptr, "CBCobj is null");
6307   assert(tinst->klass()->is_loaded(), "CBCobj is not loaded");
6308 
6309   // we want to do an instanceof comparison against the AESCrypt class
6310   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6311   if (!klass_AESCrypt->is_loaded()) {
6312     // if AESCrypt is not even loaded, we never take the intrinsic fast path
6313     Node* ctrl = control();
6314     set_control(top()); // no regular fast path
6315     return ctrl;
6316   }
6317 
6318   src = must_be_not_null(src, true);
6319   dest = must_be_not_null(dest, true);
6320 
6321   // Resolve oops to stable for CmpP below.
6322   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6323 
6324   Node* instof = gen_instanceof(embeddedCipherObj, makecon(TypeKlassPtr::make(instklass_AESCrypt)));
6325   Node* cmp_instof  = _gvn.transform(new CmpINode(instof, intcon(1)));
6326   Node* bool_instof  = _gvn.transform(new BoolNode(cmp_instof, BoolTest::ne));
6327 
6328   Node* instof_false = generate_guard(bool_instof, nullptr, PROB_MIN);
6329 
6330   // for encryption, we are done
6331   if (!decrypting)
6332     return instof_false;  // even if it is null
6333 
6334   // for decryption, we need to add a further check to avoid
6335   // taking the intrinsic path when cipher and plain are the same
6336   // see the original java code for why.
6337   RegionNode* region = new RegionNode(3);
6338   region->init_req(1, instof_false);
6339 
6340   Node* cmp_src_dest = _gvn.transform(new CmpPNode(src, dest));
6341   Node* bool_src_dest = _gvn.transform(new BoolNode(cmp_src_dest, BoolTest::eq));
6342   Node* src_dest_conjoint = generate_guard(bool_src_dest, nullptr, PROB_MIN);
6343   region->init_req(2, src_dest_conjoint);
6344 
6345   record_for_igvn(region);
6346   return _gvn.transform(region);
6347 }
6348 
6349 //----------------------------inline_electronicCodeBook_AESCrypt_predicate----------------------------
6350 // Return node representing slow path of predicate check.
6351 // the pseudo code we want to emulate with this predicate is:
6352 // for encryption:
6353 //    if (embeddedCipherObj instanceof AESCrypt) do_intrinsic, else do_javapath
6354 // for decryption:
6355 //    if ((embeddedCipherObj instanceof AESCrypt) && (cipher!=plain)) do_intrinsic, else do_javapath
6356 //    note cipher==plain is more conservative than the original java code but that's OK
6357 //
6358 Node* LibraryCallKit::inline_electronicCodeBook_AESCrypt_predicate(bool decrypting) {
6359   // The receiver was checked for null already.
6360   Node* objECB = argument(0);
6361 
6362   // Load embeddedCipher field of ElectronicCodeBook object.
6363   Node* embeddedCipherObj = load_field_from_object(objECB, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;");
6364 
6365   // get AESCrypt klass for instanceOf check
6366   // AESCrypt might not be loaded yet if some other SymmetricCipher got us to this compile point
6367   // will have same classloader as ElectronicCodeBook object
6368   const TypeInstPtr* tinst = _gvn.type(objECB)->isa_instptr();
6369   assert(tinst != nullptr, "ECBobj is null");
6370   assert(tinst->klass()->is_loaded(), "ECBobj is not loaded");
6371 
6372   // we want to do an instanceof comparison against the AESCrypt class
6373   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6374   if (!klass_AESCrypt->is_loaded()) {
6375     // if AESCrypt is not even loaded, we never take the intrinsic fast path
6376     Node* ctrl = control();
6377     set_control(top()); // no regular fast path
6378     return ctrl;
6379   }
6380   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6381 
6382   Node* instof = gen_instanceof(embeddedCipherObj, makecon(TypeKlassPtr::make(instklass_AESCrypt)));
6383   Node* cmp_instof = _gvn.transform(new CmpINode(instof, intcon(1)));
6384   Node* bool_instof = _gvn.transform(new BoolNode(cmp_instof, BoolTest::ne));
6385 
6386   Node* instof_false = generate_guard(bool_instof, nullptr, PROB_MIN);
6387 
6388   // for encryption, we are done
6389   if (!decrypting)
6390     return instof_false;  // even if it is null
6391 
6392   // for decryption, we need to add a further check to avoid
6393   // taking the intrinsic path when cipher and plain are the same
6394   // see the original java code for why.
6395   RegionNode* region = new RegionNode(3);
6396   region->init_req(1, instof_false);
6397   Node* src = argument(1);
6398   Node* dest = argument(4);
6399   Node* cmp_src_dest = _gvn.transform(new CmpPNode(src, dest));
6400   Node* bool_src_dest = _gvn.transform(new BoolNode(cmp_src_dest, BoolTest::eq));
6401   Node* src_dest_conjoint = generate_guard(bool_src_dest, nullptr, PROB_MIN);
6402   region->init_req(2, src_dest_conjoint);
6403 
6404   record_for_igvn(region);
6405   return _gvn.transform(region);
6406 }
6407 
6408 //----------------------------inline_counterMode_AESCrypt_predicate----------------------------
6409 // Return node representing slow path of predicate check.
6410 // the pseudo code we want to emulate with this predicate is:
6411 // for encryption:
6412 //    if (embeddedCipherObj instanceof AESCrypt) do_intrinsic, else do_javapath
6413 // for decryption:
6414 //    if ((embeddedCipherObj instanceof AESCrypt) && (cipher!=plain)) do_intrinsic, else do_javapath
6415 //    note cipher==plain is more conservative than the original java code but that's OK
6416 //
6417 
6418 Node* LibraryCallKit::inline_counterMode_AESCrypt_predicate() {
6419   // The receiver was checked for null already.
6420   Node* objCTR = argument(0);
6421 
6422   // Load embeddedCipher field of CipherBlockChaining object.
6423   Node* embeddedCipherObj = load_field_from_object(objCTR, "embeddedCipher", "Lcom/sun/crypto/provider/SymmetricCipher;");
6424 
6425   // get AESCrypt klass for instanceOf check
6426   // AESCrypt might not be loaded yet if some other SymmetricCipher got us to this compile point
6427   // will have same classloader as CipherBlockChaining object
6428   const TypeInstPtr* tinst = _gvn.type(objCTR)->isa_instptr();
6429   assert(tinst != nullptr, "CTRobj is null");
6430   assert(tinst->klass()->is_loaded(), "CTRobj is not loaded");
6431 
6432   // we want to do an instanceof comparison against the AESCrypt class
6433   ciKlass* klass_AESCrypt = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make("com/sun/crypto/provider/AESCrypt"));
6434   if (!klass_AESCrypt->is_loaded()) {
6435     // if AESCrypt is not even loaded, we never take the intrinsic fast path
6436     Node* ctrl = control();
6437     set_control(top()); // no regular fast path
6438     return ctrl;
6439   }
6440 
6441   ciInstanceKlass* instklass_AESCrypt = klass_AESCrypt->as_instance_klass();
6442   Node* instof = gen_instanceof(embeddedCipherObj, makecon(TypeKlassPtr::make(instklass_AESCrypt)));
6443   Node* cmp_instof = _gvn.transform(new CmpINode(instof, intcon(1)));
6444   Node* bool_instof = _gvn.transform(new BoolNode(cmp_instof, BoolTest::ne));
6445   Node* instof_false = generate_guard(bool_instof, nullptr, PROB_MIN);
6446 
6447   return instof_false; // even if it is null
6448 }
6449 
6450 //------------------------------inline_ghash_processBlocks
6451 bool LibraryCallKit::inline_ghash_processBlocks() {
6452   address stubAddr;
6453   const char *stubName;
6454   assert(UseGHASHIntrinsics, "need GHASH intrinsics support");
6455 
6456   stubAddr = StubRoutines::ghash_processBlocks();
6457   stubName = "ghash_processBlocks";
6458 
6459   Node* data           = argument(0);
6460   Node* offset         = argument(1);
6461   Node* len            = argument(2);
6462   Node* state          = argument(3);
6463   Node* subkeyH        = argument(4);
6464 
6465   state = must_be_not_null(state, true);
6466   subkeyH = must_be_not_null(subkeyH, true);
6467   data = must_be_not_null(data, true);
6468 
6469   Node* state_start  = array_element_address(state, intcon(0), T_LONG);
6470   assert(state_start, "state is null");
6471   Node* subkeyH_start  = array_element_address(subkeyH, intcon(0), T_LONG);
6472   assert(subkeyH_start, "subkeyH is null");
6473   Node* data_start  = array_element_address(data, offset, T_BYTE);
6474   assert(data_start, "data is null");
6475 
6476   Node* ghash = make_runtime_call(RC_LEAF|RC_NO_FP,
6477                                   OptoRuntime::ghash_processBlocks_Type(),
6478                                   stubAddr, stubName, TypePtr::BOTTOM,
6479                                   state_start, subkeyH_start, data_start, len);
6480   return true;
6481 }
6482 
6483 bool LibraryCallKit::inline_base64_encodeBlock() {
6484   address stubAddr;
6485   const char *stubName;
6486   assert(UseBASE64Intrinsics, "need Base64 intrinsics support");
6487   assert(callee()->signature()->size() == 6, "base64_encodeBlock has 6 parameters");
6488   stubAddr = StubRoutines::base64_encodeBlock();
6489   stubName = "encodeBlock";
6490 
6491   if (!stubAddr) return false;
6492   Node* base64obj = argument(0);
6493   Node* src = argument(1);
6494   Node* offset = argument(2);
6495   Node* len = argument(3);
6496   Node* dest = argument(4);
6497   Node* dp = argument(5);
6498   Node* isURL = argument(6);
6499 
6500   src = must_be_not_null(src, true);
6501   dest = must_be_not_null(dest, true);
6502 
6503   Node* src_start = array_element_address(src, intcon(0), T_BYTE);
6504   assert(src_start, "source array is null");
6505   Node* dest_start = array_element_address(dest, intcon(0), T_BYTE);
6506   assert(dest_start, "destination array is null");
6507 
6508   Node* base64 = make_runtime_call(RC_LEAF,
6509                                    OptoRuntime::base64_encodeBlock_Type(),
6510                                    stubAddr, stubName, TypePtr::BOTTOM,
6511                                    src_start, offset, len, dest_start, dp, isURL);
6512   return true;
6513 }
6514 
6515 bool LibraryCallKit::inline_base64_decodeBlock() {
6516   address stubAddr;
6517   const char *stubName;
6518   assert(UseBASE64Intrinsics, "need Base64 intrinsics support");
6519   assert(callee()->signature()->size() == 7, "base64_decodeBlock has 7 parameters");
6520   stubAddr = StubRoutines::base64_decodeBlock();
6521   stubName = "decodeBlock";
6522 
6523   if (!stubAddr) return false;
6524   Node* base64obj = argument(0);
6525   Node* src = argument(1);
6526   Node* src_offset = argument(2);
6527   Node* len = argument(3);
6528   Node* dest = argument(4);
6529   Node* dest_offset = argument(5);
6530   Node* isURL = argument(6);
6531   Node* isMIME = argument(7);
6532 
6533   src = must_be_not_null(src, true);
6534   dest = must_be_not_null(dest, true);
6535 
6536   Node* src_start = array_element_address(src, intcon(0), T_BYTE);
6537   assert(src_start, "source array is null");
6538   Node* dest_start = array_element_address(dest, intcon(0), T_BYTE);
6539   assert(dest_start, "destination array is null");
6540 
6541   Node* call = make_runtime_call(RC_LEAF,
6542                                  OptoRuntime::base64_decodeBlock_Type(),
6543                                  stubAddr, stubName, TypePtr::BOTTOM,
6544                                  src_start, src_offset, len, dest_start, dest_offset, isURL, isMIME);
6545   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
6546   set_result(result);
6547   return true;
6548 }
6549 
6550 //------------------------------inline_digestBase_implCompress-----------------------
6551 //
6552 // Calculate MD5 for single-block byte[] array.
6553 // void com.sun.security.provider.MD5.implCompress(byte[] buf, int ofs)
6554 //
6555 // Calculate SHA (i.e., SHA-1) for single-block byte[] array.
6556 // void com.sun.security.provider.SHA.implCompress(byte[] buf, int ofs)
6557 //
6558 // Calculate SHA2 (i.e., SHA-244 or SHA-256) for single-block byte[] array.
6559 // void com.sun.security.provider.SHA2.implCompress(byte[] buf, int ofs)
6560 //
6561 // Calculate SHA5 (i.e., SHA-384 or SHA-512) for single-block byte[] array.
6562 // void com.sun.security.provider.SHA5.implCompress(byte[] buf, int ofs)
6563 //
6564 // Calculate SHA3 (i.e., SHA3-224 or SHA3-256 or SHA3-384 or SHA3-512) for single-block byte[] array.
6565 // void com.sun.security.provider.SHA3.implCompress(byte[] buf, int ofs)
6566 //
6567 bool LibraryCallKit::inline_digestBase_implCompress(vmIntrinsics::ID id) {
6568   assert(callee()->signature()->size() == 2, "sha_implCompress has 2 parameters");
6569 
6570   Node* digestBase_obj = argument(0);
6571   Node* src            = argument(1); // type oop
6572   Node* ofs            = argument(2); // type int
6573 
6574   const Type* src_type = src->Value(&_gvn);
6575   const TypeAryPtr* top_src = src_type->isa_aryptr();
6576   if (top_src  == NULL || top_src->klass()  == NULL) {
6577     // failed array check
6578     return false;
6579   }
6580   // Figure out the size and type of the elements we will be copying.
6581   BasicType src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
6582   if (src_elem != T_BYTE) {
6583     return false;
6584   }
6585   // 'src_start' points to src array + offset
6586   src = must_be_not_null(src, true);
6587   Node* src_start = array_element_address(src, ofs, src_elem);
6588   Node* state = nullptr;
6589   Node* digest_length = nullptr;
6590   address stubAddr;
6591   const char *stubName;
6592 
6593   switch(id) {
6594   case vmIntrinsics::_md5_implCompress:
6595     assert(UseMD5Intrinsics, "need MD5 instruction support");
6596     state = get_state_from_digest_object(digestBase_obj, T_INT);
6597     stubAddr = StubRoutines::md5_implCompress();
6598     stubName = "md5_implCompress";
6599     break;
6600   case vmIntrinsics::_sha_implCompress:
6601     assert(UseSHA1Intrinsics, "need SHA1 instruction support");
6602     state = get_state_from_digest_object(digestBase_obj, T_INT);
6603     stubAddr = StubRoutines::sha1_implCompress();
6604     stubName = "sha1_implCompress";
6605     break;
6606   case vmIntrinsics::_sha2_implCompress:
6607     assert(UseSHA256Intrinsics, "need SHA256 instruction support");
6608     state = get_state_from_digest_object(digestBase_obj, T_INT);
6609     stubAddr = StubRoutines::sha256_implCompress();
6610     stubName = "sha256_implCompress";
6611     break;
6612   case vmIntrinsics::_sha5_implCompress:
6613     assert(UseSHA512Intrinsics, "need SHA512 instruction support");
6614     state = get_state_from_digest_object(digestBase_obj, T_LONG);
6615     stubAddr = StubRoutines::sha512_implCompress();
6616     stubName = "sha512_implCompress";
6617     break;
6618   case vmIntrinsics::_sha3_implCompress:
6619     assert(UseSHA3Intrinsics, "need SHA3 instruction support");
6620     state = get_state_from_digest_object(digestBase_obj, T_BYTE);
6621     stubAddr = StubRoutines::sha3_implCompress();
6622     stubName = "sha3_implCompress";
6623     digest_length = get_digest_length_from_digest_object(digestBase_obj);
6624     if (digest_length == nullptr) return false;
6625     break;
6626   default:
6627     fatal_unexpected_iid(id);
6628     return false;
6629   }
6630   if (state == nullptr) return false;
6631 
6632   assert(stubAddr != nullptr, "Stub is generated");
6633   if (stubAddr == nullptr) return false;
6634 
6635   // Call the stub.
6636   Node* call;
6637   if (digest_length == nullptr) {
6638     call = make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::digestBase_implCompress_Type(false),
6639                              stubAddr, stubName, TypePtr::BOTTOM,
6640                              src_start, state);
6641   } else {
6642     call = make_runtime_call(RC_LEAF|RC_NO_FP, OptoRuntime::digestBase_implCompress_Type(true),
6643                              stubAddr, stubName, TypePtr::BOTTOM,
6644                              src_start, state, digest_length);
6645   }
6646 
6647   return true;
6648 }
6649 
6650 //------------------------------inline_digestBase_implCompressMB-----------------------
6651 //
6652 // Calculate MD5/SHA/SHA2/SHA5/SHA3 for multi-block byte[] array.
6653 // int com.sun.security.provider.DigestBase.implCompressMultiBlock(byte[] b, int ofs, int limit)
6654 //
6655 bool LibraryCallKit::inline_digestBase_implCompressMB(int predicate) {
6656   assert(UseMD5Intrinsics || UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics || UseSHA3Intrinsics,
6657          "need MD5/SHA1/SHA256/SHA512/SHA3 instruction support");
6658   assert((uint)predicate < 5, "sanity");
6659   assert(callee()->signature()->size() == 3, "digestBase_implCompressMB has 3 parameters");
6660 
6661   Node* digestBase_obj = argument(0); // The receiver was checked for null already.
6662   Node* src            = argument(1); // byte[] array
6663   Node* ofs            = argument(2); // type int
6664   Node* limit          = argument(3); // type int
6665 
6666   const Type* src_type = src->Value(&_gvn);
6667   const TypeAryPtr* top_src = src_type->isa_aryptr();
6668   if (top_src  == NULL || top_src->klass()  == NULL) {
6669     // failed array check
6670     return false;
6671   }
6672   // Figure out the size and type of the elements we will be copying.
6673   BasicType src_elem = top_src->klass()->as_array_klass()->element_type()->basic_type();
6674   if (src_elem != T_BYTE) {
6675     return false;
6676   }
6677   // 'src_start' points to src array + offset
6678   src = must_be_not_null(src, false);
6679   Node* src_start = array_element_address(src, ofs, src_elem);
6680 
6681   const char* klass_digestBase_name = nullptr;
6682   const char* stub_name = nullptr;
6683   address     stub_addr = nullptr;
6684   BasicType elem_type = T_INT;
6685 
6686   switch (predicate) {
6687   case 0:
6688     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_md5_implCompress)) {
6689       klass_digestBase_name = "sun/security/provider/MD5";
6690       stub_name = "md5_implCompressMB";
6691       stub_addr = StubRoutines::md5_implCompressMB();
6692     }
6693     break;
6694   case 1:
6695     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_sha_implCompress)) {
6696       klass_digestBase_name = "sun/security/provider/SHA";
6697       stub_name = "sha1_implCompressMB";
6698       stub_addr = StubRoutines::sha1_implCompressMB();
6699     }
6700     break;
6701   case 2:
6702     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_sha2_implCompress)) {
6703       klass_digestBase_name = "sun/security/provider/SHA2";
6704       stub_name = "sha256_implCompressMB";
6705       stub_addr = StubRoutines::sha256_implCompressMB();
6706     }
6707     break;
6708   case 3:
6709     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_sha5_implCompress)) {
6710       klass_digestBase_name = "sun/security/provider/SHA5";
6711       stub_name = "sha512_implCompressMB";
6712       stub_addr = StubRoutines::sha512_implCompressMB();
6713       elem_type = T_LONG;
6714     }
6715     break;
6716   case 4:
6717     if (vmIntrinsics::is_intrinsic_available(vmIntrinsics::_sha3_implCompress)) {
6718       klass_digestBase_name = "sun/security/provider/SHA3";
6719       stub_name = "sha3_implCompressMB";
6720       stub_addr = StubRoutines::sha3_implCompressMB();
6721       elem_type = T_BYTE;
6722     }
6723     break;
6724   default:
6725     fatal("unknown DigestBase intrinsic predicate: %d", predicate);
6726   }
6727   if (klass_digestBase_name != nullptr) {
6728     assert(stub_addr != nullptr, "Stub is generated");
6729     if (stub_addr == nullptr) return false;
6730 
6731     // get DigestBase klass to lookup for SHA klass
6732     const TypeInstPtr* tinst = _gvn.type(digestBase_obj)->isa_instptr();
6733     assert(tinst != nullptr, "digestBase_obj is not instance???");
6734     assert(tinst->klass()->is_loaded(), "DigestBase is not loaded");
6735 
6736     ciKlass* klass_digestBase = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make(klass_digestBase_name));
6737     assert(klass_digestBase->is_loaded(), "predicate checks that this class is loaded");
6738     ciInstanceKlass* instklass_digestBase = klass_digestBase->as_instance_klass();
6739     return inline_digestBase_implCompressMB(digestBase_obj, instklass_digestBase, elem_type, stub_addr, stub_name, src_start, ofs, limit);
6740   }
6741   return false;
6742 }
6743 
6744 //------------------------------inline_digestBase_implCompressMB-----------------------
6745 bool LibraryCallKit::inline_digestBase_implCompressMB(Node* digestBase_obj, ciInstanceKlass* instklass_digestBase,
6746                                                       BasicType elem_type, address stubAddr, const char *stubName,
6747                                                       Node* src_start, Node* ofs, Node* limit) {
6748   const TypeKlassPtr* aklass = TypeKlassPtr::make(instklass_digestBase);
6749   const TypeOopPtr* xtype = aklass->as_instance_type();
6750   Node* digest_obj = new CheckCastPPNode(control(), digestBase_obj, xtype);
6751   digest_obj = _gvn.transform(digest_obj);
6752 
6753   Node* state = get_state_from_digest_object(digest_obj, elem_type);
6754   if (state == nullptr) return false;
6755 
6756   Node* digest_length = nullptr;
6757   if (strcmp("sha3_implCompressMB", stubName) == 0) {
6758     digest_length = get_digest_length_from_digest_object(digest_obj);
6759     if (digest_length == nullptr) return false;
6760   }
6761 
6762   // Call the stub.
6763   Node* call;
6764   if (digest_length == nullptr) {
6765     call = make_runtime_call(RC_LEAF|RC_NO_FP,
6766                              OptoRuntime::digestBase_implCompressMB_Type(false),
6767                              stubAddr, stubName, TypePtr::BOTTOM,
6768                              src_start, state, ofs, limit);
6769   } else {
6770      call = make_runtime_call(RC_LEAF|RC_NO_FP,
6771                              OptoRuntime::digestBase_implCompressMB_Type(true),
6772                              stubAddr, stubName, TypePtr::BOTTOM,
6773                              src_start, state, digest_length, ofs, limit);
6774   }
6775 
6776   // return ofs (int)
6777   Node* result = _gvn.transform(new ProjNode(call, TypeFunc::Parms));
6778   set_result(result);
6779 
6780   return true;
6781 }
6782 
6783 //------------------------------get_state_from_digest_object-----------------------
6784 Node * LibraryCallKit::get_state_from_digest_object(Node *digest_object, BasicType elem_type) {
6785   const char* state_type;
6786   switch (elem_type) {
6787     case T_BYTE: state_type = "[B"; break;
6788     case T_INT:  state_type = "[I"; break;
6789     case T_LONG: state_type = "[J"; break;
6790     default: ShouldNotReachHere();
6791   }
6792   Node* digest_state = load_field_from_object(digest_object, "state", state_type);
6793   assert (digest_state != nullptr, "wrong version of sun.security.provider.MD5/SHA/SHA2/SHA5/SHA3");
6794   if (digest_state == nullptr) return (Node *) nullptr;
6795 
6796   // now have the array, need to get the start address of the state array
6797   Node* state = array_element_address(digest_state, intcon(0), elem_type);
6798   return state;
6799 }
6800 
6801 //------------------------------get_digest_length_from_sha3_object----------------------------------
6802 Node * LibraryCallKit::get_digest_length_from_digest_object(Node *digest_object) {
6803   Node* digest_length = load_field_from_object(digest_object, "digestLength", "I");
6804   assert (digest_length != nullptr, "sanity");
6805   return digest_length;
6806 }
6807 
6808 //----------------------------inline_digestBase_implCompressMB_predicate----------------------------
6809 // Return node representing slow path of predicate check.
6810 // the pseudo code we want to emulate with this predicate is:
6811 //    if (digestBaseObj instanceof MD5/SHA/SHA2/SHA5/SHA3) do_intrinsic, else do_javapath
6812 //
6813 Node* LibraryCallKit::inline_digestBase_implCompressMB_predicate(int predicate) {
6814   assert(UseMD5Intrinsics || UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics || UseSHA3Intrinsics,
6815          "need MD5/SHA1/SHA256/SHA512/SHA3 instruction support");
6816   assert((uint)predicate < 5, "sanity");
6817 
6818   // The receiver was checked for null already.
6819   Node* digestBaseObj = argument(0);
6820 
6821   // get DigestBase klass for instanceOf check
6822   const TypeInstPtr* tinst = _gvn.type(digestBaseObj)->isa_instptr();
6823   assert(tinst != nullptr, "digestBaseObj is null");
6824   assert(tinst->klass()->is_loaded(), "DigestBase is not loaded");
6825 
6826   const char* klass_name = nullptr;
6827   switch (predicate) {
6828   case 0:
6829     if (UseMD5Intrinsics) {
6830       // we want to do an instanceof comparison against the MD5 class
6831       klass_name = "sun/security/provider/MD5";
6832     }
6833     break;
6834   case 1:
6835     if (UseSHA1Intrinsics) {
6836       // we want to do an instanceof comparison against the SHA class
6837       klass_name = "sun/security/provider/SHA";
6838     }
6839     break;
6840   case 2:
6841     if (UseSHA256Intrinsics) {
6842       // we want to do an instanceof comparison against the SHA2 class
6843       klass_name = "sun/security/provider/SHA2";
6844     }
6845     break;
6846   case 3:
6847     if (UseSHA512Intrinsics) {
6848       // we want to do an instanceof comparison against the SHA5 class
6849       klass_name = "sun/security/provider/SHA5";
6850     }
6851     break;
6852   case 4:
6853     if (UseSHA3Intrinsics) {
6854       // we want to do an instanceof comparison against the SHA3 class
6855       klass_name = "sun/security/provider/SHA3";
6856     }
6857     break;
6858   default:
6859     fatal("unknown SHA intrinsic predicate: %d", predicate);
6860   }
6861 
6862   ciKlass* klass = nullptr;
6863   if (klass_name != nullptr) {
6864     klass = tinst->klass()->as_instance_klass()->find_klass(ciSymbol::make(klass_name));
6865   }
6866   if ((klass == nullptr) || !klass->is_loaded()) {
6867     // if none of MD5/SHA/SHA2/SHA5 is loaded, we never take the intrinsic fast path
6868     Node* ctrl = control();
6869     set_control(top()); // no intrinsic path
6870     return ctrl;
6871   }
6872   ciInstanceKlass* instklass = klass->as_instance_klass();
6873 
6874   Node* instof = gen_instanceof(digestBaseObj, makecon(TypeKlassPtr::make(instklass)));
6875   Node* cmp_instof = _gvn.transform(new CmpINode(instof, intcon(1)));
6876   Node* bool_instof = _gvn.transform(new BoolNode(cmp_instof, BoolTest::ne));
6877   Node* instof_false = generate_guard(bool_instof, nullptr, PROB_MIN);
6878 
6879   return instof_false;  // even if it is null
6880 }
6881 
6882 //-------------inline_fma-----------------------------------
6883 bool LibraryCallKit::inline_fma(vmIntrinsics::ID id) {
6884   Node *a = nullptr;
6885   Node *b = nullptr;
6886   Node *c = nullptr;
6887   Node* result = nullptr;
6888   switch (id) {
6889   case vmIntrinsics::_fmaD:
6890     assert(callee()->signature()->size() == 6, "fma has 3 parameters of size 2 each.");
6891     // no receiver since it is static method
6892     a = round_double_node(argument(0));
6893     b = round_double_node(argument(2));
6894     c = round_double_node(argument(4));
6895     result = _gvn.transform(new FmaDNode(control(), a, b, c));
6896     break;
6897   case vmIntrinsics::_fmaF:
6898     assert(callee()->signature()->size() == 3, "fma has 3 parameters of size 1 each.");
6899     a = argument(0);
6900     b = argument(1);
6901     c = argument(2);
6902     result = _gvn.transform(new FmaFNode(control(), a, b, c));
6903     break;
6904   default:
6905     fatal_unexpected_iid(id);  break;
6906   }
6907   set_result(result);
6908   return true;
6909 }
6910 
6911 bool LibraryCallKit::inline_character_compare(vmIntrinsics::ID id) {
6912   // argument(0) is receiver
6913   Node* codePoint = argument(1);
6914   Node* n = nullptr;
6915 
6916   switch (id) {
6917     case vmIntrinsics::_isDigit :
6918       n = new DigitNode(control(), codePoint);
6919       break;
6920     case vmIntrinsics::_isLowerCase :
6921       n = new LowerCaseNode(control(), codePoint);
6922       break;
6923     case vmIntrinsics::_isUpperCase :
6924       n = new UpperCaseNode(control(), codePoint);
6925       break;
6926     case vmIntrinsics::_isWhitespace :
6927       n = new WhitespaceNode(control(), codePoint);
6928       break;
6929     default:
6930       fatal_unexpected_iid(id);
6931   }
6932 
6933   set_result(_gvn.transform(n));
6934   return true;
6935 }
6936 
6937 //------------------------------inline_fp_min_max------------------------------
6938 bool LibraryCallKit::inline_fp_min_max(vmIntrinsics::ID id) {
6939 /* DISABLED BECAUSE METHOD DATA ISN'T COLLECTED PER CALL-SITE, SEE JDK-8015416.
6940 
6941   // The intrinsic should be used only when the API branches aren't predictable,
6942   // the last one performing the most important comparison. The following heuristic
6943   // uses the branch statistics to eventually bail out if necessary.
6944 
6945   ciMethodData *md = callee()->method_data();
6946 
6947   if ( md != nullptr && md->is_mature() && md->invocation_count() > 0 ) {
6948     ciCallProfile cp = caller()->call_profile_at_bci(bci());
6949 
6950     if ( ((double)cp.count()) / ((double)md->invocation_count()) < 0.8 ) {
6951       // Bail out if the call-site didn't contribute enough to the statistics.
6952       return false;
6953     }
6954 
6955     uint taken = 0, not_taken = 0;
6956 
6957     for (ciProfileData *p = md->first_data(); md->is_valid(p); p = md->next_data(p)) {
6958       if (p->is_BranchData()) {
6959         taken = ((ciBranchData*)p)->taken();
6960         not_taken = ((ciBranchData*)p)->not_taken();
6961       }
6962     }
6963 
6964     double balance = (((double)taken) - ((double)not_taken)) / ((double)md->invocation_count());
6965     balance = balance < 0 ? -balance : balance;
6966     if ( balance > 0.2 ) {
6967       // Bail out if the most important branch is predictable enough.
6968       return false;
6969     }
6970   }
6971 */
6972 
6973   Node *a = nullptr;
6974   Node *b = nullptr;
6975   Node *n = nullptr;
6976   switch (id) {
6977   case vmIntrinsics::_maxF:
6978   case vmIntrinsics::_minF:
6979     assert(callee()->signature()->size() == 2, "minF/maxF has 2 parameters of size 1 each.");
6980     a = argument(0);
6981     b = argument(1);
6982     break;
6983   case vmIntrinsics::_maxD:
6984   case vmIntrinsics::_minD:
6985     assert(callee()->signature()->size() == 4, "minD/maxD has 2 parameters of size 2 each.");
6986     a = round_double_node(argument(0));
6987     b = round_double_node(argument(2));
6988     break;
6989   default:
6990     fatal_unexpected_iid(id);
6991     break;
6992   }
6993   switch (id) {
6994   case vmIntrinsics::_maxF:  n = new MaxFNode(a, b);  break;
6995   case vmIntrinsics::_minF:  n = new MinFNode(a, b);  break;
6996   case vmIntrinsics::_maxD:  n = new MaxDNode(a, b);  break;
6997   case vmIntrinsics::_minD:  n = new MinDNode(a, b);  break;
6998   default:  fatal_unexpected_iid(id);  break;
6999   }
7000   set_result(_gvn.transform(n));
7001   return true;
7002 }
7003 
7004 bool LibraryCallKit::inline_profileBoolean() {
7005   Node* counts = argument(1);
7006   const TypeAryPtr* ary = nullptr;
7007   ciArray* aobj = nullptr;
7008   if (counts->is_Con()
7009       && (ary = counts->bottom_type()->isa_aryptr()) != nullptr
7010       && (aobj = ary->const_oop()->as_array()) != nullptr
7011       && (aobj->length() == 2)) {
7012     // Profile is int[2] where [0] and [1] correspond to false and true value occurrences respectively.
7013     jint false_cnt = aobj->element_value(0).as_int();
7014     jint  true_cnt = aobj->element_value(1).as_int();
7015 
7016     if (C->log() != nullptr) {
7017       C->log()->elem("observe source='profileBoolean' false='%d' true='%d'",
7018                      false_cnt, true_cnt);
7019     }
7020 
7021     if (false_cnt + true_cnt == 0) {
7022       // According to profile, never executed.
7023       uncommon_trap_exact(Deoptimization::Reason_intrinsic,
7024                           Deoptimization::Action_reinterpret);
7025       return true;
7026     }
7027 
7028     // result is a boolean (0 or 1) and its profile (false_cnt & true_cnt)
7029     // is a number of each value occurrences.
7030     Node* result = argument(0);
7031     if (false_cnt == 0 || true_cnt == 0) {
7032       // According to profile, one value has been never seen.
7033       int expected_val = (false_cnt == 0) ? 1 : 0;
7034 
7035       Node* cmp  = _gvn.transform(new CmpINode(result, intcon(expected_val)));
7036       Node* test = _gvn.transform(new BoolNode(cmp, BoolTest::eq));
7037 
7038       IfNode* check = create_and_map_if(control(), test, PROB_ALWAYS, COUNT_UNKNOWN);
7039       Node* fast_path = _gvn.transform(new IfTrueNode(check));
7040       Node* slow_path = _gvn.transform(new IfFalseNode(check));
7041 
7042       { // Slow path: uncommon trap for never seen value and then reexecute
7043         // MethodHandleImpl::profileBoolean() to bump the count, so JIT knows
7044         // the value has been seen at least once.
7045         PreserveJVMState pjvms(this);
7046         PreserveReexecuteState preexecs(this);
7047         jvms()->set_should_reexecute(true);
7048 
7049         set_control(slow_path);
7050         set_i_o(i_o());
7051 
7052         uncommon_trap_exact(Deoptimization::Reason_intrinsic,
7053                             Deoptimization::Action_reinterpret);
7054       }
7055       // The guard for never seen value enables sharpening of the result and
7056       // returning a constant. It allows to eliminate branches on the same value
7057       // later on.
7058       set_control(fast_path);
7059       result = intcon(expected_val);
7060     }
7061     // Stop profiling.
7062     // MethodHandleImpl::profileBoolean() has profiling logic in its bytecode.
7063     // By replacing method body with profile data (represented as ProfileBooleanNode
7064     // on IR level) we effectively disable profiling.
7065     // It enables full speed execution once optimized code is generated.
7066     Node* profile = _gvn.transform(new ProfileBooleanNode(result, false_cnt, true_cnt));
7067     C->record_for_igvn(profile);
7068     set_result(profile);
7069     return true;
7070   } else {
7071     // Continue profiling.
7072     // Profile data isn't available at the moment. So, execute method's bytecode version.
7073     // Usually, when GWT LambdaForms are profiled it means that a stand-alone nmethod
7074     // is compiled and counters aren't available since corresponding MethodHandle
7075     // isn't a compile-time constant.
7076     return false;
7077   }
7078 }
7079 
7080 bool LibraryCallKit::inline_isCompileConstant() {
7081   Node* n = argument(0);
7082   set_result(n->is_Con() ? intcon(1) : intcon(0));
7083   return true;
7084 }
7085 
7086 //------------------------------- inline_getObjectSize --------------------------------------
7087 //
7088 // Calculate the runtime size of the object/array.
7089 //   native long sun.instrument.InstrumentationImpl.getObjectSize0(long nativeAgent, Object objectToSize);
7090 //
7091 bool LibraryCallKit::inline_getObjectSize() {
7092   Node* obj = argument(3);
7093   Node* klass_node = load_object_klass(obj);
7094 
7095   jint  layout_con = Klass::_lh_neutral_value;
7096   Node* layout_val = get_layout_helper(klass_node, layout_con);
7097   int   layout_is_con = (layout_val == nullptr);
7098 
7099   if (layout_is_con) {
7100     // Layout helper is constant, can figure out things at compile time.
7101 
7102     if (Klass::layout_helper_is_instance(layout_con)) {
7103       // Instance case:  layout_con contains the size itself.
7104       Node *size = longcon(Klass::layout_helper_size_in_bytes(layout_con));
7105       set_result(size);
7106     } else {
7107       // Array case: size is round(header + element_size*arraylength).
7108       // Since arraylength is different for every array instance, we have to
7109       // compute the whole thing at runtime.
7110 
7111       Node* arr_length = load_array_length(obj);
7112 
7113       int round_mask = MinObjAlignmentInBytes - 1;
7114       int hsize  = Klass::layout_helper_header_size(layout_con);
7115       int eshift = Klass::layout_helper_log2_element_size(layout_con);
7116 
7117       if ((round_mask & ~right_n_bits(eshift)) == 0) {
7118         round_mask = 0;  // strength-reduce it if it goes away completely
7119       }
7120       assert((hsize & right_n_bits(eshift)) == 0, "hsize is pre-rounded");
7121       Node* header_size = intcon(hsize + round_mask);
7122 
7123       Node* lengthx = ConvI2X(arr_length);
7124       Node* headerx = ConvI2X(header_size);
7125 
7126       Node* abody = lengthx;
7127       if (eshift != 0) {
7128         abody = _gvn.transform(new LShiftXNode(lengthx, intcon(eshift)));
7129       }
7130       Node* size = _gvn.transform( new AddXNode(headerx, abody) );
7131       if (round_mask != 0) {
7132         size = _gvn.transform( new AndXNode(size, MakeConX(~round_mask)) );
7133       }
7134       size = ConvX2L(size);
7135       set_result(size);
7136     }
7137   } else {
7138     // Layout helper is not constant, need to test for array-ness at runtime.
7139 
7140     enum { _instance_path = 1, _array_path, PATH_LIMIT };
7141     RegionNode* result_reg = new RegionNode(PATH_LIMIT);
7142     PhiNode* result_val = new PhiNode(result_reg, TypeLong::LONG);
7143     record_for_igvn(result_reg);
7144 
7145     Node* array_ctl = generate_array_guard(klass_node, nullptr);
7146     if (array_ctl != nullptr) {
7147       // Array case: size is round(header + element_size*arraylength).
7148       // Since arraylength is different for every array instance, we have to
7149       // compute the whole thing at runtime.
7150 
7151       PreserveJVMState pjvms(this);
7152       set_control(array_ctl);
7153       Node* arr_length = load_array_length(obj);
7154 
7155       int round_mask = MinObjAlignmentInBytes - 1;
7156       Node* mask = intcon(round_mask);
7157 
7158       Node* hss = intcon(Klass::_lh_header_size_shift);
7159       Node* hsm = intcon(Klass::_lh_header_size_mask);
7160       Node* header_size = _gvn.transform(new URShiftINode(layout_val, hss));
7161       header_size = _gvn.transform(new AndINode(header_size, hsm));
7162       header_size = _gvn.transform(new AddINode(header_size, mask));
7163 
7164       // There is no need to mask or shift this value.
7165       // The semantics of LShiftINode include an implicit mask to 0x1F.
7166       assert(Klass::_lh_log2_element_size_shift == 0, "use shift in place");
7167       Node* elem_shift = layout_val;
7168 
7169       Node* lengthx = ConvI2X(arr_length);
7170       Node* headerx = ConvI2X(header_size);
7171 
7172       Node* abody = _gvn.transform(new LShiftXNode(lengthx, elem_shift));
7173       Node* size = _gvn.transform(new AddXNode(headerx, abody));
7174       if (round_mask != 0) {
7175         size = _gvn.transform(new AndXNode(size, MakeConX(~round_mask)));
7176       }
7177       size = ConvX2L(size);
7178 
7179       result_reg->init_req(_array_path, control());
7180       result_val->init_req(_array_path, size);
7181     }
7182 
7183     if (!stopped()) {
7184       // Instance case: the layout helper gives us instance size almost directly,
7185       // but we need to mask out the _lh_instance_slow_path_bit.
7186       Node* size = ConvI2X(layout_val);
7187       assert((int) Klass::_lh_instance_slow_path_bit < BytesPerLong, "clear bit");
7188       Node* mask = MakeConX(~(intptr_t) right_n_bits(LogBytesPerLong));
7189       size = _gvn.transform(new AndXNode(size, mask));
7190       size = ConvX2L(size);
7191 
7192       result_reg->init_req(_instance_path, control());
7193       result_val->init_req(_instance_path, size);
7194     }
7195 
7196     set_result(result_reg, result_val);
7197   }
7198 
7199   return true;
7200 }
7201 
7202 //------------------------------- inline_blackhole --------------------------------------
7203 //
7204 // Make sure all arguments to this node are alive.
7205 // This matches methods that were requested to be blackholed through compile commands.
7206 //
7207 bool LibraryCallKit::inline_blackhole() {
7208   assert(callee()->is_static(), "Should have been checked before: only static methods here");
7209   assert(callee()->is_empty(), "Should have been checked before: only empty methods here");
7210   assert(callee()->holder()->is_loaded(), "Should have been checked before: only methods for loaded classes here");
7211 
7212   // Blackhole node pinches only the control, not memory. This allows
7213   // the blackhole to be pinned in the loop that computes blackholed
7214   // values, but have no other side effects, like breaking the optimizations
7215   // across the blackhole.
7216 
7217   Node* bh = _gvn.transform(new BlackholeNode(control()));
7218   set_control(_gvn.transform(new ProjNode(bh, TypeFunc::Control)));
7219 
7220   // Bind call arguments as blackhole arguments to keep them alive
7221   uint nargs = callee()->arg_size();
7222   for (uint i = 0; i < nargs; i++) {
7223     bh->add_req(argument(i));
7224   }
7225 
7226   return true;
7227 }