blob: 05d270a4e6cb1fc5c7b9fc030dd8ba6a91041e11 [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());
Mark Mendella4f12202015-08-06 15:23:34 -0400769
Serban Constantinescuba45db02016-07-12 22:53:02 +0100770 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400771}
772
773void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
774 CreateFPToFPCallLocations(arena_, invoke);
775}
776
777void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
778 GenFPToFPCall(invoke, codegen_, kQuickCos);
779}
780
781void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
782 CreateFPToFPCallLocations(arena_, invoke);
783}
784
785void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
786 GenFPToFPCall(invoke, codegen_, kQuickSin);
787}
788
789void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
790 CreateFPToFPCallLocations(arena_, invoke);
791}
792
793void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
794 GenFPToFPCall(invoke, codegen_, kQuickAcos);
795}
796
797void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
798 CreateFPToFPCallLocations(arena_, invoke);
799}
800
801void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
802 GenFPToFPCall(invoke, codegen_, kQuickAsin);
803}
804
805void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
806 CreateFPToFPCallLocations(arena_, invoke);
807}
808
809void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
810 GenFPToFPCall(invoke, codegen_, kQuickAtan);
811}
812
813void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
814 CreateFPToFPCallLocations(arena_, invoke);
815}
816
817void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
818 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
819}
820
821void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
822 CreateFPToFPCallLocations(arena_, invoke);
823}
824
825void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
826 GenFPToFPCall(invoke, codegen_, kQuickCosh);
827}
828
829void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
830 CreateFPToFPCallLocations(arena_, invoke);
831}
832
833void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
834 GenFPToFPCall(invoke, codegen_, kQuickExp);
835}
836
837void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
838 CreateFPToFPCallLocations(arena_, invoke);
839}
840
841void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
842 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
843}
844
845void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
846 CreateFPToFPCallLocations(arena_, invoke);
847}
848
849void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
850 GenFPToFPCall(invoke, codegen_, kQuickLog);
851}
852
853void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
854 CreateFPToFPCallLocations(arena_, invoke);
855}
856
857void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
858 GenFPToFPCall(invoke, codegen_, kQuickLog10);
859}
860
861void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
862 CreateFPToFPCallLocations(arena_, invoke);
863}
864
865void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
866 GenFPToFPCall(invoke, codegen_, kQuickSinh);
867}
868
869void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
870 CreateFPToFPCallLocations(arena_, invoke);
871}
872
873void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
874 GenFPToFPCall(invoke, codegen_, kQuickTan);
875}
876
877void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
878 CreateFPToFPCallLocations(arena_, invoke);
879}
880
881void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
882 GenFPToFPCall(invoke, codegen_, kQuickTanh);
883}
884
885static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
886 HInvoke* invoke) {
887 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100888 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400889 kIntrinsified);
890 InvokeRuntimeCallingConvention calling_convention;
891 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
892 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
893 locations->SetOut(Location::FpuRegisterLocation(XMM0));
894
895 // We have to ensure that the native code doesn't clobber the XMM registers which are
896 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
897 // saved in the prologue and properly restored.
898 for (auto fp_reg : non_volatile_xmm_regs) {
899 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
900 }
901}
902
903void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
904 CreateFPFPToFPCallLocations(arena_, invoke);
905}
906
907void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
908 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
909}
910
911void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
912 CreateFPFPToFPCallLocations(arena_, invoke);
913}
914
915void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
916 GenFPToFPCall(invoke, codegen_, kQuickHypot);
917}
918
919void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
920 CreateFPFPToFPCallLocations(arena_, invoke);
921}
922
923void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
924 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
925}
926
Mark Mendell6bc53a92015-07-01 14:26:52 -0400927void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
928 // Check to see if we have known failures that will cause us to have to bail out
929 // to the runtime, and just generate the runtime call directly.
930 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
931 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
932
933 // The positions must be non-negative.
934 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
935 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
936 // We will have to fail anyways.
937 return;
938 }
939
940 // The length must be > 0.
941 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
942 if (length != nullptr) {
943 int32_t len = length->GetValue();
944 if (len < 0) {
945 // Just call as normal.
946 return;
947 }
948 }
949
950 LocationSummary* locations = new (arena_) LocationSummary(invoke,
951 LocationSummary::kCallOnSlowPath,
952 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100953 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400954 locations->SetInAt(0, Location::RequiresRegister());
955 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
956 locations->SetInAt(2, Location::RequiresRegister());
957 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
958 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
959
960 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
961 locations->AddTemp(Location::RegisterLocation(RSI));
962 locations->AddTemp(Location::RegisterLocation(RDI));
963 locations->AddTemp(Location::RegisterLocation(RCX));
964}
965
966static void CheckPosition(X86_64Assembler* assembler,
967 Location pos,
968 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100969 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700970 SlowPathCode* slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100971 CpuRegister temp,
972 bool length_is_input_length = false) {
973 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400974 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
975
976 if (pos.IsConstant()) {
977 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
978 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100979 if (!length_is_input_length) {
980 // Check that length(input) >= length.
981 if (length.IsConstant()) {
982 __ cmpl(Address(input, length_offset),
983 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
984 } else {
985 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
986 }
987 __ j(kLess, slow_path->GetEntryLabel());
988 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400989 } else {
990 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100991 __ movl(temp, Address(input, length_offset));
992 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400993 __ j(kLess, slow_path->GetEntryLabel());
994
995 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100996 if (length.IsConstant()) {
997 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
998 } else {
999 __ cmpl(temp, length.AsRegister<CpuRegister>());
1000 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001001 __ j(kLess, slow_path->GetEntryLabel());
1002 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001003 } else if (length_is_input_length) {
1004 // The only way the copy can succeed is if pos is zero.
1005 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1006 __ testl(pos_reg, pos_reg);
1007 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001008 } else {
1009 // Check that pos >= 0.
1010 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1011 __ testl(pos_reg, pos_reg);
1012 __ j(kLess, slow_path->GetEntryLabel());
1013
1014 // Check that pos <= length(input).
1015 __ cmpl(Address(input, length_offset), pos_reg);
1016 __ j(kLess, slow_path->GetEntryLabel());
1017
1018 // Check that (length(input) - pos) >= length.
1019 __ movl(temp, Address(input, length_offset));
1020 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001021 if (length.IsConstant()) {
1022 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1023 } else {
1024 __ cmpl(temp, length.AsRegister<CpuRegister>());
1025 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001026 __ j(kLess, slow_path->GetEntryLabel());
1027 }
1028}
1029
1030void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1031 X86_64Assembler* assembler = GetAssembler();
1032 LocationSummary* locations = invoke->GetLocations();
1033
1034 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001035 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001036 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001037 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001038 Location length = locations->InAt(4);
1039
1040 // Temporaries that we need for MOVSW.
1041 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1042 DCHECK_EQ(src_base.AsRegister(), RSI);
1043 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1044 DCHECK_EQ(dest_base.AsRegister(), RDI);
1045 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1046 DCHECK_EQ(count.AsRegister(), RCX);
1047
Andreas Gampe85b62f22015-09-09 13:15:38 -07001048 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001049 codegen_->AddSlowPath(slow_path);
1050
1051 // Bail out if the source and destination are the same.
1052 __ cmpl(src, dest);
1053 __ j(kEqual, slow_path->GetEntryLabel());
1054
1055 // Bail out if the source is null.
1056 __ testl(src, src);
1057 __ j(kEqual, slow_path->GetEntryLabel());
1058
1059 // Bail out if the destination is null.
1060 __ testl(dest, dest);
1061 __ j(kEqual, slow_path->GetEntryLabel());
1062
1063 // If the length is negative, bail out.
1064 // We have already checked in the LocationsBuilder for the constant case.
1065 if (!length.IsConstant()) {
1066 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1067 __ j(kLess, slow_path->GetEntryLabel());
1068 }
1069
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001070 // Validity checks: source. Use src_base as a temporary register.
1071 CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001072
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001073 // Validity checks: dest. Use src_base as a temporary register.
1074 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001075
Mark Mendell6bc53a92015-07-01 14:26:52 -04001076 // We need the count in RCX.
1077 if (length.IsConstant()) {
1078 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1079 } else {
1080 __ movl(count, length.AsRegister<CpuRegister>());
1081 }
1082
Mark Mendell6bc53a92015-07-01 14:26:52 -04001083 // Okay, everything checks out. Finally time to do the copy.
1084 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1085 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1086 DCHECK_EQ(char_size, 2u);
1087
1088 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1089
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001090 if (src_pos.IsConstant()) {
1091 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1092 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001093 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001094 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001095 ScaleFactor::TIMES_2, data_offset));
1096 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001097 if (dest_pos.IsConstant()) {
1098 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1099 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001100 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001101 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001102 ScaleFactor::TIMES_2, data_offset));
1103 }
1104
1105 // Do the move.
1106 __ rep_movsw();
1107
1108 __ Bind(slow_path->GetExitLabel());
1109}
1110
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001111
1112void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001113 // The only read barrier implementation supporting the
1114 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1115 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01001116 return;
1117 }
1118
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001119 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001120}
1121
1122void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001123 // The only read barrier implementation supporting the
1124 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1125 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001126
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001127 X86_64Assembler* assembler = GetAssembler();
1128 LocationSummary* locations = invoke->GetLocations();
1129
1130 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1131 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1132 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1133 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001134 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001135
1136 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1137 Location src_pos = locations->InAt(1);
1138 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1139 Location dest_pos = locations->InAt(3);
1140 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +01001141 Location temp1_loc = locations->GetTemp(0);
1142 CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
1143 Location temp2_loc = locations->GetTemp(1);
1144 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
1145 Location temp3_loc = locations->GetTemp(2);
1146 CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
1147 Location TMP_loc = Location::RegisterLocation(TMP);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001148
Roland Levillain0b671c02016-08-19 12:02:34 +01001149 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1150 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001151
Roland Levillainebea3d22016-04-12 15:42:57 +01001152 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001153 SystemArrayCopyOptimizations optimizations(invoke);
1154
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001155 // If source and destination are the same, we go to slow path if we need to do
1156 // forward copying.
1157 if (src_pos.IsConstant()) {
1158 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1159 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001160 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1161 if (optimizations.GetDestinationIsSource()) {
1162 // Checked when building locations.
1163 DCHECK_GE(src_pos_constant, dest_pos_constant);
1164 } else if (src_pos_constant < dest_pos_constant) {
1165 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001166 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001167 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001168 } else {
1169 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001170 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001171 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001172 }
1173 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001174 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001175 }
1176 } else {
1177 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001178 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001179 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001180 }
1181 if (dest_pos.IsConstant()) {
1182 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1183 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001184 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001185 } else {
1186 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001187 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001188 }
1189 }
1190
Roland Levillainebea3d22016-04-12 15:42:57 +01001191 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001192
1193 if (!optimizations.GetSourceIsNotNull()) {
1194 // Bail out if the source is null.
1195 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01001196 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001197 }
1198
1199 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1200 // Bail out if the destination is null.
1201 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001202 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001203 }
1204
1205 // If the length is negative, bail out.
1206 // We have already checked in the LocationsBuilder for the constant case.
1207 if (!length.IsConstant() &&
1208 !optimizations.GetCountIsSourceLength() &&
1209 !optimizations.GetCountIsDestinationLength()) {
1210 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001211 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001212 }
1213
1214 // Validity checks: source.
1215 CheckPosition(assembler,
1216 src_pos,
1217 src,
1218 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001219 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001220 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001221 optimizations.GetCountIsSourceLength());
1222
1223 // Validity checks: dest.
1224 CheckPosition(assembler,
1225 dest_pos,
1226 dest,
1227 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001228 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001229 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001230 optimizations.GetCountIsDestinationLength());
1231
1232 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1233 // Check whether all elements of the source array are assignable to the component
1234 // type of the destination array. We do two checks: the classes are the same,
1235 // or the destination is Object[]. If none of these checks succeed, we go to the
1236 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01001237
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001238 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +01001239 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1240 // /* HeapReference<Class> */ temp1 = dest->klass_
1241 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001242 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001243 // Register `temp1` is not trashed by the read barrier emitted
1244 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1245 // method produces a call to a ReadBarrierMarkRegX entry point,
1246 // which saves all potentially live registers, including
1247 // temporaries such a `temp1`.
1248 // /* HeapReference<Class> */ temp2 = src->klass_
1249 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001250 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001251 // If heap poisoning is enabled, `temp1` and `temp2` have been
1252 // unpoisoned by the the previous calls to
1253 // GenerateFieldLoadWithBakerReadBarrier.
1254 } else {
1255 // /* HeapReference<Class> */ temp1 = dest->klass_
1256 __ movl(temp1, Address(dest, class_offset));
1257 // /* HeapReference<Class> */ temp2 = src->klass_
1258 __ movl(temp2, Address(src, class_offset));
1259 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1260 !optimizations.GetSourceIsNonPrimitiveArray()) {
1261 // One or two of the references need to be unpoisoned. Unpoison them
1262 // both to make the identity check valid.
1263 __ MaybeUnpoisonHeapReference(temp1);
1264 __ MaybeUnpoisonHeapReference(temp2);
1265 did_unpoison = true;
1266 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001267 }
1268
1269 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1270 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001271 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1272 // /* HeapReference<Class> */ TMP = temp1->component_type_
1273 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001274 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001275 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1276 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1277 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1278 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1279 } else {
1280 // /* HeapReference<Class> */ TMP = temp1->component_type_
1281 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1282 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1283 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1284 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1285 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001286 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001287 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001288 }
1289
1290 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1291 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001292 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1293 // For the same reason given earlier, `temp1` is not trashed by the
1294 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1295 // /* HeapReference<Class> */ TMP = temp2->component_type_
1296 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001297 invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001298 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1299 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1300 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1301 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1302 } else {
1303 // /* HeapReference<Class> */ TMP = temp2->component_type_
1304 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1305 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1306 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1307 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1308 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001309 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001310 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001311 }
1312
1313 __ cmpl(temp1, temp2);
1314
1315 if (optimizations.GetDestinationIsTypedObjectArray()) {
1316 NearLabel do_copy;
1317 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001318 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1319 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1320 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001321 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001322 // We do not need to emit a read barrier for the following
1323 // heap reference load, as `temp1` is only used in a
1324 // comparison with null below, and this reference is not
1325 // kept afterwards.
1326 __ cmpl(Address(temp1, super_offset), Immediate(0));
1327 } else {
1328 if (!did_unpoison) {
1329 __ MaybeUnpoisonHeapReference(temp1);
1330 }
1331 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1332 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001333 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001334 // No need to unpoison the following heap reference load, as
1335 // we're comparing against null.
1336 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001337 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001338 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001339 __ Bind(&do_copy);
1340 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001341 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001342 }
1343 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1344 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1345 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001346 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1347 // /* HeapReference<Class> */ temp1 = src->klass_
1348 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001349 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001350 // /* HeapReference<Class> */ TMP = temp1->component_type_
1351 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001352 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001353 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1354 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1355 } else {
1356 // /* HeapReference<Class> */ temp1 = src->klass_
1357 __ movl(temp1, Address(src, class_offset));
1358 __ MaybeUnpoisonHeapReference(temp1);
1359 // /* HeapReference<Class> */ TMP = temp1->component_type_
1360 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1361 // No need to unpoison `TMP` now, as we're comparing against null.
1362 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1363 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1364 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1365 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001366 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001367 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001368 }
1369
1370 // Compute base source address, base destination address, and end source address.
1371
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001372 int32_t element_size = Primitive::ComponentSize(Primitive::kPrimNot);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001373 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
1374 if (src_pos.IsConstant()) {
1375 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1376 __ leal(temp1, Address(src, element_size * constant + offset));
1377 } else {
1378 __ leal(temp1, Address(src, src_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1379 }
1380
1381 if (dest_pos.IsConstant()) {
1382 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1383 __ leal(temp2, Address(dest, element_size * constant + offset));
1384 } else {
1385 __ leal(temp2, Address(dest, dest_pos.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, offset));
1386 }
1387
1388 if (length.IsConstant()) {
1389 int32_t constant = length.GetConstant()->AsIntConstant()->GetValue();
1390 __ leal(temp3, Address(temp1, element_size * constant));
1391 } else {
1392 __ leal(temp3, Address(temp1, length.AsRegister<CpuRegister>(), ScaleFactor::TIMES_4, 0));
1393 }
1394
Roland Levillain0b671c02016-08-19 12:02:34 +01001395 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1396 // SystemArrayCopy implementation for Baker read barriers (see
1397 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1398 //
1399 // if (src_ptr != end_ptr) {
1400 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1401 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001402 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01001403 // if (is_gray) {
1404 // // Slow-path copy.
1405 // do {
1406 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1407 // } while (src_ptr != end_ptr)
1408 // } else {
1409 // // Fast-path copy.
1410 // do {
1411 // *dest_ptr++ = *src_ptr++;
1412 // } while (src_ptr != end_ptr)
1413 // }
1414 // }
1415
1416 NearLabel loop, done;
1417
1418 // Don't enter copy loop if `length == 0`.
1419 __ cmpl(temp1, temp3);
1420 __ j(kEqual, &done);
1421
Vladimir Marko953437b2016-08-24 08:30:46 +00001422 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001423 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1424 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00001425 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
1426 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
1427 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
1428
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001429 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00001430 // goto slow_path;
1431 // At this point, just do the "if" and make sure that flags are preserved until the branch.
1432 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01001433
1434 // Load fence to prevent load-load reordering.
1435 // Note that this is a no-op, thanks to the x86-64 memory model.
1436 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1437
1438 // Slow path used to copy array when `src` is gray.
1439 SlowPathCode* read_barrier_slow_path =
1440 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
1441 codegen_->AddSlowPath(read_barrier_slow_path);
1442
Vladimir Marko953437b2016-08-24 08:30:46 +00001443 // We have done the "if" of the gray bit check above, now branch based on the flags.
1444 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01001445
1446 // Fast-path copy.
1447 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1448 // poison/unpoison.
1449 __ Bind(&loop);
1450 __ movl(CpuRegister(TMP), Address(temp1, 0));
1451 __ movl(Address(temp2, 0), CpuRegister(TMP));
1452 __ addl(temp1, Immediate(element_size));
1453 __ addl(temp2, Immediate(element_size));
1454 __ cmpl(temp1, temp3);
1455 __ j(kNotEqual, &loop);
1456
1457 __ Bind(read_barrier_slow_path->GetExitLabel());
1458 __ Bind(&done);
1459 } else {
1460 // Non read barrier code.
1461
1462 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1463 // poison/unpoison.
1464 NearLabel loop, done;
1465 __ cmpl(temp1, temp3);
1466 __ j(kEqual, &done);
1467 __ Bind(&loop);
1468 __ movl(CpuRegister(TMP), Address(temp1, 0));
1469 __ movl(Address(temp2, 0), CpuRegister(TMP));
1470 __ addl(temp1, Immediate(element_size));
1471 __ addl(temp2, Immediate(element_size));
1472 __ cmpl(temp1, temp3);
1473 __ j(kNotEqual, &loop);
1474 __ Bind(&done);
1475 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001476
1477 // We only need one card marking on the destination array.
1478 codegen_->MarkGCCard(temp1,
1479 temp2,
1480 dest,
1481 CpuRegister(kNoRegister),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001482 /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001483
Roland Levillain0b671c02016-08-19 12:02:34 +01001484 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001485}
1486
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001487void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1488 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001489 LocationSummary::kCallOnMainAndSlowPath,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001490 kIntrinsified);
1491 InvokeRuntimeCallingConvention calling_convention;
1492 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1493 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1494 locations->SetOut(Location::RegisterLocation(RAX));
1495}
1496
1497void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1498 X86_64Assembler* assembler = GetAssembler();
1499 LocationSummary* locations = invoke->GetLocations();
1500
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001501 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001502 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001503
1504 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1505 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001506 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001507 codegen_->AddSlowPath(slow_path);
1508 __ j(kEqual, slow_path->GetEntryLabel());
1509
Serban Constantinescuba45db02016-07-12 22:53:02 +01001510 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001511 __ Bind(slow_path->GetExitLabel());
1512}
1513
Agi Csakif8cfb202015-08-13 17:54:54 -07001514void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1515 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1516 LocationSummary::kNoCall,
1517 kIntrinsified);
1518 locations->SetInAt(0, Location::RequiresRegister());
1519 locations->SetInAt(1, Location::RequiresRegister());
1520
1521 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1522 locations->AddTemp(Location::RegisterLocation(RCX));
1523 locations->AddTemp(Location::RegisterLocation(RDI));
1524
1525 // Set output, RSI needed for repe_cmpsq instruction anyways.
1526 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1527}
1528
1529void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1530 X86_64Assembler* assembler = GetAssembler();
1531 LocationSummary* locations = invoke->GetLocations();
1532
1533 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1534 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1535 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1536 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1537 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1538
Mark Mendell0c9497d2015-08-21 09:30:05 -04001539 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001540
1541 // Get offsets of count, value, and class fields within a string object.
1542 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1543 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1544 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1545
1546 // Note that the null check must have been done earlier.
1547 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1548
Vladimir Marko53b52002016-05-24 19:30:45 +01001549 StringEqualsOptimizations optimizations(invoke);
1550 if (!optimizations.GetArgumentNotNull()) {
1551 // Check if input is null, return false if it is.
1552 __ testl(arg, arg);
1553 __ j(kEqual, &return_false);
1554 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001555
Vladimir Marko53b52002016-05-24 19:30:45 +01001556 if (!optimizations.GetArgumentIsString()) {
1557 // Instanceof check for the argument by comparing class fields.
1558 // All string objects must have the same type since String cannot be subclassed.
1559 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1560 // If the argument is a string object, its class field must be equal to receiver's class field.
1561 __ movl(rcx, Address(str, class_offset));
1562 __ cmpl(rcx, Address(arg, class_offset));
1563 __ j(kNotEqual, &return_false);
1564 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001565
1566 // Reference equality check, return true if same reference.
1567 __ cmpl(str, arg);
1568 __ j(kEqual, &return_true);
1569
jessicahandojo4877b792016-09-08 19:49:13 -07001570 // Load length and compression flag of receiver string.
Agi Csakif8cfb202015-08-13 17:54:54 -07001571 __ movl(rcx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001572 // Check if lengths and compressiond flags are equal, return false if they're not.
1573 // Two identical strings will always have same compression style since
1574 // compression style is decided on alloc.
Agi Csakif8cfb202015-08-13 17:54:54 -07001575 __ cmpl(rcx, Address(arg, count_offset));
1576 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001577 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1578 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1579 "Expecting 0=compressed, 1=uncompressed");
1580 __ jrcxz(&return_true);
jessicahandojo4877b792016-09-08 19:49:13 -07001581
1582 if (mirror::kUseStringCompression) {
1583 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001584 // Extract length and differentiate between both compressed or both uncompressed.
1585 // Different compression style is cut above.
1586 __ shrl(rcx, Immediate(1));
1587 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001588 // Divide string length by 2, rounding up, and continue as if uncompressed.
1589 // Merge clearing the compression flag with +1 for rounding.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001590 __ addl(rcx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001591 __ shrl(rcx, Immediate(1));
1592 __ Bind(&string_uncompressed);
1593 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001594 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1595 __ leal(rsi, Address(str, value_offset));
1596 __ leal(rdi, Address(arg, value_offset));
1597
1598 // Divide string length by 4 and adjust for lengths not divisible by 4.
1599 __ addl(rcx, Immediate(3));
1600 __ shrl(rcx, Immediate(2));
1601
jessicahandojo4877b792016-09-08 19:49:13 -07001602 // Assertions that must hold in order to compare strings 4 characters (uncompressed)
1603 // or 8 characters (compressed) at a time.
Agi Csakif8cfb202015-08-13 17:54:54 -07001604 DCHECK_ALIGNED(value_offset, 8);
1605 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1606
1607 // Loop to compare strings four characters at a time starting at the beginning of the string.
1608 __ repe_cmpsq();
1609 // If strings are not equal, zero flag will be cleared.
1610 __ j(kNotEqual, &return_false);
1611
1612 // Return true and exit the function.
1613 // If loop does not result in returning false, we return true.
1614 __ Bind(&return_true);
1615 __ movl(rsi, Immediate(1));
1616 __ jmp(&end);
1617
1618 // Return false and exit the function.
1619 __ Bind(&return_false);
1620 __ xorl(rsi, rsi);
1621 __ Bind(&end);
1622}
1623
Andreas Gampe21030dd2015-05-07 14:46:15 -07001624static void CreateStringIndexOfLocations(HInvoke* invoke,
1625 ArenaAllocator* allocator,
1626 bool start_at_zero) {
1627 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1628 LocationSummary::kCallOnSlowPath,
1629 kIntrinsified);
1630 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1631 locations->SetInAt(0, Location::RegisterLocation(RDI));
1632 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1633 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1634 // of the instruction explicitly.
1635 // Note: This works as we don't clobber RAX anywhere.
1636 locations->SetInAt(1, Location::RegisterLocation(RAX));
1637 if (!start_at_zero) {
1638 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1639 }
1640 // As we clobber RDI during execution anyways, also use it as the output.
1641 locations->SetOut(Location::SameAsFirstInput());
1642
1643 // repne scasw uses RCX as the counter.
1644 locations->AddTemp(Location::RegisterLocation(RCX));
1645 // Need another temporary to be able to compute the result.
1646 locations->AddTemp(Location::RequiresRegister());
1647}
1648
1649static void GenerateStringIndexOf(HInvoke* invoke,
1650 X86_64Assembler* assembler,
1651 CodeGeneratorX86_64* codegen,
1652 ArenaAllocator* allocator,
1653 bool start_at_zero) {
1654 LocationSummary* locations = invoke->GetLocations();
1655
1656 // Note that the null check must have been done earlier.
1657 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1658
1659 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1660 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1661 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1662 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1663 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1664
1665 // Check our assumptions for registers.
1666 DCHECK_EQ(string_obj.AsRegister(), RDI);
1667 DCHECK_EQ(search_value.AsRegister(), RAX);
1668 DCHECK_EQ(counter.AsRegister(), RCX);
1669 DCHECK_EQ(out.AsRegister(), RDI);
1670
1671 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001672 // 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 -07001673 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001674 HInstruction* code_point = invoke->InputAt(1);
1675 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001676 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001677 std::numeric_limits<uint16_t>::max()) {
1678 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1679 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1680 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1681 codegen->AddSlowPath(slow_path);
1682 __ jmp(slow_path->GetEntryLabel());
1683 __ Bind(slow_path->GetExitLabel());
1684 return;
1685 }
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001686 } else if (code_point->GetType() != Primitive::kPrimChar) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001687 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1688 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1689 codegen->AddSlowPath(slow_path);
1690 __ j(kAbove, slow_path->GetEntryLabel());
1691 }
1692
jessicahandojo4877b792016-09-08 19:49:13 -07001693 // From here down, we know that we are looking for a char that fits in
1694 // 16 bits (uncompressed) or 8 bits (compressed).
Andreas Gampe21030dd2015-05-07 14:46:15 -07001695 // Location of reference to data array within the String object.
1696 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1697 // Location of count within the String object.
1698 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1699
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001700 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001701 __ movl(string_length, Address(string_obj, count_offset));
1702
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001703 // Do a zero-length check. Even with string compression `count == 0` means empty.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001704 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001705 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001706 __ testl(string_length, string_length);
1707 __ j(kEqual, &not_found_label);
1708
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001709 if (mirror::kUseStringCompression) {
1710 // Use TMP to keep string_length_flagged.
1711 __ movl(CpuRegister(TMP), string_length);
1712 // Mask out first bit used as compression flag.
1713 __ shrl(string_length, Immediate(1));
1714 }
1715
Andreas Gampe21030dd2015-05-07 14:46:15 -07001716 if (start_at_zero) {
1717 // Number of chars to scan is the same as the string length.
1718 __ movl(counter, string_length);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001719 // Move to the start of the string.
1720 __ addq(string_obj, Immediate(value_offset));
1721 } else {
1722 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1723
1724 // Do a start_index check.
1725 __ cmpl(start_index, string_length);
1726 __ j(kGreaterEqual, &not_found_label);
1727
1728 // Ensure we have a start index >= 0;
1729 __ xorl(counter, counter);
1730 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001731 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001732
jessicahandojo4877b792016-09-08 19:49:13 -07001733 if (mirror::kUseStringCompression) {
1734 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001735 __ testl(CpuRegister(TMP), Immediate(1));
1736 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001737 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1738 __ jmp(&modify_counter);
1739 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1740 __ Bind(&offset_uncompressed_label);
1741 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1742 __ Bind(&modify_counter);
1743 } else {
1744 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1745 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001746 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1747 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1748 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1749 }
1750
jessicahandojo4877b792016-09-08 19:49:13 -07001751 if (mirror::kUseStringCompression) {
1752 NearLabel uncompressed_string_comparison;
1753 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001754 __ testl(CpuRegister(TMP), Immediate(1));
1755 __ j(kNotZero, &uncompressed_string_comparison);
jessicahandojo4877b792016-09-08 19:49:13 -07001756 // Check if RAX (search_value) is ASCII.
1757 __ cmpl(search_value, Immediate(127));
1758 __ j(kGreater, &not_found_label);
1759 // Comparing byte-per-byte.
1760 __ repne_scasb();
1761 __ jmp(&comparison_done);
1762 // Everything is set up for repne scasw:
1763 // * Comparison address in RDI.
1764 // * Counter in ECX.
1765 __ Bind(&uncompressed_string_comparison);
1766 __ repne_scasw();
1767 __ Bind(&comparison_done);
1768 } else {
1769 __ repne_scasw();
1770 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001771 // Did we find a match?
1772 __ j(kNotEqual, &not_found_label);
1773
1774 // Yes, we matched. Compute the index of the result.
1775 __ subl(string_length, counter);
1776 __ leal(out, Address(string_length, -1));
1777
Mark Mendell0c9497d2015-08-21 09:30:05 -04001778 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001779 __ jmp(&done);
1780
1781 // Failed to match; return -1.
1782 __ Bind(&not_found_label);
1783 __ movl(out, Immediate(-1));
1784
1785 // And join up at the end.
1786 __ Bind(&done);
1787 if (slow_path != nullptr) {
1788 __ Bind(slow_path->GetExitLabel());
1789 }
1790}
1791
1792void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001793 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001794}
1795
1796void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001797 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001798}
1799
1800void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001801 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001802}
1803
1804void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001805 GenerateStringIndexOf(
1806 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001807}
1808
Jeff Hao848f70a2014-01-15 13:49:50 -08001809void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1810 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001811 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001812 kIntrinsified);
1813 InvokeRuntimeCallingConvention calling_convention;
1814 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1815 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1816 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1817 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1818 locations->SetOut(Location::RegisterLocation(RAX));
1819}
1820
1821void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1822 X86_64Assembler* assembler = GetAssembler();
1823 LocationSummary* locations = invoke->GetLocations();
1824
1825 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1826 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001827 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001828 codegen_->AddSlowPath(slow_path);
1829 __ j(kEqual, slow_path->GetEntryLabel());
1830
Serban Constantinescuba45db02016-07-12 22:53:02 +01001831 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001832 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001833 __ Bind(slow_path->GetExitLabel());
1834}
1835
1836void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1837 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001838 LocationSummary::kCallOnMainOnly,
Jeff Hao848f70a2014-01-15 13:49:50 -08001839 kIntrinsified);
1840 InvokeRuntimeCallingConvention calling_convention;
1841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1842 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1843 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1844 locations->SetOut(Location::RegisterLocation(RAX));
1845}
1846
1847void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001848 // No need to emit code checking whether `locations->InAt(2)` is a null
1849 // pointer, as callers of the native method
1850 //
1851 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1852 //
1853 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001854 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001855 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001856}
1857
1858void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1859 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001860 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001861 kIntrinsified);
1862 InvokeRuntimeCallingConvention calling_convention;
1863 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1864 locations->SetOut(Location::RegisterLocation(RAX));
1865}
1866
1867void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1868 X86_64Assembler* assembler = GetAssembler();
1869 LocationSummary* locations = invoke->GetLocations();
1870
1871 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1872 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001873 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001874 codegen_->AddSlowPath(slow_path);
1875 __ j(kEqual, slow_path->GetEntryLabel());
1876
Serban Constantinescuba45db02016-07-12 22:53:02 +01001877 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001878 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001879 __ Bind(slow_path->GetExitLabel());
1880}
1881
Mark Mendell8f8926a2015-08-17 11:39:06 -04001882void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1883 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1884 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1885 LocationSummary::kNoCall,
1886 kIntrinsified);
1887 locations->SetInAt(0, Location::RequiresRegister());
1888 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1889 locations->SetInAt(2, Location::RequiresRegister());
1890 locations->SetInAt(3, Location::RequiresRegister());
1891 locations->SetInAt(4, Location::RequiresRegister());
1892
1893 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1894 locations->AddTemp(Location::RegisterLocation(RSI));
1895 locations->AddTemp(Location::RegisterLocation(RDI));
1896 locations->AddTemp(Location::RegisterLocation(RCX));
1897}
1898
1899void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1900 X86_64Assembler* assembler = GetAssembler();
1901 LocationSummary* locations = invoke->GetLocations();
1902
1903 size_t char_component_size = Primitive::ComponentSize(Primitive::kPrimChar);
1904 // Location of data in char array buffer.
1905 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1906 // Location of char array data in string.
1907 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1908
1909 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1910 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1911 Location srcBegin = locations->InAt(1);
1912 int srcBegin_value =
1913 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1914 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1915 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1916 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1917
1918 // Check assumption that sizeof(Char) is 2 (used in scaling below).
1919 const size_t char_size = Primitive::ComponentSize(Primitive::kPrimChar);
1920 DCHECK_EQ(char_size, 2u);
1921
jessicahandojo4877b792016-09-08 19:49:13 -07001922 NearLabel done;
Mark Mendell8f8926a2015-08-17 11:39:06 -04001923 // Compute the number of chars (words) to move.
1924 __ movl(CpuRegister(RCX), srcEnd);
1925 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001926 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001927 } else {
1928 DCHECK(srcBegin.IsRegister());
1929 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1930 }
jessicahandojo4877b792016-09-08 19:49:13 -07001931 if (mirror::kUseStringCompression) {
1932 NearLabel copy_uncompressed, copy_loop;
1933 const size_t c_char_size = Primitive::ComponentSize(Primitive::kPrimByte);
1934 DCHECK_EQ(c_char_size, 1u);
1935 // Location of count in string.
1936 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Mark Mendell8f8926a2015-08-17 11:39:06 -04001937
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001938 __ testl(Address(obj, count_offset), Immediate(1));
1939 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1940 "Expecting 0=compressed, 1=uncompressed");
1941 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001942 // Compute the address of the source string by adding the number of chars from
1943 // the source beginning to the value offset of a string.
1944 __ leaq(CpuRegister(RSI),
1945 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1946 // Start the loop to copy String's value to Array of Char.
1947 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1948
1949 __ Bind(&copy_loop);
1950 __ jrcxz(&done);
1951 // Use TMP as temporary (convert byte from RSI to word).
1952 // TODO: Selecting RAX as the temporary and using LODSB/STOSW.
1953 __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
1954 __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
1955 __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
1956 __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
1957 // TODO: Add support for LOOP to X86_64Assembler.
1958 __ subl(CpuRegister(RCX), Immediate(1));
1959 __ jmp(&copy_loop);
1960
1961 __ Bind(&copy_uncompressed);
1962 }
1963
1964 __ leaq(CpuRegister(RSI),
1965 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
1966 // Compute the address of the destination buffer.
1967 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001968 // Do the move.
1969 __ rep_movsw();
jessicahandojo4877b792016-09-08 19:49:13 -07001970
1971 __ Bind(&done);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001972}
1973
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001974static void GenPeek(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
1975 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1976 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1977 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1978 // to avoid a SIGBUS.
1979 switch (size) {
1980 case Primitive::kPrimByte:
1981 __ movsxb(out, Address(address, 0));
1982 break;
1983 case Primitive::kPrimShort:
1984 __ movsxw(out, Address(address, 0));
1985 break;
1986 case Primitive::kPrimInt:
1987 __ movl(out, Address(address, 0));
1988 break;
1989 case Primitive::kPrimLong:
1990 __ movq(out, Address(address, 0));
1991 break;
1992 default:
1993 LOG(FATAL) << "Type not recognized for peek: " << size;
1994 UNREACHABLE();
1995 }
1996}
1997
1998void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
1999 CreateIntToIntLocations(arena_, invoke);
2000}
2001
2002void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
2003 GenPeek(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
2004}
2005
2006void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
2007 CreateIntToIntLocations(arena_, invoke);
2008}
2009
2010void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
2011 GenPeek(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2012}
2013
2014void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
2015 CreateIntToIntLocations(arena_, invoke);
2016}
2017
2018void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
2019 GenPeek(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2020}
2021
2022void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
2023 CreateIntToIntLocations(arena_, invoke);
2024}
2025
2026void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
2027 GenPeek(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
2028}
2029
2030static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
2031 LocationSummary* locations = new (arena) LocationSummary(invoke,
2032 LocationSummary::kNoCall,
2033 kIntrinsified);
2034 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002035 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002036}
2037
2038static void GenPoke(LocationSummary* locations, Primitive::Type size, X86_64Assembler* assembler) {
2039 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04002040 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002041 // x86 allows unaligned access. We do not have to check the input or use specific instructions
2042 // to avoid a SIGBUS.
2043 switch (size) {
2044 case Primitive::kPrimByte:
Mark Mendell40741f32015-04-20 22:10:34 -04002045 if (value.IsConstant()) {
2046 __ movb(Address(address, 0),
2047 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2048 } else {
2049 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
2050 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002051 break;
2052 case Primitive::kPrimShort:
Mark Mendell40741f32015-04-20 22:10:34 -04002053 if (value.IsConstant()) {
2054 __ movw(Address(address, 0),
2055 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2056 } else {
2057 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
2058 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002059 break;
2060 case Primitive::kPrimInt:
Mark Mendell40741f32015-04-20 22:10:34 -04002061 if (value.IsConstant()) {
2062 __ movl(Address(address, 0),
2063 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2064 } else {
2065 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
2066 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002067 break;
2068 case Primitive::kPrimLong:
Mark Mendell40741f32015-04-20 22:10:34 -04002069 if (value.IsConstant()) {
2070 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
2071 DCHECK(IsInt<32>(v));
2072 int32_t v_32 = v;
2073 __ movq(Address(address, 0), Immediate(v_32));
2074 } else {
2075 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
2076 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002077 break;
2078 default:
2079 LOG(FATAL) << "Type not recognized for poke: " << size;
2080 UNREACHABLE();
2081 }
2082}
2083
2084void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2085 CreateIntIntToVoidLocations(arena_, invoke);
2086}
2087
2088void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2089 GenPoke(invoke->GetLocations(), Primitive::kPrimByte, GetAssembler());
2090}
2091
2092void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2093 CreateIntIntToVoidLocations(arena_, invoke);
2094}
2095
2096void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2097 GenPoke(invoke->GetLocations(), Primitive::kPrimInt, GetAssembler());
2098}
2099
2100void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2101 CreateIntIntToVoidLocations(arena_, invoke);
2102}
2103
2104void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2105 GenPoke(invoke->GetLocations(), Primitive::kPrimLong, GetAssembler());
2106}
2107
2108void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2109 CreateIntIntToVoidLocations(arena_, invoke);
2110}
2111
2112void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2113 GenPoke(invoke->GetLocations(), Primitive::kPrimShort, GetAssembler());
2114}
2115
2116void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2117 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2118 LocationSummary::kNoCall,
2119 kIntrinsified);
2120 locations->SetOut(Location::RequiresRegister());
2121}
2122
2123void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2124 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07002125 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002126 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002127}
2128
Roland Levillain0d5a2812015-11-13 10:07:31 +00002129static void GenUnsafeGet(HInvoke* invoke,
2130 Primitive::Type type,
2131 bool is_volatile ATTRIBUTE_UNUSED,
2132 CodeGeneratorX86_64* codegen) {
2133 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
2134 LocationSummary* locations = invoke->GetLocations();
2135 Location base_loc = locations->InAt(1);
2136 CpuRegister base = base_loc.AsRegister<CpuRegister>();
2137 Location offset_loc = locations->InAt(2);
2138 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
2139 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002140 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002141
Andreas Gampe878d58c2015-01-15 23:24:00 -08002142 switch (type) {
2143 case Primitive::kPrimInt:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002144 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002145 break;
2146
2147 case Primitive::kPrimNot: {
2148 if (kEmitCompilerReadBarrier) {
2149 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002150 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2151 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002152 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002153 } else {
2154 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2155 codegen->GenerateReadBarrierSlow(
2156 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2157 }
2158 } else {
2159 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2160 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002161 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002162 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002163 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002164
2165 case Primitive::kPrimLong:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002166 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002167 break;
2168
2169 default:
2170 LOG(FATAL) << "Unsupported op size " << type;
2171 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002172 }
2173}
2174
Vladimir Marko953437b2016-08-24 08:30:46 +00002175static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002176 bool can_call = kEmitCompilerReadBarrier &&
2177 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2178 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002179 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002180 (can_call
2181 ? LocationSummary::kCallOnSlowPath
2182 : LocationSummary::kNoCall),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002183 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002184 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002185 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002186 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002187 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002188 locations->SetInAt(1, Location::RequiresRegister());
2189 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002190 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002191 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002192}
2193
2194void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002195 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002196}
2197void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002198 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002199}
2200void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002201 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002202}
2203void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002204 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002205}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002206void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002207 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002208}
2209void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002210 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002211}
2212
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002213
2214void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002215 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002216}
2217void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002218 GenUnsafeGet(invoke, Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002219}
2220void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002221 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002222}
2223void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002224 GenUnsafeGet(invoke, Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002225}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002226void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002227 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002228}
2229void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002230 GenUnsafeGet(invoke, Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002231}
2232
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002233
2234static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
2235 Primitive::Type type,
2236 HInvoke* invoke) {
2237 LocationSummary* locations = new (arena) LocationSummary(invoke,
2238 LocationSummary::kNoCall,
2239 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002240 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002241 locations->SetInAt(1, Location::RequiresRegister());
2242 locations->SetInAt(2, Location::RequiresRegister());
2243 locations->SetInAt(3, Location::RequiresRegister());
2244 if (type == Primitive::kPrimNot) {
2245 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002246 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002247 locations->AddTemp(Location::RequiresRegister());
2248 }
2249}
2250
2251void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
2252 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2253}
2254void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
2255 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2256}
2257void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
2258 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimInt, invoke);
2259}
2260void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
2261 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2262}
2263void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
2264 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2265}
2266void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
2267 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimNot, invoke);
2268}
2269void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
2270 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2271}
2272void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
2273 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2274}
2275void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
2276 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, Primitive::kPrimLong, invoke);
2277}
2278
2279// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2280// memory model.
2281static void GenUnsafePut(LocationSummary* locations, Primitive::Type type, bool is_volatile,
2282 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002283 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002284 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2285 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2286 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2287
2288 if (type == Primitive::kPrimLong) {
2289 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Roland Levillain4d027112015-07-01 15:41:14 +01002290 } else if (kPoisonHeapReferences && type == Primitive::kPrimNot) {
2291 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2292 __ movl(temp, value);
2293 __ PoisonHeapReference(temp);
2294 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002295 } else {
2296 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2297 }
2298
2299 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002300 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002301 }
2302
2303 if (type == Primitive::kPrimNot) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002304 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002305 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2306 locations->GetTemp(1).AsRegister<CpuRegister>(),
2307 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002308 value,
2309 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002310 }
2311}
2312
2313void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002314 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002315}
2316void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002317 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002318}
2319void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002320 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimInt, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002321}
2322void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002323 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002324}
2325void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002326 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002327}
2328void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002329 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimNot, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002330}
2331void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002332 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002333}
2334void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002335 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002336}
2337void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002338 GenUnsafePut(invoke->GetLocations(), Primitive::kPrimLong, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002339}
2340
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002341static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
2342 Primitive::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002343 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002344 bool can_call = kEmitCompilerReadBarrier &&
2345 kUseBakerReadBarrier &&
2346 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002347 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002348 (can_call
2349 ? LocationSummary::kCallOnSlowPath
2350 : LocationSummary::kNoCall),
Mark Mendell58d25fd2015-04-03 14:52:31 -04002351 kIntrinsified);
2352 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2353 locations->SetInAt(1, Location::RequiresRegister());
2354 locations->SetInAt(2, Location::RequiresRegister());
2355 // expected value must be in EAX/RAX.
2356 locations->SetInAt(3, Location::RegisterLocation(RAX));
2357 locations->SetInAt(4, Location::RequiresRegister());
2358
2359 locations->SetOut(Location::RequiresRegister());
2360 if (type == Primitive::kPrimNot) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002361 // Need temporary registers for card-marking, and possibly for
2362 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002363 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002364 locations->AddTemp(Location::RequiresRegister());
2365 }
2366}
2367
2368void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2369 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimInt, invoke);
2370}
2371
2372void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2373 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimLong, invoke);
2374}
2375
2376void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002377 // The only read barrier implementation supporting the
2378 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2379 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002380 return;
2381 }
2382
Mark Mendell58d25fd2015-04-03 14:52:31 -04002383 CreateIntIntIntIntIntToInt(arena_, Primitive::kPrimNot, invoke);
2384}
2385
2386static void GenCAS(Primitive::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002387 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002388 LocationSummary* locations = invoke->GetLocations();
2389
2390 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2391 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2392 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002393 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002394 DCHECK_EQ(expected.AsRegister(), RAX);
2395 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002396 Location out_loc = locations->Out();
2397 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002398
Roland Levillainb488b782015-10-22 11:38:49 +01002399 if (type == Primitive::kPrimNot) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002400 // The only read barrier implementation supporting the
2401 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2402 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2403
2404 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2405 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2406
Roland Levillainb488b782015-10-22 11:38:49 +01002407 // Mark card for object assuming new value is stored.
2408 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002409 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2410
2411 // The address of the field within the holding object.
2412 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2413
2414 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2415 // Need to make sure the reference stored in the field is a to-space
2416 // one before attempting the CAS or the CAS could fail incorrectly.
2417 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2418 invoke,
2419 out_loc, // Unused, used only as a "temporary" within the read barrier.
2420 base,
2421 field_addr,
2422 /* needs_null_check */ false,
2423 /* always_update_field */ true,
2424 &temp1,
2425 &temp2);
2426 }
Roland Levillain4d027112015-07-01 15:41:14 +01002427
Roland Levillainb488b782015-10-22 11:38:49 +01002428 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2429 Register value_reg = value.AsRegister();
2430 if (kPoisonHeapReferences) {
2431 if (base_equals_value) {
2432 // If `base` and `value` are the same register location, move
2433 // `value_reg` to a temporary register. This way, poisoning
2434 // `value_reg` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002435 value_reg = temp1.AsRegister();
Roland Levillainb488b782015-10-22 11:38:49 +01002436 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002437 }
Roland Levillainb488b782015-10-22 11:38:49 +01002438
2439 // Check that the register allocator did not assign the location
2440 // of `expected` (RAX) to `value` nor to `base`, so that heap
2441 // poisoning (when enabled) works as intended below.
2442 // - If `value` were equal to `expected`, both references would
2443 // be poisoned twice, meaning they would not be poisoned at
2444 // all, as heap poisoning uses address negation.
2445 // - If `base` were equal to `expected`, poisoning `expected`
2446 // would invalidate `base`.
2447 DCHECK_NE(value_reg, expected.AsRegister());
2448 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2449
2450 __ PoisonHeapReference(expected);
2451 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002452 }
2453
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002454 __ LockCmpxchgl(field_addr, CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002455
Roland Levillain0d5a2812015-11-13 10:07:31 +00002456 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002457 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002458
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002459 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002460 __ setcc(kZero, out);
2461 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002462
Roland Levillain391b8662015-12-18 11:43:38 +00002463 // If heap poisoning is enabled, we need to unpoison the values
2464 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002465 if (kPoisonHeapReferences) {
2466 if (base_equals_value) {
2467 // `value_reg` has been moved to a temporary register, no need
2468 // to unpoison it.
2469 } else {
2470 // Ensure `value` is different from `out`, so that unpoisoning
2471 // the former does not invalidate the latter.
2472 DCHECK_NE(value_reg, out.AsRegister());
2473 __ UnpoisonHeapReference(CpuRegister(value_reg));
2474 }
2475 // Ensure `expected` is different from `out`, so that unpoisoning
2476 // the former does not invalidate the latter.
2477 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2478 __ UnpoisonHeapReference(expected);
2479 }
2480 } else {
2481 if (type == Primitive::kPrimInt) {
2482 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
2483 } else if (type == Primitive::kPrimLong) {
2484 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2485 } else {
2486 LOG(FATAL) << "Unexpected CAS type " << type;
2487 }
2488
Roland Levillain0d5a2812015-11-13 10:07:31 +00002489 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002490 // scheduling barriers at this time.
2491
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002492 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002493 __ setcc(kZero, out);
2494 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002495 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002496}
2497
2498void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
2499 GenCAS(Primitive::kPrimInt, invoke, codegen_);
2500}
2501
2502void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
2503 GenCAS(Primitive::kPrimLong, invoke, codegen_);
2504}
2505
2506void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002507 // The only read barrier implementation supporting the
2508 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2509 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002510
Mark Mendell58d25fd2015-04-03 14:52:31 -04002511 GenCAS(Primitive::kPrimNot, invoke, codegen_);
2512}
2513
2514void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2515 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2516 LocationSummary::kNoCall,
2517 kIntrinsified);
2518 locations->SetInAt(0, Location::RequiresRegister());
2519 locations->SetOut(Location::SameAsFirstInput());
2520 locations->AddTemp(Location::RequiresRegister());
2521}
2522
2523static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2524 X86_64Assembler* assembler) {
2525 Immediate imm_shift(shift);
2526 Immediate imm_mask(mask);
2527 __ movl(temp, reg);
2528 __ shrl(reg, imm_shift);
2529 __ andl(temp, imm_mask);
2530 __ andl(reg, imm_mask);
2531 __ shll(temp, imm_shift);
2532 __ orl(reg, temp);
2533}
2534
2535void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002536 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002537 LocationSummary* locations = invoke->GetLocations();
2538
2539 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2540 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2541
2542 /*
2543 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2544 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2545 * compared to generic luni implementation which has 5 rounds of swapping bits.
2546 * x = bswap x
2547 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2548 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2549 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2550 */
2551 __ bswapl(reg);
2552 SwapBits(reg, temp, 1, 0x55555555, assembler);
2553 SwapBits(reg, temp, 2, 0x33333333, assembler);
2554 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2555}
2556
2557void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2558 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2559 LocationSummary::kNoCall,
2560 kIntrinsified);
2561 locations->SetInAt(0, Location::RequiresRegister());
2562 locations->SetOut(Location::SameAsFirstInput());
2563 locations->AddTemp(Location::RequiresRegister());
2564 locations->AddTemp(Location::RequiresRegister());
2565}
2566
2567static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2568 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2569 Immediate imm_shift(shift);
2570 __ movq(temp_mask, Immediate(mask));
2571 __ movq(temp, reg);
2572 __ shrq(reg, imm_shift);
2573 __ andq(temp, temp_mask);
2574 __ andq(reg, temp_mask);
2575 __ shlq(temp, imm_shift);
2576 __ orq(reg, temp);
2577}
2578
2579void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002580 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002581 LocationSummary* locations = invoke->GetLocations();
2582
2583 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2584 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2585 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2586
2587 /*
2588 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2589 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2590 * compared to generic luni implementation which has 5 rounds of swapping bits.
2591 * x = bswap x
2592 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2593 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2594 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2595 */
2596 __ bswapq(reg);
2597 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2598 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2599 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2600}
2601
Aart Bik3f67e692016-01-15 14:35:12 -08002602static void CreateBitCountLocations(
2603 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2604 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2605 // Do nothing if there is no popcnt support. This results in generating
2606 // a call for the intrinsic rather than direct code.
2607 return;
2608 }
2609 LocationSummary* locations = new (arena) LocationSummary(invoke,
2610 LocationSummary::kNoCall,
2611 kIntrinsified);
2612 locations->SetInAt(0, Location::Any());
2613 locations->SetOut(Location::RequiresRegister());
2614}
2615
Aart Bikc5d47542016-01-27 17:00:35 -08002616static void GenBitCount(X86_64Assembler* assembler,
2617 CodeGeneratorX86_64* codegen,
2618 HInvoke* invoke,
2619 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002620 LocationSummary* locations = invoke->GetLocations();
2621 Location src = locations->InAt(0);
2622 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2623
2624 if (invoke->InputAt(0)->IsConstant()) {
2625 // Evaluate this at compile time.
2626 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002627 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002628 ? POPCOUNT(static_cast<uint64_t>(value))
2629 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002630 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002631 return;
2632 }
2633
2634 if (src.IsRegister()) {
2635 if (is_long) {
2636 __ popcntq(out, src.AsRegister<CpuRegister>());
2637 } else {
2638 __ popcntl(out, src.AsRegister<CpuRegister>());
2639 }
2640 } else if (is_long) {
2641 DCHECK(src.IsDoubleStackSlot());
2642 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2643 } else {
2644 DCHECK(src.IsStackSlot());
2645 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2646 }
2647}
2648
2649void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2650 CreateBitCountLocations(arena_, codegen_, invoke);
2651}
2652
2653void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002654 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002655}
2656
2657void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2658 CreateBitCountLocations(arena_, codegen_, invoke);
2659}
2660
2661void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002662 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2663}
2664
Aart Bikc5d47542016-01-27 17:00:35 -08002665static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2666 LocationSummary* locations = new (arena) LocationSummary(invoke,
2667 LocationSummary::kNoCall,
2668 kIntrinsified);
2669 locations->SetInAt(0, Location::Any());
2670 locations->SetOut(Location::RequiresRegister());
2671 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2672 : Location::RequiresRegister()); // any will do
2673}
2674
2675static void GenOneBit(X86_64Assembler* assembler,
2676 CodeGeneratorX86_64* codegen,
2677 HInvoke* invoke,
2678 bool is_high, bool is_long) {
2679 LocationSummary* locations = invoke->GetLocations();
2680 Location src = locations->InAt(0);
2681 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2682
2683 if (invoke->InputAt(0)->IsConstant()) {
2684 // Evaluate this at compile time.
2685 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2686 if (value == 0) {
2687 __ xorl(out, out); // Clears upper bits too.
2688 return;
2689 }
2690 // Nonzero value.
2691 if (is_high) {
2692 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2693 : 31 - CLZ(static_cast<uint32_t>(value));
2694 } else {
2695 value = is_long ? CTZ(static_cast<uint64_t>(value))
2696 : CTZ(static_cast<uint32_t>(value));
2697 }
2698 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002699 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002700 } else {
2701 codegen->Load32BitValue(out, 1 << value);
2702 }
2703 return;
2704 }
2705
2706 // Handle the non-constant cases.
2707 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2708 if (is_high) {
2709 // Use architectural support: basically 1 << bsr.
2710 if (src.IsRegister()) {
2711 if (is_long) {
2712 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2713 } else {
2714 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2715 }
2716 } else if (is_long) {
2717 DCHECK(src.IsDoubleStackSlot());
2718 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2719 } else {
2720 DCHECK(src.IsStackSlot());
2721 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2722 }
2723 // BSR sets ZF if the input was zero.
2724 NearLabel is_zero, done;
2725 __ j(kEqual, &is_zero);
2726 __ movl(out, Immediate(1)); // Clears upper bits too.
2727 if (is_long) {
2728 __ shlq(out, tmp);
2729 } else {
2730 __ shll(out, tmp);
2731 }
2732 __ jmp(&done);
2733 __ Bind(&is_zero);
2734 __ xorl(out, out); // Clears upper bits too.
2735 __ Bind(&done);
2736 } else {
2737 // Copy input into temporary.
2738 if (src.IsRegister()) {
2739 if (is_long) {
2740 __ movq(tmp, src.AsRegister<CpuRegister>());
2741 } else {
2742 __ movl(tmp, src.AsRegister<CpuRegister>());
2743 }
2744 } else if (is_long) {
2745 DCHECK(src.IsDoubleStackSlot());
2746 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2747 } else {
2748 DCHECK(src.IsStackSlot());
2749 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2750 }
2751 // Do the bit twiddling: basically tmp & -tmp;
2752 if (is_long) {
2753 __ movq(out, tmp);
2754 __ negq(tmp);
2755 __ andq(out, tmp);
2756 } else {
2757 __ movl(out, tmp);
2758 __ negl(tmp);
2759 __ andl(out, tmp);
2760 }
2761 }
2762}
2763
2764void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2765 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2766}
2767
2768void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2769 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2770}
2771
2772void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2773 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2774}
2775
2776void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2777 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2778}
2779
2780void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2781 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2782}
2783
2784void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2785 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2786}
2787
2788void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2789 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2790}
2791
2792void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2793 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002794}
2795
Mark Mendelld5897672015-08-12 21:16:41 -04002796static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2797 LocationSummary* locations = new (arena) LocationSummary(invoke,
2798 LocationSummary::kNoCall,
2799 kIntrinsified);
2800 locations->SetInAt(0, Location::Any());
2801 locations->SetOut(Location::RequiresRegister());
2802}
2803
Aart Bikc5d47542016-01-27 17:00:35 -08002804static void GenLeadingZeros(X86_64Assembler* assembler,
2805 CodeGeneratorX86_64* codegen,
2806 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002807 LocationSummary* locations = invoke->GetLocations();
2808 Location src = locations->InAt(0);
2809 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2810
2811 int zero_value_result = is_long ? 64 : 32;
2812 if (invoke->InputAt(0)->IsConstant()) {
2813 // Evaluate this at compile time.
2814 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2815 if (value == 0) {
2816 value = zero_value_result;
2817 } else {
2818 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2819 }
Aart Bikc5d47542016-01-27 17:00:35 -08002820 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002821 return;
2822 }
2823
2824 // Handle the non-constant cases.
2825 if (src.IsRegister()) {
2826 if (is_long) {
2827 __ bsrq(out, src.AsRegister<CpuRegister>());
2828 } else {
2829 __ bsrl(out, src.AsRegister<CpuRegister>());
2830 }
2831 } else if (is_long) {
2832 DCHECK(src.IsDoubleStackSlot());
2833 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2834 } else {
2835 DCHECK(src.IsStackSlot());
2836 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2837 }
2838
2839 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002840 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002841 __ j(kEqual, &is_zero);
2842
2843 // Correct the result from BSR to get the CLZ result.
2844 __ xorl(out, Immediate(zero_value_result - 1));
2845 __ jmp(&done);
2846
2847 // Fix the zero case with the expected result.
2848 __ Bind(&is_zero);
2849 __ movl(out, Immediate(zero_value_result));
2850
2851 __ Bind(&done);
2852}
2853
2854void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2855 CreateLeadingZeroLocations(arena_, invoke);
2856}
2857
2858void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002859 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002860}
2861
2862void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2863 CreateLeadingZeroLocations(arena_, invoke);
2864}
2865
2866void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002867 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002868}
2869
Mark Mendell2d554792015-09-15 21:45:18 -04002870static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2871 LocationSummary* locations = new (arena) LocationSummary(invoke,
2872 LocationSummary::kNoCall,
2873 kIntrinsified);
2874 locations->SetInAt(0, Location::Any());
2875 locations->SetOut(Location::RequiresRegister());
2876}
2877
Aart Bikc5d47542016-01-27 17:00:35 -08002878static void GenTrailingZeros(X86_64Assembler* assembler,
2879 CodeGeneratorX86_64* codegen,
2880 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002881 LocationSummary* locations = invoke->GetLocations();
2882 Location src = locations->InAt(0);
2883 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2884
2885 int zero_value_result = is_long ? 64 : 32;
2886 if (invoke->InputAt(0)->IsConstant()) {
2887 // Evaluate this at compile time.
2888 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2889 if (value == 0) {
2890 value = zero_value_result;
2891 } else {
2892 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2893 }
Aart Bikc5d47542016-01-27 17:00:35 -08002894 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002895 return;
2896 }
2897
2898 // Handle the non-constant cases.
2899 if (src.IsRegister()) {
2900 if (is_long) {
2901 __ bsfq(out, src.AsRegister<CpuRegister>());
2902 } else {
2903 __ bsfl(out, src.AsRegister<CpuRegister>());
2904 }
2905 } else if (is_long) {
2906 DCHECK(src.IsDoubleStackSlot());
2907 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2908 } else {
2909 DCHECK(src.IsStackSlot());
2910 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2911 }
2912
2913 // BSF sets ZF if the input was zero, and the output is undefined.
2914 NearLabel done;
2915 __ j(kNotEqual, &done);
2916
2917 // Fix the zero case with the expected result.
2918 __ movl(out, Immediate(zero_value_result));
2919
2920 __ Bind(&done);
2921}
2922
2923void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2924 CreateTrailingZeroLocations(arena_, invoke);
2925}
2926
2927void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002928 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002929}
2930
2931void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2932 CreateTrailingZeroLocations(arena_, invoke);
2933}
2934
2935void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002936 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2937}
2938
Serguei Katkov288c7a82016-05-16 11:53:15 +06002939void IntrinsicLocationsBuilderX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2940 if (kEmitCompilerReadBarrier) {
2941 // Do not intrinsify this call with the read barrier configuration.
2942 return;
2943 }
2944 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2945 LocationSummary::kCallOnSlowPath,
2946 kIntrinsified);
2947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetOut(Location::SameAsFirstInput());
2949 locations->AddTemp(Location::RequiresRegister());
2950}
2951
2952void IntrinsicCodeGeneratorX86_64::VisitReferenceGetReferent(HInvoke* invoke) {
2953 DCHECK(!kEmitCompilerReadBarrier);
2954 LocationSummary* locations = invoke->GetLocations();
2955 X86_64Assembler* assembler = GetAssembler();
2956
2957 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
2958 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2959
2960 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
2961 codegen_->AddSlowPath(slow_path);
2962
2963 // Load ArtMethod first.
2964 HInvokeStaticOrDirect* invoke_direct = invoke->AsInvokeStaticOrDirect();
2965 DCHECK(invoke_direct != nullptr);
2966 Location temp_loc = codegen_->GenerateCalleeMethodStaticOrDirectCall(
2967 invoke_direct, locations->GetTemp(0));
2968 DCHECK(temp_loc.Equals(locations->GetTemp(0)));
2969 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
2970
2971 // Now get declaring class.
2972 __ movl(temp, Address(temp, ArtMethod::DeclaringClassOffset().Int32Value()));
2973
2974 uint32_t slow_path_flag_offset = codegen_->GetReferenceSlowFlagOffset();
2975 uint32_t disable_flag_offset = codegen_->GetReferenceDisableFlagOffset();
2976 DCHECK_NE(slow_path_flag_offset, 0u);
2977 DCHECK_NE(disable_flag_offset, 0u);
2978 DCHECK_NE(slow_path_flag_offset, disable_flag_offset);
2979
2980 // Check static flags preventing us for using intrinsic.
2981 if (slow_path_flag_offset == disable_flag_offset + 1) {
2982 __ cmpw(Address(temp, disable_flag_offset), Immediate(0));
2983 __ j(kNotEqual, slow_path->GetEntryLabel());
2984 } else {
2985 __ cmpb(Address(temp, disable_flag_offset), Immediate(0));
2986 __ j(kNotEqual, slow_path->GetEntryLabel());
2987 __ cmpb(Address(temp, slow_path_flag_offset), Immediate(0));
2988 __ j(kNotEqual, slow_path->GetEntryLabel());
2989 }
2990
2991 // Fast path.
2992 __ movl(out, Address(obj, mirror::Reference::ReferentOffset().Int32Value()));
2993 codegen_->MaybeRecordImplicitNullCheck(invoke);
2994 __ MaybeUnpoisonHeapReference(out);
2995 __ Bind(slow_path->GetExitLabel());
2996}
2997
Aart Bik2f9fcc92016-03-01 15:16:54 -08002998UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
2999UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003000
Aart Bikff7d89c2016-11-07 08:49:28 -08003001UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOf);
3002UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003003UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferAppend);
3004UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferLength);
3005UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferToString);
3006UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppend);
3007UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderLength);
3008UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003009
Aart Bik0e54c012016-03-04 12:08:31 -08003010// 1.8.
3011UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
3012UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
3013UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
3014UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
3015UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003016
Aart Bik2f9fcc92016-03-01 15:16:54 -08003017UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01003018
3019#undef __
3020
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003021} // namespace x86_64
3022} // namespace art