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