blob: 3b23798758776b1444bafa76b81b47425146e49c [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.
Roland Levillain1d775d22018-09-07 13:56:57 +0100970 //
971 // As the String class is expected to be non-movable, we can read the class
972 // field from String.equals' arguments without read barriers.
973 AssertNonMovableStringClass();
974 // Also, because we use the loaded class references only to compare them, we
975 // don't need to unpoison them.
976 // /* HeapReference<Class> */ ecx = str->klass_
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100977 __ movl(ecx, Address(str, class_offset));
Roland Levillain1d775d22018-09-07 13:56:57 +0100978 // if (ecx != /* HeapReference<Class> */ arg->klass_) return false
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +0100979 __ cmpl(ecx, Address(arg, class_offset));
980 __ j(kNotEqual, &return_false);
981 }
Agi Csakid7138c82015-08-13 17:46:44 -0700982
983 // Reference equality check, return true if same reference.
984 __ cmpl(str, arg);
985 __ j(kEqual, &return_true);
986
jessicahandojo4877b792016-09-08 19:49:13 -0700987 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -0700988 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -0700989 // Check if lengths and compression flags are equal, return false if they're not.
990 // Two identical strings will always have same compression style since
991 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -0700992 __ cmpl(ecx, Address(arg, count_offset));
993 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100994 // Return true if strings are empty. Even with string compression `count == 0` means empty.
995 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
996 "Expecting 0=compressed, 1=uncompressed");
997 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -0700998
jessicahandojo4877b792016-09-08 19:49:13 -0700999 if (mirror::kUseStringCompression) {
1000 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001001 // Extract length and differentiate between both compressed or both uncompressed.
1002 // Different compression style is cut above.
1003 __ shrl(ecx, Immediate(1));
1004 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001005 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001006 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001007 __ shrl(ecx, Immediate(1));
1008 __ Bind(&string_uncompressed);
1009 }
Agi Csakid7138c82015-08-13 17:46:44 -07001010 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1011 __ leal(esi, Address(str, value_offset));
1012 __ leal(edi, Address(arg, value_offset));
1013
jessicahandojo4877b792016-09-08 19:49:13 -07001014 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1015 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001016 __ addl(ecx, Immediate(1));
1017 __ shrl(ecx, Immediate(1));
1018
jessicahandojo4877b792016-09-08 19:49:13 -07001019 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1020 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001021 DCHECK_ALIGNED(value_offset, 4);
1022 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1023
1024 // Loop to compare strings two characters at a time starting at the beginning of the string.
1025 __ repe_cmpsl();
1026 // If strings are not equal, zero flag will be cleared.
1027 __ j(kNotEqual, &return_false);
1028
1029 // Return true and exit the function.
1030 // If loop does not result in returning false, we return true.
1031 __ Bind(&return_true);
1032 __ movl(esi, Immediate(1));
1033 __ jmp(&end);
1034
1035 // Return false and exit the function.
1036 __ Bind(&return_false);
1037 __ xorl(esi, esi);
1038 __ Bind(&end);
1039}
1040
Andreas Gampe21030dd2015-05-07 14:46:15 -07001041static void CreateStringIndexOfLocations(HInvoke* invoke,
1042 ArenaAllocator* allocator,
1043 bool start_at_zero) {
1044 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1045 LocationSummary::kCallOnSlowPath,
1046 kIntrinsified);
1047 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1048 locations->SetInAt(0, Location::RegisterLocation(EDI));
1049 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1050 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1051 // of the instruction explicitly.
1052 // Note: This works as we don't clobber EAX anywhere.
1053 locations->SetInAt(1, Location::RegisterLocation(EAX));
1054 if (!start_at_zero) {
1055 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1056 }
1057 // As we clobber EDI during execution anyways, also use it as the output.
1058 locations->SetOut(Location::SameAsFirstInput());
1059
1060 // repne scasw uses ECX as the counter.
1061 locations->AddTemp(Location::RegisterLocation(ECX));
1062 // Need another temporary to be able to compute the result.
1063 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001064 if (mirror::kUseStringCompression) {
1065 // Need another temporary to be able to save unflagged string length.
1066 locations->AddTemp(Location::RequiresRegister());
1067 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001068}
1069
1070static void GenerateStringIndexOf(HInvoke* invoke,
1071 X86Assembler* assembler,
1072 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001073 bool start_at_zero) {
1074 LocationSummary* locations = invoke->GetLocations();
1075
1076 // Note that the null check must have been done earlier.
1077 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1078
1079 Register string_obj = locations->InAt(0).AsRegister<Register>();
1080 Register search_value = locations->InAt(1).AsRegister<Register>();
1081 Register counter = locations->GetTemp(0).AsRegister<Register>();
1082 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1083 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001084 // Only used when string compression feature is on.
1085 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001086
1087 // Check our assumptions for registers.
1088 DCHECK_EQ(string_obj, EDI);
1089 DCHECK_EQ(search_value, EAX);
1090 DCHECK_EQ(counter, ECX);
1091 DCHECK_EQ(out, EDI);
1092
1093 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001094 // 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 -07001095 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001096 HInstruction* code_point = invoke->InputAt(1);
1097 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001098 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001099 std::numeric_limits<uint16_t>::max()) {
1100 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1101 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
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 __ jmp(slow_path->GetEntryLabel());
1105 __ Bind(slow_path->GetExitLabel());
1106 return;
1107 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001108 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001109 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001110 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001111 codegen->AddSlowPath(slow_path);
1112 __ j(kAbove, slow_path->GetEntryLabel());
1113 }
1114
1115 // From here down, we know that we are looking for a char that fits in 16 bits.
1116 // Location of reference to data array within the String object.
1117 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1118 // Location of count within the String object.
1119 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1120
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001121 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001122 __ movl(string_length, Address(string_obj, count_offset));
1123
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001124 // Do a zero-length check. Even with string compression `count == 0` means empty.
1125 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1126 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001127 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001128 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001129 __ testl(string_length, string_length);
1130 __ j(kEqual, &not_found_label);
1131
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001132 if (mirror::kUseStringCompression) {
1133 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1134 __ movl(string_length_flagged, string_length);
1135 // Extract the length and shift out the least significant bit used as compression flag.
1136 __ shrl(string_length, Immediate(1));
1137 }
1138
Andreas Gampe21030dd2015-05-07 14:46:15 -07001139 if (start_at_zero) {
1140 // Number of chars to scan is the same as the string length.
1141 __ movl(counter, string_length);
1142
1143 // Move to the start of the string.
1144 __ addl(string_obj, Immediate(value_offset));
1145 } else {
1146 Register start_index = locations->InAt(2).AsRegister<Register>();
1147
1148 // Do a start_index check.
1149 __ cmpl(start_index, string_length);
1150 __ j(kGreaterEqual, &not_found_label);
1151
1152 // Ensure we have a start index >= 0;
1153 __ xorl(counter, counter);
1154 __ cmpl(start_index, Immediate(0));
1155 __ cmovl(kGreater, counter, start_index);
1156
jessicahandojo4877b792016-09-08 19:49:13 -07001157 if (mirror::kUseStringCompression) {
1158 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001159 __ testl(string_length_flagged, Immediate(1));
1160 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001161 // Move to the start of the string: string_obj + value_offset + start_index.
1162 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1163 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001164
jessicahandojo4877b792016-09-08 19:49:13 -07001165 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1166 __ Bind(&offset_uncompressed_label);
1167 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1168
1169 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1170 // compare.
1171 __ Bind(&modify_counter);
1172 } else {
1173 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1174 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001175 __ negl(counter);
1176 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1177 }
1178
jessicahandojo4877b792016-09-08 19:49:13 -07001179 if (mirror::kUseStringCompression) {
1180 NearLabel uncompressed_string_comparison;
1181 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001182 __ testl(string_length_flagged, Immediate(1));
1183 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001184
jessicahandojo4877b792016-09-08 19:49:13 -07001185 // Check if EAX (search_value) is ASCII.
1186 __ cmpl(search_value, Immediate(127));
1187 __ j(kGreater, &not_found_label);
1188 // Comparing byte-per-byte.
1189 __ repne_scasb();
1190 __ jmp(&comparison_done);
1191
1192 // Everything is set up for repne scasw:
1193 // * Comparison address in EDI.
1194 // * Counter in ECX.
1195 __ Bind(&uncompressed_string_comparison);
1196 __ repne_scasw();
1197 __ Bind(&comparison_done);
1198 } else {
1199 __ repne_scasw();
1200 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001201 // Did we find a match?
1202 __ j(kNotEqual, &not_found_label);
1203
1204 // Yes, we matched. Compute the index of the result.
1205 __ subl(string_length, counter);
1206 __ leal(out, Address(string_length, -1));
1207
Mark Mendell0c9497d2015-08-21 09:30:05 -04001208 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001209 __ jmp(&done);
1210
1211 // Failed to match; return -1.
1212 __ Bind(&not_found_label);
1213 __ movl(out, Immediate(-1));
1214
1215 // And join up at the end.
1216 __ Bind(&done);
1217 if (slow_path != nullptr) {
1218 __ Bind(slow_path->GetExitLabel());
1219 }
1220}
1221
1222void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001223 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001224}
1225
1226void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001227 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001228}
1229
1230void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001231 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001232}
1233
1234void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001235 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001236}
1237
Jeff Hao848f70a2014-01-15 13:49:50 -08001238void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001239 LocationSummary* locations = new (allocator_) LocationSummary(
1240 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001241 InvokeRuntimeCallingConvention calling_convention;
1242 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1243 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1244 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1245 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1246 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001247}
1248
1249void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1250 X86Assembler* assembler = GetAssembler();
1251 LocationSummary* locations = invoke->GetLocations();
1252
1253 Register byte_array = locations->InAt(0).AsRegister<Register>();
1254 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001255 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001256 codegen_->AddSlowPath(slow_path);
1257 __ j(kEqual, slow_path->GetEntryLabel());
1258
Serban Constantinescuba45db02016-07-12 22:53:02 +01001259 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001260 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001261 __ Bind(slow_path->GetExitLabel());
1262}
1263
1264void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001265 LocationSummary* locations =
1266 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001267 InvokeRuntimeCallingConvention calling_convention;
1268 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1269 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1270 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1271 locations->SetOut(Location::RegisterLocation(EAX));
1272}
1273
1274void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001275 // No need to emit code checking whether `locations->InAt(2)` is a null
1276 // pointer, as callers of the native method
1277 //
1278 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1279 //
1280 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001281 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001282 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001283}
1284
1285void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001286 LocationSummary* locations = new (allocator_) LocationSummary(
1287 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001288 InvokeRuntimeCallingConvention calling_convention;
1289 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1290 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001291}
1292
1293void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1294 X86Assembler* assembler = GetAssembler();
1295 LocationSummary* locations = invoke->GetLocations();
1296
1297 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1298 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001299 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001300 codegen_->AddSlowPath(slow_path);
1301 __ j(kEqual, slow_path->GetEntryLabel());
1302
Serban Constantinescuba45db02016-07-12 22:53:02 +01001303 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001304 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001305 __ Bind(slow_path->GetExitLabel());
1306}
1307
Mark Mendell8f8926a2015-08-17 11:39:06 -04001308void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1309 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001310 LocationSummary* locations =
1311 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001312 locations->SetInAt(0, Location::RequiresRegister());
1313 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1314 // Place srcEnd in ECX to save a move below.
1315 locations->SetInAt(2, Location::RegisterLocation(ECX));
1316 locations->SetInAt(3, Location::RequiresRegister());
1317 locations->SetInAt(4, Location::RequiresRegister());
1318
1319 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1320 // We don't have enough registers to also grab ECX, so handle below.
1321 locations->AddTemp(Location::RegisterLocation(ESI));
1322 locations->AddTemp(Location::RegisterLocation(EDI));
1323}
1324
1325void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1326 X86Assembler* assembler = GetAssembler();
1327 LocationSummary* locations = invoke->GetLocations();
1328
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001329 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001330 // Location of data in char array buffer.
1331 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1332 // Location of char array data in string.
1333 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1334
1335 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1336 Register obj = locations->InAt(0).AsRegister<Register>();
1337 Location srcBegin = locations->InAt(1);
1338 int srcBegin_value =
1339 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1340 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1341 Register dst = locations->InAt(3).AsRegister<Register>();
1342 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1343
1344 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001345 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001346 DCHECK_EQ(char_size, 2u);
1347
Mark Mendell8f8926a2015-08-17 11:39:06 -04001348 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001349 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001350 __ pushl(ECX);
1351 int stack_adjust = kX86WordSize;
1352 __ cfi().AdjustCFAOffset(stack_adjust);
1353 DCHECK_EQ(srcEnd, ECX);
1354 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001355 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001356 } else {
1357 DCHECK(srcBegin.IsRegister());
1358 __ subl(ECX, srcBegin.AsRegister<Register>());
1359 }
1360
jessicahandojo4877b792016-09-08 19:49:13 -07001361 NearLabel done;
1362 if (mirror::kUseStringCompression) {
1363 // Location of count in string
1364 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001365 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001366 DCHECK_EQ(c_char_size, 1u);
1367 __ pushl(EAX);
1368 __ cfi().AdjustCFAOffset(stack_adjust);
1369
1370 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001371 __ testl(Address(obj, count_offset), Immediate(1));
1372 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1373 "Expecting 0=compressed, 1=uncompressed");
1374 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001375 // Compute the address of the source string by adding the number of chars from
1376 // the source beginning to the value offset of a string.
1377 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1378
1379 // Start the loop to copy String's value to Array of Char.
1380 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1381 __ Bind(&copy_loop);
1382 __ jecxz(&done);
1383 // Use EAX temporary (convert byte from ESI to word).
1384 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1385 __ movzxb(EAX, Address(ESI, 0));
1386 __ movw(Address(EDI, 0), EAX);
1387 __ leal(EDI, Address(EDI, char_size));
1388 __ leal(ESI, Address(ESI, c_char_size));
1389 // TODO: Add support for LOOP to X86Assembler.
1390 __ subl(ECX, Immediate(1));
1391 __ jmp(&copy_loop);
1392 __ Bind(&copy_uncompressed);
1393 }
1394
1395 // Do the copy for uncompressed string.
1396 // Compute the address of the destination buffer.
1397 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1398 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001399 __ rep_movsw();
1400
jessicahandojo4877b792016-09-08 19:49:13 -07001401 __ Bind(&done);
1402 if (mirror::kUseStringCompression) {
1403 // Restore EAX.
1404 __ popl(EAX);
1405 __ cfi().AdjustCFAOffset(-stack_adjust);
1406 }
1407 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001408 __ popl(ECX);
1409 __ cfi().AdjustCFAOffset(-stack_adjust);
1410}
1411
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001412static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001413 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1414 Location out_loc = locations->Out();
1415 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1416 // to avoid a SIGBUS.
1417 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001418 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001419 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1420 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001421 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001422 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1423 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001424 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001425 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1426 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001427 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001428 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1429 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1430 break;
1431 default:
1432 LOG(FATAL) << "Type not recognized for peek: " << size;
1433 UNREACHABLE();
1434 }
1435}
1436
1437void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001438 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001439}
1440
1441void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001442 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001443}
1444
1445void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001446 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001447}
1448
1449void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001450 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001451}
1452
1453void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001454 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001455}
1456
1457void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001458 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001459}
1460
1461void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001462 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001463}
1464
1465void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001466 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001467}
1468
Vladimir Markoca6fff82017-10-03 14:49:14 +01001469static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1470 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001471 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001472 LocationSummary* locations =
1473 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001474 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001475 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001476 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001477 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1478 } else {
1479 locations->SetInAt(1, Location::RegisterOrConstant(value));
1480 }
1481}
1482
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001483static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001484 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1485 Location value_loc = locations->InAt(1);
1486 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1487 // to avoid a SIGBUS.
1488 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001489 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001490 if (value_loc.IsConstant()) {
1491 __ movb(Address(address, 0),
1492 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1493 } else {
1494 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1495 }
1496 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001497 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001498 if (value_loc.IsConstant()) {
1499 __ movw(Address(address, 0),
1500 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1501 } else {
1502 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1503 }
1504 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001505 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001506 if (value_loc.IsConstant()) {
1507 __ movl(Address(address, 0),
1508 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1509 } else {
1510 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1511 }
1512 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001513 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001514 if (value_loc.IsConstant()) {
1515 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1516 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1517 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1518 } else {
1519 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1520 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1521 }
1522 break;
1523 default:
1524 LOG(FATAL) << "Type not recognized for poke: " << size;
1525 UNREACHABLE();
1526 }
1527}
1528
1529void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001530 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001531}
1532
1533void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001534 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001535}
1536
1537void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001538 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001539}
1540
1541void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001542 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001543}
1544
1545void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001546 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001547}
1548
1549void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001550 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001551}
1552
1553void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001554 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001555}
1556
1557void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001558 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001559}
1560
1561void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001562 LocationSummary* locations =
1563 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001564 locations->SetOut(Location::RequiresRegister());
1565}
1566
1567void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1568 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001569 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001570}
1571
Roland Levillain0d5a2812015-11-13 10:07:31 +00001572static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001574 bool is_volatile,
1575 CodeGeneratorX86* codegen) {
1576 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1577 LocationSummary* locations = invoke->GetLocations();
1578 Location base_loc = locations->InAt(1);
1579 Register base = base_loc.AsRegister<Register>();
1580 Location offset_loc = locations->InAt(2);
1581 Register offset = offset_loc.AsRegisterPairLow<Register>();
1582 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001583
1584 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001585 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001586 Register output = output_loc.AsRegister<Register>();
1587 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00001588 break;
1589 }
1590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001591 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001592 Register output = output_loc.AsRegister<Register>();
1593 if (kEmitCompilerReadBarrier) {
1594 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08001595 Address src(base, offset, ScaleFactor::TIMES_1, 0);
1596 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00001597 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00001598 } else {
1599 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1600 codegen->GenerateReadBarrierSlow(
1601 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
1602 }
1603 } else {
1604 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
1605 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01001606 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001607 break;
Roland Levillain4d027112015-07-01 15:41:14 +01001608 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001610 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001611 Register output_lo = output_loc.AsRegisterPairLow<Register>();
1612 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001613 if (is_volatile) {
1614 // Need to use a XMM to read atomically.
1615 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1616 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
1617 __ movd(output_lo, temp);
1618 __ psrlq(temp, Immediate(32));
1619 __ movd(output_hi, temp);
1620 } else {
1621 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
1622 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
1623 }
1624 }
1625 break;
1626
1627 default:
1628 LOG(FATAL) << "Unsupported op size " << type;
1629 UNREACHABLE();
1630 }
1631}
1632
Vladimir Markoca6fff82017-10-03 14:49:14 +01001633static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001634 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001635 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00001636 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001637 bool can_call = kEmitCompilerReadBarrier &&
1638 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
1639 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001640 LocationSummary* locations =
1641 new (allocator) LocationSummary(invoke,
1642 can_call
1643 ? LocationSummary::kCallOnSlowPath
1644 : LocationSummary::kNoCall,
1645 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01001646 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01001647 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01001648 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001649 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1650 locations->SetInAt(1, Location::RequiresRegister());
1651 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001652 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001653 if (is_volatile) {
1654 // Need to use XMM to read volatile.
1655 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01001656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001657 } else {
1658 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1659 }
1660 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001661 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001662 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001663 }
1664}
1665
1666void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001667 CreateIntIntIntToIntLocations(
1668 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001669}
1670void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001671 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001672}
1673void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001674 CreateIntIntIntToIntLocations(
1675 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001676}
1677void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001678 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001679}
1680void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001681 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001682 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001683}
1684void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001685 CreateIntIntIntToIntLocations(
1686 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001687}
1688
1689
1690void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001691 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001692}
1693void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001694 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001695}
1696void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001697 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001698}
1699void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001700 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001701}
1702void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001703 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001704}
1705void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001706 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001707}
1708
1709
Vladimir Markoca6fff82017-10-03 14:49:14 +01001710static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001711 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001712 HInvoke* invoke,
1713 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001714 LocationSummary* locations =
1715 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001716 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1717 locations->SetInAt(1, Location::RequiresRegister());
1718 locations->SetInAt(2, Location::RequiresRegister());
1719 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001720 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001721 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01001722 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04001723 // Ensure the value is in a byte register.
1724 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001725 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001726 locations->AddTemp(Location::RequiresFpuRegister());
1727 locations->AddTemp(Location::RequiresFpuRegister());
1728 }
1729}
1730
1731void IntrinsicLocationsBuilderX86::VisitUnsafePut(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 */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001734}
1735void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001736 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001737 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001738}
1739void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001740 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001741 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001742}
1743void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(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 */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001746}
1747void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001748 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001749 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001750}
1751void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001752 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001753 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001754}
1755void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(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 */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001758}
1759void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001760 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001761 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001762}
1763void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00001764 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001765 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001766}
1767
1768// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
1769// memory model.
1770static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001772 bool is_volatile,
1773 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001774 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001775 Register base = locations->InAt(1).AsRegister<Register>();
1776 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1777 Location value_loc = locations->InAt(3);
1778
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001779 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001780 Register value_lo = value_loc.AsRegisterPairLow<Register>();
1781 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
1782 if (is_volatile) {
1783 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
1784 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
1785 __ movd(temp1, value_lo);
1786 __ movd(temp2, value_hi);
1787 __ punpckldq(temp1, temp2);
1788 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
1789 } else {
1790 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
1791 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
1792 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001793 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01001794 Register temp = locations->GetTemp(0).AsRegister<Register>();
1795 __ movl(temp, value_loc.AsRegister<Register>());
1796 __ PoisonHeapReference(temp);
1797 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001798 } else {
1799 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
1800 }
1801
1802 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00001803 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001804 }
1805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001806 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001807 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04001808 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
1809 locations->GetTemp(1).AsRegister<Register>(),
1810 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01001811 value_loc.AsRegister<Register>(),
1812 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001813 }
1814}
1815
1816void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001818}
1819void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001820 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001821}
1822void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001823 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001824}
1825void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 GenUnsafePut(
1827 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001828}
1829void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001830 GenUnsafePut(
1831 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001832}
1833void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001834 GenUnsafePut(
1835 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001836}
1837void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001838 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001839}
1840void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001841 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001842}
1843void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001844 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001845}
1846
Vladimir Markoca6fff82017-10-03 14:49:14 +01001847static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04001849 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001850 bool can_call = kEmitCompilerReadBarrier &&
1851 kUseBakerReadBarrier &&
1852 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001853 LocationSummary* locations =
1854 new (allocator) LocationSummary(invoke,
1855 can_call
1856 ? LocationSummary::kCallOnSlowPath
1857 : LocationSummary::kNoCall,
1858 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001859 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
1860 locations->SetInAt(1, Location::RequiresRegister());
1861 // Offset is a long, but in 32 bit mode, we only need the low word.
1862 // Can we update the invoke here to remove a TypeConvert to Long?
1863 locations->SetInAt(2, Location::RequiresRegister());
1864 // Expected value must be in EAX or EDX:EAX.
1865 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001866 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04001867 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
1868 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
1869 } else {
1870 locations->SetInAt(3, Location::RegisterLocation(EAX));
1871 locations->SetInAt(4, Location::RequiresRegister());
1872 }
1873
1874 // Force a byte register for the output.
1875 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001877 // Need temporary registers for card-marking, and possibly for
1878 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01001879 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001880 // Need a byte register for marking.
1881 locations->AddTemp(Location::RegisterLocation(ECX));
1882 }
1883}
1884
1885void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001886 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001887}
1888
1889void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001890 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001891}
1892
1893void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001894 // The only read barrier implementation supporting the
1895 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1896 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00001897 return;
1898 }
1899
Vladimir Markoca6fff82017-10-03 14:49:14 +01001900 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001901}
1902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001903static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01001904 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04001905 LocationSummary* locations = invoke->GetLocations();
1906
1907 Register base = locations->InAt(1).AsRegister<Register>();
1908 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
1909 Location out = locations->Out();
1910 DCHECK_EQ(out.AsRegister<Register>(), EAX);
1911
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001912 // The address of the field within the holding object.
1913 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
1914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001915 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001916 // The only read barrier implementation supporting the
1917 // UnsafeCASObject intrinsic is the Baker-style read barriers.
1918 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
1919
1920 Location temp1_loc = locations->GetTemp(0);
1921 Register temp1 = temp1_loc.AsRegister<Register>();
1922 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
1923
Roland Levillain4d027112015-07-01 15:41:14 +01001924 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01001925 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01001926 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001927 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01001928
Roland Levillainb488b782015-10-22 11:38:49 +01001929 // Mark card for object assuming new value is stored.
1930 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001931 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
1932
1933 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
1934 // Need to make sure the reference stored in the field is a to-space
1935 // one before attempting the CAS or the CAS could fail incorrectly.
1936 codegen->GenerateReferenceLoadWithBakerReadBarrier(
1937 invoke,
1938 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
1939 base,
1940 field_addr,
1941 /* needs_null_check */ false,
1942 /* always_update_field */ true,
1943 &temp2);
1944 }
Roland Levillainb488b782015-10-22 11:38:49 +01001945
1946 bool base_equals_value = (base == value);
1947 if (kPoisonHeapReferences) {
1948 if (base_equals_value) {
1949 // If `base` and `value` are the same register location, move
1950 // `value` to a temporary register. This way, poisoning
1951 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001952 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01001953 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01001954 }
Roland Levillainb488b782015-10-22 11:38:49 +01001955
1956 // Check that the register allocator did not assign the location
1957 // of `expected` (EAX) to `value` nor to `base`, so that heap
1958 // poisoning (when enabled) works as intended below.
1959 // - If `value` were equal to `expected`, both references would
1960 // be poisoned twice, meaning they would not be poisoned at
1961 // all, as heap poisoning uses address negation.
1962 // - If `base` were equal to `expected`, poisoning `expected`
1963 // would invalidate `base`.
1964 DCHECK_NE(value, expected);
1965 DCHECK_NE(base, expected);
1966
1967 __ PoisonHeapReference(expected);
1968 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001969 }
1970
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001971 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04001972
Roland Levillain0d5a2812015-11-13 10:07:31 +00001973 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01001974 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04001975
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001976 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01001977 __ setb(kZero, out.AsRegister<Register>());
1978 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01001979
Roland Levillain391b8662015-12-18 11:43:38 +00001980 // If heap poisoning is enabled, we need to unpoison the values
1981 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01001982 if (kPoisonHeapReferences) {
1983 if (base_equals_value) {
1984 // `value` has been moved to a temporary register, no need to
1985 // unpoison it.
1986 } else {
1987 // Ensure `value` is different from `out`, so that unpoisoning
1988 // the former does not invalidate the latter.
1989 DCHECK_NE(value, out.AsRegister<Register>());
1990 __ UnpoisonHeapReference(value);
1991 }
1992 // Do not unpoison the reference contained in register
1993 // `expected`, as it is the same as register `out` (EAX).
1994 }
1995 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01001997 // Ensure the expected value is in EAX (required by the CMPXCHG
1998 // instruction).
1999 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002000 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002001 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002002 // Ensure the expected value is in EAX:EDX and that the new
2003 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2004 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2005 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2006 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2007 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002008 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002009 } else {
2010 LOG(FATAL) << "Unexpected CAS type " << type;
2011 }
2012
Roland Levillain0d5a2812015-11-13 10:07:31 +00002013 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2014 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002015
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002016 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002017 __ setb(kZero, out.AsRegister<Register>());
2018 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002019 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002020}
2021
2022void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002024}
2025
2026void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002028}
2029
2030void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002031 // The only read barrier implementation supporting the
2032 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2033 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002035 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002036}
2037
2038void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002039 LocationSummary* locations =
2040 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002041 locations->SetInAt(0, Location::RequiresRegister());
2042 locations->SetOut(Location::SameAsFirstInput());
2043 locations->AddTemp(Location::RequiresRegister());
2044}
2045
2046static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2047 X86Assembler* assembler) {
2048 Immediate imm_shift(shift);
2049 Immediate imm_mask(mask);
2050 __ movl(temp, reg);
2051 __ shrl(reg, imm_shift);
2052 __ andl(temp, imm_mask);
2053 __ andl(reg, imm_mask);
2054 __ shll(temp, imm_shift);
2055 __ orl(reg, temp);
2056}
2057
2058void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002059 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002060 LocationSummary* locations = invoke->GetLocations();
2061
2062 Register reg = locations->InAt(0).AsRegister<Register>();
2063 Register temp = locations->GetTemp(0).AsRegister<Register>();
2064
2065 /*
2066 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2067 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2068 * compared to generic luni implementation which has 5 rounds of swapping bits.
2069 * x = bswap x
2070 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2071 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2072 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2073 */
2074 __ bswapl(reg);
2075 SwapBits(reg, temp, 1, 0x55555555, assembler);
2076 SwapBits(reg, temp, 2, 0x33333333, assembler);
2077 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2078}
2079
2080void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002081 LocationSummary* locations =
2082 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002083 locations->SetInAt(0, Location::RequiresRegister());
2084 locations->SetOut(Location::SameAsFirstInput());
2085 locations->AddTemp(Location::RequiresRegister());
2086}
2087
2088void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002089 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002090 LocationSummary* locations = invoke->GetLocations();
2091
2092 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2093 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2094 Register temp = locations->GetTemp(0).AsRegister<Register>();
2095
2096 // We want to swap high/low, then bswap each one, and then do the same
2097 // as a 32 bit reverse.
2098 // Exchange high and low.
2099 __ movl(temp, reg_low);
2100 __ movl(reg_low, reg_high);
2101 __ movl(reg_high, temp);
2102
2103 // bit-reverse low
2104 __ bswapl(reg_low);
2105 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2106 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2107 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2108
2109 // bit-reverse high
2110 __ bswapl(reg_high);
2111 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2112 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2113 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2114}
2115
Aart Bikc39dac12016-01-21 08:59:48 -08002116static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002117 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002118 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2119 // Do nothing if there is no popcnt support. This results in generating
2120 // a call for the intrinsic rather than direct code.
2121 return;
2122 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002123 LocationSummary* locations =
2124 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002125 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002126 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002127 }
Aart Bik2a946072016-01-21 12:49:00 -08002128 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002129 locations->SetOut(Location::RequiresRegister());
2130}
2131
Aart Bika19616e2016-02-01 18:57:58 -08002132static void GenBitCount(X86Assembler* assembler,
2133 CodeGeneratorX86* codegen,
2134 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002135 LocationSummary* locations = invoke->GetLocations();
2136 Location src = locations->InAt(0);
2137 Register out = locations->Out().AsRegister<Register>();
2138
2139 if (invoke->InputAt(0)->IsConstant()) {
2140 // Evaluate this at compile time.
2141 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002142 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002143 ? POPCOUNT(static_cast<uint64_t>(value))
2144 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002145 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002146 return;
2147 }
2148
2149 // Handle the non-constant cases.
2150 if (!is_long) {
2151 if (src.IsRegister()) {
2152 __ popcntl(out, src.AsRegister<Register>());
2153 } else {
2154 DCHECK(src.IsStackSlot());
2155 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2156 }
Aart Bik2a946072016-01-21 12:49:00 -08002157 } else {
2158 // The 64-bit case needs to worry about two parts.
2159 Register temp = locations->GetTemp(0).AsRegister<Register>();
2160 if (src.IsRegisterPair()) {
2161 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2162 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2163 } else {
2164 DCHECK(src.IsDoubleStackSlot());
2165 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2166 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2167 }
2168 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002169 }
Aart Bikc39dac12016-01-21 08:59:48 -08002170}
2171
2172void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002173 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002174}
2175
2176void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002177 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002178}
2179
2180void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002181 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002182}
2183
2184void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002185 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002186}
2187
Vladimir Markoca6fff82017-10-03 14:49:14 +01002188static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2189 LocationSummary* locations =
2190 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002191 if (is_long) {
2192 locations->SetInAt(0, Location::RequiresRegister());
2193 } else {
2194 locations->SetInAt(0, Location::Any());
2195 }
2196 locations->SetOut(Location::RequiresRegister());
2197}
2198
Aart Bika19616e2016-02-01 18:57:58 -08002199static void GenLeadingZeros(X86Assembler* assembler,
2200 CodeGeneratorX86* codegen,
2201 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002202 LocationSummary* locations = invoke->GetLocations();
2203 Location src = locations->InAt(0);
2204 Register out = locations->Out().AsRegister<Register>();
2205
2206 if (invoke->InputAt(0)->IsConstant()) {
2207 // Evaluate this at compile time.
2208 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2209 if (value == 0) {
2210 value = is_long ? 64 : 32;
2211 } else {
2212 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2213 }
Aart Bika19616e2016-02-01 18:57:58 -08002214 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002215 return;
2216 }
2217
2218 // Handle the non-constant cases.
2219 if (!is_long) {
2220 if (src.IsRegister()) {
2221 __ bsrl(out, src.AsRegister<Register>());
2222 } else {
2223 DCHECK(src.IsStackSlot());
2224 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2225 }
2226
2227 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002228 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002229 __ j(kEqual, &all_zeroes);
2230
2231 // Correct the result from BSR to get the final CLZ result.
2232 __ xorl(out, Immediate(31));
2233 __ jmp(&done);
2234
2235 // Fix the zero case with the expected result.
2236 __ Bind(&all_zeroes);
2237 __ movl(out, Immediate(32));
2238
2239 __ Bind(&done);
2240 return;
2241 }
2242
2243 // 64 bit case needs to worry about both parts of the register.
2244 DCHECK(src.IsRegisterPair());
2245 Register src_lo = src.AsRegisterPairLow<Register>();
2246 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002247 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002248
2249 // Is the high word zero?
2250 __ testl(src_hi, src_hi);
2251 __ j(kEqual, &handle_low);
2252
2253 // High word is not zero. We know that the BSR result is defined in this case.
2254 __ bsrl(out, src_hi);
2255
2256 // Correct the result from BSR to get the final CLZ result.
2257 __ xorl(out, Immediate(31));
2258 __ jmp(&done);
2259
2260 // High word was zero. We have to compute the low word count and add 32.
2261 __ Bind(&handle_low);
2262 __ bsrl(out, src_lo);
2263 __ j(kEqual, &all_zeroes);
2264
2265 // We had a valid result. Use an XOR to both correct the result and add 32.
2266 __ xorl(out, Immediate(63));
2267 __ jmp(&done);
2268
2269 // All zero case.
2270 __ Bind(&all_zeroes);
2271 __ movl(out, Immediate(64));
2272
2273 __ Bind(&done);
2274}
2275
2276void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002277 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002278}
2279
2280void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002281 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002282}
2283
2284void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002285 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002286}
2287
2288void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002289 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002290}
2291
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2293 LocationSummary* locations =
2294 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002295 if (is_long) {
2296 locations->SetInAt(0, Location::RequiresRegister());
2297 } else {
2298 locations->SetInAt(0, Location::Any());
2299 }
2300 locations->SetOut(Location::RequiresRegister());
2301}
2302
Aart Bika19616e2016-02-01 18:57:58 -08002303static void GenTrailingZeros(X86Assembler* assembler,
2304 CodeGeneratorX86* codegen,
2305 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002306 LocationSummary* locations = invoke->GetLocations();
2307 Location src = locations->InAt(0);
2308 Register out = locations->Out().AsRegister<Register>();
2309
2310 if (invoke->InputAt(0)->IsConstant()) {
2311 // Evaluate this at compile time.
2312 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2313 if (value == 0) {
2314 value = is_long ? 64 : 32;
2315 } else {
2316 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2317 }
Aart Bika19616e2016-02-01 18:57:58 -08002318 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002319 return;
2320 }
2321
2322 // Handle the non-constant cases.
2323 if (!is_long) {
2324 if (src.IsRegister()) {
2325 __ bsfl(out, src.AsRegister<Register>());
2326 } else {
2327 DCHECK(src.IsStackSlot());
2328 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2329 }
2330
2331 // BSF sets ZF if the input was zero, and the output is undefined.
2332 NearLabel done;
2333 __ j(kNotEqual, &done);
2334
2335 // Fix the zero case with the expected result.
2336 __ movl(out, Immediate(32));
2337
2338 __ Bind(&done);
2339 return;
2340 }
2341
2342 // 64 bit case needs to worry about both parts of the register.
2343 DCHECK(src.IsRegisterPair());
2344 Register src_lo = src.AsRegisterPairLow<Register>();
2345 Register src_hi = src.AsRegisterPairHigh<Register>();
2346 NearLabel done, all_zeroes;
2347
2348 // If the low word is zero, then ZF will be set. If not, we have the answer.
2349 __ bsfl(out, src_lo);
2350 __ j(kNotEqual, &done);
2351
2352 // Low word was zero. We have to compute the high word count and add 32.
2353 __ bsfl(out, src_hi);
2354 __ j(kEqual, &all_zeroes);
2355
2356 // We had a valid result. Add 32 to account for the low word being zero.
2357 __ addl(out, Immediate(32));
2358 __ jmp(&done);
2359
2360 // All zero case.
2361 __ Bind(&all_zeroes);
2362 __ movl(out, Immediate(64));
2363
2364 __ Bind(&done);
2365}
2366
2367void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002368 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002369}
2370
2371void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002372 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002373}
2374
2375void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002376 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002377}
2378
2379void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002380 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002381}
2382
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002383static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2384 return instruction->InputAt(input0) == instruction->InputAt(input1);
2385}
2386
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002387// Compute base address for the System.arraycopy intrinsic in `base`.
2388static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002389 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002390 const Register& array,
2391 const Location& pos,
2392 const Register& base) {
2393 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002394 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002395 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002396 DCHECK_EQ(type, DataType::Type::kReference);
2397 const int32_t element_size = DataType::Size(type);
2398 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002399 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2400
2401 if (pos.IsConstant()) {
2402 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2403 __ leal(base, Address(array, element_size * constant + data_offset));
2404 } else {
2405 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2406 }
2407}
2408
2409// Compute end source address for the System.arraycopy intrinsic in `end`.
2410static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002412 const Location& copy_length,
2413 const Register& base,
2414 const Register& end) {
2415 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002417 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 DCHECK_EQ(type, DataType::Type::kReference);
2419 const int32_t element_size = DataType::Size(type);
2420 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002421
2422 if (copy_length.IsConstant()) {
2423 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2424 __ leal(end, Address(base, element_size * constant));
2425 } else {
2426 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2427 }
2428}
2429
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002430void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002431 // The only read barrier implementation supporting the
2432 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2433 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002434 return;
2435 }
2436
2437 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2438 if (invoke->GetLocations() != nullptr) {
2439 // Need a byte register for marking.
2440 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2441
2442 static constexpr size_t kSrc = 0;
2443 static constexpr size_t kSrcPos = 1;
2444 static constexpr size_t kDest = 2;
2445 static constexpr size_t kDestPos = 3;
2446 static constexpr size_t kLength = 4;
2447
2448 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2449 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2450 !invoke->InputAt(kLength)->IsIntConstant()) {
2451 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2452 !IsSameInput(invoke, kSrcPos, kLength) &&
2453 !IsSameInput(invoke, kDestPos, kLength) &&
2454 !IsSameInput(invoke, kSrc, kDest)) {
2455 // Not enough registers, make the length also take a stack slot.
2456 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2457 }
2458 }
2459 }
2460}
2461
2462void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002463 // The only read barrier implementation supporting the
2464 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2465 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002466
2467 X86Assembler* assembler = GetAssembler();
2468 LocationSummary* locations = invoke->GetLocations();
2469
2470 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2471 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2472 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2473 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002474 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002475
2476 Register src = locations->InAt(0).AsRegister<Register>();
2477 Location src_pos = locations->InAt(1);
2478 Register dest = locations->InAt(2).AsRegister<Register>();
2479 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002480 Location length_arg = locations->InAt(4);
2481 Location length = length_arg;
2482 Location temp1_loc = locations->GetTemp(0);
2483 Register temp1 = temp1_loc.AsRegister<Register>();
2484 Location temp2_loc = locations->GetTemp(1);
2485 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002486
Vladimir Marko174b2e22017-10-12 13:34:49 +01002487 SlowPathCode* intrinsic_slow_path =
2488 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002489 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002490
2491 NearLabel conditions_on_positions_validated;
2492 SystemArrayCopyOptimizations optimizations(invoke);
2493
2494 // If source and destination are the same, we go to slow path if we need to do
2495 // forward copying.
2496 if (src_pos.IsConstant()) {
2497 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2498 if (dest_pos.IsConstant()) {
2499 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2500 if (optimizations.GetDestinationIsSource()) {
2501 // Checked when building locations.
2502 DCHECK_GE(src_pos_constant, dest_pos_constant);
2503 } else if (src_pos_constant < dest_pos_constant) {
2504 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002505 __ j(kEqual, 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 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002513 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002514 }
2515 } else {
2516 if (!optimizations.GetDestinationIsSource()) {
2517 __ cmpl(src, dest);
2518 __ j(kNotEqual, &conditions_on_positions_validated);
2519 }
2520 if (dest_pos.IsConstant()) {
2521 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2522 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002523 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002524 } else {
2525 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002526 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002527 }
2528 }
2529
2530 __ Bind(&conditions_on_positions_validated);
2531
2532 if (!optimizations.GetSourceIsNotNull()) {
2533 // Bail out if the source is null.
2534 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002535 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002536 }
2537
2538 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2539 // Bail out if the destination is null.
2540 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002541 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002542 }
2543
Roland Levillain0b671c02016-08-19 12:02:34 +01002544 Location temp3_loc = locations->GetTemp(2);
2545 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002546 if (length.IsStackSlot()) {
2547 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2548 length = Location::RegisterLocation(temp3);
2549 }
2550
2551 // If the length is negative, bail out.
2552 // We have already checked in the LocationsBuilder for the constant case.
2553 if (!length.IsConstant() &&
2554 !optimizations.GetCountIsSourceLength() &&
2555 !optimizations.GetCountIsDestinationLength()) {
2556 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002557 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002558 }
2559
2560 // Validity checks: source.
2561 CheckPosition(assembler,
2562 src_pos,
2563 src,
2564 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002565 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002566 temp1,
2567 optimizations.GetCountIsSourceLength());
2568
2569 // Validity checks: dest.
2570 CheckPosition(assembler,
2571 dest_pos,
2572 dest,
2573 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002574 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002575 temp1,
2576 optimizations.GetCountIsDestinationLength());
2577
2578 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2579 // Check whether all elements of the source array are assignable to the component
2580 // type of the destination array. We do two checks: the classes are the same,
2581 // or the destination is Object[]. If none of these checks succeed, we go to the
2582 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01002583
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002584 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002585 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2586 // /* HeapReference<Class> */ temp1 = src->klass_
2587 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002588 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002589 // Bail out if the source is not a non primitive array.
2590 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2591 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002592 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002593 __ testl(temp1, temp1);
2594 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2595 // If heap poisoning is enabled, `temp1` has been unpoisoned
2596 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2597 } else {
2598 // /* HeapReference<Class> */ temp1 = src->klass_
2599 __ movl(temp1, Address(src, class_offset));
2600 __ MaybeUnpoisonHeapReference(temp1);
2601 // Bail out if the source is not a non primitive array.
2602 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2603 __ movl(temp1, Address(temp1, component_offset));
2604 __ testl(temp1, temp1);
2605 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2606 __ MaybeUnpoisonHeapReference(temp1);
2607 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002608 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002609 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002610 }
2611
Roland Levillain0b671c02016-08-19 12:02:34 +01002612 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2613 if (length.Equals(Location::RegisterLocation(temp3))) {
2614 // When Baker read barriers are enabled, register `temp3`,
2615 // which in the present case contains the `length` parameter,
2616 // will be overwritten below. Make the `length` location
2617 // reference the original stack location; it will be moved
2618 // back to `temp3` later if necessary.
2619 DCHECK(length_arg.IsStackSlot());
2620 length = length_arg;
2621 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002622
Roland Levillain0b671c02016-08-19 12:02:34 +01002623 // /* HeapReference<Class> */ temp1 = dest->klass_
2624 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002625 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002626
Roland Levillain0b671c02016-08-19 12:02:34 +01002627 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2628 // Bail out if the destination is not a non primitive array.
2629 //
2630 // Register `temp1` is not trashed by the read barrier emitted
2631 // by GenerateFieldLoadWithBakerReadBarrier below, as that
2632 // method produces a call to a ReadBarrierMarkRegX entry point,
2633 // which saves all potentially live registers, including
2634 // temporaries such a `temp1`.
2635 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2636 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002637 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002638 __ testl(temp2, temp2);
2639 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2640 // If heap poisoning is enabled, `temp2` has been unpoisoned
2641 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2642 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2643 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2644 }
2645
2646 // For the same reason given earlier, `temp1` is not trashed by the
2647 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
2648 // /* HeapReference<Class> */ temp2 = src->klass_
2649 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002650 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002651 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
2652 __ cmpl(temp1, temp2);
2653
2654 if (optimizations.GetDestinationIsTypedObjectArray()) {
2655 NearLabel do_copy;
2656 __ j(kEqual, &do_copy);
2657 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2658 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002659 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002660 // We do not need to emit a read barrier for the following
2661 // heap reference load, as `temp1` is only used in a
2662 // comparison with null below, and this reference is not
2663 // kept afterwards.
2664 __ cmpl(Address(temp1, super_offset), Immediate(0));
2665 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2666 __ Bind(&do_copy);
2667 } else {
2668 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2669 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002670 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01002671 // Non read barrier code.
2672
2673 // /* HeapReference<Class> */ temp1 = dest->klass_
2674 __ movl(temp1, Address(dest, class_offset));
2675 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
2676 __ MaybeUnpoisonHeapReference(temp1);
2677 // Bail out if the destination is not a non primitive array.
2678 // /* HeapReference<Class> */ temp2 = temp1->component_type_
2679 __ movl(temp2, Address(temp1, component_offset));
2680 __ testl(temp2, temp2);
2681 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2682 __ MaybeUnpoisonHeapReference(temp2);
2683 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
2684 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2685 // Re-poison the heap reference to make the compare instruction below
2686 // compare two poisoned references.
2687 __ PoisonHeapReference(temp1);
2688 }
2689
2690 // Note: if heap poisoning is on, we are comparing two poisoned references here.
2691 __ cmpl(temp1, Address(src, class_offset));
2692
2693 if (optimizations.GetDestinationIsTypedObjectArray()) {
2694 NearLabel do_copy;
2695 __ j(kEqual, &do_copy);
2696 __ MaybeUnpoisonHeapReference(temp1);
2697 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2698 __ movl(temp1, Address(temp1, component_offset));
2699 __ MaybeUnpoisonHeapReference(temp1);
2700 __ cmpl(Address(temp1, super_offset), Immediate(0));
2701 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2702 __ Bind(&do_copy);
2703 } else {
2704 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
2705 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002706 }
2707 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
2708 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
2709 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01002710 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2711 // /* HeapReference<Class> */ temp1 = src->klass_
2712 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002713 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002714 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2715 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002716 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01002717 __ testl(temp1, temp1);
2718 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2719 // If heap poisoning is enabled, `temp1` has been unpoisoned
2720 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
2721 } else {
2722 // /* HeapReference<Class> */ temp1 = src->klass_
2723 __ movl(temp1, Address(src, class_offset));
2724 __ MaybeUnpoisonHeapReference(temp1);
2725 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2726 __ movl(temp1, Address(temp1, component_offset));
2727 __ testl(temp1, temp1);
2728 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
2729 __ MaybeUnpoisonHeapReference(temp1);
2730 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002731 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01002732 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002733 }
2734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 const DataType::Type type = DataType::Type::kReference;
2736 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002737
Roland Levillain0b671c02016-08-19 12:02:34 +01002738 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002739 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002740
Roland Levillain0b671c02016-08-19 12:02:34 +01002741 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2742 // If it is needed (in the case of the fast-path loop), the base
2743 // destination address is computed later, as `temp2` is used for
2744 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002745
Roland Levillain0b671c02016-08-19 12:02:34 +01002746 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002747 if (length.IsStackSlot()) {
2748 // Location `length` is again pointing at a stack slot, as
2749 // register `temp3` (which was containing the length parameter
2750 // earlier) has been overwritten; restore it now
2751 DCHECK(length.Equals(length_arg));
2752 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2753 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002754 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002755 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002756
Roland Levillain0b671c02016-08-19 12:02:34 +01002757 // SystemArrayCopy implementation for Baker read barriers (see
2758 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
2759 //
2760 // if (src_ptr != end_ptr) {
2761 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
2762 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002763 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01002764 // if (is_gray) {
2765 // // Slow-path copy.
2766 // for (size_t i = 0; i != length; ++i) {
2767 // dest_array[dest_pos + i] =
2768 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
2769 // }
2770 // } else {
2771 // // Fast-path copy.
2772 // do {
2773 // *dest_ptr++ = *src_ptr++;
2774 // } while (src_ptr != end_ptr)
2775 // }
2776 // }
2777
2778 NearLabel loop, done;
2779
2780 // Don't enter copy loop if `length == 0`.
2781 __ cmpl(temp1, temp3);
2782 __ j(kEqual, &done);
2783
Vladimir Marko953437b2016-08-24 08:30:46 +00002784 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01002785 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002786 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00002787 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
2788 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
2789 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
2790
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07002791 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00002792 // goto slow_path;
2793 // At this point, just do the "if" and make sure that flags are preserved until the branch.
2794 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01002795
2796 // Load fence to prevent load-load reordering.
2797 // Note that this is a no-op, thanks to the x86 memory model.
2798 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
2799
2800 // Slow path used to copy array when `src` is gray.
2801 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01002802 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002803 codegen_->AddSlowPath(read_barrier_slow_path);
2804
Vladimir Marko953437b2016-08-24 08:30:46 +00002805 // We have done the "if" of the gray bit check above, now branch based on the flags.
2806 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01002807
2808 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002809 // Compute the base destination address in `temp2`.
2810 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002811 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2812 // poison/unpoison.
2813 __ Bind(&loop);
2814 __ pushl(Address(temp1, 0));
2815 __ cfi().AdjustCFAOffset(4);
2816 __ popl(Address(temp2, 0));
2817 __ cfi().AdjustCFAOffset(-4);
2818 __ addl(temp1, Immediate(element_size));
2819 __ addl(temp2, Immediate(element_size));
2820 __ cmpl(temp1, temp3);
2821 __ j(kNotEqual, &loop);
2822
2823 __ Bind(read_barrier_slow_path->GetExitLabel());
2824 __ Bind(&done);
2825 } else {
2826 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01002827 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002828 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01002829 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002830 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002831 // Iterate over the arrays and do a raw copy of the objects. We don't need to
2832 // poison/unpoison.
2833 NearLabel loop, done;
2834 __ cmpl(temp1, temp3);
2835 __ j(kEqual, &done);
2836 __ Bind(&loop);
2837 __ pushl(Address(temp1, 0));
2838 __ cfi().AdjustCFAOffset(4);
2839 __ popl(Address(temp2, 0));
2840 __ cfi().AdjustCFAOffset(-4);
2841 __ addl(temp1, Immediate(element_size));
2842 __ addl(temp2, Immediate(element_size));
2843 __ cmpl(temp1, temp3);
2844 __ j(kNotEqual, &loop);
2845 __ Bind(&done);
2846 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002847
2848 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002849 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002850
Roland Levillain0b671c02016-08-19 12:02:34 +01002851 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002852}
2853
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002854void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002855 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002856 InvokeRuntimeCallingConvention calling_convention;
2857 IntrinsicVisitor::ComputeIntegerValueOfLocations(
2858 invoke,
2859 codegen_,
2860 Location::RegisterLocation(EAX),
2861 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Markoeebb8212018-06-05 14:57:24 +01002862
2863 LocationSummary* locations = invoke->GetLocations();
2864 if (locations != nullptr) {
2865 HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect();
2866 if (invoke_static_or_direct->HasSpecialInput() &&
2867 invoke->InputAt(invoke_static_or_direct->GetSpecialInputIndex())
2868 ->IsX86ComputeBaseMethodAddress()) {
2869 locations->SetInAt(invoke_static_or_direct->GetSpecialInputIndex(),
2870 Location::RequiresRegister());
2871 }
2872 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002873}
2874
2875void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002876 DCHECK(invoke->IsInvokeStaticOrDirect());
Vladimir Marko6fd16062018-06-26 11:02:04 +01002877 IntrinsicVisitor::IntegerValueOfInfo info =
2878 IntrinsicVisitor::ComputeIntegerValueOfInfo(invoke, codegen_->GetCompilerOptions());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002879 LocationSummary* locations = invoke->GetLocations();
2880 X86Assembler* assembler = GetAssembler();
2881
2882 Register out = locations->Out().AsRegister<Register>();
2883 InvokeRuntimeCallingConvention calling_convention;
2884 if (invoke->InputAt(0)->IsConstant()) {
2885 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
Vladimir Marko6fd16062018-06-26 11:02:04 +01002886 if (static_cast<uint32_t>(value - info.low) < info.length) {
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002887 // Just embed the j.l.Integer in the code.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002888 DCHECK_NE(info.value_boot_image_reference, IntegerValueOfInfo::kInvalidReference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01002889 codegen_->LoadBootImageAddress(
Vladimir Marko6fd16062018-06-26 11:02:04 +01002890 out, info.value_boot_image_reference, invoke->AsInvokeStaticOrDirect());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002891 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002892 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002893 // Allocate and initialize a new j.l.Integer.
2894 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
2895 // JIT object table.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002896 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2897 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002898 __ movl(Address(out, info.value_offset), Immediate(value));
2899 }
2900 } else {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002901 DCHECK(locations->CanCall());
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002902 Register in = locations->InAt(0).AsRegister<Register>();
2903 // Check bounds of our cache.
2904 __ leal(out, Address(in, -info.low));
Vladimir Markoeebb8212018-06-05 14:57:24 +01002905 __ cmpl(out, Immediate(info.length));
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002906 NearLabel allocate, done;
2907 __ j(kAboveEqual, &allocate);
2908 // If the value is within the bounds, load the j.l.Integer directly from the array.
Vladimir Markoeebb8212018-06-05 14:57:24 +01002909 constexpr size_t kElementSize = sizeof(mirror::HeapReference<mirror::Object>);
Vladimir Markoeebb8212018-06-05 14:57:24 +01002910 static_assert((1u << TIMES_4) == sizeof(mirror::HeapReference<mirror::Object>),
2911 "Check heap reference size.");
Vladimir Marko6fd16062018-06-26 11:02:04 +01002912 if (codegen_->GetCompilerOptions().IsBootImage()) {
2913 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
2914 size_t method_address_index = invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
2915 HX86ComputeBaseMethodAddress* method_address =
2916 invoke->InputAt(method_address_index)->AsX86ComputeBaseMethodAddress();
2917 DCHECK(method_address != nullptr);
2918 Register method_address_reg =
2919 invoke->GetLocations()->InAt(method_address_index).AsRegister<Register>();
2920 __ movl(out, Address(method_address_reg, out, TIMES_4, CodeGeneratorX86::kDummy32BitOffset));
2921 codegen_->RecordBootImageIntrinsicPatch(method_address, info.array_data_boot_image_reference);
2922 } else {
2923 // Note: We're about to clobber the index in `out`, so we need to use `in` and
2924 // adjust the offset accordingly.
2925 uint32_t mid_array_boot_image_offset =
2926 info.array_data_boot_image_reference - info.low * kElementSize;
2927 codegen_->LoadBootImageAddress(
2928 out, mid_array_boot_image_offset, invoke->AsInvokeStaticOrDirect());
2929 DCHECK_NE(out, in);
2930 __ movl(out, Address(out, in, TIMES_4, 0));
2931 }
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002932 __ MaybeUnpoisonHeapReference(out);
2933 __ jmp(&done);
2934 __ Bind(&allocate);
2935 // Otherwise allocate and initialize a new j.l.Integer.
Vladimir Marko6fd16062018-06-26 11:02:04 +01002936 codegen_->AllocateInstanceForIntrinsic(invoke->AsInvokeStaticOrDirect(),
2937 info.integer_boot_image_offset);
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002938 __ movl(Address(out, info.value_offset), in);
2939 __ Bind(&done);
2940 }
2941}
2942
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002943void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002944 LocationSummary* locations =
2945 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002946 locations->SetOut(Location::RequiresRegister());
2947}
2948
2949void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
2950 X86Assembler* assembler = GetAssembler();
2951 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
2952 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
2953 NearLabel done;
2954 __ fs()->movl(out, address);
2955 __ testl(out, out);
2956 __ j(kEqual, &done);
2957 __ fs()->movl(address, Immediate(0));
2958 codegen_->MemoryFence();
2959 __ Bind(&done);
2960}
2961
Hans Boehmc7b28de2018-03-09 17:05:28 -08002962void IntrinsicLocationsBuilderX86::VisitReachabilityFence(HInvoke* invoke) {
2963 LocationSummary* locations =
2964 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
2965 locations->SetInAt(0, Location::Any());
2966}
2967
2968void IntrinsicCodeGeneratorX86::VisitReachabilityFence(HInvoke* invoke ATTRIBUTE_UNUSED) { }
Nicolas Geoffray365719c2017-03-08 13:11:50 +00002969
Aart Bik2f9fcc92016-03-01 15:16:54 -08002970UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00002971UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08002972UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
2973UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
2974UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
2975UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
2976UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
2977UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
Mark Mendell09ed1a32015-03-25 08:30:06 -04002978
Aart Bikff7d89c2016-11-07 08:49:28 -08002979UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
2980UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08002981UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
2982UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
2983UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
2984UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
2985UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
2986UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08002987
Aart Bik0e54c012016-03-04 12:08:31 -08002988// 1.8.
2989UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
2990UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
2991UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
2992UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
2993UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08002994
Aart Bik2f9fcc92016-03-01 15:16:54 -08002995UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01002996
2997#undef __
2998
Mark Mendell09ed1a32015-03-25 08:30:06 -04002999} // namespace x86
3000} // namespace art