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