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