blob: 20aa45f19723d0b4b089662973d2026cc1a7a665 [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"
25
26namespace art {
27
28// Function that returns whether an intrinsic is static/direct or virtual.
29static inline InvokeType GetIntrinsicInvokeType(Intrinsics i) {
30 switch (i) {
31 case Intrinsics::kNone:
32 return kInterface; // Non-sensical for intrinsic.
33#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
34 case Intrinsics::k ## Name: \
35 return IsStatic;
36#include "intrinsics_list.h"
37INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
38#undef INTRINSICS_LIST
39#undef OPTIMIZING_INTRINSICS
40 }
41 return kInterface;
42}
43
44
45
46static Primitive::Type GetType(uint64_t data, bool is_op_size) {
47 if (is_op_size) {
48 switch (static_cast<OpSize>(data)) {
49 case kSignedByte:
Andreas Gampe878d58c2015-01-15 23:24:00 -080050 return Primitive::kPrimByte;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080051 case kSignedHalf:
Andreas Gampe878d58c2015-01-15 23:24:00 -080052 return Primitive::kPrimShort;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080053 case k32:
Andreas Gampe878d58c2015-01-15 23:24:00 -080054 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080055 case k64:
Andreas Gampe878d58c2015-01-15 23:24:00 -080056 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080057 default:
58 LOG(FATAL) << "Unknown/unsupported op size " << data;
59 UNREACHABLE();
60 }
61 } else {
62 if ((data & kIntrinsicFlagIsLong) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080063 return Primitive::kPrimLong;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080064 }
65 if ((data & kIntrinsicFlagIsObject) != 0) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080066 return Primitive::kPrimNot;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080067 }
Andreas Gampe878d58c2015-01-15 23:24:00 -080068 return Primitive::kPrimInt;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080069 }
70}
71
72static Intrinsics GetIntrinsic(InlineMethod method) {
73 switch (method.opcode) {
74 // Floating-point conversions.
75 case kIntrinsicDoubleCvt:
76 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
77 Intrinsics::kDoubleDoubleToRawLongBits : Intrinsics::kDoubleLongBitsToDouble;
78 case kIntrinsicFloatCvt:
79 return ((method.d.data & kIntrinsicFlagToFloatingPoint) == 0) ?
80 Intrinsics::kFloatFloatToRawIntBits : Intrinsics::kFloatIntBitsToFloat;
81
82 // Bit manipulations.
83 case kIntrinsicReverseBits:
84 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080085 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080086 return Intrinsics::kIntegerReverse;
Andreas Gampe878d58c2015-01-15 23:24:00 -080087 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080088 return Intrinsics::kLongReverse;
89 default:
90 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
91 UNREACHABLE();
92 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -080093 case kIntrinsicReverseBytes:
94 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080095 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080096 return Intrinsics::kShortReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -080097 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080098 return Intrinsics::kIntegerReverseBytes;
Andreas Gampe878d58c2015-01-15 23:24:00 -080099 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800100 return Intrinsics::kLongReverseBytes;
101 default:
102 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
103 UNREACHABLE();
104 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800105
106 // Abs.
107 case kIntrinsicAbsDouble:
108 return Intrinsics::kMathAbsDouble;
109 case kIntrinsicAbsFloat:
110 return Intrinsics::kMathAbsFloat;
111 case kIntrinsicAbsInt:
112 return Intrinsics::kMathAbsInt;
113 case kIntrinsicAbsLong:
114 return Intrinsics::kMathAbsLong;
115
116 // Min/max.
117 case kIntrinsicMinMaxDouble:
118 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
119 Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble;
120 case kIntrinsicMinMaxFloat:
121 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
122 Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat;
123 case kIntrinsicMinMaxInt:
124 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
125 Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt;
126 case kIntrinsicMinMaxLong:
127 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
128 Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong;
129
130 // Misc math.
131 case kIntrinsicSqrt:
132 return Intrinsics::kMathSqrt;
133 case kIntrinsicCeil:
134 return Intrinsics::kMathCeil;
135 case kIntrinsicFloor:
136 return Intrinsics::kMathFloor;
137 case kIntrinsicRint:
138 return Intrinsics::kMathRint;
139 case kIntrinsicRoundDouble:
140 return Intrinsics::kMathRoundDouble;
141 case kIntrinsicRoundFloat:
142 return Intrinsics::kMathRoundFloat;
143
144 // System.arraycopy.
145 case kIntrinsicSystemArrayCopyCharArray:
146 return Intrinsics::kSystemArrayCopyChar;
147
148 // Thread.currentThread.
149 case kIntrinsicCurrentThread:
150 return Intrinsics::kThreadCurrentThread;
151
152 // Memory.peek.
153 case kIntrinsicPeek:
154 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800155 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800156 return Intrinsics::kMemoryPeekByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800157 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800158 return Intrinsics::kMemoryPeekShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800159 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800160 return Intrinsics::kMemoryPeekIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800161 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800162 return Intrinsics::kMemoryPeekLongNative;
163 default:
164 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
165 UNREACHABLE();
166 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800167
168 // Memory.poke.
169 case kIntrinsicPoke:
170 switch (GetType(method.d.data, true)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800171 case Primitive::kPrimByte:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800172 return Intrinsics::kMemoryPokeByte;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800173 case Primitive::kPrimShort:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800174 return Intrinsics::kMemoryPokeShortNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800175 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800176 return Intrinsics::kMemoryPokeIntNative;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800177 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800178 return Intrinsics::kMemoryPokeLongNative;
179 default:
180 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
181 UNREACHABLE();
182 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800183
184 // String.
185 case kIntrinsicCharAt:
186 return Intrinsics::kStringCharAt;
187 case kIntrinsicCompareTo:
188 return Intrinsics::kStringCompareTo;
189 case kIntrinsicIsEmptyOrLength:
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -0700190 // The inliner can handle these two cases - and this is the preferred approach
191 // since after inlining the call is no longer visible (as opposed to waiting
192 // until codegen to handle intrinsic).
193 return Intrinsics::kNone;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800194 case kIntrinsicIndexOf:
195 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
196 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
197
198 case kIntrinsicCas:
199 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800200 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800201 return Intrinsics::kUnsafeCASObject;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800202 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800203 return Intrinsics::kUnsafeCASInt;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800204 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800205 return Intrinsics::kUnsafeCASLong;
206 default:
207 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
208 UNREACHABLE();
209 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800210 case kIntrinsicUnsafeGet: {
211 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
212 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800213 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800214 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800215 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800216 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800217 case Primitive::kPrimNot:
218 return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800219 default:
220 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
221 UNREACHABLE();
222 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800223 }
224 case kIntrinsicUnsafePut: {
225 enum Sync { kNoSync, kVolatile, kOrdered };
226 const Sync sync =
227 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
228 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
229 kNoSync;
230 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800231 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800232 switch (sync) {
233 case kNoSync:
234 return Intrinsics::kUnsafePut;
235 case kVolatile:
236 return Intrinsics::kUnsafePutVolatile;
237 case kOrdered:
238 return Intrinsics::kUnsafePutOrdered;
239 }
240 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800241 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800242 switch (sync) {
243 case kNoSync:
244 return Intrinsics::kUnsafePutLong;
245 case kVolatile:
246 return Intrinsics::kUnsafePutLongVolatile;
247 case kOrdered:
248 return Intrinsics::kUnsafePutLongOrdered;
249 }
250 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800251 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800252 switch (sync) {
253 case kNoSync:
254 return Intrinsics::kUnsafePutObject;
255 case kVolatile:
256 return Intrinsics::kUnsafePutObjectVolatile;
257 case kOrdered:
258 return Intrinsics::kUnsafePutObjectOrdered;
259 }
260 break;
261 default:
262 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
263 UNREACHABLE();
264 }
265 break;
266 }
267
268 // Virtual cases.
269
270 case kIntrinsicReferenceGetReferent:
271 return Intrinsics::kReferenceGetReferent;
272
273 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
274 // compiler to warn on missing cases.
275
276 case kInlineOpNop:
277 case kInlineOpReturnArg:
278 case kInlineOpNonWideConst:
279 case kInlineOpIGet:
280 case kInlineOpIPut:
281 return Intrinsics::kNone;
282
283 // No default case to make the compiler warn on missing cases.
284 }
285 return Intrinsics::kNone;
286}
287
288static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) {
289 // The DexFileMethodInliner should have checked whether the methods are agreeing with
290 // what we expect, i.e., static methods are called as such. Add another check here for
291 // our expectations:
292 // Whenever the intrinsic is marked as static-or-direct, report an error if we find an
293 // InvokeVirtual. The other direction is not possible: we have intrinsics for virtual
294 // functions that will perform a check inline. If the precise type is known, however,
295 // the instruction will be sharpened to an InvokeStaticOrDirect.
296 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
297 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
298 invoke->AsInvokeStaticOrDirect()->GetInvokeType() :
299 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
300 switch (intrinsic_type) {
301 case kStatic:
302 return (invoke_type == kStatic);
303 case kDirect:
304 return (invoke_type == kDirect);
305 case kVirtual:
306 // Call might be devirtualized.
307 return (invoke_type == kVirtual || invoke_type == kDirect);
308
309 default:
310 return false;
311 }
312}
313
314// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
315void IntrinsicsRecognizer::Run() {
316 DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(dex_file_);
317 DCHECK(inliner != nullptr);
318
319 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
320 HBasicBlock* block = it.Current();
321 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
322 inst_it.Advance()) {
323 HInstruction* inst = inst_it.Current();
324 if (inst->IsInvoke()) {
325 HInvoke* invoke = inst->AsInvoke();
326 InlineMethod method;
327 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
328 Intrinsics intrinsic = GetIntrinsic(method);
329
330 if (intrinsic != Intrinsics::kNone) {
331 if (!CheckInvokeType(intrinsic, invoke)) {
332 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
333 << intrinsic << " for "
334 << PrettyMethod(invoke->GetDexMethodIndex(), *dex_file_);
335 } else {
336 invoke->SetIntrinsic(intrinsic);
337 }
338 }
339 }
340 }
341 }
342 }
343}
344
345std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
346 switch (intrinsic) {
347 case Intrinsics::kNone:
348 os << "No intrinsic.";
349 break;
350#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
351 case Intrinsics::k ## Name: \
352 os << # Name; \
353 break;
354#include "intrinsics_list.h"
355INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
356#undef STATIC_INTRINSICS_LIST
357#undef VIRTUAL_INTRINSICS_LIST
358#undef OPTIMIZING_INTRINSICS
359 }
360 return os;
361}
362
363} // namespace art
364