blob: fe0e7f2eb2d4cfdf3096db9fe870bb7ff9fb26ae [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:
50 return Primitive::Type::kPrimByte;
51 case kSignedHalf:
52 return Primitive::Type::kPrimShort;
53 case k32:
54 return Primitive::Type::kPrimInt;
55 case k64:
56 return Primitive::Type::kPrimLong;
57 default:
58 LOG(FATAL) << "Unknown/unsupported op size " << data;
59 UNREACHABLE();
60 }
61 } else {
62 if ((data & kIntrinsicFlagIsLong) != 0) {
63 return Primitive::Type::kPrimLong;
64 }
65 if ((data & kIntrinsicFlagIsObject) != 0) {
66 return Primitive::Type::kPrimNot;
67 }
68 return Primitive::Type::kPrimInt;
69 }
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)) {
85 case Primitive::Type::kPrimInt:
86 return Intrinsics::kIntegerReverse;
87 case Primitive::Type::kPrimLong:
88 return Intrinsics::kLongReverse;
89 default:
90 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
91 UNREACHABLE();
92 }
93 break;
94 case kIntrinsicReverseBytes:
95 switch (GetType(method.d.data, true)) {
96 case Primitive::Type::kPrimShort:
97 return Intrinsics::kShortReverseBytes;
98 case Primitive::Type::kPrimInt:
99 return Intrinsics::kIntegerReverseBytes;
100 case Primitive::Type::kPrimLong:
101 return Intrinsics::kLongReverseBytes;
102 default:
103 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
104 UNREACHABLE();
105 }
106 break;
107
108 // Abs.
109 case kIntrinsicAbsDouble:
110 return Intrinsics::kMathAbsDouble;
111 case kIntrinsicAbsFloat:
112 return Intrinsics::kMathAbsFloat;
113 case kIntrinsicAbsInt:
114 return Intrinsics::kMathAbsInt;
115 case kIntrinsicAbsLong:
116 return Intrinsics::kMathAbsLong;
117
118 // Min/max.
119 case kIntrinsicMinMaxDouble:
120 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
121 Intrinsics::kMathMaxDoubleDouble : Intrinsics::kMathMinDoubleDouble;
122 case kIntrinsicMinMaxFloat:
123 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
124 Intrinsics::kMathMaxFloatFloat : Intrinsics::kMathMinFloatFloat;
125 case kIntrinsicMinMaxInt:
126 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
127 Intrinsics::kMathMaxIntInt : Intrinsics::kMathMinIntInt;
128 case kIntrinsicMinMaxLong:
129 return ((method.d.data & kIntrinsicFlagMin) == 0) ?
130 Intrinsics::kMathMaxLongLong : Intrinsics::kMathMinLongLong;
131
132 // Misc math.
133 case kIntrinsicSqrt:
134 return Intrinsics::kMathSqrt;
135 case kIntrinsicCeil:
136 return Intrinsics::kMathCeil;
137 case kIntrinsicFloor:
138 return Intrinsics::kMathFloor;
139 case kIntrinsicRint:
140 return Intrinsics::kMathRint;
141 case kIntrinsicRoundDouble:
142 return Intrinsics::kMathRoundDouble;
143 case kIntrinsicRoundFloat:
144 return Intrinsics::kMathRoundFloat;
145
146 // System.arraycopy.
147 case kIntrinsicSystemArrayCopyCharArray:
148 return Intrinsics::kSystemArrayCopyChar;
149
150 // Thread.currentThread.
151 case kIntrinsicCurrentThread:
152 return Intrinsics::kThreadCurrentThread;
153
154 // Memory.peek.
155 case kIntrinsicPeek:
156 switch (GetType(method.d.data, true)) {
157 case Primitive::Type::kPrimByte:
158 return Intrinsics::kMemoryPeekByte;
159 case Primitive::Type::kPrimShort:
160 return Intrinsics::kMemoryPeekShortNative;
161 case Primitive::Type::kPrimInt:
162 return Intrinsics::kMemoryPeekIntNative;
163 case Primitive::Type::kPrimLong:
164 return Intrinsics::kMemoryPeekLongNative;
165 default:
166 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
167 UNREACHABLE();
168 }
169 break;
170
171 // Memory.poke.
172 case kIntrinsicPoke:
173 switch (GetType(method.d.data, true)) {
174 case Primitive::Type::kPrimByte:
175 return Intrinsics::kMemoryPokeByte;
176 case Primitive::Type::kPrimShort:
177 return Intrinsics::kMemoryPokeShortNative;
178 case Primitive::Type::kPrimInt:
179 return Intrinsics::kMemoryPokeIntNative;
180 case Primitive::Type::kPrimLong:
181 return Intrinsics::kMemoryPokeLongNative;
182 default:
183 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
184 UNREACHABLE();
185 }
186 break;
187
188 // String.
189 case kIntrinsicCharAt:
190 return Intrinsics::kStringCharAt;
191 case kIntrinsicCompareTo:
192 return Intrinsics::kStringCompareTo;
193 case kIntrinsicIsEmptyOrLength:
194 return ((method.d.data & kIntrinsicFlagIsEmpty) == 0) ?
195 Intrinsics::kStringLength : Intrinsics::kStringIsEmpty;
196 case kIntrinsicIndexOf:
197 return ((method.d.data & kIntrinsicFlagBase0) == 0) ?
198 Intrinsics::kStringIndexOfAfter : Intrinsics::kStringIndexOf;
199
200 case kIntrinsicCas:
201 switch (GetType(method.d.data, false)) {
202 case Primitive::Type::kPrimNot:
203 return Intrinsics::kUnsafeCASObject;
204 case Primitive::Type::kPrimInt:
205 return Intrinsics::kUnsafeCASInt;
206 case Primitive::Type::kPrimLong:
207 return Intrinsics::kUnsafeCASLong;
208 default:
209 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
210 UNREACHABLE();
211 }
212 break;
213 case kIntrinsicUnsafeGet: {
214 const bool is_volatile = (method.d.data & kIntrinsicFlagIsVolatile);
215 switch (GetType(method.d.data, false)) {
216 case Primitive::Type::kPrimInt:
217 return is_volatile ? Intrinsics::kUnsafeGetVolatile : Intrinsics::kUnsafeGet;
218 case Primitive::Type::kPrimLong:
219 return is_volatile ? Intrinsics::kUnsafeGetLongVolatile : Intrinsics::kUnsafeGetLong;
220 default:
221 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
222 UNREACHABLE();
223 }
224 break;
225 }
226 case kIntrinsicUnsafePut: {
227 enum Sync { kNoSync, kVolatile, kOrdered };
228 const Sync sync =
229 ((method.d.data & kIntrinsicFlagIsVolatile) != 0) ? kVolatile :
230 ((method.d.data & kIntrinsicFlagIsOrdered) != 0) ? kOrdered :
231 kNoSync;
232 switch (GetType(method.d.data, false)) {
233 case Primitive::Type::kPrimInt:
234 switch (sync) {
235 case kNoSync:
236 return Intrinsics::kUnsafePut;
237 case kVolatile:
238 return Intrinsics::kUnsafePutVolatile;
239 case kOrdered:
240 return Intrinsics::kUnsafePutOrdered;
241 }
242 break;
243 case Primitive::Type::kPrimLong:
244 switch (sync) {
245 case kNoSync:
246 return Intrinsics::kUnsafePutLong;
247 case kVolatile:
248 return Intrinsics::kUnsafePutLongVolatile;
249 case kOrdered:
250 return Intrinsics::kUnsafePutLongOrdered;
251 }
252 break;
253 case Primitive::Type::kPrimNot:
254 switch (sync) {
255 case kNoSync:
256 return Intrinsics::kUnsafePutObject;
257 case kVolatile:
258 return Intrinsics::kUnsafePutObjectVolatile;
259 case kOrdered:
260 return Intrinsics::kUnsafePutObjectOrdered;
261 }
262 break;
263 default:
264 LOG(FATAL) << "Unknown/unsupported op size " << method.d.data;
265 UNREACHABLE();
266 }
267 break;
268 }
269
270 // Virtual cases.
271
272 case kIntrinsicReferenceGetReferent:
273 return Intrinsics::kReferenceGetReferent;
274
275 // Quick inliner cases. Remove after refactoring. They are here so that we can use the
276 // compiler to warn on missing cases.
277
278 case kInlineOpNop:
279 case kInlineOpReturnArg:
280 case kInlineOpNonWideConst:
281 case kInlineOpIGet:
282 case kInlineOpIPut:
283 return Intrinsics::kNone;
284
285 // No default case to make the compiler warn on missing cases.
286 }
287 return Intrinsics::kNone;
288}
289
290static bool CheckInvokeType(Intrinsics intrinsic, HInvoke* invoke) {
291 // The DexFileMethodInliner should have checked whether the methods are agreeing with
292 // what we expect, i.e., static methods are called as such. Add another check here for
293 // our expectations:
294 // Whenever the intrinsic is marked as static-or-direct, report an error if we find an
295 // InvokeVirtual. The other direction is not possible: we have intrinsics for virtual
296 // functions that will perform a check inline. If the precise type is known, however,
297 // the instruction will be sharpened to an InvokeStaticOrDirect.
298 InvokeType intrinsic_type = GetIntrinsicInvokeType(intrinsic);
299 InvokeType invoke_type = invoke->IsInvokeStaticOrDirect() ?
300 invoke->AsInvokeStaticOrDirect()->GetInvokeType() :
301 invoke->IsInvokeVirtual() ? kVirtual : kSuper;
302 switch (intrinsic_type) {
303 case kStatic:
304 return (invoke_type == kStatic);
305 case kDirect:
306 return (invoke_type == kDirect);
307 case kVirtual:
308 // Call might be devirtualized.
309 return (invoke_type == kVirtual || invoke_type == kDirect);
310
311 default:
312 return false;
313 }
314}
315
316// TODO: Refactor DexFileMethodInliner and have something nicer than InlineMethod.
317void IntrinsicsRecognizer::Run() {
318 DexFileMethodInliner* inliner = driver_->GetMethodInlinerMap()->GetMethodInliner(dex_file_);
319 DCHECK(inliner != nullptr);
320
321 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
322 HBasicBlock* block = it.Current();
323 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
324 inst_it.Advance()) {
325 HInstruction* inst = inst_it.Current();
326 if (inst->IsInvoke()) {
327 HInvoke* invoke = inst->AsInvoke();
328 InlineMethod method;
329 if (inliner->IsIntrinsic(invoke->GetDexMethodIndex(), &method)) {
330 Intrinsics intrinsic = GetIntrinsic(method);
331
332 if (intrinsic != Intrinsics::kNone) {
333 if (!CheckInvokeType(intrinsic, invoke)) {
334 LOG(WARNING) << "Found an intrinsic with unexpected invoke type: "
335 << intrinsic << " for "
336 << PrettyMethod(invoke->GetDexMethodIndex(), *dex_file_);
337 } else {
338 invoke->SetIntrinsic(intrinsic);
339 }
340 }
341 }
342 }
343 }
344 }
345}
346
347std::ostream& operator<<(std::ostream& os, const Intrinsics& intrinsic) {
348 switch (intrinsic) {
349 case Intrinsics::kNone:
350 os << "No intrinsic.";
351 break;
352#define OPTIMIZING_INTRINSICS(Name, IsStatic) \
353 case Intrinsics::k ## Name: \
354 os << # Name; \
355 break;
356#include "intrinsics_list.h"
357INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
358#undef STATIC_INTRINSICS_LIST
359#undef VIRTUAL_INTRINSICS_LIST
360#undef OPTIMIZING_INTRINSICS
361 }
362 return os;
363}
364
365} // namespace art
366