blob: 74a779d9e28a3603e7e7e6b185be1236cbd2e86f [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"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010028#include "mirror/string-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#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;
Anton Kirilov5ec62182016-10-13 20:16:02 +010044using helpers::Int32ConstantFrom;
45using helpers::LocationFrom;
46using helpers::LowRegisterFrom;
47using helpers::LowSRegisterFrom;
xueliang.zhong53463ba2017-02-16 15:18:03 +000048using helpers::HighSRegisterFrom;
Anton Kirilov5ec62182016-10-13 20:16:02 +010049using helpers::OutputDRegister;
50using helpers::OutputRegister;
Anton Kirilov5ec62182016-10-13 20:16:02 +010051using helpers::RegisterFrom;
52using helpers::SRegisterFrom;
53
54using namespace vixl::aarch32; // NOLINT(build/namespaces)
55
Artem Serov0fb37192016-12-06 18:13:40 +000056using vixl::ExactAssemblyScope;
57using vixl::CodeBufferCheckScope;
58
Anton Kirilov5ec62182016-10-13 20:16:02 +010059ArmVIXLAssembler* IntrinsicCodeGeneratorARMVIXL::GetAssembler() {
60 return codegen_->GetAssembler();
61}
62
63ArenaAllocator* IntrinsicCodeGeneratorARMVIXL::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010064 return codegen_->GetGraph()->GetAllocator();
Anton Kirilov5ec62182016-10-13 20:16:02 +010065}
66
67// Default slow-path for fallback (calling the managed code to handle the intrinsic) in an
68// intrinsified call. This will copy the arguments into the positions for a regular call.
69//
70// Note: The actual parameters are required to be in the locations given by the invoke's location
71// summary. If an intrinsic modifies those locations before a slowpath call, they must be
72// restored!
73//
74// Note: If an invoke wasn't sharpened, we will put down an invoke-virtual here. That's potentially
75// sub-optimal (compared to a direct pointer call), but this is a slow-path.
76
77class IntrinsicSlowPathARMVIXL : public SlowPathCodeARMVIXL {
78 public:
79 explicit IntrinsicSlowPathARMVIXL(HInvoke* invoke)
80 : SlowPathCodeARMVIXL(invoke), invoke_(invoke) {}
81
82 Location MoveArguments(CodeGenerator* codegen) {
Artem Serovd4cc5b22016-11-04 11:19:09 +000083 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Anton Kirilov5ec62182016-10-13 20:16:02 +010084 IntrinsicVisitor::MoveArguments(invoke_, codegen, &calling_convention_visitor);
85 return calling_convention_visitor.GetMethodLocation();
86 }
87
88 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
89 ArmVIXLAssembler* assembler = down_cast<ArmVIXLAssembler*>(codegen->GetAssembler());
90 __ Bind(GetEntryLabel());
91
92 SaveLiveRegisters(codegen, invoke_->GetLocations());
93
94 Location method_loc = MoveArguments(codegen);
95
96 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +010097 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(), method_loc, this);
Anton Kirilov5ec62182016-10-13 20:16:02 +010098 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +010099 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), method_loc, this);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100100 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100101
102 // Copy the result back to the expected output.
103 Location out = invoke_->GetLocations()->Out();
104 if (out.IsValid()) {
105 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
106 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
107 codegen->MoveFromReturnRegister(out, invoke_->GetType());
108 }
109
110 RestoreLiveRegisters(codegen, invoke_->GetLocations());
111 __ B(GetExitLabel());
112 }
113
114 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPath"; }
115
116 private:
117 // The instruction where this slow path is happening.
118 HInvoke* const invoke_;
119
120 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARMVIXL);
121};
122
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000123// Compute base address for the System.arraycopy intrinsic in `base`.
124static void GenSystemArrayCopyBaseAddress(ArmVIXLAssembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100125 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000126 const vixl32::Register& array,
127 const Location& pos,
128 const vixl32::Register& base) {
129 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100130 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000131 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100132 DCHECK_EQ(type, DataType::Type::kReference);
133 const int32_t element_size = DataType::Size(type);
134 const uint32_t element_size_shift = DataType::SizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000135 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
136
137 if (pos.IsConstant()) {
138 int32_t constant = Int32ConstantFrom(pos);
139 __ Add(base, array, element_size * constant + data_offset);
140 } else {
141 __ Add(base, array, Operand(RegisterFrom(pos), vixl32::LSL, element_size_shift));
142 __ Add(base, base, data_offset);
143 }
144}
145
146// Compute end address for the System.arraycopy intrinsic in `end`.
147static void GenSystemArrayCopyEndAddress(ArmVIXLAssembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000149 const Location& copy_length,
150 const vixl32::Register& base,
151 const vixl32::Register& end) {
152 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000154 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100155 DCHECK_EQ(type, DataType::Type::kReference);
156 const int32_t element_size = DataType::Size(type);
157 const uint32_t element_size_shift = DataType::SizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000158
159 if (copy_length.IsConstant()) {
160 int32_t constant = Int32ConstantFrom(copy_length);
161 __ Add(end, base, element_size * constant);
162 } else {
163 __ Add(end, base, Operand(RegisterFrom(copy_length), vixl32::LSL, element_size_shift));
164 }
165}
166
Anton Kirilov5ec62182016-10-13 20:16:02 +0100167// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
168class ReadBarrierSystemArrayCopySlowPathARMVIXL : public SlowPathCodeARMVIXL {
169 public:
170 explicit ReadBarrierSystemArrayCopySlowPathARMVIXL(HInstruction* instruction)
171 : SlowPathCodeARMVIXL(instruction) {
172 DCHECK(kEmitCompilerReadBarrier);
173 DCHECK(kUseBakerReadBarrier);
174 }
175
176 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
177 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
178 ArmVIXLAssembler* assembler = arm_codegen->GetAssembler();
179 LocationSummary* locations = instruction_->GetLocations();
180 DCHECK(locations->CanCall());
181 DCHECK(instruction_->IsInvokeStaticOrDirect())
182 << "Unexpected instruction in read barrier arraycopy slow path: "
183 << instruction_->DebugName();
184 DCHECK(instruction_->GetLocations()->Intrinsified());
185 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100187 DataType::Type type = DataType::Type::kReference;
188 const int32_t element_size = DataType::Size(type);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100189
190 vixl32::Register dest = InputRegisterAt(instruction_, 2);
191 Location dest_pos = locations->InAt(3);
192 vixl32::Register src_curr_addr = RegisterFrom(locations->GetTemp(0));
193 vixl32::Register dst_curr_addr = RegisterFrom(locations->GetTemp(1));
194 vixl32::Register src_stop_addr = RegisterFrom(locations->GetTemp(2));
195 vixl32::Register tmp = RegisterFrom(locations->GetTemp(3));
196
197 __ Bind(GetEntryLabel());
198 // Compute the base destination address in `dst_curr_addr`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000199 GenSystemArrayCopyBaseAddress(assembler, type, dest, dest_pos, dst_curr_addr);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100200
201 vixl32::Label loop;
202 __ Bind(&loop);
203 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
204 assembler->MaybeUnpoisonHeapReference(tmp);
205 // TODO: Inline the mark bit check before calling the runtime?
206 // tmp = ReadBarrier::Mark(tmp);
207 // No need to save live registers; it's taken care of by the
208 // entrypoint. Also, there is no need to update the stack mask,
209 // as this runtime call will not trigger a garbage collection.
210 // (See ReadBarrierMarkSlowPathARM::EmitNativeCode for more
211 // explanations.)
212 DCHECK(!tmp.IsSP());
213 DCHECK(!tmp.IsLR());
214 DCHECK(!tmp.IsPC());
215 // IP is used internally by the ReadBarrierMarkRegX entry point
216 // as a temporary (and not preserved). It thus cannot be used by
217 // any live register in this slow path.
218 DCHECK(!src_curr_addr.Is(ip));
219 DCHECK(!dst_curr_addr.Is(ip));
220 DCHECK(!src_stop_addr.Is(ip));
221 DCHECK(!tmp.Is(ip));
222 DCHECK(tmp.IsRegister()) << tmp;
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000223 // TODO: Load the entrypoint once before the loop, instead of
224 // loading it at every iteration.
Anton Kirilov5ec62182016-10-13 20:16:02 +0100225 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100226 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(tmp.GetCode());
Anton Kirilov5ec62182016-10-13 20:16:02 +0100227 // This runtime call does not require a stack map.
228 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
229 assembler->MaybePoisonHeapReference(tmp);
230 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
231 __ Cmp(src_curr_addr, src_stop_addr);
Artem Serov517d9f62016-12-12 15:51:15 +0000232 __ B(ne, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100233 __ B(GetExitLabel());
234 }
235
236 const char* GetDescription() const OVERRIDE {
237 return "ReadBarrierSystemArrayCopySlowPathARMVIXL";
238 }
239
240 private:
241 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARMVIXL);
242};
243
244IntrinsicLocationsBuilderARMVIXL::IntrinsicLocationsBuilderARMVIXL(CodeGeneratorARMVIXL* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100245 : allocator_(codegen->GetGraph()->GetAllocator()),
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000246 codegen_(codegen),
Anton Kirilov5ec62182016-10-13 20:16:02 +0100247 assembler_(codegen->GetAssembler()),
248 features_(codegen->GetInstructionSetFeatures()) {}
249
250bool IntrinsicLocationsBuilderARMVIXL::TryDispatch(HInvoke* invoke) {
251 Dispatch(invoke);
252 LocationSummary* res = invoke->GetLocations();
253 if (res == nullptr) {
254 return false;
255 }
256 return res->Intrinsified();
257}
258
Vladimir Markoca6fff82017-10-03 14:49:14 +0100259static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
260 LocationSummary* locations =
261 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100262 locations->SetInAt(0, Location::RequiresFpuRegister());
263 locations->SetOut(Location::RequiresRegister());
264}
265
Vladimir Markoca6fff82017-10-03 14:49:14 +0100266static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
267 LocationSummary* locations =
268 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100269 locations->SetInAt(0, Location::RequiresRegister());
270 locations->SetOut(Location::RequiresFpuRegister());
271}
272
273static void MoveFPToInt(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
274 Location input = locations->InAt(0);
275 Location output = locations->Out();
276 if (is64bit) {
277 __ Vmov(LowRegisterFrom(output), HighRegisterFrom(output), DRegisterFrom(input));
278 } else {
279 __ Vmov(RegisterFrom(output), SRegisterFrom(input));
280 }
281}
282
283static void MoveIntToFP(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
284 Location input = locations->InAt(0);
285 Location output = locations->Out();
286 if (is64bit) {
287 __ Vmov(DRegisterFrom(output), LowRegisterFrom(input), HighRegisterFrom(input));
288 } else {
289 __ Vmov(SRegisterFrom(output), RegisterFrom(input));
290 }
291}
292
293void IntrinsicLocationsBuilderARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100294 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100295}
296void IntrinsicLocationsBuilderARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100297 CreateIntToFPLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100298}
299
300void IntrinsicCodeGeneratorARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
301 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
302}
303void IntrinsicCodeGeneratorARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
304 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
305}
306
307void IntrinsicLocationsBuilderARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100308 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100309}
310void IntrinsicLocationsBuilderARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100311 CreateIntToFPLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100312}
313
314void IntrinsicCodeGeneratorARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
315 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
316}
317void IntrinsicCodeGeneratorARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
318 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
319}
320
Vladimir Markoca6fff82017-10-03 14:49:14 +0100321static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
322 LocationSummary* locations =
323 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100324 locations->SetInAt(0, Location::RequiresRegister());
325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
326}
327
Vladimir Markoca6fff82017-10-03 14:49:14 +0100328static void CreateLongToLongLocationsWithOverlap(ArenaAllocator* allocator, HInvoke* invoke) {
329 LocationSummary* locations =
330 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +0100331 locations->SetInAt(0, Location::RequiresRegister());
332 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
333}
334
Vladimir Markoca6fff82017-10-03 14:49:14 +0100335static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
336 LocationSummary* locations =
337 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100338 locations->SetInAt(0, Location::RequiresFpuRegister());
339 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
340}
341
Anton Kirilov6f644202017-02-27 18:29:45 +0000342static void GenNumberOfLeadingZeros(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100343 DataType::Type type,
Anton Kirilov6f644202017-02-27 18:29:45 +0000344 CodeGeneratorARMVIXL* codegen) {
345 ArmVIXLAssembler* assembler = codegen->GetAssembler();
346 LocationSummary* locations = invoke->GetLocations();
Anton Kirilov5ec62182016-10-13 20:16:02 +0100347 Location in = locations->InAt(0);
348 vixl32::Register out = RegisterFrom(locations->Out());
349
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100350 DCHECK((type == DataType::Type::kInt32) || (type == DataType::Type::kInt64));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100353 vixl32::Register in_reg_lo = LowRegisterFrom(in);
354 vixl32::Register in_reg_hi = HighRegisterFrom(in);
355 vixl32::Label end;
Anton Kirilov6f644202017-02-27 18:29:45 +0000356 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100357 __ Clz(out, in_reg_hi);
Anton Kirilov6f644202017-02-27 18:29:45 +0000358 __ CompareAndBranchIfNonZero(in_reg_hi, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100359 __ Clz(out, in_reg_lo);
360 __ Add(out, out, 32);
Anton Kirilov6f644202017-02-27 18:29:45 +0000361 if (end.IsReferenced()) {
362 __ Bind(&end);
363 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100364 } else {
365 __ Clz(out, RegisterFrom(in));
366 }
367}
368
369void IntrinsicLocationsBuilderARMVIXL::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100370 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100371}
372
373void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100374 GenNumberOfLeadingZeros(invoke, DataType::Type::kInt32, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100375}
376
377void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100378 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100379}
380
381void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100382 GenNumberOfLeadingZeros(invoke, DataType::Type::kInt64, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100383}
384
Anton Kirilov6f644202017-02-27 18:29:45 +0000385static void GenNumberOfTrailingZeros(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100386 DataType::Type type,
Anton Kirilov6f644202017-02-27 18:29:45 +0000387 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100388 DCHECK((type == DataType::Type::kInt32) || (type == DataType::Type::kInt64));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100389
Anton Kirilov6f644202017-02-27 18:29:45 +0000390 ArmVIXLAssembler* assembler = codegen->GetAssembler();
391 LocationSummary* locations = invoke->GetLocations();
Anton Kirilov5ec62182016-10-13 20:16:02 +0100392 vixl32::Register out = RegisterFrom(locations->Out());
393
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100394 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100395 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
396 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
397 vixl32::Label end;
Anton Kirilov6f644202017-02-27 18:29:45 +0000398 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100399 __ Rbit(out, in_reg_lo);
400 __ Clz(out, out);
Anton Kirilov6f644202017-02-27 18:29:45 +0000401 __ CompareAndBranchIfNonZero(in_reg_lo, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100402 __ Rbit(out, in_reg_hi);
403 __ Clz(out, out);
404 __ Add(out, out, 32);
Anton Kirilov6f644202017-02-27 18:29:45 +0000405 if (end.IsReferenced()) {
406 __ Bind(&end);
407 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100408 } else {
409 vixl32::Register in = RegisterFrom(locations->InAt(0));
410 __ Rbit(out, in);
411 __ Clz(out, out);
412 }
413}
414
415void IntrinsicLocationsBuilderARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100416 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100417}
418
419void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100420 GenNumberOfTrailingZeros(invoke, DataType::Type::kInt32, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100421}
422
423void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100425}
426
427void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100428 GenNumberOfTrailingZeros(invoke, DataType::Type::kInt64, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100429}
430
Anton Kirilov5ec62182016-10-13 20:16:02 +0100431void IntrinsicLocationsBuilderARMVIXL::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100432 CreateFPToFPLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100433}
434
435void IntrinsicCodeGeneratorARMVIXL::VisitMathSqrt(HInvoke* invoke) {
436 ArmVIXLAssembler* assembler = GetAssembler();
437 __ Vsqrt(OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
438}
439
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100440void IntrinsicLocationsBuilderARMVIXL::VisitMathRint(HInvoke* invoke) {
441 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100442 CreateFPToFPLocations(allocator_, invoke);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100443 }
444}
445
446void IntrinsicCodeGeneratorARMVIXL::VisitMathRint(HInvoke* invoke) {
447 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
448 ArmVIXLAssembler* assembler = GetAssembler();
449 __ Vrintn(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
450}
451
xueliang.zhong53463ba2017-02-16 15:18:03 +0000452void IntrinsicLocationsBuilderARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
453 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100454 LocationSummary* locations =
455 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
xueliang.zhong53463ba2017-02-16 15:18:03 +0000456 locations->SetInAt(0, Location::RequiresFpuRegister());
457 locations->SetOut(Location::RequiresRegister());
458 locations->AddTemp(Location::RequiresFpuRegister());
459 }
460}
461
462void IntrinsicCodeGeneratorARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
463 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
464
465 ArmVIXLAssembler* assembler = GetAssembler();
466 vixl32::SRegister in_reg = InputSRegisterAt(invoke, 0);
467 vixl32::Register out_reg = OutputRegister(invoke);
468 vixl32::SRegister temp1 = LowSRegisterFrom(invoke->GetLocations()->GetTemp(0));
469 vixl32::SRegister temp2 = HighSRegisterFrom(invoke->GetLocations()->GetTemp(0));
470 vixl32::Label done;
471 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
472
473 // Round to nearest integer, ties away from zero.
474 __ Vcvta(S32, F32, temp1, in_reg);
475 __ Vmov(out_reg, temp1);
476
477 // For positive, zero or NaN inputs, rounding is done.
478 __ Cmp(out_reg, 0);
479 __ B(ge, final_label, /* far_target */ false);
480
481 // Handle input < 0 cases.
482 // If input is negative but not a tie, previous result (round to nearest) is valid.
483 // If input is a negative tie, change rounding direction to positive infinity, out_reg += 1.
484 __ Vrinta(F32, F32, temp1, in_reg);
485 __ Vmov(temp2, 0.5);
486 __ Vsub(F32, temp1, in_reg, temp1);
487 __ Vcmp(F32, temp1, temp2);
488 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
489 {
490 // Use ExactAsemblyScope here because we are using IT.
491 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
492 2 * kMaxInstructionSizeInBytes,
493 CodeBufferCheckScope::kMaximumSize);
494 __ it(eq);
495 __ add(eq, out_reg, out_reg, 1);
496 }
497
498 if (done.IsReferenced()) {
499 __ Bind(&done);
500 }
501}
502
Anton Kirilov5ec62182016-10-13 20:16:02 +0100503void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100504 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100505}
506
507void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
508 ArmVIXLAssembler* assembler = GetAssembler();
509 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000510 __ Ldrsb(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100511}
512
513void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100514 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100515}
516
517void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
518 ArmVIXLAssembler* assembler = GetAssembler();
519 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000520 __ Ldr(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100521}
522
523void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100524 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100525}
526
527void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
528 ArmVIXLAssembler* assembler = GetAssembler();
529 // Ignore upper 4B of long address.
530 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
531 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
532 // exception. So we can't use ldrd as addr may be unaligned.
533 vixl32::Register lo = LowRegisterFrom(invoke->GetLocations()->Out());
534 vixl32::Register hi = HighRegisterFrom(invoke->GetLocations()->Out());
535 if (addr.Is(lo)) {
536 __ Ldr(hi, MemOperand(addr, 4));
Scott Wakelingb77051e2016-11-21 19:46:00 +0000537 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100538 } else {
Scott Wakelingb77051e2016-11-21 19:46:00 +0000539 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100540 __ Ldr(hi, MemOperand(addr, 4));
541 }
542}
543
544void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100545 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100546}
547
548void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
549 ArmVIXLAssembler* assembler = GetAssembler();
550 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000551 __ Ldrsh(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100552}
553
Vladimir Markoca6fff82017-10-03 14:49:14 +0100554static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
555 LocationSummary* locations =
556 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100557 locations->SetInAt(0, Location::RequiresRegister());
558 locations->SetInAt(1, Location::RequiresRegister());
559}
560
561void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100562 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100563}
564
565void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
566 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000567 __ Strb(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100568}
569
570void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100571 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100572}
573
574void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
575 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000576 __ Str(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100577}
578
579void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100580 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100581}
582
583void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
584 ArmVIXLAssembler* assembler = GetAssembler();
585 // Ignore upper 4B of long address.
586 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
587 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
588 // exception. So we can't use ldrd as addr may be unaligned.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000589 __ Str(LowRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100590 __ Str(HighRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr, 4));
591}
592
593void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100594 CreateIntIntToVoidLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100595}
596
597void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
598 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000599 __ Strh(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100600}
601
602void IntrinsicLocationsBuilderARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100603 LocationSummary* locations =
604 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100605 locations->SetOut(Location::RequiresRegister());
606}
607
608void IntrinsicCodeGeneratorARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
609 ArmVIXLAssembler* assembler = GetAssembler();
610 __ Ldr(OutputRegister(invoke),
611 MemOperand(tr, Thread::PeerOffset<kArmPointerSize>().Int32Value()));
612}
613
614static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100615 DataType::Type type,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100616 bool is_volatile,
617 CodeGeneratorARMVIXL* codegen) {
618 LocationSummary* locations = invoke->GetLocations();
619 ArmVIXLAssembler* assembler = codegen->GetAssembler();
620 Location base_loc = locations->InAt(1);
621 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
622 Location offset_loc = locations->InAt(2);
623 vixl32::Register offset = LowRegisterFrom(offset_loc); // Long offset, lo part only.
624 Location trg_loc = locations->Out();
625
626 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100627 case DataType::Type::kInt32: {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100628 vixl32::Register trg = RegisterFrom(trg_loc);
629 __ Ldr(trg, MemOperand(base, offset));
630 if (is_volatile) {
631 __ Dmb(vixl32::ISH);
632 }
633 break;
634 }
635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100636 case DataType::Type::kReference: {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100637 vixl32::Register trg = RegisterFrom(trg_loc);
638 if (kEmitCompilerReadBarrier) {
639 if (kUseBakerReadBarrier) {
640 Location temp = locations->GetTemp(0);
Vladimir Marko248141f2018-08-10 10:40:07 +0100641 // Piggy-back on the field load path using introspection for the Baker read barrier.
642 __ Add(RegisterFrom(temp), base, Operand(offset));
643 MemOperand src(RegisterFrom(temp), 0);
644 codegen->GenerateFieldLoadWithBakerReadBarrier(
645 invoke, trg_loc, base, src, /* needs_null_check */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100646 if (is_volatile) {
647 __ Dmb(vixl32::ISH);
648 }
649 } else {
650 __ Ldr(trg, MemOperand(base, offset));
651 if (is_volatile) {
652 __ Dmb(vixl32::ISH);
653 }
654 codegen->GenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
655 }
656 } else {
657 __ Ldr(trg, MemOperand(base, offset));
658 if (is_volatile) {
659 __ Dmb(vixl32::ISH);
660 }
661 assembler->MaybeUnpoisonHeapReference(trg);
662 }
663 break;
664 }
665
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100666 case DataType::Type::kInt64: {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100667 vixl32::Register trg_lo = LowRegisterFrom(trg_loc);
668 vixl32::Register trg_hi = HighRegisterFrom(trg_loc);
669 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
Artem Serov657022c2016-11-23 14:19:38 +0000670 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
671 const vixl32::Register temp_reg = temps.Acquire();
672 __ Add(temp_reg, base, offset);
673 __ Ldrexd(trg_lo, trg_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100674 } else {
675 __ Ldrd(trg_lo, trg_hi, MemOperand(base, offset));
676 }
677 if (is_volatile) {
678 __ Dmb(vixl32::ISH);
679 }
680 break;
681 }
682
683 default:
684 LOG(FATAL) << "Unexpected type " << type;
685 UNREACHABLE();
686 }
687}
688
Vladimir Markoca6fff82017-10-03 14:49:14 +0100689static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100690 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100691 DataType::Type type) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100692 bool can_call = kEmitCompilerReadBarrier &&
693 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
694 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 LocationSummary* locations =
696 new (allocator) LocationSummary(invoke,
697 can_call
698 ? LocationSummary::kCallOnSlowPath
699 : LocationSummary::kNoCall,
700 kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100701 if (can_call && kUseBakerReadBarrier) {
702 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
703 }
704 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
705 locations->SetInAt(1, Location::RequiresRegister());
706 locations->SetInAt(2, Location::RequiresRegister());
707 locations->SetOut(Location::RequiresRegister(),
708 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100709 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100710 // We need a temporary register for the read barrier marking slow
Roland Levillain9983e302017-07-14 14:34:22 +0100711 // path in CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier.
Anton Kirilov5ec62182016-10-13 20:16:02 +0100712 locations->AddTemp(Location::RequiresRegister());
713 }
714}
715
716void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100717 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100718}
719void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100720 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100721}
722void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100723 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100724}
725void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100726 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100727}
728void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100729 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kReference);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100730}
731void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100732 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kReference);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100733}
734
735void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100736 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100737}
738void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100740}
741void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100742 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100743}
744void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100745 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100746}
747void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100748 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100749}
750void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100751 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100752}
753
Vladimir Markoca6fff82017-10-03 14:49:14 +0100754static void CreateIntIntIntIntToVoid(ArenaAllocator* allocator,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100755 const ArmInstructionSetFeatures& features,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100756 DataType::Type type,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100757 bool is_volatile,
758 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100759 LocationSummary* locations =
760 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100761 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
762 locations->SetInAt(1, Location::RequiresRegister());
763 locations->SetInAt(2, Location::RequiresRegister());
764 locations->SetInAt(3, Location::RequiresRegister());
765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100766 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100767 // Potentially need temps for ldrexd-strexd loop.
768 if (is_volatile && !features.HasAtomicLdrdAndStrd()) {
769 locations->AddTemp(Location::RequiresRegister()); // Temp_lo.
770 locations->AddTemp(Location::RequiresRegister()); // Temp_hi.
771 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100772 } else if (type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100773 // Temps for card-marking.
774 locations->AddTemp(Location::RequiresRegister()); // Temp.
775 locations->AddTemp(Location::RequiresRegister()); // Card.
776 }
777}
778
779void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100780 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100781 allocator_, features_, DataType::Type::kInt32, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100782}
783void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100784 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100785 allocator_, features_, DataType::Type::kInt32, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100786}
787void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100788 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100789 allocator_, features_, DataType::Type::kInt32, /* is_volatile */ true, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100790}
791void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100792 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100793 allocator_, features_, DataType::Type::kReference, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100794}
795void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100796 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100797 allocator_, features_, DataType::Type::kReference, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100798}
799void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100800 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100801 allocator_, features_, DataType::Type::kReference, /* is_volatile */ true, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100802}
803void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
804 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100805 allocator_, features_, DataType::Type::kInt64, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100806}
807void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
808 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100809 allocator_, features_, DataType::Type::kInt64, /* is_volatile */ false, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100810}
811void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
812 CreateIntIntIntIntToVoid(
Vladimir Markoca6fff82017-10-03 14:49:14 +0100813 allocator_, features_, DataType::Type::kInt64, /* is_volatile */ true, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100814}
815
816static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100817 DataType::Type type,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100818 bool is_volatile,
819 bool is_ordered,
820 CodeGeneratorARMVIXL* codegen) {
821 ArmVIXLAssembler* assembler = codegen->GetAssembler();
822
823 vixl32::Register base = RegisterFrom(locations->InAt(1)); // Object pointer.
824 vixl32::Register offset = LowRegisterFrom(locations->InAt(2)); // Long offset, lo part only.
825 vixl32::Register value;
826
827 if (is_volatile || is_ordered) {
828 __ Dmb(vixl32::ISH);
829 }
830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100831 if (type == DataType::Type::kInt64) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100832 vixl32::Register value_lo = LowRegisterFrom(locations->InAt(3));
833 vixl32::Register value_hi = HighRegisterFrom(locations->InAt(3));
834 value = value_lo;
835 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
836 vixl32::Register temp_lo = RegisterFrom(locations->GetTemp(0));
837 vixl32::Register temp_hi = RegisterFrom(locations->GetTemp(1));
838 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
839 const vixl32::Register temp_reg = temps.Acquire();
840
841 __ Add(temp_reg, base, offset);
842 vixl32::Label loop_head;
843 __ Bind(&loop_head);
Scott Wakelingb77051e2016-11-21 19:46:00 +0000844 __ Ldrexd(temp_lo, temp_hi, MemOperand(temp_reg));
845 __ Strexd(temp_lo, value_lo, value_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100846 __ Cmp(temp_lo, 0);
Artem Serov517d9f62016-12-12 15:51:15 +0000847 __ B(ne, &loop_head, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100848 } else {
849 __ Strd(value_lo, value_hi, MemOperand(base, offset));
850 }
851 } else {
852 value = RegisterFrom(locations->InAt(3));
853 vixl32::Register source = value;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100855 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
856 __ Mov(temp, value);
857 assembler->PoisonHeapReference(temp);
858 source = temp;
859 }
860 __ Str(source, MemOperand(base, offset));
861 }
862
863 if (is_volatile) {
864 __ Dmb(vixl32::ISH);
865 }
866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100867 if (type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100868 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
869 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
870 bool value_can_be_null = true; // TODO: Worth finding out this information?
871 codegen->MarkGCCard(temp, card, base, value, value_can_be_null);
872 }
873}
874
875void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePut(HInvoke* invoke) {
876 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100877 DataType::Type::kInt32,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100878 /* is_volatile */ false,
879 /* is_ordered */ false,
880 codegen_);
881}
882void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
883 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100884 DataType::Type::kInt32,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100885 /* is_volatile */ false,
886 /* is_ordered */ true,
887 codegen_);
888}
889void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
890 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100891 DataType::Type::kInt32,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100892 /* is_volatile */ true,
893 /* is_ordered */ false,
894 codegen_);
895}
896void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
897 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100898 DataType::Type::kReference,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100899 /* is_volatile */ false,
900 /* is_ordered */ false,
901 codegen_);
902}
903void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
904 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905 DataType::Type::kReference,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100906 /* is_volatile */ false,
907 /* is_ordered */ true,
908 codegen_);
909}
910void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
911 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100912 DataType::Type::kReference,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100913 /* is_volatile */ true,
914 /* is_ordered */ false,
915 codegen_);
916}
917void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
918 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100919 DataType::Type::kInt64,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100920 /* is_volatile */ false,
921 /* is_ordered */ false,
922 codegen_);
923}
924void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
925 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100926 DataType::Type::kInt64,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100927 /* is_volatile */ false,
928 /* is_ordered */ true,
929 codegen_);
930}
931void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
932 GenUnsafePut(invoke->GetLocations(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100933 DataType::Type::kInt64,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100934 /* is_volatile */ true,
935 /* is_ordered */ false,
936 codegen_);
937}
938
Vladimir Markod887ed82018-08-14 13:52:12 +0000939static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100940 bool can_call = kEmitCompilerReadBarrier &&
941 kUseBakerReadBarrier &&
942 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100943 LocationSummary* locations =
944 new (allocator) LocationSummary(invoke,
945 can_call
946 ? LocationSummary::kCallOnSlowPath
947 : LocationSummary::kNoCall,
948 kIntrinsified);
Vladimir Markod887ed82018-08-14 13:52:12 +0000949 if (can_call) {
950 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
951 }
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
Vladimir Markod887ed82018-08-14 13:52:12 +0000958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100959
960 // Temporary registers used in CAS. In the object case
961 // (UnsafeCASObject intrinsic), these are also used for
962 // card-marking, and possibly for (Baker) read barrier.
963 locations->AddTemp(Location::RequiresRegister()); // Pointer.
964 locations->AddTemp(Location::RequiresRegister()); // Temp 1.
965}
966
Vladimir Markod887ed82018-08-14 13:52:12 +0000967class BakerReadBarrierCasSlowPathARMVIXL : public SlowPathCodeARMVIXL {
968 public:
969 explicit BakerReadBarrierCasSlowPathARMVIXL(HInvoke* invoke)
970 : SlowPathCodeARMVIXL(invoke) {}
971
972 const char* GetDescription() const OVERRIDE { return "BakerReadBarrierCasSlowPathARMVIXL"; }
973
974 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
975 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
976 ArmVIXLAssembler* assembler = arm_codegen->GetAssembler();
977 __ Bind(GetEntryLabel());
978
979 LocationSummary* locations = instruction_->GetLocations();
980 vixl32::Register base = InputRegisterAt(instruction_, 1); // Object pointer.
981 vixl32::Register offset = LowRegisterFrom(locations->InAt(2)); // Offset (discard high 4B).
982 vixl32::Register expected = InputRegisterAt(instruction_, 3); // Expected.
983 vixl32::Register value = InputRegisterAt(instruction_, 4); // Value.
984
985 vixl32::Register tmp_ptr = RegisterFrom(locations->GetTemp(0)); // Pointer to actual memory.
986 vixl32::Register tmp = RegisterFrom(locations->GetTemp(1)); // Temporary.
987
988 // The `tmp` is initialized to `[tmp_ptr] - expected` in the main path. Reconstruct
989 // and mark the old value and compare with `expected`. We clobber `tmp_ptr` in the
990 // process due to lack of other temps suitable for the read barrier.
991 arm_codegen->GenerateUnsafeCasOldValueAddWithBakerReadBarrier(tmp_ptr, tmp, expected);
992 __ Cmp(tmp_ptr, expected);
993 __ B(ne, GetExitLabel());
994
995 // The old value we have read did not match `expected` (which is always a to-space reference)
996 // but after the read barrier in GenerateUnsafeCasOldValueAddWithBakerReadBarrier() the marked
997 // to-space value matched, so the old value must be a from-space reference to the same object.
998 // Do the same CAS loop as the main path but check for both `expected` and the unmarked
999 // old value representing the to-space and from-space references for the same object.
1000
1001 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1002 vixl32::Register adjusted_old_value = temps.Acquire(); // For saved `tmp` from main path.
1003
1004 // Recalculate the `tmp_ptr` clobbered above and store the `adjusted_old_value`, i.e. IP.
1005 __ Add(tmp_ptr, base, offset);
1006 __ Mov(adjusted_old_value, tmp);
1007
1008 // do {
1009 // tmp = [r_ptr] - expected;
1010 // } while ((tmp == 0 || tmp == adjusted_old_value) && failure([r_ptr] <- r_new_value));
1011 // result = (tmp == 0 || tmp == adjusted_old_value);
1012
1013 vixl32::Label loop_head;
1014 __ Bind(&loop_head);
1015 __ Ldrex(tmp, MemOperand(tmp_ptr)); // This can now load null stored by another thread.
1016 assembler->MaybeUnpoisonHeapReference(tmp);
1017 __ Subs(tmp, tmp, expected); // Use SUBS to get non-zero value if both compares fail.
1018 {
1019 // If the newly loaded value did not match `expected`, compare with `adjusted_old_value`.
1020 ExactAssemblyScope aas(assembler->GetVIXLAssembler(), 2 * k16BitT32InstructionSizeInBytes);
1021 __ it(ne);
1022 __ cmp(ne, tmp, adjusted_old_value);
1023 }
1024 __ B(ne, GetExitLabel());
1025 assembler->MaybePoisonHeapReference(value);
1026 __ Strex(tmp, value, MemOperand(tmp_ptr));
1027 assembler->MaybeUnpoisonHeapReference(value);
1028 __ Cmp(tmp, 0);
1029 __ B(ne, &loop_head, /* far_target */ false);
1030 __ B(GetExitLabel());
1031 }
1032};
1033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001034static void GenCas(HInvoke* invoke, DataType::Type type, CodeGeneratorARMVIXL* codegen) {
1035 DCHECK_NE(type, DataType::Type::kInt64);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001036
1037 ArmVIXLAssembler* assembler = codegen->GetAssembler();
1038 LocationSummary* locations = invoke->GetLocations();
1039
Anton Kirilov5ec62182016-10-13 20:16:02 +01001040 vixl32::Register out = OutputRegister(invoke); // Boolean result.
1041
1042 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
Vladimir Markod887ed82018-08-14 13:52:12 +00001043 vixl32::Register offset = LowRegisterFrom(locations->InAt(2)); // Offset (discard high 4B).
Anton Kirilov5ec62182016-10-13 20:16:02 +01001044 vixl32::Register expected = InputRegisterAt(invoke, 3); // Expected.
1045 vixl32::Register value = InputRegisterAt(invoke, 4); // Value.
1046
Vladimir Markod887ed82018-08-14 13:52:12 +00001047 vixl32::Register tmp_ptr = RegisterFrom(locations->GetTemp(0)); // Pointer to actual memory.
1048 vixl32::Register tmp = RegisterFrom(locations->GetTemp(1)); // Temporary.
1049
1050 vixl32::Label loop_exit_label;
1051 vixl32::Label* loop_exit = &loop_exit_label;
1052 vixl32::Label* failure = &loop_exit_label;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001054 if (type == DataType::Type::kReference) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001055 // The only read barrier implementation supporting the
1056 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1057 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1058
1059 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
1060 // object and scan the receiver at the next GC for nothing.
1061 bool value_can_be_null = true; // TODO: Worth finding out this information?
1062 codegen->MarkGCCard(tmp_ptr, tmp, base, value, value_can_be_null);
1063
1064 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markod887ed82018-08-14 13:52:12 +00001065 // If marking, check if the stored reference is a from-space reference to the same
1066 // object as the to-space reference `expected`. If so, perform a custom CAS loop.
1067 BakerReadBarrierCasSlowPathARMVIXL* slow_path =
1068 new (codegen->GetScopedAllocator()) BakerReadBarrierCasSlowPathARMVIXL(invoke);
1069 codegen->AddSlowPath(slow_path);
1070 failure = slow_path->GetEntryLabel();
1071 loop_exit = slow_path->GetExitLabel();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001072 }
1073 }
1074
1075 // Prevent reordering with prior memory operations.
1076 // Emit a DMB ISH instruction instead of an DMB ISHST one, as the
Vladimir Markod887ed82018-08-14 13:52:12 +00001077 // latter allows a preceding load to be delayed past the STREX
Anton Kirilov5ec62182016-10-13 20:16:02 +01001078 // instruction below.
1079 __ Dmb(vixl32::ISH);
1080
1081 __ Add(tmp_ptr, base, offset);
1082
Anton Kirilov5ec62182016-10-13 20:16:02 +01001083 // do {
1084 // tmp = [r_ptr] - expected;
1085 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
Vladimir Markod887ed82018-08-14 13:52:12 +00001086 // result = tmp == 0;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001087
1088 vixl32::Label loop_head;
1089 __ Bind(&loop_head);
Vladimir Markof28be432018-08-14 12:20:51 +00001090 __ Ldrex(tmp, MemOperand(tmp_ptr));
Vladimir Markod887ed82018-08-14 13:52:12 +00001091 if (type == DataType::Type::kReference) {
1092 assembler->MaybeUnpoisonHeapReference(tmp);
Vladimir Markof28be432018-08-14 12:20:51 +00001093 }
Vladimir Markod887ed82018-08-14 13:52:12 +00001094 __ Subs(tmp, tmp, expected);
1095 __ B(ne, failure, (failure == loop_exit) ? kNear : kBranchWithoutHint);
1096 if (type == DataType::Type::kReference) {
1097 assembler->MaybePoisonHeapReference(value);
1098 }
1099 __ Strex(tmp, value, MemOperand(tmp_ptr));
1100 if (type == DataType::Type::kReference) {
1101 assembler->MaybeUnpoisonHeapReference(value);
1102 }
1103 __ Cmp(tmp, 0);
1104 __ B(ne, &loop_head, /* far_target */ false);
Vladimir Markof28be432018-08-14 12:20:51 +00001105
Vladimir Markod887ed82018-08-14 13:52:12 +00001106 __ Bind(loop_exit);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001107
1108 __ Dmb(vixl32::ISH);
1109
Vladimir Markod887ed82018-08-14 13:52:12 +00001110 // out = tmp == 0.
1111 __ Clz(out, tmp);
1112 __ Lsr(out, out, WhichPowerOf2(out.GetSizeInBits()));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001113
Vladimir Markod887ed82018-08-14 13:52:12 +00001114 if (type == DataType::Type::kReference) {
1115 codegen->MaybeGenerateMarkingRegisterCheck(/* code */ 128);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001116 }
1117}
1118
1119void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markod887ed82018-08-14 13:52:12 +00001120 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001121}
1122void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1123 // The only read barrier implementation supporting the
1124 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1125 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1126 return;
1127 }
1128
Vladimir Markod887ed82018-08-14 13:52:12 +00001129 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001130}
1131void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001132 GenCas(invoke, DataType::Type::kInt32, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001133}
1134void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1135 // The only read barrier implementation supporting the
1136 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1137 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001139 GenCas(invoke, DataType::Type::kReference, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001140}
1141
1142void IntrinsicLocationsBuilderARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1143 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001144 LocationSummary* locations =
1145 new (allocator_) LocationSummary(invoke,
1146 invoke->InputAt(1)->CanBeNull()
1147 ? LocationSummary::kCallOnSlowPath
1148 : LocationSummary::kNoCall,
1149 kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001150 locations->SetInAt(0, Location::RequiresRegister());
1151 locations->SetInAt(1, Location::RequiresRegister());
1152 locations->AddTemp(Location::RequiresRegister());
1153 locations->AddTemp(Location::RequiresRegister());
1154 locations->AddTemp(Location::RequiresRegister());
1155 // Need temporary registers for String compression's feature.
1156 if (mirror::kUseStringCompression) {
1157 locations->AddTemp(Location::RequiresRegister());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001158 }
1159 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1160}
1161
Artem Serov672b9c12017-12-05 18:04:07 +00001162// Forward declaration.
1163//
1164// ART build system imposes a size limit (deviceFrameSizeLimit) on the stack frames generated
1165// by the compiler for every C++ function, and if this function gets inlined in
1166// IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo, the limit will be exceeded, resulting in a
1167// build failure. That is the reason why NO_INLINE attribute is used.
1168static void NO_INLINE GenerateStringCompareToLoop(ArmVIXLAssembler* assembler,
1169 HInvoke* invoke,
1170 vixl32::Label* end,
1171 vixl32::Label* different_compression);
1172
Anton Kirilov5ec62182016-10-13 20:16:02 +01001173void IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1174 ArmVIXLAssembler* assembler = GetAssembler();
1175 LocationSummary* locations = invoke->GetLocations();
1176
Artem Serov672b9c12017-12-05 18:04:07 +00001177 const vixl32::Register str = InputRegisterAt(invoke, 0);
1178 const vixl32::Register arg = InputRegisterAt(invoke, 1);
1179 const vixl32::Register out = OutputRegister(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001180
Artem Serov672b9c12017-12-05 18:04:07 +00001181 const vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1182 const vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1183 const vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001184 vixl32::Register temp3;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001185 if (mirror::kUseStringCompression) {
1186 temp3 = RegisterFrom(locations->GetTemp(3));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001187 }
1188
Anton Kirilov5ec62182016-10-13 20:16:02 +01001189 vixl32::Label end;
1190 vixl32::Label different_compression;
1191
1192 // Get offsets of count and value fields within a string object.
1193 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001194
1195 // Note that the null check must have been done earlier.
1196 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1197
1198 // Take slow path and throw if input can be and is null.
1199 SlowPathCodeARMVIXL* slow_path = nullptr;
1200 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1201 if (can_slow_path) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001202 slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001203 codegen_->AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001204 __ CompareAndBranchIfZero(arg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001205 }
1206
1207 // Reference equality check, return 0 if same reference.
1208 __ Subs(out, str, arg);
1209 __ B(eq, &end);
1210
Anton Kirilov5ec62182016-10-13 20:16:02 +01001211 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001212 // Load `count` fields of this and argument strings.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001213 __ Ldr(temp3, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001214 __ Ldr(temp2, MemOperand(arg, count_offset));
1215 // Extract lengths from the `count` fields.
1216 __ Lsr(temp0, temp3, 1u);
1217 __ Lsr(temp1, temp2, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001218 } else {
1219 // Load lengths of this and argument strings.
1220 __ Ldr(temp0, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001221 __ Ldr(temp1, MemOperand(arg, count_offset));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001222 }
1223 // out = length diff.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001224 __ Subs(out, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001225 // temp0 = min(len(str), len(arg)).
1226
1227 {
Artem Serov0fb37192016-12-06 18:13:40 +00001228 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1229 2 * kMaxInstructionSizeInBytes,
1230 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001231
1232 __ it(gt);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001233 __ mov(gt, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001234 }
1235
Anton Kirilov5ec62182016-10-13 20:16:02 +01001236 // Shorter string is empty?
xueliang.zhongf51bc622016-11-04 09:23:32 +00001237 // Note that mirror::kUseStringCompression==true introduces lots of instructions,
1238 // which makes &end label far away from this branch and makes it not 'CBZ-encodable'.
1239 __ CompareAndBranchIfZero(temp0, &end, mirror::kUseStringCompression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001240
1241 if (mirror::kUseStringCompression) {
1242 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001243 __ Eors(temp2, temp2, temp3);
1244 __ Lsrs(temp2, temp2, 1u);
1245 __ B(cs, &different_compression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001246 // For string compression, calculate the number of bytes to compare (not chars).
1247 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001248 __ Lsls(temp3, temp3, 31u); // Extract purely the compression flag.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001249
Artem Serov0fb37192016-12-06 18:13:40 +00001250 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1251 2 * kMaxInstructionSizeInBytes,
1252 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001253
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001254 __ it(ne);
1255 __ add(ne, temp0, temp0, temp0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001256 }
1257
Artem Serov672b9c12017-12-05 18:04:07 +00001258
1259 GenerateStringCompareToLoop(assembler, invoke, &end, &different_compression);
1260
1261 __ Bind(&end);
1262
1263 if (can_slow_path) {
1264 __ Bind(slow_path->GetExitLabel());
1265 }
1266}
1267
1268static void GenerateStringCompareToLoop(ArmVIXLAssembler* assembler,
1269 HInvoke* invoke,
1270 vixl32::Label* end,
1271 vixl32::Label* different_compression) {
1272 LocationSummary* locations = invoke->GetLocations();
1273
1274 const vixl32::Register str = InputRegisterAt(invoke, 0);
1275 const vixl32::Register arg = InputRegisterAt(invoke, 1);
1276 const vixl32::Register out = OutputRegister(invoke);
1277
1278 const vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1279 const vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1280 const vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
1281 vixl32::Register temp3;
1282 if (mirror::kUseStringCompression) {
1283 temp3 = RegisterFrom(locations->GetTemp(3));
1284 }
1285
1286 vixl32::Label loop;
1287 vixl32::Label find_char_diff;
1288
1289 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001290 // Store offset of string value in preparation for comparison loop.
1291 __ Mov(temp1, value_offset);
1292
Anton Kirilov5ec62182016-10-13 20:16:02 +01001293 // Assertions that must hold in order to compare multiple characters at a time.
1294 CHECK_ALIGNED(value_offset, 8);
1295 static_assert(IsAligned<8>(kObjectAlignment),
1296 "String data must be 8-byte aligned for unrolled CompareTo loop.");
1297
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001298 const unsigned char_size = DataType::Size(DataType::Type::kUint16);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001299 DCHECK_EQ(char_size, 2u);
1300
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001301 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1302
Anton Kirilov5ec62182016-10-13 20:16:02 +01001303 vixl32::Label find_char_diff_2nd_cmp;
1304 // Unrolled loop comparing 4x16-bit chars per iteration (ok because of string data alignment).
1305 __ Bind(&loop);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001306 vixl32::Register temp_reg = temps.Acquire();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001307 __ Ldr(temp_reg, MemOperand(str, temp1));
1308 __ Ldr(temp2, MemOperand(arg, temp1));
1309 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001310 __ B(ne, &find_char_diff, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001311 __ Add(temp1, temp1, char_size * 2);
1312
1313 __ Ldr(temp_reg, MemOperand(str, temp1));
1314 __ Ldr(temp2, MemOperand(arg, temp1));
1315 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001316 __ B(ne, &find_char_diff_2nd_cmp, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001317 __ Add(temp1, temp1, char_size * 2);
1318 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1319 __ Subs(temp0, temp0, (mirror::kUseStringCompression ? 8 : 4));
Artem Serov517d9f62016-12-12 15:51:15 +00001320 __ B(hi, &loop, /* far_target */ false);
Artem Serov672b9c12017-12-05 18:04:07 +00001321 __ B(end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001322
1323 __ Bind(&find_char_diff_2nd_cmp);
1324 if (mirror::kUseStringCompression) {
1325 __ Subs(temp0, temp0, 4); // 4 bytes previously compared.
Artem Serov672b9c12017-12-05 18:04:07 +00001326 __ B(ls, end, /* far_target */ false); // Was the second comparison fully beyond the end?
Anton Kirilov5ec62182016-10-13 20:16:02 +01001327 } else {
1328 // Without string compression, we can start treating temp0 as signed
1329 // and rely on the signed comparison below.
1330 __ Sub(temp0, temp0, 2);
1331 }
1332
1333 // Find the single character difference.
1334 __ Bind(&find_char_diff);
1335 // Get the bit position of the first character that differs.
1336 __ Eor(temp1, temp2, temp_reg);
1337 __ Rbit(temp1, temp1);
1338 __ Clz(temp1, temp1);
1339
1340 // temp0 = number of characters remaining to compare.
1341 // (Without string compression, it could be < 1 if a difference is found by the second CMP
1342 // in the comparison loop, and after the end of the shorter string data).
1343
1344 // Without string compression (temp1 >> 4) = character where difference occurs between the last
1345 // two words compared, in the interval [0,1].
1346 // (0 for low half-word different, 1 for high half-word different).
1347 // With string compression, (temp1 << 3) = byte where the difference occurs,
1348 // in the interval [0,3].
1349
1350 // If temp0 <= (temp1 >> (kUseStringCompression ? 3 : 4)), the difference occurs outside
1351 // the remaining string data, so just return length diff (out).
1352 // The comparison is unsigned for string compression, otherwise signed.
1353 __ Cmp(temp0, Operand(temp1, vixl32::LSR, (mirror::kUseStringCompression ? 3 : 4)));
Artem Serov672b9c12017-12-05 18:04:07 +00001354 __ B((mirror::kUseStringCompression ? ls : le), end, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001355
Anton Kirilov5ec62182016-10-13 20:16:02 +01001356 // Extract the characters and calculate the difference.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001357 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001358 // For compressed strings we need to clear 0x7 from temp1, for uncompressed we need to clear
1359 // 0xf. We also need to prepare the character extraction mask `uncompressed ? 0xffffu : 0xffu`.
1360 // The compression flag is now in the highest bit of temp3, so let's play some tricks.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001361 __ Orr(temp3, temp3, 0xffu << 23); // uncompressed ? 0xff800000u : 0x7ff80000u
1362 __ Bic(temp1, temp1, Operand(temp3, vixl32::LSR, 31 - 3)); // &= ~(uncompressed ? 0xfu : 0x7u)
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001363 __ Asr(temp3, temp3, 7u); // uncompressed ? 0xffff0000u : 0xff0000u.
1364 __ Lsr(temp2, temp2, temp1); // Extract second character.
1365 __ Lsr(temp3, temp3, 16u); // uncompressed ? 0xffffu : 0xffu
1366 __ Lsr(out, temp_reg, temp1); // Extract first character.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001367 __ And(temp2, temp2, temp3);
1368 __ And(out, out, temp3);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001369 } else {
Anton Kirilovb88c4842016-11-14 14:37:00 +00001370 __ Bic(temp1, temp1, 0xf);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001371 __ Lsr(temp2, temp2, temp1);
1372 __ Lsr(out, temp_reg, temp1);
Anton Kirilovb88c4842016-11-14 14:37:00 +00001373 __ Movt(temp2, 0);
1374 __ Movt(out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001375 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001376
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001377 __ Sub(out, out, temp2);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001378 temps.Release(temp_reg);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001379
1380 if (mirror::kUseStringCompression) {
Artem Serov672b9c12017-12-05 18:04:07 +00001381 __ B(end);
1382 __ Bind(different_compression);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001383
1384 // Comparison for different compression style.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001385 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001386 DCHECK_EQ(c_char_size, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001387
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001388 // We want to free up the temp3, currently holding `str.count`, for comparison.
1389 // So, we move it to the bottom bit of the iteration count `temp0` which we tnen
1390 // need to treat as unsigned. Start by freeing the bit with an ADD and continue
1391 // further down by a LSRS+SBC which will flip the meaning of the flag but allow
1392 // `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001393 __ Add(temp0, temp0, temp0); // Unlike LSL, this ADD is always 16-bit.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001394 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001395 __ Mov(temp1, str);
1396 __ Mov(temp2, arg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001397 __ Lsrs(temp3, temp3, 1u); // Continue the move of the compression flag.
1398 {
Artem Serov0fb37192016-12-06 18:13:40 +00001399 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1400 3 * kMaxInstructionSizeInBytes,
1401 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001402 __ itt(cs); // Interleave with selection of temp1 and temp2.
1403 __ mov(cs, temp1, arg); // Preserves flags.
1404 __ mov(cs, temp2, str); // Preserves flags.
1405 }
Anton Kirilovb88c4842016-11-14 14:37:00 +00001406 __ Sbc(temp0, temp0, 0); // Complete the move of the compression flag.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001407
1408 // Adjust temp1 and temp2 from string pointers to data pointers.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001409 __ Add(temp1, temp1, value_offset);
1410 __ Add(temp2, temp2, value_offset);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001411
1412 vixl32::Label different_compression_loop;
1413 vixl32::Label different_compression_diff;
1414
1415 // Main loop for different compression.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001416 temp_reg = temps.Acquire();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001417 __ Bind(&different_compression_loop);
1418 __ Ldrb(temp_reg, MemOperand(temp1, c_char_size, PostIndex));
1419 __ Ldrh(temp3, MemOperand(temp2, char_size, PostIndex));
Anton Kirilovb88c4842016-11-14 14:37:00 +00001420 __ Cmp(temp_reg, temp3);
Artem Serov517d9f62016-12-12 15:51:15 +00001421 __ B(ne, &different_compression_diff, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001422 __ Subs(temp0, temp0, 2);
Artem Serov517d9f62016-12-12 15:51:15 +00001423 __ B(hi, &different_compression_loop, /* far_target */ false);
Artem Serov672b9c12017-12-05 18:04:07 +00001424 __ B(end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001425
1426 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001427 __ Bind(&different_compression_diff);
1428 __ Sub(out, temp_reg, temp3);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001429 temps.Release(temp_reg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001430 // Flip the difference if the `arg` is compressed.
1431 // `temp0` contains inverted `str` compression flag, i.e the same as `arg` compression flag.
1432 __ Lsrs(temp0, temp0, 1u);
1433 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1434 "Expecting 0=compressed, 1=uncompressed");
1435
Artem Serov0fb37192016-12-06 18:13:40 +00001436 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1437 2 * kMaxInstructionSizeInBytes,
1438 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001439 __ it(cc);
1440 __ rsb(cc, out, out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001441 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001442}
1443
Vladimir Marko984519c2017-08-23 10:45:29 +01001444// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1445// The normal loop plus the pre-header is 9 instructions (18-26 bytes) without string compression
1446// and 12 instructions (24-32 bytes) with string compression. We can compare up to 4 bytes in 4
1447// instructions (LDR+LDR+CMP+BNE) and up to 8 bytes in 6 instructions (LDRD+LDRD+CMP+BNE+CMP+BNE).
1448// Allow up to 12 instructions (32 bytes) for the unrolled loop.
1449constexpr size_t kShortConstStringEqualsCutoffInBytes = 16;
1450
1451static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1452 if (candidate->IsLoadString()) {
1453 HLoadString* load_string = candidate->AsLoadString();
1454 const DexFile& dex_file = load_string->GetDexFile();
1455 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1456 }
1457 return nullptr;
1458}
1459
Anton Kirilov5ec62182016-10-13 20:16:02 +01001460void IntrinsicLocationsBuilderARMVIXL::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001461 if (kEmitCompilerReadBarrier &&
1462 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1463 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1464 // No support for this odd case (String class is moveable, not in the boot image).
1465 return;
1466 }
1467
Vladimir Markoca6fff82017-10-03 14:49:14 +01001468 LocationSummary* locations =
1469 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001470 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1471 locations->SetInAt(0, Location::RequiresRegister());
1472 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko984519c2017-08-23 10:45:29 +01001473
Anton Kirilov5ec62182016-10-13 20:16:02 +01001474 // Temporary registers to store lengths of strings and for calculations.
1475 // Using instruction cbz requires a low register, so explicitly set a temp to be R0.
1476 locations->AddTemp(LocationFrom(r0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001477
Vladimir Marko984519c2017-08-23 10:45:29 +01001478 // For the generic implementation and for long const strings we need an extra temporary.
1479 // We do not need it for short const strings, up to 4 bytes, see code generation below.
1480 uint32_t const_string_length = 0u;
1481 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1482 if (const_string == nullptr) {
1483 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1484 }
1485 bool is_compressed =
1486 mirror::kUseStringCompression &&
1487 const_string != nullptr &&
1488 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1489 if (const_string == nullptr || const_string_length > (is_compressed ? 4u : 2u)) {
1490 locations->AddTemp(Location::RequiresRegister());
1491 }
1492
1493 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1494 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1495 // Then we shall need an extra temporary register instead of the output register.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001496 locations->SetOut(Location::RequiresRegister());
1497}
1498
1499void IntrinsicCodeGeneratorARMVIXL::VisitStringEquals(HInvoke* invoke) {
1500 ArmVIXLAssembler* assembler = GetAssembler();
1501 LocationSummary* locations = invoke->GetLocations();
1502
1503 vixl32::Register str = InputRegisterAt(invoke, 0);
1504 vixl32::Register arg = InputRegisterAt(invoke, 1);
1505 vixl32::Register out = OutputRegister(invoke);
1506
1507 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001508
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001509 vixl32::Label loop;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001510 vixl32::Label end;
1511 vixl32::Label return_true;
1512 vixl32::Label return_false;
Anton Kirilov6f644202017-02-27 18:29:45 +00001513 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001514
1515 // Get offsets of count, value, and class fields within a string object.
1516 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1517 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1518 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1519
1520 // Note that the null check must have been done earlier.
1521 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1522
1523 StringEqualsOptimizations optimizations(invoke);
1524 if (!optimizations.GetArgumentNotNull()) {
1525 // Check if input is null, return false if it is.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001526 __ CompareAndBranchIfZero(arg, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001527 }
1528
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001529 // Reference equality check, return true if same reference.
1530 __ Cmp(str, arg);
Artem Serov517d9f62016-12-12 15:51:15 +00001531 __ B(eq, &return_true, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001532
Anton Kirilov5ec62182016-10-13 20:16:02 +01001533 if (!optimizations.GetArgumentIsString()) {
1534 // Instanceof check for the argument by comparing class fields.
1535 // All string objects must have the same type since String cannot be subclassed.
1536 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1537 // If the argument is a string object, its class field must be equal to receiver's class field.
1538 __ Ldr(temp, MemOperand(str, class_offset));
Vladimir Marko984519c2017-08-23 10:45:29 +01001539 __ Ldr(out, MemOperand(arg, class_offset));
1540 __ Cmp(temp, out);
Artem Serov517d9f62016-12-12 15:51:15 +00001541 __ B(ne, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001542 }
1543
Vladimir Marko984519c2017-08-23 10:45:29 +01001544 // Check if one of the inputs is a const string. Do not special-case both strings
1545 // being const, such cases should be handled by constant folding if needed.
1546 uint32_t const_string_length = 0u;
1547 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1548 if (const_string == nullptr) {
1549 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1550 if (const_string != nullptr) {
1551 std::swap(str, arg); // Make sure the const string is in `str`.
1552 }
1553 }
1554 bool is_compressed =
1555 mirror::kUseStringCompression &&
1556 const_string != nullptr &&
1557 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1558
1559 if (const_string != nullptr) {
1560 // Load `count` field of the argument string and check if it matches the const string.
1561 // Also compares the compression style, if differs return false.
1562 __ Ldr(temp, MemOperand(arg, count_offset));
1563 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
1564 __ B(ne, &return_false, /* far_target */ false);
1565 } else {
1566 // Load `count` fields of this and argument strings.
1567 __ Ldr(temp, MemOperand(str, count_offset));
1568 __ Ldr(out, MemOperand(arg, count_offset));
1569 // Check if `count` fields are equal, return false if they're not.
1570 // Also compares the compression style, if differs return false.
1571 __ Cmp(temp, out);
1572 __ B(ne, &return_false, /* far_target */ false);
1573 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001574
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001575 // Assertions that must hold in order to compare strings 4 bytes at a time.
Vladimir Marko984519c2017-08-23 10:45:29 +01001576 // Ok to do this because strings are zero-padded to kObjectAlignment.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001577 DCHECK_ALIGNED(value_offset, 4);
1578 static_assert(IsAligned<4>(kObjectAlignment), "String data must be aligned for fast compare.");
1579
Vladimir Marko984519c2017-08-23 10:45:29 +01001580 if (const_string != nullptr &&
1581 const_string_length <= (is_compressed ? kShortConstStringEqualsCutoffInBytes
1582 : kShortConstStringEqualsCutoffInBytes / 2u)) {
1583 // Load and compare the contents. Though we know the contents of the short const string
1584 // at compile time, materializing constants may be more code than loading from memory.
1585 int32_t offset = value_offset;
1586 size_t remaining_bytes =
1587 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 4u);
1588 while (remaining_bytes > sizeof(uint32_t)) {
1589 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1590 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1591 vixl32::Register temp2 = scratch_scope.Acquire();
1592 __ Ldrd(temp, temp1, MemOperand(str, offset));
1593 __ Ldrd(temp2, out, MemOperand(arg, offset));
1594 __ Cmp(temp, temp2);
1595 __ B(ne, &return_false, /* far_label */ false);
1596 __ Cmp(temp1, out);
1597 __ B(ne, &return_false, /* far_label */ false);
1598 offset += 2u * sizeof(uint32_t);
1599 remaining_bytes -= 2u * sizeof(uint32_t);
1600 }
1601 if (remaining_bytes != 0u) {
1602 __ Ldr(temp, MemOperand(str, offset));
1603 __ Ldr(out, MemOperand(arg, offset));
1604 __ Cmp(temp, out);
1605 __ B(ne, &return_false, /* far_label */ false);
1606 }
1607 } else {
1608 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1609 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1610 "Expecting 0=compressed, 1=uncompressed");
1611 __ CompareAndBranchIfZero(temp, &return_true, /* far_target */ false);
1612
1613 if (mirror::kUseStringCompression) {
1614 // For string compression, calculate the number of bytes to compare (not chars).
1615 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1616 __ Lsrs(temp, temp, 1u); // Extract length and check compression flag.
1617 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1618 2 * kMaxInstructionSizeInBytes,
1619 CodeBufferCheckScope::kMaximumSize);
1620 __ it(cs); // If uncompressed,
1621 __ add(cs, temp, temp, temp); // double the byte count.
1622 }
1623
1624 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1625 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1626 vixl32::Register temp2 = scratch_scope.Acquire();
1627
1628 // Store offset of string value in preparation for comparison loop.
1629 __ Mov(temp1, value_offset);
1630
1631 // Loop to compare strings 4 bytes at a time starting at the front of the string.
1632 __ Bind(&loop);
1633 __ Ldr(out, MemOperand(str, temp1));
1634 __ Ldr(temp2, MemOperand(arg, temp1));
1635 __ Add(temp1, temp1, Operand::From(sizeof(uint32_t)));
1636 __ Cmp(out, temp2);
1637 __ B(ne, &return_false, /* far_target */ false);
1638 // With string compression, we have compared 4 bytes, otherwise 2 chars.
1639 __ Subs(temp, temp, mirror::kUseStringCompression ? 4 : 2);
1640 __ B(hi, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001641 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001642
Anton Kirilov5ec62182016-10-13 20:16:02 +01001643 // Return true and exit the function.
1644 // If loop does not result in returning false, we return true.
1645 __ Bind(&return_true);
1646 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00001647 __ B(final_label);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001648
1649 // Return false and exit the function.
1650 __ Bind(&return_false);
1651 __ Mov(out, 0);
Anton Kirilov6f644202017-02-27 18:29:45 +00001652
1653 if (end.IsReferenced()) {
1654 __ Bind(&end);
1655 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001656}
1657
1658static void GenerateVisitStringIndexOf(HInvoke* invoke,
1659 ArmVIXLAssembler* assembler,
1660 CodeGeneratorARMVIXL* codegen,
Anton Kirilov5ec62182016-10-13 20:16:02 +01001661 bool start_at_zero) {
1662 LocationSummary* locations = invoke->GetLocations();
1663
1664 // Note that the null check must have been done earlier.
1665 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1666
1667 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1668 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
1669 SlowPathCodeARMVIXL* slow_path = nullptr;
1670 HInstruction* code_point = invoke->InputAt(1);
1671 if (code_point->IsIntConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001672 if (static_cast<uint32_t>(Int32ConstantFrom(code_point)) >
Anton Kirilov5ec62182016-10-13 20:16:02 +01001673 std::numeric_limits<uint16_t>::max()) {
1674 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1675 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001676 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001677 codegen->AddSlowPath(slow_path);
1678 __ B(slow_path->GetEntryLabel());
1679 __ Bind(slow_path->GetExitLabel());
1680 return;
1681 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 } else if (code_point->GetType() != DataType::Type::kUint16) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001683 vixl32::Register char_reg = InputRegisterAt(invoke, 1);
1684 // 0xffff is not modified immediate but 0x10000 is, so use `>= 0x10000` instead of `> 0xffff`.
1685 __ Cmp(char_reg, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()) + 1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001686 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001687 codegen->AddSlowPath(slow_path);
1688 __ B(hs, slow_path->GetEntryLabel());
1689 }
1690
1691 if (start_at_zero) {
1692 vixl32::Register tmp_reg = RegisterFrom(locations->GetTemp(0));
1693 DCHECK(tmp_reg.Is(r2));
1694 // Start-index = 0.
1695 __ Mov(tmp_reg, 0);
1696 }
1697
1698 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
1699 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
1700
1701 if (slow_path != nullptr) {
1702 __ Bind(slow_path->GetExitLabel());
1703 }
1704}
1705
1706void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001707 LocationSummary* locations = new (allocator_) LocationSummary(
1708 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001709 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1710 // best to align the inputs accordingly.
1711 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1712 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1713 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1714 locations->SetOut(LocationFrom(r0));
1715
1716 // Need to send start-index=0.
1717 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1718}
1719
1720void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001721 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001722}
1723
1724void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 LocationSummary* locations = new (allocator_) LocationSummary(
1726 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001727 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1728 // best to align the inputs accordingly.
1729 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1730 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1731 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1732 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1733 locations->SetOut(LocationFrom(r0));
1734}
1735
1736void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001737 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001738}
1739
1740void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001741 LocationSummary* locations = new (allocator_) LocationSummary(
1742 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001743 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1744 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1745 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1746 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1747 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1748 locations->SetOut(LocationFrom(r0));
1749}
1750
1751void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
1752 ArmVIXLAssembler* assembler = GetAssembler();
1753 vixl32::Register byte_array = InputRegisterAt(invoke, 0);
1754 __ Cmp(byte_array, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001755 SlowPathCodeARMVIXL* slow_path =
1756 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001757 codegen_->AddSlowPath(slow_path);
1758 __ B(eq, slow_path->GetEntryLabel());
1759
1760 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
1761 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
1762 __ Bind(slow_path->GetExitLabel());
1763}
1764
1765void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001766 LocationSummary* locations =
1767 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001768 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1769 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1770 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1771 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1772 locations->SetOut(LocationFrom(r0));
1773}
1774
1775void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
1776 // No need to emit code checking whether `locations->InAt(2)` is a null
1777 // pointer, as callers of the native method
1778 //
1779 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1780 //
1781 // all include a null check on `data` before calling that method.
1782 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
1783 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
1784}
1785
1786void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001787 LocationSummary* locations = new (allocator_) LocationSummary(
1788 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001789 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1790 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1791 locations->SetOut(LocationFrom(r0));
1792}
1793
1794void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
1795 ArmVIXLAssembler* assembler = GetAssembler();
1796 vixl32::Register string_to_copy = InputRegisterAt(invoke, 0);
1797 __ Cmp(string_to_copy, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001798 SlowPathCodeARMVIXL* slow_path =
1799 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001800 codegen_->AddSlowPath(slow_path);
1801 __ B(eq, slow_path->GetEntryLabel());
1802
1803 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
1804 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
1805
1806 __ Bind(slow_path->GetExitLabel());
1807}
1808
1809void IntrinsicLocationsBuilderARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
1810 // The only read barrier implementation supporting the
1811 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1812 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1813 return;
1814 }
1815
1816 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
1817 LocationSummary* locations = invoke->GetLocations();
1818 if (locations == nullptr) {
1819 return;
1820 }
1821
1822 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1823 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1824 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1825
1826 if (src_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(src_pos->GetValue())) {
1827 locations->SetInAt(1, Location::RequiresRegister());
1828 }
1829 if (dest_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(dest_pos->GetValue())) {
1830 locations->SetInAt(3, Location::RequiresRegister());
1831 }
1832 if (length != nullptr && !assembler_->ShifterOperandCanAlwaysHold(length->GetValue())) {
1833 locations->SetInAt(4, Location::RequiresRegister());
1834 }
1835 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1836 // Temporary register IP cannot be used in
1837 // ReadBarrierSystemArrayCopySlowPathARM (because that register
1838 // is clobbered by ReadBarrierMarkRegX entry points). Get an extra
1839 // temporary register from the register allocator.
1840 locations->AddTemp(Location::RequiresRegister());
1841 }
1842}
1843
1844static void CheckPosition(ArmVIXLAssembler* assembler,
1845 Location pos,
1846 vixl32::Register input,
1847 Location length,
1848 SlowPathCodeARMVIXL* slow_path,
1849 vixl32::Register temp,
1850 bool length_is_input_length = false) {
1851 // Where is the length in the Array?
1852 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1853
1854 if (pos.IsConstant()) {
1855 int32_t pos_const = Int32ConstantFrom(pos);
1856 if (pos_const == 0) {
1857 if (!length_is_input_length) {
1858 // Check that length(input) >= length.
1859 __ Ldr(temp, MemOperand(input, length_offset));
1860 if (length.IsConstant()) {
1861 __ Cmp(temp, Int32ConstantFrom(length));
1862 } else {
1863 __ Cmp(temp, RegisterFrom(length));
1864 }
1865 __ B(lt, slow_path->GetEntryLabel());
1866 }
1867 } else {
1868 // Check that length(input) >= pos.
1869 __ Ldr(temp, MemOperand(input, length_offset));
1870 __ Subs(temp, temp, pos_const);
1871 __ B(lt, slow_path->GetEntryLabel());
1872
1873 // Check that (length(input) - pos) >= length.
1874 if (length.IsConstant()) {
1875 __ Cmp(temp, Int32ConstantFrom(length));
1876 } else {
1877 __ Cmp(temp, RegisterFrom(length));
1878 }
1879 __ B(lt, slow_path->GetEntryLabel());
1880 }
1881 } else if (length_is_input_length) {
1882 // The only way the copy can succeed is if pos is zero.
1883 vixl32::Register pos_reg = RegisterFrom(pos);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001884 __ CompareAndBranchIfNonZero(pos_reg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001885 } else {
1886 // Check that pos >= 0.
1887 vixl32::Register pos_reg = RegisterFrom(pos);
1888 __ Cmp(pos_reg, 0);
1889 __ B(lt, slow_path->GetEntryLabel());
1890
1891 // Check that pos <= length(input).
1892 __ Ldr(temp, MemOperand(input, length_offset));
1893 __ Subs(temp, temp, pos_reg);
1894 __ B(lt, slow_path->GetEntryLabel());
1895
1896 // Check that (length(input) - pos) >= length.
1897 if (length.IsConstant()) {
1898 __ Cmp(temp, Int32ConstantFrom(length));
1899 } else {
1900 __ Cmp(temp, RegisterFrom(length));
1901 }
1902 __ B(lt, slow_path->GetEntryLabel());
1903 }
1904}
1905
1906void IntrinsicCodeGeneratorARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
1907 // The only read barrier implementation supporting the
1908 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1909 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1910
1911 ArmVIXLAssembler* assembler = GetAssembler();
1912 LocationSummary* locations = invoke->GetLocations();
1913
1914 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1915 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1916 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1917 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1918 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1919
1920 vixl32::Register src = InputRegisterAt(invoke, 0);
1921 Location src_pos = locations->InAt(1);
1922 vixl32::Register dest = InputRegisterAt(invoke, 2);
1923 Location dest_pos = locations->InAt(3);
1924 Location length = locations->InAt(4);
1925 Location temp1_loc = locations->GetTemp(0);
1926 vixl32::Register temp1 = RegisterFrom(temp1_loc);
1927 Location temp2_loc = locations->GetTemp(1);
1928 vixl32::Register temp2 = RegisterFrom(temp2_loc);
1929 Location temp3_loc = locations->GetTemp(2);
1930 vixl32::Register temp3 = RegisterFrom(temp3_loc);
1931
Vladimir Marko174b2e22017-10-12 13:34:49 +01001932 SlowPathCodeARMVIXL* intrinsic_slow_path =
1933 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001934 codegen_->AddSlowPath(intrinsic_slow_path);
1935
1936 vixl32::Label conditions_on_positions_validated;
1937 SystemArrayCopyOptimizations optimizations(invoke);
1938
1939 // If source and destination are the same, we go to slow path if we need to do
1940 // forward copying.
1941 if (src_pos.IsConstant()) {
1942 int32_t src_pos_constant = Int32ConstantFrom(src_pos);
1943 if (dest_pos.IsConstant()) {
1944 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
1945 if (optimizations.GetDestinationIsSource()) {
1946 // Checked when building locations.
1947 DCHECK_GE(src_pos_constant, dest_pos_constant);
1948 } else if (src_pos_constant < dest_pos_constant) {
1949 __ Cmp(src, dest);
1950 __ B(eq, intrinsic_slow_path->GetEntryLabel());
1951 }
1952
1953 // Checked when building locations.
1954 DCHECK(!optimizations.GetDestinationIsSource()
1955 || (src_pos_constant >= Int32ConstantFrom(dest_pos)));
1956 } else {
1957 if (!optimizations.GetDestinationIsSource()) {
1958 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00001959 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001960 }
1961 __ Cmp(RegisterFrom(dest_pos), src_pos_constant);
1962 __ B(gt, intrinsic_slow_path->GetEntryLabel());
1963 }
1964 } else {
1965 if (!optimizations.GetDestinationIsSource()) {
1966 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00001967 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001968 }
1969 if (dest_pos.IsConstant()) {
1970 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
1971 __ Cmp(RegisterFrom(src_pos), dest_pos_constant);
1972 } else {
1973 __ Cmp(RegisterFrom(src_pos), RegisterFrom(dest_pos));
1974 }
1975 __ B(lt, intrinsic_slow_path->GetEntryLabel());
1976 }
1977
1978 __ Bind(&conditions_on_positions_validated);
1979
1980 if (!optimizations.GetSourceIsNotNull()) {
1981 // Bail out if the source is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001982 __ CompareAndBranchIfZero(src, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001983 }
1984
1985 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1986 // Bail out if the destination is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001987 __ CompareAndBranchIfZero(dest, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001988 }
1989
1990 // If the length is negative, bail out.
1991 // We have already checked in the LocationsBuilder for the constant case.
1992 if (!length.IsConstant() &&
1993 !optimizations.GetCountIsSourceLength() &&
1994 !optimizations.GetCountIsDestinationLength()) {
1995 __ Cmp(RegisterFrom(length), 0);
1996 __ B(lt, intrinsic_slow_path->GetEntryLabel());
1997 }
1998
1999 // Validity checks: source.
2000 CheckPosition(assembler,
2001 src_pos,
2002 src,
2003 length,
2004 intrinsic_slow_path,
2005 temp1,
2006 optimizations.GetCountIsSourceLength());
2007
2008 // Validity checks: dest.
2009 CheckPosition(assembler,
2010 dest_pos,
2011 dest,
2012 length,
2013 intrinsic_slow_path,
2014 temp1,
2015 optimizations.GetCountIsDestinationLength());
2016
2017 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2018 // Check whether all elements of the source array are assignable to the component
2019 // type of the destination array. We do two checks: the classes are the same,
2020 // or the destination is Object[]. If none of these checks succeed, we go to the
2021 // slow path.
2022
2023 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2024 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2025 // /* HeapReference<Class> */ temp1 = src->klass_
2026 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2027 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2028 // Bail out if the source is not a non primitive array.
2029 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2030 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2031 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002032 __ CompareAndBranchIfZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002033 // If heap poisoning is enabled, `temp1` has been unpoisoned
2034 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2035 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2036 __ Ldrh(temp1, MemOperand(temp1, primitive_offset));
2037 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002038 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002039 }
2040
2041 // /* HeapReference<Class> */ temp1 = dest->klass_
2042 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2043 invoke, temp1_loc, dest, class_offset, temp2_loc, /* needs_null_check */ false);
2044
2045 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2046 // Bail out if the destination is not a non primitive array.
2047 //
2048 // Register `temp1` is not trashed by the read barrier emitted
2049 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2050 // method produces a call to a ReadBarrierMarkRegX entry point,
2051 // which saves all potentially live registers, including
2052 // temporaries such a `temp1`.
2053 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2054 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2055 invoke, temp2_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002056 __ CompareAndBranchIfZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002057 // If heap poisoning is enabled, `temp2` has been unpoisoned
2058 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2059 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2060 __ Ldrh(temp2, MemOperand(temp2, primitive_offset));
2061 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002062 __ CompareAndBranchIfNonZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002063 }
2064
2065 // For the same reason given earlier, `temp1` is not trashed by the
2066 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2067 // /* HeapReference<Class> */ temp2 = src->klass_
2068 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2069 invoke, temp2_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
2070 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2071 __ Cmp(temp1, temp2);
2072
2073 if (optimizations.GetDestinationIsTypedObjectArray()) {
2074 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002075 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002076 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2077 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2078 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
2079 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2080 // We do not need to emit a read barrier for the following
2081 // heap reference load, as `temp1` is only used in a
2082 // comparison with null below, and this reference is not
2083 // kept afterwards.
2084 __ Ldr(temp1, MemOperand(temp1, super_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002085 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002086 __ Bind(&do_copy);
2087 } else {
2088 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2089 }
2090 } else {
2091 // Non read barrier code.
2092
2093 // /* HeapReference<Class> */ temp1 = dest->klass_
2094 __ Ldr(temp1, MemOperand(dest, class_offset));
2095 // /* HeapReference<Class> */ temp2 = src->klass_
2096 __ Ldr(temp2, MemOperand(src, class_offset));
2097 bool did_unpoison = false;
2098 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2099 !optimizations.GetSourceIsNonPrimitiveArray()) {
2100 // One or two of the references need to be unpoisoned. Unpoison them
2101 // both to make the identity check valid.
2102 assembler->MaybeUnpoisonHeapReference(temp1);
2103 assembler->MaybeUnpoisonHeapReference(temp2);
2104 did_unpoison = true;
2105 }
2106
2107 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2108 // Bail out if the destination is not a non primitive array.
2109 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2110 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002111 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002112 assembler->MaybeUnpoisonHeapReference(temp3);
2113 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2114 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2115 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002116 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002117 }
2118
2119 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2120 // Bail out if the source is not a non primitive array.
2121 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2122 __ Ldr(temp3, MemOperand(temp2, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002123 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002124 assembler->MaybeUnpoisonHeapReference(temp3);
2125 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2126 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2127 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002128 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002129 }
2130
2131 __ Cmp(temp1, temp2);
2132
2133 if (optimizations.GetDestinationIsTypedObjectArray()) {
2134 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002135 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002136 if (!did_unpoison) {
2137 assembler->MaybeUnpoisonHeapReference(temp1);
2138 }
2139 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2140 __ Ldr(temp1, MemOperand(temp1, component_offset));
2141 assembler->MaybeUnpoisonHeapReference(temp1);
2142 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2143 __ Ldr(temp1, MemOperand(temp1, super_offset));
2144 // No need to unpoison the result, we're comparing against null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002145 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002146 __ Bind(&do_copy);
2147 } else {
2148 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2149 }
2150 }
2151 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2152 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2153 // Bail out if the source is not a non primitive array.
2154 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2155 // /* HeapReference<Class> */ temp1 = src->klass_
2156 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2157 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2158 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2159 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2160 invoke, temp3_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002161 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002162 // If heap poisoning is enabled, `temp3` has been unpoisoned
2163 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2164 } else {
2165 // /* HeapReference<Class> */ temp1 = src->klass_
2166 __ Ldr(temp1, MemOperand(src, class_offset));
2167 assembler->MaybeUnpoisonHeapReference(temp1);
2168 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2169 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002170 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002171 assembler->MaybeUnpoisonHeapReference(temp3);
2172 }
2173 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2174 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2175 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002176 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002177 }
2178
Roland Levillain1663d162017-03-17 15:15:21 +00002179 if (length.IsConstant() && Int32ConstantFrom(length) == 0) {
2180 // Null constant length: not need to emit the loop code at all.
Anton Kirilov5ec62182016-10-13 20:16:02 +01002181 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002182 vixl32::Label done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 const DataType::Type type = DataType::Type::kReference;
2184 const int32_t element_size = DataType::Size(type);
Roland Levillain1663d162017-03-17 15:15:21 +00002185
2186 if (length.IsRegister()) {
2187 // Don't enter the copy loop if the length is null.
2188 __ CompareAndBranchIfZero(RegisterFrom(length), &done, /* is_far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002189 }
Roland Levillain1663d162017-03-17 15:15:21 +00002190
2191 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2192 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2193
2194 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002195 // also CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002196 //
2197 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2198 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2199 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2200 // if (is_gray) {
2201 // // Slow-path copy.
2202 // do {
2203 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2204 // } while (src_ptr != end_ptr)
2205 // } else {
2206 // // Fast-path copy.
2207 // do {
2208 // *dest_ptr++ = *src_ptr++;
2209 // } while (src_ptr != end_ptr)
2210 // }
2211
2212 // /* int32_t */ monitor = src->monitor_
2213 __ Ldr(temp2, MemOperand(src, monitor_offset));
2214 // /* LockWord */ lock_word = LockWord(monitor)
2215 static_assert(sizeof(LockWord) == sizeof(int32_t),
2216 "art::LockWord and int32_t have different sizes.");
2217
2218 // Introduce a dependency on the lock_word including the rb_state,
2219 // which shall prevent load-load reordering without using
2220 // a memory barrier (which would be more expensive).
2221 // `src` is unchanged by this operation, but its value now depends
2222 // on `temp2`.
2223 __ Add(src, src, Operand(temp2, vixl32::LSR, 32));
2224
2225 // Compute the base source address in `temp1`.
2226 // Note that `temp1` (the base source address) is computed from
2227 // `src` (and `src_pos`) here, and thus honors the artificial
2228 // dependency of `src` on `temp2`.
2229 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2230 // Compute the end source address in `temp3`.
2231 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2232 // The base destination address is computed later, as `temp2` is
2233 // used for intermediate computations.
2234
2235 // Slow path used to copy array when `src` is gray.
2236 // Note that the base destination address is computed in `temp2`
2237 // by the slow path code.
2238 SlowPathCodeARMVIXL* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002239 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathARMVIXL(invoke);
Roland Levillain1663d162017-03-17 15:15:21 +00002240 codegen_->AddSlowPath(read_barrier_slow_path);
2241
2242 // Given the numeric representation, it's enough to check the low bit of the
2243 // rb_state. We do that by shifting the bit out of the lock word with LSRS
2244 // which can be a 16-bit instruction unlike the TST immediate.
Roland Levillain14e5a292018-06-28 12:00:56 +01002245 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Roland Levillain1663d162017-03-17 15:15:21 +00002246 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2247 __ Lsrs(temp2, temp2, LockWord::kReadBarrierStateShift + 1);
2248 // Carry flag is the last bit shifted out by LSRS.
2249 __ B(cs, read_barrier_slow_path->GetEntryLabel());
2250
2251 // Fast-path copy.
2252 // Compute the base destination address in `temp2`.
2253 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2254 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2255 // poison/unpoison.
2256 vixl32::Label loop;
2257 __ Bind(&loop);
2258 {
2259 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2260 const vixl32::Register temp_reg = temps.Acquire();
2261 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2262 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2263 }
2264 __ Cmp(temp1, temp3);
2265 __ B(ne, &loop, /* far_target */ false);
2266
2267 __ Bind(read_barrier_slow_path->GetExitLabel());
2268 } else {
2269 // Non read barrier code.
2270 // Compute the base source address in `temp1`.
2271 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2272 // Compute the base destination address in `temp2`.
2273 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2274 // Compute the end source address in `temp3`.
2275 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2276 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2277 // poison/unpoison.
2278 vixl32::Label loop;
2279 __ Bind(&loop);
2280 {
2281 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2282 const vixl32::Register temp_reg = temps.Acquire();
2283 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2284 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2285 }
2286 __ Cmp(temp1, temp3);
2287 __ B(ne, &loop, /* far_target */ false);
2288 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002289 __ Bind(&done);
2290 }
2291
2292 // We only need one card marking on the destination array.
2293 codegen_->MarkGCCard(temp1, temp2, dest, NoReg, /* value_can_be_null */ false);
2294
2295 __ Bind(intrinsic_slow_path->GetExitLabel());
2296}
2297
Vladimir Markoca6fff82017-10-03 14:49:14 +01002298static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002299 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2300 // the code generator. Furthermore, the register allocator creates fixed live intervals
2301 // for all caller-saved registers because we are doing a function call. As a result, if
2302 // the input and output locations are unallocated, the register allocator runs out of
2303 // registers and fails; however, a debuggable graph is not the common case.
2304 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2305 return;
2306 }
2307
2308 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002309 DCHECK_EQ(invoke->InputAt(0)->GetType(), DataType::Type::kFloat64);
2310 DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002311
Vladimir Markoca6fff82017-10-03 14:49:14 +01002312 LocationSummary* const locations =
2313 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002314 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2315
2316 locations->SetInAt(0, Location::RequiresFpuRegister());
2317 locations->SetOut(Location::RequiresFpuRegister());
2318 // Native code uses the soft float ABI.
2319 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2320 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2321}
2322
Vladimir Markoca6fff82017-10-03 14:49:14 +01002323static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002324 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2325 // the code generator. Furthermore, the register allocator creates fixed live intervals
2326 // for all caller-saved registers because we are doing a function call. As a result, if
2327 // the input and output locations are unallocated, the register allocator runs out of
2328 // registers and fails; however, a debuggable graph is not the common case.
2329 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2330 return;
2331 }
2332
2333 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002334 DCHECK_EQ(invoke->InputAt(0)->GetType(), DataType::Type::kFloat64);
2335 DCHECK_EQ(invoke->InputAt(1)->GetType(), DataType::Type::kFloat64);
2336 DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002337
Vladimir Markoca6fff82017-10-03 14:49:14 +01002338 LocationSummary* const locations =
2339 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002340 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2341
2342 locations->SetInAt(0, Location::RequiresFpuRegister());
2343 locations->SetInAt(1, Location::RequiresFpuRegister());
2344 locations->SetOut(Location::RequiresFpuRegister());
2345 // Native code uses the soft float ABI.
2346 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2347 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2348 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
2349 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(3)));
2350}
2351
2352static void GenFPToFPCall(HInvoke* invoke,
2353 ArmVIXLAssembler* assembler,
2354 CodeGeneratorARMVIXL* codegen,
2355 QuickEntrypointEnum entry) {
2356 LocationSummary* const locations = invoke->GetLocations();
2357
2358 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2359 DCHECK(locations->WillCall() && locations->Intrinsified());
2360
2361 // Native code uses the soft float ABI.
2362 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2363 RegisterFrom(locations->GetTemp(1)),
2364 InputDRegisterAt(invoke, 0));
2365 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2366 __ Vmov(OutputDRegister(invoke),
2367 RegisterFrom(locations->GetTemp(0)),
2368 RegisterFrom(locations->GetTemp(1)));
2369}
2370
2371static void GenFPFPToFPCall(HInvoke* invoke,
2372 ArmVIXLAssembler* assembler,
2373 CodeGeneratorARMVIXL* codegen,
2374 QuickEntrypointEnum entry) {
2375 LocationSummary* const locations = invoke->GetLocations();
2376
2377 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2378 DCHECK(locations->WillCall() && locations->Intrinsified());
2379
2380 // Native code uses the soft float ABI.
2381 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2382 RegisterFrom(locations->GetTemp(1)),
2383 InputDRegisterAt(invoke, 0));
2384 __ Vmov(RegisterFrom(locations->GetTemp(2)),
2385 RegisterFrom(locations->GetTemp(3)),
2386 InputDRegisterAt(invoke, 1));
2387 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2388 __ Vmov(OutputDRegister(invoke),
2389 RegisterFrom(locations->GetTemp(0)),
2390 RegisterFrom(locations->GetTemp(1)));
2391}
2392
2393void IntrinsicLocationsBuilderARMVIXL::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002394 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002395}
2396
2397void IntrinsicCodeGeneratorARMVIXL::VisitMathCos(HInvoke* invoke) {
2398 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCos);
2399}
2400
2401void IntrinsicLocationsBuilderARMVIXL::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002402 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002403}
2404
2405void IntrinsicCodeGeneratorARMVIXL::VisitMathSin(HInvoke* invoke) {
2406 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSin);
2407}
2408
2409void IntrinsicLocationsBuilderARMVIXL::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002410 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002411}
2412
2413void IntrinsicCodeGeneratorARMVIXL::VisitMathAcos(HInvoke* invoke) {
2414 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAcos);
2415}
2416
2417void IntrinsicLocationsBuilderARMVIXL::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002418 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002419}
2420
2421void IntrinsicCodeGeneratorARMVIXL::VisitMathAsin(HInvoke* invoke) {
2422 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAsin);
2423}
2424
2425void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002426 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002427}
2428
2429void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan(HInvoke* invoke) {
2430 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan);
2431}
2432
2433void IntrinsicLocationsBuilderARMVIXL::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002434 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002435}
2436
2437void IntrinsicCodeGeneratorARMVIXL::VisitMathCbrt(HInvoke* invoke) {
2438 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCbrt);
2439}
2440
2441void IntrinsicLocationsBuilderARMVIXL::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002442 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002443}
2444
2445void IntrinsicCodeGeneratorARMVIXL::VisitMathCosh(HInvoke* invoke) {
2446 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCosh);
2447}
2448
2449void IntrinsicLocationsBuilderARMVIXL::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002450 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002451}
2452
2453void IntrinsicCodeGeneratorARMVIXL::VisitMathExp(HInvoke* invoke) {
2454 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExp);
2455}
2456
2457void IntrinsicLocationsBuilderARMVIXL::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002458 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002459}
2460
2461void IntrinsicCodeGeneratorARMVIXL::VisitMathExpm1(HInvoke* invoke) {
2462 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExpm1);
2463}
2464
2465void IntrinsicLocationsBuilderARMVIXL::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002466 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002467}
2468
2469void IntrinsicCodeGeneratorARMVIXL::VisitMathLog(HInvoke* invoke) {
2470 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog);
2471}
2472
2473void IntrinsicLocationsBuilderARMVIXL::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002474 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002475}
2476
2477void IntrinsicCodeGeneratorARMVIXL::VisitMathLog10(HInvoke* invoke) {
2478 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog10);
2479}
2480
2481void IntrinsicLocationsBuilderARMVIXL::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002482 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002483}
2484
2485void IntrinsicCodeGeneratorARMVIXL::VisitMathSinh(HInvoke* invoke) {
2486 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSinh);
2487}
2488
2489void IntrinsicLocationsBuilderARMVIXL::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002490 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002491}
2492
2493void IntrinsicCodeGeneratorARMVIXL::VisitMathTan(HInvoke* invoke) {
2494 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTan);
2495}
2496
2497void IntrinsicLocationsBuilderARMVIXL::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002498 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002499}
2500
2501void IntrinsicCodeGeneratorARMVIXL::VisitMathTanh(HInvoke* invoke) {
2502 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTanh);
2503}
2504
2505void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002506 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002507}
2508
2509void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan2(HInvoke* invoke) {
2510 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan2);
2511}
2512
Vladimir Marko4d179872018-01-19 14:50:10 +00002513void IntrinsicLocationsBuilderARMVIXL::VisitMathPow(HInvoke* invoke) {
2514 CreateFPFPToFPCallLocations(allocator_, invoke);
2515}
2516
2517void IntrinsicCodeGeneratorARMVIXL::VisitMathPow(HInvoke* invoke) {
2518 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickPow);
2519}
2520
Anton Kirilov5ec62182016-10-13 20:16:02 +01002521void IntrinsicLocationsBuilderARMVIXL::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002522 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002523}
2524
2525void IntrinsicCodeGeneratorARMVIXL::VisitMathHypot(HInvoke* invoke) {
2526 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickHypot);
2527}
2528
2529void IntrinsicLocationsBuilderARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002530 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002531}
2532
2533void IntrinsicCodeGeneratorARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
2534 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickNextAfter);
2535}
2536
2537void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002538 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002539}
2540
2541void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
2542 ArmVIXLAssembler* assembler = GetAssembler();
2543 __ Rbit(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2544}
2545
2546void IntrinsicLocationsBuilderARMVIXL::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002547 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002548}
2549
2550void IntrinsicCodeGeneratorARMVIXL::VisitLongReverse(HInvoke* invoke) {
2551 ArmVIXLAssembler* assembler = GetAssembler();
2552 LocationSummary* locations = invoke->GetLocations();
2553
2554 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2555 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2556 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2557 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2558
2559 __ Rbit(out_reg_lo, in_reg_hi);
2560 __ Rbit(out_reg_hi, in_reg_lo);
2561}
2562
2563void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002564 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002565}
2566
2567void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
2568 ArmVIXLAssembler* assembler = GetAssembler();
2569 __ Rev(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2570}
2571
2572void IntrinsicLocationsBuilderARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002573 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002574}
2575
2576void IntrinsicCodeGeneratorARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
2577 ArmVIXLAssembler* assembler = GetAssembler();
2578 LocationSummary* locations = invoke->GetLocations();
2579
2580 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2581 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2582 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2583 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2584
2585 __ Rev(out_reg_lo, in_reg_hi);
2586 __ Rev(out_reg_hi, in_reg_lo);
2587}
2588
2589void IntrinsicLocationsBuilderARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002590 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002591}
2592
2593void IntrinsicCodeGeneratorARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
2594 ArmVIXLAssembler* assembler = GetAssembler();
2595 __ Revsh(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2596}
2597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598static void GenBitCount(HInvoke* instr, DataType::Type type, ArmVIXLAssembler* assembler) {
2599 DCHECK(DataType::IsIntOrLongType(type)) << type;
2600 DCHECK_EQ(instr->GetType(), DataType::Type::kInt32);
2601 DCHECK_EQ(DataType::Kind(instr->InputAt(0)->GetType()), type);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 bool is_long = type == DataType::Type::kInt64;
Anton Kirilov5ec62182016-10-13 20:16:02 +01002604 LocationSummary* locations = instr->GetLocations();
2605 Location in = locations->InAt(0);
2606 vixl32::Register src_0 = is_long ? LowRegisterFrom(in) : RegisterFrom(in);
2607 vixl32::Register src_1 = is_long ? HighRegisterFrom(in) : src_0;
2608 vixl32::SRegister tmp_s = LowSRegisterFrom(locations->GetTemp(0));
2609 vixl32::DRegister tmp_d = DRegisterFrom(locations->GetTemp(0));
2610 vixl32::Register out_r = OutputRegister(instr);
2611
2612 // Move data from core register(s) to temp D-reg for bit count calculation, then move back.
2613 // According to Cortex A57 and A72 optimization guides, compared to transferring to full D-reg,
2614 // transferring data from core reg to upper or lower half of vfp D-reg requires extra latency,
2615 // That's why for integer bit count, we use 'vmov d0, r0, r0' instead of 'vmov d0[0], r0'.
2616 __ Vmov(tmp_d, src_1, src_0); // Temp DReg |--src_1|--src_0|
2617 __ Vcnt(Untyped8, tmp_d, tmp_d); // Temp DReg |c|c|c|c|c|c|c|c|
2618 __ Vpaddl(U8, tmp_d, tmp_d); // Temp DReg |--c|--c|--c|--c|
2619 __ Vpaddl(U16, tmp_d, tmp_d); // Temp DReg |------c|------c|
2620 if (is_long) {
2621 __ Vpaddl(U32, tmp_d, tmp_d); // Temp DReg |--------------c|
2622 }
2623 __ Vmov(out_r, tmp_s);
2624}
2625
2626void IntrinsicLocationsBuilderARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002627 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002628 invoke->GetLocations()->AddTemp(Location::RequiresFpuRegister());
2629}
2630
2631void IntrinsicCodeGeneratorARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 GenBitCount(invoke, DataType::Type::kInt32, GetAssembler());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002633}
2634
2635void IntrinsicLocationsBuilderARMVIXL::VisitLongBitCount(HInvoke* invoke) {
2636 VisitIntegerBitCount(invoke);
2637}
2638
2639void IntrinsicCodeGeneratorARMVIXL::VisitLongBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 GenBitCount(invoke, DataType::Type::kInt64, GetAssembler());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002641}
2642
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002643static void GenHighestOneBit(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002644 DataType::Type type,
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002645 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 DCHECK(DataType::IsIntOrLongType(type));
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002647
2648 ArmVIXLAssembler* assembler = codegen->GetAssembler();
2649 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2650 const vixl32::Register temp = temps.Acquire();
2651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 if (type == DataType::Type::kInt64) {
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002653 LocationSummary* locations = invoke->GetLocations();
2654 Location in = locations->InAt(0);
2655 Location out = locations->Out();
2656
2657 vixl32::Register in_reg_lo = LowRegisterFrom(in);
2658 vixl32::Register in_reg_hi = HighRegisterFrom(in);
2659 vixl32::Register out_reg_lo = LowRegisterFrom(out);
2660 vixl32::Register out_reg_hi = HighRegisterFrom(out);
2661
2662 __ Mov(temp, 0x80000000); // Modified immediate.
2663 __ Clz(out_reg_lo, in_reg_lo);
2664 __ Clz(out_reg_hi, in_reg_hi);
2665 __ Lsr(out_reg_lo, temp, out_reg_lo);
2666 __ Lsrs(out_reg_hi, temp, out_reg_hi);
2667
2668 // Discard result for lowest 32 bits if highest 32 bits are not zero.
2669 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2670 // we check that the output is in a low register, so that a 16-bit MOV
2671 // encoding can be used. If output is in a high register, then we generate
2672 // 4 more bytes of code to avoid a branch.
2673 Operand mov_src(0);
2674 if (!out_reg_lo.IsLow()) {
2675 __ Mov(LeaveFlags, temp, 0);
2676 mov_src = Operand(temp);
2677 }
2678 ExactAssemblyScope it_scope(codegen->GetVIXLAssembler(),
2679 2 * vixl32::k16BitT32InstructionSizeInBytes,
2680 CodeBufferCheckScope::kExactSize);
2681 __ it(ne);
2682 __ mov(ne, out_reg_lo, mov_src);
2683 } else {
2684 vixl32::Register out = OutputRegister(invoke);
2685 vixl32::Register in = InputRegisterAt(invoke, 0);
2686
2687 __ Mov(temp, 0x80000000); // Modified immediate.
2688 __ Clz(out, in);
2689 __ Lsr(out, temp, out);
2690 }
2691}
2692
2693void IntrinsicLocationsBuilderARMVIXL::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002694 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002695}
2696
2697void IntrinsicCodeGeneratorARMVIXL::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 GenHighestOneBit(invoke, DataType::Type::kInt32, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002699}
2700
2701void IntrinsicLocationsBuilderARMVIXL::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002702 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002703}
2704
2705void IntrinsicCodeGeneratorARMVIXL::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 GenHighestOneBit(invoke, DataType::Type::kInt64, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002707}
2708
2709static void GenLowestOneBit(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002710 DataType::Type type,
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002711 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 DCHECK(DataType::IsIntOrLongType(type));
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002713
2714 ArmVIXLAssembler* assembler = codegen->GetAssembler();
2715 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2716 const vixl32::Register temp = temps.Acquire();
2717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 if (type == DataType::Type::kInt64) {
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002719 LocationSummary* locations = invoke->GetLocations();
2720 Location in = locations->InAt(0);
2721 Location out = locations->Out();
2722
2723 vixl32::Register in_reg_lo = LowRegisterFrom(in);
2724 vixl32::Register in_reg_hi = HighRegisterFrom(in);
2725 vixl32::Register out_reg_lo = LowRegisterFrom(out);
2726 vixl32::Register out_reg_hi = HighRegisterFrom(out);
2727
2728 __ Rsb(out_reg_hi, in_reg_hi, 0);
2729 __ Rsb(out_reg_lo, in_reg_lo, 0);
2730 __ And(out_reg_hi, out_reg_hi, in_reg_hi);
2731 // The result of this operation is 0 iff in_reg_lo is 0
2732 __ Ands(out_reg_lo, out_reg_lo, in_reg_lo);
2733
2734 // Discard result for highest 32 bits if lowest 32 bits are not zero.
2735 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2736 // we check that the output is in a low register, so that a 16-bit MOV
2737 // encoding can be used. If output is in a high register, then we generate
2738 // 4 more bytes of code to avoid a branch.
2739 Operand mov_src(0);
2740 if (!out_reg_lo.IsLow()) {
2741 __ Mov(LeaveFlags, temp, 0);
2742 mov_src = Operand(temp);
2743 }
2744 ExactAssemblyScope it_scope(codegen->GetVIXLAssembler(),
2745 2 * vixl32::k16BitT32InstructionSizeInBytes,
2746 CodeBufferCheckScope::kExactSize);
2747 __ it(ne);
2748 __ mov(ne, out_reg_hi, mov_src);
2749 } else {
2750 vixl32::Register out = OutputRegister(invoke);
2751 vixl32::Register in = InputRegisterAt(invoke, 0);
2752
2753 __ Rsb(temp, in, 0);
2754 __ And(out, temp, in);
2755 }
2756}
2757
2758void IntrinsicLocationsBuilderARMVIXL::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002759 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002760}
2761
2762void IntrinsicCodeGeneratorARMVIXL::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 GenLowestOneBit(invoke, DataType::Type::kInt32, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002764}
2765
2766void IntrinsicLocationsBuilderARMVIXL::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002767 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002768}
2769
2770void IntrinsicCodeGeneratorARMVIXL::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 GenLowestOneBit(invoke, DataType::Type::kInt64, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002772}
2773
Anton Kirilov5ec62182016-10-13 20:16:02 +01002774void IntrinsicLocationsBuilderARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002775 LocationSummary* locations =
2776 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002777 locations->SetInAt(0, Location::RequiresRegister());
2778 locations->SetInAt(1, Location::RequiresRegister());
2779 locations->SetInAt(2, Location::RequiresRegister());
2780 locations->SetInAt(3, Location::RequiresRegister());
2781 locations->SetInAt(4, Location::RequiresRegister());
2782
2783 // Temporary registers to store lengths of strings and for calculations.
2784 locations->AddTemp(Location::RequiresRegister());
2785 locations->AddTemp(Location::RequiresRegister());
2786 locations->AddTemp(Location::RequiresRegister());
2787}
2788
2789void IntrinsicCodeGeneratorARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2790 ArmVIXLAssembler* assembler = GetAssembler();
2791 LocationSummary* locations = invoke->GetLocations();
2792
2793 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002795 DCHECK_EQ(char_size, 2u);
2796
2797 // Location of data in char array buffer.
2798 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2799
2800 // Location of char array data in string.
2801 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2802
2803 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2804 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2805 vixl32::Register srcObj = InputRegisterAt(invoke, 0);
2806 vixl32::Register srcBegin = InputRegisterAt(invoke, 1);
2807 vixl32::Register srcEnd = InputRegisterAt(invoke, 2);
2808 vixl32::Register dstObj = InputRegisterAt(invoke, 3);
2809 vixl32::Register dstBegin = InputRegisterAt(invoke, 4);
2810
2811 vixl32::Register num_chr = RegisterFrom(locations->GetTemp(0));
2812 vixl32::Register src_ptr = RegisterFrom(locations->GetTemp(1));
2813 vixl32::Register dst_ptr = RegisterFrom(locations->GetTemp(2));
2814
2815 vixl32::Label done, compressed_string_loop;
Anton Kirilov6f644202017-02-27 18:29:45 +00002816 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002817 // dst to be copied.
2818 __ Add(dst_ptr, dstObj, data_offset);
2819 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, vixl32::LSL, 1));
2820
2821 __ Subs(num_chr, srcEnd, srcBegin);
2822 // Early out for valid zero-length retrievals.
Anton Kirilov6f644202017-02-27 18:29:45 +00002823 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002824
2825 // src range to copy.
2826 __ Add(src_ptr, srcObj, value_offset);
2827
2828 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2829 vixl32::Register temp;
2830 vixl32::Label compressed_string_preloop;
2831 if (mirror::kUseStringCompression) {
2832 // Location of count in string.
2833 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2834 temp = temps.Acquire();
2835 // String's length.
2836 __ Ldr(temp, MemOperand(srcObj, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002837 __ Tst(temp, 1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002838 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002839 __ B(eq, &compressed_string_preloop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002840 }
2841 __ Add(src_ptr, src_ptr, Operand(srcBegin, vixl32::LSL, 1));
2842
2843 // Do the copy.
2844 vixl32::Label loop, remainder;
2845
2846 temp = temps.Acquire();
2847 // Save repairing the value of num_chr on the < 4 character path.
2848 __ Subs(temp, num_chr, 4);
Artem Serov517d9f62016-12-12 15:51:15 +00002849 __ B(lt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002850
2851 // Keep the result of the earlier subs, we are going to fetch at least 4 characters.
2852 __ Mov(num_chr, temp);
2853
2854 // Main loop used for longer fetches loads and stores 4x16-bit characters at a time.
2855 // (LDRD/STRD fault on unaligned addresses and it's not worth inlining extra code
2856 // to rectify these everywhere this intrinsic applies.)
2857 __ Bind(&loop);
2858 __ Ldr(temp, MemOperand(src_ptr, char_size * 2));
2859 __ Subs(num_chr, num_chr, 4);
2860 __ Str(temp, MemOperand(dst_ptr, char_size * 2));
2861 __ Ldr(temp, MemOperand(src_ptr, char_size * 4, PostIndex));
2862 __ Str(temp, MemOperand(dst_ptr, char_size * 4, PostIndex));
2863 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002864 __ B(ge, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002865
2866 __ Adds(num_chr, num_chr, 4);
Anton Kirilov6f644202017-02-27 18:29:45 +00002867 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002868
2869 // Main loop for < 4 character case and remainder handling. Loads and stores one
2870 // 16-bit Java character at a time.
2871 __ Bind(&remainder);
2872 temp = temps.Acquire();
2873 __ Ldrh(temp, MemOperand(src_ptr, char_size, PostIndex));
2874 __ Subs(num_chr, num_chr, 1);
2875 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2876 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002877 __ B(gt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002878
2879 if (mirror::kUseStringCompression) {
Anton Kirilov6f644202017-02-27 18:29:45 +00002880 __ B(final_label);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002883 DCHECK_EQ(c_char_size, 1u);
2884 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2885 __ Bind(&compressed_string_preloop);
2886 __ Add(src_ptr, src_ptr, srcBegin);
2887 __ Bind(&compressed_string_loop);
2888 temp = temps.Acquire();
2889 __ Ldrb(temp, MemOperand(src_ptr, c_char_size, PostIndex));
2890 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2891 temps.Release(temp);
2892 __ Subs(num_chr, num_chr, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00002893 __ B(gt, &compressed_string_loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002894 }
2895
Anton Kirilov6f644202017-02-27 18:29:45 +00002896 if (done.IsReferenced()) {
2897 __ Bind(&done);
2898 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002899}
2900
2901void IntrinsicLocationsBuilderARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002902 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002903}
2904
2905void IntrinsicCodeGeneratorARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
2906 ArmVIXLAssembler* const assembler = GetAssembler();
2907 const vixl32::Register out = OutputRegister(invoke);
2908 // Shifting left by 1 bit makes the value encodable as an immediate operand;
2909 // we don't care about the sign bit anyway.
2910 constexpr uint32_t infinity = kPositiveInfinityFloat << 1U;
2911
2912 __ Vmov(out, InputSRegisterAt(invoke, 0));
2913 // We don't care about the sign bit, so shift left.
2914 __ Lsl(out, out, 1);
2915 __ Eor(out, out, infinity);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002916 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002917}
2918
2919void IntrinsicLocationsBuilderARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002920 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002921}
2922
2923void IntrinsicCodeGeneratorARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
2924 ArmVIXLAssembler* const assembler = GetAssembler();
2925 const vixl32::Register out = OutputRegister(invoke);
2926 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2927 const vixl32::Register temp = temps.Acquire();
2928 // The highest 32 bits of double precision positive infinity separated into
2929 // two constants encodable as immediate operands.
2930 constexpr uint32_t infinity_high = 0x7f000000U;
2931 constexpr uint32_t infinity_high2 = 0x00f00000U;
2932
2933 static_assert((infinity_high | infinity_high2) ==
2934 static_cast<uint32_t>(kPositiveInfinityDouble >> 32U),
2935 "The constants do not add up to the high 32 bits of double "
2936 "precision positive infinity.");
2937 __ Vmov(temp, out, InputDRegisterAt(invoke, 0));
2938 __ Eor(out, out, infinity_high);
2939 __ Eor(out, out, infinity_high2);
2940 // We don't care about the sign bit, so shift left.
2941 __ Orr(out, temp, Operand(out, vixl32::LSL, 1));
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002942 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002943}
2944
Artem Serov9aee2d42017-01-06 15:58:31 +00002945void IntrinsicLocationsBuilderARMVIXL::VisitMathCeil(HInvoke* invoke) {
2946 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002947 CreateFPToFPLocations(allocator_, invoke);
Artem Serov9aee2d42017-01-06 15:58:31 +00002948 }
2949}
2950
2951void IntrinsicCodeGeneratorARMVIXL::VisitMathCeil(HInvoke* invoke) {
2952 ArmVIXLAssembler* assembler = GetAssembler();
2953 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
2954 __ Vrintp(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
2955}
2956
2957void IntrinsicLocationsBuilderARMVIXL::VisitMathFloor(HInvoke* invoke) {
2958 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002959 CreateFPToFPLocations(allocator_, invoke);
Artem Serov9aee2d42017-01-06 15:58:31 +00002960 }
2961}
2962
2963void IntrinsicCodeGeneratorARMVIXL::VisitMathFloor(HInvoke* invoke) {
2964 ArmVIXLAssembler* assembler = GetAssembler();
2965 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
2966 __ Vrintm(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
2967}
2968
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002969void IntrinsicLocationsBuilderARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
2970 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2971 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2972 invoke,
2973 codegen_,
2974 LocationFrom(r0),
2975 LocationFrom(calling_convention.GetRegisterAt(0)));
2976}
2977
2978void IntrinsicCodeGeneratorARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01002979 IntrinsicVisitor::IntegerValueOfInfo info =
2980 IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002981 LocationSummary* locations = invoke->GetLocations();
2982 ArmVIXLAssembler* const assembler = GetAssembler();
2983
2984 vixl32::Register out = RegisterFrom(locations->Out());
2985 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2986 vixl32::Register temp = temps.Acquire();
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002987 if (invoke->InputAt(0)->IsConstant()) {
2988 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
Vladimir Marko6fd16062018-06-26 11:02:04 +01002989 if (static_cast<uint32_t>(value - info.low) < info.length) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002990 // Just embed the j.l.Integer in the code.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002991 DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
2992 codegen_->LoadBootImageAddress(out, info.value_boot_image_reference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002993 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002994 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002995 // Allocate and initialize a new j.l.Integer.
2996 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2997 // JIT object table.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002998 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2999 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003000 __ Mov(temp, value);
3001 assembler->StoreToOffset(kStoreWord, temp, out, info.value_offset);
3002 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3003 // one.
3004 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3005 }
3006 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01003007 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003008 vixl32::Register in = RegisterFrom(locations->InAt(0));
3009 // Check bounds of our cache.
3010 __ Add(out, in, -info.low);
Vladimir Markoeebb8212018-06-05 14:57:24 +01003011 __ Cmp(out, info.length);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003012 vixl32::Label allocate, done;
Anton Kirilovfd522532017-05-10 12:46:57 +01003013 __ B(hs, &allocate, /* is_far_target */ false);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003014 // If the value is within the bounds, load the j.l.Integer directly from the array.
Vladimir Marko6fd16062018-06-26 11:02:04 +01003015 codegen_->LoadBootImageAddress(temp, info.array_data_boot_image_reference);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 codegen_->LoadFromShiftedRegOffset(DataType::Type::kReference, locations->Out(), temp, out);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003017 assembler->MaybeUnpoisonHeapReference(out);
3018 __ B(&done);
3019 __ Bind(&allocate);
3020 // Otherwise allocate and initialize a new j.l.Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +01003021 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
3022 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003023 assembler->StoreToOffset(kStoreWord, in, out, info.value_offset);
3024 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3025 // one.
3026 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3027 __ Bind(&done);
3028 }
3029}
3030
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003031void IntrinsicLocationsBuilderARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003032 LocationSummary* locations =
3033 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003034 locations->SetOut(Location::RequiresRegister());
3035}
3036
3037void IntrinsicCodeGeneratorARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
3038 ArmVIXLAssembler* assembler = GetAssembler();
3039 vixl32::Register out = RegisterFrom(invoke->GetLocations()->Out());
3040 int32_t offset = Thread::InterruptedOffset<kArmPointerSize>().Int32Value();
3041 __ Ldr(out, MemOperand(tr, offset));
3042 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3043 vixl32::Register temp = temps.Acquire();
3044 vixl32::Label done;
Anton Kirilovfd522532017-05-10 12:46:57 +01003045 vixl32::Label* const final_label = codegen_->GetFinalLabel(invoke, &done);
3046 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003047 __ Dmb(vixl32::ISH);
3048 __ Mov(temp, 0);
3049 assembler->StoreToOffset(kStoreWord, temp, tr, offset);
3050 __ Dmb(vixl32::ISH);
Anton Kirilovfd522532017-05-10 12:46:57 +01003051 if (done.IsReferenced()) {
3052 __ Bind(&done);
3053 }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003054}
3055
Hans Boehmc7b28de2018-03-09 17:05:28 -08003056void IntrinsicLocationsBuilderARMVIXL::VisitReachabilityFence(HInvoke* invoke) {
3057 LocationSummary* locations =
3058 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
3059 locations->SetInAt(0, Location::Any());
3060}
3061
3062void IntrinsicCodeGeneratorARMVIXL::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
3063
Anton Kirilov5ec62182016-10-13 20:16:02 +01003064UNIMPLEMENTED_INTRINSIC(ARMVIXL, MathRoundDouble) // Could be done by changing rounding mode, maybe?
Anton Kirilov5ec62182016-10-13 20:16:02 +01003065UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeCASLong) // High register pressure.
3066UNIMPLEMENTED_INTRINSIC(ARMVIXL, SystemArrayCopyChar)
Vladimir Markod254f5c2017-06-02 15:18:36 +00003067UNIMPLEMENTED_INTRINSIC(ARMVIXL, ReferenceGetReferent)
Anton Kirilov5ec62182016-10-13 20:16:02 +01003068
Aart Bikff7d89c2016-11-07 08:49:28 -08003069UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOf);
3070UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003071UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferAppend);
3072UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferLength);
3073UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferToString);
3074UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderAppend);
3075UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderLength);
3076UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003077
Anton Kirilov5ec62182016-10-13 20:16:02 +01003078// 1.8.
3079UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddInt)
3080UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddLong)
3081UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetInt)
3082UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetLong)
3083UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetObject)
3084
3085UNREACHABLE_INTRINSICS(ARMVIXL)
3086
3087#undef __
3088
3089} // namespace arm
3090} // namespace art