blob: dbe75249be968892cbf3860315fc948a8e65cb1d [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.
Agi Csaki05f20562015-08-19 14:58:14 -070039#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080040 case Intrinsics::k ## Name: \
41 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.
55#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \
agicsaki57b81ec2015-08-11 17:39:37 -070056 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
66static Primitive::Type GetType(uint64_t data, bool is_op_size) {
67 if (is_op_size) {
68 switch (static_cast<OpSize>(data)) {
69 case kSignedByte:
Andreas Gampe878d58c2015-01-15 23:24:00 -080070 return Primitive::kPrimByte;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080071 case kSignedHalf:
Andreas Gampe878d58c2015-01-15 23:24:00 -080072 return Primitive::kPrimShort;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080073 case k32:
Andreas Gampe878d58c2015-01-15 23:24:00 -080074 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080075 case k64:
Andreas Gampe878d58c2015-01-15 23:24:00 -080076 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080077 default:
78 LOG(FATAL) << "Unknown/unsupported op size " << data;
79 UNREACHABLE();
80 }
81 } else {
82 if ((data & kIntrinsicFlagIsLong) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080083 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080084 }
85 if ((data & kIntrinsicFlagIsObject) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080086 return Primitive::kPrimNot;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080087 }
Andreas Gampe878d58c2015-01-15 23:24:00 -080088 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080089 }
90}
91
agicsaki6cff09a2015-08-12 21:20:43 -070092static Intrinsics GetIntrinsic(InlineMethod method, InstructionSet instruction_set) {
Chris Larsen3039e382015-08-26 07:54:08 -070093 if (instruction_set == kMips) {
agicsaki6cff09a2015-08-12 21:20:43 -070094 return Intrinsics::kNone;
95 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -080096 switch (method.opcode) {
97 // Floating-point conversions.
98 case kIntrinsicDoubleCvt:
99 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
100 Intrinsics::kDoubleDoubleToRawLongBits : Intrinsics::kDoubleLongBitsToDouble;
101 case kIntrinsicFloatCvt:
102 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
103 Intrinsics::kFloatFloatToRawIntBits : Intrinsics::kFloatIntBitsToFloat;
104
105 // Bit manipulations.
106 case kIntrinsicReverseBits:
107 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800108 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800109 return Intrinsics::kIntegerReverse;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800110 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800111 return Intrinsics::kLongReverse;
112 default:
113 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
114 UNREACHABLE();
115 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800116 case kIntrinsicReverseBytes:
117 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800118 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800119 return Intrinsics::kShortReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800120 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800121 return Intrinsics::kIntegerReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800122 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800123 return Intrinsics::kLongReverseBytes;
124 default:
125 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
126 UNREACHABLE();
127 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100128 case kIntrinsicRotateRight:
129 switch (GetType(method.d.data, true)) {
130 case Primitive::kPrimInt:
131 return Intrinsics::kIntegerRotateRight;
132 case Primitive::kPrimLong:
133 return Intrinsics::kLongRotateRight;
134 default:
135 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
136 UNREACHABLE();
137 }
138 case kIntrinsicRotateLeft:
139 switch (GetType(method.d.data, true)) {
140 case Primitive::kPrimInt:
141 return Intrinsics::kIntegerRotateLeft;
142 case Primitive::kPrimLong:
143 return Intrinsics::kLongRotateLeft;
144 default:
145 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
146 UNREACHABLE();
147 }
148
149 // Misc data processing.
Scott Wakeling611d3392015-07-10 11:42:06 +0100150 case kIntrinsicNumberOfLeadingZeros:
151 switch (GetType(method.d.data, true)) {
152 case Primitive::kPrimInt:
153 return Intrinsics::kIntegerNumberOfLeadingZeros;
154 case Primitive::kPrimLong:
155 return Intrinsics::kLongNumberOfLeadingZeros;
156 default:
157 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
158 UNREACHABLE();
159 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100160 case kIntrinsicNumberOfTrailingZeros:
161 switch (GetType(method.d.data, true)) {
162 case Primitive::kPrimInt:
163 return Intrinsics::kIntegerNumberOfTrailingZeros;
164 case Primitive::kPrimLong:
165 return Intrinsics::kLongNumberOfTrailingZeros;
166 default:
167 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
168 UNREACHABLE();
169 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800170
171 // Abs.
172 case kIntrinsicAbsDouble:
173 return Intrinsics::kMathAbsDouble;
174 case kIntrinsicAbsFloat:
175 return Intrinsics::kMathAbsFloat;
176 case kIntrinsicAbsInt:
177 return Intrinsics::kMathAbsInt;
178 case kIntrinsicAbsLong:
179 return Intrinsics::kMathAbsLong;
180
181 // Min/max.
182 case kIntrinsicMinMaxDouble:
183 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
184 Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble;
185 case kIntrinsicMinMaxFloat:
186 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
187 Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat;
188 case kIntrinsicMinMaxInt:
189 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
190 Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt;
191 case kIntrinsicMinMaxLong:
192 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
193 Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong;
194
195 // Misc math.
196 case kIntrinsicSqrt:
197 return Intrinsics::kMathSqrt;
198 case kIntrinsicCeil:
199 return Intrinsics::kMathCeil;
200 case kIntrinsicFloor:
201 return Intrinsics::kMathFloor;
202 case kIntrinsicRint:
203 return Intrinsics::kMathRint;
204 case kIntrinsicRoundDouble:
205 return Intrinsics::kMathRoundDouble;
206 case kIntrinsicRoundFloat:
207 return Intrinsics::kMathRoundFloat;
208
209 // System.arraycopy.
210 case kIntrinsicSystemArrayCopyCharArray:
211 return Intrinsics::kSystemArrayCopyChar;
212
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100213 case kIntrinsicSystemArrayCopy:
214 return Intrinsics::kSystemArrayCopy;
215
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800216 // Thread.currentThread.
217 case kIntrinsicCurrentThread:
218 return Intrinsics::kThreadCurrentThread;
219
220 // Memory.peek.
221 case kIntrinsicPeek:
222 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800223 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800224 return Intrinsics::kMemoryPeekByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800225 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800226 return Intrinsics::kMemoryPeekShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800227 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800228 return Intrinsics::kMemoryPeekIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800229 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800230 return Intrinsics::kMemoryPeekLongNative;
231 default:
232 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
233 UNREACHABLE();
234 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800235
236 // Memory.poke.
237 case kIntrinsicPoke:
238 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800239 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800240 return Intrinsics::kMemoryPokeByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800241 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800242 return Intrinsics::kMemoryPokeShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800243 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800244 return Intrinsics::kMemoryPokeIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800245 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800246 return Intrinsics::kMemoryPokeLongNative;
247 default:
248 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
249 UNREACHABLE();
250 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800251
252 // String.
253 case kIntrinsicCharAt:
254 return Intrinsics::kStringCharAt;
255 case kIntrinsicCompareTo:
256 return Intrinsics::kStringCompareTo;
agicsaki7da072f2015-08-12 20:30:17 -0700257 case kIntrinsicEquals:
258 return Intrinsics::kStringEquals;
Jeff Hao848f70a2014-01-15 13:49:50 -0800259 case kIntrinsicGetCharsNoCheck:
260 return Intrinsics::kStringGetCharsNoCheck;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800261 case kIntrinsicIsEmptyOrLength:
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -0700262 // The inliner can handle these two cases - and this is the preferred approach
263 // since after inlining the call is no longer visible (as opposed to waiting
264 // until codegen to handle intrinsic).
265 return Intrinsics::kNone;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800266 case kIntrinsicIndexOf:
267 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
268 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
Jeff Hao848f70a2014-01-15 13:49:50 -0800269 case kIntrinsicNewStringFromBytes:
270 return Intrinsics::kStringNewStringFromBytes;
271 case kIntrinsicNewStringFromChars:
272 return Intrinsics::kStringNewStringFromChars;
273 case kIntrinsicNewStringFromString:
274 return Intrinsics::kStringNewStringFromString;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800275
276 case kIntrinsicCas:
277 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800278 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800279 return Intrinsics::kUnsafeCASObject;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800280 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800281 return Intrinsics::kUnsafeCASInt;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800282 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800283 return Intrinsics::kUnsafeCASLong;
284 default:
285 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
286 UNREACHABLE();
287 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800288 case kIntrinsicUnsafeGet: {
289 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
290 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800291 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800292 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800293 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800294 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800295 case Primitive::kPrimNot:
296 return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800297 default:
298 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
299 UNREACHABLE();
300 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800301 }
302 case kIntrinsicUnsafePut: {
303 enum Sync { kNoSync, kVolatile, kOrdered };
304 const Sync sync =
305 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
306 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
307 kNoSync;
308 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800309 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800310 switch (sync) {
311 case kNoSync:
312 return Intrinsics::kUnsafePut;
313 case kVolatile:
314 return Intrinsics::kUnsafePutVolatile;
315 case kOrdered:
316 return Intrinsics::kUnsafePutOrdered;
317 }
318 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800319 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800320 switch (sync) {
321 case kNoSync:
322 return Intrinsics::kUnsafePutLong;
323 case kVolatile:
324 return Intrinsics::kUnsafePutLongVolatile;
325 case kOrdered:
326 return Intrinsics::kUnsafePutLongOrdered;
327 }
328 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800329 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800330 switch (sync) {
331 case kNoSync:
332 return Intrinsics::kUnsafePutObject;
333 case kVolatile:
334 return Intrinsics::kUnsafePutObjectVolatile;
335 case kOrdered:
336 return Intrinsics::kUnsafePutObjectOrdered;
337 }
338 break;
339 default:
340 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
341 UNREACHABLE();
342 }
343 break;
344 }
345
346 // Virtual cases.
347
348 case kIntrinsicReferenceGetReferent:
349 return Intrinsics::kReferenceGetReferent;
350
351 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
352 // compiler to warn on missing cases.
353
354 case kInlineOpNop:
355 case kInlineOpReturnArg:
356 case kInlineOpNonWideConst:
357 case kInlineOpIGet:
358 case kInlineOpIPut:
359 return Intrinsics::kNone;
360
Jeff Hao848f70a2014-01-15 13:49:50 -0800361 // String init cases, not intrinsics.
362
363 case kInlineStringInit:
364 return Intrinsics::kNone;
365
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800366 // No default case to make the compiler warn on missing cases.
367 }
368 return Intrinsics::kNone;
369}
370
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000371static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke, const DexFile& dex_file) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800372 // The DexFileMethodInliner should have checked whether the methods are agreeing with
373 // what we expect, i.e., static methods are called as such. Add another check here for
374 // our expectations:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000375 //
376 // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual.
377 //
378 // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization
379 // failure occured. We might be in a situation where we have inlined a method that calls an
380 // intrinsic, but that method is in a different dex file on which we do not have a
381 // verified_method that would have helped the compiler driver sharpen the call. In that case,
382 // make sure that the intrinsic is actually for some final method (or in a final class), as
383 // otherwise the intrinsics setup is broken.
384 //
385 // For the last direction, we have intrinsics for virtual functions that will perform a check
386 // inline. If the precise type is known, however, the instruction will be sharpened to an
387 // InvokeStaticOrDirect.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800388 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
389 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
390 invoke->AsInvokeStaticOrDirect()->GetInvokeType() :
391 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
392 switch (intrinsic_type) {
393 case kStatic:
394 return (invoke_type == kStatic);
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000395
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800396 case kDirect:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000397 if (invoke_type == kDirect) {
398 return true;
399 }
400 if (invoke_type == kVirtual) {
401 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
402 ScopedObjectAccess soa(Thread::Current());
403 ArtMethod* art_method =
404 class_linker->FindDexCache(soa.Self(), dex_file)->GetResolvedMethod(
405 invoke->GetDexMethodIndex(), class_linker->GetImagePointerSize());
406 return art_method != nullptr &&
407 (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal());
408 }
409 return false;
410
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800411 case kVirtual:
412 // Call might be devirtualized.
413 return (invoke_type == kVirtual || invoke_type == kDirect);
414
415 default:
416 return false;
417 }
418}
419
420// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
421void IntrinsicsRecognizer::Run() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800422 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
423 HBasicBlock* block = it.Current();
424 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
425 inst_it.Advance()) {
426 HInstruction* inst = inst_it.Current();
427 if (inst->IsInvoke()) {
428 HInvoke* invoke = inst->AsInvoke();
429 InlineMethod method;
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000430 const DexFile& dex_file = invoke->GetDexFile();
431 DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(&dex_file);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100432 DCHECK(inliner != nullptr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800433 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
agicsaki6cff09a2015-08-12 21:20:43 -0700434 Intrinsics intrinsic = GetIntrinsic(method, graph_->GetInstructionSet());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800435
436 if (intrinsic != Intrinsics::kNone) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000437 if (!CheckInvokeType(intrinsic, invoke, dex_file)) {
Andreas Gampea14b9fe2015-08-24 22:49:59 +0000438 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000439 << intrinsic << " for "
440 << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile())
441 << invoke->DebugName();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800442 } else {
Agi Csaki05f20562015-08-19 14:58:14 -0700443 invoke->SetIntrinsic(intrinsic, NeedsEnvironmentOrCache(intrinsic));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800444 }
445 }
446 }
447 }
448 }
449 }
450}
451
452std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
453 switch (intrinsic) {
454 case Intrinsics::kNone:
David Brazdil109c89a2015-07-31 17:10:43 +0100455 os << "None";
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800456 break;
Agi Csaki05f20562015-08-19 14:58:14 -0700457#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800458 case Intrinsics::k ## Name: \
459 os << # Name; \
460 break;
461#include "intrinsics_list.h"
462INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
463#undef STATIC_INTRINSICS_LIST
464#undef VIRTUAL_INTRINSICS_LIST
465#undef OPTIMIZING_INTRINSICS
466 }
467 return os;
468}
469
470} // namespace art