blob: 57e4bddb8f7b27cc571c6fd7c9fb7bacf9d65595 [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
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800881void IntrinsicLocationsBuilderX86_64::VisitStringCharAt(HInvoke* invoke) {
882 // The inputs plus one temp.
883 LocationSummary* locations = new (arena_) LocationSummary(invoke,
884 LocationSummary::kCallOnSlowPath,
885 kIntrinsified);
886 locations->SetInAt(0, Location::RequiresRegister());
887 locations->SetInAt(1, Location::RequiresRegister());
888 locations->SetOut(Location::SameAsFirstInput());
889 locations->AddTemp(Location::RequiresRegister());
890}
891
892void IntrinsicCodeGeneratorX86_64::VisitStringCharAt(HInvoke* invoke) {
893 LocationSummary* locations = invoke->GetLocations();
894
Mark Mendell6bc53a92015-07-01 14:26:52 -0400895 // Location of reference to data array.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800896 const int32_t value_offset = mirror::String::ValueOffset().Int32Value();
Mark Mendell6bc53a92015-07-01 14:26:52 -0400897 // Location of count.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800898 const int32_t count_offset = mirror::String::CountOffset().Int32Value();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800899
900 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
901 CpuRegister idx = locations->InAt(1).AsRegister<CpuRegister>();
902 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800903
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800904 // TODO: Maybe we can support range check elimination. Overall, though, I think it's not worth
905 // the cost.
906 // TODO: For simplicity, the index parameter is requested in a register, so different from Quick
907 // we will not optimize the code for constants (which would save a register).
908
Andreas Gampe85b62f22015-09-09 13:15:38 -0700909 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800910 codegen_->AddSlowPath(slow_path);
911
912 X86_64Assembler* assembler = GetAssembler();
913
914 __ cmpl(idx, Address(obj, count_offset));
Andreas Gampe878d58c2015-01-15 23:24:00 -0800915 codegen_->MaybeRecordImplicitNullCheck(invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800916 __ j(kAboveEqual, slow_path->GetEntryLabel());
917
Jeff Hao848f70a2014-01-15 13:49:50 -0800918 // out = out[2*idx].
919 __ movzxw(out, Address(out, idx, ScaleFactor::TIMES_2, value_offset));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800920
921 __ Bind(slow_path->GetExitLabel());
922}
923
Mark Mendell6bc53a92015-07-01 14:26:52 -0400924void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
925 // Check to see if we have known failures that will cause us to have to bail out
926 // to the runtime, and just generate the runtime call directly.
927 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
928 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
929
930 // The positions must be non-negative.
931 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
932 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
933 // We will have to fail anyways.
934 return;
935 }
936
937 // The length must be > 0.
938 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
939 if (length != nullptr) {
940 int32_t len = length->GetValue();
941 if (len < 0) {
942 // Just call as normal.
943 return;
944 }
945 }
946
947 LocationSummary* locations = new (arena_) LocationSummary(invoke,
948 LocationSummary::kCallOnSlowPath,
949 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100950 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400951 locations->SetInAt(0, Location::RequiresRegister());
952 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
953 locations->SetInAt(2, Location::RequiresRegister());
954 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
955 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
956
957 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
958 locations->AddTemp(Location::RegisterLocation(RSI));
959 locations->AddTemp(Location::RegisterLocation(RDI));
960 locations->AddTemp(Location::RegisterLocation(RCX));
961}
962
963static void CheckPosition(X86_64Assembler* assembler,
964 Location pos,
965 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100966 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700967 SlowPathCode* slow_path,
Mark Mendell6bc53a92015-07-01 14:26:52 -0400968 CpuRegister input_len,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100969 CpuRegister temp,
970 bool length_is_input_length = false) {
971 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400972 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
973
974 if (pos.IsConstant()) {
975 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
976 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100977 if (!length_is_input_length) {
978 // Check that length(input) >= length.
979 if (length.IsConstant()) {
980 __ cmpl(Address(input, length_offset),
981 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
982 } else {
983 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
984 }
985 __ j(kLess, slow_path->GetEntryLabel());
986 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400987 } else {
988 // Check that length(input) >= pos.
989 __ movl(input_len, Address(input, length_offset));
990 __ cmpl(input_len, Immediate(pos_const));
991 __ j(kLess, slow_path->GetEntryLabel());
992
993 // Check that (length(input) - pos) >= length.
994 __ leal(temp, Address(input_len, -pos_const));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100995 if (length.IsConstant()) {
996 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
997 } else {
998 __ cmpl(temp, length.AsRegister<CpuRegister>());
999 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001000 __ j(kLess, slow_path->GetEntryLabel());
1001 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001002 } else if (length_is_input_length) {
1003 // The only way the copy can succeed is if pos is zero.
1004 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1005 __ testl(pos_reg, pos_reg);
1006 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001007 } else {
1008 // Check that pos >= 0.
1009 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1010 __ testl(pos_reg, pos_reg);
1011 __ j(kLess, slow_path->GetEntryLabel());
1012
1013 // Check that pos <= length(input).
1014 __ cmpl(Address(input, length_offset), pos_reg);
1015 __ j(kLess, slow_path->GetEntryLabel());
1016
1017 // Check that (length(input) - pos) >= length.
1018 __ movl(temp, Address(input, length_offset));
1019 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001020 if (length.IsConstant()) {
1021 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1022 } else {
1023 __ cmpl(temp, length.AsRegister<CpuRegister>());
1024 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001025 __ j(kLess, slow_path->GetEntryLabel());
1026 }
1027}
1028
1029void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1030 X86_64Assembler* assembler = GetAssembler();
1031 LocationSummary* locations = invoke->GetLocations();
1032
1033 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001034 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001035 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001036 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001037 Location length = locations->InAt(4);
1038
1039 // Temporaries that we need for MOVSW.
1040 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1041 DCHECK_EQ(src_base.AsRegister(), RSI);
1042 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1043 DCHECK_EQ(dest_base.AsRegister(), RDI);
1044 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1045 DCHECK_EQ(count.AsRegister(), RCX);
1046
Andreas Gampe85b62f22015-09-09 13:15:38 -07001047 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001048 codegen_->AddSlowPath(slow_path);
1049
1050 // Bail out if the source and destination are the same.
1051 __ cmpl(src, dest);
1052 __ j(kEqual, slow_path->GetEntryLabel());
1053
1054 // Bail out if the source is null.
1055 __ testl(src, src);
1056 __ j(kEqual, slow_path->GetEntryLabel());
1057
1058 // Bail out if the destination is null.
1059 __ testl(dest, dest);
1060 __ j(kEqual, slow_path->GetEntryLabel());
1061
1062 // If the length is negative, bail out.
1063 // We have already checked in the LocationsBuilder for the constant case.
1064 if (!length.IsConstant()) {
1065 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1066 __ j(kLess, slow_path->GetEntryLabel());
1067 }
1068
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001069 // Validity checks: source.
1070 CheckPosition(assembler, src_pos, src, length, slow_path, src_base, dest_base);
1071
1072 // Validity checks: dest.
1073 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base, dest_base);
1074
Mark Mendell6bc53a92015-07-01 14:26:52 -04001075 // We need the count in RCX.
1076 if (length.IsConstant()) {
1077 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1078 } else {
1079 __ movl(count, length.AsRegister<CpuRegister>());
1080 }
1081
Mark Mendell6bc53a92015-07-01 14:26:52 -04001082 // Okay, everything checks out. Finally time to do the copy.
1083 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1084 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1085 DCHECK_EQ(char_size, 2u);
1086
1087 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1088
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001089 if (src_pos.IsConstant()) {
1090 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1091 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001092 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001093 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001094 ScaleFactor::TIMES_2, data_offset));
1095 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001096 if (dest_pos.IsConstant()) {
1097 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1098 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001099 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001100 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001101 ScaleFactor::TIMES_2, data_offset));
1102 }
1103
1104 // Do the move.
1105 __ rep_movsw();
1106
1107 __ Bind(slow_path->GetExitLabel());
1108}
1109
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001110
1111void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01001112 // TODO(rpl): Implement read barriers in the SystemArrayCopy
1113 // intrinsic and re-enable it (b/29516905).
1114 if (kEmitCompilerReadBarrier) {
1115 return;
1116 }
1117
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001118 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001119}
1120
1121void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01001122 // TODO(rpl): Implement read barriers in the SystemArrayCopy
1123 // intrinsic and re-enable it (b/29516905).
1124 DCHECK(!kEmitCompilerReadBarrier);
1125
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001126 X86_64Assembler* assembler = GetAssembler();
1127 LocationSummary* locations = invoke->GetLocations();
1128
1129 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1130 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1131 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1132 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
1133
1134 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1135 Location src_pos = locations->InAt(1);
1136 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1137 Location dest_pos = locations->InAt(3);
1138 Location length = locations->InAt(4);
1139 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
1140 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
1141 CpuRegister temp3 = locations->GetTemp(2).AsRegister<CpuRegister>();
1142
1143 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1144 codegen_->AddSlowPath(slow_path);
1145
Roland Levillainebea3d22016-04-12 15:42:57 +01001146 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001147 SystemArrayCopyOptimizations optimizations(invoke);
1148
Roland Levillainebea3d22016-04-12 15:42:57 +01001149 if (!optimizations.GetDestinationIsSource() &&
1150 (!src_pos.IsConstant() || !dest_pos.IsConstant())) {
1151 __ cmpl(src, dest);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001152 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001153 // If source and destination are the same, we go to slow path if we need to do
1154 // forward copying.
1155 if (src_pos.IsConstant()) {
1156 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1157 if (dest_pos.IsConstant()) {
1158 // Checked when building locations.
1159 DCHECK(!optimizations.GetDestinationIsSource()
1160 || (src_pos_constant >= dest_pos.GetConstant()->AsIntConstant()->GetValue()));
1161 } else {
1162 if (!optimizations.GetDestinationIsSource()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001163 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001164 }
1165 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
1166 __ j(kGreater, slow_path->GetEntryLabel());
1167 }
1168 } else {
1169 if (!optimizations.GetDestinationIsSource()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001170 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001171 }
1172 if (dest_pos.IsConstant()) {
1173 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1174 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
1175 __ j(kLess, slow_path->GetEntryLabel());
1176 } else {
1177 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
1178 __ j(kLess, slow_path->GetEntryLabel());
1179 }
1180 }
1181
Roland Levillainebea3d22016-04-12 15:42:57 +01001182 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001183
1184 if (!optimizations.GetSourceIsNotNull()) {
1185 // Bail out if the source is null.
1186 __ testl(src, src);
1187 __ j(kEqual, slow_path->GetEntryLabel());
1188 }
1189
1190 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1191 // Bail out if the destination is null.
1192 __ testl(dest, dest);
1193 __ j(kEqual, slow_path->GetEntryLabel());
1194 }
1195
1196 // If the length is negative, bail out.
1197 // We have already checked in the LocationsBuilder for the constant case.
1198 if (!length.IsConstant() &&
1199 !optimizations.GetCountIsSourceLength() &&
1200 !optimizations.GetCountIsDestinationLength()) {
1201 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1202 __ j(kLess, slow_path->GetEntryLabel());
1203 }
1204
1205 // Validity checks: source.
1206 CheckPosition(assembler,
1207 src_pos,
1208 src,
1209 length,
1210 slow_path,
1211 temp1,
1212 temp2,
1213 optimizations.GetCountIsSourceLength());
1214
1215 // Validity checks: dest.
1216 CheckPosition(assembler,
1217 dest_pos,
1218 dest,
1219 length,
1220 slow_path,
1221 temp1,
1222 temp2,
1223 optimizations.GetCountIsDestinationLength());
1224
1225 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1226 // Check whether all elements of the source array are assignable to the component
1227 // type of the destination array. We do two checks: the classes are the same,
1228 // or the destination is Object[]. If none of these checks succeed, we go to the
1229 // slow path.
1230 __ movl(temp1, Address(dest, class_offset));
1231 __ movl(temp2, Address(src, class_offset));
1232 bool did_unpoison = false;
1233 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1234 !optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillainebea3d22016-04-12 15:42:57 +01001235 // One or two of the references need to be unpoisoned. Unpoison them
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001236 // both to make the identity check valid.
1237 __ MaybeUnpoisonHeapReference(temp1);
1238 __ MaybeUnpoisonHeapReference(temp2);
1239 did_unpoison = true;
1240 }
1241
1242 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1243 // Bail out if the destination is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001244 // /* HeapReference<Class> */ TMP = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001245 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1246 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1247 __ j(kEqual, slow_path->GetEntryLabel());
1248 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1249 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1250 __ j(kNotEqual, slow_path->GetEntryLabel());
1251 }
1252
1253 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1254 // Bail out if the source is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001255 // /* HeapReference<Class> */ TMP = temp2->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001256 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1257 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1258 __ j(kEqual, slow_path->GetEntryLabel());
1259 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1260 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1261 __ j(kNotEqual, slow_path->GetEntryLabel());
1262 }
1263
1264 __ cmpl(temp1, temp2);
1265
1266 if (optimizations.GetDestinationIsTypedObjectArray()) {
1267 NearLabel do_copy;
1268 __ j(kEqual, &do_copy);
1269 if (!did_unpoison) {
1270 __ MaybeUnpoisonHeapReference(temp1);
1271 }
Roland Levillainebea3d22016-04-12 15:42:57 +01001272 // /* HeapReference<Class> */ temp1 = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001273 __ movl(temp1, Address(temp1, component_offset));
1274 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillainebea3d22016-04-12 15:42:57 +01001275 // /* HeapReference<Class> */ temp1 = temp1->super_class_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001276 __ movl(temp1, Address(temp1, super_offset));
1277 // No need to unpoison the result, we're comparing against null.
1278 __ testl(temp1, temp1);
1279 __ j(kNotEqual, slow_path->GetEntryLabel());
1280 __ Bind(&do_copy);
1281 } else {
1282 __ j(kNotEqual, slow_path->GetEntryLabel());
1283 }
1284 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1285 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1286 // Bail out if the source is not a non primitive array.
Roland Levillainebea3d22016-04-12 15:42:57 +01001287 // /* HeapReference<Class> */ temp1 = src->klass_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001288 __ movl(temp1, Address(src, class_offset));
1289 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillainebea3d22016-04-12 15:42:57 +01001290 // /* HeapReference<Class> */ TMP = temp1->component_type_
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001291 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1292 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1293 __ j(kEqual, slow_path->GetEntryLabel());
1294 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1295 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
1296 __ j(kNotEqual, slow_path->GetEntryLabel());
1297 }
1298
1299 // Compute base source address, base destination address, and end source address.
1300
1301 uint32_t element_size = sizeof(int32_t);
1302 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1303 if (src_pos.IsConstant()) {
1304 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1305 __ leal(temp1, Address(src, element_size * constant + offset));
1306 } else {
1307 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1308 }
1309
1310 if (dest_pos.IsConstant()) {
1311 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1312 __ leal(temp2, Address(dest, element_size * constant + offset));
1313 } else {
1314 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1315 }
1316
1317 if (length.IsConstant()) {
1318 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1319 __ leal(temp3, Address(temp1, element_size * constant));
1320 } else {
1321 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1322 }
1323
1324 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1325 // poison/unpoison, nor do any read barrier as the next uses of the destination
1326 // array will do it.
1327 NearLabel loop, done;
1328 __ cmpl(temp1, temp3);
1329 __ j(kEqual, &done);
1330 __ Bind(&loop);
1331 __ movl(CpuRegister(TMP), Address(temp1, 0));
1332 __ movl(Address(temp2, 0), CpuRegister(TMP));
1333 __ addl(temp1, Immediate(element_size));
1334 __ addl(temp2, Immediate(element_size));
1335 __ cmpl(temp1, temp3);
1336 __ j(kNotEqual, &loop);
1337 __ Bind(&done);
1338
1339 // We only need one card marking on the destination array.
1340 codegen_->MarkGCCard(temp1,
1341 temp2,
1342 dest,
1343 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001344 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001345
1346 __ Bind(slow_path->GetExitLabel());
1347}
1348
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001349void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1350 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1351 LocationSummary::kCall,
1352 kIntrinsified);
1353 InvokeRuntimeCallingConvention calling_convention;
1354 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1355 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1356 locations->SetOut(Location::RegisterLocation(RAX));
1357}
1358
1359void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1360 X86_64Assembler* assembler = GetAssembler();
1361 LocationSummary* locations = invoke->GetLocations();
1362
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001363 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001364 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001365
1366 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1367 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001368 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001369 codegen_->AddSlowPath(slow_path);
1370 __ j(kEqual, slow_path->GetEntryLabel());
1371
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001372 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pStringCompareTo),
1373 /* no_rip */ true));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001374 __ Bind(slow_path->GetExitLabel());
1375}
1376
Agi Csakif8cfb202015-08-13 17:54:54 -07001377void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1378 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1379 LocationSummary::kNoCall,
1380 kIntrinsified);
1381 locations->SetInAt(0, Location::RequiresRegister());
1382 locations->SetInAt(1, Location::RequiresRegister());
1383
1384 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1385 locations->AddTemp(Location::RegisterLocation(RCX));
1386 locations->AddTemp(Location::RegisterLocation(RDI));
1387
1388 // Set output, RSI needed for repe_cmpsq instruction anyways.
1389 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1390}
1391
1392void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1393 X86_64Assembler* assembler = GetAssembler();
1394 LocationSummary* locations = invoke->GetLocations();
1395
1396 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1397 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1398 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1399 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1400 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1401
Mark Mendell0c9497d2015-08-21 09:30:05 -04001402 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001403
1404 // Get offsets of count, value, and class fields within a string object.
1405 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1406 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1407 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1408
1409 // Note that the null check must have been done earlier.
1410 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1411
Vladimir Marko53b52002016-05-24 19:30:45 +01001412 StringEqualsOptimizations optimizations(invoke);
1413 if (!optimizations.GetArgumentNotNull()) {
1414 // Check if input is null, return false if it is.
1415 __ testl(arg, arg);
1416 __ j(kEqual, &return_false);
1417 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001418
Vladimir Marko53b52002016-05-24 19:30:45 +01001419 if (!optimizations.GetArgumentIsString()) {
1420 // Instanceof check for the argument by comparing class fields.
1421 // All string objects must have the same type since String cannot be subclassed.
1422 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1423 // If the argument is a string object, its class field must be equal to receiver's class field.
1424 __ movl(rcx, Address(str, class_offset));
1425 __ cmpl(rcx, Address(arg, class_offset));
1426 __ j(kNotEqual, &return_false);
1427 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001428
1429 // Reference equality check, return true if same reference.
1430 __ cmpl(str, arg);
1431 __ j(kEqual, &return_true);
1432
1433 // Load length of receiver string.
1434 __ movl(rcx, Address(str, count_offset));
1435 // Check if lengths are equal, return false if they're not.
1436 __ cmpl(rcx, Address(arg, count_offset));
1437 __ j(kNotEqual, &return_false);
1438 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001439 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001440
1441 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1442 __ leal(rsi, Address(str, value_offset));
1443 __ leal(rdi, Address(arg, value_offset));
1444
1445 // Divide string length by 4 and adjust for lengths not divisible by 4.
1446 __ addl(rcx, Immediate(3));
1447 __ shrl(rcx, Immediate(2));
1448
1449 // Assertions that must hold in order to compare strings 4 characters at a time.
1450 DCHECK_ALIGNED(value_offset, 8);
1451 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1452
1453 // Loop to compare strings four characters at a time starting at the beginning of the string.
1454 __ repe_cmpsq();
1455 // If strings are not equal, zero flag will be cleared.
1456 __ j(kNotEqual, &return_false);
1457
1458 // Return true and exit the function.
1459 // If loop does not result in returning false, we return true.
1460 __ Bind(&return_true);
1461 __ movl(rsi, Immediate(1));
1462 __ jmp(&end);
1463
1464 // Return false and exit the function.
1465 __ Bind(&return_false);
1466 __ xorl(rsi, rsi);
1467 __ Bind(&end);
1468}
1469
Andreas Gampe21030dd2015-05-07 14:46:15 -07001470static void CreateStringIndexOfLocations(HInvoke* invoke,
1471 ArenaAllocator* allocator,
1472 bool start_at_zero) {
1473 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1474 LocationSummary::kCallOnSlowPath,
1475 kIntrinsified);
1476 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1477 locations->SetInAt(0, Location::RegisterLocation(RDI));
1478 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1479 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1480 // of the instruction explicitly.
1481 // Note: This works as we don't clobber RAX anywhere.
1482 locations->SetInAt(1, Location::RegisterLocation(RAX));
1483 if (!start_at_zero) {
1484 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1485 }
1486 // As we clobber RDI during execution anyways, also use it as the output.
1487 locations->SetOut(Location::SameAsFirstInput());
1488
1489 // repne scasw uses RCX as the counter.
1490 locations->AddTemp(Location::RegisterLocation(RCX));
1491 // Need another temporary to be able to compute the result.
1492 locations->AddTemp(Location::RequiresRegister());
1493}
1494
1495static void GenerateStringIndexOf(HInvoke* invoke,
1496 X86_64Assembler* assembler,
1497 CodeGeneratorX86_64* codegen,
1498 ArenaAllocator* allocator,
1499 bool start_at_zero) {
1500 LocationSummary* locations = invoke->GetLocations();
1501
1502 // Note that the null check must have been done earlier.
1503 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1504
1505 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1506 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1507 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1508 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1509 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1510
1511 // Check our assumptions for registers.
1512 DCHECK_EQ(string_obj.AsRegister(), RDI);
1513 DCHECK_EQ(search_value.AsRegister(), RAX);
1514 DCHECK_EQ(counter.AsRegister(), RCX);
1515 DCHECK_EQ(out.AsRegister(), RDI);
1516
1517 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001518 // 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 -07001519 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001520 HInstruction* code_point = invoke->InputAt(1);
1521 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001522 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001523 std::numeric_limits<uint16_t>::max()) {
1524 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1525 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1526 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1527 codegen->AddSlowPath(slow_path);
1528 __ jmp(slow_path->GetEntryLabel());
1529 __ Bind(slow_path->GetExitLabel());
1530 return;
1531 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001532 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001533 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1534 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1535 codegen->AddSlowPath(slow_path);
1536 __ j(kAbove, slow_path->GetEntryLabel());
1537 }
1538
1539 // From here down, we know that we are looking for a char that fits in 16 bits.
1540 // Location of reference to data array within the String object.
1541 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1542 // Location of count within the String object.
1543 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1544
1545 // Load string length, i.e., the count field of the string.
1546 __ movl(string_length, Address(string_obj, count_offset));
1547
1548 // Do a length check.
1549 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001550 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001551 __ testl(string_length, string_length);
1552 __ j(kEqual, &not_found_label);
1553
1554 if (start_at_zero) {
1555 // Number of chars to scan is the same as the string length.
1556 __ movl(counter, string_length);
1557
1558 // Move to the start of the string.
1559 __ addq(string_obj, Immediate(value_offset));
1560 } else {
1561 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1562
1563 // Do a start_index check.
1564 __ cmpl(start_index, string_length);
1565 __ j(kGreaterEqual, &not_found_label);
1566
1567 // Ensure we have a start index >= 0;
1568 __ xorl(counter, counter);
1569 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001570 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001571
1572 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1573 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1574
1575 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1576 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1577 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1578 }
1579
1580 // Everything is set up for repne scasw:
1581 // * Comparison address in RDI.
1582 // * Counter in ECX.
1583 __ repne_scasw();
1584
1585 // Did we find a match?
1586 __ j(kNotEqual, &not_found_label);
1587
1588 // Yes, we matched. Compute the index of the result.
1589 __ subl(string_length, counter);
1590 __ leal(out, Address(string_length, -1));
1591
Mark Mendell0c9497d2015-08-21 09:30:05 -04001592 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001593 __ jmp(&done);
1594
1595 // Failed to match; return -1.
1596 __ Bind(&not_found_label);
1597 __ movl(out, Immediate(-1));
1598
1599 // And join up at the end.
1600 __ Bind(&done);
1601 if (slow_path != nullptr) {
1602 __ Bind(slow_path->GetExitLabel());
1603 }
1604}
1605
1606void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001607 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001608}
1609
1610void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001611 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001612}
1613
1614void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001615 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001616}
1617
1618void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001619 GenerateStringIndexOf(
1620 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001621}
1622
Jeff Hao848f70a2014-01-15 13:49:50 -08001623void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1624 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1625 LocationSummary::kCall,
1626 kIntrinsified);
1627 InvokeRuntimeCallingConvention calling_convention;
1628 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1629 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1630 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1631 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1632 locations->SetOut(Location::RegisterLocation(RAX));
1633}
1634
1635void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1636 X86_64Assembler* assembler = GetAssembler();
1637 LocationSummary* locations = invoke->GetLocations();
1638
1639 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1640 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001641 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001642 codegen_->AddSlowPath(slow_path);
1643 __ j(kEqual, slow_path->GetEntryLabel());
1644
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001645 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromBytes),
1646 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001647 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001648 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1649 __ Bind(slow_path->GetExitLabel());
1650}
1651
1652void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1653 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1654 LocationSummary::kCall,
1655 kIntrinsified);
1656 InvokeRuntimeCallingConvention calling_convention;
1657 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1658 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1659 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1660 locations->SetOut(Location::RegisterLocation(RAX));
1661}
1662
1663void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1664 X86_64Assembler* assembler = GetAssembler();
1665
Roland Levillaincc3839c2016-02-29 16:23:48 +00001666 // No need to emit code checking whether `locations->InAt(2)` is a null
1667 // pointer, as callers of the native method
1668 //
1669 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1670 //
1671 // all include a null check on `data` before calling that method.
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001672 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromChars),
1673 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001674 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001675 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1676}
1677
1678void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1679 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1680 LocationSummary::kCall,
1681 kIntrinsified);
1682 InvokeRuntimeCallingConvention calling_convention;
1683 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1684 locations->SetOut(Location::RegisterLocation(RAX));
1685}
1686
1687void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1688 X86_64Assembler* assembler = GetAssembler();
1689 LocationSummary* locations = invoke->GetLocations();
1690
1691 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1692 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001693 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001694 codegen_->AddSlowPath(slow_path);
1695 __ j(kEqual, slow_path->GetEntryLabel());
1696
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001697 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, pAllocStringFromString),
1698 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001699 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001700 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1701 __ Bind(slow_path->GetExitLabel());
1702}
1703
Mark Mendell8f8926a2015-08-17 11:39:06 -04001704void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1705 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1706 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1707 LocationSummary::kNoCall,
1708 kIntrinsified);
1709 locations->SetInAt(0, Location::RequiresRegister());
1710 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1711 locations->SetInAt(2, Location::RequiresRegister());
1712 locations->SetInAt(3, Location::RequiresRegister());
1713 locations->SetInAt(4, Location::RequiresRegister());
1714
1715 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1716 locations->AddTemp(Location::RegisterLocation(RSI));
1717 locations->AddTemp(Location::RegisterLocation(RDI));
1718 locations->AddTemp(Location::RegisterLocation(RCX));
1719}
1720
1721void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1722 X86_64Assembler* assembler = GetAssembler();
1723 LocationSummary* locations = invoke->GetLocations();
1724
1725 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1726 // Location of data in char array buffer.
1727 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1728 // Location of char array data in string.
1729 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1730
1731 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1732 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1733 Location srcBegin = locations->InAt(1);
1734 int srcBegin_value =
1735 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1736 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1737 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1738 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1739
1740 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1741 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1742 DCHECK_EQ(char_size, 2u);
1743
1744 // Compute the address of the destination buffer.
1745 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1746
1747 // Compute the address of the source string.
1748 if (srcBegin.IsConstant()) {
1749 // Compute the address of the source string by adding the number of chars from
1750 // the source beginning to the value offset of a string.
1751 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1752 } else {
1753 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1754 ScaleFactor::TIMES_2, value_offset));
1755 }
1756
1757 // Compute the number of chars (words) to move.
1758 __ movl(CpuRegister(RCX), srcEnd);
1759 if (srcBegin.IsConstant()) {
1760 if (srcBegin_value != 0) {
1761 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1762 }
1763 } else {
1764 DCHECK(srcBegin.IsRegister());
1765 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1766 }
1767
1768 // Do the move.
1769 __ rep_movsw();
1770}
1771
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001772static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1773 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1774 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1775 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1776 // to avoid a SIGBUS.
1777 switch (size) {
1778 case Primitive::kPrimByte:
1779 __ movsxb(out, Address(address, 0));
1780 break;
1781 case Primitive::kPrimShort:
1782 __ movsxw(out, Address(address, 0));
1783 break;
1784 case Primitive::kPrimInt:
1785 __ movl(out, Address(address, 0));
1786 break;
1787 case Primitive::kPrimLong:
1788 __ movq(out, Address(address, 0));
1789 break;
1790 default:
1791 LOG(FATAL) << "Type not recognized for peek: " << size;
1792 UNREACHABLE();
1793 }
1794}
1795
1796void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1797 CreateIntToIntLocations(arena_, invoke);
1798}
1799
1800void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1801 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1802}
1803
1804void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1805 CreateIntToIntLocations(arena_, invoke);
1806}
1807
1808void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1809 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1810}
1811
1812void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1813 CreateIntToIntLocations(arena_, invoke);
1814}
1815
1816void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1817 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1818}
1819
1820void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1821 CreateIntToIntLocations(arena_, invoke);
1822}
1823
1824void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1825 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1826}
1827
1828static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1829 LocationSummary* locations = new (arena) LocationSummary(invoke,
1830 LocationSummary::kNoCall,
1831 kIntrinsified);
1832 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001833 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001834}
1835
1836static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1837 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001838 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001839 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1840 // to avoid a SIGBUS.
1841 switch (size) {
1842 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001843 if (value.IsConstant()) {
1844 __ movb(Address(address, 0),
1845 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1846 } else {
1847 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1848 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001849 break;
1850 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001851 if (value.IsConstant()) {
1852 __ movw(Address(address, 0),
1853 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1854 } else {
1855 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1856 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001857 break;
1858 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04001859 if (value.IsConstant()) {
1860 __ movl(Address(address, 0),
1861 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1862 } else {
1863 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
1864 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001865 break;
1866 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04001867 if (value.IsConstant()) {
1868 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
1869 DCHECK(IsInt<32>(v));
1870 int32_t v_32 = v;
1871 __ movq(Address(address, 0), Immediate(v_32));
1872 } else {
1873 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
1874 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001875 break;
1876 default:
1877 LOG(FATAL) << "Type not recognized for poke: " << size;
1878 UNREACHABLE();
1879 }
1880}
1881
1882void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1883 CreateIntIntToVoidLocations(arena_, invoke);
1884}
1885
1886void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
1887 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1888}
1889
1890void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1891 CreateIntIntToVoidLocations(arena_, invoke);
1892}
1893
1894void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
1895 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1896}
1897
1898void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1899 CreateIntIntToVoidLocations(arena_, invoke);
1900}
1901
1902void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
1903 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1904}
1905
1906void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1907 CreateIntIntToVoidLocations(arena_, invoke);
1908}
1909
1910void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
1911 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1912}
1913
1914void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1915 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1916 LocationSummary::kNoCall,
1917 kIntrinsified);
1918 locations->SetOut(Location::RequiresRegister());
1919}
1920
1921void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
1922 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001923 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64WordSize>(),
1924 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001925}
1926
Roland Levillain0d5a2812015-11-13 10:07:31 +00001927static void GenUnsafeGet(HInvoke* invoke,
1928 Primitive::Type type,
1929 bool is_volatile ATTRIBUTE_UNUSED,
1930 CodeGeneratorX86_64* codegen) {
1931 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
1932 LocationSummary* locations = invoke->GetLocations();
1933 Location base_loc = locations->InAt(1);
1934 CpuRegister base = base_loc.AsRegister<CpuRegister>();
1935 Location offset_loc = locations->InAt(2);
1936 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
1937 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001938 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001939
Andreas Gampe878d58c2015-01-15 23:24:00 -08001940 switch (type) {
1941 case Primitive::kPrimInt:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001942 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001943 break;
1944
1945 case Primitive::kPrimNot: {
1946 if (kEmitCompilerReadBarrier) {
1947 if (kUseBakerReadBarrier) {
1948 Location temp = locations->GetTemp(0);
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001949 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1950 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1951 invoke, output_loc, base, src, temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001952 } else {
1953 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1954 codegen->GenerateReadBarrierSlow(
1955 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1956 }
1957 } else {
1958 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1959 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001960 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001961 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001962 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08001963
1964 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00001965 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08001966 break;
1967
1968 default:
1969 LOG(FATAL) << "Unsupported op size " << type;
1970 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001971 }
1972}
1973
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001974static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
1975 HInvoke* invoke,
1976 Primitive::Type type) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001977 bool can_call = kEmitCompilerReadBarrier &&
1978 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1979 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001980 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001981 can_call ?
1982 LocationSummary::kCallOnSlowPath :
1983 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001984 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08001985 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001986 locations->SetInAt(1, Location::RequiresRegister());
1987 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001988 locations->SetOut(Location::RequiresRegister(),
1989 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001990 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1991 // We need a temporary register for the read barrier marking slow
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001992 // path in InstructionCodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001993 locations->AddTemp(Location::RequiresRegister());
1994 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001995}
1996
1997void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00001998 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001999}
2000void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002001 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002002}
2003void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002004 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002005}
2006void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002007 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002008}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002009void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002010 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002011}
2012void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002013 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002014}
2015
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002016
2017void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002018 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002019}
2020void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002021 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002022}
2023void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002024 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002025}
2026void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002027 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002028}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002029void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002030 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002031}
2032void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002033 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002034}
2035
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002036
2037static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
2038 Primitive::Type type,
2039 HInvoke* invoke) {
2040 LocationSummary* locations = new (arena) LocationSummary(invoke,
2041 LocationSummary::kNoCall,
2042 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002043 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002044 locations->SetInAt(1, Location::RequiresRegister());
2045 locations->SetInAt(2, Location::RequiresRegister());
2046 locations->SetInAt(3, Location::RequiresRegister());
2047 if (type == Primitive::kPrimNot) {
2048 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002049 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002050 locations->AddTemp(Location::RequiresRegister());
2051 }
2052}
2053
2054void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2055 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2056}
2057void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2058 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2059}
2060void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2061 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2062}
2063void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2064 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2065}
2066void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2067 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2068}
2069void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2070 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2071}
2072void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2073 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2074}
2075void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2076 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2077}
2078void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2079 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2080}
2081
2082// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2083// memory model.
2084static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2085 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002086 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002087 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2088 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2089 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2090
2091 if (type == Primitive::kPrimLong) {
2092 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002093 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2094 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2095 __ movl(temp, value);
2096 __ PoisonHeapReference(temp);
2097 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002098 } else {
2099 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2100 }
2101
2102 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002103 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002104 }
2105
2106 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002107 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002108 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2109 locations->GetTemp(1).AsRegister<CpuRegister>(),
2110 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002111 value,
2112 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002113 }
2114}
2115
2116void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002117 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002118}
2119void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002120 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002121}
2122void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002123 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002124}
2125void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002126 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002127}
2128void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002129 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002130}
2131void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002132 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002133}
2134void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002135 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002136}
2137void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002138 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002139}
2140void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002141 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002142}
2143
Mark Mendell58d25fd2015-04-03 14:52:31 -04002144static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2145 HInvoke* invoke) {
2146 LocationSummary* locations = new (arena) LocationSummary(invoke,
2147 LocationSummary::kNoCall,
2148 kIntrinsified);
2149 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2150 locations->SetInAt(1, Location::RequiresRegister());
2151 locations->SetInAt(2, Location::RequiresRegister());
2152 // expected value must be in EAX/RAX.
2153 locations->SetInAt(3, Location::RegisterLocation(RAX));
2154 locations->SetInAt(4, Location::RequiresRegister());
2155
2156 locations->SetOut(Location::RequiresRegister());
2157 if (type == Primitive::kPrimNot) {
2158 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002159 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002160 locations->AddTemp(Location::RequiresRegister());
2161 }
2162}
2163
2164void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2165 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2166}
2167
2168void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2169 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2170}
2171
2172void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002173 // The UnsafeCASObject intrinsic is missing a read barrier, and
2174 // therefore sometimes does not work as expected (b/25883050).
2175 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01002176 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00002177 //
Roland Levillain3d312422016-06-23 13:53:42 +01002178 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
Roland Levillain391b8662015-12-18 11:43:38 +00002179 // this intrinsic.
2180 if (kEmitCompilerReadBarrier) {
2181 return;
2182 }
2183
Mark Mendell58d25fd2015-04-03 14:52:31 -04002184 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2185}
2186
2187static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002188 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002189 LocationSummary* locations = invoke->GetLocations();
2190
2191 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2192 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2193 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002194 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002195 DCHECK_EQ(expected.AsRegister(), RAX);
2196 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
2197 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2198
Roland Levillainb488b782015-10-22 11:38:49 +01002199 if (type == Primitive::kPrimNot) {
2200 // Mark card for object assuming new value is stored.
2201 bool value_can_be_null = true; // TODO: Worth finding out this information?
2202 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2203 locations->GetTemp(1).AsRegister<CpuRegister>(),
2204 base,
2205 value,
2206 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01002207
Roland Levillainb488b782015-10-22 11:38:49 +01002208 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2209 Register value_reg = value.AsRegister();
2210 if (kPoisonHeapReferences) {
2211 if (base_equals_value) {
2212 // If `base` and `value` are the same register location, move
2213 // `value_reg` to a temporary register. This way, poisoning
2214 // `value_reg` won't invalidate `base`.
2215 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
2216 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002217 }
Roland Levillainb488b782015-10-22 11:38:49 +01002218
2219 // Check that the register allocator did not assign the location
2220 // of `expected` (RAX) to `value` nor to `base`, so that heap
2221 // poisoning (when enabled) works as intended below.
2222 // - If `value` were equal to `expected`, both references would
2223 // be poisoned twice, meaning they would not be poisoned at
2224 // all, as heap poisoning uses address negation.
2225 // - If `base` were equal to `expected`, poisoning `expected`
2226 // would invalidate `base`.
2227 DCHECK_NE(value_reg, expected.AsRegister());
2228 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2229
2230 __ PoisonHeapReference(expected);
2231 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002232 }
2233
Roland Levillain391b8662015-12-18 11:43:38 +00002234 // TODO: Add a read barrier for the reference stored in the object
2235 // before attempting the CAS, similar to the one in the
2236 // art::Unsafe_compareAndSwapObject JNI implementation.
2237 //
2238 // Note that this code is not (yet) used when read barriers are
2239 // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject).
2240 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillainb488b782015-10-22 11:38:49 +01002241 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002242
Roland Levillain0d5a2812015-11-13 10:07:31 +00002243 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002244 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002245
Roland Levillainb488b782015-10-22 11:38:49 +01002246 // Convert ZF into the boolean result.
2247 __ setcc(kZero, out);
2248 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002249
Roland Levillain391b8662015-12-18 11:43:38 +00002250 // If heap poisoning is enabled, we need to unpoison the values
2251 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002252 if (kPoisonHeapReferences) {
2253 if (base_equals_value) {
2254 // `value_reg` has been moved to a temporary register, no need
2255 // to unpoison it.
2256 } else {
2257 // Ensure `value` is different from `out`, so that unpoisoning
2258 // the former does not invalidate the latter.
2259 DCHECK_NE(value_reg, out.AsRegister());
2260 __ UnpoisonHeapReference(CpuRegister(value_reg));
2261 }
2262 // Ensure `expected` is different from `out`, so that unpoisoning
2263 // the former does not invalidate the latter.
2264 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2265 __ UnpoisonHeapReference(expected);
2266 }
2267 } else {
2268 if (type == Primitive::kPrimInt) {
2269 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2270 } else if (type == Primitive::kPrimLong) {
2271 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2272 } else {
2273 LOG(FATAL) << "Unexpected CAS type " << type;
2274 }
2275
Roland Levillain0d5a2812015-11-13 10:07:31 +00002276 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002277 // scheduling barriers at this time.
2278
2279 // Convert ZF into the boolean result.
2280 __ setcc(kZero, out);
2281 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002282 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002283}
2284
2285void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2286 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2287}
2288
2289void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2290 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2291}
2292
2293void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002294 // The UnsafeCASObject intrinsic is missing a read barrier, and
2295 // therefore sometimes does not work as expected (b/25883050).
2296 // Turn it off temporarily as a quick fix, until the read barrier is
2297 // implemented (see TODO in GenCAS).
2298 //
2299 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
2300 // this intrinsic.
2301 DCHECK(!kEmitCompilerReadBarrier);
2302
Mark Mendell58d25fd2015-04-03 14:52:31 -04002303 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2304}
2305
2306void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(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}
2314
2315static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2316 X86_64Assembler* assembler) {
2317 Immediate imm_shift(shift);
2318 Immediate imm_mask(mask);
2319 __ movl(temp, reg);
2320 __ shrl(reg, imm_shift);
2321 __ andl(temp, imm_mask);
2322 __ andl(reg, imm_mask);
2323 __ shll(temp, imm_shift);
2324 __ orl(reg, temp);
2325}
2326
2327void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002328 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002329 LocationSummary* locations = invoke->GetLocations();
2330
2331 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2332 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2333
2334 /*
2335 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2336 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2337 * compared to generic luni implementation which has 5 rounds of swapping bits.
2338 * x = bswap x
2339 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2340 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2341 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2342 */
2343 __ bswapl(reg);
2344 SwapBits(reg, temp, 1, 0x55555555, assembler);
2345 SwapBits(reg, temp, 2, 0x33333333, assembler);
2346 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2347}
2348
2349void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2350 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2351 LocationSummary::kNoCall,
2352 kIntrinsified);
2353 locations->SetInAt(0, Location::RequiresRegister());
2354 locations->SetOut(Location::SameAsFirstInput());
2355 locations->AddTemp(Location::RequiresRegister());
2356 locations->AddTemp(Location::RequiresRegister());
2357}
2358
2359static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2360 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2361 Immediate imm_shift(shift);
2362 __ movq(temp_mask, Immediate(mask));
2363 __ movq(temp, reg);
2364 __ shrq(reg, imm_shift);
2365 __ andq(temp, temp_mask);
2366 __ andq(reg, temp_mask);
2367 __ shlq(temp, imm_shift);
2368 __ orq(reg, temp);
2369}
2370
2371void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002372 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002373 LocationSummary* locations = invoke->GetLocations();
2374
2375 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2376 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2377 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2378
2379 /*
2380 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2381 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2382 * compared to generic luni implementation which has 5 rounds of swapping bits.
2383 * x = bswap x
2384 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2385 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2386 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2387 */
2388 __ bswapq(reg);
2389 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2390 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2391 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2392}
2393
Aart Bik3f67e692016-01-15 14:35:12 -08002394static void CreateBitCountLocations(
2395 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2396 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2397 // Do nothing if there is no popcnt support. This results in generating
2398 // a call for the intrinsic rather than direct code.
2399 return;
2400 }
2401 LocationSummary* locations = new (arena) LocationSummary(invoke,
2402 LocationSummary::kNoCall,
2403 kIntrinsified);
2404 locations->SetInAt(0, Location::Any());
2405 locations->SetOut(Location::RequiresRegister());
2406}
2407
Aart Bikc5d47542016-01-27 17:00:35 -08002408static void GenBitCount(X86_64Assembler* assembler,
2409 CodeGeneratorX86_64* codegen,
2410 HInvoke* invoke,
2411 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002412 LocationSummary* locations = invoke->GetLocations();
2413 Location src = locations->InAt(0);
2414 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2415
2416 if (invoke->InputAt(0)->IsConstant()) {
2417 // Evaluate this at compile time.
2418 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002419 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002420 ? POPCOUNT(static_cast<uint64_t>(value))
2421 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002422 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002423 return;
2424 }
2425
2426 if (src.IsRegister()) {
2427 if (is_long) {
2428 __ popcntq(out, src.AsRegister<CpuRegister>());
2429 } else {
2430 __ popcntl(out, src.AsRegister<CpuRegister>());
2431 }
2432 } else if (is_long) {
2433 DCHECK(src.IsDoubleStackSlot());
2434 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2435 } else {
2436 DCHECK(src.IsStackSlot());
2437 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2438 }
2439}
2440
2441void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2442 CreateBitCountLocations(arena_, codegen_, invoke);
2443}
2444
2445void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002446 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002447}
2448
2449void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2450 CreateBitCountLocations(arena_, codegen_, invoke);
2451}
2452
2453void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002454 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2455}
2456
Aart Bikc5d47542016-01-27 17:00:35 -08002457static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2458 LocationSummary* locations = new (arena) LocationSummary(invoke,
2459 LocationSummary::kNoCall,
2460 kIntrinsified);
2461 locations->SetInAt(0, Location::Any());
2462 locations->SetOut(Location::RequiresRegister());
2463 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2464 : Location::RequiresRegister()); // any will do
2465}
2466
2467static void GenOneBit(X86_64Assembler* assembler,
2468 CodeGeneratorX86_64* codegen,
2469 HInvoke* invoke,
2470 bool is_high, bool is_long) {
2471 LocationSummary* locations = invoke->GetLocations();
2472 Location src = locations->InAt(0);
2473 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2474
2475 if (invoke->InputAt(0)->IsConstant()) {
2476 // Evaluate this at compile time.
2477 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2478 if (value == 0) {
2479 __ xorl(out, out); // Clears upper bits too.
2480 return;
2481 }
2482 // Nonzero value.
2483 if (is_high) {
2484 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2485 : 31 - CLZ(static_cast<uint32_t>(value));
2486 } else {
2487 value = is_long ? CTZ(static_cast<uint64_t>(value))
2488 : CTZ(static_cast<uint32_t>(value));
2489 }
2490 if (is_long) {
2491 codegen->Load64BitValue(out, 1L << value);
2492 } else {
2493 codegen->Load32BitValue(out, 1 << value);
2494 }
2495 return;
2496 }
2497
2498 // Handle the non-constant cases.
2499 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2500 if (is_high) {
2501 // Use architectural support: basically 1 << bsr.
2502 if (src.IsRegister()) {
2503 if (is_long) {
2504 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2505 } else {
2506 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2507 }
2508 } else if (is_long) {
2509 DCHECK(src.IsDoubleStackSlot());
2510 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2511 } else {
2512 DCHECK(src.IsStackSlot());
2513 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2514 }
2515 // BSR sets ZF if the input was zero.
2516 NearLabel is_zero, done;
2517 __ j(kEqual, &is_zero);
2518 __ movl(out, Immediate(1)); // Clears upper bits too.
2519 if (is_long) {
2520 __ shlq(out, tmp);
2521 } else {
2522 __ shll(out, tmp);
2523 }
2524 __ jmp(&done);
2525 __ Bind(&is_zero);
2526 __ xorl(out, out); // Clears upper bits too.
2527 __ Bind(&done);
2528 } else {
2529 // Copy input into temporary.
2530 if (src.IsRegister()) {
2531 if (is_long) {
2532 __ movq(tmp, src.AsRegister<CpuRegister>());
2533 } else {
2534 __ movl(tmp, src.AsRegister<CpuRegister>());
2535 }
2536 } else if (is_long) {
2537 DCHECK(src.IsDoubleStackSlot());
2538 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2539 } else {
2540 DCHECK(src.IsStackSlot());
2541 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2542 }
2543 // Do the bit twiddling: basically tmp & -tmp;
2544 if (is_long) {
2545 __ movq(out, tmp);
2546 __ negq(tmp);
2547 __ andq(out, tmp);
2548 } else {
2549 __ movl(out, tmp);
2550 __ negl(tmp);
2551 __ andl(out, tmp);
2552 }
2553 }
2554}
2555
2556void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2557 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2558}
2559
2560void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2561 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2562}
2563
2564void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2565 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2566}
2567
2568void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2569 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2570}
2571
2572void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2573 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2574}
2575
2576void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2577 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2578}
2579
2580void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2581 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2582}
2583
2584void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2585 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002586}
2587
Mark Mendelld5897672015-08-12 21:16:41 -04002588static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2589 LocationSummary* locations = new (arena) LocationSummary(invoke,
2590 LocationSummary::kNoCall,
2591 kIntrinsified);
2592 locations->SetInAt(0, Location::Any());
2593 locations->SetOut(Location::RequiresRegister());
2594}
2595
Aart Bikc5d47542016-01-27 17:00:35 -08002596static void GenLeadingZeros(X86_64Assembler* assembler,
2597 CodeGeneratorX86_64* codegen,
2598 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002599 LocationSummary* locations = invoke->GetLocations();
2600 Location src = locations->InAt(0);
2601 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2602
2603 int zero_value_result = is_long ? 64 : 32;
2604 if (invoke->InputAt(0)->IsConstant()) {
2605 // Evaluate this at compile time.
2606 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2607 if (value == 0) {
2608 value = zero_value_result;
2609 } else {
2610 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2611 }
Aart Bikc5d47542016-01-27 17:00:35 -08002612 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002613 return;
2614 }
2615
2616 // Handle the non-constant cases.
2617 if (src.IsRegister()) {
2618 if (is_long) {
2619 __ bsrq(out, src.AsRegister<CpuRegister>());
2620 } else {
2621 __ bsrl(out, src.AsRegister<CpuRegister>());
2622 }
2623 } else if (is_long) {
2624 DCHECK(src.IsDoubleStackSlot());
2625 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2626 } else {
2627 DCHECK(src.IsStackSlot());
2628 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2629 }
2630
2631 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002632 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002633 __ j(kEqual, &is_zero);
2634
2635 // Correct the result from BSR to get the CLZ result.
2636 __ xorl(out, Immediate(zero_value_result - 1));
2637 __ jmp(&done);
2638
2639 // Fix the zero case with the expected result.
2640 __ Bind(&is_zero);
2641 __ movl(out, Immediate(zero_value_result));
2642
2643 __ Bind(&done);
2644}
2645
2646void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2647 CreateLeadingZeroLocations(arena_, invoke);
2648}
2649
2650void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002651 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002652}
2653
2654void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2655 CreateLeadingZeroLocations(arena_, invoke);
2656}
2657
2658void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002659 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002660}
2661
Mark Mendell2d554792015-09-15 21:45:18 -04002662static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2663 LocationSummary* locations = new (arena) LocationSummary(invoke,
2664 LocationSummary::kNoCall,
2665 kIntrinsified);
2666 locations->SetInAt(0, Location::Any());
2667 locations->SetOut(Location::RequiresRegister());
2668}
2669
Aart Bikc5d47542016-01-27 17:00:35 -08002670static void GenTrailingZeros(X86_64Assembler* assembler,
2671 CodeGeneratorX86_64* codegen,
2672 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002673 LocationSummary* locations = invoke->GetLocations();
2674 Location src = locations->InAt(0);
2675 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2676
2677 int zero_value_result = is_long ? 64 : 32;
2678 if (invoke->InputAt(0)->IsConstant()) {
2679 // Evaluate this at compile time.
2680 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2681 if (value == 0) {
2682 value = zero_value_result;
2683 } else {
2684 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2685 }
Aart Bikc5d47542016-01-27 17:00:35 -08002686 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002687 return;
2688 }
2689
2690 // Handle the non-constant cases.
2691 if (src.IsRegister()) {
2692 if (is_long) {
2693 __ bsfq(out, src.AsRegister<CpuRegister>());
2694 } else {
2695 __ bsfl(out, src.AsRegister<CpuRegister>());
2696 }
2697 } else if (is_long) {
2698 DCHECK(src.IsDoubleStackSlot());
2699 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2700 } else {
2701 DCHECK(src.IsStackSlot());
2702 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2703 }
2704
2705 // BSF sets ZF if the input was zero, and the output is undefined.
2706 NearLabel done;
2707 __ j(kNotEqual, &done);
2708
2709 // Fix the zero case with the expected result.
2710 __ movl(out, Immediate(zero_value_result));
2711
2712 __ Bind(&done);
2713}
2714
2715void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2716 CreateTrailingZeroLocations(arena_, invoke);
2717}
2718
2719void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002720 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002721}
2722
2723void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2724 CreateTrailingZeroLocations(arena_, invoke);
2725}
2726
2727void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002728 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2729}
2730
Serguei Katkov288c7a82016-05-16 11:53:15 +06002731void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2732 if (kEmitCompilerReadBarrier) {
2733 // Do not intrinsify this call with the read barrier configuration.
2734 return;
2735 }
2736 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2737 LocationSummary::kCallOnSlowPath,
2738 kIntrinsified);
2739 locations->SetInAt(0, Location::RequiresRegister());
2740 locations->SetOut(Location::SameAsFirstInput());
2741 locations->AddTemp(Location::RequiresRegister());
2742}
2743
2744void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2745 DCHECK(!kEmitCompilerReadBarrier);
2746 LocationSummary* locations = invoke->GetLocations();
2747 X86_64Assembler* assembler = GetAssembler();
2748
2749 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2750 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2751
2752 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2753 codegen_->AddSlowPath(slow_path);
2754
2755 // Load ArtMethod first.
2756 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2757 DCHECK(invoke_direct != nullptr);
2758 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2759 invoke_direct, locations->GetTemp(0));
2760 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2761 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2762
2763 // Now get declaring class.
2764 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2765
2766 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2767 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2768 DCHECK_NE(slow_path_flag_offset, 0u);
2769 DCHECK_NE(disable_flag_offset, 0u);
2770 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2771
2772 // Check static flags preventing us for using intrinsic.
2773 if (slow_path_flag_offset == disable_flag_offset + 1) {
2774 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2775 __ j(kNotEqual, slow_path->GetEntryLabel());
2776 } else {
2777 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2778 __ j(kNotEqual, slow_path->GetEntryLabel());
2779 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2780 __ j(kNotEqual, slow_path->GetEntryLabel());
2781 }
2782
2783 // Fast path.
2784 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2785 codegen_->MaybeRecordImplicitNullCheck(invoke);
2786 __ MaybeUnpoisonHeapReference(out);
2787 __ Bind(slow_path->GetExitLabel());
2788}
2789
Aart Bik2f9fcc92016-03-01 15:16:54 -08002790UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2791UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002792
Aart Bik0e54c012016-03-04 12:08:31 -08002793// 1.8.
2794UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2795UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2796UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2797UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2798UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002799
Aart Bik2f9fcc92016-03-01 15:16:54 -08002800UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002801
2802#undef __
2803
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002804} // namespace x86_64
2805} // namespace art