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