1 /*
  2  * Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "jvm_constants.h"
 27 #include "jvm_io.h"
 28 #include "classfile/vmIntrinsics.hpp"
 29 #include "classfile/vmSymbols.hpp"
 30 #include "compiler/compilerDirectives.hpp"
 31 #include "utilities/xmlstream.hpp"
 32 
 33 // These are flag-matching functions:
 34 inline bool match_F_R(jshort flags) {
 35   const int req = 0;
 36   const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED | JVM_ACC_NATIVE;
 37   return (flags & (req | neg)) == req;
 38 }
 39 
 40 inline bool match_F_Y(jshort flags) {
 41   const int req = JVM_ACC_SYNCHRONIZED;
 42   const int neg = JVM_ACC_STATIC | JVM_ACC_NATIVE;
 43   return (flags & (req | neg)) == req;
 44 }
 45 
 46 inline bool match_F_RN(jshort flags) {
 47   const int req = JVM_ACC_NATIVE;
 48   const int neg = JVM_ACC_STATIC | JVM_ACC_SYNCHRONIZED;
 49   return (flags & (req | neg)) == req;
 50 }
 51 
 52 inline bool match_F_S(jshort flags) {
 53   const int req = JVM_ACC_STATIC;
 54   const int neg = JVM_ACC_SYNCHRONIZED | JVM_ACC_NATIVE;
 55   return (flags & (req | neg)) == req;
 56 }
 57 
 58 inline bool match_F_SN(jshort flags) {
 59   const int req = JVM_ACC_STATIC | JVM_ACC_NATIVE;
 60   const int neg = JVM_ACC_SYNCHRONIZED;
 61   return (flags & (req | neg)) == req;
 62 }
 63 
 64 bool vmIntrinsics::preserves_state(vmIntrinsics::ID id) {
 65   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
 66   switch(id) {
 67 #ifdef JFR_HAVE_INTRINSICS
 68   case vmIntrinsics::_counterTime:
 69 #endif
 70   case vmIntrinsics::_currentTimeMillis:
 71   case vmIntrinsics::_nanoTime:
 72   case vmIntrinsics::_floatToRawIntBits:
 73   case vmIntrinsics::_intBitsToFloat:
 74   case vmIntrinsics::_doubleToRawLongBits:
 75   case vmIntrinsics::_longBitsToDouble:
 76   case vmIntrinsics::_getClass:
 77   case vmIntrinsics::_isInstance:
 78   case vmIntrinsics::_currentCarrierThread:
 79   case vmIntrinsics::_currentThread:
 80   case vmIntrinsics::_extentLocalCache:
 81   case vmIntrinsics::_dabs:
 82   case vmIntrinsics::_fabs:
 83   case vmIntrinsics::_iabs:
 84   case vmIntrinsics::_labs:
 85   case vmIntrinsics::_dsqrt:
 86   case vmIntrinsics::_dsqrt_strict:
 87   case vmIntrinsics::_dsin:
 88   case vmIntrinsics::_dcos:
 89   case vmIntrinsics::_dtan:
 90   case vmIntrinsics::_dlog:
 91   case vmIntrinsics::_dlog10:
 92   case vmIntrinsics::_dexp:
 93   case vmIntrinsics::_dpow:
 94   case vmIntrinsics::_Preconditions_checkIndex:
 95   case vmIntrinsics::_Preconditions_checkLongIndex:
 96   case vmIntrinsics::_Reference_get:
 97   case vmIntrinsics::_Continuation_doYield:
 98   case vmIntrinsics::_updateCRC32:
 99   case vmIntrinsics::_updateBytesCRC32:
100   case vmIntrinsics::_updateByteBufferCRC32:
101   case vmIntrinsics::_updateBytesAdler32:
102   case vmIntrinsics::_vectorizedMismatch:
103   case vmIntrinsics::_fmaD:
104   case vmIntrinsics::_fmaF:
105   case vmIntrinsics::_isDigit:
106   case vmIntrinsics::_isLowerCase:
107   case vmIntrinsics::_isUpperCase:
108   case vmIntrinsics::_isWhitespace:
109     return true;
110   default:
111     return false;
112   }
113 }
114 
115 bool vmIntrinsics::can_trap(vmIntrinsics::ID id) {
116   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
117   switch(id) {
118 #ifdef JFR_HAVE_INTRINSICS
119   case vmIntrinsics::_counterTime:
120 #endif
121   case vmIntrinsics::_currentTimeMillis:
122   case vmIntrinsics::_nanoTime:
123   case vmIntrinsics::_floatToRawIntBits:
124   case vmIntrinsics::_intBitsToFloat:
125   case vmIntrinsics::_doubleToRawLongBits:
126   case vmIntrinsics::_longBitsToDouble:
127   case vmIntrinsics::_currentCarrierThread:
128   case vmIntrinsics::_currentThread:
129   case vmIntrinsics::_setCurrentThread:
130   case vmIntrinsics::_extentLocalCache:
131   case vmIntrinsics::_setExtentLocalCache:
132   case vmIntrinsics::_dabs:
133   case vmIntrinsics::_fabs:
134   case vmIntrinsics::_iabs:
135   case vmIntrinsics::_labs:
136   case vmIntrinsics::_dsqrt:
137   case vmIntrinsics::_dsqrt_strict:
138   case vmIntrinsics::_dsin:
139   case vmIntrinsics::_dcos:
140   case vmIntrinsics::_dtan:
141   case vmIntrinsics::_dlog:
142   case vmIntrinsics::_dlog10:
143   case vmIntrinsics::_dexp:
144   case vmIntrinsics::_dpow:
145   case vmIntrinsics::_updateCRC32:
146   case vmIntrinsics::_updateBytesCRC32:
147   case vmIntrinsics::_updateByteBufferCRC32:
148   case vmIntrinsics::_vectorizedMismatch:
149   case vmIntrinsics::_fmaD:
150   case vmIntrinsics::_fmaF:
151     return false;
152   default:
153     return true;
154   }
155 }
156 
157 // Some intrinsics produce different results if they are not pinned
158 bool vmIntrinsics::should_be_pinned(vmIntrinsics::ID id) {
159   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
160   switch(id) {
161 #ifdef JFR_HAVE_INTRINSICS
162   case vmIntrinsics::_counterTime:
163 #endif
164   case vmIntrinsics::_currentTimeMillis:
165   case vmIntrinsics::_nanoTime:
166   case vmIntrinsics::_blackhole:
167     return true;
168   default:
169     return false;
170   }
171 }
172 
173 bool vmIntrinsics::does_virtual_dispatch(vmIntrinsics::ID id) {
174   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
175   switch(id) {
176   case vmIntrinsics::_hashCode:
177   case vmIntrinsics::_clone:
178     return true;
179     break;
180   default:
181     return false;
182   }
183 }
184 
185 int vmIntrinsics::predicates_needed(vmIntrinsics::ID id) {
186   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
187   switch (id) {
188   case vmIntrinsics::_cipherBlockChaining_encryptAESCrypt:
189   case vmIntrinsics::_cipherBlockChaining_decryptAESCrypt:
190   case vmIntrinsics::_electronicCodeBook_encryptAESCrypt:
191   case vmIntrinsics::_electronicCodeBook_decryptAESCrypt:
192   case vmIntrinsics::_counterMode_AESCrypt:
193   case vmIntrinsics::_galoisCounterMode_AESCrypt:
194     return 1;
195   case vmIntrinsics::_digestBase_implCompressMB:
196     return 5;
197   default:
198     return 0;
199   }
200 }
201 
202 bool vmIntrinsics::disabled_by_jvm_flags(vmIntrinsics::ID id) {
203   assert(id != vmIntrinsics::_none, "must be a VM intrinsic");
204 
205   // -XX:-InlineNatives disables nearly all intrinsics except the ones listed in
206   // the following switch statement.
207   if (!InlineNatives) {
208     switch (id) {
209     case vmIntrinsics::_indexOfL:
210     case vmIntrinsics::_indexOfU:
211     case vmIntrinsics::_indexOfUL:
212     case vmIntrinsics::_indexOfIL:
213     case vmIntrinsics::_indexOfIU:
214     case vmIntrinsics::_indexOfIUL:
215     case vmIntrinsics::_indexOfU_char:
216     case vmIntrinsics::_indexOfL_char:
217     case vmIntrinsics::_compareToL:
218     case vmIntrinsics::_compareToU:
219     case vmIntrinsics::_compareToLU:
220     case vmIntrinsics::_compareToUL:
221     case vmIntrinsics::_equalsL:
222     case vmIntrinsics::_equalsU:
223     case vmIntrinsics::_equalsC:
224     case vmIntrinsics::_getCharStringU:
225     case vmIntrinsics::_putCharStringU:
226     case vmIntrinsics::_compressStringC:
227     case vmIntrinsics::_compressStringB:
228     case vmIntrinsics::_inflateStringC:
229     case vmIntrinsics::_inflateStringB:
230     case vmIntrinsics::_getAndAddInt:
231     case vmIntrinsics::_getAndAddLong:
232     case vmIntrinsics::_getAndSetInt:
233     case vmIntrinsics::_getAndSetLong:
234     case vmIntrinsics::_getAndSetReference:
235     case vmIntrinsics::_loadFence:
236     case vmIntrinsics::_storeFence:
237     case vmIntrinsics::_fullFence:
238     case vmIntrinsics::_countPositives:
239     case vmIntrinsics::_Reference_get:
240     case vmIntrinsics::_Continuation_doYield:
241     case vmIntrinsics::_Continuation_enterSpecial:
242       break;
243     default:
244       return true;
245     }
246   }
247 
248   switch (id) {
249   case vmIntrinsics::_asPrimaryType:
250   case vmIntrinsics::_asPrimaryTypeArg:
251   case vmIntrinsics::_asValueType:
252   case vmIntrinsics::_asValueTypeArg:
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::_extentLocalCache:
273   case vmIntrinsics::_setExtentLocalCache:
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::_arraycopy:
316     if (!InlineArrayCopy) return true;
317     break;
318   case vmIntrinsics::_updateCRC32:
319   case vmIntrinsics::_updateBytesCRC32:
320   case vmIntrinsics::_updateByteBufferCRC32:
321     if (!UseCRC32Intrinsics) return true;
322     break;
323   case vmIntrinsics::_makePrivateBuffer:
324   case vmIntrinsics::_finishPrivateBuffer:
325   case vmIntrinsics::_getReference:
326   case vmIntrinsics::_getBoolean:
327   case vmIntrinsics::_getByte:
328   case vmIntrinsics::_getShort:
329   case vmIntrinsics::_getChar:
330   case vmIntrinsics::_getInt:
331   case vmIntrinsics::_getLong:
332   case vmIntrinsics::_getFloat:
333   case vmIntrinsics::_getDouble:
334   case vmIntrinsics::_getValue:
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::_putValue:
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::_base64_encodeBlock:
487   case vmIntrinsics::_base64_decodeBlock:
488     if (!UseBASE64Intrinsics) return true;
489     break;
490   case vmIntrinsics::_updateBytesCRC32C:
491   case vmIntrinsics::_updateDirectByteBufferCRC32C:
492     if (!UseCRC32CIntrinsics) return true;
493     break;
494   case vmIntrinsics::_vectorizedMismatch:
495     if (!UseVectorizedMismatchIntrinsic) return true;
496     break;
497   case vmIntrinsics::_updateBytesAdler32:
498   case vmIntrinsics::_updateByteBufferAdler32:
499     if (!UseAdler32Intrinsics) return true;
500     break;
501   case vmIntrinsics::_copyMemory:
502     if (!InlineArrayCopy || !InlineUnsafeOps) return true;
503     break;
504 #ifdef COMPILER2
505   case vmIntrinsics::_clone:
506   case vmIntrinsics::_copyOf:
507   case vmIntrinsics::_copyOfRange:
508     // These intrinsics use both the objectcopy and the arraycopy
509     // intrinsic mechanism.
510     if (!InlineObjectCopy || !InlineArrayCopy) return true;
511     break;
512   case vmIntrinsics::_compareToL:
513   case vmIntrinsics::_compareToU:
514   case vmIntrinsics::_compareToLU:
515   case vmIntrinsics::_compareToUL:
516     if (!SpecialStringCompareTo) return true;
517     break;
518   case vmIntrinsics::_indexOfL:
519   case vmIntrinsics::_indexOfU:
520   case vmIntrinsics::_indexOfUL:
521   case vmIntrinsics::_indexOfIL:
522   case vmIntrinsics::_indexOfIU:
523   case vmIntrinsics::_indexOfIUL:
524   case vmIntrinsics::_indexOfU_char:
525   case vmIntrinsics::_indexOfL_char:
526     if (!SpecialStringIndexOf) return true;
527     break;
528   case vmIntrinsics::_equalsL:
529   case vmIntrinsics::_equalsU:
530     if (!SpecialStringEquals) return true;
531     break;
532   case vmIntrinsics::_equalsB:
533   case vmIntrinsics::_equalsC:
534     if (!SpecialArraysEquals) return true;
535     break;
536   case vmIntrinsics::_encodeISOArray:
537   case vmIntrinsics::_encodeAsciiArray:
538   case vmIntrinsics::_encodeByteISOArray:
539     if (!SpecialEncodeISOArray) return true;
540     break;
541   case vmIntrinsics::_getCallerClass:
542     if (!InlineReflectionGetCallerClass) return true;
543     break;
544   case vmIntrinsics::_multiplyToLen:
545     if (!UseMultiplyToLenIntrinsic) return true;
546     break;
547   case vmIntrinsics::_squareToLen:
548     if (!UseSquareToLenIntrinsic) return true;
549     break;
550   case vmIntrinsics::_mulAdd:
551     if (!UseMulAddIntrinsic) return true;
552     break;
553   case vmIntrinsics::_montgomeryMultiply:
554     if (!UseMontgomeryMultiplyIntrinsic) return true;
555     break;
556   case vmIntrinsics::_montgomerySquare:
557     if (!UseMontgomerySquareIntrinsic) return true;
558     break;
559   case vmIntrinsics::_bigIntegerRightShiftWorker:
560   case vmIntrinsics::_bigIntegerLeftShiftWorker:
561     break;
562   case vmIntrinsics::_addExactI:
563   case vmIntrinsics::_addExactL:
564   case vmIntrinsics::_decrementExactI:
565   case vmIntrinsics::_decrementExactL:
566   case vmIntrinsics::_incrementExactI:
567   case vmIntrinsics::_incrementExactL:
568   case vmIntrinsics::_multiplyExactI:
569   case vmIntrinsics::_multiplyExactL:
570   case vmIntrinsics::_negateExactI:
571   case vmIntrinsics::_negateExactL:
572   case vmIntrinsics::_subtractExactI:
573   case vmIntrinsics::_subtractExactL:
574     if (!UseMathExactIntrinsics || !InlineMathNatives) return true;
575     break;
576   case vmIntrinsics::_isDigit:
577   case vmIntrinsics::_isLowerCase:
578   case vmIntrinsics::_isUpperCase:
579   case vmIntrinsics::_isWhitespace:
580     if (!UseCharacterCompareIntrinsics) return true;
581     break;
582   case vmIntrinsics::_dcopySign:
583   case vmIntrinsics::_fcopySign:
584     if (!InlineMathNatives || !UseCopySignIntrinsic) return true;
585     break;
586   case vmIntrinsics::_dsignum:
587   case vmIntrinsics::_fsignum:
588     if (!InlineMathNatives || !UseSignumIntrinsic) return true;
589     break;
590 #endif // COMPILER2
591   default:
592     return false;
593   }
594 
595   return false;
596 }
597 
598 #define VM_INTRINSIC_INITIALIZE(id, klass, name, sig, flags) #id "\0"
599 static const char* vm_intrinsic_name_bodies =
600   VM_INTRINSICS_DO(VM_INTRINSIC_INITIALIZE,
601                    VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
602 
603 static const char* vm_intrinsic_name_table[vmIntrinsics::number_of_intrinsics()];
604 static TriBoolArray<(size_t)vmIntrinsics::number_of_intrinsics(), int> vm_intrinsic_control_words;
605 
606 void vmIntrinsics::init_vm_intrinsic_name_table() {
607   const char** nt = &vm_intrinsic_name_table[0];
608   char* string = (char*) &vm_intrinsic_name_bodies[0];
609 
610   for (auto index : EnumRange<vmIntrinsicID>{}) {
611     nt[as_int(index)] = string;
612     string += strlen(string); // skip string body
613     string += 1;              // skip trailing null
614   }
615   assert(!strcmp(nt[as_int(vmIntrinsics::_hashCode)], "_hashCode"), "lined up");
616   nt[as_int(vmIntrinsics::_none)] = "_none";
617 }
618 
619 const char* vmIntrinsics::name_at(vmIntrinsics::ID id) {
620   const char** nt = &vm_intrinsic_name_table[0];
621   if (nt[as_int(_none)] == NULL) {
622     init_vm_intrinsic_name_table();
623   }
624 
625   if (id < ID_LIMIT)
626     return vm_intrinsic_name_table[as_int(id)];
627   else
628     return "(unknown intrinsic)";
629 }
630 
631 vmIntrinsics::ID vmIntrinsics::find_id(const char* name) {
632   const char** nt = &vm_intrinsic_name_table[0];
633   if (nt[as_int(_none)] == NULL) {
634     init_vm_intrinsic_name_table();
635   }
636 
637   for (auto index : EnumRange<vmIntrinsicID>{}) {
638     if (0 == strcmp(name, nt[as_int(index)])) {
639       return index;
640     }
641   }
642 
643   return _none;
644 }
645 
646 bool vmIntrinsics::is_disabled_by_flags(const methodHandle& method) {
647   vmIntrinsics::ID id = method->intrinsic_id();
648   return is_disabled_by_flags(id);
649 }
650 
651 bool vmIntrinsics::is_disabled_by_flags(vmIntrinsics::ID id) {
652   assert(id > _none && id < ID_LIMIT, "must be a VM intrinsic");
653 
654   // not initialized yet, process Control/DisableIntrinsic
655   if (vm_intrinsic_control_words[as_int(_none)].is_default()) {
656     for (ControlIntrinsicIter iter(ControlIntrinsic); *iter != NULL; ++iter) {
657       vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
658 
659       if (id != vmIntrinsics::_none) {
660         vm_intrinsic_control_words[as_int(id)] = iter.is_enabled() && !disabled_by_jvm_flags(id);
661       }
662     }
663 
664     // Order matters, DisableIntrinsic can overwrite ControlIntrinsic
665     for (ControlIntrinsicIter iter(DisableIntrinsic, true/*disable_all*/); *iter != NULL; ++iter) {
666       vmIntrinsics::ID id = vmIntrinsics::find_id(*iter);
667 
668       if (id != vmIntrinsics::_none) {
669         vm_intrinsic_control_words[as_int(id)] = false;
670       }
671     }
672 
673     vm_intrinsic_control_words[as_int(_none)] = true;
674   }
675 
676   TriBool b = vm_intrinsic_control_words[as_int(id)];
677   if (b.is_default()) {
678     // unknown yet, query and cache it
679     b = vm_intrinsic_control_words[as_int(id)] = !disabled_by_jvm_flags(id);
680   }
681 
682   return !b;
683 }
684 
685 // These are for forming case labels:
686 #define ID3(x, y, z) (( jlong)(z) +                                  \
687                       ((jlong)(y) <<    vmSymbols::log2_SID_LIMIT) + \
688                       ((jlong)(x) << (2*vmSymbols::log2_SID_LIMIT))  )
689 #define SID_ENUM(n) VM_SYMBOL_ENUM_NAME(n)
690 
691 vmIntrinsics::ID vmIntrinsics::find_id_impl(vmSymbolID holder,
692                                             vmSymbolID name,
693                                             vmSymbolID sig,
694                                             jshort flags) {
695   assert((int)vmSymbolID::SID_LIMIT <= (1<<vmSymbols::log2_SID_LIMIT), "must fit");
696 
697   // Let the C compiler build the decision tree.
698 
699 #define VM_INTRINSIC_CASE(id, klass, name, sig, fcode) \
700   case ID3(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig)): \
701     if (!match_##fcode(flags))  break; \
702     return id;
703 
704   switch (ID3(holder, name, sig)) {
705     VM_INTRINSICS_DO(VM_INTRINSIC_CASE,
706                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
707   }
708   return vmIntrinsics::_none;
709 
710 #undef VM_INTRINSIC_CASE
711 }
712 
713 class vmIntrinsicsLookup {
714   bool _class_map[vmSymbols::number_of_symbols()];
715 
716   constexpr int as_index(vmSymbolID id) const {
717     int index = vmSymbols::as_int(id);
718     assert(0 <= index && index < int(sizeof(_class_map)), "must be");
719     return index;
720   }
721 
722   constexpr void set_class_map(vmSymbolID id) {
723     _class_map[as_index(id)] = true;
724   }
725 
726 public:
727   constexpr vmIntrinsicsLookup() : _class_map() {
728 
729 #define VM_INTRINSIC_CLASS_MAP(id, klass, name, sig, fcode) \
730     set_class_map(SID_ENUM(klass));
731 
732     VM_INTRINSICS_DO(VM_INTRINSIC_CLASS_MAP,
733                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE);
734 #undef VM_INTRINSIC_CLASS_MAP
735 
736 
737     // A few slightly irregular cases. See Method::init_intrinsic_id
738     set_class_map(SID_ENUM(java_lang_StrictMath));
739     set_class_map(SID_ENUM(java_lang_invoke_MethodHandle));
740     set_class_map(SID_ENUM(java_lang_invoke_VarHandle));
741   }
742 
743   bool class_has_intrinsics(vmSymbolID holder) const {
744     return _class_map[as_index(holder)];
745   }
746 };
747 
748 constexpr vmIntrinsicsLookup _intrinsics_lookup;
749 
750 bool vmIntrinsics::class_has_intrinsics(vmSymbolID holder) {
751   return _intrinsics_lookup.class_has_intrinsics(holder);
752 }
753 
754 const char* vmIntrinsics::short_name_as_C_string(vmIntrinsics::ID id, char* buf, int buflen) {
755   const char* str = name_at(id);
756 #ifndef PRODUCT
757   const char* kname = vmSymbols::name_for(class_for(id));
758   const char* mname = vmSymbols::name_for(name_for(id));
759   const char* sname = vmSymbols::name_for(signature_for(id));
760   const char* fname = "";
761   switch (flags_for(id)) {
762   case F_Y:  fname = "synchronized ";  break;
763   case F_RN: fname = "native ";        break;
764   case F_SN: fname = "native static "; break;
765   case F_S:  fname = "static ";        break;
766   default:   break;
767   }
768   const char* kptr = strrchr(kname, JVM_SIGNATURE_SLASH);
769   if (kptr != NULL)  kname = kptr + 1;
770   int len = jio_snprintf(buf, buflen, "%s: %s%s.%s%s",
771                          str, fname, kname, mname, sname);
772   if (len < buflen)
773     str = buf;
774 #endif //PRODUCT
775   return str;
776 }
777 
778 
779 // These are to get information about intrinsics.
780 
781 #define ID4(x, y, z, f) ((ID3(x, y, z) << vmIntrinsics::log2_FLAG_LIMIT) | (jlong) (f))
782 
783 #ifndef PRODUCT
784 static const jlong intrinsic_info_array[vmIntrinsics::number_of_intrinsics()+1] = {
785 #define VM_INTRINSIC_INFO(ignore_id, klass, name, sig, fcode) \
786   ID4(SID_ENUM(klass), SID_ENUM(name), SID_ENUM(sig), vmIntrinsics::fcode),
787 
788   0, VM_INTRINSICS_DO(VM_INTRINSIC_INFO,
789                      VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_SYMBOL_IGNORE, VM_ALIAS_IGNORE)
790   0
791 #undef VM_INTRINSIC_INFO
792 };
793 
794 inline jlong intrinsic_info(vmIntrinsics::ID id) {
795   return intrinsic_info_array[vmIntrinsics::as_int(id)];
796 }
797 
798 vmSymbolID vmIntrinsics::class_for(vmIntrinsics::ID id) {
799   jlong info = intrinsic_info(id);
800   int shift = 2*vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
801   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1021, "");
802   return vmSymbols::as_SID( (info >> shift) & mask );
803 }
804 
805 vmSymbolID vmIntrinsics::name_for(vmIntrinsics::ID id) {
806   jlong info = intrinsic_info(id);
807   int shift = vmSymbols::log2_SID_LIMIT + log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
808   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1022, "");
809   return vmSymbols::as_SID( (info >> shift) & mask );
810 }
811 
812 vmSymbolID vmIntrinsics::signature_for(vmIntrinsics::ID id) {
813   jlong info = intrinsic_info(id);
814   int shift = log2_FLAG_LIMIT, mask = right_n_bits(vmSymbols::log2_SID_LIMIT);
815   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 1023, "");
816   return vmSymbols::as_SID( (info >> shift) & mask );
817 }
818 
819 vmIntrinsics::Flags vmIntrinsics::flags_for(vmIntrinsics::ID id) {
820   jlong info = intrinsic_info(id);
821   int shift = 0, mask = right_n_bits(log2_FLAG_LIMIT);
822   assert(((ID4(1021,1022,1023,7) >> shift) & mask) == 7, "");
823   return Flags( (info >> shift) & mask );
824 }
825 #endif // !PRODUCT