blob: c5b44d4f5c0c0d9ad2b9020bd52cbd8921966650 [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 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000057 return res->Intrinsified();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080058}
59
Roland Levillainec525fc2015-04-28 15:50:20 +010060static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010061 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010062 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080063}
64
Andreas Gampe85b62f22015-09-09 13:15:38 -070065using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080066
Andreas Gampe71fb52f2014-12-29 17:43:08 -080067#define __ assembler->
68
69static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
70 LocationSummary* locations = new (arena) LocationSummary(invoke,
71 LocationSummary::kNoCall,
72 kIntrinsified);
73 locations->SetInAt(0, Location::RequiresFpuRegister());
74 locations->SetOut(Location::RequiresRegister());
75}
76
77static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
78 LocationSummary* locations = new (arena) LocationSummary(invoke,
79 LocationSummary::kNoCall,
80 kIntrinsified);
81 locations->SetInAt(0, Location::RequiresRegister());
82 locations->SetOut(Location::RequiresFpuRegister());
83}
84
85static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
86 Location input = locations->InAt(0);
87 Location output = locations->Out();
88 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
89}
90
91static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
92 Location input = locations->InAt(0);
93 Location output = locations->Out();
94 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
95}
96
97void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
98 CreateFPToIntLocations(arena_, invoke);
99}
100void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
101 CreateIntToFPLocations(arena_, invoke);
102}
103
104void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000105 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800106}
107void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000108 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800109}
110
111void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
112 CreateFPToIntLocations(arena_, invoke);
113}
114void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
115 CreateIntToFPLocations(arena_, invoke);
116}
117
118void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000119 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800120}
121void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000122 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800123}
124
125static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
126 LocationSummary* locations = new (arena) LocationSummary(invoke,
127 LocationSummary::kNoCall,
128 kIntrinsified);
129 locations->SetInAt(0, Location::RequiresRegister());
130 locations->SetOut(Location::SameAsFirstInput());
131}
132
133static void GenReverseBytes(LocationSummary* locations,
134 Primitive::Type size,
135 X86_64Assembler* assembler) {
136 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
137
138 switch (size) {
139 case Primitive::kPrimShort:
140 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
141 __ bswapl(out);
142 __ sarl(out, Immediate(16));
143 break;
144 case Primitive::kPrimInt:
145 __ bswapl(out);
146 break;
147 case Primitive::kPrimLong:
148 __ bswapq(out);
149 break;
150 default:
151 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
152 UNREACHABLE();
153 }
154}
155
156void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
157 CreateIntToIntLocations(arena_, invoke);
158}
159
160void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
161 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
162}
163
164void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
165 CreateIntToIntLocations(arena_, invoke);
166}
167
168void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
169 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
170}
171
172void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
173 CreateIntToIntLocations(arena_, invoke);
174}
175
176void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
177 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
178}
179
180
181// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
182// need is 64b.
183
184static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
185 // TODO: Enable memory operations when the assembler supports them.
186 LocationSummary* locations = new (arena) LocationSummary(invoke,
187 LocationSummary::kNoCall,
188 kIntrinsified);
189 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800190 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400191 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800192}
193
Mark Mendell39dcf552015-04-09 20:42:42 -0400194static void MathAbsFP(LocationSummary* locations,
195 bool is64bit,
196 X86_64Assembler* assembler,
197 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800198 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800199
Mark Mendellcfa410b2015-05-25 16:02:44 -0400200 DCHECK(output.IsFpuRegister());
201 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800202
Mark Mendellcfa410b2015-05-25 16:02:44 -0400203 // TODO: Can mask directly with constant area using pand if we can guarantee
204 // that the literal is aligned on a 16 byte boundary. This will avoid a
205 // temporary.
206 if (is64bit) {
207 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
208 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800209 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400210 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
211 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800212 }
213}
214
215void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
216 CreateFloatToFloatPlusTemps(arena_, invoke);
217}
218
219void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000220 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800221}
222
223void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
224 CreateFloatToFloatPlusTemps(arena_, invoke);
225}
226
227void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000228 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800229}
230
231static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
232 LocationSummary* locations = new (arena) LocationSummary(invoke,
233 LocationSummary::kNoCall,
234 kIntrinsified);
235 locations->SetInAt(0, Location::RequiresRegister());
236 locations->SetOut(Location::SameAsFirstInput());
237 locations->AddTemp(Location::RequiresRegister());
238}
239
240static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
241 Location output = locations->Out();
242 CpuRegister out = output.AsRegister<CpuRegister>();
243 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
244
245 if (is64bit) {
246 // Create mask.
247 __ movq(mask, out);
248 __ sarq(mask, Immediate(63));
249 // Add mask.
250 __ addq(out, mask);
251 __ xorq(out, mask);
252 } else {
253 // Create mask.
254 __ movl(mask, out);
255 __ sarl(mask, Immediate(31));
256 // Add mask.
257 __ addl(out, mask);
258 __ xorl(out, mask);
259 }
260}
261
262void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
263 CreateIntToIntPlusTemp(arena_, invoke);
264}
265
266void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000267 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800268}
269
270void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
271 CreateIntToIntPlusTemp(arena_, invoke);
272}
273
274void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000275 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800276}
277
Mark Mendell39dcf552015-04-09 20:42:42 -0400278static void GenMinMaxFP(LocationSummary* locations,
279 bool is_min,
280 bool is_double,
281 X86_64Assembler* assembler,
282 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800283 Location op1_loc = locations->InAt(0);
284 Location op2_loc = locations->InAt(1);
285 Location out_loc = locations->Out();
286 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
287
288 // Shortcut for same input locations.
289 if (op1_loc.Equals(op2_loc)) {
290 DCHECK(out_loc.Equals(op1_loc));
291 return;
292 }
293
294 // (out := op1)
295 // out <=? op2
296 // if Nan jmp Nan_label
297 // if out is min jmp done
298 // if op2 is min jmp op2_label
299 // handle -0/+0
300 // jmp done
301 // Nan_label:
302 // out := NaN
303 // op2_label:
304 // out := op2
305 // done:
306 //
307 // This removes one jmp, but needs to copy one input (op1) to out.
308 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400309 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800310
311 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
312
Mark Mendell0c9497d2015-08-21 09:30:05 -0400313 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800314 if (is_double) {
315 __ ucomisd(out, op2);
316 } else {
317 __ ucomiss(out, op2);
318 }
319
320 __ j(Condition::kParityEven, &nan);
321
322 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
323 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
324
325 // Handle 0.0/-0.0.
326 if (is_min) {
327 if (is_double) {
328 __ orpd(out, op2);
329 } else {
330 __ orps(out, op2);
331 }
332 } else {
333 if (is_double) {
334 __ andpd(out, op2);
335 } else {
336 __ andps(out, op2);
337 }
338 }
339 __ jmp(&done);
340
341 // NaN handling.
342 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800343 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400344 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800345 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400346 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800347 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800348 __ jmp(&done);
349
350 // out := op2;
351 __ Bind(&op2_label);
352 if (is_double) {
353 __ movsd(out, op2);
354 } else {
355 __ movss(out, op2);
356 }
357
358 // Done.
359 __ Bind(&done);
360}
361
Mark Mendellf55c3e02015-03-26 21:07:46 -0400362static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800363 LocationSummary* locations = new (arena) LocationSummary(invoke,
364 LocationSummary::kNoCall,
365 kIntrinsified);
366 locations->SetInAt(0, Location::RequiresFpuRegister());
367 locations->SetInAt(1, Location::RequiresFpuRegister());
368 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
369 // the second input to be the output (we can simply swap inputs).
370 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800371}
372
373void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400374 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800375}
376
377void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000378 GenMinMaxFP(
379 invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800380}
381
382void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400383 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800384}
385
386void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000387 GenMinMaxFP(
388 invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800389}
390
391void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400392 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800393}
394
395void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000396 GenMinMaxFP(
397 invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800398}
399
400void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400401 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800402}
403
404void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000405 GenMinMaxFP(
406 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800407}
408
409static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
410 X86_64Assembler* assembler) {
411 Location op1_loc = locations->InAt(0);
412 Location op2_loc = locations->InAt(1);
413
414 // Shortcut for same input locations.
415 if (op1_loc.Equals(op2_loc)) {
416 // Can return immediately, as op1_loc == out_loc.
417 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
418 // a copy here.
419 DCHECK(locations->Out().Equals(op1_loc));
420 return;
421 }
422
423 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
424 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
425
426 // (out := op1)
427 // out <=? op2
428 // if out is min jmp done
429 // out := op2
430 // done:
431
432 if (is_long) {
433 __ cmpq(out, op2);
434 } else {
435 __ cmpl(out, op2);
436 }
437
438 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
439}
440
441static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
442 LocationSummary* locations = new (arena) LocationSummary(invoke,
443 LocationSummary::kNoCall,
444 kIntrinsified);
445 locations->SetInAt(0, Location::RequiresRegister());
446 locations->SetInAt(1, Location::RequiresRegister());
447 locations->SetOut(Location::SameAsFirstInput());
448}
449
450void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
451 CreateIntIntToIntLocations(arena_, invoke);
452}
453
454void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000455 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800456}
457
458void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
459 CreateIntIntToIntLocations(arena_, invoke);
460}
461
462void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000463 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800464}
465
466void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
467 CreateIntIntToIntLocations(arena_, invoke);
468}
469
470void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000471 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800472}
473
474void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
475 CreateIntIntToIntLocations(arena_, invoke);
476}
477
478void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000479 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800480}
481
482static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
483 LocationSummary* locations = new (arena) LocationSummary(invoke,
484 LocationSummary::kNoCall,
485 kIntrinsified);
486 locations->SetInAt(0, Location::RequiresFpuRegister());
487 locations->SetOut(Location::RequiresFpuRegister());
488}
489
490void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
491 CreateFPToFPLocations(arena_, invoke);
492}
493
494void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
495 LocationSummary* locations = invoke->GetLocations();
496 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
497 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
498
499 GetAssembler()->sqrtsd(out, in);
500}
501
Mark Mendellfb8d2792015-03-31 22:16:59 -0400502static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100503 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400504
505 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100506 codegen->GenerateStaticOrDirectCall(
507 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400508 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
509
510 // Copy the result back to the expected output.
511 Location out = invoke->GetLocations()->Out();
512 if (out.IsValid()) {
513 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700514 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400515 }
516}
517
518static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
519 HInvoke* invoke,
520 CodeGeneratorX86_64* codegen) {
521 // Do we have instruction support?
522 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
523 CreateFPToFPLocations(arena, invoke);
524 return;
525 }
526
527 // We have to fall back to a call to the intrinsic.
528 LocationSummary* locations = new (arena) LocationSummary(invoke,
529 LocationSummary::kCall);
530 InvokeRuntimeCallingConvention calling_convention;
531 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
532 locations->SetOut(Location::FpuRegisterLocation(XMM0));
533 // Needs to be RDI for the invoke.
534 locations->AddTemp(Location::RegisterLocation(RDI));
535}
536
537static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
538 HInvoke* invoke,
539 X86_64Assembler* assembler,
540 int round_mode) {
541 LocationSummary* locations = invoke->GetLocations();
542 if (locations->WillCall()) {
543 InvokeOutOfLineIntrinsic(codegen, invoke);
544 } else {
545 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
546 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
547 __ roundsd(out, in, Immediate(round_mode));
548 }
549}
550
551void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
552 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
553}
554
555void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
556 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
557}
558
559void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
560 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
561}
562
563void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
564 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
565}
566
567void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
568 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
569}
570
571void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
572 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
573}
574
575static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
576 HInvoke* invoke,
577 CodeGeneratorX86_64* codegen) {
578 // Do we have instruction support?
579 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
580 LocationSummary* locations = new (arena) LocationSummary(invoke,
581 LocationSummary::kNoCall,
582 kIntrinsified);
583 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600584 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400585 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400586 return;
587 }
588
589 // We have to fall back to a call to the intrinsic.
590 LocationSummary* locations = new (arena) LocationSummary(invoke,
591 LocationSummary::kCall);
592 InvokeRuntimeCallingConvention calling_convention;
593 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
594 locations->SetOut(Location::RegisterLocation(RAX));
595 // Needs to be RDI for the invoke.
596 locations->AddTemp(Location::RegisterLocation(RDI));
597}
598
599void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800600 // See intrinsics.h.
601 if (kRoundIsPlusPointFive) {
602 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
603 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400604}
605
606void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
607 LocationSummary* locations = invoke->GetLocations();
608 if (locations->WillCall()) {
609 InvokeOutOfLineIntrinsic(codegen_, invoke);
610 return;
611 }
612
613 // Implement RoundFloat as t1 = floor(input + 0.5f); convert to int.
614 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
615 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400616 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400617 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400618 X86_64Assembler* assembler = GetAssembler();
619
Mark Mendell40741f32015-04-20 22:10:34 -0400620 // Load 0.5 into inPlusPointFive.
621 __ movss(inPlusPointFive, codegen_->LiteralFloatAddress(0.5f));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400622
623 // Add in the input.
624 __ addss(inPlusPointFive, in);
625
626 // And truncate to an integer.
627 __ roundss(inPlusPointFive, inPlusPointFive, Immediate(1));
628
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600629 // Load maxInt into out.
630 codegen_->Load64BitValue(out, kPrimIntMax);
631
Mark Mendellfb8d2792015-03-31 22:16:59 -0400632 // if inPlusPointFive >= maxInt goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400633 __ comiss(inPlusPointFive, codegen_->LiteralFloatAddress(static_cast<float>(kPrimIntMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400634 __ j(kAboveEqual, &done);
635
636 // if input == NaN goto nan
637 __ j(kUnordered, &nan);
638
639 // output = float-to-int-truncate(input)
640 __ cvttss2si(out, inPlusPointFive);
641 __ jmp(&done);
642 __ Bind(&nan);
643
644 // output = 0
645 __ xorl(out, out);
646 __ Bind(&done);
647}
648
649void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
Andreas Gampee6d0d8d2015-12-28 09:54:29 -0800650 // See intrinsics.h.
651 if (kRoundIsPlusPointFive) {
652 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
653 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400654}
655
656void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
657 LocationSummary* locations = invoke->GetLocations();
658 if (locations->WillCall()) {
659 InvokeOutOfLineIntrinsic(codegen_, invoke);
660 return;
661 }
662
663 // Implement RoundDouble as t1 = floor(input + 0.5); convert to long.
664 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
665 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -0400666 XmmRegister inPlusPointFive = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -0400667 NearLabel done, nan;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400668 X86_64Assembler* assembler = GetAssembler();
669
Mark Mendell40741f32015-04-20 22:10:34 -0400670 // Load 0.5 into inPlusPointFive.
671 __ movsd(inPlusPointFive, codegen_->LiteralDoubleAddress(0.5));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400672
673 // Add in the input.
674 __ addsd(inPlusPointFive, in);
675
676 // And truncate to an integer.
677 __ roundsd(inPlusPointFive, inPlusPointFive, Immediate(1));
678
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600679 // Load maxLong into out.
680 codegen_->Load64BitValue(out, kPrimLongMax);
681
Mark Mendellfb8d2792015-03-31 22:16:59 -0400682 // if inPlusPointFive >= maxLong goto done
Mark Mendell40741f32015-04-20 22:10:34 -0400683 __ comisd(inPlusPointFive, codegen_->LiteralDoubleAddress(static_cast<double>(kPrimLongMax)));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400684 __ j(kAboveEqual, &done);
685
686 // if input == NaN goto nan
687 __ j(kUnordered, &nan);
688
689 // output = double-to-long-truncate(input)
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000690 __ cvttsd2si(out, inPlusPointFive, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400691 __ jmp(&done);
692 __ Bind(&nan);
693
694 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -0400695 __ xorl(out, out);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400696 __ Bind(&done);
697}
698
Mark Mendella4f12202015-08-06 15:23:34 -0400699static void CreateFPToFPCallLocations(ArenaAllocator* arena,
700 HInvoke* invoke) {
701 LocationSummary* locations = new (arena) LocationSummary(invoke,
702 LocationSummary::kCall,
703 kIntrinsified);
704 InvokeRuntimeCallingConvention calling_convention;
705 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
706 locations->SetOut(Location::FpuRegisterLocation(XMM0));
707
708 // We have to ensure that the native code doesn't clobber the XMM registers which are
709 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
710 // saved in the prologue and properly restored.
711 for (auto fp_reg : non_volatile_xmm_regs) {
712 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
713 }
714}
715
716static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
717 QuickEntrypointEnum entry) {
718 LocationSummary* locations = invoke->GetLocations();
719 DCHECK(locations->WillCall());
720 DCHECK(invoke->IsInvokeStaticOrDirect());
721 X86_64Assembler* assembler = codegen->GetAssembler();
722
723 __ gs()->call(Address::Absolute(GetThreadOffset<kX86_64WordSize>(entry), true));
724 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
725}
726
727void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
728 CreateFPToFPCallLocations(arena_, invoke);
729}
730
731void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
732 GenFPToFPCall(invoke, codegen_, kQuickCos);
733}
734
735void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
736 CreateFPToFPCallLocations(arena_, invoke);
737}
738
739void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
740 GenFPToFPCall(invoke, codegen_, kQuickSin);
741}
742
743void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
744 CreateFPToFPCallLocations(arena_, invoke);
745}
746
747void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
748 GenFPToFPCall(invoke, codegen_, kQuickAcos);
749}
750
751void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
752 CreateFPToFPCallLocations(arena_, invoke);
753}
754
755void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
756 GenFPToFPCall(invoke, codegen_, kQuickAsin);
757}
758
759void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
760 CreateFPToFPCallLocations(arena_, invoke);
761}
762
763void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
764 GenFPToFPCall(invoke, codegen_, kQuickAtan);
765}
766
767void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
768 CreateFPToFPCallLocations(arena_, invoke);
769}
770
771void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
772 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
773}
774
775void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
776 CreateFPToFPCallLocations(arena_, invoke);
777}
778
779void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
780 GenFPToFPCall(invoke, codegen_, kQuickCosh);
781}
782
783void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
784 CreateFPToFPCallLocations(arena_, invoke);
785}
786
787void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
788 GenFPToFPCall(invoke, codegen_, kQuickExp);
789}
790
791void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
792 CreateFPToFPCallLocations(arena_, invoke);
793}
794
795void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
796 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
797}
798
799void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
800 CreateFPToFPCallLocations(arena_, invoke);
801}
802
803void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
804 GenFPToFPCall(invoke, codegen_, kQuickLog);
805}
806
807void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
808 CreateFPToFPCallLocations(arena_, invoke);
809}
810
811void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
812 GenFPToFPCall(invoke, codegen_, kQuickLog10);
813}
814
815void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
816 CreateFPToFPCallLocations(arena_, invoke);
817}
818
819void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
820 GenFPToFPCall(invoke, codegen_, kQuickSinh);
821}
822
823void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
824 CreateFPToFPCallLocations(arena_, invoke);
825}
826
827void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
828 GenFPToFPCall(invoke, codegen_, kQuickTan);
829}
830
831void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
832 CreateFPToFPCallLocations(arena_, invoke);
833}
834
835void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
836 GenFPToFPCall(invoke, codegen_, kQuickTanh);
837}
838
839static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
840 HInvoke* invoke) {
841 LocationSummary* locations = new (arena) LocationSummary(invoke,
842 LocationSummary::kCall,
843 kIntrinsified);
844 InvokeRuntimeCallingConvention calling_convention;
845 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
846 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
847 locations->SetOut(Location::FpuRegisterLocation(XMM0));
848
849 // We have to ensure that the native code doesn't clobber the XMM registers which are
850 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
851 // saved in the prologue and properly restored.
852 for (auto fp_reg : non_volatile_xmm_regs) {
853 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
854 }
855}
856
857void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
858 CreateFPFPToFPCallLocations(arena_, invoke);
859}
860
861void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
862 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
863}
864
865void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
866 CreateFPFPToFPCallLocations(arena_, invoke);
867}
868
869void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
870 GenFPToFPCall(invoke, codegen_, kQuickHypot);
871}
872
873void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
874 CreateFPFPToFPCallLocations(arena_, invoke);
875}
876
877void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
878 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
879}
880
Mark Mendell6bc53a92015-07-01 14:26:52 -0400881void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
882 // Check to see if we have known failures that will cause us to have to bail out
883 // to the runtime, and just generate the runtime call directly.
884 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
885 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
886
887 // The positions must be non-negative.
888 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
889 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
890 // We will have to fail anyways.
891 return;
892 }
893
894 // The length must be > 0.
895 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
896 if (length != nullptr) {
897 int32_t len = length->GetValue();
898 if (len < 0) {
899 // Just call as normal.
900 return;
901 }
902 }
903
904 LocationSummary* locations = new (arena_) LocationSummary(invoke,
905 LocationSummary::kCallOnSlowPath,
906 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100907 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400908 locations->SetInAt(0, Location::RequiresRegister());
909 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
910 locations->SetInAt(2, Location::RequiresRegister());
911 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
912 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
913
914 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
915 locations->AddTemp(Location::RegisterLocation(RSI));
916 locations->AddTemp(Location::RegisterLocation(RDI));
917 locations->AddTemp(Location::RegisterLocation(RCX));
918}
919
920static void CheckPosition(X86_64Assembler* assembler,
921 Location pos,
922 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100923 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700924 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400925 CpuRegister input_len,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100926 CpuRegister temp,
927 bool length_is_input_length = false) {
928 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400929 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
930
931 if (pos.IsConstant()) {
932 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
933 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100934 if (!length_is_input_length) {
935 // Check that length(input) >= length.
936 if (length.IsConstant()) {
937 __ cmpl(Address(input, length_offset),
938 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
939 } else {
940 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
941 }
942 __ j(kLess, slow_path->GetEntryLabel());
943 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400944 } else {
945 // Check that length(input) >= pos.
946 __ movl(input_len, Address(input, length_offset));
947 __ cmpl(input_len, Immediate(pos_const));
948 __ j(kLess, slow_path->GetEntryLabel());
949
950 // Check that (length(input) - pos) >= length.
951 __ leal(temp, Address(input_len, -pos_const));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100952 if (length.IsConstant()) {
953 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
954 } else {
955 __ cmpl(temp, length.AsRegister<CpuRegister>());
956 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400957 __ j(kLess, slow_path->GetEntryLabel());
958 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100959 } else if (length_is_input_length) {
960 // The only way the copy can succeed is if pos is zero.
961 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
962 __ testl(pos_reg, pos_reg);
963 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400964 } else {
965 // Check that pos >= 0.
966 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
967 __ testl(pos_reg, pos_reg);
968 __ j(kLess, slow_path->GetEntryLabel());
969
970 // Check that pos <= length(input).
971 __ cmpl(Address(input, length_offset), pos_reg);
972 __ j(kLess, slow_path->GetEntryLabel());
973
974 // Check that (length(input) - pos) >= length.
975 __ movl(temp, Address(input, length_offset));
976 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100977 if (length.IsConstant()) {
978 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
979 } else {
980 __ cmpl(temp, length.AsRegister<CpuRegister>());
981 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400982 __ j(kLess, slow_path->GetEntryLabel());
983 }
984}
985
986void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
987 X86_64Assembler* assembler = GetAssembler();
988 LocationSummary* locations = invoke->GetLocations();
989
990 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100991 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400992 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100993 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400994 Location length = locations->InAt(4);
995
996 // Temporaries that we need for MOVSW.
997 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
998 DCHECK_EQ(src_base.AsRegister(), RSI);
999 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1000 DCHECK_EQ(dest_base.AsRegister(), RDI);
1001 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1002 DCHECK_EQ(count.AsRegister(), RCX);
1003
Andreas Gampe85b62f22015-09-09 13:15:38 -07001004 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001005 codegen_->AddSlowPath(slow_path);
1006
1007 // Bail out if the source and destination are the same.
1008 __ cmpl(src, dest);
1009 __ j(kEqual, slow_path->GetEntryLabel());
1010
1011 // Bail out if the source is null.
1012 __ testl(src, src);
1013 __ j(kEqual, slow_path->GetEntryLabel());
1014
1015 // Bail out if the destination is null.
1016 __ testl(dest, dest);
1017 __ j(kEqual, slow_path->GetEntryLabel());
1018
1019 // If the length is negative, bail out.
1020 // We have already checked in the LocationsBuilder for the constant case.
1021 if (!length.IsConstant()) {
1022 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1023 __ j(kLess, slow_path->GetEntryLabel());
1024 }
1025
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001026 // Validity checks: source.
1027 CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base);
1028
1029 // Validity checks: dest.
1030 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base);
1031
Mark Mendell6bc53a92015-07-01 14:26:52 -04001032 // We need the count in RCX.
1033 if (length.IsConstant()) {
1034 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1035 } else {
1036 __ movl(count, length.AsRegister<CpuRegister>());
1037 }
1038
Mark Mendell6bc53a92015-07-01 14:26:52 -04001039 // Okay, everything checks out. Finally time to do the copy.
1040 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1041 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1042 DCHECK_EQ(char_size, 2u);
1043
1044 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1045
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001046 if (src_pos.IsConstant()) {
1047 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1048 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001049 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001050 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001051 ScaleFactor::TIMES_2, data_offset));
1052 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001053 if (dest_pos.IsConstant()) {
1054 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1055 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001056 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001057 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001058 ScaleFactor::TIMES_2, data_offset));
1059 }
1060
1061 // Do the move.
1062 __ rep_movsw();
1063
1064 __ Bind(slow_path->GetExitLabel());
1065}
1066
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001067
1068void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01001069 // TODO(rpl): Implement read barriers in the SystemArrayCopy
1070 // intrinsic and re-enable it (b/29516905).
1071 if (kEmitCompilerReadBarrier) {
1072 return;
1073 }
1074
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001075 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001076}
1077
1078void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01001079 // TODO(rpl): Implement read barriers in the SystemArrayCopy
1080 // intrinsic and re-enable it (b/29516905).
1081 DCHECK(!kEmitCompilerReadBarrier);
1082
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001083 X86_64Assembler* assembler = GetAssembler();
1084 LocationSummary* locations = invoke->GetLocations();
1085
1086 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1087 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1088 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1089 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1090
1091 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1092 Location src_pos = locations->InAt(1);
1093 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1094 Location dest_pos = locations->InAt(3);
1095 Location length = locations->InAt(4);
1096 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
1097 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
1098 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
1099
1100 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1101 codegen_->AddSlowPath(slow_path);
1102
Roland Levillainebea3d22016-04-12 15:42:57 +01001103 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001104 SystemArrayCopyOptimizations optimizations(invoke);
1105
Roland Levillainebea3d22016-04-12 15:42:57 +01001106 if (!optimizations.GetDestinationIsSource() &&
1107 (!src_pos.IsConstant() || !dest_pos.IsConstant())) {
1108 __ cmpl(src, dest);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001109 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001110 // If source and destination are the same, we go to slow path if we need to do
1111 // forward copying.
1112 if (src_pos.IsConstant()) {
1113 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1114 if (dest_pos.IsConstant()) {
1115 // Checked when building locations.
1116 DCHECK(!optimizations.GetDestinationIsSource()
1117 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
1118 } else {
1119 if (!optimizations.GetDestinationIsSource()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001120 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001121 }
1122 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
1123 __ j(kGreater, slow_path->GetEntryLabel());
1124 }
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 if (dest_pos.IsConstant()) {
1130 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1131 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
1132 __ j(kLess, slow_path->GetEntryLabel());
1133 } else {
1134 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
1135 __ j(kLess, slow_path->GetEntryLabel());
1136 }
1137 }
1138
Roland Levillainebea3d22016-04-12 15:42:57 +01001139 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001140
1141 if (!optimizations.GetSourceIsNotNull()) {
1142 // Bail out if the source is null.
1143 __ testl(src, src);
1144 __ j(kEqual, slow_path->GetEntryLabel());
1145 }
1146
1147 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1148 // Bail out if the destination is null.
1149 __ testl(dest, dest);
1150 __ j(kEqual, slow_path->GetEntryLabel());
1151 }
1152
1153 // If the length is negative, bail out.
1154 // We have already checked in the LocationsBuilder for the constant case.
1155 if (!length.IsConstant() &&
1156 !optimizations.GetCountIsSourceLength() &&
1157 !optimizations.GetCountIsDestinationLength()) {
1158 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1159 __ j(kLess, slow_path->GetEntryLabel());
1160 }
1161
1162 // Validity checks: source.
1163 CheckPosition(assembler,
1164 src_pos,
1165 src,
1166 length,
1167 slow_path,
1168 temp1,
1169 temp2,
1170 optimizations.GetCountIsSourceLength());
1171
1172 // Validity checks: dest.
1173 CheckPosition(assembler,
1174 dest_pos,
1175 dest,
1176 length,
1177 slow_path,
1178 temp1,
1179 temp2,
1180 optimizations.GetCountIsDestinationLength());
1181
1182 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1183 // Check whether all elements of the source array are assignable to the component
1184 // type of the destination array. We do two checks: the classes are the same,
1185 // or the destination is Object[]. If none of these checks succeed, we go to the
1186 // slow path.
1187 __ movl(temp1, Address(dest, class_offset));
1188 __ movl(temp2, Address(src, class_offset));
1189 bool did_unpoison = false;
1190 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1191 !optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001192 // One or two of the references need to be unpoisoned. Unpoison them
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001193 // both to make the identity check valid.
1194 __ MaybeUnpoisonHeapReference(temp1);
1195 __ MaybeUnpoisonHeapReference(temp2);
1196 did_unpoison = true;
1197 }
1198
1199 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1200 // Bail out if the destination is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001201 // /* HeapReference<Class> */ TMP = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001202 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1203 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1204 __ j(kEqual, slow_path->GetEntryLabel());
1205 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1206 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1207 __ j(kNotEqual, slow_path->GetEntryLabel());
1208 }
1209
1210 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1211 // Bail out if the source is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001212 // /* HeapReference<Class> */ TMP = temp2->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001213 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1214 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1215 __ j(kEqual, slow_path->GetEntryLabel());
1216 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1217 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1218 __ j(kNotEqual, slow_path->GetEntryLabel());
1219 }
1220
1221 __ cmpl(temp1, temp2);
1222
1223 if (optimizations.GetDestinationIsTypedObjectArray()) {
1224 NearLabel do_copy;
1225 __ j(kEqual, &do_copy);
1226 if (!did_unpoison) {
1227 __ MaybeUnpoisonHeapReference(temp1);
1228 }
Roland Levillainebea3d22016-04-12 15:42:57 +01001229 // /* HeapReference<Class> */ temp1 = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001230 __ movl(temp1, Address(temp1, component_offset));
1231 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillainebea3d22016-04-12 15:42:57 +01001232 // /* HeapReference<Class> */ temp1 = temp1->super_class_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001233 __ movl(temp1, Address(temp1, super_offset));
1234 // No need to unpoison the result, we're comparing against null.
1235 __ testl(temp1, temp1);
1236 __ j(kNotEqual, slow_path->GetEntryLabel());
1237 __ Bind(&do_copy);
1238 } else {
1239 __ j(kNotEqual, slow_path->GetEntryLabel());
1240 }
1241 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1242 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1243 // Bail out if the source is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001244 // /* HeapReference<Class> */ temp1 = src->klass_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001245 __ movl(temp1, Address(src, class_offset));
1246 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillainebea3d22016-04-12 15:42:57 +01001247 // /* HeapReference<Class> */ TMP = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001248 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1249 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1250 __ j(kEqual, slow_path->GetEntryLabel());
1251 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1252 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1253 __ j(kNotEqual, slow_path->GetEntryLabel());
1254 }
1255
1256 // Compute base source address, base destination address, and end source address.
1257
1258 uint32_t element_size = sizeof(int32_t);
1259 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1260 if (src_pos.IsConstant()) {
1261 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1262 __ leal(temp1, Address(src, element_size * constant + offset));
1263 } else {
1264 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1265 }
1266
1267 if (dest_pos.IsConstant()) {
1268 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1269 __ leal(temp2, Address(dest, element_size * constant + offset));
1270 } else {
1271 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1272 }
1273
1274 if (length.IsConstant()) {
1275 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1276 __ leal(temp3, Address(temp1, element_size * constant));
1277 } else {
1278 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1279 }
1280
1281 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1282 // poison/unpoison, nor do any read barrier as the next uses of the destination
1283 // array will do it.
1284 NearLabel loop, done;
1285 __ cmpl(temp1, temp3);
1286 __ j(kEqual, &done);
1287 __ Bind(&loop);
1288 __ movl(CpuRegister(TMP), Address(temp1, 0));
1289 __ movl(Address(temp2, 0), CpuRegister(TMP));
1290 __ addl(temp1, Immediate(element_size));
1291 __ addl(temp2, Immediate(element_size));
1292 __ cmpl(temp1, temp3);
1293 __ j(kNotEqual, &loop);
1294 __ Bind(&done);
1295
1296 // We only need one card marking on the destination array.
1297 codegen_->MarkGCCard(temp1,
1298 temp2,
1299 dest,
1300 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001301 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001302
1303 __ Bind(slow_path->GetExitLabel());
1304}
1305
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001306void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1307 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1308 LocationSummary::kCall,
1309 kIntrinsified);
1310 InvokeRuntimeCallingConvention calling_convention;
1311 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1312 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1313 locations->SetOut(Location::RegisterLocation(RAX));
1314}
1315
1316void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1317 X86_64Assembler* assembler = GetAssembler();
1318 LocationSummary* locations = invoke->GetLocations();
1319
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001320 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001321 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001322
1323 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1324 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001325 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001326 codegen_->AddSlowPath(slow_path);
1327 __ j(kEqual, slow_path->GetEntryLabel());
1328
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001329 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo),
1330 /* no_rip */ true));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001331 __ Bind(slow_path->GetExitLabel());
1332}
1333
Agi Csakif8cfb202015-08-13 17:54:54 -07001334void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1335 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1336 LocationSummary::kNoCall,
1337 kIntrinsified);
1338 locations->SetInAt(0, Location::RequiresRegister());
1339 locations->SetInAt(1, Location::RequiresRegister());
1340
1341 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1342 locations->AddTemp(Location::RegisterLocation(RCX));
1343 locations->AddTemp(Location::RegisterLocation(RDI));
1344
1345 // Set output, RSI needed for repe_cmpsq instruction anyways.
1346 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1347}
1348
1349void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1350 X86_64Assembler* assembler = GetAssembler();
1351 LocationSummary* locations = invoke->GetLocations();
1352
1353 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1354 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1355 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1356 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1357 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1358
Mark Mendell0c9497d2015-08-21 09:30:05 -04001359 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001360
1361 // Get offsets of count, value, and class fields within a string object.
1362 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1363 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1364 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1365
1366 // Note that the null check must have been done earlier.
1367 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1368
Vladimir Marko53b52002016-05-24 19:30:45 +01001369 StringEqualsOptimizations optimizations(invoke);
1370 if (!optimizations.GetArgumentNotNull()) {
1371 // Check if input is null, return false if it is.
1372 __ testl(arg, arg);
1373 __ j(kEqual, &return_false);
1374 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001375
Vladimir Marko53b52002016-05-24 19:30:45 +01001376 if (!optimizations.GetArgumentIsString()) {
1377 // Instanceof check for the argument by comparing class fields.
1378 // All string objects must have the same type since String cannot be subclassed.
1379 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1380 // If the argument is a string object, its class field must be equal to receiver's class field.
1381 __ movl(rcx, Address(str, class_offset));
1382 __ cmpl(rcx, Address(arg, class_offset));
1383 __ j(kNotEqual, &return_false);
1384 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001385
1386 // Reference equality check, return true if same reference.
1387 __ cmpl(str, arg);
1388 __ j(kEqual, &return_true);
1389
1390 // Load length of receiver string.
1391 __ movl(rcx, Address(str, count_offset));
1392 // Check if lengths are equal, return false if they're not.
1393 __ cmpl(rcx, Address(arg, count_offset));
1394 __ j(kNotEqual, &return_false);
1395 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001396 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001397
1398 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1399 __ leal(rsi, Address(str, value_offset));
1400 __ leal(rdi, Address(arg, value_offset));
1401
1402 // Divide string length by 4 and adjust for lengths not divisible by 4.
1403 __ addl(rcx, Immediate(3));
1404 __ shrl(rcx, Immediate(2));
1405
1406 // Assertions that must hold in order to compare strings 4 characters at a time.
1407 DCHECK_ALIGNED(value_offset, 8);
1408 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1409
1410 // Loop to compare strings four characters at a time starting at the beginning of the string.
1411 __ repe_cmpsq();
1412 // If strings are not equal, zero flag will be cleared.
1413 __ j(kNotEqual, &return_false);
1414
1415 // Return true and exit the function.
1416 // If loop does not result in returning false, we return true.
1417 __ Bind(&return_true);
1418 __ movl(rsi, Immediate(1));
1419 __ jmp(&end);
1420
1421 // Return false and exit the function.
1422 __ Bind(&return_false);
1423 __ xorl(rsi, rsi);
1424 __ Bind(&end);
1425}
1426
Andreas Gampe21030dd2015-05-07 14:46:15 -07001427static void CreateStringIndexOfLocations(HInvoke* invoke,
1428 ArenaAllocator* allocator,
1429 bool start_at_zero) {
1430 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1431 LocationSummary::kCallOnSlowPath,
1432 kIntrinsified);
1433 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1434 locations->SetInAt(0, Location::RegisterLocation(RDI));
1435 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1436 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1437 // of the instruction explicitly.
1438 // Note: This works as we don't clobber RAX anywhere.
1439 locations->SetInAt(1, Location::RegisterLocation(RAX));
1440 if (!start_at_zero) {
1441 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1442 }
1443 // As we clobber RDI during execution anyways, also use it as the output.
1444 locations->SetOut(Location::SameAsFirstInput());
1445
1446 // repne scasw uses RCX as the counter.
1447 locations->AddTemp(Location::RegisterLocation(RCX));
1448 // Need another temporary to be able to compute the result.
1449 locations->AddTemp(Location::RequiresRegister());
1450}
1451
1452static void GenerateStringIndexOf(HInvoke* invoke,
1453 X86_64Assembler* assembler,
1454 CodeGeneratorX86_64* codegen,
1455 ArenaAllocator* allocator,
1456 bool start_at_zero) {
1457 LocationSummary* locations = invoke->GetLocations();
1458
1459 // Note that the null check must have been done earlier.
1460 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1461
1462 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1463 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1464 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1465 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1466 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1467
1468 // Check our assumptions for registers.
1469 DCHECK_EQ(string_obj.AsRegister(), RDI);
1470 DCHECK_EQ(search_value.AsRegister(), RAX);
1471 DCHECK_EQ(counter.AsRegister(), RCX);
1472 DCHECK_EQ(out.AsRegister(), RDI);
1473
1474 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001475 // 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 -07001476 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001477 HInstruction* code_point = invoke->InputAt(1);
1478 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001479 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001480 std::numeric_limits<uint16_t>::max()) {
1481 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1482 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1483 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1484 codegen->AddSlowPath(slow_path);
1485 __ jmp(slow_path->GetEntryLabel());
1486 __ Bind(slow_path->GetExitLabel());
1487 return;
1488 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001489 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001490 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1491 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1492 codegen->AddSlowPath(slow_path);
1493 __ j(kAbove, slow_path->GetEntryLabel());
1494 }
1495
1496 // From here down, we know that we are looking for a char that fits in 16 bits.
1497 // Location of reference to data array within the String object.
1498 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1499 // Location of count within the String object.
1500 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1501
1502 // Load string length, i.e., the count field of the string.
1503 __ movl(string_length, Address(string_obj, count_offset));
1504
1505 // Do a length check.
1506 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001507 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001508 __ testl(string_length, string_length);
1509 __ j(kEqual, &not_found_label);
1510
1511 if (start_at_zero) {
1512 // Number of chars to scan is the same as the string length.
1513 __ movl(counter, string_length);
1514
1515 // Move to the start of the string.
1516 __ addq(string_obj, Immediate(value_offset));
1517 } else {
1518 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1519
1520 // Do a start_index check.
1521 __ cmpl(start_index, string_length);
1522 __ j(kGreaterEqual, &not_found_label);
1523
1524 // Ensure we have a start index >= 0;
1525 __ xorl(counter, counter);
1526 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001527 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001528
1529 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1530 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1531
1532 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1533 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1534 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1535 }
1536
1537 // Everything is set up for repne scasw:
1538 // * Comparison address in RDI.
1539 // * Counter in ECX.
1540 __ repne_scasw();
1541
1542 // Did we find a match?
1543 __ j(kNotEqual, &not_found_label);
1544
1545 // Yes, we matched. Compute the index of the result.
1546 __ subl(string_length, counter);
1547 __ leal(out, Address(string_length, -1));
1548
Mark Mendell0c9497d2015-08-21 09:30:05 -04001549 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001550 __ jmp(&done);
1551
1552 // Failed to match; return -1.
1553 __ Bind(&not_found_label);
1554 __ movl(out, Immediate(-1));
1555
1556 // And join up at the end.
1557 __ Bind(&done);
1558 if (slow_path != nullptr) {
1559 __ Bind(slow_path->GetExitLabel());
1560 }
1561}
1562
1563void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001564 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001565}
1566
1567void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001568 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001569}
1570
1571void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001572 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001573}
1574
1575void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001576 GenerateStringIndexOf(
1577 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001578}
1579
Jeff Hao848f70a2014-01-15 13:49:50 -08001580void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1581 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1582 LocationSummary::kCall,
1583 kIntrinsified);
1584 InvokeRuntimeCallingConvention calling_convention;
1585 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1586 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1587 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1588 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1589 locations->SetOut(Location::RegisterLocation(RAX));
1590}
1591
1592void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1593 X86_64Assembler* assembler = GetAssembler();
1594 LocationSummary* locations = invoke->GetLocations();
1595
1596 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1597 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001598 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001599 codegen_->AddSlowPath(slow_path);
1600 __ j(kEqual, slow_path->GetEntryLabel());
1601
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001602 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes),
1603 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001604 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001605 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1606 __ Bind(slow_path->GetExitLabel());
1607}
1608
1609void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1610 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1611 LocationSummary::kCall,
1612 kIntrinsified);
1613 InvokeRuntimeCallingConvention calling_convention;
1614 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1615 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1616 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1617 locations->SetOut(Location::RegisterLocation(RAX));
1618}
1619
1620void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1621 X86_64Assembler* assembler = GetAssembler();
1622
Roland Levillaincc3839c2016-02-29 16:23:48 +00001623 // No need to emit code checking whether `locations->InAt(2)` is a null
1624 // pointer, as callers of the native method
1625 //
1626 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1627 //
1628 // all include a null check on `data` before calling that method.
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001629 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars),
1630 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001631 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001632 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1633}
1634
1635void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1636 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1637 LocationSummary::kCall,
1638 kIntrinsified);
1639 InvokeRuntimeCallingConvention calling_convention;
1640 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1641 locations->SetOut(Location::RegisterLocation(RAX));
1642}
1643
1644void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1645 X86_64Assembler* assembler = GetAssembler();
1646 LocationSummary* locations = invoke->GetLocations();
1647
1648 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1649 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001650 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001651 codegen_->AddSlowPath(slow_path);
1652 __ j(kEqual, slow_path->GetEntryLabel());
1653
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001654 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString),
1655 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001656 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001657 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1658 __ Bind(slow_path->GetExitLabel());
1659}
1660
Mark Mendell8f8926a2015-08-17 11:39:06 -04001661void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1662 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1663 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1664 LocationSummary::kNoCall,
1665 kIntrinsified);
1666 locations->SetInAt(0, Location::RequiresRegister());
1667 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1668 locations->SetInAt(2, Location::RequiresRegister());
1669 locations->SetInAt(3, Location::RequiresRegister());
1670 locations->SetInAt(4, Location::RequiresRegister());
1671
1672 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1673 locations->AddTemp(Location::RegisterLocation(RSI));
1674 locations->AddTemp(Location::RegisterLocation(RDI));
1675 locations->AddTemp(Location::RegisterLocation(RCX));
1676}
1677
1678void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1679 X86_64Assembler* assembler = GetAssembler();
1680 LocationSummary* locations = invoke->GetLocations();
1681
1682 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1683 // Location of data in char array buffer.
1684 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1685 // Location of char array data in string.
1686 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1687
1688 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1689 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1690 Location srcBegin = locations->InAt(1);
1691 int srcBegin_value =
1692 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1693 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1694 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1695 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1696
1697 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1698 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1699 DCHECK_EQ(char_size, 2u);
1700
1701 // Compute the address of the destination buffer.
1702 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1703
1704 // Compute the address of the source string.
1705 if (srcBegin.IsConstant()) {
1706 // Compute the address of the source string by adding the number of chars from
1707 // the source beginning to the value offset of a string.
1708 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1709 } else {
1710 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1711 ScaleFactor::TIMES_2, value_offset));
1712 }
1713
1714 // Compute the number of chars (words) to move.
1715 __ movl(CpuRegister(RCX), srcEnd);
1716 if (srcBegin.IsConstant()) {
1717 if (srcBegin_value != 0) {
1718 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1719 }
1720 } else {
1721 DCHECK(srcBegin.IsRegister());
1722 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1723 }
1724
1725 // Do the move.
1726 __ rep_movsw();
1727}
1728
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001729static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1730 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1731 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1732 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1733 // to avoid a SIGBUS.
1734 switch (size) {
1735 case Primitive::kPrimByte:
1736 __ movsxb(out, Address(address, 0));
1737 break;
1738 case Primitive::kPrimShort:
1739 __ movsxw(out, Address(address, 0));
1740 break;
1741 case Primitive::kPrimInt:
1742 __ movl(out, Address(address, 0));
1743 break;
1744 case Primitive::kPrimLong:
1745 __ movq(out, Address(address, 0));
1746 break;
1747 default:
1748 LOG(FATAL) << "Type not recognized for peek: " << size;
1749 UNREACHABLE();
1750 }
1751}
1752
1753void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1754 CreateIntToIntLocations(arena_, invoke);
1755}
1756
1757void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1758 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1759}
1760
1761void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1762 CreateIntToIntLocations(arena_, invoke);
1763}
1764
1765void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1766 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1767}
1768
1769void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1770 CreateIntToIntLocations(arena_, invoke);
1771}
1772
1773void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1774 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1775}
1776
1777void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1778 CreateIntToIntLocations(arena_, invoke);
1779}
1780
1781void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1782 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1783}
1784
1785static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1786 LocationSummary* locations = new (arena) LocationSummary(invoke,
1787 LocationSummary::kNoCall,
1788 kIntrinsified);
1789 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001790 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001791}
1792
1793static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1794 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001795 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001796 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1797 // to avoid a SIGBUS.
1798 switch (size) {
1799 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001800 if (value.IsConstant()) {
1801 __ movb(Address(address, 0),
1802 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1803 } else {
1804 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1805 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001806 break;
1807 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001808 if (value.IsConstant()) {
1809 __ movw(Address(address, 0),
1810 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1811 } else {
1812 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1813 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001814 break;
1815 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001816 if (value.IsConstant()) {
1817 __ movl(Address(address, 0),
1818 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1819 } else {
1820 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1821 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001822 break;
1823 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001824 if (value.IsConstant()) {
1825 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1826 DCHECK(IsInt<32>(v));
1827 int32_t v_32 = v;
1828 __ movq(Address(address, 0), Immediate(v_32));
1829 } else {
1830 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1831 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001832 break;
1833 default:
1834 LOG(FATAL) << "Type not recognized for poke: " << size;
1835 UNREACHABLE();
1836 }
1837}
1838
1839void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1840 CreateIntIntToVoidLocations(arena_, invoke);
1841}
1842
1843void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1844 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1845}
1846
1847void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1848 CreateIntIntToVoidLocations(arena_, invoke);
1849}
1850
1851void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1852 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1853}
1854
1855void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1856 CreateIntIntToVoidLocations(arena_, invoke);
1857}
1858
1859void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1860 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1861}
1862
1863void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1864 CreateIntIntToVoidLocations(arena_, invoke);
1865}
1866
1867void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1868 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1869}
1870
1871void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1872 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1873 LocationSummary::kNoCall,
1874 kIntrinsified);
1875 locations->SetOut(Location::RequiresRegister());
1876}
1877
1878void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1879 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001880 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(),
1881 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001882}
1883
Roland Levillain0d5a2812015-11-13 10:07:31 +00001884static void GenUnsafeGet(HInvoke* invoke,
1885 Primitive::Type type,
1886 bool is_volatile ATTRIBUTE_UNUSED,
1887 CodeGeneratorX86_64* codegen) {
1888 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
1889 LocationSummary* locations = invoke->GetLocations();
1890 Location base_loc = locations->InAt(1);
1891 CpuRegister base = base_loc.AsRegister<CpuRegister>();
1892 Location offset_loc = locations->InAt(2);
1893 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
1894 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001895 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001896
Andreas Gampe878d58c2015-01-15 23:24:00 -08001897 switch (type) {
1898 case Primitive::kPrimInt:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001899 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001900 break;
1901
1902 case Primitive::kPrimNot: {
1903 if (kEmitCompilerReadBarrier) {
1904 if (kUseBakerReadBarrier) {
1905 Location temp = locations->GetTemp(0);
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001906 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1907 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1908 invoke, output_loc, base, src, temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001909 } else {
1910 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1911 codegen->GenerateReadBarrierSlow(
1912 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1913 }
1914 } else {
1915 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1916 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001917 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001918 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001919 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001920
1921 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001922 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08001923 break;
1924
1925 default:
1926 LOG(FATAL) << "Unsupported op size " << type;
1927 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001928 }
1929}
1930
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001931static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1932 HInvoke* invoke,
1933 Primitive::Type type) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001934 bool can_call = kEmitCompilerReadBarrier &&
1935 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1936 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001937 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001938 can_call ?
1939 LocationSummary::kCallOnSlowPath :
1940 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001941 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001942 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001943 locations->SetInAt(1, Location::RequiresRegister());
1944 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001945 locations->SetOut(Location::RequiresRegister(),
1946 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001947 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1948 // We need a temporary register for the read barrier marking slow
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001949 // path in InstructionCodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001950 locations->AddTemp(Location::RequiresRegister());
1951 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001952}
1953
1954void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001955 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001956}
1957void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001958 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001959}
1960void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001961 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001962}
1963void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001964 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001965}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001966void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001967 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001968}
1969void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001970 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001971}
1972
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001973
1974void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001975 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001976}
1977void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001978 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001979}
1980void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001981 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001982}
1983void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001984 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001985}
Andreas Gampe878d58c2015-01-15 23:24:00 -08001986void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001987 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001988}
1989void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001990 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001991}
1992
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001993
1994static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
1995 Primitive::Type type,
1996 HInvoke* invoke) {
1997 LocationSummary* locations = new (arena) LocationSummary(invoke,
1998 LocationSummary::kNoCall,
1999 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002000 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002001 locations->SetInAt(1, Location::RequiresRegister());
2002 locations->SetInAt(2, Location::RequiresRegister());
2003 locations->SetInAt(3, Location::RequiresRegister());
2004 if (type == Primitive::kPrimNot) {
2005 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002006 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002007 locations->AddTemp(Location::RequiresRegister());
2008 }
2009}
2010
2011void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2012 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2013}
2014void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2015 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2016}
2017void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2018 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2019}
2020void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2021 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2022}
2023void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2024 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2025}
2026void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2027 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2028}
2029void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2030 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2031}
2032void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2033 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2034}
2035void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2036 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2037}
2038
2039// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2040// memory model.
2041static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2042 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002043 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002044 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2045 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2046 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2047
2048 if (type == Primitive::kPrimLong) {
2049 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002050 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2051 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2052 __ movl(temp, value);
2053 __ PoisonHeapReference(temp);
2054 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002055 } else {
2056 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2057 }
2058
2059 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002060 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002061 }
2062
2063 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002064 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002065 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2066 locations->GetTemp(1).AsRegister<CpuRegister>(),
2067 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002068 value,
2069 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002070 }
2071}
2072
2073void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002074 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002075}
2076void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002077 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002078}
2079void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002080 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002081}
2082void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002083 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002084}
2085void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002086 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002087}
2088void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002089 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002090}
2091void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002092 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002093}
2094void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002095 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002096}
2097void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002098 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002099}
2100
Mark Mendell58d25fd2015-04-03 14:52:31 -04002101static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2102 HInvoke* invoke) {
2103 LocationSummary* locations = new (arena) LocationSummary(invoke,
2104 LocationSummary::kNoCall,
2105 kIntrinsified);
2106 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2107 locations->SetInAt(1, Location::RequiresRegister());
2108 locations->SetInAt(2, Location::RequiresRegister());
2109 // expected value must be in EAX/RAX.
2110 locations->SetInAt(3, Location::RegisterLocation(RAX));
2111 locations->SetInAt(4, Location::RequiresRegister());
2112
2113 locations->SetOut(Location::RequiresRegister());
2114 if (type == Primitive::kPrimNot) {
2115 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002116 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002117 locations->AddTemp(Location::RequiresRegister());
2118 }
2119}
2120
2121void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2122 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2123}
2124
2125void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2126 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2127}
2128
2129void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002130 // The UnsafeCASObject intrinsic is missing a read barrier, and
2131 // therefore sometimes does not work as expected (b/25883050).
2132 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01002133 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00002134 //
Roland Levillain3d312422016-06-23 13:53:42 +01002135 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
Roland Levillain391b8662015-12-18 11:43:38 +00002136 // this intrinsic.
2137 if (kEmitCompilerReadBarrier) {
2138 return;
2139 }
2140
Mark Mendell58d25fd2015-04-03 14:52:31 -04002141 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2142}
2143
2144static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002145 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002146 LocationSummary* locations = invoke->GetLocations();
2147
2148 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2149 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2150 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002151 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002152 DCHECK_EQ(expected.AsRegister(), RAX);
2153 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
2154 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2155
Roland Levillainb488b782015-10-22 11:38:49 +01002156 if (type == Primitive::kPrimNot) {
2157 // Mark card for object assuming new value is stored.
2158 bool value_can_be_null = true; // TODO: Worth finding out this information?
2159 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2160 locations->GetTemp(1).AsRegister<CpuRegister>(),
2161 base,
2162 value,
2163 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01002164
Roland Levillainb488b782015-10-22 11:38:49 +01002165 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2166 Register value_reg = value.AsRegister();
2167 if (kPoisonHeapReferences) {
2168 if (base_equals_value) {
2169 // If `base` and `value` are the same register location, move
2170 // `value_reg` to a temporary register. This way, poisoning
2171 // `value_reg` won't invalidate `base`.
2172 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
2173 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002174 }
Roland Levillainb488b782015-10-22 11:38:49 +01002175
2176 // Check that the register allocator did not assign the location
2177 // of `expected` (RAX) to `value` nor to `base`, so that heap
2178 // poisoning (when enabled) works as intended below.
2179 // - If `value` were equal to `expected`, both references would
2180 // be poisoned twice, meaning they would not be poisoned at
2181 // all, as heap poisoning uses address negation.
2182 // - If `base` were equal to `expected`, poisoning `expected`
2183 // would invalidate `base`.
2184 DCHECK_NE(value_reg, expected.AsRegister());
2185 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2186
2187 __ PoisonHeapReference(expected);
2188 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002189 }
2190
Roland Levillain391b8662015-12-18 11:43:38 +00002191 // TODO: Add a read barrier for the reference stored in the object
2192 // before attempting the CAS, similar to the one in the
2193 // art::Unsafe_compareAndSwapObject JNI implementation.
2194 //
2195 // Note that this code is not (yet) used when read barriers are
2196 // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject).
2197 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillainb488b782015-10-22 11:38:49 +01002198 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002199
Roland Levillain0d5a2812015-11-13 10:07:31 +00002200 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002201 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002202
Roland Levillainb488b782015-10-22 11:38:49 +01002203 // Convert ZF into the boolean result.
2204 __ setcc(kZero, out);
2205 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002206
Roland Levillain391b8662015-12-18 11:43:38 +00002207 // If heap poisoning is enabled, we need to unpoison the values
2208 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002209 if (kPoisonHeapReferences) {
2210 if (base_equals_value) {
2211 // `value_reg` has been moved to a temporary register, no need
2212 // to unpoison it.
2213 } else {
2214 // Ensure `value` is different from `out`, so that unpoisoning
2215 // the former does not invalidate the latter.
2216 DCHECK_NE(value_reg, out.AsRegister());
2217 __ UnpoisonHeapReference(CpuRegister(value_reg));
2218 }
2219 // Ensure `expected` is different from `out`, so that unpoisoning
2220 // the former does not invalidate the latter.
2221 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2222 __ UnpoisonHeapReference(expected);
2223 }
2224 } else {
2225 if (type == Primitive::kPrimInt) {
2226 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2227 } else if (type == Primitive::kPrimLong) {
2228 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2229 } else {
2230 LOG(FATAL) << "Unexpected CAS type " << type;
2231 }
2232
Roland Levillain0d5a2812015-11-13 10:07:31 +00002233 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002234 // scheduling barriers at this time.
2235
2236 // Convert ZF into the boolean result.
2237 __ setcc(kZero, out);
2238 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002239 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002240}
2241
2242void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2243 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2244}
2245
2246void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2247 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2248}
2249
2250void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002251 // The UnsafeCASObject intrinsic is missing a read barrier, and
2252 // therefore sometimes does not work as expected (b/25883050).
2253 // Turn it off temporarily as a quick fix, until the read barrier is
2254 // implemented (see TODO in GenCAS).
2255 //
2256 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
2257 // this intrinsic.
2258 DCHECK(!kEmitCompilerReadBarrier);
2259
Mark Mendell58d25fd2015-04-03 14:52:31 -04002260 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2261}
2262
2263void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2264 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2265 LocationSummary::kNoCall,
2266 kIntrinsified);
2267 locations->SetInAt(0, Location::RequiresRegister());
2268 locations->SetOut(Location::SameAsFirstInput());
2269 locations->AddTemp(Location::RequiresRegister());
2270}
2271
2272static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2273 X86_64Assembler* assembler) {
2274 Immediate imm_shift(shift);
2275 Immediate imm_mask(mask);
2276 __ movl(temp, reg);
2277 __ shrl(reg, imm_shift);
2278 __ andl(temp, imm_mask);
2279 __ andl(reg, imm_mask);
2280 __ shll(temp, imm_shift);
2281 __ orl(reg, temp);
2282}
2283
2284void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002285 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002286 LocationSummary* locations = invoke->GetLocations();
2287
2288 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2289 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2290
2291 /*
2292 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2293 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2294 * compared to generic luni implementation which has 5 rounds of swapping bits.
2295 * x = bswap x
2296 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2297 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2298 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2299 */
2300 __ bswapl(reg);
2301 SwapBits(reg, temp, 1, 0x55555555, assembler);
2302 SwapBits(reg, temp, 2, 0x33333333, assembler);
2303 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2304}
2305
2306void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2307 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2308 LocationSummary::kNoCall,
2309 kIntrinsified);
2310 locations->SetInAt(0, Location::RequiresRegister());
2311 locations->SetOut(Location::SameAsFirstInput());
2312 locations->AddTemp(Location::RequiresRegister());
2313 locations->AddTemp(Location::RequiresRegister());
2314}
2315
2316static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2317 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2318 Immediate imm_shift(shift);
2319 __ movq(temp_mask, Immediate(mask));
2320 __ movq(temp, reg);
2321 __ shrq(reg, imm_shift);
2322 __ andq(temp, temp_mask);
2323 __ andq(reg, temp_mask);
2324 __ shlq(temp, imm_shift);
2325 __ orq(reg, temp);
2326}
2327
2328void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002329 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002330 LocationSummary* locations = invoke->GetLocations();
2331
2332 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2333 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2334 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2335
2336 /*
2337 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2338 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2339 * compared to generic luni implementation which has 5 rounds of swapping bits.
2340 * x = bswap x
2341 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2342 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2343 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2344 */
2345 __ bswapq(reg);
2346 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2347 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2348 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2349}
2350
Aart Bik3f67e692016-01-15 14:35:12 -08002351static void CreateBitCountLocations(
2352 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2353 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2354 // Do nothing if there is no popcnt support. This results in generating
2355 // a call for the intrinsic rather than direct code.
2356 return;
2357 }
2358 LocationSummary* locations = new (arena) LocationSummary(invoke,
2359 LocationSummary::kNoCall,
2360 kIntrinsified);
2361 locations->SetInAt(0, Location::Any());
2362 locations->SetOut(Location::RequiresRegister());
2363}
2364
Aart Bikc5d47542016-01-27 17:00:35 -08002365static void GenBitCount(X86_64Assembler* assembler,
2366 CodeGeneratorX86_64* codegen,
2367 HInvoke* invoke,
2368 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002369 LocationSummary* locations = invoke->GetLocations();
2370 Location src = locations->InAt(0);
2371 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2372
2373 if (invoke->InputAt(0)->IsConstant()) {
2374 // Evaluate this at compile time.
2375 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002376 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002377 ? POPCOUNT(static_cast<uint64_t>(value))
2378 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002379 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002380 return;
2381 }
2382
2383 if (src.IsRegister()) {
2384 if (is_long) {
2385 __ popcntq(out, src.AsRegister<CpuRegister>());
2386 } else {
2387 __ popcntl(out, src.AsRegister<CpuRegister>());
2388 }
2389 } else if (is_long) {
2390 DCHECK(src.IsDoubleStackSlot());
2391 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2392 } else {
2393 DCHECK(src.IsStackSlot());
2394 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2395 }
2396}
2397
2398void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2399 CreateBitCountLocations(arena_, codegen_, invoke);
2400}
2401
2402void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002403 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002404}
2405
2406void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2407 CreateBitCountLocations(arena_, codegen_, invoke);
2408}
2409
2410void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002411 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2412}
2413
Aart Bikc5d47542016-01-27 17:00:35 -08002414static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2415 LocationSummary* locations = new (arena) LocationSummary(invoke,
2416 LocationSummary::kNoCall,
2417 kIntrinsified);
2418 locations->SetInAt(0, Location::Any());
2419 locations->SetOut(Location::RequiresRegister());
2420 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2421 : Location::RequiresRegister()); // any will do
2422}
2423
2424static void GenOneBit(X86_64Assembler* assembler,
2425 CodeGeneratorX86_64* codegen,
2426 HInvoke* invoke,
2427 bool is_high, bool is_long) {
2428 LocationSummary* locations = invoke->GetLocations();
2429 Location src = locations->InAt(0);
2430 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2431
2432 if (invoke->InputAt(0)->IsConstant()) {
2433 // Evaluate this at compile time.
2434 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2435 if (value == 0) {
2436 __ xorl(out, out); // Clears upper bits too.
2437 return;
2438 }
2439 // Nonzero value.
2440 if (is_high) {
2441 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2442 : 31 - CLZ(static_cast<uint32_t>(value));
2443 } else {
2444 value = is_long ? CTZ(static_cast<uint64_t>(value))
2445 : CTZ(static_cast<uint32_t>(value));
2446 }
2447 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002448 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002449 } else {
2450 codegen->Load32BitValue(out, 1 << value);
2451 }
2452 return;
2453 }
2454
2455 // Handle the non-constant cases.
2456 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2457 if (is_high) {
2458 // Use architectural support: basically 1 << bsr.
2459 if (src.IsRegister()) {
2460 if (is_long) {
2461 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2462 } else {
2463 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2464 }
2465 } else if (is_long) {
2466 DCHECK(src.IsDoubleStackSlot());
2467 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2468 } else {
2469 DCHECK(src.IsStackSlot());
2470 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2471 }
2472 // BSR sets ZF if the input was zero.
2473 NearLabel is_zero, done;
2474 __ j(kEqual, &is_zero);
2475 __ movl(out, Immediate(1)); // Clears upper bits too.
2476 if (is_long) {
2477 __ shlq(out, tmp);
2478 } else {
2479 __ shll(out, tmp);
2480 }
2481 __ jmp(&done);
2482 __ Bind(&is_zero);
2483 __ xorl(out, out); // Clears upper bits too.
2484 __ Bind(&done);
2485 } else {
2486 // Copy input into temporary.
2487 if (src.IsRegister()) {
2488 if (is_long) {
2489 __ movq(tmp, src.AsRegister<CpuRegister>());
2490 } else {
2491 __ movl(tmp, src.AsRegister<CpuRegister>());
2492 }
2493 } else if (is_long) {
2494 DCHECK(src.IsDoubleStackSlot());
2495 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2496 } else {
2497 DCHECK(src.IsStackSlot());
2498 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2499 }
2500 // Do the bit twiddling: basically tmp & -tmp;
2501 if (is_long) {
2502 __ movq(out, tmp);
2503 __ negq(tmp);
2504 __ andq(out, tmp);
2505 } else {
2506 __ movl(out, tmp);
2507 __ negl(tmp);
2508 __ andl(out, tmp);
2509 }
2510 }
2511}
2512
2513void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2514 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2515}
2516
2517void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2518 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2519}
2520
2521void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2522 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2523}
2524
2525void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2526 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2527}
2528
2529void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2530 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2531}
2532
2533void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2534 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2535}
2536
2537void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2538 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2539}
2540
2541void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2542 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002543}
2544
Mark Mendelld5897672015-08-12 21:16:41 -04002545static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2546 LocationSummary* locations = new (arena) LocationSummary(invoke,
2547 LocationSummary::kNoCall,
2548 kIntrinsified);
2549 locations->SetInAt(0, Location::Any());
2550 locations->SetOut(Location::RequiresRegister());
2551}
2552
Aart Bikc5d47542016-01-27 17:00:35 -08002553static void GenLeadingZeros(X86_64Assembler* assembler,
2554 CodeGeneratorX86_64* codegen,
2555 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002556 LocationSummary* locations = invoke->GetLocations();
2557 Location src = locations->InAt(0);
2558 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2559
2560 int zero_value_result = is_long ? 64 : 32;
2561 if (invoke->InputAt(0)->IsConstant()) {
2562 // Evaluate this at compile time.
2563 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2564 if (value == 0) {
2565 value = zero_value_result;
2566 } else {
2567 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2568 }
Aart Bikc5d47542016-01-27 17:00:35 -08002569 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002570 return;
2571 }
2572
2573 // Handle the non-constant cases.
2574 if (src.IsRegister()) {
2575 if (is_long) {
2576 __ bsrq(out, src.AsRegister<CpuRegister>());
2577 } else {
2578 __ bsrl(out, src.AsRegister<CpuRegister>());
2579 }
2580 } else if (is_long) {
2581 DCHECK(src.IsDoubleStackSlot());
2582 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2583 } else {
2584 DCHECK(src.IsStackSlot());
2585 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2586 }
2587
2588 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002589 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002590 __ j(kEqual, &is_zero);
2591
2592 // Correct the result from BSR to get the CLZ result.
2593 __ xorl(out, Immediate(zero_value_result - 1));
2594 __ jmp(&done);
2595
2596 // Fix the zero case with the expected result.
2597 __ Bind(&is_zero);
2598 __ movl(out, Immediate(zero_value_result));
2599
2600 __ Bind(&done);
2601}
2602
2603void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2604 CreateLeadingZeroLocations(arena_, invoke);
2605}
2606
2607void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002608 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002609}
2610
2611void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2612 CreateLeadingZeroLocations(arena_, invoke);
2613}
2614
2615void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002616 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002617}
2618
Mark Mendell2d554792015-09-15 21:45:18 -04002619static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2620 LocationSummary* locations = new (arena) LocationSummary(invoke,
2621 LocationSummary::kNoCall,
2622 kIntrinsified);
2623 locations->SetInAt(0, Location::Any());
2624 locations->SetOut(Location::RequiresRegister());
2625}
2626
Aart Bikc5d47542016-01-27 17:00:35 -08002627static void GenTrailingZeros(X86_64Assembler* assembler,
2628 CodeGeneratorX86_64* codegen,
2629 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002630 LocationSummary* locations = invoke->GetLocations();
2631 Location src = locations->InAt(0);
2632 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2633
2634 int zero_value_result = is_long ? 64 : 32;
2635 if (invoke->InputAt(0)->IsConstant()) {
2636 // Evaluate this at compile time.
2637 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2638 if (value == 0) {
2639 value = zero_value_result;
2640 } else {
2641 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2642 }
Aart Bikc5d47542016-01-27 17:00:35 -08002643 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002644 return;
2645 }
2646
2647 // Handle the non-constant cases.
2648 if (src.IsRegister()) {
2649 if (is_long) {
2650 __ bsfq(out, src.AsRegister<CpuRegister>());
2651 } else {
2652 __ bsfl(out, src.AsRegister<CpuRegister>());
2653 }
2654 } else if (is_long) {
2655 DCHECK(src.IsDoubleStackSlot());
2656 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2657 } else {
2658 DCHECK(src.IsStackSlot());
2659 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2660 }
2661
2662 // BSF sets ZF if the input was zero, and the output is undefined.
2663 NearLabel done;
2664 __ j(kNotEqual, &done);
2665
2666 // Fix the zero case with the expected result.
2667 __ movl(out, Immediate(zero_value_result));
2668
2669 __ Bind(&done);
2670}
2671
2672void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2673 CreateTrailingZeroLocations(arena_, invoke);
2674}
2675
2676void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002677 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002678}
2679
2680void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2681 CreateTrailingZeroLocations(arena_, invoke);
2682}
2683
2684void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002685 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2686}
2687
Serguei Katkov288c7a82016-05-16 11:53:15 +06002688void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2689 if (kEmitCompilerReadBarrier) {
2690 // Do not intrinsify this call with the read barrier configuration.
2691 return;
2692 }
2693 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2694 LocationSummary::kCallOnSlowPath,
2695 kIntrinsified);
2696 locations->SetInAt(0, Location::RequiresRegister());
2697 locations->SetOut(Location::SameAsFirstInput());
2698 locations->AddTemp(Location::RequiresRegister());
2699}
2700
2701void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2702 DCHECK(!kEmitCompilerReadBarrier);
2703 LocationSummary* locations = invoke->GetLocations();
2704 X86_64Assembler* assembler = GetAssembler();
2705
2706 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2707 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2708
2709 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2710 codegen_->AddSlowPath(slow_path);
2711
2712 // Load ArtMethod first.
2713 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2714 DCHECK(invoke_direct != nullptr);
2715 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2716 invoke_direct, locations->GetTemp(0));
2717 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2718 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2719
2720 // Now get declaring class.
2721 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2722
2723 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2724 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2725 DCHECK_NE(slow_path_flag_offset, 0u);
2726 DCHECK_NE(disable_flag_offset, 0u);
2727 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2728
2729 // Check static flags preventing us for using intrinsic.
2730 if (slow_path_flag_offset == disable_flag_offset + 1) {
2731 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2732 __ j(kNotEqual, slow_path->GetEntryLabel());
2733 } else {
2734 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2735 __ j(kNotEqual, slow_path->GetEntryLabel());
2736 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2737 __ j(kNotEqual, slow_path->GetEntryLabel());
2738 }
2739
2740 // Fast path.
2741 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2742 codegen_->MaybeRecordImplicitNullCheck(invoke);
2743 __ MaybeUnpoisonHeapReference(out);
2744 __ Bind(slow_path->GetExitLabel());
2745}
2746
Aart Bik2f9fcc92016-03-01 15:16:54 -08002747UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2748UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002749
Aart Bik0e54c012016-03-04 12:08:31 -08002750// 1.8.
2751UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2752UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2753UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2754UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2755UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002756
Aart Bik2f9fcc92016-03-01 15:16:54 -08002757UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002758
2759#undef __
2760
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002761} // namespace x86_64
2762} // namespace art