blob: 0f6b00653d1de16136960130ddf91379720739cc [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
Vladimir Marko0a516052019-10-14 13:00:44 +000039namespace art {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080040
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010083 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0b671c02016-08-19 12:02:34 +010084 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100121 const char* GetDescription() const override { return "ReadBarrierSystemArrayCopySlowPathX86_64"; }
Roland Levillain0b671c02016-08-19 12:02:34 +0100122
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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800165 MoveFPToInt(invoke->GetLocations(), /* is64bit= */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800166}
167void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800168 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) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800179 MoveFPToInt(invoke->GetLocations(), /* is64bit= */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800180}
181void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -0800182 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);
Andreas Gampe3db70682018-12-26 15:12:03 -0800433 __ cvtsi2sd(t2, out, /* is64bit= */ true);
Aart Bik349f3882016-08-02 15:40:56 -0700434 __ 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)
Andreas Gampe3db70682018-12-26 15:12:03 -0800438 __ 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(
Andreas Gampe3db70682018-12-26 15:12:03 -0800982 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(
Andreas Gampe3db70682018-12-26 15:12:03 -0800990 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(
Andreas Gampe3db70682018-12-26 15:12:03 -08001014 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(
Andreas Gampe3db70682018-12-26 15:12:03 -08001037 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(
Andreas Gampe3db70682018-12-26 15:12:03 -08001061 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(
Andreas Gampe3db70682018-12-26 15:12:03 -08001089 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(
Andreas Gampe3db70682018-12-26 15:12:03 -08001092 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.
Roland Levillain14e5a292018-06-28 12:00:56 +01001146 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001147 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.
Andreas Gampe3db70682018-12-26 15:12:03 -08001201 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 Markoca6fff82017-10-03 14:49:14 +01001233 LocationSummary* locations =
1234 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakif8cfb202015-08-13 17:54:54 -07001235 locations->SetInAt(0, Location::RequiresRegister());
1236 locations->SetInAt(1, Location::RequiresRegister());
1237
1238 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1239 locations->AddTemp(Location::RegisterLocation(RCX));
1240 locations->AddTemp(Location::RegisterLocation(RDI));
1241
1242 // Set output, RSI needed for repe_cmpsq instruction anyways.
1243 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1244}
1245
1246void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1247 X86_64Assembler* assembler = GetAssembler();
1248 LocationSummary* locations = invoke->GetLocations();
1249
1250 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1251 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1252 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1253 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1254 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1255
Mark Mendell0c9497d2015-08-21 09:30:05 -04001256 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001257
1258 // Get offsets of count, value, and class fields within a string object.
1259 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1260 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1261 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1262
1263 // Note that the null check must have been done earlier.
1264 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1265
Vladimir Marko53b52002016-05-24 19:30:45 +01001266 StringEqualsOptimizations optimizations(invoke);
1267 if (!optimizations.GetArgumentNotNull()) {
1268 // Check if input is null, return false if it is.
1269 __ testl(arg, arg);
1270 __ j(kEqual, &return_false);
1271 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001272
Vladimir Marko53b52002016-05-24 19:30:45 +01001273 if (!optimizations.GetArgumentIsString()) {
1274 // Instanceof check for the argument by comparing class fields.
1275 // All string objects must have the same type since String cannot be subclassed.
1276 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1277 // If the argument is a string object, its class field must be equal to receiver's class field.
Roland Levillain1d775d22018-09-07 13:56:57 +01001278 //
1279 // As the String class is expected to be non-movable, we can read the class
1280 // field from String.equals' arguments without read barriers.
1281 AssertNonMovableStringClass();
1282 // Also, because we use the loaded class references only to compare them, we
1283 // don't need to unpoison them.
1284 // /* HeapReference<Class> */ rcx = str->klass_
Vladimir Marko53b52002016-05-24 19:30:45 +01001285 __ movl(rcx, Address(str, class_offset));
Roland Levillain1d775d22018-09-07 13:56:57 +01001286 // if (rcx != /* HeapReference<Class> */ arg->klass_) return false
Vladimir Marko53b52002016-05-24 19:30:45 +01001287 __ cmpl(rcx, Address(arg, class_offset));
1288 __ j(kNotEqual, &return_false);
1289 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001290
1291 // Reference equality check, return true if same reference.
1292 __ cmpl(str, arg);
1293 __ j(kEqual, &return_true);
1294
jessicahandojo4877b792016-09-08 19:49:13 -07001295 // Load length and compression flag of receiver string.
Agi Csakif8cfb202015-08-13 17:54:54 -07001296 __ movl(rcx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001297 // Check if lengths and compressiond flags are equal, return false if they're not.
1298 // Two identical strings will always have same compression style since
1299 // compression style is decided on alloc.
Agi Csakif8cfb202015-08-13 17:54:54 -07001300 __ cmpl(rcx, Address(arg, count_offset));
1301 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001302 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1303 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1304 "Expecting 0=compressed, 1=uncompressed");
1305 __ jrcxz(&return_true);
jessicahandojo4877b792016-09-08 19:49:13 -07001306
1307 if (mirror::kUseStringCompression) {
1308 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001309 // Extract length and differentiate between both compressed or both uncompressed.
1310 // Different compression style is cut above.
1311 __ shrl(rcx, Immediate(1));
1312 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001313 // Divide string length by 2, rounding up, and continue as if uncompressed.
1314 // Merge clearing the compression flag with +1 for rounding.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001315 __ addl(rcx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001316 __ shrl(rcx, Immediate(1));
1317 __ Bind(&string_uncompressed);
1318 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001319 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1320 __ leal(rsi, Address(str, value_offset));
1321 __ leal(rdi, Address(arg, value_offset));
1322
1323 // Divide string length by 4 and adjust for lengths not divisible by 4.
1324 __ addl(rcx, Immediate(3));
1325 __ shrl(rcx, Immediate(2));
1326
jessicahandojo4877b792016-09-08 19:49:13 -07001327 // Assertions that must hold in order to compare strings 4 characters (uncompressed)
1328 // or 8 characters (compressed) at a time.
Agi Csakif8cfb202015-08-13 17:54:54 -07001329 DCHECK_ALIGNED(value_offset, 8);
1330 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1331
1332 // Loop to compare strings four characters at a time starting at the beginning of the string.
1333 __ repe_cmpsq();
1334 // If strings are not equal, zero flag will be cleared.
1335 __ j(kNotEqual, &return_false);
1336
1337 // Return true and exit the function.
1338 // If loop does not result in returning false, we return true.
1339 __ Bind(&return_true);
1340 __ movl(rsi, Immediate(1));
1341 __ jmp(&end);
1342
1343 // Return false and exit the function.
1344 __ Bind(&return_false);
1345 __ xorl(rsi, rsi);
1346 __ Bind(&end);
1347}
1348
Andreas Gampe21030dd2015-05-07 14:46:15 -07001349static void CreateStringIndexOfLocations(HInvoke* invoke,
1350 ArenaAllocator* allocator,
1351 bool start_at_zero) {
1352 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1353 LocationSummary::kCallOnSlowPath,
1354 kIntrinsified);
1355 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1356 locations->SetInAt(0, Location::RegisterLocation(RDI));
1357 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1358 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1359 // of the instruction explicitly.
1360 // Note: This works as we don't clobber RAX anywhere.
1361 locations->SetInAt(1, Location::RegisterLocation(RAX));
1362 if (!start_at_zero) {
1363 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1364 }
1365 // As we clobber RDI during execution anyways, also use it as the output.
1366 locations->SetOut(Location::SameAsFirstInput());
1367
1368 // repne scasw uses RCX as the counter.
1369 locations->AddTemp(Location::RegisterLocation(RCX));
1370 // Need another temporary to be able to compute the result.
1371 locations->AddTemp(Location::RequiresRegister());
1372}
1373
1374static void GenerateStringIndexOf(HInvoke* invoke,
1375 X86_64Assembler* assembler,
1376 CodeGeneratorX86_64* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001377 bool start_at_zero) {
1378 LocationSummary* locations = invoke->GetLocations();
1379
1380 // Note that the null check must have been done earlier.
1381 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1382
1383 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1384 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1385 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1386 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1387 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1388
1389 // Check our assumptions for registers.
1390 DCHECK_EQ(string_obj.AsRegister(), RDI);
1391 DCHECK_EQ(search_value.AsRegister(), RAX);
1392 DCHECK_EQ(counter.AsRegister(), RCX);
1393 DCHECK_EQ(out.AsRegister(), RDI);
1394
1395 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001396 // 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 -07001397 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001398 HInstruction* code_point = invoke->InputAt(1);
1399 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001400 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001401 std::numeric_limits<uint16_t>::max()) {
1402 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1403 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001404 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001405 codegen->AddSlowPath(slow_path);
1406 __ jmp(slow_path->GetEntryLabel());
1407 __ Bind(slow_path->GetExitLabel());
1408 return;
1409 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001411 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001412 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001413 codegen->AddSlowPath(slow_path);
1414 __ j(kAbove, slow_path->GetEntryLabel());
1415 }
1416
jessicahandojo4877b792016-09-08 19:49:13 -07001417 // From here down, we know that we are looking for a char that fits in
1418 // 16 bits (uncompressed) or 8 bits (compressed).
Andreas Gampe21030dd2015-05-07 14:46:15 -07001419 // Location of reference to data array within the String object.
1420 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1421 // Location of count within the String object.
1422 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1423
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001424 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001425 __ movl(string_length, Address(string_obj, count_offset));
1426
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001427 // Do a zero-length check. Even with string compression `count == 0` means empty.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001428 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001429 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001430 __ testl(string_length, string_length);
1431 __ j(kEqual, &not_found_label);
1432
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001433 if (mirror::kUseStringCompression) {
1434 // Use TMP to keep string_length_flagged.
1435 __ movl(CpuRegister(TMP), string_length);
1436 // Mask out first bit used as compression flag.
1437 __ shrl(string_length, Immediate(1));
1438 }
1439
Andreas Gampe21030dd2015-05-07 14:46:15 -07001440 if (start_at_zero) {
1441 // Number of chars to scan is the same as the string length.
1442 __ movl(counter, string_length);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001443 // Move to the start of the string.
1444 __ addq(string_obj, Immediate(value_offset));
1445 } else {
1446 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1447
1448 // Do a start_index check.
1449 __ cmpl(start_index, string_length);
1450 __ j(kGreaterEqual, &not_found_label);
1451
1452 // Ensure we have a start index >= 0;
1453 __ xorl(counter, counter);
1454 __ cmpl(start_index, Immediate(0));
Andreas Gampe3db70682018-12-26 15:12:03 -08001455 __ cmov(kGreater, counter, start_index, /* is64bit= */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001456
jessicahandojo4877b792016-09-08 19:49:13 -07001457 if (mirror::kUseStringCompression) {
1458 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001459 __ testl(CpuRegister(TMP), Immediate(1));
1460 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001461 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1462 __ jmp(&modify_counter);
1463 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1464 __ Bind(&offset_uncompressed_label);
1465 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1466 __ Bind(&modify_counter);
1467 } else {
1468 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1469 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001470 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1471 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1472 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1473 }
1474
jessicahandojo4877b792016-09-08 19:49:13 -07001475 if (mirror::kUseStringCompression) {
1476 NearLabel uncompressed_string_comparison;
1477 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001478 __ testl(CpuRegister(TMP), Immediate(1));
1479 __ j(kNotZero, &uncompressed_string_comparison);
jessicahandojo4877b792016-09-08 19:49:13 -07001480 // Check if RAX (search_value) is ASCII.
1481 __ cmpl(search_value, Immediate(127));
1482 __ j(kGreater, &not_found_label);
1483 // Comparing byte-per-byte.
1484 __ repne_scasb();
1485 __ jmp(&comparison_done);
1486 // Everything is set up for repne scasw:
1487 // * Comparison address in RDI.
1488 // * Counter in ECX.
1489 __ Bind(&uncompressed_string_comparison);
1490 __ repne_scasw();
1491 __ Bind(&comparison_done);
1492 } else {
1493 __ repne_scasw();
1494 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001495 // Did we find a match?
1496 __ j(kNotEqual, &not_found_label);
1497
1498 // Yes, we matched. Compute the index of the result.
1499 __ subl(string_length, counter);
1500 __ leal(out, Address(string_length, -1));
1501
Mark Mendell0c9497d2015-08-21 09:30:05 -04001502 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001503 __ jmp(&done);
1504
1505 // Failed to match; return -1.
1506 __ Bind(&not_found_label);
1507 __ movl(out, Immediate(-1));
1508
1509 // And join up at the end.
1510 __ Bind(&done);
1511 if (slow_path != nullptr) {
1512 __ Bind(slow_path->GetExitLabel());
1513 }
1514}
1515
1516void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001517 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero= */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001518}
1519
1520void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001521 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero= */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001522}
1523
1524void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001525 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero= */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001526}
1527
1528void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001529 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero= */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001530}
1531
Jeff Hao848f70a2014-01-15 13:49:50 -08001532void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001533 LocationSummary* locations = new (allocator_) LocationSummary(
1534 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001535 InvokeRuntimeCallingConvention calling_convention;
1536 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1537 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1538 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1539 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1540 locations->SetOut(Location::RegisterLocation(RAX));
1541}
1542
1543void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1544 X86_64Assembler* assembler = GetAssembler();
1545 LocationSummary* locations = invoke->GetLocations();
1546
1547 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1548 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001549 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001550 codegen_->AddSlowPath(slow_path);
1551 __ j(kEqual, slow_path->GetEntryLabel());
1552
Serban Constantinescuba45db02016-07-12 22:53:02 +01001553 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001554 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001555 __ Bind(slow_path->GetExitLabel());
1556}
1557
1558void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001559 LocationSummary* locations =
1560 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001561 InvokeRuntimeCallingConvention calling_convention;
1562 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1563 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1564 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1565 locations->SetOut(Location::RegisterLocation(RAX));
1566}
1567
1568void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001569 // No need to emit code checking whether `locations->InAt(2)` is a null
1570 // pointer, as callers of the native method
1571 //
1572 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1573 //
1574 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001575 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001576 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001577}
1578
1579void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001580 LocationSummary* locations = new (allocator_) LocationSummary(
1581 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001582 InvokeRuntimeCallingConvention calling_convention;
1583 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1584 locations->SetOut(Location::RegisterLocation(RAX));
1585}
1586
1587void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1588 X86_64Assembler* assembler = GetAssembler();
1589 LocationSummary* locations = invoke->GetLocations();
1590
1591 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1592 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001593 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001594 codegen_->AddSlowPath(slow_path);
1595 __ j(kEqual, slow_path->GetEntryLabel());
1596
Serban Constantinescuba45db02016-07-12 22:53:02 +01001597 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001598 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001599 __ Bind(slow_path->GetExitLabel());
1600}
1601
Mark Mendell8f8926a2015-08-17 11:39:06 -04001602void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1603 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001604 LocationSummary* locations =
1605 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001606 locations->SetInAt(0, Location::RequiresRegister());
1607 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1608 locations->SetInAt(2, Location::RequiresRegister());
1609 locations->SetInAt(3, Location::RequiresRegister());
1610 locations->SetInAt(4, Location::RequiresRegister());
1611
1612 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1613 locations->AddTemp(Location::RegisterLocation(RSI));
1614 locations->AddTemp(Location::RegisterLocation(RDI));
1615 locations->AddTemp(Location::RegisterLocation(RCX));
1616}
1617
1618void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1619 X86_64Assembler* assembler = GetAssembler();
1620 LocationSummary* locations = invoke->GetLocations();
1621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001622 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001623 // Location of data in char array buffer.
1624 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1625 // Location of char array data in string.
1626 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1627
1628 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1629 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1630 Location srcBegin = locations->InAt(1);
1631 int srcBegin_value =
1632 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1633 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1634 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1635 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1636
1637 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001638 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001639 DCHECK_EQ(char_size, 2u);
1640
jessicahandojo4877b792016-09-08 19:49:13 -07001641 NearLabel done;
Mark Mendell8f8926a2015-08-17 11:39:06 -04001642 // Compute the number of chars (words) to move.
1643 __ movl(CpuRegister(RCX), srcEnd);
1644 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001645 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001646 } else {
1647 DCHECK(srcBegin.IsRegister());
1648 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1649 }
jessicahandojo4877b792016-09-08 19:49:13 -07001650 if (mirror::kUseStringCompression) {
1651 NearLabel copy_uncompressed, copy_loop;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001652 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001653 DCHECK_EQ(c_char_size, 1u);
1654 // Location of count in string.
1655 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Mark Mendell8f8926a2015-08-17 11:39:06 -04001656
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001657 __ testl(Address(obj, count_offset), Immediate(1));
1658 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1659 "Expecting 0=compressed, 1=uncompressed");
1660 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001661 // Compute the address of the source string by adding the number of chars from
1662 // the source beginning to the value offset of a string.
1663 __ leaq(CpuRegister(RSI),
1664 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1665 // Start the loop to copy String's value to Array of Char.
1666 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1667
1668 __ Bind(&copy_loop);
1669 __ jrcxz(&done);
1670 // Use TMP as temporary (convert byte from RSI to word).
1671 // TODO: Selecting RAX as the temporary and using LODSB/STOSW.
1672 __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
1673 __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
1674 __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
1675 __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
1676 // TODO: Add support for LOOP to X86_64Assembler.
1677 __ subl(CpuRegister(RCX), Immediate(1));
1678 __ jmp(&copy_loop);
1679
1680 __ Bind(&copy_uncompressed);
1681 }
1682
1683 __ leaq(CpuRegister(RSI),
1684 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
1685 // Compute the address of the destination buffer.
1686 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001687 // Do the move.
1688 __ rep_movsw();
jessicahandojo4877b792016-09-08 19:49:13 -07001689
1690 __ Bind(&done);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001691}
1692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693static void GenPeek(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001694 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1695 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1696 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1697 // to avoid a SIGBUS.
1698 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699 case DataType::Type::kInt8:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001700 __ movsxb(out, Address(address, 0));
1701 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001703 __ movsxw(out, Address(address, 0));
1704 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001705 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001706 __ movl(out, Address(address, 0));
1707 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001709 __ movq(out, Address(address, 0));
1710 break;
1711 default:
1712 LOG(FATAL) << "Type not recognized for peek: " << size;
1713 UNREACHABLE();
1714 }
1715}
1716
1717void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001718 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001719}
1720
1721void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001722 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001723}
1724
1725void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001726 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001727}
1728
1729void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001730 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001731}
1732
1733void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001734 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001735}
1736
1737void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001738 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001739}
1740
1741void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001742 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001743}
1744
1745void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001746 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001747}
1748
Vladimir Markoca6fff82017-10-03 14:49:14 +01001749static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
1750 LocationSummary* locations =
1751 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001752 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001753 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001754}
1755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001756static void GenPoke(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001757 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001758 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001759 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1760 // to avoid a SIGBUS.
1761 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001762 case DataType::Type::kInt8:
Mark Mendell40741f32015-04-20 22:10:34 -04001763 if (value.IsConstant()) {
1764 __ movb(Address(address, 0),
1765 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1766 } else {
1767 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1768 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001769 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001770 case DataType::Type::kInt16:
Mark Mendell40741f32015-04-20 22:10:34 -04001771 if (value.IsConstant()) {
1772 __ movw(Address(address, 0),
1773 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1774 } else {
1775 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1776 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001777 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04001779 if (value.IsConstant()) {
1780 __ movl(Address(address, 0),
1781 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1782 } else {
1783 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1784 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001785 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001786 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04001787 if (value.IsConstant()) {
1788 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1789 DCHECK(IsInt<32>(v));
1790 int32_t v_32 = v;
1791 __ movq(Address(address, 0), Immediate(v_32));
1792 } else {
1793 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1794 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001795 break;
1796 default:
1797 LOG(FATAL) << "Type not recognized for poke: " << size;
1798 UNREACHABLE();
1799 }
1800}
1801
1802void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001803 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001804}
1805
1806void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001807 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001808}
1809
1810void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001811 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001812}
1813
1814void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001816}
1817
1818void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001819 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001820}
1821
1822void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001824}
1825
1826void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001827 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001828}
1829
1830void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001832}
1833
1834void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001835 LocationSummary* locations =
1836 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001837 locations->SetOut(Location::RequiresRegister());
1838}
1839
1840void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1841 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001842 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001843 /* no_rip= */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001844}
1845
Roland Levillain0d5a2812015-11-13 10:07:31 +00001846static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001847 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001848 bool is_volatile ATTRIBUTE_UNUSED,
1849 CodeGeneratorX86_64* codegen) {
1850 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
1851 LocationSummary* locations = invoke->GetLocations();
1852 Location base_loc = locations->InAt(1);
1853 CpuRegister base = base_loc.AsRegister<CpuRegister>();
1854 Location offset_loc = locations->InAt(2);
1855 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
1856 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001857 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001858
Andreas Gampe878d58c2015-01-15 23:24:00 -08001859 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 case DataType::Type::kInt32:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001861 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001862 break;
1863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001864 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001865 if (kEmitCompilerReadBarrier) {
1866 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001867 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1868 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08001869 invoke, output_loc, base, src, /* needs_null_check= */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001870 } else {
1871 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1872 codegen->GenerateReadBarrierSlow(
1873 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1874 }
1875 } else {
1876 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1877 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001878 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001879 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001880 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001882 case DataType::Type::kInt64:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001883 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08001884 break;
1885
1886 default:
1887 LOG(FATAL) << "Unsupported op size " << type;
1888 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001889 }
1890}
1891
Vladimir Markoca6fff82017-10-03 14:49:14 +01001892static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001893 bool can_call = kEmitCompilerReadBarrier &&
1894 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1895 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001896 LocationSummary* locations =
1897 new (allocator) LocationSummary(invoke,
1898 can_call
1899 ? LocationSummary::kCallOnSlowPath
1900 : LocationSummary::kNoCall,
1901 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01001902 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001903 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001904 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001905 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001906 locations->SetInAt(1, Location::RequiresRegister());
1907 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001908 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001909 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001910}
1911
1912void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001913 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001914}
1915void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001916 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001917}
1918void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001919 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001920}
1921void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001922 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001923}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001924void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001925 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001926}
1927void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001928 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001929}
1930
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001931
1932void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001933 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001934}
1935void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001936 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile= */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001937}
1938void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001939 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001940}
1941void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001942 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile= */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001943}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001944void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001945 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile= */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001946}
1947void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001948 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile= */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001949}
1950
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001951
Vladimir Markoca6fff82017-10-03 14:49:14 +01001952static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001953 DataType::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001954 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001955 LocationSummary* locations =
1956 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001957 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001958 locations->SetInAt(1, Location::RequiresRegister());
1959 locations->SetInAt(2, Location::RequiresRegister());
1960 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001961 if (type == DataType::Type::kReference) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001962 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001963 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001964 locations->AddTemp(Location::RequiresRegister());
1965 }
1966}
1967
1968void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001969 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001970}
1971void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001972 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001973}
1974void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001975 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001976}
1977void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001978 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001979}
1980void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001981 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001982}
1983void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001984 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001985}
1986void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001987 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001988}
1989void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001990 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001991}
1992void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001993 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001994}
1995
1996// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1997// memory model.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001998static void GenUnsafePut(LocationSummary* locations, DataType::Type type, bool is_volatile,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001999 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002000 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002001 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2002 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2003 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2004
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 if (type == DataType::Type::kInt64) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002006 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002008 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2009 __ movl(temp, value);
2010 __ PoisonHeapReference(temp);
2011 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002012 } else {
2013 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2014 }
2015
2016 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002017 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002018 }
2019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002021 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002022 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2023 locations->GetTemp(1).AsRegister<CpuRegister>(),
2024 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002025 value,
2026 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002027 }
2028}
2029
2030void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002031 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002032}
2033void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002034 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002035}
2036void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002037 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile= */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002038}
2039void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 GenUnsafePut(
Andreas Gampe3db70682018-12-26 15:12:03 -08002041 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002042}
2043void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 GenUnsafePut(
Andreas Gampe3db70682018-12-26 15:12:03 -08002045 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002046}
2047void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 GenUnsafePut(
Andreas Gampe3db70682018-12-26 15:12:03 -08002049 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile= */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002050}
2051void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002052 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002053}
2054void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002055 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile= */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002056}
2057void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002058 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile= */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002059}
2060
Vladimir Markoca6fff82017-10-03 14:49:14 +01002061static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002063 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002064 bool can_call = kEmitCompilerReadBarrier &&
2065 kUseBakerReadBarrier &&
2066 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002067 LocationSummary* locations =
2068 new (allocator) LocationSummary(invoke,
2069 can_call
2070 ? LocationSummary::kCallOnSlowPath
2071 : LocationSummary::kNoCall,
2072 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002073 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2074 locations->SetInAt(1, Location::RequiresRegister());
2075 locations->SetInAt(2, Location::RequiresRegister());
2076 // expected value must be in EAX/RAX.
2077 locations->SetInAt(3, Location::RegisterLocation(RAX));
2078 locations->SetInAt(4, Location::RequiresRegister());
2079
2080 locations->SetOut(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002081 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002082 // Need temporary registers for card-marking, and possibly for
2083 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002084 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002085 locations->AddTemp(Location::RequiresRegister());
2086 }
2087}
2088
2089void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002090 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002091}
2092
2093void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002094 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002095}
2096
2097void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002098 // The only read barrier implementation supporting the
2099 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2100 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002101 return;
2102 }
2103
Vladimir Markoca6fff82017-10-03 14:49:14 +01002104 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002105}
2106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002108 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002109 LocationSummary* locations = invoke->GetLocations();
2110
2111 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2112 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2113 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002114 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002115 DCHECK_EQ(expected.AsRegister(), RAX);
2116 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002117 Location out_loc = locations->Out();
2118 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002121 // The only read barrier implementation supporting the
2122 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2123 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2124
2125 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2126 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2127
Roland Levillainb488b782015-10-22 11:38:49 +01002128 // Mark card for object assuming new value is stored.
2129 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002130 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2131
2132 // The address of the field within the holding object.
2133 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2134
2135 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2136 // Need to make sure the reference stored in the field is a to-space
2137 // one before attempting the CAS or the CAS could fail incorrectly.
2138 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2139 invoke,
2140 out_loc, // Unused, used only as a "temporary" within the read barrier.
2141 base,
2142 field_addr,
Andreas Gampe3db70682018-12-26 15:12:03 -08002143 /* needs_null_check= */ false,
2144 /* always_update_field= */ true,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002145 &temp1,
2146 &temp2);
2147 }
Roland Levillain4d027112015-07-01 15:41:14 +01002148
Roland Levillainb488b782015-10-22 11:38:49 +01002149 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2150 Register value_reg = value.AsRegister();
2151 if (kPoisonHeapReferences) {
2152 if (base_equals_value) {
2153 // If `base` and `value` are the same register location, move
2154 // `value_reg` to a temporary register. This way, poisoning
2155 // `value_reg` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002156 value_reg = temp1.AsRegister();
Roland Levillainb488b782015-10-22 11:38:49 +01002157 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002158 }
Roland Levillainb488b782015-10-22 11:38:49 +01002159
2160 // Check that the register allocator did not assign the location
2161 // of `expected` (RAX) to `value` nor to `base`, so that heap
2162 // poisoning (when enabled) works as intended below.
2163 // - If `value` were equal to `expected`, both references would
2164 // be poisoned twice, meaning they would not be poisoned at
2165 // all, as heap poisoning uses address negation.
2166 // - If `base` were equal to `expected`, poisoning `expected`
2167 // would invalidate `base`.
2168 DCHECK_NE(value_reg, expected.AsRegister());
2169 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2170
2171 __ PoisonHeapReference(expected);
2172 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002173 }
2174
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002175 __ LockCmpxchgl(field_addr, CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002176
Roland Levillain0d5a2812015-11-13 10:07:31 +00002177 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002178 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002179
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002180 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002181 __ setcc(kZero, out);
2182 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002183
Roland Levillain391b8662015-12-18 11:43:38 +00002184 // If heap poisoning is enabled, we need to unpoison the values
2185 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002186 if (kPoisonHeapReferences) {
2187 if (base_equals_value) {
2188 // `value_reg` has been moved to a temporary register, no need
2189 // to unpoison it.
2190 } else {
2191 // Ensure `value` is different from `out`, so that unpoisoning
2192 // the former does not invalidate the latter.
2193 DCHECK_NE(value_reg, out.AsRegister());
2194 __ UnpoisonHeapReference(CpuRegister(value_reg));
2195 }
2196 // Ensure `expected` is different from `out`, so that unpoisoning
2197 // the former does not invalidate the latter.
2198 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2199 __ UnpoisonHeapReference(expected);
2200 }
2201 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002203 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002205 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2206 } else {
2207 LOG(FATAL) << "Unexpected CAS type " << type;
2208 }
2209
Roland Levillain0d5a2812015-11-13 10:07:31 +00002210 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002211 // scheduling barriers at this time.
2212
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002213 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002214 __ setcc(kZero, out);
2215 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002216 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002217}
2218
2219void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002220 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002221}
2222
2223void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002224 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002225}
2226
2227void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002228 // The only read barrier implementation supporting the
2229 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2230 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002232 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002233}
2234
2235void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002236 LocationSummary* locations =
2237 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002238 locations->SetInAt(0, Location::RequiresRegister());
2239 locations->SetOut(Location::SameAsFirstInput());
2240 locations->AddTemp(Location::RequiresRegister());
2241}
2242
2243static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2244 X86_64Assembler* assembler) {
2245 Immediate imm_shift(shift);
2246 Immediate imm_mask(mask);
2247 __ movl(temp, reg);
2248 __ shrl(reg, imm_shift);
2249 __ andl(temp, imm_mask);
2250 __ andl(reg, imm_mask);
2251 __ shll(temp, imm_shift);
2252 __ orl(reg, temp);
2253}
2254
2255void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002256 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002257 LocationSummary* locations = invoke->GetLocations();
2258
2259 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2260 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2261
2262 /*
2263 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2264 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2265 * compared to generic luni implementation which has 5 rounds of swapping bits.
2266 * x = bswap x
2267 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2268 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2269 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2270 */
2271 __ bswapl(reg);
2272 SwapBits(reg, temp, 1, 0x55555555, assembler);
2273 SwapBits(reg, temp, 2, 0x33333333, assembler);
2274 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2275}
2276
2277void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002278 LocationSummary* locations =
2279 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002280 locations->SetInAt(0, Location::RequiresRegister());
2281 locations->SetOut(Location::SameAsFirstInput());
2282 locations->AddTemp(Location::RequiresRegister());
2283 locations->AddTemp(Location::RequiresRegister());
2284}
2285
2286static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2287 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2288 Immediate imm_shift(shift);
2289 __ movq(temp_mask, Immediate(mask));
2290 __ movq(temp, reg);
2291 __ shrq(reg, imm_shift);
2292 __ andq(temp, temp_mask);
2293 __ andq(reg, temp_mask);
2294 __ shlq(temp, imm_shift);
2295 __ orq(reg, temp);
2296}
2297
2298void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002299 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002300 LocationSummary* locations = invoke->GetLocations();
2301
2302 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2303 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2304 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2305
2306 /*
2307 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2308 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2309 * compared to generic luni implementation which has 5 rounds of swapping bits.
2310 * x = bswap x
2311 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2312 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2313 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2314 */
2315 __ bswapq(reg);
2316 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2317 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2318 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2319}
2320
Aart Bik3f67e692016-01-15 14:35:12 -08002321static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002322 ArenaAllocator* allocator, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Aart Bik3f67e692016-01-15 14:35:12 -08002323 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2324 // Do nothing if there is no popcnt support. This results in generating
2325 // a call for the intrinsic rather than direct code.
2326 return;
2327 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002328 LocationSummary* locations =
2329 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bik3f67e692016-01-15 14:35:12 -08002330 locations->SetInAt(0, Location::Any());
2331 locations->SetOut(Location::RequiresRegister());
2332}
2333
Aart Bikc5d47542016-01-27 17:00:35 -08002334static void GenBitCount(X86_64Assembler* assembler,
2335 CodeGeneratorX86_64* codegen,
2336 HInvoke* invoke,
2337 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002338 LocationSummary* locations = invoke->GetLocations();
2339 Location src = locations->InAt(0);
2340 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2341
2342 if (invoke->InputAt(0)->IsConstant()) {
2343 // Evaluate this at compile time.
2344 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002345 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002346 ? POPCOUNT(static_cast<uint64_t>(value))
2347 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002348 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002349 return;
2350 }
2351
2352 if (src.IsRegister()) {
2353 if (is_long) {
2354 __ popcntq(out, src.AsRegister<CpuRegister>());
2355 } else {
2356 __ popcntl(out, src.AsRegister<CpuRegister>());
2357 }
2358 } else if (is_long) {
2359 DCHECK(src.IsDoubleStackSlot());
2360 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2361 } else {
2362 DCHECK(src.IsStackSlot());
2363 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2364 }
2365}
2366
2367void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002368 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002369}
2370
2371void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002372 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long= */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002373}
2374
2375void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002376 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002377}
2378
2379void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002380 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long= */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002381}
2382
Vladimir Markoca6fff82017-10-03 14:49:14 +01002383static void CreateOneBitLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_high) {
2384 LocationSummary* locations =
2385 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc5d47542016-01-27 17:00:35 -08002386 locations->SetInAt(0, Location::Any());
2387 locations->SetOut(Location::RequiresRegister());
2388 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2389 : Location::RequiresRegister()); // any will do
2390}
2391
2392static void GenOneBit(X86_64Assembler* assembler,
2393 CodeGeneratorX86_64* codegen,
2394 HInvoke* invoke,
2395 bool is_high, bool is_long) {
2396 LocationSummary* locations = invoke->GetLocations();
2397 Location src = locations->InAt(0);
2398 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2399
2400 if (invoke->InputAt(0)->IsConstant()) {
2401 // Evaluate this at compile time.
2402 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2403 if (value == 0) {
2404 __ xorl(out, out); // Clears upper bits too.
2405 return;
2406 }
2407 // Nonzero value.
2408 if (is_high) {
2409 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2410 : 31 - CLZ(static_cast<uint32_t>(value));
2411 } else {
2412 value = is_long ? CTZ(static_cast<uint64_t>(value))
2413 : CTZ(static_cast<uint32_t>(value));
2414 }
2415 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002416 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002417 } else {
2418 codegen->Load32BitValue(out, 1 << value);
2419 }
2420 return;
2421 }
2422
2423 // Handle the non-constant cases.
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +05302424 if (!is_high && codegen->GetInstructionSetFeatures().HasAVX2() &&
2425 src.IsRegister()) {
2426 __ blsi(out, src.AsRegister<CpuRegister>());
2427 } else {
2428 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2429 if (is_high) {
2430 // Use architectural support: basically 1 << bsr.
2431 if (src.IsRegister()) {
2432 if (is_long) {
2433 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2434 } else {
2435 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2436 }
2437 } else if (is_long) {
2438 DCHECK(src.IsDoubleStackSlot());
2439 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
Aart Bikc5d47542016-01-27 17:00:35 -08002440 } else {
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +05302441 DCHECK(src.IsStackSlot());
2442 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
Aart Bikc5d47542016-01-27 17:00:35 -08002443 }
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +05302444 // BSR sets ZF if the input was zero.
2445 NearLabel is_zero, done;
2446 __ j(kEqual, &is_zero);
2447 __ movl(out, Immediate(1)); // Clears upper bits too.
Aart Bikc5d47542016-01-27 17:00:35 -08002448 if (is_long) {
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +05302449 __ shlq(out, tmp);
Aart Bikc5d47542016-01-27 17:00:35 -08002450 } else {
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +05302451 __ shll(out, tmp);
Aart Bikc5d47542016-01-27 17:00:35 -08002452 }
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +05302453 __ jmp(&done);
2454 __ Bind(&is_zero);
2455 __ xorl(out, out); // Clears upper bits too.
2456 __ Bind(&done);
2457 } else {
2458 // Copy input into temporary.
2459 if (src.IsRegister()) {
2460 if (is_long) {
2461 __ movq(tmp, src.AsRegister<CpuRegister>());
2462 } else {
2463 __ movl(tmp, src.AsRegister<CpuRegister>());
2464 }
2465 } else if (is_long) {
2466 DCHECK(src.IsDoubleStackSlot());
2467 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2468 } else {
2469 DCHECK(src.IsStackSlot());
2470 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2471 }
2472 // Do the bit twiddling: basically tmp & -tmp;
2473 if (is_long) {
2474 __ movq(out, tmp);
2475 __ negq(tmp);
2476 __ andq(out, tmp);
2477 } else {
2478 __ movl(out, tmp);
2479 __ negl(tmp);
2480 __ andl(out, tmp);
2481 }
Aart Bikc5d47542016-01-27 17:00:35 -08002482 }
2483 }
2484}
2485
2486void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002487 CreateOneBitLocations(allocator_, invoke, /* is_high= */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002488}
2489
2490void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002491 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ true, /* is_long= */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002492}
2493
2494void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002495 CreateOneBitLocations(allocator_, invoke, /* is_high= */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002496}
2497
2498void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002499 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ true, /* is_long= */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002500}
2501
2502void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002503 CreateOneBitLocations(allocator_, invoke, /* is_high= */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002504}
2505
2506void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002507 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ false, /* is_long= */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002508}
2509
2510void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002511 CreateOneBitLocations(allocator_, invoke, /* is_high= */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002512}
2513
2514void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002515 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high= */ false, /* is_long= */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002516}
2517
Vladimir Markoca6fff82017-10-03 14:49:14 +01002518static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2519 LocationSummary* locations =
2520 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002521 locations->SetInAt(0, Location::Any());
2522 locations->SetOut(Location::RequiresRegister());
2523}
2524
Aart Bikc5d47542016-01-27 17:00:35 -08002525static void GenLeadingZeros(X86_64Assembler* assembler,
2526 CodeGeneratorX86_64* codegen,
2527 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002528 LocationSummary* locations = invoke->GetLocations();
2529 Location src = locations->InAt(0);
2530 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2531
2532 int zero_value_result = is_long ? 64 : 32;
2533 if (invoke->InputAt(0)->IsConstant()) {
2534 // Evaluate this at compile time.
2535 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2536 if (value == 0) {
2537 value = zero_value_result;
2538 } else {
2539 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2540 }
Aart Bikc5d47542016-01-27 17:00:35 -08002541 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002542 return;
2543 }
2544
2545 // Handle the non-constant cases.
2546 if (src.IsRegister()) {
2547 if (is_long) {
2548 __ bsrq(out, src.AsRegister<CpuRegister>());
2549 } else {
2550 __ bsrl(out, src.AsRegister<CpuRegister>());
2551 }
2552 } else if (is_long) {
2553 DCHECK(src.IsDoubleStackSlot());
2554 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2555 } else {
2556 DCHECK(src.IsStackSlot());
2557 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2558 }
2559
2560 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002561 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002562 __ j(kEqual, &is_zero);
2563
2564 // Correct the result from BSR to get the CLZ result.
2565 __ xorl(out, Immediate(zero_value_result - 1));
2566 __ jmp(&done);
2567
2568 // Fix the zero case with the expected result.
2569 __ Bind(&is_zero);
2570 __ movl(out, Immediate(zero_value_result));
2571
2572 __ Bind(&done);
2573}
2574
2575void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002576 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002577}
2578
2579void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002580 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002581}
2582
2583void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002584 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002585}
2586
2587void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002588 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002589}
2590
Vladimir Markoca6fff82017-10-03 14:49:14 +01002591static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2592 LocationSummary* locations =
2593 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002594 locations->SetInAt(0, Location::Any());
2595 locations->SetOut(Location::RequiresRegister());
2596}
2597
Aart Bikc5d47542016-01-27 17:00:35 -08002598static void GenTrailingZeros(X86_64Assembler* assembler,
2599 CodeGeneratorX86_64* codegen,
2600 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002601 LocationSummary* locations = invoke->GetLocations();
2602 Location src = locations->InAt(0);
2603 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2604
2605 int zero_value_result = is_long ? 64 : 32;
2606 if (invoke->InputAt(0)->IsConstant()) {
2607 // Evaluate this at compile time.
2608 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2609 if (value == 0) {
2610 value = zero_value_result;
2611 } else {
2612 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2613 }
Aart Bikc5d47542016-01-27 17:00:35 -08002614 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002615 return;
2616 }
2617
2618 // Handle the non-constant cases.
2619 if (src.IsRegister()) {
2620 if (is_long) {
2621 __ bsfq(out, src.AsRegister<CpuRegister>());
2622 } else {
2623 __ bsfl(out, src.AsRegister<CpuRegister>());
2624 }
2625 } else if (is_long) {
2626 DCHECK(src.IsDoubleStackSlot());
2627 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2628 } else {
2629 DCHECK(src.IsStackSlot());
2630 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2631 }
2632
2633 // BSF sets ZF if the input was zero, and the output is undefined.
2634 NearLabel done;
2635 __ j(kNotEqual, &done);
2636
2637 // Fix the zero case with the expected result.
2638 __ movl(out, Immediate(zero_value_result));
2639
2640 __ Bind(&done);
2641}
2642
2643void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002644 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002645}
2646
2647void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002648 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002649}
2650
2651void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002652 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002653}
2654
2655void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002656 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long= */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002657}
2658
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002659void IntrinsicLocationsBuilderX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2660 InvokeRuntimeCallingConvention calling_convention;
2661 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2662 invoke,
2663 codegen_,
2664 Location::RegisterLocation(RAX),
2665 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2666}
2667
2668void IntrinsicCodeGeneratorX86_64::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01002669 IntrinsicVisitor::IntegerValueOfInfo info =
2670 IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002671 LocationSummary* locations = invoke->GetLocations();
2672 X86_64Assembler* assembler = GetAssembler();
2673
2674 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2675 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoeebb8212018-06-05 14:57:24 +01002676 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
2677 if (invoke->InputAt(0)->IsIntConstant()) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002678 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
Vladimir Marko6fd16062018-06-26 11:02:04 +01002679 if (static_cast<uint32_t>(value - info.low) < info.length) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002680 // Just embed the j.l.Integer in the code.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002681 DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
2682 codegen_->LoadBootImageAddress(out, info.value_boot_image_reference);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002683 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002684 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002685 // Allocate and initialize a new j.l.Integer.
2686 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2687 // JIT object table.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002688 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2689 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002690 __ movl(Address(out, info.value_offset), Immediate(value));
2691 }
2692 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002693 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002694 CpuRegister in = locations->InAt(0).AsRegister<CpuRegister>();
2695 // Check bounds of our cache.
2696 __ leal(out, Address(in, -info.low));
Vladimir Markoeebb8212018-06-05 14:57:24 +01002697 __ cmpl(out, Immediate(info.length));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002698 NearLabel allocate, done;
2699 __ j(kAboveEqual, &allocate);
2700 // If the value is within the bounds, load the j.l.Integer directly from the array.
Vladimir Markoeebb8212018-06-05 14:57:24 +01002701 DCHECK_NE(out.AsRegister(), argument.AsRegister());
Vladimir Marko6fd16062018-06-26 11:02:04 +01002702 codegen_->LoadBootImageAddress(argument, info.array_data_boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01002703 static_assert((1u << TIMES_4) == sizeof(mirror::HeapReference<mirror::Object>),
2704 "Check heap reference size.");
2705 __ movl(out, Address(argument, out, TIMES_4, 0));
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.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002710 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2711 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002712 __ movl(Address(out, info.value_offset), in);
2713 __ Bind(&done);
2714 }
2715}
2716
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002717void IntrinsicLocationsBuilderX86_64::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002718 LocationSummary* locations =
2719 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002720 locations->SetOut(Location::RequiresRegister());
2721}
2722
2723void IntrinsicCodeGeneratorX86_64::VisitThreadInterrupted(HInvoke* invoke) {
2724 X86_64Assembler* assembler = GetAssembler();
2725 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
2726 Address address = Address::Absolute
Andreas Gampe3db70682018-12-26 15:12:03 -08002727 (Thread::InterruptedOffset<kX86_64PointerSize>().Int32Value(), /* no_rip= */ true);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002728 NearLabel done;
2729 __ gs()->movl(out, address);
2730 __ testl(out, out);
2731 __ j(kEqual, &done);
2732 __ gs()->movl(address, Immediate(0));
2733 codegen_->MemoryFence();
2734 __ Bind(&done);
2735}
2736
Hans Boehmc7b28de2018-03-09 17:05:28 -08002737void IntrinsicLocationsBuilderX86_64::VisitReachabilityFence(HInvoke* invoke) {
2738 LocationSummary* locations =
2739 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
2740 locations->SetInAt(0, Location::Any());
2741}
2742
2743void IntrinsicCodeGeneratorX86_64::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
2744
Vladimir Marko4ee8e292017-06-02 15:39:30 +00002745UNIMPLEMENTED_INTRINSIC(X86_64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002746UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2747UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
xueliang.zhongcb58b072017-10-13 12:06:56 +01002748UNIMPLEMENTED_INTRINSIC(X86_64, CRC32Update)
Evgeny Astigeevich15c5b972018-11-20 13:41:40 +00002749UNIMPLEMENTED_INTRINSIC(X86_64, CRC32UpdateBytes)
Evgeny Astigeevich776a7c22018-12-17 11:40:34 +00002750UNIMPLEMENTED_INTRINSIC(X86_64, CRC32UpdateByteBuffer)
xueliang.zhong9ce340f2019-01-22 17:46:09 +00002751UNIMPLEMENTED_INTRINSIC(X86_64, FP16ToFloat)
Vladimir Marko7f958e32019-10-24 09:03:58 +00002752UNIMPLEMENTED_INTRINSIC(X86_64, FP16ToHalf)
Usama Arifb9f02c22019-10-25 17:37:33 +01002753UNIMPLEMENTED_INTRINSIC(X86_64, FP16Floor)
Usama Arif665aac42019-10-29 11:13:18 +00002754UNIMPLEMENTED_INTRINSIC(X86_64, FP16Ceil)
Usama Arif681692b2019-10-30 16:23:26 +00002755UNIMPLEMENTED_INTRINSIC(X86_64, FP16Rint)
Usama Arif457e9fa2019-11-11 15:29:59 +00002756UNIMPLEMENTED_INTRINSIC(X86_64, FP16Greater)
2757UNIMPLEMENTED_INTRINSIC(X86_64, FP16GreaterEquals)
2758UNIMPLEMENTED_INTRINSIC(X86_64, FP16Less)
2759UNIMPLEMENTED_INTRINSIC(X86_64, FP16LessEquals)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002760
Aart Bikff7d89c2016-11-07 08:49:28 -08002761UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOf);
2762UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002763UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferAppend);
2764UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferLength);
2765UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferToString);
Vladimir Markod4561172017-10-30 17:48:25 +00002766UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendObject);
2767UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendString);
2768UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendCharSequence);
2769UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendCharArray);
2770UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendBoolean);
2771UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendChar);
2772UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendInt);
2773UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendLong);
2774UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendFloat);
2775UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppendDouble);
Aart Bik71bf7b42016-11-16 10:17:46 -08002776UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderLength);
2777UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002778
Aart Bik0e54c012016-03-04 12:08:31 -08002779// 1.8.
2780UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2781UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2782UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2783UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2784UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002785
Aart Bik2f9fcc92016-03-01 15:16:54 -08002786UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002787
2788#undef __
2789
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002790} // namespace x86_64
2791} // namespace art