blob: 2bd86a1ac30089fa66f2043c59ef2ddea3ee6373 [file] [log] [blame]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_x86_64.h"
18
Andreas Gampe21030dd2015-05-07 14:46:15 -070019#include <limits>
20
Mark Mendellfb8d2792015-03-31 22:16:59 -040021#include "arch/x86_64/instruction_set_features_x86_64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Mark Mendelld5897672015-08-12 21:16:41 -040023#include "base/bit_utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "code_generator_x86_64.h"
25#include "entrypoints/quick/quick_entrypoints.h"
26#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070027#include "intrinsics_utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080028#include "mirror/array-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080029#include "mirror/string.h"
30#include "thread.h"
31#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/constants_x86_64.h"
33
34namespace art {
35
36namespace x86_64 {
37
Mark Mendellfb8d2792015-03-31 22:16:59 -040038IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
39 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
40}
41
42
Andreas Gampe71fb52f2014-12-29 17:43:08 -080043X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
44 return reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
45}
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080048 return codegen_->GetGraph()->GetArena();
49}
50
51bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
52 Dispatch(invoke);
53 const LocationSummary* res = invoke->GetLocations();
54 return res != nullptr && res->Intrinsified();
55}
56
Roland Levillainec525fc2015-04-28 15:50:20 +010057static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010058 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010059 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080060}
61
Andreas Gampe85b62f22015-09-09 13:15:38 -070062using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080063
Andreas Gampe71fb52f2014-12-29 17:43:08 -080064#define __ assembler->
65
66static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
67 LocationSummary* locations = new (arena) LocationSummary(invoke,
68 LocationSummary::kNoCall,
69 kIntrinsified);
70 locations->SetInAt(0, Location::RequiresFpuRegister());
71 locations->SetOut(Location::RequiresRegister());
72}
73
74static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
75 LocationSummary* locations = new (arena) LocationSummary(invoke,
76 LocationSummary::kNoCall,
77 kIntrinsified);
78 locations->SetInAt(0, Location::RequiresRegister());
79 locations->SetOut(Location::RequiresFpuRegister());
80}
81
82static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
83 Location input = locations->InAt(0);
84 Location output = locations->Out();
85 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
86}
87
88static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
89 Location input = locations->InAt(0);
90 Location output = locations->Out();
91 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
92}
93
94void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
95 CreateFPToIntLocations(arena_, invoke);
96}
97void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
98 CreateIntToFPLocations(arena_, invoke);
99}
100
101void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
102 MoveFPToInt(invoke->GetLocations(), true, GetAssembler());
103}
104void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
105 MoveIntToFP(invoke->GetLocations(), true, GetAssembler());
106}
107
108void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
109 CreateFPToIntLocations(arena_, invoke);
110}
111void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
112 CreateIntToFPLocations(arena_, invoke);
113}
114
115void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
116 MoveFPToInt(invoke->GetLocations(), false, GetAssembler());
117}
118void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
119 MoveIntToFP(invoke->GetLocations(), false, GetAssembler());
120}
121
122static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
123 LocationSummary* locations = new (arena) LocationSummary(invoke,
124 LocationSummary::kNoCall,
125 kIntrinsified);
126 locations->SetInAt(0, Location::RequiresRegister());
127 locations->SetOut(Location::SameAsFirstInput());
128}
129
130static void GenReverseBytes(LocationSummary* locations,
131 Primitive::Type size,
132 X86_64Assembler* assembler) {
133 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
134
135 switch (size) {
136 case Primitive::kPrimShort:
137 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
138 __ bswapl(out);
139 __ sarl(out, Immediate(16));
140 break;
141 case Primitive::kPrimInt:
142 __ bswapl(out);
143 break;
144 case Primitive::kPrimLong:
145 __ bswapq(out);
146 break;
147 default:
148 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
149 UNREACHABLE();
150 }
151}
152
153void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
154 CreateIntToIntLocations(arena_, invoke);
155}
156
157void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
158 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
159}
160
161void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
162 CreateIntToIntLocations(arena_, invoke);
163}
164
165void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
166 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
167}
168
169void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
170 CreateIntToIntLocations(arena_, invoke);
171}
172
173void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
174 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
175}
176
177
178// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
179// need is 64b.
180
181static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
182 // TODO: Enable memory operations when the assembler supports them.
183 LocationSummary* locations = new (arena) LocationSummary(invoke,
184 LocationSummary::kNoCall,
185 kIntrinsified);
186 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800187 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400188 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800189}
190
Mark Mendell39dcf552015-04-09 20:42:42 -0400191static void MathAbsFP(LocationSummary* locations,
192 bool is64bit,
193 X86_64Assembler* assembler,
194 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800195 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800196
Mark Mendellcfa410b2015-05-25 16:02:44 -0400197 DCHECK(output.IsFpuRegister());
198 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800199
Mark Mendellcfa410b2015-05-25 16:02:44 -0400200 // TODO: Can mask directly with constant area using pand if we can guarantee
201 // that the literal is aligned on a 16 byte boundary. This will avoid a
202 // temporary.
203 if (is64bit) {
204 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
205 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800206 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400207 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
208 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800209 }
210}
211
212void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
213 CreateFloatToFloatPlusTemps(arena_, invoke);
214}
215
216void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400217 MathAbsFP(invoke->GetLocations(), true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800218}
219
220void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
221 CreateFloatToFloatPlusTemps(arena_, invoke);
222}
223
224void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400225 MathAbsFP(invoke->GetLocations(), false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800226}
227
228static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
229 LocationSummary* locations = new (arena) LocationSummary(invoke,
230 LocationSummary::kNoCall,
231 kIntrinsified);
232 locations->SetInAt(0, Location::RequiresRegister());
233 locations->SetOut(Location::SameAsFirstInput());
234 locations->AddTemp(Location::RequiresRegister());
235}
236
237static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
238 Location output = locations->Out();
239 CpuRegister out = output.AsRegister<CpuRegister>();
240 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
241
242 if (is64bit) {
243 // Create mask.
244 __ movq(mask, out);
245 __ sarq(mask, Immediate(63));
246 // Add mask.
247 __ addq(out, mask);
248 __ xorq(out, mask);
249 } else {
250 // Create mask.
251 __ movl(mask, out);
252 __ sarl(mask, Immediate(31));
253 // Add mask.
254 __ addl(out, mask);
255 __ xorl(out, mask);
256 }
257}
258
259void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
260 CreateIntToIntPlusTemp(arena_, invoke);
261}
262
263void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
264 GenAbsInteger(invoke->GetLocations(), false, GetAssembler());
265}
266
267void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
268 CreateIntToIntPlusTemp(arena_, invoke);
269}
270
271void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
272 GenAbsInteger(invoke->GetLocations(), true, GetAssembler());
273}
274
Mark Mendell39dcf552015-04-09 20:42:42 -0400275static void GenMinMaxFP(LocationSummary* locations,
276 bool is_min,
277 bool is_double,
278 X86_64Assembler* assembler,
279 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800280 Location op1_loc = locations->InAt(0);
281 Location op2_loc = locations->InAt(1);
282 Location out_loc = locations->Out();
283 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
284
285 // Shortcut for same input locations.
286 if (op1_loc.Equals(op2_loc)) {
287 DCHECK(out_loc.Equals(op1_loc));
288 return;
289 }
290
291 // (out := op1)
292 // out <=? op2
293 // if Nan jmp Nan_label
294 // if out is min jmp done
295 // if op2 is min jmp op2_label
296 // handle -0/+0
297 // jmp done
298 // Nan_label:
299 // out := NaN
300 // op2_label:
301 // out := op2
302 // done:
303 //
304 // This removes one jmp, but needs to copy one input (op1) to out.
305 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400306 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800307
308 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
309
Mark Mendell0c9497d2015-08-21 09:30:05 -0400310 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800311 if (is_double) {
312 __ ucomisd(out, op2);
313 } else {
314 __ ucomiss(out, op2);
315 }
316
317 __ j(Condition::kParityEven, &nan);
318
319 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
320 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
321
322 // Handle 0.0/-0.0.
323 if (is_min) {
324 if (is_double) {
325 __ orpd(out, op2);
326 } else {
327 __ orps(out, op2);
328 }
329 } else {
330 if (is_double) {
331 __ andpd(out, op2);
332 } else {
333 __ andps(out, op2);
334 }
335 }
336 __ jmp(&done);
337
338 // NaN handling.
339 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800340 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400341 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800342 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400343 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800344 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800345 __ jmp(&done);
346
347 // out := op2;
348 __ Bind(&op2_label);
349 if (is_double) {
350 __ movsd(out, op2);
351 } else {
352 __ movss(out, op2);
353 }
354
355 // Done.
356 __ Bind(&done);
357}
358
Mark Mendellf55c3e02015-03-26 21:07:46 -0400359static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800360 LocationSummary* locations = new (arena) LocationSummary(invoke,
361 LocationSummary::kNoCall,
362 kIntrinsified);
363 locations->SetInAt(0, Location::RequiresFpuRegister());
364 locations->SetInAt(1, Location::RequiresFpuRegister());
365 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
366 // the second input to be the output (we can simply swap inputs).
367 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800368}
369
370void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400371 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800372}
373
374void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400375 GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800376}
377
378void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400379 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800380}
381
382void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400383 GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800384}
385
386void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400387 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800388}
389
390void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400391 GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800392}
393
394void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400395 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800396}
397
398void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400399 GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800400}
401
402static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
403 X86_64Assembler* assembler) {
404 Location op1_loc = locations->InAt(0);
405 Location op2_loc = locations->InAt(1);
406
407 // Shortcut for same input locations.
408 if (op1_loc.Equals(op2_loc)) {
409 // Can return immediately, as op1_loc == out_loc.
410 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
411 // a copy here.
412 DCHECK(locations->Out().Equals(op1_loc));
413 return;
414 }
415
416 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
417 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
418
419 // (out := op1)
420 // out <=? op2
421 // if out is min jmp done
422 // out := op2
423 // done:
424
425 if (is_long) {
426 __ cmpq(out, op2);
427 } else {
428 __ cmpl(out, op2);
429 }
430
431 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
432}
433
434static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
435 LocationSummary* locations = new (arena) LocationSummary(invoke,
436 LocationSummary::kNoCall,
437 kIntrinsified);
438 locations->SetInAt(0, Location::RequiresRegister());
439 locations->SetInAt(1, Location::RequiresRegister());
440 locations->SetOut(Location::SameAsFirstInput());
441}
442
443void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
444 CreateIntIntToIntLocations(arena_, invoke);
445}
446
447void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
448 GenMinMax(invoke->GetLocations(), true, false, GetAssembler());
449}
450
451void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
452 CreateIntIntToIntLocations(arena_, invoke);
453}
454
455void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
456 GenMinMax(invoke->GetLocations(), true, true, GetAssembler());
457}
458
459void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
460 CreateIntIntToIntLocations(arena_, invoke);
461}
462
463void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
464 GenMinMax(invoke->GetLocations(), false, false, GetAssembler());
465}
466
467void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
468 CreateIntIntToIntLocations(arena_, invoke);
469}
470
471void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
472 GenMinMax(invoke->GetLocations(), false, true, GetAssembler());
473}
474
475static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
476 LocationSummary* locations = new (arena) LocationSummary(invoke,
477 LocationSummary::kNoCall,
478 kIntrinsified);
479 locations->SetInAt(0, Location::RequiresFpuRegister());
480 locations->SetOut(Location::RequiresFpuRegister());
481}
482
483void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
484 CreateFPToFPLocations(arena_, invoke);
485}
486
487void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
488 LocationSummary* locations = invoke->GetLocations();
489 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
490 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
491
492 GetAssembler()->sqrtsd(out, in);
493}
494
Mark Mendellfb8d2792015-03-31 22:16:59 -0400495static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100496 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400497
498 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100499 codegen->GenerateStaticOrDirectCall(
500 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400501 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
502
503 // Copy the result back to the expected output.
504 Location out = invoke->GetLocations()->Out();
505 if (out.IsValid()) {
506 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700507 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400508 }
509}
510
511static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
512 HInvoke* invoke,
513 CodeGeneratorX86_64* codegen) {
514 // Do we have instruction support?
515 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
516 CreateFPToFPLocations(arena, invoke);
517 return;
518 }
519
520 // We have to fall back to a call to the intrinsic.
521 LocationSummary* locations = new (arena) LocationSummary(invoke,
522 LocationSummary::kCall);
523 InvokeRuntimeCallingConvention calling_convention;
524 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
525 locations->SetOut(Location::FpuRegisterLocation(XMM0));
526 // Needs to be RDI for the invoke.
527 locations->AddTemp(Location::RegisterLocation(RDI));
528}
529
530static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
531 HInvoke* invoke,
532 X86_64Assembler* assembler,
533 int round_mode) {
534 LocationSummary* locations = invoke->GetLocations();
535 if (locations->WillCall()) {
536 InvokeOutOfLineIntrinsic(codegen, invoke);
537 } else {
538 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
539 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
540 __ roundsd(out, in, Immediate(round_mode));
541 }
542}
543
544void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
545 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
546}
547
548void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
549 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
550}
551
552void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
553 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
554}
555
556void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
557 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
558}
559
560void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
561 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
562}
563
564void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
565 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
566}
567
568static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
569 HInvoke* invoke,
570 CodeGeneratorX86_64* codegen) {
571 // Do we have instruction support?
572 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
573 LocationSummary* locations = new (arena) LocationSummary(invoke,
574 LocationSummary::kNoCall,
575 kIntrinsified);
576 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600577 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400578 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400579 return;
580 }
581
582 // We have to fall back to a call to the intrinsic.
583 LocationSummary* locations = new (arena) LocationSummary(invoke,
584 LocationSummary::kCall);
585 InvokeRuntimeCallingConvention calling_convention;
586 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
587 locations->SetOut(Location::RegisterLocation(RAX));
588 // Needs to be RDI for the invoke.
589 locations->AddTemp(Location::RegisterLocation(RDI));
590}
591
592void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
593 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
594}
595
596void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
597 LocationSummary* locations = invoke->GetLocations();
598 if (locations->WillCall()) {
599 InvokeOutOfLineIntrinsic(codegen_, invoke);
600 return;
601 }
602
603 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
604 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
605 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400606 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400607 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400608 X86_64Assembler* assembler = GetAssembler();
609
Mark Mendell40741f32015-04-20 22:10:34 -0400610 // Load 0.5 into inPlusPointFive.
611 __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400612
613 // Add in the input.
614 __ addss(inPlusPointFive, in);
615
616 // And truncate to an integer.
617 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
618
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600619 // Load maxInt into out.
620 codegen_->Load64BitValue(out, kPrimIntMax);
621
Mark Mendellfb8d2792015-03-31 22:16:59 -0400622 // if inPlusPointFive >= maxInt goto done
Mark Mendellcfa410b2015-05-25 16:02:44 -0400623 __ movl(out, Immediate(kPrimIntMax));
Mark Mendell40741f32015-04-20 22:10:34 -0400624 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400625 __ j(kAboveEqual, &done);
626
627 // if input == NaN goto nan
628 __ j(kUnordered, &nan);
629
630 // output = float-to-int-truncate(input)
631 __ cvttss2si(out, inPlusPointFive);
632 __ jmp(&done);
633 __ Bind(&nan);
634
635 // output = 0
636 __ xorl(out, out);
637 __ Bind(&done);
638}
639
640void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
641 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
642}
643
644void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
645 LocationSummary* locations = invoke->GetLocations();
646 if (locations->WillCall()) {
647 InvokeOutOfLineIntrinsic(codegen_, invoke);
648 return;
649 }
650
651 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
652 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
653 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400654 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400655 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400656 X86_64Assembler* assembler = GetAssembler();
657
Mark Mendell40741f32015-04-20 22:10:34 -0400658 // Load 0.5 into inPlusPointFive.
659 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400660
661 // Add in the input.
662 __ addsd(inPlusPointFive, in);
663
664 // And truncate to an integer.
665 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
666
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600667 // Load maxLong into out.
668 codegen_->Load64BitValue(out, kPrimLongMax);
669
Mark Mendellfb8d2792015-03-31 22:16:59 -0400670 // if inPlusPointFive >= maxLong goto done
Mark Mendellcfa410b2015-05-25 16:02:44 -0400671 __ movq(out, Immediate(kPrimLongMax));
Mark Mendell40741f32015-04-20 22:10:34 -0400672 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400673 __ j(kAboveEqual, &done);
674
675 // if input == NaN goto nan
676 __ j(kUnordered, &nan);
677
678 // output = double-to-long-truncate(input)
679 __ cvttsd2si(out, inPlusPointFive, true);
680 __ jmp(&done);
681 __ Bind(&nan);
682
683 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400684 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400685 __ Bind(&done);
686}
687
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800688void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) {
689 // The inputs plus one temp.
690 LocationSummary* locations = new (arena_) LocationSummary(invoke,
691 LocationSummary::kCallOnSlowPath,
692 kIntrinsified);
693 locations->SetInAt(0, Location::RequiresRegister());
694 locations->SetInAt(1, Location::RequiresRegister());
695 locations->SetOut(Location::SameAsFirstInput());
696 locations->AddTemp(Location::RequiresRegister());
697}
698
699void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) {
700 LocationSummary* locations = invoke->GetLocations();
701
Mark Mendell6bc53a92015-07-01 14:26:52 -0400702 // Location of reference to data array.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800703 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400704 // Location of count.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800705 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800706
707 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
708 CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>();
709 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800710
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800711 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
712 // the cost.
713 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
714 // we will not optimize the code for constants (which would save a register).
715
Andreas Gampe85b62f22015-09-09 13:15:38 -0700716 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800717 codegen_->AddSlowPath(slow_path);
718
719 X86_64Assembler* assembler = GetAssembler();
720
721 __ cmpl(idx, Address(obj, count_offset));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800722 codegen_->MaybeRecordImplicitNullCheck(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800723 __ j(kAboveEqual, slow_path->GetEntryLabel());
724
Jeff Hao848f70a2014-01-15 13:49:50 -0800725 // out = out[2*idx].
726 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800727
728 __ Bind(slow_path->GetExitLabel());
729}
730
Mark Mendell6bc53a92015-07-01 14:26:52 -0400731void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
732 // Check to see if we have known failures that will cause us to have to bail out
733 // to the runtime, and just generate the runtime call directly.
734 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
735 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
736
737 // The positions must be non-negative.
738 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
739 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
740 // We will have to fail anyways.
741 return;
742 }
743
744 // The length must be > 0.
745 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
746 if (length != nullptr) {
747 int32_t len = length->GetValue();
748 if (len < 0) {
749 // Just call as normal.
750 return;
751 }
752 }
753
754 LocationSummary* locations = new (arena_) LocationSummary(invoke,
755 LocationSummary::kCallOnSlowPath,
756 kIntrinsified);
757 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
758 locations->SetInAt(0, Location::RequiresRegister());
759 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
760 locations->SetInAt(2, Location::RequiresRegister());
761 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
762 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
763
764 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
765 locations->AddTemp(Location::RegisterLocation(RSI));
766 locations->AddTemp(Location::RegisterLocation(RDI));
767 locations->AddTemp(Location::RegisterLocation(RCX));
768}
769
770static void CheckPosition(X86_64Assembler* assembler,
771 Location pos,
772 CpuRegister input,
773 CpuRegister length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700774 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400775 CpuRegister input_len,
776 CpuRegister temp) {
777 // Where is the length in the String?
778 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
779
780 if (pos.IsConstant()) {
781 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
782 if (pos_const == 0) {
783 // Check that length(input) >= length.
784 __ cmpl(Address(input, length_offset), length);
785 __ j(kLess, slow_path->GetEntryLabel());
786 } else {
787 // Check that length(input) >= pos.
788 __ movl(input_len, Address(input, length_offset));
789 __ cmpl(input_len, Immediate(pos_const));
790 __ j(kLess, slow_path->GetEntryLabel());
791
792 // Check that (length(input) - pos) >= length.
793 __ leal(temp, Address(input_len, -pos_const));
794 __ cmpl(temp, length);
795 __ j(kLess, slow_path->GetEntryLabel());
796 }
797 } else {
798 // Check that pos >= 0.
799 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
800 __ testl(pos_reg, pos_reg);
801 __ j(kLess, slow_path->GetEntryLabel());
802
803 // Check that pos <= length(input).
804 __ cmpl(Address(input, length_offset), pos_reg);
805 __ j(kLess, slow_path->GetEntryLabel());
806
807 // Check that (length(input) - pos) >= length.
808 __ movl(temp, Address(input, length_offset));
809 __ subl(temp, pos_reg);
810 __ cmpl(temp, length);
811 __ j(kLess, slow_path->GetEntryLabel());
812 }
813}
814
815void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
816 X86_64Assembler* assembler = GetAssembler();
817 LocationSummary* locations = invoke->GetLocations();
818
819 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
820 Location srcPos = locations->InAt(1);
821 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
822 Location destPos = locations->InAt(3);
823 Location length = locations->InAt(4);
824
825 // Temporaries that we need for MOVSW.
826 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
827 DCHECK_EQ(src_base.AsRegister(), RSI);
828 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
829 DCHECK_EQ(dest_base.AsRegister(), RDI);
830 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
831 DCHECK_EQ(count.AsRegister(), RCX);
832
Andreas Gampe85b62f22015-09-09 13:15:38 -0700833 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400834 codegen_->AddSlowPath(slow_path);
835
836 // Bail out if the source and destination are the same.
837 __ cmpl(src, dest);
838 __ j(kEqual, slow_path->GetEntryLabel());
839
840 // Bail out if the source is null.
841 __ testl(src, src);
842 __ j(kEqual, slow_path->GetEntryLabel());
843
844 // Bail out if the destination is null.
845 __ testl(dest, dest);
846 __ j(kEqual, slow_path->GetEntryLabel());
847
848 // If the length is negative, bail out.
849 // We have already checked in the LocationsBuilder for the constant case.
850 if (!length.IsConstant()) {
851 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
852 __ j(kLess, slow_path->GetEntryLabel());
853 }
854
855 // We need the count in RCX.
856 if (length.IsConstant()) {
857 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
858 } else {
859 __ movl(count, length.AsRegister<CpuRegister>());
860 }
861
862 // Validity checks: source.
863 CheckPosition(assembler, srcPos, src, count, slow_path, src_base, dest_base);
864
865 // Validity checks: dest.
866 CheckPosition(assembler, destPos, dest, count, slow_path, src_base, dest_base);
867
868 // Okay, everything checks out. Finally time to do the copy.
869 // Check assumption that sizeof(Char) is 2 (used in scaling below).
870 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
871 DCHECK_EQ(char_size, 2u);
872
873 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
874
875 if (srcPos.IsConstant()) {
876 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
877 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
878 } else {
879 __ leal(src_base, Address(src, srcPos.AsRegister<CpuRegister>(),
880 ScaleFactor::TIMES_2, data_offset));
881 }
882 if (destPos.IsConstant()) {
883 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
884 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
885 } else {
886 __ leal(dest_base, Address(dest, destPos.AsRegister<CpuRegister>(),
887 ScaleFactor::TIMES_2, data_offset));
888 }
889
890 // Do the move.
891 __ rep_movsw();
892
893 __ Bind(slow_path->GetExitLabel());
894}
895
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000896void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
897 LocationSummary* locations = new (arena_) LocationSummary(invoke,
898 LocationSummary::kCall,
899 kIntrinsified);
900 InvokeRuntimeCallingConvention calling_convention;
901 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
902 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
903 locations->SetOut(Location::RegisterLocation(RAX));
904}
905
906void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
907 X86_64Assembler* assembler = GetAssembler();
908 LocationSummary* locations = invoke->GetLocations();
909
Nicolas Geoffray512e04d2015-03-27 17:21:24 +0000910 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +0100911 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000912
913 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
914 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -0700915 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000916 codegen_->AddSlowPath(slow_path);
917 __ j(kEqual, slow_path->GetEntryLabel());
918
919 __ gs()->call(Address::Absolute(
920 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true));
921 __ Bind(slow_path->GetExitLabel());
922}
923
Agi Csakif8cfb202015-08-13 17:54:54 -0700924void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
925 LocationSummary* locations = new (arena_) LocationSummary(invoke,
926 LocationSummary::kNoCall,
927 kIntrinsified);
928 locations->SetInAt(0, Location::RequiresRegister());
929 locations->SetInAt(1, Location::RequiresRegister());
930
931 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
932 locations->AddTemp(Location::RegisterLocation(RCX));
933 locations->AddTemp(Location::RegisterLocation(RDI));
934
935 // Set output, RSI needed for repe_cmpsq instruction anyways.
936 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
937}
938
939void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
940 X86_64Assembler* assembler = GetAssembler();
941 LocationSummary* locations = invoke->GetLocations();
942
943 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
944 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
945 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
946 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
947 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
948
Mark Mendell0c9497d2015-08-21 09:30:05 -0400949 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -0700950
951 // Get offsets of count, value, and class fields within a string object.
952 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
953 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
954 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
955
956 // Note that the null check must have been done earlier.
957 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
958
959 // Check if input is null, return false if it is.
960 __ testl(arg, arg);
961 __ j(kEqual, &return_false);
962
963 // Instanceof check for the argument by comparing class fields.
964 // All string objects must have the same type since String cannot be subclassed.
965 // Receiver must be a string object, so its class field is equal to all strings' class fields.
966 // If the argument is a string object, its class field must be equal to receiver's class field.
967 __ movl(rcx, Address(str, class_offset));
968 __ cmpl(rcx, Address(arg, class_offset));
969 __ j(kNotEqual, &return_false);
970
971 // Reference equality check, return true if same reference.
972 __ cmpl(str, arg);
973 __ j(kEqual, &return_true);
974
975 // Load length of receiver string.
976 __ movl(rcx, Address(str, count_offset));
977 // Check if lengths are equal, return false if they're not.
978 __ cmpl(rcx, Address(arg, count_offset));
979 __ j(kNotEqual, &return_false);
980 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -0400981 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -0700982
983 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
984 __ leal(rsi, Address(str, value_offset));
985 __ leal(rdi, Address(arg, value_offset));
986
987 // Divide string length by 4 and adjust for lengths not divisible by 4.
988 __ addl(rcx, Immediate(3));
989 __ shrl(rcx, Immediate(2));
990
991 // Assertions that must hold in order to compare strings 4 characters at a time.
992 DCHECK_ALIGNED(value_offset, 8);
993 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
994
995 // Loop to compare strings four characters at a time starting at the beginning of the string.
996 __ repe_cmpsq();
997 // If strings are not equal, zero flag will be cleared.
998 __ j(kNotEqual, &return_false);
999
1000 // Return true and exit the function.
1001 // If loop does not result in returning false, we return true.
1002 __ Bind(&return_true);
1003 __ movl(rsi, Immediate(1));
1004 __ jmp(&end);
1005
1006 // Return false and exit the function.
1007 __ Bind(&return_false);
1008 __ xorl(rsi, rsi);
1009 __ Bind(&end);
1010}
1011
Andreas Gampe21030dd2015-05-07 14:46:15 -07001012static void CreateStringIndexOfLocations(HInvoke* invoke,
1013 ArenaAllocator* allocator,
1014 bool start_at_zero) {
1015 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1016 LocationSummary::kCallOnSlowPath,
1017 kIntrinsified);
1018 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1019 locations->SetInAt(0, Location::RegisterLocation(RDI));
1020 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1021 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1022 // of the instruction explicitly.
1023 // Note: This works as we don't clobber RAX anywhere.
1024 locations->SetInAt(1, Location::RegisterLocation(RAX));
1025 if (!start_at_zero) {
1026 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1027 }
1028 // As we clobber RDI during execution anyways, also use it as the output.
1029 locations->SetOut(Location::SameAsFirstInput());
1030
1031 // repne scasw uses RCX as the counter.
1032 locations->AddTemp(Location::RegisterLocation(RCX));
1033 // Need another temporary to be able to compute the result.
1034 locations->AddTemp(Location::RequiresRegister());
1035}
1036
1037static void GenerateStringIndexOf(HInvoke* invoke,
1038 X86_64Assembler* assembler,
1039 CodeGeneratorX86_64* codegen,
1040 ArenaAllocator* allocator,
1041 bool start_at_zero) {
1042 LocationSummary* locations = invoke->GetLocations();
1043
1044 // Note that the null check must have been done earlier.
1045 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1046
1047 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1048 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1049 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1050 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1051 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1052
1053 // Check our assumptions for registers.
1054 DCHECK_EQ(string_obj.AsRegister(), RDI);
1055 DCHECK_EQ(search_value.AsRegister(), RAX);
1056 DCHECK_EQ(counter.AsRegister(), RCX);
1057 DCHECK_EQ(out.AsRegister(), RDI);
1058
1059 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1060 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001061 SlowPathCode* slow_path = nullptr;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001062 if (invoke->InputAt(1)->IsIntConstant()) {
1063 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1064 std::numeric_limits<uint16_t>::max()) {
1065 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1066 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1067 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1068 codegen->AddSlowPath(slow_path);
1069 __ jmp(slow_path->GetEntryLabel());
1070 __ Bind(slow_path->GetExitLabel());
1071 return;
1072 }
1073 } else {
1074 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1075 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1076 codegen->AddSlowPath(slow_path);
1077 __ j(kAbove, slow_path->GetEntryLabel());
1078 }
1079
1080 // From here down, we know that we are looking for a char that fits in 16 bits.
1081 // Location of reference to data array within the String object.
1082 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1083 // Location of count within the String object.
1084 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1085
1086 // Load string length, i.e., the count field of the string.
1087 __ movl(string_length, Address(string_obj, count_offset));
1088
1089 // Do a length check.
1090 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001091 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001092 __ testl(string_length, string_length);
1093 __ j(kEqual, &not_found_label);
1094
1095 if (start_at_zero) {
1096 // Number of chars to scan is the same as the string length.
1097 __ movl(counter, string_length);
1098
1099 // Move to the start of the string.
1100 __ addq(string_obj, Immediate(value_offset));
1101 } else {
1102 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1103
1104 // Do a start_index check.
1105 __ cmpl(start_index, string_length);
1106 __ j(kGreaterEqual, &not_found_label);
1107
1108 // Ensure we have a start index >= 0;
1109 __ xorl(counter, counter);
1110 __ cmpl(start_index, Immediate(0));
1111 __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough.
1112
1113 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1114 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1115
1116 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1117 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1118 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1119 }
1120
1121 // Everything is set up for repne scasw:
1122 // * Comparison address in RDI.
1123 // * Counter in ECX.
1124 __ repne_scasw();
1125
1126 // Did we find a match?
1127 __ j(kNotEqual, &not_found_label);
1128
1129 // Yes, we matched. Compute the index of the result.
1130 __ subl(string_length, counter);
1131 __ leal(out, Address(string_length, -1));
1132
Mark Mendell0c9497d2015-08-21 09:30:05 -04001133 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001134 __ jmp(&done);
1135
1136 // Failed to match; return -1.
1137 __ Bind(&not_found_label);
1138 __ movl(out, Immediate(-1));
1139
1140 // And join up at the end.
1141 __ Bind(&done);
1142 if (slow_path != nullptr) {
1143 __ Bind(slow_path->GetExitLabel());
1144 }
1145}
1146
1147void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
1148 CreateStringIndexOfLocations(invoke, arena_, true);
1149}
1150
1151void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
1152 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1153}
1154
1155void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1156 CreateStringIndexOfLocations(invoke, arena_, false);
1157}
1158
1159void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1160 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1161}
1162
Jeff Hao848f70a2014-01-15 13:49:50 -08001163void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1164 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1165 LocationSummary::kCall,
1166 kIntrinsified);
1167 InvokeRuntimeCallingConvention calling_convention;
1168 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1169 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1170 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1171 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1172 locations->SetOut(Location::RegisterLocation(RAX));
1173}
1174
1175void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1176 X86_64Assembler* assembler = GetAssembler();
1177 LocationSummary* locations = invoke->GetLocations();
1178
1179 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1180 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001181 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001182 codegen_->AddSlowPath(slow_path);
1183 __ j(kEqual, slow_path->GetEntryLabel());
1184
1185 __ gs()->call(Address::Absolute(
1186 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true));
1187 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1188 __ Bind(slow_path->GetExitLabel());
1189}
1190
1191void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1192 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1193 LocationSummary::kCall,
1194 kIntrinsified);
1195 InvokeRuntimeCallingConvention calling_convention;
1196 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1197 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1198 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1199 locations->SetOut(Location::RegisterLocation(RAX));
1200}
1201
1202void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1203 X86_64Assembler* assembler = GetAssembler();
1204
1205 __ gs()->call(Address::Absolute(
1206 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true));
1207 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1208}
1209
1210void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1211 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1212 LocationSummary::kCall,
1213 kIntrinsified);
1214 InvokeRuntimeCallingConvention calling_convention;
1215 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1216 locations->SetOut(Location::RegisterLocation(RAX));
1217}
1218
1219void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1220 X86_64Assembler* assembler = GetAssembler();
1221 LocationSummary* locations = invoke->GetLocations();
1222
1223 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1224 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001225 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001226 codegen_->AddSlowPath(slow_path);
1227 __ j(kEqual, slow_path->GetEntryLabel());
1228
1229 __ gs()->call(Address::Absolute(
1230 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true));
1231 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1232 __ Bind(slow_path->GetExitLabel());
1233}
1234
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001235static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1236 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1237 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1238 // 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:
1242 __ movsxb(out, Address(address, 0));
1243 break;
1244 case Primitive::kPrimShort:
1245 __ movsxw(out, Address(address, 0));
1246 break;
1247 case Primitive::kPrimInt:
1248 __ movl(out, Address(address, 0));
1249 break;
1250 case Primitive::kPrimLong:
1251 __ movq(out, Address(address, 0));
1252 break;
1253 default:
1254 LOG(FATAL) << "Type not recognized for peek: " << size;
1255 UNREACHABLE();
1256 }
1257}
1258
1259void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1260 CreateIntToIntLocations(arena_, invoke);
1261}
1262
1263void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1264 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1265}
1266
1267void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1268 CreateIntToIntLocations(arena_, invoke);
1269}
1270
1271void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1272 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1273}
1274
1275void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1276 CreateIntToIntLocations(arena_, invoke);
1277}
1278
1279void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1280 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1281}
1282
1283void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1284 CreateIntToIntLocations(arena_, invoke);
1285}
1286
1287void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1288 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1289}
1290
1291static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1292 LocationSummary* locations = new (arena) LocationSummary(invoke,
1293 LocationSummary::kNoCall,
1294 kIntrinsified);
1295 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001296 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001297}
1298
1299static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1300 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001301 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001302 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1303 // to avoid a SIGBUS.
1304 switch (size) {
1305 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001306 if (value.IsConstant()) {
1307 __ movb(Address(address, 0),
1308 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1309 } else {
1310 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1311 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001312 break;
1313 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001314 if (value.IsConstant()) {
1315 __ movw(Address(address, 0),
1316 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1317 } else {
1318 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1319 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001320 break;
1321 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001322 if (value.IsConstant()) {
1323 __ movl(Address(address, 0),
1324 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1325 } else {
1326 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1327 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001328 break;
1329 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001330 if (value.IsConstant()) {
1331 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1332 DCHECK(IsInt<32>(v));
1333 int32_t v_32 = v;
1334 __ movq(Address(address, 0), Immediate(v_32));
1335 } else {
1336 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1337 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001338 break;
1339 default:
1340 LOG(FATAL) << "Type not recognized for poke: " << size;
1341 UNREACHABLE();
1342 }
1343}
1344
1345void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1346 CreateIntIntToVoidLocations(arena_, invoke);
1347}
1348
1349void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1350 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1351}
1352
1353void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1354 CreateIntIntToVoidLocations(arena_, invoke);
1355}
1356
1357void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1358 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1359}
1360
1361void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1362 CreateIntIntToVoidLocations(arena_, invoke);
1363}
1364
1365void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1366 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1367}
1368
1369void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1370 CreateIntIntToVoidLocations(arena_, invoke);
1371}
1372
1373void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1374 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1375}
1376
1377void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1378 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1379 LocationSummary::kNoCall,
1380 kIntrinsified);
1381 locations->SetOut(Location::RequiresRegister());
1382}
1383
1384void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1385 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
1386 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true));
1387}
1388
Andreas Gampe878d58c2015-01-15 23:24:00 -08001389static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001390 bool is_volatile ATTRIBUTE_UNUSED, X86_64Assembler* assembler) {
1391 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1392 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1393 CpuRegister trg = locations->Out().AsRegister<CpuRegister>();
1394
Andreas Gampe878d58c2015-01-15 23:24:00 -08001395 switch (type) {
1396 case Primitive::kPrimInt:
1397 case Primitive::kPrimNot:
1398 __ movl(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001399 if (type == Primitive::kPrimNot) {
1400 __ MaybeUnpoisonHeapReference(trg);
1401 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001402 break;
1403
1404 case Primitive::kPrimLong:
1405 __ movq(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1406 break;
1407
1408 default:
1409 LOG(FATAL) << "Unsupported op size " << type;
1410 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001411 }
1412}
1413
1414static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
1415 LocationSummary* locations = new (arena) LocationSummary(invoke,
1416 LocationSummary::kNoCall,
1417 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001418 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001419 locations->SetInAt(1, Location::RequiresRegister());
1420 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001421 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001422}
1423
1424void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1425 CreateIntIntIntToIntLocations(arena_, invoke);
1426}
1427void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1428 CreateIntIntIntToIntLocations(arena_, invoke);
1429}
1430void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1431 CreateIntIntIntToIntLocations(arena_, invoke);
1432}
1433void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1434 CreateIntIntIntToIntLocations(arena_, invoke);
1435}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001436void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1437 CreateIntIntIntToIntLocations(arena_, invoke);
1438}
1439void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1440 CreateIntIntIntToIntLocations(arena_, invoke);
1441}
1442
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001443
1444void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001445 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001446}
1447void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001448 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001449}
1450void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001451 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001452}
1453void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001454 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001455}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001456void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1457 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1458}
1459void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1460 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1461}
1462
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001463
1464static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1465 Primitive::Type type,
1466 HInvoke* invoke) {
1467 LocationSummary* locations = new (arena) LocationSummary(invoke,
1468 LocationSummary::kNoCall,
1469 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001470 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001471 locations->SetInAt(1, Location::RequiresRegister());
1472 locations->SetInAt(2, Location::RequiresRegister());
1473 locations->SetInAt(3, Location::RequiresRegister());
1474 if (type == Primitive::kPrimNot) {
1475 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001476 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001477 locations->AddTemp(Location::RequiresRegister());
1478 }
1479}
1480
1481void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1482 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1483}
1484void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1485 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1486}
1487void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1488 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1489}
1490void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1491 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1492}
1493void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1494 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1495}
1496void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1497 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1498}
1499void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1500 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1501}
1502void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1503 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1504}
1505void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1506 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1507}
1508
1509// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1510// memory model.
1511static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1512 CodeGeneratorX86_64* codegen) {
1513 X86_64Assembler* assembler = reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1514 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1515 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1516 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1517
1518 if (type == Primitive::kPrimLong) {
1519 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001520 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1521 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1522 __ movl(temp, value);
1523 __ PoisonHeapReference(temp);
1524 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001525 } else {
1526 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1527 }
1528
1529 if (is_volatile) {
1530 __ mfence();
1531 }
1532
1533 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001534 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001535 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1536 locations->GetTemp(1).AsRegister<CpuRegister>(),
1537 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001538 value,
1539 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001540 }
1541}
1542
1543void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
1544 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1545}
1546void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1547 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1548}
1549void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1550 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1551}
1552void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1553 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1554}
1555void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1556 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1557}
1558void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1559 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1560}
1561void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1562 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1563}
1564void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1565 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1566}
1567void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1568 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1569}
1570
Mark Mendell58d25fd2015-04-03 14:52:31 -04001571static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1572 HInvoke* invoke) {
1573 LocationSummary* locations = new (arena) LocationSummary(invoke,
1574 LocationSummary::kNoCall,
1575 kIntrinsified);
1576 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1577 locations->SetInAt(1, Location::RequiresRegister());
1578 locations->SetInAt(2, Location::RequiresRegister());
1579 // expected value must be in EAX/RAX.
1580 locations->SetInAt(3, Location::RegisterLocation(RAX));
1581 locations->SetInAt(4, Location::RequiresRegister());
1582
1583 locations->SetOut(Location::RequiresRegister());
1584 if (type == Primitive::kPrimNot) {
1585 // Need temp registers for card-marking.
1586 locations->AddTemp(Location::RequiresRegister());
1587 locations->AddTemp(Location::RequiresRegister());
1588 }
1589}
1590
1591void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1592 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1593}
1594
1595void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1596 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1597}
1598
1599void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1600 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1601}
1602
1603static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1604 X86_64Assembler* assembler =
1605 reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1606 LocationSummary* locations = invoke->GetLocations();
1607
1608 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1609 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1610 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
1611 DCHECK_EQ(expected.AsRegister(), RAX);
1612 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1613 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1614
1615 if (type == Primitive::kPrimLong) {
1616 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
1617 } else {
1618 // Integer or object.
1619 if (type == Primitive::kPrimNot) {
1620 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001621 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001622 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1623 locations->GetTemp(1).AsRegister<CpuRegister>(),
1624 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001625 value,
1626 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001627
1628 if (kPoisonHeapReferences) {
1629 __ PoisonHeapReference(expected);
1630 __ PoisonHeapReference(value);
1631 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001632 }
1633
1634 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1635 }
1636
1637 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1638 // barriers at this time.
1639
1640 // Convert ZF into the boolean result.
1641 __ setcc(kZero, out);
1642 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01001643
1644 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1645 __ UnpoisonHeapReference(value);
1646 __ UnpoisonHeapReference(expected);
1647 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001648}
1649
1650void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1651 GenCAS(Primitive::kPrimInt, invoke, codegen_);
1652}
1653
1654void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1655 GenCAS(Primitive::kPrimLong, invoke, codegen_);
1656}
1657
1658void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1659 GenCAS(Primitive::kPrimNot, invoke, codegen_);
1660}
1661
1662void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
1663 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1664 LocationSummary::kNoCall,
1665 kIntrinsified);
1666 locations->SetInAt(0, Location::RequiresRegister());
1667 locations->SetOut(Location::SameAsFirstInput());
1668 locations->AddTemp(Location::RequiresRegister());
1669}
1670
1671static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
1672 X86_64Assembler* assembler) {
1673 Immediate imm_shift(shift);
1674 Immediate imm_mask(mask);
1675 __ movl(temp, reg);
1676 __ shrl(reg, imm_shift);
1677 __ andl(temp, imm_mask);
1678 __ andl(reg, imm_mask);
1679 __ shll(temp, imm_shift);
1680 __ orl(reg, temp);
1681}
1682
1683void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
1684 X86_64Assembler* assembler =
1685 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
1686 LocationSummary* locations = invoke->GetLocations();
1687
1688 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
1689 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1690
1691 /*
1692 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1693 * swapping bits to reverse bits in a number x. Using bswap to save instructions
1694 * compared to generic luni implementation which has 5 rounds of swapping bits.
1695 * x = bswap x
1696 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
1697 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
1698 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
1699 */
1700 __ bswapl(reg);
1701 SwapBits(reg, temp, 1, 0x55555555, assembler);
1702 SwapBits(reg, temp, 2, 0x33333333, assembler);
1703 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
1704}
1705
1706void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
1707 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1708 LocationSummary::kNoCall,
1709 kIntrinsified);
1710 locations->SetInAt(0, Location::RequiresRegister());
1711 locations->SetOut(Location::SameAsFirstInput());
1712 locations->AddTemp(Location::RequiresRegister());
1713 locations->AddTemp(Location::RequiresRegister());
1714}
1715
1716static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
1717 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
1718 Immediate imm_shift(shift);
1719 __ movq(temp_mask, Immediate(mask));
1720 __ movq(temp, reg);
1721 __ shrq(reg, imm_shift);
1722 __ andq(temp, temp_mask);
1723 __ andq(reg, temp_mask);
1724 __ shlq(temp, imm_shift);
1725 __ orq(reg, temp);
1726}
1727
1728void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
1729 X86_64Assembler* assembler =
1730 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
1731 LocationSummary* locations = invoke->GetLocations();
1732
1733 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
1734 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
1735 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
1736
1737 /*
1738 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
1739 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
1740 * compared to generic luni implementation which has 5 rounds of swapping bits.
1741 * x = bswap x
1742 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
1743 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
1744 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
1745 */
1746 __ bswapq(reg);
1747 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
1748 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
1749 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
1750}
1751
Mark Mendelld5897672015-08-12 21:16:41 -04001752static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
1753 LocationSummary* locations = new (arena) LocationSummary(invoke,
1754 LocationSummary::kNoCall,
1755 kIntrinsified);
1756 locations->SetInAt(0, Location::Any());
1757 locations->SetOut(Location::RequiresRegister());
1758}
1759
1760static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
1761 LocationSummary* locations = invoke->GetLocations();
1762 Location src = locations->InAt(0);
1763 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1764
1765 int zero_value_result = is_long ? 64 : 32;
1766 if (invoke->InputAt(0)->IsConstant()) {
1767 // Evaluate this at compile time.
1768 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
1769 if (value == 0) {
1770 value = zero_value_result;
1771 } else {
1772 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
1773 }
1774 if (value == 0) {
1775 __ xorl(out, out);
1776 } else {
1777 __ movl(out, Immediate(value));
1778 }
1779 return;
1780 }
1781
1782 // Handle the non-constant cases.
1783 if (src.IsRegister()) {
1784 if (is_long) {
1785 __ bsrq(out, src.AsRegister<CpuRegister>());
1786 } else {
1787 __ bsrl(out, src.AsRegister<CpuRegister>());
1788 }
1789 } else if (is_long) {
1790 DCHECK(src.IsDoubleStackSlot());
1791 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
1792 } else {
1793 DCHECK(src.IsStackSlot());
1794 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
1795 }
1796
1797 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001798 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04001799 __ j(kEqual, &is_zero);
1800
1801 // Correct the result from BSR to get the CLZ result.
1802 __ xorl(out, Immediate(zero_value_result - 1));
1803 __ jmp(&done);
1804
1805 // Fix the zero case with the expected result.
1806 __ Bind(&is_zero);
1807 __ movl(out, Immediate(zero_value_result));
1808
1809 __ Bind(&done);
1810}
1811
1812void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
1813 CreateLeadingZeroLocations(arena_, invoke);
1814}
1815
1816void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
1817 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1818 GenLeadingZeros(assembler, invoke, /* is_long */ false);
1819}
1820
1821void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
1822 CreateLeadingZeroLocations(arena_, invoke);
1823}
1824
1825void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
1826 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1827 GenLeadingZeros(assembler, invoke, /* is_long */ true);
1828}
1829
Mark Mendell2d554792015-09-15 21:45:18 -04001830static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
1831 LocationSummary* locations = new (arena) LocationSummary(invoke,
1832 LocationSummary::kNoCall,
1833 kIntrinsified);
1834 locations->SetInAt(0, Location::Any());
1835 locations->SetOut(Location::RequiresRegister());
1836}
1837
1838static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
1839 LocationSummary* locations = invoke->GetLocations();
1840 Location src = locations->InAt(0);
1841 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1842
1843 int zero_value_result = is_long ? 64 : 32;
1844 if (invoke->InputAt(0)->IsConstant()) {
1845 // Evaluate this at compile time.
1846 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
1847 if (value == 0) {
1848 value = zero_value_result;
1849 } else {
1850 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
1851 }
1852 if (value == 0) {
1853 __ xorl(out, out);
1854 } else {
1855 __ movl(out, Immediate(value));
1856 }
1857 return;
1858 }
1859
1860 // Handle the non-constant cases.
1861 if (src.IsRegister()) {
1862 if (is_long) {
1863 __ bsfq(out, src.AsRegister<CpuRegister>());
1864 } else {
1865 __ bsfl(out, src.AsRegister<CpuRegister>());
1866 }
1867 } else if (is_long) {
1868 DCHECK(src.IsDoubleStackSlot());
1869 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
1870 } else {
1871 DCHECK(src.IsStackSlot());
1872 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
1873 }
1874
1875 // BSF sets ZF if the input was zero, and the output is undefined.
1876 NearLabel done;
1877 __ j(kNotEqual, &done);
1878
1879 // Fix the zero case with the expected result.
1880 __ movl(out, Immediate(zero_value_result));
1881
1882 __ Bind(&done);
1883}
1884
1885void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
1886 CreateTrailingZeroLocations(arena_, invoke);
1887}
1888
1889void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
1890 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1891 GenTrailingZeros(assembler, invoke, /* is_long */ false);
1892}
1893
1894void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
1895 CreateTrailingZeroLocations(arena_, invoke);
1896}
1897
1898void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
1899 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1900 GenTrailingZeros(assembler, invoke, /* is_long */ true);
1901}
1902
1903static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) {
1904 LocationSummary* locations = new (arena) LocationSummary(invoke,
1905 LocationSummary::kNoCall,
1906 kIntrinsified);
1907 locations->SetInAt(0, Location::RequiresRegister());
1908 // The shift count needs to be in CL or a constant.
1909 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1)));
1910 locations->SetOut(Location::SameAsFirstInput());
1911}
1912
1913static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) {
1914 LocationSummary* locations = invoke->GetLocations();
1915 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
1916 Location second = locations->InAt(1);
1917
1918 if (is_long) {
1919 if (second.IsRegister()) {
1920 CpuRegister second_reg = second.AsRegister<CpuRegister>();
1921 if (is_left) {
1922 __ rolq(first_reg, second_reg);
1923 } else {
1924 __ rorq(first_reg, second_reg);
1925 }
1926 } else {
1927 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
1928 if (is_left) {
1929 __ rolq(first_reg, imm);
1930 } else {
1931 __ rorq(first_reg, imm);
1932 }
1933 }
1934 } else {
1935 if (second.IsRegister()) {
1936 CpuRegister second_reg = second.AsRegister<CpuRegister>();
1937 if (is_left) {
1938 __ roll(first_reg, second_reg);
1939 } else {
1940 __ rorl(first_reg, second_reg);
1941 }
1942 } else {
1943 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
1944 if (is_left) {
1945 __ roll(first_reg, imm);
1946 } else {
1947 __ rorl(first_reg, imm);
1948 }
1949 }
1950 }
1951}
1952
1953void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
1954 CreateRotateLocations(arena_, invoke);
1955}
1956
1957void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
1958 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1959 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true);
1960}
1961
1962void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
1963 CreateRotateLocations(arena_, invoke);
1964}
1965
1966void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
1967 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1968 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false);
1969}
1970
1971void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) {
1972 CreateRotateLocations(arena_, invoke);
1973}
1974
1975void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) {
1976 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1977 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true);
1978}
1979
1980void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) {
1981 CreateRotateLocations(arena_, invoke);
1982}
1983
1984void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) {
1985 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
1986 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false);
1987}
1988
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001989// Unimplemented intrinsics.
1990
1991#define UNIMPLEMENTED_INTRINSIC(Name) \
1992void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1993} \
1994void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
1995}
1996
Jeff Hao848f70a2014-01-15 13:49:50 -08001997UNIMPLEMENTED_INTRINSIC(StringGetCharsNoCheck)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001998UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
1999
Roland Levillain4d027112015-07-01 15:41:14 +01002000#undef UNIMPLEMENTED_INTRINSIC
2001
2002#undef __
2003
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002004} // namespace x86_64
2005} // namespace art