blob: f84a33bb8e33899423bdf385f24e4c7328eb8d7e [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
85 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
86 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
163 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86"; }
164
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
548void IntrinsicLocationsBuilderX86::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100549 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400550}
551
552void IntrinsicCodeGeneratorX86::VisitMathCos(HInvoke* invoke) {
553 GenFPToFPCall(invoke, codegen_, kQuickCos);
554}
555
556void IntrinsicLocationsBuilderX86::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100557 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400558}
559
560void IntrinsicCodeGeneratorX86::VisitMathSin(HInvoke* invoke) {
561 GenFPToFPCall(invoke, codegen_, kQuickSin);
562}
563
564void IntrinsicLocationsBuilderX86::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100565 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400566}
567
568void IntrinsicCodeGeneratorX86::VisitMathAcos(HInvoke* invoke) {
569 GenFPToFPCall(invoke, codegen_, kQuickAcos);
570}
571
572void IntrinsicLocationsBuilderX86::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100573 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400574}
575
576void IntrinsicCodeGeneratorX86::VisitMathAsin(HInvoke* invoke) {
577 GenFPToFPCall(invoke, codegen_, kQuickAsin);
578}
579
580void IntrinsicLocationsBuilderX86::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100581 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400582}
583
584void IntrinsicCodeGeneratorX86::VisitMathAtan(HInvoke* invoke) {
585 GenFPToFPCall(invoke, codegen_, kQuickAtan);
586}
587
588void IntrinsicLocationsBuilderX86::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100589 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400590}
591
592void IntrinsicCodeGeneratorX86::VisitMathCbrt(HInvoke* invoke) {
593 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
594}
595
596void IntrinsicLocationsBuilderX86::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100597 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400598}
599
600void IntrinsicCodeGeneratorX86::VisitMathCosh(HInvoke* invoke) {
601 GenFPToFPCall(invoke, codegen_, kQuickCosh);
602}
603
604void IntrinsicLocationsBuilderX86::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100605 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400606}
607
608void IntrinsicCodeGeneratorX86::VisitMathExp(HInvoke* invoke) {
609 GenFPToFPCall(invoke, codegen_, kQuickExp);
610}
611
612void IntrinsicLocationsBuilderX86::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100613 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400614}
615
616void IntrinsicCodeGeneratorX86::VisitMathExpm1(HInvoke* invoke) {
617 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
618}
619
620void IntrinsicLocationsBuilderX86::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100621 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400622}
623
624void IntrinsicCodeGeneratorX86::VisitMathLog(HInvoke* invoke) {
625 GenFPToFPCall(invoke, codegen_, kQuickLog);
626}
627
628void IntrinsicLocationsBuilderX86::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100629 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400630}
631
632void IntrinsicCodeGeneratorX86::VisitMathLog10(HInvoke* invoke) {
633 GenFPToFPCall(invoke, codegen_, kQuickLog10);
634}
635
636void IntrinsicLocationsBuilderX86::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100637 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400638}
639
640void IntrinsicCodeGeneratorX86::VisitMathSinh(HInvoke* invoke) {
641 GenFPToFPCall(invoke, codegen_, kQuickSinh);
642}
643
644void IntrinsicLocationsBuilderX86::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100645 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400646}
647
648void IntrinsicCodeGeneratorX86::VisitMathTan(HInvoke* invoke) {
649 GenFPToFPCall(invoke, codegen_, kQuickTan);
650}
651
652void IntrinsicLocationsBuilderX86::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100653 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400654}
655
656void IntrinsicCodeGeneratorX86::VisitMathTanh(HInvoke* invoke) {
657 GenFPToFPCall(invoke, codegen_, kQuickTanh);
658}
659
Vladimir Markoca6fff82017-10-03 14:49:14 +0100660static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
661 LocationSummary* locations =
662 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400663 InvokeRuntimeCallingConvention calling_convention;
664 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
665 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
666 locations->SetOut(Location::FpuRegisterLocation(XMM0));
667}
668
669void IntrinsicLocationsBuilderX86::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100670 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400671}
672
673void IntrinsicCodeGeneratorX86::VisitMathAtan2(HInvoke* invoke) {
674 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
675}
676
Vladimir Marko4d179872018-01-19 14:50:10 +0000677void IntrinsicLocationsBuilderX86::VisitMathPow(HInvoke* invoke) {
678 CreateFPFPToFPCallLocations(allocator_, invoke);
679}
680
681void IntrinsicCodeGeneratorX86::VisitMathPow(HInvoke* invoke) {
682 GenFPToFPCall(invoke, codegen_, kQuickPow);
683}
684
Mark Mendella4f12202015-08-06 15:23:34 -0400685void IntrinsicLocationsBuilderX86::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100686 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400687}
688
689void IntrinsicCodeGeneratorX86::VisitMathHypot(HInvoke* invoke) {
690 GenFPToFPCall(invoke, codegen_, kQuickHypot);
691}
692
693void IntrinsicLocationsBuilderX86::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100694 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400695}
696
697void IntrinsicCodeGeneratorX86::VisitMathNextAfter(HInvoke* invoke) {
698 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
699}
700
Mark Mendell6bc53a92015-07-01 14:26:52 -0400701void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
702 // We need at least two of the positions or length to be an integer constant,
703 // or else we won't have enough free registers.
704 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
705 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
706 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
707
708 int num_constants =
709 ((src_pos != nullptr) ? 1 : 0)
710 + ((dest_pos != nullptr) ? 1 : 0)
711 + ((length != nullptr) ? 1 : 0);
712
713 if (num_constants < 2) {
714 // Not enough free registers.
715 return;
716 }
717
718 // As long as we are checking, we might as well check to see if the src and dest
719 // positions are >= 0.
720 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
721 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
722 // We will have to fail anyways.
723 return;
724 }
725
726 // And since we are already checking, check the length too.
727 if (length != nullptr) {
728 int32_t len = length->GetValue();
729 if (len < 0) {
730 // Just call as normal.
731 return;
732 }
733 }
734
735 // Okay, it is safe to generate inline code.
736 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +0100737 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400738 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
739 locations->SetInAt(0, Location::RequiresRegister());
740 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
741 locations->SetInAt(2, Location::RequiresRegister());
742 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
743 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
744
745 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
746 locations->AddTemp(Location::RegisterLocation(ESI));
747 locations->AddTemp(Location::RegisterLocation(EDI));
748 locations->AddTemp(Location::RegisterLocation(ECX));
749}
750
751static void CheckPosition(X86Assembler* assembler,
752 Location pos,
753 Register input,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100754 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -0700755 SlowPathCode* slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100756 Register temp,
757 bool length_is_input_length = false) {
758 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -0400759 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
760
761 if (pos.IsConstant()) {
762 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
763 if (pos_const == 0) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100764 if (!length_is_input_length) {
765 // Check that length(input) >= length.
766 if (length.IsConstant()) {
767 __ cmpl(Address(input, length_offset),
768 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
769 } else {
770 __ cmpl(Address(input, length_offset), length.AsRegister<Register>());
771 }
772 __ j(kLess, slow_path->GetEntryLabel());
773 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400774 } else {
775 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100776 __ movl(temp, Address(input, length_offset));
777 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -0400778 __ j(kLess, slow_path->GetEntryLabel());
779
780 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100781 if (length.IsConstant()) {
782 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
783 } else {
784 __ cmpl(temp, length.AsRegister<Register>());
785 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400786 __ j(kLess, slow_path->GetEntryLabel());
787 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100788 } else if (length_is_input_length) {
789 // The only way the copy can succeed is if pos is zero.
790 Register pos_reg = pos.AsRegister<Register>();
791 __ testl(pos_reg, pos_reg);
792 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -0400793 } else {
794 // Check that pos >= 0.
795 Register pos_reg = pos.AsRegister<Register>();
796 __ testl(pos_reg, pos_reg);
797 __ j(kLess, slow_path->GetEntryLabel());
798
799 // Check that pos <= length(input).
800 __ cmpl(Address(input, length_offset), pos_reg);
801 __ j(kLess, slow_path->GetEntryLabel());
802
803 // Check that (length(input) - pos) >= length.
804 __ movl(temp, Address(input, length_offset));
805 __ subl(temp, pos_reg);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100806 if (length.IsConstant()) {
807 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
808 } else {
809 __ cmpl(temp, length.AsRegister<Register>());
810 }
Mark Mendell6bc53a92015-07-01 14:26:52 -0400811 __ j(kLess, slow_path->GetEntryLabel());
812 }
813}
814
815void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
816 X86Assembler* assembler = GetAssembler();
817 LocationSummary* locations = invoke->GetLocations();
818
819 Register src = locations->InAt(0).AsRegister<Register>();
820 Location srcPos = locations->InAt(1);
821 Register dest = locations->InAt(2).AsRegister<Register>();
822 Location destPos = locations->InAt(3);
823 Location length = locations->InAt(4);
824
825 // Temporaries that we need for MOVSW.
826 Register src_base = locations->GetTemp(0).AsRegister<Register>();
827 DCHECK_EQ(src_base, ESI);
828 Register dest_base = locations->GetTemp(1).AsRegister<Register>();
829 DCHECK_EQ(dest_base, EDI);
830 Register count = locations->GetTemp(2).AsRegister<Register>();
831 DCHECK_EQ(count, ECX);
832
Vladimir Marko174b2e22017-10-12 13:34:49 +0100833 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400834 codegen_->AddSlowPath(slow_path);
835
836 // Bail out if the source and destination are the same (to handle overlap).
837 __ cmpl(src, dest);
838 __ j(kEqual, slow_path->GetEntryLabel());
839
840 // Bail out if the source is null.
841 __ testl(src, src);
842 __ j(kEqual, slow_path->GetEntryLabel());
843
844 // Bail out if the destination is null.
845 __ testl(dest, dest);
846 __ j(kEqual, slow_path->GetEntryLabel());
847
848 // If the length is negative, bail out.
849 // We have already checked in the LocationsBuilder for the constant case.
850 if (!length.IsConstant()) {
851 __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>());
852 __ j(kLess, slow_path->GetEntryLabel());
853 }
854
855 // We need the count in ECX.
856 if (length.IsConstant()) {
857 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
858 } else {
859 __ movl(count, length.AsRegister<Register>());
860 }
861
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100862 // Validity checks: source. Use src_base as a temporary register.
863 CheckPosition(assembler, srcPos, src, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400864
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +0100865 // Validity checks: dest. Use src_base as a temporary register.
866 CheckPosition(assembler, destPos, dest, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400867
868 // Okay, everything checks out. Finally time to do the copy.
869 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100870 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -0400871 DCHECK_EQ(char_size, 2u);
872
873 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
874
875 if (srcPos.IsConstant()) {
876 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
877 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
878 } else {
879 __ leal(src_base, Address(src, srcPos.AsRegister<Register>(),
880 ScaleFactor::TIMES_2, data_offset));
881 }
882 if (destPos.IsConstant()) {
883 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
884
885 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
886 } else {
887 __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(),
888 ScaleFactor::TIMES_2, data_offset));
889 }
890
891 // Do the move.
892 __ rep_movsw();
893
894 __ Bind(slow_path->GetExitLabel());
895}
896
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000897void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
898 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100899 LocationSummary* locations = new (allocator_) LocationSummary(
900 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000901 InvokeRuntimeCallingConvention calling_convention;
902 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
903 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
904 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000905}
906
907void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
908 X86Assembler* assembler = GetAssembler();
909 LocationSummary* locations = invoke->GetLocations();
910
Nicolas Geoffray512e04d2015-03-27 17:21:24 +0000911 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +0100912 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000913
914 Register argument = locations->InAt(1).AsRegister<Register>();
915 __ testl(argument, argument);
Vladimir Marko174b2e22017-10-12 13:34:49 +0100916 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000917 codegen_->AddSlowPath(slow_path);
918 __ j(kEqual, slow_path->GetEntryLabel());
919
Serban Constantinescuba45db02016-07-12 22:53:02 +0100920 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +0000921 __ Bind(slow_path->GetExitLabel());
922}
923
Agi Csakid7138c82015-08-13 17:46:44 -0700924void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +0000925 if (kEmitCompilerReadBarrier &&
926 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
927 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
928 // No support for this odd case (String class is moveable, not in the boot image).
929 return;
930 }
931
Vladimir Markoca6fff82017-10-03 14:49:14 +0100932 LocationSummary* locations =
933 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -0700934 locations->SetInAt(0, Location::RequiresRegister());
935 locations->SetInAt(1, Location::RequiresRegister());
936
937 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
938 locations->AddTemp(Location::RegisterLocation(ECX));
939 locations->AddTemp(Location::RegisterLocation(EDI));
940
941 // Set output, ESI needed for repe_cmpsl instruction anyways.
942 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
943}
944
945void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
946 X86Assembler* assembler = GetAssembler();
947 LocationSummary* locations = invoke->GetLocations();
948
949 Register str = locations->InAt(0).AsRegister<Register>();
950 Register arg = locations->InAt(1).AsRegister<Register>();
951 Register ecx = locations->GetTemp(0).AsRegister<Register>();
952 Register edi = locations->GetTemp(1).AsRegister<Register>();
953 Register esi = locations->Out().AsRegister<Register>();
954
Mark Mendell0c9497d2015-08-21 09:30:05 -0400955 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -0700956
957 // Get offsets of count, value, and class fields within a string object.
958 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
959 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
960 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
961
962 // Note that the null check must have been done earlier.
963 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
964
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100965 StringEqualsOptimizations optimizations(invoke);
966 if (!optimizations.GetArgumentNotNull()) {
967 // Check if input is null, return false if it is.
968 __ testl(arg, arg);
969 __ j(kEqual, &return_false);
970 }
Agi Csakid7138c82015-08-13 17:46:44 -0700971
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100972 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +0100973 // Instanceof check for the argument by comparing class fields.
974 // All string objects must have the same type since String cannot be subclassed.
975 // Receiver must be a string object, so its class field is equal to all strings' class fields.
976 // 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 +0100977 __ movl(ecx, Address(str, class_offset));
978 __ cmpl(ecx, Address(arg, class_offset));
979 __ j(kNotEqual, &return_false);
980 }
Agi Csakid7138c82015-08-13 17:46:44 -0700981
982 // Reference equality check, return true if same reference.
983 __ cmpl(str, arg);
984 __ j(kEqual, &return_true);
985
jessicahandojo4877b792016-09-08 19:49:13 -0700986 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -0700987 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -0700988 // Check if lengths and compression flags are equal, return false if they're not.
989 // Two identical strings will always have same compression style since
990 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -0700991 __ cmpl(ecx, Address(arg, count_offset));
992 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100993 // Return true if strings are empty. Even with string compression `count == 0` means empty.
994 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
995 "Expecting 0=compressed, 1=uncompressed");
996 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -0700997
jessicahandojo4877b792016-09-08 19:49:13 -0700998 if (mirror::kUseStringCompression) {
999 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001000 // Extract length and differentiate between both compressed or both uncompressed.
1001 // Different compression style is cut above.
1002 __ shrl(ecx, Immediate(1));
1003 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001004 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001005 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001006 __ shrl(ecx, Immediate(1));
1007 __ Bind(&string_uncompressed);
1008 }
Agi Csakid7138c82015-08-13 17:46:44 -07001009 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1010 __ leal(esi, Address(str, value_offset));
1011 __ leal(edi, Address(arg, value_offset));
1012
jessicahandojo4877b792016-09-08 19:49:13 -07001013 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1014 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001015 __ addl(ecx, Immediate(1));
1016 __ shrl(ecx, Immediate(1));
1017
jessicahandojo4877b792016-09-08 19:49:13 -07001018 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1019 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001020 DCHECK_ALIGNED(value_offset, 4);
1021 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1022
1023 // Loop to compare strings two characters at a time starting at the beginning of the string.
1024 __ repe_cmpsl();
1025 // If strings are not equal, zero flag will be cleared.
1026 __ j(kNotEqual, &return_false);
1027
1028 // Return true and exit the function.
1029 // If loop does not result in returning false, we return true.
1030 __ Bind(&return_true);
1031 __ movl(esi, Immediate(1));
1032 __ jmp(&end);
1033
1034 // Return false and exit the function.
1035 __ Bind(&return_false);
1036 __ xorl(esi, esi);
1037 __ Bind(&end);
1038}
1039
Andreas Gampe21030dd2015-05-07 14:46:15 -07001040static void CreateStringIndexOfLocations(HInvoke* invoke,
1041 ArenaAllocator* allocator,
1042 bool start_at_zero) {
1043 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1044 LocationSummary::kCallOnSlowPath,
1045 kIntrinsified);
1046 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1047 locations->SetInAt(0, Location::RegisterLocation(EDI));
1048 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1049 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1050 // of the instruction explicitly.
1051 // Note: This works as we don't clobber EAX anywhere.
1052 locations->SetInAt(1, Location::RegisterLocation(EAX));
1053 if (!start_at_zero) {
1054 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1055 }
1056 // As we clobber EDI during execution anyways, also use it as the output.
1057 locations->SetOut(Location::SameAsFirstInput());
1058
1059 // repne scasw uses ECX as the counter.
1060 locations->AddTemp(Location::RegisterLocation(ECX));
1061 // Need another temporary to be able to compute the result.
1062 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001063 if (mirror::kUseStringCompression) {
1064 // Need another temporary to be able to save unflagged string length.
1065 locations->AddTemp(Location::RequiresRegister());
1066 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001067}
1068
1069static void GenerateStringIndexOf(HInvoke* invoke,
1070 X86Assembler* assembler,
1071 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001072 bool start_at_zero) {
1073 LocationSummary* locations = invoke->GetLocations();
1074
1075 // Note that the null check must have been done earlier.
1076 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1077
1078 Register string_obj = locations->InAt(0).AsRegister<Register>();
1079 Register search_value = locations->InAt(1).AsRegister<Register>();
1080 Register counter = locations->GetTemp(0).AsRegister<Register>();
1081 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1082 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001083 // Only used when string compression feature is on.
1084 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001085
1086 // Check our assumptions for registers.
1087 DCHECK_EQ(string_obj, EDI);
1088 DCHECK_EQ(search_value, EAX);
1089 DCHECK_EQ(counter, ECX);
1090 DCHECK_EQ(out, EDI);
1091
1092 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001093 // 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 -07001094 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001095 HInstruction* code_point = invoke->InputAt(1);
1096 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001097 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001098 std::numeric_limits<uint16_t>::max()) {
1099 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1100 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001101 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001102 codegen->AddSlowPath(slow_path);
1103 __ jmp(slow_path->GetEntryLabel());
1104 __ Bind(slow_path->GetExitLabel());
1105 return;
1106 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001107 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001108 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001109 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001110 codegen->AddSlowPath(slow_path);
1111 __ j(kAbove, slow_path->GetEntryLabel());
1112 }
1113
1114 // From here down, we know that we are looking for a char that fits in 16 bits.
1115 // Location of reference to data array within the String object.
1116 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1117 // Location of count within the String object.
1118 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1119
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001120 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001121 __ movl(string_length, Address(string_obj, count_offset));
1122
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001123 // Do a zero-length check. Even with string compression `count == 0` means empty.
1124 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1125 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001126 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001127 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001128 __ testl(string_length, string_length);
1129 __ j(kEqual, &not_found_label);
1130
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001131 if (mirror::kUseStringCompression) {
1132 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1133 __ movl(string_length_flagged, string_length);
1134 // Extract the length and shift out the least significant bit used as compression flag.
1135 __ shrl(string_length, Immediate(1));
1136 }
1137
Andreas Gampe21030dd2015-05-07 14:46:15 -07001138 if (start_at_zero) {
1139 // Number of chars to scan is the same as the string length.
1140 __ movl(counter, string_length);
1141
1142 // Move to the start of the string.
1143 __ addl(string_obj, Immediate(value_offset));
1144 } else {
1145 Register start_index = locations->InAt(2).AsRegister<Register>();
1146
1147 // Do a start_index check.
1148 __ cmpl(start_index, string_length);
1149 __ j(kGreaterEqual, &not_found_label);
1150
1151 // Ensure we have a start index >= 0;
1152 __ xorl(counter, counter);
1153 __ cmpl(start_index, Immediate(0));
1154 __ cmovl(kGreater, counter, start_index);
1155
jessicahandojo4877b792016-09-08 19:49:13 -07001156 if (mirror::kUseStringCompression) {
1157 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001158 __ testl(string_length_flagged, Immediate(1));
1159 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001160 // Move to the start of the string: string_obj + value_offset + start_index.
1161 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1162 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001163
jessicahandojo4877b792016-09-08 19:49:13 -07001164 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1165 __ Bind(&offset_uncompressed_label);
1166 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1167
1168 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1169 // compare.
1170 __ Bind(&modify_counter);
1171 } else {
1172 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1173 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001174 __ negl(counter);
1175 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1176 }
1177
jessicahandojo4877b792016-09-08 19:49:13 -07001178 if (mirror::kUseStringCompression) {
1179 NearLabel uncompressed_string_comparison;
1180 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001181 __ testl(string_length_flagged, Immediate(1));
1182 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001183
jessicahandojo4877b792016-09-08 19:49:13 -07001184 // Check if EAX (search_value) is ASCII.
1185 __ cmpl(search_value, Immediate(127));
1186 __ j(kGreater, &not_found_label);
1187 // Comparing byte-per-byte.
1188 __ repne_scasb();
1189 __ jmp(&comparison_done);
1190
1191 // Everything is set up for repne scasw:
1192 // * Comparison address in EDI.
1193 // * Counter in ECX.
1194 __ Bind(&uncompressed_string_comparison);
1195 __ repne_scasw();
1196 __ Bind(&comparison_done);
1197 } else {
1198 __ repne_scasw();
1199 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001200 // Did we find a match?
1201 __ j(kNotEqual, &not_found_label);
1202
1203 // Yes, we matched. Compute the index of the result.
1204 __ subl(string_length, counter);
1205 __ leal(out, Address(string_length, -1));
1206
Mark Mendell0c9497d2015-08-21 09:30:05 -04001207 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001208 __ jmp(&done);
1209
1210 // Failed to match; return -1.
1211 __ Bind(&not_found_label);
1212 __ movl(out, Immediate(-1));
1213
1214 // And join up at the end.
1215 __ Bind(&done);
1216 if (slow_path != nullptr) {
1217 __ Bind(slow_path->GetExitLabel());
1218 }
1219}
1220
1221void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001222 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001223}
1224
1225void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001226 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001227}
1228
1229void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001230 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001231}
1232
1233void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001234 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001235}
1236
Jeff Hao848f70a2014-01-15 13:49:50 -08001237void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001238 LocationSummary* locations = new (allocator_) LocationSummary(
1239 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001240 InvokeRuntimeCallingConvention calling_convention;
1241 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1242 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1243 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1244 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1245 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001246}
1247
1248void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1249 X86Assembler* assembler = GetAssembler();
1250 LocationSummary* locations = invoke->GetLocations();
1251
1252 Register byte_array = locations->InAt(0).AsRegister<Register>();
1253 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001254 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001255 codegen_->AddSlowPath(slow_path);
1256 __ j(kEqual, slow_path->GetEntryLabel());
1257
Serban Constantinescuba45db02016-07-12 22:53:02 +01001258 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001259 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001260 __ Bind(slow_path->GetExitLabel());
1261}
1262
1263void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001264 LocationSummary* locations =
1265 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001266 InvokeRuntimeCallingConvention calling_convention;
1267 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1268 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1269 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1270 locations->SetOut(Location::RegisterLocation(EAX));
1271}
1272
1273void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001274 // No need to emit code checking whether `locations->InAt(2)` is a null
1275 // pointer, as callers of the native method
1276 //
1277 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1278 //
1279 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001280 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001281 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001282}
1283
1284void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001285 LocationSummary* locations = new (allocator_) LocationSummary(
1286 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001287 InvokeRuntimeCallingConvention calling_convention;
1288 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1289 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001290}
1291
1292void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1293 X86Assembler* assembler = GetAssembler();
1294 LocationSummary* locations = invoke->GetLocations();
1295
1296 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1297 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001298 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001299 codegen_->AddSlowPath(slow_path);
1300 __ j(kEqual, slow_path->GetEntryLabel());
1301
Serban Constantinescuba45db02016-07-12 22:53:02 +01001302 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001303 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001304 __ Bind(slow_path->GetExitLabel());
1305}
1306
Mark Mendell8f8926a2015-08-17 11:39:06 -04001307void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1308 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001309 LocationSummary* locations =
1310 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001311 locations->SetInAt(0, Location::RequiresRegister());
1312 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1313 // Place srcEnd in ECX to save a move below.
1314 locations->SetInAt(2, Location::RegisterLocation(ECX));
1315 locations->SetInAt(3, Location::RequiresRegister());
1316 locations->SetInAt(4, Location::RequiresRegister());
1317
1318 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1319 // We don't have enough registers to also grab ECX, so handle below.
1320 locations->AddTemp(Location::RegisterLocation(ESI));
1321 locations->AddTemp(Location::RegisterLocation(EDI));
1322}
1323
1324void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1325 X86Assembler* assembler = GetAssembler();
1326 LocationSummary* locations = invoke->GetLocations();
1327
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001328 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001329 // Location of data in char array buffer.
1330 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1331 // Location of char array data in string.
1332 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1333
1334 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1335 Register obj = locations->InAt(0).AsRegister<Register>();
1336 Location srcBegin = locations->InAt(1);
1337 int srcBegin_value =
1338 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1339 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1340 Register dst = locations->InAt(3).AsRegister<Register>();
1341 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1342
1343 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001344 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001345 DCHECK_EQ(char_size, 2u);
1346
Mark Mendell8f8926a2015-08-17 11:39:06 -04001347 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001348 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001349 __ pushl(ECX);
1350 int stack_adjust = kX86WordSize;
1351 __ cfi().AdjustCFAOffset(stack_adjust);
1352 DCHECK_EQ(srcEnd, ECX);
1353 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001354 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001355 } else {
1356 DCHECK(srcBegin.IsRegister());
1357 __ subl(ECX, srcBegin.AsRegister<Register>());
1358 }
1359
jessicahandojo4877b792016-09-08 19:49:13 -07001360 NearLabel done;
1361 if (mirror::kUseStringCompression) {
1362 // Location of count in string
1363 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001364 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001365 DCHECK_EQ(c_char_size, 1u);
1366 __ pushl(EAX);
1367 __ cfi().AdjustCFAOffset(stack_adjust);
1368
1369 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001370 __ testl(Address(obj, count_offset), Immediate(1));
1371 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1372 "Expecting 0=compressed, 1=uncompressed");
1373 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001374 // Compute the address of the source string by adding the number of chars from
1375 // the source beginning to the value offset of a string.
1376 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1377
1378 // Start the loop to copy String's value to Array of Char.
1379 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1380 __ Bind(&copy_loop);
1381 __ jecxz(&done);
1382 // Use EAX temporary (convert byte from ESI to word).
1383 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1384 __ movzxb(EAX, Address(ESI, 0));
1385 __ movw(Address(EDI, 0), EAX);
1386 __ leal(EDI, Address(EDI, char_size));
1387 __ leal(ESI, Address(ESI, c_char_size));
1388 // TODO: Add support for LOOP to X86Assembler.
1389 __ subl(ECX, Immediate(1));
1390 __ jmp(&copy_loop);
1391 __ Bind(&copy_uncompressed);
1392 }
1393
1394 // Do the copy for uncompressed string.
1395 // Compute the address of the destination buffer.
1396 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1397 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001398 __ rep_movsw();
1399
jessicahandojo4877b792016-09-08 19:49:13 -07001400 __ Bind(&done);
1401 if (mirror::kUseStringCompression) {
1402 // Restore EAX.
1403 __ popl(EAX);
1404 __ cfi().AdjustCFAOffset(-stack_adjust);
1405 }
1406 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001407 __ popl(ECX);
1408 __ cfi().AdjustCFAOffset(-stack_adjust);
1409}
1410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001411static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001412 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1413 Location out_loc = locations->Out();
1414 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1415 // to avoid a SIGBUS.
1416 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001417 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001418 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1419 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001420 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001421 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1422 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001423 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001424 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1425 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001426 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001427 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1428 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1429 break;
1430 default:
1431 LOG(FATAL) << "Type not recognized for peek: " << size;
1432 UNREACHABLE();
1433 }
1434}
1435
1436void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001437 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001438}
1439
1440void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001441 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001442}
1443
1444void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001445 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001446}
1447
1448void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001450}
1451
1452void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001453 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001454}
1455
1456void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001457 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001458}
1459
1460void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001461 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001462}
1463
1464void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001466}
1467
Vladimir Markoca6fff82017-10-03 14:49:14 +01001468static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1469 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001470 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001471 LocationSummary* locations =
1472 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001473 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001474 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001475 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001476 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1477 } else {
1478 locations->SetInAt(1, Location::RegisterOrConstant(value));
1479 }
1480}
1481
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001482static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001483 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1484 Location value_loc = locations->InAt(1);
1485 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1486 // to avoid a SIGBUS.
1487 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001488 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001489 if (value_loc.IsConstant()) {
1490 __ movb(Address(address, 0),
1491 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1492 } else {
1493 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1494 }
1495 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001496 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001497 if (value_loc.IsConstant()) {
1498 __ movw(Address(address, 0),
1499 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1500 } else {
1501 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1502 }
1503 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001504 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001505 if (value_loc.IsConstant()) {
1506 __ movl(Address(address, 0),
1507 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1508 } else {
1509 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1510 }
1511 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001512 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001513 if (value_loc.IsConstant()) {
1514 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1515 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1516 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1517 } else {
1518 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1519 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1520 }
1521 break;
1522 default:
1523 LOG(FATAL) << "Type not recognized for poke: " << size;
1524 UNREACHABLE();
1525 }
1526}
1527
1528void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001529 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001530}
1531
1532void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001533 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001534}
1535
1536void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001537 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001538}
1539
1540void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001541 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001542}
1543
1544void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001545 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001546}
1547
1548void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001549 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001550}
1551
1552void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001553 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001554}
1555
1556void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001557 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001558}
1559
1560void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001561 LocationSummary* locations =
1562 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001563 locations->SetOut(Location::RequiresRegister());
1564}
1565
1566void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1567 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001568 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001569}
1570
Roland Levillain0d5a2812015-11-13 10:07:31 +00001571static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001572 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001573 bool is_volatile,
1574 CodeGeneratorX86* codegen) {
1575 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1576 LocationSummary* locations = invoke->GetLocations();
1577 Location base_loc = locations->InAt(1);
1578 Register base = base_loc.AsRegister<Register>();
1579 Location offset_loc = locations->InAt(2);
1580 Register offset = offset_loc.AsRegisterPairLow<Register>();
1581 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001582
1583 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001585 Register output = output_loc.AsRegister<Register>();
1586 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00001587 break;
1588 }
1589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001590 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001591 Register output = output_loc.AsRegister<Register>();
1592 if (kEmitCompilerReadBarrier) {
1593 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001594 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1595 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001596 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00001597 } else {
1598 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1599 codegen->GenerateReadBarrierSlow(
1600 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1601 }
1602 } else {
1603 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1604 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001605 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001606 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001607 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001608
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001610 Register output_lo = output_loc.AsRegisterPairLow<Register>();
1611 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001612 if (is_volatile) {
1613 // Need to use a XMM to read atomically.
1614 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1615 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1616 __ movd(output_lo, temp);
1617 __ psrlq(temp, Immediate(32));
1618 __ movd(output_hi, temp);
1619 } else {
1620 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1621 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1622 }
1623 }
1624 break;
1625
1626 default:
1627 LOG(FATAL) << "Unsupported op size " << type;
1628 UNREACHABLE();
1629 }
1630}
1631
Vladimir Markoca6fff82017-10-03 14:49:14 +01001632static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001633 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001634 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001635 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001636 bool can_call = kEmitCompilerReadBarrier &&
1637 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1638 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001639 LocationSummary* locations =
1640 new (allocator) LocationSummary(invoke,
1641 can_call
1642 ? LocationSummary::kCallOnSlowPath
1643 : LocationSummary::kNoCall,
1644 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01001645 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001646 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001647 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001648 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1649 locations->SetInAt(1, Location::RequiresRegister());
1650 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001651 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001652 if (is_volatile) {
1653 // Need to use XMM to read volatile.
1654 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001656 } else {
1657 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1658 }
1659 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001660 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001661 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001662 }
1663}
1664
1665void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001666 CreateIntIntIntToIntLocations(
1667 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001668}
1669void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001670 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001671}
1672void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001673 CreateIntIntIntToIntLocations(
1674 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001675}
1676void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001677 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001678}
1679void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001681 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001682}
1683void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001684 CreateIntIntIntToIntLocations(
1685 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001686}
1687
1688
1689void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001690 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001691}
1692void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001693 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001694}
1695void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001696 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001697}
1698void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001700}
1701void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001702 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001703}
1704void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001705 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001706}
1707
1708
Vladimir Markoca6fff82017-10-03 14:49:14 +01001709static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001711 HInvoke* invoke,
1712 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001713 LocationSummary* locations =
1714 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001715 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1716 locations->SetInAt(1, Location::RequiresRegister());
1717 locations->SetInAt(2, Location::RequiresRegister());
1718 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001719 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001720 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001721 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001722 // Ensure the value is in a byte register.
1723 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001724 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001725 locations->AddTemp(Location::RequiresFpuRegister());
1726 locations->AddTemp(Location::RequiresFpuRegister());
1727 }
1728}
1729
1730void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001731 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001732 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001733}
1734void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001735 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001736 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001737}
1738void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001739 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001740 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001741}
1742void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001743 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001744 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001745}
1746void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001747 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001748 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001749}
1750void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001751 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001752 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001753}
1754void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001755 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001756 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001757}
1758void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001759 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001760 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001761}
1762void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001763 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001764 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001765}
1766
1767// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1768// memory model.
1769static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001770 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001771 bool is_volatile,
1772 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001773 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001774 Register base = locations->InAt(1).AsRegister<Register>();
1775 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1776 Location value_loc = locations->InAt(3);
1777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001778 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001779 Register value_lo = value_loc.AsRegisterPairLow<Register>();
1780 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
1781 if (is_volatile) {
1782 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1783 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1784 __ movd(temp1, value_lo);
1785 __ movd(temp2, value_hi);
1786 __ punpckldq(temp1, temp2);
1787 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
1788 } else {
1789 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
1790 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
1791 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001792 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001793 Register temp = locations->GetTemp(0).AsRegister<Register>();
1794 __ movl(temp, value_loc.AsRegister<Register>());
1795 __ PoisonHeapReference(temp);
1796 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001797 } else {
1798 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
1799 }
1800
1801 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00001802 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001803 }
1804
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001805 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001806 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04001807 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1808 locations->GetTemp(1).AsRegister<Register>(),
1809 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001810 value_loc.AsRegister<Register>(),
1811 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001812 }
1813}
1814
1815void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001816 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001817}
1818void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001820}
1821void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001823}
1824void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001825 GenUnsafePut(
1826 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001827}
1828void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 GenUnsafePut(
1830 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001831}
1832void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001833 GenUnsafePut(
1834 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001835}
1836void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001837 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001838}
1839void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001840 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001841}
1842void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001843 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001844}
1845
Vladimir Markoca6fff82017-10-03 14:49:14 +01001846static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001847 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04001848 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001849 bool can_call = kEmitCompilerReadBarrier &&
1850 kUseBakerReadBarrier &&
1851 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001852 LocationSummary* locations =
1853 new (allocator) LocationSummary(invoke,
1854 can_call
1855 ? LocationSummary::kCallOnSlowPath
1856 : LocationSummary::kNoCall,
1857 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001858 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1859 locations->SetInAt(1, Location::RequiresRegister());
1860 // Offset is a long, but in 32 bit mode, we only need the low word.
1861 // Can we update the invoke here to remove a TypeConvert to Long?
1862 locations->SetInAt(2, Location::RequiresRegister());
1863 // Expected value must be in EAX or EDX:EAX.
1864 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04001866 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
1867 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
1868 } else {
1869 locations->SetInAt(3, Location::RegisterLocation(EAX));
1870 locations->SetInAt(4, Location::RequiresRegister());
1871 }
1872
1873 // Force a byte register for the output.
1874 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001875 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001876 // Need temporary registers for card-marking, and possibly for
1877 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01001878 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001879 // Need a byte register for marking.
1880 locations->AddTemp(Location::RegisterLocation(ECX));
1881 }
1882}
1883
1884void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001885 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001886}
1887
1888void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001889 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001890}
1891
1892void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001893 // The only read barrier implementation supporting the
1894 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1895 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00001896 return;
1897 }
1898
Vladimir Markoca6fff82017-10-03 14:49:14 +01001899 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001900}
1901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001902static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001903 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04001904 LocationSummary* locations = invoke->GetLocations();
1905
1906 Register base = locations->InAt(1).AsRegister<Register>();
1907 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1908 Location out = locations->Out();
1909 DCHECK_EQ(out.AsRegister<Register>(), EAX);
1910
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001911 // The address of the field within the holding object.
1912 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
1913
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001915 // The only read barrier implementation supporting the
1916 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1917 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1918
1919 Location temp1_loc = locations->GetTemp(0);
1920 Register temp1 = temp1_loc.AsRegister<Register>();
1921 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
1922
Roland Levillain4d027112015-07-01 15:41:14 +01001923 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01001924 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01001925 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001926 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01001927
Roland Levillainb488b782015-10-22 11:38:49 +01001928 // Mark card for object assuming new value is stored.
1929 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001930 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
1931
1932 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1933 // Need to make sure the reference stored in the field is a to-space
1934 // one before attempting the CAS or the CAS could fail incorrectly.
1935 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1936 invoke,
1937 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
1938 base,
1939 field_addr,
1940 /* needs_null_check */ false,
1941 /* always_update_field */ true,
1942 &temp2);
1943 }
Roland Levillainb488b782015-10-22 11:38:49 +01001944
1945 bool base_equals_value = (base == value);
1946 if (kPoisonHeapReferences) {
1947 if (base_equals_value) {
1948 // If `base` and `value` are the same register location, move
1949 // `value` to a temporary register. This way, poisoning
1950 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001951 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01001952 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01001953 }
Roland Levillainb488b782015-10-22 11:38:49 +01001954
1955 // Check that the register allocator did not assign the location
1956 // of `expected` (EAX) to `value` nor to `base`, so that heap
1957 // poisoning (when enabled) works as intended below.
1958 // - If `value` were equal to `expected`, both references would
1959 // be poisoned twice, meaning they would not be poisoned at
1960 // all, as heap poisoning uses address negation.
1961 // - If `base` were equal to `expected`, poisoning `expected`
1962 // would invalidate `base`.
1963 DCHECK_NE(value, expected);
1964 DCHECK_NE(base, expected);
1965
1966 __ PoisonHeapReference(expected);
1967 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001968 }
1969
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001970 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001971
Roland Levillain0d5a2812015-11-13 10:07:31 +00001972 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01001973 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001974
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001975 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01001976 __ setb(kZero, out.AsRegister<Register>());
1977 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01001978
Roland Levillain391b8662015-12-18 11:43:38 +00001979 // If heap poisoning is enabled, we need to unpoison the values
1980 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01001981 if (kPoisonHeapReferences) {
1982 if (base_equals_value) {
1983 // `value` has been moved to a temporary register, no need to
1984 // unpoison it.
1985 } else {
1986 // Ensure `value` is different from `out`, so that unpoisoning
1987 // the former does not invalidate the latter.
1988 DCHECK_NE(value, out.AsRegister<Register>());
1989 __ UnpoisonHeapReference(value);
1990 }
1991 // Do not unpoison the reference contained in register
1992 // `expected`, as it is the same as register `out` (EAX).
1993 }
1994 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001995 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01001996 // Ensure the expected value is in EAX (required by the CMPXCHG
1997 // instruction).
1998 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001999 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002001 // Ensure the expected value is in EAX:EDX and that the new
2002 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2003 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2004 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2005 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2006 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002007 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002008 } else {
2009 LOG(FATAL) << "Unexpected CAS type " << type;
2010 }
2011
Roland Levillain0d5a2812015-11-13 10:07:31 +00002012 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2013 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002014
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002015 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002016 __ setb(kZero, out.AsRegister<Register>());
2017 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002018 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002019}
2020
2021void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002022 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002023}
2024
2025void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002026 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002027}
2028
2029void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002030 // The only read barrier implementation supporting the
2031 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2032 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002035}
2036
2037void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002038 LocationSummary* locations =
2039 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002040 locations->SetInAt(0, Location::RequiresRegister());
2041 locations->SetOut(Location::SameAsFirstInput());
2042 locations->AddTemp(Location::RequiresRegister());
2043}
2044
2045static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2046 X86Assembler* assembler) {
2047 Immediate imm_shift(shift);
2048 Immediate imm_mask(mask);
2049 __ movl(temp, reg);
2050 __ shrl(reg, imm_shift);
2051 __ andl(temp, imm_mask);
2052 __ andl(reg, imm_mask);
2053 __ shll(temp, imm_shift);
2054 __ orl(reg, temp);
2055}
2056
2057void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002058 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002059 LocationSummary* locations = invoke->GetLocations();
2060
2061 Register reg = locations->InAt(0).AsRegister<Register>();
2062 Register temp = locations->GetTemp(0).AsRegister<Register>();
2063
2064 /*
2065 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2066 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2067 * compared to generic luni implementation which has 5 rounds of swapping bits.
2068 * x = bswap x
2069 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2070 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2071 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2072 */
2073 __ bswapl(reg);
2074 SwapBits(reg, temp, 1, 0x55555555, assembler);
2075 SwapBits(reg, temp, 2, 0x33333333, assembler);
2076 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2077}
2078
2079void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002080 LocationSummary* locations =
2081 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002082 locations->SetInAt(0, Location::RequiresRegister());
2083 locations->SetOut(Location::SameAsFirstInput());
2084 locations->AddTemp(Location::RequiresRegister());
2085}
2086
2087void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002088 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002089 LocationSummary* locations = invoke->GetLocations();
2090
2091 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2092 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2093 Register temp = locations->GetTemp(0).AsRegister<Register>();
2094
2095 // We want to swap high/low, then bswap each one, and then do the same
2096 // as a 32 bit reverse.
2097 // Exchange high and low.
2098 __ movl(temp, reg_low);
2099 __ movl(reg_low, reg_high);
2100 __ movl(reg_high, temp);
2101
2102 // bit-reverse low
2103 __ bswapl(reg_low);
2104 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2105 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2106 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2107
2108 // bit-reverse high
2109 __ bswapl(reg_high);
2110 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2111 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2112 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2113}
2114
Aart Bikc39dac12016-01-21 08:59:48 -08002115static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002116 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002117 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2118 // Do nothing if there is no popcnt support. This results in generating
2119 // a call for the intrinsic rather than direct code.
2120 return;
2121 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002122 LocationSummary* locations =
2123 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002124 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002125 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002126 }
Aart Bik2a946072016-01-21 12:49:00 -08002127 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002128 locations->SetOut(Location::RequiresRegister());
2129}
2130
Aart Bika19616e2016-02-01 18:57:58 -08002131static void GenBitCount(X86Assembler* assembler,
2132 CodeGeneratorX86* codegen,
2133 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002134 LocationSummary* locations = invoke->GetLocations();
2135 Location src = locations->InAt(0);
2136 Register out = locations->Out().AsRegister<Register>();
2137
2138 if (invoke->InputAt(0)->IsConstant()) {
2139 // Evaluate this at compile time.
2140 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002141 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002142 ? POPCOUNT(static_cast<uint64_t>(value))
2143 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002144 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002145 return;
2146 }
2147
2148 // Handle the non-constant cases.
2149 if (!is_long) {
2150 if (src.IsRegister()) {
2151 __ popcntl(out, src.AsRegister<Register>());
2152 } else {
2153 DCHECK(src.IsStackSlot());
2154 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2155 }
Aart Bik2a946072016-01-21 12:49:00 -08002156 } else {
2157 // The 64-bit case needs to worry about two parts.
2158 Register temp = locations->GetTemp(0).AsRegister<Register>();
2159 if (src.IsRegisterPair()) {
2160 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2161 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2162 } else {
2163 DCHECK(src.IsDoubleStackSlot());
2164 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2165 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2166 }
2167 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002168 }
Aart Bikc39dac12016-01-21 08:59:48 -08002169}
2170
2171void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002172 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002173}
2174
2175void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002176 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002177}
2178
2179void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002181}
2182
2183void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002184 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002185}
2186
Vladimir Markoca6fff82017-10-03 14:49:14 +01002187static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2188 LocationSummary* locations =
2189 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002190 if (is_long) {
2191 locations->SetInAt(0, Location::RequiresRegister());
2192 } else {
2193 locations->SetInAt(0, Location::Any());
2194 }
2195 locations->SetOut(Location::RequiresRegister());
2196}
2197
Aart Bika19616e2016-02-01 18:57:58 -08002198static void GenLeadingZeros(X86Assembler* assembler,
2199 CodeGeneratorX86* codegen,
2200 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002201 LocationSummary* locations = invoke->GetLocations();
2202 Location src = locations->InAt(0);
2203 Register out = locations->Out().AsRegister<Register>();
2204
2205 if (invoke->InputAt(0)->IsConstant()) {
2206 // Evaluate this at compile time.
2207 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2208 if (value == 0) {
2209 value = is_long ? 64 : 32;
2210 } else {
2211 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2212 }
Aart Bika19616e2016-02-01 18:57:58 -08002213 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002214 return;
2215 }
2216
2217 // Handle the non-constant cases.
2218 if (!is_long) {
2219 if (src.IsRegister()) {
2220 __ bsrl(out, src.AsRegister<Register>());
2221 } else {
2222 DCHECK(src.IsStackSlot());
2223 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2224 }
2225
2226 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002227 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002228 __ j(kEqual, &all_zeroes);
2229
2230 // Correct the result from BSR to get the final CLZ result.
2231 __ xorl(out, Immediate(31));
2232 __ jmp(&done);
2233
2234 // Fix the zero case with the expected result.
2235 __ Bind(&all_zeroes);
2236 __ movl(out, Immediate(32));
2237
2238 __ Bind(&done);
2239 return;
2240 }
2241
2242 // 64 bit case needs to worry about both parts of the register.
2243 DCHECK(src.IsRegisterPair());
2244 Register src_lo = src.AsRegisterPairLow<Register>();
2245 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002246 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002247
2248 // Is the high word zero?
2249 __ testl(src_hi, src_hi);
2250 __ j(kEqual, &handle_low);
2251
2252 // High word is not zero. We know that the BSR result is defined in this case.
2253 __ bsrl(out, src_hi);
2254
2255 // Correct the result from BSR to get the final CLZ result.
2256 __ xorl(out, Immediate(31));
2257 __ jmp(&done);
2258
2259 // High word was zero. We have to compute the low word count and add 32.
2260 __ Bind(&handle_low);
2261 __ bsrl(out, src_lo);
2262 __ j(kEqual, &all_zeroes);
2263
2264 // We had a valid result. Use an XOR to both correct the result and add 32.
2265 __ xorl(out, Immediate(63));
2266 __ jmp(&done);
2267
2268 // All zero case.
2269 __ Bind(&all_zeroes);
2270 __ movl(out, Immediate(64));
2271
2272 __ Bind(&done);
2273}
2274
2275void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002276 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002277}
2278
2279void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002280 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002281}
2282
2283void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002284 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002285}
2286
2287void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002288 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002289}
2290
Vladimir Markoca6fff82017-10-03 14:49:14 +01002291static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2292 LocationSummary* locations =
2293 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002294 if (is_long) {
2295 locations->SetInAt(0, Location::RequiresRegister());
2296 } else {
2297 locations->SetInAt(0, Location::Any());
2298 }
2299 locations->SetOut(Location::RequiresRegister());
2300}
2301
Aart Bika19616e2016-02-01 18:57:58 -08002302static void GenTrailingZeros(X86Assembler* assembler,
2303 CodeGeneratorX86* codegen,
2304 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002305 LocationSummary* locations = invoke->GetLocations();
2306 Location src = locations->InAt(0);
2307 Register out = locations->Out().AsRegister<Register>();
2308
2309 if (invoke->InputAt(0)->IsConstant()) {
2310 // Evaluate this at compile time.
2311 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2312 if (value == 0) {
2313 value = is_long ? 64 : 32;
2314 } else {
2315 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2316 }
Aart Bika19616e2016-02-01 18:57:58 -08002317 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002318 return;
2319 }
2320
2321 // Handle the non-constant cases.
2322 if (!is_long) {
2323 if (src.IsRegister()) {
2324 __ bsfl(out, src.AsRegister<Register>());
2325 } else {
2326 DCHECK(src.IsStackSlot());
2327 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2328 }
2329
2330 // BSF sets ZF if the input was zero, and the output is undefined.
2331 NearLabel done;
2332 __ j(kNotEqual, &done);
2333
2334 // Fix the zero case with the expected result.
2335 __ movl(out, Immediate(32));
2336
2337 __ Bind(&done);
2338 return;
2339 }
2340
2341 // 64 bit case needs to worry about both parts of the register.
2342 DCHECK(src.IsRegisterPair());
2343 Register src_lo = src.AsRegisterPairLow<Register>();
2344 Register src_hi = src.AsRegisterPairHigh<Register>();
2345 NearLabel done, all_zeroes;
2346
2347 // If the low word is zero, then ZF will be set. If not, we have the answer.
2348 __ bsfl(out, src_lo);
2349 __ j(kNotEqual, &done);
2350
2351 // Low word was zero. We have to compute the high word count and add 32.
2352 __ bsfl(out, src_hi);
2353 __ j(kEqual, &all_zeroes);
2354
2355 // We had a valid result. Add 32 to account for the low word being zero.
2356 __ addl(out, Immediate(32));
2357 __ jmp(&done);
2358
2359 // All zero case.
2360 __ Bind(&all_zeroes);
2361 __ movl(out, Immediate(64));
2362
2363 __ Bind(&done);
2364}
2365
2366void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002367 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002368}
2369
2370void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002371 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002372}
2373
2374void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002375 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002376}
2377
2378void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002379 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002380}
2381
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002382static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2383 return instruction->InputAt(input0) == instruction->InputAt(input1);
2384}
2385
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002386// Compute base address for the System.arraycopy intrinsic in `base`.
2387static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002389 const Register& array,
2390 const Location& pos,
2391 const Register& base) {
2392 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002394 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002395 DCHECK_EQ(type, DataType::Type::kReference);
2396 const int32_t element_size = DataType::Size(type);
2397 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002398 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2399
2400 if (pos.IsConstant()) {
2401 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2402 __ leal(base, Address(array, element_size * constant + data_offset));
2403 } else {
2404 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2405 }
2406}
2407
2408// Compute end source address for the System.arraycopy intrinsic in `end`.
2409static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002411 const Location& copy_length,
2412 const Register& base,
2413 const Register& end) {
2414 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002416 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002417 DCHECK_EQ(type, DataType::Type::kReference);
2418 const int32_t element_size = DataType::Size(type);
2419 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002420
2421 if (copy_length.IsConstant()) {
2422 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2423 __ leal(end, Address(base, element_size * constant));
2424 } else {
2425 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2426 }
2427}
2428
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002429void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002430 // The only read barrier implementation supporting the
2431 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2432 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002433 return;
2434 }
2435
2436 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2437 if (invoke->GetLocations() != nullptr) {
2438 // Need a byte register for marking.
2439 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2440
2441 static constexpr size_t kSrc = 0;
2442 static constexpr size_t kSrcPos = 1;
2443 static constexpr size_t kDest = 2;
2444 static constexpr size_t kDestPos = 3;
2445 static constexpr size_t kLength = 4;
2446
2447 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2448 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2449 !invoke->InputAt(kLength)->IsIntConstant()) {
2450 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2451 !IsSameInput(invoke, kSrcPos, kLength) &&
2452 !IsSameInput(invoke, kDestPos, kLength) &&
2453 !IsSameInput(invoke, kSrc, kDest)) {
2454 // Not enough registers, make the length also take a stack slot.
2455 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2456 }
2457 }
2458 }
2459}
2460
2461void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002462 // The only read barrier implementation supporting the
2463 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2464 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002465
2466 X86Assembler* assembler = GetAssembler();
2467 LocationSummary* locations = invoke->GetLocations();
2468
2469 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2470 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2471 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2472 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002473 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002474
2475 Register src = locations->InAt(0).AsRegister<Register>();
2476 Location src_pos = locations->InAt(1);
2477 Register dest = locations->InAt(2).AsRegister<Register>();
2478 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002479 Location length_arg = locations->InAt(4);
2480 Location length = length_arg;
2481 Location temp1_loc = locations->GetTemp(0);
2482 Register temp1 = temp1_loc.AsRegister<Register>();
2483 Location temp2_loc = locations->GetTemp(1);
2484 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002485
Vladimir Marko174b2e22017-10-12 13:34:49 +01002486 SlowPathCode* intrinsic_slow_path =
2487 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002488 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002489
2490 NearLabel conditions_on_positions_validated;
2491 SystemArrayCopyOptimizations optimizations(invoke);
2492
2493 // If source and destination are the same, we go to slow path if we need to do
2494 // forward copying.
2495 if (src_pos.IsConstant()) {
2496 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2497 if (dest_pos.IsConstant()) {
2498 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2499 if (optimizations.GetDestinationIsSource()) {
2500 // Checked when building locations.
2501 DCHECK_GE(src_pos_constant, dest_pos_constant);
2502 } else if (src_pos_constant < dest_pos_constant) {
2503 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002504 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002505 }
2506 } else {
2507 if (!optimizations.GetDestinationIsSource()) {
2508 __ cmpl(src, dest);
2509 __ j(kNotEqual, &conditions_on_positions_validated);
2510 }
2511 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002512 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002513 }
2514 } else {
2515 if (!optimizations.GetDestinationIsSource()) {
2516 __ cmpl(src, dest);
2517 __ j(kNotEqual, &conditions_on_positions_validated);
2518 }
2519 if (dest_pos.IsConstant()) {
2520 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2521 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002522 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002523 } else {
2524 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002525 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002526 }
2527 }
2528
2529 __ Bind(&conditions_on_positions_validated);
2530
2531 if (!optimizations.GetSourceIsNotNull()) {
2532 // Bail out if the source is null.
2533 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002534 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002535 }
2536
2537 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2538 // Bail out if the destination is null.
2539 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002540 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002541 }
2542
Roland Levillain0b671c02016-08-19 12:02:34 +01002543 Location temp3_loc = locations->GetTemp(2);
2544 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002545 if (length.IsStackSlot()) {
2546 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2547 length = Location::RegisterLocation(temp3);
2548 }
2549
2550 // If the length is negative, bail out.
2551 // We have already checked in the LocationsBuilder for the constant case.
2552 if (!length.IsConstant() &&
2553 !optimizations.GetCountIsSourceLength() &&
2554 !optimizations.GetCountIsDestinationLength()) {
2555 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002556 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002557 }
2558
2559 // Validity checks: source.
2560 CheckPosition(assembler,
2561 src_pos,
2562 src,
2563 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002564 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002565 temp1,
2566 optimizations.GetCountIsSourceLength());
2567
2568 // Validity checks: dest.
2569 CheckPosition(assembler,
2570 dest_pos,
2571 dest,
2572 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002573 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002574 temp1,
2575 optimizations.GetCountIsDestinationLength());
2576
2577 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2578 // Check whether all elements of the source array are assignable to the component
2579 // type of the destination array. We do two checks: the classes are the same,
2580 // or the destination is Object[]. If none of these checks succeed, we go to the
2581 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01002582
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002583 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002584 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2585 // /* HeapReference<Class> */ temp1 = src->klass_
2586 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002587 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002588 // Bail out if the source is not a non primitive array.
2589 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2590 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002591 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002592 __ testl(temp1, temp1);
2593 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2594 // If heap poisoning is enabled, `temp1` has been unpoisoned
2595 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2596 } else {
2597 // /* HeapReference<Class> */ temp1 = src->klass_
2598 __ movl(temp1, Address(src, class_offset));
2599 __ MaybeUnpoisonHeapReference(temp1);
2600 // Bail out if the source is not a non primitive array.
2601 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2602 __ movl(temp1, Address(temp1, component_offset));
2603 __ testl(temp1, temp1);
2604 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2605 __ MaybeUnpoisonHeapReference(temp1);
2606 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002607 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002608 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002609 }
2610
Roland Levillain0b671c02016-08-19 12:02:34 +01002611 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2612 if (length.Equals(Location::RegisterLocation(temp3))) {
2613 // When Baker read barriers are enabled, register `temp3`,
2614 // which in the present case contains the `length` parameter,
2615 // will be overwritten below. Make the `length` location
2616 // reference the original stack location; it will be moved
2617 // back to `temp3` later if necessary.
2618 DCHECK(length_arg.IsStackSlot());
2619 length = length_arg;
2620 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002621
Roland Levillain0b671c02016-08-19 12:02:34 +01002622 // /* HeapReference<Class> */ temp1 = dest->klass_
2623 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002624 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002625
Roland Levillain0b671c02016-08-19 12:02:34 +01002626 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2627 // Bail out if the destination is not a non primitive array.
2628 //
2629 // Register `temp1` is not trashed by the read barrier emitted
2630 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2631 // method produces a call to a ReadBarrierMarkRegX entry point,
2632 // which saves all potentially live registers, including
2633 // temporaries such a `temp1`.
2634 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2635 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002636 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002637 __ testl(temp2, temp2);
2638 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2639 // If heap poisoning is enabled, `temp2` has been unpoisoned
2640 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2641 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2642 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2643 }
2644
2645 // For the same reason given earlier, `temp1` is not trashed by the
2646 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2647 // /* HeapReference<Class> */ temp2 = src->klass_
2648 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002649 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002650 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2651 __ cmpl(temp1, temp2);
2652
2653 if (optimizations.GetDestinationIsTypedObjectArray()) {
2654 NearLabel do_copy;
2655 __ j(kEqual, &do_copy);
2656 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2657 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002658 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002659 // We do not need to emit a read barrier for the following
2660 // heap reference load, as `temp1` is only used in a
2661 // comparison with null below, and this reference is not
2662 // kept afterwards.
2663 __ cmpl(Address(temp1, super_offset), Immediate(0));
2664 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2665 __ Bind(&do_copy);
2666 } else {
2667 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2668 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002669 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002670 // Non read barrier code.
2671
2672 // /* HeapReference<Class> */ temp1 = dest->klass_
2673 __ movl(temp1, Address(dest, class_offset));
2674 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2675 __ MaybeUnpoisonHeapReference(temp1);
2676 // Bail out if the destination is not a non primitive array.
2677 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2678 __ movl(temp2, Address(temp1, component_offset));
2679 __ testl(temp2, temp2);
2680 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2681 __ MaybeUnpoisonHeapReference(temp2);
2682 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2683 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2684 // Re-poison the heap reference to make the compare instruction below
2685 // compare two poisoned references.
2686 __ PoisonHeapReference(temp1);
2687 }
2688
2689 // Note: if heap poisoning is on, we are comparing two poisoned references here.
2690 __ cmpl(temp1, Address(src, class_offset));
2691
2692 if (optimizations.GetDestinationIsTypedObjectArray()) {
2693 NearLabel do_copy;
2694 __ j(kEqual, &do_copy);
2695 __ MaybeUnpoisonHeapReference(temp1);
2696 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2697 __ movl(temp1, Address(temp1, component_offset));
2698 __ MaybeUnpoisonHeapReference(temp1);
2699 __ cmpl(Address(temp1, super_offset), Immediate(0));
2700 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2701 __ Bind(&do_copy);
2702 } else {
2703 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2704 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002705 }
2706 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2707 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2708 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002709 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2710 // /* HeapReference<Class> */ temp1 = src->klass_
2711 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002712 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002713 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2714 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002715 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002716 __ testl(temp1, temp1);
2717 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2718 // If heap poisoning is enabled, `temp1` has been unpoisoned
2719 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2720 } else {
2721 // /* HeapReference<Class> */ temp1 = src->klass_
2722 __ movl(temp1, Address(src, class_offset));
2723 __ MaybeUnpoisonHeapReference(temp1);
2724 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2725 __ movl(temp1, Address(temp1, component_offset));
2726 __ testl(temp1, temp1);
2727 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2728 __ MaybeUnpoisonHeapReference(temp1);
2729 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002730 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002731 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002732 }
2733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 const DataType::Type type = DataType::Type::kReference;
2735 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002736
Roland Levillain0b671c02016-08-19 12:02:34 +01002737 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002738 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002739
Roland Levillain0b671c02016-08-19 12:02:34 +01002740 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2741 // If it is needed (in the case of the fast-path loop), the base
2742 // destination address is computed later, as `temp2` is used for
2743 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002744
Roland Levillain0b671c02016-08-19 12:02:34 +01002745 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002746 if (length.IsStackSlot()) {
2747 // Location `length` is again pointing at a stack slot, as
2748 // register `temp3` (which was containing the length parameter
2749 // earlier) has been overwritten; restore it now
2750 DCHECK(length.Equals(length_arg));
2751 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2752 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002753 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002754 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002755
Roland Levillain0b671c02016-08-19 12:02:34 +01002756 // SystemArrayCopy implementation for Baker read barriers (see
2757 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
2758 //
2759 // if (src_ptr != end_ptr) {
2760 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2761 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002762 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01002763 // if (is_gray) {
2764 // // Slow-path copy.
2765 // for (size_t i = 0; i != length; ++i) {
2766 // dest_array[dest_pos + i] =
2767 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
2768 // }
2769 // } else {
2770 // // Fast-path copy.
2771 // do {
2772 // *dest_ptr++ = *src_ptr++;
2773 // } while (src_ptr != end_ptr)
2774 // }
2775 // }
2776
2777 NearLabel loop, done;
2778
2779 // Don't enter copy loop if `length == 0`.
2780 __ cmpl(temp1, temp3);
2781 __ j(kEqual, &done);
2782
Vladimir Marko953437b2016-08-24 08:30:46 +00002783 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002784 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
2785 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00002786 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
2787 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
2788 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
2789
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002790 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00002791 // goto slow_path;
2792 // At this point, just do the "if" and make sure that flags are preserved until the branch.
2793 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01002794
2795 // Load fence to prevent load-load reordering.
2796 // Note that this is a no-op, thanks to the x86 memory model.
2797 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2798
2799 // Slow path used to copy array when `src` is gray.
2800 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002801 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002802 codegen_->AddSlowPath(read_barrier_slow_path);
2803
Vladimir Marko953437b2016-08-24 08:30:46 +00002804 // We have done the "if" of the gray bit check above, now branch based on the flags.
2805 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01002806
2807 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002808 // Compute the base destination address in `temp2`.
2809 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002810 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2811 // poison/unpoison.
2812 __ Bind(&loop);
2813 __ pushl(Address(temp1, 0));
2814 __ cfi().AdjustCFAOffset(4);
2815 __ popl(Address(temp2, 0));
2816 __ cfi().AdjustCFAOffset(-4);
2817 __ addl(temp1, Immediate(element_size));
2818 __ addl(temp2, Immediate(element_size));
2819 __ cmpl(temp1, temp3);
2820 __ j(kNotEqual, &loop);
2821
2822 __ Bind(read_barrier_slow_path->GetExitLabel());
2823 __ Bind(&done);
2824 } else {
2825 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01002826 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002827 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002828 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002829 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002830 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2831 // poison/unpoison.
2832 NearLabel loop, done;
2833 __ cmpl(temp1, temp3);
2834 __ j(kEqual, &done);
2835 __ Bind(&loop);
2836 __ pushl(Address(temp1, 0));
2837 __ cfi().AdjustCFAOffset(4);
2838 __ popl(Address(temp2, 0));
2839 __ cfi().AdjustCFAOffset(-4);
2840 __ addl(temp1, Immediate(element_size));
2841 __ addl(temp2, Immediate(element_size));
2842 __ cmpl(temp1, temp3);
2843 __ j(kNotEqual, &loop);
2844 __ Bind(&done);
2845 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002846
2847 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002848 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002849
Roland Levillain0b671c02016-08-19 12:02:34 +01002850 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002851}
2852
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002853void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
2854 InvokeRuntimeCallingConvention calling_convention;
2855 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2856 invoke,
2857 codegen_,
2858 Location::RegisterLocation(EAX),
2859 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2860}
2861
2862void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
2863 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
2864 LocationSummary* locations = invoke->GetLocations();
2865 X86Assembler* assembler = GetAssembler();
2866
2867 Register out = locations->Out().AsRegister<Register>();
2868 InvokeRuntimeCallingConvention calling_convention;
2869 if (invoke->InputAt(0)->IsConstant()) {
2870 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
2871 if (value >= info.low && value <= info.high) {
2872 // Just embed the j.l.Integer in the code.
2873 ScopedObjectAccess soa(Thread::Current());
2874 mirror::Object* boxed = info.cache->Get(value + (-info.low));
2875 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
2876 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
2877 __ movl(out, Immediate(address));
2878 } else {
2879 // Allocate and initialize a new j.l.Integer.
2880 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2881 // JIT object table.
2882 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2883 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
2884 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2885 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2886 __ movl(Address(out, info.value_offset), Immediate(value));
2887 }
2888 } else {
2889 Register in = locations->InAt(0).AsRegister<Register>();
2890 // Check bounds of our cache.
2891 __ leal(out, Address(in, -info.low));
2892 __ cmpl(out, Immediate(info.high - info.low + 1));
2893 NearLabel allocate, done;
2894 __ j(kAboveEqual, &allocate);
2895 // If the value is within the bounds, load the j.l.Integer directly from the array.
2896 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
2897 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
2898 __ movl(out, Address(out, TIMES_4, data_offset + address));
2899 __ MaybeUnpoisonHeapReference(out);
2900 __ jmp(&done);
2901 __ Bind(&allocate);
2902 // Otherwise allocate and initialize a new j.l.Integer.
2903 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
2904 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
2905 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
2906 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
2907 __ movl(Address(out, info.value_offset), in);
2908 __ Bind(&done);
2909 }
2910}
2911
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002912void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002913 LocationSummary* locations =
2914 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002915 locations->SetOut(Location::RequiresRegister());
2916}
2917
2918void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
2919 X86Assembler* assembler = GetAssembler();
2920 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
2921 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
2922 NearLabel done;
2923 __ fs()->movl(out, address);
2924 __ testl(out, out);
2925 __ j(kEqual, &done);
2926 __ fs()->movl(address, Immediate(0));
2927 codegen_->MemoryFence();
2928 __ Bind(&done);
2929}
2930
Hans Boehmc7b28de2018-03-09 17:05:28 -08002931void IntrinsicLocationsBuilderX86::VisitReachabilityFence(HInvoke* invoke) {
2932 LocationSummary* locations =
2933 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
2934 locations->SetInAt(0, Location::Any());
2935}
2936
2937void IntrinsicCodeGeneratorX86::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002938
Aart Bik2f9fcc92016-03-01 15:16:54 -08002939UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00002940UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002941UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
2942UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
2943UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
2944UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
2945UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
2946UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
Mark Mendell09ed1a32015-03-25 08:30:06 -04002947
Aart Bikff7d89c2016-11-07 08:49:28 -08002948UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
2949UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002950UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
2951UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
2952UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
2953UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
2954UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
2955UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002956
Aart Bik0e54c012016-03-04 12:08:31 -08002957// 1.8.
2958UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
2959UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
2960UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
2961UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
2962UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002963
Aart Bik2f9fcc92016-03-01 15:16:54 -08002964UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01002965
2966#undef __
2967
Mark Mendell09ed1a32015-03-25 08:30:06 -04002968} // namespace x86
2969} // namespace art