blob: a73f4e8b94fc5a25b9874234cbd73609c49549ae [file] [log] [blame]
Mark Mendell09ed1a32015-03-25 08:30:06 -04001/*
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.h"
18
Andreas Gampe21030dd2015-05-07 14:46:15 -070019#include <limits>
20
Mark Mendellfb8d2792015-03-31 22:16:59 -040021#include "arch/x86/instruction_set_features_x86.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method.h"
Mark Mendelld5897672015-08-12 21:16:41 -040023#include "base/bit_utils.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "code_generator_x86.h"
25#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040027#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"
Mark Mendell09ed1a32015-03-25 08:30:06 -040030#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"
Mark Mendell09ed1a32015-03-25 08:30:06 -040033#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"
Mark Mendell09ed1a32015-03-25 08:30:06 -040036#include "utils/x86/assembler_x86.h"
37#include "utils/x86/constants_x86.h"
38
39namespace art {
40
41namespace x86 {
42
Mark Mendellfb8d2792015-03-31 22:16:59 -040043IntrinsicLocationsBuilderX86::IntrinsicLocationsBuilderX86(CodeGeneratorX86* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +010044 : allocator_(codegen->GetGraph()->GetAllocator()),
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000045 codegen_(codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -040046}
47
48
Mark Mendell09ed1a32015-03-25 08:30:06 -040049X86Assembler* IntrinsicCodeGeneratorX86::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010050 return down_cast<X86Assembler*>(codegen_->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -040051}
52
53ArenaAllocator* IntrinsicCodeGeneratorX86::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010054 return codegen_->GetGraph()->GetAllocator();
Mark Mendell09ed1a32015-03-25 08:30:06 -040055}
56
57bool IntrinsicLocationsBuilderX86::TryDispatch(HInvoke* invoke) {
58 Dispatch(invoke);
59 LocationSummary* res = invoke->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +000060 if (res == nullptr) {
61 return false;
62 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000063 return res->Intrinsified();
Mark Mendell09ed1a32015-03-25 08:30:06 -040064}
65
Roland Levillainec525fc2015-04-28 15:50:20 +010066static void MoveArguments(HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010067 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010068 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Mark Mendell09ed1a32015-03-25 08:30:06 -040069}
70
Andreas Gampe85b62f22015-09-09 13:15:38 -070071using IntrinsicSlowPathX86 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86>;
Mark Mendell09ed1a32015-03-25 08:30:06 -040072
Roland Levillain0b671c02016-08-19 12:02:34 +010073// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
74#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
75
76// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
77class ReadBarrierSystemArrayCopySlowPathX86 : public SlowPathCode {
78 public:
79 explicit ReadBarrierSystemArrayCopySlowPathX86(HInstruction* instruction)
80 : SlowPathCode(instruction) {
81 DCHECK(kEmitCompilerReadBarrier);
82 DCHECK(kUseBakerReadBarrier);
83 }
84
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010085 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0b671c02016-08-19 12:02:34 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
87 LocationSummary* locations = instruction_->GetLocations();
88 DCHECK(locations->CanCall());
89 DCHECK(instruction_->IsInvokeStaticOrDirect())
90 << "Unexpected instruction in read barrier arraycopy slow path: "
91 << instruction_->DebugName();
92 DCHECK(instruction_->GetLocations()->Intrinsified());
93 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
94
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +010096 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
97
98 Register src = locations->InAt(0).AsRegister<Register>();
99 Location src_pos = locations->InAt(1);
100 Register dest = locations->InAt(2).AsRegister<Register>();
101 Location dest_pos = locations->InAt(3);
102 Location length = locations->InAt(4);
103 Location temp1_loc = locations->GetTemp(0);
104 Register temp1 = temp1_loc.AsRegister<Register>();
105 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
106 Register temp3 = locations->GetTemp(2).AsRegister<Register>();
107
108 __ Bind(GetEntryLabel());
109 // In this code path, registers `temp1`, `temp2`, and `temp3`
110 // (resp.) are not used for the base source address, the base
111 // destination address, and the end source address (resp.), as in
112 // other SystemArrayCopy intrinsic code paths. Instead they are
113 // (resp.) used for:
114 // - the loop index (`i`);
115 // - the source index (`src_index`) and the loaded (source)
116 // reference (`value`); and
117 // - the destination index (`dest_index`).
118
119 // i = 0
120 __ xorl(temp1, temp1);
121 NearLabel loop;
122 __ Bind(&loop);
123 // value = src_array[i + src_pos]
124 if (src_pos.IsConstant()) {
125 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
126 int32_t adjusted_offset = offset + constant * element_size;
127 __ movl(temp2, Address(src, temp1, ScaleFactor::TIMES_4, adjusted_offset));
128 } else {
129 __ leal(temp2, Address(src_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0));
130 __ movl(temp2, Address(src, temp2, ScaleFactor::TIMES_4, offset));
131 }
132 __ MaybeUnpoisonHeapReference(temp2);
133 // TODO: Inline the mark bit check before calling the runtime?
134 // value = ReadBarrier::Mark(value)
135 // No need to save live registers; it's taken care of by the
136 // entrypoint. Also, there is no need to update the stack mask,
137 // as this runtime call will not trigger a garbage collection.
138 // (See ReadBarrierMarkSlowPathX86::EmitNativeCode for more
139 // explanations.)
140 DCHECK_NE(temp2, ESP);
141 DCHECK(0 <= temp2 && temp2 < kNumberOfCpuRegisters) << temp2;
Roland Levillain97c46462017-05-11 14:04:03 +0100142 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +0100143 // This runtime call does not require a stack map.
144 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
145 __ MaybePoisonHeapReference(temp2);
146 // dest_array[i + dest_pos] = value
147 if (dest_pos.IsConstant()) {
148 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
149 int32_t adjusted_offset = offset + constant * element_size;
150 __ movl(Address(dest, temp1, ScaleFactor::TIMES_4, adjusted_offset), temp2);
151 } else {
152 __ leal(temp3, Address(dest_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0));
153 __ movl(Address(dest, temp3, ScaleFactor::TIMES_4, offset), temp2);
154 }
155 // ++i
156 __ addl(temp1, Immediate(1));
157 // if (i != length) goto loop
158 x86_codegen->GenerateIntCompare(temp1_loc, length);
159 __ j(kNotEqual, &loop);
160 __ jmp(GetExitLabel());
161 }
162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100163 const char* GetDescription() const override { return "ReadBarrierSystemArrayCopySlowPathX86"; }
Roland Levillain0b671c02016-08-19 12:02:34 +0100164
165 private:
166 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86);
167};
168
169#undef __
170
Mark Mendell09ed1a32015-03-25 08:30:06 -0400171#define __ assembler->
172
Vladimir Markoca6fff82017-10-03 14:49:14 +0100173static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) {
174 LocationSummary* locations =
175 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400176 locations->SetInAt(0, Location::RequiresFpuRegister());
177 locations->SetOut(Location::RequiresRegister());
178 if (is64bit) {
179 locations->AddTemp(Location::RequiresFpuRegister());
180 }
181}
182
Vladimir Markoca6fff82017-10-03 14:49:14 +0100183static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) {
184 LocationSummary* locations =
185 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400186 locations->SetInAt(0, Location::RequiresRegister());
187 locations->SetOut(Location::RequiresFpuRegister());
188 if (is64bit) {
189 locations->AddTemp(Location::RequiresFpuRegister());
190 locations->AddTemp(Location::RequiresFpuRegister());
191 }
192}
193
194static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
195 Location input = locations->InAt(0);
196 Location output = locations->Out();
197 if (is64bit) {
198 // Need to use the temporary.
199 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
200 __ movsd(temp, input.AsFpuRegister<XmmRegister>());
201 __ movd(output.AsRegisterPairLow<Register>(), temp);
202 __ psrlq(temp, Immediate(32));
203 __ movd(output.AsRegisterPairHigh<Register>(), temp);
204 } else {
205 __ movd(output.AsRegister<Register>(), input.AsFpuRegister<XmmRegister>());
206 }
207}
208
209static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
210 Location input = locations->InAt(0);
211 Location output = locations->Out();
212 if (is64bit) {
213 // Need to use the temporary.
214 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
215 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
216 __ movd(temp1, input.AsRegisterPairLow<Register>());
217 __ movd(temp2, input.AsRegisterPairHigh<Register>());
218 __ punpckldq(temp1, temp2);
219 __ movsd(output.AsFpuRegister<XmmRegister>(), temp1);
220 } else {
221 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<Register>());
222 }
223}
224
225void IntrinsicLocationsBuilderX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100226 CreateFPToIntLocations(allocator_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400227}
228void IntrinsicLocationsBuilderX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100229 CreateIntToFPLocations(allocator_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400230}
231
232void IntrinsicCodeGeneratorX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000233 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400234}
235void IntrinsicCodeGeneratorX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000236 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400237}
238
239void IntrinsicLocationsBuilderX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100240 CreateFPToIntLocations(allocator_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400241}
242void IntrinsicLocationsBuilderX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100243 CreateIntToFPLocations(allocator_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400244}
245
246void IntrinsicCodeGeneratorX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000247 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400248}
249void IntrinsicCodeGeneratorX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000250 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400251}
252
Vladimir Markoca6fff82017-10-03 14:49:14 +0100253static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
254 LocationSummary* locations =
255 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400256 locations->SetInAt(0, Location::RequiresRegister());
257 locations->SetOut(Location::SameAsFirstInput());
258}
259
Vladimir Markoca6fff82017-10-03 14:49:14 +0100260static void CreateLongToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
261 LocationSummary* locations =
262 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400263 locations->SetInAt(0, Location::RequiresRegister());
264 locations->SetOut(Location::RequiresRegister());
265}
266
Vladimir Markoca6fff82017-10-03 14:49:14 +0100267static void CreateLongToLongLocations(ArenaAllocator* allocator, HInvoke* invoke) {
268 LocationSummary* locations =
269 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400270 locations->SetInAt(0, Location::RequiresRegister());
271 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
272}
273
274static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100275 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -0400276 X86Assembler* assembler) {
277 Register out = locations->Out().AsRegister<Register>();
278
279 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100280 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -0400281 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
282 __ bswapl(out);
283 __ sarl(out, Immediate(16));
284 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100285 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -0400286 __ bswapl(out);
287 break;
288 default:
289 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
290 UNREACHABLE();
291 }
292}
293
294void IntrinsicLocationsBuilderX86::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100295 CreateIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400296}
297
298void IntrinsicCodeGeneratorX86::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100299 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400300}
301
Mark Mendell58d25fd2015-04-03 14:52:31 -0400302void IntrinsicLocationsBuilderX86::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100303 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -0400304}
305
306void IntrinsicCodeGeneratorX86::VisitLongReverseBytes(HInvoke* invoke) {
307 LocationSummary* locations = invoke->GetLocations();
308 Location input = locations->InAt(0);
309 Register input_lo = input.AsRegisterPairLow<Register>();
310 Register input_hi = input.AsRegisterPairHigh<Register>();
311 Location output = locations->Out();
312 Register output_lo = output.AsRegisterPairLow<Register>();
313 Register output_hi = output.AsRegisterPairHigh<Register>();
314
315 X86Assembler* assembler = GetAssembler();
316 // Assign the inputs to the outputs, mixing low/high.
317 __ movl(output_lo, input_hi);
318 __ movl(output_hi, input_lo);
319 __ bswapl(output_lo);
320 __ bswapl(output_hi);
321}
322
Mark Mendell09ed1a32015-03-25 08:30:06 -0400323void IntrinsicLocationsBuilderX86::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100324 CreateIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400325}
326
327void IntrinsicCodeGeneratorX86::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100328 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400329}
330
Vladimir Markoca6fff82017-10-03 14:49:14 +0100331static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
332 LocationSummary* locations =
333 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400334 locations->SetInAt(0, Location::RequiresFpuRegister());
335 locations->SetOut(Location::RequiresFpuRegister());
336}
337
338void IntrinsicLocationsBuilderX86::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100339 CreateFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400340}
341
342void IntrinsicCodeGeneratorX86::VisitMathSqrt(HInvoke* invoke) {
343 LocationSummary* locations = invoke->GetLocations();
344 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
345 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
346
347 GetAssembler()->sqrtsd(out, in);
348}
349
Mark Mendellfb8d2792015-03-31 22:16:59 -0400350static void InvokeOutOfLineIntrinsic(CodeGeneratorX86* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100351 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400352
353 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100354 codegen->GenerateStaticOrDirectCall(invoke->AsInvokeStaticOrDirect(),
355 Location::RegisterLocation(EAX));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400356
357 // Copy the result back to the expected output.
358 Location out = invoke->GetLocations()->Out();
359 if (out.IsValid()) {
360 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700361 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400362 }
363}
364
Vladimir Markoca6fff82017-10-03 14:49:14 +0100365static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator,
366 HInvoke* invoke,
367 CodeGeneratorX86* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400368 // Do we have instruction support?
369 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100370 CreateFPToFPLocations(allocator, invoke);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400371 return;
372 }
373
374 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100375 LocationSummary* locations =
376 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400377 InvokeRuntimeCallingConvention calling_convention;
378 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
379 locations->SetOut(Location::FpuRegisterLocation(XMM0));
380 // Needs to be EAX for the invoke.
381 locations->AddTemp(Location::RegisterLocation(EAX));
382}
383
384static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86* codegen,
385 HInvoke* invoke,
386 X86Assembler* assembler,
387 int round_mode) {
388 LocationSummary* locations = invoke->GetLocations();
389 if (locations->WillCall()) {
390 InvokeOutOfLineIntrinsic(codegen, invoke);
391 } else {
392 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
393 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
394 __ roundsd(out, in, Immediate(round_mode));
395 }
396}
397
398void IntrinsicLocationsBuilderX86::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100399 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400400}
401
402void IntrinsicCodeGeneratorX86::VisitMathCeil(HInvoke* invoke) {
403 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
404}
405
406void IntrinsicLocationsBuilderX86::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100407 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400408}
409
410void IntrinsicCodeGeneratorX86::VisitMathFloor(HInvoke* invoke) {
411 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
412}
413
414void IntrinsicLocationsBuilderX86::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100415 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400416}
417
418void IntrinsicCodeGeneratorX86::VisitMathRint(HInvoke* invoke) {
419 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
420}
421
Mark Mendellfb8d2792015-03-31 22:16:59 -0400422void IntrinsicLocationsBuilderX86::VisitMathRoundFloat(HInvoke* invoke) {
423 // Do we have instruction support?
424 if (codegen_->GetInstructionSetFeatures().HasSSE4_1()) {
Aart Bik2c9f4952016-08-01 16:52:27 -0700425 HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect();
426 DCHECK(static_or_direct != nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100427 LocationSummary* locations =
428 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400429 locations->SetInAt(0, Location::RequiresFpuRegister());
Aart Bik2c9f4952016-08-01 16:52:27 -0700430 if (static_or_direct->HasSpecialInput() &&
431 invoke->InputAt(
432 static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) {
433 locations->SetInAt(1, Location::RequiresRegister());
434 }
Nicolas Geoffrayd9b92402015-04-21 10:02:22 +0100435 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400436 locations->AddTemp(Location::RequiresFpuRegister());
437 locations->AddTemp(Location::RequiresFpuRegister());
438 return;
439 }
440
441 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100442 LocationSummary* locations =
443 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400444 InvokeRuntimeCallingConvention calling_convention;
445 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
446 locations->SetOut(Location::RegisterLocation(EAX));
447 // Needs to be EAX for the invoke.
448 locations->AddTemp(Location::RegisterLocation(EAX));
449}
450
451void IntrinsicCodeGeneratorX86::VisitMathRoundFloat(HInvoke* invoke) {
452 LocationSummary* locations = invoke->GetLocations();
Aart Bik2c9f4952016-08-01 16:52:27 -0700453 if (locations->WillCall()) { // TODO: can we reach this?
Mark Mendellfb8d2792015-03-31 22:16:59 -0400454 InvokeOutOfLineIntrinsic(codegen_, invoke);
455 return;
456 }
457
Mark Mendellfb8d2792015-03-31 22:16:59 -0400458 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700459 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
460 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400461 Register out = locations->Out().AsRegister<Register>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700462 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400463 X86Assembler* assembler = GetAssembler();
464
Aart Bik2c9f4952016-08-01 16:52:27 -0700465 // Since no direct x86 rounding instruction matches the required semantics,
466 // this intrinsic is implemented as follows:
467 // result = floor(in);
468 // if (in - result >= 0.5f)
469 // result = result + 1.0f;
470 __ movss(t2, in);
471 __ roundss(t1, in, Immediate(1));
472 __ subss(t2, t1);
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700473 if (locations->GetInputCount() == 2 && locations->InAt(1).IsValid()) {
474 // Direct constant area available.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000475 HX86ComputeBaseMethodAddress* method_address =
476 invoke->InputAt(1)->AsX86ComputeBaseMethodAddress();
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700477 Register constant_area = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000478 __ comiss(t2, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(0.5f),
479 method_address,
480 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700481 __ j(kBelow, &skip_incr);
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000482 __ addss(t1, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(1.0f),
483 method_address,
484 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700485 __ Bind(&skip_incr);
486 } else {
487 // No constant area: go through stack.
488 __ pushl(Immediate(bit_cast<int32_t, float>(0.5f)));
489 __ pushl(Immediate(bit_cast<int32_t, float>(1.0f)));
490 __ comiss(t2, Address(ESP, 4));
491 __ j(kBelow, &skip_incr);
492 __ addss(t1, Address(ESP, 0));
493 __ Bind(&skip_incr);
494 __ addl(ESP, Immediate(8));
495 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400496
Aart Bik2c9f4952016-08-01 16:52:27 -0700497 // Final conversion to an integer. Unfortunately this also does not have a
498 // direct x86 instruction, since NaN should map to 0 and large positive
499 // values need to be clipped to the extreme value.
Mark Mendellfb8d2792015-03-31 22:16:59 -0400500 __ movl(out, Immediate(kPrimIntMax));
Aart Bik2c9f4952016-08-01 16:52:27 -0700501 __ cvtsi2ss(t2, out);
502 __ comiss(t1, t2);
503 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
504 __ movl(out, Immediate(0)); // does not change flags
505 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
506 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400507 __ Bind(&done);
508}
509
Vladimir Markoca6fff82017-10-03 14:49:14 +0100510static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
511 LocationSummary* locations =
512 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400513 InvokeRuntimeCallingConvention calling_convention;
514 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
515 locations->SetOut(Location::FpuRegisterLocation(XMM0));
516}
517
518static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86* codegen, QuickEntrypointEnum entry) {
519 LocationSummary* locations = invoke->GetLocations();
520 DCHECK(locations->WillCall());
521 DCHECK(invoke->IsInvokeStaticOrDirect());
522 X86Assembler* assembler = codegen->GetAssembler();
523
524 // We need some place to pass the parameters.
525 __ subl(ESP, Immediate(16));
526 __ cfi().AdjustCFAOffset(16);
527
528 // Pass the parameters at the bottom of the stack.
529 __ movsd(Address(ESP, 0), XMM0);
530
531 // If we have a second parameter, pass it next.
532 if (invoke->GetNumberOfArguments() == 2) {
533 __ movsd(Address(ESP, 8), XMM1);
534 }
535
536 // Now do the actual call.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100537 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400538
539 // Extract the return value from the FP stack.
540 __ fstpl(Address(ESP, 0));
541 __ movsd(XMM0, Address(ESP, 0));
542
543 // And clean up the stack.
544 __ addl(ESP, Immediate(16));
545 __ cfi().AdjustCFAOffset(-16);
Mark Mendella4f12202015-08-06 15:23:34 -0400546}
547
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +0530548static void CreateLowestOneBitLocations(ArenaAllocator* allocator, bool is_long, HInvoke* invoke) {
549 LocationSummary* locations =
550 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
551 if (is_long) {
552 locations->SetInAt(0, Location::RequiresRegister());
553 } else {
554 locations->SetInAt(0, Location::Any());
555 }
556 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
557}
558
559static void GenLowestOneBit(X86Assembler* assembler,
560 CodeGeneratorX86* codegen,
561 bool is_long,
562 HInvoke* invoke) {
563 LocationSummary* locations = invoke->GetLocations();
564 Location src = locations->InAt(0);
565 Location out_loc = locations->Out();
566
567 if (invoke->InputAt(0)->IsConstant()) {
568 // Evaluate this at compile time.
569 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
570 if (value == 0) {
571 if (is_long) {
572 __ xorl(out_loc.AsRegisterPairLow<Register>(), out_loc.AsRegisterPairLow<Register>());
573 __ xorl(out_loc.AsRegisterPairHigh<Register>(), out_loc.AsRegisterPairHigh<Register>());
574 } else {
575 __ xorl(out_loc.AsRegister<Register>(), out_loc.AsRegister<Register>());
576 }
577 return;
578 }
579 // Nonzero value.
580 value = is_long ? CTZ(static_cast<uint64_t>(value))
581 : CTZ(static_cast<uint32_t>(value));
582 if (is_long) {
583 if (value >= 32) {
584 int shift = value-32;
585 codegen->Load32BitValue(out_loc.AsRegisterPairLow<Register>(), 0);
586 codegen->Load32BitValue(out_loc.AsRegisterPairHigh<Register>(), 1 << shift);
587 } else {
588 codegen->Load32BitValue(out_loc.AsRegisterPairLow<Register>(), 1 << value);
589 codegen->Load32BitValue(out_loc.AsRegisterPairHigh<Register>(), 0);
590 }
591 } else {
592 codegen->Load32BitValue(out_loc.AsRegister<Register>(), 1 << value);
593 }
594 return;
595 }
596 // Handle non constant case
597 if (is_long) {
598 DCHECK(src.IsRegisterPair());
599 Register src_lo = src.AsRegisterPairLow<Register>();
600 Register src_hi = src.AsRegisterPairHigh<Register>();
601
602 Register out_lo = out_loc.AsRegisterPairLow<Register>();
603 Register out_hi = out_loc.AsRegisterPairHigh<Register>();
604
605 __ movl(out_lo, src_lo);
606 __ movl(out_hi, src_hi);
607
608 __ negl(out_lo);
609 __ adcl(out_hi, Immediate(0));
610 __ negl(out_hi);
611
612 __ andl(out_lo, src_lo);
613 __ andl(out_hi, src_hi);
614 } else {
615 if (codegen->GetInstructionSetFeatures().HasAVX2() && src.IsRegister()) {
616 Register out = out_loc.AsRegister<Register>();
617 __ blsi(out, src.AsRegister<Register>());
618 } else {
619 Register out = out_loc.AsRegister<Register>();
620 // Do tmp & -tmp
621 if (src.IsRegister()) {
622 __ movl(out, src.AsRegister<Register>());
623 } else {
624 DCHECK(src.IsStackSlot());
625 __ movl(out, Address(ESP, src.GetStackIndex()));
626 }
627 __ negl(out);
628
629 if (src.IsRegister()) {
630 __ andl(out, src.AsRegister<Register>());
631 } else {
632 __ andl(out, Address(ESP, src.GetStackIndex()));
633 }
634 }
635 }
636}
637
Mark Mendella4f12202015-08-06 15:23:34 -0400638void IntrinsicLocationsBuilderX86::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100639 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400640}
641
642void IntrinsicCodeGeneratorX86::VisitMathCos(HInvoke* invoke) {
643 GenFPToFPCall(invoke, codegen_, kQuickCos);
644}
645
646void IntrinsicLocationsBuilderX86::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100647 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400648}
649
650void IntrinsicCodeGeneratorX86::VisitMathSin(HInvoke* invoke) {
651 GenFPToFPCall(invoke, codegen_, kQuickSin);
652}
653
654void IntrinsicLocationsBuilderX86::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100655 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400656}
657
658void IntrinsicCodeGeneratorX86::VisitMathAcos(HInvoke* invoke) {
659 GenFPToFPCall(invoke, codegen_, kQuickAcos);
660}
661
662void IntrinsicLocationsBuilderX86::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100663 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400664}
665
666void IntrinsicCodeGeneratorX86::VisitMathAsin(HInvoke* invoke) {
667 GenFPToFPCall(invoke, codegen_, kQuickAsin);
668}
669
670void IntrinsicLocationsBuilderX86::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100671 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400672}
673
674void IntrinsicCodeGeneratorX86::VisitMathAtan(HInvoke* invoke) {
675 GenFPToFPCall(invoke, codegen_, kQuickAtan);
676}
677
678void IntrinsicLocationsBuilderX86::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100679 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400680}
681
682void IntrinsicCodeGeneratorX86::VisitMathCbrt(HInvoke* invoke) {
683 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
684}
685
686void IntrinsicLocationsBuilderX86::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100687 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400688}
689
690void IntrinsicCodeGeneratorX86::VisitMathCosh(HInvoke* invoke) {
691 GenFPToFPCall(invoke, codegen_, kQuickCosh);
692}
693
694void IntrinsicLocationsBuilderX86::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100695 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400696}
697
698void IntrinsicCodeGeneratorX86::VisitMathExp(HInvoke* invoke) {
699 GenFPToFPCall(invoke, codegen_, kQuickExp);
700}
701
702void IntrinsicLocationsBuilderX86::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100703 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400704}
705
706void IntrinsicCodeGeneratorX86::VisitMathExpm1(HInvoke* invoke) {
707 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
708}
709
710void IntrinsicLocationsBuilderX86::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100711 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400712}
713
714void IntrinsicCodeGeneratorX86::VisitMathLog(HInvoke* invoke) {
715 GenFPToFPCall(invoke, codegen_, kQuickLog);
716}
717
718void IntrinsicLocationsBuilderX86::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100719 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400720}
721
722void IntrinsicCodeGeneratorX86::VisitMathLog10(HInvoke* invoke) {
723 GenFPToFPCall(invoke, codegen_, kQuickLog10);
724}
725
726void IntrinsicLocationsBuilderX86::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100727 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400728}
729
730void IntrinsicCodeGeneratorX86::VisitMathSinh(HInvoke* invoke) {
731 GenFPToFPCall(invoke, codegen_, kQuickSinh);
732}
733
734void IntrinsicLocationsBuilderX86::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100735 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400736}
737
738void IntrinsicCodeGeneratorX86::VisitMathTan(HInvoke* invoke) {
739 GenFPToFPCall(invoke, codegen_, kQuickTan);
740}
741
742void IntrinsicLocationsBuilderX86::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100743 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400744}
745
746void IntrinsicCodeGeneratorX86::VisitMathTanh(HInvoke* invoke) {
747 GenFPToFPCall(invoke, codegen_, kQuickTanh);
748}
749
Shalini Salomi Bodapati8943fa12018-11-21 15:36:00 +0530750void IntrinsicLocationsBuilderX86::VisitIntegerLowestOneBit(HInvoke* invoke) {
751 CreateLowestOneBitLocations(allocator_, /*is_long=*/ false, invoke);
752}
753void IntrinsicCodeGeneratorX86::VisitIntegerLowestOneBit(HInvoke* invoke) {
754 GenLowestOneBit(GetAssembler(), codegen_, /*is_long=*/ false, invoke);
755}
756
757void IntrinsicLocationsBuilderX86::VisitLongLowestOneBit(HInvoke* invoke) {
758 CreateLowestOneBitLocations(allocator_, /*is_long=*/ true, invoke);
759}
760
761void IntrinsicCodeGeneratorX86::VisitLongLowestOneBit(HInvoke* invoke) {
762 GenLowestOneBit(GetAssembler(), codegen_, /*is_long=*/ true, invoke);
763}
764
Vladimir Markoca6fff82017-10-03 14:49:14 +0100765static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
766 LocationSummary* locations =
767 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400768 InvokeRuntimeCallingConvention calling_convention;
769 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
770 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
771 locations->SetOut(Location::FpuRegisterLocation(XMM0));
772}
773
774void IntrinsicLocationsBuilderX86::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100775 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400776}
777
778void IntrinsicCodeGeneratorX86::VisitMathAtan2(HInvoke* invoke) {
779 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
780}
781
Vladimir Marko4d179872018-01-19 14:50:10 +0000782void IntrinsicLocationsBuilderX86::VisitMathPow(HInvoke* invoke) {
783 CreateFPFPToFPCallLocations(allocator_, invoke);
784}
785
786void IntrinsicCodeGeneratorX86::VisitMathPow(HInvoke* invoke) {
787 GenFPToFPCall(invoke, codegen_, kQuickPow);
788}
789
Mark Mendella4f12202015-08-06 15:23:34 -0400790void IntrinsicLocationsBuilderX86::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100791 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400792}
793
794void IntrinsicCodeGeneratorX86::VisitMathHypot(HInvoke* invoke) {
795 GenFPToFPCall(invoke, codegen_, kQuickHypot);
796}
797
798void IntrinsicLocationsBuilderX86::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100799 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400800}
801
802void IntrinsicCodeGeneratorX86::VisitMathNextAfter(HInvoke* invoke) {
803 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
804}
805
Mark Mendell6bc53a92015-07-01 14:26:52 -0400806void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
807 // We need at least two of the positions or length to be an integer constant,
808 // or else we won't have enough free registers.
809 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
810 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
811 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
812
813 int num_constants =
814 ((src_pos != nullptr) ? 1 : 0)
815 + ((dest_pos != nullptr) ? 1 : 0)
816 + ((length != nullptr) ? 1 : 0);
817
818 if (num_constants < 2) {
819 // Not enough free registers.
820 return;
821 }
822
823 // As long as we are checking, we might as well check to see if the src and dest
824 // positions are >= 0.
825 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
826 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
827 // We will have to fail anyways.
828 return;
829 }
830
831 // And since we are already checking, check the length too.
832 if (length != nullptr) {
833 int32_t len = length->GetValue();
834 if (len < 0) {
835 // Just call as normal.
836 return;
837 }
838 }
839
840 // Okay, it is safe to generate inline code.
841 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100842 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400843 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
844 locations->SetInAt(0, Location::RequiresRegister());
845 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
846 locations->SetInAt(2, Location::RequiresRegister());
847 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
848 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
849
850 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
851 locations->AddTemp(Location::RegisterLocation(ESI));
852 locations->AddTemp(Location::RegisterLocation(EDI));
853 locations->AddTemp(Location::RegisterLocation(ECX));
854}
855
856static void CheckPosition(X86Assembler* assembler,
857 Location pos,
858 Register input,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100859 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700860 SlowPathCode* slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100861 Register temp,
862 bool length_is_input_length = false) {
863 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400864 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
865
866 if (pos.IsConstant()) {
867 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
868 if (pos_const == 0) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100869 if (!length_is_input_length) {
870 // Check that length(input) >= length.
871 if (length.IsConstant()) {
872 __ cmpl(Address(input, length_offset),
873 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
874 } else {
875 __ cmpl(Address(input, length_offset), length.AsRegister<Register>());
876 }
877 __ j(kLess, slow_path->GetEntryLabel());
878 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400879 } else {
880 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100881 __ movl(temp, Address(input, length_offset));
882 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400883 __ j(kLess, slow_path->GetEntryLabel());
884
885 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100886 if (length.IsConstant()) {
887 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
888 } else {
889 __ cmpl(temp, length.AsRegister<Register>());
890 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400891 __ j(kLess, slow_path->GetEntryLabel());
892 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100893 } else if (length_is_input_length) {
894 // The only way the copy can succeed is if pos is zero.
895 Register pos_reg = pos.AsRegister<Register>();
896 __ testl(pos_reg, pos_reg);
897 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400898 } else {
899 // Check that pos >= 0.
900 Register pos_reg = pos.AsRegister<Register>();
901 __ testl(pos_reg, pos_reg);
902 __ j(kLess, slow_path->GetEntryLabel());
903
904 // Check that pos <= length(input).
905 __ cmpl(Address(input, length_offset), pos_reg);
906 __ j(kLess, slow_path->GetEntryLabel());
907
908 // Check that (length(input) - pos) >= length.
909 __ movl(temp, Address(input, length_offset));
910 __ subl(temp, pos_reg);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100911 if (length.IsConstant()) {
912 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
913 } else {
914 __ cmpl(temp, length.AsRegister<Register>());
915 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400916 __ j(kLess, slow_path->GetEntryLabel());
917 }
918}
919
920void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
921 X86Assembler* assembler = GetAssembler();
922 LocationSummary* locations = invoke->GetLocations();
923
924 Register src = locations->InAt(0).AsRegister<Register>();
925 Location srcPos = locations->InAt(1);
926 Register dest = locations->InAt(2).AsRegister<Register>();
927 Location destPos = locations->InAt(3);
928 Location length = locations->InAt(4);
929
930 // Temporaries that we need for MOVSW.
931 Register src_base = locations->GetTemp(0).AsRegister<Register>();
932 DCHECK_EQ(src_base, ESI);
933 Register dest_base = locations->GetTemp(1).AsRegister<Register>();
934 DCHECK_EQ(dest_base, EDI);
935 Register count = locations->GetTemp(2).AsRegister<Register>();
936 DCHECK_EQ(count, ECX);
937
Vladimir Marko174b2e22017-10-12 13:34:49 +0100938 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400939 codegen_->AddSlowPath(slow_path);
940
941 // Bail out if the source and destination are the same (to handle overlap).
942 __ cmpl(src, dest);
943 __ j(kEqual, slow_path->GetEntryLabel());
944
945 // Bail out if the source is null.
946 __ testl(src, src);
947 __ j(kEqual, slow_path->GetEntryLabel());
948
949 // Bail out if the destination is null.
950 __ testl(dest, dest);
951 __ j(kEqual, slow_path->GetEntryLabel());
952
953 // If the length is negative, bail out.
954 // We have already checked in the LocationsBuilder for the constant case.
955 if (!length.IsConstant()) {
956 __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>());
957 __ j(kLess, slow_path->GetEntryLabel());
958 }
959
960 // We need the count in ECX.
961 if (length.IsConstant()) {
962 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
963 } else {
964 __ movl(count, length.AsRegister<Register>());
965 }
966
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100967 // Validity checks: source. Use src_base as a temporary register.
968 CheckPosition(assembler, srcPos, src, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400969
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100970 // Validity checks: dest. Use src_base as a temporary register.
971 CheckPosition(assembler, destPos, dest, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400972
973 // Okay, everything checks out. Finally time to do the copy.
974 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400976 DCHECK_EQ(char_size, 2u);
977
978 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
979
980 if (srcPos.IsConstant()) {
981 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
982 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
983 } else {
984 __ leal(src_base, Address(src, srcPos.AsRegister<Register>(),
985 ScaleFactor::TIMES_2, data_offset));
986 }
987 if (destPos.IsConstant()) {
988 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
989
990 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
991 } else {
992 __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(),
993 ScaleFactor::TIMES_2, data_offset));
994 }
995
996 // Do the move.
997 __ rep_movsw();
998
999 __ Bind(slow_path->GetExitLabel());
1000}
1001
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001002void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
1003 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001004 LocationSummary* locations = new (allocator_) LocationSummary(
1005 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001006 InvokeRuntimeCallingConvention calling_convention;
1007 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1008 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1009 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001010}
1011
1012void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
1013 X86Assembler* assembler = GetAssembler();
1014 LocationSummary* locations = invoke->GetLocations();
1015
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001016 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001017 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001018
1019 Register argument = locations->InAt(1).AsRegister<Register>();
1020 __ testl(argument, argument);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001021 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001022 codegen_->AddSlowPath(slow_path);
1023 __ j(kEqual, slow_path->GetEntryLabel());
1024
Serban Constantinescuba45db02016-07-12 22:53:02 +01001025 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001026 __ Bind(slow_path->GetExitLabel());
1027}
1028
Agi Csakid7138c82015-08-13 17:46:44 -07001029void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001030 LocationSummary* locations =
1031 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -07001032 locations->SetInAt(0, Location::RequiresRegister());
1033 locations->SetInAt(1, Location::RequiresRegister());
1034
1035 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
1036 locations->AddTemp(Location::RegisterLocation(ECX));
1037 locations->AddTemp(Location::RegisterLocation(EDI));
1038
1039 // Set output, ESI needed for repe_cmpsl instruction anyways.
1040 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
1041}
1042
1043void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
1044 X86Assembler* assembler = GetAssembler();
1045 LocationSummary* locations = invoke->GetLocations();
1046
1047 Register str = locations->InAt(0).AsRegister<Register>();
1048 Register arg = locations->InAt(1).AsRegister<Register>();
1049 Register ecx = locations->GetTemp(0).AsRegister<Register>();
1050 Register edi = locations->GetTemp(1).AsRegister<Register>();
1051 Register esi = locations->Out().AsRegister<Register>();
1052
Mark Mendell0c9497d2015-08-21 09:30:05 -04001053 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -07001054
1055 // Get offsets of count, value, and class fields within a string object.
1056 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1057 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1058 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1059
1060 // Note that the null check must have been done earlier.
1061 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1062
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001063 StringEqualsOptimizations optimizations(invoke);
1064 if (!optimizations.GetArgumentNotNull()) {
1065 // Check if input is null, return false if it is.
1066 __ testl(arg, arg);
1067 __ j(kEqual, &return_false);
1068 }
Agi Csakid7138c82015-08-13 17:46:44 -07001069
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001070 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +01001071 // Instanceof check for the argument by comparing class fields.
1072 // All string objects must have the same type since String cannot be subclassed.
1073 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1074 // If the argument is a string object, its class field must be equal to receiver's class field.
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001075 __ movl(ecx, Address(str, class_offset));
1076 __ cmpl(ecx, Address(arg, class_offset));
1077 __ j(kNotEqual, &return_false);
1078 }
Agi Csakid7138c82015-08-13 17:46:44 -07001079
1080 // Reference equality check, return true if same reference.
1081 __ cmpl(str, arg);
1082 __ j(kEqual, &return_true);
1083
jessicahandojo4877b792016-09-08 19:49:13 -07001084 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -07001085 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001086 // Check if lengths and compression flags are equal, return false if they're not.
1087 // Two identical strings will always have same compression style since
1088 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -07001089 __ cmpl(ecx, Address(arg, count_offset));
1090 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001091 // Return true if strings are empty. Even with string compression `count == 0` means empty.
1092 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1093 "Expecting 0=compressed, 1=uncompressed");
1094 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -07001095
jessicahandojo4877b792016-09-08 19:49:13 -07001096 if (mirror::kUseStringCompression) {
1097 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001098 // Extract length and differentiate between both compressed or both uncompressed.
1099 // Different compression style is cut above.
1100 __ shrl(ecx, Immediate(1));
1101 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001102 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001103 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001104 __ shrl(ecx, Immediate(1));
1105 __ Bind(&string_uncompressed);
1106 }
Agi Csakid7138c82015-08-13 17:46:44 -07001107 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1108 __ leal(esi, Address(str, value_offset));
1109 __ leal(edi, Address(arg, value_offset));
1110
jessicahandojo4877b792016-09-08 19:49:13 -07001111 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1112 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001113 __ addl(ecx, Immediate(1));
1114 __ shrl(ecx, Immediate(1));
1115
jessicahandojo4877b792016-09-08 19:49:13 -07001116 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1117 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001118 DCHECK_ALIGNED(value_offset, 4);
1119 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1120
1121 // Loop to compare strings two characters at a time starting at the beginning of the string.
1122 __ repe_cmpsl();
1123 // If strings are not equal, zero flag will be cleared.
1124 __ j(kNotEqual, &return_false);
1125
1126 // Return true and exit the function.
1127 // If loop does not result in returning false, we return true.
1128 __ Bind(&return_true);
1129 __ movl(esi, Immediate(1));
1130 __ jmp(&end);
1131
1132 // Return false and exit the function.
1133 __ Bind(&return_false);
1134 __ xorl(esi, esi);
1135 __ Bind(&end);
1136}
1137
Andreas Gampe21030dd2015-05-07 14:46:15 -07001138static void CreateStringIndexOfLocations(HInvoke* invoke,
1139 ArenaAllocator* allocator,
1140 bool start_at_zero) {
1141 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1142 LocationSummary::kCallOnSlowPath,
1143 kIntrinsified);
1144 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1145 locations->SetInAt(0, Location::RegisterLocation(EDI));
1146 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1147 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1148 // of the instruction explicitly.
1149 // Note: This works as we don't clobber EAX anywhere.
1150 locations->SetInAt(1, Location::RegisterLocation(EAX));
1151 if (!start_at_zero) {
1152 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1153 }
1154 // As we clobber EDI during execution anyways, also use it as the output.
1155 locations->SetOut(Location::SameAsFirstInput());
1156
1157 // repne scasw uses ECX as the counter.
1158 locations->AddTemp(Location::RegisterLocation(ECX));
1159 // Need another temporary to be able to compute the result.
1160 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001161 if (mirror::kUseStringCompression) {
1162 // Need another temporary to be able to save unflagged string length.
1163 locations->AddTemp(Location::RequiresRegister());
1164 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001165}
1166
1167static void GenerateStringIndexOf(HInvoke* invoke,
1168 X86Assembler* assembler,
1169 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001170 bool start_at_zero) {
1171 LocationSummary* locations = invoke->GetLocations();
1172
1173 // Note that the null check must have been done earlier.
1174 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1175
1176 Register string_obj = locations->InAt(0).AsRegister<Register>();
1177 Register search_value = locations->InAt(1).AsRegister<Register>();
1178 Register counter = locations->GetTemp(0).AsRegister<Register>();
1179 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1180 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001181 // Only used when string compression feature is on.
1182 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001183
1184 // Check our assumptions for registers.
1185 DCHECK_EQ(string_obj, EDI);
1186 DCHECK_EQ(search_value, EAX);
1187 DCHECK_EQ(counter, ECX);
1188 DCHECK_EQ(out, EDI);
1189
1190 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001191 // 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 -07001192 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001193 HInstruction* code_point = invoke->InputAt(1);
1194 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001195 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001196 std::numeric_limits<uint16_t>::max()) {
1197 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1198 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001199 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001200 codegen->AddSlowPath(slow_path);
1201 __ jmp(slow_path->GetEntryLabel());
1202 __ Bind(slow_path->GetExitLabel());
1203 return;
1204 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001206 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001207 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001208 codegen->AddSlowPath(slow_path);
1209 __ j(kAbove, slow_path->GetEntryLabel());
1210 }
1211
1212 // From here down, we know that we are looking for a char that fits in 16 bits.
1213 // Location of reference to data array within the String object.
1214 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1215 // Location of count within the String object.
1216 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1217
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001218 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001219 __ movl(string_length, Address(string_obj, count_offset));
1220
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001221 // Do a zero-length check. Even with string compression `count == 0` means empty.
1222 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1223 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001224 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001225 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001226 __ testl(string_length, string_length);
1227 __ j(kEqual, &not_found_label);
1228
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001229 if (mirror::kUseStringCompression) {
1230 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1231 __ movl(string_length_flagged, string_length);
1232 // Extract the length and shift out the least significant bit used as compression flag.
1233 __ shrl(string_length, Immediate(1));
1234 }
1235
Andreas Gampe21030dd2015-05-07 14:46:15 -07001236 if (start_at_zero) {
1237 // Number of chars to scan is the same as the string length.
1238 __ movl(counter, string_length);
1239
1240 // Move to the start of the string.
1241 __ addl(string_obj, Immediate(value_offset));
1242 } else {
1243 Register start_index = locations->InAt(2).AsRegister<Register>();
1244
1245 // Do a start_index check.
1246 __ cmpl(start_index, string_length);
1247 __ j(kGreaterEqual, &not_found_label);
1248
1249 // Ensure we have a start index >= 0;
1250 __ xorl(counter, counter);
1251 __ cmpl(start_index, Immediate(0));
1252 __ cmovl(kGreater, counter, start_index);
1253
jessicahandojo4877b792016-09-08 19:49:13 -07001254 if (mirror::kUseStringCompression) {
1255 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001256 __ testl(string_length_flagged, Immediate(1));
1257 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001258 // Move to the start of the string: string_obj + value_offset + start_index.
1259 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1260 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001261
jessicahandojo4877b792016-09-08 19:49:13 -07001262 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1263 __ Bind(&offset_uncompressed_label);
1264 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1265
1266 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1267 // compare.
1268 __ Bind(&modify_counter);
1269 } else {
1270 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1271 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001272 __ negl(counter);
1273 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1274 }
1275
jessicahandojo4877b792016-09-08 19:49:13 -07001276 if (mirror::kUseStringCompression) {
1277 NearLabel uncompressed_string_comparison;
1278 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001279 __ testl(string_length_flagged, Immediate(1));
1280 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001281
jessicahandojo4877b792016-09-08 19:49:13 -07001282 // Check if EAX (search_value) is ASCII.
1283 __ cmpl(search_value, Immediate(127));
1284 __ j(kGreater, &not_found_label);
1285 // Comparing byte-per-byte.
1286 __ repne_scasb();
1287 __ jmp(&comparison_done);
1288
1289 // Everything is set up for repne scasw:
1290 // * Comparison address in EDI.
1291 // * Counter in ECX.
1292 __ Bind(&uncompressed_string_comparison);
1293 __ repne_scasw();
1294 __ Bind(&comparison_done);
1295 } else {
1296 __ repne_scasw();
1297 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001298 // Did we find a match?
1299 __ j(kNotEqual, &not_found_label);
1300
1301 // Yes, we matched. Compute the index of the result.
1302 __ subl(string_length, counter);
1303 __ leal(out, Address(string_length, -1));
1304
Mark Mendell0c9497d2015-08-21 09:30:05 -04001305 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001306 __ jmp(&done);
1307
1308 // Failed to match; return -1.
1309 __ Bind(&not_found_label);
1310 __ movl(out, Immediate(-1));
1311
1312 // And join up at the end.
1313 __ Bind(&done);
1314 if (slow_path != nullptr) {
1315 __ Bind(slow_path->GetExitLabel());
1316 }
1317}
1318
1319void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001320 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001321}
1322
1323void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001324 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001325}
1326
1327void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001328 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001329}
1330
1331void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001332 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001333}
1334
Jeff Hao848f70a2014-01-15 13:49:50 -08001335void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001336 LocationSummary* locations = new (allocator_) LocationSummary(
1337 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001338 InvokeRuntimeCallingConvention calling_convention;
1339 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1340 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1341 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1342 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1343 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001344}
1345
1346void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1347 X86Assembler* assembler = GetAssembler();
1348 LocationSummary* locations = invoke->GetLocations();
1349
1350 Register byte_array = locations->InAt(0).AsRegister<Register>();
1351 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001352 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001353 codegen_->AddSlowPath(slow_path);
1354 __ j(kEqual, slow_path->GetEntryLabel());
1355
Serban Constantinescuba45db02016-07-12 22:53:02 +01001356 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001357 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001358 __ Bind(slow_path->GetExitLabel());
1359}
1360
1361void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001362 LocationSummary* locations =
1363 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001364 InvokeRuntimeCallingConvention calling_convention;
1365 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1366 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1367 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1368 locations->SetOut(Location::RegisterLocation(EAX));
1369}
1370
1371void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001372 // No need to emit code checking whether `locations->InAt(2)` is a null
1373 // pointer, as callers of the native method
1374 //
1375 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1376 //
1377 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001378 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001379 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001380}
1381
1382void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001383 LocationSummary* locations = new (allocator_) LocationSummary(
1384 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001385 InvokeRuntimeCallingConvention calling_convention;
1386 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1387 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001388}
1389
1390void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1391 X86Assembler* assembler = GetAssembler();
1392 LocationSummary* locations = invoke->GetLocations();
1393
1394 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1395 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001396 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001397 codegen_->AddSlowPath(slow_path);
1398 __ j(kEqual, slow_path->GetEntryLabel());
1399
Serban Constantinescuba45db02016-07-12 22:53:02 +01001400 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001401 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001402 __ Bind(slow_path->GetExitLabel());
1403}
1404
Mark Mendell8f8926a2015-08-17 11:39:06 -04001405void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1406 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001407 LocationSummary* locations =
1408 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001409 locations->SetInAt(0, Location::RequiresRegister());
1410 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1411 // Place srcEnd in ECX to save a move below.
1412 locations->SetInAt(2, Location::RegisterLocation(ECX));
1413 locations->SetInAt(3, Location::RequiresRegister());
1414 locations->SetInAt(4, Location::RequiresRegister());
1415
1416 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1417 // We don't have enough registers to also grab ECX, so handle below.
1418 locations->AddTemp(Location::RegisterLocation(ESI));
1419 locations->AddTemp(Location::RegisterLocation(EDI));
1420}
1421
1422void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1423 X86Assembler* assembler = GetAssembler();
1424 LocationSummary* locations = invoke->GetLocations();
1425
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001426 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001427 // Location of data in char array buffer.
1428 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1429 // Location of char array data in string.
1430 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1431
1432 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1433 Register obj = locations->InAt(0).AsRegister<Register>();
1434 Location srcBegin = locations->InAt(1);
1435 int srcBegin_value =
1436 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1437 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1438 Register dst = locations->InAt(3).AsRegister<Register>();
1439 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1440
1441 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001442 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001443 DCHECK_EQ(char_size, 2u);
1444
Mark Mendell8f8926a2015-08-17 11:39:06 -04001445 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001446 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001447 __ pushl(ECX);
1448 int stack_adjust = kX86WordSize;
1449 __ cfi().AdjustCFAOffset(stack_adjust);
1450 DCHECK_EQ(srcEnd, ECX);
1451 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001452 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001453 } else {
1454 DCHECK(srcBegin.IsRegister());
1455 __ subl(ECX, srcBegin.AsRegister<Register>());
1456 }
1457
jessicahandojo4877b792016-09-08 19:49:13 -07001458 NearLabel done;
1459 if (mirror::kUseStringCompression) {
1460 // Location of count in string
1461 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001462 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001463 DCHECK_EQ(c_char_size, 1u);
1464 __ pushl(EAX);
1465 __ cfi().AdjustCFAOffset(stack_adjust);
1466
1467 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001468 __ testl(Address(obj, count_offset), Immediate(1));
1469 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1470 "Expecting 0=compressed, 1=uncompressed");
1471 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001472 // Compute the address of the source string by adding the number of chars from
1473 // the source beginning to the value offset of a string.
1474 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1475
1476 // Start the loop to copy String's value to Array of Char.
1477 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1478 __ Bind(&copy_loop);
1479 __ jecxz(&done);
1480 // Use EAX temporary (convert byte from ESI to word).
1481 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1482 __ movzxb(EAX, Address(ESI, 0));
1483 __ movw(Address(EDI, 0), EAX);
1484 __ leal(EDI, Address(EDI, char_size));
1485 __ leal(ESI, Address(ESI, c_char_size));
1486 // TODO: Add support for LOOP to X86Assembler.
1487 __ subl(ECX, Immediate(1));
1488 __ jmp(&copy_loop);
1489 __ Bind(&copy_uncompressed);
1490 }
1491
1492 // Do the copy for uncompressed string.
1493 // Compute the address of the destination buffer.
1494 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1495 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001496 __ rep_movsw();
1497
jessicahandojo4877b792016-09-08 19:49:13 -07001498 __ Bind(&done);
1499 if (mirror::kUseStringCompression) {
1500 // Restore EAX.
1501 __ popl(EAX);
1502 __ cfi().AdjustCFAOffset(-stack_adjust);
1503 }
1504 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001505 __ popl(ECX);
1506 __ cfi().AdjustCFAOffset(-stack_adjust);
1507}
1508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001509static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001510 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1511 Location out_loc = locations->Out();
1512 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1513 // to avoid a SIGBUS.
1514 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001515 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001516 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1517 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001518 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001519 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1520 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001521 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001522 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1523 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001524 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001525 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1526 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1527 break;
1528 default:
1529 LOG(FATAL) << "Type not recognized for peek: " << size;
1530 UNREACHABLE();
1531 }
1532}
1533
1534void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001535 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001536}
1537
1538void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001540}
1541
1542void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001543 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001544}
1545
1546void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001547 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001548}
1549
1550void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001551 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001552}
1553
1554void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001555 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001556}
1557
1558void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001559 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001560}
1561
1562void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001563 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001564}
1565
Vladimir Markoca6fff82017-10-03 14:49:14 +01001566static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1567 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001568 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001569 LocationSummary* locations =
1570 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001571 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001572 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001574 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1575 } else {
1576 locations->SetInAt(1, Location::RegisterOrConstant(value));
1577 }
1578}
1579
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001580static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001581 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1582 Location value_loc = locations->InAt(1);
1583 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1584 // to avoid a SIGBUS.
1585 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001586 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001587 if (value_loc.IsConstant()) {
1588 __ movb(Address(address, 0),
1589 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1590 } else {
1591 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1592 }
1593 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001594 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001595 if (value_loc.IsConstant()) {
1596 __ movw(Address(address, 0),
1597 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1598 } else {
1599 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1600 }
1601 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001602 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001603 if (value_loc.IsConstant()) {
1604 __ movl(Address(address, 0),
1605 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1606 } else {
1607 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1608 }
1609 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001611 if (value_loc.IsConstant()) {
1612 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1613 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1614 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1615 } else {
1616 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1617 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1618 }
1619 break;
1620 default:
1621 LOG(FATAL) << "Type not recognized for poke: " << size;
1622 UNREACHABLE();
1623 }
1624}
1625
1626void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001627 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001628}
1629
1630void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001632}
1633
1634void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001635 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001636}
1637
1638void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001639 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001640}
1641
1642void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001643 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001644}
1645
1646void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001647 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001648}
1649
1650void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001651 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001652}
1653
1654void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001655 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001656}
1657
1658void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001659 LocationSummary* locations =
1660 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001661 locations->SetOut(Location::RequiresRegister());
1662}
1663
1664void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1665 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001666 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001667}
1668
Roland Levillain0d5a2812015-11-13 10:07:31 +00001669static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001670 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001671 bool is_volatile,
1672 CodeGeneratorX86* codegen) {
1673 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1674 LocationSummary* locations = invoke->GetLocations();
1675 Location base_loc = locations->InAt(1);
1676 Register base = base_loc.AsRegister<Register>();
1677 Location offset_loc = locations->InAt(2);
1678 Register offset = offset_loc.AsRegisterPairLow<Register>();
1679 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001680
1681 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001683 Register output = output_loc.AsRegister<Register>();
1684 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00001685 break;
1686 }
1687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001688 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001689 Register output = output_loc.AsRegister<Register>();
1690 if (kEmitCompilerReadBarrier) {
1691 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001692 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1693 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001694 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00001695 } else {
1696 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1697 codegen->GenerateReadBarrierSlow(
1698 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1699 }
1700 } else {
1701 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1702 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001703 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001704 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001705 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001707 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001708 Register output_lo = output_loc.AsRegisterPairLow<Register>();
1709 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001710 if (is_volatile) {
1711 // Need to use a XMM to read atomically.
1712 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1713 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1714 __ movd(output_lo, temp);
1715 __ psrlq(temp, Immediate(32));
1716 __ movd(output_hi, temp);
1717 } else {
1718 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1719 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1720 }
1721 }
1722 break;
1723
1724 default:
1725 LOG(FATAL) << "Unsupported op size " << type;
1726 UNREACHABLE();
1727 }
1728}
1729
Vladimir Markoca6fff82017-10-03 14:49:14 +01001730static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001731 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001732 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001733 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001734 bool can_call = kEmitCompilerReadBarrier &&
1735 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1736 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001737 LocationSummary* locations =
1738 new (allocator) LocationSummary(invoke,
1739 can_call
1740 ? LocationSummary::kCallOnSlowPath
1741 : LocationSummary::kNoCall,
1742 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01001743 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001744 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001745 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001746 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1747 locations->SetInAt(1, Location::RequiresRegister());
1748 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001749 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001750 if (is_volatile) {
1751 // Need to use XMM to read volatile.
1752 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001754 } else {
1755 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1756 }
1757 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001758 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001759 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001760 }
1761}
1762
1763void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001764 CreateIntIntIntToIntLocations(
1765 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001766}
1767void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001768 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001769}
1770void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001771 CreateIntIntIntToIntLocations(
1772 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001773}
1774void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001775 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001776}
1777void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001779 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001780}
1781void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001782 CreateIntIntIntToIntLocations(
1783 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001784}
1785
1786
1787void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001788 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001789}
1790void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001791 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001792}
1793void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001794 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001795}
1796void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001797 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001798}
1799void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001800 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001801}
1802void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001803 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001804}
1805
1806
Vladimir Markoca6fff82017-10-03 14:49:14 +01001807static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001808 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001809 HInvoke* invoke,
1810 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001811 LocationSummary* locations =
1812 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001813 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1814 locations->SetInAt(1, Location::RequiresRegister());
1815 locations->SetInAt(2, Location::RequiresRegister());
1816 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001818 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001819 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001820 // Ensure the value is in a byte register.
1821 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001823 locations->AddTemp(Location::RequiresFpuRegister());
1824 locations->AddTemp(Location::RequiresFpuRegister());
1825 }
1826}
1827
1828void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001829 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001830 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001831}
1832void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001833 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001834 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001835}
1836void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001837 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001838 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001839}
1840void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001841 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001842 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001843}
1844void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001845 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001846 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001847}
1848void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001849 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001850 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001851}
1852void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001853 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001854 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001855}
1856void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001857 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001858 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001859}
1860void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001861 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001862 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001863}
1864
1865// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1866// memory model.
1867static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001868 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001869 bool is_volatile,
1870 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001871 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001872 Register base = locations->InAt(1).AsRegister<Register>();
1873 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1874 Location value_loc = locations->InAt(3);
1875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001877 Register value_lo = value_loc.AsRegisterPairLow<Register>();
1878 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
1879 if (is_volatile) {
1880 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1881 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1882 __ movd(temp1, value_lo);
1883 __ movd(temp2, value_hi);
1884 __ punpckldq(temp1, temp2);
1885 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
1886 } else {
1887 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
1888 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
1889 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001890 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001891 Register temp = locations->GetTemp(0).AsRegister<Register>();
1892 __ movl(temp, value_loc.AsRegister<Register>());
1893 __ PoisonHeapReference(temp);
1894 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001895 } else {
1896 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
1897 }
1898
1899 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00001900 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001901 }
1902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001903 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001904 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04001905 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1906 locations->GetTemp(1).AsRegister<Register>(),
1907 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001908 value_loc.AsRegister<Register>(),
1909 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001910 }
1911}
1912
1913void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001915}
1916void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001917 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001918}
1919void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001920 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001921}
1922void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001923 GenUnsafePut(
1924 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001925}
1926void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 GenUnsafePut(
1928 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001929}
1930void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001931 GenUnsafePut(
1932 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001933}
1934void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001936}
1937void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001938 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001939}
1940void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001942}
1943
Vladimir Markoca6fff82017-10-03 14:49:14 +01001944static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001945 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04001946 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001947 bool can_call = kEmitCompilerReadBarrier &&
1948 kUseBakerReadBarrier &&
1949 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001950 LocationSummary* locations =
1951 new (allocator) LocationSummary(invoke,
1952 can_call
1953 ? LocationSummary::kCallOnSlowPath
1954 : LocationSummary::kNoCall,
1955 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001956 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1957 locations->SetInAt(1, Location::RequiresRegister());
1958 // Offset is a long, but in 32 bit mode, we only need the low word.
1959 // Can we update the invoke here to remove a TypeConvert to Long?
1960 locations->SetInAt(2, Location::RequiresRegister());
1961 // Expected value must be in EAX or EDX:EAX.
1962 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001963 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04001964 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
1965 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
1966 } else {
1967 locations->SetInAt(3, Location::RegisterLocation(EAX));
1968 locations->SetInAt(4, Location::RequiresRegister());
1969 }
1970
1971 // Force a byte register for the output.
1972 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001974 // Need temporary registers for card-marking, and possibly for
1975 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01001976 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001977 // Need a byte register for marking.
1978 locations->AddTemp(Location::RegisterLocation(ECX));
1979 }
1980}
1981
1982void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001983 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001984}
1985
1986void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001987 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001988}
1989
1990void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001991 // The only read barrier implementation supporting the
1992 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1993 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00001994 return;
1995 }
1996
Vladimir Markoca6fff82017-10-03 14:49:14 +01001997 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001998}
1999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002001 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002002 LocationSummary* locations = invoke->GetLocations();
2003
2004 Register base = locations->InAt(1).AsRegister<Register>();
2005 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2006 Location out = locations->Out();
2007 DCHECK_EQ(out.AsRegister<Register>(), EAX);
2008
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002009 // The address of the field within the holding object.
2010 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002012 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002013 // The only read barrier implementation supporting the
2014 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2015 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2016
2017 Location temp1_loc = locations->GetTemp(0);
2018 Register temp1 = temp1_loc.AsRegister<Register>();
2019 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2020
Roland Levillain4d027112015-07-01 15:41:14 +01002021 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01002022 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01002023 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002024 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01002025
Roland Levillainb488b782015-10-22 11:38:49 +01002026 // Mark card for object assuming new value is stored.
2027 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002028 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2029
2030 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2031 // Need to make sure the reference stored in the field is a to-space
2032 // one before attempting the CAS or the CAS could fail incorrectly.
2033 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2034 invoke,
2035 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
2036 base,
2037 field_addr,
2038 /* needs_null_check */ false,
2039 /* always_update_field */ true,
2040 &temp2);
2041 }
Roland Levillainb488b782015-10-22 11:38:49 +01002042
2043 bool base_equals_value = (base == value);
2044 if (kPoisonHeapReferences) {
2045 if (base_equals_value) {
2046 // If `base` and `value` are the same register location, move
2047 // `value` to a temporary register. This way, poisoning
2048 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002049 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01002050 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01002051 }
Roland Levillainb488b782015-10-22 11:38:49 +01002052
2053 // Check that the register allocator did not assign the location
2054 // of `expected` (EAX) to `value` nor to `base`, so that heap
2055 // poisoning (when enabled) works as intended below.
2056 // - If `value` were equal to `expected`, both references would
2057 // be poisoned twice, meaning they would not be poisoned at
2058 // all, as heap poisoning uses address negation.
2059 // - If `base` were equal to `expected`, poisoning `expected`
2060 // would invalidate `base`.
2061 DCHECK_NE(value, expected);
2062 DCHECK_NE(base, expected);
2063
2064 __ PoisonHeapReference(expected);
2065 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002066 }
2067
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002068 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002069
Roland Levillain0d5a2812015-11-13 10:07:31 +00002070 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002071 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002072
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002073 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002074 __ setb(kZero, out.AsRegister<Register>());
2075 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002076
Roland Levillain391b8662015-12-18 11:43:38 +00002077 // If heap poisoning is enabled, we need to unpoison the values
2078 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002079 if (kPoisonHeapReferences) {
2080 if (base_equals_value) {
2081 // `value` has been moved to a temporary register, no need to
2082 // unpoison it.
2083 } else {
2084 // Ensure `value` is different from `out`, so that unpoisoning
2085 // the former does not invalidate the latter.
2086 DCHECK_NE(value, out.AsRegister<Register>());
2087 __ UnpoisonHeapReference(value);
2088 }
2089 // Do not unpoison the reference contained in register
2090 // `expected`, as it is the same as register `out` (EAX).
2091 }
2092 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002093 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002094 // Ensure the expected value is in EAX (required by the CMPXCHG
2095 // instruction).
2096 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002097 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002099 // Ensure the expected value is in EAX:EDX and that the new
2100 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2101 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2102 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2103 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2104 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002105 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002106 } else {
2107 LOG(FATAL) << "Unexpected CAS type " << type;
2108 }
2109
Roland Levillain0d5a2812015-11-13 10:07:31 +00002110 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2111 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002112
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002113 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002114 __ setb(kZero, out.AsRegister<Register>());
2115 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002116 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002117}
2118
2119void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002121}
2122
2123void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002125}
2126
2127void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002128 // The only read barrier implementation supporting the
2129 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2130 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002131
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002133}
2134
2135void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002136 LocationSummary* locations =
2137 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002138 locations->SetInAt(0, Location::RequiresRegister());
2139 locations->SetOut(Location::SameAsFirstInput());
2140 locations->AddTemp(Location::RequiresRegister());
2141}
2142
2143static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2144 X86Assembler* assembler) {
2145 Immediate imm_shift(shift);
2146 Immediate imm_mask(mask);
2147 __ movl(temp, reg);
2148 __ shrl(reg, imm_shift);
2149 __ andl(temp, imm_mask);
2150 __ andl(reg, imm_mask);
2151 __ shll(temp, imm_shift);
2152 __ orl(reg, temp);
2153}
2154
2155void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002156 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002157 LocationSummary* locations = invoke->GetLocations();
2158
2159 Register reg = locations->InAt(0).AsRegister<Register>();
2160 Register temp = locations->GetTemp(0).AsRegister<Register>();
2161
2162 /*
2163 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2164 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2165 * compared to generic luni implementation which has 5 rounds of swapping bits.
2166 * x = bswap x
2167 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2168 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2169 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2170 */
2171 __ bswapl(reg);
2172 SwapBits(reg, temp, 1, 0x55555555, assembler);
2173 SwapBits(reg, temp, 2, 0x33333333, assembler);
2174 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2175}
2176
2177void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002178 LocationSummary* locations =
2179 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002180 locations->SetInAt(0, Location::RequiresRegister());
2181 locations->SetOut(Location::SameAsFirstInput());
2182 locations->AddTemp(Location::RequiresRegister());
2183}
2184
2185void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002186 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002187 LocationSummary* locations = invoke->GetLocations();
2188
2189 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2190 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2191 Register temp = locations->GetTemp(0).AsRegister<Register>();
2192
2193 // We want to swap high/low, then bswap each one, and then do the same
2194 // as a 32 bit reverse.
2195 // Exchange high and low.
2196 __ movl(temp, reg_low);
2197 __ movl(reg_low, reg_high);
2198 __ movl(reg_high, temp);
2199
2200 // bit-reverse low
2201 __ bswapl(reg_low);
2202 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2203 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2204 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2205
2206 // bit-reverse high
2207 __ bswapl(reg_high);
2208 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2209 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2210 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2211}
2212
Aart Bikc39dac12016-01-21 08:59:48 -08002213static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002214 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002215 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2216 // Do nothing if there is no popcnt support. This results in generating
2217 // a call for the intrinsic rather than direct code.
2218 return;
2219 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002220 LocationSummary* locations =
2221 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002222 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002223 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002224 }
Aart Bik2a946072016-01-21 12:49:00 -08002225 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002226 locations->SetOut(Location::RequiresRegister());
2227}
2228
Aart Bika19616e2016-02-01 18:57:58 -08002229static void GenBitCount(X86Assembler* assembler,
2230 CodeGeneratorX86* codegen,
2231 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002232 LocationSummary* locations = invoke->GetLocations();
2233 Location src = locations->InAt(0);
2234 Register out = locations->Out().AsRegister<Register>();
2235
2236 if (invoke->InputAt(0)->IsConstant()) {
2237 // Evaluate this at compile time.
2238 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002239 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002240 ? POPCOUNT(static_cast<uint64_t>(value))
2241 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002242 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002243 return;
2244 }
2245
2246 // Handle the non-constant cases.
2247 if (!is_long) {
2248 if (src.IsRegister()) {
2249 __ popcntl(out, src.AsRegister<Register>());
2250 } else {
2251 DCHECK(src.IsStackSlot());
2252 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2253 }
Aart Bik2a946072016-01-21 12:49:00 -08002254 } else {
2255 // The 64-bit case needs to worry about two parts.
2256 Register temp = locations->GetTemp(0).AsRegister<Register>();
2257 if (src.IsRegisterPair()) {
2258 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2259 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2260 } else {
2261 DCHECK(src.IsDoubleStackSlot());
2262 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2263 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2264 }
2265 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002266 }
Aart Bikc39dac12016-01-21 08:59:48 -08002267}
2268
2269void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002270 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002271}
2272
2273void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002274 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002275}
2276
2277void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002278 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002279}
2280
2281void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002282 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002283}
2284
Vladimir Markoca6fff82017-10-03 14:49:14 +01002285static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2286 LocationSummary* locations =
2287 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002288 if (is_long) {
2289 locations->SetInAt(0, Location::RequiresRegister());
2290 } else {
2291 locations->SetInAt(0, Location::Any());
2292 }
2293 locations->SetOut(Location::RequiresRegister());
2294}
2295
Aart Bika19616e2016-02-01 18:57:58 -08002296static void GenLeadingZeros(X86Assembler* assembler,
2297 CodeGeneratorX86* codegen,
2298 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002299 LocationSummary* locations = invoke->GetLocations();
2300 Location src = locations->InAt(0);
2301 Register out = locations->Out().AsRegister<Register>();
2302
2303 if (invoke->InputAt(0)->IsConstant()) {
2304 // Evaluate this at compile time.
2305 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2306 if (value == 0) {
2307 value = is_long ? 64 : 32;
2308 } else {
2309 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2310 }
Aart Bika19616e2016-02-01 18:57:58 -08002311 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002312 return;
2313 }
2314
2315 // Handle the non-constant cases.
2316 if (!is_long) {
2317 if (src.IsRegister()) {
2318 __ bsrl(out, src.AsRegister<Register>());
2319 } else {
2320 DCHECK(src.IsStackSlot());
2321 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2322 }
2323
2324 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002325 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002326 __ j(kEqual, &all_zeroes);
2327
2328 // Correct the result from BSR to get the final CLZ result.
2329 __ xorl(out, Immediate(31));
2330 __ jmp(&done);
2331
2332 // Fix the zero case with the expected result.
2333 __ Bind(&all_zeroes);
2334 __ movl(out, Immediate(32));
2335
2336 __ Bind(&done);
2337 return;
2338 }
2339
2340 // 64 bit case needs to worry about both parts of the register.
2341 DCHECK(src.IsRegisterPair());
2342 Register src_lo = src.AsRegisterPairLow<Register>();
2343 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002344 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002345
2346 // Is the high word zero?
2347 __ testl(src_hi, src_hi);
2348 __ j(kEqual, &handle_low);
2349
2350 // High word is not zero. We know that the BSR result is defined in this case.
2351 __ bsrl(out, src_hi);
2352
2353 // Correct the result from BSR to get the final CLZ result.
2354 __ xorl(out, Immediate(31));
2355 __ jmp(&done);
2356
2357 // High word was zero. We have to compute the low word count and add 32.
2358 __ Bind(&handle_low);
2359 __ bsrl(out, src_lo);
2360 __ j(kEqual, &all_zeroes);
2361
2362 // We had a valid result. Use an XOR to both correct the result and add 32.
2363 __ xorl(out, Immediate(63));
2364 __ jmp(&done);
2365
2366 // All zero case.
2367 __ Bind(&all_zeroes);
2368 __ movl(out, Immediate(64));
2369
2370 __ Bind(&done);
2371}
2372
2373void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002374 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002375}
2376
2377void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002378 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002379}
2380
2381void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002382 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002383}
2384
2385void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002386 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002387}
2388
Vladimir Markoca6fff82017-10-03 14:49:14 +01002389static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2390 LocationSummary* locations =
2391 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002392 if (is_long) {
2393 locations->SetInAt(0, Location::RequiresRegister());
2394 } else {
2395 locations->SetInAt(0, Location::Any());
2396 }
2397 locations->SetOut(Location::RequiresRegister());
2398}
2399
Aart Bika19616e2016-02-01 18:57:58 -08002400static void GenTrailingZeros(X86Assembler* assembler,
2401 CodeGeneratorX86* codegen,
2402 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002403 LocationSummary* locations = invoke->GetLocations();
2404 Location src = locations->InAt(0);
2405 Register out = locations->Out().AsRegister<Register>();
2406
2407 if (invoke->InputAt(0)->IsConstant()) {
2408 // Evaluate this at compile time.
2409 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2410 if (value == 0) {
2411 value = is_long ? 64 : 32;
2412 } else {
2413 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2414 }
Aart Bika19616e2016-02-01 18:57:58 -08002415 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002416 return;
2417 }
2418
2419 // Handle the non-constant cases.
2420 if (!is_long) {
2421 if (src.IsRegister()) {
2422 __ bsfl(out, src.AsRegister<Register>());
2423 } else {
2424 DCHECK(src.IsStackSlot());
2425 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2426 }
2427
2428 // BSF sets ZF if the input was zero, and the output is undefined.
2429 NearLabel done;
2430 __ j(kNotEqual, &done);
2431
2432 // Fix the zero case with the expected result.
2433 __ movl(out, Immediate(32));
2434
2435 __ Bind(&done);
2436 return;
2437 }
2438
2439 // 64 bit case needs to worry about both parts of the register.
2440 DCHECK(src.IsRegisterPair());
2441 Register src_lo = src.AsRegisterPairLow<Register>();
2442 Register src_hi = src.AsRegisterPairHigh<Register>();
2443 NearLabel done, all_zeroes;
2444
2445 // If the low word is zero, then ZF will be set. If not, we have the answer.
2446 __ bsfl(out, src_lo);
2447 __ j(kNotEqual, &done);
2448
2449 // Low word was zero. We have to compute the high word count and add 32.
2450 __ bsfl(out, src_hi);
2451 __ j(kEqual, &all_zeroes);
2452
2453 // We had a valid result. Add 32 to account for the low word being zero.
2454 __ addl(out, Immediate(32));
2455 __ jmp(&done);
2456
2457 // All zero case.
2458 __ Bind(&all_zeroes);
2459 __ movl(out, Immediate(64));
2460
2461 __ Bind(&done);
2462}
2463
2464void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002465 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002466}
2467
2468void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002469 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002470}
2471
2472void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002473 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002474}
2475
2476void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002477 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002478}
2479
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002480static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2481 return instruction->InputAt(input0) == instruction->InputAt(input1);
2482}
2483
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002484// Compute base address for the System.arraycopy intrinsic in `base`.
2485static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002486 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002487 const Register& array,
2488 const Location& pos,
2489 const Register& base) {
2490 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002491 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002492 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002493 DCHECK_EQ(type, DataType::Type::kReference);
2494 const int32_t element_size = DataType::Size(type);
2495 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002496 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2497
2498 if (pos.IsConstant()) {
2499 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2500 __ leal(base, Address(array, element_size * constant + data_offset));
2501 } else {
2502 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2503 }
2504}
2505
2506// Compute end source address for the System.arraycopy intrinsic in `end`.
2507static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002508 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002509 const Location& copy_length,
2510 const Register& base,
2511 const Register& end) {
2512 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002513 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002514 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002515 DCHECK_EQ(type, DataType::Type::kReference);
2516 const int32_t element_size = DataType::Size(type);
2517 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002518
2519 if (copy_length.IsConstant()) {
2520 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2521 __ leal(end, Address(base, element_size * constant));
2522 } else {
2523 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2524 }
2525}
2526
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002527void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002528 // The only read barrier implementation supporting the
2529 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2530 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002531 return;
2532 }
2533
2534 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2535 if (invoke->GetLocations() != nullptr) {
2536 // Need a byte register for marking.
2537 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2538
2539 static constexpr size_t kSrc = 0;
2540 static constexpr size_t kSrcPos = 1;
2541 static constexpr size_t kDest = 2;
2542 static constexpr size_t kDestPos = 3;
2543 static constexpr size_t kLength = 4;
2544
2545 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2546 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2547 !invoke->InputAt(kLength)->IsIntConstant()) {
2548 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2549 !IsSameInput(invoke, kSrcPos, kLength) &&
2550 !IsSameInput(invoke, kDestPos, kLength) &&
2551 !IsSameInput(invoke, kSrc, kDest)) {
2552 // Not enough registers, make the length also take a stack slot.
2553 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2554 }
2555 }
2556 }
2557}
2558
2559void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002560 // The only read barrier implementation supporting the
2561 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2562 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002563
2564 X86Assembler* assembler = GetAssembler();
2565 LocationSummary* locations = invoke->GetLocations();
2566
2567 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2568 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2569 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2570 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002571 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002572
2573 Register src = locations->InAt(0).AsRegister<Register>();
2574 Location src_pos = locations->InAt(1);
2575 Register dest = locations->InAt(2).AsRegister<Register>();
2576 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002577 Location length_arg = locations->InAt(4);
2578 Location length = length_arg;
2579 Location temp1_loc = locations->GetTemp(0);
2580 Register temp1 = temp1_loc.AsRegister<Register>();
2581 Location temp2_loc = locations->GetTemp(1);
2582 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002583
Vladimir Marko174b2e22017-10-12 13:34:49 +01002584 SlowPathCode* intrinsic_slow_path =
2585 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002586 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002587
2588 NearLabel conditions_on_positions_validated;
2589 SystemArrayCopyOptimizations optimizations(invoke);
2590
2591 // If source and destination are the same, we go to slow path if we need to do
2592 // forward copying.
2593 if (src_pos.IsConstant()) {
2594 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2595 if (dest_pos.IsConstant()) {
2596 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2597 if (optimizations.GetDestinationIsSource()) {
2598 // Checked when building locations.
2599 DCHECK_GE(src_pos_constant, dest_pos_constant);
2600 } else if (src_pos_constant < dest_pos_constant) {
2601 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002602 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002603 }
2604 } else {
2605 if (!optimizations.GetDestinationIsSource()) {
2606 __ cmpl(src, dest);
2607 __ j(kNotEqual, &conditions_on_positions_validated);
2608 }
2609 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002610 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002611 }
2612 } else {
2613 if (!optimizations.GetDestinationIsSource()) {
2614 __ cmpl(src, dest);
2615 __ j(kNotEqual, &conditions_on_positions_validated);
2616 }
2617 if (dest_pos.IsConstant()) {
2618 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2619 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002620 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002621 } else {
2622 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002623 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002624 }
2625 }
2626
2627 __ Bind(&conditions_on_positions_validated);
2628
2629 if (!optimizations.GetSourceIsNotNull()) {
2630 // Bail out if the source is null.
2631 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002632 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002633 }
2634
2635 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2636 // Bail out if the destination is null.
2637 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002638 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002639 }
2640
Roland Levillain0b671c02016-08-19 12:02:34 +01002641 Location temp3_loc = locations->GetTemp(2);
2642 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002643 if (length.IsStackSlot()) {
2644 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2645 length = Location::RegisterLocation(temp3);
2646 }
2647
2648 // If the length is negative, bail out.
2649 // We have already checked in the LocationsBuilder for the constant case.
2650 if (!length.IsConstant() &&
2651 !optimizations.GetCountIsSourceLength() &&
2652 !optimizations.GetCountIsDestinationLength()) {
2653 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002654 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002655 }
2656
2657 // Validity checks: source.
2658 CheckPosition(assembler,
2659 src_pos,
2660 src,
2661 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002662 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002663 temp1,
2664 optimizations.GetCountIsSourceLength());
2665
2666 // Validity checks: dest.
2667 CheckPosition(assembler,
2668 dest_pos,
2669 dest,
2670 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002671 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002672 temp1,
2673 optimizations.GetCountIsDestinationLength());
2674
2675 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2676 // Check whether all elements of the source array are assignable to the component
2677 // type of the destination array. We do two checks: the classes are the same,
2678 // or the destination is Object[]. If none of these checks succeed, we go to the
2679 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01002680
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002681 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002682 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2683 // /* HeapReference<Class> */ temp1 = src->klass_
2684 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002685 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002686 // Bail out if the source is not a non primitive array.
2687 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2688 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002689 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002690 __ testl(temp1, temp1);
2691 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2692 // If heap poisoning is enabled, `temp1` has been unpoisoned
2693 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2694 } else {
2695 // /* HeapReference<Class> */ temp1 = src->klass_
2696 __ movl(temp1, Address(src, class_offset));
2697 __ MaybeUnpoisonHeapReference(temp1);
2698 // Bail out if the source is not a non primitive array.
2699 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2700 __ movl(temp1, Address(temp1, component_offset));
2701 __ testl(temp1, temp1);
2702 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2703 __ MaybeUnpoisonHeapReference(temp1);
2704 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002705 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002706 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002707 }
2708
Roland Levillain0b671c02016-08-19 12:02:34 +01002709 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2710 if (length.Equals(Location::RegisterLocation(temp3))) {
2711 // When Baker read barriers are enabled, register `temp3`,
2712 // which in the present case contains the `length` parameter,
2713 // will be overwritten below. Make the `length` location
2714 // reference the original stack location; it will be moved
2715 // back to `temp3` later if necessary.
2716 DCHECK(length_arg.IsStackSlot());
2717 length = length_arg;
2718 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002719
Roland Levillain0b671c02016-08-19 12:02:34 +01002720 // /* HeapReference<Class> */ temp1 = dest->klass_
2721 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002722 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002723
Roland Levillain0b671c02016-08-19 12:02:34 +01002724 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2725 // Bail out if the destination is not a non primitive array.
2726 //
2727 // Register `temp1` is not trashed by the read barrier emitted
2728 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2729 // method produces a call to a ReadBarrierMarkRegX entry point,
2730 // which saves all potentially live registers, including
2731 // temporaries such a `temp1`.
2732 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2733 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002734 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002735 __ testl(temp2, temp2);
2736 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2737 // If heap poisoning is enabled, `temp2` has been unpoisoned
2738 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2739 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2740 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2741 }
2742
2743 // For the same reason given earlier, `temp1` is not trashed by the
2744 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2745 // /* HeapReference<Class> */ temp2 = src->klass_
2746 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002747 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002748 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2749 __ cmpl(temp1, temp2);
2750
2751 if (optimizations.GetDestinationIsTypedObjectArray()) {
2752 NearLabel do_copy;
2753 __ j(kEqual, &do_copy);
2754 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2755 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002756 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002757 // We do not need to emit a read barrier for the following
2758 // heap reference load, as `temp1` is only used in a
2759 // comparison with null below, and this reference is not
2760 // kept afterwards.
2761 __ cmpl(Address(temp1, super_offset), Immediate(0));
2762 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2763 __ Bind(&do_copy);
2764 } else {
2765 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2766 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002767 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002768 // Non read barrier code.
2769
2770 // /* HeapReference<Class> */ temp1 = dest->klass_
2771 __ movl(temp1, Address(dest, class_offset));
2772 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2773 __ MaybeUnpoisonHeapReference(temp1);
2774 // Bail out if the destination is not a non primitive array.
2775 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2776 __ movl(temp2, Address(temp1, component_offset));
2777 __ testl(temp2, temp2);
2778 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2779 __ MaybeUnpoisonHeapReference(temp2);
2780 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2781 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2782 // Re-poison the heap reference to make the compare instruction below
2783 // compare two poisoned references.
2784 __ PoisonHeapReference(temp1);
2785 }
2786
2787 // Note: if heap poisoning is on, we are comparing two poisoned references here.
2788 __ cmpl(temp1, Address(src, class_offset));
2789
2790 if (optimizations.GetDestinationIsTypedObjectArray()) {
2791 NearLabel do_copy;
2792 __ j(kEqual, &do_copy);
2793 __ MaybeUnpoisonHeapReference(temp1);
2794 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2795 __ movl(temp1, Address(temp1, component_offset));
2796 __ MaybeUnpoisonHeapReference(temp1);
2797 __ cmpl(Address(temp1, super_offset), Immediate(0));
2798 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2799 __ Bind(&do_copy);
2800 } else {
2801 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2802 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002803 }
2804 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2805 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2806 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002807 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2808 // /* HeapReference<Class> */ temp1 = src->klass_
2809 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002810 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002811 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2812 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002813 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002814 __ testl(temp1, temp1);
2815 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2816 // If heap poisoning is enabled, `temp1` has been unpoisoned
2817 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2818 } else {
2819 // /* HeapReference<Class> */ temp1 = src->klass_
2820 __ movl(temp1, Address(src, class_offset));
2821 __ MaybeUnpoisonHeapReference(temp1);
2822 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2823 __ movl(temp1, Address(temp1, component_offset));
2824 __ testl(temp1, temp1);
2825 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2826 __ MaybeUnpoisonHeapReference(temp1);
2827 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002828 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002829 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002830 }
2831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 const DataType::Type type = DataType::Type::kReference;
2833 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002834
Roland Levillain0b671c02016-08-19 12:02:34 +01002835 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002836 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002837
Roland Levillain0b671c02016-08-19 12:02:34 +01002838 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2839 // If it is needed (in the case of the fast-path loop), the base
2840 // destination address is computed later, as `temp2` is used for
2841 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002842
Roland Levillain0b671c02016-08-19 12:02:34 +01002843 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002844 if (length.IsStackSlot()) {
2845 // Location `length` is again pointing at a stack slot, as
2846 // register `temp3` (which was containing the length parameter
2847 // earlier) has been overwritten; restore it now
2848 DCHECK(length.Equals(length_arg));
2849 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2850 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002851 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002852 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002853
Roland Levillain0b671c02016-08-19 12:02:34 +01002854 // SystemArrayCopy implementation for Baker read barriers (see
2855 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
2856 //
2857 // if (src_ptr != end_ptr) {
2858 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2859 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002860 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01002861 // if (is_gray) {
2862 // // Slow-path copy.
2863 // for (size_t i = 0; i != length; ++i) {
2864 // dest_array[dest_pos + i] =
2865 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
2866 // }
2867 // } else {
2868 // // Fast-path copy.
2869 // do {
2870 // *dest_ptr++ = *src_ptr++;
2871 // } while (src_ptr != end_ptr)
2872 // }
2873 // }
2874
2875 NearLabel loop, done;
2876
2877 // Don't enter copy loop if `length == 0`.
2878 __ cmpl(temp1, temp3);
2879 __ j(kEqual, &done);
2880
Vladimir Marko953437b2016-08-24 08:30:46 +00002881 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01002882 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002883 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00002884 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
2885 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
2886 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
2887
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002888 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00002889 // goto slow_path;
2890 // At this point, just do the "if" and make sure that flags are preserved until the branch.
2891 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01002892
2893 // Load fence to prevent load-load reordering.
2894 // Note that this is a no-op, thanks to the x86 memory model.
2895 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2896
2897 // Slow path used to copy array when `src` is gray.
2898 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002899 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002900 codegen_->AddSlowPath(read_barrier_slow_path);
2901
Vladimir Marko953437b2016-08-24 08:30:46 +00002902 // We have done the "if" of the gray bit check above, now branch based on the flags.
2903 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01002904
2905 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002906 // Compute the base destination address in `temp2`.
2907 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002908 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2909 // poison/unpoison.
2910 __ Bind(&loop);
2911 __ pushl(Address(temp1, 0));
2912 __ cfi().AdjustCFAOffset(4);
2913 __ popl(Address(temp2, 0));
2914 __ cfi().AdjustCFAOffset(-4);
2915 __ addl(temp1, Immediate(element_size));
2916 __ addl(temp2, Immediate(element_size));
2917 __ cmpl(temp1, temp3);
2918 __ j(kNotEqual, &loop);
2919
2920 __ Bind(read_barrier_slow_path->GetExitLabel());
2921 __ Bind(&done);
2922 } else {
2923 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01002924 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002925 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002926 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002927 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002928 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2929 // poison/unpoison.
2930 NearLabel loop, done;
2931 __ cmpl(temp1, temp3);
2932 __ j(kEqual, &done);
2933 __ Bind(&loop);
2934 __ pushl(Address(temp1, 0));
2935 __ cfi().AdjustCFAOffset(4);
2936 __ popl(Address(temp2, 0));
2937 __ cfi().AdjustCFAOffset(-4);
2938 __ addl(temp1, Immediate(element_size));
2939 __ addl(temp2, Immediate(element_size));
2940 __ cmpl(temp1, temp3);
2941 __ j(kNotEqual, &loop);
2942 __ Bind(&done);
2943 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002944
2945 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002946 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002947
Roland Levillain0b671c02016-08-19 12:02:34 +01002948 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002949}
2950
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002951void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002952 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002953 InvokeRuntimeCallingConvention calling_convention;
2954 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2955 invoke,
2956 codegen_,
2957 Location::RegisterLocation(EAX),
2958 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Markoeebb8212018-06-05 14:57:24 +01002959
2960 LocationSummary* locations = invoke->GetLocations();
2961 if (locations != nullptr) {
2962 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
2963 if (invoke_static_or_direct->HasSpecialInput() &&
2964 invoke->InputAt(invoke_static_or_direct->GetSpecialInputIndex())
2965 ->IsX86ComputeBaseMethodAddress()) {
2966 locations->SetInAt(invoke_static_or_direct->GetSpecialInputIndex(),
2967 Location::RequiresRegister());
2968 }
2969 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002970}
2971
2972void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002973 DCHECK(invoke->IsInvokeStaticOrDirect());
Vladimir Marko6fd16062018-06-26 11:02:04 +01002974 IntrinsicVisitor::IntegerValueOfInfo info =
2975 IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002976 LocationSummary* locations = invoke->GetLocations();
2977 X86Assembler* assembler = GetAssembler();
2978
2979 Register out = locations->Out().AsRegister<Register>();
2980 InvokeRuntimeCallingConvention calling_convention;
2981 if (invoke->InputAt(0)->IsConstant()) {
2982 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
Vladimir Marko6fd16062018-06-26 11:02:04 +01002983 if (static_cast<uint32_t>(value - info.low) < info.length) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002984 // Just embed the j.l.Integer in the code.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002985 DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01002986 codegen_->LoadBootImageAddress(
Vladimir Marko6fd16062018-06-26 11:02:04 +01002987 out, info.value_boot_image_reference, invoke->AsInvokeStaticOrDirect());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002988 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002989 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002990 // 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.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002993 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2994 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002995 __ movl(Address(out, info.value_offset), Immediate(value));
2996 }
2997 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002998 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002999 Register in = locations->InAt(0).AsRegister<Register>();
3000 // Check bounds of our cache.
3001 __ leal(out, Address(in, -info.low));
Vladimir Markoeebb8212018-06-05 14:57:24 +01003002 __ cmpl(out, Immediate(info.length));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003003 NearLabel allocate, done;
3004 __ j(kAboveEqual, &allocate);
3005 // If the value is within the bounds, load the j.l.Integer directly from the array.
Vladimir Markoeebb8212018-06-05 14:57:24 +01003006 constexpr size_t kElementSize = sizeof(mirror::HeapReference<mirror::Object>);
Vladimir Markoeebb8212018-06-05 14:57:24 +01003007 static_assert((1u << TIMES_4) == sizeof(mirror::HeapReference<mirror::Object>),
3008 "Check heap reference size.");
Vladimir Marko6fd16062018-06-26 11:02:04 +01003009 if (codegen_->GetCompilerOptions().IsBootImage()) {
3010 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
3011 size_t method_address_index = invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
3012 HX86ComputeBaseMethodAddress* method_address =
3013 invoke->InputAt(method_address_index)->AsX86ComputeBaseMethodAddress();
3014 DCHECK(method_address != nullptr);
3015 Register method_address_reg =
3016 invoke->GetLocations()->InAt(method_address_index).AsRegister<Register>();
3017 __ movl(out, Address(method_address_reg, out, TIMES_4, CodeGeneratorX86::kDummy32BitOffset));
3018 codegen_->RecordBootImageIntrinsicPatch(method_address, info.array_data_boot_image_reference);
3019 } else {
3020 // Note: We're about to clobber the index in `out`, so we need to use `in` and
3021 // adjust the offset accordingly.
3022 uint32_t mid_array_boot_image_offset =
3023 info.array_data_boot_image_reference - info.low * kElementSize;
3024 codegen_->LoadBootImageAddress(
3025 out, mid_array_boot_image_offset, invoke->AsInvokeStaticOrDirect());
3026 DCHECK_NE(out, in);
3027 __ movl(out, Address(out, in, TIMES_4, 0));
3028 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003029 __ MaybeUnpoisonHeapReference(out);
3030 __ jmp(&done);
3031 __ Bind(&allocate);
3032 // Otherwise allocate and initialize a new j.l.Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +01003033 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
3034 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003035 __ movl(Address(out, info.value_offset), in);
3036 __ Bind(&done);
3037 }
3038}
3039
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003040void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003041 LocationSummary* locations =
3042 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003043 locations->SetOut(Location::RequiresRegister());
3044}
3045
3046void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
3047 X86Assembler* assembler = GetAssembler();
3048 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
3049 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
3050 NearLabel done;
3051 __ fs()->movl(out, address);
3052 __ testl(out, out);
3053 __ j(kEqual, &done);
3054 __ fs()->movl(address, Immediate(0));
3055 codegen_->MemoryFence();
3056 __ Bind(&done);
3057}
3058
Hans Boehmc7b28de2018-03-09 17:05:28 -08003059void IntrinsicLocationsBuilderX86::VisitReachabilityFence(HInvoke* invoke) {
3060 LocationSummary* locations =
3061 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
3062 locations->SetInAt(0, Location::Any());
3063}
3064
3065void IntrinsicCodeGeneratorX86::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003066
Aart Bik2f9fcc92016-03-01 15:16:54 -08003067UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003068UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003069UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
3070UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
3071UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
3072UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
xueliang.zhongcb58b072017-10-13 12:06:56 +01003073UNIMPLEMENTED_INTRINSIC(X86, CRC32Update)
Mark Mendell09ed1a32015-03-25 08:30:06 -04003074
Aart Bikff7d89c2016-11-07 08:49:28 -08003075UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
3076UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003077UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
3078UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
3079UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
3080UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
3081UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
3082UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003083
Aart Bik0e54c012016-03-04 12:08:31 -08003084// 1.8.
3085UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
3086UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
3087UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
3088UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
3089UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003090
Aart Bik2f9fcc92016-03-01 15:16:54 -08003091UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01003092
3093#undef __
3094
Mark Mendell09ed1a32015-03-25 08:30:06 -04003095} // namespace x86
3096} // namespace art