blob: 29aecbc0975bc9851c86f974ced34afd28ecb075 [file] [log] [blame]
Anton Kirilov5ec62182016-10-13 20:16:02 +01001/*
2 * Copyright (C) 2016 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#include "intrinsics_arm_vixl.h"
18
19#include "arch/arm/instruction_set_features_arm.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080020#include "art_method.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010021#include "code_generator_arm_vixl.h"
22#include "common_arm.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070023#include "heap_poisoning.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010024#include "lock_word.h"
25#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070026#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080027#include "mirror/reference.h"
28#include "mirror/string.h"
29#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070030#include "thread-current-inl.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010031
32#include "aarch32/constants-aarch32.h"
33
34namespace art {
35namespace arm {
36
37#define __ assembler->GetVIXLAssembler()->
38
39using helpers::DRegisterFrom;
40using helpers::HighRegisterFrom;
41using helpers::InputDRegisterAt;
42using helpers::InputRegisterAt;
43using helpers::InputSRegisterAt;
44using helpers::InputVRegisterAt;
45using helpers::Int32ConstantFrom;
46using helpers::LocationFrom;
47using helpers::LowRegisterFrom;
48using helpers::LowSRegisterFrom;
xueliang.zhong53463ba2017-02-16 15:18:03 +000049using helpers::HighSRegisterFrom;
Anton Kirilov5ec62182016-10-13 20:16:02 +010050using helpers::OutputDRegister;
xueliang.zhongc032e742016-03-28 16:44:32 +010051using helpers::OutputSRegister;
Anton Kirilov5ec62182016-10-13 20:16:02 +010052using helpers::OutputRegister;
53using helpers::OutputVRegister;
54using helpers::RegisterFrom;
55using helpers::SRegisterFrom;
xueliang.zhongc032e742016-03-28 16:44:32 +010056using helpers::DRegisterFromS;
Anton Kirilov5ec62182016-10-13 20:16:02 +010057
58using namespace vixl::aarch32; // NOLINT(build/namespaces)
59
Artem Serov0fb37192016-12-06 18:13:40 +000060using vixl::ExactAssemblyScope;
61using vixl::CodeBufferCheckScope;
62
Anton Kirilov5ec62182016-10-13 20:16:02 +010063ArmVIXLAssembler* IntrinsicCodeGeneratorARMVIXL::GetAssembler() {
64 return codegen_->GetAssembler();
65}
66
67ArenaAllocator* IntrinsicCodeGeneratorARMVIXL::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010068 return codegen_->GetGraph()->GetAllocator();
Anton Kirilov5ec62182016-10-13 20:16:02 +010069}
70
71// Default slow-path for fallback (calling the managed code to handle the intrinsic) in an
72// intrinsified call. This will copy the arguments into the positions for a regular call.
73//
74// Note: The actual parameters are required to be in the locations given by the invoke's location
75// summary. If an intrinsic modifies those locations before a slowpath call, they must be
76// restored!
77//
78// Note: If an invoke wasn't sharpened, we will put down an invoke-virtual here. That's potentially
79// sub-optimal (compared to a direct pointer call), but this is a slow-path.
80
81class IntrinsicSlowPathARMVIXL : public SlowPathCodeARMVIXL {
82 public:
83 explicit IntrinsicSlowPathARMVIXL(HInvoke* invoke)
84 : SlowPathCodeARMVIXL(invoke), invoke_(invoke) {}
85
86 Location MoveArguments(CodeGenerator* codegen) {
Artem Serovd4cc5b22016-11-04 11:19:09 +000087 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Anton Kirilov5ec62182016-10-13 20:16:02 +010088 IntrinsicVisitor::MoveArguments(invoke_, codegen, &calling_convention_visitor);
89 return calling_convention_visitor.GetMethodLocation();
90 }
91
92 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
93 ArmVIXLAssembler* assembler = down_cast<ArmVIXLAssembler*>(codegen->GetAssembler());
94 __ Bind(GetEntryLabel());
95
96 SaveLiveRegisters(codegen, invoke_->GetLocations());
97
98 Location method_loc = MoveArguments(codegen);
99
100 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100101 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(), method_loc, this);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100102 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100103 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), method_loc, this);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100104 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100105
106 // Copy the result back to the expected output.
107 Location out = invoke_->GetLocations()->Out();
108 if (out.IsValid()) {
109 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
110 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
111 codegen->MoveFromReturnRegister(out, invoke_->GetType());
112 }
113
114 RestoreLiveRegisters(codegen, invoke_->GetLocations());
115 __ B(GetExitLabel());
116 }
117
118 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPath"; }
119
120 private:
121 // The instruction where this slow path is happening.
122 HInvoke* const invoke_;
123
124 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARMVIXL);
125};
126
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000127// Compute base address for the System.arraycopy intrinsic in `base`.
128static void GenSystemArrayCopyBaseAddress(ArmVIXLAssembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000130 const vixl32::Register& array,
131 const Location& pos,
132 const vixl32::Register& base) {
133 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100134 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000135 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100136 DCHECK_EQ(type, DataType::Type::kReference);
137 const int32_t element_size = DataType::Size(type);
138 const uint32_t element_size_shift = DataType::SizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000139 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
140
141 if (pos.IsConstant()) {
142 int32_t constant = Int32ConstantFrom(pos);
143 __ Add(base, array, element_size * constant + data_offset);
144 } else {
145 __ Add(base, array, Operand(RegisterFrom(pos), vixl32::LSL, element_size_shift));
146 __ Add(base, base, data_offset);
147 }
148}
149
150// Compute end address for the System.arraycopy intrinsic in `end`.
151static void GenSystemArrayCopyEndAddress(ArmVIXLAssembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100152 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000153 const Location& copy_length,
154 const vixl32::Register& base,
155 const vixl32::Register& end) {
156 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100157 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000158 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159 DCHECK_EQ(type, DataType::Type::kReference);
160 const int32_t element_size = DataType::Size(type);
161 const uint32_t element_size_shift = DataType::SizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000162
163 if (copy_length.IsConstant()) {
164 int32_t constant = Int32ConstantFrom(copy_length);
165 __ Add(end, base, element_size * constant);
166 } else {
167 __ Add(end, base, Operand(RegisterFrom(copy_length), vixl32::LSL, element_size_shift));
168 }
169}
170
Anton Kirilov5ec62182016-10-13 20:16:02 +0100171// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
172class ReadBarrierSystemArrayCopySlowPathARMVIXL : public SlowPathCodeARMVIXL {
173 public:
174 explicit ReadBarrierSystemArrayCopySlowPathARMVIXL(HInstruction* instruction)
175 : SlowPathCodeARMVIXL(instruction) {
176 DCHECK(kEmitCompilerReadBarrier);
177 DCHECK(kUseBakerReadBarrier);
178 }
179
180 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
181 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
182 ArmVIXLAssembler* assembler = arm_codegen->GetAssembler();
183 LocationSummary* locations = instruction_->GetLocations();
184 DCHECK(locations->CanCall());
185 DCHECK(instruction_->IsInvokeStaticOrDirect())
186 << "Unexpected instruction in read barrier arraycopy slow path: "
187 << instruction_->DebugName();
188 DCHECK(instruction_->GetLocations()->Intrinsified());
189 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100191 DataType::Type type = DataType::Type::kReference;
192 const int32_t element_size = DataType::Size(type);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100193
194 vixl32::Register dest = InputRegisterAt(instruction_, 2);
195 Location dest_pos = locations->InAt(3);
196 vixl32::Register src_curr_addr = RegisterFrom(locations->GetTemp(0));
197 vixl32::Register dst_curr_addr = RegisterFrom(locations->GetTemp(1));
198 vixl32::Register src_stop_addr = RegisterFrom(locations->GetTemp(2));
199 vixl32::Register tmp = RegisterFrom(locations->GetTemp(3));
200
201 __ Bind(GetEntryLabel());
202 // Compute the base destination address in `dst_curr_addr`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000203 GenSystemArrayCopyBaseAddress(assembler, type, dest, dest_pos, dst_curr_addr);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100204
205 vixl32::Label loop;
206 __ Bind(&loop);
207 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
208 assembler->MaybeUnpoisonHeapReference(tmp);
209 // TODO: Inline the mark bit check before calling the runtime?
210 // tmp = ReadBarrier::Mark(tmp);
211 // No need to save live registers; it's taken care of by the
212 // entrypoint. Also, there is no need to update the stack mask,
213 // as this runtime call will not trigger a garbage collection.
214 // (See ReadBarrierMarkSlowPathARM::EmitNativeCode for more
215 // explanations.)
216 DCHECK(!tmp.IsSP());
217 DCHECK(!tmp.IsLR());
218 DCHECK(!tmp.IsPC());
219 // IP is used internally by the ReadBarrierMarkRegX entry point
220 // as a temporary (and not preserved). It thus cannot be used by
221 // any live register in this slow path.
222 DCHECK(!src_curr_addr.Is(ip));
223 DCHECK(!dst_curr_addr.Is(ip));
224 DCHECK(!src_stop_addr.Is(ip));
225 DCHECK(!tmp.Is(ip));
226 DCHECK(tmp.IsRegister()) << tmp;
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000227 // TODO: Load the entrypoint once before the loop, instead of
228 // loading it at every iteration.
Anton Kirilov5ec62182016-10-13 20:16:02 +0100229 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100230 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(tmp.GetCode());
Anton Kirilov5ec62182016-10-13 20:16:02 +0100231 // This runtime call does not require a stack map.
232 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
233 assembler->MaybePoisonHeapReference(tmp);
234 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
235 __ Cmp(src_curr_addr, src_stop_addr);
Artem Serov517d9f62016-12-12 15:51:15 +0000236 __ B(ne, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100237 __ B(GetExitLabel());
238 }
239
240 const char* GetDescription() const OVERRIDE {
241 return "ReadBarrierSystemArrayCopySlowPathARMVIXL";
242 }
243
244 private:
245 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARMVIXL);
246};
247
248IntrinsicLocationsBuilderARMVIXL::IntrinsicLocationsBuilderARMVIXL(CodeGeneratorARMVIXL* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100249 : allocator_(codegen->GetGraph()->GetAllocator()),
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000250 codegen_(codegen),
Anton Kirilov5ec62182016-10-13 20:16:02 +0100251 assembler_(codegen->GetAssembler()),
252 features_(codegen->GetInstructionSetFeatures()) {}
253
254bool IntrinsicLocationsBuilderARMVIXL::TryDispatch(HInvoke* invoke) {
255 Dispatch(invoke);
256 LocationSummary* res = invoke->GetLocations();
257 if (res == nullptr) {
258 return false;
259 }
260 return res->Intrinsified();
261}
262
Vladimir Markoca6fff82017-10-03 14:49:14 +0100263static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
264 LocationSummary* locations =
265 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100266 locations->SetInAt(0, Location::RequiresFpuRegister());
267 locations->SetOut(Location::RequiresRegister());
268}
269
Vladimir Markoca6fff82017-10-03 14:49:14 +0100270static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
271 LocationSummary* locations =
272 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100273 locations->SetInAt(0, Location::RequiresRegister());
274 locations->SetOut(Location::RequiresFpuRegister());
275}
276
277static void MoveFPToInt(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
278 Location input = locations->InAt(0);
279 Location output = locations->Out();
280 if (is64bit) {
281 __ Vmov(LowRegisterFrom(output), HighRegisterFrom(output), DRegisterFrom(input));
282 } else {
283 __ Vmov(RegisterFrom(output), SRegisterFrom(input));
284 }
285}
286
287static void MoveIntToFP(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
288 Location input = locations->InAt(0);
289 Location output = locations->Out();
290 if (is64bit) {
291 __ Vmov(DRegisterFrom(output), LowRegisterFrom(input), HighRegisterFrom(input));
292 } else {
293 __ Vmov(SRegisterFrom(output), RegisterFrom(input));
294 }
295}
296
297void IntrinsicLocationsBuilderARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100298 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100299}
300void IntrinsicLocationsBuilderARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100301 CreateIntToFPLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100302}
303
304void IntrinsicCodeGeneratorARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
305 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
306}
307void IntrinsicCodeGeneratorARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
308 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
309}
310
311void IntrinsicLocationsBuilderARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100312 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100313}
314void IntrinsicLocationsBuilderARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100315 CreateIntToFPLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100316}
317
318void IntrinsicCodeGeneratorARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
319 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
320}
321void IntrinsicCodeGeneratorARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
322 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
323}
324
Vladimir Markoca6fff82017-10-03 14:49:14 +0100325static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
326 LocationSummary* locations =
327 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100328 locations->SetInAt(0, Location::RequiresRegister());
329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
330}
331
Vladimir Markoca6fff82017-10-03 14:49:14 +0100332static void CreateLongToLongLocationsWithOverlap(ArenaAllocator* allocator, HInvoke* invoke) {
333 LocationSummary* locations =
334 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +0100335 locations->SetInAt(0, Location::RequiresRegister());
336 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
337}
338
Vladimir Markoca6fff82017-10-03 14:49:14 +0100339static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
340 LocationSummary* locations =
341 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100342 locations->SetInAt(0, Location::RequiresFpuRegister());
343 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
344}
345
Anton Kirilov6f644202017-02-27 18:29:45 +0000346static void GenNumberOfLeadingZeros(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100347 DataType::Type type,
Anton Kirilov6f644202017-02-27 18:29:45 +0000348 CodeGeneratorARMVIXL* codegen) {
349 ArmVIXLAssembler* assembler = codegen->GetAssembler();
350 LocationSummary* locations = invoke->GetLocations();
Anton Kirilov5ec62182016-10-13 20:16:02 +0100351 Location in = locations->InAt(0);
352 vixl32::Register out = RegisterFrom(locations->Out());
353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DCHECK((type == DataType::Type::kInt32) || (type == DataType::Type::kInt64));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100356 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100357 vixl32::Register in_reg_lo = LowRegisterFrom(in);
358 vixl32::Register in_reg_hi = HighRegisterFrom(in);
359 vixl32::Label end;
Anton Kirilov6f644202017-02-27 18:29:45 +0000360 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100361 __ Clz(out, in_reg_hi);
Anton Kirilov6f644202017-02-27 18:29:45 +0000362 __ CompareAndBranchIfNonZero(in_reg_hi, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100363 __ Clz(out, in_reg_lo);
364 __ Add(out, out, 32);
Anton Kirilov6f644202017-02-27 18:29:45 +0000365 if (end.IsReferenced()) {
366 __ Bind(&end);
367 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100368 } else {
369 __ Clz(out, RegisterFrom(in));
370 }
371}
372
373void IntrinsicLocationsBuilderARMVIXL::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100374 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100375}
376
377void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100378 GenNumberOfLeadingZeros(invoke, DataType::Type::kInt32, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100379}
380
381void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100382 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100383}
384
385void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100386 GenNumberOfLeadingZeros(invoke, DataType::Type::kInt64, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100387}
388
Anton Kirilov6f644202017-02-27 18:29:45 +0000389static void GenNumberOfTrailingZeros(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100390 DataType::Type type,
Anton Kirilov6f644202017-02-27 18:29:45 +0000391 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100392 DCHECK((type == DataType::Type::kInt32) || (type == DataType::Type::kInt64));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100393
Anton Kirilov6f644202017-02-27 18:29:45 +0000394 ArmVIXLAssembler* assembler = codegen->GetAssembler();
395 LocationSummary* locations = invoke->GetLocations();
Anton Kirilov5ec62182016-10-13 20:16:02 +0100396 vixl32::Register out = RegisterFrom(locations->Out());
397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100398 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100399 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
400 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
401 vixl32::Label end;
Anton Kirilov6f644202017-02-27 18:29:45 +0000402 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100403 __ Rbit(out, in_reg_lo);
404 __ Clz(out, out);
Anton Kirilov6f644202017-02-27 18:29:45 +0000405 __ CompareAndBranchIfNonZero(in_reg_lo, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100406 __ Rbit(out, in_reg_hi);
407 __ Clz(out, out);
408 __ Add(out, out, 32);
Anton Kirilov6f644202017-02-27 18:29:45 +0000409 if (end.IsReferenced()) {
410 __ Bind(&end);
411 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100412 } else {
413 vixl32::Register in = RegisterFrom(locations->InAt(0));
414 __ Rbit(out, in);
415 __ Clz(out, out);
416 }
417}
418
419void IntrinsicLocationsBuilderARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100420 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100421}
422
423void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100424 GenNumberOfTrailingZeros(invoke, DataType::Type::kInt32, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100425}
426
427void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100428 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100429}
430
431void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100432 GenNumberOfTrailingZeros(invoke, DataType::Type::kInt64, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100433}
434
Anton Kirilov5ec62182016-10-13 20:16:02 +0100435void IntrinsicLocationsBuilderARMVIXL::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100436 CreateFPToFPLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100437}
438
439void IntrinsicCodeGeneratorARMVIXL::VisitMathSqrt(HInvoke* invoke) {
440 ArmVIXLAssembler* assembler = GetAssembler();
441 __ Vsqrt(OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
442}
443
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100444void IntrinsicLocationsBuilderARMVIXL::VisitMathRint(HInvoke* invoke) {
445 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100446 CreateFPToFPLocations(allocator_, invoke);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100447 }
448}
449
450void IntrinsicCodeGeneratorARMVIXL::VisitMathRint(HInvoke* invoke) {
451 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
452 ArmVIXLAssembler* assembler = GetAssembler();
453 __ Vrintn(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
454}
455
xueliang.zhong53463ba2017-02-16 15:18:03 +0000456void IntrinsicLocationsBuilderARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
457 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100458 LocationSummary* locations =
459 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
xueliang.zhong53463ba2017-02-16 15:18:03 +0000460 locations->SetInAt(0, Location::RequiresFpuRegister());
461 locations->SetOut(Location::RequiresRegister());
462 locations->AddTemp(Location::RequiresFpuRegister());
463 }
464}
465
466void IntrinsicCodeGeneratorARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
467 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
468
469 ArmVIXLAssembler* assembler = GetAssembler();
470 vixl32::SRegister in_reg = InputSRegisterAt(invoke, 0);
471 vixl32::Register out_reg = OutputRegister(invoke);
472 vixl32::SRegister temp1 = LowSRegisterFrom(invoke->GetLocations()->GetTemp(0));
473 vixl32::SRegister temp2 = HighSRegisterFrom(invoke->GetLocations()->GetTemp(0));
474 vixl32::Label done;
475 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
476
477 // Round to nearest integer, ties away from zero.
478 __ Vcvta(S32, F32, temp1, in_reg);
479 __ Vmov(out_reg, temp1);
480
481 // For positive, zero or NaN inputs, rounding is done.
482 __ Cmp(out_reg, 0);
483 __ B(ge, final_label, /* far_target */ false);
484
485 // Handle input < 0 cases.
486 // If input is negative but not a tie, previous result (round to nearest) is valid.
487 // If input is a negative tie, change rounding direction to positive infinity, out_reg += 1.
488 __ Vrinta(F32, F32, temp1, in_reg);
489 __ Vmov(temp2, 0.5);
490 __ Vsub(F32, temp1, in_reg, temp1);
491 __ Vcmp(F32, temp1, temp2);
492 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
493 {
494 // Use ExactAsemblyScope here because we are using IT.
495 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
496 2 * kMaxInstructionSizeInBytes,
497 CodeBufferCheckScope::kMaximumSize);
498 __ it(eq);
499 __ add(eq, out_reg, out_reg, 1);
500 }
501
502 if (done.IsReferenced()) {
503 __ Bind(&done);
504 }
505}
506
Anton Kirilov5ec62182016-10-13 20:16:02 +0100507void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100508 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100509}
510
511void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
512 ArmVIXLAssembler* assembler = GetAssembler();
513 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000514 __ Ldrsb(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100515}
516
517void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100518 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100519}
520
521void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
522 ArmVIXLAssembler* assembler = GetAssembler();
523 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000524 __ Ldr(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100525}
526
527void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100528 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100529}
530
531void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
532 ArmVIXLAssembler* assembler = GetAssembler();
533 // Ignore upper 4B of long address.
534 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
535 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
536 // exception. So we can't use ldrd as addr may be unaligned.
537 vixl32::Register lo = LowRegisterFrom(invoke->GetLocations()->Out());
538 vixl32::Register hi = HighRegisterFrom(invoke->GetLocations()->Out());
539 if (addr.Is(lo)) {
540 __ Ldr(hi, MemOperand(addr, 4));
Scott Wakelingb77051e2016-11-21 19:46:00 +0000541 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100542 } else {
Scott Wakelingb77051e2016-11-21 19:46:00 +0000543 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100544 __ Ldr(hi, MemOperand(addr, 4));
545 }
546}
547
548void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100549 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100550}
551
552void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
553 ArmVIXLAssembler* assembler = GetAssembler();
554 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000555 __ Ldrsh(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100556}
557
Vladimir Markoca6fff82017-10-03 14:49:14 +0100558static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
559 LocationSummary* locations =
560 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100561 locations->SetInAt(0, Location::RequiresRegister());
562 locations->SetInAt(1, Location::RequiresRegister());
563}
564
565void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100566 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100567}
568
569void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
570 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000571 __ Strb(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100572}
573
574void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100575 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100576}
577
578void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
579 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000580 __ Str(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100581}
582
583void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100584 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100585}
586
587void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
588 ArmVIXLAssembler* assembler = GetAssembler();
589 // Ignore upper 4B of long address.
590 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
591 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
592 // exception. So we can't use ldrd as addr may be unaligned.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000593 __ Str(LowRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100594 __ Str(HighRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr, 4));
595}
596
597void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100598 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100599}
600
601void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
602 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000603 __ Strh(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100604}
605
606void IntrinsicLocationsBuilderARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100607 LocationSummary* locations =
608 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100609 locations->SetOut(Location::RequiresRegister());
610}
611
612void IntrinsicCodeGeneratorARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
613 ArmVIXLAssembler* assembler = GetAssembler();
614 __ Ldr(OutputRegister(invoke),
615 MemOperand(tr, Thread::PeerOffset<kArmPointerSize>().Int32Value()));
616}
617
618static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100619 DataType::Type type,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100620 bool is_volatile,
621 CodeGeneratorARMVIXL* codegen) {
622 LocationSummary* locations = invoke->GetLocations();
623 ArmVIXLAssembler* assembler = codegen->GetAssembler();
624 Location base_loc = locations->InAt(1);
625 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
626 Location offset_loc = locations->InAt(2);
627 vixl32::Register offset = LowRegisterFrom(offset_loc); // Long offset, lo part only.
628 Location trg_loc = locations->Out();
629
630 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100631 case DataType::Type::kInt32: {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100632 vixl32::Register trg = RegisterFrom(trg_loc);
633 __ Ldr(trg, MemOperand(base, offset));
634 if (is_volatile) {
635 __ Dmb(vixl32::ISH);
636 }
637 break;
638 }
639
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100640 case DataType::Type::kReference: {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100641 vixl32::Register trg = RegisterFrom(trg_loc);
642 if (kEmitCompilerReadBarrier) {
643 if (kUseBakerReadBarrier) {
644 Location temp = locations->GetTemp(0);
645 codegen->GenerateReferenceLoadWithBakerReadBarrier(
646 invoke, trg_loc, base, 0U, offset_loc, TIMES_1, temp, /* needs_null_check */ false);
647 if (is_volatile) {
648 __ Dmb(vixl32::ISH);
649 }
650 } else {
651 __ Ldr(trg, MemOperand(base, offset));
652 if (is_volatile) {
653 __ Dmb(vixl32::ISH);
654 }
655 codegen->GenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
656 }
657 } else {
658 __ Ldr(trg, MemOperand(base, offset));
659 if (is_volatile) {
660 __ Dmb(vixl32::ISH);
661 }
662 assembler->MaybeUnpoisonHeapReference(trg);
663 }
664 break;
665 }
666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100667 case DataType::Type::kInt64: {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100668 vixl32::Register trg_lo = LowRegisterFrom(trg_loc);
669 vixl32::Register trg_hi = HighRegisterFrom(trg_loc);
670 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
Artem Serov657022c2016-11-23 14:19:38 +0000671 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
672 const vixl32::Register temp_reg = temps.Acquire();
673 __ Add(temp_reg, base, offset);
674 __ Ldrexd(trg_lo, trg_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100675 } else {
676 __ Ldrd(trg_lo, trg_hi, MemOperand(base, offset));
677 }
678 if (is_volatile) {
679 __ Dmb(vixl32::ISH);
680 }
681 break;
682 }
683
684 default:
685 LOG(FATAL) << "Unexpected type " << type;
686 UNREACHABLE();
687 }
688}
689
Vladimir Markoca6fff82017-10-03 14:49:14 +0100690static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100691 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100692 DataType::Type type) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100693 bool can_call = kEmitCompilerReadBarrier &&
694 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
695 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100696 LocationSummary* locations =
697 new (allocator) LocationSummary(invoke,
698 can_call
699 ? LocationSummary::kCallOnSlowPath
700 : LocationSummary::kNoCall,
701 kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100702 if (can_call && kUseBakerReadBarrier) {
703 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
704 }
705 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
706 locations->SetInAt(1, Location::RequiresRegister());
707 locations->SetInAt(2, Location::RequiresRegister());
708 locations->SetOut(Location::RequiresRegister(),
709 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100710 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100711 // We need a temporary register for the read barrier marking slow
Roland Levillain9983e302017-07-14 14:34:22 +0100712 // path in CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier.
Anton Kirilov5ec62182016-10-13 20:16:02 +0100713 locations->AddTemp(Location::RequiresRegister());
714 }
715}
716
717void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100718 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100719}
720void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100721 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100722}
723void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100724 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100725}
726void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100727 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100728}
729void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100730 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kReference);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100731}
732void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100733 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kReference);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100734}
735
736void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100737 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100738}
739void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100740 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100741}
742void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100743 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100744}
745void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100746 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100747}
748void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100749 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100750}
751void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100752 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100753}
754
Vladimir Markoca6fff82017-10-03 14:49:14 +0100755static void CreateIntIntIntIntToVoid(ArenaAllocator* allocator,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100756 const ArmInstructionSetFeatures& features,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100757 DataType::Type type,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100758 bool is_volatile,
759 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100760 LocationSummary* locations =
761 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100762 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
763 locations->SetInAt(1, Location::RequiresRegister());
764 locations->SetInAt(2, Location::RequiresRegister());
765 locations->SetInAt(3, Location::RequiresRegister());
766
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100767 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100768 // Potentially need temps for ldrexd-strexd loop.
769 if (is_volatile && !features.HasAtomicLdrdAndStrd()) {
770 locations->AddTemp(Location::RequiresRegister()); // Temp_lo.
771 locations->AddTemp(Location::RequiresRegister()); // Temp_hi.
772 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100773 } else if (type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100774 // Temps for card-marking.
775 locations->AddTemp(Location::RequiresRegister()); // Temp.
776 locations->AddTemp(Location::RequiresRegister()); // Card.
777 }
778}
779
780void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100781 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100782 allocator_, features_, DataType::Type::kInt32, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100783}
784void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100785 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100786 allocator_, features_, DataType::Type::kInt32, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100787}
788void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100789 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100790 allocator_, features_, DataType::Type::kInt32, /* is_volatile */ true, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100791}
792void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100793 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100794 allocator_, features_, DataType::Type::kReference, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100795}
796void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100797 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100798 allocator_, features_, DataType::Type::kReference, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100799}
800void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100801 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100802 allocator_, features_, DataType::Type::kReference, /* is_volatile */ true, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100803}
804void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
805 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100806 allocator_, features_, DataType::Type::kInt64, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100807}
808void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
809 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100810 allocator_, features_, DataType::Type::kInt64, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100811}
812void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
813 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100814 allocator_, features_, DataType::Type::kInt64, /* is_volatile */ true, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100815}
816
817static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100818 DataType::Type type,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100819 bool is_volatile,
820 bool is_ordered,
821 CodeGeneratorARMVIXL* codegen) {
822 ArmVIXLAssembler* assembler = codegen->GetAssembler();
823
824 vixl32::Register base = RegisterFrom(locations->InAt(1)); // Object pointer.
825 vixl32::Register offset = LowRegisterFrom(locations->InAt(2)); // Long offset, lo part only.
826 vixl32::Register value;
827
828 if (is_volatile || is_ordered) {
829 __ Dmb(vixl32::ISH);
830 }
831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100833 vixl32::Register value_lo = LowRegisterFrom(locations->InAt(3));
834 vixl32::Register value_hi = HighRegisterFrom(locations->InAt(3));
835 value = value_lo;
836 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
837 vixl32::Register temp_lo = RegisterFrom(locations->GetTemp(0));
838 vixl32::Register temp_hi = RegisterFrom(locations->GetTemp(1));
839 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
840 const vixl32::Register temp_reg = temps.Acquire();
841
842 __ Add(temp_reg, base, offset);
843 vixl32::Label loop_head;
844 __ Bind(&loop_head);
Scott Wakelingb77051e2016-11-21 19:46:00 +0000845 __ Ldrexd(temp_lo, temp_hi, MemOperand(temp_reg));
846 __ Strexd(temp_lo, value_lo, value_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100847 __ Cmp(temp_lo, 0);
Artem Serov517d9f62016-12-12 15:51:15 +0000848 __ B(ne, &loop_head, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100849 } else {
850 __ Strd(value_lo, value_hi, MemOperand(base, offset));
851 }
852 } else {
853 value = RegisterFrom(locations->InAt(3));
854 vixl32::Register source = value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100855 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100856 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
857 __ Mov(temp, value);
858 assembler->PoisonHeapReference(temp);
859 source = temp;
860 }
861 __ Str(source, MemOperand(base, offset));
862 }
863
864 if (is_volatile) {
865 __ Dmb(vixl32::ISH);
866 }
867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100868 if (type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100869 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
870 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
871 bool value_can_be_null = true; // TODO: Worth finding out this information?
872 codegen->MarkGCCard(temp, card, base, value, value_can_be_null);
873 }
874}
875
876void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePut(HInvoke* invoke) {
877 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100878 DataType::Type::kInt32,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100879 /* is_volatile */ false,
880 /* is_ordered */ false,
881 codegen_);
882}
883void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
884 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100885 DataType::Type::kInt32,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100886 /* is_volatile */ false,
887 /* is_ordered */ true,
888 codegen_);
889}
890void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
891 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100892 DataType::Type::kInt32,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100893 /* is_volatile */ true,
894 /* is_ordered */ false,
895 codegen_);
896}
897void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
898 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100899 DataType::Type::kReference,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100900 /* is_volatile */ false,
901 /* is_ordered */ false,
902 codegen_);
903}
904void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
905 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100906 DataType::Type::kReference,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100907 /* is_volatile */ false,
908 /* is_ordered */ true,
909 codegen_);
910}
911void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
912 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100913 DataType::Type::kReference,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100914 /* is_volatile */ true,
915 /* is_ordered */ false,
916 codegen_);
917}
918void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
919 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100920 DataType::Type::kInt64,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100921 /* is_volatile */ false,
922 /* is_ordered */ false,
923 codegen_);
924}
925void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
926 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100927 DataType::Type::kInt64,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100928 /* is_volatile */ false,
929 /* is_ordered */ true,
930 codegen_);
931}
932void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
933 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100934 DataType::Type::kInt64,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100935 /* is_volatile */ true,
936 /* is_ordered */ false,
937 codegen_);
938}
939
Vladimir Markoca6fff82017-10-03 14:49:14 +0100940static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* allocator,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100941 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100942 DataType::Type type) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100943 bool can_call = kEmitCompilerReadBarrier &&
944 kUseBakerReadBarrier &&
945 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100946 LocationSummary* locations =
947 new (allocator) LocationSummary(invoke,
948 can_call
949 ? LocationSummary::kCallOnSlowPath
950 : LocationSummary::kNoCall,
951 kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100952 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
953 locations->SetInAt(1, Location::RequiresRegister());
954 locations->SetInAt(2, Location::RequiresRegister());
955 locations->SetInAt(3, Location::RequiresRegister());
956 locations->SetInAt(4, Location::RequiresRegister());
957
958 // If heap poisoning is enabled, we don't want the unpoisoning
959 // operations to potentially clobber the output. Likewise when
960 // emitting a (Baker) read barrier, which may call.
961 Location::OutputOverlap overlaps =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100962 ((kPoisonHeapReferences && type == DataType::Type::kReference) || can_call)
Anton Kirilov5ec62182016-10-13 20:16:02 +0100963 ? Location::kOutputOverlap
964 : Location::kNoOutputOverlap;
965 locations->SetOut(Location::RequiresRegister(), overlaps);
966
967 // Temporary registers used in CAS. In the object case
968 // (UnsafeCASObject intrinsic), these are also used for
969 // card-marking, and possibly for (Baker) read barrier.
970 locations->AddTemp(Location::RequiresRegister()); // Pointer.
971 locations->AddTemp(Location::RequiresRegister()); // Temp 1.
972}
973
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100974static void GenCas(HInvoke* invoke, DataType::Type type, CodeGeneratorARMVIXL* codegen) {
975 DCHECK_NE(type, DataType::Type::kInt64);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100976
977 ArmVIXLAssembler* assembler = codegen->GetAssembler();
978 LocationSummary* locations = invoke->GetLocations();
979
980 Location out_loc = locations->Out();
981 vixl32::Register out = OutputRegister(invoke); // Boolean result.
982
983 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
984 Location offset_loc = locations->InAt(2);
985 vixl32::Register offset = LowRegisterFrom(offset_loc); // Offset (discard high 4B).
986 vixl32::Register expected = InputRegisterAt(invoke, 3); // Expected.
987 vixl32::Register value = InputRegisterAt(invoke, 4); // Value.
988
989 Location tmp_ptr_loc = locations->GetTemp(0);
990 vixl32::Register tmp_ptr = RegisterFrom(tmp_ptr_loc); // Pointer to actual memory.
991 vixl32::Register tmp = RegisterFrom(locations->GetTemp(1)); // Value in memory.
992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100993 if (type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100994 // The only read barrier implementation supporting the
995 // UnsafeCASObject intrinsic is the Baker-style read barriers.
996 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
997
998 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
999 // object and scan the receiver at the next GC for nothing.
1000 bool value_can_be_null = true; // TODO: Worth finding out this information?
1001 codegen->MarkGCCard(tmp_ptr, tmp, base, value, value_can_be_null);
1002
1003 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1004 // Need to make sure the reference stored in the field is a to-space
1005 // one before attempting the CAS or the CAS could fail incorrectly.
Roland Levillainff487002017-03-07 16:50:01 +00001006 codegen->UpdateReferenceFieldWithBakerReadBarrier(
Anton Kirilov5ec62182016-10-13 20:16:02 +01001007 invoke,
1008 out_loc, // Unused, used only as a "temporary" within the read barrier.
1009 base,
Roland Levillainff487002017-03-07 16:50:01 +00001010 /* field_offset */ offset_loc,
Anton Kirilov5ec62182016-10-13 20:16:02 +01001011 tmp_ptr_loc,
1012 /* needs_null_check */ false,
Roland Levillainff487002017-03-07 16:50:01 +00001013 tmp);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001014 }
1015 }
1016
1017 // Prevent reordering with prior memory operations.
1018 // Emit a DMB ISH instruction instead of an DMB ISHST one, as the
1019 // latter allows a preceding load to be delayed past the STXR
1020 // instruction below.
1021 __ Dmb(vixl32::ISH);
1022
1023 __ Add(tmp_ptr, base, offset);
1024
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001025 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001026 codegen->GetAssembler()->PoisonHeapReference(expected);
1027 if (value.Is(expected)) {
1028 // Do not poison `value`, as it is the same register as
1029 // `expected`, which has just been poisoned.
1030 } else {
1031 codegen->GetAssembler()->PoisonHeapReference(value);
1032 }
1033 }
1034
1035 // do {
1036 // tmp = [r_ptr] - expected;
1037 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1038 // result = tmp != 0;
1039
1040 vixl32::Label loop_head;
1041 __ Bind(&loop_head);
1042
Scott Wakelingb77051e2016-11-21 19:46:00 +00001043 __ Ldrex(tmp, MemOperand(tmp_ptr));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001044
1045 __ Subs(tmp, tmp, expected);
1046
1047 {
Artem Serov0fb37192016-12-06 18:13:40 +00001048 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1049 3 * kMaxInstructionSizeInBytes,
1050 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001051
1052 __ itt(eq);
Scott Wakelingb77051e2016-11-21 19:46:00 +00001053 __ strex(eq, tmp, value, MemOperand(tmp_ptr));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001054 __ cmp(eq, tmp, 1);
1055 }
1056
Artem Serov517d9f62016-12-12 15:51:15 +00001057 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001058
1059 __ Dmb(vixl32::ISH);
1060
1061 __ Rsbs(out, tmp, 1);
1062
1063 {
Artem Serov0fb37192016-12-06 18:13:40 +00001064 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1065 2 * kMaxInstructionSizeInBytes,
1066 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001067
1068 __ it(cc);
1069 __ mov(cc, out, 0);
1070 }
1071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001072 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001073 codegen->GetAssembler()->UnpoisonHeapReference(expected);
1074 if (value.Is(expected)) {
1075 // Do not unpoison `value`, as it is the same register as
1076 // `expected`, which has just been unpoisoned.
1077 } else {
1078 codegen->GetAssembler()->UnpoisonHeapReference(value);
1079 }
1080 }
1081}
1082
1083void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001084 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke, DataType::Type::kInt32);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001085}
1086void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1087 // The only read barrier implementation supporting the
1088 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1089 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1090 return;
1091 }
1092
Vladimir Markoca6fff82017-10-03 14:49:14 +01001093 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke, DataType::Type::kReference);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001094}
1095void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001096 GenCas(invoke, DataType::Type::kInt32, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001097}
1098void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1099 // The only read barrier implementation supporting the
1100 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1101 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001103 GenCas(invoke, DataType::Type::kReference, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001104}
1105
1106void IntrinsicLocationsBuilderARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1107 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 LocationSummary* locations =
1109 new (allocator_) LocationSummary(invoke,
1110 invoke->InputAt(1)->CanBeNull()
1111 ? LocationSummary::kCallOnSlowPath
1112 : LocationSummary::kNoCall,
1113 kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001114 locations->SetInAt(0, Location::RequiresRegister());
1115 locations->SetInAt(1, Location::RequiresRegister());
1116 locations->AddTemp(Location::RequiresRegister());
1117 locations->AddTemp(Location::RequiresRegister());
1118 locations->AddTemp(Location::RequiresRegister());
1119 // Need temporary registers for String compression's feature.
1120 if (mirror::kUseStringCompression) {
1121 locations->AddTemp(Location::RequiresRegister());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001122 }
1123 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1124}
1125
Artem Serov672b9c12017-12-05 18:04:07 +00001126// Forward declaration.
1127//
1128// ART build system imposes a size limit (deviceFrameSizeLimit) on the stack frames generated
1129// by the compiler for every C++ function, and if this function gets inlined in
1130// IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo, the limit will be exceeded, resulting in a
1131// build failure. That is the reason why NO_INLINE attribute is used.
1132static void NO_INLINE GenerateStringCompareToLoop(ArmVIXLAssembler* assembler,
1133 HInvoke* invoke,
1134 vixl32::Label* end,
1135 vixl32::Label* different_compression);
1136
Anton Kirilov5ec62182016-10-13 20:16:02 +01001137void IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1138 ArmVIXLAssembler* assembler = GetAssembler();
1139 LocationSummary* locations = invoke->GetLocations();
1140
Artem Serov672b9c12017-12-05 18:04:07 +00001141 const vixl32::Register str = InputRegisterAt(invoke, 0);
1142 const vixl32::Register arg = InputRegisterAt(invoke, 1);
1143 const vixl32::Register out = OutputRegister(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001144
Artem Serov672b9c12017-12-05 18:04:07 +00001145 const vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1146 const vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1147 const vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001148 vixl32::Register temp3;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001149 if (mirror::kUseStringCompression) {
1150 temp3 = RegisterFrom(locations->GetTemp(3));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001151 }
1152
Anton Kirilov5ec62182016-10-13 20:16:02 +01001153 vixl32::Label end;
1154 vixl32::Label different_compression;
1155
1156 // Get offsets of count and value fields within a string object.
1157 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001158
1159 // Note that the null check must have been done earlier.
1160 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1161
1162 // Take slow path and throw if input can be and is null.
1163 SlowPathCodeARMVIXL* slow_path = nullptr;
1164 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1165 if (can_slow_path) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001166 slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001167 codegen_->AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001168 __ CompareAndBranchIfZero(arg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001169 }
1170
1171 // Reference equality check, return 0 if same reference.
1172 __ Subs(out, str, arg);
1173 __ B(eq, &end);
1174
Anton Kirilov5ec62182016-10-13 20:16:02 +01001175 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001176 // Load `count` fields of this and argument strings.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001177 __ Ldr(temp3, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001178 __ Ldr(temp2, MemOperand(arg, count_offset));
1179 // Extract lengths from the `count` fields.
1180 __ Lsr(temp0, temp3, 1u);
1181 __ Lsr(temp1, temp2, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001182 } else {
1183 // Load lengths of this and argument strings.
1184 __ Ldr(temp0, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001185 __ Ldr(temp1, MemOperand(arg, count_offset));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001186 }
1187 // out = length diff.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001188 __ Subs(out, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001189 // temp0 = min(len(str), len(arg)).
1190
1191 {
Artem Serov0fb37192016-12-06 18:13:40 +00001192 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1193 2 * kMaxInstructionSizeInBytes,
1194 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001195
1196 __ it(gt);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001197 __ mov(gt, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001198 }
1199
Anton Kirilov5ec62182016-10-13 20:16:02 +01001200 // Shorter string is empty?
xueliang.zhongf51bc622016-11-04 09:23:32 +00001201 // Note that mirror::kUseStringCompression==true introduces lots of instructions,
1202 // which makes &end label far away from this branch and makes it not 'CBZ-encodable'.
1203 __ CompareAndBranchIfZero(temp0, &end, mirror::kUseStringCompression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001204
1205 if (mirror::kUseStringCompression) {
1206 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001207 __ Eors(temp2, temp2, temp3);
1208 __ Lsrs(temp2, temp2, 1u);
1209 __ B(cs, &different_compression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001210 // For string compression, calculate the number of bytes to compare (not chars).
1211 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001212 __ Lsls(temp3, temp3, 31u); // Extract purely the compression flag.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001213
Artem Serov0fb37192016-12-06 18:13:40 +00001214 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1215 2 * kMaxInstructionSizeInBytes,
1216 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001217
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001218 __ it(ne);
1219 __ add(ne, temp0, temp0, temp0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001220 }
1221
Artem Serov672b9c12017-12-05 18:04:07 +00001222
1223 GenerateStringCompareToLoop(assembler, invoke, &end, &different_compression);
1224
1225 __ Bind(&end);
1226
1227 if (can_slow_path) {
1228 __ Bind(slow_path->GetExitLabel());
1229 }
1230}
1231
1232static void GenerateStringCompareToLoop(ArmVIXLAssembler* assembler,
1233 HInvoke* invoke,
1234 vixl32::Label* end,
1235 vixl32::Label* different_compression) {
1236 LocationSummary* locations = invoke->GetLocations();
1237
1238 const vixl32::Register str = InputRegisterAt(invoke, 0);
1239 const vixl32::Register arg = InputRegisterAt(invoke, 1);
1240 const vixl32::Register out = OutputRegister(invoke);
1241
1242 const vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1243 const vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1244 const vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
1245 vixl32::Register temp3;
1246 if (mirror::kUseStringCompression) {
1247 temp3 = RegisterFrom(locations->GetTemp(3));
1248 }
1249
1250 vixl32::Label loop;
1251 vixl32::Label find_char_diff;
1252
1253 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001254 // Store offset of string value in preparation for comparison loop.
1255 __ Mov(temp1, value_offset);
1256
Anton Kirilov5ec62182016-10-13 20:16:02 +01001257 // Assertions that must hold in order to compare multiple characters at a time.
1258 CHECK_ALIGNED(value_offset, 8);
1259 static_assert(IsAligned<8>(kObjectAlignment),
1260 "String data must be 8-byte aligned for unrolled CompareTo loop.");
1261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001262 const unsigned char_size = DataType::Size(DataType::Type::kUint16);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001263 DCHECK_EQ(char_size, 2u);
1264
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001265 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1266
Anton Kirilov5ec62182016-10-13 20:16:02 +01001267 vixl32::Label find_char_diff_2nd_cmp;
1268 // Unrolled loop comparing 4x16-bit chars per iteration (ok because of string data alignment).
1269 __ Bind(&loop);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001270 vixl32::Register temp_reg = temps.Acquire();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001271 __ Ldr(temp_reg, MemOperand(str, temp1));
1272 __ Ldr(temp2, MemOperand(arg, temp1));
1273 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001274 __ B(ne, &find_char_diff, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001275 __ Add(temp1, temp1, char_size * 2);
1276
1277 __ Ldr(temp_reg, MemOperand(str, temp1));
1278 __ Ldr(temp2, MemOperand(arg, temp1));
1279 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001280 __ B(ne, &find_char_diff_2nd_cmp, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001281 __ Add(temp1, temp1, char_size * 2);
1282 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1283 __ Subs(temp0, temp0, (mirror::kUseStringCompression ? 8 : 4));
Artem Serov517d9f62016-12-12 15:51:15 +00001284 __ B(hi, &loop, /* far_target */ false);
Artem Serov672b9c12017-12-05 18:04:07 +00001285 __ B(end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001286
1287 __ Bind(&find_char_diff_2nd_cmp);
1288 if (mirror::kUseStringCompression) {
1289 __ Subs(temp0, temp0, 4); // 4 bytes previously compared.
Artem Serov672b9c12017-12-05 18:04:07 +00001290 __ B(ls, end, /* far_target */ false); // Was the second comparison fully beyond the end?
Anton Kirilov5ec62182016-10-13 20:16:02 +01001291 } else {
1292 // Without string compression, we can start treating temp0 as signed
1293 // and rely on the signed comparison below.
1294 __ Sub(temp0, temp0, 2);
1295 }
1296
1297 // Find the single character difference.
1298 __ Bind(&find_char_diff);
1299 // Get the bit position of the first character that differs.
1300 __ Eor(temp1, temp2, temp_reg);
1301 __ Rbit(temp1, temp1);
1302 __ Clz(temp1, temp1);
1303
1304 // temp0 = number of characters remaining to compare.
1305 // (Without string compression, it could be < 1 if a difference is found by the second CMP
1306 // in the comparison loop, and after the end of the shorter string data).
1307
1308 // Without string compression (temp1 >> 4) = character where difference occurs between the last
1309 // two words compared, in the interval [0,1].
1310 // (0 for low half-word different, 1 for high half-word different).
1311 // With string compression, (temp1 << 3) = byte where the difference occurs,
1312 // in the interval [0,3].
1313
1314 // If temp0 <= (temp1 >> (kUseStringCompression ? 3 : 4)), the difference occurs outside
1315 // the remaining string data, so just return length diff (out).
1316 // The comparison is unsigned for string compression, otherwise signed.
1317 __ Cmp(temp0, Operand(temp1, vixl32::LSR, (mirror::kUseStringCompression ? 3 : 4)));
Artem Serov672b9c12017-12-05 18:04:07 +00001318 __ B((mirror::kUseStringCompression ? ls : le), end, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001319
Anton Kirilov5ec62182016-10-13 20:16:02 +01001320 // Extract the characters and calculate the difference.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001321 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001322 // For compressed strings we need to clear 0x7 from temp1, for uncompressed we need to clear
1323 // 0xf. We also need to prepare the character extraction mask `uncompressed ? 0xffffu : 0xffu`.
1324 // The compression flag is now in the highest bit of temp3, so let's play some tricks.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001325 __ Orr(temp3, temp3, 0xffu << 23); // uncompressed ? 0xff800000u : 0x7ff80000u
1326 __ Bic(temp1, temp1, Operand(temp3, vixl32::LSR, 31 - 3)); // &= ~(uncompressed ? 0xfu : 0x7u)
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001327 __ Asr(temp3, temp3, 7u); // uncompressed ? 0xffff0000u : 0xff0000u.
1328 __ Lsr(temp2, temp2, temp1); // Extract second character.
1329 __ Lsr(temp3, temp3, 16u); // uncompressed ? 0xffffu : 0xffu
1330 __ Lsr(out, temp_reg, temp1); // Extract first character.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001331 __ And(temp2, temp2, temp3);
1332 __ And(out, out, temp3);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001333 } else {
Anton Kirilovb88c4842016-11-14 14:37:00 +00001334 __ Bic(temp1, temp1, 0xf);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001335 __ Lsr(temp2, temp2, temp1);
1336 __ Lsr(out, temp_reg, temp1);
Anton Kirilovb88c4842016-11-14 14:37:00 +00001337 __ Movt(temp2, 0);
1338 __ Movt(out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001339 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001340
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001341 __ Sub(out, out, temp2);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001342 temps.Release(temp_reg);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001343
1344 if (mirror::kUseStringCompression) {
Artem Serov672b9c12017-12-05 18:04:07 +00001345 __ B(end);
1346 __ Bind(different_compression);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001347
1348 // Comparison for different compression style.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001349 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001350 DCHECK_EQ(c_char_size, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001351
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001352 // We want to free up the temp3, currently holding `str.count`, for comparison.
1353 // So, we move it to the bottom bit of the iteration count `temp0` which we tnen
1354 // need to treat as unsigned. Start by freeing the bit with an ADD and continue
1355 // further down by a LSRS+SBC which will flip the meaning of the flag but allow
1356 // `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001357 __ Add(temp0, temp0, temp0); // Unlike LSL, this ADD is always 16-bit.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001358 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001359 __ Mov(temp1, str);
1360 __ Mov(temp2, arg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001361 __ Lsrs(temp3, temp3, 1u); // Continue the move of the compression flag.
1362 {
Artem Serov0fb37192016-12-06 18:13:40 +00001363 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1364 3 * kMaxInstructionSizeInBytes,
1365 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001366 __ itt(cs); // Interleave with selection of temp1 and temp2.
1367 __ mov(cs, temp1, arg); // Preserves flags.
1368 __ mov(cs, temp2, str); // Preserves flags.
1369 }
Anton Kirilovb88c4842016-11-14 14:37:00 +00001370 __ Sbc(temp0, temp0, 0); // Complete the move of the compression flag.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001371
1372 // Adjust temp1 and temp2 from string pointers to data pointers.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001373 __ Add(temp1, temp1, value_offset);
1374 __ Add(temp2, temp2, value_offset);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001375
1376 vixl32::Label different_compression_loop;
1377 vixl32::Label different_compression_diff;
1378
1379 // Main loop for different compression.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001380 temp_reg = temps.Acquire();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001381 __ Bind(&different_compression_loop);
1382 __ Ldrb(temp_reg, MemOperand(temp1, c_char_size, PostIndex));
1383 __ Ldrh(temp3, MemOperand(temp2, char_size, PostIndex));
Anton Kirilovb88c4842016-11-14 14:37:00 +00001384 __ Cmp(temp_reg, temp3);
Artem Serov517d9f62016-12-12 15:51:15 +00001385 __ B(ne, &different_compression_diff, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001386 __ Subs(temp0, temp0, 2);
Artem Serov517d9f62016-12-12 15:51:15 +00001387 __ B(hi, &different_compression_loop, /* far_target */ false);
Artem Serov672b9c12017-12-05 18:04:07 +00001388 __ B(end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001389
1390 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001391 __ Bind(&different_compression_diff);
1392 __ Sub(out, temp_reg, temp3);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001393 temps.Release(temp_reg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001394 // Flip the difference if the `arg` is compressed.
1395 // `temp0` contains inverted `str` compression flag, i.e the same as `arg` compression flag.
1396 __ Lsrs(temp0, temp0, 1u);
1397 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1398 "Expecting 0=compressed, 1=uncompressed");
1399
Artem Serov0fb37192016-12-06 18:13:40 +00001400 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1401 2 * kMaxInstructionSizeInBytes,
1402 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001403 __ it(cc);
1404 __ rsb(cc, out, out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001405 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001406}
1407
Vladimir Marko984519c2017-08-23 10:45:29 +01001408// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1409// The normal loop plus the pre-header is 9 instructions (18-26 bytes) without string compression
1410// and 12 instructions (24-32 bytes) with string compression. We can compare up to 4 bytes in 4
1411// instructions (LDR+LDR+CMP+BNE) and up to 8 bytes in 6 instructions (LDRD+LDRD+CMP+BNE+CMP+BNE).
1412// Allow up to 12 instructions (32 bytes) for the unrolled loop.
1413constexpr size_t kShortConstStringEqualsCutoffInBytes = 16;
1414
1415static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1416 if (candidate->IsLoadString()) {
1417 HLoadString* load_string = candidate->AsLoadString();
1418 const DexFile& dex_file = load_string->GetDexFile();
1419 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1420 }
1421 return nullptr;
1422}
1423
Anton Kirilov5ec62182016-10-13 20:16:02 +01001424void IntrinsicLocationsBuilderARMVIXL::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001425 if (kEmitCompilerReadBarrier &&
1426 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1427 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1428 // No support for this odd case (String class is moveable, not in the boot image).
1429 return;
1430 }
1431
Vladimir Markoca6fff82017-10-03 14:49:14 +01001432 LocationSummary* locations =
1433 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001434 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1435 locations->SetInAt(0, Location::RequiresRegister());
1436 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko984519c2017-08-23 10:45:29 +01001437
Anton Kirilov5ec62182016-10-13 20:16:02 +01001438 // Temporary registers to store lengths of strings and for calculations.
1439 // Using instruction cbz requires a low register, so explicitly set a temp to be R0.
1440 locations->AddTemp(LocationFrom(r0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001441
Vladimir Marko984519c2017-08-23 10:45:29 +01001442 // For the generic implementation and for long const strings we need an extra temporary.
1443 // We do not need it for short const strings, up to 4 bytes, see code generation below.
1444 uint32_t const_string_length = 0u;
1445 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1446 if (const_string == nullptr) {
1447 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1448 }
1449 bool is_compressed =
1450 mirror::kUseStringCompression &&
1451 const_string != nullptr &&
1452 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1453 if (const_string == nullptr || const_string_length > (is_compressed ? 4u : 2u)) {
1454 locations->AddTemp(Location::RequiresRegister());
1455 }
1456
1457 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1458 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1459 // Then we shall need an extra temporary register instead of the output register.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001460 locations->SetOut(Location::RequiresRegister());
1461}
1462
1463void IntrinsicCodeGeneratorARMVIXL::VisitStringEquals(HInvoke* invoke) {
1464 ArmVIXLAssembler* assembler = GetAssembler();
1465 LocationSummary* locations = invoke->GetLocations();
1466
1467 vixl32::Register str = InputRegisterAt(invoke, 0);
1468 vixl32::Register arg = InputRegisterAt(invoke, 1);
1469 vixl32::Register out = OutputRegister(invoke);
1470
1471 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001472
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001473 vixl32::Label loop;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001474 vixl32::Label end;
1475 vixl32::Label return_true;
1476 vixl32::Label return_false;
Anton Kirilov6f644202017-02-27 18:29:45 +00001477 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001478
1479 // Get offsets of count, value, and class fields within a string object.
1480 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1481 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1482 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1483
1484 // Note that the null check must have been done earlier.
1485 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1486
1487 StringEqualsOptimizations optimizations(invoke);
1488 if (!optimizations.GetArgumentNotNull()) {
1489 // Check if input is null, return false if it is.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001490 __ CompareAndBranchIfZero(arg, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001491 }
1492
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001493 // Reference equality check, return true if same reference.
1494 __ Cmp(str, arg);
Artem Serov517d9f62016-12-12 15:51:15 +00001495 __ B(eq, &return_true, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001496
Anton Kirilov5ec62182016-10-13 20:16:02 +01001497 if (!optimizations.GetArgumentIsString()) {
1498 // Instanceof check for the argument by comparing class fields.
1499 // All string objects must have the same type since String cannot be subclassed.
1500 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1501 // If the argument is a string object, its class field must be equal to receiver's class field.
1502 __ Ldr(temp, MemOperand(str, class_offset));
Vladimir Marko984519c2017-08-23 10:45:29 +01001503 __ Ldr(out, MemOperand(arg, class_offset));
1504 __ Cmp(temp, out);
Artem Serov517d9f62016-12-12 15:51:15 +00001505 __ B(ne, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001506 }
1507
Vladimir Marko984519c2017-08-23 10:45:29 +01001508 // Check if one of the inputs is a const string. Do not special-case both strings
1509 // being const, such cases should be handled by constant folding if needed.
1510 uint32_t const_string_length = 0u;
1511 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1512 if (const_string == nullptr) {
1513 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1514 if (const_string != nullptr) {
1515 std::swap(str, arg); // Make sure the const string is in `str`.
1516 }
1517 }
1518 bool is_compressed =
1519 mirror::kUseStringCompression &&
1520 const_string != nullptr &&
1521 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1522
1523 if (const_string != nullptr) {
1524 // Load `count` field of the argument string and check if it matches the const string.
1525 // Also compares the compression style, if differs return false.
1526 __ Ldr(temp, MemOperand(arg, count_offset));
1527 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
1528 __ B(ne, &return_false, /* far_target */ false);
1529 } else {
1530 // Load `count` fields of this and argument strings.
1531 __ Ldr(temp, MemOperand(str, count_offset));
1532 __ Ldr(out, MemOperand(arg, count_offset));
1533 // Check if `count` fields are equal, return false if they're not.
1534 // Also compares the compression style, if differs return false.
1535 __ Cmp(temp, out);
1536 __ B(ne, &return_false, /* far_target */ false);
1537 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001538
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001539 // Assertions that must hold in order to compare strings 4 bytes at a time.
Vladimir Marko984519c2017-08-23 10:45:29 +01001540 // Ok to do this because strings are zero-padded to kObjectAlignment.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001541 DCHECK_ALIGNED(value_offset, 4);
1542 static_assert(IsAligned<4>(kObjectAlignment), "String data must be aligned for fast compare.");
1543
Vladimir Marko984519c2017-08-23 10:45:29 +01001544 if (const_string != nullptr &&
1545 const_string_length <= (is_compressed ? kShortConstStringEqualsCutoffInBytes
1546 : kShortConstStringEqualsCutoffInBytes / 2u)) {
1547 // Load and compare the contents. Though we know the contents of the short const string
1548 // at compile time, materializing constants may be more code than loading from memory.
1549 int32_t offset = value_offset;
1550 size_t remaining_bytes =
1551 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 4u);
1552 while (remaining_bytes > sizeof(uint32_t)) {
1553 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1554 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1555 vixl32::Register temp2 = scratch_scope.Acquire();
1556 __ Ldrd(temp, temp1, MemOperand(str, offset));
1557 __ Ldrd(temp2, out, MemOperand(arg, offset));
1558 __ Cmp(temp, temp2);
1559 __ B(ne, &return_false, /* far_label */ false);
1560 __ Cmp(temp1, out);
1561 __ B(ne, &return_false, /* far_label */ false);
1562 offset += 2u * sizeof(uint32_t);
1563 remaining_bytes -= 2u * sizeof(uint32_t);
1564 }
1565 if (remaining_bytes != 0u) {
1566 __ Ldr(temp, MemOperand(str, offset));
1567 __ Ldr(out, MemOperand(arg, offset));
1568 __ Cmp(temp, out);
1569 __ B(ne, &return_false, /* far_label */ false);
1570 }
1571 } else {
1572 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1573 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1574 "Expecting 0=compressed, 1=uncompressed");
1575 __ CompareAndBranchIfZero(temp, &return_true, /* far_target */ false);
1576
1577 if (mirror::kUseStringCompression) {
1578 // For string compression, calculate the number of bytes to compare (not chars).
1579 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1580 __ Lsrs(temp, temp, 1u); // Extract length and check compression flag.
1581 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1582 2 * kMaxInstructionSizeInBytes,
1583 CodeBufferCheckScope::kMaximumSize);
1584 __ it(cs); // If uncompressed,
1585 __ add(cs, temp, temp, temp); // double the byte count.
1586 }
1587
1588 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1589 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1590 vixl32::Register temp2 = scratch_scope.Acquire();
1591
1592 // Store offset of string value in preparation for comparison loop.
1593 __ Mov(temp1, value_offset);
1594
1595 // Loop to compare strings 4 bytes at a time starting at the front of the string.
1596 __ Bind(&loop);
1597 __ Ldr(out, MemOperand(str, temp1));
1598 __ Ldr(temp2, MemOperand(arg, temp1));
1599 __ Add(temp1, temp1, Operand::From(sizeof(uint32_t)));
1600 __ Cmp(out, temp2);
1601 __ B(ne, &return_false, /* far_target */ false);
1602 // With string compression, we have compared 4 bytes, otherwise 2 chars.
1603 __ Subs(temp, temp, mirror::kUseStringCompression ? 4 : 2);
1604 __ B(hi, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001605 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001606
Anton Kirilov5ec62182016-10-13 20:16:02 +01001607 // Return true and exit the function.
1608 // If loop does not result in returning false, we return true.
1609 __ Bind(&return_true);
1610 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00001611 __ B(final_label);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001612
1613 // Return false and exit the function.
1614 __ Bind(&return_false);
1615 __ Mov(out, 0);
Anton Kirilov6f644202017-02-27 18:29:45 +00001616
1617 if (end.IsReferenced()) {
1618 __ Bind(&end);
1619 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001620}
1621
1622static void GenerateVisitStringIndexOf(HInvoke* invoke,
1623 ArmVIXLAssembler* assembler,
1624 CodeGeneratorARMVIXL* codegen,
Anton Kirilov5ec62182016-10-13 20:16:02 +01001625 bool start_at_zero) {
1626 LocationSummary* locations = invoke->GetLocations();
1627
1628 // Note that the null check must have been done earlier.
1629 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1630
1631 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1632 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
1633 SlowPathCodeARMVIXL* slow_path = nullptr;
1634 HInstruction* code_point = invoke->InputAt(1);
1635 if (code_point->IsIntConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001636 if (static_cast<uint32_t>(Int32ConstantFrom(code_point)) >
Anton Kirilov5ec62182016-10-13 20:16:02 +01001637 std::numeric_limits<uint16_t>::max()) {
1638 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1639 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001640 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001641 codegen->AddSlowPath(slow_path);
1642 __ B(slow_path->GetEntryLabel());
1643 __ Bind(slow_path->GetExitLabel());
1644 return;
1645 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001646 } else if (code_point->GetType() != DataType::Type::kUint16) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001647 vixl32::Register char_reg = InputRegisterAt(invoke, 1);
1648 // 0xffff is not modified immediate but 0x10000 is, so use `>= 0x10000` instead of `> 0xffff`.
1649 __ Cmp(char_reg, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()) + 1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001650 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001651 codegen->AddSlowPath(slow_path);
1652 __ B(hs, slow_path->GetEntryLabel());
1653 }
1654
1655 if (start_at_zero) {
1656 vixl32::Register tmp_reg = RegisterFrom(locations->GetTemp(0));
1657 DCHECK(tmp_reg.Is(r2));
1658 // Start-index = 0.
1659 __ Mov(tmp_reg, 0);
1660 }
1661
1662 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
1663 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
1664
1665 if (slow_path != nullptr) {
1666 __ Bind(slow_path->GetExitLabel());
1667 }
1668}
1669
1670void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001671 LocationSummary* locations = new (allocator_) LocationSummary(
1672 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001673 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1674 // best to align the inputs accordingly.
1675 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1676 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1677 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1678 locations->SetOut(LocationFrom(r0));
1679
1680 // Need to send start-index=0.
1681 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1682}
1683
1684void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001685 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001686}
1687
1688void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001689 LocationSummary* locations = new (allocator_) LocationSummary(
1690 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001691 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1692 // best to align the inputs accordingly.
1693 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1694 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1695 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1696 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1697 locations->SetOut(LocationFrom(r0));
1698}
1699
1700void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001701 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001702}
1703
1704void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001705 LocationSummary* locations = new (allocator_) LocationSummary(
1706 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001707 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1708 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1709 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1710 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1711 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1712 locations->SetOut(LocationFrom(r0));
1713}
1714
1715void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
1716 ArmVIXLAssembler* assembler = GetAssembler();
1717 vixl32::Register byte_array = InputRegisterAt(invoke, 0);
1718 __ Cmp(byte_array, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001719 SlowPathCodeARMVIXL* slow_path =
1720 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001721 codegen_->AddSlowPath(slow_path);
1722 __ B(eq, slow_path->GetEntryLabel());
1723
1724 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
1725 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
1726 __ Bind(slow_path->GetExitLabel());
1727}
1728
1729void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001730 LocationSummary* locations =
1731 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001732 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1733 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1734 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1735 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1736 locations->SetOut(LocationFrom(r0));
1737}
1738
1739void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
1740 // No need to emit code checking whether `locations->InAt(2)` is a null
1741 // pointer, as callers of the native method
1742 //
1743 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1744 //
1745 // all include a null check on `data` before calling that method.
1746 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
1747 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
1748}
1749
1750void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001751 LocationSummary* locations = new (allocator_) LocationSummary(
1752 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001753 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1754 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1755 locations->SetOut(LocationFrom(r0));
1756}
1757
1758void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
1759 ArmVIXLAssembler* assembler = GetAssembler();
1760 vixl32::Register string_to_copy = InputRegisterAt(invoke, 0);
1761 __ Cmp(string_to_copy, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001762 SlowPathCodeARMVIXL* slow_path =
1763 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001764 codegen_->AddSlowPath(slow_path);
1765 __ B(eq, slow_path->GetEntryLabel());
1766
1767 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
1768 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
1769
1770 __ Bind(slow_path->GetExitLabel());
1771}
1772
1773void IntrinsicLocationsBuilderARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
1774 // The only read barrier implementation supporting the
1775 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1776 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1777 return;
1778 }
1779
1780 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
1781 LocationSummary* locations = invoke->GetLocations();
1782 if (locations == nullptr) {
1783 return;
1784 }
1785
1786 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1787 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1788 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1789
1790 if (src_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(src_pos->GetValue())) {
1791 locations->SetInAt(1, Location::RequiresRegister());
1792 }
1793 if (dest_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(dest_pos->GetValue())) {
1794 locations->SetInAt(3, Location::RequiresRegister());
1795 }
1796 if (length != nullptr && !assembler_->ShifterOperandCanAlwaysHold(length->GetValue())) {
1797 locations->SetInAt(4, Location::RequiresRegister());
1798 }
1799 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1800 // Temporary register IP cannot be used in
1801 // ReadBarrierSystemArrayCopySlowPathARM (because that register
1802 // is clobbered by ReadBarrierMarkRegX entry points). Get an extra
1803 // temporary register from the register allocator.
1804 locations->AddTemp(Location::RequiresRegister());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01001805 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen_);
1806 arm_codegen->MaybeAddBakerCcEntrypointTempForFields(locations);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001807 }
1808}
1809
1810static void CheckPosition(ArmVIXLAssembler* assembler,
1811 Location pos,
1812 vixl32::Register input,
1813 Location length,
1814 SlowPathCodeARMVIXL* slow_path,
1815 vixl32::Register temp,
1816 bool length_is_input_length = false) {
1817 // Where is the length in the Array?
1818 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1819
1820 if (pos.IsConstant()) {
1821 int32_t pos_const = Int32ConstantFrom(pos);
1822 if (pos_const == 0) {
1823 if (!length_is_input_length) {
1824 // Check that length(input) >= length.
1825 __ Ldr(temp, MemOperand(input, length_offset));
1826 if (length.IsConstant()) {
1827 __ Cmp(temp, Int32ConstantFrom(length));
1828 } else {
1829 __ Cmp(temp, RegisterFrom(length));
1830 }
1831 __ B(lt, slow_path->GetEntryLabel());
1832 }
1833 } else {
1834 // Check that length(input) >= pos.
1835 __ Ldr(temp, MemOperand(input, length_offset));
1836 __ Subs(temp, temp, pos_const);
1837 __ B(lt, slow_path->GetEntryLabel());
1838
1839 // Check that (length(input) - pos) >= length.
1840 if (length.IsConstant()) {
1841 __ Cmp(temp, Int32ConstantFrom(length));
1842 } else {
1843 __ Cmp(temp, RegisterFrom(length));
1844 }
1845 __ B(lt, slow_path->GetEntryLabel());
1846 }
1847 } else if (length_is_input_length) {
1848 // The only way the copy can succeed is if pos is zero.
1849 vixl32::Register pos_reg = RegisterFrom(pos);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001850 __ CompareAndBranchIfNonZero(pos_reg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001851 } else {
1852 // Check that pos >= 0.
1853 vixl32::Register pos_reg = RegisterFrom(pos);
1854 __ Cmp(pos_reg, 0);
1855 __ B(lt, slow_path->GetEntryLabel());
1856
1857 // Check that pos <= length(input).
1858 __ Ldr(temp, MemOperand(input, length_offset));
1859 __ Subs(temp, temp, pos_reg);
1860 __ B(lt, slow_path->GetEntryLabel());
1861
1862 // Check that (length(input) - pos) >= length.
1863 if (length.IsConstant()) {
1864 __ Cmp(temp, Int32ConstantFrom(length));
1865 } else {
1866 __ Cmp(temp, RegisterFrom(length));
1867 }
1868 __ B(lt, slow_path->GetEntryLabel());
1869 }
1870}
1871
1872void IntrinsicCodeGeneratorARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
1873 // The only read barrier implementation supporting the
1874 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1875 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1876
1877 ArmVIXLAssembler* assembler = GetAssembler();
1878 LocationSummary* locations = invoke->GetLocations();
1879
1880 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1881 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1882 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1883 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1884 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1885
1886 vixl32::Register src = InputRegisterAt(invoke, 0);
1887 Location src_pos = locations->InAt(1);
1888 vixl32::Register dest = InputRegisterAt(invoke, 2);
1889 Location dest_pos = locations->InAt(3);
1890 Location length = locations->InAt(4);
1891 Location temp1_loc = locations->GetTemp(0);
1892 vixl32::Register temp1 = RegisterFrom(temp1_loc);
1893 Location temp2_loc = locations->GetTemp(1);
1894 vixl32::Register temp2 = RegisterFrom(temp2_loc);
1895 Location temp3_loc = locations->GetTemp(2);
1896 vixl32::Register temp3 = RegisterFrom(temp3_loc);
1897
Vladimir Marko174b2e22017-10-12 13:34:49 +01001898 SlowPathCodeARMVIXL* intrinsic_slow_path =
1899 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001900 codegen_->AddSlowPath(intrinsic_slow_path);
1901
1902 vixl32::Label conditions_on_positions_validated;
1903 SystemArrayCopyOptimizations optimizations(invoke);
1904
1905 // If source and destination are the same, we go to slow path if we need to do
1906 // forward copying.
1907 if (src_pos.IsConstant()) {
1908 int32_t src_pos_constant = Int32ConstantFrom(src_pos);
1909 if (dest_pos.IsConstant()) {
1910 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
1911 if (optimizations.GetDestinationIsSource()) {
1912 // Checked when building locations.
1913 DCHECK_GE(src_pos_constant, dest_pos_constant);
1914 } else if (src_pos_constant < dest_pos_constant) {
1915 __ Cmp(src, dest);
1916 __ B(eq, intrinsic_slow_path->GetEntryLabel());
1917 }
1918
1919 // Checked when building locations.
1920 DCHECK(!optimizations.GetDestinationIsSource()
1921 || (src_pos_constant >= Int32ConstantFrom(dest_pos)));
1922 } else {
1923 if (!optimizations.GetDestinationIsSource()) {
1924 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00001925 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001926 }
1927 __ Cmp(RegisterFrom(dest_pos), src_pos_constant);
1928 __ B(gt, intrinsic_slow_path->GetEntryLabel());
1929 }
1930 } else {
1931 if (!optimizations.GetDestinationIsSource()) {
1932 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00001933 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001934 }
1935 if (dest_pos.IsConstant()) {
1936 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
1937 __ Cmp(RegisterFrom(src_pos), dest_pos_constant);
1938 } else {
1939 __ Cmp(RegisterFrom(src_pos), RegisterFrom(dest_pos));
1940 }
1941 __ B(lt, intrinsic_slow_path->GetEntryLabel());
1942 }
1943
1944 __ Bind(&conditions_on_positions_validated);
1945
1946 if (!optimizations.GetSourceIsNotNull()) {
1947 // Bail out if the source is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001948 __ CompareAndBranchIfZero(src, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001949 }
1950
1951 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1952 // Bail out if the destination is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001953 __ CompareAndBranchIfZero(dest, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001954 }
1955
1956 // If the length is negative, bail out.
1957 // We have already checked in the LocationsBuilder for the constant case.
1958 if (!length.IsConstant() &&
1959 !optimizations.GetCountIsSourceLength() &&
1960 !optimizations.GetCountIsDestinationLength()) {
1961 __ Cmp(RegisterFrom(length), 0);
1962 __ B(lt, intrinsic_slow_path->GetEntryLabel());
1963 }
1964
1965 // Validity checks: source.
1966 CheckPosition(assembler,
1967 src_pos,
1968 src,
1969 length,
1970 intrinsic_slow_path,
1971 temp1,
1972 optimizations.GetCountIsSourceLength());
1973
1974 // Validity checks: dest.
1975 CheckPosition(assembler,
1976 dest_pos,
1977 dest,
1978 length,
1979 intrinsic_slow_path,
1980 temp1,
1981 optimizations.GetCountIsDestinationLength());
1982
1983 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1984 // Check whether all elements of the source array are assignable to the component
1985 // type of the destination array. We do two checks: the classes are the same,
1986 // or the destination is Object[]. If none of these checks succeed, we go to the
1987 // slow path.
1988
1989 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1990 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1991 // /* HeapReference<Class> */ temp1 = src->klass_
1992 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1993 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
1994 // Bail out if the source is not a non primitive array.
1995 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1996 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1997 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001998 __ CompareAndBranchIfZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001999 // If heap poisoning is enabled, `temp1` has been unpoisoned
2000 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2001 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2002 __ Ldrh(temp1, MemOperand(temp1, primitive_offset));
2003 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002004 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002005 }
2006
2007 // /* HeapReference<Class> */ temp1 = dest->klass_
2008 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2009 invoke, temp1_loc, dest, class_offset, temp2_loc, /* needs_null_check */ false);
2010
2011 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2012 // Bail out if the destination is not a non primitive array.
2013 //
2014 // Register `temp1` is not trashed by the read barrier emitted
2015 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2016 // method produces a call to a ReadBarrierMarkRegX entry point,
2017 // which saves all potentially live registers, including
2018 // temporaries such a `temp1`.
2019 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2020 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2021 invoke, temp2_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002022 __ CompareAndBranchIfZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002023 // If heap poisoning is enabled, `temp2` has been unpoisoned
2024 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2025 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2026 __ Ldrh(temp2, MemOperand(temp2, primitive_offset));
2027 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002028 __ CompareAndBranchIfNonZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002029 }
2030
2031 // For the same reason given earlier, `temp1` is not trashed by the
2032 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2033 // /* HeapReference<Class> */ temp2 = src->klass_
2034 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2035 invoke, temp2_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
2036 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2037 __ Cmp(temp1, temp2);
2038
2039 if (optimizations.GetDestinationIsTypedObjectArray()) {
2040 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002041 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002042 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2043 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2044 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
2045 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2046 // We do not need to emit a read barrier for the following
2047 // heap reference load, as `temp1` is only used in a
2048 // comparison with null below, and this reference is not
2049 // kept afterwards.
2050 __ Ldr(temp1, MemOperand(temp1, super_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002051 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002052 __ Bind(&do_copy);
2053 } else {
2054 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2055 }
2056 } else {
2057 // Non read barrier code.
2058
2059 // /* HeapReference<Class> */ temp1 = dest->klass_
2060 __ Ldr(temp1, MemOperand(dest, class_offset));
2061 // /* HeapReference<Class> */ temp2 = src->klass_
2062 __ Ldr(temp2, MemOperand(src, class_offset));
2063 bool did_unpoison = false;
2064 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2065 !optimizations.GetSourceIsNonPrimitiveArray()) {
2066 // One or two of the references need to be unpoisoned. Unpoison them
2067 // both to make the identity check valid.
2068 assembler->MaybeUnpoisonHeapReference(temp1);
2069 assembler->MaybeUnpoisonHeapReference(temp2);
2070 did_unpoison = true;
2071 }
2072
2073 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2074 // Bail out if the destination is not a non primitive array.
2075 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2076 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002077 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002078 assembler->MaybeUnpoisonHeapReference(temp3);
2079 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2080 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2081 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002082 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002083 }
2084
2085 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2086 // Bail out if the source is not a non primitive array.
2087 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2088 __ Ldr(temp3, MemOperand(temp2, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002089 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002090 assembler->MaybeUnpoisonHeapReference(temp3);
2091 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2092 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2093 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002094 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002095 }
2096
2097 __ Cmp(temp1, temp2);
2098
2099 if (optimizations.GetDestinationIsTypedObjectArray()) {
2100 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002101 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002102 if (!did_unpoison) {
2103 assembler->MaybeUnpoisonHeapReference(temp1);
2104 }
2105 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2106 __ Ldr(temp1, MemOperand(temp1, component_offset));
2107 assembler->MaybeUnpoisonHeapReference(temp1);
2108 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2109 __ Ldr(temp1, MemOperand(temp1, super_offset));
2110 // No need to unpoison the result, we're comparing against null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002111 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002112 __ Bind(&do_copy);
2113 } else {
2114 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2115 }
2116 }
2117 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2118 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2119 // Bail out if the source is not a non primitive array.
2120 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2121 // /* HeapReference<Class> */ temp1 = src->klass_
2122 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2123 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2124 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2125 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2126 invoke, temp3_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002127 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002128 // If heap poisoning is enabled, `temp3` has been unpoisoned
2129 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2130 } else {
2131 // /* HeapReference<Class> */ temp1 = src->klass_
2132 __ Ldr(temp1, MemOperand(src, class_offset));
2133 assembler->MaybeUnpoisonHeapReference(temp1);
2134 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2135 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002136 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002137 assembler->MaybeUnpoisonHeapReference(temp3);
2138 }
2139 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2140 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2141 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002142 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002143 }
2144
Roland Levillain1663d162017-03-17 15:15:21 +00002145 if (length.IsConstant() && Int32ConstantFrom(length) == 0) {
2146 // Null constant length: not need to emit the loop code at all.
Anton Kirilov5ec62182016-10-13 20:16:02 +01002147 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002148 vixl32::Label done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 const DataType::Type type = DataType::Type::kReference;
2150 const int32_t element_size = DataType::Size(type);
Roland Levillain1663d162017-03-17 15:15:21 +00002151
2152 if (length.IsRegister()) {
2153 // Don't enter the copy loop if the length is null.
2154 __ CompareAndBranchIfZero(RegisterFrom(length), &done, /* is_far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002155 }
Roland Levillain1663d162017-03-17 15:15:21 +00002156
2157 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2158 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2159
2160 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002161 // also CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002162 //
2163 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2164 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2165 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2166 // if (is_gray) {
2167 // // Slow-path copy.
2168 // do {
2169 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2170 // } while (src_ptr != end_ptr)
2171 // } else {
2172 // // Fast-path copy.
2173 // do {
2174 // *dest_ptr++ = *src_ptr++;
2175 // } while (src_ptr != end_ptr)
2176 // }
2177
2178 // /* int32_t */ monitor = src->monitor_
2179 __ Ldr(temp2, MemOperand(src, monitor_offset));
2180 // /* LockWord */ lock_word = LockWord(monitor)
2181 static_assert(sizeof(LockWord) == sizeof(int32_t),
2182 "art::LockWord and int32_t have different sizes.");
2183
2184 // Introduce a dependency on the lock_word including the rb_state,
2185 // which shall prevent load-load reordering without using
2186 // a memory barrier (which would be more expensive).
2187 // `src` is unchanged by this operation, but its value now depends
2188 // on `temp2`.
2189 __ Add(src, src, Operand(temp2, vixl32::LSR, 32));
2190
2191 // Compute the base source address in `temp1`.
2192 // Note that `temp1` (the base source address) is computed from
2193 // `src` (and `src_pos`) here, and thus honors the artificial
2194 // dependency of `src` on `temp2`.
2195 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2196 // Compute the end source address in `temp3`.
2197 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2198 // The base destination address is computed later, as `temp2` is
2199 // used for intermediate computations.
2200
2201 // Slow path used to copy array when `src` is gray.
2202 // Note that the base destination address is computed in `temp2`
2203 // by the slow path code.
2204 SlowPathCodeARMVIXL* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002205 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathARMVIXL(invoke);
Roland Levillain1663d162017-03-17 15:15:21 +00002206 codegen_->AddSlowPath(read_barrier_slow_path);
2207
2208 // Given the numeric representation, it's enough to check the low bit of the
2209 // rb_state. We do that by shifting the bit out of the lock word with LSRS
2210 // which can be a 16-bit instruction unlike the TST immediate.
2211 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2212 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2213 __ Lsrs(temp2, temp2, LockWord::kReadBarrierStateShift + 1);
2214 // Carry flag is the last bit shifted out by LSRS.
2215 __ B(cs, read_barrier_slow_path->GetEntryLabel());
2216
2217 // Fast-path copy.
2218 // Compute the base destination address in `temp2`.
2219 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2220 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2221 // poison/unpoison.
2222 vixl32::Label loop;
2223 __ Bind(&loop);
2224 {
2225 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2226 const vixl32::Register temp_reg = temps.Acquire();
2227 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2228 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2229 }
2230 __ Cmp(temp1, temp3);
2231 __ B(ne, &loop, /* far_target */ false);
2232
2233 __ Bind(read_barrier_slow_path->GetExitLabel());
2234 } else {
2235 // Non read barrier code.
2236 // Compute the base source address in `temp1`.
2237 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2238 // Compute the base destination address in `temp2`.
2239 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2240 // Compute the end source address in `temp3`.
2241 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2242 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2243 // poison/unpoison.
2244 vixl32::Label loop;
2245 __ Bind(&loop);
2246 {
2247 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2248 const vixl32::Register temp_reg = temps.Acquire();
2249 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2250 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2251 }
2252 __ Cmp(temp1, temp3);
2253 __ B(ne, &loop, /* far_target */ false);
2254 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002255 __ Bind(&done);
2256 }
2257
2258 // We only need one card marking on the destination array.
2259 codegen_->MarkGCCard(temp1, temp2, dest, NoReg, /* value_can_be_null */ false);
2260
2261 __ Bind(intrinsic_slow_path->GetExitLabel());
2262}
2263
Vladimir Markoca6fff82017-10-03 14:49:14 +01002264static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002265 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2266 // the code generator. Furthermore, the register allocator creates fixed live intervals
2267 // for all caller-saved registers because we are doing a function call. As a result, if
2268 // the input and output locations are unallocated, the register allocator runs out of
2269 // registers and fails; however, a debuggable graph is not the common case.
2270 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2271 return;
2272 }
2273
2274 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 DCHECK_EQ(invoke->InputAt(0)->GetType(), DataType::Type::kFloat64);
2276 DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002277
Vladimir Markoca6fff82017-10-03 14:49:14 +01002278 LocationSummary* const locations =
2279 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002280 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2281
2282 locations->SetInAt(0, Location::RequiresFpuRegister());
2283 locations->SetOut(Location::RequiresFpuRegister());
2284 // Native code uses the soft float ABI.
2285 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2286 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2287}
2288
Vladimir Markoca6fff82017-10-03 14:49:14 +01002289static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002290 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2291 // the code generator. Furthermore, the register allocator creates fixed live intervals
2292 // for all caller-saved registers because we are doing a function call. As a result, if
2293 // the input and output locations are unallocated, the register allocator runs out of
2294 // registers and fails; however, a debuggable graph is not the common case.
2295 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2296 return;
2297 }
2298
2299 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 DCHECK_EQ(invoke->InputAt(0)->GetType(), DataType::Type::kFloat64);
2301 DCHECK_EQ(invoke->InputAt(1)->GetType(), DataType::Type::kFloat64);
2302 DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002303
Vladimir Markoca6fff82017-10-03 14:49:14 +01002304 LocationSummary* const locations =
2305 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002306 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2307
2308 locations->SetInAt(0, Location::RequiresFpuRegister());
2309 locations->SetInAt(1, Location::RequiresFpuRegister());
2310 locations->SetOut(Location::RequiresFpuRegister());
2311 // Native code uses the soft float ABI.
2312 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2313 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2314 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
2315 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(3)));
2316}
2317
2318static void GenFPToFPCall(HInvoke* invoke,
2319 ArmVIXLAssembler* assembler,
2320 CodeGeneratorARMVIXL* codegen,
2321 QuickEntrypointEnum entry) {
2322 LocationSummary* const locations = invoke->GetLocations();
2323
2324 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2325 DCHECK(locations->WillCall() && locations->Intrinsified());
2326
2327 // Native code uses the soft float ABI.
2328 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2329 RegisterFrom(locations->GetTemp(1)),
2330 InputDRegisterAt(invoke, 0));
2331 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2332 __ Vmov(OutputDRegister(invoke),
2333 RegisterFrom(locations->GetTemp(0)),
2334 RegisterFrom(locations->GetTemp(1)));
2335}
2336
2337static void GenFPFPToFPCall(HInvoke* invoke,
2338 ArmVIXLAssembler* assembler,
2339 CodeGeneratorARMVIXL* codegen,
2340 QuickEntrypointEnum entry) {
2341 LocationSummary* const locations = invoke->GetLocations();
2342
2343 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2344 DCHECK(locations->WillCall() && locations->Intrinsified());
2345
2346 // Native code uses the soft float ABI.
2347 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2348 RegisterFrom(locations->GetTemp(1)),
2349 InputDRegisterAt(invoke, 0));
2350 __ Vmov(RegisterFrom(locations->GetTemp(2)),
2351 RegisterFrom(locations->GetTemp(3)),
2352 InputDRegisterAt(invoke, 1));
2353 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2354 __ Vmov(OutputDRegister(invoke),
2355 RegisterFrom(locations->GetTemp(0)),
2356 RegisterFrom(locations->GetTemp(1)));
2357}
2358
2359void IntrinsicLocationsBuilderARMVIXL::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002360 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002361}
2362
2363void IntrinsicCodeGeneratorARMVIXL::VisitMathCos(HInvoke* invoke) {
2364 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCos);
2365}
2366
2367void IntrinsicLocationsBuilderARMVIXL::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002368 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002369}
2370
2371void IntrinsicCodeGeneratorARMVIXL::VisitMathSin(HInvoke* invoke) {
2372 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSin);
2373}
2374
2375void IntrinsicLocationsBuilderARMVIXL::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002376 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002377}
2378
2379void IntrinsicCodeGeneratorARMVIXL::VisitMathAcos(HInvoke* invoke) {
2380 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAcos);
2381}
2382
2383void IntrinsicLocationsBuilderARMVIXL::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002384 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002385}
2386
2387void IntrinsicCodeGeneratorARMVIXL::VisitMathAsin(HInvoke* invoke) {
2388 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAsin);
2389}
2390
2391void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002392 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002393}
2394
2395void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan(HInvoke* invoke) {
2396 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan);
2397}
2398
2399void IntrinsicLocationsBuilderARMVIXL::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002400 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002401}
2402
2403void IntrinsicCodeGeneratorARMVIXL::VisitMathCbrt(HInvoke* invoke) {
2404 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCbrt);
2405}
2406
2407void IntrinsicLocationsBuilderARMVIXL::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002408 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002409}
2410
2411void IntrinsicCodeGeneratorARMVIXL::VisitMathCosh(HInvoke* invoke) {
2412 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCosh);
2413}
2414
2415void IntrinsicLocationsBuilderARMVIXL::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002416 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002417}
2418
2419void IntrinsicCodeGeneratorARMVIXL::VisitMathExp(HInvoke* invoke) {
2420 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExp);
2421}
2422
2423void IntrinsicLocationsBuilderARMVIXL::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002424 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002425}
2426
2427void IntrinsicCodeGeneratorARMVIXL::VisitMathExpm1(HInvoke* invoke) {
2428 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExpm1);
2429}
2430
2431void IntrinsicLocationsBuilderARMVIXL::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002432 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002433}
2434
2435void IntrinsicCodeGeneratorARMVIXL::VisitMathLog(HInvoke* invoke) {
2436 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog);
2437}
2438
2439void IntrinsicLocationsBuilderARMVIXL::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002440 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002441}
2442
2443void IntrinsicCodeGeneratorARMVIXL::VisitMathLog10(HInvoke* invoke) {
2444 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog10);
2445}
2446
2447void IntrinsicLocationsBuilderARMVIXL::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002448 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002449}
2450
2451void IntrinsicCodeGeneratorARMVIXL::VisitMathSinh(HInvoke* invoke) {
2452 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSinh);
2453}
2454
2455void IntrinsicLocationsBuilderARMVIXL::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002456 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002457}
2458
2459void IntrinsicCodeGeneratorARMVIXL::VisitMathTan(HInvoke* invoke) {
2460 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTan);
2461}
2462
2463void IntrinsicLocationsBuilderARMVIXL::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002464 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002465}
2466
2467void IntrinsicCodeGeneratorARMVIXL::VisitMathTanh(HInvoke* invoke) {
2468 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTanh);
2469}
2470
2471void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002472 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002473}
2474
2475void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan2(HInvoke* invoke) {
2476 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan2);
2477}
2478
Vladimir Marko4d179872018-01-19 14:50:10 +00002479void IntrinsicLocationsBuilderARMVIXL::VisitMathPow(HInvoke* invoke) {
2480 CreateFPFPToFPCallLocations(allocator_, invoke);
2481}
2482
2483void IntrinsicCodeGeneratorARMVIXL::VisitMathPow(HInvoke* invoke) {
2484 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickPow);
2485}
2486
Anton Kirilov5ec62182016-10-13 20:16:02 +01002487void IntrinsicLocationsBuilderARMVIXL::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002488 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002489}
2490
2491void IntrinsicCodeGeneratorARMVIXL::VisitMathHypot(HInvoke* invoke) {
2492 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickHypot);
2493}
2494
2495void IntrinsicLocationsBuilderARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002496 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002497}
2498
2499void IntrinsicCodeGeneratorARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
2500 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickNextAfter);
2501}
2502
2503void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002504 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002505}
2506
2507void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
2508 ArmVIXLAssembler* assembler = GetAssembler();
2509 __ Rbit(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2510}
2511
2512void IntrinsicLocationsBuilderARMVIXL::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002513 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002514}
2515
2516void IntrinsicCodeGeneratorARMVIXL::VisitLongReverse(HInvoke* invoke) {
2517 ArmVIXLAssembler* assembler = GetAssembler();
2518 LocationSummary* locations = invoke->GetLocations();
2519
2520 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2521 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2522 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2523 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2524
2525 __ Rbit(out_reg_lo, in_reg_hi);
2526 __ Rbit(out_reg_hi, in_reg_lo);
2527}
2528
2529void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002530 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002531}
2532
2533void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
2534 ArmVIXLAssembler* assembler = GetAssembler();
2535 __ Rev(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2536}
2537
2538void IntrinsicLocationsBuilderARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002539 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002540}
2541
2542void IntrinsicCodeGeneratorARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
2543 ArmVIXLAssembler* assembler = GetAssembler();
2544 LocationSummary* locations = invoke->GetLocations();
2545
2546 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2547 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2548 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2549 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2550
2551 __ Rev(out_reg_lo, in_reg_hi);
2552 __ Rev(out_reg_hi, in_reg_lo);
2553}
2554
2555void IntrinsicLocationsBuilderARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002556 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002557}
2558
2559void IntrinsicCodeGeneratorARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
2560 ArmVIXLAssembler* assembler = GetAssembler();
2561 __ Revsh(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2562}
2563
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564static void GenBitCount(HInvoke* instr, DataType::Type type, ArmVIXLAssembler* assembler) {
2565 DCHECK(DataType::IsIntOrLongType(type)) << type;
2566 DCHECK_EQ(instr->GetType(), DataType::Type::kInt32);
2567 DCHECK_EQ(DataType::Kind(instr->InputAt(0)->GetType()), type);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002569 bool is_long = type == DataType::Type::kInt64;
Anton Kirilov5ec62182016-10-13 20:16:02 +01002570 LocationSummary* locations = instr->GetLocations();
2571 Location in = locations->InAt(0);
2572 vixl32::Register src_0 = is_long ? LowRegisterFrom(in) : RegisterFrom(in);
2573 vixl32::Register src_1 = is_long ? HighRegisterFrom(in) : src_0;
2574 vixl32::SRegister tmp_s = LowSRegisterFrom(locations->GetTemp(0));
2575 vixl32::DRegister tmp_d = DRegisterFrom(locations->GetTemp(0));
2576 vixl32::Register out_r = OutputRegister(instr);
2577
2578 // Move data from core register(s) to temp D-reg for bit count calculation, then move back.
2579 // According to Cortex A57 and A72 optimization guides, compared to transferring to full D-reg,
2580 // transferring data from core reg to upper or lower half of vfp D-reg requires extra latency,
2581 // That's why for integer bit count, we use 'vmov d0, r0, r0' instead of 'vmov d0[0], r0'.
2582 __ Vmov(tmp_d, src_1, src_0); // Temp DReg |--src_1|--src_0|
2583 __ Vcnt(Untyped8, tmp_d, tmp_d); // Temp DReg |c|c|c|c|c|c|c|c|
2584 __ Vpaddl(U8, tmp_d, tmp_d); // Temp DReg |--c|--c|--c|--c|
2585 __ Vpaddl(U16, tmp_d, tmp_d); // Temp DReg |------c|------c|
2586 if (is_long) {
2587 __ Vpaddl(U32, tmp_d, tmp_d); // Temp DReg |--------------c|
2588 }
2589 __ Vmov(out_r, tmp_s);
2590}
2591
2592void IntrinsicLocationsBuilderARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002593 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002594 invoke->GetLocations()->AddTemp(Location::RequiresFpuRegister());
2595}
2596
2597void IntrinsicCodeGeneratorARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598 GenBitCount(invoke, DataType::Type::kInt32, GetAssembler());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002599}
2600
2601void IntrinsicLocationsBuilderARMVIXL::VisitLongBitCount(HInvoke* invoke) {
2602 VisitIntegerBitCount(invoke);
2603}
2604
2605void IntrinsicCodeGeneratorARMVIXL::VisitLongBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002606 GenBitCount(invoke, DataType::Type::kInt64, GetAssembler());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002607}
2608
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002609static void GenHighestOneBit(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002610 DataType::Type type,
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002611 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 DCHECK(DataType::IsIntOrLongType(type));
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002613
2614 ArmVIXLAssembler* assembler = codegen->GetAssembler();
2615 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2616 const vixl32::Register temp = temps.Acquire();
2617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002618 if (type == DataType::Type::kInt64) {
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002619 LocationSummary* locations = invoke->GetLocations();
2620 Location in = locations->InAt(0);
2621 Location out = locations->Out();
2622
2623 vixl32::Register in_reg_lo = LowRegisterFrom(in);
2624 vixl32::Register in_reg_hi = HighRegisterFrom(in);
2625 vixl32::Register out_reg_lo = LowRegisterFrom(out);
2626 vixl32::Register out_reg_hi = HighRegisterFrom(out);
2627
2628 __ Mov(temp, 0x80000000); // Modified immediate.
2629 __ Clz(out_reg_lo, in_reg_lo);
2630 __ Clz(out_reg_hi, in_reg_hi);
2631 __ Lsr(out_reg_lo, temp, out_reg_lo);
2632 __ Lsrs(out_reg_hi, temp, out_reg_hi);
2633
2634 // Discard result for lowest 32 bits if highest 32 bits are not zero.
2635 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2636 // we check that the output is in a low register, so that a 16-bit MOV
2637 // encoding can be used. If output is in a high register, then we generate
2638 // 4 more bytes of code to avoid a branch.
2639 Operand mov_src(0);
2640 if (!out_reg_lo.IsLow()) {
2641 __ Mov(LeaveFlags, temp, 0);
2642 mov_src = Operand(temp);
2643 }
2644 ExactAssemblyScope it_scope(codegen->GetVIXLAssembler(),
2645 2 * vixl32::k16BitT32InstructionSizeInBytes,
2646 CodeBufferCheckScope::kExactSize);
2647 __ it(ne);
2648 __ mov(ne, out_reg_lo, mov_src);
2649 } else {
2650 vixl32::Register out = OutputRegister(invoke);
2651 vixl32::Register in = InputRegisterAt(invoke, 0);
2652
2653 __ Mov(temp, 0x80000000); // Modified immediate.
2654 __ Clz(out, in);
2655 __ Lsr(out, temp, out);
2656 }
2657}
2658
2659void IntrinsicLocationsBuilderARMVIXL::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002660 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002661}
2662
2663void IntrinsicCodeGeneratorARMVIXL::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002664 GenHighestOneBit(invoke, DataType::Type::kInt32, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002665}
2666
2667void IntrinsicLocationsBuilderARMVIXL::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002668 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002669}
2670
2671void IntrinsicCodeGeneratorARMVIXL::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 GenHighestOneBit(invoke, DataType::Type::kInt64, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002673}
2674
2675static void GenLowestOneBit(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 DataType::Type type,
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002677 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002678 DCHECK(DataType::IsIntOrLongType(type));
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002679
2680 ArmVIXLAssembler* assembler = codegen->GetAssembler();
2681 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2682 const vixl32::Register temp = temps.Acquire();
2683
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002684 if (type == DataType::Type::kInt64) {
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002685 LocationSummary* locations = invoke->GetLocations();
2686 Location in = locations->InAt(0);
2687 Location out = locations->Out();
2688
2689 vixl32::Register in_reg_lo = LowRegisterFrom(in);
2690 vixl32::Register in_reg_hi = HighRegisterFrom(in);
2691 vixl32::Register out_reg_lo = LowRegisterFrom(out);
2692 vixl32::Register out_reg_hi = HighRegisterFrom(out);
2693
2694 __ Rsb(out_reg_hi, in_reg_hi, 0);
2695 __ Rsb(out_reg_lo, in_reg_lo, 0);
2696 __ And(out_reg_hi, out_reg_hi, in_reg_hi);
2697 // The result of this operation is 0 iff in_reg_lo is 0
2698 __ Ands(out_reg_lo, out_reg_lo, in_reg_lo);
2699
2700 // Discard result for highest 32 bits if lowest 32 bits are not zero.
2701 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2702 // we check that the output is in a low register, so that a 16-bit MOV
2703 // encoding can be used. If output is in a high register, then we generate
2704 // 4 more bytes of code to avoid a branch.
2705 Operand mov_src(0);
2706 if (!out_reg_lo.IsLow()) {
2707 __ Mov(LeaveFlags, temp, 0);
2708 mov_src = Operand(temp);
2709 }
2710 ExactAssemblyScope it_scope(codegen->GetVIXLAssembler(),
2711 2 * vixl32::k16BitT32InstructionSizeInBytes,
2712 CodeBufferCheckScope::kExactSize);
2713 __ it(ne);
2714 __ mov(ne, out_reg_hi, mov_src);
2715 } else {
2716 vixl32::Register out = OutputRegister(invoke);
2717 vixl32::Register in = InputRegisterAt(invoke, 0);
2718
2719 __ Rsb(temp, in, 0);
2720 __ And(out, temp, in);
2721 }
2722}
2723
2724void IntrinsicLocationsBuilderARMVIXL::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002725 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002726}
2727
2728void IntrinsicCodeGeneratorARMVIXL::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002729 GenLowestOneBit(invoke, DataType::Type::kInt32, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002730}
2731
2732void IntrinsicLocationsBuilderARMVIXL::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002733 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002734}
2735
2736void IntrinsicCodeGeneratorARMVIXL::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002737 GenLowestOneBit(invoke, DataType::Type::kInt64, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002738}
2739
Anton Kirilov5ec62182016-10-13 20:16:02 +01002740void IntrinsicLocationsBuilderARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002741 LocationSummary* locations =
2742 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002743 locations->SetInAt(0, Location::RequiresRegister());
2744 locations->SetInAt(1, Location::RequiresRegister());
2745 locations->SetInAt(2, Location::RequiresRegister());
2746 locations->SetInAt(3, Location::RequiresRegister());
2747 locations->SetInAt(4, Location::RequiresRegister());
2748
2749 // Temporary registers to store lengths of strings and for calculations.
2750 locations->AddTemp(Location::RequiresRegister());
2751 locations->AddTemp(Location::RequiresRegister());
2752 locations->AddTemp(Location::RequiresRegister());
2753}
2754
2755void IntrinsicCodeGeneratorARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2756 ArmVIXLAssembler* assembler = GetAssembler();
2757 LocationSummary* locations = invoke->GetLocations();
2758
2759 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002761 DCHECK_EQ(char_size, 2u);
2762
2763 // Location of data in char array buffer.
2764 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2765
2766 // Location of char array data in string.
2767 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2768
2769 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2770 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2771 vixl32::Register srcObj = InputRegisterAt(invoke, 0);
2772 vixl32::Register srcBegin = InputRegisterAt(invoke, 1);
2773 vixl32::Register srcEnd = InputRegisterAt(invoke, 2);
2774 vixl32::Register dstObj = InputRegisterAt(invoke, 3);
2775 vixl32::Register dstBegin = InputRegisterAt(invoke, 4);
2776
2777 vixl32::Register num_chr = RegisterFrom(locations->GetTemp(0));
2778 vixl32::Register src_ptr = RegisterFrom(locations->GetTemp(1));
2779 vixl32::Register dst_ptr = RegisterFrom(locations->GetTemp(2));
2780
2781 vixl32::Label done, compressed_string_loop;
Anton Kirilov6f644202017-02-27 18:29:45 +00002782 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002783 // dst to be copied.
2784 __ Add(dst_ptr, dstObj, data_offset);
2785 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, vixl32::LSL, 1));
2786
2787 __ Subs(num_chr, srcEnd, srcBegin);
2788 // Early out for valid zero-length retrievals.
Anton Kirilov6f644202017-02-27 18:29:45 +00002789 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002790
2791 // src range to copy.
2792 __ Add(src_ptr, srcObj, value_offset);
2793
2794 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2795 vixl32::Register temp;
2796 vixl32::Label compressed_string_preloop;
2797 if (mirror::kUseStringCompression) {
2798 // Location of count in string.
2799 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2800 temp = temps.Acquire();
2801 // String's length.
2802 __ Ldr(temp, MemOperand(srcObj, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002803 __ Tst(temp, 1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002804 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002805 __ B(eq, &compressed_string_preloop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002806 }
2807 __ Add(src_ptr, src_ptr, Operand(srcBegin, vixl32::LSL, 1));
2808
2809 // Do the copy.
2810 vixl32::Label loop, remainder;
2811
2812 temp = temps.Acquire();
2813 // Save repairing the value of num_chr on the < 4 character path.
2814 __ Subs(temp, num_chr, 4);
Artem Serov517d9f62016-12-12 15:51:15 +00002815 __ B(lt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002816
2817 // Keep the result of the earlier subs, we are going to fetch at least 4 characters.
2818 __ Mov(num_chr, temp);
2819
2820 // Main loop used for longer fetches loads and stores 4x16-bit characters at a time.
2821 // (LDRD/STRD fault on unaligned addresses and it's not worth inlining extra code
2822 // to rectify these everywhere this intrinsic applies.)
2823 __ Bind(&loop);
2824 __ Ldr(temp, MemOperand(src_ptr, char_size * 2));
2825 __ Subs(num_chr, num_chr, 4);
2826 __ Str(temp, MemOperand(dst_ptr, char_size * 2));
2827 __ Ldr(temp, MemOperand(src_ptr, char_size * 4, PostIndex));
2828 __ Str(temp, MemOperand(dst_ptr, char_size * 4, PostIndex));
2829 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002830 __ B(ge, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002831
2832 __ Adds(num_chr, num_chr, 4);
Anton Kirilov6f644202017-02-27 18:29:45 +00002833 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002834
2835 // Main loop for < 4 character case and remainder handling. Loads and stores one
2836 // 16-bit Java character at a time.
2837 __ Bind(&remainder);
2838 temp = temps.Acquire();
2839 __ Ldrh(temp, MemOperand(src_ptr, char_size, PostIndex));
2840 __ Subs(num_chr, num_chr, 1);
2841 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2842 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002843 __ B(gt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002844
2845 if (mirror::kUseStringCompression) {
Anton Kirilov6f644202017-02-27 18:29:45 +00002846 __ B(final_label);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002849 DCHECK_EQ(c_char_size, 1u);
2850 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2851 __ Bind(&compressed_string_preloop);
2852 __ Add(src_ptr, src_ptr, srcBegin);
2853 __ Bind(&compressed_string_loop);
2854 temp = temps.Acquire();
2855 __ Ldrb(temp, MemOperand(src_ptr, c_char_size, PostIndex));
2856 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2857 temps.Release(temp);
2858 __ Subs(num_chr, num_chr, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00002859 __ B(gt, &compressed_string_loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002860 }
2861
Anton Kirilov6f644202017-02-27 18:29:45 +00002862 if (done.IsReferenced()) {
2863 __ Bind(&done);
2864 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002865}
2866
2867void IntrinsicLocationsBuilderARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002868 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002869}
2870
2871void IntrinsicCodeGeneratorARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
2872 ArmVIXLAssembler* const assembler = GetAssembler();
2873 const vixl32::Register out = OutputRegister(invoke);
2874 // Shifting left by 1 bit makes the value encodable as an immediate operand;
2875 // we don't care about the sign bit anyway.
2876 constexpr uint32_t infinity = kPositiveInfinityFloat << 1U;
2877
2878 __ Vmov(out, InputSRegisterAt(invoke, 0));
2879 // We don't care about the sign bit, so shift left.
2880 __ Lsl(out, out, 1);
2881 __ Eor(out, out, infinity);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002882 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002883}
2884
2885void IntrinsicLocationsBuilderARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002886 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002887}
2888
2889void IntrinsicCodeGeneratorARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
2890 ArmVIXLAssembler* const assembler = GetAssembler();
2891 const vixl32::Register out = OutputRegister(invoke);
2892 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2893 const vixl32::Register temp = temps.Acquire();
2894 // The highest 32 bits of double precision positive infinity separated into
2895 // two constants encodable as immediate operands.
2896 constexpr uint32_t infinity_high = 0x7f000000U;
2897 constexpr uint32_t infinity_high2 = 0x00f00000U;
2898
2899 static_assert((infinity_high | infinity_high2) ==
2900 static_cast<uint32_t>(kPositiveInfinityDouble >> 32U),
2901 "The constants do not add up to the high 32 bits of double "
2902 "precision positive infinity.");
2903 __ Vmov(temp, out, InputDRegisterAt(invoke, 0));
2904 __ Eor(out, out, infinity_high);
2905 __ Eor(out, out, infinity_high2);
2906 // We don't care about the sign bit, so shift left.
2907 __ Orr(out, temp, Operand(out, vixl32::LSL, 1));
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002908 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002909}
2910
Artem Serov9aee2d42017-01-06 15:58:31 +00002911void IntrinsicLocationsBuilderARMVIXL::VisitMathCeil(HInvoke* invoke) {
2912 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002913 CreateFPToFPLocations(allocator_, invoke);
Artem Serov9aee2d42017-01-06 15:58:31 +00002914 }
2915}
2916
2917void IntrinsicCodeGeneratorARMVIXL::VisitMathCeil(HInvoke* invoke) {
2918 ArmVIXLAssembler* assembler = GetAssembler();
2919 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
2920 __ Vrintp(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
2921}
2922
2923void IntrinsicLocationsBuilderARMVIXL::VisitMathFloor(HInvoke* invoke) {
2924 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002925 CreateFPToFPLocations(allocator_, invoke);
Artem Serov9aee2d42017-01-06 15:58:31 +00002926 }
2927}
2928
2929void IntrinsicCodeGeneratorARMVIXL::VisitMathFloor(HInvoke* invoke) {
2930 ArmVIXLAssembler* assembler = GetAssembler();
2931 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
2932 __ Vrintm(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
2933}
2934
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002935void IntrinsicLocationsBuilderARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
2936 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2937 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2938 invoke,
2939 codegen_,
2940 LocationFrom(r0),
2941 LocationFrom(calling_convention.GetRegisterAt(0)));
2942}
2943
2944void IntrinsicCodeGeneratorARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
2945 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2946 LocationSummary* locations = invoke->GetLocations();
2947 ArmVIXLAssembler* const assembler = GetAssembler();
2948
2949 vixl32::Register out = RegisterFrom(locations->Out());
2950 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2951 vixl32::Register temp = temps.Acquire();
2952 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2953 vixl32::Register argument = calling_convention.GetRegisterAt(0);
2954 if (invoke->InputAt(0)->IsConstant()) {
2955 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2956 if (value >= info.low && value <= info.high) {
2957 // Just embed the j.l.Integer in the code.
2958 ScopedObjectAccess soa(Thread::Current());
2959 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2960 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2961 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
2962 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
2963 } else {
2964 // Allocate and initialize a new j.l.Integer.
2965 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2966 // JIT object table.
2967 uint32_t address =
2968 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2969 __ Ldr(argument, codegen_->DeduplicateBootImageAddressLiteral(address));
2970 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2971 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2972 __ Mov(temp, value);
2973 assembler->StoreToOffset(kStoreWord, temp, out, info.value_offset);
2974 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2975 // one.
2976 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2977 }
2978 } else {
2979 vixl32::Register in = RegisterFrom(locations->InAt(0));
2980 // Check bounds of our cache.
2981 __ Add(out, in, -info.low);
2982 __ Cmp(out, info.high - info.low + 1);
2983 vixl32::Label allocate, done;
Anton Kirilovfd522532017-05-10 12:46:57 +01002984 __ B(hs, &allocate, /* is_far_target */ false);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002985 // If the value is within the bounds, load the j.l.Integer directly from the array.
2986 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2987 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
2988 __ Ldr(temp, codegen_->DeduplicateBootImageAddressLiteral(data_offset + address));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 codegen_->LoadFromShiftedRegOffset(DataType::Type::kReference, locations->Out(), temp, out);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002990 assembler->MaybeUnpoisonHeapReference(out);
2991 __ B(&done);
2992 __ Bind(&allocate);
2993 // Otherwise allocate and initialize a new j.l.Integer.
2994 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2995 __ Ldr(argument, codegen_->DeduplicateBootImageAddressLiteral(address));
2996 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2997 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2998 assembler->StoreToOffset(kStoreWord, in, out, info.value_offset);
2999 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3000 // one.
3001 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3002 __ Bind(&done);
3003 }
3004}
3005
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003006void IntrinsicLocationsBuilderARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003007 LocationSummary* locations =
3008 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003009 locations->SetOut(Location::RequiresRegister());
3010}
3011
3012void IntrinsicCodeGeneratorARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
3013 ArmVIXLAssembler* assembler = GetAssembler();
3014 vixl32::Register out = RegisterFrom(invoke->GetLocations()->Out());
3015 int32_t offset = Thread::InterruptedOffset<kArmPointerSize>().Int32Value();
3016 __ Ldr(out, MemOperand(tr, offset));
3017 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3018 vixl32::Register temp = temps.Acquire();
3019 vixl32::Label done;
Anton Kirilovfd522532017-05-10 12:46:57 +01003020 vixl32::Label* const final_label = codegen_->GetFinalLabel(invoke, &done);
3021 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003022 __ Dmb(vixl32::ISH);
3023 __ Mov(temp, 0);
3024 assembler->StoreToOffset(kStoreWord, temp, tr, offset);
3025 __ Dmb(vixl32::ISH);
Anton Kirilovfd522532017-05-10 12:46:57 +01003026 if (done.IsReferenced()) {
3027 __ Bind(&done);
3028 }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003029}
3030
Hans Boehmc7b28de2018-03-09 17:05:28 -08003031void IntrinsicLocationsBuilderARMVIXL::VisitReachabilityFence(HInvoke* invoke) {
3032 LocationSummary* locations =
3033 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
3034 locations->SetInAt(0, Location::Any());
3035}
3036
3037void IntrinsicCodeGeneratorARMVIXL::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
3038
Anton Kirilov5ec62182016-10-13 20:16:02 +01003039UNIMPLEMENTED_INTRINSIC(ARMVIXL, MathRoundDouble) // Could be done by changing rounding mode, maybe?
Anton Kirilov5ec62182016-10-13 20:16:02 +01003040UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeCASLong) // High register pressure.
3041UNIMPLEMENTED_INTRINSIC(ARMVIXL, SystemArrayCopyChar)
Vladimir Markod254f5c2017-06-02 15:18:36 +00003042UNIMPLEMENTED_INTRINSIC(ARMVIXL, ReferenceGetReferent)
Anton Kirilov5ec62182016-10-13 20:16:02 +01003043
Aart Bikff7d89c2016-11-07 08:49:28 -08003044UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOf);
3045UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003046UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferAppend);
3047UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferLength);
3048UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferToString);
3049UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderAppend);
3050UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderLength);
3051UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003052
Anton Kirilov5ec62182016-10-13 20:16:02 +01003053// 1.8.
3054UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddInt)
3055UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddLong)
3056UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetInt)
3057UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetLong)
3058UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetObject)
3059
3060UNREACHABLE_INTRINSICS(ARMVIXL)
3061
3062#undef __
3063
3064} // namespace arm
3065} // namespace art