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