blob: c3d643a7d18b76e6bad9373d365e1148a070ebc0 [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"
Andreas Gampe09659c22017-09-18 18:23:32 -070024#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080025#include "intrinsics.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080026#include "lock_word.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070028#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#include "mirror/reference.h"
Vladimir Markoe39f14f2017-02-10 15:44:25 +000030#include "mirror/string-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080031#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070032#include "thread-current-inl.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080033#include "utils/arm64/assembler_arm64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080034
Scott Wakeling97c72b72016-06-24 16:19:36 +010035using namespace vixl::aarch64; // NOLINT(build/namespaces)
Andreas Gampe878d58c2015-01-15 23:24:00 -080036
Artem Serovaf4e42a2016-08-08 15:11:24 +010037// TODO(VIXL): Make VIXL compile with -Wshadow.
Scott Wakeling97c72b72016-06-24 16:19:36 +010038#pragma GCC diagnostic push
39#pragma GCC diagnostic ignored "-Wshadow"
Artem Serovaf4e42a2016-08-08 15:11:24 +010040#include "aarch64/disasm-aarch64.h"
41#include "aarch64/macro-assembler-aarch64.h"
Scott Wakeling97c72b72016-06-24 16:19:36 +010042#pragma GCC diagnostic pop
Andreas Gampe878d58c2015-01-15 23:24:00 -080043
44namespace art {
45
46namespace arm64 {
47
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000051using helpers::LocationFrom;
Scott Wakeling9ee23f42015-07-23 10:44:35 +010052using helpers::OperandFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080053using helpers::RegisterFrom;
54using helpers::SRegisterFrom;
55using helpers::WRegisterFrom;
56using helpers::XRegisterFrom;
xueliang.zhong49924c92016-03-03 10:52:51 +000057using helpers::InputRegisterAt;
Scott Wakeling1f36f412016-04-21 11:13:45 +010058using helpers::OutputRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059
Andreas Gampe878d58c2015-01-15 23:24:00 -080060namespace {
61
62ALWAYS_INLINE inline MemOperand AbsoluteHeapOperandFrom(Location location, size_t offset = 0) {
63 return MemOperand(XRegisterFrom(location), offset);
64}
65
66} // namespace
67
Scott Wakeling97c72b72016-06-24 16:19:36 +010068MacroAssembler* IntrinsicCodeGeneratorARM64::GetVIXLAssembler() {
Alexandre Rames087930f2016-08-02 13:45:28 +010069 return codegen_->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -080070}
71
72ArenaAllocator* IntrinsicCodeGeneratorARM64::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010073 return codegen_->GetGraph()->GetAllocator();
Andreas Gampe878d58c2015-01-15 23:24:00 -080074}
75
Alexandre Rames087930f2016-08-02 13:45:28 +010076#define __ codegen->GetVIXLAssembler()->
Andreas Gampe878d58c2015-01-15 23:24:00 -080077
78static void MoveFromReturnRegister(Location trg,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079 DataType::Type type,
Andreas Gampe878d58c2015-01-15 23:24:00 -080080 CodeGeneratorARM64* codegen) {
81 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010082 DCHECK(type == DataType::Type::kVoid);
Andreas Gampe878d58c2015-01-15 23:24:00 -080083 return;
84 }
85
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010086 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe878d58c2015-01-15 23:24:00 -080087
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010088 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080089 Register trg_reg = RegisterFrom(trg, type);
90 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
91 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
92 } else {
93 FPRegister trg_reg = FPRegisterFrom(trg, type);
94 FPRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
95 __ Fmov(trg_reg, res_reg);
96 }
97}
98
Roland Levillainec525fc2015-04-28 15:50:20 +010099static void MoveArguments(HInvoke* invoke, CodeGeneratorARM64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100100 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +0100101 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800102}
103
104// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
105// call. This will copy the arguments into the positions for a regular call.
106//
107// Note: The actual parameters are required to be in the locations given by the invoke's location
108// summary. If an intrinsic modifies those locations before a slowpath call, they must be
109// restored!
110class IntrinsicSlowPathARM64 : public SlowPathCodeARM64 {
111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 explicit IntrinsicSlowPathARM64(HInvoke* invoke)
113 : SlowPathCodeARM64(invoke), invoke_(invoke) { }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800114
115 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
116 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
117 __ Bind(GetEntryLabel());
118
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000119 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800120
Roland Levillainec525fc2015-04-28 15:50:20 +0100121 MoveArguments(invoke_, codegen);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800122
Artem Serov914d7a82017-02-07 14:33:49 +0000123 {
124 // Ensure that between the BLR (emitted by Generate*Call) and RecordPcInfo there
125 // are no pools emitted.
126 vixl::EmissionCheckScope guard(codegen->GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
127 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100128 codegen->GenerateStaticOrDirectCall(
129 invoke_->AsInvokeStaticOrDirect(), LocationFrom(kArtMethodRegister), this);
Artem Serov914d7a82017-02-07 14:33:49 +0000130 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100131 codegen->GenerateVirtualCall(
132 invoke_->AsInvokeVirtual(), LocationFrom(kArtMethodRegister), this);
Artem Serov914d7a82017-02-07 14:33:49 +0000133 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800134 }
135
136 // Copy the result back to the expected output.
137 Location out = invoke_->GetLocations()->Out();
138 if (out.IsValid()) {
139 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
140 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
141 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
142 }
143
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800145 __ B(GetExitLabel());
146 }
147
Alexandre Rames9931f312015-06-19 14:47:01 +0100148 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathARM64"; }
149
Andreas Gampe878d58c2015-01-15 23:24:00 -0800150 private:
151 // The instruction where this slow path is happening.
152 HInvoke* const invoke_;
153
154 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARM64);
155};
156
Roland Levillain0b671c02016-08-19 12:02:34 +0100157// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
158class ReadBarrierSystemArrayCopySlowPathARM64 : public SlowPathCodeARM64 {
159 public:
160 ReadBarrierSystemArrayCopySlowPathARM64(HInstruction* instruction, Location tmp)
161 : SlowPathCodeARM64(instruction), tmp_(tmp) {
162 DCHECK(kEmitCompilerReadBarrier);
163 DCHECK(kUseBakerReadBarrier);
164 }
165
166 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
167 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
168 LocationSummary* locations = instruction_->GetLocations();
169 DCHECK(locations->CanCall());
170 DCHECK(instruction_->IsInvokeStaticOrDirect())
171 << "Unexpected instruction in read barrier arraycopy slow path: "
172 << instruction_->DebugName();
173 DCHECK(instruction_->GetLocations()->Intrinsified());
174 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100176 const int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +0100177
178 Register src_curr_addr = XRegisterFrom(locations->GetTemp(0));
179 Register dst_curr_addr = XRegisterFrom(locations->GetTemp(1));
180 Register src_stop_addr = XRegisterFrom(locations->GetTemp(2));
181 Register tmp_reg = WRegisterFrom(tmp_);
182
183 __ Bind(GetEntryLabel());
184 vixl::aarch64::Label slow_copy_loop;
185 __ Bind(&slow_copy_loop);
186 __ Ldr(tmp_reg, MemOperand(src_curr_addr, element_size, PostIndex));
187 codegen->GetAssembler()->MaybeUnpoisonHeapReference(tmp_reg);
188 // TODO: Inline the mark bit check before calling the runtime?
189 // tmp_reg = ReadBarrier::Mark(tmp_reg);
190 // No need to save live registers; it's taken care of by the
191 // entrypoint. Also, there is no need to update the stack mask,
192 // as this runtime call will not trigger a garbage collection.
193 // (See ReadBarrierMarkSlowPathARM64::EmitNativeCode for more
194 // explanations.)
195 DCHECK_NE(tmp_.reg(), LR);
196 DCHECK_NE(tmp_.reg(), WSP);
197 DCHECK_NE(tmp_.reg(), WZR);
198 // IP0 is used internally by the ReadBarrierMarkRegX entry point
199 // as a temporary (and not preserved). It thus cannot be used by
200 // any live register in this slow path.
201 DCHECK_NE(LocationFrom(src_curr_addr).reg(), IP0);
202 DCHECK_NE(LocationFrom(dst_curr_addr).reg(), IP0);
203 DCHECK_NE(LocationFrom(src_stop_addr).reg(), IP0);
204 DCHECK_NE(tmp_.reg(), IP0);
205 DCHECK(0 <= tmp_.reg() && tmp_.reg() < kNumberOfWRegisters) << tmp_.reg();
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000206 // TODO: Load the entrypoint once before the loop, instead of
207 // loading it at every iteration.
Roland Levillain0b671c02016-08-19 12:02:34 +0100208 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100209 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(tmp_.reg());
Roland Levillain0b671c02016-08-19 12:02:34 +0100210 // This runtime call does not require a stack map.
211 codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
212 codegen->GetAssembler()->MaybePoisonHeapReference(tmp_reg);
213 __ Str(tmp_reg, MemOperand(dst_curr_addr, element_size, PostIndex));
214 __ Cmp(src_curr_addr, src_stop_addr);
215 __ B(&slow_copy_loop, ne);
216 __ B(GetExitLabel());
217 }
218
219 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathARM64"; }
220
221 private:
222 Location tmp_;
223
224 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARM64);
225};
Andreas Gampe878d58c2015-01-15 23:24:00 -0800226#undef __
227
228bool IntrinsicLocationsBuilderARM64::TryDispatch(HInvoke* invoke) {
229 Dispatch(invoke);
230 LocationSummary* res = invoke->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000231 if (res == nullptr) {
232 return false;
233 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000234 return res->Intrinsified();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800235}
236
237#define __ masm->
238
Vladimir Markoca6fff82017-10-03 14:49:14 +0100239static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
240 LocationSummary* locations =
241 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800242 locations->SetInAt(0, Location::RequiresFpuRegister());
243 locations->SetOut(Location::RequiresRegister());
244}
245
Vladimir Markoca6fff82017-10-03 14:49:14 +0100246static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
247 LocationSummary* locations =
248 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800249 locations->SetInAt(0, Location::RequiresRegister());
250 locations->SetOut(Location::RequiresFpuRegister());
251}
252
Scott Wakeling97c72b72016-06-24 16:19:36 +0100253static void MoveFPToInt(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800254 Location input = locations->InAt(0);
255 Location output = locations->Out();
256 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
257 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
258}
259
Scott Wakeling97c72b72016-06-24 16:19:36 +0100260static void MoveIntToFP(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800261 Location input = locations->InAt(0);
262 Location output = locations->Out();
263 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
264 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
265}
266
267void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100268 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800269}
270void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100271 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800272}
273
274void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000275 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800276}
277void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000278 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800279}
280
281void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100282 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800283}
284void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100285 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800286}
287
288void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000289 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800290}
291void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000292 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800293}
294
Vladimir Markoca6fff82017-10-03 14:49:14 +0100295static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
296 LocationSummary* locations =
297 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800298 locations->SetInAt(0, Location::RequiresRegister());
299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
300}
301
302static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100303 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100304 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800305 Location in = locations->InAt(0);
306 Location out = locations->Out();
307
308 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100309 case DataType::Type::kInt16:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800310 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
311 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
312 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100313 case DataType::Type::kInt32:
314 case DataType::Type::kInt64:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800315 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
316 break;
317 default:
318 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
319 UNREACHABLE();
320 }
321}
322
323void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100324 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800325}
326
327void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100328 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800329}
330
331void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100332 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800333}
334
335void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100336 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800337}
338
339void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100340 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800341}
342
343void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100344 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800345}
346
Scott Wakeling611d3392015-07-10 11:42:06 +0100347static void GenNumberOfLeadingZeros(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100348 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100349 MacroAssembler* masm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100350 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Scott Wakeling611d3392015-07-10 11:42:06 +0100351
352 Location in = locations->InAt(0);
353 Location out = locations->Out();
354
355 __ Clz(RegisterFrom(out, type), RegisterFrom(in, type));
356}
357
358void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100359 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling611d3392015-07-10 11:42:06 +0100360}
361
362void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100363 GenNumberOfLeadingZeros(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Scott Wakeling611d3392015-07-10 11:42:06 +0100364}
365
366void IntrinsicLocationsBuilderARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100367 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling611d3392015-07-10 11:42:06 +0100368}
369
370void IntrinsicCodeGeneratorARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100371 GenNumberOfLeadingZeros(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Scott Wakeling611d3392015-07-10 11:42:06 +0100372}
373
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100374static void GenNumberOfTrailingZeros(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100375 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100376 MacroAssembler* masm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100377 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100378
379 Location in = locations->InAt(0);
380 Location out = locations->Out();
381
382 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
383 __ Clz(RegisterFrom(out, type), RegisterFrom(out, type));
384}
385
386void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100387 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100388}
389
390void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100391 GenNumberOfTrailingZeros(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100392}
393
394void IntrinsicLocationsBuilderARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100395 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100396}
397
398void IntrinsicCodeGeneratorARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100399 GenNumberOfTrailingZeros(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100400}
401
Andreas Gampe878d58c2015-01-15 23:24:00 -0800402static void GenReverse(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100403 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100404 MacroAssembler* masm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100405 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800406
407 Location in = locations->InAt(0);
408 Location out = locations->Out();
409
410 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
411}
412
413void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100414 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800415}
416
417void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100418 GenReverse(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800419}
420
421void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100422 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800423}
424
425void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100426 GenReverse(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800427}
428
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100429static void GenBitCount(HInvoke* instr, DataType::Type type, MacroAssembler* masm) {
430 DCHECK(DataType::IsIntOrLongType(type)) << type;
431 DCHECK_EQ(instr->GetType(), DataType::Type::kInt32);
432 DCHECK_EQ(DataType::Kind(instr->InputAt(0)->GetType()), type);
xueliang.zhong49924c92016-03-03 10:52:51 +0000433
xueliang.zhong49924c92016-03-03 10:52:51 +0000434 UseScratchRegisterScope temps(masm);
435
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000436 Register src = InputRegisterAt(instr, 0);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100437 Register dst = RegisterFrom(instr->GetLocations()->Out(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 FPRegister fpr = (type == DataType::Type::kInt64) ? temps.AcquireD() : temps.AcquireS();
xueliang.zhong49924c92016-03-03 10:52:51 +0000439
440 __ Fmov(fpr, src);
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000441 __ Cnt(fpr.V8B(), fpr.V8B());
442 __ Addv(fpr.B(), fpr.V8B());
xueliang.zhong49924c92016-03-03 10:52:51 +0000443 __ Fmov(dst, fpr);
444}
445
446void IntrinsicLocationsBuilderARM64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100447 CreateIntToIntLocations(allocator_, invoke);
xueliang.zhong49924c92016-03-03 10:52:51 +0000448}
449
450void IntrinsicCodeGeneratorARM64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100451 GenBitCount(invoke, DataType::Type::kInt64, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000452}
453
454void IntrinsicLocationsBuilderARM64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100455 CreateIntToIntLocations(allocator_, invoke);
xueliang.zhong49924c92016-03-03 10:52:51 +0000456}
457
458void IntrinsicCodeGeneratorARM64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100459 GenBitCount(invoke, DataType::Type::kInt32, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000460}
461
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100462static void GenHighestOneBit(HInvoke* invoke, DataType::Type type, MacroAssembler* masm) {
463 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100464
465 UseScratchRegisterScope temps(masm);
466
467 Register src = InputRegisterAt(invoke, 0);
468 Register dst = RegisterFrom(invoke->GetLocations()->Out(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100469 Register temp = (type == DataType::Type::kInt64) ? temps.AcquireX() : temps.AcquireW();
470 size_t high_bit = (type == DataType::Type::kInt64) ? 63u : 31u;
471 size_t clz_high_bit = (type == DataType::Type::kInt64) ? 6u : 5u;
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100472
473 __ Clz(temp, src);
474 __ Mov(dst, UINT64_C(1) << high_bit); // MOV (bitmask immediate)
475 __ Bic(dst, dst, Operand(temp, LSL, high_bit - clz_high_bit)); // Clear dst if src was 0.
476 __ Lsr(dst, dst, temp);
477}
478
479void IntrinsicLocationsBuilderARM64::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100480 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100481}
482
483void IntrinsicCodeGeneratorARM64::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100484 GenHighestOneBit(invoke, DataType::Type::kInt32, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100485}
486
487void IntrinsicLocationsBuilderARM64::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100488 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100489}
490
491void IntrinsicCodeGeneratorARM64::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 GenHighestOneBit(invoke, DataType::Type::kInt64, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100493}
494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100495static void GenLowestOneBit(HInvoke* invoke, DataType::Type type, MacroAssembler* masm) {
496 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100497
498 UseScratchRegisterScope temps(masm);
499
500 Register src = InputRegisterAt(invoke, 0);
501 Register dst = RegisterFrom(invoke->GetLocations()->Out(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100502 Register temp = (type == DataType::Type::kInt64) ? temps.AcquireX() : temps.AcquireW();
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100503
504 __ Neg(temp, src);
505 __ And(dst, temp, src);
506}
507
508void IntrinsicLocationsBuilderARM64::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100509 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100510}
511
512void IntrinsicCodeGeneratorARM64::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100513 GenLowestOneBit(invoke, DataType::Type::kInt32, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100514}
515
516void IntrinsicLocationsBuilderARM64::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100517 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100518}
519
520void IntrinsicCodeGeneratorARM64::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100521 GenLowestOneBit(invoke, DataType::Type::kInt64, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100522}
523
Aart Bik3dad3412018-02-28 12:01:46 -0800524static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
525 LocationSummary* locations =
526 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
527 locations->SetInAt(0, Location::RequiresFpuRegister());
528 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
529}
530
Andreas Gampe878d58c2015-01-15 23:24:00 -0800531void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100532 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800533}
534
535void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
536 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100537 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800538 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
539}
540
541void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100542 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800543}
544
545void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
546 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100547 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800548 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
549}
550
551void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100552 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800553}
554
555void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
556 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100557 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800558 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
559}
560
561void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100562 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800563}
564
565void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
566 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100567 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800568 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
569}
570
Vladimir Markoca6fff82017-10-03 14:49:14 +0100571static void CreateFPToIntPlusFPTempLocations(ArenaAllocator* allocator, HInvoke* invoke) {
572 LocationSummary* locations =
573 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800574 locations->SetInAt(0, Location::RequiresFpuRegister());
575 locations->SetOut(Location::RequiresRegister());
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100576 locations->AddTemp(Location::RequiresFpuRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800577}
578
Scott Wakeling97c72b72016-06-24 16:19:36 +0100579static void GenMathRound(HInvoke* invoke, bool is_double, vixl::aarch64::MacroAssembler* masm) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100580 // Java 8 API definition for Math.round():
581 // Return the closest long or int to the argument, with ties rounding to positive infinity.
582 //
583 // There is no single instruction in ARMv8 that can support the above definition.
584 // We choose to use FCVTAS here, because it has closest semantic.
585 // FCVTAS performs rounding to nearest integer, ties away from zero.
586 // For most inputs (positive values, zero or NaN), this instruction is enough.
587 // We only need a few handling code after FCVTAS if the input is negative half value.
588 //
589 // The reason why we didn't choose FCVTPS instruction here is that
590 // although it performs rounding toward positive infinity, it doesn't perform rounding to nearest.
591 // For example, FCVTPS(-1.9) = -1 and FCVTPS(1.1) = 2.
592 // If we were using this instruction, for most inputs, more handling code would be needed.
593 LocationSummary* l = invoke->GetLocations();
594 FPRegister in_reg = is_double ? DRegisterFrom(l->InAt(0)) : SRegisterFrom(l->InAt(0));
595 FPRegister tmp_fp = is_double ? DRegisterFrom(l->GetTemp(0)) : SRegisterFrom(l->GetTemp(0));
596 Register out_reg = is_double ? XRegisterFrom(l->Out()) : WRegisterFrom(l->Out());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100597 vixl::aarch64::Label done;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800598
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100599 // Round to nearest integer, ties away from zero.
600 __ Fcvtas(out_reg, in_reg);
601
602 // For positive values, zero or NaN inputs, rounding is done.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100603 __ Tbz(out_reg, out_reg.GetSizeInBits() - 1, &done);
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100604
605 // Handle input < 0 cases.
606 // If input is negative but not a tie, previous result (round to nearest) is valid.
607 // If input is a negative tie, out_reg += 1.
608 __ Frinta(tmp_fp, in_reg);
609 __ Fsub(tmp_fp, in_reg, tmp_fp);
610 __ Fcmp(tmp_fp, 0.5);
611 __ Cinc(out_reg, out_reg, eq);
612
613 __ Bind(&done);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800614}
615
616void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100617 CreateFPToIntPlusFPTempLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800618}
619
620void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100621 GenMathRound(invoke, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800622}
623
624void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100625 CreateFPToIntPlusFPTempLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800626}
627
628void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100629 GenMathRound(invoke, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800630}
631
632void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800634}
635
636void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100637 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800638 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
639 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
640}
641
642void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100643 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800644}
645
646void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100647 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800648 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
649 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
650}
651
652void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100653 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800654}
655
656void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100657 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800658 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
659 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
660}
661
662void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100663 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800664}
665
666void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100667 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800668 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
669 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
670}
671
Vladimir Markoca6fff82017-10-03 14:49:14 +0100672static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
673 LocationSummary* locations =
674 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800675 locations->SetInAt(0, Location::RequiresRegister());
676 locations->SetInAt(1, Location::RequiresRegister());
677}
678
679void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100680 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800681}
682
683void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100684 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800685 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
686 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
687}
688
689void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100690 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800691}
692
693void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100694 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800695 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
696 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
697}
698
699void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100700 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800701}
702
703void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100704 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800705 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
706 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
707}
708
709void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100710 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800711}
712
713void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100714 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800715 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
716 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
717}
718
719void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100720 LocationSummary* locations =
721 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800722 locations->SetOut(Location::RequiresRegister());
723}
724
725void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100726 codegen_->Load(DataType::Type::kReference, WRegisterFrom(invoke->GetLocations()->Out()),
Andreas Gampe542451c2016-07-26 09:02:02 -0700727 MemOperand(tr, Thread::PeerOffset<kArm64PointerSize>().Int32Value()));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800728}
729
730static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100731 DataType::Type type,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800732 bool is_volatile,
733 CodeGeneratorARM64* codegen) {
734 LocationSummary* locations = invoke->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100735 DCHECK((type == DataType::Type::kInt32) ||
736 (type == DataType::Type::kInt64) ||
737 (type == DataType::Type::kReference));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000738 Location base_loc = locations->InAt(1);
739 Register base = WRegisterFrom(base_loc); // Object pointer.
740 Location offset_loc = locations->InAt(2);
741 Register offset = XRegisterFrom(offset_loc); // Long offset.
742 Location trg_loc = locations->Out();
743 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100745 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +0000746 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
Roland Levillain54f869e2017-03-06 13:54:11 +0000747 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillainbfea3352016-06-23 13:48:47 +0100748 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
749 trg_loc,
750 base,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100751 /* offset */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100752 /* index */ offset_loc,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100753 /* scale_factor */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100754 temp,
755 /* needs_null_check */ false,
756 is_volatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800757 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000758 // Other cases.
759 MemOperand mem_op(base.X(), offset);
760 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000761 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000762 } else {
763 codegen->Load(type, trg, mem_op);
764 }
Roland Levillain4d027112015-07-01 15:41:14 +0100765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100766 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +0000767 DCHECK(trg.IsW());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100768 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0u, offset_loc);
Roland Levillain44015862016-01-22 11:47:17 +0000769 }
Roland Levillain4d027112015-07-01 15:41:14 +0100770 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800771}
772
Vladimir Markoca6fff82017-10-03 14:49:14 +0100773static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000774 bool can_call = kEmitCompilerReadBarrier &&
775 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
776 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100777 LocationSummary* locations =
778 new (allocator) LocationSummary(invoke,
779 can_call
780 ? LocationSummary::kCallOnSlowPath
781 : LocationSummary::kNoCall,
782 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +0100783 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100784 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +0000785 // We need a temporary register for the read barrier marking slow
786 // path in CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier.
787 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +0100788 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800789 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
790 locations->SetInAt(1, Location::RequiresRegister());
791 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100792 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100793 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800794}
795
796void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100797 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800798}
799void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100800 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800801}
802void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100803 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800804}
805void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100806 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800807}
808void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100809 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800810}
811void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100812 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800813}
814
815void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100816 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800817}
818void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100819 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800820}
821void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100822 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800823}
824void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100825 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800826}
827void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800829}
830void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100831 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800832}
833
Vladimir Markoca6fff82017-10-03 14:49:14 +0100834static void CreateIntIntIntIntToVoid(ArenaAllocator* allocator, HInvoke* invoke) {
835 LocationSummary* locations =
836 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800837 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
838 locations->SetInAt(1, Location::RequiresRegister());
839 locations->SetInAt(2, Location::RequiresRegister());
840 locations->SetInAt(3, Location::RequiresRegister());
841}
842
843void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100844 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800845}
846void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100847 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800848}
849void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100850 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800851}
852void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100853 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800854}
855void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100856 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800857}
858void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100859 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800860}
861void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100862 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800863}
864void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100865 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800866}
867void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100868 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800869}
870
Artem Serov914d7a82017-02-07 14:33:49 +0000871static void GenUnsafePut(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100872 DataType::Type type,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800873 bool is_volatile,
874 bool is_ordered,
875 CodeGeneratorARM64* codegen) {
Artem Serov914d7a82017-02-07 14:33:49 +0000876 LocationSummary* locations = invoke->GetLocations();
Alexandre Rames087930f2016-08-02 13:45:28 +0100877 MacroAssembler* masm = codegen->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800878
879 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
880 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
881 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100882 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800883 MemOperand mem_op(base.X(), offset);
884
Roland Levillain4d027112015-07-01 15:41:14 +0100885 {
886 // We use a block to end the scratch scope before the write barrier, thus
887 // freeing the temporary registers so they can be used in `MarkGCCard`.
888 UseScratchRegisterScope temps(masm);
889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100890 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +0100891 DCHECK(value.IsW());
892 Register temp = temps.AcquireW();
893 __ Mov(temp.W(), value.W());
894 codegen->GetAssembler()->PoisonHeapReference(temp.W());
895 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800896 }
Roland Levillain4d027112015-07-01 15:41:14 +0100897
898 if (is_volatile || is_ordered) {
Artem Serov914d7a82017-02-07 14:33:49 +0000899 codegen->StoreRelease(invoke, type, source, mem_op, /* needs_null_check */ false);
Roland Levillain4d027112015-07-01 15:41:14 +0100900 } else {
901 codegen->Store(type, source, mem_op);
902 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800903 }
904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +0100906 bool value_can_be_null = true; // TODO: Worth finding out this information?
907 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800908 }
909}
910
911void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000912 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100913 DataType::Type::kInt32,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000914 /* is_volatile */ false,
915 /* is_ordered */ false,
916 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800917}
918void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000919 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100920 DataType::Type::kInt32,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000921 /* is_volatile */ false,
922 /* is_ordered */ true,
923 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800924}
925void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000926 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100927 DataType::Type::kInt32,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000928 /* is_volatile */ true,
929 /* is_ordered */ false,
930 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800931}
932void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000933 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100934 DataType::Type::kReference,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000935 /* is_volatile */ false,
936 /* is_ordered */ false,
937 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800938}
939void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000940 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100941 DataType::Type::kReference,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000942 /* is_volatile */ false,
943 /* is_ordered */ true,
944 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800945}
946void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000947 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100948 DataType::Type::kReference,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000949 /* is_volatile */ true,
950 /* is_ordered */ false,
951 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800952}
953void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000954 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100955 DataType::Type::kInt64,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000956 /* is_volatile */ false,
957 /* is_ordered */ false,
958 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800959}
960void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000961 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100962 DataType::Type::kInt64,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000963 /* is_volatile */ false,
964 /* is_ordered */ true,
965 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800966}
967void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +0000968 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100969 DataType::Type::kInt64,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000970 /* is_volatile */ true,
971 /* is_ordered */ false,
972 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800973}
974
Vladimir Markoca6fff82017-10-03 14:49:14 +0100975static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000976 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100977 DataType::Type type) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100978 bool can_call = kEmitCompilerReadBarrier &&
979 kUseBakerReadBarrier &&
980 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100981 LocationSummary* locations =
982 new (allocator) LocationSummary(invoke,
983 can_call
984 ? LocationSummary::kCallOnSlowPath
985 : LocationSummary::kNoCall,
986 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800987 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
988 locations->SetInAt(1, Location::RequiresRegister());
989 locations->SetInAt(2, Location::RequiresRegister());
990 locations->SetInAt(3, Location::RequiresRegister());
991 locations->SetInAt(4, Location::RequiresRegister());
992
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000993 // If heap poisoning is enabled, we don't want the unpoisoning
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100994 // operations to potentially clobber the output. Likewise when
995 // emitting a (Baker) read barrier, which may call.
996 Location::OutputOverlap overlaps =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100997 ((kPoisonHeapReferences && type == DataType::Type::kReference) || can_call)
Roland Levillain2e50ecb2016-01-27 14:08:33 +0000998 ? Location::kOutputOverlap
999 : Location::kNoOutputOverlap;
1000 locations->SetOut(Location::RequiresRegister(), overlaps);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001001 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001002 // Temporary register for (Baker) read barrier.
1003 locations->AddTemp(Location::RequiresRegister());
1004 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001005}
1006
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001007static void GenCas(HInvoke* invoke, DataType::Type type, CodeGeneratorARM64* codegen) {
Alexandre Rames087930f2016-08-02 13:45:28 +01001008 MacroAssembler* masm = codegen->GetVIXLAssembler();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001009 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001010
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001011 Location out_loc = locations->Out();
1012 Register out = WRegisterFrom(out_loc); // Boolean result.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001013
1014 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001015 Location offset_loc = locations->InAt(2);
1016 Register offset = XRegisterFrom(offset_loc); // Long offset.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001017 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1018 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1019
1020 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001021 if (type == DataType::Type::kReference) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001022 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001023 bool value_can_be_null = true; // TODO: Worth finding out this information?
1024 codegen->MarkGCCard(base, value, value_can_be_null);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001025
1026 // The only read barrier implementation supporting the
1027 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1028 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1029
1030 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1031 Register temp = WRegisterFrom(locations->GetTemp(0));
1032 // Need to make sure the reference stored in the field is a to-space
1033 // one before attempting the CAS or the CAS could fail incorrectly.
Roland Levillainff487002017-03-07 16:50:01 +00001034 codegen->UpdateReferenceFieldWithBakerReadBarrier(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001035 invoke,
1036 out_loc, // Unused, used only as a "temporary" within the read barrier.
1037 base,
Roland Levillainff487002017-03-07 16:50:01 +00001038 /* field_offset */ offset_loc,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001039 temp,
1040 /* needs_null_check */ false,
Roland Levillainff487002017-03-07 16:50:01 +00001041 /* use_load_acquire */ false);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001042 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001043 }
1044
1045 UseScratchRegisterScope temps(masm);
1046 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1047 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1048
1049 Register tmp_32 = tmp_value.W();
1050
1051 __ Add(tmp_ptr, base.X(), Operand(offset));
1052
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001053 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001054 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001055 if (value.Is(expected)) {
1056 // Do not poison `value`, as it is the same register as
1057 // `expected`, which has just been poisoned.
1058 } else {
1059 codegen->GetAssembler()->PoisonHeapReference(value);
1060 }
Roland Levillain4d027112015-07-01 15:41:14 +01001061 }
1062
Andreas Gampe878d58c2015-01-15 23:24:00 -08001063 // do {
1064 // tmp_value = [tmp_ptr] - expected;
1065 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1066 // result = tmp_value != 0;
1067
Scott Wakeling97c72b72016-06-24 16:19:36 +01001068 vixl::aarch64::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001069 __ Bind(&loop_head);
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001070 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1071 __ Cmp(tmp_value, expected);
1072 __ B(&exit_loop, ne);
1073 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1074 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001075 __ Bind(&exit_loop);
1076 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001078 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001079 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001080 if (value.Is(expected)) {
1081 // Do not unpoison `value`, as it is the same register as
1082 // `expected`, which has just been unpoisoned.
1083 } else {
1084 codegen->GetAssembler()->UnpoisonHeapReference(value);
1085 }
Roland Levillain4d027112015-07-01 15:41:14 +01001086 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001087}
1088
1089void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001090 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kInt32);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001091}
1092void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001093 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kInt64);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001094}
1095void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001096 // The only read barrier implementation supporting the
1097 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1098 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001099 return;
1100 }
1101
Vladimir Markoca6fff82017-10-03 14:49:14 +01001102 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kReference);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001103}
1104
1105void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001106 GenCas(invoke, DataType::Type::kInt32, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001107}
1108void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001109 GenCas(invoke, DataType::Type::kInt64, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001110}
1111void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001112 // The only read barrier implementation supporting the
1113 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1114 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001116 GenCas(invoke, DataType::Type::kReference, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001117}
1118
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001119void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001120 LocationSummary* locations =
1121 new (allocator_) LocationSummary(invoke,
1122 invoke->InputAt(1)->CanBeNull()
1123 ? LocationSummary::kCallOnSlowPath
1124 : LocationSummary::kNoCall,
1125 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001126 locations->SetInAt(0, Location::RequiresRegister());
1127 locations->SetInAt(1, Location::RequiresRegister());
1128 locations->AddTemp(Location::RequiresRegister());
1129 locations->AddTemp(Location::RequiresRegister());
1130 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001131 // Need temporary registers for String compression's feature.
1132 if (mirror::kUseStringCompression) {
1133 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001134 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001135 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001136}
1137
1138void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001139 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001140 LocationSummary* locations = invoke->GetLocations();
1141
Alexandre Rames2ea91532016-08-11 17:04:14 +01001142 Register str = InputRegisterAt(invoke, 0);
1143 Register arg = InputRegisterAt(invoke, 1);
1144 DCHECK(str.IsW());
1145 DCHECK(arg.IsW());
Scott Wakeling1f36f412016-04-21 11:13:45 +01001146 Register out = OutputRegister(invoke);
1147
1148 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1149 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1150 Register temp2 = WRegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001151 Register temp3;
jessicahandojo05765752016-09-09 19:01:32 -07001152 if (mirror::kUseStringCompression) {
1153 temp3 = WRegisterFrom(locations->GetTemp(3));
jessicahandojo05765752016-09-09 19:01:32 -07001154 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001155
Scott Wakeling97c72b72016-06-24 16:19:36 +01001156 vixl::aarch64::Label loop;
1157 vixl::aarch64::Label find_char_diff;
1158 vixl::aarch64::Label end;
jessicahandojo05765752016-09-09 19:01:32 -07001159 vixl::aarch64::Label different_compression;
Scott Wakeling1f36f412016-04-21 11:13:45 +01001160
1161 // Get offsets of count and value fields within a string object.
1162 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1163 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1164
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001165 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001166 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001167
Scott Wakeling1f36f412016-04-21 11:13:45 +01001168 // Take slow path and throw if input can be and is null.
1169 SlowPathCodeARM64* slow_path = nullptr;
1170 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1171 if (can_slow_path) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001172 slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001173 codegen_->AddSlowPath(slow_path);
1174 __ Cbz(arg, slow_path->GetEntryLabel());
1175 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001176
Scott Wakeling1f36f412016-04-21 11:13:45 +01001177 // Reference equality check, return 0 if same reference.
1178 __ Subs(out, str, arg);
1179 __ B(&end, eq);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001180
jessicahandojo05765752016-09-09 19:01:32 -07001181 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001182 // Load `count` fields of this and argument strings.
jessicahandojo05765752016-09-09 19:01:32 -07001183 __ Ldr(temp3, HeapOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001184 __ Ldr(temp2, HeapOperand(arg, count_offset));
jessicahandojo05765752016-09-09 19:01:32 -07001185 // Clean out compression flag from lengths.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001186 __ Lsr(temp0, temp3, 1u);
1187 __ Lsr(temp1, temp2, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001188 } else {
1189 // Load lengths of this and argument strings.
1190 __ Ldr(temp0, HeapOperand(str, count_offset));
1191 __ Ldr(temp1, HeapOperand(arg, count_offset));
1192 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001193 // out = length diff.
1194 __ Subs(out, temp0, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001195 // temp0 = min(len(str), len(arg)).
1196 __ Csel(temp0, temp1, temp0, ge);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001197 // Shorter string is empty?
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001198 __ Cbz(temp0, &end);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001199
jessicahandojo05765752016-09-09 19:01:32 -07001200 if (mirror::kUseStringCompression) {
1201 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001202 __ Eor(temp2, temp2, Operand(temp3));
1203 // Interleave with compression flag extraction which is needed for both paths
1204 // and also set flags which is needed only for the different compressions path.
1205 __ Ands(temp3.W(), temp3.W(), Operand(1));
1206 __ Tbnz(temp2, 0, &different_compression); // Does not use flags.
jessicahandojo05765752016-09-09 19:01:32 -07001207 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001208 // Store offset of string value in preparation for comparison loop.
1209 __ Mov(temp1, value_offset);
jessicahandojo05765752016-09-09 19:01:32 -07001210 if (mirror::kUseStringCompression) {
1211 // For string compression, calculate the number of bytes to compare (not chars).
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001212 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
1213 __ Lsl(temp0, temp0, temp3);
jessicahandojo05765752016-09-09 19:01:32 -07001214 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001215
1216 UseScratchRegisterScope scratch_scope(masm);
1217 Register temp4 = scratch_scope.AcquireX();
1218
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001219 // Assertions that must hold in order to compare strings 8 bytes at a time.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001220 DCHECK_ALIGNED(value_offset, 8);
1221 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1222
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001223 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001224 DCHECK_EQ(char_size, 2u);
1225
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001226 // Promote temp2 to an X reg, ready for LDR.
1227 temp2 = temp2.X();
Scott Wakeling1f36f412016-04-21 11:13:45 +01001228
1229 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1230 __ Bind(&loop);
Alexandre Rames2ea91532016-08-11 17:04:14 +01001231 __ Ldr(temp4, MemOperand(str.X(), temp1.X()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001232 __ Ldr(temp2, MemOperand(arg.X(), temp1.X()));
1233 __ Cmp(temp4, temp2);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001234 __ B(ne, &find_char_diff);
1235 __ Add(temp1, temp1, char_size * 4);
jessicahandojo05765752016-09-09 19:01:32 -07001236 // With string compression, we have compared 8 bytes, otherwise 4 chars.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001237 __ Subs(temp0, temp0, (mirror::kUseStringCompression) ? 8 : 4);
1238 __ B(&loop, hi);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001239 __ B(&end);
1240
1241 // Promote temp1 to an X reg, ready for EOR.
1242 temp1 = temp1.X();
1243
jessicahandojo05765752016-09-09 19:01:32 -07001244 // Find the single character difference.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001245 __ Bind(&find_char_diff);
1246 // Get the bit position of the first character that differs.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001247 __ Eor(temp1, temp2, temp4);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001248 __ Rbit(temp1, temp1);
1249 __ Clz(temp1, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001250
jessicahandojo05765752016-09-09 19:01:32 -07001251 // If the number of chars remaining <= the index where the difference occurs (0-3), then
Scott Wakeling1f36f412016-04-21 11:13:45 +01001252 // the difference occurs outside the remaining string data, so just return length diff (out).
jessicahandojo05765752016-09-09 19:01:32 -07001253 // Unlike ARM, we're doing the comparison in one go here, without the subtraction at the
1254 // find_char_diff_2nd_cmp path, so it doesn't matter whether the comparison is signed or
1255 // unsigned when string compression is disabled.
1256 // When it's enabled, the comparison must be unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001257 __ Cmp(temp0, Operand(temp1.W(), LSR, (mirror::kUseStringCompression) ? 3 : 4));
jessicahandojo05765752016-09-09 19:01:32 -07001258 __ B(ls, &end);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001259
Scott Wakeling1f36f412016-04-21 11:13:45 +01001260 // Extract the characters and calculate the difference.
jessicahandojo05765752016-09-09 19:01:32 -07001261 if (mirror:: kUseStringCompression) {
jessicahandojo05765752016-09-09 19:01:32 -07001262 __ Bic(temp1, temp1, 0x7);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001263 __ Bic(temp1, temp1, Operand(temp3.X(), LSL, 3u));
1264 } else {
1265 __ Bic(temp1, temp1, 0xf);
jessicahandojo05765752016-09-09 19:01:32 -07001266 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001267 __ Lsr(temp2, temp2, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001268 __ Lsr(temp4, temp4, temp1);
jessicahandojo05765752016-09-09 19:01:32 -07001269 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001270 // Prioritize the case of compressed strings and calculate such result first.
1271 __ Uxtb(temp1, temp4);
1272 __ Sub(out, temp1.W(), Operand(temp2.W(), UXTB));
1273 __ Tbz(temp3, 0u, &end); // If actually compressed, we're done.
jessicahandojo05765752016-09-09 19:01:32 -07001274 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001275 __ Uxth(temp4, temp4);
1276 __ Sub(out, temp4.W(), Operand(temp2.W(), UXTH));
jessicahandojo05765752016-09-09 19:01:32 -07001277
1278 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001279 __ B(&end);
1280 __ Bind(&different_compression);
1281
1282 // Comparison for different compression style.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo05765752016-09-09 19:01:32 -07001284 DCHECK_EQ(c_char_size, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001285 temp1 = temp1.W();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001286 temp2 = temp2.W();
1287 temp4 = temp4.W();
jessicahandojo05765752016-09-09 19:01:32 -07001288
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001289 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
1290 // Note that flags have been set by the `str` compression flag extraction to `temp3`
1291 // before branching to the `different_compression` label.
1292 __ Csel(temp1, str, arg, eq); // Pointer to the compressed string.
1293 __ Csel(temp2, str, arg, ne); // Pointer to the uncompressed string.
jessicahandojo05765752016-09-09 19:01:32 -07001294
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001295 // We want to free up the temp3, currently holding `str` compression flag, for comparison.
1296 // So, we move it to the bottom bit of the iteration count `temp0` which we then need to treat
1297 // as unsigned. Start by freeing the bit with a LSL and continue further down by a SUB which
1298 // will allow `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
1299 __ Lsl(temp0, temp0, 1u);
1300
1301 // Adjust temp1 and temp2 from string pointers to data pointers.
1302 __ Add(temp1, temp1, Operand(value_offset));
1303 __ Add(temp2, temp2, Operand(value_offset));
1304
1305 // Complete the move of the compression flag.
1306 __ Sub(temp0, temp0, Operand(temp3));
1307
1308 vixl::aarch64::Label different_compression_loop;
1309 vixl::aarch64::Label different_compression_diff;
1310
1311 __ Bind(&different_compression_loop);
1312 __ Ldrb(temp4, MemOperand(temp1.X(), c_char_size, PostIndex));
1313 __ Ldrh(temp3, MemOperand(temp2.X(), char_size, PostIndex));
1314 __ Subs(temp4, temp4, Operand(temp3));
1315 __ B(&different_compression_diff, ne);
1316 __ Subs(temp0, temp0, 2);
1317 __ B(&different_compression_loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001318 __ B(&end);
1319
1320 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001321 __ Bind(&different_compression_diff);
1322 __ Tst(temp0, Operand(1));
1323 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1324 "Expecting 0=compressed, 1=uncompressed");
1325 __ Cneg(out, temp4, ne);
jessicahandojo05765752016-09-09 19:01:32 -07001326 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001327
1328 __ Bind(&end);
1329
1330 if (can_slow_path) {
1331 __ Bind(slow_path->GetExitLabel());
1332 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001333}
1334
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001335// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1336// The normal loop plus the pre-header is 9 instructions without string compression and 12
1337// instructions with string compression. We can compare up to 8 bytes in 4 instructions
1338// (LDR+LDR+CMP+BNE) and up to 16 bytes in 5 instructions (LDP+LDP+CMP+CCMP+BNE). Allow up
1339// to 10 instructions for the unrolled loop.
1340constexpr size_t kShortConstStringEqualsCutoffInBytes = 32;
1341
1342static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1343 if (candidate->IsLoadString()) {
1344 HLoadString* load_string = candidate->AsLoadString();
1345 const DexFile& dex_file = load_string->GetDexFile();
1346 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1347 }
1348 return nullptr;
1349}
1350
Agi Csakiea34b402015-08-13 17:51:19 -07001351void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001352 if (kEmitCompilerReadBarrier &&
1353 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1354 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1355 // No support for this odd case (String class is moveable, not in the boot image).
1356 return;
1357 }
1358
Vladimir Markoca6fff82017-10-03 14:49:14 +01001359 LocationSummary* locations =
1360 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakiea34b402015-08-13 17:51:19 -07001361 locations->SetInAt(0, Location::RequiresRegister());
1362 locations->SetInAt(1, Location::RequiresRegister());
Agi Csakiea34b402015-08-13 17:51:19 -07001363
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001364 // For the generic implementation and for long const strings we need a temporary.
1365 // We do not need it for short const strings, up to 8 bytes, see code generation below.
1366 uint32_t const_string_length = 0u;
1367 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1368 if (const_string == nullptr) {
1369 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1370 }
1371 bool is_compressed =
1372 mirror::kUseStringCompression &&
1373 const_string != nullptr &&
1374 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1375 if (const_string == nullptr || const_string_length > (is_compressed ? 8u : 4u)) {
1376 locations->AddTemp(Location::RequiresRegister());
1377 }
1378
1379 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1380 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1381 // Then we shall need an extra temporary register instead of the output register.
Agi Csakiea34b402015-08-13 17:51:19 -07001382 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1383}
1384
1385void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001386 MacroAssembler* masm = GetVIXLAssembler();
Agi Csakiea34b402015-08-13 17:51:19 -07001387 LocationSummary* locations = invoke->GetLocations();
1388
1389 Register str = WRegisterFrom(locations->InAt(0));
1390 Register arg = WRegisterFrom(locations->InAt(1));
1391 Register out = XRegisterFrom(locations->Out());
1392
1393 UseScratchRegisterScope scratch_scope(masm);
1394 Register temp = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001395 Register temp1 = scratch_scope.AcquireW();
Agi Csakiea34b402015-08-13 17:51:19 -07001396
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001397 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001398 vixl::aarch64::Label end;
1399 vixl::aarch64::Label return_true;
1400 vixl::aarch64::Label return_false;
Agi Csakiea34b402015-08-13 17:51:19 -07001401
1402 // Get offsets of count, value, and class fields within a string object.
1403 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1404 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1405 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1406
1407 // Note that the null check must have been done earlier.
1408 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1409
Vladimir Marko53b52002016-05-24 19:30:45 +01001410 StringEqualsOptimizations optimizations(invoke);
1411 if (!optimizations.GetArgumentNotNull()) {
1412 // Check if input is null, return false if it is.
1413 __ Cbz(arg, &return_false);
1414 }
Agi Csakiea34b402015-08-13 17:51:19 -07001415
1416 // Reference equality check, return true if same reference.
1417 __ Cmp(str, arg);
1418 __ B(&return_true, eq);
1419
Vladimir Marko53b52002016-05-24 19:30:45 +01001420 if (!optimizations.GetArgumentIsString()) {
1421 // Instanceof check for the argument by comparing class fields.
1422 // All string objects must have the same type since String cannot be subclassed.
1423 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1424 // If the argument is a string object, its class field must be equal to receiver's class field.
1425 __ Ldr(temp, MemOperand(str.X(), class_offset));
1426 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1427 __ Cmp(temp, temp1);
1428 __ B(&return_false, ne);
1429 }
Agi Csakiea34b402015-08-13 17:51:19 -07001430
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001431 // Check if one of the inputs is a const string. Do not special-case both strings
1432 // being const, such cases should be handled by constant folding if needed.
1433 uint32_t const_string_length = 0u;
1434 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1435 if (const_string == nullptr) {
1436 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1437 if (const_string != nullptr) {
1438 std::swap(str, arg); // Make sure the const string is in `str`.
1439 }
1440 }
1441 bool is_compressed =
1442 mirror::kUseStringCompression &&
1443 const_string != nullptr &&
1444 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1445
1446 if (const_string != nullptr) {
1447 // Load `count` field of the argument string and check if it matches the const string.
1448 // Also compares the compression style, if differs return false.
1449 __ Ldr(temp, MemOperand(arg.X(), count_offset));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001450 // Temporarily release temp1 as we may not be able to embed the flagged count in CMP immediate.
1451 scratch_scope.Release(temp1);
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001452 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001453 temp1 = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001454 __ B(&return_false, ne);
1455 } else {
1456 // Load `count` fields of this and argument strings.
1457 __ Ldr(temp, MemOperand(str.X(), count_offset));
1458 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1459 // Check if `count` fields are equal, return false if they're not.
1460 // Also compares the compression style, if differs return false.
1461 __ Cmp(temp, temp1);
1462 __ B(&return_false, ne);
1463 }
Agi Csakiea34b402015-08-13 17:51:19 -07001464
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001465 // Assertions that must hold in order to compare strings 8 bytes at a time.
Vladimir Marko984519c2017-08-23 10:45:29 +01001466 // Ok to do this because strings are zero-padded to kObjectAlignment.
Agi Csakiea34b402015-08-13 17:51:19 -07001467 DCHECK_ALIGNED(value_offset, 8);
1468 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1469
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001470 if (const_string != nullptr &&
Vladimir Marko984519c2017-08-23 10:45:29 +01001471 const_string_length <= (is_compressed ? kShortConstStringEqualsCutoffInBytes
1472 : kShortConstStringEqualsCutoffInBytes / 2u)) {
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001473 // Load and compare the contents. Though we know the contents of the short const string
1474 // at compile time, materializing constants may be more code than loading from memory.
1475 int32_t offset = value_offset;
1476 size_t remaining_bytes =
1477 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 8u);
1478 temp = temp.X();
1479 temp1 = temp1.X();
Vladimir Marko984519c2017-08-23 10:45:29 +01001480 while (remaining_bytes > sizeof(uint64_t)) {
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001481 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1482 __ Ldp(temp, temp1, MemOperand(str.X(), offset));
1483 __ Ldp(temp2, out, MemOperand(arg.X(), offset));
1484 __ Cmp(temp, temp2);
1485 __ Ccmp(temp1, out, NoFlag, eq);
1486 __ B(&return_false, ne);
1487 offset += 2u * sizeof(uint64_t);
1488 remaining_bytes -= 2u * sizeof(uint64_t);
1489 }
1490 if (remaining_bytes != 0u) {
1491 __ Ldr(temp, MemOperand(str.X(), offset));
1492 __ Ldr(temp1, MemOperand(arg.X(), offset));
1493 __ Cmp(temp, temp1);
1494 __ B(&return_false, ne);
1495 }
1496 } else {
1497 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1498 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1499 "Expecting 0=compressed, 1=uncompressed");
1500 __ Cbz(temp, &return_true);
1501
1502 if (mirror::kUseStringCompression) {
1503 // For string compression, calculate the number of bytes to compare (not chars).
1504 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1505 __ And(temp1, temp, Operand(1)); // Extract compression flag.
1506 __ Lsr(temp, temp, 1u); // Extract length.
1507 __ Lsl(temp, temp, temp1); // Calculate number of bytes to compare.
1508 }
1509
1510 // Store offset of string value in preparation for comparison loop
1511 __ Mov(temp1, value_offset);
1512
1513 temp1 = temp1.X();
1514 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1515 // Loop to compare strings 8 bytes at a time starting at the front of the string.
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001516 __ Bind(&loop);
1517 __ Ldr(out, MemOperand(str.X(), temp1));
1518 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1519 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1520 __ Cmp(out, temp2);
1521 __ B(&return_false, ne);
1522 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1523 __ Sub(temp, temp, Operand(mirror::kUseStringCompression ? 8 : 4), SetFlags);
1524 __ B(&loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001525 }
1526
Agi Csakiea34b402015-08-13 17:51:19 -07001527 // Return true and exit the function.
1528 // If loop does not result in returning false, we return true.
1529 __ Bind(&return_true);
1530 __ Mov(out, 1);
1531 __ B(&end);
1532
1533 // Return false and exit the function.
1534 __ Bind(&return_false);
1535 __ Mov(out, 0);
1536 __ Bind(&end);
1537}
1538
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001539static void GenerateVisitStringIndexOf(HInvoke* invoke,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001540 MacroAssembler* masm,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001541 CodeGeneratorARM64* codegen,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001542 bool start_at_zero) {
1543 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001544
1545 // Note that the null check must have been done earlier.
1546 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1547
1548 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001549 // 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 -07001550 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001551 HInstruction* code_point = invoke->InputAt(1);
1552 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001553 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001554 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1555 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001556 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001557 codegen->AddSlowPath(slow_path);
1558 __ B(slow_path->GetEntryLabel());
1559 __ Bind(slow_path->GetExitLabel());
1560 return;
1561 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001562 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001563 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001564 __ Tst(char_reg, 0xFFFF0000);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001565 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001566 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001567 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001568 }
1569
1570 if (start_at_zero) {
1571 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001572 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001573 __ Mov(tmp_reg, 0);
1574 }
1575
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001576 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Roland Levillain42ad2882016-02-29 18:26:54 +00001577 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001578
1579 if (slow_path != nullptr) {
1580 __ Bind(slow_path->GetExitLabel());
1581 }
1582}
1583
1584void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001585 LocationSummary* locations = new (allocator_) LocationSummary(
1586 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001587 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1588 // best to align the inputs accordingly.
1589 InvokeRuntimeCallingConvention calling_convention;
1590 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1591 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001592 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt32));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001593
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001594 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001595 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1596}
1597
1598void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001599 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001600}
1601
1602void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001603 LocationSummary* locations = new (allocator_) LocationSummary(
1604 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001605 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1606 // best to align the inputs accordingly.
1607 InvokeRuntimeCallingConvention calling_convention;
1608 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1609 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1610 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt32));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001612}
1613
1614void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001615 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001616}
1617
Jeff Hao848f70a2014-01-15 13:49:50 -08001618void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001619 LocationSummary* locations = new (allocator_) LocationSummary(
1620 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001621 InvokeRuntimeCallingConvention calling_convention;
1622 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1623 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1624 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1625 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001626 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001627}
1628
1629void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001630 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001631 LocationSummary* locations = invoke->GetLocations();
1632
1633 Register byte_array = WRegisterFrom(locations->InAt(0));
1634 __ Cmp(byte_array, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001635 SlowPathCodeARM64* slow_path =
1636 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001637 codegen_->AddSlowPath(slow_path);
1638 __ B(eq, slow_path->GetEntryLabel());
1639
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001640 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001641 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001642 __ Bind(slow_path->GetExitLabel());
1643}
1644
1645void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001646 LocationSummary* locations =
1647 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001648 InvokeRuntimeCallingConvention calling_convention;
1649 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1650 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1651 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001652 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001653}
1654
1655void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001656 // No need to emit code checking whether `locations->InAt(2)` is a null
1657 // pointer, as callers of the native method
1658 //
1659 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1660 //
1661 // all include a null check on `data` before calling that method.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001662 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001663 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001664}
1665
1666void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001667 LocationSummary* locations = new (allocator_) LocationSummary(
1668 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001669 InvokeRuntimeCallingConvention calling_convention;
1670 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001671 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001672}
1673
1674void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001675 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001676 LocationSummary* locations = invoke->GetLocations();
1677
1678 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1679 __ Cmp(string_to_copy, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001680 SlowPathCodeARM64* slow_path =
1681 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001682 codegen_->AddSlowPath(slow_path);
1683 __ B(eq, slow_path->GetEntryLabel());
1684
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001685 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001686 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001687 __ Bind(slow_path->GetExitLabel());
1688}
1689
Vladimir Markoca6fff82017-10-03 14:49:14 +01001690static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001691 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1693 DCHECK(DataType::IsFloatingPointType(invoke->GetType()));
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001694
Vladimir Markoca6fff82017-10-03 14:49:14 +01001695 LocationSummary* const locations =
1696 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001697 InvokeRuntimeCallingConvention calling_convention;
1698
1699 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1700 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1701}
1702
Vladimir Markoca6fff82017-10-03 14:49:14 +01001703static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001704 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001705 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1706 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1707 DCHECK(DataType::IsFloatingPointType(invoke->GetType()));
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001708
Vladimir Markoca6fff82017-10-03 14:49:14 +01001709 LocationSummary* const locations =
1710 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001711 InvokeRuntimeCallingConvention calling_convention;
1712
1713 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1714 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1715 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1716}
1717
1718static void GenFPToFPCall(HInvoke* invoke,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001719 CodeGeneratorARM64* codegen,
1720 QuickEntrypointEnum entry) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001721 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001722}
1723
1724void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001726}
1727
1728void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001729 GenFPToFPCall(invoke, codegen_, kQuickCos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001730}
1731
1732void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001733 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001734}
1735
1736void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001737 GenFPToFPCall(invoke, codegen_, kQuickSin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001738}
1739
1740void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001741 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001742}
1743
1744void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001745 GenFPToFPCall(invoke, codegen_, kQuickAcos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001746}
1747
1748void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001749 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001750}
1751
1752void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001753 GenFPToFPCall(invoke, codegen_, kQuickAsin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001754}
1755
1756void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001757 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001758}
1759
1760void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001761 GenFPToFPCall(invoke, codegen_, kQuickAtan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001762}
1763
1764void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001765 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001766}
1767
1768void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001769 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001770}
1771
1772void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001773 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001774}
1775
1776void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001777 GenFPToFPCall(invoke, codegen_, kQuickCosh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001778}
1779
1780void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001781 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001782}
1783
1784void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001785 GenFPToFPCall(invoke, codegen_, kQuickExp);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001786}
1787
1788void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001789 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001790}
1791
1792void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001793 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001794}
1795
1796void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001797 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001798}
1799
1800void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001801 GenFPToFPCall(invoke, codegen_, kQuickLog);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001802}
1803
1804void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001805 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001806}
1807
1808void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001809 GenFPToFPCall(invoke, codegen_, kQuickLog10);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001810}
1811
1812void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001813 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001814}
1815
1816void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001817 GenFPToFPCall(invoke, codegen_, kQuickSinh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001818}
1819
1820void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001821 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001822}
1823
1824void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001825 GenFPToFPCall(invoke, codegen_, kQuickTan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001826}
1827
1828void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001829 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001830}
1831
1832void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001833 GenFPToFPCall(invoke, codegen_, kQuickTanh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001834}
1835
1836void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001837 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001838}
1839
1840void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001841 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001842}
1843
Vladimir Marko4d179872018-01-19 14:50:10 +00001844void IntrinsicLocationsBuilderARM64::VisitMathPow(HInvoke* invoke) {
1845 CreateFPFPToFPCallLocations(allocator_, invoke);
1846}
1847
1848void IntrinsicCodeGeneratorARM64::VisitMathPow(HInvoke* invoke) {
1849 GenFPToFPCall(invoke, codegen_, kQuickPow);
1850}
1851
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001852void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001853 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001854}
1855
1856void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001857 GenFPToFPCall(invoke, codegen_, kQuickHypot);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001858}
1859
1860void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001862}
1863
1864void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001865 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001866}
1867
Tim Zhang25abd6c2016-01-19 23:39:24 +08001868void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001869 LocationSummary* locations =
1870 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001871 locations->SetInAt(0, Location::RequiresRegister());
1872 locations->SetInAt(1, Location::RequiresRegister());
1873 locations->SetInAt(2, Location::RequiresRegister());
1874 locations->SetInAt(3, Location::RequiresRegister());
1875 locations->SetInAt(4, Location::RequiresRegister());
1876
1877 locations->AddTemp(Location::RequiresRegister());
1878 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01001879 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08001880}
1881
1882void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001883 MacroAssembler* masm = GetVIXLAssembler();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001884 LocationSummary* locations = invoke->GetLocations();
1885
1886 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001887 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001888 DCHECK_EQ(char_size, 2u);
1889
1890 // Location of data in char array buffer.
1891 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1892
1893 // Location of char array data in string.
1894 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1895
1896 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1897 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
1898 Register srcObj = XRegisterFrom(locations->InAt(0));
1899 Register srcBegin = XRegisterFrom(locations->InAt(1));
1900 Register srcEnd = XRegisterFrom(locations->InAt(2));
1901 Register dstObj = XRegisterFrom(locations->InAt(3));
1902 Register dstBegin = XRegisterFrom(locations->InAt(4));
1903
1904 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001905 Register num_chr = XRegisterFrom(locations->GetTemp(1));
1906 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001907
1908 UseScratchRegisterScope temps(masm);
1909 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01001910 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001911
jessicahandojo05765752016-09-09 19:01:32 -07001912 vixl::aarch64::Label done;
1913 vixl::aarch64::Label compressed_string_loop;
1914 __ Sub(num_chr, srcEnd, srcBegin);
1915 // Early out for valid zero-length retrievals.
1916 __ Cbz(num_chr, &done);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001917
Scott Wakelingdf109d92016-04-22 11:35:56 +01001918 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001919 __ Add(dst_ptr, dstObj, Operand(data_offset));
1920 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
1921
jessicahandojo05765752016-09-09 19:01:32 -07001922 // src address to copy from.
1923 __ Add(src_ptr, srcObj, Operand(value_offset));
1924 vixl::aarch64::Label compressed_string_preloop;
1925 if (mirror::kUseStringCompression) {
1926 // Location of count in string.
1927 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1928 // String's length.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001929 __ Ldr(tmp2, MemOperand(srcObj, count_offset));
1930 __ Tbz(tmp2, 0, &compressed_string_preloop);
jessicahandojo05765752016-09-09 19:01:32 -07001931 }
1932 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001933
Tim Zhang25abd6c2016-01-19 23:39:24 +08001934 // Do the copy.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001935 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001936 vixl::aarch64::Label remainder;
Scott Wakelingdf109d92016-04-22 11:35:56 +01001937
Scott Wakelingdf109d92016-04-22 11:35:56 +01001938 // Save repairing the value of num_chr on the < 8 character path.
1939 __ Subs(tmp1, num_chr, 8);
1940 __ B(lt, &remainder);
1941
1942 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
1943 __ Mov(num_chr, tmp1);
1944
1945 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
1946 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08001947 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001948 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001949 __ Subs(num_chr, num_chr, 8);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001950 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001951 __ B(ge, &loop);
1952
1953 __ Adds(num_chr, num_chr, 8);
1954 __ B(eq, &done);
1955
1956 // Main loop for < 8 character case and remainder handling. Loads and stores one
1957 // 16-bit Java character at a time.
1958 __ Bind(&remainder);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001959 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001960 __ Subs(num_chr, num_chr, 1);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001961 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001962 __ B(gt, &remainder);
jessicahandojo05765752016-09-09 19:01:32 -07001963 __ B(&done);
1964
1965 if (mirror::kUseStringCompression) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001966 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo05765752016-09-09 19:01:32 -07001967 DCHECK_EQ(c_char_size, 1u);
1968 __ Bind(&compressed_string_preloop);
1969 __ Add(src_ptr, src_ptr, Operand(srcBegin));
1970 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
1971 __ Bind(&compressed_string_loop);
1972 __ Ldrb(tmp1, MemOperand(src_ptr, c_char_size, PostIndex));
1973 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
1974 __ Subs(num_chr, num_chr, Operand(1));
1975 __ B(gt, &compressed_string_loop);
1976 }
Scott Wakelingdf109d92016-04-22 11:35:56 +01001977
Tim Zhang25abd6c2016-01-19 23:39:24 +08001978 __ Bind(&done);
1979}
1980
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001981// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
1982// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08001983static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001984
1985static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
1986 uint32_t at,
1987 HInstruction* input) {
1988 HIntConstant* const_input = input->AsIntConstant();
Scott Wakeling97c72b72016-06-24 16:19:36 +01001989 if (const_input != nullptr && !vixl::aarch64::Assembler::IsImmAddSub(const_input->GetValue())) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00001990 locations->SetInAt(at, Location::RequiresRegister());
1991 } else {
1992 locations->SetInAt(at, Location::RegisterOrConstant(input));
1993 }
1994}
1995
1996void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1997 // Check to see if we have known failures that will cause us to have to bail out
1998 // to the runtime, and just generate the runtime call directly.
1999 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2000 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
2001
2002 // The positions must be non-negative.
2003 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2004 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
2005 // We will have to fail anyways.
2006 return;
2007 }
2008
2009 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
2010 // native implementation.
2011 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2012 if (length != nullptr) {
2013 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002014 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002015 // Just call as normal.
2016 return;
2017 }
2018 }
2019
Vladimir Markoca6fff82017-10-03 14:49:14 +01002020 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
2021 LocationSummary* locations =
2022 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002023 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
2024 locations->SetInAt(0, Location::RequiresRegister());
2025 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2026 locations->SetInAt(2, Location::RequiresRegister());
2027 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2028 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2029
2030 locations->AddTemp(Location::RequiresRegister());
2031 locations->AddTemp(Location::RequiresRegister());
2032 locations->AddTemp(Location::RequiresRegister());
2033}
2034
Scott Wakeling97c72b72016-06-24 16:19:36 +01002035static void CheckSystemArrayCopyPosition(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002036 const Location& pos,
2037 const Register& input,
2038 const Location& length,
2039 SlowPathCodeARM64* slow_path,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002040 const Register& temp,
2041 bool length_is_input_length = false) {
2042 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
2043 if (pos.IsConstant()) {
2044 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
2045 if (pos_const == 0) {
2046 if (!length_is_input_length) {
2047 // Check that length(input) >= length.
2048 __ Ldr(temp, MemOperand(input, length_offset));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 __ Cmp(temp, OperandFrom(length, DataType::Type::kInt32));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002050 __ B(slow_path->GetEntryLabel(), lt);
2051 }
2052 } else {
2053 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002054 __ Ldr(temp, MemOperand(input, length_offset));
2055 __ Subs(temp, temp, pos_const);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002056 __ B(slow_path->GetEntryLabel(), lt);
2057
2058 // Check that (length(input) - pos) >= length.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 __ Cmp(temp, OperandFrom(length, DataType::Type::kInt32));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002060 __ B(slow_path->GetEntryLabel(), lt);
2061 }
2062 } else if (length_is_input_length) {
2063 // The only way the copy can succeed is if pos is zero.
2064 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
2065 } else {
2066 // Check that pos >= 0.
2067 Register pos_reg = WRegisterFrom(pos);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002068 __ Tbnz(pos_reg, pos_reg.GetSizeInBits() - 1, slow_path->GetEntryLabel());
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002069
2070 // Check that pos <= length(input) && (length(input) - pos) >= length.
2071 __ Ldr(temp, MemOperand(input, length_offset));
2072 __ Subs(temp, temp, pos_reg);
2073 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 __ Ccmp(temp, OperandFrom(length, DataType::Type::kInt32), NFlag, ge);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002075 __ B(slow_path->GetEntryLabel(), lt);
2076 }
2077}
2078
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002079// Compute base source address, base destination address, and end
2080// source address for System.arraycopy* intrinsics in `src_base`,
2081// `dst_base` and `src_end` respectively.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002082static void GenSystemArrayCopyAddresses(MacroAssembler* masm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 DataType::Type type,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002084 const Register& src,
2085 const Location& src_pos,
2086 const Register& dst,
2087 const Location& dst_pos,
2088 const Location& copy_length,
2089 const Register& src_base,
2090 const Register& dst_base,
2091 const Register& src_end) {
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002092 // This routine is used by the SystemArrayCopy and the SystemArrayCopyChar intrinsics.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 DCHECK(type == DataType::Type::kReference || type == DataType::Type::kUint16)
Roland Levillainebea3d22016-04-12 15:42:57 +01002094 << "Unexpected element type: " << type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002095 const int32_t element_size = DataType::Size(type);
2096 const int32_t element_size_shift = DataType::SizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002097 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002098
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002099 if (src_pos.IsConstant()) {
2100 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002101 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002102 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002103 __ Add(src_base, src, data_offset);
2104 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002105 }
2106
2107 if (dst_pos.IsConstant()) {
2108 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002109 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002110 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002111 __ Add(dst_base, dst, data_offset);
2112 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002113 }
2114
2115 if (copy_length.IsConstant()) {
2116 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002117 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002118 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002119 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002120 }
2121}
2122
2123void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002124 MacroAssembler* masm = GetVIXLAssembler();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002125 LocationSummary* locations = invoke->GetLocations();
2126 Register src = XRegisterFrom(locations->InAt(0));
2127 Location src_pos = locations->InAt(1);
2128 Register dst = XRegisterFrom(locations->InAt(2));
2129 Location dst_pos = locations->InAt(3);
2130 Location length = locations->InAt(4);
2131
Vladimir Marko174b2e22017-10-12 13:34:49 +01002132 SlowPathCodeARM64* slow_path =
2133 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002134 codegen_->AddSlowPath(slow_path);
2135
2136 // If source and destination are the same, take the slow path. Overlapping copy regions must be
2137 // copied in reverse and we can't know in all cases if it's needed.
2138 __ Cmp(src, dst);
2139 __ B(slow_path->GetEntryLabel(), eq);
2140
2141 // Bail out if the source is null.
2142 __ Cbz(src, slow_path->GetEntryLabel());
2143
2144 // Bail out if the destination is null.
2145 __ Cbz(dst, slow_path->GetEntryLabel());
2146
2147 if (!length.IsConstant()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002148 // Merge the following two comparisons into one:
2149 // If the length is negative, bail out (delegate to libcore's native implementation).
2150 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002151 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002152 __ B(slow_path->GetEntryLabel(), hi);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002153 } else {
2154 // We have already checked in the LocationsBuilder for the constant case.
2155 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
2156 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
2157 }
2158
2159 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
2160 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
2161 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
2162
2163 CheckSystemArrayCopyPosition(masm,
2164 src_pos,
2165 src,
2166 length,
2167 slow_path,
2168 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002169 false);
2170
2171 CheckSystemArrayCopyPosition(masm,
2172 dst_pos,
2173 dst,
2174 length,
2175 slow_path,
2176 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002177 false);
2178
2179 src_curr_addr = src_curr_addr.X();
2180 dst_curr_addr = dst_curr_addr.X();
2181 src_stop_addr = src_stop_addr.X();
2182
2183 GenSystemArrayCopyAddresses(masm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002184 DataType::Type::kUint16,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002185 src,
2186 src_pos,
2187 dst,
2188 dst_pos,
2189 length,
2190 src_curr_addr,
2191 dst_curr_addr,
2192 src_stop_addr);
2193
2194 // Iterate over the arrays and do a raw copy of the chars.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002195 const int32_t char_size = DataType::Size(DataType::Type::kUint16);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002196 UseScratchRegisterScope temps(masm);
2197 Register tmp = temps.AcquireW();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002198 vixl::aarch64::Label loop, done;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002199 __ Bind(&loop);
2200 __ Cmp(src_curr_addr, src_stop_addr);
2201 __ B(&done, eq);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002202 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, PostIndex));
2203 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, PostIndex));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002204 __ B(&loop);
2205 __ Bind(&done);
2206
2207 __ Bind(slow_path->GetExitLabel());
2208}
2209
donghui.baic2ec9ad2016-03-10 14:02:55 +08002210// We can choose to use the native implementation there for longer copy lengths.
2211static constexpr int32_t kSystemArrayCopyThreshold = 128;
2212
2213// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2214// We want to use two temporary registers in order to reduce the register pressure in arm64.
2215// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2216void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002217 // The only read barrier implementation supporting the
2218 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2219 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01002220 return;
2221 }
2222
donghui.baic2ec9ad2016-03-10 14:02:55 +08002223 // Check to see if we have known failures that will cause us to have to bail out
2224 // to the runtime, and just generate the runtime call directly.
2225 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2226 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2227
2228 // The positions must be non-negative.
2229 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2230 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2231 // We will have to fail anyways.
2232 return;
2233 }
2234
2235 // The length must be >= 0.
2236 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2237 if (length != nullptr) {
2238 int32_t len = length->GetValue();
2239 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2240 // Just call as normal.
2241 return;
2242 }
2243 }
2244
2245 SystemArrayCopyOptimizations optimizations(invoke);
2246
2247 if (optimizations.GetDestinationIsSource()) {
2248 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2249 // We only support backward copying if source and destination are the same.
2250 return;
2251 }
2252 }
2253
2254 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2255 // We currently don't intrinsify primitive copying.
2256 return;
2257 }
2258
Vladimir Markoca6fff82017-10-03 14:49:14 +01002259 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
2260 LocationSummary* locations =
2261 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002262 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2263 locations->SetInAt(0, Location::RequiresRegister());
2264 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2265 locations->SetInAt(2, Location::RequiresRegister());
2266 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2267 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2268
2269 locations->AddTemp(Location::RequiresRegister());
2270 locations->AddTemp(Location::RequiresRegister());
Roland Levillain0b671c02016-08-19 12:02:34 +01002271 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2272 // Temporary register IP0, obtained from the VIXL scratch register
2273 // pool, cannot be used in ReadBarrierSystemArrayCopySlowPathARM64
2274 // (because that register is clobbered by ReadBarrierMarkRegX
Roland Levillain54f869e2017-03-06 13:54:11 +00002275 // entry points). It cannot be used in calls to
2276 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier
2277 // either. For these reasons, get a third extra temporary register
2278 // from the register allocator.
Roland Levillain0b671c02016-08-19 12:02:34 +01002279 locations->AddTemp(Location::RequiresRegister());
Roland Levillain54f869e2017-03-06 13:54:11 +00002280 } else {
2281 // Cases other than Baker read barriers: the third temporary will
2282 // be acquired from the VIXL scratch register pool.
Roland Levillain0b671c02016-08-19 12:02:34 +01002283 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002284}
2285
2286void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002287 // The only read barrier implementation supporting the
2288 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2289 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002290
Scott Wakeling97c72b72016-06-24 16:19:36 +01002291 MacroAssembler* masm = GetVIXLAssembler();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002292 LocationSummary* locations = invoke->GetLocations();
2293
2294 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2295 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2296 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2297 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002298 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002299
2300 Register src = XRegisterFrom(locations->InAt(0));
2301 Location src_pos = locations->InAt(1);
2302 Register dest = XRegisterFrom(locations->InAt(2));
2303 Location dest_pos = locations->InAt(3);
2304 Location length = locations->InAt(4);
2305 Register temp1 = WRegisterFrom(locations->GetTemp(0));
Roland Levillain0b671c02016-08-19 12:02:34 +01002306 Location temp1_loc = LocationFrom(temp1);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002307 Register temp2 = WRegisterFrom(locations->GetTemp(1));
Roland Levillain0b671c02016-08-19 12:02:34 +01002308 Location temp2_loc = LocationFrom(temp2);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002309
Vladimir Marko174b2e22017-10-12 13:34:49 +01002310 SlowPathCodeARM64* intrinsic_slow_path =
2311 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002312 codegen_->AddSlowPath(intrinsic_slow_path);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002313
Scott Wakeling97c72b72016-06-24 16:19:36 +01002314 vixl::aarch64::Label conditions_on_positions_validated;
donghui.baic2ec9ad2016-03-10 14:02:55 +08002315 SystemArrayCopyOptimizations optimizations(invoke);
2316
donghui.baic2ec9ad2016-03-10 14:02:55 +08002317 // If source and destination are the same, we go to slow path if we need to do
2318 // forward copying.
2319 if (src_pos.IsConstant()) {
2320 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2321 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002322 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2323 if (optimizations.GetDestinationIsSource()) {
2324 // Checked when building locations.
2325 DCHECK_GE(src_pos_constant, dest_pos_constant);
2326 } else if (src_pos_constant < dest_pos_constant) {
2327 __ Cmp(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002328 __ B(intrinsic_slow_path->GetEntryLabel(), eq);
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002329 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002330 // Checked when building locations.
2331 DCHECK(!optimizations.GetDestinationIsSource()
2332 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2333 } else {
2334 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002335 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002336 __ B(&conditions_on_positions_validated, ne);
2337 }
2338 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
Roland Levillain0b671c02016-08-19 12:02:34 +01002339 __ B(intrinsic_slow_path->GetEntryLabel(), gt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002340 }
2341 } else {
2342 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002343 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002344 __ B(&conditions_on_positions_validated, ne);
2345 }
2346 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2347 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
Roland Levillain0b671c02016-08-19 12:02:34 +01002348 __ B(intrinsic_slow_path->GetEntryLabel(), lt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002349 }
2350
2351 __ Bind(&conditions_on_positions_validated);
2352
2353 if (!optimizations.GetSourceIsNotNull()) {
2354 // Bail out if the source is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002355 __ Cbz(src, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002356 }
2357
2358 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2359 // Bail out if the destination is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002360 __ Cbz(dest, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002361 }
2362
2363 // We have already checked in the LocationsBuilder for the constant case.
2364 if (!length.IsConstant() &&
2365 !optimizations.GetCountIsSourceLength() &&
2366 !optimizations.GetCountIsDestinationLength()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002367 // Merge the following two comparisons into one:
2368 // If the length is negative, bail out (delegate to libcore's native implementation).
2369 // If the length >= 128 then (currently) prefer native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002370 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002371 __ B(intrinsic_slow_path->GetEntryLabel(), hs);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002372 }
2373 // Validity checks: source.
2374 CheckSystemArrayCopyPosition(masm,
2375 src_pos,
2376 src,
2377 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002378 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002379 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002380 optimizations.GetCountIsSourceLength());
2381
2382 // Validity checks: dest.
2383 CheckSystemArrayCopyPosition(masm,
2384 dest_pos,
2385 dest,
2386 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002387 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002388 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002389 optimizations.GetCountIsDestinationLength());
2390 {
2391 // We use a block to end the scratch scope before the write barrier, thus
2392 // freeing the temporary registers so they can be used in `MarkGCCard`.
2393 UseScratchRegisterScope temps(masm);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002394 Location temp3_loc; // Used only for Baker read barrier.
Roland Levillain54f869e2017-03-06 13:54:11 +00002395 Register temp3;
2396 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002397 temp3_loc = locations->GetTemp(2);
2398 temp3 = WRegisterFrom(temp3_loc);
Roland Levillain54f869e2017-03-06 13:54:11 +00002399 } else {
2400 temp3 = temps.AcquireW();
2401 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002402
donghui.baic2ec9ad2016-03-10 14:02:55 +08002403 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2404 // Check whether all elements of the source array are assignable to the component
2405 // type of the destination array. We do two checks: the classes are the same,
2406 // or the destination is Object[]. If none of these checks succeed, we go to the
2407 // slow path.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002408
Roland Levillain0b671c02016-08-19 12:02:34 +01002409 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2410 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2411 // /* HeapReference<Class> */ temp1 = src->klass_
2412 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2413 temp1_loc,
2414 src.W(),
2415 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002416 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002417 /* needs_null_check */ false,
2418 /* use_load_acquire */ false);
2419 // Bail out if the source is not a non primitive array.
2420 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2421 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2422 temp1_loc,
2423 temp1,
2424 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002425 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002426 /* needs_null_check */ false,
2427 /* use_load_acquire */ false);
2428 __ Cbz(temp1, intrinsic_slow_path->GetEntryLabel());
2429 // If heap poisoning is enabled, `temp1` has been unpoisoned
2430 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2431 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2432 __ Ldrh(temp1, HeapOperand(temp1, primitive_offset));
2433 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2434 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002435 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002436
2437 // /* HeapReference<Class> */ temp1 = dest->klass_
2438 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2439 temp1_loc,
2440 dest.W(),
2441 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002442 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002443 /* needs_null_check */ false,
2444 /* use_load_acquire */ false);
2445
2446 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2447 // Bail out if the destination is not a non primitive array.
2448 //
2449 // Register `temp1` is not trashed by the read barrier emitted
2450 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2451 // method produces a call to a ReadBarrierMarkRegX entry point,
2452 // which saves all potentially live registers, including
2453 // temporaries such a `temp1`.
2454 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2455 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2456 temp2_loc,
2457 temp1,
2458 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002459 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002460 /* needs_null_check */ false,
2461 /* use_load_acquire */ false);
2462 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2463 // If heap poisoning is enabled, `temp2` has been unpoisoned
2464 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2465 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2466 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
2467 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2468 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
2469 }
2470
2471 // For the same reason given earlier, `temp1` is not trashed by the
2472 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2473 // /* HeapReference<Class> */ temp2 = src->klass_
2474 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2475 temp2_loc,
2476 src.W(),
2477 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002478 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002479 /* needs_null_check */ false,
2480 /* use_load_acquire */ false);
2481 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2482 __ Cmp(temp1, temp2);
2483
2484 if (optimizations.GetDestinationIsTypedObjectArray()) {
2485 vixl::aarch64::Label do_copy;
2486 __ B(&do_copy, eq);
2487 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2488 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2489 temp1_loc,
2490 temp1,
2491 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002492 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002493 /* needs_null_check */ false,
2494 /* use_load_acquire */ false);
2495 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2496 // We do not need to emit a read barrier for the following
2497 // heap reference load, as `temp1` is only used in a
2498 // comparison with null below, and this reference is not
2499 // kept afterwards.
2500 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2501 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2502 __ Bind(&do_copy);
2503 } else {
2504 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2505 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002506 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002507 // Non read barrier code.
2508
2509 // /* HeapReference<Class> */ temp1 = dest->klass_
2510 __ Ldr(temp1, MemOperand(dest, class_offset));
2511 // /* HeapReference<Class> */ temp2 = src->klass_
2512 __ Ldr(temp2, MemOperand(src, class_offset));
2513 bool did_unpoison = false;
2514 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2515 !optimizations.GetSourceIsNonPrimitiveArray()) {
2516 // One or two of the references need to be unpoisoned. Unpoison them
2517 // both to make the identity check valid.
2518 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2519 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2520 did_unpoison = true;
2521 }
2522
2523 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2524 // Bail out if the destination is not a non primitive array.
2525 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2526 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2527 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2528 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2529 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2530 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2531 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2532 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2533 }
2534
2535 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2536 // Bail out if the source is not a non primitive array.
2537 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2538 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2539 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2540 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2541 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2542 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2543 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2544 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2545 }
2546
2547 __ Cmp(temp1, temp2);
2548
2549 if (optimizations.GetDestinationIsTypedObjectArray()) {
2550 vixl::aarch64::Label do_copy;
2551 __ B(&do_copy, eq);
2552 if (!did_unpoison) {
2553 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2554 }
2555 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2556 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2557 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2558 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2559 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2560 // No need to unpoison the result, we're comparing against null.
2561 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2562 __ Bind(&do_copy);
2563 } else {
2564 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2565 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002566 }
2567 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2568 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2569 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002570 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2571 // /* HeapReference<Class> */ temp1 = src->klass_
2572 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2573 temp1_loc,
2574 src.W(),
2575 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002576 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002577 /* needs_null_check */ false,
2578 /* use_load_acquire */ false);
2579 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2580 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2581 temp2_loc,
2582 temp1,
2583 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002584 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002585 /* needs_null_check */ false,
2586 /* use_load_acquire */ false);
2587 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2588 // If heap poisoning is enabled, `temp2` has been unpoisoned
2589 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2590 } else {
2591 // /* HeapReference<Class> */ temp1 = src->klass_
2592 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2593 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2594 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2595 __ Ldr(temp2, HeapOperand(temp1, component_offset));
2596 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2597 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2598 }
2599 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2600 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
donghui.baic2ec9ad2016-03-10 14:02:55 +08002601 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain0b671c02016-08-19 12:02:34 +01002602 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002603 }
2604
Roland Levillain1663d162017-03-17 15:15:21 +00002605 if (length.IsConstant() && length.GetConstant()->AsIntConstant()->GetValue() == 0) {
2606 // Null constant length: not need to emit the loop code at all.
Roland Levillain0b671c02016-08-19 12:02:34 +01002607 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002608 Register src_curr_addr = temp1.X();
2609 Register dst_curr_addr = temp2.X();
2610 Register src_stop_addr = temp3.X();
2611 vixl::aarch64::Label done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002612 const DataType::Type type = DataType::Type::kReference;
2613 const int32_t element_size = DataType::Size(type);
Roland Levillain1663d162017-03-17 15:15:21 +00002614
2615 if (length.IsRegister()) {
2616 // Don't enter the copy loop if the length is null.
2617 __ Cbz(WRegisterFrom(length), &done);
2618 }
2619
2620 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2621 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2622
2623 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002624 // also CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002625 //
2626 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2627 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2628 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2629 // if (is_gray) {
2630 // // Slow-path copy.
2631 // do {
2632 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2633 // } while (src_ptr != end_ptr)
2634 // } else {
2635 // // Fast-path copy.
2636 // do {
2637 // *dest_ptr++ = *src_ptr++;
2638 // } while (src_ptr != end_ptr)
2639 // }
2640
2641 // Make sure `tmp` is not IP0, as it is clobbered by
2642 // ReadBarrierMarkRegX entry points in
2643 // ReadBarrierSystemArrayCopySlowPathARM64.
Roland Levillain1ca955d2017-04-13 19:34:30 +01002644 DCHECK(temps.IsAvailable(ip0));
Roland Levillain1663d162017-03-17 15:15:21 +00002645 temps.Exclude(ip0);
Roland Levillain0b671c02016-08-19 12:02:34 +01002646 Register tmp = temps.AcquireW();
Roland Levillain1663d162017-03-17 15:15:21 +00002647 DCHECK_NE(LocationFrom(tmp).reg(), IP0);
Roland Levillain1ca955d2017-04-13 19:34:30 +01002648 // Put IP0 back in the pool so that VIXL has at least one
2649 // scratch register available to emit macro-instructions (note
2650 // that IP1 is already used for `tmp`). Indeed some
2651 // macro-instructions used in GenSystemArrayCopyAddresses
2652 // (invoked hereunder) may require a scratch register (for
2653 // instance to emit a load with a large constant offset).
2654 temps.Include(ip0);
Roland Levillain1663d162017-03-17 15:15:21 +00002655
2656 // /* int32_t */ monitor = src->monitor_
2657 __ Ldr(tmp, HeapOperand(src.W(), monitor_offset));
2658 // /* LockWord */ lock_word = LockWord(monitor)
2659 static_assert(sizeof(LockWord) == sizeof(int32_t),
2660 "art::LockWord and int32_t have different sizes.");
2661
2662 // Introduce a dependency on the lock_word including rb_state,
2663 // to prevent load-load reordering, and without using
2664 // a memory barrier (which would be more expensive).
2665 // `src` is unchanged by this operation, but its value now depends
2666 // on `tmp`.
2667 __ Add(src.X(), src.X(), Operand(tmp.X(), LSR, 32));
2668
2669 // Compute base source address, base destination address, and end
2670 // source address for System.arraycopy* intrinsics in `src_base`,
2671 // `dst_base` and `src_end` respectively.
2672 // Note that `src_curr_addr` is computed from from `src` (and
2673 // `src_pos`) here, and thus honors the artificial dependency
2674 // of `src` on `tmp`.
2675 GenSystemArrayCopyAddresses(masm,
2676 type,
2677 src,
2678 src_pos,
2679 dest,
2680 dest_pos,
2681 length,
2682 src_curr_addr,
2683 dst_curr_addr,
2684 src_stop_addr);
2685
2686 // Slow path used to copy array when `src` is gray.
2687 SlowPathCodeARM64* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002688 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathARM64(
2689 invoke, LocationFrom(tmp));
Roland Levillain1663d162017-03-17 15:15:21 +00002690 codegen_->AddSlowPath(read_barrier_slow_path);
2691
2692 // Given the numeric representation, it's enough to check the low bit of the rb_state.
2693 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2694 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2695 __ Tbnz(tmp, LockWord::kReadBarrierStateShift, read_barrier_slow_path->GetEntryLabel());
2696
2697 // Fast-path copy.
2698 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2699 // poison/unpoison.
2700 vixl::aarch64::Label loop;
2701 __ Bind(&loop);
Roland Levillain0b671c02016-08-19 12:02:34 +01002702 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2703 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
Roland Levillain1663d162017-03-17 15:15:21 +00002704 __ Cmp(src_curr_addr, src_stop_addr);
2705 __ B(&loop, ne);
2706
2707 __ Bind(read_barrier_slow_path->GetExitLabel());
2708 } else {
2709 // Non read barrier code.
2710 // Compute base source address, base destination address, and end
2711 // source address for System.arraycopy* intrinsics in `src_base`,
2712 // `dst_base` and `src_end` respectively.
2713 GenSystemArrayCopyAddresses(masm,
2714 type,
2715 src,
2716 src_pos,
2717 dest,
2718 dest_pos,
2719 length,
2720 src_curr_addr,
2721 dst_curr_addr,
2722 src_stop_addr);
2723 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2724 // poison/unpoison.
2725 vixl::aarch64::Label loop;
2726 __ Bind(&loop);
2727 {
2728 Register tmp = temps.AcquireW();
2729 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2730 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
2731 }
2732 __ Cmp(src_curr_addr, src_stop_addr);
2733 __ B(&loop, ne);
Roland Levillain0b671c02016-08-19 12:02:34 +01002734 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002735 __ Bind(&done);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002736 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002737 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002738
donghui.baic2ec9ad2016-03-10 14:02:55 +08002739 // We only need one card marking on the destination array.
2740 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2741
Roland Levillain0b671c02016-08-19 12:02:34 +01002742 __ Bind(intrinsic_slow_path->GetExitLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002743}
2744
Anton Kirilova3ffea22016-04-07 17:02:37 +01002745static void GenIsInfinite(LocationSummary* locations,
2746 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002747 MacroAssembler* masm) {
Anton Kirilova3ffea22016-04-07 17:02:37 +01002748 Operand infinity;
2749 Register out;
2750
2751 if (is64bit) {
2752 infinity = kPositiveInfinityDouble;
2753 out = XRegisterFrom(locations->Out());
2754 } else {
2755 infinity = kPositiveInfinityFloat;
2756 out = WRegisterFrom(locations->Out());
2757 }
2758
Scott Wakeling97c72b72016-06-24 16:19:36 +01002759 const Register zero = vixl::aarch64::Assembler::AppropriateZeroRegFor(out);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002760
2761 MoveFPToInt(locations, is64bit, masm);
2762 __ Eor(out, out, infinity);
2763 // We don't care about the sign bit, so shift left.
2764 __ Cmp(zero, Operand(out, LSL, 1));
2765 __ Cset(out, eq);
2766}
2767
2768void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002769 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002770}
2771
2772void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2773 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
2774}
2775
2776void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002777 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002778}
2779
2780void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2781 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
2782}
2783
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002784void IntrinsicLocationsBuilderARM64::VisitIntegerValueOf(HInvoke* invoke) {
2785 InvokeRuntimeCallingConvention calling_convention;
2786 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2787 invoke,
2788 codegen_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 calling_convention.GetReturnLocation(DataType::Type::kReference),
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002790 Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2791}
2792
2793void IntrinsicCodeGeneratorARM64::VisitIntegerValueOf(HInvoke* invoke) {
2794 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2795 LocationSummary* locations = invoke->GetLocations();
2796 MacroAssembler* masm = GetVIXLAssembler();
2797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 Register out = RegisterFrom(locations->Out(), DataType::Type::kReference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002799 UseScratchRegisterScope temps(masm);
2800 Register temp = temps.AcquireW();
2801 InvokeRuntimeCallingConvention calling_convention;
2802 Register argument = calling_convention.GetRegisterAt(0);
2803 if (invoke->InputAt(0)->IsConstant()) {
2804 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2805 if (value >= info.low && value <= info.high) {
2806 // Just embed the j.l.Integer in the code.
2807 ScopedObjectAccess soa(Thread::Current());
2808 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2809 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2810 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
2811 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2812 } else {
2813 // Allocate and initialize a new j.l.Integer.
2814 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2815 // JIT object table.
2816 uint32_t address =
2817 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2818 __ Ldr(argument.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2819 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2820 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2821 __ Mov(temp.W(), value);
2822 __ Str(temp.W(), HeapOperand(out.W(), info.value_offset));
2823 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2824 // one.
2825 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2826 }
2827 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 Register in = RegisterFrom(locations->InAt(0), DataType::Type::kInt32);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002829 // Check bounds of our cache.
2830 __ Add(out.W(), in.W(), -info.low);
2831 __ Cmp(out.W(), info.high - info.low + 1);
2832 vixl::aarch64::Label allocate, done;
2833 __ B(&allocate, hs);
2834 // If the value is within the bounds, load the j.l.Integer directly from the array.
2835 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2836 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
2837 __ Ldr(temp.W(), codegen_->DeduplicateBootImageAddressLiteral(data_offset + address));
2838 MemOperand source = HeapOperand(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002839 temp, out.X(), LSL, DataType::SizeShift(DataType::Type::kReference));
2840 codegen_->Load(DataType::Type::kReference, out, source);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002841 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(out);
2842 __ B(&done);
2843 __ Bind(&allocate);
2844 // Otherwise allocate and initialize a new j.l.Integer.
2845 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2846 __ Ldr(argument.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2847 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2848 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2849 __ Str(in.W(), HeapOperand(out.W(), info.value_offset));
2850 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2851 // one.
2852 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2853 __ Bind(&done);
2854 }
2855}
2856
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002857void IntrinsicLocationsBuilderARM64::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002858 LocationSummary* locations =
2859 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002860 locations->SetOut(Location::RequiresRegister());
2861}
2862
2863void IntrinsicCodeGeneratorARM64::VisitThreadInterrupted(HInvoke* invoke) {
2864 MacroAssembler* masm = GetVIXLAssembler();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 Register out = RegisterFrom(invoke->GetLocations()->Out(), DataType::Type::kInt32);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002866 UseScratchRegisterScope temps(masm);
2867 Register temp = temps.AcquireX();
2868
2869 __ Add(temp, tr, Thread::InterruptedOffset<kArm64PointerSize>().Int32Value());
2870 __ Ldar(out.W(), MemOperand(temp));
2871
2872 vixl::aarch64::Label done;
2873 __ Cbz(out.W(), &done);
2874 __ Stlr(wzr, MemOperand(temp));
2875 __ Bind(&done);
2876}
2877
Hans Boehmc7b28de2018-03-09 17:05:28 -08002878void IntrinsicLocationsBuilderARM64::VisitReachabilityFence(HInvoke* invoke) {
2879 LocationSummary* locations =
2880 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
2881 locations->SetInAt(0, Location::Any());
2882}
2883
2884void IntrinsicCodeGeneratorARM64::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
2885
Vladimir Markod254f5c2017-06-02 15:18:36 +00002886UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Andreas Gampe878d58c2015-01-15 23:24:00 -08002887
Aart Bikff7d89c2016-11-07 08:49:28 -08002888UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOf);
2889UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002890UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferAppend);
2891UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferLength);
2892UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferToString);
2893UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppend);
2894UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderLength);
2895UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002896
Aart Bik0e54c012016-03-04 12:08:31 -08002897// 1.8.
2898UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
2899UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
2900UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
2901UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
2902UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002903
Aart Bik2f9fcc92016-03-01 15:16:54 -08002904UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01002905
2906#undef __
2907
Andreas Gampe878d58c2015-01-15 23:24:00 -08002908} // namespace arm64
2909} // namespace art