blob: e0d88a91d3b8ca533891112902c3d7bb7cc2c361 [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 Mendell40741f32015-04-20 22:10:34 -0400623 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400624 __ j(kAboveEqual, &done);
625
626 // if input == NaN goto nan
627 __ j(kUnordered, &nan);
628
629 // output = float-to-int-truncate(input)
630 __ cvttss2si(out, inPlusPointFive);
631 __ jmp(&done);
632 __ Bind(&nan);
633
634 // output = 0
635 __ xorl(out, out);
636 __ Bind(&done);
637}
638
639void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
640 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
641}
642
643void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
644 LocationSummary* locations = invoke->GetLocations();
645 if (locations->WillCall()) {
646 InvokeOutOfLineIntrinsic(codegen_, invoke);
647 return;
648 }
649
650 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
651 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
652 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400653 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400654 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400655 X86_64Assembler* assembler = GetAssembler();
656
Mark Mendell40741f32015-04-20 22:10:34 -0400657 // Load 0.5 into inPlusPointFive.
658 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400659
660 // Add in the input.
661 __ addsd(inPlusPointFive, in);
662
663 // And truncate to an integer.
664 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
665
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600666 // Load maxLong into out.
667 codegen_->Load64BitValue(out, kPrimLongMax);
668
Mark Mendellfb8d2792015-03-31 22:16:59 -0400669 // if inPlusPointFive >= maxLong goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400670 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400671 __ j(kAboveEqual, &done);
672
673 // if input == NaN goto nan
674 __ j(kUnordered, &nan);
675
676 // output = double-to-long-truncate(input)
677 __ cvttsd2si(out, inPlusPointFive, true);
678 __ jmp(&done);
679 __ Bind(&nan);
680
681 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400682 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400683 __ Bind(&done);
684}
685
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800686void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) {
687 // The inputs plus one temp.
688 LocationSummary* locations = new (arena_) LocationSummary(invoke,
689 LocationSummary::kCallOnSlowPath,
690 kIntrinsified);
691 locations->SetInAt(0, Location::RequiresRegister());
692 locations->SetInAt(1, Location::RequiresRegister());
693 locations->SetOut(Location::SameAsFirstInput());
694 locations->AddTemp(Location::RequiresRegister());
695}
696
697void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) {
698 LocationSummary* locations = invoke->GetLocations();
699
Mark Mendell6bc53a92015-07-01 14:26:52 -0400700 // Location of reference to data array.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800701 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400702 // Location of count.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800703 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800704
705 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
706 CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>();
707 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800708
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800709 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
710 // the cost.
711 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
712 // we will not optimize the code for constants (which would save a register).
713
Andreas Gampe85b62f22015-09-09 13:15:38 -0700714 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800715 codegen_->AddSlowPath(slow_path);
716
717 X86_64Assembler* assembler = GetAssembler();
718
719 __ cmpl(idx, Address(obj, count_offset));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800720 codegen_->MaybeRecordImplicitNullCheck(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800721 __ j(kAboveEqual, slow_path->GetEntryLabel());
722
Jeff Hao848f70a2014-01-15 13:49:50 -0800723 // out = out[2*idx].
724 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800725
726 __ Bind(slow_path->GetExitLabel());
727}
728
Mark Mendell6bc53a92015-07-01 14:26:52 -0400729void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
730 // Check to see if we have known failures that will cause us to have to bail out
731 // to the runtime, and just generate the runtime call directly.
732 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
733 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
734
735 // The positions must be non-negative.
736 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
737 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
738 // We will have to fail anyways.
739 return;
740 }
741
742 // The length must be > 0.
743 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
744 if (length != nullptr) {
745 int32_t len = length->GetValue();
746 if (len < 0) {
747 // Just call as normal.
748 return;
749 }
750 }
751
752 LocationSummary* locations = new (arena_) LocationSummary(invoke,
753 LocationSummary::kCallOnSlowPath,
754 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100755 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400756 locations->SetInAt(0, Location::RequiresRegister());
757 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
758 locations->SetInAt(2, Location::RequiresRegister());
759 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
760 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
761
762 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
763 locations->AddTemp(Location::RegisterLocation(RSI));
764 locations->AddTemp(Location::RegisterLocation(RDI));
765 locations->AddTemp(Location::RegisterLocation(RCX));
766}
767
768static void CheckPosition(X86_64Assembler* assembler,
769 Location pos,
770 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100771 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700772 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400773 CpuRegister input_len,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100774 CpuRegister temp,
775 bool length_is_input_length = false) {
776 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400777 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
778
779 if (pos.IsConstant()) {
780 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
781 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100782 if (!length_is_input_length) {
783 // Check that length(input) >= length.
784 if (length.IsConstant()) {
785 __ cmpl(Address(input, length_offset),
786 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
787 } else {
788 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
789 }
790 __ j(kLess, slow_path->GetEntryLabel());
791 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400792 } else {
793 // Check that length(input) >= pos.
794 __ movl(input_len, Address(input, length_offset));
795 __ cmpl(input_len, Immediate(pos_const));
796 __ j(kLess, slow_path->GetEntryLabel());
797
798 // Check that (length(input) - pos) >= length.
799 __ leal(temp, Address(input_len, -pos_const));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100800 if (length.IsConstant()) {
801 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
802 } else {
803 __ cmpl(temp, length.AsRegister<CpuRegister>());
804 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400805 __ j(kLess, slow_path->GetEntryLabel());
806 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100807 } else if (length_is_input_length) {
808 // The only way the copy can succeed is if pos is zero.
809 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
810 __ testl(pos_reg, pos_reg);
811 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400812 } else {
813 // Check that pos >= 0.
814 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
815 __ testl(pos_reg, pos_reg);
816 __ j(kLess, slow_path->GetEntryLabel());
817
818 // Check that pos <= length(input).
819 __ cmpl(Address(input, length_offset), pos_reg);
820 __ j(kLess, slow_path->GetEntryLabel());
821
822 // Check that (length(input) - pos) >= length.
823 __ movl(temp, Address(input, length_offset));
824 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100825 if (length.IsConstant()) {
826 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
827 } else {
828 __ cmpl(temp, length.AsRegister<CpuRegister>());
829 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400830 __ j(kLess, slow_path->GetEntryLabel());
831 }
832}
833
834void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
835 X86_64Assembler* assembler = GetAssembler();
836 LocationSummary* locations = invoke->GetLocations();
837
838 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100839 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400840 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100841 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400842 Location length = locations->InAt(4);
843
844 // Temporaries that we need for MOVSW.
845 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
846 DCHECK_EQ(src_base.AsRegister(), RSI);
847 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
848 DCHECK_EQ(dest_base.AsRegister(), RDI);
849 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
850 DCHECK_EQ(count.AsRegister(), RCX);
851
Andreas Gampe85b62f22015-09-09 13:15:38 -0700852 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400853 codegen_->AddSlowPath(slow_path);
854
855 // Bail out if the source and destination are the same.
856 __ cmpl(src, dest);
857 __ j(kEqual, slow_path->GetEntryLabel());
858
859 // Bail out if the source is null.
860 __ testl(src, src);
861 __ j(kEqual, slow_path->GetEntryLabel());
862
863 // Bail out if the destination is null.
864 __ testl(dest, dest);
865 __ j(kEqual, slow_path->GetEntryLabel());
866
867 // If the length is negative, bail out.
868 // We have already checked in the LocationsBuilder for the constant case.
869 if (!length.IsConstant()) {
870 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
871 __ j(kLess, slow_path->GetEntryLabel());
872 }
873
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100874 // Validity checks: source.
875 CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base);
876
877 // Validity checks: dest.
878 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base);
879
Mark Mendell6bc53a92015-07-01 14:26:52 -0400880 // We need the count in RCX.
881 if (length.IsConstant()) {
882 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
883 } else {
884 __ movl(count, length.AsRegister<CpuRegister>());
885 }
886
Mark Mendell6bc53a92015-07-01 14:26:52 -0400887 // Okay, everything checks out. Finally time to do the copy.
888 // Check assumption that sizeof(Char) is 2 (used in scaling below).
889 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
890 DCHECK_EQ(char_size, 2u);
891
892 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
893
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100894 if (src_pos.IsConstant()) {
895 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
896 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400897 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100898 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400899 ScaleFactor::TIMES_2, data_offset));
900 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100901 if (dest_pos.IsConstant()) {
902 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
903 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400904 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100905 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400906 ScaleFactor::TIMES_2, data_offset));
907 }
908
909 // Do the move.
910 __ rep_movsw();
911
912 __ Bind(slow_path->GetExitLabel());
913}
914
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100915
916void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100917 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100918}
919
920void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
921 X86_64Assembler* assembler = GetAssembler();
922 LocationSummary* locations = invoke->GetLocations();
923
924 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
925 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
926 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
927 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
928
929 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
930 Location src_pos = locations->InAt(1);
931 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
932 Location dest_pos = locations->InAt(3);
933 Location length = locations->InAt(4);
934 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
935 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
936 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
937
938 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
939 codegen_->AddSlowPath(slow_path);
940
941 NearLabel ok;
942 SystemArrayCopyOptimizations optimizations(invoke);
943
944 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100945 if (!src_pos.IsConstant() || !dest_pos.IsConstant()) {
946 __ cmpl(src, dest);
947 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100948 }
949
950 // If source and destination are the same, we go to slow path if we need to do
951 // forward copying.
952 if (src_pos.IsConstant()) {
953 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
954 if (dest_pos.IsConstant()) {
955 // Checked when building locations.
956 DCHECK(!optimizations.GetDestinationIsSource()
957 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
958 } else {
959 if (!optimizations.GetDestinationIsSource()) {
960 __ j(kNotEqual, &ok);
961 }
962 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
963 __ j(kGreater, slow_path->GetEntryLabel());
964 }
965 } else {
966 if (!optimizations.GetDestinationIsSource()) {
967 __ j(kNotEqual, &ok);
968 }
969 if (dest_pos.IsConstant()) {
970 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
971 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
972 __ j(kLess, slow_path->GetEntryLabel());
973 } else {
974 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
975 __ j(kLess, slow_path->GetEntryLabel());
976 }
977 }
978
979 __ Bind(&ok);
980
981 if (!optimizations.GetSourceIsNotNull()) {
982 // Bail out if the source is null.
983 __ testl(src, src);
984 __ j(kEqual, slow_path->GetEntryLabel());
985 }
986
987 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
988 // Bail out if the destination is null.
989 __ testl(dest, dest);
990 __ j(kEqual, slow_path->GetEntryLabel());
991 }
992
993 // If the length is negative, bail out.
994 // We have already checked in the LocationsBuilder for the constant case.
995 if (!length.IsConstant() &&
996 !optimizations.GetCountIsSourceLength() &&
997 !optimizations.GetCountIsDestinationLength()) {
998 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
999 __ j(kLess, slow_path->GetEntryLabel());
1000 }
1001
1002 // Validity checks: source.
1003 CheckPosition(assembler,
1004 src_pos,
1005 src,
1006 length,
1007 slow_path,
1008 temp1,
1009 temp2,
1010 optimizations.GetCountIsSourceLength());
1011
1012 // Validity checks: dest.
1013 CheckPosition(assembler,
1014 dest_pos,
1015 dest,
1016 length,
1017 slow_path,
1018 temp1,
1019 temp2,
1020 optimizations.GetCountIsDestinationLength());
1021
1022 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1023 // Check whether all elements of the source array are assignable to the component
1024 // type of the destination array. We do two checks: the classes are the same,
1025 // or the destination is Object[]. If none of these checks succeed, we go to the
1026 // slow path.
1027 __ movl(temp1, Address(dest, class_offset));
1028 __ movl(temp2, Address(src, class_offset));
1029 bool did_unpoison = false;
1030 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1031 !optimizations.GetSourceIsNonPrimitiveArray()) {
1032 // One or two of the references need to be unpoisoned. Unpoisoned them
1033 // both to make the identity check valid.
1034 __ MaybeUnpoisonHeapReference(temp1);
1035 __ MaybeUnpoisonHeapReference(temp2);
1036 did_unpoison = true;
1037 }
1038
1039 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1040 // Bail out if the destination is not a non primitive array.
1041 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1042 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1043 __ j(kEqual, slow_path->GetEntryLabel());
1044 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1045 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1046 __ j(kNotEqual, slow_path->GetEntryLabel());
1047 }
1048
1049 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1050 // Bail out if the source is not a non primitive array.
1051 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1052 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1053 __ j(kEqual, slow_path->GetEntryLabel());
1054 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1055 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1056 __ j(kNotEqual, slow_path->GetEntryLabel());
1057 }
1058
1059 __ cmpl(temp1, temp2);
1060
1061 if (optimizations.GetDestinationIsTypedObjectArray()) {
1062 NearLabel do_copy;
1063 __ j(kEqual, &do_copy);
1064 if (!did_unpoison) {
1065 __ MaybeUnpoisonHeapReference(temp1);
1066 }
1067 __ movl(temp1, Address(temp1, component_offset));
1068 __ MaybeUnpoisonHeapReference(temp1);
1069 __ movl(temp1, Address(temp1, super_offset));
1070 // No need to unpoison the result, we're comparing against null.
1071 __ testl(temp1, temp1);
1072 __ j(kNotEqual, slow_path->GetEntryLabel());
1073 __ Bind(&do_copy);
1074 } else {
1075 __ j(kNotEqual, slow_path->GetEntryLabel());
1076 }
1077 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1078 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1079 // Bail out if the source is not a non primitive array.
1080 __ movl(temp1, Address(src, class_offset));
1081 __ MaybeUnpoisonHeapReference(temp1);
1082 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1083 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1084 __ j(kEqual, slow_path->GetEntryLabel());
1085 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1086 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1087 __ j(kNotEqual, slow_path->GetEntryLabel());
1088 }
1089
1090 // Compute base source address, base destination address, and end source address.
1091
1092 uint32_t element_size = sizeof(int32_t);
1093 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1094 if (src_pos.IsConstant()) {
1095 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1096 __ leal(temp1, Address(src, element_size * constant + offset));
1097 } else {
1098 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1099 }
1100
1101 if (dest_pos.IsConstant()) {
1102 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1103 __ leal(temp2, Address(dest, element_size * constant + offset));
1104 } else {
1105 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1106 }
1107
1108 if (length.IsConstant()) {
1109 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1110 __ leal(temp3, Address(temp1, element_size * constant));
1111 } else {
1112 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1113 }
1114
1115 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1116 // poison/unpoison, nor do any read barrier as the next uses of the destination
1117 // array will do it.
1118 NearLabel loop, done;
1119 __ cmpl(temp1, temp3);
1120 __ j(kEqual, &done);
1121 __ Bind(&loop);
1122 __ movl(CpuRegister(TMP), Address(temp1, 0));
1123 __ movl(Address(temp2, 0), CpuRegister(TMP));
1124 __ addl(temp1, Immediate(element_size));
1125 __ addl(temp2, Immediate(element_size));
1126 __ cmpl(temp1, temp3);
1127 __ j(kNotEqual, &loop);
1128 __ Bind(&done);
1129
1130 // We only need one card marking on the destination array.
1131 codegen_->MarkGCCard(temp1,
1132 temp2,
1133 dest,
1134 CpuRegister(kNoRegister),
1135 false);
1136
1137 __ Bind(slow_path->GetExitLabel());
1138}
1139
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001140void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1141 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1142 LocationSummary::kCall,
1143 kIntrinsified);
1144 InvokeRuntimeCallingConvention calling_convention;
1145 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1146 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1147 locations->SetOut(Location::RegisterLocation(RAX));
1148}
1149
1150void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1151 X86_64Assembler* assembler = GetAssembler();
1152 LocationSummary* locations = invoke->GetLocations();
1153
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001154 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001155 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001156
1157 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1158 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001159 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001160 codegen_->AddSlowPath(slow_path);
1161 __ j(kEqual, slow_path->GetEntryLabel());
1162
1163 __ gs()->call(Address::Absolute(
1164 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true));
1165 __ Bind(slow_path->GetExitLabel());
1166}
1167
Agi Csakif8cfb202015-08-13 17:54:54 -07001168void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1169 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1170 LocationSummary::kNoCall,
1171 kIntrinsified);
1172 locations->SetInAt(0, Location::RequiresRegister());
1173 locations->SetInAt(1, Location::RequiresRegister());
1174
1175 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1176 locations->AddTemp(Location::RegisterLocation(RCX));
1177 locations->AddTemp(Location::RegisterLocation(RDI));
1178
1179 // Set output, RSI needed for repe_cmpsq instruction anyways.
1180 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1181}
1182
1183void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1184 X86_64Assembler* assembler = GetAssembler();
1185 LocationSummary* locations = invoke->GetLocations();
1186
1187 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1188 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1189 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1190 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1191 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1192
Mark Mendell0c9497d2015-08-21 09:30:05 -04001193 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001194
1195 // Get offsets of count, value, and class fields within a string object.
1196 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1197 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1198 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1199
1200 // Note that the null check must have been done earlier.
1201 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1202
1203 // Check if input is null, return false if it is.
1204 __ testl(arg, arg);
1205 __ j(kEqual, &return_false);
1206
1207 // Instanceof check for the argument by comparing class fields.
1208 // All string objects must have the same type since String cannot be subclassed.
1209 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1210 // If the argument is a string object, its class field must be equal to receiver's class field.
1211 __ movl(rcx, Address(str, class_offset));
1212 __ cmpl(rcx, Address(arg, class_offset));
1213 __ j(kNotEqual, &return_false);
1214
1215 // Reference equality check, return true if same reference.
1216 __ cmpl(str, arg);
1217 __ j(kEqual, &return_true);
1218
1219 // Load length of receiver string.
1220 __ movl(rcx, Address(str, count_offset));
1221 // Check if lengths are equal, return false if they're not.
1222 __ cmpl(rcx, Address(arg, count_offset));
1223 __ j(kNotEqual, &return_false);
1224 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001225 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001226
1227 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1228 __ leal(rsi, Address(str, value_offset));
1229 __ leal(rdi, Address(arg, value_offset));
1230
1231 // Divide string length by 4 and adjust for lengths not divisible by 4.
1232 __ addl(rcx, Immediate(3));
1233 __ shrl(rcx, Immediate(2));
1234
1235 // Assertions that must hold in order to compare strings 4 characters at a time.
1236 DCHECK_ALIGNED(value_offset, 8);
1237 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1238
1239 // Loop to compare strings four characters at a time starting at the beginning of the string.
1240 __ repe_cmpsq();
1241 // If strings are not equal, zero flag will be cleared.
1242 __ j(kNotEqual, &return_false);
1243
1244 // Return true and exit the function.
1245 // If loop does not result in returning false, we return true.
1246 __ Bind(&return_true);
1247 __ movl(rsi, Immediate(1));
1248 __ jmp(&end);
1249
1250 // Return false and exit the function.
1251 __ Bind(&return_false);
1252 __ xorl(rsi, rsi);
1253 __ Bind(&end);
1254}
1255
Andreas Gampe21030dd2015-05-07 14:46:15 -07001256static void CreateStringIndexOfLocations(HInvoke* invoke,
1257 ArenaAllocator* allocator,
1258 bool start_at_zero) {
1259 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1260 LocationSummary::kCallOnSlowPath,
1261 kIntrinsified);
1262 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1263 locations->SetInAt(0, Location::RegisterLocation(RDI));
1264 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1265 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1266 // of the instruction explicitly.
1267 // Note: This works as we don't clobber RAX anywhere.
1268 locations->SetInAt(1, Location::RegisterLocation(RAX));
1269 if (!start_at_zero) {
1270 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1271 }
1272 // As we clobber RDI during execution anyways, also use it as the output.
1273 locations->SetOut(Location::SameAsFirstInput());
1274
1275 // repne scasw uses RCX as the counter.
1276 locations->AddTemp(Location::RegisterLocation(RCX));
1277 // Need another temporary to be able to compute the result.
1278 locations->AddTemp(Location::RequiresRegister());
1279}
1280
1281static void GenerateStringIndexOf(HInvoke* invoke,
1282 X86_64Assembler* assembler,
1283 CodeGeneratorX86_64* codegen,
1284 ArenaAllocator* allocator,
1285 bool start_at_zero) {
1286 LocationSummary* locations = invoke->GetLocations();
1287
1288 // Note that the null check must have been done earlier.
1289 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1290
1291 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1292 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1293 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1294 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1295 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1296
1297 // Check our assumptions for registers.
1298 DCHECK_EQ(string_obj.AsRegister(), RDI);
1299 DCHECK_EQ(search_value.AsRegister(), RAX);
1300 DCHECK_EQ(counter.AsRegister(), RCX);
1301 DCHECK_EQ(out.AsRegister(), RDI);
1302
1303 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1304 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001305 SlowPathCode* slow_path = nullptr;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001306 if (invoke->InputAt(1)->IsIntConstant()) {
1307 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1308 std::numeric_limits<uint16_t>::max()) {
1309 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1310 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1311 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1312 codegen->AddSlowPath(slow_path);
1313 __ jmp(slow_path->GetEntryLabel());
1314 __ Bind(slow_path->GetExitLabel());
1315 return;
1316 }
1317 } else {
1318 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1319 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1320 codegen->AddSlowPath(slow_path);
1321 __ j(kAbove, slow_path->GetEntryLabel());
1322 }
1323
1324 // From here down, we know that we are looking for a char that fits in 16 bits.
1325 // Location of reference to data array within the String object.
1326 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1327 // Location of count within the String object.
1328 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1329
1330 // Load string length, i.e., the count field of the string.
1331 __ movl(string_length, Address(string_obj, count_offset));
1332
1333 // Do a length check.
1334 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001335 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001336 __ testl(string_length, string_length);
1337 __ j(kEqual, &not_found_label);
1338
1339 if (start_at_zero) {
1340 // Number of chars to scan is the same as the string length.
1341 __ movl(counter, string_length);
1342
1343 // Move to the start of the string.
1344 __ addq(string_obj, Immediate(value_offset));
1345 } else {
1346 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1347
1348 // Do a start_index check.
1349 __ cmpl(start_index, string_length);
1350 __ j(kGreaterEqual, &not_found_label);
1351
1352 // Ensure we have a start index >= 0;
1353 __ xorl(counter, counter);
1354 __ cmpl(start_index, Immediate(0));
1355 __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough.
1356
1357 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1358 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1359
1360 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1361 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1362 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1363 }
1364
1365 // Everything is set up for repne scasw:
1366 // * Comparison address in RDI.
1367 // * Counter in ECX.
1368 __ repne_scasw();
1369
1370 // Did we find a match?
1371 __ j(kNotEqual, &not_found_label);
1372
1373 // Yes, we matched. Compute the index of the result.
1374 __ subl(string_length, counter);
1375 __ leal(out, Address(string_length, -1));
1376
Mark Mendell0c9497d2015-08-21 09:30:05 -04001377 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001378 __ jmp(&done);
1379
1380 // Failed to match; return -1.
1381 __ Bind(&not_found_label);
1382 __ movl(out, Immediate(-1));
1383
1384 // And join up at the end.
1385 __ Bind(&done);
1386 if (slow_path != nullptr) {
1387 __ Bind(slow_path->GetExitLabel());
1388 }
1389}
1390
1391void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
1392 CreateStringIndexOfLocations(invoke, arena_, true);
1393}
1394
1395void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
1396 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1397}
1398
1399void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1400 CreateStringIndexOfLocations(invoke, arena_, false);
1401}
1402
1403void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1404 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1405}
1406
Jeff Hao848f70a2014-01-15 13:49:50 -08001407void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1408 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1409 LocationSummary::kCall,
1410 kIntrinsified);
1411 InvokeRuntimeCallingConvention calling_convention;
1412 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1413 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1414 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1415 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1416 locations->SetOut(Location::RegisterLocation(RAX));
1417}
1418
1419void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1420 X86_64Assembler* assembler = GetAssembler();
1421 LocationSummary* locations = invoke->GetLocations();
1422
1423 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1424 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001425 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001426 codegen_->AddSlowPath(slow_path);
1427 __ j(kEqual, slow_path->GetEntryLabel());
1428
1429 __ gs()->call(Address::Absolute(
1430 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true));
1431 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1432 __ Bind(slow_path->GetExitLabel());
1433}
1434
1435void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1436 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1437 LocationSummary::kCall,
1438 kIntrinsified);
1439 InvokeRuntimeCallingConvention calling_convention;
1440 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1441 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1442 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1443 locations->SetOut(Location::RegisterLocation(RAX));
1444}
1445
1446void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1447 X86_64Assembler* assembler = GetAssembler();
1448
1449 __ gs()->call(Address::Absolute(
1450 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true));
1451 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1452}
1453
1454void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1455 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1456 LocationSummary::kCall,
1457 kIntrinsified);
1458 InvokeRuntimeCallingConvention calling_convention;
1459 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1460 locations->SetOut(Location::RegisterLocation(RAX));
1461}
1462
1463void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1464 X86_64Assembler* assembler = GetAssembler();
1465 LocationSummary* locations = invoke->GetLocations();
1466
1467 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1468 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001469 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001470 codegen_->AddSlowPath(slow_path);
1471 __ j(kEqual, slow_path->GetEntryLabel());
1472
1473 __ gs()->call(Address::Absolute(
1474 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true));
1475 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1476 __ Bind(slow_path->GetExitLabel());
1477}
1478
Mark Mendell8f8926a2015-08-17 11:39:06 -04001479void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1480 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1481 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1482 LocationSummary::kNoCall,
1483 kIntrinsified);
1484 locations->SetInAt(0, Location::RequiresRegister());
1485 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1486 locations->SetInAt(2, Location::RequiresRegister());
1487 locations->SetInAt(3, Location::RequiresRegister());
1488 locations->SetInAt(4, Location::RequiresRegister());
1489
1490 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1491 locations->AddTemp(Location::RegisterLocation(RSI));
1492 locations->AddTemp(Location::RegisterLocation(RDI));
1493 locations->AddTemp(Location::RegisterLocation(RCX));
1494}
1495
1496void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1497 X86_64Assembler* assembler = GetAssembler();
1498 LocationSummary* locations = invoke->GetLocations();
1499
1500 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1501 // Location of data in char array buffer.
1502 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1503 // Location of char array data in string.
1504 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1505
1506 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1507 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1508 Location srcBegin = locations->InAt(1);
1509 int srcBegin_value =
1510 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1511 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1512 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1513 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1514
1515 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1516 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1517 DCHECK_EQ(char_size, 2u);
1518
1519 // Compute the address of the destination buffer.
1520 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1521
1522 // Compute the address of the source string.
1523 if (srcBegin.IsConstant()) {
1524 // Compute the address of the source string by adding the number of chars from
1525 // the source beginning to the value offset of a string.
1526 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1527 } else {
1528 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1529 ScaleFactor::TIMES_2, value_offset));
1530 }
1531
1532 // Compute the number of chars (words) to move.
1533 __ movl(CpuRegister(RCX), srcEnd);
1534 if (srcBegin.IsConstant()) {
1535 if (srcBegin_value != 0) {
1536 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1537 }
1538 } else {
1539 DCHECK(srcBegin.IsRegister());
1540 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1541 }
1542
1543 // Do the move.
1544 __ rep_movsw();
1545}
1546
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001547static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1548 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1549 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1550 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1551 // to avoid a SIGBUS.
1552 switch (size) {
1553 case Primitive::kPrimByte:
1554 __ movsxb(out, Address(address, 0));
1555 break;
1556 case Primitive::kPrimShort:
1557 __ movsxw(out, Address(address, 0));
1558 break;
1559 case Primitive::kPrimInt:
1560 __ movl(out, Address(address, 0));
1561 break;
1562 case Primitive::kPrimLong:
1563 __ movq(out, Address(address, 0));
1564 break;
1565 default:
1566 LOG(FATAL) << "Type not recognized for peek: " << size;
1567 UNREACHABLE();
1568 }
1569}
1570
1571void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1572 CreateIntToIntLocations(arena_, invoke);
1573}
1574
1575void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1576 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1577}
1578
1579void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1580 CreateIntToIntLocations(arena_, invoke);
1581}
1582
1583void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1584 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1585}
1586
1587void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1588 CreateIntToIntLocations(arena_, invoke);
1589}
1590
1591void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1592 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1593}
1594
1595void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1596 CreateIntToIntLocations(arena_, invoke);
1597}
1598
1599void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1600 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1601}
1602
1603static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1604 LocationSummary* locations = new (arena) LocationSummary(invoke,
1605 LocationSummary::kNoCall,
1606 kIntrinsified);
1607 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001608 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001609}
1610
1611static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1612 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001613 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001614 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1615 // to avoid a SIGBUS.
1616 switch (size) {
1617 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001618 if (value.IsConstant()) {
1619 __ movb(Address(address, 0),
1620 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1621 } else {
1622 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1623 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001624 break;
1625 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001626 if (value.IsConstant()) {
1627 __ movw(Address(address, 0),
1628 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1629 } else {
1630 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1631 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001632 break;
1633 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001634 if (value.IsConstant()) {
1635 __ movl(Address(address, 0),
1636 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1637 } else {
1638 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1639 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001640 break;
1641 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001642 if (value.IsConstant()) {
1643 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1644 DCHECK(IsInt<32>(v));
1645 int32_t v_32 = v;
1646 __ movq(Address(address, 0), Immediate(v_32));
1647 } else {
1648 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1649 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001650 break;
1651 default:
1652 LOG(FATAL) << "Type not recognized for poke: " << size;
1653 UNREACHABLE();
1654 }
1655}
1656
1657void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1658 CreateIntIntToVoidLocations(arena_, invoke);
1659}
1660
1661void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1662 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1663}
1664
1665void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1666 CreateIntIntToVoidLocations(arena_, invoke);
1667}
1668
1669void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1670 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1671}
1672
1673void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1674 CreateIntIntToVoidLocations(arena_, invoke);
1675}
1676
1677void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1678 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1679}
1680
1681void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1682 CreateIntIntToVoidLocations(arena_, invoke);
1683}
1684
1685void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1686 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1687}
1688
1689void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1690 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1691 LocationSummary::kNoCall,
1692 kIntrinsified);
1693 locations->SetOut(Location::RequiresRegister());
1694}
1695
1696void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1697 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
1698 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true));
1699}
1700
Andreas Gampe878d58c2015-01-15 23:24:00 -08001701static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001702 bool is_volatile ATTRIBUTE_UNUSED, X86_64Assembler* assembler) {
1703 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1704 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1705 CpuRegister trg = locations->Out().AsRegister<CpuRegister>();
1706
Andreas Gampe878d58c2015-01-15 23:24:00 -08001707 switch (type) {
1708 case Primitive::kPrimInt:
1709 case Primitive::kPrimNot:
1710 __ movl(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001711 if (type == Primitive::kPrimNot) {
1712 __ MaybeUnpoisonHeapReference(trg);
1713 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001714 break;
1715
1716 case Primitive::kPrimLong:
1717 __ movq(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1718 break;
1719
1720 default:
1721 LOG(FATAL) << "Unsupported op size " << type;
1722 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001723 }
1724}
1725
1726static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
1727 LocationSummary* locations = new (arena) LocationSummary(invoke,
1728 LocationSummary::kNoCall,
1729 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001730 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001731 locations->SetInAt(1, Location::RequiresRegister());
1732 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001733 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001734}
1735
1736void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1737 CreateIntIntIntToIntLocations(arena_, invoke);
1738}
1739void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1740 CreateIntIntIntToIntLocations(arena_, invoke);
1741}
1742void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1743 CreateIntIntIntToIntLocations(arena_, invoke);
1744}
1745void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1746 CreateIntIntIntToIntLocations(arena_, invoke);
1747}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001748void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1749 CreateIntIntIntToIntLocations(arena_, invoke);
1750}
1751void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1752 CreateIntIntIntToIntLocations(arena_, invoke);
1753}
1754
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001755
1756void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001757 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001758}
1759void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001760 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001761}
1762void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001763 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001764}
1765void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001766 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001767}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001768void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1769 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1770}
1771void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1772 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1773}
1774
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001775
1776static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1777 Primitive::Type type,
1778 HInvoke* invoke) {
1779 LocationSummary* locations = new (arena) LocationSummary(invoke,
1780 LocationSummary::kNoCall,
1781 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001782 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001783 locations->SetInAt(1, Location::RequiresRegister());
1784 locations->SetInAt(2, Location::RequiresRegister());
1785 locations->SetInAt(3, Location::RequiresRegister());
1786 if (type == Primitive::kPrimNot) {
1787 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001788 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001789 locations->AddTemp(Location::RequiresRegister());
1790 }
1791}
1792
1793void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1794 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1795}
1796void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1797 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1798}
1799void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1800 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1801}
1802void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1803 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1804}
1805void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1806 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1807}
1808void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1809 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1810}
1811void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1812 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1813}
1814void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1815 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1816}
1817void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1818 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1819}
1820
1821// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1822// memory model.
1823static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1824 CodeGeneratorX86_64* codegen) {
1825 X86_64Assembler* assembler = reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1826 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1827 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1828 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1829
1830 if (type == Primitive::kPrimLong) {
1831 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001832 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1833 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1834 __ movl(temp, value);
1835 __ PoisonHeapReference(temp);
1836 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001837 } else {
1838 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1839 }
1840
1841 if (is_volatile) {
1842 __ mfence();
1843 }
1844
1845 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001846 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001847 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1848 locations->GetTemp(1).AsRegister<CpuRegister>(),
1849 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001850 value,
1851 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001852 }
1853}
1854
1855void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
1856 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1857}
1858void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1859 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1860}
1861void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1862 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1863}
1864void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1865 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1866}
1867void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1868 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1869}
1870void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1871 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1872}
1873void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1874 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1875}
1876void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1877 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1878}
1879void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1880 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1881}
1882
Mark Mendell58d25fd2015-04-03 14:52:31 -04001883static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1884 HInvoke* invoke) {
1885 LocationSummary* locations = new (arena) LocationSummary(invoke,
1886 LocationSummary::kNoCall,
1887 kIntrinsified);
1888 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1889 locations->SetInAt(1, Location::RequiresRegister());
1890 locations->SetInAt(2, Location::RequiresRegister());
1891 // expected value must be in EAX/RAX.
1892 locations->SetInAt(3, Location::RegisterLocation(RAX));
1893 locations->SetInAt(4, Location::RequiresRegister());
1894
1895 locations->SetOut(Location::RequiresRegister());
1896 if (type == Primitive::kPrimNot) {
1897 // Need temp registers for card-marking.
1898 locations->AddTemp(Location::RequiresRegister());
1899 locations->AddTemp(Location::RequiresRegister());
1900 }
1901}
1902
1903void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1904 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1905}
1906
1907void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1908 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1909}
1910
1911void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1912 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1913}
1914
1915static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1916 X86_64Assembler* assembler =
1917 reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1918 LocationSummary* locations = invoke->GetLocations();
1919
1920 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1921 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1922 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
1923 DCHECK_EQ(expected.AsRegister(), RAX);
1924 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1925 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1926
1927 if (type == Primitive::kPrimLong) {
1928 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
1929 } else {
1930 // Integer or object.
1931 if (type == Primitive::kPrimNot) {
1932 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001933 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001934 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1935 locations->GetTemp(1).AsRegister<CpuRegister>(),
1936 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001937 value,
1938 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001939
1940 if (kPoisonHeapReferences) {
1941 __ PoisonHeapReference(expected);
1942 __ PoisonHeapReference(value);
1943 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001944 }
1945
1946 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1947 }
1948
1949 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1950 // barriers at this time.
1951
1952 // Convert ZF into the boolean result.
1953 __ setcc(kZero, out);
1954 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01001955
1956 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1957 __ UnpoisonHeapReference(value);
1958 __ UnpoisonHeapReference(expected);
1959 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001960}
1961
1962void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1963 GenCAS(Primitive::kPrimInt, invoke, codegen_);
1964}
1965
1966void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1967 GenCAS(Primitive::kPrimLong, invoke, codegen_);
1968}
1969
1970void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1971 GenCAS(Primitive::kPrimNot, invoke, codegen_);
1972}
1973
1974void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
1975 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1976 LocationSummary::kNoCall,
1977 kIntrinsified);
1978 locations->SetInAt(0, Location::RequiresRegister());
1979 locations->SetOut(Location::SameAsFirstInput());
1980 locations->AddTemp(Location::RequiresRegister());
1981}
1982
1983static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
1984 X86_64Assembler* assembler) {
1985 Immediate imm_shift(shift);
1986 Immediate imm_mask(mask);
1987 __ movl(temp, reg);
1988 __ shrl(reg, imm_shift);
1989 __ andl(temp, imm_mask);
1990 __ andl(reg, imm_mask);
1991 __ shll(temp, imm_shift);
1992 __ orl(reg, temp);
1993}
1994
1995void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
1996 X86_64Assembler* assembler =
1997 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
1998 LocationSummary* locations = invoke->GetLocations();
1999
2000 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2001 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2002
2003 /*
2004 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2005 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2006 * compared to generic luni implementation which has 5 rounds of swapping bits.
2007 * x = bswap x
2008 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2009 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2010 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2011 */
2012 __ bswapl(reg);
2013 SwapBits(reg, temp, 1, 0x55555555, assembler);
2014 SwapBits(reg, temp, 2, 0x33333333, assembler);
2015 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2016}
2017
2018void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2019 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2020 LocationSummary::kNoCall,
2021 kIntrinsified);
2022 locations->SetInAt(0, Location::RequiresRegister());
2023 locations->SetOut(Location::SameAsFirstInput());
2024 locations->AddTemp(Location::RequiresRegister());
2025 locations->AddTemp(Location::RequiresRegister());
2026}
2027
2028static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2029 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2030 Immediate imm_shift(shift);
2031 __ movq(temp_mask, Immediate(mask));
2032 __ movq(temp, reg);
2033 __ shrq(reg, imm_shift);
2034 __ andq(temp, temp_mask);
2035 __ andq(reg, temp_mask);
2036 __ shlq(temp, imm_shift);
2037 __ orq(reg, temp);
2038}
2039
2040void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
2041 X86_64Assembler* assembler =
2042 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
2043 LocationSummary* locations = invoke->GetLocations();
2044
2045 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2046 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2047 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2048
2049 /*
2050 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2051 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2052 * compared to generic luni implementation which has 5 rounds of swapping bits.
2053 * x = bswap x
2054 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2055 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2056 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2057 */
2058 __ bswapq(reg);
2059 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2060 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2061 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2062}
2063
Mark Mendelld5897672015-08-12 21:16:41 -04002064static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2065 LocationSummary* locations = new (arena) LocationSummary(invoke,
2066 LocationSummary::kNoCall,
2067 kIntrinsified);
2068 locations->SetInAt(0, Location::Any());
2069 locations->SetOut(Location::RequiresRegister());
2070}
2071
2072static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2073 LocationSummary* locations = invoke->GetLocations();
2074 Location src = locations->InAt(0);
2075 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2076
2077 int zero_value_result = is_long ? 64 : 32;
2078 if (invoke->InputAt(0)->IsConstant()) {
2079 // Evaluate this at compile time.
2080 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2081 if (value == 0) {
2082 value = zero_value_result;
2083 } else {
2084 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2085 }
2086 if (value == 0) {
2087 __ xorl(out, out);
2088 } else {
2089 __ movl(out, Immediate(value));
2090 }
2091 return;
2092 }
2093
2094 // Handle the non-constant cases.
2095 if (src.IsRegister()) {
2096 if (is_long) {
2097 __ bsrq(out, src.AsRegister<CpuRegister>());
2098 } else {
2099 __ bsrl(out, src.AsRegister<CpuRegister>());
2100 }
2101 } else if (is_long) {
2102 DCHECK(src.IsDoubleStackSlot());
2103 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2104 } else {
2105 DCHECK(src.IsStackSlot());
2106 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2107 }
2108
2109 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002110 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002111 __ j(kEqual, &is_zero);
2112
2113 // Correct the result from BSR to get the CLZ result.
2114 __ xorl(out, Immediate(zero_value_result - 1));
2115 __ jmp(&done);
2116
2117 // Fix the zero case with the expected result.
2118 __ Bind(&is_zero);
2119 __ movl(out, Immediate(zero_value_result));
2120
2121 __ Bind(&done);
2122}
2123
2124void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2125 CreateLeadingZeroLocations(arena_, invoke);
2126}
2127
2128void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2129 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2130 GenLeadingZeros(assembler, invoke, /* is_long */ false);
2131}
2132
2133void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2134 CreateLeadingZeroLocations(arena_, invoke);
2135}
2136
2137void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2138 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2139 GenLeadingZeros(assembler, invoke, /* is_long */ true);
2140}
2141
Mark Mendell2d554792015-09-15 21:45:18 -04002142static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2143 LocationSummary* locations = new (arena) LocationSummary(invoke,
2144 LocationSummary::kNoCall,
2145 kIntrinsified);
2146 locations->SetInAt(0, Location::Any());
2147 locations->SetOut(Location::RequiresRegister());
2148}
2149
2150static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2151 LocationSummary* locations = invoke->GetLocations();
2152 Location src = locations->InAt(0);
2153 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2154
2155 int zero_value_result = is_long ? 64 : 32;
2156 if (invoke->InputAt(0)->IsConstant()) {
2157 // Evaluate this at compile time.
2158 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2159 if (value == 0) {
2160 value = zero_value_result;
2161 } else {
2162 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2163 }
2164 if (value == 0) {
2165 __ xorl(out, out);
2166 } else {
2167 __ movl(out, Immediate(value));
2168 }
2169 return;
2170 }
2171
2172 // Handle the non-constant cases.
2173 if (src.IsRegister()) {
2174 if (is_long) {
2175 __ bsfq(out, src.AsRegister<CpuRegister>());
2176 } else {
2177 __ bsfl(out, src.AsRegister<CpuRegister>());
2178 }
2179 } else if (is_long) {
2180 DCHECK(src.IsDoubleStackSlot());
2181 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2182 } else {
2183 DCHECK(src.IsStackSlot());
2184 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2185 }
2186
2187 // BSF sets ZF if the input was zero, and the output is undefined.
2188 NearLabel done;
2189 __ j(kNotEqual, &done);
2190
2191 // Fix the zero case with the expected result.
2192 __ movl(out, Immediate(zero_value_result));
2193
2194 __ Bind(&done);
2195}
2196
2197void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2198 CreateTrailingZeroLocations(arena_, invoke);
2199}
2200
2201void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2202 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2203 GenTrailingZeros(assembler, invoke, /* is_long */ false);
2204}
2205
2206void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2207 CreateTrailingZeroLocations(arena_, invoke);
2208}
2209
2210void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2211 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2212 GenTrailingZeros(assembler, invoke, /* is_long */ true);
2213}
2214
2215static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) {
2216 LocationSummary* locations = new (arena) LocationSummary(invoke,
2217 LocationSummary::kNoCall,
2218 kIntrinsified);
2219 locations->SetInAt(0, Location::RequiresRegister());
2220 // The shift count needs to be in CL or a constant.
2221 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1)));
2222 locations->SetOut(Location::SameAsFirstInput());
2223}
2224
2225static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) {
2226 LocationSummary* locations = invoke->GetLocations();
2227 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
2228 Location second = locations->InAt(1);
2229
2230 if (is_long) {
2231 if (second.IsRegister()) {
2232 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2233 if (is_left) {
2234 __ rolq(first_reg, second_reg);
2235 } else {
2236 __ rorq(first_reg, second_reg);
2237 }
2238 } else {
2239 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
2240 if (is_left) {
2241 __ rolq(first_reg, imm);
2242 } else {
2243 __ rorq(first_reg, imm);
2244 }
2245 }
2246 } else {
2247 if (second.IsRegister()) {
2248 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2249 if (is_left) {
2250 __ roll(first_reg, second_reg);
2251 } else {
2252 __ rorl(first_reg, second_reg);
2253 }
2254 } else {
2255 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2256 if (is_left) {
2257 __ roll(first_reg, imm);
2258 } else {
2259 __ rorl(first_reg, imm);
2260 }
2261 }
2262 }
2263}
2264
2265void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2266 CreateRotateLocations(arena_, invoke);
2267}
2268
2269void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2270 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2271 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true);
2272}
2273
2274void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2275 CreateRotateLocations(arena_, invoke);
2276}
2277
2278void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2279 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2280 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false);
2281}
2282
2283void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2284 CreateRotateLocations(arena_, invoke);
2285}
2286
2287void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2288 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2289 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true);
2290}
2291
2292void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) {
2293 CreateRotateLocations(arena_, invoke);
2294}
2295
2296void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) {
2297 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2298 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false);
2299}
2300
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002301// Unimplemented intrinsics.
2302
2303#define UNIMPLEMENTED_INTRINSIC(Name) \
2304void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2305} \
2306void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2307}
2308
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002309UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
2310
Roland Levillain4d027112015-07-01 15:41:14 +01002311#undef UNIMPLEMENTED_INTRINSIC
2312
2313#undef __
2314
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002315} // namespace x86_64
2316} // namespace art