blob: 7627dc9490a81b8652d001fa6a9f4cdb3194f720 [file] [log] [blame]
Andreas Gampe71fb52f2014-12-29 17:43:08 -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_x86_64.h"
18
Andreas Gampe21030dd2015-05-07 14:46:15 -070019#include <limits>
20
Mark Mendellfb8d2792015-03-31 22:16:59 -040021#include "arch/x86_64/instruction_set_features_x86_64.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080022#include "art_method.h"
Mark Mendelld5897672015-08-12 21:16:41 -040023#include "base/bit_utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "code_generator_x86_64.h"
25#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070028#include "intrinsics_utils.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#include "lock_word.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080030#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070031#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080032#include "mirror/reference.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080033#include "mirror/string.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080034#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080036#include "utils/x86_64/assembler_x86_64.h"
37#include "utils/x86_64/constants_x86_64.h"
38
39namespace art {
40
41namespace x86_64 {
42
Mark Mendellfb8d2792015-03-31 22:16:59 -040043IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +010044 : allocator_(codegen->GetGraph()->GetAllocator()), codegen_(codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -040045}
46
Andreas Gampe71fb52f2014-12-29 17:43:08 -080047X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010048 return down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -080049}
50
Andreas Gampe878d58c2015-01-15 23:24:00 -080051ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010052 return codegen_->GetGraph()->GetAllocator();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080053}
54
55bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
56 Dispatch(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +000057 LocationSummary* res = invoke->GetLocations();
58 if (res == nullptr) {
59 return false;
60 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000061 return res->Intrinsified();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080062}
63
Roland Levillainec525fc2015-04-28 15:50:20 +010064static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010065 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010066 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080067}
68
Andreas Gampe85b62f22015-09-09 13:15:38 -070069using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080070
Roland Levillain0b671c02016-08-19 12:02:34 +010071// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
72#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
73
74// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
75class ReadBarrierSystemArrayCopySlowPathX86_64 : public SlowPathCode {
76 public:
77 explicit ReadBarrierSystemArrayCopySlowPathX86_64(HInstruction* instruction)
78 : SlowPathCode(instruction) {
79 DCHECK(kEmitCompilerReadBarrier);
80 DCHECK(kUseBakerReadBarrier);
81 }
82
83 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
84 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
85 LocationSummary* locations = instruction_->GetLocations();
86 DCHECK(locations->CanCall());
87 DCHECK(instruction_->IsInvokeStaticOrDirect())
88 << "Unexpected instruction in read barrier arraycopy slow path: "
89 << instruction_->DebugName();
90 DCHECK(instruction_->GetLocations()->Intrinsified());
91 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
92
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010093 int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +010094
95 CpuRegister src_curr_addr = locations->GetTemp(0).AsRegister<CpuRegister>();
96 CpuRegister dst_curr_addr = locations->GetTemp(1).AsRegister<CpuRegister>();
97 CpuRegister src_stop_addr = locations->GetTemp(2).AsRegister<CpuRegister>();
98
99 __ Bind(GetEntryLabel());
100 NearLabel loop;
101 __ Bind(&loop);
102 __ movl(CpuRegister(TMP), Address(src_curr_addr, 0));
103 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
104 // TODO: Inline the mark bit check before calling the runtime?
105 // TMP = ReadBarrier::Mark(TMP);
106 // No need to save live registers; it's taken care of by the
107 // entrypoint. Also, there is no need to update the stack mask,
108 // as this runtime call will not trigger a garbage collection.
Roland Levillain97c46462017-05-11 14:04:03 +0100109 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(TMP);
Roland Levillain0b671c02016-08-19 12:02:34 +0100110 // This runtime call does not require a stack map.
111 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
112 __ MaybePoisonHeapReference(CpuRegister(TMP));
113 __ movl(Address(dst_curr_addr, 0), CpuRegister(TMP));
114 __ addl(src_curr_addr, Immediate(element_size));
115 __ addl(dst_curr_addr, Immediate(element_size));
116 __ cmpl(src_curr_addr, src_stop_addr);
117 __ j(kNotEqual, &loop);
118 __ jmp(GetExitLabel());
119 }
120
121 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86_64"; }
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86_64);
125};
126
127#undef __
128
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800129#define __ assembler->
130
Vladimir Markoca6fff82017-10-03 14:49:14 +0100131static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
132 LocationSummary* locations =
133 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800134 locations->SetInAt(0, Location::RequiresFpuRegister());
135 locations->SetOut(Location::RequiresRegister());
136}
137
Vladimir Markoca6fff82017-10-03 14:49:14 +0100138static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
139 LocationSummary* locations =
140 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800141 locations->SetInAt(0, Location::RequiresRegister());
142 locations->SetOut(Location::RequiresFpuRegister());
143}
144
145static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
146 Location input = locations->InAt(0);
147 Location output = locations->Out();
148 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
149}
150
151static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
152 Location input = locations->InAt(0);
153 Location output = locations->Out();
154 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
155}
156
157void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800159}
160void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100161 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800162}
163
164void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000165 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800166}
167void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000168 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800169}
170
171void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100172 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800173}
174void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100175 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800176}
177
178void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000179 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800180}
181void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000182 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800183}
184
Vladimir Markoca6fff82017-10-03 14:49:14 +0100185static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
186 LocationSummary* locations =
187 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800188 locations->SetInAt(0, Location::RequiresRegister());
189 locations->SetOut(Location::SameAsFirstInput());
190}
191
192static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100193 DataType::Type size,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800194 X86_64Assembler* assembler) {
195 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
196
197 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100198 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800199 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
200 __ bswapl(out);
201 __ sarl(out, Immediate(16));
202 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100203 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800204 __ bswapl(out);
205 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100206 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800207 __ bswapq(out);
208 break;
209 default:
210 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
211 UNREACHABLE();
212 }
213}
214
215void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100216 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800217}
218
219void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100220 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800221}
222
223void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100224 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800225}
226
227void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100228 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800229}
230
231void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100232 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800233}
234
235void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100236 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800237}
238
Vladimir Markoca6fff82017-10-03 14:49:14 +0100239static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
240 LocationSummary* locations =
241 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800242 locations->SetInAt(0, Location::RequiresFpuRegister());
243 locations->SetOut(Location::RequiresFpuRegister());
244}
245
246void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100247 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800248}
249
250void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
251 LocationSummary* locations = invoke->GetLocations();
252 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
253 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
254
255 GetAssembler()->sqrtsd(out, in);
256}
257
Mark Mendellfb8d2792015-03-31 22:16:59 -0400258static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100259 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400260
261 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100262 codegen->GenerateStaticOrDirectCall(
263 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400264
265 // Copy the result back to the expected output.
266 Location out = invoke->GetLocations()->Out();
267 if (out.IsValid()) {
268 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700269 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400270 }
271}
272
Vladimir Markoca6fff82017-10-03 14:49:14 +0100273static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator,
274 HInvoke* invoke,
275 CodeGeneratorX86_64* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400276 // Do we have instruction support?
277 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100278 CreateFPToFPLocations(allocator, invoke);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400279 return;
280 }
281
282 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100283 LocationSummary* locations =
284 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400285 InvokeRuntimeCallingConvention calling_convention;
286 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
287 locations->SetOut(Location::FpuRegisterLocation(XMM0));
288 // Needs to be RDI for the invoke.
289 locations->AddTemp(Location::RegisterLocation(RDI));
290}
291
292static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
293 HInvoke* invoke,
294 X86_64Assembler* assembler,
295 int round_mode) {
296 LocationSummary* locations = invoke->GetLocations();
297 if (locations->WillCall()) {
298 InvokeOutOfLineIntrinsic(codegen, invoke);
299 } else {
300 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
301 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
302 __ roundsd(out, in, Immediate(round_mode));
303 }
304}
305
306void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100307 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400308}
309
310void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
311 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
312}
313
314void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100315 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400316}
317
318void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
319 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
320}
321
322void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100323 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400324}
325
326void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
327 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
328}
329
Vladimir Markoca6fff82017-10-03 14:49:14 +0100330static void CreateSSE41FPToIntLocations(ArenaAllocator* allocator,
331 HInvoke* invoke,
332 CodeGeneratorX86_64* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400333 // Do we have instruction support?
334 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100335 LocationSummary* locations =
336 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400337 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600338 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400339 locations->AddTemp(Location::RequiresFpuRegister());
Aart Bik349f3882016-08-02 15:40:56 -0700340 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400341 return;
342 }
343
344 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100345 LocationSummary* locations =
346 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400347 InvokeRuntimeCallingConvention calling_convention;
348 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
349 locations->SetOut(Location::RegisterLocation(RAX));
350 // Needs to be RDI for the invoke.
351 locations->AddTemp(Location::RegisterLocation(RDI));
352}
353
354void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100355 CreateSSE41FPToIntLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400356}
357
358void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
359 LocationSummary* locations = invoke->GetLocations();
360 if (locations->WillCall()) {
361 InvokeOutOfLineIntrinsic(codegen_, invoke);
362 return;
363 }
364
Mark Mendellfb8d2792015-03-31 22:16:59 -0400365 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
366 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700367 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
368 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
369 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400370 X86_64Assembler* assembler = GetAssembler();
371
Aart Bik349f3882016-08-02 15:40:56 -0700372 // Since no direct x86 rounding instruction matches the required semantics,
373 // this intrinsic is implemented as follows:
374 // result = floor(in);
375 // if (in - result >= 0.5f)
376 // result = result + 1.0f;
377 __ movss(t2, in);
378 __ roundss(t1, in, Immediate(1));
379 __ subss(t2, t1);
380 __ comiss(t2, codegen_->LiteralFloatAddress(0.5f));
381 __ j(kBelow, &skip_incr);
382 __ addss(t1, codegen_->LiteralFloatAddress(1.0f));
383 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400384
Aart Bik349f3882016-08-02 15:40:56 -0700385 // Final conversion to an integer. Unfortunately this also does not have a
386 // direct x86 instruction, since NaN should map to 0 and large positive
387 // values need to be clipped to the extreme value.
388 codegen_->Load32BitValue(out, kPrimIntMax);
389 __ cvtsi2ss(t2, out);
390 __ comiss(t1, t2);
391 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
392 __ movl(out, Immediate(0)); // does not change flags
393 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
394 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400395 __ Bind(&done);
396}
397
398void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100399 CreateSSE41FPToIntLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400400}
401
402void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
403 LocationSummary* locations = invoke->GetLocations();
404 if (locations->WillCall()) {
405 InvokeOutOfLineIntrinsic(codegen_, invoke);
406 return;
407 }
408
Mark Mendellfb8d2792015-03-31 22:16:59 -0400409 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
410 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700411 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
412 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
413 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400414 X86_64Assembler* assembler = GetAssembler();
415
Aart Bik349f3882016-08-02 15:40:56 -0700416 // Since no direct x86 rounding instruction matches the required semantics,
417 // this intrinsic is implemented as follows:
418 // result = floor(in);
419 // if (in - result >= 0.5)
420 // result = result + 1.0f;
421 __ movsd(t2, in);
422 __ roundsd(t1, in, Immediate(1));
423 __ subsd(t2, t1);
424 __ comisd(t2, codegen_->LiteralDoubleAddress(0.5));
425 __ j(kBelow, &skip_incr);
426 __ addsd(t1, codegen_->LiteralDoubleAddress(1.0f));
427 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400428
Aart Bik349f3882016-08-02 15:40:56 -0700429 // Final conversion to an integer. Unfortunately this also does not have a
430 // direct x86 instruction, since NaN should map to 0 and large positive
431 // values need to be clipped to the extreme value.
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600432 codegen_->Load64BitValue(out, kPrimLongMax);
Aart Bik349f3882016-08-02 15:40:56 -0700433 __ cvtsi2sd(t2, out, /* is64bit */ true);
434 __ comisd(t1, t2);
435 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
436 __ movl(out, Immediate(0)); // does not change flags, implicit zero extension to 64-bit
437 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
438 __ cvttsd2si(out, t1, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400439 __ Bind(&done);
440}
441
Vladimir Markoca6fff82017-10-03 14:49:14 +0100442static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
443 LocationSummary* locations =
444 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400445 InvokeRuntimeCallingConvention calling_convention;
446 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
447 locations->SetOut(Location::FpuRegisterLocation(XMM0));
448
449 // We have to ensure that the native code doesn't clobber the XMM registers which are
450 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
451 // saved in the prologue and properly restored.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100452 for (FloatRegister fp_reg : non_volatile_xmm_regs) {
Mark Mendella4f12202015-08-06 15:23:34 -0400453 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
454 }
455}
456
457static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
458 QuickEntrypointEnum entry) {
459 LocationSummary* locations = invoke->GetLocations();
460 DCHECK(locations->WillCall());
461 DCHECK(invoke->IsInvokeStaticOrDirect());
Mark Mendella4f12202015-08-06 15:23:34 -0400462
Serban Constantinescuba45db02016-07-12 22:53:02 +0100463 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400464}
465
466void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100467 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400468}
469
470void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
471 GenFPToFPCall(invoke, codegen_, kQuickCos);
472}
473
474void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100475 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400476}
477
478void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
479 GenFPToFPCall(invoke, codegen_, kQuickSin);
480}
481
482void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100483 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400484}
485
486void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
487 GenFPToFPCall(invoke, codegen_, kQuickAcos);
488}
489
490void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100491 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400492}
493
494void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
495 GenFPToFPCall(invoke, codegen_, kQuickAsin);
496}
497
498void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100499 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400500}
501
502void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
503 GenFPToFPCall(invoke, codegen_, kQuickAtan);
504}
505
506void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100507 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400508}
509
510void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
511 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
512}
513
514void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100515 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400516}
517
518void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
519 GenFPToFPCall(invoke, codegen_, kQuickCosh);
520}
521
522void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100523 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400524}
525
526void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
527 GenFPToFPCall(invoke, codegen_, kQuickExp);
528}
529
530void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100531 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400532}
533
534void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
535 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
536}
537
538void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100539 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400540}
541
542void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
543 GenFPToFPCall(invoke, codegen_, kQuickLog);
544}
545
546void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100547 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400548}
549
550void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
551 GenFPToFPCall(invoke, codegen_, kQuickLog10);
552}
553
554void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100555 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400556}
557
558void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
559 GenFPToFPCall(invoke, codegen_, kQuickSinh);
560}
561
562void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100563 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400564}
565
566void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
567 GenFPToFPCall(invoke, codegen_, kQuickTan);
568}
569
570void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100571 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400572}
573
574void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
575 GenFPToFPCall(invoke, codegen_, kQuickTanh);
576}
577
Vladimir Markoca6fff82017-10-03 14:49:14 +0100578static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
579 LocationSummary* locations =
580 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400581 InvokeRuntimeCallingConvention calling_convention;
582 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
583 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
584 locations->SetOut(Location::FpuRegisterLocation(XMM0));
585
586 // We have to ensure that the native code doesn't clobber the XMM registers which are
587 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
588 // saved in the prologue and properly restored.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100589 for (FloatRegister fp_reg : non_volatile_xmm_regs) {
Mark Mendella4f12202015-08-06 15:23:34 -0400590 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
591 }
592}
593
594void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100595 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400596}
597
598void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
599 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
600}
601
Vladimir Marko4d179872018-01-19 14:50:10 +0000602void IntrinsicLocationsBuilderX86_64::VisitMathPow(HInvoke* invoke) {
603 CreateFPFPToFPCallLocations(allocator_, invoke);
604}
605
606void IntrinsicCodeGeneratorX86_64::VisitMathPow(HInvoke* invoke) {
607 GenFPToFPCall(invoke, codegen_, kQuickPow);
608}
609
Mark Mendella4f12202015-08-06 15:23:34 -0400610void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100611 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400612}
613
614void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
615 GenFPToFPCall(invoke, codegen_, kQuickHypot);
616}
617
618void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100619 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400620}
621
622void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
623 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
624}
625
Mark Mendell6bc53a92015-07-01 14:26:52 -0400626void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
627 // Check to see if we have known failures that will cause us to have to bail out
628 // to the runtime, and just generate the runtime call directly.
629 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
630 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
631
632 // The positions must be non-negative.
633 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
634 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
635 // We will have to fail anyways.
636 return;
637 }
638
639 // The length must be > 0.
640 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
641 if (length != nullptr) {
642 int32_t len = length->GetValue();
643 if (len < 0) {
644 // Just call as normal.
645 return;
646 }
647 }
648
Vladimir Markoca6fff82017-10-03 14:49:14 +0100649 LocationSummary* locations =
650 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100651 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400652 locations->SetInAt(0, Location::RequiresRegister());
653 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
654 locations->SetInAt(2, Location::RequiresRegister());
655 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
656 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
657
658 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
659 locations->AddTemp(Location::RegisterLocation(RSI));
660 locations->AddTemp(Location::RegisterLocation(RDI));
661 locations->AddTemp(Location::RegisterLocation(RCX));
662}
663
664static void CheckPosition(X86_64Assembler* assembler,
665 Location pos,
666 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100667 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700668 SlowPathCode* slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100669 CpuRegister temp,
670 bool length_is_input_length = false) {
671 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400672 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
673
674 if (pos.IsConstant()) {
675 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
676 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100677 if (!length_is_input_length) {
678 // Check that length(input) >= length.
679 if (length.IsConstant()) {
680 __ cmpl(Address(input, length_offset),
681 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
682 } else {
683 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
684 }
685 __ j(kLess, slow_path->GetEntryLabel());
686 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400687 } else {
688 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100689 __ movl(temp, Address(input, length_offset));
690 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400691 __ j(kLess, slow_path->GetEntryLabel());
692
693 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100694 if (length.IsConstant()) {
695 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
696 } else {
697 __ cmpl(temp, length.AsRegister<CpuRegister>());
698 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400699 __ j(kLess, slow_path->GetEntryLabel());
700 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100701 } else if (length_is_input_length) {
702 // The only way the copy can succeed is if pos is zero.
703 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
704 __ testl(pos_reg, pos_reg);
705 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400706 } else {
707 // Check that pos >= 0.
708 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
709 __ testl(pos_reg, pos_reg);
710 __ j(kLess, slow_path->GetEntryLabel());
711
712 // Check that pos <= length(input).
713 __ cmpl(Address(input, length_offset), pos_reg);
714 __ j(kLess, slow_path->GetEntryLabel());
715
716 // Check that (length(input) - pos) >= length.
717 __ movl(temp, Address(input, length_offset));
718 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100719 if (length.IsConstant()) {
720 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
721 } else {
722 __ cmpl(temp, length.AsRegister<CpuRegister>());
723 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400724 __ j(kLess, slow_path->GetEntryLabel());
725 }
726}
727
728void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
729 X86_64Assembler* assembler = GetAssembler();
730 LocationSummary* locations = invoke->GetLocations();
731
732 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100733 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400734 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100735 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400736 Location length = locations->InAt(4);
737
738 // Temporaries that we need for MOVSW.
739 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
740 DCHECK_EQ(src_base.AsRegister(), RSI);
741 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
742 DCHECK_EQ(dest_base.AsRegister(), RDI);
743 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
744 DCHECK_EQ(count.AsRegister(), RCX);
745
Vladimir Marko174b2e22017-10-12 13:34:49 +0100746 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400747 codegen_->AddSlowPath(slow_path);
748
749 // Bail out if the source and destination are the same.
750 __ cmpl(src, dest);
751 __ j(kEqual, slow_path->GetEntryLabel());
752
753 // Bail out if the source is null.
754 __ testl(src, src);
755 __ j(kEqual, slow_path->GetEntryLabel());
756
757 // Bail out if the destination is null.
758 __ testl(dest, dest);
759 __ j(kEqual, slow_path->GetEntryLabel());
760
761 // If the length is negative, bail out.
762 // We have already checked in the LocationsBuilder for the constant case.
763 if (!length.IsConstant()) {
764 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
765 __ j(kLess, slow_path->GetEntryLabel());
766 }
767
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100768 // Validity checks: source. Use src_base as a temporary register.
769 CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100770
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100771 // Validity checks: dest. Use src_base as a temporary register.
772 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100773
Mark Mendell6bc53a92015-07-01 14:26:52 -0400774 // We need the count in RCX.
775 if (length.IsConstant()) {
776 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
777 } else {
778 __ movl(count, length.AsRegister<CpuRegister>());
779 }
780
Mark Mendell6bc53a92015-07-01 14:26:52 -0400781 // Okay, everything checks out. Finally time to do the copy.
782 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100783 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400784 DCHECK_EQ(char_size, 2u);
785
786 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
787
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100788 if (src_pos.IsConstant()) {
789 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
790 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400791 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100792 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400793 ScaleFactor::TIMES_2, data_offset));
794 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100795 if (dest_pos.IsConstant()) {
796 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
797 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400798 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100799 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400800 ScaleFactor::TIMES_2, data_offset));
801 }
802
803 // Do the move.
804 __ rep_movsw();
805
806 __ Bind(slow_path->GetExitLabel());
807}
808
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100809
810void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +0100811 // The only read barrier implementation supporting the
812 // SystemArrayCopy intrinsic is the Baker-style read barriers.
813 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +0100814 return;
815 }
816
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100817 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100818}
819
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000820// Compute base source address, base destination address, and end
821// source address for the System.arraycopy intrinsic in `src_base`,
822// `dst_base` and `src_end` respectively.
823static void GenSystemArrayCopyAddresses(X86_64Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100824 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000825 const CpuRegister& src,
826 const Location& src_pos,
827 const CpuRegister& dst,
828 const Location& dst_pos,
829 const Location& copy_length,
830 const CpuRegister& src_base,
831 const CpuRegister& dst_base,
832 const CpuRegister& src_end) {
833 // This routine is only used by the SystemArrayCopy intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100834 DCHECK_EQ(type, DataType::Type::kReference);
835 const int32_t element_size = DataType::Size(type);
836 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +0000837 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
838
839 if (src_pos.IsConstant()) {
840 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
841 __ leal(src_base, Address(src, element_size * constant + data_offset));
842 } else {
843 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
844 }
845
846 if (dst_pos.IsConstant()) {
847 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
848 __ leal(dst_base, Address(dst, element_size * constant + data_offset));
849 } else {
850 __ leal(dst_base, Address(dst, dst_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
851 }
852
853 if (copy_length.IsConstant()) {
854 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
855 __ leal(src_end, Address(src_base, element_size * constant));
856 } else {
857 __ leal(src_end, Address(src_base, copy_length.AsRegister<CpuRegister>(), scale_factor, 0));
858 }
859}
860
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100861void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +0100862 // The only read barrier implementation supporting the
863 // SystemArrayCopy intrinsic is the Baker-style read barriers.
864 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +0100865
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100866 X86_64Assembler* assembler = GetAssembler();
867 LocationSummary* locations = invoke->GetLocations();
868
869 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
870 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
871 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
872 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +0100873 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100874
875 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
876 Location src_pos = locations->InAt(1);
877 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
878 Location dest_pos = locations->InAt(3);
879 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +0100880 Location temp1_loc = locations->GetTemp(0);
881 CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
882 Location temp2_loc = locations->GetTemp(1);
883 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
884 Location temp3_loc = locations->GetTemp(2);
885 CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
886 Location TMP_loc = Location::RegisterLocation(TMP);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100887
Vladimir Marko174b2e22017-10-12 13:34:49 +0100888 SlowPathCode* intrinsic_slow_path =
889 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +0100890 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100891
Roland Levillainebea3d22016-04-12 15:42:57 +0100892 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100893 SystemArrayCopyOptimizations optimizations(invoke);
894
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100895 // If source and destination are the same, we go to slow path if we need to do
896 // forward copying.
897 if (src_pos.IsConstant()) {
898 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
899 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +0100900 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
901 if (optimizations.GetDestinationIsSource()) {
902 // Checked when building locations.
903 DCHECK_GE(src_pos_constant, dest_pos_constant);
904 } else if (src_pos_constant < dest_pos_constant) {
905 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +0100906 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +0100907 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100908 } else {
909 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +0100910 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +0100911 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100912 }
913 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +0100914 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100915 }
916 } else {
917 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +0100918 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +0100919 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100920 }
921 if (dest_pos.IsConstant()) {
922 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
923 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +0100924 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100925 } else {
926 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +0100927 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100928 }
929 }
930
Roland Levillainebea3d22016-04-12 15:42:57 +0100931 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100932
933 if (!optimizations.GetSourceIsNotNull()) {
934 // Bail out if the source is null.
935 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +0100936 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100937 }
938
939 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
940 // Bail out if the destination is null.
941 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +0100942 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100943 }
944
945 // If the length is negative, bail out.
946 // We have already checked in the LocationsBuilder for the constant case.
947 if (!length.IsConstant() &&
948 !optimizations.GetCountIsSourceLength() &&
949 !optimizations.GetCountIsDestinationLength()) {
950 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +0100951 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100952 }
953
954 // Validity checks: source.
955 CheckPosition(assembler,
956 src_pos,
957 src,
958 length,
Roland Levillain0b671c02016-08-19 12:02:34 +0100959 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100960 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100961 optimizations.GetCountIsSourceLength());
962
963 // Validity checks: dest.
964 CheckPosition(assembler,
965 dest_pos,
966 dest,
967 length,
Roland Levillain0b671c02016-08-19 12:02:34 +0100968 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100969 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100970 optimizations.GetCountIsDestinationLength());
971
972 if (!optimizations.GetDoesNotNeedTypeCheck()) {
973 // Check whether all elements of the source array are assignable to the component
974 // type of the destination array. We do two checks: the classes are the same,
975 // or the destination is Object[]. If none of these checks succeed, we go to the
976 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +0100977
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100978 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +0100979 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
980 // /* HeapReference<Class> */ temp1 = dest->klass_
981 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +0000982 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +0100983 // Register `temp1` is not trashed by the read barrier emitted
984 // by GenerateFieldLoadWithBakerReadBarrier below, as that
985 // method produces a call to a ReadBarrierMarkRegX entry point,
986 // which saves all potentially live registers, including
987 // temporaries such a `temp1`.
988 // /* HeapReference<Class> */ temp2 = src->klass_
989 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +0000990 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +0100991 // If heap poisoning is enabled, `temp1` and `temp2` have been
992 // unpoisoned by the the previous calls to
993 // GenerateFieldLoadWithBakerReadBarrier.
994 } else {
995 // /* HeapReference<Class> */ temp1 = dest->klass_
996 __ movl(temp1, Address(dest, class_offset));
997 // /* HeapReference<Class> */ temp2 = src->klass_
998 __ movl(temp2, Address(src, class_offset));
999 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1000 !optimizations.GetSourceIsNonPrimitiveArray()) {
1001 // One or two of the references need to be unpoisoned. Unpoison them
1002 // both to make the identity check valid.
1003 __ MaybeUnpoisonHeapReference(temp1);
1004 __ MaybeUnpoisonHeapReference(temp2);
1005 did_unpoison = true;
1006 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001007 }
1008
1009 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1010 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001011 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1012 // /* HeapReference<Class> */ TMP = temp1->component_type_
1013 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001014 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001015 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1016 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1017 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1018 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1019 } else {
1020 // /* HeapReference<Class> */ TMP = temp1->component_type_
1021 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1022 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1023 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1024 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1025 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001026 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001027 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001028 }
1029
1030 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1031 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001032 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1033 // For the same reason given earlier, `temp1` is not trashed by the
1034 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1035 // /* HeapReference<Class> */ TMP = temp2->component_type_
1036 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001037 invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001038 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1039 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1040 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1041 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1042 } else {
1043 // /* HeapReference<Class> */ TMP = temp2->component_type_
1044 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1045 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1046 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1047 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1048 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001049 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001050 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001051 }
1052
1053 __ cmpl(temp1, temp2);
1054
1055 if (optimizations.GetDestinationIsTypedObjectArray()) {
1056 NearLabel do_copy;
1057 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001058 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1059 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1060 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001061 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001062 // We do not need to emit a read barrier for the following
1063 // heap reference load, as `temp1` is only used in a
1064 // comparison with null below, and this reference is not
1065 // kept afterwards.
1066 __ cmpl(Address(temp1, super_offset), Immediate(0));
1067 } else {
1068 if (!did_unpoison) {
1069 __ MaybeUnpoisonHeapReference(temp1);
1070 }
1071 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1072 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001073 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001074 // No need to unpoison the following heap reference load, as
1075 // we're comparing against null.
1076 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001077 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001078 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001079 __ Bind(&do_copy);
1080 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001081 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001082 }
1083 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1084 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1085 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001086 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1087 // /* HeapReference<Class> */ temp1 = src->klass_
1088 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001089 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001090 // /* HeapReference<Class> */ TMP = temp1->component_type_
1091 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001092 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001093 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1094 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1095 } else {
1096 // /* HeapReference<Class> */ temp1 = src->klass_
1097 __ movl(temp1, Address(src, class_offset));
1098 __ MaybeUnpoisonHeapReference(temp1);
1099 // /* HeapReference<Class> */ TMP = temp1->component_type_
1100 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1101 // No need to unpoison `TMP` now, as we're comparing against null.
1102 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1103 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1104 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1105 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001106 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001107 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001108 }
1109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001110 const DataType::Type type = DataType::Type::kReference;
1111 const int32_t element_size = DataType::Size(type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001112
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001113 // Compute base source address, base destination address, and end
1114 // source address in `temp1`, `temp2` and `temp3` respectively.
1115 GenSystemArrayCopyAddresses(
1116 GetAssembler(), type, src, src_pos, dest, dest_pos, length, temp1, temp2, temp3);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001117
Roland Levillain0b671c02016-08-19 12:02:34 +01001118 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1119 // SystemArrayCopy implementation for Baker read barriers (see
1120 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1121 //
1122 // if (src_ptr != end_ptr) {
1123 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1124 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001125 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01001126 // if (is_gray) {
1127 // // Slow-path copy.
1128 // do {
1129 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1130 // } while (src_ptr != end_ptr)
1131 // } else {
1132 // // Fast-path copy.
1133 // do {
1134 // *dest_ptr++ = *src_ptr++;
1135 // } while (src_ptr != end_ptr)
1136 // }
1137 // }
1138
1139 NearLabel loop, done;
1140
1141 // Don't enter copy loop if `length == 0`.
1142 __ cmpl(temp1, temp3);
1143 __ j(kEqual, &done);
1144
Vladimir Marko953437b2016-08-24 08:30:46 +00001145 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001146 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1147 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00001148 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
1149 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
1150 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
1151
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001152 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00001153 // goto slow_path;
1154 // At this point, just do the "if" and make sure that flags are preserved until the branch.
1155 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01001156
1157 // Load fence to prevent load-load reordering.
1158 // Note that this is a no-op, thanks to the x86-64 memory model.
1159 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1160
1161 // Slow path used to copy array when `src` is gray.
1162 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001163 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01001164 codegen_->AddSlowPath(read_barrier_slow_path);
1165
Vladimir Marko953437b2016-08-24 08:30:46 +00001166 // We have done the "if" of the gray bit check above, now branch based on the flags.
1167 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01001168
1169 // Fast-path copy.
1170 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1171 // poison/unpoison.
1172 __ Bind(&loop);
1173 __ movl(CpuRegister(TMP), Address(temp1, 0));
1174 __ movl(Address(temp2, 0), CpuRegister(TMP));
1175 __ addl(temp1, Immediate(element_size));
1176 __ addl(temp2, Immediate(element_size));
1177 __ cmpl(temp1, temp3);
1178 __ j(kNotEqual, &loop);
1179
1180 __ Bind(read_barrier_slow_path->GetExitLabel());
1181 __ Bind(&done);
1182 } else {
1183 // Non read barrier code.
1184
1185 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1186 // poison/unpoison.
1187 NearLabel loop, done;
1188 __ cmpl(temp1, temp3);
1189 __ j(kEqual, &done);
1190 __ Bind(&loop);
1191 __ movl(CpuRegister(TMP), Address(temp1, 0));
1192 __ movl(Address(temp2, 0), CpuRegister(TMP));
1193 __ addl(temp1, Immediate(element_size));
1194 __ addl(temp2, Immediate(element_size));
1195 __ cmpl(temp1, temp3);
1196 __ j(kNotEqual, &loop);
1197 __ Bind(&done);
1198 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001199
1200 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001201 codegen_->MarkGCCard(temp1, temp2, dest, CpuRegister(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001202
Roland Levillain0b671c02016-08-19 12:02:34 +01001203 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001204}
1205
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001206void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001207 LocationSummary* locations = new (allocator_) LocationSummary(
1208 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001209 InvokeRuntimeCallingConvention calling_convention;
1210 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1211 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1212 locations->SetOut(Location::RegisterLocation(RAX));
1213}
1214
1215void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1216 X86_64Assembler* assembler = GetAssembler();
1217 LocationSummary* locations = invoke->GetLocations();
1218
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001219 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001220 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001221
1222 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1223 __ testl(argument, argument);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001224 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001225 codegen_->AddSlowPath(slow_path);
1226 __ j(kEqual, slow_path->GetEntryLabel());
1227
Serban Constantinescuba45db02016-07-12 22:53:02 +01001228 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001229 __ Bind(slow_path->GetExitLabel());
1230}
1231
Agi Csakif8cfb202015-08-13 17:54:54 -07001232void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001233 if (kEmitCompilerReadBarrier &&
1234 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1235 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1236 // No support for this odd case (String class is moveable, not in the boot image).
1237 return;
1238 }
1239
Vladimir Markoca6fff82017-10-03 14:49:14 +01001240 LocationSummary* locations =
1241 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakif8cfb202015-08-13 17:54:54 -07001242 locations->SetInAt(0, Location::RequiresRegister());
1243 locations->SetInAt(1, Location::RequiresRegister());
1244
1245 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1246 locations->AddTemp(Location::RegisterLocation(RCX));
1247 locations->AddTemp(Location::RegisterLocation(RDI));
1248
1249 // Set output, RSI needed for repe_cmpsq instruction anyways.
1250 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1251}
1252
1253void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1254 X86_64Assembler* assembler = GetAssembler();
1255 LocationSummary* locations = invoke->GetLocations();
1256
1257 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1258 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1259 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1260 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1261 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1262
Mark Mendell0c9497d2015-08-21 09:30:05 -04001263 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001264
1265 // Get offsets of count, value, and class fields within a string object.
1266 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1267 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1268 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1269
1270 // Note that the null check must have been done earlier.
1271 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1272
Vladimir Marko53b52002016-05-24 19:30:45 +01001273 StringEqualsOptimizations optimizations(invoke);
1274 if (!optimizations.GetArgumentNotNull()) {
1275 // Check if input is null, return false if it is.
1276 __ testl(arg, arg);
1277 __ j(kEqual, &return_false);
1278 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001279
Vladimir Marko53b52002016-05-24 19:30:45 +01001280 if (!optimizations.GetArgumentIsString()) {
1281 // Instanceof check for the argument by comparing class fields.
1282 // All string objects must have the same type since String cannot be subclassed.
1283 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1284 // If the argument is a string object, its class field must be equal to receiver's class field.
1285 __ movl(rcx, Address(str, class_offset));
1286 __ cmpl(rcx, Address(arg, class_offset));
1287 __ j(kNotEqual, &return_false);
1288 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001289
1290 // Reference equality check, return true if same reference.
1291 __ cmpl(str, arg);
1292 __ j(kEqual, &return_true);
1293
jessicahandojo4877b792016-09-08 19:49:13 -07001294 // Load length and compression flag of receiver string.
Agi Csakif8cfb202015-08-13 17:54:54 -07001295 __ movl(rcx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001296 // Check if lengths and compressiond flags are equal, return false if they're not.
1297 // Two identical strings will always have same compression style since
1298 // compression style is decided on alloc.
Agi Csakif8cfb202015-08-13 17:54:54 -07001299 __ cmpl(rcx, Address(arg, count_offset));
1300 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001301 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1302 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1303 "Expecting 0=compressed, 1=uncompressed");
1304 __ jrcxz(&return_true);
jessicahandojo4877b792016-09-08 19:49:13 -07001305
1306 if (mirror::kUseStringCompression) {
1307 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001308 // Extract length and differentiate between both compressed or both uncompressed.
1309 // Different compression style is cut above.
1310 __ shrl(rcx, Immediate(1));
1311 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001312 // Divide string length by 2, rounding up, and continue as if uncompressed.
1313 // Merge clearing the compression flag with +1 for rounding.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001314 __ addl(rcx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001315 __ shrl(rcx, Immediate(1));
1316 __ Bind(&string_uncompressed);
1317 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001318 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1319 __ leal(rsi, Address(str, value_offset));
1320 __ leal(rdi, Address(arg, value_offset));
1321
1322 // Divide string length by 4 and adjust for lengths not divisible by 4.
1323 __ addl(rcx, Immediate(3));
1324 __ shrl(rcx, Immediate(2));
1325
jessicahandojo4877b792016-09-08 19:49:13 -07001326 // Assertions that must hold in order to compare strings 4 characters (uncompressed)
1327 // or 8 characters (compressed) at a time.
Agi Csakif8cfb202015-08-13 17:54:54 -07001328 DCHECK_ALIGNED(value_offset, 8);
1329 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1330
1331 // Loop to compare strings four characters at a time starting at the beginning of the string.
1332 __ repe_cmpsq();
1333 // If strings are not equal, zero flag will be cleared.
1334 __ j(kNotEqual, &return_false);
1335
1336 // Return true and exit the function.
1337 // If loop does not result in returning false, we return true.
1338 __ Bind(&return_true);
1339 __ movl(rsi, Immediate(1));
1340 __ jmp(&end);
1341
1342 // Return false and exit the function.
1343 __ Bind(&return_false);
1344 __ xorl(rsi, rsi);
1345 __ Bind(&end);
1346}
1347
Andreas Gampe21030dd2015-05-07 14:46:15 -07001348static void CreateStringIndexOfLocations(HInvoke* invoke,
1349 ArenaAllocator* allocator,
1350 bool start_at_zero) {
1351 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1352 LocationSummary::kCallOnSlowPath,
1353 kIntrinsified);
1354 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1355 locations->SetInAt(0, Location::RegisterLocation(RDI));
1356 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1357 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1358 // of the instruction explicitly.
1359 // Note: This works as we don't clobber RAX anywhere.
1360 locations->SetInAt(1, Location::RegisterLocation(RAX));
1361 if (!start_at_zero) {
1362 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1363 }
1364 // As we clobber RDI during execution anyways, also use it as the output.
1365 locations->SetOut(Location::SameAsFirstInput());
1366
1367 // repne scasw uses RCX as the counter.
1368 locations->AddTemp(Location::RegisterLocation(RCX));
1369 // Need another temporary to be able to compute the result.
1370 locations->AddTemp(Location::RequiresRegister());
1371}
1372
1373static void GenerateStringIndexOf(HInvoke* invoke,
1374 X86_64Assembler* assembler,
1375 CodeGeneratorX86_64* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001376 bool start_at_zero) {
1377 LocationSummary* locations = invoke->GetLocations();
1378
1379 // Note that the null check must have been done earlier.
1380 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1381
1382 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1383 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1384 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1385 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1386 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1387
1388 // Check our assumptions for registers.
1389 DCHECK_EQ(string_obj.AsRegister(), RDI);
1390 DCHECK_EQ(search_value.AsRegister(), RAX);
1391 DCHECK_EQ(counter.AsRegister(), RCX);
1392 DCHECK_EQ(out.AsRegister(), RDI);
1393
1394 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001395 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001396 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001397 HInstruction* code_point = invoke->InputAt(1);
1398 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001399 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001400 std::numeric_limits<uint16_t>::max()) {
1401 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1402 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001403 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001404 codegen->AddSlowPath(slow_path);
1405 __ jmp(slow_path->GetEntryLabel());
1406 __ Bind(slow_path->GetExitLabel());
1407 return;
1408 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001409 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001410 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001411 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001412 codegen->AddSlowPath(slow_path);
1413 __ j(kAbove, slow_path->GetEntryLabel());
1414 }
1415
jessicahandojo4877b792016-09-08 19:49:13 -07001416 // From here down, we know that we are looking for a char that fits in
1417 // 16 bits (uncompressed) or 8 bits (compressed).
Andreas Gampe21030dd2015-05-07 14:46:15 -07001418 // Location of reference to data array within the String object.
1419 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1420 // Location of count within the String object.
1421 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1422
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001423 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001424 __ movl(string_length, Address(string_obj, count_offset));
1425
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001426 // Do a zero-length check. Even with string compression `count == 0` means empty.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001427 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001428 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001429 __ testl(string_length, string_length);
1430 __ j(kEqual, &not_found_label);
1431
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001432 if (mirror::kUseStringCompression) {
1433 // Use TMP to keep string_length_flagged.
1434 __ movl(CpuRegister(TMP), string_length);
1435 // Mask out first bit used as compression flag.
1436 __ shrl(string_length, Immediate(1));
1437 }
1438
Andreas Gampe21030dd2015-05-07 14:46:15 -07001439 if (start_at_zero) {
1440 // Number of chars to scan is the same as the string length.
1441 __ movl(counter, string_length);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001442 // Move to the start of the string.
1443 __ addq(string_obj, Immediate(value_offset));
1444 } else {
1445 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1446
1447 // Do a start_index check.
1448 __ cmpl(start_index, string_length);
1449 __ j(kGreaterEqual, &not_found_label);
1450
1451 // Ensure we have a start index >= 0;
1452 __ xorl(counter, counter);
1453 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001454 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001455
jessicahandojo4877b792016-09-08 19:49:13 -07001456 if (mirror::kUseStringCompression) {
1457 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001458 __ testl(CpuRegister(TMP), Immediate(1));
1459 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001460 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1461 __ jmp(&modify_counter);
1462 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1463 __ Bind(&offset_uncompressed_label);
1464 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1465 __ Bind(&modify_counter);
1466 } else {
1467 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1468 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001469 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1470 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1471 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1472 }
1473
jessicahandojo4877b792016-09-08 19:49:13 -07001474 if (mirror::kUseStringCompression) {
1475 NearLabel uncompressed_string_comparison;
1476 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001477 __ testl(CpuRegister(TMP), Immediate(1));
1478 __ j(kNotZero, &uncompressed_string_comparison);
jessicahandojo4877b792016-09-08 19:49:13 -07001479 // Check if RAX (search_value) is ASCII.
1480 __ cmpl(search_value, Immediate(127));
1481 __ j(kGreater, &not_found_label);
1482 // Comparing byte-per-byte.
1483 __ repne_scasb();
1484 __ jmp(&comparison_done);
1485 // Everything is set up for repne scasw:
1486 // * Comparison address in RDI.
1487 // * Counter in ECX.
1488 __ Bind(&uncompressed_string_comparison);
1489 __ repne_scasw();
1490 __ Bind(&comparison_done);
1491 } else {
1492 __ repne_scasw();
1493 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001494 // Did we find a match?
1495 __ j(kNotEqual, &not_found_label);
1496
1497 // Yes, we matched. Compute the index of the result.
1498 __ subl(string_length, counter);
1499 __ leal(out, Address(string_length, -1));
1500
Mark Mendell0c9497d2015-08-21 09:30:05 -04001501 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001502 __ jmp(&done);
1503
1504 // Failed to match; return -1.
1505 __ Bind(&not_found_label);
1506 __ movl(out, Immediate(-1));
1507
1508 // And join up at the end.
1509 __ Bind(&done);
1510 if (slow_path != nullptr) {
1511 __ Bind(slow_path->GetExitLabel());
1512 }
1513}
1514
1515void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001516 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001517}
1518
1519void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001520 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001521}
1522
1523void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001524 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001525}
1526
1527void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001528 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001529}
1530
Jeff Hao848f70a2014-01-15 13:49:50 -08001531void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001532 LocationSummary* locations = new (allocator_) LocationSummary(
1533 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001534 InvokeRuntimeCallingConvention calling_convention;
1535 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1536 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1537 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1538 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1539 locations->SetOut(Location::RegisterLocation(RAX));
1540}
1541
1542void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1543 X86_64Assembler* assembler = GetAssembler();
1544 LocationSummary* locations = invoke->GetLocations();
1545
1546 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1547 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001548 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001549 codegen_->AddSlowPath(slow_path);
1550 __ j(kEqual, slow_path->GetEntryLabel());
1551
Serban Constantinescuba45db02016-07-12 22:53:02 +01001552 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001553 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001554 __ Bind(slow_path->GetExitLabel());
1555}
1556
1557void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001558 LocationSummary* locations =
1559 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001560 InvokeRuntimeCallingConvention calling_convention;
1561 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1562 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1563 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1564 locations->SetOut(Location::RegisterLocation(RAX));
1565}
1566
1567void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001568 // No need to emit code checking whether `locations->InAt(2)` is a null
1569 // pointer, as callers of the native method
1570 //
1571 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1572 //
1573 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001574 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001575 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001576}
1577
1578void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001579 LocationSummary* locations = new (allocator_) LocationSummary(
1580 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001581 InvokeRuntimeCallingConvention calling_convention;
1582 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1583 locations->SetOut(Location::RegisterLocation(RAX));
1584}
1585
1586void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1587 X86_64Assembler* assembler = GetAssembler();
1588 LocationSummary* locations = invoke->GetLocations();
1589
1590 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1591 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001592 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001593 codegen_->AddSlowPath(slow_path);
1594 __ j(kEqual, slow_path->GetEntryLabel());
1595
Serban Constantinescuba45db02016-07-12 22:53:02 +01001596 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001597 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001598 __ Bind(slow_path->GetExitLabel());
1599}
1600
Mark Mendell8f8926a2015-08-17 11:39:06 -04001601void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1602 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001603 LocationSummary* locations =
1604 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001605 locations->SetInAt(0, Location::RequiresRegister());
1606 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1607 locations->SetInAt(2, Location::RequiresRegister());
1608 locations->SetInAt(3, Location::RequiresRegister());
1609 locations->SetInAt(4, Location::RequiresRegister());
1610
1611 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1612 locations->AddTemp(Location::RegisterLocation(RSI));
1613 locations->AddTemp(Location::RegisterLocation(RDI));
1614 locations->AddTemp(Location::RegisterLocation(RCX));
1615}
1616
1617void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1618 X86_64Assembler* assembler = GetAssembler();
1619 LocationSummary* locations = invoke->GetLocations();
1620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001621 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001622 // Location of data in char array buffer.
1623 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1624 // Location of char array data in string.
1625 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1626
1627 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1628 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1629 Location srcBegin = locations->InAt(1);
1630 int srcBegin_value =
1631 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1632 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1633 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1634 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1635
1636 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001637 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001638 DCHECK_EQ(char_size, 2u);
1639
jessicahandojo4877b792016-09-08 19:49:13 -07001640 NearLabel done;
Mark Mendell8f8926a2015-08-17 11:39:06 -04001641 // Compute the number of chars (words) to move.
1642 __ movl(CpuRegister(RCX), srcEnd);
1643 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001644 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001645 } else {
1646 DCHECK(srcBegin.IsRegister());
1647 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1648 }
jessicahandojo4877b792016-09-08 19:49:13 -07001649 if (mirror::kUseStringCompression) {
1650 NearLabel copy_uncompressed, copy_loop;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001651 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001652 DCHECK_EQ(c_char_size, 1u);
1653 // Location of count in string.
1654 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Mark Mendell8f8926a2015-08-17 11:39:06 -04001655
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001656 __ testl(Address(obj, count_offset), Immediate(1));
1657 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1658 "Expecting 0=compressed, 1=uncompressed");
1659 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001660 // Compute the address of the source string by adding the number of chars from
1661 // the source beginning to the value offset of a string.
1662 __ leaq(CpuRegister(RSI),
1663 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1664 // Start the loop to copy String's value to Array of Char.
1665 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1666
1667 __ Bind(&copy_loop);
1668 __ jrcxz(&done);
1669 // Use TMP as temporary (convert byte from RSI to word).
1670 // TODO: Selecting RAX as the temporary and using LODSB/STOSW.
1671 __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
1672 __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
1673 __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
1674 __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
1675 // TODO: Add support for LOOP to X86_64Assembler.
1676 __ subl(CpuRegister(RCX), Immediate(1));
1677 __ jmp(&copy_loop);
1678
1679 __ Bind(&copy_uncompressed);
1680 }
1681
1682 __ leaq(CpuRegister(RSI),
1683 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
1684 // Compute the address of the destination buffer.
1685 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001686 // Do the move.
1687 __ rep_movsw();
jessicahandojo4877b792016-09-08 19:49:13 -07001688
1689 __ Bind(&done);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001690}
1691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692static void GenPeek(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001693 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1694 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1695 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1696 // to avoid a SIGBUS.
1697 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001698 case DataType::Type::kInt8:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001699 __ movsxb(out, Address(address, 0));
1700 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001701 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001702 __ movsxw(out, Address(address, 0));
1703 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001704 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001705 __ movl(out, Address(address, 0));
1706 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001708 __ movq(out, Address(address, 0));
1709 break;
1710 default:
1711 LOG(FATAL) << "Type not recognized for peek: " << size;
1712 UNREACHABLE();
1713 }
1714}
1715
1716void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001717 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001718}
1719
1720void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001721 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001722}
1723
1724void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001726}
1727
1728void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001729 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001730}
1731
1732void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001733 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001734}
1735
1736void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001737 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001738}
1739
1740void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001741 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001742}
1743
1744void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001745 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001746}
1747
Vladimir Markoca6fff82017-10-03 14:49:14 +01001748static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
1749 LocationSummary* locations =
1750 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001751 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001752 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001753}
1754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001755static void GenPoke(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001756 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001757 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001758 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1759 // to avoid a SIGBUS.
1760 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001761 case DataType::Type::kInt8:
Mark Mendell40741f32015-04-20 22:10:34 -04001762 if (value.IsConstant()) {
1763 __ movb(Address(address, 0),
1764 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1765 } else {
1766 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1767 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001768 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001769 case DataType::Type::kInt16:
Mark Mendell40741f32015-04-20 22:10:34 -04001770 if (value.IsConstant()) {
1771 __ movw(Address(address, 0),
1772 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1773 } else {
1774 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1775 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001776 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001777 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04001778 if (value.IsConstant()) {
1779 __ movl(Address(address, 0),
1780 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1781 } else {
1782 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1783 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001784 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001785 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04001786 if (value.IsConstant()) {
1787 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1788 DCHECK(IsInt<32>(v));
1789 int32_t v_32 = v;
1790 __ movq(Address(address, 0), Immediate(v_32));
1791 } else {
1792 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1793 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001794 break;
1795 default:
1796 LOG(FATAL) << "Type not recognized for poke: " << size;
1797 UNREACHABLE();
1798 }
1799}
1800
1801void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001802 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001803}
1804
1805void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001806 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001807}
1808
1809void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001810 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001811}
1812
1813void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001814 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001815}
1816
1817void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001818 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001819}
1820
1821void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001823}
1824
1825void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001826 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001827}
1828
1829void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001831}
1832
1833void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001834 LocationSummary* locations =
1835 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001836 locations->SetOut(Location::RequiresRegister());
1837}
1838
1839void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1840 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001841 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001842 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001843}
1844
Roland Levillain0d5a2812015-11-13 10:07:31 +00001845static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001846 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001847 bool is_volatile ATTRIBUTE_UNUSED,
1848 CodeGeneratorX86_64* codegen) {
1849 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
1850 LocationSummary* locations = invoke->GetLocations();
1851 Location base_loc = locations->InAt(1);
1852 CpuRegister base = base_loc.AsRegister<CpuRegister>();
1853 Location offset_loc = locations->InAt(2);
1854 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
1855 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001856 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001857
Andreas Gampe878d58c2015-01-15 23:24:00 -08001858 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001859 case DataType::Type::kInt32:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001860 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001861 break;
1862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001864 if (kEmitCompilerReadBarrier) {
1865 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001866 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1867 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001868 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001869 } else {
1870 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1871 codegen->GenerateReadBarrierSlow(
1872 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1873 }
1874 } else {
1875 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1876 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001877 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001878 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001879 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001881 case DataType::Type::kInt64:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001882 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08001883 break;
1884
1885 default:
1886 LOG(FATAL) << "Unsupported op size " << type;
1887 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001888 }
1889}
1890
Vladimir Markoca6fff82017-10-03 14:49:14 +01001891static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001892 bool can_call = kEmitCompilerReadBarrier &&
1893 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1894 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001895 LocationSummary* locations =
1896 new (allocator) LocationSummary(invoke,
1897 can_call
1898 ? LocationSummary::kCallOnSlowPath
1899 : LocationSummary::kNoCall,
1900 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01001901 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001902 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001903 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001904 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001905 locations->SetInAt(1, Location::RequiresRegister());
1906 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001907 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001908 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001909}
1910
1911void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001912 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001913}
1914void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001915 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001916}
1917void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001918 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001919}
1920void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001921 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001922}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001923void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001924 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001925}
1926void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001927 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001928}
1929
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001930
1931void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001932 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001933}
1934void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001936}
1937void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001939}
1940void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001942}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001943void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001944 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001945}
1946void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001947 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001948}
1949
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001950
Vladimir Markoca6fff82017-10-03 14:49:14 +01001951static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001952 DataType::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001953 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001954 LocationSummary* locations =
1955 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001956 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001957 locations->SetInAt(1, Location::RequiresRegister());
1958 locations->SetInAt(2, Location::RequiresRegister());
1959 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001960 if (type == DataType::Type::kReference) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001961 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001962 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001963 locations->AddTemp(Location::RequiresRegister());
1964 }
1965}
1966
1967void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001968 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001969}
1970void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001971 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001972}
1973void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001974 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001975}
1976void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001977 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001978}
1979void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001980 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001981}
1982void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001983 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001984}
1985void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001986 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001987}
1988void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001989 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001990}
1991void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001992 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001993}
1994
1995// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1996// memory model.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997static void GenUnsafePut(LocationSummary* locations, DataType::Type type, bool is_volatile,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001998 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001999 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002000 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2001 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2002 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2003
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 if (type == DataType::Type::kInt64) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002005 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002006 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002007 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2008 __ movl(temp, value);
2009 __ PoisonHeapReference(temp);
2010 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002011 } else {
2012 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2013 }
2014
2015 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002016 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002017 }
2018
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002019 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002020 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002021 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2022 locations->GetTemp(1).AsRegister<CpuRegister>(),
2023 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002024 value,
2025 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002026 }
2027}
2028
2029void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002031}
2032void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002034}
2035void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002036 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002037}
2038void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 GenUnsafePut(
2040 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002041}
2042void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 GenUnsafePut(
2044 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002045}
2046void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 GenUnsafePut(
2048 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002049}
2050void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002052}
2053void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002054 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002055}
2056void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002058}
2059
Vladimir Markoca6fff82017-10-03 14:49:14 +01002060static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002062 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002063 bool can_call = kEmitCompilerReadBarrier &&
2064 kUseBakerReadBarrier &&
2065 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002066 LocationSummary* locations =
2067 new (allocator) LocationSummary(invoke,
2068 can_call
2069 ? LocationSummary::kCallOnSlowPath
2070 : LocationSummary::kNoCall,
2071 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002072 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2073 locations->SetInAt(1, Location::RequiresRegister());
2074 locations->SetInAt(2, Location::RequiresRegister());
2075 // expected value must be in EAX/RAX.
2076 locations->SetInAt(3, Location::RegisterLocation(RAX));
2077 locations->SetInAt(4, Location::RequiresRegister());
2078
2079 locations->SetOut(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002080 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002081 // Need temporary registers for card-marking, and possibly for
2082 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002083 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002084 locations->AddTemp(Location::RequiresRegister());
2085 }
2086}
2087
2088void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002089 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002090}
2091
2092void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002093 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002094}
2095
2096void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002097 // The only read barrier implementation supporting the
2098 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2099 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002100 return;
2101 }
2102
Vladimir Markoca6fff82017-10-03 14:49:14 +01002103 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002104}
2105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002107 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002108 LocationSummary* locations = invoke->GetLocations();
2109
2110 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2111 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2112 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002113 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002114 DCHECK_EQ(expected.AsRegister(), RAX);
2115 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002116 Location out_loc = locations->Out();
2117 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002120 // The only read barrier implementation supporting the
2121 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2122 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2123
2124 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2125 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2126
Roland Levillainb488b782015-10-22 11:38:49 +01002127 // Mark card for object assuming new value is stored.
2128 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002129 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2130
2131 // The address of the field within the holding object.
2132 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2133
2134 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2135 // Need to make sure the reference stored in the field is a to-space
2136 // one before attempting the CAS or the CAS could fail incorrectly.
2137 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2138 invoke,
2139 out_loc, // Unused, used only as a "temporary" within the read barrier.
2140 base,
2141 field_addr,
2142 /* needs_null_check */ false,
2143 /* always_update_field */ true,
2144 &temp1,
2145 &temp2);
2146 }
Roland Levillain4d027112015-07-01 15:41:14 +01002147
Roland Levillainb488b782015-10-22 11:38:49 +01002148 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2149 Register value_reg = value.AsRegister();
2150 if (kPoisonHeapReferences) {
2151 if (base_equals_value) {
2152 // If `base` and `value` are the same register location, move
2153 // `value_reg` to a temporary register. This way, poisoning
2154 // `value_reg` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002155 value_reg = temp1.AsRegister();
Roland Levillainb488b782015-10-22 11:38:49 +01002156 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002157 }
Roland Levillainb488b782015-10-22 11:38:49 +01002158
2159 // Check that the register allocator did not assign the location
2160 // of `expected` (RAX) to `value` nor to `base`, so that heap
2161 // poisoning (when enabled) works as intended below.
2162 // - If `value` were equal to `expected`, both references would
2163 // be poisoned twice, meaning they would not be poisoned at
2164 // all, as heap poisoning uses address negation.
2165 // - If `base` were equal to `expected`, poisoning `expected`
2166 // would invalidate `base`.
2167 DCHECK_NE(value_reg, expected.AsRegister());
2168 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2169
2170 __ PoisonHeapReference(expected);
2171 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002172 }
2173
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002174 __ LockCmpxchgl(field_addr, CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002175
Roland Levillain0d5a2812015-11-13 10:07:31 +00002176 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002177 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002178
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002179 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002180 __ setcc(kZero, out);
2181 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002182
Roland Levillain391b8662015-12-18 11:43:38 +00002183 // If heap poisoning is enabled, we need to unpoison the values
2184 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002185 if (kPoisonHeapReferences) {
2186 if (base_equals_value) {
2187 // `value_reg` has been moved to a temporary register, no need
2188 // to unpoison it.
2189 } else {
2190 // Ensure `value` is different from `out`, so that unpoisoning
2191 // the former does not invalidate the latter.
2192 DCHECK_NE(value_reg, out.AsRegister());
2193 __ UnpoisonHeapReference(CpuRegister(value_reg));
2194 }
2195 // Ensure `expected` is different from `out`, so that unpoisoning
2196 // the former does not invalidate the latter.
2197 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2198 __ UnpoisonHeapReference(expected);
2199 }
2200 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002202 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002203 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002204 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2205 } else {
2206 LOG(FATAL) << "Unexpected CAS type " << type;
2207 }
2208
Roland Levillain0d5a2812015-11-13 10:07:31 +00002209 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002210 // scheduling barriers at this time.
2211
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002212 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002213 __ setcc(kZero, out);
2214 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002215 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002216}
2217
2218void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002219 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002220}
2221
2222void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002224}
2225
2226void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002227 // The only read barrier implementation supporting the
2228 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2229 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002232}
2233
2234void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002235 LocationSummary* locations =
2236 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002237 locations->SetInAt(0, Location::RequiresRegister());
2238 locations->SetOut(Location::SameAsFirstInput());
2239 locations->AddTemp(Location::RequiresRegister());
2240}
2241
2242static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2243 X86_64Assembler* assembler) {
2244 Immediate imm_shift(shift);
2245 Immediate imm_mask(mask);
2246 __ movl(temp, reg);
2247 __ shrl(reg, imm_shift);
2248 __ andl(temp, imm_mask);
2249 __ andl(reg, imm_mask);
2250 __ shll(temp, imm_shift);
2251 __ orl(reg, temp);
2252}
2253
2254void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002255 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002256 LocationSummary* locations = invoke->GetLocations();
2257
2258 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2259 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2260
2261 /*
2262 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2263 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2264 * compared to generic luni implementation which has 5 rounds of swapping bits.
2265 * x = bswap x
2266 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2267 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2268 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2269 */
2270 __ bswapl(reg);
2271 SwapBits(reg, temp, 1, 0x55555555, assembler);
2272 SwapBits(reg, temp, 2, 0x33333333, assembler);
2273 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2274}
2275
2276void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002277 LocationSummary* locations =
2278 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002279 locations->SetInAt(0, Location::RequiresRegister());
2280 locations->SetOut(Location::SameAsFirstInput());
2281 locations->AddTemp(Location::RequiresRegister());
2282 locations->AddTemp(Location::RequiresRegister());
2283}
2284
2285static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2286 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2287 Immediate imm_shift(shift);
2288 __ movq(temp_mask, Immediate(mask));
2289 __ movq(temp, reg);
2290 __ shrq(reg, imm_shift);
2291 __ andq(temp, temp_mask);
2292 __ andq(reg, temp_mask);
2293 __ shlq(temp, imm_shift);
2294 __ orq(reg, temp);
2295}
2296
2297void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002298 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002299 LocationSummary* locations = invoke->GetLocations();
2300
2301 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2302 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2303 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2304
2305 /*
2306 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2307 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2308 * compared to generic luni implementation which has 5 rounds of swapping bits.
2309 * x = bswap x
2310 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2311 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2312 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2313 */
2314 __ bswapq(reg);
2315 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2316 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2317 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2318}
2319
Aart Bik3f67e692016-01-15 14:35:12 -08002320static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002321 ArenaAllocator* allocator, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Aart Bik3f67e692016-01-15 14:35:12 -08002322 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2323 // Do nothing if there is no popcnt support. This results in generating
2324 // a call for the intrinsic rather than direct code.
2325 return;
2326 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002327 LocationSummary* locations =
2328 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bik3f67e692016-01-15 14:35:12 -08002329 locations->SetInAt(0, Location::Any());
2330 locations->SetOut(Location::RequiresRegister());
2331}
2332
Aart Bikc5d47542016-01-27 17:00:35 -08002333static void GenBitCount(X86_64Assembler* assembler,
2334 CodeGeneratorX86_64* codegen,
2335 HInvoke* invoke,
2336 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002337 LocationSummary* locations = invoke->GetLocations();
2338 Location src = locations->InAt(0);
2339 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2340
2341 if (invoke->InputAt(0)->IsConstant()) {
2342 // Evaluate this at compile time.
2343 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002344 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002345 ? POPCOUNT(static_cast<uint64_t>(value))
2346 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002347 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002348 return;
2349 }
2350
2351 if (src.IsRegister()) {
2352 if (is_long) {
2353 __ popcntq(out, src.AsRegister<CpuRegister>());
2354 } else {
2355 __ popcntl(out, src.AsRegister<CpuRegister>());
2356 }
2357 } else if (is_long) {
2358 DCHECK(src.IsDoubleStackSlot());
2359 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2360 } else {
2361 DCHECK(src.IsStackSlot());
2362 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2363 }
2364}
2365
2366void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002367 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002368}
2369
2370void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002371 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002372}
2373
2374void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002375 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002376}
2377
2378void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002379 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2380}
2381
Vladimir Markoca6fff82017-10-03 14:49:14 +01002382static void CreateOneBitLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_high) {
2383 LocationSummary* locations =
2384 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc5d47542016-01-27 17:00:35 -08002385 locations->SetInAt(0, Location::Any());
2386 locations->SetOut(Location::RequiresRegister());
2387 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2388 : Location::RequiresRegister()); // any will do
2389}
2390
2391static void GenOneBit(X86_64Assembler* assembler,
2392 CodeGeneratorX86_64* codegen,
2393 HInvoke* invoke,
2394 bool is_high, bool is_long) {
2395 LocationSummary* locations = invoke->GetLocations();
2396 Location src = locations->InAt(0);
2397 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2398
2399 if (invoke->InputAt(0)->IsConstant()) {
2400 // Evaluate this at compile time.
2401 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2402 if (value == 0) {
2403 __ xorl(out, out); // Clears upper bits too.
2404 return;
2405 }
2406 // Nonzero value.
2407 if (is_high) {
2408 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2409 : 31 - CLZ(static_cast<uint32_t>(value));
2410 } else {
2411 value = is_long ? CTZ(static_cast<uint64_t>(value))
2412 : CTZ(static_cast<uint32_t>(value));
2413 }
2414 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002415 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002416 } else {
2417 codegen->Load32BitValue(out, 1 << value);
2418 }
2419 return;
2420 }
2421
2422 // Handle the non-constant cases.
2423 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2424 if (is_high) {
2425 // Use architectural support: basically 1 << bsr.
2426 if (src.IsRegister()) {
2427 if (is_long) {
2428 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2429 } else {
2430 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2431 }
2432 } else if (is_long) {
2433 DCHECK(src.IsDoubleStackSlot());
2434 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2435 } else {
2436 DCHECK(src.IsStackSlot());
2437 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2438 }
2439 // BSR sets ZF if the input was zero.
2440 NearLabel is_zero, done;
2441 __ j(kEqual, &is_zero);
2442 __ movl(out, Immediate(1)); // Clears upper bits too.
2443 if (is_long) {
2444 __ shlq(out, tmp);
2445 } else {
2446 __ shll(out, tmp);
2447 }
2448 __ jmp(&done);
2449 __ Bind(&is_zero);
2450 __ xorl(out, out); // Clears upper bits too.
2451 __ Bind(&done);
2452 } else {
2453 // Copy input into temporary.
2454 if (src.IsRegister()) {
2455 if (is_long) {
2456 __ movq(tmp, src.AsRegister<CpuRegister>());
2457 } else {
2458 __ movl(tmp, src.AsRegister<CpuRegister>());
2459 }
2460 } else if (is_long) {
2461 DCHECK(src.IsDoubleStackSlot());
2462 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2463 } else {
2464 DCHECK(src.IsStackSlot());
2465 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2466 }
2467 // Do the bit twiddling: basically tmp & -tmp;
2468 if (is_long) {
2469 __ movq(out, tmp);
2470 __ negq(tmp);
2471 __ andq(out, tmp);
2472 } else {
2473 __ movl(out, tmp);
2474 __ negl(tmp);
2475 __ andl(out, tmp);
2476 }
2477 }
2478}
2479
2480void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002481 CreateOneBitLocations(allocator_, invoke, /* is_high */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002482}
2483
2484void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2485 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2486}
2487
2488void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002489 CreateOneBitLocations(allocator_, invoke, /* is_high */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002490}
2491
2492void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2493 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2494}
2495
2496void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002497 CreateOneBitLocations(allocator_, invoke, /* is_high */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002498}
2499
2500void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2501 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2502}
2503
2504void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002505 CreateOneBitLocations(allocator_, invoke, /* is_high */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002506}
2507
2508void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2509 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002510}
2511
Vladimir Markoca6fff82017-10-03 14:49:14 +01002512static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2513 LocationSummary* locations =
2514 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002515 locations->SetInAt(0, Location::Any());
2516 locations->SetOut(Location::RequiresRegister());
2517}
2518
Aart Bikc5d47542016-01-27 17:00:35 -08002519static void GenLeadingZeros(X86_64Assembler* assembler,
2520 CodeGeneratorX86_64* codegen,
2521 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002522 LocationSummary* locations = invoke->GetLocations();
2523 Location src = locations->InAt(0);
2524 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2525
2526 int zero_value_result = is_long ? 64 : 32;
2527 if (invoke->InputAt(0)->IsConstant()) {
2528 // Evaluate this at compile time.
2529 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2530 if (value == 0) {
2531 value = zero_value_result;
2532 } else {
2533 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2534 }
Aart Bikc5d47542016-01-27 17:00:35 -08002535 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002536 return;
2537 }
2538
2539 // Handle the non-constant cases.
2540 if (src.IsRegister()) {
2541 if (is_long) {
2542 __ bsrq(out, src.AsRegister<CpuRegister>());
2543 } else {
2544 __ bsrl(out, src.AsRegister<CpuRegister>());
2545 }
2546 } else if (is_long) {
2547 DCHECK(src.IsDoubleStackSlot());
2548 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2549 } else {
2550 DCHECK(src.IsStackSlot());
2551 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2552 }
2553
2554 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002555 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002556 __ j(kEqual, &is_zero);
2557
2558 // Correct the result from BSR to get the CLZ result.
2559 __ xorl(out, Immediate(zero_value_result - 1));
2560 __ jmp(&done);
2561
2562 // Fix the zero case with the expected result.
2563 __ Bind(&is_zero);
2564 __ movl(out, Immediate(zero_value_result));
2565
2566 __ Bind(&done);
2567}
2568
2569void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002570 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002571}
2572
2573void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002574 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002575}
2576
2577void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002578 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002579}
2580
2581void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002582 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002583}
2584
Vladimir Markoca6fff82017-10-03 14:49:14 +01002585static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2586 LocationSummary* locations =
2587 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002588 locations->SetInAt(0, Location::Any());
2589 locations->SetOut(Location::RequiresRegister());
2590}
2591
Aart Bikc5d47542016-01-27 17:00:35 -08002592static void GenTrailingZeros(X86_64Assembler* assembler,
2593 CodeGeneratorX86_64* codegen,
2594 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002595 LocationSummary* locations = invoke->GetLocations();
2596 Location src = locations->InAt(0);
2597 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2598
2599 int zero_value_result = is_long ? 64 : 32;
2600 if (invoke->InputAt(0)->IsConstant()) {
2601 // Evaluate this at compile time.
2602 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2603 if (value == 0) {
2604 value = zero_value_result;
2605 } else {
2606 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2607 }
Aart Bikc5d47542016-01-27 17:00:35 -08002608 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002609 return;
2610 }
2611
2612 // Handle the non-constant cases.
2613 if (src.IsRegister()) {
2614 if (is_long) {
2615 __ bsfq(out, src.AsRegister<CpuRegister>());
2616 } else {
2617 __ bsfl(out, src.AsRegister<CpuRegister>());
2618 }
2619 } else if (is_long) {
2620 DCHECK(src.IsDoubleStackSlot());
2621 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2622 } else {
2623 DCHECK(src.IsStackSlot());
2624 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2625 }
2626
2627 // BSF sets ZF if the input was zero, and the output is undefined.
2628 NearLabel done;
2629 __ j(kNotEqual, &done);
2630
2631 // Fix the zero case with the expected result.
2632 __ movl(out, Immediate(zero_value_result));
2633
2634 __ Bind(&done);
2635}
2636
2637void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002638 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002639}
2640
2641void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002642 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002643}
2644
2645void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002646 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002647}
2648
2649void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002650 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2651}
2652
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002653void IntrinsicLocationsBuilderX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2654 InvokeRuntimeCallingConvention calling_convention;
2655 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2656 invoke,
2657 codegen_,
2658 Location::RegisterLocation(RAX),
2659 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2660}
2661
2662void IntrinsicCodeGeneratorX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2663 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2664 LocationSummary* locations = invoke->GetLocations();
2665 X86_64Assembler* assembler = GetAssembler();
2666
2667 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2668 InvokeRuntimeCallingConvention calling_convention;
2669 if (invoke->InputAt(0)->IsConstant()) {
2670 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2671 if (value >= info.low && value <= info.high) {
2672 // Just embed the j.l.Integer in the code.
2673 ScopedObjectAccess soa(Thread::Current());
2674 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2675 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2676 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
Colin Cross0bd97172017-03-15 16:33:27 -07002677 __ movl(out, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002678 } else {
2679 // Allocate and initialize a new j.l.Integer.
2680 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2681 // JIT object table.
Colin Cross0bd97172017-03-15 16:33:27 -07002682 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002683 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07002684 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002685 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2686 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2687 __ movl(Address(out, info.value_offset), Immediate(value));
2688 }
2689 } else {
2690 CpuRegister in = locations->InAt(0).AsRegister<CpuRegister>();
2691 // Check bounds of our cache.
2692 __ leal(out, Address(in, -info.low));
2693 __ cmpl(out, Immediate(info.high - info.low + 1));
2694 NearLabel allocate, done;
2695 __ j(kAboveEqual, &allocate);
2696 // If the value is within the bounds, load the j.l.Integer directly from the array.
2697 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2698 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
Colin Cross0bd97172017-03-15 16:33:27 -07002699 if (data_offset + address <= std::numeric_limits<int32_t>::max()) {
2700 __ movl(out, Address(out, TIMES_4, data_offset + address));
2701 } else {
2702 CpuRegister temp = CpuRegister(calling_convention.GetRegisterAt(0));
2703 __ movl(temp, Immediate(static_cast<int32_t>(data_offset + address)));
2704 __ movl(out, Address(temp, out, TIMES_4, 0));
2705 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002706 __ MaybeUnpoisonHeapReference(out);
2707 __ jmp(&done);
2708 __ Bind(&allocate);
2709 // Otherwise allocate and initialize a new j.l.Integer.
Colin Cross0bd97172017-03-15 16:33:27 -07002710 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002711 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07002712 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002713 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2714 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2715 __ movl(Address(out, info.value_offset), in);
2716 __ Bind(&done);
2717 }
2718}
2719
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002720void IntrinsicLocationsBuilderX86_64::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002721 LocationSummary* locations =
2722 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002723 locations->SetOut(Location::RequiresRegister());
2724}
2725
2726void IntrinsicCodeGeneratorX86_64::VisitThreadInterrupted(HInvoke* invoke) {
2727 X86_64Assembler* assembler = GetAssembler();
2728 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
2729 Address address = Address::Absolute
2730 (Thread::InterruptedOffset<kX86_64PointerSize>().Int32Value(), /* no_rip */ true);
2731 NearLabel done;
2732 __ gs()->movl(out, address);
2733 __ testl(out, out);
2734 __ j(kEqual, &done);
2735 __ gs()->movl(address, Immediate(0));
2736 codegen_->MemoryFence();
2737 __ Bind(&done);
2738}
2739
Hans Boehmc7b28de2018-03-09 17:05:28 -08002740void IntrinsicLocationsBuilderX86_64::VisitReachabilityFence(HInvoke* invoke) {
2741 LocationSummary* locations =
2742 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
2743 locations->SetInAt(0, Location::Any());
2744}
2745
2746void IntrinsicCodeGeneratorX86_64::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
2747
Vladimir Marko4ee8e292017-06-02 15:39:30 +00002748UNIMPLEMENTED_INTRINSIC(X86_64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002749UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2750UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002751
Aart Bikff7d89c2016-11-07 08:49:28 -08002752UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOf);
2753UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002754UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferAppend);
2755UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferLength);
2756UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferToString);
2757UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppend);
2758UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderLength);
2759UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002760
Aart Bik0e54c012016-03-04 12:08:31 -08002761// 1.8.
2762UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2763UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2764UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2765UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2766UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002767
Aart Bik2f9fcc92016-03-01 15:16:54 -08002768UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002769
2770#undef __
2771
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002772} // namespace x86_64
2773} // namespace art