blob: ecd129f31e78d6b0937e9dd3eb77d7a2b0823e02 [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) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000118 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800119}
120void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000121 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800122}
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) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000132 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800133}
134void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000135 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800136}
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) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000233 MathAbsFP(invoke->GetLocations(), /* is64bit */ 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) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000241 MathAbsFP(invoke->GetLocations(), /* is64bit */ 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) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000280 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800281}
282
283void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
284 CreateIntToIntPlusTemp(arena_, invoke);
285}
286
287void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000288 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800289}
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) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000391 GenMinMaxFP(
392 invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800393}
394
395void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400396 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800397}
398
399void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000400 GenMinMaxFP(
401 invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800402}
403
404void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400405 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800406}
407
408void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000409 GenMinMaxFP(
410 invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800411}
412
413void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400414 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800415}
416
417void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000418 GenMinMaxFP(
419 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800420}
421
422static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
423 X86_64Assembler* assembler) {
424 Location op1_loc = locations->InAt(0);
425 Location op2_loc = locations->InAt(1);
426
427 // Shortcut for same input locations.
428 if (op1_loc.Equals(op2_loc)) {
429 // Can return immediately, as op1_loc == out_loc.
430 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
431 // a copy here.
432 DCHECK(locations->Out().Equals(op1_loc));
433 return;
434 }
435
436 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
437 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
438
439 // (out := op1)
440 // out <=? op2
441 // if out is min jmp done
442 // out := op2
443 // done:
444
445 if (is_long) {
446 __ cmpq(out, op2);
447 } else {
448 __ cmpl(out, op2);
449 }
450
451 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
452}
453
454static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
455 LocationSummary* locations = new (arena) LocationSummary(invoke,
456 LocationSummary::kNoCall,
457 kIntrinsified);
458 locations->SetInAt(0, Location::RequiresRegister());
459 locations->SetInAt(1, Location::RequiresRegister());
460 locations->SetOut(Location::SameAsFirstInput());
461}
462
463void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
464 CreateIntIntToIntLocations(arena_, invoke);
465}
466
467void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000468 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800469}
470
471void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
472 CreateIntIntToIntLocations(arena_, invoke);
473}
474
475void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000476 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800477}
478
479void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
480 CreateIntIntToIntLocations(arena_, invoke);
481}
482
483void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000484 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800485}
486
487void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
488 CreateIntIntToIntLocations(arena_, invoke);
489}
490
491void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000492 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800493}
494
495static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
496 LocationSummary* locations = new (arena) LocationSummary(invoke,
497 LocationSummary::kNoCall,
498 kIntrinsified);
499 locations->SetInAt(0, Location::RequiresFpuRegister());
500 locations->SetOut(Location::RequiresFpuRegister());
501}
502
503void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
504 CreateFPToFPLocations(arena_, invoke);
505}
506
507void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
508 LocationSummary* locations = invoke->GetLocations();
509 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
510 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
511
512 GetAssembler()->sqrtsd(out, in);
513}
514
Mark Mendellfb8d2792015-03-31 22:16:59 -0400515static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100516 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400517
518 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100519 codegen->GenerateStaticOrDirectCall(
520 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400521 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
522
523 // Copy the result back to the expected output.
524 Location out = invoke->GetLocations()->Out();
525 if (out.IsValid()) {
526 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700527 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400528 }
529}
530
531static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
532 HInvoke* invoke,
533 CodeGeneratorX86_64* codegen) {
534 // Do we have instruction support?
535 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
536 CreateFPToFPLocations(arena, invoke);
537 return;
538 }
539
540 // We have to fall back to a call to the intrinsic.
541 LocationSummary* locations = new (arena) LocationSummary(invoke,
542 LocationSummary::kCall);
543 InvokeRuntimeCallingConvention calling_convention;
544 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
545 locations->SetOut(Location::FpuRegisterLocation(XMM0));
546 // Needs to be RDI for the invoke.
547 locations->AddTemp(Location::RegisterLocation(RDI));
548}
549
550static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
551 HInvoke* invoke,
552 X86_64Assembler* assembler,
553 int round_mode) {
554 LocationSummary* locations = invoke->GetLocations();
555 if (locations->WillCall()) {
556 InvokeOutOfLineIntrinsic(codegen, invoke);
557 } else {
558 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
559 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
560 __ roundsd(out, in, Immediate(round_mode));
561 }
562}
563
564void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
565 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
566}
567
568void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
569 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
570}
571
572void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
573 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
574}
575
576void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
577 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
578}
579
580void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
581 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
582}
583
584void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
585 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
586}
587
588static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
589 HInvoke* invoke,
590 CodeGeneratorX86_64* codegen) {
591 // Do we have instruction support?
592 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
593 LocationSummary* locations = new (arena) LocationSummary(invoke,
594 LocationSummary::kNoCall,
595 kIntrinsified);
596 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600597 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400598 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400599 return;
600 }
601
602 // We have to fall back to a call to the intrinsic.
603 LocationSummary* locations = new (arena) LocationSummary(invoke,
604 LocationSummary::kCall);
605 InvokeRuntimeCallingConvention calling_convention;
606 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
607 locations->SetOut(Location::RegisterLocation(RAX));
608 // Needs to be RDI for the invoke.
609 locations->AddTemp(Location::RegisterLocation(RDI));
610}
611
612void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
613 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
614}
615
616void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
617 LocationSummary* locations = invoke->GetLocations();
618 if (locations->WillCall()) {
619 InvokeOutOfLineIntrinsic(codegen_, invoke);
620 return;
621 }
622
623 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
624 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
625 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400626 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400627 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400628 X86_64Assembler* assembler = GetAssembler();
629
Mark Mendell40741f32015-04-20 22:10:34 -0400630 // Load 0.5 into inPlusPointFive.
631 __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400632
633 // Add in the input.
634 __ addss(inPlusPointFive, in);
635
636 // And truncate to an integer.
637 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
638
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600639 // Load maxInt into out.
640 codegen_->Load64BitValue(out, kPrimIntMax);
641
Mark Mendellfb8d2792015-03-31 22:16:59 -0400642 // if inPlusPointFive >= maxInt goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400643 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400644 __ j(kAboveEqual, &done);
645
646 // if input == NaN goto nan
647 __ j(kUnordered, &nan);
648
649 // output = float-to-int-truncate(input)
650 __ cvttss2si(out, inPlusPointFive);
651 __ jmp(&done);
652 __ Bind(&nan);
653
654 // output = 0
655 __ xorl(out, out);
656 __ Bind(&done);
657}
658
659void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
660 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
661}
662
663void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
664 LocationSummary* locations = invoke->GetLocations();
665 if (locations->WillCall()) {
666 InvokeOutOfLineIntrinsic(codegen_, invoke);
667 return;
668 }
669
670 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
671 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
672 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400673 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400674 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400675 X86_64Assembler* assembler = GetAssembler();
676
Mark Mendell40741f32015-04-20 22:10:34 -0400677 // Load 0.5 into inPlusPointFive.
678 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400679
680 // Add in the input.
681 __ addsd(inPlusPointFive, in);
682
683 // And truncate to an integer.
684 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
685
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600686 // Load maxLong into out.
687 codegen_->Load64BitValue(out, kPrimLongMax);
688
Mark Mendellfb8d2792015-03-31 22:16:59 -0400689 // if inPlusPointFive >= maxLong goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400690 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400691 __ j(kAboveEqual, &done);
692
693 // if input == NaN goto nan
694 __ j(kUnordered, &nan);
695
696 // output = double-to-long-truncate(input)
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000697 __ cvttsd2si(out, inPlusPointFive, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400698 __ jmp(&done);
699 __ Bind(&nan);
700
701 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400702 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400703 __ Bind(&done);
704}
705
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800706void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) {
707 // The inputs plus one temp.
708 LocationSummary* locations = new (arena_) LocationSummary(invoke,
709 LocationSummary::kCallOnSlowPath,
710 kIntrinsified);
711 locations->SetInAt(0, Location::RequiresRegister());
712 locations->SetInAt(1, Location::RequiresRegister());
713 locations->SetOut(Location::SameAsFirstInput());
714 locations->AddTemp(Location::RequiresRegister());
715}
716
717void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) {
718 LocationSummary* locations = invoke->GetLocations();
719
Mark Mendell6bc53a92015-07-01 14:26:52 -0400720 // Location of reference to data array.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800721 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400722 // Location of count.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800723 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800724
725 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
726 CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>();
727 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800728
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800729 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
730 // the cost.
731 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
732 // we will not optimize the code for constants (which would save a register).
733
Andreas Gampe85b62f22015-09-09 13:15:38 -0700734 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800735 codegen_->AddSlowPath(slow_path);
736
737 X86_64Assembler* assembler = GetAssembler();
738
739 __ cmpl(idx, Address(obj, count_offset));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800740 codegen_->MaybeRecordImplicitNullCheck(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800741 __ j(kAboveEqual, slow_path->GetEntryLabel());
742
Jeff Hao848f70a2014-01-15 13:49:50 -0800743 // out = out[2*idx].
744 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800745
746 __ Bind(slow_path->GetExitLabel());
747}
748
Mark Mendell6bc53a92015-07-01 14:26:52 -0400749void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
750 // Check to see if we have known failures that will cause us to have to bail out
751 // to the runtime, and just generate the runtime call directly.
752 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
753 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
754
755 // The positions must be non-negative.
756 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
757 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
758 // We will have to fail anyways.
759 return;
760 }
761
762 // The length must be > 0.
763 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
764 if (length != nullptr) {
765 int32_t len = length->GetValue();
766 if (len < 0) {
767 // Just call as normal.
768 return;
769 }
770 }
771
772 LocationSummary* locations = new (arena_) LocationSummary(invoke,
773 LocationSummary::kCallOnSlowPath,
774 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100775 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400776 locations->SetInAt(0, Location::RequiresRegister());
777 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
778 locations->SetInAt(2, Location::RequiresRegister());
779 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
780 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
781
782 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
783 locations->AddTemp(Location::RegisterLocation(RSI));
784 locations->AddTemp(Location::RegisterLocation(RDI));
785 locations->AddTemp(Location::RegisterLocation(RCX));
786}
787
788static void CheckPosition(X86_64Assembler* assembler,
789 Location pos,
790 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100791 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700792 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400793 CpuRegister input_len,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100794 CpuRegister temp,
795 bool length_is_input_length = false) {
796 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400797 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
798
799 if (pos.IsConstant()) {
800 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
801 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100802 if (!length_is_input_length) {
803 // Check that length(input) >= length.
804 if (length.IsConstant()) {
805 __ cmpl(Address(input, length_offset),
806 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
807 } else {
808 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
809 }
810 __ j(kLess, slow_path->GetEntryLabel());
811 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400812 } else {
813 // Check that length(input) >= pos.
814 __ movl(input_len, Address(input, length_offset));
815 __ cmpl(input_len, Immediate(pos_const));
816 __ j(kLess, slow_path->GetEntryLabel());
817
818 // Check that (length(input) - pos) >= length.
819 __ leal(temp, Address(input_len, -pos_const));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100820 if (length.IsConstant()) {
821 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
822 } else {
823 __ cmpl(temp, length.AsRegister<CpuRegister>());
824 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400825 __ j(kLess, slow_path->GetEntryLabel());
826 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100827 } else if (length_is_input_length) {
828 // The only way the copy can succeed is if pos is zero.
829 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
830 __ testl(pos_reg, pos_reg);
831 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400832 } else {
833 // Check that pos >= 0.
834 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
835 __ testl(pos_reg, pos_reg);
836 __ j(kLess, slow_path->GetEntryLabel());
837
838 // Check that pos <= length(input).
839 __ cmpl(Address(input, length_offset), pos_reg);
840 __ j(kLess, slow_path->GetEntryLabel());
841
842 // Check that (length(input) - pos) >= length.
843 __ movl(temp, Address(input, length_offset));
844 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100845 if (length.IsConstant()) {
846 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
847 } else {
848 __ cmpl(temp, length.AsRegister<CpuRegister>());
849 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400850 __ j(kLess, slow_path->GetEntryLabel());
851 }
852}
853
854void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
855 X86_64Assembler* assembler = GetAssembler();
856 LocationSummary* locations = invoke->GetLocations();
857
858 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100859 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400860 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100861 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400862 Location length = locations->InAt(4);
863
864 // Temporaries that we need for MOVSW.
865 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
866 DCHECK_EQ(src_base.AsRegister(), RSI);
867 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
868 DCHECK_EQ(dest_base.AsRegister(), RDI);
869 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
870 DCHECK_EQ(count.AsRegister(), RCX);
871
Andreas Gampe85b62f22015-09-09 13:15:38 -0700872 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400873 codegen_->AddSlowPath(slow_path);
874
875 // Bail out if the source and destination are the same.
876 __ cmpl(src, dest);
877 __ j(kEqual, slow_path->GetEntryLabel());
878
879 // Bail out if the source is null.
880 __ testl(src, src);
881 __ j(kEqual, slow_path->GetEntryLabel());
882
883 // Bail out if the destination is null.
884 __ testl(dest, dest);
885 __ j(kEqual, slow_path->GetEntryLabel());
886
887 // If the length is negative, bail out.
888 // We have already checked in the LocationsBuilder for the constant case.
889 if (!length.IsConstant()) {
890 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
891 __ j(kLess, slow_path->GetEntryLabel());
892 }
893
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100894 // Validity checks: source.
895 CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base);
896
897 // Validity checks: dest.
898 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base);
899
Mark Mendell6bc53a92015-07-01 14:26:52 -0400900 // We need the count in RCX.
901 if (length.IsConstant()) {
902 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
903 } else {
904 __ movl(count, length.AsRegister<CpuRegister>());
905 }
906
Mark Mendell6bc53a92015-07-01 14:26:52 -0400907 // Okay, everything checks out. Finally time to do the copy.
908 // Check assumption that sizeof(Char) is 2 (used in scaling below).
909 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
910 DCHECK_EQ(char_size, 2u);
911
912 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
913
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100914 if (src_pos.IsConstant()) {
915 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
916 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400917 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100918 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400919 ScaleFactor::TIMES_2, data_offset));
920 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100921 if (dest_pos.IsConstant()) {
922 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
923 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400924 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100925 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -0400926 ScaleFactor::TIMES_2, data_offset));
927 }
928
929 // Do the move.
930 __ rep_movsw();
931
932 __ Bind(slow_path->GetExitLabel());
933}
934
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100935
936void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100937 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100938}
939
Roland Levillain0d5a2812015-11-13 10:07:31 +0000940// TODO: Implement read barriers in the SystemArrayCopy intrinsic.
941// Note that this code path is not used (yet) because we do not
942// intrinsify methods that can go into the IntrinsicSlowPathX86_64
943// slow path.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100944void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
945 X86_64Assembler* assembler = GetAssembler();
946 LocationSummary* locations = invoke->GetLocations();
947
948 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
949 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
950 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
951 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
952
953 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
954 Location src_pos = locations->InAt(1);
955 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
956 Location dest_pos = locations->InAt(3);
957 Location length = locations->InAt(4);
958 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
959 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
960 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
961
962 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
963 codegen_->AddSlowPath(slow_path);
964
965 NearLabel ok;
966 SystemArrayCopyOptimizations optimizations(invoke);
967
968 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +0100969 if (!src_pos.IsConstant() || !dest_pos.IsConstant()) {
970 __ cmpl(src, dest);
971 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100972 }
973
974 // If source and destination are the same, we go to slow path if we need to do
975 // forward copying.
976 if (src_pos.IsConstant()) {
977 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
978 if (dest_pos.IsConstant()) {
979 // Checked when building locations.
980 DCHECK(!optimizations.GetDestinationIsSource()
981 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
982 } else {
983 if (!optimizations.GetDestinationIsSource()) {
984 __ j(kNotEqual, &ok);
985 }
986 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
987 __ j(kGreater, slow_path->GetEntryLabel());
988 }
989 } else {
990 if (!optimizations.GetDestinationIsSource()) {
991 __ j(kNotEqual, &ok);
992 }
993 if (dest_pos.IsConstant()) {
994 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
995 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
996 __ j(kLess, slow_path->GetEntryLabel());
997 } else {
998 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
999 __ j(kLess, slow_path->GetEntryLabel());
1000 }
1001 }
1002
1003 __ Bind(&ok);
1004
1005 if (!optimizations.GetSourceIsNotNull()) {
1006 // Bail out if the source is null.
1007 __ testl(src, src);
1008 __ j(kEqual, slow_path->GetEntryLabel());
1009 }
1010
1011 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1012 // Bail out if the destination is null.
1013 __ testl(dest, dest);
1014 __ j(kEqual, slow_path->GetEntryLabel());
1015 }
1016
1017 // If the length is negative, bail out.
1018 // We have already checked in the LocationsBuilder for the constant case.
1019 if (!length.IsConstant() &&
1020 !optimizations.GetCountIsSourceLength() &&
1021 !optimizations.GetCountIsDestinationLength()) {
1022 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1023 __ j(kLess, slow_path->GetEntryLabel());
1024 }
1025
1026 // Validity checks: source.
1027 CheckPosition(assembler,
1028 src_pos,
1029 src,
1030 length,
1031 slow_path,
1032 temp1,
1033 temp2,
1034 optimizations.GetCountIsSourceLength());
1035
1036 // Validity checks: dest.
1037 CheckPosition(assembler,
1038 dest_pos,
1039 dest,
1040 length,
1041 slow_path,
1042 temp1,
1043 temp2,
1044 optimizations.GetCountIsDestinationLength());
1045
1046 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1047 // Check whether all elements of the source array are assignable to the component
1048 // type of the destination array. We do two checks: the classes are the same,
1049 // or the destination is Object[]. If none of these checks succeed, we go to the
1050 // slow path.
1051 __ movl(temp1, Address(dest, class_offset));
1052 __ movl(temp2, Address(src, class_offset));
1053 bool did_unpoison = false;
1054 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1055 !optimizations.GetSourceIsNonPrimitiveArray()) {
1056 // One or two of the references need to be unpoisoned. Unpoisoned them
1057 // both to make the identity check valid.
1058 __ MaybeUnpoisonHeapReference(temp1);
1059 __ MaybeUnpoisonHeapReference(temp2);
1060 did_unpoison = true;
1061 }
1062
1063 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1064 // Bail out if the destination is not a non primitive array.
1065 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1066 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1067 __ j(kEqual, slow_path->GetEntryLabel());
1068 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1069 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1070 __ j(kNotEqual, slow_path->GetEntryLabel());
1071 }
1072
1073 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1074 // Bail out if the source is not a non primitive array.
1075 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1076 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1077 __ j(kEqual, slow_path->GetEntryLabel());
1078 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1079 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1080 __ j(kNotEqual, slow_path->GetEntryLabel());
1081 }
1082
1083 __ cmpl(temp1, temp2);
1084
1085 if (optimizations.GetDestinationIsTypedObjectArray()) {
1086 NearLabel do_copy;
1087 __ j(kEqual, &do_copy);
1088 if (!did_unpoison) {
1089 __ MaybeUnpoisonHeapReference(temp1);
1090 }
1091 __ movl(temp1, Address(temp1, component_offset));
1092 __ MaybeUnpoisonHeapReference(temp1);
1093 __ movl(temp1, Address(temp1, super_offset));
1094 // No need to unpoison the result, we're comparing against null.
1095 __ testl(temp1, temp1);
1096 __ j(kNotEqual, slow_path->GetEntryLabel());
1097 __ Bind(&do_copy);
1098 } else {
1099 __ j(kNotEqual, slow_path->GetEntryLabel());
1100 }
1101 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1102 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1103 // Bail out if the source is not a non primitive array.
1104 __ movl(temp1, Address(src, class_offset));
1105 __ MaybeUnpoisonHeapReference(temp1);
1106 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1107 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1108 __ j(kEqual, slow_path->GetEntryLabel());
1109 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1110 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1111 __ j(kNotEqual, slow_path->GetEntryLabel());
1112 }
1113
1114 // Compute base source address, base destination address, and end source address.
1115
1116 uint32_t element_size = sizeof(int32_t);
1117 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1118 if (src_pos.IsConstant()) {
1119 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1120 __ leal(temp1, Address(src, element_size * constant + offset));
1121 } else {
1122 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1123 }
1124
1125 if (dest_pos.IsConstant()) {
1126 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1127 __ leal(temp2, Address(dest, element_size * constant + offset));
1128 } else {
1129 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1130 }
1131
1132 if (length.IsConstant()) {
1133 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1134 __ leal(temp3, Address(temp1, element_size * constant));
1135 } else {
1136 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1137 }
1138
1139 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1140 // poison/unpoison, nor do any read barrier as the next uses of the destination
1141 // array will do it.
1142 NearLabel loop, done;
1143 __ cmpl(temp1, temp3);
1144 __ j(kEqual, &done);
1145 __ Bind(&loop);
1146 __ movl(CpuRegister(TMP), Address(temp1, 0));
1147 __ movl(Address(temp2, 0), CpuRegister(TMP));
1148 __ addl(temp1, Immediate(element_size));
1149 __ addl(temp2, Immediate(element_size));
1150 __ cmpl(temp1, temp3);
1151 __ j(kNotEqual, &loop);
1152 __ Bind(&done);
1153
1154 // We only need one card marking on the destination array.
1155 codegen_->MarkGCCard(temp1,
1156 temp2,
1157 dest,
1158 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001159 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001160
1161 __ Bind(slow_path->GetExitLabel());
1162}
1163
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001164void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1165 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1166 LocationSummary::kCall,
1167 kIntrinsified);
1168 InvokeRuntimeCallingConvention calling_convention;
1169 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1170 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1171 locations->SetOut(Location::RegisterLocation(RAX));
1172}
1173
1174void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1175 X86_64Assembler* assembler = GetAssembler();
1176 LocationSummary* locations = invoke->GetLocations();
1177
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001178 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001179 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001180
1181 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1182 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001183 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001184 codegen_->AddSlowPath(slow_path);
1185 __ j(kEqual, slow_path->GetEntryLabel());
1186
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001187 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo),
1188 /* no_rip */ true));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001189 __ Bind(slow_path->GetExitLabel());
1190}
1191
Agi Csakif8cfb202015-08-13 17:54:54 -07001192void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1193 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1194 LocationSummary::kNoCall,
1195 kIntrinsified);
1196 locations->SetInAt(0, Location::RequiresRegister());
1197 locations->SetInAt(1, Location::RequiresRegister());
1198
1199 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1200 locations->AddTemp(Location::RegisterLocation(RCX));
1201 locations->AddTemp(Location::RegisterLocation(RDI));
1202
1203 // Set output, RSI needed for repe_cmpsq instruction anyways.
1204 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1205}
1206
1207void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1208 X86_64Assembler* assembler = GetAssembler();
1209 LocationSummary* locations = invoke->GetLocations();
1210
1211 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1212 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1213 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1214 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1215 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1216
Mark Mendell0c9497d2015-08-21 09:30:05 -04001217 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001218
1219 // Get offsets of count, value, and class fields within a string object.
1220 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1221 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1222 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1223
1224 // Note that the null check must have been done earlier.
1225 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1226
1227 // Check if input is null, return false if it is.
1228 __ testl(arg, arg);
1229 __ j(kEqual, &return_false);
1230
1231 // Instanceof check for the argument by comparing class fields.
1232 // All string objects must have the same type since String cannot be subclassed.
1233 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1234 // If the argument is a string object, its class field must be equal to receiver's class field.
1235 __ movl(rcx, Address(str, class_offset));
1236 __ cmpl(rcx, Address(arg, class_offset));
1237 __ j(kNotEqual, &return_false);
1238
1239 // Reference equality check, return true if same reference.
1240 __ cmpl(str, arg);
1241 __ j(kEqual, &return_true);
1242
1243 // Load length of receiver string.
1244 __ movl(rcx, Address(str, count_offset));
1245 // Check if lengths are equal, return false if they're not.
1246 __ cmpl(rcx, Address(arg, count_offset));
1247 __ j(kNotEqual, &return_false);
1248 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001249 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001250
1251 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1252 __ leal(rsi, Address(str, value_offset));
1253 __ leal(rdi, Address(arg, value_offset));
1254
1255 // Divide string length by 4 and adjust for lengths not divisible by 4.
1256 __ addl(rcx, Immediate(3));
1257 __ shrl(rcx, Immediate(2));
1258
1259 // Assertions that must hold in order to compare strings 4 characters at a time.
1260 DCHECK_ALIGNED(value_offset, 8);
1261 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1262
1263 // Loop to compare strings four characters at a time starting at the beginning of the string.
1264 __ repe_cmpsq();
1265 // If strings are not equal, zero flag will be cleared.
1266 __ j(kNotEqual, &return_false);
1267
1268 // Return true and exit the function.
1269 // If loop does not result in returning false, we return true.
1270 __ Bind(&return_true);
1271 __ movl(rsi, Immediate(1));
1272 __ jmp(&end);
1273
1274 // Return false and exit the function.
1275 __ Bind(&return_false);
1276 __ xorl(rsi, rsi);
1277 __ Bind(&end);
1278}
1279
Andreas Gampe21030dd2015-05-07 14:46:15 -07001280static void CreateStringIndexOfLocations(HInvoke* invoke,
1281 ArenaAllocator* allocator,
1282 bool start_at_zero) {
1283 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1284 LocationSummary::kCallOnSlowPath,
1285 kIntrinsified);
1286 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1287 locations->SetInAt(0, Location::RegisterLocation(RDI));
1288 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1289 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1290 // of the instruction explicitly.
1291 // Note: This works as we don't clobber RAX anywhere.
1292 locations->SetInAt(1, Location::RegisterLocation(RAX));
1293 if (!start_at_zero) {
1294 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1295 }
1296 // As we clobber RDI during execution anyways, also use it as the output.
1297 locations->SetOut(Location::SameAsFirstInput());
1298
1299 // repne scasw uses RCX as the counter.
1300 locations->AddTemp(Location::RegisterLocation(RCX));
1301 // Need another temporary to be able to compute the result.
1302 locations->AddTemp(Location::RequiresRegister());
1303}
1304
1305static void GenerateStringIndexOf(HInvoke* invoke,
1306 X86_64Assembler* assembler,
1307 CodeGeneratorX86_64* codegen,
1308 ArenaAllocator* allocator,
1309 bool start_at_zero) {
1310 LocationSummary* locations = invoke->GetLocations();
1311
1312 // Note that the null check must have been done earlier.
1313 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1314
1315 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1316 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1317 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1318 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1319 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1320
1321 // Check our assumptions for registers.
1322 DCHECK_EQ(string_obj.AsRegister(), RDI);
1323 DCHECK_EQ(search_value.AsRegister(), RAX);
1324 DCHECK_EQ(counter.AsRegister(), RCX);
1325 DCHECK_EQ(out.AsRegister(), RDI);
1326
1327 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
1328 // or directly dispatch if we have a constant.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001329 SlowPathCode* slow_path = nullptr;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001330 if (invoke->InputAt(1)->IsIntConstant()) {
1331 if (static_cast<uint32_t>(invoke->InputAt(1)->AsIntConstant()->GetValue()) >
1332 std::numeric_limits<uint16_t>::max()) {
1333 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1334 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1335 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1336 codegen->AddSlowPath(slow_path);
1337 __ jmp(slow_path->GetEntryLabel());
1338 __ Bind(slow_path->GetExitLabel());
1339 return;
1340 }
1341 } else {
1342 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1343 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1344 codegen->AddSlowPath(slow_path);
1345 __ j(kAbove, slow_path->GetEntryLabel());
1346 }
1347
1348 // From here down, we know that we are looking for a char that fits in 16 bits.
1349 // Location of reference to data array within the String object.
1350 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1351 // Location of count within the String object.
1352 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1353
1354 // Load string length, i.e., the count field of the string.
1355 __ movl(string_length, Address(string_obj, count_offset));
1356
1357 // Do a length check.
1358 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001359 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001360 __ testl(string_length, string_length);
1361 __ j(kEqual, &not_found_label);
1362
1363 if (start_at_zero) {
1364 // Number of chars to scan is the same as the string length.
1365 __ movl(counter, string_length);
1366
1367 // Move to the start of the string.
1368 __ addq(string_obj, Immediate(value_offset));
1369 } else {
1370 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1371
1372 // Do a start_index check.
1373 __ cmpl(start_index, string_length);
1374 __ j(kGreaterEqual, &not_found_label);
1375
1376 // Ensure we have a start index >= 0;
1377 __ xorl(counter, counter);
1378 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001379 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001380
1381 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1382 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1383
1384 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1385 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1386 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1387 }
1388
1389 // Everything is set up for repne scasw:
1390 // * Comparison address in RDI.
1391 // * Counter in ECX.
1392 __ repne_scasw();
1393
1394 // Did we find a match?
1395 __ j(kNotEqual, &not_found_label);
1396
1397 // Yes, we matched. Compute the index of the result.
1398 __ subl(string_length, counter);
1399 __ leal(out, Address(string_length, -1));
1400
Mark Mendell0c9497d2015-08-21 09:30:05 -04001401 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001402 __ jmp(&done);
1403
1404 // Failed to match; return -1.
1405 __ Bind(&not_found_label);
1406 __ movl(out, Immediate(-1));
1407
1408 // And join up at the end.
1409 __ Bind(&done);
1410 if (slow_path != nullptr) {
1411 __ Bind(slow_path->GetExitLabel());
1412 }
1413}
1414
1415void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001416 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001417}
1418
1419void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001420 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001421}
1422
1423void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001424 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001425}
1426
1427void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001428 GenerateStringIndexOf(
1429 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001430}
1431
Jeff Hao848f70a2014-01-15 13:49:50 -08001432void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1433 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1434 LocationSummary::kCall,
1435 kIntrinsified);
1436 InvokeRuntimeCallingConvention calling_convention;
1437 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1438 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1439 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1440 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1441 locations->SetOut(Location::RegisterLocation(RAX));
1442}
1443
1444void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1445 X86_64Assembler* assembler = GetAssembler();
1446 LocationSummary* locations = invoke->GetLocations();
1447
1448 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1449 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001450 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001451 codegen_->AddSlowPath(slow_path);
1452 __ j(kEqual, slow_path->GetEntryLabel());
1453
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001454 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes),
1455 /* no_rip */ true));
Jeff Hao848f70a2014-01-15 13:49:50 -08001456 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1457 __ Bind(slow_path->GetExitLabel());
1458}
1459
1460void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1461 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1462 LocationSummary::kCall,
1463 kIntrinsified);
1464 InvokeRuntimeCallingConvention calling_convention;
1465 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1466 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1467 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1468 locations->SetOut(Location::RegisterLocation(RAX));
1469}
1470
1471void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1472 X86_64Assembler* assembler = GetAssembler();
1473
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001474 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars),
1475 /* no_rip */ true));
Jeff Hao848f70a2014-01-15 13:49:50 -08001476 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1477}
1478
1479void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1480 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1481 LocationSummary::kCall,
1482 kIntrinsified);
1483 InvokeRuntimeCallingConvention calling_convention;
1484 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1485 locations->SetOut(Location::RegisterLocation(RAX));
1486}
1487
1488void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1489 X86_64Assembler* assembler = GetAssembler();
1490 LocationSummary* locations = invoke->GetLocations();
1491
1492 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1493 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001494 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001495 codegen_->AddSlowPath(slow_path);
1496 __ j(kEqual, slow_path->GetEntryLabel());
1497
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001498 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString),
1499 /* no_rip */ true));
Jeff Hao848f70a2014-01-15 13:49:50 -08001500 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1501 __ Bind(slow_path->GetExitLabel());
1502}
1503
Mark Mendell8f8926a2015-08-17 11:39:06 -04001504void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1505 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1506 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1507 LocationSummary::kNoCall,
1508 kIntrinsified);
1509 locations->SetInAt(0, Location::RequiresRegister());
1510 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1511 locations->SetInAt(2, Location::RequiresRegister());
1512 locations->SetInAt(3, Location::RequiresRegister());
1513 locations->SetInAt(4, Location::RequiresRegister());
1514
1515 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1516 locations->AddTemp(Location::RegisterLocation(RSI));
1517 locations->AddTemp(Location::RegisterLocation(RDI));
1518 locations->AddTemp(Location::RegisterLocation(RCX));
1519}
1520
1521void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1522 X86_64Assembler* assembler = GetAssembler();
1523 LocationSummary* locations = invoke->GetLocations();
1524
1525 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1526 // Location of data in char array buffer.
1527 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1528 // Location of char array data in string.
1529 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1530
1531 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1532 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1533 Location srcBegin = locations->InAt(1);
1534 int srcBegin_value =
1535 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1536 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1537 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1538 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1539
1540 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1541 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1542 DCHECK_EQ(char_size, 2u);
1543
1544 // Compute the address of the destination buffer.
1545 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1546
1547 // Compute the address of the source string.
1548 if (srcBegin.IsConstant()) {
1549 // Compute the address of the source string by adding the number of chars from
1550 // the source beginning to the value offset of a string.
1551 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1552 } else {
1553 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1554 ScaleFactor::TIMES_2, value_offset));
1555 }
1556
1557 // Compute the number of chars (words) to move.
1558 __ movl(CpuRegister(RCX), srcEnd);
1559 if (srcBegin.IsConstant()) {
1560 if (srcBegin_value != 0) {
1561 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1562 }
1563 } else {
1564 DCHECK(srcBegin.IsRegister());
1565 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1566 }
1567
1568 // Do the move.
1569 __ rep_movsw();
1570}
1571
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001572static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1573 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1574 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1575 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1576 // to avoid a SIGBUS.
1577 switch (size) {
1578 case Primitive::kPrimByte:
1579 __ movsxb(out, Address(address, 0));
1580 break;
1581 case Primitive::kPrimShort:
1582 __ movsxw(out, Address(address, 0));
1583 break;
1584 case Primitive::kPrimInt:
1585 __ movl(out, Address(address, 0));
1586 break;
1587 case Primitive::kPrimLong:
1588 __ movq(out, Address(address, 0));
1589 break;
1590 default:
1591 LOG(FATAL) << "Type not recognized for peek: " << size;
1592 UNREACHABLE();
1593 }
1594}
1595
1596void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1597 CreateIntToIntLocations(arena_, invoke);
1598}
1599
1600void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1601 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1602}
1603
1604void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1605 CreateIntToIntLocations(arena_, invoke);
1606}
1607
1608void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1609 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1610}
1611
1612void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1613 CreateIntToIntLocations(arena_, invoke);
1614}
1615
1616void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1617 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1618}
1619
1620void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1621 CreateIntToIntLocations(arena_, invoke);
1622}
1623
1624void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1625 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1626}
1627
1628static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1629 LocationSummary* locations = new (arena) LocationSummary(invoke,
1630 LocationSummary::kNoCall,
1631 kIntrinsified);
1632 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001633 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001634}
1635
1636static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1637 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001638 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001639 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1640 // to avoid a SIGBUS.
1641 switch (size) {
1642 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001643 if (value.IsConstant()) {
1644 __ movb(Address(address, 0),
1645 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1646 } else {
1647 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1648 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001649 break;
1650 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001651 if (value.IsConstant()) {
1652 __ movw(Address(address, 0),
1653 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1654 } else {
1655 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1656 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001657 break;
1658 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001659 if (value.IsConstant()) {
1660 __ movl(Address(address, 0),
1661 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1662 } else {
1663 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1664 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001665 break;
1666 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001667 if (value.IsConstant()) {
1668 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1669 DCHECK(IsInt<32>(v));
1670 int32_t v_32 = v;
1671 __ movq(Address(address, 0), Immediate(v_32));
1672 } else {
1673 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1674 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001675 break;
1676 default:
1677 LOG(FATAL) << "Type not recognized for poke: " << size;
1678 UNREACHABLE();
1679 }
1680}
1681
1682void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1683 CreateIntIntToVoidLocations(arena_, invoke);
1684}
1685
1686void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1687 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1688}
1689
1690void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1691 CreateIntIntToVoidLocations(arena_, invoke);
1692}
1693
1694void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1695 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1696}
1697
1698void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1699 CreateIntIntToVoidLocations(arena_, invoke);
1700}
1701
1702void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1703 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1704}
1705
1706void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1707 CreateIntIntToVoidLocations(arena_, invoke);
1708}
1709
1710void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1711 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1712}
1713
1714void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1715 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1716 LocationSummary::kNoCall,
1717 kIntrinsified);
1718 locations->SetOut(Location::RequiresRegister());
1719}
1720
1721void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1722 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001723 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(),
1724 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001725}
1726
Roland Levillain0d5a2812015-11-13 10:07:31 +00001727static void GenUnsafeGet(HInvoke* invoke,
1728 Primitive::Type type,
1729 bool is_volatile ATTRIBUTE_UNUSED,
1730 CodeGeneratorX86_64* codegen) {
1731 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
1732 LocationSummary* locations = invoke->GetLocations();
1733 Location base_loc = locations->InAt(1);
1734 CpuRegister base = base_loc.AsRegister<CpuRegister>();
1735 Location offset_loc = locations->InAt(2);
1736 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
1737 Location output_loc = locations->Out();
1738 CpuRegister output = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001739
Andreas Gampe878d58c2015-01-15 23:24:00 -08001740 switch (type) {
1741 case Primitive::kPrimInt:
1742 case Primitive::kPrimNot:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001743 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain4d027112015-07-01 15:41:14 +01001744 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001745 codegen->MaybeGenerateReadBarrier(invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
Roland Levillain4d027112015-07-01 15:41:14 +01001746 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001747 break;
1748
1749 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001750 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08001751 break;
1752
1753 default:
1754 LOG(FATAL) << "Unsupported op size " << type;
1755 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001756 }
1757}
1758
1759static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001760 bool can_call = kEmitCompilerReadBarrier &&
1761 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1762 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001763 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001764 can_call ?
1765 LocationSummary::kCallOnSlowPath :
1766 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001767 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001768 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001769 locations->SetInAt(1, Location::RequiresRegister());
1770 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001771 locations->SetOut(Location::RequiresRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001772}
1773
1774void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
1775 CreateIntIntIntToIntLocations(arena_, invoke);
1776}
1777void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
1778 CreateIntIntIntToIntLocations(arena_, invoke);
1779}
1780void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
1781 CreateIntIntIntToIntLocations(arena_, invoke);
1782}
1783void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
1784 CreateIntIntIntToIntLocations(arena_, invoke);
1785}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001786void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
1787 CreateIntIntIntToIntLocations(arena_, invoke);
1788}
1789void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
1790 CreateIntIntIntToIntLocations(arena_, invoke);
1791}
1792
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001793
1794void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001795 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001796}
1797void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001798 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001799}
1800void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001801 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001802}
1803void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001804 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001805}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001806void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001807 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001808}
1809void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001810 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001811}
1812
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001813
1814static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1815 Primitive::Type type,
1816 HInvoke* invoke) {
1817 LocationSummary* locations = new (arena) LocationSummary(invoke,
1818 LocationSummary::kNoCall,
1819 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001820 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001821 locations->SetInAt(1, Location::RequiresRegister());
1822 locations->SetInAt(2, Location::RequiresRegister());
1823 locations->SetInAt(3, Location::RequiresRegister());
1824 if (type == Primitive::kPrimNot) {
1825 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001826 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001827 locations->AddTemp(Location::RequiresRegister());
1828 }
1829}
1830
1831void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
1832 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1833}
1834void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
1835 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1836}
1837void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
1838 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
1839}
1840void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
1841 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1842}
1843void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
1844 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1845}
1846void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
1847 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
1848}
1849void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
1850 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1851}
1852void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
1853 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1854}
1855void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
1856 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
1857}
1858
1859// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1860// memory model.
1861static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
1862 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001863 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001864 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1865 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1866 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
1867
1868 if (type == Primitive::kPrimLong) {
1869 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01001870 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
1871 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
1872 __ movl(temp, value);
1873 __ PoisonHeapReference(temp);
1874 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001875 } else {
1876 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
1877 }
1878
1879 if (is_volatile) {
1880 __ mfence();
1881 }
1882
1883 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001884 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001885 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1886 locations->GetTemp(1).AsRegister<CpuRegister>(),
1887 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001888 value,
1889 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001890 }
1891}
1892
1893void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001894 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001895}
1896void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001897 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001898}
1899void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001900 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001901}
1902void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001903 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001904}
1905void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001906 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001907}
1908void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001909 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001910}
1911void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001912 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001913}
1914void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001915 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001916}
1917void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001918 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001919}
1920
Mark Mendell58d25fd2015-04-03 14:52:31 -04001921static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
1922 HInvoke* invoke) {
1923 LocationSummary* locations = new (arena) LocationSummary(invoke,
1924 LocationSummary::kNoCall,
1925 kIntrinsified);
1926 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1927 locations->SetInAt(1, Location::RequiresRegister());
1928 locations->SetInAt(2, Location::RequiresRegister());
1929 // expected value must be in EAX/RAX.
1930 locations->SetInAt(3, Location::RegisterLocation(RAX));
1931 locations->SetInAt(4, Location::RequiresRegister());
1932
1933 locations->SetOut(Location::RequiresRegister());
1934 if (type == Primitive::kPrimNot) {
1935 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01001936 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001937 locations->AddTemp(Location::RequiresRegister());
1938 }
1939}
1940
1941void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
1942 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
1943}
1944
1945void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
1946 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
1947}
1948
1949void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
1950 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
1951}
1952
1953static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001954 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04001955 LocationSummary* locations = invoke->GetLocations();
1956
1957 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
1958 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
1959 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01001960 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04001961 DCHECK_EQ(expected.AsRegister(), RAX);
1962 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
1963 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1964
Roland Levillainb488b782015-10-22 11:38:49 +01001965 if (type == Primitive::kPrimNot) {
1966 // Mark card for object assuming new value is stored.
1967 bool value_can_be_null = true; // TODO: Worth finding out this information?
1968 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
1969 locations->GetTemp(1).AsRegister<CpuRegister>(),
1970 base,
1971 value,
1972 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01001973
Roland Levillainb488b782015-10-22 11:38:49 +01001974 bool base_equals_value = (base.AsRegister() == value.AsRegister());
1975 Register value_reg = value.AsRegister();
1976 if (kPoisonHeapReferences) {
1977 if (base_equals_value) {
1978 // If `base` and `value` are the same register location, move
1979 // `value_reg` to a temporary register. This way, poisoning
1980 // `value_reg` won't invalidate `base`.
1981 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
1982 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01001983 }
Roland Levillainb488b782015-10-22 11:38:49 +01001984
1985 // Check that the register allocator did not assign the location
1986 // of `expected` (RAX) to `value` nor to `base`, so that heap
1987 // poisoning (when enabled) works as intended below.
1988 // - If `value` were equal to `expected`, both references would
1989 // be poisoned twice, meaning they would not be poisoned at
1990 // all, as heap poisoning uses address negation.
1991 // - If `base` were equal to `expected`, poisoning `expected`
1992 // would invalidate `base`.
1993 DCHECK_NE(value_reg, expected.AsRegister());
1994 DCHECK_NE(base.AsRegister(), expected.AsRegister());
1995
1996 __ PoisonHeapReference(expected);
1997 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04001998 }
1999
Roland Levillainb488b782015-10-22 11:38:49 +01002000 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002001
Roland Levillain0d5a2812015-11-13 10:07:31 +00002002 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002003 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002004
Roland Levillainb488b782015-10-22 11:38:49 +01002005 // Convert ZF into the boolean result.
2006 __ setcc(kZero, out);
2007 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002008
Roland Levillain0d5a2812015-11-13 10:07:31 +00002009 // In the case of the `UnsafeCASObject` intrinsic, accessing an
2010 // object in the heap with LOCK CMPXCHG does not require a read
2011 // barrier, as we do not keep a reference to this heap location.
2012 // However, if heap poisoning is enabled, we need to unpoison the
2013 // values that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002014 if (kPoisonHeapReferences) {
2015 if (base_equals_value) {
2016 // `value_reg` has been moved to a temporary register, no need
2017 // to unpoison it.
2018 } else {
2019 // Ensure `value` is different from `out`, so that unpoisoning
2020 // the former does not invalidate the latter.
2021 DCHECK_NE(value_reg, out.AsRegister());
2022 __ UnpoisonHeapReference(CpuRegister(value_reg));
2023 }
2024 // Ensure `expected` is different from `out`, so that unpoisoning
2025 // the former does not invalidate the latter.
2026 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2027 __ UnpoisonHeapReference(expected);
2028 }
2029 } else {
2030 if (type == Primitive::kPrimInt) {
2031 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2032 } else if (type == Primitive::kPrimLong) {
2033 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2034 } else {
2035 LOG(FATAL) << "Unexpected CAS type " << type;
2036 }
2037
Roland Levillain0d5a2812015-11-13 10:07:31 +00002038 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002039 // scheduling barriers at this time.
2040
2041 // Convert ZF into the boolean result.
2042 __ setcc(kZero, out);
2043 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002044 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002045}
2046
2047void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2048 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2049}
2050
2051void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2052 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2053}
2054
2055void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
2056 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2057}
2058
2059void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2060 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2061 LocationSummary::kNoCall,
2062 kIntrinsified);
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetOut(Location::SameAsFirstInput());
2065 locations->AddTemp(Location::RequiresRegister());
2066}
2067
2068static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2069 X86_64Assembler* assembler) {
2070 Immediate imm_shift(shift);
2071 Immediate imm_mask(mask);
2072 __ movl(temp, reg);
2073 __ shrl(reg, imm_shift);
2074 __ andl(temp, imm_mask);
2075 __ andl(reg, imm_mask);
2076 __ shll(temp, imm_shift);
2077 __ orl(reg, temp);
2078}
2079
2080void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Roland Levillainb488b782015-10-22 11:38:49 +01002081 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002082 LocationSummary* locations = invoke->GetLocations();
2083
2084 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2085 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2086
2087 /*
2088 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2089 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2090 * compared to generic luni implementation which has 5 rounds of swapping bits.
2091 * x = bswap x
2092 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2093 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2094 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2095 */
2096 __ bswapl(reg);
2097 SwapBits(reg, temp, 1, 0x55555555, assembler);
2098 SwapBits(reg, temp, 2, 0x33333333, assembler);
2099 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2100}
2101
2102void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2103 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2104 LocationSummary::kNoCall,
2105 kIntrinsified);
2106 locations->SetInAt(0, Location::RequiresRegister());
2107 locations->SetOut(Location::SameAsFirstInput());
2108 locations->AddTemp(Location::RequiresRegister());
2109 locations->AddTemp(Location::RequiresRegister());
2110}
2111
2112static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2113 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2114 Immediate imm_shift(shift);
2115 __ movq(temp_mask, Immediate(mask));
2116 __ movq(temp, reg);
2117 __ shrq(reg, imm_shift);
2118 __ andq(temp, temp_mask);
2119 __ andq(reg, temp_mask);
2120 __ shlq(temp, imm_shift);
2121 __ orq(reg, temp);
2122}
2123
2124void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Roland Levillainb488b782015-10-22 11:38:49 +01002125 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002126 LocationSummary* locations = invoke->GetLocations();
2127
2128 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2129 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2130 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2131
2132 /*
2133 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2134 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2135 * compared to generic luni implementation which has 5 rounds of swapping bits.
2136 * x = bswap x
2137 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2138 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2139 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2140 */
2141 __ bswapq(reg);
2142 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2143 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2144 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2145}
2146
Mark Mendelld5897672015-08-12 21:16:41 -04002147static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2148 LocationSummary* locations = new (arena) LocationSummary(invoke,
2149 LocationSummary::kNoCall,
2150 kIntrinsified);
2151 locations->SetInAt(0, Location::Any());
2152 locations->SetOut(Location::RequiresRegister());
2153}
2154
2155static void GenLeadingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2156 LocationSummary* locations = invoke->GetLocations();
2157 Location src = locations->InAt(0);
2158 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2159
2160 int zero_value_result = is_long ? 64 : 32;
2161 if (invoke->InputAt(0)->IsConstant()) {
2162 // Evaluate this at compile time.
2163 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2164 if (value == 0) {
2165 value = zero_value_result;
2166 } else {
2167 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2168 }
2169 if (value == 0) {
2170 __ xorl(out, out);
2171 } else {
2172 __ movl(out, Immediate(value));
2173 }
2174 return;
2175 }
2176
2177 // Handle the non-constant cases.
2178 if (src.IsRegister()) {
2179 if (is_long) {
2180 __ bsrq(out, src.AsRegister<CpuRegister>());
2181 } else {
2182 __ bsrl(out, src.AsRegister<CpuRegister>());
2183 }
2184 } else if (is_long) {
2185 DCHECK(src.IsDoubleStackSlot());
2186 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2187 } else {
2188 DCHECK(src.IsStackSlot());
2189 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2190 }
2191
2192 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002193 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002194 __ j(kEqual, &is_zero);
2195
2196 // Correct the result from BSR to get the CLZ result.
2197 __ xorl(out, Immediate(zero_value_result - 1));
2198 __ jmp(&done);
2199
2200 // Fix the zero case with the expected result.
2201 __ Bind(&is_zero);
2202 __ movl(out, Immediate(zero_value_result));
2203
2204 __ Bind(&done);
2205}
2206
2207void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2208 CreateLeadingZeroLocations(arena_, invoke);
2209}
2210
2211void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2212 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2213 GenLeadingZeros(assembler, invoke, /* is_long */ false);
2214}
2215
2216void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2217 CreateLeadingZeroLocations(arena_, invoke);
2218}
2219
2220void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2221 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2222 GenLeadingZeros(assembler, invoke, /* is_long */ true);
2223}
2224
Mark Mendell2d554792015-09-15 21:45:18 -04002225static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2226 LocationSummary* locations = new (arena) LocationSummary(invoke,
2227 LocationSummary::kNoCall,
2228 kIntrinsified);
2229 locations->SetInAt(0, Location::Any());
2230 locations->SetOut(Location::RequiresRegister());
2231}
2232
2233static void GenTrailingZeros(X86_64Assembler* assembler, HInvoke* invoke, bool is_long) {
2234 LocationSummary* locations = invoke->GetLocations();
2235 Location src = locations->InAt(0);
2236 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2237
2238 int zero_value_result = is_long ? 64 : 32;
2239 if (invoke->InputAt(0)->IsConstant()) {
2240 // Evaluate this at compile time.
2241 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2242 if (value == 0) {
2243 value = zero_value_result;
2244 } else {
2245 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2246 }
2247 if (value == 0) {
2248 __ xorl(out, out);
2249 } else {
2250 __ movl(out, Immediate(value));
2251 }
2252 return;
2253 }
2254
2255 // Handle the non-constant cases.
2256 if (src.IsRegister()) {
2257 if (is_long) {
2258 __ bsfq(out, src.AsRegister<CpuRegister>());
2259 } else {
2260 __ bsfl(out, src.AsRegister<CpuRegister>());
2261 }
2262 } else if (is_long) {
2263 DCHECK(src.IsDoubleStackSlot());
2264 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2265 } else {
2266 DCHECK(src.IsStackSlot());
2267 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2268 }
2269
2270 // BSF sets ZF if the input was zero, and the output is undefined.
2271 NearLabel done;
2272 __ j(kNotEqual, &done);
2273
2274 // Fix the zero case with the expected result.
2275 __ movl(out, Immediate(zero_value_result));
2276
2277 __ Bind(&done);
2278}
2279
2280void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2281 CreateTrailingZeroLocations(arena_, invoke);
2282}
2283
2284void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2285 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2286 GenTrailingZeros(assembler, invoke, /* is_long */ false);
2287}
2288
2289void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2290 CreateTrailingZeroLocations(arena_, invoke);
2291}
2292
2293void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2294 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2295 GenTrailingZeros(assembler, invoke, /* is_long */ true);
2296}
2297
2298static void CreateRotateLocations(ArenaAllocator* arena, HInvoke* invoke) {
2299 LocationSummary* locations = new (arena) LocationSummary(invoke,
2300 LocationSummary::kNoCall,
2301 kIntrinsified);
2302 locations->SetInAt(0, Location::RequiresRegister());
2303 // The shift count needs to be in CL or a constant.
2304 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, invoke->InputAt(1)));
2305 locations->SetOut(Location::SameAsFirstInput());
2306}
2307
2308static void GenRotate(X86_64Assembler* assembler, HInvoke* invoke, bool is_long, bool is_left) {
2309 LocationSummary* locations = invoke->GetLocations();
2310 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
2311 Location second = locations->InAt(1);
2312
2313 if (is_long) {
2314 if (second.IsRegister()) {
2315 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2316 if (is_left) {
2317 __ rolq(first_reg, second_reg);
2318 } else {
2319 __ rorq(first_reg, second_reg);
2320 }
2321 } else {
2322 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
2323 if (is_left) {
2324 __ rolq(first_reg, imm);
2325 } else {
2326 __ rorq(first_reg, imm);
2327 }
2328 }
2329 } else {
2330 if (second.IsRegister()) {
2331 CpuRegister second_reg = second.AsRegister<CpuRegister>();
2332 if (is_left) {
2333 __ roll(first_reg, second_reg);
2334 } else {
2335 __ rorl(first_reg, second_reg);
2336 }
2337 } else {
2338 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
2339 if (is_left) {
2340 __ roll(first_reg, imm);
2341 } else {
2342 __ rorl(first_reg, imm);
2343 }
2344 }
2345 }
2346}
2347
2348void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2349 CreateRotateLocations(arena_, invoke);
2350}
2351
2352void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateLeft(HInvoke* invoke) {
2353 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2354 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ true);
2355}
2356
2357void IntrinsicLocationsBuilderX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2358 CreateRotateLocations(arena_, invoke);
2359}
2360
2361void IntrinsicCodeGeneratorX86_64::VisitIntegerRotateRight(HInvoke* invoke) {
2362 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2363 GenRotate(assembler, invoke, /* is_long */ false, /* is_left */ false);
2364}
2365
2366void IntrinsicLocationsBuilderX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2367 CreateRotateLocations(arena_, invoke);
2368}
2369
2370void IntrinsicCodeGeneratorX86_64::VisitLongRotateLeft(HInvoke* invoke) {
2371 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2372 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ true);
2373}
2374
2375void IntrinsicLocationsBuilderX86_64::VisitLongRotateRight(HInvoke* invoke) {
2376 CreateRotateLocations(arena_, invoke);
2377}
2378
2379void IntrinsicCodeGeneratorX86_64::VisitLongRotateRight(HInvoke* invoke) {
2380 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen_->GetAssembler());
2381 GenRotate(assembler, invoke, /* is_long */ true, /* is_left */ false);
2382}
2383
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002384// Unimplemented intrinsics.
2385
2386#define UNIMPLEMENTED_INTRINSIC(Name) \
2387void IntrinsicLocationsBuilderX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2388} \
2389void IntrinsicCodeGeneratorX86_64::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \
2390}
2391
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002392UNIMPLEMENTED_INTRINSIC(ReferenceGetReferent)
2393
Roland Levillain4d027112015-07-01 15:41:14 +01002394#undef UNIMPLEMENTED_INTRINSIC
2395
2396#undef __
2397
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002398} // namespace x86_64
2399} // namespace art