blob: 6dd8b8e1f5632be231088ce17e61a26401b6fbc8 [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
Vladimir Marko174b2e22017-10-12 13:34:49 +01001036 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) 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
Vladimir Marko174b2e22017-10-12 13:34:49 +01001178 SlowPathCode* intrinsic_slow_path =
1179 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01001180 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001181
Roland Levillainebea3d22016-04-12 15:42:57 +01001182 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001183 SystemArrayCopyOptimizations optimizations(invoke);
1184
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001185 // If source and destination are the same, we go to slow path if we need to do
1186 // forward copying.
1187 if (src_pos.IsConstant()) {
1188 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1189 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001190 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1191 if (optimizations.GetDestinationIsSource()) {
1192 // Checked when building locations.
1193 DCHECK_GE(src_pos_constant, dest_pos_constant);
1194 } else if (src_pos_constant < dest_pos_constant) {
1195 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001196 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001197 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001198 } else {
1199 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001200 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001201 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001202 }
1203 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001204 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001205 }
1206 } else {
1207 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001208 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001209 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001210 }
1211 if (dest_pos.IsConstant()) {
1212 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1213 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001214 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001215 } else {
1216 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001217 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001218 }
1219 }
1220
Roland Levillainebea3d22016-04-12 15:42:57 +01001221 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001222
1223 if (!optimizations.GetSourceIsNotNull()) {
1224 // Bail out if the source is null.
1225 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01001226 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001227 }
1228
1229 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1230 // Bail out if the destination is null.
1231 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001232 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001233 }
1234
1235 // If the length is negative, bail out.
1236 // We have already checked in the LocationsBuilder for the constant case.
1237 if (!length.IsConstant() &&
1238 !optimizations.GetCountIsSourceLength() &&
1239 !optimizations.GetCountIsDestinationLength()) {
1240 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001241 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001242 }
1243
1244 // Validity checks: source.
1245 CheckPosition(assembler,
1246 src_pos,
1247 src,
1248 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001249 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001250 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001251 optimizations.GetCountIsSourceLength());
1252
1253 // Validity checks: dest.
1254 CheckPosition(assembler,
1255 dest_pos,
1256 dest,
1257 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001258 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001259 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001260 optimizations.GetCountIsDestinationLength());
1261
1262 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1263 // Check whether all elements of the source array are assignable to the component
1264 // type of the destination array. We do two checks: the classes are the same,
1265 // or the destination is Object[]. If none of these checks succeed, we go to the
1266 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01001267
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001268 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +01001269 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1270 // /* HeapReference<Class> */ temp1 = dest->klass_
1271 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001272 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001273 // Register `temp1` is not trashed by the read barrier emitted
1274 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1275 // method produces a call to a ReadBarrierMarkRegX entry point,
1276 // which saves all potentially live registers, including
1277 // temporaries such a `temp1`.
1278 // /* HeapReference<Class> */ temp2 = src->klass_
1279 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001280 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001281 // If heap poisoning is enabled, `temp1` and `temp2` have been
1282 // unpoisoned by the the previous calls to
1283 // GenerateFieldLoadWithBakerReadBarrier.
1284 } else {
1285 // /* HeapReference<Class> */ temp1 = dest->klass_
1286 __ movl(temp1, Address(dest, class_offset));
1287 // /* HeapReference<Class> */ temp2 = src->klass_
1288 __ movl(temp2, Address(src, class_offset));
1289 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1290 !optimizations.GetSourceIsNonPrimitiveArray()) {
1291 // One or two of the references need to be unpoisoned. Unpoison them
1292 // both to make the identity check valid.
1293 __ MaybeUnpoisonHeapReference(temp1);
1294 __ MaybeUnpoisonHeapReference(temp2);
1295 did_unpoison = true;
1296 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001297 }
1298
1299 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1300 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001301 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1302 // /* HeapReference<Class> */ TMP = temp1->component_type_
1303 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001304 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001305 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1306 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1307 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1308 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1309 } else {
1310 // /* HeapReference<Class> */ TMP = temp1->component_type_
1311 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1312 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1313 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1314 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1315 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001316 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001317 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001318 }
1319
1320 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1321 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001322 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1323 // For the same reason given earlier, `temp1` is not trashed by the
1324 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1325 // /* HeapReference<Class> */ TMP = temp2->component_type_
1326 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001327 invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001328 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1329 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1330 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1331 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1332 } else {
1333 // /* HeapReference<Class> */ TMP = temp2->component_type_
1334 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1335 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1336 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1337 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1338 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001339 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001340 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001341 }
1342
1343 __ cmpl(temp1, temp2);
1344
1345 if (optimizations.GetDestinationIsTypedObjectArray()) {
1346 NearLabel do_copy;
1347 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001348 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1349 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1350 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001351 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001352 // We do not need to emit a read barrier for the following
1353 // heap reference load, as `temp1` is only used in a
1354 // comparison with null below, and this reference is not
1355 // kept afterwards.
1356 __ cmpl(Address(temp1, super_offset), Immediate(0));
1357 } else {
1358 if (!did_unpoison) {
1359 __ MaybeUnpoisonHeapReference(temp1);
1360 }
1361 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1362 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001363 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001364 // No need to unpoison the following heap reference load, as
1365 // we're comparing against null.
1366 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001367 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001368 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001369 __ Bind(&do_copy);
1370 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001371 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001372 }
1373 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1374 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1375 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001376 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1377 // /* HeapReference<Class> */ temp1 = src->klass_
1378 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001379 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001380 // /* HeapReference<Class> */ TMP = temp1->component_type_
1381 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001382 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001383 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1384 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1385 } else {
1386 // /* HeapReference<Class> */ temp1 = src->klass_
1387 __ movl(temp1, Address(src, class_offset));
1388 __ MaybeUnpoisonHeapReference(temp1);
1389 // /* HeapReference<Class> */ TMP = temp1->component_type_
1390 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1391 // No need to unpoison `TMP` now, as we're comparing against null.
1392 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1393 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1394 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1395 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001396 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001397 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001398 }
1399
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001400 const DataType::Type type = DataType::Type::kReference;
1401 const int32_t element_size = DataType::Size(type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001402
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001403 // Compute base source address, base destination address, and end
1404 // source address in `temp1`, `temp2` and `temp3` respectively.
1405 GenSystemArrayCopyAddresses(
1406 GetAssembler(), type, src, src_pos, dest, dest_pos, length, temp1, temp2, temp3);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001407
Roland Levillain0b671c02016-08-19 12:02:34 +01001408 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1409 // SystemArrayCopy implementation for Baker read barriers (see
1410 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1411 //
1412 // if (src_ptr != end_ptr) {
1413 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1414 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001415 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01001416 // if (is_gray) {
1417 // // Slow-path copy.
1418 // do {
1419 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1420 // } while (src_ptr != end_ptr)
1421 // } else {
1422 // // Fast-path copy.
1423 // do {
1424 // *dest_ptr++ = *src_ptr++;
1425 // } while (src_ptr != end_ptr)
1426 // }
1427 // }
1428
1429 NearLabel loop, done;
1430
1431 // Don't enter copy loop if `length == 0`.
1432 __ cmpl(temp1, temp3);
1433 __ j(kEqual, &done);
1434
Vladimir Marko953437b2016-08-24 08:30:46 +00001435 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001436 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1437 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00001438 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
1439 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
1440 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
1441
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001442 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00001443 // goto slow_path;
1444 // At this point, just do the "if" and make sure that flags are preserved until the branch.
1445 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01001446
1447 // Load fence to prevent load-load reordering.
1448 // Note that this is a no-op, thanks to the x86-64 memory model.
1449 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1450
1451 // Slow path used to copy array when `src` is gray.
1452 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01001453 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01001454 codegen_->AddSlowPath(read_barrier_slow_path);
1455
Vladimir Marko953437b2016-08-24 08:30:46 +00001456 // We have done the "if" of the gray bit check above, now branch based on the flags.
1457 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01001458
1459 // Fast-path copy.
1460 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1461 // poison/unpoison.
1462 __ Bind(&loop);
1463 __ movl(CpuRegister(TMP), Address(temp1, 0));
1464 __ movl(Address(temp2, 0), CpuRegister(TMP));
1465 __ addl(temp1, Immediate(element_size));
1466 __ addl(temp2, Immediate(element_size));
1467 __ cmpl(temp1, temp3);
1468 __ j(kNotEqual, &loop);
1469
1470 __ Bind(read_barrier_slow_path->GetExitLabel());
1471 __ Bind(&done);
1472 } else {
1473 // Non read barrier code.
1474
1475 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1476 // poison/unpoison.
1477 NearLabel loop, done;
1478 __ cmpl(temp1, temp3);
1479 __ j(kEqual, &done);
1480 __ Bind(&loop);
1481 __ movl(CpuRegister(TMP), Address(temp1, 0));
1482 __ movl(Address(temp2, 0), CpuRegister(TMP));
1483 __ addl(temp1, Immediate(element_size));
1484 __ addl(temp2, Immediate(element_size));
1485 __ cmpl(temp1, temp3);
1486 __ j(kNotEqual, &loop);
1487 __ Bind(&done);
1488 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001489
1490 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001491 codegen_->MarkGCCard(temp1, temp2, dest, CpuRegister(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001492
Roland Levillain0b671c02016-08-19 12:02:34 +01001493 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001494}
1495
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001496void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001497 LocationSummary* locations = new (allocator_) LocationSummary(
1498 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001499 InvokeRuntimeCallingConvention calling_convention;
1500 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1501 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1502 locations->SetOut(Location::RegisterLocation(RAX));
1503}
1504
1505void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1506 X86_64Assembler* assembler = GetAssembler();
1507 LocationSummary* locations = invoke->GetLocations();
1508
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001509 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001510 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001511
1512 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1513 __ testl(argument, argument);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001514 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001515 codegen_->AddSlowPath(slow_path);
1516 __ j(kEqual, slow_path->GetEntryLabel());
1517
Serban Constantinescuba45db02016-07-12 22:53:02 +01001518 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001519 __ Bind(slow_path->GetExitLabel());
1520}
1521
Agi Csakif8cfb202015-08-13 17:54:54 -07001522void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001523 if (kEmitCompilerReadBarrier &&
1524 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1525 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1526 // No support for this odd case (String class is moveable, not in the boot image).
1527 return;
1528 }
1529
Vladimir Markoca6fff82017-10-03 14:49:14 +01001530 LocationSummary* locations =
1531 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakif8cfb202015-08-13 17:54:54 -07001532 locations->SetInAt(0, Location::RequiresRegister());
1533 locations->SetInAt(1, Location::RequiresRegister());
1534
1535 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1536 locations->AddTemp(Location::RegisterLocation(RCX));
1537 locations->AddTemp(Location::RegisterLocation(RDI));
1538
1539 // Set output, RSI needed for repe_cmpsq instruction anyways.
1540 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1541}
1542
1543void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1544 X86_64Assembler* assembler = GetAssembler();
1545 LocationSummary* locations = invoke->GetLocations();
1546
1547 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1548 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1549 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1550 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1551 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1552
Mark Mendell0c9497d2015-08-21 09:30:05 -04001553 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001554
1555 // Get offsets of count, value, and class fields within a string object.
1556 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1557 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1558 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1559
1560 // Note that the null check must have been done earlier.
1561 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1562
Vladimir Marko53b52002016-05-24 19:30:45 +01001563 StringEqualsOptimizations optimizations(invoke);
1564 if (!optimizations.GetArgumentNotNull()) {
1565 // Check if input is null, return false if it is.
1566 __ testl(arg, arg);
1567 __ j(kEqual, &return_false);
1568 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001569
Vladimir Marko53b52002016-05-24 19:30:45 +01001570 if (!optimizations.GetArgumentIsString()) {
1571 // Instanceof check for the argument by comparing class fields.
1572 // All string objects must have the same type since String cannot be subclassed.
1573 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1574 // If the argument is a string object, its class field must be equal to receiver's class field.
1575 __ movl(rcx, Address(str, class_offset));
1576 __ cmpl(rcx, Address(arg, class_offset));
1577 __ j(kNotEqual, &return_false);
1578 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001579
1580 // Reference equality check, return true if same reference.
1581 __ cmpl(str, arg);
1582 __ j(kEqual, &return_true);
1583
jessicahandojo4877b792016-09-08 19:49:13 -07001584 // Load length and compression flag of receiver string.
Agi Csakif8cfb202015-08-13 17:54:54 -07001585 __ movl(rcx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001586 // Check if lengths and compressiond flags are equal, return false if they're not.
1587 // Two identical strings will always have same compression style since
1588 // compression style is decided on alloc.
Agi Csakif8cfb202015-08-13 17:54:54 -07001589 __ cmpl(rcx, Address(arg, count_offset));
1590 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001591 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1592 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1593 "Expecting 0=compressed, 1=uncompressed");
1594 __ jrcxz(&return_true);
jessicahandojo4877b792016-09-08 19:49:13 -07001595
1596 if (mirror::kUseStringCompression) {
1597 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001598 // Extract length and differentiate between both compressed or both uncompressed.
1599 // Different compression style is cut above.
1600 __ shrl(rcx, Immediate(1));
1601 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001602 // Divide string length by 2, rounding up, and continue as if uncompressed.
1603 // Merge clearing the compression flag with +1 for rounding.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001604 __ addl(rcx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001605 __ shrl(rcx, Immediate(1));
1606 __ Bind(&string_uncompressed);
1607 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001608 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1609 __ leal(rsi, Address(str, value_offset));
1610 __ leal(rdi, Address(arg, value_offset));
1611
1612 // Divide string length by 4 and adjust for lengths not divisible by 4.
1613 __ addl(rcx, Immediate(3));
1614 __ shrl(rcx, Immediate(2));
1615
jessicahandojo4877b792016-09-08 19:49:13 -07001616 // Assertions that must hold in order to compare strings 4 characters (uncompressed)
1617 // or 8 characters (compressed) at a time.
Agi Csakif8cfb202015-08-13 17:54:54 -07001618 DCHECK_ALIGNED(value_offset, 8);
1619 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1620
1621 // Loop to compare strings four characters at a time starting at the beginning of the string.
1622 __ repe_cmpsq();
1623 // If strings are not equal, zero flag will be cleared.
1624 __ j(kNotEqual, &return_false);
1625
1626 // Return true and exit the function.
1627 // If loop does not result in returning false, we return true.
1628 __ Bind(&return_true);
1629 __ movl(rsi, Immediate(1));
1630 __ jmp(&end);
1631
1632 // Return false and exit the function.
1633 __ Bind(&return_false);
1634 __ xorl(rsi, rsi);
1635 __ Bind(&end);
1636}
1637
Andreas Gampe21030dd2015-05-07 14:46:15 -07001638static void CreateStringIndexOfLocations(HInvoke* invoke,
1639 ArenaAllocator* allocator,
1640 bool start_at_zero) {
1641 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1642 LocationSummary::kCallOnSlowPath,
1643 kIntrinsified);
1644 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1645 locations->SetInAt(0, Location::RegisterLocation(RDI));
1646 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1647 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1648 // of the instruction explicitly.
1649 // Note: This works as we don't clobber RAX anywhere.
1650 locations->SetInAt(1, Location::RegisterLocation(RAX));
1651 if (!start_at_zero) {
1652 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1653 }
1654 // As we clobber RDI during execution anyways, also use it as the output.
1655 locations->SetOut(Location::SameAsFirstInput());
1656
1657 // repne scasw uses RCX as the counter.
1658 locations->AddTemp(Location::RegisterLocation(RCX));
1659 // Need another temporary to be able to compute the result.
1660 locations->AddTemp(Location::RequiresRegister());
1661}
1662
1663static void GenerateStringIndexOf(HInvoke* invoke,
1664 X86_64Assembler* assembler,
1665 CodeGeneratorX86_64* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001666 bool start_at_zero) {
1667 LocationSummary* locations = invoke->GetLocations();
1668
1669 // Note that the null check must have been done earlier.
1670 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1671
1672 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1673 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1674 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1675 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1676 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1677
1678 // Check our assumptions for registers.
1679 DCHECK_EQ(string_obj.AsRegister(), RDI);
1680 DCHECK_EQ(search_value.AsRegister(), RAX);
1681 DCHECK_EQ(counter.AsRegister(), RCX);
1682 DCHECK_EQ(out.AsRegister(), RDI);
1683
1684 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001685 // 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 -07001686 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001687 HInstruction* code_point = invoke->InputAt(1);
1688 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001689 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001690 std::numeric_limits<uint16_t>::max()) {
1691 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1692 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001693 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001694 codegen->AddSlowPath(slow_path);
1695 __ jmp(slow_path->GetEntryLabel());
1696 __ Bind(slow_path->GetExitLabel());
1697 return;
1698 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001700 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001701 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001702 codegen->AddSlowPath(slow_path);
1703 __ j(kAbove, slow_path->GetEntryLabel());
1704 }
1705
jessicahandojo4877b792016-09-08 19:49:13 -07001706 // From here down, we know that we are looking for a char that fits in
1707 // 16 bits (uncompressed) or 8 bits (compressed).
Andreas Gampe21030dd2015-05-07 14:46:15 -07001708 // Location of reference to data array within the String object.
1709 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1710 // Location of count within the String object.
1711 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1712
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001713 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001714 __ movl(string_length, Address(string_obj, count_offset));
1715
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001716 // Do a zero-length check. Even with string compression `count == 0` means empty.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001717 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001718 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001719 __ testl(string_length, string_length);
1720 __ j(kEqual, &not_found_label);
1721
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001722 if (mirror::kUseStringCompression) {
1723 // Use TMP to keep string_length_flagged.
1724 __ movl(CpuRegister(TMP), string_length);
1725 // Mask out first bit used as compression flag.
1726 __ shrl(string_length, Immediate(1));
1727 }
1728
Andreas Gampe21030dd2015-05-07 14:46:15 -07001729 if (start_at_zero) {
1730 // Number of chars to scan is the same as the string length.
1731 __ movl(counter, string_length);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001732 // Move to the start of the string.
1733 __ addq(string_obj, Immediate(value_offset));
1734 } else {
1735 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1736
1737 // Do a start_index check.
1738 __ cmpl(start_index, string_length);
1739 __ j(kGreaterEqual, &not_found_label);
1740
1741 // Ensure we have a start index >= 0;
1742 __ xorl(counter, counter);
1743 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001744 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001745
jessicahandojo4877b792016-09-08 19:49:13 -07001746 if (mirror::kUseStringCompression) {
1747 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001748 __ testl(CpuRegister(TMP), Immediate(1));
1749 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001750 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1751 __ jmp(&modify_counter);
1752 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1753 __ Bind(&offset_uncompressed_label);
1754 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1755 __ Bind(&modify_counter);
1756 } else {
1757 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1758 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001759 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1760 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1761 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1762 }
1763
jessicahandojo4877b792016-09-08 19:49:13 -07001764 if (mirror::kUseStringCompression) {
1765 NearLabel uncompressed_string_comparison;
1766 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001767 __ testl(CpuRegister(TMP), Immediate(1));
1768 __ j(kNotZero, &uncompressed_string_comparison);
jessicahandojo4877b792016-09-08 19:49:13 -07001769 // Check if RAX (search_value) is ASCII.
1770 __ cmpl(search_value, Immediate(127));
1771 __ j(kGreater, &not_found_label);
1772 // Comparing byte-per-byte.
1773 __ repne_scasb();
1774 __ jmp(&comparison_done);
1775 // Everything is set up for repne scasw:
1776 // * Comparison address in RDI.
1777 // * Counter in ECX.
1778 __ Bind(&uncompressed_string_comparison);
1779 __ repne_scasw();
1780 __ Bind(&comparison_done);
1781 } else {
1782 __ repne_scasw();
1783 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001784 // Did we find a match?
1785 __ j(kNotEqual, &not_found_label);
1786
1787 // Yes, we matched. Compute the index of the result.
1788 __ subl(string_length, counter);
1789 __ leal(out, Address(string_length, -1));
1790
Mark Mendell0c9497d2015-08-21 09:30:05 -04001791 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001792 __ jmp(&done);
1793
1794 // Failed to match; return -1.
1795 __ Bind(&not_found_label);
1796 __ movl(out, Immediate(-1));
1797
1798 // And join up at the end.
1799 __ Bind(&done);
1800 if (slow_path != nullptr) {
1801 __ Bind(slow_path->GetExitLabel());
1802 }
1803}
1804
1805void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001806 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001807}
1808
1809void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001810 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001811}
1812
1813void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001814 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001815}
1816
1817void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001818 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001819}
1820
Jeff Hao848f70a2014-01-15 13:49:50 -08001821void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001822 LocationSummary* locations = new (allocator_) LocationSummary(
1823 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001824 InvokeRuntimeCallingConvention calling_convention;
1825 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1826 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1827 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1828 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1829 locations->SetOut(Location::RegisterLocation(RAX));
1830}
1831
1832void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1833 X86_64Assembler* assembler = GetAssembler();
1834 LocationSummary* locations = invoke->GetLocations();
1835
1836 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1837 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001838 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001839 codegen_->AddSlowPath(slow_path);
1840 __ j(kEqual, slow_path->GetEntryLabel());
1841
Serban Constantinescuba45db02016-07-12 22:53:02 +01001842 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001843 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001844 __ Bind(slow_path->GetExitLabel());
1845}
1846
1847void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001848 LocationSummary* locations =
1849 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001850 InvokeRuntimeCallingConvention calling_convention;
1851 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1852 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1853 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1854 locations->SetOut(Location::RegisterLocation(RAX));
1855}
1856
1857void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001858 // No need to emit code checking whether `locations->InAt(2)` is a null
1859 // pointer, as callers of the native method
1860 //
1861 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1862 //
1863 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001864 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001865 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001866}
1867
1868void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001869 LocationSummary* locations = new (allocator_) LocationSummary(
1870 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001871 InvokeRuntimeCallingConvention calling_convention;
1872 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1873 locations->SetOut(Location::RegisterLocation(RAX));
1874}
1875
1876void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1877 X86_64Assembler* assembler = GetAssembler();
1878 LocationSummary* locations = invoke->GetLocations();
1879
1880 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1881 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001882 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001883 codegen_->AddSlowPath(slow_path);
1884 __ j(kEqual, slow_path->GetEntryLabel());
1885
Serban Constantinescuba45db02016-07-12 22:53:02 +01001886 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001887 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001888 __ Bind(slow_path->GetExitLabel());
1889}
1890
Mark Mendell8f8926a2015-08-17 11:39:06 -04001891void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1892 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001893 LocationSummary* locations =
1894 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001895 locations->SetInAt(0, Location::RequiresRegister());
1896 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1897 locations->SetInAt(2, Location::RequiresRegister());
1898 locations->SetInAt(3, Location::RequiresRegister());
1899 locations->SetInAt(4, Location::RequiresRegister());
1900
1901 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1902 locations->AddTemp(Location::RegisterLocation(RSI));
1903 locations->AddTemp(Location::RegisterLocation(RDI));
1904 locations->AddTemp(Location::RegisterLocation(RCX));
1905}
1906
1907void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1908 X86_64Assembler* assembler = GetAssembler();
1909 LocationSummary* locations = invoke->GetLocations();
1910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001911 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001912 // Location of data in char array buffer.
1913 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1914 // Location of char array data in string.
1915 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1916
1917 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1918 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1919 Location srcBegin = locations->InAt(1);
1920 int srcBegin_value =
1921 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1922 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1923 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1924 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1925
1926 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001928 DCHECK_EQ(char_size, 2u);
1929
jessicahandojo4877b792016-09-08 19:49:13 -07001930 NearLabel done;
Mark Mendell8f8926a2015-08-17 11:39:06 -04001931 // Compute the number of chars (words) to move.
1932 __ movl(CpuRegister(RCX), srcEnd);
1933 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001934 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001935 } else {
1936 DCHECK(srcBegin.IsRegister());
1937 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1938 }
jessicahandojo4877b792016-09-08 19:49:13 -07001939 if (mirror::kUseStringCompression) {
1940 NearLabel copy_uncompressed, copy_loop;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001942 DCHECK_EQ(c_char_size, 1u);
1943 // Location of count in string.
1944 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Mark Mendell8f8926a2015-08-17 11:39:06 -04001945
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001946 __ testl(Address(obj, count_offset), Immediate(1));
1947 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1948 "Expecting 0=compressed, 1=uncompressed");
1949 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001950 // Compute the address of the source string by adding the number of chars from
1951 // the source beginning to the value offset of a string.
1952 __ leaq(CpuRegister(RSI),
1953 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1954 // Start the loop to copy String's value to Array of Char.
1955 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1956
1957 __ Bind(&copy_loop);
1958 __ jrcxz(&done);
1959 // Use TMP as temporary (convert byte from RSI to word).
1960 // TODO: Selecting RAX as the temporary and using LODSB/STOSW.
1961 __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
1962 __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
1963 __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
1964 __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
1965 // TODO: Add support for LOOP to X86_64Assembler.
1966 __ subl(CpuRegister(RCX), Immediate(1));
1967 __ jmp(&copy_loop);
1968
1969 __ Bind(&copy_uncompressed);
1970 }
1971
1972 __ leaq(CpuRegister(RSI),
1973 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
1974 // Compute the address of the destination buffer.
1975 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001976 // Do the move.
1977 __ rep_movsw();
jessicahandojo4877b792016-09-08 19:49:13 -07001978
1979 __ Bind(&done);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001980}
1981
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001982static void GenPeek(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001983 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1984 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1985 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1986 // to avoid a SIGBUS.
1987 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 case DataType::Type::kInt8:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001989 __ movsxb(out, Address(address, 0));
1990 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001991 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001992 __ movsxw(out, Address(address, 0));
1993 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001994 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001995 __ movl(out, Address(address, 0));
1996 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001998 __ movq(out, Address(address, 0));
1999 break;
2000 default:
2001 LOG(FATAL) << "Type not recognized for peek: " << size;
2002 UNREACHABLE();
2003 }
2004}
2005
2006void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002007 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002008}
2009
2010void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002011 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002012}
2013
2014void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002015 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002016}
2017
2018void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002019 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002020}
2021
2022void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002023 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002024}
2025
2026void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002028}
2029
2030void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002031 CreateIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002032}
2033
2034void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002035 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002036}
2037
Vladimir Markoca6fff82017-10-03 14:49:14 +01002038static void CreateIntIntToVoidLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2039 LocationSummary* locations =
2040 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002041 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002042 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002043}
2044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002045static void GenPoke(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002046 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04002047 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002048 // x86 allows unaligned access. We do not have to check the input or use specific instructions
2049 // to avoid a SIGBUS.
2050 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 case DataType::Type::kInt8:
Mark Mendell40741f32015-04-20 22:10:34 -04002052 if (value.IsConstant()) {
2053 __ movb(Address(address, 0),
2054 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2055 } else {
2056 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
2057 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002058 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002059 case DataType::Type::kInt16:
Mark Mendell40741f32015-04-20 22:10:34 -04002060 if (value.IsConstant()) {
2061 __ movw(Address(address, 0),
2062 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2063 } else {
2064 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
2065 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002066 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002068 if (value.IsConstant()) {
2069 __ movl(Address(address, 0),
2070 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2071 } else {
2072 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
2073 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002074 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002075 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002076 if (value.IsConstant()) {
2077 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
2078 DCHECK(IsInt<32>(v));
2079 int32_t v_32 = v;
2080 __ movq(Address(address, 0), Immediate(v_32));
2081 } else {
2082 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
2083 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002084 break;
2085 default:
2086 LOG(FATAL) << "Type not recognized for poke: " << size;
2087 UNREACHABLE();
2088 }
2089}
2090
2091void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002092 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002093}
2094
2095void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002097}
2098
2099void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002100 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002101}
2102
2103void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002104 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002105}
2106
2107void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002108 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002109}
2110
2111void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002112 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002113}
2114
2115void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002116 CreateIntIntToVoidLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002117}
2118
2119void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002121}
2122
2123void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002124 LocationSummary* locations =
2125 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002126 locations->SetOut(Location::RequiresRegister());
2127}
2128
2129void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2130 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07002131 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002132 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002133}
2134
Roland Levillain0d5a2812015-11-13 10:07:31 +00002135static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00002137 bool is_volatile ATTRIBUTE_UNUSED,
2138 CodeGeneratorX86_64* codegen) {
2139 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
2140 LocationSummary* locations = invoke->GetLocations();
2141 Location base_loc = locations->InAt(1);
2142 CpuRegister base = base_loc.AsRegister<CpuRegister>();
2143 Location offset_loc = locations->InAt(2);
2144 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
2145 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002146 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002147
Andreas Gampe878d58c2015-01-15 23:24:00 -08002148 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 case DataType::Type::kInt32:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002150 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002151 break;
2152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002154 if (kEmitCompilerReadBarrier) {
2155 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002156 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2157 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002158 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002159 } else {
2160 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2161 codegen->GenerateReadBarrierSlow(
2162 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2163 }
2164 } else {
2165 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2166 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002167 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002168 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002169 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 case DataType::Type::kInt64:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002172 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002173 break;
2174
2175 default:
2176 LOG(FATAL) << "Unsupported op size " << type;
2177 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002178 }
2179}
2180
Vladimir Markoca6fff82017-10-03 14:49:14 +01002181static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002182 bool can_call = kEmitCompilerReadBarrier &&
2183 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2184 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002185 LocationSummary* locations =
2186 new (allocator) LocationSummary(invoke,
2187 can_call
2188 ? LocationSummary::kCallOnSlowPath
2189 : LocationSummary::kNoCall,
2190 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002191 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002192 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002193 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002194 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002195 locations->SetInAt(1, Location::RequiresRegister());
2196 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002197 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002198 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002199}
2200
2201void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002202 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002203}
2204void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002205 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002206}
2207void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002208 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002209}
2210void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002211 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002212}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002213void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002214 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002215}
2216void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002217 CreateIntIntIntToIntLocations(allocator_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002218}
2219
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002220
2221void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002223}
2224void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002225 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002226}
2227void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002228 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002229}
2230void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002232}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002233void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002234 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002235}
2236void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002238}
2239
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002240
Vladimir Markoca6fff82017-10-03 14:49:14 +01002241static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 DataType::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002243 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002244 LocationSummary* locations =
2245 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002246 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002247 locations->SetInAt(1, Location::RequiresRegister());
2248 locations->SetInAt(2, Location::RequiresRegister());
2249 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 if (type == DataType::Type::kReference) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002251 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002252 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002253 locations->AddTemp(Location::RequiresRegister());
2254 }
2255}
2256
2257void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(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::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002261 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002262}
2263void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002264 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002265}
2266void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(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::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002271}
2272void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002273 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002274}
2275void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002276 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002277}
2278void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002279 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002280}
2281void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002282 CreateIntIntIntIntToVoidPlusTempsLocations(allocator_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002283}
2284
2285// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2286// memory model.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002287static void GenUnsafePut(LocationSummary* locations, DataType::Type type, bool is_volatile,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002288 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002289 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002290 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2291 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2292 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002294 if (type == DataType::Type::kInt64) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002295 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002297 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2298 __ movl(temp, value);
2299 __ PoisonHeapReference(temp);
2300 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002301 } else {
2302 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2303 }
2304
2305 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002306 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002307 }
2308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002309 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002310 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002311 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2312 locations->GetTemp(1).AsRegister<CpuRegister>(),
2313 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002314 value,
2315 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002316 }
2317}
2318
2319void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002321}
2322void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002323 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002324}
2325void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002327}
2328void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002329 GenUnsafePut(
2330 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002331}
2332void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333 GenUnsafePut(
2334 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002335}
2336void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002337 GenUnsafePut(
2338 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002339}
2340void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002342}
2343void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002344 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002345}
2346void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002347 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002348}
2349
Vladimir Markoca6fff82017-10-03 14:49:14 +01002350static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002351 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002352 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002353 bool can_call = kEmitCompilerReadBarrier &&
2354 kUseBakerReadBarrier &&
2355 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002356 LocationSummary* locations =
2357 new (allocator) LocationSummary(invoke,
2358 can_call
2359 ? LocationSummary::kCallOnSlowPath
2360 : LocationSummary::kNoCall,
2361 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002362 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2363 locations->SetInAt(1, Location::RequiresRegister());
2364 locations->SetInAt(2, Location::RequiresRegister());
2365 // expected value must be in EAX/RAX.
2366 locations->SetInAt(3, Location::RegisterLocation(RAX));
2367 locations->SetInAt(4, Location::RequiresRegister());
2368
2369 locations->SetOut(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002371 // Need temporary registers for card-marking, and possibly for
2372 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002373 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002374 locations->AddTemp(Location::RequiresRegister());
2375 }
2376}
2377
2378void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002379 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002380}
2381
2382void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002383 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002384}
2385
2386void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002387 // The only read barrier implementation supporting the
2388 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2389 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002390 return;
2391 }
2392
Vladimir Markoca6fff82017-10-03 14:49:14 +01002393 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002394}
2395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002396static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002397 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002398 LocationSummary* locations = invoke->GetLocations();
2399
2400 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2401 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2402 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002403 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002404 DCHECK_EQ(expected.AsRegister(), RAX);
2405 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002406 Location out_loc = locations->Out();
2407 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002410 // The only read barrier implementation supporting the
2411 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2412 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2413
2414 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2415 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2416
Roland Levillainb488b782015-10-22 11:38:49 +01002417 // Mark card for object assuming new value is stored.
2418 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002419 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2420
2421 // The address of the field within the holding object.
2422 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2423
2424 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2425 // Need to make sure the reference stored in the field is a to-space
2426 // one before attempting the CAS or the CAS could fail incorrectly.
2427 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2428 invoke,
2429 out_loc, // Unused, used only as a "temporary" within the read barrier.
2430 base,
2431 field_addr,
2432 /* needs_null_check */ false,
2433 /* always_update_field */ true,
2434 &temp1,
2435 &temp2);
2436 }
Roland Levillain4d027112015-07-01 15:41:14 +01002437
Roland Levillainb488b782015-10-22 11:38:49 +01002438 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2439 Register value_reg = value.AsRegister();
2440 if (kPoisonHeapReferences) {
2441 if (base_equals_value) {
2442 // If `base` and `value` are the same register location, move
2443 // `value_reg` to a temporary register. This way, poisoning
2444 // `value_reg` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002445 value_reg = temp1.AsRegister();
Roland Levillainb488b782015-10-22 11:38:49 +01002446 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002447 }
Roland Levillainb488b782015-10-22 11:38:49 +01002448
2449 // Check that the register allocator did not assign the location
2450 // of `expected` (RAX) to `value` nor to `base`, so that heap
2451 // poisoning (when enabled) works as intended below.
2452 // - If `value` were equal to `expected`, both references would
2453 // be poisoned twice, meaning they would not be poisoned at
2454 // all, as heap poisoning uses address negation.
2455 // - If `base` were equal to `expected`, poisoning `expected`
2456 // would invalidate `base`.
2457 DCHECK_NE(value_reg, expected.AsRegister());
2458 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2459
2460 __ PoisonHeapReference(expected);
2461 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002462 }
2463
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002464 __ LockCmpxchgl(field_addr, CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002465
Roland Levillain0d5a2812015-11-13 10:07:31 +00002466 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002467 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002468
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002469 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002470 __ setcc(kZero, out);
2471 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002472
Roland Levillain391b8662015-12-18 11:43:38 +00002473 // If heap poisoning is enabled, we need to unpoison the values
2474 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002475 if (kPoisonHeapReferences) {
2476 if (base_equals_value) {
2477 // `value_reg` has been moved to a temporary register, no need
2478 // to unpoison it.
2479 } else {
2480 // Ensure `value` is different from `out`, so that unpoisoning
2481 // the former does not invalidate the latter.
2482 DCHECK_NE(value_reg, out.AsRegister());
2483 __ UnpoisonHeapReference(CpuRegister(value_reg));
2484 }
2485 // Ensure `expected` is different from `out`, so that unpoisoning
2486 // the former does not invalidate the latter.
2487 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2488 __ UnpoisonHeapReference(expected);
2489 }
2490 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002492 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002494 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2495 } else {
2496 LOG(FATAL) << "Unexpected CAS type " << type;
2497 }
2498
Roland Levillain0d5a2812015-11-13 10:07:31 +00002499 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002500 // scheduling barriers at this time.
2501
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002502 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002503 __ setcc(kZero, out);
2504 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002505 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002506}
2507
2508void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002509 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002510}
2511
2512void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002514}
2515
2516void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002517 // The only read barrier implementation supporting the
2518 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2519 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002521 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002522}
2523
2524void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002525 LocationSummary* locations =
2526 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002527 locations->SetInAt(0, Location::RequiresRegister());
2528 locations->SetOut(Location::SameAsFirstInput());
2529 locations->AddTemp(Location::RequiresRegister());
2530}
2531
2532static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2533 X86_64Assembler* assembler) {
2534 Immediate imm_shift(shift);
2535 Immediate imm_mask(mask);
2536 __ movl(temp, reg);
2537 __ shrl(reg, imm_shift);
2538 __ andl(temp, imm_mask);
2539 __ andl(reg, imm_mask);
2540 __ shll(temp, imm_shift);
2541 __ orl(reg, temp);
2542}
2543
2544void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002545 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002546 LocationSummary* locations = invoke->GetLocations();
2547
2548 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2549 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2550
2551 /*
2552 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2553 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2554 * compared to generic luni implementation which has 5 rounds of swapping bits.
2555 * x = bswap x
2556 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2557 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2558 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2559 */
2560 __ bswapl(reg);
2561 SwapBits(reg, temp, 1, 0x55555555, assembler);
2562 SwapBits(reg, temp, 2, 0x33333333, assembler);
2563 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2564}
2565
2566void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002567 LocationSummary* locations =
2568 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002569 locations->SetInAt(0, Location::RequiresRegister());
2570 locations->SetOut(Location::SameAsFirstInput());
2571 locations->AddTemp(Location::RequiresRegister());
2572 locations->AddTemp(Location::RequiresRegister());
2573}
2574
2575static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2576 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2577 Immediate imm_shift(shift);
2578 __ movq(temp_mask, Immediate(mask));
2579 __ movq(temp, reg);
2580 __ shrq(reg, imm_shift);
2581 __ andq(temp, temp_mask);
2582 __ andq(reg, temp_mask);
2583 __ shlq(temp, imm_shift);
2584 __ orq(reg, temp);
2585}
2586
2587void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002588 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002589 LocationSummary* locations = invoke->GetLocations();
2590
2591 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2592 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2593 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2594
2595 /*
2596 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2597 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2598 * compared to generic luni implementation which has 5 rounds of swapping bits.
2599 * x = bswap x
2600 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2601 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2602 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2603 */
2604 __ bswapq(reg);
2605 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2606 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2607 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2608}
2609
Aart Bik3f67e692016-01-15 14:35:12 -08002610static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002611 ArenaAllocator* allocator, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Aart Bik3f67e692016-01-15 14:35:12 -08002612 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2613 // Do nothing if there is no popcnt support. This results in generating
2614 // a call for the intrinsic rather than direct code.
2615 return;
2616 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002617 LocationSummary* locations =
2618 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bik3f67e692016-01-15 14:35:12 -08002619 locations->SetInAt(0, Location::Any());
2620 locations->SetOut(Location::RequiresRegister());
2621}
2622
Aart Bikc5d47542016-01-27 17:00:35 -08002623static void GenBitCount(X86_64Assembler* assembler,
2624 CodeGeneratorX86_64* codegen,
2625 HInvoke* invoke,
2626 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002627 LocationSummary* locations = invoke->GetLocations();
2628 Location src = locations->InAt(0);
2629 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2630
2631 if (invoke->InputAt(0)->IsConstant()) {
2632 // Evaluate this at compile time.
2633 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002634 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002635 ? POPCOUNT(static_cast<uint64_t>(value))
2636 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002637 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002638 return;
2639 }
2640
2641 if (src.IsRegister()) {
2642 if (is_long) {
2643 __ popcntq(out, src.AsRegister<CpuRegister>());
2644 } else {
2645 __ popcntl(out, src.AsRegister<CpuRegister>());
2646 }
2647 } else if (is_long) {
2648 DCHECK(src.IsDoubleStackSlot());
2649 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2650 } else {
2651 DCHECK(src.IsStackSlot());
2652 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2653 }
2654}
2655
2656void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002657 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002658}
2659
2660void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002661 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002662}
2663
2664void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002665 CreateBitCountLocations(allocator_, codegen_, invoke);
Aart Bik3f67e692016-01-15 14:35:12 -08002666}
2667
2668void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002669 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2670}
2671
Vladimir Markoca6fff82017-10-03 14:49:14 +01002672static void CreateOneBitLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_high) {
2673 LocationSummary* locations =
2674 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc5d47542016-01-27 17:00:35 -08002675 locations->SetInAt(0, Location::Any());
2676 locations->SetOut(Location::RequiresRegister());
2677 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2678 : Location::RequiresRegister()); // any will do
2679}
2680
2681static void GenOneBit(X86_64Assembler* assembler,
2682 CodeGeneratorX86_64* codegen,
2683 HInvoke* invoke,
2684 bool is_high, bool is_long) {
2685 LocationSummary* locations = invoke->GetLocations();
2686 Location src = locations->InAt(0);
2687 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2688
2689 if (invoke->InputAt(0)->IsConstant()) {
2690 // Evaluate this at compile time.
2691 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2692 if (value == 0) {
2693 __ xorl(out, out); // Clears upper bits too.
2694 return;
2695 }
2696 // Nonzero value.
2697 if (is_high) {
2698 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2699 : 31 - CLZ(static_cast<uint32_t>(value));
2700 } else {
2701 value = is_long ? CTZ(static_cast<uint64_t>(value))
2702 : CTZ(static_cast<uint32_t>(value));
2703 }
2704 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002705 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002706 } else {
2707 codegen->Load32BitValue(out, 1 << value);
2708 }
2709 return;
2710 }
2711
2712 // Handle the non-constant cases.
2713 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2714 if (is_high) {
2715 // Use architectural support: basically 1 << bsr.
2716 if (src.IsRegister()) {
2717 if (is_long) {
2718 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2719 } else {
2720 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2721 }
2722 } else if (is_long) {
2723 DCHECK(src.IsDoubleStackSlot());
2724 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2725 } else {
2726 DCHECK(src.IsStackSlot());
2727 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2728 }
2729 // BSR sets ZF if the input was zero.
2730 NearLabel is_zero, done;
2731 __ j(kEqual, &is_zero);
2732 __ movl(out, Immediate(1)); // Clears upper bits too.
2733 if (is_long) {
2734 __ shlq(out, tmp);
2735 } else {
2736 __ shll(out, tmp);
2737 }
2738 __ jmp(&done);
2739 __ Bind(&is_zero);
2740 __ xorl(out, out); // Clears upper bits too.
2741 __ Bind(&done);
2742 } else {
2743 // Copy input into temporary.
2744 if (src.IsRegister()) {
2745 if (is_long) {
2746 __ movq(tmp, src.AsRegister<CpuRegister>());
2747 } else {
2748 __ movl(tmp, src.AsRegister<CpuRegister>());
2749 }
2750 } else if (is_long) {
2751 DCHECK(src.IsDoubleStackSlot());
2752 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2753 } else {
2754 DCHECK(src.IsStackSlot());
2755 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2756 }
2757 // Do the bit twiddling: basically tmp & -tmp;
2758 if (is_long) {
2759 __ movq(out, tmp);
2760 __ negq(tmp);
2761 __ andq(out, tmp);
2762 } else {
2763 __ movl(out, tmp);
2764 __ negl(tmp);
2765 __ andl(out, tmp);
2766 }
2767 }
2768}
2769
2770void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002771 CreateOneBitLocations(allocator_, invoke, /* is_high */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002772}
2773
2774void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2775 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2776}
2777
2778void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002779 CreateOneBitLocations(allocator_, invoke, /* is_high */ true);
Aart Bikc5d47542016-01-27 17:00:35 -08002780}
2781
2782void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2783 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2784}
2785
2786void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002787 CreateOneBitLocations(allocator_, invoke, /* is_high */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002788}
2789
2790void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2791 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2792}
2793
2794void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002795 CreateOneBitLocations(allocator_, invoke, /* is_high */ false);
Aart Bikc5d47542016-01-27 17:00:35 -08002796}
2797
2798void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2799 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002800}
2801
Vladimir Markoca6fff82017-10-03 14:49:14 +01002802static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2803 LocationSummary* locations =
2804 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002805 locations->SetInAt(0, Location::Any());
2806 locations->SetOut(Location::RequiresRegister());
2807}
2808
Aart Bikc5d47542016-01-27 17:00:35 -08002809static void GenLeadingZeros(X86_64Assembler* assembler,
2810 CodeGeneratorX86_64* codegen,
2811 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002812 LocationSummary* locations = invoke->GetLocations();
2813 Location src = locations->InAt(0);
2814 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2815
2816 int zero_value_result = is_long ? 64 : 32;
2817 if (invoke->InputAt(0)->IsConstant()) {
2818 // Evaluate this at compile time.
2819 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2820 if (value == 0) {
2821 value = zero_value_result;
2822 } else {
2823 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2824 }
Aart Bikc5d47542016-01-27 17:00:35 -08002825 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002826 return;
2827 }
2828
2829 // Handle the non-constant cases.
2830 if (src.IsRegister()) {
2831 if (is_long) {
2832 __ bsrq(out, src.AsRegister<CpuRegister>());
2833 } else {
2834 __ bsrl(out, src.AsRegister<CpuRegister>());
2835 }
2836 } else if (is_long) {
2837 DCHECK(src.IsDoubleStackSlot());
2838 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2839 } else {
2840 DCHECK(src.IsStackSlot());
2841 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2842 }
2843
2844 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002845 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002846 __ j(kEqual, &is_zero);
2847
2848 // Correct the result from BSR to get the CLZ result.
2849 __ xorl(out, Immediate(zero_value_result - 1));
2850 __ jmp(&done);
2851
2852 // Fix the zero case with the expected result.
2853 __ Bind(&is_zero);
2854 __ movl(out, Immediate(zero_value_result));
2855
2856 __ Bind(&done);
2857}
2858
2859void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002860 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002861}
2862
2863void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002864 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002865}
2866
2867void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002868 CreateLeadingZeroLocations(allocator_, invoke);
Mark Mendelld5897672015-08-12 21:16:41 -04002869}
2870
2871void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002872 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002873}
2874
Vladimir Markoca6fff82017-10-03 14:49:14 +01002875static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke) {
2876 LocationSummary* locations =
2877 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002878 locations->SetInAt(0, Location::Any());
2879 locations->SetOut(Location::RequiresRegister());
2880}
2881
Aart Bikc5d47542016-01-27 17:00:35 -08002882static void GenTrailingZeros(X86_64Assembler* assembler,
2883 CodeGeneratorX86_64* codegen,
2884 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002885 LocationSummary* locations = invoke->GetLocations();
2886 Location src = locations->InAt(0);
2887 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2888
2889 int zero_value_result = is_long ? 64 : 32;
2890 if (invoke->InputAt(0)->IsConstant()) {
2891 // Evaluate this at compile time.
2892 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2893 if (value == 0) {
2894 value = zero_value_result;
2895 } else {
2896 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2897 }
Aart Bikc5d47542016-01-27 17:00:35 -08002898 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002899 return;
2900 }
2901
2902 // Handle the non-constant cases.
2903 if (src.IsRegister()) {
2904 if (is_long) {
2905 __ bsfq(out, src.AsRegister<CpuRegister>());
2906 } else {
2907 __ bsfl(out, src.AsRegister<CpuRegister>());
2908 }
2909 } else if (is_long) {
2910 DCHECK(src.IsDoubleStackSlot());
2911 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2912 } else {
2913 DCHECK(src.IsStackSlot());
2914 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2915 }
2916
2917 // BSF sets ZF if the input was zero, and the output is undefined.
2918 NearLabel done;
2919 __ j(kNotEqual, &done);
2920
2921 // Fix the zero case with the expected result.
2922 __ movl(out, Immediate(zero_value_result));
2923
2924 __ Bind(&done);
2925}
2926
2927void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002928 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002929}
2930
2931void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002932 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002933}
2934
2935void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002936 CreateTrailingZeroLocations(allocator_, invoke);
Mark Mendell2d554792015-09-15 21:45:18 -04002937}
2938
2939void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002940 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2941}
2942
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002943void IntrinsicLocationsBuilderX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2944 InvokeRuntimeCallingConvention calling_convention;
2945 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2946 invoke,
2947 codegen_,
2948 Location::RegisterLocation(RAX),
2949 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2950}
2951
2952void IntrinsicCodeGeneratorX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2953 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2954 LocationSummary* locations = invoke->GetLocations();
2955 X86_64Assembler* assembler = GetAssembler();
2956
2957 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2958 InvokeRuntimeCallingConvention calling_convention;
2959 if (invoke->InputAt(0)->IsConstant()) {
2960 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2961 if (value >= info.low && value <= info.high) {
2962 // Just embed the j.l.Integer in the code.
2963 ScopedObjectAccess soa(Thread::Current());
2964 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2965 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2966 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
Colin Cross0bd97172017-03-15 16:33:27 -07002967 __ movl(out, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002968 } else {
2969 // Allocate and initialize a new j.l.Integer.
2970 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2971 // JIT object table.
Colin Cross0bd97172017-03-15 16:33:27 -07002972 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002973 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07002974 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002975 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2976 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2977 __ movl(Address(out, info.value_offset), Immediate(value));
2978 }
2979 } else {
2980 CpuRegister in = locations->InAt(0).AsRegister<CpuRegister>();
2981 // Check bounds of our cache.
2982 __ leal(out, Address(in, -info.low));
2983 __ cmpl(out, Immediate(info.high - info.low + 1));
2984 NearLabel allocate, done;
2985 __ j(kAboveEqual, &allocate);
2986 // If the value is within the bounds, load the j.l.Integer directly from the array.
2987 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2988 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
Colin Cross0bd97172017-03-15 16:33:27 -07002989 if (data_offset + address <= std::numeric_limits<int32_t>::max()) {
2990 __ movl(out, Address(out, TIMES_4, data_offset + address));
2991 } else {
2992 CpuRegister temp = CpuRegister(calling_convention.GetRegisterAt(0));
2993 __ movl(temp, Immediate(static_cast<int32_t>(data_offset + address)));
2994 __ movl(out, Address(temp, out, TIMES_4, 0));
2995 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002996 __ MaybeUnpoisonHeapReference(out);
2997 __ jmp(&done);
2998 __ Bind(&allocate);
2999 // Otherwise allocate and initialize a new j.l.Integer.
Colin Cross0bd97172017-03-15 16:33:27 -07003000 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003001 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07003002 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003003 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3004 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3005 __ movl(Address(out, info.value_offset), in);
3006 __ Bind(&done);
3007 }
3008}
3009
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003010void IntrinsicLocationsBuilderX86_64::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003011 LocationSummary* locations =
3012 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003013 locations->SetOut(Location::RequiresRegister());
3014}
3015
3016void IntrinsicCodeGeneratorX86_64::VisitThreadInterrupted(HInvoke* invoke) {
3017 X86_64Assembler* assembler = GetAssembler();
3018 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
3019 Address address = Address::Absolute
3020 (Thread::InterruptedOffset<kX86_64PointerSize>().Int32Value(), /* no_rip */ true);
3021 NearLabel done;
3022 __ gs()->movl(out, address);
3023 __ testl(out, out);
3024 __ j(kEqual, &done);
3025 __ gs()->movl(address, Immediate(0));
3026 codegen_->MemoryFence();
3027 __ Bind(&done);
3028}
3029
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003030UNIMPLEMENTED_INTRINSIC(X86_64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003031UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
3032UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003033
Aart Bikff7d89c2016-11-07 08:49:28 -08003034UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOf);
3035UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003036UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferAppend);
3037UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferLength);
3038UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferToString);
3039UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppend);
3040UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderLength);
3041UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003042
Aart Bik0e54c012016-03-04 12:08:31 -08003043// 1.8.
3044UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
3045UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
3046UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
3047UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
3048UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003049
Aart Bik2f9fcc92016-03-01 15:16:54 -08003050UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01003051
3052#undef __
3053
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003054} // namespace x86_64
3055} // namespace art