1 /*
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
20 #include "code_generator.h"
21 #include "nodes.h"
22 #include "optimization.h"
23 #include "parallel_move_resolver.h"
24
25 namespace art {
26
27 class DexFile;
28
29 // Positive floating-point infinities.
30 static constexpr uint32_t kPositiveInfinityFloat = 0x7f800000U;
31 static constexpr uint64_t kPositiveInfinityDouble = UINT64_C(0x7ff0000000000000);
32
33 static constexpr uint32_t kNanFloat = 0x7fc00000U;
34 static constexpr uint64_t kNanDouble = 0x7ff8000000000000;
35
36 class IntrinsicVisitor : public ValueObject {
37 public:
~IntrinsicVisitor()38 virtual ~IntrinsicVisitor() {}
39
40 // Dispatch logic.
41
Dispatch(HInvoke * invoke)42 void Dispatch(HInvoke* invoke) {
43 switch (invoke->GetIntrinsic()) {
44 case Intrinsics::kNone:
45 return;
46 #define OPTIMIZING_INTRINSICS(Name, ...) \
47 case Intrinsics::k ## Name: \
48 Visit ## Name(invoke); \
49 return;
50 #include "intrinsics_list.h"
51 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
52 #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
61 #define OPTIMIZING_INTRINSICS(Name, ...) \
62 virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
63 }
64 #include "intrinsics_list.h"
INTRINSICS_LIST(OPTIMIZING_INTRINSICS)65 INTRINSICS_LIST(OPTIMIZING_INTRINSICS)
66 #undef INTRINSICS_LIST
67 #undef OPTIMIZING_INTRINSICS
68
69 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();
74 // Explicit clinit checks triggered by static invokes must have been
75 // pruned by art::PrepareForRegisterAllocation.
76 DCHECK(!invoke_static_or_direct->IsStaticWithExplicitClinitCheck());
77 }
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.
88 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
89
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(¶llel_move);
99 }
100
101 static void ComputeIntegerValueOfLocations(HInvoke* invoke,
102 CodeGenerator* codegen,
103 Location return_location,
104 Location first_argument_location);
105
106 // Temporary data structure for holding Integer.valueOf data for generating code.
107 // We only use it if the boot image contains the IntegerCache objects.
108 struct IntegerValueOfInfo {
109 static constexpr uint32_t kInvalidReference = static_cast<uint32_t>(-1);
110
111 IntegerValueOfInfo();
112
113 // Offset of the Integer.value field for initializing a newly allocated instance.
114 uint32_t value_offset;
115 // The low value in the cache.
116 int32_t low;
117 // The length of the cache array.
118 uint32_t length;
119
120 // 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.
131 // If the input is out of range, the code must allocate a new Integer.
132 uint32_t array_data_boot_image_reference;
133 };
134 };
135
136 static IntegerValueOfInfo ComputeIntegerValueOfInfo(
137 HInvoke* invoke, const CompilerOptions& compiler_options);
138
139 static MemberOffset GetReferenceDisableIntrinsicOffset();
140 static MemberOffset GetReferenceSlowPathEnabledOffset();
141 static void CreateReferenceGetReferentLocations(HInvoke* invoke, CodeGenerator* codegen);
142 static void CreateReferenceRefersToLocations(HInvoke* invoke);
143
144 protected:
IntrinsicVisitor()145 IntrinsicVisitor() {}
146
147 static void AssertNonMovableStringClass();
148
149 private:
150 DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor);
151 };
152
153 #define GENERIC_OPTIMIZATION(name, bit) \
154 public: \
155 void Set##name() { SetBit(k##name); } \
156 bool Get##name() const { return IsBitSet(k##name); } \
157 private: \
158 static constexpr size_t k##name = bit
159
160 class IntrinsicOptimizations : public ValueObject {
161 public:
IntrinsicOptimizations(HInvoke * invoke)162 explicit IntrinsicOptimizations(HInvoke* invoke)
163 : value_(invoke->GetIntrinsicOptimizations()) {}
IntrinsicOptimizations(const HInvoke & invoke)164 explicit IntrinsicOptimizations(const HInvoke& invoke)
165 : value_(invoke.GetIntrinsicOptimizations()) {}
166
167 static constexpr int kNumberOfGenericOptimizations = 1;
168 GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 0);
169
170 protected:
IsBitSet(uint32_t bit)171 bool IsBitSet(uint32_t bit) const {
172 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
173 return (*value_ & (1 << bit)) != 0u;
174 }
175
SetBit(uint32_t bit)176 void SetBit(uint32_t bit) {
177 DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte);
178 *(const_cast<uint32_t* const>(value_)) |= (1 << bit);
179 }
180
181 private:
182 const uint32_t* const value_;
183
184 DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations);
185 };
186
187 #undef GENERIC_OPTIMIZATION
188
189 #define INTRINSIC_OPTIMIZATION(name, bit) \
190 public: \
191 void Set##name() { SetBit(k##name); } \
192 bool Get##name() const { return IsBitSet(k##name); } \
193 private: \
194 static constexpr size_t k##name = (bit) + kNumberOfGenericOptimizations
195
196 class StringEqualsOptimizations : public IntrinsicOptimizations {
197 public:
StringEqualsOptimizations(HInvoke * invoke)198 explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
199
200 INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0);
201 INTRINSIC_OPTIMIZATION(ArgumentIsString, 1);
202
203 private:
204 DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations);
205 };
206
207 class SystemArrayCopyOptimizations : public IntrinsicOptimizations {
208 public:
SystemArrayCopyOptimizations(HInvoke * invoke)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
227 class VarHandleOptimizations : public IntrinsicOptimizations {
228 public:
VarHandleOptimizations(HInvoke * invoke)229 explicit VarHandleOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {}
230
231 INTRINSIC_OPTIMIZATION(DoNotIntrinsify, 0); // One of the checks is statically known to fail.
232 INTRINSIC_OPTIMIZATION(SkipObjectNullCheck, 1); // Not applicable for static fields.
233
234 // Use known `VarHandle` from the boot image. To apply this optimization, the following
235 // `VarHandle` checks must pass based on static analysis:
236 // - `VarHandle` type check (must match the coordinate count),
237 // - access mode check,
238 // - var type check (including assignability for reference types),
239 // - object type check (except for static field VarHandles that do not take an object).
240 // Note that the object null check is controlled by the above flag `SkipObjectNullCheck`
241 // and arrays and byte array views (which always need a range check and sometimes also
242 // array type check) are currently unsupported.
243 INTRINSIC_OPTIMIZATION(UseKnownBootImageVarHandle, 2);
244 };
245
246 #undef INTRISIC_OPTIMIZATION
247
248 //
249 // Macros for use in the intrinsics code generators.
250 //
251
252 // Defines an unimplemented intrinsic: that is, a method call that is recognized as an
253 // intrinsic to exploit e.g. no side-effects or exceptions, but otherwise not handled
254 // by this architecture-specific intrinsics code generator. Eventually it is implemented
255 // as a true method call.
256 #define UNIMPLEMENTED_INTRINSIC(Arch, Name) \
257 void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
258 } \
259 void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
260 }
261
262 // Defines a list of unreached intrinsics: that is, method calls that are recognized as
263 // an intrinsic, and then always converted into HIR instructions before they reach any
264 // architecture-specific intrinsics code generator. This only applies to non-baseline
265 // compilation.
266 #define UNREACHABLE_INTRINSIC(Arch, Name) \
267 void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke) { \
268 if (Runtime::Current()->IsAotCompiler() && \
269 !codegen_->GetCompilerOptions().IsBaseline()) { \
270 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
271 << " should have been converted to HIR"; \
272 } \
273 } \
274 void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke) { \
275 LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \
276 << " should have been converted to HIR"; \
277 }
278 #define UNREACHABLE_INTRINSICS(Arch) \
279 UNREACHABLE_INTRINSIC(Arch, MathMinIntInt) \
280 UNREACHABLE_INTRINSIC(Arch, MathMinLongLong) \
281 UNREACHABLE_INTRINSIC(Arch, MathMinFloatFloat) \
282 UNREACHABLE_INTRINSIC(Arch, MathMinDoubleDouble) \
283 UNREACHABLE_INTRINSIC(Arch, MathMaxIntInt) \
284 UNREACHABLE_INTRINSIC(Arch, MathMaxLongLong) \
285 UNREACHABLE_INTRINSIC(Arch, MathMaxFloatFloat) \
286 UNREACHABLE_INTRINSIC(Arch, MathMaxDoubleDouble) \
287 UNREACHABLE_INTRINSIC(Arch, MathAbsInt) \
288 UNREACHABLE_INTRINSIC(Arch, MathAbsLong) \
289 UNREACHABLE_INTRINSIC(Arch, MathAbsFloat) \
290 UNREACHABLE_INTRINSIC(Arch, MathAbsDouble) \
291 UNREACHABLE_INTRINSIC(Arch, FloatFloatToIntBits) \
292 UNREACHABLE_INTRINSIC(Arch, DoubleDoubleToLongBits) \
293 UNREACHABLE_INTRINSIC(Arch, FloatIsNaN) \
294 UNREACHABLE_INTRINSIC(Arch, DoubleIsNaN) \
295 UNREACHABLE_INTRINSIC(Arch, IntegerRotateLeft) \
296 UNREACHABLE_INTRINSIC(Arch, LongRotateLeft) \
297 UNREACHABLE_INTRINSIC(Arch, IntegerRotateRight) \
298 UNREACHABLE_INTRINSIC(Arch, LongRotateRight) \
299 UNREACHABLE_INTRINSIC(Arch, IntegerCompare) \
300 UNREACHABLE_INTRINSIC(Arch, LongCompare) \
301 UNREACHABLE_INTRINSIC(Arch, IntegerSignum) \
302 UNREACHABLE_INTRINSIC(Arch, LongSignum) \
303 UNREACHABLE_INTRINSIC(Arch, StringCharAt) \
304 UNREACHABLE_INTRINSIC(Arch, StringIsEmpty) \
305 UNREACHABLE_INTRINSIC(Arch, StringLength) \
306 UNREACHABLE_INTRINSIC(Arch, UnsafeLoadFence) \
307 UNREACHABLE_INTRINSIC(Arch, UnsafeStoreFence) \
308 UNREACHABLE_INTRINSIC(Arch, UnsafeFullFence) \
309 UNREACHABLE_INTRINSIC(Arch, JdkUnsafeLoadFence) \
310 UNREACHABLE_INTRINSIC(Arch, JdkUnsafeStoreFence) \
311 UNREACHABLE_INTRINSIC(Arch, JdkUnsafeFullFence) \
312 UNREACHABLE_INTRINSIC(Arch, VarHandleFullFence) \
313 UNREACHABLE_INTRINSIC(Arch, VarHandleAcquireFence) \
314 UNREACHABLE_INTRINSIC(Arch, VarHandleReleaseFence) \
315 UNREACHABLE_INTRINSIC(Arch, VarHandleLoadLoadFence) \
316 UNREACHABLE_INTRINSIC(Arch, VarHandleStoreStoreFence)
317
318 template <typename IntrinsicLocationsBuilder, typename Codegenerator>
IsCallFreeIntrinsic(HInvoke * invoke,Codegenerator * codegen)319 bool IsCallFreeIntrinsic(HInvoke* invoke, Codegenerator* codegen) {
320 if (invoke->GetIntrinsic() != Intrinsics::kNone) {
321 // This invoke may have intrinsic code generation defined. However, we must
322 // now also determine if this code generation is truly there and call-free
323 // (not unimplemented, no bail on instruction features, or call on slow path).
324 // This is done by actually calling the locations builder on the instruction
325 // and clearing out the locations once result is known. We assume this
326 // call only has creating locations as side effects!
327 // TODO: Avoid wasting Arena memory.
328 IntrinsicLocationsBuilder builder(codegen);
329 bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall();
330 invoke->SetLocations(nullptr);
331 return success;
332 }
333 return false;
334 }
335
336 } // namespace art
337
338 #endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_
339