blob: 6dd4681847049f5e01e58c47daae71c227568495 [file] [log] [blame]
Mark Mendell09ed1a32015-03-25 08:30:06 -04001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "intrinsics_x86.h"
18
Andreas Gampe21030dd2015-05-07 14:46:15 -070019#include <limits>
20
Mark Mendellfb8d2792015-03-31 22:16:59 -040021#include "arch/x86/instruction_set_features_x86.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method.h"
Mark Mendelld5897672015-08-12 21:16:41 -040023#include "base/bit_utils.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040024#include "code_generator_x86.h"
25#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070026#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040027#include "intrinsics.h"
Andreas Gampe85b62f22015-09-09 13:15:38 -070028#include "intrinsics_utils.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080029#include "lock_word.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040030#include "mirror/array-inl.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070031#include "mirror/object_array-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080032#include "mirror/reference.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040033#include "mirror/string.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080034#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040036#include "utils/x86/assembler_x86.h"
37#include "utils/x86/constants_x86.h"
38
39namespace art {
40
41namespace x86 {
42
Mark Mendellfb8d2792015-03-31 22:16:59 -040043IntrinsicLocationsBuilderX86::IntrinsicLocationsBuilderX86(CodeGeneratorX86* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +010044 : allocator_(codegen->GetGraph()->GetAllocator()),
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000045 codegen_(codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -040046}
47
48
Mark Mendell09ed1a32015-03-25 08:30:06 -040049X86Assembler* IntrinsicCodeGeneratorX86::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010050 return down_cast<X86Assembler*>(codegen_->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -040051}
52
53ArenaAllocator* IntrinsicCodeGeneratorX86::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010054 return codegen_->GetGraph()->GetAllocator();
Mark Mendell09ed1a32015-03-25 08:30:06 -040055}
56
57bool IntrinsicLocationsBuilderX86::TryDispatch(HInvoke* invoke) {
58 Dispatch(invoke);
59 LocationSummary* res = invoke->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +000060 if (res == nullptr) {
61 return false;
62 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000063 return res->Intrinsified();
Mark Mendell09ed1a32015-03-25 08:30:06 -040064}
65
Roland Levillainec525fc2015-04-28 15:50:20 +010066static void MoveArguments(HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010067 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010068 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Mark Mendell09ed1a32015-03-25 08:30:06 -040069}
70
Andreas Gampe85b62f22015-09-09 13:15:38 -070071using IntrinsicSlowPathX86 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86>;
Mark Mendell09ed1a32015-03-25 08:30:06 -040072
Roland Levillain0b671c02016-08-19 12:02:34 +010073// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
74#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
75
76// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
77class ReadBarrierSystemArrayCopySlowPathX86 : public SlowPathCode {
78 public:
79 explicit ReadBarrierSystemArrayCopySlowPathX86(HInstruction* instruction)
80 : SlowPathCode(instruction) {
81 DCHECK(kEmitCompilerReadBarrier);
82 DCHECK(kUseBakerReadBarrier);
83 }
84
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010085 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0b671c02016-08-19 12:02:34 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
87 LocationSummary* locations = instruction_->GetLocations();
88 DCHECK(locations->CanCall());
89 DCHECK(instruction_->IsInvokeStaticOrDirect())
90 << "Unexpected instruction in read barrier arraycopy slow path: "
91 << instruction_->DebugName();
92 DCHECK(instruction_->GetLocations()->Intrinsified());
93 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
94
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +010096 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
97
98 Register src = locations->InAt(0).AsRegister<Register>();
99 Location src_pos = locations->InAt(1);
100 Register dest = locations->InAt(2).AsRegister<Register>();
101 Location dest_pos = locations->InAt(3);
102 Location length = locations->InAt(4);
103 Location temp1_loc = locations->GetTemp(0);
104 Register temp1 = temp1_loc.AsRegister<Register>();
105 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
106 Register temp3 = locations->GetTemp(2).AsRegister<Register>();
107
108 __ Bind(GetEntryLabel());
109 // In this code path, registers `temp1`, `temp2`, and `temp3`
110 // (resp.) are not used for the base source address, the base
111 // destination address, and the end source address (resp.), as in
112 // other SystemArrayCopy intrinsic code paths. Instead they are
113 // (resp.) used for:
114 // - the loop index (`i`);
115 // - the source index (`src_index`) and the loaded (source)
116 // reference (`value`); and
117 // - the destination index (`dest_index`).
118
119 // i = 0
120 __ xorl(temp1, temp1);
121 NearLabel loop;
122 __ Bind(&loop);
123 // value = src_array[i + src_pos]
124 if (src_pos.IsConstant()) {
125 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
126 int32_t adjusted_offset = offset + constant * element_size;
127 __ movl(temp2, Address(src, temp1, ScaleFactor::TIMES_4, adjusted_offset));
128 } else {
129 __ leal(temp2, Address(src_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0));
130 __ movl(temp2, Address(src, temp2, ScaleFactor::TIMES_4, offset));
131 }
132 __ MaybeUnpoisonHeapReference(temp2);
133 // TODO: Inline the mark bit check before calling the runtime?
134 // value = ReadBarrier::Mark(value)
135 // No need to save live registers; it's taken care of by the
136 // entrypoint. Also, there is no need to update the stack mask,
137 // as this runtime call will not trigger a garbage collection.
138 // (See ReadBarrierMarkSlowPathX86::EmitNativeCode for more
139 // explanations.)
140 DCHECK_NE(temp2, ESP);
141 DCHECK(0 <= temp2 && temp2 < kNumberOfCpuRegisters) << temp2;
Roland Levillain97c46462017-05-11 14:04:03 +0100142 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +0100143 // This runtime call does not require a stack map.
144 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
145 __ MaybePoisonHeapReference(temp2);
146 // dest_array[i + dest_pos] = value
147 if (dest_pos.IsConstant()) {
148 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
149 int32_t adjusted_offset = offset + constant * element_size;
150 __ movl(Address(dest, temp1, ScaleFactor::TIMES_4, adjusted_offset), temp2);
151 } else {
152 __ leal(temp3, Address(dest_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0));
153 __ movl(Address(dest, temp3, ScaleFactor::TIMES_4, offset), temp2);
154 }
155 // ++i
156 __ addl(temp1, Immediate(1));
157 // if (i != length) goto loop
158 x86_codegen->GenerateIntCompare(temp1_loc, length);
159 __ j(kNotEqual, &loop);
160 __ jmp(GetExitLabel());
161 }
162
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100163 const char* GetDescription() const override { return "ReadBarrierSystemArrayCopySlowPathX86"; }
Roland Levillain0b671c02016-08-19 12:02:34 +0100164
165 private:
166 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86);
167};
168
169#undef __
170
Mark Mendell09ed1a32015-03-25 08:30:06 -0400171#define __ assembler->
172
Vladimir Markoca6fff82017-10-03 14:49:14 +0100173static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) {
174 LocationSummary* locations =
175 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400176 locations->SetInAt(0, Location::RequiresFpuRegister());
177 locations->SetOut(Location::RequiresRegister());
178 if (is64bit) {
179 locations->AddTemp(Location::RequiresFpuRegister());
180 }
181}
182
Vladimir Markoca6fff82017-10-03 14:49:14 +0100183static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) {
184 LocationSummary* locations =
185 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400186 locations->SetInAt(0, Location::RequiresRegister());
187 locations->SetOut(Location::RequiresFpuRegister());
188 if (is64bit) {
189 locations->AddTemp(Location::RequiresFpuRegister());
190 locations->AddTemp(Location::RequiresFpuRegister());
191 }
192}
193
194static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
195 Location input = locations->InAt(0);
196 Location output = locations->Out();
197 if (is64bit) {
198 // Need to use the temporary.
199 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
200 __ movsd(temp, input.AsFpuRegister<XmmRegister>());
201 __ movd(output.AsRegisterPairLow<Register>(), temp);
202 __ psrlq(temp, Immediate(32));
203 __ movd(output.AsRegisterPairHigh<Register>(), temp);
204 } else {
205 __ movd(output.AsRegister<Register>(), input.AsFpuRegister<XmmRegister>());
206 }
207}
208
209static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
210 Location input = locations->InAt(0);
211 Location output = locations->Out();
212 if (is64bit) {
213 // Need to use the temporary.
214 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
215 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
216 __ movd(temp1, input.AsRegisterPairLow<Register>());
217 __ movd(temp2, input.AsRegisterPairHigh<Register>());
218 __ punpckldq(temp1, temp2);
219 __ movsd(output.AsFpuRegister<XmmRegister>(), temp1);
220 } else {
221 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<Register>());
222 }
223}
224
225void IntrinsicLocationsBuilderX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100226 CreateFPToIntLocations(allocator_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400227}
228void IntrinsicLocationsBuilderX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100229 CreateIntToFPLocations(allocator_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400230}
231
232void IntrinsicCodeGeneratorX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000233 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400234}
235void IntrinsicCodeGeneratorX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000236 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400237}
238
239void IntrinsicLocationsBuilderX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100240 CreateFPToIntLocations(allocator_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400241}
242void IntrinsicLocationsBuilderX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100243 CreateIntToFPLocations(allocator_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400244}
245
246void IntrinsicCodeGeneratorX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000247 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400248}
249void IntrinsicCodeGeneratorX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000250 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400251}
252
Vladimir Markoca6fff82017-10-03 14:49:14 +0100253static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
254 LocationSummary* locations =
255 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400256 locations->SetInAt(0, Location::RequiresRegister());
257 locations->SetOut(Location::SameAsFirstInput());
258}
259
Vladimir Markoca6fff82017-10-03 14:49:14 +0100260static void CreateLongToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
261 LocationSummary* locations =
262 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400263 locations->SetInAt(0, Location::RequiresRegister());
264 locations->SetOut(Location::RequiresRegister());
265}
266
Vladimir Markoca6fff82017-10-03 14:49:14 +0100267static void CreateLongToLongLocations(ArenaAllocator* allocator, HInvoke* invoke) {
268 LocationSummary* locations =
269 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400270 locations->SetInAt(0, Location::RequiresRegister());
271 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
272}
273
274static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100275 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -0400276 X86Assembler* assembler) {
277 Register out = locations->Out().AsRegister<Register>();
278
279 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100280 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -0400281 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
282 __ bswapl(out);
283 __ sarl(out, Immediate(16));
284 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100285 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -0400286 __ bswapl(out);
287 break;
288 default:
289 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
290 UNREACHABLE();
291 }
292}
293
294void IntrinsicLocationsBuilderX86::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100295 CreateIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400296}
297
298void IntrinsicCodeGeneratorX86::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100299 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400300}
301
Mark Mendell58d25fd2015-04-03 14:52:31 -0400302void IntrinsicLocationsBuilderX86::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100303 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -0400304}
305
306void IntrinsicCodeGeneratorX86::VisitLongReverseBytes(HInvoke* invoke) {
307 LocationSummary* locations = invoke->GetLocations();
308 Location input = locations->InAt(0);
309 Register input_lo = input.AsRegisterPairLow<Register>();
310 Register input_hi = input.AsRegisterPairHigh<Register>();
311 Location output = locations->Out();
312 Register output_lo = output.AsRegisterPairLow<Register>();
313 Register output_hi = output.AsRegisterPairHigh<Register>();
314
315 X86Assembler* assembler = GetAssembler();
316 // Assign the inputs to the outputs, mixing low/high.
317 __ movl(output_lo, input_hi);
318 __ movl(output_hi, input_lo);
319 __ bswapl(output_lo);
320 __ bswapl(output_hi);
321}
322
Mark Mendell09ed1a32015-03-25 08:30:06 -0400323void IntrinsicLocationsBuilderX86::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100324 CreateIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400325}
326
327void IntrinsicCodeGeneratorX86::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100328 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400329}
330
Vladimir Markoca6fff82017-10-03 14:49:14 +0100331static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
332 LocationSummary* locations =
333 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400334 locations->SetInAt(0, Location::RequiresFpuRegister());
335 locations->SetOut(Location::RequiresFpuRegister());
336}
337
338void IntrinsicLocationsBuilderX86::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100339 CreateFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400340}
341
342void IntrinsicCodeGeneratorX86::VisitMathSqrt(HInvoke* invoke) {
343 LocationSummary* locations = invoke->GetLocations();
344 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
345 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
346
347 GetAssembler()->sqrtsd(out, in);
348}
349
Mark Mendellfb8d2792015-03-31 22:16:59 -0400350static void InvokeOutOfLineIntrinsic(CodeGeneratorX86* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100351 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400352
353 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100354 codegen->GenerateStaticOrDirectCall(invoke->AsInvokeStaticOrDirect(),
355 Location::RegisterLocation(EAX));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400356
357 // Copy the result back to the expected output.
358 Location out = invoke->GetLocations()->Out();
359 if (out.IsValid()) {
360 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700361 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400362 }
363}
364
Vladimir Markoca6fff82017-10-03 14:49:14 +0100365static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator,
366 HInvoke* invoke,
367 CodeGeneratorX86* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400368 // Do we have instruction support?
369 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100370 CreateFPToFPLocations(allocator, invoke);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400371 return;
372 }
373
374 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100375 LocationSummary* locations =
376 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400377 InvokeRuntimeCallingConvention calling_convention;
378 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
379 locations->SetOut(Location::FpuRegisterLocation(XMM0));
380 // Needs to be EAX for the invoke.
381 locations->AddTemp(Location::RegisterLocation(EAX));
382}
383
384static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86* codegen,
385 HInvoke* invoke,
386 X86Assembler* assembler,
387 int round_mode) {
388 LocationSummary* locations = invoke->GetLocations();
389 if (locations->WillCall()) {
390 InvokeOutOfLineIntrinsic(codegen, invoke);
391 } else {
392 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
393 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
394 __ roundsd(out, in, Immediate(round_mode));
395 }
396}
397
398void IntrinsicLocationsBuilderX86::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100399 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400400}
401
402void IntrinsicCodeGeneratorX86::VisitMathCeil(HInvoke* invoke) {
403 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
404}
405
406void IntrinsicLocationsBuilderX86::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100407 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400408}
409
410void IntrinsicCodeGeneratorX86::VisitMathFloor(HInvoke* invoke) {
411 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
412}
413
414void IntrinsicLocationsBuilderX86::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100415 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400416}
417
418void IntrinsicCodeGeneratorX86::VisitMathRint(HInvoke* invoke) {
419 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
420}
421
Mark Mendellfb8d2792015-03-31 22:16:59 -0400422void IntrinsicLocationsBuilderX86::VisitMathRoundFloat(HInvoke* invoke) {
423 // Do we have instruction support?
424 if (codegen_->GetInstructionSetFeatures().HasSSE4_1()) {
Aart Bik2c9f4952016-08-01 16:52:27 -0700425 HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect();
426 DCHECK(static_or_direct != nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100427 LocationSummary* locations =
428 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400429 locations->SetInAt(0, Location::RequiresFpuRegister());
Aart Bik2c9f4952016-08-01 16:52:27 -0700430 if (static_or_direct->HasSpecialInput() &&
431 invoke->InputAt(
432 static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) {
433 locations->SetInAt(1, Location::RequiresRegister());
434 }
Nicolas Geoffrayd9b92402015-04-21 10:02:22 +0100435 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400436 locations->AddTemp(Location::RequiresFpuRegister());
437 locations->AddTemp(Location::RequiresFpuRegister());
438 return;
439 }
440
441 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100442 LocationSummary* locations =
443 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400444 InvokeRuntimeCallingConvention calling_convention;
445 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
446 locations->SetOut(Location::RegisterLocation(EAX));
447 // Needs to be EAX for the invoke.
448 locations->AddTemp(Location::RegisterLocation(EAX));
449}
450
451void IntrinsicCodeGeneratorX86::VisitMathRoundFloat(HInvoke* invoke) {
452 LocationSummary* locations = invoke->GetLocations();
Aart Bik2c9f4952016-08-01 16:52:27 -0700453 if (locations->WillCall()) { // TODO: can we reach this?
Mark Mendellfb8d2792015-03-31 22:16:59 -0400454 InvokeOutOfLineIntrinsic(codegen_, invoke);
455 return;
456 }
457
Mark Mendellfb8d2792015-03-31 22:16:59 -0400458 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700459 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
460 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400461 Register out = locations->Out().AsRegister<Register>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700462 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400463 X86Assembler* assembler = GetAssembler();
464
Aart Bik2c9f4952016-08-01 16:52:27 -0700465 // Since no direct x86 rounding instruction matches the required semantics,
466 // this intrinsic is implemented as follows:
467 // result = floor(in);
468 // if (in - result >= 0.5f)
469 // result = result + 1.0f;
470 __ movss(t2, in);
471 __ roundss(t1, in, Immediate(1));
472 __ subss(t2, t1);
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700473 if (locations->GetInputCount() == 2 && locations->InAt(1).IsValid()) {
474 // Direct constant area available.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000475 HX86ComputeBaseMethodAddress* method_address =
476 invoke->InputAt(1)->AsX86ComputeBaseMethodAddress();
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700477 Register constant_area = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000478 __ comiss(t2, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(0.5f),
479 method_address,
480 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700481 __ j(kBelow, &skip_incr);
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000482 __ addss(t1, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(1.0f),
483 method_address,
484 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700485 __ Bind(&skip_incr);
486 } else {
487 // No constant area: go through stack.
488 __ pushl(Immediate(bit_cast<int32_t, float>(0.5f)));
489 __ pushl(Immediate(bit_cast<int32_t, float>(1.0f)));
490 __ comiss(t2, Address(ESP, 4));
491 __ j(kBelow, &skip_incr);
492 __ addss(t1, Address(ESP, 0));
493 __ Bind(&skip_incr);
494 __ addl(ESP, Immediate(8));
495 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400496
Aart Bik2c9f4952016-08-01 16:52:27 -0700497 // Final conversion to an integer. Unfortunately this also does not have a
498 // direct x86 instruction, since NaN should map to 0 and large positive
499 // values need to be clipped to the extreme value.
Mark Mendellfb8d2792015-03-31 22:16:59 -0400500 __ movl(out, Immediate(kPrimIntMax));
Aart Bik2c9f4952016-08-01 16:52:27 -0700501 __ cvtsi2ss(t2, out);
502 __ comiss(t1, t2);
503 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
504 __ movl(out, Immediate(0)); // does not change flags
505 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
506 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400507 __ Bind(&done);
508}
509
Vladimir Markoca6fff82017-10-03 14:49:14 +0100510static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
511 LocationSummary* locations =
512 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400513 InvokeRuntimeCallingConvention calling_convention;
514 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
515 locations->SetOut(Location::FpuRegisterLocation(XMM0));
516}
517
518static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86* codegen, QuickEntrypointEnum entry) {
519 LocationSummary* locations = invoke->GetLocations();
520 DCHECK(locations->WillCall());
521 DCHECK(invoke->IsInvokeStaticOrDirect());
522 X86Assembler* assembler = codegen->GetAssembler();
523
524 // We need some place to pass the parameters.
525 __ subl(ESP, Immediate(16));
526 __ cfi().AdjustCFAOffset(16);
527
528 // Pass the parameters at the bottom of the stack.
529 __ movsd(Address(ESP, 0), XMM0);
530
531 // If we have a second parameter, pass it next.
532 if (invoke->GetNumberOfArguments() == 2) {
533 __ movsd(Address(ESP, 8), XMM1);
534 }
535
536 // Now do the actual call.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100537 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400538
539 // Extract the return value from the FP stack.
540 __ fstpl(Address(ESP, 0));
541 __ movsd(XMM0, Address(ESP, 0));
542
543 // And clean up the stack.
544 __ addl(ESP, Immediate(16));
545 __ cfi().AdjustCFAOffset(-16);
Mark Mendella4f12202015-08-06 15:23:34 -0400546}
547
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 Markoca6fff82017-10-03 14:49:14 +0100925 LocationSummary* locations =
926 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -0700927 locations->SetInAt(0, Location::RequiresRegister());
928 locations->SetInAt(1, Location::RequiresRegister());
929
930 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
931 locations->AddTemp(Location::RegisterLocation(ECX));
932 locations->AddTemp(Location::RegisterLocation(EDI));
933
934 // Set output, ESI needed for repe_cmpsl instruction anyways.
935 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
936}
937
938void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
939 X86Assembler* assembler = GetAssembler();
940 LocationSummary* locations = invoke->GetLocations();
941
942 Register str = locations->InAt(0).AsRegister<Register>();
943 Register arg = locations->InAt(1).AsRegister<Register>();
944 Register ecx = locations->GetTemp(0).AsRegister<Register>();
945 Register edi = locations->GetTemp(1).AsRegister<Register>();
946 Register esi = locations->Out().AsRegister<Register>();
947
Mark Mendell0c9497d2015-08-21 09:30:05 -0400948 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -0700949
950 // Get offsets of count, value, and class fields within a string object.
951 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
952 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
953 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
954
955 // Note that the null check must have been done earlier.
956 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
957
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100958 StringEqualsOptimizations optimizations(invoke);
959 if (!optimizations.GetArgumentNotNull()) {
960 // Check if input is null, return false if it is.
961 __ testl(arg, arg);
962 __ j(kEqual, &return_false);
963 }
Agi Csakid7138c82015-08-13 17:46:44 -0700964
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100965 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +0100966 // Instanceof check for the argument by comparing class fields.
967 // All string objects must have the same type since String cannot be subclassed.
968 // Receiver must be a string object, so its class field is equal to all strings' class fields.
969 // 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 +0100970 __ movl(ecx, Address(str, class_offset));
971 __ cmpl(ecx, Address(arg, class_offset));
972 __ j(kNotEqual, &return_false);
973 }
Agi Csakid7138c82015-08-13 17:46:44 -0700974
975 // Reference equality check, return true if same reference.
976 __ cmpl(str, arg);
977 __ j(kEqual, &return_true);
978
jessicahandojo4877b792016-09-08 19:49:13 -0700979 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -0700980 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -0700981 // Check if lengths and compression flags are equal, return false if they're not.
982 // Two identical strings will always have same compression style since
983 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -0700984 __ cmpl(ecx, Address(arg, count_offset));
985 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100986 // Return true if strings are empty. Even with string compression `count == 0` means empty.
987 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
988 "Expecting 0=compressed, 1=uncompressed");
989 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -0700990
jessicahandojo4877b792016-09-08 19:49:13 -0700991 if (mirror::kUseStringCompression) {
992 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100993 // Extract length and differentiate between both compressed or both uncompressed.
994 // Different compression style is cut above.
995 __ shrl(ecx, Immediate(1));
996 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -0700997 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100998 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700999 __ shrl(ecx, Immediate(1));
1000 __ Bind(&string_uncompressed);
1001 }
Agi Csakid7138c82015-08-13 17:46:44 -07001002 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1003 __ leal(esi, Address(str, value_offset));
1004 __ leal(edi, Address(arg, value_offset));
1005
jessicahandojo4877b792016-09-08 19:49:13 -07001006 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1007 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001008 __ addl(ecx, Immediate(1));
1009 __ shrl(ecx, Immediate(1));
1010
jessicahandojo4877b792016-09-08 19:49:13 -07001011 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1012 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001013 DCHECK_ALIGNED(value_offset, 4);
1014 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1015
1016 // Loop to compare strings two characters at a time starting at the beginning of the string.
1017 __ repe_cmpsl();
1018 // If strings are not equal, zero flag will be cleared.
1019 __ j(kNotEqual, &return_false);
1020
1021 // Return true and exit the function.
1022 // If loop does not result in returning false, we return true.
1023 __ Bind(&return_true);
1024 __ movl(esi, Immediate(1));
1025 __ jmp(&end);
1026
1027 // Return false and exit the function.
1028 __ Bind(&return_false);
1029 __ xorl(esi, esi);
1030 __ Bind(&end);
1031}
1032
Andreas Gampe21030dd2015-05-07 14:46:15 -07001033static void CreateStringIndexOfLocations(HInvoke* invoke,
1034 ArenaAllocator* allocator,
1035 bool start_at_zero) {
1036 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1037 LocationSummary::kCallOnSlowPath,
1038 kIntrinsified);
1039 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1040 locations->SetInAt(0, Location::RegisterLocation(EDI));
1041 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1042 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1043 // of the instruction explicitly.
1044 // Note: This works as we don't clobber EAX anywhere.
1045 locations->SetInAt(1, Location::RegisterLocation(EAX));
1046 if (!start_at_zero) {
1047 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1048 }
1049 // As we clobber EDI during execution anyways, also use it as the output.
1050 locations->SetOut(Location::SameAsFirstInput());
1051
1052 // repne scasw uses ECX as the counter.
1053 locations->AddTemp(Location::RegisterLocation(ECX));
1054 // Need another temporary to be able to compute the result.
1055 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001056 if (mirror::kUseStringCompression) {
1057 // Need another temporary to be able to save unflagged string length.
1058 locations->AddTemp(Location::RequiresRegister());
1059 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001060}
1061
1062static void GenerateStringIndexOf(HInvoke* invoke,
1063 X86Assembler* assembler,
1064 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001065 bool start_at_zero) {
1066 LocationSummary* locations = invoke->GetLocations();
1067
1068 // Note that the null check must have been done earlier.
1069 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1070
1071 Register string_obj = locations->InAt(0).AsRegister<Register>();
1072 Register search_value = locations->InAt(1).AsRegister<Register>();
1073 Register counter = locations->GetTemp(0).AsRegister<Register>();
1074 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1075 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001076 // Only used when string compression feature is on.
1077 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001078
1079 // Check our assumptions for registers.
1080 DCHECK_EQ(string_obj, EDI);
1081 DCHECK_EQ(search_value, EAX);
1082 DCHECK_EQ(counter, ECX);
1083 DCHECK_EQ(out, EDI);
1084
1085 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001086 // 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 -07001087 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001088 HInstruction* code_point = invoke->InputAt(1);
1089 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001090 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001091 std::numeric_limits<uint16_t>::max()) {
1092 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1093 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001094 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001095 codegen->AddSlowPath(slow_path);
1096 __ jmp(slow_path->GetEntryLabel());
1097 __ Bind(slow_path->GetExitLabel());
1098 return;
1099 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001100 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001101 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001102 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001103 codegen->AddSlowPath(slow_path);
1104 __ j(kAbove, slow_path->GetEntryLabel());
1105 }
1106
1107 // From here down, we know that we are looking for a char that fits in 16 bits.
1108 // Location of reference to data array within the String object.
1109 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1110 // Location of count within the String object.
1111 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1112
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001113 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001114 __ movl(string_length, Address(string_obj, count_offset));
1115
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001116 // Do a zero-length check. Even with string compression `count == 0` means empty.
1117 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1118 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001119 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001120 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001121 __ testl(string_length, string_length);
1122 __ j(kEqual, &not_found_label);
1123
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001124 if (mirror::kUseStringCompression) {
1125 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1126 __ movl(string_length_flagged, string_length);
1127 // Extract the length and shift out the least significant bit used as compression flag.
1128 __ shrl(string_length, Immediate(1));
1129 }
1130
Andreas Gampe21030dd2015-05-07 14:46:15 -07001131 if (start_at_zero) {
1132 // Number of chars to scan is the same as the string length.
1133 __ movl(counter, string_length);
1134
1135 // Move to the start of the string.
1136 __ addl(string_obj, Immediate(value_offset));
1137 } else {
1138 Register start_index = locations->InAt(2).AsRegister<Register>();
1139
1140 // Do a start_index check.
1141 __ cmpl(start_index, string_length);
1142 __ j(kGreaterEqual, &not_found_label);
1143
1144 // Ensure we have a start index >= 0;
1145 __ xorl(counter, counter);
1146 __ cmpl(start_index, Immediate(0));
1147 __ cmovl(kGreater, counter, start_index);
1148
jessicahandojo4877b792016-09-08 19:49:13 -07001149 if (mirror::kUseStringCompression) {
1150 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001151 __ testl(string_length_flagged, Immediate(1));
1152 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001153 // Move to the start of the string: string_obj + value_offset + start_index.
1154 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1155 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001156
jessicahandojo4877b792016-09-08 19:49:13 -07001157 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1158 __ Bind(&offset_uncompressed_label);
1159 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1160
1161 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1162 // compare.
1163 __ Bind(&modify_counter);
1164 } else {
1165 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1166 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001167 __ negl(counter);
1168 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1169 }
1170
jessicahandojo4877b792016-09-08 19:49:13 -07001171 if (mirror::kUseStringCompression) {
1172 NearLabel uncompressed_string_comparison;
1173 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001174 __ testl(string_length_flagged, Immediate(1));
1175 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001176
jessicahandojo4877b792016-09-08 19:49:13 -07001177 // Check if EAX (search_value) is ASCII.
1178 __ cmpl(search_value, Immediate(127));
1179 __ j(kGreater, &not_found_label);
1180 // Comparing byte-per-byte.
1181 __ repne_scasb();
1182 __ jmp(&comparison_done);
1183
1184 // Everything is set up for repne scasw:
1185 // * Comparison address in EDI.
1186 // * Counter in ECX.
1187 __ Bind(&uncompressed_string_comparison);
1188 __ repne_scasw();
1189 __ Bind(&comparison_done);
1190 } else {
1191 __ repne_scasw();
1192 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001193 // Did we find a match?
1194 __ j(kNotEqual, &not_found_label);
1195
1196 // Yes, we matched. Compute the index of the result.
1197 __ subl(string_length, counter);
1198 __ leal(out, Address(string_length, -1));
1199
Mark Mendell0c9497d2015-08-21 09:30:05 -04001200 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001201 __ jmp(&done);
1202
1203 // Failed to match; return -1.
1204 __ Bind(&not_found_label);
1205 __ movl(out, Immediate(-1));
1206
1207 // And join up at the end.
1208 __ Bind(&done);
1209 if (slow_path != nullptr) {
1210 __ Bind(slow_path->GetExitLabel());
1211 }
1212}
1213
1214void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001215 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001216}
1217
1218void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001219 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001220}
1221
1222void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001223 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001224}
1225
1226void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001227 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001228}
1229
Jeff Hao848f70a2014-01-15 13:49:50 -08001230void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001231 LocationSummary* locations = new (allocator_) LocationSummary(
1232 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001233 InvokeRuntimeCallingConvention calling_convention;
1234 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1235 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1236 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1237 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1238 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001239}
1240
1241void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1242 X86Assembler* assembler = GetAssembler();
1243 LocationSummary* locations = invoke->GetLocations();
1244
1245 Register byte_array = locations->InAt(0).AsRegister<Register>();
1246 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001247 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001248 codegen_->AddSlowPath(slow_path);
1249 __ j(kEqual, slow_path->GetEntryLabel());
1250
Serban Constantinescuba45db02016-07-12 22:53:02 +01001251 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001252 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001253 __ Bind(slow_path->GetExitLabel());
1254}
1255
1256void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001257 LocationSummary* locations =
1258 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001259 InvokeRuntimeCallingConvention calling_convention;
1260 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1261 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1262 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1263 locations->SetOut(Location::RegisterLocation(EAX));
1264}
1265
1266void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001267 // No need to emit code checking whether `locations->InAt(2)` is a null
1268 // pointer, as callers of the native method
1269 //
1270 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1271 //
1272 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001273 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001274 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001275}
1276
1277void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001278 LocationSummary* locations = new (allocator_) LocationSummary(
1279 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001280 InvokeRuntimeCallingConvention calling_convention;
1281 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1282 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001283}
1284
1285void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1286 X86Assembler* assembler = GetAssembler();
1287 LocationSummary* locations = invoke->GetLocations();
1288
1289 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1290 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001291 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001292 codegen_->AddSlowPath(slow_path);
1293 __ j(kEqual, slow_path->GetEntryLabel());
1294
Serban Constantinescuba45db02016-07-12 22:53:02 +01001295 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001296 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001297 __ Bind(slow_path->GetExitLabel());
1298}
1299
Mark Mendell8f8926a2015-08-17 11:39:06 -04001300void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1301 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001302 LocationSummary* locations =
1303 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001304 locations->SetInAt(0, Location::RequiresRegister());
1305 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1306 // Place srcEnd in ECX to save a move below.
1307 locations->SetInAt(2, Location::RegisterLocation(ECX));
1308 locations->SetInAt(3, Location::RequiresRegister());
1309 locations->SetInAt(4, Location::RequiresRegister());
1310
1311 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1312 // We don't have enough registers to also grab ECX, so handle below.
1313 locations->AddTemp(Location::RegisterLocation(ESI));
1314 locations->AddTemp(Location::RegisterLocation(EDI));
1315}
1316
1317void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1318 X86Assembler* assembler = GetAssembler();
1319 LocationSummary* locations = invoke->GetLocations();
1320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001321 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001322 // Location of data in char array buffer.
1323 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1324 // Location of char array data in string.
1325 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1326
1327 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1328 Register obj = locations->InAt(0).AsRegister<Register>();
1329 Location srcBegin = locations->InAt(1);
1330 int srcBegin_value =
1331 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1332 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1333 Register dst = locations->InAt(3).AsRegister<Register>();
1334 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1335
1336 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001337 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001338 DCHECK_EQ(char_size, 2u);
1339
Mark Mendell8f8926a2015-08-17 11:39:06 -04001340 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001341 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001342 __ pushl(ECX);
1343 int stack_adjust = kX86WordSize;
1344 __ cfi().AdjustCFAOffset(stack_adjust);
1345 DCHECK_EQ(srcEnd, ECX);
1346 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001347 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001348 } else {
1349 DCHECK(srcBegin.IsRegister());
1350 __ subl(ECX, srcBegin.AsRegister<Register>());
1351 }
1352
jessicahandojo4877b792016-09-08 19:49:13 -07001353 NearLabel done;
1354 if (mirror::kUseStringCompression) {
1355 // Location of count in string
1356 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001357 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001358 DCHECK_EQ(c_char_size, 1u);
1359 __ pushl(EAX);
1360 __ cfi().AdjustCFAOffset(stack_adjust);
1361
1362 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001363 __ testl(Address(obj, count_offset), Immediate(1));
1364 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1365 "Expecting 0=compressed, 1=uncompressed");
1366 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001367 // Compute the address of the source string by adding the number of chars from
1368 // the source beginning to the value offset of a string.
1369 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1370
1371 // Start the loop to copy String's value to Array of Char.
1372 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1373 __ Bind(&copy_loop);
1374 __ jecxz(&done);
1375 // Use EAX temporary (convert byte from ESI to word).
1376 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1377 __ movzxb(EAX, Address(ESI, 0));
1378 __ movw(Address(EDI, 0), EAX);
1379 __ leal(EDI, Address(EDI, char_size));
1380 __ leal(ESI, Address(ESI, c_char_size));
1381 // TODO: Add support for LOOP to X86Assembler.
1382 __ subl(ECX, Immediate(1));
1383 __ jmp(&copy_loop);
1384 __ Bind(&copy_uncompressed);
1385 }
1386
1387 // Do the copy for uncompressed string.
1388 // Compute the address of the destination buffer.
1389 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1390 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001391 __ rep_movsw();
1392
jessicahandojo4877b792016-09-08 19:49:13 -07001393 __ Bind(&done);
1394 if (mirror::kUseStringCompression) {
1395 // Restore EAX.
1396 __ popl(EAX);
1397 __ cfi().AdjustCFAOffset(-stack_adjust);
1398 }
1399 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001400 __ popl(ECX);
1401 __ cfi().AdjustCFAOffset(-stack_adjust);
1402}
1403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001404static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001405 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1406 Location out_loc = locations->Out();
1407 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1408 // to avoid a SIGBUS.
1409 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001411 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1412 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001413 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001414 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1415 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001416 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001417 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1418 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001419 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001420 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1421 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1422 break;
1423 default:
1424 LOG(FATAL) << "Type not recognized for peek: " << size;
1425 UNREACHABLE();
1426 }
1427}
1428
1429void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001430 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001431}
1432
1433void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001434 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001435}
1436
1437void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001438 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001439}
1440
1441void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001442 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001443}
1444
1445void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001446 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001447}
1448
1449void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001450 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001451}
1452
1453void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001454 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001455}
1456
1457void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001458 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001459}
1460
Vladimir Markoca6fff82017-10-03 14:49:14 +01001461static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1462 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001463 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001464 LocationSummary* locations =
1465 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001466 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001467 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001468 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001469 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1470 } else {
1471 locations->SetInAt(1, Location::RegisterOrConstant(value));
1472 }
1473}
1474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001475static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001476 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1477 Location value_loc = locations->InAt(1);
1478 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1479 // to avoid a SIGBUS.
1480 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001481 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001482 if (value_loc.IsConstant()) {
1483 __ movb(Address(address, 0),
1484 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1485 } else {
1486 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1487 }
1488 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001489 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001490 if (value_loc.IsConstant()) {
1491 __ movw(Address(address, 0),
1492 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1493 } else {
1494 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1495 }
1496 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001497 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001498 if (value_loc.IsConstant()) {
1499 __ movl(Address(address, 0),
1500 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1501 } else {
1502 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1503 }
1504 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001506 if (value_loc.IsConstant()) {
1507 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1508 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1509 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1510 } else {
1511 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1512 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1513 }
1514 break;
1515 default:
1516 LOG(FATAL) << "Type not recognized for poke: " << size;
1517 UNREACHABLE();
1518 }
1519}
1520
1521void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001522 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001523}
1524
1525void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001526 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001527}
1528
1529void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001530 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001531}
1532
1533void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001534 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001535}
1536
1537void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001538 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001539}
1540
1541void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001542 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001543}
1544
1545void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001546 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001547}
1548
1549void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001550 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001551}
1552
1553void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001554 LocationSummary* locations =
1555 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001556 locations->SetOut(Location::RequiresRegister());
1557}
1558
1559void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1560 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001561 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001562}
1563
Roland Levillain0d5a2812015-11-13 10:07:31 +00001564static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001565 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001566 bool is_volatile,
1567 CodeGeneratorX86* codegen) {
1568 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1569 LocationSummary* locations = invoke->GetLocations();
1570 Location base_loc = locations->InAt(1);
1571 Register base = base_loc.AsRegister<Register>();
1572 Location offset_loc = locations->InAt(2);
1573 Register offset = offset_loc.AsRegisterPairLow<Register>();
1574 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001575
1576 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001577 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001578 Register output = output_loc.AsRegister<Register>();
1579 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00001580 break;
1581 }
1582
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001583 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001584 Register output = output_loc.AsRegister<Register>();
1585 if (kEmitCompilerReadBarrier) {
1586 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001587 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1588 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001589 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00001590 } else {
1591 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1592 codegen->GenerateReadBarrierSlow(
1593 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1594 }
1595 } else {
1596 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1597 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001598 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001599 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001600 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001601
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001602 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001603 Register output_lo = output_loc.AsRegisterPairLow<Register>();
1604 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001605 if (is_volatile) {
1606 // Need to use a XMM to read atomically.
1607 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1608 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1609 __ movd(output_lo, temp);
1610 __ psrlq(temp, Immediate(32));
1611 __ movd(output_hi, temp);
1612 } else {
1613 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1614 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1615 }
1616 }
1617 break;
1618
1619 default:
1620 LOG(FATAL) << "Unsupported op size " << type;
1621 UNREACHABLE();
1622 }
1623}
1624
Vladimir Markoca6fff82017-10-03 14:49:14 +01001625static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001626 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001628 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001629 bool can_call = kEmitCompilerReadBarrier &&
1630 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1631 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001632 LocationSummary* locations =
1633 new (allocator) LocationSummary(invoke,
1634 can_call
1635 ? LocationSummary::kCallOnSlowPath
1636 : LocationSummary::kNoCall,
1637 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01001638 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001639 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001640 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001641 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1642 locations->SetInAt(1, Location::RequiresRegister());
1643 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001644 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001645 if (is_volatile) {
1646 // Need to use XMM to read volatile.
1647 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001649 } else {
1650 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1651 }
1652 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001653 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001654 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001655 }
1656}
1657
1658void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001659 CreateIntIntIntToIntLocations(
1660 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001661}
1662void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001663 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001664}
1665void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001666 CreateIntIntIntToIntLocations(
1667 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001668}
1669void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001670 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001671}
1672void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001673 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001674 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001675}
1676void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001677 CreateIntIntIntToIntLocations(
1678 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001679}
1680
1681
1682void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001684}
1685void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001686 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001687}
1688void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001689 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001690}
1691void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001692 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001693}
1694void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001695 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001696}
1697void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001698 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001699}
1700
1701
Vladimir Markoca6fff82017-10-03 14:49:14 +01001702static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001703 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001704 HInvoke* invoke,
1705 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001706 LocationSummary* locations =
1707 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001708 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1709 locations->SetInAt(1, Location::RequiresRegister());
1710 locations->SetInAt(2, Location::RequiresRegister());
1711 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001712 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001713 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001714 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001715 // Ensure the value is in a byte register.
1716 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001717 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001718 locations->AddTemp(Location::RequiresFpuRegister());
1719 locations->AddTemp(Location::RequiresFpuRegister());
1720 }
1721}
1722
1723void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001724 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001726}
1727void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001728 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001729 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001730}
1731void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001732 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001733 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001734}
1735void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001736 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001737 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001738}
1739void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001740 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001741 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001742}
1743void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001744 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001745 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001746}
1747void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001748 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001749 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001750}
1751void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001752 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001753 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001754}
1755void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001756 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001757 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001758}
1759
1760// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1761// memory model.
1762static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001763 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001764 bool is_volatile,
1765 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001766 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001767 Register base = locations->InAt(1).AsRegister<Register>();
1768 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1769 Location value_loc = locations->InAt(3);
1770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001772 Register value_lo = value_loc.AsRegisterPairLow<Register>();
1773 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
1774 if (is_volatile) {
1775 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1776 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1777 __ movd(temp1, value_lo);
1778 __ movd(temp2, value_hi);
1779 __ punpckldq(temp1, temp2);
1780 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
1781 } else {
1782 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
1783 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
1784 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001785 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001786 Register temp = locations->GetTemp(0).AsRegister<Register>();
1787 __ movl(temp, value_loc.AsRegister<Register>());
1788 __ PoisonHeapReference(temp);
1789 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001790 } else {
1791 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
1792 }
1793
1794 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00001795 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001796 }
1797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001798 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001799 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04001800 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1801 locations->GetTemp(1).AsRegister<Register>(),
1802 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001803 value_loc.AsRegister<Register>(),
1804 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001805 }
1806}
1807
1808void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001809 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001810}
1811void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001812 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001813}
1814void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001816}
1817void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 GenUnsafePut(
1819 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001820}
1821void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 GenUnsafePut(
1823 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001824}
1825void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 GenUnsafePut(
1827 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001828}
1829void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001831}
1832void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001833 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001834}
1835void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001837}
1838
Vladimir Markoca6fff82017-10-03 14:49:14 +01001839static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001840 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04001841 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001842 bool can_call = kEmitCompilerReadBarrier &&
1843 kUseBakerReadBarrier &&
1844 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001845 LocationSummary* locations =
1846 new (allocator) LocationSummary(invoke,
1847 can_call
1848 ? LocationSummary::kCallOnSlowPath
1849 : LocationSummary::kNoCall,
1850 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001851 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1852 locations->SetInAt(1, Location::RequiresRegister());
1853 // Offset is a long, but in 32 bit mode, we only need the low word.
1854 // Can we update the invoke here to remove a TypeConvert to Long?
1855 locations->SetInAt(2, Location::RequiresRegister());
1856 // Expected value must be in EAX or EDX:EAX.
1857 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001858 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04001859 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
1860 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
1861 } else {
1862 locations->SetInAt(3, Location::RegisterLocation(EAX));
1863 locations->SetInAt(4, Location::RequiresRegister());
1864 }
1865
1866 // Force a byte register for the output.
1867 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001868 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001869 // Need temporary registers for card-marking, and possibly for
1870 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01001871 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001872 // Need a byte register for marking.
1873 locations->AddTemp(Location::RegisterLocation(ECX));
1874 }
1875}
1876
1877void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001878 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001879}
1880
1881void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001882 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001883}
1884
1885void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001886 // The only read barrier implementation supporting the
1887 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1888 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00001889 return;
1890 }
1891
Vladimir Markoca6fff82017-10-03 14:49:14 +01001892 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001893}
1894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001895static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001896 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04001897 LocationSummary* locations = invoke->GetLocations();
1898
1899 Register base = locations->InAt(1).AsRegister<Register>();
1900 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1901 Location out = locations->Out();
1902 DCHECK_EQ(out.AsRegister<Register>(), EAX);
1903
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001904 // The address of the field within the holding object.
1905 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
1906
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001907 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001908 // The only read barrier implementation supporting the
1909 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1910 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1911
1912 Location temp1_loc = locations->GetTemp(0);
1913 Register temp1 = temp1_loc.AsRegister<Register>();
1914 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
1915
Roland Levillain4d027112015-07-01 15:41:14 +01001916 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01001917 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01001918 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001919 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01001920
Roland Levillainb488b782015-10-22 11:38:49 +01001921 // Mark card for object assuming new value is stored.
1922 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001923 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
1924
1925 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1926 // Need to make sure the reference stored in the field is a to-space
1927 // one before attempting the CAS or the CAS could fail incorrectly.
1928 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1929 invoke,
1930 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
1931 base,
1932 field_addr,
1933 /* needs_null_check */ false,
1934 /* always_update_field */ true,
1935 &temp2);
1936 }
Roland Levillainb488b782015-10-22 11:38:49 +01001937
1938 bool base_equals_value = (base == value);
1939 if (kPoisonHeapReferences) {
1940 if (base_equals_value) {
1941 // If `base` and `value` are the same register location, move
1942 // `value` to a temporary register. This way, poisoning
1943 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001944 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01001945 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01001946 }
Roland Levillainb488b782015-10-22 11:38:49 +01001947
1948 // Check that the register allocator did not assign the location
1949 // of `expected` (EAX) to `value` nor to `base`, so that heap
1950 // poisoning (when enabled) works as intended below.
1951 // - If `value` were equal to `expected`, both references would
1952 // be poisoned twice, meaning they would not be poisoned at
1953 // all, as heap poisoning uses address negation.
1954 // - If `base` were equal to `expected`, poisoning `expected`
1955 // would invalidate `base`.
1956 DCHECK_NE(value, expected);
1957 DCHECK_NE(base, expected);
1958
1959 __ PoisonHeapReference(expected);
1960 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001961 }
1962
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001963 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001964
Roland Levillain0d5a2812015-11-13 10:07:31 +00001965 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01001966 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001967
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001968 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01001969 __ setb(kZero, out.AsRegister<Register>());
1970 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01001971
Roland Levillain391b8662015-12-18 11:43:38 +00001972 // If heap poisoning is enabled, we need to unpoison the values
1973 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01001974 if (kPoisonHeapReferences) {
1975 if (base_equals_value) {
1976 // `value` has been moved to a temporary register, no need to
1977 // unpoison it.
1978 } else {
1979 // Ensure `value` is different from `out`, so that unpoisoning
1980 // the former does not invalidate the latter.
1981 DCHECK_NE(value, out.AsRegister<Register>());
1982 __ UnpoisonHeapReference(value);
1983 }
1984 // Do not unpoison the reference contained in register
1985 // `expected`, as it is the same as register `out` (EAX).
1986 }
1987 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01001989 // Ensure the expected value is in EAX (required by the CMPXCHG
1990 // instruction).
1991 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001992 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001993 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01001994 // Ensure the expected value is in EAX:EDX and that the new
1995 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
1996 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
1997 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
1998 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
1999 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002000 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002001 } else {
2002 LOG(FATAL) << "Unexpected CAS type " << type;
2003 }
2004
Roland Levillain0d5a2812015-11-13 10:07:31 +00002005 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2006 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002007
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002008 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002009 __ setb(kZero, out.AsRegister<Register>());
2010 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002011 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002012}
2013
2014void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002015 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002016}
2017
2018void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002019 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002020}
2021
2022void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002023 // The only read barrier implementation supporting the
2024 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2025 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002028}
2029
2030void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002031 LocationSummary* locations =
2032 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002033 locations->SetInAt(0, Location::RequiresRegister());
2034 locations->SetOut(Location::SameAsFirstInput());
2035 locations->AddTemp(Location::RequiresRegister());
2036}
2037
2038static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2039 X86Assembler* assembler) {
2040 Immediate imm_shift(shift);
2041 Immediate imm_mask(mask);
2042 __ movl(temp, reg);
2043 __ shrl(reg, imm_shift);
2044 __ andl(temp, imm_mask);
2045 __ andl(reg, imm_mask);
2046 __ shll(temp, imm_shift);
2047 __ orl(reg, temp);
2048}
2049
2050void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002051 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002052 LocationSummary* locations = invoke->GetLocations();
2053
2054 Register reg = locations->InAt(0).AsRegister<Register>();
2055 Register temp = locations->GetTemp(0).AsRegister<Register>();
2056
2057 /*
2058 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2059 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2060 * compared to generic luni implementation which has 5 rounds of swapping bits.
2061 * x = bswap x
2062 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2063 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2064 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2065 */
2066 __ bswapl(reg);
2067 SwapBits(reg, temp, 1, 0x55555555, assembler);
2068 SwapBits(reg, temp, 2, 0x33333333, assembler);
2069 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2070}
2071
2072void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002073 LocationSummary* locations =
2074 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002075 locations->SetInAt(0, Location::RequiresRegister());
2076 locations->SetOut(Location::SameAsFirstInput());
2077 locations->AddTemp(Location::RequiresRegister());
2078}
2079
2080void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002081 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002082 LocationSummary* locations = invoke->GetLocations();
2083
2084 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2085 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2086 Register temp = locations->GetTemp(0).AsRegister<Register>();
2087
2088 // We want to swap high/low, then bswap each one, and then do the same
2089 // as a 32 bit reverse.
2090 // Exchange high and low.
2091 __ movl(temp, reg_low);
2092 __ movl(reg_low, reg_high);
2093 __ movl(reg_high, temp);
2094
2095 // bit-reverse low
2096 __ bswapl(reg_low);
2097 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2098 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2099 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2100
2101 // bit-reverse high
2102 __ bswapl(reg_high);
2103 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2104 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2105 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2106}
2107
Aart Bikc39dac12016-01-21 08:59:48 -08002108static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002109 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002110 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2111 // Do nothing if there is no popcnt support. This results in generating
2112 // a call for the intrinsic rather than direct code.
2113 return;
2114 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002115 LocationSummary* locations =
2116 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002117 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002118 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002119 }
Aart Bik2a946072016-01-21 12:49:00 -08002120 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002121 locations->SetOut(Location::RequiresRegister());
2122}
2123
Aart Bika19616e2016-02-01 18:57:58 -08002124static void GenBitCount(X86Assembler* assembler,
2125 CodeGeneratorX86* codegen,
2126 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002127 LocationSummary* locations = invoke->GetLocations();
2128 Location src = locations->InAt(0);
2129 Register out = locations->Out().AsRegister<Register>();
2130
2131 if (invoke->InputAt(0)->IsConstant()) {
2132 // Evaluate this at compile time.
2133 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002134 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002135 ? POPCOUNT(static_cast<uint64_t>(value))
2136 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002137 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002138 return;
2139 }
2140
2141 // Handle the non-constant cases.
2142 if (!is_long) {
2143 if (src.IsRegister()) {
2144 __ popcntl(out, src.AsRegister<Register>());
2145 } else {
2146 DCHECK(src.IsStackSlot());
2147 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2148 }
Aart Bik2a946072016-01-21 12:49:00 -08002149 } else {
2150 // The 64-bit case needs to worry about two parts.
2151 Register temp = locations->GetTemp(0).AsRegister<Register>();
2152 if (src.IsRegisterPair()) {
2153 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2154 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2155 } else {
2156 DCHECK(src.IsDoubleStackSlot());
2157 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2158 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2159 }
2160 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002161 }
Aart Bikc39dac12016-01-21 08:59:48 -08002162}
2163
2164void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002165 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002166}
2167
2168void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002169 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002170}
2171
2172void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002173 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002174}
2175
2176void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002177 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002178}
2179
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2181 LocationSummary* locations =
2182 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002183 if (is_long) {
2184 locations->SetInAt(0, Location::RequiresRegister());
2185 } else {
2186 locations->SetInAt(0, Location::Any());
2187 }
2188 locations->SetOut(Location::RequiresRegister());
2189}
2190
Aart Bika19616e2016-02-01 18:57:58 -08002191static void GenLeadingZeros(X86Assembler* assembler,
2192 CodeGeneratorX86* codegen,
2193 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002194 LocationSummary* locations = invoke->GetLocations();
2195 Location src = locations->InAt(0);
2196 Register out = locations->Out().AsRegister<Register>();
2197
2198 if (invoke->InputAt(0)->IsConstant()) {
2199 // Evaluate this at compile time.
2200 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2201 if (value == 0) {
2202 value = is_long ? 64 : 32;
2203 } else {
2204 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2205 }
Aart Bika19616e2016-02-01 18:57:58 -08002206 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002207 return;
2208 }
2209
2210 // Handle the non-constant cases.
2211 if (!is_long) {
2212 if (src.IsRegister()) {
2213 __ bsrl(out, src.AsRegister<Register>());
2214 } else {
2215 DCHECK(src.IsStackSlot());
2216 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2217 }
2218
2219 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002220 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002221 __ j(kEqual, &all_zeroes);
2222
2223 // Correct the result from BSR to get the final CLZ result.
2224 __ xorl(out, Immediate(31));
2225 __ jmp(&done);
2226
2227 // Fix the zero case with the expected result.
2228 __ Bind(&all_zeroes);
2229 __ movl(out, Immediate(32));
2230
2231 __ Bind(&done);
2232 return;
2233 }
2234
2235 // 64 bit case needs to worry about both parts of the register.
2236 DCHECK(src.IsRegisterPair());
2237 Register src_lo = src.AsRegisterPairLow<Register>();
2238 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002239 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002240
2241 // Is the high word zero?
2242 __ testl(src_hi, src_hi);
2243 __ j(kEqual, &handle_low);
2244
2245 // High word is not zero. We know that the BSR result is defined in this case.
2246 __ bsrl(out, src_hi);
2247
2248 // Correct the result from BSR to get the final CLZ result.
2249 __ xorl(out, Immediate(31));
2250 __ jmp(&done);
2251
2252 // High word was zero. We have to compute the low word count and add 32.
2253 __ Bind(&handle_low);
2254 __ bsrl(out, src_lo);
2255 __ j(kEqual, &all_zeroes);
2256
2257 // We had a valid result. Use an XOR to both correct the result and add 32.
2258 __ xorl(out, Immediate(63));
2259 __ jmp(&done);
2260
2261 // All zero case.
2262 __ Bind(&all_zeroes);
2263 __ movl(out, Immediate(64));
2264
2265 __ Bind(&done);
2266}
2267
2268void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002269 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002270}
2271
2272void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002273 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002274}
2275
2276void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002277 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002278}
2279
2280void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002281 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002282}
2283
Vladimir Markoca6fff82017-10-03 14:49:14 +01002284static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2285 LocationSummary* locations =
2286 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002287 if (is_long) {
2288 locations->SetInAt(0, Location::RequiresRegister());
2289 } else {
2290 locations->SetInAt(0, Location::Any());
2291 }
2292 locations->SetOut(Location::RequiresRegister());
2293}
2294
Aart Bika19616e2016-02-01 18:57:58 -08002295static void GenTrailingZeros(X86Assembler* assembler,
2296 CodeGeneratorX86* codegen,
2297 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002298 LocationSummary* locations = invoke->GetLocations();
2299 Location src = locations->InAt(0);
2300 Register out = locations->Out().AsRegister<Register>();
2301
2302 if (invoke->InputAt(0)->IsConstant()) {
2303 // Evaluate this at compile time.
2304 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2305 if (value == 0) {
2306 value = is_long ? 64 : 32;
2307 } else {
2308 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2309 }
Aart Bika19616e2016-02-01 18:57:58 -08002310 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002311 return;
2312 }
2313
2314 // Handle the non-constant cases.
2315 if (!is_long) {
2316 if (src.IsRegister()) {
2317 __ bsfl(out, src.AsRegister<Register>());
2318 } else {
2319 DCHECK(src.IsStackSlot());
2320 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2321 }
2322
2323 // BSF sets ZF if the input was zero, and the output is undefined.
2324 NearLabel done;
2325 __ j(kNotEqual, &done);
2326
2327 // Fix the zero case with the expected result.
2328 __ movl(out, Immediate(32));
2329
2330 __ Bind(&done);
2331 return;
2332 }
2333
2334 // 64 bit case needs to worry about both parts of the register.
2335 DCHECK(src.IsRegisterPair());
2336 Register src_lo = src.AsRegisterPairLow<Register>();
2337 Register src_hi = src.AsRegisterPairHigh<Register>();
2338 NearLabel done, all_zeroes;
2339
2340 // If the low word is zero, then ZF will be set. If not, we have the answer.
2341 __ bsfl(out, src_lo);
2342 __ j(kNotEqual, &done);
2343
2344 // Low word was zero. We have to compute the high word count and add 32.
2345 __ bsfl(out, src_hi);
2346 __ j(kEqual, &all_zeroes);
2347
2348 // We had a valid result. Add 32 to account for the low word being zero.
2349 __ addl(out, Immediate(32));
2350 __ jmp(&done);
2351
2352 // All zero case.
2353 __ Bind(&all_zeroes);
2354 __ movl(out, Immediate(64));
2355
2356 __ Bind(&done);
2357}
2358
2359void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002360 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002361}
2362
2363void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002364 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002365}
2366
2367void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002368 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002369}
2370
2371void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002372 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002373}
2374
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002375static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2376 return instruction->InputAt(input0) == instruction->InputAt(input1);
2377}
2378
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002379// Compute base address for the System.arraycopy intrinsic in `base`.
2380static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002381 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002382 const Register& array,
2383 const Location& pos,
2384 const Register& base) {
2385 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002387 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002388 DCHECK_EQ(type, DataType::Type::kReference);
2389 const int32_t element_size = DataType::Size(type);
2390 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002391 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2392
2393 if (pos.IsConstant()) {
2394 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2395 __ leal(base, Address(array, element_size * constant + data_offset));
2396 } else {
2397 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2398 }
2399}
2400
2401// Compute end source address for the System.arraycopy intrinsic in `end`.
2402static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002404 const Location& copy_length,
2405 const Register& base,
2406 const Register& end) {
2407 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002408 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002409 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 DCHECK_EQ(type, DataType::Type::kReference);
2411 const int32_t element_size = DataType::Size(type);
2412 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002413
2414 if (copy_length.IsConstant()) {
2415 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2416 __ leal(end, Address(base, element_size * constant));
2417 } else {
2418 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2419 }
2420}
2421
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002422void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002423 // The only read barrier implementation supporting the
2424 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2425 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002426 return;
2427 }
2428
2429 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2430 if (invoke->GetLocations() != nullptr) {
2431 // Need a byte register for marking.
2432 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2433
2434 static constexpr size_t kSrc = 0;
2435 static constexpr size_t kSrcPos = 1;
2436 static constexpr size_t kDest = 2;
2437 static constexpr size_t kDestPos = 3;
2438 static constexpr size_t kLength = 4;
2439
2440 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2441 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2442 !invoke->InputAt(kLength)->IsIntConstant()) {
2443 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2444 !IsSameInput(invoke, kSrcPos, kLength) &&
2445 !IsSameInput(invoke, kDestPos, kLength) &&
2446 !IsSameInput(invoke, kSrc, kDest)) {
2447 // Not enough registers, make the length also take a stack slot.
2448 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2449 }
2450 }
2451 }
2452}
2453
2454void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002455 // The only read barrier implementation supporting the
2456 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2457 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002458
2459 X86Assembler* assembler = GetAssembler();
2460 LocationSummary* locations = invoke->GetLocations();
2461
2462 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2463 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2464 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2465 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002466 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002467
2468 Register src = locations->InAt(0).AsRegister<Register>();
2469 Location src_pos = locations->InAt(1);
2470 Register dest = locations->InAt(2).AsRegister<Register>();
2471 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002472 Location length_arg = locations->InAt(4);
2473 Location length = length_arg;
2474 Location temp1_loc = locations->GetTemp(0);
2475 Register temp1 = temp1_loc.AsRegister<Register>();
2476 Location temp2_loc = locations->GetTemp(1);
2477 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002478
Vladimir Marko174b2e22017-10-12 13:34:49 +01002479 SlowPathCode* intrinsic_slow_path =
2480 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002481 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002482
2483 NearLabel conditions_on_positions_validated;
2484 SystemArrayCopyOptimizations optimizations(invoke);
2485
2486 // If source and destination are the same, we go to slow path if we need to do
2487 // forward copying.
2488 if (src_pos.IsConstant()) {
2489 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2490 if (dest_pos.IsConstant()) {
2491 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2492 if (optimizations.GetDestinationIsSource()) {
2493 // Checked when building locations.
2494 DCHECK_GE(src_pos_constant, dest_pos_constant);
2495 } else if (src_pos_constant < dest_pos_constant) {
2496 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002497 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002498 }
2499 } else {
2500 if (!optimizations.GetDestinationIsSource()) {
2501 __ cmpl(src, dest);
2502 __ j(kNotEqual, &conditions_on_positions_validated);
2503 }
2504 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002505 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002506 }
2507 } else {
2508 if (!optimizations.GetDestinationIsSource()) {
2509 __ cmpl(src, dest);
2510 __ j(kNotEqual, &conditions_on_positions_validated);
2511 }
2512 if (dest_pos.IsConstant()) {
2513 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2514 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002515 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002516 } else {
2517 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002518 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002519 }
2520 }
2521
2522 __ Bind(&conditions_on_positions_validated);
2523
2524 if (!optimizations.GetSourceIsNotNull()) {
2525 // Bail out if the source is null.
2526 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002527 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002528 }
2529
2530 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2531 // Bail out if the destination is null.
2532 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002533 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002534 }
2535
Roland Levillain0b671c02016-08-19 12:02:34 +01002536 Location temp3_loc = locations->GetTemp(2);
2537 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002538 if (length.IsStackSlot()) {
2539 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2540 length = Location::RegisterLocation(temp3);
2541 }
2542
2543 // If the length is negative, bail out.
2544 // We have already checked in the LocationsBuilder for the constant case.
2545 if (!length.IsConstant() &&
2546 !optimizations.GetCountIsSourceLength() &&
2547 !optimizations.GetCountIsDestinationLength()) {
2548 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002549 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002550 }
2551
2552 // Validity checks: source.
2553 CheckPosition(assembler,
2554 src_pos,
2555 src,
2556 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002557 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002558 temp1,
2559 optimizations.GetCountIsSourceLength());
2560
2561 // Validity checks: dest.
2562 CheckPosition(assembler,
2563 dest_pos,
2564 dest,
2565 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002566 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002567 temp1,
2568 optimizations.GetCountIsDestinationLength());
2569
2570 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2571 // Check whether all elements of the source array are assignable to the component
2572 // type of the destination array. We do two checks: the classes are the same,
2573 // or the destination is Object[]. If none of these checks succeed, we go to the
2574 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01002575
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002576 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002577 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2578 // /* HeapReference<Class> */ temp1 = src->klass_
2579 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002580 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002581 // Bail out if the source is not a non primitive array.
2582 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2583 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002584 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002585 __ testl(temp1, temp1);
2586 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2587 // If heap poisoning is enabled, `temp1` has been unpoisoned
2588 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2589 } else {
2590 // /* HeapReference<Class> */ temp1 = src->klass_
2591 __ movl(temp1, Address(src, class_offset));
2592 __ MaybeUnpoisonHeapReference(temp1);
2593 // Bail out if the source is not a non primitive array.
2594 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2595 __ movl(temp1, Address(temp1, component_offset));
2596 __ testl(temp1, temp1);
2597 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2598 __ MaybeUnpoisonHeapReference(temp1);
2599 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002600 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002601 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002602 }
2603
Roland Levillain0b671c02016-08-19 12:02:34 +01002604 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2605 if (length.Equals(Location::RegisterLocation(temp3))) {
2606 // When Baker read barriers are enabled, register `temp3`,
2607 // which in the present case contains the `length` parameter,
2608 // will be overwritten below. Make the `length` location
2609 // reference the original stack location; it will be moved
2610 // back to `temp3` later if necessary.
2611 DCHECK(length_arg.IsStackSlot());
2612 length = length_arg;
2613 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002614
Roland Levillain0b671c02016-08-19 12:02:34 +01002615 // /* HeapReference<Class> */ temp1 = dest->klass_
2616 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002617 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002618
Roland Levillain0b671c02016-08-19 12:02:34 +01002619 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2620 // Bail out if the destination is not a non primitive array.
2621 //
2622 // Register `temp1` is not trashed by the read barrier emitted
2623 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2624 // method produces a call to a ReadBarrierMarkRegX entry point,
2625 // which saves all potentially live registers, including
2626 // temporaries such a `temp1`.
2627 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2628 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002629 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002630 __ testl(temp2, temp2);
2631 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2632 // If heap poisoning is enabled, `temp2` has been unpoisoned
2633 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2634 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2635 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2636 }
2637
2638 // For the same reason given earlier, `temp1` is not trashed by the
2639 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2640 // /* HeapReference<Class> */ temp2 = src->klass_
2641 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002642 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002643 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2644 __ cmpl(temp1, temp2);
2645
2646 if (optimizations.GetDestinationIsTypedObjectArray()) {
2647 NearLabel do_copy;
2648 __ j(kEqual, &do_copy);
2649 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2650 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002651 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002652 // We do not need to emit a read barrier for the following
2653 // heap reference load, as `temp1` is only used in a
2654 // comparison with null below, and this reference is not
2655 // kept afterwards.
2656 __ cmpl(Address(temp1, super_offset), Immediate(0));
2657 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2658 __ Bind(&do_copy);
2659 } else {
2660 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2661 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002662 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002663 // Non read barrier code.
2664
2665 // /* HeapReference<Class> */ temp1 = dest->klass_
2666 __ movl(temp1, Address(dest, class_offset));
2667 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2668 __ MaybeUnpoisonHeapReference(temp1);
2669 // Bail out if the destination is not a non primitive array.
2670 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2671 __ movl(temp2, Address(temp1, component_offset));
2672 __ testl(temp2, temp2);
2673 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2674 __ MaybeUnpoisonHeapReference(temp2);
2675 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2676 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2677 // Re-poison the heap reference to make the compare instruction below
2678 // compare two poisoned references.
2679 __ PoisonHeapReference(temp1);
2680 }
2681
2682 // Note: if heap poisoning is on, we are comparing two poisoned references here.
2683 __ cmpl(temp1, Address(src, class_offset));
2684
2685 if (optimizations.GetDestinationIsTypedObjectArray()) {
2686 NearLabel do_copy;
2687 __ j(kEqual, &do_copy);
2688 __ MaybeUnpoisonHeapReference(temp1);
2689 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2690 __ movl(temp1, Address(temp1, component_offset));
2691 __ MaybeUnpoisonHeapReference(temp1);
2692 __ cmpl(Address(temp1, super_offset), Immediate(0));
2693 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2694 __ Bind(&do_copy);
2695 } else {
2696 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2697 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002698 }
2699 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2700 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2701 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002702 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2703 // /* HeapReference<Class> */ temp1 = src->klass_
2704 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002705 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002706 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2707 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002708 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002709 __ testl(temp1, temp1);
2710 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2711 // If heap poisoning is enabled, `temp1` has been unpoisoned
2712 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2713 } else {
2714 // /* HeapReference<Class> */ temp1 = src->klass_
2715 __ movl(temp1, Address(src, class_offset));
2716 __ MaybeUnpoisonHeapReference(temp1);
2717 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2718 __ movl(temp1, Address(temp1, component_offset));
2719 __ testl(temp1, temp1);
2720 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2721 __ MaybeUnpoisonHeapReference(temp1);
2722 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002723 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002724 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002725 }
2726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 const DataType::Type type = DataType::Type::kReference;
2728 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002729
Roland Levillain0b671c02016-08-19 12:02:34 +01002730 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002731 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002732
Roland Levillain0b671c02016-08-19 12:02:34 +01002733 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2734 // If it is needed (in the case of the fast-path loop), the base
2735 // destination address is computed later, as `temp2` is used for
2736 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002737
Roland Levillain0b671c02016-08-19 12:02:34 +01002738 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002739 if (length.IsStackSlot()) {
2740 // Location `length` is again pointing at a stack slot, as
2741 // register `temp3` (which was containing the length parameter
2742 // earlier) has been overwritten; restore it now
2743 DCHECK(length.Equals(length_arg));
2744 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2745 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002746 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002747 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002748
Roland Levillain0b671c02016-08-19 12:02:34 +01002749 // SystemArrayCopy implementation for Baker read barriers (see
2750 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
2751 //
2752 // if (src_ptr != end_ptr) {
2753 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2754 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002755 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01002756 // if (is_gray) {
2757 // // Slow-path copy.
2758 // for (size_t i = 0; i != length; ++i) {
2759 // dest_array[dest_pos + i] =
2760 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
2761 // }
2762 // } else {
2763 // // Fast-path copy.
2764 // do {
2765 // *dest_ptr++ = *src_ptr++;
2766 // } while (src_ptr != end_ptr)
2767 // }
2768 // }
2769
2770 NearLabel loop, done;
2771
2772 // Don't enter copy loop if `length == 0`.
2773 __ cmpl(temp1, temp3);
2774 __ j(kEqual, &done);
2775
Vladimir Marko953437b2016-08-24 08:30:46 +00002776 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01002777 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002778 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00002779 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
2780 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
2781 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
2782
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002783 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00002784 // goto slow_path;
2785 // At this point, just do the "if" and make sure that flags are preserved until the branch.
2786 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01002787
2788 // Load fence to prevent load-load reordering.
2789 // Note that this is a no-op, thanks to the x86 memory model.
2790 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2791
2792 // Slow path used to copy array when `src` is gray.
2793 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002794 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002795 codegen_->AddSlowPath(read_barrier_slow_path);
2796
Vladimir Marko953437b2016-08-24 08:30:46 +00002797 // We have done the "if" of the gray bit check above, now branch based on the flags.
2798 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01002799
2800 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002801 // Compute the base destination address in `temp2`.
2802 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002803 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2804 // poison/unpoison.
2805 __ Bind(&loop);
2806 __ pushl(Address(temp1, 0));
2807 __ cfi().AdjustCFAOffset(4);
2808 __ popl(Address(temp2, 0));
2809 __ cfi().AdjustCFAOffset(-4);
2810 __ addl(temp1, Immediate(element_size));
2811 __ addl(temp2, Immediate(element_size));
2812 __ cmpl(temp1, temp3);
2813 __ j(kNotEqual, &loop);
2814
2815 __ Bind(read_barrier_slow_path->GetExitLabel());
2816 __ Bind(&done);
2817 } else {
2818 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01002819 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002820 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002821 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002822 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002823 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2824 // poison/unpoison.
2825 NearLabel loop, done;
2826 __ cmpl(temp1, temp3);
2827 __ j(kEqual, &done);
2828 __ Bind(&loop);
2829 __ pushl(Address(temp1, 0));
2830 __ cfi().AdjustCFAOffset(4);
2831 __ popl(Address(temp2, 0));
2832 __ cfi().AdjustCFAOffset(-4);
2833 __ addl(temp1, Immediate(element_size));
2834 __ addl(temp2, Immediate(element_size));
2835 __ cmpl(temp1, temp3);
2836 __ j(kNotEqual, &loop);
2837 __ Bind(&done);
2838 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002839
2840 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002841 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002842
Roland Levillain0b671c02016-08-19 12:02:34 +01002843 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002844}
2845
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002846void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002847 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002848 InvokeRuntimeCallingConvention calling_convention;
2849 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2850 invoke,
2851 codegen_,
2852 Location::RegisterLocation(EAX),
2853 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Markoeebb8212018-06-05 14:57:24 +01002854
2855 LocationSummary* locations = invoke->GetLocations();
2856 if (locations != nullptr) {
2857 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
2858 if (invoke_static_or_direct->HasSpecialInput() &&
2859 invoke->InputAt(invoke_static_or_direct->GetSpecialInputIndex())
2860 ->IsX86ComputeBaseMethodAddress()) {
2861 locations->SetInAt(invoke_static_or_direct->GetSpecialInputIndex(),
2862 Location::RequiresRegister());
2863 }
2864 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002865}
2866
2867void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002868 DCHECK(invoke->IsInvokeStaticOrDirect());
Vladimir Marko6fd16062018-06-26 11:02:04 +01002869 IntrinsicVisitor::IntegerValueOfInfo info =
2870 IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002871 LocationSummary* locations = invoke->GetLocations();
2872 X86Assembler* assembler = GetAssembler();
2873
2874 Register out = locations->Out().AsRegister<Register>();
2875 InvokeRuntimeCallingConvention calling_convention;
2876 if (invoke->InputAt(0)->IsConstant()) {
2877 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
Vladimir Marko6fd16062018-06-26 11:02:04 +01002878 if (static_cast<uint32_t>(value - info.low) < info.length) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002879 // Just embed the j.l.Integer in the code.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002880 DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01002881 codegen_->LoadBootImageAddress(
Vladimir Marko6fd16062018-06-26 11:02:04 +01002882 out, info.value_boot_image_reference, invoke->AsInvokeStaticOrDirect());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002883 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002884 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002885 // Allocate and initialize a new j.l.Integer.
2886 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2887 // JIT object table.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002888 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2889 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002890 __ movl(Address(out, info.value_offset), Immediate(value));
2891 }
2892 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002893 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002894 Register in = locations->InAt(0).AsRegister<Register>();
2895 // Check bounds of our cache.
2896 __ leal(out, Address(in, -info.low));
Vladimir Markoeebb8212018-06-05 14:57:24 +01002897 __ cmpl(out, Immediate(info.length));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002898 NearLabel allocate, done;
2899 __ j(kAboveEqual, &allocate);
2900 // If the value is within the bounds, load the j.l.Integer directly from the array.
Vladimir Markoeebb8212018-06-05 14:57:24 +01002901 constexpr size_t kElementSize = sizeof(mirror::HeapReference<mirror::Object>);
Vladimir Markoeebb8212018-06-05 14:57:24 +01002902 static_assert((1u << TIMES_4) == sizeof(mirror::HeapReference<mirror::Object>),
2903 "Check heap reference size.");
Vladimir Marko6fd16062018-06-26 11:02:04 +01002904 if (codegen_->GetCompilerOptions().IsBootImage()) {
2905 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
2906 size_t method_address_index = invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
2907 HX86ComputeBaseMethodAddress* method_address =
2908 invoke->InputAt(method_address_index)->AsX86ComputeBaseMethodAddress();
2909 DCHECK(method_address != nullptr);
2910 Register method_address_reg =
2911 invoke->GetLocations()->InAt(method_address_index).AsRegister<Register>();
2912 __ movl(out, Address(method_address_reg, out, TIMES_4, CodeGeneratorX86::kDummy32BitOffset));
2913 codegen_->RecordBootImageIntrinsicPatch(method_address, info.array_data_boot_image_reference);
2914 } else {
2915 // Note: We're about to clobber the index in `out`, so we need to use `in` and
2916 // adjust the offset accordingly.
2917 uint32_t mid_array_boot_image_offset =
2918 info.array_data_boot_image_reference - info.low * kElementSize;
2919 codegen_->LoadBootImageAddress(
2920 out, mid_array_boot_image_offset, invoke->AsInvokeStaticOrDirect());
2921 DCHECK_NE(out, in);
2922 __ movl(out, Address(out, in, TIMES_4, 0));
2923 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002924 __ MaybeUnpoisonHeapReference(out);
2925 __ jmp(&done);
2926 __ Bind(&allocate);
2927 // Otherwise allocate and initialize a new j.l.Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002928 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2929 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002930 __ movl(Address(out, info.value_offset), in);
2931 __ Bind(&done);
2932 }
2933}
2934
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002935void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002936 LocationSummary* locations =
2937 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002938 locations->SetOut(Location::RequiresRegister());
2939}
2940
2941void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
2942 X86Assembler* assembler = GetAssembler();
2943 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
2944 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
2945 NearLabel done;
2946 __ fs()->movl(out, address);
2947 __ testl(out, out);
2948 __ j(kEqual, &done);
2949 __ fs()->movl(address, Immediate(0));
2950 codegen_->MemoryFence();
2951 __ Bind(&done);
2952}
2953
Hans Boehmc7b28de2018-03-09 17:05:28 -08002954void IntrinsicLocationsBuilderX86::VisitReachabilityFence(HInvoke* invoke) {
2955 LocationSummary* locations =
2956 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
2957 locations->SetInAt(0, Location::Any());
2958}
2959
2960void IntrinsicCodeGeneratorX86::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002961
Aart Bik2f9fcc92016-03-01 15:16:54 -08002962UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00002963UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002964UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
2965UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
2966UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
2967UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
2968UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
2969UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
xueliang.zhongcb58b072017-10-13 12:06:56 +01002970UNIMPLEMENTED_INTRINSIC(X86, CRC32Update)
Mark Mendell09ed1a32015-03-25 08:30:06 -04002971
Aart Bikff7d89c2016-11-07 08:49:28 -08002972UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
2973UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002974UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
2975UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
2976UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
2977UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
2978UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
2979UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002980
Aart Bik0e54c012016-03-04 12:08:31 -08002981// 1.8.
2982UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
2983UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
2984UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
2985UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
2986UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002987
Aart Bik2f9fcc92016-03-01 15:16:54 -08002988UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01002989
2990#undef __
2991
Mark Mendell09ed1a32015-03-25 08:30:06 -04002992} // namespace x86
2993} // namespace art