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