1 /* 2 * Copyright (c) 2020, 2023, 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 "ci/ciMethod.hpp" 26 #include "classfile/javaClasses.hpp" 27 #include "opto/callGenerator.hpp" 28 #include "opto/graphKit.hpp" 29 #include "opto/castnode.hpp" 30 #include "opto/convertnode.hpp" 31 #include "opto/intrinsicnode.hpp" 32 #include "opto/movenode.hpp" 33 34 class LibraryIntrinsic : public InlineCallGenerator { 35 // Extend the set of intrinsics known to the runtime: 36 public: 37 private: 38 bool _is_virtual; 39 bool _does_virtual_dispatch; 40 int8_t _predicates_count; // Intrinsic is predicated by several conditions 41 int8_t _last_predicate; // Last generated predicate 42 vmIntrinsics::ID _intrinsic_id; 43 44 public: 45 LibraryIntrinsic(ciMethod* m, bool is_virtual, int predicates_count, bool does_virtual_dispatch, vmIntrinsics::ID id) 46 : InlineCallGenerator(m), 47 _is_virtual(is_virtual), 48 _does_virtual_dispatch(does_virtual_dispatch), 49 _predicates_count((int8_t)predicates_count), 50 _last_predicate((int8_t)-1), 51 _intrinsic_id(id) 52 { 53 } 54 virtual bool is_intrinsic() const { return true; } 55 virtual bool is_virtual() const { return _is_virtual; } 56 virtual bool is_predicated() const { return _predicates_count > 0; } 57 virtual int predicates_count() const { return _predicates_count; } 58 virtual bool does_virtual_dispatch() const { return _does_virtual_dispatch; } 59 virtual JVMState* generate(JVMState* jvms); 60 virtual Node* generate_predicate(JVMState* jvms, int predicate); 61 vmIntrinsics::ID intrinsic_id() const { return _intrinsic_id; } 62 }; 63 64 65 // Local helper class for LibraryIntrinsic: 66 class LibraryCallKit : public GraphKit { 67 private: 68 LibraryIntrinsic* _intrinsic; // the library intrinsic being called 69 Node* _result; // the result node, if any 70 int _reexecute_sp; // the stack pointer when bytecode needs to be reexecuted 71 72 const TypeOopPtr* sharpen_unsafe_type(Compile::AliasType* alias_type, const TypePtr *adr_type); 73 74 public: 75 LibraryCallKit(JVMState* jvms, LibraryIntrinsic* intrinsic) 76 : GraphKit(jvms), 77 _intrinsic(intrinsic), 78 _result(NULL) 79 { 80 // Check if this is a root compile. In that case we don't have a caller. 81 if (!jvms->has_method()) { 82 _reexecute_sp = sp(); 83 } else { 84 // Find out how many arguments the interpreter needs when deoptimizing 85 // and save the stack pointer value so it can used by uncommon_trap. 86 // We find the argument count by looking at the declared signature. 87 bool ignored_will_link; 88 ciSignature* declared_signature = NULL; 89 ciMethod* ignored_callee = caller()->get_method_at_bci(bci(), ignored_will_link, &declared_signature); 90 const int nargs = declared_signature->arg_size_for_bc(caller()->java_code_at_bci(bci())); 91 _reexecute_sp = sp() + nargs; // "push" arguments back on stack 92 } 93 } 94 95 virtual LibraryCallKit* is_LibraryCallKit() const { return (LibraryCallKit*)this; } 96 97 ciMethod* caller() const { return jvms()->method(); } 98 int bci() const { return jvms()->bci(); } 99 LibraryIntrinsic* intrinsic() const { return _intrinsic; } 100 vmIntrinsics::ID intrinsic_id() const { return _intrinsic->intrinsic_id(); } 101 ciMethod* callee() const { return _intrinsic->method(); } 102 103 bool try_to_inline(int predicate); 104 Node* try_to_predicate(int predicate); 105 106 void push_result() { 107 // Push the result onto the stack. 108 if (!stopped() && result() != NULL) { 109 BasicType bt = result()->bottom_type()->basic_type(); 110 push_node(bt, result()); 111 } 112 } 113 114 private: 115 void fatal_unexpected_iid(vmIntrinsics::ID iid) { 116 fatal("unexpected intrinsic %d: %s", vmIntrinsics::as_int(iid), vmIntrinsics::name_at(iid)); 117 } 118 119 void set_result(Node* n) { assert(_result == NULL, "only set once"); _result = n; } 120 void set_result(RegionNode* region, PhiNode* value); 121 Node* result() { return _result; } 122 123 virtual int reexecute_sp() { return _reexecute_sp; } 124 125 // Helper functions to inline natives 126 Node* generate_guard(Node* test, RegionNode* region, float true_prob); 127 Node* generate_slow_guard(Node* test, RegionNode* region); 128 Node* generate_fair_guard(Node* test, RegionNode* region); 129 Node* generate_negative_guard(Node* index, RegionNode* region, 130 // resulting CastII of index: 131 Node* *pos_index = NULL); 132 Node* generate_limit_guard(Node* offset, Node* subseq_length, 133 Node* array_length, 134 RegionNode* region); 135 void generate_string_range_check(Node* array, Node* offset, 136 Node* length, bool char_count); 137 Node* current_thread_helper(Node* &tls_output, ByteSize handle_offset, 138 bool is_immutable); 139 Node* generate_current_thread(Node* &tls_output); 140 Node* generate_virtual_thread(Node* threadObj); 141 Node* load_mirror_from_klass(Node* klass); 142 Node* load_klass_from_mirror_common(Node* mirror, bool never_see_null, 143 RegionNode* region, int null_path, 144 int offset); 145 Node* load_klass_from_mirror(Node* mirror, bool never_see_null, 146 RegionNode* region, int null_path) { 147 int offset = java_lang_Class::klass_offset(); 148 return load_klass_from_mirror_common(mirror, never_see_null, 149 region, null_path, 150 offset); 151 } 152 Node* load_array_klass_from_mirror(Node* mirror, bool never_see_null, 153 RegionNode* region, int null_path) { 154 int offset = java_lang_Class::array_klass_offset(); 155 return load_klass_from_mirror_common(mirror, never_see_null, 156 region, null_path, 157 offset); 158 } 159 Node* generate_access_flags_guard(Node* kls, 160 int modifier_mask, int modifier_bits, 161 RegionNode* region); 162 Node* generate_interface_guard(Node* kls, RegionNode* region); 163 Node* generate_hidden_class_guard(Node* kls, RegionNode* region); 164 Node* generate_array_guard(Node* kls, RegionNode* region) { 165 return generate_array_guard_common(kls, region, false, false); 166 } 167 Node* generate_non_array_guard(Node* kls, RegionNode* region) { 168 return generate_array_guard_common(kls, region, false, true); 169 } 170 Node* generate_objArray_guard(Node* kls, RegionNode* region) { 171 return generate_array_guard_common(kls, region, true, false); 172 } 173 Node* generate_non_objArray_guard(Node* kls, RegionNode* region) { 174 return generate_array_guard_common(kls, region, true, true); 175 } 176 Node* generate_array_guard_common(Node* kls, RegionNode* region, 177 bool obj_array, bool not_array); 178 Node* generate_virtual_guard(Node* obj_klass, RegionNode* slow_region); 179 CallJavaNode* generate_method_call(vmIntrinsics::ID method_id, 180 bool is_virtual = false, bool is_static = false); 181 CallJavaNode* generate_method_call_static(vmIntrinsics::ID method_id) { 182 return generate_method_call(method_id, false, true); 183 } 184 CallJavaNode* generate_method_call_virtual(vmIntrinsics::ID method_id) { 185 return generate_method_call(method_id, true, false); 186 } 187 Node* load_field_from_object(Node* fromObj, const char* fieldName, const char* fieldTypeString, DecoratorSet decorators = IN_HEAP, bool is_static = false, ciInstanceKlass* fromKls = NULL); 188 Node* field_address_from_object(Node* fromObj, const char* fieldName, const char* fieldTypeString, bool is_exact = true, bool is_static = false, ciInstanceKlass* fromKls = NULL); 189 190 Node* make_string_method_node(int opcode, Node* str1_start, Node* cnt1, Node* str2_start, Node* cnt2, StrIntrinsicNode::ArgEnc ae); 191 bool inline_string_compareTo(StrIntrinsicNode::ArgEnc ae); 192 bool inline_string_indexOf(StrIntrinsicNode::ArgEnc ae); 193 bool inline_string_indexOfI(StrIntrinsicNode::ArgEnc ae); 194 Node* make_indexOf_node(Node* src_start, Node* src_count, Node* tgt_start, Node* tgt_count, 195 RegionNode* region, Node* phi, StrIntrinsicNode::ArgEnc ae); 196 bool inline_string_indexOfChar(StrIntrinsicNode::ArgEnc ae); 197 bool inline_string_equals(StrIntrinsicNode::ArgEnc ae); 198 bool inline_vectorizedHashCode(); 199 bool inline_string_toBytesU(); 200 bool inline_string_getCharsU(); 201 bool inline_string_copy(bool compress); 202 bool inline_string_char_access(bool is_store); 203 Node* round_double_node(Node* n); 204 bool runtime_math(const TypeFunc* call_type, address funcAddr, const char* funcName); 205 bool inline_math_native(vmIntrinsics::ID id); 206 bool inline_math(vmIntrinsics::ID id); 207 bool inline_double_math(vmIntrinsics::ID id); 208 bool inline_math_pow(); 209 template <typename OverflowOp> 210 bool inline_math_overflow(Node* arg1, Node* arg2); 211 void inline_math_mathExact(Node* math, Node* test); 212 bool inline_math_addExactI(bool is_increment); 213 bool inline_math_addExactL(bool is_increment); 214 bool inline_math_multiplyExactI(); 215 bool inline_math_multiplyExactL(); 216 bool inline_math_multiplyHigh(); 217 bool inline_math_unsignedMultiplyHigh(); 218 bool inline_math_negateExactI(); 219 bool inline_math_negateExactL(); 220 bool inline_math_subtractExactI(bool is_decrement); 221 bool inline_math_subtractExactL(bool is_decrement); 222 bool inline_min_max(vmIntrinsics::ID id); 223 bool inline_notify(vmIntrinsics::ID id); 224 Node* generate_min_max(vmIntrinsics::ID id, Node* x, Node* y); 225 // This returns Type::AnyPtr, RawPtr, or OopPtr. 226 int classify_unsafe_addr(Node* &base, Node* &offset, BasicType type); 227 Node* make_unsafe_address(Node*& base, Node* offset, BasicType type = T_ILLEGAL, bool can_cast = false); 228 229 typedef enum { Relaxed, Opaque, Volatile, Acquire, Release } AccessKind; 230 DecoratorSet mo_decorator_for_access_kind(AccessKind kind); 231 bool inline_unsafe_access(bool is_store, BasicType type, AccessKind kind, bool is_unaligned); 232 static bool klass_needs_init_guard(Node* kls); 233 bool inline_unsafe_allocate(); 234 bool inline_unsafe_newArray(bool uninitialized); 235 bool inline_unsafe_writeback0(); 236 bool inline_unsafe_writebackSync0(bool is_pre); 237 bool inline_unsafe_copyMemory(); 238 239 bool inline_native_currentCarrierThread(); 240 bool inline_native_currentThread(); 241 bool inline_native_setCurrentThread(); 242 243 bool inline_native_scopedValueCache(); 244 Node* scopedValueCache_helper(); 245 bool inline_native_setScopedValueCache(); 246 247 bool inline_native_time_funcs(address method, const char* funcName); 248 #ifdef JFR_HAVE_INTRINSICS 249 bool inline_native_classID(); 250 bool inline_native_getEventWriter(); 251 void extend_setCurrentThread(Node* jt, Node* thread); 252 #endif 253 bool inline_native_Class_query(vmIntrinsics::ID id); 254 bool inline_native_subtype_check(); 255 bool inline_native_getLength(); 256 bool inline_array_copyOf(bool is_copyOfRange); 257 bool inline_array_equals(StrIntrinsicNode::ArgEnc ae); 258 bool inline_preconditions_checkIndex(BasicType bt); 259 void copy_to_clone(Node* obj, Node* alloc_obj, Node* obj_size, bool is_array); 260 bool inline_native_clone(bool is_virtual); 261 bool inline_native_Reflection_getCallerClass(); 262 // Helper function for inlining native object hash method 263 bool inline_native_hashcode(bool is_virtual, bool is_static); 264 bool inline_native_getClass(); 265 266 // Helper functions for inlining arraycopy 267 bool inline_arraycopy(); 268 AllocateArrayNode* tightly_coupled_allocation(Node* ptr); 269 static CallStaticJavaNode* get_uncommon_trap_from_success_proj(Node* node); 270 SafePointNode* create_safepoint_with_state_before_array_allocation(const AllocateArrayNode* alloc) const; 271 void replace_unrelated_uncommon_traps_with_alloc_state(AllocateArrayNode* alloc, JVMState* saved_jvms_before_guards); 272 void replace_unrelated_uncommon_traps_with_alloc_state(JVMState* saved_jvms_before_guards); 273 void create_new_uncommon_trap(CallStaticJavaNode* uncommon_trap_call); 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_before_guards, int saved_reexecute_sp, 276 uint new_idx); 277 278 typedef enum { LS_get_add, LS_get_set, LS_cmp_swap, LS_cmp_swap_weak, LS_cmp_exchange } LoadStoreKind; 279 bool inline_unsafe_load_store(BasicType type, LoadStoreKind kind, AccessKind access_kind); 280 bool inline_unsafe_fence(vmIntrinsics::ID id); 281 bool inline_onspinwait(); 282 bool inline_fp_conversions(vmIntrinsics::ID id); 283 bool inline_fp_range_check(vmIntrinsics::ID id); 284 bool inline_number_methods(vmIntrinsics::ID id); 285 bool inline_bitshuffle_methods(vmIntrinsics::ID id); 286 bool inline_compare_unsigned(vmIntrinsics::ID id); 287 bool inline_divmod_methods(vmIntrinsics::ID id); 288 bool inline_reference_get(); 289 bool inline_reference_refersTo0(bool is_phantom); 290 bool inline_Class_cast(); 291 bool inline_aescrypt_Block(vmIntrinsics::ID id); 292 bool inline_cipherBlockChaining_AESCrypt(vmIntrinsics::ID id); 293 bool inline_electronicCodeBook_AESCrypt(vmIntrinsics::ID id); 294 bool inline_counterMode_AESCrypt(vmIntrinsics::ID id); 295 Node* inline_cipherBlockChaining_AESCrypt_predicate(bool decrypting); 296 Node* inline_electronicCodeBook_AESCrypt_predicate(bool decrypting); 297 Node* inline_counterMode_AESCrypt_predicate(); 298 Node* get_key_start_from_aescrypt_object(Node* aescrypt_object); 299 bool inline_ghash_processBlocks(); 300 bool inline_chacha20Block(); 301 bool inline_base64_encodeBlock(); 302 bool inline_base64_decodeBlock(); 303 bool inline_poly1305_processBlocks(); 304 bool inline_digestBase_implCompress(vmIntrinsics::ID id); 305 bool inline_digestBase_implCompressMB(int predicate); 306 bool inline_digestBase_implCompressMB(Node* digestBaseObj, ciInstanceKlass* instklass, 307 BasicType elem_type, address stubAddr, const char *stubName, 308 Node* src_start, Node* ofs, Node* limit); 309 Node* get_state_from_digest_object(Node *digestBase_object, BasicType elem_type); 310 Node* get_block_size_from_digest_object(Node *digestBase_object); 311 Node* inline_digestBase_implCompressMB_predicate(int predicate); 312 bool inline_encodeISOArray(bool ascii); 313 bool inline_updateCRC32(); 314 bool inline_updateBytesCRC32(); 315 bool inline_updateByteBufferCRC32(); 316 Node* get_table_from_crc32c_class(ciInstanceKlass *crc32c_class); 317 bool inline_updateBytesCRC32C(); 318 bool inline_updateDirectByteBufferCRC32C(); 319 bool inline_updateBytesAdler32(); 320 bool inline_updateByteBufferAdler32(); 321 bool inline_multiplyToLen(); 322 bool inline_countPositives(); 323 bool inline_squareToLen(); 324 bool inline_mulAdd(); 325 bool inline_montgomeryMultiply(); 326 bool inline_montgomerySquare(); 327 bool inline_bigIntegerShift(bool isRightShift); 328 bool inline_vectorizedMismatch(); 329 bool inline_fma(vmIntrinsics::ID id); 330 bool inline_character_compare(vmIntrinsics::ID id); 331 bool inline_fp_min_max(vmIntrinsics::ID id); 332 bool inline_galoisCounterMode_AESCrypt(); 333 Node* inline_galoisCounterMode_AESCrypt_predicate(); 334 335 bool inline_profileBoolean(); 336 bool inline_isCompileConstant(); 337 338 bool inline_continuation_do_yield(); 339 340 // Vector API support 341 bool inline_vector_nary_operation(int n); 342 bool inline_vector_frombits_coerced(); 343 bool inline_vector_shuffle_to_vector(); 344 bool inline_vector_shuffle_iota(); 345 bool inline_vector_mask_operation(); 346 bool inline_vector_mem_operation(bool is_store); 347 bool inline_vector_mem_masked_operation(bool is_store); 348 bool inline_vector_gather_scatter(bool is_scatter); 349 bool inline_vector_reduction(); 350 bool inline_vector_test(); 351 bool inline_vector_blend(); 352 bool inline_vector_rearrange(); 353 bool inline_vector_compare(); 354 bool inline_vector_broadcast_int(); 355 bool inline_vector_convert(); 356 bool inline_vector_extract(); 357 bool inline_vector_insert(); 358 bool inline_vector_compress_expand(); 359 bool inline_index_vector(); 360 361 bool inline_timestamp(bool serial); 362 363 Node* gen_call_to_svml(int vector_api_op_id, BasicType bt, int num_elem, Node* opd1, Node* opd2); 364 365 enum VectorMaskUseType { 366 VecMaskUseLoad = 1 << 0, 367 VecMaskUseStore = 1 << 1, 368 VecMaskUseAll = VecMaskUseLoad | VecMaskUseStore, 369 VecMaskUsePred = 1 << 2, 370 VecMaskNotUsed = 1 << 3 371 }; 372 373 bool arch_supports_vector(int op, int num_elem, BasicType type, VectorMaskUseType mask_use_type, bool has_scalar_args = false); 374 bool arch_supports_vector_rotate(int opc, int num_elem, BasicType elem_bt, VectorMaskUseType mask_use_type, bool has_scalar_args = false); 375 376 void clear_upper_avx() { 377 #ifdef X86 378 if (UseAVX >= 2) { 379 C->set_clear_upper_avx(true); 380 } 381 #endif 382 } 383 384 bool inline_getObjectSize(); 385 386 bool inline_blackhole(); 387 388 bool inline_sizeOf(); 389 bool inline_sizeOf_impl(Node* obj); 390 391 bool inline_addressOf(); 392 393 }; 394