blob: 46b7f3f1cee5132f0071e4fc31c470f5e79e0694 [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
43static constexpr int kDoubleNaNHigh = 0x7FF80000;
44static constexpr int kDoubleNaNLow = 0x00000000;
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000045static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
46static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
Mark Mendell09ed1a32015-03-25 08:30:06 -040047
Mark Mendellfb8d2792015-03-31 22:16:59 -040048IntrinsicLocationsBuilderX86::IntrinsicLocationsBuilderX86(CodeGeneratorX86* codegen)
Vladimir Markoca6fff82017-10-03 14:49:14 +010049 : allocator_(codegen->GetGraph()->GetAllocator()),
Mark P Mendell2f10a5f2016-01-25 14:47:50 +000050 codegen_(codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -040051}
52
53
Mark Mendell09ed1a32015-03-25 08:30:06 -040054X86Assembler* IntrinsicCodeGeneratorX86::GetAssembler() {
Roland Levillainb488b782015-10-22 11:38:49 +010055 return down_cast<X86Assembler*>(codegen_->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -040056}
57
58ArenaAllocator* IntrinsicCodeGeneratorX86::GetAllocator() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010059 return codegen_->GetGraph()->GetAllocator();
Mark Mendell09ed1a32015-03-25 08:30:06 -040060}
61
62bool IntrinsicLocationsBuilderX86::TryDispatch(HInvoke* invoke) {
63 Dispatch(invoke);
64 LocationSummary* res = invoke->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +000065 if (res == nullptr) {
66 return false;
67 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000068 return res->Intrinsified();
Mark Mendell09ed1a32015-03-25 08:30:06 -040069}
70
Roland Levillainec525fc2015-04-28 15:50:20 +010071static void MoveArguments(HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +010072 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Roland Levillainec525fc2015-04-28 15:50:20 +010073 IntrinsicVisitor::MoveArguments(invoke, codegen, &calling_convention_visitor);
Mark Mendell09ed1a32015-03-25 08:30:06 -040074}
75
Andreas Gampe85b62f22015-09-09 13:15:38 -070076using IntrinsicSlowPathX86 = IntrinsicSlowPath<InvokeDexCallingConventionVisitorX86>;
Mark Mendell09ed1a32015-03-25 08:30:06 -040077
Roland Levillain0b671c02016-08-19 12:02:34 +010078// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
79#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
80
81// Slow path implementing the SystemArrayCopy intrinsic copy loop with read barriers.
82class ReadBarrierSystemArrayCopySlowPathX86 : public SlowPathCode {
83 public:
84 explicit ReadBarrierSystemArrayCopySlowPathX86(HInstruction* instruction)
85 : SlowPathCode(instruction) {
86 DCHECK(kEmitCompilerReadBarrier);
87 DCHECK(kUseBakerReadBarrier);
88 }
89
90 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
91 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
92 LocationSummary* locations = instruction_->GetLocations();
93 DCHECK(locations->CanCall());
94 DCHECK(instruction_->IsInvokeStaticOrDirect())
95 << "Unexpected instruction in read barrier arraycopy slow path: "
96 << instruction_->DebugName();
97 DCHECK(instruction_->GetLocations()->Intrinsified());
98 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kSystemArrayCopy);
99
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100100 int32_t element_size = DataType::Size(DataType::Type::kReference);
Roland Levillain0b671c02016-08-19 12:02:34 +0100101 uint32_t offset = mirror::Array::DataOffset(element_size).Uint32Value();
102
103 Register src = locations->InAt(0).AsRegister<Register>();
104 Location src_pos = locations->InAt(1);
105 Register dest = locations->InAt(2).AsRegister<Register>();
106 Location dest_pos = locations->InAt(3);
107 Location length = locations->InAt(4);
108 Location temp1_loc = locations->GetTemp(0);
109 Register temp1 = temp1_loc.AsRegister<Register>();
110 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
111 Register temp3 = locations->GetTemp(2).AsRegister<Register>();
112
113 __ Bind(GetEntryLabel());
114 // In this code path, registers `temp1`, `temp2`, and `temp3`
115 // (resp.) are not used for the base source address, the base
116 // destination address, and the end source address (resp.), as in
117 // other SystemArrayCopy intrinsic code paths. Instead they are
118 // (resp.) used for:
119 // - the loop index (`i`);
120 // - the source index (`src_index`) and the loaded (source)
121 // reference (`value`); and
122 // - the destination index (`dest_index`).
123
124 // i = 0
125 __ xorl(temp1, temp1);
126 NearLabel loop;
127 __ Bind(&loop);
128 // value = src_array[i + src_pos]
129 if (src_pos.IsConstant()) {
130 int32_t constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
131 int32_t adjusted_offset = offset + constant * element_size;
132 __ movl(temp2, Address(src, temp1, ScaleFactor::TIMES_4, adjusted_offset));
133 } else {
134 __ leal(temp2, Address(src_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0));
135 __ movl(temp2, Address(src, temp2, ScaleFactor::TIMES_4, offset));
136 }
137 __ MaybeUnpoisonHeapReference(temp2);
138 // TODO: Inline the mark bit check before calling the runtime?
139 // value = ReadBarrier::Mark(value)
140 // No need to save live registers; it's taken care of by the
141 // entrypoint. Also, there is no need to update the stack mask,
142 // as this runtime call will not trigger a garbage collection.
143 // (See ReadBarrierMarkSlowPathX86::EmitNativeCode for more
144 // explanations.)
145 DCHECK_NE(temp2, ESP);
146 DCHECK(0 <= temp2 && temp2 < kNumberOfCpuRegisters) << temp2;
Roland Levillain97c46462017-05-11 14:04:03 +0100147 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +0100148 // This runtime call does not require a stack map.
149 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
150 __ MaybePoisonHeapReference(temp2);
151 // dest_array[i + dest_pos] = value
152 if (dest_pos.IsConstant()) {
153 int32_t constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
154 int32_t adjusted_offset = offset + constant * element_size;
155 __ movl(Address(dest, temp1, ScaleFactor::TIMES_4, adjusted_offset), temp2);
156 } else {
157 __ leal(temp3, Address(dest_pos.AsRegister<Register>(), temp1, ScaleFactor::TIMES_1, 0));
158 __ movl(Address(dest, temp3, ScaleFactor::TIMES_4, offset), temp2);
159 }
160 // ++i
161 __ addl(temp1, Immediate(1));
162 // if (i != length) goto loop
163 x86_codegen->GenerateIntCompare(temp1_loc, length);
164 __ j(kNotEqual, &loop);
165 __ jmp(GetExitLabel());
166 }
167
168 const char* GetDescription() const OVERRIDE { return "ReadBarrierSystemArrayCopySlowPathX86"; }
169
170 private:
171 DISALLOW_COPY_AND_ASSIGN(ReadBarrierSystemArrayCopySlowPathX86);
172};
173
174#undef __
175
Mark Mendell09ed1a32015-03-25 08:30:06 -0400176#define __ assembler->
177
Vladimir Markoca6fff82017-10-03 14:49:14 +0100178static void CreateFPToIntLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) {
179 LocationSummary* locations =
180 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400181 locations->SetInAt(0, Location::RequiresFpuRegister());
182 locations->SetOut(Location::RequiresRegister());
183 if (is64bit) {
184 locations->AddTemp(Location::RequiresFpuRegister());
185 }
186}
187
Vladimir Markoca6fff82017-10-03 14:49:14 +0100188static void CreateIntToFPLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is64bit) {
189 LocationSummary* locations =
190 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400191 locations->SetInAt(0, Location::RequiresRegister());
192 locations->SetOut(Location::RequiresFpuRegister());
193 if (is64bit) {
194 locations->AddTemp(Location::RequiresFpuRegister());
195 locations->AddTemp(Location::RequiresFpuRegister());
196 }
197}
198
199static void MoveFPToInt(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
200 Location input = locations->InAt(0);
201 Location output = locations->Out();
202 if (is64bit) {
203 // Need to use the temporary.
204 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
205 __ movsd(temp, input.AsFpuRegister<XmmRegister>());
206 __ movd(output.AsRegisterPairLow<Register>(), temp);
207 __ psrlq(temp, Immediate(32));
208 __ movd(output.AsRegisterPairHigh<Register>(), temp);
209 } else {
210 __ movd(output.AsRegister<Register>(), input.AsFpuRegister<XmmRegister>());
211 }
212}
213
214static void MoveIntToFP(LocationSummary* locations, bool is64bit, X86Assembler* assembler) {
215 Location input = locations->InAt(0);
216 Location output = locations->Out();
217 if (is64bit) {
218 // Need to use the temporary.
219 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
220 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
221 __ movd(temp1, input.AsRegisterPairLow<Register>());
222 __ movd(temp2, input.AsRegisterPairHigh<Register>());
223 __ punpckldq(temp1, temp2);
224 __ movsd(output.AsFpuRegister<XmmRegister>(), temp1);
225 } else {
226 __ movd(output.AsFpuRegister<XmmRegister>(), input.AsRegister<Register>());
227 }
228}
229
230void IntrinsicLocationsBuilderX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100231 CreateFPToIntLocations(allocator_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400232}
233void IntrinsicLocationsBuilderX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100234 CreateIntToFPLocations(allocator_, invoke, /* is64bit */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400235}
236
237void IntrinsicCodeGeneratorX86::VisitDoubleDoubleToRawLongBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000238 MoveFPToInt(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400239}
240void IntrinsicCodeGeneratorX86::VisitDoubleLongBitsToDouble(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000241 MoveIntToFP(invoke->GetLocations(), /* is64bit */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400242}
243
244void IntrinsicLocationsBuilderX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100245 CreateFPToIntLocations(allocator_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400246}
247void IntrinsicLocationsBuilderX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100248 CreateIntToFPLocations(allocator_, invoke, /* is64bit */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400249}
250
251void IntrinsicCodeGeneratorX86::VisitFloatFloatToRawIntBits(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000252 MoveFPToInt(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400253}
254void IntrinsicCodeGeneratorX86::VisitFloatIntBitsToFloat(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000255 MoveIntToFP(invoke->GetLocations(), /* is64bit */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400256}
257
Vladimir Markoca6fff82017-10-03 14:49:14 +0100258static void CreateIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
259 LocationSummary* locations =
260 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400261 locations->SetInAt(0, Location::RequiresRegister());
262 locations->SetOut(Location::SameAsFirstInput());
263}
264
Vladimir Markoca6fff82017-10-03 14:49:14 +0100265static void CreateLongToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
266 LocationSummary* locations =
267 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400268 locations->SetInAt(0, Location::RequiresRegister());
269 locations->SetOut(Location::RequiresRegister());
270}
271
Vladimir Markoca6fff82017-10-03 14:49:14 +0100272static void CreateLongToLongLocations(ArenaAllocator* allocator, HInvoke* invoke) {
273 LocationSummary* locations =
274 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400275 locations->SetInAt(0, Location::RequiresRegister());
276 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
277}
278
279static void GenReverseBytes(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100280 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -0400281 X86Assembler* assembler) {
282 Register out = locations->Out().AsRegister<Register>();
283
284 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100285 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -0400286 // TODO: Can be done with an xchg of 8b registers. This is straight from Quick.
287 __ bswapl(out);
288 __ sarl(out, Immediate(16));
289 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100290 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -0400291 __ bswapl(out);
292 break;
293 default:
294 LOG(FATAL) << "Unexpected size for reverse-bytes: " << size;
295 UNREACHABLE();
296 }
297}
298
299void IntrinsicLocationsBuilderX86::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100300 CreateIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400301}
302
303void IntrinsicCodeGeneratorX86::VisitIntegerReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100304 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400305}
306
Mark Mendell58d25fd2015-04-03 14:52:31 -0400307void IntrinsicLocationsBuilderX86::VisitLongReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100308 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -0400309}
310
311void IntrinsicCodeGeneratorX86::VisitLongReverseBytes(HInvoke* invoke) {
312 LocationSummary* locations = invoke->GetLocations();
313 Location input = locations->InAt(0);
314 Register input_lo = input.AsRegisterPairLow<Register>();
315 Register input_hi = input.AsRegisterPairHigh<Register>();
316 Location output = locations->Out();
317 Register output_lo = output.AsRegisterPairLow<Register>();
318 Register output_hi = output.AsRegisterPairHigh<Register>();
319
320 X86Assembler* assembler = GetAssembler();
321 // Assign the inputs to the outputs, mixing low/high.
322 __ movl(output_lo, input_hi);
323 __ movl(output_hi, input_lo);
324 __ bswapl(output_lo);
325 __ bswapl(output_hi);
326}
327
Mark Mendell09ed1a32015-03-25 08:30:06 -0400328void IntrinsicLocationsBuilderX86::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100329 CreateIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400330}
331
332void IntrinsicCodeGeneratorX86::VisitShortReverseBytes(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100333 GenReverseBytes(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400334}
335
336
337// TODO: Consider Quick's way of doing Double abs through integer operations, as the immediate we
338// need is 64b.
339
Vladimir Markoca6fff82017-10-03 14:49:14 +0100340static void CreateFloatToFloat(ArenaAllocator* allocator, HInvoke* invoke) {
Mark Mendell09ed1a32015-03-25 08:30:06 -0400341 // TODO: Enable memory operations when the assembler supports them.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100342 LocationSummary* locations =
343 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400344 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400345 locations->SetOut(Location::SameAsFirstInput());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000346 HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect();
347 DCHECK(static_or_direct != nullptr);
Nicolas Geoffray97793072016-02-16 15:33:54 +0000348 if (static_or_direct->HasSpecialInput() &&
349 invoke->InputAt(static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000350 // We need addressibility for the constant area.
351 locations->SetInAt(1, Location::RequiresRegister());
352 // We need a temporary to hold the constant.
353 locations->AddTemp(Location::RequiresFpuRegister());
354 }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400355}
356
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000357static void MathAbsFP(HInvoke* invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000358 bool is64bit,
359 X86Assembler* assembler,
360 CodeGeneratorX86* codegen) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000361 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -0400362 Location output = locations->Out();
363
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000364 DCHECK(output.IsFpuRegister());
Nicolas Geoffray97793072016-02-16 15:33:54 +0000365 if (locations->GetInputCount() == 2 && locations->InAt(1).IsValid()) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000366 HX86ComputeBaseMethodAddress* method_address =
367 invoke->InputAt(1)->AsX86ComputeBaseMethodAddress();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000368 DCHECK(locations->InAt(1).IsRegister());
369 // We also have a constant area pointer.
370 Register constant_area = locations->InAt(1).AsRegister<Register>();
371 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
372 if (is64bit) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000373 __ movsd(temp, codegen->LiteralInt64Address(
374 INT64_C(0x7FFFFFFFFFFFFFFF), method_address, constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000375 __ andpd(output.AsFpuRegister<XmmRegister>(), temp);
376 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000377 __ movss(temp, codegen->LiteralInt32Address(
378 INT32_C(0x7FFFFFFF), method_address, constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000379 __ andps(output.AsFpuRegister<XmmRegister>(), temp);
380 }
381 } else {
Mark Mendell09ed1a32015-03-25 08:30:06 -0400382 // Create the right constant on an aligned stack.
383 if (is64bit) {
384 __ subl(ESP, Immediate(8));
385 __ pushl(Immediate(0x7FFFFFFF));
386 __ pushl(Immediate(0xFFFFFFFF));
387 __ andpd(output.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
388 } else {
389 __ subl(ESP, Immediate(12));
390 __ pushl(Immediate(0x7FFFFFFF));
391 __ andps(output.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
392 }
393 __ addl(ESP, Immediate(16));
Mark Mendell09ed1a32015-03-25 08:30:06 -0400394 }
395}
396
397void IntrinsicLocationsBuilderX86::VisitMathAbsDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100398 CreateFloatToFloat(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400399}
400
401void IntrinsicCodeGeneratorX86::VisitMathAbsDouble(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000402 MathAbsFP(invoke, /* is64bit */ true, GetAssembler(), codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400403}
404
405void IntrinsicLocationsBuilderX86::VisitMathAbsFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100406 CreateFloatToFloat(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400407}
408
409void IntrinsicCodeGeneratorX86::VisitMathAbsFloat(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000410 MathAbsFP(invoke, /* is64bit */ false, GetAssembler(), codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400411}
412
Vladimir Markoca6fff82017-10-03 14:49:14 +0100413static void CreateAbsIntLocation(ArenaAllocator* allocator, HInvoke* invoke) {
414 LocationSummary* locations =
415 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400416 locations->SetInAt(0, Location::RegisterLocation(EAX));
417 locations->SetOut(Location::SameAsFirstInput());
418 locations->AddTemp(Location::RegisterLocation(EDX));
419}
420
421static void GenAbsInteger(LocationSummary* locations, X86Assembler* assembler) {
422 Location output = locations->Out();
423 Register out = output.AsRegister<Register>();
424 DCHECK_EQ(out, EAX);
425 Register temp = locations->GetTemp(0).AsRegister<Register>();
426 DCHECK_EQ(temp, EDX);
427
428 // Sign extend EAX into EDX.
429 __ cdq();
430
431 // XOR EAX with sign.
432 __ xorl(EAX, EDX);
433
434 // Subtract out sign to correct.
435 __ subl(EAX, EDX);
436
437 // The result is in EAX.
438}
439
Vladimir Markoca6fff82017-10-03 14:49:14 +0100440static void CreateAbsLongLocation(ArenaAllocator* allocator, HInvoke* invoke) {
441 LocationSummary* locations =
442 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400443 locations->SetInAt(0, Location::RequiresRegister());
444 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
445 locations->AddTemp(Location::RequiresRegister());
446}
447
448static void GenAbsLong(LocationSummary* locations, X86Assembler* assembler) {
449 Location input = locations->InAt(0);
450 Register input_lo = input.AsRegisterPairLow<Register>();
451 Register input_hi = input.AsRegisterPairHigh<Register>();
452 Location output = locations->Out();
453 Register output_lo = output.AsRegisterPairLow<Register>();
454 Register output_hi = output.AsRegisterPairHigh<Register>();
455 Register temp = locations->GetTemp(0).AsRegister<Register>();
456
457 // Compute the sign into the temporary.
458 __ movl(temp, input_hi);
459 __ sarl(temp, Immediate(31));
460
461 // Store the sign into the output.
462 __ movl(output_lo, temp);
463 __ movl(output_hi, temp);
464
465 // XOR the input to the output.
466 __ xorl(output_lo, input_lo);
467 __ xorl(output_hi, input_hi);
468
469 // Subtract the sign.
470 __ subl(output_lo, temp);
471 __ sbbl(output_hi, temp);
472}
473
474void IntrinsicLocationsBuilderX86::VisitMathAbsInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100475 CreateAbsIntLocation(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400476}
477
478void IntrinsicCodeGeneratorX86::VisitMathAbsInt(HInvoke* invoke) {
479 GenAbsInteger(invoke->GetLocations(), GetAssembler());
480}
481
482void IntrinsicLocationsBuilderX86::VisitMathAbsLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100483 CreateAbsLongLocation(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400484}
485
486void IntrinsicCodeGeneratorX86::VisitMathAbsLong(HInvoke* invoke) {
487 GenAbsLong(invoke->GetLocations(), GetAssembler());
488}
489
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000490static void GenMinMaxFP(HInvoke* invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000491 bool is_min,
492 bool is_double,
493 X86Assembler* assembler,
494 CodeGeneratorX86* codegen) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000495 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -0400496 Location op1_loc = locations->InAt(0);
497 Location op2_loc = locations->InAt(1);
498 Location out_loc = locations->Out();
499 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
500
501 // Shortcut for same input locations.
502 if (op1_loc.Equals(op2_loc)) {
503 DCHECK(out_loc.Equals(op1_loc));
504 return;
505 }
506
507 // (out := op1)
508 // out <=? op2
509 // if Nan jmp Nan_label
510 // if out is min jmp done
511 // if op2 is min jmp op2_label
512 // handle -0/+0
513 // jmp done
514 // Nan_label:
515 // out := NaN
516 // op2_label:
517 // out := op2
518 // done:
519 //
520 // This removes one jmp, but needs to copy one input (op1) to out.
521 //
522 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
523
524 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
525
Mark Mendell0c9497d2015-08-21 09:30:05 -0400526 NearLabel nan, done, op2_label;
Mark Mendell09ed1a32015-03-25 08:30:06 -0400527 if (is_double) {
528 __ ucomisd(out, op2);
529 } else {
530 __ ucomiss(out, op2);
531 }
532
533 __ j(Condition::kParityEven, &nan);
534
535 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
536 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
537
538 // Handle 0.0/-0.0.
539 if (is_min) {
540 if (is_double) {
541 __ orpd(out, op2);
542 } else {
543 __ orps(out, op2);
544 }
545 } else {
546 if (is_double) {
547 __ andpd(out, op2);
548 } else {
549 __ andps(out, op2);
550 }
551 }
552 __ jmp(&done);
553
554 // NaN handling.
555 __ Bind(&nan);
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000556 // Do we have a constant area pointer?
Nicolas Geoffray97793072016-02-16 15:33:54 +0000557 if (locations->GetInputCount() == 3 && locations->InAt(2).IsValid()) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000558 HX86ComputeBaseMethodAddress* method_address =
559 invoke->InputAt(2)->AsX86ComputeBaseMethodAddress();
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000560 DCHECK(locations->InAt(2).IsRegister());
561 Register constant_area = locations->InAt(2).AsRegister<Register>();
562 if (is_double) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000563 __ movsd(out, codegen->LiteralInt64Address(kDoubleNaN, method_address, constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000564 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000565 __ movss(out, codegen->LiteralInt32Address(kFloatNaN, method_address, constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000566 }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400567 } else {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000568 if (is_double) {
569 __ pushl(Immediate(kDoubleNaNHigh));
570 __ pushl(Immediate(kDoubleNaNLow));
571 __ movsd(out, Address(ESP, 0));
572 __ addl(ESP, Immediate(8));
573 } else {
574 __ pushl(Immediate(kFloatNaN));
575 __ movss(out, Address(ESP, 0));
576 __ addl(ESP, Immediate(4));
577 }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400578 }
579 __ jmp(&done);
580
581 // out := op2;
582 __ Bind(&op2_label);
583 if (is_double) {
584 __ movsd(out, op2);
585 } else {
586 __ movss(out, op2);
587 }
588
589 // Done.
590 __ Bind(&done);
591}
592
Vladimir Markoca6fff82017-10-03 14:49:14 +0100593static void CreateFPFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
594 LocationSummary* locations =
595 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400596 locations->SetInAt(0, Location::RequiresFpuRegister());
597 locations->SetInAt(1, Location::RequiresFpuRegister());
598 // The following is sub-optimal, but all we can do for now. It would be fine to also accept
599 // the second input to be the output (we can simply swap inputs).
600 locations->SetOut(Location::SameAsFirstInput());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000601 HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect();
602 DCHECK(static_or_direct != nullptr);
Nicolas Geoffray97793072016-02-16 15:33:54 +0000603 if (static_or_direct->HasSpecialInput() &&
604 invoke->InputAt(static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000605 locations->SetInAt(2, Location::RequiresRegister());
606 }
Mark Mendell09ed1a32015-03-25 08:30:06 -0400607}
608
609void IntrinsicLocationsBuilderX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100610 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400611}
612
613void IntrinsicCodeGeneratorX86::VisitMathMinDoubleDouble(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000614 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000615 /* is_min */ true,
616 /* is_double */ true,
617 GetAssembler(),
618 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400619}
620
621void IntrinsicLocationsBuilderX86::VisitMathMinFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100622 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400623}
624
625void IntrinsicCodeGeneratorX86::VisitMathMinFloatFloat(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000626 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000627 /* is_min */ true,
628 /* is_double */ false,
629 GetAssembler(),
630 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400631}
632
633void IntrinsicLocationsBuilderX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100634 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400635}
636
637void IntrinsicCodeGeneratorX86::VisitMathMaxDoubleDouble(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000638 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000639 /* is_min */ false,
640 /* is_double */ true,
641 GetAssembler(),
642 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400643}
644
645void IntrinsicLocationsBuilderX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100646 CreateFPFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400647}
648
649void IntrinsicCodeGeneratorX86::VisitMathMaxFloatFloat(HInvoke* invoke) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000650 GenMinMaxFP(invoke,
Mark P Mendell2f10a5f2016-01-25 14:47:50 +0000651 /* is_min */ false,
652 /* is_double */ false,
653 GetAssembler(),
654 codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400655}
656
657static void GenMinMax(LocationSummary* locations, bool is_min, bool is_long,
658 X86Assembler* assembler) {
659 Location op1_loc = locations->InAt(0);
660 Location op2_loc = locations->InAt(1);
661
662 // Shortcut for same input locations.
663 if (op1_loc.Equals(op2_loc)) {
664 // Can return immediately, as op1_loc == out_loc.
665 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
666 // a copy here.
667 DCHECK(locations->Out().Equals(op1_loc));
668 return;
669 }
670
671 if (is_long) {
672 // Need to perform a subtract to get the sign right.
673 // op1 is already in the same location as the output.
674 Location output = locations->Out();
675 Register output_lo = output.AsRegisterPairLow<Register>();
676 Register output_hi = output.AsRegisterPairHigh<Register>();
677
678 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
679 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
680
681 // Spare register to compute the subtraction to set condition code.
682 Register temp = locations->GetTemp(0).AsRegister<Register>();
683
684 // Subtract off op2_low.
685 __ movl(temp, output_lo);
686 __ subl(temp, op2_lo);
687
688 // Now use the same tempo and the borrow to finish the subtraction of op2_hi.
689 __ movl(temp, output_hi);
690 __ sbbl(temp, op2_hi);
691
692 // Now the condition code is correct.
693 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
694 __ cmovl(cond, output_lo, op2_lo);
695 __ cmovl(cond, output_hi, op2_hi);
696 } else {
697 Register out = locations->Out().AsRegister<Register>();
698 Register op2 = op2_loc.AsRegister<Register>();
699
700 // (out := op1)
701 // out <=? op2
702 // if out is min jmp done
703 // out := op2
704 // done:
705
706 __ cmpl(out, op2);
707 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
708 __ cmovl(cond, out, op2);
709 }
710}
711
Vladimir Markoca6fff82017-10-03 14:49:14 +0100712static void CreateIntIntToIntLocations(ArenaAllocator* allocator, HInvoke* invoke) {
713 LocationSummary* locations =
714 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400715 locations->SetInAt(0, Location::RequiresRegister());
716 locations->SetInAt(1, Location::RequiresRegister());
717 locations->SetOut(Location::SameAsFirstInput());
718}
719
Vladimir Markoca6fff82017-10-03 14:49:14 +0100720static void CreateLongLongToLongLocations(ArenaAllocator* allocator, HInvoke* invoke) {
721 LocationSummary* locations =
722 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400723 locations->SetInAt(0, Location::RequiresRegister());
724 locations->SetInAt(1, Location::RequiresRegister());
725 locations->SetOut(Location::SameAsFirstInput());
726 // Register to use to perform a long subtract to set cc.
727 locations->AddTemp(Location::RequiresRegister());
728}
729
730void IntrinsicLocationsBuilderX86::VisitMathMinIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100731 CreateIntIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400732}
733
734void IntrinsicCodeGeneratorX86::VisitMathMinIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000735 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400736}
737
738void IntrinsicLocationsBuilderX86::VisitMathMinLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100739 CreateLongLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400740}
741
742void IntrinsicCodeGeneratorX86::VisitMathMinLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000743 GenMinMax(invoke->GetLocations(), /* is_min */ true, /* is_long */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400744}
745
746void IntrinsicLocationsBuilderX86::VisitMathMaxIntInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100747 CreateIntIntToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400748}
749
750void IntrinsicCodeGeneratorX86::VisitMathMaxIntInt(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000751 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ false, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400752}
753
754void IntrinsicLocationsBuilderX86::VisitMathMaxLongLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100755 CreateLongLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400756}
757
758void IntrinsicCodeGeneratorX86::VisitMathMaxLongLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +0000759 GenMinMax(invoke->GetLocations(), /* is_min */ false, /* is_long */ true, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -0400760}
761
Vladimir Markoca6fff82017-10-03 14:49:14 +0100762static void CreateFPToFPLocations(ArenaAllocator* allocator, HInvoke* invoke) {
763 LocationSummary* locations =
764 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400765 locations->SetInAt(0, Location::RequiresFpuRegister());
766 locations->SetOut(Location::RequiresFpuRegister());
767}
768
769void IntrinsicLocationsBuilderX86::VisitMathSqrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100770 CreateFPToFPLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -0400771}
772
773void IntrinsicCodeGeneratorX86::VisitMathSqrt(HInvoke* invoke) {
774 LocationSummary* locations = invoke->GetLocations();
775 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
776 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
777
778 GetAssembler()->sqrtsd(out, in);
779}
780
Mark Mendellfb8d2792015-03-31 22:16:59 -0400781static void InvokeOutOfLineIntrinsic(CodeGeneratorX86* codegen, HInvoke* invoke) {
Roland Levillainec525fc2015-04-28 15:50:20 +0100782 MoveArguments(invoke, codegen);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400783
784 DCHECK(invoke->IsInvokeStaticOrDirect());
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100785 codegen->GenerateStaticOrDirectCall(invoke->AsInvokeStaticOrDirect(),
786 Location::RegisterLocation(EAX));
Mark Mendellfb8d2792015-03-31 22:16:59 -0400787
788 // Copy the result back to the expected output.
789 Location out = invoke->GetLocations()->Out();
790 if (out.IsValid()) {
791 DCHECK(out.IsRegister());
Andreas Gampe85b62f22015-09-09 13:15:38 -0700792 codegen->MoveFromReturnRegister(out, invoke->GetType());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400793 }
794}
795
Vladimir Markoca6fff82017-10-03 14:49:14 +0100796static void CreateSSE41FPToFPLocations(ArenaAllocator* allocator,
797 HInvoke* invoke,
798 CodeGeneratorX86* codegen) {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400799 // Do we have instruction support?
800 if (codegen->GetInstructionSetFeatures().HasSSE4_1()) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100801 CreateFPToFPLocations(allocator, invoke);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400802 return;
803 }
804
805 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100806 LocationSummary* locations =
807 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400808 InvokeRuntimeCallingConvention calling_convention;
809 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
810 locations->SetOut(Location::FpuRegisterLocation(XMM0));
811 // Needs to be EAX for the invoke.
812 locations->AddTemp(Location::RegisterLocation(EAX));
813}
814
815static void GenSSE41FPToFPIntrinsic(CodeGeneratorX86* codegen,
816 HInvoke* invoke,
817 X86Assembler* assembler,
818 int round_mode) {
819 LocationSummary* locations = invoke->GetLocations();
820 if (locations->WillCall()) {
821 InvokeOutOfLineIntrinsic(codegen, invoke);
822 } else {
823 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
824 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
825 __ roundsd(out, in, Immediate(round_mode));
826 }
827}
828
829void IntrinsicLocationsBuilderX86::VisitMathCeil(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100830 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400831}
832
833void IntrinsicCodeGeneratorX86::VisitMathCeil(HInvoke* invoke) {
834 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 2);
835}
836
837void IntrinsicLocationsBuilderX86::VisitMathFloor(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100838 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400839}
840
841void IntrinsicCodeGeneratorX86::VisitMathFloor(HInvoke* invoke) {
842 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 1);
843}
844
845void IntrinsicLocationsBuilderX86::VisitMathRint(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100846 CreateSSE41FPToFPLocations(allocator_, invoke, codegen_);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400847}
848
849void IntrinsicCodeGeneratorX86::VisitMathRint(HInvoke* invoke) {
850 GenSSE41FPToFPIntrinsic(codegen_, invoke, GetAssembler(), 0);
851}
852
Mark Mendellfb8d2792015-03-31 22:16:59 -0400853void IntrinsicLocationsBuilderX86::VisitMathRoundFloat(HInvoke* invoke) {
854 // Do we have instruction support?
855 if (codegen_->GetInstructionSetFeatures().HasSSE4_1()) {
Aart Bik2c9f4952016-08-01 16:52:27 -0700856 HInvokeStaticOrDirect* static_or_direct = invoke->AsInvokeStaticOrDirect();
857 DCHECK(static_or_direct != nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100858 LocationSummary* locations =
859 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400860 locations->SetInAt(0, Location::RequiresFpuRegister());
Aart Bik2c9f4952016-08-01 16:52:27 -0700861 if (static_or_direct->HasSpecialInput() &&
862 invoke->InputAt(
863 static_or_direct->GetSpecialInputIndex())->IsX86ComputeBaseMethodAddress()) {
864 locations->SetInAt(1, Location::RequiresRegister());
865 }
Nicolas Geoffrayd9b92402015-04-21 10:02:22 +0100866 locations->SetOut(Location::RequiresRegister());
Mark Mendellfb8d2792015-03-31 22:16:59 -0400867 locations->AddTemp(Location::RequiresFpuRegister());
868 locations->AddTemp(Location::RequiresFpuRegister());
869 return;
870 }
871
872 // We have to fall back to a call to the intrinsic.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100873 LocationSummary* locations =
874 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400875 InvokeRuntimeCallingConvention calling_convention;
876 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetFpuRegisterAt(0)));
877 locations->SetOut(Location::RegisterLocation(EAX));
878 // Needs to be EAX for the invoke.
879 locations->AddTemp(Location::RegisterLocation(EAX));
880}
881
882void IntrinsicCodeGeneratorX86::VisitMathRoundFloat(HInvoke* invoke) {
883 LocationSummary* locations = invoke->GetLocations();
Aart Bik2c9f4952016-08-01 16:52:27 -0700884 if (locations->WillCall()) { // TODO: can we reach this?
Mark Mendellfb8d2792015-03-31 22:16:59 -0400885 InvokeOutOfLineIntrinsic(codegen_, invoke);
886 return;
887 }
888
Mark Mendellfb8d2792015-03-31 22:16:59 -0400889 XmmRegister in = locations->InAt(0).AsFpuRegister<XmmRegister>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700890 XmmRegister t1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
891 XmmRegister t2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mark Mendellfb8d2792015-03-31 22:16:59 -0400892 Register out = locations->Out().AsRegister<Register>();
Aart Bik2c9f4952016-08-01 16:52:27 -0700893 NearLabel skip_incr, done;
Mark Mendellfb8d2792015-03-31 22:16:59 -0400894 X86Assembler* assembler = GetAssembler();
895
Aart Bik2c9f4952016-08-01 16:52:27 -0700896 // Since no direct x86 rounding instruction matches the required semantics,
897 // this intrinsic is implemented as follows:
898 // result = floor(in);
899 // if (in - result >= 0.5f)
900 // result = result + 1.0f;
901 __ movss(t2, in);
902 __ roundss(t1, in, Immediate(1));
903 __ subss(t2, t1);
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700904 if (locations->GetInputCount() == 2 && locations->InAt(1).IsValid()) {
905 // Direct constant area available.
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000906 HX86ComputeBaseMethodAddress* method_address =
907 invoke->InputAt(1)->AsX86ComputeBaseMethodAddress();
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700908 Register constant_area = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000909 __ comiss(t2, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(0.5f),
910 method_address,
911 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700912 __ j(kBelow, &skip_incr);
Nicolas Geoffray133719e2017-01-22 15:44:39 +0000913 __ addss(t1, codegen_->LiteralInt32Address(bit_cast<int32_t, float>(1.0f),
914 method_address,
915 constant_area));
Aart Bik0cf8d9c2016-08-10 14:05:54 -0700916 __ Bind(&skip_incr);
917 } else {
918 // No constant area: go through stack.
919 __ pushl(Immediate(bit_cast<int32_t, float>(0.5f)));
920 __ pushl(Immediate(bit_cast<int32_t, float>(1.0f)));
921 __ comiss(t2, Address(ESP, 4));
922 __ j(kBelow, &skip_incr);
923 __ addss(t1, Address(ESP, 0));
924 __ Bind(&skip_incr);
925 __ addl(ESP, Immediate(8));
926 }
Mark Mendellfb8d2792015-03-31 22:16:59 -0400927
Aart Bik2c9f4952016-08-01 16:52:27 -0700928 // Final conversion to an integer. Unfortunately this also does not have a
929 // direct x86 instruction, since NaN should map to 0 and large positive
930 // values need to be clipped to the extreme value.
Mark Mendellfb8d2792015-03-31 22:16:59 -0400931 __ movl(out, Immediate(kPrimIntMax));
Aart Bik2c9f4952016-08-01 16:52:27 -0700932 __ cvtsi2ss(t2, out);
933 __ comiss(t1, t2);
934 __ j(kAboveEqual, &done); // clipped to max (already in out), does not jump on unordered
935 __ movl(out, Immediate(0)); // does not change flags
936 __ j(kUnordered, &done); // NaN mapped to 0 (just moved in out)
937 __ cvttss2si(out, t1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400938 __ Bind(&done);
939}
940
Vladimir Markoca6fff82017-10-03 14:49:14 +0100941static void CreateFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
942 LocationSummary* locations =
943 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -0400944 InvokeRuntimeCallingConvention calling_convention;
945 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
946 locations->SetOut(Location::FpuRegisterLocation(XMM0));
947}
948
949static void GenFPToFPCall(HInvoke* invoke, CodeGeneratorX86* codegen, QuickEntrypointEnum entry) {
950 LocationSummary* locations = invoke->GetLocations();
951 DCHECK(locations->WillCall());
952 DCHECK(invoke->IsInvokeStaticOrDirect());
953 X86Assembler* assembler = codegen->GetAssembler();
954
955 // We need some place to pass the parameters.
956 __ subl(ESP, Immediate(16));
957 __ cfi().AdjustCFAOffset(16);
958
959 // Pass the parameters at the bottom of the stack.
960 __ movsd(Address(ESP, 0), XMM0);
961
962 // If we have a second parameter, pass it next.
963 if (invoke->GetNumberOfArguments() == 2) {
964 __ movsd(Address(ESP, 8), XMM1);
965 }
966
967 // Now do the actual call.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100968 codegen->InvokeRuntime(entry, invoke, invoke->GetDexPc());
Mark Mendella4f12202015-08-06 15:23:34 -0400969
970 // Extract the return value from the FP stack.
971 __ fstpl(Address(ESP, 0));
972 __ movsd(XMM0, Address(ESP, 0));
973
974 // And clean up the stack.
975 __ addl(ESP, Immediate(16));
976 __ cfi().AdjustCFAOffset(-16);
Mark Mendella4f12202015-08-06 15:23:34 -0400977}
978
979void IntrinsicLocationsBuilderX86::VisitMathCos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100980 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400981}
982
983void IntrinsicCodeGeneratorX86::VisitMathCos(HInvoke* invoke) {
984 GenFPToFPCall(invoke, codegen_, kQuickCos);
985}
986
987void IntrinsicLocationsBuilderX86::VisitMathSin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100988 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400989}
990
991void IntrinsicCodeGeneratorX86::VisitMathSin(HInvoke* invoke) {
992 GenFPToFPCall(invoke, codegen_, kQuickSin);
993}
994
995void IntrinsicLocationsBuilderX86::VisitMathAcos(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100996 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -0400997}
998
999void IntrinsicCodeGeneratorX86::VisitMathAcos(HInvoke* invoke) {
1000 GenFPToFPCall(invoke, codegen_, kQuickAcos);
1001}
1002
1003void IntrinsicLocationsBuilderX86::VisitMathAsin(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001004 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001005}
1006
1007void IntrinsicCodeGeneratorX86::VisitMathAsin(HInvoke* invoke) {
1008 GenFPToFPCall(invoke, codegen_, kQuickAsin);
1009}
1010
1011void IntrinsicLocationsBuilderX86::VisitMathAtan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001012 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001013}
1014
1015void IntrinsicCodeGeneratorX86::VisitMathAtan(HInvoke* invoke) {
1016 GenFPToFPCall(invoke, codegen_, kQuickAtan);
1017}
1018
1019void IntrinsicLocationsBuilderX86::VisitMathCbrt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001020 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001021}
1022
1023void IntrinsicCodeGeneratorX86::VisitMathCbrt(HInvoke* invoke) {
1024 GenFPToFPCall(invoke, codegen_, kQuickCbrt);
1025}
1026
1027void IntrinsicLocationsBuilderX86::VisitMathCosh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001028 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001029}
1030
1031void IntrinsicCodeGeneratorX86::VisitMathCosh(HInvoke* invoke) {
1032 GenFPToFPCall(invoke, codegen_, kQuickCosh);
1033}
1034
1035void IntrinsicLocationsBuilderX86::VisitMathExp(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001036 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001037}
1038
1039void IntrinsicCodeGeneratorX86::VisitMathExp(HInvoke* invoke) {
1040 GenFPToFPCall(invoke, codegen_, kQuickExp);
1041}
1042
1043void IntrinsicLocationsBuilderX86::VisitMathExpm1(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001045}
1046
1047void IntrinsicCodeGeneratorX86::VisitMathExpm1(HInvoke* invoke) {
1048 GenFPToFPCall(invoke, codegen_, kQuickExpm1);
1049}
1050
1051void IntrinsicLocationsBuilderX86::VisitMathLog(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001053}
1054
1055void IntrinsicCodeGeneratorX86::VisitMathLog(HInvoke* invoke) {
1056 GenFPToFPCall(invoke, codegen_, kQuickLog);
1057}
1058
1059void IntrinsicLocationsBuilderX86::VisitMathLog10(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001060 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001061}
1062
1063void IntrinsicCodeGeneratorX86::VisitMathLog10(HInvoke* invoke) {
1064 GenFPToFPCall(invoke, codegen_, kQuickLog10);
1065}
1066
1067void IntrinsicLocationsBuilderX86::VisitMathSinh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001068 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001069}
1070
1071void IntrinsicCodeGeneratorX86::VisitMathSinh(HInvoke* invoke) {
1072 GenFPToFPCall(invoke, codegen_, kQuickSinh);
1073}
1074
1075void IntrinsicLocationsBuilderX86::VisitMathTan(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001076 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001077}
1078
1079void IntrinsicCodeGeneratorX86::VisitMathTan(HInvoke* invoke) {
1080 GenFPToFPCall(invoke, codegen_, kQuickTan);
1081}
1082
1083void IntrinsicLocationsBuilderX86::VisitMathTanh(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001084 CreateFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001085}
1086
1087void IntrinsicCodeGeneratorX86::VisitMathTanh(HInvoke* invoke) {
1088 GenFPToFPCall(invoke, codegen_, kQuickTanh);
1089}
1090
Vladimir Markoca6fff82017-10-03 14:49:14 +01001091static void CreateFPFPToFPCallLocations(ArenaAllocator* allocator, HInvoke* invoke) {
1092 LocationSummary* locations =
1093 new (allocator) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Mark Mendella4f12202015-08-06 15:23:34 -04001094 InvokeRuntimeCallingConvention calling_convention;
1095 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
1096 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
1097 locations->SetOut(Location::FpuRegisterLocation(XMM0));
1098}
1099
1100void IntrinsicLocationsBuilderX86::VisitMathAtan2(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001101 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001102}
1103
1104void IntrinsicCodeGeneratorX86::VisitMathAtan2(HInvoke* invoke) {
1105 GenFPToFPCall(invoke, codegen_, kQuickAtan2);
1106}
1107
Vladimir Marko4d179872018-01-19 14:50:10 +00001108void IntrinsicLocationsBuilderX86::VisitMathPow(HInvoke* invoke) {
1109 CreateFPFPToFPCallLocations(allocator_, invoke);
1110}
1111
1112void IntrinsicCodeGeneratorX86::VisitMathPow(HInvoke* invoke) {
1113 GenFPToFPCall(invoke, codegen_, kQuickPow);
1114}
1115
Mark Mendella4f12202015-08-06 15:23:34 -04001116void IntrinsicLocationsBuilderX86::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001117 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001118}
1119
1120void IntrinsicCodeGeneratorX86::VisitMathHypot(HInvoke* invoke) {
1121 GenFPToFPCall(invoke, codegen_, kQuickHypot);
1122}
1123
1124void IntrinsicLocationsBuilderX86::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001125 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001126}
1127
1128void IntrinsicCodeGeneratorX86::VisitMathNextAfter(HInvoke* invoke) {
1129 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
1130}
1131
Mark Mendell6bc53a92015-07-01 14:26:52 -04001132void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1133 // We need at least two of the positions or length to be an integer constant,
1134 // or else we won't have enough free registers.
1135 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1136 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1137 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1138
1139 int num_constants =
1140 ((src_pos != nullptr) ? 1 : 0)
1141 + ((dest_pos != nullptr) ? 1 : 0)
1142 + ((length != nullptr) ? 1 : 0);
1143
1144 if (num_constants < 2) {
1145 // Not enough free registers.
1146 return;
1147 }
1148
1149 // As long as we are checking, we might as well check to see if the src and dest
1150 // positions are >= 0.
1151 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1152 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1153 // We will have to fail anyways.
1154 return;
1155 }
1156
1157 // And since we are already checking, check the length too.
1158 if (length != nullptr) {
1159 int32_t len = length->GetValue();
1160 if (len < 0) {
1161 // Just call as normal.
1162 return;
1163 }
1164 }
1165
1166 // Okay, it is safe to generate inline code.
1167 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001168 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001169 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
1170 locations->SetInAt(0, Location::RequiresRegister());
1171 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1172 locations->SetInAt(2, Location::RequiresRegister());
1173 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1174 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1175
1176 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1177 locations->AddTemp(Location::RegisterLocation(ESI));
1178 locations->AddTemp(Location::RegisterLocation(EDI));
1179 locations->AddTemp(Location::RegisterLocation(ECX));
1180}
1181
1182static void CheckPosition(X86Assembler* assembler,
1183 Location pos,
1184 Register input,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001185 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -07001186 SlowPathCode* slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001187 Register temp,
1188 bool length_is_input_length = false) {
1189 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -04001190 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1191
1192 if (pos.IsConstant()) {
1193 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1194 if (pos_const == 0) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001195 if (!length_is_input_length) {
1196 // Check that length(input) >= length.
1197 if (length.IsConstant()) {
1198 __ cmpl(Address(input, length_offset),
1199 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1200 } else {
1201 __ cmpl(Address(input, length_offset), length.AsRegister<Register>());
1202 }
1203 __ j(kLess, slow_path->GetEntryLabel());
1204 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001205 } else {
1206 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001207 __ movl(temp, Address(input, length_offset));
1208 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001209 __ j(kLess, slow_path->GetEntryLabel());
1210
1211 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001212 if (length.IsConstant()) {
1213 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1214 } else {
1215 __ cmpl(temp, length.AsRegister<Register>());
1216 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001217 __ j(kLess, slow_path->GetEntryLabel());
1218 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001219 } else if (length_is_input_length) {
1220 // The only way the copy can succeed is if pos is zero.
1221 Register pos_reg = pos.AsRegister<Register>();
1222 __ testl(pos_reg, pos_reg);
1223 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001224 } else {
1225 // Check that pos >= 0.
1226 Register pos_reg = pos.AsRegister<Register>();
1227 __ testl(pos_reg, pos_reg);
1228 __ j(kLess, slow_path->GetEntryLabel());
1229
1230 // Check that pos <= length(input).
1231 __ cmpl(Address(input, length_offset), pos_reg);
1232 __ j(kLess, slow_path->GetEntryLabel());
1233
1234 // Check that (length(input) - pos) >= length.
1235 __ movl(temp, Address(input, length_offset));
1236 __ subl(temp, pos_reg);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001237 if (length.IsConstant()) {
1238 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1239 } else {
1240 __ cmpl(temp, length.AsRegister<Register>());
1241 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001242 __ j(kLess, slow_path->GetEntryLabel());
1243 }
1244}
1245
1246void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1247 X86Assembler* assembler = GetAssembler();
1248 LocationSummary* locations = invoke->GetLocations();
1249
1250 Register src = locations->InAt(0).AsRegister<Register>();
1251 Location srcPos = locations->InAt(1);
1252 Register dest = locations->InAt(2).AsRegister<Register>();
1253 Location destPos = locations->InAt(3);
1254 Location length = locations->InAt(4);
1255
1256 // Temporaries that we need for MOVSW.
1257 Register src_base = locations->GetTemp(0).AsRegister<Register>();
1258 DCHECK_EQ(src_base, ESI);
1259 Register dest_base = locations->GetTemp(1).AsRegister<Register>();
1260 DCHECK_EQ(dest_base, EDI);
1261 Register count = locations->GetTemp(2).AsRegister<Register>();
1262 DCHECK_EQ(count, ECX);
1263
Vladimir Marko174b2e22017-10-12 13:34:49 +01001264 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001265 codegen_->AddSlowPath(slow_path);
1266
1267 // Bail out if the source and destination are the same (to handle overlap).
1268 __ cmpl(src, dest);
1269 __ j(kEqual, slow_path->GetEntryLabel());
1270
1271 // Bail out if the source is null.
1272 __ testl(src, src);
1273 __ j(kEqual, slow_path->GetEntryLabel());
1274
1275 // Bail out if the destination is null.
1276 __ testl(dest, dest);
1277 __ j(kEqual, slow_path->GetEntryLabel());
1278
1279 // If the length is negative, bail out.
1280 // We have already checked in the LocationsBuilder for the constant case.
1281 if (!length.IsConstant()) {
1282 __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>());
1283 __ j(kLess, slow_path->GetEntryLabel());
1284 }
1285
1286 // We need the count in ECX.
1287 if (length.IsConstant()) {
1288 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1289 } else {
1290 __ movl(count, length.AsRegister<Register>());
1291 }
1292
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001293 // Validity checks: source. Use src_base as a temporary register.
1294 CheckPosition(assembler, srcPos, src, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001295
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001296 // Validity checks: dest. Use src_base as a temporary register.
1297 CheckPosition(assembler, destPos, dest, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001298
1299 // Okay, everything checks out. Finally time to do the copy.
1300 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001301 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001302 DCHECK_EQ(char_size, 2u);
1303
1304 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1305
1306 if (srcPos.IsConstant()) {
1307 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
1308 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
1309 } else {
1310 __ leal(src_base, Address(src, srcPos.AsRegister<Register>(),
1311 ScaleFactor::TIMES_2, data_offset));
1312 }
1313 if (destPos.IsConstant()) {
1314 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
1315
1316 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
1317 } else {
1318 __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(),
1319 ScaleFactor::TIMES_2, data_offset));
1320 }
1321
1322 // Do the move.
1323 __ rep_movsw();
1324
1325 __ Bind(slow_path->GetExitLabel());
1326}
1327
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001328void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
1329 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001330 LocationSummary* locations = new (allocator_) LocationSummary(
1331 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001332 InvokeRuntimeCallingConvention calling_convention;
1333 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1334 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1335 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001336}
1337
1338void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
1339 X86Assembler* assembler = GetAssembler();
1340 LocationSummary* locations = invoke->GetLocations();
1341
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001342 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001343 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001344
1345 Register argument = locations->InAt(1).AsRegister<Register>();
1346 __ testl(argument, argument);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001347 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001348 codegen_->AddSlowPath(slow_path);
1349 __ j(kEqual, slow_path->GetEntryLabel());
1350
Serban Constantinescuba45db02016-07-12 22:53:02 +01001351 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001352 __ Bind(slow_path->GetExitLabel());
1353}
1354
Agi Csakid7138c82015-08-13 17:46:44 -07001355void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoda283052017-11-07 21:17:24 +00001356 if (kEmitCompilerReadBarrier &&
1357 !StringEqualsOptimizations(invoke).GetArgumentIsString() &&
1358 !StringEqualsOptimizations(invoke).GetNoReadBarrierForStringClass()) {
1359 // No support for this odd case (String class is moveable, not in the boot image).
1360 return;
1361 }
1362
Vladimir Markoca6fff82017-10-03 14:49:14 +01001363 LocationSummary* locations =
1364 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -07001365 locations->SetInAt(0, Location::RequiresRegister());
1366 locations->SetInAt(1, Location::RequiresRegister());
1367
1368 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
1369 locations->AddTemp(Location::RegisterLocation(ECX));
1370 locations->AddTemp(Location::RegisterLocation(EDI));
1371
1372 // Set output, ESI needed for repe_cmpsl instruction anyways.
1373 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
1374}
1375
1376void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
1377 X86Assembler* assembler = GetAssembler();
1378 LocationSummary* locations = invoke->GetLocations();
1379
1380 Register str = locations->InAt(0).AsRegister<Register>();
1381 Register arg = locations->InAt(1).AsRegister<Register>();
1382 Register ecx = locations->GetTemp(0).AsRegister<Register>();
1383 Register edi = locations->GetTemp(1).AsRegister<Register>();
1384 Register esi = locations->Out().AsRegister<Register>();
1385
Mark Mendell0c9497d2015-08-21 09:30:05 -04001386 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -07001387
1388 // Get offsets of count, value, and class fields within a string object.
1389 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1390 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1391 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1392
1393 // Note that the null check must have been done earlier.
1394 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1395
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001396 StringEqualsOptimizations optimizations(invoke);
1397 if (!optimizations.GetArgumentNotNull()) {
1398 // Check if input is null, return false if it is.
1399 __ testl(arg, arg);
1400 __ j(kEqual, &return_false);
1401 }
Agi Csakid7138c82015-08-13 17:46:44 -07001402
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001403 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +01001404 // Instanceof check for the argument by comparing class fields.
1405 // All string objects must have the same type since String cannot be subclassed.
1406 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1407 // If the argument is a string object, its class field must be equal to receiver's class field.
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001408 __ movl(ecx, Address(str, class_offset));
1409 __ cmpl(ecx, Address(arg, class_offset));
1410 __ j(kNotEqual, &return_false);
1411 }
Agi Csakid7138c82015-08-13 17:46:44 -07001412
1413 // Reference equality check, return true if same reference.
1414 __ cmpl(str, arg);
1415 __ j(kEqual, &return_true);
1416
jessicahandojo4877b792016-09-08 19:49:13 -07001417 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -07001418 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001419 // Check if lengths and compression flags are equal, return false if they're not.
1420 // Two identical strings will always have same compression style since
1421 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -07001422 __ cmpl(ecx, Address(arg, count_offset));
1423 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001424 // Return true if strings are empty. Even with string compression `count == 0` means empty.
1425 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1426 "Expecting 0=compressed, 1=uncompressed");
1427 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -07001428
jessicahandojo4877b792016-09-08 19:49:13 -07001429 if (mirror::kUseStringCompression) {
1430 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001431 // Extract length and differentiate between both compressed or both uncompressed.
1432 // Different compression style is cut above.
1433 __ shrl(ecx, Immediate(1));
1434 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001435 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001436 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001437 __ shrl(ecx, Immediate(1));
1438 __ Bind(&string_uncompressed);
1439 }
Agi Csakid7138c82015-08-13 17:46:44 -07001440 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1441 __ leal(esi, Address(str, value_offset));
1442 __ leal(edi, Address(arg, value_offset));
1443
jessicahandojo4877b792016-09-08 19:49:13 -07001444 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1445 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001446 __ addl(ecx, Immediate(1));
1447 __ shrl(ecx, Immediate(1));
1448
jessicahandojo4877b792016-09-08 19:49:13 -07001449 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1450 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001451 DCHECK_ALIGNED(value_offset, 4);
1452 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1453
1454 // Loop to compare strings two characters at a time starting at the beginning of the string.
1455 __ repe_cmpsl();
1456 // If strings are not equal, zero flag will be cleared.
1457 __ j(kNotEqual, &return_false);
1458
1459 // Return true and exit the function.
1460 // If loop does not result in returning false, we return true.
1461 __ Bind(&return_true);
1462 __ movl(esi, Immediate(1));
1463 __ jmp(&end);
1464
1465 // Return false and exit the function.
1466 __ Bind(&return_false);
1467 __ xorl(esi, esi);
1468 __ Bind(&end);
1469}
1470
Andreas Gampe21030dd2015-05-07 14:46:15 -07001471static void CreateStringIndexOfLocations(HInvoke* invoke,
1472 ArenaAllocator* allocator,
1473 bool start_at_zero) {
1474 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1475 LocationSummary::kCallOnSlowPath,
1476 kIntrinsified);
1477 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1478 locations->SetInAt(0, Location::RegisterLocation(EDI));
1479 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1480 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1481 // of the instruction explicitly.
1482 // Note: This works as we don't clobber EAX anywhere.
1483 locations->SetInAt(1, Location::RegisterLocation(EAX));
1484 if (!start_at_zero) {
1485 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1486 }
1487 // As we clobber EDI during execution anyways, also use it as the output.
1488 locations->SetOut(Location::SameAsFirstInput());
1489
1490 // repne scasw uses ECX as the counter.
1491 locations->AddTemp(Location::RegisterLocation(ECX));
1492 // Need another temporary to be able to compute the result.
1493 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001494 if (mirror::kUseStringCompression) {
1495 // Need another temporary to be able to save unflagged string length.
1496 locations->AddTemp(Location::RequiresRegister());
1497 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001498}
1499
1500static void GenerateStringIndexOf(HInvoke* invoke,
1501 X86Assembler* assembler,
1502 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001503 bool start_at_zero) {
1504 LocationSummary* locations = invoke->GetLocations();
1505
1506 // Note that the null check must have been done earlier.
1507 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1508
1509 Register string_obj = locations->InAt(0).AsRegister<Register>();
1510 Register search_value = locations->InAt(1).AsRegister<Register>();
1511 Register counter = locations->GetTemp(0).AsRegister<Register>();
1512 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1513 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001514 // Only used when string compression feature is on.
1515 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001516
1517 // Check our assumptions for registers.
1518 DCHECK_EQ(string_obj, EDI);
1519 DCHECK_EQ(search_value, EAX);
1520 DCHECK_EQ(counter, ECX);
1521 DCHECK_EQ(out, EDI);
1522
1523 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001524 // 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 -07001525 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001526 HInstruction* code_point = invoke->InputAt(1);
1527 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001528 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001529 std::numeric_limits<uint16_t>::max()) {
1530 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1531 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001532 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001533 codegen->AddSlowPath(slow_path);
1534 __ jmp(slow_path->GetEntryLabel());
1535 __ Bind(slow_path->GetExitLabel());
1536 return;
1537 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001538 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001539 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001540 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001541 codegen->AddSlowPath(slow_path);
1542 __ j(kAbove, slow_path->GetEntryLabel());
1543 }
1544
1545 // From here down, we know that we are looking for a char that fits in 16 bits.
1546 // Location of reference to data array within the String object.
1547 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1548 // Location of count within the String object.
1549 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1550
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001551 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001552 __ movl(string_length, Address(string_obj, count_offset));
1553
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001554 // Do a zero-length check. Even with string compression `count == 0` means empty.
1555 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1556 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001557 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001558 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001559 __ testl(string_length, string_length);
1560 __ j(kEqual, &not_found_label);
1561
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001562 if (mirror::kUseStringCompression) {
1563 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1564 __ movl(string_length_flagged, string_length);
1565 // Extract the length and shift out the least significant bit used as compression flag.
1566 __ shrl(string_length, Immediate(1));
1567 }
1568
Andreas Gampe21030dd2015-05-07 14:46:15 -07001569 if (start_at_zero) {
1570 // Number of chars to scan is the same as the string length.
1571 __ movl(counter, string_length);
1572
1573 // Move to the start of the string.
1574 __ addl(string_obj, Immediate(value_offset));
1575 } else {
1576 Register start_index = locations->InAt(2).AsRegister<Register>();
1577
1578 // Do a start_index check.
1579 __ cmpl(start_index, string_length);
1580 __ j(kGreaterEqual, &not_found_label);
1581
1582 // Ensure we have a start index >= 0;
1583 __ xorl(counter, counter);
1584 __ cmpl(start_index, Immediate(0));
1585 __ cmovl(kGreater, counter, start_index);
1586
jessicahandojo4877b792016-09-08 19:49:13 -07001587 if (mirror::kUseStringCompression) {
1588 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001589 __ testl(string_length_flagged, Immediate(1));
1590 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001591 // Move to the start of the string: string_obj + value_offset + start_index.
1592 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1593 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001594
jessicahandojo4877b792016-09-08 19:49:13 -07001595 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1596 __ Bind(&offset_uncompressed_label);
1597 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1598
1599 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1600 // compare.
1601 __ Bind(&modify_counter);
1602 } else {
1603 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1604 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001605 __ negl(counter);
1606 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1607 }
1608
jessicahandojo4877b792016-09-08 19:49:13 -07001609 if (mirror::kUseStringCompression) {
1610 NearLabel uncompressed_string_comparison;
1611 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001612 __ testl(string_length_flagged, Immediate(1));
1613 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001614
jessicahandojo4877b792016-09-08 19:49:13 -07001615 // Check if EAX (search_value) is ASCII.
1616 __ cmpl(search_value, Immediate(127));
1617 __ j(kGreater, &not_found_label);
1618 // Comparing byte-per-byte.
1619 __ repne_scasb();
1620 __ jmp(&comparison_done);
1621
1622 // Everything is set up for repne scasw:
1623 // * Comparison address in EDI.
1624 // * Counter in ECX.
1625 __ Bind(&uncompressed_string_comparison);
1626 __ repne_scasw();
1627 __ Bind(&comparison_done);
1628 } else {
1629 __ repne_scasw();
1630 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001631 // Did we find a match?
1632 __ j(kNotEqual, &not_found_label);
1633
1634 // Yes, we matched. Compute the index of the result.
1635 __ subl(string_length, counter);
1636 __ leal(out, Address(string_length, -1));
1637
Mark Mendell0c9497d2015-08-21 09:30:05 -04001638 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001639 __ jmp(&done);
1640
1641 // Failed to match; return -1.
1642 __ Bind(&not_found_label);
1643 __ movl(out, Immediate(-1));
1644
1645 // And join up at the end.
1646 __ Bind(&done);
1647 if (slow_path != nullptr) {
1648 __ Bind(slow_path->GetExitLabel());
1649 }
1650}
1651
1652void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001653 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001654}
1655
1656void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001657 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001658}
1659
1660void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001661 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001662}
1663
1664void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001665 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001666}
1667
Jeff Hao848f70a2014-01-15 13:49:50 -08001668void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001669 LocationSummary* locations = new (allocator_) LocationSummary(
1670 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001671 InvokeRuntimeCallingConvention calling_convention;
1672 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1673 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1674 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1675 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1676 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001677}
1678
1679void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1680 X86Assembler* assembler = GetAssembler();
1681 LocationSummary* locations = invoke->GetLocations();
1682
1683 Register byte_array = locations->InAt(0).AsRegister<Register>();
1684 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001685 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001686 codegen_->AddSlowPath(slow_path);
1687 __ j(kEqual, slow_path->GetEntryLabel());
1688
Serban Constantinescuba45db02016-07-12 22:53:02 +01001689 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001690 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001691 __ Bind(slow_path->GetExitLabel());
1692}
1693
1694void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001695 LocationSummary* locations =
1696 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001697 InvokeRuntimeCallingConvention calling_convention;
1698 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1699 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1700 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1701 locations->SetOut(Location::RegisterLocation(EAX));
1702}
1703
1704void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001705 // No need to emit code checking whether `locations->InAt(2)` is a null
1706 // pointer, as callers of the native method
1707 //
1708 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1709 //
1710 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001711 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001712 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001713}
1714
1715void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001716 LocationSummary* locations = new (allocator_) LocationSummary(
1717 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001718 InvokeRuntimeCallingConvention calling_convention;
1719 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1720 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001721}
1722
1723void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1724 X86Assembler* assembler = GetAssembler();
1725 LocationSummary* locations = invoke->GetLocations();
1726
1727 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1728 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001729 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001730 codegen_->AddSlowPath(slow_path);
1731 __ j(kEqual, slow_path->GetEntryLabel());
1732
Serban Constantinescuba45db02016-07-12 22:53:02 +01001733 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001734 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001735 __ Bind(slow_path->GetExitLabel());
1736}
1737
Mark Mendell8f8926a2015-08-17 11:39:06 -04001738void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1739 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001740 LocationSummary* locations =
1741 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001742 locations->SetInAt(0, Location::RequiresRegister());
1743 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1744 // Place srcEnd in ECX to save a move below.
1745 locations->SetInAt(2, Location::RegisterLocation(ECX));
1746 locations->SetInAt(3, Location::RequiresRegister());
1747 locations->SetInAt(4, Location::RequiresRegister());
1748
1749 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1750 // We don't have enough registers to also grab ECX, so handle below.
1751 locations->AddTemp(Location::RegisterLocation(ESI));
1752 locations->AddTemp(Location::RegisterLocation(EDI));
1753}
1754
1755void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1756 X86Assembler* assembler = GetAssembler();
1757 LocationSummary* locations = invoke->GetLocations();
1758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001759 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001760 // Location of data in char array buffer.
1761 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1762 // Location of char array data in string.
1763 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1764
1765 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1766 Register obj = locations->InAt(0).AsRegister<Register>();
1767 Location srcBegin = locations->InAt(1);
1768 int srcBegin_value =
1769 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1770 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1771 Register dst = locations->InAt(3).AsRegister<Register>();
1772 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1773
1774 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001776 DCHECK_EQ(char_size, 2u);
1777
Mark Mendell8f8926a2015-08-17 11:39:06 -04001778 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001779 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001780 __ pushl(ECX);
1781 int stack_adjust = kX86WordSize;
1782 __ cfi().AdjustCFAOffset(stack_adjust);
1783 DCHECK_EQ(srcEnd, ECX);
1784 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001785 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001786 } else {
1787 DCHECK(srcBegin.IsRegister());
1788 __ subl(ECX, srcBegin.AsRegister<Register>());
1789 }
1790
jessicahandojo4877b792016-09-08 19:49:13 -07001791 NearLabel done;
1792 if (mirror::kUseStringCompression) {
1793 // Location of count in string
1794 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001795 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001796 DCHECK_EQ(c_char_size, 1u);
1797 __ pushl(EAX);
1798 __ cfi().AdjustCFAOffset(stack_adjust);
1799
1800 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001801 __ testl(Address(obj, count_offset), Immediate(1));
1802 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1803 "Expecting 0=compressed, 1=uncompressed");
1804 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001805 // Compute the address of the source string by adding the number of chars from
1806 // the source beginning to the value offset of a string.
1807 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1808
1809 // Start the loop to copy String's value to Array of Char.
1810 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1811 __ Bind(&copy_loop);
1812 __ jecxz(&done);
1813 // Use EAX temporary (convert byte from ESI to word).
1814 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1815 __ movzxb(EAX, Address(ESI, 0));
1816 __ movw(Address(EDI, 0), EAX);
1817 __ leal(EDI, Address(EDI, char_size));
1818 __ leal(ESI, Address(ESI, c_char_size));
1819 // TODO: Add support for LOOP to X86Assembler.
1820 __ subl(ECX, Immediate(1));
1821 __ jmp(&copy_loop);
1822 __ Bind(&copy_uncompressed);
1823 }
1824
1825 // Do the copy for uncompressed string.
1826 // Compute the address of the destination buffer.
1827 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1828 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001829 __ rep_movsw();
1830
jessicahandojo4877b792016-09-08 19:49:13 -07001831 __ Bind(&done);
1832 if (mirror::kUseStringCompression) {
1833 // Restore EAX.
1834 __ popl(EAX);
1835 __ cfi().AdjustCFAOffset(-stack_adjust);
1836 }
1837 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001838 __ popl(ECX);
1839 __ cfi().AdjustCFAOffset(-stack_adjust);
1840}
1841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001842static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001843 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1844 Location out_loc = locations->Out();
1845 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1846 // to avoid a SIGBUS.
1847 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001848 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001849 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1850 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001851 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001852 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1853 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001854 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001855 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1856 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001857 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001858 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1859 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1860 break;
1861 default:
1862 LOG(FATAL) << "Type not recognized for peek: " << size;
1863 UNREACHABLE();
1864 }
1865}
1866
1867void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001868 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001869}
1870
1871void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001872 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001873}
1874
1875void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001876 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001877}
1878
1879void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001880 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001881}
1882
1883void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001884 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001885}
1886
1887void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001889}
1890
1891void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001892 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001893}
1894
1895void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001896 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001897}
1898
Vladimir Markoca6fff82017-10-03 14:49:14 +01001899static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1900 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001901 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001902 LocationSummary* locations =
1903 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001904 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001905 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001906 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001907 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1908 } else {
1909 locations->SetInAt(1, Location::RegisterOrConstant(value));
1910 }
1911}
1912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001913static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001914 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1915 Location value_loc = locations->InAt(1);
1916 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1917 // to avoid a SIGBUS.
1918 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001919 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001920 if (value_loc.IsConstant()) {
1921 __ movb(Address(address, 0),
1922 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1923 } else {
1924 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1925 }
1926 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001927 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001928 if (value_loc.IsConstant()) {
1929 __ movw(Address(address, 0),
1930 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1931 } else {
1932 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1933 }
1934 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001935 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001936 if (value_loc.IsConstant()) {
1937 __ movl(Address(address, 0),
1938 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1939 } else {
1940 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1941 }
1942 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001943 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001944 if (value_loc.IsConstant()) {
1945 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1946 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1947 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1948 } else {
1949 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1950 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1951 }
1952 break;
1953 default:
1954 LOG(FATAL) << "Type not recognized for poke: " << size;
1955 UNREACHABLE();
1956 }
1957}
1958
1959void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001960 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001961}
1962
1963void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001964 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001965}
1966
1967void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001968 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001969}
1970
1971void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001972 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001973}
1974
1975void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001976 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001977}
1978
1979void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001980 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001981}
1982
1983void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001984 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001985}
1986
1987void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001989}
1990
1991void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001992 LocationSummary* locations =
1993 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001994 locations->SetOut(Location::RequiresRegister());
1995}
1996
1997void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1998 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001999 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04002000}
2001
Roland Levillain0d5a2812015-11-13 10:07:31 +00002002static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002003 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00002004 bool is_volatile,
2005 CodeGeneratorX86* codegen) {
2006 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
2007 LocationSummary* locations = invoke->GetLocations();
2008 Location base_loc = locations->InAt(1);
2009 Register base = base_loc.AsRegister<Register>();
2010 Location offset_loc = locations->InAt(2);
2011 Register offset = offset_loc.AsRegisterPairLow<Register>();
2012 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002013
2014 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002015 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002016 Register output = output_loc.AsRegister<Register>();
2017 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00002018 break;
2019 }
2020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002021 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002022 Register output = output_loc.AsRegister<Register>();
2023 if (kEmitCompilerReadBarrier) {
2024 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002025 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2026 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002027 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00002028 } else {
2029 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2030 codegen->GenerateReadBarrierSlow(
2031 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2032 }
2033 } else {
2034 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2035 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002036 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002037 break;
Roland Levillain4d027112015-07-01 15:41:14 +01002038 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002039
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002040 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002041 Register output_lo = output_loc.AsRegisterPairLow<Register>();
2042 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002043 if (is_volatile) {
2044 // Need to use a XMM to read atomically.
2045 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2046 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
2047 __ movd(output_lo, temp);
2048 __ psrlq(temp, Immediate(32));
2049 __ movd(output_hi, temp);
2050 } else {
2051 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
2052 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
2053 }
2054 }
2055 break;
2056
2057 default:
2058 LOG(FATAL) << "Unsupported op size " << type;
2059 UNREACHABLE();
2060 }
2061}
2062
Vladimir Markoca6fff82017-10-03 14:49:14 +01002063static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002064 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002065 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002066 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002067 bool can_call = kEmitCompilerReadBarrier &&
2068 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2069 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002070 LocationSummary* locations =
2071 new (allocator) LocationSummary(invoke,
2072 can_call
2073 ? LocationSummary::kCallOnSlowPath
2074 : LocationSummary::kNoCall,
2075 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002076 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002077 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002078 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002079 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2080 locations->SetInAt(1, Location::RequiresRegister());
2081 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002083 if (is_volatile) {
2084 // Need to use XMM to read volatile.
2085 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002086 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002087 } else {
2088 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2089 }
2090 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01002091 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002092 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04002093 }
2094}
2095
2096void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002097 CreateIntIntIntToIntLocations(
2098 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002099}
2100void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002101 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002102}
2103void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002104 CreateIntIntIntToIntLocations(
2105 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002106}
2107void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002108 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002109}
2110void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002111 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002112 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002113}
2114void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002115 CreateIntIntIntToIntLocations(
2116 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002117}
2118
2119
2120void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002122}
2123void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002125}
2126void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002128}
2129void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002130 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002131}
2132void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002133 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002134}
2135void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002137}
2138
2139
Vladimir Markoca6fff82017-10-03 14:49:14 +01002140static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002141 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002142 HInvoke* invoke,
2143 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002144 LocationSummary* locations =
2145 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002146 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2147 locations->SetInAt(1, Location::RequiresRegister());
2148 locations->SetInAt(2, Location::RequiresRegister());
2149 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002151 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002152 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04002153 // Ensure the value is in a byte register.
2154 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002155 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002156 locations->AddTemp(Location::RequiresFpuRegister());
2157 locations->AddTemp(Location::RequiresFpuRegister());
2158 }
2159}
2160
2161void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002162 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002163 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002164}
2165void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002166 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002167 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002168}
2169void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002170 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002171 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002172}
2173void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002174 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002175 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002176}
2177void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002178 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002179 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002180}
2181void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002182 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002183 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002184}
2185void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002186 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002187 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002188}
2189void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002190 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002191 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002192}
2193void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002194 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002195 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002196}
2197
2198// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2199// memory model.
2200static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002201 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002202 bool is_volatile,
2203 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002204 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04002205 Register base = locations->InAt(1).AsRegister<Register>();
2206 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2207 Location value_loc = locations->InAt(3);
2208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002209 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002210 Register value_lo = value_loc.AsRegisterPairLow<Register>();
2211 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
2212 if (is_volatile) {
2213 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2214 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2215 __ movd(temp1, value_lo);
2216 __ movd(temp2, value_hi);
2217 __ punpckldq(temp1, temp2);
2218 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
2219 } else {
2220 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
2221 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
2222 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002223 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002224 Register temp = locations->GetTemp(0).AsRegister<Register>();
2225 __ movl(temp, value_loc.AsRegister<Register>());
2226 __ PoisonHeapReference(temp);
2227 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002228 } else {
2229 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
2230 }
2231
2232 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002233 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002234 }
2235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002236 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002237 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04002238 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
2239 locations->GetTemp(1).AsRegister<Register>(),
2240 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002241 value_loc.AsRegister<Register>(),
2242 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002243 }
2244}
2245
2246void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002247 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002248}
2249void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002250 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002251}
2252void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002253 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002254}
2255void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002256 GenUnsafePut(
2257 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002258}
2259void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 GenUnsafePut(
2261 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002262}
2263void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 GenUnsafePut(
2265 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002266}
2267void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002269}
2270void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002272}
2273void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002274 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002275}
2276
Vladimir Markoca6fff82017-10-03 14:49:14 +01002277static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002278 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002279 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002280 bool can_call = kEmitCompilerReadBarrier &&
2281 kUseBakerReadBarrier &&
2282 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002283 LocationSummary* locations =
2284 new (allocator) LocationSummary(invoke,
2285 can_call
2286 ? LocationSummary::kCallOnSlowPath
2287 : LocationSummary::kNoCall,
2288 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002289 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2290 locations->SetInAt(1, Location::RequiresRegister());
2291 // Offset is a long, but in 32 bit mode, we only need the low word.
2292 // Can we update the invoke here to remove a TypeConvert to Long?
2293 locations->SetInAt(2, Location::RequiresRegister());
2294 // Expected value must be in EAX or EDX:EAX.
2295 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002296 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04002297 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
2298 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
2299 } else {
2300 locations->SetInAt(3, Location::RegisterLocation(EAX));
2301 locations->SetInAt(4, Location::RequiresRegister());
2302 }
2303
2304 // Force a byte register for the output.
2305 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002307 // Need temporary registers for card-marking, and possibly for
2308 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002309 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002310 // Need a byte register for marking.
2311 locations->AddTemp(Location::RegisterLocation(ECX));
2312 }
2313}
2314
2315void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002316 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002317}
2318
2319void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002320 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002321}
2322
2323void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002324 // The only read barrier implementation supporting the
2325 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2326 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002327 return;
2328 }
2329
Vladimir Markoca6fff82017-10-03 14:49:14 +01002330 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002331}
2332
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002333static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002334 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002335 LocationSummary* locations = invoke->GetLocations();
2336
2337 Register base = locations->InAt(1).AsRegister<Register>();
2338 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2339 Location out = locations->Out();
2340 DCHECK_EQ(out.AsRegister<Register>(), EAX);
2341
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002342 // The address of the field within the holding object.
2343 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002346 // The only read barrier implementation supporting the
2347 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2348 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2349
2350 Location temp1_loc = locations->GetTemp(0);
2351 Register temp1 = temp1_loc.AsRegister<Register>();
2352 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2353
Roland Levillain4d027112015-07-01 15:41:14 +01002354 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01002355 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01002356 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002357 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01002358
Roland Levillainb488b782015-10-22 11:38:49 +01002359 // Mark card for object assuming new value is stored.
2360 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002361 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2362
2363 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2364 // Need to make sure the reference stored in the field is a to-space
2365 // one before attempting the CAS or the CAS could fail incorrectly.
2366 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2367 invoke,
2368 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
2369 base,
2370 field_addr,
2371 /* needs_null_check */ false,
2372 /* always_update_field */ true,
2373 &temp2);
2374 }
Roland Levillainb488b782015-10-22 11:38:49 +01002375
2376 bool base_equals_value = (base == value);
2377 if (kPoisonHeapReferences) {
2378 if (base_equals_value) {
2379 // If `base` and `value` are the same register location, move
2380 // `value` to a temporary register. This way, poisoning
2381 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002382 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01002383 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01002384 }
Roland Levillainb488b782015-10-22 11:38:49 +01002385
2386 // Check that the register allocator did not assign the location
2387 // of `expected` (EAX) to `value` nor to `base`, so that heap
2388 // poisoning (when enabled) works as intended below.
2389 // - If `value` were equal to `expected`, both references would
2390 // be poisoned twice, meaning they would not be poisoned at
2391 // all, as heap poisoning uses address negation.
2392 // - If `base` were equal to `expected`, poisoning `expected`
2393 // would invalidate `base`.
2394 DCHECK_NE(value, expected);
2395 DCHECK_NE(base, expected);
2396
2397 __ PoisonHeapReference(expected);
2398 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002399 }
2400
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002401 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002402
Roland Levillain0d5a2812015-11-13 10:07:31 +00002403 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002404 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002405
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002406 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002407 __ setb(kZero, out.AsRegister<Register>());
2408 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002409
Roland Levillain391b8662015-12-18 11:43:38 +00002410 // If heap poisoning is enabled, we need to unpoison the values
2411 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002412 if (kPoisonHeapReferences) {
2413 if (base_equals_value) {
2414 // `value` has been moved to a temporary register, no need to
2415 // unpoison it.
2416 } else {
2417 // Ensure `value` is different from `out`, so that unpoisoning
2418 // the former does not invalidate the latter.
2419 DCHECK_NE(value, out.AsRegister<Register>());
2420 __ UnpoisonHeapReference(value);
2421 }
2422 // Do not unpoison the reference contained in register
2423 // `expected`, as it is the same as register `out` (EAX).
2424 }
2425 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002426 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002427 // Ensure the expected value is in EAX (required by the CMPXCHG
2428 // instruction).
2429 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002430 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002431 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002432 // Ensure the expected value is in EAX:EDX and that the new
2433 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2434 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2435 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2436 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2437 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002438 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002439 } else {
2440 LOG(FATAL) << "Unexpected CAS type " << type;
2441 }
2442
Roland Levillain0d5a2812015-11-13 10:07:31 +00002443 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2444 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002445
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002446 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002447 __ setb(kZero, out.AsRegister<Register>());
2448 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002449 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002450}
2451
2452void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002453 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002454}
2455
2456void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002457 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002458}
2459
2460void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002461 // The only read barrier implementation supporting the
2462 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2463 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002464
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002465 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002466}
2467
2468void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002469 LocationSummary* locations =
2470 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002471 locations->SetInAt(0, Location::RequiresRegister());
2472 locations->SetOut(Location::SameAsFirstInput());
2473 locations->AddTemp(Location::RequiresRegister());
2474}
2475
2476static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2477 X86Assembler* assembler) {
2478 Immediate imm_shift(shift);
2479 Immediate imm_mask(mask);
2480 __ movl(temp, reg);
2481 __ shrl(reg, imm_shift);
2482 __ andl(temp, imm_mask);
2483 __ andl(reg, imm_mask);
2484 __ shll(temp, imm_shift);
2485 __ orl(reg, temp);
2486}
2487
2488void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002489 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002490 LocationSummary* locations = invoke->GetLocations();
2491
2492 Register reg = locations->InAt(0).AsRegister<Register>();
2493 Register temp = locations->GetTemp(0).AsRegister<Register>();
2494
2495 /*
2496 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2497 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2498 * compared to generic luni implementation which has 5 rounds of swapping bits.
2499 * x = bswap x
2500 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2501 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2502 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2503 */
2504 __ bswapl(reg);
2505 SwapBits(reg, temp, 1, 0x55555555, assembler);
2506 SwapBits(reg, temp, 2, 0x33333333, assembler);
2507 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2508}
2509
2510void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002511 LocationSummary* locations =
2512 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002513 locations->SetInAt(0, Location::RequiresRegister());
2514 locations->SetOut(Location::SameAsFirstInput());
2515 locations->AddTemp(Location::RequiresRegister());
2516}
2517
2518void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002519 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002520 LocationSummary* locations = invoke->GetLocations();
2521
2522 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2523 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2524 Register temp = locations->GetTemp(0).AsRegister<Register>();
2525
2526 // We want to swap high/low, then bswap each one, and then do the same
2527 // as a 32 bit reverse.
2528 // Exchange high and low.
2529 __ movl(temp, reg_low);
2530 __ movl(reg_low, reg_high);
2531 __ movl(reg_high, temp);
2532
2533 // bit-reverse low
2534 __ bswapl(reg_low);
2535 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2536 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2537 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2538
2539 // bit-reverse high
2540 __ bswapl(reg_high);
2541 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2542 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2543 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2544}
2545
Aart Bikc39dac12016-01-21 08:59:48 -08002546static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002547 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002548 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2549 // Do nothing if there is no popcnt support. This results in generating
2550 // a call for the intrinsic rather than direct code.
2551 return;
2552 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002553 LocationSummary* locations =
2554 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002555 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002556 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002557 }
Aart Bik2a946072016-01-21 12:49:00 -08002558 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002559 locations->SetOut(Location::RequiresRegister());
2560}
2561
Aart Bika19616e2016-02-01 18:57:58 -08002562static void GenBitCount(X86Assembler* assembler,
2563 CodeGeneratorX86* codegen,
2564 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002565 LocationSummary* locations = invoke->GetLocations();
2566 Location src = locations->InAt(0);
2567 Register out = locations->Out().AsRegister<Register>();
2568
2569 if (invoke->InputAt(0)->IsConstant()) {
2570 // Evaluate this at compile time.
2571 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002572 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002573 ? POPCOUNT(static_cast<uint64_t>(value))
2574 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002575 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002576 return;
2577 }
2578
2579 // Handle the non-constant cases.
2580 if (!is_long) {
2581 if (src.IsRegister()) {
2582 __ popcntl(out, src.AsRegister<Register>());
2583 } else {
2584 DCHECK(src.IsStackSlot());
2585 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2586 }
Aart Bik2a946072016-01-21 12:49:00 -08002587 } else {
2588 // The 64-bit case needs to worry about two parts.
2589 Register temp = locations->GetTemp(0).AsRegister<Register>();
2590 if (src.IsRegisterPair()) {
2591 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2592 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2593 } else {
2594 DCHECK(src.IsDoubleStackSlot());
2595 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2596 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2597 }
2598 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002599 }
Aart Bikc39dac12016-01-21 08:59:48 -08002600}
2601
2602void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002603 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002604}
2605
2606void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002607 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002608}
2609
2610void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002611 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002612}
2613
2614void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002615 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002616}
2617
Vladimir Markoca6fff82017-10-03 14:49:14 +01002618static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2619 LocationSummary* locations =
2620 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002621 if (is_long) {
2622 locations->SetInAt(0, Location::RequiresRegister());
2623 } else {
2624 locations->SetInAt(0, Location::Any());
2625 }
2626 locations->SetOut(Location::RequiresRegister());
2627}
2628
Aart Bika19616e2016-02-01 18:57:58 -08002629static void GenLeadingZeros(X86Assembler* assembler,
2630 CodeGeneratorX86* codegen,
2631 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002632 LocationSummary* locations = invoke->GetLocations();
2633 Location src = locations->InAt(0);
2634 Register out = locations->Out().AsRegister<Register>();
2635
2636 if (invoke->InputAt(0)->IsConstant()) {
2637 // Evaluate this at compile time.
2638 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2639 if (value == 0) {
2640 value = is_long ? 64 : 32;
2641 } else {
2642 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2643 }
Aart Bika19616e2016-02-01 18:57:58 -08002644 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002645 return;
2646 }
2647
2648 // Handle the non-constant cases.
2649 if (!is_long) {
2650 if (src.IsRegister()) {
2651 __ bsrl(out, src.AsRegister<Register>());
2652 } else {
2653 DCHECK(src.IsStackSlot());
2654 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2655 }
2656
2657 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002658 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002659 __ j(kEqual, &all_zeroes);
2660
2661 // Correct the result from BSR to get the final CLZ result.
2662 __ xorl(out, Immediate(31));
2663 __ jmp(&done);
2664
2665 // Fix the zero case with the expected result.
2666 __ Bind(&all_zeroes);
2667 __ movl(out, Immediate(32));
2668
2669 __ Bind(&done);
2670 return;
2671 }
2672
2673 // 64 bit case needs to worry about both parts of the register.
2674 DCHECK(src.IsRegisterPair());
2675 Register src_lo = src.AsRegisterPairLow<Register>();
2676 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002677 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002678
2679 // Is the high word zero?
2680 __ testl(src_hi, src_hi);
2681 __ j(kEqual, &handle_low);
2682
2683 // High word is not zero. We know that the BSR result is defined in this case.
2684 __ bsrl(out, src_hi);
2685
2686 // Correct the result from BSR to get the final CLZ result.
2687 __ xorl(out, Immediate(31));
2688 __ jmp(&done);
2689
2690 // High word was zero. We have to compute the low word count and add 32.
2691 __ Bind(&handle_low);
2692 __ bsrl(out, src_lo);
2693 __ j(kEqual, &all_zeroes);
2694
2695 // We had a valid result. Use an XOR to both correct the result and add 32.
2696 __ xorl(out, Immediate(63));
2697 __ jmp(&done);
2698
2699 // All zero case.
2700 __ Bind(&all_zeroes);
2701 __ movl(out, Immediate(64));
2702
2703 __ Bind(&done);
2704}
2705
2706void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002707 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002708}
2709
2710void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002711 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002712}
2713
2714void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002715 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002716}
2717
2718void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002719 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002720}
2721
Vladimir Markoca6fff82017-10-03 14:49:14 +01002722static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2723 LocationSummary* locations =
2724 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002725 if (is_long) {
2726 locations->SetInAt(0, Location::RequiresRegister());
2727 } else {
2728 locations->SetInAt(0, Location::Any());
2729 }
2730 locations->SetOut(Location::RequiresRegister());
2731}
2732
Aart Bika19616e2016-02-01 18:57:58 -08002733static void GenTrailingZeros(X86Assembler* assembler,
2734 CodeGeneratorX86* codegen,
2735 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002736 LocationSummary* locations = invoke->GetLocations();
2737 Location src = locations->InAt(0);
2738 Register out = locations->Out().AsRegister<Register>();
2739
2740 if (invoke->InputAt(0)->IsConstant()) {
2741 // Evaluate this at compile time.
2742 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2743 if (value == 0) {
2744 value = is_long ? 64 : 32;
2745 } else {
2746 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2747 }
Aart Bika19616e2016-02-01 18:57:58 -08002748 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002749 return;
2750 }
2751
2752 // Handle the non-constant cases.
2753 if (!is_long) {
2754 if (src.IsRegister()) {
2755 __ bsfl(out, src.AsRegister<Register>());
2756 } else {
2757 DCHECK(src.IsStackSlot());
2758 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2759 }
2760
2761 // BSF sets ZF if the input was zero, and the output is undefined.
2762 NearLabel done;
2763 __ j(kNotEqual, &done);
2764
2765 // Fix the zero case with the expected result.
2766 __ movl(out, Immediate(32));
2767
2768 __ Bind(&done);
2769 return;
2770 }
2771
2772 // 64 bit case needs to worry about both parts of the register.
2773 DCHECK(src.IsRegisterPair());
2774 Register src_lo = src.AsRegisterPairLow<Register>();
2775 Register src_hi = src.AsRegisterPairHigh<Register>();
2776 NearLabel done, all_zeroes;
2777
2778 // If the low word is zero, then ZF will be set. If not, we have the answer.
2779 __ bsfl(out, src_lo);
2780 __ j(kNotEqual, &done);
2781
2782 // Low word was zero. We have to compute the high word count and add 32.
2783 __ bsfl(out, src_hi);
2784 __ j(kEqual, &all_zeroes);
2785
2786 // We had a valid result. Add 32 to account for the low word being zero.
2787 __ addl(out, Immediate(32));
2788 __ jmp(&done);
2789
2790 // All zero case.
2791 __ Bind(&all_zeroes);
2792 __ movl(out, Immediate(64));
2793
2794 __ Bind(&done);
2795}
2796
2797void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002798 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002799}
2800
2801void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002802 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002803}
2804
2805void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002806 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002807}
2808
2809void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002810 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002811}
2812
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002813static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2814 return instruction->InputAt(input0) == instruction->InputAt(input1);
2815}
2816
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002817// Compute base address for the System.arraycopy intrinsic in `base`.
2818static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002820 const Register& array,
2821 const Location& pos,
2822 const Register& base) {
2823 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002824 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002825 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 DCHECK_EQ(type, DataType::Type::kReference);
2827 const int32_t element_size = DataType::Size(type);
2828 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002829 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2830
2831 if (pos.IsConstant()) {
2832 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2833 __ leal(base, Address(array, element_size * constant + data_offset));
2834 } else {
2835 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2836 }
2837}
2838
2839// Compute end source address for the System.arraycopy intrinsic in `end`.
2840static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002842 const Location& copy_length,
2843 const Register& base,
2844 const Register& end) {
2845 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002846 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002847 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002848 DCHECK_EQ(type, DataType::Type::kReference);
2849 const int32_t element_size = DataType::Size(type);
2850 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002851
2852 if (copy_length.IsConstant()) {
2853 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2854 __ leal(end, Address(base, element_size * constant));
2855 } else {
2856 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2857 }
2858}
2859
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002860void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002861 // The only read barrier implementation supporting the
2862 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2863 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002864 return;
2865 }
2866
2867 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2868 if (invoke->GetLocations() != nullptr) {
2869 // Need a byte register for marking.
2870 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2871
2872 static constexpr size_t kSrc = 0;
2873 static constexpr size_t kSrcPos = 1;
2874 static constexpr size_t kDest = 2;
2875 static constexpr size_t kDestPos = 3;
2876 static constexpr size_t kLength = 4;
2877
2878 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2879 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2880 !invoke->InputAt(kLength)->IsIntConstant()) {
2881 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2882 !IsSameInput(invoke, kSrcPos, kLength) &&
2883 !IsSameInput(invoke, kDestPos, kLength) &&
2884 !IsSameInput(invoke, kSrc, kDest)) {
2885 // Not enough registers, make the length also take a stack slot.
2886 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2887 }
2888 }
2889 }
2890}
2891
2892void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002893 // The only read barrier implementation supporting the
2894 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2895 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002896
2897 X86Assembler* assembler = GetAssembler();
2898 LocationSummary* locations = invoke->GetLocations();
2899
2900 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2901 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2902 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2903 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002904 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002905
2906 Register src = locations->InAt(0).AsRegister<Register>();
2907 Location src_pos = locations->InAt(1);
2908 Register dest = locations->InAt(2).AsRegister<Register>();
2909 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002910 Location length_arg = locations->InAt(4);
2911 Location length = length_arg;
2912 Location temp1_loc = locations->GetTemp(0);
2913 Register temp1 = temp1_loc.AsRegister<Register>();
2914 Location temp2_loc = locations->GetTemp(1);
2915 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002916
Vladimir Marko174b2e22017-10-12 13:34:49 +01002917 SlowPathCode* intrinsic_slow_path =
2918 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002919 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002920
2921 NearLabel conditions_on_positions_validated;
2922 SystemArrayCopyOptimizations optimizations(invoke);
2923
2924 // If source and destination are the same, we go to slow path if we need to do
2925 // forward copying.
2926 if (src_pos.IsConstant()) {
2927 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2928 if (dest_pos.IsConstant()) {
2929 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2930 if (optimizations.GetDestinationIsSource()) {
2931 // Checked when building locations.
2932 DCHECK_GE(src_pos_constant, dest_pos_constant);
2933 } else if (src_pos_constant < dest_pos_constant) {
2934 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002935 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002936 }
2937 } else {
2938 if (!optimizations.GetDestinationIsSource()) {
2939 __ cmpl(src, dest);
2940 __ j(kNotEqual, &conditions_on_positions_validated);
2941 }
2942 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002943 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002944 }
2945 } else {
2946 if (!optimizations.GetDestinationIsSource()) {
2947 __ cmpl(src, dest);
2948 __ j(kNotEqual, &conditions_on_positions_validated);
2949 }
2950 if (dest_pos.IsConstant()) {
2951 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2952 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002953 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002954 } else {
2955 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002956 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002957 }
2958 }
2959
2960 __ Bind(&conditions_on_positions_validated);
2961
2962 if (!optimizations.GetSourceIsNotNull()) {
2963 // Bail out if the source is null.
2964 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002965 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002966 }
2967
2968 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2969 // Bail out if the destination is null.
2970 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002971 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002972 }
2973
Roland Levillain0b671c02016-08-19 12:02:34 +01002974 Location temp3_loc = locations->GetTemp(2);
2975 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002976 if (length.IsStackSlot()) {
2977 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2978 length = Location::RegisterLocation(temp3);
2979 }
2980
2981 // If the length is negative, bail out.
2982 // We have already checked in the LocationsBuilder for the constant case.
2983 if (!length.IsConstant() &&
2984 !optimizations.GetCountIsSourceLength() &&
2985 !optimizations.GetCountIsDestinationLength()) {
2986 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002987 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002988 }
2989
2990 // Validity checks: source.
2991 CheckPosition(assembler,
2992 src_pos,
2993 src,
2994 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002995 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002996 temp1,
2997 optimizations.GetCountIsSourceLength());
2998
2999 // Validity checks: dest.
3000 CheckPosition(assembler,
3001 dest_pos,
3002 dest,
3003 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01003004 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003005 temp1,
3006 optimizations.GetCountIsDestinationLength());
3007
3008 if (!optimizations.GetDoesNotNeedTypeCheck()) {
3009 // Check whether all elements of the source array are assignable to the component
3010 // type of the destination array. We do two checks: the classes are the same,
3011 // or the destination is Object[]. If none of these checks succeed, we go to the
3012 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01003013
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003014 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01003015 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3016 // /* HeapReference<Class> */ temp1 = src->klass_
3017 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003018 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003019 // Bail out if the source is not a non primitive array.
3020 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3021 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003022 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003023 __ testl(temp1, temp1);
3024 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3025 // If heap poisoning is enabled, `temp1` has been unpoisoned
3026 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3027 } else {
3028 // /* HeapReference<Class> */ temp1 = src->klass_
3029 __ movl(temp1, Address(src, class_offset));
3030 __ MaybeUnpoisonHeapReference(temp1);
3031 // Bail out if the source is not a non primitive array.
3032 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3033 __ movl(temp1, Address(temp1, component_offset));
3034 __ testl(temp1, temp1);
3035 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3036 __ MaybeUnpoisonHeapReference(temp1);
3037 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003038 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003039 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003040 }
3041
Roland Levillain0b671c02016-08-19 12:02:34 +01003042 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3043 if (length.Equals(Location::RegisterLocation(temp3))) {
3044 // When Baker read barriers are enabled, register `temp3`,
3045 // which in the present case contains the `length` parameter,
3046 // will be overwritten below. Make the `length` location
3047 // reference the original stack location; it will be moved
3048 // back to `temp3` later if necessary.
3049 DCHECK(length_arg.IsStackSlot());
3050 length = length_arg;
3051 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003052
Roland Levillain0b671c02016-08-19 12:02:34 +01003053 // /* HeapReference<Class> */ temp1 = dest->klass_
3054 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003055 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003056
Roland Levillain0b671c02016-08-19 12:02:34 +01003057 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3058 // Bail out if the destination is not a non primitive array.
3059 //
3060 // Register `temp1` is not trashed by the read barrier emitted
3061 // by GenerateFieldLoadWithBakerReadBarrier below, as that
3062 // method produces a call to a ReadBarrierMarkRegX entry point,
3063 // which saves all potentially live registers, including
3064 // temporaries such a `temp1`.
3065 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3066 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003067 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003068 __ testl(temp2, temp2);
3069 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3070 // If heap poisoning is enabled, `temp2` has been unpoisoned
3071 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3072 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3073 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3074 }
3075
3076 // For the same reason given earlier, `temp1` is not trashed by the
3077 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
3078 // /* HeapReference<Class> */ temp2 = src->klass_
3079 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003080 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003081 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
3082 __ cmpl(temp1, temp2);
3083
3084 if (optimizations.GetDestinationIsTypedObjectArray()) {
3085 NearLabel do_copy;
3086 __ j(kEqual, &do_copy);
3087 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3088 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003089 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003090 // We do not need to emit a read barrier for the following
3091 // heap reference load, as `temp1` is only used in a
3092 // comparison with null below, and this reference is not
3093 // kept afterwards.
3094 __ cmpl(Address(temp1, super_offset), Immediate(0));
3095 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3096 __ Bind(&do_copy);
3097 } else {
3098 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3099 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003100 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01003101 // Non read barrier code.
3102
3103 // /* HeapReference<Class> */ temp1 = dest->klass_
3104 __ movl(temp1, Address(dest, class_offset));
3105 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3106 __ MaybeUnpoisonHeapReference(temp1);
3107 // Bail out if the destination is not a non primitive array.
3108 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3109 __ movl(temp2, Address(temp1, component_offset));
3110 __ testl(temp2, temp2);
3111 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3112 __ MaybeUnpoisonHeapReference(temp2);
3113 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3114 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3115 // Re-poison the heap reference to make the compare instruction below
3116 // compare two poisoned references.
3117 __ PoisonHeapReference(temp1);
3118 }
3119
3120 // Note: if heap poisoning is on, we are comparing two poisoned references here.
3121 __ cmpl(temp1, Address(src, class_offset));
3122
3123 if (optimizations.GetDestinationIsTypedObjectArray()) {
3124 NearLabel do_copy;
3125 __ j(kEqual, &do_copy);
3126 __ MaybeUnpoisonHeapReference(temp1);
3127 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3128 __ movl(temp1, Address(temp1, component_offset));
3129 __ MaybeUnpoisonHeapReference(temp1);
3130 __ cmpl(Address(temp1, super_offset), Immediate(0));
3131 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3132 __ Bind(&do_copy);
3133 } else {
3134 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3135 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003136 }
3137 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
3138 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
3139 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01003140 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3141 // /* HeapReference<Class> */ temp1 = src->klass_
3142 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003143 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003144 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3145 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003146 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003147 __ testl(temp1, temp1);
3148 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3149 // If heap poisoning is enabled, `temp1` has been unpoisoned
3150 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3151 } else {
3152 // /* HeapReference<Class> */ temp1 = src->klass_
3153 __ movl(temp1, Address(src, class_offset));
3154 __ MaybeUnpoisonHeapReference(temp1);
3155 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3156 __ movl(temp1, Address(temp1, component_offset));
3157 __ testl(temp1, temp1);
3158 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3159 __ MaybeUnpoisonHeapReference(temp1);
3160 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003161 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003162 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003163 }
3164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 const DataType::Type type = DataType::Type::kReference;
3166 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003167
Roland Levillain0b671c02016-08-19 12:02:34 +01003168 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003169 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003170
Roland Levillain0b671c02016-08-19 12:02:34 +01003171 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3172 // If it is needed (in the case of the fast-path loop), the base
3173 // destination address is computed later, as `temp2` is used for
3174 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003175
Roland Levillain0b671c02016-08-19 12:02:34 +01003176 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003177 if (length.IsStackSlot()) {
3178 // Location `length` is again pointing at a stack slot, as
3179 // register `temp3` (which was containing the length parameter
3180 // earlier) has been overwritten; restore it now
3181 DCHECK(length.Equals(length_arg));
3182 __ movl(temp3, Address(ESP, length.GetStackIndex()));
3183 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003184 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003185 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003186
Roland Levillain0b671c02016-08-19 12:02:34 +01003187 // SystemArrayCopy implementation for Baker read barriers (see
3188 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
3189 //
3190 // if (src_ptr != end_ptr) {
3191 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
3192 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003193 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01003194 // if (is_gray) {
3195 // // Slow-path copy.
3196 // for (size_t i = 0; i != length; ++i) {
3197 // dest_array[dest_pos + i] =
3198 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
3199 // }
3200 // } else {
3201 // // Fast-path copy.
3202 // do {
3203 // *dest_ptr++ = *src_ptr++;
3204 // } while (src_ptr != end_ptr)
3205 // }
3206 // }
3207
3208 NearLabel loop, done;
3209
3210 // Don't enter copy loop if `length == 0`.
3211 __ cmpl(temp1, temp3);
3212 __ j(kEqual, &done);
3213
Vladimir Marko953437b2016-08-24 08:30:46 +00003214 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003215 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
3216 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00003217 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
3218 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
3219 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
3220
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003221 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00003222 // goto slow_path;
3223 // At this point, just do the "if" and make sure that flags are preserved until the branch.
3224 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01003225
3226 // Load fence to prevent load-load reordering.
3227 // Note that this is a no-op, thanks to the x86 memory model.
3228 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3229
3230 // Slow path used to copy array when `src` is gray.
3231 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003232 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01003233 codegen_->AddSlowPath(read_barrier_slow_path);
3234
Vladimir Marko953437b2016-08-24 08:30:46 +00003235 // We have done the "if" of the gray bit check above, now branch based on the flags.
3236 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01003237
3238 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003239 // Compute the base destination address in `temp2`.
3240 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003241 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3242 // poison/unpoison.
3243 __ Bind(&loop);
3244 __ pushl(Address(temp1, 0));
3245 __ cfi().AdjustCFAOffset(4);
3246 __ popl(Address(temp2, 0));
3247 __ cfi().AdjustCFAOffset(-4);
3248 __ addl(temp1, Immediate(element_size));
3249 __ addl(temp2, Immediate(element_size));
3250 __ cmpl(temp1, temp3);
3251 __ j(kNotEqual, &loop);
3252
3253 __ Bind(read_barrier_slow_path->GetExitLabel());
3254 __ Bind(&done);
3255 } else {
3256 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01003257 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003258 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003259 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003260 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003261 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3262 // poison/unpoison.
3263 NearLabel loop, done;
3264 __ cmpl(temp1, temp3);
3265 __ j(kEqual, &done);
3266 __ Bind(&loop);
3267 __ pushl(Address(temp1, 0));
3268 __ cfi().AdjustCFAOffset(4);
3269 __ popl(Address(temp2, 0));
3270 __ cfi().AdjustCFAOffset(-4);
3271 __ addl(temp1, Immediate(element_size));
3272 __ addl(temp2, Immediate(element_size));
3273 __ cmpl(temp1, temp3);
3274 __ j(kNotEqual, &loop);
3275 __ Bind(&done);
3276 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003277
3278 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003279 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003280
Roland Levillain0b671c02016-08-19 12:02:34 +01003281 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003282}
3283
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003284void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
3285 InvokeRuntimeCallingConvention calling_convention;
3286 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3287 invoke,
3288 codegen_,
3289 Location::RegisterLocation(EAX),
3290 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3291}
3292
3293void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
3294 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3295 LocationSummary* locations = invoke->GetLocations();
3296 X86Assembler* assembler = GetAssembler();
3297
3298 Register out = locations->Out().AsRegister<Register>();
3299 InvokeRuntimeCallingConvention calling_convention;
3300 if (invoke->InputAt(0)->IsConstant()) {
3301 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3302 if (value >= info.low && value <= info.high) {
3303 // Just embed the j.l.Integer in the code.
3304 ScopedObjectAccess soa(Thread::Current());
3305 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3306 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3307 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3308 __ movl(out, Immediate(address));
3309 } else {
3310 // Allocate and initialize a new j.l.Integer.
3311 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3312 // JIT object table.
3313 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3314 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3315 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3316 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3317 __ movl(Address(out, info.value_offset), Immediate(value));
3318 }
3319 } else {
3320 Register in = locations->InAt(0).AsRegister<Register>();
3321 // Check bounds of our cache.
3322 __ leal(out, Address(in, -info.low));
3323 __ cmpl(out, Immediate(info.high - info.low + 1));
3324 NearLabel allocate, done;
3325 __ j(kAboveEqual, &allocate);
3326 // If the value is within the bounds, load the j.l.Integer directly from the array.
3327 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3328 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3329 __ movl(out, Address(out, TIMES_4, data_offset + address));
3330 __ MaybeUnpoisonHeapReference(out);
3331 __ jmp(&done);
3332 __ Bind(&allocate);
3333 // Otherwise allocate and initialize a new j.l.Integer.
3334 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3335 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3336 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3337 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3338 __ movl(Address(out, info.value_offset), in);
3339 __ Bind(&done);
3340 }
3341}
3342
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003343void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003344 LocationSummary* locations =
3345 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003346 locations->SetOut(Location::RequiresRegister());
3347}
3348
3349void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
3350 X86Assembler* assembler = GetAssembler();
3351 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
3352 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
3353 NearLabel done;
3354 __ fs()->movl(out, address);
3355 __ testl(out, out);
3356 __ j(kEqual, &done);
3357 __ fs()->movl(address, Immediate(0));
3358 codegen_->MemoryFence();
3359 __ Bind(&done);
3360}
3361
3362
Aart Bik2f9fcc92016-03-01 15:16:54 -08003363UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003364UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003365UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
3366UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
3367UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
3368UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
3369UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
3370UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
Mark Mendell09ed1a32015-03-25 08:30:06 -04003371
Aart Bikff7d89c2016-11-07 08:49:28 -08003372UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
3373UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003374UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
3375UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
3376UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
3377UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
3378UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
3379UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003380
Aart Bik0e54c012016-03-04 12:08:31 -08003381// 1.8.
3382UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
3383UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
3384UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
3385UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
3386UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003387
Aart Bik2f9fcc92016-03-01 15:16:54 -08003388UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01003389
3390#undef __
3391
Mark Mendell09ed1a32015-03-25 08:30:06 -04003392} // namespace x86
3393} // namespace art