blob: 1d507530aa366a13c4b1ef51c5403f6280a5d507 [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
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100611static void CreateFPToIntPlusFPTempLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800612 LocationSummary* locations = new (arena) LocationSummary(invoke,
613 LocationSummary::kNoCall,
614 kIntrinsified);
615 locations->SetInAt(0, Location::RequiresFpuRegister());
616 locations->SetOut(Location::RequiresRegister());
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100617 locations->AddTemp(Location::RequiresFpuRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800618}
619
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100620static void GenMathRound(HInvoke* invoke, bool is_double, vixl::MacroAssembler* masm) {
621 // Java 8 API definition for Math.round():
622 // Return the closest long or int to the argument, with ties rounding to positive infinity.
623 //
624 // There is no single instruction in ARMv8 that can support the above definition.
625 // We choose to use FCVTAS here, because it has closest semantic.
626 // FCVTAS performs rounding to nearest integer, ties away from zero.
627 // For most inputs (positive values, zero or NaN), this instruction is enough.
628 // We only need a few handling code after FCVTAS if the input is negative half value.
629 //
630 // The reason why we didn't choose FCVTPS instruction here is that
631 // although it performs rounding toward positive infinity, it doesn't perform rounding to nearest.
632 // For example, FCVTPS(-1.9) = -1 and FCVTPS(1.1) = 2.
633 // If we were using this instruction, for most inputs, more handling code would be needed.
634 LocationSummary* l = invoke->GetLocations();
635 FPRegister in_reg = is_double ? DRegisterFrom(l->InAt(0)) : SRegisterFrom(l->InAt(0));
636 FPRegister tmp_fp = is_double ? DRegisterFrom(l->GetTemp(0)) : SRegisterFrom(l->GetTemp(0));
637 Register out_reg = is_double ? XRegisterFrom(l->Out()) : WRegisterFrom(l->Out());
638 vixl::Label done;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800639
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100640 // Round to nearest integer, ties away from zero.
641 __ Fcvtas(out_reg, in_reg);
642
643 // For positive values, zero or NaN inputs, rounding is done.
644 __ Tbz(out_reg, out_reg.size() - 1, &done);
645
646 // Handle input < 0 cases.
647 // If input is negative but not a tie, previous result (round to nearest) is valid.
648 // If input is a negative tie, out_reg += 1.
649 __ Frinta(tmp_fp, in_reg);
650 __ Fsub(tmp_fp, in_reg, tmp_fp);
651 __ Fcmp(tmp_fp, 0.5);
652 __ Cinc(out_reg, out_reg, eq);
653
654 __ Bind(&done);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800655}
656
657void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100658 CreateFPToIntPlusFPTempLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800659}
660
661void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100662 GenMathRound(invoke, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800663}
664
665void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100666 CreateFPToIntPlusFPTempLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800667}
668
669void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100670 GenMathRound(invoke, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800671}
672
673void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
674 CreateIntToIntLocations(arena_, invoke);
675}
676
677void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
678 vixl::MacroAssembler* masm = GetVIXLAssembler();
679 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
680 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
681}
682
683void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
684 CreateIntToIntLocations(arena_, invoke);
685}
686
687void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
688 vixl::MacroAssembler* masm = GetVIXLAssembler();
689 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
690 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
691}
692
693void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
694 CreateIntToIntLocations(arena_, invoke);
695}
696
697void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
698 vixl::MacroAssembler* masm = GetVIXLAssembler();
699 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
700 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
701}
702
703void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
704 CreateIntToIntLocations(arena_, invoke);
705}
706
707void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
708 vixl::MacroAssembler* masm = GetVIXLAssembler();
709 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
710 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
711}
712
713static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
714 LocationSummary* locations = new (arena) LocationSummary(invoke,
715 LocationSummary::kNoCall,
716 kIntrinsified);
717 locations->SetInAt(0, Location::RequiresRegister());
718 locations->SetInAt(1, Location::RequiresRegister());
719}
720
721void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
722 CreateIntIntToVoidLocations(arena_, invoke);
723}
724
725void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
726 vixl::MacroAssembler* masm = GetVIXLAssembler();
727 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
728 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
729}
730
731void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
732 CreateIntIntToVoidLocations(arena_, invoke);
733}
734
735void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
736 vixl::MacroAssembler* masm = GetVIXLAssembler();
737 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
738 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
739}
740
741void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
742 CreateIntIntToVoidLocations(arena_, invoke);
743}
744
745void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
746 vixl::MacroAssembler* masm = GetVIXLAssembler();
747 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
748 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
749}
750
751void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
752 CreateIntIntToVoidLocations(arena_, invoke);
753}
754
755void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
756 vixl::MacroAssembler* masm = GetVIXLAssembler();
757 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
758 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
759}
760
761void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
762 LocationSummary* locations = new (arena_) LocationSummary(invoke,
763 LocationSummary::kNoCall,
764 kIntrinsified);
765 locations->SetOut(Location::RequiresRegister());
766}
767
768void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
769 codegen_->Load(Primitive::kPrimNot, WRegisterFrom(invoke->GetLocations()->Out()),
770 MemOperand(tr, Thread::PeerOffset<8>().Int32Value()));
771}
772
773static void GenUnsafeGet(HInvoke* invoke,
774 Primitive::Type type,
775 bool is_volatile,
776 CodeGeneratorARM64* codegen) {
777 LocationSummary* locations = invoke->GetLocations();
778 DCHECK((type == Primitive::kPrimInt) ||
779 (type == Primitive::kPrimLong) ||
780 (type == Primitive::kPrimNot));
781 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000782 Location base_loc = locations->InAt(1);
783 Register base = WRegisterFrom(base_loc); // Object pointer.
784 Location offset_loc = locations->InAt(2);
785 Register offset = XRegisterFrom(offset_loc); // Long offset.
786 Location trg_loc = locations->Out();
787 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800788
Roland Levillain44015862016-01-22 11:47:17 +0000789 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
790 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
791 UseScratchRegisterScope temps(masm);
792 Register temp = temps.AcquireW();
Roland Levillainbfea3352016-06-23 13:48:47 +0100793 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
794 trg_loc,
795 base,
796 /* offset */ 0U,
797 /* index */ offset_loc,
798 /* scale_factor */ 0U,
799 temp,
800 /* needs_null_check */ false,
801 is_volatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800802 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000803 // Other cases.
804 MemOperand mem_op(base.X(), offset);
805 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000806 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000807 } else {
808 codegen->Load(type, trg, mem_op);
809 }
Roland Levillain4d027112015-07-01 15:41:14 +0100810
Roland Levillain44015862016-01-22 11:47:17 +0000811 if (type == Primitive::kPrimNot) {
812 DCHECK(trg.IsW());
813 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
814 }
Roland Levillain4d027112015-07-01 15:41:14 +0100815 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800816}
817
818static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000819 bool can_call = kEmitCompilerReadBarrier &&
820 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
821 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800822 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000823 can_call ?
824 LocationSummary::kCallOnSlowPath :
825 LocationSummary::kNoCall,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800826 kIntrinsified);
827 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
828 locations->SetInAt(1, Location::RequiresRegister());
829 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100830 locations->SetOut(Location::RequiresRegister(),
831 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800832}
833
834void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
835 CreateIntIntIntToIntLocations(arena_, invoke);
836}
837void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
838 CreateIntIntIntToIntLocations(arena_, invoke);
839}
840void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
841 CreateIntIntIntToIntLocations(arena_, invoke);
842}
843void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
844 CreateIntIntIntToIntLocations(arena_, invoke);
845}
846void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
847 CreateIntIntIntToIntLocations(arena_, invoke);
848}
849void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
850 CreateIntIntIntToIntLocations(arena_, invoke);
851}
852
853void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000854 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800855}
856void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000857 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800858}
859void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000860 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800861}
862void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000863 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800864}
865void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000866 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800867}
868void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000869 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800870}
871
872static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
873 LocationSummary* locations = new (arena) LocationSummary(invoke,
874 LocationSummary::kNoCall,
875 kIntrinsified);
876 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
877 locations->SetInAt(1, Location::RequiresRegister());
878 locations->SetInAt(2, Location::RequiresRegister());
879 locations->SetInAt(3, Location::RequiresRegister());
880}
881
882void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
883 CreateIntIntIntIntToVoid(arena_, invoke);
884}
885void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
886 CreateIntIntIntIntToVoid(arena_, invoke);
887}
888void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
889 CreateIntIntIntIntToVoid(arena_, invoke);
890}
891void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
892 CreateIntIntIntIntToVoid(arena_, invoke);
893}
894void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
895 CreateIntIntIntIntToVoid(arena_, invoke);
896}
897void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
898 CreateIntIntIntIntToVoid(arena_, invoke);
899}
900void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
901 CreateIntIntIntIntToVoid(arena_, invoke);
902}
903void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
904 CreateIntIntIntIntToVoid(arena_, invoke);
905}
906void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
907 CreateIntIntIntIntToVoid(arena_, invoke);
908}
909
910static void GenUnsafePut(LocationSummary* locations,
911 Primitive::Type type,
912 bool is_volatile,
913 bool is_ordered,
914 CodeGeneratorARM64* codegen) {
915 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
916
917 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
918 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
919 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100920 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800921 MemOperand mem_op(base.X(), offset);
922
Roland Levillain4d027112015-07-01 15:41:14 +0100923 {
924 // We use a block to end the scratch scope before the write barrier, thus
925 // freeing the temporary registers so they can be used in `MarkGCCard`.
926 UseScratchRegisterScope temps(masm);
927
928 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
929 DCHECK(value.IsW());
930 Register temp = temps.AcquireW();
931 __ Mov(temp.W(), value.W());
932 codegen->GetAssembler()->PoisonHeapReference(temp.W());
933 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800934 }
Roland Levillain4d027112015-07-01 15:41:14 +0100935
936 if (is_volatile || is_ordered) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000937 codegen->StoreRelease(type, source, mem_op);
Roland Levillain4d027112015-07-01 15:41:14 +0100938 } else {
939 codegen->Store(type, source, mem_op);
940 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800941 }
942
943 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100944 bool value_can_be_null = true; // TODO: Worth finding out this information?
945 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800946 }
947}
948
949void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000950 GenUnsafePut(invoke->GetLocations(),
951 Primitive::kPrimInt,
952 /* is_volatile */ false,
953 /* is_ordered */ false,
954 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800955}
956void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000957 GenUnsafePut(invoke->GetLocations(),
958 Primitive::kPrimInt,
959 /* is_volatile */ false,
960 /* is_ordered */ true,
961 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800962}
963void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000964 GenUnsafePut(invoke->GetLocations(),
965 Primitive::kPrimInt,
966 /* is_volatile */ true,
967 /* is_ordered */ false,
968 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800969}
970void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000971 GenUnsafePut(invoke->GetLocations(),
972 Primitive::kPrimNot,
973 /* is_volatile */ false,
974 /* is_ordered */ false,
975 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800976}
977void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000978 GenUnsafePut(invoke->GetLocations(),
979 Primitive::kPrimNot,
980 /* is_volatile */ false,
981 /* is_ordered */ true,
982 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800983}
984void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000985 GenUnsafePut(invoke->GetLocations(),
986 Primitive::kPrimNot,
987 /* is_volatile */ true,
988 /* is_ordered */ false,
989 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800990}
991void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000992 GenUnsafePut(invoke->GetLocations(),
993 Primitive::kPrimLong,
994 /* is_volatile */ false,
995 /* is_ordered */ false,
996 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800997}
998void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000999 GenUnsafePut(invoke->GetLocations(),
1000 Primitive::kPrimLong,
1001 /* is_volatile */ false,
1002 /* is_ordered */ true,
1003 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001004}
1005void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001006 GenUnsafePut(invoke->GetLocations(),
1007 Primitive::kPrimLong,
1008 /* is_volatile */ true,
1009 /* is_ordered */ false,
1010 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001011}
1012
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001013static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
1014 HInvoke* invoke,
1015 Primitive::Type type) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001016 LocationSummary* locations = new (arena) LocationSummary(invoke,
1017 LocationSummary::kNoCall,
1018 kIntrinsified);
1019 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1020 locations->SetInAt(1, Location::RequiresRegister());
1021 locations->SetInAt(2, Location::RequiresRegister());
1022 locations->SetInAt(3, Location::RequiresRegister());
1023 locations->SetInAt(4, Location::RequiresRegister());
1024
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001025 // If heap poisoning is enabled, we don't want the unpoisoning
1026 // operations to potentially clobber the output.
1027 Location::OutputOverlap overlaps = (kPoisonHeapReferences && type == Primitive::kPrimNot)
1028 ? Location::kOutputOverlap
1029 : Location::kNoOutputOverlap;
1030 locations->SetOut(Location::RequiresRegister(), overlaps);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001031}
1032
1033static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorARM64* codegen) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001034 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
1035
1036 Register out = WRegisterFrom(locations->Out()); // Boolean result.
1037
1038 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1039 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1040 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1041 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1042
1043 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
1044 if (type == Primitive::kPrimNot) {
1045 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001046 bool value_can_be_null = true; // TODO: Worth finding out this information?
1047 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001048 }
1049
1050 UseScratchRegisterScope temps(masm);
1051 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1052 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1053
1054 Register tmp_32 = tmp_value.W();
1055
1056 __ Add(tmp_ptr, base.X(), Operand(offset));
1057
Roland Levillain4d027112015-07-01 15:41:14 +01001058 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1059 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001060 if (value.Is(expected)) {
1061 // Do not poison `value`, as it is the same register as
1062 // `expected`, which has just been poisoned.
1063 } else {
1064 codegen->GetAssembler()->PoisonHeapReference(value);
1065 }
Roland Levillain4d027112015-07-01 15:41:14 +01001066 }
1067
Andreas Gampe878d58c2015-01-15 23:24:00 -08001068 // do {
1069 // tmp_value = [tmp_ptr] - expected;
1070 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1071 // result = tmp_value != 0;
1072
1073 vixl::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001074 __ Bind(&loop_head);
1075 // TODO: When `type == Primitive::kPrimNot`, add a read barrier for
1076 // the reference stored in the object before attempting the CAS,
1077 // similar to the one in the art::Unsafe_compareAndSwapObject JNI
1078 // implementation.
1079 //
1080 // Note that this code is not (yet) used when read barriers are
1081 // enabled (see IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject).
1082 DCHECK(!(type == Primitive::kPrimNot && kEmitCompilerReadBarrier));
1083 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1084 __ Cmp(tmp_value, expected);
1085 __ B(&exit_loop, ne);
1086 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1087 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001088 __ Bind(&exit_loop);
1089 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001090
1091 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01001092 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001093 if (value.Is(expected)) {
1094 // Do not unpoison `value`, as it is the same register as
1095 // `expected`, which has just been unpoisoned.
1096 } else {
1097 codegen->GetAssembler()->UnpoisonHeapReference(value);
1098 }
Roland Levillain4d027112015-07-01 15:41:14 +01001099 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001100}
1101
1102void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001103 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001104}
1105void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001106 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001107}
1108void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00001109 // The UnsafeCASObject intrinsic is missing a read barrier, and
1110 // therefore sometimes does not work as expected (b/25883050).
1111 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01001112 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00001113 //
Roland Levillain3d312422016-06-23 13:53:42 +01001114 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
1115 // this intrinsic.
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001116 if (kEmitCompilerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001117 return;
1118 }
1119
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001120 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001121}
1122
1123void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
1124 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1125}
1126void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
1127 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
1128}
1129void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01001130 // The UnsafeCASObject intrinsic is missing a read barrier, and
1131 // therefore sometimes does not work as expected (b/25883050).
1132 // Turn it off temporarily as a quick fix, until the read barrier is
1133 // implemented (see TODO in GenCAS).
1134 //
1135 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
1136 // this intrinsic.
1137 DCHECK(!kEmitCompilerReadBarrier);
1138
Andreas Gampe878d58c2015-01-15 23:24:00 -08001139 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1140}
1141
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001142void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001143 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Scott Wakeling1f36f412016-04-21 11:13:45 +01001144 invoke->InputAt(1)->CanBeNull()
1145 ? LocationSummary::kCallOnSlowPath
1146 : LocationSummary::kNoCall,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001147 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001148 locations->SetInAt(0, Location::RequiresRegister());
1149 locations->SetInAt(1, Location::RequiresRegister());
1150 locations->AddTemp(Location::RequiresRegister());
1151 locations->AddTemp(Location::RequiresRegister());
1152 locations->AddTemp(Location::RequiresRegister());
1153 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001154}
1155
1156void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
1157 vixl::MacroAssembler* masm = GetVIXLAssembler();
1158 LocationSummary* locations = invoke->GetLocations();
1159
Scott Wakeling1f36f412016-04-21 11:13:45 +01001160 Register str = XRegisterFrom(locations->InAt(0));
1161 Register arg = XRegisterFrom(locations->InAt(1));
1162 Register out = OutputRegister(invoke);
1163
1164 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1165 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1166 Register temp2 = WRegisterFrom(locations->GetTemp(2));
1167
1168 vixl::Label loop;
1169 vixl::Label find_char_diff;
1170 vixl::Label end;
1171
1172 // Get offsets of count and value fields within a string object.
1173 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1174 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1175
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001176 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001177 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001178
Scott Wakeling1f36f412016-04-21 11:13:45 +01001179 // Take slow path and throw if input can be and is null.
1180 SlowPathCodeARM64* slow_path = nullptr;
1181 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1182 if (can_slow_path) {
1183 slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1184 codegen_->AddSlowPath(slow_path);
1185 __ Cbz(arg, slow_path->GetEntryLabel());
1186 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001187
Scott Wakeling1f36f412016-04-21 11:13:45 +01001188 // Reference equality check, return 0 if same reference.
1189 __ Subs(out, str, arg);
1190 __ B(&end, eq);
1191 // Load lengths of this and argument strings.
1192 __ Ldr(temp0, MemOperand(str.X(), count_offset));
1193 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1194 // Return zero if both strings are empty.
1195 __ Orr(out, temp0, temp1);
1196 __ Cbz(out, &end);
1197 // out = length diff.
1198 __ Subs(out, temp0, temp1);
1199 // temp2 = min(len(str), len(arg)).
1200 __ Csel(temp2, temp1, temp0, ge);
1201 // Shorter string is empty?
1202 __ Cbz(temp2, &end);
1203
1204 // Store offset of string value in preparation for comparison loop.
1205 __ Mov(temp1, value_offset);
1206
1207 UseScratchRegisterScope scratch_scope(masm);
1208 Register temp4 = scratch_scope.AcquireX();
1209
1210 // Assertions that must hold in order to compare strings 4 characters at a time.
1211 DCHECK_ALIGNED(value_offset, 8);
1212 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1213
1214 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1215 DCHECK_EQ(char_size, 2u);
1216
1217 // Promote temp0 to an X reg, ready for LDR.
1218 temp0 = temp0.X();
1219
1220 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1221 __ Bind(&loop);
1222 __ Ldr(temp4, MemOperand(str.X(), temp1));
1223 __ Ldr(temp0, MemOperand(arg.X(), temp1));
1224 __ Cmp(temp4, temp0);
1225 __ B(ne, &find_char_diff);
1226 __ Add(temp1, temp1, char_size * 4);
1227 __ Subs(temp2, temp2, 4);
1228 __ B(gt, &loop);
1229 __ B(&end);
1230
1231 // Promote temp1 to an X reg, ready for EOR.
1232 temp1 = temp1.X();
1233
1234 // Find the single 16-bit character difference.
1235 __ Bind(&find_char_diff);
1236 // Get the bit position of the first character that differs.
1237 __ Eor(temp1, temp0, temp4);
1238 __ Rbit(temp1, temp1);
1239 __ Clz(temp1, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001240 // If the number of 16-bit chars remaining <= the index where the difference occurs (0-3), then
1241 // the difference occurs outside the remaining string data, so just return length diff (out).
1242 __ Cmp(temp2, Operand(temp1, LSR, 4));
1243 __ B(le, &end);
1244 // Extract the characters and calculate the difference.
Scott Wakelinge5ed20b2016-05-20 10:41:38 +01001245 __ Bic(temp1, temp1, 0xf);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001246 __ Lsr(temp0, temp0, temp1);
1247 __ Lsr(temp4, temp4, temp1);
1248 __ And(temp4, temp4, 0xffff);
1249 __ Sub(out, temp4, Operand(temp0, UXTH));
1250
1251 __ Bind(&end);
1252
1253 if (can_slow_path) {
1254 __ Bind(slow_path->GetExitLabel());
1255 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001256}
1257
Agi Csakiea34b402015-08-13 17:51:19 -07001258void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
1259 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1260 LocationSummary::kNoCall,
1261 kIntrinsified);
1262 locations->SetInAt(0, Location::RequiresRegister());
1263 locations->SetInAt(1, Location::RequiresRegister());
1264 // Temporary registers to store lengths of strings and for calculations.
1265 locations->AddTemp(Location::RequiresRegister());
1266 locations->AddTemp(Location::RequiresRegister());
1267
1268 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1269}
1270
1271void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
1272 vixl::MacroAssembler* masm = GetVIXLAssembler();
1273 LocationSummary* locations = invoke->GetLocations();
1274
1275 Register str = WRegisterFrom(locations->InAt(0));
1276 Register arg = WRegisterFrom(locations->InAt(1));
1277 Register out = XRegisterFrom(locations->Out());
1278
1279 UseScratchRegisterScope scratch_scope(masm);
1280 Register temp = scratch_scope.AcquireW();
1281 Register temp1 = WRegisterFrom(locations->GetTemp(0));
1282 Register temp2 = WRegisterFrom(locations->GetTemp(1));
1283
1284 vixl::Label loop;
1285 vixl::Label end;
1286 vixl::Label return_true;
1287 vixl::Label return_false;
1288
1289 // Get offsets of count, value, and class fields within a string object.
1290 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1291 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1292 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1293
1294 // Note that the null check must have been done earlier.
1295 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1296
Vladimir Marko53b52002016-05-24 19:30:45 +01001297 StringEqualsOptimizations optimizations(invoke);
1298 if (!optimizations.GetArgumentNotNull()) {
1299 // Check if input is null, return false if it is.
1300 __ Cbz(arg, &return_false);
1301 }
Agi Csakiea34b402015-08-13 17:51:19 -07001302
1303 // Reference equality check, return true if same reference.
1304 __ Cmp(str, arg);
1305 __ B(&return_true, eq);
1306
Vladimir Marko53b52002016-05-24 19:30:45 +01001307 if (!optimizations.GetArgumentIsString()) {
1308 // Instanceof check for the argument by comparing class fields.
1309 // All string objects must have the same type since String cannot be subclassed.
1310 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1311 // If the argument is a string object, its class field must be equal to receiver's class field.
1312 __ Ldr(temp, MemOperand(str.X(), class_offset));
1313 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1314 __ Cmp(temp, temp1);
1315 __ B(&return_false, ne);
1316 }
Agi Csakiea34b402015-08-13 17:51:19 -07001317
1318 // Load lengths of this and argument strings.
1319 __ Ldr(temp, MemOperand(str.X(), count_offset));
1320 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1321 // Check if lengths are equal, return false if they're not.
1322 __ Cmp(temp, temp1);
1323 __ B(&return_false, ne);
1324 // Store offset of string value in preparation for comparison loop
1325 __ Mov(temp1, value_offset);
1326 // Return true if both strings are empty.
1327 __ Cbz(temp, &return_true);
1328
1329 // Assertions that must hold in order to compare strings 4 characters at a time.
1330 DCHECK_ALIGNED(value_offset, 8);
1331 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1332
1333 temp1 = temp1.X();
1334 temp2 = temp2.X();
1335
1336 // Loop to compare strings 4 characters at a time starting at the beginning of the string.
1337 // Ok to do this because strings are zero-padded to be 8-byte aligned.
1338 __ Bind(&loop);
1339 __ Ldr(out, MemOperand(str.X(), temp1));
1340 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1341 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1342 __ Cmp(out, temp2);
1343 __ B(&return_false, ne);
1344 __ Sub(temp, temp, Operand(4), SetFlags);
1345 __ B(&loop, gt);
1346
1347 // Return true and exit the function.
1348 // If loop does not result in returning false, we return true.
1349 __ Bind(&return_true);
1350 __ Mov(out, 1);
1351 __ B(&end);
1352
1353 // Return false and exit the function.
1354 __ Bind(&return_false);
1355 __ Mov(out, 0);
1356 __ Bind(&end);
1357}
1358
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001359static void GenerateVisitStringIndexOf(HInvoke* invoke,
1360 vixl::MacroAssembler* masm,
1361 CodeGeneratorARM64* codegen,
1362 ArenaAllocator* allocator,
1363 bool start_at_zero) {
1364 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001365
1366 // Note that the null check must have been done earlier.
1367 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1368
1369 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001370 // 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 -07001371 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001372 HInstruction* code_point = invoke->InputAt(1);
1373 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001374 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001375 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1376 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1377 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1378 codegen->AddSlowPath(slow_path);
1379 __ B(slow_path->GetEntryLabel());
1380 __ Bind(slow_path->GetExitLabel());
1381 return;
1382 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001383 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001384 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001385 __ Tst(char_reg, 0xFFFF0000);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001386 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1387 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001388 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001389 }
1390
1391 if (start_at_zero) {
1392 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001393 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001394 __ Mov(tmp_reg, 0);
1395 }
1396
1397 __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pIndexOf).Int32Value()));
Roland Levillain42ad2882016-02-29 18:26:54 +00001398 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001399 __ Blr(lr);
1400
1401 if (slow_path != nullptr) {
1402 __ Bind(slow_path->GetExitLabel());
1403 }
1404}
1405
1406void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1407 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1408 LocationSummary::kCall,
1409 kIntrinsified);
1410 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1411 // best to align the inputs accordingly.
1412 InvokeRuntimeCallingConvention calling_convention;
1413 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1414 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1415 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1416
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001417 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001418 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1419}
1420
1421void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001422 GenerateVisitStringIndexOf(
1423 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001424}
1425
1426void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1427 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1428 LocationSummary::kCall,
1429 kIntrinsified);
1430 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1431 // best to align the inputs accordingly.
1432 InvokeRuntimeCallingConvention calling_convention;
1433 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1434 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1435 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1436 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001437}
1438
1439void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001440 GenerateVisitStringIndexOf(
1441 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001442}
1443
Jeff Hao848f70a2014-01-15 13:49:50 -08001444void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1445 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1446 LocationSummary::kCall,
1447 kIntrinsified);
1448 InvokeRuntimeCallingConvention calling_convention;
1449 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1450 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1451 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1452 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1453 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1454}
1455
1456void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1457 vixl::MacroAssembler* masm = GetVIXLAssembler();
1458 LocationSummary* locations = invoke->GetLocations();
1459
1460 Register byte_array = WRegisterFrom(locations->InAt(0));
1461 __ Cmp(byte_array, 0);
1462 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1463 codegen_->AddSlowPath(slow_path);
1464 __ B(eq, slow_path->GetEntryLabel());
1465
1466 __ Ldr(lr,
1467 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromBytes).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001468 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001469 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001470 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001471 __ Bind(slow_path->GetExitLabel());
1472}
1473
1474void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1475 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1476 LocationSummary::kCall,
1477 kIntrinsified);
1478 InvokeRuntimeCallingConvention calling_convention;
1479 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1480 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1481 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1482 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1483}
1484
1485void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1486 vixl::MacroAssembler* masm = GetVIXLAssembler();
1487
Roland Levillaincc3839c2016-02-29 16:23:48 +00001488 // No need to emit code checking whether `locations->InAt(2)` is a null
1489 // pointer, as callers of the native method
1490 //
1491 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1492 //
1493 // all include a null check on `data` before calling that method.
Jeff Hao848f70a2014-01-15 13:49:50 -08001494 __ Ldr(lr,
1495 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromChars).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001496 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001497 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001498 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001499}
1500
1501void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001502 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1503 LocationSummary::kCall,
1504 kIntrinsified);
1505 InvokeRuntimeCallingConvention calling_convention;
1506 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Jeff Hao848f70a2014-01-15 13:49:50 -08001507 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1508}
1509
1510void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
1511 vixl::MacroAssembler* masm = GetVIXLAssembler();
1512 LocationSummary* locations = invoke->GetLocations();
1513
1514 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1515 __ Cmp(string_to_copy, 0);
1516 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1517 codegen_->AddSlowPath(slow_path);
1518 __ B(eq, slow_path->GetEntryLabel());
1519
1520 __ Ldr(lr,
1521 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromString).Int32Value()));
Roland Levillainf969a202016-03-09 16:14:00 +00001522 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001523 __ Blr(lr);
Roland Levillainf969a202016-03-09 16:14:00 +00001524 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Jeff Hao848f70a2014-01-15 13:49:50 -08001525 __ Bind(slow_path->GetExitLabel());
1526}
1527
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001528static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1529 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
1530 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1531 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1532
1533 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1534 LocationSummary::kCall,
1535 kIntrinsified);
1536 InvokeRuntimeCallingConvention calling_convention;
1537
1538 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1539 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1540}
1541
1542static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1543 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
1544 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1545 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1546 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1547
1548 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1549 LocationSummary::kCall,
1550 kIntrinsified);
1551 InvokeRuntimeCallingConvention calling_convention;
1552
1553 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1554 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1555 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1556}
1557
1558static void GenFPToFPCall(HInvoke* invoke,
1559 vixl::MacroAssembler* masm,
1560 CodeGeneratorARM64* codegen,
1561 QuickEntrypointEnum entry) {
1562 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64WordSize>(entry).Int32Value()));
1563 __ Blr(lr);
1564 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
1565}
1566
1567void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
1568 CreateFPToFPCallLocations(arena_, invoke);
1569}
1570
1571void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
1572 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCos);
1573}
1574
1575void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
1576 CreateFPToFPCallLocations(arena_, invoke);
1577}
1578
1579void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
1580 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSin);
1581}
1582
1583void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
1584 CreateFPToFPCallLocations(arena_, invoke);
1585}
1586
1587void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
1588 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAcos);
1589}
1590
1591void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
1592 CreateFPToFPCallLocations(arena_, invoke);
1593}
1594
1595void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
1596 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAsin);
1597}
1598
1599void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
1600 CreateFPToFPCallLocations(arena_, invoke);
1601}
1602
1603void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
1604 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan);
1605}
1606
1607void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
1608 CreateFPToFPCallLocations(arena_, invoke);
1609}
1610
1611void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
1612 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCbrt);
1613}
1614
1615void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
1616 CreateFPToFPCallLocations(arena_, invoke);
1617}
1618
1619void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
1620 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCosh);
1621}
1622
1623void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
1624 CreateFPToFPCallLocations(arena_, invoke);
1625}
1626
1627void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
1628 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExp);
1629}
1630
1631void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
1632 CreateFPToFPCallLocations(arena_, invoke);
1633}
1634
1635void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
1636 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExpm1);
1637}
1638
1639void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
1640 CreateFPToFPCallLocations(arena_, invoke);
1641}
1642
1643void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
1644 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog);
1645}
1646
1647void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
1648 CreateFPToFPCallLocations(arena_, invoke);
1649}
1650
1651void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
1652 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog10);
1653}
1654
1655void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
1656 CreateFPToFPCallLocations(arena_, invoke);
1657}
1658
1659void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
1660 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSinh);
1661}
1662
1663void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
1664 CreateFPToFPCallLocations(arena_, invoke);
1665}
1666
1667void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
1668 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTan);
1669}
1670
1671void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
1672 CreateFPToFPCallLocations(arena_, invoke);
1673}
1674
1675void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
1676 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTanh);
1677}
1678
1679void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
1680 CreateFPFPToFPCallLocations(arena_, invoke);
1681}
1682
1683void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
1684 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan2);
1685}
1686
1687void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
1688 CreateFPFPToFPCallLocations(arena_, invoke);
1689}
1690
1691void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
1692 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickHypot);
1693}
1694
1695void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
1696 CreateFPFPToFPCallLocations(arena_, invoke);
1697}
1698
1699void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
1700 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickNextAfter);
1701}
1702
Tim Zhang25abd6c2016-01-19 23:39:24 +08001703void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1704 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1705 LocationSummary::kNoCall,
1706 kIntrinsified);
1707 locations->SetInAt(0, Location::RequiresRegister());
1708 locations->SetInAt(1, Location::RequiresRegister());
1709 locations->SetInAt(2, Location::RequiresRegister());
1710 locations->SetInAt(3, Location::RequiresRegister());
1711 locations->SetInAt(4, Location::RequiresRegister());
1712
1713 locations->AddTemp(Location::RequiresRegister());
1714 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01001715 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08001716}
1717
1718void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1719 vixl::MacroAssembler* masm = GetVIXLAssembler();
1720 LocationSummary* locations = invoke->GetLocations();
1721
1722 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1723 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1724 DCHECK_EQ(char_size, 2u);
1725
1726 // Location of data in char array buffer.
1727 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1728
1729 // Location of char array data in string.
1730 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1731
1732 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1733 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
1734 Register srcObj = XRegisterFrom(locations->InAt(0));
1735 Register srcBegin = XRegisterFrom(locations->InAt(1));
1736 Register srcEnd = XRegisterFrom(locations->InAt(2));
1737 Register dstObj = XRegisterFrom(locations->InAt(3));
1738 Register dstBegin = XRegisterFrom(locations->InAt(4));
1739
1740 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001741 Register num_chr = XRegisterFrom(locations->GetTemp(1));
1742 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001743
1744 UseScratchRegisterScope temps(masm);
1745 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01001746 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001747
Scott Wakelingdf109d92016-04-22 11:35:56 +01001748 // src address to copy from.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001749 __ Add(src_ptr, srcObj, Operand(value_offset));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001750 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
1751
Scott Wakelingdf109d92016-04-22 11:35:56 +01001752 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001753 __ Add(dst_ptr, dstObj, Operand(data_offset));
1754 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
1755
Scott Wakelingdf109d92016-04-22 11:35:56 +01001756 __ Sub(num_chr, srcEnd, srcBegin);
1757
Tim Zhang25abd6c2016-01-19 23:39:24 +08001758 // Do the copy.
Scott Wakelingdf109d92016-04-22 11:35:56 +01001759 vixl::Label loop;
1760 vixl::Label done;
1761 vixl::Label remainder;
1762
1763 // Early out for valid zero-length retrievals.
1764 __ Cbz(num_chr, &done);
1765
1766 // Save repairing the value of num_chr on the < 8 character path.
1767 __ Subs(tmp1, num_chr, 8);
1768 __ B(lt, &remainder);
1769
1770 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
1771 __ Mov(num_chr, tmp1);
1772
1773 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
1774 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08001775 __ Bind(&loop);
Scott Wakelingdf109d92016-04-22 11:35:56 +01001776 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, vixl::PostIndex));
1777 __ Subs(num_chr, num_chr, 8);
1778 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, vixl::PostIndex));
1779 __ B(ge, &loop);
1780
1781 __ Adds(num_chr, num_chr, 8);
1782 __ B(eq, &done);
1783
1784 // Main loop for < 8 character case and remainder handling. Loads and stores one
1785 // 16-bit Java character at a time.
1786 __ Bind(&remainder);
1787 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, vixl::PostIndex));
1788 __ Subs(num_chr, num_chr, 1);
1789 __ Strh(tmp1, MemOperand(dst_ptr, char_size, vixl::PostIndex));
1790 __ B(gt, &remainder);
1791
Tim Zhang25abd6c2016-01-19 23:39:24 +08001792 __ Bind(&done);
1793}
1794
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001795// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
1796// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001797static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001798
1799static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
1800 uint32_t at,
1801 HInstruction* input) {
1802 HIntConstant* const_input = input->AsIntConstant();
1803 if (const_input != nullptr && !vixl::Assembler::IsImmAddSub(const_input->GetValue())) {
1804 locations->SetInAt(at, Location::RequiresRegister());
1805 } else {
1806 locations->SetInAt(at, Location::RegisterOrConstant(input));
1807 }
1808}
1809
1810void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1811 // Check to see if we have known failures that will cause us to have to bail out
1812 // to the runtime, and just generate the runtime call directly.
1813 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1814 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
1815
1816 // The positions must be non-negative.
1817 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1818 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
1819 // We will have to fail anyways.
1820 return;
1821 }
1822
1823 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
1824 // native implementation.
1825 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1826 if (length != nullptr) {
1827 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08001828 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001829 // Just call as normal.
1830 return;
1831 }
1832 }
1833
1834 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1835 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1836 LocationSummary::kCallOnSlowPath,
1837 kIntrinsified);
1838 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
1839 locations->SetInAt(0, Location::RequiresRegister());
1840 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
1841 locations->SetInAt(2, Location::RequiresRegister());
1842 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
1843 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
1844
1845 locations->AddTemp(Location::RequiresRegister());
1846 locations->AddTemp(Location::RequiresRegister());
1847 locations->AddTemp(Location::RequiresRegister());
1848}
1849
1850static void CheckSystemArrayCopyPosition(vixl::MacroAssembler* masm,
1851 const Location& pos,
1852 const Register& input,
1853 const Location& length,
1854 SlowPathCodeARM64* slow_path,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001855 const Register& temp,
1856 bool length_is_input_length = false) {
1857 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
1858 if (pos.IsConstant()) {
1859 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1860 if (pos_const == 0) {
1861 if (!length_is_input_length) {
1862 // Check that length(input) >= length.
1863 __ Ldr(temp, MemOperand(input, length_offset));
1864 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1865 __ B(slow_path->GetEntryLabel(), lt);
1866 }
1867 } else {
1868 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001869 __ Ldr(temp, MemOperand(input, length_offset));
1870 __ Subs(temp, temp, pos_const);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001871 __ B(slow_path->GetEntryLabel(), lt);
1872
1873 // Check that (length(input) - pos) >= length.
1874 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
1875 __ B(slow_path->GetEntryLabel(), lt);
1876 }
1877 } else if (length_is_input_length) {
1878 // The only way the copy can succeed is if pos is zero.
1879 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
1880 } else {
1881 // Check that pos >= 0.
1882 Register pos_reg = WRegisterFrom(pos);
1883 __ Tbnz(pos_reg, pos_reg.size() - 1, slow_path->GetEntryLabel());
1884
1885 // Check that pos <= length(input) && (length(input) - pos) >= length.
1886 __ Ldr(temp, MemOperand(input, length_offset));
1887 __ Subs(temp, temp, pos_reg);
1888 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
1889 __ Ccmp(temp, OperandFrom(length, Primitive::kPrimInt), NFlag, ge);
1890 __ B(slow_path->GetEntryLabel(), lt);
1891 }
1892}
1893
1894// Compute base source address, base destination address, and end source address
1895// for System.arraycopy* intrinsics.
1896static void GenSystemArrayCopyAddresses(vixl::MacroAssembler* masm,
1897 Primitive::Type type,
1898 const Register& src,
1899 const Location& src_pos,
1900 const Register& dst,
1901 const Location& dst_pos,
1902 const Location& copy_length,
1903 const Register& src_base,
1904 const Register& dst_base,
1905 const Register& src_end) {
1906 DCHECK(type == Primitive::kPrimNot || type == Primitive::kPrimChar)
Roland Levillainebea3d22016-04-12 15:42:57 +01001907 << "Unexpected element type: " << type;
1908 const int32_t element_size = Primitive::ComponentSize(type);
1909 const int32_t element_size_shift = Primitive::ComponentSizeShift(type);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001910
Roland Levillainebea3d22016-04-12 15:42:57 +01001911 uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001912 if (src_pos.IsConstant()) {
1913 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001914 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001915 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001916 __ Add(src_base, src, data_offset);
1917 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001918 }
1919
1920 if (dst_pos.IsConstant()) {
1921 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001922 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001923 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001924 __ Add(dst_base, dst, data_offset);
1925 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001926 }
1927
1928 if (copy_length.IsConstant()) {
1929 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01001930 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001931 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01001932 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001933 }
1934}
1935
1936void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1937 vixl::MacroAssembler* masm = GetVIXLAssembler();
1938 LocationSummary* locations = invoke->GetLocations();
1939 Register src = XRegisterFrom(locations->InAt(0));
1940 Location src_pos = locations->InAt(1);
1941 Register dst = XRegisterFrom(locations->InAt(2));
1942 Location dst_pos = locations->InAt(3);
1943 Location length = locations->InAt(4);
1944
1945 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1946 codegen_->AddSlowPath(slow_path);
1947
1948 // If source and destination are the same, take the slow path. Overlapping copy regions must be
1949 // copied in reverse and we can't know in all cases if it's needed.
1950 __ Cmp(src, dst);
1951 __ B(slow_path->GetEntryLabel(), eq);
1952
1953 // Bail out if the source is null.
1954 __ Cbz(src, slow_path->GetEntryLabel());
1955
1956 // Bail out if the destination is null.
1957 __ Cbz(dst, slow_path->GetEntryLabel());
1958
1959 if (!length.IsConstant()) {
1960 // If the length is negative, bail out.
1961 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, slow_path->GetEntryLabel());
1962 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001963 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001964 __ B(slow_path->GetEntryLabel(), gt);
1965 } else {
1966 // We have already checked in the LocationsBuilder for the constant case.
1967 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
1968 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
1969 }
1970
1971 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
1972 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
1973 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
1974
1975 CheckSystemArrayCopyPosition(masm,
1976 src_pos,
1977 src,
1978 length,
1979 slow_path,
1980 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001981 false);
1982
1983 CheckSystemArrayCopyPosition(masm,
1984 dst_pos,
1985 dst,
1986 length,
1987 slow_path,
1988 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001989 false);
1990
1991 src_curr_addr = src_curr_addr.X();
1992 dst_curr_addr = dst_curr_addr.X();
1993 src_stop_addr = src_stop_addr.X();
1994
1995 GenSystemArrayCopyAddresses(masm,
1996 Primitive::kPrimChar,
1997 src,
1998 src_pos,
1999 dst,
2000 dst_pos,
2001 length,
2002 src_curr_addr,
2003 dst_curr_addr,
2004 src_stop_addr);
2005
2006 // Iterate over the arrays and do a raw copy of the chars.
2007 const int32_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2008 UseScratchRegisterScope temps(masm);
2009 Register tmp = temps.AcquireW();
2010 vixl::Label loop, done;
2011 __ Bind(&loop);
2012 __ Cmp(src_curr_addr, src_stop_addr);
2013 __ B(&done, eq);
2014 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, vixl::PostIndex));
2015 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, vixl::PostIndex));
2016 __ B(&loop);
2017 __ Bind(&done);
2018
2019 __ Bind(slow_path->GetExitLabel());
2020}
2021
donghui.baic2ec9ad2016-03-10 14:02:55 +08002022// We can choose to use the native implementation there for longer copy lengths.
2023static constexpr int32_t kSystemArrayCopyThreshold = 128;
2024
2025// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2026// We want to use two temporary registers in order to reduce the register pressure in arm64.
2027// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2028void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002029 // TODO(rpl): Implement read barriers in the SystemArrayCopy
2030 // intrinsic and re-enable it (b/29516905).
2031 if (kEmitCompilerReadBarrier) {
2032 return;
2033 }
2034
donghui.baic2ec9ad2016-03-10 14:02:55 +08002035 // Check to see if we have known failures that will cause us to have to bail out
2036 // to the runtime, and just generate the runtime call directly.
2037 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2038 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2039
2040 // The positions must be non-negative.
2041 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2042 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2043 // We will have to fail anyways.
2044 return;
2045 }
2046
2047 // The length must be >= 0.
2048 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2049 if (length != nullptr) {
2050 int32_t len = length->GetValue();
2051 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2052 // Just call as normal.
2053 return;
2054 }
2055 }
2056
2057 SystemArrayCopyOptimizations optimizations(invoke);
2058
2059 if (optimizations.GetDestinationIsSource()) {
2060 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2061 // We only support backward copying if source and destination are the same.
2062 return;
2063 }
2064 }
2065
2066 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2067 // We currently don't intrinsify primitive copying.
2068 return;
2069 }
2070
2071 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2072 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2073 LocationSummary::kCallOnSlowPath,
2074 kIntrinsified);
2075 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2076 locations->SetInAt(0, Location::RequiresRegister());
2077 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2078 locations->SetInAt(2, Location::RequiresRegister());
2079 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2080 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2081
2082 locations->AddTemp(Location::RequiresRegister());
2083 locations->AddTemp(Location::RequiresRegister());
2084}
2085
2086void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002087 // TODO(rpl): Implement read barriers in the SystemArrayCopy
2088 // intrinsic and re-enable it (b/29516905).
2089 DCHECK(!kEmitCompilerReadBarrier);
2090
donghui.baic2ec9ad2016-03-10 14:02:55 +08002091 vixl::MacroAssembler* masm = GetVIXLAssembler();
2092 LocationSummary* locations = invoke->GetLocations();
2093
2094 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2095 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2096 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2097 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
2098
2099 Register src = XRegisterFrom(locations->InAt(0));
2100 Location src_pos = locations->InAt(1);
2101 Register dest = XRegisterFrom(locations->InAt(2));
2102 Location dest_pos = locations->InAt(3);
2103 Location length = locations->InAt(4);
2104 Register temp1 = WRegisterFrom(locations->GetTemp(0));
2105 Register temp2 = WRegisterFrom(locations->GetTemp(1));
2106
2107 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2108 codegen_->AddSlowPath(slow_path);
2109
2110 vixl::Label conditions_on_positions_validated;
2111 SystemArrayCopyOptimizations optimizations(invoke);
2112
donghui.baic2ec9ad2016-03-10 14:02:55 +08002113 // If source and destination are the same, we go to slow path if we need to do
2114 // forward copying.
2115 if (src_pos.IsConstant()) {
2116 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2117 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002118 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2119 if (optimizations.GetDestinationIsSource()) {
2120 // Checked when building locations.
2121 DCHECK_GE(src_pos_constant, dest_pos_constant);
2122 } else if (src_pos_constant < dest_pos_constant) {
2123 __ Cmp(src, dest);
2124 __ B(slow_path->GetEntryLabel(), eq);
2125 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002126 // Checked when building locations.
2127 DCHECK(!optimizations.GetDestinationIsSource()
2128 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2129 } else {
2130 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002131 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002132 __ B(&conditions_on_positions_validated, ne);
2133 }
2134 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
2135 __ B(slow_path->GetEntryLabel(), gt);
2136 }
2137 } else {
2138 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002139 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002140 __ B(&conditions_on_positions_validated, ne);
2141 }
2142 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2143 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
2144 __ B(slow_path->GetEntryLabel(), lt);
2145 }
2146
2147 __ Bind(&conditions_on_positions_validated);
2148
2149 if (!optimizations.GetSourceIsNotNull()) {
2150 // Bail out if the source is null.
2151 __ Cbz(src, slow_path->GetEntryLabel());
2152 }
2153
2154 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2155 // Bail out if the destination is null.
2156 __ Cbz(dest, slow_path->GetEntryLabel());
2157 }
2158
2159 // We have already checked in the LocationsBuilder for the constant case.
2160 if (!length.IsConstant() &&
2161 !optimizations.GetCountIsSourceLength() &&
2162 !optimizations.GetCountIsDestinationLength()) {
2163 // If the length is negative, bail out.
2164 __ Tbnz(WRegisterFrom(length), kWRegSize - 1, slow_path->GetEntryLabel());
2165 // If the length >= 128 then (currently) prefer native implementation.
2166 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
2167 __ B(slow_path->GetEntryLabel(), ge);
2168 }
2169 // Validity checks: source.
2170 CheckSystemArrayCopyPosition(masm,
2171 src_pos,
2172 src,
2173 length,
2174 slow_path,
2175 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002176 optimizations.GetCountIsSourceLength());
2177
2178 // Validity checks: dest.
2179 CheckSystemArrayCopyPosition(masm,
2180 dest_pos,
2181 dest,
2182 length,
2183 slow_path,
2184 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002185 optimizations.GetCountIsDestinationLength());
2186 {
2187 // We use a block to end the scratch scope before the write barrier, thus
2188 // freeing the temporary registers so they can be used in `MarkGCCard`.
2189 UseScratchRegisterScope temps(masm);
2190 Register temp3 = temps.AcquireW();
2191 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2192 // Check whether all elements of the source array are assignable to the component
2193 // type of the destination array. We do two checks: the classes are the same,
2194 // or the destination is Object[]. If none of these checks succeed, we go to the
2195 // slow path.
2196 __ Ldr(temp1, MemOperand(dest, class_offset));
2197 __ Ldr(temp2, MemOperand(src, class_offset));
2198 bool did_unpoison = false;
2199 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2200 !optimizations.GetSourceIsNonPrimitiveArray()) {
2201 // One or two of the references need to be unpoisoned. Unpoison them
2202 // both to make the identity check valid.
2203 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2204 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2205 did_unpoison = true;
2206 }
2207
2208 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2209 // Bail out if the destination is not a non primitive array.
2210 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2211 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2212 __ Cbz(temp3, slow_path->GetEntryLabel());
2213 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2214 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2215 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2216 __ Cbnz(temp3, slow_path->GetEntryLabel());
2217 }
2218
2219 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2220 // Bail out if the source is not a non primitive array.
2221 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2222 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2223 __ Cbz(temp3, slow_path->GetEntryLabel());
2224 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2225 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2226 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2227 __ Cbnz(temp3, slow_path->GetEntryLabel());
2228 }
2229
2230 __ Cmp(temp1, temp2);
2231
2232 if (optimizations.GetDestinationIsTypedObjectArray()) {
2233 vixl::Label do_copy;
2234 __ B(&do_copy, eq);
2235 if (!did_unpoison) {
2236 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2237 }
2238 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2239 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2240 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2241 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2242 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2243 // No need to unpoison the result, we're comparing against null.
2244 __ Cbnz(temp1, slow_path->GetEntryLabel());
2245 __ Bind(&do_copy);
2246 } else {
2247 __ B(slow_path->GetEntryLabel(), ne);
2248 }
2249 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2250 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2251 // Bail out if the source is not a non primitive array.
2252 // /* HeapReference<Class> */ temp1 = src->klass_
2253 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2254 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2255 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2256 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2257 __ Cbz(temp3, slow_path->GetEntryLabel());
2258 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2259 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2260 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2261 __ Cbnz(temp3, slow_path->GetEntryLabel());
2262 }
2263
2264 Register src_curr_addr = temp1.X();
2265 Register dst_curr_addr = temp2.X();
2266 Register src_stop_addr = temp3.X();
2267
2268 GenSystemArrayCopyAddresses(masm,
2269 Primitive::kPrimNot,
2270 src,
2271 src_pos,
2272 dest,
2273 dest_pos,
2274 length,
2275 src_curr_addr,
2276 dst_curr_addr,
2277 src_stop_addr);
2278
2279 // Iterate over the arrays and do a raw copy of the objects. We don't need to
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002280 // poison/unpoison.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002281 vixl::Label loop, done;
2282 const int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
2283 __ Bind(&loop);
2284 __ Cmp(src_curr_addr, src_stop_addr);
2285 __ B(&done, eq);
2286 {
2287 Register tmp = temps.AcquireW();
2288 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, vixl::PostIndex));
2289 __ Str(tmp, MemOperand(dst_curr_addr, element_size, vixl::PostIndex));
2290 }
2291 __ B(&loop);
2292 __ Bind(&done);
2293 }
2294 // We only need one card marking on the destination array.
2295 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2296
2297 __ Bind(slow_path->GetExitLabel());
2298}
2299
Anton Kirilova3ffea22016-04-07 17:02:37 +01002300static void GenIsInfinite(LocationSummary* locations,
2301 bool is64bit,
2302 vixl::MacroAssembler* masm) {
2303 Operand infinity;
2304 Register out;
2305
2306 if (is64bit) {
2307 infinity = kPositiveInfinityDouble;
2308 out = XRegisterFrom(locations->Out());
2309 } else {
2310 infinity = kPositiveInfinityFloat;
2311 out = WRegisterFrom(locations->Out());
2312 }
2313
2314 const Register zero = vixl::Assembler::AppropriateZeroRegFor(out);
2315
2316 MoveFPToInt(locations, is64bit, masm);
2317 __ Eor(out, out, infinity);
2318 // We don't care about the sign bit, so shift left.
2319 __ Cmp(zero, Operand(out, LSL, 1));
2320 __ Cset(out, eq);
2321}
2322
2323void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2324 CreateFPToIntLocations(arena_, invoke);
2325}
2326
2327void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2328 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
2329}
2330
2331void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2332 CreateFPToIntLocations(arena_, invoke);
2333}
2334
2335void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2336 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
2337}
2338
Aart Bik2f9fcc92016-03-01 15:16:54 -08002339UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002340UNIMPLEMENTED_INTRINSIC(ARM64, IntegerHighestOneBit)
2341UNIMPLEMENTED_INTRINSIC(ARM64, LongHighestOneBit)
2342UNIMPLEMENTED_INTRINSIC(ARM64, IntegerLowestOneBit)
2343UNIMPLEMENTED_INTRINSIC(ARM64, LongLowestOneBit)
Andreas Gampe878d58c2015-01-15 23:24:00 -08002344
Aart Bik0e54c012016-03-04 12:08:31 -08002345// 1.8.
2346UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
2347UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
2348UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
2349UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
2350UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002351
Aart Bik2f9fcc92016-03-01 15:16:54 -08002352UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01002353
2354#undef __
2355
Andreas Gampe878d58c2015-01-15 23:24:00 -08002356} // namespace arm64
2357} // namespace art