blob: a6be324730c1f089f4096ec5a8889ec7f5033778 [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
Aart Bik59c94542016-01-25 14:20:58 -0800134 // Floating-point tests.
135 case kIntrinsicFloatIsInfinite:
136 return Intrinsics::kFloatIsInfinite;
137 case kIntrinsicDoubleIsInfinite:
138 return Intrinsics::kDoubleIsInfinite;
139 case kIntrinsicFloatIsNaN:
140 return Intrinsics::kFloatIsNaN;
141 case kIntrinsicDoubleIsNaN:
142 return Intrinsics::kDoubleIsNaN;
143
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800144 // Bit manipulations.
145 case kIntrinsicReverseBits:
146 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800147 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800148 return Intrinsics::kIntegerReverse;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800149 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800150 return Intrinsics::kLongReverse;
151 default:
152 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
153 UNREACHABLE();
154 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800155 case kIntrinsicReverseBytes:
156 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800157 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800158 return Intrinsics::kShortReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800159 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800160 return Intrinsics::kIntegerReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800161 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800162 return Intrinsics::kLongReverseBytes;
163 default:
164 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
165 UNREACHABLE();
166 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100167 case kIntrinsicRotateRight:
168 switch (GetType(method.d.data, true)) {
169 case Primitive::kPrimInt:
170 return Intrinsics::kIntegerRotateRight;
171 case Primitive::kPrimLong:
172 return Intrinsics::kLongRotateRight;
173 default:
174 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
175 UNREACHABLE();
176 }
177 case kIntrinsicRotateLeft:
178 switch (GetType(method.d.data, true)) {
179 case Primitive::kPrimInt:
180 return Intrinsics::kIntegerRotateLeft;
181 case Primitive::kPrimLong:
182 return Intrinsics::kLongRotateLeft;
183 default:
184 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
185 UNREACHABLE();
186 }
187
188 // Misc data processing.
Aart Bik3f67e692016-01-15 14:35:12 -0800189 case kIntrinsicBitCount:
190 switch (GetType(method.d.data, true)) {
191 case Primitive::kPrimInt:
192 return Intrinsics::kIntegerBitCount;
193 case Primitive::kPrimLong:
194 return Intrinsics::kLongBitCount;
195 default:
196 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
197 UNREACHABLE();
198 }
Aart Bik59c94542016-01-25 14:20:58 -0800199 case kIntrinsicCompare:
200 switch (GetType(method.d.data, true)) {
201 case Primitive::kPrimInt:
202 return Intrinsics::kIntegerCompare;
203 case Primitive::kPrimLong:
204 return Intrinsics::kLongCompare;
205 default:
206 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
207 UNREACHABLE();
208 }
209 case kIntrinsicHighestOneBit:
210 switch (GetType(method.d.data, true)) {
211 case Primitive::kPrimInt:
212 return Intrinsics::kIntegerHighestOneBit;
213 case Primitive::kPrimLong:
214 return Intrinsics::kLongHighestOneBit;
215 default:
216 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
217 UNREACHABLE();
218 }
219 case kIntrinsicLowestOneBit:
220 switch (GetType(method.d.data, true)) {
221 case Primitive::kPrimInt:
222 return Intrinsics::kIntegerLowestOneBit;
223 case Primitive::kPrimLong:
224 return Intrinsics::kLongLowestOneBit;
225 default:
226 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
227 UNREACHABLE();
228 }
Scott Wakeling611d3392015-07-10 11:42:06 +0100229 case kIntrinsicNumberOfLeadingZeros:
230 switch (GetType(method.d.data, true)) {
231 case Primitive::kPrimInt:
232 return Intrinsics::kIntegerNumberOfLeadingZeros;
233 case Primitive::kPrimLong:
234 return Intrinsics::kLongNumberOfLeadingZeros;
235 default:
236 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
237 UNREACHABLE();
238 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100239 case kIntrinsicNumberOfTrailingZeros:
240 switch (GetType(method.d.data, true)) {
241 case Primitive::kPrimInt:
242 return Intrinsics::kIntegerNumberOfTrailingZeros;
243 case Primitive::kPrimLong:
244 return Intrinsics::kLongNumberOfTrailingZeros;
245 default:
246 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
247 UNREACHABLE();
248 }
Aart Bik59c94542016-01-25 14:20:58 -0800249 case kIntrinsicSignum:
250 switch (GetType(method.d.data, true)) {
251 case Primitive::kPrimInt:
252 return Intrinsics::kIntegerSignum;
253 case Primitive::kPrimLong:
254 return Intrinsics::kLongSignum;
255 default:
256 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
257 UNREACHABLE();
258 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800259
260 // Abs.
261 case kIntrinsicAbsDouble:
262 return Intrinsics::kMathAbsDouble;
263 case kIntrinsicAbsFloat:
264 return Intrinsics::kMathAbsFloat;
265 case kIntrinsicAbsInt:
266 return Intrinsics::kMathAbsInt;
267 case kIntrinsicAbsLong:
268 return Intrinsics::kMathAbsLong;
269
270 // Min/max.
271 case kIntrinsicMinMaxDouble:
272 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
273 Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble;
274 case kIntrinsicMinMaxFloat:
275 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
276 Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat;
277 case kIntrinsicMinMaxInt:
278 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
279 Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt;
280 case kIntrinsicMinMaxLong:
281 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
282 Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong;
283
Mark Mendella4f12202015-08-06 15:23:34 -0400284 // More math builtins.
285 case kIntrinsicCos:
286 return Intrinsics::kMathCos;
287 case kIntrinsicSin:
288 return Intrinsics::kMathSin;
289 case kIntrinsicAcos:
290 return Intrinsics::kMathAcos;
291 case kIntrinsicAsin:
292 return Intrinsics::kMathAsin;
293 case kIntrinsicAtan:
294 return Intrinsics::kMathAtan;
295 case kIntrinsicAtan2:
296 return Intrinsics::kMathAtan2;
297 case kIntrinsicCbrt:
298 return Intrinsics::kMathCbrt;
299 case kIntrinsicCosh:
300 return Intrinsics::kMathCosh;
301 case kIntrinsicExp:
302 return Intrinsics::kMathExp;
303 case kIntrinsicExpm1:
304 return Intrinsics::kMathExpm1;
305 case kIntrinsicHypot:
306 return Intrinsics::kMathHypot;
307 case kIntrinsicLog:
308 return Intrinsics::kMathLog;
309 case kIntrinsicLog10:
310 return Intrinsics::kMathLog10;
311 case kIntrinsicNextAfter:
312 return Intrinsics::kMathNextAfter;
313 case kIntrinsicSinh:
314 return Intrinsics::kMathSinh;
315 case kIntrinsicTan:
316 return Intrinsics::kMathTan;
317 case kIntrinsicTanh:
318 return Intrinsics::kMathTanh;
319
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800320 // Misc math.
321 case kIntrinsicSqrt:
322 return Intrinsics::kMathSqrt;
323 case kIntrinsicCeil:
324 return Intrinsics::kMathCeil;
325 case kIntrinsicFloor:
326 return Intrinsics::kMathFloor;
327 case kIntrinsicRint:
328 return Intrinsics::kMathRint;
329 case kIntrinsicRoundDouble:
330 return Intrinsics::kMathRoundDouble;
331 case kIntrinsicRoundFloat:
332 return Intrinsics::kMathRoundFloat;
333
334 // System.arraycopy.
335 case kIntrinsicSystemArrayCopyCharArray:
336 return Intrinsics::kSystemArrayCopyChar;
337
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100338 case kIntrinsicSystemArrayCopy:
339 return Intrinsics::kSystemArrayCopy;
340
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800341 // Thread.currentThread.
342 case kIntrinsicCurrentThread:
Aart Bik5d75afe2015-12-14 11:57:01 -0800343 return Intrinsics::kThreadCurrentThread;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800344
345 // Memory.peek.
346 case kIntrinsicPeek:
347 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800348 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800349 return Intrinsics::kMemoryPeekByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800350 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800351 return Intrinsics::kMemoryPeekShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800352 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800353 return Intrinsics::kMemoryPeekIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800354 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800355 return Intrinsics::kMemoryPeekLongNative;
356 default:
357 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
358 UNREACHABLE();
359 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800360
361 // Memory.poke.
362 case kIntrinsicPoke:
363 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800364 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800365 return Intrinsics::kMemoryPokeByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800366 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800367 return Intrinsics::kMemoryPokeShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800368 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800369 return Intrinsics::kMemoryPokeIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800370 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800371 return Intrinsics::kMemoryPokeLongNative;
372 default:
373 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
374 UNREACHABLE();
375 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800376
377 // String.
378 case kIntrinsicCharAt:
379 return Intrinsics::kStringCharAt;
380 case kIntrinsicCompareTo:
381 return Intrinsics::kStringCompareTo;
agicsaki7da072f2015-08-12 20:30:17 -0700382 case kIntrinsicEquals:
383 return Intrinsics::kStringEquals;
Jeff Hao848f70a2014-01-15 13:49:50 -0800384 case kIntrinsicGetCharsNoCheck:
385 return Intrinsics::kStringGetCharsNoCheck;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800386 case kIntrinsicIsEmptyOrLength:
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -0700387 // The inliner can handle these two cases - and this is the preferred approach
388 // since after inlining the call is no longer visible (as opposed to waiting
389 // until codegen to handle intrinsic).
390 return Intrinsics::kNone;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800391 case kIntrinsicIndexOf:
392 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
393 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
Jeff Hao848f70a2014-01-15 13:49:50 -0800394 case kIntrinsicNewStringFromBytes:
395 return Intrinsics::kStringNewStringFromBytes;
396 case kIntrinsicNewStringFromChars:
397 return Intrinsics::kStringNewStringFromChars;
398 case kIntrinsicNewStringFromString:
399 return Intrinsics::kStringNewStringFromString;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800400
401 case kIntrinsicCas:
402 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800403 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800404 return Intrinsics::kUnsafeCASObject;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800405 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800406 return Intrinsics::kUnsafeCASInt;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800407 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800408 return Intrinsics::kUnsafeCASLong;
409 default:
410 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
411 UNREACHABLE();
412 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800413 case kIntrinsicUnsafeGet: {
414 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
415 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800416 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800417 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800418 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800419 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800420 case Primitive::kPrimNot:
421 return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800422 default:
423 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
424 UNREACHABLE();
425 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800426 }
427 case kIntrinsicUnsafePut: {
428 enum Sync { kNoSync, kVolatile, kOrdered };
429 const Sync sync =
430 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
431 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
432 kNoSync;
433 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800434 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800435 switch (sync) {
436 case kNoSync:
437 return Intrinsics::kUnsafePut;
438 case kVolatile:
439 return Intrinsics::kUnsafePutVolatile;
440 case kOrdered:
441 return Intrinsics::kUnsafePutOrdered;
442 }
443 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800444 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800445 switch (sync) {
446 case kNoSync:
447 return Intrinsics::kUnsafePutLong;
448 case kVolatile:
449 return Intrinsics::kUnsafePutLongVolatile;
450 case kOrdered:
451 return Intrinsics::kUnsafePutLongOrdered;
452 }
453 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800454 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800455 switch (sync) {
456 case kNoSync:
457 return Intrinsics::kUnsafePutObject;
458 case kVolatile:
459 return Intrinsics::kUnsafePutObjectVolatile;
460 case kOrdered:
461 return Intrinsics::kUnsafePutObjectOrdered;
462 }
463 break;
464 default:
465 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
466 UNREACHABLE();
467 }
468 break;
469 }
470
471 // Virtual cases.
472
473 case kIntrinsicReferenceGetReferent:
474 return Intrinsics::kReferenceGetReferent;
475
476 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
477 // compiler to warn on missing cases.
478
479 case kInlineOpNop:
480 case kInlineOpReturnArg:
481 case kInlineOpNonWideConst:
482 case kInlineOpIGet:
483 case kInlineOpIPut:
484 return Intrinsics::kNone;
485
Jeff Hao848f70a2014-01-15 13:49:50 -0800486 // String init cases, not intrinsics.
487
488 case kInlineStringInit:
489 return Intrinsics::kNone;
490
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800491 // No default case to make the compiler warn on missing cases.
492 }
493 return Intrinsics::kNone;
494}
495
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000496static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke, const DexFile& dex_file) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800497 // The DexFileMethodInliner should have checked whether the methods are agreeing with
498 // what we expect, i.e., static methods are called as such. Add another check here for
499 // our expectations:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000500 //
501 // Whenever the intrinsic is marked as static, report an error if we find an InvokeVirtual.
502 //
503 // Whenever the intrinsic is marked as direct and we find an InvokeVirtual, a devirtualization
504 // failure occured. We might be in a situation where we have inlined a method that calls an
505 // intrinsic, but that method is in a different dex file on which we do not have a
506 // verified_method that would have helped the compiler driver sharpen the call. In that case,
507 // make sure that the intrinsic is actually for some final method (or in a final class), as
508 // otherwise the intrinsics setup is broken.
509 //
510 // For the last direction, we have intrinsics for virtual functions that will perform a check
511 // inline. If the precise type is known, however, the instruction will be sharpened to an
512 // InvokeStaticOrDirect.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800513 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
514 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
Nicolas Geoffraye5234232015-12-02 09:06:11 +0000515 invoke->AsInvokeStaticOrDirect()->GetOptimizedInvokeType() :
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800516 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
517 switch (intrinsic_type) {
518 case kStatic:
519 return (invoke_type == kStatic);
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000520
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800521 case kDirect:
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000522 if (invoke_type == kDirect) {
523 return true;
524 }
525 if (invoke_type == kVirtual) {
526 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
527 ScopedObjectAccess soa(Thread::Current());
528 ArtMethod* art_method =
529 class_linker->FindDexCache(soa.Self(), dex_file)->GetResolvedMethod(
530 invoke->GetDexMethodIndex(), class_linker->GetImagePointerSize());
531 return art_method != nullptr &&
532 (art_method->IsFinal() || art_method->GetDeclaringClass()->IsFinal());
533 }
534 return false;
535
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800536 case kVirtual:
537 // Call might be devirtualized.
538 return (invoke_type == kVirtual || invoke_type == kDirect);
539
540 default:
541 return false;
542 }
543}
544
545// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
546void IntrinsicsRecognizer::Run() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800547 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
548 HBasicBlock* block = it.Current();
549 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
550 inst_it.Advance()) {
551 HInstruction* inst = inst_it.Current();
552 if (inst->IsInvoke()) {
553 HInvoke* invoke = inst->AsInvoke();
554 InlineMethod method;
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000555 const DexFile& dex_file = invoke->GetDexFile();
556 DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(&dex_file);
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100557 DCHECK(inliner != nullptr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800558 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
Chris Larsen16ba2b42015-11-02 10:58:31 -0800559 Intrinsics intrinsic = GetIntrinsic(method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800560
561 if (intrinsic != Intrinsics::kNone) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000562 if (!CheckInvokeType(intrinsic, invoke, dex_file)) {
Andreas Gampea14b9fe2015-08-24 22:49:59 +0000563 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000564 << intrinsic << " for "
565 << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile())
566 << invoke->DebugName();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800567 } else {
Aart Bik5d75afe2015-12-14 11:57:01 -0800568 invoke->SetIntrinsic(intrinsic,
569 NeedsEnvironmentOrCache(intrinsic),
570 GetSideEffects(intrinsic),
571 GetExceptions(intrinsic));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800572 }
573 }
574 }
575 }
576 }
577 }
578}
579
580std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
581 switch (intrinsic) {
582 case Intrinsics::kNone:
David Brazdil109c89a2015-07-31 17:10:43 +0100583 os << "None";
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800584 break;
Aart Bik5d75afe2015-12-14 11:57:01 -0800585#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache, SideEffects, Exceptions) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800586 case Intrinsics::k ## Name: \
587 os << # Name; \
588 break;
589#include "intrinsics_list.h"
590INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
591#undef STATIC_INTRINSICS_LIST
592#undef VIRTUAL_INTRINSICS_LIST
593#undef OPTIMIZING_INTRINSICS
594 }
595 return os;
596}
597
598} // namespace art