blob: 7c43f2eff29457d774d597506ba3a35435ada664 [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"
23#include "lock_word.h"
24#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070025#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080026#include "mirror/reference.h"
27#include "mirror/string.h"
28#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070029#include "thread-current-inl.h"
Anton Kirilov5ec62182016-10-13 20:16:02 +010030
31#include "aarch32/constants-aarch32.h"
32
33namespace art {
34namespace arm {
35
36#define __ assembler->GetVIXLAssembler()->
37
38using helpers::DRegisterFrom;
39using helpers::HighRegisterFrom;
40using helpers::InputDRegisterAt;
41using helpers::InputRegisterAt;
42using helpers::InputSRegisterAt;
43using helpers::InputVRegisterAt;
44using 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;
xueliang.zhongc032e742016-03-28 16:44:32 +010050using helpers::OutputSRegister;
Anton Kirilov5ec62182016-10-13 20:16:02 +010051using helpers::OutputRegister;
52using helpers::OutputVRegister;
53using helpers::RegisterFrom;
54using helpers::SRegisterFrom;
xueliang.zhongc032e742016-03-28 16:44:32 +010055using helpers::DRegisterFromS;
Anton Kirilov5ec62182016-10-13 20:16:02 +010056
57using namespace vixl::aarch32; // NOLINT(build/namespaces)
58
Artem Serov0fb37192016-12-06 18:13:40 +000059using vixl::ExactAssemblyScope;
60using vixl::CodeBufferCheckScope;
61
Anton Kirilov5ec62182016-10-13 20:16:02 +010062ArmVIXLAssembler* IntrinsicCodeGeneratorARMVIXL::GetAssembler() {
63 return codegen_->GetAssembler();
64}
65
66ArenaAllocator* IntrinsicCodeGeneratorARMVIXL::GetAllocator() {
67 return codegen_->GetGraph()->GetArena();
68}
69
70// Default slow-path for fallback (calling the managed code to handle the intrinsic) in an
71// intrinsified call. This will copy the arguments into the positions for a regular call.
72//
73// Note: The actual parameters are required to be in the locations given by the invoke's location
74// summary. If an intrinsic modifies those locations before a slowpath call, they must be
75// restored!
76//
77// Note: If an invoke wasn't sharpened, we will put down an invoke-virtual here. That's potentially
78// sub-optimal (compared to a direct pointer call), but this is a slow-path.
79
80class IntrinsicSlowPathARMVIXL : public SlowPathCodeARMVIXL {
81 public:
82 explicit IntrinsicSlowPathARMVIXL(HInvoke* invoke)
83 : SlowPathCodeARMVIXL(invoke), invoke_(invoke) {}
84
85 Location MoveArguments(CodeGenerator* codegen) {
Artem Serovd4cc5b22016-11-04 11:19:09 +000086 InvokeDexCallingConventionVisitorARMVIXL calling_convention_visitor;
Anton Kirilov5ec62182016-10-13 20:16:02 +010087 IntrinsicVisitor::MoveArguments(invoke_, codegen, &calling_convention_visitor);
88 return calling_convention_visitor.GetMethodLocation();
89 }
90
91 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
92 ArmVIXLAssembler* assembler = down_cast<ArmVIXLAssembler*>(codegen->GetAssembler());
93 __ Bind(GetEntryLabel());
94
95 SaveLiveRegisters(codegen, invoke_->GetLocations());
96
97 Location method_loc = MoveArguments(codegen);
98
99 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100100 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(), method_loc, this);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100101 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100102 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), method_loc, this);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100103 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100104
105 // Copy the result back to the expected output.
106 Location out = invoke_->GetLocations()->Out();
107 if (out.IsValid()) {
108 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
109 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
110 codegen->MoveFromReturnRegister(out, invoke_->GetType());
111 }
112
113 RestoreLiveRegisters(codegen, invoke_->GetLocations());
114 __ B(GetExitLabel());
115 }
116
117 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPath"; }
118
119 private:
120 // The instruction where this slow path is happening.
121 HInvoke* const invoke_;
122
123 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARMVIXL);
124};
125
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000126// Compute base address for the System.arraycopy intrinsic in `base`.
127static void GenSystemArrayCopyBaseAddress(ArmVIXLAssembler* assembler,
128 Primitive::Type type,
129 const vixl32::Register& array,
130 const Location& pos,
131 const vixl32::Register& base) {
132 // This routine is only used by the SystemArrayCopy intrinsic at the
133 // moment. We can allow Primitive::kPrimNot as `type` to implement
134 // the SystemArrayCopyChar intrinsic.
135 DCHECK_EQ(type, Primitive::kPrimNot);
136 const int32_t element_size = Primitive::ComponentSize(type);
137 const uint32_t element_size_shift = Primitive::ComponentSizeShift(type);
138 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
139
140 if (pos.IsConstant()) {
141 int32_t constant = Int32ConstantFrom(pos);
142 __ Add(base, array, element_size * constant + data_offset);
143 } else {
144 __ Add(base, array, Operand(RegisterFrom(pos), vixl32::LSL, element_size_shift));
145 __ Add(base, base, data_offset);
146 }
147}
148
149// Compute end address for the System.arraycopy intrinsic in `end`.
150static void GenSystemArrayCopyEndAddress(ArmVIXLAssembler* assembler,
151 Primitive::Type type,
152 const Location& copy_length,
153 const vixl32::Register& base,
154 const vixl32::Register& end) {
155 // This routine is only used by the SystemArrayCopy intrinsic at the
156 // moment. We can allow Primitive::kPrimNot as `type` to implement
157 // the SystemArrayCopyChar intrinsic.
158 DCHECK_EQ(type, Primitive::kPrimNot);
159 const int32_t element_size = Primitive::ComponentSize(type);
160 const uint32_t element_size_shift = Primitive::ComponentSizeShift(type);
161
162 if (copy_length.IsConstant()) {
163 int32_t constant = Int32ConstantFrom(copy_length);
164 __ Add(end, base, element_size * constant);
165 } else {
166 __ Add(end, base, Operand(RegisterFrom(copy_length), vixl32::LSL, element_size_shift));
167 }
168}
169
Anton Kirilov5ec62182016-10-13 20:16:02 +0100170// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
171class ReadBarrierSystemArrayCopySlowPathARMVIXL : public SlowPathCodeARMVIXL {
172 public:
173 explicit ReadBarrierSystemArrayCopySlowPathARMVIXL(HInstruction* instruction)
174 : SlowPathCodeARMVIXL(instruction) {
175 DCHECK(kEmitCompilerReadBarrier);
176 DCHECK(kUseBakerReadBarrier);
177 }
178
179 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
180 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen);
181 ArmVIXLAssembler* assembler = arm_codegen->GetAssembler();
182 LocationSummary* locations = instruction_->GetLocations();
183 DCHECK(locations->CanCall());
184 DCHECK(instruction_->IsInvokeStaticOrDirect())
185 << "Unexpected instruction in read barrier arraycopy slow path: "
186 << instruction_->DebugName();
187 DCHECK(instruction_->GetLocations()->Intrinsified());
188 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
189
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000190 Primitive::Type type = Primitive::kPrimNot;
191 const int32_t element_size = Primitive::ComponentSize(type);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100192
193 vixl32::Register dest = InputRegisterAt(instruction_, 2);
194 Location dest_pos = locations->InAt(3);
195 vixl32::Register src_curr_addr = RegisterFrom(locations->GetTemp(0));
196 vixl32::Register dst_curr_addr = RegisterFrom(locations->GetTemp(1));
197 vixl32::Register src_stop_addr = RegisterFrom(locations->GetTemp(2));
198 vixl32::Register tmp = RegisterFrom(locations->GetTemp(3));
199
200 __ Bind(GetEntryLabel());
201 // Compute the base destination address in `dst_curr_addr`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000202 GenSystemArrayCopyBaseAddress(assembler, type, dest, dest_pos, dst_curr_addr);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100203
204 vixl32::Label loop;
205 __ Bind(&loop);
206 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
207 assembler->MaybeUnpoisonHeapReference(tmp);
208 // TODO: Inline the mark bit check before calling the runtime?
209 // tmp = ReadBarrier::Mark(tmp);
210 // No need to save live registers; it's taken care of by the
211 // entrypoint. Also, there is no need to update the stack mask,
212 // as this runtime call will not trigger a garbage collection.
213 // (See ReadBarrierMarkSlowPathARM::EmitNativeCode for more
214 // explanations.)
215 DCHECK(!tmp.IsSP());
216 DCHECK(!tmp.IsLR());
217 DCHECK(!tmp.IsPC());
218 // IP is used internally by the ReadBarrierMarkRegX entry point
219 // as a temporary (and not preserved). It thus cannot be used by
220 // any live register in this slow path.
221 DCHECK(!src_curr_addr.Is(ip));
222 DCHECK(!dst_curr_addr.Is(ip));
223 DCHECK(!src_stop_addr.Is(ip));
224 DCHECK(!tmp.Is(ip));
225 DCHECK(tmp.IsRegister()) << tmp;
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000226 // TODO: Load the entrypoint once before the loop, instead of
227 // loading it at every iteration.
Anton Kirilov5ec62182016-10-13 20:16:02 +0100228 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100229 Thread::ReadBarrierMarkEntryPointsOffset<kArmPointerSize>(tmp.GetCode());
Anton Kirilov5ec62182016-10-13 20:16:02 +0100230 // This runtime call does not require a stack map.
231 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
232 assembler->MaybePoisonHeapReference(tmp);
233 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
234 __ Cmp(src_curr_addr, src_stop_addr);
Artem Serov517d9f62016-12-12 15:51:15 +0000235 __ B(ne, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100236 __ B(GetExitLabel());
237 }
238
239 const char* GetDescription() const OVERRIDE {
240 return "ReadBarrierSystemArrayCopySlowPathARMVIXL";
241 }
242
243 private:
244 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARMVIXL);
245};
246
247IntrinsicLocationsBuilderARMVIXL::IntrinsicLocationsBuilderARMVIXL(CodeGeneratorARMVIXL* codegen)
248 : arena_(codegen->GetGraph()->GetArena()),
Nicolas Geoffray331605a2017-03-01 11:01:41 +0000249 codegen_(codegen),
Anton Kirilov5ec62182016-10-13 20:16:02 +0100250 assembler_(codegen->GetAssembler()),
251 features_(codegen->GetInstructionSetFeatures()) {}
252
253bool IntrinsicLocationsBuilderARMVIXL::TryDispatch(HInvoke* invoke) {
254 Dispatch(invoke);
255 LocationSummary* res = invoke->GetLocations();
256 if (res == nullptr) {
257 return false;
258 }
259 return res->Intrinsified();
260}
261
262static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
263 LocationSummary* locations = new (arena) LocationSummary(invoke,
264 LocationSummary::kNoCall,
265 kIntrinsified);
266 locations->SetInAt(0, Location::RequiresFpuRegister());
267 locations->SetOut(Location::RequiresRegister());
268}
269
270static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
271 LocationSummary* locations = new (arena) LocationSummary(invoke,
272 LocationSummary::kNoCall,
273 kIntrinsified);
274 locations->SetInAt(0, Location::RequiresRegister());
275 locations->SetOut(Location::RequiresFpuRegister());
276}
277
278static void MoveFPToInt(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
279 Location input = locations->InAt(0);
280 Location output = locations->Out();
281 if (is64bit) {
282 __ Vmov(LowRegisterFrom(output), HighRegisterFrom(output), DRegisterFrom(input));
283 } else {
284 __ Vmov(RegisterFrom(output), SRegisterFrom(input));
285 }
286}
287
288static void MoveIntToFP(LocationSummary* locations, bool is64bit, ArmVIXLAssembler* assembler) {
289 Location input = locations->InAt(0);
290 Location output = locations->Out();
291 if (is64bit) {
292 __ Vmov(DRegisterFrom(output), LowRegisterFrom(input), HighRegisterFrom(input));
293 } else {
294 __ Vmov(SRegisterFrom(output), RegisterFrom(input));
295 }
296}
297
298void IntrinsicLocationsBuilderARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
299 CreateFPToIntLocations(arena_, invoke);
300}
301void IntrinsicLocationsBuilderARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
302 CreateIntToFPLocations(arena_, invoke);
303}
304
305void IntrinsicCodeGeneratorARMVIXL::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
306 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
307}
308void IntrinsicCodeGeneratorARMVIXL::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
309 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
310}
311
312void IntrinsicLocationsBuilderARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
313 CreateFPToIntLocations(arena_, invoke);
314}
315void IntrinsicLocationsBuilderARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
316 CreateIntToFPLocations(arena_, invoke);
317}
318
319void IntrinsicCodeGeneratorARMVIXL::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
320 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
321}
322void IntrinsicCodeGeneratorARMVIXL::VisitFloatIntBitsToFloat(HInvoke* invoke) {
323 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
324}
325
326static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
327 LocationSummary* locations = new (arena) LocationSummary(invoke,
328 LocationSummary::kNoCall,
329 kIntrinsified);
330 locations->SetInAt(0, Location::RequiresRegister());
331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
332}
333
334static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
335 LocationSummary* locations = new (arena) LocationSummary(invoke,
336 LocationSummary::kNoCall,
337 kIntrinsified);
338 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,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100343 Primitive::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
350 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
351
352 if (type == Primitive::kPrimLong) {
353 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) {
370 CreateIntToIntLocations(arena_, invoke);
371}
372
373void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000374 GenNumberOfLeadingZeros(invoke, Primitive::kPrimInt, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100375}
376
377void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
378 LocationSummary* locations = new (arena_) LocationSummary(invoke,
379 LocationSummary::kNoCall,
380 kIntrinsified);
381 locations->SetInAt(0, Location::RequiresRegister());
382 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
383}
384
385void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000386 GenNumberOfLeadingZeros(invoke, Primitive::kPrimLong, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100387}
388
Anton Kirilov6f644202017-02-27 18:29:45 +0000389static void GenNumberOfTrailingZeros(HInvoke* invoke,
Anton Kirilov5ec62182016-10-13 20:16:02 +0100390 Primitive::Type type,
Anton Kirilov6f644202017-02-27 18:29:45 +0000391 CodeGeneratorARMVIXL* codegen) {
Anton Kirilov5ec62182016-10-13 20:16:02 +0100392 DCHECK((type == Primitive::kPrimInt) || (type == Primitive::kPrimLong));
393
Anton Kirilov6f644202017-02-27 18:29:45 +0000394 ArmVIXLAssembler* assembler = codegen->GetAssembler();
395 LocationSummary* locations = invoke->GetLocations();
Anton Kirilov5ec62182016-10-13 20:16:02 +0100396 vixl32::Register out = RegisterFrom(locations->Out());
397
398 if (type == Primitive::kPrimLong) {
399 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
400 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
401 vixl32::Label end;
Anton Kirilov6f644202017-02-27 18:29:45 +0000402 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100403 __ Rbit(out, in_reg_lo);
404 __ Clz(out, out);
Anton Kirilov6f644202017-02-27 18:29:45 +0000405 __ CompareAndBranchIfNonZero(in_reg_lo, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100406 __ Rbit(out, in_reg_hi);
407 __ Clz(out, out);
408 __ Add(out, out, 32);
Anton Kirilov6f644202017-02-27 18:29:45 +0000409 if (end.IsReferenced()) {
410 __ Bind(&end);
411 }
Anton Kirilov5ec62182016-10-13 20:16:02 +0100412 } else {
413 vixl32::Register in = RegisterFrom(locations->InAt(0));
414 __ Rbit(out, in);
415 __ Clz(out, out);
416 }
417}
418
419void IntrinsicLocationsBuilderARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
420 LocationSummary* locations = new (arena_) LocationSummary(invoke,
421 LocationSummary::kNoCall,
422 kIntrinsified);
423 locations->SetInAt(0, Location::RequiresRegister());
424 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
425}
426
427void IntrinsicCodeGeneratorARMVIXL::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000428 GenNumberOfTrailingZeros(invoke, Primitive::kPrimInt, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100429}
430
431void IntrinsicLocationsBuilderARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
432 LocationSummary* locations = new (arena_) LocationSummary(invoke,
433 LocationSummary::kNoCall,
434 kIntrinsified);
435 locations->SetInAt(0, Location::RequiresRegister());
436 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
437}
438
439void IntrinsicCodeGeneratorARMVIXL::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000440 GenNumberOfTrailingZeros(invoke, Primitive::kPrimLong, codegen_);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100441}
442
443static void MathAbsFP(HInvoke* invoke, ArmVIXLAssembler* assembler) {
444 __ Vabs(OutputVRegister(invoke), InputVRegisterAt(invoke, 0));
445}
446
447void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsDouble(HInvoke* invoke) {
448 CreateFPToFPLocations(arena_, invoke);
449}
450
451void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsDouble(HInvoke* invoke) {
452 MathAbsFP(invoke, GetAssembler());
453}
454
455void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsFloat(HInvoke* invoke) {
456 CreateFPToFPLocations(arena_, invoke);
457}
458
459void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsFloat(HInvoke* invoke) {
460 MathAbsFP(invoke, GetAssembler());
461}
462
463static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
464 LocationSummary* locations = new (arena) LocationSummary(invoke,
465 LocationSummary::kNoCall,
466 kIntrinsified);
467 locations->SetInAt(0, Location::RequiresRegister());
468 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
469
470 locations->AddTemp(Location::RequiresRegister());
471}
472
473static void GenAbsInteger(LocationSummary* locations,
474 bool is64bit,
475 ArmVIXLAssembler* assembler) {
476 Location in = locations->InAt(0);
477 Location output = locations->Out();
478
479 vixl32::Register mask = RegisterFrom(locations->GetTemp(0));
480
481 if (is64bit) {
482 vixl32::Register in_reg_lo = LowRegisterFrom(in);
483 vixl32::Register in_reg_hi = HighRegisterFrom(in);
484 vixl32::Register out_reg_lo = LowRegisterFrom(output);
485 vixl32::Register out_reg_hi = HighRegisterFrom(output);
486
487 DCHECK(!out_reg_lo.Is(in_reg_hi)) << "Diagonal overlap unexpected.";
488
489 __ Asr(mask, in_reg_hi, 31);
490 __ Adds(out_reg_lo, in_reg_lo, mask);
491 __ Adc(out_reg_hi, in_reg_hi, mask);
492 __ Eor(out_reg_lo, mask, out_reg_lo);
493 __ Eor(out_reg_hi, mask, out_reg_hi);
494 } else {
495 vixl32::Register in_reg = RegisterFrom(in);
496 vixl32::Register out_reg = RegisterFrom(output);
497
498 __ Asr(mask, in_reg, 31);
499 __ Add(out_reg, in_reg, mask);
500 __ Eor(out_reg, mask, out_reg);
501 }
502}
503
504void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsInt(HInvoke* invoke) {
505 CreateIntToIntPlusTemp(arena_, invoke);
506}
507
508void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsInt(HInvoke* invoke) {
509 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
510}
511
512
513void IntrinsicLocationsBuilderARMVIXL::VisitMathAbsLong(HInvoke* invoke) {
514 CreateIntToIntPlusTemp(arena_, invoke);
515}
516
517void IntrinsicCodeGeneratorARMVIXL::VisitMathAbsLong(HInvoke* invoke) {
518 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
519}
520
Anton Kirilov6f644202017-02-27 18:29:45 +0000521static void GenMinMaxFloat(HInvoke* invoke, bool is_min, CodeGeneratorARMVIXL* codegen) {
522 ArmVIXLAssembler* assembler = codegen->GetAssembler();
xueliang.zhongc032e742016-03-28 16:44:32 +0100523 Location op1_loc = invoke->GetLocations()->InAt(0);
524 Location op2_loc = invoke->GetLocations()->InAt(1);
525 Location out_loc = invoke->GetLocations()->Out();
526
527 // Optimization: don't generate any code if inputs are the same.
528 if (op1_loc.Equals(op2_loc)) {
529 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
530 return;
531 }
532
533 vixl32::SRegister op1 = SRegisterFrom(op1_loc);
534 vixl32::SRegister op2 = SRegisterFrom(op2_loc);
535 vixl32::SRegister out = OutputSRegister(invoke);
536 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
537 const vixl32::Register temp1 = temps.Acquire();
538 vixl32::Register temp2 = RegisterFrom(invoke->GetLocations()->GetTemp(0));
539 vixl32::Label nan, done;
Anton Kirilov6f644202017-02-27 18:29:45 +0000540 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &done);
xueliang.zhongc032e742016-03-28 16:44:32 +0100541
542 DCHECK(op1.Is(out));
543
544 __ Vcmp(op1, op2);
545 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
546 __ B(vs, &nan, /* far_target */ false); // if un-ordered, go to NaN handling.
547
548 // op1 <> op2
549 vixl32::ConditionType cond = is_min ? gt : lt;
550 {
551 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
552 2 * kMaxInstructionSizeInBytes,
553 CodeBufferCheckScope::kMaximumSize);
554 __ it(cond);
555 __ vmov(cond, F32, out, op2);
556 }
Anton Kirilov6f644202017-02-27 18:29:45 +0000557 // for <>(not equal), we've done min/max calculation.
558 __ B(ne, final_label, /* far_target */ false);
xueliang.zhongc032e742016-03-28 16:44:32 +0100559
560 // handle op1 == op2, max(+0.0,-0.0), min(+0.0,-0.0).
561 __ Vmov(temp1, op1);
562 __ Vmov(temp2, op2);
563 if (is_min) {
564 __ Orr(temp1, temp1, temp2);
565 } else {
566 __ And(temp1, temp1, temp2);
567 }
568 __ Vmov(out, temp1);
Anton Kirilov6f644202017-02-27 18:29:45 +0000569 __ B(final_label);
xueliang.zhongc032e742016-03-28 16:44:32 +0100570
571 // handle NaN input.
572 __ Bind(&nan);
573 __ Movt(temp1, High16Bits(kNanFloat)); // 0x7FC0xxxx is a NaN.
574 __ Vmov(out, temp1);
575
Anton Kirilov6f644202017-02-27 18:29:45 +0000576 if (done.IsReferenced()) {
577 __ Bind(&done);
578 }
xueliang.zhongc032e742016-03-28 16:44:32 +0100579}
580
581static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
582 LocationSummary* locations = new (arena) LocationSummary(invoke,
583 LocationSummary::kNoCall,
584 kIntrinsified);
585 locations->SetInAt(0, Location::RequiresFpuRegister());
586 locations->SetInAt(1, Location::RequiresFpuRegister());
587 locations->SetOut(Location::SameAsFirstInput());
588}
589
590void IntrinsicLocationsBuilderARMVIXL::VisitMathMinFloatFloat(HInvoke* invoke) {
591 CreateFPFPToFPLocations(arena_, invoke);
592 invoke->GetLocations()->AddTemp(Location::RequiresRegister());
593}
594
595void IntrinsicCodeGeneratorARMVIXL::VisitMathMinFloatFloat(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000596 GenMinMaxFloat(invoke, /* is_min */ true, codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100597}
598
599void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxFloatFloat(HInvoke* invoke) {
600 CreateFPFPToFPLocations(arena_, invoke);
601 invoke->GetLocations()->AddTemp(Location::RequiresRegister());
602}
603
604void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxFloatFloat(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000605 GenMinMaxFloat(invoke, /* is_min */ false, codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100606}
607
Anton Kirilov6f644202017-02-27 18:29:45 +0000608static void GenMinMaxDouble(HInvoke* invoke, bool is_min, CodeGeneratorARMVIXL* codegen) {
609 ArmVIXLAssembler* assembler = codegen->GetAssembler();
xueliang.zhongc032e742016-03-28 16:44:32 +0100610 Location op1_loc = invoke->GetLocations()->InAt(0);
611 Location op2_loc = invoke->GetLocations()->InAt(1);
612 Location out_loc = invoke->GetLocations()->Out();
613
614 // Optimization: don't generate any code if inputs are the same.
615 if (op1_loc.Equals(op2_loc)) {
616 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in.
617 return;
618 }
619
620 vixl32::DRegister op1 = DRegisterFrom(op1_loc);
621 vixl32::DRegister op2 = DRegisterFrom(op2_loc);
622 vixl32::DRegister out = OutputDRegister(invoke);
623 vixl32::Label handle_nan_eq, done;
Anton Kirilov6f644202017-02-27 18:29:45 +0000624 vixl32::Label* final_label = codegen->GetFinalLabel(invoke, &done);
xueliang.zhongc032e742016-03-28 16:44:32 +0100625
626 DCHECK(op1.Is(out));
627
628 __ Vcmp(op1, op2);
629 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
630 __ B(vs, &handle_nan_eq, /* far_target */ false); // if un-ordered, go to NaN handling.
631
632 // op1 <> op2
633 vixl32::ConditionType cond = is_min ? gt : lt;
634 {
635 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
636 2 * kMaxInstructionSizeInBytes,
637 CodeBufferCheckScope::kMaximumSize);
638 __ it(cond);
639 __ vmov(cond, F64, out, op2);
640 }
Anton Kirilov6f644202017-02-27 18:29:45 +0000641 // for <>(not equal), we've done min/max calculation.
642 __ B(ne, final_label, /* far_target */ false);
xueliang.zhongc032e742016-03-28 16:44:32 +0100643
644 // handle op1 == op2, max(+0.0,-0.0).
645 if (!is_min) {
646 __ Vand(F64, out, op1, op2);
Anton Kirilov6f644202017-02-27 18:29:45 +0000647 __ B(final_label);
xueliang.zhongc032e742016-03-28 16:44:32 +0100648 }
649
650 // handle op1 == op2, min(+0.0,-0.0), NaN input.
651 __ Bind(&handle_nan_eq);
652 __ Vorr(F64, out, op1, op2); // assemble op1/-0.0/NaN.
653
Anton Kirilov6f644202017-02-27 18:29:45 +0000654 if (done.IsReferenced()) {
655 __ Bind(&done);
656 }
xueliang.zhongc032e742016-03-28 16:44:32 +0100657}
658
659void IntrinsicLocationsBuilderARMVIXL::VisitMathMinDoubleDouble(HInvoke* invoke) {
660 CreateFPFPToFPLocations(arena_, invoke);
661}
662
663void IntrinsicCodeGeneratorARMVIXL::VisitMathMinDoubleDouble(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000664 GenMinMaxDouble(invoke, /* is_min */ true , codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100665}
666
667void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxDoubleDouble(HInvoke* invoke) {
668 CreateFPFPToFPLocations(arena_, invoke);
669}
670
671void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Anton Kirilov6f644202017-02-27 18:29:45 +0000672 GenMinMaxDouble(invoke, /* is_min */ false, codegen_);
xueliang.zhongc032e742016-03-28 16:44:32 +0100673}
674
675static void GenMinMaxLong(HInvoke* invoke, bool is_min, ArmVIXLAssembler* assembler) {
676 Location op1_loc = invoke->GetLocations()->InAt(0);
677 Location op2_loc = invoke->GetLocations()->InAt(1);
678 Location out_loc = invoke->GetLocations()->Out();
679
680 // Optimization: don't generate any code if inputs are the same.
681 if (op1_loc.Equals(op2_loc)) {
682 DCHECK(out_loc.Equals(op1_loc)); // out_loc is set as SameAsFirstInput() in location builder.
683 return;
684 }
685
686 vixl32::Register op1_lo = LowRegisterFrom(op1_loc);
687 vixl32::Register op1_hi = HighRegisterFrom(op1_loc);
688 vixl32::Register op2_lo = LowRegisterFrom(op2_loc);
689 vixl32::Register op2_hi = HighRegisterFrom(op2_loc);
690 vixl32::Register out_lo = LowRegisterFrom(out_loc);
691 vixl32::Register out_hi = HighRegisterFrom(out_loc);
692 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
693 const vixl32::Register temp = temps.Acquire();
694
695 DCHECK(op1_lo.Is(out_lo));
696 DCHECK(op1_hi.Is(out_hi));
697
698 // Compare op1 >= op2, or op1 < op2.
699 __ Cmp(out_lo, op2_lo);
700 __ Sbcs(temp, out_hi, op2_hi);
701
702 // Now GE/LT condition code is correct for the long comparison.
703 {
704 vixl32::ConditionType cond = is_min ? ge : lt;
705 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
706 3 * kMaxInstructionSizeInBytes,
707 CodeBufferCheckScope::kMaximumSize);
708 __ itt(cond);
709 __ mov(cond, out_lo, op2_lo);
710 __ mov(cond, out_hi, op2_hi);
711 }
712}
713
714static void CreateLongLongToLongLocations(ArenaAllocator* arena, HInvoke* invoke) {
715 LocationSummary* locations = new (arena) LocationSummary(invoke,
716 LocationSummary::kNoCall,
717 kIntrinsified);
718 locations->SetInAt(0, Location::RequiresRegister());
719 locations->SetInAt(1, Location::RequiresRegister());
720 locations->SetOut(Location::SameAsFirstInput());
721}
722
723void IntrinsicLocationsBuilderARMVIXL::VisitMathMinLongLong(HInvoke* invoke) {
724 CreateLongLongToLongLocations(arena_, invoke);
725}
726
727void IntrinsicCodeGeneratorARMVIXL::VisitMathMinLongLong(HInvoke* invoke) {
728 GenMinMaxLong(invoke, /* is_min */ true, GetAssembler());
729}
730
731void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxLongLong(HInvoke* invoke) {
732 CreateLongLongToLongLocations(arena_, invoke);
733}
734
735void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxLongLong(HInvoke* invoke) {
736 GenMinMaxLong(invoke, /* is_min */ false, GetAssembler());
737}
738
Anton Kirilov5ec62182016-10-13 20:16:02 +0100739static void GenMinMax(HInvoke* invoke, bool is_min, ArmVIXLAssembler* assembler) {
740 vixl32::Register op1 = InputRegisterAt(invoke, 0);
741 vixl32::Register op2 = InputRegisterAt(invoke, 1);
742 vixl32::Register out = OutputRegister(invoke);
743
744 __ Cmp(op1, op2);
745
746 {
Artem Serov0fb37192016-12-06 18:13:40 +0000747 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
748 3 * kMaxInstructionSizeInBytes,
749 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +0100750
751 __ ite(is_min ? lt : gt);
752 __ mov(is_min ? lt : gt, out, op1);
753 __ mov(is_min ? ge : le, out, op2);
754 }
755}
756
757static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
758 LocationSummary* locations = new (arena) LocationSummary(invoke,
759 LocationSummary::kNoCall,
760 kIntrinsified);
761 locations->SetInAt(0, Location::RequiresRegister());
762 locations->SetInAt(1, Location::RequiresRegister());
763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
764}
765
766void IntrinsicLocationsBuilderARMVIXL::VisitMathMinIntInt(HInvoke* invoke) {
767 CreateIntIntToIntLocations(arena_, invoke);
768}
769
770void IntrinsicCodeGeneratorARMVIXL::VisitMathMinIntInt(HInvoke* invoke) {
771 GenMinMax(invoke, /* is_min */ true, GetAssembler());
772}
773
774void IntrinsicLocationsBuilderARMVIXL::VisitMathMaxIntInt(HInvoke* invoke) {
775 CreateIntIntToIntLocations(arena_, invoke);
776}
777
778void IntrinsicCodeGeneratorARMVIXL::VisitMathMaxIntInt(HInvoke* invoke) {
779 GenMinMax(invoke, /* is_min */ false, GetAssembler());
780}
781
782void IntrinsicLocationsBuilderARMVIXL::VisitMathSqrt(HInvoke* invoke) {
783 CreateFPToFPLocations(arena_, invoke);
784}
785
786void IntrinsicCodeGeneratorARMVIXL::VisitMathSqrt(HInvoke* invoke) {
787 ArmVIXLAssembler* assembler = GetAssembler();
788 __ Vsqrt(OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
789}
790
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100791void IntrinsicLocationsBuilderARMVIXL::VisitMathRint(HInvoke* invoke) {
792 if (features_.HasARMv8AInstructions()) {
793 CreateFPToFPLocations(arena_, invoke);
794 }
795}
796
797void IntrinsicCodeGeneratorARMVIXL::VisitMathRint(HInvoke* invoke) {
798 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
799 ArmVIXLAssembler* assembler = GetAssembler();
800 __ Vrintn(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
801}
802
xueliang.zhong53463ba2017-02-16 15:18:03 +0000803void IntrinsicLocationsBuilderARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
804 if (features_.HasARMv8AInstructions()) {
805 LocationSummary* locations = new (arena_) LocationSummary(invoke,
806 LocationSummary::kNoCall,
807 kIntrinsified);
808 locations->SetInAt(0, Location::RequiresFpuRegister());
809 locations->SetOut(Location::RequiresRegister());
810 locations->AddTemp(Location::RequiresFpuRegister());
811 }
812}
813
814void IntrinsicCodeGeneratorARMVIXL::VisitMathRoundFloat(HInvoke* invoke) {
815 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
816
817 ArmVIXLAssembler* assembler = GetAssembler();
818 vixl32::SRegister in_reg = InputSRegisterAt(invoke, 0);
819 vixl32::Register out_reg = OutputRegister(invoke);
820 vixl32::SRegister temp1 = LowSRegisterFrom(invoke->GetLocations()->GetTemp(0));
821 vixl32::SRegister temp2 = HighSRegisterFrom(invoke->GetLocations()->GetTemp(0));
822 vixl32::Label done;
823 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
824
825 // Round to nearest integer, ties away from zero.
826 __ Vcvta(S32, F32, temp1, in_reg);
827 __ Vmov(out_reg, temp1);
828
829 // For positive, zero or NaN inputs, rounding is done.
830 __ Cmp(out_reg, 0);
831 __ B(ge, final_label, /* far_target */ false);
832
833 // Handle input < 0 cases.
834 // If input is negative but not a tie, previous result (round to nearest) is valid.
835 // If input is a negative tie, change rounding direction to positive infinity, out_reg += 1.
836 __ Vrinta(F32, F32, temp1, in_reg);
837 __ Vmov(temp2, 0.5);
838 __ Vsub(F32, temp1, in_reg, temp1);
839 __ Vcmp(F32, temp1, temp2);
840 __ Vmrs(RegisterOrAPSR_nzcv(kPcCode), FPSCR);
841 {
842 // Use ExactAsemblyScope here because we are using IT.
843 ExactAssemblyScope it_scope(assembler->GetVIXLAssembler(),
844 2 * kMaxInstructionSizeInBytes,
845 CodeBufferCheckScope::kMaximumSize);
846 __ it(eq);
847 __ add(eq, out_reg, out_reg, 1);
848 }
849
850 if (done.IsReferenced()) {
851 __ Bind(&done);
852 }
853}
854
Anton Kirilov5ec62182016-10-13 20:16:02 +0100855void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
856 CreateIntToIntLocations(arena_, invoke);
857}
858
859void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekByte(HInvoke* invoke) {
860 ArmVIXLAssembler* assembler = GetAssembler();
861 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000862 __ Ldrsb(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100863}
864
865void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
866 CreateIntToIntLocations(arena_, invoke);
867}
868
869void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekIntNative(HInvoke* invoke) {
870 ArmVIXLAssembler* assembler = GetAssembler();
871 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000872 __ Ldr(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100873}
874
875void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
876 CreateIntToIntLocations(arena_, invoke);
877}
878
879void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekLongNative(HInvoke* invoke) {
880 ArmVIXLAssembler* assembler = GetAssembler();
881 // Ignore upper 4B of long address.
882 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
883 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
884 // exception. So we can't use ldrd as addr may be unaligned.
885 vixl32::Register lo = LowRegisterFrom(invoke->GetLocations()->Out());
886 vixl32::Register hi = HighRegisterFrom(invoke->GetLocations()->Out());
887 if (addr.Is(lo)) {
888 __ Ldr(hi, MemOperand(addr, 4));
Scott Wakelingb77051e2016-11-21 19:46:00 +0000889 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100890 } else {
Scott Wakelingb77051e2016-11-21 19:46:00 +0000891 __ Ldr(lo, MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100892 __ Ldr(hi, MemOperand(addr, 4));
893 }
894}
895
896void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
897 CreateIntToIntLocations(arena_, invoke);
898}
899
900void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPeekShortNative(HInvoke* invoke) {
901 ArmVIXLAssembler* assembler = GetAssembler();
902 // Ignore upper 4B of long address.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000903 __ Ldrsh(OutputRegister(invoke), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100904}
905
906static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
907 LocationSummary* locations = new (arena) LocationSummary(invoke,
908 LocationSummary::kNoCall,
909 kIntrinsified);
910 locations->SetInAt(0, Location::RequiresRegister());
911 locations->SetInAt(1, Location::RequiresRegister());
912}
913
914void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
915 CreateIntIntToVoidLocations(arena_, invoke);
916}
917
918void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeByte(HInvoke* invoke) {
919 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000920 __ Strb(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100921}
922
923void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
924 CreateIntIntToVoidLocations(arena_, invoke);
925}
926
927void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeIntNative(HInvoke* invoke) {
928 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000929 __ Str(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100930}
931
932void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
933 CreateIntIntToVoidLocations(arena_, invoke);
934}
935
936void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeLongNative(HInvoke* invoke) {
937 ArmVIXLAssembler* assembler = GetAssembler();
938 // Ignore upper 4B of long address.
939 vixl32::Register addr = LowRegisterFrom(invoke->GetLocations()->InAt(0));
940 // Worst case: Control register bit SCTLR.A = 0. Then unaligned accesses throw a processor
941 // exception. So we can't use ldrd as addr may be unaligned.
Scott Wakelingb77051e2016-11-21 19:46:00 +0000942 __ Str(LowRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100943 __ Str(HighRegisterFrom(invoke->GetLocations()->InAt(1)), MemOperand(addr, 4));
944}
945
946void IntrinsicLocationsBuilderARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
947 CreateIntIntToVoidLocations(arena_, invoke);
948}
949
950void IntrinsicCodeGeneratorARMVIXL::VisitMemoryPokeShortNative(HInvoke* invoke) {
951 ArmVIXLAssembler* assembler = GetAssembler();
Scott Wakelingb77051e2016-11-21 19:46:00 +0000952 __ Strh(InputRegisterAt(invoke, 1), MemOperand(LowRegisterFrom(invoke->GetLocations()->InAt(0))));
Anton Kirilov5ec62182016-10-13 20:16:02 +0100953}
954
955void IntrinsicLocationsBuilderARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
956 LocationSummary* locations = new (arena_) LocationSummary(invoke,
957 LocationSummary::kNoCall,
958 kIntrinsified);
959 locations->SetOut(Location::RequiresRegister());
960}
961
962void IntrinsicCodeGeneratorARMVIXL::VisitThreadCurrentThread(HInvoke* invoke) {
963 ArmVIXLAssembler* assembler = GetAssembler();
964 __ Ldr(OutputRegister(invoke),
965 MemOperand(tr, Thread::PeerOffset<kArmPointerSize>().Int32Value()));
966}
967
968static void GenUnsafeGet(HInvoke* invoke,
969 Primitive::Type type,
970 bool is_volatile,
971 CodeGeneratorARMVIXL* codegen) {
972 LocationSummary* locations = invoke->GetLocations();
973 ArmVIXLAssembler* assembler = codegen->GetAssembler();
974 Location base_loc = locations->InAt(1);
975 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
976 Location offset_loc = locations->InAt(2);
977 vixl32::Register offset = LowRegisterFrom(offset_loc); // Long offset, lo part only.
978 Location trg_loc = locations->Out();
979
980 switch (type) {
981 case Primitive::kPrimInt: {
982 vixl32::Register trg = RegisterFrom(trg_loc);
983 __ Ldr(trg, MemOperand(base, offset));
984 if (is_volatile) {
985 __ Dmb(vixl32::ISH);
986 }
987 break;
988 }
989
990 case Primitive::kPrimNot: {
991 vixl32::Register trg = RegisterFrom(trg_loc);
992 if (kEmitCompilerReadBarrier) {
993 if (kUseBakerReadBarrier) {
994 Location temp = locations->GetTemp(0);
995 codegen->GenerateReferenceLoadWithBakerReadBarrier(
996 invoke, trg_loc, base, 0U, offset_loc, TIMES_1, temp, /* needs_null_check */ false);
997 if (is_volatile) {
998 __ Dmb(vixl32::ISH);
999 }
1000 } else {
1001 __ Ldr(trg, MemOperand(base, offset));
1002 if (is_volatile) {
1003 __ Dmb(vixl32::ISH);
1004 }
1005 codegen->GenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
1006 }
1007 } else {
1008 __ Ldr(trg, MemOperand(base, offset));
1009 if (is_volatile) {
1010 __ Dmb(vixl32::ISH);
1011 }
1012 assembler->MaybeUnpoisonHeapReference(trg);
1013 }
1014 break;
1015 }
1016
1017 case Primitive::kPrimLong: {
1018 vixl32::Register trg_lo = LowRegisterFrom(trg_loc);
1019 vixl32::Register trg_hi = HighRegisterFrom(trg_loc);
1020 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
Artem Serov657022c2016-11-23 14:19:38 +00001021 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1022 const vixl32::Register temp_reg = temps.Acquire();
1023 __ Add(temp_reg, base, offset);
1024 __ Ldrexd(trg_lo, trg_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001025 } else {
1026 __ Ldrd(trg_lo, trg_hi, MemOperand(base, offset));
1027 }
1028 if (is_volatile) {
1029 __ Dmb(vixl32::ISH);
1030 }
1031 break;
1032 }
1033
1034 default:
1035 LOG(FATAL) << "Unexpected type " << type;
1036 UNREACHABLE();
1037 }
1038}
1039
1040static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1041 HInvoke* invoke,
1042 Primitive::Type type) {
1043 bool can_call = kEmitCompilerReadBarrier &&
1044 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1045 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
1046 LocationSummary* locations = new (arena) LocationSummary(invoke,
1047 (can_call
1048 ? LocationSummary::kCallOnSlowPath
1049 : LocationSummary::kNoCall),
1050 kIntrinsified);
1051 if (can_call && kUseBakerReadBarrier) {
1052 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
1053 }
1054 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1055 locations->SetInAt(1, Location::RequiresRegister());
1056 locations->SetInAt(2, Location::RequiresRegister());
1057 locations->SetOut(Location::RequiresRegister(),
1058 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
1059 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1060 // We need a temporary register for the read barrier marking slow
Roland Levillain9983e302017-07-14 14:34:22 +01001061 // path in CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001062 locations->AddTemp(Location::RequiresRegister());
1063 }
1064}
1065
1066void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
1067 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
1068}
1069void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
1070 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
1071}
1072void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
1073 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
1074}
1075void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1076 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
1077}
1078void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
1079 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
1080}
1081void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1082 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
1083}
1084
1085void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGet(HInvoke* invoke) {
1086 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
1087}
1088void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetVolatile(HInvoke* invoke) {
1089 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
1090}
1091void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLong(HInvoke* invoke) {
1092 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
1093}
1094void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1095 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
1096}
1097void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObject(HInvoke* invoke) {
1098 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
1099}
1100void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1101 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
1102}
1103
1104static void CreateIntIntIntIntToVoid(ArenaAllocator* arena,
1105 const ArmInstructionSetFeatures& features,
1106 Primitive::Type type,
1107 bool is_volatile,
1108 HInvoke* invoke) {
1109 LocationSummary* locations = new (arena) LocationSummary(invoke,
1110 LocationSummary::kNoCall,
1111 kIntrinsified);
1112 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1113 locations->SetInAt(1, Location::RequiresRegister());
1114 locations->SetInAt(2, Location::RequiresRegister());
1115 locations->SetInAt(3, Location::RequiresRegister());
1116
1117 if (type == Primitive::kPrimLong) {
1118 // Potentially need temps for ldrexd-strexd loop.
1119 if (is_volatile && !features.HasAtomicLdrdAndStrd()) {
1120 locations->AddTemp(Location::RequiresRegister()); // Temp_lo.
1121 locations->AddTemp(Location::RequiresRegister()); // Temp_hi.
1122 }
1123 } else if (type == Primitive::kPrimNot) {
1124 // Temps for card-marking.
1125 locations->AddTemp(Location::RequiresRegister()); // Temp.
1126 locations->AddTemp(Location::RequiresRegister()); // Card.
1127 }
1128}
1129
1130void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePut(HInvoke* invoke) {
1131 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ false, invoke);
1132}
1133void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
1134 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ false, invoke);
1135}
1136void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
1137 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimInt, /* is_volatile */ true, invoke);
1138}
1139void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
1140 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ false, invoke);
1141}
1142void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1143 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ false, invoke);
1144}
1145void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1146 CreateIntIntIntIntToVoid(arena_, features_, Primitive::kPrimNot, /* is_volatile */ true, invoke);
1147}
1148void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
1149 CreateIntIntIntIntToVoid(
1150 arena_, features_, Primitive::kPrimLong, /* is_volatile */ false, invoke);
1151}
1152void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1153 CreateIntIntIntIntToVoid(
1154 arena_, features_, Primitive::kPrimLong, /* is_volatile */ false, invoke);
1155}
1156void IntrinsicLocationsBuilderARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1157 CreateIntIntIntIntToVoid(
1158 arena_, features_, Primitive::kPrimLong, /* is_volatile */ true, invoke);
1159}
1160
1161static void GenUnsafePut(LocationSummary* locations,
1162 Primitive::Type type,
1163 bool is_volatile,
1164 bool is_ordered,
1165 CodeGeneratorARMVIXL* codegen) {
1166 ArmVIXLAssembler* assembler = codegen->GetAssembler();
1167
1168 vixl32::Register base = RegisterFrom(locations->InAt(1)); // Object pointer.
1169 vixl32::Register offset = LowRegisterFrom(locations->InAt(2)); // Long offset, lo part only.
1170 vixl32::Register value;
1171
1172 if (is_volatile || is_ordered) {
1173 __ Dmb(vixl32::ISH);
1174 }
1175
1176 if (type == Primitive::kPrimLong) {
1177 vixl32::Register value_lo = LowRegisterFrom(locations->InAt(3));
1178 vixl32::Register value_hi = HighRegisterFrom(locations->InAt(3));
1179 value = value_lo;
1180 if (is_volatile && !codegen->GetInstructionSetFeatures().HasAtomicLdrdAndStrd()) {
1181 vixl32::Register temp_lo = RegisterFrom(locations->GetTemp(0));
1182 vixl32::Register temp_hi = RegisterFrom(locations->GetTemp(1));
1183 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1184 const vixl32::Register temp_reg = temps.Acquire();
1185
1186 __ Add(temp_reg, base, offset);
1187 vixl32::Label loop_head;
1188 __ Bind(&loop_head);
Scott Wakelingb77051e2016-11-21 19:46:00 +00001189 __ Ldrexd(temp_lo, temp_hi, MemOperand(temp_reg));
1190 __ Strexd(temp_lo, value_lo, value_hi, MemOperand(temp_reg));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001191 __ Cmp(temp_lo, 0);
Artem Serov517d9f62016-12-12 15:51:15 +00001192 __ B(ne, &loop_head, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001193 } else {
1194 __ Strd(value_lo, value_hi, MemOperand(base, offset));
1195 }
1196 } else {
1197 value = RegisterFrom(locations->InAt(3));
1198 vixl32::Register source = value;
1199 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1200 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
1201 __ Mov(temp, value);
1202 assembler->PoisonHeapReference(temp);
1203 source = temp;
1204 }
1205 __ Str(source, MemOperand(base, offset));
1206 }
1207
1208 if (is_volatile) {
1209 __ Dmb(vixl32::ISH);
1210 }
1211
1212 if (type == Primitive::kPrimNot) {
1213 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
1214 vixl32::Register card = RegisterFrom(locations->GetTemp(1));
1215 bool value_can_be_null = true; // TODO: Worth finding out this information?
1216 codegen->MarkGCCard(temp, card, base, value, value_can_be_null);
1217 }
1218}
1219
1220void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePut(HInvoke* invoke) {
1221 GenUnsafePut(invoke->GetLocations(),
1222 Primitive::kPrimInt,
1223 /* is_volatile */ false,
1224 /* is_ordered */ false,
1225 codegen_);
1226}
1227void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutOrdered(HInvoke* invoke) {
1228 GenUnsafePut(invoke->GetLocations(),
1229 Primitive::kPrimInt,
1230 /* is_volatile */ false,
1231 /* is_ordered */ true,
1232 codegen_);
1233}
1234void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutVolatile(HInvoke* invoke) {
1235 GenUnsafePut(invoke->GetLocations(),
1236 Primitive::kPrimInt,
1237 /* is_volatile */ true,
1238 /* is_ordered */ false,
1239 codegen_);
1240}
1241void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObject(HInvoke* invoke) {
1242 GenUnsafePut(invoke->GetLocations(),
1243 Primitive::kPrimNot,
1244 /* is_volatile */ false,
1245 /* is_ordered */ false,
1246 codegen_);
1247}
1248void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1249 GenUnsafePut(invoke->GetLocations(),
1250 Primitive::kPrimNot,
1251 /* is_volatile */ false,
1252 /* is_ordered */ true,
1253 codegen_);
1254}
1255void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1256 GenUnsafePut(invoke->GetLocations(),
1257 Primitive::kPrimNot,
1258 /* is_volatile */ true,
1259 /* is_ordered */ false,
1260 codegen_);
1261}
1262void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLong(HInvoke* invoke) {
1263 GenUnsafePut(invoke->GetLocations(),
1264 Primitive::kPrimLong,
1265 /* is_volatile */ false,
1266 /* is_ordered */ false,
1267 codegen_);
1268}
1269void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1270 GenUnsafePut(invoke->GetLocations(),
1271 Primitive::kPrimLong,
1272 /* is_volatile */ false,
1273 /* is_ordered */ true,
1274 codegen_);
1275}
1276void IntrinsicCodeGeneratorARMVIXL::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1277 GenUnsafePut(invoke->GetLocations(),
1278 Primitive::kPrimLong,
1279 /* is_volatile */ true,
1280 /* is_ordered */ false,
1281 codegen_);
1282}
1283
1284static void CreateIntIntIntIntIntToIntPlusTemps(ArenaAllocator* arena,
1285 HInvoke* invoke,
1286 Primitive::Type type) {
1287 bool can_call = kEmitCompilerReadBarrier &&
1288 kUseBakerReadBarrier &&
1289 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
1290 LocationSummary* locations = new (arena) LocationSummary(invoke,
1291 (can_call
1292 ? LocationSummary::kCallOnSlowPath
1293 : LocationSummary::kNoCall),
1294 kIntrinsified);
1295 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1296 locations->SetInAt(1, Location::RequiresRegister());
1297 locations->SetInAt(2, Location::RequiresRegister());
1298 locations->SetInAt(3, Location::RequiresRegister());
1299 locations->SetInAt(4, Location::RequiresRegister());
1300
1301 // If heap poisoning is enabled, we don't want the unpoisoning
1302 // operations to potentially clobber the output. Likewise when
1303 // emitting a (Baker) read barrier, which may call.
1304 Location::OutputOverlap overlaps =
1305 ((kPoisonHeapReferences && type == Primitive::kPrimNot) || can_call)
1306 ? Location::kOutputOverlap
1307 : Location::kNoOutputOverlap;
1308 locations->SetOut(Location::RequiresRegister(), overlaps);
1309
1310 // Temporary registers used in CAS. In the object case
1311 // (UnsafeCASObject intrinsic), these are also used for
1312 // card-marking, and possibly for (Baker) read barrier.
1313 locations->AddTemp(Location::RequiresRegister()); // Pointer.
1314 locations->AddTemp(Location::RequiresRegister()); // Temp 1.
1315}
1316
1317static void GenCas(HInvoke* invoke, Primitive::Type type, CodeGeneratorARMVIXL* codegen) {
1318 DCHECK_NE(type, Primitive::kPrimLong);
1319
1320 ArmVIXLAssembler* assembler = codegen->GetAssembler();
1321 LocationSummary* locations = invoke->GetLocations();
1322
1323 Location out_loc = locations->Out();
1324 vixl32::Register out = OutputRegister(invoke); // Boolean result.
1325
1326 vixl32::Register base = InputRegisterAt(invoke, 1); // Object pointer.
1327 Location offset_loc = locations->InAt(2);
1328 vixl32::Register offset = LowRegisterFrom(offset_loc); // Offset (discard high 4B).
1329 vixl32::Register expected = InputRegisterAt(invoke, 3); // Expected.
1330 vixl32::Register value = InputRegisterAt(invoke, 4); // Value.
1331
1332 Location tmp_ptr_loc = locations->GetTemp(0);
1333 vixl32::Register tmp_ptr = RegisterFrom(tmp_ptr_loc); // Pointer to actual memory.
1334 vixl32::Register tmp = RegisterFrom(locations->GetTemp(1)); // Value in memory.
1335
1336 if (type == Primitive::kPrimNot) {
1337 // The only read barrier implementation supporting the
1338 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1339 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1340
1341 // Mark card for object assuming new value is stored. Worst case we will mark an unchanged
1342 // object and scan the receiver at the next GC for nothing.
1343 bool value_can_be_null = true; // TODO: Worth finding out this information?
1344 codegen->MarkGCCard(tmp_ptr, tmp, base, value, value_can_be_null);
1345
1346 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1347 // Need to make sure the reference stored in the field is a to-space
1348 // one before attempting the CAS or the CAS could fail incorrectly.
Roland Levillainff487002017-03-07 16:50:01 +00001349 codegen->UpdateReferenceFieldWithBakerReadBarrier(
Anton Kirilov5ec62182016-10-13 20:16:02 +01001350 invoke,
1351 out_loc, // Unused, used only as a "temporary" within the read barrier.
1352 base,
Roland Levillainff487002017-03-07 16:50:01 +00001353 /* field_offset */ offset_loc,
Anton Kirilov5ec62182016-10-13 20:16:02 +01001354 tmp_ptr_loc,
1355 /* needs_null_check */ false,
Roland Levillainff487002017-03-07 16:50:01 +00001356 tmp);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001357 }
1358 }
1359
1360 // Prevent reordering with prior memory operations.
1361 // Emit a DMB ISH instruction instead of an DMB ISHST one, as the
1362 // latter allows a preceding load to be delayed past the STXR
1363 // instruction below.
1364 __ Dmb(vixl32::ISH);
1365
1366 __ Add(tmp_ptr, base, offset);
1367
1368 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1369 codegen->GetAssembler()->PoisonHeapReference(expected);
1370 if (value.Is(expected)) {
1371 // Do not poison `value`, as it is the same register as
1372 // `expected`, which has just been poisoned.
1373 } else {
1374 codegen->GetAssembler()->PoisonHeapReference(value);
1375 }
1376 }
1377
1378 // do {
1379 // tmp = [r_ptr] - expected;
1380 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1381 // result = tmp != 0;
1382
1383 vixl32::Label loop_head;
1384 __ Bind(&loop_head);
1385
Scott Wakelingb77051e2016-11-21 19:46:00 +00001386 __ Ldrex(tmp, MemOperand(tmp_ptr));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001387
1388 __ Subs(tmp, tmp, expected);
1389
1390 {
Artem Serov0fb37192016-12-06 18:13:40 +00001391 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1392 3 * kMaxInstructionSizeInBytes,
1393 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001394
1395 __ itt(eq);
Scott Wakelingb77051e2016-11-21 19:46:00 +00001396 __ strex(eq, tmp, value, MemOperand(tmp_ptr));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001397 __ cmp(eq, tmp, 1);
1398 }
1399
Artem Serov517d9f62016-12-12 15:51:15 +00001400 __ B(eq, &loop_head, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001401
1402 __ Dmb(vixl32::ISH);
1403
1404 __ Rsbs(out, tmp, 1);
1405
1406 {
Artem Serov0fb37192016-12-06 18:13:40 +00001407 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1408 2 * kMaxInstructionSizeInBytes,
1409 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001410
1411 __ it(cc);
1412 __ mov(cc, out, 0);
1413 }
1414
1415 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1416 codegen->GetAssembler()->UnpoisonHeapReference(expected);
1417 if (value.Is(expected)) {
1418 // Do not unpoison `value`, as it is the same register as
1419 // `expected`, which has just been unpoisoned.
1420 } else {
1421 codegen->GetAssembler()->UnpoisonHeapReference(value);
1422 }
1423 }
1424}
1425
1426void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
1427 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke, Primitive::kPrimInt);
1428}
1429void IntrinsicLocationsBuilderARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1430 // The only read barrier implementation supporting the
1431 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1432 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
1433 return;
1434 }
1435
1436 CreateIntIntIntIntIntToIntPlusTemps(arena_, invoke, Primitive::kPrimNot);
1437}
1438void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASInt(HInvoke* invoke) {
1439 GenCas(invoke, Primitive::kPrimInt, codegen_);
1440}
1441void IntrinsicCodeGeneratorARMVIXL::VisitUnsafeCASObject(HInvoke* invoke) {
1442 // The only read barrier implementation supporting the
1443 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1444 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1445
1446 GenCas(invoke, Primitive::kPrimNot, codegen_);
1447}
1448
1449void IntrinsicLocationsBuilderARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1450 // The inputs plus one temp.
1451 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1452 invoke->InputAt(1)->CanBeNull()
1453 ? LocationSummary::kCallOnSlowPath
1454 : LocationSummary::kNoCall,
1455 kIntrinsified);
1456 locations->SetInAt(0, Location::RequiresRegister());
1457 locations->SetInAt(1, Location::RequiresRegister());
1458 locations->AddTemp(Location::RequiresRegister());
1459 locations->AddTemp(Location::RequiresRegister());
1460 locations->AddTemp(Location::RequiresRegister());
1461 // Need temporary registers for String compression's feature.
1462 if (mirror::kUseStringCompression) {
1463 locations->AddTemp(Location::RequiresRegister());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001464 }
1465 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1466}
1467
1468void IntrinsicCodeGeneratorARMVIXL::VisitStringCompareTo(HInvoke* invoke) {
1469 ArmVIXLAssembler* assembler = GetAssembler();
1470 LocationSummary* locations = invoke->GetLocations();
1471
1472 vixl32::Register str = InputRegisterAt(invoke, 0);
1473 vixl32::Register arg = InputRegisterAt(invoke, 1);
1474 vixl32::Register out = OutputRegister(invoke);
1475
1476 vixl32::Register temp0 = RegisterFrom(locations->GetTemp(0));
1477 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1478 vixl32::Register temp2 = RegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001479 vixl32::Register temp3;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001480 if (mirror::kUseStringCompression) {
1481 temp3 = RegisterFrom(locations->GetTemp(3));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001482 }
1483
1484 vixl32::Label loop;
1485 vixl32::Label find_char_diff;
1486 vixl32::Label end;
1487 vixl32::Label different_compression;
1488
1489 // Get offsets of count and value fields within a string object.
1490 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1491 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1492
1493 // Note that the null check must have been done earlier.
1494 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1495
1496 // Take slow path and throw if input can be and is null.
1497 SlowPathCodeARMVIXL* slow_path = nullptr;
1498 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1499 if (can_slow_path) {
1500 slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
1501 codegen_->AddSlowPath(slow_path);
xueliang.zhongf51bc622016-11-04 09:23:32 +00001502 __ CompareAndBranchIfZero(arg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01001503 }
1504
1505 // Reference equality check, return 0 if same reference.
1506 __ Subs(out, str, arg);
1507 __ B(eq, &end);
1508
Anton Kirilov5ec62182016-10-13 20:16:02 +01001509 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001510 // Load `count` fields of this and argument strings.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001511 __ Ldr(temp3, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001512 __ Ldr(temp2, MemOperand(arg, count_offset));
1513 // Extract lengths from the `count` fields.
1514 __ Lsr(temp0, temp3, 1u);
1515 __ Lsr(temp1, temp2, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001516 } else {
1517 // Load lengths of this and argument strings.
1518 __ Ldr(temp0, MemOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001519 __ Ldr(temp1, MemOperand(arg, count_offset));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001520 }
1521 // out = length diff.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001522 __ Subs(out, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001523 // temp0 = min(len(str), len(arg)).
1524
1525 {
Artem Serov0fb37192016-12-06 18:13:40 +00001526 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1527 2 * kMaxInstructionSizeInBytes,
1528 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001529
1530 __ it(gt);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001531 __ mov(gt, temp0, temp1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001532 }
1533
Anton Kirilov5ec62182016-10-13 20:16:02 +01001534 // Shorter string is empty?
xueliang.zhongf51bc622016-11-04 09:23:32 +00001535 // Note that mirror::kUseStringCompression==true introduces lots of instructions,
1536 // which makes &end label far away from this branch and makes it not 'CBZ-encodable'.
1537 __ CompareAndBranchIfZero(temp0, &end, mirror::kUseStringCompression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001538
1539 if (mirror::kUseStringCompression) {
1540 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001541 __ Eors(temp2, temp2, temp3);
1542 __ Lsrs(temp2, temp2, 1u);
1543 __ B(cs, &different_compression);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001544 // For string compression, calculate the number of bytes to compare (not chars).
1545 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001546 __ Lsls(temp3, temp3, 31u); // Extract purely the compression flag.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001547
Artem Serov0fb37192016-12-06 18:13:40 +00001548 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1549 2 * kMaxInstructionSizeInBytes,
1550 CodeBufferCheckScope::kMaximumSize);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001551
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001552 __ it(ne);
1553 __ add(ne, temp0, temp0, temp0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001554 }
1555
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001556 // Store offset of string value in preparation for comparison loop.
1557 __ Mov(temp1, value_offset);
1558
Anton Kirilov5ec62182016-10-13 20:16:02 +01001559 // Assertions that must hold in order to compare multiple characters at a time.
1560 CHECK_ALIGNED(value_offset, 8);
1561 static_assert(IsAligned<8>(kObjectAlignment),
1562 "String data must be 8-byte aligned for unrolled CompareTo loop.");
1563
Scott Wakelingb77051e2016-11-21 19:46:00 +00001564 const unsigned char_size = Primitive::ComponentSize(Primitive::kPrimChar);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001565 DCHECK_EQ(char_size, 2u);
1566
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001567 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
1568
Anton Kirilov5ec62182016-10-13 20:16:02 +01001569 vixl32::Label find_char_diff_2nd_cmp;
1570 // Unrolled loop comparing 4x16-bit chars per iteration (ok because of string data alignment).
1571 __ Bind(&loop);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001572 vixl32::Register temp_reg = temps.Acquire();
Anton Kirilov5ec62182016-10-13 20:16:02 +01001573 __ Ldr(temp_reg, MemOperand(str, temp1));
1574 __ Ldr(temp2, MemOperand(arg, temp1));
1575 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001576 __ B(ne, &find_char_diff, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001577 __ Add(temp1, temp1, char_size * 2);
1578
1579 __ Ldr(temp_reg, MemOperand(str, temp1));
1580 __ Ldr(temp2, MemOperand(arg, temp1));
1581 __ Cmp(temp_reg, temp2);
Artem Serov517d9f62016-12-12 15:51:15 +00001582 __ B(ne, &find_char_diff_2nd_cmp, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001583 __ Add(temp1, temp1, char_size * 2);
1584 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1585 __ Subs(temp0, temp0, (mirror::kUseStringCompression ? 8 : 4));
Artem Serov517d9f62016-12-12 15:51:15 +00001586 __ B(hi, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001587 __ B(&end);
1588
1589 __ Bind(&find_char_diff_2nd_cmp);
1590 if (mirror::kUseStringCompression) {
1591 __ Subs(temp0, temp0, 4); // 4 bytes previously compared.
Artem Serov517d9f62016-12-12 15:51:15 +00001592 __ B(ls, &end, /* far_target */ false); // Was the second comparison fully beyond the end?
Anton Kirilov5ec62182016-10-13 20:16:02 +01001593 } else {
1594 // Without string compression, we can start treating temp0 as signed
1595 // and rely on the signed comparison below.
1596 __ Sub(temp0, temp0, 2);
1597 }
1598
1599 // Find the single character difference.
1600 __ Bind(&find_char_diff);
1601 // Get the bit position of the first character that differs.
1602 __ Eor(temp1, temp2, temp_reg);
1603 __ Rbit(temp1, temp1);
1604 __ Clz(temp1, temp1);
1605
1606 // temp0 = number of characters remaining to compare.
1607 // (Without string compression, it could be < 1 if a difference is found by the second CMP
1608 // in the comparison loop, and after the end of the shorter string data).
1609
1610 // Without string compression (temp1 >> 4) = character where difference occurs between the last
1611 // two words compared, in the interval [0,1].
1612 // (0 for low half-word different, 1 for high half-word different).
1613 // With string compression, (temp1 << 3) = byte where the difference occurs,
1614 // in the interval [0,3].
1615
1616 // If temp0 <= (temp1 >> (kUseStringCompression ? 3 : 4)), the difference occurs outside
1617 // the remaining string data, so just return length diff (out).
1618 // The comparison is unsigned for string compression, otherwise signed.
1619 __ Cmp(temp0, Operand(temp1, vixl32::LSR, (mirror::kUseStringCompression ? 3 : 4)));
Artem Serov517d9f62016-12-12 15:51:15 +00001620 __ B((mirror::kUseStringCompression ? ls : le), &end, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001621
Anton Kirilov5ec62182016-10-13 20:16:02 +01001622 // Extract the characters and calculate the difference.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001623 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001624 // For compressed strings we need to clear 0x7 from temp1, for uncompressed we need to clear
1625 // 0xf. We also need to prepare the character extraction mask `uncompressed ? 0xffffu : 0xffu`.
1626 // The compression flag is now in the highest bit of temp3, so let's play some tricks.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001627 __ Orr(temp3, temp3, 0xffu << 23); // uncompressed ? 0xff800000u : 0x7ff80000u
1628 __ Bic(temp1, temp1, Operand(temp3, vixl32::LSR, 31 - 3)); // &= ~(uncompressed ? 0xfu : 0x7u)
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001629 __ Asr(temp3, temp3, 7u); // uncompressed ? 0xffff0000u : 0xff0000u.
1630 __ Lsr(temp2, temp2, temp1); // Extract second character.
1631 __ Lsr(temp3, temp3, 16u); // uncompressed ? 0xffffu : 0xffu
1632 __ Lsr(out, temp_reg, temp1); // Extract first character.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001633 __ And(temp2, temp2, temp3);
1634 __ And(out, out, temp3);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001635 } else {
Anton Kirilovb88c4842016-11-14 14:37:00 +00001636 __ Bic(temp1, temp1, 0xf);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001637 __ Lsr(temp2, temp2, temp1);
1638 __ Lsr(out, temp_reg, temp1);
Anton Kirilovb88c4842016-11-14 14:37:00 +00001639 __ Movt(temp2, 0);
1640 __ Movt(out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001641 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001642
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001643 __ Sub(out, out, temp2);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001644 temps.Release(temp_reg);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001645
1646 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001647 __ B(&end);
1648 __ Bind(&different_compression);
1649
1650 // Comparison for different compression style.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001651 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1652 DCHECK_EQ(c_char_size, 1u);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001653
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001654 // We want to free up the temp3, currently holding `str.count`, for comparison.
1655 // So, we move it to the bottom bit of the iteration count `temp0` which we tnen
1656 // need to treat as unsigned. Start by freeing the bit with an ADD and continue
1657 // further down by a LSRS+SBC which will flip the meaning of the flag but allow
1658 // `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001659 __ Add(temp0, temp0, temp0); // Unlike LSL, this ADD is always 16-bit.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001660 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001661 __ Mov(temp1, str);
1662 __ Mov(temp2, arg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001663 __ Lsrs(temp3, temp3, 1u); // Continue the move of the compression flag.
1664 {
Artem Serov0fb37192016-12-06 18:13:40 +00001665 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1666 3 * kMaxInstructionSizeInBytes,
1667 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001668 __ itt(cs); // Interleave with selection of temp1 and temp2.
1669 __ mov(cs, temp1, arg); // Preserves flags.
1670 __ mov(cs, temp2, str); // Preserves flags.
1671 }
Anton Kirilovb88c4842016-11-14 14:37:00 +00001672 __ Sbc(temp0, temp0, 0); // Complete the move of the compression flag.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001673
1674 // Adjust temp1 and temp2 from string pointers to data pointers.
Anton Kirilovb88c4842016-11-14 14:37:00 +00001675 __ Add(temp1, temp1, value_offset);
1676 __ Add(temp2, temp2, value_offset);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001677
1678 vixl32::Label different_compression_loop;
1679 vixl32::Label different_compression_diff;
1680
1681 // Main loop for different compression.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001682 temp_reg = temps.Acquire();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001683 __ Bind(&different_compression_loop);
1684 __ Ldrb(temp_reg, MemOperand(temp1, c_char_size, PostIndex));
1685 __ Ldrh(temp3, MemOperand(temp2, char_size, PostIndex));
Anton Kirilovb88c4842016-11-14 14:37:00 +00001686 __ Cmp(temp_reg, temp3);
Artem Serov517d9f62016-12-12 15:51:15 +00001687 __ B(ne, &different_compression_diff, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001688 __ Subs(temp0, temp0, 2);
Artem Serov517d9f62016-12-12 15:51:15 +00001689 __ B(hi, &different_compression_loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001690 __ B(&end);
1691
1692 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001693 __ Bind(&different_compression_diff);
1694 __ Sub(out, temp_reg, temp3);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001695 temps.Release(temp_reg);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001696 // Flip the difference if the `arg` is compressed.
1697 // `temp0` contains inverted `str` compression flag, i.e the same as `arg` compression flag.
1698 __ Lsrs(temp0, temp0, 1u);
1699 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1700 "Expecting 0=compressed, 1=uncompressed");
1701
Artem Serov0fb37192016-12-06 18:13:40 +00001702 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1703 2 * kMaxInstructionSizeInBytes,
1704 CodeBufferCheckScope::kMaximumSize);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001705 __ it(cc);
1706 __ rsb(cc, out, out, 0);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001707 }
1708
1709 __ Bind(&end);
1710
1711 if (can_slow_path) {
1712 __ Bind(slow_path->GetExitLabel());
1713 }
1714}
1715
Vladimir Marko984519c2017-08-23 10:45:29 +01001716// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1717// The normal loop plus the pre-header is 9 instructions (18-26 bytes) without string compression
1718// and 12 instructions (24-32 bytes) with string compression. We can compare up to 4 bytes in 4
1719// instructions (LDR+LDR+CMP+BNE) and up to 8 bytes in 6 instructions (LDRD+LDRD+CMP+BNE+CMP+BNE).
1720// Allow up to 12 instructions (32 bytes) for the unrolled loop.
1721constexpr size_t kShortConstStringEqualsCutoffInBytes = 16;
1722
1723static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1724 if (candidate->IsLoadString()) {
1725 HLoadString* load_string = candidate->AsLoadString();
1726 const DexFile& dex_file = load_string->GetDexFile();
1727 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1728 }
1729 return nullptr;
1730}
1731
Anton Kirilov5ec62182016-10-13 20:16:02 +01001732void IntrinsicLocationsBuilderARMVIXL::VisitStringEquals(HInvoke* invoke) {
1733 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1734 LocationSummary::kNoCall,
1735 kIntrinsified);
1736 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1737 locations->SetInAt(0, Location::RequiresRegister());
1738 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko984519c2017-08-23 10:45:29 +01001739
Anton Kirilov5ec62182016-10-13 20:16:02 +01001740 // Temporary registers to store lengths of strings and for calculations.
1741 // Using instruction cbz requires a low register, so explicitly set a temp to be R0.
1742 locations->AddTemp(LocationFrom(r0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001743
Vladimir Marko984519c2017-08-23 10:45:29 +01001744 // For the generic implementation and for long const strings we need an extra temporary.
1745 // We do not need it for short const strings, up to 4 bytes, see code generation below.
1746 uint32_t const_string_length = 0u;
1747 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1748 if (const_string == nullptr) {
1749 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1750 }
1751 bool is_compressed =
1752 mirror::kUseStringCompression &&
1753 const_string != nullptr &&
1754 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1755 if (const_string == nullptr || const_string_length > (is_compressed ? 4u : 2u)) {
1756 locations->AddTemp(Location::RequiresRegister());
1757 }
1758
1759 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1760 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1761 // Then we shall need an extra temporary register instead of the output register.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001762 locations->SetOut(Location::RequiresRegister());
1763}
1764
1765void IntrinsicCodeGeneratorARMVIXL::VisitStringEquals(HInvoke* invoke) {
1766 ArmVIXLAssembler* assembler = GetAssembler();
1767 LocationSummary* locations = invoke->GetLocations();
1768
1769 vixl32::Register str = InputRegisterAt(invoke, 0);
1770 vixl32::Register arg = InputRegisterAt(invoke, 1);
1771 vixl32::Register out = OutputRegister(invoke);
1772
1773 vixl32::Register temp = RegisterFrom(locations->GetTemp(0));
Anton Kirilov5ec62182016-10-13 20:16:02 +01001774
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001775 vixl32::Label loop;
Anton Kirilov5ec62182016-10-13 20:16:02 +01001776 vixl32::Label end;
1777 vixl32::Label return_true;
1778 vixl32::Label return_false;
Anton Kirilov6f644202017-02-27 18:29:45 +00001779 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &end);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001780
1781 // Get offsets of count, value, and class fields within a string object.
1782 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1783 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1784 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1785
1786 // Note that the null check must have been done earlier.
1787 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1788
1789 StringEqualsOptimizations optimizations(invoke);
1790 if (!optimizations.GetArgumentNotNull()) {
1791 // Check if input is null, return false if it is.
xueliang.zhongf51bc622016-11-04 09:23:32 +00001792 __ CompareAndBranchIfZero(arg, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001793 }
1794
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001795 // Reference equality check, return true if same reference.
1796 __ Cmp(str, arg);
Artem Serov517d9f62016-12-12 15:51:15 +00001797 __ B(eq, &return_true, /* far_target */ false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001798
Anton Kirilov5ec62182016-10-13 20:16:02 +01001799 if (!optimizations.GetArgumentIsString()) {
1800 // Instanceof check for the argument by comparing class fields.
1801 // All string objects must have the same type since String cannot be subclassed.
1802 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1803 // If the argument is a string object, its class field must be equal to receiver's class field.
1804 __ Ldr(temp, MemOperand(str, class_offset));
Vladimir Marko984519c2017-08-23 10:45:29 +01001805 __ Ldr(out, MemOperand(arg, class_offset));
1806 __ Cmp(temp, out);
Artem Serov517d9f62016-12-12 15:51:15 +00001807 __ B(ne, &return_false, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001808 }
1809
Vladimir Marko984519c2017-08-23 10:45:29 +01001810 // Check if one of the inputs is a const string. Do not special-case both strings
1811 // being const, such cases should be handled by constant folding if needed.
1812 uint32_t const_string_length = 0u;
1813 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1814 if (const_string == nullptr) {
1815 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1816 if (const_string != nullptr) {
1817 std::swap(str, arg); // Make sure the const string is in `str`.
1818 }
1819 }
1820 bool is_compressed =
1821 mirror::kUseStringCompression &&
1822 const_string != nullptr &&
1823 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1824
1825 if (const_string != nullptr) {
1826 // Load `count` field of the argument string and check if it matches the const string.
1827 // Also compares the compression style, if differs return false.
1828 __ Ldr(temp, MemOperand(arg, count_offset));
1829 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
1830 __ B(ne, &return_false, /* far_target */ false);
1831 } else {
1832 // Load `count` fields of this and argument strings.
1833 __ Ldr(temp, MemOperand(str, count_offset));
1834 __ Ldr(out, MemOperand(arg, count_offset));
1835 // Check if `count` fields are equal, return false if they're not.
1836 // Also compares the compression style, if differs return false.
1837 __ Cmp(temp, out);
1838 __ B(ne, &return_false, /* far_target */ false);
1839 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001840
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001841 // Assertions that must hold in order to compare strings 4 bytes at a time.
Vladimir Marko984519c2017-08-23 10:45:29 +01001842 // Ok to do this because strings are zero-padded to kObjectAlignment.
Anton Kirilov5ec62182016-10-13 20:16:02 +01001843 DCHECK_ALIGNED(value_offset, 4);
1844 static_assert(IsAligned<4>(kObjectAlignment), "String data must be aligned for fast compare.");
1845
Vladimir Marko984519c2017-08-23 10:45:29 +01001846 if (const_string != nullptr &&
1847 const_string_length <= (is_compressed ? kShortConstStringEqualsCutoffInBytes
1848 : kShortConstStringEqualsCutoffInBytes / 2u)) {
1849 // Load and compare the contents. Though we know the contents of the short const string
1850 // at compile time, materializing constants may be more code than loading from memory.
1851 int32_t offset = value_offset;
1852 size_t remaining_bytes =
1853 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 4u);
1854 while (remaining_bytes > sizeof(uint32_t)) {
1855 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1856 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1857 vixl32::Register temp2 = scratch_scope.Acquire();
1858 __ Ldrd(temp, temp1, MemOperand(str, offset));
1859 __ Ldrd(temp2, out, MemOperand(arg, offset));
1860 __ Cmp(temp, temp2);
1861 __ B(ne, &return_false, /* far_label */ false);
1862 __ Cmp(temp1, out);
1863 __ B(ne, &return_false, /* far_label */ false);
1864 offset += 2u * sizeof(uint32_t);
1865 remaining_bytes -= 2u * sizeof(uint32_t);
1866 }
1867 if (remaining_bytes != 0u) {
1868 __ Ldr(temp, MemOperand(str, offset));
1869 __ Ldr(out, MemOperand(arg, offset));
1870 __ Cmp(temp, out);
1871 __ B(ne, &return_false, /* far_label */ false);
1872 }
1873 } else {
1874 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1875 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1876 "Expecting 0=compressed, 1=uncompressed");
1877 __ CompareAndBranchIfZero(temp, &return_true, /* far_target */ false);
1878
1879 if (mirror::kUseStringCompression) {
1880 // For string compression, calculate the number of bytes to compare (not chars).
1881 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1882 __ Lsrs(temp, temp, 1u); // Extract length and check compression flag.
1883 ExactAssemblyScope aas(assembler->GetVIXLAssembler(),
1884 2 * kMaxInstructionSizeInBytes,
1885 CodeBufferCheckScope::kMaximumSize);
1886 __ it(cs); // If uncompressed,
1887 __ add(cs, temp, temp, temp); // double the byte count.
1888 }
1889
1890 vixl32::Register temp1 = RegisterFrom(locations->GetTemp(1));
1891 UseScratchRegisterScope scratch_scope(assembler->GetVIXLAssembler());
1892 vixl32::Register temp2 = scratch_scope.Acquire();
1893
1894 // Store offset of string value in preparation for comparison loop.
1895 __ Mov(temp1, value_offset);
1896
1897 // Loop to compare strings 4 bytes at a time starting at the front of the string.
1898 __ Bind(&loop);
1899 __ Ldr(out, MemOperand(str, temp1));
1900 __ Ldr(temp2, MemOperand(arg, temp1));
1901 __ Add(temp1, temp1, Operand::From(sizeof(uint32_t)));
1902 __ Cmp(out, temp2);
1903 __ B(ne, &return_false, /* far_target */ false);
1904 // With string compression, we have compared 4 bytes, otherwise 2 chars.
1905 __ Subs(temp, temp, mirror::kUseStringCompression ? 4 : 2);
1906 __ B(hi, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001907 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001908
Anton Kirilov5ec62182016-10-13 20:16:02 +01001909 // Return true and exit the function.
1910 // If loop does not result in returning false, we return true.
1911 __ Bind(&return_true);
1912 __ Mov(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00001913 __ B(final_label);
Anton Kirilov5ec62182016-10-13 20:16:02 +01001914
1915 // Return false and exit the function.
1916 __ Bind(&return_false);
1917 __ Mov(out, 0);
Anton Kirilov6f644202017-02-27 18:29:45 +00001918
1919 if (end.IsReferenced()) {
1920 __ Bind(&end);
1921 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01001922}
1923
1924static void GenerateVisitStringIndexOf(HInvoke* invoke,
1925 ArmVIXLAssembler* assembler,
1926 CodeGeneratorARMVIXL* codegen,
1927 ArenaAllocator* allocator,
1928 bool start_at_zero) {
1929 LocationSummary* locations = invoke->GetLocations();
1930
1931 // Note that the null check must have been done earlier.
1932 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1933
1934 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1935 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
1936 SlowPathCodeARMVIXL* slow_path = nullptr;
1937 HInstruction* code_point = invoke->InputAt(1);
1938 if (code_point->IsIntConstant()) {
Anton Kirilov644032c2016-12-06 17:51:43 +00001939 if (static_cast<uint32_t>(Int32ConstantFrom(code_point)) >
Anton Kirilov5ec62182016-10-13 20:16:02 +01001940 std::numeric_limits<uint16_t>::max()) {
1941 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1942 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1943 slow_path = new (allocator) IntrinsicSlowPathARMVIXL(invoke);
1944 codegen->AddSlowPath(slow_path);
1945 __ B(slow_path->GetEntryLabel());
1946 __ Bind(slow_path->GetExitLabel());
1947 return;
1948 }
1949 } else if (code_point->GetType() != Primitive::kPrimChar) {
1950 vixl32::Register char_reg = InputRegisterAt(invoke, 1);
1951 // 0xffff is not modified immediate but 0x10000 is, so use `>= 0x10000` instead of `> 0xffff`.
1952 __ Cmp(char_reg, static_cast<uint32_t>(std::numeric_limits<uint16_t>::max()) + 1);
1953 slow_path = new (allocator) IntrinsicSlowPathARMVIXL(invoke);
1954 codegen->AddSlowPath(slow_path);
1955 __ B(hs, slow_path->GetEntryLabel());
1956 }
1957
1958 if (start_at_zero) {
1959 vixl32::Register tmp_reg = RegisterFrom(locations->GetTemp(0));
1960 DCHECK(tmp_reg.Is(r2));
1961 // Start-index = 0.
1962 __ Mov(tmp_reg, 0);
1963 }
1964
1965 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
1966 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
1967
1968 if (slow_path != nullptr) {
1969 __ Bind(slow_path->GetExitLabel());
1970 }
1971}
1972
1973void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
1974 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1975 LocationSummary::kCallOnMainAndSlowPath,
1976 kIntrinsified);
1977 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1978 // best to align the inputs accordingly.
1979 InvokeRuntimeCallingConventionARMVIXL calling_convention;
1980 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1981 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1982 locations->SetOut(LocationFrom(r0));
1983
1984 // Need to send start-index=0.
1985 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1986}
1987
1988void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOf(HInvoke* invoke) {
1989 GenerateVisitStringIndexOf(
1990 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
1991}
1992
1993void IntrinsicLocationsBuilderARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
1994 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1995 LocationSummary::kCallOnMainAndSlowPath,
1996 kIntrinsified);
1997 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1998 // best to align the inputs accordingly.
1999 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2000 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2001 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
2002 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
2003 locations->SetOut(LocationFrom(r0));
2004}
2005
2006void IntrinsicCodeGeneratorARMVIXL::VisitStringIndexOfAfter(HInvoke* invoke) {
2007 GenerateVisitStringIndexOf(
2008 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
2009}
2010
2011void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
2012 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2013 LocationSummary::kCallOnMainAndSlowPath,
2014 kIntrinsified);
2015 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2016 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2017 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
2018 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
2019 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
2020 locations->SetOut(LocationFrom(r0));
2021}
2022
2023void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromBytes(HInvoke* invoke) {
2024 ArmVIXLAssembler* assembler = GetAssembler();
2025 vixl32::Register byte_array = InputRegisterAt(invoke, 0);
2026 __ Cmp(byte_array, 0);
2027 SlowPathCodeARMVIXL* slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
2028 codegen_->AddSlowPath(slow_path);
2029 __ B(eq, slow_path->GetEntryLabel());
2030
2031 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
2032 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
2033 __ Bind(slow_path->GetExitLabel());
2034}
2035
2036void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
2037 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2038 LocationSummary::kCallOnMainOnly,
2039 kIntrinsified);
2040 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2041 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2042 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
2043 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
2044 locations->SetOut(LocationFrom(r0));
2045}
2046
2047void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromChars(HInvoke* invoke) {
2048 // No need to emit code checking whether `locations->InAt(2)` is a null
2049 // pointer, as callers of the native method
2050 //
2051 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
2052 //
2053 // all include a null check on `data` before calling that method.
2054 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
2055 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
2056}
2057
2058void IntrinsicLocationsBuilderARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
2059 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2060 LocationSummary::kCallOnMainAndSlowPath,
2061 kIntrinsified);
2062 InvokeRuntimeCallingConventionARMVIXL calling_convention;
2063 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
2064 locations->SetOut(LocationFrom(r0));
2065}
2066
2067void IntrinsicCodeGeneratorARMVIXL::VisitStringNewStringFromString(HInvoke* invoke) {
2068 ArmVIXLAssembler* assembler = GetAssembler();
2069 vixl32::Register string_to_copy = InputRegisterAt(invoke, 0);
2070 __ Cmp(string_to_copy, 0);
2071 SlowPathCodeARMVIXL* slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
2072 codegen_->AddSlowPath(slow_path);
2073 __ B(eq, slow_path->GetEntryLabel());
2074
2075 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
2076 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
2077
2078 __ Bind(slow_path->GetExitLabel());
2079}
2080
2081void IntrinsicLocationsBuilderARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
2082 // The only read barrier implementation supporting the
2083 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2084 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
2085 return;
2086 }
2087
2088 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2089 LocationSummary* locations = invoke->GetLocations();
2090 if (locations == nullptr) {
2091 return;
2092 }
2093
2094 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2095 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2096 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2097
2098 if (src_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(src_pos->GetValue())) {
2099 locations->SetInAt(1, Location::RequiresRegister());
2100 }
2101 if (dest_pos != nullptr && !assembler_->ShifterOperandCanAlwaysHold(dest_pos->GetValue())) {
2102 locations->SetInAt(3, Location::RequiresRegister());
2103 }
2104 if (length != nullptr && !assembler_->ShifterOperandCanAlwaysHold(length->GetValue())) {
2105 locations->SetInAt(4, Location::RequiresRegister());
2106 }
2107 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2108 // Temporary register IP cannot be used in
2109 // ReadBarrierSystemArrayCopySlowPathARM (because that register
2110 // is clobbered by ReadBarrierMarkRegX entry points). Get an extra
2111 // temporary register from the register allocator.
2112 locations->AddTemp(Location::RequiresRegister());
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002113 CodeGeneratorARMVIXL* arm_codegen = down_cast<CodeGeneratorARMVIXL*>(codegen_);
2114 arm_codegen->MaybeAddBakerCcEntrypointTempForFields(locations);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002115 }
2116}
2117
2118static void CheckPosition(ArmVIXLAssembler* assembler,
2119 Location pos,
2120 vixl32::Register input,
2121 Location length,
2122 SlowPathCodeARMVIXL* slow_path,
2123 vixl32::Register temp,
2124 bool length_is_input_length = false) {
2125 // Where is the length in the Array?
2126 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
2127
2128 if (pos.IsConstant()) {
2129 int32_t pos_const = Int32ConstantFrom(pos);
2130 if (pos_const == 0) {
2131 if (!length_is_input_length) {
2132 // Check that length(input) >= length.
2133 __ Ldr(temp, MemOperand(input, length_offset));
2134 if (length.IsConstant()) {
2135 __ Cmp(temp, Int32ConstantFrom(length));
2136 } else {
2137 __ Cmp(temp, RegisterFrom(length));
2138 }
2139 __ B(lt, slow_path->GetEntryLabel());
2140 }
2141 } else {
2142 // Check that length(input) >= pos.
2143 __ Ldr(temp, MemOperand(input, length_offset));
2144 __ Subs(temp, temp, pos_const);
2145 __ B(lt, slow_path->GetEntryLabel());
2146
2147 // Check that (length(input) - pos) >= length.
2148 if (length.IsConstant()) {
2149 __ Cmp(temp, Int32ConstantFrom(length));
2150 } else {
2151 __ Cmp(temp, RegisterFrom(length));
2152 }
2153 __ B(lt, slow_path->GetEntryLabel());
2154 }
2155 } else if (length_is_input_length) {
2156 // The only way the copy can succeed is if pos is zero.
2157 vixl32::Register pos_reg = RegisterFrom(pos);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002158 __ CompareAndBranchIfNonZero(pos_reg, slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002159 } else {
2160 // Check that pos >= 0.
2161 vixl32::Register pos_reg = RegisterFrom(pos);
2162 __ Cmp(pos_reg, 0);
2163 __ B(lt, slow_path->GetEntryLabel());
2164
2165 // Check that pos <= length(input).
2166 __ Ldr(temp, MemOperand(input, length_offset));
2167 __ Subs(temp, temp, pos_reg);
2168 __ B(lt, slow_path->GetEntryLabel());
2169
2170 // Check that (length(input) - pos) >= length.
2171 if (length.IsConstant()) {
2172 __ Cmp(temp, Int32ConstantFrom(length));
2173 } else {
2174 __ Cmp(temp, RegisterFrom(length));
2175 }
2176 __ B(lt, slow_path->GetEntryLabel());
2177 }
2178}
2179
2180void IntrinsicCodeGeneratorARMVIXL::VisitSystemArrayCopy(HInvoke* invoke) {
2181 // The only read barrier implementation supporting the
2182 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2183 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2184
2185 ArmVIXLAssembler* assembler = GetAssembler();
2186 LocationSummary* locations = invoke->GetLocations();
2187
2188 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2189 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2190 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2191 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2192 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
2193
2194 vixl32::Register src = InputRegisterAt(invoke, 0);
2195 Location src_pos = locations->InAt(1);
2196 vixl32::Register dest = InputRegisterAt(invoke, 2);
2197 Location dest_pos = locations->InAt(3);
2198 Location length = locations->InAt(4);
2199 Location temp1_loc = locations->GetTemp(0);
2200 vixl32::Register temp1 = RegisterFrom(temp1_loc);
2201 Location temp2_loc = locations->GetTemp(1);
2202 vixl32::Register temp2 = RegisterFrom(temp2_loc);
2203 Location temp3_loc = locations->GetTemp(2);
2204 vixl32::Register temp3 = RegisterFrom(temp3_loc);
2205
2206 SlowPathCodeARMVIXL* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathARMVIXL(invoke);
2207 codegen_->AddSlowPath(intrinsic_slow_path);
2208
2209 vixl32::Label conditions_on_positions_validated;
2210 SystemArrayCopyOptimizations optimizations(invoke);
2211
2212 // If source and destination are the same, we go to slow path if we need to do
2213 // forward copying.
2214 if (src_pos.IsConstant()) {
2215 int32_t src_pos_constant = Int32ConstantFrom(src_pos);
2216 if (dest_pos.IsConstant()) {
2217 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
2218 if (optimizations.GetDestinationIsSource()) {
2219 // Checked when building locations.
2220 DCHECK_GE(src_pos_constant, dest_pos_constant);
2221 } else if (src_pos_constant < dest_pos_constant) {
2222 __ Cmp(src, dest);
2223 __ B(eq, intrinsic_slow_path->GetEntryLabel());
2224 }
2225
2226 // Checked when building locations.
2227 DCHECK(!optimizations.GetDestinationIsSource()
2228 || (src_pos_constant >= Int32ConstantFrom(dest_pos)));
2229 } else {
2230 if (!optimizations.GetDestinationIsSource()) {
2231 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00002232 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002233 }
2234 __ Cmp(RegisterFrom(dest_pos), src_pos_constant);
2235 __ B(gt, intrinsic_slow_path->GetEntryLabel());
2236 }
2237 } else {
2238 if (!optimizations.GetDestinationIsSource()) {
2239 __ Cmp(src, dest);
Artem Serov517d9f62016-12-12 15:51:15 +00002240 __ B(ne, &conditions_on_positions_validated, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002241 }
2242 if (dest_pos.IsConstant()) {
2243 int32_t dest_pos_constant = Int32ConstantFrom(dest_pos);
2244 __ Cmp(RegisterFrom(src_pos), dest_pos_constant);
2245 } else {
2246 __ Cmp(RegisterFrom(src_pos), RegisterFrom(dest_pos));
2247 }
2248 __ B(lt, intrinsic_slow_path->GetEntryLabel());
2249 }
2250
2251 __ Bind(&conditions_on_positions_validated);
2252
2253 if (!optimizations.GetSourceIsNotNull()) {
2254 // Bail out if the source is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002255 __ CompareAndBranchIfZero(src, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002256 }
2257
2258 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2259 // Bail out if the destination is null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002260 __ CompareAndBranchIfZero(dest, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002261 }
2262
2263 // If the length is negative, bail out.
2264 // We have already checked in the LocationsBuilder for the constant case.
2265 if (!length.IsConstant() &&
2266 !optimizations.GetCountIsSourceLength() &&
2267 !optimizations.GetCountIsDestinationLength()) {
2268 __ Cmp(RegisterFrom(length), 0);
2269 __ B(lt, intrinsic_slow_path->GetEntryLabel());
2270 }
2271
2272 // Validity checks: source.
2273 CheckPosition(assembler,
2274 src_pos,
2275 src,
2276 length,
2277 intrinsic_slow_path,
2278 temp1,
2279 optimizations.GetCountIsSourceLength());
2280
2281 // Validity checks: dest.
2282 CheckPosition(assembler,
2283 dest_pos,
2284 dest,
2285 length,
2286 intrinsic_slow_path,
2287 temp1,
2288 optimizations.GetCountIsDestinationLength());
2289
2290 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2291 // Check whether all elements of the source array are assignable to the component
2292 // type of the destination array. We do two checks: the classes are the same,
2293 // or the destination is Object[]. If none of these checks succeed, we go to the
2294 // slow path.
2295
2296 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2297 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2298 // /* HeapReference<Class> */ temp1 = src->klass_
2299 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2300 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2301 // Bail out if the source is not a non primitive array.
2302 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2303 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2304 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002305 __ CompareAndBranchIfZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002306 // If heap poisoning is enabled, `temp1` has been unpoisoned
2307 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2308 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2309 __ Ldrh(temp1, MemOperand(temp1, primitive_offset));
2310 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002311 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002312 }
2313
2314 // /* HeapReference<Class> */ temp1 = dest->klass_
2315 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2316 invoke, temp1_loc, dest, class_offset, temp2_loc, /* needs_null_check */ false);
2317
2318 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2319 // Bail out if the destination is not a non primitive array.
2320 //
2321 // Register `temp1` is not trashed by the read barrier emitted
2322 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2323 // method produces a call to a ReadBarrierMarkRegX entry point,
2324 // which saves all potentially live registers, including
2325 // temporaries such a `temp1`.
2326 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2327 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2328 invoke, temp2_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002329 __ CompareAndBranchIfZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002330 // If heap poisoning is enabled, `temp2` has been unpoisoned
2331 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2332 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2333 __ Ldrh(temp2, MemOperand(temp2, primitive_offset));
2334 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002335 __ CompareAndBranchIfNonZero(temp2, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002336 }
2337
2338 // For the same reason given earlier, `temp1` is not trashed by the
2339 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2340 // /* HeapReference<Class> */ temp2 = src->klass_
2341 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2342 invoke, temp2_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
2343 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2344 __ Cmp(temp1, temp2);
2345
2346 if (optimizations.GetDestinationIsTypedObjectArray()) {
2347 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002348 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002349 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2350 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2351 invoke, temp1_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
2352 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2353 // We do not need to emit a read barrier for the following
2354 // heap reference load, as `temp1` is only used in a
2355 // comparison with null below, and this reference is not
2356 // kept afterwards.
2357 __ Ldr(temp1, MemOperand(temp1, super_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002358 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002359 __ Bind(&do_copy);
2360 } else {
2361 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2362 }
2363 } else {
2364 // Non read barrier code.
2365
2366 // /* HeapReference<Class> */ temp1 = dest->klass_
2367 __ Ldr(temp1, MemOperand(dest, class_offset));
2368 // /* HeapReference<Class> */ temp2 = src->klass_
2369 __ Ldr(temp2, MemOperand(src, class_offset));
2370 bool did_unpoison = false;
2371 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2372 !optimizations.GetSourceIsNonPrimitiveArray()) {
2373 // One or two of the references need to be unpoisoned. Unpoison them
2374 // both to make the identity check valid.
2375 assembler->MaybeUnpoisonHeapReference(temp1);
2376 assembler->MaybeUnpoisonHeapReference(temp2);
2377 did_unpoison = true;
2378 }
2379
2380 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2381 // Bail out if the destination is not a non primitive array.
2382 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2383 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002384 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002385 assembler->MaybeUnpoisonHeapReference(temp3);
2386 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2387 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2388 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002389 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002390 }
2391
2392 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2393 // Bail out if the source is not a non primitive array.
2394 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2395 __ Ldr(temp3, MemOperand(temp2, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002396 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002397 assembler->MaybeUnpoisonHeapReference(temp3);
2398 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2399 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2400 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002401 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002402 }
2403
2404 __ Cmp(temp1, temp2);
2405
2406 if (optimizations.GetDestinationIsTypedObjectArray()) {
2407 vixl32::Label do_copy;
Artem Serov517d9f62016-12-12 15:51:15 +00002408 __ B(eq, &do_copy, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002409 if (!did_unpoison) {
2410 assembler->MaybeUnpoisonHeapReference(temp1);
2411 }
2412 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2413 __ Ldr(temp1, MemOperand(temp1, component_offset));
2414 assembler->MaybeUnpoisonHeapReference(temp1);
2415 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2416 __ Ldr(temp1, MemOperand(temp1, super_offset));
2417 // No need to unpoison the result, we're comparing against null.
xueliang.zhongf51bc622016-11-04 09:23:32 +00002418 __ CompareAndBranchIfNonZero(temp1, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002419 __ Bind(&do_copy);
2420 } else {
2421 __ B(ne, intrinsic_slow_path->GetEntryLabel());
2422 }
2423 }
2424 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2425 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2426 // Bail out if the source is not a non primitive array.
2427 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2428 // /* HeapReference<Class> */ temp1 = src->klass_
2429 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2430 invoke, temp1_loc, src, class_offset, temp2_loc, /* needs_null_check */ false);
2431 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2432 codegen_->GenerateFieldLoadWithBakerReadBarrier(
2433 invoke, temp3_loc, temp1, component_offset, temp2_loc, /* needs_null_check */ false);
xueliang.zhongf51bc622016-11-04 09:23:32 +00002434 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002435 // If heap poisoning is enabled, `temp3` has been unpoisoned
2436 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2437 } else {
2438 // /* HeapReference<Class> */ temp1 = src->klass_
2439 __ Ldr(temp1, MemOperand(src, class_offset));
2440 assembler->MaybeUnpoisonHeapReference(temp1);
2441 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2442 __ Ldr(temp3, MemOperand(temp1, component_offset));
xueliang.zhongf51bc622016-11-04 09:23:32 +00002443 __ CompareAndBranchIfZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002444 assembler->MaybeUnpoisonHeapReference(temp3);
2445 }
2446 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2447 __ Ldrh(temp3, MemOperand(temp3, primitive_offset));
2448 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
xueliang.zhongf51bc622016-11-04 09:23:32 +00002449 __ CompareAndBranchIfNonZero(temp3, intrinsic_slow_path->GetEntryLabel());
Anton Kirilov5ec62182016-10-13 20:16:02 +01002450 }
2451
Roland Levillain1663d162017-03-17 15:15:21 +00002452 if (length.IsConstant() && Int32ConstantFrom(length) == 0) {
2453 // Null constant length: not need to emit the loop code at all.
Anton Kirilov5ec62182016-10-13 20:16:02 +01002454 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002455 vixl32::Label done;
2456 const Primitive::Type type = Primitive::kPrimNot;
2457 const int32_t element_size = Primitive::ComponentSize(type);
2458
2459 if (length.IsRegister()) {
2460 // Don't enter the copy loop if the length is null.
2461 __ CompareAndBranchIfZero(RegisterFrom(length), &done, /* is_far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002462 }
Roland Levillain1663d162017-03-17 15:15:21 +00002463
2464 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2465 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2466
2467 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002468 // also CodeGeneratorARMVIXL::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002469 //
2470 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2471 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2472 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2473 // if (is_gray) {
2474 // // Slow-path copy.
2475 // do {
2476 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2477 // } while (src_ptr != end_ptr)
2478 // } else {
2479 // // Fast-path copy.
2480 // do {
2481 // *dest_ptr++ = *src_ptr++;
2482 // } while (src_ptr != end_ptr)
2483 // }
2484
2485 // /* int32_t */ monitor = src->monitor_
2486 __ Ldr(temp2, MemOperand(src, monitor_offset));
2487 // /* LockWord */ lock_word = LockWord(monitor)
2488 static_assert(sizeof(LockWord) == sizeof(int32_t),
2489 "art::LockWord and int32_t have different sizes.");
2490
2491 // Introduce a dependency on the lock_word including the rb_state,
2492 // which shall prevent load-load reordering without using
2493 // a memory barrier (which would be more expensive).
2494 // `src` is unchanged by this operation, but its value now depends
2495 // on `temp2`.
2496 __ Add(src, src, Operand(temp2, vixl32::LSR, 32));
2497
2498 // Compute the base source address in `temp1`.
2499 // Note that `temp1` (the base source address) is computed from
2500 // `src` (and `src_pos`) here, and thus honors the artificial
2501 // dependency of `src` on `temp2`.
2502 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2503 // Compute the end source address in `temp3`.
2504 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2505 // The base destination address is computed later, as `temp2` is
2506 // used for intermediate computations.
2507
2508 // Slow path used to copy array when `src` is gray.
2509 // Note that the base destination address is computed in `temp2`
2510 // by the slow path code.
2511 SlowPathCodeARMVIXL* read_barrier_slow_path =
2512 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathARMVIXL(invoke);
2513 codegen_->AddSlowPath(read_barrier_slow_path);
2514
2515 // Given the numeric representation, it's enough to check the low bit of the
2516 // rb_state. We do that by shifting the bit out of the lock word with LSRS
2517 // which can be a 16-bit instruction unlike the TST immediate.
2518 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2519 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2520 __ Lsrs(temp2, temp2, LockWord::kReadBarrierStateShift + 1);
2521 // Carry flag is the last bit shifted out by LSRS.
2522 __ B(cs, read_barrier_slow_path->GetEntryLabel());
2523
2524 // Fast-path copy.
2525 // Compute the base destination address in `temp2`.
2526 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2527 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2528 // poison/unpoison.
2529 vixl32::Label loop;
2530 __ Bind(&loop);
2531 {
2532 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2533 const vixl32::Register temp_reg = temps.Acquire();
2534 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2535 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2536 }
2537 __ Cmp(temp1, temp3);
2538 __ B(ne, &loop, /* far_target */ false);
2539
2540 __ Bind(read_barrier_slow_path->GetExitLabel());
2541 } else {
2542 // Non read barrier code.
2543 // Compute the base source address in `temp1`.
2544 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
2545 // Compute the base destination address in `temp2`.
2546 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
2547 // Compute the end source address in `temp3`.
2548 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
2549 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2550 // poison/unpoison.
2551 vixl32::Label loop;
2552 __ Bind(&loop);
2553 {
2554 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2555 const vixl32::Register temp_reg = temps.Acquire();
2556 __ Ldr(temp_reg, MemOperand(temp1, element_size, PostIndex));
2557 __ Str(temp_reg, MemOperand(temp2, element_size, PostIndex));
2558 }
2559 __ Cmp(temp1, temp3);
2560 __ B(ne, &loop, /* far_target */ false);
2561 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01002562 __ Bind(&done);
2563 }
2564
2565 // We only need one card marking on the destination array.
2566 codegen_->MarkGCCard(temp1, temp2, dest, NoReg, /* value_can_be_null */ false);
2567
2568 __ Bind(intrinsic_slow_path->GetExitLabel());
2569}
2570
2571static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2572 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2573 // the code generator. Furthermore, the register allocator creates fixed live intervals
2574 // for all caller-saved registers because we are doing a function call. As a result, if
2575 // the input and output locations are unallocated, the register allocator runs out of
2576 // registers and fails; however, a debuggable graph is not the common case.
2577 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2578 return;
2579 }
2580
2581 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2582 DCHECK_EQ(invoke->InputAt(0)->GetType(), Primitive::kPrimDouble);
2583 DCHECK_EQ(invoke->GetType(), Primitive::kPrimDouble);
2584
2585 LocationSummary* const locations = new (arena) LocationSummary(invoke,
2586 LocationSummary::kCallOnMainOnly,
2587 kIntrinsified);
2588 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2589
2590 locations->SetInAt(0, Location::RequiresFpuRegister());
2591 locations->SetOut(Location::RequiresFpuRegister());
2592 // Native code uses the soft float ABI.
2593 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2594 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2595}
2596
2597static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
2598 // If the graph is debuggable, all callee-saved floating-point registers are blocked by
2599 // the code generator. Furthermore, the register allocator creates fixed live intervals
2600 // for all caller-saved registers because we are doing a function call. As a result, if
2601 // the input and output locations are unallocated, the register allocator runs out of
2602 // registers and fails; however, a debuggable graph is not the common case.
2603 if (invoke->GetBlock()->GetGraph()->IsDebuggable()) {
2604 return;
2605 }
2606
2607 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2608 DCHECK_EQ(invoke->InputAt(0)->GetType(), Primitive::kPrimDouble);
2609 DCHECK_EQ(invoke->InputAt(1)->GetType(), Primitive::kPrimDouble);
2610 DCHECK_EQ(invoke->GetType(), Primitive::kPrimDouble);
2611
2612 LocationSummary* const locations = new (arena) LocationSummary(invoke,
2613 LocationSummary::kCallOnMainOnly,
2614 kIntrinsified);
2615 const InvokeRuntimeCallingConventionARMVIXL calling_convention;
2616
2617 locations->SetInAt(0, Location::RequiresFpuRegister());
2618 locations->SetInAt(1, Location::RequiresFpuRegister());
2619 locations->SetOut(Location::RequiresFpuRegister());
2620 // Native code uses the soft float ABI.
2621 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(0)));
2622 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(1)));
2623 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
2624 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(3)));
2625}
2626
2627static void GenFPToFPCall(HInvoke* invoke,
2628 ArmVIXLAssembler* assembler,
2629 CodeGeneratorARMVIXL* codegen,
2630 QuickEntrypointEnum entry) {
2631 LocationSummary* const locations = invoke->GetLocations();
2632
2633 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
2634 DCHECK(locations->WillCall() && locations->Intrinsified());
2635
2636 // Native code uses the soft float ABI.
2637 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2638 RegisterFrom(locations->GetTemp(1)),
2639 InputDRegisterAt(invoke, 0));
2640 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2641 __ Vmov(OutputDRegister(invoke),
2642 RegisterFrom(locations->GetTemp(0)),
2643 RegisterFrom(locations->GetTemp(1)));
2644}
2645
2646static void GenFPFPToFPCall(HInvoke* invoke,
2647 ArmVIXLAssembler* assembler,
2648 CodeGeneratorARMVIXL* codegen,
2649 QuickEntrypointEnum entry) {
2650 LocationSummary* const locations = invoke->GetLocations();
2651
2652 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
2653 DCHECK(locations->WillCall() && locations->Intrinsified());
2654
2655 // Native code uses the soft float ABI.
2656 __ Vmov(RegisterFrom(locations->GetTemp(0)),
2657 RegisterFrom(locations->GetTemp(1)),
2658 InputDRegisterAt(invoke, 0));
2659 __ Vmov(RegisterFrom(locations->GetTemp(2)),
2660 RegisterFrom(locations->GetTemp(3)),
2661 InputDRegisterAt(invoke, 1));
2662 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
2663 __ Vmov(OutputDRegister(invoke),
2664 RegisterFrom(locations->GetTemp(0)),
2665 RegisterFrom(locations->GetTemp(1)));
2666}
2667
2668void IntrinsicLocationsBuilderARMVIXL::VisitMathCos(HInvoke* invoke) {
2669 CreateFPToFPCallLocations(arena_, invoke);
2670}
2671
2672void IntrinsicCodeGeneratorARMVIXL::VisitMathCos(HInvoke* invoke) {
2673 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCos);
2674}
2675
2676void IntrinsicLocationsBuilderARMVIXL::VisitMathSin(HInvoke* invoke) {
2677 CreateFPToFPCallLocations(arena_, invoke);
2678}
2679
2680void IntrinsicCodeGeneratorARMVIXL::VisitMathSin(HInvoke* invoke) {
2681 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSin);
2682}
2683
2684void IntrinsicLocationsBuilderARMVIXL::VisitMathAcos(HInvoke* invoke) {
2685 CreateFPToFPCallLocations(arena_, invoke);
2686}
2687
2688void IntrinsicCodeGeneratorARMVIXL::VisitMathAcos(HInvoke* invoke) {
2689 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAcos);
2690}
2691
2692void IntrinsicLocationsBuilderARMVIXL::VisitMathAsin(HInvoke* invoke) {
2693 CreateFPToFPCallLocations(arena_, invoke);
2694}
2695
2696void IntrinsicCodeGeneratorARMVIXL::VisitMathAsin(HInvoke* invoke) {
2697 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAsin);
2698}
2699
2700void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan(HInvoke* invoke) {
2701 CreateFPToFPCallLocations(arena_, invoke);
2702}
2703
2704void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan(HInvoke* invoke) {
2705 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan);
2706}
2707
2708void IntrinsicLocationsBuilderARMVIXL::VisitMathCbrt(HInvoke* invoke) {
2709 CreateFPToFPCallLocations(arena_, invoke);
2710}
2711
2712void IntrinsicCodeGeneratorARMVIXL::VisitMathCbrt(HInvoke* invoke) {
2713 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCbrt);
2714}
2715
2716void IntrinsicLocationsBuilderARMVIXL::VisitMathCosh(HInvoke* invoke) {
2717 CreateFPToFPCallLocations(arena_, invoke);
2718}
2719
2720void IntrinsicCodeGeneratorARMVIXL::VisitMathCosh(HInvoke* invoke) {
2721 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickCosh);
2722}
2723
2724void IntrinsicLocationsBuilderARMVIXL::VisitMathExp(HInvoke* invoke) {
2725 CreateFPToFPCallLocations(arena_, invoke);
2726}
2727
2728void IntrinsicCodeGeneratorARMVIXL::VisitMathExp(HInvoke* invoke) {
2729 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExp);
2730}
2731
2732void IntrinsicLocationsBuilderARMVIXL::VisitMathExpm1(HInvoke* invoke) {
2733 CreateFPToFPCallLocations(arena_, invoke);
2734}
2735
2736void IntrinsicCodeGeneratorARMVIXL::VisitMathExpm1(HInvoke* invoke) {
2737 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickExpm1);
2738}
2739
2740void IntrinsicLocationsBuilderARMVIXL::VisitMathLog(HInvoke* invoke) {
2741 CreateFPToFPCallLocations(arena_, invoke);
2742}
2743
2744void IntrinsicCodeGeneratorARMVIXL::VisitMathLog(HInvoke* invoke) {
2745 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog);
2746}
2747
2748void IntrinsicLocationsBuilderARMVIXL::VisitMathLog10(HInvoke* invoke) {
2749 CreateFPToFPCallLocations(arena_, invoke);
2750}
2751
2752void IntrinsicCodeGeneratorARMVIXL::VisitMathLog10(HInvoke* invoke) {
2753 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickLog10);
2754}
2755
2756void IntrinsicLocationsBuilderARMVIXL::VisitMathSinh(HInvoke* invoke) {
2757 CreateFPToFPCallLocations(arena_, invoke);
2758}
2759
2760void IntrinsicCodeGeneratorARMVIXL::VisitMathSinh(HInvoke* invoke) {
2761 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickSinh);
2762}
2763
2764void IntrinsicLocationsBuilderARMVIXL::VisitMathTan(HInvoke* invoke) {
2765 CreateFPToFPCallLocations(arena_, invoke);
2766}
2767
2768void IntrinsicCodeGeneratorARMVIXL::VisitMathTan(HInvoke* invoke) {
2769 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTan);
2770}
2771
2772void IntrinsicLocationsBuilderARMVIXL::VisitMathTanh(HInvoke* invoke) {
2773 CreateFPToFPCallLocations(arena_, invoke);
2774}
2775
2776void IntrinsicCodeGeneratorARMVIXL::VisitMathTanh(HInvoke* invoke) {
2777 GenFPToFPCall(invoke, GetAssembler(), codegen_, kQuickTanh);
2778}
2779
2780void IntrinsicLocationsBuilderARMVIXL::VisitMathAtan2(HInvoke* invoke) {
2781 CreateFPFPToFPCallLocations(arena_, invoke);
2782}
2783
2784void IntrinsicCodeGeneratorARMVIXL::VisitMathAtan2(HInvoke* invoke) {
2785 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickAtan2);
2786}
2787
2788void IntrinsicLocationsBuilderARMVIXL::VisitMathHypot(HInvoke* invoke) {
2789 CreateFPFPToFPCallLocations(arena_, invoke);
2790}
2791
2792void IntrinsicCodeGeneratorARMVIXL::VisitMathHypot(HInvoke* invoke) {
2793 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickHypot);
2794}
2795
2796void IntrinsicLocationsBuilderARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
2797 CreateFPFPToFPCallLocations(arena_, invoke);
2798}
2799
2800void IntrinsicCodeGeneratorARMVIXL::VisitMathNextAfter(HInvoke* invoke) {
2801 GenFPFPToFPCall(invoke, GetAssembler(), codegen_, kQuickNextAfter);
2802}
2803
2804void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
2805 CreateIntToIntLocations(arena_, invoke);
2806}
2807
2808void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverse(HInvoke* invoke) {
2809 ArmVIXLAssembler* assembler = GetAssembler();
2810 __ Rbit(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2811}
2812
2813void IntrinsicLocationsBuilderARMVIXL::VisitLongReverse(HInvoke* invoke) {
2814 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2815 LocationSummary::kNoCall,
2816 kIntrinsified);
2817 locations->SetInAt(0, Location::RequiresRegister());
2818 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2819}
2820
2821void IntrinsicCodeGeneratorARMVIXL::VisitLongReverse(HInvoke* invoke) {
2822 ArmVIXLAssembler* assembler = GetAssembler();
2823 LocationSummary* locations = invoke->GetLocations();
2824
2825 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2826 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2827 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2828 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2829
2830 __ Rbit(out_reg_lo, in_reg_hi);
2831 __ Rbit(out_reg_hi, in_reg_lo);
2832}
2833
2834void IntrinsicLocationsBuilderARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
2835 CreateIntToIntLocations(arena_, invoke);
2836}
2837
2838void IntrinsicCodeGeneratorARMVIXL::VisitIntegerReverseBytes(HInvoke* invoke) {
2839 ArmVIXLAssembler* assembler = GetAssembler();
2840 __ Rev(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2841}
2842
2843void IntrinsicLocationsBuilderARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
2844 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2845 LocationSummary::kNoCall,
2846 kIntrinsified);
2847 locations->SetInAt(0, Location::RequiresRegister());
2848 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2849}
2850
2851void IntrinsicCodeGeneratorARMVIXL::VisitLongReverseBytes(HInvoke* invoke) {
2852 ArmVIXLAssembler* assembler = GetAssembler();
2853 LocationSummary* locations = invoke->GetLocations();
2854
2855 vixl32::Register in_reg_lo = LowRegisterFrom(locations->InAt(0));
2856 vixl32::Register in_reg_hi = HighRegisterFrom(locations->InAt(0));
2857 vixl32::Register out_reg_lo = LowRegisterFrom(locations->Out());
2858 vixl32::Register out_reg_hi = HighRegisterFrom(locations->Out());
2859
2860 __ Rev(out_reg_lo, in_reg_hi);
2861 __ Rev(out_reg_hi, in_reg_lo);
2862}
2863
2864void IntrinsicLocationsBuilderARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
2865 CreateIntToIntLocations(arena_, invoke);
2866}
2867
2868void IntrinsicCodeGeneratorARMVIXL::VisitShortReverseBytes(HInvoke* invoke) {
2869 ArmVIXLAssembler* assembler = GetAssembler();
2870 __ Revsh(OutputRegister(invoke), InputRegisterAt(invoke, 0));
2871}
2872
2873static void GenBitCount(HInvoke* instr, Primitive::Type type, ArmVIXLAssembler* assembler) {
2874 DCHECK(Primitive::IsIntOrLongType(type)) << type;
2875 DCHECK_EQ(instr->GetType(), Primitive::kPrimInt);
2876 DCHECK_EQ(Primitive::PrimitiveKind(instr->InputAt(0)->GetType()), type);
2877
2878 bool is_long = type == Primitive::kPrimLong;
2879 LocationSummary* locations = instr->GetLocations();
2880 Location in = locations->InAt(0);
2881 vixl32::Register src_0 = is_long ? LowRegisterFrom(in) : RegisterFrom(in);
2882 vixl32::Register src_1 = is_long ? HighRegisterFrom(in) : src_0;
2883 vixl32::SRegister tmp_s = LowSRegisterFrom(locations->GetTemp(0));
2884 vixl32::DRegister tmp_d = DRegisterFrom(locations->GetTemp(0));
2885 vixl32::Register out_r = OutputRegister(instr);
2886
2887 // Move data from core register(s) to temp D-reg for bit count calculation, then move back.
2888 // According to Cortex A57 and A72 optimization guides, compared to transferring to full D-reg,
2889 // transferring data from core reg to upper or lower half of vfp D-reg requires extra latency,
2890 // That's why for integer bit count, we use 'vmov d0, r0, r0' instead of 'vmov d0[0], r0'.
2891 __ Vmov(tmp_d, src_1, src_0); // Temp DReg |--src_1|--src_0|
2892 __ Vcnt(Untyped8, tmp_d, tmp_d); // Temp DReg |c|c|c|c|c|c|c|c|
2893 __ Vpaddl(U8, tmp_d, tmp_d); // Temp DReg |--c|--c|--c|--c|
2894 __ Vpaddl(U16, tmp_d, tmp_d); // Temp DReg |------c|------c|
2895 if (is_long) {
2896 __ Vpaddl(U32, tmp_d, tmp_d); // Temp DReg |--------------c|
2897 }
2898 __ Vmov(out_r, tmp_s);
2899}
2900
2901void IntrinsicLocationsBuilderARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
2902 CreateIntToIntLocations(arena_, invoke);
2903 invoke->GetLocations()->AddTemp(Location::RequiresFpuRegister());
2904}
2905
2906void IntrinsicCodeGeneratorARMVIXL::VisitIntegerBitCount(HInvoke* invoke) {
2907 GenBitCount(invoke, Primitive::kPrimInt, GetAssembler());
2908}
2909
2910void IntrinsicLocationsBuilderARMVIXL::VisitLongBitCount(HInvoke* invoke) {
2911 VisitIntegerBitCount(invoke);
2912}
2913
2914void IntrinsicCodeGeneratorARMVIXL::VisitLongBitCount(HInvoke* invoke) {
2915 GenBitCount(invoke, Primitive::kPrimLong, GetAssembler());
2916}
2917
2918void IntrinsicLocationsBuilderARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2919 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2920 LocationSummary::kNoCall,
2921 kIntrinsified);
2922 locations->SetInAt(0, Location::RequiresRegister());
2923 locations->SetInAt(1, Location::RequiresRegister());
2924 locations->SetInAt(2, Location::RequiresRegister());
2925 locations->SetInAt(3, Location::RequiresRegister());
2926 locations->SetInAt(4, Location::RequiresRegister());
2927
2928 // Temporary registers to store lengths of strings and for calculations.
2929 locations->AddTemp(Location::RequiresRegister());
2930 locations->AddTemp(Location::RequiresRegister());
2931 locations->AddTemp(Location::RequiresRegister());
2932}
2933
2934void IntrinsicCodeGeneratorARMVIXL::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2935 ArmVIXLAssembler* assembler = GetAssembler();
2936 LocationSummary* locations = invoke->GetLocations();
2937
2938 // Check assumption that sizeof(Char) is 2 (used in scaling below).
2939 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2940 DCHECK_EQ(char_size, 2u);
2941
2942 // Location of data in char array buffer.
2943 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2944
2945 // Location of char array data in string.
2946 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2947
2948 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2949 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2950 vixl32::Register srcObj = InputRegisterAt(invoke, 0);
2951 vixl32::Register srcBegin = InputRegisterAt(invoke, 1);
2952 vixl32::Register srcEnd = InputRegisterAt(invoke, 2);
2953 vixl32::Register dstObj = InputRegisterAt(invoke, 3);
2954 vixl32::Register dstBegin = InputRegisterAt(invoke, 4);
2955
2956 vixl32::Register num_chr = RegisterFrom(locations->GetTemp(0));
2957 vixl32::Register src_ptr = RegisterFrom(locations->GetTemp(1));
2958 vixl32::Register dst_ptr = RegisterFrom(locations->GetTemp(2));
2959
2960 vixl32::Label done, compressed_string_loop;
Anton Kirilov6f644202017-02-27 18:29:45 +00002961 vixl32::Label* final_label = codegen_->GetFinalLabel(invoke, &done);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002962 // dst to be copied.
2963 __ Add(dst_ptr, dstObj, data_offset);
2964 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, vixl32::LSL, 1));
2965
2966 __ Subs(num_chr, srcEnd, srcBegin);
2967 // Early out for valid zero-length retrievals.
Anton Kirilov6f644202017-02-27 18:29:45 +00002968 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002969
2970 // src range to copy.
2971 __ Add(src_ptr, srcObj, value_offset);
2972
2973 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
2974 vixl32::Register temp;
2975 vixl32::Label compressed_string_preloop;
2976 if (mirror::kUseStringCompression) {
2977 // Location of count in string.
2978 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2979 temp = temps.Acquire();
2980 // String's length.
2981 __ Ldr(temp, MemOperand(srcObj, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002982 __ Tst(temp, 1);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002983 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00002984 __ B(eq, &compressed_string_preloop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002985 }
2986 __ Add(src_ptr, src_ptr, Operand(srcBegin, vixl32::LSL, 1));
2987
2988 // Do the copy.
2989 vixl32::Label loop, remainder;
2990
2991 temp = temps.Acquire();
2992 // Save repairing the value of num_chr on the < 4 character path.
2993 __ Subs(temp, num_chr, 4);
Artem Serov517d9f62016-12-12 15:51:15 +00002994 __ B(lt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01002995
2996 // Keep the result of the earlier subs, we are going to fetch at least 4 characters.
2997 __ Mov(num_chr, temp);
2998
2999 // Main loop used for longer fetches loads and stores 4x16-bit characters at a time.
3000 // (LDRD/STRD fault on unaligned addresses and it's not worth inlining extra code
3001 // to rectify these everywhere this intrinsic applies.)
3002 __ Bind(&loop);
3003 __ Ldr(temp, MemOperand(src_ptr, char_size * 2));
3004 __ Subs(num_chr, num_chr, 4);
3005 __ Str(temp, MemOperand(dst_ptr, char_size * 2));
3006 __ Ldr(temp, MemOperand(src_ptr, char_size * 4, PostIndex));
3007 __ Str(temp, MemOperand(dst_ptr, char_size * 4, PostIndex));
3008 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00003009 __ B(ge, &loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003010
3011 __ Adds(num_chr, num_chr, 4);
Anton Kirilov6f644202017-02-27 18:29:45 +00003012 __ B(eq, final_label, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003013
3014 // Main loop for < 4 character case and remainder handling. Loads and stores one
3015 // 16-bit Java character at a time.
3016 __ Bind(&remainder);
3017 temp = temps.Acquire();
3018 __ Ldrh(temp, MemOperand(src_ptr, char_size, PostIndex));
3019 __ Subs(num_chr, num_chr, 1);
3020 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
3021 temps.Release(temp);
Artem Serov517d9f62016-12-12 15:51:15 +00003022 __ B(gt, &remainder, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003023
3024 if (mirror::kUseStringCompression) {
Anton Kirilov6f644202017-02-27 18:29:45 +00003025 __ B(final_label);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01003026
Anton Kirilov5ec62182016-10-13 20:16:02 +01003027 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
3028 DCHECK_EQ(c_char_size, 1u);
3029 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
3030 __ Bind(&compressed_string_preloop);
3031 __ Add(src_ptr, src_ptr, srcBegin);
3032 __ Bind(&compressed_string_loop);
3033 temp = temps.Acquire();
3034 __ Ldrb(temp, MemOperand(src_ptr, c_char_size, PostIndex));
3035 __ Strh(temp, MemOperand(dst_ptr, char_size, PostIndex));
3036 temps.Release(temp);
3037 __ Subs(num_chr, num_chr, 1);
Artem Serov517d9f62016-12-12 15:51:15 +00003038 __ B(gt, &compressed_string_loop, /* far_target */ false);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003039 }
3040
Anton Kirilov6f644202017-02-27 18:29:45 +00003041 if (done.IsReferenced()) {
3042 __ Bind(&done);
3043 }
Anton Kirilov5ec62182016-10-13 20:16:02 +01003044}
3045
3046void IntrinsicLocationsBuilderARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
3047 CreateFPToIntLocations(arena_, invoke);
3048}
3049
3050void IntrinsicCodeGeneratorARMVIXL::VisitFloatIsInfinite(HInvoke* invoke) {
3051 ArmVIXLAssembler* const assembler = GetAssembler();
3052 const vixl32::Register out = OutputRegister(invoke);
3053 // Shifting left by 1 bit makes the value encodable as an immediate operand;
3054 // we don't care about the sign bit anyway.
3055 constexpr uint32_t infinity = kPositiveInfinityFloat << 1U;
3056
3057 __ Vmov(out, InputSRegisterAt(invoke, 0));
3058 // We don't care about the sign bit, so shift left.
3059 __ Lsl(out, out, 1);
3060 __ Eor(out, out, infinity);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003061 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003062}
3063
3064void IntrinsicLocationsBuilderARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
3065 CreateFPToIntLocations(arena_, invoke);
3066}
3067
3068void IntrinsicCodeGeneratorARMVIXL::VisitDoubleIsInfinite(HInvoke* invoke) {
3069 ArmVIXLAssembler* const assembler = GetAssembler();
3070 const vixl32::Register out = OutputRegister(invoke);
3071 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3072 const vixl32::Register temp = temps.Acquire();
3073 // The highest 32 bits of double precision positive infinity separated into
3074 // two constants encodable as immediate operands.
3075 constexpr uint32_t infinity_high = 0x7f000000U;
3076 constexpr uint32_t infinity_high2 = 0x00f00000U;
3077
3078 static_assert((infinity_high | infinity_high2) ==
3079 static_cast<uint32_t>(kPositiveInfinityDouble >> 32U),
3080 "The constants do not add up to the high 32 bits of double "
3081 "precision positive infinity.");
3082 __ Vmov(temp, out, InputDRegisterAt(invoke, 0));
3083 __ Eor(out, out, infinity_high);
3084 __ Eor(out, out, infinity_high2);
3085 // We don't care about the sign bit, so shift left.
3086 __ Orr(out, temp, Operand(out, vixl32::LSL, 1));
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003087 codegen_->GenerateConditionWithZero(kCondEQ, out, out);
Anton Kirilov5ec62182016-10-13 20:16:02 +01003088}
3089
Artem Serov9aee2d42017-01-06 15:58:31 +00003090void IntrinsicLocationsBuilderARMVIXL::VisitMathCeil(HInvoke* invoke) {
3091 if (features_.HasARMv8AInstructions()) {
3092 CreateFPToFPLocations(arena_, invoke);
3093 }
3094}
3095
3096void IntrinsicCodeGeneratorARMVIXL::VisitMathCeil(HInvoke* invoke) {
3097 ArmVIXLAssembler* assembler = GetAssembler();
3098 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
3099 __ Vrintp(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
3100}
3101
3102void IntrinsicLocationsBuilderARMVIXL::VisitMathFloor(HInvoke* invoke) {
3103 if (features_.HasARMv8AInstructions()) {
3104 CreateFPToFPLocations(arena_, invoke);
3105 }
3106}
3107
3108void IntrinsicCodeGeneratorARMVIXL::VisitMathFloor(HInvoke* invoke) {
3109 ArmVIXLAssembler* assembler = GetAssembler();
3110 DCHECK(codegen_->GetInstructionSetFeatures().HasARMv8AInstructions());
3111 __ Vrintm(F64, F64, OutputDRegister(invoke), InputDRegisterAt(invoke, 0));
3112}
3113
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003114void IntrinsicLocationsBuilderARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
3115 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3116 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3117 invoke,
3118 codegen_,
3119 LocationFrom(r0),
3120 LocationFrom(calling_convention.GetRegisterAt(0)));
3121}
3122
3123void IntrinsicCodeGeneratorARMVIXL::VisitIntegerValueOf(HInvoke* invoke) {
3124 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3125 LocationSummary* locations = invoke->GetLocations();
3126 ArmVIXLAssembler* const assembler = GetAssembler();
3127
3128 vixl32::Register out = RegisterFrom(locations->Out());
3129 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3130 vixl32::Register temp = temps.Acquire();
3131 InvokeRuntimeCallingConventionARMVIXL calling_convention;
3132 vixl32::Register argument = calling_convention.GetRegisterAt(0);
3133 if (invoke->InputAt(0)->IsConstant()) {
3134 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3135 if (value >= info.low && value <= info.high) {
3136 // Just embed the j.l.Integer in the code.
3137 ScopedObjectAccess soa(Thread::Current());
3138 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3139 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3140 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3141 __ Ldr(out, codegen_->DeduplicateBootImageAddressLiteral(address));
3142 } else {
3143 // Allocate and initialize a new j.l.Integer.
3144 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3145 // JIT object table.
3146 uint32_t address =
3147 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3148 __ Ldr(argument, codegen_->DeduplicateBootImageAddressLiteral(address));
3149 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3150 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3151 __ Mov(temp, value);
3152 assembler->StoreToOffset(kStoreWord, temp, out, info.value_offset);
3153 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3154 // one.
3155 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3156 }
3157 } else {
3158 vixl32::Register in = RegisterFrom(locations->InAt(0));
3159 // Check bounds of our cache.
3160 __ Add(out, in, -info.low);
3161 __ Cmp(out, info.high - info.low + 1);
3162 vixl32::Label allocate, done;
Anton Kirilovfd522532017-05-10 12:46:57 +01003163 __ B(hs, &allocate, /* is_far_target */ false);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003164 // If the value is within the bounds, load the j.l.Integer directly from the array.
3165 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3166 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3167 __ Ldr(temp, codegen_->DeduplicateBootImageAddressLiteral(data_offset + address));
3168 codegen_->LoadFromShiftedRegOffset(Primitive::kPrimNot, locations->Out(), temp, out);
3169 assembler->MaybeUnpoisonHeapReference(out);
3170 __ B(&done);
3171 __ Bind(&allocate);
3172 // Otherwise allocate and initialize a new j.l.Integer.
3173 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3174 __ Ldr(argument, codegen_->DeduplicateBootImageAddressLiteral(address));
3175 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3176 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3177 assembler->StoreToOffset(kStoreWord, in, out, info.value_offset);
3178 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3179 // one.
3180 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3181 __ Bind(&done);
3182 }
3183}
3184
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003185void IntrinsicLocationsBuilderARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
3186 LocationSummary* locations = new (arena_) LocationSummary(invoke,
3187 LocationSummary::kNoCall,
3188 kIntrinsified);
3189 locations->SetOut(Location::RequiresRegister());
3190}
3191
3192void IntrinsicCodeGeneratorARMVIXL::VisitThreadInterrupted(HInvoke* invoke) {
3193 ArmVIXLAssembler* assembler = GetAssembler();
3194 vixl32::Register out = RegisterFrom(invoke->GetLocations()->Out());
3195 int32_t offset = Thread::InterruptedOffset<kArmPointerSize>().Int32Value();
3196 __ Ldr(out, MemOperand(tr, offset));
3197 UseScratchRegisterScope temps(assembler->GetVIXLAssembler());
3198 vixl32::Register temp = temps.Acquire();
3199 vixl32::Label done;
Anton Kirilovfd522532017-05-10 12:46:57 +01003200 vixl32::Label* const final_label = codegen_->GetFinalLabel(invoke, &done);
3201 __ CompareAndBranchIfZero(out, final_label, /* far_target */ false);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003202 __ Dmb(vixl32::ISH);
3203 __ Mov(temp, 0);
3204 assembler->StoreToOffset(kStoreWord, temp, tr, offset);
3205 __ Dmb(vixl32::ISH);
Anton Kirilovfd522532017-05-10 12:46:57 +01003206 if (done.IsReferenced()) {
3207 __ Bind(&done);
3208 }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003209}
3210
Anton Kirilov5ec62182016-10-13 20:16:02 +01003211UNIMPLEMENTED_INTRINSIC(ARMVIXL, MathRoundDouble) // Could be done by changing rounding mode, maybe?
Anton Kirilov5ec62182016-10-13 20:16:02 +01003212UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeCASLong) // High register pressure.
3213UNIMPLEMENTED_INTRINSIC(ARMVIXL, SystemArrayCopyChar)
Vladimir Markod254f5c2017-06-02 15:18:36 +00003214UNIMPLEMENTED_INTRINSIC(ARMVIXL, ReferenceGetReferent)
Anton Kirilov5ec62182016-10-13 20:16:02 +01003215UNIMPLEMENTED_INTRINSIC(ARMVIXL, IntegerHighestOneBit)
3216UNIMPLEMENTED_INTRINSIC(ARMVIXL, LongHighestOneBit)
3217UNIMPLEMENTED_INTRINSIC(ARMVIXL, IntegerLowestOneBit)
3218UNIMPLEMENTED_INTRINSIC(ARMVIXL, LongLowestOneBit)
3219
Aart Bikff7d89c2016-11-07 08:49:28 -08003220UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOf);
3221UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003222UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferAppend);
3223UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferLength);
3224UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBufferToString);
3225UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderAppend);
3226UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderLength);
3227UNIMPLEMENTED_INTRINSIC(ARMVIXL, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003228
Anton Kirilov5ec62182016-10-13 20:16:02 +01003229// 1.8.
3230UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddInt)
3231UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndAddLong)
3232UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetInt)
3233UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetLong)
3234UNIMPLEMENTED_INTRINSIC(ARMVIXL, UnsafeGetAndSetObject)
3235
3236UNREACHABLE_INTRINSICS(ARMVIXL)
3237
3238#undef __
3239
3240} // namespace arm
3241} // namespace art