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