blob: 5dce83a69cfd7b94a517238744d57a4172389ae1 [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;
49
Andreas Gampe878d58c2015-01-15 23:24:00 -080050namespace {
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));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800115 } else {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000116 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), LocationFrom(kArtMethodRegister));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800117 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000118 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800119
120 // Copy the result back to the expected output.
121 Location out = invoke_->GetLocations()->Out();
122 if (out.IsValid()) {
123 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
124 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
125 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
126 }
127
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000128 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800129 __ B(GetExitLabel());
130 }
131
Alexandre Rames9931f312015-06-19 14:47:01 +0100132 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathARM64"; }
133
Andreas Gampe878d58c2015-01-15 23:24:00 -0800134 private:
135 // The instruction where this slow path is happening.
136 HInvoke* const invoke_;
137
138 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARM64);
139};
140
141#undef __
142
143bool IntrinsicLocationsBuilderARM64::TryDispatch(HInvoke* invoke) {
144 Dispatch(invoke);
145 LocationSummary* res = invoke->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000146 if (res == nullptr) {
147 return false;
148 }
149 if (kEmitCompilerReadBarrier && res->CanCall()) {
150 // Generating an intrinsic for this HInvoke may produce an
151 // IntrinsicSlowPathARM64 slow path. Currently this approach
152 // does not work when using read barriers, as the emitted
153 // calling sequence will make use of another slow path
154 // (ReadBarrierForRootSlowPathARM64 for HInvokeStaticOrDirect,
155 // ReadBarrierSlowPathARM64 for HInvokeVirtual). So we bail
156 // out in this case.
157 //
158 // TODO: Find a way to have intrinsics work with read barriers.
159 invoke->SetLocations(nullptr);
160 return false;
161 }
162 return res->Intrinsified();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800163}
164
165#define __ masm->
166
167static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
168 LocationSummary* locations = new (arena) LocationSummary(invoke,
169 LocationSummary::kNoCall,
170 kIntrinsified);
171 locations->SetInAt(0, Location::RequiresFpuRegister());
172 locations->SetOut(Location::RequiresRegister());
173}
174
175static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
176 LocationSummary* locations = new (arena) LocationSummary(invoke,
177 LocationSummary::kNoCall,
178 kIntrinsified);
179 locations->SetInAt(0, Location::RequiresRegister());
180 locations->SetOut(Location::RequiresFpuRegister());
181}
182
183static void MoveFPToInt(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
184 Location input = locations->InAt(0);
185 Location output = locations->Out();
186 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
187 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
188}
189
190static void MoveIntToFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
191 Location input = locations->InAt(0);
192 Location output = locations->Out();
193 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
194 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
195}
196
197void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
198 CreateFPToIntLocations(arena_, invoke);
199}
200void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
201 CreateIntToFPLocations(arena_, invoke);
202}
203
204void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000205 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800206}
207void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000208 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800209}
210
211void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
212 CreateFPToIntLocations(arena_, invoke);
213}
214void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
215 CreateIntToFPLocations(arena_, invoke);
216}
217
218void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000219 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800220}
221void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000222 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800223}
224
225static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
226 LocationSummary* locations = new (arena) LocationSummary(invoke,
227 LocationSummary::kNoCall,
228 kIntrinsified);
229 locations->SetInAt(0, Location::RequiresRegister());
230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
231}
232
233static void GenReverseBytes(LocationSummary* locations,
234 Primitive::Type type,
235 vixl::MacroAssembler* masm) {
236 Location in = locations->InAt(0);
237 Location out = locations->Out();
238
239 switch (type) {
240 case Primitive::kPrimShort:
241 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
242 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
243 break;
244 case Primitive::kPrimInt:
245 case Primitive::kPrimLong:
246 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
247 break;
248 default:
249 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
250 UNREACHABLE();
251 }
252}
253
254void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
255 CreateIntToIntLocations(arena_, invoke);
256}
257
258void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
259 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
260}
261
262void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
263 CreateIntToIntLocations(arena_, invoke);
264}
265
266void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
267 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
268}
269
270void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
271 CreateIntToIntLocations(arena_, invoke);
272}
273
274void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
275 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetVIXLAssembler());
276}
277
Aart Bik7b565022016-01-28 14:36:22 -0800278static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
279 LocationSummary* locations = new (arena) LocationSummary(invoke,
280 LocationSummary::kNoCall,
281 kIntrinsified);
282 locations->SetInAt(0, Location::RequiresRegister());
283 locations->SetInAt(1, Location::RequiresRegister());
284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
285}
286
287static void GenCompare(LocationSummary* locations, bool is_long, vixl::MacroAssembler* masm) {
288 Location op1 = locations->InAt(0);
289 Location op2 = locations->InAt(1);
290 Location out = locations->Out();
291
292 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
293 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
294 Register out_reg = WRegisterFrom(out);
295
296 __ Cmp(op1_reg, op2_reg);
297 __ Cset(out_reg, gt); // out == +1 if GT or 0 otherwise
298 __ Cinv(out_reg, out_reg, lt); // out == -1 if LT or unchanged otherwise
299}
300
301void IntrinsicLocationsBuilderARM64::VisitIntegerCompare(HInvoke* invoke) {
302 CreateIntIntToIntLocations(arena_, invoke);
303}
304
305void IntrinsicCodeGeneratorARM64::VisitIntegerCompare(HInvoke* invoke) {
306 GenCompare(invoke->GetLocations(), /* is_long */ false, GetVIXLAssembler());
307}
308
309void IntrinsicLocationsBuilderARM64::VisitLongCompare(HInvoke* invoke) {
310 CreateIntIntToIntLocations(arena_, invoke);
311}
312
313void IntrinsicCodeGeneratorARM64::VisitLongCompare(HInvoke* invoke) {
314 GenCompare(invoke->GetLocations(), /* is_long */ true, GetVIXLAssembler());
315}
316
Scott Wakeling611d3392015-07-10 11:42:06 +0100317static void GenNumberOfLeadingZeros(LocationSummary* locations,
318 Primitive::Type type,
319 vixl::MacroAssembler* masm) {
320 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
321
322 Location in = locations->InAt(0);
323 Location out = locations->Out();
324
325 __ Clz(RegisterFrom(out, type), RegisterFrom(in, type));
326}
327
328void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
329 CreateIntToIntLocations(arena_, invoke);
330}
331
332void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
333 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
334}
335
336void IntrinsicLocationsBuilderARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
337 CreateIntToIntLocations(arena_, invoke);
338}
339
340void IntrinsicCodeGeneratorARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
341 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
342}
343
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100344static void GenNumberOfTrailingZeros(LocationSummary* locations,
345 Primitive::Type type,
346 vixl::MacroAssembler* masm) {
347 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
348
349 Location in = locations->InAt(0);
350 Location out = locations->Out();
351
352 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
353 __ Clz(RegisterFrom(out, type), RegisterFrom(out, type));
354}
355
356void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
357 CreateIntToIntLocations(arena_, invoke);
358}
359
360void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
361 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
362}
363
364void IntrinsicLocationsBuilderARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
365 CreateIntToIntLocations(arena_, invoke);
366}
367
368void IntrinsicCodeGeneratorARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
369 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
370}
371
Andreas Gampe878d58c2015-01-15 23:24:00 -0800372static void GenReverse(LocationSummary* locations,
373 Primitive::Type type,
374 vixl::MacroAssembler* masm) {
375 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
376
377 Location in = locations->InAt(0);
378 Location out = locations->Out();
379
380 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
381}
382
383void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
384 CreateIntToIntLocations(arena_, invoke);
385}
386
387void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
388 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
389}
390
391void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
392 CreateIntToIntLocations(arena_, invoke);
393}
394
395void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
396 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
397}
398
399static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800400 LocationSummary* locations = new (arena) LocationSummary(invoke,
401 LocationSummary::kNoCall,
402 kIntrinsified);
403 locations->SetInAt(0, Location::RequiresFpuRegister());
404 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
405}
406
407static void MathAbsFP(LocationSummary* locations, bool is64bit, vixl::MacroAssembler* masm) {
408 Location in = locations->InAt(0);
409 Location out = locations->Out();
410
411 FPRegister in_reg = is64bit ? DRegisterFrom(in) : SRegisterFrom(in);
412 FPRegister out_reg = is64bit ? DRegisterFrom(out) : SRegisterFrom(out);
413
414 __ Fabs(out_reg, in_reg);
415}
416
417void IntrinsicLocationsBuilderARM64::VisitMathAbsDouble(HInvoke* invoke) {
418 CreateFPToFPLocations(arena_, invoke);
419}
420
421void IntrinsicCodeGeneratorARM64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000422 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800423}
424
425void IntrinsicLocationsBuilderARM64::VisitMathAbsFloat(HInvoke* invoke) {
426 CreateFPToFPLocations(arena_, invoke);
427}
428
429void IntrinsicCodeGeneratorARM64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000430 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800431}
432
433static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
434 LocationSummary* locations = new (arena) LocationSummary(invoke,
435 LocationSummary::kNoCall,
436 kIntrinsified);
437 locations->SetInAt(0, Location::RequiresRegister());
438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
439}
440
441static void GenAbsInteger(LocationSummary* locations,
442 bool is64bit,
443 vixl::MacroAssembler* masm) {
444 Location in = locations->InAt(0);
445 Location output = locations->Out();
446
447 Register in_reg = is64bit ? XRegisterFrom(in) : WRegisterFrom(in);
448 Register out_reg = is64bit ? XRegisterFrom(output) : WRegisterFrom(output);
449
450 __ Cmp(in_reg, Operand(0));
451 __ Cneg(out_reg, in_reg, lt);
452}
453
454void IntrinsicLocationsBuilderARM64::VisitMathAbsInt(HInvoke* invoke) {
455 CreateIntToInt(arena_, invoke);
456}
457
458void IntrinsicCodeGeneratorARM64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000459 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800460}
461
462void IntrinsicLocationsBuilderARM64::VisitMathAbsLong(HInvoke* invoke) {
463 CreateIntToInt(arena_, invoke);
464}
465
466void IntrinsicCodeGeneratorARM64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000467 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800468}
469
470static void GenMinMaxFP(LocationSummary* locations,
471 bool is_min,
472 bool is_double,
473 vixl::MacroAssembler* masm) {
474 Location op1 = locations->InAt(0);
475 Location op2 = locations->InAt(1);
476 Location out = locations->Out();
477
478 FPRegister op1_reg = is_double ? DRegisterFrom(op1) : SRegisterFrom(op1);
479 FPRegister op2_reg = is_double ? DRegisterFrom(op2) : SRegisterFrom(op2);
480 FPRegister out_reg = is_double ? DRegisterFrom(out) : SRegisterFrom(out);
481 if (is_min) {
482 __ Fmin(out_reg, op1_reg, op2_reg);
483 } else {
484 __ Fmax(out_reg, op1_reg, op2_reg);
485 }
486}
487
488static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
489 LocationSummary* locations = new (arena) LocationSummary(invoke,
490 LocationSummary::kNoCall,
491 kIntrinsified);
492 locations->SetInAt(0, Location::RequiresFpuRegister());
493 locations->SetInAt(1, Location::RequiresFpuRegister());
494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
495}
496
497void IntrinsicLocationsBuilderARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
498 CreateFPFPToFPLocations(arena_, invoke);
499}
500
501void IntrinsicCodeGeneratorARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000502 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800503}
504
505void IntrinsicLocationsBuilderARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
506 CreateFPFPToFPLocations(arena_, invoke);
507}
508
509void IntrinsicCodeGeneratorARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000510 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800511}
512
513void IntrinsicLocationsBuilderARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
514 CreateFPFPToFPLocations(arena_, invoke);
515}
516
517void IntrinsicCodeGeneratorARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000518 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800519}
520
521void IntrinsicLocationsBuilderARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
522 CreateFPFPToFPLocations(arena_, invoke);
523}
524
525void IntrinsicCodeGeneratorARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000526 GenMinMaxFP(
527 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800528}
529
530static void GenMinMax(LocationSummary* locations,
531 bool is_min,
532 bool is_long,
533 vixl::MacroAssembler* masm) {
534 Location op1 = locations->InAt(0);
535 Location op2 = locations->InAt(1);
536 Location out = locations->Out();
537
538 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
539 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
540 Register out_reg = is_long ? XRegisterFrom(out) : WRegisterFrom(out);
541
542 __ Cmp(op1_reg, op2_reg);
543 __ Csel(out_reg, op1_reg, op2_reg, is_min ? lt : gt);
544}
545
Andreas Gampe878d58c2015-01-15 23:24:00 -0800546void IntrinsicLocationsBuilderARM64::VisitMathMinIntInt(HInvoke* invoke) {
547 CreateIntIntToIntLocations(arena_, invoke);
548}
549
550void IntrinsicCodeGeneratorARM64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000551 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800552}
553
554void IntrinsicLocationsBuilderARM64::VisitMathMinLongLong(HInvoke* invoke) {
555 CreateIntIntToIntLocations(arena_, invoke);
556}
557
558void IntrinsicCodeGeneratorARM64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000559 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800560}
561
562void IntrinsicLocationsBuilderARM64::VisitMathMaxIntInt(HInvoke* invoke) {
563 CreateIntIntToIntLocations(arena_, invoke);
564}
565
566void IntrinsicCodeGeneratorARM64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000567 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800568}
569
570void IntrinsicLocationsBuilderARM64::VisitMathMaxLongLong(HInvoke* invoke) {
571 CreateIntIntToIntLocations(arena_, invoke);
572}
573
574void IntrinsicCodeGeneratorARM64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000575 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800576}
577
578void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
579 CreateFPToFPLocations(arena_, invoke);
580}
581
582void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
583 LocationSummary* locations = invoke->GetLocations();
584 vixl::MacroAssembler* masm = GetVIXLAssembler();
585 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
586}
587
588void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
589 CreateFPToFPLocations(arena_, invoke);
590}
591
592void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
593 LocationSummary* locations = invoke->GetLocations();
594 vixl::MacroAssembler* masm = GetVIXLAssembler();
595 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
596}
597
598void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
599 CreateFPToFPLocations(arena_, invoke);
600}
601
602void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
603 LocationSummary* locations = invoke->GetLocations();
604 vixl::MacroAssembler* masm = GetVIXLAssembler();
605 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
606}
607
608void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
609 CreateFPToFPLocations(arena_, invoke);
610}
611
612void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
613 LocationSummary* locations = invoke->GetLocations();
614 vixl::MacroAssembler* masm = GetVIXLAssembler();
615 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
616}
617
618static void CreateFPToIntPlusTempLocations(ArenaAllocator* arena, HInvoke* invoke) {
619 LocationSummary* locations = new (arena) LocationSummary(invoke,
620 LocationSummary::kNoCall,
621 kIntrinsified);
622 locations->SetInAt(0, Location::RequiresFpuRegister());
623 locations->SetOut(Location::RequiresRegister());
624}
625
626static void GenMathRound(LocationSummary* locations,
627 bool is_double,
628 vixl::MacroAssembler* masm) {
629 FPRegister in_reg = is_double ?
630 DRegisterFrom(locations->InAt(0)) : SRegisterFrom(locations->InAt(0));
631 Register out_reg = is_double ?
632 XRegisterFrom(locations->Out()) : WRegisterFrom(locations->Out());
633 UseScratchRegisterScope temps(masm);
634 FPRegister temp1_reg = temps.AcquireSameSizeAs(in_reg);
635
636 // 0.5 can be encoded as an immediate, so use fmov.
637 if (is_double) {
638 __ Fmov(temp1_reg, static_cast<double>(0.5));
639 } else {
640 __ Fmov(temp1_reg, static_cast<float>(0.5));
641 }
642 __ Fadd(temp1_reg, in_reg, temp1_reg);
643 __ Fcvtms(out_reg, temp1_reg);
644}
645
646void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800647 // See intrinsics.h.
648 if (kRoundIsPlusPointFive) {
649 CreateFPToIntPlusTempLocations(arena_, invoke);
650 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800651}
652
653void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000654 GenMathRound(invoke->GetLocations(), /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800655}
656
657void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800658 // See intrinsics.h.
659 if (kRoundIsPlusPointFive) {
660 CreateFPToIntPlusTempLocations(arena_, invoke);
661 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800662}
663
664void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000665 GenMathRound(invoke->GetLocations(), /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800666}
667
668void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
669 CreateIntToIntLocations(arena_, invoke);
670}
671
672void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
673 vixl::MacroAssembler* masm = GetVIXLAssembler();
674 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
675 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
676}
677
678void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
679 CreateIntToIntLocations(arena_, invoke);
680}
681
682void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
683 vixl::MacroAssembler* masm = GetVIXLAssembler();
684 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
685 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
686}
687
688void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
689 CreateIntToIntLocations(arena_, invoke);
690}
691
692void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
693 vixl::MacroAssembler* masm = GetVIXLAssembler();
694 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
695 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
696}
697
698void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
699 CreateIntToIntLocations(arena_, invoke);
700}
701
702void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
703 vixl::MacroAssembler* masm = GetVIXLAssembler();
704 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
705 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
706}
707
708static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
709 LocationSummary* locations = new (arena) LocationSummary(invoke,
710 LocationSummary::kNoCall,
711 kIntrinsified);
712 locations->SetInAt(0, Location::RequiresRegister());
713 locations->SetInAt(1, Location::RequiresRegister());
714}
715
716void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
717 CreateIntIntToVoidLocations(arena_, invoke);
718}
719
720void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
721 vixl::MacroAssembler* masm = GetVIXLAssembler();
722 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
723 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
724}
725
726void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
727 CreateIntIntToVoidLocations(arena_, invoke);
728}
729
730void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
731 vixl::MacroAssembler* masm = GetVIXLAssembler();
732 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
733 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
734}
735
736void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
737 CreateIntIntToVoidLocations(arena_, invoke);
738}
739
740void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
741 vixl::MacroAssembler* masm = GetVIXLAssembler();
742 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
743 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
744}
745
746void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
747 CreateIntIntToVoidLocations(arena_, invoke);
748}
749
750void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
751 vixl::MacroAssembler* masm = GetVIXLAssembler();
752 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
753 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
754}
755
756void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
757 LocationSummary* locations = new (arena_) LocationSummary(invoke,
758 LocationSummary::kNoCall,
759 kIntrinsified);
760 locations->SetOut(Location::RequiresRegister());
761}
762
763void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
764 codegen_->Load(Primitive::kPrimNot, WRegisterFrom(invoke->GetLocations()->Out()),
765 MemOperand(tr, Thread::PeerOffset<8>().Int32Value()));
766}
767
768static void GenUnsafeGet(HInvoke* invoke,
769 Primitive::Type type,
770 bool is_volatile,
771 CodeGeneratorARM64* codegen) {
772 LocationSummary* locations = invoke->GetLocations();
773 DCHECK((type == Primitive::kPrimInt) ||
774 (type == Primitive::kPrimLong) ||
775 (type == Primitive::kPrimNot));
776 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000777 Location base_loc = locations->InAt(1);
778 Register base = WRegisterFrom(base_loc); // Object pointer.
779 Location offset_loc = locations->InAt(2);
780 Register offset = XRegisterFrom(offset_loc); // Long offset.
781 Location trg_loc = locations->Out();
782 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800783
Roland Levillain44015862016-01-22 11:47:17 +0000784 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
785 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
786 UseScratchRegisterScope temps(masm);
787 Register temp = temps.AcquireW();
788 codegen->GenerateArrayLoadWithBakerReadBarrier(
789 invoke, trg_loc, base, 0U, offset_loc, temp, /* needs_null_check */ false);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800790 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000791 // Other cases.
792 MemOperand mem_op(base.X(), offset);
793 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000794 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000795 } else {
796 codegen->Load(type, trg, mem_op);
797 }
Roland Levillain4d027112015-07-01 15:41:14 +0100798
Roland Levillain44015862016-01-22 11:47:17 +0000799 if (type == Primitive::kPrimNot) {
800 DCHECK(trg.IsW());
801 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0U, offset_loc);
802 }
Roland Levillain4d027112015-07-01 15:41:14 +0100803 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800804}
805
806static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000807 bool can_call = kEmitCompilerReadBarrier &&
808 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
809 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800810 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000811 can_call ?
812 LocationSummary::kCallOnSlowPath :
813 LocationSummary::kNoCall,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800814 kIntrinsified);
815 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
816 locations->SetInAt(1, Location::RequiresRegister());
817 locations->SetInAt(2, Location::RequiresRegister());
818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
819}
820
821void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
822 CreateIntIntIntToIntLocations(arena_, invoke);
823}
824void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
825 CreateIntIntIntToIntLocations(arena_, invoke);
826}
827void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
828 CreateIntIntIntToIntLocations(arena_, invoke);
829}
830void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
831 CreateIntIntIntToIntLocations(arena_, invoke);
832}
833void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
834 CreateIntIntIntToIntLocations(arena_, invoke);
835}
836void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
837 CreateIntIntIntToIntLocations(arena_, invoke);
838}
839
840void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000841 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800842}
843void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000844 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800845}
846void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000847 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800848}
849void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000850 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800851}
852void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000853 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800854}
855void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000856 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800857}
858
859static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
860 LocationSummary* locations = new (arena) LocationSummary(invoke,
861 LocationSummary::kNoCall,
862 kIntrinsified);
863 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
864 locations->SetInAt(1, Location::RequiresRegister());
865 locations->SetInAt(2, Location::RequiresRegister());
866 locations->SetInAt(3, Location::RequiresRegister());
867}
868
869void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
870 CreateIntIntIntIntToVoid(arena_, invoke);
871}
872void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
873 CreateIntIntIntIntToVoid(arena_, invoke);
874}
875void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
876 CreateIntIntIntIntToVoid(arena_, invoke);
877}
878void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
879 CreateIntIntIntIntToVoid(arena_, invoke);
880}
881void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
882 CreateIntIntIntIntToVoid(arena_, invoke);
883}
884void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
885 CreateIntIntIntIntToVoid(arena_, invoke);
886}
887void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
888 CreateIntIntIntIntToVoid(arena_, invoke);
889}
890void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
891 CreateIntIntIntIntToVoid(arena_, invoke);
892}
893void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
894 CreateIntIntIntIntToVoid(arena_, invoke);
895}
896
897static void GenUnsafePut(LocationSummary* locations,
898 Primitive::Type type,
899 bool is_volatile,
900 bool is_ordered,
901 CodeGeneratorARM64* codegen) {
902 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
903
904 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
905 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
906 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100907 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800908 MemOperand mem_op(base.X(), offset);
909
Roland Levillain4d027112015-07-01 15:41:14 +0100910 {
911 // We use a block to end the scratch scope before the write barrier, thus
912 // freeing the temporary registers so they can be used in `MarkGCCard`.
913 UseScratchRegisterScope temps(masm);
914
915 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
916 DCHECK(value.IsW());
917 Register temp = temps.AcquireW();
918 __ Mov(temp.W(), value.W());
919 codegen->GetAssembler()->PoisonHeapReference(temp.W());
920 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800921 }
Roland Levillain4d027112015-07-01 15:41:14 +0100922
923 if (is_volatile || is_ordered) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000924 codegen->StoreRelease(type, source, mem_op);
Roland Levillain4d027112015-07-01 15:41:14 +0100925 } else {
926 codegen->Store(type, source, mem_op);
927 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800928 }
929
930 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100931 bool value_can_be_null = true; // TODO: Worth finding out this information?
932 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800933 }
934}
935
936void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000937 GenUnsafePut(invoke->GetLocations(),
938 Primitive::kPrimInt,
939 /* is_volatile */ false,
940 /* is_ordered */ false,
941 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800942}
943void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000944 GenUnsafePut(invoke->GetLocations(),
945 Primitive::kPrimInt,
946 /* is_volatile */ false,
947 /* is_ordered */ true,
948 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800949}
950void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000951 GenUnsafePut(invoke->GetLocations(),
952 Primitive::kPrimInt,
953 /* is_volatile */ true,
954 /* is_ordered */ false,
955 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800956}
957void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000958 GenUnsafePut(invoke->GetLocations(),
959 Primitive::kPrimNot,
960 /* is_volatile */ false,
961 /* is_ordered */ false,
962 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800963}
964void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000965 GenUnsafePut(invoke->GetLocations(),
966 Primitive::kPrimNot,
967 /* is_volatile */ false,
968 /* is_ordered */ true,
969 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800970}
971void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000972 GenUnsafePut(invoke->GetLocations(),
973 Primitive::kPrimNot,
974 /* is_volatile */ true,
975 /* is_ordered */ false,
976 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800977}
978void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000979 GenUnsafePut(invoke->GetLocations(),
980 Primitive::kPrimLong,
981 /* is_volatile */ false,
982 /* is_ordered */ false,
983 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800984}
985void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000986 GenUnsafePut(invoke->GetLocations(),
987 Primitive::kPrimLong,
988 /* is_volatile */ false,
989 /* is_ordered */ true,
990 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800991}
992void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000993 GenUnsafePut(invoke->GetLocations(),
994 Primitive::kPrimLong,
995 /* is_volatile */ true,
996 /* is_ordered */ false,
997 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800998}
999
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001000static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
1001 HInvoke* invoke,
1002 Primitive::Type type) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001003 LocationSummary* locations = new (arena) LocationSummary(invoke,
1004 LocationSummary::kNoCall,
1005 kIntrinsified);
1006 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1007 locations->SetInAt(1, Location::RequiresRegister());
1008 locations->SetInAt(2, Location::RequiresRegister());
1009 locations->SetInAt(3, Location::RequiresRegister());
1010 locations->SetInAt(4, Location::RequiresRegister());
1011
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001012 // If heap poisoning is enabled, we don't want the unpoisoning
1013 // operations to potentially clobber the output.
1014 Location::OutputOverlap overlaps = (kPoisonHeapReferences && type == Primitive::kPrimNot)
1015 ? Location::kOutputOverlap
1016 : Location::kNoOutputOverlap;
1017 locations->SetOut(Location::RequiresRegister(), overlaps);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001018}
1019
1020static void GenCas(LocationSummary* locations, Primitive::Type type, CodeGeneratorARM64* codegen) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001021 vixl::MacroAssembler* masm = codegen->GetAssembler()->vixl_masm_;
1022
1023 Register out = WRegisterFrom(locations->Out()); // Boolean result.
1024
1025 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1026 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1027 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1028 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1029
1030 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
1031 if (type == Primitive::kPrimNot) {
1032 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001033 bool value_can_be_null = true; // TODO: Worth finding out this information?
1034 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001035 }
1036
1037 UseScratchRegisterScope temps(masm);
1038 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1039 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1040
1041 Register tmp_32 = tmp_value.W();
1042
1043 __ Add(tmp_ptr, base.X(), Operand(offset));
1044
Roland Levillain4d027112015-07-01 15:41:14 +01001045 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1046 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001047 if (value.Is(expected)) {
1048 // Do not poison `value`, as it is the same register as
1049 // `expected`, which has just been poisoned.
1050 } else {
1051 codegen->GetAssembler()->PoisonHeapReference(value);
1052 }
Roland Levillain4d027112015-07-01 15:41:14 +01001053 }
1054
Andreas Gampe878d58c2015-01-15 23:24:00 -08001055 // do {
1056 // tmp_value = [tmp_ptr] - expected;
1057 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1058 // result = tmp_value != 0;
1059
1060 vixl::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001061 __ Bind(&loop_head);
1062 // TODO: When `type == Primitive::kPrimNot`, add a read barrier for
1063 // the reference stored in the object before attempting the CAS,
1064 // similar to the one in the art::Unsafe_compareAndSwapObject JNI
1065 // implementation.
1066 //
1067 // Note that this code is not (yet) used when read barriers are
1068 // enabled (see IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject).
1069 DCHECK(!(type == Primitive::kPrimNot && kEmitCompilerReadBarrier));
1070 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1071 __ Cmp(tmp_value, expected);
1072 __ B(&exit_loop, ne);
1073 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1074 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001075 __ Bind(&exit_loop);
1076 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001077
1078 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01001079 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001080 if (value.Is(expected)) {
1081 // Do not unpoison `value`, as it is the same register as
1082 // `expected`, which has just been unpoisoned.
1083 } else {
1084 codegen->GetAssembler()->UnpoisonHeapReference(value);
1085 }
Roland Levillain4d027112015-07-01 15:41:14 +01001086 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001087}
1088
1089void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001090 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001091}
1092void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001093 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001094}
1095void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00001096 // The UnsafeCASObject intrinsic is missing a read barrier, and
1097 // therefore sometimes does not work as expected (b/25883050).
1098 // Turn it off temporarily as a quick fix, until the read barrier is
1099 // implemented (see TODO in GenCAS below).
1100 //
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001101 // TODO(rpl): Fix this issue and re-enable this intrinsic with read barriers.
1102 if (kEmitCompilerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001103 return;
1104 }
1105
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001106 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001107}
1108
1109void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
1110 GenCas(invoke->GetLocations(), Primitive::kPrimInt, codegen_);
1111}
1112void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
1113 GenCas(invoke->GetLocations(), Primitive::kPrimLong, codegen_);
1114}
1115void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
1116 GenCas(invoke->GetLocations(), Primitive::kPrimNot, codegen_);
1117}
1118
1119void IntrinsicLocationsBuilderARM64::VisitStringCharAt(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001120 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1121 LocationSummary::kCallOnSlowPath,
1122 kIntrinsified);
1123 locations->SetInAt(0, Location::RequiresRegister());
1124 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray82f34492015-02-04 10:44:23 +00001125 // In case we need to go in the slow path, we can't have the output be the same
1126 // as the input: the current liveness analysis considers the input to be live
1127 // at the point of the call.
1128 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001129}
1130
1131void IntrinsicCodeGeneratorARM64::VisitStringCharAt(HInvoke* invoke) {
1132 vixl::MacroAssembler* masm = GetVIXLAssembler();
1133 LocationSummary* locations = invoke->GetLocations();
1134
1135 // Location of reference to data array
1136 const MemberOffset value_offset = mirror::String::ValueOffset();
1137 // Location of count
1138 const MemberOffset count_offset = mirror::String::CountOffset();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001139
1140 Register obj = WRegisterFrom(locations->InAt(0)); // String object pointer.
1141 Register idx = WRegisterFrom(locations->InAt(1)); // Index of character.
1142 Register out = WRegisterFrom(locations->Out()); // Result character.
1143
1144 UseScratchRegisterScope temps(masm);
1145 Register temp = temps.AcquireW();
1146 Register array_temp = temps.AcquireW(); // We can trade this for worse scheduling.
1147
1148 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
1149 // the cost.
1150 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
1151 // we will not optimize the code for constants (which would save a register).
1152
1153 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1154 codegen_->AddSlowPath(slow_path);
1155
1156 __ Ldr(temp, HeapOperand(obj, count_offset)); // temp = str.length.
1157 codegen_->MaybeRecordImplicitNullCheck(invoke);
1158 __ Cmp(idx, temp);
1159 __ B(hs, slow_path->GetEntryLabel());
1160
Jeff Hao848f70a2014-01-15 13:49:50 -08001161 __ Add(array_temp, obj, Operand(value_offset.Int32Value())); // array_temp := str.value.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001162
1163 // Load the value.
Jeff Hao848f70a2014-01-15 13:49:50 -08001164 __ Ldrh(out, MemOperand(array_temp.X(), idx, UXTW, 1)); // out := array_temp[idx].
Andreas Gampe878d58c2015-01-15 23:24:00 -08001165
1166 __ Bind(slow_path->GetExitLabel());
1167}
1168
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001169void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001170 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1171 LocationSummary::kCall,
1172 kIntrinsified);
1173 InvokeRuntimeCallingConvention calling_convention;
1174 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1175 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1176 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1177}
1178
1179void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
1180 vixl::MacroAssembler* masm = GetVIXLAssembler();
1181 LocationSummary* locations = invoke->GetLocations();
1182
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001183 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001184 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001185
1186 Register argument = WRegisterFrom(locations->InAt(1));
1187 __ Cmp(argument, 0);
1188 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1189 codegen_->AddSlowPath(slow_path);
1190 __ B(eq, slow_path->GetEntryLabel());
1191
1192 __ Ldr(
1193 lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pStringCompareTo).Int32Value()));
1194 __ Blr(lr);
1195 __ Bind(slow_path->GetExitLabel());
1196}
1197
Agi Csakiea34b402015-08-13 17:51:19 -07001198void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
1199 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1200 LocationSummary::kNoCall,
1201 kIntrinsified);
1202 locations->SetInAt(0, Location::RequiresRegister());
1203 locations->SetInAt(1, Location::RequiresRegister());
1204 // Temporary registers to store lengths of strings and for calculations.
1205 locations->AddTemp(Location::RequiresRegister());
1206 locations->AddTemp(Location::RequiresRegister());
1207
1208 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1209}
1210
1211void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
1212 vixl::MacroAssembler* masm = GetVIXLAssembler();
1213 LocationSummary* locations = invoke->GetLocations();
1214
1215 Register str = WRegisterFrom(locations->InAt(0));
1216 Register arg = WRegisterFrom(locations->InAt(1));
1217 Register out = XRegisterFrom(locations->Out());
1218
1219 UseScratchRegisterScope scratch_scope(masm);
1220 Register temp = scratch_scope.AcquireW();
1221 Register temp1 = WRegisterFrom(locations->GetTemp(0));
1222 Register temp2 = WRegisterFrom(locations->GetTemp(1));
1223
1224 vixl::Label loop;
1225 vixl::Label end;
1226 vixl::Label return_true;
1227 vixl::Label return_false;
1228
1229 // Get offsets of count, value, and class fields within a string object.
1230 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1231 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1232 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1233
1234 // Note that the null check must have been done earlier.
1235 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1236
1237 // Check if input is null, return false if it is.
1238 __ Cbz(arg, &return_false);
1239
1240 // Reference equality check, return true if same reference.
1241 __ Cmp(str, arg);
1242 __ B(&return_true, eq);
1243
1244 // Instanceof check for the argument by comparing class fields.
1245 // All string objects must have the same type since String cannot be subclassed.
1246 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1247 // If the argument is a string object, its class field must be equal to receiver's class field.
1248 __ Ldr(temp, MemOperand(str.X(), class_offset));
1249 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1250 __ Cmp(temp, temp1);
1251 __ B(&return_false, ne);
1252
1253 // Load lengths of this and argument strings.
1254 __ Ldr(temp, MemOperand(str.X(), count_offset));
1255 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1256 // Check if lengths are equal, return false if they're not.
1257 __ Cmp(temp, temp1);
1258 __ B(&return_false, ne);
1259 // Store offset of string value in preparation for comparison loop
1260 __ Mov(temp1, value_offset);
1261 // Return true if both strings are empty.
1262 __ Cbz(temp, &return_true);
1263
1264 // Assertions that must hold in order to compare strings 4 characters at a time.
1265 DCHECK_ALIGNED(value_offset, 8);
1266 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1267
1268 temp1 = temp1.X();
1269 temp2 = temp2.X();
1270
1271 // Loop to compare strings 4 characters at a time starting at the beginning of the string.
1272 // Ok to do this because strings are zero-padded to be 8-byte aligned.
1273 __ Bind(&loop);
1274 __ Ldr(out, MemOperand(str.X(), temp1));
1275 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1276 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1277 __ Cmp(out, temp2);
1278 __ B(&return_false, ne);
1279 __ Sub(temp, temp, Operand(4), SetFlags);
1280 __ B(&loop, gt);
1281
1282 // Return true and exit the function.
1283 // If loop does not result in returning false, we return true.
1284 __ Bind(&return_true);
1285 __ Mov(out, 1);
1286 __ B(&end);
1287
1288 // Return false and exit the function.
1289 __ Bind(&return_false);
1290 __ Mov(out, 0);
1291 __ Bind(&end);
1292}
1293
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001294static void GenerateVisitStringIndexOf(HInvoke* invoke,
1295 vixl::MacroAssembler* masm,
1296 CodeGeneratorARM64* codegen,
1297 ArenaAllocator* allocator,
1298 bool start_at_zero) {
1299 LocationSummary* locations = invoke->GetLocations();
1300 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
1301
1302 // Note that the null check must have been done earlier.
1303 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1304
1305 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1306 // or directly dispatch if we have a constant.
1307 SlowPathCodeARM64* slow_path = nullptr;
1308 if (invoke->InputAt(1)->IsIntConstant()) {
1309 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) > 0xFFFFU) {
1310 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1311 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1312 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1313 codegen->AddSlowPath(slow_path);
1314 __ B(slow_path->GetEntryLabel());
1315 __ Bind(slow_path->GetExitLabel());
1316 return;
1317 }
1318 } else {
1319 Register char_reg = WRegisterFrom(locations->InAt(1));
1320 __ Mov(tmp_reg, 0xFFFF);
1321 __ Cmp(char_reg, Operand(tmp_reg));
1322 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1323 codegen->AddSlowPath(slow_path);
1324 __ B(hi, slow_path->GetEntryLabel());
1325 }
1326
1327 if (start_at_zero) {
1328 // Start-index = 0.
1329 __ Mov(tmp_reg, 0);
1330 }
1331
1332 __ Ldr(lr, MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pIndexOf).Int32Value()));
1333 __ Blr(lr);
1334
1335 if (slow_path != nullptr) {
1336 __ Bind(slow_path->GetExitLabel());
1337 }
1338}
1339
1340void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1341 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1342 LocationSummary::kCall,
1343 kIntrinsified);
1344 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1345 // best to align the inputs accordingly.
1346 InvokeRuntimeCallingConvention calling_convention;
1347 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1348 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1349 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1350
1351 // Need a temp for slow-path codepoint compare, and need to send start_index=0.
1352 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1353}
1354
1355void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001356 GenerateVisitStringIndexOf(
1357 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001358}
1359
1360void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1361 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1362 LocationSummary::kCall,
1363 kIntrinsified);
1364 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1365 // best to align the inputs accordingly.
1366 InvokeRuntimeCallingConvention calling_convention;
1367 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1368 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1369 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1370 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1371
1372 // Need a temp for slow-path codepoint compare.
1373 locations->AddTemp(Location::RequiresRegister());
1374}
1375
1376void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001377 GenerateVisitStringIndexOf(
1378 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001379}
1380
Jeff Hao848f70a2014-01-15 13:49:50 -08001381void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1382 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1383 LocationSummary::kCall,
1384 kIntrinsified);
1385 InvokeRuntimeCallingConvention calling_convention;
1386 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1387 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1388 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1389 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1390 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1391}
1392
1393void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1394 vixl::MacroAssembler* masm = GetVIXLAssembler();
1395 LocationSummary* locations = invoke->GetLocations();
1396
1397 Register byte_array = WRegisterFrom(locations->InAt(0));
1398 __ Cmp(byte_array, 0);
1399 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1400 codegen_->AddSlowPath(slow_path);
1401 __ B(eq, slow_path->GetEntryLabel());
1402
1403 __ Ldr(lr,
1404 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromBytes).Int32Value()));
1405 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1406 __ Blr(lr);
1407 __ Bind(slow_path->GetExitLabel());
1408}
1409
1410void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1411 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1412 LocationSummary::kCall,
1413 kIntrinsified);
1414 InvokeRuntimeCallingConvention calling_convention;
1415 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1416 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1417 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1418 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1419}
1420
1421void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1422 vixl::MacroAssembler* masm = GetVIXLAssembler();
1423
1424 __ Ldr(lr,
1425 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromChars).Int32Value()));
1426 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1427 __ Blr(lr);
1428}
1429
1430void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
1431 // The inputs plus one temp.
1432 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1433 LocationSummary::kCall,
1434 kIntrinsified);
1435 InvokeRuntimeCallingConvention calling_convention;
1436 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1437 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1438 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1439 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1440}
1441
1442void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
1443 vixl::MacroAssembler* masm = GetVIXLAssembler();
1444 LocationSummary* locations = invoke->GetLocations();
1445
1446 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1447 __ Cmp(string_to_copy, 0);
1448 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1449 codegen_->AddSlowPath(slow_path);
1450 __ B(eq, slow_path->GetEntryLabel());
1451
1452 __ Ldr(lr,
1453 MemOperand(tr, QUICK_ENTRYPOINT_OFFSET(kArm64WordSize, pAllocStringFromString).Int32Value()));
1454 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1455 __ Blr(lr);
1456 __ Bind(slow_path->GetExitLabel());
1457}
1458
Aart Bikbb24bd02016-01-29 12:54:04 -08001459static void GenSignum(LocationSummary* locations, bool is_long, vixl::MacroAssembler* masm) {
1460 Location op1 = locations->InAt(0);
1461 Location out = locations->Out();
1462
1463 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
1464 Register out_reg = WRegisterFrom(out);
1465
1466 __ Cmp(op1_reg, 0);
1467 __ Cset(out_reg, gt); // out == +1 if GT or 0 otherwise
1468 __ Cinv(out_reg, out_reg, lt); // out == -1 if LT or unchanged otherwise
1469}
1470
1471void IntrinsicLocationsBuilderARM64::VisitIntegerSignum(HInvoke* invoke) {
1472 CreateIntToIntLocations(arena_, invoke);
1473}
1474
1475void IntrinsicCodeGeneratorARM64::VisitIntegerSignum(HInvoke* invoke) {
1476 GenSignum(invoke->GetLocations(), /* is_long */ false, GetVIXLAssembler());
1477}
1478
1479void IntrinsicLocationsBuilderARM64::VisitLongSignum(HInvoke* invoke) {
1480 CreateIntToIntLocations(arena_, invoke);
1481}
1482
1483void IntrinsicCodeGeneratorARM64::VisitLongSignum(HInvoke* invoke) {
1484 GenSignum(invoke->GetLocations(), /* is_long */ true, GetVIXLAssembler());
1485}
1486
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001487static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1488 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
1489 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1490 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1491
1492 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1493 LocationSummary::kCall,
1494 kIntrinsified);
1495 InvokeRuntimeCallingConvention calling_convention;
1496
1497 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1498 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1499}
1500
1501static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1502 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
1503 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1504 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1505 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1506
1507 LocationSummary* const locations = new (arena) LocationSummary(invoke,
1508 LocationSummary::kCall,
1509 kIntrinsified);
1510 InvokeRuntimeCallingConvention calling_convention;
1511
1512 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1513 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1514 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1515}
1516
1517static void GenFPToFPCall(HInvoke* invoke,
1518 vixl::MacroAssembler* masm,
1519 CodeGeneratorARM64* codegen,
1520 QuickEntrypointEnum entry) {
1521 __ Ldr(lr, MemOperand(tr, GetThreadOffset<kArm64WordSize>(entry).Int32Value()));
1522 __ Blr(lr);
1523 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
1524}
1525
1526void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
1527 CreateFPToFPCallLocations(arena_, invoke);
1528}
1529
1530void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
1531 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCos);
1532}
1533
1534void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
1535 CreateFPToFPCallLocations(arena_, invoke);
1536}
1537
1538void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
1539 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSin);
1540}
1541
1542void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
1543 CreateFPToFPCallLocations(arena_, invoke);
1544}
1545
1546void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
1547 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAcos);
1548}
1549
1550void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
1551 CreateFPToFPCallLocations(arena_, invoke);
1552}
1553
1554void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
1555 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAsin);
1556}
1557
1558void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
1559 CreateFPToFPCallLocations(arena_, invoke);
1560}
1561
1562void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
1563 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan);
1564}
1565
1566void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
1567 CreateFPToFPCallLocations(arena_, invoke);
1568}
1569
1570void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
1571 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCbrt);
1572}
1573
1574void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
1575 CreateFPToFPCallLocations(arena_, invoke);
1576}
1577
1578void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
1579 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickCosh);
1580}
1581
1582void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
1583 CreateFPToFPCallLocations(arena_, invoke);
1584}
1585
1586void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
1587 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExp);
1588}
1589
1590void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
1591 CreateFPToFPCallLocations(arena_, invoke);
1592}
1593
1594void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
1595 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickExpm1);
1596}
1597
1598void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
1599 CreateFPToFPCallLocations(arena_, invoke);
1600}
1601
1602void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
1603 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog);
1604}
1605
1606void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
1607 CreateFPToFPCallLocations(arena_, invoke);
1608}
1609
1610void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
1611 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickLog10);
1612}
1613
1614void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
1615 CreateFPToFPCallLocations(arena_, invoke);
1616}
1617
1618void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
1619 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickSinh);
1620}
1621
1622void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
1623 CreateFPToFPCallLocations(arena_, invoke);
1624}
1625
1626void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
1627 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTan);
1628}
1629
1630void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
1631 CreateFPToFPCallLocations(arena_, invoke);
1632}
1633
1634void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
1635 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickTanh);
1636}
1637
1638void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
1639 CreateFPFPToFPCallLocations(arena_, invoke);
1640}
1641
1642void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
1643 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickAtan2);
1644}
1645
1646void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
1647 CreateFPFPToFPCallLocations(arena_, invoke);
1648}
1649
1650void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
1651 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickHypot);
1652}
1653
1654void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
1655 CreateFPFPToFPCallLocations(arena_, invoke);
1656}
1657
1658void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
1659 GenFPToFPCall(invoke, GetVIXLAssembler(), codegen_, kQuickNextAfter);
1660}
1661
Andreas Gampe878d58c2015-01-15 23:24:00 -08001662// Unimplemented intrinsics.
1663
1664#define UNIMPLEMENTED_INTRINSIC(Name) \
1665void IntrinsicLocationsBuilderARM64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1666} \
1667void IntrinsicCodeGeneratorARM64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1668}
1669
Aart Bik3f67e692016-01-15 14:35:12 -08001670UNIMPLEMENTED_INTRINSIC(IntegerBitCount)
Aart Bik3f67e692016-01-15 14:35:12 -08001671UNIMPLEMENTED_INTRINSIC(LongBitCount)
Andreas Gampe878d58c2015-01-15 23:24:00 -08001672UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001673UNIMPLEMENTED_INTRINSIC(SystemArrayCopy)
Andreas Gampe878d58c2015-01-15 23:24:00 -08001674UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
Jeff Hao848f70a2014-01-15 13:49:50 -08001675UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Andreas Gampe878d58c2015-01-15 23:24:00 -08001676
Aart Bik59c94542016-01-25 14:20:58 -08001677UNIMPLEMENTED_INTRINSIC(FloatIsInfinite)
1678UNIMPLEMENTED_INTRINSIC(DoubleIsInfinite)
1679UNIMPLEMENTED_INTRINSIC(FloatIsNaN)
1680UNIMPLEMENTED_INTRINSIC(DoubleIsNaN)
1681
Aart Bik59c94542016-01-25 14:20:58 -08001682UNIMPLEMENTED_INTRINSIC(IntegerHighestOneBit)
1683UNIMPLEMENTED_INTRINSIC(LongHighestOneBit)
1684UNIMPLEMENTED_INTRINSIC(IntegerLowestOneBit)
1685UNIMPLEMENTED_INTRINSIC(LongLowestOneBit)
Aart Bik59c94542016-01-25 14:20:58 -08001686
1687// Rotate operations are handled as HRor instructions.
1688UNIMPLEMENTED_INTRINSIC(IntegerRotateLeft)
1689UNIMPLEMENTED_INTRINSIC(IntegerRotateRight)
1690UNIMPLEMENTED_INTRINSIC(LongRotateLeft)
1691UNIMPLEMENTED_INTRINSIC(LongRotateRight)
1692
Roland Levillain4d027112015-07-01 15:41:14 +01001693#undef UNIMPLEMENTED_INTRINSIC
1694
1695#undef __
1696
Andreas Gampe878d58c2015-01-15 23:24:00 -08001697} // namespace arm64
1698} // namespace art