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