blob: 075ec1ee2e3750cf7ddfdabeaaf4574e44436ae4 [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
19#include "dex/quick/dex_file_method_inliner.h"
20#include "dex/quick/dex_file_to_method_inliner_map.h"
21#include "driver/compiler_driver.h"
22#include "invoke_type.h"
23#include "nodes.h"
24#include "quick/inline_method_analyser.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080026
27namespace art {
28
29// Function that returns whether an intrinsic is static/direct or virtual.
30static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) {
31 switch (i) {
32 case Intrinsics::kNone:
33 return kInterface; // Non-sensical for intrinsic.
Agi Csaki05f20562015-08-19 14:58:14 -070034#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080035 case Intrinsics::k ## Name: \
36 return IsStatic;
37#include "intrinsics_list.h"
38INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
39#undef INTRINSICS_LIST
40#undef OPTIMIZING_INTRINSICS
41 }
42 return kInterface;
43}
44
agicsaki57b81ec2015-08-11 17:39:37 -070045// Function that returns whether an intrinsic needs an environment or not.
Agi Csaki05f20562015-08-19 14:58:14 -070046static inline IntrinsicNeedsEnvironmentOrCache NeedsEnvironmentOrCache(Intrinsics i) {
agicsaki57b81ec2015-08-11 17:39:37 -070047 switch (i) {
48 case Intrinsics::kNone:
Agi Csaki05f20562015-08-19 14:58:14 -070049 return kNeedsEnvironmentOrCache; // Non-sensical for intrinsic.
50#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \
agicsaki57b81ec2015-08-11 17:39:37 -070051 case Intrinsics::k ## Name: \
Agi Csaki05f20562015-08-19 14:58:14 -070052 return NeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070053#include "intrinsics_list.h"
54INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
55#undef INTRINSICS_LIST
56#undef OPTIMIZING_INTRINSICS
57 }
Agi Csaki05f20562015-08-19 14:58:14 -070058 return kNeedsEnvironmentOrCache;
agicsaki57b81ec2015-08-11 17:39:37 -070059}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080060
61static Primitive::Type GetType(uint64_t data, bool is_op_size) {
62 if (is_op_size) {
63 switch (static_cast<OpSize>(data)) {
64 case kSignedByte:
Andreas Gampe878d58c2015-01-15 23:24:00 -080065 return Primitive::kPrimByte;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080066 case kSignedHalf:
Andreas Gampe878d58c2015-01-15 23:24:00 -080067 return Primitive::kPrimShort;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080068 case k32:
Andreas Gampe878d58c2015-01-15 23:24:00 -080069 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080070 case k64:
Andreas Gampe878d58c2015-01-15 23:24:00 -080071 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080072 default:
73 LOG(FATAL) << "Unknown/unsupported op size " << data;
74 UNREACHABLE();
75 }
76 } else {
77 if ((data & kIntrinsicFlagIsLong) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080078 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080079 }
80 if ((data & kIntrinsicFlagIsObject) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080081 return Primitive::kPrimNot;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080082 }
Andreas Gampe878d58c2015-01-15 23:24:00 -080083 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080084 }
85}
86
agicsaki6cff09a2015-08-12 21:20:43 -070087static Intrinsics GetIntrinsic(InlineMethod method, InstructionSet instruction_set) {
88 if (instruction_set == kMips || instruction_set == kMips64) {
89 return Intrinsics::kNone;
90 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -080091 switch (method.opcode) {
92 // Floating-point conversions.
93 case kIntrinsicDoubleCvt:
94 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
95 Intrinsics::kDoubleDoubleToRawLongBits : Intrinsics::kDoubleLongBitsToDouble;
96 case kIntrinsicFloatCvt:
97 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
98 Intrinsics::kFloatFloatToRawIntBits : Intrinsics::kFloatIntBitsToFloat;
99
100 // Bit manipulations.
101 case kIntrinsicReverseBits:
102 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800103 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800104 return Intrinsics::kIntegerReverse;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800105 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800106 return Intrinsics::kLongReverse;
107 default:
108 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
109 UNREACHABLE();
110 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800111 case kIntrinsicReverseBytes:
112 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800113 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800114 return Intrinsics::kShortReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800115 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800116 return Intrinsics::kIntegerReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800117 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800118 return Intrinsics::kLongReverseBytes;
119 default:
120 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
121 UNREACHABLE();
122 }
Scott Wakeling611d3392015-07-10 11:42:06 +0100123 case kIntrinsicNumberOfLeadingZeros:
124 switch (GetType(method.d.data, true)) {
125 case Primitive::kPrimInt:
126 return Intrinsics::kIntegerNumberOfLeadingZeros;
127 case Primitive::kPrimLong:
128 return Intrinsics::kLongNumberOfLeadingZeros;
129 default:
130 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
131 UNREACHABLE();
132 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800133
134 // Abs.
135 case kIntrinsicAbsDouble:
136 return Intrinsics::kMathAbsDouble;
137 case kIntrinsicAbsFloat:
138 return Intrinsics::kMathAbsFloat;
139 case kIntrinsicAbsInt:
140 return Intrinsics::kMathAbsInt;
141 case kIntrinsicAbsLong:
142 return Intrinsics::kMathAbsLong;
143
144 // Min/max.
145 case kIntrinsicMinMaxDouble:
146 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
147 Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble;
148 case kIntrinsicMinMaxFloat:
149 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
150 Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat;
151 case kIntrinsicMinMaxInt:
152 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
153 Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt;
154 case kIntrinsicMinMaxLong:
155 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
156 Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong;
157
158 // Misc math.
159 case kIntrinsicSqrt:
160 return Intrinsics::kMathSqrt;
161 case kIntrinsicCeil:
162 return Intrinsics::kMathCeil;
163 case kIntrinsicFloor:
164 return Intrinsics::kMathFloor;
165 case kIntrinsicRint:
166 return Intrinsics::kMathRint;
167 case kIntrinsicRoundDouble:
168 return Intrinsics::kMathRoundDouble;
169 case kIntrinsicRoundFloat:
170 return Intrinsics::kMathRoundFloat;
171
172 // System.arraycopy.
173 case kIntrinsicSystemArrayCopyCharArray:
174 return Intrinsics::kSystemArrayCopyChar;
175
176 // Thread.currentThread.
177 case kIntrinsicCurrentThread:
178 return Intrinsics::kThreadCurrentThread;
179
180 // Memory.peek.
181 case kIntrinsicPeek:
182 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800183 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800184 return Intrinsics::kMemoryPeekByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800185 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800186 return Intrinsics::kMemoryPeekShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800187 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800188 return Intrinsics::kMemoryPeekIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800189 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800190 return Intrinsics::kMemoryPeekLongNative;
191 default:
192 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
193 UNREACHABLE();
194 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800195
196 // Memory.poke.
197 case kIntrinsicPoke:
198 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800199 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800200 return Intrinsics::kMemoryPokeByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800201 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800202 return Intrinsics::kMemoryPokeShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800203 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800204 return Intrinsics::kMemoryPokeIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800205 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800206 return Intrinsics::kMemoryPokeLongNative;
207 default:
208 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
209 UNREACHABLE();
210 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800211
212 // String.
213 case kIntrinsicCharAt:
214 return Intrinsics::kStringCharAt;
215 case kIntrinsicCompareTo:
216 return Intrinsics::kStringCompareTo;
agicsaki7da072f2015-08-12 20:30:17 -0700217 case kIntrinsicEquals:
218 return Intrinsics::kStringEquals;
Jeff Hao848f70a2014-01-15 13:49:50 -0800219 case kIntrinsicGetCharsNoCheck:
220 return Intrinsics::kStringGetCharsNoCheck;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800221 case kIntrinsicIsEmptyOrLength:
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -0700222 // The inliner can handle these two cases - and this is the preferred approach
223 // since after inlining the call is no longer visible (as opposed to waiting
224 // until codegen to handle intrinsic).
225 return Intrinsics::kNone;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800226 case kIntrinsicIndexOf:
227 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
228 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
Jeff Hao848f70a2014-01-15 13:49:50 -0800229 case kIntrinsicNewStringFromBytes:
230 return Intrinsics::kStringNewStringFromBytes;
231 case kIntrinsicNewStringFromChars:
232 return Intrinsics::kStringNewStringFromChars;
233 case kIntrinsicNewStringFromString:
234 return Intrinsics::kStringNewStringFromString;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800235
236 case kIntrinsicCas:
237 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800238 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800239 return Intrinsics::kUnsafeCASObject;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800240 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800241 return Intrinsics::kUnsafeCASInt;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800242 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800243 return Intrinsics::kUnsafeCASLong;
244 default:
245 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
246 UNREACHABLE();
247 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800248 case kIntrinsicUnsafeGet: {
249 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
250 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800251 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800252 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800253 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800254 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800255 case Primitive::kPrimNot:
256 return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800257 default:
258 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
259 UNREACHABLE();
260 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800261 }
262 case kIntrinsicUnsafePut: {
263 enum Sync { kNoSync, kVolatile, kOrdered };
264 const Sync sync =
265 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
266 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
267 kNoSync;
268 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800269 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800270 switch (sync) {
271 case kNoSync:
272 return Intrinsics::kUnsafePut;
273 case kVolatile:
274 return Intrinsics::kUnsafePutVolatile;
275 case kOrdered:
276 return Intrinsics::kUnsafePutOrdered;
277 }
278 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800279 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800280 switch (sync) {
281 case kNoSync:
282 return Intrinsics::kUnsafePutLong;
283 case kVolatile:
284 return Intrinsics::kUnsafePutLongVolatile;
285 case kOrdered:
286 return Intrinsics::kUnsafePutLongOrdered;
287 }
288 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800289 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800290 switch (sync) {
291 case kNoSync:
292 return Intrinsics::kUnsafePutObject;
293 case kVolatile:
294 return Intrinsics::kUnsafePutObjectVolatile;
295 case kOrdered:
296 return Intrinsics::kUnsafePutObjectOrdered;
297 }
298 break;
299 default:
300 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
301 UNREACHABLE();
302 }
303 break;
304 }
305
306 // Virtual cases.
307
308 case kIntrinsicReferenceGetReferent:
309 return Intrinsics::kReferenceGetReferent;
310
311 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
312 // compiler to warn on missing cases.
313
314 case kInlineOpNop:
315 case kInlineOpReturnArg:
316 case kInlineOpNonWideConst:
317 case kInlineOpIGet:
318 case kInlineOpIPut:
319 return Intrinsics::kNone;
320
Jeff Hao848f70a2014-01-15 13:49:50 -0800321 // String init cases, not intrinsics.
322
323 case kInlineStringInit:
324 return Intrinsics::kNone;
325
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800326 // No default case to make the compiler warn on missing cases.
327 }
328 return Intrinsics::kNone;
329}
330
331static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) {
332 // The DexFileMethodInliner should have checked whether the methods are agreeing with
333 // what we expect, i.e., static methods are called as such. Add another check here for
334 // our expectations:
335 // Whenever the intrinsic is marked as static-or-direct, report an error if we find an
336 // InvokeVirtual. The other direction is not possible: we have intrinsics for virtual
337 // functions that will perform a check inline. If the precise type is known, however,
338 // the instruction will be sharpened to an InvokeStaticOrDirect.
339 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
340 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
341 invoke->AsInvokeStaticOrDirect()->GetInvokeType() :
342 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
343 switch (intrinsic_type) {
344 case kStatic:
345 return (invoke_type == kStatic);
346 case kDirect:
347 return (invoke_type == kDirect);
348 case kVirtual:
349 // Call might be devirtualized.
350 return (invoke_type == kVirtual || invoke_type == kDirect);
351
352 default:
353 return false;
354 }
355}
356
357// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
358void IntrinsicsRecognizer::Run() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800359 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
360 HBasicBlock* block = it.Current();
361 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
362 inst_it.Advance()) {
363 HInstruction* inst = inst_it.Current();
364 if (inst->IsInvoke()) {
365 HInvoke* invoke = inst->AsInvoke();
366 InlineMethod method;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100367 DexFileMethodInliner* inliner =
368 driver_->GetMethodInlinerMap()->GetMethodInliner(&invoke->GetDexFile());
369 DCHECK(inliner != nullptr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800370 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
agicsaki6cff09a2015-08-12 21:20:43 -0700371 Intrinsics intrinsic = GetIntrinsic(method, graph_->GetInstructionSet());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800372
373 if (intrinsic != Intrinsics::kNone) {
374 if (!CheckInvokeType(intrinsic, invoke)) {
375 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
376 << intrinsic << " for "
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100377 << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800378 } else {
Agi Csaki05f20562015-08-19 14:58:14 -0700379 invoke->SetIntrinsic(intrinsic, NeedsEnvironmentOrCache(intrinsic));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800380 }
381 }
382 }
383 }
384 }
385 }
386}
387
388std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
389 switch (intrinsic) {
390 case Intrinsics::kNone:
David Brazdil109c89a2015-07-31 17:10:43 +0100391 os << "None";
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800392 break;
Agi Csaki05f20562015-08-19 14:58:14 -0700393#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironmentOrCache) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800394 case Intrinsics::k ## Name: \
395 os << # Name; \
396 break;
397#include "intrinsics_list.h"
398INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
399#undef STATIC_INTRINSICS_LIST
400#undef VIRTUAL_INTRINSICS_LIST
401#undef OPTIMIZING_INTRINSICS
402 }
403 return os;
404}
405
406} // namespace art