blob: 1061aae84c9889c2a10e7df3efe3fbfce788ed9a [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) {
917 // Check to see if we have known failures that will cause us to have to bail out
918 // to the runtime, and just generate the runtime call directly.
919 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
920 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
921
922 // The positions must be non-negative.
923 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
924 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
925 // We will have to fail anyways.
926 return;
927 }
928
929 // The length must be > 0.
930 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
931 if (length != nullptr) {
932 int32_t len = length->GetValue();
933 if (len < 0) {
934 // Just call as normal.
935 return;
936 }
937 }
938
939 SystemArrayCopyOptimizations optimizations(invoke);
940
941 if (optimizations.GetDestinationIsSource()) {
942 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
943 // We only support backward copying if source and destination are the same.
944 return;
945 }
946 }
947
948 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
949 // We currently don't intrinsify primitive copying.
950 return;
951 }
952
953 LocationSummary* locations = new (arena_) LocationSummary(invoke,
954 LocationSummary::kCallOnSlowPath,
955 kIntrinsified);
956 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
957 locations->SetInAt(0, Location::RequiresRegister());
958 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
959 locations->SetInAt(2, Location::RequiresRegister());
960 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
961 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
962
963 locations->AddTemp(Location::RequiresRegister());
964 locations->AddTemp(Location::RequiresRegister());
965 locations->AddTemp(Location::RequiresRegister());
966}
967
968void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
969 X86_64Assembler* assembler = GetAssembler();
970 LocationSummary* locations = invoke->GetLocations();
971
972 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
973 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
974 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
975 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
976
977 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
978 Location src_pos = locations->InAt(1);
979 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
980 Location dest_pos = locations->InAt(3);
981 Location length = locations->InAt(4);
982 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
983 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
984 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
985
986 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
987 codegen_->AddSlowPath(slow_path);
988
989 NearLabel ok;
990 SystemArrayCopyOptimizations optimizations(invoke);
991
992 if (!optimizations.GetDestinationIsSource()) {
993 __ cmpl(src, dest);
994 }
995
996 // If source and destination are the same, we go to slow path if we need to do
997 // forward copying.
998 if (src_pos.IsConstant()) {
999 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1000 if (dest_pos.IsConstant()) {
1001 // Checked when building locations.
1002 DCHECK(!optimizations.GetDestinationIsSource()
1003 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
1004 } else {
1005 if (!optimizations.GetDestinationIsSource()) {
1006 __ j(kNotEqual, &ok);
1007 }
1008 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
1009 __ j(kGreater, slow_path->GetEntryLabel());
1010 }
1011 } else {
1012 if (!optimizations.GetDestinationIsSource()) {
1013 __ j(kNotEqual, &ok);
1014 }
1015 if (dest_pos.IsConstant()) {
1016 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1017 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
1018 __ j(kLess, slow_path->GetEntryLabel());
1019 } else {
1020 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
1021 __ j(kLess, slow_path->GetEntryLabel());
1022 }
1023 }
1024
1025 __ Bind(&ok);
1026
1027 if (!optimizations.GetSourceIsNotNull()) {
1028 // Bail out if the source is null.
1029 __ testl(src, src);
1030 __ j(kEqual, slow_path->GetEntryLabel());
1031 }
1032
1033 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1034 // Bail out if the destination is null.
1035 __ testl(dest, dest);
1036 __ j(kEqual, slow_path->GetEntryLabel());
1037 }
1038
1039 // If the length is negative, bail out.
1040 // We have already checked in the LocationsBuilder for the constant case.
1041 if (!length.IsConstant() &&
1042 !optimizations.GetCountIsSourceLength() &&
1043 !optimizations.GetCountIsDestinationLength()) {
1044 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1045 __ j(kLess, slow_path->GetEntryLabel());
1046 }
1047
1048 // Validity checks: source.
1049 CheckPosition(assembler,
1050 src_pos,
1051 src,
1052 length,
1053 slow_path,
1054 temp1,
1055 temp2,
1056 optimizations.GetCountIsSourceLength());
1057
1058 // Validity checks: dest.
1059 CheckPosition(assembler,
1060 dest_pos,
1061 dest,
1062 length,
1063 slow_path,
1064 temp1,
1065 temp2,
1066 optimizations.GetCountIsDestinationLength());
1067
1068 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1069 // Check whether all elements of the source array are assignable to the component
1070 // type of the destination array. We do two checks: the classes are the same,
1071 // or the destination is Object[]. If none of these checks succeed, we go to the
1072 // slow path.
1073 __ movl(temp1, Address(dest, class_offset));
1074 __ movl(temp2, Address(src, class_offset));
1075 bool did_unpoison = false;
1076 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1077 !optimizations.GetSourceIsNonPrimitiveArray()) {
1078 // One or two of the references need to be unpoisoned. Unpoisoned them
1079 // both to make the identity check valid.
1080 __ MaybeUnpoisonHeapReference(temp1);
1081 __ MaybeUnpoisonHeapReference(temp2);
1082 did_unpoison = true;
1083 }
1084
1085 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1086 // Bail out if the destination is not a non primitive array.
1087 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1088 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1089 __ j(kEqual, slow_path->GetEntryLabel());
1090 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1091 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1092 __ j(kNotEqual, slow_path->GetEntryLabel());
1093 }
1094
1095 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1096 // Bail out if the source is not a non primitive array.
1097 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1098 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1099 __ j(kEqual, slow_path->GetEntryLabel());
1100 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1101 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1102 __ j(kNotEqual, slow_path->GetEntryLabel());
1103 }
1104
1105 __ cmpl(temp1, temp2);
1106
1107 if (optimizations.GetDestinationIsTypedObjectArray()) {
1108 NearLabel do_copy;
1109 __ j(kEqual, &do_copy);
1110 if (!did_unpoison) {
1111 __ MaybeUnpoisonHeapReference(temp1);
1112 }
1113 __ movl(temp1, Address(temp1, component_offset));
1114 __ MaybeUnpoisonHeapReference(temp1);
1115 __ movl(temp1, Address(temp1, super_offset));
1116 // No need to unpoison the result, we're comparing against null.
1117 __ testl(temp1, temp1);
1118 __ j(kNotEqual, slow_path->GetEntryLabel());
1119 __ Bind(&do_copy);
1120 } else {
1121 __ j(kNotEqual, slow_path->GetEntryLabel());
1122 }
1123 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1124 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1125 // Bail out if the source is not a non primitive array.
1126 __ movl(temp1, Address(src, class_offset));
1127 __ MaybeUnpoisonHeapReference(temp1);
1128 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1129 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1130 __ j(kEqual, slow_path->GetEntryLabel());
1131 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1132 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1133 __ j(kNotEqual, slow_path->GetEntryLabel());
1134 }
1135
1136 // Compute base source address, base destination address, and end source address.
1137
1138 uint32_t element_size = sizeof(int32_t);
1139 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1140 if (src_pos.IsConstant()) {
1141 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1142 __ leal(temp1, Address(src, element_size * constant + offset));
1143 } else {
1144 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1145 }
1146
1147 if (dest_pos.IsConstant()) {
1148 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1149 __ leal(temp2, Address(dest, element_size * constant + offset));
1150 } else {
1151 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1152 }
1153
1154 if (length.IsConstant()) {
1155 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1156 __ leal(temp3, Address(temp1, element_size * constant));
1157 } else {
1158 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1159 }
1160
1161 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1162 // poison/unpoison, nor do any read barrier as the next uses of the destination
1163 // array will do it.
1164 NearLabel loop, done;
1165 __ cmpl(temp1, temp3);
1166 __ j(kEqual, &done);
1167 __ Bind(&loop);
1168 __ movl(CpuRegister(TMP), Address(temp1, 0));
1169 __ movl(Address(temp2, 0), CpuRegister(TMP));
1170 __ addl(temp1, Immediate(element_size));
1171 __ addl(temp2, Immediate(element_size));
1172 __ cmpl(temp1, temp3);
1173 __ j(kNotEqual, &loop);
1174 __ Bind(&done);
1175
1176 // We only need one card marking on the destination array.
1177 codegen_->MarkGCCard(temp1,
1178 temp2,
1179 dest,
1180 CpuRegister(kNoRegister),
1181 false);
1182
1183 __ Bind(slow_path->GetExitLabel());
1184}
1185
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001186void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1187 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1188 LocationSummary::kCall,
1189 kIntrinsified);
1190 InvokeRuntimeCallingConvention calling_convention;
1191 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1192 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1193 locations->SetOut(Location::RegisterLocation(RAX));
1194}
1195
1196void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1197 X86_64Assembler* assembler = GetAssembler();
1198 LocationSummary* locations = invoke->GetLocations();
1199
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001200 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001201 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001202
1203 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1204 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001205 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001206 codegen_->AddSlowPath(slow_path);
1207 __ j(kEqual, slow_path->GetEntryLabel());
1208
1209 __ gs()->call(Address::Absolute(
1210 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true));
1211 __ Bind(slow_path->GetExitLabel());
1212}
1213
Agi Csakif8cfb202015-08-13 17:54:54 -07001214void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1215 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1216 LocationSummary::kNoCall,
1217 kIntrinsified);
1218 locations->SetInAt(0, Location::RequiresRegister());
1219 locations->SetInAt(1, Location::RequiresRegister());
1220
1221 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1222 locations->AddTemp(Location::RegisterLocation(RCX));
1223 locations->AddTemp(Location::RegisterLocation(RDI));
1224
1225 // Set output, RSI needed for repe_cmpsq instruction anyways.
1226 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1227}
1228
1229void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1230 X86_64Assembler* assembler = GetAssembler();
1231 LocationSummary* locations = invoke->GetLocations();
1232
1233 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1234 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1235 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1236 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1237 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1238
Mark Mendell0c9497d2015-08-21 09:30:05 -04001239 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001240
1241 // Get offsets of count, value, and class fields within a string object.
1242 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1243 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1244 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1245
1246 // Note that the null check must have been done earlier.
1247 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1248
1249 // Check if input is null, return false if it is.
1250 __ testl(arg, arg);
1251 __ j(kEqual, &return_false);
1252
1253 // Instanceof check for the argument by comparing class fields.
1254 // All string objects must have the same type since String cannot be subclassed.
1255 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1256 // If the argument is a string object, its class field must be equal to receiver's class field.
1257 __ movl(rcx, Address(str, class_offset));
1258 __ cmpl(rcx, Address(arg, class_offset));
1259 __ j(kNotEqual, &return_false);
1260
1261 // Reference equality check, return true if same reference.
1262 __ cmpl(str, arg);
1263 __ j(kEqual, &return_true);
1264
1265 // Load length of receiver string.
1266 __ movl(rcx, Address(str, count_offset));
1267 // Check if lengths are equal, return false if they're not.
1268 __ cmpl(rcx, Address(arg, count_offset));
1269 __ j(kNotEqual, &return_false);
1270 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001271 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001272
1273 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1274 __ leal(rsi, Address(str, value_offset));
1275 __ leal(rdi, Address(arg, value_offset));
1276
1277 // Divide string length by 4 and adjust for lengths not divisible by 4.
1278 __ addl(rcx, Immediate(3));
1279 __ shrl(rcx, Immediate(2));
1280
1281 // Assertions that must hold in order to compare strings 4 characters at a time.
1282 DCHECK_ALIGNED(value_offset, 8);
1283 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1284
1285 // Loop to compare strings four characters at a time starting at the beginning of the string.
1286 __ repe_cmpsq();
1287 // If strings are not equal, zero flag will be cleared.
1288 __ j(kNotEqual, &return_false);
1289
1290 // Return true and exit the function.
1291 // If loop does not result in returning false, we return true.
1292 __ Bind(&return_true);
1293 __ movl(rsi, Immediate(1));
1294 __ jmp(&end);
1295
1296 // Return false and exit the function.
1297 __ Bind(&return_false);
1298 __ xorl(rsi, rsi);
1299 __ Bind(&end);
1300}
1301
Andreas Gampe21030dd2015-05-07 14:46:15 -07001302static void CreateStringIndexOfLocations(HInvoke* invoke,
1303 ArenaAllocator* allocator,
1304 bool start_at_zero) {
1305 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1306 LocationSummary::kCallOnSlowPath,
1307 kIntrinsified);
1308 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1309 locations->SetInAt(0, Location::RegisterLocation(RDI));
1310 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1311 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1312 // of the instruction explicitly.
1313 // Note: This works as we don't clobber RAX anywhere.
1314 locations->SetInAt(1, Location::RegisterLocation(RAX));
1315 if (!start_at_zero) {
1316 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1317 }
1318 // As we clobber RDI during execution anyways, also use it as the output.
1319 locations->SetOut(Location::SameAsFirstInput());
1320
1321 // repne scasw uses RCX as the counter.
1322 locations->AddTemp(Location::RegisterLocation(RCX));
1323 // Need another temporary to be able to compute the result.
1324 locations->AddTemp(Location::RequiresRegister());
1325}
1326
1327static void GenerateStringIndexOf(HInvoke* invoke,
1328 X86_64Assembler* assembler,
1329 CodeGeneratorX86_64* codegen,
1330 ArenaAllocator* allocator,
1331 bool start_at_zero) {
1332 LocationSummary* locations = invoke->GetLocations();
1333
1334 // Note that the null check must have been done earlier.
1335 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1336
1337 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1338 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1339 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1340 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1341 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1342
1343 // Check our assumptions for registers.
1344 DCHECK_EQ(string_obj.AsRegister(), RDI);
1345 DCHECK_EQ(search_value.AsRegister(), RAX);
1346 DCHECK_EQ(counter.AsRegister(), RCX);
1347 DCHECK_EQ(out.AsRegister(), RDI);
1348
1349 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1350 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001351 SlowPathCode* slow_path = nullptr;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001352 if (invoke->InputAt(1)->IsIntConstant()) {
1353 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1354 std::numeric_limits<uint16_t>::max()) {
1355 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1356 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1357 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1358 codegen->AddSlowPath(slow_path);
1359 __ jmp(slow_path->GetEntryLabel());
1360 __ Bind(slow_path->GetExitLabel());
1361 return;
1362 }
1363 } else {
1364 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1365 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1366 codegen->AddSlowPath(slow_path);
1367 __ j(kAbove, slow_path->GetEntryLabel());
1368 }
1369
1370 // From here down, we know that we are looking for a char that fits in 16 bits.
1371 // Location of reference to data array within the String object.
1372 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1373 // Location of count within the String object.
1374 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1375
1376 // Load string length, i.e., the count field of the string.
1377 __ movl(string_length, Address(string_obj, count_offset));
1378
1379 // Do a length check.
1380 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001381 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001382 __ testl(string_length, string_length);
1383 __ j(kEqual, &not_found_label);
1384
1385 if (start_at_zero) {
1386 // Number of chars to scan is the same as the string length.
1387 __ movl(counter, string_length);
1388
1389 // Move to the start of the string.
1390 __ addq(string_obj, Immediate(value_offset));
1391 } else {
1392 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1393
1394 // Do a start_index check.
1395 __ cmpl(start_index, string_length);
1396 __ j(kGreaterEqual, &not_found_label);
1397
1398 // Ensure we have a start index >= 0;
1399 __ xorl(counter, counter);
1400 __ cmpl(start_index, Immediate(0));
1401 __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough.
1402
1403 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1404 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1405
1406 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1407 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1408 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1409 }
1410
1411 // Everything is set up for repne scasw:
1412 // * Comparison address in RDI.
1413 // * Counter in ECX.
1414 __ repne_scasw();
1415
1416 // Did we find a match?
1417 __ j(kNotEqual, &not_found_label);
1418
1419 // Yes, we matched. Compute the index of the result.
1420 __ subl(string_length, counter);
1421 __ leal(out, Address(string_length, -1));
1422
Mark Mendell0c9497d2015-08-21 09:30:05 -04001423 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001424 __ jmp(&done);
1425
1426 // Failed to match; return -1.
1427 __ Bind(&not_found_label);
1428 __ movl(out, Immediate(-1));
1429
1430 // And join up at the end.
1431 __ Bind(&done);
1432 if (slow_path != nullptr) {
1433 __ Bind(slow_path->GetExitLabel());
1434 }
1435}
1436
1437void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
1438 CreateStringIndexOfLocations(invoke, arena_, true);
1439}
1440
1441void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
1442 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1443}
1444
1445void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1446 CreateStringIndexOfLocations(invoke, arena_, false);
1447}
1448
1449void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1450 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1451}
1452
Jeff Hao848f70a2014-01-15 13:49:50 -08001453void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1454 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1455 LocationSummary::kCall,
1456 kIntrinsified);
1457 InvokeRuntimeCallingConvention calling_convention;
1458 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1459 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1460 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1461 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1462 locations->SetOut(Location::RegisterLocation(RAX));
1463}
1464
1465void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1466 X86_64Assembler* assembler = GetAssembler();
1467 LocationSummary* locations = invoke->GetLocations();
1468
1469 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1470 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001471 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001472 codegen_->AddSlowPath(slow_path);
1473 __ j(kEqual, slow_path->GetEntryLabel());
1474
1475 __ gs()->call(Address::Absolute(
1476 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true));
1477 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1478 __ Bind(slow_path->GetExitLabel());
1479}
1480
1481void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1482 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1483 LocationSummary::kCall,
1484 kIntrinsified);
1485 InvokeRuntimeCallingConvention calling_convention;
1486 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1487 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1488 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1489 locations->SetOut(Location::RegisterLocation(RAX));
1490}
1491
1492void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1493 X86_64Assembler* assembler = GetAssembler();
1494
1495 __ gs()->call(Address::Absolute(
1496 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true));
1497 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1498}
1499
1500void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1501 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1502 LocationSummary::kCall,
1503 kIntrinsified);
1504 InvokeRuntimeCallingConvention calling_convention;
1505 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1506 locations->SetOut(Location::RegisterLocation(RAX));
1507}
1508
1509void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1510 X86_64Assembler* assembler = GetAssembler();
1511 LocationSummary* locations = invoke->GetLocations();
1512
1513 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1514 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001515 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001516 codegen_->AddSlowPath(slow_path);
1517 __ j(kEqual, slow_path->GetEntryLabel());
1518
1519 __ gs()->call(Address::Absolute(
1520 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true));
1521 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1522 __ Bind(slow_path->GetExitLabel());
1523}
1524
Mark Mendell8f8926a2015-08-17 11:39:06 -04001525void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1526 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1527 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1528 LocationSummary::kNoCall,
1529 kIntrinsified);
1530 locations->SetInAt(0, Location::RequiresRegister());
1531 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1532 locations->SetInAt(2, Location::RequiresRegister());
1533 locations->SetInAt(3, Location::RequiresRegister());
1534 locations->SetInAt(4, Location::RequiresRegister());
1535
1536 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1537 locations->AddTemp(Location::RegisterLocation(RSI));
1538 locations->AddTemp(Location::RegisterLocation(RDI));
1539 locations->AddTemp(Location::RegisterLocation(RCX));
1540}
1541
1542void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1543 X86_64Assembler* assembler = GetAssembler();
1544 LocationSummary* locations = invoke->GetLocations();
1545
1546 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1547 // Location of data in char array buffer.
1548 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1549 // Location of char array data in string.
1550 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1551
1552 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1553 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1554 Location srcBegin = locations->InAt(1);
1555 int srcBegin_value =
1556 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1557 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1558 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1559 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1560
1561 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1562 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1563 DCHECK_EQ(char_size, 2u);
1564
1565 // Compute the address of the destination buffer.
1566 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1567
1568 // Compute the address of the source string.
1569 if (srcBegin.IsConstant()) {
1570 // Compute the address of the source string by adding the number of chars from
1571 // the source beginning to the value offset of a string.
1572 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1573 } else {
1574 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1575 ScaleFactor::TIMES_2, value_offset));
1576 }
1577
1578 // Compute the number of chars (words) to move.
1579 __ movl(CpuRegister(RCX), srcEnd);
1580 if (srcBegin.IsConstant()) {
1581 if (srcBegin_value != 0) {
1582 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1583 }
1584 } else {
1585 DCHECK(srcBegin.IsRegister());
1586 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1587 }
1588
1589 // Do the move.
1590 __ rep_movsw();
1591}
1592
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001593static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1594 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1595 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1596 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1597 // to avoid a SIGBUS.
1598 switch (size) {
1599 case Primitive::kPrimByte:
1600 __ movsxb(out, Address(address, 0));
1601 break;
1602 case Primitive::kPrimShort:
1603 __ movsxw(out, Address(address, 0));
1604 break;
1605 case Primitive::kPrimInt:
1606 __ movl(out, Address(address, 0));
1607 break;
1608 case Primitive::kPrimLong:
1609 __ movq(out, Address(address, 0));
1610 break;
1611 default:
1612 LOG(FATAL) << "Type not recognized for peek: " << size;
1613 UNREACHABLE();
1614 }
1615}
1616
1617void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1618 CreateIntToIntLocations(arena_, invoke);
1619}
1620
1621void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1622 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1623}
1624
1625void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1626 CreateIntToIntLocations(arena_, invoke);
1627}
1628
1629void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1630 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1631}
1632
1633void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1634 CreateIntToIntLocations(arena_, invoke);
1635}
1636
1637void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1638 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1639}
1640
1641void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1642 CreateIntToIntLocations(arena_, invoke);
1643}
1644
1645void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1646 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1647}
1648
1649static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1650 LocationSummary* locations = new (arena) LocationSummary(invoke,
1651 LocationSummary::kNoCall,
1652 kIntrinsified);
1653 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001654 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001655}
1656
1657static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1658 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001659 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001660 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1661 // to avoid a SIGBUS.
1662 switch (size) {
1663 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001664 if (value.IsConstant()) {
1665 __ movb(Address(address, 0),
1666 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1667 } else {
1668 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1669 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001670 break;
1671 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001672 if (value.IsConstant()) {
1673 __ movw(Address(address, 0),
1674 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1675 } else {
1676 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1677 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001678 break;
1679 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001680 if (value.IsConstant()) {
1681 __ movl(Address(address, 0),
1682 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1683 } else {
1684 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1685 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001686 break;
1687 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001688 if (value.IsConstant()) {
1689 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1690 DCHECK(IsInt<32>(v));
1691 int32_t v_32 = v;
1692 __ movq(Address(address, 0), Immediate(v_32));
1693 } else {
1694 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1695 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001696 break;
1697 default:
1698 LOG(FATAL) << "Type not recognized for poke: " << size;
1699 UNREACHABLE();
1700 }
1701}
1702
1703void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1704 CreateIntIntToVoidLocations(arena_, invoke);
1705}
1706
1707void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1708 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1709}
1710
1711void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1712 CreateIntIntToVoidLocations(arena_, invoke);
1713}
1714
1715void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1716 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1717}
1718
1719void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1720 CreateIntIntToVoidLocations(arena_, invoke);
1721}
1722
1723void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1724 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1725}
1726
1727void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1728 CreateIntIntToVoidLocations(arena_, invoke);
1729}
1730
1731void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1732 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1733}
1734
1735void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1736 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1737 LocationSummary::kNoCall,
1738 kIntrinsified);
1739 locations->SetOut(Location::RequiresRegister());
1740}
1741
1742void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1743 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
1744 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true));
1745}
1746
Andreas Gampe878d58c2015-01-15 23:24:00 -08001747static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001748 bool is_volatile ATTRIBUTE_UNUSED, X86_64Assembler* assembler) {
1749 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1750 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1751 CpuRegister trg = locations->Out().AsRegister<CpuRegister>();
1752
Andreas Gampe878d58c2015-01-15 23:24:00 -08001753 switch (type) {
1754 case Primitive::kPrimInt:
1755 case Primitive::kPrimNot:
1756 __ movl(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001757 if (type == Primitive::kPrimNot) {
1758 __ MaybeUnpoisonHeapReference(trg);
1759 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001760 break;
1761
1762 case Primitive::kPrimLong:
1763 __ movq(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1764 break;
1765
1766 default:
1767 LOG(FATAL) << "Unsupported op size " << type;
1768 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001769 }
1770}
1771
1772static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
1773 LocationSummary* locations = new (arena) LocationSummary(invoke,
1774 LocationSummary::kNoCall,
1775 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001776 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001777 locations->SetInAt(1, Location::RequiresRegister());
1778 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001779 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001780}
1781
1782void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1783 CreateIntIntIntToIntLocations(arena_, invoke);
1784}
1785void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1786 CreateIntIntIntToIntLocations(arena_, invoke);
1787}
1788void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1789 CreateIntIntIntToIntLocations(arena_, invoke);
1790}
1791void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1792 CreateIntIntIntToIntLocations(arena_, invoke);
1793}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001794void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1795 CreateIntIntIntToIntLocations(arena_, invoke);
1796}
1797void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1798 CreateIntIntIntToIntLocations(arena_, invoke);
1799}
1800
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001801
1802void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001803 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001804}
1805void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001806 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001807}
1808void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001809 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001810}
1811void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001812 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001813}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001814void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1815 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1816}
1817void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1818 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1819}
1820
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001821
1822static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1823 Primitive::Type type,
1824 HInvoke* invoke) {
1825 LocationSummary* locations = new (arena) LocationSummary(invoke,
1826 LocationSummary::kNoCall,
1827 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001828 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001829 locations->SetInAt(1, Location::RequiresRegister());
1830 locations->SetInAt(2, Location::RequiresRegister());
1831 locations->SetInAt(3, Location::RequiresRegister());
1832 if (type == Primitive::kPrimNot) {
1833 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001834 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001835 locations->AddTemp(Location::RequiresRegister());
1836 }
1837}
1838
1839void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1840 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1841}
1842void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1843 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1844}
1845void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1846 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1847}
1848void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1849 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1850}
1851void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1852 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1853}
1854void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1855 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1856}
1857void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1858 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1859}
1860void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1861 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1862}
1863void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1864 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1865}
1866
1867// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1868// memory model.
1869static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1870 CodeGeneratorX86_64* codegen) {
1871 X86_64Assembler* assembler = reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1872 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1873 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1874 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1875
1876 if (type == Primitive::kPrimLong) {
1877 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001878 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1879 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1880 __ movl(temp, value);
1881 __ PoisonHeapReference(temp);
1882 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001883 } else {
1884 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1885 }
1886
1887 if (is_volatile) {
1888 __ mfence();
1889 }
1890
1891 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001892 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001893 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1894 locations->GetTemp(1).AsRegister<CpuRegister>(),
1895 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001896 value,
1897 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001898 }
1899}
1900
1901void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
1902 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1903}
1904void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1905 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1906}
1907void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1908 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1909}
1910void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1911 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1912}
1913void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1914 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1915}
1916void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1917 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1918}
1919void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1920 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1921}
1922void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1923 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1924}
1925void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1926 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1927}
1928
Mark Mendell58d25fd2015-04-03 14:52:31 -04001929static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1930 HInvoke* invoke) {
1931 LocationSummary* locations = new (arena) LocationSummary(invoke,
1932 LocationSummary::kNoCall,
1933 kIntrinsified);
1934 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1935 locations->SetInAt(1, Location::RequiresRegister());
1936 locations->SetInAt(2, Location::RequiresRegister());
1937 // expected value must be in EAX/RAX.
1938 locations->SetInAt(3, Location::RegisterLocation(RAX));
1939 locations->SetInAt(4, Location::RequiresRegister());
1940
1941 locations->SetOut(Location::RequiresRegister());
1942 if (type == Primitive::kPrimNot) {
1943 // Need temp registers for card-marking.
1944 locations->AddTemp(Location::RequiresRegister());
1945 locations->AddTemp(Location::RequiresRegister());
1946 }
1947}
1948
1949void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1950 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1951}
1952
1953void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1954 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1955}
1956
1957void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1958 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1959}
1960
1961static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1962 X86_64Assembler* assembler =
1963 reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1964 LocationSummary* locations = invoke->GetLocations();
1965
1966 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1967 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1968 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
1969 DCHECK_EQ(expected.AsRegister(), RAX);
1970 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1971 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1972
1973 if (type == Primitive::kPrimLong) {
1974 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
1975 } else {
1976 // Integer or object.
1977 if (type == Primitive::kPrimNot) {
1978 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001979 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001980 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1981 locations->GetTemp(1).AsRegister<CpuRegister>(),
1982 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001983 value,
1984 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001985
1986 if (kPoisonHeapReferences) {
1987 __ PoisonHeapReference(expected);
1988 __ PoisonHeapReference(value);
1989 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001990 }
1991
1992 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1993 }
1994
1995 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1996 // barriers at this time.
1997
1998 // Convert ZF into the boolean result.
1999 __ setcc(kZero, out);
2000 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002001
2002 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2003 __ UnpoisonHeapReference(value);
2004 __ UnpoisonHeapReference(expected);
2005 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002006}
2007
2008void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2009 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2010}
2011
2012void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2013 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2014}
2015
2016void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
2017 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2018}
2019
2020void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2021 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2022 LocationSummary::kNoCall,
2023 kIntrinsified);
2024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetOut(Location::SameAsFirstInput());
2026 locations->AddTemp(Location::RequiresRegister());
2027}
2028
2029static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2030 X86_64Assembler* assembler) {
2031 Immediate imm_shift(shift);
2032 Immediate imm_mask(mask);
2033 __ movl(temp, reg);
2034 __ shrl(reg, imm_shift);
2035 __ andl(temp, imm_mask);
2036 __ andl(reg, imm_mask);
2037 __ shll(temp, imm_shift);
2038 __ orl(reg, temp);
2039}
2040
2041void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
2042 X86_64Assembler* assembler =
2043 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
2044 LocationSummary* locations = invoke->GetLocations();
2045
2046 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2047 CpuRegister temp = locations->GetTemp(0).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 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 & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2055 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2056 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2057 */
2058 __ bswapl(reg);
2059 SwapBits(reg, temp, 1, 0x55555555, assembler);
2060 SwapBits(reg, temp, 2, 0x33333333, assembler);
2061 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2062}
2063
2064void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2065 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2066 LocationSummary::kNoCall,
2067 kIntrinsified);
2068 locations->SetInAt(0, Location::RequiresRegister());
2069 locations->SetOut(Location::SameAsFirstInput());
2070 locations->AddTemp(Location::RequiresRegister());
2071 locations->AddTemp(Location::RequiresRegister());
2072}
2073
2074static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2075 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2076 Immediate imm_shift(shift);
2077 __ movq(temp_mask, Immediate(mask));
2078 __ movq(temp, reg);
2079 __ shrq(reg, imm_shift);
2080 __ andq(temp, temp_mask);
2081 __ andq(reg, temp_mask);
2082 __ shlq(temp, imm_shift);
2083 __ orq(reg, temp);
2084}
2085
2086void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
2087 X86_64Assembler* assembler =
2088 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
2089 LocationSummary* locations = invoke->GetLocations();
2090
2091 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2092 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2093 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2094
2095 /*
2096 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2097 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2098 * compared to generic luni implementation which has 5 rounds of swapping bits.
2099 * x = bswap x
2100 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2101 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2102 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2103 */
2104 __ bswapq(reg);
2105 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2106 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2107 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2108}
2109
Mark Mendelld5897672015-08-12 21:16:41 -04002110static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2111 LocationSummary* locations = new (arena) LocationSummary(invoke,
2112 LocationSummary::kNoCall,
2113 kIntrinsified);
2114 locations->SetInAt(0, Location::Any());
2115 locations->SetOut(Location::RequiresRegister());
2116}
2117
2118static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2119 LocationSummary* locations = invoke->GetLocations();
2120 Location src = locations->InAt(0);
2121 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2122
2123 int zero_value_result = is_long ? 64 : 32;
2124 if (invoke->InputAt(0)->IsConstant()) {
2125 // Evaluate this at compile time.
2126 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2127 if (value == 0) {
2128 value = zero_value_result;
2129 } else {
2130 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2131 }
2132 if (value == 0) {
2133 __ xorl(out, out);
2134 } else {
2135 __ movl(out, Immediate(value));
2136 }
2137 return;
2138 }
2139
2140 // Handle the non-constant cases.
2141 if (src.IsRegister()) {
2142 if (is_long) {
2143 __ bsrq(out, src.AsRegister<CpuRegister>());
2144 } else {
2145 __ bsrl(out, src.AsRegister<CpuRegister>());
2146 }
2147 } else if (is_long) {
2148 DCHECK(src.IsDoubleStackSlot());
2149 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2150 } else {
2151 DCHECK(src.IsStackSlot());
2152 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2153 }
2154
2155 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002156 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002157 __ j(kEqual, &is_zero);
2158
2159 // Correct the result from BSR to get the CLZ result.
2160 __ xorl(out, Immediate(zero_value_result - 1));
2161 __ jmp(&done);
2162
2163 // Fix the zero case with the expected result.
2164 __ Bind(&is_zero);
2165 __ movl(out, Immediate(zero_value_result));
2166
2167 __ Bind(&done);
2168}
2169
2170void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2171 CreateLeadingZeroLocations(arena_, invoke);
2172}
2173
2174void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2175 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2176 GenLeadingZeros(assembler, invoke, /* is_long */ false);
2177}
2178
2179void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2180 CreateLeadingZeroLocations(arena_, invoke);
2181}
2182
2183void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2184 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2185 GenLeadingZeros(assembler, invoke, /* is_long */ true);
2186}
2187
Mark Mendell2d554792015-09-15 21:45:18 -04002188static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2189 LocationSummary* locations = new (arena) LocationSummary(invoke,
2190 LocationSummary::kNoCall,
2191 kIntrinsified);
2192 locations->SetInAt(0, Location::Any());
2193 locations->SetOut(Location::RequiresRegister());
2194}
2195
2196static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2197 LocationSummary* locations = invoke->GetLocations();
2198 Location src = locations->InAt(0);
2199 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2200
2201 int zero_value_result = is_long ? 64 : 32;
2202 if (invoke->InputAt(0)->IsConstant()) {
2203 // Evaluate this at compile time.
2204 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2205 if (value == 0) {
2206 value = zero_value_result;
2207 } else {
2208 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2209 }
2210 if (value == 0) {
2211 __ xorl(out, out);
2212 } else {
2213 __ movl(out, Immediate(value));
2214 }
2215 return;
2216 }
2217
2218 // Handle the non-constant cases.
2219 if (src.IsRegister()) {
2220 if (is_long) {
2221 __ bsfq(out, src.AsRegister<CpuRegister>());
2222 } else {
2223 __ bsfl(out, src.AsRegister<CpuRegister>());
2224 }
2225 } else if (is_long) {
2226 DCHECK(src.IsDoubleStackSlot());
2227 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2228 } else {
2229 DCHECK(src.IsStackSlot());
2230 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2231 }
2232
2233 // BSF sets ZF if the input was zero, and the output is undefined.
2234 NearLabel done;
2235 __ j(kNotEqual, &done);
2236
2237 // Fix the zero case with the expected result.
2238 __ movl(out, Immediate(zero_value_result));
2239
2240 __ Bind(&done);
2241}
2242
2243void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2244 CreateTrailingZeroLocations(arena_, invoke);
2245}
2246
2247void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2248 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2249 GenTrailingZeros(assembler, invoke, /* is_long */ false);
2250}
2251
2252void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2253 CreateTrailingZeroLocations(arena_, invoke);
2254}
2255
2256void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2257 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2258 GenTrailingZeros(assembler, invoke, /* is_long */ true);
2259}
2260
2261static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) {
2262 LocationSummary* locations = new (arena) LocationSummary(invoke,
2263 LocationSummary::kNoCall,
2264 kIntrinsified);
2265 locations->SetInAt(0, Location::RequiresRegister());
2266 // The shift count needs to be in CL or a constant.
2267 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1)));
2268 locations->SetOut(Location::SameAsFirstInput());
2269}
2270
2271static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) {
2272 LocationSummary* locations = invoke->GetLocations();
2273 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
2274 Location second = locations->InAt(1);
2275
2276 if (is_long) {
2277 if (second.IsRegister()) {
2278 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2279 if (is_left) {
2280 __ rolq(first_reg, second_reg);
2281 } else {
2282 __ rorq(first_reg, second_reg);
2283 }
2284 } else {
2285 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
2286 if (is_left) {
2287 __ rolq(first_reg, imm);
2288 } else {
2289 __ rorq(first_reg, imm);
2290 }
2291 }
2292 } else {
2293 if (second.IsRegister()) {
2294 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2295 if (is_left) {
2296 __ roll(first_reg, second_reg);
2297 } else {
2298 __ rorl(first_reg, second_reg);
2299 }
2300 } else {
2301 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2302 if (is_left) {
2303 __ roll(first_reg, imm);
2304 } else {
2305 __ rorl(first_reg, imm);
2306 }
2307 }
2308 }
2309}
2310
2311void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2312 CreateRotateLocations(arena_, invoke);
2313}
2314
2315void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2316 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2317 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true);
2318}
2319
2320void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2321 CreateRotateLocations(arena_, invoke);
2322}
2323
2324void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2325 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2326 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false);
2327}
2328
2329void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2330 CreateRotateLocations(arena_, invoke);
2331}
2332
2333void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2334 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2335 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true);
2336}
2337
2338void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) {
2339 CreateRotateLocations(arena_, invoke);
2340}
2341
2342void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) {
2343 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2344 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false);
2345}
2346
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002347// Unimplemented intrinsics.
2348
2349#define UNIMPLEMENTED_INTRINSIC(Name) \
2350void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2351} \
2352void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2353}
2354
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002355UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
2356
Roland Levillain4d027112015-07-01 15:41:14 +01002357#undef UNIMPLEMENTED_INTRINSIC
2358
2359#undef __
2360
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002361} // namespace x86_64
2362} // namespace art