blob: 91374b31086ffb9b9d021a9ff853404cd9e85ae6 [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,
866 /* offset */ 0U,
867 /* index */ offset_loc,
868 /* scale_factor */ 0U,
869 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());
883 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
884 }
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 Levillain22ccc3a2015-11-24 13:10:05 +0000893 can_call ?
894 LocationSummary::kCallOnSlowPath :
895 LocationSummary::kNoCall,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800896 kIntrinsified);
897 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
898 locations->SetInAt(1, Location::RequiresRegister());
899 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100900 locations->SetOut(Location::RequiresRegister(),
901 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800902}
903
904void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
905 CreateIntIntIntToIntLocations(arena_, invoke);
906}
907void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
908 CreateIntIntIntToIntLocations(arena_, invoke);
909}
910void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
911 CreateIntIntIntToIntLocations(arena_, invoke);
912}
913void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
914 CreateIntIntIntToIntLocations(arena_, invoke);
915}
916void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
917 CreateIntIntIntToIntLocations(arena_, invoke);
918}
919void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
920 CreateIntIntIntToIntLocations(arena_, invoke);
921}
922
923void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000924 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800925}
926void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000927 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800928}
929void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000930 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800931}
932void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000933 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800934}
935void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000936 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800937}
938void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000939 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800940}
941
942static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
943 LocationSummary* locations = new (arena) LocationSummary(invoke,
944 LocationSummary::kNoCall,
945 kIntrinsified);
946 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
947 locations->SetInAt(1, Location::RequiresRegister());
948 locations->SetInAt(2, Location::RequiresRegister());
949 locations->SetInAt(3, Location::RequiresRegister());
950}
951
952void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
953 CreateIntIntIntIntToVoid(arena_, invoke);
954}
955void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
956 CreateIntIntIntIntToVoid(arena_, invoke);
957}
958void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
959 CreateIntIntIntIntToVoid(arena_, invoke);
960}
961void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
962 CreateIntIntIntIntToVoid(arena_, invoke);
963}
964void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
965 CreateIntIntIntIntToVoid(arena_, invoke);
966}
967void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
968 CreateIntIntIntIntToVoid(arena_, invoke);
969}
970void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
971 CreateIntIntIntIntToVoid(arena_, invoke);
972}
973void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
974 CreateIntIntIntIntToVoid(arena_, invoke);
975}
976void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
977 CreateIntIntIntIntToVoid(arena_, invoke);
978}
979
980static void GenUnsafePut(LocationSummary* locations,
981 Primitive::Type type,
982 bool is_volatile,
983 bool is_ordered,
984 CodeGeneratorARM64* codegen) {
Alexandre Rames087930f2016-08-02 13:45:28 +0100985 MacroAssembler* masm = codegen->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800986
987 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
988 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
989 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100990 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800991 MemOperand mem_op(base.X(), offset);
992
Roland Levillain4d027112015-07-01 15:41:14 +0100993 {
994 // We use a block to end the scratch scope before the write barrier, thus
995 // freeing the temporary registers so they can be used in `MarkGCCard`.
996 UseScratchRegisterScope temps(masm);
997
998 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
999 DCHECK(value.IsW());
1000 Register temp = temps.AcquireW();
1001 __ Mov(temp.W(), value.W());
1002 codegen->GetAssembler()->PoisonHeapReference(temp.W());
1003 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -08001004 }
Roland Levillain4d027112015-07-01 15:41:14 +01001005
1006 if (is_volatile || is_ordered) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001007 codegen->StoreRelease(type, source, mem_op);
Roland Levillain4d027112015-07-01 15:41:14 +01001008 } else {
1009 codegen->Store(type, source, mem_op);
1010 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001011 }
1012
1013 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001014 bool value_can_be_null = true; // TODO: Worth finding out this information?
1015 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001016 }
1017}
1018
1019void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001020 GenUnsafePut(invoke->GetLocations(),
1021 Primitive::kPrimInt,
1022 /* is_volatile */ false,
1023 /* is_ordered */ false,
1024 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001025}
1026void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001027 GenUnsafePut(invoke->GetLocations(),
1028 Primitive::kPrimInt,
1029 /* is_volatile */ false,
1030 /* is_ordered */ true,
1031 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001032}
1033void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001034 GenUnsafePut(invoke->GetLocations(),
1035 Primitive::kPrimInt,
1036 /* is_volatile */ true,
1037 /* is_ordered */ false,
1038 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001039}
1040void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001041 GenUnsafePut(invoke->GetLocations(),
1042 Primitive::kPrimNot,
1043 /* is_volatile */ false,
1044 /* is_ordered */ false,
1045 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001046}
1047void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001048 GenUnsafePut(invoke->GetLocations(),
1049 Primitive::kPrimNot,
1050 /* is_volatile */ false,
1051 /* is_ordered */ true,
1052 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001053}
1054void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001055 GenUnsafePut(invoke->GetLocations(),
1056 Primitive::kPrimNot,
1057 /* is_volatile */ true,
1058 /* is_ordered */ false,
1059 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001060}
1061void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001062 GenUnsafePut(invoke->GetLocations(),
1063 Primitive::kPrimLong,
1064 /* is_volatile */ false,
1065 /* is_ordered */ false,
1066 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001067}
1068void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001069 GenUnsafePut(invoke->GetLocations(),
1070 Primitive::kPrimLong,
1071 /* is_volatile */ false,
1072 /* is_ordered */ true,
1073 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001074}
1075void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001076 GenUnsafePut(invoke->GetLocations(),
1077 Primitive::kPrimLong,
1078 /* is_volatile */ true,
1079 /* is_ordered */ false,
1080 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001081}
1082
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001083static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
1084 HInvoke* invoke,
1085 Primitive::Type type) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001086 LocationSummary* locations = new (arena) LocationSummary(invoke,
1087 LocationSummary::kNoCall,
1088 kIntrinsified);
1089 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1090 locations->SetInAt(1, Location::RequiresRegister());
1091 locations->SetInAt(2, Location::RequiresRegister());
1092 locations->SetInAt(3, Location::RequiresRegister());
1093 locations->SetInAt(4, Location::RequiresRegister());
1094
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001095 // If heap poisoning is enabled, we don't want the unpoisoning
1096 // operations to potentially clobber the output.
1097 Location::OutputOverlap overlaps = (kPoisonHeapReferences && type == Primitive::kPrimNot)
1098 ? Location::kOutputOverlap
1099 : Location::kNoOutputOverlap;
1100 locations->SetOut(Location::RequiresRegister(), overlaps);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001101}
1102
1103static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorARM64* codegen) {
Alexandre Rames087930f2016-08-02 13:45:28 +01001104 MacroAssembler* masm = codegen->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001105
1106 Register out = WRegisterFrom(locations->Out()); // Boolean result.
1107
1108 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1109 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1110 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1111 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1112
1113 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
1114 if (type == Primitive::kPrimNot) {
1115 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001116 bool value_can_be_null = true; // TODO: Worth finding out this information?
1117 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001118 }
1119
1120 UseScratchRegisterScope temps(masm);
1121 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1122 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1123
1124 Register tmp_32 = tmp_value.W();
1125
1126 __ Add(tmp_ptr, base.X(), Operand(offset));
1127
Roland Levillain4d027112015-07-01 15:41:14 +01001128 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1129 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001130 if (value.Is(expected)) {
1131 // Do not poison `value`, as it is the same register as
1132 // `expected`, which has just been poisoned.
1133 } else {
1134 codegen->GetAssembler()->PoisonHeapReference(value);
1135 }
Roland Levillain4d027112015-07-01 15:41:14 +01001136 }
1137
Andreas Gampe878d58c2015-01-15 23:24:00 -08001138 // do {
1139 // tmp_value = [tmp_ptr] - expected;
1140 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1141 // result = tmp_value != 0;
1142
Scott Wakeling97c72b72016-06-24 16:19:36 +01001143 vixl::aarch64::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001144 __ Bind(&loop_head);
1145 // TODO: When `type == Primitive::kPrimNot`, add a read barrier for
1146 // the reference stored in the object before attempting the CAS,
1147 // similar to the one in the art::Unsafe_compareAndSwapObject JNI
1148 // implementation.
1149 //
1150 // Note that this code is not (yet) used when read barriers are
1151 // enabled (see IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject).
1152 DCHECK(!(type == Primitive::kPrimNot && kEmitCompilerReadBarrier));
1153 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1154 __ Cmp(tmp_value, expected);
1155 __ B(&exit_loop, ne);
1156 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1157 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001158 __ Bind(&exit_loop);
1159 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001160
1161 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01001162 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001163 if (value.Is(expected)) {
1164 // Do not unpoison `value`, as it is the same register as
1165 // `expected`, which has just been unpoisoned.
1166 } else {
1167 codegen->GetAssembler()->UnpoisonHeapReference(value);
1168 }
Roland Levillain4d027112015-07-01 15:41:14 +01001169 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001170}
1171
1172void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001173 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001174}
1175void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001176 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001177}
1178void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00001179 // The UnsafeCASObject intrinsic is missing a read barrier, and
1180 // therefore sometimes does not work as expected (b/25883050).
1181 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01001182 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00001183 //
Roland Levillain3d312422016-06-23 13:53:42 +01001184 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
1185 // this intrinsic.
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001186 if (kEmitCompilerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001187 return;
1188 }
1189
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001190 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001191}
1192
1193void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
1194 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1195}
1196void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
1197 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
1198}
1199void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01001200 // The UnsafeCASObject intrinsic is missing a read barrier, and
1201 // therefore sometimes does not work as expected (b/25883050).
1202 // Turn it off temporarily as a quick fix, until the read barrier is
1203 // implemented (see TODO in GenCAS).
1204 //
1205 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
1206 // this intrinsic.
1207 DCHECK(!kEmitCompilerReadBarrier);
1208
Andreas Gampe878d58c2015-01-15 23:24:00 -08001209 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1210}
1211
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001212void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001213 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Scott Wakeling1f36f412016-04-21 11:13:45 +01001214 invoke->InputAt(1)->CanBeNull()
1215 ? LocationSummary::kCallOnSlowPath
1216 : LocationSummary::kNoCall,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001217 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001218 locations->SetInAt(0, Location::RequiresRegister());
1219 locations->SetInAt(1, Location::RequiresRegister());
1220 locations->AddTemp(Location::RequiresRegister());
1221 locations->AddTemp(Location::RequiresRegister());
1222 locations->AddTemp(Location::RequiresRegister());
1223 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001224}
1225
1226void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001227 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001228 LocationSummary* locations = invoke->GetLocations();
1229
Alexandre Rames2ea91532016-08-11 17:04:14 +01001230 Register str = InputRegisterAt(invoke, 0);
1231 Register arg = InputRegisterAt(invoke, 1);
1232 DCHECK(str.IsW());
1233 DCHECK(arg.IsW());
Scott Wakeling1f36f412016-04-21 11:13:45 +01001234 Register out = OutputRegister(invoke);
1235
1236 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1237 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1238 Register temp2 = WRegisterFrom(locations->GetTemp(2));
1239
Scott Wakeling97c72b72016-06-24 16:19:36 +01001240 vixl::aarch64::Label loop;
1241 vixl::aarch64::Label find_char_diff;
1242 vixl::aarch64::Label end;
Scott Wakeling1f36f412016-04-21 11:13:45 +01001243
1244 // Get offsets of count and value fields within a string object.
1245 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1246 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1247
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001248 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001249 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001250
Scott Wakeling1f36f412016-04-21 11:13:45 +01001251 // Take slow path and throw if input can be and is null.
1252 SlowPathCodeARM64* slow_path = nullptr;
1253 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1254 if (can_slow_path) {
1255 slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1256 codegen_->AddSlowPath(slow_path);
1257 __ Cbz(arg, slow_path->GetEntryLabel());
1258 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001259
Scott Wakeling1f36f412016-04-21 11:13:45 +01001260 // Reference equality check, return 0 if same reference.
1261 __ Subs(out, str, arg);
1262 __ B(&end, eq);
1263 // Load lengths of this and argument strings.
Alexandre Rames2ea91532016-08-11 17:04:14 +01001264 __ Ldr(temp0, HeapOperand(str, count_offset));
1265 __ Ldr(temp1, HeapOperand(arg, count_offset));
Scott Wakeling1f36f412016-04-21 11:13:45 +01001266 // Return zero if both strings are empty.
1267 __ Orr(out, temp0, temp1);
1268 __ Cbz(out, &end);
1269 // out = length diff.
1270 __ Subs(out, temp0, temp1);
1271 // temp2 = min(len(str), len(arg)).
1272 __ Csel(temp2, temp1, temp0, ge);
1273 // Shorter string is empty?
1274 __ Cbz(temp2, &end);
1275
1276 // Store offset of string value in preparation for comparison loop.
1277 __ Mov(temp1, value_offset);
1278
1279 UseScratchRegisterScope scratch_scope(masm);
1280 Register temp4 = scratch_scope.AcquireX();
1281
1282 // Assertions that must hold in order to compare strings 4 characters at a time.
1283 DCHECK_ALIGNED(value_offset, 8);
1284 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1285
1286 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1287 DCHECK_EQ(char_size, 2u);
1288
1289 // Promote temp0 to an X reg, ready for LDR.
1290 temp0 = temp0.X();
1291
1292 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1293 __ Bind(&loop);
Alexandre Rames2ea91532016-08-11 17:04:14 +01001294 __ Ldr(temp4, MemOperand(str.X(), temp1.X()));
1295 __ Ldr(temp0, MemOperand(arg.X(), temp1.X()));
Scott Wakeling1f36f412016-04-21 11:13:45 +01001296 __ Cmp(temp4, temp0);
1297 __ B(ne, &find_char_diff);
1298 __ Add(temp1, temp1, char_size * 4);
1299 __ Subs(temp2, temp2, 4);
1300 __ B(gt, &loop);
1301 __ B(&end);
1302
1303 // Promote temp1 to an X reg, ready for EOR.
1304 temp1 = temp1.X();
1305
1306 // Find the single 16-bit character difference.
1307 __ Bind(&find_char_diff);
1308 // Get the bit position of the first character that differs.
1309 __ Eor(temp1, temp0, temp4);
1310 __ Rbit(temp1, temp1);
1311 __ Clz(temp1, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001312 // If the number of 16-bit chars remaining <= the index where the difference occurs (0-3), then
1313 // the difference occurs outside the remaining string data, so just return length diff (out).
Alexandre Rames2ea91532016-08-11 17:04:14 +01001314 __ Cmp(temp2, Operand(temp1.W(), LSR, 4));
Scott Wakeling1f36f412016-04-21 11:13:45 +01001315 __ B(le, &end);
1316 // Extract the characters and calculate the difference.
Scott Wakelinge5ed20b2016-05-20 10:41:38 +01001317 __ Bic(temp1, temp1, 0xf);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001318 __ Lsr(temp0, temp0, temp1);
1319 __ Lsr(temp4, temp4, temp1);
1320 __ And(temp4, temp4, 0xffff);
Alexandre Rames2ea91532016-08-11 17:04:14 +01001321 __ Sub(out, temp4.W(), Operand(temp0.W(), UXTH));
Scott Wakeling1f36f412016-04-21 11:13:45 +01001322
1323 __ Bind(&end);
1324
1325 if (can_slow_path) {
1326 __ Bind(slow_path->GetExitLabel());
1327 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001328}
1329
Agi Csakiea34b402015-08-13 17:51:19 -07001330void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
1331 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1332 LocationSummary::kNoCall,
1333 kIntrinsified);
1334 locations->SetInAt(0, Location::RequiresRegister());
1335 locations->SetInAt(1, Location::RequiresRegister());
1336 // Temporary registers to store lengths of strings and for calculations.
1337 locations->AddTemp(Location::RequiresRegister());
1338 locations->AddTemp(Location::RequiresRegister());
1339
1340 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1341}
1342
1343void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001344 MacroAssembler* masm = GetVIXLAssembler();
Agi Csakiea34b402015-08-13 17:51:19 -07001345 LocationSummary* locations = invoke->GetLocations();
1346
1347 Register str = WRegisterFrom(locations->InAt(0));
1348 Register arg = WRegisterFrom(locations->InAt(1));
1349 Register out = XRegisterFrom(locations->Out());
1350
1351 UseScratchRegisterScope scratch_scope(masm);
1352 Register temp = scratch_scope.AcquireW();
1353 Register temp1 = WRegisterFrom(locations->GetTemp(0));
1354 Register temp2 = WRegisterFrom(locations->GetTemp(1));
1355
Scott Wakeling97c72b72016-06-24 16:19:36 +01001356 vixl::aarch64::Label loop;
1357 vixl::aarch64::Label end;
1358 vixl::aarch64::Label return_true;
1359 vixl::aarch64::Label return_false;
Agi Csakiea34b402015-08-13 17:51:19 -07001360
1361 // Get offsets of count, value, and class fields within a string object.
1362 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1363 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1364 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1365
1366 // Note that the null check must have been done earlier.
1367 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1368
Vladimir Marko53b52002016-05-24 19:30:45 +01001369 StringEqualsOptimizations optimizations(invoke);
1370 if (!optimizations.GetArgumentNotNull()) {
1371 // Check if input is null, return false if it is.
1372 __ Cbz(arg, &return_false);
1373 }
Agi Csakiea34b402015-08-13 17:51:19 -07001374
1375 // Reference equality check, return true if same reference.
1376 __ Cmp(str, arg);
1377 __ B(&return_true, eq);
1378
Vladimir Marko53b52002016-05-24 19:30:45 +01001379 if (!optimizations.GetArgumentIsString()) {
1380 // Instanceof check for the argument by comparing class fields.
1381 // All string objects must have the same type since String cannot be subclassed.
1382 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1383 // If the argument is a string object, its class field must be equal to receiver's class field.
1384 __ Ldr(temp, MemOperand(str.X(), class_offset));
1385 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1386 __ Cmp(temp, temp1);
1387 __ B(&return_false, ne);
1388 }
Agi Csakiea34b402015-08-13 17:51:19 -07001389
1390 // Load lengths of this and argument strings.
1391 __ Ldr(temp, MemOperand(str.X(), count_offset));
1392 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1393 // Check if lengths are equal, return false if they're not.
1394 __ Cmp(temp, temp1);
1395 __ B(&return_false, ne);
1396 // Store offset of string value in preparation for comparison loop
1397 __ Mov(temp1, value_offset);
1398 // Return true if both strings are empty.
1399 __ Cbz(temp, &return_true);
1400
1401 // Assertions that must hold in order to compare strings 4 characters at a time.
1402 DCHECK_ALIGNED(value_offset, 8);
1403 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1404
1405 temp1 = temp1.X();
1406 temp2 = temp2.X();
1407
1408 // Loop to compare strings 4 characters at a time starting at the beginning of the string.
1409 // Ok to do this because strings are zero-padded to be 8-byte aligned.
1410 __ Bind(&loop);
1411 __ Ldr(out, MemOperand(str.X(), temp1));
1412 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1413 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1414 __ Cmp(out, temp2);
1415 __ B(&return_false, ne);
1416 __ Sub(temp, temp, Operand(4), SetFlags);
1417 __ B(&loop, gt);
1418
1419 // Return true and exit the function.
1420 // If loop does not result in returning false, we return true.
1421 __ Bind(&return_true);
1422 __ Mov(out, 1);
1423 __ B(&end);
1424
1425 // Return false and exit the function.
1426 __ Bind(&return_false);
1427 __ Mov(out, 0);
1428 __ Bind(&end);
1429}
1430
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001431static void GenerateVisitStringIndexOf(HInvoke* invoke,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001432 MacroAssembler* masm,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001433 CodeGeneratorARM64* codegen,
1434 ArenaAllocator* allocator,
1435 bool start_at_zero) {
1436 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001437
1438 // Note that the null check must have been done earlier.
1439 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1440
1441 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001442 // 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 -07001443 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001444 HInstruction* code_point = invoke->InputAt(1);
1445 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001446 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001447 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1448 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1449 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1450 codegen->AddSlowPath(slow_path);
1451 __ B(slow_path->GetEntryLabel());
1452 __ Bind(slow_path->GetExitLabel());
1453 return;
1454 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001455 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001456 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001457 __ Tst(char_reg, 0xFFFF0000);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001458 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1459 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001460 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001461 }
1462
1463 if (start_at_zero) {
1464 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001465 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001466 __ Mov(tmp_reg, 0);
1467 }
1468
Andreas Gampe542451c2016-07-26 09:02:02 -07001469 __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, pIndexOf).Int32Value()));
Roland Levillain42ad2882016-02-29 18:26:54 +00001470 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001471 __ Blr(lr);
1472
1473 if (slow_path != nullptr) {
1474 __ Bind(slow_path->GetExitLabel());
1475 }
1476}
1477
1478void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1479 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001480 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001481 kIntrinsified);
1482 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1483 // best to align the inputs accordingly.
1484 InvokeRuntimeCallingConvention calling_convention;
1485 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1486 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1487 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1488
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001489 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001490 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1491}
1492
1493void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001494 GenerateVisitStringIndexOf(
1495 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001496}
1497
1498void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1499 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001500 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001501 kIntrinsified);
1502 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1503 // best to align the inputs accordingly.
1504 InvokeRuntimeCallingConvention calling_convention;
1505 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1506 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1507 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1508 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001509}
1510
1511void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001512 GenerateVisitStringIndexOf(
1513 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001514}
1515
Jeff Hao848f70a2014-01-15 13:49:50 -08001516void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1517 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001518 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001519 kIntrinsified);
1520 InvokeRuntimeCallingConvention calling_convention;
1521 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1522 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1523 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1524 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1525 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1526}
1527
1528void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001529 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001530 LocationSummary* locations = invoke->GetLocations();
1531
1532 Register byte_array = WRegisterFrom(locations->InAt(0));
1533 __ Cmp(byte_array, 0);
1534 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1535 codegen_->AddSlowPath(slow_path);
1536 __ B(eq, slow_path->GetEntryLabel());
1537
1538 __ Ldr(lr,
Andreas Gampe542451c2016-07-26 09:02:02 -07001539 MemOperand(tr,
1540 QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, pAllocStringFromBytes).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001541 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001542 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001543 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001544 __ Bind(slow_path->GetExitLabel());
1545}
1546
1547void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1548 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001549 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001550 kIntrinsified);
1551 InvokeRuntimeCallingConvention calling_convention;
1552 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1553 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1554 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1555 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1556}
1557
1558void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001559 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001560
Roland Levillaincc3839c2016-02-29 16:23:48 +00001561 // No need to emit code checking whether `locations->InAt(2)` is a null
1562 // pointer, as callers of the native method
1563 //
1564 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1565 //
1566 // all include a null check on `data` before calling that method.
Jeff Hao848f70a2014-01-15 13:49:50 -08001567 __ Ldr(lr,
Andreas Gampe542451c2016-07-26 09:02:02 -07001568 MemOperand(tr,
1569 QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, pAllocStringFromChars).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001570 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001571 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001572 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001573}
1574
1575void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001576 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001577 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001578 kIntrinsified);
1579 InvokeRuntimeCallingConvention calling_convention;
1580 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Jeff Hao848f70a2014-01-15 13:49:50 -08001581 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1582}
1583
1584void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001585 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001586 LocationSummary* locations = invoke->GetLocations();
1587
1588 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1589 __ Cmp(string_to_copy, 0);
1590 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1591 codegen_->AddSlowPath(slow_path);
1592 __ B(eq, slow_path->GetEntryLabel());
1593
1594 __ Ldr(lr,
Andreas Gampe542451c2016-07-26 09:02:02 -07001595 MemOperand(tr,
1596 QUICK_ENTRYPOINT_OFFSET(kArm64PointerSize, pAllocStringFromString).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001597 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001598 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001599 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001600 __ Bind(slow_path->GetExitLabel());
1601}
1602
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001603static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1604 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
1605 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1606 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1607
1608 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001609 LocationSummary::kCallOnMainOnly,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001610 kIntrinsified);
1611 InvokeRuntimeCallingConvention calling_convention;
1612
1613 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1614 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1615}
1616
1617static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1618 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
1619 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1620 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1621 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1622
1623 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001624 LocationSummary::kCallOnMainOnly,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001625 kIntrinsified);
1626 InvokeRuntimeCallingConvention calling_convention;
1627
1628 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1629 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1630 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1631}
1632
1633static void GenFPToFPCall(HInvoke* invoke,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001634 MacroAssembler* masm,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001635 CodeGeneratorARM64* codegen,
1636 QuickEntrypointEnum entry) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001637 __ Ldr(lr, MemOperand(tr,
1638 GetThreadOffset<kArm64PointerSize>(entry).Int32Value()));
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001639 __ Blr(lr);
1640 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
1641}
1642
1643void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
1644 CreateFPToFPCallLocations(arena_, invoke);
1645}
1646
1647void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
1648 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCos);
1649}
1650
1651void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
1652 CreateFPToFPCallLocations(arena_, invoke);
1653}
1654
1655void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
1656 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSin);
1657}
1658
1659void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
1660 CreateFPToFPCallLocations(arena_, invoke);
1661}
1662
1663void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
1664 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAcos);
1665}
1666
1667void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
1668 CreateFPToFPCallLocations(arena_, invoke);
1669}
1670
1671void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
1672 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAsin);
1673}
1674
1675void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
1676 CreateFPToFPCallLocations(arena_, invoke);
1677}
1678
1679void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
1680 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan);
1681}
1682
1683void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
1684 CreateFPToFPCallLocations(arena_, invoke);
1685}
1686
1687void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
1688 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCbrt);
1689}
1690
1691void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
1692 CreateFPToFPCallLocations(arena_, invoke);
1693}
1694
1695void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
1696 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCosh);
1697}
1698
1699void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
1700 CreateFPToFPCallLocations(arena_, invoke);
1701}
1702
1703void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
1704 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExp);
1705}
1706
1707void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
1708 CreateFPToFPCallLocations(arena_, invoke);
1709}
1710
1711void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
1712 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExpm1);
1713}
1714
1715void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
1716 CreateFPToFPCallLocations(arena_, invoke);
1717}
1718
1719void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
1720 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog);
1721}
1722
1723void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
1724 CreateFPToFPCallLocations(arena_, invoke);
1725}
1726
1727void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
1728 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog10);
1729}
1730
1731void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
1732 CreateFPToFPCallLocations(arena_, invoke);
1733}
1734
1735void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
1736 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSinh);
1737}
1738
1739void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
1740 CreateFPToFPCallLocations(arena_, invoke);
1741}
1742
1743void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
1744 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTan);
1745}
1746
1747void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
1748 CreateFPToFPCallLocations(arena_, invoke);
1749}
1750
1751void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
1752 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTanh);
1753}
1754
1755void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
1756 CreateFPFPToFPCallLocations(arena_, invoke);
1757}
1758
1759void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
1760 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan2);
1761}
1762
1763void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
1764 CreateFPFPToFPCallLocations(arena_, invoke);
1765}
1766
1767void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
1768 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickHypot);
1769}
1770
1771void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
1772 CreateFPFPToFPCallLocations(arena_, invoke);
1773}
1774
1775void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
1776 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickNextAfter);
1777}
1778
Tim Zhang25abd6c2016-01-19 23:39:24 +08001779void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1780 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1781 LocationSummary::kNoCall,
1782 kIntrinsified);
1783 locations->SetInAt(0, Location::RequiresRegister());
1784 locations->SetInAt(1, Location::RequiresRegister());
1785 locations->SetInAt(2, Location::RequiresRegister());
1786 locations->SetInAt(3, Location::RequiresRegister());
1787 locations->SetInAt(4, Location::RequiresRegister());
1788
1789 locations->AddTemp(Location::RequiresRegister());
1790 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01001791 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08001792}
1793
1794void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001795 MacroAssembler* masm = GetVIXLAssembler();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001796 LocationSummary* locations = invoke->GetLocations();
1797
1798 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1799 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1800 DCHECK_EQ(char_size, 2u);
1801
1802 // Location of data in char array buffer.
1803 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1804
1805 // Location of char array data in string.
1806 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1807
1808 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1809 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
1810 Register srcObj = XRegisterFrom(locations->InAt(0));
1811 Register srcBegin = XRegisterFrom(locations->InAt(1));
1812 Register srcEnd = XRegisterFrom(locations->InAt(2));
1813 Register dstObj = XRegisterFrom(locations->InAt(3));
1814 Register dstBegin = XRegisterFrom(locations->InAt(4));
1815
1816 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001817 Register num_chr = XRegisterFrom(locations->GetTemp(1));
1818 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001819
1820 UseScratchRegisterScope temps(masm);
1821 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01001822 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001823
Scott Wakelingdf109d92016-04-22 11:35:56 +01001824 // src address to copy from.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001825 __ Add(src_ptr, srcObj, Operand(value_offset));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001826 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
1827
Scott Wakelingdf109d92016-04-22 11:35:56 +01001828 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001829 __ Add(dst_ptr, dstObj, Operand(data_offset));
1830 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
1831
Scott Wakelingdf109d92016-04-22 11:35:56 +01001832 __ Sub(num_chr, srcEnd, srcBegin);
1833
Tim Zhang25abd6c2016-01-19 23:39:24 +08001834 // Do the copy.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001835 vixl::aarch64::Label loop;
1836 vixl::aarch64::Label done;
1837 vixl::aarch64::Label remainder;
Scott Wakelingdf109d92016-04-22 11:35:56 +01001838
1839 // Early out for valid zero-length retrievals.
1840 __ Cbz(num_chr, &done);
1841
1842 // Save repairing the value of num_chr on the < 8 character path.
1843 __ Subs(tmp1, num_chr, 8);
1844 __ B(lt, &remainder);
1845
1846 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
1847 __ Mov(num_chr, tmp1);
1848
1849 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
1850 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08001851 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001852 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001853 __ Subs(num_chr, num_chr, 8);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001854 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001855 __ B(ge, &loop);
1856
1857 __ Adds(num_chr, num_chr, 8);
1858 __ B(eq, &done);
1859
1860 // Main loop for < 8 character case and remainder handling. Loads and stores one
1861 // 16-bit Java character at a time.
1862 __ Bind(&remainder);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001863 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001864 __ Subs(num_chr, num_chr, 1);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001865 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001866 __ B(gt, &remainder);
1867
Tim Zhang25abd6c2016-01-19 23:39:24 +08001868 __ Bind(&done);
1869}
1870
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001871// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
1872// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001873static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001874
1875static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
1876 uint32_t at,
1877 HInstruction* input) {
1878 HIntConstant* const_input = input->AsIntConstant();
Scott Wakeling97c72b72016-06-24 16:19:36 +01001879 if (const_input != nullptr && !vixl::aarch64::Assembler::IsImmAddSub(const_input->GetValue())) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001880 locations->SetInAt(at, Location::RequiresRegister());
1881 } else {
1882 locations->SetInAt(at, Location::RegisterOrConstant(input));
1883 }
1884}
1885
1886void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1887 // Check to see if we have known failures that will cause us to have to bail out
1888 // to the runtime, and just generate the runtime call directly.
1889 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1890 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
1891
1892 // The positions must be non-negative.
1893 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1894 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
1895 // We will have to fail anyways.
1896 return;
1897 }
1898
1899 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
1900 // native implementation.
1901 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1902 if (length != nullptr) {
1903 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08001904 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001905 // Just call as normal.
1906 return;
1907 }
1908 }
1909
1910 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1911 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1912 LocationSummary::kCallOnSlowPath,
1913 kIntrinsified);
1914 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
1915 locations->SetInAt(0, Location::RequiresRegister());
1916 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
1917 locations->SetInAt(2, Location::RequiresRegister());
1918 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
1919 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
1920
1921 locations->AddTemp(Location::RequiresRegister());
1922 locations->AddTemp(Location::RequiresRegister());
1923 locations->AddTemp(Location::RequiresRegister());
1924}
1925
Scott Wakeling97c72b72016-06-24 16:19:36 +01001926static void CheckSystemArrayCopyPosition(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001927 const Location& pos,
1928 const Register& input,
1929 const Location& length,
1930 SlowPathCodeARM64* slow_path,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001931 const Register& temp,
1932 bool length_is_input_length = false) {
1933 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
1934 if (pos.IsConstant()) {
1935 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1936 if (pos_const == 0) {
1937 if (!length_is_input_length) {
1938 // Check that length(input) >= length.
1939 __ Ldr(temp, MemOperand(input, length_offset));
1940 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1941 __ B(slow_path->GetEntryLabel(), lt);
1942 }
1943 } else {
1944 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001945 __ Ldr(temp, MemOperand(input, length_offset));
1946 __ Subs(temp, temp, pos_const);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001947 __ B(slow_path->GetEntryLabel(), lt);
1948
1949 // Check that (length(input) - pos) >= length.
1950 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1951 __ B(slow_path->GetEntryLabel(), lt);
1952 }
1953 } else if (length_is_input_length) {
1954 // The only way the copy can succeed is if pos is zero.
1955 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
1956 } else {
1957 // Check that pos >= 0.
1958 Register pos_reg = WRegisterFrom(pos);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001959 __ Tbnz(pos_reg, pos_reg.GetSizeInBits() - 1, slow_path->GetEntryLabel());
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001960
1961 // Check that pos <= length(input) && (length(input) - pos) >= length.
1962 __ Ldr(temp, MemOperand(input, length_offset));
1963 __ Subs(temp, temp, pos_reg);
1964 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
1965 __ Ccmp(temp, OperandFrom(length, Primitive::kPrimInt), NFlag, ge);
1966 __ B(slow_path->GetEntryLabel(), lt);
1967 }
1968}
1969
1970// Compute base source address, base destination address, and end source address
1971// for System.arraycopy* intrinsics.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001972static void GenSystemArrayCopyAddresses(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001973 Primitive::Type type,
1974 const Register& src,
1975 const Location& src_pos,
1976 const Register& dst,
1977 const Location& dst_pos,
1978 const Location& copy_length,
1979 const Register& src_base,
1980 const Register& dst_base,
1981 const Register& src_end) {
1982 DCHECK(type == Primitive::kPrimNot || type == Primitive::kPrimChar)
Roland Levillainebea3d22016-04-12 15:42:57 +01001983 << "Unexpected element type: " << type;
1984 const int32_t element_size = Primitive::ComponentSize(type);
1985 const int32_t element_size_shift = Primitive::ComponentSizeShift(type);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001986
Roland Levillainebea3d22016-04-12 15:42:57 +01001987 uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001988 if (src_pos.IsConstant()) {
1989 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001990 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001991 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001992 __ Add(src_base, src, data_offset);
1993 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001994 }
1995
1996 if (dst_pos.IsConstant()) {
1997 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001998 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001999 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002000 __ Add(dst_base, dst, data_offset);
2001 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002002 }
2003
2004 if (copy_length.IsConstant()) {
2005 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002006 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002007 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002008 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002009 }
2010}
2011
2012void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002013 MacroAssembler* masm = GetVIXLAssembler();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002014 LocationSummary* locations = invoke->GetLocations();
2015 Register src = XRegisterFrom(locations->InAt(0));
2016 Location src_pos = locations->InAt(1);
2017 Register dst = XRegisterFrom(locations->InAt(2));
2018 Location dst_pos = locations->InAt(3);
2019 Location length = locations->InAt(4);
2020
2021 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2022 codegen_->AddSlowPath(slow_path);
2023
2024 // If source and destination are the same, take the slow path. Overlapping copy regions must be
2025 // copied in reverse and we can't know in all cases if it's needed.
2026 __ Cmp(src, dst);
2027 __ B(slow_path->GetEntryLabel(), eq);
2028
2029 // Bail out if the source is null.
2030 __ Cbz(src, slow_path->GetEntryLabel());
2031
2032 // Bail out if the destination is null.
2033 __ Cbz(dst, slow_path->GetEntryLabel());
2034
2035 if (!length.IsConstant()) {
2036 // If the length is negative, bail out.
2037 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, slow_path->GetEntryLabel());
2038 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002039 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002040 __ B(slow_path->GetEntryLabel(), gt);
2041 } else {
2042 // We have already checked in the LocationsBuilder for the constant case.
2043 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
2044 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
2045 }
2046
2047 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
2048 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
2049 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
2050
2051 CheckSystemArrayCopyPosition(masm,
2052 src_pos,
2053 src,
2054 length,
2055 slow_path,
2056 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002057 false);
2058
2059 CheckSystemArrayCopyPosition(masm,
2060 dst_pos,
2061 dst,
2062 length,
2063 slow_path,
2064 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002065 false);
2066
2067 src_curr_addr = src_curr_addr.X();
2068 dst_curr_addr = dst_curr_addr.X();
2069 src_stop_addr = src_stop_addr.X();
2070
2071 GenSystemArrayCopyAddresses(masm,
2072 Primitive::kPrimChar,
2073 src,
2074 src_pos,
2075 dst,
2076 dst_pos,
2077 length,
2078 src_curr_addr,
2079 dst_curr_addr,
2080 src_stop_addr);
2081
2082 // Iterate over the arrays and do a raw copy of the chars.
2083 const int32_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2084 UseScratchRegisterScope temps(masm);
2085 Register tmp = temps.AcquireW();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002086 vixl::aarch64::Label loop, done;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002087 __ Bind(&loop);
2088 __ Cmp(src_curr_addr, src_stop_addr);
2089 __ B(&done, eq);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002090 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, PostIndex));
2091 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, PostIndex));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002092 __ B(&loop);
2093 __ Bind(&done);
2094
2095 __ Bind(slow_path->GetExitLabel());
2096}
2097
donghui.baic2ec9ad2016-03-10 14:02:55 +08002098// We can choose to use the native implementation there for longer copy lengths.
2099static constexpr int32_t kSystemArrayCopyThreshold = 128;
2100
2101// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2102// We want to use two temporary registers in order to reduce the register pressure in arm64.
2103// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2104void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002105 // The only read barrier implementation supporting the
2106 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2107 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01002108 return;
2109 }
2110
donghui.baic2ec9ad2016-03-10 14:02:55 +08002111 // Check to see if we have known failures that will cause us to have to bail out
2112 // to the runtime, and just generate the runtime call directly.
2113 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2114 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2115
2116 // The positions must be non-negative.
2117 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2118 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2119 // We will have to fail anyways.
2120 return;
2121 }
2122
2123 // The length must be >= 0.
2124 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2125 if (length != nullptr) {
2126 int32_t len = length->GetValue();
2127 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2128 // Just call as normal.
2129 return;
2130 }
2131 }
2132
2133 SystemArrayCopyOptimizations optimizations(invoke);
2134
2135 if (optimizations.GetDestinationIsSource()) {
2136 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2137 // We only support backward copying if source and destination are the same.
2138 return;
2139 }
2140 }
2141
2142 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2143 // We currently don't intrinsify primitive copying.
2144 return;
2145 }
2146
2147 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2148 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2149 LocationSummary::kCallOnSlowPath,
2150 kIntrinsified);
2151 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2152 locations->SetInAt(0, Location::RequiresRegister());
2153 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2154 locations->SetInAt(2, Location::RequiresRegister());
2155 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2156 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2157
2158 locations->AddTemp(Location::RequiresRegister());
2159 locations->AddTemp(Location::RequiresRegister());
Roland Levillain0b671c02016-08-19 12:02:34 +01002160 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2161 // Temporary register IP0, obtained from the VIXL scratch register
2162 // pool, cannot be used in ReadBarrierSystemArrayCopySlowPathARM64
2163 // (because that register is clobbered by ReadBarrierMarkRegX
2164 // entry points). Get an extra temporary register from the
2165 // register allocator.
2166 locations->AddTemp(Location::RequiresRegister());
2167 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002168}
2169
2170void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002171 // The only read barrier implementation supporting the
2172 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2173 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002174
Scott Wakeling97c72b72016-06-24 16:19:36 +01002175 MacroAssembler* masm = GetVIXLAssembler();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002176 LocationSummary* locations = invoke->GetLocations();
2177
2178 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2179 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2180 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2181 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002182 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002183
2184 Register src = XRegisterFrom(locations->InAt(0));
2185 Location src_pos = locations->InAt(1);
2186 Register dest = XRegisterFrom(locations->InAt(2));
2187 Location dest_pos = locations->InAt(3);
2188 Location length = locations->InAt(4);
2189 Register temp1 = WRegisterFrom(locations->GetTemp(0));
Roland Levillain0b671c02016-08-19 12:02:34 +01002190 Location temp1_loc = LocationFrom(temp1);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002191 Register temp2 = WRegisterFrom(locations->GetTemp(1));
Roland Levillain0b671c02016-08-19 12:02:34 +01002192 Location temp2_loc = LocationFrom(temp2);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002193
Roland Levillain0b671c02016-08-19 12:02:34 +01002194 SlowPathCodeARM64* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2195 codegen_->AddSlowPath(intrinsic_slow_path);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002196
Scott Wakeling97c72b72016-06-24 16:19:36 +01002197 vixl::aarch64::Label conditions_on_positions_validated;
donghui.baic2ec9ad2016-03-10 14:02:55 +08002198 SystemArrayCopyOptimizations optimizations(invoke);
2199
donghui.baic2ec9ad2016-03-10 14:02:55 +08002200 // If source and destination are the same, we go to slow path if we need to do
2201 // forward copying.
2202 if (src_pos.IsConstant()) {
2203 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2204 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002205 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2206 if (optimizations.GetDestinationIsSource()) {
2207 // Checked when building locations.
2208 DCHECK_GE(src_pos_constant, dest_pos_constant);
2209 } else if (src_pos_constant < dest_pos_constant) {
2210 __ Cmp(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002211 __ B(intrinsic_slow_path->GetEntryLabel(), eq);
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002212 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002213 // Checked when building locations.
2214 DCHECK(!optimizations.GetDestinationIsSource()
2215 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2216 } else {
2217 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002218 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002219 __ B(&conditions_on_positions_validated, ne);
2220 }
2221 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
Roland Levillain0b671c02016-08-19 12:02:34 +01002222 __ B(intrinsic_slow_path->GetEntryLabel(), gt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002223 }
2224 } else {
2225 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002226 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002227 __ B(&conditions_on_positions_validated, ne);
2228 }
2229 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2230 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
Roland Levillain0b671c02016-08-19 12:02:34 +01002231 __ B(intrinsic_slow_path->GetEntryLabel(), lt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002232 }
2233
2234 __ Bind(&conditions_on_positions_validated);
2235
2236 if (!optimizations.GetSourceIsNotNull()) {
2237 // Bail out if the source is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002238 __ Cbz(src, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002239 }
2240
2241 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2242 // Bail out if the destination is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002243 __ Cbz(dest, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002244 }
2245
2246 // We have already checked in the LocationsBuilder for the constant case.
2247 if (!length.IsConstant() &&
2248 !optimizations.GetCountIsSourceLength() &&
2249 !optimizations.GetCountIsDestinationLength()) {
2250 // If the length is negative, bail out.
Roland Levillain0b671c02016-08-19 12:02:34 +01002251 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002252 // If the length >= 128 then (currently) prefer native implementation.
2253 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
Roland Levillain0b671c02016-08-19 12:02:34 +01002254 __ B(intrinsic_slow_path->GetEntryLabel(), ge);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002255 }
2256 // Validity checks: source.
2257 CheckSystemArrayCopyPosition(masm,
2258 src_pos,
2259 src,
2260 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002261 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002262 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002263 optimizations.GetCountIsSourceLength());
2264
2265 // Validity checks: dest.
2266 CheckSystemArrayCopyPosition(masm,
2267 dest_pos,
2268 dest,
2269 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002270 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002271 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002272 optimizations.GetCountIsDestinationLength());
2273 {
2274 // We use a block to end the scratch scope before the write barrier, thus
2275 // freeing the temporary registers so they can be used in `MarkGCCard`.
2276 UseScratchRegisterScope temps(masm);
Roland Levillain0b671c02016-08-19 12:02:34 +01002277 // Note: Because it is acquired from VIXL's scratch register pool,
2278 // `temp3` might be IP0, and thus cannot be used as `ref` argument
2279 // of CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier
2280 // calls below (see ReadBarrierMarkSlowPathARM64 for more details).
donghui.baic2ec9ad2016-03-10 14:02:55 +08002281 Register temp3 = temps.AcquireW();
Roland Levillain0b671c02016-08-19 12:02:34 +01002282
donghui.baic2ec9ad2016-03-10 14:02:55 +08002283 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2284 // Check whether all elements of the source array are assignable to the component
2285 // type of the destination array. We do two checks: the classes are the same,
2286 // or the destination is Object[]. If none of these checks succeed, we go to the
2287 // slow path.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002288
Roland Levillain0b671c02016-08-19 12:02:34 +01002289 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2290 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2291 // /* HeapReference<Class> */ temp1 = src->klass_
2292 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2293 temp1_loc,
2294 src.W(),
2295 class_offset,
2296 temp2,
2297 /* needs_null_check */ false,
2298 /* use_load_acquire */ false);
2299 // Bail out if the source is not a non primitive array.
2300 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2301 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2302 temp1_loc,
2303 temp1,
2304 component_offset,
2305 temp2,
2306 /* needs_null_check */ false,
2307 /* use_load_acquire */ false);
2308 __ Cbz(temp1, intrinsic_slow_path->GetEntryLabel());
2309 // If heap poisoning is enabled, `temp1` has been unpoisoned
2310 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2311 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2312 __ Ldrh(temp1, HeapOperand(temp1, primitive_offset));
2313 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2314 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002315 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002316
2317 // /* HeapReference<Class> */ temp1 = dest->klass_
2318 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2319 temp1_loc,
2320 dest.W(),
2321 class_offset,
2322 temp2,
2323 /* needs_null_check */ false,
2324 /* use_load_acquire */ false);
2325
2326 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2327 // Bail out if the destination is not a non primitive array.
2328 //
2329 // Register `temp1` is not trashed by the read barrier emitted
2330 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2331 // method produces a call to a ReadBarrierMarkRegX entry point,
2332 // which saves all potentially live registers, including
2333 // temporaries such a `temp1`.
2334 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2335 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2336 temp2_loc,
2337 temp1,
2338 component_offset,
2339 temp3,
2340 /* needs_null_check */ false,
2341 /* use_load_acquire */ false);
2342 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2343 // If heap poisoning is enabled, `temp2` has been unpoisoned
2344 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2345 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2346 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
2347 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2348 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
2349 }
2350
2351 // For the same reason given earlier, `temp1` is not trashed by the
2352 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2353 // /* HeapReference<Class> */ temp2 = src->klass_
2354 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2355 temp2_loc,
2356 src.W(),
2357 class_offset,
2358 temp3,
2359 /* needs_null_check */ false,
2360 /* use_load_acquire */ false);
2361 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2362 __ Cmp(temp1, temp2);
2363
2364 if (optimizations.GetDestinationIsTypedObjectArray()) {
2365 vixl::aarch64::Label do_copy;
2366 __ B(&do_copy, eq);
2367 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2368 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2369 temp1_loc,
2370 temp1,
2371 component_offset,
2372 temp2,
2373 /* needs_null_check */ false,
2374 /* use_load_acquire */ false);
2375 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2376 // We do not need to emit a read barrier for the following
2377 // heap reference load, as `temp1` is only used in a
2378 // comparison with null below, and this reference is not
2379 // kept afterwards.
2380 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2381 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2382 __ Bind(&do_copy);
2383 } else {
2384 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2385 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002386 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002387 // Non read barrier code.
2388
2389 // /* HeapReference<Class> */ temp1 = dest->klass_
2390 __ Ldr(temp1, MemOperand(dest, class_offset));
2391 // /* HeapReference<Class> */ temp2 = src->klass_
2392 __ Ldr(temp2, MemOperand(src, class_offset));
2393 bool did_unpoison = false;
2394 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2395 !optimizations.GetSourceIsNonPrimitiveArray()) {
2396 // One or two of the references need to be unpoisoned. Unpoison them
2397 // both to make the identity check valid.
2398 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2399 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2400 did_unpoison = true;
2401 }
2402
2403 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2404 // Bail out if the destination is not a non primitive array.
2405 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2406 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2407 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2408 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2409 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2410 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2411 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2412 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2413 }
2414
2415 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2416 // Bail out if the source is not a non primitive array.
2417 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2418 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2419 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2420 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2421 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2422 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2423 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2424 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2425 }
2426
2427 __ Cmp(temp1, temp2);
2428
2429 if (optimizations.GetDestinationIsTypedObjectArray()) {
2430 vixl::aarch64::Label do_copy;
2431 __ B(&do_copy, eq);
2432 if (!did_unpoison) {
2433 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2434 }
2435 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2436 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2437 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2438 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2439 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2440 // No need to unpoison the result, we're comparing against null.
2441 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2442 __ Bind(&do_copy);
2443 } else {
2444 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2445 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002446 }
2447 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2448 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2449 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002450 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2451 // /* HeapReference<Class> */ temp1 = src->klass_
2452 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2453 temp1_loc,
2454 src.W(),
2455 class_offset,
2456 temp2,
2457 /* needs_null_check */ false,
2458 /* use_load_acquire */ false);
2459 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2460 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2461 temp2_loc,
2462 temp1,
2463 component_offset,
2464 temp3,
2465 /* needs_null_check */ false,
2466 /* use_load_acquire */ false);
2467 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2468 // If heap poisoning is enabled, `temp2` has been unpoisoned
2469 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2470 } else {
2471 // /* HeapReference<Class> */ temp1 = src->klass_
2472 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2473 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2474 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2475 __ Ldr(temp2, HeapOperand(temp1, component_offset));
2476 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2477 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2478 }
2479 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2480 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
donghui.baic2ec9ad2016-03-10 14:02:55 +08002481 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain0b671c02016-08-19 12:02:34 +01002482 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002483 }
2484
2485 Register src_curr_addr = temp1.X();
2486 Register dst_curr_addr = temp2.X();
Roland Levillain0b671c02016-08-19 12:02:34 +01002487 Register src_stop_addr;
2488 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2489 // Temporary register IP0, obtained from the VIXL scratch
2490 // register pool as `temp3`, cannot be used in
2491 // ReadBarrierSystemArrayCopySlowPathARM64 (because that
2492 // register is clobbered by ReadBarrierMarkRegX entry points).
2493 // So another temporary register allocated by the register
2494 // allocator instead.
2495 DCHECK_EQ(LocationFrom(temp3).reg(), IP0);
2496 src_stop_addr = XRegisterFrom(locations->GetTemp(2));
2497 } else {
2498 src_stop_addr = temp3.X();
2499 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002500
2501 GenSystemArrayCopyAddresses(masm,
2502 Primitive::kPrimNot,
2503 src,
2504 src_pos,
2505 dest,
2506 dest_pos,
2507 length,
2508 src_curr_addr,
2509 dst_curr_addr,
2510 src_stop_addr);
2511
donghui.baic2ec9ad2016-03-10 14:02:55 +08002512 const int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
Roland Levillain0b671c02016-08-19 12:02:34 +01002513
2514 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2515 // SystemArrayCopy implementation for Baker read barriers (see
2516 // also CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier):
2517 //
2518 // if (src_ptr != end_ptr) {
2519 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2520 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2521 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
2522 // if (is_gray) {
2523 // // Slow-path copy.
2524 // do {
2525 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2526 // } while (src_ptr != end_ptr)
2527 // } else {
2528 // // Fast-path copy.
2529 // do {
2530 // *dest_ptr++ = *src_ptr++;
2531 // } while (src_ptr != end_ptr)
2532 // }
2533 // }
2534
2535 vixl::aarch64::Label loop, done;
2536
2537 // Don't enter copy loop if `length == 0`.
2538 __ Cmp(src_curr_addr, src_stop_addr);
2539 __ B(&done, eq);
2540
donghui.baic2ec9ad2016-03-10 14:02:55 +08002541 Register tmp = temps.AcquireW();
Roland Levillain0b671c02016-08-19 12:02:34 +01002542 // Make sure `tmp` is not IP0, as it is clobbered by
2543 // ReadBarrierMarkRegX entry points in
2544 // ReadBarrierSystemArrayCopySlowPathARM64.
2545 DCHECK_NE(LocationFrom(tmp).reg(), IP0);
2546
2547 // /* int32_t */ monitor = src->monitor_
2548 __ Ldr(tmp, HeapOperand(src.W(), monitor_offset));
2549 // /* LockWord */ lock_word = LockWord(monitor)
2550 static_assert(sizeof(LockWord) == sizeof(int32_t),
2551 "art::LockWord and int32_t have different sizes.");
2552
2553 // Introduce a dependency on the lock_word including rb_state,
2554 // to prevent load-load reordering, and without using
2555 // a memory barrier (which would be more expensive).
2556 // `src` is unchanged by this operation, but its value now depends
2557 // on `tmp`.
2558 __ Add(src.X(), src.X(), Operand(tmp.X(), LSR, 32));
2559
2560 // Slow path used to copy array when `src` is gray.
2561 SlowPathCodeARM64* read_barrier_slow_path =
2562 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathARM64(invoke, LocationFrom(tmp));
2563 codegen_->AddSlowPath(read_barrier_slow_path);
2564
2565 // Given the numeric representation, it's enough to check the low bit of the rb_state.
2566 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
2567 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
2568 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
2569 __ Tbnz(tmp, LockWord::kReadBarrierStateShift, read_barrier_slow_path->GetEntryLabel());
2570
2571 // Fast-path copy.
2572 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2573 // poison/unpoison.
2574 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002575 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2576 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
Roland Levillain0b671c02016-08-19 12:02:34 +01002577 __ Cmp(src_curr_addr, src_stop_addr);
2578 __ B(&loop, ne);
2579
2580 __ Bind(read_barrier_slow_path->GetExitLabel());
2581 __ Bind(&done);
2582 } else {
2583 // Non read barrier code.
2584
2585 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2586 // poison/unpoison.
2587 vixl::aarch64::Label loop, done;
2588 __ Bind(&loop);
2589 __ Cmp(src_curr_addr, src_stop_addr);
2590 __ B(&done, eq);
2591 {
2592 Register tmp = temps.AcquireW();
2593 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2594 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
2595 }
2596 __ B(&loop);
2597 __ Bind(&done);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002598 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002599 }
2600 // We only need one card marking on the destination array.
2601 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2602
Roland Levillain0b671c02016-08-19 12:02:34 +01002603 __ Bind(intrinsic_slow_path->GetExitLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002604}
2605
Anton Kirilova3ffea22016-04-07 17:02:37 +01002606static void GenIsInfinite(LocationSummary* locations,
2607 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002608 MacroAssembler* masm) {
Anton Kirilova3ffea22016-04-07 17:02:37 +01002609 Operand infinity;
2610 Register out;
2611
2612 if (is64bit) {
2613 infinity = kPositiveInfinityDouble;
2614 out = XRegisterFrom(locations->Out());
2615 } else {
2616 infinity = kPositiveInfinityFloat;
2617 out = WRegisterFrom(locations->Out());
2618 }
2619
Scott Wakeling97c72b72016-06-24 16:19:36 +01002620 const Register zero = vixl::aarch64::Assembler::AppropriateZeroRegFor(out);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002621
2622 MoveFPToInt(locations, is64bit, masm);
2623 __ Eor(out, out, infinity);
2624 // We don't care about the sign bit, so shift left.
2625 __ Cmp(zero, Operand(out, LSL, 1));
2626 __ Cset(out, eq);
2627}
2628
2629void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2630 CreateFPToIntLocations(arena_, invoke);
2631}
2632
2633void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2634 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
2635}
2636
2637void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2638 CreateFPToIntLocations(arena_, invoke);
2639}
2640
2641void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2642 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
2643}
2644
Aart Bik2f9fcc92016-03-01 15:16:54 -08002645UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002646UNIMPLEMENTED_INTRINSIC(ARM64, IntegerHighestOneBit)
2647UNIMPLEMENTED_INTRINSIC(ARM64, LongHighestOneBit)
2648UNIMPLEMENTED_INTRINSIC(ARM64, IntegerLowestOneBit)
2649UNIMPLEMENTED_INTRINSIC(ARM64, LongLowestOneBit)
Andreas Gampe878d58c2015-01-15 23:24:00 -08002650
Aart Bik0e54c012016-03-04 12:08:31 -08002651// 1.8.
2652UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
2653UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
2654UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
2655UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
2656UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002657
Aart Bik2f9fcc92016-03-01 15:16:54 -08002658UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01002659
2660#undef __
2661
Andreas Gampe878d58c2015-01-15 23:24:00 -08002662} // namespace arm64
2663} // namespace art