blob: c79a3e37cb23a26440aa7a208d1336f3a3098f88 [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.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"
26#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070027#include "intrinsics_utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080028#include "mirror/array-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080029#include "mirror/string.h"
30#include "thread.h"
31#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/constants_x86_64.h"
33
34namespace art {
35
36namespace x86_64 {
37
Mark Mendellfb8d2792015-03-31 22:16:59 -040038IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
39 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
40}
41
42
Andreas Gampe71fb52f2014-12-29 17:43:08 -080043X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010044 return down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -080045}
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080048 return codegen_->GetGraph()->GetArena();
49}
50
51bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
52 Dispatch(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +000053 LocationSummary* res = invoke->GetLocations();
54 if (res == nullptr) {
55 return false;
56 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000057 return res->Intrinsified();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080058}
59
Roland Levillainec525fc2015-04-28 15:50:20 +010060static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010061 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010062 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080063}
64
Andreas Gampe85b62f22015-09-09 13:15:38 -070065using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080066
Roland Levillain0b671c02016-08-19 12:02:34 +010067// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
68#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
69
70// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
71class ReadBarrierSystemArrayCopySlowPathX86_64 : public SlowPathCode {
72 public:
73 explicit ReadBarrierSystemArrayCopySlowPathX86_64(HInstruction* instruction)
74 : SlowPathCode(instruction) {
75 DCHECK(kEmitCompilerReadBarrier);
76 DCHECK(kUseBakerReadBarrier);
77 }
78
79 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
80 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
81 LocationSummary* locations = instruction_->GetLocations();
82 DCHECK(locations->CanCall());
83 DCHECK(instruction_->IsInvokeStaticOrDirect())
84 << "Unexpected instruction in read barrier arraycopy slow path: "
85 << instruction_->DebugName();
86 DCHECK(instruction_->GetLocations()->Intrinsified());
87 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
88
89 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
90
91 CpuRegister src_curr_addr = locations->GetTemp(0).AsRegister<CpuRegister>();
92 CpuRegister dst_curr_addr = locations->GetTemp(1).AsRegister<CpuRegister>();
93 CpuRegister src_stop_addr = locations->GetTemp(2).AsRegister<CpuRegister>();
94
95 __ Bind(GetEntryLabel());
96 NearLabel loop;
97 __ Bind(&loop);
98 __ movl(CpuRegister(TMP), Address(src_curr_addr, 0));
99 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
100 // TODO: Inline the mark bit check before calling the runtime?
101 // TMP = ReadBarrier::Mark(TMP);
102 // No need to save live registers; it's taken care of by the
103 // entrypoint. Also, there is no need to update the stack mask,
104 // as this runtime call will not trigger a garbage collection.
105 int32_t entry_point_offset =
106 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(TMP);
107 // This runtime call does not require a stack map.
108 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
109 __ MaybePoisonHeapReference(CpuRegister(TMP));
110 __ movl(Address(dst_curr_addr, 0), CpuRegister(TMP));
111 __ addl(src_curr_addr, Immediate(element_size));
112 __ addl(dst_curr_addr, Immediate(element_size));
113 __ cmpl(src_curr_addr, src_stop_addr);
114 __ j(kNotEqual, &loop);
115 __ jmp(GetExitLabel());
116 }
117
118 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86_64"; }
119
120 private:
121 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86_64);
122};
123
124#undef __
125
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800126#define __ assembler->
127
128static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
129 LocationSummary* locations = new (arena) LocationSummary(invoke,
130 LocationSummary::kNoCall,
131 kIntrinsified);
132 locations->SetInAt(0, Location::RequiresFpuRegister());
133 locations->SetOut(Location::RequiresRegister());
134}
135
136static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
137 LocationSummary* locations = new (arena) LocationSummary(invoke,
138 LocationSummary::kNoCall,
139 kIntrinsified);
140 locations->SetInAt(0, Location::RequiresRegister());
141 locations->SetOut(Location::RequiresFpuRegister());
142}
143
144static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
145 Location input = locations->InAt(0);
146 Location output = locations->Out();
147 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
148}
149
150static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
151 Location input = locations->InAt(0);
152 Location output = locations->Out();
153 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
154}
155
156void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
157 CreateFPToIntLocations(arena_, invoke);
158}
159void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
160 CreateIntToFPLocations(arena_, invoke);
161}
162
163void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000164 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800165}
166void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000167 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800168}
169
170void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
171 CreateFPToIntLocations(arena_, invoke);
172}
173void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
174 CreateIntToFPLocations(arena_, invoke);
175}
176
177void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000178 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800179}
180void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000181 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800182}
183
184static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
185 LocationSummary* locations = new (arena) LocationSummary(invoke,
186 LocationSummary::kNoCall,
187 kIntrinsified);
188 locations->SetInAt(0, Location::RequiresRegister());
189 locations->SetOut(Location::SameAsFirstInput());
190}
191
192static void GenReverseBytes(LocationSummary* locations,
193 Primitive::Type size,
194 X86_64Assembler* assembler) {
195 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
196
197 switch (size) {
198 case Primitive::kPrimShort:
199 // 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;
203 case Primitive::kPrimInt:
204 __ bswapl(out);
205 break;
206 case Primitive::kPrimLong:
207 __ 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) {
216 CreateIntToIntLocations(arena_, invoke);
217}
218
219void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
220 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
221}
222
223void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
224 CreateIntToIntLocations(arena_, invoke);
225}
226
227void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
228 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
229}
230
231void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
232 CreateIntToIntLocations(arena_, invoke);
233}
234
235void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
236 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
237}
238
239
240// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
241// need is 64b.
242
243static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
244 // TODO: Enable memory operations when the assembler supports them.
245 LocationSummary* locations = new (arena) LocationSummary(invoke,
246 LocationSummary::kNoCall,
247 kIntrinsified);
248 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800249 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400250 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800251}
252
Mark Mendell39dcf552015-04-09 20:42:42 -0400253static void MathAbsFP(LocationSummary* locations,
254 bool is64bit,
255 X86_64Assembler* assembler,
256 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800257 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800258
Mark Mendellcfa410b2015-05-25 16:02:44 -0400259 DCHECK(output.IsFpuRegister());
260 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800261
Mark Mendellcfa410b2015-05-25 16:02:44 -0400262 // TODO: Can mask directly with constant area using pand if we can guarantee
263 // that the literal is aligned on a 16 byte boundary. This will avoid a
264 // temporary.
265 if (is64bit) {
266 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
267 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800268 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400269 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
270 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800271 }
272}
273
274void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
275 CreateFloatToFloatPlusTemps(arena_, invoke);
276}
277
278void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000279 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800280}
281
282void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
283 CreateFloatToFloatPlusTemps(arena_, invoke);
284}
285
286void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000287 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800288}
289
290static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
291 LocationSummary* locations = new (arena) LocationSummary(invoke,
292 LocationSummary::kNoCall,
293 kIntrinsified);
294 locations->SetInAt(0, Location::RequiresRegister());
295 locations->SetOut(Location::SameAsFirstInput());
296 locations->AddTemp(Location::RequiresRegister());
297}
298
299static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
300 Location output = locations->Out();
301 CpuRegister out = output.AsRegister<CpuRegister>();
302 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
303
304 if (is64bit) {
305 // Create mask.
306 __ movq(mask, out);
307 __ sarq(mask, Immediate(63));
308 // Add mask.
309 __ addq(out, mask);
310 __ xorq(out, mask);
311 } else {
312 // Create mask.
313 __ movl(mask, out);
314 __ sarl(mask, Immediate(31));
315 // Add mask.
316 __ addl(out, mask);
317 __ xorl(out, mask);
318 }
319}
320
321void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
322 CreateIntToIntPlusTemp(arena_, invoke);
323}
324
325void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000326 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800327}
328
329void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
330 CreateIntToIntPlusTemp(arena_, invoke);
331}
332
333void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000334 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800335}
336
Mark Mendell39dcf552015-04-09 20:42:42 -0400337static void GenMinMaxFP(LocationSummary* locations,
338 bool is_min,
339 bool is_double,
340 X86_64Assembler* assembler,
341 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800342 Location op1_loc = locations->InAt(0);
343 Location op2_loc = locations->InAt(1);
344 Location out_loc = locations->Out();
345 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
346
347 // Shortcut for same input locations.
348 if (op1_loc.Equals(op2_loc)) {
349 DCHECK(out_loc.Equals(op1_loc));
350 return;
351 }
352
353 // (out := op1)
354 // out <=? op2
355 // if Nan jmp Nan_label
356 // if out is min jmp done
357 // if op2 is min jmp op2_label
358 // handle -0/+0
359 // jmp done
360 // Nan_label:
361 // out := NaN
362 // op2_label:
363 // out := op2
364 // done:
365 //
366 // This removes one jmp, but needs to copy one input (op1) to out.
367 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400368 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800369
370 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
371
Mark Mendell0c9497d2015-08-21 09:30:05 -0400372 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800373 if (is_double) {
374 __ ucomisd(out, op2);
375 } else {
376 __ ucomiss(out, op2);
377 }
378
379 __ j(Condition::kParityEven, &nan);
380
381 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
382 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
383
384 // Handle 0.0/-0.0.
385 if (is_min) {
386 if (is_double) {
387 __ orpd(out, op2);
388 } else {
389 __ orps(out, op2);
390 }
391 } else {
392 if (is_double) {
393 __ andpd(out, op2);
394 } else {
395 __ andps(out, op2);
396 }
397 }
398 __ jmp(&done);
399
400 // NaN handling.
401 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800402 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400403 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800404 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400405 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800406 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800407 __ jmp(&done);
408
409 // out := op2;
410 __ Bind(&op2_label);
411 if (is_double) {
412 __ movsd(out, op2);
413 } else {
414 __ movss(out, op2);
415 }
416
417 // Done.
418 __ Bind(&done);
419}
420
Mark Mendellf55c3e02015-03-26 21:07:46 -0400421static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800422 LocationSummary* locations = new (arena) LocationSummary(invoke,
423 LocationSummary::kNoCall,
424 kIntrinsified);
425 locations->SetInAt(0, Location::RequiresFpuRegister());
426 locations->SetInAt(1, Location::RequiresFpuRegister());
427 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
428 // the second input to be the output (we can simply swap inputs).
429 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800430}
431
432void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400433 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800434}
435
436void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000437 GenMinMaxFP(
438 invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800439}
440
441void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400442 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800443}
444
445void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000446 GenMinMaxFP(
447 invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800448}
449
450void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400451 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800452}
453
454void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000455 GenMinMaxFP(
456 invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800457}
458
459void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400460 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800461}
462
463void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000464 GenMinMaxFP(
465 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800466}
467
468static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
469 X86_64Assembler* assembler) {
470 Location op1_loc = locations->InAt(0);
471 Location op2_loc = locations->InAt(1);
472
473 // Shortcut for same input locations.
474 if (op1_loc.Equals(op2_loc)) {
475 // Can return immediately, as op1_loc == out_loc.
476 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
477 // a copy here.
478 DCHECK(locations->Out().Equals(op1_loc));
479 return;
480 }
481
482 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
483 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
484
485 // (out := op1)
486 // out <=? op2
487 // if out is min jmp done
488 // out := op2
489 // done:
490
491 if (is_long) {
492 __ cmpq(out, op2);
493 } else {
494 __ cmpl(out, op2);
495 }
496
497 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
498}
499
500static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
501 LocationSummary* locations = new (arena) LocationSummary(invoke,
502 LocationSummary::kNoCall,
503 kIntrinsified);
504 locations->SetInAt(0, Location::RequiresRegister());
505 locations->SetInAt(1, Location::RequiresRegister());
506 locations->SetOut(Location::SameAsFirstInput());
507}
508
509void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
510 CreateIntIntToIntLocations(arena_, invoke);
511}
512
513void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000514 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800515}
516
517void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
518 CreateIntIntToIntLocations(arena_, invoke);
519}
520
521void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000522 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800523}
524
525void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
526 CreateIntIntToIntLocations(arena_, invoke);
527}
528
529void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000530 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800531}
532
533void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
534 CreateIntIntToIntLocations(arena_, invoke);
535}
536
537void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000538 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800539}
540
541static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
542 LocationSummary* locations = new (arena) LocationSummary(invoke,
543 LocationSummary::kNoCall,
544 kIntrinsified);
545 locations->SetInAt(0, Location::RequiresFpuRegister());
546 locations->SetOut(Location::RequiresFpuRegister());
547}
548
549void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
550 CreateFPToFPLocations(arena_, invoke);
551}
552
553void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
554 LocationSummary* locations = invoke->GetLocations();
555 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
556 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
557
558 GetAssembler()->sqrtsd(out, in);
559}
560
Mark Mendellfb8d2792015-03-31 22:16:59 -0400561static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100562 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400563
564 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100565 codegen->GenerateStaticOrDirectCall(
566 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400567 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
568
569 // Copy the result back to the expected output.
570 Location out = invoke->GetLocations()->Out();
571 if (out.IsValid()) {
572 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700573 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400574 }
575}
576
577static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
578 HInvoke* invoke,
579 CodeGeneratorX86_64* codegen) {
580 // Do we have instruction support?
581 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
582 CreateFPToFPLocations(arena, invoke);
583 return;
584 }
585
586 // We have to fall back to a call to the intrinsic.
587 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100588 LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400589 InvokeRuntimeCallingConvention calling_convention;
590 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
591 locations->SetOut(Location::FpuRegisterLocation(XMM0));
592 // Needs to be RDI for the invoke.
593 locations->AddTemp(Location::RegisterLocation(RDI));
594}
595
596static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
597 HInvoke* invoke,
598 X86_64Assembler* assembler,
599 int round_mode) {
600 LocationSummary* locations = invoke->GetLocations();
601 if (locations->WillCall()) {
602 InvokeOutOfLineIntrinsic(codegen, invoke);
603 } else {
604 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
605 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
606 __ roundsd(out, in, Immediate(round_mode));
607 }
608}
609
610void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
611 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
612}
613
614void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
615 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
616}
617
618void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
619 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
620}
621
622void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
623 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
624}
625
626void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
627 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
628}
629
630void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
631 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
632}
633
634static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
635 HInvoke* invoke,
636 CodeGeneratorX86_64* codegen) {
637 // Do we have instruction support?
638 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
639 LocationSummary* locations = new (arena) LocationSummary(invoke,
640 LocationSummary::kNoCall,
641 kIntrinsified);
642 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600643 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400644 locations->AddTemp(Location::RequiresFpuRegister());
Aart Bik349f3882016-08-02 15:40:56 -0700645 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400646 return;
647 }
648
649 // We have to fall back to a call to the intrinsic.
650 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100651 LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400652 InvokeRuntimeCallingConvention calling_convention;
653 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
654 locations->SetOut(Location::RegisterLocation(RAX));
655 // Needs to be RDI for the invoke.
656 locations->AddTemp(Location::RegisterLocation(RDI));
657}
658
659void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
Aart Bik349f3882016-08-02 15:40:56 -0700660 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400661}
662
663void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
664 LocationSummary* locations = invoke->GetLocations();
665 if (locations->WillCall()) {
666 InvokeOutOfLineIntrinsic(codegen_, invoke);
667 return;
668 }
669
Mark Mendellfb8d2792015-03-31 22:16:59 -0400670 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
671 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700672 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
673 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
674 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400675 X86_64Assembler* assembler = GetAssembler();
676
Aart Bik349f3882016-08-02 15:40:56 -0700677 // Since no direct x86 rounding instruction matches the required semantics,
678 // this intrinsic is implemented as follows:
679 // result = floor(in);
680 // if (in - result >= 0.5f)
681 // result = result + 1.0f;
682 __ movss(t2, in);
683 __ roundss(t1, in, Immediate(1));
684 __ subss(t2, t1);
685 __ comiss(t2, codegen_->LiteralFloatAddress(0.5f));
686 __ j(kBelow, &skip_incr);
687 __ addss(t1, codegen_->LiteralFloatAddress(1.0f));
688 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400689
Aart Bik349f3882016-08-02 15:40:56 -0700690 // Final conversion to an integer. Unfortunately this also does not have a
691 // direct x86 instruction, since NaN should map to 0 and large positive
692 // values need to be clipped to the extreme value.
693 codegen_->Load32BitValue(out, kPrimIntMax);
694 __ cvtsi2ss(t2, out);
695 __ comiss(t1, t2);
696 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
697 __ movl(out, Immediate(0)); // does not change flags
698 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
699 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400700 __ Bind(&done);
701}
702
703void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
Aart Bik349f3882016-08-02 15:40:56 -0700704 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400705}
706
707void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
708 LocationSummary* locations = invoke->GetLocations();
709 if (locations->WillCall()) {
710 InvokeOutOfLineIntrinsic(codegen_, invoke);
711 return;
712 }
713
Mark Mendellfb8d2792015-03-31 22:16:59 -0400714 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
715 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700716 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
717 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
718 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400719 X86_64Assembler* assembler = GetAssembler();
720
Aart Bik349f3882016-08-02 15:40:56 -0700721 // Since no direct x86 rounding instruction matches the required semantics,
722 // this intrinsic is implemented as follows:
723 // result = floor(in);
724 // if (in - result >= 0.5)
725 // result = result + 1.0f;
726 __ movsd(t2, in);
727 __ roundsd(t1, in, Immediate(1));
728 __ subsd(t2, t1);
729 __ comisd(t2, codegen_->LiteralDoubleAddress(0.5));
730 __ j(kBelow, &skip_incr);
731 __ addsd(t1, codegen_->LiteralDoubleAddress(1.0f));
732 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400733
Aart Bik349f3882016-08-02 15:40:56 -0700734 // Final conversion to an integer. Unfortunately this also does not have a
735 // direct x86 instruction, since NaN should map to 0 and large positive
736 // values need to be clipped to the extreme value.
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600737 codegen_->Load64BitValue(out, kPrimLongMax);
Aart Bik349f3882016-08-02 15:40:56 -0700738 __ cvtsi2sd(t2, out, /* is64bit */ true);
739 __ comisd(t1, t2);
740 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
741 __ movl(out, Immediate(0)); // does not change flags, implicit zero extension to 64-bit
742 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
743 __ cvttsd2si(out, t1, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400744 __ Bind(&done);
745}
746
Mark Mendella4f12202015-08-06 15:23:34 -0400747static void CreateFPToFPCallLocations(ArenaAllocator* arena,
748 HInvoke* invoke) {
749 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100750 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400751 kIntrinsified);
752 InvokeRuntimeCallingConvention calling_convention;
753 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
754 locations->SetOut(Location::FpuRegisterLocation(XMM0));
755
756 // We have to ensure that the native code doesn't clobber the XMM registers which are
757 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
758 // saved in the prologue and properly restored.
759 for (auto fp_reg : non_volatile_xmm_regs) {
760 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
761 }
762}
763
764static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
765 QuickEntrypointEnum entry) {
766 LocationSummary* locations = invoke->GetLocations();
767 DCHECK(locations->WillCall());
768 DCHECK(invoke->IsInvokeStaticOrDirect());
769 X86_64Assembler* assembler = codegen->GetAssembler();
770
Andreas Gampe542451c2016-07-26 09:02:02 -0700771 __ gs()->call(Address::Absolute(GetThreadOffset<kX86_64PointerSize>(entry), true));
Mark Mendella4f12202015-08-06 15:23:34 -0400772 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
773}
774
775void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
776 CreateFPToFPCallLocations(arena_, invoke);
777}
778
779void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
780 GenFPToFPCall(invoke, codegen_, kQuickCos);
781}
782
783void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
784 CreateFPToFPCallLocations(arena_, invoke);
785}
786
787void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
788 GenFPToFPCall(invoke, codegen_, kQuickSin);
789}
790
791void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
792 CreateFPToFPCallLocations(arena_, invoke);
793}
794
795void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
796 GenFPToFPCall(invoke, codegen_, kQuickAcos);
797}
798
799void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
800 CreateFPToFPCallLocations(arena_, invoke);
801}
802
803void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
804 GenFPToFPCall(invoke, codegen_, kQuickAsin);
805}
806
807void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
808 CreateFPToFPCallLocations(arena_, invoke);
809}
810
811void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
812 GenFPToFPCall(invoke, codegen_, kQuickAtan);
813}
814
815void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
816 CreateFPToFPCallLocations(arena_, invoke);
817}
818
819void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
820 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
821}
822
823void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
824 CreateFPToFPCallLocations(arena_, invoke);
825}
826
827void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
828 GenFPToFPCall(invoke, codegen_, kQuickCosh);
829}
830
831void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
832 CreateFPToFPCallLocations(arena_, invoke);
833}
834
835void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
836 GenFPToFPCall(invoke, codegen_, kQuickExp);
837}
838
839void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
840 CreateFPToFPCallLocations(arena_, invoke);
841}
842
843void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
844 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
845}
846
847void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
848 CreateFPToFPCallLocations(arena_, invoke);
849}
850
851void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
852 GenFPToFPCall(invoke, codegen_, kQuickLog);
853}
854
855void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
856 CreateFPToFPCallLocations(arena_, invoke);
857}
858
859void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
860 GenFPToFPCall(invoke, codegen_, kQuickLog10);
861}
862
863void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
864 CreateFPToFPCallLocations(arena_, invoke);
865}
866
867void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
868 GenFPToFPCall(invoke, codegen_, kQuickSinh);
869}
870
871void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
872 CreateFPToFPCallLocations(arena_, invoke);
873}
874
875void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
876 GenFPToFPCall(invoke, codegen_, kQuickTan);
877}
878
879void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
880 CreateFPToFPCallLocations(arena_, invoke);
881}
882
883void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
884 GenFPToFPCall(invoke, codegen_, kQuickTanh);
885}
886
887static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
888 HInvoke* invoke) {
889 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100890 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400891 kIntrinsified);
892 InvokeRuntimeCallingConvention calling_convention;
893 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
894 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
895 locations->SetOut(Location::FpuRegisterLocation(XMM0));
896
897 // We have to ensure that the native code doesn't clobber the XMM registers which are
898 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
899 // saved in the prologue and properly restored.
900 for (auto fp_reg : non_volatile_xmm_regs) {
901 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
902 }
903}
904
905void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
906 CreateFPFPToFPCallLocations(arena_, invoke);
907}
908
909void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
910 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
911}
912
913void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
914 CreateFPFPToFPCallLocations(arena_, invoke);
915}
916
917void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
918 GenFPToFPCall(invoke, codegen_, kQuickHypot);
919}
920
921void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
922 CreateFPFPToFPCallLocations(arena_, invoke);
923}
924
925void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
926 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
927}
928
Mark Mendell6bc53a92015-07-01 14:26:52 -0400929void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
930 // Check to see if we have known failures that will cause us to have to bail out
931 // to the runtime, and just generate the runtime call directly.
932 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
933 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
934
935 // The positions must be non-negative.
936 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
937 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
938 // We will have to fail anyways.
939 return;
940 }
941
942 // The length must be > 0.
943 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
944 if (length != nullptr) {
945 int32_t len = length->GetValue();
946 if (len < 0) {
947 // Just call as normal.
948 return;
949 }
950 }
951
952 LocationSummary* locations = new (arena_) LocationSummary(invoke,
953 LocationSummary::kCallOnSlowPath,
954 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100955 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400956 locations->SetInAt(0, Location::RequiresRegister());
957 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
958 locations->SetInAt(2, Location::RequiresRegister());
959 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
960 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
961
962 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
963 locations->AddTemp(Location::RegisterLocation(RSI));
964 locations->AddTemp(Location::RegisterLocation(RDI));
965 locations->AddTemp(Location::RegisterLocation(RCX));
966}
967
968static void CheckPosition(X86_64Assembler* assembler,
969 Location pos,
970 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100971 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700972 SlowPathCode* slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100973 CpuRegister temp,
974 bool length_is_input_length = false) {
975 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400976 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
977
978 if (pos.IsConstant()) {
979 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
980 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100981 if (!length_is_input_length) {
982 // Check that length(input) >= length.
983 if (length.IsConstant()) {
984 __ cmpl(Address(input, length_offset),
985 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
986 } else {
987 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
988 }
989 __ j(kLess, slow_path->GetEntryLabel());
990 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400991 } else {
992 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100993 __ movl(temp, Address(input, length_offset));
994 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400995 __ j(kLess, slow_path->GetEntryLabel());
996
997 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100998 if (length.IsConstant()) {
999 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1000 } else {
1001 __ cmpl(temp, length.AsRegister<CpuRegister>());
1002 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001003 __ j(kLess, slow_path->GetEntryLabel());
1004 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001005 } else if (length_is_input_length) {
1006 // The only way the copy can succeed is if pos is zero.
1007 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1008 __ testl(pos_reg, pos_reg);
1009 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001010 } else {
1011 // Check that pos >= 0.
1012 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1013 __ testl(pos_reg, pos_reg);
1014 __ j(kLess, slow_path->GetEntryLabel());
1015
1016 // Check that pos <= length(input).
1017 __ cmpl(Address(input, length_offset), pos_reg);
1018 __ j(kLess, slow_path->GetEntryLabel());
1019
1020 // Check that (length(input) - pos) >= length.
1021 __ movl(temp, Address(input, length_offset));
1022 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001023 if (length.IsConstant()) {
1024 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1025 } else {
1026 __ cmpl(temp, length.AsRegister<CpuRegister>());
1027 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001028 __ j(kLess, slow_path->GetEntryLabel());
1029 }
1030}
1031
1032void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1033 X86_64Assembler* assembler = GetAssembler();
1034 LocationSummary* locations = invoke->GetLocations();
1035
1036 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001037 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001038 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001039 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001040 Location length = locations->InAt(4);
1041
1042 // Temporaries that we need for MOVSW.
1043 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1044 DCHECK_EQ(src_base.AsRegister(), RSI);
1045 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1046 DCHECK_EQ(dest_base.AsRegister(), RDI);
1047 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1048 DCHECK_EQ(count.AsRegister(), RCX);
1049
Andreas Gampe85b62f22015-09-09 13:15:38 -07001050 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001051 codegen_->AddSlowPath(slow_path);
1052
1053 // Bail out if the source and destination are the same.
1054 __ cmpl(src, dest);
1055 __ j(kEqual, slow_path->GetEntryLabel());
1056
1057 // Bail out if the source is null.
1058 __ testl(src, src);
1059 __ j(kEqual, slow_path->GetEntryLabel());
1060
1061 // Bail out if the destination is null.
1062 __ testl(dest, dest);
1063 __ j(kEqual, slow_path->GetEntryLabel());
1064
1065 // If the length is negative, bail out.
1066 // We have already checked in the LocationsBuilder for the constant case.
1067 if (!length.IsConstant()) {
1068 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1069 __ j(kLess, slow_path->GetEntryLabel());
1070 }
1071
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001072 // Validity checks: source. Use src_base as a temporary register.
1073 CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001074
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001075 // Validity checks: dest. Use src_base as a temporary register.
1076 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001077
Mark Mendell6bc53a92015-07-01 14:26:52 -04001078 // We need the count in RCX.
1079 if (length.IsConstant()) {
1080 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1081 } else {
1082 __ movl(count, length.AsRegister<CpuRegister>());
1083 }
1084
Mark Mendell6bc53a92015-07-01 14:26:52 -04001085 // Okay, everything checks out. Finally time to do the copy.
1086 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1087 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1088 DCHECK_EQ(char_size, 2u);
1089
1090 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1091
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001092 if (src_pos.IsConstant()) {
1093 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1094 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001095 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001096 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001097 ScaleFactor::TIMES_2, data_offset));
1098 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001099 if (dest_pos.IsConstant()) {
1100 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1101 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001102 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001103 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001104 ScaleFactor::TIMES_2, data_offset));
1105 }
1106
1107 // Do the move.
1108 __ rep_movsw();
1109
1110 __ Bind(slow_path->GetExitLabel());
1111}
1112
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001113
1114void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001115 // The only read barrier implementation supporting the
1116 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1117 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01001118 return;
1119 }
1120
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001121 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001122}
1123
1124void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001125 // The only read barrier implementation supporting the
1126 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1127 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001128
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001129 X86_64Assembler* assembler = GetAssembler();
1130 LocationSummary* locations = invoke->GetLocations();
1131
1132 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1133 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1134 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1135 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001136 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001137
1138 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1139 Location src_pos = locations->InAt(1);
1140 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1141 Location dest_pos = locations->InAt(3);
1142 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +01001143 Location temp1_loc = locations->GetTemp(0);
1144 CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
1145 Location temp2_loc = locations->GetTemp(1);
1146 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
1147 Location temp3_loc = locations->GetTemp(2);
1148 CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
1149 Location TMP_loc = Location::RegisterLocation(TMP);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001150
Roland Levillain0b671c02016-08-19 12:02:34 +01001151 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1152 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001153
Roland Levillainebea3d22016-04-12 15:42:57 +01001154 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001155 SystemArrayCopyOptimizations optimizations(invoke);
1156
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001157 // If source and destination are the same, we go to slow path if we need to do
1158 // forward copying.
1159 if (src_pos.IsConstant()) {
1160 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1161 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001162 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1163 if (optimizations.GetDestinationIsSource()) {
1164 // Checked when building locations.
1165 DCHECK_GE(src_pos_constant, dest_pos_constant);
1166 } else if (src_pos_constant < dest_pos_constant) {
1167 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001168 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001169 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001170 } else {
1171 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001172 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001173 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001174 }
1175 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001176 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001177 }
1178 } else {
1179 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001180 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001181 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001182 }
1183 if (dest_pos.IsConstant()) {
1184 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1185 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001186 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001187 } else {
1188 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001189 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001190 }
1191 }
1192
Roland Levillainebea3d22016-04-12 15:42:57 +01001193 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001194
1195 if (!optimizations.GetSourceIsNotNull()) {
1196 // Bail out if the source is null.
1197 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01001198 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001199 }
1200
1201 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1202 // Bail out if the destination is null.
1203 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001204 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001205 }
1206
1207 // If the length is negative, bail out.
1208 // We have already checked in the LocationsBuilder for the constant case.
1209 if (!length.IsConstant() &&
1210 !optimizations.GetCountIsSourceLength() &&
1211 !optimizations.GetCountIsDestinationLength()) {
1212 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001213 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001214 }
1215
1216 // Validity checks: source.
1217 CheckPosition(assembler,
1218 src_pos,
1219 src,
1220 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001221 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001222 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001223 optimizations.GetCountIsSourceLength());
1224
1225 // Validity checks: dest.
1226 CheckPosition(assembler,
1227 dest_pos,
1228 dest,
1229 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001230 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001231 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001232 optimizations.GetCountIsDestinationLength());
1233
1234 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1235 // Check whether all elements of the source array are assignable to the component
1236 // type of the destination array. We do two checks: the classes are the same,
1237 // or the destination is Object[]. If none of these checks succeed, we go to the
1238 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01001239
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001240 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +01001241 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1242 // /* HeapReference<Class> */ temp1 = dest->klass_
1243 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001244 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001245 // Register `temp1` is not trashed by the read barrier emitted
1246 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1247 // method produces a call to a ReadBarrierMarkRegX entry point,
1248 // which saves all potentially live registers, including
1249 // temporaries such a `temp1`.
1250 // /* HeapReference<Class> */ temp2 = src->klass_
1251 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001252 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001253 // If heap poisoning is enabled, `temp1` and `temp2` have been
1254 // unpoisoned by the the previous calls to
1255 // GenerateFieldLoadWithBakerReadBarrier.
1256 } else {
1257 // /* HeapReference<Class> */ temp1 = dest->klass_
1258 __ movl(temp1, Address(dest, class_offset));
1259 // /* HeapReference<Class> */ temp2 = src->klass_
1260 __ movl(temp2, Address(src, class_offset));
1261 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1262 !optimizations.GetSourceIsNonPrimitiveArray()) {
1263 // One or two of the references need to be unpoisoned. Unpoison them
1264 // both to make the identity check valid.
1265 __ MaybeUnpoisonHeapReference(temp1);
1266 __ MaybeUnpoisonHeapReference(temp2);
1267 did_unpoison = true;
1268 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001269 }
1270
1271 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1272 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001273 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1274 // /* HeapReference<Class> */ TMP = temp1->component_type_
1275 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001276 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001277 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1278 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1279 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1280 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1281 } else {
1282 // /* HeapReference<Class> */ TMP = temp1->component_type_
1283 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1284 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1285 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1286 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1287 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001288 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001289 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001290 }
1291
1292 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1293 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001294 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1295 // For the same reason given earlier, `temp1` is not trashed by the
1296 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1297 // /* HeapReference<Class> */ TMP = temp2->component_type_
1298 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001299 invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001300 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1301 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1302 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1303 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1304 } else {
1305 // /* HeapReference<Class> */ TMP = temp2->component_type_
1306 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1307 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1308 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1309 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1310 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001311 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001312 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001313 }
1314
1315 __ cmpl(temp1, temp2);
1316
1317 if (optimizations.GetDestinationIsTypedObjectArray()) {
1318 NearLabel do_copy;
1319 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001320 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1321 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1322 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001323 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001324 // We do not need to emit a read barrier for the following
1325 // heap reference load, as `temp1` is only used in a
1326 // comparison with null below, and this reference is not
1327 // kept afterwards.
1328 __ cmpl(Address(temp1, super_offset), Immediate(0));
1329 } else {
1330 if (!did_unpoison) {
1331 __ MaybeUnpoisonHeapReference(temp1);
1332 }
1333 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1334 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001335 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001336 // No need to unpoison the following heap reference load, as
1337 // we're comparing against null.
1338 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001339 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001340 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001341 __ Bind(&do_copy);
1342 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001343 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001344 }
1345 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1346 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1347 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001348 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1349 // /* HeapReference<Class> */ temp1 = src->klass_
1350 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001351 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001352 // /* HeapReference<Class> */ TMP = temp1->component_type_
1353 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001354 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001355 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1356 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1357 } else {
1358 // /* HeapReference<Class> */ temp1 = src->klass_
1359 __ movl(temp1, Address(src, class_offset));
1360 __ MaybeUnpoisonHeapReference(temp1);
1361 // /* HeapReference<Class> */ TMP = temp1->component_type_
1362 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1363 // No need to unpoison `TMP` now, as we're comparing against null.
1364 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1365 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1366 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1367 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001368 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001369 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001370 }
1371
1372 // Compute base source address, base destination address, and end source address.
1373
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001374 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001375 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1376 if (src_pos.IsConstant()) {
1377 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1378 __ leal(temp1, Address(src, element_size * constant + offset));
1379 } else {
1380 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1381 }
1382
1383 if (dest_pos.IsConstant()) {
1384 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1385 __ leal(temp2, Address(dest, element_size * constant + offset));
1386 } else {
1387 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1388 }
1389
1390 if (length.IsConstant()) {
1391 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1392 __ leal(temp3, Address(temp1, element_size * constant));
1393 } else {
1394 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1395 }
1396
Roland Levillain0b671c02016-08-19 12:02:34 +01001397 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1398 // SystemArrayCopy implementation for Baker read barriers (see
1399 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1400 //
1401 // if (src_ptr != end_ptr) {
1402 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1403 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1404 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
1405 // if (is_gray) {
1406 // // Slow-path copy.
1407 // do {
1408 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1409 // } while (src_ptr != end_ptr)
1410 // } else {
1411 // // Fast-path copy.
1412 // do {
1413 // *dest_ptr++ = *src_ptr++;
1414 // } while (src_ptr != end_ptr)
1415 // }
1416 // }
1417
1418 NearLabel loop, done;
1419
1420 // Don't enter copy loop if `length == 0`.
1421 __ cmpl(temp1, temp3);
1422 __ j(kEqual, &done);
1423
Vladimir Marko953437b2016-08-24 08:30:46 +00001424 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1425 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
1426 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
1427 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
1428 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
1429 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
1430 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
1431
1432 // if (rb_state == ReadBarrier::gray_ptr_)
1433 // goto slow_path;
1434 // At this point, just do the "if" and make sure that flags are preserved until the branch.
1435 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01001436
1437 // Load fence to prevent load-load reordering.
1438 // Note that this is a no-op, thanks to the x86-64 memory model.
1439 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1440
1441 // Slow path used to copy array when `src` is gray.
1442 SlowPathCode* read_barrier_slow_path =
1443 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
1444 codegen_->AddSlowPath(read_barrier_slow_path);
1445
Vladimir Marko953437b2016-08-24 08:30:46 +00001446 // We have done the "if" of the gray bit check above, now branch based on the flags.
1447 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01001448
1449 // Fast-path copy.
1450 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1451 // poison/unpoison.
1452 __ Bind(&loop);
1453 __ movl(CpuRegister(TMP), Address(temp1, 0));
1454 __ movl(Address(temp2, 0), CpuRegister(TMP));
1455 __ addl(temp1, Immediate(element_size));
1456 __ addl(temp2, Immediate(element_size));
1457 __ cmpl(temp1, temp3);
1458 __ j(kNotEqual, &loop);
1459
1460 __ Bind(read_barrier_slow_path->GetExitLabel());
1461 __ Bind(&done);
1462 } else {
1463 // Non read barrier code.
1464
1465 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1466 // poison/unpoison.
1467 NearLabel loop, done;
1468 __ cmpl(temp1, temp3);
1469 __ j(kEqual, &done);
1470 __ Bind(&loop);
1471 __ movl(CpuRegister(TMP), Address(temp1, 0));
1472 __ movl(Address(temp2, 0), CpuRegister(TMP));
1473 __ addl(temp1, Immediate(element_size));
1474 __ addl(temp2, Immediate(element_size));
1475 __ cmpl(temp1, temp3);
1476 __ j(kNotEqual, &loop);
1477 __ Bind(&done);
1478 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001479
1480 // We only need one card marking on the destination array.
1481 codegen_->MarkGCCard(temp1,
1482 temp2,
1483 dest,
1484 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001485 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001486
Roland Levillain0b671c02016-08-19 12:02:34 +01001487 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001488}
1489
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001490void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1491 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001492 LocationSummary::kCallOnMainAndSlowPath,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001493 kIntrinsified);
1494 InvokeRuntimeCallingConvention calling_convention;
1495 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1496 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1497 locations->SetOut(Location::RegisterLocation(RAX));
1498}
1499
1500void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1501 X86_64Assembler* assembler = GetAssembler();
1502 LocationSummary* locations = invoke->GetLocations();
1503
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001504 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001505 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001506
1507 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1508 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001509 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001510 codegen_->AddSlowPath(slow_path);
1511 __ j(kEqual, slow_path->GetEntryLabel());
1512
Andreas Gampe542451c2016-07-26 09:02:02 -07001513 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, pStringCompareTo),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001514 /* no_rip */ true));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001515 __ Bind(slow_path->GetExitLabel());
1516}
1517
Agi Csakif8cfb202015-08-13 17:54:54 -07001518void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1519 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1520 LocationSummary::kNoCall,
1521 kIntrinsified);
1522 locations->SetInAt(0, Location::RequiresRegister());
1523 locations->SetInAt(1, Location::RequiresRegister());
1524
1525 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1526 locations->AddTemp(Location::RegisterLocation(RCX));
1527 locations->AddTemp(Location::RegisterLocation(RDI));
1528
1529 // Set output, RSI needed for repe_cmpsq instruction anyways.
1530 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1531}
1532
1533void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1534 X86_64Assembler* assembler = GetAssembler();
1535 LocationSummary* locations = invoke->GetLocations();
1536
1537 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1538 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1539 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1540 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1541 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1542
Mark Mendell0c9497d2015-08-21 09:30:05 -04001543 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001544
1545 // Get offsets of count, value, and class fields within a string object.
1546 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1547 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1548 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1549
1550 // Note that the null check must have been done earlier.
1551 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1552
Vladimir Marko53b52002016-05-24 19:30:45 +01001553 StringEqualsOptimizations optimizations(invoke);
1554 if (!optimizations.GetArgumentNotNull()) {
1555 // Check if input is null, return false if it is.
1556 __ testl(arg, arg);
1557 __ j(kEqual, &return_false);
1558 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001559
Vladimir Marko53b52002016-05-24 19:30:45 +01001560 if (!optimizations.GetArgumentIsString()) {
1561 // Instanceof check for the argument by comparing class fields.
1562 // All string objects must have the same type since String cannot be subclassed.
1563 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1564 // If the argument is a string object, its class field must be equal to receiver's class field.
1565 __ movl(rcx, Address(str, class_offset));
1566 __ cmpl(rcx, Address(arg, class_offset));
1567 __ j(kNotEqual, &return_false);
1568 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001569
1570 // Reference equality check, return true if same reference.
1571 __ cmpl(str, arg);
1572 __ j(kEqual, &return_true);
1573
1574 // Load length of receiver string.
1575 __ movl(rcx, Address(str, count_offset));
1576 // Check if lengths are equal, return false if they're not.
1577 __ cmpl(rcx, Address(arg, count_offset));
1578 __ j(kNotEqual, &return_false);
1579 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001580 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001581
1582 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1583 __ leal(rsi, Address(str, value_offset));
1584 __ leal(rdi, Address(arg, value_offset));
1585
1586 // Divide string length by 4 and adjust for lengths not divisible by 4.
1587 __ addl(rcx, Immediate(3));
1588 __ shrl(rcx, Immediate(2));
1589
1590 // Assertions that must hold in order to compare strings 4 characters at a time.
1591 DCHECK_ALIGNED(value_offset, 8);
1592 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1593
1594 // Loop to compare strings four characters at a time starting at the beginning of the string.
1595 __ repe_cmpsq();
1596 // If strings are not equal, zero flag will be cleared.
1597 __ j(kNotEqual, &return_false);
1598
1599 // Return true and exit the function.
1600 // If loop does not result in returning false, we return true.
1601 __ Bind(&return_true);
1602 __ movl(rsi, Immediate(1));
1603 __ jmp(&end);
1604
1605 // Return false and exit the function.
1606 __ Bind(&return_false);
1607 __ xorl(rsi, rsi);
1608 __ Bind(&end);
1609}
1610
Andreas Gampe21030dd2015-05-07 14:46:15 -07001611static void CreateStringIndexOfLocations(HInvoke* invoke,
1612 ArenaAllocator* allocator,
1613 bool start_at_zero) {
1614 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1615 LocationSummary::kCallOnSlowPath,
1616 kIntrinsified);
1617 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1618 locations->SetInAt(0, Location::RegisterLocation(RDI));
1619 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1620 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1621 // of the instruction explicitly.
1622 // Note: This works as we don't clobber RAX anywhere.
1623 locations->SetInAt(1, Location::RegisterLocation(RAX));
1624 if (!start_at_zero) {
1625 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1626 }
1627 // As we clobber RDI during execution anyways, also use it as the output.
1628 locations->SetOut(Location::SameAsFirstInput());
1629
1630 // repne scasw uses RCX as the counter.
1631 locations->AddTemp(Location::RegisterLocation(RCX));
1632 // Need another temporary to be able to compute the result.
1633 locations->AddTemp(Location::RequiresRegister());
1634}
1635
1636static void GenerateStringIndexOf(HInvoke* invoke,
1637 X86_64Assembler* assembler,
1638 CodeGeneratorX86_64* codegen,
1639 ArenaAllocator* allocator,
1640 bool start_at_zero) {
1641 LocationSummary* locations = invoke->GetLocations();
1642
1643 // Note that the null check must have been done earlier.
1644 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1645
1646 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1647 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1648 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1649 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1650 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1651
1652 // Check our assumptions for registers.
1653 DCHECK_EQ(string_obj.AsRegister(), RDI);
1654 DCHECK_EQ(search_value.AsRegister(), RAX);
1655 DCHECK_EQ(counter.AsRegister(), RCX);
1656 DCHECK_EQ(out.AsRegister(), RDI);
1657
1658 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001659 // 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 -07001660 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001661 HInstruction* code_point = invoke->InputAt(1);
1662 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001663 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001664 std::numeric_limits<uint16_t>::max()) {
1665 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1666 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1667 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1668 codegen->AddSlowPath(slow_path);
1669 __ jmp(slow_path->GetEntryLabel());
1670 __ Bind(slow_path->GetExitLabel());
1671 return;
1672 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001673 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001674 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1675 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1676 codegen->AddSlowPath(slow_path);
1677 __ j(kAbove, slow_path->GetEntryLabel());
1678 }
1679
1680 // From here down, we know that we are looking for a char that fits in 16 bits.
1681 // Location of reference to data array within the String object.
1682 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1683 // Location of count within the String object.
1684 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1685
1686 // Load string length, i.e., the count field of the string.
1687 __ movl(string_length, Address(string_obj, count_offset));
1688
1689 // Do a length check.
1690 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001691 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001692 __ testl(string_length, string_length);
1693 __ j(kEqual, &not_found_label);
1694
1695 if (start_at_zero) {
1696 // Number of chars to scan is the same as the string length.
1697 __ movl(counter, string_length);
1698
1699 // Move to the start of the string.
1700 __ addq(string_obj, Immediate(value_offset));
1701 } else {
1702 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1703
1704 // Do a start_index check.
1705 __ cmpl(start_index, string_length);
1706 __ j(kGreaterEqual, &not_found_label);
1707
1708 // Ensure we have a start index >= 0;
1709 __ xorl(counter, counter);
1710 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001711 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001712
1713 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1714 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1715
1716 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1717 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1718 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1719 }
1720
1721 // Everything is set up for repne scasw:
1722 // * Comparison address in RDI.
1723 // * Counter in ECX.
1724 __ repne_scasw();
1725
1726 // Did we find a match?
1727 __ j(kNotEqual, &not_found_label);
1728
1729 // Yes, we matched. Compute the index of the result.
1730 __ subl(string_length, counter);
1731 __ leal(out, Address(string_length, -1));
1732
Mark Mendell0c9497d2015-08-21 09:30:05 -04001733 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001734 __ jmp(&done);
1735
1736 // Failed to match; return -1.
1737 __ Bind(&not_found_label);
1738 __ movl(out, Immediate(-1));
1739
1740 // And join up at the end.
1741 __ Bind(&done);
1742 if (slow_path != nullptr) {
1743 __ Bind(slow_path->GetExitLabel());
1744 }
1745}
1746
1747void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001748 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001749}
1750
1751void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001752 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001753}
1754
1755void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001756 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001757}
1758
1759void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001760 GenerateStringIndexOf(
1761 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001762}
1763
Jeff Hao848f70a2014-01-15 13:49:50 -08001764void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1765 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001766 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001767 kIntrinsified);
1768 InvokeRuntimeCallingConvention calling_convention;
1769 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1770 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1771 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1772 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1773 locations->SetOut(Location::RegisterLocation(RAX));
1774}
1775
1776void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1777 X86_64Assembler* assembler = GetAssembler();
1778 LocationSummary* locations = invoke->GetLocations();
1779
1780 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1781 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001782 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001783 codegen_->AddSlowPath(slow_path);
1784 __ j(kEqual, slow_path->GetEntryLabel());
1785
Andreas Gampe542451c2016-07-26 09:02:02 -07001786 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize,
1787 pAllocStringFromBytes),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001788 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001789 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001790 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1791 __ Bind(slow_path->GetExitLabel());
1792}
1793
1794void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1795 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001796 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001797 kIntrinsified);
1798 InvokeRuntimeCallingConvention calling_convention;
1799 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1800 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1801 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1802 locations->SetOut(Location::RegisterLocation(RAX));
1803}
1804
1805void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1806 X86_64Assembler* assembler = GetAssembler();
1807
Roland Levillaincc3839c2016-02-29 16:23:48 +00001808 // No need to emit code checking whether `locations->InAt(2)` is a null
1809 // pointer, as callers of the native method
1810 //
1811 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1812 //
1813 // all include a null check on `data` before calling that method.
Andreas Gampe542451c2016-07-26 09:02:02 -07001814 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize,
1815 pAllocStringFromChars),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001816 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001817 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001818 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1819}
1820
1821void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1822 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001823 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001824 kIntrinsified);
1825 InvokeRuntimeCallingConvention calling_convention;
1826 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1827 locations->SetOut(Location::RegisterLocation(RAX));
1828}
1829
1830void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1831 X86_64Assembler* assembler = GetAssembler();
1832 LocationSummary* locations = invoke->GetLocations();
1833
1834 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1835 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001836 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001837 codegen_->AddSlowPath(slow_path);
1838 __ j(kEqual, slow_path->GetEntryLabel());
1839
Andreas Gampe542451c2016-07-26 09:02:02 -07001840 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize,
1841 pAllocStringFromString),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001842 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001843 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001844 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1845 __ Bind(slow_path->GetExitLabel());
1846}
1847
Mark Mendell8f8926a2015-08-17 11:39:06 -04001848void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1849 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1850 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1851 LocationSummary::kNoCall,
1852 kIntrinsified);
1853 locations->SetInAt(0, Location::RequiresRegister());
1854 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1855 locations->SetInAt(2, Location::RequiresRegister());
1856 locations->SetInAt(3, Location::RequiresRegister());
1857 locations->SetInAt(4, Location::RequiresRegister());
1858
1859 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1860 locations->AddTemp(Location::RegisterLocation(RSI));
1861 locations->AddTemp(Location::RegisterLocation(RDI));
1862 locations->AddTemp(Location::RegisterLocation(RCX));
1863}
1864
1865void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1866 X86_64Assembler* assembler = GetAssembler();
1867 LocationSummary* locations = invoke->GetLocations();
1868
1869 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1870 // Location of data in char array buffer.
1871 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1872 // Location of char array data in string.
1873 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1874
1875 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1876 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1877 Location srcBegin = locations->InAt(1);
1878 int srcBegin_value =
1879 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1880 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1881 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1882 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1883
1884 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1885 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1886 DCHECK_EQ(char_size, 2u);
1887
1888 // Compute the address of the destination buffer.
1889 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1890
1891 // Compute the address of the source string.
1892 if (srcBegin.IsConstant()) {
1893 // Compute the address of the source string by adding the number of chars from
1894 // the source beginning to the value offset of a string.
1895 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1896 } else {
1897 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1898 ScaleFactor::TIMES_2, value_offset));
1899 }
1900
1901 // Compute the number of chars (words) to move.
1902 __ movl(CpuRegister(RCX), srcEnd);
1903 if (srcBegin.IsConstant()) {
1904 if (srcBegin_value != 0) {
1905 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1906 }
1907 } else {
1908 DCHECK(srcBegin.IsRegister());
1909 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1910 }
1911
1912 // Do the move.
1913 __ rep_movsw();
1914}
1915
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001916static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1917 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1918 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1919 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1920 // to avoid a SIGBUS.
1921 switch (size) {
1922 case Primitive::kPrimByte:
1923 __ movsxb(out, Address(address, 0));
1924 break;
1925 case Primitive::kPrimShort:
1926 __ movsxw(out, Address(address, 0));
1927 break;
1928 case Primitive::kPrimInt:
1929 __ movl(out, Address(address, 0));
1930 break;
1931 case Primitive::kPrimLong:
1932 __ movq(out, Address(address, 0));
1933 break;
1934 default:
1935 LOG(FATAL) << "Type not recognized for peek: " << size;
1936 UNREACHABLE();
1937 }
1938}
1939
1940void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1941 CreateIntToIntLocations(arena_, invoke);
1942}
1943
1944void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1945 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1946}
1947
1948void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1949 CreateIntToIntLocations(arena_, invoke);
1950}
1951
1952void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1953 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1954}
1955
1956void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1957 CreateIntToIntLocations(arena_, invoke);
1958}
1959
1960void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1961 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1962}
1963
1964void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1965 CreateIntToIntLocations(arena_, invoke);
1966}
1967
1968void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1969 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1970}
1971
1972static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1973 LocationSummary* locations = new (arena) LocationSummary(invoke,
1974 LocationSummary::kNoCall,
1975 kIntrinsified);
1976 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001977 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001978}
1979
1980static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1981 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001982 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001983 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1984 // to avoid a SIGBUS.
1985 switch (size) {
1986 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001987 if (value.IsConstant()) {
1988 __ movb(Address(address, 0),
1989 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1990 } else {
1991 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1992 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001993 break;
1994 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001995 if (value.IsConstant()) {
1996 __ movw(Address(address, 0),
1997 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1998 } else {
1999 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
2000 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002001 break;
2002 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04002003 if (value.IsConstant()) {
2004 __ movl(Address(address, 0),
2005 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2006 } else {
2007 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
2008 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002009 break;
2010 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04002011 if (value.IsConstant()) {
2012 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
2013 DCHECK(IsInt<32>(v));
2014 int32_t v_32 = v;
2015 __ movq(Address(address, 0), Immediate(v_32));
2016 } else {
2017 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
2018 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002019 break;
2020 default:
2021 LOG(FATAL) << "Type not recognized for poke: " << size;
2022 UNREACHABLE();
2023 }
2024}
2025
2026void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2027 CreateIntIntToVoidLocations(arena_, invoke);
2028}
2029
2030void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2031 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
2032}
2033
2034void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2035 CreateIntIntToVoidLocations(arena_, invoke);
2036}
2037
2038void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2039 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2040}
2041
2042void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2043 CreateIntIntToVoidLocations(arena_, invoke);
2044}
2045
2046void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2047 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2048}
2049
2050void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2051 CreateIntIntToVoidLocations(arena_, invoke);
2052}
2053
2054void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2055 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
2056}
2057
2058void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2059 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2060 LocationSummary::kNoCall,
2061 kIntrinsified);
2062 locations->SetOut(Location::RequiresRegister());
2063}
2064
2065void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2066 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07002067 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002068 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002069}
2070
Roland Levillain0d5a2812015-11-13 10:07:31 +00002071static void GenUnsafeGet(HInvoke* invoke,
2072 Primitive::Type type,
2073 bool is_volatile ATTRIBUTE_UNUSED,
2074 CodeGeneratorX86_64* codegen) {
2075 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
2076 LocationSummary* locations = invoke->GetLocations();
2077 Location base_loc = locations->InAt(1);
2078 CpuRegister base = base_loc.AsRegister<CpuRegister>();
2079 Location offset_loc = locations->InAt(2);
2080 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
2081 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002082 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002083
Andreas Gampe878d58c2015-01-15 23:24:00 -08002084 switch (type) {
2085 case Primitive::kPrimInt:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002086 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002087 break;
2088
2089 case Primitive::kPrimNot: {
2090 if (kEmitCompilerReadBarrier) {
2091 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002092 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2093 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002094 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002095 } else {
2096 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2097 codegen->GenerateReadBarrierSlow(
2098 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2099 }
2100 } else {
2101 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2102 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002103 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002104 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002105 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002106
2107 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002108 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002109 break;
2110
2111 default:
2112 LOG(FATAL) << "Unsupported op size " << type;
2113 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002114 }
2115}
2116
Vladimir Marko953437b2016-08-24 08:30:46 +00002117static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002118 bool can_call = kEmitCompilerReadBarrier &&
2119 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2120 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002121 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00002122 can_call ?
2123 LocationSummary::kCallOnSlowPath :
2124 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002125 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002126 if (can_call && kUseBakerReadBarrier) {
2127 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
2128 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002129 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002130 locations->SetInAt(1, Location::RequiresRegister());
2131 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002132 locations->SetOut(Location::RequiresRegister(),
2133 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002134}
2135
2136void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002137 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002138}
2139void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002140 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002141}
2142void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002143 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002144}
2145void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002146 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002147}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002148void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002149 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002150}
2151void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002152 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002153}
2154
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002155
2156void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002157 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002158}
2159void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002160 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002161}
2162void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002163 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002164}
2165void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002166 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002167}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002168void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002169 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002170}
2171void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002172 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002173}
2174
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002175
2176static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
2177 Primitive::Type type,
2178 HInvoke* invoke) {
2179 LocationSummary* locations = new (arena) LocationSummary(invoke,
2180 LocationSummary::kNoCall,
2181 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002182 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002183 locations->SetInAt(1, Location::RequiresRegister());
2184 locations->SetInAt(2, Location::RequiresRegister());
2185 locations->SetInAt(3, Location::RequiresRegister());
2186 if (type == Primitive::kPrimNot) {
2187 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002188 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002189 locations->AddTemp(Location::RequiresRegister());
2190 }
2191}
2192
2193void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2194 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2195}
2196void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2197 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2198}
2199void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2200 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2201}
2202void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2203 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2204}
2205void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2206 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2207}
2208void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2209 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2210}
2211void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2212 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2213}
2214void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2215 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2216}
2217void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2218 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2219}
2220
2221// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2222// memory model.
2223static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2224 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002225 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002226 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2227 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2228 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2229
2230 if (type == Primitive::kPrimLong) {
2231 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002232 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2233 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2234 __ movl(temp, value);
2235 __ PoisonHeapReference(temp);
2236 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002237 } else {
2238 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2239 }
2240
2241 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002242 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002243 }
2244
2245 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002246 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002247 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2248 locations->GetTemp(1).AsRegister<CpuRegister>(),
2249 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002250 value,
2251 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002252 }
2253}
2254
2255void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002256 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002257}
2258void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002259 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002260}
2261void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002262 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002263}
2264void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002265 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002266}
2267void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002268 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002269}
2270void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002271 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002272}
2273void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002274 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002275}
2276void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002277 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002278}
2279void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002280 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002281}
2282
Mark Mendell58d25fd2015-04-03 14:52:31 -04002283static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2284 HInvoke* invoke) {
2285 LocationSummary* locations = new (arena) LocationSummary(invoke,
2286 LocationSummary::kNoCall,
2287 kIntrinsified);
2288 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2289 locations->SetInAt(1, Location::RequiresRegister());
2290 locations->SetInAt(2, Location::RequiresRegister());
2291 // expected value must be in EAX/RAX.
2292 locations->SetInAt(3, Location::RegisterLocation(RAX));
2293 locations->SetInAt(4, Location::RequiresRegister());
2294
2295 locations->SetOut(Location::RequiresRegister());
2296 if (type == Primitive::kPrimNot) {
2297 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002298 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002299 locations->AddTemp(Location::RequiresRegister());
2300 }
2301}
2302
2303void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2304 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2305}
2306
2307void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2308 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2309}
2310
2311void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002312 // The UnsafeCASObject intrinsic is missing a read barrier, and
2313 // therefore sometimes does not work as expected (b/25883050).
2314 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01002315 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00002316 //
Roland Levillain3d312422016-06-23 13:53:42 +01002317 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
Roland Levillain391b8662015-12-18 11:43:38 +00002318 // this intrinsic.
2319 if (kEmitCompilerReadBarrier) {
2320 return;
2321 }
2322
Mark Mendell58d25fd2015-04-03 14:52:31 -04002323 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2324}
2325
2326static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002327 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002328 LocationSummary* locations = invoke->GetLocations();
2329
2330 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2331 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2332 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002333 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002334 DCHECK_EQ(expected.AsRegister(), RAX);
2335 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
2336 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2337
Roland Levillainb488b782015-10-22 11:38:49 +01002338 if (type == Primitive::kPrimNot) {
2339 // Mark card for object assuming new value is stored.
2340 bool value_can_be_null = true; // TODO: Worth finding out this information?
2341 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2342 locations->GetTemp(1).AsRegister<CpuRegister>(),
2343 base,
2344 value,
2345 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01002346
Roland Levillainb488b782015-10-22 11:38:49 +01002347 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2348 Register value_reg = value.AsRegister();
2349 if (kPoisonHeapReferences) {
2350 if (base_equals_value) {
2351 // If `base` and `value` are the same register location, move
2352 // `value_reg` to a temporary register. This way, poisoning
2353 // `value_reg` won't invalidate `base`.
2354 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
2355 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002356 }
Roland Levillainb488b782015-10-22 11:38:49 +01002357
2358 // Check that the register allocator did not assign the location
2359 // of `expected` (RAX) to `value` nor to `base`, so that heap
2360 // poisoning (when enabled) works as intended below.
2361 // - If `value` were equal to `expected`, both references would
2362 // be poisoned twice, meaning they would not be poisoned at
2363 // all, as heap poisoning uses address negation.
2364 // - If `base` were equal to `expected`, poisoning `expected`
2365 // would invalidate `base`.
2366 DCHECK_NE(value_reg, expected.AsRegister());
2367 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2368
2369 __ PoisonHeapReference(expected);
2370 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002371 }
2372
Roland Levillain391b8662015-12-18 11:43:38 +00002373 // TODO: Add a read barrier for the reference stored in the object
2374 // before attempting the CAS, similar to the one in the
2375 // art::Unsafe_compareAndSwapObject JNI implementation.
2376 //
2377 // Note that this code is not (yet) used when read barriers are
2378 // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject).
2379 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillainb488b782015-10-22 11:38:49 +01002380 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002381
Roland Levillain0d5a2812015-11-13 10:07:31 +00002382 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002383 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002384
Roland Levillainb488b782015-10-22 11:38:49 +01002385 // Convert ZF into the boolean result.
2386 __ setcc(kZero, out);
2387 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002388
Roland Levillain391b8662015-12-18 11:43:38 +00002389 // If heap poisoning is enabled, we need to unpoison the values
2390 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002391 if (kPoisonHeapReferences) {
2392 if (base_equals_value) {
2393 // `value_reg` has been moved to a temporary register, no need
2394 // to unpoison it.
2395 } else {
2396 // Ensure `value` is different from `out`, so that unpoisoning
2397 // the former does not invalidate the latter.
2398 DCHECK_NE(value_reg, out.AsRegister());
2399 __ UnpoisonHeapReference(CpuRegister(value_reg));
2400 }
2401 // Ensure `expected` is different from `out`, so that unpoisoning
2402 // the former does not invalidate the latter.
2403 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2404 __ UnpoisonHeapReference(expected);
2405 }
2406 } else {
2407 if (type == Primitive::kPrimInt) {
2408 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2409 } else if (type == Primitive::kPrimLong) {
2410 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2411 } else {
2412 LOG(FATAL) << "Unexpected CAS type " << type;
2413 }
2414
Roland Levillain0d5a2812015-11-13 10:07:31 +00002415 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002416 // scheduling barriers at this time.
2417
2418 // Convert ZF into the boolean result.
2419 __ setcc(kZero, out);
2420 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002421 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002422}
2423
2424void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2425 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2426}
2427
2428void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2429 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2430}
2431
2432void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002433 // The UnsafeCASObject intrinsic is missing a read barrier, and
2434 // therefore sometimes does not work as expected (b/25883050).
2435 // Turn it off temporarily as a quick fix, until the read barrier is
2436 // implemented (see TODO in GenCAS).
2437 //
2438 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
2439 // this intrinsic.
2440 DCHECK(!kEmitCompilerReadBarrier);
2441
Mark Mendell58d25fd2015-04-03 14:52:31 -04002442 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2443}
2444
2445void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2446 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2447 LocationSummary::kNoCall,
2448 kIntrinsified);
2449 locations->SetInAt(0, Location::RequiresRegister());
2450 locations->SetOut(Location::SameAsFirstInput());
2451 locations->AddTemp(Location::RequiresRegister());
2452}
2453
2454static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2455 X86_64Assembler* assembler) {
2456 Immediate imm_shift(shift);
2457 Immediate imm_mask(mask);
2458 __ movl(temp, reg);
2459 __ shrl(reg, imm_shift);
2460 __ andl(temp, imm_mask);
2461 __ andl(reg, imm_mask);
2462 __ shll(temp, imm_shift);
2463 __ orl(reg, temp);
2464}
2465
2466void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002467 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002468 LocationSummary* locations = invoke->GetLocations();
2469
2470 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2471 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2472
2473 /*
2474 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2475 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2476 * compared to generic luni implementation which has 5 rounds of swapping bits.
2477 * x = bswap x
2478 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2479 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2480 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2481 */
2482 __ bswapl(reg);
2483 SwapBits(reg, temp, 1, 0x55555555, assembler);
2484 SwapBits(reg, temp, 2, 0x33333333, assembler);
2485 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2486}
2487
2488void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2489 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2490 LocationSummary::kNoCall,
2491 kIntrinsified);
2492 locations->SetInAt(0, Location::RequiresRegister());
2493 locations->SetOut(Location::SameAsFirstInput());
2494 locations->AddTemp(Location::RequiresRegister());
2495 locations->AddTemp(Location::RequiresRegister());
2496}
2497
2498static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2499 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2500 Immediate imm_shift(shift);
2501 __ movq(temp_mask, Immediate(mask));
2502 __ movq(temp, reg);
2503 __ shrq(reg, imm_shift);
2504 __ andq(temp, temp_mask);
2505 __ andq(reg, temp_mask);
2506 __ shlq(temp, imm_shift);
2507 __ orq(reg, temp);
2508}
2509
2510void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002511 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002512 LocationSummary* locations = invoke->GetLocations();
2513
2514 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2515 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2516 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2517
2518 /*
2519 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2520 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2521 * compared to generic luni implementation which has 5 rounds of swapping bits.
2522 * x = bswap x
2523 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2524 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2525 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2526 */
2527 __ bswapq(reg);
2528 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2529 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2530 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2531}
2532
Aart Bik3f67e692016-01-15 14:35:12 -08002533static void CreateBitCountLocations(
2534 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2535 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2536 // Do nothing if there is no popcnt support. This results in generating
2537 // a call for the intrinsic rather than direct code.
2538 return;
2539 }
2540 LocationSummary* locations = new (arena) LocationSummary(invoke,
2541 LocationSummary::kNoCall,
2542 kIntrinsified);
2543 locations->SetInAt(0, Location::Any());
2544 locations->SetOut(Location::RequiresRegister());
2545}
2546
Aart Bikc5d47542016-01-27 17:00:35 -08002547static void GenBitCount(X86_64Assembler* assembler,
2548 CodeGeneratorX86_64* codegen,
2549 HInvoke* invoke,
2550 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002551 LocationSummary* locations = invoke->GetLocations();
2552 Location src = locations->InAt(0);
2553 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2554
2555 if (invoke->InputAt(0)->IsConstant()) {
2556 // Evaluate this at compile time.
2557 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002558 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002559 ? POPCOUNT(static_cast<uint64_t>(value))
2560 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002561 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002562 return;
2563 }
2564
2565 if (src.IsRegister()) {
2566 if (is_long) {
2567 __ popcntq(out, src.AsRegister<CpuRegister>());
2568 } else {
2569 __ popcntl(out, src.AsRegister<CpuRegister>());
2570 }
2571 } else if (is_long) {
2572 DCHECK(src.IsDoubleStackSlot());
2573 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2574 } else {
2575 DCHECK(src.IsStackSlot());
2576 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2577 }
2578}
2579
2580void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2581 CreateBitCountLocations(arena_, codegen_, invoke);
2582}
2583
2584void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002585 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002586}
2587
2588void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2589 CreateBitCountLocations(arena_, codegen_, invoke);
2590}
2591
2592void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002593 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2594}
2595
Aart Bikc5d47542016-01-27 17:00:35 -08002596static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2597 LocationSummary* locations = new (arena) LocationSummary(invoke,
2598 LocationSummary::kNoCall,
2599 kIntrinsified);
2600 locations->SetInAt(0, Location::Any());
2601 locations->SetOut(Location::RequiresRegister());
2602 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2603 : Location::RequiresRegister()); // any will do
2604}
2605
2606static void GenOneBit(X86_64Assembler* assembler,
2607 CodeGeneratorX86_64* codegen,
2608 HInvoke* invoke,
2609 bool is_high, bool is_long) {
2610 LocationSummary* locations = invoke->GetLocations();
2611 Location src = locations->InAt(0);
2612 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2613
2614 if (invoke->InputAt(0)->IsConstant()) {
2615 // Evaluate this at compile time.
2616 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2617 if (value == 0) {
2618 __ xorl(out, out); // Clears upper bits too.
2619 return;
2620 }
2621 // Nonzero value.
2622 if (is_high) {
2623 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2624 : 31 - CLZ(static_cast<uint32_t>(value));
2625 } else {
2626 value = is_long ? CTZ(static_cast<uint64_t>(value))
2627 : CTZ(static_cast<uint32_t>(value));
2628 }
2629 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002630 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002631 } else {
2632 codegen->Load32BitValue(out, 1 << value);
2633 }
2634 return;
2635 }
2636
2637 // Handle the non-constant cases.
2638 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2639 if (is_high) {
2640 // Use architectural support: basically 1 << bsr.
2641 if (src.IsRegister()) {
2642 if (is_long) {
2643 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2644 } else {
2645 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2646 }
2647 } else if (is_long) {
2648 DCHECK(src.IsDoubleStackSlot());
2649 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2650 } else {
2651 DCHECK(src.IsStackSlot());
2652 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2653 }
2654 // BSR sets ZF if the input was zero.
2655 NearLabel is_zero, done;
2656 __ j(kEqual, &is_zero);
2657 __ movl(out, Immediate(1)); // Clears upper bits too.
2658 if (is_long) {
2659 __ shlq(out, tmp);
2660 } else {
2661 __ shll(out, tmp);
2662 }
2663 __ jmp(&done);
2664 __ Bind(&is_zero);
2665 __ xorl(out, out); // Clears upper bits too.
2666 __ Bind(&done);
2667 } else {
2668 // Copy input into temporary.
2669 if (src.IsRegister()) {
2670 if (is_long) {
2671 __ movq(tmp, src.AsRegister<CpuRegister>());
2672 } else {
2673 __ movl(tmp, src.AsRegister<CpuRegister>());
2674 }
2675 } else if (is_long) {
2676 DCHECK(src.IsDoubleStackSlot());
2677 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2678 } else {
2679 DCHECK(src.IsStackSlot());
2680 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2681 }
2682 // Do the bit twiddling: basically tmp & -tmp;
2683 if (is_long) {
2684 __ movq(out, tmp);
2685 __ negq(tmp);
2686 __ andq(out, tmp);
2687 } else {
2688 __ movl(out, tmp);
2689 __ negl(tmp);
2690 __ andl(out, tmp);
2691 }
2692 }
2693}
2694
2695void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2696 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2697}
2698
2699void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2700 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2701}
2702
2703void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2704 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2705}
2706
2707void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2708 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2709}
2710
2711void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2712 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2713}
2714
2715void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2716 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2717}
2718
2719void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2720 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2721}
2722
2723void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2724 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002725}
2726
Mark Mendelld5897672015-08-12 21:16:41 -04002727static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2728 LocationSummary* locations = new (arena) LocationSummary(invoke,
2729 LocationSummary::kNoCall,
2730 kIntrinsified);
2731 locations->SetInAt(0, Location::Any());
2732 locations->SetOut(Location::RequiresRegister());
2733}
2734
Aart Bikc5d47542016-01-27 17:00:35 -08002735static void GenLeadingZeros(X86_64Assembler* assembler,
2736 CodeGeneratorX86_64* codegen,
2737 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002738 LocationSummary* locations = invoke->GetLocations();
2739 Location src = locations->InAt(0);
2740 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2741
2742 int zero_value_result = is_long ? 64 : 32;
2743 if (invoke->InputAt(0)->IsConstant()) {
2744 // Evaluate this at compile time.
2745 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2746 if (value == 0) {
2747 value = zero_value_result;
2748 } else {
2749 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2750 }
Aart Bikc5d47542016-01-27 17:00:35 -08002751 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002752 return;
2753 }
2754
2755 // Handle the non-constant cases.
2756 if (src.IsRegister()) {
2757 if (is_long) {
2758 __ bsrq(out, src.AsRegister<CpuRegister>());
2759 } else {
2760 __ bsrl(out, src.AsRegister<CpuRegister>());
2761 }
2762 } else if (is_long) {
2763 DCHECK(src.IsDoubleStackSlot());
2764 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2765 } else {
2766 DCHECK(src.IsStackSlot());
2767 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2768 }
2769
2770 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002771 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002772 __ j(kEqual, &is_zero);
2773
2774 // Correct the result from BSR to get the CLZ result.
2775 __ xorl(out, Immediate(zero_value_result - 1));
2776 __ jmp(&done);
2777
2778 // Fix the zero case with the expected result.
2779 __ Bind(&is_zero);
2780 __ movl(out, Immediate(zero_value_result));
2781
2782 __ Bind(&done);
2783}
2784
2785void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2786 CreateLeadingZeroLocations(arena_, invoke);
2787}
2788
2789void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002790 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002791}
2792
2793void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2794 CreateLeadingZeroLocations(arena_, invoke);
2795}
2796
2797void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002798 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002799}
2800
Mark Mendell2d554792015-09-15 21:45:18 -04002801static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2802 LocationSummary* locations = new (arena) LocationSummary(invoke,
2803 LocationSummary::kNoCall,
2804 kIntrinsified);
2805 locations->SetInAt(0, Location::Any());
2806 locations->SetOut(Location::RequiresRegister());
2807}
2808
Aart Bikc5d47542016-01-27 17:00:35 -08002809static void GenTrailingZeros(X86_64Assembler* assembler,
2810 CodeGeneratorX86_64* codegen,
2811 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -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 ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2824 }
Aart Bikc5d47542016-01-27 17:00:35 -08002825 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002826 return;
2827 }
2828
2829 // Handle the non-constant cases.
2830 if (src.IsRegister()) {
2831 if (is_long) {
2832 __ bsfq(out, src.AsRegister<CpuRegister>());
2833 } else {
2834 __ bsfl(out, src.AsRegister<CpuRegister>());
2835 }
2836 } else if (is_long) {
2837 DCHECK(src.IsDoubleStackSlot());
2838 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2839 } else {
2840 DCHECK(src.IsStackSlot());
2841 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2842 }
2843
2844 // BSF sets ZF if the input was zero, and the output is undefined.
2845 NearLabel done;
2846 __ j(kNotEqual, &done);
2847
2848 // Fix the zero case with the expected result.
2849 __ movl(out, Immediate(zero_value_result));
2850
2851 __ Bind(&done);
2852}
2853
2854void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2855 CreateTrailingZeroLocations(arena_, invoke);
2856}
2857
2858void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002859 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002860}
2861
2862void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2863 CreateTrailingZeroLocations(arena_, invoke);
2864}
2865
2866void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002867 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2868}
2869
Serguei Katkov288c7a82016-05-16 11:53:15 +06002870void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2871 if (kEmitCompilerReadBarrier) {
2872 // Do not intrinsify this call with the read barrier configuration.
2873 return;
2874 }
2875 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2876 LocationSummary::kCallOnSlowPath,
2877 kIntrinsified);
2878 locations->SetInAt(0, Location::RequiresRegister());
2879 locations->SetOut(Location::SameAsFirstInput());
2880 locations->AddTemp(Location::RequiresRegister());
2881}
2882
2883void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2884 DCHECK(!kEmitCompilerReadBarrier);
2885 LocationSummary* locations = invoke->GetLocations();
2886 X86_64Assembler* assembler = GetAssembler();
2887
2888 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2889 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2890
2891 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2892 codegen_->AddSlowPath(slow_path);
2893
2894 // Load ArtMethod first.
2895 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2896 DCHECK(invoke_direct != nullptr);
2897 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2898 invoke_direct, locations->GetTemp(0));
2899 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2900 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2901
2902 // Now get declaring class.
2903 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2904
2905 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2906 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2907 DCHECK_NE(slow_path_flag_offset, 0u);
2908 DCHECK_NE(disable_flag_offset, 0u);
2909 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2910
2911 // Check static flags preventing us for using intrinsic.
2912 if (slow_path_flag_offset == disable_flag_offset + 1) {
2913 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2914 __ j(kNotEqual, slow_path->GetEntryLabel());
2915 } else {
2916 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2917 __ j(kNotEqual, slow_path->GetEntryLabel());
2918 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2919 __ j(kNotEqual, slow_path->GetEntryLabel());
2920 }
2921
2922 // Fast path.
2923 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2924 codegen_->MaybeRecordImplicitNullCheck(invoke);
2925 __ MaybeUnpoisonHeapReference(out);
2926 __ Bind(slow_path->GetExitLabel());
2927}
2928
Aart Bik2f9fcc92016-03-01 15:16:54 -08002929UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2930UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002931
Aart Bik0e54c012016-03-04 12:08:31 -08002932// 1.8.
2933UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2934UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2935UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2936UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2937UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002938
Aart Bik2f9fcc92016-03-01 15:16:54 -08002939UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002940
2941#undef __
2942
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002943} // namespace x86_64
2944} // namespace art