blob: 4b0afca122546ab1321a80b5e9360b3bee31705f [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());
Mark Mendella4f12202015-08-06 15:23:34 -0400769
Serban Constantinescuba45db02016-07-12 22:53:02 +0100770 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400771}
772
773void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
774 CreateFPToFPCallLocations(arena_, invoke);
775}
776
777void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
778 GenFPToFPCall(invoke, codegen_, kQuickCos);
779}
780
781void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
782 CreateFPToFPCallLocations(arena_, invoke);
783}
784
785void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
786 GenFPToFPCall(invoke, codegen_, kQuickSin);
787}
788
789void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
790 CreateFPToFPCallLocations(arena_, invoke);
791}
792
793void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
794 GenFPToFPCall(invoke, codegen_, kQuickAcos);
795}
796
797void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
798 CreateFPToFPCallLocations(arena_, invoke);
799}
800
801void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
802 GenFPToFPCall(invoke, codegen_, kQuickAsin);
803}
804
805void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
806 CreateFPToFPCallLocations(arena_, invoke);
807}
808
809void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
810 GenFPToFPCall(invoke, codegen_, kQuickAtan);
811}
812
813void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
814 CreateFPToFPCallLocations(arena_, invoke);
815}
816
817void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
818 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
819}
820
821void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
822 CreateFPToFPCallLocations(arena_, invoke);
823}
824
825void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
826 GenFPToFPCall(invoke, codegen_, kQuickCosh);
827}
828
829void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
830 CreateFPToFPCallLocations(arena_, invoke);
831}
832
833void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
834 GenFPToFPCall(invoke, codegen_, kQuickExp);
835}
836
837void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
838 CreateFPToFPCallLocations(arena_, invoke);
839}
840
841void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
842 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
843}
844
845void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
846 CreateFPToFPCallLocations(arena_, invoke);
847}
848
849void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
850 GenFPToFPCall(invoke, codegen_, kQuickLog);
851}
852
853void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
854 CreateFPToFPCallLocations(arena_, invoke);
855}
856
857void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
858 GenFPToFPCall(invoke, codegen_, kQuickLog10);
859}
860
861void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
862 CreateFPToFPCallLocations(arena_, invoke);
863}
864
865void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
866 GenFPToFPCall(invoke, codegen_, kQuickSinh);
867}
868
869void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
870 CreateFPToFPCallLocations(arena_, invoke);
871}
872
873void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
874 GenFPToFPCall(invoke, codegen_, kQuickTan);
875}
876
877void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
878 CreateFPToFPCallLocations(arena_, invoke);
879}
880
881void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
882 GenFPToFPCall(invoke, codegen_, kQuickTanh);
883}
884
885static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
886 HInvoke* invoke) {
887 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100888 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400889 kIntrinsified);
890 InvokeRuntimeCallingConvention calling_convention;
891 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
892 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
893 locations->SetOut(Location::FpuRegisterLocation(XMM0));
894
895 // We have to ensure that the native code doesn't clobber the XMM registers which are
896 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
897 // saved in the prologue and properly restored.
898 for (auto fp_reg : non_volatile_xmm_regs) {
899 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
900 }
901}
902
903void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
904 CreateFPFPToFPCallLocations(arena_, invoke);
905}
906
907void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
908 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
909}
910
911void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
912 CreateFPFPToFPCallLocations(arena_, invoke);
913}
914
915void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
916 GenFPToFPCall(invoke, codegen_, kQuickHypot);
917}
918
919void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
920 CreateFPFPToFPCallLocations(arena_, invoke);
921}
922
923void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
924 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
925}
926
Mark Mendell6bc53a92015-07-01 14:26:52 -0400927void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
928 // Check to see if we have known failures that will cause us to have to bail out
929 // to the runtime, and just generate the runtime call directly.
930 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
931 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
932
933 // The positions must be non-negative.
934 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
935 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
936 // We will have to fail anyways.
937 return;
938 }
939
940 // The length must be > 0.
941 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
942 if (length != nullptr) {
943 int32_t len = length->GetValue();
944 if (len < 0) {
945 // Just call as normal.
946 return;
947 }
948 }
949
950 LocationSummary* locations = new (arena_) LocationSummary(invoke,
951 LocationSummary::kCallOnSlowPath,
952 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100953 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400954 locations->SetInAt(0, Location::RequiresRegister());
955 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
956 locations->SetInAt(2, Location::RequiresRegister());
957 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
958 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
959
960 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
961 locations->AddTemp(Location::RegisterLocation(RSI));
962 locations->AddTemp(Location::RegisterLocation(RDI));
963 locations->AddTemp(Location::RegisterLocation(RCX));
964}
965
966static void CheckPosition(X86_64Assembler* assembler,
967 Location pos,
968 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100969 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700970 SlowPathCode* slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100971 CpuRegister temp,
972 bool length_is_input_length = false) {
973 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400974 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
975
976 if (pos.IsConstant()) {
977 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
978 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100979 if (!length_is_input_length) {
980 // Check that length(input) >= length.
981 if (length.IsConstant()) {
982 __ cmpl(Address(input, length_offset),
983 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
984 } else {
985 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
986 }
987 __ j(kLess, slow_path->GetEntryLabel());
988 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400989 } else {
990 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100991 __ movl(temp, Address(input, length_offset));
992 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400993 __ j(kLess, slow_path->GetEntryLabel());
994
995 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100996 if (length.IsConstant()) {
997 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
998 } else {
999 __ cmpl(temp, length.AsRegister<CpuRegister>());
1000 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001001 __ j(kLess, slow_path->GetEntryLabel());
1002 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001003 } else if (length_is_input_length) {
1004 // The only way the copy can succeed is if pos is zero.
1005 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1006 __ testl(pos_reg, pos_reg);
1007 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001008 } else {
1009 // Check that pos >= 0.
1010 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1011 __ testl(pos_reg, pos_reg);
1012 __ j(kLess, slow_path->GetEntryLabel());
1013
1014 // Check that pos <= length(input).
1015 __ cmpl(Address(input, length_offset), pos_reg);
1016 __ j(kLess, slow_path->GetEntryLabel());
1017
1018 // Check that (length(input) - pos) >= length.
1019 __ movl(temp, Address(input, length_offset));
1020 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001021 if (length.IsConstant()) {
1022 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1023 } else {
1024 __ cmpl(temp, length.AsRegister<CpuRegister>());
1025 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001026 __ j(kLess, slow_path->GetEntryLabel());
1027 }
1028}
1029
1030void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1031 X86_64Assembler* assembler = GetAssembler();
1032 LocationSummary* locations = invoke->GetLocations();
1033
1034 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001035 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001036 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001037 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001038 Location length = locations->InAt(4);
1039
1040 // Temporaries that we need for MOVSW.
1041 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1042 DCHECK_EQ(src_base.AsRegister(), RSI);
1043 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1044 DCHECK_EQ(dest_base.AsRegister(), RDI);
1045 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1046 DCHECK_EQ(count.AsRegister(), RCX);
1047
Andreas Gampe85b62f22015-09-09 13:15:38 -07001048 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001049 codegen_->AddSlowPath(slow_path);
1050
1051 // Bail out if the source and destination are the same.
1052 __ cmpl(src, dest);
1053 __ j(kEqual, slow_path->GetEntryLabel());
1054
1055 // Bail out if the source is null.
1056 __ testl(src, src);
1057 __ j(kEqual, slow_path->GetEntryLabel());
1058
1059 // Bail out if the destination is null.
1060 __ testl(dest, dest);
1061 __ j(kEqual, slow_path->GetEntryLabel());
1062
1063 // If the length is negative, bail out.
1064 // We have already checked in the LocationsBuilder for the constant case.
1065 if (!length.IsConstant()) {
1066 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1067 __ j(kLess, slow_path->GetEntryLabel());
1068 }
1069
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001070 // Validity checks: source. Use src_base as a temporary register.
1071 CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001072
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001073 // Validity checks: dest. Use src_base as a temporary register.
1074 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001075
Mark Mendell6bc53a92015-07-01 14:26:52 -04001076 // We need the count in RCX.
1077 if (length.IsConstant()) {
1078 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1079 } else {
1080 __ movl(count, length.AsRegister<CpuRegister>());
1081 }
1082
Mark Mendell6bc53a92015-07-01 14:26:52 -04001083 // Okay, everything checks out. Finally time to do the copy.
1084 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1085 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1086 DCHECK_EQ(char_size, 2u);
1087
1088 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1089
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001090 if (src_pos.IsConstant()) {
1091 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1092 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001093 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001094 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001095 ScaleFactor::TIMES_2, data_offset));
1096 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001097 if (dest_pos.IsConstant()) {
1098 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1099 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001100 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001101 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001102 ScaleFactor::TIMES_2, data_offset));
1103 }
1104
1105 // Do the move.
1106 __ rep_movsw();
1107
1108 __ Bind(slow_path->GetExitLabel());
1109}
1110
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001111
1112void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001113 // The only read barrier implementation supporting the
1114 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1115 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01001116 return;
1117 }
1118
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001119 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001120}
1121
1122void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001123 // The only read barrier implementation supporting the
1124 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1125 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001126
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001127 X86_64Assembler* assembler = GetAssembler();
1128 LocationSummary* locations = invoke->GetLocations();
1129
1130 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1131 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1132 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1133 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001134 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001135
1136 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1137 Location src_pos = locations->InAt(1);
1138 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1139 Location dest_pos = locations->InAt(3);
1140 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +01001141 Location temp1_loc = locations->GetTemp(0);
1142 CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
1143 Location temp2_loc = locations->GetTemp(1);
1144 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
1145 Location temp3_loc = locations->GetTemp(2);
1146 CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
1147 Location TMP_loc = Location::RegisterLocation(TMP);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001148
Roland Levillain0b671c02016-08-19 12:02:34 +01001149 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1150 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001151
Roland Levillainebea3d22016-04-12 15:42:57 +01001152 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001153 SystemArrayCopyOptimizations optimizations(invoke);
1154
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001155 // If source and destination are the same, we go to slow path if we need to do
1156 // forward copying.
1157 if (src_pos.IsConstant()) {
1158 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1159 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001160 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1161 if (optimizations.GetDestinationIsSource()) {
1162 // Checked when building locations.
1163 DCHECK_GE(src_pos_constant, dest_pos_constant);
1164 } else if (src_pos_constant < dest_pos_constant) {
1165 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001166 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001167 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001168 } else {
1169 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001170 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001171 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001172 }
1173 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001174 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001175 }
1176 } else {
1177 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001178 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001179 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001180 }
1181 if (dest_pos.IsConstant()) {
1182 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1183 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001184 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001185 } else {
1186 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001187 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001188 }
1189 }
1190
Roland Levillainebea3d22016-04-12 15:42:57 +01001191 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001192
1193 if (!optimizations.GetSourceIsNotNull()) {
1194 // Bail out if the source is null.
1195 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01001196 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001197 }
1198
1199 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1200 // Bail out if the destination is null.
1201 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001202 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001203 }
1204
1205 // If the length is negative, bail out.
1206 // We have already checked in the LocationsBuilder for the constant case.
1207 if (!length.IsConstant() &&
1208 !optimizations.GetCountIsSourceLength() &&
1209 !optimizations.GetCountIsDestinationLength()) {
1210 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001211 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001212 }
1213
1214 // Validity checks: source.
1215 CheckPosition(assembler,
1216 src_pos,
1217 src,
1218 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001219 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001220 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001221 optimizations.GetCountIsSourceLength());
1222
1223 // Validity checks: dest.
1224 CheckPosition(assembler,
1225 dest_pos,
1226 dest,
1227 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001228 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001229 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001230 optimizations.GetCountIsDestinationLength());
1231
1232 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1233 // Check whether all elements of the source array are assignable to the component
1234 // type of the destination array. We do two checks: the classes are the same,
1235 // or the destination is Object[]. If none of these checks succeed, we go to the
1236 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01001237
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001238 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +01001239 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1240 // /* HeapReference<Class> */ temp1 = dest->klass_
1241 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001242 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001243 // Register `temp1` is not trashed by the read barrier emitted
1244 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1245 // method produces a call to a ReadBarrierMarkRegX entry point,
1246 // which saves all potentially live registers, including
1247 // temporaries such a `temp1`.
1248 // /* HeapReference<Class> */ temp2 = src->klass_
1249 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001250 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001251 // If heap poisoning is enabled, `temp1` and `temp2` have been
1252 // unpoisoned by the the previous calls to
1253 // GenerateFieldLoadWithBakerReadBarrier.
1254 } else {
1255 // /* HeapReference<Class> */ temp1 = dest->klass_
1256 __ movl(temp1, Address(dest, class_offset));
1257 // /* HeapReference<Class> */ temp2 = src->klass_
1258 __ movl(temp2, Address(src, class_offset));
1259 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1260 !optimizations.GetSourceIsNonPrimitiveArray()) {
1261 // One or two of the references need to be unpoisoned. Unpoison them
1262 // both to make the identity check valid.
1263 __ MaybeUnpoisonHeapReference(temp1);
1264 __ MaybeUnpoisonHeapReference(temp2);
1265 did_unpoison = true;
1266 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001267 }
1268
1269 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1270 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001271 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1272 // /* HeapReference<Class> */ TMP = temp1->component_type_
1273 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001274 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001275 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1276 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1277 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1278 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1279 } else {
1280 // /* HeapReference<Class> */ TMP = temp1->component_type_
1281 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1282 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1283 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1284 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1285 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001286 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001287 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001288 }
1289
1290 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1291 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001292 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1293 // For the same reason given earlier, `temp1` is not trashed by the
1294 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1295 // /* HeapReference<Class> */ TMP = temp2->component_type_
1296 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001297 invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001298 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1299 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1300 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1301 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1302 } else {
1303 // /* HeapReference<Class> */ TMP = temp2->component_type_
1304 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1305 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1306 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1307 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1308 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001309 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001310 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001311 }
1312
1313 __ cmpl(temp1, temp2);
1314
1315 if (optimizations.GetDestinationIsTypedObjectArray()) {
1316 NearLabel do_copy;
1317 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001318 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1319 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1320 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001321 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001322 // We do not need to emit a read barrier for the following
1323 // heap reference load, as `temp1` is only used in a
1324 // comparison with null below, and this reference is not
1325 // kept afterwards.
1326 __ cmpl(Address(temp1, super_offset), Immediate(0));
1327 } else {
1328 if (!did_unpoison) {
1329 __ MaybeUnpoisonHeapReference(temp1);
1330 }
1331 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1332 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001333 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001334 // No need to unpoison the following heap reference load, as
1335 // we're comparing against null.
1336 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001337 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001338 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001339 __ Bind(&do_copy);
1340 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001341 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001342 }
1343 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1344 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1345 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001346 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1347 // /* HeapReference<Class> */ temp1 = src->klass_
1348 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001349 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001350 // /* HeapReference<Class> */ TMP = temp1->component_type_
1351 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001352 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001353 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1354 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1355 } else {
1356 // /* HeapReference<Class> */ temp1 = src->klass_
1357 __ movl(temp1, Address(src, class_offset));
1358 __ MaybeUnpoisonHeapReference(temp1);
1359 // /* HeapReference<Class> */ TMP = temp1->component_type_
1360 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1361 // No need to unpoison `TMP` now, as we're comparing against null.
1362 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1363 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1364 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1365 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001366 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001367 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001368 }
1369
1370 // Compute base source address, base destination address, and end source address.
1371
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001372 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001373 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1374 if (src_pos.IsConstant()) {
1375 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1376 __ leal(temp1, Address(src, element_size * constant + offset));
1377 } else {
1378 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1379 }
1380
1381 if (dest_pos.IsConstant()) {
1382 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1383 __ leal(temp2, Address(dest, element_size * constant + offset));
1384 } else {
1385 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1386 }
1387
1388 if (length.IsConstant()) {
1389 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1390 __ leal(temp3, Address(temp1, element_size * constant));
1391 } else {
1392 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1393 }
1394
Roland Levillain0b671c02016-08-19 12:02:34 +01001395 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1396 // SystemArrayCopy implementation for Baker read barriers (see
1397 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1398 //
1399 // if (src_ptr != end_ptr) {
1400 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1401 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1402 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
1403 // if (is_gray) {
1404 // // Slow-path copy.
1405 // do {
1406 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1407 // } while (src_ptr != end_ptr)
1408 // } else {
1409 // // Fast-path copy.
1410 // do {
1411 // *dest_ptr++ = *src_ptr++;
1412 // } while (src_ptr != end_ptr)
1413 // }
1414 // }
1415
1416 NearLabel loop, done;
1417
1418 // Don't enter copy loop if `length == 0`.
1419 __ cmpl(temp1, temp3);
1420 __ j(kEqual, &done);
1421
Vladimir Marko953437b2016-08-24 08:30:46 +00001422 // Given the numeric representation, it's enough to check the low bit of the rb_state.
1423 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
1424 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
1425 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
1426 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
1427 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
1428 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
1429
1430 // if (rb_state == ReadBarrier::gray_ptr_)
1431 // goto slow_path;
1432 // At this point, just do the "if" and make sure that flags are preserved until the branch.
1433 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01001434
1435 // Load fence to prevent load-load reordering.
1436 // Note that this is a no-op, thanks to the x86-64 memory model.
1437 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1438
1439 // Slow path used to copy array when `src` is gray.
1440 SlowPathCode* read_barrier_slow_path =
1441 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
1442 codegen_->AddSlowPath(read_barrier_slow_path);
1443
Vladimir Marko953437b2016-08-24 08:30:46 +00001444 // We have done the "if" of the gray bit check above, now branch based on the flags.
1445 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01001446
1447 // Fast-path copy.
1448 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1449 // poison/unpoison.
1450 __ Bind(&loop);
1451 __ movl(CpuRegister(TMP), Address(temp1, 0));
1452 __ movl(Address(temp2, 0), CpuRegister(TMP));
1453 __ addl(temp1, Immediate(element_size));
1454 __ addl(temp2, Immediate(element_size));
1455 __ cmpl(temp1, temp3);
1456 __ j(kNotEqual, &loop);
1457
1458 __ Bind(read_barrier_slow_path->GetExitLabel());
1459 __ Bind(&done);
1460 } else {
1461 // Non read barrier code.
1462
1463 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1464 // poison/unpoison.
1465 NearLabel loop, done;
1466 __ cmpl(temp1, temp3);
1467 __ j(kEqual, &done);
1468 __ Bind(&loop);
1469 __ movl(CpuRegister(TMP), Address(temp1, 0));
1470 __ movl(Address(temp2, 0), CpuRegister(TMP));
1471 __ addl(temp1, Immediate(element_size));
1472 __ addl(temp2, Immediate(element_size));
1473 __ cmpl(temp1, temp3);
1474 __ j(kNotEqual, &loop);
1475 __ Bind(&done);
1476 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001477
1478 // We only need one card marking on the destination array.
1479 codegen_->MarkGCCard(temp1,
1480 temp2,
1481 dest,
1482 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001483 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001484
Roland Levillain0b671c02016-08-19 12:02:34 +01001485 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001486}
1487
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001488void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1489 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001490 LocationSummary::kCallOnMainAndSlowPath,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001491 kIntrinsified);
1492 InvokeRuntimeCallingConvention calling_convention;
1493 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1494 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1495 locations->SetOut(Location::RegisterLocation(RAX));
1496}
1497
1498void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1499 X86_64Assembler* assembler = GetAssembler();
1500 LocationSummary* locations = invoke->GetLocations();
1501
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001502 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001503 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001504
1505 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1506 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001507 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001508 codegen_->AddSlowPath(slow_path);
1509 __ j(kEqual, slow_path->GetEntryLabel());
1510
Serban Constantinescuba45db02016-07-12 22:53:02 +01001511 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001512 __ Bind(slow_path->GetExitLabel());
1513}
1514
Agi Csakif8cfb202015-08-13 17:54:54 -07001515void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1516 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1517 LocationSummary::kNoCall,
1518 kIntrinsified);
1519 locations->SetInAt(0, Location::RequiresRegister());
1520 locations->SetInAt(1, Location::RequiresRegister());
1521
1522 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1523 locations->AddTemp(Location::RegisterLocation(RCX));
1524 locations->AddTemp(Location::RegisterLocation(RDI));
1525
1526 // Set output, RSI needed for repe_cmpsq instruction anyways.
1527 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1528}
1529
1530void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1531 X86_64Assembler* assembler = GetAssembler();
1532 LocationSummary* locations = invoke->GetLocations();
1533
1534 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1535 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1536 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1537 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1538 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1539
Mark Mendell0c9497d2015-08-21 09:30:05 -04001540 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001541
1542 // Get offsets of count, value, and class fields within a string object.
1543 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1544 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1545 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1546
1547 // Note that the null check must have been done earlier.
1548 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1549
Vladimir Marko53b52002016-05-24 19:30:45 +01001550 StringEqualsOptimizations optimizations(invoke);
1551 if (!optimizations.GetArgumentNotNull()) {
1552 // Check if input is null, return false if it is.
1553 __ testl(arg, arg);
1554 __ j(kEqual, &return_false);
1555 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001556
Vladimir Marko53b52002016-05-24 19:30:45 +01001557 if (!optimizations.GetArgumentIsString()) {
1558 // Instanceof check for the argument by comparing class fields.
1559 // All string objects must have the same type since String cannot be subclassed.
1560 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1561 // If the argument is a string object, its class field must be equal to receiver's class field.
1562 __ movl(rcx, Address(str, class_offset));
1563 __ cmpl(rcx, Address(arg, class_offset));
1564 __ j(kNotEqual, &return_false);
1565 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001566
1567 // Reference equality check, return true if same reference.
1568 __ cmpl(str, arg);
1569 __ j(kEqual, &return_true);
1570
jessicahandojo4877b792016-09-08 19:49:13 -07001571 // Load length and compression flag of receiver string.
Agi Csakif8cfb202015-08-13 17:54:54 -07001572 __ movl(rcx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001573 // Check if lengths and compressiond flags are equal, return false if they're not.
1574 // Two identical strings will always have same compression style since
1575 // compression style is decided on alloc.
Agi Csakif8cfb202015-08-13 17:54:54 -07001576 __ cmpl(rcx, Address(arg, count_offset));
1577 __ j(kNotEqual, &return_false);
jessicahandojo4877b792016-09-08 19:49:13 -07001578
1579 if (mirror::kUseStringCompression) {
1580 NearLabel string_uncompressed;
1581 // Both string are compressed.
1582 __ cmpl(rcx, Immediate(0));
1583 __ j(kGreaterEqual, &string_uncompressed);
1584 // Divide string length by 2, rounding up, and continue as if uncompressed.
1585 // Merge clearing the compression flag with +1 for rounding.
1586 __ addl(rcx, Immediate(static_cast<int32_t>(0x80000001)));
1587 __ shrl(rcx, Immediate(1));
1588 __ Bind(&string_uncompressed);
1589 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001590 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001591 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001592 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1593 __ leal(rsi, Address(str, value_offset));
1594 __ leal(rdi, Address(arg, value_offset));
1595
1596 // Divide string length by 4 and adjust for lengths not divisible by 4.
1597 __ addl(rcx, Immediate(3));
1598 __ shrl(rcx, Immediate(2));
1599
jessicahandojo4877b792016-09-08 19:49:13 -07001600 // Assertions that must hold in order to compare strings 4 characters (uncompressed)
1601 // or 8 characters (compressed) at a time.
Agi Csakif8cfb202015-08-13 17:54:54 -07001602 DCHECK_ALIGNED(value_offset, 8);
1603 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1604
1605 // Loop to compare strings four characters at a time starting at the beginning of the string.
1606 __ repe_cmpsq();
1607 // If strings are not equal, zero flag will be cleared.
1608 __ j(kNotEqual, &return_false);
1609
1610 // Return true and exit the function.
1611 // If loop does not result in returning false, we return true.
1612 __ Bind(&return_true);
1613 __ movl(rsi, Immediate(1));
1614 __ jmp(&end);
1615
1616 // Return false and exit the function.
1617 __ Bind(&return_false);
1618 __ xorl(rsi, rsi);
1619 __ Bind(&end);
1620}
1621
Andreas Gampe21030dd2015-05-07 14:46:15 -07001622static void CreateStringIndexOfLocations(HInvoke* invoke,
1623 ArenaAllocator* allocator,
1624 bool start_at_zero) {
1625 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1626 LocationSummary::kCallOnSlowPath,
1627 kIntrinsified);
1628 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1629 locations->SetInAt(0, Location::RegisterLocation(RDI));
1630 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1631 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1632 // of the instruction explicitly.
1633 // Note: This works as we don't clobber RAX anywhere.
1634 locations->SetInAt(1, Location::RegisterLocation(RAX));
1635 if (!start_at_zero) {
1636 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1637 }
1638 // As we clobber RDI during execution anyways, also use it as the output.
1639 locations->SetOut(Location::SameAsFirstInput());
1640
1641 // repne scasw uses RCX as the counter.
1642 locations->AddTemp(Location::RegisterLocation(RCX));
1643 // Need another temporary to be able to compute the result.
1644 locations->AddTemp(Location::RequiresRegister());
1645}
1646
1647static void GenerateStringIndexOf(HInvoke* invoke,
1648 X86_64Assembler* assembler,
1649 CodeGeneratorX86_64* codegen,
1650 ArenaAllocator* allocator,
1651 bool start_at_zero) {
1652 LocationSummary* locations = invoke->GetLocations();
1653
1654 // Note that the null check must have been done earlier.
1655 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1656
1657 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1658 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1659 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1660 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1661 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1662
1663 // Check our assumptions for registers.
1664 DCHECK_EQ(string_obj.AsRegister(), RDI);
1665 DCHECK_EQ(search_value.AsRegister(), RAX);
1666 DCHECK_EQ(counter.AsRegister(), RCX);
1667 DCHECK_EQ(out.AsRegister(), RDI);
1668
1669 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001670 // 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 -07001671 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001672 HInstruction* code_point = invoke->InputAt(1);
1673 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001674 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001675 std::numeric_limits<uint16_t>::max()) {
1676 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1677 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1678 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1679 codegen->AddSlowPath(slow_path);
1680 __ jmp(slow_path->GetEntryLabel());
1681 __ Bind(slow_path->GetExitLabel());
1682 return;
1683 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001684 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001685 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1686 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1687 codegen->AddSlowPath(slow_path);
1688 __ j(kAbove, slow_path->GetEntryLabel());
1689 }
1690
jessicahandojo4877b792016-09-08 19:49:13 -07001691 // From here down, we know that we are looking for a char that fits in
1692 // 16 bits (uncompressed) or 8 bits (compressed).
Andreas Gampe21030dd2015-05-07 14:46:15 -07001693 // Location of reference to data array within the String object.
1694 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1695 // Location of count within the String object.
1696 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1697
1698 // Load string length, i.e., the count field of the string.
1699 __ movl(string_length, Address(string_obj, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001700 if (mirror::kUseStringCompression) {
1701 // Use TMP to keep string_length_flagged.
1702 __ movl(CpuRegister(TMP), string_length);
1703 // Mask out first bit used as compression flag.
1704 __ andl(string_length, Immediate(INT32_MAX));
1705 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001706
1707 // Do a length check.
1708 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001709 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001710 __ testl(string_length, string_length);
1711 __ j(kEqual, &not_found_label);
1712
1713 if (start_at_zero) {
1714 // Number of chars to scan is the same as the string length.
1715 __ movl(counter, string_length);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001716 // Move to the start of the string.
1717 __ addq(string_obj, Immediate(value_offset));
1718 } else {
1719 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1720
1721 // Do a start_index check.
1722 __ cmpl(start_index, string_length);
1723 __ j(kGreaterEqual, &not_found_label);
1724
1725 // Ensure we have a start index >= 0;
1726 __ xorl(counter, counter);
1727 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001728 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001729
jessicahandojo4877b792016-09-08 19:49:13 -07001730 if (mirror::kUseStringCompression) {
1731 NearLabel modify_counter, offset_uncompressed_label;
1732 __ cmpl(CpuRegister(TMP), Immediate(0));
1733 __ j(kGreaterEqual, &offset_uncompressed_label);
1734 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1735 __ jmp(&modify_counter);
1736 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1737 __ Bind(&offset_uncompressed_label);
1738 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1739 __ Bind(&modify_counter);
1740 } else {
1741 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1742 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001743 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1744 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1745 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1746 }
1747
jessicahandojo4877b792016-09-08 19:49:13 -07001748 if (mirror::kUseStringCompression) {
1749 NearLabel uncompressed_string_comparison;
1750 NearLabel comparison_done;
1751 __ cmpl(CpuRegister(TMP), Immediate(0));
1752 __ j(kGreater, &uncompressed_string_comparison);
1753 // Check if RAX (search_value) is ASCII.
1754 __ cmpl(search_value, Immediate(127));
1755 __ j(kGreater, &not_found_label);
1756 // Comparing byte-per-byte.
1757 __ repne_scasb();
1758 __ jmp(&comparison_done);
1759 // Everything is set up for repne scasw:
1760 // * Comparison address in RDI.
1761 // * Counter in ECX.
1762 __ Bind(&uncompressed_string_comparison);
1763 __ repne_scasw();
1764 __ Bind(&comparison_done);
1765 } else {
1766 __ repne_scasw();
1767 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001768 // Did we find a match?
1769 __ j(kNotEqual, &not_found_label);
1770
1771 // Yes, we matched. Compute the index of the result.
1772 __ subl(string_length, counter);
1773 __ leal(out, Address(string_length, -1));
1774
Mark Mendell0c9497d2015-08-21 09:30:05 -04001775 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001776 __ jmp(&done);
1777
1778 // Failed to match; return -1.
1779 __ Bind(&not_found_label);
1780 __ movl(out, Immediate(-1));
1781
1782 // And join up at the end.
1783 __ Bind(&done);
1784 if (slow_path != nullptr) {
1785 __ Bind(slow_path->GetExitLabel());
1786 }
1787}
1788
1789void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001790 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001791}
1792
1793void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001794 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001795}
1796
1797void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001798 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001799}
1800
1801void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001802 GenerateStringIndexOf(
1803 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001804}
1805
Jeff Hao848f70a2014-01-15 13:49:50 -08001806void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1807 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001808 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001809 kIntrinsified);
1810 InvokeRuntimeCallingConvention calling_convention;
1811 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1812 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1813 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1814 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1815 locations->SetOut(Location::RegisterLocation(RAX));
1816}
1817
1818void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1819 X86_64Assembler* assembler = GetAssembler();
1820 LocationSummary* locations = invoke->GetLocations();
1821
1822 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1823 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001824 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001825 codegen_->AddSlowPath(slow_path);
1826 __ j(kEqual, slow_path->GetEntryLabel());
1827
Serban Constantinescuba45db02016-07-12 22:53:02 +01001828 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001829 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001830 __ Bind(slow_path->GetExitLabel());
1831}
1832
1833void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1834 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001835 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001836 kIntrinsified);
1837 InvokeRuntimeCallingConvention calling_convention;
1838 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1839 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1840 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1841 locations->SetOut(Location::RegisterLocation(RAX));
1842}
1843
1844void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001845 // No need to emit code checking whether `locations->InAt(2)` is a null
1846 // pointer, as callers of the native method
1847 //
1848 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1849 //
1850 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001851 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001852 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001853}
1854
1855void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1856 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001857 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001858 kIntrinsified);
1859 InvokeRuntimeCallingConvention calling_convention;
1860 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1861 locations->SetOut(Location::RegisterLocation(RAX));
1862}
1863
1864void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1865 X86_64Assembler* assembler = GetAssembler();
1866 LocationSummary* locations = invoke->GetLocations();
1867
1868 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1869 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001870 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001871 codegen_->AddSlowPath(slow_path);
1872 __ j(kEqual, slow_path->GetEntryLabel());
1873
Serban Constantinescuba45db02016-07-12 22:53:02 +01001874 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001875 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001876 __ Bind(slow_path->GetExitLabel());
1877}
1878
Mark Mendell8f8926a2015-08-17 11:39:06 -04001879void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1880 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1881 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1882 LocationSummary::kNoCall,
1883 kIntrinsified);
1884 locations->SetInAt(0, Location::RequiresRegister());
1885 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1886 locations->SetInAt(2, Location::RequiresRegister());
1887 locations->SetInAt(3, Location::RequiresRegister());
1888 locations->SetInAt(4, Location::RequiresRegister());
1889
1890 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1891 locations->AddTemp(Location::RegisterLocation(RSI));
1892 locations->AddTemp(Location::RegisterLocation(RDI));
1893 locations->AddTemp(Location::RegisterLocation(RCX));
1894}
1895
1896void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1897 X86_64Assembler* assembler = GetAssembler();
1898 LocationSummary* locations = invoke->GetLocations();
1899
1900 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1901 // Location of data in char array buffer.
1902 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1903 // Location of char array data in string.
1904 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1905
1906 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1907 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1908 Location srcBegin = locations->InAt(1);
1909 int srcBegin_value =
1910 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1911 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1912 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1913 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1914
1915 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1916 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1917 DCHECK_EQ(char_size, 2u);
1918
jessicahandojo4877b792016-09-08 19:49:13 -07001919 NearLabel done;
Mark Mendell8f8926a2015-08-17 11:39:06 -04001920 // Compute the number of chars (words) to move.
1921 __ movl(CpuRegister(RCX), srcEnd);
1922 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001923 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001924 } else {
1925 DCHECK(srcBegin.IsRegister());
1926 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1927 }
jessicahandojo4877b792016-09-08 19:49:13 -07001928 if (mirror::kUseStringCompression) {
1929 NearLabel copy_uncompressed, copy_loop;
1930 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1931 DCHECK_EQ(c_char_size, 1u);
1932 // Location of count in string.
1933 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Mark Mendell8f8926a2015-08-17 11:39:06 -04001934
jessicahandojo4877b792016-09-08 19:49:13 -07001935 __ cmpl(Address(obj, count_offset), Immediate(0));
1936 __ j(kGreaterEqual, &copy_uncompressed);
1937 // Compute the address of the source string by adding the number of chars from
1938 // the source beginning to the value offset of a string.
1939 __ leaq(CpuRegister(RSI),
1940 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1941 // Start the loop to copy String's value to Array of Char.
1942 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1943
1944 __ Bind(&copy_loop);
1945 __ jrcxz(&done);
1946 // Use TMP as temporary (convert byte from RSI to word).
1947 // TODO: Selecting RAX as the temporary and using LODSB/STOSW.
1948 __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
1949 __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
1950 __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
1951 __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
1952 // TODO: Add support for LOOP to X86_64Assembler.
1953 __ subl(CpuRegister(RCX), Immediate(1));
1954 __ jmp(&copy_loop);
1955
1956 __ Bind(&copy_uncompressed);
1957 }
1958
1959 __ leaq(CpuRegister(RSI),
1960 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
1961 // Compute the address of the destination buffer.
1962 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001963 // Do the move.
1964 __ rep_movsw();
jessicahandojo4877b792016-09-08 19:49:13 -07001965
1966 __ Bind(&done);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001967}
1968
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001969static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1970 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1971 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1972 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1973 // to avoid a SIGBUS.
1974 switch (size) {
1975 case Primitive::kPrimByte:
1976 __ movsxb(out, Address(address, 0));
1977 break;
1978 case Primitive::kPrimShort:
1979 __ movsxw(out, Address(address, 0));
1980 break;
1981 case Primitive::kPrimInt:
1982 __ movl(out, Address(address, 0));
1983 break;
1984 case Primitive::kPrimLong:
1985 __ movq(out, Address(address, 0));
1986 break;
1987 default:
1988 LOG(FATAL) << "Type not recognized for peek: " << size;
1989 UNREACHABLE();
1990 }
1991}
1992
1993void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1994 CreateIntToIntLocations(arena_, invoke);
1995}
1996
1997void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1998 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1999}
2000
2001void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
2002 CreateIntToIntLocations(arena_, invoke);
2003}
2004
2005void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
2006 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2007}
2008
2009void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
2010 CreateIntToIntLocations(arena_, invoke);
2011}
2012
2013void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
2014 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2015}
2016
2017void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
2018 CreateIntToIntLocations(arena_, invoke);
2019}
2020
2021void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
2022 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
2023}
2024
2025static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
2026 LocationSummary* locations = new (arena) LocationSummary(invoke,
2027 LocationSummary::kNoCall,
2028 kIntrinsified);
2029 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002030 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002031}
2032
2033static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
2034 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04002035 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002036 // x86 allows unaligned access. We do not have to check the input or use specific instructions
2037 // to avoid a SIGBUS.
2038 switch (size) {
2039 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04002040 if (value.IsConstant()) {
2041 __ movb(Address(address, 0),
2042 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2043 } else {
2044 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
2045 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002046 break;
2047 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04002048 if (value.IsConstant()) {
2049 __ movw(Address(address, 0),
2050 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2051 } else {
2052 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
2053 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002054 break;
2055 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04002056 if (value.IsConstant()) {
2057 __ movl(Address(address, 0),
2058 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2059 } else {
2060 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
2061 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002062 break;
2063 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04002064 if (value.IsConstant()) {
2065 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
2066 DCHECK(IsInt<32>(v));
2067 int32_t v_32 = v;
2068 __ movq(Address(address, 0), Immediate(v_32));
2069 } else {
2070 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
2071 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002072 break;
2073 default:
2074 LOG(FATAL) << "Type not recognized for poke: " << size;
2075 UNREACHABLE();
2076 }
2077}
2078
2079void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2080 CreateIntIntToVoidLocations(arena_, invoke);
2081}
2082
2083void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2084 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
2085}
2086
2087void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2088 CreateIntIntToVoidLocations(arena_, invoke);
2089}
2090
2091void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2092 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2093}
2094
2095void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2096 CreateIntIntToVoidLocations(arena_, invoke);
2097}
2098
2099void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2100 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2101}
2102
2103void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2104 CreateIntIntToVoidLocations(arena_, invoke);
2105}
2106
2107void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2108 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
2109}
2110
2111void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2112 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2113 LocationSummary::kNoCall,
2114 kIntrinsified);
2115 locations->SetOut(Location::RequiresRegister());
2116}
2117
2118void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2119 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07002120 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002121 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002122}
2123
Roland Levillain0d5a2812015-11-13 10:07:31 +00002124static void GenUnsafeGet(HInvoke* invoke,
2125 Primitive::Type type,
2126 bool is_volatile ATTRIBUTE_UNUSED,
2127 CodeGeneratorX86_64* codegen) {
2128 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
2129 LocationSummary* locations = invoke->GetLocations();
2130 Location base_loc = locations->InAt(1);
2131 CpuRegister base = base_loc.AsRegister<CpuRegister>();
2132 Location offset_loc = locations->InAt(2);
2133 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
2134 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002135 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002136
Andreas Gampe878d58c2015-01-15 23:24:00 -08002137 switch (type) {
2138 case Primitive::kPrimInt:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002139 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002140 break;
2141
2142 case Primitive::kPrimNot: {
2143 if (kEmitCompilerReadBarrier) {
2144 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002145 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2146 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002147 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002148 } else {
2149 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2150 codegen->GenerateReadBarrierSlow(
2151 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2152 }
2153 } else {
2154 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2155 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002156 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002157 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002158 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002159
2160 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002161 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002162 break;
2163
2164 default:
2165 LOG(FATAL) << "Unsupported op size " << type;
2166 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002167 }
2168}
2169
Vladimir Marko953437b2016-08-24 08:30:46 +00002170static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002171 bool can_call = kEmitCompilerReadBarrier &&
2172 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2173 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002174 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00002175 can_call ?
2176 LocationSummary::kCallOnSlowPath :
2177 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002178 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002179 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002180 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002181 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002182 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002183 locations->SetInAt(1, Location::RequiresRegister());
2184 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002185 locations->SetOut(Location::RequiresRegister(),
2186 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002187}
2188
2189void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002190 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002191}
2192void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002193 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194}
2195void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002196 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002197}
2198void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002199 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002200}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002201void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002202 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002203}
2204void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002205 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002206}
2207
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002208
2209void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002210 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002211}
2212void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002213 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002214}
2215void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002216 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002217}
2218void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002219 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002220}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002221void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002222 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002223}
2224void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002225 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002226}
2227
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002228
2229static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
2230 Primitive::Type type,
2231 HInvoke* invoke) {
2232 LocationSummary* locations = new (arena) LocationSummary(invoke,
2233 LocationSummary::kNoCall,
2234 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002235 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002236 locations->SetInAt(1, Location::RequiresRegister());
2237 locations->SetInAt(2, Location::RequiresRegister());
2238 locations->SetInAt(3, Location::RequiresRegister());
2239 if (type == Primitive::kPrimNot) {
2240 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002241 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002242 locations->AddTemp(Location::RequiresRegister());
2243 }
2244}
2245
2246void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2247 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2248}
2249void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2250 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2251}
2252void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2253 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2254}
2255void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2256 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2257}
2258void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2259 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2260}
2261void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2262 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2263}
2264void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2265 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2266}
2267void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2268 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2269}
2270void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2271 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2272}
2273
2274// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2275// memory model.
2276static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2277 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002278 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002279 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2280 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2281 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2282
2283 if (type == Primitive::kPrimLong) {
2284 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002285 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2286 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2287 __ movl(temp, value);
2288 __ PoisonHeapReference(temp);
2289 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002290 } else {
2291 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2292 }
2293
2294 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002295 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002296 }
2297
2298 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002299 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002300 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2301 locations->GetTemp(1).AsRegister<CpuRegister>(),
2302 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002303 value,
2304 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002305 }
2306}
2307
2308void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002309 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002310}
2311void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002312 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002313}
2314void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002315 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002316}
2317void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002318 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002319}
2320void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002321 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002322}
2323void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002324 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002325}
2326void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002327 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002328}
2329void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002330 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002331}
2332void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002333 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002334}
2335
Mark Mendell58d25fd2015-04-03 14:52:31 -04002336static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2337 HInvoke* invoke) {
2338 LocationSummary* locations = new (arena) LocationSummary(invoke,
2339 LocationSummary::kNoCall,
2340 kIntrinsified);
2341 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2342 locations->SetInAt(1, Location::RequiresRegister());
2343 locations->SetInAt(2, Location::RequiresRegister());
2344 // expected value must be in EAX/RAX.
2345 locations->SetInAt(3, Location::RegisterLocation(RAX));
2346 locations->SetInAt(4, Location::RequiresRegister());
2347
2348 locations->SetOut(Location::RequiresRegister());
2349 if (type == Primitive::kPrimNot) {
2350 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002351 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002352 locations->AddTemp(Location::RequiresRegister());
2353 }
2354}
2355
2356void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2357 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2358}
2359
2360void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2361 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2362}
2363
2364void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002365 // The UnsafeCASObject intrinsic is missing a read barrier, and
2366 // therefore sometimes does not work as expected (b/25883050).
2367 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01002368 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00002369 //
Roland Levillain3d312422016-06-23 13:53:42 +01002370 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
Roland Levillain391b8662015-12-18 11:43:38 +00002371 // this intrinsic.
2372 if (kEmitCompilerReadBarrier) {
2373 return;
2374 }
2375
Mark Mendell58d25fd2015-04-03 14:52:31 -04002376 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2377}
2378
2379static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002380 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002381 LocationSummary* locations = invoke->GetLocations();
2382
2383 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2384 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2385 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002386 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002387 DCHECK_EQ(expected.AsRegister(), RAX);
2388 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
2389 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2390
Roland Levillainb488b782015-10-22 11:38:49 +01002391 if (type == Primitive::kPrimNot) {
2392 // Mark card for object assuming new value is stored.
2393 bool value_can_be_null = true; // TODO: Worth finding out this information?
2394 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2395 locations->GetTemp(1).AsRegister<CpuRegister>(),
2396 base,
2397 value,
2398 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01002399
Roland Levillainb488b782015-10-22 11:38:49 +01002400 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2401 Register value_reg = value.AsRegister();
2402 if (kPoisonHeapReferences) {
2403 if (base_equals_value) {
2404 // If `base` and `value` are the same register location, move
2405 // `value_reg` to a temporary register. This way, poisoning
2406 // `value_reg` won't invalidate `base`.
2407 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
2408 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002409 }
Roland Levillainb488b782015-10-22 11:38:49 +01002410
2411 // Check that the register allocator did not assign the location
2412 // of `expected` (RAX) to `value` nor to `base`, so that heap
2413 // poisoning (when enabled) works as intended below.
2414 // - If `value` were equal to `expected`, both references would
2415 // be poisoned twice, meaning they would not be poisoned at
2416 // all, as heap poisoning uses address negation.
2417 // - If `base` were equal to `expected`, poisoning `expected`
2418 // would invalidate `base`.
2419 DCHECK_NE(value_reg, expected.AsRegister());
2420 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2421
2422 __ PoisonHeapReference(expected);
2423 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002424 }
2425
Roland Levillain391b8662015-12-18 11:43:38 +00002426 // TODO: Add a read barrier for the reference stored in the object
2427 // before attempting the CAS, similar to the one in the
2428 // art::Unsafe_compareAndSwapObject JNI implementation.
2429 //
2430 // Note that this code is not (yet) used when read barriers are
2431 // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject).
2432 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillainb488b782015-10-22 11:38:49 +01002433 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002434
Roland Levillain0d5a2812015-11-13 10:07:31 +00002435 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002436 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002437
Roland Levillainb488b782015-10-22 11:38:49 +01002438 // Convert ZF into the boolean result.
2439 __ setcc(kZero, out);
2440 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002441
Roland Levillain391b8662015-12-18 11:43:38 +00002442 // If heap poisoning is enabled, we need to unpoison the values
2443 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002444 if (kPoisonHeapReferences) {
2445 if (base_equals_value) {
2446 // `value_reg` has been moved to a temporary register, no need
2447 // to unpoison it.
2448 } else {
2449 // Ensure `value` is different from `out`, so that unpoisoning
2450 // the former does not invalidate the latter.
2451 DCHECK_NE(value_reg, out.AsRegister());
2452 __ UnpoisonHeapReference(CpuRegister(value_reg));
2453 }
2454 // Ensure `expected` is different from `out`, so that unpoisoning
2455 // the former does not invalidate the latter.
2456 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2457 __ UnpoisonHeapReference(expected);
2458 }
2459 } else {
2460 if (type == Primitive::kPrimInt) {
2461 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2462 } else if (type == Primitive::kPrimLong) {
2463 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2464 } else {
2465 LOG(FATAL) << "Unexpected CAS type " << type;
2466 }
2467
Roland Levillain0d5a2812015-11-13 10:07:31 +00002468 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002469 // scheduling barriers at this time.
2470
2471 // Convert ZF into the boolean result.
2472 __ setcc(kZero, out);
2473 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002474 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002475}
2476
2477void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2478 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2479}
2480
2481void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2482 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2483}
2484
2485void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002486 // The UnsafeCASObject intrinsic is missing a read barrier, and
2487 // therefore sometimes does not work as expected (b/25883050).
2488 // Turn it off temporarily as a quick fix, until the read barrier is
2489 // implemented (see TODO in GenCAS).
2490 //
2491 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
2492 // this intrinsic.
2493 DCHECK(!kEmitCompilerReadBarrier);
2494
Mark Mendell58d25fd2015-04-03 14:52:31 -04002495 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2496}
2497
2498void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2499 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2500 LocationSummary::kNoCall,
2501 kIntrinsified);
2502 locations->SetInAt(0, Location::RequiresRegister());
2503 locations->SetOut(Location::SameAsFirstInput());
2504 locations->AddTemp(Location::RequiresRegister());
2505}
2506
2507static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2508 X86_64Assembler* assembler) {
2509 Immediate imm_shift(shift);
2510 Immediate imm_mask(mask);
2511 __ movl(temp, reg);
2512 __ shrl(reg, imm_shift);
2513 __ andl(temp, imm_mask);
2514 __ andl(reg, imm_mask);
2515 __ shll(temp, imm_shift);
2516 __ orl(reg, temp);
2517}
2518
2519void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002520 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002521 LocationSummary* locations = invoke->GetLocations();
2522
2523 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2524 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2525
2526 /*
2527 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2528 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2529 * compared to generic luni implementation which has 5 rounds of swapping bits.
2530 * x = bswap x
2531 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2532 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2533 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2534 */
2535 __ bswapl(reg);
2536 SwapBits(reg, temp, 1, 0x55555555, assembler);
2537 SwapBits(reg, temp, 2, 0x33333333, assembler);
2538 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2539}
2540
2541void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2542 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2543 LocationSummary::kNoCall,
2544 kIntrinsified);
2545 locations->SetInAt(0, Location::RequiresRegister());
2546 locations->SetOut(Location::SameAsFirstInput());
2547 locations->AddTemp(Location::RequiresRegister());
2548 locations->AddTemp(Location::RequiresRegister());
2549}
2550
2551static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2552 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2553 Immediate imm_shift(shift);
2554 __ movq(temp_mask, Immediate(mask));
2555 __ movq(temp, reg);
2556 __ shrq(reg, imm_shift);
2557 __ andq(temp, temp_mask);
2558 __ andq(reg, temp_mask);
2559 __ shlq(temp, imm_shift);
2560 __ orq(reg, temp);
2561}
2562
2563void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002564 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002565 LocationSummary* locations = invoke->GetLocations();
2566
2567 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2568 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2569 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2570
2571 /*
2572 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2573 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2574 * compared to generic luni implementation which has 5 rounds of swapping bits.
2575 * x = bswap x
2576 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2577 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2578 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2579 */
2580 __ bswapq(reg);
2581 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2582 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2583 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2584}
2585
Aart Bik3f67e692016-01-15 14:35:12 -08002586static void CreateBitCountLocations(
2587 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2588 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2589 // Do nothing if there is no popcnt support. This results in generating
2590 // a call for the intrinsic rather than direct code.
2591 return;
2592 }
2593 LocationSummary* locations = new (arena) LocationSummary(invoke,
2594 LocationSummary::kNoCall,
2595 kIntrinsified);
2596 locations->SetInAt(0, Location::Any());
2597 locations->SetOut(Location::RequiresRegister());
2598}
2599
Aart Bikc5d47542016-01-27 17:00:35 -08002600static void GenBitCount(X86_64Assembler* assembler,
2601 CodeGeneratorX86_64* codegen,
2602 HInvoke* invoke,
2603 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002604 LocationSummary* locations = invoke->GetLocations();
2605 Location src = locations->InAt(0);
2606 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2607
2608 if (invoke->InputAt(0)->IsConstant()) {
2609 // Evaluate this at compile time.
2610 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002611 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002612 ? POPCOUNT(static_cast<uint64_t>(value))
2613 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002614 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002615 return;
2616 }
2617
2618 if (src.IsRegister()) {
2619 if (is_long) {
2620 __ popcntq(out, src.AsRegister<CpuRegister>());
2621 } else {
2622 __ popcntl(out, src.AsRegister<CpuRegister>());
2623 }
2624 } else if (is_long) {
2625 DCHECK(src.IsDoubleStackSlot());
2626 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2627 } else {
2628 DCHECK(src.IsStackSlot());
2629 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2630 }
2631}
2632
2633void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2634 CreateBitCountLocations(arena_, codegen_, invoke);
2635}
2636
2637void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002638 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002639}
2640
2641void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2642 CreateBitCountLocations(arena_, codegen_, invoke);
2643}
2644
2645void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002646 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2647}
2648
Aart Bikc5d47542016-01-27 17:00:35 -08002649static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2650 LocationSummary* locations = new (arena) LocationSummary(invoke,
2651 LocationSummary::kNoCall,
2652 kIntrinsified);
2653 locations->SetInAt(0, Location::Any());
2654 locations->SetOut(Location::RequiresRegister());
2655 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2656 : Location::RequiresRegister()); // any will do
2657}
2658
2659static void GenOneBit(X86_64Assembler* assembler,
2660 CodeGeneratorX86_64* codegen,
2661 HInvoke* invoke,
2662 bool is_high, bool is_long) {
2663 LocationSummary* locations = invoke->GetLocations();
2664 Location src = locations->InAt(0);
2665 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2666
2667 if (invoke->InputAt(0)->IsConstant()) {
2668 // Evaluate this at compile time.
2669 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2670 if (value == 0) {
2671 __ xorl(out, out); // Clears upper bits too.
2672 return;
2673 }
2674 // Nonzero value.
2675 if (is_high) {
2676 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2677 : 31 - CLZ(static_cast<uint32_t>(value));
2678 } else {
2679 value = is_long ? CTZ(static_cast<uint64_t>(value))
2680 : CTZ(static_cast<uint32_t>(value));
2681 }
2682 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002683 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002684 } else {
2685 codegen->Load32BitValue(out, 1 << value);
2686 }
2687 return;
2688 }
2689
2690 // Handle the non-constant cases.
2691 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2692 if (is_high) {
2693 // Use architectural support: basically 1 << bsr.
2694 if (src.IsRegister()) {
2695 if (is_long) {
2696 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2697 } else {
2698 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2699 }
2700 } else if (is_long) {
2701 DCHECK(src.IsDoubleStackSlot());
2702 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2703 } else {
2704 DCHECK(src.IsStackSlot());
2705 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2706 }
2707 // BSR sets ZF if the input was zero.
2708 NearLabel is_zero, done;
2709 __ j(kEqual, &is_zero);
2710 __ movl(out, Immediate(1)); // Clears upper bits too.
2711 if (is_long) {
2712 __ shlq(out, tmp);
2713 } else {
2714 __ shll(out, tmp);
2715 }
2716 __ jmp(&done);
2717 __ Bind(&is_zero);
2718 __ xorl(out, out); // Clears upper bits too.
2719 __ Bind(&done);
2720 } else {
2721 // Copy input into temporary.
2722 if (src.IsRegister()) {
2723 if (is_long) {
2724 __ movq(tmp, src.AsRegister<CpuRegister>());
2725 } else {
2726 __ movl(tmp, src.AsRegister<CpuRegister>());
2727 }
2728 } else if (is_long) {
2729 DCHECK(src.IsDoubleStackSlot());
2730 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2731 } else {
2732 DCHECK(src.IsStackSlot());
2733 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2734 }
2735 // Do the bit twiddling: basically tmp & -tmp;
2736 if (is_long) {
2737 __ movq(out, tmp);
2738 __ negq(tmp);
2739 __ andq(out, tmp);
2740 } else {
2741 __ movl(out, tmp);
2742 __ negl(tmp);
2743 __ andl(out, tmp);
2744 }
2745 }
2746}
2747
2748void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2749 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2750}
2751
2752void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2753 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2754}
2755
2756void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2757 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2758}
2759
2760void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2761 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2762}
2763
2764void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2765 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2766}
2767
2768void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2769 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2770}
2771
2772void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2773 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2774}
2775
2776void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2777 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002778}
2779
Mark Mendelld5897672015-08-12 21:16:41 -04002780static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2781 LocationSummary* locations = new (arena) LocationSummary(invoke,
2782 LocationSummary::kNoCall,
2783 kIntrinsified);
2784 locations->SetInAt(0, Location::Any());
2785 locations->SetOut(Location::RequiresRegister());
2786}
2787
Aart Bikc5d47542016-01-27 17:00:35 -08002788static void GenLeadingZeros(X86_64Assembler* assembler,
2789 CodeGeneratorX86_64* codegen,
2790 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002791 LocationSummary* locations = invoke->GetLocations();
2792 Location src = locations->InAt(0);
2793 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2794
2795 int zero_value_result = is_long ? 64 : 32;
2796 if (invoke->InputAt(0)->IsConstant()) {
2797 // Evaluate this at compile time.
2798 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2799 if (value == 0) {
2800 value = zero_value_result;
2801 } else {
2802 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2803 }
Aart Bikc5d47542016-01-27 17:00:35 -08002804 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002805 return;
2806 }
2807
2808 // Handle the non-constant cases.
2809 if (src.IsRegister()) {
2810 if (is_long) {
2811 __ bsrq(out, src.AsRegister<CpuRegister>());
2812 } else {
2813 __ bsrl(out, src.AsRegister<CpuRegister>());
2814 }
2815 } else if (is_long) {
2816 DCHECK(src.IsDoubleStackSlot());
2817 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2818 } else {
2819 DCHECK(src.IsStackSlot());
2820 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2821 }
2822
2823 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002824 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002825 __ j(kEqual, &is_zero);
2826
2827 // Correct the result from BSR to get the CLZ result.
2828 __ xorl(out, Immediate(zero_value_result - 1));
2829 __ jmp(&done);
2830
2831 // Fix the zero case with the expected result.
2832 __ Bind(&is_zero);
2833 __ movl(out, Immediate(zero_value_result));
2834
2835 __ Bind(&done);
2836}
2837
2838void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2839 CreateLeadingZeroLocations(arena_, invoke);
2840}
2841
2842void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002843 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002844}
2845
2846void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2847 CreateLeadingZeroLocations(arena_, invoke);
2848}
2849
2850void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002851 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002852}
2853
Mark Mendell2d554792015-09-15 21:45:18 -04002854static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2855 LocationSummary* locations = new (arena) LocationSummary(invoke,
2856 LocationSummary::kNoCall,
2857 kIntrinsified);
2858 locations->SetInAt(0, Location::Any());
2859 locations->SetOut(Location::RequiresRegister());
2860}
2861
Aart Bikc5d47542016-01-27 17:00:35 -08002862static void GenTrailingZeros(X86_64Assembler* assembler,
2863 CodeGeneratorX86_64* codegen,
2864 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002865 LocationSummary* locations = invoke->GetLocations();
2866 Location src = locations->InAt(0);
2867 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2868
2869 int zero_value_result = is_long ? 64 : 32;
2870 if (invoke->InputAt(0)->IsConstant()) {
2871 // Evaluate this at compile time.
2872 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2873 if (value == 0) {
2874 value = zero_value_result;
2875 } else {
2876 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2877 }
Aart Bikc5d47542016-01-27 17:00:35 -08002878 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002879 return;
2880 }
2881
2882 // Handle the non-constant cases.
2883 if (src.IsRegister()) {
2884 if (is_long) {
2885 __ bsfq(out, src.AsRegister<CpuRegister>());
2886 } else {
2887 __ bsfl(out, src.AsRegister<CpuRegister>());
2888 }
2889 } else if (is_long) {
2890 DCHECK(src.IsDoubleStackSlot());
2891 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2892 } else {
2893 DCHECK(src.IsStackSlot());
2894 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2895 }
2896
2897 // BSF sets ZF if the input was zero, and the output is undefined.
2898 NearLabel done;
2899 __ j(kNotEqual, &done);
2900
2901 // Fix the zero case with the expected result.
2902 __ movl(out, Immediate(zero_value_result));
2903
2904 __ Bind(&done);
2905}
2906
2907void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2908 CreateTrailingZeroLocations(arena_, invoke);
2909}
2910
2911void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002912 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002913}
2914
2915void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2916 CreateTrailingZeroLocations(arena_, invoke);
2917}
2918
2919void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002920 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2921}
2922
Serguei Katkov288c7a82016-05-16 11:53:15 +06002923void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2924 if (kEmitCompilerReadBarrier) {
2925 // Do not intrinsify this call with the read barrier configuration.
2926 return;
2927 }
2928 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2929 LocationSummary::kCallOnSlowPath,
2930 kIntrinsified);
2931 locations->SetInAt(0, Location::RequiresRegister());
2932 locations->SetOut(Location::SameAsFirstInput());
2933 locations->AddTemp(Location::RequiresRegister());
2934}
2935
2936void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2937 DCHECK(!kEmitCompilerReadBarrier);
2938 LocationSummary* locations = invoke->GetLocations();
2939 X86_64Assembler* assembler = GetAssembler();
2940
2941 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2942 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2943
2944 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2945 codegen_->AddSlowPath(slow_path);
2946
2947 // Load ArtMethod first.
2948 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2949 DCHECK(invoke_direct != nullptr);
2950 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2951 invoke_direct, locations->GetTemp(0));
2952 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2953 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2954
2955 // Now get declaring class.
2956 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2957
2958 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2959 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2960 DCHECK_NE(slow_path_flag_offset, 0u);
2961 DCHECK_NE(disable_flag_offset, 0u);
2962 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2963
2964 // Check static flags preventing us for using intrinsic.
2965 if (slow_path_flag_offset == disable_flag_offset + 1) {
2966 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2967 __ j(kNotEqual, slow_path->GetEntryLabel());
2968 } else {
2969 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2970 __ j(kNotEqual, slow_path->GetEntryLabel());
2971 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2972 __ j(kNotEqual, slow_path->GetEntryLabel());
2973 }
2974
2975 // Fast path.
2976 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2977 codegen_->MaybeRecordImplicitNullCheck(invoke);
2978 __ MaybeUnpoisonHeapReference(out);
2979 __ Bind(slow_path->GetExitLabel());
2980}
2981
Aart Bik2f9fcc92016-03-01 15:16:54 -08002982UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2983UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002984
Aart Bik0e54c012016-03-04 12:08:31 -08002985// 1.8.
2986UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2987UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2988UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2989UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2990UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002991
Aart Bik2f9fcc92016-03-01 15:16:54 -08002992UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002993
2994#undef __
2995
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002996} // namespace x86_64
2997} // namespace art