blob: 78ac167a8743e69c11061074c9cd669ef779ee06 [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;
Andreas Gampe878d58c2015-01-15 23:24:00 -080044using helpers::RegisterFrom;
45using helpers::SRegisterFrom;
46using helpers::WRegisterFrom;
47using helpers::XRegisterFrom;
48
49
50namespace {
51
52ALWAYS_INLINE inline MemOperand AbsoluteHeapOperandFrom(Location location, size_t offset = 0) {
53 return MemOperand(XRegisterFrom(location), offset);
54}
55
56} // namespace
57
58vixl::MacroAssembler* IntrinsicCodeGeneratorARM64::GetVIXLAssembler() {
59 return codegen_->GetAssembler()->vixl_masm_;
60}
61
62ArenaAllocator* IntrinsicCodeGeneratorARM64::GetAllocator() {
63 return codegen_->GetGraph()->GetArena();
64}
65
66#define __ codegen->GetAssembler()->vixl_masm_->
67
68static void MoveFromReturnRegister(Location trg,
69 Primitive::Type type,
70 CodeGeneratorARM64* codegen) {
71 if (!trg.IsValid()) {
72 DCHECK(type == Primitive::kPrimVoid);
73 return;
74 }
75
76 DCHECK_NE(type, Primitive::kPrimVoid);
77
Jeff Hao848f70a2014-01-15 13:49:50 -080078 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080079 Register trg_reg = RegisterFrom(trg, type);
80 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
81 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
82 } else {
83 FPRegister trg_reg = FPRegisterFrom(trg, type);
84 FPRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
85 __ Fmov(trg_reg, res_reg);
86 }
87}
88
Roland Levillainec525fc2015-04-28 15:50:20 +010089static void MoveArguments(HInvoke* invoke, CodeGeneratorARM64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010090 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010091 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe878d58c2015-01-15 23:24:00 -080092}
93
94// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
95// call. This will copy the arguments into the positions for a regular call.
96//
97// Note: The actual parameters are required to be in the locations given by the invoke's location
98// summary. If an intrinsic modifies those locations before a slowpath call, they must be
99// restored!
100class IntrinsicSlowPathARM64 : public SlowPathCodeARM64 {
101 public:
102 explicit IntrinsicSlowPathARM64(HInvoke* invoke) : invoke_(invoke) { }
103
104 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
105 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
106 __ Bind(GetEntryLabel());
107
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000108 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800109
Roland Levillainec525fc2015-04-28 15:50:20 +0100110 MoveArguments(invoke_, codegen);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800111
112 if (invoke_->IsInvokeStaticOrDirect()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100113 codegen->GenerateStaticOrDirectCall(invoke_->AsInvokeStaticOrDirect(),
114 LocationFrom(kArtMethodRegister));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000115 RecordPcInfo(codegen, invoke_, invoke_->GetDexPc());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800116 } else {
117 UNIMPLEMENTED(FATAL) << "Non-direct intrinsic slow-path not yet implemented";
118 UNREACHABLE();
119 }
120
121 // Copy the result back to the expected output.
122 Location out = invoke_->GetLocations()->Out();
123 if (out.IsValid()) {
124 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
125 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
126 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
127 }
128
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000129 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800130 __ B(GetExitLabel());
131 }
132
Alexandre Rames9931f312015-06-19 14:47:01 +0100133 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathARM64"; }
134
Andreas Gampe878d58c2015-01-15 23:24:00 -0800135 private:
136 // The instruction where this slow path is happening.
137 HInvoke* const invoke_;
138
139 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARM64);
140};
141
142#undef __
143
144bool IntrinsicLocationsBuilderARM64::TryDispatch(HInvoke* invoke) {
145 Dispatch(invoke);
146 LocationSummary* res = invoke->GetLocations();
147 return res != nullptr && res->Intrinsified();
148}
149
150#define __ masm->
151
152static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
153 LocationSummary* locations = new (arena) LocationSummary(invoke,
154 LocationSummary::kNoCall,
155 kIntrinsified);
156 locations->SetInAt(0, Location::RequiresFpuRegister());
157 locations->SetOut(Location::RequiresRegister());
158}
159
160static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
161 LocationSummary* locations = new (arena) LocationSummary(invoke,
162 LocationSummary::kNoCall,
163 kIntrinsified);
164 locations->SetInAt(0, Location::RequiresRegister());
165 locations->SetOut(Location::RequiresFpuRegister());
166}
167
168static void MoveFPToInt(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
169 Location input = locations->InAt(0);
170 Location output = locations->Out();
171 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
172 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
173}
174
175static void MoveIntToFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
176 Location input = locations->InAt(0);
177 Location output = locations->Out();
178 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
179 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
180}
181
182void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
183 CreateFPToIntLocations(arena_, invoke);
184}
185void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
186 CreateIntToFPLocations(arena_, invoke);
187}
188
189void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
190 MoveFPToInt(invoke->GetLocations(), true, GetVIXLAssembler());
191}
192void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
193 MoveIntToFP(invoke->GetLocations(), true, GetVIXLAssembler());
194}
195
196void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
197 CreateFPToIntLocations(arena_, invoke);
198}
199void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
200 CreateIntToFPLocations(arena_, invoke);
201}
202
203void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
204 MoveFPToInt(invoke->GetLocations(), false, GetVIXLAssembler());
205}
206void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
207 MoveIntToFP(invoke->GetLocations(), false, GetVIXLAssembler());
208}
209
210static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
211 LocationSummary* locations = new (arena) LocationSummary(invoke,
212 LocationSummary::kNoCall,
213 kIntrinsified);
214 locations->SetInAt(0, Location::RequiresRegister());
215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
216}
217
218static void GenReverseBytes(LocationSummary* locations,
219 Primitive::Type type,
220 vixl::MacroAssembler* masm) {
221 Location in = locations->InAt(0);
222 Location out = locations->Out();
223
224 switch (type) {
225 case Primitive::kPrimShort:
226 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
227 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
228 break;
229 case Primitive::kPrimInt:
230 case Primitive::kPrimLong:
231 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
232 break;
233 default:
234 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
235 UNREACHABLE();
236 }
237}
238
239void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
240 CreateIntToIntLocations(arena_, invoke);
241}
242
243void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
244 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
245}
246
247void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
248 CreateIntToIntLocations(arena_, invoke);
249}
250
251void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
252 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
253}
254
255void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
256 CreateIntToIntLocations(arena_, invoke);
257}
258
259void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
260 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetVIXLAssembler());
261}
262
263static void GenReverse(LocationSummary* locations,
264 Primitive::Type type,
265 vixl::MacroAssembler* masm) {
266 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
267
268 Location in = locations->InAt(0);
269 Location out = locations->Out();
270
271 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
272}
273
274void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
275 CreateIntToIntLocations(arena_, invoke);
276}
277
278void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
279 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
280}
281
282void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
283 CreateIntToIntLocations(arena_, invoke);
284}
285
286void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
287 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
288}
289
290static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800291 LocationSummary* locations = new (arena) LocationSummary(invoke,
292 LocationSummary::kNoCall,
293 kIntrinsified);
294 locations->SetInAt(0, Location::RequiresFpuRegister());
295 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
296}
297
298static void MathAbsFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
299 Location in = locations->InAt(0);
300 Location out = locations->Out();
301
302 FPRegister in_reg = is64bit ? DRegisterFrom(in) : SRegisterFrom(in);
303 FPRegister out_reg = is64bit ? DRegisterFrom(out) : SRegisterFrom(out);
304
305 __ Fabs(out_reg, in_reg);
306}
307
308void IntrinsicLocationsBuilderARM64::VisitMathAbsDouble(HInvoke* invoke) {
309 CreateFPToFPLocations(arena_, invoke);
310}
311
312void IntrinsicCodeGeneratorARM64::VisitMathAbsDouble(HInvoke* invoke) {
313 MathAbsFP(invoke->GetLocations(), true, GetVIXLAssembler());
314}
315
316void IntrinsicLocationsBuilderARM64::VisitMathAbsFloat(HInvoke* invoke) {
317 CreateFPToFPLocations(arena_, invoke);
318}
319
320void IntrinsicCodeGeneratorARM64::VisitMathAbsFloat(HInvoke* invoke) {
321 MathAbsFP(invoke->GetLocations(), false, GetVIXLAssembler());
322}
323
324static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
325 LocationSummary* locations = new (arena) LocationSummary(invoke,
326 LocationSummary::kNoCall,
327 kIntrinsified);
328 locations->SetInAt(0, Location::RequiresRegister());
329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
330}
331
332static void GenAbsInteger(LocationSummary* locations,
333 bool is64bit,
334 vixl::MacroAssembler* masm) {
335 Location in = locations->InAt(0);
336 Location output = locations->Out();
337
338 Register in_reg = is64bit ? XRegisterFrom(in) : WRegisterFrom(in);
339 Register out_reg = is64bit ? XRegisterFrom(output) : WRegisterFrom(output);
340
341 __ Cmp(in_reg, Operand(0));
342 __ Cneg(out_reg, in_reg, lt);
343}
344
345void IntrinsicLocationsBuilderARM64::VisitMathAbsInt(HInvoke* invoke) {
346 CreateIntToInt(arena_, invoke);
347}
348
349void IntrinsicCodeGeneratorARM64::VisitMathAbsInt(HInvoke* invoke) {
350 GenAbsInteger(invoke->GetLocations(), false, GetVIXLAssembler());
351}
352
353void IntrinsicLocationsBuilderARM64::VisitMathAbsLong(HInvoke* invoke) {
354 CreateIntToInt(arena_, invoke);
355}
356
357void IntrinsicCodeGeneratorARM64::VisitMathAbsLong(HInvoke* invoke) {
358 GenAbsInteger(invoke->GetLocations(), true, GetVIXLAssembler());
359}
360
361static void GenMinMaxFP(LocationSummary* locations,
362 bool is_min,
363 bool is_double,
364 vixl::MacroAssembler* masm) {
365 Location op1 = locations->InAt(0);
366 Location op2 = locations->InAt(1);
367 Location out = locations->Out();
368
369 FPRegister op1_reg = is_double ? DRegisterFrom(op1) : SRegisterFrom(op1);
370 FPRegister op2_reg = is_double ? DRegisterFrom(op2) : SRegisterFrom(op2);
371 FPRegister out_reg = is_double ? DRegisterFrom(out) : SRegisterFrom(out);
372 if (is_min) {
373 __ Fmin(out_reg, op1_reg, op2_reg);
374 } else {
375 __ Fmax(out_reg, op1_reg, op2_reg);
376 }
377}
378
379static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
380 LocationSummary* locations = new (arena) LocationSummary(invoke,
381 LocationSummary::kNoCall,
382 kIntrinsified);
383 locations->SetInAt(0, Location::RequiresFpuRegister());
384 locations->SetInAt(1, Location::RequiresFpuRegister());
385 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
386}
387
388void IntrinsicLocationsBuilderARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
389 CreateFPFPToFPLocations(arena_, invoke);
390}
391
392void IntrinsicCodeGeneratorARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
393 GenMinMaxFP(invoke->GetLocations(), true, true, GetVIXLAssembler());
394}
395
396void IntrinsicLocationsBuilderARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
397 CreateFPFPToFPLocations(arena_, invoke);
398}
399
400void IntrinsicCodeGeneratorARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
401 GenMinMaxFP(invoke->GetLocations(), true, false, GetVIXLAssembler());
402}
403
404void IntrinsicLocationsBuilderARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
405 CreateFPFPToFPLocations(arena_, invoke);
406}
407
408void IntrinsicCodeGeneratorARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
409 GenMinMaxFP(invoke->GetLocations(), false, true, GetVIXLAssembler());
410}
411
412void IntrinsicLocationsBuilderARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
413 CreateFPFPToFPLocations(arena_, invoke);
414}
415
416void IntrinsicCodeGeneratorARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
417 GenMinMaxFP(invoke->GetLocations(), false, false, GetVIXLAssembler());
418}
419
420static void GenMinMax(LocationSummary* locations,
421 bool is_min,
422 bool is_long,
423 vixl::MacroAssembler* masm) {
424 Location op1 = locations->InAt(0);
425 Location op2 = locations->InAt(1);
426 Location out = locations->Out();
427
428 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
429 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
430 Register out_reg = is_long ? XRegisterFrom(out) : WRegisterFrom(out);
431
432 __ Cmp(op1_reg, op2_reg);
433 __ Csel(out_reg, op1_reg, op2_reg, is_min ? lt : gt);
434}
435
436static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
437 LocationSummary* locations = new (arena) LocationSummary(invoke,
438 LocationSummary::kNoCall,
439 kIntrinsified);
440 locations->SetInAt(0, Location::RequiresRegister());
441 locations->SetInAt(1, Location::RequiresRegister());
442 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
443}
444
445void IntrinsicLocationsBuilderARM64::VisitMathMinIntInt(HInvoke* invoke) {
446 CreateIntIntToIntLocations(arena_, invoke);
447}
448
449void IntrinsicCodeGeneratorARM64::VisitMathMinIntInt(HInvoke* invoke) {
450 GenMinMax(invoke->GetLocations(), true, false, GetVIXLAssembler());
451}
452
453void IntrinsicLocationsBuilderARM64::VisitMathMinLongLong(HInvoke* invoke) {
454 CreateIntIntToIntLocations(arena_, invoke);
455}
456
457void IntrinsicCodeGeneratorARM64::VisitMathMinLongLong(HInvoke* invoke) {
458 GenMinMax(invoke->GetLocations(), true, true, GetVIXLAssembler());
459}
460
461void IntrinsicLocationsBuilderARM64::VisitMathMaxIntInt(HInvoke* invoke) {
462 CreateIntIntToIntLocations(arena_, invoke);
463}
464
465void IntrinsicCodeGeneratorARM64::VisitMathMaxIntInt(HInvoke* invoke) {
466 GenMinMax(invoke->GetLocations(), false, false, GetVIXLAssembler());
467}
468
469void IntrinsicLocationsBuilderARM64::VisitMathMaxLongLong(HInvoke* invoke) {
470 CreateIntIntToIntLocations(arena_, invoke);
471}
472
473void IntrinsicCodeGeneratorARM64::VisitMathMaxLongLong(HInvoke* invoke) {
474 GenMinMax(invoke->GetLocations(), false, true, GetVIXLAssembler());
475}
476
477void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
478 CreateFPToFPLocations(arena_, invoke);
479}
480
481void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
482 LocationSummary* locations = invoke->GetLocations();
483 vixl::MacroAssembler* masm = GetVIXLAssembler();
484 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
485}
486
487void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
488 CreateFPToFPLocations(arena_, invoke);
489}
490
491void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
492 LocationSummary* locations = invoke->GetLocations();
493 vixl::MacroAssembler* masm = GetVIXLAssembler();
494 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
495}
496
497void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
498 CreateFPToFPLocations(arena_, invoke);
499}
500
501void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
502 LocationSummary* locations = invoke->GetLocations();
503 vixl::MacroAssembler* masm = GetVIXLAssembler();
504 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
505}
506
507void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
508 CreateFPToFPLocations(arena_, invoke);
509}
510
511void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
512 LocationSummary* locations = invoke->GetLocations();
513 vixl::MacroAssembler* masm = GetVIXLAssembler();
514 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
515}
516
517static void CreateFPToIntPlusTempLocations(ArenaAllocator* arena, HInvoke* invoke) {
518 LocationSummary* locations = new (arena) LocationSummary(invoke,
519 LocationSummary::kNoCall,
520 kIntrinsified);
521 locations->SetInAt(0, Location::RequiresFpuRegister());
522 locations->SetOut(Location::RequiresRegister());
523}
524
525static void GenMathRound(LocationSummary* locations,
526 bool is_double,
527 vixl::MacroAssembler* masm) {
528 FPRegister in_reg = is_double ?
529 DRegisterFrom(locations->InAt(0)) : SRegisterFrom(locations->InAt(0));
530 Register out_reg = is_double ?
531 XRegisterFrom(locations->Out()) : WRegisterFrom(locations->Out());
532 UseScratchRegisterScope temps(masm);
533 FPRegister temp1_reg = temps.AcquireSameSizeAs(in_reg);
534
535 // 0.5 can be encoded as an immediate, so use fmov.
536 if (is_double) {
537 __ Fmov(temp1_reg, static_cast<double>(0.5));
538 } else {
539 __ Fmov(temp1_reg, static_cast<float>(0.5));
540 }
541 __ Fadd(temp1_reg, in_reg, temp1_reg);
542 __ Fcvtms(out_reg, temp1_reg);
543}
544
545void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
546 CreateFPToIntPlusTempLocations(arena_, invoke);
547}
548
549void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
550 GenMathRound(invoke->GetLocations(), true, GetVIXLAssembler());
551}
552
553void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
554 CreateFPToIntPlusTempLocations(arena_, invoke);
555}
556
557void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
558 GenMathRound(invoke->GetLocations(), false, GetVIXLAssembler());
559}
560
561void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
562 CreateIntToIntLocations(arena_, invoke);
563}
564
565void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
566 vixl::MacroAssembler* masm = GetVIXLAssembler();
567 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
568 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
569}
570
571void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
572 CreateIntToIntLocations(arena_, invoke);
573}
574
575void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
576 vixl::MacroAssembler* masm = GetVIXLAssembler();
577 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
578 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
579}
580
581void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
582 CreateIntToIntLocations(arena_, invoke);
583}
584
585void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
586 vixl::MacroAssembler* masm = GetVIXLAssembler();
587 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
588 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
589}
590
591void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
592 CreateIntToIntLocations(arena_, invoke);
593}
594
595void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
596 vixl::MacroAssembler* masm = GetVIXLAssembler();
597 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
598 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
599}
600
601static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
602 LocationSummary* locations = new (arena) LocationSummary(invoke,
603 LocationSummary::kNoCall,
604 kIntrinsified);
605 locations->SetInAt(0, Location::RequiresRegister());
606 locations->SetInAt(1, Location::RequiresRegister());
607}
608
609void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
610 CreateIntIntToVoidLocations(arena_, invoke);
611}
612
613void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
614 vixl::MacroAssembler* masm = GetVIXLAssembler();
615 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
616 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
617}
618
619void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
620 CreateIntIntToVoidLocations(arena_, invoke);
621}
622
623void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
624 vixl::MacroAssembler* masm = GetVIXLAssembler();
625 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
626 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
627}
628
629void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
630 CreateIntIntToVoidLocations(arena_, invoke);
631}
632
633void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
634 vixl::MacroAssembler* masm = GetVIXLAssembler();
635 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
636 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
637}
638
639void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
640 CreateIntIntToVoidLocations(arena_, invoke);
641}
642
643void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
644 vixl::MacroAssembler* masm = GetVIXLAssembler();
645 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
646 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
647}
648
649void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
650 LocationSummary* locations = new (arena_) LocationSummary(invoke,
651 LocationSummary::kNoCall,
652 kIntrinsified);
653 locations->SetOut(Location::RequiresRegister());
654}
655
656void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
657 codegen_->Load(Primitive::kPrimNot, WRegisterFrom(invoke->GetLocations()->Out()),
658 MemOperand(tr, Thread::PeerOffset<8>().Int32Value()));
659}
660
661static void GenUnsafeGet(HInvoke* invoke,
662 Primitive::Type type,
663 bool is_volatile,
664 CodeGeneratorARM64* codegen) {
665 LocationSummary* locations = invoke->GetLocations();
666 DCHECK((type == Primitive::kPrimInt) ||
667 (type == Primitive::kPrimLong) ||
668 (type == Primitive::kPrimNot));
669 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
670 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
671 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
672 Register trg = RegisterFrom(locations->Out(), type);
Serban Constantinescu579885a2015-02-22 20:51:33 +0000673 bool use_acquire_release = codegen->GetInstructionSetFeatures().PreferAcquireRelease();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800674
675 MemOperand mem_op(base.X(), offset);
676 if (is_volatile) {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000677 if (use_acquire_release) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800678 codegen->LoadAcquire(invoke, trg, mem_op);
679 } else {
680 codegen->Load(type, trg, mem_op);
681 __ Dmb(InnerShareable, BarrierReads);
682 }
683 } else {
684 codegen->Load(type, trg, mem_op);
685 }
Roland Levillain4d027112015-07-01 15:41:14 +0100686
687 if (type == Primitive::kPrimNot) {
688 DCHECK(trg.IsW());
689 codegen->GetAssembler()->MaybeUnpoisonHeapReference(trg);
690 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800691}
692
693static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
694 LocationSummary* locations = new (arena) LocationSummary(invoke,
695 LocationSummary::kNoCall,
696 kIntrinsified);
697 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
698 locations->SetInAt(1, Location::RequiresRegister());
699 locations->SetInAt(2, Location::RequiresRegister());
700 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
701}
702
703void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
704 CreateIntIntIntToIntLocations(arena_, invoke);
705}
706void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
707 CreateIntIntIntToIntLocations(arena_, invoke);
708}
709void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
710 CreateIntIntIntToIntLocations(arena_, invoke);
711}
712void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
713 CreateIntIntIntToIntLocations(arena_, invoke);
714}
715void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
716 CreateIntIntIntToIntLocations(arena_, invoke);
717}
718void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
719 CreateIntIntIntToIntLocations(arena_, invoke);
720}
721
722void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
723 GenUnsafeGet(invoke, Primitive::kPrimInt, false, codegen_);
724}
725void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
726 GenUnsafeGet(invoke, Primitive::kPrimInt, true, codegen_);
727}
728void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
729 GenUnsafeGet(invoke, Primitive::kPrimLong, false, codegen_);
730}
731void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
732 GenUnsafeGet(invoke, Primitive::kPrimLong, true, codegen_);
733}
734void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
735 GenUnsafeGet(invoke, Primitive::kPrimNot, false, codegen_);
736}
737void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
738 GenUnsafeGet(invoke, Primitive::kPrimNot, true, codegen_);
739}
740
741static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
742 LocationSummary* locations = new (arena) LocationSummary(invoke,
743 LocationSummary::kNoCall,
744 kIntrinsified);
745 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
746 locations->SetInAt(1, Location::RequiresRegister());
747 locations->SetInAt(2, Location::RequiresRegister());
748 locations->SetInAt(3, Location::RequiresRegister());
749}
750
751void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
752 CreateIntIntIntIntToVoid(arena_, invoke);
753}
754void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
755 CreateIntIntIntIntToVoid(arena_, invoke);
756}
757void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
758 CreateIntIntIntIntToVoid(arena_, invoke);
759}
760void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
761 CreateIntIntIntIntToVoid(arena_, invoke);
762}
763void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
764 CreateIntIntIntIntToVoid(arena_, invoke);
765}
766void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
767 CreateIntIntIntIntToVoid(arena_, invoke);
768}
769void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
770 CreateIntIntIntIntToVoid(arena_, invoke);
771}
772void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
773 CreateIntIntIntIntToVoid(arena_, invoke);
774}
775void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
776 CreateIntIntIntIntToVoid(arena_, invoke);
777}
778
779static void GenUnsafePut(LocationSummary* locations,
780 Primitive::Type type,
781 bool is_volatile,
782 bool is_ordered,
783 CodeGeneratorARM64* codegen) {
784 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
785
786 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
787 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
788 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100789 Register source = value;
Serban Constantinescu579885a2015-02-22 20:51:33 +0000790 bool use_acquire_release = codegen->GetInstructionSetFeatures().PreferAcquireRelease();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800791
792 MemOperand mem_op(base.X(), offset);
793
Roland Levillain4d027112015-07-01 15:41:14 +0100794 {
795 // We use a block to end the scratch scope before the write barrier, thus
796 // freeing the temporary registers so they can be used in `MarkGCCard`.
797 UseScratchRegisterScope temps(masm);
798
799 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
800 DCHECK(value.IsW());
801 Register temp = temps.AcquireW();
802 __ Mov(temp.W(), value.W());
803 codegen->GetAssembler()->PoisonHeapReference(temp.W());
804 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800805 }
Roland Levillain4d027112015-07-01 15:41:14 +0100806
807 if (is_volatile || is_ordered) {
808 if (use_acquire_release) {
809 codegen->StoreRelease(type, source, mem_op);
810 } else {
811 __ Dmb(InnerShareable, BarrierAll);
812 codegen->Store(type, source, mem_op);
813 if (is_volatile) {
814 __ Dmb(InnerShareable, BarrierReads);
815 }
816 }
817 } else {
818 codegen->Store(type, source, mem_op);
819 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800820 }
821
822 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100823 bool value_can_be_null = true; // TODO: Worth finding out this information?
824 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800825 }
826}
827
828void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
829 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, false, codegen_);
830}
831void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
832 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, true, codegen_);
833}
834void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
835 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, false, codegen_);
836}
837void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
838 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, false, codegen_);
839}
840void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
841 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, true, codegen_);
842}
843void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
844 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, false, codegen_);
845}
846void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
847 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, false, codegen_);
848}
849void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
850 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, true, codegen_);
851}
852void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
853 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, false, codegen_);
854}
855
856static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
857 LocationSummary* locations = new (arena) LocationSummary(invoke,
858 LocationSummary::kNoCall,
859 kIntrinsified);
860 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
861 locations->SetInAt(1, Location::RequiresRegister());
862 locations->SetInAt(2, Location::RequiresRegister());
863 locations->SetInAt(3, Location::RequiresRegister());
864 locations->SetInAt(4, Location::RequiresRegister());
865
866 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
867}
868
869static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorARM64* codegen) {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000870 bool use_acquire_release = codegen->GetInstructionSetFeatures().PreferAcquireRelease();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800871 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
872
873 Register out = WRegisterFrom(locations->Out()); // Boolean result.
874
875 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
876 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
877 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
878 Register value = RegisterFrom(locations->InAt(4), type); // Value.
879
880 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
881 if (type == Primitive::kPrimNot) {
882 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100883 bool value_can_be_null = true; // TODO: Worth finding out this information?
884 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800885 }
886
887 UseScratchRegisterScope temps(masm);
888 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
889 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
890
891 Register tmp_32 = tmp_value.W();
892
893 __ Add(tmp_ptr, base.X(), Operand(offset));
894
Roland Levillain4d027112015-07-01 15:41:14 +0100895 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
896 codegen->GetAssembler()->PoisonHeapReference(expected);
897 codegen->GetAssembler()->PoisonHeapReference(value);
898 }
899
Andreas Gampe878d58c2015-01-15 23:24:00 -0800900 // do {
901 // tmp_value = [tmp_ptr] - expected;
902 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
903 // result = tmp_value != 0;
904
905 vixl::Label loop_head, exit_loop;
Serban Constantinescu579885a2015-02-22 20:51:33 +0000906 if (use_acquire_release) {
907 __ Bind(&loop_head);
908 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
909 __ Cmp(tmp_value, expected);
910 __ B(&exit_loop, ne);
911 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
912 __ Cbnz(tmp_32, &loop_head);
913 } else {
914 __ Dmb(InnerShareable, BarrierWrites);
915 __ Bind(&loop_head);
916 __ Ldxr(tmp_value, MemOperand(tmp_ptr));
917 __ Cmp(tmp_value, expected);
918 __ B(&exit_loop, ne);
919 __ Stxr(tmp_32, value, MemOperand(tmp_ptr));
920 __ Cbnz(tmp_32, &loop_head);
921 __ Dmb(InnerShareable, BarrierAll);
922 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800923 __ Bind(&exit_loop);
924 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +0100925
926 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
927 codegen->GetAssembler()->UnpoisonHeapReference(value);
928 codegen->GetAssembler()->UnpoisonHeapReference(expected);
929 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800930}
931
932void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
933 CreateIntIntIntIntIntToInt(arena_, invoke);
934}
935void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
936 CreateIntIntIntIntIntToInt(arena_, invoke);
937}
938void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
939 CreateIntIntIntIntIntToInt(arena_, invoke);
940}
941
942void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
943 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
944}
945void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
946 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
947}
948void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
949 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
950}
951
952void IntrinsicLocationsBuilderARM64::VisitStringCharAt(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800953 LocationSummary* locations = new (arena_) LocationSummary(invoke,
954 LocationSummary::kCallOnSlowPath,
955 kIntrinsified);
956 locations->SetInAt(0, Location::RequiresRegister());
957 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray82f34492015-02-04 10:44:23 +0000958 // In case we need to go in the slow path, we can't have the output be the same
959 // as the input: the current liveness analysis considers the input to be live
960 // at the point of the call.
961 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800962}
963
964void IntrinsicCodeGeneratorARM64::VisitStringCharAt(HInvoke* invoke) {
965 vixl::MacroAssembler* masm = GetVIXLAssembler();
966 LocationSummary* locations = invoke->GetLocations();
967
968 // Location of reference to data array
969 const MemberOffset value_offset = mirror::String::ValueOffset();
970 // Location of count
971 const MemberOffset count_offset = mirror::String::CountOffset();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800972
973 Register obj = WRegisterFrom(locations->InAt(0)); // String object pointer.
974 Register idx = WRegisterFrom(locations->InAt(1)); // Index of character.
975 Register out = WRegisterFrom(locations->Out()); // Result character.
976
977 UseScratchRegisterScope temps(masm);
978 Register temp = temps.AcquireW();
979 Register array_temp = temps.AcquireW(); // We can trade this for worse scheduling.
980
981 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
982 // the cost.
983 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
984 // we will not optimize the code for constants (which would save a register).
985
986 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
987 codegen_->AddSlowPath(slow_path);
988
989 __ Ldr(temp, HeapOperand(obj, count_offset)); // temp = str.length.
990 codegen_->MaybeRecordImplicitNullCheck(invoke);
991 __ Cmp(idx, temp);
992 __ B(hs, slow_path->GetEntryLabel());
993
Jeff Hao848f70a2014-01-15 13:49:50 -0800994 __ Add(array_temp, obj, Operand(value_offset.Int32Value())); // array_temp := str.value.
Andreas Gampe878d58c2015-01-15 23:24:00 -0800995
996 // Load the value.
Jeff Hao848f70a2014-01-15 13:49:50 -0800997 __ Ldrh(out, MemOperand(array_temp.X(), idx, UXTW, 1)); // out := array_temp[idx].
Andreas Gampe878d58c2015-01-15 23:24:00 -0800998
999 __ Bind(slow_path->GetExitLabel());
1000}
1001
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001002void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001003 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1004 LocationSummary::kCall,
1005 kIntrinsified);
1006 InvokeRuntimeCallingConvention calling_convention;
1007 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1008 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1009 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1010}
1011
1012void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
1013 vixl::MacroAssembler* masm = GetVIXLAssembler();
1014 LocationSummary* locations = invoke->GetLocations();
1015
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001016 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001017 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001018
1019 Register argument = WRegisterFrom(locations->InAt(1));
1020 __ Cmp(argument, 0);
1021 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1022 codegen_->AddSlowPath(slow_path);
1023 __ B(eq, slow_path->GetEntryLabel());
1024
1025 __ Ldr(
1026 lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pStringCompareTo).Int32Value()));
1027 __ Blr(lr);
1028 __ Bind(slow_path->GetExitLabel());
1029}
1030
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001031static void GenerateVisitStringIndexOf(HInvoke* invoke,
1032 vixl::MacroAssembler* masm,
1033 CodeGeneratorARM64* codegen,
1034 ArenaAllocator* allocator,
1035 bool start_at_zero) {
1036 LocationSummary* locations = invoke->GetLocations();
1037 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
1038
1039 // Note that the null check must have been done earlier.
1040 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1041
1042 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1043 // or directly dispatch if we have a constant.
1044 SlowPathCodeARM64* slow_path = nullptr;
1045 if (invoke->InputAt(1)->IsIntConstant()) {
1046 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) > 0xFFFFU) {
1047 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1048 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1049 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1050 codegen->AddSlowPath(slow_path);
1051 __ B(slow_path->GetEntryLabel());
1052 __ Bind(slow_path->GetExitLabel());
1053 return;
1054 }
1055 } else {
1056 Register char_reg = WRegisterFrom(locations->InAt(1));
1057 __ Mov(tmp_reg, 0xFFFF);
1058 __ Cmp(char_reg, Operand(tmp_reg));
1059 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1060 codegen->AddSlowPath(slow_path);
1061 __ B(hi, slow_path->GetEntryLabel());
1062 }
1063
1064 if (start_at_zero) {
1065 // Start-index = 0.
1066 __ Mov(tmp_reg, 0);
1067 }
1068
1069 __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pIndexOf).Int32Value()));
1070 __ Blr(lr);
1071
1072 if (slow_path != nullptr) {
1073 __ Bind(slow_path->GetExitLabel());
1074 }
1075}
1076
1077void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1078 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1079 LocationSummary::kCall,
1080 kIntrinsified);
1081 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1082 // best to align the inputs accordingly.
1083 InvokeRuntimeCallingConvention calling_convention;
1084 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1085 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1086 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1087
1088 // Need a temp for slow-path codepoint compare, and need to send start_index=0.
1089 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1090}
1091
1092void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
1093 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, GetAllocator(), true);
1094}
1095
1096void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1097 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1098 LocationSummary::kCall,
1099 kIntrinsified);
1100 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1101 // best to align the inputs accordingly.
1102 InvokeRuntimeCallingConvention calling_convention;
1103 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1104 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1105 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1106 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1107
1108 // Need a temp for slow-path codepoint compare.
1109 locations->AddTemp(Location::RequiresRegister());
1110}
1111
1112void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1113 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, GetAllocator(), false);
1114}
1115
Jeff Hao848f70a2014-01-15 13:49:50 -08001116void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1117 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1118 LocationSummary::kCall,
1119 kIntrinsified);
1120 InvokeRuntimeCallingConvention calling_convention;
1121 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1122 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1123 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1124 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1125 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1126}
1127
1128void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1129 vixl::MacroAssembler* masm = GetVIXLAssembler();
1130 LocationSummary* locations = invoke->GetLocations();
1131
1132 Register byte_array = WRegisterFrom(locations->InAt(0));
1133 __ Cmp(byte_array, 0);
1134 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1135 codegen_->AddSlowPath(slow_path);
1136 __ B(eq, slow_path->GetEntryLabel());
1137
1138 __ Ldr(lr,
1139 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromBytes).Int32Value()));
1140 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1141 __ Blr(lr);
1142 __ Bind(slow_path->GetExitLabel());
1143}
1144
1145void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1146 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1147 LocationSummary::kCall,
1148 kIntrinsified);
1149 InvokeRuntimeCallingConvention calling_convention;
1150 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1151 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1152 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1153 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1154}
1155
1156void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1157 vixl::MacroAssembler* masm = GetVIXLAssembler();
1158
1159 __ Ldr(lr,
1160 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromChars).Int32Value()));
1161 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1162 __ Blr(lr);
1163}
1164
1165void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
1166 // The inputs plus one temp.
1167 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1168 LocationSummary::kCall,
1169 kIntrinsified);
1170 InvokeRuntimeCallingConvention calling_convention;
1171 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1172 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1173 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1174 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1175}
1176
1177void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
1178 vixl::MacroAssembler* masm = GetVIXLAssembler();
1179 LocationSummary* locations = invoke->GetLocations();
1180
1181 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1182 __ Cmp(string_to_copy, 0);
1183 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1184 codegen_->AddSlowPath(slow_path);
1185 __ B(eq, slow_path->GetEntryLabel());
1186
1187 __ Ldr(lr,
1188 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromString).Int32Value()));
1189 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1190 __ Blr(lr);
1191 __ Bind(slow_path->GetExitLabel());
1192}
1193
Andreas Gampe878d58c2015-01-15 23:24:00 -08001194// Unimplemented intrinsics.
1195
1196#define UNIMPLEMENTED_INTRINSIC(Name) \
1197void IntrinsicLocationsBuilderARM64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1198} \
1199void IntrinsicCodeGeneratorARM64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1200}
1201
1202UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Andreas Gampe878d58c2015-01-15 23:24:00 -08001203UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
Jeff Hao848f70a2014-01-15 13:49:50 -08001204UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Andreas Gampe878d58c2015-01-15 23:24:00 -08001205
Roland Levillain4d027112015-07-01 15:41:14 +01001206#undef UNIMPLEMENTED_INTRINSIC
1207
1208#undef __
1209
Andreas Gampe878d58c2015-01-15 23:24:00 -08001210} // namespace arm64
1211} // namespace art