blob: c6da9a3f5e17305543e85ab0ea85320262a91fcc [file] [log] [blame]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics.h"
18
Andreas Gampebfb5ba92015-09-01 15:45:02 +000019#include "art_method.h"
20#include "class_linker.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "dex/quick/dex_file_method_inliner.h"
22#include "dex/quick/dex_file_to_method_inliner_map.h"
23#include "driver/compiler_driver.h"
24#include "invoke_type.h"
Andreas Gampebfb5ba92015-09-01 15:45:02 +000025#include "mirror/dex_cache-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080026#include "nodes.h"
27#include "quick/inline_method_analyser.h"
Andreas Gampebfb5ba92015-09-01 15:45:02 +000028#include "scoped_thread_state_change.h"
29#include "thread-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010030#include "utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080031
32namespace art {
33
34// Function that returns whether an intrinsic is static/direct or virtual.
35static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) {
36 switch (i) {
37 case Intrinsics::kNone:
38 return kInterface; // Non-sensical for intrinsic.
Aart Bik5d75afe2015-12-14 11:57:01 -080039#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
40 case Intrinsics::k ## Name: \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080041 return IsStatic;
42#include "intrinsics_list.h"
43INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
44#undef INTRINSICS_LIST
45#undef OPTIMIZING_INTRINSICS
46 }
47 return kInterface;
48}
49
agicsaki57b81ec2015-08-11 17:39:37 -070050// Function that returns whether an intrinsic needs an environment or not.
Agi Csaki05f20562015-08-19 14:58:14 -070051static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) {
agicsaki57b81ec2015-08-11 17:39:37 -070052 switch (i) {
53 case Intrinsics::kNone:
Agi Csaki05f20562015-08-19 14:58:14 -070054 return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic.
Aart Bik5d75afe2015-12-14 11:57:01 -080055#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
56 case Intrinsics::k ## Name: \
Agi Csaki05f20562015-08-19 14:58:14 -070057 return NeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070058#include "intrinsics_list.h"
59INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
60#undef INTRINSICS_LIST
61#undef OPTIMIZING_INTRINSICS
62 }
Agi Csaki05f20562015-08-19 14:58:14 -070063 return kNeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070064}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080065
Aart Bik5d75afe2015-12-14 11:57:01 -080066// Function that returns whether an intrinsic has side effects.
67static inline IntrinsicSideEffects GetSideEffects(Intrinsics i) {
68 switch (i) {
69 case Intrinsics::kNone:
70 return kAllSideEffects;
71#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
72 case Intrinsics::k ## Name: \
73 return SideEffects;
74#include "intrinsics_list.h"
75INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
76#undef INTRINSICS_LIST
77#undef OPTIMIZING_INTRINSICS
78 }
79 return kAllSideEffects;
80}
81
82// Function that returns whether an intrinsic can throw exceptions.
83static inline IntrinsicExceptions GetExceptions(Intrinsics i) {
84 switch (i) {
85 case Intrinsics::kNone:
86 return kCanThrow;
87#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
88 case Intrinsics::k ## Name: \
89 return Exceptions;
90#include "intrinsics_list.h"
91INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
92#undef INTRINSICS_LIST
93#undef OPTIMIZING_INTRINSICS
94 }
95 return kCanThrow;
96}
97
Andreas Gampe71fb52f2014-12-29 17:43:08 -080098static Primitive::Type GetType(uint64_t data, bool is_op_size) {
99 if (is_op_size) {
100 switch (static_cast<OpSize>(data)) {
101 case kSignedByte:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800102 return Primitive::kPrimByte;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800103 case kSignedHalf:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800104 return Primitive::kPrimShort;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800105 case k32:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800106 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800107 case k64:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800108 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800109 default:
110 LOG(FATAL) << "Unknown/unsupported op size " << data;
111 UNREACHABLE();
112 }
113 } else {
114 if ((data & kIntrinsicFlagIsLong) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800115 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800116 }
117 if ((data & kIntrinsicFlagIsObject) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800118 return Primitive::kPrimNot;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800119 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800120 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800121 }
122}
123
Chris Larsen16ba2b42015-11-02 10:58:31 -0800124static Intrinsics GetIntrinsic(InlineMethod method) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800125 switch (method.opcode) {
126 // Floating-point conversions.
127 case kIntrinsicDoubleCvt:
128 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
129 Intrinsics::kDoubleDoubleToRawLongBits : Intrinsics::kDoubleLongBitsToDouble;
130 case kIntrinsicFloatCvt:
131 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
132 Intrinsics::kFloatFloatToRawIntBits : Intrinsics::kFloatIntBitsToFloat;
133
134 // Bit manipulations.
135 case kIntrinsicReverseBits:
136 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800137 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800138 return Intrinsics::kIntegerReverse;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800139 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800140 return Intrinsics::kLongReverse;
141 default:
142 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
143 UNREACHABLE();
144 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800145 case kIntrinsicReverseBytes:
146 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800147 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800148 return Intrinsics::kShortReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800149 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800150 return Intrinsics::kIntegerReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800151 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800152 return Intrinsics::kLongReverseBytes;
153 default:
154 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
155 UNREACHABLE();
156 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100157 case kIntrinsicRotateRight:
158 switch (GetType(method.d.data, true)) {
159 case Primitive::kPrimInt:
160 return Intrinsics::kIntegerRotateRight;
161 case Primitive::kPrimLong:
162 return Intrinsics::kLongRotateRight;
163 default:
164 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
165 UNREACHABLE();
166 }
167 case kIntrinsicRotateLeft:
168 switch (GetType(method.d.data, true)) {
169 case Primitive::kPrimInt:
170 return Intrinsics::kIntegerRotateLeft;
171 case Primitive::kPrimLong:
172 return Intrinsics::kLongRotateLeft;
173 default:
174 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
175 UNREACHABLE();
176 }
177
178 // Misc data processing.
Scott Wakeling611d3392015-07-10 11:42:06 +0100179 case kIntrinsicNumberOfLeadingZeros:
180 switch (GetType(method.d.data, true)) {
181 case Primitive::kPrimInt:
182 return Intrinsics::kIntegerNumberOfLeadingZeros;
183 case Primitive::kPrimLong:
184 return Intrinsics::kLongNumberOfLeadingZeros;
185 default:
186 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
187 UNREACHABLE();
188 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100189 case kIntrinsicNumberOfTrailingZeros:
190 switch (GetType(method.d.data, true)) {
191 case Primitive::kPrimInt:
192 return Intrinsics::kIntegerNumberOfTrailingZeros;
193 case Primitive::kPrimLong:
194 return Intrinsics::kLongNumberOfTrailingZeros;
195 default:
196 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
197 UNREACHABLE();
198 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800199
200 // Abs.
201 case kIntrinsicAbsDouble:
202 return Intrinsics::kMathAbsDouble;
203 case kIntrinsicAbsFloat:
204 return Intrinsics::kMathAbsFloat;
205 case kIntrinsicAbsInt:
206 return Intrinsics::kMathAbsInt;
207 case kIntrinsicAbsLong:
208 return Intrinsics::kMathAbsLong;
209
210 // Min/max.
211 case kIntrinsicMinMaxDouble:
212 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
213 Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble;
214 case kIntrinsicMinMaxFloat:
215 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
216 Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat;
217 case kIntrinsicMinMaxInt:
218 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
219 Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt;
220 case kIntrinsicMinMaxLong:
221 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
222 Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong;
223
Mark Mendella4f12202015-08-06 15:23:34 -0400224 // More math builtins.
225 case kIntrinsicCos:
226 return Intrinsics::kMathCos;
227 case kIntrinsicSin:
228 return Intrinsics::kMathSin;
229 case kIntrinsicAcos:
230 return Intrinsics::kMathAcos;
231 case kIntrinsicAsin:
232 return Intrinsics::kMathAsin;
233 case kIntrinsicAtan:
234 return Intrinsics::kMathAtan;
235 case kIntrinsicAtan2:
236 return Intrinsics::kMathAtan2;
237 case kIntrinsicCbrt:
238 return Intrinsics::kMathCbrt;
239 case kIntrinsicCosh:
240 return Intrinsics::kMathCosh;
241 case kIntrinsicExp:
242 return Intrinsics::kMathExp;
243 case kIntrinsicExpm1:
244 return Intrinsics::kMathExpm1;
245 case kIntrinsicHypot:
246 return Intrinsics::kMathHypot;
247 case kIntrinsicLog:
248 return Intrinsics::kMathLog;
249 case kIntrinsicLog10:
250 return Intrinsics::kMathLog10;
251 case kIntrinsicNextAfter:
252 return Intrinsics::kMathNextAfter;
253 case kIntrinsicSinh:
254 return Intrinsics::kMathSinh;
255 case kIntrinsicTan:
256 return Intrinsics::kMathTan;
257 case kIntrinsicTanh:
258 return Intrinsics::kMathTanh;
259
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800260 // Misc math.
261 case kIntrinsicSqrt:
262 return Intrinsics::kMathSqrt;
263 case kIntrinsicCeil:
264 return Intrinsics::kMathCeil;
265 case kIntrinsicFloor:
266 return Intrinsics::kMathFloor;
267 case kIntrinsicRint:
268 return Intrinsics::kMathRint;
269 case kIntrinsicRoundDouble:
270 return Intrinsics::kMathRoundDouble;
271 case kIntrinsicRoundFloat:
272 return Intrinsics::kMathRoundFloat;
273
274 // System.arraycopy.
275 case kIntrinsicSystemArrayCopyCharArray:
276 return Intrinsics::kSystemArrayCopyChar;
277
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100278 case kIntrinsicSystemArrayCopy:
279 return Intrinsics::kSystemArrayCopy;
280
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800281 // Thread.currentThread.
282 case kIntrinsicCurrentThread:
Aart Bik5d75afe2015-12-14 11:57:01 -0800283 return Intrinsics::kThreadCurrentThread;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800284
285 // Memory.peek.
286 case kIntrinsicPeek:
287 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800288 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800289 return Intrinsics::kMemoryPeekByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800290 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800291 return Intrinsics::kMemoryPeekShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800292 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800293 return Intrinsics::kMemoryPeekIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800294 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800295 return Intrinsics::kMemoryPeekLongNative;
296 default:
297 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
298 UNREACHABLE();
299 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800300
301 // Memory.poke.
302 case kIntrinsicPoke:
303 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800304 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800305 return Intrinsics::kMemoryPokeByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800306 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800307 return Intrinsics::kMemoryPokeShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800308 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800309 return Intrinsics::kMemoryPokeIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800310 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800311 return Intrinsics::kMemoryPokeLongNative;
312 default:
313 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
314 UNREACHABLE();
315 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800316
317 // String.
318 case kIntrinsicCharAt:
319 return Intrinsics::kStringCharAt;
320 case kIntrinsicCompareTo:
321 return Intrinsics::kStringCompareTo;
agicsaki7da072f2015-08-12 20:30:17 -0700322 case kIntrinsicEquals:
323 return Intrinsics::kStringEquals;
Jeff Hao848f70a2014-01-15 13:49:50 -0800324 case kIntrinsicGetCharsNoCheck:
325 return Intrinsics::kStringGetCharsNoCheck;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800326 case kIntrinsicIsEmptyOrLength:
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -0700327 // The inliner can handle these two cases - and this is the preferred approach
328 // since after inlining the call is no longer visible (as opposed to waiting
329 // until codegen to handle intrinsic).
330 return Intrinsics::kNone;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800331 case kIntrinsicIndexOf:
332 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
333 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
Jeff Hao848f70a2014-01-15 13:49:50 -0800334 case kIntrinsicNewStringFromBytes:
335 return Intrinsics::kStringNewStringFromBytes;
336 case kIntrinsicNewStringFromChars:
337 return Intrinsics::kStringNewStringFromChars;
338 case kIntrinsicNewStringFromString:
339 return Intrinsics::kStringNewStringFromString;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800340
341 case kIntrinsicCas:
342 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800343 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800344 return Intrinsics::kUnsafeCASObject;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800345 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800346 return Intrinsics::kUnsafeCASInt;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800347 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800348 return Intrinsics::kUnsafeCASLong;
349 default:
350 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
351 UNREACHABLE();
352 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800353 case kIntrinsicUnsafeGet: {
354 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
355 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800356 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800357 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800358 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800359 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800360 case Primitive::kPrimNot:
361 return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800362 default:
363 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
364 UNREACHABLE();
365 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800366 }
367 case kIntrinsicUnsafePut: {
368 enum Sync { kNoSync, kVolatile, kOrdered };
369 const Sync sync =
370 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
371 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
372 kNoSync;
373 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800374 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800375 switch (sync) {
376 case kNoSync:
377 return Intrinsics::kUnsafePut;
378 case kVolatile:
379 return Intrinsics::kUnsafePutVolatile;
380 case kOrdered:
381 return Intrinsics::kUnsafePutOrdered;
382 }
383 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800384 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800385 switch (sync) {
386 case kNoSync:
387 return Intrinsics::kUnsafePutLong;
388 case kVolatile:
389 return Intrinsics::kUnsafePutLongVolatile;
390 case kOrdered:
391 return Intrinsics::kUnsafePutLongOrdered;
392 }
393 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800394 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800395 switch (sync) {
396 case kNoSync:
397 return Intrinsics::kUnsafePutObject;
398 case kVolatile:
399 return Intrinsics::kUnsafePutObjectVolatile;
400 case kOrdered:
401 return Intrinsics::kUnsafePutObjectOrdered;
402 }
403 break;
404 default:
405 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
406 UNREACHABLE();
407 }
408 break;
409 }
410
411 // Virtual cases.
412
413 case kIntrinsicReferenceGetReferent:
414 return Intrinsics::kReferenceGetReferent;
415
416 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
417 // compiler to warn on missing cases.
418
419 case kInlineOpNop:
420 case kInlineOpReturnArg:
421 case kInlineOpNonWideConst:
422 case kInlineOpIGet:
423 case kInlineOpIPut:
424 return Intrinsics::kNone;
425
Jeff Hao848f70a2014-01-15 13:49:50 -0800426 // String init cases, not intrinsics.
427
428 case kInlineStringInit:
429 return Intrinsics::kNone;
430
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800431 // No default case to make the compiler warn on missing cases.
432 }
433 return Intrinsics::kNone;
434}
435
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000436static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke, const DexFile& dex_file) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800437 // The DexFileMethodInliner should have checked whether the methods are agreeing with
438 // what we expect, i.e., static methods are called as such. Add another check here for
439 // our expectations:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000440 //
441 // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual.
442 //
443 // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization
444 // failure occured. We might be in a situation where we have inlined a method that calls an
445 // intrinsic, but that method is in a different dex file on which we do not have a
446 // verified_method that would have helped the compiler driver sharpen the call. In that case,
447 // make sure that the intrinsic is actually for some final method (or in a final class), as
448 // otherwise the intrinsics setup is broken.
449 //
450 // For the last direction, we have intrinsics for virtual functions that will perform a check
451 // inline. If the precise type is known, however, the instruction will be sharpened to an
452 // InvokeStaticOrDirect.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800453 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
454 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000455 invoke->AsInvokeStaticOrDirect()->GetOptimizedInvokeType() :
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800456 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
457 switch (intrinsic_type) {
458 case kStatic:
459 return (invoke_type == kStatic);
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000460
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800461 case kDirect:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000462 if (invoke_type == kDirect) {
463 return true;
464 }
465 if (invoke_type == kVirtual) {
466 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
467 ScopedObjectAccess soa(Thread::Current());
468 ArtMethod* art_method =
469 class_linker->FindDexCache(soa.Self(), dex_file)->GetResolvedMethod(
470 invoke->GetDexMethodIndex(), class_linker->GetImagePointerSize());
471 return art_method != nullptr &&
472 (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal());
473 }
474 return false;
475
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800476 case kVirtual:
477 // Call might be devirtualized.
478 return (invoke_type == kVirtual || invoke_type == kDirect);
479
480 default:
481 return false;
482 }
483}
484
485// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
486void IntrinsicsRecognizer::Run() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800487 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
488 HBasicBlock* block = it.Current();
489 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
490 inst_it.Advance()) {
491 HInstruction* inst = inst_it.Current();
492 if (inst->IsInvoke()) {
493 HInvoke* invoke = inst->AsInvoke();
494 InlineMethod method;
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000495 const DexFile& dex_file = invoke->GetDexFile();
496 DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(&dex_file);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100497 DCHECK(inliner != nullptr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800498 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
Chris Larsen16ba2b42015-11-02 10:58:31 -0800499 Intrinsics intrinsic = GetIntrinsic(method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800500
501 if (intrinsic != Intrinsics::kNone) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000502 if (!CheckInvokeType(intrinsic, invoke, dex_file)) {
Andreas Gampea14b9fe2015-08-24 22:49:59 +0000503 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000504 << intrinsic << " for "
505 << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile())
506 << invoke->DebugName();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800507 } else {
Aart Bik5d75afe2015-12-14 11:57:01 -0800508 invoke->SetIntrinsic(intrinsic,
509 NeedsEnvironmentOrCache(intrinsic),
510 GetSideEffects(intrinsic),
511 GetExceptions(intrinsic));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800512 }
513 }
514 }
515 }
516 }
517 }
518}
519
520std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
521 switch (intrinsic) {
522 case Intrinsics::kNone:
David Brazdil109c89a2015-07-31 17:10:43 +0100523 os << "None";
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800524 break;
Aart Bik5d75afe2015-12-14 11:57:01 -0800525#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800526 case Intrinsics::k ## Name: \
527 os << # Name; \
528 break;
529#include "intrinsics_list.h"
530INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
531#undef STATIC_INTRINSICS_LIST
532#undef VIRTUAL_INTRINSICS_LIST
533#undef OPTIMIZING_INTRINSICS
534 }
535 return os;
536}
537
538} // namespace art