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