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