blob: 0b04fff927c02acb6e1e1439316cd64264e93946 [file] [log] [blame]
Andreas Gampe878d58c2015-01-15 23:24:00 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_arm64.h"
18
Serban Constantinescu579885a2015-02-22 20:51:33 +000019#include "arch/arm64/instruction_set_features_arm64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080021#include "code_generator_arm64.h"
22#include "common_arm64.h"
23#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070024#include "heap_poisoning.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080025#include "intrinsics.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080026#include "lock_word.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080027#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070028#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#include "mirror/reference.h"
Vladimir Markoe39f14f2017-02-10 15:44:25 +000030#include "mirror/string-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080031#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070032#include "thread-current-inl.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080033#include "utils/arm64/assembler_arm64.h"
Andreas Gampe878d58c2015-01-15 23:24:00 -080034
Scott Wakeling97c72b72016-06-24 16:19:36 +010035using namespace vixl::aarch64; // NOLINT(build/namespaces)
Andreas Gampe878d58c2015-01-15 23:24:00 -080036
Artem Serovaf4e42a2016-08-08 15:11:24 +010037// TODO(VIXL): Make VIXL compile with -Wshadow.
Scott Wakeling97c72b72016-06-24 16:19:36 +010038#pragma GCC diagnostic push
39#pragma GCC diagnostic ignored "-Wshadow"
Artem Serovaf4e42a2016-08-08 15:11:24 +010040#include "aarch64/disasm-aarch64.h"
41#include "aarch64/macro-assembler-aarch64.h"
Scott Wakeling97c72b72016-06-24 16:19:36 +010042#pragma GCC diagnostic pop
Andreas Gampe878d58c2015-01-15 23:24:00 -080043
44namespace art {
45
46namespace arm64 {
47
48using helpers::DRegisterFrom;
49using helpers::FPRegisterFrom;
50using helpers::HeapOperand;
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +000051using helpers::LocationFrom;
Scott Wakeling9ee23f42015-07-23 10:44:35 +010052using helpers::OperandFrom;
Andreas Gampe878d58c2015-01-15 23:24:00 -080053using helpers::RegisterFrom;
54using helpers::SRegisterFrom;
55using helpers::WRegisterFrom;
56using helpers::XRegisterFrom;
xueliang.zhong49924c92016-03-03 10:52:51 +000057using helpers::InputRegisterAt;
Scott Wakeling1f36f412016-04-21 11:13:45 +010058using helpers::OutputRegister;
Andreas Gampe878d58c2015-01-15 23:24:00 -080059
Andreas Gampe878d58c2015-01-15 23:24:00 -080060namespace {
61
62ALWAYS_INLINE inline MemOperand AbsoluteHeapOperandFrom(Location location, size_t offset = 0) {
63 return MemOperand(XRegisterFrom(location), offset);
64}
65
66} // namespace
67
Scott Wakeling97c72b72016-06-24 16:19:36 +010068MacroAssembler* IntrinsicCodeGeneratorARM64::GetVIXLAssembler() {
Alexandre Rames087930f2016-08-02 13:45:28 +010069 return codegen_->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -080070}
71
72ArenaAllocator* IntrinsicCodeGeneratorARM64::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010073 return codegen_->GetGraph()->GetAllocator();
Andreas Gampe878d58c2015-01-15 23:24:00 -080074}
75
Alexandre Rames087930f2016-08-02 13:45:28 +010076#define __ codegen->GetVIXLAssembler()->
Andreas Gampe878d58c2015-01-15 23:24:00 -080077
78static void MoveFromReturnRegister(Location trg,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079 DataType::Type type,
Andreas Gampe878d58c2015-01-15 23:24:00 -080080 CodeGeneratorARM64* codegen) {
81 if (!trg.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010082 DCHECK(type == DataType::Type::kVoid);
Andreas Gampe878d58c2015-01-15 23:24:00 -080083 return;
84 }
85
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010086 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe878d58c2015-01-15 23:24:00 -080087
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010088 if (DataType::IsIntegralType(type) || type == DataType::Type::kReference) {
Andreas Gampe878d58c2015-01-15 23:24:00 -080089 Register trg_reg = RegisterFrom(trg, type);
90 Register res_reg = RegisterFrom(ARM64ReturnLocation(type), type);
91 __ Mov(trg_reg, res_reg, kDiscardForSameWReg);
92 } else {
93 FPRegister trg_reg = FPRegisterFrom(trg, type);
94 FPRegister res_reg = FPRegisterFrom(ARM64ReturnLocation(type), type);
95 __ Fmov(trg_reg, res_reg);
96 }
97}
98
Roland Levillainec525fc2015-04-28 15:50:20 +010099static void MoveArguments(HInvoke* invoke, CodeGeneratorARM64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100100 InvokeDexCallingConventionVisitorARM64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +0100101 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800102}
103
104// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
105// call. This will copy the arguments into the positions for a regular call.
106//
107// Note: The actual parameters are required to be in the locations given by the invoke's location
108// summary. If an intrinsic modifies those locations before a slowpath call, they must be
109// restored!
110class IntrinsicSlowPathARM64 : public SlowPathCodeARM64 {
111 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000112 explicit IntrinsicSlowPathARM64(HInvoke* invoke)
113 : SlowPathCodeARM64(invoke), invoke_(invoke) { }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800114
115 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
116 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
117 __ Bind(GetEntryLabel());
118
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000119 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800120
Roland Levillainec525fc2015-04-28 15:50:20 +0100121 MoveArguments(invoke_, codegen);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800122
Artem Serov914d7a82017-02-07 14:33:49 +0000123 {
124 // Ensure that between the BLR (emitted by Generate*Call) and RecordPcInfo there
125 // are no pools emitted.
126 vixl::EmissionCheckScope guard(codegen->GetVIXLAssembler(), kInvokeCodeMarginSizeInBytes);
127 if (invoke_->IsInvokeStaticOrDirect()) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100128 codegen->GenerateStaticOrDirectCall(
129 invoke_->AsInvokeStaticOrDirect(), LocationFrom(kArtMethodRegister), this);
Artem Serov914d7a82017-02-07 14:33:49 +0000130 } else {
Vladimir Markoe7197bf2017-06-02 17:00:23 +0100131 codegen->GenerateVirtualCall(
132 invoke_->AsInvokeVirtual(), LocationFrom(kArtMethodRegister), this);
Artem Serov914d7a82017-02-07 14:33:49 +0000133 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800134 }
135
136 // Copy the result back to the expected output.
137 Location out = invoke_->GetLocations()->Out();
138 if (out.IsValid()) {
139 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
140 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
141 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
142 }
143
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000144 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800145 __ B(GetExitLabel());
146 }
147
Alexandre Rames9931f312015-06-19 14:47:01 +0100148 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathARM64"; }
149
Andreas Gampe878d58c2015-01-15 23:24:00 -0800150 private:
151 // The instruction where this slow path is happening.
152 HInvoke* const invoke_;
153
154 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathARM64);
155};
156
Roland Levillain0b671c02016-08-19 12:02:34 +0100157// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
158class ReadBarrierSystemArrayCopySlowPathARM64 : public SlowPathCodeARM64 {
159 public:
160 ReadBarrierSystemArrayCopySlowPathARM64(HInstruction* instruction, Location tmp)
161 : SlowPathCodeARM64(instruction), tmp_(tmp) {
162 DCHECK(kEmitCompilerReadBarrier);
163 DCHECK(kUseBakerReadBarrier);
164 }
165
166 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
167 CodeGeneratorARM64* codegen = down_cast<CodeGeneratorARM64*>(codegen_in);
168 LocationSummary* locations = instruction_->GetLocations();
169 DCHECK(locations->CanCall());
170 DCHECK(instruction_->IsInvokeStaticOrDirect())
171 << "Unexpected instruction in read barrier arraycopy slow path: "
172 << instruction_->DebugName();
173 DCHECK(instruction_->GetLocations()->Intrinsified());
174 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
175
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100176 const int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +0100177
178 Register src_curr_addr = XRegisterFrom(locations->GetTemp(0));
179 Register dst_curr_addr = XRegisterFrom(locations->GetTemp(1));
180 Register src_stop_addr = XRegisterFrom(locations->GetTemp(2));
181 Register tmp_reg = WRegisterFrom(tmp_);
182
183 __ Bind(GetEntryLabel());
184 vixl::aarch64::Label slow_copy_loop;
185 __ Bind(&slow_copy_loop);
186 __ Ldr(tmp_reg, MemOperand(src_curr_addr, element_size, PostIndex));
187 codegen->GetAssembler()->MaybeUnpoisonHeapReference(tmp_reg);
188 // TODO: Inline the mark bit check before calling the runtime?
189 // tmp_reg = ReadBarrier::Mark(tmp_reg);
190 // No need to save live registers; it's taken care of by the
191 // entrypoint. Also, there is no need to update the stack mask,
192 // as this runtime call will not trigger a garbage collection.
193 // (See ReadBarrierMarkSlowPathARM64::EmitNativeCode for more
194 // explanations.)
195 DCHECK_NE(tmp_.reg(), LR);
196 DCHECK_NE(tmp_.reg(), WSP);
197 DCHECK_NE(tmp_.reg(), WZR);
198 // IP0 is used internally by the ReadBarrierMarkRegX entry point
199 // as a temporary (and not preserved). It thus cannot be used by
200 // any live register in this slow path.
201 DCHECK_NE(LocationFrom(src_curr_addr).reg(), IP0);
202 DCHECK_NE(LocationFrom(dst_curr_addr).reg(), IP0);
203 DCHECK_NE(LocationFrom(src_stop_addr).reg(), IP0);
204 DCHECK_NE(tmp_.reg(), IP0);
205 DCHECK(0 <= tmp_.reg() && tmp_.reg() < kNumberOfWRegisters) << tmp_.reg();
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000206 // TODO: Load the entrypoint once before the loop, instead of
207 // loading it at every iteration.
Roland Levillain0b671c02016-08-19 12:02:34 +0100208 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100209 Thread::ReadBarrierMarkEntryPointsOffset<kArm64PointerSize>(tmp_.reg());
Roland Levillain0b671c02016-08-19 12:02:34 +0100210 // This runtime call does not require a stack map.
211 codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
212 codegen->GetAssembler()->MaybePoisonHeapReference(tmp_reg);
213 __ Str(tmp_reg, MemOperand(dst_curr_addr, element_size, PostIndex));
214 __ Cmp(src_curr_addr, src_stop_addr);
215 __ B(&slow_copy_loop, ne);
216 __ B(GetExitLabel());
217 }
218
219 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathARM64"; }
220
221 private:
222 Location tmp_;
223
224 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathARM64);
225};
Andreas Gampe878d58c2015-01-15 23:24:00 -0800226#undef __
227
228bool IntrinsicLocationsBuilderARM64::TryDispatch(HInvoke* invoke) {
229 Dispatch(invoke);
230 LocationSummary* res = invoke->GetLocations();
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000231 if (res == nullptr) {
232 return false;
233 }
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000234 return res->Intrinsified();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800235}
236
237#define __ masm->
238
Vladimir Markoca6fff82017-10-03 14:49:14 +0100239static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
240 LocationSummary* locations =
241 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800242 locations->SetInAt(0, Location::RequiresFpuRegister());
243 locations->SetOut(Location::RequiresRegister());
244}
245
Vladimir Markoca6fff82017-10-03 14:49:14 +0100246static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
247 LocationSummary* locations =
248 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800249 locations->SetInAt(0, Location::RequiresRegister());
250 locations->SetOut(Location::RequiresFpuRegister());
251}
252
Scott Wakeling97c72b72016-06-24 16:19:36 +0100253static void MoveFPToInt(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800254 Location input = locations->InAt(0);
255 Location output = locations->Out();
256 __ Fmov(is64bit ? XRegisterFrom(output) : WRegisterFrom(output),
257 is64bit ? DRegisterFrom(input) : SRegisterFrom(input));
258}
259
Scott Wakeling97c72b72016-06-24 16:19:36 +0100260static void MoveIntToFP(LocationSummary* locations, bool is64bit, MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800261 Location input = locations->InAt(0);
262 Location output = locations->Out();
263 __ Fmov(is64bit ? DRegisterFrom(output) : SRegisterFrom(output),
264 is64bit ? XRegisterFrom(input) : WRegisterFrom(input));
265}
266
267void IntrinsicLocationsBuilderARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100268 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800269}
270void IntrinsicLocationsBuilderARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100271 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800272}
273
274void IntrinsicCodeGeneratorARM64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000275 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800276}
277void IntrinsicCodeGeneratorARM64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000278 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800279}
280
281void IntrinsicLocationsBuilderARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100282 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800283}
284void IntrinsicLocationsBuilderARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100285 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800286}
287
288void IntrinsicCodeGeneratorARM64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000289 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800290}
291void IntrinsicCodeGeneratorARM64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000292 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800293}
294
Vladimir Markoca6fff82017-10-03 14:49:14 +0100295static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
296 LocationSummary* locations =
297 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800298 locations->SetInAt(0, Location::RequiresRegister());
299 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
300}
301
302static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100303 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100304 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800305 Location in = locations->InAt(0);
306 Location out = locations->Out();
307
308 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100309 case DataType::Type::kInt16:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800310 __ Rev16(WRegisterFrom(out), WRegisterFrom(in));
311 __ Sxth(WRegisterFrom(out), WRegisterFrom(out));
312 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100313 case DataType::Type::kInt32:
314 case DataType::Type::kInt64:
Andreas Gampe878d58c2015-01-15 23:24:00 -0800315 __ Rev(RegisterFrom(out, type), RegisterFrom(in, type));
316 break;
317 default:
318 LOG(FATAL) << "Unexpected size for reverse-bytes: " << type;
319 UNREACHABLE();
320 }
321}
322
323void IntrinsicLocationsBuilderARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100324 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800325}
326
327void IntrinsicCodeGeneratorARM64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100328 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800329}
330
331void IntrinsicLocationsBuilderARM64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100332 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800333}
334
335void IntrinsicCodeGeneratorARM64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100336 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800337}
338
339void IntrinsicLocationsBuilderARM64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100340 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800341}
342
343void IntrinsicCodeGeneratorARM64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100344 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800345}
346
Vladimir Markoca6fff82017-10-03 14:49:14 +0100347static void CreateIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
348 LocationSummary* locations =
349 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bik7b565022016-01-28 14:36:22 -0800350 locations->SetInAt(0, Location::RequiresRegister());
351 locations->SetInAt(1, Location::RequiresRegister());
352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
353}
354
Scott Wakeling611d3392015-07-10 11:42:06 +0100355static void GenNumberOfLeadingZeros(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100356 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100357 MacroAssembler* masm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100358 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Scott Wakeling611d3392015-07-10 11:42:06 +0100359
360 Location in = locations->InAt(0);
361 Location out = locations->Out();
362
363 __ Clz(RegisterFrom(out, type), RegisterFrom(in, type));
364}
365
366void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100367 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling611d3392015-07-10 11:42:06 +0100368}
369
370void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100371 GenNumberOfLeadingZeros(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Scott Wakeling611d3392015-07-10 11:42:06 +0100372}
373
374void IntrinsicLocationsBuilderARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100375 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling611d3392015-07-10 11:42:06 +0100376}
377
378void IntrinsicCodeGeneratorARM64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100379 GenNumberOfLeadingZeros(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Scott Wakeling611d3392015-07-10 11:42:06 +0100380}
381
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100382static void GenNumberOfTrailingZeros(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100383 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100384 MacroAssembler* masm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100385 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100386
387 Location in = locations->InAt(0);
388 Location out = locations->Out();
389
390 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
391 __ Clz(RegisterFrom(out, type), RegisterFrom(out, type));
392}
393
394void IntrinsicLocationsBuilderARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100395 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100396}
397
398void IntrinsicCodeGeneratorARM64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100399 GenNumberOfTrailingZeros(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100400}
401
402void IntrinsicLocationsBuilderARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100403 CreateIntToIntLocations(allocator_, invoke);
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100404}
405
406void IntrinsicCodeGeneratorARM64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407 GenNumberOfTrailingZeros(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Scott Wakeling9ee23f42015-07-23 10:44:35 +0100408}
409
Andreas Gampe878d58c2015-01-15 23:24:00 -0800410static void GenReverse(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100411 DataType::Type type,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100412 MacroAssembler* masm) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100413 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800414
415 Location in = locations->InAt(0);
416 Location out = locations->Out();
417
418 __ Rbit(RegisterFrom(out, type), RegisterFrom(in, type));
419}
420
421void IntrinsicLocationsBuilderARM64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100422 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800423}
424
425void IntrinsicCodeGeneratorARM64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100426 GenReverse(invoke->GetLocations(), DataType::Type::kInt32, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800427}
428
429void IntrinsicLocationsBuilderARM64::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100430 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800431}
432
433void IntrinsicCodeGeneratorARM64::VisitLongReverse(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100434 GenReverse(invoke->GetLocations(), DataType::Type::kInt64, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800435}
436
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100437static void GenBitCount(HInvoke* instr, DataType::Type type, MacroAssembler* masm) {
438 DCHECK(DataType::IsIntOrLongType(type)) << type;
439 DCHECK_EQ(instr->GetType(), DataType::Type::kInt32);
440 DCHECK_EQ(DataType::Kind(instr->InputAt(0)->GetType()), type);
xueliang.zhong49924c92016-03-03 10:52:51 +0000441
xueliang.zhong49924c92016-03-03 10:52:51 +0000442 UseScratchRegisterScope temps(masm);
443
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000444 Register src = InputRegisterAt(instr, 0);
Roland Levillainfa3912e2016-04-01 18:21:55 +0100445 Register dst = RegisterFrom(instr->GetLocations()->Out(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100446 FPRegister fpr = (type == DataType::Type::kInt64) ? temps.AcquireD() : temps.AcquireS();
xueliang.zhong49924c92016-03-03 10:52:51 +0000447
448 __ Fmov(fpr, src);
Nicolas Geoffray457413a2016-03-04 11:10:17 +0000449 __ Cnt(fpr.V8B(), fpr.V8B());
450 __ Addv(fpr.B(), fpr.V8B());
xueliang.zhong49924c92016-03-03 10:52:51 +0000451 __ Fmov(dst, fpr);
452}
453
454void IntrinsicLocationsBuilderARM64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100455 CreateIntToIntLocations(allocator_, invoke);
xueliang.zhong49924c92016-03-03 10:52:51 +0000456}
457
458void IntrinsicCodeGeneratorARM64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100459 GenBitCount(invoke, DataType::Type::kInt64, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000460}
461
462void IntrinsicLocationsBuilderARM64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100463 CreateIntToIntLocations(allocator_, invoke);
xueliang.zhong49924c92016-03-03 10:52:51 +0000464}
465
466void IntrinsicCodeGeneratorARM64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100467 GenBitCount(invoke, DataType::Type::kInt32, GetVIXLAssembler());
xueliang.zhong49924c92016-03-03 10:52:51 +0000468}
469
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100470static void GenHighestOneBit(HInvoke* invoke, DataType::Type type, MacroAssembler* masm) {
471 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100472
473 UseScratchRegisterScope temps(masm);
474
475 Register src = InputRegisterAt(invoke, 0);
476 Register dst = RegisterFrom(invoke->GetLocations()->Out(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100477 Register temp = (type == DataType::Type::kInt64) ? temps.AcquireX() : temps.AcquireW();
478 size_t high_bit = (type == DataType::Type::kInt64) ? 63u : 31u;
479 size_t clz_high_bit = (type == DataType::Type::kInt64) ? 6u : 5u;
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100480
481 __ Clz(temp, src);
482 __ Mov(dst, UINT64_C(1) << high_bit); // MOV (bitmask immediate)
483 __ Bic(dst, dst, Operand(temp, LSL, high_bit - clz_high_bit)); // Clear dst if src was 0.
484 __ Lsr(dst, dst, temp);
485}
486
487void IntrinsicLocationsBuilderARM64::VisitIntegerHighestOneBit(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::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100492 GenHighestOneBit(invoke, DataType::Type::kInt32, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100493}
494
495void IntrinsicLocationsBuilderARM64::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100496 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100497}
498
499void IntrinsicCodeGeneratorARM64::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100500 GenHighestOneBit(invoke, DataType::Type::kInt64, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100501}
502
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100503static void GenLowestOneBit(HInvoke* invoke, DataType::Type type, MacroAssembler* masm) {
504 DCHECK(type == DataType::Type::kInt32 || type == DataType::Type::kInt64);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100505
506 UseScratchRegisterScope temps(masm);
507
508 Register src = InputRegisterAt(invoke, 0);
509 Register dst = RegisterFrom(invoke->GetLocations()->Out(), type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100510 Register temp = (type == DataType::Type::kInt64) ? temps.AcquireX() : temps.AcquireW();
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100511
512 __ Neg(temp, src);
513 __ And(dst, temp, src);
514}
515
516void IntrinsicLocationsBuilderARM64::VisitIntegerLowestOneBit(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::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100521 GenLowestOneBit(invoke, DataType::Type::kInt32, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100522}
523
524void IntrinsicLocationsBuilderARM64::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100525 CreateIntToIntLocations(allocator_, invoke);
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100526}
527
528void IntrinsicCodeGeneratorARM64::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 GenLowestOneBit(invoke, DataType::Type::kInt64, GetVIXLAssembler());
Petre-Ionut Tudorda483162017-08-14 13:54:31 +0100530}
531
Andreas Gampe878d58c2015-01-15 23:24:00 -0800532static void GenMinMaxFP(LocationSummary* locations,
533 bool is_min,
534 bool is_double,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100535 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800536 Location op1 = locations->InAt(0);
537 Location op2 = locations->InAt(1);
538 Location out = locations->Out();
539
540 FPRegister op1_reg = is_double ? DRegisterFrom(op1) : SRegisterFrom(op1);
541 FPRegister op2_reg = is_double ? DRegisterFrom(op2) : SRegisterFrom(op2);
542 FPRegister out_reg = is_double ? DRegisterFrom(out) : SRegisterFrom(out);
543 if (is_min) {
544 __ Fmin(out_reg, op1_reg, op2_reg);
545 } else {
546 __ Fmax(out_reg, op1_reg, op2_reg);
547 }
548}
549
Vladimir Markoca6fff82017-10-03 14:49:14 +0100550static void CreateFPFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
551 LocationSummary* locations =
552 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800553 locations->SetInAt(0, Location::RequiresFpuRegister());
554 locations->SetInAt(1, Location::RequiresFpuRegister());
555 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
556}
557
558void IntrinsicLocationsBuilderARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100559 CreateFPFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800560}
561
562void IntrinsicCodeGeneratorARM64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000563 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800564}
565
566void IntrinsicLocationsBuilderARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100567 CreateFPFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800568}
569
570void IntrinsicCodeGeneratorARM64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000571 GenMinMaxFP(invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800572}
573
574void IntrinsicLocationsBuilderARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100575 CreateFPFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800576}
577
578void IntrinsicCodeGeneratorARM64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000579 GenMinMaxFP(invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800580}
581
582void IntrinsicLocationsBuilderARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100583 CreateFPFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800584}
585
586void IntrinsicCodeGeneratorARM64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000587 GenMinMaxFP(
588 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800589}
590
591static void GenMinMax(LocationSummary* locations,
592 bool is_min,
593 bool is_long,
Scott Wakeling97c72b72016-06-24 16:19:36 +0100594 MacroAssembler* masm) {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800595 Location op1 = locations->InAt(0);
596 Location op2 = locations->InAt(1);
597 Location out = locations->Out();
598
599 Register op1_reg = is_long ? XRegisterFrom(op1) : WRegisterFrom(op1);
600 Register op2_reg = is_long ? XRegisterFrom(op2) : WRegisterFrom(op2);
601 Register out_reg = is_long ? XRegisterFrom(out) : WRegisterFrom(out);
602
603 __ Cmp(op1_reg, op2_reg);
604 __ Csel(out_reg, op1_reg, op2_reg, is_min ? lt : gt);
605}
606
Andreas Gampe878d58c2015-01-15 23:24:00 -0800607void IntrinsicLocationsBuilderARM64::VisitMathMinIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100608 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800609}
610
611void IntrinsicCodeGeneratorARM64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000612 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800613}
614
615void IntrinsicLocationsBuilderARM64::VisitMathMinLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100616 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800617}
618
619void IntrinsicCodeGeneratorARM64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000620 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800621}
622
623void IntrinsicLocationsBuilderARM64::VisitMathMaxIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100624 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800625}
626
627void IntrinsicCodeGeneratorARM64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000628 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800629}
630
631void IntrinsicLocationsBuilderARM64::VisitMathMaxLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100632 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800633}
634
635void IntrinsicCodeGeneratorARM64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000636 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800637}
638
Aart Bik3dad3412018-02-28 12:01:46 -0800639static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
640 LocationSummary* locations =
641 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
642 locations->SetInAt(0, Location::RequiresFpuRegister());
643 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
644}
645
Andreas Gampe878d58c2015-01-15 23:24:00 -0800646void IntrinsicLocationsBuilderARM64::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800648}
649
650void IntrinsicCodeGeneratorARM64::VisitMathSqrt(HInvoke* invoke) {
651 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100652 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800653 __ Fsqrt(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
654}
655
656void IntrinsicLocationsBuilderARM64::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100657 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800658}
659
660void IntrinsicCodeGeneratorARM64::VisitMathCeil(HInvoke* invoke) {
661 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100662 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800663 __ Frintp(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
664}
665
666void IntrinsicLocationsBuilderARM64::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100667 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800668}
669
670void IntrinsicCodeGeneratorARM64::VisitMathFloor(HInvoke* invoke) {
671 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100672 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800673 __ Frintm(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
674}
675
676void IntrinsicLocationsBuilderARM64::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100677 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800678}
679
680void IntrinsicCodeGeneratorARM64::VisitMathRint(HInvoke* invoke) {
681 LocationSummary* locations = invoke->GetLocations();
Scott Wakeling97c72b72016-06-24 16:19:36 +0100682 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800683 __ Frintn(DRegisterFrom(locations->Out()), DRegisterFrom(locations->InAt(0)));
684}
685
Vladimir Markoca6fff82017-10-03 14:49:14 +0100686static void CreateFPToIntPlusFPTempLocations(ArenaAllocator* allocator, HInvoke* invoke) {
687 LocationSummary* locations =
688 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800689 locations->SetInAt(0, Location::RequiresFpuRegister());
690 locations->SetOut(Location::RequiresRegister());
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100691 locations->AddTemp(Location::RequiresFpuRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800692}
693
Scott Wakeling97c72b72016-06-24 16:19:36 +0100694static void GenMathRound(HInvoke* invoke, bool is_double, vixl::aarch64::MacroAssembler* masm) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100695 // Java 8 API definition for Math.round():
696 // Return the closest long or int to the argument, with ties rounding to positive infinity.
697 //
698 // There is no single instruction in ARMv8 that can support the above definition.
699 // We choose to use FCVTAS here, because it has closest semantic.
700 // FCVTAS performs rounding to nearest integer, ties away from zero.
701 // For most inputs (positive values, zero or NaN), this instruction is enough.
702 // We only need a few handling code after FCVTAS if the input is negative half value.
703 //
704 // The reason why we didn't choose FCVTPS instruction here is that
705 // although it performs rounding toward positive infinity, it doesn't perform rounding to nearest.
706 // For example, FCVTPS(-1.9) = -1 and FCVTPS(1.1) = 2.
707 // If we were using this instruction, for most inputs, more handling code would be needed.
708 LocationSummary* l = invoke->GetLocations();
709 FPRegister in_reg = is_double ? DRegisterFrom(l->InAt(0)) : SRegisterFrom(l->InAt(0));
710 FPRegister tmp_fp = is_double ? DRegisterFrom(l->GetTemp(0)) : SRegisterFrom(l->GetTemp(0));
711 Register out_reg = is_double ? XRegisterFrom(l->Out()) : WRegisterFrom(l->Out());
Scott Wakeling97c72b72016-06-24 16:19:36 +0100712 vixl::aarch64::Label done;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800713
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100714 // Round to nearest integer, ties away from zero.
715 __ Fcvtas(out_reg, in_reg);
716
717 // For positive values, zero or NaN inputs, rounding is done.
Scott Wakeling97c72b72016-06-24 16:19:36 +0100718 __ Tbz(out_reg, out_reg.GetSizeInBits() - 1, &done);
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100719
720 // Handle input < 0 cases.
721 // If input is negative but not a tie, previous result (round to nearest) is valid.
722 // If input is a negative tie, out_reg += 1.
723 __ Frinta(tmp_fp, in_reg);
724 __ Fsub(tmp_fp, in_reg, tmp_fp);
725 __ Fcmp(tmp_fp, 0.5);
726 __ Cinc(out_reg, out_reg, eq);
727
728 __ Bind(&done);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800729}
730
731void IntrinsicLocationsBuilderARM64::VisitMathRoundDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100732 CreateFPToIntPlusFPTempLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800733}
734
735void IntrinsicCodeGeneratorARM64::VisitMathRoundDouble(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100736 GenMathRound(invoke, /* is_double */ true, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800737}
738
739void IntrinsicLocationsBuilderARM64::VisitMathRoundFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100740 CreateFPToIntPlusFPTempLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800741}
742
743void IntrinsicCodeGeneratorARM64::VisitMathRoundFloat(HInvoke* invoke) {
xueliang.zhongd1e153c2016-05-27 18:56:13 +0100744 GenMathRound(invoke, /* is_double */ false, GetVIXLAssembler());
Andreas Gampe878d58c2015-01-15 23:24:00 -0800745}
746
747void IntrinsicLocationsBuilderARM64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100748 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800749}
750
751void IntrinsicCodeGeneratorARM64::VisitMemoryPeekByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100752 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800753 __ Ldrsb(WRegisterFrom(invoke->GetLocations()->Out()),
754 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
755}
756
757void IntrinsicLocationsBuilderARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100758 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800759}
760
761void IntrinsicCodeGeneratorARM64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100762 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800763 __ Ldr(WRegisterFrom(invoke->GetLocations()->Out()),
764 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
765}
766
767void IntrinsicLocationsBuilderARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100768 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800769}
770
771void IntrinsicCodeGeneratorARM64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100772 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800773 __ Ldr(XRegisterFrom(invoke->GetLocations()->Out()),
774 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
775}
776
777void IntrinsicLocationsBuilderARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100778 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800779}
780
781void IntrinsicCodeGeneratorARM64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100782 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800783 __ Ldrsh(WRegisterFrom(invoke->GetLocations()->Out()),
784 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
785}
786
Vladimir Markoca6fff82017-10-03 14:49:14 +0100787static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
788 LocationSummary* locations =
789 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800790 locations->SetInAt(0, Location::RequiresRegister());
791 locations->SetInAt(1, Location::RequiresRegister());
792}
793
794void IntrinsicLocationsBuilderARM64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100795 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800796}
797
798void IntrinsicCodeGeneratorARM64::VisitMemoryPokeByte(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100799 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800800 __ Strb(WRegisterFrom(invoke->GetLocations()->InAt(1)),
801 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
802}
803
804void IntrinsicLocationsBuilderARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100805 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800806}
807
808void IntrinsicCodeGeneratorARM64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100809 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800810 __ Str(WRegisterFrom(invoke->GetLocations()->InAt(1)),
811 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
812}
813
814void IntrinsicLocationsBuilderARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100815 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800816}
817
818void IntrinsicCodeGeneratorARM64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100819 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800820 __ Str(XRegisterFrom(invoke->GetLocations()->InAt(1)),
821 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
822}
823
824void IntrinsicLocationsBuilderARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100825 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800826}
827
828void IntrinsicCodeGeneratorARM64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +0100829 MacroAssembler* masm = GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800830 __ Strh(WRegisterFrom(invoke->GetLocations()->InAt(1)),
831 AbsoluteHeapOperandFrom(invoke->GetLocations()->InAt(0), 0));
832}
833
834void IntrinsicLocationsBuilderARM64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100835 LocationSummary* locations =
836 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800837 locations->SetOut(Location::RequiresRegister());
838}
839
840void IntrinsicCodeGeneratorARM64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100841 codegen_->Load(DataType::Type::kReference, WRegisterFrom(invoke->GetLocations()->Out()),
Andreas Gampe542451c2016-07-26 09:02:02 -0700842 MemOperand(tr, Thread::PeerOffset<kArm64PointerSize>().Int32Value()));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800843}
844
845static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100846 DataType::Type type,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800847 bool is_volatile,
848 CodeGeneratorARM64* codegen) {
849 LocationSummary* locations = invoke->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100850 DCHECK((type == DataType::Type::kInt32) ||
851 (type == DataType::Type::kInt64) ||
852 (type == DataType::Type::kReference));
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000853 Location base_loc = locations->InAt(1);
854 Register base = WRegisterFrom(base_loc); // Object pointer.
855 Location offset_loc = locations->InAt(2);
856 Register offset = XRegisterFrom(offset_loc); // Long offset.
857 Location trg_loc = locations->Out();
858 Register trg = RegisterFrom(trg_loc, type);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100860 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain44015862016-01-22 11:47:17 +0000861 // UnsafeGetObject/UnsafeGetObjectVolatile with Baker's read barrier case.
Roland Levillain54f869e2017-03-06 13:54:11 +0000862 Register temp = WRegisterFrom(locations->GetTemp(0));
Roland Levillainbfea3352016-06-23 13:48:47 +0100863 codegen->GenerateReferenceLoadWithBakerReadBarrier(invoke,
864 trg_loc,
865 base,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100866 /* offset */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100867 /* index */ offset_loc,
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100868 /* scale_factor */ 0u,
Roland Levillainbfea3352016-06-23 13:48:47 +0100869 temp,
870 /* needs_null_check */ false,
871 is_volatile);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800872 } else {
Roland Levillain44015862016-01-22 11:47:17 +0000873 // Other cases.
874 MemOperand mem_op(base.X(), offset);
875 if (is_volatile) {
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +0000876 codegen->LoadAcquire(invoke, trg, mem_op, /* needs_null_check */ true);
Roland Levillain44015862016-01-22 11:47:17 +0000877 } else {
878 codegen->Load(type, trg, mem_op);
879 }
Roland Levillain4d027112015-07-01 15:41:14 +0100880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100881 if (type == DataType::Type::kReference) {
Roland Levillain44015862016-01-22 11:47:17 +0000882 DCHECK(trg.IsW());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100883 codegen->MaybeGenerateReadBarrierSlow(invoke, trg_loc, trg_loc, base_loc, 0u, offset_loc);
Roland Levillain44015862016-01-22 11:47:17 +0000884 }
Roland Levillain4d027112015-07-01 15:41:14 +0100885 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800886}
887
Vladimir Markoca6fff82017-10-03 14:49:14 +0100888static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Roland Levillain22ccc3a2015-11-24 13:10:05 +0000889 bool can_call = kEmitCompilerReadBarrier &&
890 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
891 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100892 LocationSummary* locations =
893 new (allocator) LocationSummary(invoke,
894 can_call
895 ? LocationSummary::kCallOnSlowPath
896 : LocationSummary::kNoCall,
897 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +0100898 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +0100899 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Roland Levillain54f869e2017-03-06 13:54:11 +0000900 // We need a temporary register for the read barrier marking slow
901 // path in CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier.
902 locations->AddTemp(Location::RequiresRegister());
Vladimir Marko70e97462016-08-09 11:04:26 +0100903 }
Andreas Gampe878d58c2015-01-15 23:24:00 -0800904 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
905 locations->SetInAt(1, Location::RequiresRegister());
906 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillainbfea3352016-06-23 13:48:47 +0100907 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100908 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800909}
910
911void IntrinsicLocationsBuilderARM64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100912 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800913}
914void IntrinsicLocationsBuilderARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100915 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800916}
917void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100918 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800919}
920void IntrinsicLocationsBuilderARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100921 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800922}
923void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100924 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800925}
926void IntrinsicLocationsBuilderARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100927 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800928}
929
930void IntrinsicCodeGeneratorARM64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100931 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800932}
933void IntrinsicCodeGeneratorARM64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100934 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800935}
936void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100937 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800938}
939void IntrinsicCodeGeneratorARM64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100940 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800941}
942void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100943 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800944}
945void IntrinsicCodeGeneratorARM64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100946 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800947}
948
Vladimir Markoca6fff82017-10-03 14:49:14 +0100949static void CreateIntIntIntIntToVoid(ArenaAllocator* allocator, HInvoke* invoke) {
950 LocationSummary* locations =
951 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800952 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
953 locations->SetInAt(1, Location::RequiresRegister());
954 locations->SetInAt(2, Location::RequiresRegister());
955 locations->SetInAt(3, Location::RequiresRegister());
956}
957
958void IntrinsicLocationsBuilderARM64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100959 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800960}
961void IntrinsicLocationsBuilderARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100962 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800963}
964void IntrinsicLocationsBuilderARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100965 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800966}
967void IntrinsicLocationsBuilderARM64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100968 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800969}
970void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100971 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800972}
973void IntrinsicLocationsBuilderARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100974 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800975}
976void IntrinsicLocationsBuilderARM64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100977 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800978}
979void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100980 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800981}
982void IntrinsicLocationsBuilderARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100983 CreateIntIntIntIntToVoid(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -0800984}
985
Artem Serov914d7a82017-02-07 14:33:49 +0000986static void GenUnsafePut(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100987 DataType::Type type,
Andreas Gampe878d58c2015-01-15 23:24:00 -0800988 bool is_volatile,
989 bool is_ordered,
990 CodeGeneratorARM64* codegen) {
Artem Serov914d7a82017-02-07 14:33:49 +0000991 LocationSummary* locations = invoke->GetLocations();
Alexandre Rames087930f2016-08-02 13:45:28 +0100992 MacroAssembler* masm = codegen->GetVIXLAssembler();
Andreas Gampe878d58c2015-01-15 23:24:00 -0800993
994 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
995 Register offset = XRegisterFrom(locations->InAt(2)); // Long offset.
996 Register value = RegisterFrom(locations->InAt(3), type);
Roland Levillain4d027112015-07-01 15:41:14 +0100997 Register source = value;
Andreas Gampe878d58c2015-01-15 23:24:00 -0800998 MemOperand mem_op(base.X(), offset);
999
Roland Levillain4d027112015-07-01 15:41:14 +01001000 {
1001 // We use a block to end the scratch scope before the write barrier, thus
1002 // freeing the temporary registers so they can be used in `MarkGCCard`.
1003 UseScratchRegisterScope temps(masm);
1004
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001005 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001006 DCHECK(value.IsW());
1007 Register temp = temps.AcquireW();
1008 __ Mov(temp.W(), value.W());
1009 codegen->GetAssembler()->PoisonHeapReference(temp.W());
1010 source = temp;
Andreas Gampe878d58c2015-01-15 23:24:00 -08001011 }
Roland Levillain4d027112015-07-01 15:41:14 +01001012
1013 if (is_volatile || is_ordered) {
Artem Serov914d7a82017-02-07 14:33:49 +00001014 codegen->StoreRelease(invoke, type, source, mem_op, /* needs_null_check */ false);
Roland Levillain4d027112015-07-01 15:41:14 +01001015 } else {
1016 codegen->Store(type, source, mem_op);
1017 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001018 }
1019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001020 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001021 bool value_can_be_null = true; // TODO: Worth finding out this information?
1022 codegen->MarkGCCard(base, value, value_can_be_null);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001023 }
1024}
1025
1026void IntrinsicCodeGeneratorARM64::VisitUnsafePut(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001027 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001028 DataType::Type::kInt32,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001029 /* is_volatile */ false,
1030 /* is_ordered */ false,
1031 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001032}
1033void IntrinsicCodeGeneratorARM64::VisitUnsafePutOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001034 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001035 DataType::Type::kInt32,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001036 /* is_volatile */ false,
1037 /* is_ordered */ true,
1038 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001039}
1040void IntrinsicCodeGeneratorARM64::VisitUnsafePutVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001041 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001042 DataType::Type::kInt32,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001043 /* is_volatile */ true,
1044 /* is_ordered */ false,
1045 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001046}
1047void IntrinsicCodeGeneratorARM64::VisitUnsafePutObject(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001048 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001049 DataType::Type::kReference,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001050 /* is_volatile */ false,
1051 /* is_ordered */ false,
1052 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001053}
1054void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001055 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001056 DataType::Type::kReference,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001057 /* is_volatile */ false,
1058 /* is_ordered */ true,
1059 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001060}
1061void IntrinsicCodeGeneratorARM64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001062 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001063 DataType::Type::kReference,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001064 /* is_volatile */ true,
1065 /* is_ordered */ false,
1066 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001067}
1068void IntrinsicCodeGeneratorARM64::VisitUnsafePutLong(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001069 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001070 DataType::Type::kInt64,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001071 /* is_volatile */ false,
1072 /* is_ordered */ false,
1073 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001074}
1075void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001076 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001077 DataType::Type::kInt64,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001078 /* is_volatile */ false,
1079 /* is_ordered */ true,
1080 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001081}
1082void IntrinsicCodeGeneratorARM64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Artem Serov914d7a82017-02-07 14:33:49 +00001083 GenUnsafePut(invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001084 DataType::Type::kInt64,
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001085 /* is_volatile */ true,
1086 /* is_ordered */ false,
1087 codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001088}
1089
Vladimir Markoca6fff82017-10-03 14:49:14 +01001090static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001091 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001092 DataType::Type type) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001093 bool can_call = kEmitCompilerReadBarrier &&
1094 kUseBakerReadBarrier &&
1095 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001096 LocationSummary* locations =
1097 new (allocator) LocationSummary(invoke,
1098 can_call
1099 ? LocationSummary::kCallOnSlowPath
1100 : LocationSummary::kNoCall,
1101 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001102 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1103 locations->SetInAt(1, Location::RequiresRegister());
1104 locations->SetInAt(2, Location::RequiresRegister());
1105 locations->SetInAt(3, Location::RequiresRegister());
1106 locations->SetInAt(4, Location::RequiresRegister());
1107
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001108 // If heap poisoning is enabled, we don't want the unpoisoning
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001109 // operations to potentially clobber the output. Likewise when
1110 // emitting a (Baker) read barrier, which may call.
1111 Location::OutputOverlap overlaps =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001112 ((kPoisonHeapReferences && type == DataType::Type::kReference) || can_call)
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001113 ? Location::kOutputOverlap
1114 : Location::kNoOutputOverlap;
1115 locations->SetOut(Location::RequiresRegister(), overlaps);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001116 if (type == DataType::Type::kReference && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001117 // Temporary register for (Baker) read barrier.
1118 locations->AddTemp(Location::RequiresRegister());
1119 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001120}
1121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001122static void GenCas(HInvoke* invoke, DataType::Type type, CodeGeneratorARM64* codegen) {
Alexandre Rames087930f2016-08-02 13:45:28 +01001123 MacroAssembler* masm = codegen->GetVIXLAssembler();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001124 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe878d58c2015-01-15 23:24:00 -08001125
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001126 Location out_loc = locations->Out();
1127 Register out = WRegisterFrom(out_loc); // Boolean result.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001128
1129 Register base = WRegisterFrom(locations->InAt(1)); // Object pointer.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001130 Location offset_loc = locations->InAt(2);
1131 Register offset = XRegisterFrom(offset_loc); // Long offset.
Andreas Gampe878d58c2015-01-15 23:24:00 -08001132 Register expected = RegisterFrom(locations->InAt(3), type); // Expected.
1133 Register value = RegisterFrom(locations->InAt(4), type); // Value.
1134
1135 // This needs to be before the temp registers, as MarkGCCard also uses VIXL temps.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001136 if (type == DataType::Type::kReference) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001137 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001138 bool value_can_be_null = true; // TODO: Worth finding out this information?
1139 codegen->MarkGCCard(base, value, value_can_be_null);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001140
1141 // The only read barrier implementation supporting the
1142 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1143 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1144
1145 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1146 Register temp = WRegisterFrom(locations->GetTemp(0));
1147 // Need to make sure the reference stored in the field is a to-space
1148 // one before attempting the CAS or the CAS could fail incorrectly.
Roland Levillainff487002017-03-07 16:50:01 +00001149 codegen->UpdateReferenceFieldWithBakerReadBarrier(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001150 invoke,
1151 out_loc, // Unused, used only as a "temporary" within the read barrier.
1152 base,
Roland Levillainff487002017-03-07 16:50:01 +00001153 /* field_offset */ offset_loc,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001154 temp,
1155 /* needs_null_check */ false,
Roland Levillainff487002017-03-07 16:50:01 +00001156 /* use_load_acquire */ false);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001157 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001158 }
1159
1160 UseScratchRegisterScope temps(masm);
1161 Register tmp_ptr = temps.AcquireX(); // Pointer to actual memory.
1162 Register tmp_value = temps.AcquireSameSizeAs(value); // Value in memory.
1163
1164 Register tmp_32 = tmp_value.W();
1165
1166 __ Add(tmp_ptr, base.X(), Operand(offset));
1167
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001168 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001169 codegen->GetAssembler()->PoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001170 if (value.Is(expected)) {
1171 // Do not poison `value`, as it is the same register as
1172 // `expected`, which has just been poisoned.
1173 } else {
1174 codegen->GetAssembler()->PoisonHeapReference(value);
1175 }
Roland Levillain4d027112015-07-01 15:41:14 +01001176 }
1177
Andreas Gampe878d58c2015-01-15 23:24:00 -08001178 // do {
1179 // tmp_value = [tmp_ptr] - expected;
1180 // } while (tmp_value == 0 && failure([tmp_ptr] <- r_new_value));
1181 // result = tmp_value != 0;
1182
Scott Wakeling97c72b72016-06-24 16:19:36 +01001183 vixl::aarch64::Label loop_head, exit_loop;
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001184 __ Bind(&loop_head);
Serban Constantinescu4a6a67c2016-01-27 09:19:56 +00001185 __ Ldaxr(tmp_value, MemOperand(tmp_ptr));
1186 __ Cmp(tmp_value, expected);
1187 __ B(&exit_loop, ne);
1188 __ Stlxr(tmp_32, value, MemOperand(tmp_ptr));
1189 __ Cbnz(tmp_32, &loop_head);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001190 __ Bind(&exit_loop);
1191 __ Cset(out, eq);
Roland Levillain4d027112015-07-01 15:41:14 +01001192
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001194 codegen->GetAssembler()->UnpoisonHeapReference(expected);
Roland Levillain2e50ecb2016-01-27 14:08:33 +00001195 if (value.Is(expected)) {
1196 // Do not unpoison `value`, as it is the same register as
1197 // `expected`, which has just been unpoisoned.
1198 } else {
1199 codegen->GetAssembler()->UnpoisonHeapReference(value);
1200 }
Roland Levillain4d027112015-07-01 15:41:14 +01001201 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001202}
1203
1204void IntrinsicLocationsBuilderARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001205 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kInt32);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001206}
1207void IntrinsicLocationsBuilderARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001208 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kInt64);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001209}
1210void IntrinsicLocationsBuilderARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001211 // The only read barrier implementation supporting the
1212 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1213 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain985ff702015-10-23 13:25:35 +01001214 return;
1215 }
1216
Vladimir Markoca6fff82017-10-03 14:49:14 +01001217 CreateIntIntIntIntIntToInt(allocator_, invoke, DataType::Type::kReference);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001218}
1219
1220void IntrinsicCodeGeneratorARM64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001221 GenCas(invoke, DataType::Type::kInt32, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001222}
1223void IntrinsicCodeGeneratorARM64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 GenCas(invoke, DataType::Type::kInt64, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001225}
1226void IntrinsicCodeGeneratorARM64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001227 // The only read barrier implementation supporting the
1228 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1229 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001231 GenCas(invoke, DataType::Type::kReference, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001232}
1233
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001234void IntrinsicLocationsBuilderARM64::VisitStringCompareTo(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001235 LocationSummary* locations =
1236 new (allocator_) LocationSummary(invoke,
1237 invoke->InputAt(1)->CanBeNull()
1238 ? LocationSummary::kCallOnSlowPath
1239 : LocationSummary::kNoCall,
1240 kIntrinsified);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001241 locations->SetInAt(0, Location::RequiresRegister());
1242 locations->SetInAt(1, Location::RequiresRegister());
1243 locations->AddTemp(Location::RequiresRegister());
1244 locations->AddTemp(Location::RequiresRegister());
1245 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001246 // Need temporary registers for String compression's feature.
1247 if (mirror::kUseStringCompression) {
1248 locations->AddTemp(Location::RequiresRegister());
jessicahandojo05765752016-09-09 19:01:32 -07001249 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001250 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001251}
1252
1253void IntrinsicCodeGeneratorARM64::VisitStringCompareTo(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001254 MacroAssembler* masm = GetVIXLAssembler();
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001255 LocationSummary* locations = invoke->GetLocations();
1256
Alexandre Rames2ea91532016-08-11 17:04:14 +01001257 Register str = InputRegisterAt(invoke, 0);
1258 Register arg = InputRegisterAt(invoke, 1);
1259 DCHECK(str.IsW());
1260 DCHECK(arg.IsW());
Scott Wakeling1f36f412016-04-21 11:13:45 +01001261 Register out = OutputRegister(invoke);
1262
1263 Register temp0 = WRegisterFrom(locations->GetTemp(0));
1264 Register temp1 = WRegisterFrom(locations->GetTemp(1));
1265 Register temp2 = WRegisterFrom(locations->GetTemp(2));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001266 Register temp3;
jessicahandojo05765752016-09-09 19:01:32 -07001267 if (mirror::kUseStringCompression) {
1268 temp3 = WRegisterFrom(locations->GetTemp(3));
jessicahandojo05765752016-09-09 19:01:32 -07001269 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001270
Scott Wakeling97c72b72016-06-24 16:19:36 +01001271 vixl::aarch64::Label loop;
1272 vixl::aarch64::Label find_char_diff;
1273 vixl::aarch64::Label end;
jessicahandojo05765752016-09-09 19:01:32 -07001274 vixl::aarch64::Label different_compression;
Scott Wakeling1f36f412016-04-21 11:13:45 +01001275
1276 // Get offsets of count and value fields within a string object.
1277 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1278 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1279
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001280 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001281 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001282
Scott Wakeling1f36f412016-04-21 11:13:45 +01001283 // Take slow path and throw if input can be and is null.
1284 SlowPathCodeARM64* slow_path = nullptr;
1285 const bool can_slow_path = invoke->InputAt(1)->CanBeNull();
1286 if (can_slow_path) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001287 slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001288 codegen_->AddSlowPath(slow_path);
1289 __ Cbz(arg, slow_path->GetEntryLabel());
1290 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001291
Scott Wakeling1f36f412016-04-21 11:13:45 +01001292 // Reference equality check, return 0 if same reference.
1293 __ Subs(out, str, arg);
1294 __ B(&end, eq);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001295
jessicahandojo05765752016-09-09 19:01:32 -07001296 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001297 // Load `count` fields of this and argument strings.
jessicahandojo05765752016-09-09 19:01:32 -07001298 __ Ldr(temp3, HeapOperand(str, count_offset));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001299 __ Ldr(temp2, HeapOperand(arg, count_offset));
jessicahandojo05765752016-09-09 19:01:32 -07001300 // Clean out compression flag from lengths.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001301 __ Lsr(temp0, temp3, 1u);
1302 __ Lsr(temp1, temp2, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001303 } else {
1304 // Load lengths of this and argument strings.
1305 __ Ldr(temp0, HeapOperand(str, count_offset));
1306 __ Ldr(temp1, HeapOperand(arg, count_offset));
1307 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001308 // out = length diff.
1309 __ Subs(out, temp0, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001310 // temp0 = min(len(str), len(arg)).
1311 __ Csel(temp0, temp1, temp0, ge);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001312 // Shorter string is empty?
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001313 __ Cbz(temp0, &end);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001314
jessicahandojo05765752016-09-09 19:01:32 -07001315 if (mirror::kUseStringCompression) {
1316 // Check if both strings using same compression style to use this comparison loop.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001317 __ Eor(temp2, temp2, Operand(temp3));
1318 // Interleave with compression flag extraction which is needed for both paths
1319 // and also set flags which is needed only for the different compressions path.
1320 __ Ands(temp3.W(), temp3.W(), Operand(1));
1321 __ Tbnz(temp2, 0, &different_compression); // Does not use flags.
jessicahandojo05765752016-09-09 19:01:32 -07001322 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001323 // Store offset of string value in preparation for comparison loop.
1324 __ Mov(temp1, value_offset);
jessicahandojo05765752016-09-09 19:01:32 -07001325 if (mirror::kUseStringCompression) {
1326 // For string compression, calculate the number of bytes to compare (not chars).
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001327 // This could in theory exceed INT32_MAX, so treat temp0 as unsigned.
1328 __ Lsl(temp0, temp0, temp3);
jessicahandojo05765752016-09-09 19:01:32 -07001329 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001330
1331 UseScratchRegisterScope scratch_scope(masm);
1332 Register temp4 = scratch_scope.AcquireX();
1333
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001334 // Assertions that must hold in order to compare strings 8 bytes at a time.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001335 DCHECK_ALIGNED(value_offset, 8);
1336 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001338 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001339 DCHECK_EQ(char_size, 2u);
1340
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001341 // Promote temp2 to an X reg, ready for LDR.
1342 temp2 = temp2.X();
Scott Wakeling1f36f412016-04-21 11:13:45 +01001343
1344 // Loop to compare 4x16-bit characters at a time (ok because of string data alignment).
1345 __ Bind(&loop);
Alexandre Rames2ea91532016-08-11 17:04:14 +01001346 __ Ldr(temp4, MemOperand(str.X(), temp1.X()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001347 __ Ldr(temp2, MemOperand(arg.X(), temp1.X()));
1348 __ Cmp(temp4, temp2);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001349 __ B(ne, &find_char_diff);
1350 __ Add(temp1, temp1, char_size * 4);
jessicahandojo05765752016-09-09 19:01:32 -07001351 // With string compression, we have compared 8 bytes, otherwise 4 chars.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001352 __ Subs(temp0, temp0, (mirror::kUseStringCompression) ? 8 : 4);
1353 __ B(&loop, hi);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001354 __ B(&end);
1355
1356 // Promote temp1 to an X reg, ready for EOR.
1357 temp1 = temp1.X();
1358
jessicahandojo05765752016-09-09 19:01:32 -07001359 // Find the single character difference.
Scott Wakeling1f36f412016-04-21 11:13:45 +01001360 __ Bind(&find_char_diff);
1361 // Get the bit position of the first character that differs.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001362 __ Eor(temp1, temp2, temp4);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001363 __ Rbit(temp1, temp1);
1364 __ Clz(temp1, temp1);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001365
jessicahandojo05765752016-09-09 19:01:32 -07001366 // If the number of chars remaining <= the index where the difference occurs (0-3), then
Scott Wakeling1f36f412016-04-21 11:13:45 +01001367 // the difference occurs outside the remaining string data, so just return length diff (out).
jessicahandojo05765752016-09-09 19:01:32 -07001368 // Unlike ARM, we're doing the comparison in one go here, without the subtraction at the
1369 // find_char_diff_2nd_cmp path, so it doesn't matter whether the comparison is signed or
1370 // unsigned when string compression is disabled.
1371 // When it's enabled, the comparison must be unsigned.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001372 __ Cmp(temp0, Operand(temp1.W(), LSR, (mirror::kUseStringCompression) ? 3 : 4));
jessicahandojo05765752016-09-09 19:01:32 -07001373 __ B(ls, &end);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001374
Scott Wakeling1f36f412016-04-21 11:13:45 +01001375 // Extract the characters and calculate the difference.
jessicahandojo05765752016-09-09 19:01:32 -07001376 if (mirror:: kUseStringCompression) {
jessicahandojo05765752016-09-09 19:01:32 -07001377 __ Bic(temp1, temp1, 0x7);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001378 __ Bic(temp1, temp1, Operand(temp3.X(), LSL, 3u));
1379 } else {
1380 __ Bic(temp1, temp1, 0xf);
jessicahandojo05765752016-09-09 19:01:32 -07001381 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001382 __ Lsr(temp2, temp2, temp1);
Scott Wakeling1f36f412016-04-21 11:13:45 +01001383 __ Lsr(temp4, temp4, temp1);
jessicahandojo05765752016-09-09 19:01:32 -07001384 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001385 // Prioritize the case of compressed strings and calculate such result first.
1386 __ Uxtb(temp1, temp4);
1387 __ Sub(out, temp1.W(), Operand(temp2.W(), UXTB));
1388 __ Tbz(temp3, 0u, &end); // If actually compressed, we're done.
jessicahandojo05765752016-09-09 19:01:32 -07001389 }
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001390 __ Uxth(temp4, temp4);
1391 __ Sub(out, temp4.W(), Operand(temp2.W(), UXTH));
jessicahandojo05765752016-09-09 19:01:32 -07001392
1393 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001394 __ B(&end);
1395 __ Bind(&different_compression);
1396
1397 // Comparison for different compression style.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001398 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo05765752016-09-09 19:01:32 -07001399 DCHECK_EQ(c_char_size, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07001400 temp1 = temp1.W();
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001401 temp2 = temp2.W();
1402 temp4 = temp4.W();
jessicahandojo05765752016-09-09 19:01:32 -07001403
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001404 // `temp1` will hold the compressed data pointer, `temp2` the uncompressed data pointer.
1405 // Note that flags have been set by the `str` compression flag extraction to `temp3`
1406 // before branching to the `different_compression` label.
1407 __ Csel(temp1, str, arg, eq); // Pointer to the compressed string.
1408 __ Csel(temp2, str, arg, ne); // Pointer to the uncompressed string.
jessicahandojo05765752016-09-09 19:01:32 -07001409
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001410 // We want to free up the temp3, currently holding `str` compression flag, for comparison.
1411 // So, we move it to the bottom bit of the iteration count `temp0` which we then need to treat
1412 // as unsigned. Start by freeing the bit with a LSL and continue further down by a SUB which
1413 // will allow `subs temp0, #2; bhi different_compression_loop` to serve as the loop condition.
1414 __ Lsl(temp0, temp0, 1u);
1415
1416 // Adjust temp1 and temp2 from string pointers to data pointers.
1417 __ Add(temp1, temp1, Operand(value_offset));
1418 __ Add(temp2, temp2, Operand(value_offset));
1419
1420 // Complete the move of the compression flag.
1421 __ Sub(temp0, temp0, Operand(temp3));
1422
1423 vixl::aarch64::Label different_compression_loop;
1424 vixl::aarch64::Label different_compression_diff;
1425
1426 __ Bind(&different_compression_loop);
1427 __ Ldrb(temp4, MemOperand(temp1.X(), c_char_size, PostIndex));
1428 __ Ldrh(temp3, MemOperand(temp2.X(), char_size, PostIndex));
1429 __ Subs(temp4, temp4, Operand(temp3));
1430 __ B(&different_compression_diff, ne);
1431 __ Subs(temp0, temp0, 2);
1432 __ B(&different_compression_loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001433 __ B(&end);
1434
1435 // Calculate the difference.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001436 __ Bind(&different_compression_diff);
1437 __ Tst(temp0, Operand(1));
1438 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1439 "Expecting 0=compressed, 1=uncompressed");
1440 __ Cneg(out, temp4, ne);
jessicahandojo05765752016-09-09 19:01:32 -07001441 }
Scott Wakeling1f36f412016-04-21 11:13:45 +01001442
1443 __ Bind(&end);
1444
1445 if (can_slow_path) {
1446 __ Bind(slow_path->GetExitLabel());
1447 }
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001448}
1449
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001450// The cut off for unrolling the loop in String.equals() intrinsic for const strings.
1451// The normal loop plus the pre-header is 9 instructions without string compression and 12
1452// instructions with string compression. We can compare up to 8 bytes in 4 instructions
1453// (LDR+LDR+CMP+BNE) and up to 16 bytes in 5 instructions (LDP+LDP+CMP+CCMP+BNE). Allow up
1454// to 10 instructions for the unrolled loop.
1455constexpr size_t kShortConstStringEqualsCutoffInBytes = 32;
1456
1457static const char* GetConstString(HInstruction* candidate, uint32_t* utf16_length) {
1458 if (candidate->IsLoadString()) {
1459 HLoadString* load_string = candidate->AsLoadString();
1460 const DexFile& dex_file = load_string->GetDexFile();
1461 return dex_file.StringDataAndUtf16LengthByIdx(load_string->GetStringIndex(), utf16_length);
1462 }
1463 return nullptr;
1464}
1465
Agi Csakiea34b402015-08-13 17:51:19 -07001466void IntrinsicLocationsBuilderARM64::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001467 if (kEmitCompilerReadBarrier &&
1468 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1469 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1470 // No support for this odd case (String class is moveable, not in the boot image).
1471 return;
1472 }
1473
Vladimir Markoca6fff82017-10-03 14:49:14 +01001474 LocationSummary* locations =
1475 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakiea34b402015-08-13 17:51:19 -07001476 locations->SetInAt(0, Location::RequiresRegister());
1477 locations->SetInAt(1, Location::RequiresRegister());
Agi Csakiea34b402015-08-13 17:51:19 -07001478
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001479 // For the generic implementation and for long const strings we need a temporary.
1480 // We do not need it for short const strings, up to 8 bytes, see code generation below.
1481 uint32_t const_string_length = 0u;
1482 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1483 if (const_string == nullptr) {
1484 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1485 }
1486 bool is_compressed =
1487 mirror::kUseStringCompression &&
1488 const_string != nullptr &&
1489 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1490 if (const_string == nullptr || const_string_length > (is_compressed ? 8u : 4u)) {
1491 locations->AddTemp(Location::RequiresRegister());
1492 }
1493
1494 // TODO: If the String.equals() is used only for an immediately following HIf, we can
1495 // mark it as emitted-at-use-site and emit branches directly to the appropriate blocks.
1496 // Then we shall need an extra temporary register instead of the output register.
Agi Csakiea34b402015-08-13 17:51:19 -07001497 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1498}
1499
1500void IntrinsicCodeGeneratorARM64::VisitStringEquals(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001501 MacroAssembler* masm = GetVIXLAssembler();
Agi Csakiea34b402015-08-13 17:51:19 -07001502 LocationSummary* locations = invoke->GetLocations();
1503
1504 Register str = WRegisterFrom(locations->InAt(0));
1505 Register arg = WRegisterFrom(locations->InAt(1));
1506 Register out = XRegisterFrom(locations->Out());
1507
1508 UseScratchRegisterScope scratch_scope(masm);
1509 Register temp = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001510 Register temp1 = scratch_scope.AcquireW();
Agi Csakiea34b402015-08-13 17:51:19 -07001511
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001512 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01001513 vixl::aarch64::Label end;
1514 vixl::aarch64::Label return_true;
1515 vixl::aarch64::Label return_false;
Agi Csakiea34b402015-08-13 17:51:19 -07001516
1517 // Get offsets of count, value, and class fields within a string object.
1518 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
1519 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1520 const int32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1521
1522 // Note that the null check must have been done earlier.
1523 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1524
Vladimir Marko53b52002016-05-24 19:30:45 +01001525 StringEqualsOptimizations optimizations(invoke);
1526 if (!optimizations.GetArgumentNotNull()) {
1527 // Check if input is null, return false if it is.
1528 __ Cbz(arg, &return_false);
1529 }
Agi Csakiea34b402015-08-13 17:51:19 -07001530
1531 // Reference equality check, return true if same reference.
1532 __ Cmp(str, arg);
1533 __ B(&return_true, eq);
1534
Vladimir Marko53b52002016-05-24 19:30:45 +01001535 if (!optimizations.GetArgumentIsString()) {
1536 // Instanceof check for the argument by comparing class fields.
1537 // All string objects must have the same type since String cannot be subclassed.
1538 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1539 // If the argument is a string object, its class field must be equal to receiver's class field.
1540 __ Ldr(temp, MemOperand(str.X(), class_offset));
1541 __ Ldr(temp1, MemOperand(arg.X(), class_offset));
1542 __ Cmp(temp, temp1);
1543 __ B(&return_false, ne);
1544 }
Agi Csakiea34b402015-08-13 17:51:19 -07001545
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001546 // Check if one of the inputs is a const string. Do not special-case both strings
1547 // being const, such cases should be handled by constant folding if needed.
1548 uint32_t const_string_length = 0u;
1549 const char* const_string = GetConstString(invoke->InputAt(0), &const_string_length);
1550 if (const_string == nullptr) {
1551 const_string = GetConstString(invoke->InputAt(1), &const_string_length);
1552 if (const_string != nullptr) {
1553 std::swap(str, arg); // Make sure the const string is in `str`.
1554 }
1555 }
1556 bool is_compressed =
1557 mirror::kUseStringCompression &&
1558 const_string != nullptr &&
1559 mirror::String::DexFileStringAllASCII(const_string, const_string_length);
1560
1561 if (const_string != nullptr) {
1562 // Load `count` field of the argument string and check if it matches the const string.
1563 // Also compares the compression style, if differs return false.
1564 __ Ldr(temp, MemOperand(arg.X(), count_offset));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001565 // Temporarily release temp1 as we may not be able to embed the flagged count in CMP immediate.
1566 scratch_scope.Release(temp1);
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001567 __ Cmp(temp, Operand(mirror::String::GetFlaggedCount(const_string_length, is_compressed)));
Vladimir Marko26ec3ca2017-03-14 13:37:14 +00001568 temp1 = scratch_scope.AcquireW();
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001569 __ B(&return_false, ne);
1570 } else {
1571 // Load `count` fields of this and argument strings.
1572 __ Ldr(temp, MemOperand(str.X(), count_offset));
1573 __ Ldr(temp1, MemOperand(arg.X(), count_offset));
1574 // Check if `count` fields are equal, return false if they're not.
1575 // Also compares the compression style, if differs return false.
1576 __ Cmp(temp, temp1);
1577 __ B(&return_false, ne);
1578 }
Agi Csakiea34b402015-08-13 17:51:19 -07001579
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001580 // Assertions that must hold in order to compare strings 8 bytes at a time.
Vladimir Marko984519c2017-08-23 10:45:29 +01001581 // Ok to do this because strings are zero-padded to kObjectAlignment.
Agi Csakiea34b402015-08-13 17:51:19 -07001582 DCHECK_ALIGNED(value_offset, 8);
1583 static_assert(IsAligned<8>(kObjectAlignment), "String of odd length is not zero padded");
1584
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001585 if (const_string != nullptr &&
Vladimir Marko984519c2017-08-23 10:45:29 +01001586 const_string_length <= (is_compressed ? kShortConstStringEqualsCutoffInBytes
1587 : kShortConstStringEqualsCutoffInBytes / 2u)) {
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001588 // Load and compare the contents. Though we know the contents of the short const string
1589 // at compile time, materializing constants may be more code than loading from memory.
1590 int32_t offset = value_offset;
1591 size_t remaining_bytes =
1592 RoundUp(is_compressed ? const_string_length : const_string_length * 2u, 8u);
1593 temp = temp.X();
1594 temp1 = temp1.X();
Vladimir Marko984519c2017-08-23 10:45:29 +01001595 while (remaining_bytes > sizeof(uint64_t)) {
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001596 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1597 __ Ldp(temp, temp1, MemOperand(str.X(), offset));
1598 __ Ldp(temp2, out, MemOperand(arg.X(), offset));
1599 __ Cmp(temp, temp2);
1600 __ Ccmp(temp1, out, NoFlag, eq);
1601 __ B(&return_false, ne);
1602 offset += 2u * sizeof(uint64_t);
1603 remaining_bytes -= 2u * sizeof(uint64_t);
1604 }
1605 if (remaining_bytes != 0u) {
1606 __ Ldr(temp, MemOperand(str.X(), offset));
1607 __ Ldr(temp1, MemOperand(arg.X(), offset));
1608 __ Cmp(temp, temp1);
1609 __ B(&return_false, ne);
1610 }
1611 } else {
1612 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1613 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1614 "Expecting 0=compressed, 1=uncompressed");
1615 __ Cbz(temp, &return_true);
1616
1617 if (mirror::kUseStringCompression) {
1618 // For string compression, calculate the number of bytes to compare (not chars).
1619 // This could in theory exceed INT32_MAX, so treat temp as unsigned.
1620 __ And(temp1, temp, Operand(1)); // Extract compression flag.
1621 __ Lsr(temp, temp, 1u); // Extract length.
1622 __ Lsl(temp, temp, temp1); // Calculate number of bytes to compare.
1623 }
1624
1625 // Store offset of string value in preparation for comparison loop
1626 __ Mov(temp1, value_offset);
1627
1628 temp1 = temp1.X();
1629 Register temp2 = XRegisterFrom(locations->GetTemp(0));
1630 // Loop to compare strings 8 bytes at a time starting at the front of the string.
Vladimir Markoe39f14f2017-02-10 15:44:25 +00001631 __ Bind(&loop);
1632 __ Ldr(out, MemOperand(str.X(), temp1));
1633 __ Ldr(temp2, MemOperand(arg.X(), temp1));
1634 __ Add(temp1, temp1, Operand(sizeof(uint64_t)));
1635 __ Cmp(out, temp2);
1636 __ B(&return_false, ne);
1637 // With string compression, we have compared 8 bytes, otherwise 4 chars.
1638 __ Sub(temp, temp, Operand(mirror::kUseStringCompression ? 8 : 4), SetFlags);
1639 __ B(&loop, hi);
jessicahandojo05765752016-09-09 19:01:32 -07001640 }
1641
Agi Csakiea34b402015-08-13 17:51:19 -07001642 // Return true and exit the function.
1643 // If loop does not result in returning false, we return true.
1644 __ Bind(&return_true);
1645 __ Mov(out, 1);
1646 __ B(&end);
1647
1648 // Return false and exit the function.
1649 __ Bind(&return_false);
1650 __ Mov(out, 0);
1651 __ Bind(&end);
1652}
1653
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001654static void GenerateVisitStringIndexOf(HInvoke* invoke,
Scott Wakeling97c72b72016-06-24 16:19:36 +01001655 MacroAssembler* masm,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001656 CodeGeneratorARM64* codegen,
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001657 bool start_at_zero) {
1658 LocationSummary* locations = invoke->GetLocations();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001659
1660 // Note that the null check must have been done earlier.
1661 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1662
1663 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001664 // 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 -07001665 SlowPathCodeARM64* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001666 HInstruction* code_point = invoke->InputAt(1);
1667 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001668 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) > 0xFFFFU) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001669 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1670 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001671 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001672 codegen->AddSlowPath(slow_path);
1673 __ B(slow_path->GetEntryLabel());
1674 __ Bind(slow_path->GetExitLabel());
1675 return;
1676 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001677 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001678 Register char_reg = WRegisterFrom(locations->InAt(1));
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001679 __ Tst(char_reg, 0xFFFF0000);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001680 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001681 codegen->AddSlowPath(slow_path);
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001682 __ B(ne, slow_path->GetEntryLabel());
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001683 }
1684
1685 if (start_at_zero) {
1686 // Start-index = 0.
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001687 Register tmp_reg = WRegisterFrom(locations->GetTemp(0));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001688 __ Mov(tmp_reg, 0);
1689 }
1690
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001691 codegen->InvokeRuntime(kQuickIndexOf, invoke, invoke->GetDexPc(), slow_path);
Roland Levillain42ad2882016-02-29 18:26:54 +00001692 CheckEntrypointTypes<kQuickIndexOf, int32_t, void*, uint32_t, uint32_t>();
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001693
1694 if (slow_path != nullptr) {
1695 __ Bind(slow_path->GetExitLabel());
1696 }
1697}
1698
1699void IntrinsicLocationsBuilderARM64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001700 LocationSummary* locations = new (allocator_) LocationSummary(
1701 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001702 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1703 // best to align the inputs accordingly.
1704 InvokeRuntimeCallingConvention calling_convention;
1705 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1706 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt32));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001708
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001709 // Need to send start_index=0.
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001710 locations->AddTemp(LocationFrom(calling_convention.GetRegisterAt(2)));
1711}
1712
1713void IntrinsicCodeGeneratorARM64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001714 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001715}
1716
1717void IntrinsicLocationsBuilderARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001718 LocationSummary* locations = new (allocator_) LocationSummary(
1719 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001720 // We have a hand-crafted assembly stub that follows the runtime calling convention. So it's
1721 // best to align the inputs accordingly.
1722 InvokeRuntimeCallingConvention calling_convention;
1723 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1724 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1725 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001726 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt32));
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001727}
1728
1729void IntrinsicCodeGeneratorARM64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001730 GenerateVisitStringIndexOf(invoke, GetVIXLAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -07001731}
1732
Jeff Hao848f70a2014-01-15 13:49:50 -08001733void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001734 LocationSummary* locations = new (allocator_) LocationSummary(
1735 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001736 InvokeRuntimeCallingConvention calling_convention;
1737 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1738 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1739 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
1740 locations->SetInAt(3, LocationFrom(calling_convention.GetRegisterAt(3)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001741 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001742}
1743
1744void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001745 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001746 LocationSummary* locations = invoke->GetLocations();
1747
1748 Register byte_array = WRegisterFrom(locations->InAt(0));
1749 __ Cmp(byte_array, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001750 SlowPathCodeARM64* slow_path =
1751 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001752 codegen_->AddSlowPath(slow_path);
1753 __ B(eq, slow_path->GetEntryLabel());
1754
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001755 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001756 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001757 __ Bind(slow_path->GetExitLabel());
1758}
1759
1760void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001761 LocationSummary* locations =
1762 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001763 InvokeRuntimeCallingConvention calling_convention;
1764 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
1765 locations->SetInAt(1, LocationFrom(calling_convention.GetRegisterAt(1)));
1766 locations->SetInAt(2, LocationFrom(calling_convention.GetRegisterAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001767 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001768}
1769
1770void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001771 // No need to emit code checking whether `locations->InAt(2)` is a null
1772 // pointer, as callers of the native method
1773 //
1774 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1775 //
1776 // all include a null check on `data` before calling that method.
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001777 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001778 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001779}
1780
1781void IntrinsicLocationsBuilderARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001782 LocationSummary* locations = new (allocator_) LocationSummary(
1783 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001784 InvokeRuntimeCallingConvention calling_convention;
1785 locations->SetInAt(0, LocationFrom(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001786 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Jeff Hao848f70a2014-01-15 13:49:50 -08001787}
1788
1789void IntrinsicCodeGeneratorARM64::VisitStringNewStringFromString(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001790 MacroAssembler* masm = GetVIXLAssembler();
Jeff Hao848f70a2014-01-15 13:49:50 -08001791 LocationSummary* locations = invoke->GetLocations();
1792
1793 Register string_to_copy = WRegisterFrom(locations->InAt(0));
1794 __ Cmp(string_to_copy, 0);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001795 SlowPathCodeARM64* slow_path =
1796 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001797 codegen_->AddSlowPath(slow_path);
1798 __ B(eq, slow_path->GetEntryLabel());
1799
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001800 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc(), slow_path);
Roland Levillainf969a202016-03-09 16:14:00 +00001801 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001802 __ Bind(slow_path->GetExitLabel());
1803}
1804
Vladimir Markoca6fff82017-10-03 14:49:14 +01001805static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001806 DCHECK_EQ(invoke->GetNumberOfArguments(), 1U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001807 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1808 DCHECK(DataType::IsFloatingPointType(invoke->GetType()));
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001809
Vladimir Markoca6fff82017-10-03 14:49:14 +01001810 LocationSummary* const locations =
1811 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001812 InvokeRuntimeCallingConvention calling_convention;
1813
1814 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1815 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1816}
1817
Vladimir Markoca6fff82017-10-03 14:49:14 +01001818static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001819 DCHECK_EQ(invoke->GetNumberOfArguments(), 2U);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001820 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(0)->GetType()));
1821 DCHECK(DataType::IsFloatingPointType(invoke->InputAt(1)->GetType()));
1822 DCHECK(DataType::IsFloatingPointType(invoke->GetType()));
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001823
Vladimir Markoca6fff82017-10-03 14:49:14 +01001824 LocationSummary* const locations =
1825 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001826 InvokeRuntimeCallingConvention calling_convention;
1827
1828 locations->SetInAt(0, LocationFrom(calling_convention.GetFpuRegisterAt(0)));
1829 locations->SetInAt(1, LocationFrom(calling_convention.GetFpuRegisterAt(1)));
1830 locations->SetOut(calling_convention.GetReturnLocation(invoke->GetType()));
1831}
1832
1833static void GenFPToFPCall(HInvoke* invoke,
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001834 CodeGeneratorARM64* codegen,
1835 QuickEntrypointEnum entry) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001836 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001837}
1838
1839void IntrinsicLocationsBuilderARM64::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001840 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001841}
1842
1843void IntrinsicCodeGeneratorARM64::VisitMathCos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001844 GenFPToFPCall(invoke, codegen_, kQuickCos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001845}
1846
1847void IntrinsicLocationsBuilderARM64::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001848 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001849}
1850
1851void IntrinsicCodeGeneratorARM64::VisitMathSin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001852 GenFPToFPCall(invoke, codegen_, kQuickSin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001853}
1854
1855void IntrinsicLocationsBuilderARM64::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001856 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001857}
1858
1859void IntrinsicCodeGeneratorARM64::VisitMathAcos(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001860 GenFPToFPCall(invoke, codegen_, kQuickAcos);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001861}
1862
1863void IntrinsicLocationsBuilderARM64::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001864 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001865}
1866
1867void IntrinsicCodeGeneratorARM64::VisitMathAsin(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001868 GenFPToFPCall(invoke, codegen_, kQuickAsin);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001869}
1870
1871void IntrinsicLocationsBuilderARM64::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001872 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001873}
1874
1875void IntrinsicCodeGeneratorARM64::VisitMathAtan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001876 GenFPToFPCall(invoke, codegen_, kQuickAtan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001877}
1878
1879void IntrinsicLocationsBuilderARM64::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001880 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001881}
1882
1883void IntrinsicCodeGeneratorARM64::VisitMathCbrt(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001884 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001885}
1886
1887void IntrinsicLocationsBuilderARM64::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001888 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001889}
1890
1891void IntrinsicCodeGeneratorARM64::VisitMathCosh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001892 GenFPToFPCall(invoke, codegen_, kQuickCosh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001893}
1894
1895void IntrinsicLocationsBuilderARM64::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001896 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001897}
1898
1899void IntrinsicCodeGeneratorARM64::VisitMathExp(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001900 GenFPToFPCall(invoke, codegen_, kQuickExp);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001901}
1902
1903void IntrinsicLocationsBuilderARM64::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001904 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001905}
1906
1907void IntrinsicCodeGeneratorARM64::VisitMathExpm1(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001908 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001909}
1910
1911void IntrinsicLocationsBuilderARM64::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001912 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001913}
1914
1915void IntrinsicCodeGeneratorARM64::VisitMathLog(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001916 GenFPToFPCall(invoke, codegen_, kQuickLog);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001917}
1918
1919void IntrinsicLocationsBuilderARM64::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001920 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001921}
1922
1923void IntrinsicCodeGeneratorARM64::VisitMathLog10(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001924 GenFPToFPCall(invoke, codegen_, kQuickLog10);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001925}
1926
1927void IntrinsicLocationsBuilderARM64::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001928 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001929}
1930
1931void IntrinsicCodeGeneratorARM64::VisitMathSinh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001932 GenFPToFPCall(invoke, codegen_, kQuickSinh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001933}
1934
1935void IntrinsicLocationsBuilderARM64::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001936 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001937}
1938
1939void IntrinsicCodeGeneratorARM64::VisitMathTan(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001940 GenFPToFPCall(invoke, codegen_, kQuickTan);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001941}
1942
1943void IntrinsicLocationsBuilderARM64::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001944 CreateFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001945}
1946
1947void IntrinsicCodeGeneratorARM64::VisitMathTanh(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001948 GenFPToFPCall(invoke, codegen_, kQuickTanh);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001949}
1950
1951void IntrinsicLocationsBuilderARM64::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001952 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001953}
1954
1955void IntrinsicCodeGeneratorARM64::VisitMathAtan2(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001956 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001957}
1958
Vladimir Marko4d179872018-01-19 14:50:10 +00001959void IntrinsicLocationsBuilderARM64::VisitMathPow(HInvoke* invoke) {
1960 CreateFPFPToFPCallLocations(allocator_, invoke);
1961}
1962
1963void IntrinsicCodeGeneratorARM64::VisitMathPow(HInvoke* invoke) {
1964 GenFPToFPCall(invoke, codegen_, kQuickPow);
1965}
1966
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001967void IntrinsicLocationsBuilderARM64::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001968 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001969}
1970
1971void IntrinsicCodeGeneratorARM64::VisitMathHypot(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001972 GenFPToFPCall(invoke, codegen_, kQuickHypot);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001973}
1974
1975void IntrinsicLocationsBuilderARM64::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001976 CreateFPFPToFPCallLocations(allocator_, invoke);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001977}
1978
1979void IntrinsicCodeGeneratorARM64::VisitMathNextAfter(HInvoke* invoke) {
Serban Constantinescu22f81d32016-02-18 16:06:31 +00001980 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
Anton Kirilov02fc24e2016-01-20 16:48:19 +00001981}
1982
Tim Zhang25abd6c2016-01-19 23:39:24 +08001983void IntrinsicLocationsBuilderARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001984 LocationSummary* locations =
1985 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Tim Zhang25abd6c2016-01-19 23:39:24 +08001986 locations->SetInAt(0, Location::RequiresRegister());
1987 locations->SetInAt(1, Location::RequiresRegister());
1988 locations->SetInAt(2, Location::RequiresRegister());
1989 locations->SetInAt(3, Location::RequiresRegister());
1990 locations->SetInAt(4, Location::RequiresRegister());
1991
1992 locations->AddTemp(Location::RequiresRegister());
1993 locations->AddTemp(Location::RequiresRegister());
Scott Wakelingdf109d92016-04-22 11:35:56 +01001994 locations->AddTemp(Location::RequiresRegister());
Tim Zhang25abd6c2016-01-19 23:39:24 +08001995}
1996
1997void IntrinsicCodeGeneratorARM64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01001998 MacroAssembler* masm = GetVIXLAssembler();
Tim Zhang25abd6c2016-01-19 23:39:24 +08001999 LocationSummary* locations = invoke->GetLocations();
2000
2001 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Tim Zhang25abd6c2016-01-19 23:39:24 +08002003 DCHECK_EQ(char_size, 2u);
2004
2005 // Location of data in char array buffer.
2006 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
2007
2008 // Location of char array data in string.
2009 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
2010
2011 // void getCharsNoCheck(int srcBegin, int srcEnd, char[] dst, int dstBegin);
2012 // Since getChars() calls getCharsNoCheck() - we use registers rather than constants.
2013 Register srcObj = XRegisterFrom(locations->InAt(0));
2014 Register srcBegin = XRegisterFrom(locations->InAt(1));
2015 Register srcEnd = XRegisterFrom(locations->InAt(2));
2016 Register dstObj = XRegisterFrom(locations->InAt(3));
2017 Register dstBegin = XRegisterFrom(locations->InAt(4));
2018
2019 Register src_ptr = XRegisterFrom(locations->GetTemp(0));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002020 Register num_chr = XRegisterFrom(locations->GetTemp(1));
2021 Register tmp1 = XRegisterFrom(locations->GetTemp(2));
Tim Zhang25abd6c2016-01-19 23:39:24 +08002022
2023 UseScratchRegisterScope temps(masm);
2024 Register dst_ptr = temps.AcquireX();
Scott Wakelingdf109d92016-04-22 11:35:56 +01002025 Register tmp2 = temps.AcquireX();
Tim Zhang25abd6c2016-01-19 23:39:24 +08002026
jessicahandojo05765752016-09-09 19:01:32 -07002027 vixl::aarch64::Label done;
2028 vixl::aarch64::Label compressed_string_loop;
2029 __ Sub(num_chr, srcEnd, srcBegin);
2030 // Early out for valid zero-length retrievals.
2031 __ Cbz(num_chr, &done);
Tim Zhang25abd6c2016-01-19 23:39:24 +08002032
Scott Wakelingdf109d92016-04-22 11:35:56 +01002033 // dst address start to copy to.
Tim Zhang25abd6c2016-01-19 23:39:24 +08002034 __ Add(dst_ptr, dstObj, Operand(data_offset));
2035 __ Add(dst_ptr, dst_ptr, Operand(dstBegin, LSL, 1));
2036
jessicahandojo05765752016-09-09 19:01:32 -07002037 // src address to copy from.
2038 __ Add(src_ptr, srcObj, Operand(value_offset));
2039 vixl::aarch64::Label compressed_string_preloop;
2040 if (mirror::kUseStringCompression) {
2041 // Location of count in string.
2042 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2043 // String's length.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01002044 __ Ldr(tmp2, MemOperand(srcObj, count_offset));
2045 __ Tbz(tmp2, 0, &compressed_string_preloop);
jessicahandojo05765752016-09-09 19:01:32 -07002046 }
2047 __ Add(src_ptr, src_ptr, Operand(srcBegin, LSL, 1));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002048
Tim Zhang25abd6c2016-01-19 23:39:24 +08002049 // Do the copy.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002050 vixl::aarch64::Label loop;
Scott Wakeling97c72b72016-06-24 16:19:36 +01002051 vixl::aarch64::Label remainder;
Scott Wakelingdf109d92016-04-22 11:35:56 +01002052
Scott Wakelingdf109d92016-04-22 11:35:56 +01002053 // Save repairing the value of num_chr on the < 8 character path.
2054 __ Subs(tmp1, num_chr, 8);
2055 __ B(lt, &remainder);
2056
2057 // Keep the result of the earlier subs, we are going to fetch at least 8 characters.
2058 __ Mov(num_chr, tmp1);
2059
2060 // Main loop used for longer fetches loads and stores 8x16-bit characters at a time.
2061 // (Unaligned addresses are acceptable here and not worth inlining extra code to rectify.)
Tim Zhang25abd6c2016-01-19 23:39:24 +08002062 __ Bind(&loop);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002063 __ Ldp(tmp1, tmp2, MemOperand(src_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002064 __ Subs(num_chr, num_chr, 8);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002065 __ Stp(tmp1, tmp2, MemOperand(dst_ptr, char_size * 8, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002066 __ B(ge, &loop);
2067
2068 __ Adds(num_chr, num_chr, 8);
2069 __ B(eq, &done);
2070
2071 // Main loop for < 8 character case and remainder handling. Loads and stores one
2072 // 16-bit Java character at a time.
2073 __ Bind(&remainder);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002074 __ Ldrh(tmp1, MemOperand(src_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002075 __ Subs(num_chr, num_chr, 1);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002076 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
Scott Wakelingdf109d92016-04-22 11:35:56 +01002077 __ B(gt, &remainder);
jessicahandojo05765752016-09-09 19:01:32 -07002078 __ B(&done);
2079
2080 if (mirror::kUseStringCompression) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002081 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo05765752016-09-09 19:01:32 -07002082 DCHECK_EQ(c_char_size, 1u);
2083 __ Bind(&compressed_string_preloop);
2084 __ Add(src_ptr, src_ptr, Operand(srcBegin));
2085 // Copy loop for compressed src, copying 1 character (8-bit) to (16-bit) at a time.
2086 __ Bind(&compressed_string_loop);
2087 __ Ldrb(tmp1, MemOperand(src_ptr, c_char_size, PostIndex));
2088 __ Strh(tmp1, MemOperand(dst_ptr, char_size, PostIndex));
2089 __ Subs(num_chr, num_chr, Operand(1));
2090 __ B(gt, &compressed_string_loop);
2091 }
Scott Wakelingdf109d92016-04-22 11:35:56 +01002092
Tim Zhang25abd6c2016-01-19 23:39:24 +08002093 __ Bind(&done);
2094}
2095
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002096// Mirrors ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD in libcore, so we can choose to use the native
2097// implementation there for longer copy lengths.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002098static constexpr int32_t kSystemArrayCopyCharThreshold = 32;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002099
2100static void SetSystemArrayCopyLocationRequires(LocationSummary* locations,
2101 uint32_t at,
2102 HInstruction* input) {
2103 HIntConstant* const_input = input->AsIntConstant();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002104 if (const_input != nullptr && !vixl::aarch64::Assembler::IsImmAddSub(const_input->GetValue())) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002105 locations->SetInAt(at, Location::RequiresRegister());
2106 } else {
2107 locations->SetInAt(at, Location::RegisterOrConstant(input));
2108 }
2109}
2110
2111void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
2112 // Check to see if we have known failures that will cause us to have to bail out
2113 // to the runtime, and just generate the runtime call directly.
2114 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2115 HIntConstant* dst_pos = invoke->InputAt(3)->AsIntConstant();
2116
2117 // The positions must be non-negative.
2118 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2119 (dst_pos != nullptr && dst_pos->GetValue() < 0)) {
2120 // We will have to fail anyways.
2121 return;
2122 }
2123
2124 // The length must be >= 0 and not so long that we would (currently) prefer libcore's
2125 // native implementation.
2126 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2127 if (length != nullptr) {
2128 int32_t len = length->GetValue();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002129 if (len < 0 || len > kSystemArrayCopyCharThreshold) {
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002130 // Just call as normal.
2131 return;
2132 }
2133 }
2134
Vladimir Markoca6fff82017-10-03 14:49:14 +01002135 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
2136 LocationSummary* locations =
2137 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002138 // arraycopy(char[] src, int src_pos, char[] dst, int dst_pos, int length).
2139 locations->SetInAt(0, Location::RequiresRegister());
2140 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2141 locations->SetInAt(2, Location::RequiresRegister());
2142 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2143 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2144
2145 locations->AddTemp(Location::RequiresRegister());
2146 locations->AddTemp(Location::RequiresRegister());
2147 locations->AddTemp(Location::RequiresRegister());
2148}
2149
Scott Wakeling97c72b72016-06-24 16:19:36 +01002150static void CheckSystemArrayCopyPosition(MacroAssembler* masm,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002151 const Location& pos,
2152 const Register& input,
2153 const Location& length,
2154 SlowPathCodeARM64* slow_path,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002155 const Register& temp,
2156 bool length_is_input_length = false) {
2157 const int32_t length_offset = mirror::Array::LengthOffset().Int32Value();
2158 if (pos.IsConstant()) {
2159 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
2160 if (pos_const == 0) {
2161 if (!length_is_input_length) {
2162 // Check that length(input) >= length.
2163 __ Ldr(temp, MemOperand(input, length_offset));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002164 __ Cmp(temp, OperandFrom(length, DataType::Type::kInt32));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002165 __ B(slow_path->GetEntryLabel(), lt);
2166 }
2167 } else {
2168 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002169 __ Ldr(temp, MemOperand(input, length_offset));
2170 __ Subs(temp, temp, pos_const);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002171 __ B(slow_path->GetEntryLabel(), lt);
2172
2173 // Check that (length(input) - pos) >= length.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002174 __ Cmp(temp, OperandFrom(length, DataType::Type::kInt32));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002175 __ B(slow_path->GetEntryLabel(), lt);
2176 }
2177 } else if (length_is_input_length) {
2178 // The only way the copy can succeed is if pos is zero.
2179 __ Cbnz(WRegisterFrom(pos), slow_path->GetEntryLabel());
2180 } else {
2181 // Check that pos >= 0.
2182 Register pos_reg = WRegisterFrom(pos);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002183 __ Tbnz(pos_reg, pos_reg.GetSizeInBits() - 1, slow_path->GetEntryLabel());
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002184
2185 // Check that pos <= length(input) && (length(input) - pos) >= length.
2186 __ Ldr(temp, MemOperand(input, length_offset));
2187 __ Subs(temp, temp, pos_reg);
2188 // Ccmp if length(input) >= pos, else definitely bail to slow path (N!=V == lt).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002189 __ Ccmp(temp, OperandFrom(length, DataType::Type::kInt32), NFlag, ge);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002190 __ B(slow_path->GetEntryLabel(), lt);
2191 }
2192}
2193
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002194// Compute base source address, base destination address, and end
2195// source address for System.arraycopy* intrinsics in `src_base`,
2196// `dst_base` and `src_end` respectively.
Scott Wakeling97c72b72016-06-24 16:19:36 +01002197static void GenSystemArrayCopyAddresses(MacroAssembler* masm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002198 DataType::Type type,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002199 const Register& src,
2200 const Location& src_pos,
2201 const Register& dst,
2202 const Location& dst_pos,
2203 const Location& copy_length,
2204 const Register& src_base,
2205 const Register& dst_base,
2206 const Register& src_end) {
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002207 // This routine is used by the SystemArrayCopy and the SystemArrayCopyChar intrinsics.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 DCHECK(type == DataType::Type::kReference || type == DataType::Type::kUint16)
Roland Levillainebea3d22016-04-12 15:42:57 +01002209 << "Unexpected element type: " << type;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002210 const int32_t element_size = DataType::Size(type);
2211 const int32_t element_size_shift = DataType::SizeShift(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002212 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002213
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002214 if (src_pos.IsConstant()) {
2215 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002216 __ Add(src_base, src, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002217 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002218 __ Add(src_base, src, data_offset);
2219 __ Add(src_base, src_base, Operand(XRegisterFrom(src_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002220 }
2221
2222 if (dst_pos.IsConstant()) {
2223 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002224 __ Add(dst_base, dst, element_size * constant + data_offset);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002225 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002226 __ Add(dst_base, dst, data_offset);
2227 __ Add(dst_base, dst_base, Operand(XRegisterFrom(dst_pos), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002228 }
2229
2230 if (copy_length.IsConstant()) {
2231 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
Roland Levillainebea3d22016-04-12 15:42:57 +01002232 __ Add(src_end, src_base, element_size * constant);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002233 } else {
Roland Levillainebea3d22016-04-12 15:42:57 +01002234 __ Add(src_end, src_base, Operand(XRegisterFrom(copy_length), LSL, element_size_shift));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002235 }
2236}
2237
2238void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopyChar(HInvoke* invoke) {
Scott Wakeling97c72b72016-06-24 16:19:36 +01002239 MacroAssembler* masm = GetVIXLAssembler();
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002240 LocationSummary* locations = invoke->GetLocations();
2241 Register src = XRegisterFrom(locations->InAt(0));
2242 Location src_pos = locations->InAt(1);
2243 Register dst = XRegisterFrom(locations->InAt(2));
2244 Location dst_pos = locations->InAt(3);
2245 Location length = locations->InAt(4);
2246
Vladimir Marko174b2e22017-10-12 13:34:49 +01002247 SlowPathCodeARM64* slow_path =
2248 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002249 codegen_->AddSlowPath(slow_path);
2250
2251 // If source and destination are the same, take the slow path. Overlapping copy regions must be
2252 // copied in reverse and we can't know in all cases if it's needed.
2253 __ Cmp(src, dst);
2254 __ B(slow_path->GetEntryLabel(), eq);
2255
2256 // Bail out if the source is null.
2257 __ Cbz(src, slow_path->GetEntryLabel());
2258
2259 // Bail out if the destination is null.
2260 __ Cbz(dst, slow_path->GetEntryLabel());
2261
2262 if (!length.IsConstant()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002263 // Merge the following two comparisons into one:
2264 // If the length is negative, bail out (delegate to libcore's native implementation).
2265 // If the length > 32 then (currently) prefer libcore's native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002266 __ Cmp(WRegisterFrom(length), kSystemArrayCopyCharThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002267 __ B(slow_path->GetEntryLabel(), hi);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002268 } else {
2269 // We have already checked in the LocationsBuilder for the constant case.
2270 DCHECK_GE(length.GetConstant()->AsIntConstant()->GetValue(), 0);
2271 DCHECK_LE(length.GetConstant()->AsIntConstant()->GetValue(), 32);
2272 }
2273
2274 Register src_curr_addr = WRegisterFrom(locations->GetTemp(0));
2275 Register dst_curr_addr = WRegisterFrom(locations->GetTemp(1));
2276 Register src_stop_addr = WRegisterFrom(locations->GetTemp(2));
2277
2278 CheckSystemArrayCopyPosition(masm,
2279 src_pos,
2280 src,
2281 length,
2282 slow_path,
2283 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002284 false);
2285
2286 CheckSystemArrayCopyPosition(masm,
2287 dst_pos,
2288 dst,
2289 length,
2290 slow_path,
2291 src_curr_addr,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002292 false);
2293
2294 src_curr_addr = src_curr_addr.X();
2295 dst_curr_addr = dst_curr_addr.X();
2296 src_stop_addr = src_stop_addr.X();
2297
2298 GenSystemArrayCopyAddresses(masm,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 DataType::Type::kUint16,
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002300 src,
2301 src_pos,
2302 dst,
2303 dst_pos,
2304 length,
2305 src_curr_addr,
2306 dst_curr_addr,
2307 src_stop_addr);
2308
2309 // Iterate over the arrays and do a raw copy of the chars.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002310 const int32_t char_size = DataType::Size(DataType::Type::kUint16);
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002311 UseScratchRegisterScope temps(masm);
2312 Register tmp = temps.AcquireW();
Scott Wakeling97c72b72016-06-24 16:19:36 +01002313 vixl::aarch64::Label loop, done;
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002314 __ Bind(&loop);
2315 __ Cmp(src_curr_addr, src_stop_addr);
2316 __ B(&done, eq);
Scott Wakeling97c72b72016-06-24 16:19:36 +01002317 __ Ldrh(tmp, MemOperand(src_curr_addr, char_size, PostIndex));
2318 __ Strh(tmp, MemOperand(dst_curr_addr, char_size, PostIndex));
Scott Wakelingd3d0da52016-02-29 15:17:20 +00002319 __ B(&loop);
2320 __ Bind(&done);
2321
2322 __ Bind(slow_path->GetExitLabel());
2323}
2324
donghui.baic2ec9ad2016-03-10 14:02:55 +08002325// We can choose to use the native implementation there for longer copy lengths.
2326static constexpr int32_t kSystemArrayCopyThreshold = 128;
2327
2328// CodeGenerator::CreateSystemArrayCopyLocationSummary use three temporary registers.
2329// We want to use two temporary registers in order to reduce the register pressure in arm64.
2330// So we don't use the CodeGenerator::CreateSystemArrayCopyLocationSummary.
2331void IntrinsicLocationsBuilderARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002332 // The only read barrier implementation supporting the
2333 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2334 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01002335 return;
2336 }
2337
donghui.baic2ec9ad2016-03-10 14:02:55 +08002338 // Check to see if we have known failures that will cause us to have to bail out
2339 // to the runtime, and just generate the runtime call directly.
2340 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
2341 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
2342
2343 // The positions must be non-negative.
2344 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
2345 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
2346 // We will have to fail anyways.
2347 return;
2348 }
2349
2350 // The length must be >= 0.
2351 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
2352 if (length != nullptr) {
2353 int32_t len = length->GetValue();
2354 if (len < 0 || len >= kSystemArrayCopyThreshold) {
2355 // Just call as normal.
2356 return;
2357 }
2358 }
2359
2360 SystemArrayCopyOptimizations optimizations(invoke);
2361
2362 if (optimizations.GetDestinationIsSource()) {
2363 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
2364 // We only support backward copying if source and destination are the same.
2365 return;
2366 }
2367 }
2368
2369 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
2370 // We currently don't intrinsify primitive copying.
2371 return;
2372 }
2373
Vladimir Markoca6fff82017-10-03 14:49:14 +01002374 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetAllocator();
2375 LocationSummary* locations =
2376 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002377 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
2378 locations->SetInAt(0, Location::RequiresRegister());
2379 SetSystemArrayCopyLocationRequires(locations, 1, invoke->InputAt(1));
2380 locations->SetInAt(2, Location::RequiresRegister());
2381 SetSystemArrayCopyLocationRequires(locations, 3, invoke->InputAt(3));
2382 SetSystemArrayCopyLocationRequires(locations, 4, invoke->InputAt(4));
2383
2384 locations->AddTemp(Location::RequiresRegister());
2385 locations->AddTemp(Location::RequiresRegister());
Roland Levillain0b671c02016-08-19 12:02:34 +01002386 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2387 // Temporary register IP0, obtained from the VIXL scratch register
2388 // pool, cannot be used in ReadBarrierSystemArrayCopySlowPathARM64
2389 // (because that register is clobbered by ReadBarrierMarkRegX
Roland Levillain54f869e2017-03-06 13:54:11 +00002390 // entry points). It cannot be used in calls to
2391 // CodeGeneratorARM64::GenerateFieldLoadWithBakerReadBarrier
2392 // either. For these reasons, get a third extra temporary register
2393 // from the register allocator.
Roland Levillain0b671c02016-08-19 12:02:34 +01002394 locations->AddTemp(Location::RequiresRegister());
Roland Levillain54f869e2017-03-06 13:54:11 +00002395 } else {
2396 // Cases other than Baker read barriers: the third temporary will
2397 // be acquired from the VIXL scratch register pool.
Roland Levillain0b671c02016-08-19 12:02:34 +01002398 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002399}
2400
2401void IntrinsicCodeGeneratorARM64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002402 // The only read barrier implementation supporting the
2403 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2404 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002405
Scott Wakeling97c72b72016-06-24 16:19:36 +01002406 MacroAssembler* masm = GetVIXLAssembler();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002407 LocationSummary* locations = invoke->GetLocations();
2408
2409 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2410 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2411 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2412 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002413 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
donghui.baic2ec9ad2016-03-10 14:02:55 +08002414
2415 Register src = XRegisterFrom(locations->InAt(0));
2416 Location src_pos = locations->InAt(1);
2417 Register dest = XRegisterFrom(locations->InAt(2));
2418 Location dest_pos = locations->InAt(3);
2419 Location length = locations->InAt(4);
2420 Register temp1 = WRegisterFrom(locations->GetTemp(0));
Roland Levillain0b671c02016-08-19 12:02:34 +01002421 Location temp1_loc = LocationFrom(temp1);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002422 Register temp2 = WRegisterFrom(locations->GetTemp(1));
Roland Levillain0b671c02016-08-19 12:02:34 +01002423 Location temp2_loc = LocationFrom(temp2);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002424
Vladimir Marko174b2e22017-10-12 13:34:49 +01002425 SlowPathCodeARM64* intrinsic_slow_path =
2426 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathARM64(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002427 codegen_->AddSlowPath(intrinsic_slow_path);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002428
Scott Wakeling97c72b72016-06-24 16:19:36 +01002429 vixl::aarch64::Label conditions_on_positions_validated;
donghui.baic2ec9ad2016-03-10 14:02:55 +08002430 SystemArrayCopyOptimizations optimizations(invoke);
2431
donghui.baic2ec9ad2016-03-10 14:02:55 +08002432 // If source and destination are the same, we go to slow path if we need to do
2433 // forward copying.
2434 if (src_pos.IsConstant()) {
2435 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2436 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002437 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2438 if (optimizations.GetDestinationIsSource()) {
2439 // Checked when building locations.
2440 DCHECK_GE(src_pos_constant, dest_pos_constant);
2441 } else if (src_pos_constant < dest_pos_constant) {
2442 __ Cmp(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002443 __ B(intrinsic_slow_path->GetEntryLabel(), eq);
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002444 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002445 // Checked when building locations.
2446 DCHECK(!optimizations.GetDestinationIsSource()
2447 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
2448 } else {
2449 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002450 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002451 __ B(&conditions_on_positions_validated, ne);
2452 }
2453 __ Cmp(WRegisterFrom(dest_pos), src_pos_constant);
Roland Levillain0b671c02016-08-19 12:02:34 +01002454 __ B(intrinsic_slow_path->GetEntryLabel(), gt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002455 }
2456 } else {
2457 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01002458 __ Cmp(src, dest);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002459 __ B(&conditions_on_positions_validated, ne);
2460 }
2461 __ Cmp(RegisterFrom(src_pos, invoke->InputAt(1)->GetType()),
2462 OperandFrom(dest_pos, invoke->InputAt(3)->GetType()));
Roland Levillain0b671c02016-08-19 12:02:34 +01002463 __ B(intrinsic_slow_path->GetEntryLabel(), lt);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002464 }
2465
2466 __ Bind(&conditions_on_positions_validated);
2467
2468 if (!optimizations.GetSourceIsNotNull()) {
2469 // Bail out if the source is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002470 __ Cbz(src, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002471 }
2472
2473 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2474 // Bail out if the destination is null.
Roland Levillain0b671c02016-08-19 12:02:34 +01002475 __ Cbz(dest, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002476 }
2477
2478 // We have already checked in the LocationsBuilder for the constant case.
2479 if (!length.IsConstant() &&
2480 !optimizations.GetCountIsSourceLength() &&
2481 !optimizations.GetCountIsDestinationLength()) {
Vladimir Markoc5646202016-11-28 16:03:15 +00002482 // Merge the following two comparisons into one:
2483 // If the length is negative, bail out (delegate to libcore's native implementation).
2484 // If the length >= 128 then (currently) prefer native implementation.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002485 __ Cmp(WRegisterFrom(length), kSystemArrayCopyThreshold);
Vladimir Markoc5646202016-11-28 16:03:15 +00002486 __ B(intrinsic_slow_path->GetEntryLabel(), hs);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002487 }
2488 // Validity checks: source.
2489 CheckSystemArrayCopyPosition(masm,
2490 src_pos,
2491 src,
2492 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002493 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002494 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002495 optimizations.GetCountIsSourceLength());
2496
2497 // Validity checks: dest.
2498 CheckSystemArrayCopyPosition(masm,
2499 dest_pos,
2500 dest,
2501 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002502 intrinsic_slow_path,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002503 temp1,
donghui.baic2ec9ad2016-03-10 14:02:55 +08002504 optimizations.GetCountIsDestinationLength());
2505 {
2506 // We use a block to end the scratch scope before the write barrier, thus
2507 // freeing the temporary registers so they can be used in `MarkGCCard`.
2508 UseScratchRegisterScope temps(masm);
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002509 Location temp3_loc; // Used only for Baker read barrier.
Roland Levillain54f869e2017-03-06 13:54:11 +00002510 Register temp3;
2511 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002512 temp3_loc = locations->GetTemp(2);
2513 temp3 = WRegisterFrom(temp3_loc);
Roland Levillain54f869e2017-03-06 13:54:11 +00002514 } else {
2515 temp3 = temps.AcquireW();
2516 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002517
donghui.baic2ec9ad2016-03-10 14:02:55 +08002518 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2519 // Check whether all elements of the source array are assignable to the component
2520 // type of the destination array. We do two checks: the classes are the same,
2521 // or the destination is Object[]. If none of these checks succeed, we go to the
2522 // slow path.
donghui.baic2ec9ad2016-03-10 14:02:55 +08002523
Roland Levillain0b671c02016-08-19 12:02:34 +01002524 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2525 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2526 // /* HeapReference<Class> */ temp1 = src->klass_
2527 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2528 temp1_loc,
2529 src.W(),
2530 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002531 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002532 /* needs_null_check */ false,
2533 /* use_load_acquire */ false);
2534 // Bail out if the source is not a non primitive array.
2535 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2536 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2537 temp1_loc,
2538 temp1,
2539 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002540 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002541 /* needs_null_check */ false,
2542 /* use_load_acquire */ false);
2543 __ Cbz(temp1, intrinsic_slow_path->GetEntryLabel());
2544 // If heap poisoning is enabled, `temp1` has been unpoisoned
2545 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2546 // /* uint16_t */ temp1 = static_cast<uint16>(temp1->primitive_type_);
2547 __ Ldrh(temp1, HeapOperand(temp1, primitive_offset));
2548 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2549 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002550 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002551
2552 // /* HeapReference<Class> */ temp1 = dest->klass_
2553 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2554 temp1_loc,
2555 dest.W(),
2556 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002557 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002558 /* needs_null_check */ false,
2559 /* use_load_acquire */ false);
2560
2561 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2562 // Bail out if the destination is not a non primitive array.
2563 //
2564 // Register `temp1` is not trashed by the read barrier emitted
2565 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2566 // method produces a call to a ReadBarrierMarkRegX entry point,
2567 // which saves all potentially live registers, including
2568 // temporaries such a `temp1`.
2569 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2570 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2571 temp2_loc,
2572 temp1,
2573 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002574 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002575 /* needs_null_check */ false,
2576 /* use_load_acquire */ false);
2577 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2578 // If heap poisoning is enabled, `temp2` has been unpoisoned
2579 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2580 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2581 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
2582 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2583 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
2584 }
2585
2586 // For the same reason given earlier, `temp1` is not trashed by the
2587 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2588 // /* HeapReference<Class> */ temp2 = src->klass_
2589 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2590 temp2_loc,
2591 src.W(),
2592 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002593 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002594 /* needs_null_check */ false,
2595 /* use_load_acquire */ false);
2596 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2597 __ Cmp(temp1, temp2);
2598
2599 if (optimizations.GetDestinationIsTypedObjectArray()) {
2600 vixl::aarch64::Label do_copy;
2601 __ B(&do_copy, eq);
2602 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2603 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2604 temp1_loc,
2605 temp1,
2606 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002607 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002608 /* needs_null_check */ false,
2609 /* use_load_acquire */ false);
2610 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2611 // We do not need to emit a read barrier for the following
2612 // heap reference load, as `temp1` is only used in a
2613 // comparison with null below, and this reference is not
2614 // kept afterwards.
2615 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2616 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2617 __ Bind(&do_copy);
2618 } else {
2619 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2620 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002621 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002622 // Non read barrier code.
2623
2624 // /* HeapReference<Class> */ temp1 = dest->klass_
2625 __ Ldr(temp1, MemOperand(dest, class_offset));
2626 // /* HeapReference<Class> */ temp2 = src->klass_
2627 __ Ldr(temp2, MemOperand(src, class_offset));
2628 bool did_unpoison = false;
2629 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
2630 !optimizations.GetSourceIsNonPrimitiveArray()) {
2631 // One or two of the references need to be unpoisoned. Unpoison them
2632 // both to make the identity check valid.
2633 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2634 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2635 did_unpoison = true;
2636 }
2637
2638 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2639 // Bail out if the destination is not a non primitive array.
2640 // /* HeapReference<Class> */ temp3 = temp1->component_type_
2641 __ Ldr(temp3, HeapOperand(temp1, component_offset));
2642 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2643 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2644 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2645 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2646 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2647 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2648 }
2649
2650 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2651 // Bail out if the source is not a non primitive array.
2652 // /* HeapReference<Class> */ temp3 = temp2->component_type_
2653 __ Ldr(temp3, HeapOperand(temp2, component_offset));
2654 __ Cbz(temp3, intrinsic_slow_path->GetEntryLabel());
2655 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp3);
2656 // /* uint16_t */ temp3 = static_cast<uint16>(temp3->primitive_type_);
2657 __ Ldrh(temp3, HeapOperand(temp3, primitive_offset));
2658 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
2659 __ Cbnz(temp3, intrinsic_slow_path->GetEntryLabel());
2660 }
2661
2662 __ Cmp(temp1, temp2);
2663
2664 if (optimizations.GetDestinationIsTypedObjectArray()) {
2665 vixl::aarch64::Label do_copy;
2666 __ B(&do_copy, eq);
2667 if (!did_unpoison) {
2668 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2669 }
2670 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2671 __ Ldr(temp1, HeapOperand(temp1, component_offset));
2672 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2673 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2674 __ Ldr(temp1, HeapOperand(temp1, super_offset));
2675 // No need to unpoison the result, we're comparing against null.
2676 __ Cbnz(temp1, intrinsic_slow_path->GetEntryLabel());
2677 __ Bind(&do_copy);
2678 } else {
2679 __ B(intrinsic_slow_path->GetEntryLabel(), ne);
2680 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002681 }
2682 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2683 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2684 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002685 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2686 // /* HeapReference<Class> */ temp1 = src->klass_
2687 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2688 temp1_loc,
2689 src.W(),
2690 class_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002691 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002692 /* needs_null_check */ false,
2693 /* use_load_acquire */ false);
2694 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2695 codegen_->GenerateFieldLoadWithBakerReadBarrier(invoke,
2696 temp2_loc,
2697 temp1,
2698 component_offset,
Vladimir Markof4f2daa2017-03-20 18:26:59 +00002699 temp3_loc,
Roland Levillain0b671c02016-08-19 12:02:34 +01002700 /* needs_null_check */ false,
2701 /* use_load_acquire */ false);
2702 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2703 // If heap poisoning is enabled, `temp2` has been unpoisoned
2704 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2705 } else {
2706 // /* HeapReference<Class> */ temp1 = src->klass_
2707 __ Ldr(temp1, HeapOperand(src.W(), class_offset));
2708 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp1);
2709 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2710 __ Ldr(temp2, HeapOperand(temp1, component_offset));
2711 __ Cbz(temp2, intrinsic_slow_path->GetEntryLabel());
2712 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(temp2);
2713 }
2714 // /* uint16_t */ temp2 = static_cast<uint16>(temp2->primitive_type_);
2715 __ Ldrh(temp2, HeapOperand(temp2, primitive_offset));
donghui.baic2ec9ad2016-03-10 14:02:55 +08002716 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Roland Levillain0b671c02016-08-19 12:02:34 +01002717 __ Cbnz(temp2, intrinsic_slow_path->GetEntryLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002718 }
2719
Roland Levillain1663d162017-03-17 15:15:21 +00002720 if (length.IsConstant() && length.GetConstant()->AsIntConstant()->GetValue() == 0) {
2721 // Null constant length: not need to emit the loop code at all.
Roland Levillain0b671c02016-08-19 12:02:34 +01002722 } else {
Roland Levillain1663d162017-03-17 15:15:21 +00002723 Register src_curr_addr = temp1.X();
2724 Register dst_curr_addr = temp2.X();
2725 Register src_stop_addr = temp3.X();
2726 vixl::aarch64::Label done;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 const DataType::Type type = DataType::Type::kReference;
2728 const int32_t element_size = DataType::Size(type);
Roland Levillain1663d162017-03-17 15:15:21 +00002729
2730 if (length.IsRegister()) {
2731 // Don't enter the copy loop if the length is null.
2732 __ Cbz(WRegisterFrom(length), &done);
2733 }
2734
2735 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2736 // TODO: Also convert this intrinsic to the IsGcMarking strategy?
2737
2738 // SystemArrayCopy implementation for Baker read barriers (see
Roland Levillain9983e302017-07-14 14:34:22 +01002739 // also CodeGeneratorARM64::GenerateReferenceLoadWithBakerReadBarrier):
Roland Levillain1663d162017-03-17 15:15:21 +00002740 //
2741 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2742 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
2743 // bool is_gray = (rb_state == ReadBarrier::GrayState());
2744 // if (is_gray) {
2745 // // Slow-path copy.
2746 // do {
2747 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
2748 // } while (src_ptr != end_ptr)
2749 // } else {
2750 // // Fast-path copy.
2751 // do {
2752 // *dest_ptr++ = *src_ptr++;
2753 // } while (src_ptr != end_ptr)
2754 // }
2755
2756 // Make sure `tmp` is not IP0, as it is clobbered by
2757 // ReadBarrierMarkRegX entry points in
2758 // ReadBarrierSystemArrayCopySlowPathARM64.
Roland Levillain1ca955d2017-04-13 19:34:30 +01002759 DCHECK(temps.IsAvailable(ip0));
Roland Levillain1663d162017-03-17 15:15:21 +00002760 temps.Exclude(ip0);
Roland Levillain0b671c02016-08-19 12:02:34 +01002761 Register tmp = temps.AcquireW();
Roland Levillain1663d162017-03-17 15:15:21 +00002762 DCHECK_NE(LocationFrom(tmp).reg(), IP0);
Roland Levillain1ca955d2017-04-13 19:34:30 +01002763 // Put IP0 back in the pool so that VIXL has at least one
2764 // scratch register available to emit macro-instructions (note
2765 // that IP1 is already used for `tmp`). Indeed some
2766 // macro-instructions used in GenSystemArrayCopyAddresses
2767 // (invoked hereunder) may require a scratch register (for
2768 // instance to emit a load with a large constant offset).
2769 temps.Include(ip0);
Roland Levillain1663d162017-03-17 15:15:21 +00002770
2771 // /* int32_t */ monitor = src->monitor_
2772 __ Ldr(tmp, HeapOperand(src.W(), monitor_offset));
2773 // /* LockWord */ lock_word = LockWord(monitor)
2774 static_assert(sizeof(LockWord) == sizeof(int32_t),
2775 "art::LockWord and int32_t have different sizes.");
2776
2777 // Introduce a dependency on the lock_word including rb_state,
2778 // to prevent load-load reordering, and without using
2779 // a memory barrier (which would be more expensive).
2780 // `src` is unchanged by this operation, but its value now depends
2781 // on `tmp`.
2782 __ Add(src.X(), src.X(), Operand(tmp.X(), LSR, 32));
2783
2784 // Compute base source address, base destination address, and end
2785 // source address for System.arraycopy* intrinsics in `src_base`,
2786 // `dst_base` and `src_end` respectively.
2787 // Note that `src_curr_addr` is computed from from `src` (and
2788 // `src_pos`) here, and thus honors the artificial dependency
2789 // of `src` on `tmp`.
2790 GenSystemArrayCopyAddresses(masm,
2791 type,
2792 src,
2793 src_pos,
2794 dest,
2795 dest_pos,
2796 length,
2797 src_curr_addr,
2798 dst_curr_addr,
2799 src_stop_addr);
2800
2801 // Slow path used to copy array when `src` is gray.
2802 SlowPathCodeARM64* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002803 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathARM64(
2804 invoke, LocationFrom(tmp));
Roland Levillain1663d162017-03-17 15:15:21 +00002805 codegen_->AddSlowPath(read_barrier_slow_path);
2806
2807 // Given the numeric representation, it's enough to check the low bit of the rb_state.
2808 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2809 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
2810 __ Tbnz(tmp, LockWord::kReadBarrierStateShift, read_barrier_slow_path->GetEntryLabel());
2811
2812 // Fast-path copy.
2813 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2814 // poison/unpoison.
2815 vixl::aarch64::Label loop;
2816 __ Bind(&loop);
Roland Levillain0b671c02016-08-19 12:02:34 +01002817 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2818 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
Roland Levillain1663d162017-03-17 15:15:21 +00002819 __ Cmp(src_curr_addr, src_stop_addr);
2820 __ B(&loop, ne);
2821
2822 __ Bind(read_barrier_slow_path->GetExitLabel());
2823 } else {
2824 // Non read barrier code.
2825 // Compute base source address, base destination address, and end
2826 // source address for System.arraycopy* intrinsics in `src_base`,
2827 // `dst_base` and `src_end` respectively.
2828 GenSystemArrayCopyAddresses(masm,
2829 type,
2830 src,
2831 src_pos,
2832 dest,
2833 dest_pos,
2834 length,
2835 src_curr_addr,
2836 dst_curr_addr,
2837 src_stop_addr);
2838 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2839 // poison/unpoison.
2840 vixl::aarch64::Label loop;
2841 __ Bind(&loop);
2842 {
2843 Register tmp = temps.AcquireW();
2844 __ Ldr(tmp, MemOperand(src_curr_addr, element_size, PostIndex));
2845 __ Str(tmp, MemOperand(dst_curr_addr, element_size, PostIndex));
2846 }
2847 __ Cmp(src_curr_addr, src_stop_addr);
2848 __ B(&loop, ne);
Roland Levillain0b671c02016-08-19 12:02:34 +01002849 }
Roland Levillain0b671c02016-08-19 12:02:34 +01002850 __ Bind(&done);
donghui.baic2ec9ad2016-03-10 14:02:55 +08002851 }
donghui.baic2ec9ad2016-03-10 14:02:55 +08002852 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002853
donghui.baic2ec9ad2016-03-10 14:02:55 +08002854 // We only need one card marking on the destination array.
2855 codegen_->MarkGCCard(dest.W(), Register(), /* value_can_be_null */ false);
2856
Roland Levillain0b671c02016-08-19 12:02:34 +01002857 __ Bind(intrinsic_slow_path->GetExitLabel());
donghui.baic2ec9ad2016-03-10 14:02:55 +08002858}
2859
Anton Kirilova3ffea22016-04-07 17:02:37 +01002860static void GenIsInfinite(LocationSummary* locations,
2861 bool is64bit,
Scott Wakeling97c72b72016-06-24 16:19:36 +01002862 MacroAssembler* masm) {
Anton Kirilova3ffea22016-04-07 17:02:37 +01002863 Operand infinity;
2864 Register out;
2865
2866 if (is64bit) {
2867 infinity = kPositiveInfinityDouble;
2868 out = XRegisterFrom(locations->Out());
2869 } else {
2870 infinity = kPositiveInfinityFloat;
2871 out = WRegisterFrom(locations->Out());
2872 }
2873
Scott Wakeling97c72b72016-06-24 16:19:36 +01002874 const Register zero = vixl::aarch64::Assembler::AppropriateZeroRegFor(out);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002875
2876 MoveFPToInt(locations, is64bit, masm);
2877 __ Eor(out, out, infinity);
2878 // We don't care about the sign bit, so shift left.
2879 __ Cmp(zero, Operand(out, LSL, 1));
2880 __ Cset(out, eq);
2881}
2882
2883void IntrinsicLocationsBuilderARM64::VisitFloatIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002884 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002885}
2886
2887void IntrinsicCodeGeneratorARM64::VisitFloatIsInfinite(HInvoke* invoke) {
2888 GenIsInfinite(invoke->GetLocations(), /* is64bit */ false, GetVIXLAssembler());
2889}
2890
2891void IntrinsicLocationsBuilderARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002892 CreateFPToIntLocations(allocator_, invoke);
Anton Kirilova3ffea22016-04-07 17:02:37 +01002893}
2894
2895void IntrinsicCodeGeneratorARM64::VisitDoubleIsInfinite(HInvoke* invoke) {
2896 GenIsInfinite(invoke->GetLocations(), /* is64bit */ true, GetVIXLAssembler());
2897}
2898
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002899void IntrinsicLocationsBuilderARM64::VisitIntegerValueOf(HInvoke* invoke) {
2900 InvokeRuntimeCallingConvention calling_convention;
2901 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2902 invoke,
2903 codegen_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 calling_convention.GetReturnLocation(DataType::Type::kReference),
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002905 Location::RegisterLocation(calling_convention.GetRegisterAt(0).GetCode()));
2906}
2907
2908void IntrinsicCodeGeneratorARM64::VisitIntegerValueOf(HInvoke* invoke) {
2909 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2910 LocationSummary* locations = invoke->GetLocations();
2911 MacroAssembler* masm = GetVIXLAssembler();
2912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 Register out = RegisterFrom(locations->Out(), DataType::Type::kReference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002914 UseScratchRegisterScope temps(masm);
2915 Register temp = temps.AcquireW();
2916 InvokeRuntimeCallingConvention calling_convention;
2917 Register argument = calling_convention.GetRegisterAt(0);
2918 if (invoke->InputAt(0)->IsConstant()) {
2919 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2920 if (value >= info.low && value <= info.high) {
2921 // Just embed the j.l.Integer in the code.
2922 ScopedObjectAccess soa(Thread::Current());
2923 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2924 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2925 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
2926 __ Ldr(out.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2927 } else {
2928 // Allocate and initialize a new j.l.Integer.
2929 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2930 // JIT object table.
2931 uint32_t address =
2932 dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2933 __ Ldr(argument.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2934 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2935 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2936 __ Mov(temp.W(), value);
2937 __ Str(temp.W(), HeapOperand(out.W(), info.value_offset));
2938 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2939 // one.
2940 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2941 }
2942 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002943 Register in = RegisterFrom(locations->InAt(0), DataType::Type::kInt32);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002944 // Check bounds of our cache.
2945 __ Add(out.W(), in.W(), -info.low);
2946 __ Cmp(out.W(), info.high - info.low + 1);
2947 vixl::aarch64::Label allocate, done;
2948 __ B(&allocate, hs);
2949 // If the value is within the bounds, load the j.l.Integer directly from the array.
2950 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2951 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
2952 __ Ldr(temp.W(), codegen_->DeduplicateBootImageAddressLiteral(data_offset + address));
2953 MemOperand source = HeapOperand(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 temp, out.X(), LSL, DataType::SizeShift(DataType::Type::kReference));
2955 codegen_->Load(DataType::Type::kReference, out, source);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002956 codegen_->GetAssembler()->MaybeUnpoisonHeapReference(out);
2957 __ B(&done);
2958 __ Bind(&allocate);
2959 // Otherwise allocate and initialize a new j.l.Integer.
2960 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2961 __ Ldr(argument.W(), codegen_->DeduplicateBootImageAddressLiteral(address));
2962 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2963 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2964 __ Str(in.W(), HeapOperand(out.W(), info.value_offset));
2965 // `value` is a final field :-( Ideally, we'd merge this memory barrier with the allocation
2966 // one.
2967 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2968 __ Bind(&done);
2969 }
2970}
2971
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002972void IntrinsicLocationsBuilderARM64::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002973 LocationSummary* locations =
2974 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002975 locations->SetOut(Location::RequiresRegister());
2976}
2977
2978void IntrinsicCodeGeneratorARM64::VisitThreadInterrupted(HInvoke* invoke) {
2979 MacroAssembler* masm = GetVIXLAssembler();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002980 Register out = RegisterFrom(invoke->GetLocations()->Out(), DataType::Type::kInt32);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002981 UseScratchRegisterScope temps(masm);
2982 Register temp = temps.AcquireX();
2983
2984 __ Add(temp, tr, Thread::InterruptedOffset<kArm64PointerSize>().Int32Value());
2985 __ Ldar(out.W(), MemOperand(temp));
2986
2987 vixl::aarch64::Label done;
2988 __ Cbz(out.W(), &done);
2989 __ Stlr(wzr, MemOperand(temp));
2990 __ Bind(&done);
2991}
2992
Vladimir Markod254f5c2017-06-02 15:18:36 +00002993UNIMPLEMENTED_INTRINSIC(ARM64, ReferenceGetReferent)
Andreas Gampe878d58c2015-01-15 23:24:00 -08002994
Aart Bikff7d89c2016-11-07 08:49:28 -08002995UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOf);
2996UNIMPLEMENTED_INTRINSIC(ARM64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002997UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferAppend);
2998UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferLength);
2999UNIMPLEMENTED_INTRINSIC(ARM64, StringBufferToString);
3000UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderAppend);
3001UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderLength);
3002UNIMPLEMENTED_INTRINSIC(ARM64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003003
Aart Bik0e54c012016-03-04 12:08:31 -08003004// 1.8.
3005UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddInt)
3006UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndAddLong)
3007UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetInt)
3008UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetLong)
3009UNIMPLEMENTED_INTRINSIC(ARM64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003010
Aart Bik2f9fcc92016-03-01 15:16:54 -08003011UNREACHABLE_INTRINSICS(ARM64)
Roland Levillain4d027112015-07-01 15:41:14 +01003012
3013#undef __
3014
Andreas Gampe878d58c2015-01-15 23:24:00 -08003015} // namespace arm64
3016} // namespace art