blob: bf797678221731039e3f104f228bfe6cad93c6b3 [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"
29#include "utils/arm64/constants_arm64.h"
30
Serban Constantinescu82e52ce2015-03-26 16:50:57 +000031#include "vixl/a64/disasm-a64.h"
32#include "vixl/a64/macro-assembler-a64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080033
34using namespace vixl; // NOLINT(build/namespaces)
35
36namespace art {
37
38namespace arm64 {
39
40using helpers::DRegisterFrom;
41using helpers::FPRegisterFrom;
42using helpers::HeapOperand;
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000043using helpers::LocationFrom;
Scott Wakeling9ee23f42015-07-23 10:44:35 +010044using helpers::OperandFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080045using helpers::RegisterFrom;
46using helpers::SRegisterFrom;
47using helpers::WRegisterFrom;
48using helpers::XRegisterFrom;
xueliang.zhong49924c92016-03-03 10:52:51 +000049using helpers::InputRegisterAt;
Scott Wakeling1f36f412016-04-21 11:13:45 +010050using helpers::OutputRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080051
Andreas Gampe878d58c2015-01-15 23:24:00 -080052namespace {
53
54ALWAYS_INLINE inline MemOperand AbsoluteHeapOperandFrom(Location location, size_t offset = 0) {
55 return MemOperand(XRegisterFrom(location), offset);
56}
57
58} // namespace
59
60vixl::MacroAssembler* IntrinsicCodeGeneratorARM64::GetVIXLAssembler() {
61 return codegen_->GetAssembler()->vixl_masm_;
62}
63
64ArenaAllocator* IntrinsicCodeGeneratorARM64::GetAllocator() {
65 return codegen_->GetGraph()->GetArena();
66}
67
68#define __ codegen->GetAssembler()->vixl_masm_->
69
70static void MoveFromReturnRegister(Location trg,
71 Primitive::Type type,
72 CodeGeneratorARM64* codegen) {
73 if (!trg.IsValid()) {
74 DCHECK(type == Primitive::kPrimVoid);
75 return;
76 }
77
78 DCHECK_NE(type, Primitive::kPrimVoid);
79
Jeff Hao848f70a2014-01-15 13:49:50 -080080 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080081 Register trg_reg = RegisterFrom(trg, type);
82 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
83 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
84 } else {
85 FPRegister trg_reg = FPRegisterFrom(trg, type);
86 FPRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
87 __ Fmov(trg_reg, res_reg);
88 }
89}
90
Roland Levillainec525fc2015-04-28 15:50:20 +010091static void MoveArguments(HInvoke* invoke, CodeGeneratorARM64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010092 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010093 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe878d58c2015-01-15 23:24:00 -080094}
95
96// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
97// call. This will copy the arguments into the positions for a regular call.
98//
99// Note: The actual parameters are required to be in the locations given by the invoke's location
100// summary. If an intrinsic modifies those locations before a slowpath call, they must be
101// restored!
102class IntrinsicSlowPathARM64 : public SlowPathCodeARM64 {
103 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000104 explicit IntrinsicSlowPathARM64(HInvoke* invoke)
105 : SlowPathCodeARM64(invoke), invoke_(invoke) { }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800106
107 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
108 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
109 __ Bind(GetEntryLabel());
110
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000111 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800112
Roland Levillainec525fc2015-04-28 15:50:20 +0100113 MoveArguments(invoke_, codegen);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800114
115 if (invoke_->IsInvokeStaticOrDirect()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100116 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
117 LocationFrom(kArtMethodRegister));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800118 } else {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000119 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), LocationFrom(kArtMethodRegister));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800120 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000121 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800122
123 // Copy the result back to the expected output.
124 Location out = invoke_->GetLocations()->Out();
125 if (out.IsValid()) {
126 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
127 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
128 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
129 }
130
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000131 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800132 __ B(GetExitLabel());
133 }
134
Alexandre Rames9931f312015-06-19 14:47:01 +0100135 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathARM64"; }
136
Andreas Gampe878d58c2015-01-15 23:24:00 -0800137 private:
138 // The instruction where this slow path is happening.
139 HInvoke* const invoke_;
140
141 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARM64);
142};
143
144#undef __
145
146bool IntrinsicLocationsBuilderARM64::TryDispatch(HInvoke* invoke) {
147 Dispatch(invoke);
148 LocationSummary* res = invoke->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000149 if (res == nullptr) {
150 return false;
151 }
152 if (kEmitCompilerReadBarrier && res->CanCall()) {
153 // Generating an intrinsic for this HInvoke may produce an
154 // IntrinsicSlowPathARM64 slow path. Currently this approach
155 // does not work when using read barriers, as the emitted
156 // calling sequence will make use of another slow path
157 // (ReadBarrierForRootSlowPathARM64 for HInvokeStaticOrDirect,
158 // ReadBarrierSlowPathARM64 for HInvokeVirtual). So we bail
159 // out in this case.
160 //
161 // TODO: Find a way to have intrinsics work with read barriers.
162 invoke->SetLocations(nullptr);
163 return false;
164 }
165 return res->Intrinsified();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800166}
167
168#define __ masm->
169
170static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
171 LocationSummary* locations = new (arena) LocationSummary(invoke,
172 LocationSummary::kNoCall,
173 kIntrinsified);
174 locations->SetInAt(0, Location::RequiresFpuRegister());
175 locations->SetOut(Location::RequiresRegister());
176}
177
178static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
179 LocationSummary* locations = new (arena) LocationSummary(invoke,
180 LocationSummary::kNoCall,
181 kIntrinsified);
182 locations->SetInAt(0, Location::RequiresRegister());
183 locations->SetOut(Location::RequiresFpuRegister());
184}
185
186static void MoveFPToInt(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
187 Location input = locations->InAt(0);
188 Location output = locations->Out();
189 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
190 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
191}
192
193static void MoveIntToFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
194 Location input = locations->InAt(0);
195 Location output = locations->Out();
196 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
197 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
198}
199
200void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
201 CreateFPToIntLocations(arena_, invoke);
202}
203void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
204 CreateIntToFPLocations(arena_, invoke);
205}
206
207void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000208 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800209}
210void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000211 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800212}
213
214void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
215 CreateFPToIntLocations(arena_, invoke);
216}
217void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
218 CreateIntToFPLocations(arena_, invoke);
219}
220
221void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000222 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800223}
224void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000225 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800226}
227
228static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
229 LocationSummary* locations = new (arena) LocationSummary(invoke,
230 LocationSummary::kNoCall,
231 kIntrinsified);
232 locations->SetInAt(0, Location::RequiresRegister());
233 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
234}
235
236static void GenReverseBytes(LocationSummary* locations,
237 Primitive::Type type,
238 vixl::MacroAssembler* masm) {
239 Location in = locations->InAt(0);
240 Location out = locations->Out();
241
242 switch (type) {
243 case Primitive::kPrimShort:
244 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
245 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
246 break;
247 case Primitive::kPrimInt:
248 case Primitive::kPrimLong:
249 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
250 break;
251 default:
252 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
253 UNREACHABLE();
254 }
255}
256
257void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
258 CreateIntToIntLocations(arena_, invoke);
259}
260
261void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
262 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
263}
264
265void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
266 CreateIntToIntLocations(arena_, invoke);
267}
268
269void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
270 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
271}
272
273void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
274 CreateIntToIntLocations(arena_, invoke);
275}
276
277void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
278 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetVIXLAssembler());
279}
280
Aart Bik7b565022016-01-28 14:36:22 -0800281static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
282 LocationSummary* locations = new (arena) LocationSummary(invoke,
283 LocationSummary::kNoCall,
284 kIntrinsified);
285 locations->SetInAt(0, Location::RequiresRegister());
286 locations->SetInAt(1, Location::RequiresRegister());
287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
288}
289
Scott Wakeling611d3392015-07-10 11:42:06 +0100290static void GenNumberOfLeadingZeros(LocationSummary* locations,
291 Primitive::Type type,
292 vixl::MacroAssembler* masm) {
293 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
294
295 Location in = locations->InAt(0);
296 Location out = locations->Out();
297
298 __ Clz(RegisterFrom(out, type), RegisterFrom(in, type));
299}
300
301void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
302 CreateIntToIntLocations(arena_, invoke);
303}
304
305void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
306 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
307}
308
309void IntrinsicLocationsBuilderARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
310 CreateIntToIntLocations(arena_, invoke);
311}
312
313void IntrinsicCodeGeneratorARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
314 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
315}
316
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100317static void GenNumberOfTrailingZeros(LocationSummary* locations,
318 Primitive::Type type,
319 vixl::MacroAssembler* masm) {
320 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
321
322 Location in = locations->InAt(0);
323 Location out = locations->Out();
324
325 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
326 __ Clz(RegisterFrom(out, type), RegisterFrom(out, type));
327}
328
329void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
330 CreateIntToIntLocations(arena_, invoke);
331}
332
333void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
334 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
335}
336
337void IntrinsicLocationsBuilderARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
338 CreateIntToIntLocations(arena_, invoke);
339}
340
341void IntrinsicCodeGeneratorARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
342 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
343}
344
Andreas Gampe878d58c2015-01-15 23:24:00 -0800345static void GenReverse(LocationSummary* locations,
346 Primitive::Type type,
347 vixl::MacroAssembler* masm) {
348 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
349
350 Location in = locations->InAt(0);
351 Location out = locations->Out();
352
353 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
354}
355
356void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
357 CreateIntToIntLocations(arena_, invoke);
358}
359
360void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
361 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
362}
363
364void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
365 CreateIntToIntLocations(arena_, invoke);
366}
367
368void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
369 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
370}
371
Roland Levillainfa3912e2016-04-01 18:21:55 +0100372static void GenBitCount(HInvoke* instr, Primitive::Type type, vixl::MacroAssembler* masm) {
373 DCHECK(Primitive::IsIntOrLongType(type)) << type;
374 DCHECK_EQ(instr->GetType(), Primitive::kPrimInt);
375 DCHECK_EQ(Primitive::PrimitiveKind(instr->InputAt(0)->GetType()), type);
xueliang.zhong49924c92016-03-03 10:52:51 +0000376
xueliang.zhong49924c92016-03-03 10:52:51 +0000377 UseScratchRegisterScope temps(masm);
378
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000379 Register src = InputRegisterAt(instr, 0);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100380 Register dst = RegisterFrom(instr->GetLocations()->Out(), type);
381 FPRegister fpr = (type == Primitive::kPrimLong) ? temps.AcquireD() : temps.AcquireS();
xueliang.zhong49924c92016-03-03 10:52:51 +0000382
383 __ Fmov(fpr, src);
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000384 __ Cnt(fpr.V8B(), fpr.V8B());
385 __ Addv(fpr.B(), fpr.V8B());
xueliang.zhong49924c92016-03-03 10:52:51 +0000386 __ Fmov(dst, fpr);
387}
388
389void IntrinsicLocationsBuilderARM64::VisitLongBitCount(HInvoke* invoke) {
390 CreateIntToIntLocations(arena_, invoke);
391}
392
393void IntrinsicCodeGeneratorARM64::VisitLongBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100394 GenBitCount(invoke, Primitive::kPrimLong, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000395}
396
397void IntrinsicLocationsBuilderARM64::VisitIntegerBitCount(HInvoke* invoke) {
398 CreateIntToIntLocations(arena_, invoke);
399}
400
401void IntrinsicCodeGeneratorARM64::VisitIntegerBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100402 GenBitCount(invoke, Primitive::kPrimInt, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000403}
404
Andreas Gampe878d58c2015-01-15 23:24:00 -0800405static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800406 LocationSummary* locations = new (arena) LocationSummary(invoke,
407 LocationSummary::kNoCall,
408 kIntrinsified);
409 locations->SetInAt(0, Location::RequiresFpuRegister());
410 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
411}
412
413static void MathAbsFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
414 Location in = locations->InAt(0);
415 Location out = locations->Out();
416
417 FPRegister in_reg = is64bit ? DRegisterFrom(in) : SRegisterFrom(in);
418 FPRegister out_reg = is64bit ? DRegisterFrom(out) : SRegisterFrom(out);
419
420 __ Fabs(out_reg, in_reg);
421}
422
423void IntrinsicLocationsBuilderARM64::VisitMathAbsDouble(HInvoke* invoke) {
424 CreateFPToFPLocations(arena_, invoke);
425}
426
427void IntrinsicCodeGeneratorARM64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000428 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800429}
430
431void IntrinsicLocationsBuilderARM64::VisitMathAbsFloat(HInvoke* invoke) {
432 CreateFPToFPLocations(arena_, invoke);
433}
434
435void IntrinsicCodeGeneratorARM64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000436 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800437}
438
439static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
440 LocationSummary* locations = new (arena) LocationSummary(invoke,
441 LocationSummary::kNoCall,
442 kIntrinsified);
443 locations->SetInAt(0, Location::RequiresRegister());
444 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
445}
446
447static void GenAbsInteger(LocationSummary* locations,
448 bool is64bit,
449 vixl::MacroAssembler* masm) {
450 Location in = locations->InAt(0);
451 Location output = locations->Out();
452
453 Register in_reg = is64bit ? XRegisterFrom(in) : WRegisterFrom(in);
454 Register out_reg = is64bit ? XRegisterFrom(output) : WRegisterFrom(output);
455
456 __ Cmp(in_reg, Operand(0));
457 __ Cneg(out_reg, in_reg, lt);
458}
459
460void IntrinsicLocationsBuilderARM64::VisitMathAbsInt(HInvoke* invoke) {
461 CreateIntToInt(arena_, invoke);
462}
463
464void IntrinsicCodeGeneratorARM64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000465 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800466}
467
468void IntrinsicLocationsBuilderARM64::VisitMathAbsLong(HInvoke* invoke) {
469 CreateIntToInt(arena_, invoke);
470}
471
472void IntrinsicCodeGeneratorARM64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000473 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800474}
475
476static void GenMinMaxFP(LocationSummary* locations,
477 bool is_min,
478 bool is_double,
479 vixl::MacroAssembler* masm) {
480 Location op1 = locations->InAt(0);
481 Location op2 = locations->InAt(1);
482 Location out = locations->Out();
483
484 FPRegister op1_reg = is_double ? DRegisterFrom(op1) : SRegisterFrom(op1);
485 FPRegister op2_reg = is_double ? DRegisterFrom(op2) : SRegisterFrom(op2);
486 FPRegister out_reg = is_double ? DRegisterFrom(out) : SRegisterFrom(out);
487 if (is_min) {
488 __ Fmin(out_reg, op1_reg, op2_reg);
489 } else {
490 __ Fmax(out_reg, op1_reg, op2_reg);
491 }
492}
493
494static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
495 LocationSummary* locations = new (arena) LocationSummary(invoke,
496 LocationSummary::kNoCall,
497 kIntrinsified);
498 locations->SetInAt(0, Location::RequiresFpuRegister());
499 locations->SetInAt(1, Location::RequiresFpuRegister());
500 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
501}
502
503void IntrinsicLocationsBuilderARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
504 CreateFPFPToFPLocations(arena_, invoke);
505}
506
507void IntrinsicCodeGeneratorARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000508 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800509}
510
511void IntrinsicLocationsBuilderARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
512 CreateFPFPToFPLocations(arena_, invoke);
513}
514
515void IntrinsicCodeGeneratorARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000516 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800517}
518
519void IntrinsicLocationsBuilderARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
520 CreateFPFPToFPLocations(arena_, invoke);
521}
522
523void IntrinsicCodeGeneratorARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000524 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800525}
526
527void IntrinsicLocationsBuilderARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
528 CreateFPFPToFPLocations(arena_, invoke);
529}
530
531void IntrinsicCodeGeneratorARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000532 GenMinMaxFP(
533 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800534}
535
536static void GenMinMax(LocationSummary* locations,
537 bool is_min,
538 bool is_long,
539 vixl::MacroAssembler* masm) {
540 Location op1 = locations->InAt(0);
541 Location op2 = locations->InAt(1);
542 Location out = locations->Out();
543
544 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
545 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
546 Register out_reg = is_long ? XRegisterFrom(out) : WRegisterFrom(out);
547
548 __ Cmp(op1_reg, op2_reg);
549 __ Csel(out_reg, op1_reg, op2_reg, is_min ? lt : gt);
550}
551
Andreas Gampe878d58c2015-01-15 23:24:00 -0800552void IntrinsicLocationsBuilderARM64::VisitMathMinIntInt(HInvoke* invoke) {
553 CreateIntIntToIntLocations(arena_, invoke);
554}
555
556void IntrinsicCodeGeneratorARM64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000557 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800558}
559
560void IntrinsicLocationsBuilderARM64::VisitMathMinLongLong(HInvoke* invoke) {
561 CreateIntIntToIntLocations(arena_, invoke);
562}
563
564void IntrinsicCodeGeneratorARM64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000565 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800566}
567
568void IntrinsicLocationsBuilderARM64::VisitMathMaxIntInt(HInvoke* invoke) {
569 CreateIntIntToIntLocations(arena_, invoke);
570}
571
572void IntrinsicCodeGeneratorARM64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000573 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800574}
575
576void IntrinsicLocationsBuilderARM64::VisitMathMaxLongLong(HInvoke* invoke) {
577 CreateIntIntToIntLocations(arena_, invoke);
578}
579
580void IntrinsicCodeGeneratorARM64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000581 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800582}
583
584void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
585 CreateFPToFPLocations(arena_, invoke);
586}
587
588void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
589 LocationSummary* locations = invoke->GetLocations();
590 vixl::MacroAssembler* masm = GetVIXLAssembler();
591 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
592}
593
594void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
595 CreateFPToFPLocations(arena_, invoke);
596}
597
598void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
599 LocationSummary* locations = invoke->GetLocations();
600 vixl::MacroAssembler* masm = GetVIXLAssembler();
601 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
602}
603
604void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
605 CreateFPToFPLocations(arena_, invoke);
606}
607
608void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
609 LocationSummary* locations = invoke->GetLocations();
610 vixl::MacroAssembler* masm = GetVIXLAssembler();
611 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
612}
613
614void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
615 CreateFPToFPLocations(arena_, invoke);
616}
617
618void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
619 LocationSummary* locations = invoke->GetLocations();
620 vixl::MacroAssembler* masm = GetVIXLAssembler();
621 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
622}
623
624static void CreateFPToIntPlusTempLocations(ArenaAllocator* arena, HInvoke* invoke) {
625 LocationSummary* locations = new (arena) LocationSummary(invoke,
626 LocationSummary::kNoCall,
627 kIntrinsified);
628 locations->SetInAt(0, Location::RequiresFpuRegister());
629 locations->SetOut(Location::RequiresRegister());
630}
631
632static void GenMathRound(LocationSummary* locations,
633 bool is_double,
634 vixl::MacroAssembler* masm) {
635 FPRegister in_reg = is_double ?
636 DRegisterFrom(locations->InAt(0)) : SRegisterFrom(locations->InAt(0));
637 Register out_reg = is_double ?
638 XRegisterFrom(locations->Out()) : WRegisterFrom(locations->Out());
639 UseScratchRegisterScope temps(masm);
640 FPRegister temp1_reg = temps.AcquireSameSizeAs(in_reg);
641
642 // 0.5 can be encoded as an immediate, so use fmov.
643 if (is_double) {
644 __ Fmov(temp1_reg, static_cast<double>(0.5));
645 } else {
646 __ Fmov(temp1_reg, static_cast<float>(0.5));
647 }
648 __ Fadd(temp1_reg, in_reg, temp1_reg);
649 __ Fcvtms(out_reg, temp1_reg);
650}
651
652void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800653 // See intrinsics.h.
654 if (kRoundIsPlusPointFive) {
655 CreateFPToIntPlusTempLocations(arena_, invoke);
656 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800657}
658
659void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000660 GenMathRound(invoke->GetLocations(), /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800661}
662
663void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800664 // See intrinsics.h.
665 if (kRoundIsPlusPointFive) {
666 CreateFPToIntPlusTempLocations(arena_, invoke);
667 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800668}
669
670void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000671 GenMathRound(invoke->GetLocations(), /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800672}
673
674void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
675 CreateIntToIntLocations(arena_, invoke);
676}
677
678void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
679 vixl::MacroAssembler* masm = GetVIXLAssembler();
680 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
681 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
682}
683
684void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
685 CreateIntToIntLocations(arena_, invoke);
686}
687
688void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
689 vixl::MacroAssembler* masm = GetVIXLAssembler();
690 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
691 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
692}
693
694void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
695 CreateIntToIntLocations(arena_, invoke);
696}
697
698void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
699 vixl::MacroAssembler* masm = GetVIXLAssembler();
700 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
701 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
702}
703
704void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
705 CreateIntToIntLocations(arena_, invoke);
706}
707
708void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
709 vixl::MacroAssembler* masm = GetVIXLAssembler();
710 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
711 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
712}
713
714static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
715 LocationSummary* locations = new (arena) LocationSummary(invoke,
716 LocationSummary::kNoCall,
717 kIntrinsified);
718 locations->SetInAt(0, Location::RequiresRegister());
719 locations->SetInAt(1, Location::RequiresRegister());
720}
721
722void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
723 CreateIntIntToVoidLocations(arena_, invoke);
724}
725
726void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
727 vixl::MacroAssembler* masm = GetVIXLAssembler();
728 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
729 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
730}
731
732void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
733 CreateIntIntToVoidLocations(arena_, invoke);
734}
735
736void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
737 vixl::MacroAssembler* masm = GetVIXLAssembler();
738 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
739 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
740}
741
742void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
743 CreateIntIntToVoidLocations(arena_, invoke);
744}
745
746void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
747 vixl::MacroAssembler* masm = GetVIXLAssembler();
748 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
749 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
750}
751
752void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
753 CreateIntIntToVoidLocations(arena_, invoke);
754}
755
756void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
757 vixl::MacroAssembler* masm = GetVIXLAssembler();
758 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
759 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
760}
761
762void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
763 LocationSummary* locations = new (arena_) LocationSummary(invoke,
764 LocationSummary::kNoCall,
765 kIntrinsified);
766 locations->SetOut(Location::RequiresRegister());
767}
768
769void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
770 codegen_->Load(Primitive::kPrimNot, WRegisterFrom(invoke->GetLocations()->Out()),
771 MemOperand(tr, Thread::PeerOffset<8>().Int32Value()));
772}
773
774static void GenUnsafeGet(HInvoke* invoke,
775 Primitive::Type type,
776 bool is_volatile,
777 CodeGeneratorARM64* codegen) {
778 LocationSummary* locations = invoke->GetLocations();
779 DCHECK((type == Primitive::kPrimInt) ||
780 (type == Primitive::kPrimLong) ||
781 (type == Primitive::kPrimNot));
782 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000783 Location base_loc = locations->InAt(1);
784 Register base = WRegisterFrom(base_loc); // Object pointer.
785 Location offset_loc = locations->InAt(2);
786 Register offset = XRegisterFrom(offset_loc); // Long offset.
787 Location trg_loc = locations->Out();
788 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800789
Roland Levillain44015862016-01-22 11:47:17 +0000790 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
791 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
792 UseScratchRegisterScope temps(masm);
793 Register temp = temps.AcquireW();
794 codegen->GenerateArrayLoadWithBakerReadBarrier(
795 invoke, trg_loc, base, 0U, offset_loc, temp, /* needs_null_check */ false);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800796 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000797 // Other cases.
798 MemOperand mem_op(base.X(), offset);
799 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000800 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000801 } else {
802 codegen->Load(type, trg, mem_op);
803 }
Roland Levillain4d027112015-07-01 15:41:14 +0100804
Roland Levillain44015862016-01-22 11:47:17 +0000805 if (type == Primitive::kPrimNot) {
806 DCHECK(trg.IsW());
807 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
808 }
Roland Levillain4d027112015-07-01 15:41:14 +0100809 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800810}
811
812static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000813 bool can_call = kEmitCompilerReadBarrier &&
814 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
815 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800816 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000817 can_call ?
818 LocationSummary::kCallOnSlowPath :
819 LocationSummary::kNoCall,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800820 kIntrinsified);
821 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
822 locations->SetInAt(1, Location::RequiresRegister());
823 locations->SetInAt(2, Location::RequiresRegister());
824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
825}
826
827void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
828 CreateIntIntIntToIntLocations(arena_, invoke);
829}
830void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
831 CreateIntIntIntToIntLocations(arena_, invoke);
832}
833void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
834 CreateIntIntIntToIntLocations(arena_, invoke);
835}
836void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
837 CreateIntIntIntToIntLocations(arena_, invoke);
838}
839void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
840 CreateIntIntIntToIntLocations(arena_, invoke);
841}
842void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
843 CreateIntIntIntToIntLocations(arena_, invoke);
844}
845
846void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000847 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800848}
849void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000850 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800851}
852void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000853 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800854}
855void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000856 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800857}
858void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000859 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800860}
861void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000862 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800863}
864
865static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
866 LocationSummary* locations = new (arena) LocationSummary(invoke,
867 LocationSummary::kNoCall,
868 kIntrinsified);
869 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
870 locations->SetInAt(1, Location::RequiresRegister());
871 locations->SetInAt(2, Location::RequiresRegister());
872 locations->SetInAt(3, Location::RequiresRegister());
873}
874
875void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
876 CreateIntIntIntIntToVoid(arena_, invoke);
877}
878void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
879 CreateIntIntIntIntToVoid(arena_, invoke);
880}
881void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
882 CreateIntIntIntIntToVoid(arena_, invoke);
883}
884void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
885 CreateIntIntIntIntToVoid(arena_, invoke);
886}
887void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
888 CreateIntIntIntIntToVoid(arena_, invoke);
889}
890void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
891 CreateIntIntIntIntToVoid(arena_, invoke);
892}
893void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
894 CreateIntIntIntIntToVoid(arena_, invoke);
895}
896void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
897 CreateIntIntIntIntToVoid(arena_, invoke);
898}
899void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
900 CreateIntIntIntIntToVoid(arena_, invoke);
901}
902
903static void GenUnsafePut(LocationSummary* locations,
904 Primitive::Type type,
905 bool is_volatile,
906 bool is_ordered,
907 CodeGeneratorARM64* codegen) {
908 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
909
910 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
911 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
912 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100913 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800914 MemOperand mem_op(base.X(), offset);
915
Roland Levillain4d027112015-07-01 15:41:14 +0100916 {
917 // We use a block to end the scratch scope before the write barrier, thus
918 // freeing the temporary registers so they can be used in `MarkGCCard`.
919 UseScratchRegisterScope temps(masm);
920
921 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
922 DCHECK(value.IsW());
923 Register temp = temps.AcquireW();
924 __ Mov(temp.W(), value.W());
925 codegen->GetAssembler()->PoisonHeapReference(temp.W());
926 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800927 }
Roland Levillain4d027112015-07-01 15:41:14 +0100928
929 if (is_volatile || is_ordered) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000930 codegen->StoreRelease(type, source, mem_op);
Roland Levillain4d027112015-07-01 15:41:14 +0100931 } else {
932 codegen->Store(type, source, mem_op);
933 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800934 }
935
936 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100937 bool value_can_be_null = true; // TODO: Worth finding out this information?
938 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800939 }
940}
941
942void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000943 GenUnsafePut(invoke->GetLocations(),
944 Primitive::kPrimInt,
945 /* is_volatile */ false,
946 /* is_ordered */ false,
947 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800948}
949void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000950 GenUnsafePut(invoke->GetLocations(),
951 Primitive::kPrimInt,
952 /* is_volatile */ false,
953 /* is_ordered */ true,
954 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800955}
956void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000957 GenUnsafePut(invoke->GetLocations(),
958 Primitive::kPrimInt,
959 /* is_volatile */ true,
960 /* is_ordered */ false,
961 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800962}
963void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000964 GenUnsafePut(invoke->GetLocations(),
965 Primitive::kPrimNot,
966 /* is_volatile */ false,
967 /* is_ordered */ false,
968 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800969}
970void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000971 GenUnsafePut(invoke->GetLocations(),
972 Primitive::kPrimNot,
973 /* is_volatile */ false,
974 /* is_ordered */ true,
975 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800976}
977void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000978 GenUnsafePut(invoke->GetLocations(),
979 Primitive::kPrimNot,
980 /* is_volatile */ true,
981 /* is_ordered */ false,
982 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800983}
984void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000985 GenUnsafePut(invoke->GetLocations(),
986 Primitive::kPrimLong,
987 /* is_volatile */ false,
988 /* is_ordered */ false,
989 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800990}
991void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000992 GenUnsafePut(invoke->GetLocations(),
993 Primitive::kPrimLong,
994 /* is_volatile */ false,
995 /* is_ordered */ true,
996 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800997}
998void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000999 GenUnsafePut(invoke->GetLocations(),
1000 Primitive::kPrimLong,
1001 /* is_volatile */ true,
1002 /* is_ordered */ false,
1003 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001004}
1005
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001006static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
1007 HInvoke* invoke,
1008 Primitive::Type type) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001009 LocationSummary* locations = new (arena) LocationSummary(invoke,
1010 LocationSummary::kNoCall,
1011 kIntrinsified);
1012 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1013 locations->SetInAt(1, Location::RequiresRegister());
1014 locations->SetInAt(2, Location::RequiresRegister());
1015 locations->SetInAt(3, Location::RequiresRegister());
1016 locations->SetInAt(4, Location::RequiresRegister());
1017
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001018 // If heap poisoning is enabled, we don't want the unpoisoning
1019 // operations to potentially clobber the output.
1020 Location::OutputOverlap overlaps = (kPoisonHeapReferences && type == Primitive::kPrimNot)
1021 ? Location::kOutputOverlap
1022 : Location::kNoOutputOverlap;
1023 locations->SetOut(Location::RequiresRegister(), overlaps);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001024}
1025
1026static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorARM64* codegen) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001027 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
1028
1029 Register out = WRegisterFrom(locations->Out()); // Boolean result.
1030
1031 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1032 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1033 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1034 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1035
1036 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
1037 if (type == Primitive::kPrimNot) {
1038 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001039 bool value_can_be_null = true; // TODO: Worth finding out this information?
1040 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001041 }
1042
1043 UseScratchRegisterScope temps(masm);
1044 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1045 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1046
1047 Register tmp_32 = tmp_value.W();
1048
1049 __ Add(tmp_ptr, base.X(), Operand(offset));
1050
Roland Levillain4d027112015-07-01 15:41:14 +01001051 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1052 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001053 if (value.Is(expected)) {
1054 // Do not poison `value`, as it is the same register as
1055 // `expected`, which has just been poisoned.
1056 } else {
1057 codegen->GetAssembler()->PoisonHeapReference(value);
1058 }
Roland Levillain4d027112015-07-01 15:41:14 +01001059 }
1060
Andreas Gampe878d58c2015-01-15 23:24:00 -08001061 // do {
1062 // tmp_value = [tmp_ptr] - expected;
1063 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1064 // result = tmp_value != 0;
1065
1066 vixl::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001067 __ Bind(&loop_head);
1068 // TODO: When `type == Primitive::kPrimNot`, add a read barrier for
1069 // the reference stored in the object before attempting the CAS,
1070 // similar to the one in the art::Unsafe_compareAndSwapObject JNI
1071 // implementation.
1072 //
1073 // Note that this code is not (yet) used when read barriers are
1074 // enabled (see IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject).
1075 DCHECK(!(type == Primitive::kPrimNot && kEmitCompilerReadBarrier));
1076 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1077 __ Cmp(tmp_value, expected);
1078 __ B(&exit_loop, ne);
1079 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1080 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001081 __ Bind(&exit_loop);
1082 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001083
1084 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01001085 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001086 if (value.Is(expected)) {
1087 // Do not unpoison `value`, as it is the same register as
1088 // `expected`, which has just been unpoisoned.
1089 } else {
1090 codegen->GetAssembler()->UnpoisonHeapReference(value);
1091 }
Roland Levillain4d027112015-07-01 15:41:14 +01001092 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001093}
1094
1095void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001096 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001097}
1098void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001099 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001100}
1101void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00001102 // The UnsafeCASObject intrinsic is missing a read barrier, and
1103 // therefore sometimes does not work as expected (b/25883050).
1104 // Turn it off temporarily as a quick fix, until the read barrier is
1105 // implemented (see TODO in GenCAS below).
1106 //
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001107 // TODO(rpl): Fix this issue and re-enable this intrinsic with read barriers.
1108 if (kEmitCompilerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001109 return;
1110 }
1111
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001112 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001113}
1114
1115void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
1116 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1117}
1118void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
1119 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
1120}
1121void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
1122 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1123}
1124
1125void IntrinsicLocationsBuilderARM64::VisitStringCharAt(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001126 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1127 LocationSummary::kCallOnSlowPath,
1128 kIntrinsified);
1129 locations->SetInAt(0, Location::RequiresRegister());
1130 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray82f34492015-02-04 10:44:23 +00001131 // In case we need to go in the slow path, we can't have the output be the same
1132 // as the input: the current liveness analysis considers the input to be live
1133 // at the point of the call.
1134 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001135}
1136
1137void IntrinsicCodeGeneratorARM64::VisitStringCharAt(HInvoke* invoke) {
1138 vixl::MacroAssembler* masm = GetVIXLAssembler();
1139 LocationSummary* locations = invoke->GetLocations();
1140
1141 // Location of reference to data array
1142 const MemberOffset value_offset = mirror::String::ValueOffset();
1143 // Location of count
1144 const MemberOffset count_offset = mirror::String::CountOffset();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001145
1146 Register obj = WRegisterFrom(locations->InAt(0)); // String object pointer.
1147 Register idx = WRegisterFrom(locations->InAt(1)); // Index of character.
1148 Register out = WRegisterFrom(locations->Out()); // Result character.
1149
1150 UseScratchRegisterScope temps(masm);
1151 Register temp = temps.AcquireW();
1152 Register array_temp = temps.AcquireW(); // We can trade this for worse scheduling.
1153
1154 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
1155 // the cost.
1156 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
1157 // we will not optimize the code for constants (which would save a register).
1158
1159 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1160 codegen_->AddSlowPath(slow_path);
1161
1162 __ Ldr(temp, HeapOperand(obj, count_offset)); // temp = str.length.
1163 codegen_->MaybeRecordImplicitNullCheck(invoke);
1164 __ Cmp(idx, temp);
1165 __ B(hs, slow_path->GetEntryLabel());
1166
Jeff Hao848f70a2014-01-15 13:49:50 -08001167 __ Add(array_temp, obj, Operand(value_offset.Int32Value())); // array_temp := str.value.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001168
1169 // Load the value.
Jeff Hao848f70a2014-01-15 13:49:50 -08001170 __ Ldrh(out, MemOperand(array_temp.X(), idx, UXTW, 1)); // out := array_temp[idx].
Andreas Gampe878d58c2015-01-15 23:24:00 -08001171
1172 __ Bind(slow_path->GetExitLabel());
1173}
1174
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001175void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001176 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Scott Wakeling1f36f412016-04-21 11:13:45 +01001177 invoke->InputAt(1)->CanBeNull()
1178 ? LocationSummary::kCallOnSlowPath
1179 : LocationSummary::kNoCall,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001180 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001181 locations->SetInAt(0, Location::RequiresRegister());
1182 locations->SetInAt(1, Location::RequiresRegister());
1183 locations->AddTemp(Location::RequiresRegister());
1184 locations->AddTemp(Location::RequiresRegister());
1185 locations->AddTemp(Location::RequiresRegister());
1186 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001187}
1188
1189void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
1190 vixl::MacroAssembler* masm = GetVIXLAssembler();
1191 LocationSummary* locations = invoke->GetLocations();
1192
Scott Wakeling1f36f412016-04-21 11:13:45 +01001193 Register str = XRegisterFrom(locations->InAt(0));
1194 Register arg = XRegisterFrom(locations->InAt(1));
1195 Register out = OutputRegister(invoke);
1196
1197 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1198 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1199 Register temp2 = WRegisterFrom(locations->GetTemp(2));
1200
1201 vixl::Label loop;
1202 vixl::Label find_char_diff;
1203 vixl::Label end;
1204
1205 // Get offsets of count and value fields within a string object.
1206 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1207 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1208
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001209 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001210 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001211
Scott Wakeling1f36f412016-04-21 11:13:45 +01001212 // Take slow path and throw if input can be and is null.
1213 SlowPathCodeARM64* slow_path = nullptr;
1214 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1215 if (can_slow_path) {
1216 slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1217 codegen_->AddSlowPath(slow_path);
1218 __ Cbz(arg, slow_path->GetEntryLabel());
1219 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001220
Scott Wakeling1f36f412016-04-21 11:13:45 +01001221 // Reference equality check, return 0 if same reference.
1222 __ Subs(out, str, arg);
1223 __ B(&end, eq);
1224 // Load lengths of this and argument strings.
1225 __ Ldr(temp0, MemOperand(str.X(), count_offset));
1226 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1227 // Return zero if both strings are empty.
1228 __ Orr(out, temp0, temp1);
1229 __ Cbz(out, &end);
1230 // out = length diff.
1231 __ Subs(out, temp0, temp1);
1232 // temp2 = min(len(str), len(arg)).
1233 __ Csel(temp2, temp1, temp0, ge);
1234 // Shorter string is empty?
1235 __ Cbz(temp2, &end);
1236
1237 // Store offset of string value in preparation for comparison loop.
1238 __ Mov(temp1, value_offset);
1239
1240 UseScratchRegisterScope scratch_scope(masm);
1241 Register temp4 = scratch_scope.AcquireX();
1242
1243 // Assertions that must hold in order to compare strings 4 characters at a time.
1244 DCHECK_ALIGNED(value_offset, 8);
1245 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1246
1247 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1248 DCHECK_EQ(char_size, 2u);
1249
1250 // Promote temp0 to an X reg, ready for LDR.
1251 temp0 = temp0.X();
1252
1253 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1254 __ Bind(&loop);
1255 __ Ldr(temp4, MemOperand(str.X(), temp1));
1256 __ Ldr(temp0, MemOperand(arg.X(), temp1));
1257 __ Cmp(temp4, temp0);
1258 __ B(ne, &find_char_diff);
1259 __ Add(temp1, temp1, char_size * 4);
1260 __ Subs(temp2, temp2, 4);
1261 __ B(gt, &loop);
1262 __ B(&end);
1263
1264 // Promote temp1 to an X reg, ready for EOR.
1265 temp1 = temp1.X();
1266
1267 // Find the single 16-bit character difference.
1268 __ Bind(&find_char_diff);
1269 // Get the bit position of the first character that differs.
1270 __ Eor(temp1, temp0, temp4);
1271 __ Rbit(temp1, temp1);
1272 __ Clz(temp1, temp1);
1273 __ Bic(temp1, temp1, 0xf);
1274 // If the number of 16-bit chars remaining <= the index where the difference occurs (0-3), then
1275 // the difference occurs outside the remaining string data, so just return length diff (out).
1276 __ Cmp(temp2, Operand(temp1, LSR, 4));
1277 __ B(le, &end);
1278 // Extract the characters and calculate the difference.
1279 __ Lsr(temp0, temp0, temp1);
1280 __ Lsr(temp4, temp4, temp1);
1281 __ And(temp4, temp4, 0xffff);
1282 __ Sub(out, temp4, Operand(temp0, UXTH));
1283
1284 __ Bind(&end);
1285
1286 if (can_slow_path) {
1287 __ Bind(slow_path->GetExitLabel());
1288 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001289}
1290
Agi Csakiea34b402015-08-13 17:51:19 -07001291void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
1292 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1293 LocationSummary::kNoCall,
1294 kIntrinsified);
1295 locations->SetInAt(0, Location::RequiresRegister());
1296 locations->SetInAt(1, Location::RequiresRegister());
1297 // Temporary registers to store lengths of strings and for calculations.
1298 locations->AddTemp(Location::RequiresRegister());
1299 locations->AddTemp(Location::RequiresRegister());
1300
1301 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1302}
1303
1304void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
1305 vixl::MacroAssembler* masm = GetVIXLAssembler();
1306 LocationSummary* locations = invoke->GetLocations();
1307
1308 Register str = WRegisterFrom(locations->InAt(0));
1309 Register arg = WRegisterFrom(locations->InAt(1));
1310 Register out = XRegisterFrom(locations->Out());
1311
1312 UseScratchRegisterScope scratch_scope(masm);
1313 Register temp = scratch_scope.AcquireW();
1314 Register temp1 = WRegisterFrom(locations->GetTemp(0));
1315 Register temp2 = WRegisterFrom(locations->GetTemp(1));
1316
1317 vixl::Label loop;
1318 vixl::Label end;
1319 vixl::Label return_true;
1320 vixl::Label return_false;
1321
1322 // Get offsets of count, value, and class fields within a string object.
1323 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1324 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1325 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1326
1327 // Note that the null check must have been done earlier.
1328 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1329
1330 // Check if input is null, return false if it is.
1331 __ Cbz(arg, &return_false);
1332
1333 // Reference equality check, return true if same reference.
1334 __ Cmp(str, arg);
1335 __ B(&return_true, eq);
1336
1337 // Instanceof check for the argument by comparing class fields.
1338 // All string objects must have the same type since String cannot be subclassed.
1339 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1340 // If the argument is a string object, its class field must be equal to receiver's class field.
1341 __ Ldr(temp, MemOperand(str.X(), class_offset));
1342 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1343 __ Cmp(temp, temp1);
1344 __ B(&return_false, ne);
1345
1346 // Load lengths of this and argument strings.
1347 __ Ldr(temp, MemOperand(str.X(), count_offset));
1348 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1349 // Check if lengths are equal, return false if they're not.
1350 __ Cmp(temp, temp1);
1351 __ B(&return_false, ne);
1352 // Store offset of string value in preparation for comparison loop
1353 __ Mov(temp1, value_offset);
1354 // Return true if both strings are empty.
1355 __ Cbz(temp, &return_true);
1356
1357 // Assertions that must hold in order to compare strings 4 characters at a time.
1358 DCHECK_ALIGNED(value_offset, 8);
1359 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1360
1361 temp1 = temp1.X();
1362 temp2 = temp2.X();
1363
1364 // Loop to compare strings 4 characters at a time starting at the beginning of the string.
1365 // Ok to do this because strings are zero-padded to be 8-byte aligned.
1366 __ Bind(&loop);
1367 __ Ldr(out, MemOperand(str.X(), temp1));
1368 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1369 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1370 __ Cmp(out, temp2);
1371 __ B(&return_false, ne);
1372 __ Sub(temp, temp, Operand(4), SetFlags);
1373 __ B(&loop, gt);
1374
1375 // Return true and exit the function.
1376 // If loop does not result in returning false, we return true.
1377 __ Bind(&return_true);
1378 __ Mov(out, 1);
1379 __ B(&end);
1380
1381 // Return false and exit the function.
1382 __ Bind(&return_false);
1383 __ Mov(out, 0);
1384 __ Bind(&end);
1385}
1386
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001387static void GenerateVisitStringIndexOf(HInvoke* invoke,
1388 vixl::MacroAssembler* masm,
1389 CodeGeneratorARM64* codegen,
1390 ArenaAllocator* allocator,
1391 bool start_at_zero) {
1392 LocationSummary* locations = invoke->GetLocations();
1393 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
1394
1395 // Note that the null check must have been done earlier.
1396 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1397
1398 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1399 // or directly dispatch if we have a constant.
1400 SlowPathCodeARM64* slow_path = nullptr;
1401 if (invoke->InputAt(1)->IsIntConstant()) {
1402 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) > 0xFFFFU) {
1403 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1404 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1405 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1406 codegen->AddSlowPath(slow_path);
1407 __ B(slow_path->GetEntryLabel());
1408 __ Bind(slow_path->GetExitLabel());
1409 return;
1410 }
1411 } else {
1412 Register char_reg = WRegisterFrom(locations->InAt(1));
1413 __ Mov(tmp_reg, 0xFFFF);
1414 __ Cmp(char_reg, Operand(tmp_reg));
1415 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1416 codegen->AddSlowPath(slow_path);
1417 __ B(hi, slow_path->GetEntryLabel());
1418 }
1419
1420 if (start_at_zero) {
1421 // Start-index = 0.
1422 __ Mov(tmp_reg, 0);
1423 }
1424
1425 __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pIndexOf).Int32Value()));
Roland Levillain42ad2882016-02-29 18:26:54 +00001426 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001427 __ Blr(lr);
1428
1429 if (slow_path != nullptr) {
1430 __ Bind(slow_path->GetExitLabel());
1431 }
1432}
1433
1434void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1435 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1436 LocationSummary::kCall,
1437 kIntrinsified);
1438 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1439 // best to align the inputs accordingly.
1440 InvokeRuntimeCallingConvention calling_convention;
1441 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1442 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1443 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1444
1445 // Need a temp for slow-path codepoint compare, and need to send start_index=0.
1446 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1447}
1448
1449void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001450 GenerateVisitStringIndexOf(
1451 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001452}
1453
1454void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1455 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1456 LocationSummary::kCall,
1457 kIntrinsified);
1458 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1459 // best to align the inputs accordingly.
1460 InvokeRuntimeCallingConvention calling_convention;
1461 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1462 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1463 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1464 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1465
1466 // Need a temp for slow-path codepoint compare.
1467 locations->AddTemp(Location::RequiresRegister());
1468}
1469
1470void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001471 GenerateVisitStringIndexOf(
1472 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001473}
1474
Jeff Hao848f70a2014-01-15 13:49:50 -08001475void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1476 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1477 LocationSummary::kCall,
1478 kIntrinsified);
1479 InvokeRuntimeCallingConvention calling_convention;
1480 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1481 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1482 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1483 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1484 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1485}
1486
1487void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1488 vixl::MacroAssembler* masm = GetVIXLAssembler();
1489 LocationSummary* locations = invoke->GetLocations();
1490
1491 Register byte_array = WRegisterFrom(locations->InAt(0));
1492 __ Cmp(byte_array, 0);
1493 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1494 codegen_->AddSlowPath(slow_path);
1495 __ B(eq, slow_path->GetEntryLabel());
1496
1497 __ Ldr(lr,
1498 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromBytes).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001499 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001500 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001501 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001502 __ Bind(slow_path->GetExitLabel());
1503}
1504
1505void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1506 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1507 LocationSummary::kCall,
1508 kIntrinsified);
1509 InvokeRuntimeCallingConvention calling_convention;
1510 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1511 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1512 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1513 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1514}
1515
1516void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1517 vixl::MacroAssembler* masm = GetVIXLAssembler();
1518
Roland Levillaincc3839c2016-02-29 16:23:48 +00001519 // No need to emit code checking whether `locations->InAt(2)` is a null
1520 // pointer, as callers of the native method
1521 //
1522 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1523 //
1524 // all include a null check on `data` before calling that method.
Jeff Hao848f70a2014-01-15 13:49:50 -08001525 __ Ldr(lr,
1526 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromChars).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001527 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001528 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001529 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001530}
1531
1532void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001533 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1534 LocationSummary::kCall,
1535 kIntrinsified);
1536 InvokeRuntimeCallingConvention calling_convention;
1537 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Jeff Hao848f70a2014-01-15 13:49:50 -08001538 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1539}
1540
1541void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
1542 vixl::MacroAssembler* masm = GetVIXLAssembler();
1543 LocationSummary* locations = invoke->GetLocations();
1544
1545 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1546 __ Cmp(string_to_copy, 0);
1547 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1548 codegen_->AddSlowPath(slow_path);
1549 __ B(eq, slow_path->GetEntryLabel());
1550
1551 __ Ldr(lr,
1552 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromString).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001553 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001554 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001555 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001556 __ Bind(slow_path->GetExitLabel());
1557}
1558
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001559static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1560 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
1561 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1562 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1563
1564 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1565 LocationSummary::kCall,
1566 kIntrinsified);
1567 InvokeRuntimeCallingConvention calling_convention;
1568
1569 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1570 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1571}
1572
1573static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1574 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
1575 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1576 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1577 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1578
1579 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1580 LocationSummary::kCall,
1581 kIntrinsified);
1582 InvokeRuntimeCallingConvention calling_convention;
1583
1584 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1585 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1586 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1587}
1588
1589static void GenFPToFPCall(HInvoke* invoke,
1590 vixl::MacroAssembler* masm,
1591 CodeGeneratorARM64* codegen,
1592 QuickEntrypointEnum entry) {
1593 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64WordSize>(entry).Int32Value()));
1594 __ Blr(lr);
1595 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
1596}
1597
1598void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
1599 CreateFPToFPCallLocations(arena_, invoke);
1600}
1601
1602void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
1603 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCos);
1604}
1605
1606void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
1607 CreateFPToFPCallLocations(arena_, invoke);
1608}
1609
1610void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
1611 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSin);
1612}
1613
1614void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
1615 CreateFPToFPCallLocations(arena_, invoke);
1616}
1617
1618void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
1619 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAcos);
1620}
1621
1622void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
1623 CreateFPToFPCallLocations(arena_, invoke);
1624}
1625
1626void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
1627 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAsin);
1628}
1629
1630void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
1631 CreateFPToFPCallLocations(arena_, invoke);
1632}
1633
1634void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
1635 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan);
1636}
1637
1638void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
1639 CreateFPToFPCallLocations(arena_, invoke);
1640}
1641
1642void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
1643 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCbrt);
1644}
1645
1646void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
1647 CreateFPToFPCallLocations(arena_, invoke);
1648}
1649
1650void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
1651 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCosh);
1652}
1653
1654void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
1655 CreateFPToFPCallLocations(arena_, invoke);
1656}
1657
1658void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
1659 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExp);
1660}
1661
1662void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
1663 CreateFPToFPCallLocations(arena_, invoke);
1664}
1665
1666void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
1667 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExpm1);
1668}
1669
1670void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
1671 CreateFPToFPCallLocations(arena_, invoke);
1672}
1673
1674void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
1675 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog);
1676}
1677
1678void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
1679 CreateFPToFPCallLocations(arena_, invoke);
1680}
1681
1682void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
1683 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog10);
1684}
1685
1686void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
1687 CreateFPToFPCallLocations(arena_, invoke);
1688}
1689
1690void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
1691 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSinh);
1692}
1693
1694void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
1695 CreateFPToFPCallLocations(arena_, invoke);
1696}
1697
1698void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
1699 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTan);
1700}
1701
1702void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
1703 CreateFPToFPCallLocations(arena_, invoke);
1704}
1705
1706void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
1707 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTanh);
1708}
1709
1710void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
1711 CreateFPFPToFPCallLocations(arena_, invoke);
1712}
1713
1714void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
1715 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan2);
1716}
1717
1718void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
1719 CreateFPFPToFPCallLocations(arena_, invoke);
1720}
1721
1722void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
1723 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickHypot);
1724}
1725
1726void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
1727 CreateFPFPToFPCallLocations(arena_, invoke);
1728}
1729
1730void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
1731 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickNextAfter);
1732}
1733
Tim Zhang25abd6c2016-01-19 23:39:24 +08001734void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1735 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1736 LocationSummary::kNoCall,
1737 kIntrinsified);
1738 locations->SetInAt(0, Location::RequiresRegister());
1739 locations->SetInAt(1, Location::RequiresRegister());
1740 locations->SetInAt(2, Location::RequiresRegister());
1741 locations->SetInAt(3, Location::RequiresRegister());
1742 locations->SetInAt(4, Location::RequiresRegister());
1743
1744 locations->AddTemp(Location::RequiresRegister());
1745 locations->AddTemp(Location::RequiresRegister());
1746}
1747
1748void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1749 vixl::MacroAssembler* masm = GetVIXLAssembler();
1750 LocationSummary* locations = invoke->GetLocations();
1751
1752 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1753 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1754 DCHECK_EQ(char_size, 2u);
1755
1756 // Location of data in char array buffer.
1757 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1758
1759 // Location of char array data in string.
1760 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1761
1762 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1763 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
1764 Register srcObj = XRegisterFrom(locations->InAt(0));
1765 Register srcBegin = XRegisterFrom(locations->InAt(1));
1766 Register srcEnd = XRegisterFrom(locations->InAt(2));
1767 Register dstObj = XRegisterFrom(locations->InAt(3));
1768 Register dstBegin = XRegisterFrom(locations->InAt(4));
1769
1770 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
1771 Register src_ptr_end = XRegisterFrom(locations->GetTemp(1));
1772
1773 UseScratchRegisterScope temps(masm);
1774 Register dst_ptr = temps.AcquireX();
1775 Register tmp = temps.AcquireW();
1776
1777 // src range to copy.
1778 __ Add(src_ptr, srcObj, Operand(value_offset));
1779 __ Add(src_ptr_end, src_ptr, Operand(srcEnd, LSL, 1));
1780 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
1781
1782 // dst to be copied.
1783 __ Add(dst_ptr, dstObj, Operand(data_offset));
1784 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
1785
1786 // Do the copy.
1787 vixl::Label loop, done;
1788 __ Bind(&loop);
1789 __ Cmp(src_ptr, src_ptr_end);
1790 __ B(&done, eq);
1791 __ Ldrh(tmp, MemOperand(src_ptr, char_size, vixl::PostIndex));
1792 __ Strh(tmp, MemOperand(dst_ptr, char_size, vixl::PostIndex));
1793 __ B(&loop);
1794 __ Bind(&done);
1795}
1796
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001797// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
1798// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001799static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001800
1801static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
1802 uint32_t at,
1803 HInstruction* input) {
1804 HIntConstant* const_input = input->AsIntConstant();
1805 if (const_input != nullptr && !vixl::Assembler::IsImmAddSub(const_input->GetValue())) {
1806 locations->SetInAt(at, Location::RequiresRegister());
1807 } else {
1808 locations->SetInAt(at, Location::RegisterOrConstant(input));
1809 }
1810}
1811
1812void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1813 // Check to see if we have known failures that will cause us to have to bail out
1814 // to the runtime, and just generate the runtime call directly.
1815 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1816 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
1817
1818 // The positions must be non-negative.
1819 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1820 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
1821 // We will have to fail anyways.
1822 return;
1823 }
1824
1825 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
1826 // native implementation.
1827 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1828 if (length != nullptr) {
1829 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08001830 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001831 // Just call as normal.
1832 return;
1833 }
1834 }
1835
1836 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1837 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1838 LocationSummary::kCallOnSlowPath,
1839 kIntrinsified);
1840 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
1841 locations->SetInAt(0, Location::RequiresRegister());
1842 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
1843 locations->SetInAt(2, Location::RequiresRegister());
1844 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
1845 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
1846
1847 locations->AddTemp(Location::RequiresRegister());
1848 locations->AddTemp(Location::RequiresRegister());
1849 locations->AddTemp(Location::RequiresRegister());
1850}
1851
1852static void CheckSystemArrayCopyPosition(vixl::MacroAssembler* masm,
1853 const Location& pos,
1854 const Register& input,
1855 const Location& length,
1856 SlowPathCodeARM64* slow_path,
1857 const Register& input_len,
1858 const Register& temp,
1859 bool length_is_input_length = false) {
1860 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
1861 if (pos.IsConstant()) {
1862 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1863 if (pos_const == 0) {
1864 if (!length_is_input_length) {
1865 // Check that length(input) >= length.
1866 __ Ldr(temp, MemOperand(input, length_offset));
1867 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1868 __ B(slow_path->GetEntryLabel(), lt);
1869 }
1870 } else {
1871 // Check that length(input) >= pos.
1872 __ Ldr(input_len, MemOperand(input, length_offset));
1873 __ Subs(temp, input_len, pos_const);
1874 __ B(slow_path->GetEntryLabel(), lt);
1875
1876 // Check that (length(input) - pos) >= length.
1877 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1878 __ B(slow_path->GetEntryLabel(), lt);
1879 }
1880 } else if (length_is_input_length) {
1881 // The only way the copy can succeed is if pos is zero.
1882 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
1883 } else {
1884 // Check that pos >= 0.
1885 Register pos_reg = WRegisterFrom(pos);
1886 __ Tbnz(pos_reg, pos_reg.size() - 1, slow_path->GetEntryLabel());
1887
1888 // Check that pos <= length(input) && (length(input) - pos) >= length.
1889 __ Ldr(temp, MemOperand(input, length_offset));
1890 __ Subs(temp, temp, pos_reg);
1891 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
1892 __ Ccmp(temp, OperandFrom(length, Primitive::kPrimInt), NFlag, ge);
1893 __ B(slow_path->GetEntryLabel(), lt);
1894 }
1895}
1896
1897// Compute base source address, base destination address, and end source address
1898// for System.arraycopy* intrinsics.
1899static void GenSystemArrayCopyAddresses(vixl::MacroAssembler* masm,
1900 Primitive::Type type,
1901 const Register& src,
1902 const Location& src_pos,
1903 const Register& dst,
1904 const Location& dst_pos,
1905 const Location& copy_length,
1906 const Register& src_base,
1907 const Register& dst_base,
1908 const Register& src_end) {
1909 DCHECK(type == Primitive::kPrimNot || type == Primitive::kPrimChar)
Roland Levillainebea3d22016-04-12 15:42:57 +01001910 << "Unexpected element type: " << type;
1911 const int32_t element_size = Primitive::ComponentSize(type);
1912 const int32_t element_size_shift = Primitive::ComponentSizeShift(type);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001913
Roland Levillainebea3d22016-04-12 15:42:57 +01001914 uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001915 if (src_pos.IsConstant()) {
1916 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001917 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001918 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001919 __ Add(src_base, src, data_offset);
1920 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001921 }
1922
1923 if (dst_pos.IsConstant()) {
1924 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001925 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001926 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001927 __ Add(dst_base, dst, data_offset);
1928 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001929 }
1930
1931 if (copy_length.IsConstant()) {
1932 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001933 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001934 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001935 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001936 }
1937}
1938
1939void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1940 vixl::MacroAssembler* masm = GetVIXLAssembler();
1941 LocationSummary* locations = invoke->GetLocations();
1942 Register src = XRegisterFrom(locations->InAt(0));
1943 Location src_pos = locations->InAt(1);
1944 Register dst = XRegisterFrom(locations->InAt(2));
1945 Location dst_pos = locations->InAt(3);
1946 Location length = locations->InAt(4);
1947
1948 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1949 codegen_->AddSlowPath(slow_path);
1950
1951 // If source and destination are the same, take the slow path. Overlapping copy regions must be
1952 // copied in reverse and we can't know in all cases if it's needed.
1953 __ Cmp(src, dst);
1954 __ B(slow_path->GetEntryLabel(), eq);
1955
1956 // Bail out if the source is null.
1957 __ Cbz(src, slow_path->GetEntryLabel());
1958
1959 // Bail out if the destination is null.
1960 __ Cbz(dst, slow_path->GetEntryLabel());
1961
1962 if (!length.IsConstant()) {
1963 // If the length is negative, bail out.
1964 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, slow_path->GetEntryLabel());
1965 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001966 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001967 __ B(slow_path->GetEntryLabel(), gt);
1968 } else {
1969 // We have already checked in the LocationsBuilder for the constant case.
1970 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
1971 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
1972 }
1973
1974 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
1975 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
1976 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
1977
1978 CheckSystemArrayCopyPosition(masm,
1979 src_pos,
1980 src,
1981 length,
1982 slow_path,
1983 src_curr_addr,
1984 dst_curr_addr,
1985 false);
1986
1987 CheckSystemArrayCopyPosition(masm,
1988 dst_pos,
1989 dst,
1990 length,
1991 slow_path,
1992 src_curr_addr,
1993 dst_curr_addr,
1994 false);
1995
1996 src_curr_addr = src_curr_addr.X();
1997 dst_curr_addr = dst_curr_addr.X();
1998 src_stop_addr = src_stop_addr.X();
1999
2000 GenSystemArrayCopyAddresses(masm,
2001 Primitive::kPrimChar,
2002 src,
2003 src_pos,
2004 dst,
2005 dst_pos,
2006 length,
2007 src_curr_addr,
2008 dst_curr_addr,
2009 src_stop_addr);
2010
2011 // Iterate over the arrays and do a raw copy of the chars.
2012 const int32_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2013 UseScratchRegisterScope temps(masm);
2014 Register tmp = temps.AcquireW();
2015 vixl::Label loop, done;
2016 __ Bind(&loop);
2017 __ Cmp(src_curr_addr, src_stop_addr);
2018 __ B(&done, eq);
2019 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, vixl::PostIndex));
2020 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, vixl::PostIndex));
2021 __ B(&loop);
2022 __ Bind(&done);
2023
2024 __ Bind(slow_path->GetExitLabel());
2025}
2026
donghui.baic2ec9ad2016-03-10 14:02:55 +08002027// We can choose to use the native implementation there for longer copy lengths.
2028static constexpr int32_t kSystemArrayCopyThreshold = 128;
2029
2030// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2031// We want to use two temporary registers in order to reduce the register pressure in arm64.
2032// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2033void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
2034 // Check to see if we have known failures that will cause us to have to bail out
2035 // to the runtime, and just generate the runtime call directly.
2036 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2037 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2038
2039 // The positions must be non-negative.
2040 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2041 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2042 // We will have to fail anyways.
2043 return;
2044 }
2045
2046 // The length must be >= 0.
2047 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2048 if (length != nullptr) {
2049 int32_t len = length->GetValue();
2050 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2051 // Just call as normal.
2052 return;
2053 }
2054 }
2055
2056 SystemArrayCopyOptimizations optimizations(invoke);
2057
2058 if (optimizations.GetDestinationIsSource()) {
2059 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2060 // We only support backward copying if source and destination are the same.
2061 return;
2062 }
2063 }
2064
2065 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2066 // We currently don't intrinsify primitive copying.
2067 return;
2068 }
2069
2070 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2071 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2072 LocationSummary::kCallOnSlowPath,
2073 kIntrinsified);
2074 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2075 locations->SetInAt(0, Location::RequiresRegister());
2076 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2077 locations->SetInAt(2, Location::RequiresRegister());
2078 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2079 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2080
2081 locations->AddTemp(Location::RequiresRegister());
2082 locations->AddTemp(Location::RequiresRegister());
2083}
2084
2085void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
2086 vixl::MacroAssembler* masm = GetVIXLAssembler();
2087 LocationSummary* locations = invoke->GetLocations();
2088
2089 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2090 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2091 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2092 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2093
2094 Register src = XRegisterFrom(locations->InAt(0));
2095 Location src_pos = locations->InAt(1);
2096 Register dest = XRegisterFrom(locations->InAt(2));
2097 Location dest_pos = locations->InAt(3);
2098 Location length = locations->InAt(4);
2099 Register temp1 = WRegisterFrom(locations->GetTemp(0));
2100 Register temp2 = WRegisterFrom(locations->GetTemp(1));
2101
2102 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2103 codegen_->AddSlowPath(slow_path);
2104
2105 vixl::Label conditions_on_positions_validated;
2106 SystemArrayCopyOptimizations optimizations(invoke);
2107
2108 if (!optimizations.GetDestinationIsSource() &&
2109 (!src_pos.IsConstant() || !dest_pos.IsConstant())) {
2110 __ Cmp(src, dest);
2111 }
2112 // If source and destination are the same, we go to slow path if we need to do
2113 // forward copying.
2114 if (src_pos.IsConstant()) {
2115 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2116 if (dest_pos.IsConstant()) {
2117 // Checked when building locations.
2118 DCHECK(!optimizations.GetDestinationIsSource()
2119 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2120 } else {
2121 if (!optimizations.GetDestinationIsSource()) {
2122 __ B(&conditions_on_positions_validated, ne);
2123 }
2124 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
2125 __ B(slow_path->GetEntryLabel(), gt);
2126 }
2127 } else {
2128 if (!optimizations.GetDestinationIsSource()) {
2129 __ B(&conditions_on_positions_validated, ne);
2130 }
2131 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2132 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
2133 __ B(slow_path->GetEntryLabel(), lt);
2134 }
2135
2136 __ Bind(&conditions_on_positions_validated);
2137
2138 if (!optimizations.GetSourceIsNotNull()) {
2139 // Bail out if the source is null.
2140 __ Cbz(src, slow_path->GetEntryLabel());
2141 }
2142
2143 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2144 // Bail out if the destination is null.
2145 __ Cbz(dest, slow_path->GetEntryLabel());
2146 }
2147
2148 // We have already checked in the LocationsBuilder for the constant case.
2149 if (!length.IsConstant() &&
2150 !optimizations.GetCountIsSourceLength() &&
2151 !optimizations.GetCountIsDestinationLength()) {
2152 // If the length is negative, bail out.
2153 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, slow_path->GetEntryLabel());
2154 // If the length >= 128 then (currently) prefer native implementation.
2155 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
2156 __ B(slow_path->GetEntryLabel(), ge);
2157 }
2158 // Validity checks: source.
2159 CheckSystemArrayCopyPosition(masm,
2160 src_pos,
2161 src,
2162 length,
2163 slow_path,
2164 temp1,
2165 temp2,
2166 optimizations.GetCountIsSourceLength());
2167
2168 // Validity checks: dest.
2169 CheckSystemArrayCopyPosition(masm,
2170 dest_pos,
2171 dest,
2172 length,
2173 slow_path,
2174 temp1,
2175 temp2,
2176 optimizations.GetCountIsDestinationLength());
2177 {
2178 // We use a block to end the scratch scope before the write barrier, thus
2179 // freeing the temporary registers so they can be used in `MarkGCCard`.
2180 UseScratchRegisterScope temps(masm);
2181 Register temp3 = temps.AcquireW();
2182 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2183 // Check whether all elements of the source array are assignable to the component
2184 // type of the destination array. We do two checks: the classes are the same,
2185 // or the destination is Object[]. If none of these checks succeed, we go to the
2186 // slow path.
2187 __ Ldr(temp1, MemOperand(dest, class_offset));
2188 __ Ldr(temp2, MemOperand(src, class_offset));
2189 bool did_unpoison = false;
2190 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2191 !optimizations.GetSourceIsNonPrimitiveArray()) {
2192 // One or two of the references need to be unpoisoned. Unpoison them
2193 // both to make the identity check valid.
2194 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2195 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2196 did_unpoison = true;
2197 }
2198
2199 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2200 // Bail out if the destination is not a non primitive array.
2201 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2202 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2203 __ Cbz(temp3, slow_path->GetEntryLabel());
2204 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2205 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2206 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2207 __ Cbnz(temp3, slow_path->GetEntryLabel());
2208 }
2209
2210 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2211 // Bail out if the source is not a non primitive array.
2212 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2213 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2214 __ Cbz(temp3, slow_path->GetEntryLabel());
2215 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2216 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2217 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2218 __ Cbnz(temp3, slow_path->GetEntryLabel());
2219 }
2220
2221 __ Cmp(temp1, temp2);
2222
2223 if (optimizations.GetDestinationIsTypedObjectArray()) {
2224 vixl::Label do_copy;
2225 __ B(&do_copy, eq);
2226 if (!did_unpoison) {
2227 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2228 }
2229 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2230 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2231 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2232 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2233 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2234 // No need to unpoison the result, we're comparing against null.
2235 __ Cbnz(temp1, slow_path->GetEntryLabel());
2236 __ Bind(&do_copy);
2237 } else {
2238 __ B(slow_path->GetEntryLabel(), ne);
2239 }
2240 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2241 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2242 // Bail out if the source is not a non primitive array.
2243 // /* HeapReference<Class> */ temp1 = src->klass_
2244 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2245 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2246 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2247 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2248 __ Cbz(temp3, slow_path->GetEntryLabel());
2249 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2250 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2251 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2252 __ Cbnz(temp3, slow_path->GetEntryLabel());
2253 }
2254
2255 Register src_curr_addr = temp1.X();
2256 Register dst_curr_addr = temp2.X();
2257 Register src_stop_addr = temp3.X();
2258
2259 GenSystemArrayCopyAddresses(masm,
2260 Primitive::kPrimNot,
2261 src,
2262 src_pos,
2263 dest,
2264 dest_pos,
2265 length,
2266 src_curr_addr,
2267 dst_curr_addr,
2268 src_stop_addr);
2269
2270 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2271 // poison/unpoison, nor do any read barrier as the next uses of the destination
2272 // array will do it.
2273 vixl::Label loop, done;
2274 const int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
2275 __ Bind(&loop);
2276 __ Cmp(src_curr_addr, src_stop_addr);
2277 __ B(&done, eq);
2278 {
2279 Register tmp = temps.AcquireW();
2280 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, vixl::PostIndex));
2281 __ Str(tmp, MemOperand(dst_curr_addr, element_size, vixl::PostIndex));
2282 }
2283 __ B(&loop);
2284 __ Bind(&done);
2285 }
2286 // We only need one card marking on the destination array.
2287 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2288
2289 __ Bind(slow_path->GetExitLabel());
2290}
2291
Aart Bik2f9fcc92016-03-01 15:16:54 -08002292UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
2293UNIMPLEMENTED_INTRINSIC(ARM64, FloatIsInfinite)
2294UNIMPLEMENTED_INTRINSIC(ARM64, DoubleIsInfinite)
2295UNIMPLEMENTED_INTRINSIC(ARM64, IntegerHighestOneBit)
2296UNIMPLEMENTED_INTRINSIC(ARM64, LongHighestOneBit)
2297UNIMPLEMENTED_INTRINSIC(ARM64, IntegerLowestOneBit)
2298UNIMPLEMENTED_INTRINSIC(ARM64, LongLowestOneBit)
Andreas Gampe878d58c2015-01-15 23:24:00 -08002299
Aart Bik0e54c012016-03-04 12:08:31 -08002300// 1.8.
2301UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
2302UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
2303UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
2304UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
2305UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002306
Aart Bik2f9fcc92016-03-01 15:16:54 -08002307UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01002308
2309#undef __
2310
Andreas Gampe878d58c2015-01-15 23:24:00 -08002311} // namespace arm64
2312} // namespace art