blob: 0c463a280e49048a5d4fce0c447d805e8661ad08 [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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010088 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilov5ec62182016-10-13 20:16:02 +010089 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100114 const char* GetDescription() const override { return "IntrinsicSlowPath"; }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100115
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100176 void EmitNativeCode(CodeGenerator* codegen) override {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100177 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100236 const char* GetDescription() const override {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100237 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();
Roland Levillain94770472018-10-04 12:47:01 +0000449 __ Vrintn(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100450}
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.
Roland Levillain94770472018-10-04 12:47:01 +0000484 __ Vrinta(F32, F32, temp1, in_reg);
xueliang.zhong53463ba2017-02-16 15:18:03 +0000485 __ 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100972 const char* GetDescription() const override { return "BakerReadBarrierCasSlowPathARMVIXL"; }
Vladimir Markod887ed82018-08-14 13:52:12 +0000973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100974 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markod887ed82018-08-14 13:52:12 +0000975 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);
Andreas Gampe25877422018-09-20 13:32:57 -07001095 static_cast<vixl32::MacroAssembler*>(assembler->GetVIXLAssembler())->
1096 B(ne, failure, /* hint= */ (failure == loop_exit) ? kNear : kBranchWithoutHint);
Vladimir Markod887ed82018-08-14 13:52:12 +00001097 if (type == DataType::Type::kReference) {
1098 assembler->MaybePoisonHeapReference(value);
1099 }
1100 __ Strex(tmp, value, MemOperand(tmp_ptr));
1101 if (type == DataType::Type::kReference) {
1102 assembler->MaybeUnpoisonHeapReference(value);
1103 }
1104 __ Cmp(tmp, 0);
1105 __ B(ne, &loop_head, /* far_target */ false);
Vladimir Markof28be432018-08-14 12:20:51 +00001106
Vladimir Markod887ed82018-08-14 13:52:12 +00001107 __ Bind(loop_exit);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001108
1109 __ Dmb(vixl32::ISH);
1110
Vladimir Markod887ed82018-08-14 13:52:12 +00001111 // out = tmp == 0.
1112 __ Clz(out, tmp);
1113 __ Lsr(out, out, WhichPowerOf2(out.GetSizeInBits()));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001114
Vladimir Markod887ed82018-08-14 13:52:12 +00001115 if (type == DataType::Type::kReference) {
1116 codegen->MaybeGenerateMarkingRegisterCheck(/* code */ 128);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001117 }
1118}
1119
1120void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markod887ed82018-08-14 13:52:12 +00001121 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001122}
1123void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1124 // The only read barrier implementation supporting the
1125 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1126 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1127 return;
1128 }
1129
Vladimir Markod887ed82018-08-14 13:52:12 +00001130 CreateIntIntIntIntIntToIntPlusTemps(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001131}
1132void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001133 GenCas(invoke, DataType::Type::kInt32, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001134}
1135void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1136 // The only read barrier implementation supporting the
1137 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1138 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001140 GenCas(invoke, DataType::Type::kReference, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001141}
1142
1143void IntrinsicLocationsBuilderARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1144 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001145 LocationSummary* locations =
1146 new (allocator_) LocationSummary(invoke,
1147 invoke->InputAt(1)->CanBeNull()
1148 ? LocationSummary::kCallOnSlowPath
1149 : LocationSummary::kNoCall,
1150 kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001151 locations->SetInAt(0, Location::RequiresRegister());
1152 locations->SetInAt(1, Location::RequiresRegister());
1153 locations->AddTemp(Location::RequiresRegister());
1154 locations->AddTemp(Location::RequiresRegister());
1155 locations->AddTemp(Location::RequiresRegister());
1156 // Need temporary registers for String compression's feature.
1157 if (mirror::kUseStringCompression) {
1158 locations->AddTemp(Location::RequiresRegister());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001159 }
1160 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1161}
1162
Artem Serov672b9c12017-12-05 18:04:07 +00001163// Forward declaration.
1164//
1165// ART build system imposes a size limit (deviceFrameSizeLimit) on the stack frames generated
1166// by the compiler for every C++ function, and if this function gets inlined in
1167// IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo, the limit will be exceeded, resulting in a
1168// build failure. That is the reason why NO_INLINE attribute is used.
1169static void NO_INLINE GenerateStringCompareToLoop(ArmVIXLAssembler* assembler,
1170 HInvoke* invoke,
1171 vixl32::Label* end,
1172 vixl32::Label* different_compression);
1173
Anton Kirilov5ec62182016-10-13 20:16:02 +01001174void IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1175 ArmVIXLAssembler* assembler = GetAssembler();
1176 LocationSummary* locations = invoke->GetLocations();
1177
Artem Serov672b9c12017-12-05 18:04:07 +00001178 const vixl32::Register str = InputRegisterAt(invoke, 0);
1179 const vixl32::Register arg = InputRegisterAt(invoke, 1);
1180 const vixl32::Register out = OutputRegister(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001181
Artem Serov672b9c12017-12-05 18:04:07 +00001182 const vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1183 const vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1184 const vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001185 vixl32::Register temp3;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001186 if (mirror::kUseStringCompression) {
1187 temp3 = RegisterFrom(locations->GetTemp(3));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001188 }
1189
Anton Kirilov5ec62182016-10-13 20:16:02 +01001190 vixl32::Label end;
1191 vixl32::Label different_compression;
1192
1193 // Get offsets of count and value fields within a string object.
1194 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001195
1196 // Note that the null check must have been done earlier.
1197 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1198
1199 // Take slow path and throw if input can be and is null.
1200 SlowPathCodeARMVIXL* slow_path = nullptr;
1201 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1202 if (can_slow_path) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001203 slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001204 codegen_->AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001205 __ CompareAndBranchIfZero(arg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001206 }
1207
1208 // Reference equality check, return 0 if same reference.
1209 __ Subs(out, str, arg);
1210 __ B(eq, &end);
1211
Anton Kirilov5ec62182016-10-13 20:16:02 +01001212 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001213 // Load `count` fields of this and argument strings.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001214 __ Ldr(temp3, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001215 __ Ldr(temp2, MemOperand(arg, count_offset));
1216 // Extract lengths from the `count` fields.
1217 __ Lsr(temp0, temp3, 1u);
1218 __ Lsr(temp1, temp2, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001219 } else {
1220 // Load lengths of this and argument strings.
1221 __ Ldr(temp0, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001222 __ Ldr(temp1, MemOperand(arg, count_offset));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001223 }
1224 // out = length diff.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001225 __ Subs(out, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001226 // temp0 = min(len(str), len(arg)).
1227
1228 {
Artem Serov0fb37192016-12-06 18:13:40 +00001229 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1230 2 * kMaxInstructionSizeInBytes,
1231 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001232
1233 __ it(gt);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001234 __ mov(gt, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001235 }
1236
Anton Kirilov5ec62182016-10-13 20:16:02 +01001237 // Shorter string is empty?
xueliang.zhongf51bc622016-11-04 09:23:32 +00001238 // Note that mirror::kUseStringCompression==true introduces lots of instructions,
1239 // which makes &end label far away from this branch and makes it not 'CBZ-encodable'.
1240 __ CompareAndBranchIfZero(temp0, &end, mirror::kUseStringCompression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001241
1242 if (mirror::kUseStringCompression) {
1243 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001244 __ Eors(temp2, temp2, temp3);
1245 __ Lsrs(temp2, temp2, 1u);
1246 __ B(cs, &different_compression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001247 // For string compression, calculate the number of bytes to compare (not chars).
1248 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001249 __ Lsls(temp3, temp3, 31u); // Extract purely the compression flag.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001250
Artem Serov0fb37192016-12-06 18:13:40 +00001251 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1252 2 * kMaxInstructionSizeInBytes,
1253 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001254
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001255 __ it(ne);
1256 __ add(ne, temp0, temp0, temp0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001257 }
1258
Artem Serov672b9c12017-12-05 18:04:07 +00001259
1260 GenerateStringCompareToLoop(assembler, invoke, &end, &different_compression);
1261
1262 __ Bind(&end);
1263
1264 if (can_slow_path) {
1265 __ Bind(slow_path->GetExitLabel());
1266 }
1267}
1268
1269static void GenerateStringCompareToLoop(ArmVIXLAssembler* assembler,
1270 HInvoke* invoke,
1271 vixl32::Label* end,
1272 vixl32::Label* different_compression) {
1273 LocationSummary* locations = invoke->GetLocations();
1274
1275 const vixl32::Register str = InputRegisterAt(invoke, 0);
1276 const vixl32::Register arg = InputRegisterAt(invoke, 1);
1277 const vixl32::Register out = OutputRegister(invoke);
1278
1279 const vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1280 const vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1281 const vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
1282 vixl32::Register temp3;
1283 if (mirror::kUseStringCompression) {
1284 temp3 = RegisterFrom(locations->GetTemp(3));
1285 }
1286
1287 vixl32::Label loop;
1288 vixl32::Label find_char_diff;
1289
1290 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001291 // Store offset of string value in preparation for comparison loop.
1292 __ Mov(temp1, value_offset);
1293
Anton Kirilov5ec62182016-10-13 20:16:02 +01001294 // Assertions that must hold in order to compare multiple characters at a time.
1295 CHECK_ALIGNED(value_offset, 8);
1296 static_assert(IsAligned<8>(kObjectAlignment),
1297 "String data must be 8-byte aligned for unrolled CompareTo loop.");
1298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001299 const unsigned char_size = DataType::Size(DataType::Type::kUint16);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001300 DCHECK_EQ(char_size, 2u);
1301
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001302 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1303
Anton Kirilov5ec62182016-10-13 20:16:02 +01001304 vixl32::Label find_char_diff_2nd_cmp;
1305 // Unrolled loop comparing 4x16-bit chars per iteration (ok because of string data alignment).
1306 __ Bind(&loop);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001307 vixl32::Register temp_reg = temps.Acquire();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001308 __ Ldr(temp_reg, MemOperand(str, temp1));
1309 __ Ldr(temp2, MemOperand(arg, temp1));
1310 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001311 __ B(ne, &find_char_diff, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001312 __ Add(temp1, temp1, char_size * 2);
1313
1314 __ Ldr(temp_reg, MemOperand(str, temp1));
1315 __ Ldr(temp2, MemOperand(arg, temp1));
1316 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001317 __ B(ne, &find_char_diff_2nd_cmp, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001318 __ Add(temp1, temp1, char_size * 2);
1319 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1320 __ Subs(temp0, temp0, (mirror::kUseStringCompression ? 8 : 4));
Artem Serov517d9f62016-12-12 15:51:15 +00001321 __ B(hi, &loop, /* far_target */ false);
Artem Serov672b9c12017-12-05 18:04:07 +00001322 __ B(end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001323
1324 __ Bind(&find_char_diff_2nd_cmp);
1325 if (mirror::kUseStringCompression) {
1326 __ Subs(temp0, temp0, 4); // 4 bytes previously compared.
Artem Serov672b9c12017-12-05 18:04:07 +00001327 __ B(ls, end, /* far_target */ false); // Was the second comparison fully beyond the end?
Anton Kirilov5ec62182016-10-13 20:16:02 +01001328 } else {
1329 // Without string compression, we can start treating temp0 as signed
1330 // and rely on the signed comparison below.
1331 __ Sub(temp0, temp0, 2);
1332 }
1333
1334 // Find the single character difference.
1335 __ Bind(&find_char_diff);
1336 // Get the bit position of the first character that differs.
1337 __ Eor(temp1, temp2, temp_reg);
1338 __ Rbit(temp1, temp1);
1339 __ Clz(temp1, temp1);
1340
1341 // temp0 = number of characters remaining to compare.
1342 // (Without string compression, it could be < 1 if a difference is found by the second CMP
1343 // in the comparison loop, and after the end of the shorter string data).
1344
1345 // Without string compression (temp1 >> 4) = character where difference occurs between the last
1346 // two words compared, in the interval [0,1].
1347 // (0 for low half-word different, 1 for high half-word different).
1348 // With string compression, (temp1 << 3) = byte where the difference occurs,
1349 // in the interval [0,3].
1350
1351 // If temp0 <= (temp1 >> (kUseStringCompression ? 3 : 4)), the difference occurs outside
1352 // the remaining string data, so just return length diff (out).
1353 // The comparison is unsigned for string compression, otherwise signed.
1354 __ Cmp(temp0, Operand(temp1, vixl32::LSR, (mirror::kUseStringCompression ? 3 : 4)));
Artem Serov672b9c12017-12-05 18:04:07 +00001355 __ B((mirror::kUseStringCompression ? ls : le), end, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001356
Anton Kirilov5ec62182016-10-13 20:16:02 +01001357 // Extract the characters and calculate the difference.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001358 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001359 // For compressed strings we need to clear 0x7 from temp1, for uncompressed we need to clear
1360 // 0xf. We also need to prepare the character extraction mask `uncompressed ? 0xffffu : 0xffu`.
1361 // The compression flag is now in the highest bit of temp3, so let's play some tricks.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001362 __ Orr(temp3, temp3, 0xffu << 23); // uncompressed ? 0xff800000u : 0x7ff80000u
1363 __ Bic(temp1, temp1, Operand(temp3, vixl32::LSR, 31 - 3)); // &= ~(uncompressed ? 0xfu : 0x7u)
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001364 __ Asr(temp3, temp3, 7u); // uncompressed ? 0xffff0000u : 0xff0000u.
1365 __ Lsr(temp2, temp2, temp1); // Extract second character.
1366 __ Lsr(temp3, temp3, 16u); // uncompressed ? 0xffffu : 0xffu
1367 __ Lsr(out, temp_reg, temp1); // Extract first character.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001368 __ And(temp2, temp2, temp3);
1369 __ And(out, out, temp3);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001370 } else {
Anton Kirilovb88c4842016-11-14 14:37:00 +00001371 __ Bic(temp1, temp1, 0xf);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001372 __ Lsr(temp2, temp2, temp1);
1373 __ Lsr(out, temp_reg, temp1);
Anton Kirilovb88c4842016-11-14 14:37:00 +00001374 __ Movt(temp2, 0);
1375 __ Movt(out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001376 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001377
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001378 __ Sub(out, out, temp2);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001379 temps.Release(temp_reg);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001380
1381 if (mirror::kUseStringCompression) {
Artem Serov672b9c12017-12-05 18:04:07 +00001382 __ B(end);
1383 __ Bind(different_compression);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001384
1385 // Comparison for different compression style.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001386 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001387 DCHECK_EQ(c_char_size, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001388
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001389 // We want to free up the temp3, currently holding `str.count`, for comparison.
1390 // So, we move it to the bottom bit of the iteration count `temp0` which we tnen
1391 // need to treat as unsigned. Start by freeing the bit with an ADD and continue
1392 // further down by a LSRS+SBC which will flip the meaning of the flag but allow
1393 // `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001394 __ Add(temp0, temp0, temp0); // Unlike LSL, this ADD is always 16-bit.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001395 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001396 __ Mov(temp1, str);
1397 __ Mov(temp2, arg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001398 __ Lsrs(temp3, temp3, 1u); // Continue the move of the compression flag.
1399 {
Artem Serov0fb37192016-12-06 18:13:40 +00001400 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1401 3 * kMaxInstructionSizeInBytes,
1402 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001403 __ itt(cs); // Interleave with selection of temp1 and temp2.
1404 __ mov(cs, temp1, arg); // Preserves flags.
1405 __ mov(cs, temp2, str); // Preserves flags.
1406 }
Anton Kirilovb88c4842016-11-14 14:37:00 +00001407 __ Sbc(temp0, temp0, 0); // Complete the move of the compression flag.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001408
1409 // Adjust temp1 and temp2 from string pointers to data pointers.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001410 __ Add(temp1, temp1, value_offset);
1411 __ Add(temp2, temp2, value_offset);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001412
1413 vixl32::Label different_compression_loop;
1414 vixl32::Label different_compression_diff;
1415
1416 // Main loop for different compression.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001417 temp_reg = temps.Acquire();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001418 __ Bind(&different_compression_loop);
1419 __ Ldrb(temp_reg, MemOperand(temp1, c_char_size, PostIndex));
1420 __ Ldrh(temp3, MemOperand(temp2, char_size, PostIndex));
Anton Kirilovb88c4842016-11-14 14:37:00 +00001421 __ Cmp(temp_reg, temp3);
Artem Serov517d9f62016-12-12 15:51:15 +00001422 __ B(ne, &different_compression_diff, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001423 __ Subs(temp0, temp0, 2);
Artem Serov517d9f62016-12-12 15:51:15 +00001424 __ B(hi, &different_compression_loop, /* far_target */ false);
Artem Serov672b9c12017-12-05 18:04:07 +00001425 __ B(end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001426
1427 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001428 __ Bind(&different_compression_diff);
1429 __ Sub(out, temp_reg, temp3);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001430 temps.Release(temp_reg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001431 // Flip the difference if the `arg` is compressed.
1432 // `temp0` contains inverted `str` compression flag, i.e the same as `arg` compression flag.
1433 __ Lsrs(temp0, temp0, 1u);
1434 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1435 "Expecting 0=compressed, 1=uncompressed");
1436
Artem Serov0fb37192016-12-06 18:13:40 +00001437 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1438 2 * kMaxInstructionSizeInBytes,
1439 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001440 __ it(cc);
1441 __ rsb(cc, out, out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001442 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001443}
1444
Vladimir Marko984519c2017-08-23 10:45:29 +01001445// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1446// The normal loop plus the pre-header is 9 instructions (18-26 bytes) without string compression
1447// and 12 instructions (24-32 bytes) with string compression. We can compare up to 4 bytes in 4
1448// instructions (LDR+LDR+CMP+BNE) and up to 8 bytes in 6 instructions (LDRD+LDRD+CMP+BNE+CMP+BNE).
1449// Allow up to 12 instructions (32 bytes) for the unrolled loop.
1450constexpr size_t kShortConstStringEqualsCutoffInBytes = 16;
1451
1452static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1453 if (candidate->IsLoadString()) {
1454 HLoadString* load_string = candidate->AsLoadString();
1455 const DexFile& dex_file = load_string->GetDexFile();
1456 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1457 }
1458 return nullptr;
1459}
1460
Anton Kirilov5ec62182016-10-13 20:16:02 +01001461void IntrinsicLocationsBuilderARMVIXL::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001462 LocationSummary* locations =
1463 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001464 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1465 locations->SetInAt(0, Location::RequiresRegister());
1466 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko984519c2017-08-23 10:45:29 +01001467
Anton Kirilov5ec62182016-10-13 20:16:02 +01001468 // Temporary registers to store lengths of strings and for calculations.
1469 // Using instruction cbz requires a low register, so explicitly set a temp to be R0.
1470 locations->AddTemp(LocationFrom(r0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001471
Vladimir Marko984519c2017-08-23 10:45:29 +01001472 // For the generic implementation and for long const strings we need an extra temporary.
1473 // We do not need it for short const strings, up to 4 bytes, see code generation below.
1474 uint32_t const_string_length = 0u;
1475 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1476 if (const_string == nullptr) {
1477 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1478 }
1479 bool is_compressed =
1480 mirror::kUseStringCompression &&
1481 const_string != nullptr &&
1482 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1483 if (const_string == nullptr || const_string_length > (is_compressed ? 4u : 2u)) {
1484 locations->AddTemp(Location::RequiresRegister());
1485 }
1486
1487 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1488 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1489 // Then we shall need an extra temporary register instead of the output register.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001490 locations->SetOut(Location::RequiresRegister());
1491}
1492
1493void IntrinsicCodeGeneratorARMVIXL::VisitStringEquals(HInvoke* invoke) {
1494 ArmVIXLAssembler* assembler = GetAssembler();
1495 LocationSummary* locations = invoke->GetLocations();
1496
1497 vixl32::Register str = InputRegisterAt(invoke, 0);
1498 vixl32::Register arg = InputRegisterAt(invoke, 1);
1499 vixl32::Register out = OutputRegister(invoke);
1500
1501 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001502
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001503 vixl32::Label loop;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001504 vixl32::Label end;
1505 vixl32::Label return_true;
1506 vixl32::Label return_false;
Anton Kirilov6f644202017-02-27 18:29:45 +00001507 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001508
1509 // Get offsets of count, value, and class fields within a string object.
1510 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1511 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1512 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1513
1514 // Note that the null check must have been done earlier.
1515 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1516
1517 StringEqualsOptimizations optimizations(invoke);
1518 if (!optimizations.GetArgumentNotNull()) {
1519 // Check if input is null, return false if it is.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001520 __ CompareAndBranchIfZero(arg, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001521 }
1522
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001523 // Reference equality check, return true if same reference.
1524 __ Cmp(str, arg);
Artem Serov517d9f62016-12-12 15:51:15 +00001525 __ B(eq, &return_true, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001526
Anton Kirilov5ec62182016-10-13 20:16:02 +01001527 if (!optimizations.GetArgumentIsString()) {
1528 // Instanceof check for the argument by comparing class fields.
1529 // All string objects must have the same type since String cannot be subclassed.
1530 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1531 // If the argument is a string object, its class field must be equal to receiver's class field.
1532 __ Ldr(temp, MemOperand(str, class_offset));
Vladimir Marko984519c2017-08-23 10:45:29 +01001533 __ Ldr(out, MemOperand(arg, class_offset));
1534 __ Cmp(temp, out);
Artem Serov517d9f62016-12-12 15:51:15 +00001535 __ B(ne, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001536 }
1537
Vladimir Marko984519c2017-08-23 10:45:29 +01001538 // Check if one of the inputs is a const string. Do not special-case both strings
1539 // being const, such cases should be handled by constant folding if needed.
1540 uint32_t const_string_length = 0u;
1541 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1542 if (const_string == nullptr) {
1543 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1544 if (const_string != nullptr) {
1545 std::swap(str, arg); // Make sure the const string is in `str`.
1546 }
1547 }
1548 bool is_compressed =
1549 mirror::kUseStringCompression &&
1550 const_string != nullptr &&
1551 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1552
1553 if (const_string != nullptr) {
1554 // Load `count` field of the argument string and check if it matches the const string.
1555 // Also compares the compression style, if differs return false.
1556 __ Ldr(temp, MemOperand(arg, count_offset));
1557 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
1558 __ B(ne, &return_false, /* far_target */ false);
1559 } else {
1560 // Load `count` fields of this and argument strings.
1561 __ Ldr(temp, MemOperand(str, count_offset));
1562 __ Ldr(out, MemOperand(arg, count_offset));
1563 // Check if `count` fields are equal, return false if they're not.
1564 // Also compares the compression style, if differs return false.
1565 __ Cmp(temp, out);
1566 __ B(ne, &return_false, /* far_target */ false);
1567 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001568
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001569 // Assertions that must hold in order to compare strings 4 bytes at a time.
Vladimir Marko984519c2017-08-23 10:45:29 +01001570 // Ok to do this because strings are zero-padded to kObjectAlignment.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001571 DCHECK_ALIGNED(value_offset, 4);
1572 static_assert(IsAligned<4>(kObjectAlignment), "String data must be aligned for fast compare.");
1573
Vladimir Marko984519c2017-08-23 10:45:29 +01001574 if (const_string != nullptr &&
1575 const_string_length <= (is_compressed ? kShortConstStringEqualsCutoffInBytes
1576 : kShortConstStringEqualsCutoffInBytes / 2u)) {
1577 // Load and compare the contents. Though we know the contents of the short const string
1578 // at compile time, materializing constants may be more code than loading from memory.
1579 int32_t offset = value_offset;
1580 size_t remaining_bytes =
1581 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 4u);
1582 while (remaining_bytes > sizeof(uint32_t)) {
1583 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1584 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1585 vixl32::Register temp2 = scratch_scope.Acquire();
1586 __ Ldrd(temp, temp1, MemOperand(str, offset));
1587 __ Ldrd(temp2, out, MemOperand(arg, offset));
1588 __ Cmp(temp, temp2);
1589 __ B(ne, &return_false, /* far_label */ false);
1590 __ Cmp(temp1, out);
1591 __ B(ne, &return_false, /* far_label */ false);
1592 offset += 2u * sizeof(uint32_t);
1593 remaining_bytes -= 2u * sizeof(uint32_t);
1594 }
1595 if (remaining_bytes != 0u) {
1596 __ Ldr(temp, MemOperand(str, offset));
1597 __ Ldr(out, MemOperand(arg, offset));
1598 __ Cmp(temp, out);
1599 __ B(ne, &return_false, /* far_label */ false);
1600 }
1601 } else {
1602 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1603 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1604 "Expecting 0=compressed, 1=uncompressed");
1605 __ CompareAndBranchIfZero(temp, &return_true, /* far_target */ false);
1606
1607 if (mirror::kUseStringCompression) {
1608 // For string compression, calculate the number of bytes to compare (not chars).
1609 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1610 __ Lsrs(temp, temp, 1u); // Extract length and check compression flag.
1611 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1612 2 * kMaxInstructionSizeInBytes,
1613 CodeBufferCheckScope::kMaximumSize);
1614 __ it(cs); // If uncompressed,
1615 __ add(cs, temp, temp, temp); // double the byte count.
1616 }
1617
1618 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1619 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1620 vixl32::Register temp2 = scratch_scope.Acquire();
1621
1622 // Store offset of string value in preparation for comparison loop.
1623 __ Mov(temp1, value_offset);
1624
1625 // Loop to compare strings 4 bytes at a time starting at the front of the string.
1626 __ Bind(&loop);
1627 __ Ldr(out, MemOperand(str, temp1));
1628 __ Ldr(temp2, MemOperand(arg, temp1));
1629 __ Add(temp1, temp1, Operand::From(sizeof(uint32_t)));
1630 __ Cmp(out, temp2);
1631 __ B(ne, &return_false, /* far_target */ false);
1632 // With string compression, we have compared 4 bytes, otherwise 2 chars.
1633 __ Subs(temp, temp, mirror::kUseStringCompression ? 4 : 2);
1634 __ B(hi, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001635 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001636
Anton Kirilov5ec62182016-10-13 20:16:02 +01001637 // Return true and exit the function.
1638 // If loop does not result in returning false, we return true.
1639 __ Bind(&return_true);
1640 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00001641 __ B(final_label);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001642
1643 // Return false and exit the function.
1644 __ Bind(&return_false);
1645 __ Mov(out, 0);
Anton Kirilov6f644202017-02-27 18:29:45 +00001646
1647 if (end.IsReferenced()) {
1648 __ Bind(&end);
1649 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001650}
1651
1652static void GenerateVisitStringIndexOf(HInvoke* invoke,
1653 ArmVIXLAssembler* assembler,
1654 CodeGeneratorARMVIXL* codegen,
Anton Kirilov5ec62182016-10-13 20:16:02 +01001655 bool start_at_zero) {
1656 LocationSummary* locations = invoke->GetLocations();
1657
1658 // Note that the null check must have been done earlier.
1659 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1660
1661 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1662 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
1663 SlowPathCodeARMVIXL* slow_path = nullptr;
1664 HInstruction* code_point = invoke->InputAt(1);
1665 if (code_point->IsIntConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001666 if (static_cast<uint32_t>(Int32ConstantFrom(code_point)) >
Anton Kirilov5ec62182016-10-13 20:16:02 +01001667 std::numeric_limits<uint16_t>::max()) {
1668 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1669 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001670 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001671 codegen->AddSlowPath(slow_path);
1672 __ B(slow_path->GetEntryLabel());
1673 __ Bind(slow_path->GetExitLabel());
1674 return;
1675 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 } else if (code_point->GetType() != DataType::Type::kUint16) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01001677 vixl32::Register char_reg = InputRegisterAt(invoke, 1);
1678 // 0xffff is not modified immediate but 0x10000 is, so use `>= 0x10000` instead of `> 0xffff`.
1679 __ Cmp(char_reg, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()) + 1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001680 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001681 codegen->AddSlowPath(slow_path);
1682 __ B(hs, slow_path->GetEntryLabel());
1683 }
1684
1685 if (start_at_zero) {
1686 vixl32::Register tmp_reg = RegisterFrom(locations->GetTemp(0));
1687 DCHECK(tmp_reg.Is(r2));
1688 // Start-index = 0.
1689 __ Mov(tmp_reg, 0);
1690 }
1691
1692 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
1693 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
1694
1695 if (slow_path != nullptr) {
1696 __ Bind(slow_path->GetExitLabel());
1697 }
1698}
1699
1700void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001701 LocationSummary* locations = new (allocator_) LocationSummary(
1702 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001703 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1704 // best to align the inputs accordingly.
1705 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1706 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1707 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1708 locations->SetOut(LocationFrom(r0));
1709
1710 // Need to send start-index=0.
1711 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1712}
1713
1714void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001715 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001716}
1717
1718void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001719 LocationSummary* locations = new (allocator_) LocationSummary(
1720 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001721 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1722 // best to align the inputs accordingly.
1723 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1724 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1725 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1726 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1727 locations->SetOut(LocationFrom(r0));
1728}
1729
1730void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001731 GenerateVisitStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001732}
1733
1734void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001735 LocationSummary* locations = new (allocator_) LocationSummary(
1736 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001737 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1738 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1739 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1740 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1741 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1742 locations->SetOut(LocationFrom(r0));
1743}
1744
1745void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
1746 ArmVIXLAssembler* assembler = GetAssembler();
1747 vixl32::Register byte_array = InputRegisterAt(invoke, 0);
1748 __ Cmp(byte_array, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001749 SlowPathCodeARMVIXL* slow_path =
1750 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001751 codegen_->AddSlowPath(slow_path);
1752 __ B(eq, slow_path->GetEntryLabel());
1753
1754 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
1755 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
1756 __ Bind(slow_path->GetExitLabel());
1757}
1758
1759void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001760 LocationSummary* locations =
1761 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001762 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1763 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1764 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1765 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1766 locations->SetOut(LocationFrom(r0));
1767}
1768
1769void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
1770 // No need to emit code checking whether `locations->InAt(2)` is a null
1771 // pointer, as callers of the native method
1772 //
1773 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1774 //
1775 // all include a null check on `data` before calling that method.
1776 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
1777 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
1778}
1779
1780void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001781 LocationSummary* locations = new (allocator_) LocationSummary(
1782 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001783 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1784 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1785 locations->SetOut(LocationFrom(r0));
1786}
1787
1788void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
1789 ArmVIXLAssembler* assembler = GetAssembler();
1790 vixl32::Register string_to_copy = InputRegisterAt(invoke, 0);
1791 __ Cmp(string_to_copy, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001792 SlowPathCodeARMVIXL* slow_path =
1793 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001794 codegen_->AddSlowPath(slow_path);
1795 __ B(eq, slow_path->GetEntryLabel());
1796
1797 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
1798 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
1799
1800 __ Bind(slow_path->GetExitLabel());
1801}
1802
1803void IntrinsicLocationsBuilderARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
1804 // The only read barrier implementation supporting the
1805 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1806 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1807 return;
1808 }
1809
1810 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
1811 LocationSummary* locations = invoke->GetLocations();
1812 if (locations == nullptr) {
1813 return;
1814 }
1815
1816 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1817 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1818 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1819
1820 if (src_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(src_pos->GetValue())) {
1821 locations->SetInAt(1, Location::RequiresRegister());
1822 }
1823 if (dest_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(dest_pos->GetValue())) {
1824 locations->SetInAt(3, Location::RequiresRegister());
1825 }
1826 if (length != nullptr && !assembler_->ShifterOperandCanAlwaysHold(length->GetValue())) {
1827 locations->SetInAt(4, Location::RequiresRegister());
1828 }
1829 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1830 // Temporary register IP cannot be used in
1831 // ReadBarrierSystemArrayCopySlowPathARM (because that register
1832 // is clobbered by ReadBarrierMarkRegX entry points). Get an extra
1833 // temporary register from the register allocator.
1834 locations->AddTemp(Location::RequiresRegister());
1835 }
1836}
1837
1838static void CheckPosition(ArmVIXLAssembler* assembler,
1839 Location pos,
1840 vixl32::Register input,
1841 Location length,
1842 SlowPathCodeARMVIXL* slow_path,
1843 vixl32::Register temp,
1844 bool length_is_input_length = false) {
1845 // Where is the length in the Array?
1846 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1847
1848 if (pos.IsConstant()) {
1849 int32_t pos_const = Int32ConstantFrom(pos);
1850 if (pos_const == 0) {
1851 if (!length_is_input_length) {
1852 // Check that length(input) >= length.
1853 __ Ldr(temp, MemOperand(input, length_offset));
1854 if (length.IsConstant()) {
1855 __ Cmp(temp, Int32ConstantFrom(length));
1856 } else {
1857 __ Cmp(temp, RegisterFrom(length));
1858 }
1859 __ B(lt, slow_path->GetEntryLabel());
1860 }
1861 } else {
1862 // Check that length(input) >= pos.
1863 __ Ldr(temp, MemOperand(input, length_offset));
1864 __ Subs(temp, temp, pos_const);
1865 __ B(lt, slow_path->GetEntryLabel());
1866
1867 // Check that (length(input) - pos) >= length.
1868 if (length.IsConstant()) {
1869 __ Cmp(temp, Int32ConstantFrom(length));
1870 } else {
1871 __ Cmp(temp, RegisterFrom(length));
1872 }
1873 __ B(lt, slow_path->GetEntryLabel());
1874 }
1875 } else if (length_is_input_length) {
1876 // The only way the copy can succeed is if pos is zero.
1877 vixl32::Register pos_reg = RegisterFrom(pos);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001878 __ CompareAndBranchIfNonZero(pos_reg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001879 } else {
1880 // Check that pos >= 0.
1881 vixl32::Register pos_reg = RegisterFrom(pos);
1882 __ Cmp(pos_reg, 0);
1883 __ B(lt, slow_path->GetEntryLabel());
1884
1885 // Check that pos <= length(input).
1886 __ Ldr(temp, MemOperand(input, length_offset));
1887 __ Subs(temp, temp, pos_reg);
1888 __ B(lt, slow_path->GetEntryLabel());
1889
1890 // Check that (length(input) - pos) >= length.
1891 if (length.IsConstant()) {
1892 __ Cmp(temp, Int32ConstantFrom(length));
1893 } else {
1894 __ Cmp(temp, RegisterFrom(length));
1895 }
1896 __ B(lt, slow_path->GetEntryLabel());
1897 }
1898}
1899
1900void IntrinsicCodeGeneratorARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
1901 // The only read barrier implementation supporting the
1902 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1903 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1904
1905 ArmVIXLAssembler* assembler = GetAssembler();
1906 LocationSummary* locations = invoke->GetLocations();
1907
1908 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1909 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1910 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1911 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1912 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1913
1914 vixl32::Register src = InputRegisterAt(invoke, 0);
1915 Location src_pos = locations->InAt(1);
1916 vixl32::Register dest = InputRegisterAt(invoke, 2);
1917 Location dest_pos = locations->InAt(3);
1918 Location length = locations->InAt(4);
1919 Location temp1_loc = locations->GetTemp(0);
1920 vixl32::Register temp1 = RegisterFrom(temp1_loc);
1921 Location temp2_loc = locations->GetTemp(1);
1922 vixl32::Register temp2 = RegisterFrom(temp2_loc);
1923 Location temp3_loc = locations->GetTemp(2);
1924 vixl32::Register temp3 = RegisterFrom(temp3_loc);
1925
Vladimir Marko174b2e22017-10-12 13:34:49 +01001926 SlowPathCodeARMVIXL* intrinsic_slow_path =
1927 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARMVIXL(invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001928 codegen_->AddSlowPath(intrinsic_slow_path);
1929
1930 vixl32::Label conditions_on_positions_validated;
1931 SystemArrayCopyOptimizations optimizations(invoke);
1932
1933 // If source and destination are the same, we go to slow path if we need to do
1934 // forward copying.
1935 if (src_pos.IsConstant()) {
1936 int32_t src_pos_constant = Int32ConstantFrom(src_pos);
1937 if (dest_pos.IsConstant()) {
1938 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
1939 if (optimizations.GetDestinationIsSource()) {
1940 // Checked when building locations.
1941 DCHECK_GE(src_pos_constant, dest_pos_constant);
1942 } else if (src_pos_constant < dest_pos_constant) {
1943 __ Cmp(src, dest);
1944 __ B(eq, intrinsic_slow_path->GetEntryLabel());
1945 }
1946
1947 // Checked when building locations.
1948 DCHECK(!optimizations.GetDestinationIsSource()
1949 || (src_pos_constant >= Int32ConstantFrom(dest_pos)));
1950 } else {
1951 if (!optimizations.GetDestinationIsSource()) {
1952 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00001953 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001954 }
1955 __ Cmp(RegisterFrom(dest_pos), src_pos_constant);
1956 __ B(gt, intrinsic_slow_path->GetEntryLabel());
1957 }
1958 } else {
1959 if (!optimizations.GetDestinationIsSource()) {
1960 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00001961 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001962 }
1963 if (dest_pos.IsConstant()) {
1964 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
1965 __ Cmp(RegisterFrom(src_pos), dest_pos_constant);
1966 } else {
1967 __ Cmp(RegisterFrom(src_pos), RegisterFrom(dest_pos));
1968 }
1969 __ B(lt, intrinsic_slow_path->GetEntryLabel());
1970 }
1971
1972 __ Bind(&conditions_on_positions_validated);
1973
1974 if (!optimizations.GetSourceIsNotNull()) {
1975 // Bail out if the source is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001976 __ CompareAndBranchIfZero(src, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001977 }
1978
1979 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1980 // Bail out if the destination is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001981 __ CompareAndBranchIfZero(dest, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001982 }
1983
1984 // If the length is negative, bail out.
1985 // We have already checked in the LocationsBuilder for the constant case.
1986 if (!length.IsConstant() &&
1987 !optimizations.GetCountIsSourceLength() &&
1988 !optimizations.GetCountIsDestinationLength()) {
1989 __ Cmp(RegisterFrom(length), 0);
1990 __ B(lt, intrinsic_slow_path->GetEntryLabel());
1991 }
1992
1993 // Validity checks: source.
1994 CheckPosition(assembler,
1995 src_pos,
1996 src,
1997 length,
1998 intrinsic_slow_path,
1999 temp1,
2000 optimizations.GetCountIsSourceLength());
2001
2002 // Validity checks: dest.
2003 CheckPosition(assembler,
2004 dest_pos,
2005 dest,
2006 length,
2007 intrinsic_slow_path,
2008 temp1,
2009 optimizations.GetCountIsDestinationLength());
2010
2011 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2012 // Check whether all elements of the source array are assignable to the component
2013 // type of the destination array. We do two checks: the classes are the same,
2014 // or the destination is Object[]. If none of these checks succeed, we go to the
2015 // slow path.
2016
2017 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2018 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2019 // /* HeapReference<Class> */ temp1 = src->klass_
2020 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2021 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2022 // Bail out if the source is not a non primitive array.
2023 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2024 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2025 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002026 __ CompareAndBranchIfZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002027 // If heap poisoning is enabled, `temp1` has been unpoisoned
2028 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2029 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2030 __ Ldrh(temp1, MemOperand(temp1, primitive_offset));
2031 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002032 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002033 }
2034
2035 // /* HeapReference<Class> */ temp1 = dest->klass_
2036 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2037 invoke, temp1_loc, dest, class_offset, temp2_loc, /* needs_null_check */ false);
2038
2039 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2040 // Bail out if the destination is not a non primitive array.
2041 //
2042 // Register `temp1` is not trashed by the read barrier emitted
2043 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2044 // method produces a call to a ReadBarrierMarkRegX entry point,
2045 // which saves all potentially live registers, including
2046 // temporaries such a `temp1`.
2047 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2048 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2049 invoke, temp2_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002050 __ CompareAndBranchIfZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002051 // If heap poisoning is enabled, `temp2` has been unpoisoned
2052 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2053 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2054 __ Ldrh(temp2, MemOperand(temp2, primitive_offset));
2055 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002056 __ CompareAndBranchIfNonZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002057 }
2058
2059 // For the same reason given earlier, `temp1` is not trashed by the
2060 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2061 // /* HeapReference<Class> */ temp2 = src->klass_
2062 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2063 invoke, temp2_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
2064 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2065 __ Cmp(temp1, temp2);
2066
2067 if (optimizations.GetDestinationIsTypedObjectArray()) {
2068 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002069 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002070 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2071 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2072 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
2073 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2074 // We do not need to emit a read barrier for the following
2075 // heap reference load, as `temp1` is only used in a
2076 // comparison with null below, and this reference is not
2077 // kept afterwards.
2078 __ Ldr(temp1, MemOperand(temp1, super_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002079 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002080 __ Bind(&do_copy);
2081 } else {
2082 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2083 }
2084 } else {
2085 // Non read barrier code.
2086
2087 // /* HeapReference<Class> */ temp1 = dest->klass_
2088 __ Ldr(temp1, MemOperand(dest, class_offset));
2089 // /* HeapReference<Class> */ temp2 = src->klass_
2090 __ Ldr(temp2, MemOperand(src, class_offset));
2091 bool did_unpoison = false;
2092 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2093 !optimizations.GetSourceIsNonPrimitiveArray()) {
2094 // One or two of the references need to be unpoisoned. Unpoison them
2095 // both to make the identity check valid.
2096 assembler->MaybeUnpoisonHeapReference(temp1);
2097 assembler->MaybeUnpoisonHeapReference(temp2);
2098 did_unpoison = true;
2099 }
2100
2101 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2102 // Bail out if the destination is not a non primitive array.
2103 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2104 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002105 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002106 assembler->MaybeUnpoisonHeapReference(temp3);
2107 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2108 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2109 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002110 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002111 }
2112
2113 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2114 // Bail out if the source is not a non primitive array.
2115 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2116 __ Ldr(temp3, MemOperand(temp2, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002117 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002118 assembler->MaybeUnpoisonHeapReference(temp3);
2119 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2120 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2121 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002122 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002123 }
2124
2125 __ Cmp(temp1, temp2);
2126
2127 if (optimizations.GetDestinationIsTypedObjectArray()) {
2128 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002129 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002130 if (!did_unpoison) {
2131 assembler->MaybeUnpoisonHeapReference(temp1);
2132 }
2133 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2134 __ Ldr(temp1, MemOperand(temp1, component_offset));
2135 assembler->MaybeUnpoisonHeapReference(temp1);
2136 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2137 __ Ldr(temp1, MemOperand(temp1, super_offset));
2138 // No need to unpoison the result, we're comparing against null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002139 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002140 __ Bind(&do_copy);
2141 } else {
2142 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2143 }
2144 }
2145 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2146 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2147 // Bail out if the source is not a non primitive array.
2148 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2149 // /* HeapReference<Class> */ temp1 = src->klass_
2150 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2151 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2152 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2153 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2154 invoke, temp3_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002155 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002156 // If heap poisoning is enabled, `temp3` has been unpoisoned
2157 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2158 } else {
2159 // /* HeapReference<Class> */ temp1 = src->klass_
2160 __ Ldr(temp1, MemOperand(src, class_offset));
2161 assembler->MaybeUnpoisonHeapReference(temp1);
2162 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2163 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002164 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002165 assembler->MaybeUnpoisonHeapReference(temp3);
2166 }
2167 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2168 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2169 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002170 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002171 }
2172
Roland Levillain1663d162017-03-17 15:15:21 +00002173 if (length.IsConstant() && Int32ConstantFrom(length) == 0) {
2174 // Null constant length: not need to emit the loop code at all.
Anton Kirilov5ec62182016-10-13 20:16:02 +01002175 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002176 vixl32::Label done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002177 const DataType::Type type = DataType::Type::kReference;
2178 const int32_t element_size = DataType::Size(type);
Roland Levillain1663d162017-03-17 15:15:21 +00002179
2180 if (length.IsRegister()) {
2181 // Don't enter the copy loop if the length is null.
2182 __ CompareAndBranchIfZero(RegisterFrom(length), &done, /* is_far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002183 }
Roland Levillain1663d162017-03-17 15:15:21 +00002184
2185 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2186 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2187
2188 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002189 // also CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002190 //
2191 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2192 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2193 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2194 // if (is_gray) {
2195 // // Slow-path copy.
2196 // do {
2197 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2198 // } while (src_ptr != end_ptr)
2199 // } else {
2200 // // Fast-path copy.
2201 // do {
2202 // *dest_ptr++ = *src_ptr++;
2203 // } while (src_ptr != end_ptr)
2204 // }
2205
2206 // /* int32_t */ monitor = src->monitor_
2207 __ Ldr(temp2, MemOperand(src, monitor_offset));
2208 // /* LockWord */ lock_word = LockWord(monitor)
2209 static_assert(sizeof(LockWord) == sizeof(int32_t),
2210 "art::LockWord and int32_t have different sizes.");
2211
2212 // Introduce a dependency on the lock_word including the rb_state,
2213 // which shall prevent load-load reordering without using
2214 // a memory barrier (which would be more expensive).
2215 // `src` is unchanged by this operation, but its value now depends
2216 // on `temp2`.
2217 __ Add(src, src, Operand(temp2, vixl32::LSR, 32));
2218
2219 // Compute the base source address in `temp1`.
2220 // Note that `temp1` (the base source address) is computed from
2221 // `src` (and `src_pos`) here, and thus honors the artificial
2222 // dependency of `src` on `temp2`.
2223 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2224 // Compute the end source address in `temp3`.
2225 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2226 // The base destination address is computed later, as `temp2` is
2227 // used for intermediate computations.
2228
2229 // Slow path used to copy array when `src` is gray.
2230 // Note that the base destination address is computed in `temp2`
2231 // by the slow path code.
2232 SlowPathCodeARMVIXL* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002233 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathARMVIXL(invoke);
Roland Levillain1663d162017-03-17 15:15:21 +00002234 codegen_->AddSlowPath(read_barrier_slow_path);
2235
2236 // Given the numeric representation, it's enough to check the low bit of the
2237 // rb_state. We do that by shifting the bit out of the lock word with LSRS
2238 // which can be a 16-bit instruction unlike the TST immediate.
Roland Levillain14e5a292018-06-28 12:00:56 +01002239 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Roland Levillain1663d162017-03-17 15:15:21 +00002240 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2241 __ Lsrs(temp2, temp2, LockWord::kReadBarrierStateShift + 1);
2242 // Carry flag is the last bit shifted out by LSRS.
2243 __ B(cs, read_barrier_slow_path->GetEntryLabel());
2244
2245 // Fast-path copy.
2246 // Compute the base destination address in `temp2`.
2247 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2248 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2249 // poison/unpoison.
2250 vixl32::Label loop;
2251 __ Bind(&loop);
2252 {
2253 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2254 const vixl32::Register temp_reg = temps.Acquire();
2255 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2256 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2257 }
2258 __ Cmp(temp1, temp3);
2259 __ B(ne, &loop, /* far_target */ false);
2260
2261 __ Bind(read_barrier_slow_path->GetExitLabel());
2262 } else {
2263 // Non read barrier code.
2264 // Compute the base source address in `temp1`.
2265 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2266 // Compute the base destination address in `temp2`.
2267 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2268 // Compute the end source address in `temp3`.
2269 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2270 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2271 // poison/unpoison.
2272 vixl32::Label loop;
2273 __ Bind(&loop);
2274 {
2275 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2276 const vixl32::Register temp_reg = temps.Acquire();
2277 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2278 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2279 }
2280 __ Cmp(temp1, temp3);
2281 __ B(ne, &loop, /* far_target */ false);
2282 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002283 __ Bind(&done);
2284 }
2285
2286 // We only need one card marking on the destination array.
2287 codegen_->MarkGCCard(temp1, temp2, dest, NoReg, /* value_can_be_null */ false);
2288
2289 __ Bind(intrinsic_slow_path->GetExitLabel());
2290}
2291
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002293 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2294 // the code generator. Furthermore, the register allocator creates fixed live intervals
2295 // for all caller-saved registers because we are doing a function call. As a result, if
2296 // the input and output locations are unallocated, the register allocator runs out of
2297 // registers and fails; however, a debuggable graph is not the common case.
2298 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2299 return;
2300 }
2301
2302 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 DCHECK_EQ(invoke->InputAt(0)->GetType(), DataType::Type::kFloat64);
2304 DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002305
Vladimir Markoca6fff82017-10-03 14:49:14 +01002306 LocationSummary* const locations =
2307 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002308 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2309
2310 locations->SetInAt(0, Location::RequiresFpuRegister());
2311 locations->SetOut(Location::RequiresFpuRegister());
2312 // Native code uses the soft float ABI.
2313 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2314 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2315}
2316
Vladimir Markoca6fff82017-10-03 14:49:14 +01002317static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov5ec62182016-10-13 20:16:02 +01002318 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2319 // the code generator. Furthermore, the register allocator creates fixed live intervals
2320 // for all caller-saved registers because we are doing a function call. As a result, if
2321 // the input and output locations are unallocated, the register allocator runs out of
2322 // registers and fails; however, a debuggable graph is not the common case.
2323 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2324 return;
2325 }
2326
2327 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 DCHECK_EQ(invoke->InputAt(0)->GetType(), DataType::Type::kFloat64);
2329 DCHECK_EQ(invoke->InputAt(1)->GetType(), DataType::Type::kFloat64);
2330 DCHECK_EQ(invoke->GetType(), DataType::Type::kFloat64);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002331
Vladimir Markoca6fff82017-10-03 14:49:14 +01002332 LocationSummary* const locations =
2333 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002334 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2335
2336 locations->SetInAt(0, Location::RequiresFpuRegister());
2337 locations->SetInAt(1, Location::RequiresFpuRegister());
2338 locations->SetOut(Location::RequiresFpuRegister());
2339 // Native code uses the soft float ABI.
2340 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2341 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2342 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
2343 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(3)));
2344}
2345
2346static void GenFPToFPCall(HInvoke* invoke,
2347 ArmVIXLAssembler* assembler,
2348 CodeGeneratorARMVIXL* codegen,
2349 QuickEntrypointEnum entry) {
2350 LocationSummary* const locations = invoke->GetLocations();
2351
2352 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2353 DCHECK(locations->WillCall() && locations->Intrinsified());
2354
2355 // Native code uses the soft float ABI.
2356 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2357 RegisterFrom(locations->GetTemp(1)),
2358 InputDRegisterAt(invoke, 0));
2359 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2360 __ Vmov(OutputDRegister(invoke),
2361 RegisterFrom(locations->GetTemp(0)),
2362 RegisterFrom(locations->GetTemp(1)));
2363}
2364
2365static void GenFPFPToFPCall(HInvoke* invoke,
2366 ArmVIXLAssembler* assembler,
2367 CodeGeneratorARMVIXL* codegen,
2368 QuickEntrypointEnum entry) {
2369 LocationSummary* const locations = invoke->GetLocations();
2370
2371 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2372 DCHECK(locations->WillCall() && locations->Intrinsified());
2373
2374 // Native code uses the soft float ABI.
2375 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2376 RegisterFrom(locations->GetTemp(1)),
2377 InputDRegisterAt(invoke, 0));
2378 __ Vmov(RegisterFrom(locations->GetTemp(2)),
2379 RegisterFrom(locations->GetTemp(3)),
2380 InputDRegisterAt(invoke, 1));
2381 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2382 __ Vmov(OutputDRegister(invoke),
2383 RegisterFrom(locations->GetTemp(0)),
2384 RegisterFrom(locations->GetTemp(1)));
2385}
2386
2387void IntrinsicLocationsBuilderARMVIXL::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002388 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002389}
2390
2391void IntrinsicCodeGeneratorARMVIXL::VisitMathCos(HInvoke* invoke) {
2392 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCos);
2393}
2394
2395void IntrinsicLocationsBuilderARMVIXL::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002396 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002397}
2398
2399void IntrinsicCodeGeneratorARMVIXL::VisitMathSin(HInvoke* invoke) {
2400 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSin);
2401}
2402
2403void IntrinsicLocationsBuilderARMVIXL::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002404 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002405}
2406
2407void IntrinsicCodeGeneratorARMVIXL::VisitMathAcos(HInvoke* invoke) {
2408 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAcos);
2409}
2410
2411void IntrinsicLocationsBuilderARMVIXL::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002412 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002413}
2414
2415void IntrinsicCodeGeneratorARMVIXL::VisitMathAsin(HInvoke* invoke) {
2416 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAsin);
2417}
2418
2419void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002420 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002421}
2422
2423void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan(HInvoke* invoke) {
2424 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan);
2425}
2426
2427void IntrinsicLocationsBuilderARMVIXL::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002428 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002429}
2430
2431void IntrinsicCodeGeneratorARMVIXL::VisitMathCbrt(HInvoke* invoke) {
2432 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCbrt);
2433}
2434
2435void IntrinsicLocationsBuilderARMVIXL::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002436 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002437}
2438
2439void IntrinsicCodeGeneratorARMVIXL::VisitMathCosh(HInvoke* invoke) {
2440 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCosh);
2441}
2442
2443void IntrinsicLocationsBuilderARMVIXL::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002444 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002445}
2446
2447void IntrinsicCodeGeneratorARMVIXL::VisitMathExp(HInvoke* invoke) {
2448 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExp);
2449}
2450
2451void IntrinsicLocationsBuilderARMVIXL::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002452 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002453}
2454
2455void IntrinsicCodeGeneratorARMVIXL::VisitMathExpm1(HInvoke* invoke) {
2456 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExpm1);
2457}
2458
2459void IntrinsicLocationsBuilderARMVIXL::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002460 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002461}
2462
2463void IntrinsicCodeGeneratorARMVIXL::VisitMathLog(HInvoke* invoke) {
2464 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog);
2465}
2466
2467void IntrinsicLocationsBuilderARMVIXL::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002468 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002469}
2470
2471void IntrinsicCodeGeneratorARMVIXL::VisitMathLog10(HInvoke* invoke) {
2472 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog10);
2473}
2474
2475void IntrinsicLocationsBuilderARMVIXL::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002476 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002477}
2478
2479void IntrinsicCodeGeneratorARMVIXL::VisitMathSinh(HInvoke* invoke) {
2480 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSinh);
2481}
2482
2483void IntrinsicLocationsBuilderARMVIXL::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002484 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002485}
2486
2487void IntrinsicCodeGeneratorARMVIXL::VisitMathTan(HInvoke* invoke) {
2488 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTan);
2489}
2490
2491void IntrinsicLocationsBuilderARMVIXL::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002492 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002493}
2494
2495void IntrinsicCodeGeneratorARMVIXL::VisitMathTanh(HInvoke* invoke) {
2496 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTanh);
2497}
2498
2499void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002500 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002501}
2502
2503void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan2(HInvoke* invoke) {
2504 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan2);
2505}
2506
Vladimir Marko4d179872018-01-19 14:50:10 +00002507void IntrinsicLocationsBuilderARMVIXL::VisitMathPow(HInvoke* invoke) {
2508 CreateFPFPToFPCallLocations(allocator_, invoke);
2509}
2510
2511void IntrinsicCodeGeneratorARMVIXL::VisitMathPow(HInvoke* invoke) {
2512 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickPow);
2513}
2514
Anton Kirilov5ec62182016-10-13 20:16:02 +01002515void IntrinsicLocationsBuilderARMVIXL::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002516 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002517}
2518
2519void IntrinsicCodeGeneratorARMVIXL::VisitMathHypot(HInvoke* invoke) {
2520 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickHypot);
2521}
2522
2523void IntrinsicLocationsBuilderARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002524 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002525}
2526
2527void IntrinsicCodeGeneratorARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
2528 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickNextAfter);
2529}
2530
2531void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002532 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002533}
2534
2535void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
2536 ArmVIXLAssembler* assembler = GetAssembler();
2537 __ Rbit(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2538}
2539
2540void IntrinsicLocationsBuilderARMVIXL::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002541 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002542}
2543
2544void IntrinsicCodeGeneratorARMVIXL::VisitLongReverse(HInvoke* invoke) {
2545 ArmVIXLAssembler* assembler = GetAssembler();
2546 LocationSummary* locations = invoke->GetLocations();
2547
2548 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2549 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2550 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2551 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2552
2553 __ Rbit(out_reg_lo, in_reg_hi);
2554 __ Rbit(out_reg_hi, in_reg_lo);
2555}
2556
2557void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002558 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002559}
2560
2561void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
2562 ArmVIXLAssembler* assembler = GetAssembler();
2563 __ Rev(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2564}
2565
2566void IntrinsicLocationsBuilderARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002567 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002568}
2569
2570void IntrinsicCodeGeneratorARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
2571 ArmVIXLAssembler* assembler = GetAssembler();
2572 LocationSummary* locations = invoke->GetLocations();
2573
2574 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2575 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2576 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2577 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2578
2579 __ Rev(out_reg_lo, in_reg_hi);
2580 __ Rev(out_reg_hi, in_reg_lo);
2581}
2582
2583void IntrinsicLocationsBuilderARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002584 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002585}
2586
2587void IntrinsicCodeGeneratorARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
2588 ArmVIXLAssembler* assembler = GetAssembler();
2589 __ Revsh(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2590}
2591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592static void GenBitCount(HInvoke* instr, DataType::Type type, ArmVIXLAssembler* assembler) {
2593 DCHECK(DataType::IsIntOrLongType(type)) << type;
2594 DCHECK_EQ(instr->GetType(), DataType::Type::kInt32);
2595 DCHECK_EQ(DataType::Kind(instr->InputAt(0)->GetType()), type);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002597 bool is_long = type == DataType::Type::kInt64;
Anton Kirilov5ec62182016-10-13 20:16:02 +01002598 LocationSummary* locations = instr->GetLocations();
2599 Location in = locations->InAt(0);
2600 vixl32::Register src_0 = is_long ? LowRegisterFrom(in) : RegisterFrom(in);
2601 vixl32::Register src_1 = is_long ? HighRegisterFrom(in) : src_0;
2602 vixl32::SRegister tmp_s = LowSRegisterFrom(locations->GetTemp(0));
2603 vixl32::DRegister tmp_d = DRegisterFrom(locations->GetTemp(0));
2604 vixl32::Register out_r = OutputRegister(instr);
2605
2606 // Move data from core register(s) to temp D-reg for bit count calculation, then move back.
2607 // According to Cortex A57 and A72 optimization guides, compared to transferring to full D-reg,
2608 // transferring data from core reg to upper or lower half of vfp D-reg requires extra latency,
2609 // That's why for integer bit count, we use 'vmov d0, r0, r0' instead of 'vmov d0[0], r0'.
2610 __ Vmov(tmp_d, src_1, src_0); // Temp DReg |--src_1|--src_0|
2611 __ Vcnt(Untyped8, tmp_d, tmp_d); // Temp DReg |c|c|c|c|c|c|c|c|
2612 __ Vpaddl(U8, tmp_d, tmp_d); // Temp DReg |--c|--c|--c|--c|
2613 __ Vpaddl(U16, tmp_d, tmp_d); // Temp DReg |------c|------c|
2614 if (is_long) {
2615 __ Vpaddl(U32, tmp_d, tmp_d); // Temp DReg |--------------c|
2616 }
2617 __ Vmov(out_r, tmp_s);
2618}
2619
2620void IntrinsicLocationsBuilderARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002621 CreateIntToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002622 invoke->GetLocations()->AddTemp(Location::RequiresFpuRegister());
2623}
2624
2625void IntrinsicCodeGeneratorARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002626 GenBitCount(invoke, DataType::Type::kInt32, GetAssembler());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002627}
2628
2629void IntrinsicLocationsBuilderARMVIXL::VisitLongBitCount(HInvoke* invoke) {
2630 VisitIntegerBitCount(invoke);
2631}
2632
2633void IntrinsicCodeGeneratorARMVIXL::VisitLongBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 GenBitCount(invoke, DataType::Type::kInt64, GetAssembler());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002635}
2636
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002637static void GenHighestOneBit(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002638 DataType::Type type,
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002639 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002640 DCHECK(DataType::IsIntOrLongType(type));
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002641
2642 ArmVIXLAssembler* assembler = codegen->GetAssembler();
2643 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2644 const vixl32::Register temp = temps.Acquire();
2645
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002646 if (type == DataType::Type::kInt64) {
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002647 LocationSummary* locations = invoke->GetLocations();
2648 Location in = locations->InAt(0);
2649 Location out = locations->Out();
2650
2651 vixl32::Register in_reg_lo = LowRegisterFrom(in);
2652 vixl32::Register in_reg_hi = HighRegisterFrom(in);
2653 vixl32::Register out_reg_lo = LowRegisterFrom(out);
2654 vixl32::Register out_reg_hi = HighRegisterFrom(out);
2655
2656 __ Mov(temp, 0x80000000); // Modified immediate.
2657 __ Clz(out_reg_lo, in_reg_lo);
2658 __ Clz(out_reg_hi, in_reg_hi);
2659 __ Lsr(out_reg_lo, temp, out_reg_lo);
2660 __ Lsrs(out_reg_hi, temp, out_reg_hi);
2661
2662 // Discard result for lowest 32 bits if highest 32 bits are not zero.
2663 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2664 // we check that the output is in a low register, so that a 16-bit MOV
2665 // encoding can be used. If output is in a high register, then we generate
2666 // 4 more bytes of code to avoid a branch.
2667 Operand mov_src(0);
2668 if (!out_reg_lo.IsLow()) {
2669 __ Mov(LeaveFlags, temp, 0);
2670 mov_src = Operand(temp);
2671 }
2672 ExactAssemblyScope it_scope(codegen->GetVIXLAssembler(),
2673 2 * vixl32::k16BitT32InstructionSizeInBytes,
2674 CodeBufferCheckScope::kExactSize);
2675 __ it(ne);
2676 __ mov(ne, out_reg_lo, mov_src);
2677 } else {
2678 vixl32::Register out = OutputRegister(invoke);
2679 vixl32::Register in = InputRegisterAt(invoke, 0);
2680
2681 __ Mov(temp, 0x80000000); // Modified immediate.
2682 __ Clz(out, in);
2683 __ Lsr(out, temp, out);
2684 }
2685}
2686
2687void IntrinsicLocationsBuilderARMVIXL::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002688 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002689}
2690
2691void IntrinsicCodeGeneratorARMVIXL::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 GenHighestOneBit(invoke, DataType::Type::kInt32, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002693}
2694
2695void IntrinsicLocationsBuilderARMVIXL::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002696 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002697}
2698
2699void IntrinsicCodeGeneratorARMVIXL::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 GenHighestOneBit(invoke, DataType::Type::kInt64, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002701}
2702
2703static void GenLowestOneBit(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 DataType::Type type,
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002705 CodeGeneratorARMVIXL* codegen) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 DCHECK(DataType::IsIntOrLongType(type));
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002707
2708 ArmVIXLAssembler* assembler = codegen->GetAssembler();
2709 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2710 const vixl32::Register temp = temps.Acquire();
2711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002712 if (type == DataType::Type::kInt64) {
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002713 LocationSummary* locations = invoke->GetLocations();
2714 Location in = locations->InAt(0);
2715 Location out = locations->Out();
2716
2717 vixl32::Register in_reg_lo = LowRegisterFrom(in);
2718 vixl32::Register in_reg_hi = HighRegisterFrom(in);
2719 vixl32::Register out_reg_lo = LowRegisterFrom(out);
2720 vixl32::Register out_reg_hi = HighRegisterFrom(out);
2721
2722 __ Rsb(out_reg_hi, in_reg_hi, 0);
2723 __ Rsb(out_reg_lo, in_reg_lo, 0);
2724 __ And(out_reg_hi, out_reg_hi, in_reg_hi);
2725 // The result of this operation is 0 iff in_reg_lo is 0
2726 __ Ands(out_reg_lo, out_reg_lo, in_reg_lo);
2727
2728 // Discard result for highest 32 bits if lowest 32 bits are not zero.
2729 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2730 // we check that the output is in a low register, so that a 16-bit MOV
2731 // encoding can be used. If output is in a high register, then we generate
2732 // 4 more bytes of code to avoid a branch.
2733 Operand mov_src(0);
2734 if (!out_reg_lo.IsLow()) {
2735 __ Mov(LeaveFlags, temp, 0);
2736 mov_src = Operand(temp);
2737 }
2738 ExactAssemblyScope it_scope(codegen->GetVIXLAssembler(),
2739 2 * vixl32::k16BitT32InstructionSizeInBytes,
2740 CodeBufferCheckScope::kExactSize);
2741 __ it(ne);
2742 __ mov(ne, out_reg_hi, mov_src);
2743 } else {
2744 vixl32::Register out = OutputRegister(invoke);
2745 vixl32::Register in = InputRegisterAt(invoke, 0);
2746
2747 __ Rsb(temp, in, 0);
2748 __ And(out, temp, in);
2749 }
2750}
2751
2752void IntrinsicLocationsBuilderARMVIXL::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002753 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002754}
2755
2756void IntrinsicCodeGeneratorARMVIXL::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002757 GenLowestOneBit(invoke, DataType::Type::kInt32, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002758}
2759
2760void IntrinsicLocationsBuilderARMVIXL::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002761 CreateLongToLongLocationsWithOverlap(allocator_, invoke);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002762}
2763
2764void IntrinsicCodeGeneratorARMVIXL::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002765 GenLowestOneBit(invoke, DataType::Type::kInt64, codegen_);
Petre-Ionut Tudor27292e62017-08-04 16:06:45 +01002766}
2767
Anton Kirilov5ec62182016-10-13 20:16:02 +01002768void IntrinsicLocationsBuilderARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002769 LocationSummary* locations =
2770 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002771 locations->SetInAt(0, Location::RequiresRegister());
2772 locations->SetInAt(1, Location::RequiresRegister());
2773 locations->SetInAt(2, Location::RequiresRegister());
2774 locations->SetInAt(3, Location::RequiresRegister());
2775 locations->SetInAt(4, Location::RequiresRegister());
2776
2777 // Temporary registers to store lengths of strings and for calculations.
2778 locations->AddTemp(Location::RequiresRegister());
2779 locations->AddTemp(Location::RequiresRegister());
2780 locations->AddTemp(Location::RequiresRegister());
2781}
2782
2783void IntrinsicCodeGeneratorARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2784 ArmVIXLAssembler* assembler = GetAssembler();
2785 LocationSummary* locations = invoke->GetLocations();
2786
2787 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002788 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002789 DCHECK_EQ(char_size, 2u);
2790
2791 // Location of data in char array buffer.
2792 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2793
2794 // Location of char array data in string.
2795 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2796
2797 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2798 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2799 vixl32::Register srcObj = InputRegisterAt(invoke, 0);
2800 vixl32::Register srcBegin = InputRegisterAt(invoke, 1);
2801 vixl32::Register srcEnd = InputRegisterAt(invoke, 2);
2802 vixl32::Register dstObj = InputRegisterAt(invoke, 3);
2803 vixl32::Register dstBegin = InputRegisterAt(invoke, 4);
2804
2805 vixl32::Register num_chr = RegisterFrom(locations->GetTemp(0));
2806 vixl32::Register src_ptr = RegisterFrom(locations->GetTemp(1));
2807 vixl32::Register dst_ptr = RegisterFrom(locations->GetTemp(2));
2808
2809 vixl32::Label done, compressed_string_loop;
Anton Kirilov6f644202017-02-27 18:29:45 +00002810 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002811 // dst to be copied.
2812 __ Add(dst_ptr, dstObj, data_offset);
2813 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, vixl32::LSL, 1));
2814
2815 __ Subs(num_chr, srcEnd, srcBegin);
2816 // Early out for valid zero-length retrievals.
Anton Kirilov6f644202017-02-27 18:29:45 +00002817 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002818
2819 // src range to copy.
2820 __ Add(src_ptr, srcObj, value_offset);
2821
2822 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2823 vixl32::Register temp;
2824 vixl32::Label compressed_string_preloop;
2825 if (mirror::kUseStringCompression) {
2826 // Location of count in string.
2827 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2828 temp = temps.Acquire();
2829 // String's length.
2830 __ Ldr(temp, MemOperand(srcObj, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002831 __ Tst(temp, 1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002832 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002833 __ B(eq, &compressed_string_preloop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002834 }
2835 __ Add(src_ptr, src_ptr, Operand(srcBegin, vixl32::LSL, 1));
2836
2837 // Do the copy.
2838 vixl32::Label loop, remainder;
2839
2840 temp = temps.Acquire();
2841 // Save repairing the value of num_chr on the < 4 character path.
2842 __ Subs(temp, num_chr, 4);
Artem Serov517d9f62016-12-12 15:51:15 +00002843 __ B(lt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002844
2845 // Keep the result of the earlier subs, we are going to fetch at least 4 characters.
2846 __ Mov(num_chr, temp);
2847
2848 // Main loop used for longer fetches loads and stores 4x16-bit characters at a time.
2849 // (LDRD/STRD fault on unaligned addresses and it's not worth inlining extra code
2850 // to rectify these everywhere this intrinsic applies.)
2851 __ Bind(&loop);
2852 __ Ldr(temp, MemOperand(src_ptr, char_size * 2));
2853 __ Subs(num_chr, num_chr, 4);
2854 __ Str(temp, MemOperand(dst_ptr, char_size * 2));
2855 __ Ldr(temp, MemOperand(src_ptr, char_size * 4, PostIndex));
2856 __ Str(temp, MemOperand(dst_ptr, char_size * 4, PostIndex));
2857 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002858 __ B(ge, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002859
2860 __ Adds(num_chr, num_chr, 4);
Anton Kirilov6f644202017-02-27 18:29:45 +00002861 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002862
2863 // Main loop for < 4 character case and remainder handling. Loads and stores one
2864 // 16-bit Java character at a time.
2865 __ Bind(&remainder);
2866 temp = temps.Acquire();
2867 __ Ldrh(temp, MemOperand(src_ptr, char_size, PostIndex));
2868 __ Subs(num_chr, num_chr, 1);
2869 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2870 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002871 __ B(gt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002872
2873 if (mirror::kUseStringCompression) {
Anton Kirilov6f644202017-02-27 18:29:45 +00002874 __ B(final_label);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002877 DCHECK_EQ(c_char_size, 1u);
2878 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2879 __ Bind(&compressed_string_preloop);
2880 __ Add(src_ptr, src_ptr, srcBegin);
2881 __ Bind(&compressed_string_loop);
2882 temp = temps.Acquire();
2883 __ Ldrb(temp, MemOperand(src_ptr, c_char_size, PostIndex));
2884 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
2885 temps.Release(temp);
2886 __ Subs(num_chr, num_chr, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00002887 __ B(gt, &compressed_string_loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002888 }
2889
Anton Kirilov6f644202017-02-27 18:29:45 +00002890 if (done.IsReferenced()) {
2891 __ Bind(&done);
2892 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002893}
2894
2895void IntrinsicLocationsBuilderARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002896 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002897}
2898
2899void IntrinsicCodeGeneratorARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
2900 ArmVIXLAssembler* const assembler = GetAssembler();
2901 const vixl32::Register out = OutputRegister(invoke);
2902 // Shifting left by 1 bit makes the value encodable as an immediate operand;
2903 // we don't care about the sign bit anyway.
2904 constexpr uint32_t infinity = kPositiveInfinityFloat << 1U;
2905
2906 __ Vmov(out, InputSRegisterAt(invoke, 0));
2907 // We don't care about the sign bit, so shift left.
2908 __ Lsl(out, out, 1);
2909 __ Eor(out, out, infinity);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002910 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002911}
2912
2913void IntrinsicLocationsBuilderARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002914 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002915}
2916
2917void IntrinsicCodeGeneratorARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
2918 ArmVIXLAssembler* const assembler = GetAssembler();
2919 const vixl32::Register out = OutputRegister(invoke);
2920 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2921 const vixl32::Register temp = temps.Acquire();
2922 // The highest 32 bits of double precision positive infinity separated into
2923 // two constants encodable as immediate operands.
2924 constexpr uint32_t infinity_high = 0x7f000000U;
2925 constexpr uint32_t infinity_high2 = 0x00f00000U;
2926
2927 static_assert((infinity_high | infinity_high2) ==
2928 static_cast<uint32_t>(kPositiveInfinityDouble >> 32U),
2929 "The constants do not add up to the high 32 bits of double "
2930 "precision positive infinity.");
2931 __ Vmov(temp, out, InputDRegisterAt(invoke, 0));
2932 __ Eor(out, out, infinity_high);
2933 __ Eor(out, out, infinity_high2);
2934 // We don't care about the sign bit, so shift left.
2935 __ Orr(out, temp, Operand(out, vixl32::LSL, 1));
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002936 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002937}
2938
Artem Serov9aee2d42017-01-06 15:58:31 +00002939void IntrinsicLocationsBuilderARMVIXL::VisitMathCeil(HInvoke* invoke) {
2940 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002941 CreateFPToFPLocations(allocator_, invoke);
Artem Serov9aee2d42017-01-06 15:58:31 +00002942 }
2943}
2944
2945void IntrinsicCodeGeneratorARMVIXL::VisitMathCeil(HInvoke* invoke) {
2946 ArmVIXLAssembler* assembler = GetAssembler();
2947 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
Roland Levillain94770472018-10-04 12:47:01 +00002948 __ Vrintp(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
Artem Serov9aee2d42017-01-06 15:58:31 +00002949}
2950
2951void IntrinsicLocationsBuilderARMVIXL::VisitMathFloor(HInvoke* invoke) {
2952 if (features_.HasARMv8AInstructions()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002953 CreateFPToFPLocations(allocator_, invoke);
Artem Serov9aee2d42017-01-06 15:58:31 +00002954 }
2955}
2956
2957void IntrinsicCodeGeneratorARMVIXL::VisitMathFloor(HInvoke* invoke) {
2958 ArmVIXLAssembler* assembler = GetAssembler();
2959 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
Roland Levillain94770472018-10-04 12:47:01 +00002960 __ Vrintm(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
Artem Serov9aee2d42017-01-06 15:58:31 +00002961}
2962
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002963void IntrinsicLocationsBuilderARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
2964 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2965 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2966 invoke,
2967 codegen_,
2968 LocationFrom(r0),
2969 LocationFrom(calling_convention.GetRegisterAt(0)));
2970}
2971
2972void IntrinsicCodeGeneratorARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01002973 IntrinsicVisitor::IntegerValueOfInfo info =
2974 IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002975 LocationSummary* locations = invoke->GetLocations();
2976 ArmVIXLAssembler* const assembler = GetAssembler();
2977
2978 vixl32::Register out = RegisterFrom(locations->Out());
2979 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2980 vixl32::Register temp = temps.Acquire();
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002981 if (invoke->InputAt(0)->IsConstant()) {
2982 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
Vladimir Marko6fd16062018-06-26 11:02:04 +01002983 if (static_cast<uint32_t>(value - info.low) < info.length) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002984 // Just embed the j.l.Integer in the code.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002985 DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
2986 codegen_->LoadBootImageAddress(out, info.value_boot_image_reference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002987 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002988 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002989 // Allocate and initialize a new j.l.Integer.
2990 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2991 // JIT object table.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002992 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2993 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002994 __ Mov(temp, value);
2995 assembler->StoreToOffset(kStoreWord, temp, out, info.value_offset);
2996 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2997 // one.
2998 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2999 }
3000 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01003001 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003002 vixl32::Register in = RegisterFrom(locations->InAt(0));
3003 // Check bounds of our cache.
3004 __ Add(out, in, -info.low);
Vladimir Markoeebb8212018-06-05 14:57:24 +01003005 __ Cmp(out, info.length);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003006 vixl32::Label allocate, done;
Anton Kirilovfd522532017-05-10 12:46:57 +01003007 __ B(hs, &allocate, /* is_far_target */ false);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003008 // If the value is within the bounds, load the j.l.Integer directly from the array.
Vladimir Marko6fd16062018-06-26 11:02:04 +01003009 codegen_->LoadBootImageAddress(temp, info.array_data_boot_image_reference);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003010 codegen_->LoadFromShiftedRegOffset(DataType::Type::kReference, locations->Out(), temp, out);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003011 assembler->MaybeUnpoisonHeapReference(out);
3012 __ B(&done);
3013 __ Bind(&allocate);
3014 // Otherwise allocate and initialize a new j.l.Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +01003015 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
3016 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003017 assembler->StoreToOffset(kStoreWord, in, out, info.value_offset);
3018 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3019 // one.
3020 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3021 __ Bind(&done);
3022 }
3023}
3024
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003025void IntrinsicLocationsBuilderARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003026 LocationSummary* locations =
3027 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003028 locations->SetOut(Location::RequiresRegister());
3029}
3030
3031void IntrinsicCodeGeneratorARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
3032 ArmVIXLAssembler* assembler = GetAssembler();
3033 vixl32::Register out = RegisterFrom(invoke->GetLocations()->Out());
3034 int32_t offset = Thread::InterruptedOffset<kArmPointerSize>().Int32Value();
3035 __ Ldr(out, MemOperand(tr, offset));
3036 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3037 vixl32::Register temp = temps.Acquire();
3038 vixl32::Label done;
Anton Kirilovfd522532017-05-10 12:46:57 +01003039 vixl32::Label* const final_label = codegen_->GetFinalLabel(invoke, &done);
3040 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003041 __ Dmb(vixl32::ISH);
3042 __ Mov(temp, 0);
3043 assembler->StoreToOffset(kStoreWord, temp, tr, offset);
3044 __ Dmb(vixl32::ISH);
Anton Kirilovfd522532017-05-10 12:46:57 +01003045 if (done.IsReferenced()) {
3046 __ Bind(&done);
3047 }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003048}
3049
Hans Boehmc7b28de2018-03-09 17:05:28 -08003050void IntrinsicLocationsBuilderARMVIXL::VisitReachabilityFence(HInvoke* invoke) {
3051 LocationSummary* locations =
3052 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
3053 locations->SetInAt(0, Location::Any());
3054}
3055
3056void IntrinsicCodeGeneratorARMVIXL::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
3057
Anton Kirilov5ec62182016-10-13 20:16:02 +01003058UNIMPLEMENTED_INTRINSIC(ARMVIXL, MathRoundDouble) // Could be done by changing rounding mode, maybe?
Anton Kirilov5ec62182016-10-13 20:16:02 +01003059UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeCASLong) // High register pressure.
3060UNIMPLEMENTED_INTRINSIC(ARMVIXL, SystemArrayCopyChar)
Vladimir Markod254f5c2017-06-02 15:18:36 +00003061UNIMPLEMENTED_INTRINSIC(ARMVIXL, ReferenceGetReferent)
xueliang.zhongcb58b072017-10-13 12:06:56 +01003062UNIMPLEMENTED_INTRINSIC(ARMVIXL, CRC32Update)
Anton Kirilov5ec62182016-10-13 20:16:02 +01003063
Aart Bikff7d89c2016-11-07 08:49:28 -08003064UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOf);
3065UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003066UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferAppend);
3067UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferLength);
3068UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferToString);
3069UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderAppend);
3070UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderLength);
3071UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003072
Anton Kirilov5ec62182016-10-13 20:16:02 +01003073// 1.8.
3074UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddInt)
3075UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddLong)
3076UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetInt)
3077UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetLong)
3078UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetObject)
3079
3080UNREACHABLE_INTRINSICS(ARMVIXL)
3081
3082#undef __
3083
3084} // namespace arm
3085} // namespace art