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