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