1 /*
  2  * Copyright (c) 2020, 2024, 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 "classfile/vmIntrinsics.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "compiler/compilerDirectives.hpp"
 29 #include "jvm_constants.h"
 30 #include "jvm_io.h"
 31 #include "runtime/vm_version.hpp"
 32 #include "utilities/checkedCast.hpp"
 33 #include "utilities/tribool.hpp"
 34 #include "utilities/xmlstream.hpp"
 35 
 36 // These are flag-matching functions:
 37 inline bool match_F_R(jshort flags) {
 38   const int req = 0;
 39   const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED | JVM_ACC_NATIVE;
 40   return (flags & (req | neg)) == req;
 41 }
 42 
 43 inline bool match_F_Y(jshort flags) {
 44   const int req = JVM_ACC_SYNCHRONIZED;
 45   const int neg = JVM_ACC_STATIC | JVM_ACC_NATIVE;
 46   return (flags & (req | neg)) == req;
 47 }
 48 
 49 inline bool match_F_RN(jshort flags) {
 50   const int req = JVM_ACC_NATIVE;
 51   const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED;
 52   return (flags & (req | neg)) == req;
 53 }
 54 
 55 inline bool match_F_S(jshort flags) {
 56   const int req = JVM_ACC_STATIC;
 57   const int neg = JVM_ACC_SYNCHRONIZED | JVM_ACC_NATIVE;
 58   return (flags & (req | neg)) == req;
 59 }
 60 
 61 inline bool match_F_SN(jshort flags) {
 62   const int req = JVM_ACC_STATIC | JVM_ACC_NATIVE;
 63   const int neg = JVM_ACC_SYNCHRONIZED;
 64   return (flags & (req | neg)) == req;
 65 }
 66 
 67 bool vmIntrinsics::preserves_state(vmIntrinsics::ID id) {
 68   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 69   switch(id) {
 70 #ifdef JFR_HAVE_INTRINSICS
 71   case vmIntrinsics::_counterTime:
 72 #endif
 73   case vmIntrinsics::_currentTimeMillis:
 74   case vmIntrinsics::_nanoTime:
 75   case vmIntrinsics::_floatToRawIntBits:
 76   case vmIntrinsics::_intBitsToFloat:
 77   case vmIntrinsics::_doubleToRawLongBits:
 78   case vmIntrinsics::_longBitsToDouble:
 79   case vmIntrinsics::_getClass:
 80   case vmIntrinsics::_isInstance:
 81   case vmIntrinsics::_currentCarrierThread:
 82   case vmIntrinsics::_currentThread:
 83   case vmIntrinsics::_scopedValueCache:
 84   case vmIntrinsics::_dabs:
 85   case vmIntrinsics::_fabs:
 86   case vmIntrinsics::_iabs:
 87   case vmIntrinsics::_labs:
 88   case vmIntrinsics::_dsqrt:
 89   case vmIntrinsics::_dsqrt_strict:
 90   case vmIntrinsics::_dsin:
 91   case vmIntrinsics::_dcos:
 92   case vmIntrinsics::_dtan:
 93   case vmIntrinsics::_dtanh:
 94   case vmIntrinsics::_dlog:
 95   case vmIntrinsics::_dlog10:
 96   case vmIntrinsics::_dexp:
 97   case vmIntrinsics::_dpow:
 98   case vmIntrinsics::_Preconditions_checkIndex:
 99   case vmIntrinsics::_Preconditions_checkLongIndex:
100   case vmIntrinsics::_Reference_get:
101   case vmIntrinsics::_Continuation_doYield:
102   case vmIntrinsics::_updateCRC32:
103   case vmIntrinsics::_updateBytesCRC32:
104   case vmIntrinsics::_updateByteBufferCRC32:
105   case vmIntrinsics::_updateBytesAdler32:
106   case vmIntrinsics::_vectorizedMismatch:
107   case vmIntrinsics::_fmaD:
108   case vmIntrinsics::_fmaF:
109   case vmIntrinsics::_isDigit:
110   case vmIntrinsics::_isLowerCase:
111   case vmIntrinsics::_isUpperCase:
112   case vmIntrinsics::_isWhitespace:
113     return true;
114   default:
115     return false;
116   }
117 }
118 
119 bool vmIntrinsics::can_trap(vmIntrinsics::ID id) {
120   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
121   switch(id) {
122 #ifdef JFR_HAVE_INTRINSICS
123   case vmIntrinsics::_counterTime:
124 #endif
125   case vmIntrinsics::_currentTimeMillis:
126   case vmIntrinsics::_nanoTime:
127   case vmIntrinsics::_floatToRawIntBits:
128   case vmIntrinsics::_intBitsToFloat:
129   case vmIntrinsics::_doubleToRawLongBits:
130   case vmIntrinsics::_longBitsToDouble:
131   case vmIntrinsics::_currentCarrierThread:
132   case vmIntrinsics::_currentThread:
133   case vmIntrinsics::_setCurrentThread:
134   case vmIntrinsics::_scopedValueCache:
135   case vmIntrinsics::_setScopedValueCache:
136   case vmIntrinsics::_dabs:
137   case vmIntrinsics::_fabs:
138   case vmIntrinsics::_iabs:
139   case vmIntrinsics::_labs:
140   case vmIntrinsics::_dsqrt:
141   case vmIntrinsics::_dsqrt_strict:
142   case vmIntrinsics::_dsin:
143   case vmIntrinsics::_dcos:
144   case vmIntrinsics::_dtan:
145   case vmIntrinsics::_dtanh:
146   case vmIntrinsics::_dlog:
147   case vmIntrinsics::_dlog10:
148   case vmIntrinsics::_dexp:
149   case vmIntrinsics::_dpow:
150   case vmIntrinsics::_updateCRC32:
151   case vmIntrinsics::_updateBytesCRC32:
152   case vmIntrinsics::_updateByteBufferCRC32:
153   case vmIntrinsics::_vectorizedMismatch:
154   case vmIntrinsics::_fmaD:
155   case vmIntrinsics::_fmaF:
156     return false;
157   default:
158     return true;
159   }
160 }
161 
162 // Some intrinsics produce different results if they are not pinned
163 bool vmIntrinsics::should_be_pinned(vmIntrinsics::ID id) {
164   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
165   switch(id) {
166 #ifdef JFR_HAVE_INTRINSICS
167   case vmIntrinsics::_counterTime:
168 #endif
169   case vmIntrinsics::_currentTimeMillis:
170   case vmIntrinsics::_nanoTime:
171   case vmIntrinsics::_blackhole:
172     return true;
173   default:
174     return false;
175   }
176 }
177 
178 bool vmIntrinsics::does_virtual_dispatch(vmIntrinsics::ID id) {
179   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
180   switch(id) {
181   case vmIntrinsics::_hashCode:
182   case vmIntrinsics::_clone:
183     return true;
184     break;
185   default:
186     return false;
187   }
188 }
189 
190 int vmIntrinsics::predicates_needed(vmIntrinsics::ID id) {
191   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
192   switch (id) {
193   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
194   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
195   case vmIntrinsics::_electronicCodeBook_encryptAESCrypt:
196   case vmIntrinsics::_electronicCodeBook_decryptAESCrypt:
197   case vmIntrinsics::_counterMode_AESCrypt:
198   case vmIntrinsics::_galoisCounterMode_AESCrypt:
199     return 1;
200   case vmIntrinsics::_digestBase_implCompressMB:
201     return 5;
202   default:
203     return 0;
204   }
205 }
206 
207 bool vmIntrinsics::disabled_by_jvm_flags(vmIntrinsics::ID id) {
208   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
209 
210   // -XX:-InlineNatives disables nearly all intrinsics except the ones listed in
211   // the following switch statement.
212   if (!InlineNatives) {
213     switch (id) {
214     case vmIntrinsics::_indexOfL:
215     case vmIntrinsics::_indexOfU:
216     case vmIntrinsics::_indexOfUL:
217     case vmIntrinsics::_indexOfIL:
218     case vmIntrinsics::_indexOfIU:
219     case vmIntrinsics::_indexOfIUL:
220     case vmIntrinsics::_indexOfU_char:
221     case vmIntrinsics::_indexOfL_char:
222     case vmIntrinsics::_compareToL:
223     case vmIntrinsics::_compareToU:
224     case vmIntrinsics::_compareToLU:
225     case vmIntrinsics::_compareToUL:
226     case vmIntrinsics::_equalsL:
227     case vmIntrinsics::_equalsC:
228     case vmIntrinsics::_vectorizedHashCode:
229     case vmIntrinsics::_getCharStringU:
230     case vmIntrinsics::_putCharStringU:
231     case vmIntrinsics::_compressStringC:
232     case vmIntrinsics::_compressStringB:
233     case vmIntrinsics::_inflateStringC:
234     case vmIntrinsics::_inflateStringB:
235     case vmIntrinsics::_getAndAddInt:
236     case vmIntrinsics::_getAndAddLong:
237     case vmIntrinsics::_getAndSetInt:
238     case vmIntrinsics::_getAndSetLong:
239     case vmIntrinsics::_getAndSetReference:
240     case vmIntrinsics::_loadFence:
241     case vmIntrinsics::_storeFence:
242     case vmIntrinsics::_fullFence:
243     case vmIntrinsics::_countPositives:
244     case vmIntrinsics::_Reference_get:
245     case vmIntrinsics::_Continuation_doYield:
246     case vmIntrinsics::_Continuation_enterSpecial:
247     case vmIntrinsics::_Continuation_pin:
248     case vmIntrinsics::_Continuation_unpin:
249       break;
250     default:
251       return true;
252     }
253   }
254 
255   switch (id) {
256   case vmIntrinsics::_isInstance:
257   case vmIntrinsics::_isAssignableFrom:
258   case vmIntrinsics::_getModifiers:
259   case vmIntrinsics::_isInterface:
260   case vmIntrinsics::_isArray:
261   case vmIntrinsics::_isPrimitive:
262   case vmIntrinsics::_isHidden:
263   case vmIntrinsics::_getSuperclass:
264   case vmIntrinsics::_Class_cast:
265   case vmIntrinsics::_getLength:
266   case vmIntrinsics::_newArray:
267   case vmIntrinsics::_getClass:
268     if (!InlineClassNatives) return true;
269     break;
270   case vmIntrinsics::_currentCarrierThread:
271   case vmIntrinsics::_currentThread:
272     if (!InlineThreadNatives) return true;
273     break;
274   case vmIntrinsics::_setCurrentThread:
275   case vmIntrinsics::_scopedValueCache:
276   case vmIntrinsics::_setScopedValueCache:
277   case vmIntrinsics::_floatToRawIntBits:
278   case vmIntrinsics::_intBitsToFloat:
279   case vmIntrinsics::_doubleToRawLongBits:
280   case vmIntrinsics::_longBitsToDouble:
281   case vmIntrinsics::_ceil:
282   case vmIntrinsics::_floor:
283   case vmIntrinsics::_rint:
284   case vmIntrinsics::_dabs:
285   case vmIntrinsics::_fabs:
286   case vmIntrinsics::_iabs:
287   case vmIntrinsics::_labs:
288   case vmIntrinsics::_dsqrt:
289   case vmIntrinsics::_dsqrt_strict:
290   case vmIntrinsics::_dsin:
291   case vmIntrinsics::_dcos:
292   case vmIntrinsics::_dtan:
293   case vmIntrinsics::_dtanh:
294   case vmIntrinsics::_dlog:
295   case vmIntrinsics::_dexp:
296   case vmIntrinsics::_dpow:
297   case vmIntrinsics::_dlog10:
298   case vmIntrinsics::_datan2:
299   case vmIntrinsics::_floatToIntBits:
300   case vmIntrinsics::_doubleToLongBits:
301   case vmIntrinsics::_min:
302   case vmIntrinsics::_max:
303   case vmIntrinsics::_maxF:
304   case vmIntrinsics::_minF:
305   case vmIntrinsics::_maxD:
306   case vmIntrinsics::_minD:
307   case vmIntrinsics::_min_strict:
308   case vmIntrinsics::_max_strict:
309   case vmIntrinsics::_maxF_strict:
310   case vmIntrinsics::_minF_strict:
311   case vmIntrinsics::_maxD_strict:
312   case vmIntrinsics::_minD_strict:
313     if (!InlineMathNatives) return true;
314     break;
315   case vmIntrinsics::_fmaD:
316   case vmIntrinsics::_fmaF:
317     if (!InlineMathNatives || !UseFMA) return true;
318     break;
319   case vmIntrinsics::_floatToFloat16:
320   case vmIntrinsics::_float16ToFloat:
321     if (!InlineIntrinsics) return true;
322     break;
323   case vmIntrinsics::_arraycopy:
324     if (!InlineArrayCopy) return true;
325     break;
326   case vmIntrinsics::_updateCRC32:
327   case vmIntrinsics::_updateBytesCRC32:
328   case vmIntrinsics::_updateByteBufferCRC32:
329     if (!UseCRC32Intrinsics) return true;
330     break;
331   case vmIntrinsics::_getReference:
332   case vmIntrinsics::_getBoolean:
333   case vmIntrinsics::_getByte:
334   case vmIntrinsics::_getShort:
335   case vmIntrinsics::_getChar:
336   case vmIntrinsics::_getInt:
337   case vmIntrinsics::_getLong:
338   case vmIntrinsics::_getFloat:
339   case vmIntrinsics::_getDouble:
340   case vmIntrinsics::_putReference:
341   case vmIntrinsics::_putBoolean:
342   case vmIntrinsics::_putByte:
343   case vmIntrinsics::_putShort:
344   case vmIntrinsics::_putChar:
345   case vmIntrinsics::_putInt:
346   case vmIntrinsics::_putLong:
347   case vmIntrinsics::_putFloat:
348   case vmIntrinsics::_putDouble:
349   case vmIntrinsics::_getReferenceVolatile:
350   case vmIntrinsics::_getBooleanVolatile:
351   case vmIntrinsics::_getByteVolatile:
352   case vmIntrinsics::_getShortVolatile:
353   case vmIntrinsics::_getCharVolatile:
354   case vmIntrinsics::_getIntVolatile:
355   case vmIntrinsics::_getLongVolatile:
356   case vmIntrinsics::_getFloatVolatile:
357   case vmIntrinsics::_getDoubleVolatile:
358   case vmIntrinsics::_putReferenceVolatile:
359   case vmIntrinsics::_putBooleanVolatile:
360   case vmIntrinsics::_putByteVolatile:
361   case vmIntrinsics::_putShortVolatile:
362   case vmIntrinsics::_putCharVolatile:
363   case vmIntrinsics::_putIntVolatile:
364   case vmIntrinsics::_putLongVolatile:
365   case vmIntrinsics::_putFloatVolatile:
366   case vmIntrinsics::_putDoubleVolatile:
367   case vmIntrinsics::_getReferenceAcquire:
368   case vmIntrinsics::_getBooleanAcquire:
369   case vmIntrinsics::_getByteAcquire:
370   case vmIntrinsics::_getShortAcquire:
371   case vmIntrinsics::_getCharAcquire:
372   case vmIntrinsics::_getIntAcquire:
373   case vmIntrinsics::_getLongAcquire:
374   case vmIntrinsics::_getFloatAcquire:
375   case vmIntrinsics::_getDoubleAcquire:
376   case vmIntrinsics::_putReferenceRelease:
377   case vmIntrinsics::_putBooleanRelease:
378   case vmIntrinsics::_putByteRelease:
379   case vmIntrinsics::_putShortRelease:
380   case vmIntrinsics::_putCharRelease:
381   case vmIntrinsics::_putIntRelease:
382   case vmIntrinsics::_putLongRelease:
383   case vmIntrinsics::_putFloatRelease:
384   case vmIntrinsics::_putDoubleRelease:
385   case vmIntrinsics::_getReferenceOpaque:
386   case vmIntrinsics::_getBooleanOpaque:
387   case vmIntrinsics::_getByteOpaque:
388   case vmIntrinsics::_getShortOpaque:
389   case vmIntrinsics::_getCharOpaque:
390   case vmIntrinsics::_getIntOpaque:
391   case vmIntrinsics::_getLongOpaque:
392   case vmIntrinsics::_getFloatOpaque:
393   case vmIntrinsics::_getDoubleOpaque:
394   case vmIntrinsics::_putReferenceOpaque:
395   case vmIntrinsics::_putBooleanOpaque:
396   case vmIntrinsics::_putByteOpaque:
397   case vmIntrinsics::_putShortOpaque:
398   case vmIntrinsics::_putCharOpaque:
399   case vmIntrinsics::_putIntOpaque:
400   case vmIntrinsics::_putLongOpaque:
401   case vmIntrinsics::_putFloatOpaque:
402   case vmIntrinsics::_putDoubleOpaque:
403   case vmIntrinsics::_getAndAddInt:
404   case vmIntrinsics::_getAndAddLong:
405   case vmIntrinsics::_getAndSetInt:
406   case vmIntrinsics::_getAndSetLong:
407   case vmIntrinsics::_getAndSetReference:
408   case vmIntrinsics::_loadFence:
409   case vmIntrinsics::_storeFence:
410   case vmIntrinsics::_fullFence:
411   case vmIntrinsics::_compareAndSetLong:
412   case vmIntrinsics::_weakCompareAndSetLong:
413   case vmIntrinsics::_weakCompareAndSetLongPlain:
414   case vmIntrinsics::_weakCompareAndSetLongAcquire:
415   case vmIntrinsics::_weakCompareAndSetLongRelease:
416   case vmIntrinsics::_compareAndSetInt:
417   case vmIntrinsics::_weakCompareAndSetInt:
418   case vmIntrinsics::_weakCompareAndSetIntPlain:
419   case vmIntrinsics::_weakCompareAndSetIntAcquire:
420   case vmIntrinsics::_weakCompareAndSetIntRelease:
421   case vmIntrinsics::_compareAndSetReference:
422   case vmIntrinsics::_weakCompareAndSetReference:
423   case vmIntrinsics::_weakCompareAndSetReferencePlain:
424   case vmIntrinsics::_weakCompareAndSetReferenceAcquire:
425   case vmIntrinsics::_weakCompareAndSetReferenceRelease:
426   case vmIntrinsics::_compareAndExchangeInt:
427   case vmIntrinsics::_compareAndExchangeIntAcquire:
428   case vmIntrinsics::_compareAndExchangeIntRelease:
429   case vmIntrinsics::_compareAndExchangeLong:
430   case vmIntrinsics::_compareAndExchangeLongAcquire:
431   case vmIntrinsics::_compareAndExchangeLongRelease:
432   case vmIntrinsics::_compareAndExchangeReference:
433   case vmIntrinsics::_compareAndExchangeReferenceAcquire:
434   case vmIntrinsics::_compareAndExchangeReferenceRelease:
435   case vmIntrinsics::_allocateInstance:
436     if (!InlineUnsafeOps) return true;
437     break;
438   case vmIntrinsics::_getShortUnaligned:
439   case vmIntrinsics::_getCharUnaligned:
440   case vmIntrinsics::_getIntUnaligned:
441   case vmIntrinsics::_getLongUnaligned:
442   case vmIntrinsics::_putShortUnaligned:
443   case vmIntrinsics::_putCharUnaligned:
444   case vmIntrinsics::_putIntUnaligned:
445   case vmIntrinsics::_putLongUnaligned:
446     if (!InlineUnsafeOps || !UseUnalignedAccesses) return true;
447     break;
448   case vmIntrinsics::_hashCode:
449     if (!InlineObjectHash) return true;
450     break;
451   case vmIntrinsics::_aescrypt_encryptBlock:
452   case vmIntrinsics::_aescrypt_decryptBlock:
453     if (!UseAESIntrinsics) return true;
454     break;
455   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
456   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
457     if (!UseAESIntrinsics) return true;
458     break;
459   case vmIntrinsics::_electronicCodeBook_encryptAESCrypt:
460   case vmIntrinsics::_electronicCodeBook_decryptAESCrypt:
461     if (!UseAESIntrinsics) return true;
462     break;
463   case vmIntrinsics::_counterMode_AESCrypt:
464     if (!UseAESCTRIntrinsics) return true;
465     break;
466   case vmIntrinsics::_galoisCounterMode_AESCrypt:
467     if (!UseAESIntrinsics) return true;
468     break;
469   case vmIntrinsics::_md5_implCompress:
470     if (!UseMD5Intrinsics) return true;
471     break;
472   case vmIntrinsics::_sha_implCompress:
473     if (!UseSHA1Intrinsics) return true;
474     break;
475   case vmIntrinsics::_sha2_implCompress:
476     if (!UseSHA256Intrinsics) return true;
477     break;
478   case vmIntrinsics::_sha5_implCompress:
479     if (!UseSHA512Intrinsics) return true;
480     break;
481   case vmIntrinsics::_sha3_implCompress:
482     if (!UseSHA3Intrinsics) return true;
483     break;
484   case vmIntrinsics::_digestBase_implCompressMB:
485     if (!(UseMD5Intrinsics || UseSHA1Intrinsics || UseSHA256Intrinsics || UseSHA512Intrinsics || UseSHA3Intrinsics)) return true;
486     break;
487   case vmIntrinsics::_ghash_processBlocks:
488     if (!UseGHASHIntrinsics) return true;
489     break;
490   case vmIntrinsics::_chacha20Block:
491     if (!UseChaCha20Intrinsics) return true;
492     break;
493   case vmIntrinsics::_base64_encodeBlock:
494   case vmIntrinsics::_base64_decodeBlock:
495     if (!UseBASE64Intrinsics) return true;
496     break;
497   case vmIntrinsics::_poly1305_processBlocks:
498     if (!UsePoly1305Intrinsics) return true;
499     break;
500   case vmIntrinsics::_intpoly_montgomeryMult_P256:
501   case vmIntrinsics::_intpoly_assign:
502     if (!UseIntPolyIntrinsics) return true;
503     break;
504   case vmIntrinsics::_updateBytesCRC32C:
505   case vmIntrinsics::_updateDirectByteBufferCRC32C:
506     if (!UseCRC32CIntrinsics) return true;
507     break;
508   case vmIntrinsics::_vectorizedMismatch:
509     if (!UseVectorizedMismatchIntrinsic) return true;
510     break;
511   case vmIntrinsics::_updateBytesAdler32:
512   case vmIntrinsics::_updateByteBufferAdler32:
513     if (!UseAdler32Intrinsics) return true;
514     break;
515   case vmIntrinsics::_copyMemory:
516     if (!InlineArrayCopy || !InlineUnsafeOps) return true;
517     break;
518   case vmIntrinsics::_setMemory:
519     if (!InlineUnsafeOps) return true;
520     break;
521 #ifdef COMPILER2
522   case vmIntrinsics::_clone:
523   case vmIntrinsics::_copyOf:
524   case vmIntrinsics::_copyOfRange:
525     // These intrinsics use both the objectcopy and the arraycopy
526     // intrinsic mechanism.
527     if (!InlineObjectCopy || !InlineArrayCopy) return true;
528     break;
529   case vmIntrinsics::_compareToL:
530   case vmIntrinsics::_compareToU:
531   case vmIntrinsics::_compareToLU:
532   case vmIntrinsics::_compareToUL:
533     if (!SpecialStringCompareTo) return true;
534     break;
535   case vmIntrinsics::_indexOfL:
536   case vmIntrinsics::_indexOfU:
537   case vmIntrinsics::_indexOfUL:
538   case vmIntrinsics::_indexOfIL:
539   case vmIntrinsics::_indexOfIU:
540   case vmIntrinsics::_indexOfIUL:
541   case vmIntrinsics::_indexOfU_char:
542   case vmIntrinsics::_indexOfL_char:
543     if (!SpecialStringIndexOf) return true;
544     break;
545   case vmIntrinsics::_equalsL:
546     if (!SpecialStringEquals) return true;
547     break;
548   case vmIntrinsics::_vectorizedHashCode:
549     if (!UseVectorizedHashCodeIntrinsic) return true;
550     break;
551   case vmIntrinsics::_equalsB:
552   case vmIntrinsics::_equalsC:
553     if (!SpecialArraysEquals) return true;
554     break;
555   case vmIntrinsics::_encodeISOArray:
556   case vmIntrinsics::_encodeAsciiArray:
557   case vmIntrinsics::_encodeByteISOArray:
558     if (!SpecialEncodeISOArray) return true;
559     break;
560   case vmIntrinsics::_getCallerClass:
561     if (!InlineReflectionGetCallerClass) return true;
562     break;
563   case vmIntrinsics::_multiplyToLen:
564     if (!UseMultiplyToLenIntrinsic) return true;
565     break;
566   case vmIntrinsics::_squareToLen:
567     if (!UseSquareToLenIntrinsic) return true;
568     break;
569   case vmIntrinsics::_mulAdd:
570     if (!UseMulAddIntrinsic) return true;
571     break;
572   case vmIntrinsics::_montgomeryMultiply:
573     if (!UseMontgomeryMultiplyIntrinsic) return true;
574     break;
575   case vmIntrinsics::_montgomerySquare:
576     if (!UseMontgomerySquareIntrinsic) return true;
577     break;
578   case vmIntrinsics::_bigIntegerRightShiftWorker:
579   case vmIntrinsics::_bigIntegerLeftShiftWorker:
580     break;
581   case vmIntrinsics::_addExactI:
582   case vmIntrinsics::_addExactL:
583   case vmIntrinsics::_decrementExactI:
584   case vmIntrinsics::_decrementExactL:
585   case vmIntrinsics::_incrementExactI:
586   case vmIntrinsics::_incrementExactL:
587   case vmIntrinsics::_multiplyExactI:
588   case vmIntrinsics::_multiplyExactL:
589   case vmIntrinsics::_negateExactI:
590   case vmIntrinsics::_negateExactL:
591   case vmIntrinsics::_subtractExactI:
592   case vmIntrinsics::_subtractExactL:
593     if (!UseMathExactIntrinsics || !InlineMathNatives) return true;
594     break;
595   case vmIntrinsics::_isDigit:
596   case vmIntrinsics::_isLowerCase:
597   case vmIntrinsics::_isUpperCase:
598   case vmIntrinsics::_isWhitespace:
599     if (!UseCharacterCompareIntrinsics) return true;
600     break;
601   case vmIntrinsics::_dcopySign:
602   case vmIntrinsics::_fcopySign:
603     if (!InlineMathNatives || !UseCopySignIntrinsic) return true;
604     break;
605   case vmIntrinsics::_dsignum:
606   case vmIntrinsics::_fsignum:
607     if (!InlineMathNatives || !UseSignumIntrinsic) return true;
608     break;
609 #endif // COMPILER2
610   default:
611     return false;
612   }
613 
614   return false;
615 }
616 
617 #define VM_INTRINSIC_INITIALIZE(id, klass, name, sig, flags) #id "\0"
618 static const char* vm_intrinsic_name_bodies =
619   VM_INTRINSICS_DO(VM_INTRINSIC_INITIALIZE,
620                    VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
621 
622 static const char* vm_intrinsic_name_table[vmIntrinsics::number_of_intrinsics()];
623 static TriBoolArray<(size_t)vmIntrinsics::number_of_intrinsics(), int> vm_intrinsic_control_words;
624 
625 void vmIntrinsics::init_vm_intrinsic_name_table() {
626   const char** nt = &vm_intrinsic_name_table[0];
627   char* string = (char*) &vm_intrinsic_name_bodies[0];
628 
629   for (auto index : EnumRange<vmIntrinsicID>{}) {
630     nt[as_int(index)] = string;
631     string += strlen(string); // skip string body
632     string += 1;              // skip trailing null
633   }
634   assert(!strcmp(nt[as_int(vmIntrinsics::_hashCode)], "_hashCode"), "lined up");
635   nt[as_int(vmIntrinsics::_none)] = "_none";
636 }
637 
638 const char* vmIntrinsics::name_at(vmIntrinsics::ID id) {
639   const char** nt = &vm_intrinsic_name_table[0];
640   if (nt[as_int(_none)] == nullptr) {
641     init_vm_intrinsic_name_table();
642   }
643 
644   if (id < ID_LIMIT)
645     return vm_intrinsic_name_table[as_int(id)];
646   else
647     return "(unknown intrinsic)";
648 }
649 
650 vmIntrinsics::ID vmIntrinsics::find_id(const char* name) {
651   const char** nt = &vm_intrinsic_name_table[0];
652   if (nt[as_int(_none)] == nullptr) {
653     init_vm_intrinsic_name_table();
654   }
655 
656   for (auto index : EnumRange<vmIntrinsicID>{}) {
657     if (0 == strcmp(name, nt[as_int(index)])) {
658       return index;
659     }
660   }
661 
662   return _none;
663 }
664 
665 bool vmIntrinsics::is_intrinsic_available(vmIntrinsics::ID id) {
666   return VM_Version::is_intrinsic_supported(id) && !is_disabled_by_flags(id);
667 }
668 
669 bool vmIntrinsics::is_disabled_by_flags(vmIntrinsics::ID id) {
670   assert(id > _none && id < ID_LIMIT, "must be a VM intrinsic");
671 
672   // not initialized yet, process Control/DisableIntrinsic
673   if (vm_intrinsic_control_words[as_int(_none)].is_default()) {
674     for (ControlIntrinsicIter iter(ControlIntrinsic); *iter != nullptr; ++iter) {
675       vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
676 
677       if (id != vmIntrinsics::_none) {
678         vm_intrinsic_control_words[as_int(id)] = iter.is_enabled() && !disabled_by_jvm_flags(id);
679       }
680     }
681 
682     // Order matters, DisableIntrinsic can overwrite ControlIntrinsic
683     for (ControlIntrinsicIter iter(DisableIntrinsic, true/*disable_all*/); *iter != nullptr; ++iter) {
684       vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
685 
686       if (id != vmIntrinsics::_none) {
687         vm_intrinsic_control_words[as_int(id)] = false;
688       }
689     }
690 
691     vm_intrinsic_control_words[as_int(_none)] = true;
692   }
693 
694   TriBool b = vm_intrinsic_control_words[as_int(id)];
695   if (b.is_default()) {
696     // unknown yet, query and cache it
697     b = vm_intrinsic_control_words[as_int(id)] = !disabled_by_jvm_flags(id);
698   }
699 
700   return !b;
701 }
702 
703 // These are for forming case labels:
704 #define ID3(x, y, z) (( jlong)(z) +                                  \
705                       ((jlong)(y) <<    vmSymbols::log2_SID_LIMIT) + \
706                       ((jlong)(x) << (2*vmSymbols::log2_SID_LIMIT))  )
707 #define SID_ENUM(n) VM_SYMBOL_ENUM_NAME(n)
708 
709 vmIntrinsics::ID vmIntrinsics::find_id_impl(vmSymbolID holder,
710                                             vmSymbolID name,
711                                             vmSymbolID sig,
712                                             jshort flags) {
713   assert((int)vmSymbolID::SID_LIMIT <= (1<<vmSymbols::log2_SID_LIMIT), "must fit");
714 
715   // Let the C compiler build the decision tree.
716 
717 #define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
718   case ID3(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig)): \
719     if (!match_##fcode(flags))  break; \
720     return id;
721 
722   switch (ID3(holder, name, sig)) {
723     VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
724                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
725   }
726   return vmIntrinsics::_none;
727 
728 #undef VM_INTRINSIC_CASE
729 }
730 
731 class vmIntrinsicsLookup {
732   bool _class_map[vmSymbols::number_of_symbols()];
733 
734   constexpr int as_index(vmSymbolID id) const {
735     int index = vmSymbols::as_int(id);
736     assert(0 <= index && index < int(sizeof(_class_map)), "must be");
737     return index;
738   }
739 
740   constexpr void set_class_map(vmSymbolID id) {
741     _class_map[as_index(id)] = true;
742   }
743 
744 public:
745   constexpr vmIntrinsicsLookup() : _class_map() {
746 
747 #define VM_INTRINSIC_CLASS_MAP(id, klass, name, sig, fcode) \
748     set_class_map(SID_ENUM(klass));
749 
750     VM_INTRINSICS_DO(VM_INTRINSIC_CLASS_MAP,
751                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
752 #undef VM_INTRINSIC_CLASS_MAP
753 
754 
755     // A few slightly irregular cases. See Method::init_intrinsic_id
756     set_class_map(SID_ENUM(java_lang_StrictMath));
757     set_class_map(SID_ENUM(java_lang_invoke_MethodHandle));
758     set_class_map(SID_ENUM(java_lang_invoke_VarHandle));
759   }
760 
761   bool class_has_intrinsics(vmSymbolID holder) const {
762     return _class_map[as_index(holder)];
763   }
764 };
765 
766 constexpr vmIntrinsicsLookup _intrinsics_lookup;
767 
768 bool vmIntrinsics::class_has_intrinsics(vmSymbolID holder) {
769   return _intrinsics_lookup.class_has_intrinsics(holder);
770 }
771 
772 const char* vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID id, char* buf, int buflen) {
773   const char* str = name_at(id);
774 #ifndef PRODUCT
775   const char* kname = vmSymbols::name_for(class_for(id));
776   const char* mname = vmSymbols::name_for(name_for(id));
777   const char* sname = vmSymbols::name_for(signature_for(id));
778   const char* fname = "";
779   switch (flags_for(id)) {
780   case F_Y:  fname = "synchronized ";  break;
781   case F_RN: fname = "native ";        break;
782   case F_SN: fname = "native static "; break;
783   case F_S:  fname = "static ";        break;
784   default:   break;
785   }
786   const char* kptr = strrchr(kname, JVM_SIGNATURE_SLASH);
787   if (kptr != nullptr)  kname = kptr + 1;
788   int len = jio_snprintf(buf, buflen, "%s: %s%s.%s%s",
789                          str, fname, kname, mname, sname);
790   if (len < buflen)
791     str = buf;
792 #endif //PRODUCT
793   return str;
794 }
795 
796 
797 // These are to get information about intrinsics.
798 
799 #define ID4(x, y, z, f) ((ID3(x, y, z) << vmIntrinsics::log2_FLAG_LIMIT) | (jlong) (f))
800 
801 #ifndef PRODUCT
802 static const jlong intrinsic_info_array[vmIntrinsics::number_of_intrinsics()+1] = {
803 #define VM_INTRINSIC_INFO(ignore_id, klass, name, sig, fcode) \
804   ID4(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig), vmIntrinsics::fcode),
805 
806   0, VM_INTRINSICS_DO(VM_INTRINSIC_INFO,
807                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE)
808   0
809 #undef VM_INTRINSIC_INFO
810 };
811 
812 inline jlong intrinsic_info(vmIntrinsics::ID id) {
813   return intrinsic_info_array[vmIntrinsics::as_int(id)];
814 }
815 
816 vmSymbolID vmIntrinsics::class_for(vmIntrinsics::ID id) {
817   jlong info = intrinsic_info(id);
818   int shift = 2*vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
819   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1021, "");
820   return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
821 }
822 
823 vmSymbolID vmIntrinsics::name_for(vmIntrinsics::ID id) {
824   jlong info = intrinsic_info(id);
825   int shift = vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
826   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1022, "");
827   return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
828 }
829 
830 vmSymbolID vmIntrinsics::signature_for(vmIntrinsics::ID id) {
831   jlong info = intrinsic_info(id);
832   int shift = log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
833   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1023, "");
834   return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
835 }
836 
837 vmIntrinsics::Flags vmIntrinsics::flags_for(vmIntrinsics::ID id) {
838   jlong info = intrinsic_info(id);
839   int shift = 0, mask = right_n_bits(log2_FLAG_LIMIT);
840   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 7, "");
841   return Flags( (info >> shift) & mask );
842 }
843 #endif // !PRODUCT