blob: 340a81dcdd7ce8b0b44933926799bdbffc606c28 [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"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080023#include "code_generator_x86_64.h"
24#include "entrypoints/quick/quick_entrypoints.h"
25#include "intrinsics.h"
26#include "mirror/array-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027#include "mirror/string.h"
28#include "thread.h"
29#include "utils/x86_64/assembler_x86_64.h"
30#include "utils/x86_64/constants_x86_64.h"
31
32namespace art {
33
34namespace x86_64 {
35
Mark Mendellfb8d2792015-03-31 22:16:59 -040036IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
37 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
38}
39
40
Andreas Gampe71fb52f2014-12-29 17:43:08 -080041X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
42 return reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
43}
44
Andreas Gampe878d58c2015-01-15 23:24:00 -080045ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080046 return codegen_->GetGraph()->GetArena();
47}
48
49bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
50 Dispatch(invoke);
51 const LocationSummary* res = invoke->GetLocations();
52 return res != nullptr && res->Intrinsified();
53}
54
55#define __ reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler())->
56
57// TODO: trg as memory.
58static void MoveFromReturnRegister(Location trg,
59 Primitive::Type type,
60 CodeGeneratorX86_64* codegen) {
61 if (!trg.IsValid()) {
62 DCHECK(type == Primitive::kPrimVoid);
63 return;
64 }
65
66 switch (type) {
67 case Primitive::kPrimBoolean:
68 case Primitive::kPrimByte:
69 case Primitive::kPrimChar:
70 case Primitive::kPrimShort:
71 case Primitive::kPrimInt:
72 case Primitive::kPrimNot: {
73 CpuRegister trg_reg = trg.AsRegister<CpuRegister>();
74 if (trg_reg.AsRegister() != RAX) {
75 __ movl(trg_reg, CpuRegister(RAX));
76 }
77 break;
78 }
79 case Primitive::kPrimLong: {
80 CpuRegister trg_reg = trg.AsRegister<CpuRegister>();
81 if (trg_reg.AsRegister() != RAX) {
82 __ movq(trg_reg, CpuRegister(RAX));
83 }
84 break;
85 }
86
87 case Primitive::kPrimVoid:
88 LOG(FATAL) << "Unexpected void type for valid location " << trg;
89 UNREACHABLE();
90
91 case Primitive::kPrimDouble: {
92 XmmRegister trg_reg = trg.AsFpuRegister<XmmRegister>();
93 if (trg_reg.AsFloatRegister() != XMM0) {
94 __ movsd(trg_reg, XmmRegister(XMM0));
95 }
96 break;
97 }
98 case Primitive::kPrimFloat: {
99 XmmRegister trg_reg = trg.AsFpuRegister<XmmRegister>();
100 if (trg_reg.AsFloatRegister() != XMM0) {
101 __ movss(trg_reg, XmmRegister(XMM0));
102 }
103 break;
104 }
105 }
106}
107
Roland Levillainec525fc2015-04-28 15:50:20 +0100108static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100109 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +0100110 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800111}
112
113// Slow-path for fallback (calling the managed code to handle the intrinsic) in an intrinsified
114// call. This will copy the arguments into the positions for a regular call.
115//
116// Note: The actual parameters are required to be in the locations given by the invoke's location
117// summary. If an intrinsic modifies those locations before a slowpath call, they must be
118// restored!
119class IntrinsicSlowPathX86_64 : public SlowPathCodeX86_64 {
120 public:
121 explicit IntrinsicSlowPathX86_64(HInvoke* invoke) : invoke_(invoke) { }
122
123 void EmitNativeCode(CodeGenerator* codegen_in) OVERRIDE {
124 CodeGeneratorX86_64* codegen = down_cast<CodeGeneratorX86_64*>(codegen_in);
125 __ Bind(GetEntryLabel());
126
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000127 SaveLiveRegisters(codegen, invoke_->GetLocations());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800128
Roland Levillainec525fc2015-04-28 15:50:20 +0100129 MoveArguments(invoke_, codegen);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800130
131 if (invoke_->IsInvokeStaticOrDirect()) {
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100132 codegen->GenerateStaticOrDirectCall(
133 invoke_->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000134 RecordPcInfo(codegen, invoke_, invoke_->GetDexPc());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800135 } else {
136 UNIMPLEMENTED(FATAL) << "Non-direct intrinsic slow-path not yet implemented";
137 UNREACHABLE();
138 }
139
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
Mark Mendell6bc53a92015-07-01 14:26:52 -0400800 // Location of reference to data array.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800801 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400802 // Location of count.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800803 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
Mark Mendell6bc53a92015-07-01 14:26:52 -0400829void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
830 // Check to see if we have known failures that will cause us to have to bail out
831 // to the runtime, and just generate the runtime call directly.
832 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
833 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
834
835 // The positions must be non-negative.
836 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
837 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
838 // We will have to fail anyways.
839 return;
840 }
841
842 // The length must be > 0.
843 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
844 if (length != nullptr) {
845 int32_t len = length->GetValue();
846 if (len < 0) {
847 // Just call as normal.
848 return;
849 }
850 }
851
852 LocationSummary* locations = new (arena_) LocationSummary(invoke,
853 LocationSummary::kCallOnSlowPath,
854 kIntrinsified);
855 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
856 locations->SetInAt(0, Location::RequiresRegister());
857 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
858 locations->SetInAt(2, Location::RequiresRegister());
859 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
860 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
861
862 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
863 locations->AddTemp(Location::RegisterLocation(RSI));
864 locations->AddTemp(Location::RegisterLocation(RDI));
865 locations->AddTemp(Location::RegisterLocation(RCX));
866}
867
868static void CheckPosition(X86_64Assembler* assembler,
869 Location pos,
870 CpuRegister input,
871 CpuRegister length,
872 SlowPathCodeX86_64* slow_path,
873 CpuRegister input_len,
874 CpuRegister temp) {
875 // Where is the length in the String?
876 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
877
878 if (pos.IsConstant()) {
879 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
880 if (pos_const == 0) {
881 // Check that length(input) >= length.
882 __ cmpl(Address(input, length_offset), length);
883 __ j(kLess, slow_path->GetEntryLabel());
884 } else {
885 // Check that length(input) >= pos.
886 __ movl(input_len, Address(input, length_offset));
887 __ cmpl(input_len, Immediate(pos_const));
888 __ j(kLess, slow_path->GetEntryLabel());
889
890 // Check that (length(input) - pos) >= length.
891 __ leal(temp, Address(input_len, -pos_const));
892 __ cmpl(temp, length);
893 __ j(kLess, slow_path->GetEntryLabel());
894 }
895 } else {
896 // Check that pos >= 0.
897 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
898 __ testl(pos_reg, pos_reg);
899 __ j(kLess, slow_path->GetEntryLabel());
900
901 // Check that pos <= length(input).
902 __ cmpl(Address(input, length_offset), pos_reg);
903 __ j(kLess, slow_path->GetEntryLabel());
904
905 // Check that (length(input) - pos) >= length.
906 __ movl(temp, Address(input, length_offset));
907 __ subl(temp, pos_reg);
908 __ cmpl(temp, length);
909 __ j(kLess, slow_path->GetEntryLabel());
910 }
911}
912
913void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
914 X86_64Assembler* assembler = GetAssembler();
915 LocationSummary* locations = invoke->GetLocations();
916
917 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
918 Location srcPos = locations->InAt(1);
919 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
920 Location destPos = locations->InAt(3);
921 Location length = locations->InAt(4);
922
923 // Temporaries that we need for MOVSW.
924 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
925 DCHECK_EQ(src_base.AsRegister(), RSI);
926 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
927 DCHECK_EQ(dest_base.AsRegister(), RDI);
928 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
929 DCHECK_EQ(count.AsRegister(), RCX);
930
931 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
932 codegen_->AddSlowPath(slow_path);
933
934 // Bail out if the source and destination are the same.
935 __ cmpl(src, dest);
936 __ j(kEqual, slow_path->GetEntryLabel());
937
938 // Bail out if the source is null.
939 __ testl(src, src);
940 __ j(kEqual, slow_path->GetEntryLabel());
941
942 // Bail out if the destination is null.
943 __ testl(dest, dest);
944 __ j(kEqual, slow_path->GetEntryLabel());
945
946 // If the length is negative, bail out.
947 // We have already checked in the LocationsBuilder for the constant case.
948 if (!length.IsConstant()) {
949 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
950 __ j(kLess, slow_path->GetEntryLabel());
951 }
952
953 // We need the count in RCX.
954 if (length.IsConstant()) {
955 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
956 } else {
957 __ movl(count, length.AsRegister<CpuRegister>());
958 }
959
960 // Validity checks: source.
961 CheckPosition(assembler, srcPos, src, count, slow_path, src_base, dest_base);
962
963 // Validity checks: dest.
964 CheckPosition(assembler, destPos, dest, count, slow_path, src_base, dest_base);
965
966 // Okay, everything checks out. Finally time to do the copy.
967 // Check assumption that sizeof(Char) is 2 (used in scaling below).
968 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
969 DCHECK_EQ(char_size, 2u);
970
971 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
972
973 if (srcPos.IsConstant()) {
974 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
975 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
976 } else {
977 __ leal(src_base, Address(src, srcPos.AsRegister<CpuRegister>(),
978 ScaleFactor::TIMES_2, data_offset));
979 }
980 if (destPos.IsConstant()) {
981 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
982 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
983 } else {
984 __ leal(dest_base, Address(dest, destPos.AsRegister<CpuRegister>(),
985 ScaleFactor::TIMES_2, data_offset));
986 }
987
988 // Do the move.
989 __ rep_movsw();
990
991 __ Bind(slow_path->GetExitLabel());
992}
993
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000994void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
995 LocationSummary* locations = new (arena_) LocationSummary(invoke,
996 LocationSummary::kCall,
997 kIntrinsified);
998 InvokeRuntimeCallingConvention calling_convention;
999 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1000 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1001 locations->SetOut(Location::RegisterLocation(RAX));
1002}
1003
1004void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1005 X86_64Assembler* assembler = GetAssembler();
1006 LocationSummary* locations = invoke->GetLocations();
1007
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001008 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001009 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001010
1011 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1012 __ testl(argument, argument);
1013 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1014 codegen_->AddSlowPath(slow_path);
1015 __ j(kEqual, slow_path->GetEntryLabel());
1016
1017 __ gs()->call(Address::Absolute(
1018 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true));
1019 __ Bind(slow_path->GetExitLabel());
1020}
1021
Andreas Gampe21030dd2015-05-07 14:46:15 -07001022static void CreateStringIndexOfLocations(HInvoke* invoke,
1023 ArenaAllocator* allocator,
1024 bool start_at_zero) {
1025 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1026 LocationSummary::kCallOnSlowPath,
1027 kIntrinsified);
1028 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1029 locations->SetInAt(0, Location::RegisterLocation(RDI));
1030 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1031 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1032 // of the instruction explicitly.
1033 // Note: This works as we don't clobber RAX anywhere.
1034 locations->SetInAt(1, Location::RegisterLocation(RAX));
1035 if (!start_at_zero) {
1036 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1037 }
1038 // As we clobber RDI during execution anyways, also use it as the output.
1039 locations->SetOut(Location::SameAsFirstInput());
1040
1041 // repne scasw uses RCX as the counter.
1042 locations->AddTemp(Location::RegisterLocation(RCX));
1043 // Need another temporary to be able to compute the result.
1044 locations->AddTemp(Location::RequiresRegister());
1045}
1046
1047static void GenerateStringIndexOf(HInvoke* invoke,
1048 X86_64Assembler* assembler,
1049 CodeGeneratorX86_64* codegen,
1050 ArenaAllocator* allocator,
1051 bool start_at_zero) {
1052 LocationSummary* locations = invoke->GetLocations();
1053
1054 // Note that the null check must have been done earlier.
1055 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1056
1057 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1058 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1059 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1060 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1061 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1062
1063 // Check our assumptions for registers.
1064 DCHECK_EQ(string_obj.AsRegister(), RDI);
1065 DCHECK_EQ(search_value.AsRegister(), RAX);
1066 DCHECK_EQ(counter.AsRegister(), RCX);
1067 DCHECK_EQ(out.AsRegister(), RDI);
1068
1069 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1070 // or directly dispatch if we have a constant.
1071 SlowPathCodeX86_64* slow_path = nullptr;
1072 if (invoke->InputAt(1)->IsIntConstant()) {
1073 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1074 std::numeric_limits<uint16_t>::max()) {
1075 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1076 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1077 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1078 codegen->AddSlowPath(slow_path);
1079 __ jmp(slow_path->GetEntryLabel());
1080 __ Bind(slow_path->GetExitLabel());
1081 return;
1082 }
1083 } else {
1084 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1085 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1086 codegen->AddSlowPath(slow_path);
1087 __ j(kAbove, slow_path->GetEntryLabel());
1088 }
1089
1090 // From here down, we know that we are looking for a char that fits in 16 bits.
1091 // Location of reference to data array within the String object.
1092 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1093 // Location of count within the String object.
1094 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1095
1096 // Load string length, i.e., the count field of the string.
1097 __ movl(string_length, Address(string_obj, count_offset));
1098
1099 // Do a length check.
1100 // TODO: Support jecxz.
1101 Label not_found_label;
1102 __ testl(string_length, string_length);
1103 __ j(kEqual, &not_found_label);
1104
1105 if (start_at_zero) {
1106 // Number of chars to scan is the same as the string length.
1107 __ movl(counter, string_length);
1108
1109 // Move to the start of the string.
1110 __ addq(string_obj, Immediate(value_offset));
1111 } else {
1112 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1113
1114 // Do a start_index check.
1115 __ cmpl(start_index, string_length);
1116 __ j(kGreaterEqual, &not_found_label);
1117
1118 // Ensure we have a start index >= 0;
1119 __ xorl(counter, counter);
1120 __ cmpl(start_index, Immediate(0));
1121 __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough.
1122
1123 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1124 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1125
1126 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1127 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1128 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1129 }
1130
1131 // Everything is set up for repne scasw:
1132 // * Comparison address in RDI.
1133 // * Counter in ECX.
1134 __ repne_scasw();
1135
1136 // Did we find a match?
1137 __ j(kNotEqual, &not_found_label);
1138
1139 // Yes, we matched. Compute the index of the result.
1140 __ subl(string_length, counter);
1141 __ leal(out, Address(string_length, -1));
1142
1143 Label done;
1144 __ jmp(&done);
1145
1146 // Failed to match; return -1.
1147 __ Bind(&not_found_label);
1148 __ movl(out, Immediate(-1));
1149
1150 // And join up at the end.
1151 __ Bind(&done);
1152 if (slow_path != nullptr) {
1153 __ Bind(slow_path->GetExitLabel());
1154 }
1155}
1156
1157void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
1158 CreateStringIndexOfLocations(invoke, arena_, true);
1159}
1160
1161void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
1162 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1163}
1164
1165void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1166 CreateStringIndexOfLocations(invoke, arena_, false);
1167}
1168
1169void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1170 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1171}
1172
Jeff Hao848f70a2014-01-15 13:49:50 -08001173void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1174 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1175 LocationSummary::kCall,
1176 kIntrinsified);
1177 InvokeRuntimeCallingConvention calling_convention;
1178 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1179 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1180 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1181 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1182 locations->SetOut(Location::RegisterLocation(RAX));
1183}
1184
1185void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1186 X86_64Assembler* assembler = GetAssembler();
1187 LocationSummary* locations = invoke->GetLocations();
1188
1189 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1190 __ testl(byte_array, byte_array);
1191 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1192 codegen_->AddSlowPath(slow_path);
1193 __ j(kEqual, slow_path->GetEntryLabel());
1194
1195 __ gs()->call(Address::Absolute(
1196 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true));
1197 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1198 __ Bind(slow_path->GetExitLabel());
1199}
1200
1201void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1202 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1203 LocationSummary::kCall,
1204 kIntrinsified);
1205 InvokeRuntimeCallingConvention calling_convention;
1206 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1207 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1208 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1209 locations->SetOut(Location::RegisterLocation(RAX));
1210}
1211
1212void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1213 X86_64Assembler* assembler = GetAssembler();
1214
1215 __ gs()->call(Address::Absolute(
1216 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true));
1217 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1218}
1219
1220void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1221 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1222 LocationSummary::kCall,
1223 kIntrinsified);
1224 InvokeRuntimeCallingConvention calling_convention;
1225 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1226 locations->SetOut(Location::RegisterLocation(RAX));
1227}
1228
1229void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1230 X86_64Assembler* assembler = GetAssembler();
1231 LocationSummary* locations = invoke->GetLocations();
1232
1233 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1234 __ testl(string_to_copy, string_to_copy);
1235 SlowPathCodeX86_64* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1236 codegen_->AddSlowPath(slow_path);
1237 __ j(kEqual, slow_path->GetEntryLabel());
1238
1239 __ gs()->call(Address::Absolute(
1240 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true));
1241 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1242 __ Bind(slow_path->GetExitLabel());
1243}
1244
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001245static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1246 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1247 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1248 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1249 // to avoid a SIGBUS.
1250 switch (size) {
1251 case Primitive::kPrimByte:
1252 __ movsxb(out, Address(address, 0));
1253 break;
1254 case Primitive::kPrimShort:
1255 __ movsxw(out, Address(address, 0));
1256 break;
1257 case Primitive::kPrimInt:
1258 __ movl(out, Address(address, 0));
1259 break;
1260 case Primitive::kPrimLong:
1261 __ movq(out, Address(address, 0));
1262 break;
1263 default:
1264 LOG(FATAL) << "Type not recognized for peek: " << size;
1265 UNREACHABLE();
1266 }
1267}
1268
1269void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1270 CreateIntToIntLocations(arena_, invoke);
1271}
1272
1273void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1274 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1275}
1276
1277void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1278 CreateIntToIntLocations(arena_, invoke);
1279}
1280
1281void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1282 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1283}
1284
1285void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1286 CreateIntToIntLocations(arena_, invoke);
1287}
1288
1289void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1290 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1291}
1292
1293void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1294 CreateIntToIntLocations(arena_, invoke);
1295}
1296
1297void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1298 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1299}
1300
1301static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1302 LocationSummary* locations = new (arena) LocationSummary(invoke,
1303 LocationSummary::kNoCall,
1304 kIntrinsified);
1305 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001306 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001307}
1308
1309static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1310 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001311 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001312 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1313 // to avoid a SIGBUS.
1314 switch (size) {
1315 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001316 if (value.IsConstant()) {
1317 __ movb(Address(address, 0),
1318 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1319 } else {
1320 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1321 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001322 break;
1323 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001324 if (value.IsConstant()) {
1325 __ movw(Address(address, 0),
1326 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1327 } else {
1328 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1329 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001330 break;
1331 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001332 if (value.IsConstant()) {
1333 __ movl(Address(address, 0),
1334 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1335 } else {
1336 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1337 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001338 break;
1339 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001340 if (value.IsConstant()) {
1341 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1342 DCHECK(IsInt<32>(v));
1343 int32_t v_32 = v;
1344 __ movq(Address(address, 0), Immediate(v_32));
1345 } else {
1346 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1347 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001348 break;
1349 default:
1350 LOG(FATAL) << "Type not recognized for poke: " << size;
1351 UNREACHABLE();
1352 }
1353}
1354
1355void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1356 CreateIntIntToVoidLocations(arena_, invoke);
1357}
1358
1359void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1360 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1361}
1362
1363void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1364 CreateIntIntToVoidLocations(arena_, invoke);
1365}
1366
1367void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1368 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1369}
1370
1371void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1372 CreateIntIntToVoidLocations(arena_, invoke);
1373}
1374
1375void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1376 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1377}
1378
1379void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1380 CreateIntIntToVoidLocations(arena_, invoke);
1381}
1382
1383void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1384 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1385}
1386
1387void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1388 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1389 LocationSummary::kNoCall,
1390 kIntrinsified);
1391 locations->SetOut(Location::RequiresRegister());
1392}
1393
1394void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1395 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
1396 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true));
1397}
1398
Andreas Gampe878d58c2015-01-15 23:24:00 -08001399static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001400 bool is_volatile ATTRIBUTE_UNUSED, X86_64Assembler* assembler) {
1401 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1402 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1403 CpuRegister trg = locations->Out().AsRegister<CpuRegister>();
1404
Andreas Gampe878d58c2015-01-15 23:24:00 -08001405 switch (type) {
1406 case Primitive::kPrimInt:
1407 case Primitive::kPrimNot:
1408 __ movl(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001409 if (type == Primitive::kPrimNot) {
1410 __ MaybeUnpoisonHeapReference(trg);
1411 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001412 break;
1413
1414 case Primitive::kPrimLong:
1415 __ movq(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1416 break;
1417
1418 default:
1419 LOG(FATAL) << "Unsupported op size " << type;
1420 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001421 }
1422}
1423
1424static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
1425 LocationSummary* locations = new (arena) LocationSummary(invoke,
1426 LocationSummary::kNoCall,
1427 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001428 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001429 locations->SetInAt(1, Location::RequiresRegister());
1430 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001431 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001432}
1433
1434void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1435 CreateIntIntIntToIntLocations(arena_, invoke);
1436}
1437void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1438 CreateIntIntIntToIntLocations(arena_, invoke);
1439}
1440void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1441 CreateIntIntIntToIntLocations(arena_, invoke);
1442}
1443void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1444 CreateIntIntIntToIntLocations(arena_, invoke);
1445}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001446void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1447 CreateIntIntIntToIntLocations(arena_, invoke);
1448}
1449void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1450 CreateIntIntIntToIntLocations(arena_, invoke);
1451}
1452
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001453
1454void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001455 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001456}
1457void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001458 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001459}
1460void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001461 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001462}
1463void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001464 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001465}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001466void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1467 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1468}
1469void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1470 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1471}
1472
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001473
1474static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1475 Primitive::Type type,
1476 HInvoke* invoke) {
1477 LocationSummary* locations = new (arena) LocationSummary(invoke,
1478 LocationSummary::kNoCall,
1479 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001480 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001481 locations->SetInAt(1, Location::RequiresRegister());
1482 locations->SetInAt(2, Location::RequiresRegister());
1483 locations->SetInAt(3, Location::RequiresRegister());
1484 if (type == Primitive::kPrimNot) {
1485 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001486 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001487 locations->AddTemp(Location::RequiresRegister());
1488 }
1489}
1490
1491void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1492 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1493}
1494void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1495 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1496}
1497void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1498 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1499}
1500void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1501 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1502}
1503void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1504 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1505}
1506void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1507 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1508}
1509void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1510 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1511}
1512void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1513 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1514}
1515void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1516 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1517}
1518
1519// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1520// memory model.
1521static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1522 CodeGeneratorX86_64* codegen) {
1523 X86_64Assembler* assembler = reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1524 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1525 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1526 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1527
1528 if (type == Primitive::kPrimLong) {
1529 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001530 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1531 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1532 __ movl(temp, value);
1533 __ PoisonHeapReference(temp);
1534 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001535 } else {
1536 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1537 }
1538
1539 if (is_volatile) {
1540 __ mfence();
1541 }
1542
1543 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001544 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001545 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1546 locations->GetTemp(1).AsRegister<CpuRegister>(),
1547 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001548 value,
1549 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001550 }
1551}
1552
1553void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
1554 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1555}
1556void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1557 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1558}
1559void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1560 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1561}
1562void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1563 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1564}
1565void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1566 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1567}
1568void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1569 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1570}
1571void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1572 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1573}
1574void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1575 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1576}
1577void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1578 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1579}
1580
Mark Mendell58d25fd2015-04-03 14:52:31 -04001581static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1582 HInvoke* invoke) {
1583 LocationSummary* locations = new (arena) LocationSummary(invoke,
1584 LocationSummary::kNoCall,
1585 kIntrinsified);
1586 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1587 locations->SetInAt(1, Location::RequiresRegister());
1588 locations->SetInAt(2, Location::RequiresRegister());
1589 // expected value must be in EAX/RAX.
1590 locations->SetInAt(3, Location::RegisterLocation(RAX));
1591 locations->SetInAt(4, Location::RequiresRegister());
1592
1593 locations->SetOut(Location::RequiresRegister());
1594 if (type == Primitive::kPrimNot) {
1595 // Need temp registers for card-marking.
1596 locations->AddTemp(Location::RequiresRegister());
1597 locations->AddTemp(Location::RequiresRegister());
1598 }
1599}
1600
1601void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1602 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1603}
1604
1605void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1606 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1607}
1608
1609void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1610 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1611}
1612
1613static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1614 X86_64Assembler* assembler =
1615 reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1616 LocationSummary* locations = invoke->GetLocations();
1617
1618 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1619 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1620 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
1621 DCHECK_EQ(expected.AsRegister(), RAX);
1622 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1623 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1624
1625 if (type == Primitive::kPrimLong) {
1626 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
1627 } else {
1628 // Integer or object.
1629 if (type == Primitive::kPrimNot) {
1630 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001631 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001632 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1633 locations->GetTemp(1).AsRegister<CpuRegister>(),
1634 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001635 value,
1636 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001637
1638 if (kPoisonHeapReferences) {
1639 __ PoisonHeapReference(expected);
1640 __ PoisonHeapReference(value);
1641 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001642 }
1643
1644 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1645 }
1646
1647 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1648 // barriers at this time.
1649
1650 // Convert ZF into the boolean result.
1651 __ setcc(kZero, out);
1652 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01001653
1654 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1655 __ UnpoisonHeapReference(value);
1656 __ UnpoisonHeapReference(expected);
1657 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001658}
1659
1660void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1661 GenCAS(Primitive::kPrimInt, invoke, codegen_);
1662}
1663
1664void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1665 GenCAS(Primitive::kPrimLong, invoke, codegen_);
1666}
1667
1668void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1669 GenCAS(Primitive::kPrimNot, invoke, codegen_);
1670}
1671
1672void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
1673 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1674 LocationSummary::kNoCall,
1675 kIntrinsified);
1676 locations->SetInAt(0, Location::RequiresRegister());
1677 locations->SetOut(Location::SameAsFirstInput());
1678 locations->AddTemp(Location::RequiresRegister());
1679}
1680
1681static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
1682 X86_64Assembler* assembler) {
1683 Immediate imm_shift(shift);
1684 Immediate imm_mask(mask);
1685 __ movl(temp, reg);
1686 __ shrl(reg, imm_shift);
1687 __ andl(temp, imm_mask);
1688 __ andl(reg, imm_mask);
1689 __ shll(temp, imm_shift);
1690 __ orl(reg, temp);
1691}
1692
1693void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
1694 X86_64Assembler* assembler =
1695 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
1696 LocationSummary* locations = invoke->GetLocations();
1697
1698 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
1699 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1700
1701 /*
1702 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1703 * swapping bits to reverse bits in a number x. Using bswap to save instructions
1704 * compared to generic luni implementation which has 5 rounds of swapping bits.
1705 * x = bswap x
1706 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
1707 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
1708 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
1709 */
1710 __ bswapl(reg);
1711 SwapBits(reg, temp, 1, 0x55555555, assembler);
1712 SwapBits(reg, temp, 2, 0x33333333, assembler);
1713 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
1714}
1715
1716void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
1717 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1718 LocationSummary::kNoCall,
1719 kIntrinsified);
1720 locations->SetInAt(0, Location::RequiresRegister());
1721 locations->SetOut(Location::SameAsFirstInput());
1722 locations->AddTemp(Location::RequiresRegister());
1723 locations->AddTemp(Location::RequiresRegister());
1724}
1725
1726static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
1727 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
1728 Immediate imm_shift(shift);
1729 __ movq(temp_mask, Immediate(mask));
1730 __ movq(temp, reg);
1731 __ shrq(reg, imm_shift);
1732 __ andq(temp, temp_mask);
1733 __ andq(reg, temp_mask);
1734 __ shlq(temp, imm_shift);
1735 __ orq(reg, temp);
1736}
1737
1738void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
1739 X86_64Assembler* assembler =
1740 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
1741 LocationSummary* locations = invoke->GetLocations();
1742
1743 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
1744 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
1745 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
1746
1747 /*
1748 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1749 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
1750 * compared to generic luni implementation which has 5 rounds of swapping bits.
1751 * x = bswap x
1752 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
1753 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
1754 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
1755 */
1756 __ bswapq(reg);
1757 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
1758 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
1759 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
1760}
1761
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001762// Unimplemented intrinsics.
1763
1764#define UNIMPLEMENTED_INTRINSIC(Name) \
1765void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1766} \
1767void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1768}
1769
Jeff Hao848f70a2014-01-15 13:49:50 -08001770UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001771UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
Scott Wakeling611d3392015-07-10 11:42:06 +01001772UNIMPLEMENTED_INTRINSIC(IntegerNumberOfLeadingZeros)
1773UNIMPLEMENTED_INTRINSIC(LongNumberOfLeadingZeros)
agicsaki7da072f2015-08-12 20:30:17 -07001774UNIMPLEMENTED_INTRINSIC(StringEquals)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001775
Roland Levillain4d027112015-07-01 15:41:14 +01001776#undef UNIMPLEMENTED_INTRINSIC
1777
1778#undef __
1779
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001780} // namespace x86_64
1781} // namespace art