blob: 6337900b7163a62f21867a49aa21685e31375cab [file] [log] [blame]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_x86_64.h"
18
Andreas Gampe21030dd2015-05-07 14:46:15 -070019#include <limits>
20
Mark Mendellfb8d2792015-03-31 22:16:59 -040021#include "arch/x86_64/instruction_set_features_x86_64.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080022#include "art_method.h"
Mark Mendelld5897672015-08-12 21:16:41 -040023#include "base/bit_utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "code_generator_x86_64.h"
25#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070028#include "intrinsics_utils.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#include "lock_word.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080030#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070031#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080032#include "mirror/reference.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080033#include "mirror/string.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080034#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080036#include "utils/x86_64/assembler_x86_64.h"
37#include "utils/x86_64/constants_x86_64.h"
38
39namespace art {
40
41namespace x86_64 {
42
Mark Mendellfb8d2792015-03-31 22:16:59 -040043IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +010044 : allocator_(codegen->GetGraph()->GetAllocator()), codegen_(codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -040045}
46
Andreas Gampe71fb52f2014-12-29 17:43:08 -080047X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010048 return down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -080049}
50
Andreas Gampe878d58c2015-01-15 23:24:00 -080051ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010052 return codegen_->GetGraph()->GetAllocator();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080053}
54
55bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
56 Dispatch(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +000057 LocationSummary* res = invoke->GetLocations();
58 if (res == nullptr) {
59 return false;
60 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000061 return res->Intrinsified();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080062}
63
Roland Levillainec525fc2015-04-28 15:50:20 +010064static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010065 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010066 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080067}
68
Andreas Gampe85b62f22015-09-09 13:15:38 -070069using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080070
Roland Levillain0b671c02016-08-19 12:02:34 +010071// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
72#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
73
74// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
75class ReadBarrierSystemArrayCopySlowPathX86_64 : public SlowPathCode {
76 public:
77 explicit ReadBarrierSystemArrayCopySlowPathX86_64(HInstruction* instruction)
78 : SlowPathCode(instruction) {
79 DCHECK(kEmitCompilerReadBarrier);
80 DCHECK(kUseBakerReadBarrier);
81 }
82
83 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
84 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
85 LocationSummary* locations = instruction_->GetLocations();
86 DCHECK(locations->CanCall());
87 DCHECK(instruction_->IsInvokeStaticOrDirect())
88 << "Unexpected instruction in read barrier arraycopy slow path: "
89 << instruction_->DebugName();
90 DCHECK(instruction_->GetLocations()->Intrinsified());
91 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
92
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010093 int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +010094
95 CpuRegister src_curr_addr = locations->GetTemp(0).AsRegister<CpuRegister>();
96 CpuRegister dst_curr_addr = locations->GetTemp(1).AsRegister<CpuRegister>();
97 CpuRegister src_stop_addr = locations->GetTemp(2).AsRegister<CpuRegister>();
98
99 __ Bind(GetEntryLabel());
100 NearLabel loop;
101 __ Bind(&loop);
102 __ movl(CpuRegister(TMP), Address(src_curr_addr, 0));
103 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
104 // TODO: Inline the mark bit check before calling the runtime?
105 // TMP = ReadBarrier::Mark(TMP);
106 // No need to save live registers; it's taken care of by the
107 // entrypoint. Also, there is no need to update the stack mask,
108 // as this runtime call will not trigger a garbage collection.
Roland Levillain97c46462017-05-11 14:04:03 +0100109 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(TMP);
Roland Levillain0b671c02016-08-19 12:02:34 +0100110 // This runtime call does not require a stack map.
111 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
112 __ MaybePoisonHeapReference(CpuRegister(TMP));
113 __ movl(Address(dst_curr_addr, 0), CpuRegister(TMP));
114 __ addl(src_curr_addr, Immediate(element_size));
115 __ addl(dst_curr_addr, Immediate(element_size));
116 __ cmpl(src_curr_addr, src_stop_addr);
117 __ j(kNotEqual, &loop);
118 __ jmp(GetExitLabel());
119 }
120
121 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86_64"; }
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86_64);
125};
126
127#undef __
128
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800129#define __ assembler->
130
Vladimir Markoca6fff82017-10-03 14:49:14 +0100131static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
132 LocationSummary* locations =
133 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800134 locations->SetInAt(0, Location::RequiresFpuRegister());
135 locations->SetOut(Location::RequiresRegister());
136}
137
Vladimir Markoca6fff82017-10-03 14:49:14 +0100138static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
139 LocationSummary* locations =
140 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800141 locations->SetInAt(0, Location::RequiresRegister());
142 locations->SetOut(Location::RequiresFpuRegister());
143}
144
145static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
146 Location input = locations->InAt(0);
147 Location output = locations->Out();
148 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
149}
150
151static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
152 Location input = locations->InAt(0);
153 Location output = locations->Out();
154 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
155}
156
157void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100158 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800159}
160void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100161 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800162}
163
164void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000165 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800166}
167void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000168 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800169}
170
171void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100172 CreateFPToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800173}
174void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100175 CreateIntToFPLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800176}
177
178void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000179 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800180}
181void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000182 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800183}
184
Vladimir Markoca6fff82017-10-03 14:49:14 +0100185static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
186 LocationSummary* locations =
187 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800188 locations->SetInAt(0, Location::RequiresRegister());
189 locations->SetOut(Location::SameAsFirstInput());
190}
191
192static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100193 DataType::Type size,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800194 X86_64Assembler* assembler) {
195 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
196
197 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100198 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800199 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
200 __ bswapl(out);
201 __ sarl(out, Immediate(16));
202 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100203 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800204 __ bswapl(out);
205 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100206 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800207 __ bswapq(out);
208 break;
209 default:
210 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
211 UNREACHABLE();
212 }
213}
214
215void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100216 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800217}
218
219void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100220 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800221}
222
223void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100224 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800225}
226
227void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100228 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800229}
230
231void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100232 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800233}
234
235void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100236 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800237}
238
239
240// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
241// need is 64b.
242
Vladimir Markoca6fff82017-10-03 14:49:14 +0100243static void CreateFloatToFloatPlusTemps(ArenaAllocator* allocator, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800244 // TODO: Enable memory operations when the assembler supports them.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100245 LocationSummary* locations =
246 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800247 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800248 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400249 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800250}
251
Mark Mendell39dcf552015-04-09 20:42:42 -0400252static void MathAbsFP(LocationSummary* locations,
253 bool is64bit,
254 X86_64Assembler* assembler,
255 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800256 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800257
Mark Mendellcfa410b2015-05-25 16:02:44 -0400258 DCHECK(output.IsFpuRegister());
259 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800260
Mark Mendellcfa410b2015-05-25 16:02:44 -0400261 // TODO: Can mask directly with constant area using pand if we can guarantee
262 // that the literal is aligned on a 16 byte boundary. This will avoid a
263 // temporary.
264 if (is64bit) {
265 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
266 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800267 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400268 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
269 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800270 }
271}
272
273void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100274 CreateFloatToFloatPlusTemps(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800275}
276
277void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000278 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800279}
280
281void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100282 CreateFloatToFloatPlusTemps(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800283}
284
285void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000286 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800287}
288
Vladimir Markoca6fff82017-10-03 14:49:14 +0100289static void CreateIntToIntPlusTemp(ArenaAllocator* allocator, HInvoke* invoke) {
290 LocationSummary* locations =
291 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800292 locations->SetInAt(0, Location::RequiresRegister());
293 locations->SetOut(Location::SameAsFirstInput());
294 locations->AddTemp(Location::RequiresRegister());
295}
296
297static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
298 Location output = locations->Out();
299 CpuRegister out = output.AsRegister<CpuRegister>();
300 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
301
302 if (is64bit) {
303 // Create mask.
304 __ movq(mask, out);
305 __ sarq(mask, Immediate(63));
306 // Add mask.
307 __ addq(out, mask);
308 __ xorq(out, mask);
309 } else {
310 // Create mask.
311 __ movl(mask, out);
312 __ sarl(mask, Immediate(31));
313 // Add mask.
314 __ addl(out, mask);
315 __ xorl(out, mask);
316 }
317}
318
319void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100320 CreateIntToIntPlusTemp(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800321}
322
323void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000324 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800325}
326
327void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100328 CreateIntToIntPlusTemp(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800329}
330
331void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000332 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800333}
334
Mark Mendell39dcf552015-04-09 20:42:42 -0400335static void GenMinMaxFP(LocationSummary* locations,
336 bool is_min,
337 bool is_double,
338 X86_64Assembler* assembler,
339 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800340 Location op1_loc = locations->InAt(0);
341 Location op2_loc = locations->InAt(1);
342 Location out_loc = locations->Out();
343 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
344
345 // Shortcut for same input locations.
346 if (op1_loc.Equals(op2_loc)) {
347 DCHECK(out_loc.Equals(op1_loc));
348 return;
349 }
350
351 // (out := op1)
352 // out <=? op2
353 // if Nan jmp Nan_label
354 // if out is min jmp done
355 // if op2 is min jmp op2_label
356 // handle -0/+0
357 // jmp done
358 // Nan_label:
359 // out := NaN
360 // op2_label:
361 // out := op2
362 // done:
363 //
364 // This removes one jmp, but needs to copy one input (op1) to out.
365 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400366 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800367
368 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
369
Mark Mendell0c9497d2015-08-21 09:30:05 -0400370 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800371 if (is_double) {
372 __ ucomisd(out, op2);
373 } else {
374 __ ucomiss(out, op2);
375 }
376
377 __ j(Condition::kParityEven, &nan);
378
379 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
380 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
381
382 // Handle 0.0/-0.0.
383 if (is_min) {
384 if (is_double) {
385 __ orpd(out, op2);
386 } else {
387 __ orps(out, op2);
388 }
389 } else {
390 if (is_double) {
391 __ andpd(out, op2);
392 } else {
393 __ andps(out, op2);
394 }
395 }
396 __ jmp(&done);
397
398 // NaN handling.
399 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800400 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400401 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800402 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400403 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800404 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800405 __ jmp(&done);
406
407 // out := op2;
408 __ Bind(&op2_label);
409 if (is_double) {
410 __ movsd(out, op2);
411 } else {
412 __ movss(out, op2);
413 }
414
415 // Done.
416 __ Bind(&done);
417}
418
Vladimir Markoca6fff82017-10-03 14:49:14 +0100419static void CreateFPFPToFP(ArenaAllocator* allocator, HInvoke* invoke) {
420 LocationSummary* locations =
421 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800422 locations->SetInAt(0, Location::RequiresFpuRegister());
423 locations->SetInAt(1, Location::RequiresFpuRegister());
424 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
425 // the second input to be the output (we can simply swap inputs).
426 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800427}
428
429void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100430 CreateFPFPToFP(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800431}
432
433void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000434 GenMinMaxFP(
435 invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800436}
437
438void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100439 CreateFPFPToFP(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800440}
441
442void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000443 GenMinMaxFP(
444 invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800445}
446
447void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100448 CreateFPFPToFP(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800449}
450
451void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000452 GenMinMaxFP(
453 invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800454}
455
456void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100457 CreateFPFPToFP(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800458}
459
460void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000461 GenMinMaxFP(
462 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800463}
464
465static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
466 X86_64Assembler* assembler) {
467 Location op1_loc = locations->InAt(0);
468 Location op2_loc = locations->InAt(1);
469
470 // Shortcut for same input locations.
471 if (op1_loc.Equals(op2_loc)) {
472 // Can return immediately, as op1_loc == out_loc.
473 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
474 // a copy here.
475 DCHECK(locations->Out().Equals(op1_loc));
476 return;
477 }
478
479 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
480 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
481
482 // (out := op1)
483 // out <=? op2
484 // if out is min jmp done
485 // out := op2
486 // done:
487
488 if (is_long) {
489 __ cmpq(out, op2);
490 } else {
491 __ cmpl(out, op2);
492 }
493
494 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
495}
496
Vladimir Markoca6fff82017-10-03 14:49:14 +0100497static void CreateIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
498 LocationSummary* locations =
499 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800500 locations->SetInAt(0, Location::RequiresRegister());
501 locations->SetInAt(1, Location::RequiresRegister());
502 locations->SetOut(Location::SameAsFirstInput());
503}
504
505void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100506 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800507}
508
509void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000510 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800511}
512
513void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100514 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800515}
516
517void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000518 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800519}
520
521void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100522 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800523}
524
525void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000526 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800527}
528
529void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100530 CreateIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800531}
532
533void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000534 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800535}
536
Vladimir Markoca6fff82017-10-03 14:49:14 +0100537static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
538 LocationSummary* locations =
539 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800540 locations->SetInAt(0, Location::RequiresFpuRegister());
541 locations->SetOut(Location::RequiresFpuRegister());
542}
543
544void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100545 CreateFPToFPLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800546}
547
548void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
549 LocationSummary* locations = invoke->GetLocations();
550 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
551 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
552
553 GetAssembler()->sqrtsd(out, in);
554}
555
Mark Mendellfb8d2792015-03-31 22:16:59 -0400556static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100557 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400558
559 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100560 codegen->GenerateStaticOrDirectCall(
561 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400562
563 // Copy the result back to the expected output.
564 Location out = invoke->GetLocations()->Out();
565 if (out.IsValid()) {
566 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700567 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400568 }
569}
570
Vladimir Markoca6fff82017-10-03 14:49:14 +0100571static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator,
572 HInvoke* invoke,
573 CodeGeneratorX86_64* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400574 // Do we have instruction support?
575 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100576 CreateFPToFPLocations(allocator, invoke);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400577 return;
578 }
579
580 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100581 LocationSummary* locations =
582 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400583 InvokeRuntimeCallingConvention calling_convention;
584 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
585 locations->SetOut(Location::FpuRegisterLocation(XMM0));
586 // Needs to be RDI for the invoke.
587 locations->AddTemp(Location::RegisterLocation(RDI));
588}
589
590static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
591 HInvoke* invoke,
592 X86_64Assembler* assembler,
593 int round_mode) {
594 LocationSummary* locations = invoke->GetLocations();
595 if (locations->WillCall()) {
596 InvokeOutOfLineIntrinsic(codegen, invoke);
597 } else {
598 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
599 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
600 __ roundsd(out, in, Immediate(round_mode));
601 }
602}
603
604void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100605 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400606}
607
608void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
609 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
610}
611
612void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100613 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400614}
615
616void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
617 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
618}
619
620void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100621 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400622}
623
624void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
625 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
626}
627
Vladimir Markoca6fff82017-10-03 14:49:14 +0100628static void CreateSSE41FPToIntLocations(ArenaAllocator* allocator,
629 HInvoke* invoke,
630 CodeGeneratorX86_64* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400631 // Do we have instruction support?
632 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100633 LocationSummary* locations =
634 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400635 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600636 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400637 locations->AddTemp(Location::RequiresFpuRegister());
Aart Bik349f3882016-08-02 15:40:56 -0700638 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400639 return;
640 }
641
642 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100643 LocationSummary* locations =
644 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400645 InvokeRuntimeCallingConvention calling_convention;
646 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
647 locations->SetOut(Location::RegisterLocation(RAX));
648 // Needs to be RDI for the invoke.
649 locations->AddTemp(Location::RegisterLocation(RDI));
650}
651
652void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100653 CreateSSE41FPToIntLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400654}
655
656void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
657 LocationSummary* locations = invoke->GetLocations();
658 if (locations->WillCall()) {
659 InvokeOutOfLineIntrinsic(codegen_, invoke);
660 return;
661 }
662
Mark Mendellfb8d2792015-03-31 22:16:59 -0400663 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
664 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700665 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
666 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
667 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400668 X86_64Assembler* assembler = GetAssembler();
669
Aart Bik349f3882016-08-02 15:40:56 -0700670 // Since no direct x86 rounding instruction matches the required semantics,
671 // this intrinsic is implemented as follows:
672 // result = floor(in);
673 // if (in - result >= 0.5f)
674 // result = result + 1.0f;
675 __ movss(t2, in);
676 __ roundss(t1, in, Immediate(1));
677 __ subss(t2, t1);
678 __ comiss(t2, codegen_->LiteralFloatAddress(0.5f));
679 __ j(kBelow, &skip_incr);
680 __ addss(t1, codegen_->LiteralFloatAddress(1.0f));
681 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400682
Aart Bik349f3882016-08-02 15:40:56 -0700683 // Final conversion to an integer. Unfortunately this also does not have a
684 // direct x86 instruction, since NaN should map to 0 and large positive
685 // values need to be clipped to the extreme value.
686 codegen_->Load32BitValue(out, kPrimIntMax);
687 __ cvtsi2ss(t2, out);
688 __ comiss(t1, t2);
689 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
690 __ movl(out, Immediate(0)); // does not change flags
691 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
692 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400693 __ Bind(&done);
694}
695
696void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100697 CreateSSE41FPToIntLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400698}
699
700void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
701 LocationSummary* locations = invoke->GetLocations();
702 if (locations->WillCall()) {
703 InvokeOutOfLineIntrinsic(codegen_, invoke);
704 return;
705 }
706
Mark Mendellfb8d2792015-03-31 22:16:59 -0400707 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
708 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700709 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
710 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
711 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400712 X86_64Assembler* assembler = GetAssembler();
713
Aart Bik349f3882016-08-02 15:40:56 -0700714 // Since no direct x86 rounding instruction matches the required semantics,
715 // this intrinsic is implemented as follows:
716 // result = floor(in);
717 // if (in - result >= 0.5)
718 // result = result + 1.0f;
719 __ movsd(t2, in);
720 __ roundsd(t1, in, Immediate(1));
721 __ subsd(t2, t1);
722 __ comisd(t2, codegen_->LiteralDoubleAddress(0.5));
723 __ j(kBelow, &skip_incr);
724 __ addsd(t1, codegen_->LiteralDoubleAddress(1.0f));
725 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400726
Aart Bik349f3882016-08-02 15:40:56 -0700727 // Final conversion to an integer. Unfortunately this also does not have a
728 // direct x86 instruction, since NaN should map to 0 and large positive
729 // values need to be clipped to the extreme value.
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600730 codegen_->Load64BitValue(out, kPrimLongMax);
Aart Bik349f3882016-08-02 15:40:56 -0700731 __ cvtsi2sd(t2, out, /* is64bit */ true);
732 __ comisd(t1, t2);
733 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
734 __ movl(out, Immediate(0)); // does not change flags, implicit zero extension to 64-bit
735 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
736 __ cvttsd2si(out, t1, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400737 __ Bind(&done);
738}
739
Vladimir Markoca6fff82017-10-03 14:49:14 +0100740static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
741 LocationSummary* locations =
742 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400743 InvokeRuntimeCallingConvention calling_convention;
744 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
745 locations->SetOut(Location::FpuRegisterLocation(XMM0));
746
747 // We have to ensure that the native code doesn't clobber the XMM registers which are
748 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
749 // saved in the prologue and properly restored.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100750 for (FloatRegister fp_reg : non_volatile_xmm_regs) {
Mark Mendella4f12202015-08-06 15:23:34 -0400751 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
752 }
753}
754
755static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
756 QuickEntrypointEnum entry) {
757 LocationSummary* locations = invoke->GetLocations();
758 DCHECK(locations->WillCall());
759 DCHECK(invoke->IsInvokeStaticOrDirect());
Mark Mendella4f12202015-08-06 15:23:34 -0400760
Serban Constantinescuba45db02016-07-12 22:53:02 +0100761 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400762}
763
764void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100765 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400766}
767
768void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
769 GenFPToFPCall(invoke, codegen_, kQuickCos);
770}
771
772void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100773 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400774}
775
776void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
777 GenFPToFPCall(invoke, codegen_, kQuickSin);
778}
779
780void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100781 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400782}
783
784void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
785 GenFPToFPCall(invoke, codegen_, kQuickAcos);
786}
787
788void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100789 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400790}
791
792void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
793 GenFPToFPCall(invoke, codegen_, kQuickAsin);
794}
795
796void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100797 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400798}
799
800void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
801 GenFPToFPCall(invoke, codegen_, kQuickAtan);
802}
803
804void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100805 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400806}
807
808void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
809 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
810}
811
812void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100813 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400814}
815
816void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
817 GenFPToFPCall(invoke, codegen_, kQuickCosh);
818}
819
820void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100821 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400822}
823
824void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
825 GenFPToFPCall(invoke, codegen_, kQuickExp);
826}
827
828void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100829 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400830}
831
832void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
833 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
834}
835
836void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100837 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400838}
839
840void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
841 GenFPToFPCall(invoke, codegen_, kQuickLog);
842}
843
844void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100845 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400846}
847
848void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
849 GenFPToFPCall(invoke, codegen_, kQuickLog10);
850}
851
852void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100853 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400854}
855
856void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
857 GenFPToFPCall(invoke, codegen_, kQuickSinh);
858}
859
860void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100861 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400862}
863
864void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
865 GenFPToFPCall(invoke, codegen_, kQuickTan);
866}
867
868void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100869 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400870}
871
872void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
873 GenFPToFPCall(invoke, codegen_, kQuickTanh);
874}
875
Vladimir Markoca6fff82017-10-03 14:49:14 +0100876static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
877 LocationSummary* locations =
878 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400879 InvokeRuntimeCallingConvention calling_convention;
880 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
881 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
882 locations->SetOut(Location::FpuRegisterLocation(XMM0));
883
884 // We have to ensure that the native code doesn't clobber the XMM registers which are
885 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
886 // saved in the prologue and properly restored.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100887 for (FloatRegister fp_reg : non_volatile_xmm_regs) {
Mark Mendella4f12202015-08-06 15:23:34 -0400888 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
889 }
890}
891
892void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100893 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400894}
895
896void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
897 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
898}
899
900void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100901 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400902}
903
904void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
905 GenFPToFPCall(invoke, codegen_, kQuickHypot);
906}
907
908void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100909 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400910}
911
912void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
913 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
914}
915
Mark Mendell6bc53a92015-07-01 14:26:52 -0400916void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
917 // Check to see if we have known failures that will cause us to have to bail out
918 // to the runtime, and just generate the runtime call directly.
919 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
920 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
921
922 // The positions must be non-negative.
923 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
924 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
925 // We will have to fail anyways.
926 return;
927 }
928
929 // The length must be > 0.
930 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
931 if (length != nullptr) {
932 int32_t len = length->GetValue();
933 if (len < 0) {
934 // Just call as normal.
935 return;
936 }
937 }
938
Vladimir Markoca6fff82017-10-03 14:49:14 +0100939 LocationSummary* locations =
940 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100941 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400942 locations->SetInAt(0, Location::RequiresRegister());
943 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
944 locations->SetInAt(2, Location::RequiresRegister());
945 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
946 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
947
948 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
949 locations->AddTemp(Location::RegisterLocation(RSI));
950 locations->AddTemp(Location::RegisterLocation(RDI));
951 locations->AddTemp(Location::RegisterLocation(RCX));
952}
953
954static void CheckPosition(X86_64Assembler* assembler,
955 Location pos,
956 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100957 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700958 SlowPathCode* slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100959 CpuRegister temp,
960 bool length_is_input_length = false) {
961 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400962 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
963
964 if (pos.IsConstant()) {
965 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
966 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100967 if (!length_is_input_length) {
968 // Check that length(input) >= length.
969 if (length.IsConstant()) {
970 __ cmpl(Address(input, length_offset),
971 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
972 } else {
973 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
974 }
975 __ j(kLess, slow_path->GetEntryLabel());
976 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400977 } else {
978 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100979 __ movl(temp, Address(input, length_offset));
980 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400981 __ j(kLess, slow_path->GetEntryLabel());
982
983 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100984 if (length.IsConstant()) {
985 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
986 } else {
987 __ cmpl(temp, length.AsRegister<CpuRegister>());
988 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400989 __ j(kLess, slow_path->GetEntryLabel());
990 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100991 } else if (length_is_input_length) {
992 // The only way the copy can succeed is if pos is zero.
993 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
994 __ testl(pos_reg, pos_reg);
995 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400996 } else {
997 // Check that pos >= 0.
998 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
999 __ testl(pos_reg, pos_reg);
1000 __ j(kLess, slow_path->GetEntryLabel());
1001
1002 // Check that pos <= length(input).
1003 __ cmpl(Address(input, length_offset), pos_reg);
1004 __ j(kLess, slow_path->GetEntryLabel());
1005
1006 // Check that (length(input) - pos) >= length.
1007 __ movl(temp, Address(input, length_offset));
1008 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001009 if (length.IsConstant()) {
1010 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1011 } else {
1012 __ cmpl(temp, length.AsRegister<CpuRegister>());
1013 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001014 __ j(kLess, slow_path->GetEntryLabel());
1015 }
1016}
1017
1018void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1019 X86_64Assembler* assembler = GetAssembler();
1020 LocationSummary* locations = invoke->GetLocations();
1021
1022 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001023 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001024 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001025 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001026 Location length = locations->InAt(4);
1027
1028 // Temporaries that we need for MOVSW.
1029 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1030 DCHECK_EQ(src_base.AsRegister(), RSI);
1031 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1032 DCHECK_EQ(dest_base.AsRegister(), RDI);
1033 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1034 DCHECK_EQ(count.AsRegister(), RCX);
1035
Andreas Gampe85b62f22015-09-09 13:15:38 -07001036 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001037 codegen_->AddSlowPath(slow_path);
1038
1039 // Bail out if the source and destination are the same.
1040 __ cmpl(src, dest);
1041 __ j(kEqual, slow_path->GetEntryLabel());
1042
1043 // Bail out if the source is null.
1044 __ testl(src, src);
1045 __ j(kEqual, slow_path->GetEntryLabel());
1046
1047 // Bail out if the destination is null.
1048 __ testl(dest, dest);
1049 __ j(kEqual, slow_path->GetEntryLabel());
1050
1051 // If the length is negative, bail out.
1052 // We have already checked in the LocationsBuilder for the constant case.
1053 if (!length.IsConstant()) {
1054 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1055 __ j(kLess, slow_path->GetEntryLabel());
1056 }
1057
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001058 // Validity checks: source. Use src_base as a temporary register.
1059 CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001060
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001061 // Validity checks: dest. Use src_base as a temporary register.
1062 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001063
Mark Mendell6bc53a92015-07-01 14:26:52 -04001064 // We need the count in RCX.
1065 if (length.IsConstant()) {
1066 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1067 } else {
1068 __ movl(count, length.AsRegister<CpuRegister>());
1069 }
1070
Mark Mendell6bc53a92015-07-01 14:26:52 -04001071 // Okay, everything checks out. Finally time to do the copy.
1072 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001073 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001074 DCHECK_EQ(char_size, 2u);
1075
1076 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1077
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001078 if (src_pos.IsConstant()) {
1079 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1080 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001081 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001082 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001083 ScaleFactor::TIMES_2, data_offset));
1084 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001085 if (dest_pos.IsConstant()) {
1086 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1087 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001088 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001089 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001090 ScaleFactor::TIMES_2, data_offset));
1091 }
1092
1093 // Do the move.
1094 __ rep_movsw();
1095
1096 __ Bind(slow_path->GetExitLabel());
1097}
1098
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001099
1100void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001101 // The only read barrier implementation supporting the
1102 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1103 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01001104 return;
1105 }
1106
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001107 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001108}
1109
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001110// Compute base source address, base destination address, and end
1111// source address for the System.arraycopy intrinsic in `src_base`,
1112// `dst_base` and `src_end` respectively.
1113static void GenSystemArrayCopyAddresses(X86_64Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001114 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001115 const CpuRegister& src,
1116 const Location& src_pos,
1117 const CpuRegister& dst,
1118 const Location& dst_pos,
1119 const Location& copy_length,
1120 const CpuRegister& src_base,
1121 const CpuRegister& dst_base,
1122 const CpuRegister& src_end) {
1123 // This routine is only used by the SystemArrayCopy intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001124 DCHECK_EQ(type, DataType::Type::kReference);
1125 const int32_t element_size = DataType::Size(type);
1126 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001127 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
1128
1129 if (src_pos.IsConstant()) {
1130 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1131 __ leal(src_base, Address(src, element_size * constant + data_offset));
1132 } else {
1133 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
1134 }
1135
1136 if (dst_pos.IsConstant()) {
1137 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
1138 __ leal(dst_base, Address(dst, element_size * constant + data_offset));
1139 } else {
1140 __ leal(dst_base, Address(dst, dst_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
1141 }
1142
1143 if (copy_length.IsConstant()) {
1144 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
1145 __ leal(src_end, Address(src_base, element_size * constant));
1146 } else {
1147 __ leal(src_end, Address(src_base, copy_length.AsRegister<CpuRegister>(), scale_factor, 0));
1148 }
1149}
1150
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001151void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001152 // The only read barrier implementation supporting the
1153 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1154 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001155
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001156 X86_64Assembler* assembler = GetAssembler();
1157 LocationSummary* locations = invoke->GetLocations();
1158
1159 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1160 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1161 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1162 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001163 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001164
1165 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1166 Location src_pos = locations->InAt(1);
1167 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1168 Location dest_pos = locations->InAt(3);
1169 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +01001170 Location temp1_loc = locations->GetTemp(0);
1171 CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
1172 Location temp2_loc = locations->GetTemp(1);
1173 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
1174 Location temp3_loc = locations->GetTemp(2);
1175 CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
1176 Location TMP_loc = Location::RegisterLocation(TMP);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001177
Roland Levillain0b671c02016-08-19 12:02:34 +01001178 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1179 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001180
Roland Levillainebea3d22016-04-12 15:42:57 +01001181 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001182 SystemArrayCopyOptimizations optimizations(invoke);
1183
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001184 // If source and destination are the same, we go to slow path if we need to do
1185 // forward copying.
1186 if (src_pos.IsConstant()) {
1187 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1188 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001189 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1190 if (optimizations.GetDestinationIsSource()) {
1191 // Checked when building locations.
1192 DCHECK_GE(src_pos_constant, dest_pos_constant);
1193 } else if (src_pos_constant < dest_pos_constant) {
1194 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001195 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001196 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001197 } else {
1198 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001199 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001200 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001201 }
1202 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001203 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001204 }
1205 } else {
1206 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001207 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001208 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001209 }
1210 if (dest_pos.IsConstant()) {
1211 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1212 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001213 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001214 } else {
1215 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001216 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001217 }
1218 }
1219
Roland Levillainebea3d22016-04-12 15:42:57 +01001220 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001221
1222 if (!optimizations.GetSourceIsNotNull()) {
1223 // Bail out if the source is null.
1224 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01001225 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001226 }
1227
1228 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1229 // Bail out if the destination is null.
1230 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001231 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001232 }
1233
1234 // If the length is negative, bail out.
1235 // We have already checked in the LocationsBuilder for the constant case.
1236 if (!length.IsConstant() &&
1237 !optimizations.GetCountIsSourceLength() &&
1238 !optimizations.GetCountIsDestinationLength()) {
1239 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001240 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001241 }
1242
1243 // Validity checks: source.
1244 CheckPosition(assembler,
1245 src_pos,
1246 src,
1247 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001248 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001249 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001250 optimizations.GetCountIsSourceLength());
1251
1252 // Validity checks: dest.
1253 CheckPosition(assembler,
1254 dest_pos,
1255 dest,
1256 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001257 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001258 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001259 optimizations.GetCountIsDestinationLength());
1260
1261 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1262 // Check whether all elements of the source array are assignable to the component
1263 // type of the destination array. We do two checks: the classes are the same,
1264 // or the destination is Object[]. If none of these checks succeed, we go to the
1265 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01001266
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001267 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +01001268 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1269 // /* HeapReference<Class> */ temp1 = dest->klass_
1270 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001271 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001272 // Register `temp1` is not trashed by the read barrier emitted
1273 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1274 // method produces a call to a ReadBarrierMarkRegX entry point,
1275 // which saves all potentially live registers, including
1276 // temporaries such a `temp1`.
1277 // /* HeapReference<Class> */ temp2 = src->klass_
1278 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001279 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001280 // If heap poisoning is enabled, `temp1` and `temp2` have been
1281 // unpoisoned by the the previous calls to
1282 // GenerateFieldLoadWithBakerReadBarrier.
1283 } else {
1284 // /* HeapReference<Class> */ temp1 = dest->klass_
1285 __ movl(temp1, Address(dest, class_offset));
1286 // /* HeapReference<Class> */ temp2 = src->klass_
1287 __ movl(temp2, Address(src, class_offset));
1288 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1289 !optimizations.GetSourceIsNonPrimitiveArray()) {
1290 // One or two of the references need to be unpoisoned. Unpoison them
1291 // both to make the identity check valid.
1292 __ MaybeUnpoisonHeapReference(temp1);
1293 __ MaybeUnpoisonHeapReference(temp2);
1294 did_unpoison = true;
1295 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001296 }
1297
1298 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1299 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001300 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1301 // /* HeapReference<Class> */ TMP = temp1->component_type_
1302 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001303 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001304 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1305 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1306 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1307 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1308 } else {
1309 // /* HeapReference<Class> */ TMP = temp1->component_type_
1310 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1311 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1312 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1313 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1314 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001315 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001316 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001317 }
1318
1319 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1320 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001321 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1322 // For the same reason given earlier, `temp1` is not trashed by the
1323 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1324 // /* HeapReference<Class> */ TMP = temp2->component_type_
1325 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001326 invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001327 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1328 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1329 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1330 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1331 } else {
1332 // /* HeapReference<Class> */ TMP = temp2->component_type_
1333 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1334 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1335 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1336 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1337 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001338 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001339 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001340 }
1341
1342 __ cmpl(temp1, temp2);
1343
1344 if (optimizations.GetDestinationIsTypedObjectArray()) {
1345 NearLabel do_copy;
1346 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001347 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1348 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1349 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001350 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001351 // We do not need to emit a read barrier for the following
1352 // heap reference load, as `temp1` is only used in a
1353 // comparison with null below, and this reference is not
1354 // kept afterwards.
1355 __ cmpl(Address(temp1, super_offset), Immediate(0));
1356 } else {
1357 if (!did_unpoison) {
1358 __ MaybeUnpoisonHeapReference(temp1);
1359 }
1360 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1361 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001362 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001363 // No need to unpoison the following heap reference load, as
1364 // we're comparing against null.
1365 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001366 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001367 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001368 __ Bind(&do_copy);
1369 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001370 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001371 }
1372 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1373 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1374 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001375 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1376 // /* HeapReference<Class> */ temp1 = src->klass_
1377 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001378 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001379 // /* HeapReference<Class> */ TMP = temp1->component_type_
1380 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001381 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001382 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1383 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1384 } else {
1385 // /* HeapReference<Class> */ temp1 = src->klass_
1386 __ movl(temp1, Address(src, class_offset));
1387 __ MaybeUnpoisonHeapReference(temp1);
1388 // /* HeapReference<Class> */ TMP = temp1->component_type_
1389 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1390 // No need to unpoison `TMP` now, as we're comparing against null.
1391 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1392 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1393 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1394 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001395 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001396 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001397 }
1398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001399 const DataType::Type type = DataType::Type::kReference;
1400 const int32_t element_size = DataType::Size(type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001401
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001402 // Compute base source address, base destination address, and end
1403 // source address in `temp1`, `temp2` and `temp3` respectively.
1404 GenSystemArrayCopyAddresses(
1405 GetAssembler(), type, src, src_pos, dest, dest_pos, length, temp1, temp2, temp3);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001406
Roland Levillain0b671c02016-08-19 12:02:34 +01001407 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1408 // SystemArrayCopy implementation for Baker read barriers (see
1409 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1410 //
1411 // if (src_ptr != end_ptr) {
1412 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1413 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001414 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01001415 // if (is_gray) {
1416 // // Slow-path copy.
1417 // do {
1418 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1419 // } while (src_ptr != end_ptr)
1420 // } else {
1421 // // Fast-path copy.
1422 // do {
1423 // *dest_ptr++ = *src_ptr++;
1424 // } while (src_ptr != end_ptr)
1425 // }
1426 // }
1427
1428 NearLabel loop, done;
1429
1430 // Don't enter copy loop if `length == 0`.
1431 __ cmpl(temp1, temp3);
1432 __ j(kEqual, &done);
1433
Vladimir Marko953437b2016-08-24 08:30:46 +00001434 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001435 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1436 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00001437 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
1438 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
1439 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
1440
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001441 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00001442 // goto slow_path;
1443 // At this point, just do the "if" and make sure that flags are preserved until the branch.
1444 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01001445
1446 // Load fence to prevent load-load reordering.
1447 // Note that this is a no-op, thanks to the x86-64 memory model.
1448 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1449
1450 // Slow path used to copy array when `src` is gray.
1451 SlowPathCode* read_barrier_slow_path =
1452 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
1453 codegen_->AddSlowPath(read_barrier_slow_path);
1454
Vladimir Marko953437b2016-08-24 08:30:46 +00001455 // We have done the "if" of the gray bit check above, now branch based on the flags.
1456 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01001457
1458 // Fast-path copy.
1459 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1460 // poison/unpoison.
1461 __ Bind(&loop);
1462 __ movl(CpuRegister(TMP), Address(temp1, 0));
1463 __ movl(Address(temp2, 0), CpuRegister(TMP));
1464 __ addl(temp1, Immediate(element_size));
1465 __ addl(temp2, Immediate(element_size));
1466 __ cmpl(temp1, temp3);
1467 __ j(kNotEqual, &loop);
1468
1469 __ Bind(read_barrier_slow_path->GetExitLabel());
1470 __ Bind(&done);
1471 } else {
1472 // Non read barrier code.
1473
1474 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1475 // poison/unpoison.
1476 NearLabel loop, done;
1477 __ cmpl(temp1, temp3);
1478 __ j(kEqual, &done);
1479 __ Bind(&loop);
1480 __ movl(CpuRegister(TMP), Address(temp1, 0));
1481 __ movl(Address(temp2, 0), CpuRegister(TMP));
1482 __ addl(temp1, Immediate(element_size));
1483 __ addl(temp2, Immediate(element_size));
1484 __ cmpl(temp1, temp3);
1485 __ j(kNotEqual, &loop);
1486 __ Bind(&done);
1487 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001488
1489 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001490 codegen_->MarkGCCard(temp1, temp2, dest, CpuRegister(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001491
Roland Levillain0b671c02016-08-19 12:02:34 +01001492 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001493}
1494
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001495void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001496 LocationSummary* locations = new (allocator_) LocationSummary(
1497 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001498 InvokeRuntimeCallingConvention calling_convention;
1499 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1500 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1501 locations->SetOut(Location::RegisterLocation(RAX));
1502}
1503
1504void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1505 X86_64Assembler* assembler = GetAssembler();
1506 LocationSummary* locations = invoke->GetLocations();
1507
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001508 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001509 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001510
1511 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1512 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001513 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001514 codegen_->AddSlowPath(slow_path);
1515 __ j(kEqual, slow_path->GetEntryLabel());
1516
Serban Constantinescuba45db02016-07-12 22:53:02 +01001517 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001518 __ Bind(slow_path->GetExitLabel());
1519}
1520
Agi Csakif8cfb202015-08-13 17:54:54 -07001521void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001522 LocationSummary* locations =
1523 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakif8cfb202015-08-13 17:54:54 -07001524 locations->SetInAt(0, Location::RequiresRegister());
1525 locations->SetInAt(1, Location::RequiresRegister());
1526
1527 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1528 locations->AddTemp(Location::RegisterLocation(RCX));
1529 locations->AddTemp(Location::RegisterLocation(RDI));
1530
1531 // Set output, RSI needed for repe_cmpsq instruction anyways.
1532 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1533}
1534
1535void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1536 X86_64Assembler* assembler = GetAssembler();
1537 LocationSummary* locations = invoke->GetLocations();
1538
1539 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1540 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1541 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1542 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1543 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1544
Mark Mendell0c9497d2015-08-21 09:30:05 -04001545 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001546
1547 // Get offsets of count, value, and class fields within a string object.
1548 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1549 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1550 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1551
1552 // Note that the null check must have been done earlier.
1553 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1554
Vladimir Marko53b52002016-05-24 19:30:45 +01001555 StringEqualsOptimizations optimizations(invoke);
1556 if (!optimizations.GetArgumentNotNull()) {
1557 // Check if input is null, return false if it is.
1558 __ testl(arg, arg);
1559 __ j(kEqual, &return_false);
1560 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001561
Vladimir Marko53b52002016-05-24 19:30:45 +01001562 if (!optimizations.GetArgumentIsString()) {
1563 // Instanceof check for the argument by comparing class fields.
1564 // All string objects must have the same type since String cannot be subclassed.
1565 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1566 // If the argument is a string object, its class field must be equal to receiver's class field.
1567 __ movl(rcx, Address(str, class_offset));
1568 __ cmpl(rcx, Address(arg, class_offset));
1569 __ j(kNotEqual, &return_false);
1570 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001571
1572 // Reference equality check, return true if same reference.
1573 __ cmpl(str, arg);
1574 __ j(kEqual, &return_true);
1575
jessicahandojo4877b792016-09-08 19:49:13 -07001576 // Load length and compression flag of receiver string.
Agi Csakif8cfb202015-08-13 17:54:54 -07001577 __ movl(rcx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001578 // Check if lengths and compressiond flags are equal, return false if they're not.
1579 // Two identical strings will always have same compression style since
1580 // compression style is decided on alloc.
Agi Csakif8cfb202015-08-13 17:54:54 -07001581 __ cmpl(rcx, Address(arg, count_offset));
1582 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001583 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1584 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1585 "Expecting 0=compressed, 1=uncompressed");
1586 __ jrcxz(&return_true);
jessicahandojo4877b792016-09-08 19:49:13 -07001587
1588 if (mirror::kUseStringCompression) {
1589 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001590 // Extract length and differentiate between both compressed or both uncompressed.
1591 // Different compression style is cut above.
1592 __ shrl(rcx, Immediate(1));
1593 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001594 // Divide string length by 2, rounding up, and continue as if uncompressed.
1595 // Merge clearing the compression flag with +1 for rounding.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001596 __ addl(rcx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001597 __ shrl(rcx, Immediate(1));
1598 __ Bind(&string_uncompressed);
1599 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001600 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1601 __ leal(rsi, Address(str, value_offset));
1602 __ leal(rdi, Address(arg, value_offset));
1603
1604 // Divide string length by 4 and adjust for lengths not divisible by 4.
1605 __ addl(rcx, Immediate(3));
1606 __ shrl(rcx, Immediate(2));
1607
jessicahandojo4877b792016-09-08 19:49:13 -07001608 // Assertions that must hold in order to compare strings 4 characters (uncompressed)
1609 // or 8 characters (compressed) at a time.
Agi Csakif8cfb202015-08-13 17:54:54 -07001610 DCHECK_ALIGNED(value_offset, 8);
1611 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1612
1613 // Loop to compare strings four characters at a time starting at the beginning of the string.
1614 __ repe_cmpsq();
1615 // If strings are not equal, zero flag will be cleared.
1616 __ j(kNotEqual, &return_false);
1617
1618 // Return true and exit the function.
1619 // If loop does not result in returning false, we return true.
1620 __ Bind(&return_true);
1621 __ movl(rsi, Immediate(1));
1622 __ jmp(&end);
1623
1624 // Return false and exit the function.
1625 __ Bind(&return_false);
1626 __ xorl(rsi, rsi);
1627 __ Bind(&end);
1628}
1629
Andreas Gampe21030dd2015-05-07 14:46:15 -07001630static void CreateStringIndexOfLocations(HInvoke* invoke,
1631 ArenaAllocator* allocator,
1632 bool start_at_zero) {
1633 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1634 LocationSummary::kCallOnSlowPath,
1635 kIntrinsified);
1636 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1637 locations->SetInAt(0, Location::RegisterLocation(RDI));
1638 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1639 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1640 // of the instruction explicitly.
1641 // Note: This works as we don't clobber RAX anywhere.
1642 locations->SetInAt(1, Location::RegisterLocation(RAX));
1643 if (!start_at_zero) {
1644 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1645 }
1646 // As we clobber RDI during execution anyways, also use it as the output.
1647 locations->SetOut(Location::SameAsFirstInput());
1648
1649 // repne scasw uses RCX as the counter.
1650 locations->AddTemp(Location::RegisterLocation(RCX));
1651 // Need another temporary to be able to compute the result.
1652 locations->AddTemp(Location::RequiresRegister());
1653}
1654
1655static void GenerateStringIndexOf(HInvoke* invoke,
1656 X86_64Assembler* assembler,
1657 CodeGeneratorX86_64* codegen,
1658 ArenaAllocator* allocator,
1659 bool start_at_zero) {
1660 LocationSummary* locations = invoke->GetLocations();
1661
1662 // Note that the null check must have been done earlier.
1663 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1664
1665 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1666 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1667 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1668 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1669 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1670
1671 // Check our assumptions for registers.
1672 DCHECK_EQ(string_obj.AsRegister(), RDI);
1673 DCHECK_EQ(search_value.AsRegister(), RAX);
1674 DCHECK_EQ(counter.AsRegister(), RCX);
1675 DCHECK_EQ(out.AsRegister(), RDI);
1676
1677 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001678 // 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 -07001679 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001680 HInstruction* code_point = invoke->InputAt(1);
1681 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001682 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001683 std::numeric_limits<uint16_t>::max()) {
1684 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1685 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1686 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1687 codegen->AddSlowPath(slow_path);
1688 __ jmp(slow_path->GetEntryLabel());
1689 __ Bind(slow_path->GetExitLabel());
1690 return;
1691 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001693 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1694 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1695 codegen->AddSlowPath(slow_path);
1696 __ j(kAbove, slow_path->GetEntryLabel());
1697 }
1698
jessicahandojo4877b792016-09-08 19:49:13 -07001699 // From here down, we know that we are looking for a char that fits in
1700 // 16 bits (uncompressed) or 8 bits (compressed).
Andreas Gampe21030dd2015-05-07 14:46:15 -07001701 // Location of reference to data array within the String object.
1702 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1703 // Location of count within the String object.
1704 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1705
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001706 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001707 __ movl(string_length, Address(string_obj, count_offset));
1708
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001709 // Do a zero-length check. Even with string compression `count == 0` means empty.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001710 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001711 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001712 __ testl(string_length, string_length);
1713 __ j(kEqual, &not_found_label);
1714
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001715 if (mirror::kUseStringCompression) {
1716 // Use TMP to keep string_length_flagged.
1717 __ movl(CpuRegister(TMP), string_length);
1718 // Mask out first bit used as compression flag.
1719 __ shrl(string_length, Immediate(1));
1720 }
1721
Andreas Gampe21030dd2015-05-07 14:46:15 -07001722 if (start_at_zero) {
1723 // Number of chars to scan is the same as the string length.
1724 __ movl(counter, string_length);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001725 // Move to the start of the string.
1726 __ addq(string_obj, Immediate(value_offset));
1727 } else {
1728 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1729
1730 // Do a start_index check.
1731 __ cmpl(start_index, string_length);
1732 __ j(kGreaterEqual, &not_found_label);
1733
1734 // Ensure we have a start index >= 0;
1735 __ xorl(counter, counter);
1736 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001737 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001738
jessicahandojo4877b792016-09-08 19:49:13 -07001739 if (mirror::kUseStringCompression) {
1740 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001741 __ testl(CpuRegister(TMP), Immediate(1));
1742 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001743 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1744 __ jmp(&modify_counter);
1745 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1746 __ Bind(&offset_uncompressed_label);
1747 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1748 __ Bind(&modify_counter);
1749 } else {
1750 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1751 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001752 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1753 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1754 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1755 }
1756
jessicahandojo4877b792016-09-08 19:49:13 -07001757 if (mirror::kUseStringCompression) {
1758 NearLabel uncompressed_string_comparison;
1759 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001760 __ testl(CpuRegister(TMP), Immediate(1));
1761 __ j(kNotZero, &uncompressed_string_comparison);
jessicahandojo4877b792016-09-08 19:49:13 -07001762 // Check if RAX (search_value) is ASCII.
1763 __ cmpl(search_value, Immediate(127));
1764 __ j(kGreater, &not_found_label);
1765 // Comparing byte-per-byte.
1766 __ repne_scasb();
1767 __ jmp(&comparison_done);
1768 // Everything is set up for repne scasw:
1769 // * Comparison address in RDI.
1770 // * Counter in ECX.
1771 __ Bind(&uncompressed_string_comparison);
1772 __ repne_scasw();
1773 __ Bind(&comparison_done);
1774 } else {
1775 __ repne_scasw();
1776 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001777 // Did we find a match?
1778 __ j(kNotEqual, &not_found_label);
1779
1780 // Yes, we matched. Compute the index of the result.
1781 __ subl(string_length, counter);
1782 __ leal(out, Address(string_length, -1));
1783
Mark Mendell0c9497d2015-08-21 09:30:05 -04001784 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001785 __ jmp(&done);
1786
1787 // Failed to match; return -1.
1788 __ Bind(&not_found_label);
1789 __ movl(out, Immediate(-1));
1790
1791 // And join up at the end.
1792 __ Bind(&done);
1793 if (slow_path != nullptr) {
1794 __ Bind(slow_path->GetExitLabel());
1795 }
1796}
1797
1798void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001799 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001800}
1801
1802void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001803 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001804}
1805
1806void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001807 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001808}
1809
1810void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001811 GenerateStringIndexOf(
1812 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001813}
1814
Jeff Hao848f70a2014-01-15 13:49:50 -08001815void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001816 LocationSummary* locations = new (allocator_) LocationSummary(
1817 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001818 InvokeRuntimeCallingConvention calling_convention;
1819 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1820 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1821 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1822 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1823 locations->SetOut(Location::RegisterLocation(RAX));
1824}
1825
1826void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1827 X86_64Assembler* assembler = GetAssembler();
1828 LocationSummary* locations = invoke->GetLocations();
1829
1830 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1831 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001832 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001833 codegen_->AddSlowPath(slow_path);
1834 __ j(kEqual, slow_path->GetEntryLabel());
1835
Serban Constantinescuba45db02016-07-12 22:53:02 +01001836 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001837 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001838 __ Bind(slow_path->GetExitLabel());
1839}
1840
1841void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001842 LocationSummary* locations =
1843 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001844 InvokeRuntimeCallingConvention calling_convention;
1845 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1846 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1847 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1848 locations->SetOut(Location::RegisterLocation(RAX));
1849}
1850
1851void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001852 // No need to emit code checking whether `locations->InAt(2)` is a null
1853 // pointer, as callers of the native method
1854 //
1855 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1856 //
1857 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001858 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001859 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001860}
1861
1862void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001863 LocationSummary* locations = new (allocator_) LocationSummary(
1864 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001865 InvokeRuntimeCallingConvention calling_convention;
1866 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1867 locations->SetOut(Location::RegisterLocation(RAX));
1868}
1869
1870void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1871 X86_64Assembler* assembler = GetAssembler();
1872 LocationSummary* locations = invoke->GetLocations();
1873
1874 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1875 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001876 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001877 codegen_->AddSlowPath(slow_path);
1878 __ j(kEqual, slow_path->GetEntryLabel());
1879
Serban Constantinescuba45db02016-07-12 22:53:02 +01001880 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001881 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001882 __ Bind(slow_path->GetExitLabel());
1883}
1884
Mark Mendell8f8926a2015-08-17 11:39:06 -04001885void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1886 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001887 LocationSummary* locations =
1888 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001889 locations->SetInAt(0, Location::RequiresRegister());
1890 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1891 locations->SetInAt(2, Location::RequiresRegister());
1892 locations->SetInAt(3, Location::RequiresRegister());
1893 locations->SetInAt(4, Location::RequiresRegister());
1894
1895 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1896 locations->AddTemp(Location::RegisterLocation(RSI));
1897 locations->AddTemp(Location::RegisterLocation(RDI));
1898 locations->AddTemp(Location::RegisterLocation(RCX));
1899}
1900
1901void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1902 X86_64Assembler* assembler = GetAssembler();
1903 LocationSummary* locations = invoke->GetLocations();
1904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001905 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001906 // Location of data in char array buffer.
1907 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1908 // Location of char array data in string.
1909 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1910
1911 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1912 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1913 Location srcBegin = locations->InAt(1);
1914 int srcBegin_value =
1915 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1916 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1917 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1918 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1919
1920 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001921 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001922 DCHECK_EQ(char_size, 2u);
1923
jessicahandojo4877b792016-09-08 19:49:13 -07001924 NearLabel done;
Mark Mendell8f8926a2015-08-17 11:39:06 -04001925 // Compute the number of chars (words) to move.
1926 __ movl(CpuRegister(RCX), srcEnd);
1927 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001928 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001929 } else {
1930 DCHECK(srcBegin.IsRegister());
1931 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1932 }
jessicahandojo4877b792016-09-08 19:49:13 -07001933 if (mirror::kUseStringCompression) {
1934 NearLabel copy_uncompressed, copy_loop;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001936 DCHECK_EQ(c_char_size, 1u);
1937 // Location of count in string.
1938 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Mark Mendell8f8926a2015-08-17 11:39:06 -04001939
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001940 __ testl(Address(obj, count_offset), Immediate(1));
1941 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1942 "Expecting 0=compressed, 1=uncompressed");
1943 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001944 // Compute the address of the source string by adding the number of chars from
1945 // the source beginning to the value offset of a string.
1946 __ leaq(CpuRegister(RSI),
1947 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1948 // Start the loop to copy String's value to Array of Char.
1949 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1950
1951 __ Bind(&copy_loop);
1952 __ jrcxz(&done);
1953 // Use TMP as temporary (convert byte from RSI to word).
1954 // TODO: Selecting RAX as the temporary and using LODSB/STOSW.
1955 __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
1956 __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
1957 __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
1958 __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
1959 // TODO: Add support for LOOP to X86_64Assembler.
1960 __ subl(CpuRegister(RCX), Immediate(1));
1961 __ jmp(&copy_loop);
1962
1963 __ Bind(&copy_uncompressed);
1964 }
1965
1966 __ leaq(CpuRegister(RSI),
1967 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
1968 // Compute the address of the destination buffer.
1969 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001970 // Do the move.
1971 __ rep_movsw();
jessicahandojo4877b792016-09-08 19:49:13 -07001972
1973 __ Bind(&done);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001974}
1975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001976static void GenPeek(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001977 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1978 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1979 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1980 // to avoid a SIGBUS.
1981 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982 case DataType::Type::kInt8:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001983 __ movsxb(out, Address(address, 0));
1984 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001985 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001986 __ movsxw(out, Address(address, 0));
1987 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001989 __ movl(out, Address(address, 0));
1990 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001992 __ movq(out, Address(address, 0));
1993 break;
1994 default:
1995 LOG(FATAL) << "Type not recognized for peek: " << size;
1996 UNREACHABLE();
1997 }
1998}
1999
2000void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002001 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002002}
2003
2004void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002006}
2007
2008void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002009 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002010}
2011
2012void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002014}
2015
2016void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002017 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002018}
2019
2020void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002021 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002022}
2023
2024void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002025 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002026}
2027
2028void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002029 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002030}
2031
Vladimir Markoca6fff82017-10-03 14:49:14 +01002032static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2033 LocationSummary* locations =
2034 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002035 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002036 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002037}
2038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039static void GenPoke(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002040 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04002041 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002042 // x86 allows unaligned access. We do not have to check the input or use specific instructions
2043 // to avoid a SIGBUS.
2044 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045 case DataType::Type::kInt8:
Mark Mendell40741f32015-04-20 22:10:34 -04002046 if (value.IsConstant()) {
2047 __ movb(Address(address, 0),
2048 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2049 } else {
2050 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
2051 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002052 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002053 case DataType::Type::kInt16:
Mark Mendell40741f32015-04-20 22:10:34 -04002054 if (value.IsConstant()) {
2055 __ movw(Address(address, 0),
2056 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2057 } else {
2058 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
2059 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002060 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002061 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002062 if (value.IsConstant()) {
2063 __ movl(Address(address, 0),
2064 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2065 } else {
2066 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
2067 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002068 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002070 if (value.IsConstant()) {
2071 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
2072 DCHECK(IsInt<32>(v));
2073 int32_t v_32 = v;
2074 __ movq(Address(address, 0), Immediate(v_32));
2075 } else {
2076 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
2077 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002078 break;
2079 default:
2080 LOG(FATAL) << "Type not recognized for poke: " << size;
2081 UNREACHABLE();
2082 }
2083}
2084
2085void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002086 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002087}
2088
2089void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002091}
2092
2093void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002094 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002095}
2096
2097void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002099}
2100
2101void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002102 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002103}
2104
2105void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002107}
2108
2109void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002110 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002111}
2112
2113void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002114 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002115}
2116
2117void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002118 LocationSummary* locations =
2119 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002120 locations->SetOut(Location::RequiresRegister());
2121}
2122
2123void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2124 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07002125 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002126 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002127}
2128
Roland Levillain0d5a2812015-11-13 10:07:31 +00002129static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002130 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00002131 bool is_volatile ATTRIBUTE_UNUSED,
2132 CodeGeneratorX86_64* codegen) {
2133 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
2134 LocationSummary* locations = invoke->GetLocations();
2135 Location base_loc = locations->InAt(1);
2136 CpuRegister base = base_loc.AsRegister<CpuRegister>();
2137 Location offset_loc = locations->InAt(2);
2138 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
2139 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002140 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002141
Andreas Gampe878d58c2015-01-15 23:24:00 -08002142 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002143 case DataType::Type::kInt32:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002144 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002145 break;
2146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002148 if (kEmitCompilerReadBarrier) {
2149 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002150 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2151 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002152 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002153 } else {
2154 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2155 codegen->GenerateReadBarrierSlow(
2156 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2157 }
2158 } else {
2159 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2160 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002161 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002162 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002163 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002165 case DataType::Type::kInt64:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002166 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002167 break;
2168
2169 default:
2170 LOG(FATAL) << "Unsupported op size " << type;
2171 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002172 }
2173}
2174
Vladimir Markoca6fff82017-10-03 14:49:14 +01002175static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002176 bool can_call = kEmitCompilerReadBarrier &&
2177 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2178 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002179 LocationSummary* locations =
2180 new (allocator) LocationSummary(invoke,
2181 can_call
2182 ? LocationSummary::kCallOnSlowPath
2183 : LocationSummary::kNoCall,
2184 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002185 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002186 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002187 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002188 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002189 locations->SetInAt(1, Location::RequiresRegister());
2190 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002191 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002192 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002193}
2194
2195void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002196 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002197}
2198void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002199 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002200}
2201void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002202 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002203}
2204void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002205 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002206}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002207void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002208 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002209}
2210void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002211 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002212}
2213
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002214
2215void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002216 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002217}
2218void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002219 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002220}
2221void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002223}
2224void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002225 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002226}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002227void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002228 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002229}
2230void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002232}
2233
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002234
Vladimir Markoca6fff82017-10-03 14:49:14 +01002235static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002236 DataType::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002237 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002238 LocationSummary* locations =
2239 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002240 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002241 locations->SetInAt(1, Location::RequiresRegister());
2242 locations->SetInAt(2, Location::RequiresRegister());
2243 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002244 if (type == DataType::Type::kReference) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002245 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002246 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002247 locations->AddTemp(Location::RequiresRegister());
2248 }
2249}
2250
2251void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002252 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002253}
2254void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002255 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002256}
2257void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002258 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002259}
2260void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002261 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002262}
2263void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002264 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002265}
2266void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002267 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002268}
2269void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002271}
2272void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002273 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002274}
2275void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002276 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002277}
2278
2279// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2280// memory model.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281static void GenUnsafePut(LocationSummary* locations, DataType::Type type, bool is_volatile,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002282 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002283 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002284 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2285 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2286 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 if (type == DataType::Type::kInt64) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002289 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002290 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002291 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2292 __ movl(temp, value);
2293 __ PoisonHeapReference(temp);
2294 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002295 } else {
2296 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2297 }
2298
2299 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002300 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002301 }
2302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002304 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002305 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2306 locations->GetTemp(1).AsRegister<CpuRegister>(),
2307 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002308 value,
2309 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002310 }
2311}
2312
2313void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002314 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002315}
2316void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002317 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002318}
2319void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002321}
2322void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002323 GenUnsafePut(
2324 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002325}
2326void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002327 GenUnsafePut(
2328 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002329}
2330void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002331 GenUnsafePut(
2332 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002333}
2334void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002335 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002336}
2337void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002339}
2340void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002342}
2343
Vladimir Markoca6fff82017-10-03 14:49:14 +01002344static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002346 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002347 bool can_call = kEmitCompilerReadBarrier &&
2348 kUseBakerReadBarrier &&
2349 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002350 LocationSummary* locations =
2351 new (allocator) LocationSummary(invoke,
2352 can_call
2353 ? LocationSummary::kCallOnSlowPath
2354 : LocationSummary::kNoCall,
2355 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002356 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2357 locations->SetInAt(1, Location::RequiresRegister());
2358 locations->SetInAt(2, Location::RequiresRegister());
2359 // expected value must be in EAX/RAX.
2360 locations->SetInAt(3, Location::RegisterLocation(RAX));
2361 locations->SetInAt(4, Location::RequiresRegister());
2362
2363 locations->SetOut(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002364 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002365 // Need temporary registers for card-marking, and possibly for
2366 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002367 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002368 locations->AddTemp(Location::RequiresRegister());
2369 }
2370}
2371
2372void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002373 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002374}
2375
2376void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002377 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002378}
2379
2380void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002381 // The only read barrier implementation supporting the
2382 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2383 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002384 return;
2385 }
2386
Vladimir Markoca6fff82017-10-03 14:49:14 +01002387 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002388}
2389
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002390static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002391 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002392 LocationSummary* locations = invoke->GetLocations();
2393
2394 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2395 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2396 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002397 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002398 DCHECK_EQ(expected.AsRegister(), RAX);
2399 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002400 Location out_loc = locations->Out();
2401 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002402
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002404 // The only read barrier implementation supporting the
2405 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2406 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2407
2408 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2409 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2410
Roland Levillainb488b782015-10-22 11:38:49 +01002411 // Mark card for object assuming new value is stored.
2412 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002413 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2414
2415 // The address of the field within the holding object.
2416 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2417
2418 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2419 // Need to make sure the reference stored in the field is a to-space
2420 // one before attempting the CAS or the CAS could fail incorrectly.
2421 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2422 invoke,
2423 out_loc, // Unused, used only as a "temporary" within the read barrier.
2424 base,
2425 field_addr,
2426 /* needs_null_check */ false,
2427 /* always_update_field */ true,
2428 &temp1,
2429 &temp2);
2430 }
Roland Levillain4d027112015-07-01 15:41:14 +01002431
Roland Levillainb488b782015-10-22 11:38:49 +01002432 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2433 Register value_reg = value.AsRegister();
2434 if (kPoisonHeapReferences) {
2435 if (base_equals_value) {
2436 // If `base` and `value` are the same register location, move
2437 // `value_reg` to a temporary register. This way, poisoning
2438 // `value_reg` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002439 value_reg = temp1.AsRegister();
Roland Levillainb488b782015-10-22 11:38:49 +01002440 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002441 }
Roland Levillainb488b782015-10-22 11:38:49 +01002442
2443 // Check that the register allocator did not assign the location
2444 // of `expected` (RAX) to `value` nor to `base`, so that heap
2445 // poisoning (when enabled) works as intended below.
2446 // - If `value` were equal to `expected`, both references would
2447 // be poisoned twice, meaning they would not be poisoned at
2448 // all, as heap poisoning uses address negation.
2449 // - If `base` were equal to `expected`, poisoning `expected`
2450 // would invalidate `base`.
2451 DCHECK_NE(value_reg, expected.AsRegister());
2452 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2453
2454 __ PoisonHeapReference(expected);
2455 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002456 }
2457
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002458 __ LockCmpxchgl(field_addr, CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002459
Roland Levillain0d5a2812015-11-13 10:07:31 +00002460 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002461 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002462
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002463 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002464 __ setcc(kZero, out);
2465 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002466
Roland Levillain391b8662015-12-18 11:43:38 +00002467 // If heap poisoning is enabled, we need to unpoison the values
2468 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002469 if (kPoisonHeapReferences) {
2470 if (base_equals_value) {
2471 // `value_reg` has been moved to a temporary register, no need
2472 // to unpoison it.
2473 } else {
2474 // Ensure `value` is different from `out`, so that unpoisoning
2475 // the former does not invalidate the latter.
2476 DCHECK_NE(value_reg, out.AsRegister());
2477 __ UnpoisonHeapReference(CpuRegister(value_reg));
2478 }
2479 // Ensure `expected` is different from `out`, so that unpoisoning
2480 // the former does not invalidate the latter.
2481 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2482 __ UnpoisonHeapReference(expected);
2483 }
2484 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002485 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002486 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002488 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2489 } else {
2490 LOG(FATAL) << "Unexpected CAS type " << type;
2491 }
2492
Roland Levillain0d5a2812015-11-13 10:07:31 +00002493 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002494 // scheduling barriers at this time.
2495
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002496 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002497 __ setcc(kZero, out);
2498 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002499 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002500}
2501
2502void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002503 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002504}
2505
2506void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002507 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002508}
2509
2510void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002511 // The only read barrier implementation supporting the
2512 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2513 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002516}
2517
2518void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002519 LocationSummary* locations =
2520 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002521 locations->SetInAt(0, Location::RequiresRegister());
2522 locations->SetOut(Location::SameAsFirstInput());
2523 locations->AddTemp(Location::RequiresRegister());
2524}
2525
2526static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2527 X86_64Assembler* assembler) {
2528 Immediate imm_shift(shift);
2529 Immediate imm_mask(mask);
2530 __ movl(temp, reg);
2531 __ shrl(reg, imm_shift);
2532 __ andl(temp, imm_mask);
2533 __ andl(reg, imm_mask);
2534 __ shll(temp, imm_shift);
2535 __ orl(reg, temp);
2536}
2537
2538void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002539 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002540 LocationSummary* locations = invoke->GetLocations();
2541
2542 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2543 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2544
2545 /*
2546 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2547 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2548 * compared to generic luni implementation which has 5 rounds of swapping bits.
2549 * x = bswap x
2550 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2551 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2552 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2553 */
2554 __ bswapl(reg);
2555 SwapBits(reg, temp, 1, 0x55555555, assembler);
2556 SwapBits(reg, temp, 2, 0x33333333, assembler);
2557 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2558}
2559
2560void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002561 LocationSummary* locations =
2562 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002563 locations->SetInAt(0, Location::RequiresRegister());
2564 locations->SetOut(Location::SameAsFirstInput());
2565 locations->AddTemp(Location::RequiresRegister());
2566 locations->AddTemp(Location::RequiresRegister());
2567}
2568
2569static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2570 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2571 Immediate imm_shift(shift);
2572 __ movq(temp_mask, Immediate(mask));
2573 __ movq(temp, reg);
2574 __ shrq(reg, imm_shift);
2575 __ andq(temp, temp_mask);
2576 __ andq(reg, temp_mask);
2577 __ shlq(temp, imm_shift);
2578 __ orq(reg, temp);
2579}
2580
2581void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002582 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002583 LocationSummary* locations = invoke->GetLocations();
2584
2585 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2586 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2587 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2588
2589 /*
2590 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2591 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2592 * compared to generic luni implementation which has 5 rounds of swapping bits.
2593 * x = bswap x
2594 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2595 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2596 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2597 */
2598 __ bswapq(reg);
2599 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2600 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2601 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2602}
2603
Aart Bik3f67e692016-01-15 14:35:12 -08002604static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002605 ArenaAllocator* allocator, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Aart Bik3f67e692016-01-15 14:35:12 -08002606 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2607 // Do nothing if there is no popcnt support. This results in generating
2608 // a call for the intrinsic rather than direct code.
2609 return;
2610 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002611 LocationSummary* locations =
2612 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bik3f67e692016-01-15 14:35:12 -08002613 locations->SetInAt(0, Location::Any());
2614 locations->SetOut(Location::RequiresRegister());
2615}
2616
Aart Bikc5d47542016-01-27 17:00:35 -08002617static void GenBitCount(X86_64Assembler* assembler,
2618 CodeGeneratorX86_64* codegen,
2619 HInvoke* invoke,
2620 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002621 LocationSummary* locations = invoke->GetLocations();
2622 Location src = locations->InAt(0);
2623 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2624
2625 if (invoke->InputAt(0)->IsConstant()) {
2626 // Evaluate this at compile time.
2627 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002628 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002629 ? POPCOUNT(static_cast<uint64_t>(value))
2630 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002631 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002632 return;
2633 }
2634
2635 if (src.IsRegister()) {
2636 if (is_long) {
2637 __ popcntq(out, src.AsRegister<CpuRegister>());
2638 } else {
2639 __ popcntl(out, src.AsRegister<CpuRegister>());
2640 }
2641 } else if (is_long) {
2642 DCHECK(src.IsDoubleStackSlot());
2643 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2644 } else {
2645 DCHECK(src.IsStackSlot());
2646 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2647 }
2648}
2649
2650void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002651 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002652}
2653
2654void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002655 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002656}
2657
2658void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002659 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002660}
2661
2662void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002663 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2664}
2665
Vladimir Markoca6fff82017-10-03 14:49:14 +01002666static void CreateOneBitLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_high) {
2667 LocationSummary* locations =
2668 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc5d47542016-01-27 17:00:35 -08002669 locations->SetInAt(0, Location::Any());
2670 locations->SetOut(Location::RequiresRegister());
2671 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2672 : Location::RequiresRegister()); // any will do
2673}
2674
2675static void GenOneBit(X86_64Assembler* assembler,
2676 CodeGeneratorX86_64* codegen,
2677 HInvoke* invoke,
2678 bool is_high, bool is_long) {
2679 LocationSummary* locations = invoke->GetLocations();
2680 Location src = locations->InAt(0);
2681 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2682
2683 if (invoke->InputAt(0)->IsConstant()) {
2684 // Evaluate this at compile time.
2685 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2686 if (value == 0) {
2687 __ xorl(out, out); // Clears upper bits too.
2688 return;
2689 }
2690 // Nonzero value.
2691 if (is_high) {
2692 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2693 : 31 - CLZ(static_cast<uint32_t>(value));
2694 } else {
2695 value = is_long ? CTZ(static_cast<uint64_t>(value))
2696 : CTZ(static_cast<uint32_t>(value));
2697 }
2698 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002699 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002700 } else {
2701 codegen->Load32BitValue(out, 1 << value);
2702 }
2703 return;
2704 }
2705
2706 // Handle the non-constant cases.
2707 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2708 if (is_high) {
2709 // Use architectural support: basically 1 << bsr.
2710 if (src.IsRegister()) {
2711 if (is_long) {
2712 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2713 } else {
2714 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2715 }
2716 } else if (is_long) {
2717 DCHECK(src.IsDoubleStackSlot());
2718 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2719 } else {
2720 DCHECK(src.IsStackSlot());
2721 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2722 }
2723 // BSR sets ZF if the input was zero.
2724 NearLabel is_zero, done;
2725 __ j(kEqual, &is_zero);
2726 __ movl(out, Immediate(1)); // Clears upper bits too.
2727 if (is_long) {
2728 __ shlq(out, tmp);
2729 } else {
2730 __ shll(out, tmp);
2731 }
2732 __ jmp(&done);
2733 __ Bind(&is_zero);
2734 __ xorl(out, out); // Clears upper bits too.
2735 __ Bind(&done);
2736 } else {
2737 // Copy input into temporary.
2738 if (src.IsRegister()) {
2739 if (is_long) {
2740 __ movq(tmp, src.AsRegister<CpuRegister>());
2741 } else {
2742 __ movl(tmp, src.AsRegister<CpuRegister>());
2743 }
2744 } else if (is_long) {
2745 DCHECK(src.IsDoubleStackSlot());
2746 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2747 } else {
2748 DCHECK(src.IsStackSlot());
2749 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2750 }
2751 // Do the bit twiddling: basically tmp & -tmp;
2752 if (is_long) {
2753 __ movq(out, tmp);
2754 __ negq(tmp);
2755 __ andq(out, tmp);
2756 } else {
2757 __ movl(out, tmp);
2758 __ negl(tmp);
2759 __ andl(out, tmp);
2760 }
2761 }
2762}
2763
2764void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002765 CreateOneBitLocations(allocator_, invoke, /* is_high */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002766}
2767
2768void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2769 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2770}
2771
2772void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002773 CreateOneBitLocations(allocator_, invoke, /* is_high */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002774}
2775
2776void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2777 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2778}
2779
2780void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002781 CreateOneBitLocations(allocator_, invoke, /* is_high */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002782}
2783
2784void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2785 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2786}
2787
2788void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002789 CreateOneBitLocations(allocator_, invoke, /* is_high */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002790}
2791
2792void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2793 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002794}
2795
Vladimir Markoca6fff82017-10-03 14:49:14 +01002796static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2797 LocationSummary* locations =
2798 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002799 locations->SetInAt(0, Location::Any());
2800 locations->SetOut(Location::RequiresRegister());
2801}
2802
Aart Bikc5d47542016-01-27 17:00:35 -08002803static void GenLeadingZeros(X86_64Assembler* assembler,
2804 CodeGeneratorX86_64* codegen,
2805 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002806 LocationSummary* locations = invoke->GetLocations();
2807 Location src = locations->InAt(0);
2808 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2809
2810 int zero_value_result = is_long ? 64 : 32;
2811 if (invoke->InputAt(0)->IsConstant()) {
2812 // Evaluate this at compile time.
2813 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2814 if (value == 0) {
2815 value = zero_value_result;
2816 } else {
2817 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2818 }
Aart Bikc5d47542016-01-27 17:00:35 -08002819 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002820 return;
2821 }
2822
2823 // Handle the non-constant cases.
2824 if (src.IsRegister()) {
2825 if (is_long) {
2826 __ bsrq(out, src.AsRegister<CpuRegister>());
2827 } else {
2828 __ bsrl(out, src.AsRegister<CpuRegister>());
2829 }
2830 } else if (is_long) {
2831 DCHECK(src.IsDoubleStackSlot());
2832 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2833 } else {
2834 DCHECK(src.IsStackSlot());
2835 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2836 }
2837
2838 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002839 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002840 __ j(kEqual, &is_zero);
2841
2842 // Correct the result from BSR to get the CLZ result.
2843 __ xorl(out, Immediate(zero_value_result - 1));
2844 __ jmp(&done);
2845
2846 // Fix the zero case with the expected result.
2847 __ Bind(&is_zero);
2848 __ movl(out, Immediate(zero_value_result));
2849
2850 __ Bind(&done);
2851}
2852
2853void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002854 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002855}
2856
2857void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002858 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002859}
2860
2861void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002862 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002863}
2864
2865void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002866 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002867}
2868
Vladimir Markoca6fff82017-10-03 14:49:14 +01002869static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2870 LocationSummary* locations =
2871 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002872 locations->SetInAt(0, Location::Any());
2873 locations->SetOut(Location::RequiresRegister());
2874}
2875
Aart Bikc5d47542016-01-27 17:00:35 -08002876static void GenTrailingZeros(X86_64Assembler* assembler,
2877 CodeGeneratorX86_64* codegen,
2878 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002879 LocationSummary* locations = invoke->GetLocations();
2880 Location src = locations->InAt(0);
2881 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2882
2883 int zero_value_result = is_long ? 64 : 32;
2884 if (invoke->InputAt(0)->IsConstant()) {
2885 // Evaluate this at compile time.
2886 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2887 if (value == 0) {
2888 value = zero_value_result;
2889 } else {
2890 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2891 }
Aart Bikc5d47542016-01-27 17:00:35 -08002892 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002893 return;
2894 }
2895
2896 // Handle the non-constant cases.
2897 if (src.IsRegister()) {
2898 if (is_long) {
2899 __ bsfq(out, src.AsRegister<CpuRegister>());
2900 } else {
2901 __ bsfl(out, src.AsRegister<CpuRegister>());
2902 }
2903 } else if (is_long) {
2904 DCHECK(src.IsDoubleStackSlot());
2905 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2906 } else {
2907 DCHECK(src.IsStackSlot());
2908 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2909 }
2910
2911 // BSF sets ZF if the input was zero, and the output is undefined.
2912 NearLabel done;
2913 __ j(kNotEqual, &done);
2914
2915 // Fix the zero case with the expected result.
2916 __ movl(out, Immediate(zero_value_result));
2917
2918 __ Bind(&done);
2919}
2920
2921void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002922 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002923}
2924
2925void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002926 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002927}
2928
2929void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002930 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002931}
2932
2933void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002934 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2935}
2936
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002937void IntrinsicLocationsBuilderX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2938 InvokeRuntimeCallingConvention calling_convention;
2939 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2940 invoke,
2941 codegen_,
2942 Location::RegisterLocation(RAX),
2943 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2944}
2945
2946void IntrinsicCodeGeneratorX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2947 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2948 LocationSummary* locations = invoke->GetLocations();
2949 X86_64Assembler* assembler = GetAssembler();
2950
2951 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2952 InvokeRuntimeCallingConvention calling_convention;
2953 if (invoke->InputAt(0)->IsConstant()) {
2954 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2955 if (value >= info.low && value <= info.high) {
2956 // Just embed the j.l.Integer in the code.
2957 ScopedObjectAccess soa(Thread::Current());
2958 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2959 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2960 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
Colin Cross0bd97172017-03-15 16:33:27 -07002961 __ movl(out, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002962 } else {
2963 // Allocate and initialize a new j.l.Integer.
2964 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2965 // JIT object table.
Colin Cross0bd97172017-03-15 16:33:27 -07002966 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002967 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07002968 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002969 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2970 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2971 __ movl(Address(out, info.value_offset), Immediate(value));
2972 }
2973 } else {
2974 CpuRegister in = locations->InAt(0).AsRegister<CpuRegister>();
2975 // Check bounds of our cache.
2976 __ leal(out, Address(in, -info.low));
2977 __ cmpl(out, Immediate(info.high - info.low + 1));
2978 NearLabel allocate, done;
2979 __ j(kAboveEqual, &allocate);
2980 // If the value is within the bounds, load the j.l.Integer directly from the array.
2981 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2982 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
Colin Cross0bd97172017-03-15 16:33:27 -07002983 if (data_offset + address <= std::numeric_limits<int32_t>::max()) {
2984 __ movl(out, Address(out, TIMES_4, data_offset + address));
2985 } else {
2986 CpuRegister temp = CpuRegister(calling_convention.GetRegisterAt(0));
2987 __ movl(temp, Immediate(static_cast<int32_t>(data_offset + address)));
2988 __ movl(out, Address(temp, out, TIMES_4, 0));
2989 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002990 __ MaybeUnpoisonHeapReference(out);
2991 __ jmp(&done);
2992 __ Bind(&allocate);
2993 // Otherwise allocate and initialize a new j.l.Integer.
Colin Cross0bd97172017-03-15 16:33:27 -07002994 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002995 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07002996 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002997 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2998 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2999 __ movl(Address(out, info.value_offset), in);
3000 __ Bind(&done);
3001 }
3002}
3003
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003004void IntrinsicLocationsBuilderX86_64::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003005 LocationSummary* locations =
3006 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003007 locations->SetOut(Location::RequiresRegister());
3008}
3009
3010void IntrinsicCodeGeneratorX86_64::VisitThreadInterrupted(HInvoke* invoke) {
3011 X86_64Assembler* assembler = GetAssembler();
3012 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
3013 Address address = Address::Absolute
3014 (Thread::InterruptedOffset<kX86_64PointerSize>().Int32Value(), /* no_rip */ true);
3015 NearLabel done;
3016 __ gs()->movl(out, address);
3017 __ testl(out, out);
3018 __ j(kEqual, &done);
3019 __ gs()->movl(address, Immediate(0));
3020 codegen_->MemoryFence();
3021 __ Bind(&done);
3022}
3023
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003024UNIMPLEMENTED_INTRINSIC(X86_64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003025UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
3026UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003027
Aart Bikff7d89c2016-11-07 08:49:28 -08003028UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOf);
3029UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003030UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferAppend);
3031UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferLength);
3032UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferToString);
3033UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppend);
3034UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderLength);
3035UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003036
Aart Bik0e54c012016-03-04 12:08:31 -08003037// 1.8.
3038UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
3039UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
3040UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
3041UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
3042UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003043
Aart Bik2f9fcc92016-03-01 15:16:54 -08003044UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01003045
3046#undef __
3047
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003048} // namespace x86_64
3049} // namespace art