blob: a4ee5462376a3b932b158f5606436fc53028597d [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);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002126 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002127 locations->SetInAt(1, Location::RequiresRegister());
2128 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002129 locations->SetOut(Location::RequiresRegister(),
2130 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002131}
2132
2133void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002134 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002135}
2136void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002137 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002138}
2139void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002140 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002141}
2142void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002143 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002144}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002145void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002146 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002147}
2148void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002149 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002150}
2151
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002152
2153void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002154 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002155}
2156void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002157 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002158}
2159void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002160 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002161}
2162void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002163 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002164}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002165void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002166 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002167}
2168void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002169 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002170}
2171
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002172
2173static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
2174 Primitive::Type type,
2175 HInvoke* invoke) {
2176 LocationSummary* locations = new (arena) LocationSummary(invoke,
2177 LocationSummary::kNoCall,
2178 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002179 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002180 locations->SetInAt(1, Location::RequiresRegister());
2181 locations->SetInAt(2, Location::RequiresRegister());
2182 locations->SetInAt(3, Location::RequiresRegister());
2183 if (type == Primitive::kPrimNot) {
2184 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002185 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002186 locations->AddTemp(Location::RequiresRegister());
2187 }
2188}
2189
2190void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2191 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2192}
2193void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2194 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2195}
2196void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2197 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2198}
2199void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2200 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2201}
2202void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2203 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2204}
2205void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2206 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2207}
2208void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2209 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2210}
2211void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2212 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2213}
2214void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2215 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2216}
2217
2218// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2219// memory model.
2220static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2221 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002222 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002223 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2224 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2225 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2226
2227 if (type == Primitive::kPrimLong) {
2228 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002229 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2230 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2231 __ movl(temp, value);
2232 __ PoisonHeapReference(temp);
2233 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002234 } else {
2235 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2236 }
2237
2238 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002239 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002240 }
2241
2242 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002243 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002244 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2245 locations->GetTemp(1).AsRegister<CpuRegister>(),
2246 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002247 value,
2248 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002249 }
2250}
2251
2252void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002253 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002254}
2255void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(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::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002259 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002260}
2261void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002262 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002263}
2264void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(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::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002268 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002269}
2270void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002271 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002272}
2273void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(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::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002277 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002278}
2279
Mark Mendell58d25fd2015-04-03 14:52:31 -04002280static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2281 HInvoke* invoke) {
2282 LocationSummary* locations = new (arena) LocationSummary(invoke,
2283 LocationSummary::kNoCall,
2284 kIntrinsified);
2285 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2286 locations->SetInAt(1, Location::RequiresRegister());
2287 locations->SetInAt(2, Location::RequiresRegister());
2288 // expected value must be in EAX/RAX.
2289 locations->SetInAt(3, Location::RegisterLocation(RAX));
2290 locations->SetInAt(4, Location::RequiresRegister());
2291
2292 locations->SetOut(Location::RequiresRegister());
2293 if (type == Primitive::kPrimNot) {
2294 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002295 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002296 locations->AddTemp(Location::RequiresRegister());
2297 }
2298}
2299
2300void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2301 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2302}
2303
2304void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2305 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2306}
2307
2308void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002309 // The UnsafeCASObject intrinsic is missing a read barrier, and
2310 // therefore sometimes does not work as expected (b/25883050).
2311 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01002312 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00002313 //
Roland Levillain3d312422016-06-23 13:53:42 +01002314 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
Roland Levillain391b8662015-12-18 11:43:38 +00002315 // this intrinsic.
2316 if (kEmitCompilerReadBarrier) {
2317 return;
2318 }
2319
Mark Mendell58d25fd2015-04-03 14:52:31 -04002320 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2321}
2322
2323static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002324 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002325 LocationSummary* locations = invoke->GetLocations();
2326
2327 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2328 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2329 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002330 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002331 DCHECK_EQ(expected.AsRegister(), RAX);
2332 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
2333 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2334
Roland Levillainb488b782015-10-22 11:38:49 +01002335 if (type == Primitive::kPrimNot) {
2336 // Mark card for object assuming new value is stored.
2337 bool value_can_be_null = true; // TODO: Worth finding out this information?
2338 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2339 locations->GetTemp(1).AsRegister<CpuRegister>(),
2340 base,
2341 value,
2342 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01002343
Roland Levillainb488b782015-10-22 11:38:49 +01002344 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2345 Register value_reg = value.AsRegister();
2346 if (kPoisonHeapReferences) {
2347 if (base_equals_value) {
2348 // If `base` and `value` are the same register location, move
2349 // `value_reg` to a temporary register. This way, poisoning
2350 // `value_reg` won't invalidate `base`.
2351 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
2352 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002353 }
Roland Levillainb488b782015-10-22 11:38:49 +01002354
2355 // Check that the register allocator did not assign the location
2356 // of `expected` (RAX) to `value` nor to `base`, so that heap
2357 // poisoning (when enabled) works as intended below.
2358 // - If `value` were equal to `expected`, both references would
2359 // be poisoned twice, meaning they would not be poisoned at
2360 // all, as heap poisoning uses address negation.
2361 // - If `base` were equal to `expected`, poisoning `expected`
2362 // would invalidate `base`.
2363 DCHECK_NE(value_reg, expected.AsRegister());
2364 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2365
2366 __ PoisonHeapReference(expected);
2367 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002368 }
2369
Roland Levillain391b8662015-12-18 11:43:38 +00002370 // TODO: Add a read barrier for the reference stored in the object
2371 // before attempting the CAS, similar to the one in the
2372 // art::Unsafe_compareAndSwapObject JNI implementation.
2373 //
2374 // Note that this code is not (yet) used when read barriers are
2375 // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject).
2376 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillainb488b782015-10-22 11:38:49 +01002377 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002378
Roland Levillain0d5a2812015-11-13 10:07:31 +00002379 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002380 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002381
Roland Levillainb488b782015-10-22 11:38:49 +01002382 // Convert ZF into the boolean result.
2383 __ setcc(kZero, out);
2384 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002385
Roland Levillain391b8662015-12-18 11:43:38 +00002386 // If heap poisoning is enabled, we need to unpoison the values
2387 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002388 if (kPoisonHeapReferences) {
2389 if (base_equals_value) {
2390 // `value_reg` has been moved to a temporary register, no need
2391 // to unpoison it.
2392 } else {
2393 // Ensure `value` is different from `out`, so that unpoisoning
2394 // the former does not invalidate the latter.
2395 DCHECK_NE(value_reg, out.AsRegister());
2396 __ UnpoisonHeapReference(CpuRegister(value_reg));
2397 }
2398 // Ensure `expected` is different from `out`, so that unpoisoning
2399 // the former does not invalidate the latter.
2400 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2401 __ UnpoisonHeapReference(expected);
2402 }
2403 } else {
2404 if (type == Primitive::kPrimInt) {
2405 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2406 } else if (type == Primitive::kPrimLong) {
2407 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2408 } else {
2409 LOG(FATAL) << "Unexpected CAS type " << type;
2410 }
2411
Roland Levillain0d5a2812015-11-13 10:07:31 +00002412 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002413 // scheduling barriers at this time.
2414
2415 // Convert ZF into the boolean result.
2416 __ setcc(kZero, out);
2417 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002418 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002419}
2420
2421void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2422 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2423}
2424
2425void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2426 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2427}
2428
2429void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002430 // The UnsafeCASObject intrinsic is missing a read barrier, and
2431 // therefore sometimes does not work as expected (b/25883050).
2432 // Turn it off temporarily as a quick fix, until the read barrier is
2433 // implemented (see TODO in GenCAS).
2434 //
2435 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
2436 // this intrinsic.
2437 DCHECK(!kEmitCompilerReadBarrier);
2438
Mark Mendell58d25fd2015-04-03 14:52:31 -04002439 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2440}
2441
2442void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2443 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2444 LocationSummary::kNoCall,
2445 kIntrinsified);
2446 locations->SetInAt(0, Location::RequiresRegister());
2447 locations->SetOut(Location::SameAsFirstInput());
2448 locations->AddTemp(Location::RequiresRegister());
2449}
2450
2451static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2452 X86_64Assembler* assembler) {
2453 Immediate imm_shift(shift);
2454 Immediate imm_mask(mask);
2455 __ movl(temp, reg);
2456 __ shrl(reg, imm_shift);
2457 __ andl(temp, imm_mask);
2458 __ andl(reg, imm_mask);
2459 __ shll(temp, imm_shift);
2460 __ orl(reg, temp);
2461}
2462
2463void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002464 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002465 LocationSummary* locations = invoke->GetLocations();
2466
2467 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2468 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2469
2470 /*
2471 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2472 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2473 * compared to generic luni implementation which has 5 rounds of swapping bits.
2474 * x = bswap x
2475 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2476 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2477 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2478 */
2479 __ bswapl(reg);
2480 SwapBits(reg, temp, 1, 0x55555555, assembler);
2481 SwapBits(reg, temp, 2, 0x33333333, assembler);
2482 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2483}
2484
2485void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2486 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2487 LocationSummary::kNoCall,
2488 kIntrinsified);
2489 locations->SetInAt(0, Location::RequiresRegister());
2490 locations->SetOut(Location::SameAsFirstInput());
2491 locations->AddTemp(Location::RequiresRegister());
2492 locations->AddTemp(Location::RequiresRegister());
2493}
2494
2495static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2496 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2497 Immediate imm_shift(shift);
2498 __ movq(temp_mask, Immediate(mask));
2499 __ movq(temp, reg);
2500 __ shrq(reg, imm_shift);
2501 __ andq(temp, temp_mask);
2502 __ andq(reg, temp_mask);
2503 __ shlq(temp, imm_shift);
2504 __ orq(reg, temp);
2505}
2506
2507void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002508 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002509 LocationSummary* locations = invoke->GetLocations();
2510
2511 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2512 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2513 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2514
2515 /*
2516 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2517 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2518 * compared to generic luni implementation which has 5 rounds of swapping bits.
2519 * x = bswap x
2520 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2521 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2522 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2523 */
2524 __ bswapq(reg);
2525 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2526 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2527 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2528}
2529
Aart Bik3f67e692016-01-15 14:35:12 -08002530static void CreateBitCountLocations(
2531 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2532 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2533 // Do nothing if there is no popcnt support. This results in generating
2534 // a call for the intrinsic rather than direct code.
2535 return;
2536 }
2537 LocationSummary* locations = new (arena) LocationSummary(invoke,
2538 LocationSummary::kNoCall,
2539 kIntrinsified);
2540 locations->SetInAt(0, Location::Any());
2541 locations->SetOut(Location::RequiresRegister());
2542}
2543
Aart Bikc5d47542016-01-27 17:00:35 -08002544static void GenBitCount(X86_64Assembler* assembler,
2545 CodeGeneratorX86_64* codegen,
2546 HInvoke* invoke,
2547 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002548 LocationSummary* locations = invoke->GetLocations();
2549 Location src = locations->InAt(0);
2550 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2551
2552 if (invoke->InputAt(0)->IsConstant()) {
2553 // Evaluate this at compile time.
2554 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002555 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002556 ? POPCOUNT(static_cast<uint64_t>(value))
2557 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002558 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002559 return;
2560 }
2561
2562 if (src.IsRegister()) {
2563 if (is_long) {
2564 __ popcntq(out, src.AsRegister<CpuRegister>());
2565 } else {
2566 __ popcntl(out, src.AsRegister<CpuRegister>());
2567 }
2568 } else if (is_long) {
2569 DCHECK(src.IsDoubleStackSlot());
2570 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2571 } else {
2572 DCHECK(src.IsStackSlot());
2573 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2574 }
2575}
2576
2577void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2578 CreateBitCountLocations(arena_, codegen_, invoke);
2579}
2580
2581void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002582 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002583}
2584
2585void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2586 CreateBitCountLocations(arena_, codegen_, invoke);
2587}
2588
2589void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002590 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2591}
2592
Aart Bikc5d47542016-01-27 17:00:35 -08002593static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2594 LocationSummary* locations = new (arena) LocationSummary(invoke,
2595 LocationSummary::kNoCall,
2596 kIntrinsified);
2597 locations->SetInAt(0, Location::Any());
2598 locations->SetOut(Location::RequiresRegister());
2599 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2600 : Location::RequiresRegister()); // any will do
2601}
2602
2603static void GenOneBit(X86_64Assembler* assembler,
2604 CodeGeneratorX86_64* codegen,
2605 HInvoke* invoke,
2606 bool is_high, bool is_long) {
2607 LocationSummary* locations = invoke->GetLocations();
2608 Location src = locations->InAt(0);
2609 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2610
2611 if (invoke->InputAt(0)->IsConstant()) {
2612 // Evaluate this at compile time.
2613 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2614 if (value == 0) {
2615 __ xorl(out, out); // Clears upper bits too.
2616 return;
2617 }
2618 // Nonzero value.
2619 if (is_high) {
2620 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2621 : 31 - CLZ(static_cast<uint32_t>(value));
2622 } else {
2623 value = is_long ? CTZ(static_cast<uint64_t>(value))
2624 : CTZ(static_cast<uint32_t>(value));
2625 }
2626 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002627 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002628 } else {
2629 codegen->Load32BitValue(out, 1 << value);
2630 }
2631 return;
2632 }
2633
2634 // Handle the non-constant cases.
2635 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2636 if (is_high) {
2637 // Use architectural support: basically 1 << bsr.
2638 if (src.IsRegister()) {
2639 if (is_long) {
2640 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2641 } else {
2642 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2643 }
2644 } else if (is_long) {
2645 DCHECK(src.IsDoubleStackSlot());
2646 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2647 } else {
2648 DCHECK(src.IsStackSlot());
2649 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2650 }
2651 // BSR sets ZF if the input was zero.
2652 NearLabel is_zero, done;
2653 __ j(kEqual, &is_zero);
2654 __ movl(out, Immediate(1)); // Clears upper bits too.
2655 if (is_long) {
2656 __ shlq(out, tmp);
2657 } else {
2658 __ shll(out, tmp);
2659 }
2660 __ jmp(&done);
2661 __ Bind(&is_zero);
2662 __ xorl(out, out); // Clears upper bits too.
2663 __ Bind(&done);
2664 } else {
2665 // Copy input into temporary.
2666 if (src.IsRegister()) {
2667 if (is_long) {
2668 __ movq(tmp, src.AsRegister<CpuRegister>());
2669 } else {
2670 __ movl(tmp, src.AsRegister<CpuRegister>());
2671 }
2672 } else if (is_long) {
2673 DCHECK(src.IsDoubleStackSlot());
2674 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2675 } else {
2676 DCHECK(src.IsStackSlot());
2677 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2678 }
2679 // Do the bit twiddling: basically tmp & -tmp;
2680 if (is_long) {
2681 __ movq(out, tmp);
2682 __ negq(tmp);
2683 __ andq(out, tmp);
2684 } else {
2685 __ movl(out, tmp);
2686 __ negl(tmp);
2687 __ andl(out, tmp);
2688 }
2689 }
2690}
2691
2692void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2693 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2694}
2695
2696void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2697 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2698}
2699
2700void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2701 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2702}
2703
2704void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2705 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2706}
2707
2708void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2709 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2710}
2711
2712void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2713 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2714}
2715
2716void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2717 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2718}
2719
2720void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2721 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002722}
2723
Mark Mendelld5897672015-08-12 21:16:41 -04002724static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2725 LocationSummary* locations = new (arena) LocationSummary(invoke,
2726 LocationSummary::kNoCall,
2727 kIntrinsified);
2728 locations->SetInAt(0, Location::Any());
2729 locations->SetOut(Location::RequiresRegister());
2730}
2731
Aart Bikc5d47542016-01-27 17:00:35 -08002732static void GenLeadingZeros(X86_64Assembler* assembler,
2733 CodeGeneratorX86_64* codegen,
2734 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002735 LocationSummary* locations = invoke->GetLocations();
2736 Location src = locations->InAt(0);
2737 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2738
2739 int zero_value_result = is_long ? 64 : 32;
2740 if (invoke->InputAt(0)->IsConstant()) {
2741 // Evaluate this at compile time.
2742 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2743 if (value == 0) {
2744 value = zero_value_result;
2745 } else {
2746 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2747 }
Aart Bikc5d47542016-01-27 17:00:35 -08002748 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002749 return;
2750 }
2751
2752 // Handle the non-constant cases.
2753 if (src.IsRegister()) {
2754 if (is_long) {
2755 __ bsrq(out, src.AsRegister<CpuRegister>());
2756 } else {
2757 __ bsrl(out, src.AsRegister<CpuRegister>());
2758 }
2759 } else if (is_long) {
2760 DCHECK(src.IsDoubleStackSlot());
2761 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2762 } else {
2763 DCHECK(src.IsStackSlot());
2764 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2765 }
2766
2767 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002768 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002769 __ j(kEqual, &is_zero);
2770
2771 // Correct the result from BSR to get the CLZ result.
2772 __ xorl(out, Immediate(zero_value_result - 1));
2773 __ jmp(&done);
2774
2775 // Fix the zero case with the expected result.
2776 __ Bind(&is_zero);
2777 __ movl(out, Immediate(zero_value_result));
2778
2779 __ Bind(&done);
2780}
2781
2782void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2783 CreateLeadingZeroLocations(arena_, invoke);
2784}
2785
2786void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002787 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002788}
2789
2790void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2791 CreateLeadingZeroLocations(arena_, invoke);
2792}
2793
2794void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002795 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002796}
2797
Mark Mendell2d554792015-09-15 21:45:18 -04002798static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2799 LocationSummary* locations = new (arena) LocationSummary(invoke,
2800 LocationSummary::kNoCall,
2801 kIntrinsified);
2802 locations->SetInAt(0, Location::Any());
2803 locations->SetOut(Location::RequiresRegister());
2804}
2805
Aart Bikc5d47542016-01-27 17:00:35 -08002806static void GenTrailingZeros(X86_64Assembler* assembler,
2807 CodeGeneratorX86_64* codegen,
2808 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002809 LocationSummary* locations = invoke->GetLocations();
2810 Location src = locations->InAt(0);
2811 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2812
2813 int zero_value_result = is_long ? 64 : 32;
2814 if (invoke->InputAt(0)->IsConstant()) {
2815 // Evaluate this at compile time.
2816 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2817 if (value == 0) {
2818 value = zero_value_result;
2819 } else {
2820 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2821 }
Aart Bikc5d47542016-01-27 17:00:35 -08002822 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002823 return;
2824 }
2825
2826 // Handle the non-constant cases.
2827 if (src.IsRegister()) {
2828 if (is_long) {
2829 __ bsfq(out, src.AsRegister<CpuRegister>());
2830 } else {
2831 __ bsfl(out, src.AsRegister<CpuRegister>());
2832 }
2833 } else if (is_long) {
2834 DCHECK(src.IsDoubleStackSlot());
2835 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2836 } else {
2837 DCHECK(src.IsStackSlot());
2838 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2839 }
2840
2841 // BSF sets ZF if the input was zero, and the output is undefined.
2842 NearLabel done;
2843 __ j(kNotEqual, &done);
2844
2845 // Fix the zero case with the expected result.
2846 __ movl(out, Immediate(zero_value_result));
2847
2848 __ Bind(&done);
2849}
2850
2851void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2852 CreateTrailingZeroLocations(arena_, invoke);
2853}
2854
2855void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002856 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002857}
2858
2859void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2860 CreateTrailingZeroLocations(arena_, invoke);
2861}
2862
2863void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002864 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2865}
2866
Serguei Katkov288c7a82016-05-16 11:53:15 +06002867void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2868 if (kEmitCompilerReadBarrier) {
2869 // Do not intrinsify this call with the read barrier configuration.
2870 return;
2871 }
2872 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2873 LocationSummary::kCallOnSlowPath,
2874 kIntrinsified);
2875 locations->SetInAt(0, Location::RequiresRegister());
2876 locations->SetOut(Location::SameAsFirstInput());
2877 locations->AddTemp(Location::RequiresRegister());
2878}
2879
2880void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2881 DCHECK(!kEmitCompilerReadBarrier);
2882 LocationSummary* locations = invoke->GetLocations();
2883 X86_64Assembler* assembler = GetAssembler();
2884
2885 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2886 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2887
2888 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2889 codegen_->AddSlowPath(slow_path);
2890
2891 // Load ArtMethod first.
2892 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2893 DCHECK(invoke_direct != nullptr);
2894 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2895 invoke_direct, locations->GetTemp(0));
2896 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2897 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2898
2899 // Now get declaring class.
2900 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2901
2902 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2903 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2904 DCHECK_NE(slow_path_flag_offset, 0u);
2905 DCHECK_NE(disable_flag_offset, 0u);
2906 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2907
2908 // Check static flags preventing us for using intrinsic.
2909 if (slow_path_flag_offset == disable_flag_offset + 1) {
2910 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2911 __ j(kNotEqual, slow_path->GetEntryLabel());
2912 } else {
2913 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2914 __ j(kNotEqual, slow_path->GetEntryLabel());
2915 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2916 __ j(kNotEqual, slow_path->GetEntryLabel());
2917 }
2918
2919 // Fast path.
2920 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2921 codegen_->MaybeRecordImplicitNullCheck(invoke);
2922 __ MaybeUnpoisonHeapReference(out);
2923 __ Bind(slow_path->GetExitLabel());
2924}
2925
Aart Bik2f9fcc92016-03-01 15:16:54 -08002926UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2927UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002928
Aart Bik0e54c012016-03-04 12:08:31 -08002929// 1.8.
2930UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2931UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2932UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2933UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2934UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002935
Aart Bik2f9fcc92016-03-01 15:16:54 -08002936UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002937
2938#undef __
2939
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002940} // namespace x86_64
2941} // namespace art