blob: 405b4c02ec86e91cc7804b800d1338de3c189ac8 [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) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800613 // See intrinsics.h.
614 if (kRoundIsPlusPointFive) {
615 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
616 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400617}
618
619void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
620 LocationSummary* locations = invoke->GetLocations();
621 if (locations->WillCall()) {
622 InvokeOutOfLineIntrinsic(codegen_, invoke);
623 return;
624 }
625
626 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
627 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
628 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400629 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400630 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400631 X86_64Assembler* assembler = GetAssembler();
632
Mark Mendell40741f32015-04-20 22:10:34 -0400633 // Load 0.5 into inPlusPointFive.
634 __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400635
636 // Add in the input.
637 __ addss(inPlusPointFive, in);
638
639 // And truncate to an integer.
640 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
641
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600642 // Load maxInt into out.
643 codegen_->Load64BitValue(out, kPrimIntMax);
644
Mark Mendellfb8d2792015-03-31 22:16:59 -0400645 // if inPlusPointFive >= maxInt goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400646 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400647 __ j(kAboveEqual, &done);
648
649 // if input == NaN goto nan
650 __ j(kUnordered, &nan);
651
652 // output = float-to-int-truncate(input)
653 __ cvttss2si(out, inPlusPointFive);
654 __ jmp(&done);
655 __ Bind(&nan);
656
657 // output = 0
658 __ xorl(out, out);
659 __ Bind(&done);
660}
661
662void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800663 // See intrinsics.h.
664 if (kRoundIsPlusPointFive) {
665 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
666 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400667}
668
669void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
670 LocationSummary* locations = invoke->GetLocations();
671 if (locations->WillCall()) {
672 InvokeOutOfLineIntrinsic(codegen_, invoke);
673 return;
674 }
675
676 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
677 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
678 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400679 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400680 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400681 X86_64Assembler* assembler = GetAssembler();
682
Mark Mendell40741f32015-04-20 22:10:34 -0400683 // Load 0.5 into inPlusPointFive.
684 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400685
686 // Add in the input.
687 __ addsd(inPlusPointFive, in);
688
689 // And truncate to an integer.
690 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
691
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600692 // Load maxLong into out.
693 codegen_->Load64BitValue(out, kPrimLongMax);
694
Mark Mendellfb8d2792015-03-31 22:16:59 -0400695 // if inPlusPointFive >= maxLong goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400696 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400697 __ j(kAboveEqual, &done);
698
699 // if input == NaN goto nan
700 __ j(kUnordered, &nan);
701
702 // output = double-to-long-truncate(input)
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000703 __ cvttsd2si(out, inPlusPointFive, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400704 __ jmp(&done);
705 __ Bind(&nan);
706
707 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400708 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400709 __ Bind(&done);
710}
711
Mark Mendella4f12202015-08-06 15:23:34 -0400712static void CreateFPToFPCallLocations(ArenaAllocator* arena,
713 HInvoke* invoke) {
714 LocationSummary* locations = new (arena) LocationSummary(invoke,
715 LocationSummary::kCall,
716 kIntrinsified);
717 InvokeRuntimeCallingConvention calling_convention;
718 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
719 locations->SetOut(Location::FpuRegisterLocation(XMM0));
720
721 // We have to ensure that the native code doesn't clobber the XMM registers which are
722 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
723 // saved in the prologue and properly restored.
724 for (auto fp_reg : non_volatile_xmm_regs) {
725 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
726 }
727}
728
729static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
730 QuickEntrypointEnum entry) {
731 LocationSummary* locations = invoke->GetLocations();
732 DCHECK(locations->WillCall());
733 DCHECK(invoke->IsInvokeStaticOrDirect());
734 X86_64Assembler* assembler = codegen->GetAssembler();
735
736 __ gs()->call(Address::Absolute(GetThreadOffset<kX86_64WordSize>(entry), true));
737 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
738}
739
740void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
741 CreateFPToFPCallLocations(arena_, invoke);
742}
743
744void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
745 GenFPToFPCall(invoke, codegen_, kQuickCos);
746}
747
748void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
749 CreateFPToFPCallLocations(arena_, invoke);
750}
751
752void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
753 GenFPToFPCall(invoke, codegen_, kQuickSin);
754}
755
756void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
757 CreateFPToFPCallLocations(arena_, invoke);
758}
759
760void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
761 GenFPToFPCall(invoke, codegen_, kQuickAcos);
762}
763
764void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
765 CreateFPToFPCallLocations(arena_, invoke);
766}
767
768void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
769 GenFPToFPCall(invoke, codegen_, kQuickAsin);
770}
771
772void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
773 CreateFPToFPCallLocations(arena_, invoke);
774}
775
776void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
777 GenFPToFPCall(invoke, codegen_, kQuickAtan);
778}
779
780void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
781 CreateFPToFPCallLocations(arena_, invoke);
782}
783
784void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
785 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
786}
787
788void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
789 CreateFPToFPCallLocations(arena_, invoke);
790}
791
792void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
793 GenFPToFPCall(invoke, codegen_, kQuickCosh);
794}
795
796void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
797 CreateFPToFPCallLocations(arena_, invoke);
798}
799
800void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
801 GenFPToFPCall(invoke, codegen_, kQuickExp);
802}
803
804void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
805 CreateFPToFPCallLocations(arena_, invoke);
806}
807
808void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
809 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
810}
811
812void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
813 CreateFPToFPCallLocations(arena_, invoke);
814}
815
816void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
817 GenFPToFPCall(invoke, codegen_, kQuickLog);
818}
819
820void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
821 CreateFPToFPCallLocations(arena_, invoke);
822}
823
824void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
825 GenFPToFPCall(invoke, codegen_, kQuickLog10);
826}
827
828void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
829 CreateFPToFPCallLocations(arena_, invoke);
830}
831
832void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
833 GenFPToFPCall(invoke, codegen_, kQuickSinh);
834}
835
836void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
837 CreateFPToFPCallLocations(arena_, invoke);
838}
839
840void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
841 GenFPToFPCall(invoke, codegen_, kQuickTan);
842}
843
844void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
845 CreateFPToFPCallLocations(arena_, invoke);
846}
847
848void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
849 GenFPToFPCall(invoke, codegen_, kQuickTanh);
850}
851
852static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
853 HInvoke* invoke) {
854 LocationSummary* locations = new (arena) LocationSummary(invoke,
855 LocationSummary::kCall,
856 kIntrinsified);
857 InvokeRuntimeCallingConvention calling_convention;
858 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
859 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
860 locations->SetOut(Location::FpuRegisterLocation(XMM0));
861
862 // We have to ensure that the native code doesn't clobber the XMM registers which are
863 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
864 // saved in the prologue and properly restored.
865 for (auto fp_reg : non_volatile_xmm_regs) {
866 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
867 }
868}
869
870void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
871 CreateFPFPToFPCallLocations(arena_, invoke);
872}
873
874void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
875 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
876}
877
878void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
879 CreateFPFPToFPCallLocations(arena_, invoke);
880}
881
882void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
883 GenFPToFPCall(invoke, codegen_, kQuickHypot);
884}
885
886void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
887 CreateFPFPToFPCallLocations(arena_, invoke);
888}
889
890void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
891 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
892}
893
Mark Mendell6bc53a92015-07-01 14:26:52 -0400894void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
895 // Check to see if we have known failures that will cause us to have to bail out
896 // to the runtime, and just generate the runtime call directly.
897 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
898 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
899
900 // The positions must be non-negative.
901 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
902 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
903 // We will have to fail anyways.
904 return;
905 }
906
907 // The length must be > 0.
908 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
909 if (length != nullptr) {
910 int32_t len = length->GetValue();
911 if (len < 0) {
912 // Just call as normal.
913 return;
914 }
915 }
916
917 LocationSummary* locations = new (arena_) LocationSummary(invoke,
918 LocationSummary::kCallOnSlowPath,
919 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100920 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400921 locations->SetInAt(0, Location::RequiresRegister());
922 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
923 locations->SetInAt(2, Location::RequiresRegister());
924 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
925 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
926
927 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
928 locations->AddTemp(Location::RegisterLocation(RSI));
929 locations->AddTemp(Location::RegisterLocation(RDI));
930 locations->AddTemp(Location::RegisterLocation(RCX));
931}
932
933static void CheckPosition(X86_64Assembler* assembler,
934 Location pos,
935 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100936 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700937 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400938 CpuRegister input_len,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100939 CpuRegister temp,
940 bool length_is_input_length = false) {
941 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400942 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
943
944 if (pos.IsConstant()) {
945 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
946 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100947 if (!length_is_input_length) {
948 // Check that length(input) >= length.
949 if (length.IsConstant()) {
950 __ cmpl(Address(input, length_offset),
951 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
952 } else {
953 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
954 }
955 __ j(kLess, slow_path->GetEntryLabel());
956 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400957 } else {
958 // Check that length(input) >= pos.
959 __ movl(input_len, Address(input, length_offset));
960 __ cmpl(input_len, Immediate(pos_const));
961 __ j(kLess, slow_path->GetEntryLabel());
962
963 // Check that (length(input) - pos) >= length.
964 __ leal(temp, Address(input_len, -pos_const));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100965 if (length.IsConstant()) {
966 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
967 } else {
968 __ cmpl(temp, length.AsRegister<CpuRegister>());
969 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400970 __ j(kLess, slow_path->GetEntryLabel());
971 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100972 } else if (length_is_input_length) {
973 // The only way the copy can succeed is if pos is zero.
974 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
975 __ testl(pos_reg, pos_reg);
976 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400977 } else {
978 // Check that pos >= 0.
979 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
980 __ testl(pos_reg, pos_reg);
981 __ j(kLess, slow_path->GetEntryLabel());
982
983 // Check that pos <= length(input).
984 __ cmpl(Address(input, length_offset), pos_reg);
985 __ j(kLess, slow_path->GetEntryLabel());
986
987 // Check that (length(input) - pos) >= length.
988 __ movl(temp, Address(input, length_offset));
989 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100990 if (length.IsConstant()) {
991 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
992 } else {
993 __ cmpl(temp, length.AsRegister<CpuRegister>());
994 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400995 __ j(kLess, slow_path->GetEntryLabel());
996 }
997}
998
999void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1000 X86_64Assembler* assembler = GetAssembler();
1001 LocationSummary* locations = invoke->GetLocations();
1002
1003 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001004 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001005 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001006 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001007 Location length = locations->InAt(4);
1008
1009 // Temporaries that we need for MOVSW.
1010 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1011 DCHECK_EQ(src_base.AsRegister(), RSI);
1012 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1013 DCHECK_EQ(dest_base.AsRegister(), RDI);
1014 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1015 DCHECK_EQ(count.AsRegister(), RCX);
1016
Andreas Gampe85b62f22015-09-09 13:15:38 -07001017 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001018 codegen_->AddSlowPath(slow_path);
1019
1020 // Bail out if the source and destination are the same.
1021 __ cmpl(src, dest);
1022 __ j(kEqual, slow_path->GetEntryLabel());
1023
1024 // Bail out if the source is null.
1025 __ testl(src, src);
1026 __ j(kEqual, slow_path->GetEntryLabel());
1027
1028 // Bail out if the destination is null.
1029 __ testl(dest, dest);
1030 __ j(kEqual, slow_path->GetEntryLabel());
1031
1032 // If the length is negative, bail out.
1033 // We have already checked in the LocationsBuilder for the constant case.
1034 if (!length.IsConstant()) {
1035 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1036 __ j(kLess, slow_path->GetEntryLabel());
1037 }
1038
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001039 // Validity checks: source.
1040 CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base);
1041
1042 // Validity checks: dest.
1043 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base);
1044
Mark Mendell6bc53a92015-07-01 14:26:52 -04001045 // We need the count in RCX.
1046 if (length.IsConstant()) {
1047 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1048 } else {
1049 __ movl(count, length.AsRegister<CpuRegister>());
1050 }
1051
Mark Mendell6bc53a92015-07-01 14:26:52 -04001052 // Okay, everything checks out. Finally time to do the copy.
1053 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1054 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1055 DCHECK_EQ(char_size, 2u);
1056
1057 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1058
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001059 if (src_pos.IsConstant()) {
1060 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1061 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001062 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001063 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001064 ScaleFactor::TIMES_2, data_offset));
1065 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001066 if (dest_pos.IsConstant()) {
1067 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1068 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001069 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001070 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001071 ScaleFactor::TIMES_2, data_offset));
1072 }
1073
1074 // Do the move.
1075 __ rep_movsw();
1076
1077 __ Bind(slow_path->GetExitLabel());
1078}
1079
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001080
1081void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001082 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001083}
1084
Roland Levillain0d5a2812015-11-13 10:07:31 +00001085// TODO: Implement read barriers in the SystemArrayCopy intrinsic.
1086// Note that this code path is not used (yet) because we do not
1087// intrinsify methods that can go into the IntrinsicSlowPathX86_64
1088// slow path.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001089void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
1090 X86_64Assembler* assembler = GetAssembler();
1091 LocationSummary* locations = invoke->GetLocations();
1092
1093 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1094 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1095 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1096 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1097
1098 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1099 Location src_pos = locations->InAt(1);
1100 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1101 Location dest_pos = locations->InAt(3);
1102 Location length = locations->InAt(4);
1103 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
1104 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
1105 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
1106
1107 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1108 codegen_->AddSlowPath(slow_path);
1109
Roland Levillainebea3d22016-04-12 15:42:57 +01001110 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001111 SystemArrayCopyOptimizations optimizations(invoke);
1112
Roland Levillainebea3d22016-04-12 15:42:57 +01001113 if (!optimizations.GetDestinationIsSource() &&
1114 (!src_pos.IsConstant() || !dest_pos.IsConstant())) {
1115 __ cmpl(src, dest);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001116 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001117 // If source and destination are the same, we go to slow path if we need to do
1118 // forward copying.
1119 if (src_pos.IsConstant()) {
1120 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1121 if (dest_pos.IsConstant()) {
1122 // Checked when building locations.
1123 DCHECK(!optimizations.GetDestinationIsSource()
1124 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
1125 } else {
1126 if (!optimizations.GetDestinationIsSource()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001127 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001128 }
1129 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
1130 __ j(kGreater, slow_path->GetEntryLabel());
1131 }
1132 } else {
1133 if (!optimizations.GetDestinationIsSource()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001134 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001135 }
1136 if (dest_pos.IsConstant()) {
1137 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1138 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
1139 __ j(kLess, slow_path->GetEntryLabel());
1140 } else {
1141 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
1142 __ j(kLess, slow_path->GetEntryLabel());
1143 }
1144 }
1145
Roland Levillainebea3d22016-04-12 15:42:57 +01001146 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001147
1148 if (!optimizations.GetSourceIsNotNull()) {
1149 // Bail out if the source is null.
1150 __ testl(src, src);
1151 __ j(kEqual, slow_path->GetEntryLabel());
1152 }
1153
1154 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1155 // Bail out if the destination is null.
1156 __ testl(dest, dest);
1157 __ j(kEqual, slow_path->GetEntryLabel());
1158 }
1159
1160 // If the length is negative, bail out.
1161 // We have already checked in the LocationsBuilder for the constant case.
1162 if (!length.IsConstant() &&
1163 !optimizations.GetCountIsSourceLength() &&
1164 !optimizations.GetCountIsDestinationLength()) {
1165 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1166 __ j(kLess, slow_path->GetEntryLabel());
1167 }
1168
1169 // Validity checks: source.
1170 CheckPosition(assembler,
1171 src_pos,
1172 src,
1173 length,
1174 slow_path,
1175 temp1,
1176 temp2,
1177 optimizations.GetCountIsSourceLength());
1178
1179 // Validity checks: dest.
1180 CheckPosition(assembler,
1181 dest_pos,
1182 dest,
1183 length,
1184 slow_path,
1185 temp1,
1186 temp2,
1187 optimizations.GetCountIsDestinationLength());
1188
1189 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1190 // Check whether all elements of the source array are assignable to the component
1191 // type of the destination array. We do two checks: the classes are the same,
1192 // or the destination is Object[]. If none of these checks succeed, we go to the
1193 // slow path.
1194 __ movl(temp1, Address(dest, class_offset));
1195 __ movl(temp2, Address(src, class_offset));
1196 bool did_unpoison = false;
1197 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1198 !optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001199 // One or two of the references need to be unpoisoned. Unpoison them
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001200 // both to make the identity check valid.
1201 __ MaybeUnpoisonHeapReference(temp1);
1202 __ MaybeUnpoisonHeapReference(temp2);
1203 did_unpoison = true;
1204 }
1205
1206 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1207 // Bail out if the destination is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001208 // /* HeapReference<Class> */ TMP = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001209 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1210 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1211 __ j(kEqual, slow_path->GetEntryLabel());
1212 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1213 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1214 __ j(kNotEqual, slow_path->GetEntryLabel());
1215 }
1216
1217 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1218 // Bail out if the source is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001219 // /* HeapReference<Class> */ TMP = temp2->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001220 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1221 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1222 __ j(kEqual, slow_path->GetEntryLabel());
1223 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1224 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1225 __ j(kNotEqual, slow_path->GetEntryLabel());
1226 }
1227
1228 __ cmpl(temp1, temp2);
1229
1230 if (optimizations.GetDestinationIsTypedObjectArray()) {
1231 NearLabel do_copy;
1232 __ j(kEqual, &do_copy);
1233 if (!did_unpoison) {
1234 __ MaybeUnpoisonHeapReference(temp1);
1235 }
Roland Levillainebea3d22016-04-12 15:42:57 +01001236 // /* HeapReference<Class> */ temp1 = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001237 __ movl(temp1, Address(temp1, component_offset));
1238 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillainebea3d22016-04-12 15:42:57 +01001239 // /* HeapReference<Class> */ temp1 = temp1->super_class_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001240 __ movl(temp1, Address(temp1, super_offset));
1241 // No need to unpoison the result, we're comparing against null.
1242 __ testl(temp1, temp1);
1243 __ j(kNotEqual, slow_path->GetEntryLabel());
1244 __ Bind(&do_copy);
1245 } else {
1246 __ j(kNotEqual, slow_path->GetEntryLabel());
1247 }
1248 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1249 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1250 // Bail out if the source is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001251 // /* HeapReference<Class> */ temp1 = src->klass_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001252 __ movl(temp1, Address(src, class_offset));
1253 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillainebea3d22016-04-12 15:42:57 +01001254 // /* HeapReference<Class> */ TMP = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001255 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1256 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1257 __ j(kEqual, slow_path->GetEntryLabel());
1258 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1259 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1260 __ j(kNotEqual, slow_path->GetEntryLabel());
1261 }
1262
1263 // Compute base source address, base destination address, and end source address.
1264
1265 uint32_t element_size = sizeof(int32_t);
1266 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1267 if (src_pos.IsConstant()) {
1268 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1269 __ leal(temp1, Address(src, element_size * constant + offset));
1270 } else {
1271 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1272 }
1273
1274 if (dest_pos.IsConstant()) {
1275 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1276 __ leal(temp2, Address(dest, element_size * constant + offset));
1277 } else {
1278 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1279 }
1280
1281 if (length.IsConstant()) {
1282 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1283 __ leal(temp3, Address(temp1, element_size * constant));
1284 } else {
1285 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1286 }
1287
1288 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1289 // poison/unpoison, nor do any read barrier as the next uses of the destination
1290 // array will do it.
1291 NearLabel loop, done;
1292 __ cmpl(temp1, temp3);
1293 __ j(kEqual, &done);
1294 __ Bind(&loop);
1295 __ movl(CpuRegister(TMP), Address(temp1, 0));
1296 __ movl(Address(temp2, 0), CpuRegister(TMP));
1297 __ addl(temp1, Immediate(element_size));
1298 __ addl(temp2, Immediate(element_size));
1299 __ cmpl(temp1, temp3);
1300 __ j(kNotEqual, &loop);
1301 __ Bind(&done);
1302
1303 // We only need one card marking on the destination array.
1304 codegen_->MarkGCCard(temp1,
1305 temp2,
1306 dest,
1307 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001308 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001309
1310 __ Bind(slow_path->GetExitLabel());
1311}
1312
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001313void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1314 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1315 LocationSummary::kCall,
1316 kIntrinsified);
1317 InvokeRuntimeCallingConvention calling_convention;
1318 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1319 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1320 locations->SetOut(Location::RegisterLocation(RAX));
1321}
1322
1323void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1324 X86_64Assembler* assembler = GetAssembler();
1325 LocationSummary* locations = invoke->GetLocations();
1326
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001327 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001328 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001329
1330 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1331 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001332 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001333 codegen_->AddSlowPath(slow_path);
1334 __ j(kEqual, slow_path->GetEntryLabel());
1335
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001336 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo),
1337 /* no_rip */ true));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001338 __ Bind(slow_path->GetExitLabel());
1339}
1340
Agi Csakif8cfb202015-08-13 17:54:54 -07001341void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1342 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1343 LocationSummary::kNoCall,
1344 kIntrinsified);
1345 locations->SetInAt(0, Location::RequiresRegister());
1346 locations->SetInAt(1, Location::RequiresRegister());
1347
1348 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1349 locations->AddTemp(Location::RegisterLocation(RCX));
1350 locations->AddTemp(Location::RegisterLocation(RDI));
1351
1352 // Set output, RSI needed for repe_cmpsq instruction anyways.
1353 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1354}
1355
1356void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1357 X86_64Assembler* assembler = GetAssembler();
1358 LocationSummary* locations = invoke->GetLocations();
1359
1360 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1361 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1362 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1363 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1364 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1365
Mark Mendell0c9497d2015-08-21 09:30:05 -04001366 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001367
1368 // Get offsets of count, value, and class fields within a string object.
1369 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1370 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1371 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1372
1373 // Note that the null check must have been done earlier.
1374 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1375
Vladimir Marko53b52002016-05-24 19:30:45 +01001376 StringEqualsOptimizations optimizations(invoke);
1377 if (!optimizations.GetArgumentNotNull()) {
1378 // Check if input is null, return false if it is.
1379 __ testl(arg, arg);
1380 __ j(kEqual, &return_false);
1381 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001382
Vladimir Marko53b52002016-05-24 19:30:45 +01001383 if (!optimizations.GetArgumentIsString()) {
1384 // Instanceof check for the argument by comparing class fields.
1385 // All string objects must have the same type since String cannot be subclassed.
1386 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1387 // If the argument is a string object, its class field must be equal to receiver's class field.
1388 __ movl(rcx, Address(str, class_offset));
1389 __ cmpl(rcx, Address(arg, class_offset));
1390 __ j(kNotEqual, &return_false);
1391 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001392
1393 // Reference equality check, return true if same reference.
1394 __ cmpl(str, arg);
1395 __ j(kEqual, &return_true);
1396
1397 // Load length of receiver string.
1398 __ movl(rcx, Address(str, count_offset));
1399 // Check if lengths are equal, return false if they're not.
1400 __ cmpl(rcx, Address(arg, count_offset));
1401 __ j(kNotEqual, &return_false);
1402 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001403 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001404
1405 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1406 __ leal(rsi, Address(str, value_offset));
1407 __ leal(rdi, Address(arg, value_offset));
1408
1409 // Divide string length by 4 and adjust for lengths not divisible by 4.
1410 __ addl(rcx, Immediate(3));
1411 __ shrl(rcx, Immediate(2));
1412
1413 // Assertions that must hold in order to compare strings 4 characters at a time.
1414 DCHECK_ALIGNED(value_offset, 8);
1415 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1416
1417 // Loop to compare strings four characters at a time starting at the beginning of the string.
1418 __ repe_cmpsq();
1419 // If strings are not equal, zero flag will be cleared.
1420 __ j(kNotEqual, &return_false);
1421
1422 // Return true and exit the function.
1423 // If loop does not result in returning false, we return true.
1424 __ Bind(&return_true);
1425 __ movl(rsi, Immediate(1));
1426 __ jmp(&end);
1427
1428 // Return false and exit the function.
1429 __ Bind(&return_false);
1430 __ xorl(rsi, rsi);
1431 __ Bind(&end);
1432}
1433
Andreas Gampe21030dd2015-05-07 14:46:15 -07001434static void CreateStringIndexOfLocations(HInvoke* invoke,
1435 ArenaAllocator* allocator,
1436 bool start_at_zero) {
1437 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1438 LocationSummary::kCallOnSlowPath,
1439 kIntrinsified);
1440 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1441 locations->SetInAt(0, Location::RegisterLocation(RDI));
1442 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1443 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1444 // of the instruction explicitly.
1445 // Note: This works as we don't clobber RAX anywhere.
1446 locations->SetInAt(1, Location::RegisterLocation(RAX));
1447 if (!start_at_zero) {
1448 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1449 }
1450 // As we clobber RDI during execution anyways, also use it as the output.
1451 locations->SetOut(Location::SameAsFirstInput());
1452
1453 // repne scasw uses RCX as the counter.
1454 locations->AddTemp(Location::RegisterLocation(RCX));
1455 // Need another temporary to be able to compute the result.
1456 locations->AddTemp(Location::RequiresRegister());
1457}
1458
1459static void GenerateStringIndexOf(HInvoke* invoke,
1460 X86_64Assembler* assembler,
1461 CodeGeneratorX86_64* codegen,
1462 ArenaAllocator* allocator,
1463 bool start_at_zero) {
1464 LocationSummary* locations = invoke->GetLocations();
1465
1466 // Note that the null check must have been done earlier.
1467 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1468
1469 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1470 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1471 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1472 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1473 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1474
1475 // Check our assumptions for registers.
1476 DCHECK_EQ(string_obj.AsRegister(), RDI);
1477 DCHECK_EQ(search_value.AsRegister(), RAX);
1478 DCHECK_EQ(counter.AsRegister(), RCX);
1479 DCHECK_EQ(out.AsRegister(), RDI);
1480
1481 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001482 // or directly dispatch for a large constant, or omit slow-path for a small constant or a char.
Andreas Gampe85b62f22015-09-09 13:15:38 -07001483 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001484 HInstruction* code_point = invoke->InputAt(1);
1485 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001486 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001487 std::numeric_limits<uint16_t>::max()) {
1488 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1489 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1490 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1491 codegen->AddSlowPath(slow_path);
1492 __ jmp(slow_path->GetEntryLabel());
1493 __ Bind(slow_path->GetExitLabel());
1494 return;
1495 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001496 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001497 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1498 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1499 codegen->AddSlowPath(slow_path);
1500 __ j(kAbove, slow_path->GetEntryLabel());
1501 }
1502
1503 // From here down, we know that we are looking for a char that fits in 16 bits.
1504 // Location of reference to data array within the String object.
1505 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1506 // Location of count within the String object.
1507 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1508
1509 // Load string length, i.e., the count field of the string.
1510 __ movl(string_length, Address(string_obj, count_offset));
1511
1512 // Do a length check.
1513 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001514 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001515 __ testl(string_length, string_length);
1516 __ j(kEqual, &not_found_label);
1517
1518 if (start_at_zero) {
1519 // Number of chars to scan is the same as the string length.
1520 __ movl(counter, string_length);
1521
1522 // Move to the start of the string.
1523 __ addq(string_obj, Immediate(value_offset));
1524 } else {
1525 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1526
1527 // Do a start_index check.
1528 __ cmpl(start_index, string_length);
1529 __ j(kGreaterEqual, &not_found_label);
1530
1531 // Ensure we have a start index >= 0;
1532 __ xorl(counter, counter);
1533 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001534 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001535
1536 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1537 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1538
1539 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1540 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1541 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1542 }
1543
1544 // Everything is set up for repne scasw:
1545 // * Comparison address in RDI.
1546 // * Counter in ECX.
1547 __ repne_scasw();
1548
1549 // Did we find a match?
1550 __ j(kNotEqual, &not_found_label);
1551
1552 // Yes, we matched. Compute the index of the result.
1553 __ subl(string_length, counter);
1554 __ leal(out, Address(string_length, -1));
1555
Mark Mendell0c9497d2015-08-21 09:30:05 -04001556 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001557 __ jmp(&done);
1558
1559 // Failed to match; return -1.
1560 __ Bind(&not_found_label);
1561 __ movl(out, Immediate(-1));
1562
1563 // And join up at the end.
1564 __ Bind(&done);
1565 if (slow_path != nullptr) {
1566 __ Bind(slow_path->GetExitLabel());
1567 }
1568}
1569
1570void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001571 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001572}
1573
1574void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001575 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001576}
1577
1578void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001579 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001580}
1581
1582void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001583 GenerateStringIndexOf(
1584 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001585}
1586
Jeff Hao848f70a2014-01-15 13:49:50 -08001587void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1588 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1589 LocationSummary::kCall,
1590 kIntrinsified);
1591 InvokeRuntimeCallingConvention calling_convention;
1592 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1593 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1594 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1595 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1596 locations->SetOut(Location::RegisterLocation(RAX));
1597}
1598
1599void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1600 X86_64Assembler* assembler = GetAssembler();
1601 LocationSummary* locations = invoke->GetLocations();
1602
1603 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1604 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001605 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001606 codegen_->AddSlowPath(slow_path);
1607 __ j(kEqual, slow_path->GetEntryLabel());
1608
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001609 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes),
1610 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001611 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001612 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1613 __ Bind(slow_path->GetExitLabel());
1614}
1615
1616void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1617 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1618 LocationSummary::kCall,
1619 kIntrinsified);
1620 InvokeRuntimeCallingConvention calling_convention;
1621 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1622 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1623 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1624 locations->SetOut(Location::RegisterLocation(RAX));
1625}
1626
1627void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1628 X86_64Assembler* assembler = GetAssembler();
1629
Roland Levillaincc3839c2016-02-29 16:23:48 +00001630 // No need to emit code checking whether `locations->InAt(2)` is a null
1631 // pointer, as callers of the native method
1632 //
1633 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1634 //
1635 // all include a null check on `data` before calling that method.
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001636 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars),
1637 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001638 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001639 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1640}
1641
1642void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1643 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1644 LocationSummary::kCall,
1645 kIntrinsified);
1646 InvokeRuntimeCallingConvention calling_convention;
1647 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1648 locations->SetOut(Location::RegisterLocation(RAX));
1649}
1650
1651void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1652 X86_64Assembler* assembler = GetAssembler();
1653 LocationSummary* locations = invoke->GetLocations();
1654
1655 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1656 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001657 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001658 codegen_->AddSlowPath(slow_path);
1659 __ j(kEqual, slow_path->GetEntryLabel());
1660
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001661 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString),
1662 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001663 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001664 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1665 __ Bind(slow_path->GetExitLabel());
1666}
1667
Mark Mendell8f8926a2015-08-17 11:39:06 -04001668void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1669 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1670 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1671 LocationSummary::kNoCall,
1672 kIntrinsified);
1673 locations->SetInAt(0, Location::RequiresRegister());
1674 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1675 locations->SetInAt(2, Location::RequiresRegister());
1676 locations->SetInAt(3, Location::RequiresRegister());
1677 locations->SetInAt(4, Location::RequiresRegister());
1678
1679 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1680 locations->AddTemp(Location::RegisterLocation(RSI));
1681 locations->AddTemp(Location::RegisterLocation(RDI));
1682 locations->AddTemp(Location::RegisterLocation(RCX));
1683}
1684
1685void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1686 X86_64Assembler* assembler = GetAssembler();
1687 LocationSummary* locations = invoke->GetLocations();
1688
1689 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1690 // Location of data in char array buffer.
1691 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1692 // Location of char array data in string.
1693 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1694
1695 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1696 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1697 Location srcBegin = locations->InAt(1);
1698 int srcBegin_value =
1699 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1700 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1701 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1702 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1703
1704 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1705 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1706 DCHECK_EQ(char_size, 2u);
1707
1708 // Compute the address of the destination buffer.
1709 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1710
1711 // Compute the address of the source string.
1712 if (srcBegin.IsConstant()) {
1713 // Compute the address of the source string by adding the number of chars from
1714 // the source beginning to the value offset of a string.
1715 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1716 } else {
1717 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1718 ScaleFactor::TIMES_2, value_offset));
1719 }
1720
1721 // Compute the number of chars (words) to move.
1722 __ movl(CpuRegister(RCX), srcEnd);
1723 if (srcBegin.IsConstant()) {
1724 if (srcBegin_value != 0) {
1725 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1726 }
1727 } else {
1728 DCHECK(srcBegin.IsRegister());
1729 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1730 }
1731
1732 // Do the move.
1733 __ rep_movsw();
1734}
1735
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001736static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1737 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1738 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1739 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1740 // to avoid a SIGBUS.
1741 switch (size) {
1742 case Primitive::kPrimByte:
1743 __ movsxb(out, Address(address, 0));
1744 break;
1745 case Primitive::kPrimShort:
1746 __ movsxw(out, Address(address, 0));
1747 break;
1748 case Primitive::kPrimInt:
1749 __ movl(out, Address(address, 0));
1750 break;
1751 case Primitive::kPrimLong:
1752 __ movq(out, Address(address, 0));
1753 break;
1754 default:
1755 LOG(FATAL) << "Type not recognized for peek: " << size;
1756 UNREACHABLE();
1757 }
1758}
1759
1760void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1761 CreateIntToIntLocations(arena_, invoke);
1762}
1763
1764void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1765 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1766}
1767
1768void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1769 CreateIntToIntLocations(arena_, invoke);
1770}
1771
1772void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1773 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1774}
1775
1776void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1777 CreateIntToIntLocations(arena_, invoke);
1778}
1779
1780void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1781 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1782}
1783
1784void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1785 CreateIntToIntLocations(arena_, invoke);
1786}
1787
1788void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1789 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1790}
1791
1792static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1793 LocationSummary* locations = new (arena) LocationSummary(invoke,
1794 LocationSummary::kNoCall,
1795 kIntrinsified);
1796 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001797 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001798}
1799
1800static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1801 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001802 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001803 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1804 // to avoid a SIGBUS.
1805 switch (size) {
1806 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001807 if (value.IsConstant()) {
1808 __ movb(Address(address, 0),
1809 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1810 } else {
1811 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1812 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001813 break;
1814 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001815 if (value.IsConstant()) {
1816 __ movw(Address(address, 0),
1817 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1818 } else {
1819 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1820 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001821 break;
1822 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001823 if (value.IsConstant()) {
1824 __ movl(Address(address, 0),
1825 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1826 } else {
1827 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1828 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001829 break;
1830 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001831 if (value.IsConstant()) {
1832 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1833 DCHECK(IsInt<32>(v));
1834 int32_t v_32 = v;
1835 __ movq(Address(address, 0), Immediate(v_32));
1836 } else {
1837 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1838 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001839 break;
1840 default:
1841 LOG(FATAL) << "Type not recognized for poke: " << size;
1842 UNREACHABLE();
1843 }
1844}
1845
1846void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1847 CreateIntIntToVoidLocations(arena_, invoke);
1848}
1849
1850void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1851 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1852}
1853
1854void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1855 CreateIntIntToVoidLocations(arena_, invoke);
1856}
1857
1858void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1859 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1860}
1861
1862void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1863 CreateIntIntToVoidLocations(arena_, invoke);
1864}
1865
1866void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1867 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1868}
1869
1870void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1871 CreateIntIntToVoidLocations(arena_, invoke);
1872}
1873
1874void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1875 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1876}
1877
1878void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1879 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1880 LocationSummary::kNoCall,
1881 kIntrinsified);
1882 locations->SetOut(Location::RequiresRegister());
1883}
1884
1885void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1886 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001887 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(),
1888 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001889}
1890
Roland Levillain0d5a2812015-11-13 10:07:31 +00001891static void GenUnsafeGet(HInvoke* invoke,
1892 Primitive::Type type,
1893 bool is_volatile ATTRIBUTE_UNUSED,
1894 CodeGeneratorX86_64* codegen) {
1895 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
1896 LocationSummary* locations = invoke->GetLocations();
1897 Location base_loc = locations->InAt(1);
1898 CpuRegister base = base_loc.AsRegister<CpuRegister>();
1899 Location offset_loc = locations->InAt(2);
1900 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
1901 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001902 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001903
Andreas Gampe878d58c2015-01-15 23:24:00 -08001904 switch (type) {
1905 case Primitive::kPrimInt:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001906 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001907 break;
1908
1909 case Primitive::kPrimNot: {
1910 if (kEmitCompilerReadBarrier) {
1911 if (kUseBakerReadBarrier) {
1912 Location temp = locations->GetTemp(0);
1913 codegen->GenerateArrayLoadWithBakerReadBarrier(
1914 invoke, output_loc, base, 0U, offset_loc, temp, /* needs_null_check */ false);
1915 } else {
1916 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1917 codegen->GenerateReadBarrierSlow(
1918 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1919 }
1920 } else {
1921 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1922 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001923 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001924 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001925 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001926
1927 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001928 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08001929 break;
1930
1931 default:
1932 LOG(FATAL) << "Unsupported op size " << type;
1933 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001934 }
1935}
1936
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001937static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1938 HInvoke* invoke,
1939 Primitive::Type type) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001940 bool can_call = kEmitCompilerReadBarrier &&
1941 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1942 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001943 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001944 can_call ?
1945 LocationSummary::kCallOnSlowPath :
1946 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001947 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001948 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001949 locations->SetInAt(1, Location::RequiresRegister());
1950 locations->SetInAt(2, Location::RequiresRegister());
Andreas Gampe878d58c2015-01-15 23:24:00 -08001951 locations->SetOut(Location::RequiresRegister());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001952 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1953 // We need a temporary register for the read barrier marking slow
1954 // path in InstructionCodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier.
1955 locations->AddTemp(Location::RequiresRegister());
1956 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001957}
1958
1959void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001960 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001961}
1962void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001963 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001964}
1965void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001966 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001967}
1968void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001969 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001970}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001971void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001972 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001973}
1974void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001975 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001976}
1977
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001978
1979void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001980 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001981}
1982void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001983 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001984}
1985void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001986 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001987}
1988void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001989 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001990}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001991void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001992 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001993}
1994void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001995 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001996}
1997
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001998
1999static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
2000 Primitive::Type type,
2001 HInvoke* invoke) {
2002 LocationSummary* locations = new (arena) LocationSummary(invoke,
2003 LocationSummary::kNoCall,
2004 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002005 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002006 locations->SetInAt(1, Location::RequiresRegister());
2007 locations->SetInAt(2, Location::RequiresRegister());
2008 locations->SetInAt(3, Location::RequiresRegister());
2009 if (type == Primitive::kPrimNot) {
2010 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002011 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002012 locations->AddTemp(Location::RequiresRegister());
2013 }
2014}
2015
2016void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2017 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2018}
2019void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2020 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2021}
2022void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2023 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2024}
2025void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2026 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2027}
2028void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2029 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2030}
2031void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2032 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2033}
2034void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2035 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2036}
2037void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2038 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2039}
2040void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2041 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2042}
2043
2044// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2045// memory model.
2046static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2047 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002048 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002049 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2050 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2051 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2052
2053 if (type == Primitive::kPrimLong) {
2054 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002055 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2056 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2057 __ movl(temp, value);
2058 __ PoisonHeapReference(temp);
2059 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002060 } else {
2061 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2062 }
2063
2064 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002065 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002066 }
2067
2068 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002069 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002070 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2071 locations->GetTemp(1).AsRegister<CpuRegister>(),
2072 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002073 value,
2074 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002075 }
2076}
2077
2078void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002079 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002080}
2081void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002082 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002083}
2084void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002085 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002086}
2087void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002088 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002089}
2090void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002091 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002092}
2093void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002094 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002095}
2096void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002097 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002098}
2099void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002100 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002101}
2102void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002103 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002104}
2105
Mark Mendell58d25fd2015-04-03 14:52:31 -04002106static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2107 HInvoke* invoke) {
2108 LocationSummary* locations = new (arena) LocationSummary(invoke,
2109 LocationSummary::kNoCall,
2110 kIntrinsified);
2111 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2112 locations->SetInAt(1, Location::RequiresRegister());
2113 locations->SetInAt(2, Location::RequiresRegister());
2114 // expected value must be in EAX/RAX.
2115 locations->SetInAt(3, Location::RegisterLocation(RAX));
2116 locations->SetInAt(4, Location::RequiresRegister());
2117
2118 locations->SetOut(Location::RequiresRegister());
2119 if (type == Primitive::kPrimNot) {
2120 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002121 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002122 locations->AddTemp(Location::RequiresRegister());
2123 }
2124}
2125
2126void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2127 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2128}
2129
2130void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2131 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2132}
2133
2134void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002135 // The UnsafeCASObject intrinsic is missing a read barrier, and
2136 // therefore sometimes does not work as expected (b/25883050).
2137 // Turn it off temporarily as a quick fix, until the read barrier is
2138 // implemented.
2139 //
2140 // TODO(rpl): Implement a read barrier in GenCAS below and re-enable
2141 // this intrinsic.
2142 if (kEmitCompilerReadBarrier) {
2143 return;
2144 }
2145
Mark Mendell58d25fd2015-04-03 14:52:31 -04002146 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2147}
2148
2149static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002150 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002151 LocationSummary* locations = invoke->GetLocations();
2152
2153 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2154 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2155 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002156 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002157 DCHECK_EQ(expected.AsRegister(), RAX);
2158 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
2159 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2160
Roland Levillainb488b782015-10-22 11:38:49 +01002161 if (type == Primitive::kPrimNot) {
2162 // Mark card for object assuming new value is stored.
2163 bool value_can_be_null = true; // TODO: Worth finding out this information?
2164 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2165 locations->GetTemp(1).AsRegister<CpuRegister>(),
2166 base,
2167 value,
2168 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01002169
Roland Levillainb488b782015-10-22 11:38:49 +01002170 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2171 Register value_reg = value.AsRegister();
2172 if (kPoisonHeapReferences) {
2173 if (base_equals_value) {
2174 // If `base` and `value` are the same register location, move
2175 // `value_reg` to a temporary register. This way, poisoning
2176 // `value_reg` won't invalidate `base`.
2177 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
2178 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002179 }
Roland Levillainb488b782015-10-22 11:38:49 +01002180
2181 // Check that the register allocator did not assign the location
2182 // of `expected` (RAX) to `value` nor to `base`, so that heap
2183 // poisoning (when enabled) works as intended below.
2184 // - If `value` were equal to `expected`, both references would
2185 // be poisoned twice, meaning they would not be poisoned at
2186 // all, as heap poisoning uses address negation.
2187 // - If `base` were equal to `expected`, poisoning `expected`
2188 // would invalidate `base`.
2189 DCHECK_NE(value_reg, expected.AsRegister());
2190 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2191
2192 __ PoisonHeapReference(expected);
2193 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002194 }
2195
Roland Levillain391b8662015-12-18 11:43:38 +00002196 // TODO: Add a read barrier for the reference stored in the object
2197 // before attempting the CAS, similar to the one in the
2198 // art::Unsafe_compareAndSwapObject JNI implementation.
2199 //
2200 // Note that this code is not (yet) used when read barriers are
2201 // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject).
2202 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillainb488b782015-10-22 11:38:49 +01002203 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002204
Roland Levillain0d5a2812015-11-13 10:07:31 +00002205 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002206 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002207
Roland Levillainb488b782015-10-22 11:38:49 +01002208 // Convert ZF into the boolean result.
2209 __ setcc(kZero, out);
2210 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002211
Roland Levillain391b8662015-12-18 11:43:38 +00002212 // If heap poisoning is enabled, we need to unpoison the values
2213 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002214 if (kPoisonHeapReferences) {
2215 if (base_equals_value) {
2216 // `value_reg` has been moved to a temporary register, no need
2217 // to unpoison it.
2218 } else {
2219 // Ensure `value` is different from `out`, so that unpoisoning
2220 // the former does not invalidate the latter.
2221 DCHECK_NE(value_reg, out.AsRegister());
2222 __ UnpoisonHeapReference(CpuRegister(value_reg));
2223 }
2224 // Ensure `expected` is different from `out`, so that unpoisoning
2225 // the former does not invalidate the latter.
2226 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2227 __ UnpoisonHeapReference(expected);
2228 }
2229 } else {
2230 if (type == Primitive::kPrimInt) {
2231 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2232 } else if (type == Primitive::kPrimLong) {
2233 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2234 } else {
2235 LOG(FATAL) << "Unexpected CAS type " << type;
2236 }
2237
Roland Levillain0d5a2812015-11-13 10:07:31 +00002238 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002239 // scheduling barriers at this time.
2240
2241 // Convert ZF into the boolean result.
2242 __ setcc(kZero, out);
2243 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002244 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002245}
2246
2247void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2248 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2249}
2250
2251void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2252 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2253}
2254
2255void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
2256 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2257}
2258
2259void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2260 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2261 LocationSummary::kNoCall,
2262 kIntrinsified);
2263 locations->SetInAt(0, Location::RequiresRegister());
2264 locations->SetOut(Location::SameAsFirstInput());
2265 locations->AddTemp(Location::RequiresRegister());
2266}
2267
2268static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2269 X86_64Assembler* assembler) {
2270 Immediate imm_shift(shift);
2271 Immediate imm_mask(mask);
2272 __ movl(temp, reg);
2273 __ shrl(reg, imm_shift);
2274 __ andl(temp, imm_mask);
2275 __ andl(reg, imm_mask);
2276 __ shll(temp, imm_shift);
2277 __ orl(reg, temp);
2278}
2279
2280void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002281 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002282 LocationSummary* locations = invoke->GetLocations();
2283
2284 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2285 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2286
2287 /*
2288 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2289 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2290 * compared to generic luni implementation which has 5 rounds of swapping bits.
2291 * x = bswap x
2292 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2293 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2294 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2295 */
2296 __ bswapl(reg);
2297 SwapBits(reg, temp, 1, 0x55555555, assembler);
2298 SwapBits(reg, temp, 2, 0x33333333, assembler);
2299 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2300}
2301
2302void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2303 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2304 LocationSummary::kNoCall,
2305 kIntrinsified);
2306 locations->SetInAt(0, Location::RequiresRegister());
2307 locations->SetOut(Location::SameAsFirstInput());
2308 locations->AddTemp(Location::RequiresRegister());
2309 locations->AddTemp(Location::RequiresRegister());
2310}
2311
2312static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2313 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2314 Immediate imm_shift(shift);
2315 __ movq(temp_mask, Immediate(mask));
2316 __ movq(temp, reg);
2317 __ shrq(reg, imm_shift);
2318 __ andq(temp, temp_mask);
2319 __ andq(reg, temp_mask);
2320 __ shlq(temp, imm_shift);
2321 __ orq(reg, temp);
2322}
2323
2324void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002325 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002326 LocationSummary* locations = invoke->GetLocations();
2327
2328 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2329 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2330 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2331
2332 /*
2333 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2334 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2335 * compared to generic luni implementation which has 5 rounds of swapping bits.
2336 * x = bswap x
2337 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2338 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2339 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2340 */
2341 __ bswapq(reg);
2342 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2343 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2344 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2345}
2346
Aart Bik3f67e692016-01-15 14:35:12 -08002347static void CreateBitCountLocations(
2348 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2349 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2350 // Do nothing if there is no popcnt support. This results in generating
2351 // a call for the intrinsic rather than direct code.
2352 return;
2353 }
2354 LocationSummary* locations = new (arena) LocationSummary(invoke,
2355 LocationSummary::kNoCall,
2356 kIntrinsified);
2357 locations->SetInAt(0, Location::Any());
2358 locations->SetOut(Location::RequiresRegister());
2359}
2360
Aart Bikc5d47542016-01-27 17:00:35 -08002361static void GenBitCount(X86_64Assembler* assembler,
2362 CodeGeneratorX86_64* codegen,
2363 HInvoke* invoke,
2364 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002365 LocationSummary* locations = invoke->GetLocations();
2366 Location src = locations->InAt(0);
2367 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2368
2369 if (invoke->InputAt(0)->IsConstant()) {
2370 // Evaluate this at compile time.
2371 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002372 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002373 ? POPCOUNT(static_cast<uint64_t>(value))
2374 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002375 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002376 return;
2377 }
2378
2379 if (src.IsRegister()) {
2380 if (is_long) {
2381 __ popcntq(out, src.AsRegister<CpuRegister>());
2382 } else {
2383 __ popcntl(out, src.AsRegister<CpuRegister>());
2384 }
2385 } else if (is_long) {
2386 DCHECK(src.IsDoubleStackSlot());
2387 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2388 } else {
2389 DCHECK(src.IsStackSlot());
2390 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2391 }
2392}
2393
2394void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2395 CreateBitCountLocations(arena_, codegen_, invoke);
2396}
2397
2398void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002399 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002400}
2401
2402void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2403 CreateBitCountLocations(arena_, codegen_, invoke);
2404}
2405
2406void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002407 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2408}
2409
Aart Bikc5d47542016-01-27 17:00:35 -08002410static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2411 LocationSummary* locations = new (arena) LocationSummary(invoke,
2412 LocationSummary::kNoCall,
2413 kIntrinsified);
2414 locations->SetInAt(0, Location::Any());
2415 locations->SetOut(Location::RequiresRegister());
2416 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2417 : Location::RequiresRegister()); // any will do
2418}
2419
2420static void GenOneBit(X86_64Assembler* assembler,
2421 CodeGeneratorX86_64* codegen,
2422 HInvoke* invoke,
2423 bool is_high, bool is_long) {
2424 LocationSummary* locations = invoke->GetLocations();
2425 Location src = locations->InAt(0);
2426 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2427
2428 if (invoke->InputAt(0)->IsConstant()) {
2429 // Evaluate this at compile time.
2430 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2431 if (value == 0) {
2432 __ xorl(out, out); // Clears upper bits too.
2433 return;
2434 }
2435 // Nonzero value.
2436 if (is_high) {
2437 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2438 : 31 - CLZ(static_cast<uint32_t>(value));
2439 } else {
2440 value = is_long ? CTZ(static_cast<uint64_t>(value))
2441 : CTZ(static_cast<uint32_t>(value));
2442 }
2443 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002444 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002445 } else {
2446 codegen->Load32BitValue(out, 1 << value);
2447 }
2448 return;
2449 }
2450
2451 // Handle the non-constant cases.
2452 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2453 if (is_high) {
2454 // Use architectural support: basically 1 << bsr.
2455 if (src.IsRegister()) {
2456 if (is_long) {
2457 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2458 } else {
2459 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2460 }
2461 } else if (is_long) {
2462 DCHECK(src.IsDoubleStackSlot());
2463 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2464 } else {
2465 DCHECK(src.IsStackSlot());
2466 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2467 }
2468 // BSR sets ZF if the input was zero.
2469 NearLabel is_zero, done;
2470 __ j(kEqual, &is_zero);
2471 __ movl(out, Immediate(1)); // Clears upper bits too.
2472 if (is_long) {
2473 __ shlq(out, tmp);
2474 } else {
2475 __ shll(out, tmp);
2476 }
2477 __ jmp(&done);
2478 __ Bind(&is_zero);
2479 __ xorl(out, out); // Clears upper bits too.
2480 __ Bind(&done);
2481 } else {
2482 // Copy input into temporary.
2483 if (src.IsRegister()) {
2484 if (is_long) {
2485 __ movq(tmp, src.AsRegister<CpuRegister>());
2486 } else {
2487 __ movl(tmp, src.AsRegister<CpuRegister>());
2488 }
2489 } else if (is_long) {
2490 DCHECK(src.IsDoubleStackSlot());
2491 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2492 } else {
2493 DCHECK(src.IsStackSlot());
2494 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2495 }
2496 // Do the bit twiddling: basically tmp & -tmp;
2497 if (is_long) {
2498 __ movq(out, tmp);
2499 __ negq(tmp);
2500 __ andq(out, tmp);
2501 } else {
2502 __ movl(out, tmp);
2503 __ negl(tmp);
2504 __ andl(out, tmp);
2505 }
2506 }
2507}
2508
2509void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2510 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2511}
2512
2513void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2514 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2515}
2516
2517void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2518 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2519}
2520
2521void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2522 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2523}
2524
2525void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2526 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2527}
2528
2529void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2530 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2531}
2532
2533void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2534 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2535}
2536
2537void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2538 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002539}
2540
Mark Mendelld5897672015-08-12 21:16:41 -04002541static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2542 LocationSummary* locations = new (arena) LocationSummary(invoke,
2543 LocationSummary::kNoCall,
2544 kIntrinsified);
2545 locations->SetInAt(0, Location::Any());
2546 locations->SetOut(Location::RequiresRegister());
2547}
2548
Aart Bikc5d47542016-01-27 17:00:35 -08002549static void GenLeadingZeros(X86_64Assembler* assembler,
2550 CodeGeneratorX86_64* codegen,
2551 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002552 LocationSummary* locations = invoke->GetLocations();
2553 Location src = locations->InAt(0);
2554 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2555
2556 int zero_value_result = is_long ? 64 : 32;
2557 if (invoke->InputAt(0)->IsConstant()) {
2558 // Evaluate this at compile time.
2559 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2560 if (value == 0) {
2561 value = zero_value_result;
2562 } else {
2563 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2564 }
Aart Bikc5d47542016-01-27 17:00:35 -08002565 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002566 return;
2567 }
2568
2569 // Handle the non-constant cases.
2570 if (src.IsRegister()) {
2571 if (is_long) {
2572 __ bsrq(out, src.AsRegister<CpuRegister>());
2573 } else {
2574 __ bsrl(out, src.AsRegister<CpuRegister>());
2575 }
2576 } else if (is_long) {
2577 DCHECK(src.IsDoubleStackSlot());
2578 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2579 } else {
2580 DCHECK(src.IsStackSlot());
2581 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2582 }
2583
2584 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002585 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002586 __ j(kEqual, &is_zero);
2587
2588 // Correct the result from BSR to get the CLZ result.
2589 __ xorl(out, Immediate(zero_value_result - 1));
2590 __ jmp(&done);
2591
2592 // Fix the zero case with the expected result.
2593 __ Bind(&is_zero);
2594 __ movl(out, Immediate(zero_value_result));
2595
2596 __ Bind(&done);
2597}
2598
2599void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2600 CreateLeadingZeroLocations(arena_, invoke);
2601}
2602
2603void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002604 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002605}
2606
2607void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2608 CreateLeadingZeroLocations(arena_, invoke);
2609}
2610
2611void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002612 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002613}
2614
Mark Mendell2d554792015-09-15 21:45:18 -04002615static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2616 LocationSummary* locations = new (arena) LocationSummary(invoke,
2617 LocationSummary::kNoCall,
2618 kIntrinsified);
2619 locations->SetInAt(0, Location::Any());
2620 locations->SetOut(Location::RequiresRegister());
2621}
2622
Aart Bikc5d47542016-01-27 17:00:35 -08002623static void GenTrailingZeros(X86_64Assembler* assembler,
2624 CodeGeneratorX86_64* codegen,
2625 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002626 LocationSummary* locations = invoke->GetLocations();
2627 Location src = locations->InAt(0);
2628 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2629
2630 int zero_value_result = is_long ? 64 : 32;
2631 if (invoke->InputAt(0)->IsConstant()) {
2632 // Evaluate this at compile time.
2633 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2634 if (value == 0) {
2635 value = zero_value_result;
2636 } else {
2637 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2638 }
Aart Bikc5d47542016-01-27 17:00:35 -08002639 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002640 return;
2641 }
2642
2643 // Handle the non-constant cases.
2644 if (src.IsRegister()) {
2645 if (is_long) {
2646 __ bsfq(out, src.AsRegister<CpuRegister>());
2647 } else {
2648 __ bsfl(out, src.AsRegister<CpuRegister>());
2649 }
2650 } else if (is_long) {
2651 DCHECK(src.IsDoubleStackSlot());
2652 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2653 } else {
2654 DCHECK(src.IsStackSlot());
2655 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2656 }
2657
2658 // BSF sets ZF if the input was zero, and the output is undefined.
2659 NearLabel done;
2660 __ j(kNotEqual, &done);
2661
2662 // Fix the zero case with the expected result.
2663 __ movl(out, Immediate(zero_value_result));
2664
2665 __ Bind(&done);
2666}
2667
2668void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2669 CreateTrailingZeroLocations(arena_, invoke);
2670}
2671
2672void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002673 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002674}
2675
2676void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2677 CreateTrailingZeroLocations(arena_, invoke);
2678}
2679
2680void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002681 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2682}
2683
Serguei Katkov288c7a82016-05-16 11:53:15 +06002684void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2685 if (kEmitCompilerReadBarrier) {
2686 // Do not intrinsify this call with the read barrier configuration.
2687 return;
2688 }
2689 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2690 LocationSummary::kCallOnSlowPath,
2691 kIntrinsified);
2692 locations->SetInAt(0, Location::RequiresRegister());
2693 locations->SetOut(Location::SameAsFirstInput());
2694 locations->AddTemp(Location::RequiresRegister());
2695}
2696
2697void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2698 DCHECK(!kEmitCompilerReadBarrier);
2699 LocationSummary* locations = invoke->GetLocations();
2700 X86_64Assembler* assembler = GetAssembler();
2701
2702 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2703 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2704
2705 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2706 codegen_->AddSlowPath(slow_path);
2707
2708 // Load ArtMethod first.
2709 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2710 DCHECK(invoke_direct != nullptr);
2711 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2712 invoke_direct, locations->GetTemp(0));
2713 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2714 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2715
2716 // Now get declaring class.
2717 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2718
2719 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2720 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2721 DCHECK_NE(slow_path_flag_offset, 0u);
2722 DCHECK_NE(disable_flag_offset, 0u);
2723 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2724
2725 // Check static flags preventing us for using intrinsic.
2726 if (slow_path_flag_offset == disable_flag_offset + 1) {
2727 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2728 __ j(kNotEqual, slow_path->GetEntryLabel());
2729 } else {
2730 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2731 __ j(kNotEqual, slow_path->GetEntryLabel());
2732 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2733 __ j(kNotEqual, slow_path->GetEntryLabel());
2734 }
2735
2736 // Fast path.
2737 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2738 codegen_->MaybeRecordImplicitNullCheck(invoke);
2739 __ MaybeUnpoisonHeapReference(out);
2740 __ Bind(slow_path->GetExitLabel());
2741}
2742
Aart Bik2f9fcc92016-03-01 15:16:54 -08002743UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2744UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002745
Aart Bik0e54c012016-03-04 12:08:31 -08002746// 1.8.
2747UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2748UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2749UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2750UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2751UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002752
Aart Bik2f9fcc92016-03-01 15:16:54 -08002753UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002754
2755#undef __
2756
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002757} // namespace x86_64
2758} // namespace art