blob: e5463bfc29b677e2989fdc65a5f01869f13d6d83 [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"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080025#include "lock_word.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080026#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070027#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080028#include "mirror/reference.h"
Vladimir Markoe39f14f2017-02-10 15:44:25 +000029#include "mirror/string-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080030#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070031#include "thread-current-inl.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080032#include "utils/arm64/assembler_arm64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080033
Scott Wakeling97c72b72016-06-24 16:19:36 +010034using namespace vixl::aarch64; // NOLINT(build/namespaces)
Andreas Gampe878d58c2015-01-15 23:24:00 -080035
Artem Serovaf4e42a2016-08-08 15:11:24 +010036// TODO(VIXL): Make VIXL compile with -Wshadow.
Scott Wakeling97c72b72016-06-24 16:19:36 +010037#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Wshadow"
Artem Serovaf4e42a2016-08-08 15:11:24 +010039#include "aarch64/disasm-aarch64.h"
40#include "aarch64/macro-assembler-aarch64.h"
Scott Wakeling97c72b72016-06-24 16:19:36 +010041#pragma GCC diagnostic pop
Andreas Gampe878d58c2015-01-15 23:24:00 -080042
43namespace art {
44
45namespace arm64 {
46
47using helpers::DRegisterFrom;
48using helpers::FPRegisterFrom;
49using helpers::HeapOperand;
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000050using helpers::LocationFrom;
Scott Wakeling9ee23f42015-07-23 10:44:35 +010051using helpers::OperandFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080052using helpers::RegisterFrom;
53using helpers::SRegisterFrom;
54using helpers::WRegisterFrom;
55using helpers::XRegisterFrom;
xueliang.zhong49924c92016-03-03 10:52:51 +000056using helpers::InputRegisterAt;
Scott Wakeling1f36f412016-04-21 11:13:45 +010057using helpers::OutputRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080058
Andreas Gampe878d58c2015-01-15 23:24:00 -080059namespace {
60
61ALWAYS_INLINE inline MemOperand AbsoluteHeapOperandFrom(Location location, size_t offset = 0) {
62 return MemOperand(XRegisterFrom(location), offset);
63}
64
65} // namespace
66
Scott Wakeling97c72b72016-06-24 16:19:36 +010067MacroAssembler* IntrinsicCodeGeneratorARM64::GetVIXLAssembler() {
Alexandre Rames087930f2016-08-02 13:45:28 +010068 return codegen_->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -080069}
70
71ArenaAllocator* IntrinsicCodeGeneratorARM64::GetAllocator() {
72 return codegen_->GetGraph()->GetArena();
73}
74
Alexandre Rames087930f2016-08-02 13:45:28 +010075#define __ codegen->GetVIXLAssembler()->
Andreas Gampe878d58c2015-01-15 23:24:00 -080076
77static void MoveFromReturnRegister(Location trg,
78 Primitive::Type type,
79 CodeGeneratorARM64* codegen) {
80 if (!trg.IsValid()) {
81 DCHECK(type == Primitive::kPrimVoid);
82 return;
83 }
84
85 DCHECK_NE(type, Primitive::kPrimVoid);
86
Jeff Hao848f70a2014-01-15 13:49:50 -080087 if (Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080088 Register trg_reg = RegisterFrom(trg, type);
89 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
90 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
91 } else {
92 FPRegister trg_reg = FPRegisterFrom(trg, type);
93 FPRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
94 __ Fmov(trg_reg, res_reg);
95 }
96}
97
Roland Levillainec525fc2015-04-28 15:50:20 +010098static void MoveArguments(HInvoke* invoke, CodeGeneratorARM64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010099 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +0100100 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800101}
102
103// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
104// call. This will copy the arguments into the positions for a regular call.
105//
106// Note: The actual parameters are required to be in the locations given by the invoke's location
107// summary. If an intrinsic modifies those locations before a slowpath call, they must be
108// restored!
109class IntrinsicSlowPathARM64 : public SlowPathCodeARM64 {
110 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000111 explicit IntrinsicSlowPathARM64(HInvoke* invoke)
112 : SlowPathCodeARM64(invoke), invoke_(invoke) { }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800113
114 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
115 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
116 __ Bind(GetEntryLabel());
117
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000118 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800119
Roland Levillainec525fc2015-04-28 15:50:20 +0100120 MoveArguments(invoke_, codegen);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800121
Artem Serov914d7a82017-02-07 14:33:49 +0000122 {
123 // Ensure that between the BLR (emitted by Generate*Call) and RecordPcInfo there
124 // are no pools emitted.
125 vixl::EmissionCheckScope guard(codegen->GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
126 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100127 codegen->GenerateStaticOrDirectCall(
128 invoke_->AsInvokeStaticOrDirect(), LocationFrom(kArtMethodRegister), this);
Artem Serov914d7a82017-02-07 14:33:49 +0000129 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100130 codegen->GenerateVirtualCall(
131 invoke_->AsInvokeVirtual(), LocationFrom(kArtMethodRegister), this);
Artem Serov914d7a82017-02-07 14:33:49 +0000132 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800133 }
134
135 // Copy the result back to the expected output.
136 Location out = invoke_->GetLocations()->Out();
137 if (out.IsValid()) {
138 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
139 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
140 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
141 }
142
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000143 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800144 __ B(GetExitLabel());
145 }
146
Alexandre Rames9931f312015-06-19 14:47:01 +0100147 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathARM64"; }
148
Andreas Gampe878d58c2015-01-15 23:24:00 -0800149 private:
150 // The instruction where this slow path is happening.
151 HInvoke* const invoke_;
152
153 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARM64);
154};
155
Roland Levillain0b671c02016-08-19 12:02:34 +0100156// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
157class ReadBarrierSystemArrayCopySlowPathARM64 : public SlowPathCodeARM64 {
158 public:
159 ReadBarrierSystemArrayCopySlowPathARM64(HInstruction* instruction, Location tmp)
160 : SlowPathCodeARM64(instruction), tmp_(tmp) {
161 DCHECK(kEmitCompilerReadBarrier);
162 DCHECK(kUseBakerReadBarrier);
163 }
164
165 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
166 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
167 LocationSummary* locations = instruction_->GetLocations();
168 DCHECK(locations->CanCall());
169 DCHECK(instruction_->IsInvokeStaticOrDirect())
170 << "Unexpected instruction in read barrier arraycopy slow path: "
171 << instruction_->DebugName();
172 DCHECK(instruction_->GetLocations()->Intrinsified());
173 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
174
175 const int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
176
177 Register src_curr_addr = XRegisterFrom(locations->GetTemp(0));
178 Register dst_curr_addr = XRegisterFrom(locations->GetTemp(1));
179 Register src_stop_addr = XRegisterFrom(locations->GetTemp(2));
180 Register tmp_reg = WRegisterFrom(tmp_);
181
182 __ Bind(GetEntryLabel());
183 vixl::aarch64::Label slow_copy_loop;
184 __ Bind(&slow_copy_loop);
185 __ Ldr(tmp_reg, MemOperand(src_curr_addr, element_size, PostIndex));
186 codegen->GetAssembler()->MaybeUnpoisonHeapReference(tmp_reg);
187 // TODO: Inline the mark bit check before calling the runtime?
188 // tmp_reg = ReadBarrier::Mark(tmp_reg);
189 // No need to save live registers; it's taken care of by the
190 // entrypoint. Also, there is no need to update the stack mask,
191 // as this runtime call will not trigger a garbage collection.
192 // (See ReadBarrierMarkSlowPathARM64::EmitNativeCode for more
193 // explanations.)
194 DCHECK_NE(tmp_.reg(), LR);
195 DCHECK_NE(tmp_.reg(), WSP);
196 DCHECK_NE(tmp_.reg(), WZR);
197 // IP0 is used internally by the ReadBarrierMarkRegX entry point
198 // as a temporary (and not preserved). It thus cannot be used by
199 // any live register in this slow path.
200 DCHECK_NE(LocationFrom(src_curr_addr).reg(), IP0);
201 DCHECK_NE(LocationFrom(dst_curr_addr).reg(), IP0);
202 DCHECK_NE(LocationFrom(src_stop_addr).reg(), IP0);
203 DCHECK_NE(tmp_.reg(), IP0);
204 DCHECK(0 <= tmp_.reg() && tmp_.reg() < kNumberOfWRegisters) << tmp_.reg();
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000205 // TODO: Load the entrypoint once before the loop, instead of
206 // loading it at every iteration.
Roland Levillain0b671c02016-08-19 12:02:34 +0100207 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100208 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(tmp_.reg());
Roland Levillain0b671c02016-08-19 12:02:34 +0100209 // This runtime call does not require a stack map.
210 codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
211 codegen->GetAssembler()->MaybePoisonHeapReference(tmp_reg);
212 __ Str(tmp_reg, MemOperand(dst_curr_addr, element_size, PostIndex));
213 __ Cmp(src_curr_addr, src_stop_addr);
214 __ B(&slow_copy_loop, ne);
215 __ B(GetExitLabel());
216 }
217
218 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathARM64"; }
219
220 private:
221 Location tmp_;
222
223 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARM64);
224};
Andreas Gampe878d58c2015-01-15 23:24:00 -0800225#undef __
226
227bool IntrinsicLocationsBuilderARM64::TryDispatch(HInvoke* invoke) {
228 Dispatch(invoke);
229 LocationSummary* res = invoke->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000230 if (res == nullptr) {
231 return false;
232 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000233 return res->Intrinsified();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800234}
235
236#define __ masm->
237
238static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
239 LocationSummary* locations = new (arena) LocationSummary(invoke,
240 LocationSummary::kNoCall,
241 kIntrinsified);
242 locations->SetInAt(0, Location::RequiresFpuRegister());
243 locations->SetOut(Location::RequiresRegister());
244}
245
246static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
247 LocationSummary* locations = new (arena) LocationSummary(invoke,
248 LocationSummary::kNoCall,
249 kIntrinsified);
250 locations->SetInAt(0, Location::RequiresRegister());
251 locations->SetOut(Location::RequiresFpuRegister());
252}
253
Scott Wakeling97c72b72016-06-24 16:19:36 +0100254static void MoveFPToInt(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800255 Location input = locations->InAt(0);
256 Location output = locations->Out();
257 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
258 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
259}
260
Scott Wakeling97c72b72016-06-24 16:19:36 +0100261static void MoveIntToFP(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800262 Location input = locations->InAt(0);
263 Location output = locations->Out();
264 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
265 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
266}
267
268void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
269 CreateFPToIntLocations(arena_, invoke);
270}
271void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
272 CreateIntToFPLocations(arena_, invoke);
273}
274
275void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000276 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800277}
278void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000279 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800280}
281
282void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
283 CreateFPToIntLocations(arena_, invoke);
284}
285void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
286 CreateIntToFPLocations(arena_, invoke);
287}
288
289void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000290 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800291}
292void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000293 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800294}
295
296static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
297 LocationSummary* locations = new (arena) LocationSummary(invoke,
298 LocationSummary::kNoCall,
299 kIntrinsified);
300 locations->SetInAt(0, Location::RequiresRegister());
301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
302}
303
304static void GenReverseBytes(LocationSummary* locations,
305 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100306 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800307 Location in = locations->InAt(0);
308 Location out = locations->Out();
309
310 switch (type) {
311 case Primitive::kPrimShort:
312 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
313 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
314 break;
315 case Primitive::kPrimInt:
316 case Primitive::kPrimLong:
317 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
318 break;
319 default:
320 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
321 UNREACHABLE();
322 }
323}
324
325void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
326 CreateIntToIntLocations(arena_, invoke);
327}
328
329void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
330 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
331}
332
333void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
334 CreateIntToIntLocations(arena_, invoke);
335}
336
337void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
338 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
339}
340
341void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
342 CreateIntToIntLocations(arena_, invoke);
343}
344
345void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
346 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetVIXLAssembler());
347}
348
Aart Bik7b565022016-01-28 14:36:22 -0800349static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
350 LocationSummary* locations = new (arena) LocationSummary(invoke,
351 LocationSummary::kNoCall,
352 kIntrinsified);
353 locations->SetInAt(0, Location::RequiresRegister());
354 locations->SetInAt(1, Location::RequiresRegister());
355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
356}
357
Scott Wakeling611d3392015-07-10 11:42:06 +0100358static void GenNumberOfLeadingZeros(LocationSummary* locations,
359 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100360 MacroAssembler* masm) {
Scott Wakeling611d3392015-07-10 11:42:06 +0100361 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
362
363 Location in = locations->InAt(0);
364 Location out = locations->Out();
365
366 __ Clz(RegisterFrom(out, type), RegisterFrom(in, type));
367}
368
369void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
370 CreateIntToIntLocations(arena_, invoke);
371}
372
373void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
374 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
375}
376
377void IntrinsicLocationsBuilderARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
378 CreateIntToIntLocations(arena_, invoke);
379}
380
381void IntrinsicCodeGeneratorARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
382 GenNumberOfLeadingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
383}
384
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100385static void GenNumberOfTrailingZeros(LocationSummary* locations,
386 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100387 MacroAssembler* masm) {
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100388 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
389
390 Location in = locations->InAt(0);
391 Location out = locations->Out();
392
393 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
394 __ Clz(RegisterFrom(out, type), RegisterFrom(out, type));
395}
396
397void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
398 CreateIntToIntLocations(arena_, invoke);
399}
400
401void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
402 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
403}
404
405void IntrinsicLocationsBuilderARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
406 CreateIntToIntLocations(arena_, invoke);
407}
408
409void IntrinsicCodeGeneratorARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
410 GenNumberOfTrailingZeros(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
411}
412
Andreas Gampe878d58c2015-01-15 23:24:00 -0800413static void GenReverse(LocationSummary* locations,
414 Primitive::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100415 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800416 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
417
418 Location in = locations->InAt(0);
419 Location out = locations->Out();
420
421 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
422}
423
424void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
425 CreateIntToIntLocations(arena_, invoke);
426}
427
428void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
429 GenReverse(invoke->GetLocations(), Primitive::kPrimInt, GetVIXLAssembler());
430}
431
432void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
433 CreateIntToIntLocations(arena_, invoke);
434}
435
436void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
437 GenReverse(invoke->GetLocations(), Primitive::kPrimLong, GetVIXLAssembler());
438}
439
Scott Wakeling97c72b72016-06-24 16:19:36 +0100440static void GenBitCount(HInvoke* instr, Primitive::Type type, MacroAssembler* masm) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100441 DCHECK(Primitive::IsIntOrLongType(type)) << type;
442 DCHECK_EQ(instr->GetType(), Primitive::kPrimInt);
443 DCHECK_EQ(Primitive::PrimitiveKind(instr->InputAt(0)->GetType()), type);
xueliang.zhong49924c92016-03-03 10:52:51 +0000444
xueliang.zhong49924c92016-03-03 10:52:51 +0000445 UseScratchRegisterScope temps(masm);
446
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000447 Register src = InputRegisterAt(instr, 0);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100448 Register dst = RegisterFrom(instr->GetLocations()->Out(), type);
449 FPRegister fpr = (type == Primitive::kPrimLong) ? temps.AcquireD() : temps.AcquireS();
xueliang.zhong49924c92016-03-03 10:52:51 +0000450
451 __ Fmov(fpr, src);
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000452 __ Cnt(fpr.V8B(), fpr.V8B());
453 __ Addv(fpr.B(), fpr.V8B());
xueliang.zhong49924c92016-03-03 10:52:51 +0000454 __ Fmov(dst, fpr);
455}
456
457void IntrinsicLocationsBuilderARM64::VisitLongBitCount(HInvoke* invoke) {
458 CreateIntToIntLocations(arena_, invoke);
459}
460
461void IntrinsicCodeGeneratorARM64::VisitLongBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100462 GenBitCount(invoke, Primitive::kPrimLong, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000463}
464
465void IntrinsicLocationsBuilderARM64::VisitIntegerBitCount(HInvoke* invoke) {
466 CreateIntToIntLocations(arena_, invoke);
467}
468
469void IntrinsicCodeGeneratorARM64::VisitIntegerBitCount(HInvoke* invoke) {
Roland Levillainfa3912e2016-04-01 18:21:55 +0100470 GenBitCount(invoke, Primitive::kPrimInt, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000471}
472
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100473static void GenHighestOneBit(HInvoke* invoke, Primitive::Type type, MacroAssembler* masm) {
474 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
475
476 UseScratchRegisterScope temps(masm);
477
478 Register src = InputRegisterAt(invoke, 0);
479 Register dst = RegisterFrom(invoke->GetLocations()->Out(), type);
480 Register temp = (type == Primitive::kPrimLong) ? temps.AcquireX() : temps.AcquireW();
481 size_t high_bit = (type == Primitive::kPrimLong) ? 63u : 31u;
482 size_t clz_high_bit = (type == Primitive::kPrimLong) ? 6u : 5u;
483
484 __ Clz(temp, src);
485 __ Mov(dst, UINT64_C(1) << high_bit); // MOV (bitmask immediate)
486 __ Bic(dst, dst, Operand(temp, LSL, high_bit - clz_high_bit)); // Clear dst if src was 0.
487 __ Lsr(dst, dst, temp);
488}
489
490void IntrinsicLocationsBuilderARM64::VisitIntegerHighestOneBit(HInvoke* invoke) {
491 CreateIntToIntLocations(arena_, invoke);
492}
493
494void IntrinsicCodeGeneratorARM64::VisitIntegerHighestOneBit(HInvoke* invoke) {
495 GenHighestOneBit(invoke, Primitive::kPrimInt, GetVIXLAssembler());
496}
497
498void IntrinsicLocationsBuilderARM64::VisitLongHighestOneBit(HInvoke* invoke) {
499 CreateIntToIntLocations(arena_, invoke);
500}
501
502void IntrinsicCodeGeneratorARM64::VisitLongHighestOneBit(HInvoke* invoke) {
503 GenHighestOneBit(invoke, Primitive::kPrimLong, GetVIXLAssembler());
504}
505
506static void GenLowestOneBit(HInvoke* invoke, Primitive::Type type, MacroAssembler* masm) {
507 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong);
508
509 UseScratchRegisterScope temps(masm);
510
511 Register src = InputRegisterAt(invoke, 0);
512 Register dst = RegisterFrom(invoke->GetLocations()->Out(), type);
513 Register temp = (type == Primitive::kPrimLong) ? temps.AcquireX() : temps.AcquireW();
514
515 __ Neg(temp, src);
516 __ And(dst, temp, src);
517}
518
519void IntrinsicLocationsBuilderARM64::VisitIntegerLowestOneBit(HInvoke* invoke) {
520 CreateIntToIntLocations(arena_, invoke);
521}
522
523void IntrinsicCodeGeneratorARM64::VisitIntegerLowestOneBit(HInvoke* invoke) {
524 GenLowestOneBit(invoke, Primitive::kPrimInt, GetVIXLAssembler());
525}
526
527void IntrinsicLocationsBuilderARM64::VisitLongLowestOneBit(HInvoke* invoke) {
528 CreateIntToIntLocations(arena_, invoke);
529}
530
531void IntrinsicCodeGeneratorARM64::VisitLongLowestOneBit(HInvoke* invoke) {
532 GenLowestOneBit(invoke, Primitive::kPrimLong, GetVIXLAssembler());
533}
534
Andreas Gampe878d58c2015-01-15 23:24:00 -0800535static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800536 LocationSummary* locations = new (arena) LocationSummary(invoke,
537 LocationSummary::kNoCall,
538 kIntrinsified);
539 locations->SetInAt(0, Location::RequiresFpuRegister());
540 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
541}
542
Scott Wakeling97c72b72016-06-24 16:19:36 +0100543static void MathAbsFP(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800544 Location in = locations->InAt(0);
545 Location out = locations->Out();
546
547 FPRegister in_reg = is64bit ? DRegisterFrom(in) : SRegisterFrom(in);
548 FPRegister out_reg = is64bit ? DRegisterFrom(out) : SRegisterFrom(out);
549
550 __ Fabs(out_reg, in_reg);
551}
552
553void IntrinsicLocationsBuilderARM64::VisitMathAbsDouble(HInvoke* invoke) {
554 CreateFPToFPLocations(arena_, invoke);
555}
556
557void IntrinsicCodeGeneratorARM64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000558 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800559}
560
561void IntrinsicLocationsBuilderARM64::VisitMathAbsFloat(HInvoke* invoke) {
562 CreateFPToFPLocations(arena_, invoke);
563}
564
565void IntrinsicCodeGeneratorARM64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000566 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800567}
568
569static void CreateIntToInt(ArenaAllocator* arena, HInvoke* invoke) {
570 LocationSummary* locations = new (arena) LocationSummary(invoke,
571 LocationSummary::kNoCall,
572 kIntrinsified);
573 locations->SetInAt(0, Location::RequiresRegister());
574 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
575}
576
577static void GenAbsInteger(LocationSummary* locations,
578 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100579 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800580 Location in = locations->InAt(0);
581 Location output = locations->Out();
582
583 Register in_reg = is64bit ? XRegisterFrom(in) : WRegisterFrom(in);
584 Register out_reg = is64bit ? XRegisterFrom(output) : WRegisterFrom(output);
585
586 __ Cmp(in_reg, Operand(0));
587 __ Cneg(out_reg, in_reg, lt);
588}
589
590void IntrinsicLocationsBuilderARM64::VisitMathAbsInt(HInvoke* invoke) {
591 CreateIntToInt(arena_, invoke);
592}
593
594void IntrinsicCodeGeneratorARM64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000595 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800596}
597
598void IntrinsicLocationsBuilderARM64::VisitMathAbsLong(HInvoke* invoke) {
599 CreateIntToInt(arena_, invoke);
600}
601
602void IntrinsicCodeGeneratorARM64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000603 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800604}
605
606static void GenMinMaxFP(LocationSummary* locations,
607 bool is_min,
608 bool is_double,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100609 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800610 Location op1 = locations->InAt(0);
611 Location op2 = locations->InAt(1);
612 Location out = locations->Out();
613
614 FPRegister op1_reg = is_double ? DRegisterFrom(op1) : SRegisterFrom(op1);
615 FPRegister op2_reg = is_double ? DRegisterFrom(op2) : SRegisterFrom(op2);
616 FPRegister out_reg = is_double ? DRegisterFrom(out) : SRegisterFrom(out);
617 if (is_min) {
618 __ Fmin(out_reg, op1_reg, op2_reg);
619 } else {
620 __ Fmax(out_reg, op1_reg, op2_reg);
621 }
622}
623
624static void CreateFPFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
625 LocationSummary* locations = new (arena) LocationSummary(invoke,
626 LocationSummary::kNoCall,
627 kIntrinsified);
628 locations->SetInAt(0, Location::RequiresFpuRegister());
629 locations->SetInAt(1, Location::RequiresFpuRegister());
630 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
631}
632
633void IntrinsicLocationsBuilderARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
634 CreateFPFPToFPLocations(arena_, invoke);
635}
636
637void IntrinsicCodeGeneratorARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000638 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800639}
640
641void IntrinsicLocationsBuilderARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
642 CreateFPFPToFPLocations(arena_, invoke);
643}
644
645void IntrinsicCodeGeneratorARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000646 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800647}
648
649void IntrinsicLocationsBuilderARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
650 CreateFPFPToFPLocations(arena_, invoke);
651}
652
653void IntrinsicCodeGeneratorARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000654 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800655}
656
657void IntrinsicLocationsBuilderARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
658 CreateFPFPToFPLocations(arena_, invoke);
659}
660
661void IntrinsicCodeGeneratorARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000662 GenMinMaxFP(
663 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800664}
665
666static void GenMinMax(LocationSummary* locations,
667 bool is_min,
668 bool is_long,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100669 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800670 Location op1 = locations->InAt(0);
671 Location op2 = locations->InAt(1);
672 Location out = locations->Out();
673
674 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
675 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
676 Register out_reg = is_long ? XRegisterFrom(out) : WRegisterFrom(out);
677
678 __ Cmp(op1_reg, op2_reg);
679 __ Csel(out_reg, op1_reg, op2_reg, is_min ? lt : gt);
680}
681
Andreas Gampe878d58c2015-01-15 23:24:00 -0800682void IntrinsicLocationsBuilderARM64::VisitMathMinIntInt(HInvoke* invoke) {
683 CreateIntIntToIntLocations(arena_, invoke);
684}
685
686void IntrinsicCodeGeneratorARM64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000687 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800688}
689
690void IntrinsicLocationsBuilderARM64::VisitMathMinLongLong(HInvoke* invoke) {
691 CreateIntIntToIntLocations(arena_, invoke);
692}
693
694void IntrinsicCodeGeneratorARM64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000695 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800696}
697
698void IntrinsicLocationsBuilderARM64::VisitMathMaxIntInt(HInvoke* invoke) {
699 CreateIntIntToIntLocations(arena_, invoke);
700}
701
702void IntrinsicCodeGeneratorARM64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000703 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800704}
705
706void IntrinsicLocationsBuilderARM64::VisitMathMaxLongLong(HInvoke* invoke) {
707 CreateIntIntToIntLocations(arena_, invoke);
708}
709
710void IntrinsicCodeGeneratorARM64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000711 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800712}
713
714void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
715 CreateFPToFPLocations(arena_, invoke);
716}
717
718void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
719 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100720 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800721 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
722}
723
724void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
725 CreateFPToFPLocations(arena_, invoke);
726}
727
728void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
729 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100730 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800731 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
732}
733
734void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
735 CreateFPToFPLocations(arena_, invoke);
736}
737
738void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
739 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100740 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800741 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
742}
743
744void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
745 CreateFPToFPLocations(arena_, invoke);
746}
747
748void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
749 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100750 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800751 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
752}
753
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100754static void CreateFPToIntPlusFPTempLocations(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800755 LocationSummary* locations = new (arena) LocationSummary(invoke,
756 LocationSummary::kNoCall,
757 kIntrinsified);
758 locations->SetInAt(0, Location::RequiresFpuRegister());
759 locations->SetOut(Location::RequiresRegister());
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100760 locations->AddTemp(Location::RequiresFpuRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800761}
762
Scott Wakeling97c72b72016-06-24 16:19:36 +0100763static void GenMathRound(HInvoke* invoke, bool is_double, vixl::aarch64::MacroAssembler* masm) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100764 // Java 8 API definition for Math.round():
765 // Return the closest long or int to the argument, with ties rounding to positive infinity.
766 //
767 // There is no single instruction in ARMv8 that can support the above definition.
768 // We choose to use FCVTAS here, because it has closest semantic.
769 // FCVTAS performs rounding to nearest integer, ties away from zero.
770 // For most inputs (positive values, zero or NaN), this instruction is enough.
771 // We only need a few handling code after FCVTAS if the input is negative half value.
772 //
773 // The reason why we didn't choose FCVTPS instruction here is that
774 // although it performs rounding toward positive infinity, it doesn't perform rounding to nearest.
775 // For example, FCVTPS(-1.9) = -1 and FCVTPS(1.1) = 2.
776 // If we were using this instruction, for most inputs, more handling code would be needed.
777 LocationSummary* l = invoke->GetLocations();
778 FPRegister in_reg = is_double ? DRegisterFrom(l->InAt(0)) : SRegisterFrom(l->InAt(0));
779 FPRegister tmp_fp = is_double ? DRegisterFrom(l->GetTemp(0)) : SRegisterFrom(l->GetTemp(0));
780 Register out_reg = is_double ? XRegisterFrom(l->Out()) : WRegisterFrom(l->Out());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100781 vixl::aarch64::Label done;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800782
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100783 // Round to nearest integer, ties away from zero.
784 __ Fcvtas(out_reg, in_reg);
785
786 // For positive values, zero or NaN inputs, rounding is done.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100787 __ Tbz(out_reg, out_reg.GetSizeInBits() - 1, &done);
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100788
789 // Handle input < 0 cases.
790 // If input is negative but not a tie, previous result (round to nearest) is valid.
791 // If input is a negative tie, out_reg += 1.
792 __ Frinta(tmp_fp, in_reg);
793 __ Fsub(tmp_fp, in_reg, tmp_fp);
794 __ Fcmp(tmp_fp, 0.5);
795 __ Cinc(out_reg, out_reg, eq);
796
797 __ Bind(&done);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800798}
799
800void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100801 CreateFPToIntPlusFPTempLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800802}
803
804void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100805 GenMathRound(invoke, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800806}
807
808void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100809 CreateFPToIntPlusFPTempLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800810}
811
812void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100813 GenMathRound(invoke, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800814}
815
816void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
817 CreateIntToIntLocations(arena_, invoke);
818}
819
820void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100821 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800822 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
823 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
824}
825
826void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
827 CreateIntToIntLocations(arena_, invoke);
828}
829
830void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100831 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800832 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
833 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
834}
835
836void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
837 CreateIntToIntLocations(arena_, invoke);
838}
839
840void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100841 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800842 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
843 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
844}
845
846void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
847 CreateIntToIntLocations(arena_, invoke);
848}
849
850void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100851 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800852 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
853 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
854}
855
856static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
857 LocationSummary* locations = new (arena) LocationSummary(invoke,
858 LocationSummary::kNoCall,
859 kIntrinsified);
860 locations->SetInAt(0, Location::RequiresRegister());
861 locations->SetInAt(1, Location::RequiresRegister());
862}
863
864void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
865 CreateIntIntToVoidLocations(arena_, invoke);
866}
867
868void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100869 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800870 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
871 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
872}
873
874void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
875 CreateIntIntToVoidLocations(arena_, invoke);
876}
877
878void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100879 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800880 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
881 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
882}
883
884void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
885 CreateIntIntToVoidLocations(arena_, invoke);
886}
887
888void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100889 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800890 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
891 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
892}
893
894void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
895 CreateIntIntToVoidLocations(arena_, invoke);
896}
897
898void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100899 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800900 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
901 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
902}
903
904void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
905 LocationSummary* locations = new (arena_) LocationSummary(invoke,
906 LocationSummary::kNoCall,
907 kIntrinsified);
908 locations->SetOut(Location::RequiresRegister());
909}
910
911void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
912 codegen_->Load(Primitive::kPrimNot, WRegisterFrom(invoke->GetLocations()->Out()),
Andreas Gampe542451c2016-07-26 09:02:02 -0700913 MemOperand(tr, Thread::PeerOffset<kArm64PointerSize>().Int32Value()));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800914}
915
916static void GenUnsafeGet(HInvoke* invoke,
917 Primitive::Type type,
918 bool is_volatile,
919 CodeGeneratorARM64* codegen) {
920 LocationSummary* locations = invoke->GetLocations();
921 DCHECK((type == Primitive::kPrimInt) ||
922 (type == Primitive::kPrimLong) ||
923 (type == Primitive::kPrimNot));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000924 Location base_loc = locations->InAt(1);
925 Register base = WRegisterFrom(base_loc); // Object pointer.
926 Location offset_loc = locations->InAt(2);
927 Register offset = XRegisterFrom(offset_loc); // Long offset.
928 Location trg_loc = locations->Out();
929 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800930
Roland Levillain44015862016-01-22 11:47:17 +0000931 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
932 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
Roland Levillain54f869e2017-03-06 13:54:11 +0000933 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillainbfea3352016-06-23 13:48:47 +0100934 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
935 trg_loc,
936 base,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100937 /* offset */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100938 /* index */ offset_loc,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100939 /* scale_factor */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100940 temp,
941 /* needs_null_check */ false,
942 is_volatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800943 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000944 // Other cases.
945 MemOperand mem_op(base.X(), offset);
946 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000947 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000948 } else {
949 codegen->Load(type, trg, mem_op);
950 }
Roland Levillain4d027112015-07-01 15:41:14 +0100951
Roland Levillain44015862016-01-22 11:47:17 +0000952 if (type == Primitive::kPrimNot) {
953 DCHECK(trg.IsW());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100954 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0u, offset_loc);
Roland Levillain44015862016-01-22 11:47:17 +0000955 }
Roland Levillain4d027112015-07-01 15:41:14 +0100956 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800957}
958
959static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000960 bool can_call = kEmitCompilerReadBarrier &&
961 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
962 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800963 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100964 (can_call
965 ? LocationSummary::kCallOnSlowPath
966 : LocationSummary::kNoCall),
Andreas Gampe878d58c2015-01-15 23:24:00 -0800967 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +0100968 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100969 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +0000970 // We need a temporary register for the read barrier marking slow
971 // path in CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier.
972 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +0100973 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800974 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
975 locations->SetInAt(1, Location::RequiresRegister());
976 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100977 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100978 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800979}
980
981void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
982 CreateIntIntIntToIntLocations(arena_, invoke);
983}
984void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
985 CreateIntIntIntToIntLocations(arena_, invoke);
986}
987void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
988 CreateIntIntIntToIntLocations(arena_, invoke);
989}
990void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
991 CreateIntIntIntToIntLocations(arena_, invoke);
992}
993void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
994 CreateIntIntIntToIntLocations(arena_, invoke);
995}
996void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
997 CreateIntIntIntToIntLocations(arena_, invoke);
998}
999
1000void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001001 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001002}
1003void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001004 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001005}
1006void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001007 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001008}
1009void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001010 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001011}
1012void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001013 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001014}
1015void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001016 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001017}
1018
1019static void CreateIntIntIntIntToVoid(ArenaAllocator* arena, HInvoke* invoke) {
1020 LocationSummary* locations = new (arena) LocationSummary(invoke,
1021 LocationSummary::kNoCall,
1022 kIntrinsified);
1023 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1024 locations->SetInAt(1, Location::RequiresRegister());
1025 locations->SetInAt(2, Location::RequiresRegister());
1026 locations->SetInAt(3, Location::RequiresRegister());
1027}
1028
1029void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
1030 CreateIntIntIntIntToVoid(arena_, invoke);
1031}
1032void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
1033 CreateIntIntIntIntToVoid(arena_, invoke);
1034}
1035void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
1036 CreateIntIntIntIntToVoid(arena_, invoke);
1037}
1038void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
1039 CreateIntIntIntIntToVoid(arena_, invoke);
1040}
1041void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1042 CreateIntIntIntIntToVoid(arena_, invoke);
1043}
1044void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1045 CreateIntIntIntIntToVoid(arena_, invoke);
1046}
1047void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
1048 CreateIntIntIntIntToVoid(arena_, invoke);
1049}
1050void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1051 CreateIntIntIntIntToVoid(arena_, invoke);
1052}
1053void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1054 CreateIntIntIntIntToVoid(arena_, invoke);
1055}
1056
Artem Serov914d7a82017-02-07 14:33:49 +00001057static void GenUnsafePut(HInvoke* invoke,
Andreas Gampe878d58c2015-01-15 23:24:00 -08001058 Primitive::Type type,
1059 bool is_volatile,
1060 bool is_ordered,
1061 CodeGeneratorARM64* codegen) {
Artem Serov914d7a82017-02-07 14:33:49 +00001062 LocationSummary* locations = invoke->GetLocations();
Alexandre Rames087930f2016-08-02 13:45:28 +01001063 MacroAssembler* masm = codegen->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001064
1065 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1066 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1067 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +01001068 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -08001069 MemOperand mem_op(base.X(), offset);
1070
Roland Levillain4d027112015-07-01 15:41:14 +01001071 {
1072 // We use a block to end the scratch scope before the write barrier, thus
1073 // freeing the temporary registers so they can be used in `MarkGCCard`.
1074 UseScratchRegisterScope temps(masm);
1075
1076 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1077 DCHECK(value.IsW());
1078 Register temp = temps.AcquireW();
1079 __ Mov(temp.W(), value.W());
1080 codegen->GetAssembler()->PoisonHeapReference(temp.W());
1081 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -08001082 }
Roland Levillain4d027112015-07-01 15:41:14 +01001083
1084 if (is_volatile || is_ordered) {
Artem Serov914d7a82017-02-07 14:33:49 +00001085 codegen->StoreRelease(invoke, type, source, mem_op, /* needs_null_check */ false);
Roland Levillain4d027112015-07-01 15:41:14 +01001086 } else {
1087 codegen->Store(type, source, mem_op);
1088 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001089 }
1090
1091 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001092 bool value_can_be_null = true; // TODO: Worth finding out this information?
1093 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001094 }
1095}
1096
1097void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001098 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001099 Primitive::kPrimInt,
1100 /* is_volatile */ false,
1101 /* is_ordered */ false,
1102 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001103}
1104void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001105 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001106 Primitive::kPrimInt,
1107 /* is_volatile */ false,
1108 /* is_ordered */ true,
1109 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001110}
1111void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001112 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001113 Primitive::kPrimInt,
1114 /* is_volatile */ true,
1115 /* is_ordered */ false,
1116 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001117}
1118void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001119 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001120 Primitive::kPrimNot,
1121 /* is_volatile */ false,
1122 /* is_ordered */ false,
1123 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001124}
1125void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001126 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001127 Primitive::kPrimNot,
1128 /* is_volatile */ false,
1129 /* is_ordered */ true,
1130 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001131}
1132void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001133 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001134 Primitive::kPrimNot,
1135 /* is_volatile */ true,
1136 /* is_ordered */ false,
1137 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001138}
1139void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001140 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001141 Primitive::kPrimLong,
1142 /* is_volatile */ false,
1143 /* is_ordered */ false,
1144 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001145}
1146void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001147 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001148 Primitive::kPrimLong,
1149 /* is_volatile */ false,
1150 /* is_ordered */ true,
1151 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001152}
1153void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001154 GenUnsafePut(invoke,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001155 Primitive::kPrimLong,
1156 /* is_volatile */ true,
1157 /* is_ordered */ false,
1158 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001159}
1160
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001161static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
1162 HInvoke* invoke,
1163 Primitive::Type type) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001164 bool can_call = kEmitCompilerReadBarrier &&
1165 kUseBakerReadBarrier &&
1166 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001167 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001168 (can_call
1169 ? LocationSummary::kCallOnSlowPath
1170 : LocationSummary::kNoCall),
Andreas Gampe878d58c2015-01-15 23:24:00 -08001171 kIntrinsified);
1172 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1173 locations->SetInAt(1, Location::RequiresRegister());
1174 locations->SetInAt(2, Location::RequiresRegister());
1175 locations->SetInAt(3, Location::RequiresRegister());
1176 locations->SetInAt(4, Location::RequiresRegister());
1177
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001178 // If heap poisoning is enabled, we don't want the unpoisoning
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001179 // operations to potentially clobber the output. Likewise when
1180 // emitting a (Baker) read barrier, which may call.
1181 Location::OutputOverlap overlaps =
1182 ((kPoisonHeapReferences && type == Primitive::kPrimNot) || can_call)
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001183 ? Location::kOutputOverlap
1184 : Location::kNoOutputOverlap;
1185 locations->SetOut(Location::RequiresRegister(), overlaps);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001186 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1187 // Temporary register for (Baker) read barrier.
1188 locations->AddTemp(Location::RequiresRegister());
1189 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001190}
1191
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001192static void GenCas(HInvoke* invoke, Primitive::Type type, CodeGeneratorARM64* codegen) {
Alexandre Rames087930f2016-08-02 13:45:28 +01001193 MacroAssembler* masm = codegen->GetVIXLAssembler();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001194 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001195
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001196 Location out_loc = locations->Out();
1197 Register out = WRegisterFrom(out_loc); // Boolean result.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001198
1199 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001200 Location offset_loc = locations->InAt(2);
1201 Register offset = XRegisterFrom(offset_loc); // Long offset.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001202 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1203 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1204
1205 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
1206 if (type == Primitive::kPrimNot) {
1207 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001208 bool value_can_be_null = true; // TODO: Worth finding out this information?
1209 codegen->MarkGCCard(base, value, value_can_be_null);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001210
1211 // The only read barrier implementation supporting the
1212 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1213 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1214
1215 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1216 Register temp = WRegisterFrom(locations->GetTemp(0));
1217 // Need to make sure the reference stored in the field is a to-space
1218 // one before attempting the CAS or the CAS could fail incorrectly.
Roland Levillainff487002017-03-07 16:50:01 +00001219 codegen->UpdateReferenceFieldWithBakerReadBarrier(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001220 invoke,
1221 out_loc, // Unused, used only as a "temporary" within the read barrier.
1222 base,
Roland Levillainff487002017-03-07 16:50:01 +00001223 /* field_offset */ offset_loc,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001224 temp,
1225 /* needs_null_check */ false,
Roland Levillainff487002017-03-07 16:50:01 +00001226 /* use_load_acquire */ false);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001227 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001228 }
1229
1230 UseScratchRegisterScope temps(masm);
1231 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1232 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1233
1234 Register tmp_32 = tmp_value.W();
1235
1236 __ Add(tmp_ptr, base.X(), Operand(offset));
1237
Roland Levillain4d027112015-07-01 15:41:14 +01001238 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1239 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001240 if (value.Is(expected)) {
1241 // Do not poison `value`, as it is the same register as
1242 // `expected`, which has just been poisoned.
1243 } else {
1244 codegen->GetAssembler()->PoisonHeapReference(value);
1245 }
Roland Levillain4d027112015-07-01 15:41:14 +01001246 }
1247
Andreas Gampe878d58c2015-01-15 23:24:00 -08001248 // do {
1249 // tmp_value = [tmp_ptr] - expected;
1250 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1251 // result = tmp_value != 0;
1252
Scott Wakeling97c72b72016-06-24 16:19:36 +01001253 vixl::aarch64::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001254 __ Bind(&loop_head);
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001255 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1256 __ Cmp(tmp_value, expected);
1257 __ B(&exit_loop, ne);
1258 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1259 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001260 __ Bind(&exit_loop);
1261 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001262
1263 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
Roland Levillain4d027112015-07-01 15:41:14 +01001264 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001265 if (value.Is(expected)) {
1266 // Do not unpoison `value`, as it is the same register as
1267 // `expected`, which has just been unpoisoned.
1268 } else {
1269 codegen->GetAssembler()->UnpoisonHeapReference(value);
1270 }
Roland Levillain4d027112015-07-01 15:41:14 +01001271 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001272}
1273
1274void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001275 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001276}
1277void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001278 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001279}
1280void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001281 // The only read barrier implementation supporting the
1282 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1283 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001284 return;
1285 }
1286
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001287 CreateIntIntIntIntIntToInt(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001288}
1289
1290void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001291 GenCas(invoke, Primitive::kPrimInt, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001292}
1293void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001294 GenCas(invoke, Primitive::kPrimLong, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001295}
1296void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001297 // The only read barrier implementation supporting the
1298 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1299 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001300
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001301 GenCas(invoke, Primitive::kPrimNot, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001302}
1303
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001304void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001305 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Scott Wakeling1f36f412016-04-21 11:13:45 +01001306 invoke->InputAt(1)->CanBeNull()
1307 ? LocationSummary::kCallOnSlowPath
1308 : LocationSummary::kNoCall,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001309 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001310 locations->SetInAt(0, Location::RequiresRegister());
1311 locations->SetInAt(1, Location::RequiresRegister());
1312 locations->AddTemp(Location::RequiresRegister());
1313 locations->AddTemp(Location::RequiresRegister());
1314 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001315 // Need temporary registers for String compression's feature.
1316 if (mirror::kUseStringCompression) {
1317 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001318 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001319 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001320}
1321
1322void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001323 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001324 LocationSummary* locations = invoke->GetLocations();
1325
Alexandre Rames2ea91532016-08-11 17:04:14 +01001326 Register str = InputRegisterAt(invoke, 0);
1327 Register arg = InputRegisterAt(invoke, 1);
1328 DCHECK(str.IsW());
1329 DCHECK(arg.IsW());
Scott Wakeling1f36f412016-04-21 11:13:45 +01001330 Register out = OutputRegister(invoke);
1331
1332 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1333 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1334 Register temp2 = WRegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001335 Register temp3;
jessicahandojo05765752016-09-09 19:01:32 -07001336 if (mirror::kUseStringCompression) {
1337 temp3 = WRegisterFrom(locations->GetTemp(3));
jessicahandojo05765752016-09-09 19:01:32 -07001338 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001339
Scott Wakeling97c72b72016-06-24 16:19:36 +01001340 vixl::aarch64::Label loop;
1341 vixl::aarch64::Label find_char_diff;
1342 vixl::aarch64::Label end;
jessicahandojo05765752016-09-09 19:01:32 -07001343 vixl::aarch64::Label different_compression;
Scott Wakeling1f36f412016-04-21 11:13:45 +01001344
1345 // Get offsets of count and value fields within a string object.
1346 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1347 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1348
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001349 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001350 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001351
Scott Wakeling1f36f412016-04-21 11:13:45 +01001352 // Take slow path and throw if input can be and is null.
1353 SlowPathCodeARM64* slow_path = nullptr;
1354 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1355 if (can_slow_path) {
1356 slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1357 codegen_->AddSlowPath(slow_path);
1358 __ Cbz(arg, slow_path->GetEntryLabel());
1359 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001360
Scott Wakeling1f36f412016-04-21 11:13:45 +01001361 // Reference equality check, return 0 if same reference.
1362 __ Subs(out, str, arg);
1363 __ B(&end, eq);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001364
jessicahandojo05765752016-09-09 19:01:32 -07001365 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001366 // Load `count` fields of this and argument strings.
jessicahandojo05765752016-09-09 19:01:32 -07001367 __ Ldr(temp3, HeapOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001368 __ Ldr(temp2, HeapOperand(arg, count_offset));
jessicahandojo05765752016-09-09 19:01:32 -07001369 // Clean out compression flag from lengths.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001370 __ Lsr(temp0, temp3, 1u);
1371 __ Lsr(temp1, temp2, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001372 } else {
1373 // Load lengths of this and argument strings.
1374 __ Ldr(temp0, HeapOperand(str, count_offset));
1375 __ Ldr(temp1, HeapOperand(arg, count_offset));
1376 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001377 // out = length diff.
1378 __ Subs(out, temp0, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001379 // temp0 = min(len(str), len(arg)).
1380 __ Csel(temp0, temp1, temp0, ge);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001381 // Shorter string is empty?
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001382 __ Cbz(temp0, &end);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001383
jessicahandojo05765752016-09-09 19:01:32 -07001384 if (mirror::kUseStringCompression) {
1385 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001386 __ Eor(temp2, temp2, Operand(temp3));
1387 // Interleave with compression flag extraction which is needed for both paths
1388 // and also set flags which is needed only for the different compressions path.
1389 __ Ands(temp3.W(), temp3.W(), Operand(1));
1390 __ Tbnz(temp2, 0, &different_compression); // Does not use flags.
jessicahandojo05765752016-09-09 19:01:32 -07001391 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001392 // Store offset of string value in preparation for comparison loop.
1393 __ Mov(temp1, value_offset);
jessicahandojo05765752016-09-09 19:01:32 -07001394 if (mirror::kUseStringCompression) {
1395 // For string compression, calculate the number of bytes to compare (not chars).
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001396 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
1397 __ Lsl(temp0, temp0, temp3);
jessicahandojo05765752016-09-09 19:01:32 -07001398 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001399
1400 UseScratchRegisterScope scratch_scope(masm);
1401 Register temp4 = scratch_scope.AcquireX();
1402
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001403 // Assertions that must hold in order to compare strings 8 bytes at a time.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001404 DCHECK_ALIGNED(value_offset, 8);
1405 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1406
1407 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1408 DCHECK_EQ(char_size, 2u);
1409
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001410 // Promote temp2 to an X reg, ready for LDR.
1411 temp2 = temp2.X();
Scott Wakeling1f36f412016-04-21 11:13:45 +01001412
1413 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1414 __ Bind(&loop);
Alexandre Rames2ea91532016-08-11 17:04:14 +01001415 __ Ldr(temp4, MemOperand(str.X(), temp1.X()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001416 __ Ldr(temp2, MemOperand(arg.X(), temp1.X()));
1417 __ Cmp(temp4, temp2);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001418 __ B(ne, &find_char_diff);
1419 __ Add(temp1, temp1, char_size * 4);
jessicahandojo05765752016-09-09 19:01:32 -07001420 // With string compression, we have compared 8 bytes, otherwise 4 chars.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001421 __ Subs(temp0, temp0, (mirror::kUseStringCompression) ? 8 : 4);
1422 __ B(&loop, hi);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001423 __ B(&end);
1424
1425 // Promote temp1 to an X reg, ready for EOR.
1426 temp1 = temp1.X();
1427
jessicahandojo05765752016-09-09 19:01:32 -07001428 // Find the single character difference.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001429 __ Bind(&find_char_diff);
1430 // Get the bit position of the first character that differs.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001431 __ Eor(temp1, temp2, temp4);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001432 __ Rbit(temp1, temp1);
1433 __ Clz(temp1, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001434
jessicahandojo05765752016-09-09 19:01:32 -07001435 // If the number of chars remaining <= the index where the difference occurs (0-3), then
Scott Wakeling1f36f412016-04-21 11:13:45 +01001436 // the difference occurs outside the remaining string data, so just return length diff (out).
jessicahandojo05765752016-09-09 19:01:32 -07001437 // Unlike ARM, we're doing the comparison in one go here, without the subtraction at the
1438 // find_char_diff_2nd_cmp path, so it doesn't matter whether the comparison is signed or
1439 // unsigned when string compression is disabled.
1440 // When it's enabled, the comparison must be unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001441 __ Cmp(temp0, Operand(temp1.W(), LSR, (mirror::kUseStringCompression) ? 3 : 4));
jessicahandojo05765752016-09-09 19:01:32 -07001442 __ B(ls, &end);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001443
Scott Wakeling1f36f412016-04-21 11:13:45 +01001444 // Extract the characters and calculate the difference.
jessicahandojo05765752016-09-09 19:01:32 -07001445 if (mirror:: kUseStringCompression) {
jessicahandojo05765752016-09-09 19:01:32 -07001446 __ Bic(temp1, temp1, 0x7);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001447 __ Bic(temp1, temp1, Operand(temp3.X(), LSL, 3u));
1448 } else {
1449 __ Bic(temp1, temp1, 0xf);
jessicahandojo05765752016-09-09 19:01:32 -07001450 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001451 __ Lsr(temp2, temp2, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001452 __ Lsr(temp4, temp4, temp1);
jessicahandojo05765752016-09-09 19:01:32 -07001453 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001454 // Prioritize the case of compressed strings and calculate such result first.
1455 __ Uxtb(temp1, temp4);
1456 __ Sub(out, temp1.W(), Operand(temp2.W(), UXTB));
1457 __ Tbz(temp3, 0u, &end); // If actually compressed, we're done.
jessicahandojo05765752016-09-09 19:01:32 -07001458 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001459 __ Uxth(temp4, temp4);
1460 __ Sub(out, temp4.W(), Operand(temp2.W(), UXTH));
jessicahandojo05765752016-09-09 19:01:32 -07001461
1462 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001463 __ B(&end);
1464 __ Bind(&different_compression);
1465
1466 // Comparison for different compression style.
jessicahandojo05765752016-09-09 19:01:32 -07001467 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1468 DCHECK_EQ(c_char_size, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001469 temp1 = temp1.W();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001470 temp2 = temp2.W();
1471 temp4 = temp4.W();
jessicahandojo05765752016-09-09 19:01:32 -07001472
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001473 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
1474 // Note that flags have been set by the `str` compression flag extraction to `temp3`
1475 // before branching to the `different_compression` label.
1476 __ Csel(temp1, str, arg, eq); // Pointer to the compressed string.
1477 __ Csel(temp2, str, arg, ne); // Pointer to the uncompressed string.
jessicahandojo05765752016-09-09 19:01:32 -07001478
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001479 // We want to free up the temp3, currently holding `str` compression flag, for comparison.
1480 // So, we move it to the bottom bit of the iteration count `temp0` which we then need to treat
1481 // as unsigned. Start by freeing the bit with a LSL and continue further down by a SUB which
1482 // will allow `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
1483 __ Lsl(temp0, temp0, 1u);
1484
1485 // Adjust temp1 and temp2 from string pointers to data pointers.
1486 __ Add(temp1, temp1, Operand(value_offset));
1487 __ Add(temp2, temp2, Operand(value_offset));
1488
1489 // Complete the move of the compression flag.
1490 __ Sub(temp0, temp0, Operand(temp3));
1491
1492 vixl::aarch64::Label different_compression_loop;
1493 vixl::aarch64::Label different_compression_diff;
1494
1495 __ Bind(&different_compression_loop);
1496 __ Ldrb(temp4, MemOperand(temp1.X(), c_char_size, PostIndex));
1497 __ Ldrh(temp3, MemOperand(temp2.X(), char_size, PostIndex));
1498 __ Subs(temp4, temp4, Operand(temp3));
1499 __ B(&different_compression_diff, ne);
1500 __ Subs(temp0, temp0, 2);
1501 __ B(&different_compression_loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001502 __ B(&end);
1503
1504 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001505 __ Bind(&different_compression_diff);
1506 __ Tst(temp0, Operand(1));
1507 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1508 "Expecting 0=compressed, 1=uncompressed");
1509 __ Cneg(out, temp4, ne);
jessicahandojo05765752016-09-09 19:01:32 -07001510 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001511
1512 __ Bind(&end);
1513
1514 if (can_slow_path) {
1515 __ Bind(slow_path->GetExitLabel());
1516 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001517}
1518
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001519// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1520// The normal loop plus the pre-header is 9 instructions without string compression and 12
1521// instructions with string compression. We can compare up to 8 bytes in 4 instructions
1522// (LDR+LDR+CMP+BNE) and up to 16 bytes in 5 instructions (LDP+LDP+CMP+CCMP+BNE). Allow up
1523// to 10 instructions for the unrolled loop.
1524constexpr size_t kShortConstStringEqualsCutoffInBytes = 32;
1525
1526static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1527 if (candidate->IsLoadString()) {
1528 HLoadString* load_string = candidate->AsLoadString();
1529 const DexFile& dex_file = load_string->GetDexFile();
1530 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1531 }
1532 return nullptr;
1533}
1534
Agi Csakiea34b402015-08-13 17:51:19 -07001535void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
1536 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1537 LocationSummary::kNoCall,
1538 kIntrinsified);
1539 locations->SetInAt(0, Location::RequiresRegister());
1540 locations->SetInAt(1, Location::RequiresRegister());
Agi Csakiea34b402015-08-13 17:51:19 -07001541
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001542 // For the generic implementation and for long const strings we need a temporary.
1543 // We do not need it for short const strings, up to 8 bytes, see code generation below.
1544 uint32_t const_string_length = 0u;
1545 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1546 if (const_string == nullptr) {
1547 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1548 }
1549 bool is_compressed =
1550 mirror::kUseStringCompression &&
1551 const_string != nullptr &&
1552 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1553 if (const_string == nullptr || const_string_length > (is_compressed ? 8u : 4u)) {
1554 locations->AddTemp(Location::RequiresRegister());
1555 }
1556
1557 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1558 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1559 // Then we shall need an extra temporary register instead of the output register.
Agi Csakiea34b402015-08-13 17:51:19 -07001560 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1561}
1562
1563void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001564 MacroAssembler* masm = GetVIXLAssembler();
Agi Csakiea34b402015-08-13 17:51:19 -07001565 LocationSummary* locations = invoke->GetLocations();
1566
1567 Register str = WRegisterFrom(locations->InAt(0));
1568 Register arg = WRegisterFrom(locations->InAt(1));
1569 Register out = XRegisterFrom(locations->Out());
1570
1571 UseScratchRegisterScope scratch_scope(masm);
1572 Register temp = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001573 Register temp1 = scratch_scope.AcquireW();
Agi Csakiea34b402015-08-13 17:51:19 -07001574
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001575 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001576 vixl::aarch64::Label end;
1577 vixl::aarch64::Label return_true;
1578 vixl::aarch64::Label return_false;
Agi Csakiea34b402015-08-13 17:51:19 -07001579
1580 // Get offsets of count, value, and class fields within a string object.
1581 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1582 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1583 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1584
1585 // Note that the null check must have been done earlier.
1586 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1587
Vladimir Marko53b52002016-05-24 19:30:45 +01001588 StringEqualsOptimizations optimizations(invoke);
1589 if (!optimizations.GetArgumentNotNull()) {
1590 // Check if input is null, return false if it is.
1591 __ Cbz(arg, &return_false);
1592 }
Agi Csakiea34b402015-08-13 17:51:19 -07001593
1594 // Reference equality check, return true if same reference.
1595 __ Cmp(str, arg);
1596 __ B(&return_true, eq);
1597
Vladimir Marko53b52002016-05-24 19:30:45 +01001598 if (!optimizations.GetArgumentIsString()) {
1599 // Instanceof check for the argument by comparing class fields.
1600 // All string objects must have the same type since String cannot be subclassed.
1601 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1602 // If the argument is a string object, its class field must be equal to receiver's class field.
1603 __ Ldr(temp, MemOperand(str.X(), class_offset));
1604 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1605 __ Cmp(temp, temp1);
1606 __ B(&return_false, ne);
1607 }
Agi Csakiea34b402015-08-13 17:51:19 -07001608
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001609 // Check if one of the inputs is a const string. Do not special-case both strings
1610 // being const, such cases should be handled by constant folding if needed.
1611 uint32_t const_string_length = 0u;
1612 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1613 if (const_string == nullptr) {
1614 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1615 if (const_string != nullptr) {
1616 std::swap(str, arg); // Make sure the const string is in `str`.
1617 }
1618 }
1619 bool is_compressed =
1620 mirror::kUseStringCompression &&
1621 const_string != nullptr &&
1622 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1623
1624 if (const_string != nullptr) {
1625 // Load `count` field of the argument string and check if it matches the const string.
1626 // Also compares the compression style, if differs return false.
1627 __ Ldr(temp, MemOperand(arg.X(), count_offset));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001628 // Temporarily release temp1 as we may not be able to embed the flagged count in CMP immediate.
1629 scratch_scope.Release(temp1);
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001630 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001631 temp1 = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001632 __ B(&return_false, ne);
1633 } else {
1634 // Load `count` fields of this and argument strings.
1635 __ Ldr(temp, MemOperand(str.X(), count_offset));
1636 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1637 // Check if `count` fields are equal, return false if they're not.
1638 // Also compares the compression style, if differs return false.
1639 __ Cmp(temp, temp1);
1640 __ B(&return_false, ne);
1641 }
Agi Csakiea34b402015-08-13 17:51:19 -07001642
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001643 // Assertions that must hold in order to compare strings 8 bytes at a time.
Agi Csakiea34b402015-08-13 17:51:19 -07001644 DCHECK_ALIGNED(value_offset, 8);
1645 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1646
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001647 if (const_string != nullptr &&
1648 const_string_length < (is_compressed ? kShortConstStringEqualsCutoffInBytes
1649 : kShortConstStringEqualsCutoffInBytes / 2u)) {
1650 // Load and compare the contents. Though we know the contents of the short const string
1651 // at compile time, materializing constants may be more code than loading from memory.
1652 int32_t offset = value_offset;
1653 size_t remaining_bytes =
1654 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 8u);
1655 temp = temp.X();
1656 temp1 = temp1.X();
1657 while (remaining_bytes > 8u) {
1658 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1659 __ Ldp(temp, temp1, MemOperand(str.X(), offset));
1660 __ Ldp(temp2, out, MemOperand(arg.X(), offset));
1661 __ Cmp(temp, temp2);
1662 __ Ccmp(temp1, out, NoFlag, eq);
1663 __ B(&return_false, ne);
1664 offset += 2u * sizeof(uint64_t);
1665 remaining_bytes -= 2u * sizeof(uint64_t);
1666 }
1667 if (remaining_bytes != 0u) {
1668 __ Ldr(temp, MemOperand(str.X(), offset));
1669 __ Ldr(temp1, MemOperand(arg.X(), offset));
1670 __ Cmp(temp, temp1);
1671 __ B(&return_false, ne);
1672 }
1673 } else {
1674 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1675 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1676 "Expecting 0=compressed, 1=uncompressed");
1677 __ Cbz(temp, &return_true);
1678
1679 if (mirror::kUseStringCompression) {
1680 // For string compression, calculate the number of bytes to compare (not chars).
1681 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1682 __ And(temp1, temp, Operand(1)); // Extract compression flag.
1683 __ Lsr(temp, temp, 1u); // Extract length.
1684 __ Lsl(temp, temp, temp1); // Calculate number of bytes to compare.
1685 }
1686
1687 // Store offset of string value in preparation for comparison loop
1688 __ Mov(temp1, value_offset);
1689
1690 temp1 = temp1.X();
1691 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1692 // Loop to compare strings 8 bytes at a time starting at the front of the string.
1693 // Ok to do this because strings are zero-padded to kObjectAlignment.
1694 __ Bind(&loop);
1695 __ Ldr(out, MemOperand(str.X(), temp1));
1696 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1697 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1698 __ Cmp(out, temp2);
1699 __ B(&return_false, ne);
1700 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1701 __ Sub(temp, temp, Operand(mirror::kUseStringCompression ? 8 : 4), SetFlags);
1702 __ B(&loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001703 }
1704
Agi Csakiea34b402015-08-13 17:51:19 -07001705 // Return true and exit the function.
1706 // If loop does not result in returning false, we return true.
1707 __ Bind(&return_true);
1708 __ Mov(out, 1);
1709 __ B(&end);
1710
1711 // Return false and exit the function.
1712 __ Bind(&return_false);
1713 __ Mov(out, 0);
1714 __ Bind(&end);
1715}
1716
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001717static void GenerateVisitStringIndexOf(HInvoke* invoke,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001718 MacroAssembler* masm,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001719 CodeGeneratorARM64* codegen,
1720 ArenaAllocator* allocator,
1721 bool start_at_zero) {
1722 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001723
1724 // Note that the null check must have been done earlier.
1725 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1726
1727 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001728 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001729 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001730 HInstruction* code_point = invoke->InputAt(1);
1731 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001732 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001733 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1734 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1735 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1736 codegen->AddSlowPath(slow_path);
1737 __ B(slow_path->GetEntryLabel());
1738 __ Bind(slow_path->GetExitLabel());
1739 return;
1740 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001741 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001742 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001743 __ Tst(char_reg, 0xFFFF0000);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001744 slow_path = new (allocator) IntrinsicSlowPathARM64(invoke);
1745 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001746 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001747 }
1748
1749 if (start_at_zero) {
1750 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001751 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001752 __ Mov(tmp_reg, 0);
1753 }
1754
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001755 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Roland Levillain42ad2882016-02-29 18:26:54 +00001756 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001757
1758 if (slow_path != nullptr) {
1759 __ Bind(slow_path->GetExitLabel());
1760 }
1761}
1762
1763void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
1764 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001765 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001766 kIntrinsified);
1767 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1768 // best to align the inputs accordingly.
1769 InvokeRuntimeCallingConvention calling_convention;
1770 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1771 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1772 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
1773
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001774 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001775 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1776}
1777
1778void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001779 GenerateVisitStringIndexOf(
1780 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001781}
1782
1783void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
1784 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001785 LocationSummary::kCallOnMainAndSlowPath,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001786 kIntrinsified);
1787 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1788 // best to align the inputs accordingly.
1789 InvokeRuntimeCallingConvention calling_convention;
1790 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1791 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1792 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1793 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimInt));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001794}
1795
1796void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001797 GenerateVisitStringIndexOf(
1798 invoke, GetVIXLAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001799}
1800
Jeff Hao848f70a2014-01-15 13:49:50 -08001801void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1802 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001803 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001804 kIntrinsified);
1805 InvokeRuntimeCallingConvention calling_convention;
1806 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1807 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1808 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1809 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
1810 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1811}
1812
1813void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001814 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001815 LocationSummary* locations = invoke->GetLocations();
1816
1817 Register byte_array = WRegisterFrom(locations->InAt(0));
1818 __ Cmp(byte_array, 0);
1819 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1820 codegen_->AddSlowPath(slow_path);
1821 __ B(eq, slow_path->GetEntryLabel());
1822
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001823 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001824 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001825 __ Bind(slow_path->GetExitLabel());
1826}
1827
1828void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
1829 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001830 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001831 kIntrinsified);
1832 InvokeRuntimeCallingConvention calling_convention;
1833 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1834 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1835 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1836 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1837}
1838
1839void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001840 // No need to emit code checking whether `locations->InAt(2)` is a null
1841 // pointer, as callers of the native method
1842 //
1843 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1844 //
1845 // all include a null check on `data` before calling that method.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001846 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001847 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001848}
1849
1850void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Jeff Hao848f70a2014-01-15 13:49:50 -08001851 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001852 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001853 kIntrinsified);
1854 InvokeRuntimeCallingConvention calling_convention;
1855 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Jeff Hao848f70a2014-01-15 13:49:50 -08001856 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
1857}
1858
1859void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001860 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001861 LocationSummary* locations = invoke->GetLocations();
1862
1863 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1864 __ Cmp(string_to_copy, 0);
1865 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
1866 codegen_->AddSlowPath(slow_path);
1867 __ B(eq, slow_path->GetEntryLabel());
1868
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001869 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001870 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001871 __ Bind(slow_path->GetExitLabel());
1872}
1873
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001874static void CreateFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1875 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
1876 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1877 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1878
1879 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001880 LocationSummary::kCallOnMainOnly,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001881 kIntrinsified);
1882 InvokeRuntimeCallingConvention calling_convention;
1883
1884 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1885 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1886}
1887
1888static void CreateFPFPToFPCallLocations(ArenaAllocator* arena, HInvoke* invoke) {
1889 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
1890 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1891 DCHECK(Primitive::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1892 DCHECK(Primitive::IsFloatingPointType(invoke->GetType()));
1893
1894 LocationSummary* const locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001895 LocationSummary::kCallOnMainOnly,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001896 kIntrinsified);
1897 InvokeRuntimeCallingConvention calling_convention;
1898
1899 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1900 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1901 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1902}
1903
1904static void GenFPToFPCall(HInvoke* invoke,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001905 CodeGeneratorARM64* codegen,
1906 QuickEntrypointEnum entry) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001907 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001908}
1909
1910void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
1911 CreateFPToFPCallLocations(arena_, invoke);
1912}
1913
1914void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001915 GenFPToFPCall(invoke, codegen_, kQuickCos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001916}
1917
1918void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
1919 CreateFPToFPCallLocations(arena_, invoke);
1920}
1921
1922void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001923 GenFPToFPCall(invoke, codegen_, kQuickSin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001924}
1925
1926void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
1927 CreateFPToFPCallLocations(arena_, invoke);
1928}
1929
1930void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001931 GenFPToFPCall(invoke, codegen_, kQuickAcos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001932}
1933
1934void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
1935 CreateFPToFPCallLocations(arena_, invoke);
1936}
1937
1938void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001939 GenFPToFPCall(invoke, codegen_, kQuickAsin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001940}
1941
1942void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
1943 CreateFPToFPCallLocations(arena_, invoke);
1944}
1945
1946void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001947 GenFPToFPCall(invoke, codegen_, kQuickAtan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001948}
1949
1950void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
1951 CreateFPToFPCallLocations(arena_, invoke);
1952}
1953
1954void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001955 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001956}
1957
1958void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
1959 CreateFPToFPCallLocations(arena_, invoke);
1960}
1961
1962void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001963 GenFPToFPCall(invoke, codegen_, kQuickCosh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001964}
1965
1966void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
1967 CreateFPToFPCallLocations(arena_, invoke);
1968}
1969
1970void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001971 GenFPToFPCall(invoke, codegen_, kQuickExp);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001972}
1973
1974void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
1975 CreateFPToFPCallLocations(arena_, invoke);
1976}
1977
1978void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001979 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001980}
1981
1982void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
1983 CreateFPToFPCallLocations(arena_, invoke);
1984}
1985
1986void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001987 GenFPToFPCall(invoke, codegen_, kQuickLog);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001988}
1989
1990void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
1991 CreateFPToFPCallLocations(arena_, invoke);
1992}
1993
1994void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001995 GenFPToFPCall(invoke, codegen_, kQuickLog10);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001996}
1997
1998void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
1999 CreateFPToFPCallLocations(arena_, invoke);
2000}
2001
2002void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00002003 GenFPToFPCall(invoke, codegen_, kQuickSinh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00002004}
2005
2006void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
2007 CreateFPToFPCallLocations(arena_, invoke);
2008}
2009
2010void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00002011 GenFPToFPCall(invoke, codegen_, kQuickTan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00002012}
2013
2014void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
2015 CreateFPToFPCallLocations(arena_, invoke);
2016}
2017
2018void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00002019 GenFPToFPCall(invoke, codegen_, kQuickTanh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00002020}
2021
2022void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
2023 CreateFPFPToFPCallLocations(arena_, invoke);
2024}
2025
2026void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00002027 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00002028}
2029
2030void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
2031 CreateFPFPToFPCallLocations(arena_, invoke);
2032}
2033
2034void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00002035 GenFPToFPCall(invoke, codegen_, kQuickHypot);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00002036}
2037
2038void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
2039 CreateFPFPToFPCallLocations(arena_, invoke);
2040}
2041
2042void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00002043 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00002044}
2045
Tim Zhang25abd6c2016-01-19 23:39:24 +08002046void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
2047 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2048 LocationSummary::kNoCall,
2049 kIntrinsified);
2050 locations->SetInAt(0, Location::RequiresRegister());
2051 locations->SetInAt(1, Location::RequiresRegister());
2052 locations->SetInAt(2, Location::RequiresRegister());
2053 locations->SetInAt(3, Location::RequiresRegister());
2054 locations->SetInAt(4, Location::RequiresRegister());
2055
2056 locations->AddTemp(Location::RequiresRegister());
2057 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01002058 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08002059}
2060
2061void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002062 MacroAssembler* masm = GetVIXLAssembler();
Tim Zhang25abd6c2016-01-19 23:39:24 +08002063 LocationSummary* locations = invoke->GetLocations();
2064
2065 // Check assumption that sizeof(Char) is 2 (used in scaling below).
2066 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2067 DCHECK_EQ(char_size, 2u);
2068
2069 // Location of data in char array buffer.
2070 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2071
2072 // Location of char array data in string.
2073 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2074
2075 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2076 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2077 Register srcObj = XRegisterFrom(locations->InAt(0));
2078 Register srcBegin = XRegisterFrom(locations->InAt(1));
2079 Register srcEnd = XRegisterFrom(locations->InAt(2));
2080 Register dstObj = XRegisterFrom(locations->InAt(3));
2081 Register dstBegin = XRegisterFrom(locations->InAt(4));
2082
2083 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002084 Register num_chr = XRegisterFrom(locations->GetTemp(1));
2085 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08002086
2087 UseScratchRegisterScope temps(masm);
2088 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01002089 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08002090
jessicahandojo05765752016-09-09 19:01:32 -07002091 vixl::aarch64::Label done;
2092 vixl::aarch64::Label compressed_string_loop;
2093 __ Sub(num_chr, srcEnd, srcBegin);
2094 // Early out for valid zero-length retrievals.
2095 __ Cbz(num_chr, &done);
Tim Zhang25abd6c2016-01-19 23:39:24 +08002096
Scott Wakelingdf109d92016-04-22 11:35:56 +01002097 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08002098 __ Add(dst_ptr, dstObj, Operand(data_offset));
2099 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
2100
jessicahandojo05765752016-09-09 19:01:32 -07002101 // src address to copy from.
2102 __ Add(src_ptr, srcObj, Operand(value_offset));
2103 vixl::aarch64::Label compressed_string_preloop;
2104 if (mirror::kUseStringCompression) {
2105 // Location of count in string.
2106 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2107 // String's length.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002108 __ Ldr(tmp2, MemOperand(srcObj, count_offset));
2109 __ Tbz(tmp2, 0, &compressed_string_preloop);
jessicahandojo05765752016-09-09 19:01:32 -07002110 }
2111 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002112
Tim Zhang25abd6c2016-01-19 23:39:24 +08002113 // Do the copy.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002114 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002115 vixl::aarch64::Label remainder;
Scott Wakelingdf109d92016-04-22 11:35:56 +01002116
Scott Wakelingdf109d92016-04-22 11:35:56 +01002117 // Save repairing the value of num_chr on the < 8 character path.
2118 __ Subs(tmp1, num_chr, 8);
2119 __ B(lt, &remainder);
2120
2121 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
2122 __ Mov(num_chr, tmp1);
2123
2124 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
2125 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08002126 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002127 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002128 __ Subs(num_chr, num_chr, 8);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002129 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002130 __ B(ge, &loop);
2131
2132 __ Adds(num_chr, num_chr, 8);
2133 __ B(eq, &done);
2134
2135 // Main loop for < 8 character case and remainder handling. Loads and stores one
2136 // 16-bit Java character at a time.
2137 __ Bind(&remainder);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002138 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002139 __ Subs(num_chr, num_chr, 1);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002140 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002141 __ B(gt, &remainder);
jessicahandojo05765752016-09-09 19:01:32 -07002142 __ B(&done);
2143
2144 if (mirror::kUseStringCompression) {
2145 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
2146 DCHECK_EQ(c_char_size, 1u);
2147 __ Bind(&compressed_string_preloop);
2148 __ Add(src_ptr, src_ptr, Operand(srcBegin));
2149 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2150 __ Bind(&compressed_string_loop);
2151 __ Ldrb(tmp1, MemOperand(src_ptr, c_char_size, PostIndex));
2152 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
2153 __ Subs(num_chr, num_chr, Operand(1));
2154 __ B(gt, &compressed_string_loop);
2155 }
Scott Wakelingdf109d92016-04-22 11:35:56 +01002156
Tim Zhang25abd6c2016-01-19 23:39:24 +08002157 __ Bind(&done);
2158}
2159
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002160// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
2161// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002162static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002163
2164static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
2165 uint32_t at,
2166 HInstruction* input) {
2167 HIntConstant* const_input = input->AsIntConstant();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002168 if (const_input != nullptr && !vixl::aarch64::Assembler::IsImmAddSub(const_input->GetValue())) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002169 locations->SetInAt(at, Location::RequiresRegister());
2170 } else {
2171 locations->SetInAt(at, Location::RegisterOrConstant(input));
2172 }
2173}
2174
2175void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
2176 // Check to see if we have known failures that will cause us to have to bail out
2177 // to the runtime, and just generate the runtime call directly.
2178 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2179 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
2180
2181 // The positions must be non-negative.
2182 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2183 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
2184 // We will have to fail anyways.
2185 return;
2186 }
2187
2188 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
2189 // native implementation.
2190 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2191 if (length != nullptr) {
2192 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002193 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002194 // Just call as normal.
2195 return;
2196 }
2197 }
2198
2199 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2200 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2201 LocationSummary::kCallOnSlowPath,
2202 kIntrinsified);
2203 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
2204 locations->SetInAt(0, Location::RequiresRegister());
2205 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2206 locations->SetInAt(2, Location::RequiresRegister());
2207 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2208 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2209
2210 locations->AddTemp(Location::RequiresRegister());
2211 locations->AddTemp(Location::RequiresRegister());
2212 locations->AddTemp(Location::RequiresRegister());
2213}
2214
Scott Wakeling97c72b72016-06-24 16:19:36 +01002215static void CheckSystemArrayCopyPosition(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002216 const Location& pos,
2217 const Register& input,
2218 const Location& length,
2219 SlowPathCodeARM64* slow_path,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002220 const Register& temp,
2221 bool length_is_input_length = false) {
2222 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
2223 if (pos.IsConstant()) {
2224 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
2225 if (pos_const == 0) {
2226 if (!length_is_input_length) {
2227 // Check that length(input) >= length.
2228 __ Ldr(temp, MemOperand(input, length_offset));
2229 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
2230 __ B(slow_path->GetEntryLabel(), lt);
2231 }
2232 } else {
2233 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002234 __ Ldr(temp, MemOperand(input, length_offset));
2235 __ Subs(temp, temp, pos_const);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002236 __ B(slow_path->GetEntryLabel(), lt);
2237
2238 // Check that (length(input) - pos) >= length.
2239 __ Cmp(temp, OperandFrom(length, Primitive::kPrimInt));
2240 __ B(slow_path->GetEntryLabel(), lt);
2241 }
2242 } else if (length_is_input_length) {
2243 // The only way the copy can succeed is if pos is zero.
2244 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
2245 } else {
2246 // Check that pos >= 0.
2247 Register pos_reg = WRegisterFrom(pos);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002248 __ Tbnz(pos_reg, pos_reg.GetSizeInBits() - 1, slow_path->GetEntryLabel());
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002249
2250 // Check that pos <= length(input) && (length(input) - pos) >= length.
2251 __ Ldr(temp, MemOperand(input, length_offset));
2252 __ Subs(temp, temp, pos_reg);
2253 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
2254 __ Ccmp(temp, OperandFrom(length, Primitive::kPrimInt), NFlag, ge);
2255 __ B(slow_path->GetEntryLabel(), lt);
2256 }
2257}
2258
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002259// Compute base source address, base destination address, and end
2260// source address for System.arraycopy* intrinsics in `src_base`,
2261// `dst_base` and `src_end` respectively.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002262static void GenSystemArrayCopyAddresses(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002263 Primitive::Type type,
2264 const Register& src,
2265 const Location& src_pos,
2266 const Register& dst,
2267 const Location& dst_pos,
2268 const Location& copy_length,
2269 const Register& src_base,
2270 const Register& dst_base,
2271 const Register& src_end) {
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002272 // This routine is used by the SystemArrayCopy and the SystemArrayCopyChar intrinsics.
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002273 DCHECK(type == Primitive::kPrimNot || type == Primitive::kPrimChar)
Roland Levillainebea3d22016-04-12 15:42:57 +01002274 << "Unexpected element type: " << type;
2275 const int32_t element_size = Primitive::ComponentSize(type);
2276 const int32_t element_size_shift = Primitive::ComponentSizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002277 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002278
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002279 if (src_pos.IsConstant()) {
2280 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002281 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002282 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002283 __ Add(src_base, src, data_offset);
2284 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002285 }
2286
2287 if (dst_pos.IsConstant()) {
2288 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002289 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002290 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002291 __ Add(dst_base, dst, data_offset);
2292 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002293 }
2294
2295 if (copy_length.IsConstant()) {
2296 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002297 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002298 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002299 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002300 }
2301}
2302
2303void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002304 MacroAssembler* masm = GetVIXLAssembler();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002305 LocationSummary* locations = invoke->GetLocations();
2306 Register src = XRegisterFrom(locations->InAt(0));
2307 Location src_pos = locations->InAt(1);
2308 Register dst = XRegisterFrom(locations->InAt(2));
2309 Location dst_pos = locations->InAt(3);
2310 Location length = locations->InAt(4);
2311
2312 SlowPathCodeARM64* slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2313 codegen_->AddSlowPath(slow_path);
2314
2315 // If source and destination are the same, take the slow path. Overlapping copy regions must be
2316 // copied in reverse and we can't know in all cases if it's needed.
2317 __ Cmp(src, dst);
2318 __ B(slow_path->GetEntryLabel(), eq);
2319
2320 // Bail out if the source is null.
2321 __ Cbz(src, slow_path->GetEntryLabel());
2322
2323 // Bail out if the destination is null.
2324 __ Cbz(dst, slow_path->GetEntryLabel());
2325
2326 if (!length.IsConstant()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002327 // Merge the following two comparisons into one:
2328 // If the length is negative, bail out (delegate to libcore's native implementation).
2329 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002330 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002331 __ B(slow_path->GetEntryLabel(), hi);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002332 } else {
2333 // We have already checked in the LocationsBuilder for the constant case.
2334 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
2335 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
2336 }
2337
2338 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
2339 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
2340 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
2341
2342 CheckSystemArrayCopyPosition(masm,
2343 src_pos,
2344 src,
2345 length,
2346 slow_path,
2347 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002348 false);
2349
2350 CheckSystemArrayCopyPosition(masm,
2351 dst_pos,
2352 dst,
2353 length,
2354 slow_path,
2355 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002356 false);
2357
2358 src_curr_addr = src_curr_addr.X();
2359 dst_curr_addr = dst_curr_addr.X();
2360 src_stop_addr = src_stop_addr.X();
2361
2362 GenSystemArrayCopyAddresses(masm,
2363 Primitive::kPrimChar,
2364 src,
2365 src_pos,
2366 dst,
2367 dst_pos,
2368 length,
2369 src_curr_addr,
2370 dst_curr_addr,
2371 src_stop_addr);
2372
2373 // Iterate over the arrays and do a raw copy of the chars.
2374 const int32_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
2375 UseScratchRegisterScope temps(masm);
2376 Register tmp = temps.AcquireW();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002377 vixl::aarch64::Label loop, done;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002378 __ Bind(&loop);
2379 __ Cmp(src_curr_addr, src_stop_addr);
2380 __ B(&done, eq);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002381 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, PostIndex));
2382 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, PostIndex));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002383 __ B(&loop);
2384 __ Bind(&done);
2385
2386 __ Bind(slow_path->GetExitLabel());
2387}
2388
donghui.baic2ec9ad2016-03-10 14:02:55 +08002389// We can choose to use the native implementation there for longer copy lengths.
2390static constexpr int32_t kSystemArrayCopyThreshold = 128;
2391
2392// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2393// We want to use two temporary registers in order to reduce the register pressure in arm64.
2394// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2395void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002396 // The only read barrier implementation supporting the
2397 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2398 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01002399 return;
2400 }
2401
donghui.baic2ec9ad2016-03-10 14:02:55 +08002402 // Check to see if we have known failures that will cause us to have to bail out
2403 // to the runtime, and just generate the runtime call directly.
2404 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2405 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2406
2407 // The positions must be non-negative.
2408 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2409 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2410 // We will have to fail anyways.
2411 return;
2412 }
2413
2414 // The length must be >= 0.
2415 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2416 if (length != nullptr) {
2417 int32_t len = length->GetValue();
2418 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2419 // Just call as normal.
2420 return;
2421 }
2422 }
2423
2424 SystemArrayCopyOptimizations optimizations(invoke);
2425
2426 if (optimizations.GetDestinationIsSource()) {
2427 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2428 // We only support backward copying if source and destination are the same.
2429 return;
2430 }
2431 }
2432
2433 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2434 // We currently don't intrinsify primitive copying.
2435 return;
2436 }
2437
2438 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
2439 LocationSummary* locations = new (allocator) LocationSummary(invoke,
2440 LocationSummary::kCallOnSlowPath,
2441 kIntrinsified);
2442 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2443 locations->SetInAt(0, Location::RequiresRegister());
2444 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2445 locations->SetInAt(2, Location::RequiresRegister());
2446 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2447 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2448
2449 locations->AddTemp(Location::RequiresRegister());
2450 locations->AddTemp(Location::RequiresRegister());
Roland Levillain0b671c02016-08-19 12:02:34 +01002451 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2452 // Temporary register IP0, obtained from the VIXL scratch register
2453 // pool, cannot be used in ReadBarrierSystemArrayCopySlowPathARM64
2454 // (because that register is clobbered by ReadBarrierMarkRegX
Roland Levillain54f869e2017-03-06 13:54:11 +00002455 // entry points). It cannot be used in calls to
2456 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier
2457 // either. For these reasons, get a third extra temporary register
2458 // from the register allocator.
Roland Levillain0b671c02016-08-19 12:02:34 +01002459 locations->AddTemp(Location::RequiresRegister());
Roland Levillain54f869e2017-03-06 13:54:11 +00002460 } else {
2461 // Cases other than Baker read barriers: the third temporary will
2462 // be acquired from the VIXL scratch register pool.
Roland Levillain0b671c02016-08-19 12:02:34 +01002463 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002464}
2465
2466void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002467 // The only read barrier implementation supporting the
2468 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2469 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002470
Scott Wakeling97c72b72016-06-24 16:19:36 +01002471 MacroAssembler* masm = GetVIXLAssembler();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002472 LocationSummary* locations = invoke->GetLocations();
2473
2474 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2475 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2476 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2477 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002478 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002479
2480 Register src = XRegisterFrom(locations->InAt(0));
2481 Location src_pos = locations->InAt(1);
2482 Register dest = XRegisterFrom(locations->InAt(2));
2483 Location dest_pos = locations->InAt(3);
2484 Location length = locations->InAt(4);
2485 Register temp1 = WRegisterFrom(locations->GetTemp(0));
Roland Levillain0b671c02016-08-19 12:02:34 +01002486 Location temp1_loc = LocationFrom(temp1);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002487 Register temp2 = WRegisterFrom(locations->GetTemp(1));
Roland Levillain0b671c02016-08-19 12:02:34 +01002488 Location temp2_loc = LocationFrom(temp2);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002489
Roland Levillain0b671c02016-08-19 12:02:34 +01002490 SlowPathCodeARM64* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathARM64(invoke);
2491 codegen_->AddSlowPath(intrinsic_slow_path);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002492
Scott Wakeling97c72b72016-06-24 16:19:36 +01002493 vixl::aarch64::Label conditions_on_positions_validated;
donghui.baic2ec9ad2016-03-10 14:02:55 +08002494 SystemArrayCopyOptimizations optimizations(invoke);
2495
donghui.baic2ec9ad2016-03-10 14:02:55 +08002496 // If source and destination are the same, we go to slow path if we need to do
2497 // forward copying.
2498 if (src_pos.IsConstant()) {
2499 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2500 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002501 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2502 if (optimizations.GetDestinationIsSource()) {
2503 // Checked when building locations.
2504 DCHECK_GE(src_pos_constant, dest_pos_constant);
2505 } else if (src_pos_constant < dest_pos_constant) {
2506 __ Cmp(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002507 __ B(intrinsic_slow_path->GetEntryLabel(), eq);
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002508 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002509 // Checked when building locations.
2510 DCHECK(!optimizations.GetDestinationIsSource()
2511 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2512 } else {
2513 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002514 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002515 __ B(&conditions_on_positions_validated, ne);
2516 }
2517 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
Roland Levillain0b671c02016-08-19 12:02:34 +01002518 __ B(intrinsic_slow_path->GetEntryLabel(), gt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002519 }
2520 } else {
2521 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002522 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002523 __ B(&conditions_on_positions_validated, ne);
2524 }
2525 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2526 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
Roland Levillain0b671c02016-08-19 12:02:34 +01002527 __ B(intrinsic_slow_path->GetEntryLabel(), lt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002528 }
2529
2530 __ Bind(&conditions_on_positions_validated);
2531
2532 if (!optimizations.GetSourceIsNotNull()) {
2533 // Bail out if the source is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002534 __ Cbz(src, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002535 }
2536
2537 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2538 // Bail out if the destination is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002539 __ Cbz(dest, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002540 }
2541
2542 // We have already checked in the LocationsBuilder for the constant case.
2543 if (!length.IsConstant() &&
2544 !optimizations.GetCountIsSourceLength() &&
2545 !optimizations.GetCountIsDestinationLength()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002546 // Merge the following two comparisons into one:
2547 // If the length is negative, bail out (delegate to libcore's native implementation).
2548 // If the length >= 128 then (currently) prefer native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002549 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002550 __ B(intrinsic_slow_path->GetEntryLabel(), hs);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002551 }
2552 // Validity checks: source.
2553 CheckSystemArrayCopyPosition(masm,
2554 src_pos,
2555 src,
2556 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002557 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002558 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002559 optimizations.GetCountIsSourceLength());
2560
2561 // Validity checks: dest.
2562 CheckSystemArrayCopyPosition(masm,
2563 dest_pos,
2564 dest,
2565 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002566 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002567 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002568 optimizations.GetCountIsDestinationLength());
2569 {
2570 // We use a block to end the scratch scope before the write barrier, thus
2571 // freeing the temporary registers so they can be used in `MarkGCCard`.
2572 UseScratchRegisterScope temps(masm);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002573 Location temp3_loc; // Used only for Baker read barrier.
Roland Levillain54f869e2017-03-06 13:54:11 +00002574 Register temp3;
2575 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002576 temp3_loc = locations->GetTemp(2);
2577 temp3 = WRegisterFrom(temp3_loc);
Roland Levillain54f869e2017-03-06 13:54:11 +00002578 } else {
2579 temp3 = temps.AcquireW();
2580 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002581
donghui.baic2ec9ad2016-03-10 14:02:55 +08002582 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2583 // Check whether all elements of the source array are assignable to the component
2584 // type of the destination array. We do two checks: the classes are the same,
2585 // or the destination is Object[]. If none of these checks succeed, we go to the
2586 // slow path.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002587
Roland Levillain0b671c02016-08-19 12:02:34 +01002588 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2589 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2590 // /* HeapReference<Class> */ temp1 = src->klass_
2591 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2592 temp1_loc,
2593 src.W(),
2594 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002595 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002596 /* needs_null_check */ false,
2597 /* use_load_acquire */ false);
2598 // Bail out if the source is not a non primitive array.
2599 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2600 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2601 temp1_loc,
2602 temp1,
2603 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002604 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002605 /* needs_null_check */ false,
2606 /* use_load_acquire */ false);
2607 __ Cbz(temp1, intrinsic_slow_path->GetEntryLabel());
2608 // If heap poisoning is enabled, `temp1` has been unpoisoned
2609 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2610 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2611 __ Ldrh(temp1, HeapOperand(temp1, primitive_offset));
2612 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2613 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002614 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002615
2616 // /* HeapReference<Class> */ temp1 = dest->klass_
2617 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2618 temp1_loc,
2619 dest.W(),
2620 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002621 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002622 /* needs_null_check */ false,
2623 /* use_load_acquire */ false);
2624
2625 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2626 // Bail out if the destination is not a non primitive array.
2627 //
2628 // Register `temp1` is not trashed by the read barrier emitted
2629 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2630 // method produces a call to a ReadBarrierMarkRegX entry point,
2631 // which saves all potentially live registers, including
2632 // temporaries such a `temp1`.
2633 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2634 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2635 temp2_loc,
2636 temp1,
2637 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002638 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002639 /* needs_null_check */ false,
2640 /* use_load_acquire */ false);
2641 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2642 // If heap poisoning is enabled, `temp2` has been unpoisoned
2643 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2644 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2645 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
2646 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2647 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
2648 }
2649
2650 // For the same reason given earlier, `temp1` is not trashed by the
2651 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2652 // /* HeapReference<Class> */ temp2 = src->klass_
2653 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2654 temp2_loc,
2655 src.W(),
2656 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002657 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002658 /* needs_null_check */ false,
2659 /* use_load_acquire */ false);
2660 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2661 __ Cmp(temp1, temp2);
2662
2663 if (optimizations.GetDestinationIsTypedObjectArray()) {
2664 vixl::aarch64::Label do_copy;
2665 __ B(&do_copy, eq);
2666 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2667 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2668 temp1_loc,
2669 temp1,
2670 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002671 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002672 /* needs_null_check */ false,
2673 /* use_load_acquire */ false);
2674 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2675 // We do not need to emit a read barrier for the following
2676 // heap reference load, as `temp1` is only used in a
2677 // comparison with null below, and this reference is not
2678 // kept afterwards.
2679 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2680 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2681 __ Bind(&do_copy);
2682 } else {
2683 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2684 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002685 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002686 // Non read barrier code.
2687
2688 // /* HeapReference<Class> */ temp1 = dest->klass_
2689 __ Ldr(temp1, MemOperand(dest, class_offset));
2690 // /* HeapReference<Class> */ temp2 = src->klass_
2691 __ Ldr(temp2, MemOperand(src, class_offset));
2692 bool did_unpoison = false;
2693 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2694 !optimizations.GetSourceIsNonPrimitiveArray()) {
2695 // One or two of the references need to be unpoisoned. Unpoison them
2696 // both to make the identity check valid.
2697 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2698 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2699 did_unpoison = true;
2700 }
2701
2702 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2703 // Bail out if the destination is not a non primitive array.
2704 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2705 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2706 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2707 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2708 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2709 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2710 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2711 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2712 }
2713
2714 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2715 // Bail out if the source is not a non primitive array.
2716 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2717 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2718 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2719 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2720 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2721 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2722 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2723 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2724 }
2725
2726 __ Cmp(temp1, temp2);
2727
2728 if (optimizations.GetDestinationIsTypedObjectArray()) {
2729 vixl::aarch64::Label do_copy;
2730 __ B(&do_copy, eq);
2731 if (!did_unpoison) {
2732 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2733 }
2734 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2735 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2736 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2737 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2738 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2739 // No need to unpoison the result, we're comparing against null.
2740 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2741 __ Bind(&do_copy);
2742 } else {
2743 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2744 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002745 }
2746 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2747 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2748 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002749 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2750 // /* HeapReference<Class> */ temp1 = src->klass_
2751 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2752 temp1_loc,
2753 src.W(),
2754 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002755 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002756 /* needs_null_check */ false,
2757 /* use_load_acquire */ false);
2758 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2759 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2760 temp2_loc,
2761 temp1,
2762 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002763 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002764 /* needs_null_check */ false,
2765 /* use_load_acquire */ false);
2766 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2767 // If heap poisoning is enabled, `temp2` has been unpoisoned
2768 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2769 } else {
2770 // /* HeapReference<Class> */ temp1 = src->klass_
2771 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2772 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2773 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2774 __ Ldr(temp2, HeapOperand(temp1, component_offset));
2775 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2776 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2777 }
2778 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2779 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
donghui.baic2ec9ad2016-03-10 14:02:55 +08002780 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain0b671c02016-08-19 12:02:34 +01002781 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002782 }
2783
Roland Levillain1663d162017-03-17 15:15:21 +00002784 if (length.IsConstant() && length.GetConstant()->AsIntConstant()->GetValue() == 0) {
2785 // Null constant length: not need to emit the loop code at all.
Roland Levillain0b671c02016-08-19 12:02:34 +01002786 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002787 Register src_curr_addr = temp1.X();
2788 Register dst_curr_addr = temp2.X();
2789 Register src_stop_addr = temp3.X();
2790 vixl::aarch64::Label done;
2791 const Primitive::Type type = Primitive::kPrimNot;
2792 const int32_t element_size = Primitive::ComponentSize(type);
2793
2794 if (length.IsRegister()) {
2795 // Don't enter the copy loop if the length is null.
2796 __ Cbz(WRegisterFrom(length), &done);
2797 }
2798
2799 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2800 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2801
2802 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002803 // also CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002804 //
2805 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2806 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2807 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2808 // if (is_gray) {
2809 // // Slow-path copy.
2810 // do {
2811 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2812 // } while (src_ptr != end_ptr)
2813 // } else {
2814 // // Fast-path copy.
2815 // do {
2816 // *dest_ptr++ = *src_ptr++;
2817 // } while (src_ptr != end_ptr)
2818 // }
2819
2820 // Make sure `tmp` is not IP0, as it is clobbered by
2821 // ReadBarrierMarkRegX entry points in
2822 // ReadBarrierSystemArrayCopySlowPathARM64.
Roland Levillain1ca955d2017-04-13 19:34:30 +01002823 DCHECK(temps.IsAvailable(ip0));
Roland Levillain1663d162017-03-17 15:15:21 +00002824 temps.Exclude(ip0);
Roland Levillain0b671c02016-08-19 12:02:34 +01002825 Register tmp = temps.AcquireW();
Roland Levillain1663d162017-03-17 15:15:21 +00002826 DCHECK_NE(LocationFrom(tmp).reg(), IP0);
Roland Levillain1ca955d2017-04-13 19:34:30 +01002827 // Put IP0 back in the pool so that VIXL has at least one
2828 // scratch register available to emit macro-instructions (note
2829 // that IP1 is already used for `tmp`). Indeed some
2830 // macro-instructions used in GenSystemArrayCopyAddresses
2831 // (invoked hereunder) may require a scratch register (for
2832 // instance to emit a load with a large constant offset).
2833 temps.Include(ip0);
Roland Levillain1663d162017-03-17 15:15:21 +00002834
2835 // /* int32_t */ monitor = src->monitor_
2836 __ Ldr(tmp, HeapOperand(src.W(), monitor_offset));
2837 // /* LockWord */ lock_word = LockWord(monitor)
2838 static_assert(sizeof(LockWord) == sizeof(int32_t),
2839 "art::LockWord and int32_t have different sizes.");
2840
2841 // Introduce a dependency on the lock_word including rb_state,
2842 // to prevent load-load reordering, and without using
2843 // a memory barrier (which would be more expensive).
2844 // `src` is unchanged by this operation, but its value now depends
2845 // on `tmp`.
2846 __ Add(src.X(), src.X(), Operand(tmp.X(), LSR, 32));
2847
2848 // Compute base source address, base destination address, and end
2849 // source address for System.arraycopy* intrinsics in `src_base`,
2850 // `dst_base` and `src_end` respectively.
2851 // Note that `src_curr_addr` is computed from from `src` (and
2852 // `src_pos`) here, and thus honors the artificial dependency
2853 // of `src` on `tmp`.
2854 GenSystemArrayCopyAddresses(masm,
2855 type,
2856 src,
2857 src_pos,
2858 dest,
2859 dest_pos,
2860 length,
2861 src_curr_addr,
2862 dst_curr_addr,
2863 src_stop_addr);
2864
2865 // Slow path used to copy array when `src` is gray.
2866 SlowPathCodeARM64* read_barrier_slow_path =
2867 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathARM64(invoke, LocationFrom(tmp));
2868 codegen_->AddSlowPath(read_barrier_slow_path);
2869
2870 // Given the numeric representation, it's enough to check the low bit of the rb_state.
2871 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2872 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2873 __ Tbnz(tmp, LockWord::kReadBarrierStateShift, read_barrier_slow_path->GetEntryLabel());
2874
2875 // Fast-path copy.
2876 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2877 // poison/unpoison.
2878 vixl::aarch64::Label loop;
2879 __ Bind(&loop);
Roland Levillain0b671c02016-08-19 12:02:34 +01002880 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2881 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
Roland Levillain1663d162017-03-17 15:15:21 +00002882 __ Cmp(src_curr_addr, src_stop_addr);
2883 __ B(&loop, ne);
2884
2885 __ Bind(read_barrier_slow_path->GetExitLabel());
2886 } else {
2887 // Non read barrier code.
2888 // Compute base source address, base destination address, and end
2889 // source address for System.arraycopy* intrinsics in `src_base`,
2890 // `dst_base` and `src_end` respectively.
2891 GenSystemArrayCopyAddresses(masm,
2892 type,
2893 src,
2894 src_pos,
2895 dest,
2896 dest_pos,
2897 length,
2898 src_curr_addr,
2899 dst_curr_addr,
2900 src_stop_addr);
2901 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2902 // poison/unpoison.
2903 vixl::aarch64::Label loop;
2904 __ Bind(&loop);
2905 {
2906 Register tmp = temps.AcquireW();
2907 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2908 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
2909 }
2910 __ Cmp(src_curr_addr, src_stop_addr);
2911 __ B(&loop, ne);
Roland Levillain0b671c02016-08-19 12:02:34 +01002912 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002913 __ Bind(&done);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002914 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002915 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002916
donghui.baic2ec9ad2016-03-10 14:02:55 +08002917 // We only need one card marking on the destination array.
2918 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2919
Roland Levillain0b671c02016-08-19 12:02:34 +01002920 __ Bind(intrinsic_slow_path->GetExitLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002921}
2922
Anton Kirilova3ffea22016-04-07 17:02:37 +01002923static void GenIsInfinite(LocationSummary* locations,
2924 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002925 MacroAssembler* masm) {
Anton Kirilova3ffea22016-04-07 17:02:37 +01002926 Operand infinity;
2927 Register out;
2928
2929 if (is64bit) {
2930 infinity = kPositiveInfinityDouble;
2931 out = XRegisterFrom(locations->Out());
2932 } else {
2933 infinity = kPositiveInfinityFloat;
2934 out = WRegisterFrom(locations->Out());
2935 }
2936
Scott Wakeling97c72b72016-06-24 16:19:36 +01002937 const Register zero = vixl::aarch64::Assembler::AppropriateZeroRegFor(out);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002938
2939 MoveFPToInt(locations, is64bit, masm);
2940 __ Eor(out, out, infinity);
2941 // We don't care about the sign bit, so shift left.
2942 __ Cmp(zero, Operand(out, LSL, 1));
2943 __ Cset(out, eq);
2944}
2945
2946void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2947 CreateFPToIntLocations(arena_, invoke);
2948}
2949
2950void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2951 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
2952}
2953
2954void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2955 CreateFPToIntLocations(arena_, invoke);
2956}
2957
2958void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2959 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
2960}
2961
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002962void IntrinsicLocationsBuilderARM64::VisitIntegerValueOf(HInvoke* invoke) {
2963 InvokeRuntimeCallingConvention calling_convention;
2964 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2965 invoke,
2966 codegen_,
2967 calling_convention.GetReturnLocation(Primitive::kPrimNot),
2968 Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2969}
2970
2971void IntrinsicCodeGeneratorARM64::VisitIntegerValueOf(HInvoke* invoke) {
2972 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2973 LocationSummary* locations = invoke->GetLocations();
2974 MacroAssembler* masm = GetVIXLAssembler();
2975
2976 Register out = RegisterFrom(locations->Out(), Primitive::kPrimNot);
2977 UseScratchRegisterScope temps(masm);
2978 Register temp = temps.AcquireW();
2979 InvokeRuntimeCallingConvention calling_convention;
2980 Register argument = calling_convention.GetRegisterAt(0);
2981 if (invoke->InputAt(0)->IsConstant()) {
2982 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2983 if (value >= info.low && value <= info.high) {
2984 // Just embed the j.l.Integer in the code.
2985 ScopedObjectAccess soa(Thread::Current());
2986 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2987 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2988 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
2989 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2990 } else {
2991 // Allocate and initialize a new j.l.Integer.
2992 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2993 // JIT object table.
2994 uint32_t address =
2995 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2996 __ Ldr(argument.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2997 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2998 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2999 __ Mov(temp.W(), value);
3000 __ Str(temp.W(), HeapOperand(out.W(), info.value_offset));
3001 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3002 // one.
3003 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3004 }
3005 } else {
3006 Register in = RegisterFrom(locations->InAt(0), Primitive::kPrimInt);
3007 // Check bounds of our cache.
3008 __ Add(out.W(), in.W(), -info.low);
3009 __ Cmp(out.W(), info.high - info.low + 1);
3010 vixl::aarch64::Label allocate, done;
3011 __ B(&allocate, hs);
3012 // If the value is within the bounds, load the j.l.Integer directly from the array.
3013 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3014 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3015 __ Ldr(temp.W(), codegen_->DeduplicateBootImageAddressLiteral(data_offset + address));
3016 MemOperand source = HeapOperand(
3017 temp, out.X(), LSL, Primitive::ComponentSizeShift(Primitive::kPrimNot));
3018 codegen_->Load(Primitive::kPrimNot, out, source);
3019 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(out);
3020 __ B(&done);
3021 __ Bind(&allocate);
3022 // Otherwise allocate and initialize a new j.l.Integer.
3023 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3024 __ Ldr(argument.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
3025 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3026 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3027 __ Str(in.W(), HeapOperand(out.W(), info.value_offset));
3028 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
3029 // one.
3030 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3031 __ Bind(&done);
3032 }
3033}
3034
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003035void IntrinsicLocationsBuilderARM64::VisitThreadInterrupted(HInvoke* invoke) {
3036 LocationSummary* locations = new (arena_) LocationSummary(invoke,
3037 LocationSummary::kNoCall,
3038 kIntrinsified);
3039 locations->SetOut(Location::RequiresRegister());
3040}
3041
3042void IntrinsicCodeGeneratorARM64::VisitThreadInterrupted(HInvoke* invoke) {
3043 MacroAssembler* masm = GetVIXLAssembler();
3044 Register out = RegisterFrom(invoke->GetLocations()->Out(), Primitive::kPrimInt);
3045 UseScratchRegisterScope temps(masm);
3046 Register temp = temps.AcquireX();
3047
3048 __ Add(temp, tr, Thread::InterruptedOffset<kArm64PointerSize>().Int32Value());
3049 __ Ldar(out.W(), MemOperand(temp));
3050
3051 vixl::aarch64::Label done;
3052 __ Cbz(out.W(), &done);
3053 __ Stlr(wzr, MemOperand(temp));
3054 __ Bind(&done);
3055}
3056
Vladimir Markod254f5c2017-06-02 15:18:36 +00003057UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Andreas Gampe878d58c2015-01-15 23:24:00 -08003058
Aart Bikff7d89c2016-11-07 08:49:28 -08003059UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOf);
3060UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003061UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferAppend);
3062UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferLength);
3063UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferToString);
3064UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppend);
3065UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderLength);
3066UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003067
Aart Bik0e54c012016-03-04 12:08:31 -08003068// 1.8.
3069UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
3070UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
3071UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
3072UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
3073UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003074
Aart Bik2f9fcc92016-03-01 15:16:54 -08003075UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01003076
3077#undef __
3078
Andreas Gampe878d58c2015-01-15 23:24:00 -08003079} // namespace arm64
3080} // namespace art