blob: fa1ff23086c25d3b2c17ee3c8cbc4e8e16632180 [file] [log] [blame]
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_x86_64.h"
18
Andreas Gampe21030dd2015-05-07 14:46:15 -070019#include <limits>
20
Mark Mendellfb8d2792015-03-31 22:16:59 -040021#include "arch/x86_64/instruction_set_features_x86_64.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Mark Mendelld5897672015-08-12 21:16:41 -040023#include "base/bit_utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080024#include "code_generator_x86_64.h"
25#include "entrypoints/quick/quick_entrypoints.h"
26#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070027#include "intrinsics_utils.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080028#include "mirror/array-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080029#include "mirror/string.h"
30#include "thread.h"
31#include "utils/x86_64/assembler_x86_64.h"
32#include "utils/x86_64/constants_x86_64.h"
33
34namespace art {
35
36namespace x86_64 {
37
Mark Mendellfb8d2792015-03-31 22:16:59 -040038IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
39 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
40}
41
42
Andreas Gampe71fb52f2014-12-29 17:43:08 -080043X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
44 return reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
45}
46
Andreas Gampe878d58c2015-01-15 23:24:00 -080047ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080048 return codegen_->GetGraph()->GetArena();
49}
50
51bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
52 Dispatch(invoke);
53 const LocationSummary* res = invoke->GetLocations();
54 return res != nullptr && res->Intrinsified();
55}
56
Roland Levillainec525fc2015-04-28 15:50:20 +010057static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010058 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010059 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080060}
61
Andreas Gampe85b62f22015-09-09 13:15:38 -070062using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080063
Andreas Gampe71fb52f2014-12-29 17:43:08 -080064#define __ assembler->
65
66static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
67 LocationSummary* locations = new (arena) LocationSummary(invoke,
68 LocationSummary::kNoCall,
69 kIntrinsified);
70 locations->SetInAt(0, Location::RequiresFpuRegister());
71 locations->SetOut(Location::RequiresRegister());
72}
73
74static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
75 LocationSummary* locations = new (arena) LocationSummary(invoke,
76 LocationSummary::kNoCall,
77 kIntrinsified);
78 locations->SetInAt(0, Location::RequiresRegister());
79 locations->SetOut(Location::RequiresFpuRegister());
80}
81
82static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
83 Location input = locations->InAt(0);
84 Location output = locations->Out();
85 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
86}
87
88static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
89 Location input = locations->InAt(0);
90 Location output = locations->Out();
91 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
92}
93
94void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
95 CreateFPToIntLocations(arena_, invoke);
96}
97void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
98 CreateIntToFPLocations(arena_, invoke);
99}
100
101void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
102 MoveFPToInt(invoke->GetLocations(), true, GetAssembler());
103}
104void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
105 MoveIntToFP(invoke->GetLocations(), true, GetAssembler());
106}
107
108void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
109 CreateFPToIntLocations(arena_, invoke);
110}
111void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
112 CreateIntToFPLocations(arena_, invoke);
113}
114
115void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
116 MoveFPToInt(invoke->GetLocations(), false, GetAssembler());
117}
118void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
119 MoveIntToFP(invoke->GetLocations(), false, GetAssembler());
120}
121
122static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
123 LocationSummary* locations = new (arena) LocationSummary(invoke,
124 LocationSummary::kNoCall,
125 kIntrinsified);
126 locations->SetInAt(0, Location::RequiresRegister());
127 locations->SetOut(Location::SameAsFirstInput());
128}
129
130static void GenReverseBytes(LocationSummary* locations,
131 Primitive::Type size,
132 X86_64Assembler* assembler) {
133 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
134
135 switch (size) {
136 case Primitive::kPrimShort:
137 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
138 __ bswapl(out);
139 __ sarl(out, Immediate(16));
140 break;
141 case Primitive::kPrimInt:
142 __ bswapl(out);
143 break;
144 case Primitive::kPrimLong:
145 __ bswapq(out);
146 break;
147 default:
148 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
149 UNREACHABLE();
150 }
151}
152
153void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
154 CreateIntToIntLocations(arena_, invoke);
155}
156
157void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
158 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
159}
160
161void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
162 CreateIntToIntLocations(arena_, invoke);
163}
164
165void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
166 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
167}
168
169void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
170 CreateIntToIntLocations(arena_, invoke);
171}
172
173void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
174 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
175}
176
177
178// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
179// need is 64b.
180
181static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
182 // TODO: Enable memory operations when the assembler supports them.
183 LocationSummary* locations = new (arena) LocationSummary(invoke,
184 LocationSummary::kNoCall,
185 kIntrinsified);
186 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800187 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400188 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800189}
190
Mark Mendell39dcf552015-04-09 20:42:42 -0400191static void MathAbsFP(LocationSummary* locations,
192 bool is64bit,
193 X86_64Assembler* assembler,
194 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800195 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800196
Mark Mendellcfa410b2015-05-25 16:02:44 -0400197 DCHECK(output.IsFpuRegister());
198 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800199
Mark Mendellcfa410b2015-05-25 16:02:44 -0400200 // TODO: Can mask directly with constant area using pand if we can guarantee
201 // that the literal is aligned on a 16 byte boundary. This will avoid a
202 // temporary.
203 if (is64bit) {
204 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
205 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800206 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400207 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
208 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800209 }
210}
211
212void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
213 CreateFloatToFloatPlusTemps(arena_, invoke);
214}
215
216void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400217 MathAbsFP(invoke->GetLocations(), true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800218}
219
220void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
221 CreateFloatToFloatPlusTemps(arena_, invoke);
222}
223
224void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400225 MathAbsFP(invoke->GetLocations(), false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800226}
227
228static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
229 LocationSummary* locations = new (arena) LocationSummary(invoke,
230 LocationSummary::kNoCall,
231 kIntrinsified);
232 locations->SetInAt(0, Location::RequiresRegister());
233 locations->SetOut(Location::SameAsFirstInput());
234 locations->AddTemp(Location::RequiresRegister());
235}
236
237static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
238 Location output = locations->Out();
239 CpuRegister out = output.AsRegister<CpuRegister>();
240 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
241
242 if (is64bit) {
243 // Create mask.
244 __ movq(mask, out);
245 __ sarq(mask, Immediate(63));
246 // Add mask.
247 __ addq(out, mask);
248 __ xorq(out, mask);
249 } else {
250 // Create mask.
251 __ movl(mask, out);
252 __ sarl(mask, Immediate(31));
253 // Add mask.
254 __ addl(out, mask);
255 __ xorl(out, mask);
256 }
257}
258
259void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
260 CreateIntToIntPlusTemp(arena_, invoke);
261}
262
263void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
264 GenAbsInteger(invoke->GetLocations(), false, GetAssembler());
265}
266
267void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
268 CreateIntToIntPlusTemp(arena_, invoke);
269}
270
271void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
272 GenAbsInteger(invoke->GetLocations(), true, GetAssembler());
273}
274
Mark Mendell39dcf552015-04-09 20:42:42 -0400275static void GenMinMaxFP(LocationSummary* locations,
276 bool is_min,
277 bool is_double,
278 X86_64Assembler* assembler,
279 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800280 Location op1_loc = locations->InAt(0);
281 Location op2_loc = locations->InAt(1);
282 Location out_loc = locations->Out();
283 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
284
285 // Shortcut for same input locations.
286 if (op1_loc.Equals(op2_loc)) {
287 DCHECK(out_loc.Equals(op1_loc));
288 return;
289 }
290
291 // (out := op1)
292 // out <=? op2
293 // if Nan jmp Nan_label
294 // if out is min jmp done
295 // if op2 is min jmp op2_label
296 // handle -0/+0
297 // jmp done
298 // Nan_label:
299 // out := NaN
300 // op2_label:
301 // out := op2
302 // done:
303 //
304 // This removes one jmp, but needs to copy one input (op1) to out.
305 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400306 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800307
308 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
309
Mark Mendell0c9497d2015-08-21 09:30:05 -0400310 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800311 if (is_double) {
312 __ ucomisd(out, op2);
313 } else {
314 __ ucomiss(out, op2);
315 }
316
317 __ j(Condition::kParityEven, &nan);
318
319 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
320 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
321
322 // Handle 0.0/-0.0.
323 if (is_min) {
324 if (is_double) {
325 __ orpd(out, op2);
326 } else {
327 __ orps(out, op2);
328 }
329 } else {
330 if (is_double) {
331 __ andpd(out, op2);
332 } else {
333 __ andps(out, op2);
334 }
335 }
336 __ jmp(&done);
337
338 // NaN handling.
339 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800340 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400341 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800342 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400343 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800344 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800345 __ jmp(&done);
346
347 // out := op2;
348 __ Bind(&op2_label);
349 if (is_double) {
350 __ movsd(out, op2);
351 } else {
352 __ movss(out, op2);
353 }
354
355 // Done.
356 __ Bind(&done);
357}
358
Mark Mendellf55c3e02015-03-26 21:07:46 -0400359static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800360 LocationSummary* locations = new (arena) LocationSummary(invoke,
361 LocationSummary::kNoCall,
362 kIntrinsified);
363 locations->SetInAt(0, Location::RequiresFpuRegister());
364 locations->SetInAt(1, Location::RequiresFpuRegister());
365 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
366 // the second input to be the output (we can simply swap inputs).
367 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800368}
369
370void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400371 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800372}
373
374void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400375 GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800376}
377
378void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400379 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800380}
381
382void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400383 GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800384}
385
386void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400387 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800388}
389
390void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400391 GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800392}
393
394void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400395 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800396}
397
398void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400399 GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800400}
401
402static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
403 X86_64Assembler* assembler) {
404 Location op1_loc = locations->InAt(0);
405 Location op2_loc = locations->InAt(1);
406
407 // Shortcut for same input locations.
408 if (op1_loc.Equals(op2_loc)) {
409 // Can return immediately, as op1_loc == out_loc.
410 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
411 // a copy here.
412 DCHECK(locations->Out().Equals(op1_loc));
413 return;
414 }
415
416 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
417 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
418
419 // (out := op1)
420 // out <=? op2
421 // if out is min jmp done
422 // out := op2
423 // done:
424
425 if (is_long) {
426 __ cmpq(out, op2);
427 } else {
428 __ cmpl(out, op2);
429 }
430
431 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
432}
433
434static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
435 LocationSummary* locations = new (arena) LocationSummary(invoke,
436 LocationSummary::kNoCall,
437 kIntrinsified);
438 locations->SetInAt(0, Location::RequiresRegister());
439 locations->SetInAt(1, Location::RequiresRegister());
440 locations->SetOut(Location::SameAsFirstInput());
441}
442
443void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
444 CreateIntIntToIntLocations(arena_, invoke);
445}
446
447void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
448 GenMinMax(invoke->GetLocations(), true, false, GetAssembler());
449}
450
451void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
452 CreateIntIntToIntLocations(arena_, invoke);
453}
454
455void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
456 GenMinMax(invoke->GetLocations(), true, true, GetAssembler());
457}
458
459void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
460 CreateIntIntToIntLocations(arena_, invoke);
461}
462
463void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
464 GenMinMax(invoke->GetLocations(), false, false, GetAssembler());
465}
466
467void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
468 CreateIntIntToIntLocations(arena_, invoke);
469}
470
471void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
472 GenMinMax(invoke->GetLocations(), false, true, GetAssembler());
473}
474
475static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
476 LocationSummary* locations = new (arena) LocationSummary(invoke,
477 LocationSummary::kNoCall,
478 kIntrinsified);
479 locations->SetInAt(0, Location::RequiresFpuRegister());
480 locations->SetOut(Location::RequiresFpuRegister());
481}
482
483void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
484 CreateFPToFPLocations(arena_, invoke);
485}
486
487void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
488 LocationSummary* locations = invoke->GetLocations();
489 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
490 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
491
492 GetAssembler()->sqrtsd(out, in);
493}
494
Mark Mendellfb8d2792015-03-31 22:16:59 -0400495static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100496 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400497
498 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100499 codegen->GenerateStaticOrDirectCall(
500 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400501 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
502
503 // Copy the result back to the expected output.
504 Location out = invoke->GetLocations()->Out();
505 if (out.IsValid()) {
506 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700507 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400508 }
509}
510
511static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
512 HInvoke* invoke,
513 CodeGeneratorX86_64* codegen) {
514 // Do we have instruction support?
515 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
516 CreateFPToFPLocations(arena, invoke);
517 return;
518 }
519
520 // We have to fall back to a call to the intrinsic.
521 LocationSummary* locations = new (arena) LocationSummary(invoke,
522 LocationSummary::kCall);
523 InvokeRuntimeCallingConvention calling_convention;
524 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
525 locations->SetOut(Location::FpuRegisterLocation(XMM0));
526 // Needs to be RDI for the invoke.
527 locations->AddTemp(Location::RegisterLocation(RDI));
528}
529
530static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
531 HInvoke* invoke,
532 X86_64Assembler* assembler,
533 int round_mode) {
534 LocationSummary* locations = invoke->GetLocations();
535 if (locations->WillCall()) {
536 InvokeOutOfLineIntrinsic(codegen, invoke);
537 } else {
538 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
539 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
540 __ roundsd(out, in, Immediate(round_mode));
541 }
542}
543
544void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
545 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
546}
547
548void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
549 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
550}
551
552void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
553 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
554}
555
556void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
557 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
558}
559
560void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
561 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
562}
563
564void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
565 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
566}
567
568static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
569 HInvoke* invoke,
570 CodeGeneratorX86_64* codegen) {
571 // Do we have instruction support?
572 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
573 LocationSummary* locations = new (arena) LocationSummary(invoke,
574 LocationSummary::kNoCall,
575 kIntrinsified);
576 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600577 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400578 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400579 return;
580 }
581
582 // We have to fall back to a call to the intrinsic.
583 LocationSummary* locations = new (arena) LocationSummary(invoke,
584 LocationSummary::kCall);
585 InvokeRuntimeCallingConvention calling_convention;
586 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
587 locations->SetOut(Location::RegisterLocation(RAX));
588 // Needs to be RDI for the invoke.
589 locations->AddTemp(Location::RegisterLocation(RDI));
590}
591
592void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
593 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
594}
595
596void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
597 LocationSummary* locations = invoke->GetLocations();
598 if (locations->WillCall()) {
599 InvokeOutOfLineIntrinsic(codegen_, invoke);
600 return;
601 }
602
603 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
604 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
605 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400606 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400607 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400608 X86_64Assembler* assembler = GetAssembler();
609
Mark Mendell40741f32015-04-20 22:10:34 -0400610 // Load 0.5 into inPlusPointFive.
611 __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400612
613 // Add in the input.
614 __ addss(inPlusPointFive, in);
615
616 // And truncate to an integer.
617 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
618
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600619 // Load maxInt into out.
620 codegen_->Load64BitValue(out, kPrimIntMax);
621
Mark Mendellfb8d2792015-03-31 22:16:59 -0400622 // if inPlusPointFive >= maxInt goto done
Mark Mendellcfa410b2015-05-25 16:02:44 -0400623 __ movl(out, Immediate(kPrimIntMax));
Mark Mendell40741f32015-04-20 22:10:34 -0400624 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400625 __ j(kAboveEqual, &done);
626
627 // if input == NaN goto nan
628 __ j(kUnordered, &nan);
629
630 // output = float-to-int-truncate(input)
631 __ cvttss2si(out, inPlusPointFive);
632 __ jmp(&done);
633 __ Bind(&nan);
634
635 // output = 0
636 __ xorl(out, out);
637 __ Bind(&done);
638}
639
640void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
641 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
642}
643
644void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
645 LocationSummary* locations = invoke->GetLocations();
646 if (locations->WillCall()) {
647 InvokeOutOfLineIntrinsic(codegen_, invoke);
648 return;
649 }
650
651 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
652 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
653 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400654 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400655 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400656 X86_64Assembler* assembler = GetAssembler();
657
Mark Mendell40741f32015-04-20 22:10:34 -0400658 // Load 0.5 into inPlusPointFive.
659 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400660
661 // Add in the input.
662 __ addsd(inPlusPointFive, in);
663
664 // And truncate to an integer.
665 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
666
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600667 // Load maxLong into out.
668 codegen_->Load64BitValue(out, kPrimLongMax);
669
Mark Mendellfb8d2792015-03-31 22:16:59 -0400670 // if inPlusPointFive >= maxLong goto done
Mark Mendellcfa410b2015-05-25 16:02:44 -0400671 __ movq(out, Immediate(kPrimLongMax));
Mark Mendell40741f32015-04-20 22:10:34 -0400672 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400673 __ j(kAboveEqual, &done);
674
675 // if input == NaN goto nan
676 __ j(kUnordered, &nan);
677
678 // output = double-to-long-truncate(input)
679 __ cvttsd2si(out, inPlusPointFive, true);
680 __ jmp(&done);
681 __ Bind(&nan);
682
683 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400684 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400685 __ Bind(&done);
686}
687
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800688void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) {
689 // The inputs plus one temp.
690 LocationSummary* locations = new (arena_) LocationSummary(invoke,
691 LocationSummary::kCallOnSlowPath,
692 kIntrinsified);
693 locations->SetInAt(0, Location::RequiresRegister());
694 locations->SetInAt(1, Location::RequiresRegister());
695 locations->SetOut(Location::SameAsFirstInput());
696 locations->AddTemp(Location::RequiresRegister());
697}
698
699void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) {
700 LocationSummary* locations = invoke->GetLocations();
701
Mark Mendell6bc53a92015-07-01 14:26:52 -0400702 // Location of reference to data array.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800703 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400704 // Location of count.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800705 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800706
707 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
708 CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>();
709 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800710
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800711 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
712 // the cost.
713 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
714 // we will not optimize the code for constants (which would save a register).
715
Andreas Gampe85b62f22015-09-09 13:15:38 -0700716 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800717 codegen_->AddSlowPath(slow_path);
718
719 X86_64Assembler* assembler = GetAssembler();
720
721 __ cmpl(idx, Address(obj, count_offset));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800722 codegen_->MaybeRecordImplicitNullCheck(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800723 __ j(kAboveEqual, slow_path->GetEntryLabel());
724
Jeff Hao848f70a2014-01-15 13:49:50 -0800725 // out = out[2*idx].
726 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800727
728 __ Bind(slow_path->GetExitLabel());
729}
730
Mark Mendell6bc53a92015-07-01 14:26:52 -0400731void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
732 // Check to see if we have known failures that will cause us to have to bail out
733 // to the runtime, and just generate the runtime call directly.
734 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
735 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
736
737 // The positions must be non-negative.
738 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
739 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
740 // We will have to fail anyways.
741 return;
742 }
743
744 // The length must be > 0.
745 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
746 if (length != nullptr) {
747 int32_t len = length->GetValue();
748 if (len < 0) {
749 // Just call as normal.
750 return;
751 }
752 }
753
754 LocationSummary* locations = new (arena_) LocationSummary(invoke,
755 LocationSummary::kCallOnSlowPath,
756 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100757 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400758 locations->SetInAt(0, Location::RequiresRegister());
759 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
760 locations->SetInAt(2, Location::RequiresRegister());
761 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
762 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
763
764 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
765 locations->AddTemp(Location::RegisterLocation(RSI));
766 locations->AddTemp(Location::RegisterLocation(RDI));
767 locations->AddTemp(Location::RegisterLocation(RCX));
768}
769
770static void CheckPosition(X86_64Assembler* assembler,
771 Location pos,
772 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100773 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700774 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400775 CpuRegister input_len,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100776 CpuRegister temp,
777 bool length_is_input_length = false) {
778 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400779 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
780
781 if (pos.IsConstant()) {
782 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
783 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100784 if (!length_is_input_length) {
785 // Check that length(input) >= length.
786 if (length.IsConstant()) {
787 __ cmpl(Address(input, length_offset),
788 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
789 } else {
790 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
791 }
792 __ j(kLess, slow_path->GetEntryLabel());
793 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400794 } else {
795 // Check that length(input) >= pos.
796 __ movl(input_len, Address(input, length_offset));
797 __ cmpl(input_len, Immediate(pos_const));
798 __ j(kLess, slow_path->GetEntryLabel());
799
800 // Check that (length(input) - pos) >= length.
801 __ leal(temp, Address(input_len, -pos_const));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100802 if (length.IsConstant()) {
803 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
804 } else {
805 __ cmpl(temp, length.AsRegister<CpuRegister>());
806 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400807 __ j(kLess, slow_path->GetEntryLabel());
808 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100809 } else if (length_is_input_length) {
810 // The only way the copy can succeed is if pos is zero.
811 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
812 __ testl(pos_reg, pos_reg);
813 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400814 } else {
815 // Check that pos >= 0.
816 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
817 __ testl(pos_reg, pos_reg);
818 __ j(kLess, slow_path->GetEntryLabel());
819
820 // Check that pos <= length(input).
821 __ cmpl(Address(input, length_offset), pos_reg);
822 __ j(kLess, slow_path->GetEntryLabel());
823
824 // Check that (length(input) - pos) >= length.
825 __ movl(temp, Address(input, length_offset));
826 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100827 if (length.IsConstant()) {
828 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
829 } else {
830 __ cmpl(temp, length.AsRegister<CpuRegister>());
831 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400832 __ j(kLess, slow_path->GetEntryLabel());
833 }
834}
835
836void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
837 X86_64Assembler* assembler = GetAssembler();
838 LocationSummary* locations = invoke->GetLocations();
839
840 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100841 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400842 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100843 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400844 Location length = locations->InAt(4);
845
846 // Temporaries that we need for MOVSW.
847 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
848 DCHECK_EQ(src_base.AsRegister(), RSI);
849 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
850 DCHECK_EQ(dest_base.AsRegister(), RDI);
851 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
852 DCHECK_EQ(count.AsRegister(), RCX);
853
Andreas Gampe85b62f22015-09-09 13:15:38 -0700854 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400855 codegen_->AddSlowPath(slow_path);
856
857 // Bail out if the source and destination are the same.
858 __ cmpl(src, dest);
859 __ j(kEqual, slow_path->GetEntryLabel());
860
861 // Bail out if the source is null.
862 __ testl(src, src);
863 __ j(kEqual, slow_path->GetEntryLabel());
864
865 // Bail out if the destination is null.
866 __ testl(dest, dest);
867 __ j(kEqual, slow_path->GetEntryLabel());
868
869 // If the length is negative, bail out.
870 // We have already checked in the LocationsBuilder for the constant case.
871 if (!length.IsConstant()) {
872 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
873 __ j(kLess, slow_path->GetEntryLabel());
874 }
875
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100876 // Validity checks: source.
877 CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base);
878
879 // Validity checks: dest.
880 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base);
881
Mark Mendell6bc53a92015-07-01 14:26:52 -0400882 // We need the count in RCX.
883 if (length.IsConstant()) {
884 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
885 } else {
886 __ movl(count, length.AsRegister<CpuRegister>());
887 }
888
Mark Mendell6bc53a92015-07-01 14:26:52 -0400889 // Okay, everything checks out. Finally time to do the copy.
890 // Check assumption that sizeof(Char) is 2 (used in scaling below).
891 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
892 DCHECK_EQ(char_size, 2u);
893
894 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
895
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100896 if (src_pos.IsConstant()) {
897 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
898 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400899 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100900 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400901 ScaleFactor::TIMES_2, data_offset));
902 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100903 if (dest_pos.IsConstant()) {
904 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
905 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400906 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100907 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400908 ScaleFactor::TIMES_2, data_offset));
909 }
910
911 // Do the move.
912 __ rep_movsw();
913
914 __ Bind(slow_path->GetExitLabel());
915}
916
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100917
918void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
919 // Check to see if we have known failures that will cause us to have to bail out
920 // to the runtime, and just generate the runtime call directly.
921 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
922 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
923
924 // The positions must be non-negative.
925 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
926 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
927 // We will have to fail anyways.
928 return;
929 }
930
931 // The length must be > 0.
932 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
933 if (length != nullptr) {
934 int32_t len = length->GetValue();
935 if (len < 0) {
936 // Just call as normal.
937 return;
938 }
939 }
940
941 SystemArrayCopyOptimizations optimizations(invoke);
942
943 if (optimizations.GetDestinationIsSource()) {
944 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
945 // We only support backward copying if source and destination are the same.
946 return;
947 }
948 }
949
950 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
951 // We currently don't intrinsify primitive copying.
952 return;
953 }
954
955 LocationSummary* locations = new (arena_) LocationSummary(invoke,
956 LocationSummary::kCallOnSlowPath,
957 kIntrinsified);
958 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
959 locations->SetInAt(0, Location::RequiresRegister());
960 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
961 locations->SetInAt(2, Location::RequiresRegister());
962 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
963 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
964
965 locations->AddTemp(Location::RequiresRegister());
966 locations->AddTemp(Location::RequiresRegister());
967 locations->AddTemp(Location::RequiresRegister());
968}
969
970void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
971 X86_64Assembler* assembler = GetAssembler();
972 LocationSummary* locations = invoke->GetLocations();
973
974 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
975 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
976 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
977 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
978
979 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
980 Location src_pos = locations->InAt(1);
981 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
982 Location dest_pos = locations->InAt(3);
983 Location length = locations->InAt(4);
984 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
985 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
986 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
987
988 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
989 codegen_->AddSlowPath(slow_path);
990
991 NearLabel ok;
992 SystemArrayCopyOptimizations optimizations(invoke);
993
994 if (!optimizations.GetDestinationIsSource()) {
995 __ cmpl(src, dest);
996 }
997
998 // If source and destination are the same, we go to slow path if we need to do
999 // forward copying.
1000 if (src_pos.IsConstant()) {
1001 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1002 if (dest_pos.IsConstant()) {
1003 // Checked when building locations.
1004 DCHECK(!optimizations.GetDestinationIsSource()
1005 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
1006 } else {
1007 if (!optimizations.GetDestinationIsSource()) {
1008 __ j(kNotEqual, &ok);
1009 }
1010 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
1011 __ j(kGreater, slow_path->GetEntryLabel());
1012 }
1013 } else {
1014 if (!optimizations.GetDestinationIsSource()) {
1015 __ j(kNotEqual, &ok);
1016 }
1017 if (dest_pos.IsConstant()) {
1018 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1019 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
1020 __ j(kLess, slow_path->GetEntryLabel());
1021 } else {
1022 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
1023 __ j(kLess, slow_path->GetEntryLabel());
1024 }
1025 }
1026
1027 __ Bind(&ok);
1028
1029 if (!optimizations.GetSourceIsNotNull()) {
1030 // Bail out if the source is null.
1031 __ testl(src, src);
1032 __ j(kEqual, slow_path->GetEntryLabel());
1033 }
1034
1035 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1036 // Bail out if the destination is null.
1037 __ testl(dest, dest);
1038 __ j(kEqual, slow_path->GetEntryLabel());
1039 }
1040
1041 // If the length is negative, bail out.
1042 // We have already checked in the LocationsBuilder for the constant case.
1043 if (!length.IsConstant() &&
1044 !optimizations.GetCountIsSourceLength() &&
1045 !optimizations.GetCountIsDestinationLength()) {
1046 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1047 __ j(kLess, slow_path->GetEntryLabel());
1048 }
1049
1050 // Validity checks: source.
1051 CheckPosition(assembler,
1052 src_pos,
1053 src,
1054 length,
1055 slow_path,
1056 temp1,
1057 temp2,
1058 optimizations.GetCountIsSourceLength());
1059
1060 // Validity checks: dest.
1061 CheckPosition(assembler,
1062 dest_pos,
1063 dest,
1064 length,
1065 slow_path,
1066 temp1,
1067 temp2,
1068 optimizations.GetCountIsDestinationLength());
1069
1070 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1071 // Check whether all elements of the source array are assignable to the component
1072 // type of the destination array. We do two checks: the classes are the same,
1073 // or the destination is Object[]. If none of these checks succeed, we go to the
1074 // slow path.
1075 __ movl(temp1, Address(dest, class_offset));
1076 __ movl(temp2, Address(src, class_offset));
1077 bool did_unpoison = false;
1078 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1079 !optimizations.GetSourceIsNonPrimitiveArray()) {
1080 // One or two of the references need to be unpoisoned. Unpoisoned them
1081 // both to make the identity check valid.
1082 __ MaybeUnpoisonHeapReference(temp1);
1083 __ MaybeUnpoisonHeapReference(temp2);
1084 did_unpoison = true;
1085 }
1086
1087 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1088 // Bail out if the destination is not a non primitive array.
1089 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1090 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1091 __ j(kEqual, slow_path->GetEntryLabel());
1092 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1093 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1094 __ j(kNotEqual, slow_path->GetEntryLabel());
1095 }
1096
1097 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1098 // Bail out if the source is not a non primitive array.
1099 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1100 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1101 __ j(kEqual, slow_path->GetEntryLabel());
1102 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1103 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1104 __ j(kNotEqual, slow_path->GetEntryLabel());
1105 }
1106
1107 __ cmpl(temp1, temp2);
1108
1109 if (optimizations.GetDestinationIsTypedObjectArray()) {
1110 NearLabel do_copy;
1111 __ j(kEqual, &do_copy);
1112 if (!did_unpoison) {
1113 __ MaybeUnpoisonHeapReference(temp1);
1114 }
1115 __ movl(temp1, Address(temp1, component_offset));
1116 __ MaybeUnpoisonHeapReference(temp1);
1117 __ movl(temp1, Address(temp1, super_offset));
1118 // No need to unpoison the result, we're comparing against null.
1119 __ testl(temp1, temp1);
1120 __ j(kNotEqual, slow_path->GetEntryLabel());
1121 __ Bind(&do_copy);
1122 } else {
1123 __ j(kNotEqual, slow_path->GetEntryLabel());
1124 }
1125 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1126 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1127 // Bail out if the source is not a non primitive array.
1128 __ movl(temp1, Address(src, class_offset));
1129 __ MaybeUnpoisonHeapReference(temp1);
1130 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1131 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1132 __ j(kEqual, slow_path->GetEntryLabel());
1133 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1134 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1135 __ j(kNotEqual, slow_path->GetEntryLabel());
1136 }
1137
1138 // Compute base source address, base destination address, and end source address.
1139
1140 uint32_t element_size = sizeof(int32_t);
1141 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1142 if (src_pos.IsConstant()) {
1143 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1144 __ leal(temp1, Address(src, element_size * constant + offset));
1145 } else {
1146 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1147 }
1148
1149 if (dest_pos.IsConstant()) {
1150 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1151 __ leal(temp2, Address(dest, element_size * constant + offset));
1152 } else {
1153 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1154 }
1155
1156 if (length.IsConstant()) {
1157 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1158 __ leal(temp3, Address(temp1, element_size * constant));
1159 } else {
1160 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1161 }
1162
1163 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1164 // poison/unpoison, nor do any read barrier as the next uses of the destination
1165 // array will do it.
1166 NearLabel loop, done;
1167 __ cmpl(temp1, temp3);
1168 __ j(kEqual, &done);
1169 __ Bind(&loop);
1170 __ movl(CpuRegister(TMP), Address(temp1, 0));
1171 __ movl(Address(temp2, 0), CpuRegister(TMP));
1172 __ addl(temp1, Immediate(element_size));
1173 __ addl(temp2, Immediate(element_size));
1174 __ cmpl(temp1, temp3);
1175 __ j(kNotEqual, &loop);
1176 __ Bind(&done);
1177
1178 // We only need one card marking on the destination array.
1179 codegen_->MarkGCCard(temp1,
1180 temp2,
1181 dest,
1182 CpuRegister(kNoRegister),
1183 false);
1184
1185 __ Bind(slow_path->GetExitLabel());
1186}
1187
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001188void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1189 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1190 LocationSummary::kCall,
1191 kIntrinsified);
1192 InvokeRuntimeCallingConvention calling_convention;
1193 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1194 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1195 locations->SetOut(Location::RegisterLocation(RAX));
1196}
1197
1198void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1199 X86_64Assembler* assembler = GetAssembler();
1200 LocationSummary* locations = invoke->GetLocations();
1201
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001202 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001203 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001204
1205 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1206 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001207 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001208 codegen_->AddSlowPath(slow_path);
1209 __ j(kEqual, slow_path->GetEntryLabel());
1210
1211 __ gs()->call(Address::Absolute(
1212 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true));
1213 __ Bind(slow_path->GetExitLabel());
1214}
1215
Agi Csakif8cfb202015-08-13 17:54:54 -07001216void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1217 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1218 LocationSummary::kNoCall,
1219 kIntrinsified);
1220 locations->SetInAt(0, Location::RequiresRegister());
1221 locations->SetInAt(1, Location::RequiresRegister());
1222
1223 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1224 locations->AddTemp(Location::RegisterLocation(RCX));
1225 locations->AddTemp(Location::RegisterLocation(RDI));
1226
1227 // Set output, RSI needed for repe_cmpsq instruction anyways.
1228 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1229}
1230
1231void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1232 X86_64Assembler* assembler = GetAssembler();
1233 LocationSummary* locations = invoke->GetLocations();
1234
1235 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1236 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1237 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1238 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1239 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1240
Mark Mendell0c9497d2015-08-21 09:30:05 -04001241 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001242
1243 // Get offsets of count, value, and class fields within a string object.
1244 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1245 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1246 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1247
1248 // Note that the null check must have been done earlier.
1249 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1250
1251 // Check if input is null, return false if it is.
1252 __ testl(arg, arg);
1253 __ j(kEqual, &return_false);
1254
1255 // Instanceof check for the argument by comparing class fields.
1256 // All string objects must have the same type since String cannot be subclassed.
1257 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1258 // If the argument is a string object, its class field must be equal to receiver's class field.
1259 __ movl(rcx, Address(str, class_offset));
1260 __ cmpl(rcx, Address(arg, class_offset));
1261 __ j(kNotEqual, &return_false);
1262
1263 // Reference equality check, return true if same reference.
1264 __ cmpl(str, arg);
1265 __ j(kEqual, &return_true);
1266
1267 // Load length of receiver string.
1268 __ movl(rcx, Address(str, count_offset));
1269 // Check if lengths are equal, return false if they're not.
1270 __ cmpl(rcx, Address(arg, count_offset));
1271 __ j(kNotEqual, &return_false);
1272 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001273 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001274
1275 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1276 __ leal(rsi, Address(str, value_offset));
1277 __ leal(rdi, Address(arg, value_offset));
1278
1279 // Divide string length by 4 and adjust for lengths not divisible by 4.
1280 __ addl(rcx, Immediate(3));
1281 __ shrl(rcx, Immediate(2));
1282
1283 // Assertions that must hold in order to compare strings 4 characters at a time.
1284 DCHECK_ALIGNED(value_offset, 8);
1285 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1286
1287 // Loop to compare strings four characters at a time starting at the beginning of the string.
1288 __ repe_cmpsq();
1289 // If strings are not equal, zero flag will be cleared.
1290 __ j(kNotEqual, &return_false);
1291
1292 // Return true and exit the function.
1293 // If loop does not result in returning false, we return true.
1294 __ Bind(&return_true);
1295 __ movl(rsi, Immediate(1));
1296 __ jmp(&end);
1297
1298 // Return false and exit the function.
1299 __ Bind(&return_false);
1300 __ xorl(rsi, rsi);
1301 __ Bind(&end);
1302}
1303
Andreas Gampe21030dd2015-05-07 14:46:15 -07001304static void CreateStringIndexOfLocations(HInvoke* invoke,
1305 ArenaAllocator* allocator,
1306 bool start_at_zero) {
1307 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1308 LocationSummary::kCallOnSlowPath,
1309 kIntrinsified);
1310 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1311 locations->SetInAt(0, Location::RegisterLocation(RDI));
1312 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1313 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1314 // of the instruction explicitly.
1315 // Note: This works as we don't clobber RAX anywhere.
1316 locations->SetInAt(1, Location::RegisterLocation(RAX));
1317 if (!start_at_zero) {
1318 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1319 }
1320 // As we clobber RDI during execution anyways, also use it as the output.
1321 locations->SetOut(Location::SameAsFirstInput());
1322
1323 // repne scasw uses RCX as the counter.
1324 locations->AddTemp(Location::RegisterLocation(RCX));
1325 // Need another temporary to be able to compute the result.
1326 locations->AddTemp(Location::RequiresRegister());
1327}
1328
1329static void GenerateStringIndexOf(HInvoke* invoke,
1330 X86_64Assembler* assembler,
1331 CodeGeneratorX86_64* codegen,
1332 ArenaAllocator* allocator,
1333 bool start_at_zero) {
1334 LocationSummary* locations = invoke->GetLocations();
1335
1336 // Note that the null check must have been done earlier.
1337 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1338
1339 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1340 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1341 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1342 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1343 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1344
1345 // Check our assumptions for registers.
1346 DCHECK_EQ(string_obj.AsRegister(), RDI);
1347 DCHECK_EQ(search_value.AsRegister(), RAX);
1348 DCHECK_EQ(counter.AsRegister(), RCX);
1349 DCHECK_EQ(out.AsRegister(), RDI);
1350
1351 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1352 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001353 SlowPathCode* slow_path = nullptr;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001354 if (invoke->InputAt(1)->IsIntConstant()) {
1355 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1356 std::numeric_limits<uint16_t>::max()) {
1357 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1358 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1359 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1360 codegen->AddSlowPath(slow_path);
1361 __ jmp(slow_path->GetEntryLabel());
1362 __ Bind(slow_path->GetExitLabel());
1363 return;
1364 }
1365 } else {
1366 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1367 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1368 codegen->AddSlowPath(slow_path);
1369 __ j(kAbove, slow_path->GetEntryLabel());
1370 }
1371
1372 // From here down, we know that we are looking for a char that fits in 16 bits.
1373 // Location of reference to data array within the String object.
1374 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1375 // Location of count within the String object.
1376 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1377
1378 // Load string length, i.e., the count field of the string.
1379 __ movl(string_length, Address(string_obj, count_offset));
1380
1381 // Do a length check.
1382 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001383 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001384 __ testl(string_length, string_length);
1385 __ j(kEqual, &not_found_label);
1386
1387 if (start_at_zero) {
1388 // Number of chars to scan is the same as the string length.
1389 __ movl(counter, string_length);
1390
1391 // Move to the start of the string.
1392 __ addq(string_obj, Immediate(value_offset));
1393 } else {
1394 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1395
1396 // Do a start_index check.
1397 __ cmpl(start_index, string_length);
1398 __ j(kGreaterEqual, &not_found_label);
1399
1400 // Ensure we have a start index >= 0;
1401 __ xorl(counter, counter);
1402 __ cmpl(start_index, Immediate(0));
1403 __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough.
1404
1405 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1406 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1407
1408 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1409 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1410 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1411 }
1412
1413 // Everything is set up for repne scasw:
1414 // * Comparison address in RDI.
1415 // * Counter in ECX.
1416 __ repne_scasw();
1417
1418 // Did we find a match?
1419 __ j(kNotEqual, &not_found_label);
1420
1421 // Yes, we matched. Compute the index of the result.
1422 __ subl(string_length, counter);
1423 __ leal(out, Address(string_length, -1));
1424
Mark Mendell0c9497d2015-08-21 09:30:05 -04001425 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001426 __ jmp(&done);
1427
1428 // Failed to match; return -1.
1429 __ Bind(&not_found_label);
1430 __ movl(out, Immediate(-1));
1431
1432 // And join up at the end.
1433 __ Bind(&done);
1434 if (slow_path != nullptr) {
1435 __ Bind(slow_path->GetExitLabel());
1436 }
1437}
1438
1439void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
1440 CreateStringIndexOfLocations(invoke, arena_, true);
1441}
1442
1443void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
1444 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1445}
1446
1447void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1448 CreateStringIndexOfLocations(invoke, arena_, false);
1449}
1450
1451void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1452 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1453}
1454
Jeff Hao848f70a2014-01-15 13:49:50 -08001455void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1456 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1457 LocationSummary::kCall,
1458 kIntrinsified);
1459 InvokeRuntimeCallingConvention calling_convention;
1460 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1461 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1462 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1463 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1464 locations->SetOut(Location::RegisterLocation(RAX));
1465}
1466
1467void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1468 X86_64Assembler* assembler = GetAssembler();
1469 LocationSummary* locations = invoke->GetLocations();
1470
1471 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1472 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001473 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001474 codegen_->AddSlowPath(slow_path);
1475 __ j(kEqual, slow_path->GetEntryLabel());
1476
1477 __ gs()->call(Address::Absolute(
1478 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true));
1479 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1480 __ Bind(slow_path->GetExitLabel());
1481}
1482
1483void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1484 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1485 LocationSummary::kCall,
1486 kIntrinsified);
1487 InvokeRuntimeCallingConvention calling_convention;
1488 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1489 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1490 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1491 locations->SetOut(Location::RegisterLocation(RAX));
1492}
1493
1494void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1495 X86_64Assembler* assembler = GetAssembler();
1496
1497 __ gs()->call(Address::Absolute(
1498 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true));
1499 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1500}
1501
1502void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1503 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1504 LocationSummary::kCall,
1505 kIntrinsified);
1506 InvokeRuntimeCallingConvention calling_convention;
1507 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1508 locations->SetOut(Location::RegisterLocation(RAX));
1509}
1510
1511void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1512 X86_64Assembler* assembler = GetAssembler();
1513 LocationSummary* locations = invoke->GetLocations();
1514
1515 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1516 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001517 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001518 codegen_->AddSlowPath(slow_path);
1519 __ j(kEqual, slow_path->GetEntryLabel());
1520
1521 __ gs()->call(Address::Absolute(
1522 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true));
1523 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1524 __ Bind(slow_path->GetExitLabel());
1525}
1526
Mark Mendell8f8926a2015-08-17 11:39:06 -04001527void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1528 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1529 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1530 LocationSummary::kNoCall,
1531 kIntrinsified);
1532 locations->SetInAt(0, Location::RequiresRegister());
1533 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1534 locations->SetInAt(2, Location::RequiresRegister());
1535 locations->SetInAt(3, Location::RequiresRegister());
1536 locations->SetInAt(4, Location::RequiresRegister());
1537
1538 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1539 locations->AddTemp(Location::RegisterLocation(RSI));
1540 locations->AddTemp(Location::RegisterLocation(RDI));
1541 locations->AddTemp(Location::RegisterLocation(RCX));
1542}
1543
1544void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1545 X86_64Assembler* assembler = GetAssembler();
1546 LocationSummary* locations = invoke->GetLocations();
1547
1548 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1549 // Location of data in char array buffer.
1550 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1551 // Location of char array data in string.
1552 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1553
1554 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1555 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1556 Location srcBegin = locations->InAt(1);
1557 int srcBegin_value =
1558 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1559 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1560 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1561 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1562
1563 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1564 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1565 DCHECK_EQ(char_size, 2u);
1566
1567 // Compute the address of the destination buffer.
1568 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1569
1570 // Compute the address of the source string.
1571 if (srcBegin.IsConstant()) {
1572 // Compute the address of the source string by adding the number of chars from
1573 // the source beginning to the value offset of a string.
1574 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1575 } else {
1576 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1577 ScaleFactor::TIMES_2, value_offset));
1578 }
1579
1580 // Compute the number of chars (words) to move.
1581 __ movl(CpuRegister(RCX), srcEnd);
1582 if (srcBegin.IsConstant()) {
1583 if (srcBegin_value != 0) {
1584 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1585 }
1586 } else {
1587 DCHECK(srcBegin.IsRegister());
1588 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1589 }
1590
1591 // Do the move.
1592 __ rep_movsw();
1593}
1594
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001595static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1596 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1597 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1598 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1599 // to avoid a SIGBUS.
1600 switch (size) {
1601 case Primitive::kPrimByte:
1602 __ movsxb(out, Address(address, 0));
1603 break;
1604 case Primitive::kPrimShort:
1605 __ movsxw(out, Address(address, 0));
1606 break;
1607 case Primitive::kPrimInt:
1608 __ movl(out, Address(address, 0));
1609 break;
1610 case Primitive::kPrimLong:
1611 __ movq(out, Address(address, 0));
1612 break;
1613 default:
1614 LOG(FATAL) << "Type not recognized for peek: " << size;
1615 UNREACHABLE();
1616 }
1617}
1618
1619void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1620 CreateIntToIntLocations(arena_, invoke);
1621}
1622
1623void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1624 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1625}
1626
1627void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1628 CreateIntToIntLocations(arena_, invoke);
1629}
1630
1631void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1632 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1633}
1634
1635void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1636 CreateIntToIntLocations(arena_, invoke);
1637}
1638
1639void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1640 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1641}
1642
1643void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1644 CreateIntToIntLocations(arena_, invoke);
1645}
1646
1647void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1648 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1649}
1650
1651static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1652 LocationSummary* locations = new (arena) LocationSummary(invoke,
1653 LocationSummary::kNoCall,
1654 kIntrinsified);
1655 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001656 locations->SetInAt(1, Location::RegisterOrInt32LongConstant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001657}
1658
1659static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1660 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001661 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001662 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1663 // to avoid a SIGBUS.
1664 switch (size) {
1665 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001666 if (value.IsConstant()) {
1667 __ movb(Address(address, 0),
1668 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1669 } else {
1670 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1671 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001672 break;
1673 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001674 if (value.IsConstant()) {
1675 __ movw(Address(address, 0),
1676 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1677 } else {
1678 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1679 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001680 break;
1681 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001682 if (value.IsConstant()) {
1683 __ movl(Address(address, 0),
1684 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1685 } else {
1686 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1687 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001688 break;
1689 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001690 if (value.IsConstant()) {
1691 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1692 DCHECK(IsInt<32>(v));
1693 int32_t v_32 = v;
1694 __ movq(Address(address, 0), Immediate(v_32));
1695 } else {
1696 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1697 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001698 break;
1699 default:
1700 LOG(FATAL) << "Type not recognized for poke: " << size;
1701 UNREACHABLE();
1702 }
1703}
1704
1705void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1706 CreateIntIntToVoidLocations(arena_, invoke);
1707}
1708
1709void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1710 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1711}
1712
1713void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1714 CreateIntIntToVoidLocations(arena_, invoke);
1715}
1716
1717void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1718 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1719}
1720
1721void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1722 CreateIntIntToVoidLocations(arena_, invoke);
1723}
1724
1725void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1726 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1727}
1728
1729void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1730 CreateIntIntToVoidLocations(arena_, invoke);
1731}
1732
1733void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1734 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1735}
1736
1737void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1738 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1739 LocationSummary::kNoCall,
1740 kIntrinsified);
1741 locations->SetOut(Location::RequiresRegister());
1742}
1743
1744void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1745 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
1746 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true));
1747}
1748
Andreas Gampe878d58c2015-01-15 23:24:00 -08001749static void GenUnsafeGet(LocationSummary* locations, Primitive::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001750 bool is_volatile ATTRIBUTE_UNUSED, X86_64Assembler* assembler) {
1751 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1752 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1753 CpuRegister trg = locations->Out().AsRegister<CpuRegister>();
1754
Andreas Gampe878d58c2015-01-15 23:24:00 -08001755 switch (type) {
1756 case Primitive::kPrimInt:
1757 case Primitive::kPrimNot:
1758 __ movl(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001759 if (type == Primitive::kPrimNot) {
1760 __ MaybeUnpoisonHeapReference(trg);
1761 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001762 break;
1763
1764 case Primitive::kPrimLong:
1765 __ movq(trg, Address(base, offset, ScaleFactor::TIMES_1, 0));
1766 break;
1767
1768 default:
1769 LOG(FATAL) << "Unsupported op size " << type;
1770 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001771 }
1772}
1773
1774static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
1775 LocationSummary* locations = new (arena) LocationSummary(invoke,
1776 LocationSummary::kNoCall,
1777 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001778 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001779 locations->SetInAt(1, Location::RequiresRegister());
1780 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001781 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001782}
1783
1784void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1785 CreateIntIntIntToIntLocations(arena_, invoke);
1786}
1787void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1788 CreateIntIntIntToIntLocations(arena_, invoke);
1789}
1790void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1791 CreateIntIntIntToIntLocations(arena_, invoke);
1792}
1793void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1794 CreateIntIntIntToIntLocations(arena_, invoke);
1795}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001796void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1797 CreateIntIntIntToIntLocations(arena_, invoke);
1798}
1799void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1800 CreateIntIntIntToIntLocations(arena_, invoke);
1801}
1802
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001803
1804void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001805 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001806}
1807void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001808 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimInt, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001809}
1810void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001811 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001812}
1813void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Andreas Gampe878d58c2015-01-15 23:24:00 -08001814 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimLong, true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001815}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001816void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1817 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, false, GetAssembler());
1818}
1819void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1820 GenUnsafeGet(invoke->GetLocations(), Primitive::kPrimNot, true, GetAssembler());
1821}
1822
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001823
1824static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1825 Primitive::Type type,
1826 HInvoke* invoke) {
1827 LocationSummary* locations = new (arena) LocationSummary(invoke,
1828 LocationSummary::kNoCall,
1829 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001830 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001831 locations->SetInAt(1, Location::RequiresRegister());
1832 locations->SetInAt(2, Location::RequiresRegister());
1833 locations->SetInAt(3, Location::RequiresRegister());
1834 if (type == Primitive::kPrimNot) {
1835 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001836 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001837 locations->AddTemp(Location::RequiresRegister());
1838 }
1839}
1840
1841void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1842 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1843}
1844void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1845 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1846}
1847void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1848 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1849}
1850void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1851 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1852}
1853void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1854 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1855}
1856void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1857 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1858}
1859void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1860 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1861}
1862void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1863 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1864}
1865void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1866 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1867}
1868
1869// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1870// memory model.
1871static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1872 CodeGeneratorX86_64* codegen) {
1873 X86_64Assembler* assembler = reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1874 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1875 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1876 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1877
1878 if (type == Primitive::kPrimLong) {
1879 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001880 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1881 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1882 __ movl(temp, value);
1883 __ PoisonHeapReference(temp);
1884 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001885 } else {
1886 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1887 }
1888
1889 if (is_volatile) {
1890 __ mfence();
1891 }
1892
1893 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001894 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001895 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1896 locations->GetTemp(1).AsRegister<CpuRegister>(),
1897 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001898 value,
1899 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001900 }
1901}
1902
1903void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
1904 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1905}
1906void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1907 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1908}
1909void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1910 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1911}
1912void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1913 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1914}
1915void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1916 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1917}
1918void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1919 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1920}
1921void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1922 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1923}
1924void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1925 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1926}
1927void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1928 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1929}
1930
Mark Mendell58d25fd2015-04-03 14:52:31 -04001931static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1932 HInvoke* invoke) {
1933 LocationSummary* locations = new (arena) LocationSummary(invoke,
1934 LocationSummary::kNoCall,
1935 kIntrinsified);
1936 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1937 locations->SetInAt(1, Location::RequiresRegister());
1938 locations->SetInAt(2, Location::RequiresRegister());
1939 // expected value must be in EAX/RAX.
1940 locations->SetInAt(3, Location::RegisterLocation(RAX));
1941 locations->SetInAt(4, Location::RequiresRegister());
1942
1943 locations->SetOut(Location::RequiresRegister());
1944 if (type == Primitive::kPrimNot) {
1945 // Need temp registers for card-marking.
1946 locations->AddTemp(Location::RequiresRegister());
1947 locations->AddTemp(Location::RequiresRegister());
1948 }
1949}
1950
1951void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1952 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1953}
1954
1955void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1956 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1957}
1958
1959void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1960 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1961}
1962
1963static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
1964 X86_64Assembler* assembler =
1965 reinterpret_cast<X86_64Assembler*>(codegen->GetAssembler());
1966 LocationSummary* locations = invoke->GetLocations();
1967
1968 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1969 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1970 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
1971 DCHECK_EQ(expected.AsRegister(), RAX);
1972 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1973 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1974
1975 if (type == Primitive::kPrimLong) {
1976 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
1977 } else {
1978 // Integer or object.
1979 if (type == Primitive::kPrimNot) {
1980 // Mark card for object assuming new value is stored.
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001981 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell58d25fd2015-04-03 14:52:31 -04001982 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1983 locations->GetTemp(1).AsRegister<CpuRegister>(),
1984 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001985 value,
1986 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001987
1988 if (kPoisonHeapReferences) {
1989 __ PoisonHeapReference(expected);
1990 __ PoisonHeapReference(value);
1991 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04001992 }
1993
1994 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
1995 }
1996
1997 // locked cmpxchg has full barrier semantics, and we don't need scheduling
1998 // barriers at this time.
1999
2000 // Convert ZF into the boolean result.
2001 __ setcc(kZero, out);
2002 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002003
2004 if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2005 __ UnpoisonHeapReference(value);
2006 __ UnpoisonHeapReference(expected);
2007 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002008}
2009
2010void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2011 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2012}
2013
2014void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2015 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2016}
2017
2018void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
2019 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2020}
2021
2022void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2023 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2024 LocationSummary::kNoCall,
2025 kIntrinsified);
2026 locations->SetInAt(0, Location::RequiresRegister());
2027 locations->SetOut(Location::SameAsFirstInput());
2028 locations->AddTemp(Location::RequiresRegister());
2029}
2030
2031static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2032 X86_64Assembler* assembler) {
2033 Immediate imm_shift(shift);
2034 Immediate imm_mask(mask);
2035 __ movl(temp, reg);
2036 __ shrl(reg, imm_shift);
2037 __ andl(temp, imm_mask);
2038 __ andl(reg, imm_mask);
2039 __ shll(temp, imm_shift);
2040 __ orl(reg, temp);
2041}
2042
2043void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
2044 X86_64Assembler* assembler =
2045 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
2046 LocationSummary* locations = invoke->GetLocations();
2047
2048 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2049 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2050
2051 /*
2052 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2053 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2054 * compared to generic luni implementation which has 5 rounds of swapping bits.
2055 * x = bswap x
2056 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2057 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2058 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2059 */
2060 __ bswapl(reg);
2061 SwapBits(reg, temp, 1, 0x55555555, assembler);
2062 SwapBits(reg, temp, 2, 0x33333333, assembler);
2063 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2064}
2065
2066void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2067 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2068 LocationSummary::kNoCall,
2069 kIntrinsified);
2070 locations->SetInAt(0, Location::RequiresRegister());
2071 locations->SetOut(Location::SameAsFirstInput());
2072 locations->AddTemp(Location::RequiresRegister());
2073 locations->AddTemp(Location::RequiresRegister());
2074}
2075
2076static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2077 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2078 Immediate imm_shift(shift);
2079 __ movq(temp_mask, Immediate(mask));
2080 __ movq(temp, reg);
2081 __ shrq(reg, imm_shift);
2082 __ andq(temp, temp_mask);
2083 __ andq(reg, temp_mask);
2084 __ shlq(temp, imm_shift);
2085 __ orq(reg, temp);
2086}
2087
2088void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
2089 X86_64Assembler* assembler =
2090 reinterpret_cast<X86_64Assembler*>(codegen_->GetAssembler());
2091 LocationSummary* locations = invoke->GetLocations();
2092
2093 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2094 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2095 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2096
2097 /*
2098 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2099 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2100 * compared to generic luni implementation which has 5 rounds of swapping bits.
2101 * x = bswap x
2102 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2103 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2104 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2105 */
2106 __ bswapq(reg);
2107 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2108 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2109 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2110}
2111
Mark Mendelld5897672015-08-12 21:16:41 -04002112static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2113 LocationSummary* locations = new (arena) LocationSummary(invoke,
2114 LocationSummary::kNoCall,
2115 kIntrinsified);
2116 locations->SetInAt(0, Location::Any());
2117 locations->SetOut(Location::RequiresRegister());
2118}
2119
2120static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2121 LocationSummary* locations = invoke->GetLocations();
2122 Location src = locations->InAt(0);
2123 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2124
2125 int zero_value_result = is_long ? 64 : 32;
2126 if (invoke->InputAt(0)->IsConstant()) {
2127 // Evaluate this at compile time.
2128 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2129 if (value == 0) {
2130 value = zero_value_result;
2131 } else {
2132 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2133 }
2134 if (value == 0) {
2135 __ xorl(out, out);
2136 } else {
2137 __ movl(out, Immediate(value));
2138 }
2139 return;
2140 }
2141
2142 // Handle the non-constant cases.
2143 if (src.IsRegister()) {
2144 if (is_long) {
2145 __ bsrq(out, src.AsRegister<CpuRegister>());
2146 } else {
2147 __ bsrl(out, src.AsRegister<CpuRegister>());
2148 }
2149 } else if (is_long) {
2150 DCHECK(src.IsDoubleStackSlot());
2151 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2152 } else {
2153 DCHECK(src.IsStackSlot());
2154 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2155 }
2156
2157 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002158 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002159 __ j(kEqual, &is_zero);
2160
2161 // Correct the result from BSR to get the CLZ result.
2162 __ xorl(out, Immediate(zero_value_result - 1));
2163 __ jmp(&done);
2164
2165 // Fix the zero case with the expected result.
2166 __ Bind(&is_zero);
2167 __ movl(out, Immediate(zero_value_result));
2168
2169 __ Bind(&done);
2170}
2171
2172void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2173 CreateLeadingZeroLocations(arena_, invoke);
2174}
2175
2176void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2177 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2178 GenLeadingZeros(assembler, invoke, /* is_long */ false);
2179}
2180
2181void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2182 CreateLeadingZeroLocations(arena_, invoke);
2183}
2184
2185void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2186 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2187 GenLeadingZeros(assembler, invoke, /* is_long */ true);
2188}
2189
Mark Mendell2d554792015-09-15 21:45:18 -04002190static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2191 LocationSummary* locations = new (arena) LocationSummary(invoke,
2192 LocationSummary::kNoCall,
2193 kIntrinsified);
2194 locations->SetInAt(0, Location::Any());
2195 locations->SetOut(Location::RequiresRegister());
2196}
2197
2198static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2199 LocationSummary* locations = invoke->GetLocations();
2200 Location src = locations->InAt(0);
2201 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2202
2203 int zero_value_result = is_long ? 64 : 32;
2204 if (invoke->InputAt(0)->IsConstant()) {
2205 // Evaluate this at compile time.
2206 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2207 if (value == 0) {
2208 value = zero_value_result;
2209 } else {
2210 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2211 }
2212 if (value == 0) {
2213 __ xorl(out, out);
2214 } else {
2215 __ movl(out, Immediate(value));
2216 }
2217 return;
2218 }
2219
2220 // Handle the non-constant cases.
2221 if (src.IsRegister()) {
2222 if (is_long) {
2223 __ bsfq(out, src.AsRegister<CpuRegister>());
2224 } else {
2225 __ bsfl(out, src.AsRegister<CpuRegister>());
2226 }
2227 } else if (is_long) {
2228 DCHECK(src.IsDoubleStackSlot());
2229 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2230 } else {
2231 DCHECK(src.IsStackSlot());
2232 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2233 }
2234
2235 // BSF sets ZF if the input was zero, and the output is undefined.
2236 NearLabel done;
2237 __ j(kNotEqual, &done);
2238
2239 // Fix the zero case with the expected result.
2240 __ movl(out, Immediate(zero_value_result));
2241
2242 __ Bind(&done);
2243}
2244
2245void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2246 CreateTrailingZeroLocations(arena_, invoke);
2247}
2248
2249void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2250 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2251 GenTrailingZeros(assembler, invoke, /* is_long */ false);
2252}
2253
2254void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2255 CreateTrailingZeroLocations(arena_, invoke);
2256}
2257
2258void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2259 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2260 GenTrailingZeros(assembler, invoke, /* is_long */ true);
2261}
2262
2263static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) {
2264 LocationSummary* locations = new (arena) LocationSummary(invoke,
2265 LocationSummary::kNoCall,
2266 kIntrinsified);
2267 locations->SetInAt(0, Location::RequiresRegister());
2268 // The shift count needs to be in CL or a constant.
2269 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1)));
2270 locations->SetOut(Location::SameAsFirstInput());
2271}
2272
2273static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) {
2274 LocationSummary* locations = invoke->GetLocations();
2275 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
2276 Location second = locations->InAt(1);
2277
2278 if (is_long) {
2279 if (second.IsRegister()) {
2280 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2281 if (is_left) {
2282 __ rolq(first_reg, second_reg);
2283 } else {
2284 __ rorq(first_reg, second_reg);
2285 }
2286 } else {
2287 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
2288 if (is_left) {
2289 __ rolq(first_reg, imm);
2290 } else {
2291 __ rorq(first_reg, imm);
2292 }
2293 }
2294 } else {
2295 if (second.IsRegister()) {
2296 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2297 if (is_left) {
2298 __ roll(first_reg, second_reg);
2299 } else {
2300 __ rorl(first_reg, second_reg);
2301 }
2302 } else {
2303 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2304 if (is_left) {
2305 __ roll(first_reg, imm);
2306 } else {
2307 __ rorl(first_reg, imm);
2308 }
2309 }
2310 }
2311}
2312
2313void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2314 CreateRotateLocations(arena_, invoke);
2315}
2316
2317void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2318 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2319 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true);
2320}
2321
2322void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2323 CreateRotateLocations(arena_, invoke);
2324}
2325
2326void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2327 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2328 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false);
2329}
2330
2331void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2332 CreateRotateLocations(arena_, invoke);
2333}
2334
2335void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2336 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2337 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true);
2338}
2339
2340void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) {
2341 CreateRotateLocations(arena_, invoke);
2342}
2343
2344void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) {
2345 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2346 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false);
2347}
2348
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002349// Unimplemented intrinsics.
2350
2351#define UNIMPLEMENTED_INTRINSIC(Name) \
2352void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2353} \
2354void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2355}
2356
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002357UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
2358
Roland Levillain4d027112015-07-01 15:41:14 +01002359#undef UNIMPLEMENTED_INTRINSIC
2360
2361#undef __
2362
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002363} // namespace x86_64
2364} // namespace art