blob: 1e73cf67df4db8f6d5db3adfc94c677c0121da4c [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
Andreas Gampe71fb52f2014-12-29 17:43:08 -080034// Recognize intrinsics from HInvoke nodes.
35class IntrinsicsRecognizer : public HOptimization {
36 public:
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037 IntrinsicsRecognizer(HGraph* graph, OptimizingCompilerStats* stats)
38 : HOptimization(graph, kIntrinsicsRecognizerPassName, stats) {}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080039
40 void Run() OVERRIDE;
41
Andreas Gampe7c3952f2015-02-19 18:21:24 -080042 static constexpr const char* kIntrinsicsRecognizerPassName = "intrinsics_recognition";
43
Andreas Gampe71fb52f2014-12-29 17:43:08 -080044 private:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080045 DISALLOW_COPY_AND_ASSIGN(IntrinsicsRecognizer);
46};
47
48class IntrinsicVisitor : public ValueObject {
49 public:
50 virtual ~IntrinsicVisitor() {}
51
52 // Dispatch logic.
53
54 void Dispatch(HInvoke* invoke) {
55 switch (invoke->GetIntrinsic()) {
56 case Intrinsics::kNone:
57 return;
Nicolas Geoffray762869d2016-07-15 15:28:35 +010058#define OPTIMIZING_INTRINSICS(Name, ...) \
Aart Bik5d75afe2015-12-14 11:57:01 -080059 case Intrinsics::k ## Name: \
60 Visit ## Name(invoke); \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080061 return;
62#include "intrinsics_list.h"
63INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
64#undef INTRINSICS_LIST
65#undef OPTIMIZING_INTRINSICS
66
67 // Do not put a default case. That way the compiler will complain if we missed a case.
68 }
69 }
70
71 // Define visitor methods.
72
Nicolas Geoffray762869d2016-07-15 15:28:35 +010073#define OPTIMIZING_INTRINSICS(Name, ...) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080074 virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
75 }
76#include "intrinsics_list.h"
77INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
78#undef INTRINSICS_LIST
79#undef OPTIMIZING_INTRINSICS
80
Roland Levillainec525fc2015-04-28 15:50:20 +010081 static void MoveArguments(HInvoke* invoke,
82 CodeGenerator* codegen,
83 InvokeDexCallingConventionVisitor* calling_convention_visitor) {
84 if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) {
85 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
David Brazdil58282f42016-01-14 12:45:10 +000086 // Explicit clinit checks triggered by static invokes must have been
87 // pruned by art::PrepareForRegisterAllocation.
88 DCHECK(!invoke_static_or_direct->IsStaticWithExplicitClinitCheck());
Roland Levillainec525fc2015-04-28 15:50:20 +010089 }
90
91 if (invoke->GetNumberOfArguments() == 0) {
92 // No argument to move.
93 return;
94 }
95
96 LocationSummary* locations = invoke->GetLocations();
97
98 // We're moving potentially two or more locations to locations that could overlap, so we need
99 // a parallel move resolver.
100 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
101
102 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
103 HInstruction* input = invoke->InputAt(i);
104 Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType());
105 Location actual_loc = locations->InAt(i);
106
107 parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr);
108 }
109
110 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
111 }
112
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800113 protected:
114 IntrinsicVisitor() {}
115
116 private:
117 DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor);
118};
119
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100120#define GENERIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100121public: \
122void Set##name() { SetBit(k##name); } \
123bool Get##name() const { return IsBitSet(k##name); } \
124private: \
Roland Levillainebea3d22016-04-12 15:42:57 +0100125static constexpr size_t k##name = bit
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100126
127class IntrinsicOptimizations : public ValueObject {
128 public:
Roland Levillainebea3d22016-04-12 15:42:57 +0100129 explicit IntrinsicOptimizations(HInvoke* invoke)
130 : value_(invoke->GetIntrinsicOptimizations()) {}
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100131 explicit IntrinsicOptimizations(const HInvoke& invoke)
132 : value_(invoke.GetIntrinsicOptimizations()) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100133
134 static constexpr int kNumberOfGenericOptimizations = 2;
135 GENERIC_OPTIMIZATION(DoesNotNeedDexCache, 0);
136 GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 1);
137
138 protected:
139 bool IsBitSet(uint32_t bit) const {
Roland Levillainebea3d22016-04-12 15:42:57 +0100140 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100141 return (*value_ & (1 << bit)) != 0u;
142 }
143
144 void SetBit(uint32_t bit) {
Roland Levillainebea3d22016-04-12 15:42:57 +0100145 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
146 *(const_cast<uint32_t* const>(value_)) |= (1 << bit);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100147 }
148
149 private:
Roland Levillainebea3d22016-04-12 15:42:57 +0100150 const uint32_t* const value_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100151
152 DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations);
153};
154
155#undef GENERIC_OPTIMIZATION
156
157#define INTRINSIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100158public: \
159void Set##name() { SetBit(k##name); } \
160bool Get##name() const { return IsBitSet(k##name); } \
161private: \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700162static constexpr size_t k##name = (bit) + kNumberOfGenericOptimizations
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100163
164class StringEqualsOptimizations : public IntrinsicOptimizations {
165 public:
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100166 explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100167
168 INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0);
169 INTRINSIC_OPTIMIZATION(ArgumentIsString, 1);
170
171 private:
172 DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations);
173};
174
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100175class SystemArrayCopyOptimizations : public IntrinsicOptimizations {
176 public:
177 explicit SystemArrayCopyOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
178
179 INTRINSIC_OPTIMIZATION(SourceIsNotNull, 0);
180 INTRINSIC_OPTIMIZATION(DestinationIsNotNull, 1);
181 INTRINSIC_OPTIMIZATION(DestinationIsSource, 2);
182 INTRINSIC_OPTIMIZATION(CountIsSourceLength, 3);
183 INTRINSIC_OPTIMIZATION(CountIsDestinationLength, 4);
184 INTRINSIC_OPTIMIZATION(DoesNotNeedTypeCheck, 5);
185 INTRINSIC_OPTIMIZATION(DestinationIsTypedObjectArray, 6);
186 INTRINSIC_OPTIMIZATION(DestinationIsNonPrimitiveArray, 7);
187 INTRINSIC_OPTIMIZATION(DestinationIsPrimitiveArray, 8);
188 INTRINSIC_OPTIMIZATION(SourceIsNonPrimitiveArray, 9);
189 INTRINSIC_OPTIMIZATION(SourceIsPrimitiveArray, 10);
190
191 private:
192 DISALLOW_COPY_AND_ASSIGN(SystemArrayCopyOptimizations);
193};
194
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100195#undef INTRISIC_OPTIMIZATION
196
Aart Bik2f9fcc92016-03-01 15:16:54 -0800197//
198// Macros for use in the intrinsics code generators.
199//
200
201// Defines an unimplemented intrinsic: that is, a method call that is recognized as an
202// intrinsic to exploit e.g. no side-effects or exceptions, but otherwise not handled
203// by this architecture-specific intrinsics code generator. Eventually it is implemented
204// as a true method call.
205#define UNIMPLEMENTED_INTRINSIC(Arch, Name) \
206void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
207} \
208void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
209}
210
211// Defines a list of unreached intrinsics: that is, method calls that are recognized as
212// an intrinsic, and then always converted into HIR instructions before they reach any
213// architecture-specific intrinsics code generator.
214#define UNREACHABLE_INTRINSIC(Arch, Name) \
215void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke) { \
216 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
217 << " should have been converted to HIR"; \
218} \
219void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke) { \
220 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
221 << " should have been converted to HIR"; \
222}
223#define UNREACHABLE_INTRINSICS(Arch) \
224UNREACHABLE_INTRINSIC(Arch, FloatFloatToIntBits) \
225UNREACHABLE_INTRINSIC(Arch, DoubleDoubleToLongBits) \
226UNREACHABLE_INTRINSIC(Arch, FloatIsNaN) \
227UNREACHABLE_INTRINSIC(Arch, DoubleIsNaN) \
228UNREACHABLE_INTRINSIC(Arch, IntegerRotateLeft) \
229UNREACHABLE_INTRINSIC(Arch, LongRotateLeft) \
230UNREACHABLE_INTRINSIC(Arch, IntegerRotateRight) \
231UNREACHABLE_INTRINSIC(Arch, LongRotateRight) \
232UNREACHABLE_INTRINSIC(Arch, IntegerCompare) \
233UNREACHABLE_INTRINSIC(Arch, LongCompare) \
234UNREACHABLE_INTRINSIC(Arch, IntegerSignum) \
Aart Bik11932592016-03-08 12:42:25 -0800235UNREACHABLE_INTRINSIC(Arch, LongSignum) \
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100236UNREACHABLE_INTRINSIC(Arch, StringCharAt) \
Vladimir Markodce016e2016-04-28 13:10:02 +0100237UNREACHABLE_INTRINSIC(Arch, StringIsEmpty) \
238UNREACHABLE_INTRINSIC(Arch, StringLength) \
Aart Bik11932592016-03-08 12:42:25 -0800239UNREACHABLE_INTRINSIC(Arch, UnsafeLoadFence) \
240UNREACHABLE_INTRINSIC(Arch, UnsafeStoreFence) \
241UNREACHABLE_INTRINSIC(Arch, UnsafeFullFence)
Aart Bik2f9fcc92016-03-01 15:16:54 -0800242
Vladimir Marko68c981f2016-08-26 13:13:33 +0100243template <typename IntrinsicLocationsBuilder, typename Codegenerator>
244bool IsCallFreeIntrinsic(HInvoke* invoke, Codegenerator* codegen) {
245 if (invoke->GetIntrinsic() != Intrinsics::kNone) {
246 // This invoke may have intrinsic code generation defined. However, we must
247 // now also determine if this code generation is truly there and call-free
248 // (not unimplemented, no bail on instruction features, or call on slow path).
249 // This is done by actually calling the locations builder on the instruction
250 // and clearing out the locations once result is known. We assume this
251 // call only has creating locations as side effects!
252 // TODO: Avoid wasting Arena memory.
253 IntrinsicLocationsBuilder builder(codegen);
254 bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall();
255 invoke->SetLocations(nullptr);
256 return success;
257 }
258 return false;
259}
260
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800261} // namespace art
262
263#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_