blob: 5d3db5c6f29a0bd95a6b5a7c00e4cd0a7a93f200 [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;
Jeff Hao848f70a2014-01-15 13:49:50 -0800189 case kIntrinsicGetCharsNoCheck:
190 return Intrinsics::kStringGetCharsNoCheck;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800191 case kIntrinsicIsEmptyOrLength:
Razvan A Lupusoru3e90a962015-03-27 13:44:44 -0700192 // The inliner can handle these two cases - and this is the preferred approach
193 // since after inlining the call is no longer visible (as opposed to waiting
194 // until codegen to handle intrinsic).
195 return Intrinsics::kNone;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800196 case kIntrinsicIndexOf:
197 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
198 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
Jeff Hao848f70a2014-01-15 13:49:50 -0800199 case kIntrinsicNewStringFromBytes:
200 return Intrinsics::kStringNewStringFromBytes;
201 case kIntrinsicNewStringFromChars:
202 return Intrinsics::kStringNewStringFromChars;
203 case kIntrinsicNewStringFromString:
204 return Intrinsics::kStringNewStringFromString;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800205
206 case kIntrinsicCas:
207 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800208 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800209 return Intrinsics::kUnsafeCASObject;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800210 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800211 return Intrinsics::kUnsafeCASInt;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800212 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800213 return Intrinsics::kUnsafeCASLong;
214 default:
215 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
216 UNREACHABLE();
217 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800218 case kIntrinsicUnsafeGet: {
219 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
220 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800221 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800222 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800223 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800224 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800225 case Primitive::kPrimNot:
226 return is_volatile ? Intrinsics::kUnsafeGetObjectVolatile : Intrinsics::kUnsafeGetObject;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800227 default:
228 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
229 UNREACHABLE();
230 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800231 }
232 case kIntrinsicUnsafePut: {
233 enum Sync { kNoSync, kVolatile, kOrdered };
234 const Sync sync =
235 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
236 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
237 kNoSync;
238 switch (GetType(method.d.data, false)) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800239 case Primitive::kPrimInt:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800240 switch (sync) {
241 case kNoSync:
242 return Intrinsics::kUnsafePut;
243 case kVolatile:
244 return Intrinsics::kUnsafePutVolatile;
245 case kOrdered:
246 return Intrinsics::kUnsafePutOrdered;
247 }
248 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800249 case Primitive::kPrimLong:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800250 switch (sync) {
251 case kNoSync:
252 return Intrinsics::kUnsafePutLong;
253 case kVolatile:
254 return Intrinsics::kUnsafePutLongVolatile;
255 case kOrdered:
256 return Intrinsics::kUnsafePutLongOrdered;
257 }
258 break;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800259 case Primitive::kPrimNot:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800260 switch (sync) {
261 case kNoSync:
262 return Intrinsics::kUnsafePutObject;
263 case kVolatile:
264 return Intrinsics::kUnsafePutObjectVolatile;
265 case kOrdered:
266 return Intrinsics::kUnsafePutObjectOrdered;
267 }
268 break;
269 default:
270 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
271 UNREACHABLE();
272 }
273 break;
274 }
275
276 // Virtual cases.
277
278 case kIntrinsicReferenceGetReferent:
279 return Intrinsics::kReferenceGetReferent;
280
281 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
282 // compiler to warn on missing cases.
283
284 case kInlineOpNop:
285 case kInlineOpReturnArg:
286 case kInlineOpNonWideConst:
287 case kInlineOpIGet:
288 case kInlineOpIPut:
289 return Intrinsics::kNone;
290
Jeff Hao848f70a2014-01-15 13:49:50 -0800291 // String init cases, not intrinsics.
292
293 case kInlineStringInit:
294 return Intrinsics::kNone;
295
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800296 // No default case to make the compiler warn on missing cases.
297 }
298 return Intrinsics::kNone;
299}
300
301static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) {
302 // The DexFileMethodInliner should have checked whether the methods are agreeing with
303 // what we expect, i.e., static methods are called as such. Add another check here for
304 // our expectations:
305 // Whenever the intrinsic is marked as static-or-direct, report an error if we find an
306 // InvokeVirtual. The other direction is not possible: we have intrinsics for virtual
307 // functions that will perform a check inline. If the precise type is known, however,
308 // the instruction will be sharpened to an InvokeStaticOrDirect.
309 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
310 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
311 invoke->AsInvokeStaticOrDirect()->GetInvokeType() :
312 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
313 switch (intrinsic_type) {
314 case kStatic:
315 return (invoke_type == kStatic);
316 case kDirect:
317 return (invoke_type == kDirect);
318 case kVirtual:
319 // Call might be devirtualized.
320 return (invoke_type == kVirtual || invoke_type == kDirect);
321
322 default:
323 return false;
324 }
325}
326
327// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
328void IntrinsicsRecognizer::Run() {
329 DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(dex_file_);
330 DCHECK(inliner != nullptr);
331
332 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
333 HBasicBlock* block = it.Current();
334 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
335 inst_it.Advance()) {
336 HInstruction* inst = inst_it.Current();
337 if (inst->IsInvoke()) {
338 HInvoke* invoke = inst->AsInvoke();
339 InlineMethod method;
340 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
341 Intrinsics intrinsic = GetIntrinsic(method);
342
343 if (intrinsic != Intrinsics::kNone) {
344 if (!CheckInvokeType(intrinsic, invoke)) {
345 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
346 << intrinsic << " for "
347 << PrettyMethod(invoke->GetDexMethodIndex(), *dex_file_);
348 } else {
349 invoke->SetIntrinsic(intrinsic);
350 }
351 }
352 }
353 }
354 }
355 }
356}
357
358std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
359 switch (intrinsic) {
360 case Intrinsics::kNone:
361 os << "No intrinsic.";
362 break;
363#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
364 case Intrinsics::k ## Name: \
365 os << # Name; \
366 break;
367#include "intrinsics_list.h"
368INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
369#undef STATIC_INTRINSICS_LIST
370#undef VIRTUAL_INTRINSICS_LIST
371#undef OPTIMIZING_INTRINSICS
372 }
373 return os;
374}
375
376} // namespace art
377