blob: 83a512738b6ab1f0ad256b42ec96889fc0c29b6b [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
Andreas Gampee6d0d8d2015-12-28 09:54:29 -080030// Temporary measure until we have caught up with the Java 7 definition of Math.round. b/26327751
31static constexpr bool kRoundIsPlusPointFive = false;
32
Anton Kirilova3ffea22016-04-07 17:02:37 +010033// Positive floating-point infinities.
34static constexpr uint32_t kPositiveInfinityFloat = 0x7f800000U;
35static constexpr uint64_t kPositiveInfinityDouble = UINT64_C(0x7ff0000000000000);
36
Andreas Gampe71fb52f2014-12-29 17:43:08 -080037// Recognize intrinsics from HInvoke nodes.
38class IntrinsicsRecognizer : public HOptimization {
39 public:
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +010040 IntrinsicsRecognizer(HGraph* graph, CompilerDriver* driver, OptimizingCompilerStats* stats)
41 : HOptimization(graph, kIntrinsicsRecognizerPassName, stats),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +010042 driver_(driver) {}
Andreas Gampe71fb52f2014-12-29 17:43:08 -080043
44 void Run() OVERRIDE;
45
Andreas Gampe7c3952f2015-02-19 18:21:24 -080046 static constexpr const char* kIntrinsicsRecognizerPassName = "intrinsics_recognition";
47
Andreas Gampe71fb52f2014-12-29 17:43:08 -080048 private:
Andreas Gampe71fb52f2014-12-29 17:43:08 -080049 CompilerDriver* driver_;
50
51 DISALLOW_COPY_AND_ASSIGN(IntrinsicsRecognizer);
52};
53
54class IntrinsicVisitor : public ValueObject {
55 public:
56 virtual ~IntrinsicVisitor() {}
57
58 // Dispatch logic.
59
60 void Dispatch(HInvoke* invoke) {
61 switch (invoke->GetIntrinsic()) {
62 case Intrinsics::kNone:
63 return;
Aart Bik5d75afe2015-12-14 11:57:01 -080064#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment, SideEffects, Exceptions) \
65 case Intrinsics::k ## Name: \
66 Visit ## Name(invoke); \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080067 return;
68#include "intrinsics_list.h"
69INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
70#undef INTRINSICS_LIST
71#undef OPTIMIZING_INTRINSICS
72
73 // Do not put a default case. That way the compiler will complain if we missed a case.
74 }
75 }
76
77 // Define visitor methods.
78
Aart Bik5d75afe2015-12-14 11:57:01 -080079#define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment, SideEffects, Exceptions) \
Andreas Gampe71fb52f2014-12-29 17:43:08 -080080 virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
81 }
82#include "intrinsics_list.h"
83INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
84#undef INTRINSICS_LIST
85#undef OPTIMIZING_INTRINSICS
86
Roland Levillainec525fc2015-04-28 15:50:20 +010087 static void MoveArguments(HInvoke* invoke,
88 CodeGenerator* codegen,
89 InvokeDexCallingConventionVisitor* calling_convention_visitor) {
90 if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) {
91 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
David Brazdil58282f42016-01-14 12:45:10 +000092 // Explicit clinit checks triggered by static invokes must have been
93 // pruned by art::PrepareForRegisterAllocation.
94 DCHECK(!invoke_static_or_direct->IsStaticWithExplicitClinitCheck());
Roland Levillainec525fc2015-04-28 15:50:20 +010095 }
96
97 if (invoke->GetNumberOfArguments() == 0) {
98 // No argument to move.
99 return;
100 }
101
102 LocationSummary* locations = invoke->GetLocations();
103
104 // We're moving potentially two or more locations to locations that could overlap, so we need
105 // a parallel move resolver.
106 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
107
108 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
109 HInstruction* input = invoke->InputAt(i);
110 Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType());
111 Location actual_loc = locations->InAt(i);
112
113 parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr);
114 }
115
116 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
117 }
118
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800119 protected:
120 IntrinsicVisitor() {}
121
122 private:
123 DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor);
124};
125
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100126#define GENERIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100127public: \
128void Set##name() { SetBit(k##name); } \
129bool Get##name() const { return IsBitSet(k##name); } \
130private: \
Roland Levillainebea3d22016-04-12 15:42:57 +0100131static constexpr size_t k##name = bit
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100132
133class IntrinsicOptimizations : public ValueObject {
134 public:
Roland Levillainebea3d22016-04-12 15:42:57 +0100135 explicit IntrinsicOptimizations(HInvoke* invoke)
136 : value_(invoke->GetIntrinsicOptimizations()) {}
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100137 explicit IntrinsicOptimizations(const HInvoke& invoke)
138 : value_(invoke.GetIntrinsicOptimizations()) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100139
140 static constexpr int kNumberOfGenericOptimizations = 2;
141 GENERIC_OPTIMIZATION(DoesNotNeedDexCache, 0);
142 GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 1);
143
144 protected:
145 bool IsBitSet(uint32_t bit) const {
Roland Levillainebea3d22016-04-12 15:42:57 +0100146 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100147 return (*value_ & (1 << bit)) != 0u;
148 }
149
150 void SetBit(uint32_t bit) {
Roland Levillainebea3d22016-04-12 15:42:57 +0100151 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
152 *(const_cast<uint32_t* const>(value_)) |= (1 << bit);
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100153 }
154
155 private:
Roland Levillainebea3d22016-04-12 15:42:57 +0100156 const uint32_t* const value_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100157
158 DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations);
159};
160
161#undef GENERIC_OPTIMIZATION
162
163#define INTRINSIC_OPTIMIZATION(name, bit) \
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100164public: \
165void Set##name() { SetBit(k##name); } \
166bool Get##name() const { return IsBitSet(k##name); } \
167private: \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700168static constexpr size_t k##name = (bit) + kNumberOfGenericOptimizations
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100169
170class StringEqualsOptimizations : public IntrinsicOptimizations {
171 public:
Nicolas Geoffray12be6622015-10-07 11:52:21 +0100172 explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100173
174 INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0);
175 INTRINSIC_OPTIMIZATION(ArgumentIsString, 1);
176
177 private:
178 DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations);
179};
180
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100181class SystemArrayCopyOptimizations : public IntrinsicOptimizations {
182 public:
183 explicit SystemArrayCopyOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
184
185 INTRINSIC_OPTIMIZATION(SourceIsNotNull, 0);
186 INTRINSIC_OPTIMIZATION(DestinationIsNotNull, 1);
187 INTRINSIC_OPTIMIZATION(DestinationIsSource, 2);
188 INTRINSIC_OPTIMIZATION(CountIsSourceLength, 3);
189 INTRINSIC_OPTIMIZATION(CountIsDestinationLength, 4);
190 INTRINSIC_OPTIMIZATION(DoesNotNeedTypeCheck, 5);
191 INTRINSIC_OPTIMIZATION(DestinationIsTypedObjectArray, 6);
192 INTRINSIC_OPTIMIZATION(DestinationIsNonPrimitiveArray, 7);
193 INTRINSIC_OPTIMIZATION(DestinationIsPrimitiveArray, 8);
194 INTRINSIC_OPTIMIZATION(SourceIsNonPrimitiveArray, 9);
195 INTRINSIC_OPTIMIZATION(SourceIsPrimitiveArray, 10);
196
197 private:
198 DISALLOW_COPY_AND_ASSIGN(SystemArrayCopyOptimizations);
199};
200
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100201#undef INTRISIC_OPTIMIZATION
202
Aart Bik2f9fcc92016-03-01 15:16:54 -0800203//
204// Macros for use in the intrinsics code generators.
205//
206
207// Defines an unimplemented intrinsic: that is, a method call that is recognized as an
208// intrinsic to exploit e.g. no side-effects or exceptions, but otherwise not handled
209// by this architecture-specific intrinsics code generator. Eventually it is implemented
210// as a true method call.
211#define UNIMPLEMENTED_INTRINSIC(Arch, Name) \
212void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
213} \
214void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
215}
216
217// Defines a list of unreached intrinsics: that is, method calls that are recognized as
218// an intrinsic, and then always converted into HIR instructions before they reach any
219// architecture-specific intrinsics code generator.
220#define UNREACHABLE_INTRINSIC(Arch, Name) \
221void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke) { \
222 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
223 << " should have been converted to HIR"; \
224} \
225void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke) { \
226 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
227 << " should have been converted to HIR"; \
228}
229#define UNREACHABLE_INTRINSICS(Arch) \
230UNREACHABLE_INTRINSIC(Arch, FloatFloatToIntBits) \
231UNREACHABLE_INTRINSIC(Arch, DoubleDoubleToLongBits) \
232UNREACHABLE_INTRINSIC(Arch, FloatIsNaN) \
233UNREACHABLE_INTRINSIC(Arch, DoubleIsNaN) \
234UNREACHABLE_INTRINSIC(Arch, IntegerRotateLeft) \
235UNREACHABLE_INTRINSIC(Arch, LongRotateLeft) \
236UNREACHABLE_INTRINSIC(Arch, IntegerRotateRight) \
237UNREACHABLE_INTRINSIC(Arch, LongRotateRight) \
238UNREACHABLE_INTRINSIC(Arch, IntegerCompare) \
239UNREACHABLE_INTRINSIC(Arch, LongCompare) \
240UNREACHABLE_INTRINSIC(Arch, IntegerSignum) \
Aart Bik11932592016-03-08 12:42:25 -0800241UNREACHABLE_INTRINSIC(Arch, LongSignum) \
Vladimir Markodce016e2016-04-28 13:10:02 +0100242UNREACHABLE_INTRINSIC(Arch, StringIsEmpty) \
243UNREACHABLE_INTRINSIC(Arch, StringLength) \
Aart Bik11932592016-03-08 12:42:25 -0800244UNREACHABLE_INTRINSIC(Arch, UnsafeLoadFence) \
245UNREACHABLE_INTRINSIC(Arch, UnsafeStoreFence) \
246UNREACHABLE_INTRINSIC(Arch, UnsafeFullFence)
Aart Bik2f9fcc92016-03-01 15:16:54 -0800247
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800248} // namespace art
249
250#endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_