blob: 311e1cd6eb0c1fe9fd0080727378b1162e16933e [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
Roland Levillain0b671c02016-08-19 12:02:34 +010067// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
68#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
69
70// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
71class ReadBarrierSystemArrayCopySlowPathX86_64 : public SlowPathCode {
72 public:
73 explicit ReadBarrierSystemArrayCopySlowPathX86_64(HInstruction* instruction)
74 : SlowPathCode(instruction) {
75 DCHECK(kEmitCompilerReadBarrier);
76 DCHECK(kUseBakerReadBarrier);
77 }
78
79 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
80 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
81 LocationSummary* locations = instruction_->GetLocations();
82 DCHECK(locations->CanCall());
83 DCHECK(instruction_->IsInvokeStaticOrDirect())
84 << "Unexpected instruction in read barrier arraycopy slow path: "
85 << instruction_->DebugName();
86 DCHECK(instruction_->GetLocations()->Intrinsified());
87 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
88
89 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
90
91 CpuRegister src_curr_addr = locations->GetTemp(0).AsRegister<CpuRegister>();
92 CpuRegister dst_curr_addr = locations->GetTemp(1).AsRegister<CpuRegister>();
93 CpuRegister src_stop_addr = locations->GetTemp(2).AsRegister<CpuRegister>();
94
95 __ Bind(GetEntryLabel());
96 NearLabel loop;
97 __ Bind(&loop);
98 __ movl(CpuRegister(TMP), Address(src_curr_addr, 0));
99 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
100 // TODO: Inline the mark bit check before calling the runtime?
101 // TMP = ReadBarrier::Mark(TMP);
102 // No need to save live registers; it's taken care of by the
103 // entrypoint. Also, there is no need to update the stack mask,
104 // as this runtime call will not trigger a garbage collection.
105 int32_t entry_point_offset =
106 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(TMP);
107 // This runtime call does not require a stack map.
108 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
109 __ MaybePoisonHeapReference(CpuRegister(TMP));
110 __ movl(Address(dst_curr_addr, 0), CpuRegister(TMP));
111 __ addl(src_curr_addr, Immediate(element_size));
112 __ addl(dst_curr_addr, Immediate(element_size));
113 __ cmpl(src_curr_addr, src_stop_addr);
114 __ j(kNotEqual, &loop);
115 __ jmp(GetExitLabel());
116 }
117
118 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86_64"; }
119
120 private:
121 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86_64);
122};
123
124#undef __
125
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800126#define __ assembler->
127
128static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
129 LocationSummary* locations = new (arena) LocationSummary(invoke,
130 LocationSummary::kNoCall,
131 kIntrinsified);
132 locations->SetInAt(0, Location::RequiresFpuRegister());
133 locations->SetOut(Location::RequiresRegister());
134}
135
136static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
137 LocationSummary* locations = new (arena) LocationSummary(invoke,
138 LocationSummary::kNoCall,
139 kIntrinsified);
140 locations->SetInAt(0, Location::RequiresRegister());
141 locations->SetOut(Location::RequiresFpuRegister());
142}
143
144static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
145 Location input = locations->InAt(0);
146 Location output = locations->Out();
147 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
148}
149
150static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
151 Location input = locations->InAt(0);
152 Location output = locations->Out();
153 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
154}
155
156void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
157 CreateFPToIntLocations(arena_, invoke);
158}
159void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
160 CreateIntToFPLocations(arena_, invoke);
161}
162
163void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000164 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800165}
166void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000167 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800168}
169
170void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
171 CreateFPToIntLocations(arena_, invoke);
172}
173void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
174 CreateIntToFPLocations(arena_, invoke);
175}
176
177void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000178 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800179}
180void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000181 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800182}
183
184static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
185 LocationSummary* locations = new (arena) LocationSummary(invoke,
186 LocationSummary::kNoCall,
187 kIntrinsified);
188 locations->SetInAt(0, Location::RequiresRegister());
189 locations->SetOut(Location::SameAsFirstInput());
190}
191
192static void GenReverseBytes(LocationSummary* locations,
193 Primitive::Type size,
194 X86_64Assembler* assembler) {
195 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
196
197 switch (size) {
198 case Primitive::kPrimShort:
199 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
200 __ bswapl(out);
201 __ sarl(out, Immediate(16));
202 break;
203 case Primitive::kPrimInt:
204 __ bswapl(out);
205 break;
206 case Primitive::kPrimLong:
207 __ bswapq(out);
208 break;
209 default:
210 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
211 UNREACHABLE();
212 }
213}
214
215void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
216 CreateIntToIntLocations(arena_, invoke);
217}
218
219void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
220 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
221}
222
223void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
224 CreateIntToIntLocations(arena_, invoke);
225}
226
227void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
228 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
229}
230
231void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
232 CreateIntToIntLocations(arena_, invoke);
233}
234
235void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
236 GenReverseBytes(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
237}
238
239
240// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
241// need is 64b.
242
243static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
244 // TODO: Enable memory operations when the assembler supports them.
245 LocationSummary* locations = new (arena) LocationSummary(invoke,
246 LocationSummary::kNoCall,
247 kIntrinsified);
248 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800249 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400250 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800251}
252
Mark Mendell39dcf552015-04-09 20:42:42 -0400253static void MathAbsFP(LocationSummary* locations,
254 bool is64bit,
255 X86_64Assembler* assembler,
256 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800257 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800258
Mark Mendellcfa410b2015-05-25 16:02:44 -0400259 DCHECK(output.IsFpuRegister());
260 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800261
Mark Mendellcfa410b2015-05-25 16:02:44 -0400262 // TODO: Can mask directly with constant area using pand if we can guarantee
263 // that the literal is aligned on a 16 byte boundary. This will avoid a
264 // temporary.
265 if (is64bit) {
266 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
267 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800268 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400269 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
270 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800271 }
272}
273
274void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
275 CreateFloatToFloatPlusTemps(arena_, invoke);
276}
277
278void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000279 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800280}
281
282void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
283 CreateFloatToFloatPlusTemps(arena_, invoke);
284}
285
286void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000287 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800288}
289
290static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
291 LocationSummary* locations = new (arena) LocationSummary(invoke,
292 LocationSummary::kNoCall,
293 kIntrinsified);
294 locations->SetInAt(0, Location::RequiresRegister());
295 locations->SetOut(Location::SameAsFirstInput());
296 locations->AddTemp(Location::RequiresRegister());
297}
298
299static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
300 Location output = locations->Out();
301 CpuRegister out = output.AsRegister<CpuRegister>();
302 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
303
304 if (is64bit) {
305 // Create mask.
306 __ movq(mask, out);
307 __ sarq(mask, Immediate(63));
308 // Add mask.
309 __ addq(out, mask);
310 __ xorq(out, mask);
311 } else {
312 // Create mask.
313 __ movl(mask, out);
314 __ sarl(mask, Immediate(31));
315 // Add mask.
316 __ addl(out, mask);
317 __ xorl(out, mask);
318 }
319}
320
321void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
322 CreateIntToIntPlusTemp(arena_, invoke);
323}
324
325void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000326 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800327}
328
329void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
330 CreateIntToIntPlusTemp(arena_, invoke);
331}
332
333void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000334 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800335}
336
Mark Mendell39dcf552015-04-09 20:42:42 -0400337static void GenMinMaxFP(LocationSummary* locations,
338 bool is_min,
339 bool is_double,
340 X86_64Assembler* assembler,
341 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800342 Location op1_loc = locations->InAt(0);
343 Location op2_loc = locations->InAt(1);
344 Location out_loc = locations->Out();
345 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
346
347 // Shortcut for same input locations.
348 if (op1_loc.Equals(op2_loc)) {
349 DCHECK(out_loc.Equals(op1_loc));
350 return;
351 }
352
353 // (out := op1)
354 // out <=? op2
355 // if Nan jmp Nan_label
356 // if out is min jmp done
357 // if op2 is min jmp op2_label
358 // handle -0/+0
359 // jmp done
360 // Nan_label:
361 // out := NaN
362 // op2_label:
363 // out := op2
364 // done:
365 //
366 // This removes one jmp, but needs to copy one input (op1) to out.
367 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400368 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800369
370 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
371
Mark Mendell0c9497d2015-08-21 09:30:05 -0400372 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800373 if (is_double) {
374 __ ucomisd(out, op2);
375 } else {
376 __ ucomiss(out, op2);
377 }
378
379 __ j(Condition::kParityEven, &nan);
380
381 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
382 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
383
384 // Handle 0.0/-0.0.
385 if (is_min) {
386 if (is_double) {
387 __ orpd(out, op2);
388 } else {
389 __ orps(out, op2);
390 }
391 } else {
392 if (is_double) {
393 __ andpd(out, op2);
394 } else {
395 __ andps(out, op2);
396 }
397 }
398 __ jmp(&done);
399
400 // NaN handling.
401 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800402 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400403 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800404 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400405 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800406 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800407 __ jmp(&done);
408
409 // out := op2;
410 __ Bind(&op2_label);
411 if (is_double) {
412 __ movsd(out, op2);
413 } else {
414 __ movss(out, op2);
415 }
416
417 // Done.
418 __ Bind(&done);
419}
420
Mark Mendellf55c3e02015-03-26 21:07:46 -0400421static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800422 LocationSummary* locations = new (arena) LocationSummary(invoke,
423 LocationSummary::kNoCall,
424 kIntrinsified);
425 locations->SetInAt(0, Location::RequiresFpuRegister());
426 locations->SetInAt(1, Location::RequiresFpuRegister());
427 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
428 // the second input to be the output (we can simply swap inputs).
429 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800430}
431
432void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400433 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800434}
435
436void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000437 GenMinMaxFP(
438 invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800439}
440
441void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400442 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800443}
444
445void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000446 GenMinMaxFP(
447 invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800448}
449
450void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400451 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800452}
453
454void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000455 GenMinMaxFP(
456 invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800457}
458
459void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400460 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800461}
462
463void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000464 GenMinMaxFP(
465 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800466}
467
468static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
469 X86_64Assembler* assembler) {
470 Location op1_loc = locations->InAt(0);
471 Location op2_loc = locations->InAt(1);
472
473 // Shortcut for same input locations.
474 if (op1_loc.Equals(op2_loc)) {
475 // Can return immediately, as op1_loc == out_loc.
476 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
477 // a copy here.
478 DCHECK(locations->Out().Equals(op1_loc));
479 return;
480 }
481
482 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
483 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
484
485 // (out := op1)
486 // out <=? op2
487 // if out is min jmp done
488 // out := op2
489 // done:
490
491 if (is_long) {
492 __ cmpq(out, op2);
493 } else {
494 __ cmpl(out, op2);
495 }
496
497 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
498}
499
500static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
501 LocationSummary* locations = new (arena) LocationSummary(invoke,
502 LocationSummary::kNoCall,
503 kIntrinsified);
504 locations->SetInAt(0, Location::RequiresRegister());
505 locations->SetInAt(1, Location::RequiresRegister());
506 locations->SetOut(Location::SameAsFirstInput());
507}
508
509void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
510 CreateIntIntToIntLocations(arena_, invoke);
511}
512
513void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000514 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800515}
516
517void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
518 CreateIntIntToIntLocations(arena_, invoke);
519}
520
521void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000522 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800523}
524
525void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
526 CreateIntIntToIntLocations(arena_, invoke);
527}
528
529void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000530 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800531}
532
533void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
534 CreateIntIntToIntLocations(arena_, invoke);
535}
536
537void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000538 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800539}
540
541static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
542 LocationSummary* locations = new (arena) LocationSummary(invoke,
543 LocationSummary::kNoCall,
544 kIntrinsified);
545 locations->SetInAt(0, Location::RequiresFpuRegister());
546 locations->SetOut(Location::RequiresFpuRegister());
547}
548
549void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
550 CreateFPToFPLocations(arena_, invoke);
551}
552
553void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
554 LocationSummary* locations = invoke->GetLocations();
555 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
556 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
557
558 GetAssembler()->sqrtsd(out, in);
559}
560
Mark Mendellfb8d2792015-03-31 22:16:59 -0400561static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100562 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400563
564 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100565 codegen->GenerateStaticOrDirectCall(
566 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400567 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
568
569 // Copy the result back to the expected output.
570 Location out = invoke->GetLocations()->Out();
571 if (out.IsValid()) {
572 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700573 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400574 }
575}
576
577static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
578 HInvoke* invoke,
579 CodeGeneratorX86_64* codegen) {
580 // Do we have instruction support?
581 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
582 CreateFPToFPLocations(arena, invoke);
583 return;
584 }
585
586 // We have to fall back to a call to the intrinsic.
587 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100588 LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400589 InvokeRuntimeCallingConvention calling_convention;
590 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
591 locations->SetOut(Location::FpuRegisterLocation(XMM0));
592 // Needs to be RDI for the invoke.
593 locations->AddTemp(Location::RegisterLocation(RDI));
594}
595
596static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
597 HInvoke* invoke,
598 X86_64Assembler* assembler,
599 int round_mode) {
600 LocationSummary* locations = invoke->GetLocations();
601 if (locations->WillCall()) {
602 InvokeOutOfLineIntrinsic(codegen, invoke);
603 } else {
604 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
605 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
606 __ roundsd(out, in, Immediate(round_mode));
607 }
608}
609
610void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
611 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
612}
613
614void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
615 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
616}
617
618void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
619 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
620}
621
622void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
623 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
624}
625
626void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
627 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
628}
629
630void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
631 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
632}
633
634static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
635 HInvoke* invoke,
636 CodeGeneratorX86_64* codegen) {
637 // Do we have instruction support?
638 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
639 LocationSummary* locations = new (arena) LocationSummary(invoke,
640 LocationSummary::kNoCall,
641 kIntrinsified);
642 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600643 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400644 locations->AddTemp(Location::RequiresFpuRegister());
Aart Bik349f3882016-08-02 15:40:56 -0700645 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400646 return;
647 }
648
649 // We have to fall back to a call to the intrinsic.
650 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100651 LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400652 InvokeRuntimeCallingConvention calling_convention;
653 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
654 locations->SetOut(Location::RegisterLocation(RAX));
655 // Needs to be RDI for the invoke.
656 locations->AddTemp(Location::RegisterLocation(RDI));
657}
658
659void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
Aart Bik349f3882016-08-02 15:40:56 -0700660 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400661}
662
663void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
664 LocationSummary* locations = invoke->GetLocations();
665 if (locations->WillCall()) {
666 InvokeOutOfLineIntrinsic(codegen_, invoke);
667 return;
668 }
669
Mark Mendellfb8d2792015-03-31 22:16:59 -0400670 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
671 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700672 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
673 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
674 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400675 X86_64Assembler* assembler = GetAssembler();
676
Aart Bik349f3882016-08-02 15:40:56 -0700677 // Since no direct x86 rounding instruction matches the required semantics,
678 // this intrinsic is implemented as follows:
679 // result = floor(in);
680 // if (in - result >= 0.5f)
681 // result = result + 1.0f;
682 __ movss(t2, in);
683 __ roundss(t1, in, Immediate(1));
684 __ subss(t2, t1);
685 __ comiss(t2, codegen_->LiteralFloatAddress(0.5f));
686 __ j(kBelow, &skip_incr);
687 __ addss(t1, codegen_->LiteralFloatAddress(1.0f));
688 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400689
Aart Bik349f3882016-08-02 15:40:56 -0700690 // Final conversion to an integer. Unfortunately this also does not have a
691 // direct x86 instruction, since NaN should map to 0 and large positive
692 // values need to be clipped to the extreme value.
693 codegen_->Load32BitValue(out, kPrimIntMax);
694 __ cvtsi2ss(t2, out);
695 __ comiss(t1, t2);
696 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
697 __ movl(out, Immediate(0)); // does not change flags
698 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
699 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400700 __ Bind(&done);
701}
702
703void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
Aart Bik349f3882016-08-02 15:40:56 -0700704 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400705}
706
707void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
708 LocationSummary* locations = invoke->GetLocations();
709 if (locations->WillCall()) {
710 InvokeOutOfLineIntrinsic(codegen_, invoke);
711 return;
712 }
713
Mark Mendellfb8d2792015-03-31 22:16:59 -0400714 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
715 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700716 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
717 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
718 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400719 X86_64Assembler* assembler = GetAssembler();
720
Aart Bik349f3882016-08-02 15:40:56 -0700721 // Since no direct x86 rounding instruction matches the required semantics,
722 // this intrinsic is implemented as follows:
723 // result = floor(in);
724 // if (in - result >= 0.5)
725 // result = result + 1.0f;
726 __ movsd(t2, in);
727 __ roundsd(t1, in, Immediate(1));
728 __ subsd(t2, t1);
729 __ comisd(t2, codegen_->LiteralDoubleAddress(0.5));
730 __ j(kBelow, &skip_incr);
731 __ addsd(t1, codegen_->LiteralDoubleAddress(1.0f));
732 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400733
Aart Bik349f3882016-08-02 15:40:56 -0700734 // Final conversion to an integer. Unfortunately this also does not have a
735 // direct x86 instruction, since NaN should map to 0 and large positive
736 // values need to be clipped to the extreme value.
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600737 codegen_->Load64BitValue(out, kPrimLongMax);
Aart Bik349f3882016-08-02 15:40:56 -0700738 __ cvtsi2sd(t2, out, /* is64bit */ true);
739 __ comisd(t1, t2);
740 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
741 __ movl(out, Immediate(0)); // does not change flags, implicit zero extension to 64-bit
742 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
743 __ cvttsd2si(out, t1, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400744 __ Bind(&done);
745}
746
Mark Mendella4f12202015-08-06 15:23:34 -0400747static void CreateFPToFPCallLocations(ArenaAllocator* arena,
748 HInvoke* invoke) {
749 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100750 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400751 kIntrinsified);
752 InvokeRuntimeCallingConvention calling_convention;
753 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
754 locations->SetOut(Location::FpuRegisterLocation(XMM0));
755
756 // We have to ensure that the native code doesn't clobber the XMM registers which are
757 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
758 // saved in the prologue and properly restored.
759 for (auto fp_reg : non_volatile_xmm_regs) {
760 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
761 }
762}
763
764static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
765 QuickEntrypointEnum entry) {
766 LocationSummary* locations = invoke->GetLocations();
767 DCHECK(locations->WillCall());
768 DCHECK(invoke->IsInvokeStaticOrDirect());
769 X86_64Assembler* assembler = codegen->GetAssembler();
770
Andreas Gampe542451c2016-07-26 09:02:02 -0700771 __ gs()->call(Address::Absolute(GetThreadOffset<kX86_64PointerSize>(entry), true));
Mark Mendella4f12202015-08-06 15:23:34 -0400772 codegen->RecordPcInfo(invoke, invoke->GetDexPc());
773}
774
775void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
776 CreateFPToFPCallLocations(arena_, invoke);
777}
778
779void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
780 GenFPToFPCall(invoke, codegen_, kQuickCos);
781}
782
783void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
784 CreateFPToFPCallLocations(arena_, invoke);
785}
786
787void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
788 GenFPToFPCall(invoke, codegen_, kQuickSin);
789}
790
791void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
792 CreateFPToFPCallLocations(arena_, invoke);
793}
794
795void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
796 GenFPToFPCall(invoke, codegen_, kQuickAcos);
797}
798
799void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
800 CreateFPToFPCallLocations(arena_, invoke);
801}
802
803void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
804 GenFPToFPCall(invoke, codegen_, kQuickAsin);
805}
806
807void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
808 CreateFPToFPCallLocations(arena_, invoke);
809}
810
811void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
812 GenFPToFPCall(invoke, codegen_, kQuickAtan);
813}
814
815void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
816 CreateFPToFPCallLocations(arena_, invoke);
817}
818
819void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
820 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
821}
822
823void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
824 CreateFPToFPCallLocations(arena_, invoke);
825}
826
827void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
828 GenFPToFPCall(invoke, codegen_, kQuickCosh);
829}
830
831void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
832 CreateFPToFPCallLocations(arena_, invoke);
833}
834
835void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
836 GenFPToFPCall(invoke, codegen_, kQuickExp);
837}
838
839void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
840 CreateFPToFPCallLocations(arena_, invoke);
841}
842
843void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
844 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
845}
846
847void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
848 CreateFPToFPCallLocations(arena_, invoke);
849}
850
851void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
852 GenFPToFPCall(invoke, codegen_, kQuickLog);
853}
854
855void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
856 CreateFPToFPCallLocations(arena_, invoke);
857}
858
859void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
860 GenFPToFPCall(invoke, codegen_, kQuickLog10);
861}
862
863void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
864 CreateFPToFPCallLocations(arena_, invoke);
865}
866
867void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
868 GenFPToFPCall(invoke, codegen_, kQuickSinh);
869}
870
871void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
872 CreateFPToFPCallLocations(arena_, invoke);
873}
874
875void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
876 GenFPToFPCall(invoke, codegen_, kQuickTan);
877}
878
879void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
880 CreateFPToFPCallLocations(arena_, invoke);
881}
882
883void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
884 GenFPToFPCall(invoke, codegen_, kQuickTanh);
885}
886
887static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
888 HInvoke* invoke) {
889 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100890 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400891 kIntrinsified);
892 InvokeRuntimeCallingConvention calling_convention;
893 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
894 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
895 locations->SetOut(Location::FpuRegisterLocation(XMM0));
896
897 // We have to ensure that the native code doesn't clobber the XMM registers which are
898 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
899 // saved in the prologue and properly restored.
900 for (auto fp_reg : non_volatile_xmm_regs) {
901 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
902 }
903}
904
905void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
906 CreateFPFPToFPCallLocations(arena_, invoke);
907}
908
909void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
910 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
911}
912
913void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
914 CreateFPFPToFPCallLocations(arena_, invoke);
915}
916
917void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
918 GenFPToFPCall(invoke, codegen_, kQuickHypot);
919}
920
921void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
922 CreateFPFPToFPCallLocations(arena_, invoke);
923}
924
925void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
926 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
927}
928
Mark Mendell6bc53a92015-07-01 14:26:52 -0400929void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
930 // Check to see if we have known failures that will cause us to have to bail out
931 // to the runtime, and just generate the runtime call directly.
932 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
933 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
934
935 // The positions must be non-negative.
936 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
937 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
938 // We will have to fail anyways.
939 return;
940 }
941
942 // The length must be > 0.
943 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
944 if (length != nullptr) {
945 int32_t len = length->GetValue();
946 if (len < 0) {
947 // Just call as normal.
948 return;
949 }
950 }
951
952 LocationSummary* locations = new (arena_) LocationSummary(invoke,
953 LocationSummary::kCallOnSlowPath,
954 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100955 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400956 locations->SetInAt(0, Location::RequiresRegister());
957 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
958 locations->SetInAt(2, Location::RequiresRegister());
959 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
960 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
961
962 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
963 locations->AddTemp(Location::RegisterLocation(RSI));
964 locations->AddTemp(Location::RegisterLocation(RDI));
965 locations->AddTemp(Location::RegisterLocation(RCX));
966}
967
968static void CheckPosition(X86_64Assembler* assembler,
969 Location pos,
970 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100971 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700972 SlowPathCode* slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100973 CpuRegister temp,
974 bool length_is_input_length = false) {
975 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400976 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
977
978 if (pos.IsConstant()) {
979 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
980 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100981 if (!length_is_input_length) {
982 // Check that length(input) >= length.
983 if (length.IsConstant()) {
984 __ cmpl(Address(input, length_offset),
985 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
986 } else {
987 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
988 }
989 __ j(kLess, slow_path->GetEntryLabel());
990 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400991 } else {
992 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100993 __ movl(temp, Address(input, length_offset));
994 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400995 __ j(kLess, slow_path->GetEntryLabel());
996
997 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100998 if (length.IsConstant()) {
999 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1000 } else {
1001 __ cmpl(temp, length.AsRegister<CpuRegister>());
1002 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001003 __ j(kLess, slow_path->GetEntryLabel());
1004 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001005 } else if (length_is_input_length) {
1006 // The only way the copy can succeed is if pos is zero.
1007 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1008 __ testl(pos_reg, pos_reg);
1009 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001010 } else {
1011 // Check that pos >= 0.
1012 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1013 __ testl(pos_reg, pos_reg);
1014 __ j(kLess, slow_path->GetEntryLabel());
1015
1016 // Check that pos <= length(input).
1017 __ cmpl(Address(input, length_offset), pos_reg);
1018 __ j(kLess, slow_path->GetEntryLabel());
1019
1020 // Check that (length(input) - pos) >= length.
1021 __ movl(temp, Address(input, length_offset));
1022 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001023 if (length.IsConstant()) {
1024 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1025 } else {
1026 __ cmpl(temp, length.AsRegister<CpuRegister>());
1027 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001028 __ j(kLess, slow_path->GetEntryLabel());
1029 }
1030}
1031
1032void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1033 X86_64Assembler* assembler = GetAssembler();
1034 LocationSummary* locations = invoke->GetLocations();
1035
1036 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001037 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001038 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001039 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001040 Location length = locations->InAt(4);
1041
1042 // Temporaries that we need for MOVSW.
1043 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1044 DCHECK_EQ(src_base.AsRegister(), RSI);
1045 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1046 DCHECK_EQ(dest_base.AsRegister(), RDI);
1047 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1048 DCHECK_EQ(count.AsRegister(), RCX);
1049
Andreas Gampe85b62f22015-09-09 13:15:38 -07001050 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001051 codegen_->AddSlowPath(slow_path);
1052
1053 // Bail out if the source and destination are the same.
1054 __ cmpl(src, dest);
1055 __ j(kEqual, slow_path->GetEntryLabel());
1056
1057 // Bail out if the source is null.
1058 __ testl(src, src);
1059 __ j(kEqual, slow_path->GetEntryLabel());
1060
1061 // Bail out if the destination is null.
1062 __ testl(dest, dest);
1063 __ j(kEqual, slow_path->GetEntryLabel());
1064
1065 // If the length is negative, bail out.
1066 // We have already checked in the LocationsBuilder for the constant case.
1067 if (!length.IsConstant()) {
1068 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1069 __ j(kLess, slow_path->GetEntryLabel());
1070 }
1071
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001072 // Validity checks: source. Use src_base as a temporary register.
1073 CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001074
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001075 // Validity checks: dest. Use src_base as a temporary register.
1076 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001077
Mark Mendell6bc53a92015-07-01 14:26:52 -04001078 // We need the count in RCX.
1079 if (length.IsConstant()) {
1080 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1081 } else {
1082 __ movl(count, length.AsRegister<CpuRegister>());
1083 }
1084
Mark Mendell6bc53a92015-07-01 14:26:52 -04001085 // Okay, everything checks out. Finally time to do the copy.
1086 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1087 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1088 DCHECK_EQ(char_size, 2u);
1089
1090 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1091
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001092 if (src_pos.IsConstant()) {
1093 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1094 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001095 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001096 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001097 ScaleFactor::TIMES_2, data_offset));
1098 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001099 if (dest_pos.IsConstant()) {
1100 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1101 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001102 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001103 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001104 ScaleFactor::TIMES_2, data_offset));
1105 }
1106
1107 // Do the move.
1108 __ rep_movsw();
1109
1110 __ Bind(slow_path->GetExitLabel());
1111}
1112
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001113
1114void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001115 // The only read barrier implementation supporting the
1116 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1117 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01001118 return;
1119 }
1120
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001121 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001122}
1123
1124void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001125 // The only read barrier implementation supporting the
1126 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1127 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001128
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001129 X86_64Assembler* assembler = GetAssembler();
1130 LocationSummary* locations = invoke->GetLocations();
1131
1132 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1133 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1134 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1135 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001136 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001137
1138 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1139 Location src_pos = locations->InAt(1);
1140 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1141 Location dest_pos = locations->InAt(3);
1142 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +01001143 Location temp1_loc = locations->GetTemp(0);
1144 CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
1145 Location temp2_loc = locations->GetTemp(1);
1146 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
1147 Location temp3_loc = locations->GetTemp(2);
1148 CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
1149 Location TMP_loc = Location::RegisterLocation(TMP);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001150
Roland Levillain0b671c02016-08-19 12:02:34 +01001151 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1152 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001153
Roland Levillainebea3d22016-04-12 15:42:57 +01001154 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001155 SystemArrayCopyOptimizations optimizations(invoke);
1156
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001157 // If source and destination are the same, we go to slow path if we need to do
1158 // forward copying.
1159 if (src_pos.IsConstant()) {
1160 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1161 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001162 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1163 if (optimizations.GetDestinationIsSource()) {
1164 // Checked when building locations.
1165 DCHECK_GE(src_pos_constant, dest_pos_constant);
1166 } else if (src_pos_constant < dest_pos_constant) {
1167 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001168 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001169 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001170 } else {
1171 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001172 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001173 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001174 }
1175 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001176 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001177 }
1178 } else {
1179 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001180 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001181 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001182 }
1183 if (dest_pos.IsConstant()) {
1184 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1185 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001186 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001187 } else {
1188 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001189 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001190 }
1191 }
1192
Roland Levillainebea3d22016-04-12 15:42:57 +01001193 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001194
1195 if (!optimizations.GetSourceIsNotNull()) {
1196 // Bail out if the source is null.
1197 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01001198 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001199 }
1200
1201 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1202 // Bail out if the destination is null.
1203 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001204 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001205 }
1206
1207 // If the length is negative, bail out.
1208 // We have already checked in the LocationsBuilder for the constant case.
1209 if (!length.IsConstant() &&
1210 !optimizations.GetCountIsSourceLength() &&
1211 !optimizations.GetCountIsDestinationLength()) {
1212 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001213 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001214 }
1215
1216 // Validity checks: source.
1217 CheckPosition(assembler,
1218 src_pos,
1219 src,
1220 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001221 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001222 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001223 optimizations.GetCountIsSourceLength());
1224
1225 // Validity checks: dest.
1226 CheckPosition(assembler,
1227 dest_pos,
1228 dest,
1229 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001230 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001231 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001232 optimizations.GetCountIsDestinationLength());
1233
1234 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1235 // Check whether all elements of the source array are assignable to the component
1236 // type of the destination array. We do two checks: the classes are the same,
1237 // or the destination is Object[]. If none of these checks succeed, we go to the
1238 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01001239
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001240 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +01001241 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1242 // /* HeapReference<Class> */ temp1 = dest->klass_
1243 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1244 invoke, temp1_loc, dest, class_offset, temp3_loc, /* needs_null_check */ false);
1245 // Register `temp1` is not trashed by the read barrier emitted
1246 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1247 // method produces a call to a ReadBarrierMarkRegX entry point,
1248 // which saves all potentially live registers, including
1249 // temporaries such a `temp1`.
1250 // /* HeapReference<Class> */ temp2 = src->klass_
1251 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1252 invoke, temp2_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
1253 // If heap poisoning is enabled, `temp1` and `temp2` have been
1254 // unpoisoned by the the previous calls to
1255 // GenerateFieldLoadWithBakerReadBarrier.
1256 } else {
1257 // /* HeapReference<Class> */ temp1 = dest->klass_
1258 __ movl(temp1, Address(dest, class_offset));
1259 // /* HeapReference<Class> */ temp2 = src->klass_
1260 __ movl(temp2, Address(src, class_offset));
1261 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1262 !optimizations.GetSourceIsNonPrimitiveArray()) {
1263 // One or two of the references need to be unpoisoned. Unpoison them
1264 // both to make the identity check valid.
1265 __ MaybeUnpoisonHeapReference(temp1);
1266 __ MaybeUnpoisonHeapReference(temp2);
1267 did_unpoison = true;
1268 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001269 }
1270
1271 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1272 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001273 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1274 // /* HeapReference<Class> */ TMP = temp1->component_type_
1275 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1276 invoke, TMP_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
1277 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1278 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1279 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1280 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1281 } else {
1282 // /* HeapReference<Class> */ TMP = temp1->component_type_
1283 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1284 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1285 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1286 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1287 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001288 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001289 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001290 }
1291
1292 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1293 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001294 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1295 // For the same reason given earlier, `temp1` is not trashed by the
1296 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1297 // /* HeapReference<Class> */ TMP = temp2->component_type_
1298 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1299 invoke, TMP_loc, temp2, component_offset, temp3_loc, /* needs_null_check */ false);
1300 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1301 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1302 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1303 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1304 } else {
1305 // /* HeapReference<Class> */ TMP = temp2->component_type_
1306 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1307 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1308 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1309 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1310 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001311 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001312 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001313 }
1314
1315 __ cmpl(temp1, temp2);
1316
1317 if (optimizations.GetDestinationIsTypedObjectArray()) {
1318 NearLabel do_copy;
1319 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001320 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1321 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1322 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1323 invoke, temp1_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
1324 // We do not need to emit a read barrier for the following
1325 // heap reference load, as `temp1` is only used in a
1326 // comparison with null below, and this reference is not
1327 // kept afterwards.
1328 __ cmpl(Address(temp1, super_offset), Immediate(0));
1329 } else {
1330 if (!did_unpoison) {
1331 __ MaybeUnpoisonHeapReference(temp1);
1332 }
1333 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1334 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001335 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001336 // No need to unpoison the following heap reference load, as
1337 // we're comparing against null.
1338 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001339 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001340 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001341 __ Bind(&do_copy);
1342 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001343 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001344 }
1345 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1346 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1347 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001348 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1349 // /* HeapReference<Class> */ temp1 = src->klass_
1350 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1351 invoke, temp1_loc, src, class_offset, temp3_loc, /* needs_null_check */ false);
1352 // /* HeapReference<Class> */ TMP = temp1->component_type_
1353 codegen_->GenerateFieldLoadWithBakerReadBarrier(
1354 invoke, TMP_loc, temp1, component_offset, temp3_loc, /* needs_null_check */ false);
1355 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1356 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1357 } else {
1358 // /* HeapReference<Class> */ temp1 = src->klass_
1359 __ movl(temp1, Address(src, class_offset));
1360 __ MaybeUnpoisonHeapReference(temp1);
1361 // /* HeapReference<Class> */ TMP = temp1->component_type_
1362 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1363 // No need to unpoison `TMP` now, as we're comparing against null.
1364 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1365 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1366 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1367 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001368 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001369 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001370 }
1371
1372 // Compute base source address, base destination address, and end source address.
1373
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001374 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001375 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1376 if (src_pos.IsConstant()) {
1377 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1378 __ leal(temp1, Address(src, element_size * constant + offset));
1379 } else {
1380 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1381 }
1382
1383 if (dest_pos.IsConstant()) {
1384 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1385 __ leal(temp2, Address(dest, element_size * constant + offset));
1386 } else {
1387 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1388 }
1389
1390 if (length.IsConstant()) {
1391 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1392 __ leal(temp3, Address(temp1, element_size * constant));
1393 } else {
1394 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1395 }
1396
Roland Levillain0b671c02016-08-19 12:02:34 +01001397 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1398 // SystemArrayCopy implementation for Baker read barriers (see
1399 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1400 //
1401 // if (src_ptr != end_ptr) {
1402 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1403 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1404 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
1405 // if (is_gray) {
1406 // // Slow-path copy.
1407 // do {
1408 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1409 // } while (src_ptr != end_ptr)
1410 // } else {
1411 // // Fast-path copy.
1412 // do {
1413 // *dest_ptr++ = *src_ptr++;
1414 // } while (src_ptr != end_ptr)
1415 // }
1416 // }
1417
1418 NearLabel loop, done;
1419
1420 // Don't enter copy loop if `length == 0`.
1421 __ cmpl(temp1, temp3);
1422 __ j(kEqual, &done);
1423
1424 // /* int32_t */ monitor = src->monitor_
1425 __ movl(CpuRegister(TMP), Address(src, monitor_offset));
1426 // /* LockWord */ lock_word = LockWord(monitor)
1427 static_assert(sizeof(LockWord) == sizeof(int32_t),
1428 "art::LockWord and int32_t have different sizes.");
1429
1430 // Load fence to prevent load-load reordering.
1431 // Note that this is a no-op, thanks to the x86-64 memory model.
1432 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1433
1434 // Slow path used to copy array when `src` is gray.
1435 SlowPathCode* read_barrier_slow_path =
1436 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
1437 codegen_->AddSlowPath(read_barrier_slow_path);
1438
1439 // Given the numeric representation, it's enough to check the low bit of the
1440 // rb_state. We do that by shifting the bit out of the lock word with SHR.
1441 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
1442 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
1443 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
1444 __ shrl(CpuRegister(TMP), Immediate(LockWord::kReadBarrierStateShift + 1));
1445 __ j(kCarrySet, read_barrier_slow_path->GetEntryLabel());
1446
1447 // Fast-path copy.
1448 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1449 // poison/unpoison.
1450 __ Bind(&loop);
1451 __ movl(CpuRegister(TMP), Address(temp1, 0));
1452 __ movl(Address(temp2, 0), CpuRegister(TMP));
1453 __ addl(temp1, Immediate(element_size));
1454 __ addl(temp2, Immediate(element_size));
1455 __ cmpl(temp1, temp3);
1456 __ j(kNotEqual, &loop);
1457
1458 __ Bind(read_barrier_slow_path->GetExitLabel());
1459 __ Bind(&done);
1460 } else {
1461 // Non read barrier code.
1462
1463 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1464 // poison/unpoison.
1465 NearLabel loop, done;
1466 __ cmpl(temp1, temp3);
1467 __ j(kEqual, &done);
1468 __ Bind(&loop);
1469 __ movl(CpuRegister(TMP), Address(temp1, 0));
1470 __ movl(Address(temp2, 0), CpuRegister(TMP));
1471 __ addl(temp1, Immediate(element_size));
1472 __ addl(temp2, Immediate(element_size));
1473 __ cmpl(temp1, temp3);
1474 __ j(kNotEqual, &loop);
1475 __ Bind(&done);
1476 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001477
1478 // We only need one card marking on the destination array.
1479 codegen_->MarkGCCard(temp1,
1480 temp2,
1481 dest,
1482 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001483 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001484
Roland Levillain0b671c02016-08-19 12:02:34 +01001485 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001486}
1487
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001488void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1489 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001490 LocationSummary::kCallOnMainAndSlowPath,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001491 kIntrinsified);
1492 InvokeRuntimeCallingConvention calling_convention;
1493 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1494 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1495 locations->SetOut(Location::RegisterLocation(RAX));
1496}
1497
1498void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1499 X86_64Assembler* assembler = GetAssembler();
1500 LocationSummary* locations = invoke->GetLocations();
1501
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001502 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001503 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001504
1505 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1506 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001507 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001508 codegen_->AddSlowPath(slow_path);
1509 __ j(kEqual, slow_path->GetEntryLabel());
1510
Andreas Gampe542451c2016-07-26 09:02:02 -07001511 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize, pStringCompareTo),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001512 /* no_rip */ true));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001513 __ Bind(slow_path->GetExitLabel());
1514}
1515
Agi Csakif8cfb202015-08-13 17:54:54 -07001516void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1517 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1518 LocationSummary::kNoCall,
1519 kIntrinsified);
1520 locations->SetInAt(0, Location::RequiresRegister());
1521 locations->SetInAt(1, Location::RequiresRegister());
1522
1523 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1524 locations->AddTemp(Location::RegisterLocation(RCX));
1525 locations->AddTemp(Location::RegisterLocation(RDI));
1526
1527 // Set output, RSI needed for repe_cmpsq instruction anyways.
1528 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1529}
1530
1531void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1532 X86_64Assembler* assembler = GetAssembler();
1533 LocationSummary* locations = invoke->GetLocations();
1534
1535 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1536 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1537 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1538 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1539 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1540
Mark Mendell0c9497d2015-08-21 09:30:05 -04001541 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001542
1543 // Get offsets of count, value, and class fields within a string object.
1544 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1545 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1546 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1547
1548 // Note that the null check must have been done earlier.
1549 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1550
Vladimir Marko53b52002016-05-24 19:30:45 +01001551 StringEqualsOptimizations optimizations(invoke);
1552 if (!optimizations.GetArgumentNotNull()) {
1553 // Check if input is null, return false if it is.
1554 __ testl(arg, arg);
1555 __ j(kEqual, &return_false);
1556 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001557
Vladimir Marko53b52002016-05-24 19:30:45 +01001558 if (!optimizations.GetArgumentIsString()) {
1559 // Instanceof check for the argument by comparing class fields.
1560 // All string objects must have the same type since String cannot be subclassed.
1561 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1562 // If the argument is a string object, its class field must be equal to receiver's class field.
1563 __ movl(rcx, Address(str, class_offset));
1564 __ cmpl(rcx, Address(arg, class_offset));
1565 __ j(kNotEqual, &return_false);
1566 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001567
1568 // Reference equality check, return true if same reference.
1569 __ cmpl(str, arg);
1570 __ j(kEqual, &return_true);
1571
1572 // Load length of receiver string.
1573 __ movl(rcx, Address(str, count_offset));
1574 // Check if lengths are equal, return false if they're not.
1575 __ cmpl(rcx, Address(arg, count_offset));
1576 __ j(kNotEqual, &return_false);
1577 // Return true if both strings are empty.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001578 __ jrcxz(&return_true);
Agi Csakif8cfb202015-08-13 17:54:54 -07001579
1580 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1581 __ leal(rsi, Address(str, value_offset));
1582 __ leal(rdi, Address(arg, value_offset));
1583
1584 // Divide string length by 4 and adjust for lengths not divisible by 4.
1585 __ addl(rcx, Immediate(3));
1586 __ shrl(rcx, Immediate(2));
1587
1588 // Assertions that must hold in order to compare strings 4 characters at a time.
1589 DCHECK_ALIGNED(value_offset, 8);
1590 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1591
1592 // Loop to compare strings four characters at a time starting at the beginning of the string.
1593 __ repe_cmpsq();
1594 // If strings are not equal, zero flag will be cleared.
1595 __ j(kNotEqual, &return_false);
1596
1597 // Return true and exit the function.
1598 // If loop does not result in returning false, we return true.
1599 __ Bind(&return_true);
1600 __ movl(rsi, Immediate(1));
1601 __ jmp(&end);
1602
1603 // Return false and exit the function.
1604 __ Bind(&return_false);
1605 __ xorl(rsi, rsi);
1606 __ Bind(&end);
1607}
1608
Andreas Gampe21030dd2015-05-07 14:46:15 -07001609static void CreateStringIndexOfLocations(HInvoke* invoke,
1610 ArenaAllocator* allocator,
1611 bool start_at_zero) {
1612 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1613 LocationSummary::kCallOnSlowPath,
1614 kIntrinsified);
1615 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1616 locations->SetInAt(0, Location::RegisterLocation(RDI));
1617 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1618 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1619 // of the instruction explicitly.
1620 // Note: This works as we don't clobber RAX anywhere.
1621 locations->SetInAt(1, Location::RegisterLocation(RAX));
1622 if (!start_at_zero) {
1623 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1624 }
1625 // As we clobber RDI during execution anyways, also use it as the output.
1626 locations->SetOut(Location::SameAsFirstInput());
1627
1628 // repne scasw uses RCX as the counter.
1629 locations->AddTemp(Location::RegisterLocation(RCX));
1630 // Need another temporary to be able to compute the result.
1631 locations->AddTemp(Location::RequiresRegister());
1632}
1633
1634static void GenerateStringIndexOf(HInvoke* invoke,
1635 X86_64Assembler* assembler,
1636 CodeGeneratorX86_64* codegen,
1637 ArenaAllocator* allocator,
1638 bool start_at_zero) {
1639 LocationSummary* locations = invoke->GetLocations();
1640
1641 // Note that the null check must have been done earlier.
1642 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1643
1644 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1645 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1646 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1647 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1648 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1649
1650 // Check our assumptions for registers.
1651 DCHECK_EQ(string_obj.AsRegister(), RDI);
1652 DCHECK_EQ(search_value.AsRegister(), RAX);
1653 DCHECK_EQ(counter.AsRegister(), RCX);
1654 DCHECK_EQ(out.AsRegister(), RDI);
1655
1656 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001657 // 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 -07001658 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001659 HInstruction* code_point = invoke->InputAt(1);
1660 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001661 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001662 std::numeric_limits<uint16_t>::max()) {
1663 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1664 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1665 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1666 codegen->AddSlowPath(slow_path);
1667 __ jmp(slow_path->GetEntryLabel());
1668 __ Bind(slow_path->GetExitLabel());
1669 return;
1670 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001671 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001672 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1673 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1674 codegen->AddSlowPath(slow_path);
1675 __ j(kAbove, slow_path->GetEntryLabel());
1676 }
1677
1678 // From here down, we know that we are looking for a char that fits in 16 bits.
1679 // Location of reference to data array within the String object.
1680 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1681 // Location of count within the String object.
1682 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1683
1684 // Load string length, i.e., the count field of the string.
1685 __ movl(string_length, Address(string_obj, count_offset));
1686
1687 // Do a length check.
1688 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001689 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001690 __ testl(string_length, string_length);
1691 __ j(kEqual, &not_found_label);
1692
1693 if (start_at_zero) {
1694 // Number of chars to scan is the same as the string length.
1695 __ movl(counter, string_length);
1696
1697 // Move to the start of the string.
1698 __ addq(string_obj, Immediate(value_offset));
1699 } else {
1700 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1701
1702 // Do a start_index check.
1703 __ cmpl(start_index, string_length);
1704 __ j(kGreaterEqual, &not_found_label);
1705
1706 // Ensure we have a start index >= 0;
1707 __ xorl(counter, counter);
1708 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001709 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001710
1711 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1712 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1713
1714 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1715 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1716 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1717 }
1718
1719 // Everything is set up for repne scasw:
1720 // * Comparison address in RDI.
1721 // * Counter in ECX.
1722 __ repne_scasw();
1723
1724 // Did we find a match?
1725 __ j(kNotEqual, &not_found_label);
1726
1727 // Yes, we matched. Compute the index of the result.
1728 __ subl(string_length, counter);
1729 __ leal(out, Address(string_length, -1));
1730
Mark Mendell0c9497d2015-08-21 09:30:05 -04001731 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001732 __ jmp(&done);
1733
1734 // Failed to match; return -1.
1735 __ Bind(&not_found_label);
1736 __ movl(out, Immediate(-1));
1737
1738 // And join up at the end.
1739 __ Bind(&done);
1740 if (slow_path != nullptr) {
1741 __ Bind(slow_path->GetExitLabel());
1742 }
1743}
1744
1745void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001746 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001747}
1748
1749void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001750 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001751}
1752
1753void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001754 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001755}
1756
1757void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001758 GenerateStringIndexOf(
1759 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001760}
1761
Jeff Hao848f70a2014-01-15 13:49:50 -08001762void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1763 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001764 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001765 kIntrinsified);
1766 InvokeRuntimeCallingConvention calling_convention;
1767 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1768 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1769 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1770 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1771 locations->SetOut(Location::RegisterLocation(RAX));
1772}
1773
1774void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1775 X86_64Assembler* assembler = GetAssembler();
1776 LocationSummary* locations = invoke->GetLocations();
1777
1778 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1779 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001780 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001781 codegen_->AddSlowPath(slow_path);
1782 __ j(kEqual, slow_path->GetEntryLabel());
1783
Andreas Gampe542451c2016-07-26 09:02:02 -07001784 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize,
1785 pAllocStringFromBytes),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001786 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001787 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001788 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1789 __ Bind(slow_path->GetExitLabel());
1790}
1791
1792void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1793 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001794 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001795 kIntrinsified);
1796 InvokeRuntimeCallingConvention calling_convention;
1797 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1798 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1799 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1800 locations->SetOut(Location::RegisterLocation(RAX));
1801}
1802
1803void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1804 X86_64Assembler* assembler = GetAssembler();
1805
Roland Levillaincc3839c2016-02-29 16:23:48 +00001806 // No need to emit code checking whether `locations->InAt(2)` is a null
1807 // pointer, as callers of the native method
1808 //
1809 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1810 //
1811 // all include a null check on `data` before calling that method.
Andreas Gampe542451c2016-07-26 09:02:02 -07001812 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize,
1813 pAllocStringFromChars),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001814 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001815 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001816 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1817}
1818
1819void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1820 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001821 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001822 kIntrinsified);
1823 InvokeRuntimeCallingConvention calling_convention;
1824 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1825 locations->SetOut(Location::RegisterLocation(RAX));
1826}
1827
1828void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1829 X86_64Assembler* assembler = GetAssembler();
1830 LocationSummary* locations = invoke->GetLocations();
1831
1832 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1833 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001834 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001835 codegen_->AddSlowPath(slow_path);
1836 __ j(kEqual, slow_path->GetEntryLabel());
1837
Andreas Gampe542451c2016-07-26 09:02:02 -07001838 __ gs()->call(Address::Absolute(QUICK_ENTRYPOINT_OFFSET(kX86_64PointerSize,
1839 pAllocStringFromString),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001840 /* no_rip */ true));
Roland Levillainf969a202016-03-09 16:14:00 +00001841 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001842 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1843 __ Bind(slow_path->GetExitLabel());
1844}
1845
Mark Mendell8f8926a2015-08-17 11:39:06 -04001846void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1847 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1848 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1849 LocationSummary::kNoCall,
1850 kIntrinsified);
1851 locations->SetInAt(0, Location::RequiresRegister());
1852 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1853 locations->SetInAt(2, Location::RequiresRegister());
1854 locations->SetInAt(3, Location::RequiresRegister());
1855 locations->SetInAt(4, Location::RequiresRegister());
1856
1857 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1858 locations->AddTemp(Location::RegisterLocation(RSI));
1859 locations->AddTemp(Location::RegisterLocation(RDI));
1860 locations->AddTemp(Location::RegisterLocation(RCX));
1861}
1862
1863void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1864 X86_64Assembler* assembler = GetAssembler();
1865 LocationSummary* locations = invoke->GetLocations();
1866
1867 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1868 // Location of data in char array buffer.
1869 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1870 // Location of char array data in string.
1871 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1872
1873 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1874 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1875 Location srcBegin = locations->InAt(1);
1876 int srcBegin_value =
1877 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1878 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1879 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1880 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1881
1882 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1883 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1884 DCHECK_EQ(char_size, 2u);
1885
1886 // Compute the address of the destination buffer.
1887 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1888
1889 // Compute the address of the source string.
1890 if (srcBegin.IsConstant()) {
1891 // Compute the address of the source string by adding the number of chars from
1892 // the source beginning to the value offset of a string.
1893 __ leaq(CpuRegister(RSI), Address(obj, srcBegin_value * char_size + value_offset));
1894 } else {
1895 __ leaq(CpuRegister(RSI), Address(obj, srcBegin.AsRegister<CpuRegister>(),
1896 ScaleFactor::TIMES_2, value_offset));
1897 }
1898
1899 // Compute the number of chars (words) to move.
1900 __ movl(CpuRegister(RCX), srcEnd);
1901 if (srcBegin.IsConstant()) {
1902 if (srcBegin_value != 0) {
1903 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
1904 }
1905 } else {
1906 DCHECK(srcBegin.IsRegister());
1907 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1908 }
1909
1910 // Do the move.
1911 __ rep_movsw();
1912}
1913
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001914static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1915 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1916 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1917 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1918 // to avoid a SIGBUS.
1919 switch (size) {
1920 case Primitive::kPrimByte:
1921 __ movsxb(out, Address(address, 0));
1922 break;
1923 case Primitive::kPrimShort:
1924 __ movsxw(out, Address(address, 0));
1925 break;
1926 case Primitive::kPrimInt:
1927 __ movl(out, Address(address, 0));
1928 break;
1929 case Primitive::kPrimLong:
1930 __ movq(out, Address(address, 0));
1931 break;
1932 default:
1933 LOG(FATAL) << "Type not recognized for peek: " << size;
1934 UNREACHABLE();
1935 }
1936}
1937
1938void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1939 CreateIntToIntLocations(arena_, invoke);
1940}
1941
1942void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1943 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
1944}
1945
1946void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1947 CreateIntToIntLocations(arena_, invoke);
1948}
1949
1950void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
1951 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
1952}
1953
1954void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1955 CreateIntToIntLocations(arena_, invoke);
1956}
1957
1958void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
1959 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
1960}
1961
1962void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1963 CreateIntToIntLocations(arena_, invoke);
1964}
1965
1966void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
1967 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
1968}
1969
1970static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
1971 LocationSummary* locations = new (arena) LocationSummary(invoke,
1972 LocationSummary::kNoCall,
1973 kIntrinsified);
1974 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04001975 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001976}
1977
1978static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1979 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04001980 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001981 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1982 // to avoid a SIGBUS.
1983 switch (size) {
1984 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04001985 if (value.IsConstant()) {
1986 __ movb(Address(address, 0),
1987 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1988 } else {
1989 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
1990 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001991 break;
1992 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04001993 if (value.IsConstant()) {
1994 __ movw(Address(address, 0),
1995 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
1996 } else {
1997 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
1998 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001999 break;
2000 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04002001 if (value.IsConstant()) {
2002 __ movl(Address(address, 0),
2003 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2004 } else {
2005 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
2006 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002007 break;
2008 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04002009 if (value.IsConstant()) {
2010 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
2011 DCHECK(IsInt<32>(v));
2012 int32_t v_32 = v;
2013 __ movq(Address(address, 0), Immediate(v_32));
2014 } else {
2015 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
2016 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002017 break;
2018 default:
2019 LOG(FATAL) << "Type not recognized for poke: " << size;
2020 UNREACHABLE();
2021 }
2022}
2023
2024void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2025 CreateIntIntToVoidLocations(arena_, invoke);
2026}
2027
2028void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2029 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
2030}
2031
2032void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2033 CreateIntIntToVoidLocations(arena_, invoke);
2034}
2035
2036void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2037 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2038}
2039
2040void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2041 CreateIntIntToVoidLocations(arena_, invoke);
2042}
2043
2044void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2045 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2046}
2047
2048void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2049 CreateIntIntToVoidLocations(arena_, invoke);
2050}
2051
2052void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2053 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
2054}
2055
2056void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2057 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2058 LocationSummary::kNoCall,
2059 kIntrinsified);
2060 locations->SetOut(Location::RequiresRegister());
2061}
2062
2063void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2064 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07002065 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002066 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002067}
2068
Roland Levillain0d5a2812015-11-13 10:07:31 +00002069static void GenUnsafeGet(HInvoke* invoke,
2070 Primitive::Type type,
2071 bool is_volatile ATTRIBUTE_UNUSED,
2072 CodeGeneratorX86_64* codegen) {
2073 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
2074 LocationSummary* locations = invoke->GetLocations();
2075 Location base_loc = locations->InAt(1);
2076 CpuRegister base = base_loc.AsRegister<CpuRegister>();
2077 Location offset_loc = locations->InAt(2);
2078 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
2079 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002080 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002081
Andreas Gampe878d58c2015-01-15 23:24:00 -08002082 switch (type) {
2083 case Primitive::kPrimInt:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002084 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002085 break;
2086
2087 case Primitive::kPrimNot: {
2088 if (kEmitCompilerReadBarrier) {
2089 if (kUseBakerReadBarrier) {
2090 Location temp = locations->GetTemp(0);
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002091 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2092 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2093 invoke, output_loc, base, src, temp, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002094 } else {
2095 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2096 codegen->GenerateReadBarrierSlow(
2097 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2098 }
2099 } else {
2100 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2101 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002102 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002103 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002104 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002105
2106 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002107 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002108 break;
2109
2110 default:
2111 LOG(FATAL) << "Unsupported op size " << type;
2112 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002113 }
2114}
2115
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002116static void CreateIntIntIntToIntLocations(ArenaAllocator* arena,
2117 HInvoke* invoke,
2118 Primitive::Type type) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002119 bool can_call = kEmitCompilerReadBarrier &&
2120 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2121 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002122 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillain0d5a2812015-11-13 10:07:31 +00002123 can_call ?
2124 LocationSummary::kCallOnSlowPath :
2125 LocationSummary::kNoCall,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002126 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002127 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002128 locations->SetInAt(1, Location::RequiresRegister());
2129 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002130 locations->SetOut(Location::RequiresRegister(),
2131 can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002132 if (type == Primitive::kPrimNot && kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2133 // We need a temporary register for the read barrier marking slow
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002134 // path in InstructionCodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002135 locations->AddTemp(Location::RequiresRegister());
2136 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002137}
2138
2139void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002140 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002141}
2142void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002143 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimInt);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002144}
2145void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002146 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002147}
2148void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002149 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimLong);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002150}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002151void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002152 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002153}
2154void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002155 CreateIntIntIntToIntLocations(arena_, invoke, Primitive::kPrimNot);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002156}
2157
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002158
2159void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002160 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002161}
2162void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002163 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002164}
2165void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002166 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002167}
2168void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002169 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002170}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002171void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002172 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002173}
2174void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002175 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002176}
2177
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002178
2179static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
2180 Primitive::Type type,
2181 HInvoke* invoke) {
2182 LocationSummary* locations = new (arena) LocationSummary(invoke,
2183 LocationSummary::kNoCall,
2184 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002185 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002186 locations->SetInAt(1, Location::RequiresRegister());
2187 locations->SetInAt(2, Location::RequiresRegister());
2188 locations->SetInAt(3, Location::RequiresRegister());
2189 if (type == Primitive::kPrimNot) {
2190 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002191 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002192 locations->AddTemp(Location::RequiresRegister());
2193 }
2194}
2195
2196void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2197 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2198}
2199void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2200 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2201}
2202void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2203 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2204}
2205void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2206 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2207}
2208void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2209 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2210}
2211void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2212 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2213}
2214void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2215 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2216}
2217void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2218 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2219}
2220void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2221 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2222}
2223
2224// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2225// memory model.
2226static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2227 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002228 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002229 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2230 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2231 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2232
2233 if (type == Primitive::kPrimLong) {
2234 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002235 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2236 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2237 __ movl(temp, value);
2238 __ PoisonHeapReference(temp);
2239 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002240 } else {
2241 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2242 }
2243
2244 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002245 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002246 }
2247
2248 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002249 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002250 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2251 locations->GetTemp(1).AsRegister<CpuRegister>(),
2252 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002253 value,
2254 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002255 }
2256}
2257
2258void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002259 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002260}
2261void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002262 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002263}
2264void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002265 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002266}
2267void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002268 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002269}
2270void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002271 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002272}
2273void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002274 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002275}
2276void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002277 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002278}
2279void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002280 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002281}
2282void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002283 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002284}
2285
Mark Mendell58d25fd2015-04-03 14:52:31 -04002286static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena, Primitive::Type type,
2287 HInvoke* invoke) {
2288 LocationSummary* locations = new (arena) LocationSummary(invoke,
2289 LocationSummary::kNoCall,
2290 kIntrinsified);
2291 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2292 locations->SetInAt(1, Location::RequiresRegister());
2293 locations->SetInAt(2, Location::RequiresRegister());
2294 // expected value must be in EAX/RAX.
2295 locations->SetInAt(3, Location::RegisterLocation(RAX));
2296 locations->SetInAt(4, Location::RequiresRegister());
2297
2298 locations->SetOut(Location::RequiresRegister());
2299 if (type == Primitive::kPrimNot) {
2300 // Need temp registers for card-marking.
Roland Levillainb488b782015-10-22 11:38:49 +01002301 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002302 locations->AddTemp(Location::RequiresRegister());
2303 }
2304}
2305
2306void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2307 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2308}
2309
2310void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2311 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2312}
2313
2314void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain391b8662015-12-18 11:43:38 +00002315 // The UnsafeCASObject intrinsic is missing a read barrier, and
2316 // therefore sometimes does not work as expected (b/25883050).
2317 // Turn it off temporarily as a quick fix, until the read barrier is
Roland Levillain3d312422016-06-23 13:53:42 +01002318 // implemented (see TODO in GenCAS).
Roland Levillain391b8662015-12-18 11:43:38 +00002319 //
Roland Levillain3d312422016-06-23 13:53:42 +01002320 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
Roland Levillain391b8662015-12-18 11:43:38 +00002321 // this intrinsic.
2322 if (kEmitCompilerReadBarrier) {
2323 return;
2324 }
2325
Mark Mendell58d25fd2015-04-03 14:52:31 -04002326 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2327}
2328
2329static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002330 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002331 LocationSummary* locations = invoke->GetLocations();
2332
2333 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2334 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2335 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002336 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002337 DCHECK_EQ(expected.AsRegister(), RAX);
2338 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
2339 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2340
Roland Levillainb488b782015-10-22 11:38:49 +01002341 if (type == Primitive::kPrimNot) {
2342 // Mark card for object assuming new value is stored.
2343 bool value_can_be_null = true; // TODO: Worth finding out this information?
2344 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2345 locations->GetTemp(1).AsRegister<CpuRegister>(),
2346 base,
2347 value,
2348 value_can_be_null);
Roland Levillain4d027112015-07-01 15:41:14 +01002349
Roland Levillainb488b782015-10-22 11:38:49 +01002350 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2351 Register value_reg = value.AsRegister();
2352 if (kPoisonHeapReferences) {
2353 if (base_equals_value) {
2354 // If `base` and `value` are the same register location, move
2355 // `value_reg` to a temporary register. This way, poisoning
2356 // `value_reg` won't invalidate `base`.
2357 value_reg = locations->GetTemp(0).AsRegister<CpuRegister>().AsRegister();
2358 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002359 }
Roland Levillainb488b782015-10-22 11:38:49 +01002360
2361 // Check that the register allocator did not assign the location
2362 // of `expected` (RAX) to `value` nor to `base`, so that heap
2363 // poisoning (when enabled) works as intended below.
2364 // - If `value` were equal to `expected`, both references would
2365 // be poisoned twice, meaning they would not be poisoned at
2366 // all, as heap poisoning uses address negation.
2367 // - If `base` were equal to `expected`, poisoning `expected`
2368 // would invalidate `base`.
2369 DCHECK_NE(value_reg, expected.AsRegister());
2370 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2371
2372 __ PoisonHeapReference(expected);
2373 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002374 }
2375
Roland Levillain391b8662015-12-18 11:43:38 +00002376 // TODO: Add a read barrier for the reference stored in the object
2377 // before attempting the CAS, similar to the one in the
2378 // art::Unsafe_compareAndSwapObject JNI implementation.
2379 //
2380 // Note that this code is not (yet) used when read barriers are
2381 // enabled (see IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject).
2382 DCHECK(!kEmitCompilerReadBarrier);
Roland Levillainb488b782015-10-22 11:38:49 +01002383 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002384
Roland Levillain0d5a2812015-11-13 10:07:31 +00002385 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002386 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002387
Roland Levillainb488b782015-10-22 11:38:49 +01002388 // Convert ZF into the boolean result.
2389 __ setcc(kZero, out);
2390 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002391
Roland Levillain391b8662015-12-18 11:43:38 +00002392 // If heap poisoning is enabled, we need to unpoison the values
2393 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002394 if (kPoisonHeapReferences) {
2395 if (base_equals_value) {
2396 // `value_reg` has been moved to a temporary register, no need
2397 // to unpoison it.
2398 } else {
2399 // Ensure `value` is different from `out`, so that unpoisoning
2400 // the former does not invalidate the latter.
2401 DCHECK_NE(value_reg, out.AsRegister());
2402 __ UnpoisonHeapReference(CpuRegister(value_reg));
2403 }
2404 // Ensure `expected` is different from `out`, so that unpoisoning
2405 // the former does not invalidate the latter.
2406 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2407 __ UnpoisonHeapReference(expected);
2408 }
2409 } else {
2410 if (type == Primitive::kPrimInt) {
2411 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2412 } else if (type == Primitive::kPrimLong) {
2413 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2414 } else {
2415 LOG(FATAL) << "Unexpected CAS type " << type;
2416 }
2417
Roland Levillain0d5a2812015-11-13 10:07:31 +00002418 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002419 // scheduling barriers at this time.
2420
2421 // Convert ZF into the boolean result.
2422 __ setcc(kZero, out);
2423 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002424 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002425}
2426
2427void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2428 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2429}
2430
2431void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2432 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2433}
2434
2435void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillain3d312422016-06-23 13:53:42 +01002436 // The UnsafeCASObject intrinsic is missing a read barrier, and
2437 // therefore sometimes does not work as expected (b/25883050).
2438 // Turn it off temporarily as a quick fix, until the read barrier is
2439 // implemented (see TODO in GenCAS).
2440 //
2441 // TODO(rpl): Implement read barrier support in GenCAS and re-enable
2442 // this intrinsic.
2443 DCHECK(!kEmitCompilerReadBarrier);
2444
Mark Mendell58d25fd2015-04-03 14:52:31 -04002445 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2446}
2447
2448void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2449 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2450 LocationSummary::kNoCall,
2451 kIntrinsified);
2452 locations->SetInAt(0, Location::RequiresRegister());
2453 locations->SetOut(Location::SameAsFirstInput());
2454 locations->AddTemp(Location::RequiresRegister());
2455}
2456
2457static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2458 X86_64Assembler* assembler) {
2459 Immediate imm_shift(shift);
2460 Immediate imm_mask(mask);
2461 __ movl(temp, reg);
2462 __ shrl(reg, imm_shift);
2463 __ andl(temp, imm_mask);
2464 __ andl(reg, imm_mask);
2465 __ shll(temp, imm_shift);
2466 __ orl(reg, temp);
2467}
2468
2469void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002470 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002471 LocationSummary* locations = invoke->GetLocations();
2472
2473 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2474 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2475
2476 /*
2477 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2478 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2479 * compared to generic luni implementation which has 5 rounds of swapping bits.
2480 * x = bswap x
2481 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2482 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2483 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2484 */
2485 __ bswapl(reg);
2486 SwapBits(reg, temp, 1, 0x55555555, assembler);
2487 SwapBits(reg, temp, 2, 0x33333333, assembler);
2488 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2489}
2490
2491void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2492 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2493 LocationSummary::kNoCall,
2494 kIntrinsified);
2495 locations->SetInAt(0, Location::RequiresRegister());
2496 locations->SetOut(Location::SameAsFirstInput());
2497 locations->AddTemp(Location::RequiresRegister());
2498 locations->AddTemp(Location::RequiresRegister());
2499}
2500
2501static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2502 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2503 Immediate imm_shift(shift);
2504 __ movq(temp_mask, Immediate(mask));
2505 __ movq(temp, reg);
2506 __ shrq(reg, imm_shift);
2507 __ andq(temp, temp_mask);
2508 __ andq(reg, temp_mask);
2509 __ shlq(temp, imm_shift);
2510 __ orq(reg, temp);
2511}
2512
2513void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002514 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002515 LocationSummary* locations = invoke->GetLocations();
2516
2517 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2518 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2519 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2520
2521 /*
2522 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2523 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2524 * compared to generic luni implementation which has 5 rounds of swapping bits.
2525 * x = bswap x
2526 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2527 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2528 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2529 */
2530 __ bswapq(reg);
2531 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2532 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2533 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2534}
2535
Aart Bik3f67e692016-01-15 14:35:12 -08002536static void CreateBitCountLocations(
2537 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2538 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2539 // Do nothing if there is no popcnt support. This results in generating
2540 // a call for the intrinsic rather than direct code.
2541 return;
2542 }
2543 LocationSummary* locations = new (arena) LocationSummary(invoke,
2544 LocationSummary::kNoCall,
2545 kIntrinsified);
2546 locations->SetInAt(0, Location::Any());
2547 locations->SetOut(Location::RequiresRegister());
2548}
2549
Aart Bikc5d47542016-01-27 17:00:35 -08002550static void GenBitCount(X86_64Assembler* assembler,
2551 CodeGeneratorX86_64* codegen,
2552 HInvoke* invoke,
2553 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002554 LocationSummary* locations = invoke->GetLocations();
2555 Location src = locations->InAt(0);
2556 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2557
2558 if (invoke->InputAt(0)->IsConstant()) {
2559 // Evaluate this at compile time.
2560 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002561 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002562 ? POPCOUNT(static_cast<uint64_t>(value))
2563 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002564 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002565 return;
2566 }
2567
2568 if (src.IsRegister()) {
2569 if (is_long) {
2570 __ popcntq(out, src.AsRegister<CpuRegister>());
2571 } else {
2572 __ popcntl(out, src.AsRegister<CpuRegister>());
2573 }
2574 } else if (is_long) {
2575 DCHECK(src.IsDoubleStackSlot());
2576 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2577 } else {
2578 DCHECK(src.IsStackSlot());
2579 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2580 }
2581}
2582
2583void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2584 CreateBitCountLocations(arena_, codegen_, invoke);
2585}
2586
2587void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002588 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002589}
2590
2591void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2592 CreateBitCountLocations(arena_, codegen_, invoke);
2593}
2594
2595void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002596 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2597}
2598
Aart Bikc5d47542016-01-27 17:00:35 -08002599static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2600 LocationSummary* locations = new (arena) LocationSummary(invoke,
2601 LocationSummary::kNoCall,
2602 kIntrinsified);
2603 locations->SetInAt(0, Location::Any());
2604 locations->SetOut(Location::RequiresRegister());
2605 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2606 : Location::RequiresRegister()); // any will do
2607}
2608
2609static void GenOneBit(X86_64Assembler* assembler,
2610 CodeGeneratorX86_64* codegen,
2611 HInvoke* invoke,
2612 bool is_high, bool is_long) {
2613 LocationSummary* locations = invoke->GetLocations();
2614 Location src = locations->InAt(0);
2615 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2616
2617 if (invoke->InputAt(0)->IsConstant()) {
2618 // Evaluate this at compile time.
2619 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2620 if (value == 0) {
2621 __ xorl(out, out); // Clears upper bits too.
2622 return;
2623 }
2624 // Nonzero value.
2625 if (is_high) {
2626 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2627 : 31 - CLZ(static_cast<uint32_t>(value));
2628 } else {
2629 value = is_long ? CTZ(static_cast<uint64_t>(value))
2630 : CTZ(static_cast<uint32_t>(value));
2631 }
2632 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002633 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002634 } else {
2635 codegen->Load32BitValue(out, 1 << value);
2636 }
2637 return;
2638 }
2639
2640 // Handle the non-constant cases.
2641 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2642 if (is_high) {
2643 // Use architectural support: basically 1 << bsr.
2644 if (src.IsRegister()) {
2645 if (is_long) {
2646 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2647 } else {
2648 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2649 }
2650 } else if (is_long) {
2651 DCHECK(src.IsDoubleStackSlot());
2652 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2653 } else {
2654 DCHECK(src.IsStackSlot());
2655 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2656 }
2657 // BSR sets ZF if the input was zero.
2658 NearLabel is_zero, done;
2659 __ j(kEqual, &is_zero);
2660 __ movl(out, Immediate(1)); // Clears upper bits too.
2661 if (is_long) {
2662 __ shlq(out, tmp);
2663 } else {
2664 __ shll(out, tmp);
2665 }
2666 __ jmp(&done);
2667 __ Bind(&is_zero);
2668 __ xorl(out, out); // Clears upper bits too.
2669 __ Bind(&done);
2670 } else {
2671 // Copy input into temporary.
2672 if (src.IsRegister()) {
2673 if (is_long) {
2674 __ movq(tmp, src.AsRegister<CpuRegister>());
2675 } else {
2676 __ movl(tmp, src.AsRegister<CpuRegister>());
2677 }
2678 } else if (is_long) {
2679 DCHECK(src.IsDoubleStackSlot());
2680 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2681 } else {
2682 DCHECK(src.IsStackSlot());
2683 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2684 }
2685 // Do the bit twiddling: basically tmp & -tmp;
2686 if (is_long) {
2687 __ movq(out, tmp);
2688 __ negq(tmp);
2689 __ andq(out, tmp);
2690 } else {
2691 __ movl(out, tmp);
2692 __ negl(tmp);
2693 __ andl(out, tmp);
2694 }
2695 }
2696}
2697
2698void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2699 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2700}
2701
2702void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2703 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2704}
2705
2706void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2707 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2708}
2709
2710void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2711 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2712}
2713
2714void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2715 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2716}
2717
2718void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2719 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2720}
2721
2722void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2723 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2724}
2725
2726void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2727 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002728}
2729
Mark Mendelld5897672015-08-12 21:16:41 -04002730static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2731 LocationSummary* locations = new (arena) LocationSummary(invoke,
2732 LocationSummary::kNoCall,
2733 kIntrinsified);
2734 locations->SetInAt(0, Location::Any());
2735 locations->SetOut(Location::RequiresRegister());
2736}
2737
Aart Bikc5d47542016-01-27 17:00:35 -08002738static void GenLeadingZeros(X86_64Assembler* assembler,
2739 CodeGeneratorX86_64* codegen,
2740 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002741 LocationSummary* locations = invoke->GetLocations();
2742 Location src = locations->InAt(0);
2743 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2744
2745 int zero_value_result = is_long ? 64 : 32;
2746 if (invoke->InputAt(0)->IsConstant()) {
2747 // Evaluate this at compile time.
2748 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2749 if (value == 0) {
2750 value = zero_value_result;
2751 } else {
2752 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2753 }
Aart Bikc5d47542016-01-27 17:00:35 -08002754 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002755 return;
2756 }
2757
2758 // Handle the non-constant cases.
2759 if (src.IsRegister()) {
2760 if (is_long) {
2761 __ bsrq(out, src.AsRegister<CpuRegister>());
2762 } else {
2763 __ bsrl(out, src.AsRegister<CpuRegister>());
2764 }
2765 } else if (is_long) {
2766 DCHECK(src.IsDoubleStackSlot());
2767 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2768 } else {
2769 DCHECK(src.IsStackSlot());
2770 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2771 }
2772
2773 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002774 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002775 __ j(kEqual, &is_zero);
2776
2777 // Correct the result from BSR to get the CLZ result.
2778 __ xorl(out, Immediate(zero_value_result - 1));
2779 __ jmp(&done);
2780
2781 // Fix the zero case with the expected result.
2782 __ Bind(&is_zero);
2783 __ movl(out, Immediate(zero_value_result));
2784
2785 __ Bind(&done);
2786}
2787
2788void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2789 CreateLeadingZeroLocations(arena_, invoke);
2790}
2791
2792void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002793 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002794}
2795
2796void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2797 CreateLeadingZeroLocations(arena_, invoke);
2798}
2799
2800void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002801 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002802}
2803
Mark Mendell2d554792015-09-15 21:45:18 -04002804static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2805 LocationSummary* locations = new (arena) LocationSummary(invoke,
2806 LocationSummary::kNoCall,
2807 kIntrinsified);
2808 locations->SetInAt(0, Location::Any());
2809 locations->SetOut(Location::RequiresRegister());
2810}
2811
Aart Bikc5d47542016-01-27 17:00:35 -08002812static void GenTrailingZeros(X86_64Assembler* assembler,
2813 CodeGeneratorX86_64* codegen,
2814 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002815 LocationSummary* locations = invoke->GetLocations();
2816 Location src = locations->InAt(0);
2817 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2818
2819 int zero_value_result = is_long ? 64 : 32;
2820 if (invoke->InputAt(0)->IsConstant()) {
2821 // Evaluate this at compile time.
2822 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2823 if (value == 0) {
2824 value = zero_value_result;
2825 } else {
2826 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2827 }
Aart Bikc5d47542016-01-27 17:00:35 -08002828 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002829 return;
2830 }
2831
2832 // Handle the non-constant cases.
2833 if (src.IsRegister()) {
2834 if (is_long) {
2835 __ bsfq(out, src.AsRegister<CpuRegister>());
2836 } else {
2837 __ bsfl(out, src.AsRegister<CpuRegister>());
2838 }
2839 } else if (is_long) {
2840 DCHECK(src.IsDoubleStackSlot());
2841 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2842 } else {
2843 DCHECK(src.IsStackSlot());
2844 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2845 }
2846
2847 // BSF sets ZF if the input was zero, and the output is undefined.
2848 NearLabel done;
2849 __ j(kNotEqual, &done);
2850
2851 // Fix the zero case with the expected result.
2852 __ movl(out, Immediate(zero_value_result));
2853
2854 __ Bind(&done);
2855}
2856
2857void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2858 CreateTrailingZeroLocations(arena_, invoke);
2859}
2860
2861void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002862 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002863}
2864
2865void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2866 CreateTrailingZeroLocations(arena_, invoke);
2867}
2868
2869void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002870 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2871}
2872
Serguei Katkov288c7a82016-05-16 11:53:15 +06002873void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2874 if (kEmitCompilerReadBarrier) {
2875 // Do not intrinsify this call with the read barrier configuration.
2876 return;
2877 }
2878 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2879 LocationSummary::kCallOnSlowPath,
2880 kIntrinsified);
2881 locations->SetInAt(0, Location::RequiresRegister());
2882 locations->SetOut(Location::SameAsFirstInput());
2883 locations->AddTemp(Location::RequiresRegister());
2884}
2885
2886void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2887 DCHECK(!kEmitCompilerReadBarrier);
2888 LocationSummary* locations = invoke->GetLocations();
2889 X86_64Assembler* assembler = GetAssembler();
2890
2891 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2892 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2893
2894 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2895 codegen_->AddSlowPath(slow_path);
2896
2897 // Load ArtMethod first.
2898 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2899 DCHECK(invoke_direct != nullptr);
2900 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2901 invoke_direct, locations->GetTemp(0));
2902 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2903 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2904
2905 // Now get declaring class.
2906 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2907
2908 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2909 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2910 DCHECK_NE(slow_path_flag_offset, 0u);
2911 DCHECK_NE(disable_flag_offset, 0u);
2912 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2913
2914 // Check static flags preventing us for using intrinsic.
2915 if (slow_path_flag_offset == disable_flag_offset + 1) {
2916 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2917 __ j(kNotEqual, slow_path->GetEntryLabel());
2918 } else {
2919 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2920 __ j(kNotEqual, slow_path->GetEntryLabel());
2921 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2922 __ j(kNotEqual, slow_path->GetEntryLabel());
2923 }
2924
2925 // Fast path.
2926 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2927 codegen_->MaybeRecordImplicitNullCheck(invoke);
2928 __ MaybeUnpoisonHeapReference(out);
2929 __ Bind(slow_path->GetExitLabel());
2930}
2931
Aart Bik2f9fcc92016-03-01 15:16:54 -08002932UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2933UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002934
Aart Bik0e54c012016-03-04 12:08:31 -08002935// 1.8.
2936UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
2937UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
2938UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
2939UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
2940UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002941
Aart Bik2f9fcc92016-03-01 15:16:54 -08002942UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01002943
2944#undef __
2945
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002946} // namespace x86_64
2947} // namespace art