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::_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::_intpoly_montgomeryMult_P256:
496   case vmIntrinsics::_intpoly_assign:
497     if (!UseIntPolyIntrinsics) return true;
498     break;
499   case vmIntrinsics::_updateBytesCRC32C:
500   case vmIntrinsics::_updateDirectByteBufferCRC32C:
501     if (!UseCRC32CIntrinsics) return true;
502     break;
503   case vmIntrinsics::_vectorizedMismatch:
504     if (!UseVectorizedMismatchIntrinsic) return true;
505     break;
506   case vmIntrinsics::_updateBytesAdler32:
507   case vmIntrinsics::_updateByteBufferAdler32:
508     if (!UseAdler32Intrinsics) return true;
509     break;
510   case vmIntrinsics::_copyMemory:
511     if (!InlineArrayCopy || !InlineUnsafeOps) return true;
512     break;
513   case vmIntrinsics::_setMemory:
514     if (!InlineUnsafeOps) return true;
515     break;
516 #ifdef COMPILER2
517   case vmIntrinsics::_clone:
518   case vmIntrinsics::_copyOf:
519   case vmIntrinsics::_copyOfRange:
520     // These intrinsics use both the objectcopy and the arraycopy
521     // intrinsic mechanism.
522     if (!InlineObjectCopy || !InlineArrayCopy) return true;
523     break;
524   case vmIntrinsics::_compareToL:
525   case vmIntrinsics::_compareToU:
526   case vmIntrinsics::_compareToLU:
527   case vmIntrinsics::_compareToUL:
528     if (!SpecialStringCompareTo) return true;
529     break;
530   case vmIntrinsics::_indexOfL:
531   case vmIntrinsics::_indexOfU:
532   case vmIntrinsics::_indexOfUL:
533   case vmIntrinsics::_indexOfIL:
534   case vmIntrinsics::_indexOfIU:
535   case vmIntrinsics::_indexOfIUL:
536   case vmIntrinsics::_indexOfU_char:
537   case vmIntrinsics::_indexOfL_char:
538     if (!SpecialStringIndexOf) return true;
539     break;
540   case vmIntrinsics::_equalsL:
541     if (!SpecialStringEquals) return true;
542     break;
543   case vmIntrinsics::_vectorizedHashCode:
544     if (!UseVectorizedHashCodeIntrinsic) return true;
545     break;
546   case vmIntrinsics::_equalsB:
547   case vmIntrinsics::_equalsC:
548     if (!SpecialArraysEquals) return true;
549     break;
550   case vmIntrinsics::_encodeISOArray:
551   case vmIntrinsics::_encodeAsciiArray:
552   case vmIntrinsics::_encodeByteISOArray:
553     if (!SpecialEncodeISOArray) return true;
554     break;
555   case vmIntrinsics::_getCallerClass:
556     if (!InlineReflectionGetCallerClass) return true;
557     break;
558   case vmIntrinsics::_multiplyToLen:
559     if (!UseMultiplyToLenIntrinsic) return true;
560     break;
561   case vmIntrinsics::_squareToLen:
562     if (!UseSquareToLenIntrinsic) return true;
563     break;
564   case vmIntrinsics::_mulAdd:
565     if (!UseMulAddIntrinsic) return true;
566     break;
567   case vmIntrinsics::_montgomeryMultiply:
568     if (!UseMontgomeryMultiplyIntrinsic) return true;
569     break;
570   case vmIntrinsics::_montgomerySquare:
571     if (!UseMontgomerySquareIntrinsic) return true;
572     break;
573   case vmIntrinsics::_bigIntegerRightShiftWorker:
574   case vmIntrinsics::_bigIntegerLeftShiftWorker:
575     break;
576   case vmIntrinsics::_addExactI:
577   case vmIntrinsics::_addExactL:
578   case vmIntrinsics::_decrementExactI:
579   case vmIntrinsics::_decrementExactL:
580   case vmIntrinsics::_incrementExactI:
581   case vmIntrinsics::_incrementExactL:
582   case vmIntrinsics::_multiplyExactI:
583   case vmIntrinsics::_multiplyExactL:
584   case vmIntrinsics::_negateExactI:
585   case vmIntrinsics::_negateExactL:
586   case vmIntrinsics::_subtractExactI:
587   case vmIntrinsics::_subtractExactL:
588     if (!UseMathExactIntrinsics || !InlineMathNatives) return true;
589     break;
590   case vmIntrinsics::_isDigit:
591   case vmIntrinsics::_isLowerCase:
592   case vmIntrinsics::_isUpperCase:
593   case vmIntrinsics::_isWhitespace:
594     if (!UseCharacterCompareIntrinsics) return true;
595     break;
596   case vmIntrinsics::_dcopySign:
597   case vmIntrinsics::_fcopySign:
598     if (!InlineMathNatives || !UseCopySignIntrinsic) return true;
599     break;
600   case vmIntrinsics::_dsignum:
601   case vmIntrinsics::_fsignum:
602     if (!InlineMathNatives || !UseSignumIntrinsic) return true;
603     break;
604 #endif // COMPILER2
605   default:
606     return false;
607   }
608 
609   return false;
610 }
611 
612 #define VM_INTRINSIC_INITIALIZE(id, klass, name, sig, flags) #id "\0"
613 static const char* vm_intrinsic_name_bodies =
614   VM_INTRINSICS_DO(VM_INTRINSIC_INITIALIZE,
615                    VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
616 
617 static const char* vm_intrinsic_name_table[vmIntrinsics::number_of_intrinsics()];
618 static TriBoolArray<(size_t)vmIntrinsics::number_of_intrinsics(), int> vm_intrinsic_control_words;
619 
620 void vmIntrinsics::init_vm_intrinsic_name_table() {
621   const char** nt = &vm_intrinsic_name_table[0];
622   char* string = (char*) &vm_intrinsic_name_bodies[0];
623 
624   for (auto index : EnumRange<vmIntrinsicID>{}) {
625     nt[as_int(index)] = string;
626     string += strlen(string); // skip string body
627     string += 1;              // skip trailing null
628   }
629   assert(!strcmp(nt[as_int(vmIntrinsics::_hashCode)], "_hashCode"), "lined up");
630   nt[as_int(vmIntrinsics::_none)] = "_none";
631 }
632 
633 const char* vmIntrinsics::name_at(vmIntrinsics::ID id) {
634   const char** nt = &vm_intrinsic_name_table[0];
635   if (nt[as_int(_none)] == nullptr) {
636     init_vm_intrinsic_name_table();
637   }
638 
639   if (id < ID_LIMIT)
640     return vm_intrinsic_name_table[as_int(id)];
641   else
642     return "(unknown intrinsic)";
643 }
644 
645 vmIntrinsics::ID vmIntrinsics::find_id(const char* name) {
646   const char** nt = &vm_intrinsic_name_table[0];
647   if (nt[as_int(_none)] == nullptr) {
648     init_vm_intrinsic_name_table();
649   }
650 
651   for (auto index : EnumRange<vmIntrinsicID>{}) {
652     if (0 == strcmp(name, nt[as_int(index)])) {
653       return index;
654     }
655   }
656 
657   return _none;
658 }
659 
660 bool vmIntrinsics::is_intrinsic_available(vmIntrinsics::ID id) {
661   return VM_Version::is_intrinsic_supported(id) && !is_disabled_by_flags(id);
662 }
663 
664 bool vmIntrinsics::is_disabled_by_flags(vmIntrinsics::ID id) {
665   assert(id > _none && id < ID_LIMIT, "must be a VM intrinsic");
666 
667   // not initialized yet, process Control/DisableIntrinsic
668   if (vm_intrinsic_control_words[as_int(_none)].is_default()) {
669     for (ControlIntrinsicIter iter(ControlIntrinsic); *iter != nullptr; ++iter) {
670       vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
671 
672       if (id != vmIntrinsics::_none) {
673         vm_intrinsic_control_words[as_int(id)] = iter.is_enabled() && !disabled_by_jvm_flags(id);
674       }
675     }
676 
677     // Order matters, DisableIntrinsic can overwrite ControlIntrinsic
678     for (ControlIntrinsicIter iter(DisableIntrinsic, true/*disable_all*/); *iter != nullptr; ++iter) {
679       vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
680 
681       if (id != vmIntrinsics::_none) {
682         vm_intrinsic_control_words[as_int(id)] = false;
683       }
684     }
685 
686     vm_intrinsic_control_words[as_int(_none)] = true;
687   }
688 
689   TriBool b = vm_intrinsic_control_words[as_int(id)];
690   if (b.is_default()) {
691     // unknown yet, query and cache it
692     b = vm_intrinsic_control_words[as_int(id)] = !disabled_by_jvm_flags(id);
693   }
694 
695   return !b;
696 }
697 
698 // These are for forming case labels:
699 #define ID3(x, y, z) (( jlong)(z) +                                  \
700                       ((jlong)(y) <<    vmSymbols::log2_SID_LIMIT) + \
701                       ((jlong)(x) << (2*vmSymbols::log2_SID_LIMIT))  )
702 #define SID_ENUM(n) VM_SYMBOL_ENUM_NAME(n)
703 
704 vmIntrinsics::ID vmIntrinsics::find_id_impl(vmSymbolID holder,
705                                             vmSymbolID name,
706                                             vmSymbolID sig,
707                                             jshort flags) {
708   assert((int)vmSymbolID::SID_LIMIT <= (1<<vmSymbols::log2_SID_LIMIT), "must fit");
709 
710   // Let the C compiler build the decision tree.
711 
712 #define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
713   case ID3(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig)): \
714     if (!match_##fcode(flags))  break; \
715     return id;
716 
717   switch (ID3(holder, name, sig)) {
718     VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
719                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
720   }
721   return vmIntrinsics::_none;
722 
723 #undef VM_INTRINSIC_CASE
724 }
725 
726 class vmIntrinsicsLookup {
727   bool _class_map[vmSymbols::number_of_symbols()];
728 
729   constexpr int as_index(vmSymbolID id) const {
730     int index = vmSymbols::as_int(id);
731     assert(0 <= index && index < int(sizeof(_class_map)), "must be");
732     return index;
733   }
734 
735   constexpr void set_class_map(vmSymbolID id) {
736     _class_map[as_index(id)] = true;
737   }
738 
739 public:
740   constexpr vmIntrinsicsLookup() : _class_map() {
741 
742 #define VM_INTRINSIC_CLASS_MAP(id, klass, name, sig, fcode) \
743     set_class_map(SID_ENUM(klass));
744 
745     VM_INTRINSICS_DO(VM_INTRINSIC_CLASS_MAP,
746                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
747 #undef VM_INTRINSIC_CLASS_MAP
748 
749 
750     // A few slightly irregular cases. See Method::init_intrinsic_id
751     set_class_map(SID_ENUM(java_lang_StrictMath));
752     set_class_map(SID_ENUM(java_lang_invoke_MethodHandle));
753     set_class_map(SID_ENUM(java_lang_invoke_VarHandle));
754   }
755 
756   bool class_has_intrinsics(vmSymbolID holder) const {
757     return _class_map[as_index(holder)];
758   }
759 };
760 
761 constexpr vmIntrinsicsLookup _intrinsics_lookup;
762 
763 bool vmIntrinsics::class_has_intrinsics(vmSymbolID holder) {
764   return _intrinsics_lookup.class_has_intrinsics(holder);
765 }
766 
767 const char* vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID id, char* buf, int buflen) {
768   const char* str = name_at(id);
769 #ifndef PRODUCT
770   const char* kname = vmSymbols::name_for(class_for(id));
771   const char* mname = vmSymbols::name_for(name_for(id));
772   const char* sname = vmSymbols::name_for(signature_for(id));
773   const char* fname = "";
774   switch (flags_for(id)) {
775   case F_Y:  fname = "synchronized ";  break;
776   case F_RN: fname = "native ";        break;
777   case F_SN: fname = "native static "; break;
778   case F_S:  fname = "static ";        break;
779   default:   break;
780   }
781   const char* kptr = strrchr(kname, JVM_SIGNATURE_SLASH);
782   if (kptr != nullptr)  kname = kptr + 1;
783   int len = jio_snprintf(buf, buflen, "%s: %s%s.%s%s",
784                          str, fname, kname, mname, sname);
785   if (len < buflen)
786     str = buf;
787 #endif //PRODUCT
788   return str;
789 }
790 
791 
792 // These are to get information about intrinsics.
793 
794 #define ID4(x, y, z, f) ((ID3(x, y, z) << vmIntrinsics::log2_FLAG_LIMIT) | (jlong) (f))
795 
796 #ifndef PRODUCT
797 static const jlong intrinsic_info_array[vmIntrinsics::number_of_intrinsics()+1] = {
798 #define VM_INTRINSIC_INFO(ignore_id, klass, name, sig, fcode) \
799   ID4(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig), vmIntrinsics::fcode),
800 
801   0, VM_INTRINSICS_DO(VM_INTRINSIC_INFO,
802                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE)
803   0
804 #undef VM_INTRINSIC_INFO
805 };
806 
807 inline jlong intrinsic_info(vmIntrinsics::ID id) {
808   return intrinsic_info_array[vmIntrinsics::as_int(id)];
809 }
810 
811 vmSymbolID vmIntrinsics::class_for(vmIntrinsics::ID id) {
812   jlong info = intrinsic_info(id);
813   int shift = 2*vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
814   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1021, "");
815   return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
816 }
817 
818 vmSymbolID vmIntrinsics::name_for(vmIntrinsics::ID id) {
819   jlong info = intrinsic_info(id);
820   int shift = vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
821   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1022, "");
822   return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
823 }
824 
825 vmSymbolID vmIntrinsics::signature_for(vmIntrinsics::ID id) {
826   jlong info = intrinsic_info(id);
827   int shift = log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
828   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1023, "");
829   return vmSymbols::as_SID( checked_cast<int>((info >> shift) & mask));
830 }
831 
832 vmIntrinsics::Flags vmIntrinsics::flags_for(vmIntrinsics::ID id) {
833   jlong info = intrinsic_info(id);
834   int shift = 0, mask = right_n_bits(log2_FLAG_LIMIT);
835   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 7, "");
836   return Flags( (info >> shift) & mask );
837 }
838 #endif // !PRODUCT