blob: bc7da802b15f59d78a67e7e4cf41ab7802860b66 [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.
34#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
35 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
45
46
47static Primitive::Type GetType(uint64_t data, bool is_op_size) {
48 if (is_op_size) {
49 switch (static_cast<OpSize>(data)) {
50 case kSignedByte:
Andreas Gampe878d58c2015-01-15 23:24:00 -080051 return Primitive::kPrimByte;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080052 case kSignedHalf:
Andreas Gampe878d58c2015-01-15 23:24:00 -080053 return Primitive::kPrimShort;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080054 case k32:
Andreas Gampe878d58c2015-01-15 23:24:00 -080055 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080056 case k64:
Andreas Gampe878d58c2015-01-15 23:24:00 -080057 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080058 default:
59 LOG(FATAL) << "Unknown/unsupported op size " << data;
60 UNREACHABLE();
61 }
62 } else {
63 if ((data & kIntrinsicFlagIsLong) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080064 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080065 }
66 if ((data & kIntrinsicFlagIsObject) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080067 return Primitive::kPrimNot;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080068 }
Andreas Gampe878d58c2015-01-15 23:24:00 -080069 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080070 }
71}
72
73static Intrinsics GetIntrinsic(InlineMethod method) {
74 switch (method.opcode) {
75 // Floating-point conversions.
76 case kIntrinsicDoubleCvt:
77 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
78 Intrinsics::kDoubleDoubleToRawLongBits : Intrinsics::kDoubleLongBitsToDouble;
79 case kIntrinsicFloatCvt:
80 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
81 Intrinsics::kFloatFloatToRawIntBits : Intrinsics::kFloatIntBitsToFloat;
82
83 // Bit manipulations.
84 case kIntrinsicReverseBits:
85 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080086 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080087 return Intrinsics::kIntegerReverse;
Andreas Gampe878d58c2015-01-15 23:24:00 -080088 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080089 return Intrinsics::kLongReverse;
90 default:
91 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
92 UNREACHABLE();
93 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -080094 case kIntrinsicReverseBytes:
95 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080096 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080097 return Intrinsics::kShortReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -080098 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080099 return Intrinsics::kIntegerReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800100 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800101 return Intrinsics::kLongReverseBytes;
102 default:
103 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
104 UNREACHABLE();
105 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800106
107 // Abs.
108 case kIntrinsicAbsDouble:
109 return Intrinsics::kMathAbsDouble;
110 case kIntrinsicAbsFloat:
111 return Intrinsics::kMathAbsFloat;
112 case kIntrinsicAbsInt:
113 return Intrinsics::kMathAbsInt;
114 case kIntrinsicAbsLong:
115 return Intrinsics::kMathAbsLong;
116
117 // Min/max.
118 case kIntrinsicMinMaxDouble:
119 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
120 Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble;
121 case kIntrinsicMinMaxFloat:
122 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
123 Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat;
124 case kIntrinsicMinMaxInt:
125 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
126 Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt;
127 case kIntrinsicMinMaxLong:
128 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
129 Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong;
130
131 // Misc math.
132 case kIntrinsicSqrt:
133 return Intrinsics::kMathSqrt;
134 case kIntrinsicCeil:
135 return Intrinsics::kMathCeil;
136 case kIntrinsicFloor:
137 return Intrinsics::kMathFloor;
138 case kIntrinsicRint:
139 return Intrinsics::kMathRint;
140 case kIntrinsicRoundDouble:
141 return Intrinsics::kMathRoundDouble;
142 case kIntrinsicRoundFloat:
143 return Intrinsics::kMathRoundFloat;
144
145 // System.arraycopy.
146 case kIntrinsicSystemArrayCopyCharArray:
147 return Intrinsics::kSystemArrayCopyChar;
148
149 // Thread.currentThread.
150 case kIntrinsicCurrentThread:
151 return Intrinsics::kThreadCurrentThread;
152
153 // Memory.peek.
154 case kIntrinsicPeek:
155 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800156 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800157 return Intrinsics::kMemoryPeekByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800158 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800159 return Intrinsics::kMemoryPeekShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800160 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800161 return Intrinsics::kMemoryPeekIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800162 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800163 return Intrinsics::kMemoryPeekLongNative;
164 default:
165 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
166 UNREACHABLE();
167 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800168
169 // Memory.poke.
170 case kIntrinsicPoke:
171 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800172 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800173 return Intrinsics::kMemoryPokeByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800174 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800175 return Intrinsics::kMemoryPokeShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800176 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800177 return Intrinsics::kMemoryPokeIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800178 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800179 return Intrinsics::kMemoryPokeLongNative;
180 default:
181 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
182 UNREACHABLE();
183 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800184
185 // String.
186 case kIntrinsicCharAt:
187 return Intrinsics::kStringCharAt;
188 case kIntrinsicCompareTo:
189 return Intrinsics::kStringCompareTo;
Jeff Hao848f70a2014-01-15 13:49:50 -0800190 case kIntrinsicGetCharsNoCheck:
191 return Intrinsics::kStringGetCharsNoCheck;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800192 case kIntrinsicIsEmptyOrLength:
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -0700193 // The inliner can handle these two cases - and this is the preferred approach
194 // since after inlining the call is no longer visible (as opposed to waiting
195 // until codegen to handle intrinsic).
196 return Intrinsics::kNone;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800197 case kIntrinsicIndexOf:
198 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
199 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
Jeff Hao848f70a2014-01-15 13:49:50 -0800200 case kIntrinsicNewStringFromBytes:
201 return Intrinsics::kStringNewStringFromBytes;
202 case kIntrinsicNewStringFromChars:
203 return Intrinsics::kStringNewStringFromChars;
204 case kIntrinsicNewStringFromString:
205 return Intrinsics::kStringNewStringFromString;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800206
207 case kIntrinsicCas:
208 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800209 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800210 return Intrinsics::kUnsafeCASObject;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800211 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800212 return Intrinsics::kUnsafeCASInt;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800213 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800214 return Intrinsics::kUnsafeCASLong;
215 default:
216 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
217 UNREACHABLE();
218 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800219 case kIntrinsicUnsafeGet: {
220 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
221 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800222 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800223 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800224 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800225 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800226 case Primitive::kPrimNot:
227 return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800228 default:
229 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
230 UNREACHABLE();
231 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800232 }
233 case kIntrinsicUnsafePut: {
234 enum Sync { kNoSync, kVolatile, kOrdered };
235 const Sync sync =
236 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
237 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
238 kNoSync;
239 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800240 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800241 switch (sync) {
242 case kNoSync:
243 return Intrinsics::kUnsafePut;
244 case kVolatile:
245 return Intrinsics::kUnsafePutVolatile;
246 case kOrdered:
247 return Intrinsics::kUnsafePutOrdered;
248 }
249 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800250 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800251 switch (sync) {
252 case kNoSync:
253 return Intrinsics::kUnsafePutLong;
254 case kVolatile:
255 return Intrinsics::kUnsafePutLongVolatile;
256 case kOrdered:
257 return Intrinsics::kUnsafePutLongOrdered;
258 }
259 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800260 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800261 switch (sync) {
262 case kNoSync:
263 return Intrinsics::kUnsafePutObject;
264 case kVolatile:
265 return Intrinsics::kUnsafePutObjectVolatile;
266 case kOrdered:
267 return Intrinsics::kUnsafePutObjectOrdered;
268 }
269 break;
270 default:
271 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
272 UNREACHABLE();
273 }
274 break;
275 }
276
277 // Virtual cases.
278
279 case kIntrinsicReferenceGetReferent:
280 return Intrinsics::kReferenceGetReferent;
281
282 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
283 // compiler to warn on missing cases.
284
285 case kInlineOpNop:
286 case kInlineOpReturnArg:
287 case kInlineOpNonWideConst:
288 case kInlineOpIGet:
289 case kInlineOpIPut:
290 return Intrinsics::kNone;
291
Jeff Hao848f70a2014-01-15 13:49:50 -0800292 // String init cases, not intrinsics.
293
294 case kInlineStringInit:
295 return Intrinsics::kNone;
296
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800297 // No default case to make the compiler warn on missing cases.
298 }
299 return Intrinsics::kNone;
300}
301
302static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) {
303 // The DexFileMethodInliner should have checked whether the methods are agreeing with
304 // what we expect, i.e., static methods are called as such. Add another check here for
305 // our expectations:
306 // Whenever the intrinsic is marked as static-or-direct, report an error if we find an
307 // InvokeVirtual. The other direction is not possible: we have intrinsics for virtual
308 // functions that will perform a check inline. If the precise type is known, however,
309 // the instruction will be sharpened to an InvokeStaticOrDirect.
310 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
311 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
312 invoke->AsInvokeStaticOrDirect()->GetInvokeType() :
313 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
314 switch (intrinsic_type) {
315 case kStatic:
316 return (invoke_type == kStatic);
317 case kDirect:
318 return (invoke_type == kDirect);
319 case kVirtual:
320 // Call might be devirtualized.
321 return (invoke_type == kVirtual || invoke_type == kDirect);
322
323 default:
324 return false;
325 }
326}
327
328// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
329void IntrinsicsRecognizer::Run() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800330 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
331 HBasicBlock* block = it.Current();
332 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
333 inst_it.Advance()) {
334 HInstruction* inst = inst_it.Current();
335 if (inst->IsInvoke()) {
336 HInvoke* invoke = inst->AsInvoke();
337 InlineMethod method;
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100338 DexFileMethodInliner* inliner =
339 driver_->GetMethodInlinerMap()->GetMethodInliner(&invoke->GetDexFile());
340 DCHECK(inliner != nullptr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800341 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
342 Intrinsics intrinsic = GetIntrinsic(method);
343
344 if (intrinsic != Intrinsics::kNone) {
345 if (!CheckInvokeType(intrinsic, invoke)) {
346 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
347 << intrinsic << " for "
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +0100348 << PrettyMethod(invoke->GetDexMethodIndex(), invoke->GetDexFile());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800349 } else {
350 invoke->SetIntrinsic(intrinsic);
351 }
352 }
353 }
354 }
355 }
356 }
357}
358
359std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
360 switch (intrinsic) {
361 case Intrinsics::kNone:
David Brazdil109c89a2015-07-31 17:10:43 +0100362 os << "None";
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800363 break;
364#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
365 case Intrinsics::k ## Name: \
366 os << # Name; \
367 break;
368#include "intrinsics_list.h"
369INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
370#undef STATIC_INTRINSICS_LIST
371#undef VIRTUAL_INTRINSICS_LIST
372#undef OPTIMIZING_INTRINSICS
373 }
374 return os;
375}
376
377} // namespace art