blob: 549407e1e7f63076eeb31a980dad20287e2b8fd5 [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 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000152 return res->Intrinsified();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800153}
154
155#define __ masm->
156
157static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
158 LocationSummary* locations = new (arena) LocationSummary(invoke,
159 LocationSummary::kNoCall,
160 kIntrinsified);
161 locations->SetInAt(0, Location::RequiresFpuRegister());
162 locations->SetOut(Location::RequiresRegister());
163}
164
165static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
166 LocationSummary* locations = new (arena) LocationSummary(invoke,
167 LocationSummary::kNoCall,
168 kIntrinsified);
169 locations->SetInAt(0, Location::RequiresRegister());
170 locations->SetOut(Location::RequiresFpuRegister());
171}
172
173static void MoveFPToInt(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
174 Location input = locations->InAt(0);
175 Location output = locations->Out();
176 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
177 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
178}
179
180static void MoveIntToFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
181 Location input = locations->InAt(0);
182 Location output = locations->Out();
183 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
184 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
185}
186
187void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
188 CreateFPToIntLocations(arena_, invoke);
189}
190void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
191 CreateIntToFPLocations(arena_, invoke);
192}
193
194void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000195 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800196}
197void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000198 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800199}
200
201void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
202 CreateFPToIntLocations(arena_, invoke);
203}
204void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
205 CreateIntToFPLocations(arena_, invoke);
206}
207
208void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000209 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800210}
211void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000212 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800213}
214
215static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
216 LocationSummary* locations = new (arena) LocationSummary(invoke,
217 LocationSummary::kNoCall,
218 kIntrinsified);
219 locations->SetInAt(0, Location::RequiresRegister());
220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
221}
222
223static void GenReverseBytes(LocationSummary* locations,
224 Primitive::Type type,
225 vixl::MacroAssembler* masm) {
226 Location in = locations->InAt(0);
227 Location out = locations->Out();
228
229 switch (type) {
230 case Primitive::kPrimShort:
231 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
232 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
233 break;
234 case Primitive::kPrimInt:
235 case Primitive::kPrimLong:
236 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
237 break;
238 default:
239 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
240 UNREACHABLE();
241 }
242}
243
244void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
245 CreateIntToIntLocations(arena_, invoke);
246}
247
248void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
249 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
250}
251
252void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
253 CreateIntToIntLocations(arena_, invoke);
254}
255
256void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
257 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
258}
259
260void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
261 CreateIntToIntLocations(arena_, invoke);
262}
263
264void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
265 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetVIXLAssembler());
266}
267
Aart Bik7b565022016-01-28 14:36:22 -0800268static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
269 LocationSummary* locations = new (arena) LocationSummary(invoke,
270 LocationSummary::kNoCall,
271 kIntrinsified);
272 locations->SetInAt(0, Location::RequiresRegister());
273 locations->SetInAt(1, Location::RequiresRegister());
274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
275}
276
Scott Wakeling611d3392015-07-10 11:42:06 +0100277static void GenNumberOfLeadingZeros(LocationSummary* locations,
278 Primitive::Type type,
279 vixl::MacroAssembler* masm) {
280 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
281
282 Location in = locations->InAt(0);
283 Location out = locations->Out();
284
285 __ Clz(RegisterFrom(out, type), RegisterFrom(in, type));
286}
287
288void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
289 CreateIntToIntLocations(arena_, invoke);
290}
291
292void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
293 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
294}
295
296void IntrinsicLocationsBuilderARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
297 CreateIntToIntLocations(arena_, invoke);
298}
299
300void IntrinsicCodeGeneratorARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
301 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
302}
303
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100304static void GenNumberOfTrailingZeros(LocationSummary* locations,
305 Primitive::Type type,
306 vixl::MacroAssembler* masm) {
307 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
308
309 Location in = locations->InAt(0);
310 Location out = locations->Out();
311
312 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
313 __ Clz(RegisterFrom(out, type), RegisterFrom(out, type));
314}
315
316void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
317 CreateIntToIntLocations(arena_, invoke);
318}
319
320void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
321 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
322}
323
324void IntrinsicLocationsBuilderARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
325 CreateIntToIntLocations(arena_, invoke);
326}
327
328void IntrinsicCodeGeneratorARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
329 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
330}
331
Andreas Gampe878d58c2015-01-15 23:24:00 -0800332static void GenReverse(LocationSummary* locations,
333 Primitive::Type type,
334 vixl::MacroAssembler* masm) {
335 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
336
337 Location in = locations->InAt(0);
338 Location out = locations->Out();
339
340 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
341}
342
343void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
344 CreateIntToIntLocations(arena_, invoke);
345}
346
347void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
348 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
349}
350
351void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
352 CreateIntToIntLocations(arena_, invoke);
353}
354
355void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
356 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
357}
358
Roland Levillainfa3912e2016-04-01 18:21:55 +0100359static void GenBitCount(HInvoke* instr, Primitive::Type type, vixl::MacroAssembler* masm) {
360 DCHECK(Primitive::IsIntOrLongType(type)) << type;
361 DCHECK_EQ(instr->GetType(), Primitive::kPrimInt);
362 DCHECK_EQ(Primitive::PrimitiveKind(instr->InputAt(0)->GetType()), type);
xueliang.zhong49924c92016-03-03 10:52:51 +0000363
xueliang.zhong49924c92016-03-03 10:52:51 +0000364 UseScratchRegisterScope temps(masm);
365
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000366 Register src = InputRegisterAt(instr, 0);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100367 Register dst = RegisterFrom(instr->GetLocations()->Out(), type);
368 FPRegister fpr = (type == Primitive::kPrimLong) ? temps.AcquireD() : temps.AcquireS();
xueliang.zhong49924c92016-03-03 10:52:51 +0000369
370 __ Fmov(fpr, src);
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000371 __ Cnt(fpr.V8B(), fpr.V8B());
372 __ Addv(fpr.B(), fpr.V8B());
xueliang.zhong49924c92016-03-03 10:52:51 +0000373 __ Fmov(dst, fpr);
374}
375
376void IntrinsicLocationsBuilderARM64::VisitLongBitCount(HInvoke* invoke) {
377 CreateIntToIntLocations(arena_, invoke);
378}
379
380void IntrinsicCodeGeneratorARM64::VisitLongBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100381 GenBitCount(invoke, Primitive::kPrimLong, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000382}
383
384void IntrinsicLocationsBuilderARM64::VisitIntegerBitCount(HInvoke* invoke) {
385 CreateIntToIntLocations(arena_, invoke);
386}
387
388void IntrinsicCodeGeneratorARM64::VisitIntegerBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100389 GenBitCount(invoke, Primitive::kPrimInt, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000390}
391
Andreas Gampe878d58c2015-01-15 23:24:00 -0800392static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800393 LocationSummary* locations = new (arena) LocationSummary(invoke,
394 LocationSummary::kNoCall,
395 kIntrinsified);
396 locations->SetInAt(0, Location::RequiresFpuRegister());
397 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
398}
399
400static void MathAbsFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
401 Location in = locations->InAt(0);
402 Location out = locations->Out();
403
404 FPRegister in_reg = is64bit ? DRegisterFrom(in) : SRegisterFrom(in);
405 FPRegister out_reg = is64bit ? DRegisterFrom(out) : SRegisterFrom(out);
406
407 __ Fabs(out_reg, in_reg);
408}
409
410void IntrinsicLocationsBuilderARM64::VisitMathAbsDouble(HInvoke* invoke) {
411 CreateFPToFPLocations(arena_, invoke);
412}
413
414void IntrinsicCodeGeneratorARM64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000415 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800416}
417
418void IntrinsicLocationsBuilderARM64::VisitMathAbsFloat(HInvoke* invoke) {
419 CreateFPToFPLocations(arena_, invoke);
420}
421
422void IntrinsicCodeGeneratorARM64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000423 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800424}
425
426static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
427 LocationSummary* locations = new (arena) LocationSummary(invoke,
428 LocationSummary::kNoCall,
429 kIntrinsified);
430 locations->SetInAt(0, Location::RequiresRegister());
431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
432}
433
434static void GenAbsInteger(LocationSummary* locations,
435 bool is64bit,
436 vixl::MacroAssembler* masm) {
437 Location in = locations->InAt(0);
438 Location output = locations->Out();
439
440 Register in_reg = is64bit ? XRegisterFrom(in) : WRegisterFrom(in);
441 Register out_reg = is64bit ? XRegisterFrom(output) : WRegisterFrom(output);
442
443 __ Cmp(in_reg, Operand(0));
444 __ Cneg(out_reg, in_reg, lt);
445}
446
447void IntrinsicLocationsBuilderARM64::VisitMathAbsInt(HInvoke* invoke) {
448 CreateIntToInt(arena_, invoke);
449}
450
451void IntrinsicCodeGeneratorARM64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000452 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800453}
454
455void IntrinsicLocationsBuilderARM64::VisitMathAbsLong(HInvoke* invoke) {
456 CreateIntToInt(arena_, invoke);
457}
458
459void IntrinsicCodeGeneratorARM64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000460 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800461}
462
463static void GenMinMaxFP(LocationSummary* locations,
464 bool is_min,
465 bool is_double,
466 vixl::MacroAssembler* masm) {
467 Location op1 = locations->InAt(0);
468 Location op2 = locations->InAt(1);
469 Location out = locations->Out();
470
471 FPRegister op1_reg = is_double ? DRegisterFrom(op1) : SRegisterFrom(op1);
472 FPRegister op2_reg = is_double ? DRegisterFrom(op2) : SRegisterFrom(op2);
473 FPRegister out_reg = is_double ? DRegisterFrom(out) : SRegisterFrom(out);
474 if (is_min) {
475 __ Fmin(out_reg, op1_reg, op2_reg);
476 } else {
477 __ Fmax(out_reg, op1_reg, op2_reg);
478 }
479}
480
481static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
482 LocationSummary* locations = new (arena) LocationSummary(invoke,
483 LocationSummary::kNoCall,
484 kIntrinsified);
485 locations->SetInAt(0, Location::RequiresFpuRegister());
486 locations->SetInAt(1, Location::RequiresFpuRegister());
487 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
488}
489
490void IntrinsicLocationsBuilderARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
491 CreateFPFPToFPLocations(arena_, invoke);
492}
493
494void IntrinsicCodeGeneratorARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000495 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800496}
497
498void IntrinsicLocationsBuilderARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
499 CreateFPFPToFPLocations(arena_, invoke);
500}
501
502void IntrinsicCodeGeneratorARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000503 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800504}
505
506void IntrinsicLocationsBuilderARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
507 CreateFPFPToFPLocations(arena_, invoke);
508}
509
510void IntrinsicCodeGeneratorARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000511 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800512}
513
514void IntrinsicLocationsBuilderARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
515 CreateFPFPToFPLocations(arena_, invoke);
516}
517
518void IntrinsicCodeGeneratorARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000519 GenMinMaxFP(
520 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800521}
522
523static void GenMinMax(LocationSummary* locations,
524 bool is_min,
525 bool is_long,
526 vixl::MacroAssembler* masm) {
527 Location op1 = locations->InAt(0);
528 Location op2 = locations->InAt(1);
529 Location out = locations->Out();
530
531 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
532 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
533 Register out_reg = is_long ? XRegisterFrom(out) : WRegisterFrom(out);
534
535 __ Cmp(op1_reg, op2_reg);
536 __ Csel(out_reg, op1_reg, op2_reg, is_min ? lt : gt);
537}
538
Andreas Gampe878d58c2015-01-15 23:24:00 -0800539void IntrinsicLocationsBuilderARM64::VisitMathMinIntInt(HInvoke* invoke) {
540 CreateIntIntToIntLocations(arena_, invoke);
541}
542
543void IntrinsicCodeGeneratorARM64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000544 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800545}
546
547void IntrinsicLocationsBuilderARM64::VisitMathMinLongLong(HInvoke* invoke) {
548 CreateIntIntToIntLocations(arena_, invoke);
549}
550
551void IntrinsicCodeGeneratorARM64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000552 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800553}
554
555void IntrinsicLocationsBuilderARM64::VisitMathMaxIntInt(HInvoke* invoke) {
556 CreateIntIntToIntLocations(arena_, invoke);
557}
558
559void IntrinsicCodeGeneratorARM64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000560 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800561}
562
563void IntrinsicLocationsBuilderARM64::VisitMathMaxLongLong(HInvoke* invoke) {
564 CreateIntIntToIntLocations(arena_, invoke);
565}
566
567void IntrinsicCodeGeneratorARM64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000568 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800569}
570
571void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
572 CreateFPToFPLocations(arena_, invoke);
573}
574
575void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
576 LocationSummary* locations = invoke->GetLocations();
577 vixl::MacroAssembler* masm = GetVIXLAssembler();
578 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
579}
580
581void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
582 CreateFPToFPLocations(arena_, invoke);
583}
584
585void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
586 LocationSummary* locations = invoke->GetLocations();
587 vixl::MacroAssembler* masm = GetVIXLAssembler();
588 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
589}
590
591void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
592 CreateFPToFPLocations(arena_, invoke);
593}
594
595void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
596 LocationSummary* locations = invoke->GetLocations();
597 vixl::MacroAssembler* masm = GetVIXLAssembler();
598 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
599}
600
601void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
602 CreateFPToFPLocations(arena_, invoke);
603}
604
605void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
606 LocationSummary* locations = invoke->GetLocations();
607 vixl::MacroAssembler* masm = GetVIXLAssembler();
608 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
609}
610
611static void CreateFPToIntPlusTempLocations(ArenaAllocator* arena, HInvoke* invoke) {
612 LocationSummary* locations = new (arena) LocationSummary(invoke,
613 LocationSummary::kNoCall,
614 kIntrinsified);
615 locations->SetInAt(0, Location::RequiresFpuRegister());
616 locations->SetOut(Location::RequiresRegister());
617}
618
619static void GenMathRound(LocationSummary* locations,
620 bool is_double,
621 vixl::MacroAssembler* masm) {
622 FPRegister in_reg = is_double ?
623 DRegisterFrom(locations->InAt(0)) : SRegisterFrom(locations->InAt(0));
624 Register out_reg = is_double ?
625 XRegisterFrom(locations->Out()) : WRegisterFrom(locations->Out());
626 UseScratchRegisterScope temps(masm);
627 FPRegister temp1_reg = temps.AcquireSameSizeAs(in_reg);
628
629 // 0.5 can be encoded as an immediate, so use fmov.
630 if (is_double) {
631 __ Fmov(temp1_reg, static_cast<double>(0.5));
632 } else {
633 __ Fmov(temp1_reg, static_cast<float>(0.5));
634 }
635 __ Fadd(temp1_reg, in_reg, temp1_reg);
636 __ Fcvtms(out_reg, temp1_reg);
637}
638
639void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800640 // See intrinsics.h.
641 if (kRoundIsPlusPointFive) {
642 CreateFPToIntPlusTempLocations(arena_, invoke);
643 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800644}
645
646void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000647 GenMathRound(invoke->GetLocations(), /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800648}
649
650void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800651 // See intrinsics.h.
652 if (kRoundIsPlusPointFive) {
653 CreateFPToIntPlusTempLocations(arena_, invoke);
654 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800655}
656
657void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000658 GenMathRound(invoke->GetLocations(), /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800659}
660
661void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
662 CreateIntToIntLocations(arena_, invoke);
663}
664
665void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
666 vixl::MacroAssembler* masm = GetVIXLAssembler();
667 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
668 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
669}
670
671void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
672 CreateIntToIntLocations(arena_, invoke);
673}
674
675void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
676 vixl::MacroAssembler* masm = GetVIXLAssembler();
677 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
678 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
679}
680
681void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
682 CreateIntToIntLocations(arena_, invoke);
683}
684
685void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
686 vixl::MacroAssembler* masm = GetVIXLAssembler();
687 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
688 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
689}
690
691void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
692 CreateIntToIntLocations(arena_, invoke);
693}
694
695void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
696 vixl::MacroAssembler* masm = GetVIXLAssembler();
697 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
698 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
699}
700
701static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
702 LocationSummary* locations = new (arena) LocationSummary(invoke,
703 LocationSummary::kNoCall,
704 kIntrinsified);
705 locations->SetInAt(0, Location::RequiresRegister());
706 locations->SetInAt(1, Location::RequiresRegister());
707}
708
709void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
710 CreateIntIntToVoidLocations(arena_, invoke);
711}
712
713void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
714 vixl::MacroAssembler* masm = GetVIXLAssembler();
715 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
716 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
717}
718
719void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
720 CreateIntIntToVoidLocations(arena_, invoke);
721}
722
723void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
724 vixl::MacroAssembler* masm = GetVIXLAssembler();
725 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
726 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
727}
728
729void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
730 CreateIntIntToVoidLocations(arena_, invoke);
731}
732
733void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
734 vixl::MacroAssembler* masm = GetVIXLAssembler();
735 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
736 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
737}
738
739void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
740 CreateIntIntToVoidLocations(arena_, invoke);
741}
742
743void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
744 vixl::MacroAssembler* masm = GetVIXLAssembler();
745 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
746 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
747}
748
749void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
750 LocationSummary* locations = new (arena_) LocationSummary(invoke,
751 LocationSummary::kNoCall,
752 kIntrinsified);
753 locations->SetOut(Location::RequiresRegister());
754}
755
756void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
757 codegen_->Load(Primitive::kPrimNot, WRegisterFrom(invoke->GetLocations()->Out()),
758 MemOperand(tr, Thread::PeerOffset<8>().Int32Value()));
759}
760
761static void GenUnsafeGet(HInvoke* invoke,
762 Primitive::Type type,
763 bool is_volatile,
764 CodeGeneratorARM64* codegen) {
765 LocationSummary* locations = invoke->GetLocations();
766 DCHECK((type == Primitive::kPrimInt) ||
767 (type == Primitive::kPrimLong) ||
768 (type == Primitive::kPrimNot));
769 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000770 Location base_loc = locations->InAt(1);
771 Register base = WRegisterFrom(base_loc); // Object pointer.
772 Location offset_loc = locations->InAt(2);
773 Register offset = XRegisterFrom(offset_loc); // Long offset.
774 Location trg_loc = locations->Out();
775 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800776
Roland Levillain44015862016-01-22 11:47:17 +0000777 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
778 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
779 UseScratchRegisterScope temps(masm);
780 Register temp = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +0100781 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
782 trg_loc,
783 base,
784 /* offset */ 0U,
785 /* index */ offset_loc,
786 /* scale_factor */ 0U,
787 temp,
788 /* needs_null_check */ false,
789 is_volatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800790 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000791 // Other cases.
792 MemOperand mem_op(base.X(), offset);
793 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000794 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000795 } else {
796 codegen->Load(type, trg, mem_op);
797 }
Roland Levillain4d027112015-07-01 15:41:14 +0100798
Roland Levillain44015862016-01-22 11:47:17 +0000799 if (type == Primitive::kPrimNot) {
800 DCHECK(trg.IsW());
801 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
802 }
Roland Levillain4d027112015-07-01 15:41:14 +0100803 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800804}
805
806static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807 bool can_call = kEmitCompilerReadBarrier &&
808 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
809 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800810 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000811 can_call ?
812 LocationSummary::kCallOnSlowPath :
813 LocationSummary::kNoCall,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800814 kIntrinsified);
815 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
816 locations->SetInAt(1, Location::RequiresRegister());
817 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100818 locations->SetOut(Location::RequiresRegister(),
819 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800820}
821
822void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
823 CreateIntIntIntToIntLocations(arena_, invoke);
824}
825void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
826 CreateIntIntIntToIntLocations(arena_, invoke);
827}
828void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
829 CreateIntIntIntToIntLocations(arena_, invoke);
830}
831void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
832 CreateIntIntIntToIntLocations(arena_, invoke);
833}
834void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
835 CreateIntIntIntToIntLocations(arena_, invoke);
836}
837void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
838 CreateIntIntIntToIntLocations(arena_, invoke);
839}
840
841void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000842 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800843}
844void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000845 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800846}
847void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000848 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800849}
850void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000851 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800852}
853void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000854 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800855}
856void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000857 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800858}
859
860static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
861 LocationSummary* locations = new (arena) LocationSummary(invoke,
862 LocationSummary::kNoCall,
863 kIntrinsified);
864 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
865 locations->SetInAt(1, Location::RequiresRegister());
866 locations->SetInAt(2, Location::RequiresRegister());
867 locations->SetInAt(3, Location::RequiresRegister());
868}
869
870void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
871 CreateIntIntIntIntToVoid(arena_, invoke);
872}
873void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
874 CreateIntIntIntIntToVoid(arena_, invoke);
875}
876void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
877 CreateIntIntIntIntToVoid(arena_, invoke);
878}
879void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
880 CreateIntIntIntIntToVoid(arena_, invoke);
881}
882void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
883 CreateIntIntIntIntToVoid(arena_, invoke);
884}
885void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
886 CreateIntIntIntIntToVoid(arena_, invoke);
887}
888void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
889 CreateIntIntIntIntToVoid(arena_, invoke);
890}
891void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
892 CreateIntIntIntIntToVoid(arena_, invoke);
893}
894void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
895 CreateIntIntIntIntToVoid(arena_, invoke);
896}
897
898static void GenUnsafePut(LocationSummary* locations,
899 Primitive::Type type,
900 bool is_volatile,
901 bool is_ordered,
902 CodeGeneratorARM64* codegen) {
903 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
904
905 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
906 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
907 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100908 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800909 MemOperand mem_op(base.X(), offset);
910
Roland Levillain4d027112015-07-01 15:41:14 +0100911 {
912 // We use a block to end the scratch scope before the write barrier, thus
913 // freeing the temporary registers so they can be used in `MarkGCCard`.
914 UseScratchRegisterScope temps(masm);
915
916 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
917 DCHECK(value.IsW());
918 Register temp = temps.AcquireW();
919 __ Mov(temp.W(), value.W());
920 codegen->GetAssembler()->PoisonHeapReference(temp.W());
921 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800922 }
Roland Levillain4d027112015-07-01 15:41:14 +0100923
924 if (is_volatile || is_ordered) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000925 codegen->StoreRelease(type, source, mem_op);
Roland Levillain4d027112015-07-01 15:41:14 +0100926 } else {
927 codegen->Store(type, source, mem_op);
928 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800929 }
930
931 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100932 bool value_can_be_null = true; // TODO: Worth finding out this information?
933 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800934 }
935}
936
937void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000938 GenUnsafePut(invoke->GetLocations(),
939 Primitive::kPrimInt,
940 /* is_volatile */ false,
941 /* is_ordered */ false,
942 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800943}
944void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000945 GenUnsafePut(invoke->GetLocations(),
946 Primitive::kPrimInt,
947 /* is_volatile */ false,
948 /* is_ordered */ true,
949 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800950}
951void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000952 GenUnsafePut(invoke->GetLocations(),
953 Primitive::kPrimInt,
954 /* is_volatile */ true,
955 /* is_ordered */ false,
956 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800957}
958void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000959 GenUnsafePut(invoke->GetLocations(),
960 Primitive::kPrimNot,
961 /* is_volatile */ false,
962 /* is_ordered */ false,
963 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800964}
965void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000966 GenUnsafePut(invoke->GetLocations(),
967 Primitive::kPrimNot,
968 /* is_volatile */ false,
969 /* is_ordered */ true,
970 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800971}
972void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000973 GenUnsafePut(invoke->GetLocations(),
974 Primitive::kPrimNot,
975 /* is_volatile */ true,
976 /* is_ordered */ false,
977 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800978}
979void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000980 GenUnsafePut(invoke->GetLocations(),
981 Primitive::kPrimLong,
982 /* is_volatile */ false,
983 /* is_ordered */ false,
984 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800985}
986void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000987 GenUnsafePut(invoke->GetLocations(),
988 Primitive::kPrimLong,
989 /* is_volatile */ false,
990 /* is_ordered */ true,
991 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800992}
993void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000994 GenUnsafePut(invoke->GetLocations(),
995 Primitive::kPrimLong,
996 /* is_volatile */ true,
997 /* is_ordered */ false,
998 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800999}
1000
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001001static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
1002 HInvoke* invoke,
1003 Primitive::Type type) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001004 LocationSummary* locations = new (arena) LocationSummary(invoke,
1005 LocationSummary::kNoCall,
1006 kIntrinsified);
1007 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1008 locations->SetInAt(1, Location::RequiresRegister());
1009 locations->SetInAt(2, Location::RequiresRegister());
1010 locations->SetInAt(3, Location::RequiresRegister());
1011 locations->SetInAt(4, Location::RequiresRegister());
1012
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001013 // If heap poisoning is enabled, we don't want the unpoisoning
1014 // operations to potentially clobber the output.
1015 Location::OutputOverlap overlaps = (kPoisonHeapReferences && type == Primitive::kPrimNot)
1016 ? Location::kOutputOverlap
1017 : Location::kNoOutputOverlap;
1018 locations->SetOut(Location::RequiresRegister(), overlaps);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001019}
1020
1021static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorARM64* codegen) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001022 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
1023
1024 Register out = WRegisterFrom(locations->Out()); // Boolean result.
1025
1026 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1027 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1028 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1029 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1030
1031 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
1032 if (type == Primitive::kPrimNot) {
1033 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001034 bool value_can_be_null = true; // TODO: Worth finding out this information?
1035 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001036 }
1037
1038 UseScratchRegisterScope temps(masm);
1039 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1040 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1041
1042 Register tmp_32 = tmp_value.W();
1043
1044 __ Add(tmp_ptr, base.X(), Operand(offset));
1045
Roland Levillain4d027112015-07-01 15:41:14 +01001046 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1047 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001048 if (value.Is(expected)) {
1049 // Do not poison `value`, as it is the same register as
1050 // `expected`, which has just been poisoned.
1051 } else {
1052 codegen->GetAssembler()->PoisonHeapReference(value);
1053 }
Roland Levillain4d027112015-07-01 15:41:14 +01001054 }
1055
Andreas Gampe878d58c2015-01-15 23:24:00 -08001056 // do {
1057 // tmp_value = [tmp_ptr] - expected;
1058 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1059 // result = tmp_value != 0;
1060
1061 vixl::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001062 __ Bind(&loop_head);
1063 // TODO: When `type == Primitive::kPrimNot`, add a read barrier for
1064 // the reference stored in the object before attempting the CAS,
1065 // similar to the one in the art::Unsafe_compareAndSwapObject JNI
1066 // implementation.
1067 //
1068 // Note that this code is not (yet) used when read barriers are
1069 // enabled (see IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject).
1070 DCHECK(!(type == Primitive::kPrimNot && kEmitCompilerReadBarrier));
1071 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1072 __ Cmp(tmp_value, expected);
1073 __ B(&exit_loop, ne);
1074 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1075 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001076 __ Bind(&exit_loop);
1077 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001078
1079 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01001080 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001081 if (value.Is(expected)) {
1082 // Do not unpoison `value`, as it is the same register as
1083 // `expected`, which has just been unpoisoned.
1084 } else {
1085 codegen->GetAssembler()->UnpoisonHeapReference(value);
1086 }
Roland Levillain4d027112015-07-01 15:41:14 +01001087 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001088}
1089
1090void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001091 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001092}
1093void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001094 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001095}
1096void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00001097 // The UnsafeCASObject intrinsic is missing a read barrier, and
1098 // therefore sometimes does not work as expected (b/25883050).
1099 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01001100 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00001101 //
Roland Levillain3d312422016-06-23 13:53:42 +01001102 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
1103 // this intrinsic.
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001104 if (kEmitCompilerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001105 return;
1106 }
1107
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001108 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001109}
1110
1111void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
1112 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1113}
1114void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
1115 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
1116}
1117void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01001118 // The UnsafeCASObject intrinsic is missing a read barrier, and
1119 // therefore sometimes does not work as expected (b/25883050).
1120 // Turn it off temporarily as a quick fix, until the read barrier is
1121 // implemented (see TODO in GenCAS).
1122 //
1123 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
1124 // this intrinsic.
1125 DCHECK(!kEmitCompilerReadBarrier);
1126
Andreas Gampe878d58c2015-01-15 23:24:00 -08001127 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1128}
1129
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001130void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001131 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Scott Wakeling1f36f412016-04-21 11:13:45 +01001132 invoke->InputAt(1)->CanBeNull()
1133 ? LocationSummary::kCallOnSlowPath
1134 : LocationSummary::kNoCall,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001135 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001136 locations->SetInAt(0, Location::RequiresRegister());
1137 locations->SetInAt(1, Location::RequiresRegister());
1138 locations->AddTemp(Location::RequiresRegister());
1139 locations->AddTemp(Location::RequiresRegister());
1140 locations->AddTemp(Location::RequiresRegister());
1141 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001142}
1143
1144void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
1145 vixl::MacroAssembler* masm = GetVIXLAssembler();
1146 LocationSummary* locations = invoke->GetLocations();
1147
Scott Wakeling1f36f412016-04-21 11:13:45 +01001148 Register str = XRegisterFrom(locations->InAt(0));
1149 Register arg = XRegisterFrom(locations->InAt(1));
1150 Register out = OutputRegister(invoke);
1151
1152 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1153 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1154 Register temp2 = WRegisterFrom(locations->GetTemp(2));
1155
1156 vixl::Label loop;
1157 vixl::Label find_char_diff;
1158 vixl::Label end;
1159
1160 // Get offsets of count and value fields within a string object.
1161 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1162 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1163
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001164 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001165 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001166
Scott Wakeling1f36f412016-04-21 11:13:45 +01001167 // Take slow path and throw if input can be and is null.
1168 SlowPathCodeARM64* slow_path = nullptr;
1169 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1170 if (can_slow_path) {
1171 slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1172 codegen_->AddSlowPath(slow_path);
1173 __ Cbz(arg, slow_path->GetEntryLabel());
1174 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001175
Scott Wakeling1f36f412016-04-21 11:13:45 +01001176 // Reference equality check, return 0 if same reference.
1177 __ Subs(out, str, arg);
1178 __ B(&end, eq);
1179 // Load lengths of this and argument strings.
1180 __ Ldr(temp0, MemOperand(str.X(), count_offset));
1181 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1182 // Return zero if both strings are empty.
1183 __ Orr(out, temp0, temp1);
1184 __ Cbz(out, &end);
1185 // out = length diff.
1186 __ Subs(out, temp0, temp1);
1187 // temp2 = min(len(str), len(arg)).
1188 __ Csel(temp2, temp1, temp0, ge);
1189 // Shorter string is empty?
1190 __ Cbz(temp2, &end);
1191
1192 // Store offset of string value in preparation for comparison loop.
1193 __ Mov(temp1, value_offset);
1194
1195 UseScratchRegisterScope scratch_scope(masm);
1196 Register temp4 = scratch_scope.AcquireX();
1197
1198 // Assertions that must hold in order to compare strings 4 characters at a time.
1199 DCHECK_ALIGNED(value_offset, 8);
1200 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1201
1202 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1203 DCHECK_EQ(char_size, 2u);
1204
1205 // Promote temp0 to an X reg, ready for LDR.
1206 temp0 = temp0.X();
1207
1208 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1209 __ Bind(&loop);
1210 __ Ldr(temp4, MemOperand(str.X(), temp1));
1211 __ Ldr(temp0, MemOperand(arg.X(), temp1));
1212 __ Cmp(temp4, temp0);
1213 __ B(ne, &find_char_diff);
1214 __ Add(temp1, temp1, char_size * 4);
1215 __ Subs(temp2, temp2, 4);
1216 __ B(gt, &loop);
1217 __ B(&end);
1218
1219 // Promote temp1 to an X reg, ready for EOR.
1220 temp1 = temp1.X();
1221
1222 // Find the single 16-bit character difference.
1223 __ Bind(&find_char_diff);
1224 // Get the bit position of the first character that differs.
1225 __ Eor(temp1, temp0, temp4);
1226 __ Rbit(temp1, temp1);
1227 __ Clz(temp1, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001228 // If the number of 16-bit chars remaining <= the index where the difference occurs (0-3), then
1229 // the difference occurs outside the remaining string data, so just return length diff (out).
1230 __ Cmp(temp2, Operand(temp1, LSR, 4));
1231 __ B(le, &end);
1232 // Extract the characters and calculate the difference.
Scott Wakelinge5ed20b2016-05-20 10:41:38 +01001233 __ Bic(temp1, temp1, 0xf);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001234 __ Lsr(temp0, temp0, temp1);
1235 __ Lsr(temp4, temp4, temp1);
1236 __ And(temp4, temp4, 0xffff);
1237 __ Sub(out, temp4, Operand(temp0, UXTH));
1238
1239 __ Bind(&end);
1240
1241 if (can_slow_path) {
1242 __ Bind(slow_path->GetExitLabel());
1243 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001244}
1245
Agi Csakiea34b402015-08-13 17:51:19 -07001246void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
1247 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1248 LocationSummary::kNoCall,
1249 kIntrinsified);
1250 locations->SetInAt(0, Location::RequiresRegister());
1251 locations->SetInAt(1, Location::RequiresRegister());
1252 // Temporary registers to store lengths of strings and for calculations.
1253 locations->AddTemp(Location::RequiresRegister());
1254 locations->AddTemp(Location::RequiresRegister());
1255
1256 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1257}
1258
1259void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
1260 vixl::MacroAssembler* masm = GetVIXLAssembler();
1261 LocationSummary* locations = invoke->GetLocations();
1262
1263 Register str = WRegisterFrom(locations->InAt(0));
1264 Register arg = WRegisterFrom(locations->InAt(1));
1265 Register out = XRegisterFrom(locations->Out());
1266
1267 UseScratchRegisterScope scratch_scope(masm);
1268 Register temp = scratch_scope.AcquireW();
1269 Register temp1 = WRegisterFrom(locations->GetTemp(0));
1270 Register temp2 = WRegisterFrom(locations->GetTemp(1));
1271
1272 vixl::Label loop;
1273 vixl::Label end;
1274 vixl::Label return_true;
1275 vixl::Label return_false;
1276
1277 // Get offsets of count, value, and class fields within a string object.
1278 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1279 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1280 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1281
1282 // Note that the null check must have been done earlier.
1283 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1284
Vladimir Marko53b52002016-05-24 19:30:45 +01001285 StringEqualsOptimizations optimizations(invoke);
1286 if (!optimizations.GetArgumentNotNull()) {
1287 // Check if input is null, return false if it is.
1288 __ Cbz(arg, &return_false);
1289 }
Agi Csakiea34b402015-08-13 17:51:19 -07001290
1291 // Reference equality check, return true if same reference.
1292 __ Cmp(str, arg);
1293 __ B(&return_true, eq);
1294
Vladimir Marko53b52002016-05-24 19:30:45 +01001295 if (!optimizations.GetArgumentIsString()) {
1296 // Instanceof check for the argument by comparing class fields.
1297 // All string objects must have the same type since String cannot be subclassed.
1298 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1299 // If the argument is a string object, its class field must be equal to receiver's class field.
1300 __ Ldr(temp, MemOperand(str.X(), class_offset));
1301 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1302 __ Cmp(temp, temp1);
1303 __ B(&return_false, ne);
1304 }
Agi Csakiea34b402015-08-13 17:51:19 -07001305
1306 // Load lengths of this and argument strings.
1307 __ Ldr(temp, MemOperand(str.X(), count_offset));
1308 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1309 // Check if lengths are equal, return false if they're not.
1310 __ Cmp(temp, temp1);
1311 __ B(&return_false, ne);
1312 // Store offset of string value in preparation for comparison loop
1313 __ Mov(temp1, value_offset);
1314 // Return true if both strings are empty.
1315 __ Cbz(temp, &return_true);
1316
1317 // Assertions that must hold in order to compare strings 4 characters at a time.
1318 DCHECK_ALIGNED(value_offset, 8);
1319 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1320
1321 temp1 = temp1.X();
1322 temp2 = temp2.X();
1323
1324 // Loop to compare strings 4 characters at a time starting at the beginning of the string.
1325 // Ok to do this because strings are zero-padded to be 8-byte aligned.
1326 __ Bind(&loop);
1327 __ Ldr(out, MemOperand(str.X(), temp1));
1328 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1329 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1330 __ Cmp(out, temp2);
1331 __ B(&return_false, ne);
1332 __ Sub(temp, temp, Operand(4), SetFlags);
1333 __ B(&loop, gt);
1334
1335 // Return true and exit the function.
1336 // If loop does not result in returning false, we return true.
1337 __ Bind(&return_true);
1338 __ Mov(out, 1);
1339 __ B(&end);
1340
1341 // Return false and exit the function.
1342 __ Bind(&return_false);
1343 __ Mov(out, 0);
1344 __ Bind(&end);
1345}
1346
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001347static void GenerateVisitStringIndexOf(HInvoke* invoke,
1348 vixl::MacroAssembler* masm,
1349 CodeGeneratorARM64* codegen,
1350 ArenaAllocator* allocator,
1351 bool start_at_zero) {
1352 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001353
1354 // Note that the null check must have been done earlier.
1355 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1356
1357 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001358 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001359 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001360 HInstruction* code_point = invoke->InputAt(1);
1361 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001362 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001363 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1364 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1365 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1366 codegen->AddSlowPath(slow_path);
1367 __ B(slow_path->GetEntryLabel());
1368 __ Bind(slow_path->GetExitLabel());
1369 return;
1370 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001371 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001372 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001373 __ Tst(char_reg, 0xFFFF0000);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001374 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1375 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001376 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001377 }
1378
1379 if (start_at_zero) {
1380 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001381 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001382 __ Mov(tmp_reg, 0);
1383 }
1384
1385 __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pIndexOf).Int32Value()));
Roland Levillain42ad2882016-02-29 18:26:54 +00001386 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001387 __ Blr(lr);
1388
1389 if (slow_path != nullptr) {
1390 __ Bind(slow_path->GetExitLabel());
1391 }
1392}
1393
1394void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1395 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1396 LocationSummary::kCall,
1397 kIntrinsified);
1398 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1399 // best to align the inputs accordingly.
1400 InvokeRuntimeCallingConvention calling_convention;
1401 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1402 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1403 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1404
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001405 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001406 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1407}
1408
1409void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001410 GenerateVisitStringIndexOf(
1411 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001412}
1413
1414void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1415 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1416 LocationSummary::kCall,
1417 kIntrinsified);
1418 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1419 // best to align the inputs accordingly.
1420 InvokeRuntimeCallingConvention calling_convention;
1421 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1422 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1423 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1424 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001425}
1426
1427void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001428 GenerateVisitStringIndexOf(
1429 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001430}
1431
Jeff Hao848f70a2014-01-15 13:49:50 -08001432void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1433 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1434 LocationSummary::kCall,
1435 kIntrinsified);
1436 InvokeRuntimeCallingConvention calling_convention;
1437 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1438 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1439 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1440 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1441 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1442}
1443
1444void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1445 vixl::MacroAssembler* masm = GetVIXLAssembler();
1446 LocationSummary* locations = invoke->GetLocations();
1447
1448 Register byte_array = WRegisterFrom(locations->InAt(0));
1449 __ Cmp(byte_array, 0);
1450 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1451 codegen_->AddSlowPath(slow_path);
1452 __ B(eq, slow_path->GetEntryLabel());
1453
1454 __ Ldr(lr,
1455 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromBytes).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001456 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001457 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001458 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001459 __ Bind(slow_path->GetExitLabel());
1460}
1461
1462void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1463 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1464 LocationSummary::kCall,
1465 kIntrinsified);
1466 InvokeRuntimeCallingConvention calling_convention;
1467 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1468 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1469 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1470 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1471}
1472
1473void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1474 vixl::MacroAssembler* masm = GetVIXLAssembler();
1475
Roland Levillaincc3839c2016-02-29 16:23:48 +00001476 // No need to emit code checking whether `locations->InAt(2)` is a null
1477 // pointer, as callers of the native method
1478 //
1479 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1480 //
1481 // all include a null check on `data` before calling that method.
Jeff Hao848f70a2014-01-15 13:49:50 -08001482 __ Ldr(lr,
1483 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromChars).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001484 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001485 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001486 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001487}
1488
1489void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001490 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1491 LocationSummary::kCall,
1492 kIntrinsified);
1493 InvokeRuntimeCallingConvention calling_convention;
1494 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Jeff Hao848f70a2014-01-15 13:49:50 -08001495 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1496}
1497
1498void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
1499 vixl::MacroAssembler* masm = GetVIXLAssembler();
1500 LocationSummary* locations = invoke->GetLocations();
1501
1502 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1503 __ Cmp(string_to_copy, 0);
1504 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1505 codegen_->AddSlowPath(slow_path);
1506 __ B(eq, slow_path->GetEntryLabel());
1507
1508 __ Ldr(lr,
1509 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromString).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001510 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001511 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001512 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001513 __ Bind(slow_path->GetExitLabel());
1514}
1515
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001516static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1517 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
1518 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1519 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1520
1521 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1522 LocationSummary::kCall,
1523 kIntrinsified);
1524 InvokeRuntimeCallingConvention calling_convention;
1525
1526 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1527 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1528}
1529
1530static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1531 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
1532 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1533 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1534 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1535
1536 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1537 LocationSummary::kCall,
1538 kIntrinsified);
1539 InvokeRuntimeCallingConvention calling_convention;
1540
1541 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1542 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1543 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1544}
1545
1546static void GenFPToFPCall(HInvoke* invoke,
1547 vixl::MacroAssembler* masm,
1548 CodeGeneratorARM64* codegen,
1549 QuickEntrypointEnum entry) {
1550 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64WordSize>(entry).Int32Value()));
1551 __ Blr(lr);
1552 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
1553}
1554
1555void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
1556 CreateFPToFPCallLocations(arena_, invoke);
1557}
1558
1559void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
1560 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCos);
1561}
1562
1563void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
1564 CreateFPToFPCallLocations(arena_, invoke);
1565}
1566
1567void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
1568 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSin);
1569}
1570
1571void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
1572 CreateFPToFPCallLocations(arena_, invoke);
1573}
1574
1575void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
1576 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAcos);
1577}
1578
1579void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
1580 CreateFPToFPCallLocations(arena_, invoke);
1581}
1582
1583void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
1584 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAsin);
1585}
1586
1587void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
1588 CreateFPToFPCallLocations(arena_, invoke);
1589}
1590
1591void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
1592 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan);
1593}
1594
1595void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
1596 CreateFPToFPCallLocations(arena_, invoke);
1597}
1598
1599void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
1600 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCbrt);
1601}
1602
1603void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
1604 CreateFPToFPCallLocations(arena_, invoke);
1605}
1606
1607void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
1608 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCosh);
1609}
1610
1611void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
1612 CreateFPToFPCallLocations(arena_, invoke);
1613}
1614
1615void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
1616 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExp);
1617}
1618
1619void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
1620 CreateFPToFPCallLocations(arena_, invoke);
1621}
1622
1623void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
1624 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExpm1);
1625}
1626
1627void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
1628 CreateFPToFPCallLocations(arena_, invoke);
1629}
1630
1631void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
1632 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog);
1633}
1634
1635void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
1636 CreateFPToFPCallLocations(arena_, invoke);
1637}
1638
1639void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
1640 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog10);
1641}
1642
1643void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
1644 CreateFPToFPCallLocations(arena_, invoke);
1645}
1646
1647void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
1648 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSinh);
1649}
1650
1651void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
1652 CreateFPToFPCallLocations(arena_, invoke);
1653}
1654
1655void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
1656 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTan);
1657}
1658
1659void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
1660 CreateFPToFPCallLocations(arena_, invoke);
1661}
1662
1663void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
1664 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTanh);
1665}
1666
1667void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
1668 CreateFPFPToFPCallLocations(arena_, invoke);
1669}
1670
1671void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
1672 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan2);
1673}
1674
1675void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
1676 CreateFPFPToFPCallLocations(arena_, invoke);
1677}
1678
1679void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
1680 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickHypot);
1681}
1682
1683void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
1684 CreateFPFPToFPCallLocations(arena_, invoke);
1685}
1686
1687void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
1688 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickNextAfter);
1689}
1690
Tim Zhang25abd6c2016-01-19 23:39:24 +08001691void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1692 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1693 LocationSummary::kNoCall,
1694 kIntrinsified);
1695 locations->SetInAt(0, Location::RequiresRegister());
1696 locations->SetInAt(1, Location::RequiresRegister());
1697 locations->SetInAt(2, Location::RequiresRegister());
1698 locations->SetInAt(3, Location::RequiresRegister());
1699 locations->SetInAt(4, Location::RequiresRegister());
1700
1701 locations->AddTemp(Location::RequiresRegister());
1702 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01001703 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08001704}
1705
1706void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1707 vixl::MacroAssembler* masm = GetVIXLAssembler();
1708 LocationSummary* locations = invoke->GetLocations();
1709
1710 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1711 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1712 DCHECK_EQ(char_size, 2u);
1713
1714 // Location of data in char array buffer.
1715 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1716
1717 // Location of char array data in string.
1718 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1719
1720 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1721 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
1722 Register srcObj = XRegisterFrom(locations->InAt(0));
1723 Register srcBegin = XRegisterFrom(locations->InAt(1));
1724 Register srcEnd = XRegisterFrom(locations->InAt(2));
1725 Register dstObj = XRegisterFrom(locations->InAt(3));
1726 Register dstBegin = XRegisterFrom(locations->InAt(4));
1727
1728 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001729 Register num_chr = XRegisterFrom(locations->GetTemp(1));
1730 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001731
1732 UseScratchRegisterScope temps(masm);
1733 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01001734 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001735
Scott Wakelingdf109d92016-04-22 11:35:56 +01001736 // src address to copy from.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001737 __ Add(src_ptr, srcObj, Operand(value_offset));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001738 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
1739
Scott Wakelingdf109d92016-04-22 11:35:56 +01001740 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001741 __ Add(dst_ptr, dstObj, Operand(data_offset));
1742 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
1743
Scott Wakelingdf109d92016-04-22 11:35:56 +01001744 __ Sub(num_chr, srcEnd, srcBegin);
1745
Tim Zhang25abd6c2016-01-19 23:39:24 +08001746 // Do the copy.
Scott Wakelingdf109d92016-04-22 11:35:56 +01001747 vixl::Label loop;
1748 vixl::Label done;
1749 vixl::Label remainder;
1750
1751 // Early out for valid zero-length retrievals.
1752 __ Cbz(num_chr, &done);
1753
1754 // Save repairing the value of num_chr on the < 8 character path.
1755 __ Subs(tmp1, num_chr, 8);
1756 __ B(lt, &remainder);
1757
1758 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
1759 __ Mov(num_chr, tmp1);
1760
1761 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
1762 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08001763 __ Bind(&loop);
Scott Wakelingdf109d92016-04-22 11:35:56 +01001764 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, vixl::PostIndex));
1765 __ Subs(num_chr, num_chr, 8);
1766 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, vixl::PostIndex));
1767 __ B(ge, &loop);
1768
1769 __ Adds(num_chr, num_chr, 8);
1770 __ B(eq, &done);
1771
1772 // Main loop for < 8 character case and remainder handling. Loads and stores one
1773 // 16-bit Java character at a time.
1774 __ Bind(&remainder);
1775 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, vixl::PostIndex));
1776 __ Subs(num_chr, num_chr, 1);
1777 __ Strh(tmp1, MemOperand(dst_ptr, char_size, vixl::PostIndex));
1778 __ B(gt, &remainder);
1779
Tim Zhang25abd6c2016-01-19 23:39:24 +08001780 __ Bind(&done);
1781}
1782
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001783// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
1784// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001785static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001786
1787static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
1788 uint32_t at,
1789 HInstruction* input) {
1790 HIntConstant* const_input = input->AsIntConstant();
1791 if (const_input != nullptr && !vixl::Assembler::IsImmAddSub(const_input->GetValue())) {
1792 locations->SetInAt(at, Location::RequiresRegister());
1793 } else {
1794 locations->SetInAt(at, Location::RegisterOrConstant(input));
1795 }
1796}
1797
1798void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1799 // Check to see if we have known failures that will cause us to have to bail out
1800 // to the runtime, and just generate the runtime call directly.
1801 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1802 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
1803
1804 // The positions must be non-negative.
1805 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1806 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
1807 // We will have to fail anyways.
1808 return;
1809 }
1810
1811 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
1812 // native implementation.
1813 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1814 if (length != nullptr) {
1815 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08001816 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001817 // Just call as normal.
1818 return;
1819 }
1820 }
1821
1822 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1823 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1824 LocationSummary::kCallOnSlowPath,
1825 kIntrinsified);
1826 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
1827 locations->SetInAt(0, Location::RequiresRegister());
1828 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
1829 locations->SetInAt(2, Location::RequiresRegister());
1830 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
1831 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
1832
1833 locations->AddTemp(Location::RequiresRegister());
1834 locations->AddTemp(Location::RequiresRegister());
1835 locations->AddTemp(Location::RequiresRegister());
1836}
1837
1838static void CheckSystemArrayCopyPosition(vixl::MacroAssembler* masm,
1839 const Location& pos,
1840 const Register& input,
1841 const Location& length,
1842 SlowPathCodeARM64* slow_path,
1843 const Register& input_len,
1844 const Register& temp,
1845 bool length_is_input_length = false) {
1846 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
1847 if (pos.IsConstant()) {
1848 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1849 if (pos_const == 0) {
1850 if (!length_is_input_length) {
1851 // Check that length(input) >= length.
1852 __ Ldr(temp, MemOperand(input, length_offset));
1853 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1854 __ B(slow_path->GetEntryLabel(), lt);
1855 }
1856 } else {
1857 // Check that length(input) >= pos.
1858 __ Ldr(input_len, MemOperand(input, length_offset));
1859 __ Subs(temp, input_len, pos_const);
1860 __ B(slow_path->GetEntryLabel(), lt);
1861
1862 // Check that (length(input) - pos) >= length.
1863 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1864 __ B(slow_path->GetEntryLabel(), lt);
1865 }
1866 } else if (length_is_input_length) {
1867 // The only way the copy can succeed is if pos is zero.
1868 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
1869 } else {
1870 // Check that pos >= 0.
1871 Register pos_reg = WRegisterFrom(pos);
1872 __ Tbnz(pos_reg, pos_reg.size() - 1, slow_path->GetEntryLabel());
1873
1874 // Check that pos <= length(input) && (length(input) - pos) >= length.
1875 __ Ldr(temp, MemOperand(input, length_offset));
1876 __ Subs(temp, temp, pos_reg);
1877 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
1878 __ Ccmp(temp, OperandFrom(length, Primitive::kPrimInt), NFlag, ge);
1879 __ B(slow_path->GetEntryLabel(), lt);
1880 }
1881}
1882
1883// Compute base source address, base destination address, and end source address
1884// for System.arraycopy* intrinsics.
1885static void GenSystemArrayCopyAddresses(vixl::MacroAssembler* masm,
1886 Primitive::Type type,
1887 const Register& src,
1888 const Location& src_pos,
1889 const Register& dst,
1890 const Location& dst_pos,
1891 const Location& copy_length,
1892 const Register& src_base,
1893 const Register& dst_base,
1894 const Register& src_end) {
1895 DCHECK(type == Primitive::kPrimNot || type == Primitive::kPrimChar)
Roland Levillainebea3d22016-04-12 15:42:57 +01001896 << "Unexpected element type: " << type;
1897 const int32_t element_size = Primitive::ComponentSize(type);
1898 const int32_t element_size_shift = Primitive::ComponentSizeShift(type);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001899
Roland Levillainebea3d22016-04-12 15:42:57 +01001900 uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001901 if (src_pos.IsConstant()) {
1902 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001903 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001904 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001905 __ Add(src_base, src, data_offset);
1906 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001907 }
1908
1909 if (dst_pos.IsConstant()) {
1910 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001911 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001912 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001913 __ Add(dst_base, dst, data_offset);
1914 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001915 }
1916
1917 if (copy_length.IsConstant()) {
1918 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001919 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001920 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001921 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001922 }
1923}
1924
1925void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1926 vixl::MacroAssembler* masm = GetVIXLAssembler();
1927 LocationSummary* locations = invoke->GetLocations();
1928 Register src = XRegisterFrom(locations->InAt(0));
1929 Location src_pos = locations->InAt(1);
1930 Register dst = XRegisterFrom(locations->InAt(2));
1931 Location dst_pos = locations->InAt(3);
1932 Location length = locations->InAt(4);
1933
1934 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1935 codegen_->AddSlowPath(slow_path);
1936
1937 // If source and destination are the same, take the slow path. Overlapping copy regions must be
1938 // copied in reverse and we can't know in all cases if it's needed.
1939 __ Cmp(src, dst);
1940 __ B(slow_path->GetEntryLabel(), eq);
1941
1942 // Bail out if the source is null.
1943 __ Cbz(src, slow_path->GetEntryLabel());
1944
1945 // Bail out if the destination is null.
1946 __ Cbz(dst, slow_path->GetEntryLabel());
1947
1948 if (!length.IsConstant()) {
1949 // If the length is negative, bail out.
1950 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, slow_path->GetEntryLabel());
1951 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001952 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001953 __ B(slow_path->GetEntryLabel(), gt);
1954 } else {
1955 // We have already checked in the LocationsBuilder for the constant case.
1956 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
1957 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
1958 }
1959
1960 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
1961 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
1962 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
1963
1964 CheckSystemArrayCopyPosition(masm,
1965 src_pos,
1966 src,
1967 length,
1968 slow_path,
1969 src_curr_addr,
1970 dst_curr_addr,
1971 false);
1972
1973 CheckSystemArrayCopyPosition(masm,
1974 dst_pos,
1975 dst,
1976 length,
1977 slow_path,
1978 src_curr_addr,
1979 dst_curr_addr,
1980 false);
1981
1982 src_curr_addr = src_curr_addr.X();
1983 dst_curr_addr = dst_curr_addr.X();
1984 src_stop_addr = src_stop_addr.X();
1985
1986 GenSystemArrayCopyAddresses(masm,
1987 Primitive::kPrimChar,
1988 src,
1989 src_pos,
1990 dst,
1991 dst_pos,
1992 length,
1993 src_curr_addr,
1994 dst_curr_addr,
1995 src_stop_addr);
1996
1997 // Iterate over the arrays and do a raw copy of the chars.
1998 const int32_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1999 UseScratchRegisterScope temps(masm);
2000 Register tmp = temps.AcquireW();
2001 vixl::Label loop, done;
2002 __ Bind(&loop);
2003 __ Cmp(src_curr_addr, src_stop_addr);
2004 __ B(&done, eq);
2005 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, vixl::PostIndex));
2006 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, vixl::PostIndex));
2007 __ B(&loop);
2008 __ Bind(&done);
2009
2010 __ Bind(slow_path->GetExitLabel());
2011}
2012
donghui.baic2ec9ad2016-03-10 14:02:55 +08002013// We can choose to use the native implementation there for longer copy lengths.
2014static constexpr int32_t kSystemArrayCopyThreshold = 128;
2015
2016// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2017// We want to use two temporary registers in order to reduce the register pressure in arm64.
2018// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2019void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002020 // TODO(rpl): Implement read barriers in the SystemArrayCopy
2021 // intrinsic and re-enable it (b/29516905).
2022 if (kEmitCompilerReadBarrier) {
2023 return;
2024 }
2025
donghui.baic2ec9ad2016-03-10 14:02:55 +08002026 // Check to see if we have known failures that will cause us to have to bail out
2027 // to the runtime, and just generate the runtime call directly.
2028 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2029 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2030
2031 // The positions must be non-negative.
2032 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2033 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2034 // We will have to fail anyways.
2035 return;
2036 }
2037
2038 // The length must be >= 0.
2039 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2040 if (length != nullptr) {
2041 int32_t len = length->GetValue();
2042 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2043 // Just call as normal.
2044 return;
2045 }
2046 }
2047
2048 SystemArrayCopyOptimizations optimizations(invoke);
2049
2050 if (optimizations.GetDestinationIsSource()) {
2051 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2052 // We only support backward copying if source and destination are the same.
2053 return;
2054 }
2055 }
2056
2057 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2058 // We currently don't intrinsify primitive copying.
2059 return;
2060 }
2061
2062 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2063 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2064 LocationSummary::kCallOnSlowPath,
2065 kIntrinsified);
2066 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2067 locations->SetInAt(0, Location::RequiresRegister());
2068 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2069 locations->SetInAt(2, Location::RequiresRegister());
2070 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2071 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2072
2073 locations->AddTemp(Location::RequiresRegister());
2074 locations->AddTemp(Location::RequiresRegister());
2075}
2076
2077void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002078 // TODO(rpl): Implement read barriers in the SystemArrayCopy
2079 // intrinsic and re-enable it (b/29516905).
2080 DCHECK(!kEmitCompilerReadBarrier);
2081
donghui.baic2ec9ad2016-03-10 14:02:55 +08002082 vixl::MacroAssembler* masm = GetVIXLAssembler();
2083 LocationSummary* locations = invoke->GetLocations();
2084
2085 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2086 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2087 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2088 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2089
2090 Register src = XRegisterFrom(locations->InAt(0));
2091 Location src_pos = locations->InAt(1);
2092 Register dest = XRegisterFrom(locations->InAt(2));
2093 Location dest_pos = locations->InAt(3);
2094 Location length = locations->InAt(4);
2095 Register temp1 = WRegisterFrom(locations->GetTemp(0));
2096 Register temp2 = WRegisterFrom(locations->GetTemp(1));
2097
2098 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2099 codegen_->AddSlowPath(slow_path);
2100
2101 vixl::Label conditions_on_positions_validated;
2102 SystemArrayCopyOptimizations optimizations(invoke);
2103
donghui.baic2ec9ad2016-03-10 14:02:55 +08002104 // If source and destination are the same, we go to slow path if we need to do
2105 // forward copying.
2106 if (src_pos.IsConstant()) {
2107 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2108 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002109 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2110 if (optimizations.GetDestinationIsSource()) {
2111 // Checked when building locations.
2112 DCHECK_GE(src_pos_constant, dest_pos_constant);
2113 } else if (src_pos_constant < dest_pos_constant) {
2114 __ Cmp(src, dest);
2115 __ B(slow_path->GetEntryLabel(), eq);
2116 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002117 // Checked when building locations.
2118 DCHECK(!optimizations.GetDestinationIsSource()
2119 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2120 } else {
2121 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002122 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002123 __ B(&conditions_on_positions_validated, ne);
2124 }
2125 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
2126 __ B(slow_path->GetEntryLabel(), gt);
2127 }
2128 } else {
2129 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002130 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002131 __ B(&conditions_on_positions_validated, ne);
2132 }
2133 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2134 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
2135 __ B(slow_path->GetEntryLabel(), lt);
2136 }
2137
2138 __ Bind(&conditions_on_positions_validated);
2139
2140 if (!optimizations.GetSourceIsNotNull()) {
2141 // Bail out if the source is null.
2142 __ Cbz(src, slow_path->GetEntryLabel());
2143 }
2144
2145 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2146 // Bail out if the destination is null.
2147 __ Cbz(dest, slow_path->GetEntryLabel());
2148 }
2149
2150 // We have already checked in the LocationsBuilder for the constant case.
2151 if (!length.IsConstant() &&
2152 !optimizations.GetCountIsSourceLength() &&
2153 !optimizations.GetCountIsDestinationLength()) {
2154 // If the length is negative, bail out.
2155 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, slow_path->GetEntryLabel());
2156 // If the length >= 128 then (currently) prefer native implementation.
2157 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
2158 __ B(slow_path->GetEntryLabel(), ge);
2159 }
2160 // Validity checks: source.
2161 CheckSystemArrayCopyPosition(masm,
2162 src_pos,
2163 src,
2164 length,
2165 slow_path,
2166 temp1,
2167 temp2,
2168 optimizations.GetCountIsSourceLength());
2169
2170 // Validity checks: dest.
2171 CheckSystemArrayCopyPosition(masm,
2172 dest_pos,
2173 dest,
2174 length,
2175 slow_path,
2176 temp1,
2177 temp2,
2178 optimizations.GetCountIsDestinationLength());
2179 {
2180 // We use a block to end the scratch scope before the write barrier, thus
2181 // freeing the temporary registers so they can be used in `MarkGCCard`.
2182 UseScratchRegisterScope temps(masm);
2183 Register temp3 = temps.AcquireW();
2184 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2185 // Check whether all elements of the source array are assignable to the component
2186 // type of the destination array. We do two checks: the classes are the same,
2187 // or the destination is Object[]. If none of these checks succeed, we go to the
2188 // slow path.
2189 __ Ldr(temp1, MemOperand(dest, class_offset));
2190 __ Ldr(temp2, MemOperand(src, class_offset));
2191 bool did_unpoison = false;
2192 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2193 !optimizations.GetSourceIsNonPrimitiveArray()) {
2194 // One or two of the references need to be unpoisoned. Unpoison them
2195 // both to make the identity check valid.
2196 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2197 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2198 did_unpoison = true;
2199 }
2200
2201 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2202 // Bail out if the destination is not a non primitive array.
2203 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2204 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2205 __ Cbz(temp3, slow_path->GetEntryLabel());
2206 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2207 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2208 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2209 __ Cbnz(temp3, slow_path->GetEntryLabel());
2210 }
2211
2212 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2213 // Bail out if the source is not a non primitive array.
2214 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2215 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2216 __ Cbz(temp3, slow_path->GetEntryLabel());
2217 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2218 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2219 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2220 __ Cbnz(temp3, slow_path->GetEntryLabel());
2221 }
2222
2223 __ Cmp(temp1, temp2);
2224
2225 if (optimizations.GetDestinationIsTypedObjectArray()) {
2226 vixl::Label do_copy;
2227 __ B(&do_copy, eq);
2228 if (!did_unpoison) {
2229 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2230 }
2231 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2232 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2233 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2234 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2235 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2236 // No need to unpoison the result, we're comparing against null.
2237 __ Cbnz(temp1, slow_path->GetEntryLabel());
2238 __ Bind(&do_copy);
2239 } else {
2240 __ B(slow_path->GetEntryLabel(), ne);
2241 }
2242 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2243 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2244 // Bail out if the source is not a non primitive array.
2245 // /* HeapReference<Class> */ temp1 = src->klass_
2246 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2247 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2248 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2249 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2250 __ Cbz(temp3, slow_path->GetEntryLabel());
2251 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2252 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2253 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2254 __ Cbnz(temp3, slow_path->GetEntryLabel());
2255 }
2256
2257 Register src_curr_addr = temp1.X();
2258 Register dst_curr_addr = temp2.X();
2259 Register src_stop_addr = temp3.X();
2260
2261 GenSystemArrayCopyAddresses(masm,
2262 Primitive::kPrimNot,
2263 src,
2264 src_pos,
2265 dest,
2266 dest_pos,
2267 length,
2268 src_curr_addr,
2269 dst_curr_addr,
2270 src_stop_addr);
2271
2272 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2273 // poison/unpoison, nor do any read barrier as the next uses of the destination
2274 // array will do it.
2275 vixl::Label loop, done;
2276 const int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
2277 __ Bind(&loop);
2278 __ Cmp(src_curr_addr, src_stop_addr);
2279 __ B(&done, eq);
2280 {
2281 Register tmp = temps.AcquireW();
2282 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, vixl::PostIndex));
2283 __ Str(tmp, MemOperand(dst_curr_addr, element_size, vixl::PostIndex));
2284 }
2285 __ B(&loop);
2286 __ Bind(&done);
2287 }
2288 // We only need one card marking on the destination array.
2289 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2290
2291 __ Bind(slow_path->GetExitLabel());
2292}
2293
Anton Kirilova3ffea22016-04-07 17:02:37 +01002294static void GenIsInfinite(LocationSummary* locations,
2295 bool is64bit,
2296 vixl::MacroAssembler* masm) {
2297 Operand infinity;
2298 Register out;
2299
2300 if (is64bit) {
2301 infinity = kPositiveInfinityDouble;
2302 out = XRegisterFrom(locations->Out());
2303 } else {
2304 infinity = kPositiveInfinityFloat;
2305 out = WRegisterFrom(locations->Out());
2306 }
2307
2308 const Register zero = vixl::Assembler::AppropriateZeroRegFor(out);
2309
2310 MoveFPToInt(locations, is64bit, masm);
2311 __ Eor(out, out, infinity);
2312 // We don't care about the sign bit, so shift left.
2313 __ Cmp(zero, Operand(out, LSL, 1));
2314 __ Cset(out, eq);
2315}
2316
2317void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2318 CreateFPToIntLocations(arena_, invoke);
2319}
2320
2321void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2322 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
2323}
2324
2325void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2326 CreateFPToIntLocations(arena_, invoke);
2327}
2328
2329void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2330 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
2331}
2332
Aart Bik2f9fcc92016-03-01 15:16:54 -08002333UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002334UNIMPLEMENTED_INTRINSIC(ARM64, IntegerHighestOneBit)
2335UNIMPLEMENTED_INTRINSIC(ARM64, LongHighestOneBit)
2336UNIMPLEMENTED_INTRINSIC(ARM64, IntegerLowestOneBit)
2337UNIMPLEMENTED_INTRINSIC(ARM64, LongLowestOneBit)
Andreas Gampe878d58c2015-01-15 23:24:00 -08002338
Aart Bik0e54c012016-03-04 12:08:31 -08002339// 1.8.
2340UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
2341UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
2342UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
2343UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
2344UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002345
Aart Bik2f9fcc92016-03-01 15:16:54 -08002346UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01002347
2348#undef __
2349
Andreas Gampe878d58c2015-01-15 23:24:00 -08002350} // namespace arm64
2351} // namespace art