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