blob: d8a896e9262c4842d0dd9d38952a2405ea5746e2 [file] [log] [blame]
Andreas Gampe878d58c2015-01-15 23:24:00 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080021#include "code_generator_arm64.h"
22#include "common_arm64.h"
23#include "entrypoints/quick/quick_entrypoints.h"
24#include "intrinsics.h"
25#include "mirror/array-inl.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "mirror/string.h"
27#include "thread.h"
28#include "utils/arm64/assembler_arm64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080029
Scott Wakeling97c72b72016-06-24 16:19:36 +010030using namespace vixl::aarch64; // NOLINT(build/namespaces)
Andreas Gampe878d58c2015-01-15 23:24:00 -080031
Artem Serovaf4e42a2016-08-08 15:11:24 +010032// TODO(VIXL): Make VIXL compile with -Wshadow.
Scott Wakeling97c72b72016-06-24 16:19:36 +010033#pragma GCC diagnostic push
34#pragma GCC diagnostic ignored "-Wshadow"
Artem Serovaf4e42a2016-08-08 15:11:24 +010035#include "aarch64/disasm-aarch64.h"
36#include "aarch64/macro-assembler-aarch64.h"
Scott Wakeling97c72b72016-06-24 16:19:36 +010037#pragma GCC diagnostic pop
Andreas Gampe878d58c2015-01-15 23:24:00 -080038
39namespace art {
40
41namespace arm64 {
42
43using helpers::DRegisterFrom;
44using helpers::FPRegisterFrom;
45using helpers::HeapOperand;
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000046using helpers::LocationFrom;
Scott Wakeling9ee23f42015-07-23 10:44:35 +010047using helpers::OperandFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080048using helpers::RegisterFrom;
49using helpers::SRegisterFrom;
50using helpers::WRegisterFrom;
51using helpers::XRegisterFrom;
xueliang.zhong49924c92016-03-03 10:52:51 +000052using helpers::InputRegisterAt;
Scott Wakeling1f36f412016-04-21 11:13:45 +010053using helpers::OutputRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080054
Andreas Gampe878d58c2015-01-15 23:24:00 -080055namespace {
56
57ALWAYS_INLINE inline MemOperand AbsoluteHeapOperandFrom(Location location, size_t offset = 0) {
58 return MemOperand(XRegisterFrom(location), offset);
59}
60
61} // namespace
62
Scott Wakeling97c72b72016-06-24 16:19:36 +010063MacroAssembler* IntrinsicCodeGeneratorARM64::GetVIXLAssembler() {
Alexandre Rames087930f2016-08-02 13:45:28 +010064 return codegen_->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -080065}
66
67ArenaAllocator* IntrinsicCodeGeneratorARM64::GetAllocator() {
68 return codegen_->GetGraph()->GetArena();
69}
70
Alexandre Rames087930f2016-08-02 13:45:28 +010071#define __ codegen->GetVIXLAssembler()->
Andreas Gampe878d58c2015-01-15 23:24:00 -080072
73static void MoveFromReturnRegister(Location trg,
74 Primitive::Type type,
75 CodeGeneratorARM64* codegen) {
76 if (!trg.IsValid()) {
77 DCHECK(type == Primitive::kPrimVoid);
78 return;
79 }
80
81 DCHECK_NE(type, Primitive::kPrimVoid);
82
Jeff Hao848f70a2014-01-15 13:49:50 -080083 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080084 Register trg_reg = RegisterFrom(trg, type);
85 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
86 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
87 } else {
88 FPRegister trg_reg = FPRegisterFrom(trg, type);
89 FPRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
90 __ Fmov(trg_reg, res_reg);
91 }
92}
93
Roland Levillainec525fc2015-04-28 15:50:20 +010094static void MoveArguments(HInvoke* invoke, CodeGeneratorARM64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010095 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010096 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe878d58c2015-01-15 23:24:00 -080097}
98
99// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
100// call. This will copy the arguments into the positions for a regular call.
101//
102// Note: The actual parameters are required to be in the locations given by the invoke's location
103// summary. If an intrinsic modifies those locations before a slowpath call, they must be
104// restored!
105class IntrinsicSlowPathARM64 : public SlowPathCodeARM64 {
106 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000107 explicit IntrinsicSlowPathARM64(HInvoke* invoke)
108 : SlowPathCodeARM64(invoke), invoke_(invoke) { }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800109
110 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
111 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
112 __ Bind(GetEntryLabel());
113
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000114 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800115
Roland Levillainec525fc2015-04-28 15:50:20 +0100116 MoveArguments(invoke_, codegen);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800117
118 if (invoke_->IsInvokeStaticOrDirect()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100119 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
120 LocationFrom(kArtMethodRegister));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800121 } else {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000122 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), LocationFrom(kArtMethodRegister));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800123 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000124 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800125
126 // Copy the result back to the expected output.
127 Location out = invoke_->GetLocations()->Out();
128 if (out.IsValid()) {
129 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
130 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
131 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
132 }
133
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800135 __ B(GetExitLabel());
136 }
137
Alexandre Rames9931f312015-06-19 14:47:01 +0100138 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathARM64"; }
139
Andreas Gampe878d58c2015-01-15 23:24:00 -0800140 private:
141 // The instruction where this slow path is happening.
142 HInvoke* const invoke_;
143
144 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARM64);
145};
146
Roland Levillain0b671c02016-08-19 12:02:34 +0100147// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
148class ReadBarrierSystemArrayCopySlowPathARM64 : public SlowPathCodeARM64 {
149 public:
150 ReadBarrierSystemArrayCopySlowPathARM64(HInstruction* instruction, Location tmp)
151 : SlowPathCodeARM64(instruction), tmp_(tmp) {
152 DCHECK(kEmitCompilerReadBarrier);
153 DCHECK(kUseBakerReadBarrier);
154 }
155
156 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
157 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
158 LocationSummary* locations = instruction_->GetLocations();
159 DCHECK(locations->CanCall());
160 DCHECK(instruction_->IsInvokeStaticOrDirect())
161 << "Unexpected instruction in read barrier arraycopy slow path: "
162 << instruction_->DebugName();
163 DCHECK(instruction_->GetLocations()->Intrinsified());
164 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
165
166 const int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
167
168 Register src_curr_addr = XRegisterFrom(locations->GetTemp(0));
169 Register dst_curr_addr = XRegisterFrom(locations->GetTemp(1));
170 Register src_stop_addr = XRegisterFrom(locations->GetTemp(2));
171 Register tmp_reg = WRegisterFrom(tmp_);
172
173 __ Bind(GetEntryLabel());
174 vixl::aarch64::Label slow_copy_loop;
175 __ Bind(&slow_copy_loop);
176 __ Ldr(tmp_reg, MemOperand(src_curr_addr, element_size, PostIndex));
177 codegen->GetAssembler()->MaybeUnpoisonHeapReference(tmp_reg);
178 // TODO: Inline the mark bit check before calling the runtime?
179 // tmp_reg = ReadBarrier::Mark(tmp_reg);
180 // No need to save live registers; it's taken care of by the
181 // entrypoint. Also, there is no need to update the stack mask,
182 // as this runtime call will not trigger a garbage collection.
183 // (See ReadBarrierMarkSlowPathARM64::EmitNativeCode for more
184 // explanations.)
185 DCHECK_NE(tmp_.reg(), LR);
186 DCHECK_NE(tmp_.reg(), WSP);
187 DCHECK_NE(tmp_.reg(), WZR);
188 // IP0 is used internally by the ReadBarrierMarkRegX entry point
189 // as a temporary (and not preserved). It thus cannot be used by
190 // any live register in this slow path.
191 DCHECK_NE(LocationFrom(src_curr_addr).reg(), IP0);
192 DCHECK_NE(LocationFrom(dst_curr_addr).reg(), IP0);
193 DCHECK_NE(LocationFrom(src_stop_addr).reg(), IP0);
194 DCHECK_NE(tmp_.reg(), IP0);
195 DCHECK(0 <= tmp_.reg() && tmp_.reg() < kNumberOfWRegisters) << tmp_.reg();
196 int32_t entry_point_offset =
197 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(tmp_.reg());
198 // This runtime call does not require a stack map.
199 codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
200 codegen->GetAssembler()->MaybePoisonHeapReference(tmp_reg);
201 __ Str(tmp_reg, MemOperand(dst_curr_addr, element_size, PostIndex));
202 __ Cmp(src_curr_addr, src_stop_addr);
203 __ B(&slow_copy_loop, ne);
204 __ B(GetExitLabel());
205 }
206
207 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathARM64"; }
208
209 private:
210 Location tmp_;
211
212 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARM64);
213};
Andreas Gampe878d58c2015-01-15 23:24:00 -0800214#undef __
215
216bool IntrinsicLocationsBuilderARM64::TryDispatch(HInvoke* invoke) {
217 Dispatch(invoke);
218 LocationSummary* res = invoke->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000219 if (res == nullptr) {
220 return false;
221 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000222 return res->Intrinsified();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800223}
224
225#define __ masm->
226
227static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
228 LocationSummary* locations = new (arena) LocationSummary(invoke,
229 LocationSummary::kNoCall,
230 kIntrinsified);
231 locations->SetInAt(0, Location::RequiresFpuRegister());
232 locations->SetOut(Location::RequiresRegister());
233}
234
235static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
236 LocationSummary* locations = new (arena) LocationSummary(invoke,
237 LocationSummary::kNoCall,
238 kIntrinsified);
239 locations->SetInAt(0, Location::RequiresRegister());
240 locations->SetOut(Location::RequiresFpuRegister());
241}
242
Scott Wakeling97c72b72016-06-24 16:19:36 +0100243static void MoveFPToInt(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800244 Location input = locations->InAt(0);
245 Location output = locations->Out();
246 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
247 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
248}
249
Scott Wakeling97c72b72016-06-24 16:19:36 +0100250static void MoveIntToFP(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800251 Location input = locations->InAt(0);
252 Location output = locations->Out();
253 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
254 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
255}
256
257void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
258 CreateFPToIntLocations(arena_, invoke);
259}
260void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
261 CreateIntToFPLocations(arena_, invoke);
262}
263
264void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000265 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800266}
267void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000268 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800269}
270
271void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
272 CreateFPToIntLocations(arena_, invoke);
273}
274void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
275 CreateIntToFPLocations(arena_, invoke);
276}
277
278void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000279 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800280}
281void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000282 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800283}
284
285static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
286 LocationSummary* locations = new (arena) LocationSummary(invoke,
287 LocationSummary::kNoCall,
288 kIntrinsified);
289 locations->SetInAt(0, Location::RequiresRegister());
290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
291}
292
293static void GenReverseBytes(LocationSummary* locations,
294 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100295 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800296 Location in = locations->InAt(0);
297 Location out = locations->Out();
298
299 switch (type) {
300 case Primitive::kPrimShort:
301 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
302 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
303 break;
304 case Primitive::kPrimInt:
305 case Primitive::kPrimLong:
306 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
307 break;
308 default:
309 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
310 UNREACHABLE();
311 }
312}
313
314void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
315 CreateIntToIntLocations(arena_, invoke);
316}
317
318void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
319 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
320}
321
322void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
323 CreateIntToIntLocations(arena_, invoke);
324}
325
326void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
327 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
328}
329
330void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
331 CreateIntToIntLocations(arena_, invoke);
332}
333
334void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
335 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetVIXLAssembler());
336}
337
Aart Bik7b565022016-01-28 14:36:22 -0800338static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
339 LocationSummary* locations = new (arena) LocationSummary(invoke,
340 LocationSummary::kNoCall,
341 kIntrinsified);
342 locations->SetInAt(0, Location::RequiresRegister());
343 locations->SetInAt(1, Location::RequiresRegister());
344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
345}
346
Scott Wakeling611d3392015-07-10 11:42:06 +0100347static void GenNumberOfLeadingZeros(LocationSummary* locations,
348 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100349 MacroAssembler* masm) {
Scott Wakeling611d3392015-07-10 11:42:06 +0100350 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
351
352 Location in = locations->InAt(0);
353 Location out = locations->Out();
354
355 __ Clz(RegisterFrom(out, type), RegisterFrom(in, type));
356}
357
358void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
359 CreateIntToIntLocations(arena_, invoke);
360}
361
362void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
363 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
364}
365
366void IntrinsicLocationsBuilderARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
367 CreateIntToIntLocations(arena_, invoke);
368}
369
370void IntrinsicCodeGeneratorARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
371 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
372}
373
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100374static void GenNumberOfTrailingZeros(LocationSummary* locations,
375 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100376 MacroAssembler* masm) {
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100377 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
378
379 Location in = locations->InAt(0);
380 Location out = locations->Out();
381
382 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
383 __ Clz(RegisterFrom(out, type), RegisterFrom(out, type));
384}
385
386void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
387 CreateIntToIntLocations(arena_, invoke);
388}
389
390void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
391 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
392}
393
394void IntrinsicLocationsBuilderARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
395 CreateIntToIntLocations(arena_, invoke);
396}
397
398void IntrinsicCodeGeneratorARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
399 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
400}
401
Andreas Gampe878d58c2015-01-15 23:24:00 -0800402static void GenReverse(LocationSummary* locations,
403 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100404 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800405 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
406
407 Location in = locations->InAt(0);
408 Location out = locations->Out();
409
410 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
411}
412
413void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
414 CreateIntToIntLocations(arena_, invoke);
415}
416
417void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
418 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
419}
420
421void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
422 CreateIntToIntLocations(arena_, invoke);
423}
424
425void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
426 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
427}
428
Scott Wakeling97c72b72016-06-24 16:19:36 +0100429static void GenBitCount(HInvoke* instr, Primitive::Type type, MacroAssembler* masm) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100430 DCHECK(Primitive::IsIntOrLongType(type)) << type;
431 DCHECK_EQ(instr->GetType(), Primitive::kPrimInt);
432 DCHECK_EQ(Primitive::PrimitiveKind(instr->InputAt(0)->GetType()), type);
xueliang.zhong49924c92016-03-03 10:52:51 +0000433
xueliang.zhong49924c92016-03-03 10:52:51 +0000434 UseScratchRegisterScope temps(masm);
435
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000436 Register src = InputRegisterAt(instr, 0);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100437 Register dst = RegisterFrom(instr->GetLocations()->Out(), type);
438 FPRegister fpr = (type == Primitive::kPrimLong) ? temps.AcquireD() : temps.AcquireS();
xueliang.zhong49924c92016-03-03 10:52:51 +0000439
440 __ Fmov(fpr, src);
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000441 __ Cnt(fpr.V8B(), fpr.V8B());
442 __ Addv(fpr.B(), fpr.V8B());
xueliang.zhong49924c92016-03-03 10:52:51 +0000443 __ Fmov(dst, fpr);
444}
445
446void IntrinsicLocationsBuilderARM64::VisitLongBitCount(HInvoke* invoke) {
447 CreateIntToIntLocations(arena_, invoke);
448}
449
450void IntrinsicCodeGeneratorARM64::VisitLongBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100451 GenBitCount(invoke, Primitive::kPrimLong, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000452}
453
454void IntrinsicLocationsBuilderARM64::VisitIntegerBitCount(HInvoke* invoke) {
455 CreateIntToIntLocations(arena_, invoke);
456}
457
458void IntrinsicCodeGeneratorARM64::VisitIntegerBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100459 GenBitCount(invoke, Primitive::kPrimInt, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000460}
461
Andreas Gampe878d58c2015-01-15 23:24:00 -0800462static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800463 LocationSummary* locations = new (arena) LocationSummary(invoke,
464 LocationSummary::kNoCall,
465 kIntrinsified);
466 locations->SetInAt(0, Location::RequiresFpuRegister());
467 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
468}
469
Scott Wakeling97c72b72016-06-24 16:19:36 +0100470static void MathAbsFP(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800471 Location in = locations->InAt(0);
472 Location out = locations->Out();
473
474 FPRegister in_reg = is64bit ? DRegisterFrom(in) : SRegisterFrom(in);
475 FPRegister out_reg = is64bit ? DRegisterFrom(out) : SRegisterFrom(out);
476
477 __ Fabs(out_reg, in_reg);
478}
479
480void IntrinsicLocationsBuilderARM64::VisitMathAbsDouble(HInvoke* invoke) {
481 CreateFPToFPLocations(arena_, invoke);
482}
483
484void IntrinsicCodeGeneratorARM64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000485 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800486}
487
488void IntrinsicLocationsBuilderARM64::VisitMathAbsFloat(HInvoke* invoke) {
489 CreateFPToFPLocations(arena_, invoke);
490}
491
492void IntrinsicCodeGeneratorARM64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000493 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800494}
495
496static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
497 LocationSummary* locations = new (arena) LocationSummary(invoke,
498 LocationSummary::kNoCall,
499 kIntrinsified);
500 locations->SetInAt(0, Location::RequiresRegister());
501 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
502}
503
504static void GenAbsInteger(LocationSummary* locations,
505 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100506 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800507 Location in = locations->InAt(0);
508 Location output = locations->Out();
509
510 Register in_reg = is64bit ? XRegisterFrom(in) : WRegisterFrom(in);
511 Register out_reg = is64bit ? XRegisterFrom(output) : WRegisterFrom(output);
512
513 __ Cmp(in_reg, Operand(0));
514 __ Cneg(out_reg, in_reg, lt);
515}
516
517void IntrinsicLocationsBuilderARM64::VisitMathAbsInt(HInvoke* invoke) {
518 CreateIntToInt(arena_, invoke);
519}
520
521void IntrinsicCodeGeneratorARM64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000522 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800523}
524
525void IntrinsicLocationsBuilderARM64::VisitMathAbsLong(HInvoke* invoke) {
526 CreateIntToInt(arena_, invoke);
527}
528
529void IntrinsicCodeGeneratorARM64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000530 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800531}
532
533static void GenMinMaxFP(LocationSummary* locations,
534 bool is_min,
535 bool is_double,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100536 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800537 Location op1 = locations->InAt(0);
538 Location op2 = locations->InAt(1);
539 Location out = locations->Out();
540
541 FPRegister op1_reg = is_double ? DRegisterFrom(op1) : SRegisterFrom(op1);
542 FPRegister op2_reg = is_double ? DRegisterFrom(op2) : SRegisterFrom(op2);
543 FPRegister out_reg = is_double ? DRegisterFrom(out) : SRegisterFrom(out);
544 if (is_min) {
545 __ Fmin(out_reg, op1_reg, op2_reg);
546 } else {
547 __ Fmax(out_reg, op1_reg, op2_reg);
548 }
549}
550
551static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
552 LocationSummary* locations = new (arena) LocationSummary(invoke,
553 LocationSummary::kNoCall,
554 kIntrinsified);
555 locations->SetInAt(0, Location::RequiresFpuRegister());
556 locations->SetInAt(1, Location::RequiresFpuRegister());
557 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
558}
559
560void IntrinsicLocationsBuilderARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
561 CreateFPFPToFPLocations(arena_, invoke);
562}
563
564void IntrinsicCodeGeneratorARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000565 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800566}
567
568void IntrinsicLocationsBuilderARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
569 CreateFPFPToFPLocations(arena_, invoke);
570}
571
572void IntrinsicCodeGeneratorARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000573 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800574}
575
576void IntrinsicLocationsBuilderARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
577 CreateFPFPToFPLocations(arena_, invoke);
578}
579
580void IntrinsicCodeGeneratorARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000581 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800582}
583
584void IntrinsicLocationsBuilderARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
585 CreateFPFPToFPLocations(arena_, invoke);
586}
587
588void IntrinsicCodeGeneratorARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000589 GenMinMaxFP(
590 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800591}
592
593static void GenMinMax(LocationSummary* locations,
594 bool is_min,
595 bool is_long,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100596 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800597 Location op1 = locations->InAt(0);
598 Location op2 = locations->InAt(1);
599 Location out = locations->Out();
600
601 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
602 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
603 Register out_reg = is_long ? XRegisterFrom(out) : WRegisterFrom(out);
604
605 __ Cmp(op1_reg, op2_reg);
606 __ Csel(out_reg, op1_reg, op2_reg, is_min ? lt : gt);
607}
608
Andreas Gampe878d58c2015-01-15 23:24:00 -0800609void IntrinsicLocationsBuilderARM64::VisitMathMinIntInt(HInvoke* invoke) {
610 CreateIntIntToIntLocations(arena_, invoke);
611}
612
613void IntrinsicCodeGeneratorARM64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000614 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800615}
616
617void IntrinsicLocationsBuilderARM64::VisitMathMinLongLong(HInvoke* invoke) {
618 CreateIntIntToIntLocations(arena_, invoke);
619}
620
621void IntrinsicCodeGeneratorARM64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000622 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800623}
624
625void IntrinsicLocationsBuilderARM64::VisitMathMaxIntInt(HInvoke* invoke) {
626 CreateIntIntToIntLocations(arena_, invoke);
627}
628
629void IntrinsicCodeGeneratorARM64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000630 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800631}
632
633void IntrinsicLocationsBuilderARM64::VisitMathMaxLongLong(HInvoke* invoke) {
634 CreateIntIntToIntLocations(arena_, invoke);
635}
636
637void IntrinsicCodeGeneratorARM64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000638 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800639}
640
641void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
642 CreateFPToFPLocations(arena_, invoke);
643}
644
645void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
646 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100647 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800648 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
649}
650
651void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
652 CreateFPToFPLocations(arena_, invoke);
653}
654
655void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
656 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100657 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800658 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
659}
660
661void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
662 CreateFPToFPLocations(arena_, invoke);
663}
664
665void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
666 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100667 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800668 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
669}
670
671void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
672 CreateFPToFPLocations(arena_, invoke);
673}
674
675void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
676 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100677 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800678 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
679}
680
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100681static void CreateFPToIntPlusFPTempLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800682 LocationSummary* locations = new (arena) LocationSummary(invoke,
683 LocationSummary::kNoCall,
684 kIntrinsified);
685 locations->SetInAt(0, Location::RequiresFpuRegister());
686 locations->SetOut(Location::RequiresRegister());
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100687 locations->AddTemp(Location::RequiresFpuRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800688}
689
Scott Wakeling97c72b72016-06-24 16:19:36 +0100690static void GenMathRound(HInvoke* invoke, bool is_double, vixl::aarch64::MacroAssembler* masm) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100691 // Java 8 API definition for Math.round():
692 // Return the closest long or int to the argument, with ties rounding to positive infinity.
693 //
694 // There is no single instruction in ARMv8 that can support the above definition.
695 // We choose to use FCVTAS here, because it has closest semantic.
696 // FCVTAS performs rounding to nearest integer, ties away from zero.
697 // For most inputs (positive values, zero or NaN), this instruction is enough.
698 // We only need a few handling code after FCVTAS if the input is negative half value.
699 //
700 // The reason why we didn't choose FCVTPS instruction here is that
701 // although it performs rounding toward positive infinity, it doesn't perform rounding to nearest.
702 // For example, FCVTPS(-1.9) = -1 and FCVTPS(1.1) = 2.
703 // If we were using this instruction, for most inputs, more handling code would be needed.
704 LocationSummary* l = invoke->GetLocations();
705 FPRegister in_reg = is_double ? DRegisterFrom(l->InAt(0)) : SRegisterFrom(l->InAt(0));
706 FPRegister tmp_fp = is_double ? DRegisterFrom(l->GetTemp(0)) : SRegisterFrom(l->GetTemp(0));
707 Register out_reg = is_double ? XRegisterFrom(l->Out()) : WRegisterFrom(l->Out());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100708 vixl::aarch64::Label done;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800709
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100710 // Round to nearest integer, ties away from zero.
711 __ Fcvtas(out_reg, in_reg);
712
713 // For positive values, zero or NaN inputs, rounding is done.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100714 __ Tbz(out_reg, out_reg.GetSizeInBits() - 1, &done);
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100715
716 // Handle input < 0 cases.
717 // If input is negative but not a tie, previous result (round to nearest) is valid.
718 // If input is a negative tie, out_reg += 1.
719 __ Frinta(tmp_fp, in_reg);
720 __ Fsub(tmp_fp, in_reg, tmp_fp);
721 __ Fcmp(tmp_fp, 0.5);
722 __ Cinc(out_reg, out_reg, eq);
723
724 __ Bind(&done);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800725}
726
727void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100728 CreateFPToIntPlusFPTempLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800729}
730
731void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100732 GenMathRound(invoke, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800733}
734
735void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100736 CreateFPToIntPlusFPTempLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800737}
738
739void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100740 GenMathRound(invoke, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800741}
742
743void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
744 CreateIntToIntLocations(arena_, invoke);
745}
746
747void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100748 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800749 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
750 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
751}
752
753void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
754 CreateIntToIntLocations(arena_, invoke);
755}
756
757void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100758 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800759 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
760 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
761}
762
763void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
764 CreateIntToIntLocations(arena_, invoke);
765}
766
767void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100768 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800769 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
770 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
771}
772
773void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
774 CreateIntToIntLocations(arena_, invoke);
775}
776
777void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100778 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800779 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
780 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
781}
782
783static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
784 LocationSummary* locations = new (arena) LocationSummary(invoke,
785 LocationSummary::kNoCall,
786 kIntrinsified);
787 locations->SetInAt(0, Location::RequiresRegister());
788 locations->SetInAt(1, Location::RequiresRegister());
789}
790
791void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
792 CreateIntIntToVoidLocations(arena_, invoke);
793}
794
795void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100796 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800797 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
798 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
799}
800
801void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
802 CreateIntIntToVoidLocations(arena_, invoke);
803}
804
805void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100806 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800807 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
808 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
809}
810
811void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
812 CreateIntIntToVoidLocations(arena_, invoke);
813}
814
815void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100816 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800817 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
818 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
819}
820
821void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
822 CreateIntIntToVoidLocations(arena_, invoke);
823}
824
825void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100826 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800827 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
828 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
829}
830
831void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
832 LocationSummary* locations = new (arena_) LocationSummary(invoke,
833 LocationSummary::kNoCall,
834 kIntrinsified);
835 locations->SetOut(Location::RequiresRegister());
836}
837
838void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
839 codegen_->Load(Primitive::kPrimNot, WRegisterFrom(invoke->GetLocations()->Out()),
Andreas Gampe542451c2016-07-26 09:02:02 -0700840 MemOperand(tr, Thread::PeerOffset<kArm64PointerSize>().Int32Value()));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800841}
842
843static void GenUnsafeGet(HInvoke* invoke,
844 Primitive::Type type,
845 bool is_volatile,
846 CodeGeneratorARM64* codegen) {
847 LocationSummary* locations = invoke->GetLocations();
848 DCHECK((type == Primitive::kPrimInt) ||
849 (type == Primitive::kPrimLong) ||
850 (type == Primitive::kPrimNot));
Alexandre Rames087930f2016-08-02 13:45:28 +0100851 MacroAssembler* masm = codegen->GetVIXLAssembler();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000852 Location base_loc = locations->InAt(1);
853 Register base = WRegisterFrom(base_loc); // Object pointer.
854 Location offset_loc = locations->InAt(2);
855 Register offset = XRegisterFrom(offset_loc); // Long offset.
856 Location trg_loc = locations->Out();
857 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800858
Roland Levillain44015862016-01-22 11:47:17 +0000859 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
860 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
861 UseScratchRegisterScope temps(masm);
862 Register temp = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +0100863 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
864 trg_loc,
865 base,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100866 /* offset */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100867 /* index */ offset_loc,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100868 /* scale_factor */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100869 temp,
870 /* needs_null_check */ false,
871 is_volatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800872 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000873 // Other cases.
874 MemOperand mem_op(base.X(), offset);
875 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000876 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000877 } else {
878 codegen->Load(type, trg, mem_op);
879 }
Roland Levillain4d027112015-07-01 15:41:14 +0100880
Roland Levillain44015862016-01-22 11:47:17 +0000881 if (type == Primitive::kPrimNot) {
882 DCHECK(trg.IsW());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100883 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0u, offset_loc);
Roland Levillain44015862016-01-22 11:47:17 +0000884 }
Roland Levillain4d027112015-07-01 15:41:14 +0100885 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800886}
887
888static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000889 bool can_call = kEmitCompilerReadBarrier &&
890 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
891 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800892 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100893 (can_call
894 ? LocationSummary::kCallOnSlowPath
895 : LocationSummary::kNoCall),
Andreas Gampe878d58c2015-01-15 23:24:00 -0800896 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +0100897 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100898 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +0100899 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800900 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
901 locations->SetInAt(1, Location::RequiresRegister());
902 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100903 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100904 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800905}
906
907void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
908 CreateIntIntIntToIntLocations(arena_, invoke);
909}
910void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
911 CreateIntIntIntToIntLocations(arena_, invoke);
912}
913void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
914 CreateIntIntIntToIntLocations(arena_, invoke);
915}
916void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
917 CreateIntIntIntToIntLocations(arena_, invoke);
918}
919void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
920 CreateIntIntIntToIntLocations(arena_, invoke);
921}
922void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
923 CreateIntIntIntToIntLocations(arena_, invoke);
924}
925
926void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000927 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800928}
929void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000930 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800931}
932void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000933 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800934}
935void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000936 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800937}
938void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000939 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800940}
941void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000942 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800943}
944
945static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
946 LocationSummary* locations = new (arena) LocationSummary(invoke,
947 LocationSummary::kNoCall,
948 kIntrinsified);
949 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
950 locations->SetInAt(1, Location::RequiresRegister());
951 locations->SetInAt(2, Location::RequiresRegister());
952 locations->SetInAt(3, Location::RequiresRegister());
953}
954
955void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
956 CreateIntIntIntIntToVoid(arena_, invoke);
957}
958void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
959 CreateIntIntIntIntToVoid(arena_, invoke);
960}
961void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
962 CreateIntIntIntIntToVoid(arena_, invoke);
963}
964void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
965 CreateIntIntIntIntToVoid(arena_, invoke);
966}
967void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
968 CreateIntIntIntIntToVoid(arena_, invoke);
969}
970void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
971 CreateIntIntIntIntToVoid(arena_, invoke);
972}
973void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
974 CreateIntIntIntIntToVoid(arena_, invoke);
975}
976void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
977 CreateIntIntIntIntToVoid(arena_, invoke);
978}
979void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
980 CreateIntIntIntIntToVoid(arena_, invoke);
981}
982
983static void GenUnsafePut(LocationSummary* locations,
984 Primitive::Type type,
985 bool is_volatile,
986 bool is_ordered,
987 CodeGeneratorARM64* codegen) {
Alexandre Rames087930f2016-08-02 13:45:28 +0100988 MacroAssembler* masm = codegen->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800989
990 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
991 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
992 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100993 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800994 MemOperand mem_op(base.X(), offset);
995
Roland Levillain4d027112015-07-01 15:41:14 +0100996 {
997 // We use a block to end the scratch scope before the write barrier, thus
998 // freeing the temporary registers so they can be used in `MarkGCCard`.
999 UseScratchRegisterScope temps(masm);
1000
1001 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1002 DCHECK(value.IsW());
1003 Register temp = temps.AcquireW();
1004 __ Mov(temp.W(), value.W());
1005 codegen->GetAssembler()->PoisonHeapReference(temp.W());
1006 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -08001007 }
Roland Levillain4d027112015-07-01 15:41:14 +01001008
1009 if (is_volatile || is_ordered) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001010 codegen->StoreRelease(type, source, mem_op);
Roland Levillain4d027112015-07-01 15:41:14 +01001011 } else {
1012 codegen->Store(type, source, mem_op);
1013 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001014 }
1015
1016 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001017 bool value_can_be_null = true; // TODO: Worth finding out this information?
1018 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001019 }
1020}
1021
1022void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001023 GenUnsafePut(invoke->GetLocations(),
1024 Primitive::kPrimInt,
1025 /* is_volatile */ false,
1026 /* is_ordered */ false,
1027 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001028}
1029void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001030 GenUnsafePut(invoke->GetLocations(),
1031 Primitive::kPrimInt,
1032 /* is_volatile */ false,
1033 /* is_ordered */ true,
1034 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001035}
1036void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001037 GenUnsafePut(invoke->GetLocations(),
1038 Primitive::kPrimInt,
1039 /* is_volatile */ true,
1040 /* is_ordered */ false,
1041 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001042}
1043void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001044 GenUnsafePut(invoke->GetLocations(),
1045 Primitive::kPrimNot,
1046 /* is_volatile */ false,
1047 /* is_ordered */ false,
1048 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001049}
1050void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001051 GenUnsafePut(invoke->GetLocations(),
1052 Primitive::kPrimNot,
1053 /* is_volatile */ false,
1054 /* is_ordered */ true,
1055 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001056}
1057void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001058 GenUnsafePut(invoke->GetLocations(),
1059 Primitive::kPrimNot,
1060 /* is_volatile */ true,
1061 /* is_ordered */ false,
1062 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001063}
1064void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001065 GenUnsafePut(invoke->GetLocations(),
1066 Primitive::kPrimLong,
1067 /* is_volatile */ false,
1068 /* is_ordered */ false,
1069 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001070}
1071void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001072 GenUnsafePut(invoke->GetLocations(),
1073 Primitive::kPrimLong,
1074 /* is_volatile */ false,
1075 /* is_ordered */ true,
1076 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001077}
1078void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001079 GenUnsafePut(invoke->GetLocations(),
1080 Primitive::kPrimLong,
1081 /* is_volatile */ true,
1082 /* is_ordered */ false,
1083 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001084}
1085
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001086static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
1087 HInvoke* invoke,
1088 Primitive::Type type) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001089 bool can_call = kEmitCompilerReadBarrier &&
1090 kUseBakerReadBarrier &&
1091 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001092 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001093 (can_call
1094 ? LocationSummary::kCallOnSlowPath
1095 : LocationSummary::kNoCall),
Andreas Gampe878d58c2015-01-15 23:24:00 -08001096 kIntrinsified);
1097 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1098 locations->SetInAt(1, Location::RequiresRegister());
1099 locations->SetInAt(2, Location::RequiresRegister());
1100 locations->SetInAt(3, Location::RequiresRegister());
1101 locations->SetInAt(4, Location::RequiresRegister());
1102
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001103 // If heap poisoning is enabled, we don't want the unpoisoning
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001104 // operations to potentially clobber the output. Likewise when
1105 // emitting a (Baker) read barrier, which may call.
1106 Location::OutputOverlap overlaps =
1107 ((kPoisonHeapReferences && type == Primitive::kPrimNot) || can_call)
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001108 ? Location::kOutputOverlap
1109 : Location::kNoOutputOverlap;
1110 locations->SetOut(Location::RequiresRegister(), overlaps);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001111 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1112 // Temporary register for (Baker) read barrier.
1113 locations->AddTemp(Location::RequiresRegister());
1114 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001115}
1116
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001117static void GenCas(HInvoke* invoke, Primitive::Type type, CodeGeneratorARM64* codegen) {
Alexandre Rames087930f2016-08-02 13:45:28 +01001118 MacroAssembler* masm = codegen->GetVIXLAssembler();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001119 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001120
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001121 Location out_loc = locations->Out();
1122 Register out = WRegisterFrom(out_loc); // Boolean result.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001123
1124 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001125 Location offset_loc = locations->InAt(2);
1126 Register offset = XRegisterFrom(offset_loc); // Long offset.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001127 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1128 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1129
1130 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
1131 if (type == Primitive::kPrimNot) {
1132 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001133 bool value_can_be_null = true; // TODO: Worth finding out this information?
1134 codegen->MarkGCCard(base, value, value_can_be_null);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001135
1136 // The only read barrier implementation supporting the
1137 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1138 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1139
1140 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1141 Register temp = WRegisterFrom(locations->GetTemp(0));
1142 // Need to make sure the reference stored in the field is a to-space
1143 // one before attempting the CAS or the CAS could fail incorrectly.
1144 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1145 invoke,
1146 out_loc, // Unused, used only as a "temporary" within the read barrier.
1147 base,
1148 /* offset */ 0u,
1149 /* index */ offset_loc,
1150 /* scale_factor */ 0u,
1151 temp,
1152 /* needs_null_check */ false,
1153 /* use_load_acquire */ false,
1154 /* always_update_field */ true);
1155 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001156 }
1157
1158 UseScratchRegisterScope temps(masm);
1159 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1160 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1161
1162 Register tmp_32 = tmp_value.W();
1163
1164 __ Add(tmp_ptr, base.X(), Operand(offset));
1165
Roland Levillain4d027112015-07-01 15:41:14 +01001166 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1167 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001168 if (value.Is(expected)) {
1169 // Do not poison `value`, as it is the same register as
1170 // `expected`, which has just been poisoned.
1171 } else {
1172 codegen->GetAssembler()->PoisonHeapReference(value);
1173 }
Roland Levillain4d027112015-07-01 15:41:14 +01001174 }
1175
Andreas Gampe878d58c2015-01-15 23:24:00 -08001176 // do {
1177 // tmp_value = [tmp_ptr] - expected;
1178 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1179 // result = tmp_value != 0;
1180
Scott Wakeling97c72b72016-06-24 16:19:36 +01001181 vixl::aarch64::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001182 __ Bind(&loop_head);
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001183 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1184 __ Cmp(tmp_value, expected);
1185 __ B(&exit_loop, ne);
1186 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1187 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001188 __ Bind(&exit_loop);
1189 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001190
1191 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01001192 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001193 if (value.Is(expected)) {
1194 // Do not unpoison `value`, as it is the same register as
1195 // `expected`, which has just been unpoisoned.
1196 } else {
1197 codegen->GetAssembler()->UnpoisonHeapReference(value);
1198 }
Roland Levillain4d027112015-07-01 15:41:14 +01001199 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001200}
1201
1202void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001203 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001204}
1205void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001206 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001207}
1208void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001209 // The only read barrier implementation supporting the
1210 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1211 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001212 return;
1213 }
1214
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001215 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001216}
1217
1218void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001219 GenCas(invoke, Primitive::kPrimInt, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001220}
1221void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001222 GenCas(invoke, Primitive::kPrimLong, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001223}
1224void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001225 // The only read barrier implementation supporting the
1226 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1227 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001228
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001229 GenCas(invoke, Primitive::kPrimNot, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001230}
1231
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001232void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001233 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Scott Wakeling1f36f412016-04-21 11:13:45 +01001234 invoke->InputAt(1)->CanBeNull()
1235 ? LocationSummary::kCallOnSlowPath
1236 : LocationSummary::kNoCall,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001237 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001238 locations->SetInAt(0, Location::RequiresRegister());
1239 locations->SetInAt(1, Location::RequiresRegister());
1240 locations->AddTemp(Location::RequiresRegister());
1241 locations->AddTemp(Location::RequiresRegister());
1242 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001243 // Need temporary registers for String compression's feature.
1244 if (mirror::kUseStringCompression) {
1245 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001246 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001247 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001248}
1249
1250void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001251 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001252 LocationSummary* locations = invoke->GetLocations();
1253
Alexandre Rames2ea91532016-08-11 17:04:14 +01001254 Register str = InputRegisterAt(invoke, 0);
1255 Register arg = InputRegisterAt(invoke, 1);
1256 DCHECK(str.IsW());
1257 DCHECK(arg.IsW());
Scott Wakeling1f36f412016-04-21 11:13:45 +01001258 Register out = OutputRegister(invoke);
1259
1260 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1261 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1262 Register temp2 = WRegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001263 Register temp3;
jessicahandojo05765752016-09-09 19:01:32 -07001264 if (mirror::kUseStringCompression) {
1265 temp3 = WRegisterFrom(locations->GetTemp(3));
jessicahandojo05765752016-09-09 19:01:32 -07001266 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001267
Scott Wakeling97c72b72016-06-24 16:19:36 +01001268 vixl::aarch64::Label loop;
1269 vixl::aarch64::Label find_char_diff;
1270 vixl::aarch64::Label end;
jessicahandojo05765752016-09-09 19:01:32 -07001271 vixl::aarch64::Label different_compression;
Scott Wakeling1f36f412016-04-21 11:13:45 +01001272
1273 // Get offsets of count and value fields within a string object.
1274 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1275 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1276
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001277 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001278 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001279
Scott Wakeling1f36f412016-04-21 11:13:45 +01001280 // Take slow path and throw if input can be and is null.
1281 SlowPathCodeARM64* slow_path = nullptr;
1282 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1283 if (can_slow_path) {
1284 slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1285 codegen_->AddSlowPath(slow_path);
1286 __ Cbz(arg, slow_path->GetEntryLabel());
1287 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001288
Scott Wakeling1f36f412016-04-21 11:13:45 +01001289 // Reference equality check, return 0 if same reference.
1290 __ Subs(out, str, arg);
1291 __ B(&end, eq);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001292
jessicahandojo05765752016-09-09 19:01:32 -07001293 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001294 // Load `count` fields of this and argument strings.
jessicahandojo05765752016-09-09 19:01:32 -07001295 __ Ldr(temp3, HeapOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001296 __ Ldr(temp2, HeapOperand(arg, count_offset));
jessicahandojo05765752016-09-09 19:01:32 -07001297 // Clean out compression flag from lengths.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001298 __ Lsr(temp0, temp3, 1u);
1299 __ Lsr(temp1, temp2, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001300 } else {
1301 // Load lengths of this and argument strings.
1302 __ Ldr(temp0, HeapOperand(str, count_offset));
1303 __ Ldr(temp1, HeapOperand(arg, count_offset));
1304 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001305 // out = length diff.
1306 __ Subs(out, temp0, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001307 // temp0 = min(len(str), len(arg)).
1308 __ Csel(temp0, temp1, temp0, ge);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001309 // Shorter string is empty?
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001310 __ Cbz(temp0, &end);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001311
jessicahandojo05765752016-09-09 19:01:32 -07001312 if (mirror::kUseStringCompression) {
1313 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001314 __ Eor(temp2, temp2, Operand(temp3));
1315 // Interleave with compression flag extraction which is needed for both paths
1316 // and also set flags which is needed only for the different compressions path.
1317 __ Ands(temp3.W(), temp3.W(), Operand(1));
1318 __ Tbnz(temp2, 0, &different_compression); // Does not use flags.
jessicahandojo05765752016-09-09 19:01:32 -07001319 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001320 // Store offset of string value in preparation for comparison loop.
1321 __ Mov(temp1, value_offset);
jessicahandojo05765752016-09-09 19:01:32 -07001322 if (mirror::kUseStringCompression) {
1323 // For string compression, calculate the number of bytes to compare (not chars).
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001324 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
1325 __ Lsl(temp0, temp0, temp3);
jessicahandojo05765752016-09-09 19:01:32 -07001326 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001327
1328 UseScratchRegisterScope scratch_scope(masm);
1329 Register temp4 = scratch_scope.AcquireX();
1330
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001331 // Assertions that must hold in order to compare strings 8 bytes at a time.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001332 DCHECK_ALIGNED(value_offset, 8);
1333 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1334
1335 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1336 DCHECK_EQ(char_size, 2u);
1337
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001338 // Promote temp2 to an X reg, ready for LDR.
1339 temp2 = temp2.X();
Scott Wakeling1f36f412016-04-21 11:13:45 +01001340
1341 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1342 __ Bind(&loop);
Alexandre Rames2ea91532016-08-11 17:04:14 +01001343 __ Ldr(temp4, MemOperand(str.X(), temp1.X()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001344 __ Ldr(temp2, MemOperand(arg.X(), temp1.X()));
1345 __ Cmp(temp4, temp2);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001346 __ B(ne, &find_char_diff);
1347 __ Add(temp1, temp1, char_size * 4);
jessicahandojo05765752016-09-09 19:01:32 -07001348 // With string compression, we have compared 8 bytes, otherwise 4 chars.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001349 __ Subs(temp0, temp0, (mirror::kUseStringCompression) ? 8 : 4);
1350 __ B(&loop, hi);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001351 __ B(&end);
1352
1353 // Promote temp1 to an X reg, ready for EOR.
1354 temp1 = temp1.X();
1355
jessicahandojo05765752016-09-09 19:01:32 -07001356 // Find the single character difference.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001357 __ Bind(&find_char_diff);
1358 // Get the bit position of the first character that differs.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001359 __ Eor(temp1, temp2, temp4);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001360 __ Rbit(temp1, temp1);
1361 __ Clz(temp1, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001362
jessicahandojo05765752016-09-09 19:01:32 -07001363 // If the number of chars remaining <= the index where the difference occurs (0-3), then
Scott Wakeling1f36f412016-04-21 11:13:45 +01001364 // the difference occurs outside the remaining string data, so just return length diff (out).
jessicahandojo05765752016-09-09 19:01:32 -07001365 // Unlike ARM, we're doing the comparison in one go here, without the subtraction at the
1366 // find_char_diff_2nd_cmp path, so it doesn't matter whether the comparison is signed or
1367 // unsigned when string compression is disabled.
1368 // When it's enabled, the comparison must be unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001369 __ Cmp(temp0, Operand(temp1.W(), LSR, (mirror::kUseStringCompression) ? 3 : 4));
jessicahandojo05765752016-09-09 19:01:32 -07001370 __ B(ls, &end);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001371
Scott Wakeling1f36f412016-04-21 11:13:45 +01001372 // Extract the characters and calculate the difference.
jessicahandojo05765752016-09-09 19:01:32 -07001373 if (mirror:: kUseStringCompression) {
jessicahandojo05765752016-09-09 19:01:32 -07001374 __ Bic(temp1, temp1, 0x7);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001375 __ Bic(temp1, temp1, Operand(temp3.X(), LSL, 3u));
1376 } else {
1377 __ Bic(temp1, temp1, 0xf);
jessicahandojo05765752016-09-09 19:01:32 -07001378 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001379 __ Lsr(temp2, temp2, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001380 __ Lsr(temp4, temp4, temp1);
jessicahandojo05765752016-09-09 19:01:32 -07001381 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001382 // Prioritize the case of compressed strings and calculate such result first.
1383 __ Uxtb(temp1, temp4);
1384 __ Sub(out, temp1.W(), Operand(temp2.W(), UXTB));
1385 __ Tbz(temp3, 0u, &end); // If actually compressed, we're done.
jessicahandojo05765752016-09-09 19:01:32 -07001386 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001387 __ Uxth(temp4, temp4);
1388 __ Sub(out, temp4.W(), Operand(temp2.W(), UXTH));
jessicahandojo05765752016-09-09 19:01:32 -07001389
1390 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001391 __ B(&end);
1392 __ Bind(&different_compression);
1393
1394 // Comparison for different compression style.
jessicahandojo05765752016-09-09 19:01:32 -07001395 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1396 DCHECK_EQ(c_char_size, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001397 temp1 = temp1.W();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001398 temp2 = temp2.W();
1399 temp4 = temp4.W();
jessicahandojo05765752016-09-09 19:01:32 -07001400
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001401 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
1402 // Note that flags have been set by the `str` compression flag extraction to `temp3`
1403 // before branching to the `different_compression` label.
1404 __ Csel(temp1, str, arg, eq); // Pointer to the compressed string.
1405 __ Csel(temp2, str, arg, ne); // Pointer to the uncompressed string.
jessicahandojo05765752016-09-09 19:01:32 -07001406
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001407 // We want to free up the temp3, currently holding `str` compression flag, for comparison.
1408 // So, we move it to the bottom bit of the iteration count `temp0` which we then need to treat
1409 // as unsigned. Start by freeing the bit with a LSL and continue further down by a SUB which
1410 // will allow `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
1411 __ Lsl(temp0, temp0, 1u);
1412
1413 // Adjust temp1 and temp2 from string pointers to data pointers.
1414 __ Add(temp1, temp1, Operand(value_offset));
1415 __ Add(temp2, temp2, Operand(value_offset));
1416
1417 // Complete the move of the compression flag.
1418 __ Sub(temp0, temp0, Operand(temp3));
1419
1420 vixl::aarch64::Label different_compression_loop;
1421 vixl::aarch64::Label different_compression_diff;
1422
1423 __ Bind(&different_compression_loop);
1424 __ Ldrb(temp4, MemOperand(temp1.X(), c_char_size, PostIndex));
1425 __ Ldrh(temp3, MemOperand(temp2.X(), char_size, PostIndex));
1426 __ Subs(temp4, temp4, Operand(temp3));
1427 __ B(&different_compression_diff, ne);
1428 __ Subs(temp0, temp0, 2);
1429 __ B(&different_compression_loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001430 __ B(&end);
1431
1432 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001433 __ Bind(&different_compression_diff);
1434 __ Tst(temp0, Operand(1));
1435 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1436 "Expecting 0=compressed, 1=uncompressed");
1437 __ Cneg(out, temp4, ne);
jessicahandojo05765752016-09-09 19:01:32 -07001438 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001439
1440 __ Bind(&end);
1441
1442 if (can_slow_path) {
1443 __ Bind(slow_path->GetExitLabel());
1444 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001445}
1446
Agi Csakiea34b402015-08-13 17:51:19 -07001447void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
1448 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1449 LocationSummary::kNoCall,
1450 kIntrinsified);
1451 locations->SetInAt(0, Location::RequiresRegister());
1452 locations->SetInAt(1, Location::RequiresRegister());
1453 // Temporary registers to store lengths of strings and for calculations.
1454 locations->AddTemp(Location::RequiresRegister());
1455 locations->AddTemp(Location::RequiresRegister());
1456
1457 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1458}
1459
1460void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001461 MacroAssembler* masm = GetVIXLAssembler();
Agi Csakiea34b402015-08-13 17:51:19 -07001462 LocationSummary* locations = invoke->GetLocations();
1463
1464 Register str = WRegisterFrom(locations->InAt(0));
1465 Register arg = WRegisterFrom(locations->InAt(1));
1466 Register out = XRegisterFrom(locations->Out());
1467
1468 UseScratchRegisterScope scratch_scope(masm);
1469 Register temp = scratch_scope.AcquireW();
1470 Register temp1 = WRegisterFrom(locations->GetTemp(0));
1471 Register temp2 = WRegisterFrom(locations->GetTemp(1));
1472
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001473 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001474 vixl::aarch64::Label end;
1475 vixl::aarch64::Label return_true;
1476 vixl::aarch64::Label return_false;
Agi Csakiea34b402015-08-13 17:51:19 -07001477
1478 // Get offsets of count, value, and class fields within a string object.
1479 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1480 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1481 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1482
1483 // Note that the null check must have been done earlier.
1484 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1485
Vladimir Marko53b52002016-05-24 19:30:45 +01001486 StringEqualsOptimizations optimizations(invoke);
1487 if (!optimizations.GetArgumentNotNull()) {
1488 // Check if input is null, return false if it is.
1489 __ Cbz(arg, &return_false);
1490 }
Agi Csakiea34b402015-08-13 17:51:19 -07001491
1492 // Reference equality check, return true if same reference.
1493 __ Cmp(str, arg);
1494 __ B(&return_true, eq);
1495
Vladimir Marko53b52002016-05-24 19:30:45 +01001496 if (!optimizations.GetArgumentIsString()) {
1497 // Instanceof check for the argument by comparing class fields.
1498 // All string objects must have the same type since String cannot be subclassed.
1499 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1500 // If the argument is a string object, its class field must be equal to receiver's class field.
1501 __ Ldr(temp, MemOperand(str.X(), class_offset));
1502 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1503 __ Cmp(temp, temp1);
1504 __ B(&return_false, ne);
1505 }
Agi Csakiea34b402015-08-13 17:51:19 -07001506
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001507 // Load `count` fields of this and argument strings.
Agi Csakiea34b402015-08-13 17:51:19 -07001508 __ Ldr(temp, MemOperand(str.X(), count_offset));
1509 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001510 // Check if `count` fields are equal, return false if they're not.
jessicahandojo05765752016-09-09 19:01:32 -07001511 // Also compares the compression style, if differs return false.
Agi Csakiea34b402015-08-13 17:51:19 -07001512 __ Cmp(temp, temp1);
1513 __ B(&return_false, ne);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001514 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1515 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1516 "Expecting 0=compressed, 1=uncompressed");
Agi Csakiea34b402015-08-13 17:51:19 -07001517 __ Cbz(temp, &return_true);
1518
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001519 // Assertions that must hold in order to compare strings 8 bytes at a time.
Agi Csakiea34b402015-08-13 17:51:19 -07001520 DCHECK_ALIGNED(value_offset, 8);
1521 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1522
jessicahandojo05765752016-09-09 19:01:32 -07001523 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001524 // For string compression, calculate the number of bytes to compare (not chars).
1525 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1526 __ Lsr(temp, temp, 1u); // Extract length.
1527 __ And(temp1, temp1, Operand(1)); // Extract compression flag.
1528 __ Lsl(temp, temp, temp1); // Calculate number of bytes to compare.
jessicahandojo05765752016-09-09 19:01:32 -07001529 }
1530
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001531 // Store offset of string value in preparation for comparison loop
1532 __ Mov(temp1, value_offset);
1533
Agi Csakiea34b402015-08-13 17:51:19 -07001534 temp1 = temp1.X();
1535 temp2 = temp2.X();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001536 // Loop to compare strings 8 bytes at a time starting at the front of the string.
1537 // Ok to do this because strings are zero-padded to kObjectAlignment.
Agi Csakiea34b402015-08-13 17:51:19 -07001538 __ Bind(&loop);
1539 __ Ldr(out, MemOperand(str.X(), temp1));
1540 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1541 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1542 __ Cmp(out, temp2);
1543 __ B(&return_false, ne);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001544 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1545 __ Sub(temp, temp, Operand(mirror::kUseStringCompression ? 8 : 4), SetFlags);
1546 __ B(&loop, hi);
Agi Csakiea34b402015-08-13 17:51:19 -07001547
1548 // Return true and exit the function.
1549 // If loop does not result in returning false, we return true.
1550 __ Bind(&return_true);
1551 __ Mov(out, 1);
1552 __ B(&end);
1553
1554 // Return false and exit the function.
1555 __ Bind(&return_false);
1556 __ Mov(out, 0);
1557 __ Bind(&end);
1558}
1559
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001560static void GenerateVisitStringIndexOf(HInvoke* invoke,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001561 MacroAssembler* masm,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001562 CodeGeneratorARM64* codegen,
1563 ArenaAllocator* allocator,
1564 bool start_at_zero) {
1565 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001566
1567 // Note that the null check must have been done earlier.
1568 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1569
1570 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001571 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001572 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001573 HInstruction* code_point = invoke->InputAt(1);
1574 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001575 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001576 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1577 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1578 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1579 codegen->AddSlowPath(slow_path);
1580 __ B(slow_path->GetEntryLabel());
1581 __ Bind(slow_path->GetExitLabel());
1582 return;
1583 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001584 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001585 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001586 __ Tst(char_reg, 0xFFFF0000);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001587 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1588 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001589 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001590 }
1591
1592 if (start_at_zero) {
1593 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001594 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001595 __ Mov(tmp_reg, 0);
1596 }
1597
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001598 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Roland Levillain42ad2882016-02-29 18:26:54 +00001599 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001600
1601 if (slow_path != nullptr) {
1602 __ Bind(slow_path->GetExitLabel());
1603 }
1604}
1605
1606void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1607 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001608 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001609 kIntrinsified);
1610 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1611 // best to align the inputs accordingly.
1612 InvokeRuntimeCallingConvention calling_convention;
1613 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1614 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1615 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1616
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001617 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001618 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1619}
1620
1621void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001622 GenerateVisitStringIndexOf(
1623 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001624}
1625
1626void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1627 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001628 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001629 kIntrinsified);
1630 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1631 // best to align the inputs accordingly.
1632 InvokeRuntimeCallingConvention calling_convention;
1633 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1634 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1635 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1636 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001637}
1638
1639void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001640 GenerateVisitStringIndexOf(
1641 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001642}
1643
Jeff Hao848f70a2014-01-15 13:49:50 -08001644void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1645 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001646 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001647 kIntrinsified);
1648 InvokeRuntimeCallingConvention calling_convention;
1649 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1650 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1651 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1652 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1653 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1654}
1655
1656void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001657 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001658 LocationSummary* locations = invoke->GetLocations();
1659
1660 Register byte_array = WRegisterFrom(locations->InAt(0));
1661 __ Cmp(byte_array, 0);
1662 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1663 codegen_->AddSlowPath(slow_path);
1664 __ B(eq, slow_path->GetEntryLabel());
1665
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001666 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001667 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001668 __ Bind(slow_path->GetExitLabel());
1669}
1670
1671void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1672 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001673 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001674 kIntrinsified);
1675 InvokeRuntimeCallingConvention calling_convention;
1676 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1677 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1678 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1679 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1680}
1681
1682void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001683 // No need to emit code checking whether `locations->InAt(2)` is a null
1684 // pointer, as callers of the native method
1685 //
1686 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1687 //
1688 // all include a null check on `data` before calling that method.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001689 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001690 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001691}
1692
1693void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001694 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001695 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001696 kIntrinsified);
1697 InvokeRuntimeCallingConvention calling_convention;
1698 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Jeff Hao848f70a2014-01-15 13:49:50 -08001699 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1700}
1701
1702void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001703 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001704 LocationSummary* locations = invoke->GetLocations();
1705
1706 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1707 __ Cmp(string_to_copy, 0);
1708 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1709 codegen_->AddSlowPath(slow_path);
1710 __ B(eq, slow_path->GetEntryLabel());
1711
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001712 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001713 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001714 __ Bind(slow_path->GetExitLabel());
1715}
1716
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001717static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1718 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
1719 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1720 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1721
1722 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001723 LocationSummary::kCallOnMainOnly,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001724 kIntrinsified);
1725 InvokeRuntimeCallingConvention calling_convention;
1726
1727 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1728 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1729}
1730
1731static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1732 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
1733 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1734 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1735 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1736
1737 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001738 LocationSummary::kCallOnMainOnly,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001739 kIntrinsified);
1740 InvokeRuntimeCallingConvention calling_convention;
1741
1742 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1743 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1744 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1745}
1746
1747static void GenFPToFPCall(HInvoke* invoke,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001748 CodeGeneratorARM64* codegen,
1749 QuickEntrypointEnum entry) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001750 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001751}
1752
1753void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
1754 CreateFPToFPCallLocations(arena_, invoke);
1755}
1756
1757void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001758 GenFPToFPCall(invoke, codegen_, kQuickCos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001759}
1760
1761void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
1762 CreateFPToFPCallLocations(arena_, invoke);
1763}
1764
1765void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001766 GenFPToFPCall(invoke, codegen_, kQuickSin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001767}
1768
1769void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
1770 CreateFPToFPCallLocations(arena_, invoke);
1771}
1772
1773void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001774 GenFPToFPCall(invoke, codegen_, kQuickAcos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001775}
1776
1777void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
1778 CreateFPToFPCallLocations(arena_, invoke);
1779}
1780
1781void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001782 GenFPToFPCall(invoke, codegen_, kQuickAsin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001783}
1784
1785void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
1786 CreateFPToFPCallLocations(arena_, invoke);
1787}
1788
1789void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001790 GenFPToFPCall(invoke, codegen_, kQuickAtan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001791}
1792
1793void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
1794 CreateFPToFPCallLocations(arena_, invoke);
1795}
1796
1797void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001798 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001799}
1800
1801void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
1802 CreateFPToFPCallLocations(arena_, invoke);
1803}
1804
1805void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001806 GenFPToFPCall(invoke, codegen_, kQuickCosh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001807}
1808
1809void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
1810 CreateFPToFPCallLocations(arena_, invoke);
1811}
1812
1813void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001814 GenFPToFPCall(invoke, codegen_, kQuickExp);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001815}
1816
1817void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
1818 CreateFPToFPCallLocations(arena_, invoke);
1819}
1820
1821void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001822 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001823}
1824
1825void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
1826 CreateFPToFPCallLocations(arena_, invoke);
1827}
1828
1829void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001830 GenFPToFPCall(invoke, codegen_, kQuickLog);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001831}
1832
1833void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
1834 CreateFPToFPCallLocations(arena_, invoke);
1835}
1836
1837void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001838 GenFPToFPCall(invoke, codegen_, kQuickLog10);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001839}
1840
1841void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
1842 CreateFPToFPCallLocations(arena_, invoke);
1843}
1844
1845void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001846 GenFPToFPCall(invoke, codegen_, kQuickSinh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001847}
1848
1849void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
1850 CreateFPToFPCallLocations(arena_, invoke);
1851}
1852
1853void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001854 GenFPToFPCall(invoke, codegen_, kQuickTan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001855}
1856
1857void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
1858 CreateFPToFPCallLocations(arena_, invoke);
1859}
1860
1861void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001862 GenFPToFPCall(invoke, codegen_, kQuickTanh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001863}
1864
1865void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
1866 CreateFPFPToFPCallLocations(arena_, invoke);
1867}
1868
1869void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001870 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001871}
1872
1873void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
1874 CreateFPFPToFPCallLocations(arena_, invoke);
1875}
1876
1877void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001878 GenFPToFPCall(invoke, codegen_, kQuickHypot);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001879}
1880
1881void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
1882 CreateFPFPToFPCallLocations(arena_, invoke);
1883}
1884
1885void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001886 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001887}
1888
Tim Zhang25abd6c2016-01-19 23:39:24 +08001889void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1890 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1891 LocationSummary::kNoCall,
1892 kIntrinsified);
1893 locations->SetInAt(0, Location::RequiresRegister());
1894 locations->SetInAt(1, Location::RequiresRegister());
1895 locations->SetInAt(2, Location::RequiresRegister());
1896 locations->SetInAt(3, Location::RequiresRegister());
1897 locations->SetInAt(4, Location::RequiresRegister());
1898
1899 locations->AddTemp(Location::RequiresRegister());
1900 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01001901 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08001902}
1903
1904void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001905 MacroAssembler* masm = GetVIXLAssembler();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001906 LocationSummary* locations = invoke->GetLocations();
1907
1908 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1909 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1910 DCHECK_EQ(char_size, 2u);
1911
1912 // Location of data in char array buffer.
1913 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1914
1915 // Location of char array data in string.
1916 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1917
1918 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1919 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
1920 Register srcObj = XRegisterFrom(locations->InAt(0));
1921 Register srcBegin = XRegisterFrom(locations->InAt(1));
1922 Register srcEnd = XRegisterFrom(locations->InAt(2));
1923 Register dstObj = XRegisterFrom(locations->InAt(3));
1924 Register dstBegin = XRegisterFrom(locations->InAt(4));
1925
1926 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001927 Register num_chr = XRegisterFrom(locations->GetTemp(1));
1928 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001929
1930 UseScratchRegisterScope temps(masm);
1931 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01001932 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001933
jessicahandojo05765752016-09-09 19:01:32 -07001934 vixl::aarch64::Label done;
1935 vixl::aarch64::Label compressed_string_loop;
1936 __ Sub(num_chr, srcEnd, srcBegin);
1937 // Early out for valid zero-length retrievals.
1938 __ Cbz(num_chr, &done);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001939
Scott Wakelingdf109d92016-04-22 11:35:56 +01001940 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001941 __ Add(dst_ptr, dstObj, Operand(data_offset));
1942 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
1943
jessicahandojo05765752016-09-09 19:01:32 -07001944 // src address to copy from.
1945 __ Add(src_ptr, srcObj, Operand(value_offset));
1946 vixl::aarch64::Label compressed_string_preloop;
1947 if (mirror::kUseStringCompression) {
1948 // Location of count in string.
1949 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1950 // String's length.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001951 __ Ldr(tmp2, MemOperand(srcObj, count_offset));
1952 __ Tbz(tmp2, 0, &compressed_string_preloop);
jessicahandojo05765752016-09-09 19:01:32 -07001953 }
1954 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001955
Tim Zhang25abd6c2016-01-19 23:39:24 +08001956 // Do the copy.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001957 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001958 vixl::aarch64::Label remainder;
Scott Wakelingdf109d92016-04-22 11:35:56 +01001959
Scott Wakelingdf109d92016-04-22 11:35:56 +01001960 // Save repairing the value of num_chr on the < 8 character path.
1961 __ Subs(tmp1, num_chr, 8);
1962 __ B(lt, &remainder);
1963
1964 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
1965 __ Mov(num_chr, tmp1);
1966
1967 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
1968 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08001969 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001970 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001971 __ Subs(num_chr, num_chr, 8);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001972 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001973 __ B(ge, &loop);
1974
1975 __ Adds(num_chr, num_chr, 8);
1976 __ B(eq, &done);
1977
1978 // Main loop for < 8 character case and remainder handling. Loads and stores one
1979 // 16-bit Java character at a time.
1980 __ Bind(&remainder);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001981 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001982 __ Subs(num_chr, num_chr, 1);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001983 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001984 __ B(gt, &remainder);
jessicahandojo05765752016-09-09 19:01:32 -07001985 __ B(&done);
1986
1987 if (mirror::kUseStringCompression) {
1988 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1989 DCHECK_EQ(c_char_size, 1u);
1990 __ Bind(&compressed_string_preloop);
1991 __ Add(src_ptr, src_ptr, Operand(srcBegin));
1992 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
1993 __ Bind(&compressed_string_loop);
1994 __ Ldrb(tmp1, MemOperand(src_ptr, c_char_size, PostIndex));
1995 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
1996 __ Subs(num_chr, num_chr, Operand(1));
1997 __ B(gt, &compressed_string_loop);
1998 }
Scott Wakelingdf109d92016-04-22 11:35:56 +01001999
Tim Zhang25abd6c2016-01-19 23:39:24 +08002000 __ Bind(&done);
2001}
2002
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002003// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
2004// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002005static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002006
2007static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
2008 uint32_t at,
2009 HInstruction* input) {
2010 HIntConstant* const_input = input->AsIntConstant();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002011 if (const_input != nullptr && !vixl::aarch64::Assembler::IsImmAddSub(const_input->GetValue())) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002012 locations->SetInAt(at, Location::RequiresRegister());
2013 } else {
2014 locations->SetInAt(at, Location::RegisterOrConstant(input));
2015 }
2016}
2017
2018void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
2019 // Check to see if we have known failures that will cause us to have to bail out
2020 // to the runtime, and just generate the runtime call directly.
2021 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2022 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
2023
2024 // The positions must be non-negative.
2025 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2026 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
2027 // We will have to fail anyways.
2028 return;
2029 }
2030
2031 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
2032 // native implementation.
2033 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2034 if (length != nullptr) {
2035 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002036 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002037 // Just call as normal.
2038 return;
2039 }
2040 }
2041
2042 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2043 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2044 LocationSummary::kCallOnSlowPath,
2045 kIntrinsified);
2046 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
2047 locations->SetInAt(0, Location::RequiresRegister());
2048 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2049 locations->SetInAt(2, Location::RequiresRegister());
2050 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2051 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2052
2053 locations->AddTemp(Location::RequiresRegister());
2054 locations->AddTemp(Location::RequiresRegister());
2055 locations->AddTemp(Location::RequiresRegister());
2056}
2057
Scott Wakeling97c72b72016-06-24 16:19:36 +01002058static void CheckSystemArrayCopyPosition(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002059 const Location& pos,
2060 const Register& input,
2061 const Location& length,
2062 SlowPathCodeARM64* slow_path,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002063 const Register& temp,
2064 bool length_is_input_length = false) {
2065 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
2066 if (pos.IsConstant()) {
2067 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
2068 if (pos_const == 0) {
2069 if (!length_is_input_length) {
2070 // Check that length(input) >= length.
2071 __ Ldr(temp, MemOperand(input, length_offset));
2072 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
2073 __ B(slow_path->GetEntryLabel(), lt);
2074 }
2075 } else {
2076 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002077 __ Ldr(temp, MemOperand(input, length_offset));
2078 __ Subs(temp, temp, pos_const);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002079 __ B(slow_path->GetEntryLabel(), lt);
2080
2081 // Check that (length(input) - pos) >= length.
2082 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
2083 __ B(slow_path->GetEntryLabel(), lt);
2084 }
2085 } else if (length_is_input_length) {
2086 // The only way the copy can succeed is if pos is zero.
2087 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
2088 } else {
2089 // Check that pos >= 0.
2090 Register pos_reg = WRegisterFrom(pos);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002091 __ Tbnz(pos_reg, pos_reg.GetSizeInBits() - 1, slow_path->GetEntryLabel());
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002092
2093 // Check that pos <= length(input) && (length(input) - pos) >= length.
2094 __ Ldr(temp, MemOperand(input, length_offset));
2095 __ Subs(temp, temp, pos_reg);
2096 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
2097 __ Ccmp(temp, OperandFrom(length, Primitive::kPrimInt), NFlag, ge);
2098 __ B(slow_path->GetEntryLabel(), lt);
2099 }
2100}
2101
2102// Compute base source address, base destination address, and end source address
2103// for System.arraycopy* intrinsics.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002104static void GenSystemArrayCopyAddresses(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002105 Primitive::Type type,
2106 const Register& src,
2107 const Location& src_pos,
2108 const Register& dst,
2109 const Location& dst_pos,
2110 const Location& copy_length,
2111 const Register& src_base,
2112 const Register& dst_base,
2113 const Register& src_end) {
2114 DCHECK(type == Primitive::kPrimNot || type == Primitive::kPrimChar)
Roland Levillainebea3d22016-04-12 15:42:57 +01002115 << "Unexpected element type: " << type;
2116 const int32_t element_size = Primitive::ComponentSize(type);
2117 const int32_t element_size_shift = Primitive::ComponentSizeShift(type);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002118
Roland Levillainebea3d22016-04-12 15:42:57 +01002119 uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002120 if (src_pos.IsConstant()) {
2121 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002122 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002123 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002124 __ Add(src_base, src, data_offset);
2125 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002126 }
2127
2128 if (dst_pos.IsConstant()) {
2129 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002130 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002131 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002132 __ Add(dst_base, dst, data_offset);
2133 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002134 }
2135
2136 if (copy_length.IsConstant()) {
2137 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002138 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002139 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002140 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002141 }
2142}
2143
2144void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002145 MacroAssembler* masm = GetVIXLAssembler();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002146 LocationSummary* locations = invoke->GetLocations();
2147 Register src = XRegisterFrom(locations->InAt(0));
2148 Location src_pos = locations->InAt(1);
2149 Register dst = XRegisterFrom(locations->InAt(2));
2150 Location dst_pos = locations->InAt(3);
2151 Location length = locations->InAt(4);
2152
2153 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2154 codegen_->AddSlowPath(slow_path);
2155
2156 // If source and destination are the same, take the slow path. Overlapping copy regions must be
2157 // copied in reverse and we can't know in all cases if it's needed.
2158 __ Cmp(src, dst);
2159 __ B(slow_path->GetEntryLabel(), eq);
2160
2161 // Bail out if the source is null.
2162 __ Cbz(src, slow_path->GetEntryLabel());
2163
2164 // Bail out if the destination is null.
2165 __ Cbz(dst, slow_path->GetEntryLabel());
2166
2167 if (!length.IsConstant()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002168 // Merge the following two comparisons into one:
2169 // If the length is negative, bail out (delegate to libcore's native implementation).
2170 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002171 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002172 __ B(slow_path->GetEntryLabel(), hi);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002173 } else {
2174 // We have already checked in the LocationsBuilder for the constant case.
2175 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
2176 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
2177 }
2178
2179 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
2180 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
2181 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
2182
2183 CheckSystemArrayCopyPosition(masm,
2184 src_pos,
2185 src,
2186 length,
2187 slow_path,
2188 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002189 false);
2190
2191 CheckSystemArrayCopyPosition(masm,
2192 dst_pos,
2193 dst,
2194 length,
2195 slow_path,
2196 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002197 false);
2198
2199 src_curr_addr = src_curr_addr.X();
2200 dst_curr_addr = dst_curr_addr.X();
2201 src_stop_addr = src_stop_addr.X();
2202
2203 GenSystemArrayCopyAddresses(masm,
2204 Primitive::kPrimChar,
2205 src,
2206 src_pos,
2207 dst,
2208 dst_pos,
2209 length,
2210 src_curr_addr,
2211 dst_curr_addr,
2212 src_stop_addr);
2213
2214 // Iterate over the arrays and do a raw copy of the chars.
2215 const int32_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2216 UseScratchRegisterScope temps(masm);
2217 Register tmp = temps.AcquireW();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002218 vixl::aarch64::Label loop, done;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002219 __ Bind(&loop);
2220 __ Cmp(src_curr_addr, src_stop_addr);
2221 __ B(&done, eq);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002222 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, PostIndex));
2223 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, PostIndex));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002224 __ B(&loop);
2225 __ Bind(&done);
2226
2227 __ Bind(slow_path->GetExitLabel());
2228}
2229
donghui.baic2ec9ad2016-03-10 14:02:55 +08002230// We can choose to use the native implementation there for longer copy lengths.
2231static constexpr int32_t kSystemArrayCopyThreshold = 128;
2232
2233// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2234// We want to use two temporary registers in order to reduce the register pressure in arm64.
2235// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2236void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002237 // The only read barrier implementation supporting the
2238 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2239 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01002240 return;
2241 }
2242
donghui.baic2ec9ad2016-03-10 14:02:55 +08002243 // Check to see if we have known failures that will cause us to have to bail out
2244 // to the runtime, and just generate the runtime call directly.
2245 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2246 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2247
2248 // The positions must be non-negative.
2249 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2250 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2251 // We will have to fail anyways.
2252 return;
2253 }
2254
2255 // The length must be >= 0.
2256 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2257 if (length != nullptr) {
2258 int32_t len = length->GetValue();
2259 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2260 // Just call as normal.
2261 return;
2262 }
2263 }
2264
2265 SystemArrayCopyOptimizations optimizations(invoke);
2266
2267 if (optimizations.GetDestinationIsSource()) {
2268 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2269 // We only support backward copying if source and destination are the same.
2270 return;
2271 }
2272 }
2273
2274 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2275 // We currently don't intrinsify primitive copying.
2276 return;
2277 }
2278
2279 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2280 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2281 LocationSummary::kCallOnSlowPath,
2282 kIntrinsified);
2283 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2284 locations->SetInAt(0, Location::RequiresRegister());
2285 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2286 locations->SetInAt(2, Location::RequiresRegister());
2287 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2288 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2289
2290 locations->AddTemp(Location::RequiresRegister());
2291 locations->AddTemp(Location::RequiresRegister());
Roland Levillain0b671c02016-08-19 12:02:34 +01002292 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2293 // Temporary register IP0, obtained from the VIXL scratch register
2294 // pool, cannot be used in ReadBarrierSystemArrayCopySlowPathARM64
2295 // (because that register is clobbered by ReadBarrierMarkRegX
2296 // entry points). Get an extra temporary register from the
2297 // register allocator.
2298 locations->AddTemp(Location::RequiresRegister());
2299 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002300}
2301
2302void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002303 // The only read barrier implementation supporting the
2304 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2305 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002306
Scott Wakeling97c72b72016-06-24 16:19:36 +01002307 MacroAssembler* masm = GetVIXLAssembler();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002308 LocationSummary* locations = invoke->GetLocations();
2309
2310 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2311 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2312 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2313 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002314 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002315
2316 Register src = XRegisterFrom(locations->InAt(0));
2317 Location src_pos = locations->InAt(1);
2318 Register dest = XRegisterFrom(locations->InAt(2));
2319 Location dest_pos = locations->InAt(3);
2320 Location length = locations->InAt(4);
2321 Register temp1 = WRegisterFrom(locations->GetTemp(0));
Roland Levillain0b671c02016-08-19 12:02:34 +01002322 Location temp1_loc = LocationFrom(temp1);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002323 Register temp2 = WRegisterFrom(locations->GetTemp(1));
Roland Levillain0b671c02016-08-19 12:02:34 +01002324 Location temp2_loc = LocationFrom(temp2);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002325
Roland Levillain0b671c02016-08-19 12:02:34 +01002326 SlowPathCodeARM64* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2327 codegen_->AddSlowPath(intrinsic_slow_path);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002328
Scott Wakeling97c72b72016-06-24 16:19:36 +01002329 vixl::aarch64::Label conditions_on_positions_validated;
donghui.baic2ec9ad2016-03-10 14:02:55 +08002330 SystemArrayCopyOptimizations optimizations(invoke);
2331
donghui.baic2ec9ad2016-03-10 14:02:55 +08002332 // If source and destination are the same, we go to slow path if we need to do
2333 // forward copying.
2334 if (src_pos.IsConstant()) {
2335 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2336 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002337 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2338 if (optimizations.GetDestinationIsSource()) {
2339 // Checked when building locations.
2340 DCHECK_GE(src_pos_constant, dest_pos_constant);
2341 } else if (src_pos_constant < dest_pos_constant) {
2342 __ Cmp(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002343 __ B(intrinsic_slow_path->GetEntryLabel(), eq);
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002344 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002345 // Checked when building locations.
2346 DCHECK(!optimizations.GetDestinationIsSource()
2347 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2348 } else {
2349 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002350 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002351 __ B(&conditions_on_positions_validated, ne);
2352 }
2353 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
Roland Levillain0b671c02016-08-19 12:02:34 +01002354 __ B(intrinsic_slow_path->GetEntryLabel(), gt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002355 }
2356 } else {
2357 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002358 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002359 __ B(&conditions_on_positions_validated, ne);
2360 }
2361 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2362 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
Roland Levillain0b671c02016-08-19 12:02:34 +01002363 __ B(intrinsic_slow_path->GetEntryLabel(), lt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002364 }
2365
2366 __ Bind(&conditions_on_positions_validated);
2367
2368 if (!optimizations.GetSourceIsNotNull()) {
2369 // Bail out if the source is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002370 __ Cbz(src, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002371 }
2372
2373 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2374 // Bail out if the destination is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002375 __ Cbz(dest, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002376 }
2377
2378 // We have already checked in the LocationsBuilder for the constant case.
2379 if (!length.IsConstant() &&
2380 !optimizations.GetCountIsSourceLength() &&
2381 !optimizations.GetCountIsDestinationLength()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002382 // Merge the following two comparisons into one:
2383 // If the length is negative, bail out (delegate to libcore's native implementation).
2384 // If the length >= 128 then (currently) prefer native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002385 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002386 __ B(intrinsic_slow_path->GetEntryLabel(), hs);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002387 }
2388 // Validity checks: source.
2389 CheckSystemArrayCopyPosition(masm,
2390 src_pos,
2391 src,
2392 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002393 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002394 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002395 optimizations.GetCountIsSourceLength());
2396
2397 // Validity checks: dest.
2398 CheckSystemArrayCopyPosition(masm,
2399 dest_pos,
2400 dest,
2401 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002402 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002403 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002404 optimizations.GetCountIsDestinationLength());
2405 {
2406 // We use a block to end the scratch scope before the write barrier, thus
2407 // freeing the temporary registers so they can be used in `MarkGCCard`.
2408 UseScratchRegisterScope temps(masm);
Roland Levillain0b671c02016-08-19 12:02:34 +01002409 // Note: Because it is acquired from VIXL's scratch register pool,
2410 // `temp3` might be IP0, and thus cannot be used as `ref` argument
2411 // of CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier
2412 // calls below (see ReadBarrierMarkSlowPathARM64 for more details).
donghui.baic2ec9ad2016-03-10 14:02:55 +08002413 Register temp3 = temps.AcquireW();
Roland Levillain0b671c02016-08-19 12:02:34 +01002414
donghui.baic2ec9ad2016-03-10 14:02:55 +08002415 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2416 // Check whether all elements of the source array are assignable to the component
2417 // type of the destination array. We do two checks: the classes are the same,
2418 // or the destination is Object[]. If none of these checks succeed, we go to the
2419 // slow path.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002420
Roland Levillain0b671c02016-08-19 12:02:34 +01002421 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2422 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2423 // /* HeapReference<Class> */ temp1 = src->klass_
2424 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2425 temp1_loc,
2426 src.W(),
2427 class_offset,
2428 temp2,
2429 /* needs_null_check */ false,
2430 /* use_load_acquire */ false);
2431 // Bail out if the source is not a non primitive array.
2432 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2433 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2434 temp1_loc,
2435 temp1,
2436 component_offset,
2437 temp2,
2438 /* needs_null_check */ false,
2439 /* use_load_acquire */ false);
2440 __ Cbz(temp1, intrinsic_slow_path->GetEntryLabel());
2441 // If heap poisoning is enabled, `temp1` has been unpoisoned
2442 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2443 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2444 __ Ldrh(temp1, HeapOperand(temp1, primitive_offset));
2445 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2446 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002447 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002448
2449 // /* HeapReference<Class> */ temp1 = dest->klass_
2450 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2451 temp1_loc,
2452 dest.W(),
2453 class_offset,
2454 temp2,
2455 /* needs_null_check */ false,
2456 /* use_load_acquire */ false);
2457
2458 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2459 // Bail out if the destination is not a non primitive array.
2460 //
2461 // Register `temp1` is not trashed by the read barrier emitted
2462 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2463 // method produces a call to a ReadBarrierMarkRegX entry point,
2464 // which saves all potentially live registers, including
2465 // temporaries such a `temp1`.
2466 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2467 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2468 temp2_loc,
2469 temp1,
2470 component_offset,
2471 temp3,
2472 /* needs_null_check */ false,
2473 /* use_load_acquire */ false);
2474 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2475 // If heap poisoning is enabled, `temp2` has been unpoisoned
2476 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2477 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2478 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
2479 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2480 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
2481 }
2482
2483 // For the same reason given earlier, `temp1` is not trashed by the
2484 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2485 // /* HeapReference<Class> */ temp2 = src->klass_
2486 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2487 temp2_loc,
2488 src.W(),
2489 class_offset,
2490 temp3,
2491 /* needs_null_check */ false,
2492 /* use_load_acquire */ false);
2493 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2494 __ Cmp(temp1, temp2);
2495
2496 if (optimizations.GetDestinationIsTypedObjectArray()) {
2497 vixl::aarch64::Label do_copy;
2498 __ B(&do_copy, eq);
2499 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2500 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2501 temp1_loc,
2502 temp1,
2503 component_offset,
2504 temp2,
2505 /* needs_null_check */ false,
2506 /* use_load_acquire */ false);
2507 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2508 // We do not need to emit a read barrier for the following
2509 // heap reference load, as `temp1` is only used in a
2510 // comparison with null below, and this reference is not
2511 // kept afterwards.
2512 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2513 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2514 __ Bind(&do_copy);
2515 } else {
2516 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2517 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002518 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002519 // Non read barrier code.
2520
2521 // /* HeapReference<Class> */ temp1 = dest->klass_
2522 __ Ldr(temp1, MemOperand(dest, class_offset));
2523 // /* HeapReference<Class> */ temp2 = src->klass_
2524 __ Ldr(temp2, MemOperand(src, class_offset));
2525 bool did_unpoison = false;
2526 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2527 !optimizations.GetSourceIsNonPrimitiveArray()) {
2528 // One or two of the references need to be unpoisoned. Unpoison them
2529 // both to make the identity check valid.
2530 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2531 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2532 did_unpoison = true;
2533 }
2534
2535 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2536 // Bail out if the destination is not a non primitive array.
2537 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2538 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2539 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2540 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2541 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2542 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2543 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2544 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2545 }
2546
2547 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2548 // Bail out if the source is not a non primitive array.
2549 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2550 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2551 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2552 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2553 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2554 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2555 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2556 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2557 }
2558
2559 __ Cmp(temp1, temp2);
2560
2561 if (optimizations.GetDestinationIsTypedObjectArray()) {
2562 vixl::aarch64::Label do_copy;
2563 __ B(&do_copy, eq);
2564 if (!did_unpoison) {
2565 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2566 }
2567 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2568 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2569 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2570 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2571 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2572 // No need to unpoison the result, we're comparing against null.
2573 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2574 __ Bind(&do_copy);
2575 } else {
2576 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2577 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002578 }
2579 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2580 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2581 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002582 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2583 // /* HeapReference<Class> */ temp1 = src->klass_
2584 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2585 temp1_loc,
2586 src.W(),
2587 class_offset,
2588 temp2,
2589 /* needs_null_check */ false,
2590 /* use_load_acquire */ false);
2591 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2592 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2593 temp2_loc,
2594 temp1,
2595 component_offset,
2596 temp3,
2597 /* needs_null_check */ false,
2598 /* use_load_acquire */ false);
2599 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2600 // If heap poisoning is enabled, `temp2` has been unpoisoned
2601 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2602 } else {
2603 // /* HeapReference<Class> */ temp1 = src->klass_
2604 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2605 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2606 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2607 __ Ldr(temp2, HeapOperand(temp1, component_offset));
2608 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2609 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2610 }
2611 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2612 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
donghui.baic2ec9ad2016-03-10 14:02:55 +08002613 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain0b671c02016-08-19 12:02:34 +01002614 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002615 }
2616
2617 Register src_curr_addr = temp1.X();
2618 Register dst_curr_addr = temp2.X();
Roland Levillain0b671c02016-08-19 12:02:34 +01002619 Register src_stop_addr;
2620 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2621 // Temporary register IP0, obtained from the VIXL scratch
2622 // register pool as `temp3`, cannot be used in
2623 // ReadBarrierSystemArrayCopySlowPathARM64 (because that
2624 // register is clobbered by ReadBarrierMarkRegX entry points).
2625 // So another temporary register allocated by the register
2626 // allocator instead.
2627 DCHECK_EQ(LocationFrom(temp3).reg(), IP0);
2628 src_stop_addr = XRegisterFrom(locations->GetTemp(2));
2629 } else {
2630 src_stop_addr = temp3.X();
2631 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002632
2633 GenSystemArrayCopyAddresses(masm,
2634 Primitive::kPrimNot,
2635 src,
2636 src_pos,
2637 dest,
2638 dest_pos,
2639 length,
2640 src_curr_addr,
2641 dst_curr_addr,
2642 src_stop_addr);
2643
donghui.baic2ec9ad2016-03-10 14:02:55 +08002644 const int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
Roland Levillain0b671c02016-08-19 12:02:34 +01002645
2646 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2647 // SystemArrayCopy implementation for Baker read barriers (see
2648 // also CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier):
2649 //
2650 // if (src_ptr != end_ptr) {
2651 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2652 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002653 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01002654 // if (is_gray) {
2655 // // Slow-path copy.
2656 // do {
2657 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2658 // } while (src_ptr != end_ptr)
2659 // } else {
2660 // // Fast-path copy.
2661 // do {
2662 // *dest_ptr++ = *src_ptr++;
2663 // } while (src_ptr != end_ptr)
2664 // }
2665 // }
2666
2667 vixl::aarch64::Label loop, done;
2668
2669 // Don't enter copy loop if `length == 0`.
2670 __ Cmp(src_curr_addr, src_stop_addr);
2671 __ B(&done, eq);
2672
donghui.baic2ec9ad2016-03-10 14:02:55 +08002673 Register tmp = temps.AcquireW();
Roland Levillain0b671c02016-08-19 12:02:34 +01002674 // Make sure `tmp` is not IP0, as it is clobbered by
2675 // ReadBarrierMarkRegX entry points in
2676 // ReadBarrierSystemArrayCopySlowPathARM64.
2677 DCHECK_NE(LocationFrom(tmp).reg(), IP0);
2678
2679 // /* int32_t */ monitor = src->monitor_
2680 __ Ldr(tmp, HeapOperand(src.W(), monitor_offset));
2681 // /* LockWord */ lock_word = LockWord(monitor)
2682 static_assert(sizeof(LockWord) == sizeof(int32_t),
2683 "art::LockWord and int32_t have different sizes.");
2684
2685 // Introduce a dependency on the lock_word including rb_state,
2686 // to prevent load-load reordering, and without using
2687 // a memory barrier (which would be more expensive).
2688 // `src` is unchanged by this operation, but its value now depends
2689 // on `tmp`.
2690 __ Add(src.X(), src.X(), Operand(tmp.X(), LSR, 32));
2691
2692 // Slow path used to copy array when `src` is gray.
2693 SlowPathCodeARM64* read_barrier_slow_path =
2694 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathARM64(invoke, LocationFrom(tmp));
2695 codegen_->AddSlowPath(read_barrier_slow_path);
2696
2697 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002698 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2699 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Roland Levillain0b671c02016-08-19 12:02:34 +01002700 __ Tbnz(tmp, LockWord::kReadBarrierStateShift, read_barrier_slow_path->GetEntryLabel());
2701
2702 // Fast-path copy.
2703 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2704 // poison/unpoison.
2705 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002706 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2707 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
Roland Levillain0b671c02016-08-19 12:02:34 +01002708 __ Cmp(src_curr_addr, src_stop_addr);
2709 __ B(&loop, ne);
2710
2711 __ Bind(read_barrier_slow_path->GetExitLabel());
2712 __ Bind(&done);
2713 } else {
2714 // Non read barrier code.
2715
2716 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2717 // poison/unpoison.
2718 vixl::aarch64::Label loop, done;
2719 __ Bind(&loop);
2720 __ Cmp(src_curr_addr, src_stop_addr);
2721 __ B(&done, eq);
2722 {
2723 Register tmp = temps.AcquireW();
2724 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2725 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
2726 }
2727 __ B(&loop);
2728 __ Bind(&done);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002729 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002730 }
2731 // We only need one card marking on the destination array.
2732 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2733
Roland Levillain0b671c02016-08-19 12:02:34 +01002734 __ Bind(intrinsic_slow_path->GetExitLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002735}
2736
Anton Kirilova3ffea22016-04-07 17:02:37 +01002737static void GenIsInfinite(LocationSummary* locations,
2738 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002739 MacroAssembler* masm) {
Anton Kirilova3ffea22016-04-07 17:02:37 +01002740 Operand infinity;
2741 Register out;
2742
2743 if (is64bit) {
2744 infinity = kPositiveInfinityDouble;
2745 out = XRegisterFrom(locations->Out());
2746 } else {
2747 infinity = kPositiveInfinityFloat;
2748 out = WRegisterFrom(locations->Out());
2749 }
2750
Scott Wakeling97c72b72016-06-24 16:19:36 +01002751 const Register zero = vixl::aarch64::Assembler::AppropriateZeroRegFor(out);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002752
2753 MoveFPToInt(locations, is64bit, masm);
2754 __ Eor(out, out, infinity);
2755 // We don't care about the sign bit, so shift left.
2756 __ Cmp(zero, Operand(out, LSL, 1));
2757 __ Cset(out, eq);
2758}
2759
2760void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2761 CreateFPToIntLocations(arena_, invoke);
2762}
2763
2764void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2765 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
2766}
2767
2768void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2769 CreateFPToIntLocations(arena_, invoke);
2770}
2771
2772void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2773 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
2774}
2775
Aart Bik2f9fcc92016-03-01 15:16:54 -08002776UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002777UNIMPLEMENTED_INTRINSIC(ARM64, IntegerHighestOneBit)
2778UNIMPLEMENTED_INTRINSIC(ARM64, LongHighestOneBit)
2779UNIMPLEMENTED_INTRINSIC(ARM64, IntegerLowestOneBit)
2780UNIMPLEMENTED_INTRINSIC(ARM64, LongLowestOneBit)
Andreas Gampe878d58c2015-01-15 23:24:00 -08002781
Aart Bikff7d89c2016-11-07 08:49:28 -08002782UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOf);
2783UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002784UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferAppend);
2785UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferLength);
2786UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferToString);
2787UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppend);
2788UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderLength);
2789UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002790
Aart Bik0e54c012016-03-04 12:08:31 -08002791// 1.8.
2792UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
2793UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
2794UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
2795UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
2796UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002797
Aart Bik2f9fcc92016-03-01 15:16:54 -08002798UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01002799
2800#undef __
2801
Andreas Gampe878d58c2015-01-15 23:24:00 -08002802} // namespace arm64
2803} // namespace art