blob: 48a103530ecb23af8ce857c34b6a28068fc408cc [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
Vladimir Marko0a516052019-10-14 13:00:44 +000025namespace art {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080026
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027class DexFile;
28
Anton Kirilova3ffea22016-04-07 17:02:37 +010029// Positive floating-point infinities.
30static constexpr uint32_t kPositiveInfinityFloat = 0x7f800000U;
31static constexpr uint64_t kPositiveInfinityDouble = UINT64_C(0x7ff0000000000000);
32
xueliang.zhongc032e742016-03-28 16:44:32 +010033static constexpr uint32_t kNanFloat = 0x7fc00000U;
34static constexpr uint64_t kNanDouble = 0x7ff8000000000000;
35
Andreas Gampe71fb52f2014-12-29 17:43:08 -080036class IntrinsicVisitor : public ValueObject {
37 public:
38 virtual ~IntrinsicVisitor() {}
39
40 // Dispatch logic.
41
42 void Dispatch(HInvoke* invoke) {
43 switch (invoke->GetIntrinsic()) {
44 case Intrinsics::kNone:
45 return;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010046#define OPTIMIZING_INTRINSICS(Name, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080047 case Intrinsics::k ## Name: \
48 Visit ## Name(invoke); \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080049 return;
50#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070051 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -080052#undef INTRINSICS_LIST
53#undef OPTIMIZING_INTRINSICS
54
55 // Do not put a default case. That way the compiler will complain if we missed a case.
56 }
57 }
58
59 // Define visitor methods.
60
Nicolas Geoffray762869d2016-07-15 15:28:35 +010061#define OPTIMIZING_INTRINSICS(Name, ...) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080062 virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
63 }
64#include "intrinsics_list.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070065 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
Andreas Gampe71fb52f2014-12-29 17:43:08 -080066#undef INTRINSICS_LIST
67#undef OPTIMIZING_INTRINSICS
68
Roland Levillainec525fc2015-04-28 15:50:20 +010069 static void MoveArguments(HInvoke* invoke,
70 CodeGenerator* codegen,
71 InvokeDexCallingConventionVisitor* calling_convention_visitor) {
72 if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) {
73 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
David Brazdil58282f42016-01-14 12:45:10 +000074 // Explicit clinit checks triggered by static invokes must have been
75 // pruned by art::PrepareForRegisterAllocation.
76 DCHECK(!invoke_static_or_direct->IsStaticWithExplicitClinitCheck());
Roland Levillainec525fc2015-04-28 15:50:20 +010077 }
78
79 if (invoke->GetNumberOfArguments() == 0) {
80 // No argument to move.
81 return;
82 }
83
84 LocationSummary* locations = invoke->GetLocations();
85
86 // We're moving potentially two or more locations to locations that could overlap, so we need
87 // a parallel move resolver.
Vladimir Markoca6fff82017-10-03 14:49:14 +010088 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillainec525fc2015-04-28 15:50:20 +010089
90 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
91 HInstruction* input = invoke->InputAt(i);
92 Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType());
93 Location actual_loc = locations->InAt(i);
94
95 parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr);
96 }
97
98 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
99 }
100
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000101 static void ComputeIntegerValueOfLocations(HInvoke* invoke,
102 CodeGenerator* codegen,
103 Location return_location,
104 Location first_argument_location);
105
Vladimir Markoeebb8212018-06-05 14:57:24 +0100106 // Temporary data structure for holding Integer.valueOf data for generating code.
107 // We only use it if the boot image contains the IntegerCache objects.
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000108 struct IntegerValueOfInfo {
Vladimir Marko6fd16062018-06-26 11:02:04 +0100109 static constexpr uint32_t kInvalidReference = static_cast<uint32_t>(-1);
110
Vladimir Markoeebb8212018-06-05 14:57:24 +0100111 IntegerValueOfInfo();
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000112
Vladimir Markoeebb8212018-06-05 14:57:24 +0100113 // Offset of the Integer.value field for initializing a newly allocated instance.
114 uint32_t value_offset;
115 // The low value in the cache.
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000116 int32_t low;
Vladimir Markoeebb8212018-06-05 14:57:24 +0100117 // The length of the cache array.
118 uint32_t length;
119
Vladimir Marko6fd16062018-06-26 11:02:04 +0100120 // This union contains references to the boot image. For app AOT or JIT compilation,
121 // these are the boot image offsets of the target. For boot image compilation, the
122 // location shall be known only at link time, so we encode a symbolic reference using
123 // IntrinsicObjects::EncodePatch().
124 union {
125 // The target value for a constant input in the cache range. If the constant input
126 // is out of range (use `low` and `length` to check), this value is bogus (set to
127 // kInvalidReference) and the code must allocate a new Integer.
128 uint32_t value_boot_image_reference;
129
130 // The cache array data used for a non-constant input in the cache range.
Vladimir Markoeebb8212018-06-05 14:57:24 +0100131 // If the input is out of range, the code must allocate a new Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +0100132 uint32_t array_data_boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +0100133 };
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000134 };
135
Vladimir Marko6fd16062018-06-26 11:02:04 +0100136 static IntegerValueOfInfo ComputeIntegerValueOfInfo(
137 HInvoke* invoke, const CompilerOptions& compiler_options);
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000138
Vladimir Marko01b65522020-10-28 15:43:54 +0000139 static MemberOffset GetReferenceDisableIntrinsicOffset();
140 static MemberOffset GetReferenceSlowPathEnabledOffset();
141 static void CreateReferenceGetReferentLocations(HInvoke* invoke, CodeGenerator* codegen);
142
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
Nicolas Geoffray8f2eb252020-11-06 13:39:54 +0000166 static constexpr int kNumberOfGenericOptimizations = 1;
167 GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 0);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100168
169 protected:
170 bool IsBitSet(uint32_t bit) const {
Roland Levillainebea3d22016-04-12 15:42:57 +0100171 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100172 return (*value_ & (1 << bit)) != 0u;
173 }
174
175 void SetBit(uint32_t bit) {
Roland Levillainebea3d22016-04-12 15:42:57 +0100176 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
177 *(const_cast<uint32_t* const>(value_)) |= (1 << bit);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100178 }
179
180 private:
Roland Levillainebea3d22016-04-12 15:42:57 +0100181 const uint32_t* const value_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100182
183 DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations);
184};
185
186#undef GENERIC_OPTIMIZATION
187
188#define INTRINSIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100189public: \
190void Set##name() { SetBit(k##name); } \
191bool Get##name() const { return IsBitSet(k##name); } \
192private: \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700193static constexpr size_t k##name = (bit) + kNumberOfGenericOptimizations
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100194
195class StringEqualsOptimizations : public IntrinsicOptimizations {
196 public:
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100197 explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100198
199 INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0);
200 INTRINSIC_OPTIMIZATION(ArgumentIsString, 1);
201
202 private:
203 DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations);
204};
205
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100206class SystemArrayCopyOptimizations : public IntrinsicOptimizations {
207 public:
208 explicit SystemArrayCopyOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
209
210 INTRINSIC_OPTIMIZATION(SourceIsNotNull, 0);
211 INTRINSIC_OPTIMIZATION(DestinationIsNotNull, 1);
212 INTRINSIC_OPTIMIZATION(DestinationIsSource, 2);
213 INTRINSIC_OPTIMIZATION(CountIsSourceLength, 3);
214 INTRINSIC_OPTIMIZATION(CountIsDestinationLength, 4);
215 INTRINSIC_OPTIMIZATION(DoesNotNeedTypeCheck, 5);
216 INTRINSIC_OPTIMIZATION(DestinationIsTypedObjectArray, 6);
217 INTRINSIC_OPTIMIZATION(DestinationIsNonPrimitiveArray, 7);
218 INTRINSIC_OPTIMIZATION(DestinationIsPrimitiveArray, 8);
219 INTRINSIC_OPTIMIZATION(SourceIsNonPrimitiveArray, 9);
220 INTRINSIC_OPTIMIZATION(SourceIsPrimitiveArray, 10);
221
222 private:
223 DISALLOW_COPY_AND_ASSIGN(SystemArrayCopyOptimizations);
224};
225
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100226#undef INTRISIC_OPTIMIZATION
227
Aart Bik2f9fcc92016-03-01 15:16:54 -0800228//
229// Macros for use in the intrinsics code generators.
230//
231
232// Defines an unimplemented intrinsic: that is, a method call that is recognized as an
233// intrinsic to exploit e.g. no side-effects or exceptions, but otherwise not handled
234// by this architecture-specific intrinsics code generator. Eventually it is implemented
235// as a true method call.
236#define UNIMPLEMENTED_INTRINSIC(Arch, Name) \
237void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
238} \
239void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
240}
241
242// Defines a list of unreached intrinsics: that is, method calls that are recognized as
243// an intrinsic, and then always converted into HIR instructions before they reach any
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +0100244// architecture-specific intrinsics code generator. This only applies to non-baseline
245// compilation.
Aart Bik2f9fcc92016-03-01 15:16:54 -0800246#define UNREACHABLE_INTRINSIC(Arch, Name) \
247void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke) { \
Nicolas Geoffray075456e2018-12-14 08:54:21 +0000248 if (Runtime::Current()->IsAotCompiler() && \
249 !codegen_->GetCompilerOptions().IsBaseline()) { \
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +0100250 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
251 << " should have been converted to HIR"; \
252 } \
Aart Bik2f9fcc92016-03-01 15:16:54 -0800253} \
254void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke) { \
255 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
256 << " should have been converted to HIR"; \
257}
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100258#define UNREACHABLE_INTRINSICS(Arch) \
Aart Bik1f8d51b2018-02-15 10:42:37 -0800259UNREACHABLE_INTRINSIC(Arch, MathMinIntInt) \
260UNREACHABLE_INTRINSIC(Arch, MathMinLongLong) \
261UNREACHABLE_INTRINSIC(Arch, MathMinFloatFloat) \
262UNREACHABLE_INTRINSIC(Arch, MathMinDoubleDouble) \
263UNREACHABLE_INTRINSIC(Arch, MathMaxIntInt) \
264UNREACHABLE_INTRINSIC(Arch, MathMaxLongLong) \
265UNREACHABLE_INTRINSIC(Arch, MathMaxFloatFloat) \
266UNREACHABLE_INTRINSIC(Arch, MathMaxDoubleDouble) \
Aart Bik3dad3412018-02-28 12:01:46 -0800267UNREACHABLE_INTRINSIC(Arch, MathAbsInt) \
268UNREACHABLE_INTRINSIC(Arch, MathAbsLong) \
269UNREACHABLE_INTRINSIC(Arch, MathAbsFloat) \
270UNREACHABLE_INTRINSIC(Arch, MathAbsDouble) \
Orion Hodson43f0cdb2017-10-10 14:47:32 +0100271UNREACHABLE_INTRINSIC(Arch, FloatFloatToIntBits) \
272UNREACHABLE_INTRINSIC(Arch, DoubleDoubleToLongBits) \
273UNREACHABLE_INTRINSIC(Arch, FloatIsNaN) \
274UNREACHABLE_INTRINSIC(Arch, DoubleIsNaN) \
275UNREACHABLE_INTRINSIC(Arch, IntegerRotateLeft) \
276UNREACHABLE_INTRINSIC(Arch, LongRotateLeft) \
277UNREACHABLE_INTRINSIC(Arch, IntegerRotateRight) \
278UNREACHABLE_INTRINSIC(Arch, LongRotateRight) \
279UNREACHABLE_INTRINSIC(Arch, IntegerCompare) \
280UNREACHABLE_INTRINSIC(Arch, LongCompare) \
281UNREACHABLE_INTRINSIC(Arch, IntegerSignum) \
282UNREACHABLE_INTRINSIC(Arch, LongSignum) \
283UNREACHABLE_INTRINSIC(Arch, StringCharAt) \
284UNREACHABLE_INTRINSIC(Arch, StringIsEmpty) \
285UNREACHABLE_INTRINSIC(Arch, StringLength) \
286UNREACHABLE_INTRINSIC(Arch, UnsafeLoadFence) \
287UNREACHABLE_INTRINSIC(Arch, UnsafeStoreFence) \
Vladimir Markoeff9b012020-11-06 14:22:08 +0000288UNREACHABLE_INTRINSIC(Arch, UnsafeFullFence) \
289UNREACHABLE_INTRINSIC(Arch, VarHandleFullFence) \
290UNREACHABLE_INTRINSIC(Arch, VarHandleAcquireFence) \
291UNREACHABLE_INTRINSIC(Arch, VarHandleReleaseFence) \
292UNREACHABLE_INTRINSIC(Arch, VarHandleLoadLoadFence) \
293UNREACHABLE_INTRINSIC(Arch, VarHandleStoreStoreFence)
Aart Bik2f9fcc92016-03-01 15:16:54 -0800294
Vladimir Marko68c981f2016-08-26 13:13:33 +0100295template <typename IntrinsicLocationsBuilder, typename Codegenerator>
296bool IsCallFreeIntrinsic(HInvoke* invoke, Codegenerator* codegen) {
297 if (invoke->GetIntrinsic() != Intrinsics::kNone) {
298 // This invoke may have intrinsic code generation defined. However, we must
299 // now also determine if this code generation is truly there and call-free
300 // (not unimplemented, no bail on instruction features, or call on slow path).
301 // This is done by actually calling the locations builder on the instruction
302 // and clearing out the locations once result is known. We assume this
303 // call only has creating locations as side effects!
304 // TODO: Avoid wasting Arena memory.
305 IntrinsicLocationsBuilder builder(codegen);
306 bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall();
307 invoke->SetLocations(nullptr);
308 return success;
309 }
310 return false;
311}
312
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800313} // namespace art
314
315#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_