blob: f78a7268c1cb4a4f15b06b4e9c662c028785b63b [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"
27#include "mirror/array-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080028#include "mirror/string.h"
29#include "thread.h"
30#include "utils/x86_64/assembler_x86_64.h"
31#include "utils/x86_64/constants_x86_64.h"
32
33namespace art {
34
35namespace x86_64 {
36
Mark Mendellfb8d2792015-03-31 22:16:59 -040037IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
38 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
39}
40
41
Andreas Gampe71fb52f2014-12-29 17:43:08 -080042X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
43 return reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
44}
45
Andreas Gampe878d58c2015-01-15 23:24:00 -080046ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080047 return codegen_->GetGraph()->GetArena();
48}
49
50bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
51 Dispatch(invoke);
52 const LocationSummary* res = invoke->GetLocations();
53 return res != nullptr && res->Intrinsified();
54}
55
56#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
57
58// TODO: trg as memory.
59static void MoveFromReturnRegister(Location trg,
60 Primitive::Type type,
61 CodeGeneratorX86_64* codegen) {
62 if (!trg.IsValid()) {
63 DCHECK(type == Primitive::kPrimVoid);
64 return;
65 }
66
67 switch (type) {
68 case Primitive::kPrimBoolean:
69 case Primitive::kPrimByte:
70 case Primitive::kPrimChar:
71 case Primitive::kPrimShort:
72 case Primitive::kPrimInt:
73 case Primitive::kPrimNot: {
74 CpuRegister trg_reg = trg.AsRegister<CpuRegister>();
75 if (trg_reg.AsRegister() != RAX) {
76 __ movl(trg_reg, CpuRegister(RAX));
77 }
78 break;
79 }
80 case Primitive::kPrimLong: {
81 CpuRegister trg_reg = trg.AsRegister<CpuRegister>();
82 if (trg_reg.AsRegister() != RAX) {
83 __ movq(trg_reg, CpuRegister(RAX));
84 }
85 break;
86 }
87
88 case Primitive::kPrimVoid:
89 LOG(FATAL) << "Unexpected void type for valid location " << trg;
90 UNREACHABLE();
91
92 case Primitive::kPrimDouble: {
93 XmmRegister trg_reg = trg.AsFpuRegister<XmmRegister>();
94 if (trg_reg.AsFloatRegister() != XMM0) {
95 __ movsd(trg_reg, XmmRegister(XMM0));
96 }
97 break;
98 }
99 case Primitive::kPrimFloat: {
100 XmmRegister trg_reg = trg.AsFpuRegister<XmmRegister>();
101 if (trg_reg.AsFloatRegister() != XMM0) {
102 __ movss(trg_reg, XmmRegister(XMM0));
103 }
104 break;
105 }
106 }
107}
108
Roland Levillainec525fc2015-04-28 15:50:20 +0100109static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100110 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +0100111 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800112}
113
114// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
115// call. This will copy the arguments into the positions for a regular call.
116//
117// Note: The actual parameters are required to be in the locations given by the invoke's location
118// summary. If an intrinsic modifies those locations before a slowpath call, they must be
119// restored!
120class IntrinsicSlowPathX86_64 : public SlowPathCodeX86_64 {
121 public:
122 explicit IntrinsicSlowPathX86_64(HInvoke* invoke) : invoke_(invoke) { }
123
124 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
125 CodeGeneratorX86_64* codegen = down_cast<CodeGeneratorX86_64*>(codegen_in);
126 __ Bind(GetEntryLabel());
127
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000128 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800129
Roland Levillainec525fc2015-04-28 15:50:20 +0100130 MoveArguments(invoke_, codegen);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800131
132 if (invoke_->IsInvokeStaticOrDirect()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100133 codegen->GenerateStaticOrDirectCall(
134 invoke_->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800135 } else {
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000136 codegen->GenerateVirtualCall(invoke_->AsInvokeVirtual(), Location::RegisterLocation(RDI));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800137 }
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000138 codegen->RecordPcInfo(invoke_, invoke_->GetDexPc(), this);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800139
140 // Copy the result back to the expected output.
141 Location out = invoke_->GetLocations()->Out();
142 if (out.IsValid()) {
143 DCHECK(out.IsRegister()); // TODO: Replace this when we support output in memory.
144 DCHECK(!invoke_->GetLocations()->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
145 MoveFromReturnRegister(out, invoke_->GetType(), codegen);
146 }
147
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000148 RestoreLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800149 __ jmp(GetExitLabel());
150 }
151
Alexandre Rames9931f312015-06-19 14:47:01 +0100152 const char* GetDescription() const OVERRIDE { return "IntrinsicSlowPathX86_64"; }
153
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800154 private:
155 // The instruction where this slow path is happening.
156 HInvoke* const invoke_;
157
158 DISALLOW_COPY_AND_ASSIGN(IntrinsicSlowPathX86_64);
159};
160
161#undef __
162#define __ assembler->
163
164static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
165 LocationSummary* locations = new (arena) LocationSummary(invoke,
166 LocationSummary::kNoCall,
167 kIntrinsified);
168 locations->SetInAt(0, Location::RequiresFpuRegister());
169 locations->SetOut(Location::RequiresRegister());
170}
171
172static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
173 LocationSummary* locations = new (arena) LocationSummary(invoke,
174 LocationSummary::kNoCall,
175 kIntrinsified);
176 locations->SetInAt(0, Location::RequiresRegister());
177 locations->SetOut(Location::RequiresFpuRegister());
178}
179
180static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
181 Location input = locations->InAt(0);
182 Location output = locations->Out();
183 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
184}
185
186static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
187 Location input = locations->InAt(0);
188 Location output = locations->Out();
189 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
190}
191
192void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
193 CreateFPToIntLocations(arena_, invoke);
194}
195void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
196 CreateIntToFPLocations(arena_, invoke);
197}
198
199void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
200 MoveFPToInt(invoke->GetLocations(), true, GetAssembler());
201}
202void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
203 MoveIntToFP(invoke->GetLocations(), true, GetAssembler());
204}
205
206void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
207 CreateFPToIntLocations(arena_, invoke);
208}
209void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
210 CreateIntToFPLocations(arena_, invoke);
211}
212
213void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
214 MoveFPToInt(invoke->GetLocations(), false, GetAssembler());
215}
216void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
217 MoveIntToFP(invoke->GetLocations(), false, GetAssembler());
218}
219
220static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
221 LocationSummary* locations = new (arena) LocationSummary(invoke,
222 LocationSummary::kNoCall,
223 kIntrinsified);
224 locations->SetInAt(0, Location::RequiresRegister());
225 locations->SetOut(Location::SameAsFirstInput());
226}
227
228static void GenReverseBytes(LocationSummary* locations,
229 Primitive::Type size,
230 X86_64Assembler* assembler) {
231 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
232
233 switch (size) {
234 case Primitive::kPrimShort:
235 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
236 __ bswapl(out);
237 __ sarl(out, Immediate(16));
238 break;
239 case Primitive::kPrimInt:
240 __ bswapl(out);
241 break;
242 case Primitive::kPrimLong:
243 __ bswapq(out);
244 break;
245 default:
246 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
247 UNREACHABLE();
248 }
249}
250
251void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
252 CreateIntToIntLocations(arena_, invoke);
253}
254
255void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
256 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
257}
258
259void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
260 CreateIntToIntLocations(arena_, invoke);
261}
262
263void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
264 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
265}
266
267void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
268 CreateIntToIntLocations(arena_, invoke);
269}
270
271void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
272 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
273}
274
275
276// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
277// need is 64b.
278
279static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
280 // TODO: Enable memory operations when the assembler supports them.
281 LocationSummary* locations = new (arena) LocationSummary(invoke,
282 LocationSummary::kNoCall,
283 kIntrinsified);
284 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800285 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400286 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800287}
288
Mark Mendell39dcf552015-04-09 20:42:42 -0400289static void MathAbsFP(LocationSummary* locations,
290 bool is64bit,
291 X86_64Assembler* assembler,
292 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800293 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800294
Mark Mendellcfa410b2015-05-25 16:02:44 -0400295 DCHECK(output.IsFpuRegister());
296 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800297
Mark Mendellcfa410b2015-05-25 16:02:44 -0400298 // TODO: Can mask directly with constant area using pand if we can guarantee
299 // that the literal is aligned on a 16 byte boundary. This will avoid a
300 // temporary.
301 if (is64bit) {
302 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
303 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800304 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400305 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
306 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800307 }
308}
309
310void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
311 CreateFloatToFloatPlusTemps(arena_, invoke);
312}
313
314void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400315 MathAbsFP(invoke->GetLocations(), true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800316}
317
318void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
319 CreateFloatToFloatPlusTemps(arena_, invoke);
320}
321
322void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400323 MathAbsFP(invoke->GetLocations(), false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800324}
325
326static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
327 LocationSummary* locations = new (arena) LocationSummary(invoke,
328 LocationSummary::kNoCall,
329 kIntrinsified);
330 locations->SetInAt(0, Location::RequiresRegister());
331 locations->SetOut(Location::SameAsFirstInput());
332 locations->AddTemp(Location::RequiresRegister());
333}
334
335static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
336 Location output = locations->Out();
337 CpuRegister out = output.AsRegister<CpuRegister>();
338 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
339
340 if (is64bit) {
341 // Create mask.
342 __ movq(mask, out);
343 __ sarq(mask, Immediate(63));
344 // Add mask.
345 __ addq(out, mask);
346 __ xorq(out, mask);
347 } else {
348 // Create mask.
349 __ movl(mask, out);
350 __ sarl(mask, Immediate(31));
351 // Add mask.
352 __ addl(out, mask);
353 __ xorl(out, mask);
354 }
355}
356
357void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
358 CreateIntToIntPlusTemp(arena_, invoke);
359}
360
361void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
362 GenAbsInteger(invoke->GetLocations(), false, GetAssembler());
363}
364
365void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
366 CreateIntToIntPlusTemp(arena_, invoke);
367}
368
369void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
370 GenAbsInteger(invoke->GetLocations(), true, GetAssembler());
371}
372
Mark Mendell39dcf552015-04-09 20:42:42 -0400373static void GenMinMaxFP(LocationSummary* locations,
374 bool is_min,
375 bool is_double,
376 X86_64Assembler* assembler,
377 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800378 Location op1_loc = locations->InAt(0);
379 Location op2_loc = locations->InAt(1);
380 Location out_loc = locations->Out();
381 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
382
383 // Shortcut for same input locations.
384 if (op1_loc.Equals(op2_loc)) {
385 DCHECK(out_loc.Equals(op1_loc));
386 return;
387 }
388
389 // (out := op1)
390 // out <=? op2
391 // if Nan jmp Nan_label
392 // if out is min jmp done
393 // if op2 is min jmp op2_label
394 // handle -0/+0
395 // jmp done
396 // Nan_label:
397 // out := NaN
398 // op2_label:
399 // out := op2
400 // done:
401 //
402 // This removes one jmp, but needs to copy one input (op1) to out.
403 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400404 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800405
406 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
407
408 Label nan, done, op2_label;
409 if (is_double) {
410 __ ucomisd(out, op2);
411 } else {
412 __ ucomiss(out, op2);
413 }
414
415 __ j(Condition::kParityEven, &nan);
416
417 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
418 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
419
420 // Handle 0.0/-0.0.
421 if (is_min) {
422 if (is_double) {
423 __ orpd(out, op2);
424 } else {
425 __ orps(out, op2);
426 }
427 } else {
428 if (is_double) {
429 __ andpd(out, op2);
430 } else {
431 __ andps(out, op2);
432 }
433 }
434 __ jmp(&done);
435
436 // NaN handling.
437 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800438 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400439 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800440 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400441 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800442 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800443 __ jmp(&done);
444
445 // out := op2;
446 __ Bind(&op2_label);
447 if (is_double) {
448 __ movsd(out, op2);
449 } else {
450 __ movss(out, op2);
451 }
452
453 // Done.
454 __ Bind(&done);
455}
456
Mark Mendellf55c3e02015-03-26 21:07:46 -0400457static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800458 LocationSummary* locations = new (arena) LocationSummary(invoke,
459 LocationSummary::kNoCall,
460 kIntrinsified);
461 locations->SetInAt(0, Location::RequiresFpuRegister());
462 locations->SetInAt(1, Location::RequiresFpuRegister());
463 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
464 // the second input to be the output (we can simply swap inputs).
465 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800466}
467
468void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400469 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800470}
471
472void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400473 GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800474}
475
476void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400477 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800478}
479
480void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400481 GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800482}
483
484void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400485 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800486}
487
488void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400489 GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800490}
491
492void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400493 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800494}
495
496void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400497 GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800498}
499
500static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
501 X86_64Assembler* assembler) {
502 Location op1_loc = locations->InAt(0);
503 Location op2_loc = locations->InAt(1);
504
505 // Shortcut for same input locations.
506 if (op1_loc.Equals(op2_loc)) {
507 // Can return immediately, as op1_loc == out_loc.
508 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
509 // a copy here.
510 DCHECK(locations->Out().Equals(op1_loc));
511 return;
512 }
513
514 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
515 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
516
517 // (out := op1)
518 // out <=? op2
519 // if out is min jmp done
520 // out := op2
521 // done:
522
523 if (is_long) {
524 __ cmpq(out, op2);
525 } else {
526 __ cmpl(out, op2);
527 }
528
529 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
530}
531
532static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
533 LocationSummary* locations = new (arena) LocationSummary(invoke,
534 LocationSummary::kNoCall,
535 kIntrinsified);
536 locations->SetInAt(0, Location::RequiresRegister());
537 locations->SetInAt(1, Location::RequiresRegister());
538 locations->SetOut(Location::SameAsFirstInput());
539}
540
541void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
542 CreateIntIntToIntLocations(arena_, invoke);
543}
544
545void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
546 GenMinMax(invoke->GetLocations(), true, false, GetAssembler());
547}
548
549void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
550 CreateIntIntToIntLocations(arena_, invoke);
551}
552
553void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
554 GenMinMax(invoke->GetLocations(), true, true, GetAssembler());
555}
556
557void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
558 CreateIntIntToIntLocations(arena_, invoke);
559}
560
561void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
562 GenMinMax(invoke->GetLocations(), false, false, GetAssembler());
563}
564
565void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
566 CreateIntIntToIntLocations(arena_, invoke);
567}
568
569void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
570 GenMinMax(invoke->GetLocations(), false, true, GetAssembler());
571}
572
573static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
574 LocationSummary* locations = new (arena) LocationSummary(invoke,
575 LocationSummary::kNoCall,
576 kIntrinsified);
577 locations->SetInAt(0, Location::RequiresFpuRegister());
578 locations->SetOut(Location::RequiresFpuRegister());
579}
580
581void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
582 CreateFPToFPLocations(arena_, invoke);
583}
584
585void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
586 LocationSummary* locations = invoke->GetLocations();
587 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
588 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
589
590 GetAssembler()->sqrtsd(out, in);
591}
592
Mark Mendellfb8d2792015-03-31 22:16:59 -0400593static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100594 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400595
596 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100597 codegen->GenerateStaticOrDirectCall(
598 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400599 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
600
601 // Copy the result back to the expected output.
602 Location out = invoke->GetLocations()->Out();
603 if (out.IsValid()) {
604 DCHECK(out.IsRegister());
605 MoveFromReturnRegister(out, invoke->GetType(), codegen);
606 }
607}
608
609static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
610 HInvoke* invoke,
611 CodeGeneratorX86_64* codegen) {
612 // Do we have instruction support?
613 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
614 CreateFPToFPLocations(arena, invoke);
615 return;
616 }
617
618 // We have to fall back to a call to the intrinsic.
619 LocationSummary* locations = new (arena) LocationSummary(invoke,
620 LocationSummary::kCall);
621 InvokeRuntimeCallingConvention calling_convention;
622 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
623 locations->SetOut(Location::FpuRegisterLocation(XMM0));
624 // Needs to be RDI for the invoke.
625 locations->AddTemp(Location::RegisterLocation(RDI));
626}
627
628static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
629 HInvoke* invoke,
630 X86_64Assembler* assembler,
631 int round_mode) {
632 LocationSummary* locations = invoke->GetLocations();
633 if (locations->WillCall()) {
634 InvokeOutOfLineIntrinsic(codegen, invoke);
635 } else {
636 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
637 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
638 __ roundsd(out, in, Immediate(round_mode));
639 }
640}
641
642void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
643 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
644}
645
646void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
647 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
648}
649
650void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
651 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
652}
653
654void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
655 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
656}
657
658void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
659 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
660}
661
662void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
663 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
664}
665
666static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
667 HInvoke* invoke,
668 CodeGeneratorX86_64* codegen) {
669 // Do we have instruction support?
670 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
671 LocationSummary* locations = new (arena) LocationSummary(invoke,
672 LocationSummary::kNoCall,
673 kIntrinsified);
674 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600675 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400676 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400677 return;
678 }
679
680 // We have to fall back to a call to the intrinsic.
681 LocationSummary* locations = new (arena) LocationSummary(invoke,
682 LocationSummary::kCall);
683 InvokeRuntimeCallingConvention calling_convention;
684 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
685 locations->SetOut(Location::RegisterLocation(RAX));
686 // Needs to be RDI for the invoke.
687 locations->AddTemp(Location::RegisterLocation(RDI));
688}
689
690void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
691 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
692}
693
694void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
695 LocationSummary* locations = invoke->GetLocations();
696 if (locations->WillCall()) {
697 InvokeOutOfLineIntrinsic(codegen_, invoke);
698 return;
699 }
700
701 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
702 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
703 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400704 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400705 Label done, nan;
706 X86_64Assembler* assembler = GetAssembler();
707
Mark Mendell40741f32015-04-20 22:10:34 -0400708 // Load 0.5 into inPlusPointFive.
709 __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400710
711 // Add in the input.
712 __ addss(inPlusPointFive, in);
713
714 // And truncate to an integer.
715 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
716
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600717 // Load maxInt into out.
718 codegen_->Load64BitValue(out, kPrimIntMax);
719
Mark Mendellfb8d2792015-03-31 22:16:59 -0400720 // if inPlusPointFive >= maxInt goto done
Mark Mendellcfa410b2015-05-25 16:02:44 -0400721 __ movl(out, Immediate(kPrimIntMax));
Mark Mendell40741f32015-04-20 22:10:34 -0400722 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400723 __ j(kAboveEqual, &done);
724
725 // if input == NaN goto nan
726 __ j(kUnordered, &nan);
727
728 // output = float-to-int-truncate(input)
729 __ cvttss2si(out, inPlusPointFive);
730 __ jmp(&done);
731 __ Bind(&nan);
732
733 // output = 0
734 __ xorl(out, out);
735 __ Bind(&done);
736}
737
738void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
739 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
740}
741
742void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
743 LocationSummary* locations = invoke->GetLocations();
744 if (locations->WillCall()) {
745 InvokeOutOfLineIntrinsic(codegen_, invoke);
746 return;
747 }
748
749 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
750 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
751 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400752 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400753 Label done, nan;
754 X86_64Assembler* assembler = GetAssembler();
755
Mark Mendell40741f32015-04-20 22:10:34 -0400756 // Load 0.5 into inPlusPointFive.
757 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400758
759 // Add in the input.
760 __ addsd(inPlusPointFive, in);
761
762 // And truncate to an integer.
763 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
764
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600765 // Load maxLong into out.
766 codegen_->Load64BitValue(out, kPrimLongMax);
767
Mark Mendellfb8d2792015-03-31 22:16:59 -0400768 // if inPlusPointFive >= maxLong goto done
Mark Mendellcfa410b2015-05-25 16:02:44 -0400769 __ movq(out, Immediate(kPrimLongMax));
Mark Mendell40741f32015-04-20 22:10:34 -0400770 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400771 __ j(kAboveEqual, &done);
772
773 // if input == NaN goto nan
774 __ j(kUnordered, &nan);
775
776 // output = double-to-long-truncate(input)
777 __ cvttsd2si(out, inPlusPointFive, true);
778 __ jmp(&done);
779 __ Bind(&nan);
780
781 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400782 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400783 __ Bind(&done);
784}
785
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800786void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) {
787 // The inputs plus one temp.
788 LocationSummary* locations = new (arena_) LocationSummary(invoke,
789 LocationSummary::kCallOnSlowPath,
790 kIntrinsified);
791 locations->SetInAt(0, Location::RequiresRegister());
792 locations->SetInAt(1, Location::RequiresRegister());
793 locations->SetOut(Location::SameAsFirstInput());
794 locations->AddTemp(Location::RequiresRegister());
795}
796
797void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) {
798 LocationSummary* locations = invoke->GetLocations();
799
800 // Location of reference to data array
801 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
802 // Location of count
803 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800804
805 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
806 CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>();
807 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800808
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800809 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
810 // the cost.
811 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
812 // we will not optimize the code for constants (which would save a register).
813
Andreas Gampe878d58c2015-01-15 23:24:00 -0800814 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800815 codegen_->AddSlowPath(slow_path);
816
817 X86_64Assembler* assembler = GetAssembler();
818
819 __ cmpl(idx, Address(obj, count_offset));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800820 codegen_->MaybeRecordImplicitNullCheck(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800821 __ j(kAboveEqual, slow_path->GetEntryLabel());
822
Jeff Hao848f70a2014-01-15 13:49:50 -0800823 // out = out[2*idx].
824 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800825
826 __ Bind(slow_path->GetExitLabel());
827}
828
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000829void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
830 LocationSummary* locations = new (arena_) LocationSummary(invoke,
831 LocationSummary::kCall,
832 kIntrinsified);
833 InvokeRuntimeCallingConvention calling_convention;
834 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
835 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
836 locations->SetOut(Location::RegisterLocation(RAX));
837}
838
839void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
840 X86_64Assembler* assembler = GetAssembler();
841 LocationSummary* locations = invoke->GetLocations();
842
Nicolas Geoffray512e04d2015-03-27 17:21:24 +0000843 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +0100844 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000845
846 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
847 __ testl(argument, argument);
848 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
849 codegen_->AddSlowPath(slow_path);
850 __ j(kEqual, slow_path->GetEntryLabel());
851
852 __ gs()->call(Address::Absolute(
853 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true));
854 __ Bind(slow_path->GetExitLabel());
855}
856
Agi Csakif8cfb202015-08-13 17:54:54 -0700857void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
858 LocationSummary* locations = new (arena_) LocationSummary(invoke,
859 LocationSummary::kNoCall,
860 kIntrinsified);
861 locations->SetInAt(0, Location::RequiresRegister());
862 locations->SetInAt(1, Location::RequiresRegister());
863
864 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
865 locations->AddTemp(Location::RegisterLocation(RCX));
866 locations->AddTemp(Location::RegisterLocation(RDI));
867
868 // Set output, RSI needed for repe_cmpsq instruction anyways.
869 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
870}
871
872void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
873 X86_64Assembler* assembler = GetAssembler();
874 LocationSummary* locations = invoke->GetLocations();
875
876 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
877 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
878 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
879 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
880 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
881
882 Label end;
883 Label return_true;
884 Label return_false;
885
886 // Get offsets of count, value, and class fields within a string object.
887 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
888 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
889 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
890
891 // Note that the null check must have been done earlier.
892 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
893
894 // Check if input is null, return false if it is.
895 __ testl(arg, arg);
896 __ j(kEqual, &return_false);
897
898 // Instanceof check for the argument by comparing class fields.
899 // All string objects must have the same type since String cannot be subclassed.
900 // Receiver must be a string object, so its class field is equal to all strings' class fields.
901 // If the argument is a string object, its class field must be equal to receiver's class field.
902 __ movl(rcx, Address(str, class_offset));
903 __ cmpl(rcx, Address(arg, class_offset));
904 __ j(kNotEqual, &return_false);
905
906 // Reference equality check, return true if same reference.
907 __ cmpl(str, arg);
908 __ j(kEqual, &return_true);
909
910 // Load length of receiver string.
911 __ movl(rcx, Address(str, count_offset));
912 // Check if lengths are equal, return false if they're not.
913 __ cmpl(rcx, Address(arg, count_offset));
914 __ j(kNotEqual, &return_false);
915 // Return true if both strings are empty.
916 __ testl(rcx, rcx);
917 __ j(kEqual, &return_true);
918
919 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
920 __ leal(rsi, Address(str, value_offset));
921 __ leal(rdi, Address(arg, value_offset));
922
923 // Divide string length by 4 and adjust for lengths not divisible by 4.
924 __ addl(rcx, Immediate(3));
925 __ shrl(rcx, Immediate(2));
926
927 // Assertions that must hold in order to compare strings 4 characters at a time.
928 DCHECK_ALIGNED(value_offset, 8);
929 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
930
931 // Loop to compare strings four characters at a time starting at the beginning of the string.
932 __ repe_cmpsq();
933 // If strings are not equal, zero flag will be cleared.
934 __ j(kNotEqual, &return_false);
935
936 // Return true and exit the function.
937 // If loop does not result in returning false, we return true.
938 __ Bind(&return_true);
939 __ movl(rsi, Immediate(1));
940 __ jmp(&end);
941
942 // Return false and exit the function.
943 __ Bind(&return_false);
944 __ xorl(rsi, rsi);
945 __ Bind(&end);
946}
947
Andreas Gampe21030dd2015-05-07 14:46:15 -0700948static void CreateStringIndexOfLocations(HInvoke* invoke,
949 ArenaAllocator* allocator,
950 bool start_at_zero) {
951 LocationSummary* locations = new (allocator) LocationSummary(invoke,
952 LocationSummary::kCallOnSlowPath,
953 kIntrinsified);
954 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
955 locations->SetInAt(0, Location::RegisterLocation(RDI));
956 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
957 // allocator to do that, anyways. We can still do the constant check by checking the parameter
958 // of the instruction explicitly.
959 // Note: This works as we don't clobber RAX anywhere.
960 locations->SetInAt(1, Location::RegisterLocation(RAX));
961 if (!start_at_zero) {
962 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
963 }
964 // As we clobber RDI during execution anyways, also use it as the output.
965 locations->SetOut(Location::SameAsFirstInput());
966
967 // repne scasw uses RCX as the counter.
968 locations->AddTemp(Location::RegisterLocation(RCX));
969 // Need another temporary to be able to compute the result.
970 locations->AddTemp(Location::RequiresRegister());
971}
972
973static void GenerateStringIndexOf(HInvoke* invoke,
974 X86_64Assembler* assembler,
975 CodeGeneratorX86_64* codegen,
976 ArenaAllocator* allocator,
977 bool start_at_zero) {
978 LocationSummary* locations = invoke->GetLocations();
979
980 // Note that the null check must have been done earlier.
981 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
982
983 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
984 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
985 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
986 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
987 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
988
989 // Check our assumptions for registers.
990 DCHECK_EQ(string_obj.AsRegister(), RDI);
991 DCHECK_EQ(search_value.AsRegister(), RAX);
992 DCHECK_EQ(counter.AsRegister(), RCX);
993 DCHECK_EQ(out.AsRegister(), RDI);
994
995 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
996 // or directly dispatch if we have a constant.
997 SlowPathCodeX86_64* slow_path = nullptr;
998 if (invoke->InputAt(1)->IsIntConstant()) {
999 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1000 std::numeric_limits<uint16_t>::max()) {
1001 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1002 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1003 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1004 codegen->AddSlowPath(slow_path);
1005 __ jmp(slow_path->GetEntryLabel());
1006 __ Bind(slow_path->GetExitLabel());
1007 return;
1008 }
1009 } else {
1010 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1011 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1012 codegen->AddSlowPath(slow_path);
1013 __ j(kAbove, slow_path->GetEntryLabel());
1014 }
1015
1016 // From here down, we know that we are looking for a char that fits in 16 bits.
1017 // Location of reference to data array within the String object.
1018 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1019 // Location of count within the String object.
1020 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1021
1022 // Load string length, i.e., the count field of the string.
1023 __ movl(string_length, Address(string_obj, count_offset));
1024
1025 // Do a length check.
1026 // TODO: Support jecxz.
1027 Label not_found_label;
1028 __ testl(string_length, string_length);
1029 __ j(kEqual, &not_found_label);
1030
1031 if (start_at_zero) {
1032 // Number of chars to scan is the same as the string length.
1033 __ movl(counter, string_length);
1034
1035 // Move to the start of the string.
1036 __ addq(string_obj, Immediate(value_offset));
1037 } else {
1038 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1039
1040 // Do a start_index check.
1041 __ cmpl(start_index, string_length);
1042 __ j(kGreaterEqual, &not_found_label);
1043
1044 // Ensure we have a start index >= 0;
1045 __ xorl(counter, counter);
1046 __ cmpl(start_index, Immediate(0));
1047 __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough.
1048
1049 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1050 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1051
1052 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1053 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1054 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1055 }
1056
1057 // Everything is set up for repne scasw:
1058 // * Comparison address in RDI.
1059 // * Counter in ECX.
1060 __ repne_scasw();
1061
1062 // Did we find a match?
1063 __ j(kNotEqual, &not_found_label);
1064
1065 // Yes, we matched. Compute the index of the result.
1066 __ subl(string_length, counter);
1067 __ leal(out, Address(string_length, -1));
1068
1069 Label done;
1070 __ jmp(&done);
1071
1072 // Failed to match; return -1.
1073 __ Bind(&not_found_label);
1074 __ movl(out, Immediate(-1));
1075
1076 // And join up at the end.
1077 __ Bind(&done);
1078 if (slow_path != nullptr) {
1079 __ Bind(slow_path->GetExitLabel());
1080 }
1081}
1082
1083void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
1084 CreateStringIndexOfLocations(invoke, arena_, true);
1085}
1086
1087void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
1088 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1089}
1090
1091void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1092 CreateStringIndexOfLocations(invoke, arena_, false);
1093}
1094
1095void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1096 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1097}
1098
Jeff Hao848f70a2014-01-15 13:49:50 -08001099void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1100 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1101 LocationSummary::kCall,
1102 kIntrinsified);
1103 InvokeRuntimeCallingConvention calling_convention;
1104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1105 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1106 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1107 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1108 locations->SetOut(Location::RegisterLocation(RAX));
1109}
1110
1111void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1112 X86_64Assembler* assembler = GetAssembler();
1113 LocationSummary* locations = invoke->GetLocations();
1114
1115 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1116 __ testl(byte_array, byte_array);
1117 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1118 codegen_->AddSlowPath(slow_path);
1119 __ j(kEqual, slow_path->GetEntryLabel());
1120
1121 __ gs()->call(Address::Absolute(
1122 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true));
1123 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1124 __ Bind(slow_path->GetExitLabel());
1125}
1126
1127void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1128 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1129 LocationSummary::kCall,
1130 kIntrinsified);
1131 InvokeRuntimeCallingConvention calling_convention;
1132 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1133 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1134 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1135 locations->SetOut(Location::RegisterLocation(RAX));
1136}
1137
1138void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1139 X86_64Assembler* assembler = GetAssembler();
1140
1141 __ gs()->call(Address::Absolute(
1142 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true));
1143 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1144}
1145
1146void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1147 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1148 LocationSummary::kCall,
1149 kIntrinsified);
1150 InvokeRuntimeCallingConvention calling_convention;
1151 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1152 locations->SetOut(Location::RegisterLocation(RAX));
1153}
1154
1155void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1156 X86_64Assembler* assembler = GetAssembler();
1157 LocationSummary* locations = invoke->GetLocations();
1158
1159 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1160 __ testl(string_to_copy, string_to_copy);
1161 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1162 codegen_->AddSlowPath(slow_path);
1163 __ j(kEqual, slow_path->GetEntryLabel());
1164
1165 __ gs()->call(Address::Absolute(
1166 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true));
1167 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1168 __ Bind(slow_path->GetExitLabel());
1169}
1170
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001171static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1172 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1173 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1174 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1175 // to avoid a SIGBUS.
1176 switch (size) {
1177 case Primitive::kPrimByte:
1178 __ movsxb(out, Address(address, 0));
1179 break;
1180 case Primitive::kPrimShort:
1181 __ movsxw(out, Address(address, 0));
1182 break;
1183 case Primitive::kPrimInt:
1184 __ movl(out, Address(address, 0));
1185 break;
1186 case Primitive::kPrimLong:
1187 __ movq(out, Address(address, 0));
1188 break;
1189 default:
1190 LOG(FATAL) << "Type not recognized for peek: " << size;
1191 UNREACHABLE();
1192 }
1193}
1194
1195void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1196 CreateIntToIntLocations(arena_, invoke);
1197}
1198
1199void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1200 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1201}
1202
1203void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1204 CreateIntToIntLocations(arena_, invoke);
1205}
1206
1207void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1208 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1209}
1210
1211void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1212 CreateIntToIntLocations(arena_, invoke);
1213}
1214
1215void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1216 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1217}
1218
1219void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1220 CreateIntToIntLocations(arena_, invoke);
1221}
1222
1223void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1224 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1225}
1226
1227static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1228 LocationSummary* locations = new (arena) LocationSummary(invoke,
1229 LocationSummary::kNoCall,
1230 kIntrinsified);
1231 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001232 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001233}
1234
1235static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1236 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001237 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001238 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1239 // to avoid a SIGBUS.
1240 switch (size) {
1241 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001242 if (value.IsConstant()) {
1243 __ movb(Address(address, 0),
1244 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1245 } else {
1246 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1247 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001248 break;
1249 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001250 if (value.IsConstant()) {
1251 __ movw(Address(address, 0),
1252 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1253 } else {
1254 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1255 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001256 break;
1257 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001258 if (value.IsConstant()) {
1259 __ movl(Address(address, 0),
1260 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1261 } else {
1262 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1263 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001264 break;
1265 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001266 if (value.IsConstant()) {
1267 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1268 DCHECK(IsInt<32>(v));
1269 int32_t v_32 = v;
1270 __ movq(Address(address, 0), Immediate(v_32));
1271 } else {
1272 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1273 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001274 break;
1275 default:
1276 LOG(FATAL) << "Type not recognized for poke: " << size;
1277 UNREACHABLE();
1278 }
1279}
1280
1281void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1282 CreateIntIntToVoidLocations(arena_, invoke);
1283}
1284
1285void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1286 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1287}
1288
1289void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1290 CreateIntIntToVoidLocations(arena_, invoke);
1291}
1292
1293void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1294 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1295}
1296
1297void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1298 CreateIntIntToVoidLocations(arena_, invoke);
1299}
1300
1301void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1302 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1303}
1304
1305void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1306 CreateIntIntToVoidLocations(arena_, invoke);
1307}
1308
1309void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1310 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1311}
1312
1313void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1314 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1315 LocationSummary::kNoCall,
1316 kIntrinsified);
1317 locations->SetOut(Location::RequiresRegister());
1318}
1319
1320void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1321 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
1322 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true));
1323}
1324
Andreas Gampe878d58c2015-01-15 23:24:00 -08001325static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001326 bool is_volatile ATTRIBUTE_UNUSED, X86_64Assembler* assembler) {
1327 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1328 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1329 CpuRegister trg = locations->Out().AsRegister<CpuRegister>();
1330
Andreas Gampe878d58c2015-01-15 23:24:00 -08001331 switch (type) {
1332 case Primitive::kPrimInt:
1333 case Primitive::kPrimNot:
1334 __ movl(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001335 if (type == Primitive::kPrimNot) {
1336 __ MaybeUnpoisonHeapReference(trg);
1337 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001338 break;
1339
1340 case Primitive::kPrimLong:
1341 __ movq(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1342 break;
1343
1344 default:
1345 LOG(FATAL) << "Unsupported op size " << type;
1346 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001347 }
1348}
1349
1350static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
1351 LocationSummary* locations = new (arena) LocationSummary(invoke,
1352 LocationSummary::kNoCall,
1353 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001354 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001355 locations->SetInAt(1, Location::RequiresRegister());
1356 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001357 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001358}
1359
1360void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1361 CreateIntIntIntToIntLocations(arena_, invoke);
1362}
1363void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1364 CreateIntIntIntToIntLocations(arena_, invoke);
1365}
1366void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1367 CreateIntIntIntToIntLocations(arena_, invoke);
1368}
1369void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1370 CreateIntIntIntToIntLocations(arena_, invoke);
1371}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001372void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1373 CreateIntIntIntToIntLocations(arena_, invoke);
1374}
1375void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1376 CreateIntIntIntToIntLocations(arena_, invoke);
1377}
1378
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001379
1380void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001381 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001382}
1383void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001384 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001385}
1386void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001387 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001388}
1389void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001390 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001391}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001392void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1393 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1394}
1395void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1396 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1397}
1398
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001399
1400static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1401 Primitive::Type type,
1402 HInvoke* invoke) {
1403 LocationSummary* locations = new (arena) LocationSummary(invoke,
1404 LocationSummary::kNoCall,
1405 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001406 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001407 locations->SetInAt(1, Location::RequiresRegister());
1408 locations->SetInAt(2, Location::RequiresRegister());
1409 locations->SetInAt(3, Location::RequiresRegister());
1410 if (type == Primitive::kPrimNot) {
1411 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001412 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001413 locations->AddTemp(Location::RequiresRegister());
1414 }
1415}
1416
1417void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1418 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1419}
1420void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1421 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1422}
1423void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1424 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1425}
1426void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1427 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1428}
1429void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1430 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1431}
1432void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1433 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1434}
1435void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1436 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1437}
1438void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1439 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1440}
1441void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1442 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1443}
1444
1445// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1446// memory model.
1447static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1448 CodeGeneratorX86_64* codegen) {
1449 X86_64Assembler* assembler = reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1450 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1451 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1452 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1453
1454 if (type == Primitive::kPrimLong) {
1455 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001456 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1457 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1458 __ movl(temp, value);
1459 __ PoisonHeapReference(temp);
1460 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001461 } else {
1462 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1463 }
1464
1465 if (is_volatile) {
1466 __ mfence();
1467 }
1468
1469 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001470 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001471 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1472 locations->GetTemp(1).AsRegister<CpuRegister>(),
1473 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001474 value,
1475 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001476 }
1477}
1478
1479void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
1480 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1481}
1482void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1483 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1484}
1485void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1486 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1487}
1488void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1489 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1490}
1491void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1492 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1493}
1494void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1495 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1496}
1497void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1498 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1499}
1500void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1501 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1502}
1503void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1504 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1505}
1506
Mark Mendell58d25fd2015-04-03 14:52:31 -04001507static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1508 HInvoke* invoke) {
1509 LocationSummary* locations = new (arena) LocationSummary(invoke,
1510 LocationSummary::kNoCall,
1511 kIntrinsified);
1512 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1513 locations->SetInAt(1, Location::RequiresRegister());
1514 locations->SetInAt(2, Location::RequiresRegister());
1515 // expected value must be in EAX/RAX.
1516 locations->SetInAt(3, Location::RegisterLocation(RAX));
1517 locations->SetInAt(4, Location::RequiresRegister());
1518
1519 locations->SetOut(Location::RequiresRegister());
1520 if (type == Primitive::kPrimNot) {
1521 // Need temp registers for card-marking.
1522 locations->AddTemp(Location::RequiresRegister());
1523 locations->AddTemp(Location::RequiresRegister());
1524 }
1525}
1526
1527void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1528 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1529}
1530
1531void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1532 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1533}
1534
1535void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1536 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1537}
1538
1539static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1540 X86_64Assembler* assembler =
1541 reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1542 LocationSummary* locations = invoke->GetLocations();
1543
1544 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1545 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1546 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
1547 DCHECK_EQ(expected.AsRegister(), RAX);
1548 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1549 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1550
1551 if (type == Primitive::kPrimLong) {
1552 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
1553 } else {
1554 // Integer or object.
1555 if (type == Primitive::kPrimNot) {
1556 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001557 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001558 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1559 locations->GetTemp(1).AsRegister<CpuRegister>(),
1560 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001561 value,
1562 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001563
1564 if (kPoisonHeapReferences) {
1565 __ PoisonHeapReference(expected);
1566 __ PoisonHeapReference(value);
1567 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001568 }
1569
1570 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1571 }
1572
1573 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1574 // barriers at this time.
1575
1576 // Convert ZF into the boolean result.
1577 __ setcc(kZero, out);
1578 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01001579
1580 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1581 __ UnpoisonHeapReference(value);
1582 __ UnpoisonHeapReference(expected);
1583 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001584}
1585
1586void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1587 GenCAS(Primitive::kPrimInt, invoke, codegen_);
1588}
1589
1590void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1591 GenCAS(Primitive::kPrimLong, invoke, codegen_);
1592}
1593
1594void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1595 GenCAS(Primitive::kPrimNot, invoke, codegen_);
1596}
1597
1598void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
1599 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1600 LocationSummary::kNoCall,
1601 kIntrinsified);
1602 locations->SetInAt(0, Location::RequiresRegister());
1603 locations->SetOut(Location::SameAsFirstInput());
1604 locations->AddTemp(Location::RequiresRegister());
1605}
1606
1607static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
1608 X86_64Assembler* assembler) {
1609 Immediate imm_shift(shift);
1610 Immediate imm_mask(mask);
1611 __ movl(temp, reg);
1612 __ shrl(reg, imm_shift);
1613 __ andl(temp, imm_mask);
1614 __ andl(reg, imm_mask);
1615 __ shll(temp, imm_shift);
1616 __ orl(reg, temp);
1617}
1618
1619void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
1620 X86_64Assembler* assembler =
1621 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
1622 LocationSummary* locations = invoke->GetLocations();
1623
1624 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
1625 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1626
1627 /*
1628 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1629 * swapping bits to reverse bits in a number x. Using bswap to save instructions
1630 * compared to generic luni implementation which has 5 rounds of swapping bits.
1631 * x = bswap x
1632 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
1633 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
1634 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
1635 */
1636 __ bswapl(reg);
1637 SwapBits(reg, temp, 1, 0x55555555, assembler);
1638 SwapBits(reg, temp, 2, 0x33333333, assembler);
1639 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
1640}
1641
1642void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
1643 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1644 LocationSummary::kNoCall,
1645 kIntrinsified);
1646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetOut(Location::SameAsFirstInput());
1648 locations->AddTemp(Location::RequiresRegister());
1649 locations->AddTemp(Location::RequiresRegister());
1650}
1651
1652static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
1653 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
1654 Immediate imm_shift(shift);
1655 __ movq(temp_mask, Immediate(mask));
1656 __ movq(temp, reg);
1657 __ shrq(reg, imm_shift);
1658 __ andq(temp, temp_mask);
1659 __ andq(reg, temp_mask);
1660 __ shlq(temp, imm_shift);
1661 __ orq(reg, temp);
1662}
1663
1664void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
1665 X86_64Assembler* assembler =
1666 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
1667 LocationSummary* locations = invoke->GetLocations();
1668
1669 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
1670 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
1671 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
1672
1673 /*
1674 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1675 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
1676 * compared to generic luni implementation which has 5 rounds of swapping bits.
1677 * x = bswap x
1678 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
1679 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
1680 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
1681 */
1682 __ bswapq(reg);
1683 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
1684 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
1685 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
1686}
1687
Mark Mendelld5897672015-08-12 21:16:41 -04001688static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
1689 LocationSummary* locations = new (arena) LocationSummary(invoke,
1690 LocationSummary::kNoCall,
1691 kIntrinsified);
1692 locations->SetInAt(0, Location::Any());
1693 locations->SetOut(Location::RequiresRegister());
1694}
1695
1696static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
1697 LocationSummary* locations = invoke->GetLocations();
1698 Location src = locations->InAt(0);
1699 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1700
1701 int zero_value_result = is_long ? 64 : 32;
1702 if (invoke->InputAt(0)->IsConstant()) {
1703 // Evaluate this at compile time.
1704 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
1705 if (value == 0) {
1706 value = zero_value_result;
1707 } else {
1708 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
1709 }
1710 if (value == 0) {
1711 __ xorl(out, out);
1712 } else {
1713 __ movl(out, Immediate(value));
1714 }
1715 return;
1716 }
1717
1718 // Handle the non-constant cases.
1719 if (src.IsRegister()) {
1720 if (is_long) {
1721 __ bsrq(out, src.AsRegister<CpuRegister>());
1722 } else {
1723 __ bsrl(out, src.AsRegister<CpuRegister>());
1724 }
1725 } else if (is_long) {
1726 DCHECK(src.IsDoubleStackSlot());
1727 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
1728 } else {
1729 DCHECK(src.IsStackSlot());
1730 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
1731 }
1732
1733 // BSR sets ZF if the input was zero, and the output is undefined.
1734 Label is_zero, done;
1735 __ j(kEqual, &is_zero);
1736
1737 // Correct the result from BSR to get the CLZ result.
1738 __ xorl(out, Immediate(zero_value_result - 1));
1739 __ jmp(&done);
1740
1741 // Fix the zero case with the expected result.
1742 __ Bind(&is_zero);
1743 __ movl(out, Immediate(zero_value_result));
1744
1745 __ Bind(&done);
1746}
1747
1748void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
1749 CreateLeadingZeroLocations(arena_, invoke);
1750}
1751
1752void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
1753 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1754 GenLeadingZeros(assembler, invoke, /* is_long */ false);
1755}
1756
1757void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
1758 CreateLeadingZeroLocations(arena_, invoke);
1759}
1760
1761void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
1762 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1763 GenLeadingZeros(assembler, invoke, /* is_long */ true);
1764}
1765
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001766// Unimplemented intrinsics.
1767
1768#define UNIMPLEMENTED_INTRINSIC(Name) \
1769void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1770} \
1771void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1772}
1773
Jeff Hao848f70a2014-01-15 13:49:50 -08001774UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001775UNIMPLEMENTED_INTRINSIC(SystemArrayCopyChar)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001776UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
1777
Roland Levillain4d027112015-07-01 15:41:14 +01001778#undef UNIMPLEMENTED_INTRINSIC
1779
1780#undef __
1781
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001782} // namespace x86_64
1783} // namespace art