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