blob: 993648f76505254337949405495fd67e4b79eaf1 [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#ifndef ART_COMPILER_OPTIMIZING_INTRINSICS_H_
18#define ART_COMPILER_OPTIMIZING_INTRINSICS_H_
19
Roland Levillainec525fc2015-04-28 15:50:20 +010020#include "code_generator.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080021#include "nodes.h"
22#include "optimization.h"
Roland Levillainec525fc2015-04-28 15:50:20 +010023#include "parallel_move_resolver.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024
25namespace art {
26
27class CompilerDriver;
28class DexFile;
29
Anton Kirilova3ffea22016-04-07 17:02:37 +010030// Positive floating-point infinities.
31static constexpr uint32_t kPositiveInfinityFloat = 0x7f800000U;
32static constexpr uint64_t kPositiveInfinityDouble = UINT64_C(0x7ff0000000000000);
33
xueliang.zhongc032e742016-03-28 16:44:32 +010034static constexpr uint32_t kNanFloat = 0x7fc00000U;
35static constexpr uint64_t kNanDouble = 0x7ff8000000000000;
36
Andreas Gampe71fb52f2014-12-29 17:43:08 -080037// Recognize intrinsics from HInvoke nodes.
38class IntrinsicsRecognizer : public HOptimization {
39 public:
Aart Bik2ca10eb2017-11-15 15:17:53 -080040 IntrinsicsRecognizer(HGraph* graph,
41 OptimizingCompilerStats* stats,
42 const char* name = kIntrinsicsRecognizerPassName)
43 : HOptimization(graph, name, stats) {}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080044
Aart Bik24773202018-04-26 10:28:51 -070045 bool Run() OVERRIDE;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080046
Aart Bikf0010dd2017-11-21 16:31:53 -080047 // Static helper that recognizes intrinsic call. Returns true on success.
48 // If it fails due to invoke type mismatch, wrong_invoke_type is set.
Orion Hodson4c71d002017-11-29 11:03:25 +000049 // Useful to recognize intrinsics on individual calls outside this full pass.
Mingyao Yang6b1aebe2017-11-27 15:39:04 -080050 static bool Recognize(HInvoke* invoke, ArtMethod* method, /*out*/ bool* wrong_invoke_type)
Orion Hodson4c71d002017-11-29 11:03:25 +000051 REQUIRES_SHARED(Locks::mutator_lock_);
Aart Bikf0010dd2017-11-21 16:31:53 -080052
Andreas Gampe7c3952f2015-02-19 18:21:24 -080053 static constexpr const char* kIntrinsicsRecognizerPassName = "intrinsics_recognition";
54
Andreas Gampe71fb52f2014-12-29 17:43:08 -080055 private:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080056 DISALLOW_COPY_AND_ASSIGN(IntrinsicsRecognizer);
57};
58
59class IntrinsicVisitor : public ValueObject {
60 public:
61 virtual ~IntrinsicVisitor() {}
62
63 // Dispatch logic.
64
65 void Dispatch(HInvoke* invoke) {
66 switch (invoke->GetIntrinsic()) {
67 case Intrinsics::kNone:
68 return;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010069#define OPTIMIZING_INTRINSICS(Name, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080070 case Intrinsics::k ## Name: \
71 Visit ## Name(invoke); \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080072 return;
73#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070074 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -080075#undef INTRINSICS_LIST
76#undef OPTIMIZING_INTRINSICS
77
78 // Do not put a default case. That way the compiler will complain if we missed a case.
79 }
80 }
81
82 // Define visitor methods.
83
Nicolas Geoffray762869d2016-07-15 15:28:35 +010084#define OPTIMIZING_INTRINSICS(Name, ...) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080085 virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
86 }
87#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070088 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -080089#undef INTRINSICS_LIST
90#undef OPTIMIZING_INTRINSICS
91
Roland Levillainec525fc2015-04-28 15:50:20 +010092 static void MoveArguments(HInvoke* invoke,
93 CodeGenerator* codegen,
94 InvokeDexCallingConventionVisitor* calling_convention_visitor) {
95 if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) {
96 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
David Brazdil58282f42016-01-14 12:45:10 +000097 // Explicit clinit checks triggered by static invokes must have been
98 // pruned by art::PrepareForRegisterAllocation.
99 DCHECK(!invoke_static_or_direct->IsStaticWithExplicitClinitCheck());
Roland Levillainec525fc2015-04-28 15:50:20 +0100100 }
101
102 if (invoke->GetNumberOfArguments() == 0) {
103 // No argument to move.
104 return;
105 }
106
107 LocationSummary* locations = invoke->GetLocations();
108
109 // We're moving potentially two or more locations to locations that could overlap, so we need
110 // a parallel move resolver.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100111 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillainec525fc2015-04-28 15:50:20 +0100112
113 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
114 HInstruction* input = invoke->InputAt(i);
115 Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType());
116 Location actual_loc = locations->InAt(i);
117
118 parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr);
119 }
120
121 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
122 }
123
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000124 static void ComputeIntegerValueOfLocations(HInvoke* invoke,
125 CodeGenerator* codegen,
126 Location return_location,
127 Location first_argument_location);
128
Vladimir Markoeebb8212018-06-05 14:57:24 +0100129 // Temporary data structure for holding Integer.valueOf data for generating code.
130 // We only use it if the boot image contains the IntegerCache objects.
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000131 struct IntegerValueOfInfo {
Vladimir Marko6fd16062018-06-26 11:02:04 +0100132 static constexpr uint32_t kInvalidReference = static_cast<uint32_t>(-1);
133
Vladimir Markoeebb8212018-06-05 14:57:24 +0100134 IntegerValueOfInfo();
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000135
Vladimir Markoeebb8212018-06-05 14:57:24 +0100136 // Offset of the Integer.value field for initializing a newly allocated instance.
137 uint32_t value_offset;
138 // The low value in the cache.
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000139 int32_t low;
Vladimir Markoeebb8212018-06-05 14:57:24 +0100140 // The length of the cache array.
141 uint32_t length;
142
Vladimir Marko6fd16062018-06-26 11:02:04 +0100143 // Boot image offset of java.lang.Integer for allocating an instance.
144 uint32_t integer_boot_image_offset; // Set to kInvalidReference when compiling the boot image.
Vladimir Markoeebb8212018-06-05 14:57:24 +0100145
Vladimir Marko6fd16062018-06-26 11:02:04 +0100146 // This union contains references to the boot image. For app AOT or JIT compilation,
147 // these are the boot image offsets of the target. For boot image compilation, the
148 // location shall be known only at link time, so we encode a symbolic reference using
149 // IntrinsicObjects::EncodePatch().
150 union {
151 // The target value for a constant input in the cache range. If the constant input
152 // is out of range (use `low` and `length` to check), this value is bogus (set to
153 // kInvalidReference) and the code must allocate a new Integer.
154 uint32_t value_boot_image_reference;
155
156 // The cache array data used for a non-constant input in the cache range.
Vladimir Markoeebb8212018-06-05 14:57:24 +0100157 // If the input is out of range, the code must allocate a new Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +0100158 uint32_t array_data_boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +0100159 };
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000160 };
161
Vladimir Marko6fd16062018-06-26 11:02:04 +0100162 static IntegerValueOfInfo ComputeIntegerValueOfInfo(
163 HInvoke* invoke, const CompilerOptions& compiler_options);
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000164
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800165 protected:
166 IntrinsicVisitor() {}
167
168 private:
169 DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor);
170};
171
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100172#define GENERIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100173public: \
174void Set##name() { SetBit(k##name); } \
175bool Get##name() const { return IsBitSet(k##name); } \
176private: \
Roland Levillainebea3d22016-04-12 15:42:57 +0100177static constexpr size_t k##name = bit
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100178
179class IntrinsicOptimizations : public ValueObject {
180 public:
Roland Levillainebea3d22016-04-12 15:42:57 +0100181 explicit IntrinsicOptimizations(HInvoke* invoke)
182 : value_(invoke->GetIntrinsicOptimizations()) {}
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100183 explicit IntrinsicOptimizations(const HInvoke& invoke)
184 : value_(invoke.GetIntrinsicOptimizations()) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100185
186 static constexpr int kNumberOfGenericOptimizations = 2;
187 GENERIC_OPTIMIZATION(DoesNotNeedDexCache, 0);
188 GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 1);
189
190 protected:
191 bool IsBitSet(uint32_t bit) const {
Roland Levillainebea3d22016-04-12 15:42:57 +0100192 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100193 return (*value_ & (1 << bit)) != 0u;
194 }
195
196 void SetBit(uint32_t bit) {
Roland Levillainebea3d22016-04-12 15:42:57 +0100197 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
198 *(const_cast<uint32_t* const>(value_)) |= (1 << bit);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100199 }
200
201 private:
Roland Levillainebea3d22016-04-12 15:42:57 +0100202 const uint32_t* const value_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100203
204 DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations);
205};
206
207#undef GENERIC_OPTIMIZATION
208
209#define INTRINSIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100210public: \
211void Set##name() { SetBit(k##name); } \
212bool Get##name() const { return IsBitSet(k##name); } \
213private: \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700214static constexpr size_t k##name = (bit) + kNumberOfGenericOptimizations
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100215
216class StringEqualsOptimizations : public IntrinsicOptimizations {
217 public:
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100218 explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100219
220 INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0);
221 INTRINSIC_OPTIMIZATION(ArgumentIsString, 1);
Vladimir Markoda283052017-11-07 21:17:24 +0000222 INTRINSIC_OPTIMIZATION(NoReadBarrierForStringClass, 2);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100223
224 private:
225 DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations);
226};
227
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100228class SystemArrayCopyOptimizations : public IntrinsicOptimizations {
229 public:
230 explicit SystemArrayCopyOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
231
232 INTRINSIC_OPTIMIZATION(SourceIsNotNull, 0);
233 INTRINSIC_OPTIMIZATION(DestinationIsNotNull, 1);
234 INTRINSIC_OPTIMIZATION(DestinationIsSource, 2);
235 INTRINSIC_OPTIMIZATION(CountIsSourceLength, 3);
236 INTRINSIC_OPTIMIZATION(CountIsDestinationLength, 4);
237 INTRINSIC_OPTIMIZATION(DoesNotNeedTypeCheck, 5);
238 INTRINSIC_OPTIMIZATION(DestinationIsTypedObjectArray, 6);
239 INTRINSIC_OPTIMIZATION(DestinationIsNonPrimitiveArray, 7);
240 INTRINSIC_OPTIMIZATION(DestinationIsPrimitiveArray, 8);
241 INTRINSIC_OPTIMIZATION(SourceIsNonPrimitiveArray, 9);
242 INTRINSIC_OPTIMIZATION(SourceIsPrimitiveArray, 10);
243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(SystemArrayCopyOptimizations);
246};
247
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100248#undef INTRISIC_OPTIMIZATION
249
Aart Bik2f9fcc92016-03-01 15:16:54 -0800250//
251// Macros for use in the intrinsics code generators.
252//
253
254// Defines an unimplemented intrinsic: that is, a method call that is recognized as an
255// intrinsic to exploit e.g. no side-effects or exceptions, but otherwise not handled
256// by this architecture-specific intrinsics code generator. Eventually it is implemented
257// as a true method call.
258#define UNIMPLEMENTED_INTRINSIC(Arch, Name) \
259void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
260} \
261void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
262}
263
264// Defines a list of unreached intrinsics: that is, method calls that are recognized as
265// an intrinsic, and then always converted into HIR instructions before they reach any
266// architecture-specific intrinsics code generator.
267#define UNREACHABLE_INTRINSIC(Arch, Name) \
268void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke) { \
269 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
270 << " should have been converted to HIR"; \
271} \
272void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke) { \
273 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
274 << " should have been converted to HIR"; \
275}
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100276#define UNREACHABLE_INTRINSICS(Arch) \
Aart Bik1f8d51b2018-02-15 10:42:37 -0800277UNREACHABLE_INTRINSIC(Arch, MathMinIntInt) \
278UNREACHABLE_INTRINSIC(Arch, MathMinLongLong) \
279UNREACHABLE_INTRINSIC(Arch, MathMinFloatFloat) \
280UNREACHABLE_INTRINSIC(Arch, MathMinDoubleDouble) \
281UNREACHABLE_INTRINSIC(Arch, MathMaxIntInt) \
282UNREACHABLE_INTRINSIC(Arch, MathMaxLongLong) \
283UNREACHABLE_INTRINSIC(Arch, MathMaxFloatFloat) \
284UNREACHABLE_INTRINSIC(Arch, MathMaxDoubleDouble) \
Aart Bik3dad3412018-02-28 12:01:46 -0800285UNREACHABLE_INTRINSIC(Arch, MathAbsInt) \
286UNREACHABLE_INTRINSIC(Arch, MathAbsLong) \
287UNREACHABLE_INTRINSIC(Arch, MathAbsFloat) \
288UNREACHABLE_INTRINSIC(Arch, MathAbsDouble) \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100289UNREACHABLE_INTRINSIC(Arch, FloatFloatToIntBits) \
290UNREACHABLE_INTRINSIC(Arch, DoubleDoubleToLongBits) \
291UNREACHABLE_INTRINSIC(Arch, FloatIsNaN) \
292UNREACHABLE_INTRINSIC(Arch, DoubleIsNaN) \
293UNREACHABLE_INTRINSIC(Arch, IntegerRotateLeft) \
294UNREACHABLE_INTRINSIC(Arch, LongRotateLeft) \
295UNREACHABLE_INTRINSIC(Arch, IntegerRotateRight) \
296UNREACHABLE_INTRINSIC(Arch, LongRotateRight) \
297UNREACHABLE_INTRINSIC(Arch, IntegerCompare) \
298UNREACHABLE_INTRINSIC(Arch, LongCompare) \
299UNREACHABLE_INTRINSIC(Arch, IntegerSignum) \
300UNREACHABLE_INTRINSIC(Arch, LongSignum) \
301UNREACHABLE_INTRINSIC(Arch, StringCharAt) \
302UNREACHABLE_INTRINSIC(Arch, StringIsEmpty) \
303UNREACHABLE_INTRINSIC(Arch, StringLength) \
304UNREACHABLE_INTRINSIC(Arch, UnsafeLoadFence) \
305UNREACHABLE_INTRINSIC(Arch, UnsafeStoreFence) \
306UNREACHABLE_INTRINSIC(Arch, UnsafeFullFence) \
307UNREACHABLE_INTRINSIC(Arch, VarHandleFullFence) \
308UNREACHABLE_INTRINSIC(Arch, VarHandleAcquireFence) \
309UNREACHABLE_INTRINSIC(Arch, VarHandleReleaseFence) \
310UNREACHABLE_INTRINSIC(Arch, VarHandleLoadLoadFence) \
311UNREACHABLE_INTRINSIC(Arch, VarHandleStoreStoreFence) \
312UNREACHABLE_INTRINSIC(Arch, MethodHandleInvokeExact) \
313UNREACHABLE_INTRINSIC(Arch, MethodHandleInvoke) \
314UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndExchange) \
315UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndExchangeAcquire) \
316UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndExchangeRelease) \
317UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndSet) \
318UNREACHABLE_INTRINSIC(Arch, VarHandleGet) \
319UNREACHABLE_INTRINSIC(Arch, VarHandleGetAcquire) \
320UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndAdd) \
321UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndAddAcquire) \
322UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndAddRelease) \
323UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseAnd) \
324UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseAndAcquire) \
325UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseAndRelease) \
326UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseOr) \
327UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseOrAcquire) \
328UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseOrRelease) \
329UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseXor) \
330UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseXorAcquire) \
331UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseXorRelease) \
332UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndSet) \
333UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndSetAcquire) \
334UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndSetRelease) \
335UNREACHABLE_INTRINSIC(Arch, VarHandleGetOpaque) \
336UNREACHABLE_INTRINSIC(Arch, VarHandleGetVolatile) \
337UNREACHABLE_INTRINSIC(Arch, VarHandleSet) \
338UNREACHABLE_INTRINSIC(Arch, VarHandleSetOpaque) \
339UNREACHABLE_INTRINSIC(Arch, VarHandleSetRelease) \
340UNREACHABLE_INTRINSIC(Arch, VarHandleSetVolatile) \
341UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSet) \
342UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSetAcquire) \
343UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSetPlain) \
344UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSetRelease)
Aart Bik2f9fcc92016-03-01 15:16:54 -0800345
Vladimir Marko68c981f2016-08-26 13:13:33 +0100346template <typename IntrinsicLocationsBuilder, typename Codegenerator>
347bool IsCallFreeIntrinsic(HInvoke* invoke, Codegenerator* codegen) {
348 if (invoke->GetIntrinsic() != Intrinsics::kNone) {
349 // This invoke may have intrinsic code generation defined. However, we must
350 // now also determine if this code generation is truly there and call-free
351 // (not unimplemented, no bail on instruction features, or call on slow path).
352 // This is done by actually calling the locations builder on the instruction
353 // and clearing out the locations once result is known. We assume this
354 // call only has creating locations as side effects!
355 // TODO: Avoid wasting Arena memory.
356 IntrinsicLocationsBuilder builder(codegen);
357 bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall();
358 invoke->SetLocations(nullptr);
359 return success;
360 }
361 return false;
362}
363
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800364} // namespace art
365
366#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_