blob: 8a0b6aeb0ee9d46e4004d763d2ae762853335db3 [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
1108void IntrinsicLocationsBuilderX86::VisitMathHypot(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001109 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001110}
1111
1112void IntrinsicCodeGeneratorX86::VisitMathHypot(HInvoke* invoke) {
1113 GenFPToFPCall(invoke, codegen_, kQuickHypot);
1114}
1115
1116void IntrinsicLocationsBuilderX86::VisitMathNextAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001117 CreateFPFPToFPCallLocations(allocator_, invoke);
Mark Mendella4f12202015-08-06 15:23:34 -04001118}
1119
1120void IntrinsicCodeGeneratorX86::VisitMathNextAfter(HInvoke* invoke) {
1121 GenFPToFPCall(invoke, codegen_, kQuickNextAfter);
1122}
1123
Mark Mendell6bc53a92015-07-01 14:26:52 -04001124void IntrinsicLocationsBuilderX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1125 // We need at least two of the positions or length to be an integer constant,
1126 // or else we won't have enough free registers.
1127 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1128 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1129 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1130
1131 int num_constants =
1132 ((src_pos != nullptr) ? 1 : 0)
1133 + ((dest_pos != nullptr) ? 1 : 0)
1134 + ((length != nullptr) ? 1 : 0);
1135
1136 if (num_constants < 2) {
1137 // Not enough free registers.
1138 return;
1139 }
1140
1141 // As long as we are checking, we might as well check to see if the src and dest
1142 // positions are >= 0.
1143 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1144 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1145 // We will have to fail anyways.
1146 return;
1147 }
1148
1149 // And since we are already checking, check the length too.
1150 if (length != nullptr) {
1151 int32_t len = length->GetValue();
1152 if (len < 0) {
1153 // Just call as normal.
1154 return;
1155 }
1156 }
1157
1158 // Okay, it is safe to generate inline code.
1159 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001160 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnSlowPath, kIntrinsified);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001161 // arraycopy(Object src, int srcPos, Object dest, int destPos, int length).
1162 locations->SetInAt(0, Location::RequiresRegister());
1163 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1164 locations->SetInAt(2, Location::RequiresRegister());
1165 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1166 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1167
1168 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1169 locations->AddTemp(Location::RegisterLocation(ESI));
1170 locations->AddTemp(Location::RegisterLocation(EDI));
1171 locations->AddTemp(Location::RegisterLocation(ECX));
1172}
1173
1174static void CheckPosition(X86Assembler* assembler,
1175 Location pos,
1176 Register input,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001177 Location length,
Andreas Gampe85b62f22015-09-09 13:15:38 -07001178 SlowPathCode* slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001179 Register temp,
1180 bool length_is_input_length = false) {
1181 // Where is the length in the Array?
Mark Mendell6bc53a92015-07-01 14:26:52 -04001182 const uint32_t length_offset = mirror::Array::LengthOffset().Uint32Value();
1183
1184 if (pos.IsConstant()) {
1185 int32_t pos_const = pos.GetConstant()->AsIntConstant()->GetValue();
1186 if (pos_const == 0) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001187 if (!length_is_input_length) {
1188 // Check that length(input) >= length.
1189 if (length.IsConstant()) {
1190 __ cmpl(Address(input, length_offset),
1191 Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1192 } else {
1193 __ cmpl(Address(input, length_offset), length.AsRegister<Register>());
1194 }
1195 __ j(kLess, slow_path->GetEntryLabel());
1196 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001197 } else {
1198 // Check that length(input) >= pos.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001199 __ movl(temp, Address(input, length_offset));
1200 __ subl(temp, Immediate(pos_const));
Mark Mendell6bc53a92015-07-01 14:26:52 -04001201 __ j(kLess, slow_path->GetEntryLabel());
1202
1203 // Check that (length(input) - pos) >= length.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001204 if (length.IsConstant()) {
1205 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1206 } else {
1207 __ cmpl(temp, length.AsRegister<Register>());
1208 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001209 __ j(kLess, slow_path->GetEntryLabel());
1210 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001211 } else if (length_is_input_length) {
1212 // The only way the copy can succeed is if pos is zero.
1213 Register pos_reg = pos.AsRegister<Register>();
1214 __ testl(pos_reg, pos_reg);
1215 __ j(kNotEqual, slow_path->GetEntryLabel());
Mark Mendell6bc53a92015-07-01 14:26:52 -04001216 } else {
1217 // Check that pos >= 0.
1218 Register pos_reg = pos.AsRegister<Register>();
1219 __ testl(pos_reg, pos_reg);
1220 __ j(kLess, slow_path->GetEntryLabel());
1221
1222 // Check that pos <= length(input).
1223 __ cmpl(Address(input, length_offset), pos_reg);
1224 __ j(kLess, slow_path->GetEntryLabel());
1225
1226 // Check that (length(input) - pos) >= length.
1227 __ movl(temp, Address(input, length_offset));
1228 __ subl(temp, pos_reg);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001229 if (length.IsConstant()) {
1230 __ cmpl(temp, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1231 } else {
1232 __ cmpl(temp, length.AsRegister<Register>());
1233 }
Mark Mendell6bc53a92015-07-01 14:26:52 -04001234 __ j(kLess, slow_path->GetEntryLabel());
1235 }
1236}
1237
1238void IntrinsicCodeGeneratorX86::VisitSystemArrayCopyChar(HInvoke* invoke) {
1239 X86Assembler* assembler = GetAssembler();
1240 LocationSummary* locations = invoke->GetLocations();
1241
1242 Register src = locations->InAt(0).AsRegister<Register>();
1243 Location srcPos = locations->InAt(1);
1244 Register dest = locations->InAt(2).AsRegister<Register>();
1245 Location destPos = locations->InAt(3);
1246 Location length = locations->InAt(4);
1247
1248 // Temporaries that we need for MOVSW.
1249 Register src_base = locations->GetTemp(0).AsRegister<Register>();
1250 DCHECK_EQ(src_base, ESI);
1251 Register dest_base = locations->GetTemp(1).AsRegister<Register>();
1252 DCHECK_EQ(dest_base, EDI);
1253 Register count = locations->GetTemp(2).AsRegister<Register>();
1254 DCHECK_EQ(count, ECX);
1255
Vladimir Marko174b2e22017-10-12 13:34:49 +01001256 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001257 codegen_->AddSlowPath(slow_path);
1258
1259 // Bail out if the source and destination are the same (to handle overlap).
1260 __ cmpl(src, dest);
1261 __ j(kEqual, slow_path->GetEntryLabel());
1262
1263 // Bail out if the source is null.
1264 __ testl(src, src);
1265 __ j(kEqual, slow_path->GetEntryLabel());
1266
1267 // Bail out if the destination is null.
1268 __ testl(dest, dest);
1269 __ j(kEqual, slow_path->GetEntryLabel());
1270
1271 // If the length is negative, bail out.
1272 // We have already checked in the LocationsBuilder for the constant case.
1273 if (!length.IsConstant()) {
1274 __ cmpl(length.AsRegister<Register>(), length.AsRegister<Register>());
1275 __ j(kLess, slow_path->GetEntryLabel());
1276 }
1277
1278 // We need the count in ECX.
1279 if (length.IsConstant()) {
1280 __ movl(count, Immediate(length.GetConstant()->AsIntConstant()->GetValue()));
1281 } else {
1282 __ movl(count, length.AsRegister<Register>());
1283 }
1284
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001285 // Validity checks: source. Use src_base as a temporary register.
1286 CheckPosition(assembler, srcPos, src, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001287
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01001288 // Validity checks: dest. Use src_base as a temporary register.
1289 CheckPosition(assembler, destPos, dest, Location::RegisterLocation(count), slow_path, src_base);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001290
1291 // Okay, everything checks out. Finally time to do the copy.
1292 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001293 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell6bc53a92015-07-01 14:26:52 -04001294 DCHECK_EQ(char_size, 2u);
1295
1296 const uint32_t data_offset = mirror::Array::DataOffset(char_size).Uint32Value();
1297
1298 if (srcPos.IsConstant()) {
1299 int32_t srcPos_const = srcPos.GetConstant()->AsIntConstant()->GetValue();
1300 __ leal(src_base, Address(src, char_size * srcPos_const + data_offset));
1301 } else {
1302 __ leal(src_base, Address(src, srcPos.AsRegister<Register>(),
1303 ScaleFactor::TIMES_2, data_offset));
1304 }
1305 if (destPos.IsConstant()) {
1306 int32_t destPos_const = destPos.GetConstant()->AsIntConstant()->GetValue();
1307
1308 __ leal(dest_base, Address(dest, char_size * destPos_const + data_offset));
1309 } else {
1310 __ leal(dest_base, Address(dest, destPos.AsRegister<Register>(),
1311 ScaleFactor::TIMES_2, data_offset));
1312 }
1313
1314 // Do the move.
1315 __ rep_movsw();
1316
1317 __ Bind(slow_path->GetExitLabel());
1318}
1319
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001320void IntrinsicLocationsBuilderX86::VisitStringCompareTo(HInvoke* invoke) {
1321 // The inputs plus one temp.
Vladimir Markoca6fff82017-10-03 14:49:14 +01001322 LocationSummary* locations = new (allocator_) LocationSummary(
1323 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001324 InvokeRuntimeCallingConvention calling_convention;
1325 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1326 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1327 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001328}
1329
1330void IntrinsicCodeGeneratorX86::VisitStringCompareTo(HInvoke* invoke) {
1331 X86Assembler* assembler = GetAssembler();
1332 LocationSummary* locations = invoke->GetLocations();
1333
Nicolas Geoffray512e04d2015-03-27 17:21:24 +00001334 // Note that the null check must have been done earlier.
Calin Juravle641547a2015-04-21 22:08:51 +01001335 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001336
1337 Register argument = locations->InAt(1).AsRegister<Register>();
1338 __ testl(argument, argument);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001339 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001340 codegen_->AddSlowPath(slow_path);
1341 __ j(kEqual, slow_path->GetEntryLabel());
1342
Serban Constantinescuba45db02016-07-12 22:53:02 +01001343 codegen_->InvokeRuntime(kQuickStringCompareTo, invoke, invoke->GetDexPc(), slow_path);
Nicolas Geoffrayd75948a2015-03-27 09:53:16 +00001344 __ Bind(slow_path->GetExitLabel());
1345}
1346
Agi Csakid7138c82015-08-13 17:46:44 -07001347void IntrinsicLocationsBuilderX86::VisitStringEquals(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001348 LocationSummary* locations =
1349 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Agi Csakid7138c82015-08-13 17:46:44 -07001350 locations->SetInAt(0, Location::RequiresRegister());
1351 locations->SetInAt(1, Location::RequiresRegister());
1352
1353 // Request temporary registers, ECX and EDI needed for repe_cmpsl instruction.
1354 locations->AddTemp(Location::RegisterLocation(ECX));
1355 locations->AddTemp(Location::RegisterLocation(EDI));
1356
1357 // Set output, ESI needed for repe_cmpsl instruction anyways.
1358 locations->SetOut(Location::RegisterLocation(ESI), Location::kOutputOverlap);
1359}
1360
1361void IntrinsicCodeGeneratorX86::VisitStringEquals(HInvoke* invoke) {
1362 X86Assembler* assembler = GetAssembler();
1363 LocationSummary* locations = invoke->GetLocations();
1364
1365 Register str = locations->InAt(0).AsRegister<Register>();
1366 Register arg = locations->InAt(1).AsRegister<Register>();
1367 Register ecx = locations->GetTemp(0).AsRegister<Register>();
1368 Register edi = locations->GetTemp(1).AsRegister<Register>();
1369 Register esi = locations->Out().AsRegister<Register>();
1370
Mark Mendell0c9497d2015-08-21 09:30:05 -04001371 NearLabel end, return_true, return_false;
Agi Csakid7138c82015-08-13 17:46:44 -07001372
1373 // Get offsets of count, value, and class fields within a string object.
1374 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
1375 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1376 const uint32_t class_offset = mirror::Object::ClassOffset().Uint32Value();
1377
1378 // Note that the null check must have been done earlier.
1379 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1380
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001381 StringEqualsOptimizations optimizations(invoke);
1382 if (!optimizations.GetArgumentNotNull()) {
1383 // Check if input is null, return false if it is.
1384 __ testl(arg, arg);
1385 __ j(kEqual, &return_false);
1386 }
Agi Csakid7138c82015-08-13 17:46:44 -07001387
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01001388 if (!optimizations.GetArgumentIsString()) {
Vladimir Marko53b52002016-05-24 19:30:45 +01001389 // Instanceof check for the argument by comparing class fields.
1390 // All string objects must have the same type since String cannot be subclassed.
1391 // Receiver must be a string object, so its class field is equal to all strings' class fields.
1392 // 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 +01001393 __ movl(ecx, Address(str, class_offset));
1394 __ cmpl(ecx, Address(arg, class_offset));
1395 __ j(kNotEqual, &return_false);
1396 }
Agi Csakid7138c82015-08-13 17:46:44 -07001397
1398 // Reference equality check, return true if same reference.
1399 __ cmpl(str, arg);
1400 __ j(kEqual, &return_true);
1401
jessicahandojo4877b792016-09-08 19:49:13 -07001402 // Load length and compression flag of receiver string.
Agi Csakid7138c82015-08-13 17:46:44 -07001403 __ movl(ecx, Address(str, count_offset));
jessicahandojo4877b792016-09-08 19:49:13 -07001404 // Check if lengths and compression flags are equal, return false if they're not.
1405 // Two identical strings will always have same compression style since
1406 // compression style is decided on alloc.
Agi Csakid7138c82015-08-13 17:46:44 -07001407 __ cmpl(ecx, Address(arg, count_offset));
1408 __ j(kNotEqual, &return_false);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001409 // Return true if strings are empty. Even with string compression `count == 0` means empty.
1410 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1411 "Expecting 0=compressed, 1=uncompressed");
1412 __ jecxz(&return_true);
Agi Csakid7138c82015-08-13 17:46:44 -07001413
jessicahandojo4877b792016-09-08 19:49:13 -07001414 if (mirror::kUseStringCompression) {
1415 NearLabel string_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001416 // Extract length and differentiate between both compressed or both uncompressed.
1417 // Different compression style is cut above.
1418 __ shrl(ecx, Immediate(1));
1419 __ j(kCarrySet, &string_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001420 // Divide string length by 2, rounding up, and continue as if uncompressed.
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001421 __ addl(ecx, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07001422 __ shrl(ecx, Immediate(1));
1423 __ Bind(&string_uncompressed);
1424 }
Agi Csakid7138c82015-08-13 17:46:44 -07001425 // Load starting addresses of string values into ESI/EDI as required for repe_cmpsl instruction.
1426 __ leal(esi, Address(str, value_offset));
1427 __ leal(edi, Address(arg, value_offset));
1428
jessicahandojo4877b792016-09-08 19:49:13 -07001429 // Divide string length by 2 to compare characters 2 at a time and adjust for lengths not
1430 // divisible by 2.
Agi Csakid7138c82015-08-13 17:46:44 -07001431 __ addl(ecx, Immediate(1));
1432 __ shrl(ecx, Immediate(1));
1433
jessicahandojo4877b792016-09-08 19:49:13 -07001434 // Assertions that must hold in order to compare strings 2 characters (uncompressed)
1435 // or 4 characters (compressed) at a time.
Agi Csakid7138c82015-08-13 17:46:44 -07001436 DCHECK_ALIGNED(value_offset, 4);
1437 static_assert(IsAligned<4>(kObjectAlignment), "String of odd length is not zero padded");
1438
1439 // Loop to compare strings two characters at a time starting at the beginning of the string.
1440 __ repe_cmpsl();
1441 // If strings are not equal, zero flag will be cleared.
1442 __ j(kNotEqual, &return_false);
1443
1444 // Return true and exit the function.
1445 // If loop does not result in returning false, we return true.
1446 __ Bind(&return_true);
1447 __ movl(esi, Immediate(1));
1448 __ jmp(&end);
1449
1450 // Return false and exit the function.
1451 __ Bind(&return_false);
1452 __ xorl(esi, esi);
1453 __ Bind(&end);
1454}
1455
Andreas Gampe21030dd2015-05-07 14:46:15 -07001456static void CreateStringIndexOfLocations(HInvoke* invoke,
1457 ArenaAllocator* allocator,
1458 bool start_at_zero) {
1459 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1460 LocationSummary::kCallOnSlowPath,
1461 kIntrinsified);
1462 // The data needs to be in EDI for scasw. So request that the string is there, anyways.
1463 locations->SetInAt(0, Location::RegisterLocation(EDI));
1464 // If we look for a constant char, we'll still have to copy it into EAX. So just request the
1465 // allocator to do that, anyways. We can still do the constant check by checking the parameter
1466 // of the instruction explicitly.
1467 // Note: This works as we don't clobber EAX anywhere.
1468 locations->SetInAt(1, Location::RegisterLocation(EAX));
1469 if (!start_at_zero) {
1470 locations->SetInAt(2, Location::RequiresRegister()); // The starting index.
1471 }
1472 // As we clobber EDI during execution anyways, also use it as the output.
1473 locations->SetOut(Location::SameAsFirstInput());
1474
1475 // repne scasw uses ECX as the counter.
1476 locations->AddTemp(Location::RegisterLocation(ECX));
1477 // Need another temporary to be able to compute the result.
1478 locations->AddTemp(Location::RequiresRegister());
jessicahandojo4877b792016-09-08 19:49:13 -07001479 if (mirror::kUseStringCompression) {
1480 // Need another temporary to be able to save unflagged string length.
1481 locations->AddTemp(Location::RequiresRegister());
1482 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001483}
1484
1485static void GenerateStringIndexOf(HInvoke* invoke,
1486 X86Assembler* assembler,
1487 CodeGeneratorX86* codegen,
Andreas Gampe21030dd2015-05-07 14:46:15 -07001488 bool start_at_zero) {
1489 LocationSummary* locations = invoke->GetLocations();
1490
1491 // Note that the null check must have been done earlier.
1492 DCHECK(!invoke->CanDoImplicitNullCheckOn(invoke->InputAt(0)));
1493
1494 Register string_obj = locations->InAt(0).AsRegister<Register>();
1495 Register search_value = locations->InAt(1).AsRegister<Register>();
1496 Register counter = locations->GetTemp(0).AsRegister<Register>();
1497 Register string_length = locations->GetTemp(1).AsRegister<Register>();
1498 Register out = locations->Out().AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07001499 // Only used when string compression feature is on.
1500 Register string_length_flagged;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001501
1502 // Check our assumptions for registers.
1503 DCHECK_EQ(string_obj, EDI);
1504 DCHECK_EQ(search_value, EAX);
1505 DCHECK_EQ(counter, ECX);
1506 DCHECK_EQ(out, EDI);
1507
1508 // Check for code points > 0xFFFF. Either a slow-path check when we don't know statically,
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001509 // 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 -07001510 SlowPathCode* slow_path = nullptr;
Vladimir Markofb6c90a2016-05-06 15:52:12 +01001511 HInstruction* code_point = invoke->InputAt(1);
1512 if (code_point->IsIntConstant()) {
Vladimir Markoda051082016-05-17 16:10:20 +01001513 if (static_cast<uint32_t>(code_point->AsIntConstant()->GetValue()) >
Andreas Gampe21030dd2015-05-07 14:46:15 -07001514 std::numeric_limits<uint16_t>::max()) {
1515 // Always needs the slow-path. We could directly dispatch to it, but this case should be
1516 // rare, so for simplicity just put the full slow-path down and branch unconditionally.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001517 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001518 codegen->AddSlowPath(slow_path);
1519 __ jmp(slow_path->GetEntryLabel());
1520 __ Bind(slow_path->GetExitLabel());
1521 return;
1522 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001523 } else if (code_point->GetType() != DataType::Type::kUint16) {
Andreas Gampe21030dd2015-05-07 14:46:15 -07001524 __ cmpl(search_value, Immediate(std::numeric_limits<uint16_t>::max()));
Vladimir Marko174b2e22017-10-12 13:34:49 +01001525 slow_path = new (codegen->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001526 codegen->AddSlowPath(slow_path);
1527 __ j(kAbove, slow_path->GetEntryLabel());
1528 }
1529
1530 // From here down, we know that we are looking for a char that fits in 16 bits.
1531 // Location of reference to data array within the String object.
1532 int32_t value_offset = mirror::String::ValueOffset().Int32Value();
1533 // Location of count within the String object.
1534 int32_t count_offset = mirror::String::CountOffset().Int32Value();
1535
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001536 // Load the count field of the string containing the length and compression flag.
Andreas Gampe21030dd2015-05-07 14:46:15 -07001537 __ movl(string_length, Address(string_obj, count_offset));
1538
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001539 // Do a zero-length check. Even with string compression `count == 0` means empty.
1540 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1541 "Expecting 0=compressed, 1=uncompressed");
Andreas Gampe21030dd2015-05-07 14:46:15 -07001542 // TODO: Support jecxz.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001543 NearLabel not_found_label;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001544 __ testl(string_length, string_length);
1545 __ j(kEqual, &not_found_label);
1546
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001547 if (mirror::kUseStringCompression) {
1548 string_length_flagged = locations->GetTemp(2).AsRegister<Register>();
1549 __ movl(string_length_flagged, string_length);
1550 // Extract the length and shift out the least significant bit used as compression flag.
1551 __ shrl(string_length, Immediate(1));
1552 }
1553
Andreas Gampe21030dd2015-05-07 14:46:15 -07001554 if (start_at_zero) {
1555 // Number of chars to scan is the same as the string length.
1556 __ movl(counter, string_length);
1557
1558 // Move to the start of the string.
1559 __ addl(string_obj, Immediate(value_offset));
1560 } else {
1561 Register start_index = locations->InAt(2).AsRegister<Register>();
1562
1563 // Do a start_index check.
1564 __ cmpl(start_index, string_length);
1565 __ j(kGreaterEqual, &not_found_label);
1566
1567 // Ensure we have a start index >= 0;
1568 __ xorl(counter, counter);
1569 __ cmpl(start_index, Immediate(0));
1570 __ cmovl(kGreater, counter, start_index);
1571
jessicahandojo4877b792016-09-08 19:49:13 -07001572 if (mirror::kUseStringCompression) {
1573 NearLabel modify_counter, offset_uncompressed_label;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001574 __ testl(string_length_flagged, Immediate(1));
1575 __ j(kNotZero, &offset_uncompressed_label);
jessicahandojo4877b792016-09-08 19:49:13 -07001576 // Move to the start of the string: string_obj + value_offset + start_index.
1577 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_1, value_offset));
1578 __ jmp(&modify_counter);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001579
jessicahandojo4877b792016-09-08 19:49:13 -07001580 // Move to the start of the string: string_obj + value_offset + 2 * start_index.
1581 __ Bind(&offset_uncompressed_label);
1582 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1583
1584 // Now update ecx (the repne scasw work counter). We have string.length - start_index left to
1585 // compare.
1586 __ Bind(&modify_counter);
1587 } else {
1588 __ leal(string_obj, Address(string_obj, counter, ScaleFactor::TIMES_2, value_offset));
1589 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001590 __ negl(counter);
1591 __ leal(counter, Address(string_length, counter, ScaleFactor::TIMES_1, 0));
1592 }
1593
jessicahandojo4877b792016-09-08 19:49:13 -07001594 if (mirror::kUseStringCompression) {
1595 NearLabel uncompressed_string_comparison;
1596 NearLabel comparison_done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001597 __ testl(string_length_flagged, Immediate(1));
1598 __ j(kNotZero, &uncompressed_string_comparison);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001599
jessicahandojo4877b792016-09-08 19:49:13 -07001600 // Check if EAX (search_value) is ASCII.
1601 __ cmpl(search_value, Immediate(127));
1602 __ j(kGreater, &not_found_label);
1603 // Comparing byte-per-byte.
1604 __ repne_scasb();
1605 __ jmp(&comparison_done);
1606
1607 // Everything is set up for repne scasw:
1608 // * Comparison address in EDI.
1609 // * Counter in ECX.
1610 __ Bind(&uncompressed_string_comparison);
1611 __ repne_scasw();
1612 __ Bind(&comparison_done);
1613 } else {
1614 __ repne_scasw();
1615 }
Andreas Gampe21030dd2015-05-07 14:46:15 -07001616 // Did we find a match?
1617 __ j(kNotEqual, &not_found_label);
1618
1619 // Yes, we matched. Compute the index of the result.
1620 __ subl(string_length, counter);
1621 __ leal(out, Address(string_length, -1));
1622
Mark Mendell0c9497d2015-08-21 09:30:05 -04001623 NearLabel done;
Andreas Gampe21030dd2015-05-07 14:46:15 -07001624 __ jmp(&done);
1625
1626 // Failed to match; return -1.
1627 __ Bind(&not_found_label);
1628 __ movl(out, Immediate(-1));
1629
1630 // And join up at the end.
1631 __ Bind(&done);
1632 if (slow_path != nullptr) {
1633 __ Bind(slow_path->GetExitLabel());
1634 }
1635}
1636
1637void IntrinsicLocationsBuilderX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001638 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001639}
1640
1641void IntrinsicCodeGeneratorX86::VisitStringIndexOf(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001642 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ true);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001643}
1644
1645void IntrinsicLocationsBuilderX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001646 CreateStringIndexOfLocations(invoke, allocator_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001647}
1648
1649void IntrinsicCodeGeneratorX86::VisitStringIndexOfAfter(HInvoke* invoke) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001650 GenerateStringIndexOf(invoke, GetAssembler(), codegen_, /* start_at_zero */ false);
Andreas Gampe21030dd2015-05-07 14:46:15 -07001651}
1652
Jeff Hao848f70a2014-01-15 13:49:50 -08001653void IntrinsicLocationsBuilderX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001654 LocationSummary* locations = new (allocator_) LocationSummary(
1655 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001656 InvokeRuntimeCallingConvention calling_convention;
1657 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1658 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1659 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1660 locations->SetInAt(3, Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
1661 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001662}
1663
1664void IntrinsicCodeGeneratorX86::VisitStringNewStringFromBytes(HInvoke* invoke) {
1665 X86Assembler* assembler = GetAssembler();
1666 LocationSummary* locations = invoke->GetLocations();
1667
1668 Register byte_array = locations->InAt(0).AsRegister<Register>();
1669 __ testl(byte_array, byte_array);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001670 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001671 codegen_->AddSlowPath(slow_path);
1672 __ j(kEqual, slow_path->GetEntryLabel());
1673
Serban Constantinescuba45db02016-07-12 22:53:02 +01001674 codegen_->InvokeRuntime(kQuickAllocStringFromBytes, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001675 CheckEntrypointTypes<kQuickAllocStringFromBytes, void*, void*, int32_t, int32_t, int32_t>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001676 __ Bind(slow_path->GetExitLabel());
1677}
1678
1679void IntrinsicLocationsBuilderX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001680 LocationSummary* locations =
1681 new (allocator_) LocationSummary(invoke, LocationSummary::kCallOnMainOnly, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001682 InvokeRuntimeCallingConvention calling_convention;
1683 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1684 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
1685 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
1686 locations->SetOut(Location::RegisterLocation(EAX));
1687}
1688
1689void IntrinsicCodeGeneratorX86::VisitStringNewStringFromChars(HInvoke* invoke) {
Roland Levillaincc3839c2016-02-29 16:23:48 +00001690 // No need to emit code checking whether `locations->InAt(2)` is a null
1691 // pointer, as callers of the native method
1692 //
1693 // java.lang.StringFactory.newStringFromChars(int offset, int charCount, char[] data)
1694 //
1695 // all include a null check on `data` before calling that method.
Serban Constantinescuba45db02016-07-12 22:53:02 +01001696 codegen_->InvokeRuntime(kQuickAllocStringFromChars, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001697 CheckEntrypointTypes<kQuickAllocStringFromChars, void*, int32_t, int32_t, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001698}
1699
1700void IntrinsicLocationsBuilderX86::VisitStringNewStringFromString(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001701 LocationSummary* locations = new (allocator_) LocationSummary(
1702 invoke, LocationSummary::kCallOnMainAndSlowPath, kIntrinsified);
Jeff Hao848f70a2014-01-15 13:49:50 -08001703 InvokeRuntimeCallingConvention calling_convention;
1704 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1705 locations->SetOut(Location::RegisterLocation(EAX));
Jeff Hao848f70a2014-01-15 13:49:50 -08001706}
1707
1708void IntrinsicCodeGeneratorX86::VisitStringNewStringFromString(HInvoke* invoke) {
1709 X86Assembler* assembler = GetAssembler();
1710 LocationSummary* locations = invoke->GetLocations();
1711
1712 Register string_to_copy = locations->InAt(0).AsRegister<Register>();
1713 __ testl(string_to_copy, string_to_copy);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001714 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Jeff Hao848f70a2014-01-15 13:49:50 -08001715 codegen_->AddSlowPath(slow_path);
1716 __ j(kEqual, slow_path->GetEntryLabel());
1717
Serban Constantinescuba45db02016-07-12 22:53:02 +01001718 codegen_->InvokeRuntime(kQuickAllocStringFromString, invoke, invoke->GetDexPc());
Roland Levillainf969a202016-03-09 16:14:00 +00001719 CheckEntrypointTypes<kQuickAllocStringFromString, void*, void*>();
Jeff Hao848f70a2014-01-15 13:49:50 -08001720 __ Bind(slow_path->GetExitLabel());
1721}
1722
Mark Mendell8f8926a2015-08-17 11:39:06 -04001723void IntrinsicLocationsBuilderX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1724 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001725 LocationSummary* locations =
1726 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001727 locations->SetInAt(0, Location::RequiresRegister());
1728 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1729 // Place srcEnd in ECX to save a move below.
1730 locations->SetInAt(2, Location::RegisterLocation(ECX));
1731 locations->SetInAt(3, Location::RequiresRegister());
1732 locations->SetInAt(4, Location::RequiresRegister());
1733
1734 // And we need some temporaries. We will use REP MOVSW, so we need fixed registers.
1735 // We don't have enough registers to also grab ECX, so handle below.
1736 locations->AddTemp(Location::RegisterLocation(ESI));
1737 locations->AddTemp(Location::RegisterLocation(EDI));
1738}
1739
1740void IntrinsicCodeGeneratorX86::VisitStringGetCharsNoCheck(HInvoke* invoke) {
1741 X86Assembler* assembler = GetAssembler();
1742 LocationSummary* locations = invoke->GetLocations();
1743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001744 size_t char_component_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001745 // Location of data in char array buffer.
1746 const uint32_t data_offset = mirror::Array::DataOffset(char_component_size).Uint32Value();
1747 // Location of char array data in string.
1748 const uint32_t value_offset = mirror::String::ValueOffset().Uint32Value();
1749
1750 // public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
1751 Register obj = locations->InAt(0).AsRegister<Register>();
1752 Location srcBegin = locations->InAt(1);
1753 int srcBegin_value =
1754 srcBegin.IsConstant() ? srcBegin.GetConstant()->AsIntConstant()->GetValue() : 0;
1755 Register srcEnd = locations->InAt(2).AsRegister<Register>();
1756 Register dst = locations->InAt(3).AsRegister<Register>();
1757 Register dstBegin = locations->InAt(4).AsRegister<Register>();
1758
1759 // Check assumption that sizeof(Char) is 2 (used in scaling below).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001760 const size_t char_size = DataType::Size(DataType::Type::kUint16);
Mark Mendell8f8926a2015-08-17 11:39:06 -04001761 DCHECK_EQ(char_size, 2u);
1762
Mark Mendell8f8926a2015-08-17 11:39:06 -04001763 // Compute the number of chars (words) to move.
jessicahandojo4877b792016-09-08 19:49:13 -07001764 // Save ECX, since we don't know if it will be used later.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001765 __ pushl(ECX);
1766 int stack_adjust = kX86WordSize;
1767 __ cfi().AdjustCFAOffset(stack_adjust);
1768 DCHECK_EQ(srcEnd, ECX);
1769 if (srcBegin.IsConstant()) {
jessicahandojo4877b792016-09-08 19:49:13 -07001770 __ subl(ECX, Immediate(srcBegin_value));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001771 } else {
1772 DCHECK(srcBegin.IsRegister());
1773 __ subl(ECX, srcBegin.AsRegister<Register>());
1774 }
1775
jessicahandojo4877b792016-09-08 19:49:13 -07001776 NearLabel done;
1777 if (mirror::kUseStringCompression) {
1778 // Location of count in string
1779 const uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001780 const size_t c_char_size = DataType::Size(DataType::Type::kInt8);
jessicahandojo4877b792016-09-08 19:49:13 -07001781 DCHECK_EQ(c_char_size, 1u);
1782 __ pushl(EAX);
1783 __ cfi().AdjustCFAOffset(stack_adjust);
1784
1785 NearLabel copy_loop, copy_uncompressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001786 __ testl(Address(obj, count_offset), Immediate(1));
1787 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
1788 "Expecting 0=compressed, 1=uncompressed");
1789 __ j(kNotZero, &copy_uncompressed);
jessicahandojo4877b792016-09-08 19:49:13 -07001790 // Compute the address of the source string by adding the number of chars from
1791 // the source beginning to the value offset of a string.
1792 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_1, value_offset));
1793
1794 // Start the loop to copy String's value to Array of Char.
1795 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1796 __ Bind(&copy_loop);
1797 __ jecxz(&done);
1798 // Use EAX temporary (convert byte from ESI to word).
1799 // TODO: Use LODSB/STOSW (not supported by X86Assembler) with AH initialized to 0.
1800 __ movzxb(EAX, Address(ESI, 0));
1801 __ movw(Address(EDI, 0), EAX);
1802 __ leal(EDI, Address(EDI, char_size));
1803 __ leal(ESI, Address(ESI, c_char_size));
1804 // TODO: Add support for LOOP to X86Assembler.
1805 __ subl(ECX, Immediate(1));
1806 __ jmp(&copy_loop);
1807 __ Bind(&copy_uncompressed);
1808 }
1809
1810 // Do the copy for uncompressed string.
1811 // Compute the address of the destination buffer.
1812 __ leal(EDI, Address(dst, dstBegin, ScaleFactor::TIMES_2, data_offset));
1813 __ leal(ESI, CodeGeneratorX86::ArrayAddress(obj, srcBegin, TIMES_2, value_offset));
Mark Mendell8f8926a2015-08-17 11:39:06 -04001814 __ rep_movsw();
1815
jessicahandojo4877b792016-09-08 19:49:13 -07001816 __ Bind(&done);
1817 if (mirror::kUseStringCompression) {
1818 // Restore EAX.
1819 __ popl(EAX);
1820 __ cfi().AdjustCFAOffset(-stack_adjust);
1821 }
1822 // Restore ECX.
Mark Mendell8f8926a2015-08-17 11:39:06 -04001823 __ popl(ECX);
1824 __ cfi().AdjustCFAOffset(-stack_adjust);
1825}
1826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001827static void GenPeek(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001828 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1829 Location out_loc = locations->Out();
1830 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1831 // to avoid a SIGBUS.
1832 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001833 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001834 __ movsxb(out_loc.AsRegister<Register>(), Address(address, 0));
1835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001837 __ movsxw(out_loc.AsRegister<Register>(), Address(address, 0));
1838 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001839 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001840 __ movl(out_loc.AsRegister<Register>(), Address(address, 0));
1841 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001842 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001843 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(address, 0));
1844 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(address, 4));
1845 break;
1846 default:
1847 LOG(FATAL) << "Type not recognized for peek: " << size;
1848 UNREACHABLE();
1849 }
1850}
1851
1852void IntrinsicLocationsBuilderX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001853 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001854}
1855
1856void IntrinsicCodeGeneratorX86::VisitMemoryPeekByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001857 GenPeek(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001858}
1859
1860void IntrinsicLocationsBuilderX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001861 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001862}
1863
1864void IntrinsicCodeGeneratorX86::VisitMemoryPeekIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 GenPeek(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001866}
1867
1868void IntrinsicLocationsBuilderX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001869 CreateLongToLongLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001870}
1871
1872void IntrinsicCodeGeneratorX86::VisitMemoryPeekLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001873 GenPeek(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001874}
1875
1876void IntrinsicLocationsBuilderX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001877 CreateLongToIntLocations(allocator_, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001878}
1879
1880void IntrinsicCodeGeneratorX86::VisitMemoryPeekShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001881 GenPeek(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001882}
1883
Vladimir Markoca6fff82017-10-03 14:49:14 +01001884static void CreateLongIntToVoidLocations(ArenaAllocator* allocator,
1885 DataType::Type size,
Mark Mendell09ed1a32015-03-25 08:30:06 -04001886 HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001887 LocationSummary* locations =
1888 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001889 locations->SetInAt(0, Location::RequiresRegister());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001890 HInstruction* value = invoke->InputAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001891 if (size == DataType::Type::kInt8) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001892 locations->SetInAt(1, Location::ByteRegisterOrConstant(EDX, value));
1893 } else {
1894 locations->SetInAt(1, Location::RegisterOrConstant(value));
1895 }
1896}
1897
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001898static void GenPoke(LocationSummary* locations, DataType::Type size, X86Assembler* assembler) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04001899 Register address = locations->InAt(0).AsRegisterPairLow<Register>();
1900 Location value_loc = locations->InAt(1);
1901 // x86 allows unaligned access. We do not have to check the input or use specific instructions
1902 // to avoid a SIGBUS.
1903 switch (size) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001904 case DataType::Type::kInt8:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001905 if (value_loc.IsConstant()) {
1906 __ movb(Address(address, 0),
1907 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1908 } else {
1909 __ movb(Address(address, 0), value_loc.AsRegister<ByteRegister>());
1910 }
1911 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001912 case DataType::Type::kInt16:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001913 if (value_loc.IsConstant()) {
1914 __ movw(Address(address, 0),
1915 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1916 } else {
1917 __ movw(Address(address, 0), value_loc.AsRegister<Register>());
1918 }
1919 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001920 case DataType::Type::kInt32:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001921 if (value_loc.IsConstant()) {
1922 __ movl(Address(address, 0),
1923 Immediate(value_loc.GetConstant()->AsIntConstant()->GetValue()));
1924 } else {
1925 __ movl(Address(address, 0), value_loc.AsRegister<Register>());
1926 }
1927 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001928 case DataType::Type::kInt64:
Mark Mendell09ed1a32015-03-25 08:30:06 -04001929 if (value_loc.IsConstant()) {
1930 int64_t value = value_loc.GetConstant()->AsLongConstant()->GetValue();
1931 __ movl(Address(address, 0), Immediate(Low32Bits(value)));
1932 __ movl(Address(address, 4), Immediate(High32Bits(value)));
1933 } else {
1934 __ movl(Address(address, 0), value_loc.AsRegisterPairLow<Register>());
1935 __ movl(Address(address, 4), value_loc.AsRegisterPairHigh<Register>());
1936 }
1937 break;
1938 default:
1939 LOG(FATAL) << "Type not recognized for poke: " << size;
1940 UNREACHABLE();
1941 }
1942}
1943
1944void IntrinsicLocationsBuilderX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001945 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt8, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001946}
1947
1948void IntrinsicCodeGeneratorX86::VisitMemoryPokeByte(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001949 GenPoke(invoke->GetLocations(), DataType::Type::kInt8, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001950}
1951
1952void IntrinsicLocationsBuilderX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001953 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001954}
1955
1956void IntrinsicCodeGeneratorX86::VisitMemoryPokeIntNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 GenPoke(invoke->GetLocations(), DataType::Type::kInt32, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001958}
1959
1960void IntrinsicLocationsBuilderX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001961 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001962}
1963
1964void IntrinsicCodeGeneratorX86::VisitMemoryPokeLongNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001965 GenPoke(invoke->GetLocations(), DataType::Type::kInt64, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001966}
1967
1968void IntrinsicLocationsBuilderX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001969 CreateLongIntToVoidLocations(allocator_, DataType::Type::kInt16, invoke);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001970}
1971
1972void IntrinsicCodeGeneratorX86::VisitMemoryPokeShortNative(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 GenPoke(invoke->GetLocations(), DataType::Type::kInt16, GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04001974}
1975
1976void IntrinsicLocationsBuilderX86::VisitThreadCurrentThread(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001977 LocationSummary* locations =
1978 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001979 locations->SetOut(Location::RequiresRegister());
1980}
1981
1982void IntrinsicCodeGeneratorX86::VisitThreadCurrentThread(HInvoke* invoke) {
1983 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07001984 GetAssembler()->fs()->movl(out, Address::Absolute(Thread::PeerOffset<kX86PointerSize>()));
Mark Mendell09ed1a32015-03-25 08:30:06 -04001985}
1986
Roland Levillain0d5a2812015-11-13 10:07:31 +00001987static void GenUnsafeGet(HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 DataType::Type type,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001989 bool is_volatile,
1990 CodeGeneratorX86* codegen) {
1991 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
1992 LocationSummary* locations = invoke->GetLocations();
1993 Location base_loc = locations->InAt(1);
1994 Register base = base_loc.AsRegister<Register>();
1995 Location offset_loc = locations->InAt(2);
1996 Register offset = offset_loc.AsRegisterPairLow<Register>();
1997 Location output_loc = locations->Out();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001998
1999 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 case DataType::Type::kInt32: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002001 Register output = output_loc.AsRegister<Register>();
2002 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
Roland Levillain7c1559a2015-12-15 10:55:36 +00002003 break;
2004 }
2005
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002006 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002007 Register output = output_loc.AsRegister<Register>();
2008 if (kEmitCompilerReadBarrier) {
2009 if (kUseBakerReadBarrier) {
Sang, Chunlei0fcd2b82016-04-05 17:12:59 +08002010 Address src(base, offset, ScaleFactor::TIMES_1, 0);
2011 codegen->GenerateReferenceLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00002012 invoke, output_loc, base, src, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00002013 } else {
2014 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2015 codegen->GenerateReadBarrierSlow(
2016 invoke, output_loc, output_loc, base_loc, 0U, offset_loc);
2017 }
2018 } else {
2019 __ movl(output, Address(base, offset, ScaleFactor::TIMES_1, 0));
2020 __ MaybeUnpoisonHeapReference(output);
Roland Levillain4d027112015-07-01 15:41:14 +01002021 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002022 break;
Roland Levillain4d027112015-07-01 15:41:14 +01002023 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002024
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 case DataType::Type::kInt64: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 Register output_lo = output_loc.AsRegisterPairLow<Register>();
2027 Register output_hi = output_loc.AsRegisterPairHigh<Register>();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002028 if (is_volatile) {
2029 // Need to use a XMM to read atomically.
2030 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2031 __ movsd(temp, Address(base, offset, ScaleFactor::TIMES_1, 0));
2032 __ movd(output_lo, temp);
2033 __ psrlq(temp, Immediate(32));
2034 __ movd(output_hi, temp);
2035 } else {
2036 __ movl(output_lo, Address(base, offset, ScaleFactor::TIMES_1, 0));
2037 __ movl(output_hi, Address(base, offset, ScaleFactor::TIMES_1, 4));
2038 }
2039 }
2040 break;
2041
2042 default:
2043 LOG(FATAL) << "Unsupported op size " << type;
2044 UNREACHABLE();
2045 }
2046}
2047
Vladimir Markoca6fff82017-10-03 14:49:14 +01002048static void CreateIntIntIntToIntLocations(ArenaAllocator* allocator,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002049 HInvoke* invoke,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002050 DataType::Type type,
Roland Levillain7c1559a2015-12-15 10:55:36 +00002051 bool is_volatile) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002052 bool can_call = kEmitCompilerReadBarrier &&
2053 (invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObject ||
2054 invoke->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002055 LocationSummary* locations =
2056 new (allocator) LocationSummary(invoke,
2057 can_call
2058 ? LocationSummary::kCallOnSlowPath
2059 : LocationSummary::kNoCall,
2060 kIntrinsified);
Vladimir Marko70e97462016-08-09 11:04:26 +01002061 if (can_call && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002062 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01002063 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002064 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2065 locations->SetInAt(1, Location::RequiresRegister());
2066 locations->SetInAt(2, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002067 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002068 if (is_volatile) {
2069 // Need to use XMM to read volatile.
2070 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain3d312422016-06-23 13:53:42 +01002071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002072 } else {
2073 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2074 }
2075 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01002076 locations->SetOut(Location::RequiresRegister(),
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002077 (can_call ? Location::kOutputOverlap : Location::kNoOutputOverlap));
Mark Mendell09ed1a32015-03-25 08:30:06 -04002078 }
2079}
2080
2081void IntrinsicLocationsBuilderX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002082 CreateIntIntIntToIntLocations(
2083 allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002084}
2085void IntrinsicLocationsBuilderX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002086 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt32, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002087}
2088void IntrinsicLocationsBuilderX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002089 CreateIntIntIntToIntLocations(
2090 allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002091}
2092void IntrinsicLocationsBuilderX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002093 CreateIntIntIntToIntLocations(allocator_, invoke, DataType::Type::kInt64, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002094}
2095void IntrinsicLocationsBuilderX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 CreateIntIntIntToIntLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002097 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002098}
2099void IntrinsicLocationsBuilderX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002100 CreateIntIntIntToIntLocations(
2101 allocator_, invoke, DataType::Type::kReference, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002102}
2103
2104
2105void IntrinsicCodeGeneratorX86::VisitUnsafeGet(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002106 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002107}
2108void IntrinsicCodeGeneratorX86::VisitUnsafeGetVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 GenUnsafeGet(invoke, DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002110}
2111void IntrinsicCodeGeneratorX86::VisitUnsafeGetLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002112 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002113}
2114void IntrinsicCodeGeneratorX86::VisitUnsafeGetLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 GenUnsafeGet(invoke, DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002116}
2117void IntrinsicCodeGeneratorX86::VisitUnsafeGetObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002118 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002119}
2120void IntrinsicCodeGeneratorX86::VisitUnsafeGetObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 GenUnsafeGet(invoke, DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002122}
2123
2124
Vladimir Markoca6fff82017-10-03 14:49:14 +01002125static void CreateIntIntIntIntToVoidPlusTempsLocations(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002127 HInvoke* invoke,
2128 bool is_volatile) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002129 LocationSummary* locations =
2130 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002131 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2132 locations->SetInAt(1, Location::RequiresRegister());
2133 locations->SetInAt(2, Location::RequiresRegister());
2134 locations->SetInAt(3, Location::RequiresRegister());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 if (type == DataType::Type::kReference) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002136 // Need temp registers for card-marking.
Roland Levillain4d027112015-07-01 15:41:14 +01002137 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell09ed1a32015-03-25 08:30:06 -04002138 // Ensure the value is in a byte register.
2139 locations->AddTemp(Location::RegisterLocation(ECX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002140 } else if (type == DataType::Type::kInt64 && is_volatile) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002141 locations->AddTemp(Location::RequiresFpuRegister());
2142 locations->AddTemp(Location::RequiresFpuRegister());
2143 }
2144}
2145
2146void IntrinsicLocationsBuilderX86::VisitUnsafePut(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002147 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002148 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002149}
2150void IntrinsicLocationsBuilderX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002151 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002152 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002153}
2154void IntrinsicLocationsBuilderX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002155 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002156 allocator_, DataType::Type::kInt32, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002157}
2158void IntrinsicLocationsBuilderX86::VisitUnsafePutObject(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002159 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002160 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002161}
2162void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002163 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002164 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002165}
2166void IntrinsicLocationsBuilderX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002167 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002168 allocator_, DataType::Type::kReference, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002169}
2170void IntrinsicLocationsBuilderX86::VisitUnsafePutLong(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002171 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002172 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002173}
2174void IntrinsicLocationsBuilderX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002175 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002176 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ false);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002177}
2178void IntrinsicLocationsBuilderX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Roland Levillainbf84a3d2015-12-04 14:33:02 +00002179 CreateIntIntIntIntToVoidPlusTempsLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002180 allocator_, DataType::Type::kInt64, invoke, /* is_volatile */ true);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002181}
2182
2183// We don't care for ordered: it requires an AnyStore barrier, which is already given by the x86
2184// memory model.
2185static void GenUnsafePut(LocationSummary* locations,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002186 DataType::Type type,
Mark Mendell09ed1a32015-03-25 08:30:06 -04002187 bool is_volatile,
2188 CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002189 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell09ed1a32015-03-25 08:30:06 -04002190 Register base = locations->InAt(1).AsRegister<Register>();
2191 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2192 Location value_loc = locations->InAt(3);
2193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002194 if (type == DataType::Type::kInt64) {
Mark Mendell09ed1a32015-03-25 08:30:06 -04002195 Register value_lo = value_loc.AsRegisterPairLow<Register>();
2196 Register value_hi = value_loc.AsRegisterPairHigh<Register>();
2197 if (is_volatile) {
2198 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2199 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
2200 __ movd(temp1, value_lo);
2201 __ movd(temp2, value_hi);
2202 __ punpckldq(temp1, temp2);
2203 __ movsd(Address(base, offset, ScaleFactor::TIMES_1, 0), temp1);
2204 } else {
2205 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_lo);
2206 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 4), value_hi);
2207 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 } else if (kPoisonHeapReferences && type == DataType::Type::kReference) {
Roland Levillain4d027112015-07-01 15:41:14 +01002209 Register temp = locations->GetTemp(0).AsRegister<Register>();
2210 __ movl(temp, value_loc.AsRegister<Register>());
2211 __ PoisonHeapReference(temp);
2212 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), temp);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002213 } else {
2214 __ movl(Address(base, offset, ScaleFactor::TIMES_1, 0), value_loc.AsRegister<Register>());
2215 }
2216
2217 if (is_volatile) {
Mark P Mendell17077d82015-12-16 19:15:59 +00002218 codegen->MemoryFence();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002219 }
2220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002221 if (type == DataType::Type::kReference) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002222 bool value_can_be_null = true; // TODO: Worth finding out this information?
Mark Mendell09ed1a32015-03-25 08:30:06 -04002223 codegen->MarkGCCard(locations->GetTemp(0).AsRegister<Register>(),
2224 locations->GetTemp(1).AsRegister<Register>(),
2225 base,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01002226 value_loc.AsRegister<Register>(),
2227 value_can_be_null);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002228 }
2229}
2230
2231void IntrinsicCodeGeneratorX86::VisitUnsafePut(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002232 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002233}
2234void IntrinsicCodeGeneratorX86::VisitUnsafePutOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002235 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002236}
2237void IntrinsicCodeGeneratorX86::VisitUnsafePutVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002238 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt32, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002239}
2240void IntrinsicCodeGeneratorX86::VisitUnsafePutObject(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002241 GenUnsafePut(
2242 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002243}
2244void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 GenUnsafePut(
2246 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002247}
2248void IntrinsicCodeGeneratorX86::VisitUnsafePutObjectVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002249 GenUnsafePut(
2250 invoke->GetLocations(), DataType::Type::kReference, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002251}
2252void IntrinsicCodeGeneratorX86::VisitUnsafePutLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002253 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002254}
2255void IntrinsicCodeGeneratorX86::VisitUnsafePutLongOrdered(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002256 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ false, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002257}
2258void IntrinsicCodeGeneratorX86::VisitUnsafePutLongVolatile(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 GenUnsafePut(invoke->GetLocations(), DataType::Type::kInt64, /* is_volatile */ true, codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002260}
2261
Vladimir Markoca6fff82017-10-03 14:49:14 +01002262static void CreateIntIntIntIntIntToInt(ArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002263 DataType::Type type,
Mark Mendell58d25fd2015-04-03 14:52:31 -04002264 HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002265 bool can_call = kEmitCompilerReadBarrier &&
2266 kUseBakerReadBarrier &&
2267 (invoke->GetIntrinsic() == Intrinsics::kUnsafeCASObject);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002268 LocationSummary* locations =
2269 new (allocator) LocationSummary(invoke,
2270 can_call
2271 ? LocationSummary::kCallOnSlowPath
2272 : LocationSummary::kNoCall,
2273 kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002274 locations->SetInAt(0, Location::NoLocation()); // Unused receiver.
2275 locations->SetInAt(1, Location::RequiresRegister());
2276 // Offset is a long, but in 32 bit mode, we only need the low word.
2277 // Can we update the invoke here to remove a TypeConvert to Long?
2278 locations->SetInAt(2, Location::RequiresRegister());
2279 // Expected value must be in EAX or EDX:EAX.
2280 // For long, new value must be in ECX:EBX.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002281 if (type == DataType::Type::kInt64) {
Mark Mendell58d25fd2015-04-03 14:52:31 -04002282 locations->SetInAt(3, Location::RegisterPairLocation(EAX, EDX));
2283 locations->SetInAt(4, Location::RegisterPairLocation(EBX, ECX));
2284 } else {
2285 locations->SetInAt(3, Location::RegisterLocation(EAX));
2286 locations->SetInAt(4, Location::RequiresRegister());
2287 }
2288
2289 // Force a byte register for the output.
2290 locations->SetOut(Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002292 // Need temporary registers for card-marking, and possibly for
2293 // (Baker) read barrier.
Roland Levillainb488b782015-10-22 11:38:49 +01002294 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002295 // Need a byte register for marking.
2296 locations->AddTemp(Location::RegisterLocation(ECX));
2297 }
2298}
2299
2300void IntrinsicLocationsBuilderX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002301 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt32, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002302}
2303
2304void IntrinsicLocationsBuilderX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002305 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kInt64, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002306}
2307
2308void IntrinsicLocationsBuilderX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002309 // The only read barrier implementation supporting the
2310 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2311 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Roland Levillain391b8662015-12-18 11:43:38 +00002312 return;
2313 }
2314
Vladimir Markoca6fff82017-10-03 14:49:14 +01002315 CreateIntIntIntIntIntToInt(allocator_, DataType::Type::kReference, invoke);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002316}
2317
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002318static void GenCAS(DataType::Type type, HInvoke* invoke, CodeGeneratorX86* codegen) {
Roland Levillainb488b782015-10-22 11:38:49 +01002319 X86Assembler* assembler = down_cast<X86Assembler*>(codegen->GetAssembler());
Mark Mendell58d25fd2015-04-03 14:52:31 -04002320 LocationSummary* locations = invoke->GetLocations();
2321
2322 Register base = locations->InAt(1).AsRegister<Register>();
2323 Register offset = locations->InAt(2).AsRegisterPairLow<Register>();
2324 Location out = locations->Out();
2325 DCHECK_EQ(out.AsRegister<Register>(), EAX);
2326
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002327 // The address of the field within the holding object.
2328 Address field_addr(base, offset, ScaleFactor::TIMES_1, 0);
2329
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002330 if (type == DataType::Type::kReference) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002331 // The only read barrier implementation supporting the
2332 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2333 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
2334
2335 Location temp1_loc = locations->GetTemp(0);
2336 Register temp1 = temp1_loc.AsRegister<Register>();
2337 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2338
Roland Levillain4d027112015-07-01 15:41:14 +01002339 Register expected = locations->InAt(3).AsRegister<Register>();
Roland Levillainb488b782015-10-22 11:38:49 +01002340 // Ensure `expected` is in EAX (required by the CMPXCHG instruction).
Roland Levillain4d027112015-07-01 15:41:14 +01002341 DCHECK_EQ(expected, EAX);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002342 Register value = locations->InAt(4).AsRegister<Register>();
Roland Levillain4d027112015-07-01 15:41:14 +01002343
Roland Levillainb488b782015-10-22 11:38:49 +01002344 // Mark card for object assuming new value is stored.
2345 bool value_can_be_null = true; // TODO: Worth finding out this information?
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002346 codegen->MarkGCCard(temp1, temp2, base, value, value_can_be_null);
2347
2348 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2349 // Need to make sure the reference stored in the field is a to-space
2350 // one before attempting the CAS or the CAS could fail incorrectly.
2351 codegen->GenerateReferenceLoadWithBakerReadBarrier(
2352 invoke,
2353 temp1_loc, // Unused, used only as a "temporary" within the read barrier.
2354 base,
2355 field_addr,
2356 /* needs_null_check */ false,
2357 /* always_update_field */ true,
2358 &temp2);
2359 }
Roland Levillainb488b782015-10-22 11:38:49 +01002360
2361 bool base_equals_value = (base == value);
2362 if (kPoisonHeapReferences) {
2363 if (base_equals_value) {
2364 // If `base` and `value` are the same register location, move
2365 // `value` to a temporary register. This way, poisoning
2366 // `value` won't invalidate `base`.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002367 value = temp1;
Roland Levillainb488b782015-10-22 11:38:49 +01002368 __ movl(value, base);
Roland Levillain4d027112015-07-01 15:41:14 +01002369 }
Roland Levillainb488b782015-10-22 11:38:49 +01002370
2371 // Check that the register allocator did not assign the location
2372 // of `expected` (EAX) to `value` nor to `base`, so that heap
2373 // poisoning (when enabled) works as intended below.
2374 // - If `value` were equal to `expected`, both references would
2375 // be poisoned twice, meaning they would not be poisoned at
2376 // all, as heap poisoning uses address negation.
2377 // - If `base` were equal to `expected`, poisoning `expected`
2378 // would invalidate `base`.
2379 DCHECK_NE(value, expected);
2380 DCHECK_NE(base, expected);
2381
2382 __ PoisonHeapReference(expected);
2383 __ PoisonHeapReference(value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002384 }
2385
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002386 __ LockCmpxchgl(field_addr, value);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002387
Roland Levillain0d5a2812015-11-13 10:07:31 +00002388 // LOCK CMPXCHG has full barrier semantics, and we don't need
Roland Levillainb488b782015-10-22 11:38:49 +01002389 // scheduling barriers at this time.
Mark Mendell58d25fd2015-04-03 14:52:31 -04002390
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002391 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002392 __ setb(kZero, out.AsRegister<Register>());
2393 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002394
Roland Levillain391b8662015-12-18 11:43:38 +00002395 // If heap poisoning is enabled, we need to unpoison the values
2396 // that were poisoned earlier.
Roland Levillainb488b782015-10-22 11:38:49 +01002397 if (kPoisonHeapReferences) {
2398 if (base_equals_value) {
2399 // `value` has been moved to a temporary register, no need to
2400 // unpoison it.
2401 } else {
2402 // Ensure `value` is different from `out`, so that unpoisoning
2403 // the former does not invalidate the latter.
2404 DCHECK_NE(value, out.AsRegister<Register>());
2405 __ UnpoisonHeapReference(value);
2406 }
2407 // Do not unpoison the reference contained in register
2408 // `expected`, as it is the same as register `out` (EAX).
2409 }
2410 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 if (type == DataType::Type::kInt32) {
Roland Levillainb488b782015-10-22 11:38:49 +01002412 // Ensure the expected value is in EAX (required by the CMPXCHG
2413 // instruction).
2414 DCHECK_EQ(locations->InAt(3).AsRegister<Register>(), EAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002415 __ LockCmpxchgl(field_addr, locations->InAt(4).AsRegister<Register>());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002416 } else if (type == DataType::Type::kInt64) {
Roland Levillainb488b782015-10-22 11:38:49 +01002417 // Ensure the expected value is in EAX:EDX and that the new
2418 // value is in EBX:ECX (required by the CMPXCHG8B instruction).
2419 DCHECK_EQ(locations->InAt(3).AsRegisterPairLow<Register>(), EAX);
2420 DCHECK_EQ(locations->InAt(3).AsRegisterPairHigh<Register>(), EDX);
2421 DCHECK_EQ(locations->InAt(4).AsRegisterPairLow<Register>(), EBX);
2422 DCHECK_EQ(locations->InAt(4).AsRegisterPairHigh<Register>(), ECX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002423 __ LockCmpxchg8b(field_addr);
Roland Levillainb488b782015-10-22 11:38:49 +01002424 } else {
2425 LOG(FATAL) << "Unexpected CAS type " << type;
2426 }
2427
Roland Levillain0d5a2812015-11-13 10:07:31 +00002428 // LOCK CMPXCHG/LOCK CMPXCHG8B have full barrier semantics, and we
2429 // don't need scheduling barriers at this time.
Roland Levillainb488b782015-10-22 11:38:49 +01002430
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002431 // Convert ZF into the Boolean result.
Roland Levillainb488b782015-10-22 11:38:49 +01002432 __ setb(kZero, out.AsRegister<Register>());
2433 __ movzxb(out.AsRegister<Register>(), out.AsRegister<ByteRegister>());
Roland Levillain4d027112015-07-01 15:41:14 +01002434 }
Mark Mendell58d25fd2015-04-03 14:52:31 -04002435}
2436
2437void IntrinsicCodeGeneratorX86::VisitUnsafeCASInt(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 GenCAS(DataType::Type::kInt32, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002439}
2440
2441void IntrinsicCodeGeneratorX86::VisitUnsafeCASLong(HInvoke* invoke) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002442 GenCAS(DataType::Type::kInt64, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002443}
2444
2445void IntrinsicCodeGeneratorX86::VisitUnsafeCASObject(HInvoke* invoke) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01002446 // The only read barrier implementation supporting the
2447 // UnsafeCASObject intrinsic is the Baker-style read barriers.
2448 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Roland Levillain3d312422016-06-23 13:53:42 +01002449
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002450 GenCAS(DataType::Type::kReference, invoke, codegen_);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002451}
2452
2453void IntrinsicLocationsBuilderX86::VisitIntegerReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002454 LocationSummary* locations =
2455 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetOut(Location::SameAsFirstInput());
2458 locations->AddTemp(Location::RequiresRegister());
2459}
2460
2461static void SwapBits(Register reg, Register temp, int32_t shift, int32_t mask,
2462 X86Assembler* assembler) {
2463 Immediate imm_shift(shift);
2464 Immediate imm_mask(mask);
2465 __ movl(temp, reg);
2466 __ shrl(reg, imm_shift);
2467 __ andl(temp, imm_mask);
2468 __ andl(reg, imm_mask);
2469 __ shll(temp, imm_shift);
2470 __ orl(reg, temp);
2471}
2472
2473void IntrinsicCodeGeneratorX86::VisitIntegerReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002474 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002475 LocationSummary* locations = invoke->GetLocations();
2476
2477 Register reg = locations->InAt(0).AsRegister<Register>();
2478 Register temp = locations->GetTemp(0).AsRegister<Register>();
2479
2480 /*
2481 * Use one bswap instruction to reverse byte order first and then use 3 rounds of
2482 * swapping bits to reverse bits in a number x. Using bswap to save instructions
2483 * compared to generic luni implementation which has 5 rounds of swapping bits.
2484 * x = bswap x
2485 * x = (x & 0x55555555) << 1 | (x >> 1) & 0x55555555;
2486 * x = (x & 0x33333333) << 2 | (x >> 2) & 0x33333333;
2487 * x = (x & 0x0F0F0F0F) << 4 | (x >> 4) & 0x0F0F0F0F;
2488 */
2489 __ bswapl(reg);
2490 SwapBits(reg, temp, 1, 0x55555555, assembler);
2491 SwapBits(reg, temp, 2, 0x33333333, assembler);
2492 SwapBits(reg, temp, 4, 0x0f0f0f0f, assembler);
2493}
2494
2495void IntrinsicLocationsBuilderX86::VisitLongReverse(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002496 LocationSummary* locations =
2497 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell58d25fd2015-04-03 14:52:31 -04002498 locations->SetInAt(0, Location::RequiresRegister());
2499 locations->SetOut(Location::SameAsFirstInput());
2500 locations->AddTemp(Location::RequiresRegister());
2501}
2502
2503void IntrinsicCodeGeneratorX86::VisitLongReverse(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002504 X86Assembler* assembler = GetAssembler();
Mark Mendell58d25fd2015-04-03 14:52:31 -04002505 LocationSummary* locations = invoke->GetLocations();
2506
2507 Register reg_low = locations->InAt(0).AsRegisterPairLow<Register>();
2508 Register reg_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2509 Register temp = locations->GetTemp(0).AsRegister<Register>();
2510
2511 // We want to swap high/low, then bswap each one, and then do the same
2512 // as a 32 bit reverse.
2513 // Exchange high and low.
2514 __ movl(temp, reg_low);
2515 __ movl(reg_low, reg_high);
2516 __ movl(reg_high, temp);
2517
2518 // bit-reverse low
2519 __ bswapl(reg_low);
2520 SwapBits(reg_low, temp, 1, 0x55555555, assembler);
2521 SwapBits(reg_low, temp, 2, 0x33333333, assembler);
2522 SwapBits(reg_low, temp, 4, 0x0f0f0f0f, assembler);
2523
2524 // bit-reverse high
2525 __ bswapl(reg_high);
2526 SwapBits(reg_high, temp, 1, 0x55555555, assembler);
2527 SwapBits(reg_high, temp, 2, 0x33333333, assembler);
2528 SwapBits(reg_high, temp, 4, 0x0f0f0f0f, assembler);
2529}
2530
Aart Bikc39dac12016-01-21 08:59:48 -08002531static void CreateBitCountLocations(
Vladimir Markoca6fff82017-10-03 14:49:14 +01002532 ArenaAllocator* allocator, CodeGeneratorX86* codegen, HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002533 if (!codegen->GetInstructionSetFeatures().HasPopCnt()) {
2534 // Do nothing if there is no popcnt support. This results in generating
2535 // a call for the intrinsic rather than direct code.
2536 return;
2537 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002538 LocationSummary* locations =
2539 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Aart Bikc39dac12016-01-21 08:59:48 -08002540 if (is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002541 locations->AddTemp(Location::RequiresRegister());
Aart Bikc39dac12016-01-21 08:59:48 -08002542 }
Aart Bik2a946072016-01-21 12:49:00 -08002543 locations->SetInAt(0, Location::Any());
Aart Bikc39dac12016-01-21 08:59:48 -08002544 locations->SetOut(Location::RequiresRegister());
2545}
2546
Aart Bika19616e2016-02-01 18:57:58 -08002547static void GenBitCount(X86Assembler* assembler,
2548 CodeGeneratorX86* codegen,
2549 HInvoke* invoke, bool is_long) {
Aart Bikc39dac12016-01-21 08:59:48 -08002550 LocationSummary* locations = invoke->GetLocations();
2551 Location src = locations->InAt(0);
2552 Register out = locations->Out().AsRegister<Register>();
2553
2554 if (invoke->InputAt(0)->IsConstant()) {
2555 // Evaluate this at compile time.
2556 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
Roland Levillainfa3912e2016-04-01 18:21:55 +01002557 int32_t result = is_long
Aart Bikc39dac12016-01-21 08:59:48 -08002558 ? POPCOUNT(static_cast<uint64_t>(value))
2559 : POPCOUNT(static_cast<uint32_t>(value));
Roland Levillainfa3912e2016-04-01 18:21:55 +01002560 codegen->Load32BitValue(out, result);
Aart Bikc39dac12016-01-21 08:59:48 -08002561 return;
2562 }
2563
2564 // Handle the non-constant cases.
2565 if (!is_long) {
2566 if (src.IsRegister()) {
2567 __ popcntl(out, src.AsRegister<Register>());
2568 } else {
2569 DCHECK(src.IsStackSlot());
2570 __ popcntl(out, Address(ESP, src.GetStackIndex()));
2571 }
Aart Bik2a946072016-01-21 12:49:00 -08002572 } else {
2573 // The 64-bit case needs to worry about two parts.
2574 Register temp = locations->GetTemp(0).AsRegister<Register>();
2575 if (src.IsRegisterPair()) {
2576 __ popcntl(temp, src.AsRegisterPairLow<Register>());
2577 __ popcntl(out, src.AsRegisterPairHigh<Register>());
2578 } else {
2579 DCHECK(src.IsDoubleStackSlot());
2580 __ popcntl(temp, Address(ESP, src.GetStackIndex()));
2581 __ popcntl(out, Address(ESP, src.GetHighStackIndex(kX86WordSize)));
2582 }
2583 __ addl(out, temp);
Aart Bikc39dac12016-01-21 08:59:48 -08002584 }
Aart Bikc39dac12016-01-21 08:59:48 -08002585}
2586
2587void IntrinsicLocationsBuilderX86::VisitIntegerBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002588 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002589}
2590
2591void IntrinsicCodeGeneratorX86::VisitIntegerBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002592 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ false);
Aart Bikc39dac12016-01-21 08:59:48 -08002593}
2594
2595void IntrinsicLocationsBuilderX86::VisitLongBitCount(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002596 CreateBitCountLocations(allocator_, codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002597}
2598
2599void IntrinsicCodeGeneratorX86::VisitLongBitCount(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002600 GenBitCount(GetAssembler(), codegen_, invoke, /* is_long */ true);
Aart Bikc39dac12016-01-21 08:59:48 -08002601}
2602
Vladimir Markoca6fff82017-10-03 14:49:14 +01002603static void CreateLeadingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2604 LocationSummary* locations =
2605 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendelld5897672015-08-12 21:16:41 -04002606 if (is_long) {
2607 locations->SetInAt(0, Location::RequiresRegister());
2608 } else {
2609 locations->SetInAt(0, Location::Any());
2610 }
2611 locations->SetOut(Location::RequiresRegister());
2612}
2613
Aart Bika19616e2016-02-01 18:57:58 -08002614static void GenLeadingZeros(X86Assembler* assembler,
2615 CodeGeneratorX86* codegen,
2616 HInvoke* invoke, bool is_long) {
Mark Mendelld5897672015-08-12 21:16:41 -04002617 LocationSummary* locations = invoke->GetLocations();
2618 Location src = locations->InAt(0);
2619 Register out = locations->Out().AsRegister<Register>();
2620
2621 if (invoke->InputAt(0)->IsConstant()) {
2622 // Evaluate this at compile time.
2623 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2624 if (value == 0) {
2625 value = is_long ? 64 : 32;
2626 } else {
2627 value = is_long ? CLZ(static_cast<uint64_t>(value)) : CLZ(static_cast<uint32_t>(value));
2628 }
Aart Bika19616e2016-02-01 18:57:58 -08002629 codegen->Load32BitValue(out, value);
Mark Mendelld5897672015-08-12 21:16:41 -04002630 return;
2631 }
2632
2633 // Handle the non-constant cases.
2634 if (!is_long) {
2635 if (src.IsRegister()) {
2636 __ bsrl(out, src.AsRegister<Register>());
2637 } else {
2638 DCHECK(src.IsStackSlot());
2639 __ bsrl(out, Address(ESP, src.GetStackIndex()));
2640 }
2641
2642 // BSR sets ZF if the input was zero, and the output is undefined.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002643 NearLabel all_zeroes, done;
Mark Mendelld5897672015-08-12 21:16:41 -04002644 __ j(kEqual, &all_zeroes);
2645
2646 // Correct the result from BSR to get the final CLZ result.
2647 __ xorl(out, Immediate(31));
2648 __ jmp(&done);
2649
2650 // Fix the zero case with the expected result.
2651 __ Bind(&all_zeroes);
2652 __ movl(out, Immediate(32));
2653
2654 __ Bind(&done);
2655 return;
2656 }
2657
2658 // 64 bit case needs to worry about both parts of the register.
2659 DCHECK(src.IsRegisterPair());
2660 Register src_lo = src.AsRegisterPairLow<Register>();
2661 Register src_hi = src.AsRegisterPairHigh<Register>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002662 NearLabel handle_low, done, all_zeroes;
Mark Mendelld5897672015-08-12 21:16:41 -04002663
2664 // Is the high word zero?
2665 __ testl(src_hi, src_hi);
2666 __ j(kEqual, &handle_low);
2667
2668 // High word is not zero. We know that the BSR result is defined in this case.
2669 __ bsrl(out, src_hi);
2670
2671 // Correct the result from BSR to get the final CLZ result.
2672 __ xorl(out, Immediate(31));
2673 __ jmp(&done);
2674
2675 // High word was zero. We have to compute the low word count and add 32.
2676 __ Bind(&handle_low);
2677 __ bsrl(out, src_lo);
2678 __ j(kEqual, &all_zeroes);
2679
2680 // We had a valid result. Use an XOR to both correct the result and add 32.
2681 __ xorl(out, Immediate(63));
2682 __ jmp(&done);
2683
2684 // All zero case.
2685 __ Bind(&all_zeroes);
2686 __ movl(out, Immediate(64));
2687
2688 __ Bind(&done);
2689}
2690
2691void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002692 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002693}
2694
2695void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002696 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendelld5897672015-08-12 21:16:41 -04002697}
2698
2699void IntrinsicLocationsBuilderX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002700 CreateLeadingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002701}
2702
2703void IntrinsicCodeGeneratorX86::VisitLongNumberOfLeadingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002704 GenLeadingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendelld5897672015-08-12 21:16:41 -04002705}
2706
Vladimir Markoca6fff82017-10-03 14:49:14 +01002707static void CreateTrailingZeroLocations(ArenaAllocator* allocator, HInvoke* invoke, bool is_long) {
2708 LocationSummary* locations =
2709 new (allocator) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Mark Mendell2d554792015-09-15 21:45:18 -04002710 if (is_long) {
2711 locations->SetInAt(0, Location::RequiresRegister());
2712 } else {
2713 locations->SetInAt(0, Location::Any());
2714 }
2715 locations->SetOut(Location::RequiresRegister());
2716}
2717
Aart Bika19616e2016-02-01 18:57:58 -08002718static void GenTrailingZeros(X86Assembler* assembler,
2719 CodeGeneratorX86* codegen,
2720 HInvoke* invoke, bool is_long) {
Mark Mendell2d554792015-09-15 21:45:18 -04002721 LocationSummary* locations = invoke->GetLocations();
2722 Location src = locations->InAt(0);
2723 Register out = locations->Out().AsRegister<Register>();
2724
2725 if (invoke->InputAt(0)->IsConstant()) {
2726 // Evaluate this at compile time.
2727 int64_t value = Int64FromConstant(invoke->InputAt(0)->AsConstant());
2728 if (value == 0) {
2729 value = is_long ? 64 : 32;
2730 } else {
2731 value = is_long ? CTZ(static_cast<uint64_t>(value)) : CTZ(static_cast<uint32_t>(value));
2732 }
Aart Bika19616e2016-02-01 18:57:58 -08002733 codegen->Load32BitValue(out, value);
Mark Mendell2d554792015-09-15 21:45:18 -04002734 return;
2735 }
2736
2737 // Handle the non-constant cases.
2738 if (!is_long) {
2739 if (src.IsRegister()) {
2740 __ bsfl(out, src.AsRegister<Register>());
2741 } else {
2742 DCHECK(src.IsStackSlot());
2743 __ bsfl(out, Address(ESP, src.GetStackIndex()));
2744 }
2745
2746 // BSF sets ZF if the input was zero, and the output is undefined.
2747 NearLabel done;
2748 __ j(kNotEqual, &done);
2749
2750 // Fix the zero case with the expected result.
2751 __ movl(out, Immediate(32));
2752
2753 __ Bind(&done);
2754 return;
2755 }
2756
2757 // 64 bit case needs to worry about both parts of the register.
2758 DCHECK(src.IsRegisterPair());
2759 Register src_lo = src.AsRegisterPairLow<Register>();
2760 Register src_hi = src.AsRegisterPairHigh<Register>();
2761 NearLabel done, all_zeroes;
2762
2763 // If the low word is zero, then ZF will be set. If not, we have the answer.
2764 __ bsfl(out, src_lo);
2765 __ j(kNotEqual, &done);
2766
2767 // Low word was zero. We have to compute the high word count and add 32.
2768 __ bsfl(out, src_hi);
2769 __ j(kEqual, &all_zeroes);
2770
2771 // We had a valid result. Add 32 to account for the low word being zero.
2772 __ addl(out, Immediate(32));
2773 __ jmp(&done);
2774
2775 // All zero case.
2776 __ Bind(&all_zeroes);
2777 __ movl(out, Immediate(64));
2778
2779 __ Bind(&done);
2780}
2781
2782void IntrinsicLocationsBuilderX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002783 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002784}
2785
2786void IntrinsicCodeGeneratorX86::VisitIntegerNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002787 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ false);
Mark Mendell2d554792015-09-15 21:45:18 -04002788}
2789
2790void IntrinsicLocationsBuilderX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002791 CreateTrailingZeroLocations(allocator_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002792}
2793
2794void IntrinsicCodeGeneratorX86::VisitLongNumberOfTrailingZeros(HInvoke* invoke) {
Aart Bika19616e2016-02-01 18:57:58 -08002795 GenTrailingZeros(GetAssembler(), codegen_, invoke, /* is_long */ true);
Mark Mendell2d554792015-09-15 21:45:18 -04002796}
2797
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002798static bool IsSameInput(HInstruction* instruction, size_t input0, size_t input1) {
2799 return instruction->InputAt(input0) == instruction->InputAt(input1);
2800}
2801
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002802// Compute base address for the System.arraycopy intrinsic in `base`.
2803static void GenSystemArrayCopyBaseAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002805 const Register& array,
2806 const Location& pos,
2807 const Register& base) {
2808 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002809 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002810 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 DCHECK_EQ(type, DataType::Type::kReference);
2812 const int32_t element_size = DataType::Size(type);
2813 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002814 const uint32_t data_offset = mirror::Array::DataOffset(element_size).Uint32Value();
2815
2816 if (pos.IsConstant()) {
2817 int32_t constant = pos.GetConstant()->AsIntConstant()->GetValue();
2818 __ leal(base, Address(array, element_size * constant + data_offset));
2819 } else {
2820 __ leal(base, Address(array, pos.AsRegister<Register>(), scale_factor, data_offset));
2821 }
2822}
2823
2824// Compute end source address for the System.arraycopy intrinsic in `end`.
2825static void GenSystemArrayCopyEndAddress(X86Assembler* assembler,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 DataType::Type type,
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002827 const Location& copy_length,
2828 const Register& base,
2829 const Register& end) {
2830 // This routine is only used by the SystemArrayCopy intrinsic at the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002831 // moment. We can allow DataType::Type::kReference as `type` to implement
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002832 // the SystemArrayCopyChar intrinsic.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 DCHECK_EQ(type, DataType::Type::kReference);
2834 const int32_t element_size = DataType::Size(type);
2835 const ScaleFactor scale_factor = static_cast<ScaleFactor>(DataType::SizeShift(type));
Roland Levillain9cc0ea82017-03-16 11:25:59 +00002836
2837 if (copy_length.IsConstant()) {
2838 int32_t constant = copy_length.GetConstant()->AsIntConstant()->GetValue();
2839 __ leal(end, Address(base, element_size * constant));
2840 } else {
2841 __ leal(end, Address(base, copy_length.AsRegister<Register>(), scale_factor, 0));
2842 }
2843}
2844
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002845void IntrinsicLocationsBuilderX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002846 // The only read barrier implementation supporting the
2847 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2848 if (kEmitCompilerReadBarrier && !kUseBakerReadBarrier) {
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002849 return;
2850 }
2851
2852 CodeGenerator::CreateSystemArrayCopyLocationSummary(invoke);
2853 if (invoke->GetLocations() != nullptr) {
2854 // Need a byte register for marking.
2855 invoke->GetLocations()->SetTempAt(1, Location::RegisterLocation(ECX));
2856
2857 static constexpr size_t kSrc = 0;
2858 static constexpr size_t kSrcPos = 1;
2859 static constexpr size_t kDest = 2;
2860 static constexpr size_t kDestPos = 3;
2861 static constexpr size_t kLength = 4;
2862
2863 if (!invoke->InputAt(kSrcPos)->IsIntConstant() &&
2864 !invoke->InputAt(kDestPos)->IsIntConstant() &&
2865 !invoke->InputAt(kLength)->IsIntConstant()) {
2866 if (!IsSameInput(invoke, kSrcPos, kDestPos) &&
2867 !IsSameInput(invoke, kSrcPos, kLength) &&
2868 !IsSameInput(invoke, kDestPos, kLength) &&
2869 !IsSameInput(invoke, kSrc, kDest)) {
2870 // Not enough registers, make the length also take a stack slot.
2871 invoke->GetLocations()->SetInAt(kLength, Location::Any());
2872 }
2873 }
2874 }
2875}
2876
2877void IntrinsicCodeGeneratorX86::VisitSystemArrayCopy(HInvoke* invoke) {
Roland Levillain0b671c02016-08-19 12:02:34 +01002878 // The only read barrier implementation supporting the
2879 // SystemArrayCopy intrinsic is the Baker-style read barriers.
2880 DCHECK(!kEmitCompilerReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002881
2882 X86Assembler* assembler = GetAssembler();
2883 LocationSummary* locations = invoke->GetLocations();
2884
2885 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2886 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2887 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2888 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Roland Levillain0b671c02016-08-19 12:02:34 +01002889 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002890
2891 Register src = locations->InAt(0).AsRegister<Register>();
2892 Location src_pos = locations->InAt(1);
2893 Register dest = locations->InAt(2).AsRegister<Register>();
2894 Location dest_pos = locations->InAt(3);
Roland Levillain0b671c02016-08-19 12:02:34 +01002895 Location length_arg = locations->InAt(4);
2896 Location length = length_arg;
2897 Location temp1_loc = locations->GetTemp(0);
2898 Register temp1 = temp1_loc.AsRegister<Register>();
2899 Location temp2_loc = locations->GetTemp(1);
2900 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002901
Vladimir Marko174b2e22017-10-12 13:34:49 +01002902 SlowPathCode* intrinsic_slow_path =
2903 new (codegen_->GetScopedAllocator()) IntrinsicSlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01002904 codegen_->AddSlowPath(intrinsic_slow_path);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002905
2906 NearLabel conditions_on_positions_validated;
2907 SystemArrayCopyOptimizations optimizations(invoke);
2908
2909 // If source and destination are the same, we go to slow path if we need to do
2910 // forward copying.
2911 if (src_pos.IsConstant()) {
2912 int32_t src_pos_constant = src_pos.GetConstant()->AsIntConstant()->GetValue();
2913 if (dest_pos.IsConstant()) {
2914 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2915 if (optimizations.GetDestinationIsSource()) {
2916 // Checked when building locations.
2917 DCHECK_GE(src_pos_constant, dest_pos_constant);
2918 } else if (src_pos_constant < dest_pos_constant) {
2919 __ cmpl(src, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002920 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002921 }
2922 } else {
2923 if (!optimizations.GetDestinationIsSource()) {
2924 __ cmpl(src, dest);
2925 __ j(kNotEqual, &conditions_on_positions_validated);
2926 }
2927 __ cmpl(dest_pos.AsRegister<Register>(), Immediate(src_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002928 __ j(kGreater, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002929 }
2930 } else {
2931 if (!optimizations.GetDestinationIsSource()) {
2932 __ cmpl(src, dest);
2933 __ j(kNotEqual, &conditions_on_positions_validated);
2934 }
2935 if (dest_pos.IsConstant()) {
2936 int32_t dest_pos_constant = dest_pos.GetConstant()->AsIntConstant()->GetValue();
2937 __ cmpl(src_pos.AsRegister<Register>(), Immediate(dest_pos_constant));
Roland Levillain0b671c02016-08-19 12:02:34 +01002938 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002939 } else {
2940 __ cmpl(src_pos.AsRegister<Register>(), dest_pos.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002941 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002942 }
2943 }
2944
2945 __ Bind(&conditions_on_positions_validated);
2946
2947 if (!optimizations.GetSourceIsNotNull()) {
2948 // Bail out if the source is null.
2949 __ testl(src, src);
Roland Levillain0b671c02016-08-19 12:02:34 +01002950 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002951 }
2952
2953 if (!optimizations.GetDestinationIsNotNull() && !optimizations.GetDestinationIsSource()) {
2954 // Bail out if the destination is null.
2955 __ testl(dest, dest);
Roland Levillain0b671c02016-08-19 12:02:34 +01002956 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002957 }
2958
Roland Levillain0b671c02016-08-19 12:02:34 +01002959 Location temp3_loc = locations->GetTemp(2);
2960 Register temp3 = temp3_loc.AsRegister<Register>();
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002961 if (length.IsStackSlot()) {
2962 __ movl(temp3, Address(ESP, length.GetStackIndex()));
2963 length = Location::RegisterLocation(temp3);
2964 }
2965
2966 // If the length is negative, bail out.
2967 // We have already checked in the LocationsBuilder for the constant case.
2968 if (!length.IsConstant() &&
2969 !optimizations.GetCountIsSourceLength() &&
2970 !optimizations.GetCountIsDestinationLength()) {
2971 __ testl(length.AsRegister<Register>(), length.AsRegister<Register>());
Roland Levillain0b671c02016-08-19 12:02:34 +01002972 __ j(kLess, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002973 }
2974
2975 // Validity checks: source.
2976 CheckPosition(assembler,
2977 src_pos,
2978 src,
2979 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002980 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002981 temp1,
2982 optimizations.GetCountIsSourceLength());
2983
2984 // Validity checks: dest.
2985 CheckPosition(assembler,
2986 dest_pos,
2987 dest,
2988 length,
Roland Levillain0b671c02016-08-19 12:02:34 +01002989 intrinsic_slow_path,
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002990 temp1,
2991 optimizations.GetCountIsDestinationLength());
2992
2993 if (!optimizations.GetDoesNotNeedTypeCheck()) {
2994 // Check whether all elements of the source array are assignable to the component
2995 // type of the destination array. We do two checks: the classes are the same,
2996 // or the destination is Object[]. If none of these checks succeed, we go to the
2997 // slow path.
Roland Levillain0b671c02016-08-19 12:02:34 +01002998
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01002999 if (!optimizations.GetSourceIsNonPrimitiveArray()) {
Roland Levillain0b671c02016-08-19 12:02:34 +01003000 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3001 // /* HeapReference<Class> */ temp1 = src->klass_
3002 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003003 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003004 // Bail out if the source is not a non primitive array.
3005 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3006 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003007 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003008 __ testl(temp1, temp1);
3009 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3010 // If heap poisoning is enabled, `temp1` has been unpoisoned
3011 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3012 } else {
3013 // /* HeapReference<Class> */ temp1 = src->klass_
3014 __ movl(temp1, Address(src, class_offset));
3015 __ MaybeUnpoisonHeapReference(temp1);
3016 // Bail out if the source is not a non primitive array.
3017 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3018 __ movl(temp1, Address(temp1, component_offset));
3019 __ testl(temp1, temp1);
3020 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3021 __ MaybeUnpoisonHeapReference(temp1);
3022 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003023 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003024 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003025 }
3026
Roland Levillain0b671c02016-08-19 12:02:34 +01003027 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3028 if (length.Equals(Location::RegisterLocation(temp3))) {
3029 // When Baker read barriers are enabled, register `temp3`,
3030 // which in the present case contains the `length` parameter,
3031 // will be overwritten below. Make the `length` location
3032 // reference the original stack location; it will be moved
3033 // back to `temp3` later if necessary.
3034 DCHECK(length_arg.IsStackSlot());
3035 length = length_arg;
3036 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003037
Roland Levillain0b671c02016-08-19 12:02:34 +01003038 // /* HeapReference<Class> */ temp1 = dest->klass_
3039 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003040 invoke, temp1_loc, dest, class_offset, /* needs_null_check */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003041
Roland Levillain0b671c02016-08-19 12:02:34 +01003042 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3043 // Bail out if the destination is not a non primitive array.
3044 //
3045 // Register `temp1` is not trashed by the read barrier emitted
3046 // by GenerateFieldLoadWithBakerReadBarrier below, as that
3047 // method produces a call to a ReadBarrierMarkRegX entry point,
3048 // which saves all potentially live registers, including
3049 // temporaries such a `temp1`.
3050 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3051 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003052 invoke, temp2_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003053 __ testl(temp2, temp2);
3054 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3055 // If heap poisoning is enabled, `temp2` has been unpoisoned
3056 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3057 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3058 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3059 }
3060
3061 // For the same reason given earlier, `temp1` is not trashed by the
3062 // read barrier emitted by GenerateFieldLoadWithBakerReadBarrier below.
3063 // /* HeapReference<Class> */ temp2 = src->klass_
3064 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003065 invoke, temp2_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003066 // Note: if heap poisoning is on, we are comparing two unpoisoned references here.
3067 __ cmpl(temp1, temp2);
3068
3069 if (optimizations.GetDestinationIsTypedObjectArray()) {
3070 NearLabel do_copy;
3071 __ j(kEqual, &do_copy);
3072 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3073 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003074 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003075 // We do not need to emit a read barrier for the following
3076 // heap reference load, as `temp1` is only used in a
3077 // comparison with null below, and this reference is not
3078 // kept afterwards.
3079 __ cmpl(Address(temp1, super_offset), Immediate(0));
3080 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3081 __ Bind(&do_copy);
3082 } else {
3083 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3084 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003085 } else {
Roland Levillain0b671c02016-08-19 12:02:34 +01003086 // Non read barrier code.
3087
3088 // /* HeapReference<Class> */ temp1 = dest->klass_
3089 __ movl(temp1, Address(dest, class_offset));
3090 if (!optimizations.GetDestinationIsNonPrimitiveArray()) {
3091 __ MaybeUnpoisonHeapReference(temp1);
3092 // Bail out if the destination is not a non primitive array.
3093 // /* HeapReference<Class> */ temp2 = temp1->component_type_
3094 __ movl(temp2, Address(temp1, component_offset));
3095 __ testl(temp2, temp2);
3096 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3097 __ MaybeUnpoisonHeapReference(temp2);
3098 __ cmpw(Address(temp2, primitive_offset), Immediate(Primitive::kPrimNot));
3099 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3100 // Re-poison the heap reference to make the compare instruction below
3101 // compare two poisoned references.
3102 __ PoisonHeapReference(temp1);
3103 }
3104
3105 // Note: if heap poisoning is on, we are comparing two poisoned references here.
3106 __ cmpl(temp1, Address(src, class_offset));
3107
3108 if (optimizations.GetDestinationIsTypedObjectArray()) {
3109 NearLabel do_copy;
3110 __ j(kEqual, &do_copy);
3111 __ MaybeUnpoisonHeapReference(temp1);
3112 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3113 __ movl(temp1, Address(temp1, component_offset));
3114 __ MaybeUnpoisonHeapReference(temp1);
3115 __ cmpl(Address(temp1, super_offset), Immediate(0));
3116 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3117 __ Bind(&do_copy);
3118 } else {
3119 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
3120 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003121 }
3122 } else if (!optimizations.GetSourceIsNonPrimitiveArray()) {
3123 DCHECK(optimizations.GetDestinationIsNonPrimitiveArray());
3124 // Bail out if the source is not a non primitive array.
Roland Levillain0b671c02016-08-19 12:02:34 +01003125 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3126 // /* HeapReference<Class> */ temp1 = src->klass_
3127 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003128 invoke, temp1_loc, src, class_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003129 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3130 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00003131 invoke, temp1_loc, temp1, component_offset, /* needs_null_check */ false);
Roland Levillain0b671c02016-08-19 12:02:34 +01003132 __ testl(temp1, temp1);
3133 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3134 // If heap poisoning is enabled, `temp1` has been unpoisoned
3135 // by the the previous call to GenerateFieldLoadWithBakerReadBarrier.
3136 } else {
3137 // /* HeapReference<Class> */ temp1 = src->klass_
3138 __ movl(temp1, Address(src, class_offset));
3139 __ MaybeUnpoisonHeapReference(temp1);
3140 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3141 __ movl(temp1, Address(temp1, component_offset));
3142 __ testl(temp1, temp1);
3143 __ j(kEqual, intrinsic_slow_path->GetEntryLabel());
3144 __ MaybeUnpoisonHeapReference(temp1);
3145 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003146 __ cmpw(Address(temp1, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0b671c02016-08-19 12:02:34 +01003147 __ j(kNotEqual, intrinsic_slow_path->GetEntryLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003148 }
3149
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003150 const DataType::Type type = DataType::Type::kReference;
3151 const int32_t element_size = DataType::Size(type);
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003152
Roland Levillain0b671c02016-08-19 12:02:34 +01003153 // Compute the base source address in `temp1`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003154 GenSystemArrayCopyBaseAddress(GetAssembler(), type, src, src_pos, temp1);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003155
Roland Levillain0b671c02016-08-19 12:02:34 +01003156 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
3157 // If it is needed (in the case of the fast-path loop), the base
3158 // destination address is computed later, as `temp2` is used for
3159 // intermediate computations.
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003160
Roland Levillain0b671c02016-08-19 12:02:34 +01003161 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003162 if (length.IsStackSlot()) {
3163 // Location `length` is again pointing at a stack slot, as
3164 // register `temp3` (which was containing the length parameter
3165 // earlier) has been overwritten; restore it now
3166 DCHECK(length.Equals(length_arg));
3167 __ movl(temp3, Address(ESP, length.GetStackIndex()));
3168 length = Location::RegisterLocation(temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003169 }
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003170 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003171
Roland Levillain0b671c02016-08-19 12:02:34 +01003172 // SystemArrayCopy implementation for Baker read barriers (see
3173 // also CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier):
3174 //
3175 // if (src_ptr != end_ptr) {
3176 // uint32_t rb_state = Lockword(src->monitor_).ReadBarrierState();
3177 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003178 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain0b671c02016-08-19 12:02:34 +01003179 // if (is_gray) {
3180 // // Slow-path copy.
3181 // for (size_t i = 0; i != length; ++i) {
3182 // dest_array[dest_pos + i] =
3183 // MaybePoison(ReadBarrier::Mark(MaybeUnpoison(src_array[src_pos + i])));
3184 // }
3185 // } else {
3186 // // Fast-path copy.
3187 // do {
3188 // *dest_ptr++ = *src_ptr++;
3189 // } while (src_ptr != end_ptr)
3190 // }
3191 // }
3192
3193 NearLabel loop, done;
3194
3195 // Don't enter copy loop if `length == 0`.
3196 __ cmpl(temp1, temp3);
3197 __ j(kEqual, &done);
3198
Vladimir Marko953437b2016-08-24 08:30:46 +00003199 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003200 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
3201 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00003202 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
3203 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
3204 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
3205
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07003206 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00003207 // goto slow_path;
3208 // At this point, just do the "if" and make sure that flags are preserved until the branch.
3209 __ testb(Address(src, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain0b671c02016-08-19 12:02:34 +01003210
3211 // Load fence to prevent load-load reordering.
3212 // Note that this is a no-op, thanks to the x86 memory model.
3213 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3214
3215 // Slow path used to copy array when `src` is gray.
3216 SlowPathCode* read_barrier_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003217 new (codegen_->GetScopedAllocator()) ReadBarrierSystemArrayCopySlowPathX86(invoke);
Roland Levillain0b671c02016-08-19 12:02:34 +01003218 codegen_->AddSlowPath(read_barrier_slow_path);
3219
Vladimir Marko953437b2016-08-24 08:30:46 +00003220 // We have done the "if" of the gray bit check above, now branch based on the flags.
3221 __ j(kNotZero, read_barrier_slow_path->GetEntryLabel());
Roland Levillain0b671c02016-08-19 12:02:34 +01003222
3223 // Fast-path copy.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003224 // Compute the base destination address in `temp2`.
3225 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003226 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3227 // poison/unpoison.
3228 __ Bind(&loop);
3229 __ pushl(Address(temp1, 0));
3230 __ cfi().AdjustCFAOffset(4);
3231 __ popl(Address(temp2, 0));
3232 __ cfi().AdjustCFAOffset(-4);
3233 __ addl(temp1, Immediate(element_size));
3234 __ addl(temp2, Immediate(element_size));
3235 __ cmpl(temp1, temp3);
3236 __ j(kNotEqual, &loop);
3237
3238 __ Bind(read_barrier_slow_path->GetExitLabel());
3239 __ Bind(&done);
3240 } else {
3241 // Non read barrier code.
Roland Levillain0b671c02016-08-19 12:02:34 +01003242 // Compute the base destination address in `temp2`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003243 GenSystemArrayCopyBaseAddress(GetAssembler(), type, dest, dest_pos, temp2);
Roland Levillain0b671c02016-08-19 12:02:34 +01003244 // Compute the end source address in `temp3`.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003245 GenSystemArrayCopyEndAddress(GetAssembler(), type, length, temp1, temp3);
Roland Levillain0b671c02016-08-19 12:02:34 +01003246 // Iterate over the arrays and do a raw copy of the objects. We don't need to
3247 // poison/unpoison.
3248 NearLabel loop, done;
3249 __ cmpl(temp1, temp3);
3250 __ j(kEqual, &done);
3251 __ Bind(&loop);
3252 __ pushl(Address(temp1, 0));
3253 __ cfi().AdjustCFAOffset(4);
3254 __ popl(Address(temp2, 0));
3255 __ cfi().AdjustCFAOffset(-4);
3256 __ addl(temp1, Immediate(element_size));
3257 __ addl(temp2, Immediate(element_size));
3258 __ cmpl(temp1, temp3);
3259 __ j(kNotEqual, &loop);
3260 __ Bind(&done);
3261 }
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003262
3263 // We only need one card marking on the destination array.
Roland Levillain9cc0ea82017-03-16 11:25:59 +00003264 codegen_->MarkGCCard(temp1, temp2, dest, Register(kNoRegister), /* value_can_be_null */ false);
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003265
Roland Levillain0b671c02016-08-19 12:02:34 +01003266 __ Bind(intrinsic_slow_path->GetExitLabel());
Nicolas Geoffrayfea1abd2016-07-06 12:09:12 +01003267}
3268
Nicolas Geoffray331605a2017-03-01 11:01:41 +00003269void IntrinsicLocationsBuilderX86::VisitIntegerValueOf(HInvoke* invoke) {
3270 InvokeRuntimeCallingConvention calling_convention;
3271 IntrinsicVisitor::ComputeIntegerValueOfLocations(
3272 invoke,
3273 codegen_,
3274 Location::RegisterLocation(EAX),
3275 Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3276}
3277
3278void IntrinsicCodeGeneratorX86::VisitIntegerValueOf(HInvoke* invoke) {
3279 IntrinsicVisitor::IntegerValueOfInfo info = IntrinsicVisitor::ComputeIntegerValueOfInfo();
3280 LocationSummary* locations = invoke->GetLocations();
3281 X86Assembler* assembler = GetAssembler();
3282
3283 Register out = locations->Out().AsRegister<Register>();
3284 InvokeRuntimeCallingConvention calling_convention;
3285 if (invoke->InputAt(0)->IsConstant()) {
3286 int32_t value = invoke->InputAt(0)->AsIntConstant()->GetValue();
3287 if (value >= info.low && value <= info.high) {
3288 // Just embed the j.l.Integer in the code.
3289 ScopedObjectAccess soa(Thread::Current());
3290 mirror::Object* boxed = info.cache->Get(value + (-info.low));
3291 DCHECK(boxed != nullptr && Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(boxed));
3292 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(boxed));
3293 __ movl(out, Immediate(address));
3294 } else {
3295 // Allocate and initialize a new j.l.Integer.
3296 // TODO: If we JIT, we could allocate the j.l.Integer now, and store it in the
3297 // JIT object table.
3298 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3299 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3300 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3301 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3302 __ movl(Address(out, info.value_offset), Immediate(value));
3303 }
3304 } else {
3305 Register in = locations->InAt(0).AsRegister<Register>();
3306 // Check bounds of our cache.
3307 __ leal(out, Address(in, -info.low));
3308 __ cmpl(out, Immediate(info.high - info.low + 1));
3309 NearLabel allocate, done;
3310 __ j(kAboveEqual, &allocate);
3311 // If the value is within the bounds, load the j.l.Integer directly from the array.
3312 uint32_t data_offset = mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3313 uint32_t address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.cache));
3314 __ movl(out, Address(out, TIMES_4, data_offset + address));
3315 __ MaybeUnpoisonHeapReference(out);
3316 __ jmp(&done);
3317 __ Bind(&allocate);
3318 // Otherwise allocate and initialize a new j.l.Integer.
3319 address = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(info.integer));
3320 __ movl(calling_convention.GetRegisterAt(0), Immediate(address));
3321 codegen_->InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
3322 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
3323 __ movl(Address(out, info.value_offset), in);
3324 __ Bind(&done);
3325 }
3326}
3327
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003328void IntrinsicLocationsBuilderX86::VisitThreadInterrupted(HInvoke* invoke) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003329 LocationSummary* locations =
3330 new (allocator_) LocationSummary(invoke, LocationSummary::kNoCall, kIntrinsified);
Nicolas Geoffray365719c2017-03-08 13:11:50 +00003331 locations->SetOut(Location::RequiresRegister());
3332}
3333
3334void IntrinsicCodeGeneratorX86::VisitThreadInterrupted(HInvoke* invoke) {
3335 X86Assembler* assembler = GetAssembler();
3336 Register out = invoke->GetLocations()->Out().AsRegister<Register>();
3337 Address address = Address::Absolute(Thread::InterruptedOffset<kX86PointerSize>().Int32Value());
3338 NearLabel done;
3339 __ fs()->movl(out, address);
3340 __ testl(out, out);
3341 __ j(kEqual, &done);
3342 __ fs()->movl(address, Immediate(0));
3343 codegen_->MemoryFence();
3344 __ Bind(&done);
3345}
3346
3347
Aart Bik2f9fcc92016-03-01 15:16:54 -08003348UNIMPLEMENTED_INTRINSIC(X86, MathRoundDouble)
Vladimir Marko4ee8e292017-06-02 15:39:30 +00003349UNIMPLEMENTED_INTRINSIC(X86, ReferenceGetReferent)
Aart Bik2f9fcc92016-03-01 15:16:54 -08003350UNIMPLEMENTED_INTRINSIC(X86, FloatIsInfinite)
3351UNIMPLEMENTED_INTRINSIC(X86, DoubleIsInfinite)
3352UNIMPLEMENTED_INTRINSIC(X86, IntegerHighestOneBit)
3353UNIMPLEMENTED_INTRINSIC(X86, LongHighestOneBit)
3354UNIMPLEMENTED_INTRINSIC(X86, IntegerLowestOneBit)
3355UNIMPLEMENTED_INTRINSIC(X86, LongLowestOneBit)
Mark Mendell09ed1a32015-03-25 08:30:06 -04003356
Aart Bikff7d89c2016-11-07 08:49:28 -08003357UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOf);
3358UNIMPLEMENTED_INTRINSIC(X86, StringStringIndexOfAfter);
Aart Bik71bf7b42016-11-16 10:17:46 -08003359UNIMPLEMENTED_INTRINSIC(X86, StringBufferAppend);
3360UNIMPLEMENTED_INTRINSIC(X86, StringBufferLength);
3361UNIMPLEMENTED_INTRINSIC(X86, StringBufferToString);
3362UNIMPLEMENTED_INTRINSIC(X86, StringBuilderAppend);
3363UNIMPLEMENTED_INTRINSIC(X86, StringBuilderLength);
3364UNIMPLEMENTED_INTRINSIC(X86, StringBuilderToString);
Aart Bikff7d89c2016-11-07 08:49:28 -08003365
Aart Bik0e54c012016-03-04 12:08:31 -08003366// 1.8.
3367UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddInt)
3368UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndAddLong)
3369UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetInt)
3370UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetLong)
3371UNIMPLEMENTED_INTRINSIC(X86, UnsafeGetAndSetObject)
Aart Bik0e54c012016-03-04 12:08:31 -08003372
Aart Bik2f9fcc92016-03-01 15:16:54 -08003373UNREACHABLE_INTRINSICS(X86)
Roland Levillain4d027112015-07-01 15:41:14 +01003374
3375#undef __
3376
Mark Mendell09ed1a32015-03-25 08:30:06 -04003377} // namespace x86
3378} // namespace art