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