blob: 2d9f01b821299cd52a2e446c2d65ebd850ac7a87 [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() {
Roland Levillainb488b782015-10-22 11:38:49 +010044 return down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -080045}
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);
Roland Levillain0d5a2812015-11-13 10:07:31 +000053 LocationSummary* res = invoke->GetLocations();
54 if (res == nullptr) {
55 return false;
56 }
57 if (kEmitCompilerReadBarrier && res->CanCall()) {
58 // Generating an intrinsic for this HInvoke may produce an
59 // IntrinsicSlowPathX86_64 slow path. Currently this approach
60 // does not work when using read barriers, as the emitted
61 // calling sequence will make use of another slow path
62 // (ReadBarrierForRootSlowPathX86_64 for HInvokeStaticOrDirect,
63 // ReadBarrierSlowPathX86_64 for HInvokeVirtual). So we bail
64 // out in this case.
65 //
66 // TODO: Find a way to have intrinsics work with read barriers.
67 invoke->SetLocations(nullptr);
68 return false;
69 }
70 return res->Intrinsified();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080071}
72
Roland Levillainec525fc2015-04-28 15:50:20 +010073static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010074 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010075 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080076}
77
Andreas Gampe85b62f22015-09-09 13:15:38 -070078using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080079
Andreas Gampe71fb52f2014-12-29 17:43:08 -080080#define __ assembler->
81
82static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
83 LocationSummary* locations = new (arena) LocationSummary(invoke,
84 LocationSummary::kNoCall,
85 kIntrinsified);
86 locations->SetInAt(0, Location::RequiresFpuRegister());
87 locations->SetOut(Location::RequiresRegister());
88}
89
90static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
91 LocationSummary* locations = new (arena) LocationSummary(invoke,
92 LocationSummary::kNoCall,
93 kIntrinsified);
94 locations->SetInAt(0, Location::RequiresRegister());
95 locations->SetOut(Location::RequiresFpuRegister());
96}
97
98static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
99 Location input = locations->InAt(0);
100 Location output = locations->Out();
101 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
102}
103
104static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
105 Location input = locations->InAt(0);
106 Location output = locations->Out();
107 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
108}
109
110void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
111 CreateFPToIntLocations(arena_, invoke);
112}
113void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
114 CreateIntToFPLocations(arena_, invoke);
115}
116
117void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
118 MoveFPToInt(invoke->GetLocations(), true, GetAssembler());
119}
120void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
121 MoveIntToFP(invoke->GetLocations(), true, GetAssembler());
122}
123
124void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
125 CreateFPToIntLocations(arena_, invoke);
126}
127void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
128 CreateIntToFPLocations(arena_, invoke);
129}
130
131void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
132 MoveFPToInt(invoke->GetLocations(), false, GetAssembler());
133}
134void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
135 MoveIntToFP(invoke->GetLocations(), false, GetAssembler());
136}
137
138static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
139 LocationSummary* locations = new (arena) LocationSummary(invoke,
140 LocationSummary::kNoCall,
141 kIntrinsified);
142 locations->SetInAt(0, Location::RequiresRegister());
143 locations->SetOut(Location::SameAsFirstInput());
144}
145
146static void GenReverseBytes(LocationSummary* locations,
147 Primitive::Type size,
148 X86_64Assembler* assembler) {
149 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
150
151 switch (size) {
152 case Primitive::kPrimShort:
153 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
154 __ bswapl(out);
155 __ sarl(out, Immediate(16));
156 break;
157 case Primitive::kPrimInt:
158 __ bswapl(out);
159 break;
160 case Primitive::kPrimLong:
161 __ bswapq(out);
162 break;
163 default:
164 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
165 UNREACHABLE();
166 }
167}
168
169void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
170 CreateIntToIntLocations(arena_, invoke);
171}
172
173void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
174 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
175}
176
177void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
178 CreateIntToIntLocations(arena_, invoke);
179}
180
181void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
182 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
183}
184
185void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
186 CreateIntToIntLocations(arena_, invoke);
187}
188
189void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
190 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
191}
192
193
194// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
195// need is 64b.
196
197static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
198 // TODO: Enable memory operations when the assembler supports them.
199 LocationSummary* locations = new (arena) LocationSummary(invoke,
200 LocationSummary::kNoCall,
201 kIntrinsified);
202 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800203 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400204 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800205}
206
Mark Mendell39dcf552015-04-09 20:42:42 -0400207static void MathAbsFP(LocationSummary* locations,
208 bool is64bit,
209 X86_64Assembler* assembler,
210 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800211 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800212
Mark Mendellcfa410b2015-05-25 16:02:44 -0400213 DCHECK(output.IsFpuRegister());
214 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800215
Mark Mendellcfa410b2015-05-25 16:02:44 -0400216 // TODO: Can mask directly with constant area using pand if we can guarantee
217 // that the literal is aligned on a 16 byte boundary. This will avoid a
218 // temporary.
219 if (is64bit) {
220 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
221 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800222 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400223 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
224 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800225 }
226}
227
228void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
229 CreateFloatToFloatPlusTemps(arena_, invoke);
230}
231
232void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400233 MathAbsFP(invoke->GetLocations(), true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800234}
235
236void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
237 CreateFloatToFloatPlusTemps(arena_, invoke);
238}
239
240void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400241 MathAbsFP(invoke->GetLocations(), false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800242}
243
244static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
245 LocationSummary* locations = new (arena) LocationSummary(invoke,
246 LocationSummary::kNoCall,
247 kIntrinsified);
248 locations->SetInAt(0, Location::RequiresRegister());
249 locations->SetOut(Location::SameAsFirstInput());
250 locations->AddTemp(Location::RequiresRegister());
251}
252
253static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
254 Location output = locations->Out();
255 CpuRegister out = output.AsRegister<CpuRegister>();
256 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
257
258 if (is64bit) {
259 // Create mask.
260 __ movq(mask, out);
261 __ sarq(mask, Immediate(63));
262 // Add mask.
263 __ addq(out, mask);
264 __ xorq(out, mask);
265 } else {
266 // Create mask.
267 __ movl(mask, out);
268 __ sarl(mask, Immediate(31));
269 // Add mask.
270 __ addl(out, mask);
271 __ xorl(out, mask);
272 }
273}
274
275void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
276 CreateIntToIntPlusTemp(arena_, invoke);
277}
278
279void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
280 GenAbsInteger(invoke->GetLocations(), false, GetAssembler());
281}
282
283void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
284 CreateIntToIntPlusTemp(arena_, invoke);
285}
286
287void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
288 GenAbsInteger(invoke->GetLocations(), true, GetAssembler());
289}
290
Mark Mendell39dcf552015-04-09 20:42:42 -0400291static void GenMinMaxFP(LocationSummary* locations,
292 bool is_min,
293 bool is_double,
294 X86_64Assembler* assembler,
295 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800296 Location op1_loc = locations->InAt(0);
297 Location op2_loc = locations->InAt(1);
298 Location out_loc = locations->Out();
299 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
300
301 // Shortcut for same input locations.
302 if (op1_loc.Equals(op2_loc)) {
303 DCHECK(out_loc.Equals(op1_loc));
304 return;
305 }
306
307 // (out := op1)
308 // out <=? op2
309 // if Nan jmp Nan_label
310 // if out is min jmp done
311 // if op2 is min jmp op2_label
312 // handle -0/+0
313 // jmp done
314 // Nan_label:
315 // out := NaN
316 // op2_label:
317 // out := op2
318 // done:
319 //
320 // This removes one jmp, but needs to copy one input (op1) to out.
321 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400322 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800323
324 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
325
Mark Mendell0c9497d2015-08-21 09:30:05 -0400326 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800327 if (is_double) {
328 __ ucomisd(out, op2);
329 } else {
330 __ ucomiss(out, op2);
331 }
332
333 __ j(Condition::kParityEven, &nan);
334
335 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
336 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
337
338 // Handle 0.0/-0.0.
339 if (is_min) {
340 if (is_double) {
341 __ orpd(out, op2);
342 } else {
343 __ orps(out, op2);
344 }
345 } else {
346 if (is_double) {
347 __ andpd(out, op2);
348 } else {
349 __ andps(out, op2);
350 }
351 }
352 __ jmp(&done);
353
354 // NaN handling.
355 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800356 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400357 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800358 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400359 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800360 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800361 __ jmp(&done);
362
363 // out := op2;
364 __ Bind(&op2_label);
365 if (is_double) {
366 __ movsd(out, op2);
367 } else {
368 __ movss(out, op2);
369 }
370
371 // Done.
372 __ Bind(&done);
373}
374
Mark Mendellf55c3e02015-03-26 21:07:46 -0400375static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800376 LocationSummary* locations = new (arena) LocationSummary(invoke,
377 LocationSummary::kNoCall,
378 kIntrinsified);
379 locations->SetInAt(0, Location::RequiresFpuRegister());
380 locations->SetInAt(1, Location::RequiresFpuRegister());
381 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
382 // the second input to be the output (we can simply swap inputs).
383 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800384}
385
386void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(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::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400391 GenMinMaxFP(invoke->GetLocations(), true, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800392}
393
394void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(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::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400399 GenMinMaxFP(invoke->GetLocations(), true, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800400}
401
402void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400403 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800404}
405
406void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400407 GenMinMaxFP(invoke->GetLocations(), false, true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800408}
409
410void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400411 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800412}
413
414void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400415 GenMinMaxFP(invoke->GetLocations(), false, false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800416}
417
418static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
419 X86_64Assembler* assembler) {
420 Location op1_loc = locations->InAt(0);
421 Location op2_loc = locations->InAt(1);
422
423 // Shortcut for same input locations.
424 if (op1_loc.Equals(op2_loc)) {
425 // Can return immediately, as op1_loc == out_loc.
426 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
427 // a copy here.
428 DCHECK(locations->Out().Equals(op1_loc));
429 return;
430 }
431
432 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
433 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
434
435 // (out := op1)
436 // out <=? op2
437 // if out is min jmp done
438 // out := op2
439 // done:
440
441 if (is_long) {
442 __ cmpq(out, op2);
443 } else {
444 __ cmpl(out, op2);
445 }
446
447 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
448}
449
450static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
451 LocationSummary* locations = new (arena) LocationSummary(invoke,
452 LocationSummary::kNoCall,
453 kIntrinsified);
454 locations->SetInAt(0, Location::RequiresRegister());
455 locations->SetInAt(1, Location::RequiresRegister());
456 locations->SetOut(Location::SameAsFirstInput());
457}
458
459void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
460 CreateIntIntToIntLocations(arena_, invoke);
461}
462
463void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
464 GenMinMax(invoke->GetLocations(), true, false, GetAssembler());
465}
466
467void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
468 CreateIntIntToIntLocations(arena_, invoke);
469}
470
471void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
472 GenMinMax(invoke->GetLocations(), true, true, GetAssembler());
473}
474
475void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
476 CreateIntIntToIntLocations(arena_, invoke);
477}
478
479void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
480 GenMinMax(invoke->GetLocations(), false, false, GetAssembler());
481}
482
483void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
484 CreateIntIntToIntLocations(arena_, invoke);
485}
486
487void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
488 GenMinMax(invoke->GetLocations(), false, true, GetAssembler());
489}
490
491static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
492 LocationSummary* locations = new (arena) LocationSummary(invoke,
493 LocationSummary::kNoCall,
494 kIntrinsified);
495 locations->SetInAt(0, Location::RequiresFpuRegister());
496 locations->SetOut(Location::RequiresFpuRegister());
497}
498
499void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
500 CreateFPToFPLocations(arena_, invoke);
501}
502
503void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
504 LocationSummary* locations = invoke->GetLocations();
505 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
506 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
507
508 GetAssembler()->sqrtsd(out, in);
509}
510
Mark Mendellfb8d2792015-03-31 22:16:59 -0400511static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100512 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400513
514 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100515 codegen->GenerateStaticOrDirectCall(
516 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400517 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
518
519 // Copy the result back to the expected output.
520 Location out = invoke->GetLocations()->Out();
521 if (out.IsValid()) {
522 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700523 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400524 }
525}
526
527static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
528 HInvoke* invoke,
529 CodeGeneratorX86_64* codegen) {
530 // Do we have instruction support?
531 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
532 CreateFPToFPLocations(arena, invoke);
533 return;
534 }
535
536 // We have to fall back to a call to the intrinsic.
537 LocationSummary* locations = new (arena) LocationSummary(invoke,
538 LocationSummary::kCall);
539 InvokeRuntimeCallingConvention calling_convention;
540 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
541 locations->SetOut(Location::FpuRegisterLocation(XMM0));
542 // Needs to be RDI for the invoke.
543 locations->AddTemp(Location::RegisterLocation(RDI));
544}
545
546static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
547 HInvoke* invoke,
548 X86_64Assembler* assembler,
549 int round_mode) {
550 LocationSummary* locations = invoke->GetLocations();
551 if (locations->WillCall()) {
552 InvokeOutOfLineIntrinsic(codegen, invoke);
553 } else {
554 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
555 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
556 __ roundsd(out, in, Immediate(round_mode));
557 }
558}
559
560void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
561 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
562}
563
564void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
565 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
566}
567
568void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
569 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
570}
571
572void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
573 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
574}
575
576void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
577 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
578}
579
580void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
581 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
582}
583
584static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
585 HInvoke* invoke,
586 CodeGeneratorX86_64* codegen) {
587 // Do we have instruction support?
588 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
589 LocationSummary* locations = new (arena) LocationSummary(invoke,
590 LocationSummary::kNoCall,
591 kIntrinsified);
592 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600593 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400594 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400595 return;
596 }
597
598 // We have to fall back to a call to the intrinsic.
599 LocationSummary* locations = new (arena) LocationSummary(invoke,
600 LocationSummary::kCall);
601 InvokeRuntimeCallingConvention calling_convention;
602 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
603 locations->SetOut(Location::RegisterLocation(RAX));
604 // Needs to be RDI for the invoke.
605 locations->AddTemp(Location::RegisterLocation(RDI));
606}
607
608void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
609 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
610}
611
612void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
613 LocationSummary* locations = invoke->GetLocations();
614 if (locations->WillCall()) {
615 InvokeOutOfLineIntrinsic(codegen_, invoke);
616 return;
617 }
618
619 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
620 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
621 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400622 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400623 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400624 X86_64Assembler* assembler = GetAssembler();
625
Mark Mendell40741f32015-04-20 22:10:34 -0400626 // Load 0.5 into inPlusPointFive.
627 __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400628
629 // Add in the input.
630 __ addss(inPlusPointFive, in);
631
632 // And truncate to an integer.
633 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
634
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600635 // Load maxInt into out.
636 codegen_->Load64BitValue(out, kPrimIntMax);
637
Mark Mendellfb8d2792015-03-31 22:16:59 -0400638 // if inPlusPointFive >= maxInt goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400639 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400640 __ j(kAboveEqual, &done);
641
642 // if input == NaN goto nan
643 __ j(kUnordered, &nan);
644
645 // output = float-to-int-truncate(input)
646 __ cvttss2si(out, inPlusPointFive);
647 __ jmp(&done);
648 __ Bind(&nan);
649
650 // output = 0
651 __ xorl(out, out);
652 __ Bind(&done);
653}
654
655void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
656 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
657}
658
659void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
660 LocationSummary* locations = invoke->GetLocations();
661 if (locations->WillCall()) {
662 InvokeOutOfLineIntrinsic(codegen_, invoke);
663 return;
664 }
665
666 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
667 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
668 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400669 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400670 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400671 X86_64Assembler* assembler = GetAssembler();
672
Mark Mendell40741f32015-04-20 22:10:34 -0400673 // Load 0.5 into inPlusPointFive.
674 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400675
676 // Add in the input.
677 __ addsd(inPlusPointFive, in);
678
679 // And truncate to an integer.
680 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
681
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600682 // Load maxLong into out.
683 codegen_->Load64BitValue(out, kPrimLongMax);
684
Mark Mendellfb8d2792015-03-31 22:16:59 -0400685 // if inPlusPointFive >= maxLong goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400686 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400687 __ j(kAboveEqual, &done);
688
689 // if input == NaN goto nan
690 __ j(kUnordered, &nan);
691
692 // output = double-to-long-truncate(input)
693 __ cvttsd2si(out, inPlusPointFive, true);
694 __ jmp(&done);
695 __ Bind(&nan);
696
697 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400698 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400699 __ Bind(&done);
700}
701
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800702void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) {
703 // The inputs plus one temp.
704 LocationSummary* locations = new (arena_) LocationSummary(invoke,
705 LocationSummary::kCallOnSlowPath,
706 kIntrinsified);
707 locations->SetInAt(0, Location::RequiresRegister());
708 locations->SetInAt(1, Location::RequiresRegister());
709 locations->SetOut(Location::SameAsFirstInput());
710 locations->AddTemp(Location::RequiresRegister());
711}
712
713void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) {
714 LocationSummary* locations = invoke->GetLocations();
715
Mark Mendell6bc53a92015-07-01 14:26:52 -0400716 // Location of reference to data array.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800717 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400718 // Location of count.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800719 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800720
721 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
722 CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>();
723 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800724
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800725 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
726 // the cost.
727 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
728 // we will not optimize the code for constants (which would save a register).
729
Andreas Gampe85b62f22015-09-09 13:15:38 -0700730 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800731 codegen_->AddSlowPath(slow_path);
732
733 X86_64Assembler* assembler = GetAssembler();
734
735 __ cmpl(idx, Address(obj, count_offset));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800736 codegen_->MaybeRecordImplicitNullCheck(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800737 __ j(kAboveEqual, slow_path->GetEntryLabel());
738
Jeff Hao848f70a2014-01-15 13:49:50 -0800739 // out = out[2*idx].
740 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800741
742 __ Bind(slow_path->GetExitLabel());
743}
744
Mark Mendell6bc53a92015-07-01 14:26:52 -0400745void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
746 // Check to see if we have known failures that will cause us to have to bail out
747 // to the runtime, and just generate the runtime call directly.
748 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
749 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
750
751 // The positions must be non-negative.
752 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
753 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
754 // We will have to fail anyways.
755 return;
756 }
757
758 // The length must be > 0.
759 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
760 if (length != nullptr) {
761 int32_t len = length->GetValue();
762 if (len < 0) {
763 // Just call as normal.
764 return;
765 }
766 }
767
768 LocationSummary* locations = new (arena_) LocationSummary(invoke,
769 LocationSummary::kCallOnSlowPath,
770 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100771 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400772 locations->SetInAt(0, Location::RequiresRegister());
773 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
774 locations->SetInAt(2, Location::RequiresRegister());
775 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
776 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
777
778 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
779 locations->AddTemp(Location::RegisterLocation(RSI));
780 locations->AddTemp(Location::RegisterLocation(RDI));
781 locations->AddTemp(Location::RegisterLocation(RCX));
782}
783
784static void CheckPosition(X86_64Assembler* assembler,
785 Location pos,
786 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100787 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700788 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400789 CpuRegister input_len,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100790 CpuRegister temp,
791 bool length_is_input_length = false) {
792 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400793 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
794
795 if (pos.IsConstant()) {
796 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
797 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100798 if (!length_is_input_length) {
799 // Check that length(input) >= length.
800 if (length.IsConstant()) {
801 __ cmpl(Address(input, length_offset),
802 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
803 } else {
804 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
805 }
806 __ j(kLess, slow_path->GetEntryLabel());
807 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400808 } else {
809 // Check that length(input) >= pos.
810 __ movl(input_len, Address(input, length_offset));
811 __ cmpl(input_len, Immediate(pos_const));
812 __ j(kLess, slow_path->GetEntryLabel());
813
814 // Check that (length(input) - pos) >= length.
815 __ leal(temp, Address(input_len, -pos_const));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100816 if (length.IsConstant()) {
817 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
818 } else {
819 __ cmpl(temp, length.AsRegister<CpuRegister>());
820 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400821 __ j(kLess, slow_path->GetEntryLabel());
822 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100823 } else if (length_is_input_length) {
824 // The only way the copy can succeed is if pos is zero.
825 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
826 __ testl(pos_reg, pos_reg);
827 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400828 } else {
829 // Check that pos >= 0.
830 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
831 __ testl(pos_reg, pos_reg);
832 __ j(kLess, slow_path->GetEntryLabel());
833
834 // Check that pos <= length(input).
835 __ cmpl(Address(input, length_offset), pos_reg);
836 __ j(kLess, slow_path->GetEntryLabel());
837
838 // Check that (length(input) - pos) >= length.
839 __ movl(temp, Address(input, length_offset));
840 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100841 if (length.IsConstant()) {
842 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
843 } else {
844 __ cmpl(temp, length.AsRegister<CpuRegister>());
845 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400846 __ j(kLess, slow_path->GetEntryLabel());
847 }
848}
849
850void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
851 X86_64Assembler* assembler = GetAssembler();
852 LocationSummary* locations = invoke->GetLocations();
853
854 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100855 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400856 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100857 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400858 Location length = locations->InAt(4);
859
860 // Temporaries that we need for MOVSW.
861 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
862 DCHECK_EQ(src_base.AsRegister(), RSI);
863 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
864 DCHECK_EQ(dest_base.AsRegister(), RDI);
865 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
866 DCHECK_EQ(count.AsRegister(), RCX);
867
Andreas Gampe85b62f22015-09-09 13:15:38 -0700868 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400869 codegen_->AddSlowPath(slow_path);
870
871 // Bail out if the source and destination are the same.
872 __ cmpl(src, dest);
873 __ j(kEqual, slow_path->GetEntryLabel());
874
875 // Bail out if the source is null.
876 __ testl(src, src);
877 __ j(kEqual, slow_path->GetEntryLabel());
878
879 // Bail out if the destination is null.
880 __ testl(dest, dest);
881 __ j(kEqual, slow_path->GetEntryLabel());
882
883 // If the length is negative, bail out.
884 // We have already checked in the LocationsBuilder for the constant case.
885 if (!length.IsConstant()) {
886 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
887 __ j(kLess, slow_path->GetEntryLabel());
888 }
889
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100890 // Validity checks: source.
891 CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base);
892
893 // Validity checks: dest.
894 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base);
895
Mark Mendell6bc53a92015-07-01 14:26:52 -0400896 // We need the count in RCX.
897 if (length.IsConstant()) {
898 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
899 } else {
900 __ movl(count, length.AsRegister<CpuRegister>());
901 }
902
Mark Mendell6bc53a92015-07-01 14:26:52 -0400903 // Okay, everything checks out. Finally time to do the copy.
904 // Check assumption that sizeof(Char) is 2 (used in scaling below).
905 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
906 DCHECK_EQ(char_size, 2u);
907
908 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
909
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100910 if (src_pos.IsConstant()) {
911 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
912 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400913 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100914 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400915 ScaleFactor::TIMES_2, data_offset));
916 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100917 if (dest_pos.IsConstant()) {
918 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
919 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400920 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100921 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400922 ScaleFactor::TIMES_2, data_offset));
923 }
924
925 // Do the move.
926 __ rep_movsw();
927
928 __ Bind(slow_path->GetExitLabel());
929}
930
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100931
932void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100933 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100934}
935
Roland Levillain0d5a2812015-11-13 10:07:31 +0000936// TODO: Implement read barriers in the SystemArrayCopy intrinsic.
937// Note that this code path is not used (yet) because we do not
938// intrinsify methods that can go into the IntrinsicSlowPathX86_64
939// slow path.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100940void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
941 X86_64Assembler* assembler = GetAssembler();
942 LocationSummary* locations = invoke->GetLocations();
943
944 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
945 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
946 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
947 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
948
949 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
950 Location src_pos = locations->InAt(1);
951 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
952 Location dest_pos = locations->InAt(3);
953 Location length = locations->InAt(4);
954 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
955 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
956 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
957
958 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
959 codegen_->AddSlowPath(slow_path);
960
961 NearLabel ok;
962 SystemArrayCopyOptimizations optimizations(invoke);
963
964 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100965 if (!src_pos.IsConstant() || !dest_pos.IsConstant()) {
966 __ cmpl(src, dest);
967 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100968 }
969
970 // If source and destination are the same, we go to slow path if we need to do
971 // forward copying.
972 if (src_pos.IsConstant()) {
973 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
974 if (dest_pos.IsConstant()) {
975 // Checked when building locations.
976 DCHECK(!optimizations.GetDestinationIsSource()
977 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
978 } else {
979 if (!optimizations.GetDestinationIsSource()) {
980 __ j(kNotEqual, &ok);
981 }
982 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
983 __ j(kGreater, slow_path->GetEntryLabel());
984 }
985 } else {
986 if (!optimizations.GetDestinationIsSource()) {
987 __ j(kNotEqual, &ok);
988 }
989 if (dest_pos.IsConstant()) {
990 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
991 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
992 __ j(kLess, slow_path->GetEntryLabel());
993 } else {
994 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
995 __ j(kLess, slow_path->GetEntryLabel());
996 }
997 }
998
999 __ Bind(&ok);
1000
1001 if (!optimizations.GetSourceIsNotNull()) {
1002 // Bail out if the source is null.
1003 __ testl(src, src);
1004 __ j(kEqual, slow_path->GetEntryLabel());
1005 }
1006
1007 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1008 // Bail out if the destination is null.
1009 __ testl(dest, dest);
1010 __ j(kEqual, slow_path->GetEntryLabel());
1011 }
1012
1013 // If the length is negative, bail out.
1014 // We have already checked in the LocationsBuilder for the constant case.
1015 if (!length.IsConstant() &&
1016 !optimizations.GetCountIsSourceLength() &&
1017 !optimizations.GetCountIsDestinationLength()) {
1018 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1019 __ j(kLess, slow_path->GetEntryLabel());
1020 }
1021
1022 // Validity checks: source.
1023 CheckPosition(assembler,
1024 src_pos,
1025 src,
1026 length,
1027 slow_path,
1028 temp1,
1029 temp2,
1030 optimizations.GetCountIsSourceLength());
1031
1032 // Validity checks: dest.
1033 CheckPosition(assembler,
1034 dest_pos,
1035 dest,
1036 length,
1037 slow_path,
1038 temp1,
1039 temp2,
1040 optimizations.GetCountIsDestinationLength());
1041
1042 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1043 // Check whether all elements of the source array are assignable to the component
1044 // type of the destination array. We do two checks: the classes are the same,
1045 // or the destination is Object[]. If none of these checks succeed, we go to the
1046 // slow path.
1047 __ movl(temp1, Address(dest, class_offset));
1048 __ movl(temp2, Address(src, class_offset));
1049 bool did_unpoison = false;
1050 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1051 !optimizations.GetSourceIsNonPrimitiveArray()) {
1052 // One or two of the references need to be unpoisoned. Unpoisoned them
1053 // both to make the identity check valid.
1054 __ MaybeUnpoisonHeapReference(temp1);
1055 __ MaybeUnpoisonHeapReference(temp2);
1056 did_unpoison = true;
1057 }
1058
1059 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1060 // Bail out if the destination is not a non primitive array.
1061 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1062 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1063 __ j(kEqual, slow_path->GetEntryLabel());
1064 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1065 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1066 __ j(kNotEqual, slow_path->GetEntryLabel());
1067 }
1068
1069 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1070 // Bail out if the source is not a non primitive array.
1071 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1072 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1073 __ j(kEqual, slow_path->GetEntryLabel());
1074 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1075 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1076 __ j(kNotEqual, slow_path->GetEntryLabel());
1077 }
1078
1079 __ cmpl(temp1, temp2);
1080
1081 if (optimizations.GetDestinationIsTypedObjectArray()) {
1082 NearLabel do_copy;
1083 __ j(kEqual, &do_copy);
1084 if (!did_unpoison) {
1085 __ MaybeUnpoisonHeapReference(temp1);
1086 }
1087 __ movl(temp1, Address(temp1, component_offset));
1088 __ MaybeUnpoisonHeapReference(temp1);
1089 __ movl(temp1, Address(temp1, super_offset));
1090 // No need to unpoison the result, we're comparing against null.
1091 __ testl(temp1, temp1);
1092 __ j(kNotEqual, slow_path->GetEntryLabel());
1093 __ Bind(&do_copy);
1094 } else {
1095 __ j(kNotEqual, slow_path->GetEntryLabel());
1096 }
1097 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1098 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1099 // Bail out if the source is not a non primitive array.
1100 __ movl(temp1, Address(src, class_offset));
1101 __ MaybeUnpoisonHeapReference(temp1);
1102 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1103 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1104 __ j(kEqual, slow_path->GetEntryLabel());
1105 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1106 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1107 __ j(kNotEqual, slow_path->GetEntryLabel());
1108 }
1109
1110 // Compute base source address, base destination address, and end source address.
1111
1112 uint32_t element_size = sizeof(int32_t);
1113 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1114 if (src_pos.IsConstant()) {
1115 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1116 __ leal(temp1, Address(src, element_size * constant + offset));
1117 } else {
1118 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1119 }
1120
1121 if (dest_pos.IsConstant()) {
1122 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1123 __ leal(temp2, Address(dest, element_size * constant + offset));
1124 } else {
1125 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1126 }
1127
1128 if (length.IsConstant()) {
1129 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1130 __ leal(temp3, Address(temp1, element_size * constant));
1131 } else {
1132 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1133 }
1134
1135 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1136 // poison/unpoison, nor do any read barrier as the next uses of the destination
1137 // array will do it.
1138 NearLabel loop, done;
1139 __ cmpl(temp1, temp3);
1140 __ j(kEqual, &done);
1141 __ Bind(&loop);
1142 __ movl(CpuRegister(TMP), Address(temp1, 0));
1143 __ movl(Address(temp2, 0), CpuRegister(TMP));
1144 __ addl(temp1, Immediate(element_size));
1145 __ addl(temp2, Immediate(element_size));
1146 __ cmpl(temp1, temp3);
1147 __ j(kNotEqual, &loop);
1148 __ Bind(&done);
1149
1150 // We only need one card marking on the destination array.
1151 codegen_->MarkGCCard(temp1,
1152 temp2,
1153 dest,
1154 CpuRegister(kNoRegister),
1155 false);
1156
1157 __ Bind(slow_path->GetExitLabel());
1158}
1159
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001160void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1161 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1162 LocationSummary::kCall,
1163 kIntrinsified);
1164 InvokeRuntimeCallingConvention calling_convention;
1165 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1166 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1167 locations->SetOut(Location::RegisterLocation(RAX));
1168}
1169
1170void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1171 X86_64Assembler* assembler = GetAssembler();
1172 LocationSummary* locations = invoke->GetLocations();
1173
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001174 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001175 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001176
1177 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1178 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001179 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001180 codegen_->AddSlowPath(slow_path);
1181 __ j(kEqual, slow_path->GetEntryLabel());
1182
1183 __ gs()->call(Address::Absolute(
1184 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo), true));
1185 __ Bind(slow_path->GetExitLabel());
1186}
1187
Agi Csakif8cfb202015-08-13 17:54:54 -07001188void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1189 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1190 LocationSummary::kNoCall,
1191 kIntrinsified);
1192 locations->SetInAt(0, Location::RequiresRegister());
1193 locations->SetInAt(1, Location::RequiresRegister());
1194
1195 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1196 locations->AddTemp(Location::RegisterLocation(RCX));
1197 locations->AddTemp(Location::RegisterLocation(RDI));
1198
1199 // Set output, RSI needed for repe_cmpsq instruction anyways.
1200 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1201}
1202
1203void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1204 X86_64Assembler* assembler = GetAssembler();
1205 LocationSummary* locations = invoke->GetLocations();
1206
1207 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1208 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1209 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1210 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1211 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1212
Mark Mendell0c9497d2015-08-21 09:30:05 -04001213 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001214
1215 // Get offsets of count, value, and class fields within a string object.
1216 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1217 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1218 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1219
1220 // Note that the null check must have been done earlier.
1221 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1222
1223 // Check if input is null, return false if it is.
1224 __ testl(arg, arg);
1225 __ j(kEqual, &return_false);
1226
1227 // Instanceof check for the argument by comparing class fields.
1228 // All string objects must have the same type since String cannot be subclassed.
1229 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1230 // If the argument is a string object, its class field must be equal to receiver's class field.
1231 __ movl(rcx, Address(str, class_offset));
1232 __ cmpl(rcx, Address(arg, class_offset));
1233 __ j(kNotEqual, &return_false);
1234
1235 // Reference equality check, return true if same reference.
1236 __ cmpl(str, arg);
1237 __ j(kEqual, &return_true);
1238
1239 // Load length of receiver string.
1240 __ movl(rcx, Address(str, count_offset));
1241 // Check if lengths are equal, return false if they're not.
1242 __ cmpl(rcx, Address(arg, count_offset));
1243 __ j(kNotEqual, &return_false);
1244 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001245 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001246
1247 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1248 __ leal(rsi, Address(str, value_offset));
1249 __ leal(rdi, Address(arg, value_offset));
1250
1251 // Divide string length by 4 and adjust for lengths not divisible by 4.
1252 __ addl(rcx, Immediate(3));
1253 __ shrl(rcx, Immediate(2));
1254
1255 // Assertions that must hold in order to compare strings 4 characters at a time.
1256 DCHECK_ALIGNED(value_offset, 8);
1257 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1258
1259 // Loop to compare strings four characters at a time starting at the beginning of the string.
1260 __ repe_cmpsq();
1261 // If strings are not equal, zero flag will be cleared.
1262 __ j(kNotEqual, &return_false);
1263
1264 // Return true and exit the function.
1265 // If loop does not result in returning false, we return true.
1266 __ Bind(&return_true);
1267 __ movl(rsi, Immediate(1));
1268 __ jmp(&end);
1269
1270 // Return false and exit the function.
1271 __ Bind(&return_false);
1272 __ xorl(rsi, rsi);
1273 __ Bind(&end);
1274}
1275
Andreas Gampe21030dd2015-05-07 14:46:15 -07001276static void CreateStringIndexOfLocations(HInvoke* invoke,
1277 ArenaAllocator* allocator,
1278 bool start_at_zero) {
1279 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1280 LocationSummary::kCallOnSlowPath,
1281 kIntrinsified);
1282 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1283 locations->SetInAt(0, Location::RegisterLocation(RDI));
1284 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1285 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1286 // of the instruction explicitly.
1287 // Note: This works as we don't clobber RAX anywhere.
1288 locations->SetInAt(1, Location::RegisterLocation(RAX));
1289 if (!start_at_zero) {
1290 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1291 }
1292 // As we clobber RDI during execution anyways, also use it as the output.
1293 locations->SetOut(Location::SameAsFirstInput());
1294
1295 // repne scasw uses RCX as the counter.
1296 locations->AddTemp(Location::RegisterLocation(RCX));
1297 // Need another temporary to be able to compute the result.
1298 locations->AddTemp(Location::RequiresRegister());
1299}
1300
1301static void GenerateStringIndexOf(HInvoke* invoke,
1302 X86_64Assembler* assembler,
1303 CodeGeneratorX86_64* codegen,
1304 ArenaAllocator* allocator,
1305 bool start_at_zero) {
1306 LocationSummary* locations = invoke->GetLocations();
1307
1308 // Note that the null check must have been done earlier.
1309 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1310
1311 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1312 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1313 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1314 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1315 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1316
1317 // Check our assumptions for registers.
1318 DCHECK_EQ(string_obj.AsRegister(), RDI);
1319 DCHECK_EQ(search_value.AsRegister(), RAX);
1320 DCHECK_EQ(counter.AsRegister(), RCX);
1321 DCHECK_EQ(out.AsRegister(), RDI);
1322
1323 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1324 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001325 SlowPathCode* slow_path = nullptr;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001326 if (invoke->InputAt(1)->IsIntConstant()) {
1327 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1328 std::numeric_limits<uint16_t>::max()) {
1329 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1330 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1331 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1332 codegen->AddSlowPath(slow_path);
1333 __ jmp(slow_path->GetEntryLabel());
1334 __ Bind(slow_path->GetExitLabel());
1335 return;
1336 }
1337 } else {
1338 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1339 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1340 codegen->AddSlowPath(slow_path);
1341 __ j(kAbove, slow_path->GetEntryLabel());
1342 }
1343
1344 // From here down, we know that we are looking for a char that fits in 16 bits.
1345 // Location of reference to data array within the String object.
1346 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1347 // Location of count within the String object.
1348 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1349
1350 // Load string length, i.e., the count field of the string.
1351 __ movl(string_length, Address(string_obj, count_offset));
1352
1353 // Do a length check.
1354 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001355 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001356 __ testl(string_length, string_length);
1357 __ j(kEqual, &not_found_label);
1358
1359 if (start_at_zero) {
1360 // Number of chars to scan is the same as the string length.
1361 __ movl(counter, string_length);
1362
1363 // Move to the start of the string.
1364 __ addq(string_obj, Immediate(value_offset));
1365 } else {
1366 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1367
1368 // Do a start_index check.
1369 __ cmpl(start_index, string_length);
1370 __ j(kGreaterEqual, &not_found_label);
1371
1372 // Ensure we have a start index >= 0;
1373 __ xorl(counter, counter);
1374 __ cmpl(start_index, Immediate(0));
1375 __ cmov(kGreater, counter, start_index, false); // 32-bit copy is enough.
1376
1377 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1378 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1379
1380 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1381 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1382 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1383 }
1384
1385 // Everything is set up for repne scasw:
1386 // * Comparison address in RDI.
1387 // * Counter in ECX.
1388 __ repne_scasw();
1389
1390 // Did we find a match?
1391 __ j(kNotEqual, &not_found_label);
1392
1393 // Yes, we matched. Compute the index of the result.
1394 __ subl(string_length, counter);
1395 __ leal(out, Address(string_length, -1));
1396
Mark Mendell0c9497d2015-08-21 09:30:05 -04001397 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001398 __ jmp(&done);
1399
1400 // Failed to match; return -1.
1401 __ Bind(&not_found_label);
1402 __ movl(out, Immediate(-1));
1403
1404 // And join up at the end.
1405 __ Bind(&done);
1406 if (slow_path != nullptr) {
1407 __ Bind(slow_path->GetExitLabel());
1408 }
1409}
1410
1411void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
1412 CreateStringIndexOfLocations(invoke, arena_, true);
1413}
1414
1415void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
1416 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), true);
1417}
1418
1419void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1420 CreateStringIndexOfLocations(invoke, arena_, false);
1421}
1422
1423void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
1424 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), false);
1425}
1426
Jeff Hao848f70a2014-01-15 13:49:50 -08001427void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1428 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1429 LocationSummary::kCall,
1430 kIntrinsified);
1431 InvokeRuntimeCallingConvention calling_convention;
1432 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1433 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1434 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1435 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1436 locations->SetOut(Location::RegisterLocation(RAX));
1437}
1438
1439void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1440 X86_64Assembler* assembler = GetAssembler();
1441 LocationSummary* locations = invoke->GetLocations();
1442
1443 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1444 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001445 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001446 codegen_->AddSlowPath(slow_path);
1447 __ j(kEqual, slow_path->GetEntryLabel());
1448
1449 __ gs()->call(Address::Absolute(
1450 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes), true));
1451 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1452 __ Bind(slow_path->GetExitLabel());
1453}
1454
1455void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(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->SetOut(Location::RegisterLocation(RAX));
1464}
1465
1466void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1467 X86_64Assembler* assembler = GetAssembler();
1468
1469 __ gs()->call(Address::Absolute(
1470 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars), true));
1471 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1472}
1473
1474void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1475 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1476 LocationSummary::kCall,
1477 kIntrinsified);
1478 InvokeRuntimeCallingConvention calling_convention;
1479 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1480 locations->SetOut(Location::RegisterLocation(RAX));
1481}
1482
1483void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1484 X86_64Assembler* assembler = GetAssembler();
1485 LocationSummary* locations = invoke->GetLocations();
1486
1487 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1488 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001489 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001490 codegen_->AddSlowPath(slow_path);
1491 __ j(kEqual, slow_path->GetEntryLabel());
1492
1493 __ gs()->call(Address::Absolute(
1494 QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString), true));
1495 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1496 __ Bind(slow_path->GetExitLabel());
1497}
1498
Mark Mendell8f8926a2015-08-17 11:39:06 -04001499void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1500 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1501 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1502 LocationSummary::kNoCall,
1503 kIntrinsified);
1504 locations->SetInAt(0, Location::RequiresRegister());
1505 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1506 locations->SetInAt(2, Location::RequiresRegister());
1507 locations->SetInAt(3, Location::RequiresRegister());
1508 locations->SetInAt(4, Location::RequiresRegister());
1509
1510 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1511 locations->AddTemp(Location::RegisterLocation(RSI));
1512 locations->AddTemp(Location::RegisterLocation(RDI));
1513 locations->AddTemp(Location::RegisterLocation(RCX));
1514}
1515
1516void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1517 X86_64Assembler* assembler = GetAssembler();
1518 LocationSummary* locations = invoke->GetLocations();
1519
1520 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1521 // Location of data in char array buffer.
1522 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1523 // Location of char array data in string.
1524 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1525
1526 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1527 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1528 Location srcBegin = locations->InAt(1);
1529 int srcBegin_value =
1530 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1531 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1532 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1533 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1534
1535 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1536 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1537 DCHECK_EQ(char_size, 2u);
1538
1539 // Compute the address of the destination buffer.
1540 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1541
1542 // Compute the address of the source string.
1543 if (srcBegin.IsConstant()) {
1544 // Compute the address of the source string by adding the number of chars from
1545 // the source beginning to the value offset of a string.
1546 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1547 } else {
1548 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1549 ScaleFactor::TIMES_2, value_offset));
1550 }
1551
1552 // Compute the number of chars (words) to move.
1553 __ movl(CpuRegister(RCX), srcEnd);
1554 if (srcBegin.IsConstant()) {
1555 if (srcBegin_value != 0) {
1556 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1557 }
1558 } else {
1559 DCHECK(srcBegin.IsRegister());
1560 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1561 }
1562
1563 // Do the move.
1564 __ rep_movsw();
1565}
1566
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001567static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1568 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1569 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1570 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1571 // to avoid a SIGBUS.
1572 switch (size) {
1573 case Primitive::kPrimByte:
1574 __ movsxb(out, Address(address, 0));
1575 break;
1576 case Primitive::kPrimShort:
1577 __ movsxw(out, Address(address, 0));
1578 break;
1579 case Primitive::kPrimInt:
1580 __ movl(out, Address(address, 0));
1581 break;
1582 case Primitive::kPrimLong:
1583 __ movq(out, Address(address, 0));
1584 break;
1585 default:
1586 LOG(FATAL) << "Type not recognized for peek: " << size;
1587 UNREACHABLE();
1588 }
1589}
1590
1591void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1592 CreateIntToIntLocations(arena_, invoke);
1593}
1594
1595void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1596 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1597}
1598
1599void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1600 CreateIntToIntLocations(arena_, invoke);
1601}
1602
1603void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1604 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1605}
1606
1607void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1608 CreateIntToIntLocations(arena_, invoke);
1609}
1610
1611void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1612 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1613}
1614
1615void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1616 CreateIntToIntLocations(arena_, invoke);
1617}
1618
1619void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1620 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1621}
1622
1623static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1624 LocationSummary* locations = new (arena) LocationSummary(invoke,
1625 LocationSummary::kNoCall,
1626 kIntrinsified);
1627 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001628 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001629}
1630
1631static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1632 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001633 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001634 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1635 // to avoid a SIGBUS.
1636 switch (size) {
1637 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001638 if (value.IsConstant()) {
1639 __ movb(Address(address, 0),
1640 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1641 } else {
1642 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1643 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001644 break;
1645 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001646 if (value.IsConstant()) {
1647 __ movw(Address(address, 0),
1648 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1649 } else {
1650 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1651 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001652 break;
1653 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001654 if (value.IsConstant()) {
1655 __ movl(Address(address, 0),
1656 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1657 } else {
1658 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1659 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001660 break;
1661 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001662 if (value.IsConstant()) {
1663 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1664 DCHECK(IsInt<32>(v));
1665 int32_t v_32 = v;
1666 __ movq(Address(address, 0), Immediate(v_32));
1667 } else {
1668 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1669 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001670 break;
1671 default:
1672 LOG(FATAL) << "Type not recognized for poke: " << size;
1673 UNREACHABLE();
1674 }
1675}
1676
1677void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1678 CreateIntIntToVoidLocations(arena_, invoke);
1679}
1680
1681void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1682 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1683}
1684
1685void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1686 CreateIntIntToVoidLocations(arena_, invoke);
1687}
1688
1689void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1690 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1691}
1692
1693void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1694 CreateIntIntToVoidLocations(arena_, invoke);
1695}
1696
1697void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1698 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1699}
1700
1701void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1702 CreateIntIntToVoidLocations(arena_, invoke);
1703}
1704
1705void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1706 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1707}
1708
1709void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1710 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1711 LocationSummary::kNoCall,
1712 kIntrinsified);
1713 locations->SetOut(Location::RequiresRegister());
1714}
1715
1716void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1717 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
1718 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(), true));
1719}
1720
Roland Levillain0d5a2812015-11-13 10:07:31 +00001721static void GenUnsafeGet(HInvoke* invoke,
1722 Primitive::Type type,
1723 bool is_volatile ATTRIBUTE_UNUSED,
1724 CodeGeneratorX86_64* codegen) {
1725 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
1726 LocationSummary* locations = invoke->GetLocations();
1727 Location base_loc = locations->InAt(1);
1728 CpuRegister base = base_loc.AsRegister<CpuRegister>();
1729 Location offset_loc = locations->InAt(2);
1730 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
1731 Location output_loc = locations->Out();
1732 CpuRegister output = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001733
Andreas Gampe878d58c2015-01-15 23:24:00 -08001734 switch (type) {
1735 case Primitive::kPrimInt:
1736 case Primitive::kPrimNot:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001737 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001738 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001739 codegen->MaybeGenerateReadBarrier(invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
Roland Levillain4d027112015-07-01 15:41:14 +01001740 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001741 break;
1742
1743 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001744 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08001745 break;
1746
1747 default:
1748 LOG(FATAL) << "Unsupported op size " << type;
1749 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001750 }
1751}
1752
1753static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001754 bool can_call = kEmitCompilerReadBarrier &&
1755 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1756 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001757 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001758 can_call ?
1759 LocationSummary::kCallOnSlowPath :
1760 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001761 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001762 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001763 locations->SetInAt(1, Location::RequiresRegister());
1764 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001765 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001766}
1767
1768void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1769 CreateIntIntIntToIntLocations(arena_, invoke);
1770}
1771void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1772 CreateIntIntIntToIntLocations(arena_, invoke);
1773}
1774void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1775 CreateIntIntIntToIntLocations(arena_, invoke);
1776}
1777void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1778 CreateIntIntIntToIntLocations(arena_, invoke);
1779}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001780void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1781 CreateIntIntIntToIntLocations(arena_, invoke);
1782}
1783void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1784 CreateIntIntIntToIntLocations(arena_, invoke);
1785}
1786
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001787
1788void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001789 GenUnsafeGet(invoke, Primitive::kPrimInt, false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001790}
1791void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001792 GenUnsafeGet(invoke, Primitive::kPrimInt, true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001793}
1794void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001795 GenUnsafeGet(invoke, Primitive::kPrimLong, false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001796}
1797void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001798 GenUnsafeGet(invoke, Primitive::kPrimLong, true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001799}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001800void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001801 GenUnsafeGet(invoke, Primitive::kPrimNot, false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001802}
1803void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001804 GenUnsafeGet(invoke, Primitive::kPrimNot, true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001805}
1806
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001807
1808static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1809 Primitive::Type type,
1810 HInvoke* invoke) {
1811 LocationSummary* locations = new (arena) LocationSummary(invoke,
1812 LocationSummary::kNoCall,
1813 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001814 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001815 locations->SetInAt(1, Location::RequiresRegister());
1816 locations->SetInAt(2, Location::RequiresRegister());
1817 locations->SetInAt(3, Location::RequiresRegister());
1818 if (type == Primitive::kPrimNot) {
1819 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001820 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001821 locations->AddTemp(Location::RequiresRegister());
1822 }
1823}
1824
1825void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1826 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1827}
1828void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1829 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1830}
1831void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1832 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1833}
1834void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1835 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1836}
1837void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1838 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1839}
1840void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1841 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1842}
1843void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1844 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1845}
1846void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1847 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1848}
1849void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1850 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1851}
1852
1853// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1854// memory model.
1855static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1856 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001857 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001858 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1859 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1860 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1861
1862 if (type == Primitive::kPrimLong) {
1863 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001864 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1865 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1866 __ movl(temp, value);
1867 __ PoisonHeapReference(temp);
1868 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001869 } else {
1870 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1871 }
1872
1873 if (is_volatile) {
1874 __ mfence();
1875 }
1876
1877 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001878 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001879 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1880 locations->GetTemp(1).AsRegister<CpuRegister>(),
1881 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001882 value,
1883 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001884 }
1885}
1886
1887void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
1888 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1889}
1890void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1891 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, false, codegen_);
1892}
1893void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1894 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, true, codegen_);
1895}
1896void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1897 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1898}
1899void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1900 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, false, codegen_);
1901}
1902void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1903 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, true, codegen_);
1904}
1905void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1906 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1907}
1908void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1909 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, false, codegen_);
1910}
1911void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1912 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, true, codegen_);
1913}
1914
Mark Mendell58d25fd2015-04-03 14:52:31 -04001915static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1916 HInvoke* invoke) {
1917 LocationSummary* locations = new (arena) LocationSummary(invoke,
1918 LocationSummary::kNoCall,
1919 kIntrinsified);
1920 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1921 locations->SetInAt(1, Location::RequiresRegister());
1922 locations->SetInAt(2, Location::RequiresRegister());
1923 // expected value must be in EAX/RAX.
1924 locations->SetInAt(3, Location::RegisterLocation(RAX));
1925 locations->SetInAt(4, Location::RequiresRegister());
1926
1927 locations->SetOut(Location::RequiresRegister());
1928 if (type == Primitive::kPrimNot) {
1929 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01001930 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001931 locations->AddTemp(Location::RequiresRegister());
1932 }
1933}
1934
1935void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1936 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1937}
1938
1939void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1940 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1941}
1942
1943void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1944 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1945}
1946
1947static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001948 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04001949 LocationSummary* locations = invoke->GetLocations();
1950
1951 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1952 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1953 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01001954 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04001955 DCHECK_EQ(expected.AsRegister(), RAX);
1956 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1957 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1958
Roland Levillainb488b782015-10-22 11:38:49 +01001959 if (type == Primitive::kPrimNot) {
1960 // Mark card for object assuming new value is stored.
1961 bool value_can_be_null = true; // TODO: Worth finding out this information?
1962 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1963 locations->GetTemp(1).AsRegister<CpuRegister>(),
1964 base,
1965 value,
1966 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001967
Roland Levillainb488b782015-10-22 11:38:49 +01001968 bool base_equals_value = (base.AsRegister() == value.AsRegister());
1969 Register value_reg = value.AsRegister();
1970 if (kPoisonHeapReferences) {
1971 if (base_equals_value) {
1972 // If `base` and `value` are the same register location, move
1973 // `value_reg` to a temporary register. This way, poisoning
1974 // `value_reg` won't invalidate `base`.
1975 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
1976 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01001977 }
Roland Levillainb488b782015-10-22 11:38:49 +01001978
1979 // Check that the register allocator did not assign the location
1980 // of `expected` (RAX) to `value` nor to `base`, so that heap
1981 // poisoning (when enabled) works as intended below.
1982 // - If `value` were equal to `expected`, both references would
1983 // be poisoned twice, meaning they would not be poisoned at
1984 // all, as heap poisoning uses address negation.
1985 // - If `base` were equal to `expected`, poisoning `expected`
1986 // would invalidate `base`.
1987 DCHECK_NE(value_reg, expected.AsRegister());
1988 DCHECK_NE(base.AsRegister(), expected.AsRegister());
1989
1990 __ PoisonHeapReference(expected);
1991 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04001992 }
1993
Roland Levillainb488b782015-10-22 11:38:49 +01001994 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04001995
Roland Levillain0d5a2812015-11-13 10:07:31 +00001996 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01001997 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001998
Roland Levillainb488b782015-10-22 11:38:49 +01001999 // Convert ZF into the boolean result.
2000 __ setcc(kZero, out);
2001 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002002
Roland Levillain0d5a2812015-11-13 10:07:31 +00002003 // In the case of the `UnsafeCASObject` intrinsic, accessing an
2004 // object in the heap with LOCK CMPXCHG does not require a read
2005 // barrier, as we do not keep a reference to this heap location.
2006 // However, if heap poisoning is enabled, we need to unpoison the
2007 // values that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002008 if (kPoisonHeapReferences) {
2009 if (base_equals_value) {
2010 // `value_reg` has been moved to a temporary register, no need
2011 // to unpoison it.
2012 } else {
2013 // Ensure `value` is different from `out`, so that unpoisoning
2014 // the former does not invalidate the latter.
2015 DCHECK_NE(value_reg, out.AsRegister());
2016 __ UnpoisonHeapReference(CpuRegister(value_reg));
2017 }
2018 // Ensure `expected` is different from `out`, so that unpoisoning
2019 // the former does not invalidate the latter.
2020 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2021 __ UnpoisonHeapReference(expected);
2022 }
2023 } else {
2024 if (type == Primitive::kPrimInt) {
2025 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2026 } else if (type == Primitive::kPrimLong) {
2027 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2028 } else {
2029 LOG(FATAL) << "Unexpected CAS type " << type;
2030 }
2031
Roland Levillain0d5a2812015-11-13 10:07:31 +00002032 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002033 // scheduling barriers at this time.
2034
2035 // Convert ZF into the boolean result.
2036 __ setcc(kZero, out);
2037 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002038 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002039}
2040
2041void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2042 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2043}
2044
2045void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2046 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2047}
2048
2049void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
2050 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2051}
2052
2053void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2054 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2055 LocationSummary::kNoCall,
2056 kIntrinsified);
2057 locations->SetInAt(0, Location::RequiresRegister());
2058 locations->SetOut(Location::SameAsFirstInput());
2059 locations->AddTemp(Location::RequiresRegister());
2060}
2061
2062static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2063 X86_64Assembler* assembler) {
2064 Immediate imm_shift(shift);
2065 Immediate imm_mask(mask);
2066 __ movl(temp, reg);
2067 __ shrl(reg, imm_shift);
2068 __ andl(temp, imm_mask);
2069 __ andl(reg, imm_mask);
2070 __ shll(temp, imm_shift);
2071 __ orl(reg, temp);
2072}
2073
2074void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Roland Levillainb488b782015-10-22 11:38:49 +01002075 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002076 LocationSummary* locations = invoke->GetLocations();
2077
2078 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2079 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2080
2081 /*
2082 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2083 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2084 * compared to generic luni implementation which has 5 rounds of swapping bits.
2085 * x = bswap x
2086 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2087 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2088 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2089 */
2090 __ bswapl(reg);
2091 SwapBits(reg, temp, 1, 0x55555555, assembler);
2092 SwapBits(reg, temp, 2, 0x33333333, assembler);
2093 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2094}
2095
2096void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2097 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2098 LocationSummary::kNoCall,
2099 kIntrinsified);
2100 locations->SetInAt(0, Location::RequiresRegister());
2101 locations->SetOut(Location::SameAsFirstInput());
2102 locations->AddTemp(Location::RequiresRegister());
2103 locations->AddTemp(Location::RequiresRegister());
2104}
2105
2106static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2107 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2108 Immediate imm_shift(shift);
2109 __ movq(temp_mask, Immediate(mask));
2110 __ movq(temp, reg);
2111 __ shrq(reg, imm_shift);
2112 __ andq(temp, temp_mask);
2113 __ andq(reg, temp_mask);
2114 __ shlq(temp, imm_shift);
2115 __ orq(reg, temp);
2116}
2117
2118void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Roland Levillainb488b782015-10-22 11:38:49 +01002119 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002120 LocationSummary* locations = invoke->GetLocations();
2121
2122 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2123 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2124 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2125
2126 /*
2127 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2128 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2129 * compared to generic luni implementation which has 5 rounds of swapping bits.
2130 * x = bswap x
2131 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2132 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2133 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2134 */
2135 __ bswapq(reg);
2136 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2137 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2138 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2139}
2140
Mark Mendelld5897672015-08-12 21:16:41 -04002141static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2142 LocationSummary* locations = new (arena) LocationSummary(invoke,
2143 LocationSummary::kNoCall,
2144 kIntrinsified);
2145 locations->SetInAt(0, Location::Any());
2146 locations->SetOut(Location::RequiresRegister());
2147}
2148
2149static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2150 LocationSummary* locations = invoke->GetLocations();
2151 Location src = locations->InAt(0);
2152 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2153
2154 int zero_value_result = is_long ? 64 : 32;
2155 if (invoke->InputAt(0)->IsConstant()) {
2156 // Evaluate this at compile time.
2157 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2158 if (value == 0) {
2159 value = zero_value_result;
2160 } else {
2161 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2162 }
2163 if (value == 0) {
2164 __ xorl(out, out);
2165 } else {
2166 __ movl(out, Immediate(value));
2167 }
2168 return;
2169 }
2170
2171 // Handle the non-constant cases.
2172 if (src.IsRegister()) {
2173 if (is_long) {
2174 __ bsrq(out, src.AsRegister<CpuRegister>());
2175 } else {
2176 __ bsrl(out, src.AsRegister<CpuRegister>());
2177 }
2178 } else if (is_long) {
2179 DCHECK(src.IsDoubleStackSlot());
2180 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2181 } else {
2182 DCHECK(src.IsStackSlot());
2183 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2184 }
2185
2186 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002187 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002188 __ j(kEqual, &is_zero);
2189
2190 // Correct the result from BSR to get the CLZ result.
2191 __ xorl(out, Immediate(zero_value_result - 1));
2192 __ jmp(&done);
2193
2194 // Fix the zero case with the expected result.
2195 __ Bind(&is_zero);
2196 __ movl(out, Immediate(zero_value_result));
2197
2198 __ Bind(&done);
2199}
2200
2201void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2202 CreateLeadingZeroLocations(arena_, invoke);
2203}
2204
2205void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2206 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2207 GenLeadingZeros(assembler, invoke, /* is_long */ false);
2208}
2209
2210void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2211 CreateLeadingZeroLocations(arena_, invoke);
2212}
2213
2214void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2215 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2216 GenLeadingZeros(assembler, invoke, /* is_long */ true);
2217}
2218
Mark Mendell2d554792015-09-15 21:45:18 -04002219static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2220 LocationSummary* locations = new (arena) LocationSummary(invoke,
2221 LocationSummary::kNoCall,
2222 kIntrinsified);
2223 locations->SetInAt(0, Location::Any());
2224 locations->SetOut(Location::RequiresRegister());
2225}
2226
2227static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2228 LocationSummary* locations = invoke->GetLocations();
2229 Location src = locations->InAt(0);
2230 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2231
2232 int zero_value_result = is_long ? 64 : 32;
2233 if (invoke->InputAt(0)->IsConstant()) {
2234 // Evaluate this at compile time.
2235 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2236 if (value == 0) {
2237 value = zero_value_result;
2238 } else {
2239 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2240 }
2241 if (value == 0) {
2242 __ xorl(out, out);
2243 } else {
2244 __ movl(out, Immediate(value));
2245 }
2246 return;
2247 }
2248
2249 // Handle the non-constant cases.
2250 if (src.IsRegister()) {
2251 if (is_long) {
2252 __ bsfq(out, src.AsRegister<CpuRegister>());
2253 } else {
2254 __ bsfl(out, src.AsRegister<CpuRegister>());
2255 }
2256 } else if (is_long) {
2257 DCHECK(src.IsDoubleStackSlot());
2258 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2259 } else {
2260 DCHECK(src.IsStackSlot());
2261 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2262 }
2263
2264 // BSF sets ZF if the input was zero, and the output is undefined.
2265 NearLabel done;
2266 __ j(kNotEqual, &done);
2267
2268 // Fix the zero case with the expected result.
2269 __ movl(out, Immediate(zero_value_result));
2270
2271 __ Bind(&done);
2272}
2273
2274void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2275 CreateTrailingZeroLocations(arena_, invoke);
2276}
2277
2278void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2279 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2280 GenTrailingZeros(assembler, invoke, /* is_long */ false);
2281}
2282
2283void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2284 CreateTrailingZeroLocations(arena_, invoke);
2285}
2286
2287void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2288 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2289 GenTrailingZeros(assembler, invoke, /* is_long */ true);
2290}
2291
2292static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) {
2293 LocationSummary* locations = new (arena) LocationSummary(invoke,
2294 LocationSummary::kNoCall,
2295 kIntrinsified);
2296 locations->SetInAt(0, Location::RequiresRegister());
2297 // The shift count needs to be in CL or a constant.
2298 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1)));
2299 locations->SetOut(Location::SameAsFirstInput());
2300}
2301
2302static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) {
2303 LocationSummary* locations = invoke->GetLocations();
2304 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
2305 Location second = locations->InAt(1);
2306
2307 if (is_long) {
2308 if (second.IsRegister()) {
2309 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2310 if (is_left) {
2311 __ rolq(first_reg, second_reg);
2312 } else {
2313 __ rorq(first_reg, second_reg);
2314 }
2315 } else {
2316 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
2317 if (is_left) {
2318 __ rolq(first_reg, imm);
2319 } else {
2320 __ rorq(first_reg, imm);
2321 }
2322 }
2323 } else {
2324 if (second.IsRegister()) {
2325 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2326 if (is_left) {
2327 __ roll(first_reg, second_reg);
2328 } else {
2329 __ rorl(first_reg, second_reg);
2330 }
2331 } else {
2332 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2333 if (is_left) {
2334 __ roll(first_reg, imm);
2335 } else {
2336 __ rorl(first_reg, imm);
2337 }
2338 }
2339 }
2340}
2341
2342void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2343 CreateRotateLocations(arena_, invoke);
2344}
2345
2346void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2347 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2348 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true);
2349}
2350
2351void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2352 CreateRotateLocations(arena_, invoke);
2353}
2354
2355void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2356 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2357 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false);
2358}
2359
2360void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2361 CreateRotateLocations(arena_, invoke);
2362}
2363
2364void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2365 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2366 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true);
2367}
2368
2369void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) {
2370 CreateRotateLocations(arena_, invoke);
2371}
2372
2373void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) {
2374 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2375 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false);
2376}
2377
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002378// Unimplemented intrinsics.
2379
2380#define UNIMPLEMENTED_INTRINSIC(Name) \
2381void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2382} \
2383void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2384}
2385
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002386UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
2387
Roland Levillain4d027112015-07-01 15:41:14 +01002388#undef UNIMPLEMENTED_INTRINSIC
2389
2390#undef __
2391
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002392} // namespace x86_64
2393} // namespace art