blob: a2545ee3d8c6e1798bee82f91299f0315555fb04 [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"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080022#include "art_method.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"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080027#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070028#include "intrinsics_utils.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#include "lock_word.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080030#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070031#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080032#include "mirror/reference.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080033#include "mirror/string.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080034#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080036#include "utils/x86_64/assembler_x86_64.h"
37#include "utils/x86_64/constants_x86_64.h"
38
39namespace art {
40
41namespace x86_64 {
42
Mark Mendellfb8d2792015-03-31 22:16:59 -040043IntrinsicLocationsBuilderX86_64::IntrinsicLocationsBuilderX86_64(CodeGeneratorX86_64* codegen)
44 : arena_(codegen->GetGraph()->GetArena()), codegen_(codegen) {
45}
46
Andreas Gampe71fb52f2014-12-29 17:43:08 -080047X86_64Assembler* IntrinsicCodeGeneratorX86_64::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010048 return down_cast<X86_64Assembler*>(codegen_->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -080049}
50
Andreas Gampe878d58c2015-01-15 23:24:00 -080051ArenaAllocator* IntrinsicCodeGeneratorX86_64::GetAllocator() {
Andreas Gampe71fb52f2014-12-29 17:43:08 -080052 return codegen_->GetGraph()->GetArena();
53}
54
55bool IntrinsicLocationsBuilderX86_64::TryDispatch(HInvoke* invoke) {
56 Dispatch(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +000057 LocationSummary* res = invoke->GetLocations();
58 if (res == nullptr) {
59 return false;
60 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000061 return res->Intrinsified();
Andreas Gampe71fb52f2014-12-29 17:43:08 -080062}
63
Roland Levillainec525fc2015-04-28 15:50:20 +010064static void MoveArguments(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010065 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010066 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Andreas Gampe71fb52f2014-12-29 17:43:08 -080067}
68
Andreas Gampe85b62f22015-09-09 13:15:38 -070069using IntrinsicSlowPathX86_64 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86_64>;
Andreas Gampe71fb52f2014-12-29 17:43:08 -080070
Roland Levillain0b671c02016-08-19 12:02:34 +010071// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
72#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())-> // NOLINT
73
74// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
75class ReadBarrierSystemArrayCopySlowPathX86_64 : public SlowPathCode {
76 public:
77 explicit ReadBarrierSystemArrayCopySlowPathX86_64(HInstruction* instruction)
78 : SlowPathCode(instruction) {
79 DCHECK(kEmitCompilerReadBarrier);
80 DCHECK(kUseBakerReadBarrier);
81 }
82
83 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
84 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
85 LocationSummary* locations = instruction_->GetLocations();
86 DCHECK(locations->CanCall());
87 DCHECK(instruction_->IsInvokeStaticOrDirect())
88 << "Unexpected instruction in read barrier arraycopy slow path: "
89 << instruction_->DebugName();
90 DCHECK(instruction_->GetLocations()->Intrinsified());
91 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
92
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010093 int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +010094
95 CpuRegister src_curr_addr = locations->GetTemp(0).AsRegister<CpuRegister>();
96 CpuRegister dst_curr_addr = locations->GetTemp(1).AsRegister<CpuRegister>();
97 CpuRegister src_stop_addr = locations->GetTemp(2).AsRegister<CpuRegister>();
98
99 __ Bind(GetEntryLabel());
100 NearLabel loop;
101 __ Bind(&loop);
102 __ movl(CpuRegister(TMP), Address(src_curr_addr, 0));
103 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
104 // TODO: Inline the mark bit check before calling the runtime?
105 // TMP = ReadBarrier::Mark(TMP);
106 // No need to save live registers; it's taken care of by the
107 // entrypoint. Also, there is no need to update the stack mask,
108 // as this runtime call will not trigger a garbage collection.
Roland Levillain97c46462017-05-11 14:04:03 +0100109 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86_64PointerSize>(TMP);
Roland Levillain0b671c02016-08-19 12:02:34 +0100110 // This runtime call does not require a stack map.
111 x86_64_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
112 __ MaybePoisonHeapReference(CpuRegister(TMP));
113 __ movl(Address(dst_curr_addr, 0), CpuRegister(TMP));
114 __ addl(src_curr_addr, Immediate(element_size));
115 __ addl(dst_curr_addr, Immediate(element_size));
116 __ cmpl(src_curr_addr, src_stop_addr);
117 __ j(kNotEqual, &loop);
118 __ jmp(GetExitLabel());
119 }
120
121 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86_64"; }
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86_64);
125};
126
127#undef __
128
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800129#define __ assembler->
130
131static void CreateFPToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
132 LocationSummary* locations = new (arena) LocationSummary(invoke,
133 LocationSummary::kNoCall,
134 kIntrinsified);
135 locations->SetInAt(0, Location::RequiresFpuRegister());
136 locations->SetOut(Location::RequiresRegister());
137}
138
139static void CreateIntToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
140 LocationSummary* locations = new (arena) LocationSummary(invoke,
141 LocationSummary::kNoCall,
142 kIntrinsified);
143 locations->SetInAt(0, Location::RequiresRegister());
144 locations->SetOut(Location::RequiresFpuRegister());
145}
146
147static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
148 Location input = locations->InAt(0);
149 Location output = locations->Out();
150 __ movd(output.AsRegister<CpuRegister>(), input.AsFpuRegister<XmmRegister>(), is64bit);
151}
152
153static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
154 Location input = locations->InAt(0);
155 Location output = locations->Out();
156 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<CpuRegister>(), is64bit);
157}
158
159void IntrinsicLocationsBuilderX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
160 CreateFPToIntLocations(arena_, invoke);
161}
162void IntrinsicLocationsBuilderX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
163 CreateIntToFPLocations(arena_, invoke);
164}
165
166void IntrinsicCodeGeneratorX86_64::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000167 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800168}
169void IntrinsicCodeGeneratorX86_64::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000170 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800171}
172
173void IntrinsicLocationsBuilderX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
174 CreateFPToIntLocations(arena_, invoke);
175}
176void IntrinsicLocationsBuilderX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
177 CreateIntToFPLocations(arena_, invoke);
178}
179
180void IntrinsicCodeGeneratorX86_64::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000181 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800182}
183void IntrinsicCodeGeneratorX86_64::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000184 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800185}
186
187static void CreateIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
188 LocationSummary* locations = new (arena) LocationSummary(invoke,
189 LocationSummary::kNoCall,
190 kIntrinsified);
191 locations->SetInAt(0, Location::RequiresRegister());
192 locations->SetOut(Location::SameAsFirstInput());
193}
194
195static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100196 DataType::Type size,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800197 X86_64Assembler* assembler) {
198 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
199
200 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100201 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800202 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
203 __ bswapl(out);
204 __ sarl(out, Immediate(16));
205 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100206 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800207 __ bswapl(out);
208 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100209 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800210 __ bswapq(out);
211 break;
212 default:
213 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
214 UNREACHABLE();
215 }
216}
217
218void IntrinsicLocationsBuilderX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
219 CreateIntToIntLocations(arena_, invoke);
220}
221
222void IntrinsicCodeGeneratorX86_64::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100223 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800224}
225
226void IntrinsicLocationsBuilderX86_64::VisitLongReverseBytes(HInvoke* invoke) {
227 CreateIntToIntLocations(arena_, invoke);
228}
229
230void IntrinsicCodeGeneratorX86_64::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100231 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800232}
233
234void IntrinsicLocationsBuilderX86_64::VisitShortReverseBytes(HInvoke* invoke) {
235 CreateIntToIntLocations(arena_, invoke);
236}
237
238void IntrinsicCodeGeneratorX86_64::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100239 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800240}
241
242
243// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
244// need is 64b.
245
246static void CreateFloatToFloatPlusTemps(ArenaAllocator* arena, HInvoke* invoke) {
247 // TODO: Enable memory operations when the assembler supports them.
248 LocationSummary* locations = new (arena) LocationSummary(invoke,
249 LocationSummary::kNoCall,
250 kIntrinsified);
251 locations->SetInAt(0, Location::RequiresFpuRegister());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800252 locations->SetOut(Location::SameAsFirstInput());
Mark Mendellf55c3e02015-03-26 21:07:46 -0400253 locations->AddTemp(Location::RequiresFpuRegister()); // FP reg to hold mask.
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800254}
255
Mark Mendell39dcf552015-04-09 20:42:42 -0400256static void MathAbsFP(LocationSummary* locations,
257 bool is64bit,
258 X86_64Assembler* assembler,
259 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800260 Location output = locations->Out();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800261
Mark Mendellcfa410b2015-05-25 16:02:44 -0400262 DCHECK(output.IsFpuRegister());
263 XmmRegister xmm_temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800264
Mark Mendellcfa410b2015-05-25 16:02:44 -0400265 // TODO: Can mask directly with constant area using pand if we can guarantee
266 // that the literal is aligned on a 16 byte boundary. This will avoid a
267 // temporary.
268 if (is64bit) {
269 __ movsd(xmm_temp, codegen->LiteralInt64Address(INT64_C(0x7FFFFFFFFFFFFFFF)));
270 __ andpd(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800271 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400272 __ movss(xmm_temp, codegen->LiteralInt32Address(INT32_C(0x7FFFFFFF)));
273 __ andps(output.AsFpuRegister<XmmRegister>(), xmm_temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800274 }
275}
276
277void IntrinsicLocationsBuilderX86_64::VisitMathAbsDouble(HInvoke* invoke) {
278 CreateFloatToFloatPlusTemps(arena_, invoke);
279}
280
281void IntrinsicCodeGeneratorX86_64::VisitMathAbsDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000282 MathAbsFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800283}
284
285void IntrinsicLocationsBuilderX86_64::VisitMathAbsFloat(HInvoke* invoke) {
286 CreateFloatToFloatPlusTemps(arena_, invoke);
287}
288
289void IntrinsicCodeGeneratorX86_64::VisitMathAbsFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000290 MathAbsFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800291}
292
293static void CreateIntToIntPlusTemp(ArenaAllocator* arena, HInvoke* invoke) {
294 LocationSummary* locations = new (arena) LocationSummary(invoke,
295 LocationSummary::kNoCall,
296 kIntrinsified);
297 locations->SetInAt(0, Location::RequiresRegister());
298 locations->SetOut(Location::SameAsFirstInput());
299 locations->AddTemp(Location::RequiresRegister());
300}
301
302static void GenAbsInteger(LocationSummary* locations, bool is64bit, X86_64Assembler* assembler) {
303 Location output = locations->Out();
304 CpuRegister out = output.AsRegister<CpuRegister>();
305 CpuRegister mask = locations->GetTemp(0).AsRegister<CpuRegister>();
306
307 if (is64bit) {
308 // Create mask.
309 __ movq(mask, out);
310 __ sarq(mask, Immediate(63));
311 // Add mask.
312 __ addq(out, mask);
313 __ xorq(out, mask);
314 } else {
315 // Create mask.
316 __ movl(mask, out);
317 __ sarl(mask, Immediate(31));
318 // Add mask.
319 __ addl(out, mask);
320 __ xorl(out, mask);
321 }
322}
323
324void IntrinsicLocationsBuilderX86_64::VisitMathAbsInt(HInvoke* invoke) {
325 CreateIntToIntPlusTemp(arena_, invoke);
326}
327
328void IntrinsicCodeGeneratorX86_64::VisitMathAbsInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000329 GenAbsInteger(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800330}
331
332void IntrinsicLocationsBuilderX86_64::VisitMathAbsLong(HInvoke* invoke) {
333 CreateIntToIntPlusTemp(arena_, invoke);
334}
335
336void IntrinsicCodeGeneratorX86_64::VisitMathAbsLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000337 GenAbsInteger(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800338}
339
Mark Mendell39dcf552015-04-09 20:42:42 -0400340static void GenMinMaxFP(LocationSummary* locations,
341 bool is_min,
342 bool is_double,
343 X86_64Assembler* assembler,
344 CodeGeneratorX86_64* codegen) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800345 Location op1_loc = locations->InAt(0);
346 Location op2_loc = locations->InAt(1);
347 Location out_loc = locations->Out();
348 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
349
350 // Shortcut for same input locations.
351 if (op1_loc.Equals(op2_loc)) {
352 DCHECK(out_loc.Equals(op1_loc));
353 return;
354 }
355
356 // (out := op1)
357 // out <=? op2
358 // if Nan jmp Nan_label
359 // if out is min jmp done
360 // if op2 is min jmp op2_label
361 // handle -0/+0
362 // jmp done
363 // Nan_label:
364 // out := NaN
365 // op2_label:
366 // out := op2
367 // done:
368 //
369 // This removes one jmp, but needs to copy one input (op1) to out.
370 //
Mark Mendellf55c3e02015-03-26 21:07:46 -0400371 // TODO: This is straight from Quick. Make NaN an out-of-line slowpath?
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800372
373 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
374
Mark Mendell0c9497d2015-08-21 09:30:05 -0400375 NearLabel nan, done, op2_label;
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800376 if (is_double) {
377 __ ucomisd(out, op2);
378 } else {
379 __ ucomiss(out, op2);
380 }
381
382 __ j(Condition::kParityEven, &nan);
383
384 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
385 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
386
387 // Handle 0.0/-0.0.
388 if (is_min) {
389 if (is_double) {
390 __ orpd(out, op2);
391 } else {
392 __ orps(out, op2);
393 }
394 } else {
395 if (is_double) {
396 __ andpd(out, op2);
397 } else {
398 __ andps(out, op2);
399 }
400 }
401 __ jmp(&done);
402
403 // NaN handling.
404 __ Bind(&nan);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800405 if (is_double) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400406 __ movsd(out, codegen->LiteralInt64Address(INT64_C(0x7FF8000000000000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800407 } else {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400408 __ movss(out, codegen->LiteralInt32Address(INT32_C(0x7FC00000)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800409 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800410 __ jmp(&done);
411
412 // out := op2;
413 __ Bind(&op2_label);
414 if (is_double) {
415 __ movsd(out, op2);
416 } else {
417 __ movss(out, op2);
418 }
419
420 // Done.
421 __ Bind(&done);
422}
423
Mark Mendellf55c3e02015-03-26 21:07:46 -0400424static void CreateFPFPToFP(ArenaAllocator* arena, HInvoke* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800425 LocationSummary* locations = new (arena) LocationSummary(invoke,
426 LocationSummary::kNoCall,
427 kIntrinsified);
428 locations->SetInAt(0, Location::RequiresFpuRegister());
429 locations->SetInAt(1, Location::RequiresFpuRegister());
430 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
431 // the second input to be the output (we can simply swap inputs).
432 locations->SetOut(Location::SameAsFirstInput());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800433}
434
435void IntrinsicLocationsBuilderX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400436 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800437}
438
439void IntrinsicCodeGeneratorX86_64::VisitMathMinDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000440 GenMinMaxFP(
441 invoke->GetLocations(), /* is_min */ true, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800442}
443
444void IntrinsicLocationsBuilderX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400445 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800446}
447
448void IntrinsicCodeGeneratorX86_64::VisitMathMinFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000449 GenMinMaxFP(
450 invoke->GetLocations(), /* is_min */ true, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800451}
452
453void IntrinsicLocationsBuilderX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400454 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800455}
456
457void IntrinsicCodeGeneratorX86_64::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000458 GenMinMaxFP(
459 invoke->GetLocations(), /* is_min */ false, /* is_double */ true, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800460}
461
462void IntrinsicLocationsBuilderX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Mark Mendellf55c3e02015-03-26 21:07:46 -0400463 CreateFPFPToFP(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800464}
465
466void IntrinsicCodeGeneratorX86_64::VisitMathMaxFloatFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000467 GenMinMaxFP(
468 invoke->GetLocations(), /* is_min */ false, /* is_double */ false, GetAssembler(), codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800469}
470
471static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
472 X86_64Assembler* assembler) {
473 Location op1_loc = locations->InAt(0);
474 Location op2_loc = locations->InAt(1);
475
476 // Shortcut for same input locations.
477 if (op1_loc.Equals(op2_loc)) {
478 // Can return immediately, as op1_loc == out_loc.
479 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
480 // a copy here.
481 DCHECK(locations->Out().Equals(op1_loc));
482 return;
483 }
484
485 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
486 CpuRegister op2 = op2_loc.AsRegister<CpuRegister>();
487
488 // (out := op1)
489 // out <=? op2
490 // if out is min jmp done
491 // out := op2
492 // done:
493
494 if (is_long) {
495 __ cmpq(out, op2);
496 } else {
497 __ cmpl(out, op2);
498 }
499
500 __ cmov(is_min ? Condition::kGreater : Condition::kLess, out, op2, is_long);
501}
502
503static void CreateIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
504 LocationSummary* locations = new (arena) LocationSummary(invoke,
505 LocationSummary::kNoCall,
506 kIntrinsified);
507 locations->SetInAt(0, Location::RequiresRegister());
508 locations->SetInAt(1, Location::RequiresRegister());
509 locations->SetOut(Location::SameAsFirstInput());
510}
511
512void IntrinsicLocationsBuilderX86_64::VisitMathMinIntInt(HInvoke* invoke) {
513 CreateIntIntToIntLocations(arena_, invoke);
514}
515
516void IntrinsicCodeGeneratorX86_64::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000517 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800518}
519
520void IntrinsicLocationsBuilderX86_64::VisitMathMinLongLong(HInvoke* invoke) {
521 CreateIntIntToIntLocations(arena_, invoke);
522}
523
524void IntrinsicCodeGeneratorX86_64::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000525 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800526}
527
528void IntrinsicLocationsBuilderX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
529 CreateIntIntToIntLocations(arena_, invoke);
530}
531
532void IntrinsicCodeGeneratorX86_64::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000533 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800534}
535
536void IntrinsicLocationsBuilderX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
537 CreateIntIntToIntLocations(arena_, invoke);
538}
539
540void IntrinsicCodeGeneratorX86_64::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000541 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800542}
543
544static void CreateFPToFPLocations(ArenaAllocator* arena, HInvoke* invoke) {
545 LocationSummary* locations = new (arena) LocationSummary(invoke,
546 LocationSummary::kNoCall,
547 kIntrinsified);
548 locations->SetInAt(0, Location::RequiresFpuRegister());
549 locations->SetOut(Location::RequiresFpuRegister());
550}
551
552void IntrinsicLocationsBuilderX86_64::VisitMathSqrt(HInvoke* invoke) {
553 CreateFPToFPLocations(arena_, invoke);
554}
555
556void IntrinsicCodeGeneratorX86_64::VisitMathSqrt(HInvoke* invoke) {
557 LocationSummary* locations = invoke->GetLocations();
558 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
559 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
560
561 GetAssembler()->sqrtsd(out, in);
562}
563
Mark Mendellfb8d2792015-03-31 22:16:59 -0400564static void InvokeOutOfLineIntrinsic(CodeGeneratorX86_64* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100565 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400566
567 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100568 codegen->GenerateStaticOrDirectCall(
569 invoke->AsInvokeStaticOrDirect(), Location::RegisterLocation(RDI));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400570
571 // Copy the result back to the expected output.
572 Location out = invoke->GetLocations()->Out();
573 if (out.IsValid()) {
574 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700575 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400576 }
577}
578
579static void CreateSSE41FPToFPLocations(ArenaAllocator* arena,
580 HInvoke* invoke,
581 CodeGeneratorX86_64* codegen) {
582 // Do we have instruction support?
583 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
584 CreateFPToFPLocations(arena, invoke);
585 return;
586 }
587
588 // We have to fall back to a call to the intrinsic.
589 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100590 LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400591 InvokeRuntimeCallingConvention calling_convention;
592 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
593 locations->SetOut(Location::FpuRegisterLocation(XMM0));
594 // Needs to be RDI for the invoke.
595 locations->AddTemp(Location::RegisterLocation(RDI));
596}
597
598static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86_64* codegen,
599 HInvoke* invoke,
600 X86_64Assembler* assembler,
601 int round_mode) {
602 LocationSummary* locations = invoke->GetLocations();
603 if (locations->WillCall()) {
604 InvokeOutOfLineIntrinsic(codegen, invoke);
605 } else {
606 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
607 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
608 __ roundsd(out, in, Immediate(round_mode));
609 }
610}
611
612void IntrinsicLocationsBuilderX86_64::VisitMathCeil(HInvoke* invoke) {
613 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
614}
615
616void IntrinsicCodeGeneratorX86_64::VisitMathCeil(HInvoke* invoke) {
617 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
618}
619
620void IntrinsicLocationsBuilderX86_64::VisitMathFloor(HInvoke* invoke) {
621 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
622}
623
624void IntrinsicCodeGeneratorX86_64::VisitMathFloor(HInvoke* invoke) {
625 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
626}
627
628void IntrinsicLocationsBuilderX86_64::VisitMathRint(HInvoke* invoke) {
629 CreateSSE41FPToFPLocations(arena_, invoke, codegen_);
630}
631
632void IntrinsicCodeGeneratorX86_64::VisitMathRint(HInvoke* invoke) {
633 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
634}
635
636static void CreateSSE41FPToIntLocations(ArenaAllocator* arena,
637 HInvoke* invoke,
638 CodeGeneratorX86_64* codegen) {
639 // Do we have instruction support?
640 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
641 LocationSummary* locations = new (arena) LocationSummary(invoke,
642 LocationSummary::kNoCall,
643 kIntrinsified);
644 locations->SetInAt(0, Location::RequiresFpuRegister());
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600645 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400646 locations->AddTemp(Location::RequiresFpuRegister());
Aart Bik349f3882016-08-02 15:40:56 -0700647 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400648 return;
649 }
650
651 // We have to fall back to a call to the intrinsic.
652 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100653 LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400654 InvokeRuntimeCallingConvention calling_convention;
655 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
656 locations->SetOut(Location::RegisterLocation(RAX));
657 // Needs to be RDI for the invoke.
658 locations->AddTemp(Location::RegisterLocation(RDI));
659}
660
661void IntrinsicLocationsBuilderX86_64::VisitMathRoundFloat(HInvoke* invoke) {
Aart Bik349f3882016-08-02 15:40:56 -0700662 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400663}
664
665void IntrinsicCodeGeneratorX86_64::VisitMathRoundFloat(HInvoke* invoke) {
666 LocationSummary* locations = invoke->GetLocations();
667 if (locations->WillCall()) {
668 InvokeOutOfLineIntrinsic(codegen_, invoke);
669 return;
670 }
671
Mark Mendellfb8d2792015-03-31 22:16:59 -0400672 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
673 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700674 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
675 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
676 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400677 X86_64Assembler* assembler = GetAssembler();
678
Aart Bik349f3882016-08-02 15:40:56 -0700679 // Since no direct x86 rounding instruction matches the required semantics,
680 // this intrinsic is implemented as follows:
681 // result = floor(in);
682 // if (in - result >= 0.5f)
683 // result = result + 1.0f;
684 __ movss(t2, in);
685 __ roundss(t1, in, Immediate(1));
686 __ subss(t2, t1);
687 __ comiss(t2, codegen_->LiteralFloatAddress(0.5f));
688 __ j(kBelow, &skip_incr);
689 __ addss(t1, codegen_->LiteralFloatAddress(1.0f));
690 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400691
Aart Bik349f3882016-08-02 15:40:56 -0700692 // Final conversion to an integer. Unfortunately this also does not have a
693 // direct x86 instruction, since NaN should map to 0 and large positive
694 // values need to be clipped to the extreme value.
695 codegen_->Load32BitValue(out, kPrimIntMax);
696 __ cvtsi2ss(t2, out);
697 __ comiss(t1, t2);
698 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
699 __ movl(out, Immediate(0)); // does not change flags
700 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
701 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400702 __ Bind(&done);
703}
704
705void IntrinsicLocationsBuilderX86_64::VisitMathRoundDouble(HInvoke* invoke) {
Aart Bik349f3882016-08-02 15:40:56 -0700706 CreateSSE41FPToIntLocations(arena_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400707}
708
709void IntrinsicCodeGeneratorX86_64::VisitMathRoundDouble(HInvoke* invoke) {
710 LocationSummary* locations = invoke->GetLocations();
711 if (locations->WillCall()) {
712 InvokeOutOfLineIntrinsic(codegen_, invoke);
713 return;
714 }
715
Mark Mendellfb8d2792015-03-31 22:16:59 -0400716 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
717 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Aart Bik349f3882016-08-02 15:40:56 -0700718 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
719 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
720 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400721 X86_64Assembler* assembler = GetAssembler();
722
Aart Bik349f3882016-08-02 15:40:56 -0700723 // Since no direct x86 rounding instruction matches the required semantics,
724 // this intrinsic is implemented as follows:
725 // result = floor(in);
726 // if (in - result >= 0.5)
727 // result = result + 1.0f;
728 __ movsd(t2, in);
729 __ roundsd(t1, in, Immediate(1));
730 __ subsd(t2, t1);
731 __ comisd(t2, codegen_->LiteralDoubleAddress(0.5));
732 __ j(kBelow, &skip_incr);
733 __ addsd(t1, codegen_->LiteralDoubleAddress(1.0f));
734 __ Bind(&skip_incr);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400735
Aart Bik349f3882016-08-02 15:40:56 -0700736 // Final conversion to an integer. Unfortunately this also does not have a
737 // direct x86 instruction, since NaN should map to 0 and large positive
738 // values need to be clipped to the extreme value.
Pavel Vyssotski9ca25712015-07-31 13:03:17 +0600739 codegen_->Load64BitValue(out, kPrimLongMax);
Aart Bik349f3882016-08-02 15:40:56 -0700740 __ cvtsi2sd(t2, out, /* is64bit */ true);
741 __ comisd(t1, t2);
742 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
743 __ movl(out, Immediate(0)); // does not change flags, implicit zero extension to 64-bit
744 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
745 __ cvttsd2si(out, t1, /* is64bit */ true);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400746 __ Bind(&done);
747}
748
Mark Mendella4f12202015-08-06 15:23:34 -0400749static void CreateFPToFPCallLocations(ArenaAllocator* arena,
750 HInvoke* invoke) {
751 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100752 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400753 kIntrinsified);
754 InvokeRuntimeCallingConvention calling_convention;
755 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
756 locations->SetOut(Location::FpuRegisterLocation(XMM0));
757
758 // We have to ensure that the native code doesn't clobber the XMM registers which are
759 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
760 // saved in the prologue and properly restored.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100761 for (FloatRegister fp_reg : non_volatile_xmm_regs) {
Mark Mendella4f12202015-08-06 15:23:34 -0400762 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
763 }
764}
765
766static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86_64* codegen,
767 QuickEntrypointEnum entry) {
768 LocationSummary* locations = invoke->GetLocations();
769 DCHECK(locations->WillCall());
770 DCHECK(invoke->IsInvokeStaticOrDirect());
Mark Mendella4f12202015-08-06 15:23:34 -0400771
Serban Constantinescuba45db02016-07-12 22:53:02 +0100772 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400773}
774
775void IntrinsicLocationsBuilderX86_64::VisitMathCos(HInvoke* invoke) {
776 CreateFPToFPCallLocations(arena_, invoke);
777}
778
779void IntrinsicCodeGeneratorX86_64::VisitMathCos(HInvoke* invoke) {
780 GenFPToFPCall(invoke, codegen_, kQuickCos);
781}
782
783void IntrinsicLocationsBuilderX86_64::VisitMathSin(HInvoke* invoke) {
784 CreateFPToFPCallLocations(arena_, invoke);
785}
786
787void IntrinsicCodeGeneratorX86_64::VisitMathSin(HInvoke* invoke) {
788 GenFPToFPCall(invoke, codegen_, kQuickSin);
789}
790
791void IntrinsicLocationsBuilderX86_64::VisitMathAcos(HInvoke* invoke) {
792 CreateFPToFPCallLocations(arena_, invoke);
793}
794
795void IntrinsicCodeGeneratorX86_64::VisitMathAcos(HInvoke* invoke) {
796 GenFPToFPCall(invoke, codegen_, kQuickAcos);
797}
798
799void IntrinsicLocationsBuilderX86_64::VisitMathAsin(HInvoke* invoke) {
800 CreateFPToFPCallLocations(arena_, invoke);
801}
802
803void IntrinsicCodeGeneratorX86_64::VisitMathAsin(HInvoke* invoke) {
804 GenFPToFPCall(invoke, codegen_, kQuickAsin);
805}
806
807void IntrinsicLocationsBuilderX86_64::VisitMathAtan(HInvoke* invoke) {
808 CreateFPToFPCallLocations(arena_, invoke);
809}
810
811void IntrinsicCodeGeneratorX86_64::VisitMathAtan(HInvoke* invoke) {
812 GenFPToFPCall(invoke, codegen_, kQuickAtan);
813}
814
815void IntrinsicLocationsBuilderX86_64::VisitMathCbrt(HInvoke* invoke) {
816 CreateFPToFPCallLocations(arena_, invoke);
817}
818
819void IntrinsicCodeGeneratorX86_64::VisitMathCbrt(HInvoke* invoke) {
820 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
821}
822
823void IntrinsicLocationsBuilderX86_64::VisitMathCosh(HInvoke* invoke) {
824 CreateFPToFPCallLocations(arena_, invoke);
825}
826
827void IntrinsicCodeGeneratorX86_64::VisitMathCosh(HInvoke* invoke) {
828 GenFPToFPCall(invoke, codegen_, kQuickCosh);
829}
830
831void IntrinsicLocationsBuilderX86_64::VisitMathExp(HInvoke* invoke) {
832 CreateFPToFPCallLocations(arena_, invoke);
833}
834
835void IntrinsicCodeGeneratorX86_64::VisitMathExp(HInvoke* invoke) {
836 GenFPToFPCall(invoke, codegen_, kQuickExp);
837}
838
839void IntrinsicLocationsBuilderX86_64::VisitMathExpm1(HInvoke* invoke) {
840 CreateFPToFPCallLocations(arena_, invoke);
841}
842
843void IntrinsicCodeGeneratorX86_64::VisitMathExpm1(HInvoke* invoke) {
844 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
845}
846
847void IntrinsicLocationsBuilderX86_64::VisitMathLog(HInvoke* invoke) {
848 CreateFPToFPCallLocations(arena_, invoke);
849}
850
851void IntrinsicCodeGeneratorX86_64::VisitMathLog(HInvoke* invoke) {
852 GenFPToFPCall(invoke, codegen_, kQuickLog);
853}
854
855void IntrinsicLocationsBuilderX86_64::VisitMathLog10(HInvoke* invoke) {
856 CreateFPToFPCallLocations(arena_, invoke);
857}
858
859void IntrinsicCodeGeneratorX86_64::VisitMathLog10(HInvoke* invoke) {
860 GenFPToFPCall(invoke, codegen_, kQuickLog10);
861}
862
863void IntrinsicLocationsBuilderX86_64::VisitMathSinh(HInvoke* invoke) {
864 CreateFPToFPCallLocations(arena_, invoke);
865}
866
867void IntrinsicCodeGeneratorX86_64::VisitMathSinh(HInvoke* invoke) {
868 GenFPToFPCall(invoke, codegen_, kQuickSinh);
869}
870
871void IntrinsicLocationsBuilderX86_64::VisitMathTan(HInvoke* invoke) {
872 CreateFPToFPCallLocations(arena_, invoke);
873}
874
875void IntrinsicCodeGeneratorX86_64::VisitMathTan(HInvoke* invoke) {
876 GenFPToFPCall(invoke, codegen_, kQuickTan);
877}
878
879void IntrinsicLocationsBuilderX86_64::VisitMathTanh(HInvoke* invoke) {
880 CreateFPToFPCallLocations(arena_, invoke);
881}
882
883void IntrinsicCodeGeneratorX86_64::VisitMathTanh(HInvoke* invoke) {
884 GenFPToFPCall(invoke, codegen_, kQuickTanh);
885}
886
887static void CreateFPFPToFPCallLocations(ArenaAllocator* arena,
888 HInvoke* invoke) {
889 LocationSummary* locations = new (arena) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +0100890 LocationSummary::kCallOnMainOnly,
Mark Mendella4f12202015-08-06 15:23:34 -0400891 kIntrinsified);
892 InvokeRuntimeCallingConvention calling_convention;
893 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
894 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
895 locations->SetOut(Location::FpuRegisterLocation(XMM0));
896
897 // We have to ensure that the native code doesn't clobber the XMM registers which are
898 // non-volatile for ART, but volatile for Native calls. This will ensure that they are
899 // saved in the prologue and properly restored.
Vladimir Marko7d157fc2017-05-10 16:29:23 +0100900 for (FloatRegister fp_reg : non_volatile_xmm_regs) {
Mark Mendella4f12202015-08-06 15:23:34 -0400901 locations->AddTemp(Location::FpuRegisterLocation(fp_reg));
902 }
903}
904
905void IntrinsicLocationsBuilderX86_64::VisitMathAtan2(HInvoke* invoke) {
906 CreateFPFPToFPCallLocations(arena_, invoke);
907}
908
909void IntrinsicCodeGeneratorX86_64::VisitMathAtan2(HInvoke* invoke) {
910 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
911}
912
913void IntrinsicLocationsBuilderX86_64::VisitMathHypot(HInvoke* invoke) {
914 CreateFPFPToFPCallLocations(arena_, invoke);
915}
916
917void IntrinsicCodeGeneratorX86_64::VisitMathHypot(HInvoke* invoke) {
918 GenFPToFPCall(invoke, codegen_, kQuickHypot);
919}
920
921void IntrinsicLocationsBuilderX86_64::VisitMathNextAfter(HInvoke* invoke) {
922 CreateFPFPToFPCallLocations(arena_, invoke);
923}
924
925void IntrinsicCodeGeneratorX86_64::VisitMathNextAfter(HInvoke* invoke) {
926 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
927}
928
Mark Mendell6bc53a92015-07-01 14:26:52 -0400929void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
930 // Check to see if we have known failures that will cause us to have to bail out
931 // to the runtime, and just generate the runtime call directly.
932 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
933 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
934
935 // The positions must be non-negative.
936 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
937 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
938 // We will have to fail anyways.
939 return;
940 }
941
942 // The length must be > 0.
943 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
944 if (length != nullptr) {
945 int32_t len = length->GetValue();
946 if (len < 0) {
947 // Just call as normal.
948 return;
949 }
950 }
951
952 LocationSummary* locations = new (arena_) LocationSummary(invoke,
953 LocationSummary::kCallOnSlowPath,
954 kIntrinsified);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100955 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
Mark Mendell6bc53a92015-07-01 14:26:52 -0400956 locations->SetInAt(0, Location::RequiresRegister());
957 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
958 locations->SetInAt(2, Location::RequiresRegister());
959 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
960 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
961
962 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
963 locations->AddTemp(Location::RegisterLocation(RSI));
964 locations->AddTemp(Location::RegisterLocation(RDI));
965 locations->AddTemp(Location::RegisterLocation(RCX));
966}
967
968static void CheckPosition(X86_64Assembler* assembler,
969 Location pos,
970 CpuRegister input,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100971 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700972 SlowPathCode* slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100973 CpuRegister temp,
974 bool length_is_input_length = false) {
975 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400976 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
977
978 if (pos.IsConstant()) {
979 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
980 if (pos_const == 0) {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100981 if (!length_is_input_length) {
982 // Check that length(input) >= length.
983 if (length.IsConstant()) {
984 __ cmpl(Address(input, length_offset),
985 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
986 } else {
987 __ cmpl(Address(input, length_offset), length.AsRegister<CpuRegister>());
988 }
989 __ j(kLess, slow_path->GetEntryLabel());
990 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400991 } else {
992 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100993 __ movl(temp, Address(input, length_offset));
994 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400995 __ j(kLess, slow_path->GetEntryLabel());
996
997 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +0100998 if (length.IsConstant()) {
999 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1000 } else {
1001 __ cmpl(temp, length.AsRegister<CpuRegister>());
1002 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001003 __ j(kLess, slow_path->GetEntryLabel());
1004 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001005 } else if (length_is_input_length) {
1006 // The only way the copy can succeed is if pos is zero.
1007 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1008 __ testl(pos_reg, pos_reg);
1009 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001010 } else {
1011 // Check that pos >= 0.
1012 CpuRegister pos_reg = pos.AsRegister<CpuRegister>();
1013 __ testl(pos_reg, pos_reg);
1014 __ j(kLess, slow_path->GetEntryLabel());
1015
1016 // Check that pos <= length(input).
1017 __ cmpl(Address(input, length_offset), pos_reg);
1018 __ j(kLess, slow_path->GetEntryLabel());
1019
1020 // Check that (length(input) - pos) >= length.
1021 __ movl(temp, Address(input, length_offset));
1022 __ subl(temp, pos_reg);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001023 if (length.IsConstant()) {
1024 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1025 } else {
1026 __ cmpl(temp, length.AsRegister<CpuRegister>());
1027 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001028 __ j(kLess, slow_path->GetEntryLabel());
1029 }
1030}
1031
1032void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopyChar(HInvoke* invoke) {
1033 X86_64Assembler* assembler = GetAssembler();
1034 LocationSummary* locations = invoke->GetLocations();
1035
1036 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001037 Location src_pos = locations->InAt(1);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001038 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001039 Location dest_pos = locations->InAt(3);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001040 Location length = locations->InAt(4);
1041
1042 // Temporaries that we need for MOVSW.
1043 CpuRegister src_base = locations->GetTemp(0).AsRegister<CpuRegister>();
1044 DCHECK_EQ(src_base.AsRegister(), RSI);
1045 CpuRegister dest_base = locations->GetTemp(1).AsRegister<CpuRegister>();
1046 DCHECK_EQ(dest_base.AsRegister(), RDI);
1047 CpuRegister count = locations->GetTemp(2).AsRegister<CpuRegister>();
1048 DCHECK_EQ(count.AsRegister(), RCX);
1049
Andreas Gampe85b62f22015-09-09 13:15:38 -07001050 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001051 codegen_->AddSlowPath(slow_path);
1052
1053 // Bail out if the source and destination are the same.
1054 __ cmpl(src, dest);
1055 __ j(kEqual, slow_path->GetEntryLabel());
1056
1057 // Bail out if the source is null.
1058 __ testl(src, src);
1059 __ j(kEqual, slow_path->GetEntryLabel());
1060
1061 // Bail out if the destination is null.
1062 __ testl(dest, dest);
1063 __ j(kEqual, slow_path->GetEntryLabel());
1064
1065 // If the length is negative, bail out.
1066 // We have already checked in the LocationsBuilder for the constant case.
1067 if (!length.IsConstant()) {
1068 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
1069 __ j(kLess, slow_path->GetEntryLabel());
1070 }
1071
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001072 // Validity checks: source. Use src_base as a temporary register.
1073 CheckPosition(assembler, src_pos, src, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001074
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001075 // Validity checks: dest. Use src_base as a temporary register.
1076 CheckPosition(assembler, dest_pos, dest, length, slow_path, src_base);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001077
Mark Mendell6bc53a92015-07-01 14:26:52 -04001078 // We need the count in RCX.
1079 if (length.IsConstant()) {
1080 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1081 } else {
1082 __ movl(count, length.AsRegister<CpuRegister>());
1083 }
1084
Mark Mendell6bc53a92015-07-01 14:26:52 -04001085 // Okay, everything checks out. Finally time to do the copy.
1086 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001087 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001088 DCHECK_EQ(char_size, 2u);
1089
1090 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1091
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001092 if (src_pos.IsConstant()) {
1093 int32_t src_pos_const = src_pos.GetConstant()->AsIntConstant()->GetValue();
1094 __ leal(src_base, Address(src, char_size * src_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001095 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001096 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001097 ScaleFactor::TIMES_2, data_offset));
1098 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001099 if (dest_pos.IsConstant()) {
1100 int32_t dest_pos_const = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1101 __ leal(dest_base, Address(dest, char_size * dest_pos_const + data_offset));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001102 } else {
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001103 __ leal(dest_base, Address(dest, dest_pos.AsRegister<CpuRegister>(),
Mark Mendell6bc53a92015-07-01 14:26:52 -04001104 ScaleFactor::TIMES_2, data_offset));
1105 }
1106
1107 // Do the move.
1108 __ rep_movsw();
1109
1110 __ Bind(slow_path->GetExitLabel());
1111}
1112
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001113
1114void IntrinsicLocationsBuilderX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001115 // The only read barrier implementation supporting the
1116 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1117 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain3d312422016-06-23 13:53:42 +01001118 return;
1119 }
1120
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001121 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001122}
1123
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001124// Compute base source address, base destination address, and end
1125// source address for the System.arraycopy intrinsic in `src_base`,
1126// `dst_base` and `src_end` respectively.
1127static void GenSystemArrayCopyAddresses(X86_64Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001128 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001129 const CpuRegister& src,
1130 const Location& src_pos,
1131 const CpuRegister& dst,
1132 const Location& dst_pos,
1133 const Location& copy_length,
1134 const CpuRegister& src_base,
1135 const CpuRegister& dst_base,
1136 const CpuRegister& src_end) {
1137 // This routine is only used by the SystemArrayCopy intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001138 DCHECK_EQ(type, DataType::Type::kReference);
1139 const int32_t element_size = DataType::Size(type);
1140 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001141 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
1142
1143 if (src_pos.IsConstant()) {
1144 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1145 __ leal(src_base, Address(src, element_size * constant + data_offset));
1146 } else {
1147 __ leal(src_base, Address(src, src_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
1148 }
1149
1150 if (dst_pos.IsConstant()) {
1151 int32_t constant = dst_pos.GetConstant()->AsIntConstant()->GetValue();
1152 __ leal(dst_base, Address(dst, element_size * constant + data_offset));
1153 } else {
1154 __ leal(dst_base, Address(dst, dst_pos.AsRegister<CpuRegister>(), scale_factor, data_offset));
1155 }
1156
1157 if (copy_length.IsConstant()) {
1158 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
1159 __ leal(src_end, Address(src_base, element_size * constant));
1160 } else {
1161 __ leal(src_end, Address(src_base, copy_length.AsRegister<CpuRegister>(), scale_factor, 0));
1162 }
1163}
1164
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001165void IntrinsicCodeGeneratorX86_64::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01001166 // The only read barrier implementation supporting the
1167 // SystemArrayCopy intrinsic is the Baker-style read barriers.
1168 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01001169
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001170 X86_64Assembler* assembler = GetAssembler();
1171 LocationSummary* locations = invoke->GetLocations();
1172
1173 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1174 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
1175 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
1176 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01001177 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001178
1179 CpuRegister src = locations->InAt(0).AsRegister<CpuRegister>();
1180 Location src_pos = locations->InAt(1);
1181 CpuRegister dest = locations->InAt(2).AsRegister<CpuRegister>();
1182 Location dest_pos = locations->InAt(3);
1183 Location length = locations->InAt(4);
Roland Levillain0b671c02016-08-19 12:02:34 +01001184 Location temp1_loc = locations->GetTemp(0);
1185 CpuRegister temp1 = temp1_loc.AsRegister<CpuRegister>();
1186 Location temp2_loc = locations->GetTemp(1);
1187 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
1188 Location temp3_loc = locations->GetTemp(2);
1189 CpuRegister temp3 = temp3_loc.AsRegister<CpuRegister>();
1190 Location TMP_loc = Location::RegisterLocation(TMP);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001191
Roland Levillain0b671c02016-08-19 12:02:34 +01001192 SlowPathCode* intrinsic_slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
1193 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001194
Roland Levillainebea3d22016-04-12 15:42:57 +01001195 NearLabel conditions_on_positions_validated;
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001196 SystemArrayCopyOptimizations optimizations(invoke);
1197
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001198 // If source and destination are the same, we go to slow path if we need to do
1199 // forward copying.
1200 if (src_pos.IsConstant()) {
1201 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
1202 if (dest_pos.IsConstant()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001203 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1204 if (optimizations.GetDestinationIsSource()) {
1205 // Checked when building locations.
1206 DCHECK_GE(src_pos_constant, dest_pos_constant);
1207 } else if (src_pos_constant < dest_pos_constant) {
1208 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001209 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001210 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001211 } else {
1212 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001213 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001214 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001215 }
1216 __ cmpl(dest_pos.AsRegister<CpuRegister>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001217 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001218 }
1219 } else {
1220 if (!optimizations.GetDestinationIsSource()) {
Nicolas Geoffray9f65db82016-07-07 12:07:42 +01001221 __ cmpl(src, dest);
Roland Levillainebea3d22016-04-12 15:42:57 +01001222 __ j(kNotEqual, &conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001223 }
1224 if (dest_pos.IsConstant()) {
1225 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
1226 __ cmpl(src_pos.AsRegister<CpuRegister>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01001227 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001228 } else {
1229 __ cmpl(src_pos.AsRegister<CpuRegister>(), dest_pos.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001230 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001231 }
1232 }
1233
Roland Levillainebea3d22016-04-12 15:42:57 +01001234 __ Bind(&conditions_on_positions_validated);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001235
1236 if (!optimizations.GetSourceIsNotNull()) {
1237 // Bail out if the source is null.
1238 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01001239 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001240 }
1241
1242 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
1243 // Bail out if the destination is null.
1244 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01001245 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001246 }
1247
1248 // If the length is negative, bail out.
1249 // We have already checked in the LocationsBuilder for the constant case.
1250 if (!length.IsConstant() &&
1251 !optimizations.GetCountIsSourceLength() &&
1252 !optimizations.GetCountIsDestinationLength()) {
1253 __ testl(length.AsRegister<CpuRegister>(), length.AsRegister<CpuRegister>());
Roland Levillain0b671c02016-08-19 12:02:34 +01001254 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001255 }
1256
1257 // Validity checks: source.
1258 CheckPosition(assembler,
1259 src_pos,
1260 src,
1261 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001262 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001263 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001264 optimizations.GetCountIsSourceLength());
1265
1266 // Validity checks: dest.
1267 CheckPosition(assembler,
1268 dest_pos,
1269 dest,
1270 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01001271 intrinsic_slow_path,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001272 temp1,
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001273 optimizations.GetCountIsDestinationLength());
1274
1275 if (!optimizations.GetDoesNotNeedTypeCheck()) {
1276 // Check whether all elements of the source array are assignable to the component
1277 // type of the destination array. We do two checks: the classes are the same,
1278 // or the destination is Object[]. If none of these checks succeed, we go to the
1279 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01001280
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001281 bool did_unpoison = false;
Roland Levillain0b671c02016-08-19 12:02:34 +01001282 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1283 // /* HeapReference<Class> */ temp1 = dest->klass_
1284 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001285 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001286 // Register `temp1` is not trashed by the read barrier emitted
1287 // by GenerateFieldLoadWithBakerReadBarrier below, as that
1288 // method produces a call to a ReadBarrierMarkRegX entry point,
1289 // which saves all potentially live registers, including
1290 // temporaries such a `temp1`.
1291 // /* HeapReference<Class> */ temp2 = src->klass_
1292 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001293 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001294 // If heap poisoning is enabled, `temp1` and `temp2` have been
1295 // unpoisoned by the the previous calls to
1296 // GenerateFieldLoadWithBakerReadBarrier.
1297 } else {
1298 // /* HeapReference<Class> */ temp1 = dest->klass_
1299 __ movl(temp1, Address(dest, class_offset));
1300 // /* HeapReference<Class> */ temp2 = src->klass_
1301 __ movl(temp2, Address(src, class_offset));
1302 if (!optimizations.GetDestinationIsNonPrimitiveArray() ||
1303 !optimizations.GetSourceIsNonPrimitiveArray()) {
1304 // One or two of the references need to be unpoisoned. Unpoison them
1305 // both to make the identity check valid.
1306 __ MaybeUnpoisonHeapReference(temp1);
1307 __ MaybeUnpoisonHeapReference(temp2);
1308 did_unpoison = true;
1309 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001310 }
1311
1312 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
1313 // Bail out if the destination is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001314 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1315 // /* HeapReference<Class> */ TMP = temp1->component_type_
1316 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001317 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001318 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1319 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1320 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1321 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1322 } else {
1323 // /* HeapReference<Class> */ TMP = temp1->component_type_
1324 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1325 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1326 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1327 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1328 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001329 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001330 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001331 }
1332
1333 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1334 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001335 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1336 // For the same reason given earlier, `temp1` is not trashed by the
1337 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
1338 // /* HeapReference<Class> */ TMP = temp2->component_type_
1339 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001340 invoke, TMP_loc, temp2, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001341 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1342 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1343 // If heap poisoning is enabled, `TMP` has been unpoisoned by
1344 // the the previous call to GenerateFieldLoadWithBakerReadBarrier.
1345 } else {
1346 // /* HeapReference<Class> */ TMP = temp2->component_type_
1347 __ movl(CpuRegister(TMP), Address(temp2, component_offset));
1348 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1349 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1350 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1351 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001352 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001353 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001354 }
1355
1356 __ cmpl(temp1, temp2);
1357
1358 if (optimizations.GetDestinationIsTypedObjectArray()) {
1359 NearLabel do_copy;
1360 __ j(kEqual, &do_copy);
Roland Levillain0b671c02016-08-19 12:02:34 +01001361 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1362 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1363 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001364 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001365 // We do not need to emit a read barrier for the following
1366 // heap reference load, as `temp1` is only used in a
1367 // comparison with null below, and this reference is not
1368 // kept afterwards.
1369 __ cmpl(Address(temp1, super_offset), Immediate(0));
1370 } else {
1371 if (!did_unpoison) {
1372 __ MaybeUnpoisonHeapReference(temp1);
1373 }
1374 // /* HeapReference<Class> */ temp1 = temp1->component_type_
1375 __ movl(temp1, Address(temp1, component_offset));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001376 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain0b671c02016-08-19 12:02:34 +01001377 // No need to unpoison the following heap reference load, as
1378 // we're comparing against null.
1379 __ cmpl(Address(temp1, super_offset), Immediate(0));
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001380 }
Roland Levillain0b671c02016-08-19 12:02:34 +01001381 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001382 __ Bind(&do_copy);
1383 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01001384 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001385 }
1386 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
1387 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
1388 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01001389 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1390 // /* HeapReference<Class> */ temp1 = src->klass_
1391 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001392 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001393 // /* HeapReference<Class> */ TMP = temp1->component_type_
1394 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001395 invoke, TMP_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01001396 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1397 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1398 } else {
1399 // /* HeapReference<Class> */ temp1 = src->klass_
1400 __ movl(temp1, Address(src, class_offset));
1401 __ MaybeUnpoisonHeapReference(temp1);
1402 // /* HeapReference<Class> */ TMP = temp1->component_type_
1403 __ movl(CpuRegister(TMP), Address(temp1, component_offset));
1404 // No need to unpoison `TMP` now, as we're comparing against null.
1405 __ testl(CpuRegister(TMP), CpuRegister(TMP));
1406 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
1407 __ MaybeUnpoisonHeapReference(CpuRegister(TMP));
1408 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001409 __ cmpw(Address(CpuRegister(TMP), primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01001410 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001411 }
1412
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001413 const DataType::Type type = DataType::Type::kReference;
1414 const int32_t element_size = DataType::Size(type);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001415
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001416 // Compute base source address, base destination address, and end
1417 // source address in `temp1`, `temp2` and `temp3` respectively.
1418 GenSystemArrayCopyAddresses(
1419 GetAssembler(), type, src, src_pos, dest, dest_pos, length, temp1, temp2, temp3);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001420
Roland Levillain0b671c02016-08-19 12:02:34 +01001421 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1422 // SystemArrayCopy implementation for Baker read barriers (see
1423 // also CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier):
1424 //
1425 // if (src_ptr != end_ptr) {
1426 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
1427 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001428 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01001429 // if (is_gray) {
1430 // // Slow-path copy.
1431 // do {
1432 // *dest_ptr++ = MaybePoison(ReadBarrier::Mark(MaybeUnpoison(*src_ptr++)));
1433 // } while (src_ptr != end_ptr)
1434 // } else {
1435 // // Fast-path copy.
1436 // do {
1437 // *dest_ptr++ = *src_ptr++;
1438 // } while (src_ptr != end_ptr)
1439 // }
1440 // }
1441
1442 NearLabel loop, done;
1443
1444 // Don't enter copy loop if `length == 0`.
1445 __ cmpl(temp1, temp3);
1446 __ j(kEqual, &done);
1447
Vladimir Marko953437b2016-08-24 08:30:46 +00001448 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001449 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1450 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00001451 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
1452 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
1453 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
1454
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07001455 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00001456 // goto slow_path;
1457 // At this point, just do the "if" and make sure that flags are preserved until the branch.
1458 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01001459
1460 // Load fence to prevent load-load reordering.
1461 // Note that this is a no-op, thanks to the x86-64 memory model.
1462 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
1463
1464 // Slow path used to copy array when `src` is gray.
1465 SlowPathCode* read_barrier_slow_path =
1466 new (GetAllocator()) ReadBarrierSystemArrayCopySlowPathX86_64(invoke);
1467 codegen_->AddSlowPath(read_barrier_slow_path);
1468
Vladimir Marko953437b2016-08-24 08:30:46 +00001469 // We have done the "if" of the gray bit check above, now branch based on the flags.
1470 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01001471
1472 // Fast-path copy.
1473 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1474 // poison/unpoison.
1475 __ Bind(&loop);
1476 __ movl(CpuRegister(TMP), Address(temp1, 0));
1477 __ movl(Address(temp2, 0), CpuRegister(TMP));
1478 __ addl(temp1, Immediate(element_size));
1479 __ addl(temp2, Immediate(element_size));
1480 __ cmpl(temp1, temp3);
1481 __ j(kNotEqual, &loop);
1482
1483 __ Bind(read_barrier_slow_path->GetExitLabel());
1484 __ Bind(&done);
1485 } else {
1486 // Non read barrier code.
1487
1488 // Iterate over the arrays and do a raw copy of the objects. We don't need to
1489 // poison/unpoison.
1490 NearLabel loop, done;
1491 __ cmpl(temp1, temp3);
1492 __ j(kEqual, &done);
1493 __ Bind(&loop);
1494 __ movl(CpuRegister(TMP), Address(temp1, 0));
1495 __ movl(Address(temp2, 0), CpuRegister(TMP));
1496 __ addl(temp1, Immediate(element_size));
1497 __ addl(temp2, Immediate(element_size));
1498 __ cmpl(temp1, temp3);
1499 __ j(kNotEqual, &loop);
1500 __ Bind(&done);
1501 }
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001502
1503 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00001504 codegen_->MarkGCCard(temp1, temp2, dest, CpuRegister(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001505
Roland Levillain0b671c02016-08-19 12:02:34 +01001506 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayee3cf072015-10-06 11:45:02 +01001507}
1508
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001509void IntrinsicLocationsBuilderX86_64::VisitStringCompareTo(HInvoke* invoke) {
1510 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001511 LocationSummary::kCallOnMainAndSlowPath,
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001512 kIntrinsified);
1513 InvokeRuntimeCallingConvention calling_convention;
1514 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1515 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1516 locations->SetOut(Location::RegisterLocation(RAX));
1517}
1518
1519void IntrinsicCodeGeneratorX86_64::VisitStringCompareTo(HInvoke* invoke) {
1520 X86_64Assembler* assembler = GetAssembler();
1521 LocationSummary* locations = invoke->GetLocations();
1522
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001523 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001524 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001525
1526 CpuRegister argument = locations->InAt(1).AsRegister<CpuRegister>();
1527 __ testl(argument, argument);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001528 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001529 codegen_->AddSlowPath(slow_path);
1530 __ j(kEqual, slow_path->GetEntryLabel());
1531
Serban Constantinescuba45db02016-07-12 22:53:02 +01001532 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001533 __ Bind(slow_path->GetExitLabel());
1534}
1535
Agi Csakif8cfb202015-08-13 17:54:54 -07001536void IntrinsicLocationsBuilderX86_64::VisitStringEquals(HInvoke* invoke) {
1537 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1538 LocationSummary::kNoCall,
1539 kIntrinsified);
1540 locations->SetInAt(0, Location::RequiresRegister());
1541 locations->SetInAt(1, Location::RequiresRegister());
1542
1543 // Request temporary registers, RCX and RDI needed for repe_cmpsq instruction.
1544 locations->AddTemp(Location::RegisterLocation(RCX));
1545 locations->AddTemp(Location::RegisterLocation(RDI));
1546
1547 // Set output, RSI needed for repe_cmpsq instruction anyways.
1548 locations->SetOut(Location::RegisterLocation(RSI), Location::kOutputOverlap);
1549}
1550
1551void IntrinsicCodeGeneratorX86_64::VisitStringEquals(HInvoke* invoke) {
1552 X86_64Assembler* assembler = GetAssembler();
1553 LocationSummary* locations = invoke->GetLocations();
1554
1555 CpuRegister str = locations->InAt(0).AsRegister<CpuRegister>();
1556 CpuRegister arg = locations->InAt(1).AsRegister<CpuRegister>();
1557 CpuRegister rcx = locations->GetTemp(0).AsRegister<CpuRegister>();
1558 CpuRegister rdi = locations->GetTemp(1).AsRegister<CpuRegister>();
1559 CpuRegister rsi = locations->Out().AsRegister<CpuRegister>();
1560
Mark Mendell0c9497d2015-08-21 09:30:05 -04001561 NearLabel end, return_true, return_false;
Agi Csakif8cfb202015-08-13 17:54:54 -07001562
1563 // Get offsets of count, value, and class fields within a string object.
1564 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1565 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1566 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1567
1568 // Note that the null check must have been done earlier.
1569 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1570
Vladimir Marko53b52002016-05-24 19:30:45 +01001571 StringEqualsOptimizations optimizations(invoke);
1572 if (!optimizations.GetArgumentNotNull()) {
1573 // Check if input is null, return false if it is.
1574 __ testl(arg, arg);
1575 __ j(kEqual, &return_false);
1576 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001577
Vladimir Marko53b52002016-05-24 19:30:45 +01001578 if (!optimizations.GetArgumentIsString()) {
1579 // Instanceof check for the argument by comparing class fields.
1580 // All string objects must have the same type since String cannot be subclassed.
1581 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1582 // If the argument is a string object, its class field must be equal to receiver's class field.
1583 __ movl(rcx, Address(str, class_offset));
1584 __ cmpl(rcx, Address(arg, class_offset));
1585 __ j(kNotEqual, &return_false);
1586 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001587
1588 // Reference equality check, return true if same reference.
1589 __ cmpl(str, arg);
1590 __ j(kEqual, &return_true);
1591
jessicahandojo4877b792016-09-08 19:49:13 -07001592 // Load length and compression flag of receiver string.
Agi Csakif8cfb202015-08-13 17:54:54 -07001593 __ movl(rcx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001594 // Check if lengths and compressiond flags are equal, return false if they're not.
1595 // Two identical strings will always have same compression style since
1596 // compression style is decided on alloc.
Agi Csakif8cfb202015-08-13 17:54:54 -07001597 __ cmpl(rcx, Address(arg, count_offset));
1598 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001599 // Return true if both strings are empty. Even with string compression `count == 0` means empty.
1600 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1601 "Expecting 0=compressed, 1=uncompressed");
1602 __ jrcxz(&return_true);
jessicahandojo4877b792016-09-08 19:49:13 -07001603
1604 if (mirror::kUseStringCompression) {
1605 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001606 // Extract length and differentiate between both compressed or both uncompressed.
1607 // Different compression style is cut above.
1608 __ shrl(rcx, Immediate(1));
1609 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001610 // Divide string length by 2, rounding up, and continue as if uncompressed.
1611 // Merge clearing the compression flag with +1 for rounding.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001612 __ addl(rcx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001613 __ shrl(rcx, Immediate(1));
1614 __ Bind(&string_uncompressed);
1615 }
Agi Csakif8cfb202015-08-13 17:54:54 -07001616 // Load starting addresses of string values into RSI/RDI as required for repe_cmpsq instruction.
1617 __ leal(rsi, Address(str, value_offset));
1618 __ leal(rdi, Address(arg, value_offset));
1619
1620 // Divide string length by 4 and adjust for lengths not divisible by 4.
1621 __ addl(rcx, Immediate(3));
1622 __ shrl(rcx, Immediate(2));
1623
jessicahandojo4877b792016-09-08 19:49:13 -07001624 // Assertions that must hold in order to compare strings 4 characters (uncompressed)
1625 // or 8 characters (compressed) at a time.
Agi Csakif8cfb202015-08-13 17:54:54 -07001626 DCHECK_ALIGNED(value_offset, 8);
1627 static_assert(IsAligned<8>(kObjectAlignment), "String is not zero padded");
1628
1629 // Loop to compare strings four characters at a time starting at the beginning of the string.
1630 __ repe_cmpsq();
1631 // If strings are not equal, zero flag will be cleared.
1632 __ j(kNotEqual, &return_false);
1633
1634 // Return true and exit the function.
1635 // If loop does not result in returning false, we return true.
1636 __ Bind(&return_true);
1637 __ movl(rsi, Immediate(1));
1638 __ jmp(&end);
1639
1640 // Return false and exit the function.
1641 __ Bind(&return_false);
1642 __ xorl(rsi, rsi);
1643 __ Bind(&end);
1644}
1645
Andreas Gampe21030dd2015-05-07 14:46:15 -07001646static void CreateStringIndexOfLocations(HInvoke* invoke,
1647 ArenaAllocator* allocator,
1648 bool start_at_zero) {
1649 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1650 LocationSummary::kCallOnSlowPath,
1651 kIntrinsified);
1652 // The data needs to be in RDI for scasw. So request that the string is there, anyways.
1653 locations->SetInAt(0, Location::RegisterLocation(RDI));
1654 // If we look for a constant char, we'll still have to copy it into RAX. So just request the
1655 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1656 // of the instruction explicitly.
1657 // Note: This works as we don't clobber RAX anywhere.
1658 locations->SetInAt(1, Location::RegisterLocation(RAX));
1659 if (!start_at_zero) {
1660 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1661 }
1662 // As we clobber RDI during execution anyways, also use it as the output.
1663 locations->SetOut(Location::SameAsFirstInput());
1664
1665 // repne scasw uses RCX as the counter.
1666 locations->AddTemp(Location::RegisterLocation(RCX));
1667 // Need another temporary to be able to compute the result.
1668 locations->AddTemp(Location::RequiresRegister());
1669}
1670
1671static void GenerateStringIndexOf(HInvoke* invoke,
1672 X86_64Assembler* assembler,
1673 CodeGeneratorX86_64* codegen,
1674 ArenaAllocator* allocator,
1675 bool start_at_zero) {
1676 LocationSummary* locations = invoke->GetLocations();
1677
1678 // Note that the null check must have been done earlier.
1679 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1680
1681 CpuRegister string_obj = locations->InAt(0).AsRegister<CpuRegister>();
1682 CpuRegister search_value = locations->InAt(1).AsRegister<CpuRegister>();
1683 CpuRegister counter = locations->GetTemp(0).AsRegister<CpuRegister>();
1684 CpuRegister string_length = locations->GetTemp(1).AsRegister<CpuRegister>();
1685 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
1686
1687 // Check our assumptions for registers.
1688 DCHECK_EQ(string_obj.AsRegister(), RDI);
1689 DCHECK_EQ(search_value.AsRegister(), RAX);
1690 DCHECK_EQ(counter.AsRegister(), RCX);
1691 DCHECK_EQ(out.AsRegister(), RDI);
1692
1693 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001694 // 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 -07001695 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001696 HInstruction* code_point = invoke->InputAt(1);
1697 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001698 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001699 std::numeric_limits<uint16_t>::max()) {
1700 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1701 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
1702 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1703 codegen->AddSlowPath(slow_path);
1704 __ jmp(slow_path->GetEntryLabel());
1705 __ Bind(slow_path->GetExitLabel());
1706 return;
1707 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001709 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
1710 slow_path = new (allocator) IntrinsicSlowPathX86_64(invoke);
1711 codegen->AddSlowPath(slow_path);
1712 __ j(kAbove, slow_path->GetEntryLabel());
1713 }
1714
jessicahandojo4877b792016-09-08 19:49:13 -07001715 // From here down, we know that we are looking for a char that fits in
1716 // 16 bits (uncompressed) or 8 bits (compressed).
Andreas Gampe21030dd2015-05-07 14:46:15 -07001717 // Location of reference to data array within the String object.
1718 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1719 // Location of count within the String object.
1720 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1721
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001722 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001723 __ movl(string_length, Address(string_obj, count_offset));
1724
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001725 // Do a zero-length check. Even with string compression `count == 0` means empty.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001726 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001727 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001728 __ testl(string_length, string_length);
1729 __ j(kEqual, &not_found_label);
1730
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001731 if (mirror::kUseStringCompression) {
1732 // Use TMP to keep string_length_flagged.
1733 __ movl(CpuRegister(TMP), string_length);
1734 // Mask out first bit used as compression flag.
1735 __ shrl(string_length, Immediate(1));
1736 }
1737
Andreas Gampe21030dd2015-05-07 14:46:15 -07001738 if (start_at_zero) {
1739 // Number of chars to scan is the same as the string length.
1740 __ movl(counter, string_length);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001741 // Move to the start of the string.
1742 __ addq(string_obj, Immediate(value_offset));
1743 } else {
1744 CpuRegister start_index = locations->InAt(2).AsRegister<CpuRegister>();
1745
1746 // Do a start_index check.
1747 __ cmpl(start_index, string_length);
1748 __ j(kGreaterEqual, &not_found_label);
1749
1750 // Ensure we have a start index >= 0;
1751 __ xorl(counter, counter);
1752 __ cmpl(start_index, Immediate(0));
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001753 __ cmov(kGreater, counter, start_index, /* is64bit */ false); // 32-bit copy is enough.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001754
jessicahandojo4877b792016-09-08 19:49:13 -07001755 if (mirror::kUseStringCompression) {
1756 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001757 __ testl(CpuRegister(TMP), Immediate(1));
1758 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001759 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1760 __ jmp(&modify_counter);
1761 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1762 __ Bind(&offset_uncompressed_label);
1763 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1764 __ Bind(&modify_counter);
1765 } else {
1766 __ leaq(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1767 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001768 // Now update ecx, the work counter: it's gonna be string.length - start_index.
1769 __ negq(counter); // Needs to be 64-bit negation, as the address computation is 64-bit.
1770 __ leaq(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1771 }
1772
jessicahandojo4877b792016-09-08 19:49:13 -07001773 if (mirror::kUseStringCompression) {
1774 NearLabel uncompressed_string_comparison;
1775 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001776 __ testl(CpuRegister(TMP), Immediate(1));
1777 __ j(kNotZero, &uncompressed_string_comparison);
jessicahandojo4877b792016-09-08 19:49:13 -07001778 // Check if RAX (search_value) is ASCII.
1779 __ cmpl(search_value, Immediate(127));
1780 __ j(kGreater, &not_found_label);
1781 // Comparing byte-per-byte.
1782 __ repne_scasb();
1783 __ jmp(&comparison_done);
1784 // Everything is set up for repne scasw:
1785 // * Comparison address in RDI.
1786 // * Counter in ECX.
1787 __ Bind(&uncompressed_string_comparison);
1788 __ repne_scasw();
1789 __ Bind(&comparison_done);
1790 } else {
1791 __ repne_scasw();
1792 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001793 // Did we find a match?
1794 __ j(kNotEqual, &not_found_label);
1795
1796 // Yes, we matched. Compute the index of the result.
1797 __ subl(string_length, counter);
1798 __ leal(out, Address(string_length, -1));
1799
Mark Mendell0c9497d2015-08-21 09:30:05 -04001800 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001801 __ jmp(&done);
1802
1803 // Failed to match; return -1.
1804 __ Bind(&not_found_label);
1805 __ movl(out, Immediate(-1));
1806
1807 // And join up at the end.
1808 __ Bind(&done);
1809 if (slow_path != nullptr) {
1810 __ Bind(slow_path->GetExitLabel());
1811 }
1812}
1813
1814void IntrinsicLocationsBuilderX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001815 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001816}
1817
1818void IntrinsicCodeGeneratorX86_64::VisitStringIndexOf(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001819 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001820}
1821
1822void IntrinsicLocationsBuilderX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001823 CreateStringIndexOfLocations(invoke, arena_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001824}
1825
1826void IntrinsicCodeGeneratorX86_64::VisitStringIndexOfAfter(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001827 GenerateStringIndexOf(
1828 invoke, GetAssembler(), codegen_, GetAllocator(), /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001829}
1830
Jeff Hao848f70a2014-01-15 13:49:50 -08001831void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1832 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001833 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001834 kIntrinsified);
1835 InvokeRuntimeCallingConvention calling_convention;
1836 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1837 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1838 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1839 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1840 locations->SetOut(Location::RegisterLocation(RAX));
1841}
1842
1843void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromBytes(HInvoke* invoke) {
1844 X86_64Assembler* assembler = GetAssembler();
1845 LocationSummary* locations = invoke->GetLocations();
1846
1847 CpuRegister byte_array = locations->InAt(0).AsRegister<CpuRegister>();
1848 __ testl(byte_array, byte_array);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001849 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001850 codegen_->AddSlowPath(slow_path);
1851 __ j(kEqual, slow_path->GetEntryLabel());
1852
Serban Constantinescuba45db02016-07-12 22:53:02 +01001853 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001854 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001855 __ Bind(slow_path->GetExitLabel());
1856}
1857
1858void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
1859 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu54ff4822016-07-07 18:03:19 +01001860 LocationSummary::kCallOnMainOnly,
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->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1865 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1866 locations->SetOut(Location::RegisterLocation(RAX));
1867}
1868
1869void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001870 // No need to emit code checking whether `locations->InAt(2)` is a null
1871 // pointer, as callers of the native method
1872 //
1873 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1874 //
1875 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001876 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001877 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001878}
1879
1880void IntrinsicLocationsBuilderX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1881 LocationSummary* locations = new (arena_) LocationSummary(invoke,
Serban Constantinescu806f0122016-03-09 11:10:16 +00001882 LocationSummary::kCallOnMainAndSlowPath,
Jeff Hao848f70a2014-01-15 13:49:50 -08001883 kIntrinsified);
1884 InvokeRuntimeCallingConvention calling_convention;
1885 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1886 locations->SetOut(Location::RegisterLocation(RAX));
1887}
1888
1889void IntrinsicCodeGeneratorX86_64::VisitStringNewStringFromString(HInvoke* invoke) {
1890 X86_64Assembler* assembler = GetAssembler();
1891 LocationSummary* locations = invoke->GetLocations();
1892
1893 CpuRegister string_to_copy = locations->InAt(0).AsRegister<CpuRegister>();
1894 __ testl(string_to_copy, string_to_copy);
Andreas Gampe85b62f22015-09-09 13:15:38 -07001895 SlowPathCode* slow_path = new (GetAllocator()) IntrinsicSlowPathX86_64(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001896 codegen_->AddSlowPath(slow_path);
1897 __ j(kEqual, slow_path->GetEntryLabel());
1898
Serban Constantinescuba45db02016-07-12 22:53:02 +01001899 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001900 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001901 __ Bind(slow_path->GetExitLabel());
1902}
1903
Mark Mendell8f8926a2015-08-17 11:39:06 -04001904void IntrinsicLocationsBuilderX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1905 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1906 LocationSummary* locations = new (arena_) LocationSummary(invoke,
1907 LocationSummary::kNoCall,
1908 kIntrinsified);
1909 locations->SetInAt(0, Location::RequiresRegister());
1910 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1911 locations->SetInAt(2, Location::RequiresRegister());
1912 locations->SetInAt(3, Location::RequiresRegister());
1913 locations->SetInAt(4, Location::RequiresRegister());
1914
1915 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1916 locations->AddTemp(Location::RegisterLocation(RSI));
1917 locations->AddTemp(Location::RegisterLocation(RDI));
1918 locations->AddTemp(Location::RegisterLocation(RCX));
1919}
1920
1921void IntrinsicCodeGeneratorX86_64::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1922 X86_64Assembler* assembler = GetAssembler();
1923 LocationSummary* locations = invoke->GetLocations();
1924
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001925 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001926 // Location of data in char array buffer.
1927 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1928 // Location of char array data in string.
1929 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1930
1931 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1932 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
1933 Location srcBegin = locations->InAt(1);
1934 int srcBegin_value =
1935 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1936 CpuRegister srcEnd = locations->InAt(2).AsRegister<CpuRegister>();
1937 CpuRegister dst = locations->InAt(3).AsRegister<CpuRegister>();
1938 CpuRegister dstBegin = locations->InAt(4).AsRegister<CpuRegister>();
1939
1940 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001942 DCHECK_EQ(char_size, 2u);
1943
jessicahandojo4877b792016-09-08 19:49:13 -07001944 NearLabel done;
Mark Mendell8f8926a2015-08-17 11:39:06 -04001945 // Compute the number of chars (words) to move.
1946 __ movl(CpuRegister(RCX), srcEnd);
1947 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001948 __ subl(CpuRegister(RCX), Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001949 } else {
1950 DCHECK(srcBegin.IsRegister());
1951 __ subl(CpuRegister(RCX), srcBegin.AsRegister<CpuRegister>());
1952 }
jessicahandojo4877b792016-09-08 19:49:13 -07001953 if (mirror::kUseStringCompression) {
1954 NearLabel copy_uncompressed, copy_loop;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001955 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001956 DCHECK_EQ(c_char_size, 1u);
1957 // Location of count in string.
1958 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Mark Mendell8f8926a2015-08-17 11:39:06 -04001959
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001960 __ testl(Address(obj, count_offset), Immediate(1));
1961 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1962 "Expecting 0=compressed, 1=uncompressed");
1963 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001964 // Compute the address of the source string by adding the number of chars from
1965 // the source beginning to the value offset of a string.
1966 __ leaq(CpuRegister(RSI),
1967 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1968 // Start the loop to copy String's value to Array of Char.
1969 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1970
1971 __ Bind(&copy_loop);
1972 __ jrcxz(&done);
1973 // Use TMP as temporary (convert byte from RSI to word).
1974 // TODO: Selecting RAX as the temporary and using LODSB/STOSW.
1975 __ movzxb(CpuRegister(TMP), Address(CpuRegister(RSI), 0));
1976 __ movw(Address(CpuRegister(RDI), 0), CpuRegister(TMP));
1977 __ leaq(CpuRegister(RDI), Address(CpuRegister(RDI), char_size));
1978 __ leaq(CpuRegister(RSI), Address(CpuRegister(RSI), c_char_size));
1979 // TODO: Add support for LOOP to X86_64Assembler.
1980 __ subl(CpuRegister(RCX), Immediate(1));
1981 __ jmp(&copy_loop);
1982
1983 __ Bind(&copy_uncompressed);
1984 }
1985
1986 __ leaq(CpuRegister(RSI),
1987 CodeGeneratorX86_64::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
1988 // Compute the address of the destination buffer.
1989 __ leaq(CpuRegister(RDI), Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001990 // Do the move.
1991 __ rep_movsw();
jessicahandojo4877b792016-09-08 19:49:13 -07001992
1993 __ Bind(&done);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001994}
1995
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996static void GenPeek(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08001997 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
1998 CpuRegister out = locations->Out().AsRegister<CpuRegister>(); // == address, here for clarity.
1999 // x86 allows unaligned access. We do not have to check the input or use specific instructions
2000 // to avoid a SIGBUS.
2001 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kInt8:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002003 __ movsxb(out, Address(address, 0));
2004 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 case DataType::Type::kInt16:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002006 __ movsxw(out, Address(address, 0));
2007 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002008 case DataType::Type::kInt32:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002009 __ movl(out, Address(address, 0));
2010 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002011 case DataType::Type::kInt64:
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002012 __ movq(out, Address(address, 0));
2013 break;
2014 default:
2015 LOG(FATAL) << "Type not recognized for peek: " << size;
2016 UNREACHABLE();
2017 }
2018}
2019
2020void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
2021 CreateIntToIntLocations(arena_, invoke);
2022}
2023
2024void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002026}
2027
2028void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
2029 CreateIntToIntLocations(arena_, invoke);
2030}
2031
2032void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002034}
2035
2036void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
2037 CreateIntToIntLocations(arena_, invoke);
2038}
2039
2040void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002042}
2043
2044void IntrinsicLocationsBuilderX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
2045 CreateIntToIntLocations(arena_, invoke);
2046}
2047
2048void IntrinsicCodeGeneratorX86_64::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002050}
2051
2052static void CreateIntIntToVoidLocations(ArenaAllocator* arena, HInvoke* invoke) {
2053 LocationSummary* locations = new (arena) LocationSummary(invoke,
2054 LocationSummary::kNoCall,
2055 kIntrinsified);
2056 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002057 locations->SetInAt(1, Location::RegisterOrInt32Constant(invoke->InputAt(1)));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002058}
2059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002060static void GenPoke(LocationSummary* locations, DataType::Type size, X86_64Assembler* assembler) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002061 CpuRegister address = locations->InAt(0).AsRegister<CpuRegister>();
Mark Mendell40741f32015-04-20 22:10:34 -04002062 Location value = locations->InAt(1);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002063 // x86 allows unaligned access. We do not have to check the input or use specific instructions
2064 // to avoid a SIGBUS.
2065 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002066 case DataType::Type::kInt8:
Mark Mendell40741f32015-04-20 22:10:34 -04002067 if (value.IsConstant()) {
2068 __ movb(Address(address, 0),
2069 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2070 } else {
2071 __ movb(Address(address, 0), value.AsRegister<CpuRegister>());
2072 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002073 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002074 case DataType::Type::kInt16:
Mark Mendell40741f32015-04-20 22:10:34 -04002075 if (value.IsConstant()) {
2076 __ movw(Address(address, 0),
2077 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2078 } else {
2079 __ movw(Address(address, 0), value.AsRegister<CpuRegister>());
2080 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002081 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 case DataType::Type::kInt32:
Mark Mendell40741f32015-04-20 22:10:34 -04002083 if (value.IsConstant()) {
2084 __ movl(Address(address, 0),
2085 Immediate(CodeGenerator::GetInt32ValueOf(value.GetConstant())));
2086 } else {
2087 __ movl(Address(address, 0), value.AsRegister<CpuRegister>());
2088 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002089 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002090 case DataType::Type::kInt64:
Mark Mendell40741f32015-04-20 22:10:34 -04002091 if (value.IsConstant()) {
2092 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
2093 DCHECK(IsInt<32>(v));
2094 int32_t v_32 = v;
2095 __ movq(Address(address, 0), Immediate(v_32));
2096 } else {
2097 __ movq(Address(address, 0), value.AsRegister<CpuRegister>());
2098 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002099 break;
2100 default:
2101 LOG(FATAL) << "Type not recognized for poke: " << size;
2102 UNREACHABLE();
2103 }
2104}
2105
2106void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
2107 CreateIntIntToVoidLocations(arena_, invoke);
2108}
2109
2110void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002111 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002112}
2113
2114void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
2115 CreateIntIntToVoidLocations(arena_, invoke);
2116}
2117
2118void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002120}
2121
2122void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
2123 CreateIntIntToVoidLocations(arena_, invoke);
2124}
2125
2126void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002128}
2129
2130void IntrinsicLocationsBuilderX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
2131 CreateIntIntToVoidLocations(arena_, invoke);
2132}
2133
2134void IntrinsicCodeGeneratorX86_64::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002136}
2137
2138void IntrinsicLocationsBuilderX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2139 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2140 LocationSummary::kNoCall,
2141 kIntrinsified);
2142 locations->SetOut(Location::RequiresRegister());
2143}
2144
2145void IntrinsicCodeGeneratorX86_64::VisitThreadCurrentThread(HInvoke* invoke) {
2146 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
Andreas Gampe542451c2016-07-26 09:02:02 -07002147 GetAssembler()->gs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86_64PointerSize>(),
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002148 /* no_rip */ true));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002149}
2150
Roland Levillain0d5a2812015-11-13 10:07:31 +00002151static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002152 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00002153 bool is_volatile ATTRIBUTE_UNUSED,
2154 CodeGeneratorX86_64* codegen) {
2155 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
2156 LocationSummary* locations = invoke->GetLocations();
2157 Location base_loc = locations->InAt(1);
2158 CpuRegister base = base_loc.AsRegister<CpuRegister>();
2159 Location offset_loc = locations->InAt(2);
2160 CpuRegister offset = offset_loc.AsRegister<CpuRegister>();
2161 Location output_loc = locations->Out();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002162 CpuRegister output = output_loc.AsRegister<CpuRegister>();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002163
Andreas Gampe878d58c2015-01-15 23:24:00 -08002164 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002165 case DataType::Type::kInt32:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002166 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002167 break;
2168
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002169 case DataType::Type::kReference: {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002170 if (kEmitCompilerReadBarrier) {
2171 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002172 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2173 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002174 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002175 } else {
2176 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2177 codegen->GenerateReadBarrierSlow(
2178 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2179 }
2180 } else {
2181 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2182 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002183 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002184 break;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00002185 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002187 case DataType::Type::kInt64:
Roland Levillain0d5a2812015-11-13 10:07:31 +00002188 __ movq(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Andreas Gampe878d58c2015-01-15 23:24:00 -08002189 break;
2190
2191 default:
2192 LOG(FATAL) << "Unsupported op size " << type;
2193 UNREACHABLE();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002194 }
2195}
2196
Vladimir Marko953437b2016-08-24 08:30:46 +00002197static void CreateIntIntIntToIntLocations(ArenaAllocator* arena, HInvoke* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002198 bool can_call = kEmitCompilerReadBarrier &&
2199 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2200 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002201 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002202 (can_call
2203 ? LocationSummary::kCallOnSlowPath
2204 : LocationSummary::kNoCall),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002205 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002206 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002207 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002208 }
Andreas Gampe878d58c2015-01-15 23:24:00 -08002209 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002210 locations->SetInAt(1, Location::RequiresRegister());
2211 locations->SetInAt(2, Location::RequiresRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002212 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002213 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002214}
2215
2216void IntrinsicLocationsBuilderX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002217 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002218}
2219void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002220 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002221}
2222void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002223 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002224}
2225void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002226 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002227}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002228void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002229 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002230}
2231void IntrinsicLocationsBuilderX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko953437b2016-08-24 08:30:46 +00002232 CreateIntIntIntToIntLocations(arena_, invoke);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002233}
2234
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002235
2236void IntrinsicCodeGeneratorX86_64::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002237 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002238}
2239void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002241}
2242void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002243 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002244}
2245void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002247}
Andreas Gampe878d58c2015-01-15 23:24:00 -08002248void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002249 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002250}
2251void IntrinsicCodeGeneratorX86_64::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002253}
2254
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002255
2256static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* arena,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 DataType::Type type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002258 HInvoke* invoke) {
2259 LocationSummary* locations = new (arena) LocationSummary(invoke,
2260 LocationSummary::kNoCall,
2261 kIntrinsified);
Andreas Gampe878d58c2015-01-15 23:24:00 -08002262 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002263 locations->SetInAt(1, Location::RequiresRegister());
2264 locations->SetInAt(2, Location::RequiresRegister());
2265 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 if (type == DataType::Type::kReference) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002267 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002268 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002269 locations->AddTemp(Location::RequiresRegister());
2270 }
2271}
2272
2273void IntrinsicLocationsBuilderX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002275}
2276void IntrinsicLocationsBuilderX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002278}
2279void IntrinsicLocationsBuilderX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002280 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kInt32, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002281}
2282void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002283 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002284}
2285void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002287}
2288void IntrinsicLocationsBuilderX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002289 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kReference, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002290}
2291void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002292 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002293}
2294void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002296}
2297void IntrinsicLocationsBuilderX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 CreateIntIntIntIntToVoidPlusTempsLocations(arena_, DataType::Type::kInt64, invoke);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002299}
2300
2301// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2302// memory model.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002303static void GenUnsafePut(LocationSummary* locations, DataType::Type type, bool is_volatile,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002304 CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002305 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002306 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2307 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2308 CpuRegister value = locations->InAt(3).AsRegister<CpuRegister>();
2309
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002310 if (type == DataType::Type::kInt64) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002311 __ movq(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002313 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2314 __ movl(temp, value);
2315 __ PoisonHeapReference(temp);
2316 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002317 } else {
2318 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value);
2319 }
2320
2321 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002322 codegen->MemoryFence();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002323 }
2324
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002325 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002326 bool value_can_be_null = true; // TODO: Worth finding out this information?
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002327 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<CpuRegister>(),
2328 locations->GetTemp(1).AsRegister<CpuRegister>(),
2329 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002330 value,
2331 value_can_be_null);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002332 }
2333}
2334
2335void IntrinsicCodeGeneratorX86_64::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002336 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002337}
2338void IntrinsicCodeGeneratorX86_64::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002339 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002340}
2341void IntrinsicCodeGeneratorX86_64::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002342 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002343}
2344void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 GenUnsafePut(
2346 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002347}
2348void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002349 GenUnsafePut(
2350 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002351}
2352void IntrinsicCodeGeneratorX86_64::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 GenUnsafePut(
2354 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002355}
2356void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002357 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002358}
2359void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002361}
2362void IntrinsicCodeGeneratorX86_64::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002363 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002364}
2365
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002366static void CreateIntIntIntIntIntToInt(ArenaAllocator* arena,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002368 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002369 bool can_call = kEmitCompilerReadBarrier &&
2370 kUseBakerReadBarrier &&
2371 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002372 LocationSummary* locations = new (arena) LocationSummary(invoke,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002373 (can_call
2374 ? LocationSummary::kCallOnSlowPath
2375 : LocationSummary::kNoCall),
Mark Mendell58d25fd2015-04-03 14:52:31 -04002376 kIntrinsified);
2377 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2378 locations->SetInAt(1, Location::RequiresRegister());
2379 locations->SetInAt(2, Location::RequiresRegister());
2380 // expected value must be in EAX/RAX.
2381 locations->SetInAt(3, Location::RegisterLocation(RAX));
2382 locations->SetInAt(4, Location::RequiresRegister());
2383
2384 locations->SetOut(Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002386 // Need temporary registers for card-marking, and possibly for
2387 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002388 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002389 locations->AddTemp(Location::RequiresRegister());
2390 }
2391}
2392
2393void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002394 CreateIntIntIntIntIntToInt(arena_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002395}
2396
2397void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 CreateIntIntIntIntIntToInt(arena_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002399}
2400
2401void IntrinsicLocationsBuilderX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002402 // The only read barrier implementation supporting the
2403 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2404 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002405 return;
2406 }
2407
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 CreateIntIntIntIntIntToInt(arena_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002409}
2410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86_64* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002412 X86_64Assembler* assembler = down_cast<X86_64Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002413 LocationSummary* locations = invoke->GetLocations();
2414
2415 CpuRegister base = locations->InAt(1).AsRegister<CpuRegister>();
2416 CpuRegister offset = locations->InAt(2).AsRegister<CpuRegister>();
2417 CpuRegister expected = locations->InAt(3).AsRegister<CpuRegister>();
Roland Levillainb488b782015-10-22 11:38:49 +01002418 // Ensure `expected` is in RAX (required by the CMPXCHG instruction).
Mark Mendell58d25fd2015-04-03 14:52:31 -04002419 DCHECK_EQ(expected.AsRegister(), RAX);
2420 CpuRegister value = locations->InAt(4).AsRegister<CpuRegister>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002421 Location out_loc = locations->Out();
2422 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002423
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002424 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002425 // The only read barrier implementation supporting the
2426 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2427 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2428
2429 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2430 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2431
Roland Levillainb488b782015-10-22 11:38:49 +01002432 // Mark card for object assuming new value is stored.
2433 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002434 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2435
2436 // The address of the field within the holding object.
2437 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2438
2439 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2440 // Need to make sure the reference stored in the field is a to-space
2441 // one before attempting the CAS or the CAS could fail incorrectly.
2442 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2443 invoke,
2444 out_loc, // Unused, used only as a "temporary" within the read barrier.
2445 base,
2446 field_addr,
2447 /* needs_null_check */ false,
2448 /* always_update_field */ true,
2449 &temp1,
2450 &temp2);
2451 }
Roland Levillain4d027112015-07-01 15:41:14 +01002452
Roland Levillainb488b782015-10-22 11:38:49 +01002453 bool base_equals_value = (base.AsRegister() == value.AsRegister());
2454 Register value_reg = value.AsRegister();
2455 if (kPoisonHeapReferences) {
2456 if (base_equals_value) {
2457 // If `base` and `value` are the same register location, move
2458 // `value_reg` to a temporary register. This way, poisoning
2459 // `value_reg` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002460 value_reg = temp1.AsRegister();
Roland Levillainb488b782015-10-22 11:38:49 +01002461 __ movl(CpuRegister(value_reg), base);
Roland Levillain4d027112015-07-01 15:41:14 +01002462 }
Roland Levillainb488b782015-10-22 11:38:49 +01002463
2464 // Check that the register allocator did not assign the location
2465 // of `expected` (RAX) to `value` nor to `base`, so that heap
2466 // poisoning (when enabled) works as intended below.
2467 // - If `value` were equal to `expected`, both references would
2468 // be poisoned twice, meaning they would not be poisoned at
2469 // all, as heap poisoning uses address negation.
2470 // - If `base` were equal to `expected`, poisoning `expected`
2471 // would invalidate `base`.
2472 DCHECK_NE(value_reg, expected.AsRegister());
2473 DCHECK_NE(base.AsRegister(), expected.AsRegister());
2474
2475 __ PoisonHeapReference(expected);
2476 __ PoisonHeapReference(CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002477 }
2478
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002479 __ LockCmpxchgl(field_addr, CpuRegister(value_reg));
Mark Mendell58d25fd2015-04-03 14:52:31 -04002480
Roland Levillain0d5a2812015-11-13 10:07:31 +00002481 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002482 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002483
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002484 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002485 __ setcc(kZero, out);
2486 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002487
Roland Levillain391b8662015-12-18 11:43:38 +00002488 // If heap poisoning is enabled, we need to unpoison the values
2489 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002490 if (kPoisonHeapReferences) {
2491 if (base_equals_value) {
2492 // `value_reg` has been moved to a temporary register, no need
2493 // to unpoison it.
2494 } else {
2495 // Ensure `value` is different from `out`, so that unpoisoning
2496 // the former does not invalidate the latter.
2497 DCHECK_NE(value_reg, out.AsRegister());
2498 __ UnpoisonHeapReference(CpuRegister(value_reg));
2499 }
2500 // Ensure `expected` is different from `out`, so that unpoisoning
2501 // the former does not invalidate the latter.
2502 DCHECK_NE(expected.AsRegister(), out.AsRegister());
2503 __ UnpoisonHeapReference(expected);
2504 }
2505 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002506 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002507 __ LockCmpxchgl(Address(base, offset, TIMES_1, 0), value);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002509 __ LockCmpxchgq(Address(base, offset, TIMES_1, 0), value);
2510 } else {
2511 LOG(FATAL) << "Unexpected CAS type " << type;
2512 }
2513
Roland Levillain0d5a2812015-11-13 10:07:31 +00002514 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002515 // scheduling barriers at this time.
2516
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002517 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002518 __ setcc(kZero, out);
2519 __ movzxb(out, out);
Roland Levillain4d027112015-07-01 15:41:14 +01002520 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002521}
2522
2523void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002524 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002525}
2526
2527void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002528 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002529}
2530
2531void IntrinsicCodeGeneratorX86_64::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002532 // The only read barrier implementation supporting the
2533 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2534 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002536 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002537}
2538
2539void IntrinsicLocationsBuilderX86_64::VisitIntegerReverse(HInvoke* invoke) {
2540 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2541 LocationSummary::kNoCall,
2542 kIntrinsified);
2543 locations->SetInAt(0, Location::RequiresRegister());
2544 locations->SetOut(Location::SameAsFirstInput());
2545 locations->AddTemp(Location::RequiresRegister());
2546}
2547
2548static void SwapBits(CpuRegister reg, CpuRegister temp, int32_t shift, int32_t mask,
2549 X86_64Assembler* assembler) {
2550 Immediate imm_shift(shift);
2551 Immediate imm_mask(mask);
2552 __ movl(temp, reg);
2553 __ shrl(reg, imm_shift);
2554 __ andl(temp, imm_mask);
2555 __ andl(reg, imm_mask);
2556 __ shll(temp, imm_shift);
2557 __ orl(reg, temp);
2558}
2559
2560void IntrinsicCodeGeneratorX86_64::VisitIntegerReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002561 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002562 LocationSummary* locations = invoke->GetLocations();
2563
2564 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2565 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2566
2567 /*
2568 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2569 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2570 * compared to generic luni implementation which has 5 rounds of swapping bits.
2571 * x = bswap x
2572 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2573 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2574 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2575 */
2576 __ bswapl(reg);
2577 SwapBits(reg, temp, 1, 0x55555555, assembler);
2578 SwapBits(reg, temp, 2, 0x33333333, assembler);
2579 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2580}
2581
2582void IntrinsicLocationsBuilderX86_64::VisitLongReverse(HInvoke* invoke) {
2583 LocationSummary* locations = new (arena_) LocationSummary(invoke,
2584 LocationSummary::kNoCall,
2585 kIntrinsified);
2586 locations->SetInAt(0, Location::RequiresRegister());
2587 locations->SetOut(Location::SameAsFirstInput());
2588 locations->AddTemp(Location::RequiresRegister());
2589 locations->AddTemp(Location::RequiresRegister());
2590}
2591
2592static void SwapBits64(CpuRegister reg, CpuRegister temp, CpuRegister temp_mask,
2593 int32_t shift, int64_t mask, X86_64Assembler* assembler) {
2594 Immediate imm_shift(shift);
2595 __ movq(temp_mask, Immediate(mask));
2596 __ movq(temp, reg);
2597 __ shrq(reg, imm_shift);
2598 __ andq(temp, temp_mask);
2599 __ andq(reg, temp_mask);
2600 __ shlq(temp, imm_shift);
2601 __ orq(reg, temp);
2602}
2603
2604void IntrinsicCodeGeneratorX86_64::VisitLongReverse(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002605 X86_64Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002606 LocationSummary* locations = invoke->GetLocations();
2607
2608 CpuRegister reg = locations->InAt(0).AsRegister<CpuRegister>();
2609 CpuRegister temp1 = locations->GetTemp(0).AsRegister<CpuRegister>();
2610 CpuRegister temp2 = locations->GetTemp(1).AsRegister<CpuRegister>();
2611
2612 /*
2613 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2614 * swapping bits to reverse bits in a long number x. Using bswap to save instructions
2615 * compared to generic luni implementation which has 5 rounds of swapping bits.
2616 * x = bswap x
2617 * x = (x & 0x5555555555555555) << 1 | (x >> 1) & 0x5555555555555555;
2618 * x = (x & 0x3333333333333333) << 2 | (x >> 2) & 0x3333333333333333;
2619 * x = (x & 0x0F0F0F0F0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F0F0F0F0F;
2620 */
2621 __ bswapq(reg);
2622 SwapBits64(reg, temp1, temp2, 1, INT64_C(0x5555555555555555), assembler);
2623 SwapBits64(reg, temp1, temp2, 2, INT64_C(0x3333333333333333), assembler);
2624 SwapBits64(reg, temp1, temp2, 4, INT64_C(0x0f0f0f0f0f0f0f0f), assembler);
2625}
2626
Aart Bik3f67e692016-01-15 14:35:12 -08002627static void CreateBitCountLocations(
2628 ArenaAllocator* arena, CodeGeneratorX86_64* codegen, HInvoke* invoke) {
2629 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2630 // Do nothing if there is no popcnt support. This results in generating
2631 // a call for the intrinsic rather than direct code.
2632 return;
2633 }
2634 LocationSummary* locations = new (arena) LocationSummary(invoke,
2635 LocationSummary::kNoCall,
2636 kIntrinsified);
2637 locations->SetInAt(0, Location::Any());
2638 locations->SetOut(Location::RequiresRegister());
2639}
2640
Aart Bikc5d47542016-01-27 17:00:35 -08002641static void GenBitCount(X86_64Assembler* assembler,
2642 CodeGeneratorX86_64* codegen,
2643 HInvoke* invoke,
2644 bool is_long) {
Aart Bik3f67e692016-01-15 14:35:12 -08002645 LocationSummary* locations = invoke->GetLocations();
2646 Location src = locations->InAt(0);
2647 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2648
2649 if (invoke->InputAt(0)->IsConstant()) {
2650 // Evaluate this at compile time.
2651 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002652 int32_t result = is_long
Aart Bik3f67e692016-01-15 14:35:12 -08002653 ? POPCOUNT(static_cast<uint64_t>(value))
2654 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002655 codegen->Load32BitValue(out, result);
Aart Bik3f67e692016-01-15 14:35:12 -08002656 return;
2657 }
2658
2659 if (src.IsRegister()) {
2660 if (is_long) {
2661 __ popcntq(out, src.AsRegister<CpuRegister>());
2662 } else {
2663 __ popcntl(out, src.AsRegister<CpuRegister>());
2664 }
2665 } else if (is_long) {
2666 DCHECK(src.IsDoubleStackSlot());
2667 __ popcntq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2668 } else {
2669 DCHECK(src.IsStackSlot());
2670 __ popcntl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2671 }
2672}
2673
2674void IntrinsicLocationsBuilderX86_64::VisitIntegerBitCount(HInvoke* invoke) {
2675 CreateBitCountLocations(arena_, codegen_, invoke);
2676}
2677
2678void IntrinsicCodeGeneratorX86_64::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002679 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bik3f67e692016-01-15 14:35:12 -08002680}
2681
2682void IntrinsicLocationsBuilderX86_64::VisitLongBitCount(HInvoke* invoke) {
2683 CreateBitCountLocations(arena_, codegen_, invoke);
2684}
2685
2686void IntrinsicCodeGeneratorX86_64::VisitLongBitCount(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002687 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
2688}
2689
Aart Bikc5d47542016-01-27 17:00:35 -08002690static void CreateOneBitLocations(ArenaAllocator* arena, HInvoke* invoke, bool is_high) {
2691 LocationSummary* locations = new (arena) LocationSummary(invoke,
2692 LocationSummary::kNoCall,
2693 kIntrinsified);
2694 locations->SetInAt(0, Location::Any());
2695 locations->SetOut(Location::RequiresRegister());
2696 locations->AddTemp(is_high ? Location::RegisterLocation(RCX) // needs CL
2697 : Location::RequiresRegister()); // any will do
2698}
2699
2700static void GenOneBit(X86_64Assembler* assembler,
2701 CodeGeneratorX86_64* codegen,
2702 HInvoke* invoke,
2703 bool is_high, bool is_long) {
2704 LocationSummary* locations = invoke->GetLocations();
2705 Location src = locations->InAt(0);
2706 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2707
2708 if (invoke->InputAt(0)->IsConstant()) {
2709 // Evaluate this at compile time.
2710 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2711 if (value == 0) {
2712 __ xorl(out, out); // Clears upper bits too.
2713 return;
2714 }
2715 // Nonzero value.
2716 if (is_high) {
2717 value = is_long ? 63 - CLZ(static_cast<uint64_t>(value))
2718 : 31 - CLZ(static_cast<uint32_t>(value));
2719 } else {
2720 value = is_long ? CTZ(static_cast<uint64_t>(value))
2721 : CTZ(static_cast<uint32_t>(value));
2722 }
2723 if (is_long) {
Pavel Vyssotski7f7f6da2016-06-22 12:36:10 +06002724 codegen->Load64BitValue(out, 1ULL << value);
Aart Bikc5d47542016-01-27 17:00:35 -08002725 } else {
2726 codegen->Load32BitValue(out, 1 << value);
2727 }
2728 return;
2729 }
2730
2731 // Handle the non-constant cases.
2732 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
2733 if (is_high) {
2734 // Use architectural support: basically 1 << bsr.
2735 if (src.IsRegister()) {
2736 if (is_long) {
2737 __ bsrq(tmp, src.AsRegister<CpuRegister>());
2738 } else {
2739 __ bsrl(tmp, src.AsRegister<CpuRegister>());
2740 }
2741 } else if (is_long) {
2742 DCHECK(src.IsDoubleStackSlot());
2743 __ bsrq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2744 } else {
2745 DCHECK(src.IsStackSlot());
2746 __ bsrl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2747 }
2748 // BSR sets ZF if the input was zero.
2749 NearLabel is_zero, done;
2750 __ j(kEqual, &is_zero);
2751 __ movl(out, Immediate(1)); // Clears upper bits too.
2752 if (is_long) {
2753 __ shlq(out, tmp);
2754 } else {
2755 __ shll(out, tmp);
2756 }
2757 __ jmp(&done);
2758 __ Bind(&is_zero);
2759 __ xorl(out, out); // Clears upper bits too.
2760 __ Bind(&done);
2761 } else {
2762 // Copy input into temporary.
2763 if (src.IsRegister()) {
2764 if (is_long) {
2765 __ movq(tmp, src.AsRegister<CpuRegister>());
2766 } else {
2767 __ movl(tmp, src.AsRegister<CpuRegister>());
2768 }
2769 } else if (is_long) {
2770 DCHECK(src.IsDoubleStackSlot());
2771 __ movq(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2772 } else {
2773 DCHECK(src.IsStackSlot());
2774 __ movl(tmp, Address(CpuRegister(RSP), src.GetStackIndex()));
2775 }
2776 // Do the bit twiddling: basically tmp & -tmp;
2777 if (is_long) {
2778 __ movq(out, tmp);
2779 __ negq(tmp);
2780 __ andq(out, tmp);
2781 } else {
2782 __ movl(out, tmp);
2783 __ negl(tmp);
2784 __ andl(out, tmp);
2785 }
2786 }
2787}
2788
2789void IntrinsicLocationsBuilderX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2790 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2791}
2792
2793void IntrinsicCodeGeneratorX86_64::VisitIntegerHighestOneBit(HInvoke* invoke) {
2794 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ false);
2795}
2796
2797void IntrinsicLocationsBuilderX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2798 CreateOneBitLocations(arena_, invoke, /* is_high */ true);
2799}
2800
2801void IntrinsicCodeGeneratorX86_64::VisitLongHighestOneBit(HInvoke* invoke) {
2802 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ true, /* is_long */ true);
2803}
2804
2805void IntrinsicLocationsBuilderX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2806 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2807}
2808
2809void IntrinsicCodeGeneratorX86_64::VisitIntegerLowestOneBit(HInvoke* invoke) {
2810 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ false);
2811}
2812
2813void IntrinsicLocationsBuilderX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2814 CreateOneBitLocations(arena_, invoke, /* is_high */ false);
2815}
2816
2817void IntrinsicCodeGeneratorX86_64::VisitLongLowestOneBit(HInvoke* invoke) {
2818 GenOneBit(GetAssembler(), codegen_, invoke, /* is_high */ false, /* is_long */ true);
Aart Bik3f67e692016-01-15 14:35:12 -08002819}
2820
Mark Mendelld5897672015-08-12 21:16:41 -04002821static void CreateLeadingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2822 LocationSummary* locations = new (arena) LocationSummary(invoke,
2823 LocationSummary::kNoCall,
2824 kIntrinsified);
2825 locations->SetInAt(0, Location::Any());
2826 locations->SetOut(Location::RequiresRegister());
2827}
2828
Aart Bikc5d47542016-01-27 17:00:35 -08002829static void GenLeadingZeros(X86_64Assembler* assembler,
2830 CodeGeneratorX86_64* codegen,
2831 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002832 LocationSummary* locations = invoke->GetLocations();
2833 Location src = locations->InAt(0);
2834 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2835
2836 int zero_value_result = is_long ? 64 : 32;
2837 if (invoke->InputAt(0)->IsConstant()) {
2838 // Evaluate this at compile time.
2839 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2840 if (value == 0) {
2841 value = zero_value_result;
2842 } else {
2843 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2844 }
Aart Bikc5d47542016-01-27 17:00:35 -08002845 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002846 return;
2847 }
2848
2849 // Handle the non-constant cases.
2850 if (src.IsRegister()) {
2851 if (is_long) {
2852 __ bsrq(out, src.AsRegister<CpuRegister>());
2853 } else {
2854 __ bsrl(out, src.AsRegister<CpuRegister>());
2855 }
2856 } else if (is_long) {
2857 DCHECK(src.IsDoubleStackSlot());
2858 __ bsrq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2859 } else {
2860 DCHECK(src.IsStackSlot());
2861 __ bsrl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2862 }
2863
2864 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002865 NearLabel is_zero, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002866 __ j(kEqual, &is_zero);
2867
2868 // Correct the result from BSR to get the CLZ result.
2869 __ xorl(out, Immediate(zero_value_result - 1));
2870 __ jmp(&done);
2871
2872 // Fix the zero case with the expected result.
2873 __ Bind(&is_zero);
2874 __ movl(out, Immediate(zero_value_result));
2875
2876 __ Bind(&done);
2877}
2878
2879void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
2880 CreateLeadingZeroLocations(arena_, invoke);
2881}
2882
2883void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002884 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002885}
2886
2887void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
2888 CreateLeadingZeroLocations(arena_, invoke);
2889}
2890
2891void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002892 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002893}
2894
Mark Mendell2d554792015-09-15 21:45:18 -04002895static void CreateTrailingZeroLocations(ArenaAllocator* arena, HInvoke* invoke) {
2896 LocationSummary* locations = new (arena) LocationSummary(invoke,
2897 LocationSummary::kNoCall,
2898 kIntrinsified);
2899 locations->SetInAt(0, Location::Any());
2900 locations->SetOut(Location::RequiresRegister());
2901}
2902
Aart Bikc5d47542016-01-27 17:00:35 -08002903static void GenTrailingZeros(X86_64Assembler* assembler,
2904 CodeGeneratorX86_64* codegen,
2905 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002906 LocationSummary* locations = invoke->GetLocations();
2907 Location src = locations->InAt(0);
2908 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2909
2910 int zero_value_result = is_long ? 64 : 32;
2911 if (invoke->InputAt(0)->IsConstant()) {
2912 // Evaluate this at compile time.
2913 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2914 if (value == 0) {
2915 value = zero_value_result;
2916 } else {
2917 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2918 }
Aart Bikc5d47542016-01-27 17:00:35 -08002919 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002920 return;
2921 }
2922
2923 // Handle the non-constant cases.
2924 if (src.IsRegister()) {
2925 if (is_long) {
2926 __ bsfq(out, src.AsRegister<CpuRegister>());
2927 } else {
2928 __ bsfl(out, src.AsRegister<CpuRegister>());
2929 }
2930 } else if (is_long) {
2931 DCHECK(src.IsDoubleStackSlot());
2932 __ bsfq(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2933 } else {
2934 DCHECK(src.IsStackSlot());
2935 __ bsfl(out, Address(CpuRegister(RSP), src.GetStackIndex()));
2936 }
2937
2938 // BSF sets ZF if the input was zero, and the output is undefined.
2939 NearLabel done;
2940 __ j(kNotEqual, &done);
2941
2942 // Fix the zero case with the expected result.
2943 __ movl(out, Immediate(zero_value_result));
2944
2945 __ Bind(&done);
2946}
2947
2948void IntrinsicLocationsBuilderX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
2949 CreateTrailingZeroLocations(arena_, invoke);
2950}
2951
2952void IntrinsicCodeGeneratorX86_64::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002953 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002954}
2955
2956void IntrinsicLocationsBuilderX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
2957 CreateTrailingZeroLocations(arena_, invoke);
2958}
2959
2960void IntrinsicCodeGeneratorX86_64::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bikc5d47542016-01-27 17:00:35 -08002961 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
2962}
2963
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002964void IntrinsicLocationsBuilderX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2965 InvokeRuntimeCallingConvention calling_convention;
2966 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2967 invoke,
2968 codegen_,
2969 Location::RegisterLocation(RAX),
2970 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2971}
2972
2973void IntrinsicCodeGeneratorX86_64::VisitIntegerValueOf(HInvoke* invoke) {
2974 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2975 LocationSummary* locations = invoke->GetLocations();
2976 X86_64Assembler* assembler = GetAssembler();
2977
2978 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
2979 InvokeRuntimeCallingConvention calling_convention;
2980 if (invoke->InputAt(0)->IsConstant()) {
2981 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2982 if (value >= info.low && value <= info.high) {
2983 // Just embed the j.l.Integer in the code.
2984 ScopedObjectAccess soa(Thread::Current());
2985 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2986 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2987 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
Colin Cross0bd97172017-03-15 16:33:27 -07002988 __ movl(out, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002989 } else {
2990 // Allocate and initialize a new j.l.Integer.
2991 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2992 // JIT object table.
Colin Cross0bd97172017-03-15 16:33:27 -07002993 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002994 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07002995 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002996 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2997 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2998 __ movl(Address(out, info.value_offset), Immediate(value));
2999 }
3000 } else {
3001 CpuRegister in = locations->InAt(0).AsRegister<CpuRegister>();
3002 // Check bounds of our cache.
3003 __ leal(out, Address(in, -info.low));
3004 __ cmpl(out, Immediate(info.high - info.low + 1));
3005 NearLabel allocate, done;
3006 __ j(kAboveEqual, &allocate);
3007 // If the value is within the bounds, load the j.l.Integer directly from the array.
3008 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3009 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
Colin Cross0bd97172017-03-15 16:33:27 -07003010 if (data_offset + address <= std::numeric_limits<int32_t>::max()) {
3011 __ movl(out, Address(out, TIMES_4, data_offset + address));
3012 } else {
3013 CpuRegister temp = CpuRegister(calling_convention.GetRegisterAt(0));
3014 __ movl(temp, Immediate(static_cast<int32_t>(data_offset + address)));
3015 __ movl(out, Address(temp, out, TIMES_4, 0));
3016 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003017 __ MaybeUnpoisonHeapReference(out);
3018 __ jmp(&done);
3019 __ Bind(&allocate);
3020 // Otherwise allocate and initialize a new j.l.Integer.
Colin Cross0bd97172017-03-15 16:33:27 -07003021 CpuRegister argument = CpuRegister(calling_convention.GetRegisterAt(0));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003022 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
Colin Cross0bd97172017-03-15 16:33:27 -07003023 __ movl(argument, Immediate(static_cast<int32_t>(address)));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003024 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3025 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3026 __ movl(Address(out, info.value_offset), in);
3027 __ Bind(&done);
3028 }
3029}
3030
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003031void IntrinsicLocationsBuilderX86_64::VisitThreadInterrupted(HInvoke* invoke) {
3032 LocationSummary* locations = new (arena_) LocationSummary(invoke,
3033 LocationSummary::kNoCall,
3034 kIntrinsified);
3035 locations->SetOut(Location::RequiresRegister());
3036}
3037
3038void IntrinsicCodeGeneratorX86_64::VisitThreadInterrupted(HInvoke* invoke) {
3039 X86_64Assembler* assembler = GetAssembler();
3040 CpuRegister out = invoke->GetLocations()->Out().AsRegister<CpuRegister>();
3041 Address address = Address::Absolute
3042 (Thread::InterruptedOffset<kX86_64PointerSize>().Int32Value(), /* no_rip */ true);
3043 NearLabel done;
3044 __ gs()->movl(out, address);
3045 __ testl(out, out);
3046 __ j(kEqual, &done);
3047 __ gs()->movl(address, Immediate(0));
3048 codegen_->MemoryFence();
3049 __ Bind(&done);
3050}
3051
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003052UNIMPLEMENTED_INTRINSIC(X86_64, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003053UNIMPLEMENTED_INTRINSIC(X86_64, FloatIsInfinite)
3054UNIMPLEMENTED_INTRINSIC(X86_64, DoubleIsInfinite)
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003055
Aart Bikff7d89c2016-11-07 08:49:28 -08003056UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOf);
3057UNIMPLEMENTED_INTRINSIC(X86_64, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003058UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferAppend);
3059UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferLength);
3060UNIMPLEMENTED_INTRINSIC(X86_64, StringBufferToString);
3061UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderAppend);
3062UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderLength);
3063UNIMPLEMENTED_INTRINSIC(X86_64, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003064
Aart Bik0e54c012016-03-04 12:08:31 -08003065// 1.8.
3066UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddInt)
3067UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndAddLong)
3068UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetInt)
3069UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetLong)
3070UNIMPLEMENTED_INTRINSIC(X86_64, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003071
Aart Bik2f9fcc92016-03-01 15:16:54 -08003072UNREACHABLE_INTRINSICS(X86_64)
Roland Levillain4d027112015-07-01 15:41:14 +01003073
3074#undef __
3075
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003076} // namespace x86_64
3077} // namespace art