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