blob: a33ed8b4fe8ea63427240cb3183be609bfde77a7 [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
Vladimir Markoe2727152019-10-10 10:46:42 +010020#include "base/macros.h"
Roland Levillainec525fc2015-04-28 15:50:20 +010021#include "code_generator.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080022#include "nodes.h"
23#include "optimization.h"
Roland Levillainec525fc2015-04-28 15:50:20 +010024#include "parallel_move_resolver.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080025
Vladimir Markoe2727152019-10-10 10:46:42 +010026namespace art HIDDEN {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027
Andreas Gampe71fb52f2014-12-29 17:43:08 -080028class 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 -080037class IntrinsicVisitor : public ValueObject {
38 public:
39 virtual ~IntrinsicVisitor() {}
40
41 // Dispatch logic.
42
43 void Dispatch(HInvoke* invoke) {
44 switch (invoke->GetIntrinsic()) {
45 case Intrinsics::kNone:
46 return;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010047#define OPTIMIZING_INTRINSICS(Name, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080048 case Intrinsics::k ## Name: \
49 Visit ## Name(invoke); \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080050 return;
51#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070052 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -080053#undef INTRINSICS_LIST
54#undef OPTIMIZING_INTRINSICS
55
56 // Do not put a default case. That way the compiler will complain if we missed a case.
57 }
58 }
59
60 // Define visitor methods.
61
Nicolas Geoffray762869d2016-07-15 15:28:35 +010062#define OPTIMIZING_INTRINSICS(Name, ...) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080063 virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
64 }
65#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070066 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -080067#undef INTRINSICS_LIST
68#undef OPTIMIZING_INTRINSICS
69
Roland Levillainec525fc2015-04-28 15:50:20 +010070 static void MoveArguments(HInvoke* invoke,
71 CodeGenerator* codegen,
72 InvokeDexCallingConventionVisitor* calling_convention_visitor) {
73 if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) {
74 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
David Brazdil58282f42016-01-14 12:45:10 +000075 // Explicit clinit checks triggered by static invokes must have been
76 // pruned by art::PrepareForRegisterAllocation.
77 DCHECK(!invoke_static_or_direct->IsStaticWithExplicitClinitCheck());
Roland Levillainec525fc2015-04-28 15:50:20 +010078 }
79
80 if (invoke->GetNumberOfArguments() == 0) {
81 // No argument to move.
82 return;
83 }
84
85 LocationSummary* locations = invoke->GetLocations();
86
87 // We're moving potentially two or more locations to locations that could overlap, so we need
88 // a parallel move resolver.
Vladimir Markoca6fff82017-10-03 14:49:14 +010089 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillainec525fc2015-04-28 15:50:20 +010090
91 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
92 HInstruction* input = invoke->InputAt(i);
93 Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType());
94 Location actual_loc = locations->InAt(i);
95
96 parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr);
97 }
98
99 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
100 }
101
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000102 static void ComputeIntegerValueOfLocations(HInvoke* invoke,
103 CodeGenerator* codegen,
104 Location return_location,
105 Location first_argument_location);
106
Vladimir Markoeebb8212018-06-05 14:57:24 +0100107 // Temporary data structure for holding Integer.valueOf data for generating code.
108 // We only use it if the boot image contains the IntegerCache objects.
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000109 struct IntegerValueOfInfo {
Vladimir Marko6fd16062018-06-26 11:02:04 +0100110 static constexpr uint32_t kInvalidReference = static_cast<uint32_t>(-1);
111
Vladimir Markoeebb8212018-06-05 14:57:24 +0100112 IntegerValueOfInfo();
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000113
Vladimir Markoeebb8212018-06-05 14:57:24 +0100114 // Offset of the Integer.value field for initializing a newly allocated instance.
115 uint32_t value_offset;
116 // The low value in the cache.
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000117 int32_t low;
Vladimir Markoeebb8212018-06-05 14:57:24 +0100118 // The length of the cache array.
119 uint32_t length;
120
Vladimir Marko6fd16062018-06-26 11:02:04 +0100121 // Boot image offset of java.lang.Integer for allocating an instance.
122 uint32_t integer_boot_image_offset; // Set to kInvalidReference when compiling the boot image.
Vladimir Markoeebb8212018-06-05 14:57:24 +0100123
Vladimir Marko6fd16062018-06-26 11:02:04 +0100124 // This union contains references to the boot image. For app AOT or JIT compilation,
125 // these are the boot image offsets of the target. For boot image compilation, the
126 // location shall be known only at link time, so we encode a symbolic reference using
127 // IntrinsicObjects::EncodePatch().
128 union {
129 // The target value for a constant input in the cache range. If the constant input
130 // is out of range (use `low` and `length` to check), this value is bogus (set to
131 // kInvalidReference) and the code must allocate a new Integer.
132 uint32_t value_boot_image_reference;
133
134 // The cache array data used for a non-constant input in the cache range.
Vladimir Markoeebb8212018-06-05 14:57:24 +0100135 // If the input is out of range, the code must allocate a new Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +0100136 uint32_t array_data_boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +0100137 };
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000138 };
139
Vladimir Marko6fd16062018-06-26 11:02:04 +0100140 static IntegerValueOfInfo ComputeIntegerValueOfInfo(
141 HInvoke* invoke, const CompilerOptions& compiler_options);
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000142
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800143 protected:
144 IntrinsicVisitor() {}
145
Roland Levillain1d775d22018-09-07 13:56:57 +0100146 static void AssertNonMovableStringClass();
147
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800148 private:
149 DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor);
150};
151
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100152#define GENERIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100153public: \
154void Set##name() { SetBit(k##name); } \
155bool Get##name() const { return IsBitSet(k##name); } \
156private: \
Roland Levillainebea3d22016-04-12 15:42:57 +0100157static constexpr size_t k##name = bit
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100158
159class IntrinsicOptimizations : public ValueObject {
160 public:
Roland Levillainebea3d22016-04-12 15:42:57 +0100161 explicit IntrinsicOptimizations(HInvoke* invoke)
162 : value_(invoke->GetIntrinsicOptimizations()) {}
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100163 explicit IntrinsicOptimizations(const HInvoke& invoke)
164 : value_(invoke.GetIntrinsicOptimizations()) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100165
166 static constexpr int kNumberOfGenericOptimizations = 2;
167 GENERIC_OPTIMIZATION(DoesNotNeedDexCache, 0);
168 GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 1);
169
170 protected:
171 bool IsBitSet(uint32_t bit) const {
Roland Levillainebea3d22016-04-12 15:42:57 +0100172 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100173 return (*value_ & (1 << bit)) != 0u;
174 }
175
176 void SetBit(uint32_t bit) {
Roland Levillainebea3d22016-04-12 15:42:57 +0100177 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
178 *(const_cast<uint32_t* const>(value_)) |= (1 << bit);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100179 }
180
181 private:
Roland Levillainebea3d22016-04-12 15:42:57 +0100182 const uint32_t* const value_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100183
184 DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations);
185};
186
187#undef GENERIC_OPTIMIZATION
188
189#define INTRINSIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100190public: \
191void Set##name() { SetBit(k##name); } \
192bool Get##name() const { return IsBitSet(k##name); } \
193private: \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700194static constexpr size_t k##name = (bit) + kNumberOfGenericOptimizations
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100195
196class StringEqualsOptimizations : public IntrinsicOptimizations {
197 public:
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100198 explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100199
200 INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0);
201 INTRINSIC_OPTIMIZATION(ArgumentIsString, 1);
202
203 private:
204 DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations);
205};
206
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100207class SystemArrayCopyOptimizations : public IntrinsicOptimizations {
208 public:
209 explicit SystemArrayCopyOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
210
211 INTRINSIC_OPTIMIZATION(SourceIsNotNull, 0);
212 INTRINSIC_OPTIMIZATION(DestinationIsNotNull, 1);
213 INTRINSIC_OPTIMIZATION(DestinationIsSource, 2);
214 INTRINSIC_OPTIMIZATION(CountIsSourceLength, 3);
215 INTRINSIC_OPTIMIZATION(CountIsDestinationLength, 4);
216 INTRINSIC_OPTIMIZATION(DoesNotNeedTypeCheck, 5);
217 INTRINSIC_OPTIMIZATION(DestinationIsTypedObjectArray, 6);
218 INTRINSIC_OPTIMIZATION(DestinationIsNonPrimitiveArray, 7);
219 INTRINSIC_OPTIMIZATION(DestinationIsPrimitiveArray, 8);
220 INTRINSIC_OPTIMIZATION(SourceIsNonPrimitiveArray, 9);
221 INTRINSIC_OPTIMIZATION(SourceIsPrimitiveArray, 10);
222
223 private:
224 DISALLOW_COPY_AND_ASSIGN(SystemArrayCopyOptimizations);
225};
226
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100227#undef INTRISIC_OPTIMIZATION
228
Aart Bik2f9fcc92016-03-01 15:16:54 -0800229//
230// Macros for use in the intrinsics code generators.
231//
232
233// Defines an unimplemented intrinsic: that is, a method call that is recognized as an
234// intrinsic to exploit e.g. no side-effects or exceptions, but otherwise not handled
235// by this architecture-specific intrinsics code generator. Eventually it is implemented
236// as a true method call.
237#define UNIMPLEMENTED_INTRINSIC(Arch, Name) \
238void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
239} \
240void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
241}
242
243// Defines a list of unreached intrinsics: that is, method calls that are recognized as
244// an intrinsic, and then always converted into HIR instructions before they reach any
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +0100245// architecture-specific intrinsics code generator. This only applies to non-baseline
246// compilation.
Aart Bik2f9fcc92016-03-01 15:16:54 -0800247#define UNREACHABLE_INTRINSIC(Arch, Name) \
248void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke) { \
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000249 if (Runtime::Current()->IsAotCompiler() && \
250 !codegen_->GetCompilerOptions().IsBaseline()) { \
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +0100251 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
252 << " should have been converted to HIR"; \
253 } \
Aart Bik2f9fcc92016-03-01 15:16:54 -0800254} \
255void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke) { \
256 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
257 << " should have been converted to HIR"; \
258}
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100259#define UNREACHABLE_INTRINSICS(Arch) \
Aart Bik1f8d51b2018-02-15 10:42:37 -0800260UNREACHABLE_INTRINSIC(Arch, MathMinIntInt) \
261UNREACHABLE_INTRINSIC(Arch, MathMinLongLong) \
262UNREACHABLE_INTRINSIC(Arch, MathMinFloatFloat) \
263UNREACHABLE_INTRINSIC(Arch, MathMinDoubleDouble) \
264UNREACHABLE_INTRINSIC(Arch, MathMaxIntInt) \
265UNREACHABLE_INTRINSIC(Arch, MathMaxLongLong) \
266UNREACHABLE_INTRINSIC(Arch, MathMaxFloatFloat) \
267UNREACHABLE_INTRINSIC(Arch, MathMaxDoubleDouble) \
Aart Bik3dad3412018-02-28 12:01:46 -0800268UNREACHABLE_INTRINSIC(Arch, MathAbsInt) \
269UNREACHABLE_INTRINSIC(Arch, MathAbsLong) \
270UNREACHABLE_INTRINSIC(Arch, MathAbsFloat) \
271UNREACHABLE_INTRINSIC(Arch, MathAbsDouble) \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100272UNREACHABLE_INTRINSIC(Arch, FloatFloatToIntBits) \
273UNREACHABLE_INTRINSIC(Arch, DoubleDoubleToLongBits) \
274UNREACHABLE_INTRINSIC(Arch, FloatIsNaN) \
275UNREACHABLE_INTRINSIC(Arch, DoubleIsNaN) \
276UNREACHABLE_INTRINSIC(Arch, IntegerRotateLeft) \
277UNREACHABLE_INTRINSIC(Arch, LongRotateLeft) \
278UNREACHABLE_INTRINSIC(Arch, IntegerRotateRight) \
279UNREACHABLE_INTRINSIC(Arch, LongRotateRight) \
280UNREACHABLE_INTRINSIC(Arch, IntegerCompare) \
281UNREACHABLE_INTRINSIC(Arch, LongCompare) \
282UNREACHABLE_INTRINSIC(Arch, IntegerSignum) \
283UNREACHABLE_INTRINSIC(Arch, LongSignum) \
284UNREACHABLE_INTRINSIC(Arch, StringCharAt) \
285UNREACHABLE_INTRINSIC(Arch, StringIsEmpty) \
286UNREACHABLE_INTRINSIC(Arch, StringLength) \
287UNREACHABLE_INTRINSIC(Arch, UnsafeLoadFence) \
288UNREACHABLE_INTRINSIC(Arch, UnsafeStoreFence) \
289UNREACHABLE_INTRINSIC(Arch, UnsafeFullFence) \
290UNREACHABLE_INTRINSIC(Arch, VarHandleFullFence) \
291UNREACHABLE_INTRINSIC(Arch, VarHandleAcquireFence) \
292UNREACHABLE_INTRINSIC(Arch, VarHandleReleaseFence) \
293UNREACHABLE_INTRINSIC(Arch, VarHandleLoadLoadFence) \
294UNREACHABLE_INTRINSIC(Arch, VarHandleStoreStoreFence) \
295UNREACHABLE_INTRINSIC(Arch, MethodHandleInvokeExact) \
296UNREACHABLE_INTRINSIC(Arch, MethodHandleInvoke) \
297UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndExchange) \
298UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndExchangeAcquire) \
299UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndExchangeRelease) \
300UNREACHABLE_INTRINSIC(Arch, VarHandleCompareAndSet) \
301UNREACHABLE_INTRINSIC(Arch, VarHandleGet) \
302UNREACHABLE_INTRINSIC(Arch, VarHandleGetAcquire) \
303UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndAdd) \
304UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndAddAcquire) \
305UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndAddRelease) \
306UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseAnd) \
307UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseAndAcquire) \
308UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseAndRelease) \
309UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseOr) \
310UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseOrAcquire) \
311UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseOrRelease) \
312UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseXor) \
313UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseXorAcquire) \
314UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndBitwiseXorRelease) \
315UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndSet) \
316UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndSetAcquire) \
317UNREACHABLE_INTRINSIC(Arch, VarHandleGetAndSetRelease) \
318UNREACHABLE_INTRINSIC(Arch, VarHandleGetOpaque) \
319UNREACHABLE_INTRINSIC(Arch, VarHandleGetVolatile) \
320UNREACHABLE_INTRINSIC(Arch, VarHandleSet) \
321UNREACHABLE_INTRINSIC(Arch, VarHandleSetOpaque) \
322UNREACHABLE_INTRINSIC(Arch, VarHandleSetRelease) \
323UNREACHABLE_INTRINSIC(Arch, VarHandleSetVolatile) \
324UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSet) \
325UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSetAcquire) \
326UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSetPlain) \
327UNREACHABLE_INTRINSIC(Arch, VarHandleWeakCompareAndSetRelease)
Aart Bik2f9fcc92016-03-01 15:16:54 -0800328
Vladimir Marko68c981f2016-08-26 13:13:33 +0100329template <typename IntrinsicLocationsBuilder, typename Codegenerator>
330bool IsCallFreeIntrinsic(HInvoke* invoke, Codegenerator* codegen) {
331 if (invoke->GetIntrinsic() != Intrinsics::kNone) {
332 // This invoke may have intrinsic code generation defined. However, we must
333 // now also determine if this code generation is truly there and call-free
334 // (not unimplemented, no bail on instruction features, or call on slow path).
335 // This is done by actually calling the locations builder on the instruction
336 // and clearing out the locations once result is known. We assume this
337 // call only has creating locations as side effects!
338 // TODO: Avoid wasting Arena memory.
339 IntrinsicLocationsBuilder builder(codegen);
340 bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall();
341 invoke->SetLocations(nullptr);
342 return success;
343 }
344 return false;
345}
346
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800347} // namespace art
348
349#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_