blob: da1874e205689b1914cf61c789358d06440ab43c [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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100115 void EmitNativeCode(CodeGenerator* codegen_in) override {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800116 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100148 const char* GetDescription() const override { return "IntrinsicSlowPathARM64"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100149
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100166 void EmitNativeCode(CodeGenerator* codegen_in) override {
Roland Levillain0b671c02016-08-19 12:02:34 +0100167 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100219 const char* GetDescription() const override { return "ReadBarrierSystemArrayCopySlowPathARM64"; }
Roland Levillain0b671c02016-08-19 12:02:34 +0100220
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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800275 MoveFPToInt(invoke->GetLocations(), /* is64bit= */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800276}
277void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800278 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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800289 MoveFPToInt(invoke->GetLocations(), /* is64bit= */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800290}
291void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800292 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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800621 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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800629 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));
Vladimir Marko248141f2018-08-10 10:40:07 +0100748 MacroAssembler* masm = codegen->GetVIXLAssembler();
749 // Piggy-back on the field load path using introspection for the Baker read barrier.
750 __ Add(temp, base, offset.W()); // Offset should not exceed 32 bits.
751 codegen->GenerateFieldLoadWithBakerReadBarrier(invoke,
752 trg_loc,
753 base,
754 MemOperand(temp.X()),
Andreas Gampe3db70682018-12-26 15:12:03 -0800755 /* needs_null_check= */ false,
Vladimir Marko248141f2018-08-10 10:40:07 +0100756 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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800761 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.
Vladimir Marko248141f2018-08-10 10:40:07 +0100785 // We need a temporary register for the read barrier load in order to use
786 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier().
787 locations->AddTemp(FixedTempLocation());
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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800816 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile= */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800817}
818void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800819 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile= */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800820}
821void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800822 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile= */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800823}
824void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800825 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile= */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800826}
827void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800828 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile= */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800829}
830void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800831 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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800899 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800914 /* is_volatile= */ false,
915 /* is_ordered= */ false,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000916 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800921 /* is_volatile= */ false,
922 /* is_ordered= */ true,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000923 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800928 /* is_volatile= */ true,
929 /* is_ordered= */ false,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000930 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800935 /* is_volatile= */ false,
936 /* is_ordered= */ false,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000937 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800942 /* is_volatile= */ false,
943 /* is_ordered= */ true,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000944 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800949 /* is_volatile= */ true,
950 /* is_ordered= */ false,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000951 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800956 /* is_volatile= */ false,
957 /* is_ordered= */ false,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000958 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800963 /* is_volatile= */ false,
964 /* is_ordered= */ true,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000965 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800970 /* is_volatile= */ true,
971 /* is_ordered= */ false,
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000972 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);
Vladimir Marko94796f82018-08-08 15:15:33 +0100987 if (can_call) {
988 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
989 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800990 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
991 locations->SetInAt(1, Location::RequiresRegister());
992 locations->SetInAt(2, Location::RequiresRegister());
993 locations->SetInAt(3, Location::RequiresRegister());
994 locations->SetInAt(4, Location::RequiresRegister());
995
Vladimir Marko94796f82018-08-08 15:15:33 +0100996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100997 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Marko94796f82018-08-08 15:15:33 +0100998 // We need two non-scratch temporary registers for (Baker) read barrier.
999 locations->AddTemp(Location::RequiresRegister());
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001000 locations->AddTemp(Location::RequiresRegister());
1001 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001002}
1003
Vladimir Marko94796f82018-08-08 15:15:33 +01001004class BakerReadBarrierCasSlowPathARM64 : public SlowPathCodeARM64 {
1005 public:
1006 explicit BakerReadBarrierCasSlowPathARM64(HInvoke* invoke)
1007 : SlowPathCodeARM64(invoke) {}
1008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001009 const char* GetDescription() const override { return "BakerReadBarrierCasSlowPathARM64"; }
Vladimir Marko94796f82018-08-08 15:15:33 +01001010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001011 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko94796f82018-08-08 15:15:33 +01001012 CodeGeneratorARM64* arm64_codegen = down_cast<CodeGeneratorARM64*>(codegen);
1013 Arm64Assembler* assembler = arm64_codegen->GetAssembler();
1014 MacroAssembler* masm = assembler->GetVIXLAssembler();
1015 __ Bind(GetEntryLabel());
1016
1017 // Get the locations.
1018 LocationSummary* locations = instruction_->GetLocations();
1019 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1020 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1021 Register expected = WRegisterFrom(locations->InAt(3)); // Expected.
1022 Register value = WRegisterFrom(locations->InAt(4)); // Value.
1023
1024 Register old_value = WRegisterFrom(locations->GetTemp(0)); // The old value from main path.
1025 Register marked = WRegisterFrom(locations->GetTemp(1)); // The marked old value.
1026
1027 // Mark the `old_value` from the main path and compare with `expected`. This clobbers the
1028 // `tmp_ptr` scratch register but we do not want to allocate another non-scratch temporary.
1029 arm64_codegen->GenerateUnsafeCasOldValueMovWithBakerReadBarrier(marked, old_value);
1030 __ Cmp(marked, expected);
1031 __ B(GetExitLabel(), ne); // If taken, Z=false indicates failure.
1032
1033 // The `old_value` we have read did not match `expected` (which is always a to-space reference)
1034 // but after the read barrier in GenerateUnsafeCasOldValueMovWithBakerReadBarrier() the marked
1035 // to-space value matched, so the `old_value` must be a from-space reference to the same
1036 // object. Do the same CAS loop as the main path but check for both `expected` and the unmarked
1037 // old value representing the to-space and from-space references for the same object.
1038
1039 UseScratchRegisterScope temps(masm);
1040 Register tmp_ptr = temps.AcquireX();
1041 Register tmp = temps.AcquireSameSizeAs(value);
1042
1043 // Recalculate the `tmp_ptr` clobbered above.
1044 __ Add(tmp_ptr, base.X(), Operand(offset));
1045
1046 // do {
1047 // tmp_value = [tmp_ptr];
1048 // } while ((tmp_value == expected || tmp == old_value) && failure([tmp_ptr] <- r_new_value));
1049 // result = (tmp_value == expected || tmp == old_value);
1050
1051 vixl::aarch64::Label loop_head;
1052 __ Bind(&loop_head);
1053 __ Ldaxr(tmp, MemOperand(tmp_ptr));
1054 assembler->MaybeUnpoisonHeapReference(tmp);
1055 __ Cmp(tmp, expected);
1056 __ Ccmp(tmp, old_value, ZFlag, ne);
1057 __ B(GetExitLabel(), ne); // If taken, Z=false indicates failure.
1058 assembler->MaybePoisonHeapReference(value);
1059 __ Stlxr(tmp.W(), value, MemOperand(tmp_ptr));
1060 assembler->MaybeUnpoisonHeapReference(value);
1061 __ Cbnz(tmp.W(), &loop_head);
1062
1063 // Z=true from the above CMP+CCMP indicates success.
1064 __ B(GetExitLabel());
1065 }
1066};
1067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001068static void GenCas(HInvoke* invoke, DataType::Type type, CodeGeneratorARM64* codegen) {
Vladimir Marko94796f82018-08-08 15:15:33 +01001069 Arm64Assembler* assembler = codegen->GetAssembler();
1070 MacroAssembler* masm = assembler->GetVIXLAssembler();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001071 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001072
Vladimir Marko94796f82018-08-08 15:15:33 +01001073 Register out = WRegisterFrom(locations->Out()); // Boolean result.
1074 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
1075 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
1076 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1077 Register value = RegisterFrom(locations->InAt(4), type); // Value.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001078
1079 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001080 if (type == DataType::Type::kReference) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001081 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001082 bool value_can_be_null = true; // TODO: Worth finding out this information?
1083 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001084 }
1085
1086 UseScratchRegisterScope temps(masm);
1087 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
Vladimir Marko94796f82018-08-08 15:15:33 +01001088 Register old_value; // Value in memory.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001089
Vladimir Marko94796f82018-08-08 15:15:33 +01001090 vixl::aarch64::Label exit_loop_label;
1091 vixl::aarch64::Label* exit_loop = &exit_loop_label;
1092 vixl::aarch64::Label* failure = &exit_loop_label;
1093
1094 if (kEmitCompilerReadBarrier && type == DataType::Type::kReference) {
1095 // The only read barrier implementation supporting the
1096 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1097 DCHECK(kUseBakerReadBarrier);
1098
1099 BakerReadBarrierCasSlowPathARM64* slow_path =
1100 new (codegen->GetScopedAllocator()) BakerReadBarrierCasSlowPathARM64(invoke);
1101 codegen->AddSlowPath(slow_path);
1102 exit_loop = slow_path->GetExitLabel();
1103 failure = slow_path->GetEntryLabel();
1104 // We need to store the `old_value` in a non-scratch register to make sure
1105 // the Baker read barrier in the slow path does not clobber it.
1106 old_value = WRegisterFrom(locations->GetTemp(0));
1107 } else {
1108 old_value = temps.AcquireSameSizeAs(value);
1109 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001110
1111 __ Add(tmp_ptr, base.X(), Operand(offset));
1112
1113 // do {
Vladimir Marko94796f82018-08-08 15:15:33 +01001114 // tmp_value = [tmp_ptr];
1115 // } while (tmp_value == expected && failure([tmp_ptr] <- r_new_value));
1116 // result = tmp_value == expected;
Andreas Gampe878d58c2015-01-15 23:24:00 -08001117
Vladimir Marko94796f82018-08-08 15:15:33 +01001118 vixl::aarch64::Label loop_head;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001119 __ Bind(&loop_head);
Vladimir Marko94796f82018-08-08 15:15:33 +01001120 __ Ldaxr(old_value, MemOperand(tmp_ptr));
1121 if (type == DataType::Type::kReference) {
1122 assembler->MaybeUnpoisonHeapReference(old_value);
Roland Levillain4d027112015-07-01 15:41:14 +01001123 }
Vladimir Marko94796f82018-08-08 15:15:33 +01001124 __ Cmp(old_value, expected);
1125 __ B(failure, ne);
1126 if (type == DataType::Type::kReference) {
1127 assembler->MaybePoisonHeapReference(value);
1128 }
1129 __ Stlxr(old_value.W(), value, MemOperand(tmp_ptr)); // Reuse `old_value` for STLXR result.
1130 if (type == DataType::Type::kReference) {
1131 assembler->MaybeUnpoisonHeapReference(value);
1132 }
1133 __ Cbnz(old_value.W(), &loop_head);
1134 __ Bind(exit_loop);
1135 __ Cset(out, eq);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001136}
1137
1138void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001139 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kInt32);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001140}
1141void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001142 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kInt64);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001143}
1144void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001145 // The only read barrier implementation supporting the
1146 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1147 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001148 return;
1149 }
1150
Vladimir Markoca6fff82017-10-03 14:49:14 +01001151 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kReference);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001152}
1153
1154void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001155 GenCas(invoke, DataType::Type::kInt32, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001156}
1157void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001158 GenCas(invoke, DataType::Type::kInt64, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001159}
1160void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001161 // The only read barrier implementation supporting the
1162 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1163 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001165 GenCas(invoke, DataType::Type::kReference, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001166}
1167
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001168void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001169 LocationSummary* locations =
1170 new (allocator_) LocationSummary(invoke,
1171 invoke->InputAt(1)->CanBeNull()
1172 ? LocationSummary::kCallOnSlowPath
1173 : LocationSummary::kNoCall,
1174 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001175 locations->SetInAt(0, Location::RequiresRegister());
1176 locations->SetInAt(1, Location::RequiresRegister());
1177 locations->AddTemp(Location::RequiresRegister());
1178 locations->AddTemp(Location::RequiresRegister());
1179 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001180 // Need temporary registers for String compression's feature.
1181 if (mirror::kUseStringCompression) {
1182 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001183 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001184 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001185}
1186
1187void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001188 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001189 LocationSummary* locations = invoke->GetLocations();
1190
Alexandre Rames2ea91532016-08-11 17:04:14 +01001191 Register str = InputRegisterAt(invoke, 0);
1192 Register arg = InputRegisterAt(invoke, 1);
1193 DCHECK(str.IsW());
1194 DCHECK(arg.IsW());
Scott Wakeling1f36f412016-04-21 11:13:45 +01001195 Register out = OutputRegister(invoke);
1196
1197 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1198 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1199 Register temp2 = WRegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001200 Register temp3;
jessicahandojo05765752016-09-09 19:01:32 -07001201 if (mirror::kUseStringCompression) {
1202 temp3 = WRegisterFrom(locations->GetTemp(3));
jessicahandojo05765752016-09-09 19:01:32 -07001203 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001204
Scott Wakeling97c72b72016-06-24 16:19:36 +01001205 vixl::aarch64::Label loop;
1206 vixl::aarch64::Label find_char_diff;
1207 vixl::aarch64::Label end;
jessicahandojo05765752016-09-09 19:01:32 -07001208 vixl::aarch64::Label different_compression;
Scott Wakeling1f36f412016-04-21 11:13:45 +01001209
1210 // Get offsets of count and value fields within a string object.
1211 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1212 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1213
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001214 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001215 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001216
Scott Wakeling1f36f412016-04-21 11:13:45 +01001217 // Take slow path and throw if input can be and is null.
1218 SlowPathCodeARM64* slow_path = nullptr;
1219 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1220 if (can_slow_path) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001221 slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001222 codegen_->AddSlowPath(slow_path);
1223 __ Cbz(arg, slow_path->GetEntryLabel());
1224 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001225
Scott Wakeling1f36f412016-04-21 11:13:45 +01001226 // Reference equality check, return 0 if same reference.
1227 __ Subs(out, str, arg);
1228 __ B(&end, eq);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001229
jessicahandojo05765752016-09-09 19:01:32 -07001230 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001231 // Load `count` fields of this and argument strings.
jessicahandojo05765752016-09-09 19:01:32 -07001232 __ Ldr(temp3, HeapOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001233 __ Ldr(temp2, HeapOperand(arg, count_offset));
jessicahandojo05765752016-09-09 19:01:32 -07001234 // Clean out compression flag from lengths.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001235 __ Lsr(temp0, temp3, 1u);
1236 __ Lsr(temp1, temp2, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001237 } else {
1238 // Load lengths of this and argument strings.
1239 __ Ldr(temp0, HeapOperand(str, count_offset));
1240 __ Ldr(temp1, HeapOperand(arg, count_offset));
1241 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001242 // out = length diff.
1243 __ Subs(out, temp0, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001244 // temp0 = min(len(str), len(arg)).
1245 __ Csel(temp0, temp1, temp0, ge);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001246 // Shorter string is empty?
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001247 __ Cbz(temp0, &end);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001248
jessicahandojo05765752016-09-09 19:01:32 -07001249 if (mirror::kUseStringCompression) {
1250 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001251 __ Eor(temp2, temp2, Operand(temp3));
1252 // Interleave with compression flag extraction which is needed for both paths
1253 // and also set flags which is needed only for the different compressions path.
1254 __ Ands(temp3.W(), temp3.W(), Operand(1));
1255 __ Tbnz(temp2, 0, &different_compression); // Does not use flags.
jessicahandojo05765752016-09-09 19:01:32 -07001256 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001257 // Store offset of string value in preparation for comparison loop.
1258 __ Mov(temp1, value_offset);
jessicahandojo05765752016-09-09 19:01:32 -07001259 if (mirror::kUseStringCompression) {
1260 // For string compression, calculate the number of bytes to compare (not chars).
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001261 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
1262 __ Lsl(temp0, temp0, temp3);
jessicahandojo05765752016-09-09 19:01:32 -07001263 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001264
1265 UseScratchRegisterScope scratch_scope(masm);
1266 Register temp4 = scratch_scope.AcquireX();
1267
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001268 // Assertions that must hold in order to compare strings 8 bytes at a time.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001269 DCHECK_ALIGNED(value_offset, 8);
1270 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1271
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001273 DCHECK_EQ(char_size, 2u);
1274
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001275 // Promote temp2 to an X reg, ready for LDR.
1276 temp2 = temp2.X();
Scott Wakeling1f36f412016-04-21 11:13:45 +01001277
1278 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1279 __ Bind(&loop);
Alexandre Rames2ea91532016-08-11 17:04:14 +01001280 __ Ldr(temp4, MemOperand(str.X(), temp1.X()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001281 __ Ldr(temp2, MemOperand(arg.X(), temp1.X()));
1282 __ Cmp(temp4, temp2);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001283 __ B(ne, &find_char_diff);
1284 __ Add(temp1, temp1, char_size * 4);
jessicahandojo05765752016-09-09 19:01:32 -07001285 // With string compression, we have compared 8 bytes, otherwise 4 chars.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001286 __ Subs(temp0, temp0, (mirror::kUseStringCompression) ? 8 : 4);
1287 __ B(&loop, hi);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001288 __ B(&end);
1289
1290 // Promote temp1 to an X reg, ready for EOR.
1291 temp1 = temp1.X();
1292
jessicahandojo05765752016-09-09 19:01:32 -07001293 // Find the single character difference.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001294 __ Bind(&find_char_diff);
1295 // Get the bit position of the first character that differs.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001296 __ Eor(temp1, temp2, temp4);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001297 __ Rbit(temp1, temp1);
1298 __ Clz(temp1, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001299
jessicahandojo05765752016-09-09 19:01:32 -07001300 // If the number of chars remaining <= the index where the difference occurs (0-3), then
Scott Wakeling1f36f412016-04-21 11:13:45 +01001301 // the difference occurs outside the remaining string data, so just return length diff (out).
jessicahandojo05765752016-09-09 19:01:32 -07001302 // Unlike ARM, we're doing the comparison in one go here, without the subtraction at the
1303 // find_char_diff_2nd_cmp path, so it doesn't matter whether the comparison is signed or
1304 // unsigned when string compression is disabled.
1305 // When it's enabled, the comparison must be unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001306 __ Cmp(temp0, Operand(temp1.W(), LSR, (mirror::kUseStringCompression) ? 3 : 4));
jessicahandojo05765752016-09-09 19:01:32 -07001307 __ B(ls, &end);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001308
Scott Wakeling1f36f412016-04-21 11:13:45 +01001309 // Extract the characters and calculate the difference.
jessicahandojo05765752016-09-09 19:01:32 -07001310 if (mirror:: kUseStringCompression) {
jessicahandojo05765752016-09-09 19:01:32 -07001311 __ Bic(temp1, temp1, 0x7);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001312 __ Bic(temp1, temp1, Operand(temp3.X(), LSL, 3u));
1313 } else {
1314 __ Bic(temp1, temp1, 0xf);
jessicahandojo05765752016-09-09 19:01:32 -07001315 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001316 __ Lsr(temp2, temp2, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001317 __ Lsr(temp4, temp4, temp1);
jessicahandojo05765752016-09-09 19:01:32 -07001318 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001319 // Prioritize the case of compressed strings and calculate such result first.
1320 __ Uxtb(temp1, temp4);
1321 __ Sub(out, temp1.W(), Operand(temp2.W(), UXTB));
1322 __ Tbz(temp3, 0u, &end); // If actually compressed, we're done.
jessicahandojo05765752016-09-09 19:01:32 -07001323 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001324 __ Uxth(temp4, temp4);
1325 __ Sub(out, temp4.W(), Operand(temp2.W(), UXTH));
jessicahandojo05765752016-09-09 19:01:32 -07001326
1327 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001328 __ B(&end);
1329 __ Bind(&different_compression);
1330
1331 // Comparison for different compression style.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001332 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo05765752016-09-09 19:01:32 -07001333 DCHECK_EQ(c_char_size, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001334 temp1 = temp1.W();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001335 temp2 = temp2.W();
1336 temp4 = temp4.W();
jessicahandojo05765752016-09-09 19:01:32 -07001337
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001338 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
1339 // Note that flags have been set by the `str` compression flag extraction to `temp3`
1340 // before branching to the `different_compression` label.
1341 __ Csel(temp1, str, arg, eq); // Pointer to the compressed string.
1342 __ Csel(temp2, str, arg, ne); // Pointer to the uncompressed string.
jessicahandojo05765752016-09-09 19:01:32 -07001343
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001344 // We want to free up the temp3, currently holding `str` compression flag, for comparison.
1345 // So, we move it to the bottom bit of the iteration count `temp0` which we then need to treat
1346 // as unsigned. Start by freeing the bit with a LSL and continue further down by a SUB which
1347 // will allow `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
1348 __ Lsl(temp0, temp0, 1u);
1349
1350 // Adjust temp1 and temp2 from string pointers to data pointers.
1351 __ Add(temp1, temp1, Operand(value_offset));
1352 __ Add(temp2, temp2, Operand(value_offset));
1353
1354 // Complete the move of the compression flag.
1355 __ Sub(temp0, temp0, Operand(temp3));
1356
1357 vixl::aarch64::Label different_compression_loop;
1358 vixl::aarch64::Label different_compression_diff;
1359
1360 __ Bind(&different_compression_loop);
1361 __ Ldrb(temp4, MemOperand(temp1.X(), c_char_size, PostIndex));
1362 __ Ldrh(temp3, MemOperand(temp2.X(), char_size, PostIndex));
1363 __ Subs(temp4, temp4, Operand(temp3));
1364 __ B(&different_compression_diff, ne);
1365 __ Subs(temp0, temp0, 2);
1366 __ B(&different_compression_loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001367 __ B(&end);
1368
1369 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001370 __ Bind(&different_compression_diff);
1371 __ Tst(temp0, Operand(1));
1372 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1373 "Expecting 0=compressed, 1=uncompressed");
1374 __ Cneg(out, temp4, ne);
jessicahandojo05765752016-09-09 19:01:32 -07001375 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001376
1377 __ Bind(&end);
1378
1379 if (can_slow_path) {
1380 __ Bind(slow_path->GetExitLabel());
1381 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001382}
1383
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001384// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1385// The normal loop plus the pre-header is 9 instructions without string compression and 12
1386// instructions with string compression. We can compare up to 8 bytes in 4 instructions
1387// (LDR+LDR+CMP+BNE) and up to 16 bytes in 5 instructions (LDP+LDP+CMP+CCMP+BNE). Allow up
1388// to 10 instructions for the unrolled loop.
1389constexpr size_t kShortConstStringEqualsCutoffInBytes = 32;
1390
1391static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1392 if (candidate->IsLoadString()) {
1393 HLoadString* load_string = candidate->AsLoadString();
1394 const DexFile& dex_file = load_string->GetDexFile();
1395 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1396 }
1397 return nullptr;
1398}
1399
Agi Csakiea34b402015-08-13 17:51:19 -07001400void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001401 LocationSummary* locations =
1402 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakiea34b402015-08-13 17:51:19 -07001403 locations->SetInAt(0, Location::RequiresRegister());
1404 locations->SetInAt(1, Location::RequiresRegister());
Agi Csakiea34b402015-08-13 17:51:19 -07001405
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001406 // For the generic implementation and for long const strings we need a temporary.
1407 // We do not need it for short const strings, up to 8 bytes, see code generation below.
1408 uint32_t const_string_length = 0u;
1409 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1410 if (const_string == nullptr) {
1411 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1412 }
1413 bool is_compressed =
1414 mirror::kUseStringCompression &&
1415 const_string != nullptr &&
1416 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1417 if (const_string == nullptr || const_string_length > (is_compressed ? 8u : 4u)) {
1418 locations->AddTemp(Location::RequiresRegister());
1419 }
1420
1421 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1422 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1423 // Then we shall need an extra temporary register instead of the output register.
Agi Csakiea34b402015-08-13 17:51:19 -07001424 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1425}
1426
1427void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001428 MacroAssembler* masm = GetVIXLAssembler();
Agi Csakiea34b402015-08-13 17:51:19 -07001429 LocationSummary* locations = invoke->GetLocations();
1430
1431 Register str = WRegisterFrom(locations->InAt(0));
1432 Register arg = WRegisterFrom(locations->InAt(1));
1433 Register out = XRegisterFrom(locations->Out());
1434
1435 UseScratchRegisterScope scratch_scope(masm);
1436 Register temp = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001437 Register temp1 = scratch_scope.AcquireW();
Agi Csakiea34b402015-08-13 17:51:19 -07001438
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001439 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001440 vixl::aarch64::Label end;
1441 vixl::aarch64::Label return_true;
1442 vixl::aarch64::Label return_false;
Agi Csakiea34b402015-08-13 17:51:19 -07001443
1444 // Get offsets of count, value, and class fields within a string object.
1445 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1446 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1447 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1448
1449 // Note that the null check must have been done earlier.
1450 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1451
Vladimir Marko53b52002016-05-24 19:30:45 +01001452 StringEqualsOptimizations optimizations(invoke);
1453 if (!optimizations.GetArgumentNotNull()) {
1454 // Check if input is null, return false if it is.
1455 __ Cbz(arg, &return_false);
1456 }
Agi Csakiea34b402015-08-13 17:51:19 -07001457
1458 // Reference equality check, return true if same reference.
1459 __ Cmp(str, arg);
1460 __ B(&return_true, eq);
1461
Vladimir Marko53b52002016-05-24 19:30:45 +01001462 if (!optimizations.GetArgumentIsString()) {
1463 // Instanceof check for the argument by comparing class fields.
1464 // All string objects must have the same type since String cannot be subclassed.
1465 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1466 // If the argument is a string object, its class field must be equal to receiver's class field.
Roland Levillain1d775d22018-09-07 13:56:57 +01001467 //
1468 // As the String class is expected to be non-movable, we can read the class
1469 // field from String.equals' arguments without read barriers.
1470 AssertNonMovableStringClass();
1471 // /* HeapReference<Class> */ temp = str->klass_
Vladimir Marko53b52002016-05-24 19:30:45 +01001472 __ Ldr(temp, MemOperand(str.X(), class_offset));
Roland Levillain1d775d22018-09-07 13:56:57 +01001473 // /* HeapReference<Class> */ temp1 = arg->klass_
Vladimir Marko53b52002016-05-24 19:30:45 +01001474 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
Roland Levillain1d775d22018-09-07 13:56:57 +01001475 // Also, because we use the previously loaded class references only in the
1476 // following comparison, we don't need to unpoison them.
Vladimir Marko53b52002016-05-24 19:30:45 +01001477 __ Cmp(temp, temp1);
1478 __ B(&return_false, ne);
1479 }
Agi Csakiea34b402015-08-13 17:51:19 -07001480
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001481 // Check if one of the inputs is a const string. Do not special-case both strings
1482 // being const, such cases should be handled by constant folding if needed.
1483 uint32_t const_string_length = 0u;
1484 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1485 if (const_string == nullptr) {
1486 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1487 if (const_string != nullptr) {
1488 std::swap(str, arg); // Make sure the const string is in `str`.
1489 }
1490 }
1491 bool is_compressed =
1492 mirror::kUseStringCompression &&
1493 const_string != nullptr &&
1494 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1495
1496 if (const_string != nullptr) {
1497 // Load `count` field of the argument string and check if it matches the const string.
1498 // Also compares the compression style, if differs return false.
1499 __ Ldr(temp, MemOperand(arg.X(), count_offset));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001500 // Temporarily release temp1 as we may not be able to embed the flagged count in CMP immediate.
1501 scratch_scope.Release(temp1);
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001502 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001503 temp1 = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001504 __ B(&return_false, ne);
1505 } else {
1506 // Load `count` fields of this and argument strings.
1507 __ Ldr(temp, MemOperand(str.X(), count_offset));
1508 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1509 // Check if `count` fields are equal, return false if they're not.
1510 // Also compares the compression style, if differs return false.
1511 __ Cmp(temp, temp1);
1512 __ B(&return_false, ne);
1513 }
Agi Csakiea34b402015-08-13 17:51:19 -07001514
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001515 // Assertions that must hold in order to compare strings 8 bytes at a time.
Vladimir Marko984519c2017-08-23 10:45:29 +01001516 // Ok to do this because strings are zero-padded to kObjectAlignment.
Agi Csakiea34b402015-08-13 17:51:19 -07001517 DCHECK_ALIGNED(value_offset, 8);
1518 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1519
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001520 if (const_string != nullptr &&
Vladimir Marko984519c2017-08-23 10:45:29 +01001521 const_string_length <= (is_compressed ? kShortConstStringEqualsCutoffInBytes
1522 : kShortConstStringEqualsCutoffInBytes / 2u)) {
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001523 // Load and compare the contents. Though we know the contents of the short const string
1524 // at compile time, materializing constants may be more code than loading from memory.
1525 int32_t offset = value_offset;
1526 size_t remaining_bytes =
1527 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 8u);
1528 temp = temp.X();
1529 temp1 = temp1.X();
Vladimir Marko984519c2017-08-23 10:45:29 +01001530 while (remaining_bytes > sizeof(uint64_t)) {
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001531 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1532 __ Ldp(temp, temp1, MemOperand(str.X(), offset));
1533 __ Ldp(temp2, out, MemOperand(arg.X(), offset));
1534 __ Cmp(temp, temp2);
1535 __ Ccmp(temp1, out, NoFlag, eq);
1536 __ B(&return_false, ne);
1537 offset += 2u * sizeof(uint64_t);
1538 remaining_bytes -= 2u * sizeof(uint64_t);
1539 }
1540 if (remaining_bytes != 0u) {
1541 __ Ldr(temp, MemOperand(str.X(), offset));
1542 __ Ldr(temp1, MemOperand(arg.X(), offset));
1543 __ Cmp(temp, temp1);
1544 __ B(&return_false, ne);
1545 }
1546 } else {
1547 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1548 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1549 "Expecting 0=compressed, 1=uncompressed");
1550 __ Cbz(temp, &return_true);
1551
1552 if (mirror::kUseStringCompression) {
1553 // For string compression, calculate the number of bytes to compare (not chars).
1554 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1555 __ And(temp1, temp, Operand(1)); // Extract compression flag.
1556 __ Lsr(temp, temp, 1u); // Extract length.
1557 __ Lsl(temp, temp, temp1); // Calculate number of bytes to compare.
1558 }
1559
1560 // Store offset of string value in preparation for comparison loop
1561 __ Mov(temp1, value_offset);
1562
1563 temp1 = temp1.X();
1564 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1565 // Loop to compare strings 8 bytes at a time starting at the front of the string.
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001566 __ Bind(&loop);
1567 __ Ldr(out, MemOperand(str.X(), temp1));
1568 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1569 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1570 __ Cmp(out, temp2);
1571 __ B(&return_false, ne);
1572 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1573 __ Sub(temp, temp, Operand(mirror::kUseStringCompression ? 8 : 4), SetFlags);
1574 __ B(&loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001575 }
1576
Agi Csakiea34b402015-08-13 17:51:19 -07001577 // Return true and exit the function.
1578 // If loop does not result in returning false, we return true.
1579 __ Bind(&return_true);
1580 __ Mov(out, 1);
1581 __ B(&end);
1582
1583 // Return false and exit the function.
1584 __ Bind(&return_false);
1585 __ Mov(out, 0);
1586 __ Bind(&end);
1587}
1588
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001589static void GenerateVisitStringIndexOf(HInvoke* invoke,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001590 MacroAssembler* masm,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001591 CodeGeneratorARM64* codegen,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001592 bool start_at_zero) {
1593 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001594
1595 // Note that the null check must have been done earlier.
1596 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1597
1598 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001599 // 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 -07001600 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001601 HInstruction* code_point = invoke->InputAt(1);
1602 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001603 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001604 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1605 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001606 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001607 codegen->AddSlowPath(slow_path);
1608 __ B(slow_path->GetEntryLabel());
1609 __ Bind(slow_path->GetExitLabel());
1610 return;
1611 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001612 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001613 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001614 __ Tst(char_reg, 0xFFFF0000);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001615 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001616 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001617 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001618 }
1619
1620 if (start_at_zero) {
1621 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001622 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001623 __ Mov(tmp_reg, 0);
1624 }
1625
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001626 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Roland Levillain42ad2882016-02-29 18:26:54 +00001627 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001628
1629 if (slow_path != nullptr) {
1630 __ Bind(slow_path->GetExitLabel());
1631 }
1632}
1633
1634void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001635 LocationSummary* locations = new (allocator_) LocationSummary(
1636 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001637 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1638 // best to align the inputs accordingly.
1639 InvokeRuntimeCallingConvention calling_convention;
1640 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1641 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001642 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt32));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001643
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001644 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001645 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1646}
1647
1648void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001649 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, /* start_at_zero= */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001650}
1651
1652void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001653 LocationSummary* locations = new (allocator_) LocationSummary(
1654 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001655 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1656 // best to align the inputs accordingly.
1657 InvokeRuntimeCallingConvention calling_convention;
1658 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1659 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1660 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001661 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt32));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001662}
1663
1664void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001665 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, /* start_at_zero= */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001666}
1667
Jeff Hao848f70a2014-01-15 13:49:50 -08001668void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001669 LocationSummary* locations = new (allocator_) LocationSummary(
1670 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001671 InvokeRuntimeCallingConvention calling_convention;
1672 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1673 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1674 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1675 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001676 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001677}
1678
1679void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001680 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001681 LocationSummary* locations = invoke->GetLocations();
1682
1683 Register byte_array = WRegisterFrom(locations->InAt(0));
1684 __ Cmp(byte_array, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001685 SlowPathCodeARM64* slow_path =
1686 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001687 codegen_->AddSlowPath(slow_path);
1688 __ B(eq, slow_path->GetEntryLabel());
1689
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001690 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001691 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001692 __ Bind(slow_path->GetExitLabel());
1693}
1694
1695void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001696 LocationSummary* locations =
1697 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001698 InvokeRuntimeCallingConvention calling_convention;
1699 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1700 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1701 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001703}
1704
1705void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001706 // No need to emit code checking whether `locations->InAt(2)` is a null
1707 // pointer, as callers of the native method
1708 //
1709 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1710 //
1711 // all include a null check on `data` before calling that method.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001712 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001713 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001714}
1715
1716void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001717 LocationSummary* locations = new (allocator_) LocationSummary(
1718 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001719 InvokeRuntimeCallingConvention calling_convention;
1720 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001721 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001722}
1723
1724void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001725 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001726 LocationSummary* locations = invoke->GetLocations();
1727
1728 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1729 __ Cmp(string_to_copy, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001730 SlowPathCodeARM64* slow_path =
1731 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001732 codegen_->AddSlowPath(slow_path);
1733 __ B(eq, slow_path->GetEntryLabel());
1734
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001735 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001736 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001737 __ Bind(slow_path->GetExitLabel());
1738}
1739
Vladimir Markoca6fff82017-10-03 14:49:14 +01001740static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001741 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001742 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1743 DCHECK(DataType::IsFloatingPointType(invoke->GetType()));
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001744
Vladimir Markoca6fff82017-10-03 14:49:14 +01001745 LocationSummary* const locations =
1746 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001747 InvokeRuntimeCallingConvention calling_convention;
1748
1749 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1750 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1751}
1752
Vladimir Markoca6fff82017-10-03 14:49:14 +01001753static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001754 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1756 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1757 DCHECK(DataType::IsFloatingPointType(invoke->GetType()));
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001758
Vladimir Markoca6fff82017-10-03 14:49:14 +01001759 LocationSummary* const locations =
1760 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001761 InvokeRuntimeCallingConvention calling_convention;
1762
1763 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1764 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1765 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1766}
1767
1768static void GenFPToFPCall(HInvoke* invoke,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001769 CodeGeneratorARM64* codegen,
1770 QuickEntrypointEnum entry) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001771 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001772}
1773
1774void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001775 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001776}
1777
1778void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001779 GenFPToFPCall(invoke, codegen_, kQuickCos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001780}
1781
1782void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001783 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001784}
1785
1786void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001787 GenFPToFPCall(invoke, codegen_, kQuickSin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001788}
1789
1790void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001791 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001792}
1793
1794void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001795 GenFPToFPCall(invoke, codegen_, kQuickAcos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001796}
1797
1798void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001799 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001800}
1801
1802void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001803 GenFPToFPCall(invoke, codegen_, kQuickAsin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001804}
1805
1806void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001807 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001808}
1809
1810void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001811 GenFPToFPCall(invoke, codegen_, kQuickAtan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001812}
1813
1814void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001815 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001816}
1817
1818void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001819 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001820}
1821
1822void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001823 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001824}
1825
1826void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001827 GenFPToFPCall(invoke, codegen_, kQuickCosh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001828}
1829
1830void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001831 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001832}
1833
1834void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001835 GenFPToFPCall(invoke, codegen_, kQuickExp);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001836}
1837
1838void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001839 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001840}
1841
1842void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001843 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001844}
1845
1846void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001847 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001848}
1849
1850void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001851 GenFPToFPCall(invoke, codegen_, kQuickLog);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001852}
1853
1854void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001855 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001856}
1857
1858void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001859 GenFPToFPCall(invoke, codegen_, kQuickLog10);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001860}
1861
1862void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001863 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001864}
1865
1866void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001867 GenFPToFPCall(invoke, codegen_, kQuickSinh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001868}
1869
1870void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001871 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001872}
1873
1874void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001875 GenFPToFPCall(invoke, codegen_, kQuickTan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001876}
1877
1878void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001879 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001880}
1881
1882void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001883 GenFPToFPCall(invoke, codegen_, kQuickTanh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001884}
1885
1886void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001887 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001888}
1889
1890void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001891 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001892}
1893
Vladimir Marko4d179872018-01-19 14:50:10 +00001894void IntrinsicLocationsBuilderARM64::VisitMathPow(HInvoke* invoke) {
1895 CreateFPFPToFPCallLocations(allocator_, invoke);
1896}
1897
1898void IntrinsicCodeGeneratorARM64::VisitMathPow(HInvoke* invoke) {
1899 GenFPToFPCall(invoke, codegen_, kQuickPow);
1900}
1901
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001902void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001903 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001904}
1905
1906void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001907 GenFPToFPCall(invoke, codegen_, kQuickHypot);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001908}
1909
1910void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001911 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001912}
1913
1914void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001915 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001916}
1917
Tim Zhang25abd6c2016-01-19 23:39:24 +08001918void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001919 LocationSummary* locations =
1920 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001921 locations->SetInAt(0, Location::RequiresRegister());
1922 locations->SetInAt(1, Location::RequiresRegister());
1923 locations->SetInAt(2, Location::RequiresRegister());
1924 locations->SetInAt(3, Location::RequiresRegister());
1925 locations->SetInAt(4, Location::RequiresRegister());
1926
1927 locations->AddTemp(Location::RequiresRegister());
1928 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01001929 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08001930}
1931
1932void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001933 MacroAssembler* masm = GetVIXLAssembler();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001934 LocationSummary* locations = invoke->GetLocations();
1935
1936 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001937 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001938 DCHECK_EQ(char_size, 2u);
1939
1940 // Location of data in char array buffer.
1941 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1942
1943 // Location of char array data in string.
1944 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1945
1946 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1947 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
1948 Register srcObj = XRegisterFrom(locations->InAt(0));
1949 Register srcBegin = XRegisterFrom(locations->InAt(1));
1950 Register srcEnd = XRegisterFrom(locations->InAt(2));
1951 Register dstObj = XRegisterFrom(locations->InAt(3));
1952 Register dstBegin = XRegisterFrom(locations->InAt(4));
1953
1954 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001955 Register num_chr = XRegisterFrom(locations->GetTemp(1));
1956 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08001957
1958 UseScratchRegisterScope temps(masm);
1959 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01001960 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001961
jessicahandojo05765752016-09-09 19:01:32 -07001962 vixl::aarch64::Label done;
1963 vixl::aarch64::Label compressed_string_loop;
1964 __ Sub(num_chr, srcEnd, srcBegin);
1965 // Early out for valid zero-length retrievals.
1966 __ Cbz(num_chr, &done);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001967
Scott Wakelingdf109d92016-04-22 11:35:56 +01001968 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08001969 __ Add(dst_ptr, dstObj, Operand(data_offset));
1970 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
1971
jessicahandojo05765752016-09-09 19:01:32 -07001972 // src address to copy from.
1973 __ Add(src_ptr, srcObj, Operand(value_offset));
1974 vixl::aarch64::Label compressed_string_preloop;
1975 if (mirror::kUseStringCompression) {
1976 // Location of count in string.
1977 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1978 // String's length.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001979 __ Ldr(tmp2, MemOperand(srcObj, count_offset));
1980 __ Tbz(tmp2, 0, &compressed_string_preloop);
jessicahandojo05765752016-09-09 19:01:32 -07001981 }
1982 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001983
Tim Zhang25abd6c2016-01-19 23:39:24 +08001984 // Do the copy.
Scott Wakeling97c72b72016-06-24 16:19:36 +01001985 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001986 vixl::aarch64::Label remainder;
Scott Wakelingdf109d92016-04-22 11:35:56 +01001987
Scott Wakelingdf109d92016-04-22 11:35:56 +01001988 // Save repairing the value of num_chr on the < 8 character path.
1989 __ Subs(tmp1, num_chr, 8);
1990 __ B(lt, &remainder);
1991
1992 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
1993 __ Mov(num_chr, tmp1);
1994
1995 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
1996 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08001997 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01001998 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01001999 __ Subs(num_chr, num_chr, 8);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002000 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002001 __ B(ge, &loop);
2002
2003 __ Adds(num_chr, num_chr, 8);
2004 __ B(eq, &done);
2005
2006 // Main loop for < 8 character case and remainder handling. Loads and stores one
2007 // 16-bit Java character at a time.
2008 __ Bind(&remainder);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002009 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002010 __ Subs(num_chr, num_chr, 1);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002011 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002012 __ B(gt, &remainder);
jessicahandojo05765752016-09-09 19:01:32 -07002013 __ B(&done);
2014
2015 if (mirror::kUseStringCompression) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002016 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo05765752016-09-09 19:01:32 -07002017 DCHECK_EQ(c_char_size, 1u);
2018 __ Bind(&compressed_string_preloop);
2019 __ Add(src_ptr, src_ptr, Operand(srcBegin));
2020 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2021 __ Bind(&compressed_string_loop);
2022 __ Ldrb(tmp1, MemOperand(src_ptr, c_char_size, PostIndex));
2023 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
2024 __ Subs(num_chr, num_chr, Operand(1));
2025 __ B(gt, &compressed_string_loop);
2026 }
Scott Wakelingdf109d92016-04-22 11:35:56 +01002027
Tim Zhang25abd6c2016-01-19 23:39:24 +08002028 __ Bind(&done);
2029}
2030
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002031// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
2032// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002033static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002034
2035static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
2036 uint32_t at,
2037 HInstruction* input) {
2038 HIntConstant* const_input = input->AsIntConstant();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002039 if (const_input != nullptr && !vixl::aarch64::Assembler::IsImmAddSub(const_input->GetValue())) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002040 locations->SetInAt(at, Location::RequiresRegister());
2041 } else {
2042 locations->SetInAt(at, Location::RegisterOrConstant(input));
2043 }
2044}
2045
2046void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
2047 // Check to see if we have known failures that will cause us to have to bail out
2048 // to the runtime, and just generate the runtime call directly.
2049 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2050 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
2051
2052 // The positions must be non-negative.
2053 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2054 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
2055 // We will have to fail anyways.
2056 return;
2057 }
2058
2059 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
2060 // native implementation.
2061 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2062 if (length != nullptr) {
2063 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002064 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002065 // Just call as normal.
2066 return;
2067 }
2068 }
2069
Vladimir Markoca6fff82017-10-03 14:49:14 +01002070 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
2071 LocationSummary* locations =
2072 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002073 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
2074 locations->SetInAt(0, Location::RequiresRegister());
2075 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2076 locations->SetInAt(2, Location::RequiresRegister());
2077 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2078 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2079
2080 locations->AddTemp(Location::RequiresRegister());
2081 locations->AddTemp(Location::RequiresRegister());
2082 locations->AddTemp(Location::RequiresRegister());
2083}
2084
Scott Wakeling97c72b72016-06-24 16:19:36 +01002085static void CheckSystemArrayCopyPosition(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002086 const Location& pos,
2087 const Register& input,
2088 const Location& length,
2089 SlowPathCodeARM64* slow_path,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002090 const Register& temp,
2091 bool length_is_input_length = false) {
2092 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
2093 if (pos.IsConstant()) {
2094 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
2095 if (pos_const == 0) {
2096 if (!length_is_input_length) {
2097 // Check that length(input) >= length.
2098 __ Ldr(temp, MemOperand(input, length_offset));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 __ Cmp(temp, OperandFrom(length, DataType::Type::kInt32));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002100 __ B(slow_path->GetEntryLabel(), lt);
2101 }
2102 } else {
2103 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002104 __ Ldr(temp, MemOperand(input, length_offset));
2105 __ Subs(temp, temp, pos_const);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002106 __ B(slow_path->GetEntryLabel(), lt);
2107
2108 // Check that (length(input) - pos) >= length.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 __ Cmp(temp, OperandFrom(length, DataType::Type::kInt32));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002110 __ B(slow_path->GetEntryLabel(), lt);
2111 }
2112 } else if (length_is_input_length) {
2113 // The only way the copy can succeed is if pos is zero.
2114 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
2115 } else {
2116 // Check that pos >= 0.
2117 Register pos_reg = WRegisterFrom(pos);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002118 __ Tbnz(pos_reg, pos_reg.GetSizeInBits() - 1, slow_path->GetEntryLabel());
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002119
2120 // Check that pos <= length(input) && (length(input) - pos) >= length.
2121 __ Ldr(temp, MemOperand(input, length_offset));
2122 __ Subs(temp, temp, pos_reg);
2123 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 __ Ccmp(temp, OperandFrom(length, DataType::Type::kInt32), NFlag, ge);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002125 __ B(slow_path->GetEntryLabel(), lt);
2126 }
2127}
2128
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002129// Compute base source address, base destination address, and end
2130// source address for System.arraycopy* intrinsics in `src_base`,
2131// `dst_base` and `src_end` respectively.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002132static void GenSystemArrayCopyAddresses(MacroAssembler* masm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 DataType::Type type,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002134 const Register& src,
2135 const Location& src_pos,
2136 const Register& dst,
2137 const Location& dst_pos,
2138 const Location& copy_length,
2139 const Register& src_base,
2140 const Register& dst_base,
2141 const Register& src_end) {
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002142 // This routine is used by the SystemArrayCopy and the SystemArrayCopyChar intrinsics.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 DCHECK(type == DataType::Type::kReference || type == DataType::Type::kUint16)
Roland Levillainebea3d22016-04-12 15:42:57 +01002144 << "Unexpected element type: " << type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002145 const int32_t element_size = DataType::Size(type);
2146 const int32_t element_size_shift = DataType::SizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002147 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002148
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002149 if (src_pos.IsConstant()) {
2150 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002151 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002152 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002153 __ Add(src_base, src, data_offset);
2154 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002155 }
2156
2157 if (dst_pos.IsConstant()) {
2158 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002159 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002160 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002161 __ Add(dst_base, dst, data_offset);
2162 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002163 }
2164
2165 if (copy_length.IsConstant()) {
2166 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002167 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002168 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002169 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002170 }
2171}
2172
2173void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002174 MacroAssembler* masm = GetVIXLAssembler();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002175 LocationSummary* locations = invoke->GetLocations();
2176 Register src = XRegisterFrom(locations->InAt(0));
2177 Location src_pos = locations->InAt(1);
2178 Register dst = XRegisterFrom(locations->InAt(2));
2179 Location dst_pos = locations->InAt(3);
2180 Location length = locations->InAt(4);
2181
Vladimir Marko174b2e22017-10-12 13:34:49 +01002182 SlowPathCodeARM64* slow_path =
2183 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002184 codegen_->AddSlowPath(slow_path);
2185
2186 // If source and destination are the same, take the slow path. Overlapping copy regions must be
2187 // copied in reverse and we can't know in all cases if it's needed.
2188 __ Cmp(src, dst);
2189 __ B(slow_path->GetEntryLabel(), eq);
2190
2191 // Bail out if the source is null.
2192 __ Cbz(src, slow_path->GetEntryLabel());
2193
2194 // Bail out if the destination is null.
2195 __ Cbz(dst, slow_path->GetEntryLabel());
2196
2197 if (!length.IsConstant()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002198 // Merge the following two comparisons into one:
2199 // If the length is negative, bail out (delegate to libcore's native implementation).
2200 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002201 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002202 __ B(slow_path->GetEntryLabel(), hi);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002203 } else {
2204 // We have already checked in the LocationsBuilder for the constant case.
2205 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
2206 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
2207 }
2208
2209 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
2210 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
2211 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
2212
2213 CheckSystemArrayCopyPosition(masm,
2214 src_pos,
2215 src,
2216 length,
2217 slow_path,
2218 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002219 false);
2220
2221 CheckSystemArrayCopyPosition(masm,
2222 dst_pos,
2223 dst,
2224 length,
2225 slow_path,
2226 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002227 false);
2228
2229 src_curr_addr = src_curr_addr.X();
2230 dst_curr_addr = dst_curr_addr.X();
2231 src_stop_addr = src_stop_addr.X();
2232
2233 GenSystemArrayCopyAddresses(masm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 DataType::Type::kUint16,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002235 src,
2236 src_pos,
2237 dst,
2238 dst_pos,
2239 length,
2240 src_curr_addr,
2241 dst_curr_addr,
2242 src_stop_addr);
2243
2244 // Iterate over the arrays and do a raw copy of the chars.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 const int32_t char_size = DataType::Size(DataType::Type::kUint16);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002246 UseScratchRegisterScope temps(masm);
2247 Register tmp = temps.AcquireW();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002248 vixl::aarch64::Label loop, done;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002249 __ Bind(&loop);
2250 __ Cmp(src_curr_addr, src_stop_addr);
2251 __ B(&done, eq);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002252 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, PostIndex));
2253 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, PostIndex));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002254 __ B(&loop);
2255 __ Bind(&done);
2256
2257 __ Bind(slow_path->GetExitLabel());
2258}
2259
donghui.baic2ec9ad2016-03-10 14:02:55 +08002260// We can choose to use the native implementation there for longer copy lengths.
2261static constexpr int32_t kSystemArrayCopyThreshold = 128;
2262
2263// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2264// We want to use two temporary registers in order to reduce the register pressure in arm64.
2265// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2266void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002267 // The only read barrier implementation supporting the
2268 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2269 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01002270 return;
2271 }
2272
donghui.baic2ec9ad2016-03-10 14:02:55 +08002273 // Check to see if we have known failures that will cause us to have to bail out
2274 // to the runtime, and just generate the runtime call directly.
2275 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2276 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2277
2278 // The positions must be non-negative.
2279 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2280 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2281 // We will have to fail anyways.
2282 return;
2283 }
2284
2285 // The length must be >= 0.
2286 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2287 if (length != nullptr) {
2288 int32_t len = length->GetValue();
2289 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2290 // Just call as normal.
2291 return;
2292 }
2293 }
2294
2295 SystemArrayCopyOptimizations optimizations(invoke);
2296
2297 if (optimizations.GetDestinationIsSource()) {
2298 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2299 // We only support backward copying if source and destination are the same.
2300 return;
2301 }
2302 }
2303
2304 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2305 // We currently don't intrinsify primitive copying.
2306 return;
2307 }
2308
Vladimir Markoca6fff82017-10-03 14:49:14 +01002309 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
2310 LocationSummary* locations =
2311 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002312 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2313 locations->SetInAt(0, Location::RequiresRegister());
2314 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2315 locations->SetInAt(2, Location::RequiresRegister());
2316 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2317 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2318
2319 locations->AddTemp(Location::RequiresRegister());
2320 locations->AddTemp(Location::RequiresRegister());
Roland Levillain0b671c02016-08-19 12:02:34 +01002321 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2322 // Temporary register IP0, obtained from the VIXL scratch register
2323 // pool, cannot be used in ReadBarrierSystemArrayCopySlowPathARM64
2324 // (because that register is clobbered by ReadBarrierMarkRegX
Roland Levillain54f869e2017-03-06 13:54:11 +00002325 // entry points). It cannot be used in calls to
2326 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier
2327 // either. For these reasons, get a third extra temporary register
2328 // from the register allocator.
Roland Levillain0b671c02016-08-19 12:02:34 +01002329 locations->AddTemp(Location::RequiresRegister());
Roland Levillain54f869e2017-03-06 13:54:11 +00002330 } else {
2331 // Cases other than Baker read barriers: the third temporary will
2332 // be acquired from the VIXL scratch register pool.
Roland Levillain0b671c02016-08-19 12:02:34 +01002333 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002334}
2335
2336void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002337 // The only read barrier implementation supporting the
2338 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2339 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002340
Scott Wakeling97c72b72016-06-24 16:19:36 +01002341 MacroAssembler* masm = GetVIXLAssembler();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002342 LocationSummary* locations = invoke->GetLocations();
2343
2344 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2345 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2346 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2347 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002348 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002349
2350 Register src = XRegisterFrom(locations->InAt(0));
2351 Location src_pos = locations->InAt(1);
2352 Register dest = XRegisterFrom(locations->InAt(2));
2353 Location dest_pos = locations->InAt(3);
2354 Location length = locations->InAt(4);
2355 Register temp1 = WRegisterFrom(locations->GetTemp(0));
Roland Levillain0b671c02016-08-19 12:02:34 +01002356 Location temp1_loc = LocationFrom(temp1);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002357 Register temp2 = WRegisterFrom(locations->GetTemp(1));
Roland Levillain0b671c02016-08-19 12:02:34 +01002358 Location temp2_loc = LocationFrom(temp2);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002359
Vladimir Marko174b2e22017-10-12 13:34:49 +01002360 SlowPathCodeARM64* intrinsic_slow_path =
2361 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002362 codegen_->AddSlowPath(intrinsic_slow_path);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002363
Scott Wakeling97c72b72016-06-24 16:19:36 +01002364 vixl::aarch64::Label conditions_on_positions_validated;
donghui.baic2ec9ad2016-03-10 14:02:55 +08002365 SystemArrayCopyOptimizations optimizations(invoke);
2366
donghui.baic2ec9ad2016-03-10 14:02:55 +08002367 // If source and destination are the same, we go to slow path if we need to do
2368 // forward copying.
2369 if (src_pos.IsConstant()) {
2370 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2371 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002372 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2373 if (optimizations.GetDestinationIsSource()) {
2374 // Checked when building locations.
2375 DCHECK_GE(src_pos_constant, dest_pos_constant);
2376 } else if (src_pos_constant < dest_pos_constant) {
2377 __ Cmp(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002378 __ B(intrinsic_slow_path->GetEntryLabel(), eq);
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002379 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002380 // Checked when building locations.
2381 DCHECK(!optimizations.GetDestinationIsSource()
2382 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2383 } else {
2384 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002385 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002386 __ B(&conditions_on_positions_validated, ne);
2387 }
2388 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
Roland Levillain0b671c02016-08-19 12:02:34 +01002389 __ B(intrinsic_slow_path->GetEntryLabel(), gt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002390 }
2391 } else {
2392 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002393 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002394 __ B(&conditions_on_positions_validated, ne);
2395 }
2396 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2397 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
Roland Levillain0b671c02016-08-19 12:02:34 +01002398 __ B(intrinsic_slow_path->GetEntryLabel(), lt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002399 }
2400
2401 __ Bind(&conditions_on_positions_validated);
2402
2403 if (!optimizations.GetSourceIsNotNull()) {
2404 // Bail out if the source is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002405 __ Cbz(src, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002406 }
2407
2408 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2409 // Bail out if the destination is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002410 __ Cbz(dest, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002411 }
2412
2413 // We have already checked in the LocationsBuilder for the constant case.
2414 if (!length.IsConstant() &&
2415 !optimizations.GetCountIsSourceLength() &&
2416 !optimizations.GetCountIsDestinationLength()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002417 // Merge the following two comparisons into one:
2418 // If the length is negative, bail out (delegate to libcore's native implementation).
2419 // If the length >= 128 then (currently) prefer native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002420 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002421 __ B(intrinsic_slow_path->GetEntryLabel(), hs);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002422 }
2423 // Validity checks: source.
2424 CheckSystemArrayCopyPosition(masm,
2425 src_pos,
2426 src,
2427 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002428 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002429 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002430 optimizations.GetCountIsSourceLength());
2431
2432 // Validity checks: dest.
2433 CheckSystemArrayCopyPosition(masm,
2434 dest_pos,
2435 dest,
2436 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002437 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002438 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002439 optimizations.GetCountIsDestinationLength());
2440 {
2441 // We use a block to end the scratch scope before the write barrier, thus
2442 // freeing the temporary registers so they can be used in `MarkGCCard`.
2443 UseScratchRegisterScope temps(masm);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002444 Location temp3_loc; // Used only for Baker read barrier.
Roland Levillain54f869e2017-03-06 13:54:11 +00002445 Register temp3;
2446 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002447 temp3_loc = locations->GetTemp(2);
2448 temp3 = WRegisterFrom(temp3_loc);
Roland Levillain54f869e2017-03-06 13:54:11 +00002449 } else {
2450 temp3 = temps.AcquireW();
2451 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002452
donghui.baic2ec9ad2016-03-10 14:02:55 +08002453 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2454 // Check whether all elements of the source array are assignable to the component
2455 // type of the destination array. We do two checks: the classes are the same,
2456 // or the destination is Object[]. If none of these checks succeed, we go to the
2457 // slow path.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002458
Roland Levillain0b671c02016-08-19 12:02:34 +01002459 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2460 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2461 // /* HeapReference<Class> */ temp1 = src->klass_
2462 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2463 temp1_loc,
2464 src.W(),
2465 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002466 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002467 /* needs_null_check= */ false,
2468 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002469 // Bail out if the source is not a non primitive array.
2470 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2471 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2472 temp1_loc,
2473 temp1,
2474 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002475 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002476 /* needs_null_check= */ false,
2477 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002478 __ Cbz(temp1, intrinsic_slow_path->GetEntryLabel());
2479 // If heap poisoning is enabled, `temp1` has been unpoisoned
2480 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2481 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2482 __ Ldrh(temp1, HeapOperand(temp1, primitive_offset));
2483 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2484 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002485 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002486
2487 // /* HeapReference<Class> */ temp1 = dest->klass_
2488 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2489 temp1_loc,
2490 dest.W(),
2491 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002492 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002493 /* needs_null_check= */ false,
2494 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002495
2496 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2497 // Bail out if the destination is not a non primitive array.
2498 //
2499 // Register `temp1` is not trashed by the read barrier emitted
2500 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2501 // method produces a call to a ReadBarrierMarkRegX entry point,
2502 // which saves all potentially live registers, including
2503 // temporaries such a `temp1`.
2504 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2505 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2506 temp2_loc,
2507 temp1,
2508 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002509 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002510 /* needs_null_check= */ false,
2511 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002512 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2513 // If heap poisoning is enabled, `temp2` has been unpoisoned
2514 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2515 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2516 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
2517 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2518 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
2519 }
2520
2521 // For the same reason given earlier, `temp1` is not trashed by the
2522 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2523 // /* HeapReference<Class> */ temp2 = src->klass_
2524 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2525 temp2_loc,
2526 src.W(),
2527 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002528 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002529 /* needs_null_check= */ false,
2530 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002531 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2532 __ Cmp(temp1, temp2);
2533
2534 if (optimizations.GetDestinationIsTypedObjectArray()) {
2535 vixl::aarch64::Label do_copy;
2536 __ B(&do_copy, eq);
2537 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2538 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2539 temp1_loc,
2540 temp1,
2541 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002542 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002543 /* needs_null_check= */ false,
2544 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002545 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2546 // We do not need to emit a read barrier for the following
2547 // heap reference load, as `temp1` is only used in a
2548 // comparison with null below, and this reference is not
2549 // kept afterwards.
2550 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2551 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2552 __ Bind(&do_copy);
2553 } else {
2554 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2555 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002556 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002557 // Non read barrier code.
2558
2559 // /* HeapReference<Class> */ temp1 = dest->klass_
2560 __ Ldr(temp1, MemOperand(dest, class_offset));
2561 // /* HeapReference<Class> */ temp2 = src->klass_
2562 __ Ldr(temp2, MemOperand(src, class_offset));
2563 bool did_unpoison = false;
2564 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2565 !optimizations.GetSourceIsNonPrimitiveArray()) {
2566 // One or two of the references need to be unpoisoned. Unpoison them
2567 // both to make the identity check valid.
2568 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2569 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2570 did_unpoison = true;
2571 }
2572
2573 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2574 // Bail out if the destination is not a non primitive array.
2575 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2576 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2577 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2578 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2579 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2580 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2581 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2582 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2583 }
2584
2585 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2586 // Bail out if the source is not a non primitive array.
2587 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2588 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2589 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2590 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2591 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2592 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2593 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2594 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2595 }
2596
2597 __ Cmp(temp1, temp2);
2598
2599 if (optimizations.GetDestinationIsTypedObjectArray()) {
2600 vixl::aarch64::Label do_copy;
2601 __ B(&do_copy, eq);
2602 if (!did_unpoison) {
2603 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2604 }
2605 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2606 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2607 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2608 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2609 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2610 // No need to unpoison the result, we're comparing against null.
2611 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2612 __ Bind(&do_copy);
2613 } else {
2614 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2615 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002616 }
2617 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2618 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2619 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002620 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2621 // /* HeapReference<Class> */ temp1 = src->klass_
2622 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2623 temp1_loc,
2624 src.W(),
2625 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002626 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002627 /* needs_null_check= */ false,
2628 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002629 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2630 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2631 temp2_loc,
2632 temp1,
2633 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002634 temp3_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08002635 /* needs_null_check= */ false,
2636 /* use_load_acquire= */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002637 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2638 // If heap poisoning is enabled, `temp2` has been unpoisoned
2639 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2640 } else {
2641 // /* HeapReference<Class> */ temp1 = src->klass_
2642 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2643 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2644 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2645 __ Ldr(temp2, HeapOperand(temp1, component_offset));
2646 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2647 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2648 }
2649 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2650 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
donghui.baic2ec9ad2016-03-10 14:02:55 +08002651 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain0b671c02016-08-19 12:02:34 +01002652 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002653 }
2654
Roland Levillain1663d162017-03-17 15:15:21 +00002655 if (length.IsConstant() && length.GetConstant()->AsIntConstant()->GetValue() == 0) {
2656 // Null constant length: not need to emit the loop code at all.
Roland Levillain0b671c02016-08-19 12:02:34 +01002657 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002658 Register src_curr_addr = temp1.X();
2659 Register dst_curr_addr = temp2.X();
2660 Register src_stop_addr = temp3.X();
2661 vixl::aarch64::Label done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002662 const DataType::Type type = DataType::Type::kReference;
2663 const int32_t element_size = DataType::Size(type);
Roland Levillain1663d162017-03-17 15:15:21 +00002664
2665 if (length.IsRegister()) {
2666 // Don't enter the copy loop if the length is null.
2667 __ Cbz(WRegisterFrom(length), &done);
2668 }
2669
2670 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2671 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2672
2673 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002674 // also CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002675 //
2676 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2677 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2678 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2679 // if (is_gray) {
2680 // // Slow-path copy.
2681 // do {
2682 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2683 // } while (src_ptr != end_ptr)
2684 // } else {
2685 // // Fast-path copy.
2686 // do {
2687 // *dest_ptr++ = *src_ptr++;
2688 // } while (src_ptr != end_ptr)
2689 // }
2690
2691 // Make sure `tmp` is not IP0, as it is clobbered by
2692 // ReadBarrierMarkRegX entry points in
2693 // ReadBarrierSystemArrayCopySlowPathARM64.
Roland Levillain1ca955d2017-04-13 19:34:30 +01002694 DCHECK(temps.IsAvailable(ip0));
Roland Levillain1663d162017-03-17 15:15:21 +00002695 temps.Exclude(ip0);
Roland Levillain0b671c02016-08-19 12:02:34 +01002696 Register tmp = temps.AcquireW();
Roland Levillain1663d162017-03-17 15:15:21 +00002697 DCHECK_NE(LocationFrom(tmp).reg(), IP0);
Roland Levillain1ca955d2017-04-13 19:34:30 +01002698 // Put IP0 back in the pool so that VIXL has at least one
2699 // scratch register available to emit macro-instructions (note
2700 // that IP1 is already used for `tmp`). Indeed some
2701 // macro-instructions used in GenSystemArrayCopyAddresses
2702 // (invoked hereunder) may require a scratch register (for
2703 // instance to emit a load with a large constant offset).
2704 temps.Include(ip0);
Roland Levillain1663d162017-03-17 15:15:21 +00002705
2706 // /* int32_t */ monitor = src->monitor_
2707 __ Ldr(tmp, HeapOperand(src.W(), monitor_offset));
2708 // /* LockWord */ lock_word = LockWord(monitor)
2709 static_assert(sizeof(LockWord) == sizeof(int32_t),
2710 "art::LockWord and int32_t have different sizes.");
2711
2712 // Introduce a dependency on the lock_word including rb_state,
2713 // to prevent load-load reordering, and without using
2714 // a memory barrier (which would be more expensive).
2715 // `src` is unchanged by this operation, but its value now depends
2716 // on `tmp`.
2717 __ Add(src.X(), src.X(), Operand(tmp.X(), LSR, 32));
2718
2719 // Compute base source address, base destination address, and end
2720 // source address for System.arraycopy* intrinsics in `src_base`,
2721 // `dst_base` and `src_end` respectively.
2722 // Note that `src_curr_addr` is computed from from `src` (and
2723 // `src_pos`) here, and thus honors the artificial dependency
2724 // of `src` on `tmp`.
2725 GenSystemArrayCopyAddresses(masm,
2726 type,
2727 src,
2728 src_pos,
2729 dest,
2730 dest_pos,
2731 length,
2732 src_curr_addr,
2733 dst_curr_addr,
2734 src_stop_addr);
2735
2736 // Slow path used to copy array when `src` is gray.
2737 SlowPathCodeARM64* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002738 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathARM64(
2739 invoke, LocationFrom(tmp));
Roland Levillain1663d162017-03-17 15:15:21 +00002740 codegen_->AddSlowPath(read_barrier_slow_path);
2741
2742 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01002743 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Roland Levillain1663d162017-03-17 15:15:21 +00002744 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2745 __ Tbnz(tmp, LockWord::kReadBarrierStateShift, read_barrier_slow_path->GetEntryLabel());
2746
2747 // Fast-path copy.
2748 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2749 // poison/unpoison.
2750 vixl::aarch64::Label loop;
2751 __ Bind(&loop);
Roland Levillain0b671c02016-08-19 12:02:34 +01002752 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2753 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
Roland Levillain1663d162017-03-17 15:15:21 +00002754 __ Cmp(src_curr_addr, src_stop_addr);
2755 __ B(&loop, ne);
2756
2757 __ Bind(read_barrier_slow_path->GetExitLabel());
2758 } else {
2759 // Non read barrier code.
2760 // Compute base source address, base destination address, and end
2761 // source address for System.arraycopy* intrinsics in `src_base`,
2762 // `dst_base` and `src_end` respectively.
2763 GenSystemArrayCopyAddresses(masm,
2764 type,
2765 src,
2766 src_pos,
2767 dest,
2768 dest_pos,
2769 length,
2770 src_curr_addr,
2771 dst_curr_addr,
2772 src_stop_addr);
2773 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2774 // poison/unpoison.
2775 vixl::aarch64::Label loop;
2776 __ Bind(&loop);
2777 {
2778 Register tmp = temps.AcquireW();
2779 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2780 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
2781 }
2782 __ Cmp(src_curr_addr, src_stop_addr);
2783 __ B(&loop, ne);
Roland Levillain0b671c02016-08-19 12:02:34 +01002784 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002785 __ Bind(&done);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002786 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002787 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002788
donghui.baic2ec9ad2016-03-10 14:02:55 +08002789 // We only need one card marking on the destination array.
Andreas Gampe3db70682018-12-26 15:12:03 -08002790 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null= */ false);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002791
Roland Levillain0b671c02016-08-19 12:02:34 +01002792 __ Bind(intrinsic_slow_path->GetExitLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002793}
2794
Anton Kirilova3ffea22016-04-07 17:02:37 +01002795static void GenIsInfinite(LocationSummary* locations,
2796 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002797 MacroAssembler* masm) {
Anton Kirilova3ffea22016-04-07 17:02:37 +01002798 Operand infinity;
2799 Register out;
2800
2801 if (is64bit) {
2802 infinity = kPositiveInfinityDouble;
2803 out = XRegisterFrom(locations->Out());
2804 } else {
2805 infinity = kPositiveInfinityFloat;
2806 out = WRegisterFrom(locations->Out());
2807 }
2808
Scott Wakeling97c72b72016-06-24 16:19:36 +01002809 const Register zero = vixl::aarch64::Assembler::AppropriateZeroRegFor(out);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002810
2811 MoveFPToInt(locations, is64bit, masm);
2812 __ Eor(out, out, infinity);
2813 // We don't care about the sign bit, so shift left.
2814 __ Cmp(zero, Operand(out, LSL, 1));
2815 __ Cset(out, eq);
2816}
2817
2818void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002819 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002820}
2821
2822void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002823 GenIsInfinite(invoke->GetLocations(), /* is64bit= */ false, GetVIXLAssembler());
Anton Kirilova3ffea22016-04-07 17:02:37 +01002824}
2825
2826void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002827 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002828}
2829
2830void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002831 GenIsInfinite(invoke->GetLocations(), /* is64bit= */ true, GetVIXLAssembler());
Anton Kirilova3ffea22016-04-07 17:02:37 +01002832}
2833
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002834void IntrinsicLocationsBuilderARM64::VisitIntegerValueOf(HInvoke* invoke) {
2835 InvokeRuntimeCallingConvention calling_convention;
2836 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2837 invoke,
2838 codegen_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002839 calling_convention.GetReturnLocation(DataType::Type::kReference),
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002840 Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2841}
2842
2843void IntrinsicCodeGeneratorARM64::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01002844 IntrinsicVisitor::IntegerValueOfInfo info =
2845 IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002846 LocationSummary* locations = invoke->GetLocations();
2847 MacroAssembler* masm = GetVIXLAssembler();
2848
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002849 Register out = RegisterFrom(locations->Out(), DataType::Type::kReference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002850 UseScratchRegisterScope temps(masm);
2851 Register temp = temps.AcquireW();
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002852 if (invoke->InputAt(0)->IsConstant()) {
2853 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
Vladimir Marko6fd16062018-06-26 11:02:04 +01002854 if (static_cast<uint32_t>(value - info.low) < info.length) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002855 // Just embed the j.l.Integer in the code.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002856 DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
2857 codegen_->LoadBootImageAddress(out, info.value_boot_image_reference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002858 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002859 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002860 // Allocate and initialize a new j.l.Integer.
2861 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2862 // JIT object table.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002863 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2864 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002865 __ Mov(temp.W(), value);
2866 __ Str(temp.W(), HeapOperand(out.W(), info.value_offset));
2867 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2868 // one.
2869 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2870 }
2871 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002872 DCHECK(locations->CanCall());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 Register in = RegisterFrom(locations->InAt(0), DataType::Type::kInt32);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002874 // Check bounds of our cache.
2875 __ Add(out.W(), in.W(), -info.low);
Vladimir Markoeebb8212018-06-05 14:57:24 +01002876 __ Cmp(out.W(), info.length);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002877 vixl::aarch64::Label allocate, done;
2878 __ B(&allocate, hs);
2879 // If the value is within the bounds, load the j.l.Integer directly from the array.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002880 codegen_->LoadBootImageAddress(temp, info.array_data_boot_image_reference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002881 MemOperand source = HeapOperand(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 temp, out.X(), LSL, DataType::SizeShift(DataType::Type::kReference));
2883 codegen_->Load(DataType::Type::kReference, out, source);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002884 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(out);
2885 __ B(&done);
2886 __ Bind(&allocate);
2887 // Otherwise allocate and initialize a new j.l.Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002888 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2889 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002890 __ Str(in.W(), HeapOperand(out.W(), info.value_offset));
2891 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2892 // one.
2893 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2894 __ Bind(&done);
2895 }
2896}
2897
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002898void IntrinsicLocationsBuilderARM64::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002899 LocationSummary* locations =
2900 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002901 locations->SetOut(Location::RequiresRegister());
2902}
2903
2904void IntrinsicCodeGeneratorARM64::VisitThreadInterrupted(HInvoke* invoke) {
2905 MacroAssembler* masm = GetVIXLAssembler();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002906 Register out = RegisterFrom(invoke->GetLocations()->Out(), DataType::Type::kInt32);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002907 UseScratchRegisterScope temps(masm);
2908 Register temp = temps.AcquireX();
2909
2910 __ Add(temp, tr, Thread::InterruptedOffset<kArm64PointerSize>().Int32Value());
2911 __ Ldar(out.W(), MemOperand(temp));
2912
2913 vixl::aarch64::Label done;
2914 __ Cbz(out.W(), &done);
2915 __ Stlr(wzr, MemOperand(temp));
2916 __ Bind(&done);
2917}
2918
Hans Boehmc7b28de2018-03-09 17:05:28 -08002919void IntrinsicLocationsBuilderARM64::VisitReachabilityFence(HInvoke* invoke) {
2920 LocationSummary* locations =
2921 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
2922 locations->SetInAt(0, Location::Any());
2923}
2924
2925void IntrinsicCodeGeneratorARM64::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
2926
xueliang.zhongcb58b072017-10-13 12:06:56 +01002927void IntrinsicLocationsBuilderARM64::VisitCRC32Update(HInvoke* invoke) {
2928 if (!codegen_->GetInstructionSetFeatures().HasCRC()) {
2929 return;
2930 }
2931
2932 LocationSummary* locations = new (allocator_) LocationSummary(invoke,
2933 LocationSummary::kNoCall,
2934 kIntrinsified);
2935
2936 locations->SetInAt(0, Location::RequiresRegister());
2937 locations->SetInAt(1, Location::RequiresRegister());
Evgeny Astigeevichc01dc292018-12-12 15:32:57 +00002938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
xueliang.zhongcb58b072017-10-13 12:06:56 +01002939}
2940
2941// Lower the invoke of CRC32.update(int crc, int b).
2942void IntrinsicCodeGeneratorARM64::VisitCRC32Update(HInvoke* invoke) {
2943 DCHECK(codegen_->GetInstructionSetFeatures().HasCRC());
2944
2945 MacroAssembler* masm = GetVIXLAssembler();
2946
2947 Register crc = InputRegisterAt(invoke, 0);
2948 Register val = InputRegisterAt(invoke, 1);
2949 Register out = OutputRegister(invoke);
2950
2951 // The general algorithm of the CRC32 calculation is:
2952 // crc = ~crc
2953 // result = crc32_for_byte(crc, b)
2954 // crc = ~result
2955 // It is directly lowered to three instructions.
Evgeny Astigeevichc01dc292018-12-12 15:32:57 +00002956
2957 UseScratchRegisterScope temps(masm);
2958 Register tmp = temps.AcquireSameSizeAs(out);
2959
2960 __ Mvn(tmp, crc);
2961 __ Crc32b(tmp, tmp, val);
2962 __ Mvn(out, tmp);
xueliang.zhongcb58b072017-10-13 12:06:56 +01002963}
2964
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00002965// Generate code using CRC32 instructions which calculates
2966// a CRC32 value of a byte.
Evgeny Astigeevich15c5b972018-11-20 13:41:40 +00002967//
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00002968// Parameters:
2969// masm - VIXL macro assembler
2970// crc - a register holding an initial CRC value
2971// ptr - a register holding a memory address of bytes
2972// length - a register holding a number of bytes to process
2973// out - a register to put a result of calculation
2974static void GenerateCodeForCalculationCRC32ValueOfBytes(MacroAssembler* masm,
2975 const Register& crc,
2976 const Register& ptr,
2977 const Register& length,
2978 const Register& out) {
Evgeny Astigeevich15c5b972018-11-20 13:41:40 +00002979 // The algorithm of CRC32 of bytes is:
2980 // crc = ~crc
2981 // process a few first bytes to make the array 8-byte aligned
2982 // while array has 8 bytes do:
2983 // crc = crc32_of_8bytes(crc, 8_bytes(array))
2984 // if array has 4 bytes:
2985 // crc = crc32_of_4bytes(crc, 4_bytes(array))
2986 // if array has 2 bytes:
2987 // crc = crc32_of_2bytes(crc, 2_bytes(array))
2988 // if array has a byte:
2989 // crc = crc32_of_byte(crc, 1_byte(array))
2990 // crc = ~crc
2991
2992 vixl::aarch64::Label loop, done;
2993 vixl::aarch64::Label process_4bytes, process_2bytes, process_1byte;
2994 vixl::aarch64::Label aligned2, aligned4, aligned8;
2995
2996 // Use VIXL scratch registers as the VIXL macro assembler won't use them in
2997 // instructions below.
2998 UseScratchRegisterScope temps(masm);
2999 Register len = temps.AcquireW();
3000 Register array_elem = temps.AcquireW();
3001
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00003002 __ Mvn(out, crc);
Evgeny Astigeevich15c5b972018-11-20 13:41:40 +00003003 __ Mov(len, length);
3004
3005 __ Tbz(ptr, 0, &aligned2);
3006 __ Subs(len, len, 1);
3007 __ B(&done, lo);
3008 __ Ldrb(array_elem, MemOperand(ptr, 1, PostIndex));
3009 __ Crc32b(out, out, array_elem);
3010
3011 __ Bind(&aligned2);
3012 __ Tbz(ptr, 1, &aligned4);
3013 __ Subs(len, len, 2);
3014 __ B(&process_1byte, lo);
3015 __ Ldrh(array_elem, MemOperand(ptr, 2, PostIndex));
3016 __ Crc32h(out, out, array_elem);
3017
3018 __ Bind(&aligned4);
3019 __ Tbz(ptr, 2, &aligned8);
3020 __ Subs(len, len, 4);
3021 __ B(&process_2bytes, lo);
3022 __ Ldr(array_elem, MemOperand(ptr, 4, PostIndex));
3023 __ Crc32w(out, out, array_elem);
3024
3025 __ Bind(&aligned8);
3026 __ Subs(len, len, 8);
3027 // If len < 8 go to process data by 4 bytes, 2 bytes and a byte.
3028 __ B(&process_4bytes, lo);
3029
3030 // The main loop processing data by 8 bytes.
3031 __ Bind(&loop);
3032 __ Ldr(array_elem.X(), MemOperand(ptr, 8, PostIndex));
3033 __ Subs(len, len, 8);
3034 __ Crc32x(out, out, array_elem.X());
3035 // if len >= 8, process the next 8 bytes.
3036 __ B(&loop, hs);
3037
3038 // Process the data which is less than 8 bytes.
3039 // The code generated below works with values of len
3040 // which come in the range [-8, 0].
3041 // The first three bits are used to detect whether 4 bytes or 2 bytes or
3042 // a byte can be processed.
3043 // The checking order is from bit 2 to bit 0:
3044 // bit 2 is set: at least 4 bytes available
3045 // bit 1 is set: at least 2 bytes available
3046 // bit 0 is set: at least a byte available
3047 __ Bind(&process_4bytes);
3048 // Goto process_2bytes if less than four bytes available
3049 __ Tbz(len, 2, &process_2bytes);
3050 __ Ldr(array_elem, MemOperand(ptr, 4, PostIndex));
3051 __ Crc32w(out, out, array_elem);
3052
3053 __ Bind(&process_2bytes);
3054 // Goto process_1bytes if less than two bytes available
3055 __ Tbz(len, 1, &process_1byte);
3056 __ Ldrh(array_elem, MemOperand(ptr, 2, PostIndex));
3057 __ Crc32h(out, out, array_elem);
3058
3059 __ Bind(&process_1byte);
3060 // Goto done if no bytes available
3061 __ Tbz(len, 0, &done);
3062 __ Ldrb(array_elem, MemOperand(ptr));
3063 __ Crc32b(out, out, array_elem);
3064
3065 __ Bind(&done);
3066 __ Mvn(out, out);
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00003067}
3068
3069// The threshold for sizes of arrays to use the library provided implementation
3070// of CRC32.updateBytes instead of the intrinsic.
3071static constexpr int32_t kCRC32UpdateBytesThreshold = 64 * 1024;
3072
3073void IntrinsicLocationsBuilderARM64::VisitCRC32UpdateBytes(HInvoke* invoke) {
3074 if (!codegen_->GetInstructionSetFeatures().HasCRC()) {
3075 return;
3076 }
3077
3078 LocationSummary* locations =
3079 new (allocator_) LocationSummary(invoke,
3080 LocationSummary::kCallOnSlowPath,
3081 kIntrinsified);
3082
3083 locations->SetInAt(0, Location::RequiresRegister());
3084 locations->SetInAt(1, Location::RequiresRegister());
3085 locations->SetInAt(2, Location::RegisterOrConstant(invoke->InputAt(2)));
3086 locations->SetInAt(3, Location::RequiresRegister());
3087 locations->AddTemp(Location::RequiresRegister());
3088 locations->SetOut(Location::RequiresRegister());
3089}
3090
3091// Lower the invoke of CRC32.updateBytes(int crc, byte[] b, int off, int len)
3092//
3093// Note: The intrinsic is not used if len exceeds a threshold.
3094void IntrinsicCodeGeneratorARM64::VisitCRC32UpdateBytes(HInvoke* invoke) {
3095 DCHECK(codegen_->GetInstructionSetFeatures().HasCRC());
3096
Evgeny Astigeeviche36f5f62019-01-08 17:01:31 +00003097 MacroAssembler* masm = GetVIXLAssembler();
3098 LocationSummary* locations = invoke->GetLocations();
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00003099
Evgeny Astigeeviche36f5f62019-01-08 17:01:31 +00003100 SlowPathCodeARM64* slow_path =
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00003101 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
3102 codegen_->AddSlowPath(slow_path);
3103
3104 Register length = WRegisterFrom(locations->InAt(3));
3105 __ Cmp(length, kCRC32UpdateBytesThreshold);
3106 __ B(slow_path->GetEntryLabel(), hi);
3107
3108 const uint32_t array_data_offset =
3109 mirror::Array::DataOffset(Primitive::kPrimByte).Uint32Value();
3110 Register ptr = XRegisterFrom(locations->GetTemp(0));
3111 Register array = XRegisterFrom(locations->InAt(1));
Evgeny Astigeeviche36f5f62019-01-08 17:01:31 +00003112 Location offset = locations->InAt(2);
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00003113 if (offset.IsConstant()) {
3114 int32_t offset_value = offset.GetConstant()->AsIntConstant()->GetValue();
3115 __ Add(ptr, array, array_data_offset + offset_value);
3116 } else {
3117 __ Add(ptr, array, array_data_offset);
3118 __ Add(ptr, ptr, XRegisterFrom(offset));
3119 }
3120
3121 Register crc = WRegisterFrom(locations->InAt(0));
3122 Register out = WRegisterFrom(locations->Out());
3123
3124 GenerateCodeForCalculationCRC32ValueOfBytes(masm, crc, ptr, length, out);
Evgeny Astigeevich15c5b972018-11-20 13:41:40 +00003125
3126 __ Bind(slow_path->GetExitLabel());
3127}
3128
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00003129void IntrinsicLocationsBuilderARM64::VisitCRC32UpdateByteBuffer(HInvoke* invoke) {
3130 if (!codegen_->GetInstructionSetFeatures().HasCRC()) {
3131 return;
3132 }
3133
3134 LocationSummary* locations =
3135 new (allocator_) LocationSummary(invoke,
3136 LocationSummary::kNoCall,
3137 kIntrinsified);
3138
3139 locations->SetInAt(0, Location::RequiresRegister());
3140 locations->SetInAt(1, Location::RequiresRegister());
3141 locations->SetInAt(2, Location::RequiresRegister());
3142 locations->SetInAt(3, Location::RequiresRegister());
3143 locations->AddTemp(Location::RequiresRegister());
3144 locations->SetOut(Location::RequiresRegister());
3145}
3146
3147// Lower the invoke of CRC32.updateByteBuffer(int crc, long addr, int off, int len)
3148//
3149// There is no need to generate code checking if addr is 0.
3150// The method updateByteBuffer is a private method of java.util.zip.CRC32.
3151// This guarantees no calls outside of the CRC32 class.
3152// An address of DirectBuffer is always passed to the call of updateByteBuffer.
3153// It might be an implementation of an empty DirectBuffer which can use a zero
3154// address but it must have the length to be zero. The current generated code
3155// correctly works with the zero length.
3156void IntrinsicCodeGeneratorARM64::VisitCRC32UpdateByteBuffer(HInvoke* invoke) {
3157 DCHECK(codegen_->GetInstructionSetFeatures().HasCRC());
3158
Evgeny Astigeeviche36f5f62019-01-08 17:01:31 +00003159 MacroAssembler* masm = GetVIXLAssembler();
3160 LocationSummary* locations = invoke->GetLocations();
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00003161
3162 Register addr = XRegisterFrom(locations->InAt(1));
3163 Register ptr = XRegisterFrom(locations->GetTemp(0));
3164 __ Add(ptr, addr, XRegisterFrom(locations->InAt(2)));
3165
3166 Register crc = WRegisterFrom(locations->InAt(0));
3167 Register length = WRegisterFrom(locations->InAt(3));
3168 Register out = WRegisterFrom(locations->Out());
3169 GenerateCodeForCalculationCRC32ValueOfBytes(masm, crc, ptr, length, out);
3170}
3171
Vladimir Markod254f5c2017-06-02 15:18:36 +00003172UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Andreas Gampe878d58c2015-01-15 23:24:00 -08003173
Aart Bikff7d89c2016-11-07 08:49:28 -08003174UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOf);
3175UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003176UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferAppend);
3177UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferLength);
3178UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferToString);
Vladimir Markod4561172017-10-30 17:48:25 +00003179UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendObject);
3180UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendString);
3181UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendCharSequence);
3182UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendCharArray);
3183UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendBoolean);
3184UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendChar);
3185UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendInt);
3186UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendLong);
3187UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendFloat);
3188UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppendDouble);
Aart Bik71bf7b42016-11-16 10:17:46 -08003189UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderLength);
3190UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003191
Aart Bik0e54c012016-03-04 12:08:31 -08003192// 1.8.
3193UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
3194UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
3195UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
3196UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
3197UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003198
Aart Bik2f9fcc92016-03-01 15:16:54 -08003199UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01003200
3201#undef __
3202
Andreas Gampe878d58c2015-01-15 23:24:00 -08003203} // namespace arm64
3204} // namespace art